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