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