use a pthread mutex for interrupts
[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 "xilinx.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 void hexdump(unsigned char *buf, int len) {
41 int i;
42
43 for(i=0; i<len; i++) {
44 fprintf(stderr,"%02x ", buf[i]);
45 if ((i % 16) == 15)
46 fprintf(stderr,"\n");
47 }
48 }
49
50 int usb_deviceinfo(unsigned char *buf) {
51 int i,j,k,l;
52 int len = 0;
53 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
54 WDU_INTERFACE **pActiveInterface;
55
56 if (buf) {
57 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
58
59 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
60 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
61 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
62 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
63 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
64 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
65 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
66 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
67 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
68 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
69 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
70 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
71 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
72 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
73
74 /* TODO: Fix Pipe0! */
75 udi->Pipe0.dwNumber = 0x00;
76 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
77 udi->Pipe0.type = 0;
78 udi->Pipe0.direction = 3;
79 udi->Pipe0.dwInterval = 0;
80
81 pConfigs = &(udi->pConfigs);
82 pActiveConfig = &(udi->pActiveConfig);
83 pActiveInterface = &(udi->pActiveInterface[0]);
84 }
85
86 len = sizeof(struct usb_device_info);
87
88 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
89 {
90 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
91 WDU_INTERFACE **pInterfaces;
92 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
93 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
94
95 if (buf) {
96 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
97
98 *pConfigs = cfg;
99 *pActiveConfig = cfg;
100
101 cfg->Descriptor.bLength = conf_desc->bLength;
102 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
103 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
104 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
105 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
106 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
107 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
108 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
109
110 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
111
112 pInterfaces = &(cfg->pInterfaces);
113 }
114 len += sizeof(WDU_CONFIGURATION);
115
116 if (buf) {
117 *pInterfaces = (WDU_INTERFACE*)(buf+len);
118 for (j=0; j<conf_desc->bNumInterfaces; j++) {
119 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
120
121 pActiveInterface[j] = iface;
122
123 pAlternateSettings[j] = &(iface->pAlternateSettings);
124 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
125 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
126
127 len += sizeof(WDU_INTERFACE);
128 }
129 } else {
130 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
131 }
132
133 for (j=0; j<conf_desc->bNumInterfaces; j++)
134 {
135 struct usb_interface *interface = &usbdevice->config[i].interface[j];
136
137 if (buf) {
138 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
139 /* FIXME: */
140 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
141 }
142
143 for(k=0; k<interface->num_altsetting; k++)
144 {
145 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
146 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
147 WDU_PIPE_INFO **pPipes;
148
149 if (buf) {
150 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
151
152 altset->Descriptor.bLength = interface->altsetting[k].bLength;
153 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
154 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
155 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
156 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
157 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
158 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
159 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
160 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
161 pEndpointDescriptors = &(altset->pEndpointDescriptors);
162 pPipes = &(altset->pPipes);
163
164 }
165 len +=sizeof(WDU_ALTERNATE_SETTING);
166
167 if (buf) {
168 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
169 for (l = 0; l < bNumEndpoints; l++) {
170 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
171
172 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
173 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
174 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
175 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
176 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
177 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
178
179 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
180 }
181
182 *pPipes = (WDU_PIPE_INFO*)(buf+len);
183 for (l = 0; l < bNumEndpoints; l++) {
184 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
185
186 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
187 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
188 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
189 if (pi->type == PIPE_TYPE_CONTROL)
190 pi->direction = WDU_DIR_IN_OUT;
191 else
192 {
193 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
194 }
195
196 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
197
198 len += sizeof(WDU_PIPE_INFO);
199 }
200 } else {
201 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
202 }
203 }
204 }
205 }
206
207 return len;
208 }
209
210 int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
211 struct header_struct* wdheader = (struct header_struct*)wdioctl;
212 struct version_struct *version;
213 int ret = 0;
214
215 if (wdheader->magic != MAGIC) {
216 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
217 return (*ioctl_func) (fd, request, wdioctl);
218 }
219
220 switch(request) {
221 case VERSION:
222 version = (struct version_struct*)(wdheader->data);
223 strcpy(version->version, "WinDriver no more");
224 version->versionul = 999;
225 #ifdef DEBUG
226 fprintf(stderr,"faking VERSION\n");
227 #endif
228 break;
229
230 case LICENSE:
231 #ifdef DEBUG
232 fprintf(stderr,"faking LICENSE\n");
233 #endif
234 break;
235
236 case CARD_REGISTER:
237 /* TODO: Implement for LPT-support */
238 #if 0
239 {
240 struct card_register* cr = (struct card_register*)(wdheader->data);
241 }
242 #endif
243 #ifdef DEBUG
244 fprintf(stderr,"faking CARD_REGISTER\n");
245 #endif
246 break;
247
248 case USB_TRANSFER:
249 #ifdef DEBUG
250 fprintf(stderr,"in USB_TRANSFER");
251 #endif
252 {
253 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
254
255 #ifdef DEBUG
256 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);
257 fprintf(stderr,"setup packet: ");
258 hexdump(ut->SetupPacket, 8);
259 fprintf(stderr,"\n");
260
261 if (!ut->fRead && ut->dwBufferSize)
262 {
263 hexdump(ut->pBuffer, ut->dwBufferSize);
264 fprintf(stderr,"\n");
265 }
266 #endif
267
268 #ifndef NO_WINDRVR
269 ret = (*ioctl_func) (fd, request, wdioctl);
270 #else
271 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
272 if (ut->dwPipeNum == 0) { /* control pipe */
273 int requesttype, request, value, index, size;
274 requesttype = ut->SetupPacket[0];
275 request = ut->SetupPacket[1];
276 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
277 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
278 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
279 #ifdef DEBUG
280 fprintf(stderr, "requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
281 #endif
282 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
283 } else {
284 if (ut->fRead) {
285 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
286
287 } else {
288 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
289 }
290 }
291
292 if (ret < 0) {
293 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
294 } else {
295 ut->dwBytesTransferred = ret;
296 ret = 0;
297 }
298 #endif
299
300 #ifdef DEBUG
301 fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
302 if (ut->fRead && ut->dwBytesTransferred)
303 {
304 fprintf(stderr,"Read: ");
305 hexdump(ut->pBuffer, ut->dwBytesTransferred);
306 fprintf(stderr,"\n");
307 }
308 #endif
309 }
310 break;
311
312 case INT_ENABLE:
313 #ifdef DEBUG
314 fprintf(stderr,"INT_ENABLE\n");
315 #endif
316 {
317 struct interrupt *it = (struct interrupt*)(wdheader->data);
318
319 #ifdef DEBUG
320 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);
321 #endif
322
323 it->fEnableOk = 1;
324 ints_enabled = 1;
325 pthread_mutex_trylock(&int_wait);
326 }
327
328 break;
329
330 case INT_DISABLE:
331 #ifdef DEBUG
332 fprintf(stderr,"INT_DISABLE\n");
333 #endif
334 {
335 struct interrupt *it = (struct interrupt*)(wdheader->data);
336
337 #ifdef DEBUG
338 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);
339 #endif
340 #ifndef NO_WINDRVR
341 ret = (*ioctl_func) (fd, request, wdioctl);
342 #else
343 it->dwCounter = 0;
344 it->fStopped = 1;
345 ints_enabled = 0;
346 pthread_mutex_unlock(&int_wait);
347 #endif
348 #ifdef DEBUG
349 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);
350 #endif
351 }
352 break;
353
354 case USB_SET_INTERFACE:
355 #ifdef DEBUG
356 fprintf(stderr,"USB_SET_INTERFACE\n");
357 #endif
358 {
359 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
360
361 #ifdef DEBUG
362 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
363 #endif
364 #ifndef NO_WINDRVR
365 ret = (*ioctl_func) (fd, request, wdioctl);
366 #else
367 if (usbdevice) {
368 if (!usb_devhandle)
369 usb_devhandle = usb_open(usbdevice);
370
371 /* FIXME: Select right interface! */
372 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
373 if (!ret) {
374 if(!ret) {
375 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
376 if (ret)
377 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
378 } else {
379 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
380 }
381 } else {
382 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n", usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber, ret, usb_strerror());
383 }
384 }
385 #endif
386 #ifdef DEBUG
387 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
388 #endif
389 }
390 break;
391
392 case USB_GET_DEVICE_DATA:
393 #ifdef DEBUG
394 fprintf(stderr,"faking USB_GET_DEVICE_DATA\n");
395 #endif
396 {
397 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
398 int pSize;
399
400 #ifdef DEBUG
401 fprintf(stderr, "unique: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions);
402 #endif
403 pSize = ugdd->dwBytes;
404 if (!ugdd->dwBytes) {
405 if (usbdevice) {
406 ugdd->dwBytes = usb_deviceinfo(NULL);
407 }
408 } else {
409 usb_deviceinfo((unsigned char*)ugdd->pBuf);
410 }
411 }
412 break;
413
414 case EVENT_REGISTER:
415 #ifdef DEBUG
416 fprintf(stderr,"EVENT_REGISTER\n");
417 #endif
418 {
419 struct event *e = (struct event*)(wdheader->data);
420 struct usb_bus *bus;
421 int i;
422
423 #ifdef DEBUG
424 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);
425 #endif
426 for (i = 0; i < e->dwNumMatchTables; i++) {
427 #ifdef DEBUG
428 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);
429 #endif
430
431 for (bus = busses; bus; bus = bus->next) {
432 struct usb_device *dev;
433
434 for (dev = bus->devices; dev; dev = dev->next) {
435 struct usb_device_descriptor *desc = &(dev->descriptor);
436
437 if((desc->idVendor == e->matchTables[i].VendorId) &&
438 (desc->idProduct == e->matchTables[i].ProductId) &&
439 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
440 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
441 int ac;
442 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
443 struct usb_interface *interface = dev->config[ac].interface;
444 int ai;
445
446 for (ai = 0; ai < interface->num_altsetting; ai++) {
447 #ifdef DEBUG
448 fprintf(stderr, "intclass: %x, intsubclass: %x, intproto: %x\n", interface->altsetting[i].bInterfaceClass, interface->altsetting[i].bInterfaceSubClass, interface->altsetting[i].bInterfaceProtocol);
449 #endif
450 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
451 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
452 /* TODO: check interfaceClass! */
453 #ifdef DEBUG
454 fprintf(stderr,"!!!FOUND DEVICE WITH LIBUSB!!!\n");
455 #endif
456 usbdevice = dev;
457 card_type = e->dwCardType;
458 }
459 }
460 }
461 }
462 }
463 }
464 }
465
466 #ifndef NO_WINDRVR
467 ret = (*ioctl_func) (fd, request, wdioctl);
468 #else
469 e->handle++;
470 #endif
471
472 #ifdef DEBUG
473 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);
474 for (i = 0; i < e->dwNumMatchTables; i++)
475 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);
476 #endif
477 }
478 break;
479
480 case TRANSFER:
481 fprintf(stderr,"TRANSFER\n");
482 #ifndef NO_WINDRVR
483 ret = (*ioctl_func) (fd, request, wdioctl);
484 #endif
485 break;
486
487 case EVENT_UNREGISTER:
488 #ifdef DEBUG
489 fprintf(stderr,"EVENT_UNREGISTER\n");
490 #endif
491 #ifndef NO_WINDRVR
492 ret = (*ioctl_func) (fd, request, wdioctl);
493 #endif
494 break;
495
496 case INT_WAIT:
497 #ifdef DEBUG
498 fprintf(stderr,"INT_WAIT\n");
499 #endif
500 {
501 struct interrupt *it = (struct interrupt*)(wdheader->data);
502
503 #ifdef DEBUG
504 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);
505 #endif
506
507 #ifndef NO_WINDRVR
508 ret = (*ioctl_func) (fd, request, wdioctl);
509 #else
510 if (usbdevice) {
511 if (it->dwCounter == 0) {
512 it->dwCounter = 1;
513 } else {
514 pthread_mutex_lock(&int_wait);
515 }
516 } else {
517 pthread_mutex_lock(&int_wait);
518 }
519 #endif
520
521 #ifdef DEBUG
522 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);
523 #endif
524 }
525 break;
526
527 case CARD_UNREGISTER:
528 fprintf(stderr,"CARD_UNREGISTER\n");
529 #ifndef NO_WINDRVR
530 ret = (*ioctl_func) (fd, request, wdioctl);
531 #endif
532 break;
533
534 case EVENT_PULL:
535 #ifdef DEBUG
536 fprintf(stderr,"EVENT_PULL\n");
537 #endif
538 {
539 struct event *e = (struct event*)(wdheader->data);
540 #ifdef DEBUG
541 int i;
542
543 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);
544 for (i = 0; i < e->dwNumMatchTables; i++)
545 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);
546 #endif
547
548 #ifndef NO_WINDRVR
549 ret = (*ioctl_func) (fd, request, wdioctl);
550 #else
551 if (usbdevice) {
552 struct usb_interface *interface = usbdevice->config->interface;
553
554 e->dwCardType = card_type;
555 e->dwAction = 1;
556 e->dwEventId = 109;
557 e->u.Usb.dwUniqueID = 110;
558 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
559 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
560 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
561 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
562 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
563 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
564 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
565 }
566 #endif
567
568 #ifdef DEBUG
569 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);
570 for (i = 0; i < e->dwNumMatchTables; i++)
571 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);
572 #endif
573
574 }
575 break;
576
577 default:
578 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
579 #ifndef NO_WINDRVR
580 ret = (*ioctl_func) (fd, request, wdioctl);
581 #endif
582 break;
583 }
584
585 return ret;
586 }
587
588 int ioctl(int fd, int request, ...)
589 {
590 va_list args;
591 void *argp;
592 int ret;
593
594 if (!ioctl_func)
595 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
596
597 va_start (args, request);
598 argp = va_arg (args, void *);
599 va_end (args);
600
601 if (fd == windrvrfd)
602 ret = do_wdioctl(fd, request, argp);
603 else
604 ret = (*ioctl_func) (fd, request, argp);
605
606 return ret;
607 }
608
609 typedef int (*open_funcptr_t) (const char *, int, mode_t);
610
611 int open (const char *pathname, int flags, ...)
612 {
613 static open_funcptr_t func = NULL;
614 mode_t mode = 0;
615 va_list args;
616 int fd;
617
618 if (!func)
619 func = (open_funcptr_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