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