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"
46 static int (*ioctl_func
) (int, int, void *) = NULL
;
47 static int windrvrfd
= -1;
48 static int parportfd
= -1;
49 static unsigned long ppbase
= 0;
50 static unsigned long ecpbase
= 0;
51 FILE *modulesfp
= NULL
;
52 FILE *baseaddrfp
= NULL
;
54 static int modules_read
= 0;
55 static struct usb_bus
*busses
= NULL
;
56 static struct usb_device
*usbdevice
;
57 static usb_dev_handle
*usb_devhandle
= NULL
;
58 static int usbinterface
= -1;
59 static unsigned long card_type
;
60 static int ints_enabled
= 0;
61 static pthread_mutex_t int_wait
= PTHREAD_MUTEX_INITIALIZER
;
66 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
67 void hexdump(unsigned char *buf
, int len
) {
70 for(i
=0; i
<len
; i
++) {
71 fprintf(stderr
,"%02x ", buf
[i
]);
78 #define DPRINTF(format, args...)
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 int 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
);
259 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
264 if (port
== ppbase
+ PP_DATA
) {
265 DPRINTF("data port\n");
266 switch(tr
->cmdTrans
) {
268 ret
= 0; /* We don't support reading of the data port */
272 ret
= ioctl(parportfd
, PPWDATA
, &val
);
277 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
281 } else if (port
== ppbase
+ PP_STATUS
) {
282 DPRINTF("status port (last write: %d)\n", last_pp_write
);
283 switch(tr
->cmdTrans
) {
285 ret
= ioctl(parportfd
, PPRSTATUS
, &val
);
286 #ifdef FORCE_PC3_IDENT
288 if (last_pp_write
& 64)
296 ret
= 0; /* Status Port is readonly */
300 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
304 } else if (port
== ppbase
+ PP_CONTROL
) {
305 DPRINTF("control port\n");
306 switch(tr
->cmdTrans
) {
308 ret
= ioctl(parportfd
, PPRCONTROL
, &val
);
312 ret
= ioctl(parportfd
, PPWCONTROL
, &val
);
316 fprintf(stderr
,"!!!Unsupported TRANSFER command: %lu!!!\n", tr
->cmdTrans
);
320 } else if ((port
== ecpbase
+ PP_ECP_CFGA
) && ecpbase
) {
321 DPRINTF("ECP_CFGA port\n");
322 } else if ((port
== ecpbase
+ PP_ECP_CFGB
) && ecpbase
) {
323 DPRINTF("ECP_CFGB port\n");
324 } else if ((port
== ecpbase
+ PP_ECP_ECR
) && ecpbase
) {
325 DPRINTF("ECP_ECR port\n");
327 DPRINTF("access to unsupported address range!\n");
334 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
335 (unsigned long)tr
->dwPort
, tr
->cmdTrans
, tr
->dwBytes
,
336 tr
->fAutoinc
, tr
->dwOptions
);
338 if (tr
->cmdTrans
== 10)
339 DPRINTF("read byte: %d\n", tr
->Data
.Byte
);
345 int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
346 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
347 struct version_struct
*version
;
350 if (wdheader
->magic
!= MAGIC
) {
351 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
352 return (*ioctl_func
) (fd
, request
, wdioctl
);
355 switch(request
& ~(0xc0000000)) {
357 version
= (struct version_struct
*)(wdheader
->data
);
358 strcpy(version
->version
, "libusb-driver.so $Revision: 1.60 $");
359 version
->versionul
= 802;
360 DPRINTF("VERSION\n");
364 DPRINTF("LICENSE\n");
367 case CARD_REGISTER_OLD
:
369 DPRINTF("CARD_REGISTER\n");
371 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
374 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
376 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
377 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
378 cr
->Card
.Item
[0].I
.IO
.dwBar
);
380 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
382 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
383 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
384 cr
->Card
.Item
[1].I
.IO
.dwBar
);
386 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
389 snprintf(ppdev
, sizeof(ppdev
), "/dev/parport%lu",
390 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
391 DPRINTF("opening %s\n", ppdev
);
392 parportfd
= open(ppdev
, O_RDWR
|O_EXCL
);
395 fprintf(stderr
,"Can't open %s: %s\n", ppdev
, strerror(errno
));
398 if (parportfd
>= 0) {
401 if (ioctl(parportfd
, PPCLAIM
) == -1)
405 pmode
= IEEE1284_MODE_COMPAT
;
406 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
409 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
) {
410 DPRINTF("ECP mode requested\n");
411 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
412 /* TODO: Implement ECP mode */
414 pmode
= IEEE1284_MODE_ECP
;
416 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1) {
418 pmode
= IEEE1284_MODE_COMPAT
;
419 if (ioctl(parportfd
, PPNEGOT
, &pmode
) == -1)
425 cr
->hCard
= parportfd
;
426 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
429 DPRINTF("hCard: %lu\n", cr
->hCard
);
434 DPRINTF("in USB_TRANSFER");
436 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
439 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
440 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
441 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
442 DPRINTF("setup packet: ");
443 hexdump(ut
->SetupPacket
, 8);
445 if (!ut
->fRead
&& ut
->dwBufferSize
)
447 hexdump(ut
->pBuffer
, ut
->dwBufferSize
);
452 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
454 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
455 if (ut
->dwPipeNum
== 0) { /* control pipe */
456 int requesttype
, request
, value
, index
, size
;
457 requesttype
= ut
->SetupPacket
[0];
458 request
= ut
->SetupPacket
[1];
459 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
460 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
461 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
462 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
463 ret
= usb_control_msg(usb_devhandle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
466 ret
= usb_bulk_read(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
469 ret
= usb_bulk_write(usb_devhandle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
474 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
476 ut
->dwBytesTransferred
= ret
;
482 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
483 if (ut
->fRead
&& ut
->dwBytesTransferred
)
486 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
);
494 DPRINTF("INT_ENABLE\n");
496 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
498 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
499 it
->hInterrupt
, it
->dwOptions
,
500 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
501 it
->dwLost
, it
->fStopped
);
506 pthread_mutex_trylock(&int_wait
);
512 DPRINTF("INT_DISABLE\n");
514 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
516 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
517 it
->hInterrupt
, it
->dwOptions
,
518 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
519 it
->dwLost
, it
->fStopped
);
521 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
526 if (pthread_mutex_trylock(&int_wait
) == EBUSY
)
527 pthread_mutex_unlock(&int_wait
);
529 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
530 it
->hInterrupt
, it
->dwOptions
,
531 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
532 it
->dwLost
, it
->fStopped
);
536 case USB_SET_INTERFACE
:
537 DPRINTF("USB_SET_INTERFACE\n");
539 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
541 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
542 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
543 usi
->dwAlternateSetting
, usi
->dwOptions
);
545 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
549 usb_devhandle
= usb_open(usbdevice
);
551 /* FIXME: Select right interface! */
552 ret
= usb_claim_interface(usb_devhandle
, usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
);
555 usbinterface
= usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
556 ret
= usb_set_altinterface(usb_devhandle
, usi
->dwAlternateSetting
);
558 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
560 fprintf(stderr
, "usb_set_configuration: %d (%s)\n", ret
, usb_strerror());
563 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
564 usbdevice
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
,
565 ret
, usb_strerror());
569 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
570 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
571 usi
->dwAlternateSetting
, usi
->dwOptions
);
575 case USB_GET_DEVICE_DATA_OLD
:
576 case USB_GET_DEVICE_DATA
:
577 DPRINTF("USB_GET_DEVICE_DATA\n");
579 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
582 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
583 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
586 pSize
= ugdd
->dwBytes
;
587 if (!ugdd
->dwBytes
) {
589 ugdd
->dwBytes
= usb_deviceinfo(NULL
);
592 usb_deviceinfo((unsigned char*)ugdd
->pBuf
);
597 case EVENT_REGISTER_OLD
:
599 DPRINTF("EVENT_REGISTER\n");
601 struct event
*e
= (struct event
*)(wdheader
->data
);
605 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",
606 e
->handle
, e
->dwAction
,
607 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
608 e
->hKernelPlugIn
, e
->dwOptions
,
609 e
->u
.Usb
.deviceId
.dwVendorId
,
610 e
->u
.Usb
.deviceId
.dwProductId
,
611 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
612 e
->dwNumMatchTables
);
614 for (i
= 0; i
< e
->dwNumMatchTables
; i
++) {
616 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
617 e
->matchTables
[i
].VendorId
,
618 e
->matchTables
[i
].ProductId
,
619 e
->matchTables
[i
].bDeviceClass
,
620 e
->matchTables
[i
].bDeviceSubClass
,
621 e
->matchTables
[i
].bInterfaceClass
,
622 e
->matchTables
[i
].bInterfaceSubClass
,
623 e
->matchTables
[i
].bInterfaceProtocol
);
625 for (bus
= busses
; bus
; bus
= bus
->next
) {
626 struct usb_device
*dev
;
628 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
629 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
631 if((desc
->idVendor
== e
->matchTables
[i
].VendorId
) &&
632 (desc
->idProduct
== e
->matchTables
[i
].ProductId
) &&
633 (desc
->bDeviceClass
== e
->matchTables
[i
].bDeviceClass
) &&
634 (desc
->bDeviceSubClass
== e
->matchTables
[i
].bDeviceSubClass
)) {
636 for (ac
= 0; ac
< desc
->bNumConfigurations
; ac
++) {
637 struct usb_interface
*interface
= dev
->config
[ac
].interface
;
640 for (ai
= 0; ai
< interface
->num_altsetting
; ai
++) {
642 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
643 interface
->altsetting
[i
].bInterfaceClass
,
644 interface
->altsetting
[i
].bInterfaceSubClass
,
645 interface
->altsetting
[i
].bInterfaceProtocol
);
647 if ((interface
->altsetting
[ai
].bInterfaceSubClass
== e
->matchTables
[i
].bInterfaceSubClass
) &&
648 (interface
->altsetting
[ai
].bInterfaceProtocol
== e
->matchTables
[i
].bInterfaceProtocol
)){
649 /* TODO: check interfaceClass! */
650 DPRINTF("found device with libusb\n");
652 card_type
= e
->dwCardType
;
662 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
668 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",
669 e
->handle
, e
->dwAction
,
670 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
671 e
->hKernelPlugIn
, e
->dwOptions
,
672 e
->u
.Usb
.deviceId
.dwVendorId
,
673 e
->u
.Usb
.deviceId
.dwProductId
,
674 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
675 e
->dwNumMatchTables
);
677 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
678 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
679 e
->matchTables
[i
].VendorId
,
680 e
->matchTables
[i
].ProductId
,
681 e
->matchTables
[i
].bDeviceClass
,
682 e
->matchTables
[i
].bDeviceSubClass
,
683 e
->matchTables
[i
].bInterfaceClass
,
684 e
->matchTables
[i
].bInterfaceSubClass
,
685 e
->matchTables
[i
].bInterfaceProtocol
);
692 DPRINTF("TRANSFER\n");
694 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
696 ret
= pp_transfer(tr
, fd
, request
, wdioctl
);
700 case MULTI_TRANSFER_OLD
:
702 DPRINTF("MULTI_TRANSFER\n");
704 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
705 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
709 for (i
= 0; i
< num
; i
++) {
710 DPRINTF("Transfer %d:\n", i
+1);
712 wdheader
->size
= sizeof(WD_TRANSFER
);
714 wdheader
->data
= tr
+ i
;
716 ret
= pp_transfer(tr
+ i
, fd
, request
, wdioctl
);
727 case EVENT_UNREGISTER
:
728 DPRINTF("EVENT_UNREGISTER\n");
730 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
735 DPRINTF("INT_WAIT\n");
737 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
739 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
740 it
->hInterrupt
, it
->dwOptions
,
741 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
742 it
->dwLost
, it
->fStopped
);
745 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
748 if (it
->dwCounter
== 0) {
751 pthread_mutex_lock(&int_wait
);
752 pthread_mutex_unlock(&int_wait
);
755 pthread_mutex_lock(&int_wait
);
756 pthread_mutex_unlock(&int_wait
);
760 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
761 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
762 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
767 case CARD_UNREGISTER
:
768 DPRINTF("CARD_UNREGISTER\n");
770 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
772 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
773 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
774 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
775 cr
->Card
.Item
[0].I
.IO
.dwBar
);
777 DPRINTF("hCard: %lu\n", cr
->hCard
);
780 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
782 if (parportfd
== cr
->hCard
&& parportfd
>= 0) {
783 ioctl(parportfd
, PPRELEASE
);
792 DPRINTF("EVENT_PULL\n");
794 struct event
*e
= (struct event
*)(wdheader
->data
);
798 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",
799 e
->handle
, e
->dwAction
, e
->dwStatus
,
800 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
801 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
802 e
->u
.Usb
.deviceId
.dwProductId
,
803 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
804 e
->dwNumMatchTables
);
806 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
807 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
808 e
->matchTables
[i
].VendorId
,
809 e
->matchTables
[i
].ProductId
,
810 e
->matchTables
[i
].bDeviceClass
,
811 e
->matchTables
[i
].bDeviceSubClass
,
812 e
->matchTables
[i
].bInterfaceClass
,
813 e
->matchTables
[i
].bInterfaceSubClass
,
814 e
->matchTables
[i
].bInterfaceProtocol
);
818 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
821 struct usb_interface
*interface
= usbdevice
->config
->interface
;
823 e
->dwCardType
= card_type
;
826 e
->u
.Usb
.dwUniqueID
= 110;
827 e
->matchTables
[0].VendorId
= usbdevice
->descriptor
.idVendor
;
828 e
->matchTables
[0].ProductId
= usbdevice
->descriptor
.idProduct
;
829 e
->matchTables
[0].bDeviceClass
= usbdevice
->descriptor
.bDeviceClass
;
830 e
->matchTables
[0].bDeviceSubClass
= usbdevice
->descriptor
.bDeviceSubClass
;
831 e
->matchTables
[0].bInterfaceClass
= interface
->altsetting
[0].bInterfaceClass
;
832 e
->matchTables
[0].bInterfaceSubClass
= interface
->altsetting
[0].bInterfaceSubClass
;
833 e
->matchTables
[0].bInterfaceProtocol
= interface
->altsetting
[0].bInterfaceProtocol
;
838 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",
839 e
->handle
, e
->dwAction
, e
->dwStatus
,
840 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
841 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
842 e
->u
.Usb
.deviceId
.dwProductId
,
843 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
844 e
->dwNumMatchTables
);
846 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
847 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
848 e
->matchTables
[i
].VendorId
,
849 e
->matchTables
[i
].ProductId
,
850 e
->matchTables
[i
].bDeviceClass
,
851 e
->matchTables
[i
].bDeviceSubClass
,
852 e
->matchTables
[i
].bInterfaceClass
,
853 e
->matchTables
[i
].bInterfaceSubClass
,
854 e
->matchTables
[i
].bInterfaceProtocol
);
861 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
863 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
871 int ioctl(int fd
, unsigned long int request
, ...) {
877 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
879 va_start (args
, request
);
880 argp
= va_arg (args
, void *);
884 ret
= do_wdioctl(fd
, request
, argp
);
886 ret
= (*ioctl_func
) (fd
, request
, argp
);
891 int open (const char *pathname
, int flags
, ...) {
892 static int (*func
) (const char *, int, mode_t
) = NULL
;
898 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
900 if (flags
& O_CREAT
) {
901 va_start(args
, flags
);
902 mode
= va_arg(args
, mode_t
);
906 if (!strcmp (pathname
, "/dev/windrvr6")) {
907 DPRINTF("opening windrvr6\n");
909 windrvrfd
= fd
= (*func
) ("/dev/null", flags
, mode
);
911 windrvrfd
= fd
= (*func
) (pathname
, flags
, mode
);
918 busses
= usb_get_busses();
924 return (*func
) (pathname
, flags
, mode
);
928 static int (*func
) (int) = NULL
;
931 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
933 if (fd
== windrvrfd
&& windrvrfd
>= 0) {
934 DPRINTF("close windrvrfd\n");
935 if (usbinterface
>= 0)
936 usb_release_interface(usb_devhandle
, usbinterface
);
939 usb_close(usb_devhandle
);
941 usb_devhandle
= NULL
;
949 FILE *fopen(const char *path
, const char *mode
) {
951 static FILE* (*func
) (const char*, const char*) = NULL
;
956 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
958 ret
= (*func
) (path
, mode
);
960 if (!strcmp(path
, "/proc/modules")) {
961 DPRINTF("opening /proc/modules\n");
969 for (i
= 0; i
< 4; i
++) {
970 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
971 if (!strcmp(path
, buf
)) {
972 DPRINTF("open base-addr of parport%d\n", i
);
982 char *fgets(char *s
, int size
, FILE *stream
) {
983 static char* (*func
) (char*, int, FILE*) = NULL
;
984 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
990 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
992 if (modulesfp
== stream
) {
993 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
994 strcpy(s
, modules
[modules_read
]);
998 } else if (baseaddrfp
== stream
) {
999 snprintf(s
, sizeof(buf
), "%d\t%d\n",
1000 (baseaddrnum
) * 0x10,
1001 ((baseaddrnum
) * 0x10) + 0x400);
1004 ret
= (*func
)(s
,size
,stream
);
1010 int fclose(FILE *fp
) {
1011 static int (*func
) (FILE*) = NULL
;
1014 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
1016 if (fp
== modulesfp
) {
1020 if (fp
== baseaddrfp
) {
1027 int access(const char *pathname
, int mode
) {
1028 static int (*func
) (const char*, int);
1031 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
1033 if (!strcmp(pathname
, "/dev/windrvr6")) {
1036 return (*func
)(pathname
, mode
);