hint for me how to solve current problem
[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>
da3ba95a 23#include "xilinx.h"
cdc711dc 24
9c9fd67c 25static int (*ioctl_func) (int, int, void *) = NULL;
2c2119eb 26static int windrvrfd = 0;
f10480d1 27static struct usb_bus *busses = NULL;
2a7af812 28static struct usb_device *usbdevice;
29static usb_dev_handle *usb_devhandle = NULL;
292160ed 30static unsigned long card_type;
31
795992ad 32#define NO_WINDRVR 1
cdc711dc 33
34void hexdump(unsigned char *buf, int len);
da3ba95a 35void diff(unsigned char *buf1, unsigned char *buf2, int len);
cdc711dc 36
792bf5f2 37//unique: 94, bytes: 276, options: 0
38//Vendor: 3fd
39//12 01 00 02 00 00 00 40 fd 03 08 00 00 00 01 02 12 01 00 02 00 00 00 40 fd 03 08 00 00 00 01 02
40//00 01 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 40 00 00 00 00 00 00 00
3664a3e3 41//03 00 00 00 00 00 00 00 38 45 21 08 38 45 21 08 03 00 00 00 00 00 00 00 38 45 21 08 38 45 21 08
42//4c 45 21 08 00 00 00 00 00 00 00 00 00 00 00 00 4c 45 21 08 00 00 00 00 00 00 00 00 00 00 00 00
792bf5f2 43//00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
44//00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
45//00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
46//00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
47//00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
48//00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
49//00 00 00 00 00 00 00 00 09 02 20 00 01 02 00 80 00 00 00 00 00 00 00 00 09 02 20 00 01 02 00 80
3664a3e3 50//8c 00 00 00 01 00 00 00 4c 45 21 08 58 45 21 08 8c 00 00 00 01 00 00 00 4c 45 21 08 58 45 21 08
51//01 00 00 00 58 45 21 08 09 04 00 00 02 ff 00 00 01 00 00 00 58 45 21 08 09 04 00 00 02 ff 00 00
52//00 00 00 00 6c 45 21 08 7c 45 21 08 07 05 02 02 00 00 00 00 6c 45 21 08 7c 45 21 08 07 05 02 02
792bf5f2 53//00 02 00 00 07 05 86 02 00 02 00 00 02 00 00 00 00 02 00 00 07 05 86 02 00 02 00 00 02 00 00 00
54//00 02 00 00 02 00 00 00 02 00 00 00 00 00 00 00 00 02 00 00 02 00 00 00 02 00 00 00 00 00 00 00
55//86 00 00 00 00 02 00 00 02 00 00 00 01 00 00 00 86 00 00 00 00 02 00 00 02 00 00 00 01 00 00 00
56//00 00 00 00 00 00 00 00
f95f1d2e 57int usb_deviceinfo(unsigned char *buf) {
58 int i,j,k,l;
59 int len = 0;
3664a3e3 60 WDU_CONFIGURATION **pConfigs, **pActiveConfig;
61 WDU_INTERFACE **pActiveInterface;
f95f1d2e 62
63 if (buf) {
64 struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
65
66 udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR);
2a7af812 67 udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType;
68 udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB;
69 udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass;
70 udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
71 udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol;
72 udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0;
73 udi->Descriptor.idVendor = usbdevice->descriptor.idVendor;
74 udi->Descriptor.idProduct = usbdevice->descriptor.idProduct;
75 udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice;
76 udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer;
77 udi->Descriptor.iProduct = usbdevice->descriptor.iProduct;
78 udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber;
79 udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations;
f95f1d2e 80
81 /* TODO: Fix Pipe0! */
82 udi->Pipe0.dwNumber = 0x00;
2a7af812 83 udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0;
f95f1d2e 84 udi->Pipe0.type = 0;
85 udi->Pipe0.direction = 3;
86 udi->Pipe0.dwInterval = 0;
3664a3e3 87
88 pConfigs = &(udi->pConfigs);
89 pActiveConfig = &(udi->pActiveConfig);
90 pActiveInterface = &(udi->pActiveInterface[0]);
f95f1d2e 91 }
92
93 len = sizeof(struct usb_device_info);
94
2a7af812 95 for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++)
f95f1d2e 96 {
2a7af812 97 struct usb_config_descriptor *conf_desc = &usbdevice->config[i];
3664a3e3 98 WDU_INTERFACE **pInterfaces;
99 WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces];
100 WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces];
101
f95f1d2e 102 if (buf) {
103 WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len);
104
3664a3e3 105 *pConfigs = cfg;
106 *pActiveConfig = cfg;
107
f95f1d2e 108 cfg->Descriptor.bLength = conf_desc->bLength;
109 cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType;
110 cfg->Descriptor.wTotalLength = conf_desc->wTotalLength;
111 cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces;
112 cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue;
113 cfg->Descriptor.iConfiguration = conf_desc->iConfiguration;
114 cfg->Descriptor.bmAttributes = conf_desc->bmAttributes;
115 cfg->Descriptor.MaxPower = conf_desc->MaxPower;
116
117 cfg->dwNumInterfaces = conf_desc->bNumInterfaces;
3664a3e3 118
119 pInterfaces = &(cfg->pInterfaces);
f95f1d2e 120 }
121 len += sizeof(WDU_CONFIGURATION);
3664a3e3 122
f95f1d2e 123 if (buf) {
3664a3e3 124 *pInterfaces = (WDU_INTERFACE*)(buf+len);
f95f1d2e 125 for (j=0; j<conf_desc->bNumInterfaces; j++) {
126 WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len);
3664a3e3 127
128 pActiveInterface[j] = iface;
129
130 pAlternateSettings[j] = &(iface->pAlternateSettings);
2a7af812 131 iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting;
3664a3e3 132 pActiveAltSetting[j] = &(iface->pActiveAltSetting);
f95f1d2e 133
134 len += sizeof(WDU_INTERFACE);
135 }
136 } else {
137 len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces;
138 }
139
140 for (j=0; j<conf_desc->bNumInterfaces; j++)
141 {
2a7af812 142 struct usb_interface *interface = &usbdevice->config[i].interface[j];
3664a3e3 143
144 if (buf) {
145 *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
146 /* FIXME: */
147 *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len);
148 }
149
f95f1d2e 150 for(k=0; k<interface->num_altsetting; k++)
151 {
3664a3e3 152 unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints;
153 WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors;
154 WDU_PIPE_INFO **pPipes;
155
f95f1d2e 156 if (buf) {
157 WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len);
158
159 altset->Descriptor.bLength = interface->altsetting[k].bLength;
160 altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType;
161 altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber;
162 altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting;
163 altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints;
164 altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass;
165 altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass;
166 altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol;
167 altset->Descriptor.iInterface = interface->altsetting[k].iInterface;
3664a3e3 168 pEndpointDescriptors = &(altset->pEndpointDescriptors);
169 pPipes = &(altset->pPipes);
f95f1d2e 170
171 }
172 len +=sizeof(WDU_ALTERNATE_SETTING);
173
174 if (buf) {
3664a3e3 175 *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 176 for (l = 0; l < bNumEndpoints; l++) {
177 WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len);
f95f1d2e 178
179 ed->bLength = interface->altsetting[k].endpoint[l].bLength;
180 ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType;
181 ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress;
182 ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes;
183 ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize;
184 ed->bInterval = interface->altsetting[k].endpoint[l].bInterval;
185
186 len += sizeof(WDU_ENDPOINT_DESCRIPTOR);
792bf5f2 187 }
f95f1d2e 188
3664a3e3 189 *pPipes = (WDU_PIPE_INFO*)(buf+len);
792bf5f2 190 for (l = 0; l < bNumEndpoints; l++) {
191 WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len);
f95f1d2e 192
193 pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress;
194 pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize);
195 pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK;
196 if (pi->type == PIPE_TYPE_CONTROL)
197 pi->direction = WDU_DIR_IN_OUT;
198 else
199 {
200 pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT;
201 }
202
203 pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval;
204
205 len += sizeof(WDU_PIPE_INFO);
206 }
207 } else {
208 len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints;
209 }
210 }
211 }
212 }
213
214 return len;
215}
216
9c9fd67c 217int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 218 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 219 struct version_struct *version;
220 int ret = 0;
cdc711dc 221
da3ba95a 222 if (wdheader->magic != MAGIC) {
2c2119eb 223 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
224 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 225 }
226
cdc711dc 227 switch(request) {
da3ba95a 228 case VERSION:
9c9fd67c 229 version = (struct version_struct*)(wdheader->data);
230 strcpy(version->version, "WinDriver no more");
231 version->versionul = 999;
232 fprintf(stderr,"faking VERSION\n");
cdc711dc 233 break;
2c2119eb 234
235 case LICENSE:
236 fprintf(stderr,"faking LICENSE\n");
237 break;
238
9c9fd67c 239 case CARD_REGISTER:
240 {
2c2119eb 241 //struct card_register* cr = (struct card_register*)(wdheader->data);
9c9fd67c 242 /* Todo: LPT-Port already in use */
243 }
244 fprintf(stderr,"faking CARD_REGISTER\n");
da3ba95a 245 break;
2c2119eb 246
da3ba95a 247 case USB_TRANSFER:
9c9fd67c 248 fprintf(stderr,"in USB_TRANSFER");
da3ba95a 249 {
250 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
251
2c2119eb 252 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 253 fprintf(stderr,"setup packet: ");
254 hexdump(ut->SetupPacket, 8);
255 fprintf(stderr,"\n");
256 if (!ut->fRead && ut->dwBufferSize)
257 {
258 hexdump(ut->pBuffer, ut->dwBufferSize);
259 fprintf(stderr,"\n");
260 }
261
795992ad 262#ifndef NO_WINDRVR
9c9fd67c 263 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 264#endif
9c9fd67c 265
2c2119eb 266 fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 267 if (ut->fRead && ut->dwBytesTransferred)
268 {
269 fprintf(stderr,"Read: ");
270 hexdump(ut->pBuffer, ut->dwBytesTransferred);
271 }
da3ba95a 272 fprintf(stderr,"\n");
273 }
cdc711dc 274 break;
2c2119eb 275
da3ba95a 276 case INT_ENABLE:
9c9fd67c 277 fprintf(stderr,"faking INT_ENABLE");
da3ba95a 278 {
9c9fd67c 279 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 280
53f639f2 281 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);
cdc711dc 282
9c9fd67c 283 it->fEnableOk = 1;
284 //ret = (*ioctl_func) (fd, request, wdioctl);
285 }
cdc711dc 286
cdc711dc 287 break;
9c9fd67c 288
da3ba95a 289 case INT_DISABLE:
9c9fd67c 290 fprintf(stderr,"INT_DISABLE\n");
da3ba95a 291 {
9c9fd67c 292 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 293
53f639f2 294 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);
795992ad 295#ifndef NO_WINDRVR
9c9fd67c 296 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 297#else
298 it->dwCounter = 0;
299 it->fStopped = 1;
292160ed 300#endif
53f639f2 301 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);
da3ba95a 302 }
da3ba95a 303 break;
da3ba95a 304
9c9fd67c 305 case USB_SET_INTERFACE:
306 fprintf(stderr,"USB_SET_INTERFACE\n");
da3ba95a 307 {
9c9fd67c 308 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
309
2c2119eb 310 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
795992ad 311#ifndef NO_WINDRVR
9c9fd67c 312 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 313#else
314 if (usbdevice) {
315 int iface;
316
317 if (!usb_devhandle)
318 usb_devhandle = usb_open(usbdevice);
319//MGMG
320 ret = usb_claim_interface(usb_devhandle, iface);
321 }
292160ed 322#endif
2a7af812 323 fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 324 }
cdc711dc 325 break;
da3ba95a 326
9c9fd67c 327 case USB_GET_DEVICE_DATA:
328 fprintf(stderr,"USB_GET_DEVICE_DATA\n");
329 {
330 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
331 int pSize;
332
e71b6bf3 333 fprintf(stderr, "unique: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions);
9c9fd67c 334 pSize = ugdd->dwBytes;
795992ad 335#ifndef NO_WINDRVR
9c9fd67c 336 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 337#else
338 if (!ugdd->dwBytes) {
2a7af812 339 if (usbdevice) {
f95f1d2e 340 ugdd->dwBytes = usb_deviceinfo(NULL);
e71b6bf3 341 }
342 } else {
f95f1d2e 343 usb_deviceinfo((unsigned char*)ugdd->pBuf);
e71b6bf3 344 }
292160ed 345#endif
9c9fd67c 346 if (pSize) {
e71b6bf3 347 struct usb_device_info *udi = (struct usb_device_info*)ugdd->pBuf;
348
349 fprintf(stderr, "Vendor: %x\n", udi->Descriptor.idVendor);
350
9c9fd67c 351 hexdump(ugdd->pBuf, pSize);
352 fprintf(stderr, "\n");
da3ba95a 353 }
354 }
9c9fd67c 355 break;
356
b0f621dd 357 case EVENT_REGISTER:
358 fprintf(stderr,"EVENT_REGISTER\n");
359 {
360 struct event *e = (struct event*)(wdheader->data);
ac22d975 361 struct usb_bus *bus;
b0f621dd 362 int i;
363
364 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);
ac22d975 365 for (i = 0; i < e->dwNumMatchTables; i++) {
e71b6bf3 366 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);
b0f621dd 367
ac22d975 368 for (bus = busses; bus; bus = bus->next) {
369 struct usb_device *dev;
370
371 for (dev = bus->devices; dev; dev = dev->next) {
372 struct usb_device_descriptor *desc = &(dev->descriptor);
373
374 if((desc->idVendor == e->matchTables[i].VendorId) &&
375 (desc->idProduct == e->matchTables[i].ProductId) &&
376 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
377 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) {
2a7af812 378 int ac;
379 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
380 struct usb_interface *interface = dev->config[ac].interface;
381 int ai;
382
383 for (ai = 0; ai < interface->num_altsetting; ai++) {
384 fprintf(stderr, "intclass: %x, intsubclass: %x, intproto: %x\n", interface->altsetting[i].bInterfaceClass, interface->altsetting[i].bInterfaceSubClass, interface->altsetting[i].bInterfaceProtocol);
385 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
386 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
387 /* TODO: check interfaceClass! */
388 fprintf(stderr,"!!!FOUND DEVICE WITH LIBUSB!!!\n");
389 usbdevice = dev;
390 card_type = e->dwCardType;
391 }
392 }
393 }
ac22d975 394 }
395 }
396 }
397 }
398
795992ad 399#ifndef NO_WINDRVR
b0f621dd 400 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 401#else
402//handle: 0, action: 16371, status: 0, eventid: 0, cardtype: 4294967294, kplug: 0, options: 0, dev: 0:0, unique: 0, ver: 1, nummatch: 2
403//handle: 1, action: 16371, status: 0, eventid: 0, cardtype: 4294967294, kplug: 0, options: 0, dev: 0:0, unique: 0, ver: 1, nummatch: 2
404 e->handle++;
292160ed 405#endif
b0f621dd 406
407 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);
408 for (i = 0; i < e->dwNumMatchTables; i++)
e71b6bf3 409 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);
b0f621dd 410 }
411 break;
412
da3ba95a 413 case TRANSFER:
9c9fd67c 414 fprintf(stderr,"TRANSFER\n");
795992ad 415#ifndef NO_WINDRVR
9c9fd67c 416 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 417#endif
9c9fd67c 418 break;
419
da3ba95a 420 case EVENT_UNREGISTER:
9c9fd67c 421 fprintf(stderr,"EVENT_UNREGISTER\n");
795992ad 422#ifndef NO_WINDRVR
9c9fd67c 423 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 424#endif
9c9fd67c 425 break;
426
da3ba95a 427 case INT_WAIT:
9c9fd67c 428 fprintf(stderr,"INT_WAIT\n");
b0f621dd 429 {
430 struct interrupt *it = (struct interrupt*)(wdheader->data);
431
53f639f2 432 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);
b0f621dd 433
795992ad 434#ifndef NO_WINDRVR
b0f621dd 435 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 436#else
2a7af812 437 if (usbdevice) {
438 if (it->dwCounter == 0) {
439 it->dwCounter = 1;
440 } else {
adfa5221 441 //FIXME: signal durch FUTEX, overload futex!
2a7af812 442 kill(getpid(), SIGHUP);
443 }
94038a57 444 } else {
445 kill(getpid(), SIGHUP);
2a7af812 446 }
292160ed 447#endif
448
53f639f2 449 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);
b0f621dd 450 }
9c9fd67c 451 break;
452
da3ba95a 453 case CARD_UNREGISTER:
9c9fd67c 454 fprintf(stderr,"CARD_UNREGISTER\n");
795992ad 455#ifndef NO_WINDRVR
9c9fd67c 456 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 457#endif
9c9fd67c 458 break;
459
da3ba95a 460 case EVENT_PULL:
9c9fd67c 461 fprintf(stderr,"EVENT_PULL\n");
b1831983 462 {
463 struct event *e = (struct event*)(wdheader->data);
464 int i;
465
2a7af812 466 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 467 for (i = 0; i < e->dwNumMatchTables; i++)
e71b6bf3 468 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);
b1831983 469
795992ad 470#ifndef NO_WINDRVR
b1831983 471 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 472#else
473//EVENT_PULL
474//handle: 1, action: 0, status: 0, eventid: 0, cardtype: 0, kplug: 0, options: 0, dev: 0:0, unique: 0, ver: 1, nummatch: 1
475//match: dev: 0:0, class: 0, subclass: 0, intclass: 0, intsubclass: 0, intproto: 0
476//handle: 1, action: 1, status: 0, eventid: 109, cardtype: 4294967294, kplug: 0, options: 0, dev: 0:0, unique: 90, ver: 1, nummatch: 1
477//match: dev: 3fd:8, class: 0, subclass: 0, intclass: ff, intsubclass: 0, intproto: 0
2a7af812 478 if (usbdevice) {
479 struct usb_interface *interface = usbdevice->config->interface;
292160ed 480
481 e->dwCardType = card_type;
482 e->dwAction = 1;
483 e->dwEventId = 109;
484 e->u.Usb.dwUniqueID = 4711;
2a7af812 485 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
486 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
487 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
488 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
292160ed 489 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
490 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
491 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
492 }
493#endif
b1831983 494
2a7af812 495 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 496 for (i = 0; i < e->dwNumMatchTables; i++)
e71b6bf3 497 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);
b1831983 498 }
9c9fd67c 499 break;
500
da3ba95a 501 default:
292160ed 502 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 503#ifndef NO_WINDRVR
9c9fd67c 504 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 505#endif
506 break;
cdc711dc 507 }
da3ba95a 508
9c9fd67c 509 return ret;
cdc711dc 510}
511
512
513typedef int (*open_funcptr_t) (const char *, int, mode_t);
514
cdc711dc 515int open (const char *pathname, int flags, ...)
516{
517 static open_funcptr_t func = NULL;
518 mode_t mode = 0;
519 va_list args;
520 int fd;
521
522 if (!func)
523 func = (open_funcptr_t) dlsym (REAL_LIBC, "open");
524
525 if (flags & O_CREAT) {
526 va_start(args, flags);
527 mode = va_arg(args, mode_t);
528 va_end(args);
529 }
530
cdc711dc 531 if (!strcmp (pathname, "/dev/windrvr6")) {
532 fprintf(stderr,"opening windrvr6\n");
795992ad 533#ifdef NO_WINDRVR
01b99d52 534 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
535#else
536 windrvrfd = fd = (*func) (pathname, flags, mode);
537#endif
f10480d1 538 if (!busses) {
539 usb_init();
540 usb_find_busses();
541 usb_find_devices();
542
543 busses = usb_get_busses();
544 }
01b99d52 545 } else {
546 fd = (*func) (pathname, flags, mode);
cdc711dc 547 }
548
549 return fd;
550}
551
552void diff(unsigned char *buf1, unsigned char *buf2, int len) {
553 int i;
554
555 for(i=0; i<len; i++) {
556 if (buf1[i] != buf2[i]) {
557 fprintf(stderr,"Diff at %d: %02x(%c)->%02x(%c)\n", i, buf1[i], ((buf1[i] >= 31 && buf1[i] <= 126)?buf1[i]:'.'), buf2[i], ((buf2[i] >= 31 && buf2[i] <= 126)?buf2[i]:'.'));
558 }
559 }
560}
561
562void hexdump(unsigned char *buf, int len) {
563 int i;
564
565 for(i=0; i<len; i++) {
566 fprintf(stderr,"%02x ", buf[i]);
567 if ((i % 16) == 15)
568 fprintf(stderr,"\n");
569 }
570}
571
572int ioctl(int fd, int request, ...)
573{
cdc711dc 574 va_list args;
575 void *argp;
cdc711dc 576 int ret;
577
9c9fd67c 578 if (!ioctl_func)
579 ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl");
cdc711dc 580
581 va_start (args, request);
582 argp = va_arg (args, void *);
583 va_end (args);
584
9c9fd67c 585 if (fd == windrvrfd)
586 ret = do_wdioctl(fd, request, argp);
587 else
588 ret = (*ioctl_func) (fd, request, argp);
cdc711dc 589
cdc711dc 590 return ret;
591}
592
da3ba95a 593#if 0
cdc711dc 594void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
595{
596 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
597 void *ret;
598
599 if (!func)
600 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap");
601
602 ret = (*func) (start, length, prot, flags, fd, offset);
603 fprintf(stderr,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret);
604 mmapped = ret;
605 mmapplen = length;
606
607 return ret;
608}
609
610void *mmap64(void *start, size_t length, int prot, int flags, int fd, off64_t offset)
611{
612 static void* (*func) (void *, size_t, int, int, int, off64_t) = NULL;
613 void *ret;
614
615 if (!func)
616 func = (void* (*) (void *, size_t, int, int, int, off64_t)) dlsym (REAL_LIBC, "mmap64");
617
618 ret = (*func) (start, length, prot, flags, fd, offset);
619 fprintf(stderr,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret);
620 mmapped = ret;
621 mmapplen = length;
622
623 return ret;
624}
625
626void *mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
627{
628 static void* (*func) (void *, size_t, int, int, int, off_t) = NULL;
629 void *ret;
630
631 if (!func)
632 func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap2");
633
634 ret = (*func) (start, length, prot, flags, fd, pgoffset);
635 fprintf(stderr,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, pgoffset, (unsigned int)ret);
636 mmapped = ret;
637 mmapplen = length;
638
639 return ret;
640}
641
642void *malloc(size_t size)
643{
644 static void* (*func) (size_t) = NULL;
645 void *ret;
646
647 if (!func)
648 func = (void* (*) (size_t)) dlsym(REAL_LIBC, "malloc");
649
650 ret = (*func) (size);
651
652 //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret);
653
654 return ret;
655}
da3ba95a 656#endif
cdc711dc 657
658
659#endif
Impressum, Datenschutz