]> cvs.zerfleddert.de Git - usb-driver/blame - usb-driver.c
make it possible to share the USB and parallel cable between applications
[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>
c42237a3
MG
42#include <sys/utsname.h>
43#include <bits/wordsize.h>
2c0c1255 44#include "usb-driver.h"
3e670223 45#include "config.h"
cdc711dc 46
9c9fd67c 47static int (*ioctl_func) (int, int, void *) = NULL;
332ced7a 48static int windrvrfd = -1;
54357994 49static unsigned long ppbase = 0;
50static unsigned long ecpbase = 0;
25ba7a49 51static struct parport_config *pport = NULL;
be59993b
MG
52static FILE *modulesfp = NULL;
53static FILE *baseaddrfp = NULL;
54static int baseaddrnum = 0;
723d9aa0 55static int modules_read = 0;
f10480d1 56static struct usb_bus *busses = NULL;
2a7af812 57static struct usb_device *usbdevice;
58static usb_dev_handle *usb_devhandle = NULL;
010cbaa6 59static int usbinterface = -1;
cf55ed64 60static int usbalternate = -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
be59993b 78static int usb_deviceinfo(unsigned char *buf) {
f95f1d2e 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
cf55ed64
MG
238static int usb_claim(int claim) {
239 int ret = 0;
240 static int claimed = 0;
241
242 if (usbinterface < 0)
243 return -1;
244
245 if (claim) {
246 if (claimed)
247 return 0;
248
249 ret = usb_claim_interface(usb_devhandle, usbinterface);
250 if (!ret) {
251 claimed = 1;
252 ret = usb_set_altinterface(usb_devhandle, usbalternate);
253 if (ret)
254 fprintf(stderr, "usb_set_altinterface: %d\n", ret);
255 } else {
256 fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
257 usbinterface, ret, usb_strerror());
258 }
259 } else {
260 if (!claimed)
261 return 0;
262
263 ret = usb_release_interface(usb_devhandle, usbinterface);
264 if (!ret)
265 claimed = 0;
266 }
267
268 return ret;
269}
270
be59993b 271static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 272 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 273 struct version_struct *version;
274 int ret = 0;
cdc711dc 275
da3ba95a 276 if (wdheader->magic != MAGIC) {
2c2119eb 277 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
278 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 279 }
280
bdc65937 281 switch(request & ~(0xc0000000)) {
da3ba95a 282 case VERSION:
9c9fd67c 283 version = (struct version_struct*)(wdheader->data);
81cf3658 284 strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION);
90831fba 285 version->versionul = 802;
f1405f13 286 DPRINTF("VERSION\n");
cdc711dc 287 break;
2c2119eb 288
289 case LICENSE:
f1405f13 290 DPRINTF("LICENSE\n");
2c2119eb 291 break;
292
f152c048 293 case CARD_REGISTER_OLD:
9c9fd67c 294 case CARD_REGISTER:
54357994 295 DPRINTF("CARD_REGISTER\n");
9c9fd67c 296 {
533f4b68 297 struct card_register* cr = (struct card_register*)(wdheader->data);
54357994 298
299 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
300 cr->Card.dwItems,
301 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
302 cr->Card.Item[0].I.IO.dwBytes,
303 cr->Card.Item[0].I.IO.dwBar);
304
305 DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
306 cr->Card.dwItems,
307 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
308 cr->Card.Item[1].I.IO.dwBytes,
309 cr->Card.Item[1].I.IO.dwBar);
576995a8 310#ifndef NO_WINDRVR
311 ret = (*ioctl_func) (fd, request, wdioctl);
ac9e3f59 312#else
bccac33b 313
25ba7a49 314 pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
61e0de7a 315 if (!pport)
316 break;
317
25ba7a49 318 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
3e670223 319
5e3d963b 320 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
bccac33b 321
5e3d963b 322 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
323 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
19b2e286 324
5e3d963b 325 if (ret >= 0) {
326 cr->hCard = ret;
327 } else {
19b2e286 328 cr->hCard = 0;
5e3d963b 329 }
576995a8 330#endif
ac9e3f59 331 DPRINTF("hCard: %lu\n", cr->hCard);
9c9fd67c 332 }
da3ba95a 333 break;
2c2119eb 334
da3ba95a 335 case USB_TRANSFER:
f1405f13 336 DPRINTF("in USB_TRANSFER");
da3ba95a 337 {
338 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
339
723d9aa0 340#ifdef DEBUG
f1405f13 341 DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
342 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
343 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
cf55ed64
MG
344 if (ut->dwPipeNum == 0) {
345 DPRINTF("setup packet: ");
346 hexdump(ut->SetupPacket, 8);
347 }
723d9aa0 348
9c9fd67c 349 if (!ut->fRead && ut->dwBufferSize)
350 {
351 hexdump(ut->pBuffer, ut->dwBufferSize);
9c9fd67c 352 }
723d9aa0 353#endif
9c9fd67c 354
795992ad 355#ifndef NO_WINDRVR
9c9fd67c 356 ret = (*ioctl_func) (fd, request, wdioctl);
411af373 357#else
cf55ed64 358 usb_claim(1);
411af373 359 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
a70f4f38 360 if (ut->dwPipeNum == 0) { /* control pipe */
361 int requesttype, request, value, index, size;
362 requesttype = ut->SetupPacket[0];
363 request = ut->SetupPacket[1];
364 value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
365 index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
366 size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
f1405f13 367 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
a70f4f38 368 ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
369 } else {
370 if (ut->fRead) {
371 ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
a70f4f38 372 } else {
373 ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
374 }
cf55ed64 375 usb_claim(0);
a70f4f38 376 }
377
411af373 378 if (ret < 0) {
a70f4f38 379 fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror());
411af373 380 } else {
381 ut->dwBytesTransferred = ret;
382 ret = 0;
383 }
292160ed 384#endif
9c9fd67c 385
723d9aa0 386#ifdef DEBUG
f1405f13 387 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 388 if (ut->fRead && ut->dwBytesTransferred)
389 {
f1405f13 390 DPRINTF("Read: ");
9c9fd67c 391 hexdump(ut->pBuffer, ut->dwBytesTransferred);
392 }
723d9aa0 393#endif
da3ba95a 394 }
cdc711dc 395 break;
2c2119eb 396
f152c048 397 case INT_ENABLE_OLD:
da3ba95a 398 case INT_ENABLE:
f1405f13 399 DPRINTF("INT_ENABLE\n");
da3ba95a 400 {
9c9fd67c 401 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 402
f1405f13 403 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
404 it->hInterrupt, it->dwOptions,
405 it->dwCmds, it->fEnableOk, it->dwCounter,
406 it->dwLost, it->fStopped);
cdc711dc 407
9c9fd67c 408 it->fEnableOk = 1;
9ba1e383 409 it->fStopped = 0;
ca18111b 410 ints_enabled = 1;
533f4b68 411 pthread_mutex_trylock(&int_wait);
9c9fd67c 412 }
cdc711dc 413
cdc711dc 414 break;
9c9fd67c 415
da3ba95a 416 case INT_DISABLE:
f1405f13 417 DPRINTF("INT_DISABLE\n");
da3ba95a 418 {
9c9fd67c 419 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 420
f1405f13 421 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
422 it->hInterrupt, it->dwOptions,
423 it->dwCmds, it->fEnableOk, it->dwCounter,
424 it->dwLost, it->fStopped);
795992ad 425#ifndef NO_WINDRVR
9c9fd67c 426 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 427#else
428 it->dwCounter = 0;
429 it->fStopped = 1;
ca18111b 430 ints_enabled = 0;
9ba1e383 431 if (pthread_mutex_trylock(&int_wait) == EBUSY)
432 pthread_mutex_unlock(&int_wait);
292160ed 433#endif
f1405f13 434 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
435 it->hInterrupt, it->dwOptions,
436 it->dwCmds, it->fEnableOk, it->dwCounter,
437 it->dwLost, it->fStopped);
da3ba95a 438 }
da3ba95a 439 break;
da3ba95a 440
9c9fd67c 441 case USB_SET_INTERFACE:
f1405f13 442 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 443 {
9c9fd67c 444 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
445
f1405f13 446 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
447 usi->dwUniqueID, usi->dwInterfaceNum,
448 usi->dwAlternateSetting, usi->dwOptions);
795992ad 449#ifndef NO_WINDRVR
9c9fd67c 450 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 451#else
452 if (usbdevice) {
2a7af812 453 if (!usb_devhandle)
454 usb_devhandle = usb_open(usbdevice);
d0676964 455
cf55ed64
MG
456 usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
457 usbalternate = usi->dwAlternateSetting;
2a7af812 458 }
292160ed 459#endif
f1405f13 460 DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
461 usi->dwUniqueID, usi->dwInterfaceNum,
462 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 463 }
cdc711dc 464 break;
da3ba95a 465
f152c048 466 case USB_GET_DEVICE_DATA_OLD:
9c9fd67c 467 case USB_GET_DEVICE_DATA:
f1405f13 468 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 469 {
470 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
471 int pSize;
472
f1405f13 473 DPRINTF("unique: %lu, bytes: %lu, options: %lx\n",
474 ugdd->dwUniqueID, ugdd->dwBytes,
475 ugdd->dwOptions);
476
9c9fd67c 477 pSize = ugdd->dwBytes;
e71b6bf3 478 if (!ugdd->dwBytes) {
2a7af812 479 if (usbdevice) {
f95f1d2e 480 ugdd->dwBytes = usb_deviceinfo(NULL);
e71b6bf3 481 }
482 } else {
f95f1d2e 483 usb_deviceinfo((unsigned char*)ugdd->pBuf);
e71b6bf3 484 }
da3ba95a 485 }
9c9fd67c 486 break;
487
f152c048 488 case EVENT_REGISTER_OLD:
b0f621dd 489 case EVENT_REGISTER:
f1405f13 490 DPRINTF("EVENT_REGISTER\n");
b0f621dd 491 {
492 struct event *e = (struct event*)(wdheader->data);
ac22d975 493 struct usb_bus *bus;
3a2a22bc
MG
494 char* devpos;
495 int busnum = -1, devnum = -1;
b0f621dd 496 int i;
497
f1405f13 498 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",
499 e->handle, e->dwAction,
500 e->dwStatus, e->dwEventId, e->dwCardType,
501 e->hKernelPlugIn, e->dwOptions,
502 e->u.Usb.deviceId.dwVendorId,
503 e->u.Usb.deviceId.dwProductId,
504 e->u.Usb.dwUniqueID, e->dwEventVer,
505 e->dwNumMatchTables);
506
3a2a22bc
MG
507 devpos = getenv("XILINX_USB_DEV");
508 if (devpos != NULL) {
509 int j;
510 char *devstr = NULL, *remainder;
511
512 DPRINTF("XILINX_USB_DEV=%s\n", devpos);
513
514 for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) {
515 if (devpos[j] == ':') {
516 devpos[j] = 0;
517 devstr = &(devpos[j+1]);
518 }
519 }
d377df04 520
3a2a22bc
MG
521 if (devstr && strlen(devstr) > 0) {
522 busnum = strtol(devpos, &remainder, 10);
523 if (devpos == remainder) {
524 busnum = -1;
525 } else {
526 devnum = strtol(devstr, &remainder, 10);
527 if (devstr == remainder) {
77683ab6 528 busnum = -1;
3a2a22bc
MG
529 devnum = -1;
530 } else {
531 fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n",
532 busnum, devnum);
533 }
534 }
535 }
536 }
d377df04 537
ac22d975 538 for (i = 0; i < e->dwNumMatchTables; i++) {
f1405f13 539
540 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
541 e->matchTables[i].VendorId,
542 e->matchTables[i].ProductId,
543 e->matchTables[i].bDeviceClass,
544 e->matchTables[i].bDeviceSubClass,
545 e->matchTables[i].bInterfaceClass,
546 e->matchTables[i].bInterfaceSubClass,
547 e->matchTables[i].bInterfaceProtocol);
b0f621dd 548
ac22d975 549 for (bus = busses; bus; bus = bus->next) {
550 struct usb_device *dev;
551
3a2a22bc
MG
552 if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum))
553 continue;
554
ac22d975 555 for (dev = bus->devices; dev; dev = dev->next) {
556 struct usb_device_descriptor *desc = &(dev->descriptor);
557
558 if((desc->idVendor == e->matchTables[i].VendorId) &&
559 (desc->idProduct == e->matchTables[i].ProductId) &&
560 (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
d377df04 561 (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
a2a952c0 562 ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) {
2a7af812 563 int ac;
564 for (ac = 0; ac < desc->bNumConfigurations; ac++) {
565 struct usb_interface *interface = dev->config[ac].interface;
566 int ai;
567
568 for (ai = 0; ai < interface->num_altsetting; ai++) {
f1405f13 569
570 DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
571 interface->altsetting[i].bInterfaceClass,
572 interface->altsetting[i].bInterfaceSubClass,
573 interface->altsetting[i].bInterfaceProtocol);
574
2a7af812 575 if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
576 (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
577 /* TODO: check interfaceClass! */
f1405f13 578 DPRINTF("found device with libusb\n");
2a7af812 579 usbdevice = dev;
580 card_type = e->dwCardType;
581 }
582 }
583 }
ac22d975 584 }
585 }
586 }
587 }
588
795992ad 589#ifndef NO_WINDRVR
b0f621dd 590 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 591#else
2a7af812 592 e->handle++;
292160ed 593#endif
b0f621dd 594
723d9aa0 595#ifdef DEBUG
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
b0f621dd 605 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 606 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
607 e->matchTables[i].VendorId,
608 e->matchTables[i].ProductId,
609 e->matchTables[i].bDeviceClass,
610 e->matchTables[i].bDeviceSubClass,
611 e->matchTables[i].bInterfaceClass,
612 e->matchTables[i].bInterfaceSubClass,
613 e->matchTables[i].bInterfaceProtocol);
723d9aa0 614#endif
b0f621dd 615 }
616 break;
617
f152c048 618 case TRANSFER_OLD:
da3ba95a 619 case TRANSFER:
f1405f13 620 DPRINTF("TRANSFER\n");
ac9e3f59 621 {
622 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
623
1dac5195 624#ifndef NO_WINDRVR
625 ret = (*ioctl_func) (fd, request, wdioctl);
626#else
25ba7a49 627 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
1dac5195 628#endif
ac9e3f59 629 }
9c9fd67c 630 break;
631
54357994 632 case MULTI_TRANSFER_OLD:
576995a8 633 case MULTI_TRANSFER:
634 DPRINTF("MULTI_TRANSFER\n");
ac9e3f59 635 {
636 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
637 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
1dac5195 638#ifndef NO_WINDRVR
639 ret = (*ioctl_func) (fd, request, wdioctl);
640#else
25ba7a49 641 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
ac9e3f59 642#endif
ac9e3f59 643 }
576995a8 644 break;
645
da3ba95a 646 case EVENT_UNREGISTER:
f1405f13 647 DPRINTF("EVENT_UNREGISTER\n");
795992ad 648#ifndef NO_WINDRVR
9c9fd67c 649 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 650#endif
9c9fd67c 651 break;
652
da3ba95a 653 case INT_WAIT:
f1405f13 654 DPRINTF("INT_WAIT\n");
b0f621dd 655 {
656 struct interrupt *it = (struct interrupt*)(wdheader->data);
657
f1405f13 658 DPRINTF("Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
659 it->hInterrupt, it->dwOptions,
660 it->dwCmds, it->fEnableOk, it->dwCounter,
661 it->dwLost, it->fStopped);
b0f621dd 662
795992ad 663#ifndef NO_WINDRVR
b0f621dd 664 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 665#else
2a7af812 666 if (usbdevice) {
667 if (it->dwCounter == 0) {
668 it->dwCounter = 1;
669 } else {
533f4b68 670 pthread_mutex_lock(&int_wait);
be452175 671 pthread_mutex_unlock(&int_wait);
2a7af812 672 }
94038a57 673 } else {
533f4b68 674 pthread_mutex_lock(&int_wait);
be452175 675 pthread_mutex_unlock(&int_wait);
2a7af812 676 }
292160ed 677#endif
678
f1405f13 679 DPRINTF("INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
680 it->hInterrupt, it->dwOptions, it->dwCmds,
681 it->fEnableOk, it->dwCounter, it->dwLost,
682 it->fStopped);
b0f621dd 683 }
9c9fd67c 684 break;
685
da3ba95a 686 case CARD_UNREGISTER:
f1405f13 687 DPRINTF("CARD_UNREGISTER\n");
54357994 688 {
689 struct card_register* cr = (struct card_register*)(wdheader->data);
690
691 DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
692 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
693 cr->Card.Item[0].I.IO.dwBytes,
694 cr->Card.Item[0].I.IO.dwBar);
695
696 DPRINTF("hCard: %lu\n", cr->hCard);
697
795992ad 698#ifndef NO_WINDRVR
54357994 699 ret = (*ioctl_func) (fd, request, wdioctl);
700#else
72ce448b
MG
701 if (pport)
702 pport->close(cr->hCard);
703
704 pport = NULL;
292160ed 705#endif
54357994 706 }
9c9fd67c 707 break;
708
da3ba95a 709 case EVENT_PULL:
f1405f13 710 DPRINTF("EVENT_PULL\n");
b1831983 711 {
712 struct event *e = (struct event*)(wdheader->data);
723d9aa0 713#ifdef DEBUG
b1831983 714 int i;
715
f1405f13 716 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",
717 e->handle, e->dwAction, e->dwStatus,
718 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
719 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
720 e->u.Usb.deviceId.dwProductId,
721 e->u.Usb.dwUniqueID, e->dwEventVer,
722 e->dwNumMatchTables);
723
b1831983 724 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 725 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
726 e->matchTables[i].VendorId,
727 e->matchTables[i].ProductId,
728 e->matchTables[i].bDeviceClass,
729 e->matchTables[i].bDeviceSubClass,
730 e->matchTables[i].bInterfaceClass,
731 e->matchTables[i].bInterfaceSubClass,
732 e->matchTables[i].bInterfaceProtocol);
723d9aa0 733#endif
b1831983 734
795992ad 735#ifndef NO_WINDRVR
b1831983 736 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 737#else
2a7af812 738 if (usbdevice) {
739 struct usb_interface *interface = usbdevice->config->interface;
292160ed 740
741 e->dwCardType = card_type;
742 e->dwAction = 1;
743 e->dwEventId = 109;
ca18111b 744 e->u.Usb.dwUniqueID = 110;
2a7af812 745 e->matchTables[0].VendorId = usbdevice->descriptor.idVendor;
746 e->matchTables[0].ProductId = usbdevice->descriptor.idProduct;
747 e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass;
748 e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass;
292160ed 749 e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
750 e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
751 e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
752 }
753#endif
b1831983 754
723d9aa0 755#ifdef DEBUG
f1405f13 756 DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
757 e->handle, e->dwAction, e->dwStatus,
758 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
759 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
760 e->u.Usb.deviceId.dwProductId,
761 e->u.Usb.dwUniqueID, e->dwEventVer,
762 e->dwNumMatchTables);
763
b1831983 764 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 765 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
766 e->matchTables[i].VendorId,
767 e->matchTables[i].ProductId,
768 e->matchTables[i].bDeviceClass,
769 e->matchTables[i].bDeviceSubClass,
770 e->matchTables[i].bInterfaceClass,
771 e->matchTables[i].bInterfaceSubClass,
772 e->matchTables[i].bInterfaceProtocol);
723d9aa0 773#endif
ca18111b 774
b1831983 775 }
9c9fd67c 776 break;
777
da3ba95a 778 default:
292160ed 779 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 780#ifndef NO_WINDRVR
9c9fd67c 781 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 782#endif
783 break;
cdc711dc 784 }
da3ba95a 785
9c9fd67c 786 return ret;
cdc711dc 787}
788
54357994 789int ioctl(int fd, unsigned long int request, ...) {
dbda1264 790 va_list args;
791 void *argp;
792 int ret;
793
794 if (!ioctl_func)
f1405f13 795 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 796
797 va_start (args, request);
798 argp = va_arg (args, void *);
799 va_end (args);
800
332ced7a 801 if (fd == windrvrfd)
dbda1264 802 ret = do_wdioctl(fd, request, argp);
803 else
804 ret = (*ioctl_func) (fd, request, argp);
805
806 return ret;
807}
cdc711dc 808
be452175 809int open (const char *pathname, int flags, ...) {
810 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 811 mode_t mode = 0;
812 va_list args;
813 int fd;
814
815 if (!func)
f1405f13 816 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 817
818 if (flags & O_CREAT) {
819 va_start(args, flags);
820 mode = va_arg(args, mode_t);
821 va_end(args);
822 }
823
cdc711dc 824 if (!strcmp (pathname, "/dev/windrvr6")) {
f1405f13 825 DPRINTF("opening windrvr6\n");
795992ad 826#ifdef NO_WINDRVR
01b99d52 827 windrvrfd = fd = (*func) ("/dev/null", flags, mode);
828#else
829 windrvrfd = fd = (*func) (pathname, flags, mode);
830#endif
f10480d1 831 if (!busses) {
832 usb_init();
833 usb_find_busses();
834 usb_find_devices();
835
836 busses = usb_get_busses();
837 }
723d9aa0 838
839 return fd;
cdc711dc 840 }
841
723d9aa0 842 return (*func) (pathname, flags, mode);
cdc711dc 843}
844
723d9aa0 845int close(int fd) {
846 static int (*func) (int) = NULL;
cdc711dc 847
723d9aa0 848 if (!func)
f1405f13 849 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 850
11d01742 851 if (fd == windrvrfd && windrvrfd >= 0) {
f1405f13 852 DPRINTF("close windrvrfd\n");
010cbaa6 853
cf55ed64 854 if (usb_devhandle)
010cbaa6 855 usb_close(usb_devhandle);
856
857 usb_devhandle = NULL;
858 usbinterface = -1;
332ced7a 859 windrvrfd = -1;
cdc711dc 860 }
cdc711dc 861
723d9aa0 862 return (*func) (fd);
cdc711dc 863}
864
723d9aa0 865FILE *fopen(const char *path, const char *mode) {
866 FILE *ret;
867 static FILE* (*func) (const char*, const char*) = NULL;
0dca330e 868 char buf[256];
869 int i;
ca18111b 870
871 if (!func)
f1405f13 872 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 873
3e670223 874 for (i = 0; i < 4; i++) {
875 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
876 if (!strcmp(path, buf)) {
877 DPRINTF("open base-addr of parport%d\n", i);
878 if (config_is_real_pport(i)) {
879 ret = (*func) (path, mode);
880 } else {
881 ret = (*func) ("/dev/null", mode);
882 }
883
884 if (ret) {
885 baseaddrfp = ret;
886 baseaddrnum = i;
887 }
888
889 return ret;
890 }
891 }
892
893 ret = (*func) (path, mode);
ca18111b 894
0dca330e 895 if (!strcmp(path, "/proc/modules")) {
f1405f13 896 DPRINTF("opening /proc/modules\n");
723d9aa0 897#ifdef NO_WINDRVR
dbda1264 898 modulesfp = ret;
723d9aa0 899 modules_read = 0;
900#endif
901 }
cdc711dc 902
903 return ret;
904}
905
dbda1264 906char *fgets(char *s, int size, FILE *stream) {
907 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 908 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
0dca330e 909 char buf[256];
dbda1264 910 char *ret = NULL;
911
cdc711dc 912
913 if (!func)
f1405f13 914 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 915
dbda1264 916 if (modulesfp == stream) {
54357994 917 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
16f6b164 918 strcpy(s, modules[modules_read]);
dbda1264 919 ret = s;
16f6b164 920 modules_read++;
dbda1264 921 }
0dca330e 922 } else if (baseaddrfp == stream) {
923 snprintf(s, sizeof(buf), "%d\t%d\n",
924 (baseaddrnum) * 0x10,
925 ((baseaddrnum) * 0x10) + 0x400);
926 ret = s;
723d9aa0 927 } else {
dbda1264 928 ret = (*func)(s,size,stream);
723d9aa0 929 }
cdc711dc 930
931 return ret;
932}
933
dbda1264 934int fclose(FILE *fp) {
935 static int (*func) (FILE*) = NULL;
cdc711dc 936
dbda1264 937 if (!func)
f1405f13 938 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 939
dbda1264 940 if (fp == modulesfp) {
941 modulesfp = NULL;
942 }
0dca330e 943
944 if (fp == baseaddrfp) {
945 baseaddrfp = NULL;
946 }
dbda1264 947
948 return (*func)(fp);
cdc711dc 949}
419f2c98 950
951int access(const char *pathname, int mode) {
952 static int (*func) (const char*, int);
953
954 if (!func)
f1405f13 955 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
3eee002c 956
b8b756f5 957 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
419f2c98 958 return 0;
959 } else {
960 return (*func)(pathname, mode);
961 }
962}
c42237a3
MG
963
964#if __WORDSIZE == 32
965int uname (struct utsname *__name) {
966 static int (*func) (struct utsname*);
967 int ret;
968
969 if (!func)
970 func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname");
971
972 ret = (*func)(__name);
973
974 if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) {
975 strcpy(__name->machine, "i686");
976 }
977
978 return ret;
979}
980#endif
Impressum, Datenschutz