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
) {
243 unsigned long port
= (unsigned long)tr
->dwPort
;
245 static unsigned char last_pp_write
= 0;
247 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
248 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
249 tr
->fAutoinc
, tr
->dwOptions
);
254 if (tr
->cmdTrans
== 13)
255 DPRINTF("write byte: %d\n", val
);
261 if (port
== ppbase
+ PP_DATA
) {
262 DPRINTF("data port\n");
263 switch(tr
->cmdTrans
) {
265 ret
= 0; /* We don't support reading of the data port */
269 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 (last write: %d)\n", last_pp_write
);
280 switch(tr
->cmdTrans
) {
282 ret
= ioctl(parportfd
, PPRSTATUS
, &val
);
283 #ifdef FORCE_PC3_IDENT
285 if (last_pp_write
& 0x40)
293 ret
= 0; /* Status Port is readonly */
297 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
301 } else if (port
== ppbase
+ PP_CONTROL
) {
302 DPRINTF("control port\n");
303 switch(tr
->cmdTrans
) {
305 ret
= ioctl(parportfd
, PPRCONTROL
, &val
);
309 ret
= ioctl(parportfd
, PPWCONTROL
, &val
);
313 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
317 } else if ((port
== ecpbase
+ PP_ECP_CFGA
) && ecpbase
) {
318 DPRINTF("ECP_CFGA port\n");
319 } else if ((port
== ecpbase
+ PP_ECP_CFGB
) && ecpbase
) {
320 DPRINTF("ECP_CFGB port\n");
321 } else if ((port
== ecpbase
+ PP_ECP_ECR
) && ecpbase
) {
322 DPRINTF("ECP_ECR port\n");
324 DPRINTF("access to unsupported address range!\n");
330 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
331 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
332 tr
->fAutoinc
, tr
->dwOptions
);
334 if (tr
->cmdTrans
== 10)
335 DPRINTF("read byte: %d\n", tr
->Data
.Byte
);
341 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
342 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
343 struct version_struct
*version
;
346 if (wdheader
->magic
!= MAGIC
) {
347 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
348 return (*ioctl_func
) (fd
, request
, wdioctl
);
351 switch(request
& ~(0xc0000000)) {
353 version
= (struct version_struct
*)(wdheader
->data
);
354 strcpy(version
->version
, "libusb-driver.so $Revision: 1.63 $");
355 version
->versionul
= 802;
356 DPRINTF("VERSION\n");
360 DPRINTF("LICENSE\n");
363 case CARD_REGISTER_OLD
:
365 DPRINTF("CARD_REGISTER\n");
367 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
370 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
372 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
373 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
374 cr
->Card
.Item
[0].I
.IO
.dwBar
);
376 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
378 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
379 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
380 cr
->Card
.Item
[1].I
.IO
.dwBar
);
382 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
385 /* FIXME: Ugly hack which maps amontec JtagKey to 4. parallel port */
387 if ((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
== 0x30) {
388 ret
=jtagkey_init(0x0403, 0xcff8); /* I need a config file... */
390 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
398 snprintf(ppdev
, sizeof(ppdev
), "/dev/parport%lu",
399 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
400 DPRINTF("opening %s\n", ppdev
);
401 parportfd
= open(ppdev
, O_RDWR
|O_EXCL
);
404 fprintf(stderr
,"Can't open %s: %s\n", ppdev
, strerror(errno
));
407 if (parportfd
>= 0) {
410 if (ioctl(parportfd
, PPCLAIM
) == -1)
414 pmode
= IEEE1284_MODE_COMPAT
;
415 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
418 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
) {
419 DPRINTF("ECP mode requested\n");
420 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
421 /* TODO: Implement ECP mode */
423 pmode
= IEEE1284_MODE_ECP
;
425 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1) {
427 pmode
= IEEE1284_MODE_COMPAT
;
428 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
434 cr
->hCard
= parportfd
;
437 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
441 DPRINTF("hCard: %lu\n", cr
->hCard
);
446 DPRINTF("in USB_TRANSFER");
448 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
451 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
452 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
453 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
454 DPRINTF("setup packet: ");
455 hexdump(ut
->SetupPacket
, 8);
457 if (!ut
->fRead
&& ut
->dwBufferSize
)
459 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
464 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
466 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
467 if (ut
->dwPipeNum
== 0) { /* control pipe */
468 int requesttype
, request
, value
, index
, size
;
469 requesttype
= ut
->SetupPacket
[0];
470 request
= ut
->SetupPacket
[1];
471 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
472 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
473 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
474 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
475 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
478 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
481 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
486 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
488 ut
->dwBytesTransferred
= ret
;
494 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
495 if (ut
->fRead
&& ut
->dwBytesTransferred
)
498 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
506 DPRINTF("INT_ENABLE\n");
508 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
510 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
511 it
->hInterrupt
, it
->dwOptions
,
512 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
513 it
->dwLost
, it
->fStopped
);
518 pthread_mutex_trylock(&int_wait
);
524 DPRINTF("INT_DISABLE\n");
526 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
528 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
529 it
->hInterrupt
, it
->dwOptions
,
530 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
531 it
->dwLost
, it
->fStopped
);
533 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
538 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
539 pthread_mutex_unlock(&int_wait
);
541 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
542 it
->hInterrupt
, it
->dwOptions
,
543 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
544 it
->dwLost
, it
->fStopped
);
548 case USB_SET_INTERFACE
:
549 DPRINTF("USB_SET_INTERFACE\n");
551 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
553 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
554 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
555 usi
->dwAlternateSetting
, usi
->dwOptions
);
557 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
561 usb_devhandle
= usb_open(usbdevice
);
563 /* FIXME: Select right interface! */
564 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
567 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
568 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
570 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
572 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
575 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
576 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
577 ret
, usb_strerror());
581 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
582 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
583 usi
->dwAlternateSetting
, usi
->dwOptions
);
587 case USB_GET_DEVICE_DATA_OLD
:
588 case USB_GET_DEVICE_DATA
:
589 DPRINTF("USB_GET_DEVICE_DATA\n");
591 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
594 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
595 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
598 pSize
= ugdd
->dwBytes
;
599 if (!ugdd
->dwBytes
) {
601 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
604 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
609 case EVENT_REGISTER_OLD
:
611 DPRINTF("EVENT_REGISTER\n");
613 struct event
*e
= (struct event
*)(wdheader
->data
);
617 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",
618 e
->handle
, e
->dwAction
,
619 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
620 e
->hKernelPlugIn
, e
->dwOptions
,
621 e
->u
.Usb
.deviceId
.dwVendorId
,
622 e
->u
.Usb
.deviceId
.dwProductId
,
623 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
624 e
->dwNumMatchTables
);
626 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
628 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
629 e
->matchTables
[i
].VendorId
,
630 e
->matchTables
[i
].ProductId
,
631 e
->matchTables
[i
].bDeviceClass
,
632 e
->matchTables
[i
].bDeviceSubClass
,
633 e
->matchTables
[i
].bInterfaceClass
,
634 e
->matchTables
[i
].bInterfaceSubClass
,
635 e
->matchTables
[i
].bInterfaceProtocol
);
637 for (bus
= busses
; bus
; bus
= bus
->next
) {
638 struct usb_device
*dev
;
640 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
641 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
643 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
644 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
645 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
646 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
648 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
649 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
652 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
654 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
655 interface
->altsetting
[i
].bInterfaceClass
,
656 interface
->altsetting
[i
].bInterfaceSubClass
,
657 interface
->altsetting
[i
].bInterfaceProtocol
);
659 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
660 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
661 /* TODO: check interfaceClass! */
662 DPRINTF("found device with libusb\n");
664 card_type
= e
->dwCardType
;
674 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
680 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",
681 e
->handle
, e
->dwAction
,
682 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
683 e
->hKernelPlugIn
, e
->dwOptions
,
684 e
->u
.Usb
.deviceId
.dwVendorId
,
685 e
->u
.Usb
.deviceId
.dwProductId
,
686 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
687 e
->dwNumMatchTables
);
689 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
690 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
691 e
->matchTables
[i
].VendorId
,
692 e
->matchTables
[i
].ProductId
,
693 e
->matchTables
[i
].bDeviceClass
,
694 e
->matchTables
[i
].bDeviceSubClass
,
695 e
->matchTables
[i
].bInterfaceClass
,
696 e
->matchTables
[i
].bInterfaceSubClass
,
697 e
->matchTables
[i
].bInterfaceProtocol
);
704 DPRINTF("TRANSFER\n");
706 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
709 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
713 if (ppbase
== 0x30) {
714 ret
= jtagkey_transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
718 ret
= pp_transfer(tr
, fd
, request
);
723 case MULTI_TRANSFER_OLD
:
725 DPRINTF("MULTI_TRANSFER\n");
727 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
728 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
731 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
735 /* FIXME: Config file and mor intelligent mapping! */
736 if (ppbase
== 0x30) {
737 ret
= jtagkey_transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
742 for (i
= 0; i
< num
; i
++) {
743 DPRINTF("Transfer %d:\n", i
+1);
744 ret
= pp_transfer(tr
+ i
, fd
, request
);
750 case EVENT_UNREGISTER
:
751 DPRINTF("EVENT_UNREGISTER\n");
753 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
758 DPRINTF("INT_WAIT\n");
760 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
762 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
763 it
->hInterrupt
, it
->dwOptions
,
764 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
765 it
->dwLost
, it
->fStopped
);
768 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
771 if (it
->dwCounter
== 0) {
774 pthread_mutex_lock(&int_wait
);
775 pthread_mutex_unlock(&int_wait
);
778 pthread_mutex_lock(&int_wait
);
779 pthread_mutex_unlock(&int_wait
);
783 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
784 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
785 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
790 case CARD_UNREGISTER
:
791 DPRINTF("CARD_UNREGISTER\n");
793 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
795 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
796 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
797 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
798 cr
->Card
.Item
[0].I
.IO
.dwBar
);
800 DPRINTF("hCard: %lu\n", cr
->hCard
);
803 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
805 if (parportfd
== cr
->hCard
&& parportfd
>= 0) {
806 ioctl(parportfd
, PPRELEASE
);
815 DPRINTF("EVENT_PULL\n");
817 struct event
*e
= (struct event
*)(wdheader
->data
);
821 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",
822 e
->handle
, e
->dwAction
, e
->dwStatus
,
823 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
824 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
825 e
->u
.Usb
.deviceId
.dwProductId
,
826 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
827 e
->dwNumMatchTables
);
829 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
830 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
831 e
->matchTables
[i
].VendorId
,
832 e
->matchTables
[i
].ProductId
,
833 e
->matchTables
[i
].bDeviceClass
,
834 e
->matchTables
[i
].bDeviceSubClass
,
835 e
->matchTables
[i
].bInterfaceClass
,
836 e
->matchTables
[i
].bInterfaceSubClass
,
837 e
->matchTables
[i
].bInterfaceProtocol
);
841 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
844 struct usb_interface
*interface
= usbdevice
->config
->interface
;
846 e
->dwCardType
= card_type
;
849 e
->u
.Usb
.dwUniqueID
= 110;
850 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
851 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
852 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
853 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
854 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
855 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
856 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
861 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",
862 e
->handle
, e
->dwAction
, e
->dwStatus
,
863 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
864 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
865 e
->u
.Usb
.deviceId
.dwProductId
,
866 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
867 e
->dwNumMatchTables
);
869 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
870 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
871 e
->matchTables
[i
].VendorId
,
872 e
->matchTables
[i
].ProductId
,
873 e
->matchTables
[i
].bDeviceClass
,
874 e
->matchTables
[i
].bDeviceSubClass
,
875 e
->matchTables
[i
].bInterfaceClass
,
876 e
->matchTables
[i
].bInterfaceSubClass
,
877 e
->matchTables
[i
].bInterfaceProtocol
);
884 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
886 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
894 int ioctl(int fd
, unsigned long int request
, ...) {
900 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
902 va_start (args
, request
);
903 argp
= va_arg (args
, void *);
907 ret
= do_wdioctl(fd
, request
, argp
);
909 ret
= (*ioctl_func
) (fd
, request
, argp
);
914 int open (const char *pathname
, int flags
, ...) {
915 static int (*func
) (const char *, int, mode_t
) = NULL
;
921 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
923 if (flags
& O_CREAT
) {
924 va_start(args
, flags
);
925 mode
= va_arg(args
, mode_t
);
929 if (!strcmp (pathname
, "/dev/windrvr6")) {
930 DPRINTF("opening windrvr6\n");
932 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
934 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
941 busses
= usb_get_busses();
947 return (*func
) (pathname
, flags
, mode
);
951 static int (*func
) (int) = NULL
;
954 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
956 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
957 DPRINTF("close windrvrfd\n");
958 if (usbinterface
>= 0)
959 usb_release_interface(usb_devhandle
, usbinterface
);
962 usb_close(usb_devhandle
);
964 usb_devhandle
= NULL
;
972 FILE *fopen(const char *path
, const char *mode
) {
974 static FILE* (*func
) (const char*, const char*) = NULL
;
979 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
982 /* FIXME: Hack for parport mapping */
983 if (!strcmp(path
, "/proc/sys/dev/parport/parport3/base-addr")) {
984 ret
= (*func
) ("/dev/null", mode
);
987 ret
= (*func
) (path
, mode
);
989 if (!strcmp(path
, "/proc/modules")) {
990 DPRINTF("opening /proc/modules\n");
998 for (i
= 0; i
< 4; i
++) {
999 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
1000 if (!strcmp(path
, buf
)) {
1001 DPRINTF("open base-addr of parport%d\n", i
);
1011 char *fgets(char *s
, int size
, FILE *stream
) {
1012 static char* (*func
) (char*, int, FILE*) = NULL
;
1013 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
1019 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
1021 if (modulesfp
== stream
) {
1022 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
1023 strcpy(s
, modules
[modules_read
]);
1027 } else if (baseaddrfp
== stream
) {
1028 snprintf(s
, sizeof(buf
), "%d\t%d\n",
1029 (baseaddrnum
) * 0x10,
1030 ((baseaddrnum
) * 0x10) + 0x400);
1033 ret
= (*func
)(s
,size
,stream
);
1039 int fclose(FILE *fp
) {
1040 static int (*func
) (FILE*) = NULL
;
1043 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
1045 if (fp
== modulesfp
) {
1049 if (fp
== baseaddrfp
) {
1056 int access(const char *pathname
, int mode
) {
1057 static int (*func
) (const char*, int);
1060 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
1062 if (!strcmp(pathname
, "/dev/windrvr6")) {
1065 return (*func
)(pathname
, mode
);