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"
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 FILE *modulesfp
= NULL
;
53 static FILE *baseaddrfp
= NULL
;
54 static int baseaddrnum
= 0;
55 static int modules_read
= 0;
56 static struct usb_bus
*busses
= NULL
;
57 static struct usb_device
*usbdevice
;
58 static usb_dev_handle
*usb_devhandle
= NULL
;
59 static int usbinterface
= -1;
60 static unsigned long card_type
;
61 static int ints_enabled
= 0;
62 static pthread_mutex_t int_wait
= PTHREAD_MUTEX_INITIALIZER
;
66 void hexdump(unsigned char *buf
, int len
) {
69 for(i
=0; i
<len
; i
++) {
70 fprintf(stderr
,"%02x ", buf
[i
]);
77 static int usb_deviceinfo(unsigned char *buf
) {
80 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
81 WDU_INTERFACE
**pActiveInterface
;
84 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
86 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
87 udi
->Descriptor
.bDescriptorType
= usbdevice
->descriptor
.bDescriptorType
;
88 udi
->Descriptor
.bcdUSB
= usbdevice
->descriptor
.bcdUSB
;
89 udi
->Descriptor
.bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
90 udi
->Descriptor
.bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
91 udi
->Descriptor
.bDeviceProtocol
= usbdevice
->descriptor
.bDeviceProtocol
;
92 udi
->Descriptor
.bMaxPacketSize0
= usbdevice
->descriptor
.bMaxPacketSize0
;
93 udi
->Descriptor
.idVendor
= usbdevice
->descriptor
.idVendor
;
94 udi
->Descriptor
.idProduct
= usbdevice
->descriptor
.idProduct
;
95 udi
->Descriptor
.bcdDevice
= usbdevice
->descriptor
.bcdDevice
;
96 udi
->Descriptor
.iManufacturer
= usbdevice
->descriptor
.iManufacturer
;
97 udi
->Descriptor
.iProduct
= usbdevice
->descriptor
.iProduct
;
98 udi
->Descriptor
.iSerialNumber
= usbdevice
->descriptor
.iSerialNumber
;
99 udi
->Descriptor
.bNumConfigurations
= usbdevice
->descriptor
.bNumConfigurations
;
101 /* TODO: Fix Pipe0! */
102 udi
->Pipe0
.dwNumber
= 0x00;
103 udi
->Pipe0
.dwMaximumPacketSize
= usbdevice
->descriptor
.bMaxPacketSize0
;
105 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
106 udi
->Pipe0
.dwInterval
= 0;
108 pConfigs
= &(udi
->pConfigs
);
109 pActiveConfig
= &(udi
->pActiveConfig
);
110 pActiveInterface
= &(udi
->pActiveInterface
[0]);
113 len
= sizeof(struct usb_device_info
);
115 for (i
=0; i
<usbdevice
->descriptor
.bNumConfigurations
; i
++)
117 struct usb_config_descriptor
*conf_desc
= &usbdevice
->config
[i
];
118 WDU_INTERFACE
**pInterfaces
;
119 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
120 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
123 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
126 *pActiveConfig
= cfg
;
128 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
129 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
130 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
131 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
132 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
133 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
134 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
135 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
137 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
139 pInterfaces
= &(cfg
->pInterfaces
);
141 len
+= sizeof(WDU_CONFIGURATION
);
144 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
145 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
146 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
148 pActiveInterface
[j
] = iface
;
150 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
151 iface
->dwNumAltSettings
= usbdevice
->config
[i
].interface
[j
].num_altsetting
;
152 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
154 len
+= sizeof(WDU_INTERFACE
);
157 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
160 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
162 struct usb_interface
*interface
= &usbdevice
->config
[i
].interface
[j
];
165 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
167 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
170 for(k
=0; k
<interface
->num_altsetting
; k
++)
172 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
173 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
174 WDU_PIPE_INFO
**pPipes
;
177 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
179 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
180 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
181 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
182 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
183 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
184 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
185 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
186 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
187 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
188 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
189 pPipes
= &(altset
->pPipes
);
192 len
+=sizeof(WDU_ALTERNATE_SETTING
);
195 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
196 for (l
= 0; l
< bNumEndpoints
; l
++) {
197 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
199 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
200 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
201 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
202 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
203 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
204 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
206 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
209 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
210 for (l
= 0; l
< bNumEndpoints
; l
++) {
211 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
213 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
214 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
215 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
216 if (pi
->type
== PIPE_TYPE_CONTROL
)
217 pi
->direction
= WDU_DIR_IN_OUT
;
220 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
223 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
225 len
+= sizeof(WDU_PIPE_INFO
);
228 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
237 static int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
238 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
239 struct version_struct
*version
;
242 if (wdheader
->magic
!= MAGIC
) {
243 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
244 return (*ioctl_func
) (fd
, request
, wdioctl
);
247 switch(request
& ~(0xc0000000)) {
249 version
= (struct version_struct
*)(wdheader
->data
);
250 strcpy(version
->version
, "libusb-driver.so version: " USB_DRIVER_VERSION
);
251 version
->versionul
= 802;
252 DPRINTF("VERSION\n");
256 DPRINTF("LICENSE\n");
259 case CARD_REGISTER_OLD
:
261 DPRINTF("CARD_REGISTER\n");
263 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
265 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
267 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
268 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
269 cr
->Card
.Item
[0].I
.IO
.dwBar
);
271 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
273 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
274 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
275 cr
->Card
.Item
[1].I
.IO
.dwBar
);
277 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
280 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
284 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
286 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
288 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
289 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
297 DPRINTF("hCard: %lu\n", cr
->hCard
);
302 DPRINTF("in USB_TRANSFER");
304 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
307 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
308 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
309 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
310 DPRINTF("setup packet: ");
311 hexdump(ut
->SetupPacket
, 8);
313 if (!ut
->fRead
&& ut
->dwBufferSize
)
315 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
320 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
322 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
323 if (ut
->dwPipeNum
== 0) { /* control pipe */
324 int requesttype
, request
, value
, index
, size
;
325 requesttype
= ut
->SetupPacket
[0];
326 request
= ut
->SetupPacket
[1];
327 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
328 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
329 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
330 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
331 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
334 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
337 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
342 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
344 ut
->dwBytesTransferred
= ret
;
350 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
351 if (ut
->fRead
&& ut
->dwBytesTransferred
)
354 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
362 DPRINTF("INT_ENABLE\n");
364 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
366 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
367 it
->hInterrupt
, it
->dwOptions
,
368 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
369 it
->dwLost
, it
->fStopped
);
374 pthread_mutex_trylock(&int_wait
);
380 DPRINTF("INT_DISABLE\n");
382 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
384 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
385 it
->hInterrupt
, it
->dwOptions
,
386 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
387 it
->dwLost
, it
->fStopped
);
389 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
394 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
395 pthread_mutex_unlock(&int_wait
);
397 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
398 it
->hInterrupt
, it
->dwOptions
,
399 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
400 it
->dwLost
, it
->fStopped
);
404 case USB_SET_INTERFACE
:
405 DPRINTF("USB_SET_INTERFACE\n");
407 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
409 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
410 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
411 usi
->dwAlternateSetting
, usi
->dwOptions
);
413 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
417 usb_devhandle
= usb_open(usbdevice
);
419 /* FIXME: Select right interface! */
420 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
423 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
424 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
426 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
428 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
431 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
432 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
433 ret
, usb_strerror());
437 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
438 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
439 usi
->dwAlternateSetting
, usi
->dwOptions
);
443 case USB_GET_DEVICE_DATA_OLD
:
444 case USB_GET_DEVICE_DATA
:
445 DPRINTF("USB_GET_DEVICE_DATA\n");
447 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
450 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
451 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
454 pSize
= ugdd
->dwBytes
;
455 if (!ugdd
->dwBytes
) {
457 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
460 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
465 case EVENT_REGISTER_OLD
:
467 DPRINTF("EVENT_REGISTER\n");
469 struct event
*e
= (struct event
*)(wdheader
->data
);
472 int busnum
= -1, devnum
= -1;
475 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
476 e
->handle
, e
->dwAction
,
477 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
478 e
->hKernelPlugIn
, e
->dwOptions
,
479 e
->u
.Usb
.deviceId
.dwVendorId
,
480 e
->u
.Usb
.deviceId
.dwProductId
,
481 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
482 e
->dwNumMatchTables
);
484 devpos
= getenv("XILINX_USB_DEV");
485 if (devpos
!= NULL
) {
487 char *devstr
= NULL
, *remainder
;
489 DPRINTF("XILINX_USB_DEV=%s\n", devpos
);
491 for (j
= 0; j
< strlen(devpos
) && devpos
[j
] != 0; j
++) {
492 if (devpos
[j
] == ':') {
494 devstr
= &(devpos
[j
+1]);
498 if (devstr
&& strlen(devstr
) > 0) {
499 busnum
= strtol(devpos
, &remainder
, 10);
500 if (devpos
== remainder
) {
503 devnum
= strtol(devstr
, &remainder
, 10);
504 if (devstr
== remainder
) {
508 fprintf(stderr
,"Using XILINX platform cable USB at %03d:%03d\n",
515 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
517 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
518 e
->matchTables
[i
].VendorId
,
519 e
->matchTables
[i
].ProductId
,
520 e
->matchTables
[i
].bDeviceClass
,
521 e
->matchTables
[i
].bDeviceSubClass
,
522 e
->matchTables
[i
].bInterfaceClass
,
523 e
->matchTables
[i
].bInterfaceSubClass
,
524 e
->matchTables
[i
].bInterfaceProtocol
);
526 for (bus
= busses
; bus
; bus
= bus
->next
) {
527 struct usb_device
*dev
;
529 if ((devnum
!= -1) && (strtol(bus
->dirname
, NULL
, 10) != busnum
))
532 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
533 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
535 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
536 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
537 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
538 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
) &&
539 ((devnum
== -1) || (strtol(dev
->filename
, NULL
, 10) == devnum
)) ) {
541 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
542 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
545 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
547 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
548 interface
->altsetting
[i
].bInterfaceClass
,
549 interface
->altsetting
[i
].bInterfaceSubClass
,
550 interface
->altsetting
[i
].bInterfaceProtocol
);
552 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
553 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
554 /* TODO: check interfaceClass! */
555 DPRINTF("found device with libusb\n");
557 card_type
= e
->dwCardType
;
567 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
573 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
574 e
->handle
, e
->dwAction
,
575 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
576 e
->hKernelPlugIn
, e
->dwOptions
,
577 e
->u
.Usb
.deviceId
.dwVendorId
,
578 e
->u
.Usb
.deviceId
.dwProductId
,
579 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
580 e
->dwNumMatchTables
);
582 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
583 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
584 e
->matchTables
[i
].VendorId
,
585 e
->matchTables
[i
].ProductId
,
586 e
->matchTables
[i
].bDeviceClass
,
587 e
->matchTables
[i
].bDeviceSubClass
,
588 e
->matchTables
[i
].bInterfaceClass
,
589 e
->matchTables
[i
].bInterfaceSubClass
,
590 e
->matchTables
[i
].bInterfaceProtocol
);
597 DPRINTF("TRANSFER\n");
599 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
602 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
604 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
609 case MULTI_TRANSFER_OLD
:
611 DPRINTF("MULTI_TRANSFER\n");
613 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
614 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
616 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
618 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
623 case EVENT_UNREGISTER
:
624 DPRINTF("EVENT_UNREGISTER\n");
626 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
631 DPRINTF("INT_WAIT\n");
633 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
635 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
636 it
->hInterrupt
, it
->dwOptions
,
637 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
638 it
->dwLost
, it
->fStopped
);
641 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
644 if (it
->dwCounter
== 0) {
647 pthread_mutex_lock(&int_wait
);
648 pthread_mutex_unlock(&int_wait
);
651 pthread_mutex_lock(&int_wait
);
652 pthread_mutex_unlock(&int_wait
);
656 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
657 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
658 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
663 case CARD_UNREGISTER
:
664 DPRINTF("CARD_UNREGISTER\n");
666 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
668 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
669 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
670 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
671 cr
->Card
.Item
[0].I
.IO
.dwBar
);
673 DPRINTF("hCard: %lu\n", cr
->hCard
);
676 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
679 pport
->close(cr
->hCard
);
687 DPRINTF("EVENT_PULL\n");
689 struct event
*e
= (struct event
*)(wdheader
->data
);
693 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
694 e
->handle
, e
->dwAction
, e
->dwStatus
,
695 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
696 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
697 e
->u
.Usb
.deviceId
.dwProductId
,
698 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
699 e
->dwNumMatchTables
);
701 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
702 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
703 e
->matchTables
[i
].VendorId
,
704 e
->matchTables
[i
].ProductId
,
705 e
->matchTables
[i
].bDeviceClass
,
706 e
->matchTables
[i
].bDeviceSubClass
,
707 e
->matchTables
[i
].bInterfaceClass
,
708 e
->matchTables
[i
].bInterfaceSubClass
,
709 e
->matchTables
[i
].bInterfaceProtocol
);
713 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
716 struct usb_interface
*interface
= usbdevice
->config
->interface
;
718 e
->dwCardType
= card_type
;
721 e
->u
.Usb
.dwUniqueID
= 110;
722 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
723 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
724 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
725 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
726 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
727 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
728 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
733 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
734 e
->handle
, e
->dwAction
, e
->dwStatus
,
735 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
736 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
737 e
->u
.Usb
.deviceId
.dwProductId
,
738 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
739 e
->dwNumMatchTables
);
741 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
742 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
743 e
->matchTables
[i
].VendorId
,
744 e
->matchTables
[i
].ProductId
,
745 e
->matchTables
[i
].bDeviceClass
,
746 e
->matchTables
[i
].bDeviceSubClass
,
747 e
->matchTables
[i
].bInterfaceClass
,
748 e
->matchTables
[i
].bInterfaceSubClass
,
749 e
->matchTables
[i
].bInterfaceProtocol
);
756 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
758 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
766 int ioctl(int fd
, unsigned long int request
, ...) {
772 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
774 va_start (args
, request
);
775 argp
= va_arg (args
, void *);
779 ret
= do_wdioctl(fd
, request
, argp
);
781 ret
= (*ioctl_func
) (fd
, request
, argp
);
786 int open (const char *pathname
, int flags
, ...) {
787 static int (*func
) (const char *, int, mode_t
) = NULL
;
793 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
795 if (flags
& O_CREAT
) {
796 va_start(args
, flags
);
797 mode
= va_arg(args
, mode_t
);
801 if (!strcmp (pathname
, "/dev/windrvr6")) {
802 DPRINTF("opening windrvr6\n");
804 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
806 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
813 busses
= usb_get_busses();
819 return (*func
) (pathname
, flags
, mode
);
823 static int (*func
) (int) = NULL
;
826 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
828 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
829 DPRINTF("close windrvrfd\n");
830 if (usbinterface
>= 0)
831 usb_release_interface(usb_devhandle
, usbinterface
);
835 usb_reset(usb_devhandle
);
837 usb_close(usb_devhandle
);
840 usb_devhandle
= NULL
;
848 FILE *fopen(const char *path
, const char *mode
) {
850 static FILE* (*func
) (const char*, const char*) = NULL
;
855 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
857 for (i
= 0; i
< 4; i
++) {
858 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
859 if (!strcmp(path
, buf
)) {
860 DPRINTF("open base-addr of parport%d\n", i
);
861 if (config_is_real_pport(i
)) {
862 ret
= (*func
) (path
, mode
);
864 ret
= (*func
) ("/dev/null", mode
);
876 ret
= (*func
) (path
, mode
);
878 if (!strcmp(path
, "/proc/modules")) {
879 DPRINTF("opening /proc/modules\n");
889 char *fgets(char *s
, int size
, FILE *stream
) {
890 static char* (*func
) (char*, int, FILE*) = NULL
;
891 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
897 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
899 if (modulesfp
== stream
) {
900 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
901 strcpy(s
, modules
[modules_read
]);
905 } else if (baseaddrfp
== stream
) {
906 snprintf(s
, sizeof(buf
), "%d\t%d\n",
907 (baseaddrnum
) * 0x10,
908 ((baseaddrnum
) * 0x10) + 0x400);
911 ret
= (*func
)(s
,size
,stream
);
917 int fclose(FILE *fp
) {
918 static int (*func
) (FILE*) = NULL
;
921 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
923 if (fp
== modulesfp
) {
927 if (fp
== baseaddrfp
) {
934 int access(const char *pathname
, int mode
) {
935 static int (*func
) (const char*, int);
938 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
940 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
943 return (*func
)(pathname
, mode
);
948 int uname (struct utsname
*__name
) {
949 static int (*func
) (struct utsname
*);
953 func
= (int (*) (struct utsname
*)) dlsym(RTLD_NEXT
, "uname");
955 ret
= (*func
)(__name
);
957 if (ret
== 0 && (!strcmp(__name
->machine
, "x86_64"))) {
958 strcpy(__name
->machine
, "i686");