]> git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
825c35cf3e4d4a4df33626d5b2fb84937db1411c
[usb-driver] / usb-driver.c
1 /* libusb 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 "usb-driver.h"
42
43 static int (*ioctl_func) (int, int, void *) = NULL;
44 static int windrvrfd = -1;
45 FILE *modulesfp = NULL;
46 static int modules_read = 0;
47 static struct usb_bus *busses = NULL;
48 static struct usb_device *usbdevice;
49 static usb_dev_handle *usb_devhandle = NULL;
50 static int usbinterface = -1;
51 static unsigned long card_type;
52 static int ints_enabled = 0;
53 static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
54
55 #define NO_WINDRVR 1
56 #undef PARPORT
57
58 #ifdef DEBUG
59 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
60 void hexdump(unsigned char *buf, int len) {
61 int i;
62
63 for(i=0; i<len; i++) {
64 fprintf(stderr,"%02x ", buf[i]);
65 if ((i % 16) == 15)
66 fprintf(stderr,"\n");
67 }
68 fprintf(stderr,"\n");
69 }
70 #else
71 #define DPRINTF(format, args...)
72 #endif
73
74 int usb_deviceinfo(unsigned char *buf) {
75 int i,j,k,l;
76 int len = 0;
77 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
78 WDU_INTERFACE **pActiveInterface;
79
80 if (buf) {
81 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
82
83 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
84 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
85 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
86 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
87 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
88 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
89 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
90 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
91 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
92 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
93 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
94 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
95 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
96 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
97
98 /* TODO: Fix Pipe0! */
99 udi->Pipe0.dwNumber = 0x00;
100 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
101 udi->Pipe0.type = 0;
102 udi->Pipe0.direction = WDU_DIR_IN_OUT;
103 udi->Pipe0.dwInterval = 0;
104
105 pConfigs = &(udi->pConfigs);
106 pActiveConfig = &(udi->pActiveConfig);
107 pActiveInterface = &(udi->pActiveInterface[0]);
108 }
109
110 len = sizeof(struct usb_device_info);
111
112 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
113 {
114 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
115 WDU_INTERFACE **pInterfaces;
116 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
117 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
118
119 if (buf) {
120 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
121
122 *pConfigs = cfg;
123 *pActiveConfig = cfg;
124
125 cfg->Descriptor.bLength = conf_desc->bLength;
126 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
127 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
128 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
129 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
130 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
131 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
132 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
133
134 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
135
136 pInterfaces = &(cfg->pInterfaces);
137 }
138 len += sizeof(WDU_CONFIGURATION);
139
140 if (buf) {
141 *pInterfaces = (WDU_INTERFACE*)(buf+len);
142 for (j=0; j<conf_desc->bNumInterfaces; j++) {
143 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
144
145 pActiveInterface[j] = iface;
146
147 pAlternateSettings[j] = &(iface->pAlternateSettings);
148 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
149 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
150
151 len += sizeof(WDU_INTERFACE);
152 }
153 } else {
154 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
155 }
156
157 for (j=0; j<conf_desc->bNumInterfaces; j++)
158 {
159 struct usb_interface *interface = &usbdevice->config[i].interface[j];
160
161 if (buf) {
162 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
163 /* FIXME: */
164 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
165 }
166
167 for(k=0; k<interface->num_altsetting; k++)
168 {
169 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
170 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
171 WDU_PIPE_INFO **pPipes;
172
173 if (buf) {
174 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
175
176 altset->Descriptor.bLength = interface->altsetting[k].bLength;
177 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
178 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
179 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
180 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
181 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
182 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
183 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
184 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
185 pEndpointDescriptors = &(altset->pEndpointDescriptors);
186 pPipes = &(altset->pPipes);
187
188 }
189 len +=sizeof(WDU_ALTERNATE_SETTING);
190
191 if (buf) {
192 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
193 for (l = 0; l < bNumEndpoints; l++) {
194 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
195
196 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
197 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
198 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
199 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
200 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
201 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
202
203 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
204 }
205
206 *pPipes = (WDU_PIPE_INFO*)(buf+len);
207 for (l = 0; l < bNumEndpoints; l++) {
208 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
209
210 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
211 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
212 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
213 if (pi->type == PIPE_TYPE_CONTROL)
214 pi->direction = WDU_DIR_IN_OUT;
215 else
216 {
217 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
218 }
219
220 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
221
222 len += sizeof(WDU_PIPE_INFO);
223 }
224 } else {
225 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
226 }
227 }
228 }
229 }
230
231 return len;
232 }
233
234 int pp_transfer(WD_TRANSFER *tr, int fd, unsigned int request, unsigned char *wdioctl) {
235 int ret = 0;
236
237 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
238 (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
239 tr->fAutoinc, tr->dwOptions);
240
241 #ifdef DEBUG
242 if (tr->cmdTrans == 13)
243 DPRINTF("write byte: %d\n", tr->Data.Byte);
244 #endif
245
246 #ifndef NO_WINDRVR
247 ret = (*ioctl_func) (fd, request, wdioctl);
248 #endif
249
250 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
251 (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
252 tr->fAutoinc, tr->dwOptions);
253 #ifdef DEBUG
254 if (tr->cmdTrans == 10)
255 DPRINTF("read byte: %d\n", tr->Data.Byte);
256 #endif
257
258 return ret;
259 }
260
261 int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
262 struct header_struct* wdheader = (struct header_struct*)wdioctl;
263 struct version_struct *version;
264 int ret = 0;
265
266 if (wdheader->magic != MAGIC) {
267 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
268 return (*ioctl_func) (fd, request, wdioctl);
269 }
270
271 switch(request & ~(0xc0000000)) {
272 case VERSION:
273 version = (struct version_struct*)(wdheader->data);
274 strcpy(version->version, "WinDriver no more");
275 version->versionul = 802;
276 DPRINTF("VERSION\n");
277 break;
278
279 case LICENSE:
280 DPRINTF("LICENSE\n");
281 break;
282
283 case CARD_REGISTER_OLD:
284 case CARD_REGISTER:
285 /* TODO: Implement for LPT-support */
286 #ifdef PARPORT
287 {
288 struct card_register* cr = (struct card_register*)(wdheader->data);
289 #ifndef NO_WINDRVR
290 ret = (*ioctl_func) (fd, request, wdioctl);
291 #else
292 /* TODO: Open /dev/parport, check, ... */
293 cr->hCard = 1;
294 #endif
295 DPRINTF("hCard: %lu\n", cr->hCard);
296 }
297 #endif
298 DPRINTF("CARD_REGISTER\n");
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 int i;
472
473 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",
474 e->handle, e->dwAction,
475 e->dwStatus, e->dwEventId, e->dwCardType,
476 e->hKernelPlugIn, e->dwOptions,
477 e->u.Usb.deviceId.dwVendorId,
478 e->u.Usb.deviceId.dwProductId,
479 e->u.Usb.dwUniqueID, e->dwEventVer,
480 e->dwNumMatchTables);
481
482 for (i = 0; i < e->dwNumMatchTables; i++) {
483
484 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
485 e->matchTables[i].VendorId,
486 e->matchTables[i].ProductId,
487 e->matchTables[i].bDeviceClass,
488 e->matchTables[i].bDeviceSubClass,
489 e->matchTables[i].bInterfaceClass,
490 e->matchTables[i].bInterfaceSubClass,
491 e->matchTables[i].bInterfaceProtocol);
492
493 for (bus = busses; bus; bus = bus->next) {
494 struct usb_device *dev;
495
496 for (dev = bus->devices; dev; dev = dev->next) {
497 struct usb_device_descriptor *desc = &(dev->descriptor);
498
499 if((desc->idVendor == e->matchTables[i].VendorId) &&
500 (desc->idProduct == e->matchTables[i].ProductId) &&
501 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
502 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
503 int ac;
504 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
505 struct usb_interface *interface = dev->config[ac].interface;
506 int ai;
507
508 for (ai = 0; ai < interface->num_altsetting; ai++) {
509
510 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
511 interface->altsetting[i].bInterfaceClass,
512 interface->altsetting[i].bInterfaceSubClass,
513 interface->altsetting[i].bInterfaceProtocol);
514
515 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
516 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
517 /* TODO: check interfaceClass! */
518 DPRINTF("found device with libusb\n");
519 usbdevice = dev;
520 card_type = e->dwCardType;
521 }
522 }
523 }
524 }
525 }
526 }
527 }
528
529 #ifndef NO_WINDRVR
530 ret = (*ioctl_func) (fd, request, wdioctl);
531 #else
532 e->handle++;
533 #endif
534
535 #ifdef DEBUG
536 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",
537 e->handle, e->dwAction,
538 e->dwStatus, e->dwEventId, e->dwCardType,
539 e->hKernelPlugIn, e->dwOptions,
540 e->u.Usb.deviceId.dwVendorId,
541 e->u.Usb.deviceId.dwProductId,
542 e->u.Usb.dwUniqueID, e->dwEventVer,
543 e->dwNumMatchTables);
544
545 for (i = 0; i < e->dwNumMatchTables; i++)
546 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
547 e->matchTables[i].VendorId,
548 e->matchTables[i].ProductId,
549 e->matchTables[i].bDeviceClass,
550 e->matchTables[i].bDeviceSubClass,
551 e->matchTables[i].bInterfaceClass,
552 e->matchTables[i].bInterfaceSubClass,
553 e->matchTables[i].bInterfaceProtocol);
554 #endif
555 }
556 break;
557
558 case TRANSFER_OLD:
559 case TRANSFER:
560 DPRINTF("TRANSFER\n");
561 {
562 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
563
564 ret = pp_transfer(tr, fd, request, wdioctl);
565 }
566 break;
567
568 case MULTI_TRANSFER:
569 DPRINTF("MULTI_TRANSFER\n");
570 {
571 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
572 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
573 int i;
574
575
576 for (i = 0; i < num; i++) {
577 DPRINTF("Transfer %d:\n", i+1);
578 #ifndef NO_WINDRVR
579 wdheader->size = sizeof(WD_TRANSFER);
580 request = TRANSFER;
581 wdheader->data = tr + i;
582 #endif
583 ret = pp_transfer(tr + i, fd, request, wdioctl);
584 }
585
586 #ifndef NO_WINDRVR
587 wdheader->data = tr;
588 #endif
589
590 return ret;
591 }
592 break;
593
594 case EVENT_UNREGISTER:
595 DPRINTF("EVENT_UNREGISTER\n");
596 #ifndef NO_WINDRVR
597 ret = (*ioctl_func) (fd, request, wdioctl);
598 #endif
599 break;
600
601 case INT_WAIT:
602 DPRINTF("INT_WAIT\n");
603 {
604 struct interrupt *it = (struct interrupt*)(wdheader->data);
605
606 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
607 it->hInterrupt, it->dwOptions,
608 it->dwCmds, it->fEnableOk, it->dwCounter,
609 it->dwLost, it->fStopped);
610
611 #ifndef NO_WINDRVR
612 ret = (*ioctl_func) (fd, request, wdioctl);
613 #else
614 if (usbdevice) {
615 if (it->dwCounter == 0) {
616 it->dwCounter = 1;
617 } else {
618 pthread_mutex_lock(&int_wait);
619 pthread_mutex_unlock(&int_wait);
620 }
621 } else {
622 pthread_mutex_lock(&int_wait);
623 pthread_mutex_unlock(&int_wait);
624 }
625 #endif
626
627 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
628 it->hInterrupt, it->dwOptions, it->dwCmds,
629 it->fEnableOk, it->dwCounter, it->dwLost,
630 it->fStopped);
631 }
632 break;
633
634 case CARD_UNREGISTER:
635 DPRINTF("CARD_UNREGISTER\n");
636 #ifndef NO_WINDRVR
637 ret = (*ioctl_func) (fd, request, wdioctl);
638 #endif
639 break;
640
641 case EVENT_PULL:
642 DPRINTF("EVENT_PULL\n");
643 {
644 struct event *e = (struct event*)(wdheader->data);
645 #ifdef DEBUG
646 int i;
647
648 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",
649 e->handle, e->dwAction, e->dwStatus,
650 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
651 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
652 e->u.Usb.deviceId.dwProductId,
653 e->u.Usb.dwUniqueID, e->dwEventVer,
654 e->dwNumMatchTables);
655
656 for (i = 0; i < e->dwNumMatchTables; i++)
657 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
658 e->matchTables[i].VendorId,
659 e->matchTables[i].ProductId,
660 e->matchTables[i].bDeviceClass,
661 e->matchTables[i].bDeviceSubClass,
662 e->matchTables[i].bInterfaceClass,
663 e->matchTables[i].bInterfaceSubClass,
664 e->matchTables[i].bInterfaceProtocol);
665 #endif
666
667 #ifndef NO_WINDRVR
668 ret = (*ioctl_func) (fd, request, wdioctl);
669 #else
670 if (usbdevice) {
671 struct usb_interface *interface = usbdevice->config->interface;
672
673 e->dwCardType = card_type;
674 e->dwAction = 1;
675 e->dwEventId = 109;
676 e->u.Usb.dwUniqueID = 110;
677 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
678 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
679 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
680 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
681 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
682 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
683 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
684 }
685 #endif
686
687 #ifdef DEBUG
688 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",
689 e->handle, e->dwAction, e->dwStatus,
690 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
691 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
692 e->u.Usb.deviceId.dwProductId,
693 e->u.Usb.dwUniqueID, e->dwEventVer,
694 e->dwNumMatchTables);
695
696 for (i = 0; i < e->dwNumMatchTables; i++)
697 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
698 e->matchTables[i].VendorId,
699 e->matchTables[i].ProductId,
700 e->matchTables[i].bDeviceClass,
701 e->matchTables[i].bDeviceSubClass,
702 e->matchTables[i].bInterfaceClass,
703 e->matchTables[i].bInterfaceSubClass,
704 e->matchTables[i].bInterfaceProtocol);
705 #endif
706
707 }
708 break;
709
710 default:
711 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
712 #ifndef NO_WINDRVR
713 ret = (*ioctl_func) (fd, request, wdioctl);
714 #endif
715 break;
716 }
717
718 return ret;
719 }
720
721 int ioctl(int fd, int request, ...) {
722 va_list args;
723 void *argp;
724 int ret;
725
726 if (!ioctl_func)
727 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
728
729 va_start (args, request);
730 argp = va_arg (args, void *);
731 va_end (args);
732
733 if (fd == windrvrfd)
734 ret = do_wdioctl(fd, request, argp);
735 else
736 ret = (*ioctl_func) (fd, request, argp);
737
738 return ret;
739 }
740
741 int open (const char *pathname, int flags, ...) {
742 static int (*func) (const char *, int, mode_t) = NULL;
743 mode_t mode = 0;
744 va_list args;
745 int fd;
746
747 if (!func)
748 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
749
750 if (flags & O_CREAT) {
751 va_start(args, flags);
752 mode = va_arg(args, mode_t);
753 va_end(args);
754 }
755
756 if (!strcmp (pathname, "/dev/windrvr6")) {
757 DPRINTF("opening windrvr6\n");
758 #ifdef NO_WINDRVR
759 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
760 #else
761 windrvrfd = fd = (*func) (pathname, flags, mode);
762 #endif
763 if (!busses) {
764 usb_init();
765 usb_find_busses();
766 usb_find_devices();
767
768 busses = usb_get_busses();
769 }
770
771 return fd;
772 }
773
774 return (*func) (pathname, flags, mode);
775 }
776
777 int close(int fd) {
778 static int (*func) (int) = NULL;
779
780 if (!func)
781 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
782
783 if (fd == windrvrfd && windrvrfd >= 0) {
784 DPRINTF("close windrvrfd\n");
785 if (usbinterface >= 0)
786 usb_release_interface(usb_devhandle, usbinterface);
787
788 if (usb_devhandle)
789 usb_close(usb_devhandle);
790
791 usb_devhandle = NULL;
792 usbinterface = -1;
793 windrvrfd = -1;
794 }
795
796 return (*func) (fd);
797 }
798
799 FILE *fopen(const char *path, const char *mode) {
800 FILE *ret;
801 static FILE* (*func) (const char*, const char*) = NULL;
802
803 if (!func)
804 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
805
806 ret = (*func) (path, mode);
807
808 if (!strcmp (path, "/proc/modules")) {
809 DPRINTF("opening /proc/modules\n");
810 #ifdef NO_WINDRVR
811 modulesfp = ret;
812 modules_read = 0;
813 #endif
814 }
815
816 return ret;
817 }
818
819 char *fgets(char *s, int size, FILE *stream) {
820 static char* (*func) (char*, int, FILE*) = NULL;
821 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
822 char *ret = NULL;
823
824
825 if (!func)
826 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
827
828 if (modulesfp == stream) {
829 if (modules_read < sizeof(modules)) {
830 strcpy(s, modules[modules_read]);
831 ret = s;
832 modules_read++;
833 }
834 } else {
835 ret = (*func)(s,size,stream);
836 }
837
838 return ret;
839 }
840
841 int fclose(FILE *fp) {
842 static int (*func) (FILE*) = NULL;
843
844 if (!func)
845 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
846
847 if (fp == modulesfp) {
848 modulesfp = NULL;
849 }
850
851 return (*func)(fp);
852 }
853
854 int access(const char *pathname, int mode) {
855 static int (*func) (const char*, int);
856
857 if (!func)
858 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
859
860 if (!strcmp(pathname, "/dev/windrvr6")) {
861 return 0;
862 } else {
863 return (*func)(pathname, mode);
864 }
865 }
Impressum, Datenschutz