]> git.zerfleddert.de Git - usb-driver/blame - usb-driver.c
allow concurrent access to the cable
[usb-driver] / usb-driver.c
CommitLineData
f1405f13 1/* libusb 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 */
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>
2c0c1255 41#include "usb-driver.h"
cdc711dc 42
9c9fd67c 43static int (*ioctl_func) (int, int, void *) = NULL;
332ced7a 44static int windrvrfd = -1;
45FILE *modulesfp = NULL;
723d9aa0 46static int modules_read = 0;
f10480d1 47static struct usb_bus *busses = NULL;
2a7af812 48static struct usb_device *usbdevice;
49static usb_dev_handle *usb_devhandle = NULL;
010cbaa6 50static int usbinterface = -1;
b72b86b4 51static unsigned long card_type;
ca18111b 52static int ints_enabled = 0;
533f4b68 53static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
292160ed 54
795992ad 55#define NO_WINDRVR 1
723d9aa0 56
be452175 57#ifdef DEBUG
f1405f13 58#define DPRINTF(format, args...) fprintf(stderr, format, ##args)
723d9aa0 59void hexdump(unsigned char *buf, int len) {
60 int i;
61
62 for(i=0; i<len; i++) {
63 fprintf(stderr,"%02x ", buf[i]);
64 if ((i % 16) == 15)
65 fprintf(stderr,"\n");
66 }
f1405f13 67 fprintf(stderr,"\n");
723d9aa0 68}
f1405f13 69#else
70#define DPRINTF(format, args...)
be452175 71#endif
cdc711dc 72
f95f1d2e 73int usb_deviceinfo(unsigned char *buf) {
74 int i,j,k,l;
75 int len = 0;
3664a3e3 76 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
77 WDU_INTERFACE **pActiveInterface;
f95f1d2e 78
79 if (buf) {
80 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
81
82 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
2a7af812 83 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
84 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
85 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
86 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
87 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
88 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
89 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
90 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
91 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
92 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
93 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
94 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
95 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
f95f1d2e 96
97 /* TODO: Fix Pipe0! */
98 udi->Pipe0.dwNumber = 0x00;
2a7af812 99 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
f95f1d2e 100 udi->Pipe0.type = 0;
8923df66 101 udi->Pipe0.direction = WDU_DIR_IN_OUT;
f95f1d2e 102 udi->Pipe0.dwInterval = 0;
3664a3e3 103
104 pConfigs = &(udi->pConfigs);
105 pActiveConfig = &(udi->pActiveConfig);
106 pActiveInterface = &(udi->pActiveInterface[0]);
f95f1d2e 107 }
108
109 len = sizeof(struct usb_device_info);
110
2a7af812 111 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
f95f1d2e 112 {
2a7af812 113 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
3664a3e3 114 WDU_INTERFACE **pInterfaces;
115 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
116 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
117
f95f1d2e 118 if (buf) {
119 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
120
3664a3e3 121 *pConfigs = cfg;
122 *pActiveConfig = cfg;
123
f95f1d2e 124 cfg->Descriptor.bLength = conf_desc->bLength;
125 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
126 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
127 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
128 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
129 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
130 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
131 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
132
133 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
3664a3e3 134
135 pInterfaces = &(cfg->pInterfaces);
f95f1d2e 136 }
137 len += sizeof(WDU_CONFIGURATION);
3664a3e3 138
f95f1d2e 139 if (buf) {
3664a3e3 140 *pInterfaces = (WDU_INTERFACE*)(buf+len);
f95f1d2e 141 for (j=0; j<conf_desc->bNumInterfaces; j++) {
142 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
3664a3e3 143
144 pActiveInterface[j] = iface;
145
146 pAlternateSettings[j] = &(iface->pAlternateSettings);
2a7af812 147 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
3664a3e3 148 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
f95f1d2e 149
150 len += sizeof(WDU_INTERFACE);
151 }
152 } else {
153 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
154 }
155
156 for (j=0; j<conf_desc->bNumInterfaces; j++)
157 {
2a7af812 158 struct usb_interface *interface = &usbdevice->config[i].interface[j];
3664a3e3 159
160 if (buf) {
161 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
162 /* FIXME: */
163 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
164 }
165
f95f1d2e 166 for(k=0; k<interface->num_altsetting; k++)
167 {
3664a3e3 168 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
169 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
170 WDU_PIPE_INFO **pPipes;
171
f95f1d2e 172 if (buf) {
173 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
174
175 altset->Descriptor.bLength = interface->altsetting[k].bLength;
176 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
177 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
178 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
179 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
180 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
181 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
182 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
183 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
3664a3e3 184 pEndpointDescriptors = &(altset->pEndpointDescriptors);
185 pPipes = &(altset->pPipes);
f95f1d2e 186
187 }
188 len +=sizeof(WDU_ALTERNATE_SETTING);
189
190 if (buf) {
3664a3e3 191 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 192 for (l = 0; l < bNumEndpoints; l++) {
193 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 194
195 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
196 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
197 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
198 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
199 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
200 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
201
202 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
792bf5f2 203 }
f95f1d2e 204
3664a3e3 205 *pPipes = (WDU_PIPE_INFO*)(buf+len);
792bf5f2 206 for (l = 0; l < bNumEndpoints; l++) {
207 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
f95f1d2e 208
209 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
210 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
211 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
212 if (pi->type == PIPE_TYPE_CONTROL)
213 pi->direction = WDU_DIR_IN_OUT;
214 else
215 {
216 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
217 }
218
219 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
220
221 len += sizeof(WDU_PIPE_INFO);
222 }
223 } else {
224 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
225 }
226 }
227 }
228 }
229
230 return len;
231}
232
9c9fd67c 233int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 234 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 235 struct version_struct *version;
236 int ret = 0;
cdc711dc 237
da3ba95a 238 if (wdheader->magic != MAGIC) {
2c2119eb 239 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
240 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 241 }
242
cdc711dc 243 switch(request) {
da3ba95a 244 case VERSION:
9c9fd67c 245 version = (struct version_struct*)(wdheader->data);
246 strcpy(version->version, "WinDriver no more");
90831fba 247 version->versionul = 802;
f1405f13 248 DPRINTF("VERSION\n");
cdc711dc 249 break;
2c2119eb 250
251 case LICENSE:
f1405f13 252 DPRINTF("LICENSE\n");
2c2119eb 253 break;
254
9c9fd67c 255 case CARD_REGISTER:
533f4b68 256 /* TODO: Implement for LPT-support */
257#if 0
9c9fd67c 258 {
533f4b68 259 struct card_register* cr = (struct card_register*)(wdheader->data);
9c9fd67c 260 }
533f4b68 261#endif
f1405f13 262 DPRINTF("CARD_REGISTER\n");
da3ba95a 263 break;
2c2119eb 264
da3ba95a 265 case USB_TRANSFER:
f1405f13 266 DPRINTF("in USB_TRANSFER");
da3ba95a 267 {
268 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
269
723d9aa0 270#ifdef DEBUG
f1405f13 271 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
272 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
273 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
274 DPRINTF("setup packet: ");
9c9fd67c 275 hexdump(ut->SetupPacket, 8);
723d9aa0 276
9c9fd67c 277 if (!ut->fRead && ut->dwBufferSize)
278 {
279 hexdump(ut->pBuffer, ut->dwBufferSize);
9c9fd67c 280 }
723d9aa0 281#endif
9c9fd67c 282
795992ad 283#ifndef NO_WINDRVR
9c9fd67c 284 ret = (*ioctl_func) (fd, request, wdioctl);
411af373 285#else
286 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
a70f4f38 287 if (ut->dwPipeNum == 0) { /* control pipe */
288 int requesttype, request, value, index, size;
289 requesttype = ut->SetupPacket[0];
290 request = ut->SetupPacket[1];
291 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
292 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
293 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
f1405f13 294 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
a70f4f38 295 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
296 } else {
297 if (ut->fRead) {
298 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
299
300 } else {
301 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
302 }
303 }
304
411af373 305 if (ret < 0) {
a70f4f38 306 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
411af373 307 } else {
308 ut->dwBytesTransferred = ret;
309 ret = 0;
310 }
292160ed 311#endif
9c9fd67c 312
723d9aa0 313#ifdef DEBUG
f1405f13 314 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 315 if (ut->fRead && ut->dwBytesTransferred)
316 {
f1405f13 317 DPRINTF("Read: ");
9c9fd67c 318 hexdump(ut->pBuffer, ut->dwBytesTransferred);
319 }
723d9aa0 320#endif
da3ba95a 321 }
cdc711dc 322 break;
2c2119eb 323
da3ba95a 324 case INT_ENABLE:
f1405f13 325 DPRINTF("INT_ENABLE\n");
da3ba95a 326 {
9c9fd67c 327 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 328
f1405f13 329 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
330 it->hInterrupt, it->dwOptions,
331 it->dwCmds, it->fEnableOk, it->dwCounter,
332 it->dwLost, it->fStopped);
cdc711dc 333
9c9fd67c 334 it->fEnableOk = 1;
9ba1e383 335 it->fStopped = 0;
ca18111b 336 ints_enabled = 1;
533f4b68 337 pthread_mutex_trylock(&int_wait);
9c9fd67c 338 }
cdc711dc 339
cdc711dc 340 break;
9c9fd67c 341
da3ba95a 342 case INT_DISABLE:
f1405f13 343 DPRINTF("INT_DISABLE\n");
da3ba95a 344 {
9c9fd67c 345 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 346
f1405f13 347 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
348 it->hInterrupt, it->dwOptions,
349 it->dwCmds, it->fEnableOk, it->dwCounter,
350 it->dwLost, it->fStopped);
795992ad 351#ifndef NO_WINDRVR
9c9fd67c 352 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 353#else
354 it->dwCounter = 0;
355 it->fStopped = 1;
ca18111b 356 ints_enabled = 0;
9ba1e383 357 if (pthread_mutex_trylock(&int_wait) == EBUSY)
358 pthread_mutex_unlock(&int_wait);
292160ed 359#endif
f1405f13 360 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
361 it->hInterrupt, it->dwOptions,
362 it->dwCmds, it->fEnableOk, it->dwCounter,
363 it->dwLost, it->fStopped);
da3ba95a 364 }
da3ba95a 365 break;
da3ba95a 366
9c9fd67c 367 case USB_SET_INTERFACE:
f1405f13 368 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 369 {
9c9fd67c 370 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
371
f1405f13 372 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
373 usi->dwUniqueID, usi->dwInterfaceNum,
374 usi->dwAlternateSetting, usi->dwOptions);
795992ad 375#ifndef NO_WINDRVR
9c9fd67c 376 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 377#else
378 if (usbdevice) {
2a7af812 379 if (!usb_devhandle)
380 usb_devhandle = usb_open(usbdevice);
d0676964 381
382 /* FIXME: Select right interface! */
383 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
384 if (!ret) {
411af373 385 if(!ret) {
010cbaa6 386 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
411af373 387 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
388 if (ret)
389 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
390 } else {
391 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
392 }
d0676964 393 } else {
f1405f13 394 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
395 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
396 ret, usb_strerror());
d0676964 397 }
2a7af812 398 }
292160ed 399#endif
f1405f13 400 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
401 usi->dwUniqueID, usi->dwInterfaceNum,
402 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 403 }
cdc711dc 404 break;
da3ba95a 405
9c9fd67c 406 case USB_GET_DEVICE_DATA:
f1405f13 407 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 408 {
409 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
410 int pSize;
411
f1405f13 412 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
413 ugdd->dwUniqueID, ugdd->dwBytes,
414 ugdd->dwOptions);
415
9c9fd67c 416 pSize = ugdd->dwBytes;
e71b6bf3 417 if (!ugdd->dwBytes) {
2a7af812 418 if (usbdevice) {
f95f1d2e 419 ugdd->dwBytes = usb_deviceinfo(NULL);
e71b6bf3 420 }
421 } else {
f95f1d2e 422 usb_deviceinfo((unsigned char*)ugdd->pBuf);
e71b6bf3 423 }
da3ba95a 424 }
9c9fd67c 425 break;
426
b0f621dd 427 case EVENT_REGISTER:
f1405f13 428 DPRINTF("EVENT_REGISTER\n");
b0f621dd 429 {
430 struct event *e = (struct event*)(wdheader->data);
ac22d975 431 struct usb_bus *bus;
b0f621dd 432 int i;
433
f1405f13 434 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",
435 e->handle, e->dwAction,
436 e->dwStatus, e->dwEventId, e->dwCardType,
437 e->hKernelPlugIn, e->dwOptions,
438 e->u.Usb.deviceId.dwVendorId,
439 e->u.Usb.deviceId.dwProductId,
440 e->u.Usb.dwUniqueID, e->dwEventVer,
441 e->dwNumMatchTables);
442
ac22d975 443 for (i = 0; i < e->dwNumMatchTables; i++) {
f1405f13 444
445 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
446 e->matchTables[i].VendorId,
447 e->matchTables[i].ProductId,
448 e->matchTables[i].bDeviceClass,
449 e->matchTables[i].bDeviceSubClass,
450 e->matchTables[i].bInterfaceClass,
451 e->matchTables[i].bInterfaceSubClass,
452 e->matchTables[i].bInterfaceProtocol);
b0f621dd 453
ac22d975 454 for (bus = busses; bus; bus = bus->next) {
455 struct usb_device *dev;
456
457 for (dev = bus->devices; dev; dev = dev->next) {
458 struct usb_device_descriptor *desc = &(dev->descriptor);
459
460 if((desc->idVendor == e->matchTables[i].VendorId) &&
461 (desc->idProduct == e->matchTables[i].ProductId) &&
462 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
463 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
2a7af812 464 int ac;
465 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
466 struct usb_interface *interface = dev->config[ac].interface;
467 int ai;
468
469 for (ai = 0; ai < interface->num_altsetting; ai++) {
f1405f13 470
471 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
472 interface->altsetting[i].bInterfaceClass,
473 interface->altsetting[i].bInterfaceSubClass,
474 interface->altsetting[i].bInterfaceProtocol);
475
2a7af812 476 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
477 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
478 /* TODO: check interfaceClass! */
f1405f13 479 DPRINTF("found device with libusb\n");
2a7af812 480 usbdevice = dev;
481 card_type = e->dwCardType;
482 }
483 }
484 }
ac22d975 485 }
486 }
487 }
488 }
489
795992ad 490#ifndef NO_WINDRVR
b0f621dd 491 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 492#else
2a7af812 493 e->handle++;
292160ed 494#endif
b0f621dd 495
723d9aa0 496#ifdef DEBUG
f1405f13 497 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",
498 e->handle, e->dwAction,
499 e->dwStatus, e->dwEventId, e->dwCardType,
500 e->hKernelPlugIn, e->dwOptions,
501 e->u.Usb.deviceId.dwVendorId,
502 e->u.Usb.deviceId.dwProductId,
503 e->u.Usb.dwUniqueID, e->dwEventVer,
504 e->dwNumMatchTables);
505
b0f621dd 506 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 507 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
508 e->matchTables[i].VendorId,
509 e->matchTables[i].ProductId,
510 e->matchTables[i].bDeviceClass,
511 e->matchTables[i].bDeviceSubClass,
512 e->matchTables[i].bInterfaceClass,
513 e->matchTables[i].bInterfaceSubClass,
514 e->matchTables[i].bInterfaceProtocol);
723d9aa0 515#endif
b0f621dd 516 }
517 break;
518
da3ba95a 519 case TRANSFER:
f1405f13 520 DPRINTF("TRANSFER\n");
795992ad 521#ifndef NO_WINDRVR
9c9fd67c 522 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 523#endif
9c9fd67c 524 break;
525
da3ba95a 526 case EVENT_UNREGISTER:
f1405f13 527 DPRINTF("EVENT_UNREGISTER\n");
795992ad 528#ifndef NO_WINDRVR
9c9fd67c 529 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 530#endif
9c9fd67c 531 break;
532
da3ba95a 533 case INT_WAIT:
f1405f13 534 DPRINTF("INT_WAIT\n");
b0f621dd 535 {
536 struct interrupt *it = (struct interrupt*)(wdheader->data);
537
f1405f13 538 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
539 it->hInterrupt, it->dwOptions,
540 it->dwCmds, it->fEnableOk, it->dwCounter,
541 it->dwLost, it->fStopped);
b0f621dd 542
795992ad 543#ifndef NO_WINDRVR
b0f621dd 544 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 545#else
2a7af812 546 if (usbdevice) {
547 if (it->dwCounter == 0) {
548 it->dwCounter = 1;
549 } else {
533f4b68 550 pthread_mutex_lock(&int_wait);
be452175 551 pthread_mutex_unlock(&int_wait);
2a7af812 552 }
94038a57 553 } else {
533f4b68 554 pthread_mutex_lock(&int_wait);
be452175 555 pthread_mutex_unlock(&int_wait);
2a7af812 556 }
292160ed 557#endif
558
f1405f13 559 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
560 it->hInterrupt, it->dwOptions, it->dwCmds,
561 it->fEnableOk, it->dwCounter, it->dwLost,
562 it->fStopped);
b0f621dd 563 }
9c9fd67c 564 break;
565
da3ba95a 566 case CARD_UNREGISTER:
f1405f13 567 DPRINTF("CARD_UNREGISTER\n");
795992ad 568#ifndef NO_WINDRVR
9c9fd67c 569 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 570#endif
9c9fd67c 571 break;
572
da3ba95a 573 case EVENT_PULL:
f1405f13 574 DPRINTF("EVENT_PULL\n");
b1831983 575 {
576 struct event *e = (struct event*)(wdheader->data);
723d9aa0 577#ifdef DEBUG
b1831983 578 int i;
579
f1405f13 580 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",
581 e->handle, e->dwAction, e->dwStatus,
582 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
583 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
584 e->u.Usb.deviceId.dwProductId,
585 e->u.Usb.dwUniqueID, e->dwEventVer,
586 e->dwNumMatchTables);
587
b1831983 588 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 589 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
590 e->matchTables[i].VendorId,
591 e->matchTables[i].ProductId,
592 e->matchTables[i].bDeviceClass,
593 e->matchTables[i].bDeviceSubClass,
594 e->matchTables[i].bInterfaceClass,
595 e->matchTables[i].bInterfaceSubClass,
596 e->matchTables[i].bInterfaceProtocol);
723d9aa0 597#endif
b1831983 598
795992ad 599#ifndef NO_WINDRVR
b1831983 600 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 601#else
2a7af812 602 if (usbdevice) {
603 struct usb_interface *interface = usbdevice->config->interface;
292160ed 604
605 e->dwCardType = card_type;
606 e->dwAction = 1;
607 e->dwEventId = 109;
ca18111b 608 e->u.Usb.dwUniqueID = 110;
2a7af812 609 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
610 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
611 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
612 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
292160ed 613 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
614 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
615 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
616 }
617#endif
b1831983 618
723d9aa0 619#ifdef DEBUG
f1405f13 620 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",
621 e->handle, e->dwAction, e->dwStatus,
622 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
623 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
624 e->u.Usb.deviceId.dwProductId,
625 e->u.Usb.dwUniqueID, e->dwEventVer,
626 e->dwNumMatchTables);
627
b1831983 628 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 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);
723d9aa0 637#endif
ca18111b 638
b1831983 639 }
9c9fd67c 640 break;
641
da3ba95a 642 default:
292160ed 643 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 644#ifndef NO_WINDRVR
9c9fd67c 645 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 646#endif
647 break;
cdc711dc 648 }
da3ba95a 649
9c9fd67c 650 return ret;
cdc711dc 651}
652
be452175 653int ioctl(int fd, int request, ...) {
dbda1264 654 va_list args;
655 void *argp;
656 int ret;
657
658 if (!ioctl_func)
f1405f13 659 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 660
661 va_start (args, request);
662 argp = va_arg (args, void *);
663 va_end (args);
664
332ced7a 665 if (fd == windrvrfd)
dbda1264 666 ret = do_wdioctl(fd, request, argp);
667 else
668 ret = (*ioctl_func) (fd, request, argp);
669
670 return ret;
671}
cdc711dc 672
be452175 673int open (const char *pathname, int flags, ...) {
674 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 675 mode_t mode = 0;
676 va_list args;
677 int fd;
678
679 if (!func)
f1405f13 680 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 681
682 if (flags & O_CREAT) {
683 va_start(args, flags);
684 mode = va_arg(args, mode_t);
685 va_end(args);
686 }
687
cdc711dc 688 if (!strcmp (pathname, "/dev/windrvr6")) {
f1405f13 689 DPRINTF("opening windrvr6\n");
795992ad 690#ifdef NO_WINDRVR
01b99d52 691 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
692#else
693 windrvrfd = fd = (*func) (pathname, flags, mode);
694#endif
f10480d1 695 if (!busses) {
696 usb_init();
697 usb_find_busses();
698 usb_find_devices();
699
700 busses = usb_get_busses();
701 }
723d9aa0 702
703 return fd;
cdc711dc 704 }
705
723d9aa0 706 return (*func) (pathname, flags, mode);
cdc711dc 707}
708
723d9aa0 709int close(int fd) {
710 static int (*func) (int) = NULL;
cdc711dc 711
723d9aa0 712 if (!func)
f1405f13 713 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 714
11d01742 715 if (fd == windrvrfd && windrvrfd >= 0) {
f1405f13 716 DPRINTF("close windrvrfd\n");
010cbaa6 717 if (usbinterface >= 0)
718 usb_release_interface(usb_devhandle, usbinterface);
719
720 if (usb_devhandle)
721 usb_close(usb_devhandle);
722
723 usb_devhandle = NULL;
724 usbinterface = -1;
332ced7a 725 windrvrfd = -1;
cdc711dc 726 }
cdc711dc 727
723d9aa0 728 return (*func) (fd);
cdc711dc 729}
730
723d9aa0 731FILE *fopen(const char *path, const char *mode) {
732 FILE *ret;
733 static FILE* (*func) (const char*, const char*) = NULL;
ca18111b 734
735 if (!func)
f1405f13 736 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 737
723d9aa0 738 ret = (*func) (path, mode);
ca18111b 739
723d9aa0 740 if (!strcmp (path, "/proc/modules")) {
f1405f13 741 DPRINTF("opening /proc/modules\n");
723d9aa0 742#ifdef NO_WINDRVR
dbda1264 743 modulesfp = ret;
723d9aa0 744 modules_read = 0;
745#endif
746 }
cdc711dc 747
748 return ret;
749}
750
dbda1264 751char *fgets(char *s, int size, FILE *stream) {
752 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 753 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
dbda1264 754 char *ret = NULL;
755
cdc711dc 756
757 if (!func)
f1405f13 758 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 759
dbda1264 760 if (modulesfp == stream) {
16f6b164 761 if (modules_read < sizeof(modules)) {
762 strcpy(s, modules[modules_read]);
dbda1264 763 ret = s;
16f6b164 764 modules_read++;
dbda1264 765 }
723d9aa0 766 } else {
dbda1264 767 ret = (*func)(s,size,stream);
723d9aa0 768 }
cdc711dc 769
770 return ret;
771}
772
dbda1264 773int fclose(FILE *fp) {
774 static int (*func) (FILE*) = NULL;
cdc711dc 775
dbda1264 776 if (!func)
f1405f13 777 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 778
dbda1264 779 if (fp == modulesfp) {
780 modulesfp = NULL;
781 }
782
783 return (*func)(fp);
cdc711dc 784}
419f2c98 785
786int access(const char *pathname, int mode) {
787 static int (*func) (const char*, int);
788
789 if (!func)
f1405f13 790 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
419f2c98 791
792 if (!strcmp(pathname, "/dev/windrvr6")) {
793 return 0;
794 } else {
795 return (*func)(pathname, mode);
796 }
797}
Impressum, Datenschutz