]>
git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
0383d123d034d62188bc63fc6c09b86ca1a149f3
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>
40 #include <sys/ioctl.h>
41 #include <sys/utsname.h>
42 #include <bits/wordsize.h>
43 #include "usb-driver.h"
47 static int (*ioctl_func
) (int, int, void *) = NULL
;
48 static int windrvrfd
= -1;
49 static unsigned long ppbase
= 0;
50 static unsigned long ecpbase
= 0;
51 static struct parport_config
*pport
= NULL
;
52 static struct xpcu_s
*xpcu
= NULL
;
53 static FILE *modulesfp
= NULL
;
54 static FILE *baseaddrfp
= NULL
;
55 static int baseaddrnum
= 0;
56 static int modules_read
= 0;
60 void hexdump(unsigned char *buf
, int len
) {
63 for(i
=0; i
<len
; i
++) {
64 fprintf(stderr
,"%02x ", buf
[i
]);
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 xpcu_transfer(xpcu
, ut
);
163 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
164 if (ut
->fRead
&& ut
->dwBytesTransferred
)
166 DPRINTF("<- Read: ");
167 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
175 DPRINTF("INT_ENABLE\n");
177 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
179 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
180 it
->hInterrupt
, it
->dwOptions
,
181 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
182 it
->dwLost
, it
->fStopped
);
185 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
187 xpcu_int_state(xpcu
, it
, ENABLE_INTERRUPT
);
190 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
191 it
->hInterrupt
, it
->dwOptions
,
192 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
193 it
->dwLost
, it
->fStopped
);
199 DPRINTF("INT_DISABLE\n");
201 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
203 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
204 it
->hInterrupt
, it
->dwOptions
,
205 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
206 it
->dwLost
, it
->fStopped
);
208 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
210 xpcu_int_state(xpcu
, it
, DISABLE_INTERRUPT
);
212 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
213 it
->hInterrupt
, it
->dwOptions
,
214 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
215 it
->dwLost
, it
->fStopped
);
219 case USB_SET_INTERFACE
:
220 DPRINTF("USB_SET_INTERFACE\n");
222 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
224 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
225 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
226 usi
->dwAlternateSetting
, usi
->dwOptions
);
228 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
230 xpcu_set_interface(xpcu
, usi
);
232 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
233 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
234 usi
->dwAlternateSetting
, usi
->dwOptions
);
238 case USB_GET_DEVICE_DATA_OLD
:
239 case USB_GET_DEVICE_DATA
:
240 DPRINTF("USB_GET_DEVICE_DATA\n");
242 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
244 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
245 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
248 ugdd
->dwBytes
= xpcu_deviceinfo(xpcu
, ugdd
);
253 case EVENT_REGISTER_OLD
:
255 DPRINTF("EVENT_REGISTER\n");
257 struct event
*e
= (struct event
*)(wdheader
->data
);
262 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",
263 e
->handle
, e
->dwAction
,
264 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
265 e
->hKernelPlugIn
, e
->dwOptions
,
266 e
->u
.Usb
.deviceId
.dwVendorId
,
267 e
->u
.Usb
.deviceId
.dwProductId
,
268 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
269 e
->dwNumMatchTables
);
272 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
278 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",
279 e
->handle
, e
->dwAction
,
280 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
281 e
->hKernelPlugIn
, e
->dwOptions
,
282 e
->u
.Usb
.deviceId
.dwVendorId
,
283 e
->u
.Usb
.deviceId
.dwProductId
,
284 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
285 e
->dwNumMatchTables
);
287 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
288 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
289 e
->matchTables
[i
].VendorId
,
290 e
->matchTables
[i
].ProductId
,
291 e
->matchTables
[i
].bDeviceClass
,
292 e
->matchTables
[i
].bDeviceSubClass
,
293 e
->matchTables
[i
].bInterfaceClass
,
294 e
->matchTables
[i
].bInterfaceSubClass
,
295 e
->matchTables
[i
].bInterfaceProtocol
);
302 DPRINTF("TRANSFER\n");
304 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
307 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
309 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
314 case MULTI_TRANSFER_OLD
:
316 DPRINTF("MULTI_TRANSFER\n");
318 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
319 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
321 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
323 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
328 case EVENT_UNREGISTER
:
330 struct event
*e
= (struct event
*)(wdheader
->data
);
332 DPRINTF("EVENT_UNREGISTER\n");
334 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
342 DPRINTF("INT_WAIT\n");
344 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
346 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
347 it
->hInterrupt
, it
->dwOptions
,
348 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
349 it
->dwLost
, it
->fStopped
);
352 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
354 xpcu_int_wait(xpcu
, it
);
357 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
358 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
359 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
364 case CARD_UNREGISTER
:
365 DPRINTF("CARD_UNREGISTER\n");
367 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
369 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
370 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
371 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
372 cr
->Card
.Item
[0].I
.IO
.dwBar
);
374 DPRINTF("-> hCard: %lu\n", cr
->hCard
);
377 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
380 pport
->close(cr
->hCard
);
388 DPRINTF("EVENT_PULL\n");
390 struct event
*e
= (struct event
*)(wdheader
->data
);
394 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",
395 e
->handle
, e
->dwAction
, e
->dwStatus
,
396 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
397 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
398 e
->u
.Usb
.deviceId
.dwProductId
,
399 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
400 e
->dwNumMatchTables
);
402 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
403 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
404 e
->matchTables
[i
].VendorId
,
405 e
->matchTables
[i
].ProductId
,
406 e
->matchTables
[i
].bDeviceClass
,
407 e
->matchTables
[i
].bDeviceSubClass
,
408 e
->matchTables
[i
].bInterfaceClass
,
409 e
->matchTables
[i
].bInterfaceSubClass
,
410 e
->matchTables
[i
].bInterfaceProtocol
);
414 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
420 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",
421 e
->handle
, e
->dwAction
, e
->dwStatus
,
422 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
423 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
424 e
->u
.Usb
.deviceId
.dwProductId
,
425 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
426 e
->dwNumMatchTables
);
428 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
429 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
430 e
->matchTables
[i
].VendorId
,
431 e
->matchTables
[i
].ProductId
,
432 e
->matchTables
[i
].bDeviceClass
,
433 e
->matchTables
[i
].bDeviceSubClass
,
434 e
->matchTables
[i
].bInterfaceClass
,
435 e
->matchTables
[i
].bInterfaceSubClass
,
436 e
->matchTables
[i
].bInterfaceProtocol
);
443 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
445 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
453 int ioctl(int fd
, unsigned long int request
, ...) {
459 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
461 va_start (args
, request
);
462 argp
= va_arg (args
, void *);
466 ret
= do_wdioctl(fd
, request
, argp
);
468 ret
= (*ioctl_func
) (fd
, request
, argp
);
473 int open (const char *pathname
, int flags
, ...) {
474 static int (*func
) (const char *, int, mode_t
) = NULL
;
480 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
482 if (flags
& O_CREAT
) {
483 va_start(args
, flags
);
484 mode
= va_arg(args
, mode_t
);
488 if (!strcmp (pathname
, "/dev/windrvr6")) {
489 DPRINTF("opening windrvr6\n");
491 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
493 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
499 return (*func
) (pathname
, flags
, mode
);
503 static int (*func
) (int) = NULL
;
506 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
508 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
509 DPRINTF("close windrvrfd\n");
518 FILE *fopen(const char *path
, const char *mode
) {
520 static FILE* (*func
) (const char*, const char*) = NULL
;
525 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
527 for (i
= 0; i
< 4; i
++) {
528 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
529 if (!strcmp(path
, buf
)) {
530 DPRINTF("open base-addr of parport%d\n", i
);
531 if (config_is_real_pport(i
)) {
532 ret
= (*func
) (path
, mode
);
534 ret
= (*func
) ("/dev/null", mode
);
546 ret
= (*func
) (path
, mode
);
548 if (!strcmp(path
, "/proc/modules")) {
549 DPRINTF("opening /proc/modules\n");
559 char *fgets(char *s
, int size
, FILE *stream
) {
560 static char* (*func
) (char*, int, FILE*) = NULL
;
561 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
567 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
569 if (modulesfp
== stream
) {
570 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
571 strcpy(s
, modules
[modules_read
]);
575 } else if (baseaddrfp
== stream
) {
576 snprintf(s
, sizeof(buf
), "%d\t%d\n",
577 (baseaddrnum
) * 0x10,
578 ((baseaddrnum
) * 0x10) + 0x400);
581 ret
= (*func
)(s
,size
,stream
);
587 int fclose(FILE *fp
) {
588 static int (*func
) (FILE*) = NULL
;
591 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
593 if (fp
== modulesfp
) {
597 if (fp
== baseaddrfp
) {
604 int access(const char *pathname
, int mode
) {
605 static int (*func
) (const char*, int);
608 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
610 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
613 return (*func
)(pathname
, mode
);
618 int uname (struct utsname
*__name
) {
619 static int (*func
) (struct utsname
*);
623 func
= (int (*) (struct utsname
*)) dlsym(RTLD_NEXT
, "uname");
625 ret
= (*func
)(__name
);
627 if (ret
== 0 && (!strcmp(__name
->machine
, "x86_64"))) {
628 strcpy(__name
->machine
, "i686");