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 <linux/parport.h>
43 #include <linux/ppdev.h>
44 #include "usb-driver.h"
46 static int (*ioctl_func
) (int, int, void *) = NULL
;
47 static int windrvrfd
= -1;
48 static int parportfd
= -1;
49 static int parportnum
= 0;
50 static unsigned long ppbase
= 0;
51 static unsigned long ecpbase
= 0;
52 FILE *modulesfp
= NULL
;
53 static int modules_read
= 0;
54 static struct usb_bus
*busses
= NULL
;
55 static struct usb_device
*usbdevice
;
56 static usb_dev_handle
*usb_devhandle
= NULL
;
57 static int usbinterface
= -1;
58 static unsigned long card_type
;
59 static int ints_enabled
= 0;
60 static pthread_mutex_t int_wait
= PTHREAD_MUTEX_INITIALIZER
;
65 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
66 void hexdump(unsigned char *buf
, int len
) {
69 for(i
=0; i
<len
; i
++) {
70 fprintf(stderr
,"%02x ", buf
[i
]);
77 #define DPRINTF(format, args...)
80 int usb_deviceinfo(unsigned char *buf
) {
83 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
84 WDU_INTERFACE
**pActiveInterface
;
87 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
89 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
90 udi
->Descriptor
.bDescriptorType
= usbdevice
->descriptor
.bDescriptorType
;
91 udi
->Descriptor
.bcdUSB
= usbdevice
->descriptor
.bcdUSB
;
92 udi
->Descriptor
.bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
93 udi
->Descriptor
.bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
94 udi
->Descriptor
.bDeviceProtocol
= usbdevice
->descriptor
.bDeviceProtocol
;
95 udi
->Descriptor
.bMaxPacketSize0
= usbdevice
->descriptor
.bMaxPacketSize0
;
96 udi
->Descriptor
.idVendor
= usbdevice
->descriptor
.idVendor
;
97 udi
->Descriptor
.idProduct
= usbdevice
->descriptor
.idProduct
;
98 udi
->Descriptor
.bcdDevice
= usbdevice
->descriptor
.bcdDevice
;
99 udi
->Descriptor
.iManufacturer
= usbdevice
->descriptor
.iManufacturer
;
100 udi
->Descriptor
.iProduct
= usbdevice
->descriptor
.iProduct
;
101 udi
->Descriptor
.iSerialNumber
= usbdevice
->descriptor
.iSerialNumber
;
102 udi
->Descriptor
.bNumConfigurations
= usbdevice
->descriptor
.bNumConfigurations
;
104 /* TODO: Fix Pipe0! */
105 udi
->Pipe0
.dwNumber
= 0x00;
106 udi
->Pipe0
.dwMaximumPacketSize
= usbdevice
->descriptor
.bMaxPacketSize0
;
108 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
109 udi
->Pipe0
.dwInterval
= 0;
111 pConfigs
= &(udi
->pConfigs
);
112 pActiveConfig
= &(udi
->pActiveConfig
);
113 pActiveInterface
= &(udi
->pActiveInterface
[0]);
116 len
= sizeof(struct usb_device_info
);
118 for (i
=0; i
<usbdevice
->descriptor
.bNumConfigurations
; i
++)
120 struct usb_config_descriptor
*conf_desc
= &usbdevice
->config
[i
];
121 WDU_INTERFACE
**pInterfaces
;
122 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
123 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
126 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
129 *pActiveConfig
= cfg
;
131 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
132 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
133 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
134 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
135 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
136 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
137 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
138 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
140 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
142 pInterfaces
= &(cfg
->pInterfaces
);
144 len
+= sizeof(WDU_CONFIGURATION
);
147 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
148 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
149 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
151 pActiveInterface
[j
] = iface
;
153 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
154 iface
->dwNumAltSettings
= usbdevice
->config
[i
].interface
[j
].num_altsetting
;
155 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
157 len
+= sizeof(WDU_INTERFACE
);
160 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
163 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
165 struct usb_interface
*interface
= &usbdevice
->config
[i
].interface
[j
];
168 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
170 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
173 for(k
=0; k
<interface
->num_altsetting
; k
++)
175 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
176 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
177 WDU_PIPE_INFO
**pPipes
;
180 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
182 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
183 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
184 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
185 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
186 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
187 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
188 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
189 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
190 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
191 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
192 pPipes
= &(altset
->pPipes
);
195 len
+=sizeof(WDU_ALTERNATE_SETTING
);
198 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
199 for (l
= 0; l
< bNumEndpoints
; l
++) {
200 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
202 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
203 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
204 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
205 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
206 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
207 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
209 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
212 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
213 for (l
= 0; l
< bNumEndpoints
; l
++) {
214 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
216 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
217 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
218 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
219 if (pi
->type
== PIPE_TYPE_CONTROL
)
220 pi
->direction
= WDU_DIR_IN_OUT
;
223 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
226 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
228 len
+= sizeof(WDU_PIPE_INFO
);
231 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
240 int pp_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, unsigned char *wdioctl
) {
244 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
245 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
246 tr
->fAutoinc
, tr
->dwOptions
);
251 if (tr
->cmdTrans
== 13)
252 DPRINTF("write byte: %d\n", val
);
256 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
261 switch(tr
->cmdTrans
) {
262 case 10: /* Read Byte */
263 if ((unsigned long)tr
->dwPort
== ppbase
) { /* Data Port */
264 ret
= 0; /* We don't support reading of the data port */
265 } else if ((unsigned long)tr
->dwPort
== ppbase
+ 1) { /* Status Port */
266 DPRINTF("status port\n");
267 ret
= ioctl(parportfd
, PPRSTATUS
, &val
);
268 } else if ((unsigned long)tr
->dwPort
== ppbase
+ 2) { /* Control Port */
269 DPRINTF("control port\n");
270 ret
= ioctl(parportfd
, PPRCONTROL
, &val
);
273 case 13: /* Write Byte */
274 if ((unsigned long)tr
->dwPort
== ppbase
) { /* Data Port */
275 DPRINTF("data port\n");
276 ret
= ioctl(parportfd
, PPWDATA
, &val
);
277 } else if ((unsigned long)tr
->dwPort
== ppbase
+ 1) { /* Status Port */
278 ret
= 0; /* Status Port is readonly */
279 } else if ((unsigned long)tr
->dwPort
== ppbase
+ 2) { /* Control Port */
280 DPRINTF("control port\n");
281 ret
= ioctl(parportfd
, PPWCONTROL
, &val
);
285 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
293 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
294 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
295 tr
->fAutoinc
, tr
->dwOptions
);
297 if (tr
->cmdTrans
== 10)
298 DPRINTF("read byte: %d\n", tr
->Data
.Byte
);
304 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
305 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
306 struct version_struct
*version
;
309 if (wdheader
->magic
!= MAGIC
) {
310 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
311 return (*ioctl_func
) (fd
, request
, wdioctl
);
314 switch(request
& ~(0xc0000000)) {
316 version
= (struct version_struct
*)(wdheader
->data
);
317 strcpy(version
->version
, "WinDriver no more");
318 version
->versionul
= 802;
319 DPRINTF("VERSION\n");
323 DPRINTF("LICENSE\n");
326 case CARD_REGISTER_OLD
:
328 DPRINTF("CARD_REGISTER\n");
330 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
333 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
335 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
336 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
337 cr
->Card
.Item
[0].I
.IO
.dwBar
);
339 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
341 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
342 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
343 cr
->Card
.Item
[1].I
.IO
.dwBar
);
345 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
348 snprintf(ppdev
, sizeof(ppdev
), "/dev/parport%d", parportnum
);
349 DPRINTF("opening %s\n", ppdev
);
350 parportfd
= open(ppdev
, O_RDWR
|O_EXCL
);
354 fprintf(stderr
,"Can't open %s: %s\n", ppdev
, strerror(errno
));
357 if (parportfd
>= 0) {
360 if (ioctl(parportfd
, PPCLAIM
) == -1)
363 pmode
= IEEE1284_MODE_COMPAT
;
364 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
367 if (cr
->Card
.dwItems
> 1) {
368 ecpbase
= cr
->Card
.Item
[1].I
.IO
.dwBytes
;
369 /* TODO: Implement ECP mode */
371 pmode
= IEEE1284_MODE_ECP
;
373 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1) {
374 pmode
= IEEE1284_MODE_COMPAT
;
375 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
381 cr
->hCard
= parportfd
;
382 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
385 DPRINTF("hCard: %lu\n", cr
->hCard
);
390 DPRINTF("in USB_TRANSFER");
392 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
395 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
396 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
397 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
398 DPRINTF("setup packet: ");
399 hexdump(ut
->SetupPacket
, 8);
401 if (!ut
->fRead
&& ut
->dwBufferSize
)
403 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
408 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
410 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
411 if (ut
->dwPipeNum
== 0) { /* control pipe */
412 int requesttype
, request
, value
, index
, size
;
413 requesttype
= ut
->SetupPacket
[0];
414 request
= ut
->SetupPacket
[1];
415 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
416 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
417 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
418 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
419 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
422 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
425 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
430 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
432 ut
->dwBytesTransferred
= ret
;
438 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
439 if (ut
->fRead
&& ut
->dwBytesTransferred
)
442 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
450 DPRINTF("INT_ENABLE\n");
452 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
454 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
455 it
->hInterrupt
, it
->dwOptions
,
456 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
457 it
->dwLost
, it
->fStopped
);
462 pthread_mutex_trylock(&int_wait
);
468 DPRINTF("INT_DISABLE\n");
470 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
472 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
473 it
->hInterrupt
, it
->dwOptions
,
474 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
475 it
->dwLost
, it
->fStopped
);
477 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
482 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
483 pthread_mutex_unlock(&int_wait
);
485 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
486 it
->hInterrupt
, it
->dwOptions
,
487 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
488 it
->dwLost
, it
->fStopped
);
492 case USB_SET_INTERFACE
:
493 DPRINTF("USB_SET_INTERFACE\n");
495 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
497 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
498 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
499 usi
->dwAlternateSetting
, usi
->dwOptions
);
501 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
505 usb_devhandle
= usb_open(usbdevice
);
507 /* FIXME: Select right interface! */
508 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
511 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
512 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
514 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
516 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
519 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
520 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
521 ret
, usb_strerror());
525 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
526 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
527 usi
->dwAlternateSetting
, usi
->dwOptions
);
531 case USB_GET_DEVICE_DATA_OLD
:
532 case USB_GET_DEVICE_DATA
:
533 DPRINTF("USB_GET_DEVICE_DATA\n");
535 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
538 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
539 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
542 pSize
= ugdd
->dwBytes
;
543 if (!ugdd
->dwBytes
) {
545 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
548 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
553 case EVENT_REGISTER_OLD
:
555 DPRINTF("EVENT_REGISTER\n");
557 struct event
*e
= (struct event
*)(wdheader
->data
);
561 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",
562 e
->handle
, e
->dwAction
,
563 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
564 e
->hKernelPlugIn
, e
->dwOptions
,
565 e
->u
.Usb
.deviceId
.dwVendorId
,
566 e
->u
.Usb
.deviceId
.dwProductId
,
567 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
568 e
->dwNumMatchTables
);
570 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
572 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
573 e
->matchTables
[i
].VendorId
,
574 e
->matchTables
[i
].ProductId
,
575 e
->matchTables
[i
].bDeviceClass
,
576 e
->matchTables
[i
].bDeviceSubClass
,
577 e
->matchTables
[i
].bInterfaceClass
,
578 e
->matchTables
[i
].bInterfaceSubClass
,
579 e
->matchTables
[i
].bInterfaceProtocol
);
581 for (bus
= busses
; bus
; bus
= bus
->next
) {
582 struct usb_device
*dev
;
584 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
585 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
587 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
588 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
589 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
590 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
592 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
593 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
596 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
598 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
599 interface
->altsetting
[i
].bInterfaceClass
,
600 interface
->altsetting
[i
].bInterfaceSubClass
,
601 interface
->altsetting
[i
].bInterfaceProtocol
);
603 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
604 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
605 /* TODO: check interfaceClass! */
606 DPRINTF("found device with libusb\n");
608 card_type
= e
->dwCardType
;
618 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
624 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",
625 e
->handle
, e
->dwAction
,
626 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
627 e
->hKernelPlugIn
, e
->dwOptions
,
628 e
->u
.Usb
.deviceId
.dwVendorId
,
629 e
->u
.Usb
.deviceId
.dwProductId
,
630 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
631 e
->dwNumMatchTables
);
633 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
634 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
635 e
->matchTables
[i
].VendorId
,
636 e
->matchTables
[i
].ProductId
,
637 e
->matchTables
[i
].bDeviceClass
,
638 e
->matchTables
[i
].bDeviceSubClass
,
639 e
->matchTables
[i
].bInterfaceClass
,
640 e
->matchTables
[i
].bInterfaceSubClass
,
641 e
->matchTables
[i
].bInterfaceProtocol
);
648 DPRINTF("TRANSFER\n");
650 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
652 ret
= pp_transfer(tr
, fd
, request
, wdioctl
);
656 case MULTI_TRANSFER_OLD
:
658 DPRINTF("MULTI_TRANSFER\n");
660 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
661 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
665 for (i
= 0; i
< num
; i
++) {
666 DPRINTF("Transfer %d:\n", i
+1);
668 wdheader
->size
= sizeof(WD_TRANSFER
);
670 wdheader
->data
= tr
+ i
;
672 ret
= pp_transfer(tr
+ i
, fd
, request
, wdioctl
);
683 case EVENT_UNREGISTER
:
684 DPRINTF("EVENT_UNREGISTER\n");
686 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
691 DPRINTF("INT_WAIT\n");
693 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
695 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
696 it
->hInterrupt
, it
->dwOptions
,
697 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
698 it
->dwLost
, it
->fStopped
);
701 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
704 if (it
->dwCounter
== 0) {
707 pthread_mutex_lock(&int_wait
);
708 pthread_mutex_unlock(&int_wait
);
711 pthread_mutex_lock(&int_wait
);
712 pthread_mutex_unlock(&int_wait
);
716 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
717 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
718 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
723 case CARD_UNREGISTER
:
724 DPRINTF("CARD_UNREGISTER\n");
726 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
728 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
729 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
730 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
731 cr
->Card
.Item
[0].I
.IO
.dwBar
);
733 DPRINTF("hCard: %lu\n", cr
->hCard
);
736 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
738 if (parportfd
== cr
->hCard
&& parportfd
>= 0) {
739 ioctl(parportfd
, PPRELEASE
);
749 DPRINTF("EVENT_PULL\n");
751 struct event
*e
= (struct event
*)(wdheader
->data
);
755 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",
756 e
->handle
, e
->dwAction
, e
->dwStatus
,
757 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
758 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
759 e
->u
.Usb
.deviceId
.dwProductId
,
760 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
761 e
->dwNumMatchTables
);
763 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
764 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
765 e
->matchTables
[i
].VendorId
,
766 e
->matchTables
[i
].ProductId
,
767 e
->matchTables
[i
].bDeviceClass
,
768 e
->matchTables
[i
].bDeviceSubClass
,
769 e
->matchTables
[i
].bInterfaceClass
,
770 e
->matchTables
[i
].bInterfaceSubClass
,
771 e
->matchTables
[i
].bInterfaceProtocol
);
775 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
778 struct usb_interface
*interface
= usbdevice
->config
->interface
;
780 e
->dwCardType
= card_type
;
783 e
->u
.Usb
.dwUniqueID
= 110;
784 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
785 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
786 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
787 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
788 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
789 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
790 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
795 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",
796 e
->handle
, e
->dwAction
, e
->dwStatus
,
797 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
798 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
799 e
->u
.Usb
.deviceId
.dwProductId
,
800 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
801 e
->dwNumMatchTables
);
803 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
804 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
805 e
->matchTables
[i
].VendorId
,
806 e
->matchTables
[i
].ProductId
,
807 e
->matchTables
[i
].bDeviceClass
,
808 e
->matchTables
[i
].bDeviceSubClass
,
809 e
->matchTables
[i
].bInterfaceClass
,
810 e
->matchTables
[i
].bInterfaceSubClass
,
811 e
->matchTables
[i
].bInterfaceProtocol
);
818 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
820 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
828 int ioctl(int fd
, unsigned long int request
, ...) {
834 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
836 va_start (args
, request
);
837 argp
= va_arg (args
, void *);
841 ret
= do_wdioctl(fd
, request
, argp
);
843 ret
= (*ioctl_func
) (fd
, request
, argp
);
848 int open (const char *pathname
, int flags
, ...) {
849 static int (*func
) (const char *, int, mode_t
) = NULL
;
855 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
857 if (flags
& O_CREAT
) {
858 va_start(args
, flags
);
859 mode
= va_arg(args
, mode_t
);
863 if (!strcmp (pathname
, "/dev/windrvr6")) {
864 DPRINTF("opening windrvr6\n");
866 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
868 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
875 busses
= usb_get_busses();
881 return (*func
) (pathname
, flags
, mode
);
885 static int (*func
) (int) = NULL
;
888 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
890 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
891 DPRINTF("close windrvrfd\n");
892 if (usbinterface
>= 0)
893 usb_release_interface(usb_devhandle
, usbinterface
);
896 usb_close(usb_devhandle
);
898 usb_devhandle
= NULL
;
906 FILE *fopen(const char *path
, const char *mode
) {
908 static FILE* (*func
) (const char*, const char*) = NULL
;
911 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
913 ret
= (*func
) (path
, mode
);
915 if (!strcmp (path
, "/proc/modules")) {
916 DPRINTF("opening /proc/modules\n");
926 char *fgets(char *s
, int size
, FILE *stream
) {
927 static char* (*func
) (char*, int, FILE*) = NULL
;
928 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
933 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
935 if (modulesfp
== stream
) {
936 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
937 strcpy(s
, modules
[modules_read
]);
942 ret
= (*func
)(s
,size
,stream
);
948 int fclose(FILE *fp
) {
949 static int (*func
) (FILE*) = NULL
;
952 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
954 if (fp
== modulesfp
) {
961 int access(const char *pathname
, int mode
) {
962 static int (*func
) (const char*, int);
965 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
967 if (!strcmp(pathname
, "/dev/windrvr6")) {
970 return (*func
)(pathname
, mode
);