]> git.zerfleddert.de Git - usb-driver/blob - usb-driver.c
d1d59303a148f5c6f6b5a5d6210b5c4ebf9e8547
[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 #ifdef JTAGKEY
46 #include "jtagkey.h"
47 #endif
48
49 static int (*ioctl_func) (int, int, void *) = NULL;
50 static int windrvrfd = -1;
51 static int parportfd = -1;
52 static unsigned long ppbase = 0;
53 static unsigned long ecpbase = 0;
54 FILE *modulesfp = NULL;
55 FILE *baseaddrfp = NULL;
56 int baseaddrnum = 0;
57 static int modules_read = 0;
58 static struct usb_bus *busses = NULL;
59 static struct usb_device *usbdevice;
60 static usb_dev_handle *usb_devhandle = NULL;
61 static int usbinterface = -1;
62 static unsigned long card_type;
63 static int ints_enabled = 0;
64 static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
65
66 #define NO_WINDRVR 1
67
68 #ifdef DEBUG
69 void hexdump(unsigned char *buf, int len) {
70 int i;
71
72 for(i=0; i<len; i++) {
73 fprintf(stderr,"%02x ", buf[i]);
74 if ((i % 16) == 15)
75 fprintf(stderr,"\n");
76 }
77 fprintf(stderr,"\n");
78 }
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 unsigned char last_pp_write = 0;
246
247 #ifdef JTAGKEY
248 /* FIXME: Config file and mor intelligent mapping! */
249 if (ppbase == 0x30)
250 return jtagkey_transfer(tr, fd, request, ppbase, ecpbase, 1);
251 #endif
252
253 DPRINTF("dwPort: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
254 (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
255 tr->fAutoinc, tr->dwOptions);
256
257 val = tr->Data.Byte;
258
259 #ifdef DEBUG
260 if (tr->cmdTrans == 13)
261 DPRINTF("write byte: %d\n", val);
262 #endif
263
264 #ifndef NO_WINDRVR
265 ret = (*ioctl_func) (fd, request, wdioctl);
266 #else
267 if (parportfd < 0)
268 return ret;
269
270 if (port == ppbase + PP_DATA) {
271 DPRINTF("data port\n");
272 switch(tr->cmdTrans) {
273 case PP_READ:
274 ret = 0; /* We don't support reading of the data port */
275 break;
276
277 case PP_WRITE:
278 ret = ioctl(parportfd, PPWDATA, &val);
279 last_pp_write = val;
280 break;
281
282 default:
283 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
284 ret = -1;
285 break;
286 }
287 } else if (port == ppbase + PP_STATUS) {
288 DPRINTF("status port (last write: %d)\n", last_pp_write);
289 switch(tr->cmdTrans) {
290 case PP_READ:
291 ret = ioctl(parportfd, PPRSTATUS, &val);
292 #ifdef FORCE_PC3_IDENT
293 val &= 0x5f;
294 if (last_pp_write & 0x40)
295 val |= 0x20;
296 else
297 val |= 0x80;
298 #endif
299 break;
300
301 case PP_WRITE:
302 ret = 0; /* Status Port is readonly */
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 == ppbase + PP_CONTROL) {
311 DPRINTF("control port\n");
312 switch(tr->cmdTrans) {
313 case PP_READ:
314 ret = ioctl(parportfd, PPRCONTROL, &val);
315 break;
316
317 case PP_WRITE:
318 ret = ioctl(parportfd, PPWCONTROL, &val);
319 break;
320
321 default:
322 fprintf(stderr,"!!!Unsupported TRANSFER command: %lu!!!\n", tr->cmdTrans);
323 ret = -1;
324 break;
325 }
326 } else if ((port == ecpbase + PP_ECP_CFGA) && ecpbase) {
327 DPRINTF("ECP_CFGA port\n");
328 } else if ((port == ecpbase + PP_ECP_CFGB) && ecpbase) {
329 DPRINTF("ECP_CFGB port\n");
330 } else if ((port == ecpbase + PP_ECP_ECR) && ecpbase) {
331 DPRINTF("ECP_ECR port\n");
332 } else {
333 DPRINTF("access to unsupported address range!\n");
334 ret = 0;
335 }
336
337 tr->Data.Byte = val;
338 #endif
339
340 DPRINTF("dwPortReturn: 0x%lx, cmdTrans: %lu, dwbytes: %ld, fautoinc: %ld, dwoptions: %ld\n",
341 (unsigned long)tr->dwPort, tr->cmdTrans, tr->dwBytes,
342 tr->fAutoinc, tr->dwOptions);
343 #ifdef DEBUG
344 if (tr->cmdTrans == 10)
345 DPRINTF("read byte: %d\n", tr->Data.Byte);
346 #endif
347
348 return ret;
349 }
350
351 int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
352 struct header_struct* wdheader = (struct header_struct*)wdioctl;
353 struct version_struct *version;
354 int ret = 0;
355
356 if (wdheader->magic != MAGIC) {
357 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
358 return (*ioctl_func) (fd, request, wdioctl);
359 }
360
361 switch(request & ~(0xc0000000)) {
362 case VERSION:
363 version = (struct version_struct*)(wdheader->data);
364 strcpy(version->version, "libusb-driver.so $Revision: 1.61 $");
365 version->versionul = 802;
366 DPRINTF("VERSION\n");
367 break;
368
369 case LICENSE:
370 DPRINTF("LICENSE\n");
371 break;
372
373 case CARD_REGISTER_OLD:
374 case CARD_REGISTER:
375 DPRINTF("CARD_REGISTER\n");
376 {
377 struct card_register* cr = (struct card_register*)(wdheader->data);
378 char ppdev[32];
379
380 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
381 cr->Card.dwItems,
382 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
383 cr->Card.Item[0].I.IO.dwBytes,
384 cr->Card.Item[0].I.IO.dwBar);
385
386 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
387 cr->Card.dwItems,
388 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
389 cr->Card.Item[1].I.IO.dwBytes,
390 cr->Card.Item[1].I.IO.dwBar);
391 #ifndef NO_WINDRVR
392 ret = (*ioctl_func) (fd, request, wdioctl);
393 #else
394
395 /* FIXME: Ugly hack which maps amontec JtagKey to 4. parallel port */
396 #ifdef JTAGKEY
397 if ((unsigned long)cr->Card.Item[0].I.IO.dwAddr != 0x30)
398 #endif
399 {
400 if (parportfd < 0) {
401 snprintf(ppdev, sizeof(ppdev), "/dev/parport%lu",
402 (unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
403 DPRINTF("opening %s\n", ppdev);
404 parportfd = open(ppdev, O_RDWR|O_EXCL);
405
406 if (parportfd < 0)
407 fprintf(stderr,"Can't open %s: %s\n", ppdev, strerror(errno));
408 }
409
410 if (parportfd >= 0) {
411 int pmode;
412
413 if (ioctl(parportfd, PPCLAIM) == -1)
414 return ret;
415
416 ecpbase = 0;
417 pmode = IEEE1284_MODE_COMPAT;
418 if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
419 return ret;
420
421 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr) {
422 DPRINTF("ECP mode requested\n");
423 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
424 /* TODO: Implement ECP mode */
425 #if 0
426 pmode = IEEE1284_MODE_ECP;
427
428 if (ioctl(parportfd, PPNEGOT, &pmode) == -1) {
429 ecpbase = 0;
430 pmode = IEEE1284_MODE_COMPAT;
431 if (ioctl(parportfd, PPNEGOT, &pmode) == -1)
432 return ret;
433 }
434 #endif
435 }
436
437 cr->hCard = parportfd;
438 }
439 #ifdef JTAGKEY
440 } else {
441 ret=jtagkey_init(0x0403, 0xcff8); /* I need a config file... */
442 cr->hCard = 0xff;
443 #endif
444 }
445
446 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
447 if (ret < 0)
448 cr->hCard = 0;
449 #endif
450 DPRINTF("hCard: %lu\n", cr->hCard);
451 }
452 break;
453
454 case USB_TRANSFER:
455 DPRINTF("in USB_TRANSFER");
456 {
457 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
458
459 #ifdef DEBUG
460 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
461 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
462 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
463 DPRINTF("setup packet: ");
464 hexdump(ut->SetupPacket, 8);
465
466 if (!ut->fRead && ut->dwBufferSize)
467 {
468 hexdump(ut->pBuffer, ut->dwBufferSize);
469 }
470 #endif
471
472 #ifndef NO_WINDRVR
473 ret = (*ioctl_func) (fd, request, wdioctl);
474 #else
475 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
476 if (ut->dwPipeNum == 0) { /* control pipe */
477 int requesttype, request, value, index, size;
478 requesttype = ut->SetupPacket[0];
479 request = ut->SetupPacket[1];
480 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
481 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
482 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
483 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
484 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
485 } else {
486 if (ut->fRead) {
487 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
488
489 } else {
490 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
491 }
492 }
493
494 if (ret < 0) {
495 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
496 } else {
497 ut->dwBytesTransferred = ret;
498 ret = 0;
499 }
500 #endif
501
502 #ifdef DEBUG
503 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
504 if (ut->fRead && ut->dwBytesTransferred)
505 {
506 DPRINTF("Read: ");
507 hexdump(ut->pBuffer, ut->dwBytesTransferred);
508 }
509 #endif
510 }
511 break;
512
513 case INT_ENABLE_OLD:
514 case INT_ENABLE:
515 DPRINTF("INT_ENABLE\n");
516 {
517 struct interrupt *it = (struct interrupt*)(wdheader->data);
518
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 it->fEnableOk = 1;
525 it->fStopped = 0;
526 ints_enabled = 1;
527 pthread_mutex_trylock(&int_wait);
528 }
529
530 break;
531
532 case INT_DISABLE:
533 DPRINTF("INT_DISABLE\n");
534 {
535 struct interrupt *it = (struct interrupt*)(wdheader->data);
536
537 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
538 it->hInterrupt, it->dwOptions,
539 it->dwCmds, it->fEnableOk, it->dwCounter,
540 it->dwLost, it->fStopped);
541 #ifndef NO_WINDRVR
542 ret = (*ioctl_func) (fd, request, wdioctl);
543 #else
544 it->dwCounter = 0;
545 it->fStopped = 1;
546 ints_enabled = 0;
547 if (pthread_mutex_trylock(&int_wait) == EBUSY)
548 pthread_mutex_unlock(&int_wait);
549 #endif
550 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
551 it->hInterrupt, it->dwOptions,
552 it->dwCmds, it->fEnableOk, it->dwCounter,
553 it->dwLost, it->fStopped);
554 }
555 break;
556
557 case USB_SET_INTERFACE:
558 DPRINTF("USB_SET_INTERFACE\n");
559 {
560 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
561
562 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
563 usi->dwUniqueID, usi->dwInterfaceNum,
564 usi->dwAlternateSetting, usi->dwOptions);
565 #ifndef NO_WINDRVR
566 ret = (*ioctl_func) (fd, request, wdioctl);
567 #else
568 if (usbdevice) {
569 if (!usb_devhandle)
570 usb_devhandle = usb_open(usbdevice);
571
572 /* FIXME: Select right interface! */
573 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
574 if (!ret) {
575 if(!ret) {
576 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
577 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
578 if (ret)
579 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
580 } else {
581 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
582 }
583 } else {
584 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
585 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
586 ret, usb_strerror());
587 }
588 }
589 #endif
590 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
591 usi->dwUniqueID, usi->dwInterfaceNum,
592 usi->dwAlternateSetting, usi->dwOptions);
593 }
594 break;
595
596 case USB_GET_DEVICE_DATA_OLD:
597 case USB_GET_DEVICE_DATA:
598 DPRINTF("USB_GET_DEVICE_DATA\n");
599 {
600 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
601 int pSize;
602
603 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
604 ugdd->dwUniqueID, ugdd->dwBytes,
605 ugdd->dwOptions);
606
607 pSize = ugdd->dwBytes;
608 if (!ugdd->dwBytes) {
609 if (usbdevice) {
610 ugdd->dwBytes = usb_deviceinfo(NULL);
611 }
612 } else {
613 usb_deviceinfo((unsigned char*)ugdd->pBuf);
614 }
615 }
616 break;
617
618 case EVENT_REGISTER_OLD:
619 case EVENT_REGISTER:
620 DPRINTF("EVENT_REGISTER\n");
621 {
622 struct event *e = (struct event*)(wdheader->data);
623 struct usb_bus *bus;
624 int i;
625
626 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",
627 e->handle, e->dwAction,
628 e->dwStatus, e->dwEventId, e->dwCardType,
629 e->hKernelPlugIn, e->dwOptions,
630 e->u.Usb.deviceId.dwVendorId,
631 e->u.Usb.deviceId.dwProductId,
632 e->u.Usb.dwUniqueID, e->dwEventVer,
633 e->dwNumMatchTables);
634
635 for (i = 0; i < e->dwNumMatchTables; i++) {
636
637 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
638 e->matchTables[i].VendorId,
639 e->matchTables[i].ProductId,
640 e->matchTables[i].bDeviceClass,
641 e->matchTables[i].bDeviceSubClass,
642 e->matchTables[i].bInterfaceClass,
643 e->matchTables[i].bInterfaceSubClass,
644 e->matchTables[i].bInterfaceProtocol);
645
646 for (bus = busses; bus; bus = bus->next) {
647 struct usb_device *dev;
648
649 for (dev = bus->devices; dev; dev = dev->next) {
650 struct usb_device_descriptor *desc = &(dev->descriptor);
651
652 if((desc->idVendor == e->matchTables[i].VendorId) &&
653 (desc->idProduct == e->matchTables[i].ProductId) &&
654 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
655 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
656 int ac;
657 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
658 struct usb_interface *interface = dev->config[ac].interface;
659 int ai;
660
661 for (ai = 0; ai < interface->num_altsetting; ai++) {
662
663 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
664 interface->altsetting[i].bInterfaceClass,
665 interface->altsetting[i].bInterfaceSubClass,
666 interface->altsetting[i].bInterfaceProtocol);
667
668 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
669 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
670 /* TODO: check interfaceClass! */
671 DPRINTF("found device with libusb\n");
672 usbdevice = dev;
673 card_type = e->dwCardType;
674 }
675 }
676 }
677 }
678 }
679 }
680 }
681
682 #ifndef NO_WINDRVR
683 ret = (*ioctl_func) (fd, request, wdioctl);
684 #else
685 e->handle++;
686 #endif
687
688 #ifdef DEBUG
689 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",
690 e->handle, e->dwAction,
691 e->dwStatus, e->dwEventId, e->dwCardType,
692 e->hKernelPlugIn, e->dwOptions,
693 e->u.Usb.deviceId.dwVendorId,
694 e->u.Usb.deviceId.dwProductId,
695 e->u.Usb.dwUniqueID, e->dwEventVer,
696 e->dwNumMatchTables);
697
698 for (i = 0; i < e->dwNumMatchTables; i++)
699 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
700 e->matchTables[i].VendorId,
701 e->matchTables[i].ProductId,
702 e->matchTables[i].bDeviceClass,
703 e->matchTables[i].bDeviceSubClass,
704 e->matchTables[i].bInterfaceClass,
705 e->matchTables[i].bInterfaceSubClass,
706 e->matchTables[i].bInterfaceProtocol);
707 #endif
708 }
709 break;
710
711 case TRANSFER_OLD:
712 case TRANSFER:
713 DPRINTF("TRANSFER\n");
714 {
715 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
716
717 ret = pp_transfer(tr, fd, request, wdioctl);
718 }
719 break;
720
721 case MULTI_TRANSFER_OLD:
722 case MULTI_TRANSFER:
723 DPRINTF("MULTI_TRANSFER\n");
724 {
725 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
726 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
727 int i;
728
729 for (i = 0; i < num; i++) {
730 DPRINTF("Transfer %d:\n", i+1);
731 #ifndef NO_WINDRVR
732 wdheader->size = sizeof(WD_TRANSFER);
733 request = TRANSFER;
734 wdheader->data = tr + i;
735 #endif
736 ret = pp_transfer(tr + i, fd, request, wdioctl);
737 }
738
739 #ifndef NO_WINDRVR
740 wdheader->data = tr;
741 #endif
742
743 return ret;
744 }
745 break;
746
747 case EVENT_UNREGISTER:
748 DPRINTF("EVENT_UNREGISTER\n");
749 #ifndef NO_WINDRVR
750 ret = (*ioctl_func) (fd, request, wdioctl);
751 #endif
752 break;
753
754 case INT_WAIT:
755 DPRINTF("INT_WAIT\n");
756 {
757 struct interrupt *it = (struct interrupt*)(wdheader->data);
758
759 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
760 it->hInterrupt, it->dwOptions,
761 it->dwCmds, it->fEnableOk, it->dwCounter,
762 it->dwLost, it->fStopped);
763
764 #ifndef NO_WINDRVR
765 ret = (*ioctl_func) (fd, request, wdioctl);
766 #else
767 if (usbdevice) {
768 if (it->dwCounter == 0) {
769 it->dwCounter = 1;
770 } else {
771 pthread_mutex_lock(&int_wait);
772 pthread_mutex_unlock(&int_wait);
773 }
774 } else {
775 pthread_mutex_lock(&int_wait);
776 pthread_mutex_unlock(&int_wait);
777 }
778 #endif
779
780 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
781 it->hInterrupt, it->dwOptions, it->dwCmds,
782 it->fEnableOk, it->dwCounter, it->dwLost,
783 it->fStopped);
784 }
785 break;
786
787 case CARD_UNREGISTER:
788 DPRINTF("CARD_UNREGISTER\n");
789 {
790 struct card_register* cr = (struct card_register*)(wdheader->data);
791
792 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
793 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
794 cr->Card.Item[0].I.IO.dwBytes,
795 cr->Card.Item[0].I.IO.dwBar);
796
797 DPRINTF("hCard: %lu\n", cr->hCard);
798
799 #ifndef NO_WINDRVR
800 ret = (*ioctl_func) (fd, request, wdioctl);
801 #else
802 if (parportfd == cr->hCard && parportfd >= 0) {
803 ioctl(parportfd, PPRELEASE);
804 close(parportfd);
805 parportfd = -1;
806 }
807 #endif
808 }
809 break;
810
811 case EVENT_PULL:
812 DPRINTF("EVENT_PULL\n");
813 {
814 struct event *e = (struct event*)(wdheader->data);
815 #ifdef DEBUG
816 int i;
817
818 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",
819 e->handle, e->dwAction, e->dwStatus,
820 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
821 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
822 e->u.Usb.deviceId.dwProductId,
823 e->u.Usb.dwUniqueID, e->dwEventVer,
824 e->dwNumMatchTables);
825
826 for (i = 0; i < e->dwNumMatchTables; i++)
827 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
828 e->matchTables[i].VendorId,
829 e->matchTables[i].ProductId,
830 e->matchTables[i].bDeviceClass,
831 e->matchTables[i].bDeviceSubClass,
832 e->matchTables[i].bInterfaceClass,
833 e->matchTables[i].bInterfaceSubClass,
834 e->matchTables[i].bInterfaceProtocol);
835 #endif
836
837 #ifndef NO_WINDRVR
838 ret = (*ioctl_func) (fd, request, wdioctl);
839 #else
840 if (usbdevice) {
841 struct usb_interface *interface = usbdevice->config->interface;
842
843 e->dwCardType = card_type;
844 e->dwAction = 1;
845 e->dwEventId = 109;
846 e->u.Usb.dwUniqueID = 110;
847 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
848 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
849 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
850 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
851 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
852 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
853 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
854 }
855 #endif
856
857 #ifdef DEBUG
858 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",
859 e->handle, e->dwAction, e->dwStatus,
860 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
861 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
862 e->u.Usb.deviceId.dwProductId,
863 e->u.Usb.dwUniqueID, e->dwEventVer,
864 e->dwNumMatchTables);
865
866 for (i = 0; i < e->dwNumMatchTables; i++)
867 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
868 e->matchTables[i].VendorId,
869 e->matchTables[i].ProductId,
870 e->matchTables[i].bDeviceClass,
871 e->matchTables[i].bDeviceSubClass,
872 e->matchTables[i].bInterfaceClass,
873 e->matchTables[i].bInterfaceSubClass,
874 e->matchTables[i].bInterfaceProtocol);
875 #endif
876
877 }
878 break;
879
880 default:
881 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
882 #ifndef NO_WINDRVR
883 ret = (*ioctl_func) (fd, request, wdioctl);
884 #endif
885 break;
886 }
887
888 return ret;
889 }
890
891 int ioctl(int fd, unsigned long int request, ...) {
892 va_list args;
893 void *argp;
894 int ret;
895
896 if (!ioctl_func)
897 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
898
899 va_start (args, request);
900 argp = va_arg (args, void *);
901 va_end (args);
902
903 if (fd == windrvrfd)
904 ret = do_wdioctl(fd, request, argp);
905 else
906 ret = (*ioctl_func) (fd, request, argp);
907
908 return ret;
909 }
910
911 int open (const char *pathname, int flags, ...) {
912 static int (*func) (const char *, int, mode_t) = NULL;
913 mode_t mode = 0;
914 va_list args;
915 int fd;
916
917 if (!func)
918 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
919
920 if (flags & O_CREAT) {
921 va_start(args, flags);
922 mode = va_arg(args, mode_t);
923 va_end(args);
924 }
925
926 if (!strcmp (pathname, "/dev/windrvr6")) {
927 DPRINTF("opening windrvr6\n");
928 #ifdef NO_WINDRVR
929 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
930 #else
931 windrvrfd = fd = (*func) (pathname, flags, mode);
932 #endif
933 if (!busses) {
934 usb_init();
935 usb_find_busses();
936 usb_find_devices();
937
938 busses = usb_get_busses();
939 }
940
941 return fd;
942 }
943
944 return (*func) (pathname, flags, mode);
945 }
946
947 int close(int fd) {
948 static int (*func) (int) = NULL;
949
950 if (!func)
951 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
952
953 if (fd == windrvrfd && windrvrfd >= 0) {
954 DPRINTF("close windrvrfd\n");
955 if (usbinterface >= 0)
956 usb_release_interface(usb_devhandle, usbinterface);
957
958 if (usb_devhandle)
959 usb_close(usb_devhandle);
960
961 usb_devhandle = NULL;
962 usbinterface = -1;
963 windrvrfd = -1;
964 }
965
966 return (*func) (fd);
967 }
968
969 FILE *fopen(const char *path, const char *mode) {
970 FILE *ret;
971 static FILE* (*func) (const char*, const char*) = NULL;
972 char buf[256];
973 int i;
974
975 if (!func)
976 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
977
978 #ifdef JTAGKEY
979 /* FIXME: Hack for parport mapping */
980 if (!strcmp(path, "/proc/sys/dev/parport/parport3/base-addr")) {
981 ret = (*func) ("/dev/null", mode);
982 } else
983 #endif
984 ret = (*func) (path, mode);
985
986 if (!strcmp(path, "/proc/modules")) {
987 DPRINTF("opening /proc/modules\n");
988 #ifdef NO_WINDRVR
989 modulesfp = ret;
990 modules_read = 0;
991 #endif
992 }
993
994 if (ret) {
995 for (i = 0; i < 4; i++) {
996 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
997 if (!strcmp(path, buf)) {
998 DPRINTF("open base-addr of parport%d\n", i);
999 baseaddrfp = ret;
1000 baseaddrnum = i;
1001 }
1002 }
1003 }
1004
1005 return ret;
1006 }
1007
1008 char *fgets(char *s, int size, FILE *stream) {
1009 static char* (*func) (char*, int, FILE*) = NULL;
1010 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
1011 char buf[256];
1012 char *ret = NULL;
1013
1014
1015 if (!func)
1016 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
1017
1018 if (modulesfp == stream) {
1019 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
1020 strcpy(s, modules[modules_read]);
1021 ret = s;
1022 modules_read++;
1023 }
1024 } else if (baseaddrfp == stream) {
1025 snprintf(s, sizeof(buf), "%d\t%d\n",
1026 (baseaddrnum) * 0x10,
1027 ((baseaddrnum) * 0x10) + 0x400);
1028 ret = s;
1029 } else {
1030 ret = (*func)(s,size,stream);
1031 }
1032
1033 return ret;
1034 }
1035
1036 int fclose(FILE *fp) {
1037 static int (*func) (FILE*) = NULL;
1038
1039 if (!func)
1040 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
1041
1042 if (fp == modulesfp) {
1043 modulesfp = NULL;
1044 }
1045
1046 if (fp == baseaddrfp) {
1047 baseaddrfp = NULL;
1048 }
1049
1050 return (*func)(fp);
1051 }
1052
1053 int access(const char *pathname, int mode) {
1054 static int (*func) (const char*, int);
1055
1056 if (!func)
1057 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
1058
1059 if (!strcmp(pathname, "/dev/windrvr6")) {
1060 return 0;
1061 } else {
1062 return (*func)(pathname, mode);
1063 }
1064 }
Impressum, Datenschutz