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