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"
49 static int (*ioctl_func
) (int, int, void *) = NULL
;
50 static int windrvrfd
= -1;
51 static int parportfd
= -1;
52 static unsigned long ppbase
= 0;
53 static unsigned long ecpbase
= 0;
54 FILE *modulesfp
= NULL
;
55 FILE *baseaddrfp
= NULL
;
57 static int modules_read
= 0;
58 static struct usb_bus
*busses
= NULL
;
59 static struct usb_device
*usbdevice
;
60 static usb_dev_handle
*usb_devhandle
= NULL
;
61 static int usbinterface
= -1;
62 static unsigned long card_type
;
63 static int ints_enabled
= 0;
64 static pthread_mutex_t int_wait
= PTHREAD_MUTEX_INITIALIZER
;
69 void hexdump(unsigned char *buf
, int len
) {
72 for(i
=0; i
<len
; i
++) {
73 fprintf(stderr
,"%02x ", buf
[i
]);
81 int usb_deviceinfo(unsigned char *buf
) {
84 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
85 WDU_INTERFACE
**pActiveInterface
;
88 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
90 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
91 udi
->Descriptor
.bDescriptorType
= usbdevice
->descriptor
.bDescriptorType
;
92 udi
->Descriptor
.bcdUSB
= usbdevice
->descriptor
.bcdUSB
;
93 udi
->Descriptor
.bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
94 udi
->Descriptor
.bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
95 udi
->Descriptor
.bDeviceProtocol
= usbdevice
->descriptor
.bDeviceProtocol
;
96 udi
->Descriptor
.bMaxPacketSize0
= usbdevice
->descriptor
.bMaxPacketSize0
;
97 udi
->Descriptor
.idVendor
= usbdevice
->descriptor
.idVendor
;
98 udi
->Descriptor
.idProduct
= usbdevice
->descriptor
.idProduct
;
99 udi
->Descriptor
.bcdDevice
= usbdevice
->descriptor
.bcdDevice
;
100 udi
->Descriptor
.iManufacturer
= usbdevice
->descriptor
.iManufacturer
;
101 udi
->Descriptor
.iProduct
= usbdevice
->descriptor
.iProduct
;
102 udi
->Descriptor
.iSerialNumber
= usbdevice
->descriptor
.iSerialNumber
;
103 udi
->Descriptor
.bNumConfigurations
= usbdevice
->descriptor
.bNumConfigurations
;
105 /* TODO: Fix Pipe0! */
106 udi
->Pipe0
.dwNumber
= 0x00;
107 udi
->Pipe0
.dwMaximumPacketSize
= usbdevice
->descriptor
.bMaxPacketSize0
;
109 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
110 udi
->Pipe0
.dwInterval
= 0;
112 pConfigs
= &(udi
->pConfigs
);
113 pActiveConfig
= &(udi
->pActiveConfig
);
114 pActiveInterface
= &(udi
->pActiveInterface
[0]);
117 len
= sizeof(struct usb_device_info
);
119 for (i
=0; i
<usbdevice
->descriptor
.bNumConfigurations
; i
++)
121 struct usb_config_descriptor
*conf_desc
= &usbdevice
->config
[i
];
122 WDU_INTERFACE
**pInterfaces
;
123 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
124 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
127 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
130 *pActiveConfig
= cfg
;
132 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
133 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
134 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
135 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
136 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
137 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
138 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
139 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
141 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
143 pInterfaces
= &(cfg
->pInterfaces
);
145 len
+= sizeof(WDU_CONFIGURATION
);
148 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
149 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
150 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
152 pActiveInterface
[j
] = iface
;
154 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
155 iface
->dwNumAltSettings
= usbdevice
->config
[i
].interface
[j
].num_altsetting
;
156 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
158 len
+= sizeof(WDU_INTERFACE
);
161 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
164 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
166 struct usb_interface
*interface
= &usbdevice
->config
[i
].interface
[j
];
169 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
171 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
174 for(k
=0; k
<interface
->num_altsetting
; k
++)
176 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
177 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
178 WDU_PIPE_INFO
**pPipes
;
181 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
183 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
184 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
185 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
186 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
187 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
188 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
189 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
190 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
191 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
192 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
193 pPipes
= &(altset
->pPipes
);
196 len
+=sizeof(WDU_ALTERNATE_SETTING
);
199 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
200 for (l
= 0; l
< bNumEndpoints
; l
++) {
201 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
203 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
204 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
205 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
206 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
207 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
208 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
210 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
213 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
214 for (l
= 0; l
< bNumEndpoints
; l
++) {
215 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
217 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
218 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
219 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
220 if (pi
->type
== PIPE_TYPE_CONTROL
)
221 pi
->direction
= WDU_DIR_IN_OUT
;
224 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
227 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
229 len
+= sizeof(WDU_PIPE_INFO
);
232 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
241 int pp_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
, unsigned char *wdioctl
) {
243 unsigned long port
= (unsigned long)tr
->dwPort
;
245 static unsigned char last_pp_write
= 0;
248 /* FIXME: Config file and mor intelligent mapping! */
250 return jtagkey_transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
253 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
254 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
255 tr
->fAutoinc
, tr
->dwOptions
);
260 if (tr
->cmdTrans
== 13)
261 DPRINTF("write byte: %d\n", val
);
265 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
270 if (port
== ppbase
+ PP_DATA
) {
271 DPRINTF("data port\n");
272 switch(tr
->cmdTrans
) {
274 ret
= 0; /* We don't support reading of the data port */
278 ret
= ioctl(parportfd
, PPWDATA
, &val
);
283 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
287 } else if (port
== ppbase
+ PP_STATUS
) {
288 DPRINTF("status port (last write: %d)\n", last_pp_write
);
289 switch(tr
->cmdTrans
) {
291 ret
= ioctl(parportfd
, PPRSTATUS
, &val
);
292 #ifdef FORCE_PC3_IDENT
294 if (last_pp_write
& 0x40)
302 ret
= 0; /* Status Port is readonly */
306 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
310 } else if (port
== ppbase
+ PP_CONTROL
) {
311 DPRINTF("control port\n");
312 switch(tr
->cmdTrans
) {
314 ret
= ioctl(parportfd
, PPRCONTROL
, &val
);
318 ret
= ioctl(parportfd
, PPWCONTROL
, &val
);
322 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
326 } else if ((port
== ecpbase
+ PP_ECP_CFGA
) && ecpbase
) {
327 DPRINTF("ECP_CFGA port\n");
328 } else if ((port
== ecpbase
+ PP_ECP_CFGB
) && ecpbase
) {
329 DPRINTF("ECP_CFGB port\n");
330 } else if ((port
== ecpbase
+ PP_ECP_ECR
) && ecpbase
) {
331 DPRINTF("ECP_ECR port\n");
333 DPRINTF("access to unsupported address range!\n");
340 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
341 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
342 tr
->fAutoinc
, tr
->dwOptions
);
344 if (tr
->cmdTrans
== 10)
345 DPRINTF("read byte: %d\n", tr
->Data
.Byte
);
351 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
352 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
353 struct version_struct
*version
;
356 if (wdheader
->magic
!= MAGIC
) {
357 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
358 return (*ioctl_func
) (fd
, request
, wdioctl
);
361 switch(request
& ~(0xc0000000)) {
363 version
= (struct version_struct
*)(wdheader
->data
);
364 strcpy(version
->version
, "libusb-driver.so $Revision: 1.62 $");
365 version
->versionul
= 802;
366 DPRINTF("VERSION\n");
370 DPRINTF("LICENSE\n");
373 case CARD_REGISTER_OLD
:
375 DPRINTF("CARD_REGISTER\n");
377 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
380 DPRINTF("Items: %lu, 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("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
388 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
389 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
390 cr
->Card
.Item
[1].I
.IO
.dwBar
);
392 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
395 /* FIXME: Ugly hack which maps amontec JtagKey to 4. parallel port */
397 if ((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
== 0x30) {
398 ret
=jtagkey_init(0x0403, 0xcff8); /* I need a config file... */
400 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
408 snprintf(ppdev
, sizeof(ppdev
), "/dev/parport%lu",
409 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
410 DPRINTF("opening %s\n", ppdev
);
411 parportfd
= open(ppdev
, O_RDWR
|O_EXCL
);
414 fprintf(stderr
,"Can't open %s: %s\n", ppdev
, strerror(errno
));
417 if (parportfd
>= 0) {
420 if (ioctl(parportfd
, PPCLAIM
) == -1)
424 pmode
= IEEE1284_MODE_COMPAT
;
425 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
428 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
) {
429 DPRINTF("ECP mode requested\n");
430 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
431 /* TODO: Implement ECP mode */
433 pmode
= IEEE1284_MODE_ECP
;
435 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1) {
437 pmode
= IEEE1284_MODE_COMPAT
;
438 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
444 cr
->hCard
= parportfd
;
447 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
451 DPRINTF("hCard: %lu\n", cr
->hCard
);
456 DPRINTF("in USB_TRANSFER");
458 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
461 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
462 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
463 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
464 DPRINTF("setup packet: ");
465 hexdump(ut
->SetupPacket
, 8);
467 if (!ut
->fRead
&& ut
->dwBufferSize
)
469 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
474 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
476 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
477 if (ut
->dwPipeNum
== 0) { /* control pipe */
478 int requesttype
, request
, value
, index
, size
;
479 requesttype
= ut
->SetupPacket
[0];
480 request
= ut
->SetupPacket
[1];
481 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
482 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
483 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
484 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
485 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
488 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
491 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
496 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
498 ut
->dwBytesTransferred
= ret
;
504 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
505 if (ut
->fRead
&& ut
->dwBytesTransferred
)
508 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
516 DPRINTF("INT_ENABLE\n");
518 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
520 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
521 it
->hInterrupt
, it
->dwOptions
,
522 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
523 it
->dwLost
, it
->fStopped
);
528 pthread_mutex_trylock(&int_wait
);
534 DPRINTF("INT_DISABLE\n");
536 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
538 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
539 it
->hInterrupt
, it
->dwOptions
,
540 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
541 it
->dwLost
, it
->fStopped
);
543 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
548 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
549 pthread_mutex_unlock(&int_wait
);
551 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
552 it
->hInterrupt
, it
->dwOptions
,
553 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
554 it
->dwLost
, it
->fStopped
);
558 case USB_SET_INTERFACE
:
559 DPRINTF("USB_SET_INTERFACE\n");
561 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
563 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
564 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
565 usi
->dwAlternateSetting
, usi
->dwOptions
);
567 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
571 usb_devhandle
= usb_open(usbdevice
);
573 /* FIXME: Select right interface! */
574 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
577 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
578 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
580 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
582 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
585 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
586 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
587 ret
, usb_strerror());
591 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
592 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
593 usi
->dwAlternateSetting
, usi
->dwOptions
);
597 case USB_GET_DEVICE_DATA_OLD
:
598 case USB_GET_DEVICE_DATA
:
599 DPRINTF("USB_GET_DEVICE_DATA\n");
601 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
604 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
605 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
608 pSize
= ugdd
->dwBytes
;
609 if (!ugdd
->dwBytes
) {
611 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
614 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
619 case EVENT_REGISTER_OLD
:
621 DPRINTF("EVENT_REGISTER\n");
623 struct event
*e
= (struct event
*)(wdheader
->data
);
627 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",
628 e
->handle
, e
->dwAction
,
629 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
630 e
->hKernelPlugIn
, e
->dwOptions
,
631 e
->u
.Usb
.deviceId
.dwVendorId
,
632 e
->u
.Usb
.deviceId
.dwProductId
,
633 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
634 e
->dwNumMatchTables
);
636 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
638 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
639 e
->matchTables
[i
].VendorId
,
640 e
->matchTables
[i
].ProductId
,
641 e
->matchTables
[i
].bDeviceClass
,
642 e
->matchTables
[i
].bDeviceSubClass
,
643 e
->matchTables
[i
].bInterfaceClass
,
644 e
->matchTables
[i
].bInterfaceSubClass
,
645 e
->matchTables
[i
].bInterfaceProtocol
);
647 for (bus
= busses
; bus
; bus
= bus
->next
) {
648 struct usb_device
*dev
;
650 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
651 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
653 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
654 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
655 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
656 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
658 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
659 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
662 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
664 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
665 interface
->altsetting
[i
].bInterfaceClass
,
666 interface
->altsetting
[i
].bInterfaceSubClass
,
667 interface
->altsetting
[i
].bInterfaceProtocol
);
669 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
670 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
671 /* TODO: check interfaceClass! */
672 DPRINTF("found device with libusb\n");
674 card_type
= e
->dwCardType
;
684 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
690 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",
691 e
->handle
, e
->dwAction
,
692 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
693 e
->hKernelPlugIn
, e
->dwOptions
,
694 e
->u
.Usb
.deviceId
.dwVendorId
,
695 e
->u
.Usb
.deviceId
.dwProductId
,
696 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
697 e
->dwNumMatchTables
);
699 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
700 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
701 e
->matchTables
[i
].VendorId
,
702 e
->matchTables
[i
].ProductId
,
703 e
->matchTables
[i
].bDeviceClass
,
704 e
->matchTables
[i
].bDeviceSubClass
,
705 e
->matchTables
[i
].bInterfaceClass
,
706 e
->matchTables
[i
].bInterfaceSubClass
,
707 e
->matchTables
[i
].bInterfaceProtocol
);
714 DPRINTF("TRANSFER\n");
716 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
718 ret
= pp_transfer(tr
, fd
, request
, wdioctl
);
722 case MULTI_TRANSFER_OLD
:
724 DPRINTF("MULTI_TRANSFER\n");
726 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
727 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
730 for (i
= 0; i
< num
; i
++) {
731 DPRINTF("Transfer %d:\n", i
+1);
733 wdheader
->size
= sizeof(WD_TRANSFER
);
735 wdheader
->data
= tr
+ i
;
737 ret
= pp_transfer(tr
+ i
, fd
, request
, wdioctl
);
748 case EVENT_UNREGISTER
:
749 DPRINTF("EVENT_UNREGISTER\n");
751 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
756 DPRINTF("INT_WAIT\n");
758 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
760 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
761 it
->hInterrupt
, it
->dwOptions
,
762 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
763 it
->dwLost
, it
->fStopped
);
766 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
769 if (it
->dwCounter
== 0) {
772 pthread_mutex_lock(&int_wait
);
773 pthread_mutex_unlock(&int_wait
);
776 pthread_mutex_lock(&int_wait
);
777 pthread_mutex_unlock(&int_wait
);
781 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
782 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
783 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
788 case CARD_UNREGISTER
:
789 DPRINTF("CARD_UNREGISTER\n");
791 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
793 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
794 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
795 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
796 cr
->Card
.Item
[0].I
.IO
.dwBar
);
798 DPRINTF("hCard: %lu\n", cr
->hCard
);
801 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
803 if (parportfd
== cr
->hCard
&& parportfd
>= 0) {
804 ioctl(parportfd
, PPRELEASE
);
813 DPRINTF("EVENT_PULL\n");
815 struct event
*e
= (struct event
*)(wdheader
->data
);
819 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",
820 e
->handle
, e
->dwAction
, e
->dwStatus
,
821 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
822 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
823 e
->u
.Usb
.deviceId
.dwProductId
,
824 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
825 e
->dwNumMatchTables
);
827 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
828 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
829 e
->matchTables
[i
].VendorId
,
830 e
->matchTables
[i
].ProductId
,
831 e
->matchTables
[i
].bDeviceClass
,
832 e
->matchTables
[i
].bDeviceSubClass
,
833 e
->matchTables
[i
].bInterfaceClass
,
834 e
->matchTables
[i
].bInterfaceSubClass
,
835 e
->matchTables
[i
].bInterfaceProtocol
);
839 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
842 struct usb_interface
*interface
= usbdevice
->config
->interface
;
844 e
->dwCardType
= card_type
;
847 e
->u
.Usb
.dwUniqueID
= 110;
848 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
849 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
850 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
851 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
852 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
853 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
854 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
859 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",
860 e
->handle
, e
->dwAction
, e
->dwStatus
,
861 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
862 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
863 e
->u
.Usb
.deviceId
.dwProductId
,
864 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
865 e
->dwNumMatchTables
);
867 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
868 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
869 e
->matchTables
[i
].VendorId
,
870 e
->matchTables
[i
].ProductId
,
871 e
->matchTables
[i
].bDeviceClass
,
872 e
->matchTables
[i
].bDeviceSubClass
,
873 e
->matchTables
[i
].bInterfaceClass
,
874 e
->matchTables
[i
].bInterfaceSubClass
,
875 e
->matchTables
[i
].bInterfaceProtocol
);
882 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
884 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
892 int ioctl(int fd
, unsigned long int request
, ...) {
898 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
900 va_start (args
, request
);
901 argp
= va_arg (args
, void *);
905 ret
= do_wdioctl(fd
, request
, argp
);
907 ret
= (*ioctl_func
) (fd
, request
, argp
);
912 int open (const char *pathname
, int flags
, ...) {
913 static int (*func
) (const char *, int, mode_t
) = NULL
;
919 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
921 if (flags
& O_CREAT
) {
922 va_start(args
, flags
);
923 mode
= va_arg(args
, mode_t
);
927 if (!strcmp (pathname
, "/dev/windrvr6")) {
928 DPRINTF("opening windrvr6\n");
930 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
932 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
939 busses
= usb_get_busses();
945 return (*func
) (pathname
, flags
, mode
);
949 static int (*func
) (int) = NULL
;
952 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
954 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
955 DPRINTF("close windrvrfd\n");
956 if (usbinterface
>= 0)
957 usb_release_interface(usb_devhandle
, usbinterface
);
960 usb_close(usb_devhandle
);
962 usb_devhandle
= NULL
;
970 FILE *fopen(const char *path
, const char *mode
) {
972 static FILE* (*func
) (const char*, const char*) = NULL
;
977 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
980 /* FIXME: Hack for parport mapping */
981 if (!strcmp(path
, "/proc/sys/dev/parport/parport3/base-addr")) {
982 ret
= (*func
) ("/dev/null", mode
);
985 ret
= (*func
) (path
, mode
);
987 if (!strcmp(path
, "/proc/modules")) {
988 DPRINTF("opening /proc/modules\n");
996 for (i
= 0; i
< 4; i
++) {
997 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
998 if (!strcmp(path
, buf
)) {
999 DPRINTF("open base-addr of parport%d\n", i
);
1009 char *fgets(char *s
, int size
, FILE *stream
) {
1010 static char* (*func
) (char*, int, FILE*) = NULL
;
1011 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
1017 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
1019 if (modulesfp
== stream
) {
1020 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
1021 strcpy(s
, modules
[modules_read
]);
1025 } else if (baseaddrfp
== stream
) {
1026 snprintf(s
, sizeof(buf
), "%d\t%d\n",
1027 (baseaddrnum
) * 0x10,
1028 ((baseaddrnum
) * 0x10) + 0x400);
1031 ret
= (*func
)(s
,size
,stream
);
1037 int fclose(FILE *fp
) {
1038 static int (*func
) (FILE*) = NULL
;
1041 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
1043 if (fp
== modulesfp
) {
1047 if (fp
== baseaddrfp
) {
1054 int access(const char *pathname
, int mode
) {
1055 static int (*func
) (const char*, int);
1058 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
1060 if (!strcmp(pathname
, "/dev/windrvr6")) {
1063 return (*func
)(pathname
, mode
);