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