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