]> cvs.zerfleddert.de Git - usb-driver/blob - usb-driver.c
parallel cable transfer cleanup
[usb-driver] / usb-driver.c
1 /* libusb/ppdev connector for XILINX impact
2 *
3 * Copyright (c) 2007 Michael Gernoth <michael@gernoth.net>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #define _GNU_SOURCE 1
25
26 #include <dlfcn.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <stdio.h>
36 #include <usb.h>
37 #include <signal.h>
38 #include <pthread.h>
39 #include <errno.h>
40 #include <inttypes.h>
41 #include <sys/ioctl.h>
42 #include <linux/parport.h>
43 #include <linux/ppdev.h>
44 #include "usb-driver.h"
45
46 static int (*ioctl_func) (int, int, void *) = NULL;
47 static int windrvrfd = -1;
48 static int parportfd = -1;
49 static int parportnum = 0;
50 static unsigned long ppbase = 0;
51 static unsigned long ecpbase = 0;
52 FILE *modulesfp = NULL;
53 static int modules_read = 0;
54 static struct usb_bus *busses = NULL;
55 static struct usb_device *usbdevice;
56 static usb_dev_handle *usb_devhandle = NULL;
57 static int usbinterface = -1;
58 static unsigned long card_type;
59 static int ints_enabled = 0;
60 static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
61
62 #define NO_WINDRVR 1
63
64 #ifdef DEBUG
65 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
66 void hexdump(unsigned char *buf, int len) {
67 int i;
68
69 for(i=0; i<len; i++) {
70 fprintf(stderr,"%02x ", buf[i]);
71 if ((i % 16) == 15)
72 fprintf(stderr,"\n");
73 }
74 fprintf(stderr,"\n");
75 }
76 #else
77 #define DPRINTF(format, args...)
78 #endif
79
80 int usb_deviceinfo(unsigned char *buf) {
81 int i,j,k,l;
82 int len = 0;
83 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
84 WDU_INTERFACE **pActiveInterface;
85
86 if (buf) {
87 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
88
89 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
90 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
91 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
92 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
93 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
94 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
95 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
96 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
97 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
98 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
99 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
100 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
101 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
102 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
103
104 /* TODO: Fix Pipe0! */
105 udi->Pipe0.dwNumber = 0x00;
106 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
107 udi->Pipe0.type = 0;
108 udi->Pipe0.direction = WDU_DIR_IN_OUT;
109 udi->Pipe0.dwInterval = 0;
110
111 pConfigs = &(udi->pConfigs);
112 pActiveConfig = &(udi->pActiveConfig);
113 pActiveInterface = &(udi->pActiveInterface[0]);
114 }
115
116 len = sizeof(struct usb_device_info);
117
118 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
119 {
120 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
121 WDU_INTERFACE **pInterfaces;
122 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
123 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
124
125 if (buf) {
126 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
127
128 *pConfigs = cfg;
129 *pActiveConfig = cfg;
130
131 cfg->Descriptor.bLength = conf_desc->bLength;
132 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
133 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
134 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
135 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
136 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
137 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
138 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
139
140 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
141
142 pInterfaces = &(cfg->pInterfaces);
143 }
144 len += sizeof(WDU_CONFIGURATION);
145
146 if (buf) {
147 *pInterfaces = (WDU_INTERFACE*)(buf+len);
148 for (j=0; j<conf_desc->bNumInterfaces; j++) {
149 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
150
151 pActiveInterface[j] = iface;
152
153 pAlternateSettings[j] = &(iface->pAlternateSettings);
154 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
155 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
156
157 len += sizeof(WDU_INTERFACE);
158 }
159 } else {
160 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
161 }
162
163 for (j=0; j<conf_desc->bNumInterfaces; j++)
164 {
165 struct usb_interface *interface = &usbdevice->config[i].interface[j];
166
167 if (buf) {
168 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
169 /* FIXME: */
170 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
171 }
172
173 for(k=0; k<interface->num_altsetting; k++)
174 {
175 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
176 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
177 WDU_PIPE_INFO **pPipes;
178
179 if (buf) {
180 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
181
182 altset->Descriptor.bLength = interface->altsetting[k].bLength;
183 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
184 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
185 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
186 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
187 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
188 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
189 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
190 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
191 pEndpointDescriptors = &(altset->pEndpointDescriptors);
192 pPipes = &(altset->pPipes);
193
194 }
195 len +=sizeof(WDU_ALTERNATE_SETTING);
196
197 if (buf) {
198 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
199 for (l = 0; l < bNumEndpoints; l++) {
200 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
201
202 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
203 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
204 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
205 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
206 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
207 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
208
209 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
210 }
211
212 *pPipes = (WDU_PIPE_INFO*)(buf+len);
213 for (l = 0; l < bNumEndpoints; l++) {
214 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
215
216 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
217 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
218 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
219 if (pi->type == PIPE_TYPE_CONTROL)
220 pi->direction = WDU_DIR_IN_OUT;
221 else
222 {
223 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
224 }
225
226 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
227
228 len += sizeof(WDU_PIPE_INFO);
229 }
230 } else {
231 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
232 }
233 }
234 }
235 }
236
237 return len;
238 }
239
240 int pp_transfer(WD_TRANSFER *tr, int fd, unsigned int request, unsigned char *wdioctl) {
241 int ret = 0;
242 unsigned long port = (unsigned long)tr->dwPort;
243 unsigned char val;
244
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);
248
249 val = tr->Data.Byte;
250
251 #ifdef DEBUG
252 if (tr->cmdTrans == 13)
253 DPRINTF("write byte: %d\n", val);
254 #endif
255
256 #ifndef NO_WINDRVR
257 ret = (*ioctl_func) (fd, request, wdioctl);
258 #else
259 if (parportfd < 0)
260 return ret;
261
262 if (port == ppbase + PP_DATA) {
263 DPRINTF("data port\n");
264 switch(tr->cmdTrans) {
265 case PP_READ:
266 ret = 0; /* We don't support reading of the data port */
267 break;
268
269 case PP_WRITE:
270 ret = ioctl(parportfd, PPWDATA, &val);
271 break;
272
273 default:
274 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
275 ret = -1;
276 break;
277 }
278 } else if (port == ppbase + PP_STATUS) {
279 DPRINTF("status port\n");
280 switch(tr->cmdTrans) {
281 case PP_READ:
282 ret = ioctl(parportfd, PPRSTATUS, &val);
283 break;
284
285 case PP_WRITE:
286 ret = 0; /* Status Port is readonly */
287 break;
288
289 default:
290 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
291 ret = -1;
292 break;
293 }
294 } else if (port == ppbase + PP_CONTROL) {
295 DPRINTF("control port\n");
296 switch(tr->cmdTrans) {
297 case PP_READ:
298 ret = ioctl(parportfd, PPRCONTROL, &val);
299 break;
300
301 case PP_WRITE:
302 ret = ioctl(parportfd, PPWCONTROL, &val);
303 break;
304
305 default:
306 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
307 ret = -1;
308 break;
309 }
310 } else {
311 DPRINTF("access to unsupported address range (probably ECP)!\n");
312 ret = 0;
313 }
314
315 tr->Data.Byte = val;
316 #endif
317
318 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
319 (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
320 tr->fAutoinc, tr->dwOptions);
321 #ifdef DEBUG
322 if (tr->cmdTrans == 10)
323 DPRINTF("read byte: %d\n", tr->Data.Byte);
324 #endif
325
326 return ret;
327 }
328
329 int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
330 struct header_struct* wdheader = (struct header_struct*)wdioctl;
331 struct version_struct *version;
332 int ret = 0;
333
334 if (wdheader->magic != MAGIC) {
335 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
336 return (*ioctl_func) (fd, request, wdioctl);
337 }
338
339 switch(request & ~(0xc0000000)) {
340 case VERSION:
341 version = (struct version_struct*)(wdheader->data);
342 strcpy(version->version, "WinDriver no more");
343 version->versionul = 802;
344 DPRINTF("VERSION\n");
345 break;
346
347 case LICENSE:
348 DPRINTF("LICENSE\n");
349 break;
350
351 case CARD_REGISTER_OLD:
352 case CARD_REGISTER:
353 DPRINTF("CARD_REGISTER\n");
354 {
355 struct card_register* cr = (struct card_register*)(wdheader->data);
356 char ppdev[32];
357
358 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
359 cr->Card.dwItems,
360 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
361 cr->Card.Item[0].I.IO.dwBytes,
362 cr->Card.Item[0].I.IO.dwBar);
363
364 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
365 cr->Card.dwItems,
366 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
367 cr->Card.Item[1].I.IO.dwBytes,
368 cr->Card.Item[1].I.IO.dwBar);
369 #ifndef NO_WINDRVR
370 ret = (*ioctl_func) (fd, request, wdioctl);
371 #else
372 if (parportfd < 0) {
373 snprintf(ppdev, sizeof(ppdev), "/dev/parport%d", parportnum);
374 DPRINTF("opening %s\n", ppdev);
375 parportfd = open(ppdev, O_RDWR|O_EXCL);
376 parportnum++;
377
378 if (parportfd < 0)
379 fprintf(stderr,"Can't open %s: %s\n", ppdev, strerror(errno));
380 }
381
382 if (parportfd >= 0) {
383 int pmode;
384
385 if (ioctl(parportfd, PPCLAIM) == -1)
386 return ret;
387
388 ecpbase = 0;
389 pmode = IEEE1284_MODE_COMPAT;
390 if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
391 return ret;
392
393 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwBytes) {
394 DPRINTF("ECP mode requested\n");
395 ecpbase = cr->Card.Item[1].I.IO.dwBytes;
396 /* TODO: Implement ECP mode */
397 #if 0
398 pmode = IEEE1284_MODE_ECP;
399
400 if (ioctl(parportfd, PPNEGOT, &pmode) == -1) {
401 ecpbase = 0;
402 pmode = IEEE1284_MODE_COMPAT;
403 if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
404 return ret;
405 }
406 #endif
407 }
408
409 cr->hCard = parportfd;
410 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
411 }
412 #endif
413 DPRINTF("hCard: %lu\n", cr->hCard);
414 }
415 break;
416
417 case USB_TRANSFER:
418 DPRINTF("in USB_TRANSFER");
419 {
420 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
421
422 #ifdef DEBUG
423 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
424 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
425 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
426 DPRINTF("setup packet: ");
427 hexdump(ut->SetupPacket, 8);
428
429 if (!ut->fRead && ut->dwBufferSize)
430 {
431 hexdump(ut->pBuffer, ut->dwBufferSize);
432 }
433 #endif
434
435 #ifndef NO_WINDRVR
436 ret = (*ioctl_func) (fd, request, wdioctl);
437 #else
438 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
439 if (ut->dwPipeNum == 0) { /* control pipe */
440 int requesttype, request, value, index, size;
441 requesttype = ut->SetupPacket[0];
442 request = ut->SetupPacket[1];
443 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
444 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
445 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
446 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
447 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
448 } else {
449 if (ut->fRead) {
450 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
451
452 } else {
453 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
454 }
455 }
456
457 if (ret < 0) {
458 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
459 } else {
460 ut->dwBytesTransferred = ret;
461 ret = 0;
462 }
463 #endif
464
465 #ifdef DEBUG
466 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
467 if (ut->fRead && ut->dwBytesTransferred)
468 {
469 DPRINTF("Read: ");
470 hexdump(ut->pBuffer, ut->dwBytesTransferred);
471 }
472 #endif
473 }
474 break;
475
476 case INT_ENABLE_OLD:
477 case INT_ENABLE:
478 DPRINTF("INT_ENABLE\n");
479 {
480 struct interrupt *it = (struct interrupt*)(wdheader->data);
481
482 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
483 it->hInterrupt, it->dwOptions,
484 it->dwCmds, it->fEnableOk, it->dwCounter,
485 it->dwLost, it->fStopped);
486
487 it->fEnableOk = 1;
488 it->fStopped = 0;
489 ints_enabled = 1;
490 pthread_mutex_trylock(&int_wait);
491 }
492
493 break;
494
495 case INT_DISABLE:
496 DPRINTF("INT_DISABLE\n");
497 {
498 struct interrupt *it = (struct interrupt*)(wdheader->data);
499
500 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
501 it->hInterrupt, it->dwOptions,
502 it->dwCmds, it->fEnableOk, it->dwCounter,
503 it->dwLost, it->fStopped);
504 #ifndef NO_WINDRVR
505 ret = (*ioctl_func) (fd, request, wdioctl);
506 #else
507 it->dwCounter = 0;
508 it->fStopped = 1;
509 ints_enabled = 0;
510 if (pthread_mutex_trylock(&int_wait) == EBUSY)
511 pthread_mutex_unlock(&int_wait);
512 #endif
513 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
514 it->hInterrupt, it->dwOptions,
515 it->dwCmds, it->fEnableOk, it->dwCounter,
516 it->dwLost, it->fStopped);
517 }
518 break;
519
520 case USB_SET_INTERFACE:
521 DPRINTF("USB_SET_INTERFACE\n");
522 {
523 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
524
525 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
526 usi->dwUniqueID, usi->dwInterfaceNum,
527 usi->dwAlternateSetting, usi->dwOptions);
528 #ifndef NO_WINDRVR
529 ret = (*ioctl_func) (fd, request, wdioctl);
530 #else
531 if (usbdevice) {
532 if (!usb_devhandle)
533 usb_devhandle = usb_open(usbdevice);
534
535 /* FIXME: Select right interface! */
536 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
537 if (!ret) {
538 if(!ret) {
539 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
540 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
541 if (ret)
542 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
543 } else {
544 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
545 }
546 } else {
547 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
548 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
549 ret, usb_strerror());
550 }
551 }
552 #endif
553 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
554 usi->dwUniqueID, usi->dwInterfaceNum,
555 usi->dwAlternateSetting, usi->dwOptions);
556 }
557 break;
558
559 case USB_GET_DEVICE_DATA_OLD:
560 case USB_GET_DEVICE_DATA:
561 DPRINTF("USB_GET_DEVICE_DATA\n");
562 {
563 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
564 int pSize;
565
566 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
567 ugdd->dwUniqueID, ugdd->dwBytes,
568 ugdd->dwOptions);
569
570 pSize = ugdd->dwBytes;
571 if (!ugdd->dwBytes) {
572 if (usbdevice) {
573 ugdd->dwBytes = usb_deviceinfo(NULL);
574 }
575 } else {
576 usb_deviceinfo((unsigned char*)ugdd->pBuf);
577 }
578 }
579 break;
580
581 case EVENT_REGISTER_OLD:
582 case EVENT_REGISTER:
583 DPRINTF("EVENT_REGISTER\n");
584 {
585 struct event *e = (struct event*)(wdheader->data);
586 struct usb_bus *bus;
587 int i;
588
589 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",
590 e->handle, e->dwAction,
591 e->dwStatus, e->dwEventId, e->dwCardType,
592 e->hKernelPlugIn, e->dwOptions,
593 e->u.Usb.deviceId.dwVendorId,
594 e->u.Usb.deviceId.dwProductId,
595 e->u.Usb.dwUniqueID, e->dwEventVer,
596 e->dwNumMatchTables);
597
598 for (i = 0; i < e->dwNumMatchTables; i++) {
599
600 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
601 e->matchTables[i].VendorId,
602 e->matchTables[i].ProductId,
603 e->matchTables[i].bDeviceClass,
604 e->matchTables[i].bDeviceSubClass,
605 e->matchTables[i].bInterfaceClass,
606 e->matchTables[i].bInterfaceSubClass,
607 e->matchTables[i].bInterfaceProtocol);
608
609 for (bus = busses; bus; bus = bus->next) {
610 struct usb_device *dev;
611
612 for (dev = bus->devices; dev; dev = dev->next) {
613 struct usb_device_descriptor *desc = &(dev->descriptor);
614
615 if((desc->idVendor == e->matchTables[i].VendorId) &&
616 (desc->idProduct == e->matchTables[i].ProductId) &&
617 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
618 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
619 int ac;
620 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
621 struct usb_interface *interface = dev->config[ac].interface;
622 int ai;
623
624 for (ai = 0; ai < interface->num_altsetting; ai++) {
625
626 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
627 interface->altsetting[i].bInterfaceClass,
628 interface->altsetting[i].bInterfaceSubClass,
629 interface->altsetting[i].bInterfaceProtocol);
630
631 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
632 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
633 /* TODO: check interfaceClass! */
634 DPRINTF("found device with libusb\n");
635 usbdevice = dev;
636 card_type = e->dwCardType;
637 }
638 }
639 }
640 }
641 }
642 }
643 }
644
645 #ifndef NO_WINDRVR
646 ret = (*ioctl_func) (fd, request, wdioctl);
647 #else
648 e->handle++;
649 #endif
650
651 #ifdef DEBUG
652 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",
653 e->handle, e->dwAction,
654 e->dwStatus, e->dwEventId, e->dwCardType,
655 e->hKernelPlugIn, e->dwOptions,
656 e->u.Usb.deviceId.dwVendorId,
657 e->u.Usb.deviceId.dwProductId,
658 e->u.Usb.dwUniqueID, e->dwEventVer,
659 e->dwNumMatchTables);
660
661 for (i = 0; i < e->dwNumMatchTables; i++)
662 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
663 e->matchTables[i].VendorId,
664 e->matchTables[i].ProductId,
665 e->matchTables[i].bDeviceClass,
666 e->matchTables[i].bDeviceSubClass,
667 e->matchTables[i].bInterfaceClass,
668 e->matchTables[i].bInterfaceSubClass,
669 e->matchTables[i].bInterfaceProtocol);
670 #endif
671 }
672 break;
673
674 case TRANSFER_OLD:
675 case TRANSFER:
676 DPRINTF("TRANSFER\n");
677 {
678 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
679
680 ret = pp_transfer(tr, fd, request, wdioctl);
681 }
682 break;
683
684 case MULTI_TRANSFER_OLD:
685 case MULTI_TRANSFER:
686 DPRINTF("MULTI_TRANSFER\n");
687 {
688 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
689 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
690 int i;
691
692
693 for (i = 0; i < num; i++) {
694 DPRINTF("Transfer %d:\n", i+1);
695 #ifndef NO_WINDRVR
696 wdheader->size = sizeof(WD_TRANSFER);
697 request = TRANSFER;
698 wdheader->data = tr + i;
699 #endif
700 ret = pp_transfer(tr + i, fd, request, wdioctl);
701 }
702
703 #ifndef NO_WINDRVR
704 wdheader->data = tr;
705 #endif
706
707 return ret;
708 }
709 break;
710
711 case EVENT_UNREGISTER:
712 DPRINTF("EVENT_UNREGISTER\n");
713 #ifndef NO_WINDRVR
714 ret = (*ioctl_func) (fd, request, wdioctl);
715 #endif
716 break;
717
718 case INT_WAIT:
719 DPRINTF("INT_WAIT\n");
720 {
721 struct interrupt *it = (struct interrupt*)(wdheader->data);
722
723 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
724 it->hInterrupt, it->dwOptions,
725 it->dwCmds, it->fEnableOk, it->dwCounter,
726 it->dwLost, it->fStopped);
727
728 #ifndef NO_WINDRVR
729 ret = (*ioctl_func) (fd, request, wdioctl);
730 #else
731 if (usbdevice) {
732 if (it->dwCounter == 0) {
733 it->dwCounter = 1;
734 } else {
735 pthread_mutex_lock(&int_wait);
736 pthread_mutex_unlock(&int_wait);
737 }
738 } else {
739 pthread_mutex_lock(&int_wait);
740 pthread_mutex_unlock(&int_wait);
741 }
742 #endif
743
744 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
745 it->hInterrupt, it->dwOptions, it->dwCmds,
746 it->fEnableOk, it->dwCounter, it->dwLost,
747 it->fStopped);
748 }
749 break;
750
751 case CARD_UNREGISTER:
752 DPRINTF("CARD_UNREGISTER\n");
753 {
754 struct card_register* cr = (struct card_register*)(wdheader->data);
755
756 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
757 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
758 cr->Card.Item[0].I.IO.dwBytes,
759 cr->Card.Item[0].I.IO.dwBar);
760
761 DPRINTF("hCard: %lu\n", cr->hCard);
762
763 #ifndef NO_WINDRVR
764 ret = (*ioctl_func) (fd, request, wdioctl);
765 #else
766 if (parportfd == cr->hCard && parportfd >= 0) {
767 ioctl(parportfd, PPRELEASE);
768 close(parportfd);
769 parportfd = -1;
770 parportnum--;
771 }
772 #endif
773 }
774 break;
775
776 case EVENT_PULL:
777 DPRINTF("EVENT_PULL\n");
778 {
779 struct event *e = (struct event*)(wdheader->data);
780 #ifdef DEBUG
781 int i;
782
783 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",
784 e->handle, e->dwAction, e->dwStatus,
785 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
786 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
787 e->u.Usb.deviceId.dwProductId,
788 e->u.Usb.dwUniqueID, e->dwEventVer,
789 e->dwNumMatchTables);
790
791 for (i = 0; i < e->dwNumMatchTables; i++)
792 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
793 e->matchTables[i].VendorId,
794 e->matchTables[i].ProductId,
795 e->matchTables[i].bDeviceClass,
796 e->matchTables[i].bDeviceSubClass,
797 e->matchTables[i].bInterfaceClass,
798 e->matchTables[i].bInterfaceSubClass,
799 e->matchTables[i].bInterfaceProtocol);
800 #endif
801
802 #ifndef NO_WINDRVR
803 ret = (*ioctl_func) (fd, request, wdioctl);
804 #else
805 if (usbdevice) {
806 struct usb_interface *interface = usbdevice->config->interface;
807
808 e->dwCardType = card_type;
809 e->dwAction = 1;
810 e->dwEventId = 109;
811 e->u.Usb.dwUniqueID = 110;
812 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
813 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
814 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
815 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
816 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
817 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
818 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
819 }
820 #endif
821
822 #ifdef DEBUG
823 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",
824 e->handle, e->dwAction, e->dwStatus,
825 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
826 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
827 e->u.Usb.deviceId.dwProductId,
828 e->u.Usb.dwUniqueID, e->dwEventVer,
829 e->dwNumMatchTables);
830
831 for (i = 0; i < e->dwNumMatchTables; i++)
832 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
833 e->matchTables[i].VendorId,
834 e->matchTables[i].ProductId,
835 e->matchTables[i].bDeviceClass,
836 e->matchTables[i].bDeviceSubClass,
837 e->matchTables[i].bInterfaceClass,
838 e->matchTables[i].bInterfaceSubClass,
839 e->matchTables[i].bInterfaceProtocol);
840 #endif
841
842 }
843 break;
844
845 default:
846 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
847 #ifndef NO_WINDRVR
848 ret = (*ioctl_func) (fd, request, wdioctl);
849 #endif
850 break;
851 }
852
853 return ret;
854 }
855
856 int ioctl(int fd, unsigned long int request, ...) {
857 va_list args;
858 void *argp;
859 int ret;
860
861 if (!ioctl_func)
862 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
863
864 va_start (args, request);
865 argp = va_arg (args, void *);
866 va_end (args);
867
868 if (fd == windrvrfd)
869 ret = do_wdioctl(fd, request, argp);
870 else
871 ret = (*ioctl_func) (fd, request, argp);
872
873 return ret;
874 }
875
876 int open (const char *pathname, int flags, ...) {
877 static int (*func) (const char *, int, mode_t) = NULL;
878 mode_t mode = 0;
879 va_list args;
880 int fd;
881
882 if (!func)
883 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
884
885 if (flags & O_CREAT) {
886 va_start(args, flags);
887 mode = va_arg(args, mode_t);
888 va_end(args);
889 }
890
891 if (!strcmp (pathname, "/dev/windrvr6")) {
892 DPRINTF("opening windrvr6\n");
893 #ifdef NO_WINDRVR
894 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
895 #else
896 windrvrfd = fd = (*func) (pathname, flags, mode);
897 #endif
898 if (!busses) {
899 usb_init();
900 usb_find_busses();
901 usb_find_devices();
902
903 busses = usb_get_busses();
904 }
905
906 return fd;
907 }
908
909 return (*func) (pathname, flags, mode);
910 }
911
912 int close(int fd) {
913 static int (*func) (int) = NULL;
914
915 if (!func)
916 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
917
918 if (fd == windrvrfd && windrvrfd >= 0) {
919 DPRINTF("close windrvrfd\n");
920 if (usbinterface >= 0)
921 usb_release_interface(usb_devhandle, usbinterface);
922
923 if (usb_devhandle)
924 usb_close(usb_devhandle);
925
926 usb_devhandle = NULL;
927 usbinterface = -1;
928 windrvrfd = -1;
929 }
930
931 return (*func) (fd);
932 }
933
934 FILE *fopen(const char *path, const char *mode) {
935 FILE *ret;
936 static FILE* (*func) (const char*, const char*) = NULL;
937
938 if (!func)
939 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
940
941 ret = (*func) (path, mode);
942
943 if (!strcmp (path, "/proc/modules")) {
944 DPRINTF("opening /proc/modules\n");
945 #ifdef NO_WINDRVR
946 modulesfp = ret;
947 modules_read = 0;
948 #endif
949 }
950
951 return ret;
952 }
953
954 char *fgets(char *s, int size, FILE *stream) {
955 static char* (*func) (char*, int, FILE*) = NULL;
956 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
957 char *ret = NULL;
958
959
960 if (!func)
961 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
962
963 if (modulesfp == stream) {
964 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
965 strcpy(s, modules[modules_read]);
966 ret = s;
967 modules_read++;
968 }
969 } else {
970 ret = (*func)(s,size,stream);
971 }
972
973 return ret;
974 }
975
976 int fclose(FILE *fp) {
977 static int (*func) (FILE*) = NULL;
978
979 if (!func)
980 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
981
982 if (fp == modulesfp) {
983 modulesfp = NULL;
984 }
985
986 return (*func)(fp);
987 }
988
989 int access(const char *pathname, int mode) {
990 static int (*func) (const char*, int);
991
992 if (!func)
993 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
994
995 if (!strcmp(pathname, "/dev/windrvr6")) {
996 return 0;
997 } else {
998 return (*func)(pathname, mode);
999 }
1000 }
Impressum, Datenschutz