]> git.zerfleddert.de Git - usb-driver/blame_incremental - usb-driver.c
pass pointer to usb-driver structure through impact
[usb-driver] / usb-driver.c
... / ...
CommitLineData
1/* libusb/ppdev connector for XILINX impact
2 *
3 * Copyright (c) 2007 Michael Gernoth <michael@gernoth.net>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#define _GNU_SOURCE 1
25
26#include <dlfcn.h>
27#include <stdarg.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/time.h>
35#include <stdio.h>
36#include <signal.h>
37#include <errno.h>
38#include <inttypes.h>
39#include <sys/ioctl.h>
40#include <sys/utsname.h>
41#include <bits/wordsize.h>
42#include "usb-driver.h"
43#include "config.h"
44#include "xpcu.h"
45
46static int (*ioctl_func) (int, int, void *) = NULL;
47static int windrvrfd = -1;
48static unsigned long ppbase = 0;
49static unsigned long ecpbase = 0;
50static struct parport_config *pport = NULL;
51static FILE *modulesfp = NULL;
52static FILE *baseaddrfp = NULL;
53static int baseaddrnum = 0;
54static int modules_read = 0;
55
56#define NO_WINDRVR 1
57
58void hexdump(unsigned char *buf, int len) {
59 int i;
60
61 for(i=0; i<len; i++) {
62 fprintf(stderr,"%02x ", buf[i]);
63 if ((i % 16) == 15)
64 fprintf(stderr,"\n");
65 }
66 fprintf(stderr,"\n");
67}
68
69
70static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
71 struct header_struct* wdheader = (struct header_struct*)wdioctl;
72 struct version_struct *version;
73 int ret = 0;
74
75 if (wdheader->magic != MAGIC) {
76 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
77 return (*ioctl_func) (fd, request, wdioctl);
78 }
79
80 switch(request & ~(0xc0000000)) {
81 case VERSION:
82 version = (struct version_struct*)(wdheader->data);
83 strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION);
84 version->versionul = 802;
85 DPRINTF("VERSION\n");
86 break;
87
88 case LICENSE:
89 DPRINTF("LICENSE\n");
90 break;
91
92 case CARD_REGISTER_OLD:
93 case CARD_REGISTER:
94 DPRINTF("CARD_REGISTER\n");
95 {
96 struct card_register* cr = (struct card_register*)(wdheader->data);
97
98 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
99 cr->Card.dwItems,
100 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
101 cr->Card.Item[0].I.IO.dwBytes,
102 cr->Card.Item[0].I.IO.dwBar);
103
104 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
105 cr->Card.dwItems,
106 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
107 cr->Card.Item[1].I.IO.dwBytes,
108 cr->Card.Item[1].I.IO.dwBar);
109#ifndef NO_WINDRVR
110 ret = (*ioctl_func) (fd, request, wdioctl);
111#else
112
113 pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
114 if (!pport)
115 break;
116
117 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
118
119 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
120
121 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
122 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
123
124 if (ret >= 0) {
125 cr->hCard = ret;
126 } else {
127 cr->hCard = 0;
128 }
129#endif
130 DPRINTF("<-hCard: %lu\n", cr->hCard);
131 }
132 break;
133
134 case USB_TRANSFER:
135 DPRINTF("USB_TRANSFER\n");
136 {
137 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
138
139#ifdef DEBUG
140 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
141 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
142 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
143 if (ut->dwPipeNum == 0) {
144 DPRINTF("-> setup packet: ");
145 hexdump(ut->SetupPacket, 8);
146 }
147
148 if (!ut->fRead && ut->dwBufferSize)
149 {
150 hexdump(ut->pBuffer, ut->dwBufferSize);
151 }
152#endif
153
154#ifndef NO_WINDRVR
155 ret = (*ioctl_func) (fd, request, wdioctl);
156#else
157 ret = xpcu_transfer(ut);
158#endif
159
160#ifdef DEBUG
161 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
162 if (ut->fRead && ut->dwBytesTransferred)
163 {
164 DPRINTF("<- Read: ");
165 hexdump(ut->pBuffer, ut->dwBytesTransferred);
166 }
167#endif
168 }
169 break;
170
171 case INT_ENABLE_OLD:
172 case INT_ENABLE:
173 DPRINTF("INT_ENABLE\n");
174 {
175 struct interrupt *it = (struct interrupt*)(wdheader->data);
176
177 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
178 it->hInterrupt, it->dwOptions,
179 it->dwCmds, it->fEnableOk, it->dwCounter,
180 it->dwLost, it->fStopped);
181
182#ifndef NO_WINDRVR
183 ret = (*ioctl_func) (fd, request, wdioctl);
184#else
185 ret = xpcu_int_state(it, ENABLE_INTERRUPT);
186#endif
187
188 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
189 it->hInterrupt, it->dwOptions,
190 it->dwCmds, it->fEnableOk, it->dwCounter,
191 it->dwLost, it->fStopped);
192 }
193
194 break;
195
196 case INT_DISABLE:
197 DPRINTF("INT_DISABLE\n");
198 {
199 struct interrupt *it = (struct interrupt*)(wdheader->data);
200
201 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
202 it->hInterrupt, it->dwOptions,
203 it->dwCmds, it->fEnableOk, it->dwCounter,
204 it->dwLost, it->fStopped);
205#ifndef NO_WINDRVR
206 ret = (*ioctl_func) (fd, request, wdioctl);
207#else
208 ret = xpcu_int_state(it, DISABLE_INTERRUPT);
209#endif
210 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
211 it->hInterrupt, it->dwOptions,
212 it->dwCmds, it->fEnableOk, it->dwCounter,
213 it->dwLost, it->fStopped);
214 }
215 break;
216
217 case USB_SET_INTERFACE:
218 DPRINTF("USB_SET_INTERFACE\n");
219 {
220 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
221
222 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
223 usi->dwUniqueID, usi->dwInterfaceNum,
224 usi->dwAlternateSetting, usi->dwOptions);
225#ifndef NO_WINDRVR
226 ret = (*ioctl_func) (fd, request, wdioctl);
227#else
228 ret = xpcu_set_interface(usi);
229#endif
230 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
231 usi->dwUniqueID, usi->dwInterfaceNum,
232 usi->dwAlternateSetting, usi->dwOptions);
233 }
234 break;
235
236 case USB_GET_DEVICE_DATA_OLD:
237 case USB_GET_DEVICE_DATA:
238 DPRINTF("USB_GET_DEVICE_DATA\n");
239 {
240 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
241
242 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
243 ugdd->dwUniqueID, ugdd->dwBytes,
244 ugdd->dwOptions);
245
246 ret = xpcu_deviceinfo(ugdd);
247
248 }
249 break;
250
251 case EVENT_REGISTER_OLD:
252 case EVENT_REGISTER:
253 DPRINTF("EVENT_REGISTER\n");
254 {
255 struct event *e = (struct event*)(wdheader->data);
256#ifdef DEBUG
257 int i;
258#endif
259
260 DPRINTF("-> handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
261 e->handle, e->dwAction,
262 e->dwStatus, e->dwEventId, e->dwCardType,
263 e->hKernelPlugIn, e->dwOptions,
264 e->u.Usb.deviceId.dwVendorId,
265 e->u.Usb.deviceId.dwProductId,
266 e->u.Usb.dwUniqueID, e->dwEventVer,
267 e->dwNumMatchTables);
268
269#ifndef NO_WINDRVR
270 ret = (*ioctl_func) (fd, request, wdioctl);
271#else
272 ret = xpcu_find(e);
273#endif
274
275#ifdef DEBUG
276 DPRINTF("<- handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
277 e->handle, e->dwAction,
278 e->dwStatus, e->dwEventId, e->dwCardType,
279 e->hKernelPlugIn, e->dwOptions,
280 e->u.Usb.deviceId.dwVendorId,
281 e->u.Usb.deviceId.dwProductId,
282 e->u.Usb.dwUniqueID, e->dwEventVer,
283 e->dwNumMatchTables);
284
285 for (i = 0; i < e->dwNumMatchTables; i++)
286 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
287 e->matchTables[i].VendorId,
288 e->matchTables[i].ProductId,
289 e->matchTables[i].bDeviceClass,
290 e->matchTables[i].bDeviceSubClass,
291 e->matchTables[i].bInterfaceClass,
292 e->matchTables[i].bInterfaceSubClass,
293 e->matchTables[i].bInterfaceProtocol);
294#endif
295 }
296 break;
297
298 case TRANSFER_OLD:
299 case TRANSFER:
300 DPRINTF("TRANSFER\n");
301 {
302 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
303
304#ifndef NO_WINDRVR
305 ret = (*ioctl_func) (fd, request, wdioctl);
306#else
307 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
308#endif
309 }
310 break;
311
312 case MULTI_TRANSFER_OLD:
313 case MULTI_TRANSFER:
314 DPRINTF("MULTI_TRANSFER\n");
315 {
316 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
317 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
318#ifndef NO_WINDRVR
319 ret = (*ioctl_func) (fd, request, wdioctl);
320#else
321 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
322#endif
323 }
324 break;
325
326 case EVENT_UNREGISTER:
327 {
328 struct event *e = (struct event*)(wdheader->data);
329
330 DPRINTF("EVENT_UNREGISTER\n");
331#ifndef NO_WINDRVR
332 ret = (*ioctl_func) (fd, request, wdioctl);
333#else
334 ret = xpcu_close(e);
335#endif
336 }
337 break;
338
339 case INT_WAIT:
340 DPRINTF("INT_WAIT\n");
341 {
342 struct interrupt *it = (struct interrupt*)(wdheader->data);
343
344 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
345 it->hInterrupt, it->dwOptions,
346 it->dwCmds, it->fEnableOk, it->dwCounter,
347 it->dwLost, it->fStopped);
348
349#ifndef NO_WINDRVR
350 ret = (*ioctl_func) (fd, request, wdioctl);
351#else
352 ret = xpcu_int_wait(it);
353#endif
354
355 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
356 it->hInterrupt, it->dwOptions, it->dwCmds,
357 it->fEnableOk, it->dwCounter, it->dwLost,
358 it->fStopped);
359 }
360 break;
361
362 case CARD_UNREGISTER:
363 DPRINTF("CARD_UNREGISTER\n");
364 {
365 struct card_register* cr = (struct card_register*)(wdheader->data);
366
367 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
368 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
369 cr->Card.Item[0].I.IO.dwBytes,
370 cr->Card.Item[0].I.IO.dwBar);
371
372 DPRINTF("-> hCard: %lu\n", cr->hCard);
373
374#ifndef NO_WINDRVR
375 ret = (*ioctl_func) (fd, request, wdioctl);
376#else
377 if (pport)
378 pport->close(cr->hCard);
379
380 pport = NULL;
381#endif
382 }
383 break;
384
385 case EVENT_PULL:
386 DPRINTF("EVENT_PULL\n");
387 {
388 struct event *e = (struct event*)(wdheader->data);
389#ifdef DEBUG
390 int i;
391
392 DPRINTF("-> handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
393 e->handle, e->dwAction, e->dwStatus,
394 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
395 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
396 e->u.Usb.deviceId.dwProductId,
397 e->u.Usb.dwUniqueID, e->dwEventVer,
398 e->dwNumMatchTables);
399
400 for (i = 0; i < e->dwNumMatchTables; i++)
401 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
402 e->matchTables[i].VendorId,
403 e->matchTables[i].ProductId,
404 e->matchTables[i].bDeviceClass,
405 e->matchTables[i].bDeviceSubClass,
406 e->matchTables[i].bInterfaceClass,
407 e->matchTables[i].bInterfaceSubClass,
408 e->matchTables[i].bInterfaceProtocol);
409#endif
410
411#ifndef NO_WINDRVR
412 ret = (*ioctl_func) (fd, request, wdioctl);
413#else
414 ret = xpcu_found(e);
415#endif
416
417#ifdef DEBUG
418 DPRINTF("<- handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
419 e->handle, e->dwAction, e->dwStatus,
420 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
421 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
422 e->u.Usb.deviceId.dwProductId,
423 e->u.Usb.dwUniqueID, e->dwEventVer,
424 e->dwNumMatchTables);
425
426 for (i = 0; i < e->dwNumMatchTables; i++)
427 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
428 e->matchTables[i].VendorId,
429 e->matchTables[i].ProductId,
430 e->matchTables[i].bDeviceClass,
431 e->matchTables[i].bDeviceSubClass,
432 e->matchTables[i].bInterfaceClass,
433 e->matchTables[i].bInterfaceSubClass,
434 e->matchTables[i].bInterfaceProtocol);
435#endif
436
437 }
438 break;
439
440 default:
441 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
442#ifndef NO_WINDRVR
443 ret = (*ioctl_func) (fd, request, wdioctl);
444#endif
445 break;
446 }
447
448 return ret;
449}
450
451int ioctl(int fd, unsigned long int request, ...) {
452 va_list args;
453 void *argp;
454 int ret;
455
456 if (!ioctl_func)
457 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
458
459 va_start (args, request);
460 argp = va_arg (args, void *);
461 va_end (args);
462
463 if (fd == windrvrfd)
464 ret = do_wdioctl(fd, request, argp);
465 else
466 ret = (*ioctl_func) (fd, request, argp);
467
468 return ret;
469}
470
471int open (const char *pathname, int flags, ...) {
472 static int (*func) (const char *, int, mode_t) = NULL;
473 mode_t mode = 0;
474 va_list args;
475 int fd;
476
477 if (!func)
478 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
479
480 if (flags & O_CREAT) {
481 va_start(args, flags);
482 mode = va_arg(args, mode_t);
483 va_end(args);
484 }
485
486 if (!strcmp (pathname, "/dev/windrvr6")) {
487 DPRINTF("opening windrvr6\n");
488#ifdef NO_WINDRVR
489 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
490#else
491 windrvrfd = fd = (*func) (pathname, flags, mode);
492#endif
493
494 return fd;
495 }
496
497 return (*func) (pathname, flags, mode);
498}
499
500int close(int fd) {
501 static int (*func) (int) = NULL;
502
503 if (!func)
504 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
505
506 if (fd == windrvrfd && windrvrfd >= 0) {
507 DPRINTF("close windrvrfd\n");
508 windrvrfd = -1;
509 }
510
511 return (*func) (fd);
512}
513
514FILE *fopen(const char *path, const char *mode) {
515 FILE *ret;
516 static FILE* (*func) (const char*, const char*) = NULL;
517 char buf[256];
518 int i;
519
520 if (!func)
521 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
522
523 for (i = 0; i < 4; i++) {
524 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
525 if (!strcmp(path, buf)) {
526 DPRINTF("open base-addr of parport%d\n", i);
527 if (config_is_real_pport(i)) {
528 ret = (*func) (path, mode);
529 } else {
530 ret = (*func) ("/dev/null", mode);
531 }
532
533 if (ret) {
534 baseaddrfp = ret;
535 baseaddrnum = i;
536 }
537
538 return ret;
539 }
540 }
541
542 ret = (*func) (path, mode);
543
544 if (!strcmp(path, "/proc/modules")) {
545 DPRINTF("opening /proc/modules\n");
546#ifdef NO_WINDRVR
547 modulesfp = ret;
548 modules_read = 0;
549#endif
550 }
551
552 return ret;
553}
554
555char *fgets(char *s, int size, FILE *stream) {
556 static char* (*func) (char*, int, FILE*) = NULL;
557 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
558 char buf[256];
559 char *ret = NULL;
560
561
562 if (!func)
563 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
564
565 if (modulesfp == stream) {
566 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
567 strcpy(s, modules[modules_read]);
568 ret = s;
569 modules_read++;
570 }
571 } else if (baseaddrfp == stream) {
572 snprintf(s, sizeof(buf), "%d\t%d\n",
573 (baseaddrnum) * 0x10,
574 ((baseaddrnum) * 0x10) + 0x400);
575 ret = s;
576 } else {
577 ret = (*func)(s,size,stream);
578 }
579
580 return ret;
581}
582
583int fclose(FILE *fp) {
584 static int (*func) (FILE*) = NULL;
585
586 if (!func)
587 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
588
589 if (fp == modulesfp) {
590 modulesfp = NULL;
591 }
592
593 if (fp == baseaddrfp) {
594 baseaddrfp = NULL;
595 }
596
597 return (*func)(fp);
598}
599
600int access(const char *pathname, int mode) {
601 static int (*func) (const char*, int);
602
603 if (!func)
604 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
605
606 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
607 return 0;
608 } else {
609 return (*func)(pathname, mode);
610 }
611}
612
613#if __WORDSIZE == 32
614int uname (struct utsname *__name) {
615 static int (*func) (struct utsname*);
616 int ret;
617
618 if (!func)
619 func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname");
620
621 ret = (*func)(__name);
622
623 if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) {
624 strcpy(__name->machine, "i686");
625 }
626
627 return ret;
628}
629#endif
Impressum, Datenschutz