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