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>
41 #include <sys/ioctl.h>
42 #include <sys/utsname.h>
43 #include <bits/wordsize.h>
44 #include "usb-driver.h"
48 static int (*ioctl_func
) (int, int, void *) = NULL
;
49 static int windrvrfd
= -1;
50 static unsigned long ppbase
= 0;
51 static unsigned long ecpbase
= 0;
52 static struct parport_config
*pport
= NULL
;
53 static struct xpcu_s
*xpcu
= NULL
;
54 static FILE *modulesfp
= NULL
;
55 static FILE *baseaddrfp
= NULL
;
56 static int baseaddrnum
= 0;
57 static int modules_read
= 0;
58 static int ints_enabled
= 0;
59 static pthread_mutex_t int_wait
= PTHREAD_MUTEX_INITIALIZER
;
63 void hexdump(unsigned char *buf
, int len
) {
66 for(i
=0; i
<len
; i
++) {
67 fprintf(stderr
,"%02x ", buf
[i
]);
75 static int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
76 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
77 struct version_struct
*version
;
80 if (wdheader
->magic
!= MAGIC
) {
81 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
82 return (*ioctl_func
) (fd
, request
, wdioctl
);
85 switch(request
& ~(0xc0000000)) {
87 version
= (struct version_struct
*)(wdheader
->data
);
88 strcpy(version
->version
, "libusb-driver.so version: " USB_DRIVER_VERSION
);
89 version
->versionul
= 802;
97 case CARD_REGISTER_OLD
:
99 DPRINTF("CARD_REGISTER\n");
101 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
103 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
105 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
106 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
107 cr
->Card
.Item
[0].I
.IO
.dwBar
);
109 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
111 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
112 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
113 cr
->Card
.Item
[1].I
.IO
.dwBar
);
115 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
118 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
122 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
124 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
126 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
127 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
135 DPRINTF("hCard: %lu\n", cr
->hCard
);
140 DPRINTF("in USB_TRANSFER");
142 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
145 DPRINTF(" unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
146 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
147 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
148 if (ut
->dwPipeNum
== 0) {
149 DPRINTF("setup packet: ");
150 hexdump(ut
->SetupPacket
, 8);
153 if (!ut
->fRead
&& ut
->dwBufferSize
)
155 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
160 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
162 xpcu_transfer(xpcu
, ut
);
166 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
167 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
);
190 pthread_mutex_trylock(&int_wait
);
196 DPRINTF("INT_DISABLE\n");
198 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
200 DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
201 it
->hInterrupt
, it
->dwOptions
,
202 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
203 it
->dwLost
, it
->fStopped
);
205 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
210 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
211 pthread_mutex_unlock(&int_wait
);
213 DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
214 it
->hInterrupt
, it
->dwOptions
,
215 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
216 it
->dwLost
, it
->fStopped
);
220 case USB_SET_INTERFACE
:
221 DPRINTF("USB_SET_INTERFACE\n");
223 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
225 DPRINTF("unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
226 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
227 usi
->dwAlternateSetting
, usi
->dwOptions
);
229 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
231 xpcu_set_interface(xpcu
, usi
);
233 DPRINTF("unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
234 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
235 usi
->dwAlternateSetting
, usi
->dwOptions
);
239 case USB_GET_DEVICE_DATA_OLD
:
240 case USB_GET_DEVICE_DATA
:
241 DPRINTF("USB_GET_DEVICE_DATA\n");
243 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
245 DPRINTF("unique: 0x%lx, bytes: %lu, options: %lx\n",
246 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
249 ugdd
->dwBytes
= xpcu_deviceinfo(xpcu
, ugdd
);
254 case EVENT_REGISTER_OLD
:
256 DPRINTF("EVENT_REGISTER\n");
258 struct event
*e
= (struct event
*)(wdheader
->data
);
263 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",
264 e
->handle
, e
->dwAction
,
265 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
266 e
->hKernelPlugIn
, e
->dwOptions
,
267 e
->u
.Usb
.deviceId
.dwVendorId
,
268 e
->u
.Usb
.deviceId
.dwProductId
,
269 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
270 e
->dwNumMatchTables
);
274 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
276 e
->handle
= (unsigned long)xpcu
;
280 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",
281 e
->handle
, e
->dwAction
,
282 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
283 e
->hKernelPlugIn
, e
->dwOptions
,
284 e
->u
.Usb
.deviceId
.dwVendorId
,
285 e
->u
.Usb
.deviceId
.dwProductId
,
286 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
287 e
->dwNumMatchTables
);
289 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
290 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
291 e
->matchTables
[i
].VendorId
,
292 e
->matchTables
[i
].ProductId
,
293 e
->matchTables
[i
].bDeviceClass
,
294 e
->matchTables
[i
].bDeviceSubClass
,
295 e
->matchTables
[i
].bInterfaceClass
,
296 e
->matchTables
[i
].bInterfaceSubClass
,
297 e
->matchTables
[i
].bInterfaceProtocol
);
304 DPRINTF("TRANSFER\n");
306 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
309 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
311 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
316 case MULTI_TRANSFER_OLD
:
318 DPRINTF("MULTI_TRANSFER\n");
320 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
321 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
323 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
325 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
330 case EVENT_UNREGISTER
:
332 struct event
*e
= (struct event
*)(wdheader
->data
);
334 DPRINTF("EVENT_UNREGISTER\n");
336 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
344 DPRINTF("INT_WAIT\n");
346 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
348 DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
349 it
->hInterrupt
, it
->dwOptions
,
350 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
351 it
->dwLost
, it
->fStopped
);
354 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
357 if (it
->dwCounter
== 0) {
360 pthread_mutex_lock(&int_wait
);
361 pthread_mutex_unlock(&int_wait
);
364 pthread_mutex_lock(&int_wait
);
365 pthread_mutex_unlock(&int_wait
);
369 DPRINTF("INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
370 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
371 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
376 case CARD_UNREGISTER
:
377 DPRINTF("CARD_UNREGISTER\n");
379 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
381 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
382 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
383 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
384 cr
->Card
.Item
[0].I
.IO
.dwBar
);
386 DPRINTF("hCard: %lu\n", cr
->hCard
);
389 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
392 pport
->close(cr
->hCard
);
400 DPRINTF("EVENT_PULL\n");
402 struct event
*e
= (struct event
*)(wdheader
->data
);
406 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",
407 e
->handle
, e
->dwAction
, e
->dwStatus
,
408 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
409 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
410 e
->u
.Usb
.deviceId
.dwProductId
,
411 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
412 e
->dwNumMatchTables
);
414 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
415 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
416 e
->matchTables
[i
].VendorId
,
417 e
->matchTables
[i
].ProductId
,
418 e
->matchTables
[i
].bDeviceClass
,
419 e
->matchTables
[i
].bDeviceSubClass
,
420 e
->matchTables
[i
].bInterfaceClass
,
421 e
->matchTables
[i
].bInterfaceSubClass
,
422 e
->matchTables
[i
].bInterfaceProtocol
);
426 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
432 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",
433 e
->handle
, e
->dwAction
, e
->dwStatus
,
434 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
435 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
436 e
->u
.Usb
.deviceId
.dwProductId
,
437 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
438 e
->dwNumMatchTables
);
440 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
441 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
442 e
->matchTables
[i
].VendorId
,
443 e
->matchTables
[i
].ProductId
,
444 e
->matchTables
[i
].bDeviceClass
,
445 e
->matchTables
[i
].bDeviceSubClass
,
446 e
->matchTables
[i
].bInterfaceClass
,
447 e
->matchTables
[i
].bInterfaceSubClass
,
448 e
->matchTables
[i
].bInterfaceProtocol
);
455 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
457 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
465 int ioctl(int fd
, unsigned long int request
, ...) {
471 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
473 va_start (args
, request
);
474 argp
= va_arg (args
, void *);
478 ret
= do_wdioctl(fd
, request
, argp
);
480 ret
= (*ioctl_func
) (fd
, request
, argp
);
485 int open (const char *pathname
, int flags
, ...) {
486 static int (*func
) (const char *, int, mode_t
) = NULL
;
492 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
494 if (flags
& O_CREAT
) {
495 va_start(args
, flags
);
496 mode
= va_arg(args
, mode_t
);
500 if (!strcmp (pathname
, "/dev/windrvr6")) {
501 DPRINTF("opening windrvr6\n");
503 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
505 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
511 return (*func
) (pathname
, flags
, mode
);
515 static int (*func
) (int) = NULL
;
518 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
520 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
521 DPRINTF("close windrvrfd\n");
530 FILE *fopen(const char *path
, const char *mode
) {
532 static FILE* (*func
) (const char*, const char*) = NULL
;
537 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
539 for (i
= 0; i
< 4; i
++) {
540 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
541 if (!strcmp(path
, buf
)) {
542 DPRINTF("open base-addr of parport%d\n", i
);
543 if (config_is_real_pport(i
)) {
544 ret
= (*func
) (path
, mode
);
546 ret
= (*func
) ("/dev/null", mode
);
558 ret
= (*func
) (path
, mode
);
560 if (!strcmp(path
, "/proc/modules")) {
561 DPRINTF("opening /proc/modules\n");
571 char *fgets(char *s
, int size
, FILE *stream
) {
572 static char* (*func
) (char*, int, FILE*) = NULL
;
573 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
579 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
581 if (modulesfp
== stream
) {
582 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
583 strcpy(s
, modules
[modules_read
]);
587 } else if (baseaddrfp
== stream
) {
588 snprintf(s
, sizeof(buf
), "%d\t%d\n",
589 (baseaddrnum
) * 0x10,
590 ((baseaddrnum
) * 0x10) + 0x400);
593 ret
= (*func
)(s
,size
,stream
);
599 int fclose(FILE *fp
) {
600 static int (*func
) (FILE*) = NULL
;
603 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
605 if (fp
== modulesfp
) {
609 if (fp
== baseaddrfp
) {
616 int access(const char *pathname
, int mode
) {
617 static int (*func
) (const char*, int);
620 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
622 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
625 return (*func
)(pathname
, mode
);
630 int uname (struct utsname
*__name
) {
631 static int (*func
) (struct utsname
*);
635 func
= (int (*) (struct utsname
*)) dlsym(RTLD_NEXT
, "uname");
637 ret
= (*func
)(__name
);
639 if (ret
== 0 && (!strcmp(__name
->machine
, "x86_64"))) {
640 strcpy(__name
->machine
, "i686");