]> git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
a6d64ce824c9c8136155da5fab61dc5d4d4f1709
[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 "usb-driver.h"
41
42 static int (*ioctl_func) (int, int, void *) = NULL;
43 static int windrvrfd = -1;
44 FILE *modulesfp = NULL;
45 static int modules_read = 0;
46 static struct usb_bus *busses = NULL;
47 static struct usb_device *usbdevice;
48 static usb_dev_handle *usb_devhandle = NULL;
49 static unsigned long card_type;
50 static int ints_enabled = 0;
51 static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
52
53 #define NO_WINDRVR 1
54 #undef DEBUG
55
56 #ifdef DEBUG
57 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
58 void hexdump(unsigned char *buf, int len) {
59 int i;
60
61 for(i=0; i<len; i++) {
62 fprintf(stderr,"%02x ", buf[i]);
63 if ((i % 16) == 15)
64 fprintf(stderr,"\n");
65 }
66 fprintf(stderr,"\n");
67 }
68 #else
69 #define DPRINTF(format, args...)
70 #endif
71
72 int usb_deviceinfo(unsigned char *buf) {
73 int i,j,k,l;
74 int len = 0;
75 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
76 WDU_INTERFACE **pActiveInterface;
77
78 if (buf) {
79 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
80
81 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
82 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
83 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
84 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
85 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
86 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
87 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
88 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
89 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
90 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
91 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
92 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
93 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
94 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
95
96 /* TODO: Fix Pipe0! */
97 udi->Pipe0.dwNumber = 0x00;
98 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
99 udi->Pipe0.type = 0;
100 udi->Pipe0.direction = WDU_DIR_IN_OUT;
101 udi->Pipe0.dwInterval = 0;
102
103 pConfigs = &(udi->pConfigs);
104 pActiveConfig = &(udi->pActiveConfig);
105 pActiveInterface = &(udi->pActiveInterface[0]);
106 }
107
108 len = sizeof(struct usb_device_info);
109
110 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
111 {
112 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
113 WDU_INTERFACE **pInterfaces;
114 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
115 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
116
117 if (buf) {
118 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
119
120 *pConfigs = cfg;
121 *pActiveConfig = cfg;
122
123 cfg->Descriptor.bLength = conf_desc->bLength;
124 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
125 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
126 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
127 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
128 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
129 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
130 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
131
132 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
133
134 pInterfaces = &(cfg->pInterfaces);
135 }
136 len += sizeof(WDU_CONFIGURATION);
137
138 if (buf) {
139 *pInterfaces = (WDU_INTERFACE*)(buf+len);
140 for (j=0; j<conf_desc->bNumInterfaces; j++) {
141 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
142
143 pActiveInterface[j] = iface;
144
145 pAlternateSettings[j] = &(iface->pAlternateSettings);
146 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
147 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
148
149 len += sizeof(WDU_INTERFACE);
150 }
151 } else {
152 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
153 }
154
155 for (j=0; j<conf_desc->bNumInterfaces; j++)
156 {
157 struct usb_interface *interface = &usbdevice->config[i].interface[j];
158
159 if (buf) {
160 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
161 /* FIXME: */
162 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
163 }
164
165 for(k=0; k<interface->num_altsetting; k++)
166 {
167 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
168 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
169 WDU_PIPE_INFO **pPipes;
170
171 if (buf) {
172 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
173
174 altset->Descriptor.bLength = interface->altsetting[k].bLength;
175 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
176 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
177 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
178 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
179 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
180 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
181 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
182 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
183 pEndpointDescriptors = &(altset->pEndpointDescriptors);
184 pPipes = &(altset->pPipes);
185
186 }
187 len +=sizeof(WDU_ALTERNATE_SETTING);
188
189 if (buf) {
190 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
191 for (l = 0; l < bNumEndpoints; l++) {
192 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
193
194 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
195 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
196 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
197 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
198 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
199 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
200
201 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
202 }
203
204 *pPipes = (WDU_PIPE_INFO*)(buf+len);
205 for (l = 0; l < bNumEndpoints; l++) {
206 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
207
208 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
209 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
210 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
211 if (pi->type == PIPE_TYPE_CONTROL)
212 pi->direction = WDU_DIR_IN_OUT;
213 else
214 {
215 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
216 }
217
218 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
219
220 len += sizeof(WDU_PIPE_INFO);
221 }
222 } else {
223 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
224 }
225 }
226 }
227 }
228
229 return len;
230 }
231
232 int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
233 struct header_struct* wdheader = (struct header_struct*)wdioctl;
234 struct version_struct *version;
235 int ret = 0;
236
237 if (wdheader->magic != MAGIC) {
238 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
239 return (*ioctl_func) (fd, request, wdioctl);
240 }
241
242 switch(request) {
243 case VERSION:
244 version = (struct version_struct*)(wdheader->data);
245 strcpy(version->version, "WinDriver no more");
246 version->versionul = 802;
247 DPRINTF("VERSION\n");
248 break;
249
250 case LICENSE:
251 DPRINTF("LICENSE\n");
252 break;
253
254 case CARD_REGISTER:
255 /* TODO: Implement for LPT-support */
256 #if 0
257 {
258 struct card_register* cr = (struct card_register*)(wdheader->data);
259 }
260 #endif
261 DPRINTF("CARD_REGISTER\n");
262 break;
263
264 case USB_TRANSFER:
265 DPRINTF("in USB_TRANSFER");
266 {
267 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
268
269 #ifdef DEBUG
270 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
271 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
272 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
273 DPRINTF("setup packet: ");
274 hexdump(ut->SetupPacket, 8);
275
276 if (!ut->fRead && ut->dwBufferSize)
277 {
278 hexdump(ut->pBuffer, ut->dwBufferSize);
279 }
280 #endif
281
282 #ifndef NO_WINDRVR
283 ret = (*ioctl_func) (fd, request, wdioctl);
284 #else
285 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
286 if (ut->dwPipeNum == 0) { /* control pipe */
287 int requesttype, request, value, index, size;
288 requesttype = ut->SetupPacket[0];
289 request = ut->SetupPacket[1];
290 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
291 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
292 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
293 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
294 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
295 } else {
296 if (ut->fRead) {
297 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
298
299 } else {
300 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
301 }
302 }
303
304 if (ret < 0) {
305 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
306 } else {
307 ut->dwBytesTransferred = ret;
308 ret = 0;
309 }
310 #endif
311
312 #ifdef DEBUG
313 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
314 if (ut->fRead && ut->dwBytesTransferred)
315 {
316 DPRINTF("Read: ");
317 hexdump(ut->pBuffer, ut->dwBytesTransferred);
318 }
319 #endif
320 }
321 break;
322
323 case INT_ENABLE:
324 DPRINTF("INT_ENABLE\n");
325 {
326 struct interrupt *it = (struct interrupt*)(wdheader->data);
327
328 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
329 it->hInterrupt, it->dwOptions,
330 it->dwCmds, it->fEnableOk, it->dwCounter,
331 it->dwLost, it->fStopped);
332
333 it->fEnableOk = 1;
334 it->fStopped = 0;
335 ints_enabled = 1;
336 pthread_mutex_trylock(&int_wait);
337 }
338
339 break;
340
341 case INT_DISABLE:
342 DPRINTF("INT_DISABLE\n");
343 {
344 struct interrupt *it = (struct interrupt*)(wdheader->data);
345
346 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
347 it->hInterrupt, it->dwOptions,
348 it->dwCmds, it->fEnableOk, it->dwCounter,
349 it->dwLost, it->fStopped);
350 #ifndef NO_WINDRVR
351 ret = (*ioctl_func) (fd, request, wdioctl);
352 #else
353 it->dwCounter = 0;
354 it->fStopped = 1;
355 ints_enabled = 0;
356 if (pthread_mutex_trylock(&int_wait) == EBUSY)
357 pthread_mutex_unlock(&int_wait);
358 #endif
359 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
360 it->hInterrupt, it->dwOptions,
361 it->dwCmds, it->fEnableOk, it->dwCounter,
362 it->dwLost, it->fStopped);
363 }
364 break;
365
366 case USB_SET_INTERFACE:
367 DPRINTF("USB_SET_INTERFACE\n");
368 {
369 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
370
371 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
372 usi->dwUniqueID, usi->dwInterfaceNum,
373 usi->dwAlternateSetting, usi->dwOptions);
374 #ifndef NO_WINDRVR
375 ret = (*ioctl_func) (fd, request, wdioctl);
376 #else
377 if (usbdevice) {
378 if (!usb_devhandle)
379 usb_devhandle = usb_open(usbdevice);
380
381 /* FIXME: Select right interface! */
382 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
383 if (!ret) {
384 if(!ret) {
385 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
386 if (ret)
387 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
388 } else {
389 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
390 }
391 } else {
392 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
393 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
394 ret, usb_strerror());
395 }
396 }
397 #endif
398 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
399 usi->dwUniqueID, usi->dwInterfaceNum,
400 usi->dwAlternateSetting, usi->dwOptions);
401 }
402 break;
403
404 case USB_GET_DEVICE_DATA:
405 DPRINTF("USB_GET_DEVICE_DATA\n");
406 {
407 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
408 int pSize;
409
410 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
411 ugdd->dwUniqueID, ugdd->dwBytes,
412 ugdd->dwOptions);
413
414 pSize = ugdd->dwBytes;
415 if (!ugdd->dwBytes) {
416 if (usbdevice) {
417 ugdd->dwBytes = usb_deviceinfo(NULL);
418 }
419 } else {
420 usb_deviceinfo((unsigned char*)ugdd->pBuf);
421 }
422 }
423 break;
424
425 case EVENT_REGISTER:
426 DPRINTF("EVENT_REGISTER\n");
427 {
428 struct event *e = (struct event*)(wdheader->data);
429 struct usb_bus *bus;
430 int i;
431
432 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",
433 e->handle, e->dwAction,
434 e->dwStatus, e->dwEventId, e->dwCardType,
435 e->hKernelPlugIn, e->dwOptions,
436 e->u.Usb.deviceId.dwVendorId,
437 e->u.Usb.deviceId.dwProductId,
438 e->u.Usb.dwUniqueID, e->dwEventVer,
439 e->dwNumMatchTables);
440
441 for (i = 0; i < e->dwNumMatchTables; i++) {
442
443 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
444 e->matchTables[i].VendorId,
445 e->matchTables[i].ProductId,
446 e->matchTables[i].bDeviceClass,
447 e->matchTables[i].bDeviceSubClass,
448 e->matchTables[i].bInterfaceClass,
449 e->matchTables[i].bInterfaceSubClass,
450 e->matchTables[i].bInterfaceProtocol);
451
452 for (bus = busses; bus; bus = bus->next) {
453 struct usb_device *dev;
454
455 for (dev = bus->devices; dev; dev = dev->next) {
456 struct usb_device_descriptor *desc = &(dev->descriptor);
457
458 if((desc->idVendor == e->matchTables[i].VendorId) &&
459 (desc->idProduct == e->matchTables[i].ProductId) &&
460 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
461 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
462 int ac;
463 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
464 struct usb_interface *interface = dev->config[ac].interface;
465 int ai;
466
467 for (ai = 0; ai < interface->num_altsetting; ai++) {
468
469 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
470 interface->altsetting[i].bInterfaceClass,
471 interface->altsetting[i].bInterfaceSubClass,
472 interface->altsetting[i].bInterfaceProtocol);
473
474 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
475 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
476 /* TODO: check interfaceClass! */
477 DPRINTF("found device with libusb\n");
478 usbdevice = dev;
479 card_type = e->dwCardType;
480 }
481 }
482 }
483 }
484 }
485 }
486 }
487
488 #ifndef NO_WINDRVR
489 ret = (*ioctl_func) (fd, request, wdioctl);
490 #else
491 e->handle++;
492 #endif
493
494 #ifdef DEBUG
495 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",
496 e->handle, e->dwAction,
497 e->dwStatus, e->dwEventId, e->dwCardType,
498 e->hKernelPlugIn, e->dwOptions,
499 e->u.Usb.deviceId.dwVendorId,
500 e->u.Usb.deviceId.dwProductId,
501 e->u.Usb.dwUniqueID, e->dwEventVer,
502 e->dwNumMatchTables);
503
504 for (i = 0; i < e->dwNumMatchTables; i++)
505 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
506 e->matchTables[i].VendorId,
507 e->matchTables[i].ProductId,
508 e->matchTables[i].bDeviceClass,
509 e->matchTables[i].bDeviceSubClass,
510 e->matchTables[i].bInterfaceClass,
511 e->matchTables[i].bInterfaceSubClass,
512 e->matchTables[i].bInterfaceProtocol);
513 #endif
514 }
515 break;
516
517 case TRANSFER:
518 DPRINTF("TRANSFER\n");
519 #ifndef NO_WINDRVR
520 ret = (*ioctl_func) (fd, request, wdioctl);
521 #endif
522 break;
523
524 case EVENT_UNREGISTER:
525 DPRINTF("EVENT_UNREGISTER\n");
526 #ifndef NO_WINDRVR
527 ret = (*ioctl_func) (fd, request, wdioctl);
528 #endif
529 break;
530
531 case INT_WAIT:
532 DPRINTF("INT_WAIT\n");
533 {
534 struct interrupt *it = (struct interrupt*)(wdheader->data);
535
536 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
537 it->hInterrupt, it->dwOptions,
538 it->dwCmds, it->fEnableOk, it->dwCounter,
539 it->dwLost, it->fStopped);
540
541 #ifndef NO_WINDRVR
542 ret = (*ioctl_func) (fd, request, wdioctl);
543 #else
544 if (usbdevice) {
545 if (it->dwCounter == 0) {
546 it->dwCounter = 1;
547 } else {
548 pthread_mutex_lock(&int_wait);
549 pthread_mutex_unlock(&int_wait);
550 }
551 } else {
552 pthread_mutex_lock(&int_wait);
553 pthread_mutex_unlock(&int_wait);
554 }
555 #endif
556
557 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
558 it->hInterrupt, it->dwOptions, it->dwCmds,
559 it->fEnableOk, it->dwCounter, it->dwLost,
560 it->fStopped);
561 }
562 break;
563
564 case CARD_UNREGISTER:
565 DPRINTF("CARD_UNREGISTER\n");
566 #ifndef NO_WINDRVR
567 ret = (*ioctl_func) (fd, request, wdioctl);
568 #endif
569 break;
570
571 case EVENT_PULL:
572 DPRINTF("EVENT_PULL\n");
573 {
574 struct event *e = (struct event*)(wdheader->data);
575 #ifdef DEBUG
576 int i;
577
578 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",
579 e->handle, e->dwAction, e->dwStatus,
580 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
581 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
582 e->u.Usb.deviceId.dwProductId,
583 e->u.Usb.dwUniqueID, e->dwEventVer,
584 e->dwNumMatchTables);
585
586 for (i = 0; i < e->dwNumMatchTables; i++)
587 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
588 e->matchTables[i].VendorId,
589 e->matchTables[i].ProductId,
590 e->matchTables[i].bDeviceClass,
591 e->matchTables[i].bDeviceSubClass,
592 e->matchTables[i].bInterfaceClass,
593 e->matchTables[i].bInterfaceSubClass,
594 e->matchTables[i].bInterfaceProtocol);
595 #endif
596
597 #ifndef NO_WINDRVR
598 ret = (*ioctl_func) (fd, request, wdioctl);
599 #else
600 if (usbdevice) {
601 struct usb_interface *interface = usbdevice->config->interface;
602
603 e->dwCardType = card_type;
604 e->dwAction = 1;
605 e->dwEventId = 109;
606 e->u.Usb.dwUniqueID = 110;
607 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
608 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
609 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
610 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
611 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
612 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
613 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
614 }
615 #endif
616
617 #ifdef DEBUG
618 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",
619 e->handle, e->dwAction, e->dwStatus,
620 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
621 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
622 e->u.Usb.deviceId.dwProductId,
623 e->u.Usb.dwUniqueID, e->dwEventVer,
624 e->dwNumMatchTables);
625
626 for (i = 0; i < e->dwNumMatchTables; i++)
627 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
628 e->matchTables[i].VendorId,
629 e->matchTables[i].ProductId,
630 e->matchTables[i].bDeviceClass,
631 e->matchTables[i].bDeviceSubClass,
632 e->matchTables[i].bInterfaceClass,
633 e->matchTables[i].bInterfaceSubClass,
634 e->matchTables[i].bInterfaceProtocol);
635 #endif
636
637 }
638 break;
639
640 default:
641 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
642 #ifndef NO_WINDRVR
643 ret = (*ioctl_func) (fd, request, wdioctl);
644 #endif
645 break;
646 }
647
648 return ret;
649 }
650
651 int ioctl(int fd, int request, ...) {
652 va_list args;
653 void *argp;
654 int ret;
655
656 if (!ioctl_func)
657 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
658
659 va_start (args, request);
660 argp = va_arg (args, void *);
661 va_end (args);
662
663 if (fd == windrvrfd)
664 ret = do_wdioctl(fd, request, argp);
665 else
666 ret = (*ioctl_func) (fd, request, argp);
667
668 return ret;
669 }
670
671 int open (const char *pathname, int flags, ...) {
672 static int (*func) (const char *, int, mode_t) = NULL;
673 mode_t mode = 0;
674 va_list args;
675 int fd;
676
677 if (!func)
678 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
679
680 if (flags & O_CREAT) {
681 va_start(args, flags);
682 mode = va_arg(args, mode_t);
683 va_end(args);
684 }
685
686 if (!strcmp (pathname, "/dev/windrvr6")) {
687 DPRINTF("opening windrvr6\n");
688 #ifdef NO_WINDRVR
689 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
690 #else
691 windrvrfd = fd = (*func) (pathname, flags, mode);
692 #endif
693 if (!busses) {
694 usb_init();
695 usb_find_busses();
696 usb_find_devices();
697
698 busses = usb_get_busses();
699 }
700
701 return fd;
702 }
703
704 return (*func) (pathname, flags, mode);
705 }
706
707 int close(int fd) {
708 static int (*func) (int) = NULL;
709
710 if (!func)
711 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
712
713 if (fd == windrvrfd) {
714 DPRINTF("close windrvrfd\n");
715 windrvrfd = -1;
716 }
717
718 return (*func) (fd);
719 }
720
721 FILE *fopen(const char *path, const char *mode) {
722 FILE *ret;
723 static FILE* (*func) (const char*, const char*) = NULL;
724
725 if (!func)
726 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
727
728 ret = (*func) (path, mode);
729
730 if (!strcmp (path, "/proc/modules")) {
731 DPRINTF("opening /proc/modules\n");
732 #ifdef NO_WINDRVR
733 modulesfp = ret;
734 modules_read = 0;
735 #endif
736 }
737
738 return ret;
739 }
740
741 char *fgets(char *s, int size, FILE *stream) {
742 static char* (*func) (char*, int, FILE*) = NULL;
743 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
744 char *ret = NULL;
745
746
747 if (!func)
748 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
749
750 if (modulesfp == stream) {
751 if (modules_read < sizeof(modules)) {
752 strcpy(s, modules[modules_read]);
753 ret = s;
754 modules_read++;
755 }
756 } else {
757 ret = (*func)(s,size,stream);
758 }
759
760 return ret;
761 }
762
763 int fclose(FILE *fp) {
764 static int (*func) (FILE*) = NULL;
765
766 if (!func)
767 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
768
769 if (fp == modulesfp) {
770 modulesfp = NULL;
771 }
772
773 return (*func)(fp);
774 }
775
776 int access(const char *pathname, int mode) {
777 static int (*func) (const char*, int);
778
779 if (!func)
780 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
781
782 if (!strcmp(pathname, "/dev/windrvr6")) {
783 return 0;
784 } else {
785 return (*func)(pathname, mode);
786 }
787 }
Impressum, Datenschutz