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