]> git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
1fcfae19d172872a5de2c89e87898cfbfbdcec54
[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 version: " USB_DRIVER_VERSION);
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 if (!pport)
280 break;
281
282 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
283
284 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
285
286 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
287 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
288
289 if (ret >= 0) {
290 cr->hCard = ret;
291 } else {
292 cr->hCard = 0;
293 }
294 #endif
295 DPRINTF("hCard: %lu\n", cr->hCard);
296 }
297 break;
298
299 case USB_TRANSFER:
300 DPRINTF("in USB_TRANSFER");
301 {
302 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
303
304 #ifdef DEBUG
305 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
306 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
307 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
308 DPRINTF("setup packet: ");
309 hexdump(ut->SetupPacket, 8);
310
311 if (!ut->fRead && ut->dwBufferSize)
312 {
313 hexdump(ut->pBuffer, ut->dwBufferSize);
314 }
315 #endif
316
317 #ifndef NO_WINDRVR
318 ret = (*ioctl_func) (fd, request, wdioctl);
319 #else
320 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
321 if (ut->dwPipeNum == 0) { /* control pipe */
322 int requesttype, request, value, index, size;
323 requesttype = ut->SetupPacket[0];
324 request = ut->SetupPacket[1];
325 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
326 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
327 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
328 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
329 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
330 } else {
331 if (ut->fRead) {
332 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
333
334 } else {
335 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
336 }
337 }
338
339 if (ret < 0) {
340 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
341 } else {
342 ut->dwBytesTransferred = ret;
343 ret = 0;
344 }
345 #endif
346
347 #ifdef DEBUG
348 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
349 if (ut->fRead && ut->dwBytesTransferred)
350 {
351 DPRINTF("Read: ");
352 hexdump(ut->pBuffer, ut->dwBytesTransferred);
353 }
354 #endif
355 }
356 break;
357
358 case INT_ENABLE_OLD:
359 case INT_ENABLE:
360 DPRINTF("INT_ENABLE\n");
361 {
362 struct interrupt *it = (struct interrupt*)(wdheader->data);
363
364 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
365 it->hInterrupt, it->dwOptions,
366 it->dwCmds, it->fEnableOk, it->dwCounter,
367 it->dwLost, it->fStopped);
368
369 it->fEnableOk = 1;
370 it->fStopped = 0;
371 ints_enabled = 1;
372 pthread_mutex_trylock(&int_wait);
373 }
374
375 break;
376
377 case INT_DISABLE:
378 DPRINTF("INT_DISABLE\n");
379 {
380 struct interrupt *it = (struct interrupt*)(wdheader->data);
381
382 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
383 it->hInterrupt, it->dwOptions,
384 it->dwCmds, it->fEnableOk, it->dwCounter,
385 it->dwLost, it->fStopped);
386 #ifndef NO_WINDRVR
387 ret = (*ioctl_func) (fd, request, wdioctl);
388 #else
389 it->dwCounter = 0;
390 it->fStopped = 1;
391 ints_enabled = 0;
392 if (pthread_mutex_trylock(&int_wait) == EBUSY)
393 pthread_mutex_unlock(&int_wait);
394 #endif
395 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
396 it->hInterrupt, it->dwOptions,
397 it->dwCmds, it->fEnableOk, it->dwCounter,
398 it->dwLost, it->fStopped);
399 }
400 break;
401
402 case USB_SET_INTERFACE:
403 DPRINTF("USB_SET_INTERFACE\n");
404 {
405 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
406
407 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
408 usi->dwUniqueID, usi->dwInterfaceNum,
409 usi->dwAlternateSetting, usi->dwOptions);
410 #ifndef NO_WINDRVR
411 ret = (*ioctl_func) (fd, request, wdioctl);
412 #else
413 if (usbdevice) {
414 if (!usb_devhandle)
415 usb_devhandle = usb_open(usbdevice);
416
417 /* FIXME: Select right interface! */
418 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
419 if (!ret) {
420 if(!ret) {
421 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
422 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
423 if (ret)
424 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
425 } else {
426 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
427 }
428 } else {
429 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
430 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
431 ret, usb_strerror());
432 }
433 }
434 #endif
435 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
436 usi->dwUniqueID, usi->dwInterfaceNum,
437 usi->dwAlternateSetting, usi->dwOptions);
438 }
439 break;
440
441 case USB_GET_DEVICE_DATA_OLD:
442 case USB_GET_DEVICE_DATA:
443 DPRINTF("USB_GET_DEVICE_DATA\n");
444 {
445 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
446 int pSize;
447
448 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
449 ugdd->dwUniqueID, ugdd->dwBytes,
450 ugdd->dwOptions);
451
452 pSize = ugdd->dwBytes;
453 if (!ugdd->dwBytes) {
454 if (usbdevice) {
455 ugdd->dwBytes = usb_deviceinfo(NULL);
456 }
457 } else {
458 usb_deviceinfo((unsigned char*)ugdd->pBuf);
459 }
460 }
461 break;
462
463 case EVENT_REGISTER_OLD:
464 case EVENT_REGISTER:
465 DPRINTF("EVENT_REGISTER\n");
466 {
467 struct event *e = (struct event*)(wdheader->data);
468 struct usb_bus *bus;
469 char* device_num;
470 char* remainder;
471 int devnum = -1;
472 int i;
473
474 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",
475 e->handle, e->dwAction,
476 e->dwStatus, e->dwEventId, e->dwCardType,
477 e->hKernelPlugIn, e->dwOptions,
478 e->u.Usb.deviceId.dwVendorId,
479 e->u.Usb.deviceId.dwProductId,
480 e->u.Usb.dwUniqueID, e->dwEventVer,
481 e->dwNumMatchTables);
482
483 device_num=getenv("XILINX_USB_DEVNUM");
484 if (device_num!=NULL) {
485 DPRINTF("XILINX_USB_BUS=%s\n",device_num);
486 devnum=strtol(device_num,&remainder,10);
487 if (device_num==remainder) /* no integer in env variable */
488 devnum=-1;
489 }
490
491
492 for (i = 0; i < e->dwNumMatchTables; i++) {
493
494 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
495 e->matchTables[i].VendorId,
496 e->matchTables[i].ProductId,
497 e->matchTables[i].bDeviceClass,
498 e->matchTables[i].bDeviceSubClass,
499 e->matchTables[i].bInterfaceClass,
500 e->matchTables[i].bInterfaceSubClass,
501 e->matchTables[i].bInterfaceProtocol);
502
503 for (bus = busses; bus; bus = bus->next) {
504 struct usb_device *dev;
505
506 for (dev = bus->devices; dev; dev = dev->next) {
507 struct usb_device_descriptor *desc = &(dev->descriptor);
508
509 if((desc->idVendor == e->matchTables[i].VendorId) &&
510 (desc->idProduct == e->matchTables[i].ProductId) &&
511 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
512 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
513 ((devnum == -1) || (dev->devnum == devnum)) ) {
514 int ac;
515 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
516 struct usb_interface *interface = dev->config[ac].interface;
517 int ai;
518
519 for (ai = 0; ai < interface->num_altsetting; ai++) {
520
521 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
522 interface->altsetting[i].bInterfaceClass,
523 interface->altsetting[i].bInterfaceSubClass,
524 interface->altsetting[i].bInterfaceProtocol);
525
526 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
527 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
528 /* TODO: check interfaceClass! */
529 DPRINTF("found device with libusb\n");
530 usbdevice = dev;
531 card_type = e->dwCardType;
532 }
533 }
534 }
535 }
536 }
537 }
538 }
539
540 #ifndef NO_WINDRVR
541 ret = (*ioctl_func) (fd, request, wdioctl);
542 #else
543 e->handle++;
544 #endif
545
546 #ifdef DEBUG
547 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",
548 e->handle, e->dwAction,
549 e->dwStatus, e->dwEventId, e->dwCardType,
550 e->hKernelPlugIn, e->dwOptions,
551 e->u.Usb.deviceId.dwVendorId,
552 e->u.Usb.deviceId.dwProductId,
553 e->u.Usb.dwUniqueID, e->dwEventVer,
554 e->dwNumMatchTables);
555
556 for (i = 0; i < e->dwNumMatchTables; i++)
557 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
558 e->matchTables[i].VendorId,
559 e->matchTables[i].ProductId,
560 e->matchTables[i].bDeviceClass,
561 e->matchTables[i].bDeviceSubClass,
562 e->matchTables[i].bInterfaceClass,
563 e->matchTables[i].bInterfaceSubClass,
564 e->matchTables[i].bInterfaceProtocol);
565 #endif
566 }
567 break;
568
569 case TRANSFER_OLD:
570 case TRANSFER:
571 DPRINTF("TRANSFER\n");
572 {
573 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
574
575 #ifndef NO_WINDRVR
576 ret = (*ioctl_func) (fd, request, wdioctl);
577 #else
578 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
579 #endif
580 }
581 break;
582
583 case MULTI_TRANSFER_OLD:
584 case MULTI_TRANSFER:
585 DPRINTF("MULTI_TRANSFER\n");
586 {
587 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
588 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
589 #ifndef NO_WINDRVR
590 ret = (*ioctl_func) (fd, request, wdioctl);
591 #else
592 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
593 #endif
594 }
595 break;
596
597 case EVENT_UNREGISTER:
598 DPRINTF("EVENT_UNREGISTER\n");
599 #ifndef NO_WINDRVR
600 ret = (*ioctl_func) (fd, request, wdioctl);
601 #endif
602 break;
603
604 case INT_WAIT:
605 DPRINTF("INT_WAIT\n");
606 {
607 struct interrupt *it = (struct interrupt*)(wdheader->data);
608
609 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
610 it->hInterrupt, it->dwOptions,
611 it->dwCmds, it->fEnableOk, it->dwCounter,
612 it->dwLost, it->fStopped);
613
614 #ifndef NO_WINDRVR
615 ret = (*ioctl_func) (fd, request, wdioctl);
616 #else
617 if (usbdevice) {
618 if (it->dwCounter == 0) {
619 it->dwCounter = 1;
620 } else {
621 pthread_mutex_lock(&int_wait);
622 pthread_mutex_unlock(&int_wait);
623 }
624 } else {
625 pthread_mutex_lock(&int_wait);
626 pthread_mutex_unlock(&int_wait);
627 }
628 #endif
629
630 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
631 it->hInterrupt, it->dwOptions, it->dwCmds,
632 it->fEnableOk, it->dwCounter, it->dwLost,
633 it->fStopped);
634 }
635 break;
636
637 case CARD_UNREGISTER:
638 DPRINTF("CARD_UNREGISTER\n");
639 {
640 struct card_register* cr = (struct card_register*)(wdheader->data);
641
642 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
643 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
644 cr->Card.Item[0].I.IO.dwBytes,
645 cr->Card.Item[0].I.IO.dwBar);
646
647 DPRINTF("hCard: %lu\n", cr->hCard);
648
649 #ifndef NO_WINDRVR
650 ret = (*ioctl_func) (fd, request, wdioctl);
651 #else
652 if (pport)
653 pport->close(cr->hCard);
654
655 pport = NULL;
656 #endif
657 }
658 break;
659
660 case EVENT_PULL:
661 DPRINTF("EVENT_PULL\n");
662 {
663 struct event *e = (struct event*)(wdheader->data);
664 #ifdef DEBUG
665 int i;
666
667 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",
668 e->handle, e->dwAction, e->dwStatus,
669 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
670 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
671 e->u.Usb.deviceId.dwProductId,
672 e->u.Usb.dwUniqueID, e->dwEventVer,
673 e->dwNumMatchTables);
674
675 for (i = 0; i < e->dwNumMatchTables; i++)
676 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
677 e->matchTables[i].VendorId,
678 e->matchTables[i].ProductId,
679 e->matchTables[i].bDeviceClass,
680 e->matchTables[i].bDeviceSubClass,
681 e->matchTables[i].bInterfaceClass,
682 e->matchTables[i].bInterfaceSubClass,
683 e->matchTables[i].bInterfaceProtocol);
684 #endif
685
686 #ifndef NO_WINDRVR
687 ret = (*ioctl_func) (fd, request, wdioctl);
688 #else
689 if (usbdevice) {
690 struct usb_interface *interface = usbdevice->config->interface;
691
692 e->dwCardType = card_type;
693 e->dwAction = 1;
694 e->dwEventId = 109;
695 e->u.Usb.dwUniqueID = 110;
696 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
697 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
698 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
699 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
700 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
701 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
702 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
703 }
704 #endif
705
706 #ifdef DEBUG
707 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",
708 e->handle, e->dwAction, e->dwStatus,
709 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
710 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
711 e->u.Usb.deviceId.dwProductId,
712 e->u.Usb.dwUniqueID, e->dwEventVer,
713 e->dwNumMatchTables);
714
715 for (i = 0; i < e->dwNumMatchTables; i++)
716 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
717 e->matchTables[i].VendorId,
718 e->matchTables[i].ProductId,
719 e->matchTables[i].bDeviceClass,
720 e->matchTables[i].bDeviceSubClass,
721 e->matchTables[i].bInterfaceClass,
722 e->matchTables[i].bInterfaceSubClass,
723 e->matchTables[i].bInterfaceProtocol);
724 #endif
725
726 }
727 break;
728
729 default:
730 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
731 #ifndef NO_WINDRVR
732 ret = (*ioctl_func) (fd, request, wdioctl);
733 #endif
734 break;
735 }
736
737 return ret;
738 }
739
740 int ioctl(int fd, unsigned long int request, ...) {
741 va_list args;
742 void *argp;
743 int ret;
744
745 if (!ioctl_func)
746 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
747
748 va_start (args, request);
749 argp = va_arg (args, void *);
750 va_end (args);
751
752 if (fd == windrvrfd)
753 ret = do_wdioctl(fd, request, argp);
754 else
755 ret = (*ioctl_func) (fd, request, argp);
756
757 return ret;
758 }
759
760 int open (const char *pathname, int flags, ...) {
761 static int (*func) (const char *, int, mode_t) = NULL;
762 mode_t mode = 0;
763 va_list args;
764 int fd;
765
766 if (!func)
767 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
768
769 if (flags & O_CREAT) {
770 va_start(args, flags);
771 mode = va_arg(args, mode_t);
772 va_end(args);
773 }
774
775 if (!strcmp (pathname, "/dev/windrvr6")) {
776 DPRINTF("opening windrvr6\n");
777 #ifdef NO_WINDRVR
778 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
779 #else
780 windrvrfd = fd = (*func) (pathname, flags, mode);
781 #endif
782 if (!busses) {
783 usb_init();
784 usb_find_busses();
785 usb_find_devices();
786
787 busses = usb_get_busses();
788 }
789
790 return fd;
791 }
792
793 return (*func) (pathname, flags, mode);
794 }
795
796 int close(int fd) {
797 static int (*func) (int) = NULL;
798
799 if (!func)
800 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
801
802 if (fd == windrvrfd && windrvrfd >= 0) {
803 DPRINTF("close windrvrfd\n");
804 if (usbinterface >= 0)
805 usb_release_interface(usb_devhandle, usbinterface);
806
807 if (usb_devhandle)
808 usb_close(usb_devhandle);
809
810 usb_devhandle = NULL;
811 usbinterface = -1;
812 windrvrfd = -1;
813 }
814
815 return (*func) (fd);
816 }
817
818 FILE *fopen(const char *path, const char *mode) {
819 FILE *ret;
820 static FILE* (*func) (const char*, const char*) = NULL;
821 char buf[256];
822 int i;
823
824 if (!func)
825 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
826
827 for (i = 0; i < 4; i++) {
828 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
829 if (!strcmp(path, buf)) {
830 DPRINTF("open base-addr of parport%d\n", i);
831 if (config_is_real_pport(i)) {
832 ret = (*func) (path, mode);
833 } else {
834 ret = (*func) ("/dev/null", mode);
835 }
836
837 if (ret) {
838 baseaddrfp = ret;
839 baseaddrnum = i;
840 }
841
842 return ret;
843 }
844 }
845
846 ret = (*func) (path, mode);
847
848 if (!strcmp(path, "/proc/modules")) {
849 DPRINTF("opening /proc/modules\n");
850 #ifdef NO_WINDRVR
851 modulesfp = ret;
852 modules_read = 0;
853 #endif
854 }
855
856 return ret;
857 }
858
859 char *fgets(char *s, int size, FILE *stream) {
860 static char* (*func) (char*, int, FILE*) = NULL;
861 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
862 char buf[256];
863 char *ret = NULL;
864
865
866 if (!func)
867 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
868
869 if (modulesfp == stream) {
870 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
871 strcpy(s, modules[modules_read]);
872 ret = s;
873 modules_read++;
874 }
875 } else if (baseaddrfp == stream) {
876 snprintf(s, sizeof(buf), "%d\t%d\n",
877 (baseaddrnum) * 0x10,
878 ((baseaddrnum) * 0x10) + 0x400);
879 ret = s;
880 } else {
881 ret = (*func)(s,size,stream);
882 }
883
884 return ret;
885 }
886
887 int fclose(FILE *fp) {
888 static int (*func) (FILE*) = NULL;
889
890 if (!func)
891 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
892
893 if (fp == modulesfp) {
894 modulesfp = NULL;
895 }
896
897 if (fp == baseaddrfp) {
898 baseaddrfp = NULL;
899 }
900
901 return (*func)(fp);
902 }
903
904 int access(const char *pathname, int mode) {
905 static int (*func) (const char*, int);
906
907 if (!func)
908 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
909
910 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
911 return 0;
912 } else {
913 return (*func)(pathname, mode);
914 }
915 }
Impressum, Datenschutz