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 unsigned long ppbase
= 0;
50 static unsigned long ecpbase
= 0;
51 static struct pports
*pplist
= NULL
;
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
) {
242 unsigned long port
= (unsigned long)tr
->dwPort
;
245 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
246 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
247 tr
->fAutoinc
, tr
->dwOptions
);
252 if (tr
->cmdTrans
== 13)
253 DPRINTF("write byte: %d\n", val
);
257 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
262 if (port
== ppbase
+ PP_DATA
) {
263 DPRINTF("data port\n");
264 switch(tr
->cmdTrans
) {
266 ret
= 0; /* We don't support reading of the data port */
270 ret
= ioctl(parportfd
, PPWDATA
, &val
);
274 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
278 } else if (port
== ppbase
+ PP_STATUS
) {
279 DPRINTF("status port\n");
280 switch(tr
->cmdTrans
) {
282 ret
= ioctl(parportfd
, PPRSTATUS
, &val
);
286 ret
= 0; /* Status Port is readonly */
290 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
294 } else if (port
== ppbase
+ PP_CONTROL
) {
295 DPRINTF("control port\n");
296 switch(tr
->cmdTrans
) {
298 ret
= ioctl(parportfd
, PPRCONTROL
, &val
);
302 ret
= ioctl(parportfd
, PPWCONTROL
, &val
);
306 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
310 } else if ((port
== ecpbase
+ PP_ECP_CFGA
) && ecpbase
) {
311 DPRINTF("ECP_CFGA port\n");
312 } else if ((port
== ecpbase
+ PP_ECP_CFGB
) && ecpbase
) {
313 DPRINTF("ECP_CFGB port\n");
314 } else if ((port
== ecpbase
+ PP_ECP_ECR
) && ecpbase
) {
315 DPRINTF("ECP_ECR port\n");
317 DPRINTF("access to unsupported address range!\n");
324 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
325 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
326 tr
->fAutoinc
, tr
->dwOptions
);
328 if (tr
->cmdTrans
== 10)
329 DPRINTF("read byte: %d\n", tr
->Data
.Byte
);
335 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
336 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
337 struct version_struct
*version
;
340 if (wdheader
->magic
!= MAGIC
) {
341 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
342 return (*ioctl_func
) (fd
, request
, wdioctl
);
345 switch(request
& ~(0xc0000000)) {
347 version
= (struct version_struct
*)(wdheader
->data
);
348 strcpy(version
->version
, "WinDriver no more");
349 version
->versionul
= 802;
350 DPRINTF("VERSION\n");
354 DPRINTF("LICENSE\n");
357 case CARD_REGISTER_OLD
:
359 DPRINTF("CARD_REGISTER\n");
361 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
364 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
366 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
367 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
368 cr
->Card
.Item
[0].I
.IO
.dwBar
);
370 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
372 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
373 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
374 cr
->Card
.Item
[1].I
.IO
.dwBar
);
376 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
380 struct pports
**port
= &pplist
;
383 DPRINTF("Looking up parallel port in linked list, entry: %d\n", (*port
)->num
);
384 if (max
< (*port
)->num
)
387 if ((*port
)->base
== (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
) {
391 port
= &((*port
)->next
);
394 if (!(*port
)) { /* not found */
395 (*port
) = malloc(sizeof(struct pports
));
401 (*port
)->base
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
402 (*port
)->num
= max
+1;
403 (*port
)->next
= NULL
;
405 DPRINTF("parallel port not in linked list, new entry: %d\n", (*port
)->num
);
408 snprintf(ppdev
, sizeof(ppdev
), "/dev/parport%d", (*port
)->num
);
409 DPRINTF("opening %s\n", ppdev
);
410 parportfd
= open(ppdev
, O_RDWR
|O_EXCL
);
413 fprintf(stderr
,"Can't open %s: %s\n", ppdev
, strerror(errno
));
416 if (parportfd
>= 0) {
419 if (ioctl(parportfd
, PPCLAIM
) == -1)
423 pmode
= IEEE1284_MODE_COMPAT
;
424 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
427 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
) {
428 DPRINTF("ECP mode requested\n");
429 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
430 /* TODO: Implement ECP mode */
432 pmode
= IEEE1284_MODE_ECP
;
434 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1) {
436 pmode
= IEEE1284_MODE_COMPAT
;
437 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
443 cr
->hCard
= parportfd
;
444 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
447 DPRINTF("hCard: %lu\n", cr
->hCard
);
452 DPRINTF("in USB_TRANSFER");
454 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
457 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
458 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
459 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
460 DPRINTF("setup packet: ");
461 hexdump(ut
->SetupPacket
, 8);
463 if (!ut
->fRead
&& ut
->dwBufferSize
)
465 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
470 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
472 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
473 if (ut
->dwPipeNum
== 0) { /* control pipe */
474 int requesttype
, request
, value
, index
, size
;
475 requesttype
= ut
->SetupPacket
[0];
476 request
= ut
->SetupPacket
[1];
477 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
478 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
479 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
480 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
481 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
484 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
487 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
492 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
494 ut
->dwBytesTransferred
= ret
;
500 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
501 if (ut
->fRead
&& ut
->dwBytesTransferred
)
504 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
512 DPRINTF("INT_ENABLE\n");
514 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
516 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
517 it
->hInterrupt
, it
->dwOptions
,
518 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
519 it
->dwLost
, it
->fStopped
);
524 pthread_mutex_trylock(&int_wait
);
530 DPRINTF("INT_DISABLE\n");
532 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
534 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
535 it
->hInterrupt
, it
->dwOptions
,
536 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
537 it
->dwLost
, it
->fStopped
);
539 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
544 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
545 pthread_mutex_unlock(&int_wait
);
547 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
548 it
->hInterrupt
, it
->dwOptions
,
549 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
550 it
->dwLost
, it
->fStopped
);
554 case USB_SET_INTERFACE
:
555 DPRINTF("USB_SET_INTERFACE\n");
557 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
559 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
560 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
561 usi
->dwAlternateSetting
, usi
->dwOptions
);
563 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
567 usb_devhandle
= usb_open(usbdevice
);
569 /* FIXME: Select right interface! */
570 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
573 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
574 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
576 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
578 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
581 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
582 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
583 ret
, usb_strerror());
587 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
588 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
589 usi
->dwAlternateSetting
, usi
->dwOptions
);
593 case USB_GET_DEVICE_DATA_OLD
:
594 case USB_GET_DEVICE_DATA
:
595 DPRINTF("USB_GET_DEVICE_DATA\n");
597 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
600 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
601 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
604 pSize
= ugdd
->dwBytes
;
605 if (!ugdd
->dwBytes
) {
607 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
610 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
615 case EVENT_REGISTER_OLD
:
617 DPRINTF("EVENT_REGISTER\n");
619 struct event
*e
= (struct event
*)(wdheader
->data
);
623 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",
624 e
->handle
, e
->dwAction
,
625 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
626 e
->hKernelPlugIn
, e
->dwOptions
,
627 e
->u
.Usb
.deviceId
.dwVendorId
,
628 e
->u
.Usb
.deviceId
.dwProductId
,
629 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
630 e
->dwNumMatchTables
);
632 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
);
643 for (bus
= busses
; bus
; bus
= bus
->next
) {
644 struct usb_device
*dev
;
646 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
647 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
649 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
650 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
651 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
652 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
654 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
655 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
658 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
660 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
661 interface
->altsetting
[i
].bInterfaceClass
,
662 interface
->altsetting
[i
].bInterfaceSubClass
,
663 interface
->altsetting
[i
].bInterfaceProtocol
);
665 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
666 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
667 /* TODO: check interfaceClass! */
668 DPRINTF("found device with libusb\n");
670 card_type
= e
->dwCardType
;
680 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
686 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",
687 e
->handle
, e
->dwAction
,
688 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
689 e
->hKernelPlugIn
, e
->dwOptions
,
690 e
->u
.Usb
.deviceId
.dwVendorId
,
691 e
->u
.Usb
.deviceId
.dwProductId
,
692 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
693 e
->dwNumMatchTables
);
695 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
696 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
697 e
->matchTables
[i
].VendorId
,
698 e
->matchTables
[i
].ProductId
,
699 e
->matchTables
[i
].bDeviceClass
,
700 e
->matchTables
[i
].bDeviceSubClass
,
701 e
->matchTables
[i
].bInterfaceClass
,
702 e
->matchTables
[i
].bInterfaceSubClass
,
703 e
->matchTables
[i
].bInterfaceProtocol
);
710 DPRINTF("TRANSFER\n");
712 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
714 ret
= pp_transfer(tr
, fd
, request
, wdioctl
);
718 case MULTI_TRANSFER_OLD
:
720 DPRINTF("MULTI_TRANSFER\n");
722 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
723 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
727 for (i
= 0; i
< num
; i
++) {
728 DPRINTF("Transfer %d:\n", i
+1);
730 wdheader
->size
= sizeof(WD_TRANSFER
);
732 wdheader
->data
= tr
+ i
;
734 ret
= pp_transfer(tr
+ i
, fd
, request
, wdioctl
);
745 case EVENT_UNREGISTER
:
746 DPRINTF("EVENT_UNREGISTER\n");
748 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
753 DPRINTF("INT_WAIT\n");
755 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
757 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
758 it
->hInterrupt
, it
->dwOptions
,
759 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
760 it
->dwLost
, it
->fStopped
);
763 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
766 if (it
->dwCounter
== 0) {
769 pthread_mutex_lock(&int_wait
);
770 pthread_mutex_unlock(&int_wait
);
773 pthread_mutex_lock(&int_wait
);
774 pthread_mutex_unlock(&int_wait
);
778 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
779 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
780 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
785 case CARD_UNREGISTER
:
786 DPRINTF("CARD_UNREGISTER\n");
788 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
790 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
791 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
792 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
793 cr
->Card
.Item
[0].I
.IO
.dwBar
);
795 DPRINTF("hCard: %lu\n", cr
->hCard
);
798 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
800 if (parportfd
== cr
->hCard
&& parportfd
>= 0) {
801 ioctl(parportfd
, PPRELEASE
);
810 DPRINTF("EVENT_PULL\n");
812 struct event
*e
= (struct event
*)(wdheader
->data
);
816 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",
817 e
->handle
, e
->dwAction
, e
->dwStatus
,
818 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
819 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
820 e
->u
.Usb
.deviceId
.dwProductId
,
821 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
822 e
->dwNumMatchTables
);
824 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
825 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
826 e
->matchTables
[i
].VendorId
,
827 e
->matchTables
[i
].ProductId
,
828 e
->matchTables
[i
].bDeviceClass
,
829 e
->matchTables
[i
].bDeviceSubClass
,
830 e
->matchTables
[i
].bInterfaceClass
,
831 e
->matchTables
[i
].bInterfaceSubClass
,
832 e
->matchTables
[i
].bInterfaceProtocol
);
836 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
839 struct usb_interface
*interface
= usbdevice
->config
->interface
;
841 e
->dwCardType
= card_type
;
844 e
->u
.Usb
.dwUniqueID
= 110;
845 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
846 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
847 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
848 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
849 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
850 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
851 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
856 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",
857 e
->handle
, e
->dwAction
, e
->dwStatus
,
858 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
859 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
860 e
->u
.Usb
.deviceId
.dwProductId
,
861 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
862 e
->dwNumMatchTables
);
864 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
865 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
866 e
->matchTables
[i
].VendorId
,
867 e
->matchTables
[i
].ProductId
,
868 e
->matchTables
[i
].bDeviceClass
,
869 e
->matchTables
[i
].bDeviceSubClass
,
870 e
->matchTables
[i
].bInterfaceClass
,
871 e
->matchTables
[i
].bInterfaceSubClass
,
872 e
->matchTables
[i
].bInterfaceProtocol
);
879 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
881 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
889 int ioctl(int fd
, unsigned long int request
, ...) {
895 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
897 va_start (args
, request
);
898 argp
= va_arg (args
, void *);
902 ret
= do_wdioctl(fd
, request
, argp
);
904 ret
= (*ioctl_func
) (fd
, request
, argp
);
909 int open (const char *pathname
, int flags
, ...) {
910 static int (*func
) (const char *, int, mode_t
) = NULL
;
916 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
918 if (flags
& O_CREAT
) {
919 va_start(args
, flags
);
920 mode
= va_arg(args
, mode_t
);
924 if (!strcmp (pathname
, "/dev/windrvr6")) {
925 DPRINTF("opening windrvr6\n");
927 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
929 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
936 busses
= usb_get_busses();
942 return (*func
) (pathname
, flags
, mode
);
946 static int (*func
) (int) = NULL
;
949 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
951 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
952 DPRINTF("close windrvrfd\n");
953 if (usbinterface
>= 0)
954 usb_release_interface(usb_devhandle
, usbinterface
);
957 usb_close(usb_devhandle
);
959 usb_devhandle
= NULL
;
967 FILE *fopen(const char *path
, const char *mode
) {
969 static FILE* (*func
) (const char*, const char*) = NULL
;
972 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
974 ret
= (*func
) (path
, mode
);
976 if (!strcmp (path
, "/proc/modules")) {
977 DPRINTF("opening /proc/modules\n");
987 char *fgets(char *s
, int size
, FILE *stream
) {
988 static char* (*func
) (char*, int, FILE*) = NULL
;
989 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
994 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
996 if (modulesfp
== stream
) {
997 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
998 strcpy(s
, modules
[modules_read
]);
1003 ret
= (*func
)(s
,size
,stream
);
1009 int fclose(FILE *fp
) {
1010 static int (*func
) (FILE*) = NULL
;
1013 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
1015 if (fp
== modulesfp
) {
1022 int access(const char *pathname
, int mode
) {
1023 static int (*func
) (const char*, int);
1026 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
1028 if (!strcmp(pathname
, "/dev/windrvr6")) {
1031 return (*func
)(pathname
, mode
);