]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
44a280b1e94704f48a30f7e1eb34e64e9a5fdf8e
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>
45 #include <linux/personality.h>
46 #include "usb-driver.h"
50 static int (*ioctl_func
) (int, int, void *) = NULL
;
51 static int *windrvrfds
= NULL
;
52 static int windrvrfds_count
= 0;
53 static unsigned long ppbase
= 0;
54 static unsigned long ecpbase
= 0;
55 static struct parport_config
*pport
= NULL
;
56 static FILE *modulesfp
= NULL
;
57 static FILE *baseaddrfp
= NULL
;
58 static int baseaddrnum
= 0;
59 static int modules_read
= 0;
63 void hexdump(unsigned char *buf
, int len
, char *prefix
) {
66 fprintf(stderr
, "%s ", prefix
);
67 for(i
=0; i
<len
; i
++) {
68 fprintf(stderr
,"%02x ", buf
[i
]);
70 fprintf(stderr
,"\n%s ", prefix
);
76 static int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
77 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
78 struct version_struct
*version
;
81 if (wdheader
->magic
!= MAGIC
) {
82 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
83 return (*ioctl_func
) (fd
, request
, wdioctl
);
86 switch(request
& ~(0xc0000000)) {
88 version
= (struct version_struct
*)(wdheader
->data
);
89 strcpy(version
->version
, "libusb-driver.so version: " USB_DRIVER_VERSION
);
90 version
->versionul
= 802;
98 case CARD_REGISTER_OLD
:
100 DPRINTF("CARD_REGISTER\n");
102 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
104 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
106 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
107 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
108 cr
->Card
.Item
[0].I
.IO
.dwBar
);
110 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
112 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
113 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
114 cr
->Card
.Item
[1].I
.IO
.dwBar
);
116 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
119 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
123 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
125 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
127 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
128 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
136 DPRINTF("<-hCard: %lu\n", cr
->hCard
);
141 DPRINTF("USB_TRANSFER\n");
143 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
146 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
147 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
148 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
149 if (ut
->dwPipeNum
== 0) {
150 DPRINTF("-> setup packet:");
151 hexdump(ut
->SetupPacket
, 8, "");
154 if (!ut
->fRead
&& ut
->dwBufferSize
)
156 hexdump(ut
->pBuffer
, ut
->dwBufferSize
, "->");
161 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
163 ret
= xpcu_transfer(ut
);
167 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
168 if (ut
->fRead
&& ut
->dwBytesTransferred
)
170 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
, "<-");
178 DPRINTF("INT_ENABLE\n");
180 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
182 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
183 it
->hInterrupt
, it
->dwOptions
,
184 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
185 it
->dwLost
, it
->fStopped
);
188 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
190 ret
= xpcu_int_state(it
, ENABLE_INTERRUPT
);
193 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
194 it
->hInterrupt
, it
->dwOptions
,
195 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
196 it
->dwLost
, it
->fStopped
);
202 DPRINTF("INT_DISABLE\n");
204 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
206 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
207 it
->hInterrupt
, it
->dwOptions
,
208 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
209 it
->dwLost
, it
->fStopped
);
211 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
213 ret
= xpcu_int_state(it
, DISABLE_INTERRUPT
);
215 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
216 it
->hInterrupt
, it
->dwOptions
,
217 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
218 it
->dwLost
, it
->fStopped
);
222 case USB_SET_INTERFACE
:
223 DPRINTF("USB_SET_INTERFACE\n");
225 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
227 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
228 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
229 usi
->dwAlternateSetting
, usi
->dwOptions
);
231 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
233 ret
= xpcu_set_interface(usi
);
235 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
236 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
237 usi
->dwAlternateSetting
, usi
->dwOptions
);
241 case USB_GET_DEVICE_DATA_OLD
:
242 case USB_GET_DEVICE_DATA
:
243 DPRINTF("USB_GET_DEVICE_DATA\n");
245 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
247 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
248 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
251 ret
= xpcu_deviceinfo(ugdd
);
256 case EVENT_REGISTER_OLD
:
258 DPRINTF("EVENT_REGISTER\n");
260 struct event
*e
= (struct event
*)(wdheader
->data
);
265 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",
266 e
->handle
, e
->dwAction
,
267 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
268 e
->hKernelPlugIn
, e
->dwOptions
,
269 e
->u
.Usb
.deviceId
.dwVendorId
,
270 e
->u
.Usb
.deviceId
.dwProductId
,
271 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
272 e
->dwNumMatchTables
);
275 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
281 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",
282 e
->handle
, e
->dwAction
,
283 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
284 e
->hKernelPlugIn
, e
->dwOptions
,
285 e
->u
.Usb
.deviceId
.dwVendorId
,
286 e
->u
.Usb
.deviceId
.dwProductId
,
287 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
288 e
->dwNumMatchTables
);
290 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
291 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
292 e
->matchTables
[i
].VendorId
,
293 e
->matchTables
[i
].ProductId
,
294 e
->matchTables
[i
].bDeviceClass
,
295 e
->matchTables
[i
].bDeviceSubClass
,
296 e
->matchTables
[i
].bInterfaceClass
,
297 e
->matchTables
[i
].bInterfaceSubClass
,
298 e
->matchTables
[i
].bInterfaceProtocol
);
305 DPRINTF("TRANSFER\n");
307 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
310 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
312 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
317 case MULTI_TRANSFER_OLD
:
319 DPRINTF("MULTI_TRANSFER\n");
321 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
322 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
324 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
326 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
331 case EVENT_UNREGISTER
:
333 struct event
*e
= (struct event
*)(wdheader
->data
);
335 DPRINTF("EVENT_UNREGISTER\n");
337 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
345 DPRINTF("INT_WAIT\n");
347 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
349 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
350 it
->hInterrupt
, it
->dwOptions
,
351 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
352 it
->dwLost
, it
->fStopped
);
355 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
357 ret
= xpcu_int_wait(it
);
360 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
361 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
362 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
367 case CARD_UNREGISTER
:
368 DPRINTF("CARD_UNREGISTER\n");
370 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
372 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
373 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
374 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
375 cr
->Card
.Item
[0].I
.IO
.dwBar
);
377 DPRINTF("-> hCard: %lu\n", cr
->hCard
);
380 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
383 pport
->close(cr
->hCard
);
391 DPRINTF("EVENT_PULL\n");
393 struct event
*e
= (struct event
*)(wdheader
->data
);
397 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",
398 e
->handle
, e
->dwAction
, e
->dwStatus
,
399 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
400 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
401 e
->u
.Usb
.deviceId
.dwProductId
,
402 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
403 e
->dwNumMatchTables
);
405 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
406 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
407 e
->matchTables
[i
].VendorId
,
408 e
->matchTables
[i
].ProductId
,
409 e
->matchTables
[i
].bDeviceClass
,
410 e
->matchTables
[i
].bDeviceSubClass
,
411 e
->matchTables
[i
].bInterfaceClass
,
412 e
->matchTables
[i
].bInterfaceSubClass
,
413 e
->matchTables
[i
].bInterfaceProtocol
);
417 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
423 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",
424 e
->handle
, e
->dwAction
, e
->dwStatus
,
425 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
426 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
427 e
->u
.Usb
.deviceId
.dwProductId
,
428 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
429 e
->dwNumMatchTables
);
431 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
432 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
433 e
->matchTables
[i
].VendorId
,
434 e
->matchTables
[i
].ProductId
,
435 e
->matchTables
[i
].bDeviceClass
,
436 e
->matchTables
[i
].bDeviceSubClass
,
437 e
->matchTables
[i
].bInterfaceClass
,
438 e
->matchTables
[i
].bInterfaceSubClass
,
439 e
->matchTables
[i
].bInterfaceProtocol
);
446 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
448 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
456 int ioctl(int fd
, unsigned long int request
, ...) {
462 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
464 va_start (args
, request
);
465 argp
= va_arg (args
, void *);
468 for (i
= 0; i
< windrvrfds_count
; i
++) {
469 if (fd
== windrvrfds
[i
])
470 return do_wdioctl(fd
, request
, argp
);
473 return (*ioctl_func
) (fd
, request
, argp
);
476 int open (const char *pathname
, int flags
, ...) {
477 static int (*func
) (const char *, int, mode_t
) = NULL
;
483 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
485 if (flags
& O_CREAT
) {
486 va_start(args
, flags
);
487 mode
= va_arg(args
, mode_t
);
491 if (!strcmp (pathname
, "/dev/windrvr6")) {
492 DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count
);
493 windrvrfds
= realloc(windrvrfds
, sizeof(int) * (++windrvrfds_count
));
498 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) ("/dev/null", flags
, mode
);
500 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) (pathname
, flags
, mode
);
506 return (*func
) (pathname
, flags
, mode
);
510 static int (*func
) (int) = NULL
;
514 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
516 for (i
= 0; i
< windrvrfds_count
; i
++) {
517 if (fd
== windrvrfds
[i
] && windrvrfds
[i
] >= 0) {
518 int remaining
= windrvrfds_count
- (i
+ 1);
519 DPRINTF("close windrvr6 (%d)\n", i
);
521 memmove(&(windrvrfds
[i
]), &(windrvrfds
[i
+1]), remaining
* sizeof(int));
522 windrvrfds
= realloc(windrvrfds
, sizeof(int) * --windrvrfds_count
);
523 if (!windrvrfds_count
)
532 FILE *fopen(const char *path
, const char *mode
) {
534 static FILE* (*func
) (const char*, const char*) = NULL
;
539 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
541 for (i
= 0; i
< 4; i
++) {
542 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
543 if (!strcmp(path
, buf
)) {
544 DPRINTF("open base-addr of parport%d\n", i
);
545 if (config_is_real_pport(i
)) {
546 ret
= (*func
) (path
, mode
);
548 ret
= (*func
) ("/dev/null", mode
);
560 ret
= (*func
) (path
, mode
);
562 if (!strcmp(path
, "/proc/modules")) {
563 DPRINTF("opening /proc/modules\n");
564 if (!ret
&& errno
== ENOENT
) {
565 /* Hmm.. there appears to be no /proc/modules file
567 ret
= (*func
)("/dev/null", mode
);
568 DPRINTF("No /proc/modules -- faking\n");
579 char *fgets(char *s
, int size
, FILE *stream
) {
580 static char* (*func
) (char*, int, FILE*) = NULL
;
581 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
587 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
589 if (modulesfp
== stream
) {
590 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
591 strcpy(s
, modules
[modules_read
]);
595 } else if (baseaddrfp
== stream
) {
596 snprintf(s
, sizeof(buf
), "%d\t%d\n",
597 (baseaddrnum
) * 0x10,
598 ((baseaddrnum
) * 0x10) + 0x400);
601 ret
= (*func
)(s
,size
,stream
);
607 int fclose(FILE *fp
) {
608 static int (*func
) (FILE*) = NULL
;
611 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
613 if (fp
== modulesfp
) {
617 if (fp
== baseaddrfp
) {
624 int access(const char *pathname
, int mode
) {
625 static int (*func
) (const char*, int);
628 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
630 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
633 return (*func
)(pathname
, mode
);
638 /* USB cable sharing needs to overload semop, TODO! */
639 int semop (int __semid
, struct sembuf
*__sops
, size_t __nsops
) {
640 static int (*func
) (int, struct sembuf
*, size_t) = NULL
;
644 func
= (int (*) (int, struct sembuf
*, size_t)) dlsym(RTLD_NEXT
, "semop");
646 fprintf(stderr
,"semop: semid: 0x%X, elements: %d\n", __semid
, __nsops
);
647 for (i
= 0; i
< __nsops
; i
++) {
648 fprintf(stderr
, " num: %u, op: %d, flg: %d\n", __sops
[i
].sem_num
, __sops
[i
].sem_op
, __sops
[i
].sem_flg
);
649 if (__sops
[i
].sem_op
< 0) {
650 fprintf(stderr
, "SEMAPHORE LOCK\n");
652 fprintf(stderr
, "SEMAPHORE UNLOCK\n");
656 return (*func
)(__semid
, __sops
, __nsops
);
661 * Ugly hack for ISE 12. They don't seem to open /proc/modules with
663 * echo '_Z14isModuleLoadedPci' | c++filt
665 int _Z14isModuleLoadedPci(char *module_name
, int i
) {
666 DPRINTF("_Z14isModuleLoadedPci: Checking for module %s (%d)\n", module_name
, i
);
671 static void __attribute__ ((constructor
)) libusbdriver_init(void) {
678 if (ret
== 0 && (!strcmp(un
.machine
, "x86_64"))) {
679 DPRINTF("setting 32bit personality\n");
680 (long)syscall(SYS_personality
, PER_LINUX32
);