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