]> cvs.zerfleddert.de Git - usb-driver/blame - usb-driver.c
typo
[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>
2a7af812 36#include <signal.h>
9ba1e383 37#include <errno.h>
11d01742 38#include <inttypes.h>
54357994 39#include <sys/ioctl.h>
c42237a3
MG
40#include <sys/utsname.h>
41#include <bits/wordsize.h>
1fc8f7a4
MG
42#include <sys/ipc.h>
43#include <sys/sem.h>
daa4fc04
MG
44#include <syscall.h>
45#include <linux/personality.h>
2c0c1255 46#include "usb-driver.h"
3e670223 47#include "config.h"
cbfa0ac6 48#include "xpcu.h"
cdc711dc 49
9c9fd67c 50static int (*ioctl_func) (int, int, void *) = NULL;
9bfaca62
MG
51static int *windrvrfds = NULL;
52static int windrvrfds_count = 0;
54357994 53static unsigned long ppbase = 0;
54static unsigned long ecpbase = 0;
25ba7a49 55static struct parport_config *pport = NULL;
be59993b
MG
56static FILE *modulesfp = NULL;
57static FILE *baseaddrfp = NULL;
58static int baseaddrnum = 0;
723d9aa0 59static int modules_read = 0;
292160ed 60
795992ad 61#define NO_WINDRVR 1
723d9aa0 62
325556c9 63void hexdump(unsigned char *buf, int len, char *prefix) {
723d9aa0 64 int i;
65
325556c9 66 fprintf(stderr, "%s ", prefix);
723d9aa0 67 for(i=0; i<len; i++) {
68 fprintf(stderr,"%02x ", buf[i]);
69 if ((i % 16) == 15)
325556c9 70 fprintf(stderr,"\n%s ", prefix);
723d9aa0 71 }
f1405f13 72 fprintf(stderr,"\n");
723d9aa0 73}
cdc711dc 74
cf55ed64 75
be59993b 76static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
cdc711dc 77 struct header_struct* wdheader = (struct header_struct*)wdioctl;
9c9fd67c 78 struct version_struct *version;
79 int ret = 0;
cdc711dc 80
da3ba95a 81 if (wdheader->magic != MAGIC) {
2c2119eb 82 fprintf(stderr,"!!!ERROR: magic header does not match!!!\n");
83 return (*ioctl_func) (fd, request, wdioctl);
cdc711dc 84 }
85
bdc65937 86 switch(request & ~(0xc0000000)) {
da3ba95a 87 case VERSION:
9c9fd67c 88 version = (struct version_struct*)(wdheader->data);
81cf3658 89 strcpy(version->version, "libusb-driver.so version: " USB_DRIVER_VERSION);
90831fba 90 version->versionul = 802;
f1405f13 91 DPRINTF("VERSION\n");
cdc711dc 92 break;
2c2119eb 93
94 case LICENSE:
f1405f13 95 DPRINTF("LICENSE\n");
2c2119eb 96 break;
97
f152c048 98 case CARD_REGISTER_OLD:
9c9fd67c 99 case CARD_REGISTER:
54357994 100 DPRINTF("CARD_REGISTER\n");
9c9fd67c 101 {
533f4b68 102 struct card_register* cr = (struct card_register*)(wdheader->data);
54357994 103
501f1c21 104 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
54357994 105 cr->Card.dwItems,
106 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
107 cr->Card.Item[0].I.IO.dwBytes,
108 cr->Card.Item[0].I.IO.dwBar);
109
501f1c21 110 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
54357994 111 cr->Card.dwItems,
112 (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
113 cr->Card.Item[1].I.IO.dwBytes,
114 cr->Card.Item[1].I.IO.dwBar);
576995a8 115#ifndef NO_WINDRVR
116 ret = (*ioctl_func) (fd, request, wdioctl);
ac9e3f59 117#else
4c90fa39 118 cr->hCard = 0;
bccac33b 119
25ba7a49 120 pport = config_get((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
61e0de7a 121 if (!pport)
122 break;
123
25ba7a49 124 ret = pport->open((unsigned long)cr->Card.Item[0].I.IO.dwAddr / 0x10);
3e670223 125
5e3d963b 126 ppbase = (unsigned long)cr->Card.Item[0].I.IO.dwAddr;
bccac33b 127
5e3d963b 128 if (cr->Card.dwItems > 1 && cr->Card.Item[1].I.IO.dwAddr)
129 ecpbase = (unsigned long)cr->Card.Item[1].I.IO.dwAddr;
19b2e286 130
5e3d963b 131 if (ret >= 0) {
132 cr->hCard = ret;
133 } else {
19b2e286 134 cr->hCard = 0;
5e3d963b 135 }
576995a8 136#endif
501f1c21 137 DPRINTF("<-hCard: %lu\n", cr->hCard);
9c9fd67c 138 }
da3ba95a 139 break;
2c2119eb 140
da3ba95a 141 case USB_TRANSFER:
501f1c21 142 DPRINTF("USB_TRANSFER\n");
da3ba95a 143 {
144 struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
145
723d9aa0 146#ifdef DEBUG
501f1c21 147 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
f1405f13 148 ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
149 ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
cf55ed64 150 if (ut->dwPipeNum == 0) {
325556c9
MG
151 DPRINTF("-> setup packet:");
152 hexdump(ut->SetupPacket, 8, "");
cf55ed64 153 }
723d9aa0 154
9c9fd67c 155 if (!ut->fRead && ut->dwBufferSize)
156 {
325556c9 157 hexdump(ut->pBuffer, ut->dwBufferSize, "->");
9c9fd67c 158 }
723d9aa0 159#endif
9c9fd67c 160
795992ad 161#ifndef NO_WINDRVR
9c9fd67c 162 ret = (*ioctl_func) (fd, request, wdioctl);
411af373 163#else
19acdb82 164 ret = xpcu_transfer(ut);
292160ed 165#endif
9c9fd67c 166
723d9aa0 167#ifdef DEBUG
f1405f13 168 DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
9c9fd67c 169 if (ut->fRead && ut->dwBytesTransferred)
170 {
325556c9 171 hexdump(ut->pBuffer, ut->dwBytesTransferred, "<-");
9c9fd67c 172 }
723d9aa0 173#endif
da3ba95a 174 }
cdc711dc 175 break;
2c2119eb 176
f152c048 177 case INT_ENABLE_OLD:
da3ba95a 178 case INT_ENABLE:
f1405f13 179 DPRINTF("INT_ENABLE\n");
da3ba95a 180 {
9c9fd67c 181 struct interrupt *it = (struct interrupt*)(wdheader->data);
cdc711dc 182
501f1c21 183 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 184 it->hInterrupt, it->dwOptions,
185 it->dwCmds, it->fEnableOk, it->dwCounter,
186 it->dwLost, it->fStopped);
cdc711dc 187
501f1c21
MG
188#ifndef NO_WINDRVR
189 ret = (*ioctl_func) (fd, request, wdioctl);
190#else
19acdb82 191 ret = xpcu_int_state(it, ENABLE_INTERRUPT);
501f1c21
MG
192#endif
193
194 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
195 it->hInterrupt, it->dwOptions,
196 it->dwCmds, it->fEnableOk, it->dwCounter,
197 it->dwLost, it->fStopped);
9c9fd67c 198 }
cdc711dc 199
cdc711dc 200 break;
9c9fd67c 201
da3ba95a 202 case INT_DISABLE:
f1405f13 203 DPRINTF("INT_DISABLE\n");
da3ba95a 204 {
9c9fd67c 205 struct interrupt *it = (struct interrupt*)(wdheader->data);
da3ba95a 206
501f1c21 207 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 208 it->hInterrupt, it->dwOptions,
209 it->dwCmds, it->fEnableOk, it->dwCounter,
210 it->dwLost, it->fStopped);
795992ad 211#ifndef NO_WINDRVR
9c9fd67c 212 ret = (*ioctl_func) (fd, request, wdioctl);
e71b6bf3 213#else
19acdb82 214 ret = xpcu_int_state(it, DISABLE_INTERRUPT);
292160ed 215#endif
501f1c21 216 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 217 it->hInterrupt, it->dwOptions,
218 it->dwCmds, it->fEnableOk, it->dwCounter,
219 it->dwLost, it->fStopped);
da3ba95a 220 }
da3ba95a 221 break;
da3ba95a 222
9c9fd67c 223 case USB_SET_INTERFACE:
f1405f13 224 DPRINTF("USB_SET_INTERFACE\n");
da3ba95a 225 {
9c9fd67c 226 struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
227
501f1c21 228 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
f1405f13 229 usi->dwUniqueID, usi->dwInterfaceNum,
230 usi->dwAlternateSetting, usi->dwOptions);
795992ad 231#ifndef NO_WINDRVR
9c9fd67c 232 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 233#else
19acdb82 234 ret = xpcu_set_interface(usi);
292160ed 235#endif
501f1c21 236 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
f1405f13 237 usi->dwUniqueID, usi->dwInterfaceNum,
238 usi->dwAlternateSetting, usi->dwOptions);
da3ba95a 239 }
cdc711dc 240 break;
da3ba95a 241
f152c048 242 case USB_GET_DEVICE_DATA_OLD:
9c9fd67c 243 case USB_GET_DEVICE_DATA:
f1405f13 244 DPRINTF("USB_GET_DEVICE_DATA\n");
9c9fd67c 245 {
246 struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
9c9fd67c 247
501f1c21 248 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
f1405f13 249 ugdd->dwUniqueID, ugdd->dwBytes,
250 ugdd->dwOptions);
251
19acdb82 252 ret = xpcu_deviceinfo(ugdd);
f92c0fbc 253
da3ba95a 254 }
9c9fd67c 255 break;
256
f152c048 257 case EVENT_REGISTER_OLD:
b0f621dd 258 case EVENT_REGISTER:
f1405f13 259 DPRINTF("EVENT_REGISTER\n");
b0f621dd 260 {
261 struct event *e = (struct event*)(wdheader->data);
f92c0fbc 262#ifdef DEBUG
b0f621dd 263 int i;
f92c0fbc 264#endif
b0f621dd 265
501f1c21 266 DPRINTF("-> handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 267 e->handle, e->dwAction,
268 e->dwStatus, e->dwEventId, e->dwCardType,
269 e->hKernelPlugIn, e->dwOptions,
270 e->u.Usb.deviceId.dwVendorId,
271 e->u.Usb.deviceId.dwProductId,
272 e->u.Usb.dwUniqueID, e->dwEventVer,
273 e->dwNumMatchTables);
274
795992ad 275#ifndef NO_WINDRVR
b0f621dd 276 ret = (*ioctl_func) (fd, request, wdioctl);
2a7af812 277#else
19acdb82 278 ret = xpcu_find(e);
292160ed 279#endif
b0f621dd 280
723d9aa0 281#ifdef DEBUG
501f1c21 282 DPRINTF("<- handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 283 e->handle, e->dwAction,
284 e->dwStatus, e->dwEventId, e->dwCardType,
285 e->hKernelPlugIn, e->dwOptions,
286 e->u.Usb.deviceId.dwVendorId,
287 e->u.Usb.deviceId.dwProductId,
288 e->u.Usb.dwUniqueID, e->dwEventVer,
289 e->dwNumMatchTables);
290
b0f621dd 291 for (i = 0; i < e->dwNumMatchTables; i++)
f1405f13 292 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
293 e->matchTables[i].VendorId,
294 e->matchTables[i].ProductId,
295 e->matchTables[i].bDeviceClass,
296 e->matchTables[i].bDeviceSubClass,
297 e->matchTables[i].bInterfaceClass,
298 e->matchTables[i].bInterfaceSubClass,
299 e->matchTables[i].bInterfaceProtocol);
723d9aa0 300#endif
b0f621dd 301 }
302 break;
303
f152c048 304 case TRANSFER_OLD:
da3ba95a 305 case TRANSFER:
f1405f13 306 DPRINTF("TRANSFER\n");
ac9e3f59 307 {
308 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
309
1dac5195 310#ifndef NO_WINDRVR
311 ret = (*ioctl_func) (fd, request, wdioctl);
312#else
25ba7a49 313 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, 1);
1dac5195 314#endif
ac9e3f59 315 }
9c9fd67c 316 break;
317
54357994 318 case MULTI_TRANSFER_OLD:
576995a8 319 case MULTI_TRANSFER:
320 DPRINTF("MULTI_TRANSFER\n");
ac9e3f59 321 {
322 WD_TRANSFER *tr = (WD_TRANSFER*)(wdheader->data);
323 unsigned long num = wdheader->size/sizeof(WD_TRANSFER);
1dac5195 324#ifndef NO_WINDRVR
325 ret = (*ioctl_func) (fd, request, wdioctl);
326#else
25ba7a49 327 ret = pport->transfer(tr, fd, request, ppbase, ecpbase, num);
ac9e3f59 328#endif
ac9e3f59 329 }
576995a8 330 break;
331
da3ba95a 332 case EVENT_UNREGISTER:
f92c0fbc
MG
333 {
334 struct event *e = (struct event*)(wdheader->data);
335
336 DPRINTF("EVENT_UNREGISTER\n");
795992ad 337#ifndef NO_WINDRVR
f92c0fbc
MG
338 ret = (*ioctl_func) (fd, request, wdioctl);
339#else
19acdb82 340 ret = xpcu_close(e);
292160ed 341#endif
f92c0fbc 342 }
9c9fd67c 343 break;
344
da3ba95a 345 case INT_WAIT:
f1405f13 346 DPRINTF("INT_WAIT\n");
b0f621dd 347 {
348 struct interrupt *it = (struct interrupt*)(wdheader->data);
349
501f1c21 350 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 351 it->hInterrupt, it->dwOptions,
352 it->dwCmds, it->fEnableOk, it->dwCounter,
353 it->dwLost, it->fStopped);
b0f621dd 354
795992ad 355#ifndef NO_WINDRVR
b0f621dd 356 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 357#else
19acdb82 358 ret = xpcu_int_wait(it);
292160ed 359#endif
360
501f1c21 361 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
f1405f13 362 it->hInterrupt, it->dwOptions, it->dwCmds,
363 it->fEnableOk, it->dwCounter, it->dwLost,
364 it->fStopped);
b0f621dd 365 }
9c9fd67c 366 break;
367
da3ba95a 368 case CARD_UNREGISTER:
f1405f13 369 DPRINTF("CARD_UNREGISTER\n");
54357994 370 {
371 struct card_register* cr = (struct card_register*)(wdheader->data);
372
501f1c21 373 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
54357994 374 (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
375 cr->Card.Item[0].I.IO.dwBytes,
376 cr->Card.Item[0].I.IO.dwBar);
377
501f1c21 378 DPRINTF("-> hCard: %lu\n", cr->hCard);
54357994 379
795992ad 380#ifndef NO_WINDRVR
54357994 381 ret = (*ioctl_func) (fd, request, wdioctl);
382#else
72ce448b
MG
383 if (pport)
384 pport->close(cr->hCard);
385
386 pport = NULL;
292160ed 387#endif
54357994 388 }
9c9fd67c 389 break;
390
da3ba95a 391 case EVENT_PULL:
f1405f13 392 DPRINTF("EVENT_PULL\n");
b1831983 393 {
394 struct event *e = (struct event*)(wdheader->data);
723d9aa0 395#ifdef DEBUG
b1831983 396 int i;
397
501f1c21 398 DPRINTF("-> handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 399 e->handle, e->dwAction, e->dwStatus,
400 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
401 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
402 e->u.Usb.deviceId.dwProductId,
403 e->u.Usb.dwUniqueID, e->dwEventVer,
404 e->dwNumMatchTables);
405
b1831983 406 for (i = 0; i < e->dwNumMatchTables; i++)
501f1c21 407 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
f1405f13 408 e->matchTables[i].VendorId,
409 e->matchTables[i].ProductId,
410 e->matchTables[i].bDeviceClass,
411 e->matchTables[i].bDeviceSubClass,
412 e->matchTables[i].bInterfaceClass,
413 e->matchTables[i].bInterfaceSubClass,
414 e->matchTables[i].bInterfaceProtocol);
723d9aa0 415#endif
b1831983 416
795992ad 417#ifndef NO_WINDRVR
b1831983 418 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 419#else
19acdb82 420 ret = xpcu_found(e);
292160ed 421#endif
b1831983 422
723d9aa0 423#ifdef DEBUG
501f1c21 424 DPRINTF("<- handle: 0x%lx, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: 0x%lx, ver: %lu, nummatch: %lu\n",
f1405f13 425 e->handle, e->dwAction, e->dwStatus,
426 e->dwEventId, e->dwCardType, e->hKernelPlugIn,
427 e->dwOptions, e->u.Usb.deviceId.dwVendorId,
428 e->u.Usb.deviceId.dwProductId,
429 e->u.Usb.dwUniqueID, e->dwEventVer,
430 e->dwNumMatchTables);
431
b1831983 432 for (i = 0; i < e->dwNumMatchTables; i++)
501f1c21 433 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
f1405f13 434 e->matchTables[i].VendorId,
435 e->matchTables[i].ProductId,
436 e->matchTables[i].bDeviceClass,
437 e->matchTables[i].bDeviceSubClass,
438 e->matchTables[i].bInterfaceClass,
439 e->matchTables[i].bInterfaceSubClass,
440 e->matchTables[i].bInterfaceProtocol);
723d9aa0 441#endif
ca18111b 442
b1831983 443 }
9c9fd67c 444 break;
445
da3ba95a 446 default:
292160ed 447 fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request);
795992ad 448#ifndef NO_WINDRVR
9c9fd67c 449 ret = (*ioctl_func) (fd, request, wdioctl);
292160ed 450#endif
451 break;
cdc711dc 452 }
da3ba95a 453
9c9fd67c 454 return ret;
cdc711dc 455}
456
54357994 457int ioctl(int fd, unsigned long int request, ...) {
dbda1264 458 va_list args;
459 void *argp;
9bfaca62 460 int i;
dbda1264 461
462 if (!ioctl_func)
f1405f13 463 ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
dbda1264 464
465 va_start (args, request);
466 argp = va_arg (args, void *);
467 va_end (args);
468
9bfaca62
MG
469 for (i = 0; i < windrvrfds_count; i++) {
470 if (fd == windrvrfds[i])
471 return do_wdioctl(fd, request, argp);
472 }
dbda1264 473
9bfaca62 474 return (*ioctl_func) (fd, request, argp);
dbda1264 475}
cdc711dc 476
be452175 477int open (const char *pathname, int flags, ...) {
478 static int (*func) (const char *, int, mode_t) = NULL;
cdc711dc 479 mode_t mode = 0;
480 va_list args;
481 int fd;
482
483 if (!func)
f1405f13 484 func = (int (*) (const char *, int, mode_t)) dlsym (RTLD_NEXT, "open");
cdc711dc 485
486 if (flags & O_CREAT) {
487 va_start(args, flags);
488 mode = va_arg(args, mode_t);
489 va_end(args);
490 }
491
cdc711dc 492 if (!strcmp (pathname, "/dev/windrvr6")) {
9bfaca62
MG
493 DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count);
494 windrvrfds = realloc(windrvrfds, sizeof(int) * (++windrvrfds_count));
495 if (!windrvrfds)
496 return -ENOMEM;
497
795992ad 498#ifdef NO_WINDRVR
9bfaca62 499 windrvrfds[windrvrfds_count-1] = fd = (*func) ("/dev/null", flags, mode);
01b99d52 500#else
9bfaca62 501 windrvrfds[windrvrfds_count-1] = fd = (*func) (pathname, flags, mode);
01b99d52 502#endif
723d9aa0 503
504 return fd;
cdc711dc 505 }
506
723d9aa0 507 return (*func) (pathname, flags, mode);
cdc711dc 508}
509
723d9aa0 510int close(int fd) {
511 static int (*func) (int) = NULL;
9bfaca62 512 int i;
cdc711dc 513
723d9aa0 514 if (!func)
f1405f13 515 func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
723d9aa0 516
9bfaca62
MG
517 for (i = 0; i < windrvrfds_count; i++) {
518 if (fd == windrvrfds[i] && windrvrfds[i] >= 0) {
519 int remaining = windrvrfds_count - (i + 1);
520 DPRINTF("close windrvr6 (%d)\n", i);
521 if (remaining)
522 memmove(&(windrvrfds[i]), &(windrvrfds[i+1]), remaining * sizeof(int));
523 windrvrfds = realloc(windrvrfds, sizeof(int) * --windrvrfds_count);
524 if (!windrvrfds_count)
525 windrvrfds = NULL;
526 break;
527 }
cdc711dc 528 }
cdc711dc 529
723d9aa0 530 return (*func) (fd);
cdc711dc 531}
532
723d9aa0 533FILE *fopen(const char *path, const char *mode) {
534 FILE *ret;
535 static FILE* (*func) (const char*, const char*) = NULL;
0dca330e 536 char buf[256];
537 int i;
ca18111b 538
539 if (!func)
f1405f13 540 func = (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT, "fopen");
ca18111b 541
3e670223 542 for (i = 0; i < 4; i++) {
543 snprintf(buf, sizeof(buf), "/proc/sys/dev/parport/parport%d/base-addr", i);
544 if (!strcmp(path, buf)) {
545 DPRINTF("open base-addr of parport%d\n", i);
546 if (config_is_real_pport(i)) {
547 ret = (*func) (path, mode);
548 } else {
549 ret = (*func) ("/dev/null", mode);
550 }
551
552 if (ret) {
553 baseaddrfp = ret;
554 baseaddrnum = i;
555 }
556
557 return ret;
558 }
559 }
560
561 ret = (*func) (path, mode);
ca18111b 562
0dca330e 563 if (!strcmp(path, "/proc/modules")) {
f1405f13 564 DPRINTF("opening /proc/modules\n");
aed36bb3
MG
565 if (!ret && errno == ENOENT) {
566 /* Hmm.. there appears to be no /proc/modules file
567 * fake it then */
568 ret = (*func)("/dev/null", mode);
569 DPRINTF("No /proc/modules -- faking\n");
570 }
723d9aa0 571#ifdef NO_WINDRVR
dbda1264 572 modulesfp = ret;
723d9aa0 573 modules_read = 0;
574#endif
575 }
cdc711dc 576
577 return ret;
578}
579
dbda1264 580char *fgets(char *s, int size, FILE *stream) {
581 static char* (*func) (char*, int, FILE*) = NULL;
16f6b164 582 const char modules[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
0dca330e 583 char buf[256];
dbda1264 584 char *ret = NULL;
585
cdc711dc 586
587 if (!func)
f1405f13 588 func = (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT, "fgets");
723d9aa0 589
dbda1264 590 if (modulesfp == stream) {
54357994 591 if (modules_read < sizeof(modules) / sizeof(modules[0])) {
16f6b164 592 strcpy(s, modules[modules_read]);
dbda1264 593 ret = s;
16f6b164 594 modules_read++;
dbda1264 595 }
0dca330e 596 } else if (baseaddrfp == stream) {
597 snprintf(s, sizeof(buf), "%d\t%d\n",
598 (baseaddrnum) * 0x10,
599 ((baseaddrnum) * 0x10) + 0x400);
600 ret = s;
723d9aa0 601 } else {
dbda1264 602 ret = (*func)(s,size,stream);
723d9aa0 603 }
cdc711dc 604
605 return ret;
606}
607
dbda1264 608int fclose(FILE *fp) {
609 static int (*func) (FILE*) = NULL;
cdc711dc 610
dbda1264 611 if (!func)
f1405f13 612 func = (int (*) (FILE*)) dlsym(RTLD_NEXT, "fclose");
cdc711dc 613
dbda1264 614 if (fp == modulesfp) {
615 modulesfp = NULL;
616 }
0dca330e 617
618 if (fp == baseaddrfp) {
619 baseaddrfp = NULL;
620 }
dbda1264 621
622 return (*func)(fp);
cdc711dc 623}
419f2c98 624
625int access(const char *pathname, int mode) {
626 static int (*func) (const char*, int);
627
628 if (!func)
f1405f13 629 func = (int (*) (const char*, int)) dlsym(RTLD_NEXT, "access");
3eee002c 630
b8b756f5 631 if (pathname && !strcmp(pathname, "/dev/windrvr6")) {
419f2c98 632 return 0;
633 } else {
634 return (*func)(pathname, mode);
635 }
636}
c42237a3 637
1fc8f7a4
MG
638#if 0
639/* USB cable sharing needs to overload semop, TODO! */
640int semop (int __semid, struct sembuf *__sops, size_t __nsops) {
641 static int (*func) (int, struct sembuf*, size_t) = NULL;
642 int i;
643
644 if (!func)
645 func = (int (*) (int, struct sembuf*, size_t)) dlsym(RTLD_NEXT, "semop");
646
647 fprintf(stderr,"semop: semid: 0x%X, elements: %d\n", __semid, __nsops);
648 for (i = 0; i < __nsops; i++) {
649 fprintf(stderr, " num: %u, op: %d, flg: %d\n", __sops[i].sem_num, __sops[i].sem_op, __sops[i].sem_flg);
650 if (__sops[i].sem_op < 0) {
651 fprintf(stderr, "SEMAPHORE LOCK\n");
652 } else {
653 fprintf(stderr, "SEMAPHORE UNLOCK\n");
654 }
655 }
656
657 return (*func)(__semid, __sops, __nsops);
658}
659#endif
660
9f78065e 661/*
b05f6dfe
MG
662 * Ugly hack for ISE 12. Preload doesn't seem to work correctly for
663 * libImpactComm.so. Even though the file is still read with fopen(),
664 * the version from libc is used and not the one from this file.
665 * Replace the function calling fopen() instead...
9f78065e
MG
666 * echo '_Z14isModuleLoadedPci' | c++filt
667 */
80a14787 668long int _Z14isModuleLoadedPci(char *module_name, int i) {
9f78065e 669 DPRINTF("_Z14isModuleLoadedPci: Checking for module %s (%d)\n", module_name, i);
4ba5d6d9
MG
670
671 return 1;
672}
daa4fc04
MG
673
674static void __attribute__ ((constructor)) libusbdriver_init(void) {
b05f6dfe
MG
675 int i;
676 char buf[256];
677 char buf2[256];
678
679 for (i = 0; i < 4; i++) {
680 snprintf(buf, sizeof(buf), "XIL_IMPACT_ENV_LPT%d_BASE_ADDRESS", i+1);
681 snprintf(buf2, sizeof(buf2), "%x", 0x10*i);
682 setenv(buf, buf2, 1);
683 snprintf(buf, sizeof(buf), "XIL_IMPACT_ENV_LPT%d_ECP_ADDRESS", i+1);
684 snprintf(buf2, sizeof(buf2), "%x", (0x10*i)+0x400);
685 setenv(buf, buf2, 1);
686 }
687
688 setenv("XIL_IMPACT_USE_LIBUSB", "0", 1);
689 setenv("XIL_IMPACT_USE_WINDRIVER", "1", 1);
690
daa4fc04
MG
691 #if __WORDSIZE == 32
692 struct utsname un;
693 int ret;
694
695 ret = uname(&un);
696
697 if (ret == 0 && (!strcmp(un.machine, "x86_64"))) {
698 DPRINTF("setting 32bit personality\n");
699 (long)syscall(SYS_personality, PER_LINUX32);
700 }
701 #endif
702}
Impressum, Datenschutz