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 "usb-driver.h"
49 static int (*ioctl_func
) (int, int, void *) = NULL
;
50 static int windrvrfd
= -1;
51 static unsigned long ppbase
= 0;
52 static unsigned long ecpbase
= 0;
53 FILE *modulesfp
= NULL
;
54 FILE *baseaddrfp
= NULL
;
56 static int modules_read
= 0;
57 static struct usb_bus
*busses
= NULL
;
58 static struct usb_device
*usbdevice
;
59 static usb_dev_handle
*usb_devhandle
= NULL
;
60 static int usbinterface
= -1;
61 static unsigned long card_type
;
62 static int ints_enabled
= 0;
63 static pthread_mutex_t int_wait
= PTHREAD_MUTEX_INITIALIZER
;
67 void hexdump(unsigned char *buf
, int len
) {
70 for(i
=0; i
<len
; i
++) {
71 fprintf(stderr
,"%02x ", buf
[i
]);
78 int usb_deviceinfo(unsigned char *buf
) {
81 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
82 WDU_INTERFACE
**pActiveInterface
;
85 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
87 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
88 udi
->Descriptor
.bDescriptorType
= usbdevice
->descriptor
.bDescriptorType
;
89 udi
->Descriptor
.bcdUSB
= usbdevice
->descriptor
.bcdUSB
;
90 udi
->Descriptor
.bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
91 udi
->Descriptor
.bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
92 udi
->Descriptor
.bDeviceProtocol
= usbdevice
->descriptor
.bDeviceProtocol
;
93 udi
->Descriptor
.bMaxPacketSize0
= usbdevice
->descriptor
.bMaxPacketSize0
;
94 udi
->Descriptor
.idVendor
= usbdevice
->descriptor
.idVendor
;
95 udi
->Descriptor
.idProduct
= usbdevice
->descriptor
.idProduct
;
96 udi
->Descriptor
.bcdDevice
= usbdevice
->descriptor
.bcdDevice
;
97 udi
->Descriptor
.iManufacturer
= usbdevice
->descriptor
.iManufacturer
;
98 udi
->Descriptor
.iProduct
= usbdevice
->descriptor
.iProduct
;
99 udi
->Descriptor
.iSerialNumber
= usbdevice
->descriptor
.iSerialNumber
;
100 udi
->Descriptor
.bNumConfigurations
= usbdevice
->descriptor
.bNumConfigurations
;
102 /* TODO: Fix Pipe0! */
103 udi
->Pipe0
.dwNumber
= 0x00;
104 udi
->Pipe0
.dwMaximumPacketSize
= usbdevice
->descriptor
.bMaxPacketSize0
;
106 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
107 udi
->Pipe0
.dwInterval
= 0;
109 pConfigs
= &(udi
->pConfigs
);
110 pActiveConfig
= &(udi
->pActiveConfig
);
111 pActiveInterface
= &(udi
->pActiveInterface
[0]);
114 len
= sizeof(struct usb_device_info
);
116 for (i
=0; i
<usbdevice
->descriptor
.bNumConfigurations
; i
++)
118 struct usb_config_descriptor
*conf_desc
= &usbdevice
->config
[i
];
119 WDU_INTERFACE
**pInterfaces
;
120 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
121 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
124 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
127 *pActiveConfig
= cfg
;
129 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
130 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
131 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
132 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
133 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
134 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
135 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
136 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
138 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
140 pInterfaces
= &(cfg
->pInterfaces
);
142 len
+= sizeof(WDU_CONFIGURATION
);
145 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
146 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
147 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
149 pActiveInterface
[j
] = iface
;
151 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
152 iface
->dwNumAltSettings
= usbdevice
->config
[i
].interface
[j
].num_altsetting
;
153 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
155 len
+= sizeof(WDU_INTERFACE
);
158 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
161 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
163 struct usb_interface
*interface
= &usbdevice
->config
[i
].interface
[j
];
166 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
168 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
171 for(k
=0; k
<interface
->num_altsetting
; k
++)
173 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
174 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
175 WDU_PIPE_INFO
**pPipes
;
178 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
180 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
181 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
182 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
183 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
184 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
185 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
186 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
187 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
188 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
189 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
190 pPipes
= &(altset
->pPipes
);
193 len
+=sizeof(WDU_ALTERNATE_SETTING
);
196 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
197 for (l
= 0; l
< bNumEndpoints
; l
++) {
198 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
200 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
201 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
202 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
203 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
204 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
205 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
207 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
210 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
211 for (l
= 0; l
< bNumEndpoints
; l
++) {
212 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
214 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
215 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
216 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
217 if (pi
->type
== PIPE_TYPE_CONTROL
)
218 pi
->direction
= WDU_DIR_IN_OUT
;
221 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
224 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
226 len
+= sizeof(WDU_PIPE_INFO
);
229 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
238 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
239 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
240 struct version_struct
*version
;
243 if (wdheader
->magic
!= MAGIC
) {
244 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
245 return (*ioctl_func
) (fd
, request
, wdioctl
);
248 switch(request
& ~(0xc0000000)) {
250 version
= (struct version_struct
*)(wdheader
->data
);
251 strcpy(version
->version
, "libusb-driver.so $Revision: 1.67 $");
252 version
->versionul
= 802;
253 DPRINTF("VERSION\n");
257 DPRINTF("LICENSE\n");
260 case CARD_REGISTER_OLD
:
262 DPRINTF("CARD_REGISTER\n");
264 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
266 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
268 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
269 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
270 cr
->Card
.Item
[0].I
.IO
.dwBar
);
272 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
274 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
275 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
276 cr
->Card
.Item
[1].I
.IO
.dwBar
);
278 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
282 if (!config_is_real_pport((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10))
283 ret
= jtagkey_open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
286 ret
= parport_open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
288 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
290 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
291 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
299 DPRINTF("hCard: %lu\n", cr
->hCard
);
304 DPRINTF("in USB_TRANSFER");
306 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
309 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
310 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
311 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
312 DPRINTF("setup packet: ");
313 hexdump(ut
->SetupPacket
, 8);
315 if (!ut
->fRead
&& ut
->dwBufferSize
)
317 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
322 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
324 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
325 if (ut
->dwPipeNum
== 0) { /* control pipe */
326 int requesttype
, request
, value
, index
, size
;
327 requesttype
= ut
->SetupPacket
[0];
328 request
= ut
->SetupPacket
[1];
329 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
330 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
331 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
332 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
333 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
336 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
339 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
344 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
346 ut
->dwBytesTransferred
= ret
;
352 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
353 if (ut
->fRead
&& ut
->dwBytesTransferred
)
356 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
364 DPRINTF("INT_ENABLE\n");
366 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
368 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
369 it
->hInterrupt
, it
->dwOptions
,
370 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
371 it
->dwLost
, it
->fStopped
);
376 pthread_mutex_trylock(&int_wait
);
382 DPRINTF("INT_DISABLE\n");
384 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
386 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
387 it
->hInterrupt
, it
->dwOptions
,
388 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
389 it
->dwLost
, it
->fStopped
);
391 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
396 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
397 pthread_mutex_unlock(&int_wait
);
399 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
400 it
->hInterrupt
, it
->dwOptions
,
401 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
402 it
->dwLost
, it
->fStopped
);
406 case USB_SET_INTERFACE
:
407 DPRINTF("USB_SET_INTERFACE\n");
409 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
411 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
412 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
413 usi
->dwAlternateSetting
, usi
->dwOptions
);
415 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
419 usb_devhandle
= usb_open(usbdevice
);
421 /* FIXME: Select right interface! */
422 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
425 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
426 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
428 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
430 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
433 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
434 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
435 ret
, usb_strerror());
439 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
440 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
441 usi
->dwAlternateSetting
, usi
->dwOptions
);
445 case USB_GET_DEVICE_DATA_OLD
:
446 case USB_GET_DEVICE_DATA
:
447 DPRINTF("USB_GET_DEVICE_DATA\n");
449 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
452 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
453 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
456 pSize
= ugdd
->dwBytes
;
457 if (!ugdd
->dwBytes
) {
459 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
462 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
467 case EVENT_REGISTER_OLD
:
469 DPRINTF("EVENT_REGISTER\n");
471 struct event
*e
= (struct event
*)(wdheader
->data
);
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 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
486 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
487 e
->matchTables
[i
].VendorId
,
488 e
->matchTables
[i
].ProductId
,
489 e
->matchTables
[i
].bDeviceClass
,
490 e
->matchTables
[i
].bDeviceSubClass
,
491 e
->matchTables
[i
].bInterfaceClass
,
492 e
->matchTables
[i
].bInterfaceSubClass
,
493 e
->matchTables
[i
].bInterfaceProtocol
);
495 for (bus
= busses
; bus
; bus
= bus
->next
) {
496 struct usb_device
*dev
;
498 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
499 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
501 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
502 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
503 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
504 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
506 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
507 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
510 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
512 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
513 interface
->altsetting
[i
].bInterfaceClass
,
514 interface
->altsetting
[i
].bInterfaceSubClass
,
515 interface
->altsetting
[i
].bInterfaceProtocol
);
517 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
518 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
519 /* TODO: check interfaceClass! */
520 DPRINTF("found device with libusb\n");
522 card_type
= e
->dwCardType
;
532 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
538 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",
539 e
->handle
, e
->dwAction
,
540 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
541 e
->hKernelPlugIn
, e
->dwOptions
,
542 e
->u
.Usb
.deviceId
.dwVendorId
,
543 e
->u
.Usb
.deviceId
.dwProductId
,
544 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
545 e
->dwNumMatchTables
);
547 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
548 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
549 e
->matchTables
[i
].VendorId
,
550 e
->matchTables
[i
].ProductId
,
551 e
->matchTables
[i
].bDeviceClass
,
552 e
->matchTables
[i
].bDeviceSubClass
,
553 e
->matchTables
[i
].bInterfaceClass
,
554 e
->matchTables
[i
].bInterfaceSubClass
,
555 e
->matchTables
[i
].bInterfaceProtocol
);
562 DPRINTF("TRANSFER\n");
564 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
567 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
571 if (!config_is_real_pport(ppbase
/ 0x10)) {
572 ret
= jtagkey_transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
576 ret
= pp_transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
581 case MULTI_TRANSFER_OLD
:
583 DPRINTF("MULTI_TRANSFER\n");
585 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
586 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
588 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
592 if (!config_is_real_pport(ppbase
/ 0x10)) {
593 ret
= jtagkey_transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
596 ret
= pp_transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
601 case EVENT_UNREGISTER
:
602 DPRINTF("EVENT_UNREGISTER\n");
604 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
609 DPRINTF("INT_WAIT\n");
611 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
613 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
614 it
->hInterrupt
, it
->dwOptions
,
615 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
616 it
->dwLost
, it
->fStopped
);
619 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
622 if (it
->dwCounter
== 0) {
625 pthread_mutex_lock(&int_wait
);
626 pthread_mutex_unlock(&int_wait
);
629 pthread_mutex_lock(&int_wait
);
630 pthread_mutex_unlock(&int_wait
);
634 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
635 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
636 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
641 case CARD_UNREGISTER
:
642 DPRINTF("CARD_UNREGISTER\n");
644 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
646 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
647 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
648 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
649 cr
->Card
.Item
[0].I
.IO
.dwBar
);
651 DPRINTF("hCard: %lu\n", cr
->hCard
);
654 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
657 if (cr
->hCard
== 0xff)
658 jtagkey_close(cr
->hCard
);
661 parport_close(cr
->hCard
);
667 DPRINTF("EVENT_PULL\n");
669 struct event
*e
= (struct event
*)(wdheader
->data
);
673 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",
674 e
->handle
, e
->dwAction
, e
->dwStatus
,
675 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
676 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
677 e
->u
.Usb
.deviceId
.dwProductId
,
678 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
679 e
->dwNumMatchTables
);
681 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
682 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
683 e
->matchTables
[i
].VendorId
,
684 e
->matchTables
[i
].ProductId
,
685 e
->matchTables
[i
].bDeviceClass
,
686 e
->matchTables
[i
].bDeviceSubClass
,
687 e
->matchTables
[i
].bInterfaceClass
,
688 e
->matchTables
[i
].bInterfaceSubClass
,
689 e
->matchTables
[i
].bInterfaceProtocol
);
693 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
696 struct usb_interface
*interface
= usbdevice
->config
->interface
;
698 e
->dwCardType
= card_type
;
701 e
->u
.Usb
.dwUniqueID
= 110;
702 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
703 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
704 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
705 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
706 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
707 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
708 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
713 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",
714 e
->handle
, e
->dwAction
, e
->dwStatus
,
715 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
716 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
717 e
->u
.Usb
.deviceId
.dwProductId
,
718 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
719 e
->dwNumMatchTables
);
721 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
722 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
723 e
->matchTables
[i
].VendorId
,
724 e
->matchTables
[i
].ProductId
,
725 e
->matchTables
[i
].bDeviceClass
,
726 e
->matchTables
[i
].bDeviceSubClass
,
727 e
->matchTables
[i
].bInterfaceClass
,
728 e
->matchTables
[i
].bInterfaceSubClass
,
729 e
->matchTables
[i
].bInterfaceProtocol
);
736 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
738 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
746 int ioctl(int fd
, unsigned long int request
, ...) {
752 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
754 va_start (args
, request
);
755 argp
= va_arg (args
, void *);
759 ret
= do_wdioctl(fd
, request
, argp
);
761 ret
= (*ioctl_func
) (fd
, request
, argp
);
766 int open (const char *pathname
, int flags
, ...) {
767 static int (*func
) (const char *, int, mode_t
) = NULL
;
773 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
775 if (flags
& O_CREAT
) {
776 va_start(args
, flags
);
777 mode
= va_arg(args
, mode_t
);
781 if (!strcmp (pathname
, "/dev/windrvr6")) {
782 DPRINTF("opening windrvr6\n");
784 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
786 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
793 busses
= usb_get_busses();
799 return (*func
) (pathname
, flags
, mode
);
803 static int (*func
) (int) = NULL
;
806 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
808 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
809 DPRINTF("close windrvrfd\n");
810 if (usbinterface
>= 0)
811 usb_release_interface(usb_devhandle
, usbinterface
);
814 usb_close(usb_devhandle
);
816 usb_devhandle
= NULL
;
824 FILE *fopen(const char *path
, const char *mode
) {
826 static FILE* (*func
) (const char*, const char*) = NULL
;
831 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
833 for (i
= 0; i
< 4; i
++) {
834 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
835 if (!strcmp(path
, buf
)) {
836 DPRINTF("open base-addr of parport%d\n", i
);
837 if (config_is_real_pport(i
)) {
838 ret
= (*func
) (path
, mode
);
840 ret
= (*func
) ("/dev/null", mode
);
852 ret
= (*func
) (path
, mode
);
854 if (!strcmp(path
, "/proc/modules")) {
855 DPRINTF("opening /proc/modules\n");
865 char *fgets(char *s
, int size
, FILE *stream
) {
866 static char* (*func
) (char*, int, FILE*) = NULL
;
867 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
873 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
875 if (modulesfp
== stream
) {
876 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
877 strcpy(s
, modules
[modules_read
]);
881 } else if (baseaddrfp
== stream
) {
882 snprintf(s
, sizeof(buf
), "%d\t%d\n",
883 (baseaddrnum
) * 0x10,
884 ((baseaddrnum
) * 0x10) + 0x400);
887 ret
= (*func
)(s
,size
,stream
);
893 int fclose(FILE *fp
) {
894 static int (*func
) (FILE*) = NULL
;
897 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
899 if (fp
== modulesfp
) {
903 if (fp
== baseaddrfp
) {
910 int access(const char *pathname
, int mode
) {
911 static int (*func
) (const char*, int);
914 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
916 if (!strcmp(pathname
, "/dev/windrvr6")) {
919 return (*func
)(pathname
, mode
);