]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
1e1f026bbdb342c4eadae64988bf4754df35aed1
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
);
120 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
124 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
126 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
128 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
129 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
137 DPRINTF("<-hCard: %lu\n", cr
->hCard
);
142 DPRINTF("USB_TRANSFER\n");
144 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
147 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
148 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
149 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
150 if (ut
->dwPipeNum
== 0) {
151 DPRINTF("-> setup packet:");
152 hexdump(ut
->SetupPacket
, 8, "");
155 if (!ut
->fRead
&& ut
->dwBufferSize
)
157 hexdump(ut
->pBuffer
, ut
->dwBufferSize
, "->");
162 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
164 ret
= xpcu_transfer(ut
);
168 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
169 if (ut
->fRead
&& ut
->dwBytesTransferred
)
171 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
, "<-");
179 DPRINTF("INT_ENABLE\n");
181 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
183 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
184 it
->hInterrupt
, it
->dwOptions
,
185 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
186 it
->dwLost
, it
->fStopped
);
189 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
191 ret
= xpcu_int_state(it
, ENABLE_INTERRUPT
);
194 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
195 it
->hInterrupt
, it
->dwOptions
,
196 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
197 it
->dwLost
, it
->fStopped
);
203 DPRINTF("INT_DISABLE\n");
205 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
207 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
208 it
->hInterrupt
, it
->dwOptions
,
209 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
210 it
->dwLost
, it
->fStopped
);
212 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
214 ret
= xpcu_int_state(it
, DISABLE_INTERRUPT
);
216 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
217 it
->hInterrupt
, it
->dwOptions
,
218 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
219 it
->dwLost
, it
->fStopped
);
223 case USB_SET_INTERFACE
:
224 DPRINTF("USB_SET_INTERFACE\n");
226 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
228 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
229 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
230 usi
->dwAlternateSetting
, usi
->dwOptions
);
232 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
234 ret
= xpcu_set_interface(usi
);
236 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
237 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
238 usi
->dwAlternateSetting
, usi
->dwOptions
);
242 case USB_GET_DEVICE_DATA_OLD
:
243 case USB_GET_DEVICE_DATA
:
244 DPRINTF("USB_GET_DEVICE_DATA\n");
246 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
248 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
249 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
252 ret
= xpcu_deviceinfo(ugdd
);
257 case EVENT_REGISTER_OLD
:
259 DPRINTF("EVENT_REGISTER\n");
261 struct event
*e
= (struct event
*)(wdheader
->data
);
266 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",
267 e
->handle
, e
->dwAction
,
268 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
269 e
->hKernelPlugIn
, e
->dwOptions
,
270 e
->u
.Usb
.deviceId
.dwVendorId
,
271 e
->u
.Usb
.deviceId
.dwProductId
,
272 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
273 e
->dwNumMatchTables
);
276 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
282 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",
283 e
->handle
, e
->dwAction
,
284 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
285 e
->hKernelPlugIn
, e
->dwOptions
,
286 e
->u
.Usb
.deviceId
.dwVendorId
,
287 e
->u
.Usb
.deviceId
.dwProductId
,
288 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
289 e
->dwNumMatchTables
);
291 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
292 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
293 e
->matchTables
[i
].VendorId
,
294 e
->matchTables
[i
].ProductId
,
295 e
->matchTables
[i
].bDeviceClass
,
296 e
->matchTables
[i
].bDeviceSubClass
,
297 e
->matchTables
[i
].bInterfaceClass
,
298 e
->matchTables
[i
].bInterfaceSubClass
,
299 e
->matchTables
[i
].bInterfaceProtocol
);
306 DPRINTF("TRANSFER\n");
308 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
311 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
313 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
318 case MULTI_TRANSFER_OLD
:
320 DPRINTF("MULTI_TRANSFER\n");
322 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
323 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
325 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
327 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
332 case EVENT_UNREGISTER
:
334 struct event
*e
= (struct event
*)(wdheader
->data
);
336 DPRINTF("EVENT_UNREGISTER\n");
338 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
346 DPRINTF("INT_WAIT\n");
348 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
350 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
351 it
->hInterrupt
, it
->dwOptions
,
352 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
353 it
->dwLost
, it
->fStopped
);
356 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
358 ret
= xpcu_int_wait(it
);
361 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
362 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
363 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
368 case CARD_UNREGISTER
:
369 DPRINTF("CARD_UNREGISTER\n");
371 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
373 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
374 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
375 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
376 cr
->Card
.Item
[0].I
.IO
.dwBar
);
378 DPRINTF("-> hCard: %lu\n", cr
->hCard
);
381 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
384 pport
->close(cr
->hCard
);
392 DPRINTF("EVENT_PULL\n");
394 struct event
*e
= (struct event
*)(wdheader
->data
);
398 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",
399 e
->handle
, e
->dwAction
, e
->dwStatus
,
400 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
401 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
402 e
->u
.Usb
.deviceId
.dwProductId
,
403 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
404 e
->dwNumMatchTables
);
406 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
407 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
408 e
->matchTables
[i
].VendorId
,
409 e
->matchTables
[i
].ProductId
,
410 e
->matchTables
[i
].bDeviceClass
,
411 e
->matchTables
[i
].bDeviceSubClass
,
412 e
->matchTables
[i
].bInterfaceClass
,
413 e
->matchTables
[i
].bInterfaceSubClass
,
414 e
->matchTables
[i
].bInterfaceProtocol
);
418 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
424 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",
425 e
->handle
, e
->dwAction
, e
->dwStatus
,
426 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
427 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
428 e
->u
.Usb
.deviceId
.dwProductId
,
429 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
430 e
->dwNumMatchTables
);
432 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
433 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
434 e
->matchTables
[i
].VendorId
,
435 e
->matchTables
[i
].ProductId
,
436 e
->matchTables
[i
].bDeviceClass
,
437 e
->matchTables
[i
].bDeviceSubClass
,
438 e
->matchTables
[i
].bInterfaceClass
,
439 e
->matchTables
[i
].bInterfaceSubClass
,
440 e
->matchTables
[i
].bInterfaceProtocol
);
447 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
449 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
457 int ioctl(int fd
, unsigned long int request
, ...) {
463 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
465 va_start (args
, request
);
466 argp
= va_arg (args
, void *);
469 for (i
= 0; i
< windrvrfds_count
; i
++) {
470 if (fd
== windrvrfds
[i
])
471 return do_wdioctl(fd
, request
, argp
);
474 return (*ioctl_func
) (fd
, request
, argp
);
477 int open (const char *pathname
, int flags
, ...) {
478 static int (*func
) (const char *, int, mode_t
) = NULL
;
484 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
486 if (flags
& O_CREAT
) {
487 va_start(args
, flags
);
488 mode
= va_arg(args
, mode_t
);
492 if (!strcmp (pathname
, "/dev/windrvr6")) {
493 DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count
);
494 windrvrfds
= realloc(windrvrfds
, sizeof(int) * (++windrvrfds_count
));
499 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) ("/dev/null", flags
, mode
);
501 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) (pathname
, flags
, mode
);
507 return (*func
) (pathname
, flags
, mode
);
511 static int (*func
) (int) = NULL
;
515 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
517 for (i
= 0; i
< windrvrfds_count
; i
++) {
518 if (fd
== windrvrfds
[i
] && windrvrfds
[i
] >= 0) {
519 int remaining
= windrvrfds_count
- (i
+ 1);
520 DPRINTF("close windrvr6 (%d)\n", i
);
522 memmove(&(windrvrfds
[i
]), &(windrvrfds
[i
+1]), remaining
* sizeof(int));
523 windrvrfds
= realloc(windrvrfds
, sizeof(int) * --windrvrfds_count
);
524 if (!windrvrfds_count
)
533 FILE *fopen(const char *path
, const char *mode
) {
535 static FILE* (*func
) (const char*, const char*) = NULL
;
540 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
542 for (i
= 0; i
< 4; i
++) {
543 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
544 if (!strcmp(path
, buf
)) {
545 DPRINTF("open base-addr of parport%d\n", i
);
546 if (config_is_real_pport(i
)) {
547 ret
= (*func
) (path
, mode
);
549 ret
= (*func
) ("/dev/null", mode
);
561 ret
= (*func
) (path
, mode
);
563 if (!strcmp(path
, "/proc/modules")) {
564 DPRINTF("opening /proc/modules\n");
565 if (!ret
&& errno
== ENOENT
) {
566 /* Hmm.. there appears to be no /proc/modules file
568 ret
= (*func
)("/dev/null", mode
);
569 DPRINTF("No /proc/modules -- faking\n");
580 char *fgets(char *s
, int size
, FILE *stream
) {
581 static char* (*func
) (char*, int, FILE*) = NULL
;
582 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
588 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
590 if (modulesfp
== stream
) {
591 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
592 strcpy(s
, modules
[modules_read
]);
596 } else if (baseaddrfp
== stream
) {
597 snprintf(s
, sizeof(buf
), "%d\t%d\n",
598 (baseaddrnum
) * 0x10,
599 ((baseaddrnum
) * 0x10) + 0x400);
602 ret
= (*func
)(s
,size
,stream
);
608 int fclose(FILE *fp
) {
609 static int (*func
) (FILE*) = NULL
;
612 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
614 if (fp
== modulesfp
) {
618 if (fp
== baseaddrfp
) {
625 int access(const char *pathname
, int mode
) {
626 static int (*func
) (const char*, int);
629 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
631 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
634 return (*func
)(pathname
, mode
);
639 /* USB cable sharing needs to overload semop, TODO! */
640 int semop (int __semid
, struct sembuf
*__sops
, size_t __nsops
) {
641 static int (*func
) (int, struct sembuf
*, size_t) = NULL
;
645 func
= (int (*) (int, struct sembuf
*, size_t)) dlsym(RTLD_NEXT
, "semop");
647 fprintf(stderr
,"semop: semid: 0x%X, elements: %d\n", __semid
, __nsops
);
648 for (i
= 0; i
< __nsops
; i
++) {
649 fprintf(stderr
, " num: %u, op: %d, flg: %d\n", __sops
[i
].sem_num
, __sops
[i
].sem_op
, __sops
[i
].sem_flg
);
650 if (__sops
[i
].sem_op
< 0) {
651 fprintf(stderr
, "SEMAPHORE LOCK\n");
653 fprintf(stderr
, "SEMAPHORE UNLOCK\n");
657 return (*func
)(__semid
, __sops
, __nsops
);
662 * Ugly hack for ISE 12. Preload doesn't seem to work correctly for
663 * libImpactComm.so. Even though the file is still read with fopen(),
664 * the version from libc is used and not the one from this file.
665 * Replace the function calling fopen() instead...
666 * echo '_Z14isModuleLoadedPci' | c++filt
668 long int _Z14isModuleLoadedPci(char *module_name
, int i
) {
669 DPRINTF("_Z14isModuleLoadedPci: Checking for module %s (%d)\n", module_name
, i
);
674 static void __attribute__ ((constructor
)) libusbdriver_init(void) {
679 for (i
= 0; i
< 4; i
++) {
680 snprintf(buf
, sizeof(buf
), "XIL_IMPACT_ENV_LPT%d_BASE_ADDRESS", i
+1);
681 snprintf(buf2
, sizeof(buf2
), "%x", 0x10*i
);
682 setenv(buf
, buf2
, 1);
683 snprintf(buf
, sizeof(buf
), "XIL_IMPACT_ENV_LPT%d_ECP_ADDRESS", i
+1);
684 snprintf(buf2
, sizeof(buf2
), "%x", (0x10*i
)+0x400);
685 setenv(buf
, buf2
, 1);
688 setenv("XIL_IMPACT_USE_LIBUSB", "0", 1);
689 setenv("XIL_IMPACT_USE_WINDRIVER", "1", 1);
698 if (ret
== 0 && (!strcmp(un
.machine
, "x86_64"))) {
699 DPRINTF("setting 32bit personality\n");
700 (long)syscall(SYS_personality
, PER_LINUX32
);