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.61 $");
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)
401 snprintf(ppdev
, sizeof(ppdev
), "/dev/parport%lu",
402 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
403 DPRINTF("opening %s\n", ppdev
);
404 parportfd
= open(ppdev
, O_RDWR
|O_EXCL
);
407 fprintf(stderr
,"Can't open %s: %s\n", ppdev
, strerror(errno
));
410 if (parportfd
>= 0) {
413 if (ioctl(parportfd
, PPCLAIM
) == -1)
417 pmode
= IEEE1284_MODE_COMPAT
;
418 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
421 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
) {
422 DPRINTF("ECP mode requested\n");
423 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
424 /* TODO: Implement ECP mode */
426 pmode
= IEEE1284_MODE_ECP
;
428 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1) {
430 pmode
= IEEE1284_MODE_COMPAT
;
431 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
437 cr
->hCard
= parportfd
;
441 ret
=jtagkey_init(0x0403, 0xcff8); /* I need a config file... */
446 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
450 DPRINTF("hCard: %lu\n", cr
->hCard
);
455 DPRINTF("in USB_TRANSFER");
457 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
460 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
461 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
462 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
463 DPRINTF("setup packet: ");
464 hexdump(ut
->SetupPacket
, 8);
466 if (!ut
->fRead
&& ut
->dwBufferSize
)
468 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
473 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
475 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
476 if (ut
->dwPipeNum
== 0) { /* control pipe */
477 int requesttype
, request
, value
, index
, size
;
478 requesttype
= ut
->SetupPacket
[0];
479 request
= ut
->SetupPacket
[1];
480 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
481 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
482 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
483 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
484 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
487 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
490 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
495 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
497 ut
->dwBytesTransferred
= ret
;
503 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
504 if (ut
->fRead
&& ut
->dwBytesTransferred
)
507 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
515 DPRINTF("INT_ENABLE\n");
517 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
519 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
520 it
->hInterrupt
, it
->dwOptions
,
521 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
522 it
->dwLost
, it
->fStopped
);
527 pthread_mutex_trylock(&int_wait
);
533 DPRINTF("INT_DISABLE\n");
535 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
537 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
538 it
->hInterrupt
, it
->dwOptions
,
539 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
540 it
->dwLost
, it
->fStopped
);
542 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
547 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
548 pthread_mutex_unlock(&int_wait
);
550 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
551 it
->hInterrupt
, it
->dwOptions
,
552 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
553 it
->dwLost
, it
->fStopped
);
557 case USB_SET_INTERFACE
:
558 DPRINTF("USB_SET_INTERFACE\n");
560 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
562 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
563 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
564 usi
->dwAlternateSetting
, usi
->dwOptions
);
566 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
570 usb_devhandle
= usb_open(usbdevice
);
572 /* FIXME: Select right interface! */
573 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
576 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
577 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
579 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
581 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
584 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
585 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
586 ret
, usb_strerror());
590 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
591 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
592 usi
->dwAlternateSetting
, usi
->dwOptions
);
596 case USB_GET_DEVICE_DATA_OLD
:
597 case USB_GET_DEVICE_DATA
:
598 DPRINTF("USB_GET_DEVICE_DATA\n");
600 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
603 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
604 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
607 pSize
= ugdd
->dwBytes
;
608 if (!ugdd
->dwBytes
) {
610 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
613 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
618 case EVENT_REGISTER_OLD
:
620 DPRINTF("EVENT_REGISTER\n");
622 struct event
*e
= (struct event
*)(wdheader
->data
);
626 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",
627 e
->handle
, e
->dwAction
,
628 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
629 e
->hKernelPlugIn
, e
->dwOptions
,
630 e
->u
.Usb
.deviceId
.dwVendorId
,
631 e
->u
.Usb
.deviceId
.dwProductId
,
632 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
633 e
->dwNumMatchTables
);
635 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
637 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
638 e
->matchTables
[i
].VendorId
,
639 e
->matchTables
[i
].ProductId
,
640 e
->matchTables
[i
].bDeviceClass
,
641 e
->matchTables
[i
].bDeviceSubClass
,
642 e
->matchTables
[i
].bInterfaceClass
,
643 e
->matchTables
[i
].bInterfaceSubClass
,
644 e
->matchTables
[i
].bInterfaceProtocol
);
646 for (bus
= busses
; bus
; bus
= bus
->next
) {
647 struct usb_device
*dev
;
649 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
650 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
652 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
653 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
654 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
655 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
657 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
658 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
661 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
663 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
664 interface
->altsetting
[i
].bInterfaceClass
,
665 interface
->altsetting
[i
].bInterfaceSubClass
,
666 interface
->altsetting
[i
].bInterfaceProtocol
);
668 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
669 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
670 /* TODO: check interfaceClass! */
671 DPRINTF("found device with libusb\n");
673 card_type
= e
->dwCardType
;
683 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
689 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",
690 e
->handle
, e
->dwAction
,
691 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
692 e
->hKernelPlugIn
, e
->dwOptions
,
693 e
->u
.Usb
.deviceId
.dwVendorId
,
694 e
->u
.Usb
.deviceId
.dwProductId
,
695 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
696 e
->dwNumMatchTables
);
698 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
699 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
700 e
->matchTables
[i
].VendorId
,
701 e
->matchTables
[i
].ProductId
,
702 e
->matchTables
[i
].bDeviceClass
,
703 e
->matchTables
[i
].bDeviceSubClass
,
704 e
->matchTables
[i
].bInterfaceClass
,
705 e
->matchTables
[i
].bInterfaceSubClass
,
706 e
->matchTables
[i
].bInterfaceProtocol
);
713 DPRINTF("TRANSFER\n");
715 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
717 ret
= pp_transfer(tr
, fd
, request
, wdioctl
);
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 for (i
= 0; i
< num
; i
++) {
730 DPRINTF("Transfer %d:\n", i
+1);
732 wdheader
->size
= sizeof(WD_TRANSFER
);
734 wdheader
->data
= tr
+ i
;
736 ret
= pp_transfer(tr
+ i
, fd
, request
, wdioctl
);
747 case EVENT_UNREGISTER
:
748 DPRINTF("EVENT_UNREGISTER\n");
750 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
755 DPRINTF("INT_WAIT\n");
757 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
759 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
760 it
->hInterrupt
, it
->dwOptions
,
761 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
762 it
->dwLost
, it
->fStopped
);
765 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
768 if (it
->dwCounter
== 0) {
771 pthread_mutex_lock(&int_wait
);
772 pthread_mutex_unlock(&int_wait
);
775 pthread_mutex_lock(&int_wait
);
776 pthread_mutex_unlock(&int_wait
);
780 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
781 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
782 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
787 case CARD_UNREGISTER
:
788 DPRINTF("CARD_UNREGISTER\n");
790 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
792 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
793 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
794 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
795 cr
->Card
.Item
[0].I
.IO
.dwBar
);
797 DPRINTF("hCard: %lu\n", cr
->hCard
);
800 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
802 if (parportfd
== cr
->hCard
&& parportfd
>= 0) {
803 ioctl(parportfd
, PPRELEASE
);
812 DPRINTF("EVENT_PULL\n");
814 struct event
*e
= (struct event
*)(wdheader
->data
);
818 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",
819 e
->handle
, e
->dwAction
, e
->dwStatus
,
820 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
821 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
822 e
->u
.Usb
.deviceId
.dwProductId
,
823 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
824 e
->dwNumMatchTables
);
826 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
827 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
828 e
->matchTables
[i
].VendorId
,
829 e
->matchTables
[i
].ProductId
,
830 e
->matchTables
[i
].bDeviceClass
,
831 e
->matchTables
[i
].bDeviceSubClass
,
832 e
->matchTables
[i
].bInterfaceClass
,
833 e
->matchTables
[i
].bInterfaceSubClass
,
834 e
->matchTables
[i
].bInterfaceProtocol
);
838 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
841 struct usb_interface
*interface
= usbdevice
->config
->interface
;
843 e
->dwCardType
= card_type
;
846 e
->u
.Usb
.dwUniqueID
= 110;
847 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
848 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
849 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
850 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
851 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
852 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
853 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
858 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",
859 e
->handle
, e
->dwAction
, e
->dwStatus
,
860 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
861 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
862 e
->u
.Usb
.deviceId
.dwProductId
,
863 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
864 e
->dwNumMatchTables
);
866 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
867 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
868 e
->matchTables
[i
].VendorId
,
869 e
->matchTables
[i
].ProductId
,
870 e
->matchTables
[i
].bDeviceClass
,
871 e
->matchTables
[i
].bDeviceSubClass
,
872 e
->matchTables
[i
].bInterfaceClass
,
873 e
->matchTables
[i
].bInterfaceSubClass
,
874 e
->matchTables
[i
].bInterfaceProtocol
);
881 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
883 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
891 int ioctl(int fd
, unsigned long int request
, ...) {
897 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
899 va_start (args
, request
);
900 argp
= va_arg (args
, void *);
904 ret
= do_wdioctl(fd
, request
, argp
);
906 ret
= (*ioctl_func
) (fd
, request
, argp
);
911 int open (const char *pathname
, int flags
, ...) {
912 static int (*func
) (const char *, int, mode_t
) = NULL
;
918 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
920 if (flags
& O_CREAT
) {
921 va_start(args
, flags
);
922 mode
= va_arg(args
, mode_t
);
926 if (!strcmp (pathname
, "/dev/windrvr6")) {
927 DPRINTF("opening windrvr6\n");
929 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
931 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
938 busses
= usb_get_busses();
944 return (*func
) (pathname
, flags
, mode
);
948 static int (*func
) (int) = NULL
;
951 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
953 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
954 DPRINTF("close windrvrfd\n");
955 if (usbinterface
>= 0)
956 usb_release_interface(usb_devhandle
, usbinterface
);
959 usb_close(usb_devhandle
);
961 usb_devhandle
= NULL
;
969 FILE *fopen(const char *path
, const char *mode
) {
971 static FILE* (*func
) (const char*, const char*) = NULL
;
976 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
979 /* FIXME: Hack for parport mapping */
980 if (!strcmp(path
, "/proc/sys/dev/parport/parport3/base-addr")) {
981 ret
= (*func
) ("/dev/null", mode
);
984 ret
= (*func
) (path
, mode
);
986 if (!strcmp(path
, "/proc/modules")) {
987 DPRINTF("opening /proc/modules\n");
995 for (i
= 0; i
< 4; i
++) {
996 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
997 if (!strcmp(path
, buf
)) {
998 DPRINTF("open base-addr of parport%d\n", i
);
1008 char *fgets(char *s
, int size
, FILE *stream
) {
1009 static char* (*func
) (char*, int, FILE*) = NULL
;
1010 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
1016 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
1018 if (modulesfp
== stream
) {
1019 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
1020 strcpy(s
, modules
[modules_read
]);
1024 } else if (baseaddrfp
== stream
) {
1025 snprintf(s
, sizeof(buf
), "%d\t%d\n",
1026 (baseaddrnum
) * 0x10,
1027 ((baseaddrnum
) * 0x10) + 0x400);
1030 ret
= (*func
)(s
,size
,stream
);
1036 int fclose(FILE *fp
) {
1037 static int (*func
) (FILE*) = NULL
;
1040 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
1042 if (fp
== modulesfp
) {
1046 if (fp
== baseaddrfp
) {
1053 int access(const char *pathname
, int mode
) {
1054 static int (*func
) (const char*, int);
1057 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
1059 if (!strcmp(pathname
, "/dev/windrvr6")) {
1062 return (*func
)(pathname
, mode
);