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