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