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