add tested software
[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
bdc65937 243 switch(request & ~(0xc0000000)) {
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
f152c048 255 case CARD_REGISTER_OLD:
9c9fd67c 256 case CARD_REGISTER:
533f4b68 257 /* TODO: Implement for LPT-support */
258#if 0
9c9fd67c 259 {
533f4b68 260 struct card_register* cr = (struct card_register*)(wdheader->data);
576995a8 261#ifndef NO_WINDRVR
262 ret = (*ioctl_func) (fd, request, wdioctl);
263#endif
9c9fd67c 264 }
533f4b68 265#endif
f1405f13 266 DPRINTF("CARD_REGISTER\n");
da3ba95a 267 break;
2c2119eb 268
da3ba95a 269 case USB_TRANSFER:
f1405f13 270 DPRINTF("in USB_TRANSFER");
da3ba95a 271 {
272 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
273
723d9aa0 274#ifdef DEBUG
f1405f13 275 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
276 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
277 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
278 DPRINTF("setup packet: ");
9c9fd67c 279 hexdump(ut->SetupPacket, 8);
723d9aa0 280
9c9fd67c 281 if (!ut->fRead && ut->dwBufferSize)
282 {
283 hexdump(ut->pBuffer, ut->dwBufferSize);
9c9fd67c 284 }
723d9aa0 285#endif
9c9fd67c 286
795992ad 287#ifndef NO_WINDRVR
9c9fd67c 288 ret = (*ioctl_func) (fd, request, wdioctl);
411af373 289#else
290 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
a70f4f38 291 if (ut->dwPipeNum == 0) { /* control pipe */
292 int requesttype, request, value, index, size;
293 requesttype = ut->SetupPacket[0];
294 request = ut->SetupPacket[1];
295 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
296 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
297 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
f1405f13 298 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
a70f4f38 299 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
300 } else {
301 if (ut->fRead) {
302 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
303
304 } else {
305 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
306 }
307 }
308
411af373 309 if (ret < 0) {
a70f4f38 310 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
411af373 311 } else {
312 ut->dwBytesTransferred = ret;
313 ret = 0;
314 }
292160ed 315#endif
9c9fd67c 316
723d9aa0 317#ifdef DEBUG
f1405f13 318 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 319 if (ut->fRead && ut->dwBytesTransferred)
320 {
f1405f13 321 DPRINTF("Read: ");
9c9fd67c 322 hexdump(ut->pBuffer, ut->dwBytesTransferred);
323 }
723d9aa0 324#endif
da3ba95a 325 }
cdc711dc 326 break;
2c2119eb 327
f152c048 328 case INT_ENABLE_OLD:
da3ba95a 329 case INT_ENABLE:
f1405f13 330 DPRINTF("INT_ENABLE\n");
da3ba95a 331 {
9c9fd67c 332 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 333
f1405f13 334 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
335 it->hInterrupt, it->dwOptions,
336 it->dwCmds, it->fEnableOk, it->dwCounter,
337 it->dwLost, it->fStopped);
cdc711dc 338
9c9fd67c 339 it->fEnableOk = 1;
9ba1e383 340 it->fStopped = 0;
ca18111b 341 ints_enabled = 1;
533f4b68 342 pthread_mutex_trylock(&int_wait);
9c9fd67c 343 }
cdc711dc 344
cdc711dc 345 break;
9c9fd67c 346
da3ba95a 347 case INT_DISABLE:
f1405f13 348 DPRINTF("INT_DISABLE\n");
da3ba95a 349 {
9c9fd67c 350 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 351
f1405f13 352 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
353 it->hInterrupt, it->dwOptions,
354 it->dwCmds, it->fEnableOk, it->dwCounter,
355 it->dwLost, it->fStopped);
795992ad 356#ifndef NO_WINDRVR
9c9fd67c 357 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 358#else
359 it->dwCounter = 0;
360 it->fStopped = 1;
ca18111b 361 ints_enabled = 0;
9ba1e383 362 if (pthread_mutex_trylock(&int_wait) == EBUSY)
363 pthread_mutex_unlock(&int_wait);
292160ed 364#endif
f1405f13 365 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
366 it->hInterrupt, it->dwOptions,
367 it->dwCmds, it->fEnableOk, it->dwCounter,
368 it->dwLost, it->fStopped);
da3ba95a 369 }
da3ba95a 370 break;
da3ba95a 371
9c9fd67c 372 case USB_SET_INTERFACE:
f1405f13 373 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 374 {
9c9fd67c 375 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
376
f1405f13 377 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
378 usi->dwUniqueID, usi->dwInterfaceNum,
379 usi->dwAlternateSetting, usi->dwOptions);
795992ad 380#ifndef NO_WINDRVR
9c9fd67c 381 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 382#else
383 if (usbdevice) {
2a7af812 384 if (!usb_devhandle)
385 usb_devhandle = usb_open(usbdevice);
d0676964 386
387 /* FIXME: Select right interface! */
388 ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
389 if (!ret) {
411af373 390 if(!ret) {
010cbaa6 391 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
411af373 392 ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
393 if (ret)
394 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
395 } else {
396 fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
397 }
d0676964 398 } else {
f1405f13 399 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
400 usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
401 ret, usb_strerror());
d0676964 402 }
2a7af812 403 }
292160ed 404#endif
f1405f13 405 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
406 usi->dwUniqueID, usi->dwInterfaceNum,
407 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 408 }
cdc711dc 409 break;
da3ba95a 410
f152c048 411 case USB_GET_DEVICE_DATA_OLD:
9c9fd67c 412 case USB_GET_DEVICE_DATA:
f1405f13 413 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 414 {
415 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
416 int pSize;
417
f1405f13 418 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
419 ugdd->dwUniqueID, ugdd->dwBytes,
420 ugdd->dwOptions);
421
9c9fd67c 422 pSize = ugdd->dwBytes;
e71b6bf3 423 if (!ugdd->dwBytes) {
2a7af812 424 if (usbdevice) {
f95f1d2e 425 ugdd->dwBytes = usb_deviceinfo(NULL);
e71b6bf3 426 }
427 } else {
f95f1d2e 428 usb_deviceinfo((unsigned char*)ugdd->pBuf);
e71b6bf3 429 }
da3ba95a 430 }
9c9fd67c 431 break;
432
f152c048 433 case EVENT_REGISTER_OLD:
b0f621dd 434 case EVENT_REGISTER:
f1405f13 435 DPRINTF("EVENT_REGISTER\n");
b0f621dd 436 {
437 struct event *e = (struct event*)(wdheader->data);
ac22d975 438 struct usb_bus *bus;
b0f621dd 439 int i;
440
f1405f13 441 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",
442 e->handle, e->dwAction,
443 e->dwStatus, e->dwEventId, e->dwCardType,
444 e->hKernelPlugIn, e->dwOptions,
445 e->u.Usb.deviceId.dwVendorId,
446 e->u.Usb.deviceId.dwProductId,
447 e->u.Usb.dwUniqueID, e->dwEventVer,
448 e->dwNumMatchTables);
449
ac22d975 450 for (i = 0; i < e->dwNumMatchTables; i++) {
f1405f13 451
452 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
453 e->matchTables[i].VendorId,
454 e->matchTables[i].ProductId,
455 e->matchTables[i].bDeviceClass,
456 e->matchTables[i].bDeviceSubClass,
457 e->matchTables[i].bInterfaceClass,
458 e->matchTables[i].bInterfaceSubClass,
459 e->matchTables[i].bInterfaceProtocol);
b0f621dd 460
ac22d975 461 for (bus = busses; bus; bus = bus->next) {
462 struct usb_device *dev;
463
464 for (dev = bus->devices; dev; dev = dev->next) {
465 struct usb_device_descriptor *desc = &(dev->descriptor);
466
467 if((desc->idVendor == e->matchTables[i].VendorId) &&
468 (desc->idProduct == e->matchTables[i].ProductId) &&
469 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
470 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
2a7af812 471 int ac;
472 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
473 struct usb_interface *interface = dev->config[ac].interface;
474 int ai;
475
476 for (ai = 0; ai < interface->num_altsetting; ai++) {
f1405f13 477
478 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
479 interface->altsetting[i].bInterfaceClass,
480 interface->altsetting[i].bInterfaceSubClass,
481 interface->altsetting[i].bInterfaceProtocol);
482
2a7af812 483 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
484 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
485 /* TODO: check interfaceClass! */
f1405f13 486 DPRINTF("found device with libusb\n");
2a7af812 487 usbdevice = dev;
488 card_type = e->dwCardType;
489 }
490 }
491 }
ac22d975 492 }
493 }
494 }
495 }
496
795992ad 497#ifndef NO_WINDRVR
b0f621dd 498 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 499#else
2a7af812 500 e->handle++;
292160ed 501#endif
b0f621dd 502
723d9aa0 503#ifdef DEBUG
f1405f13 504 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",
505 e->handle, e->dwAction,
506 e->dwStatus, e->dwEventId, e->dwCardType,
507 e->hKernelPlugIn, e->dwOptions,
508 e->u.Usb.deviceId.dwVendorId,
509 e->u.Usb.deviceId.dwProductId,
510 e->u.Usb.dwUniqueID, e->dwEventVer,
511 e->dwNumMatchTables);
512
b0f621dd 513 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 514 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
515 e->matchTables[i].VendorId,
516 e->matchTables[i].ProductId,
517 e->matchTables[i].bDeviceClass,
518 e->matchTables[i].bDeviceSubClass,
519 e->matchTables[i].bInterfaceClass,
520 e->matchTables[i].bInterfaceSubClass,
521 e->matchTables[i].bInterfaceProtocol);
723d9aa0 522#endif
b0f621dd 523 }
524 break;
525
f152c048 526 case TRANSFER_OLD:
da3ba95a 527 case TRANSFER:
f1405f13 528 DPRINTF("TRANSFER\n");
795992ad 529#ifndef NO_WINDRVR
9c9fd67c 530 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 531#endif
9c9fd67c 532 break;
533
576995a8 534 case MULTI_TRANSFER:
535 DPRINTF("MULTI_TRANSFER\n");
536#ifndef NO_WINDRVR
537 ret = (*ioctl_func) (fd, request, wdioctl);
538#endif
539 break;
540
da3ba95a 541 case EVENT_UNREGISTER:
f1405f13 542 DPRINTF("EVENT_UNREGISTER\n");
795992ad 543#ifndef NO_WINDRVR
9c9fd67c 544 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 545#endif
9c9fd67c 546 break;
547
da3ba95a 548 case INT_WAIT:
f1405f13 549 DPRINTF("INT_WAIT\n");
b0f621dd 550 {
551 struct interrupt *it = (struct interrupt*)(wdheader->data);
552
f1405f13 553 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
554 it->hInterrupt, it->dwOptions,
555 it->dwCmds, it->fEnableOk, it->dwCounter,
556 it->dwLost, it->fStopped);
b0f621dd 557
795992ad 558#ifndef NO_WINDRVR
b0f621dd 559 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 560#else
2a7af812 561 if (usbdevice) {
562 if (it->dwCounter == 0) {
563 it->dwCounter = 1;
564 } else {
533f4b68 565 pthread_mutex_lock(&int_wait);
be452175 566 pthread_mutex_unlock(&int_wait);
2a7af812 567 }
94038a57 568 } else {
533f4b68 569 pthread_mutex_lock(&int_wait);
be452175 570 pthread_mutex_unlock(&int_wait);
2a7af812 571 }
292160ed 572#endif
573
f1405f13 574 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
575 it->hInterrupt, it->dwOptions, it->dwCmds,
576 it->fEnableOk, it->dwCounter, it->dwLost,
577 it->fStopped);
b0f621dd 578 }
9c9fd67c 579 break;
580
da3ba95a 581 case CARD_UNREGISTER:
f1405f13 582 DPRINTF("CARD_UNREGISTER\n");
795992ad 583#ifndef NO_WINDRVR
9c9fd67c 584 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 585#endif
9c9fd67c 586 break;
587
da3ba95a 588 case EVENT_PULL:
f1405f13 589 DPRINTF("EVENT_PULL\n");
b1831983 590 {
591 struct event *e = (struct event*)(wdheader->data);
723d9aa0 592#ifdef DEBUG
b1831983 593 int i;
594
f1405f13 595 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",
596 e->handle, e->dwAction, e->dwStatus,
597 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
598 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
599 e->u.Usb.deviceId.dwProductId,
600 e->u.Usb.dwUniqueID, e->dwEventVer,
601 e->dwNumMatchTables);
602
b1831983 603 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 604 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
605 e->matchTables[i].VendorId,
606 e->matchTables[i].ProductId,
607 e->matchTables[i].bDeviceClass,
608 e->matchTables[i].bDeviceSubClass,
609 e->matchTables[i].bInterfaceClass,
610 e->matchTables[i].bInterfaceSubClass,
611 e->matchTables[i].bInterfaceProtocol);
723d9aa0 612#endif
b1831983 613
795992ad 614#ifndef NO_WINDRVR
b1831983 615 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 616#else
2a7af812 617 if (usbdevice) {
618 struct usb_interface *interface = usbdevice->config->interface;
292160ed 619
620 e->dwCardType = card_type;
621 e->dwAction = 1;
622 e->dwEventId = 109;
ca18111b 623 e->u.Usb.dwUniqueID = 110;
2a7af812 624 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
625 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
626 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
627 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
292160ed 628 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
629 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
630 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
631 }
632#endif
b1831983 633
723d9aa0 634#ifdef DEBUG
f1405f13 635 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",
636 e->handle, e->dwAction, e->dwStatus,
637 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
638 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
639 e->u.Usb.deviceId.dwProductId,
640 e->u.Usb.dwUniqueID, e->dwEventVer,
641 e->dwNumMatchTables);
642
b1831983 643 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 644 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
645 e->matchTables[i].VendorId,
646 e->matchTables[i].ProductId,
647 e->matchTables[i].bDeviceClass,
648 e->matchTables[i].bDeviceSubClass,
649 e->matchTables[i].bInterfaceClass,
650 e->matchTables[i].bInterfaceSubClass,
651 e->matchTables[i].bInterfaceProtocol);
723d9aa0 652#endif
ca18111b 653
b1831983 654 }
9c9fd67c 655 break;
656
da3ba95a 657 default:
292160ed 658 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 659#ifndef NO_WINDRVR
9c9fd67c 660 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 661#endif
662 break;
cdc711dc 663 }
da3ba95a 664
9c9fd67c 665 return ret;
cdc711dc 666}
667
be452175 668int ioctl(int fd, int request, ...) {
dbda1264 669 va_list args;
670 void *argp;
671 int ret;
672
673 if (!ioctl_func)
f1405f13 674 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 675
676 va_start (args, request);
677 argp = va_arg (args, void *);
678 va_end (args);
679
332ced7a 680 if (fd == windrvrfd)
dbda1264 681 ret = do_wdioctl(fd, request, argp);
682 else
683 ret = (*ioctl_func) (fd, request, argp);
684
685 return ret;
686}
cdc711dc 687
be452175 688int open (const char *pathname, int flags, ...) {
689 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 690 mode_t mode = 0;
691 va_list args;
692 int fd;
693
694 if (!func)
f1405f13 695 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 696
697 if (flags & O_CREAT) {
698 va_start(args, flags);
699 mode = va_arg(args, mode_t);
700 va_end(args);
701 }
702
cdc711dc 703 if (!strcmp (pathname, "/dev/windrvr6")) {
f1405f13 704 DPRINTF("opening windrvr6\n");
795992ad 705#ifdef NO_WINDRVR
01b99d52 706 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
707#else
708 windrvrfd = fd = (*func) (pathname, flags, mode);
709#endif
f10480d1 710 if (!busses) {
711 usb_init();
712 usb_find_busses();
713 usb_find_devices();
714
715 busses = usb_get_busses();
716 }
723d9aa0 717
718 return fd;
cdc711dc 719 }
720
723d9aa0 721 return (*func) (pathname, flags, mode);
cdc711dc 722}
723
723d9aa0 724int close(int fd) {
725 static int (*func) (int) = NULL;
cdc711dc 726
723d9aa0 727 if (!func)
f1405f13 728 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 729
11d01742 730 if (fd == windrvrfd && windrvrfd >= 0) {
f1405f13 731 DPRINTF("close windrvrfd\n");
010cbaa6 732 if (usbinterface >= 0)
733 usb_release_interface(usb_devhandle, usbinterface);
734
735 if (usb_devhandle)
736 usb_close(usb_devhandle);
737
738 usb_devhandle = NULL;
739 usbinterface = -1;
332ced7a 740 windrvrfd = -1;
cdc711dc 741 }
cdc711dc 742
723d9aa0 743 return (*func) (fd);
cdc711dc 744}
745
723d9aa0 746FILE *fopen(const char *path, const char *mode) {
747 FILE *ret;
748 static FILE* (*func) (const char*, const char*) = NULL;
ca18111b 749
750 if (!func)
f1405f13 751 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 752
723d9aa0 753 ret = (*func) (path, mode);
ca18111b 754
723d9aa0 755 if (!strcmp (path, "/proc/modules")) {
f1405f13 756 DPRINTF("opening /proc/modules\n");
723d9aa0 757#ifdef NO_WINDRVR
dbda1264 758 modulesfp = ret;
723d9aa0 759 modules_read = 0;
760#endif
761 }
cdc711dc 762
763 return ret;
764}
765
dbda1264 766char *fgets(char *s, int size, FILE *stream) {
767 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 768 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
dbda1264 769 char *ret = NULL;
770
cdc711dc 771
772 if (!func)
f1405f13 773 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 774
dbda1264 775 if (modulesfp == stream) {
16f6b164 776 if (modules_read < sizeof(modules)) {
777 strcpy(s, modules[modules_read]);
dbda1264 778 ret = s;
16f6b164 779 modules_read++;
dbda1264 780 }
723d9aa0 781 } else {
dbda1264 782 ret = (*func)(s,size,stream);
723d9aa0 783 }
cdc711dc 784
785 return ret;
786}
787
dbda1264 788int fclose(FILE *fp) {
789 static int (*func) (FILE*) = NULL;
cdc711dc 790
dbda1264 791 if (!func)
f1405f13 792 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 793
dbda1264 794 if (fp == modulesfp) {
795 modulesfp = NULL;
796 }
797
798 return (*func)(fp);
cdc711dc 799}
419f2c98 800
801int access(const char *pathname, int mode) {
802 static int (*func) (const char*, int);
803
804 if (!func)
f1405f13 805 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
419f2c98 806
807 if (!strcmp(pathname, "/dev/windrvr6")) {
808 return 0;
809 } else {
810 return (*func)(pathname, mode);
811 }
812}
Impressum, Datenschutz