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