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