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