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