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