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