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