]> git.zerfleddert.de Git - usb-driver/blame - usb-driver.c
reset the USB cable when /dev/windrvr6 is closed.
[usb-driver] / usb-driver.c
CommitLineData
54357994 1/* libusb/ppdev connector for XILINX impact
f1405f13 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 */
cdc711dc 23
24#define _GNU_SOURCE 1
25
cdc711dc 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>
f10480d1 36#include <usb.h>
2a7af812 37#include <signal.h>
533f4b68 38#include <pthread.h>
9ba1e383 39#include <errno.h>
11d01742 40#include <inttypes.h>
54357994 41#include <sys/ioctl.h>
c42237a3
MG
42#include <sys/utsname.h>
43#include <bits/wordsize.h>
2c0c1255 44#include "usb-driver.h"
3e670223 45#include "config.h"
cdc711dc 46
9c9fd67c 47static int (*ioctl_func) (int, int, void *) = NULL;
332ced7a 48static int windrvrfd = -1;
54357994 49static unsigned long ppbase = 0;
50static unsigned long ecpbase = 0;
25ba7a49 51static struct parport_config *pport = NULL;
be59993b
MG
52static FILE *modulesfp = NULL;
53static FILE *baseaddrfp = NULL;
54static int baseaddrnum = 0;
723d9aa0 55static int modules_read = 0;
f10480d1 56static struct usb_bus *busses = NULL;
2a7af812 57static struct usb_device *usbdevice;
58static usb_dev_handle *usb_devhandle = NULL;
010cbaa6 59static int usbinterface = -1;
b72b86b4 60static unsigned long card_type;
ca18111b 61static int ints_enabled = 0;
533f4b68 62static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
292160ed 63
795992ad 64#define NO_WINDRVR 1
723d9aa0 65
66void hexdump(unsigned char *buf, int len) {
67 int i;
68
69 for(i=0; i<len; i++) {
70 fprintf(stderr,"%02x ", buf[i]);
71 if ((i % 16) == 15)
72 fprintf(stderr,"\n");
73 }
f1405f13 74 fprintf(stderr,"\n");
723d9aa0 75}
cdc711dc 76
be59993b 77static int usb_deviceinfo(unsigned char *buf) {
f95f1d2e 78 int i,j,k,l;
79 int len = 0;
3664a3e3 80 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
81 WDU_INTERFACE **pActiveInterface;
f95f1d2e 82
83 if (buf) {
84 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
85
86 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
2a7af812 87 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
88 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
89 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
90 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
91 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
92 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
93 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
94 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
95 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
96 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
97 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
98 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
99 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
f95f1d2e 100
101 /* TODO: Fix Pipe0! */
102 udi->Pipe0.dwNumber = 0x00;
2a7af812 103 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
f95f1d2e 104 udi->Pipe0.type = 0;
8923df66 105 udi->Pipe0.direction = WDU_DIR_IN_OUT;
f95f1d2e 106 udi->Pipe0.dwInterval = 0;
3664a3e3 107
108 pConfigs = &(udi->pConfigs);
109 pActiveConfig = &(udi->pActiveConfig);
110 pActiveInterface = &(udi->pActiveInterface[0]);
f95f1d2e 111 }
112
113 len = sizeof(struct usb_device_info);
114
2a7af812 115 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
f95f1d2e 116 {
2a7af812 117 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
3664a3e3 118 WDU_INTERFACE **pInterfaces;
119 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
120 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
121
f95f1d2e 122 if (buf) {
123 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
124
3664a3e3 125 *pConfigs = cfg;
126 *pActiveConfig = cfg;
127
f95f1d2e 128 cfg->Descriptor.bLength = conf_desc->bLength;
129 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
130 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
131 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
132 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
133 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
134 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
135 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
136
137 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
3664a3e3 138
139 pInterfaces = &(cfg->pInterfaces);
f95f1d2e 140 }
141 len += sizeof(WDU_CONFIGURATION);
3664a3e3 142
f95f1d2e 143 if (buf) {
3664a3e3 144 *pInterfaces = (WDU_INTERFACE*)(buf+len);
f95f1d2e 145 for (j=0; j<conf_desc->bNumInterfaces; j++) {
146 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
3664a3e3 147
148 pActiveInterface[j] = iface;
149
150 pAlternateSettings[j] = &(iface->pAlternateSettings);
2a7af812 151 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
3664a3e3 152 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
f95f1d2e 153
154 len += sizeof(WDU_INTERFACE);
155 }
156 } else {
157 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
158 }
159
160 for (j=0; j<conf_desc->bNumInterfaces; j++)
161 {
2a7af812 162 struct usb_interface *interface = &usbdevice->config[i].interface[j];
3664a3e3 163
164 if (buf) {
165 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
166 /* FIXME: */
167 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
168 }
169
f95f1d2e 170 for(k=0; k<interface->num_altsetting; k++)
171 {
3664a3e3 172 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
173 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
174 WDU_PIPE_INFO **pPipes;
175
f95f1d2e 176 if (buf) {
177 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
178
179 altset->Descriptor.bLength = interface->altsetting[k].bLength;
180 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
181 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
182 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
183 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
184 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
185 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
186 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
187 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
3664a3e3 188 pEndpointDescriptors = &(altset->pEndpointDescriptors);
189 pPipes = &(altset->pPipes);
f95f1d2e 190
191 }
192 len +=sizeof(WDU_ALTERNATE_SETTING);
193
194 if (buf) {
3664a3e3 195 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 196 for (l = 0; l < bNumEndpoints; l++) {
197 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 198
199 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
200 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
201 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
202 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
203 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
204 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
205
206 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
792bf5f2 207 }
f95f1d2e 208
3664a3e3 209 *pPipes = (WDU_PIPE_INFO*)(buf+len);
792bf5f2 210 for (l = 0; l < bNumEndpoints; l++) {
211 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
f95f1d2e 212
213 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
214 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
215 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
216 if (pi->type == PIPE_TYPE_CONTROL)
217 pi->direction = WDU_DIR_IN_OUT;
218 else
219 {
220 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
221 }
222
223 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
224
225 len += sizeof(WDU_PIPE_INFO);
226 }
227 } else {
228 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
229 }
230 }
231 }
232 }
233
234 return len;
235}
236
be59993b 237static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 238 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 239 struct version_struct *version;
240 int ret = 0;
cdc711dc 241
da3ba95a 242 if (wdheader->magic != MAGIC) {
2c2119eb 243 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
244 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 245 }
246
bdc65937 247 switch(request & ~(0xc0000000)) {
da3ba95a 248 case VERSION:
9c9fd67c 249 version = (struct version_struct*)(wdheader->data);
81cf3658 250 strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION);
90831fba 251 version->versionul = 802;
f1405f13 252 DPRINTF("VERSION\n");
cdc711dc 253 break;
2c2119eb 254
255 case LICENSE:
f1405f13 256 DPRINTF("LICENSE\n");
2c2119eb 257 break;
258
f152c048 259 case CARD_REGISTER_OLD:
9c9fd67c 260 case CARD_REGISTER:
54357994 261 DPRINTF("CARD_REGISTER\n");
9c9fd67c 262 {
533f4b68 263 struct card_register* cr = (struct card_register*)(wdheader->data);
54357994 264
265 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
266 cr->Card.dwItems,
267 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
268 cr->Card.Item[0].I.IO.dwBytes,
269 cr->Card.Item[0].I.IO.dwBar);
270
271 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
272 cr->Card.dwItems,
273 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
274 cr->Card.Item[1].I.IO.dwBytes,
275 cr->Card.Item[1].I.IO.dwBar);
576995a8 276#ifndef NO_WINDRVR
277 ret = (*ioctl_func) (fd, request, wdioctl);
ac9e3f59 278#else
bccac33b 279
25ba7a49 280 pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
61e0de7a 281 if (!pport)
282 break;
283
25ba7a49 284 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
3e670223 285
5e3d963b 286 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
bccac33b 287
5e3d963b 288 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
289 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
19b2e286 290
5e3d963b 291 if (ret >= 0) {
292 cr->hCard = ret;
293 } else {
19b2e286 294 cr->hCard = 0;
5e3d963b 295 }
576995a8 296#endif
ac9e3f59 297 DPRINTF("hCard: %lu\n", cr->hCard);
9c9fd67c 298 }
da3ba95a 299 break;
2c2119eb 300
da3ba95a 301 case USB_TRANSFER:
f1405f13 302 DPRINTF("in USB_TRANSFER");
da3ba95a 303 {
304 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
305
723d9aa0 306#ifdef DEBUG
f1405f13 307 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
308 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
309 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
310 DPRINTF("setup packet: ");
9c9fd67c 311 hexdump(ut->SetupPacket, 8);
723d9aa0 312
9c9fd67c 313 if (!ut->fRead && ut->dwBufferSize)
314 {
315 hexdump(ut->pBuffer, ut->dwBufferSize);
9c9fd67c 316 }
723d9aa0 317#endif
9c9fd67c 318
795992ad 319#ifndef NO_WINDRVR
9c9fd67c 320 ret = (*ioctl_func) (fd, request, wdioctl);
411af373 321#else
322 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
a70f4f38 323 if (ut->dwPipeNum == 0) { /* control pipe */
324 int requesttype, request, value, index, size;
325 requesttype = ut->SetupPacket[0];
326 request = ut->SetupPacket[1];
327 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
328 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
329 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
f1405f13 330 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
a70f4f38 331 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
332 } else {
333 if (ut->fRead) {
334 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
335
336 } else {
337 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
338 }
339 }
340
411af373 341 if (ret < 0) {
a70f4f38 342 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
411af373 343 } else {
344 ut->dwBytesTransferred = ret;
345 ret = 0;
346 }
292160ed 347#endif
9c9fd67c 348
723d9aa0 349#ifdef DEBUG
f1405f13 350 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 351 if (ut->fRead && ut->dwBytesTransferred)
352 {
f1405f13 353 DPRINTF("Read: ");
9c9fd67c 354 hexdump(ut->pBuffer, ut->dwBytesTransferred);
355 }
723d9aa0 356#endif
da3ba95a 357 }
cdc711dc 358 break;
2c2119eb 359
f152c048 360 case INT_ENABLE_OLD:
da3ba95a 361 case INT_ENABLE:
f1405f13 362 DPRINTF("INT_ENABLE\n");
da3ba95a 363 {
9c9fd67c 364 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 365
f1405f13 366 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
367 it->hInterrupt, it->dwOptions,
368 it->dwCmds, it->fEnableOk, it->dwCounter,
369 it->dwLost, it->fStopped);
cdc711dc 370
9c9fd67c 371 it->fEnableOk = 1;
9ba1e383 372 it->fStopped = 0;
ca18111b 373 ints_enabled = 1;
533f4b68 374 pthread_mutex_trylock(&int_wait);
9c9fd67c 375 }
cdc711dc 376
cdc711dc 377 break;
9c9fd67c 378
da3ba95a 379 case INT_DISABLE:
f1405f13 380 DPRINTF("INT_DISABLE\n");
da3ba95a 381 {
9c9fd67c 382 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 383
f1405f13 384 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
385 it->hInterrupt, it->dwOptions,
386 it->dwCmds, it->fEnableOk, it->dwCounter,
387 it->dwLost, it->fStopped);
795992ad 388#ifndef NO_WINDRVR
9c9fd67c 389 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 390#else
391 it->dwCounter = 0;
392 it->fStopped = 1;
ca18111b 393 ints_enabled = 0;
9ba1e383 394 if (pthread_mutex_trylock(&int_wait) == EBUSY)
395 pthread_mutex_unlock(&int_wait);
292160ed 396#endif
f1405f13 397 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
398 it->hInterrupt, it->dwOptions,
399 it->dwCmds, it->fEnableOk, it->dwCounter,
400 it->dwLost, it->fStopped);
da3ba95a 401 }
da3ba95a 402 break;
da3ba95a 403
9c9fd67c 404 case USB_SET_INTERFACE:
f1405f13 405 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 406 {
9c9fd67c 407 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
408
f1405f13 409 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
410 usi->dwUniqueID, usi->dwInterfaceNum,
411 usi->dwAlternateSetting, usi->dwOptions);
795992ad 412#ifndef NO_WINDRVR
9c9fd67c 413 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 414#else
415 if (usbdevice) {
2a7af812 416 if (!usb_devhandle)
417 usb_devhandle = usb_open(usbdevice);
d0676964 418
419 /* FIXME: Select right interface! */
420 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
421 if (!ret) {
411af373 422 if(!ret) {
010cbaa6 423 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
411af373 424 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
425 if (ret)
426 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
427 } else {
428 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
429 }
d0676964 430 } else {
f1405f13 431 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
432 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
433 ret, usb_strerror());
d0676964 434 }
2a7af812 435 }
292160ed 436#endif
f1405f13 437 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
438 usi->dwUniqueID, usi->dwInterfaceNum,
439 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 440 }
cdc711dc 441 break;
da3ba95a 442
f152c048 443 case USB_GET_DEVICE_DATA_OLD:
9c9fd67c 444 case USB_GET_DEVICE_DATA:
f1405f13 445 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 446 {
447 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
448 int pSize;
449
f1405f13 450 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
451 ugdd->dwUniqueID, ugdd->dwBytes,
452 ugdd->dwOptions);
453
9c9fd67c 454 pSize = ugdd->dwBytes;
e71b6bf3 455 if (!ugdd->dwBytes) {
2a7af812 456 if (usbdevice) {
f95f1d2e 457 ugdd->dwBytes = usb_deviceinfo(NULL);
e71b6bf3 458 }
459 } else {
f95f1d2e 460 usb_deviceinfo((unsigned char*)ugdd->pBuf);
e71b6bf3 461 }
da3ba95a 462 }
9c9fd67c 463 break;
464
f152c048 465 case EVENT_REGISTER_OLD:
b0f621dd 466 case EVENT_REGISTER:
f1405f13 467 DPRINTF("EVENT_REGISTER\n");
b0f621dd 468 {
469 struct event *e = (struct event*)(wdheader->data);
ac22d975 470 struct usb_bus *bus;
3a2a22bc
MG
471 char* devpos;
472 int busnum = -1, devnum = -1;
b0f621dd 473 int i;
474
f1405f13 475 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",
476 e->handle, e->dwAction,
477 e->dwStatus, e->dwEventId, e->dwCardType,
478 e->hKernelPlugIn, e->dwOptions,
479 e->u.Usb.deviceId.dwVendorId,
480 e->u.Usb.deviceId.dwProductId,
481 e->u.Usb.dwUniqueID, e->dwEventVer,
482 e->dwNumMatchTables);
483
3a2a22bc
MG
484 devpos = getenv("XILINX_USB_DEV");
485 if (devpos != NULL) {
486 int j;
487 char *devstr = NULL, *remainder;
488
489 DPRINTF("XILINX_USB_DEV=%s\n", devpos);
490
491 for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) {
492 if (devpos[j] == ':') {
493 devpos[j] = 0;
494 devstr = &(devpos[j+1]);
495 }
496 }
d377df04 497
3a2a22bc
MG
498 if (devstr && strlen(devstr) > 0) {
499 busnum = strtol(devpos, &remainder, 10);
500 if (devpos == remainder) {
501 busnum = -1;
502 } else {
503 devnum = strtol(devstr, &remainder, 10);
504 if (devstr == remainder) {
77683ab6 505 busnum = -1;
3a2a22bc
MG
506 devnum = -1;
507 } else {
508 fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n",
509 busnum, devnum);
510 }
511 }
512 }
513 }
d377df04 514
ac22d975 515 for (i = 0; i < e->dwNumMatchTables; i++) {
f1405f13 516
517 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
518 e->matchTables[i].VendorId,
519 e->matchTables[i].ProductId,
520 e->matchTables[i].bDeviceClass,
521 e->matchTables[i].bDeviceSubClass,
522 e->matchTables[i].bInterfaceClass,
523 e->matchTables[i].bInterfaceSubClass,
524 e->matchTables[i].bInterfaceProtocol);
b0f621dd 525
ac22d975 526 for (bus = busses; bus; bus = bus->next) {
527 struct usb_device *dev;
528
3a2a22bc
MG
529 if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum))
530 continue;
531
ac22d975 532 for (dev = bus->devices; dev; dev = dev->next) {
533 struct usb_device_descriptor *desc = &(dev->descriptor);
534
535 if((desc->idVendor == e->matchTables[i].VendorId) &&
536 (desc->idProduct == e->matchTables[i].ProductId) &&
537 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
d377df04 538 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
a2a952c0 539 ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) {
2a7af812 540 int ac;
541 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
542 struct usb_interface *interface = dev->config[ac].interface;
543 int ai;
544
545 for (ai = 0; ai < interface->num_altsetting; ai++) {
f1405f13 546
547 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
548 interface->altsetting[i].bInterfaceClass,
549 interface->altsetting[i].bInterfaceSubClass,
550 interface->altsetting[i].bInterfaceProtocol);
551
2a7af812 552 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
553 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
554 /* TODO: check interfaceClass! */
f1405f13 555 DPRINTF("found device with libusb\n");
2a7af812 556 usbdevice = dev;
557 card_type = e->dwCardType;
558 }
559 }
560 }
ac22d975 561 }
562 }
563 }
564 }
565
795992ad 566#ifndef NO_WINDRVR
b0f621dd 567 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 568#else
2a7af812 569 e->handle++;
292160ed 570#endif
b0f621dd 571
723d9aa0 572#ifdef DEBUG
f1405f13 573 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",
574 e->handle, e->dwAction,
575 e->dwStatus, e->dwEventId, e->dwCardType,
576 e->hKernelPlugIn, e->dwOptions,
577 e->u.Usb.deviceId.dwVendorId,
578 e->u.Usb.deviceId.dwProductId,
579 e->u.Usb.dwUniqueID, e->dwEventVer,
580 e->dwNumMatchTables);
581
b0f621dd 582 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 583 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
584 e->matchTables[i].VendorId,
585 e->matchTables[i].ProductId,
586 e->matchTables[i].bDeviceClass,
587 e->matchTables[i].bDeviceSubClass,
588 e->matchTables[i].bInterfaceClass,
589 e->matchTables[i].bInterfaceSubClass,
590 e->matchTables[i].bInterfaceProtocol);
723d9aa0 591#endif
b0f621dd 592 }
593 break;
594
f152c048 595 case TRANSFER_OLD:
da3ba95a 596 case TRANSFER:
f1405f13 597 DPRINTF("TRANSFER\n");
ac9e3f59 598 {
599 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
600
1dac5195 601#ifndef NO_WINDRVR
602 ret = (*ioctl_func) (fd, request, wdioctl);
603#else
25ba7a49 604 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
1dac5195 605#endif
ac9e3f59 606 }
9c9fd67c 607 break;
608
54357994 609 case MULTI_TRANSFER_OLD:
576995a8 610 case MULTI_TRANSFER:
611 DPRINTF("MULTI_TRANSFER\n");
ac9e3f59 612 {
613 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
614 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
1dac5195 615#ifndef NO_WINDRVR
616 ret = (*ioctl_func) (fd, request, wdioctl);
617#else
25ba7a49 618 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
ac9e3f59 619#endif
ac9e3f59 620 }
576995a8 621 break;
622
da3ba95a 623 case EVENT_UNREGISTER:
f1405f13 624 DPRINTF("EVENT_UNREGISTER\n");
795992ad 625#ifndef NO_WINDRVR
9c9fd67c 626 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 627#endif
9c9fd67c 628 break;
629
da3ba95a 630 case INT_WAIT:
f1405f13 631 DPRINTF("INT_WAIT\n");
b0f621dd 632 {
633 struct interrupt *it = (struct interrupt*)(wdheader->data);
634
f1405f13 635 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
636 it->hInterrupt, it->dwOptions,
637 it->dwCmds, it->fEnableOk, it->dwCounter,
638 it->dwLost, it->fStopped);
b0f621dd 639
795992ad 640#ifndef NO_WINDRVR
b0f621dd 641 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 642#else
2a7af812 643 if (usbdevice) {
644 if (it->dwCounter == 0) {
645 it->dwCounter = 1;
646 } else {
533f4b68 647 pthread_mutex_lock(&int_wait);
be452175 648 pthread_mutex_unlock(&int_wait);
2a7af812 649 }
94038a57 650 } else {
533f4b68 651 pthread_mutex_lock(&int_wait);
be452175 652 pthread_mutex_unlock(&int_wait);
2a7af812 653 }
292160ed 654#endif
655
f1405f13 656 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
657 it->hInterrupt, it->dwOptions, it->dwCmds,
658 it->fEnableOk, it->dwCounter, it->dwLost,
659 it->fStopped);
b0f621dd 660 }
9c9fd67c 661 break;
662
da3ba95a 663 case CARD_UNREGISTER:
f1405f13 664 DPRINTF("CARD_UNREGISTER\n");
54357994 665 {
666 struct card_register* cr = (struct card_register*)(wdheader->data);
667
668 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
669 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
670 cr->Card.Item[0].I.IO.dwBytes,
671 cr->Card.Item[0].I.IO.dwBar);
672
673 DPRINTF("hCard: %lu\n", cr->hCard);
674
795992ad 675#ifndef NO_WINDRVR
54357994 676 ret = (*ioctl_func) (fd, request, wdioctl);
677#else
72ce448b
MG
678 if (pport)
679 pport->close(cr->hCard);
680
681 pport = NULL;
292160ed 682#endif
54357994 683 }
9c9fd67c 684 break;
685
da3ba95a 686 case EVENT_PULL:
f1405f13 687 DPRINTF("EVENT_PULL\n");
b1831983 688 {
689 struct event *e = (struct event*)(wdheader->data);
723d9aa0 690#ifdef DEBUG
b1831983 691 int i;
692
f1405f13 693 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",
694 e->handle, e->dwAction, e->dwStatus,
695 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
696 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
697 e->u.Usb.deviceId.dwProductId,
698 e->u.Usb.dwUniqueID, e->dwEventVer,
699 e->dwNumMatchTables);
700
b1831983 701 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 702 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
703 e->matchTables[i].VendorId,
704 e->matchTables[i].ProductId,
705 e->matchTables[i].bDeviceClass,
706 e->matchTables[i].bDeviceSubClass,
707 e->matchTables[i].bInterfaceClass,
708 e->matchTables[i].bInterfaceSubClass,
709 e->matchTables[i].bInterfaceProtocol);
723d9aa0 710#endif
b1831983 711
795992ad 712#ifndef NO_WINDRVR
b1831983 713 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 714#else
2a7af812 715 if (usbdevice) {
716 struct usb_interface *interface = usbdevice->config->interface;
292160ed 717
718 e->dwCardType = card_type;
719 e->dwAction = 1;
720 e->dwEventId = 109;
ca18111b 721 e->u.Usb.dwUniqueID = 110;
2a7af812 722 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
723 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
724 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
725 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
292160ed 726 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
727 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
728 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
729 }
730#endif
b1831983 731
723d9aa0 732#ifdef DEBUG
f1405f13 733 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",
734 e->handle, e->dwAction, e->dwStatus,
735 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
736 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
737 e->u.Usb.deviceId.dwProductId,
738 e->u.Usb.dwUniqueID, e->dwEventVer,
739 e->dwNumMatchTables);
740
b1831983 741 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 742 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
743 e->matchTables[i].VendorId,
744 e->matchTables[i].ProductId,
745 e->matchTables[i].bDeviceClass,
746 e->matchTables[i].bDeviceSubClass,
747 e->matchTables[i].bInterfaceClass,
748 e->matchTables[i].bInterfaceSubClass,
749 e->matchTables[i].bInterfaceProtocol);
723d9aa0 750#endif
ca18111b 751
b1831983 752 }
9c9fd67c 753 break;
754
da3ba95a 755 default:
292160ed 756 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 757#ifndef NO_WINDRVR
9c9fd67c 758 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 759#endif
760 break;
cdc711dc 761 }
da3ba95a 762
9c9fd67c 763 return ret;
cdc711dc 764}
765
54357994 766int ioctl(int fd, unsigned long int request, ...) {
dbda1264 767 va_list args;
768 void *argp;
769 int ret;
770
771 if (!ioctl_func)
f1405f13 772 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 773
774 va_start (args, request);
775 argp = va_arg (args, void *);
776 va_end (args);
777
332ced7a 778 if (fd == windrvrfd)
dbda1264 779 ret = do_wdioctl(fd, request, argp);
780 else
781 ret = (*ioctl_func) (fd, request, argp);
782
783 return ret;
784}
cdc711dc 785
be452175 786int open (const char *pathname, int flags, ...) {
787 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 788 mode_t mode = 0;
789 va_list args;
790 int fd;
791
792 if (!func)
f1405f13 793 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 794
795 if (flags & O_CREAT) {
796 va_start(args, flags);
797 mode = va_arg(args, mode_t);
798 va_end(args);
799 }
800
cdc711dc 801 if (!strcmp (pathname, "/dev/windrvr6")) {
f1405f13 802 DPRINTF("opening windrvr6\n");
795992ad 803#ifdef NO_WINDRVR
01b99d52 804 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
805#else
806 windrvrfd = fd = (*func) (pathname, flags, mode);
807#endif
f10480d1 808 if (!busses) {
809 usb_init();
810 usb_find_busses();
811 usb_find_devices();
812
813 busses = usb_get_busses();
814 }
723d9aa0 815
816 return fd;
cdc711dc 817 }
818
723d9aa0 819 return (*func) (pathname, flags, mode);
cdc711dc 820}
821
723d9aa0 822int close(int fd) {
823 static int (*func) (int) = NULL;
cdc711dc 824
723d9aa0 825 if (!func)
f1405f13 826 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 827
11d01742 828 if (fd == windrvrfd && windrvrfd >= 0) {
f1405f13 829 DPRINTF("close windrvrfd\n");
010cbaa6 830 if (usbinterface >= 0)
831 usb_release_interface(usb_devhandle, usbinterface);
832
bbc3baea
MG
833 if (usb_devhandle) {
834#ifndef NO_USB_RESET
835 usb_reset(usb_devhandle);
836#endif
010cbaa6 837 usb_close(usb_devhandle);
bbc3baea 838 }
010cbaa6 839
840 usb_devhandle = NULL;
841 usbinterface = -1;
332ced7a 842 windrvrfd = -1;
cdc711dc 843 }
cdc711dc 844
723d9aa0 845 return (*func) (fd);
cdc711dc 846}
847
723d9aa0 848FILE *fopen(const char *path, const char *mode) {
849 FILE *ret;
850 static FILE* (*func) (const char*, const char*) = NULL;
0dca330e 851 char buf[256];
852 int i;
ca18111b 853
854 if (!func)
f1405f13 855 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 856
3e670223 857 for (i = 0; i < 4; i++) {
858 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
859 if (!strcmp(path, buf)) {
860 DPRINTF("open base-addr of parport%d\n", i);
861 if (config_is_real_pport(i)) {
862 ret = (*func) (path, mode);
863 } else {
864 ret = (*func) ("/dev/null", mode);
865 }
866
867 if (ret) {
868 baseaddrfp = ret;
869 baseaddrnum = i;
870 }
871
872 return ret;
873 }
874 }
875
876 ret = (*func) (path, mode);
ca18111b 877
0dca330e 878 if (!strcmp(path, "/proc/modules")) {
f1405f13 879 DPRINTF("opening /proc/modules\n");
723d9aa0 880#ifdef NO_WINDRVR
dbda1264 881 modulesfp = ret;
723d9aa0 882 modules_read = 0;
883#endif
884 }
cdc711dc 885
886 return ret;
887}
888
dbda1264 889char *fgets(char *s, int size, FILE *stream) {
890 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 891 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
0dca330e 892 char buf[256];
dbda1264 893 char *ret = NULL;
894
cdc711dc 895
896 if (!func)
f1405f13 897 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 898
dbda1264 899 if (modulesfp == stream) {
54357994 900 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
16f6b164 901 strcpy(s, modules[modules_read]);
dbda1264 902 ret = s;
16f6b164 903 modules_read++;
dbda1264 904 }
0dca330e 905 } else if (baseaddrfp == stream) {
906 snprintf(s, sizeof(buf), "%d\t%d\n",
907 (baseaddrnum) * 0x10,
908 ((baseaddrnum) * 0x10) + 0x400);
909 ret = s;
723d9aa0 910 } else {
dbda1264 911 ret = (*func)(s,size,stream);
723d9aa0 912 }
cdc711dc 913
914 return ret;
915}
916
dbda1264 917int fclose(FILE *fp) {
918 static int (*func) (FILE*) = NULL;
cdc711dc 919
dbda1264 920 if (!func)
f1405f13 921 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 922
dbda1264 923 if (fp == modulesfp) {
924 modulesfp = NULL;
925 }
0dca330e 926
927 if (fp == baseaddrfp) {
928 baseaddrfp = NULL;
929 }
dbda1264 930
931 return (*func)(fp);
cdc711dc 932}
419f2c98 933
934int access(const char *pathname, int mode) {
935 static int (*func) (const char*, int);
936
937 if (!func)
f1405f13 938 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
3eee002c 939
b8b756f5 940 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
419f2c98 941 return 0;
942 } else {
943 return (*func)(pathname, mode);
944 }
945}
c42237a3
MG
946
947#if __WORDSIZE == 32
948int uname (struct utsname *__name) {
949 static int (*func) (struct utsname*);
950 int ret;
951
952 if (!func)
953 func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname");
954
955 ret = (*func)(__name);
956
957 if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) {
958 strcpy(__name->machine, "i686");
959 }
960
961 return ret;
962}
963#endif
Impressum, Datenschutz