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