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