]>
git.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 windrvrfd
= -1;
48 static unsigned long ppbase
= 0;
49 static unsigned long ecpbase
= 0;
50 static struct parport_config
*pport
= NULL
;
51 static FILE *modulesfp
= NULL
;
52 static FILE *baseaddrfp
= NULL
;
53 static int baseaddrnum
= 0;
54 static int modules_read
= 0;
58 void hexdump(unsigned char *buf
, int len
) {
61 for(i
=0; i
<len
; i
++) {
62 fprintf(stderr
,"%02x ", buf
[i
]);
70 static int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
71 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
72 struct version_struct
*version
;
75 if (wdheader
->magic
!= MAGIC
) {
76 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
77 return (*ioctl_func
) (fd
, request
, wdioctl
);
80 switch(request
& ~(0xc0000000)) {
82 version
= (struct version_struct
*)(wdheader
->data
);
83 strcpy(version
->version
, "libusb-driver.so version: " USB_DRIVER_VERSION
);
84 version
->versionul
= 802;
92 case CARD_REGISTER_OLD
:
94 DPRINTF("CARD_REGISTER\n");
96 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
98 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
100 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
101 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
102 cr
->Card
.Item
[0].I
.IO
.dwBar
);
104 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
106 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
107 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
108 cr
->Card
.Item
[1].I
.IO
.dwBar
);
110 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
113 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
117 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
119 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
121 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
122 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
130 DPRINTF("<-hCard: %lu\n", cr
->hCard
);
135 DPRINTF("USB_TRANSFER\n");
137 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
140 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
141 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
142 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
143 if (ut
->dwPipeNum
== 0) {
144 DPRINTF("-> setup packet: ");
145 hexdump(ut
->SetupPacket
, 8);
148 if (!ut
->fRead
&& ut
->dwBufferSize
)
150 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
155 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
157 ret
= xpcu_transfer(ut
);
161 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
162 if (ut
->fRead
&& ut
->dwBytesTransferred
)
164 DPRINTF("<- Read: ");
165 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
173 DPRINTF("INT_ENABLE\n");
175 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
177 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
178 it
->hInterrupt
, it
->dwOptions
,
179 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
180 it
->dwLost
, it
->fStopped
);
183 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
185 ret
= xpcu_int_state(it
, ENABLE_INTERRUPT
);
188 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
189 it
->hInterrupt
, it
->dwOptions
,
190 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
191 it
->dwLost
, it
->fStopped
);
197 DPRINTF("INT_DISABLE\n");
199 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
201 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
202 it
->hInterrupt
, it
->dwOptions
,
203 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
204 it
->dwLost
, it
->fStopped
);
206 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
208 ret
= xpcu_int_state(it
, DISABLE_INTERRUPT
);
210 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
211 it
->hInterrupt
, it
->dwOptions
,
212 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
213 it
->dwLost
, it
->fStopped
);
217 case USB_SET_INTERFACE
:
218 DPRINTF("USB_SET_INTERFACE\n");
220 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
222 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
223 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
224 usi
->dwAlternateSetting
, usi
->dwOptions
);
226 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
228 ret
= xpcu_set_interface(usi
);
230 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
231 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
232 usi
->dwAlternateSetting
, usi
->dwOptions
);
236 case USB_GET_DEVICE_DATA_OLD
:
237 case USB_GET_DEVICE_DATA
:
238 DPRINTF("USB_GET_DEVICE_DATA\n");
240 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
242 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
243 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
246 ret
= xpcu_deviceinfo(ugdd
);
251 case EVENT_REGISTER_OLD
:
253 DPRINTF("EVENT_REGISTER\n");
255 struct event
*e
= (struct event
*)(wdheader
->data
);
260 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",
261 e
->handle
, e
->dwAction
,
262 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
263 e
->hKernelPlugIn
, e
->dwOptions
,
264 e
->u
.Usb
.deviceId
.dwVendorId
,
265 e
->u
.Usb
.deviceId
.dwProductId
,
266 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
267 e
->dwNumMatchTables
);
270 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
276 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",
277 e
->handle
, e
->dwAction
,
278 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
279 e
->hKernelPlugIn
, e
->dwOptions
,
280 e
->u
.Usb
.deviceId
.dwVendorId
,
281 e
->u
.Usb
.deviceId
.dwProductId
,
282 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
283 e
->dwNumMatchTables
);
285 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
286 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
287 e
->matchTables
[i
].VendorId
,
288 e
->matchTables
[i
].ProductId
,
289 e
->matchTables
[i
].bDeviceClass
,
290 e
->matchTables
[i
].bDeviceSubClass
,
291 e
->matchTables
[i
].bInterfaceClass
,
292 e
->matchTables
[i
].bInterfaceSubClass
,
293 e
->matchTables
[i
].bInterfaceProtocol
);
300 DPRINTF("TRANSFER\n");
302 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
305 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
307 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
312 case MULTI_TRANSFER_OLD
:
314 DPRINTF("MULTI_TRANSFER\n");
316 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
317 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
319 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
321 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
326 case EVENT_UNREGISTER
:
328 struct event
*e
= (struct event
*)(wdheader
->data
);
330 DPRINTF("EVENT_UNREGISTER\n");
332 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
340 DPRINTF("INT_WAIT\n");
342 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
344 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
345 it
->hInterrupt
, it
->dwOptions
,
346 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
347 it
->dwLost
, it
->fStopped
);
350 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
352 ret
= xpcu_int_wait(it
);
355 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
356 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
357 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
362 case CARD_UNREGISTER
:
363 DPRINTF("CARD_UNREGISTER\n");
365 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
367 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
368 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
369 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
370 cr
->Card
.Item
[0].I
.IO
.dwBar
);
372 DPRINTF("-> hCard: %lu\n", cr
->hCard
);
375 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
378 pport
->close(cr
->hCard
);
386 DPRINTF("EVENT_PULL\n");
388 struct event
*e
= (struct event
*)(wdheader
->data
);
392 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",
393 e
->handle
, e
->dwAction
, e
->dwStatus
,
394 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
395 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
396 e
->u
.Usb
.deviceId
.dwProductId
,
397 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
398 e
->dwNumMatchTables
);
400 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
401 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
402 e
->matchTables
[i
].VendorId
,
403 e
->matchTables
[i
].ProductId
,
404 e
->matchTables
[i
].bDeviceClass
,
405 e
->matchTables
[i
].bDeviceSubClass
,
406 e
->matchTables
[i
].bInterfaceClass
,
407 e
->matchTables
[i
].bInterfaceSubClass
,
408 e
->matchTables
[i
].bInterfaceProtocol
);
412 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
418 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",
419 e
->handle
, e
->dwAction
, e
->dwStatus
,
420 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
421 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
422 e
->u
.Usb
.deviceId
.dwProductId
,
423 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
424 e
->dwNumMatchTables
);
426 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
427 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
428 e
->matchTables
[i
].VendorId
,
429 e
->matchTables
[i
].ProductId
,
430 e
->matchTables
[i
].bDeviceClass
,
431 e
->matchTables
[i
].bDeviceSubClass
,
432 e
->matchTables
[i
].bInterfaceClass
,
433 e
->matchTables
[i
].bInterfaceSubClass
,
434 e
->matchTables
[i
].bInterfaceProtocol
);
441 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
443 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
451 int ioctl(int fd
, unsigned long int request
, ...) {
457 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
459 va_start (args
, request
);
460 argp
= va_arg (args
, void *);
464 ret
= do_wdioctl(fd
, request
, argp
);
466 ret
= (*ioctl_func
) (fd
, request
, argp
);
471 int open (const char *pathname
, int flags
, ...) {
472 static int (*func
) (const char *, int, mode_t
) = NULL
;
478 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
480 if (flags
& O_CREAT
) {
481 va_start(args
, flags
);
482 mode
= va_arg(args
, mode_t
);
486 if (!strcmp (pathname
, "/dev/windrvr6")) {
487 DPRINTF("opening windrvr6\n");
489 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
491 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
497 return (*func
) (pathname
, flags
, mode
);
501 static int (*func
) (int) = NULL
;
504 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
506 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
507 DPRINTF("close windrvrfd\n");
514 FILE *fopen(const char *path
, const char *mode
) {
516 static FILE* (*func
) (const char*, const char*) = NULL
;
521 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
523 for (i
= 0; i
< 4; i
++) {
524 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
525 if (!strcmp(path
, buf
)) {
526 DPRINTF("open base-addr of parport%d\n", i
);
527 if (config_is_real_pport(i
)) {
528 ret
= (*func
) (path
, mode
);
530 ret
= (*func
) ("/dev/null", mode
);
542 ret
= (*func
) (path
, mode
);
544 if (!strcmp(path
, "/proc/modules")) {
545 DPRINTF("opening /proc/modules\n");
555 char *fgets(char *s
, int size
, FILE *stream
) {
556 static char* (*func
) (char*, int, FILE*) = NULL
;
557 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
563 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
565 if (modulesfp
== stream
) {
566 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
567 strcpy(s
, modules
[modules_read
]);
571 } else if (baseaddrfp
== stream
) {
572 snprintf(s
, sizeof(buf
), "%d\t%d\n",
573 (baseaddrnum
) * 0x10,
574 ((baseaddrnum
) * 0x10) + 0x400);
577 ret
= (*func
)(s
,size
,stream
);
583 int fclose(FILE *fp
) {
584 static int (*func
) (FILE*) = NULL
;
587 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
589 if (fp
== modulesfp
) {
593 if (fp
== baseaddrfp
) {
600 int access(const char *pathname
, int mode
) {
601 static int (*func
) (const char*, int);
604 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
606 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
609 return (*func
)(pathname
, mode
);
614 int uname (struct utsname
*__name
) {
615 static int (*func
) (struct utsname
*);
619 func
= (int (*) (struct utsname
*)) dlsym(RTLD_NEXT
, "uname");
621 ret
= (*func
)(__name
);
623 if (ret
== 0 && (!strcmp(__name
->machine
, "x86_64"))) {
624 strcpy(__name
->machine
, "i686");