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
;
68 void hexdump(unsigned char *buf
, int len
) {
71 for(i
=0; i
<len
; i
++) {
72 fprintf(stderr
,"%02x ", buf
[i
]);
79 int usb_deviceinfo(unsigned char *buf
) {
82 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
83 WDU_INTERFACE
**pActiveInterface
;
86 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
88 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
89 udi
->Descriptor
.bDescriptorType
= usbdevice
->descriptor
.bDescriptorType
;
90 udi
->Descriptor
.bcdUSB
= usbdevice
->descriptor
.bcdUSB
;
91 udi
->Descriptor
.bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
92 udi
->Descriptor
.bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
93 udi
->Descriptor
.bDeviceProtocol
= usbdevice
->descriptor
.bDeviceProtocol
;
94 udi
->Descriptor
.bMaxPacketSize0
= usbdevice
->descriptor
.bMaxPacketSize0
;
95 udi
->Descriptor
.idVendor
= usbdevice
->descriptor
.idVendor
;
96 udi
->Descriptor
.idProduct
= usbdevice
->descriptor
.idProduct
;
97 udi
->Descriptor
.bcdDevice
= usbdevice
->descriptor
.bcdDevice
;
98 udi
->Descriptor
.iManufacturer
= usbdevice
->descriptor
.iManufacturer
;
99 udi
->Descriptor
.iProduct
= usbdevice
->descriptor
.iProduct
;
100 udi
->Descriptor
.iSerialNumber
= usbdevice
->descriptor
.iSerialNumber
;
101 udi
->Descriptor
.bNumConfigurations
= usbdevice
->descriptor
.bNumConfigurations
;
103 /* TODO: Fix Pipe0! */
104 udi
->Pipe0
.dwNumber
= 0x00;
105 udi
->Pipe0
.dwMaximumPacketSize
= usbdevice
->descriptor
.bMaxPacketSize0
;
107 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
108 udi
->Pipe0
.dwInterval
= 0;
110 pConfigs
= &(udi
->pConfigs
);
111 pActiveConfig
= &(udi
->pActiveConfig
);
112 pActiveInterface
= &(udi
->pActiveInterface
[0]);
115 len
= sizeof(struct usb_device_info
);
117 for (i
=0; i
<usbdevice
->descriptor
.bNumConfigurations
; i
++)
119 struct usb_config_descriptor
*conf_desc
= &usbdevice
->config
[i
];
120 WDU_INTERFACE
**pInterfaces
;
121 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
122 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
125 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
128 *pActiveConfig
= cfg
;
130 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
131 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
132 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
133 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
134 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
135 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
136 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
137 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
139 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
141 pInterfaces
= &(cfg
->pInterfaces
);
143 len
+= sizeof(WDU_CONFIGURATION
);
146 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
147 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
148 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
150 pActiveInterface
[j
] = iface
;
152 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
153 iface
->dwNumAltSettings
= usbdevice
->config
[i
].interface
[j
].num_altsetting
;
154 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
156 len
+= sizeof(WDU_INTERFACE
);
159 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
162 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
164 struct usb_interface
*interface
= &usbdevice
->config
[i
].interface
[j
];
167 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
169 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
172 for(k
=0; k
<interface
->num_altsetting
; k
++)
174 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
175 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
176 WDU_PIPE_INFO
**pPipes
;
179 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
181 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
182 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
183 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
184 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
185 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
186 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
187 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
188 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
189 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
190 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
191 pPipes
= &(altset
->pPipes
);
194 len
+=sizeof(WDU_ALTERNATE_SETTING
);
197 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
198 for (l
= 0; l
< bNumEndpoints
; l
++) {
199 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
201 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
202 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
203 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
204 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
205 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
206 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
208 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
211 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
212 for (l
= 0; l
< bNumEndpoints
; l
++) {
213 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
215 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
216 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
217 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
218 if (pi
->type
== PIPE_TYPE_CONTROL
)
219 pi
->direction
= WDU_DIR_IN_OUT
;
222 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
225 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
227 len
+= sizeof(WDU_PIPE_INFO
);
230 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
239 int pp_transfer(WD_TRANSFER
*tr
, int fd
, unsigned int request
) {
241 unsigned long port
= (unsigned long)tr
->dwPort
;
243 static unsigned char last_pp_write
= 0;
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
);
259 if (port
== ppbase
+ PP_DATA
) {
260 DPRINTF("data port\n");
261 switch(tr
->cmdTrans
) {
263 ret
= 0; /* We don't support reading of the data port */
267 ret
= ioctl(parportfd
, PPWDATA
, &val
);
272 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
276 } else if (port
== ppbase
+ PP_STATUS
) {
277 DPRINTF("status port (last write: %d)\n", last_pp_write
);
278 switch(tr
->cmdTrans
) {
280 ret
= ioctl(parportfd
, PPRSTATUS
, &val
);
281 #ifdef FORCE_PC3_IDENT
283 if (last_pp_write
& 0x40)
291 ret
= 0; /* Status Port is readonly */
295 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
299 } else if (port
== ppbase
+ PP_CONTROL
) {
300 DPRINTF("control port\n");
301 switch(tr
->cmdTrans
) {
303 ret
= ioctl(parportfd
, PPRCONTROL
, &val
);
307 ret
= ioctl(parportfd
, PPWCONTROL
, &val
);
311 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
315 } else if ((port
== ecpbase
+ PP_ECP_CFGA
) && ecpbase
) {
316 DPRINTF("ECP_CFGA port\n");
317 } else if ((port
== ecpbase
+ PP_ECP_CFGB
) && ecpbase
) {
318 DPRINTF("ECP_CFGB port\n");
319 } else if ((port
== ecpbase
+ PP_ECP_ECR
) && ecpbase
) {
320 DPRINTF("ECP_ECR port\n");
322 DPRINTF("access to unsupported address range!\n");
328 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
329 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
330 tr
->fAutoinc
, tr
->dwOptions
);
332 if (tr
->cmdTrans
== 10)
333 DPRINTF("read byte: %d\n", tr
->Data
.Byte
);
339 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
340 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
341 struct version_struct
*version
;
344 if (wdheader
->magic
!= MAGIC
) {
345 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
346 return (*ioctl_func
) (fd
, request
, wdioctl
);
349 switch(request
& ~(0xc0000000)) {
351 version
= (struct version_struct
*)(wdheader
->data
);
352 strcpy(version
->version
, "libusb-driver.so $Revision: 1.65 $");
353 version
->versionul
= 802;
354 DPRINTF("VERSION\n");
358 DPRINTF("LICENSE\n");
361 case CARD_REGISTER_OLD
:
363 DPRINTF("CARD_REGISTER\n");
365 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
368 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
370 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
371 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
372 cr
->Card
.Item
[0].I
.IO
.dwBar
);
374 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
376 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
377 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
378 cr
->Card
.Item
[1].I
.IO
.dwBar
);
380 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
383 /* FIXME: Ugly hack which maps amontec JtagKey to 4. parallel port */
385 if ((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
== 0x30) {
386 ret
=jtagkey_init(0x0403, 0xcff8); /* I need a config file... */
388 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
396 snprintf(ppdev
, sizeof(ppdev
), "/dev/parport%lu",
397 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
398 DPRINTF("opening %s\n", ppdev
);
399 parportfd
= open(ppdev
, O_RDWR
|O_EXCL
);
402 fprintf(stderr
,"Can't open %s: %s\n", ppdev
, strerror(errno
));
405 if (parportfd
>= 0) {
408 if (ioctl(parportfd
, PPCLAIM
) == -1)
412 pmode
= IEEE1284_MODE_COMPAT
;
413 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
416 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
) {
417 DPRINTF("ECP mode requested\n");
418 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
419 /* TODO: Implement ECP mode */
421 pmode
= IEEE1284_MODE_ECP
;
423 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1) {
425 pmode
= IEEE1284_MODE_COMPAT
;
426 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
432 cr
->hCard
= parportfd
;
435 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
439 DPRINTF("hCard: %lu\n", cr
->hCard
);
444 DPRINTF("in USB_TRANSFER");
446 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
449 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
450 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
451 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
452 DPRINTF("setup packet: ");
453 hexdump(ut
->SetupPacket
, 8);
455 if (!ut
->fRead
&& ut
->dwBufferSize
)
457 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
462 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
464 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
465 if (ut
->dwPipeNum
== 0) { /* control pipe */
466 int requesttype
, request
, value
, index
, size
;
467 requesttype
= ut
->SetupPacket
[0];
468 request
= ut
->SetupPacket
[1];
469 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
470 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
471 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
472 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
473 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
476 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
479 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
484 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
486 ut
->dwBytesTransferred
= ret
;
492 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
493 if (ut
->fRead
&& ut
->dwBytesTransferred
)
496 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
504 DPRINTF("INT_ENABLE\n");
506 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
508 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
509 it
->hInterrupt
, it
->dwOptions
,
510 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
511 it
->dwLost
, it
->fStopped
);
516 pthread_mutex_trylock(&int_wait
);
522 DPRINTF("INT_DISABLE\n");
524 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
526 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
527 it
->hInterrupt
, it
->dwOptions
,
528 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
529 it
->dwLost
, it
->fStopped
);
531 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
536 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
537 pthread_mutex_unlock(&int_wait
);
539 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
540 it
->hInterrupt
, it
->dwOptions
,
541 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
542 it
->dwLost
, it
->fStopped
);
546 case USB_SET_INTERFACE
:
547 DPRINTF("USB_SET_INTERFACE\n");
549 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
551 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
552 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
553 usi
->dwAlternateSetting
, usi
->dwOptions
);
555 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
559 usb_devhandle
= usb_open(usbdevice
);
561 /* FIXME: Select right interface! */
562 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
565 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
566 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
568 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
570 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
573 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
574 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
575 ret
, usb_strerror());
579 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
580 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
581 usi
->dwAlternateSetting
, usi
->dwOptions
);
585 case USB_GET_DEVICE_DATA_OLD
:
586 case USB_GET_DEVICE_DATA
:
587 DPRINTF("USB_GET_DEVICE_DATA\n");
589 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
592 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
593 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
596 pSize
= ugdd
->dwBytes
;
597 if (!ugdd
->dwBytes
) {
599 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
602 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
607 case EVENT_REGISTER_OLD
:
609 DPRINTF("EVENT_REGISTER\n");
611 struct event
*e
= (struct event
*)(wdheader
->data
);
615 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",
616 e
->handle
, e
->dwAction
,
617 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
618 e
->hKernelPlugIn
, e
->dwOptions
,
619 e
->u
.Usb
.deviceId
.dwVendorId
,
620 e
->u
.Usb
.deviceId
.dwProductId
,
621 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
622 e
->dwNumMatchTables
);
624 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
626 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
627 e
->matchTables
[i
].VendorId
,
628 e
->matchTables
[i
].ProductId
,
629 e
->matchTables
[i
].bDeviceClass
,
630 e
->matchTables
[i
].bDeviceSubClass
,
631 e
->matchTables
[i
].bInterfaceClass
,
632 e
->matchTables
[i
].bInterfaceSubClass
,
633 e
->matchTables
[i
].bInterfaceProtocol
);
635 for (bus
= busses
; bus
; bus
= bus
->next
) {
636 struct usb_device
*dev
;
638 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
639 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
641 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
642 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
643 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
644 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
646 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
647 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
650 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
652 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
653 interface
->altsetting
[i
].bInterfaceClass
,
654 interface
->altsetting
[i
].bInterfaceSubClass
,
655 interface
->altsetting
[i
].bInterfaceProtocol
);
657 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
658 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
659 /* TODO: check interfaceClass! */
660 DPRINTF("found device with libusb\n");
662 card_type
= e
->dwCardType
;
672 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
678 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",
679 e
->handle
, e
->dwAction
,
680 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
681 e
->hKernelPlugIn
, e
->dwOptions
,
682 e
->u
.Usb
.deviceId
.dwVendorId
,
683 e
->u
.Usb
.deviceId
.dwProductId
,
684 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
685 e
->dwNumMatchTables
);
687 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
688 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
689 e
->matchTables
[i
].VendorId
,
690 e
->matchTables
[i
].ProductId
,
691 e
->matchTables
[i
].bDeviceClass
,
692 e
->matchTables
[i
].bDeviceSubClass
,
693 e
->matchTables
[i
].bInterfaceClass
,
694 e
->matchTables
[i
].bInterfaceSubClass
,
695 e
->matchTables
[i
].bInterfaceProtocol
);
702 DPRINTF("TRANSFER\n");
704 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
707 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
711 if (ppbase
== 0x30) {
712 ret
= jtagkey_transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
716 ret
= pp_transfer(tr
, fd
, request
);
721 case MULTI_TRANSFER_OLD
:
723 DPRINTF("MULTI_TRANSFER\n");
725 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
726 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
729 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
733 /* FIXME: Config file and mor intelligent mapping! */
734 if (ppbase
== 0x30) {
735 ret
= jtagkey_transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
740 for (i
= 0; i
< num
; i
++) {
741 DPRINTF("Transfer %d:\n", i
+1);
742 ret
= pp_transfer(tr
+ i
, fd
, request
);
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
);
804 if (cr
->hCard
== 0xff)
808 if (parportfd
== cr
->hCard
&& parportfd
>= 0) {
809 ioctl(parportfd
, PPRELEASE
);
818 DPRINTF("EVENT_PULL\n");
820 struct event
*e
= (struct event
*)(wdheader
->data
);
824 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",
825 e
->handle
, e
->dwAction
, e
->dwStatus
,
826 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
827 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
828 e
->u
.Usb
.deviceId
.dwProductId
,
829 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
830 e
->dwNumMatchTables
);
832 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
833 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
834 e
->matchTables
[i
].VendorId
,
835 e
->matchTables
[i
].ProductId
,
836 e
->matchTables
[i
].bDeviceClass
,
837 e
->matchTables
[i
].bDeviceSubClass
,
838 e
->matchTables
[i
].bInterfaceClass
,
839 e
->matchTables
[i
].bInterfaceSubClass
,
840 e
->matchTables
[i
].bInterfaceProtocol
);
844 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
847 struct usb_interface
*interface
= usbdevice
->config
->interface
;
849 e
->dwCardType
= card_type
;
852 e
->u
.Usb
.dwUniqueID
= 110;
853 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
854 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
855 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
856 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
857 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
858 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
859 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
864 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",
865 e
->handle
, e
->dwAction
, e
->dwStatus
,
866 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
867 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
868 e
->u
.Usb
.deviceId
.dwProductId
,
869 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
870 e
->dwNumMatchTables
);
872 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
873 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
874 e
->matchTables
[i
].VendorId
,
875 e
->matchTables
[i
].ProductId
,
876 e
->matchTables
[i
].bDeviceClass
,
877 e
->matchTables
[i
].bDeviceSubClass
,
878 e
->matchTables
[i
].bInterfaceClass
,
879 e
->matchTables
[i
].bInterfaceSubClass
,
880 e
->matchTables
[i
].bInterfaceProtocol
);
887 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
889 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
897 int ioctl(int fd
, unsigned long int request
, ...) {
903 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
905 va_start (args
, request
);
906 argp
= va_arg (args
, void *);
910 ret
= do_wdioctl(fd
, request
, argp
);
912 ret
= (*ioctl_func
) (fd
, request
, argp
);
917 int open (const char *pathname
, int flags
, ...) {
918 static int (*func
) (const char *, int, mode_t
) = NULL
;
924 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
926 if (flags
& O_CREAT
) {
927 va_start(args
, flags
);
928 mode
= va_arg(args
, mode_t
);
932 if (!strcmp (pathname
, "/dev/windrvr6")) {
933 DPRINTF("opening windrvr6\n");
935 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
937 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
944 busses
= usb_get_busses();
950 return (*func
) (pathname
, flags
, mode
);
954 static int (*func
) (int) = NULL
;
957 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
959 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
960 DPRINTF("close windrvrfd\n");
961 if (usbinterface
>= 0)
962 usb_release_interface(usb_devhandle
, usbinterface
);
965 usb_close(usb_devhandle
);
967 usb_devhandle
= NULL
;
975 FILE *fopen(const char *path
, const char *mode
) {
977 static FILE* (*func
) (const char*, const char*) = NULL
;
982 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
985 /* FIXME: Hack for parport mapping */
986 if (!strcmp(path
, "/proc/sys/dev/parport/parport3/base-addr")) {
987 ret
= (*func
) ("/dev/null", mode
);
990 ret
= (*func
) (path
, mode
);
992 if (!strcmp(path
, "/proc/modules")) {
993 DPRINTF("opening /proc/modules\n");
1001 for (i
= 0; i
< 4; i
++) {
1002 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
1003 if (!strcmp(path
, buf
)) {
1004 DPRINTF("open base-addr of parport%d\n", i
);
1014 char *fgets(char *s
, int size
, FILE *stream
) {
1015 static char* (*func
) (char*, int, FILE*) = NULL
;
1016 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
1022 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
1024 if (modulesfp
== stream
) {
1025 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
1026 strcpy(s
, modules
[modules_read
]);
1030 } else if (baseaddrfp
== stream
) {
1031 snprintf(s
, sizeof(buf
), "%d\t%d\n",
1032 (baseaddrnum
) * 0x10,
1033 ((baseaddrnum
) * 0x10) + 0x400);
1036 ret
= (*func
)(s
,size
,stream
);
1042 int fclose(FILE *fp
) {
1043 static int (*func
) (FILE*) = NULL
;
1046 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
1048 if (fp
== modulesfp
) {
1052 if (fp
== baseaddrfp
) {
1059 int access(const char *pathname
, int mode
) {
1060 static int (*func
) (const char*, int);
1063 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
1065 if (!strcmp(pathname
, "/dev/windrvr6")) {
1068 return (*func
)(pathname
, mode
);