]>
cvs.zerfleddert.de Git - usb-driver/blob - usb-driver.c
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>
42 #include "usb-driver.h"
46 static int (*ioctl_func
) (int, int, void *) = NULL
;
47 static int *windrvrfds
= NULL
;
48 static int windrvrfds_count
= 0;
49 static unsigned long ppbase
= 0;
50 static unsigned long ecpbase
= 0;
51 static struct parport_config
*pport
= NULL
;
52 static FILE *modulesfp
= NULL
;
53 static FILE *baseaddrfp
= NULL
;
54 static int baseaddrnum
= 0;
55 static int modules_read
= 0;
59 void hexdump(unsigned char *buf
, int len
, char *prefix
) {
62 fprintf(stderr
, "%s ", prefix
);
63 for(i
=0; i
<len
; i
++) {
64 fprintf(stderr
,"%02x ", buf
[i
]);
66 fprintf(stderr
,"\n%s ", prefix
);
72 static int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
73 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
74 struct version_struct
*version
;
77 if (wdheader
->magic
!= MAGIC
) {
78 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
79 return (*ioctl_func
) (fd
, request
, wdioctl
);
82 switch(request
& ~(0xc0000000)) {
84 version
= (struct version_struct
*)(wdheader
->data
);
85 strcpy(version
->version
, "libusb-driver.so version: " USB_DRIVER_VERSION
);
86 version
->versionul
= 802;
94 case CARD_REGISTER_OLD
:
96 DPRINTF("CARD_REGISTER\n");
98 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
100 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
102 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
103 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
104 cr
->Card
.Item
[0].I
.IO
.dwBar
);
106 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
108 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
109 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
110 cr
->Card
.Item
[1].I
.IO
.dwBar
);
112 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
115 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
119 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
121 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
123 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
124 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
132 DPRINTF("<-hCard: %lu\n", cr
->hCard
);
137 DPRINTF("USB_TRANSFER\n");
139 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
142 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
143 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
144 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
145 if (ut
->dwPipeNum
== 0) {
146 DPRINTF("-> setup packet:");
147 hexdump(ut
->SetupPacket
, 8, "");
150 if (!ut
->fRead
&& ut
->dwBufferSize
)
152 hexdump(ut
->pBuffer
, ut
->dwBufferSize
, "->");
157 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
159 ret
= xpcu_transfer(ut
);
163 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
164 if (ut
->fRead
&& ut
->dwBytesTransferred
)
166 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
, "<-");
174 DPRINTF("INT_ENABLE\n");
176 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
178 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
179 it
->hInterrupt
, it
->dwOptions
,
180 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
181 it
->dwLost
, it
->fStopped
);
184 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
186 ret
= xpcu_int_state(it
, ENABLE_INTERRUPT
);
189 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
190 it
->hInterrupt
, it
->dwOptions
,
191 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
192 it
->dwLost
, it
->fStopped
);
198 DPRINTF("INT_DISABLE\n");
200 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
202 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
203 it
->hInterrupt
, it
->dwOptions
,
204 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
205 it
->dwLost
, it
->fStopped
);
207 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
209 ret
= xpcu_int_state(it
, DISABLE_INTERRUPT
);
211 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
212 it
->hInterrupt
, it
->dwOptions
,
213 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
214 it
->dwLost
, it
->fStopped
);
218 case USB_SET_INTERFACE
:
219 DPRINTF("USB_SET_INTERFACE\n");
221 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
223 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
224 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
225 usi
->dwAlternateSetting
, usi
->dwOptions
);
227 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
229 ret
= xpcu_set_interface(usi
);
231 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
232 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
233 usi
->dwAlternateSetting
, usi
->dwOptions
);
237 case USB_GET_DEVICE_DATA_OLD
:
238 case USB_GET_DEVICE_DATA
:
239 DPRINTF("USB_GET_DEVICE_DATA\n");
241 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
243 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
244 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
247 ret
= xpcu_deviceinfo(ugdd
);
252 case EVENT_REGISTER_OLD
:
254 DPRINTF("EVENT_REGISTER\n");
256 struct event
*e
= (struct event
*)(wdheader
->data
);
261 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",
262 e
->handle
, e
->dwAction
,
263 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
264 e
->hKernelPlugIn
, e
->dwOptions
,
265 e
->u
.Usb
.deviceId
.dwVendorId
,
266 e
->u
.Usb
.deviceId
.dwProductId
,
267 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
268 e
->dwNumMatchTables
);
271 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
277 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",
278 e
->handle
, e
->dwAction
,
279 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
280 e
->hKernelPlugIn
, e
->dwOptions
,
281 e
->u
.Usb
.deviceId
.dwVendorId
,
282 e
->u
.Usb
.deviceId
.dwProductId
,
283 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
284 e
->dwNumMatchTables
);
286 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
287 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
288 e
->matchTables
[i
].VendorId
,
289 e
->matchTables
[i
].ProductId
,
290 e
->matchTables
[i
].bDeviceClass
,
291 e
->matchTables
[i
].bDeviceSubClass
,
292 e
->matchTables
[i
].bInterfaceClass
,
293 e
->matchTables
[i
].bInterfaceSubClass
,
294 e
->matchTables
[i
].bInterfaceProtocol
);
301 DPRINTF("TRANSFER\n");
303 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
306 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
308 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
313 case MULTI_TRANSFER_OLD
:
315 DPRINTF("MULTI_TRANSFER\n");
317 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
318 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
320 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
322 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
327 case EVENT_UNREGISTER
:
329 struct event
*e
= (struct event
*)(wdheader
->data
);
331 DPRINTF("EVENT_UNREGISTER\n");
333 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
341 DPRINTF("INT_WAIT\n");
343 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
345 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
346 it
->hInterrupt
, it
->dwOptions
,
347 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
348 it
->dwLost
, it
->fStopped
);
351 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
353 ret
= xpcu_int_wait(it
);
356 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
357 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
358 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
363 case CARD_UNREGISTER
:
364 DPRINTF("CARD_UNREGISTER\n");
366 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
368 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
369 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
370 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
371 cr
->Card
.Item
[0].I
.IO
.dwBar
);
373 DPRINTF("-> hCard: %lu\n", cr
->hCard
);
376 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
379 pport
->close(cr
->hCard
);
387 DPRINTF("EVENT_PULL\n");
389 struct event
*e
= (struct event
*)(wdheader
->data
);
393 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",
394 e
->handle
, e
->dwAction
, e
->dwStatus
,
395 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
396 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
397 e
->u
.Usb
.deviceId
.dwProductId
,
398 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
399 e
->dwNumMatchTables
);
401 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
402 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
403 e
->matchTables
[i
].VendorId
,
404 e
->matchTables
[i
].ProductId
,
405 e
->matchTables
[i
].bDeviceClass
,
406 e
->matchTables
[i
].bDeviceSubClass
,
407 e
->matchTables
[i
].bInterfaceClass
,
408 e
->matchTables
[i
].bInterfaceSubClass
,
409 e
->matchTables
[i
].bInterfaceProtocol
);
413 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
419 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",
420 e
->handle
, e
->dwAction
, e
->dwStatus
,
421 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
422 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
423 e
->u
.Usb
.deviceId
.dwProductId
,
424 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
425 e
->dwNumMatchTables
);
427 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
428 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
429 e
->matchTables
[i
].VendorId
,
430 e
->matchTables
[i
].ProductId
,
431 e
->matchTables
[i
].bDeviceClass
,
432 e
->matchTables
[i
].bDeviceSubClass
,
433 e
->matchTables
[i
].bInterfaceClass
,
434 e
->matchTables
[i
].bInterfaceSubClass
,
435 e
->matchTables
[i
].bInterfaceProtocol
);
442 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
444 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
452 int ioctl(int fd
, unsigned long int request
, ...) {
458 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
460 va_start (args
, request
);
461 argp
= va_arg (args
, void *);
464 for (i
= 0; i
< windrvrfds_count
; i
++) {
465 if (fd
== windrvrfds
[i
])
466 return do_wdioctl(fd
, request
, argp
);
469 return (*ioctl_func
) (fd
, request
, argp
);
472 int open (const char *pathname
, int flags
, ...) {
473 static int (*func
) (const char *, int, mode_t
) = NULL
;
479 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
481 if (flags
& O_CREAT
) {
482 va_start(args
, flags
);
483 mode
= va_arg(args
, mode_t
);
487 if (!strcmp (pathname
, "/dev/windrvr6")) {
488 DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count
);
489 windrvrfds
= realloc(windrvrfds
, sizeof(int) * (++windrvrfds_count
));
494 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) ("/dev/null", flags
, mode
);
496 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) (pathname
, flags
, mode
);
502 return (*func
) (pathname
, flags
, mode
);
506 static int (*func
) (int) = NULL
;
510 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
512 for (i
= 0; i
< windrvrfds_count
; i
++) {
513 if (fd
== windrvrfds
[i
] && windrvrfds
[i
] >= 0) {
514 int remaining
= windrvrfds_count
- (i
+ 1);
515 DPRINTF("close windrvr6 (%d)\n", i
);
517 memmove(&(windrvrfds
[i
]), &(windrvrfds
[i
+1]), remaining
* sizeof(int));
518 windrvrfds
= realloc(windrvrfds
, sizeof(int) * --windrvrfds_count
);
519 if (!windrvrfds_count
)
528 FILE *fopen(const char *path
, const char *mode
) {
530 static FILE* (*func
) (const char*, const char*) = NULL
;
535 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
537 for (i
= 0; i
< 4; i
++) {
538 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
539 if (!strcmp(path
, buf
)) {
540 DPRINTF("open base-addr of parport%d\n", i
);
541 if (config_is_real_pport(i
)) {
542 ret
= (*func
) (path
, mode
);
544 ret
= (*func
) ("/dev/null", mode
);
556 ret
= (*func
) (path
, mode
);
558 if (!strcmp(path
, "/proc/modules")) {
559 DPRINTF("opening /proc/modules\n");
569 char *fgets(char *s
, int size
, FILE *stream
) {
570 static char* (*func
) (char*, int, FILE*) = NULL
;
571 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
577 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
579 if (modulesfp
== stream
) {
580 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
581 strcpy(s
, modules
[modules_read
]);
585 } else if (baseaddrfp
== stream
) {
586 snprintf(s
, sizeof(buf
), "%d\t%d\n",
587 (baseaddrnum
) * 0x10,
588 ((baseaddrnum
) * 0x10) + 0x400);
591 ret
= (*func
)(s
,size
,stream
);
597 int fclose(FILE *fp
) {
598 static int (*func
) (FILE*) = NULL
;
601 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
603 if (fp
== modulesfp
) {
607 if (fp
== baseaddrfp
) {
614 int access(const char *pathname
, int mode
) {
615 static int (*func
) (const char*, int);
618 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
620 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
623 return (*func
)(pathname
, mode
);
628 int uname (struct utsname
*__name
) {
629 static int (*func
) (struct utsname
*);
633 func
= (int (*) (struct utsname
*)) dlsym(RTLD_NEXT
, "uname");
635 ret
= (*func
)(__name
);
637 if (ret
== 0 && (!strcmp(__name
->machine
, "x86_64"))) {
638 strcpy(__name
->machine
, "i686");