]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
6a7df90d44f052210d5c54b970131867b92d29c5
1 /* libusb/ppdev connector for XILINX impact
3 * Copyright (c) 2007 Michael Gernoth <michael@gernoth.net>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
32 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/utsname.h>
41 #include <bits/wordsize.h>
44 #include "usb-driver.h"
48 static int (*ioctl_func
) (int, int, void *) = NULL
;
49 static int *windrvrfds
= NULL
;
50 static int windrvrfds_count
= 0;
51 static unsigned long ppbase
= 0;
52 static unsigned long ecpbase
= 0;
53 static struct parport_config
*pport
= NULL
;
54 static FILE *modulesfp
= NULL
;
55 static FILE *baseaddrfp
= NULL
;
56 static int baseaddrnum
= 0;
57 static int modules_read
= 0;
61 void hexdump(unsigned char *buf
, int len
, char *prefix
) {
64 fprintf(stderr
, "%s ", prefix
);
65 for(i
=0; i
<len
; i
++) {
66 fprintf(stderr
,"%02x ", buf
[i
]);
68 fprintf(stderr
,"\n%s ", prefix
);
74 static int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
75 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
76 struct version_struct
*version
;
79 if (wdheader
->magic
!= MAGIC
) {
80 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
81 return (*ioctl_func
) (fd
, request
, wdioctl
);
84 switch(request
& ~(0xc0000000)) {
86 version
= (struct version_struct
*)(wdheader
->data
);
87 strcpy(version
->version
, "libusb-driver.so version: " USB_DRIVER_VERSION
);
88 version
->versionul
= 802;
96 case CARD_REGISTER_OLD
:
98 DPRINTF("CARD_REGISTER\n");
100 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
102 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
104 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
105 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
106 cr
->Card
.Item
[0].I
.IO
.dwBar
);
108 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
110 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
111 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
112 cr
->Card
.Item
[1].I
.IO
.dwBar
);
114 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
117 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
121 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
123 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
125 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
126 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
134 DPRINTF("<-hCard: %lu\n", cr
->hCard
);
139 DPRINTF("USB_TRANSFER\n");
141 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
144 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
145 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
146 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
147 if (ut
->dwPipeNum
== 0) {
148 DPRINTF("-> setup packet:");
149 hexdump(ut
->SetupPacket
, 8, "");
152 if (!ut
->fRead
&& ut
->dwBufferSize
)
154 hexdump(ut
->pBuffer
, ut
->dwBufferSize
, "->");
159 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
161 ret
= xpcu_transfer(ut
);
165 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
166 if (ut
->fRead
&& ut
->dwBytesTransferred
)
168 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
, "<-");
176 DPRINTF("INT_ENABLE\n");
178 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
180 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
181 it
->hInterrupt
, it
->dwOptions
,
182 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
183 it
->dwLost
, it
->fStopped
);
186 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
188 ret
= xpcu_int_state(it
, ENABLE_INTERRUPT
);
191 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
192 it
->hInterrupt
, it
->dwOptions
,
193 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
194 it
->dwLost
, it
->fStopped
);
200 DPRINTF("INT_DISABLE\n");
202 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
204 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
205 it
->hInterrupt
, it
->dwOptions
,
206 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
207 it
->dwLost
, it
->fStopped
);
209 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
211 ret
= xpcu_int_state(it
, DISABLE_INTERRUPT
);
213 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
214 it
->hInterrupt
, it
->dwOptions
,
215 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
216 it
->dwLost
, it
->fStopped
);
220 case USB_SET_INTERFACE
:
221 DPRINTF("USB_SET_INTERFACE\n");
223 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
225 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
226 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
227 usi
->dwAlternateSetting
, usi
->dwOptions
);
229 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
231 ret
= xpcu_set_interface(usi
);
233 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
234 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
235 usi
->dwAlternateSetting
, usi
->dwOptions
);
239 case USB_GET_DEVICE_DATA_OLD
:
240 case USB_GET_DEVICE_DATA
:
241 DPRINTF("USB_GET_DEVICE_DATA\n");
243 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
245 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
246 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
249 ret
= xpcu_deviceinfo(ugdd
);
254 case EVENT_REGISTER_OLD
:
256 DPRINTF("EVENT_REGISTER\n");
258 struct event
*e
= (struct event
*)(wdheader
->data
);
263 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",
264 e
->handle
, e
->dwAction
,
265 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
266 e
->hKernelPlugIn
, e
->dwOptions
,
267 e
->u
.Usb
.deviceId
.dwVendorId
,
268 e
->u
.Usb
.deviceId
.dwProductId
,
269 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
270 e
->dwNumMatchTables
);
273 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
279 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",
280 e
->handle
, e
->dwAction
,
281 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
282 e
->hKernelPlugIn
, e
->dwOptions
,
283 e
->u
.Usb
.deviceId
.dwVendorId
,
284 e
->u
.Usb
.deviceId
.dwProductId
,
285 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
286 e
->dwNumMatchTables
);
288 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
289 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
290 e
->matchTables
[i
].VendorId
,
291 e
->matchTables
[i
].ProductId
,
292 e
->matchTables
[i
].bDeviceClass
,
293 e
->matchTables
[i
].bDeviceSubClass
,
294 e
->matchTables
[i
].bInterfaceClass
,
295 e
->matchTables
[i
].bInterfaceSubClass
,
296 e
->matchTables
[i
].bInterfaceProtocol
);
303 DPRINTF("TRANSFER\n");
305 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
308 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
310 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
315 case MULTI_TRANSFER_OLD
:
317 DPRINTF("MULTI_TRANSFER\n");
319 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
320 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
322 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
324 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
329 case EVENT_UNREGISTER
:
331 struct event
*e
= (struct event
*)(wdheader
->data
);
333 DPRINTF("EVENT_UNREGISTER\n");
335 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
343 DPRINTF("INT_WAIT\n");
345 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
347 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
348 it
->hInterrupt
, it
->dwOptions
,
349 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
350 it
->dwLost
, it
->fStopped
);
353 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
355 ret
= xpcu_int_wait(it
);
358 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
359 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
360 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
365 case CARD_UNREGISTER
:
366 DPRINTF("CARD_UNREGISTER\n");
368 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
370 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
371 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
372 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
373 cr
->Card
.Item
[0].I
.IO
.dwBar
);
375 DPRINTF("-> hCard: %lu\n", cr
->hCard
);
378 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
381 pport
->close(cr
->hCard
);
389 DPRINTF("EVENT_PULL\n");
391 struct event
*e
= (struct event
*)(wdheader
->data
);
395 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",
396 e
->handle
, e
->dwAction
, e
->dwStatus
,
397 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
398 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
399 e
->u
.Usb
.deviceId
.dwProductId
,
400 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
401 e
->dwNumMatchTables
);
403 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
404 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
405 e
->matchTables
[i
].VendorId
,
406 e
->matchTables
[i
].ProductId
,
407 e
->matchTables
[i
].bDeviceClass
,
408 e
->matchTables
[i
].bDeviceSubClass
,
409 e
->matchTables
[i
].bInterfaceClass
,
410 e
->matchTables
[i
].bInterfaceSubClass
,
411 e
->matchTables
[i
].bInterfaceProtocol
);
415 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
421 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",
422 e
->handle
, e
->dwAction
, e
->dwStatus
,
423 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
424 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
425 e
->u
.Usb
.deviceId
.dwProductId
,
426 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
427 e
->dwNumMatchTables
);
429 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
430 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
431 e
->matchTables
[i
].VendorId
,
432 e
->matchTables
[i
].ProductId
,
433 e
->matchTables
[i
].bDeviceClass
,
434 e
->matchTables
[i
].bDeviceSubClass
,
435 e
->matchTables
[i
].bInterfaceClass
,
436 e
->matchTables
[i
].bInterfaceSubClass
,
437 e
->matchTables
[i
].bInterfaceProtocol
);
444 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
446 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
454 int ioctl(int fd
, unsigned long int request
, ...) {
460 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
462 va_start (args
, request
);
463 argp
= va_arg (args
, void *);
466 for (i
= 0; i
< windrvrfds_count
; i
++) {
467 if (fd
== windrvrfds
[i
])
468 return do_wdioctl(fd
, request
, argp
);
471 return (*ioctl_func
) (fd
, request
, argp
);
474 int open (const char *pathname
, int flags
, ...) {
475 static int (*func
) (const char *, int, mode_t
) = NULL
;
481 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
483 if (flags
& O_CREAT
) {
484 va_start(args
, flags
);
485 mode
= va_arg(args
, mode_t
);
489 if (!strcmp (pathname
, "/dev/windrvr6")) {
490 DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count
);
491 windrvrfds
= realloc(windrvrfds
, sizeof(int) * (++windrvrfds_count
));
496 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) ("/dev/null", flags
, mode
);
498 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) (pathname
, flags
, mode
);
504 return (*func
) (pathname
, flags
, mode
);
508 static int (*func
) (int) = NULL
;
512 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
514 for (i
= 0; i
< windrvrfds_count
; i
++) {
515 if (fd
== windrvrfds
[i
] && windrvrfds
[i
] >= 0) {
516 int remaining
= windrvrfds_count
- (i
+ 1);
517 DPRINTF("close windrvr6 (%d)\n", i
);
519 memmove(&(windrvrfds
[i
]), &(windrvrfds
[i
+1]), remaining
* sizeof(int));
520 windrvrfds
= realloc(windrvrfds
, sizeof(int) * --windrvrfds_count
);
521 if (!windrvrfds_count
)
530 FILE *fopen(const char *path
, const char *mode
) {
532 static FILE* (*func
) (const char*, const char*) = NULL
;
537 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
539 for (i
= 0; i
< 4; i
++) {
540 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
541 if (!strcmp(path
, buf
)) {
542 DPRINTF("open base-addr of parport%d\n", i
);
543 if (config_is_real_pport(i
)) {
544 ret
= (*func
) (path
, mode
);
546 ret
= (*func
) ("/dev/null", mode
);
558 ret
= (*func
) (path
, mode
);
560 if (!strcmp(path
, "/proc/modules")) {
561 DPRINTF("opening /proc/modules\n");
562 if (!ret
&& errno
== ENOENT
) {
563 /* Hmm.. there appears to be no /proc/modules file
565 ret
= (*func
)("/dev/null", mode
);
566 DPRINTF("No /proc/modules -- faking\n");
577 char *fgets(char *s
, int size
, FILE *stream
) {
578 static char* (*func
) (char*, int, FILE*) = NULL
;
579 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
585 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
587 if (modulesfp
== stream
) {
588 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
589 strcpy(s
, modules
[modules_read
]);
593 } else if (baseaddrfp
== stream
) {
594 snprintf(s
, sizeof(buf
), "%d\t%d\n",
595 (baseaddrnum
) * 0x10,
596 ((baseaddrnum
) * 0x10) + 0x400);
599 ret
= (*func
)(s
,size
,stream
);
605 int fclose(FILE *fp
) {
606 static int (*func
) (FILE*) = NULL
;
609 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
611 if (fp
== modulesfp
) {
615 if (fp
== baseaddrfp
) {
622 int access(const char *pathname
, int mode
) {
623 static int (*func
) (const char*, int);
626 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
628 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
631 return (*func
)(pathname
, mode
);
636 /* USB cable sharing needs to overload semop, TODO! */
637 int semop (int __semid
, struct sembuf
*__sops
, size_t __nsops
) {
638 static int (*func
) (int, struct sembuf
*, size_t) = NULL
;
642 func
= (int (*) (int, struct sembuf
*, size_t)) dlsym(RTLD_NEXT
, "semop");
644 fprintf(stderr
,"semop: semid: 0x%X, elements: %d\n", __semid
, __nsops
);
645 for (i
= 0; i
< __nsops
; i
++) {
646 fprintf(stderr
, " num: %u, op: %d, flg: %d\n", __sops
[i
].sem_num
, __sops
[i
].sem_op
, __sops
[i
].sem_flg
);
647 if (__sops
[i
].sem_op
< 0) {
648 fprintf(stderr
, "SEMAPHORE LOCK\n");
650 fprintf(stderr
, "SEMAPHORE UNLOCK\n");
654 return (*func
)(__semid
, __sops
, __nsops
);
659 int uname (struct utsname
*__name
) {
660 static int (*func
) (struct utsname
*);
664 func
= (int (*) (struct utsname
*)) dlsym(RTLD_NEXT
, "uname");
666 ret
= (*func
)(__name
);
668 if (ret
== 0 && (!strcmp(__name
->machine
, "x86_64"))) {
669 strcpy(__name
->machine
, "i686");
677 * Ugly hack for ISE 12. They don't seem to open /proc/modules with
679 * echo '_Z14isModuleLoadedPci' | c++filt
681 int _Z14isModuleLoadedPci(char *module_name
, int i
) {
682 DPRINTF("_Z14isModuleLoadedPci: Checking for module %s (%d)\n", module_name
, i
);