f1405f13 |
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 | */ |
cdc711dc |
23 | |
24 | #define _GNU_SOURCE 1 |
25 | |
cdc711dc |
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> |
f10480d1 |
36 | #include <usb.h> |
2a7af812 |
37 | #include <signal.h> |
533f4b68 |
38 | #include <pthread.h> |
9ba1e383 |
39 | #include <errno.h> |
11d01742 |
40 | #include <inttypes.h> |
2c0c1255 |
41 | #include "usb-driver.h" |
cdc711dc |
42 | |
9c9fd67c |
43 | static int (*ioctl_func) (int, int, void *) = NULL; |
332ced7a |
44 | static int windrvrfd = -1; |
45 | FILE *modulesfp = NULL; |
723d9aa0 |
46 | static int modules_read = 0; |
f10480d1 |
47 | static struct usb_bus *busses = NULL; |
2a7af812 |
48 | static struct usb_device *usbdevice; |
49 | static usb_dev_handle *usb_devhandle = NULL; |
010cbaa6 |
50 | static int usbinterface = -1; |
b72b86b4 |
51 | static unsigned long card_type; |
ca18111b |
52 | static int ints_enabled = 0; |
533f4b68 |
53 | static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER; |
292160ed |
54 | |
795992ad |
55 | #define NO_WINDRVR 1 |
723d9aa0 |
56 | |
be452175 |
57 | #ifdef DEBUG |
f1405f13 |
58 | #define DPRINTF(format, args...) fprintf(stderr, format, ##args) |
723d9aa0 |
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 | } |
f1405f13 |
67 | fprintf(stderr,"\n"); |
723d9aa0 |
68 | } |
f1405f13 |
69 | #else |
70 | #define DPRINTF(format, args...) |
be452175 |
71 | #endif |
cdc711dc |
72 | |
f95f1d2e |
73 | int usb_deviceinfo(unsigned char *buf) { |
74 | int i,j,k,l; |
75 | int len = 0; |
3664a3e3 |
76 | WDU_CONFIGURATION **pConfigs, **pActiveConfig; |
77 | WDU_INTERFACE **pActiveInterface; |
f95f1d2e |
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); |
2a7af812 |
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; |
f95f1d2e |
96 | |
97 | /* TODO: Fix Pipe0! */ |
98 | udi->Pipe0.dwNumber = 0x00; |
2a7af812 |
99 | udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0; |
f95f1d2e |
100 | udi->Pipe0.type = 0; |
8923df66 |
101 | udi->Pipe0.direction = WDU_DIR_IN_OUT; |
f95f1d2e |
102 | udi->Pipe0.dwInterval = 0; |
3664a3e3 |
103 | |
104 | pConfigs = &(udi->pConfigs); |
105 | pActiveConfig = &(udi->pActiveConfig); |
106 | pActiveInterface = &(udi->pActiveInterface[0]); |
f95f1d2e |
107 | } |
108 | |
109 | len = sizeof(struct usb_device_info); |
110 | |
2a7af812 |
111 | for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++) |
f95f1d2e |
112 | { |
2a7af812 |
113 | struct usb_config_descriptor *conf_desc = &usbdevice->config[i]; |
3664a3e3 |
114 | WDU_INTERFACE **pInterfaces; |
115 | WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces]; |
116 | WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces]; |
117 | |
f95f1d2e |
118 | if (buf) { |
119 | WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len); |
120 | |
3664a3e3 |
121 | *pConfigs = cfg; |
122 | *pActiveConfig = cfg; |
123 | |
f95f1d2e |
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; |
3664a3e3 |
134 | |
135 | pInterfaces = &(cfg->pInterfaces); |
f95f1d2e |
136 | } |
137 | len += sizeof(WDU_CONFIGURATION); |
3664a3e3 |
138 | |
f95f1d2e |
139 | if (buf) { |
3664a3e3 |
140 | *pInterfaces = (WDU_INTERFACE*)(buf+len); |
f95f1d2e |
141 | for (j=0; j<conf_desc->bNumInterfaces; j++) { |
142 | WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len); |
3664a3e3 |
143 | |
144 | pActiveInterface[j] = iface; |
145 | |
146 | pAlternateSettings[j] = &(iface->pAlternateSettings); |
2a7af812 |
147 | iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting; |
3664a3e3 |
148 | pActiveAltSetting[j] = &(iface->pActiveAltSetting); |
f95f1d2e |
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 | { |
2a7af812 |
158 | struct usb_interface *interface = &usbdevice->config[i].interface[j]; |
3664a3e3 |
159 | |
160 | if (buf) { |
161 | *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len); |
162 | /* FIXME: */ |
163 | *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len); |
164 | } |
165 | |
f95f1d2e |
166 | for(k=0; k<interface->num_altsetting; k++) |
167 | { |
3664a3e3 |
168 | unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints; |
169 | WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors; |
170 | WDU_PIPE_INFO **pPipes; |
171 | |
f95f1d2e |
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; |
3664a3e3 |
184 | pEndpointDescriptors = &(altset->pEndpointDescriptors); |
185 | pPipes = &(altset->pPipes); |
f95f1d2e |
186 | |
187 | } |
188 | len +=sizeof(WDU_ALTERNATE_SETTING); |
189 | |
190 | if (buf) { |
3664a3e3 |
191 | *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len); |
f95f1d2e |
192 | for (l = 0; l < bNumEndpoints; l++) { |
193 | WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len); |
f95f1d2e |
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); |
792bf5f2 |
203 | } |
f95f1d2e |
204 | |
3664a3e3 |
205 | *pPipes = (WDU_PIPE_INFO*)(buf+len); |
792bf5f2 |
206 | for (l = 0; l < bNumEndpoints; l++) { |
207 | WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len); |
f95f1d2e |
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 | |
9c9fd67c |
233 | int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) { |
cdc711dc |
234 | struct header_struct* wdheader = (struct header_struct*)wdioctl; |
9c9fd67c |
235 | struct version_struct *version; |
236 | int ret = 0; |
cdc711dc |
237 | |
da3ba95a |
238 | if (wdheader->magic != MAGIC) { |
2c2119eb |
239 | fprintf(stderr,"!!!ERROR: magic header does not match!!!\n"); |
240 | return (*ioctl_func) (fd, request, wdioctl); |
cdc711dc |
241 | } |
242 | |
bdc65937 |
243 | switch(request & ~(0xc0000000)) { |
da3ba95a |
244 | case VERSION: |
9c9fd67c |
245 | version = (struct version_struct*)(wdheader->data); |
246 | strcpy(version->version, "WinDriver no more"); |
90831fba |
247 | version->versionul = 802; |
f1405f13 |
248 | DPRINTF("VERSION\n"); |
cdc711dc |
249 | break; |
2c2119eb |
250 | |
251 | case LICENSE: |
f1405f13 |
252 | DPRINTF("LICENSE\n"); |
2c2119eb |
253 | break; |
254 | |
f152c048 |
255 | case CARD_REGISTER_OLD: |
9c9fd67c |
256 | case CARD_REGISTER: |
533f4b68 |
257 | /* TODO: Implement for LPT-support */ |
258 | #if 0 |
9c9fd67c |
259 | { |
533f4b68 |
260 | struct card_register* cr = (struct card_register*)(wdheader->data); |
9c9fd67c |
261 | } |
533f4b68 |
262 | #endif |
f1405f13 |
263 | DPRINTF("CARD_REGISTER\n"); |
da3ba95a |
264 | break; |
2c2119eb |
265 | |
da3ba95a |
266 | case USB_TRANSFER: |
f1405f13 |
267 | DPRINTF("in USB_TRANSFER"); |
da3ba95a |
268 | { |
269 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); |
270 | |
723d9aa0 |
271 | #ifdef DEBUG |
f1405f13 |
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: "); |
9c9fd67c |
276 | hexdump(ut->SetupPacket, 8); |
723d9aa0 |
277 | |
9c9fd67c |
278 | if (!ut->fRead && ut->dwBufferSize) |
279 | { |
280 | hexdump(ut->pBuffer, ut->dwBufferSize); |
9c9fd67c |
281 | } |
723d9aa0 |
282 | #endif |
9c9fd67c |
283 | |
795992ad |
284 | #ifndef NO_WINDRVR |
9c9fd67c |
285 | ret = (*ioctl_func) (fd, request, wdioctl); |
411af373 |
286 | #else |
287 | /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */ |
a70f4f38 |
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); |
f1405f13 |
295 | DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size); |
a70f4f38 |
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 | |
411af373 |
306 | if (ret < 0) { |
a70f4f38 |
307 | fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror()); |
411af373 |
308 | } else { |
309 | ut->dwBytesTransferred = ret; |
310 | ret = 0; |
311 | } |
292160ed |
312 | #endif |
9c9fd67c |
313 | |
723d9aa0 |
314 | #ifdef DEBUG |
f1405f13 |
315 | DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); |
9c9fd67c |
316 | if (ut->fRead && ut->dwBytesTransferred) |
317 | { |
f1405f13 |
318 | DPRINTF("Read: "); |
9c9fd67c |
319 | hexdump(ut->pBuffer, ut->dwBytesTransferred); |
320 | } |
723d9aa0 |
321 | #endif |
da3ba95a |
322 | } |
cdc711dc |
323 | break; |
2c2119eb |
324 | |
f152c048 |
325 | case INT_ENABLE_OLD: |
da3ba95a |
326 | case INT_ENABLE: |
f1405f13 |
327 | DPRINTF("INT_ENABLE\n"); |
da3ba95a |
328 | { |
9c9fd67c |
329 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
cdc711dc |
330 | |
f1405f13 |
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); |
cdc711dc |
335 | |
9c9fd67c |
336 | it->fEnableOk = 1; |
9ba1e383 |
337 | it->fStopped = 0; |
ca18111b |
338 | ints_enabled = 1; |
533f4b68 |
339 | pthread_mutex_trylock(&int_wait); |
9c9fd67c |
340 | } |
cdc711dc |
341 | |
cdc711dc |
342 | break; |
9c9fd67c |
343 | |
da3ba95a |
344 | case INT_DISABLE: |
f1405f13 |
345 | DPRINTF("INT_DISABLE\n"); |
da3ba95a |
346 | { |
9c9fd67c |
347 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
da3ba95a |
348 | |
f1405f13 |
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); |
795992ad |
353 | #ifndef NO_WINDRVR |
9c9fd67c |
354 | ret = (*ioctl_func) (fd, request, wdioctl); |
e71b6bf3 |
355 | #else |
356 | it->dwCounter = 0; |
357 | it->fStopped = 1; |
ca18111b |
358 | ints_enabled = 0; |
9ba1e383 |
359 | if (pthread_mutex_trylock(&int_wait) == EBUSY) |
360 | pthread_mutex_unlock(&int_wait); |
292160ed |
361 | #endif |
f1405f13 |
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); |
da3ba95a |
366 | } |
da3ba95a |
367 | break; |
da3ba95a |
368 | |
9c9fd67c |
369 | case USB_SET_INTERFACE: |
f1405f13 |
370 | DPRINTF("USB_SET_INTERFACE\n"); |
da3ba95a |
371 | { |
9c9fd67c |
372 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); |
373 | |
f1405f13 |
374 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", |
375 | usi->dwUniqueID, usi->dwInterfaceNum, |
376 | usi->dwAlternateSetting, usi->dwOptions); |
795992ad |
377 | #ifndef NO_WINDRVR |
9c9fd67c |
378 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 |
379 | #else |
380 | if (usbdevice) { |
2a7af812 |
381 | if (!usb_devhandle) |
382 | usb_devhandle = usb_open(usbdevice); |
d0676964 |
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) { |
411af373 |
387 | if(!ret) { |
010cbaa6 |
388 | usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber; |
411af373 |
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 | } |
d0676964 |
395 | } else { |
f1405f13 |
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()); |
d0676964 |
399 | } |
2a7af812 |
400 | } |
292160ed |
401 | #endif |
f1405f13 |
402 | DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", |
403 | usi->dwUniqueID, usi->dwInterfaceNum, |
404 | usi->dwAlternateSetting, usi->dwOptions); |
da3ba95a |
405 | } |
cdc711dc |
406 | break; |
da3ba95a |
407 | |
f152c048 |
408 | case USB_GET_DEVICE_DATA_OLD: |
9c9fd67c |
409 | case USB_GET_DEVICE_DATA: |
f1405f13 |
410 | DPRINTF("USB_GET_DEVICE_DATA\n"); |
9c9fd67c |
411 | { |
412 | struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data); |
413 | int pSize; |
414 | |
f1405f13 |
415 | DPRINTF("unique: %lu, bytes: %lu, options: %lx\n", |
416 | ugdd->dwUniqueID, ugdd->dwBytes, |
417 | ugdd->dwOptions); |
418 | |
9c9fd67c |
419 | pSize = ugdd->dwBytes; |
e71b6bf3 |
420 | if (!ugdd->dwBytes) { |
2a7af812 |
421 | if (usbdevice) { |
f95f1d2e |
422 | ugdd->dwBytes = usb_deviceinfo(NULL); |
e71b6bf3 |
423 | } |
424 | } else { |
f95f1d2e |
425 | usb_deviceinfo((unsigned char*)ugdd->pBuf); |
e71b6bf3 |
426 | } |
da3ba95a |
427 | } |
9c9fd67c |
428 | break; |
429 | |
f152c048 |
430 | case EVENT_REGISTER_OLD: |
b0f621dd |
431 | case EVENT_REGISTER: |
f1405f13 |
432 | DPRINTF("EVENT_REGISTER\n"); |
b0f621dd |
433 | { |
434 | struct event *e = (struct event*)(wdheader->data); |
ac22d975 |
435 | struct usb_bus *bus; |
b0f621dd |
436 | int i; |
437 | |
f1405f13 |
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 | |
ac22d975 |
447 | for (i = 0; i < e->dwNumMatchTables; i++) { |
f1405f13 |
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); |
b0f621dd |
457 | |
ac22d975 |
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)) { |
2a7af812 |
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++) { |
f1405f13 |
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 | |
2a7af812 |
480 | if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) && |
481 | (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){ |
482 | /* TODO: check interfaceClass! */ |
f1405f13 |
483 | DPRINTF("found device with libusb\n"); |
2a7af812 |
484 | usbdevice = dev; |
485 | card_type = e->dwCardType; |
486 | } |
487 | } |
488 | } |
ac22d975 |
489 | } |
490 | } |
491 | } |
492 | } |
493 | |
795992ad |
494 | #ifndef NO_WINDRVR |
b0f621dd |
495 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 |
496 | #else |
2a7af812 |
497 | e->handle++; |
292160ed |
498 | #endif |
b0f621dd |
499 | |
723d9aa0 |
500 | #ifdef DEBUG |
f1405f13 |
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 | |
b0f621dd |
510 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 |
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); |
723d9aa0 |
519 | #endif |
b0f621dd |
520 | } |
521 | break; |
522 | |
f152c048 |
523 | case TRANSFER_OLD: |
da3ba95a |
524 | case TRANSFER: |
f1405f13 |
525 | DPRINTF("TRANSFER\n"); |
795992ad |
526 | #ifndef NO_WINDRVR |
9c9fd67c |
527 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
528 | #endif |
9c9fd67c |
529 | break; |
530 | |
da3ba95a |
531 | case EVENT_UNREGISTER: |
f1405f13 |
532 | DPRINTF("EVENT_UNREGISTER\n"); |
795992ad |
533 | #ifndef NO_WINDRVR |
9c9fd67c |
534 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
535 | #endif |
9c9fd67c |
536 | break; |
537 | |
da3ba95a |
538 | case INT_WAIT: |
f1405f13 |
539 | DPRINTF("INT_WAIT\n"); |
b0f621dd |
540 | { |
541 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
542 | |
f1405f13 |
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); |
b0f621dd |
547 | |
795992ad |
548 | #ifndef NO_WINDRVR |
b0f621dd |
549 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
550 | #else |
2a7af812 |
551 | if (usbdevice) { |
552 | if (it->dwCounter == 0) { |
553 | it->dwCounter = 1; |
554 | } else { |
533f4b68 |
555 | pthread_mutex_lock(&int_wait); |
be452175 |
556 | pthread_mutex_unlock(&int_wait); |
2a7af812 |
557 | } |
94038a57 |
558 | } else { |
533f4b68 |
559 | pthread_mutex_lock(&int_wait); |
be452175 |
560 | pthread_mutex_unlock(&int_wait); |
2a7af812 |
561 | } |
292160ed |
562 | #endif |
563 | |
f1405f13 |
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); |
b0f621dd |
568 | } |
9c9fd67c |
569 | break; |
570 | |
da3ba95a |
571 | case CARD_UNREGISTER: |
f1405f13 |
572 | DPRINTF("CARD_UNREGISTER\n"); |
795992ad |
573 | #ifndef NO_WINDRVR |
9c9fd67c |
574 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
575 | #endif |
9c9fd67c |
576 | break; |
577 | |
da3ba95a |
578 | case EVENT_PULL: |
f1405f13 |
579 | DPRINTF("EVENT_PULL\n"); |
b1831983 |
580 | { |
581 | struct event *e = (struct event*)(wdheader->data); |
723d9aa0 |
582 | #ifdef DEBUG |
b1831983 |
583 | int i; |
584 | |
f1405f13 |
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 | |
b1831983 |
593 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 |
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); |
723d9aa0 |
602 | #endif |
b1831983 |
603 | |
795992ad |
604 | #ifndef NO_WINDRVR |
b1831983 |
605 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
606 | #else |
2a7af812 |
607 | if (usbdevice) { |
608 | struct usb_interface *interface = usbdevice->config->interface; |
292160ed |
609 | |
610 | e->dwCardType = card_type; |
611 | e->dwAction = 1; |
612 | e->dwEventId = 109; |
ca18111b |
613 | e->u.Usb.dwUniqueID = 110; |
2a7af812 |
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; |
292160ed |
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 |
b1831983 |
623 | |
723d9aa0 |
624 | #ifdef DEBUG |
f1405f13 |
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 | |
b1831983 |
633 | for (i = 0; i < e->dwNumMatchTables; i++) |
f1405f13 |
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); |
723d9aa0 |
642 | #endif |
ca18111b |
643 | |
b1831983 |
644 | } |
9c9fd67c |
645 | break; |
646 | |
da3ba95a |
647 | default: |
292160ed |
648 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); |
795992ad |
649 | #ifndef NO_WINDRVR |
9c9fd67c |
650 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
651 | #endif |
652 | break; |
cdc711dc |
653 | } |
da3ba95a |
654 | |
9c9fd67c |
655 | return ret; |
cdc711dc |
656 | } |
657 | |
be452175 |
658 | int ioctl(int fd, int request, ...) { |
dbda1264 |
659 | va_list args; |
660 | void *argp; |
661 | int ret; |
662 | |
663 | if (!ioctl_func) |
f1405f13 |
664 | ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl"); |
dbda1264 |
665 | |
666 | va_start (args, request); |
667 | argp = va_arg (args, void *); |
668 | va_end (args); |
669 | |
332ced7a |
670 | if (fd == windrvrfd) |
dbda1264 |
671 | ret = do_wdioctl(fd, request, argp); |
672 | else |
673 | ret = (*ioctl_func) (fd, request, argp); |
674 | |
675 | return ret; |
676 | } |
cdc711dc |
677 | |
be452175 |
678 | int open (const char *pathname, int flags, ...) { |
679 | static int (*func) (const char *, int, mode_t) = NULL; |
cdc711dc |
680 | mode_t mode = 0; |
681 | va_list args; |
682 | int fd; |
683 | |
684 | if (!func) |
f1405f13 |
685 | func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open"); |
cdc711dc |
686 | |
687 | if (flags & O_CREAT) { |
688 | va_start(args, flags); |
689 | mode = va_arg(args, mode_t); |
690 | va_end(args); |
691 | } |
692 | |
cdc711dc |
693 | if (!strcmp (pathname, "/dev/windrvr6")) { |
f1405f13 |
694 | DPRINTF("opening windrvr6\n"); |
795992ad |
695 | #ifdef NO_WINDRVR |
01b99d52 |
696 | windrvrfd = fd = (*func) ("/dev/null", flags, mode); |
697 | #else |
698 | windrvrfd = fd = (*func) (pathname, flags, mode); |
699 | #endif |
f10480d1 |
700 | if (!busses) { |
701 | usb_init(); |
702 | usb_find_busses(); |
703 | usb_find_devices(); |
704 | |
705 | busses = usb_get_busses(); |
706 | } |
723d9aa0 |
707 | |
708 | return fd; |
cdc711dc |
709 | } |
710 | |
723d9aa0 |
711 | return (*func) (pathname, flags, mode); |
cdc711dc |
712 | } |
713 | |
723d9aa0 |
714 | int close(int fd) { |
715 | static int (*func) (int) = NULL; |
cdc711dc |
716 | |
723d9aa0 |
717 | if (!func) |
f1405f13 |
718 | func = (int (*) (int)) dlsym(RTLD_NEXT, "close"); |
723d9aa0 |
719 | |
11d01742 |
720 | if (fd == windrvrfd && windrvrfd >= 0) { |
f1405f13 |
721 | DPRINTF("close windrvrfd\n"); |
010cbaa6 |
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; |
332ced7a |
730 | windrvrfd = -1; |
cdc711dc |
731 | } |
cdc711dc |
732 | |
723d9aa0 |
733 | return (*func) (fd); |
cdc711dc |
734 | } |
735 | |
723d9aa0 |
736 | FILE *fopen(const char *path, const char *mode) { |
737 | FILE *ret; |
738 | static FILE* (*func) (const char*, const char*) = NULL; |
ca18111b |
739 | |
740 | if (!func) |
f1405f13 |
741 | func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen"); |
ca18111b |
742 | |
723d9aa0 |
743 | ret = (*func) (path, mode); |
ca18111b |
744 | |
723d9aa0 |
745 | if (!strcmp (path, "/proc/modules")) { |
f1405f13 |
746 | DPRINTF("opening /proc/modules\n"); |
723d9aa0 |
747 | #ifdef NO_WINDRVR |
dbda1264 |
748 | modulesfp = ret; |
723d9aa0 |
749 | modules_read = 0; |
750 | #endif |
751 | } |
cdc711dc |
752 | |
753 | return ret; |
754 | } |
755 | |
dbda1264 |
756 | char *fgets(char *s, int size, FILE *stream) { |
757 | static char* (*func) (char*, int, FILE*) = NULL; |
16f6b164 |
758 | const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"}; |
dbda1264 |
759 | char *ret = NULL; |
760 | |
cdc711dc |
761 | |
762 | if (!func) |
f1405f13 |
763 | func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets"); |
723d9aa0 |
764 | |
dbda1264 |
765 | if (modulesfp == stream) { |
16f6b164 |
766 | if (modules_read < sizeof(modules)) { |
767 | strcpy(s, modules[modules_read]); |
dbda1264 |
768 | ret = s; |
16f6b164 |
769 | modules_read++; |
dbda1264 |
770 | } |
723d9aa0 |
771 | } else { |
dbda1264 |
772 | ret = (*func)(s,size,stream); |
723d9aa0 |
773 | } |
cdc711dc |
774 | |
775 | return ret; |
776 | } |
777 | |
dbda1264 |
778 | int fclose(FILE *fp) { |
779 | static int (*func) (FILE*) = NULL; |
cdc711dc |
780 | |
dbda1264 |
781 | if (!func) |
f1405f13 |
782 | func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose"); |
cdc711dc |
783 | |
dbda1264 |
784 | if (fp == modulesfp) { |
785 | modulesfp = NULL; |
786 | } |
787 | |
788 | return (*func)(fp); |
cdc711dc |
789 | } |
419f2c98 |
790 | |
791 | int access(const char *pathname, int mode) { |
792 | static int (*func) (const char*, int); |
793 | |
794 | if (!func) |
f1405f13 |
795 | func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access"); |
419f2c98 |
796 | |
797 | if (!strcmp(pathname, "/dev/windrvr6")) { |
798 | return 0; |
799 | } else { |
800 | return (*func)(pathname, mode); |
801 | } |
802 | } |