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