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