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