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