]>
cvs.zerfleddert.de Git - usb-driver/blob - usb-driver.c
1 /* libusb/ppdev connector for XILINX impact
3 * Copyright (c) 2007 Michael Gernoth <michael@gernoth.net>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
32 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/utsname.h>
41 #include <bits/wordsize.h>
46 #include <linux/personality.h>
47 #include "usb-driver.h"
51 static int (*ioctl_func
) (int, int, void *) = NULL
;
52 static int *windrvrfds
= NULL
;
53 static int windrvrfds_count
= 0;
54 static unsigned long ppbase
= 0;
55 static unsigned long ecpbase
= 0;
56 static struct parport_config
*pport
= NULL
;
57 static FILE *modulesfp
= NULL
;
58 static FILE *baseaddrfp
= NULL
;
59 static int baseaddrnum
= 0;
60 static int modules_read
= 0;
64 void hexdump(unsigned char *buf
, int len
, char *prefix
) {
67 fprintf(stderr
, "%s %03x: ", prefix
, i
);
68 for(i
= 0; i
<len
; i
++) {
69 fprintf(stderr
,"%02x ", buf
[i
]);
72 if (((i
% 16) == 15) && ((i
+1) < len
))
73 fprintf(stderr
,"\n%s %03x: ", prefix
, i
+1);
79 static int do_wdioctl(int fd
, unsigned int request
, unsigned char *wdioctl
) {
80 struct header_struct
* wdheader
= (struct header_struct
*)wdioctl
;
81 struct version_struct
*version
;
84 if (wdheader
->magic
!= MAGIC
) {
85 fprintf(stderr
,"!!!ERROR: magic header does not match!!!\n");
86 return (*ioctl_func
) (fd
, request
, wdioctl
);
89 switch(request
& ~(0xc0000000)) {
91 version
= (struct version_struct
*)(wdheader
->data
);
92 strcpy(version
->version
, "libusb-driver.so version: " USB_DRIVER_VERSION
);
93 version
->versionul
= 802;
101 case CARD_REGISTER_OLD
:
103 DPRINTF("CARD_REGISTER\n");
105 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
107 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
109 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
110 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
111 cr
->Card
.Item
[0].I
.IO
.dwBar
);
113 DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
115 (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
,
116 cr
->Card
.Item
[1].I
.IO
.dwBytes
,
117 cr
->Card
.Item
[1].I
.IO
.dwBar
);
119 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
123 pport
= config_get((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
127 ret
= pport
->open((unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
/ 0x10);
129 ppbase
= (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
;
131 if (cr
->Card
.dwItems
> 1 && cr
->Card
.Item
[1].I
.IO
.dwAddr
)
132 ecpbase
= (unsigned long)cr
->Card
.Item
[1].I
.IO
.dwAddr
;
140 DPRINTF("<-hCard: %lu\n", cr
->hCard
);
145 DPRINTF("USB_TRANSFER\n");
147 struct usb_transfer
*ut
= (struct usb_transfer
*)(wdheader
->data
);
150 DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
151 ut
->dwUniqueID
, ut
->dwPipeNum
, ut
->fRead
,
152 ut
->dwOptions
, ut
->dwBufferSize
, ut
->dwTimeout
);
153 if (ut
->dwPipeNum
== 0) {
154 DPRINTF("-> setup packet:");
155 hexdump(ut
->SetupPacket
, 8, "");
158 if (!ut
->fRead
&& ut
->dwBufferSize
)
160 hexdump(ut
->pBuffer
, ut
->dwBufferSize
, "->");
165 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
167 ret
= xpcu_transfer(ut
);
171 DPRINTF("Transferred: %lu (%s)\n",ut
->dwBytesTransferred
, (ut
->fRead
?"read":"write"));
172 if (ut
->fRead
&& ut
->dwBytesTransferred
)
174 hexdump(ut
->pBuffer
, ut
->dwBytesTransferred
, "<-");
182 DPRINTF("INT_ENABLE\n");
184 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
186 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
187 it
->hInterrupt
, it
->dwOptions
,
188 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
189 it
->dwLost
, it
->fStopped
);
192 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
194 ret
= xpcu_int_state(it
, ENABLE_INTERRUPT
);
197 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
198 it
->hInterrupt
, it
->dwOptions
,
199 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
200 it
->dwLost
, it
->fStopped
);
206 DPRINTF("INT_DISABLE\n");
208 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
210 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
211 it
->hInterrupt
, it
->dwOptions
,
212 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
213 it
->dwLost
, it
->fStopped
);
215 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
217 ret
= xpcu_int_state(it
, DISABLE_INTERRUPT
);
219 DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
220 it
->hInterrupt
, it
->dwOptions
,
221 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
222 it
->dwLost
, it
->fStopped
);
226 case USB_SET_INTERFACE
:
227 DPRINTF("USB_SET_INTERFACE\n");
229 struct usb_set_interface
*usi
= (struct usb_set_interface
*)(wdheader
->data
);
231 DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
232 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
233 usi
->dwAlternateSetting
, usi
->dwOptions
);
235 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
237 ret
= xpcu_set_interface(usi
);
239 DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
240 usi
->dwUniqueID
, usi
->dwInterfaceNum
,
241 usi
->dwAlternateSetting
, usi
->dwOptions
);
245 case USB_GET_DEVICE_DATA_OLD
:
246 case USB_GET_DEVICE_DATA
:
247 DPRINTF("USB_GET_DEVICE_DATA\n");
249 struct usb_get_device_data
*ugdd
= (struct usb_get_device_data
*)(wdheader
->data
);
251 DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
252 ugdd
->dwUniqueID
, ugdd
->dwBytes
,
255 ret
= xpcu_deviceinfo(ugdd
);
260 case EVENT_REGISTER_OLD
:
262 DPRINTF("EVENT_REGISTER\n");
264 struct event
*e
= (struct event
*)(wdheader
->data
);
269 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",
270 e
->handle
, e
->dwAction
,
271 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
272 e
->hKernelPlugIn
, e
->dwOptions
,
273 e
->u
.Usb
.deviceId
.dwVendorId
,
274 e
->u
.Usb
.deviceId
.dwProductId
,
275 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
276 e
->dwNumMatchTables
);
279 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
285 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",
286 e
->handle
, e
->dwAction
,
287 e
->dwStatus
, e
->dwEventId
, e
->dwCardType
,
288 e
->hKernelPlugIn
, e
->dwOptions
,
289 e
->u
.Usb
.deviceId
.dwVendorId
,
290 e
->u
.Usb
.deviceId
.dwProductId
,
291 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
292 e
->dwNumMatchTables
);
294 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
295 DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
296 e
->matchTables
[i
].VendorId
,
297 e
->matchTables
[i
].ProductId
,
298 e
->matchTables
[i
].bDeviceClass
,
299 e
->matchTables
[i
].bDeviceSubClass
,
300 e
->matchTables
[i
].bInterfaceClass
,
301 e
->matchTables
[i
].bInterfaceSubClass
,
302 e
->matchTables
[i
].bInterfaceProtocol
);
309 DPRINTF("TRANSFER\n");
311 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
314 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
316 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, 1);
321 case MULTI_TRANSFER_OLD
:
323 DPRINTF("MULTI_TRANSFER\n");
325 WD_TRANSFER
*tr
= (WD_TRANSFER
*)(wdheader
->data
);
326 unsigned long num
= wdheader
->size
/sizeof(WD_TRANSFER
);
328 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
330 ret
= pport
->transfer(tr
, fd
, request
, ppbase
, ecpbase
, num
);
335 case EVENT_UNREGISTER
:
337 struct event
*e
= (struct event
*)(wdheader
->data
);
339 DPRINTF("EVENT_UNREGISTER\n");
341 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
349 DPRINTF("INT_WAIT\n");
351 struct interrupt
*it
= (struct interrupt
*)(wdheader
->data
);
353 DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
354 it
->hInterrupt
, it
->dwOptions
,
355 it
->dwCmds
, it
->fEnableOk
, it
->dwCounter
,
356 it
->dwLost
, it
->fStopped
);
359 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
361 ret
= xpcu_int_wait(it
);
364 DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
365 it
->hInterrupt
, it
->dwOptions
, it
->dwCmds
,
366 it
->fEnableOk
, it
->dwCounter
, it
->dwLost
,
371 case CARD_UNREGISTER
:
372 DPRINTF("CARD_UNREGISTER\n");
374 struct card_register
* cr
= (struct card_register
*)(wdheader
->data
);
376 DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
377 (unsigned long)cr
->Card
.Item
[0].I
.IO
.dwAddr
,
378 cr
->Card
.Item
[0].I
.IO
.dwBytes
,
379 cr
->Card
.Item
[0].I
.IO
.dwBar
);
381 DPRINTF("-> hCard: %lu\n", cr
->hCard
);
384 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
387 pport
->close(cr
->hCard
);
395 DPRINTF("EVENT_PULL\n");
397 struct event
*e
= (struct event
*)(wdheader
->data
);
401 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",
402 e
->handle
, e
->dwAction
, e
->dwStatus
,
403 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
404 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
405 e
->u
.Usb
.deviceId
.dwProductId
,
406 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
407 e
->dwNumMatchTables
);
409 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
410 DPRINTF("-> match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
411 e
->matchTables
[i
].VendorId
,
412 e
->matchTables
[i
].ProductId
,
413 e
->matchTables
[i
].bDeviceClass
,
414 e
->matchTables
[i
].bDeviceSubClass
,
415 e
->matchTables
[i
].bInterfaceClass
,
416 e
->matchTables
[i
].bInterfaceSubClass
,
417 e
->matchTables
[i
].bInterfaceProtocol
);
421 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
427 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",
428 e
->handle
, e
->dwAction
, e
->dwStatus
,
429 e
->dwEventId
, e
->dwCardType
, e
->hKernelPlugIn
,
430 e
->dwOptions
, e
->u
.Usb
.deviceId
.dwVendorId
,
431 e
->u
.Usb
.deviceId
.dwProductId
,
432 e
->u
.Usb
.dwUniqueID
, e
->dwEventVer
,
433 e
->dwNumMatchTables
);
435 for (i
= 0; i
< e
->dwNumMatchTables
; i
++)
436 DPRINTF("<- match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
437 e
->matchTables
[i
].VendorId
,
438 e
->matchTables
[i
].ProductId
,
439 e
->matchTables
[i
].bDeviceClass
,
440 e
->matchTables
[i
].bDeviceSubClass
,
441 e
->matchTables
[i
].bInterfaceClass
,
442 e
->matchTables
[i
].bInterfaceSubClass
,
443 e
->matchTables
[i
].bInterfaceProtocol
);
450 fprintf(stderr
,"!!!Unsupported IOCTL: %x!!!\n", request
);
452 ret
= (*ioctl_func
) (fd
, request
, wdioctl
);
460 int ioctl(int fd
, unsigned long int request
, ...) {
466 ioctl_func
= (int (*) (int, int, void *)) dlsym (RTLD_NEXT
, "ioctl");
468 va_start (args
, request
);
469 argp
= va_arg (args
, void *);
472 for (i
= 0; i
< windrvrfds_count
; i
++) {
473 if (fd
== windrvrfds
[i
])
474 return do_wdioctl(fd
, request
, argp
);
477 return (*ioctl_func
) (fd
, request
, argp
);
480 int open (const char *pathname
, int flags
, ...) {
481 static int (*func
) (const char *, int, mode_t
) = NULL
;
487 func
= (int (*) (const char *, int, mode_t
)) dlsym (RTLD_NEXT
, "open");
489 if (flags
& O_CREAT
) {
490 va_start(args
, flags
);
491 mode
= va_arg(args
, mode_t
);
495 if (!strcmp (pathname
, "/dev/windrvr6")) {
496 DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count
);
497 windrvrfds
= realloc(windrvrfds
, sizeof(int) * (++windrvrfds_count
));
502 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) ("/dev/null", flags
, mode
);
504 windrvrfds
[windrvrfds_count
-1] = fd
= (*func
) (pathname
, flags
, mode
);
510 return (*func
) (pathname
, flags
, mode
);
514 static int (*func
) (int) = NULL
;
518 func
= (int (*) (int)) dlsym(RTLD_NEXT
, "close");
520 for (i
= 0; i
< windrvrfds_count
; i
++) {
521 if (fd
== windrvrfds
[i
] && windrvrfds
[i
] >= 0) {
522 int remaining
= windrvrfds_count
- (i
+ 1);
523 DPRINTF("close windrvr6 (%d)\n", i
);
525 memmove(&(windrvrfds
[i
]), &(windrvrfds
[i
+1]), remaining
* sizeof(int));
526 windrvrfds
= realloc(windrvrfds
, sizeof(int) * --windrvrfds_count
);
527 if (!windrvrfds_count
)
536 FILE *fopen(const char *path
, const char *mode
) {
538 static FILE* (*func
) (const char*, const char*) = NULL
;
543 func
= (FILE* (*) (const char*, const char*)) dlsym(RTLD_NEXT
, "fopen");
545 for (i
= 0; i
< 4; i
++) {
546 snprintf(buf
, sizeof(buf
), "/proc/sys/dev/parport/parport%d/base-addr", i
);
547 if (!strcmp(path
, buf
)) {
548 DPRINTF("open base-addr of parport%d\n", i
);
549 if (config_is_real_pport(i
)) {
550 ret
= (*func
) (path
, mode
);
552 ret
= (*func
) ("/dev/null", mode
);
564 ret
= (*func
) (path
, mode
);
566 if (!strcmp(path
, "/proc/modules")) {
567 DPRINTF("opening /proc/modules\n");
568 if (!ret
&& errno
== ENOENT
) {
569 /* Hmm.. there appears to be no /proc/modules file
571 ret
= (*func
)("/dev/null", mode
);
572 DPRINTF("No /proc/modules -- faking\n");
583 char *fgets(char *s
, int size
, FILE *stream
) {
584 static char* (*func
) (char*, int, FILE*) = NULL
;
585 const char modules
[][256] = {"windrvr6 1 0 - Live 0xdeadbeef\n", "parport_pc 1 0 - Live 0xdeadbeef\n"};
591 func
= (char* (*) (char*, int, FILE*)) dlsym(RTLD_NEXT
, "fgets");
593 if (modulesfp
== stream
) {
594 if (modules_read
< sizeof(modules
) / sizeof(modules
[0])) {
595 strcpy(s
, modules
[modules_read
]);
599 } else if (baseaddrfp
== stream
) {
600 snprintf(s
, sizeof(buf
), "%d\t%d\n",
601 (baseaddrnum
) * 0x10,
602 ((baseaddrnum
) * 0x10) + 0x400);
605 ret
= (*func
)(s
,size
,stream
);
611 int fclose(FILE *fp
) {
612 static int (*func
) (FILE*) = NULL
;
615 func
= (int (*) (FILE*)) dlsym(RTLD_NEXT
, "fclose");
617 if (fp
== modulesfp
) {
621 if (fp
== baseaddrfp
) {
628 int access(const char *pathname
, int mode
) {
629 static int (*func
) (const char*, int);
632 func
= (int (*) (const char*, int)) dlsym(RTLD_NEXT
, "access");
634 if (pathname
&& !strcmp(pathname
, "/dev/windrvr6")) {
637 return (*func
)(pathname
, mode
);
642 /* USB cable sharing needs to overload semop, TODO! */
643 int semop (int __semid
, struct sembuf
*__sops
, size_t __nsops
) {
644 static int (*func
) (int, struct sembuf
*, size_t) = NULL
;
648 func
= (int (*) (int, struct sembuf
*, size_t)) dlsym(RTLD_NEXT
, "semop");
650 fprintf(stderr
,"semop: semid: 0x%X, elements: %d\n", __semid
, __nsops
);
651 for (i
= 0; i
< __nsops
; i
++) {
652 fprintf(stderr
, " num: %u, op: %d, flg: %d\n", __sops
[i
].sem_num
, __sops
[i
].sem_op
, __sops
[i
].sem_flg
);
653 if (__sops
[i
].sem_op
< 0) {
654 fprintf(stderr
, "SEMAPHORE LOCK\n");
656 fprintf(stderr
, "SEMAPHORE UNLOCK\n");
660 return (*func
)(__semid
, __sops
, __nsops
);
665 * Ugly hack for ISE 12. Preload doesn't seem to work correctly for
666 * libImpactComm.so. Even though the file is still read with fopen(),
667 * the version from libc is used and not the one from this file.
668 * Replace the function calling fopen() instead...
669 * echo '_Z14isModuleLoadedPci' | c++filt
671 long int _Z14isModuleLoadedPci(char *module_name
, int i
) {
672 DPRINTF("_Z14isModuleLoadedPci: Checking for module %s (%d)\n", module_name
, i
);
677 void cpr_segv_handler(int sig
, siginfo_t
*info
, void *context
) {
680 DPRINTF("SEGV at %p, mapping memory\n", info
->si_addr
);
682 newmem
= mmap(info
->si_addr
, 1, PROT_READ
, MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_FIXED
, -1, 0);
683 if (newmem
!= info
->si_addr
) {
684 perror("libusb-driver.so: Can't map memory, crashing now");
689 /* XilCommNS::CPortResources::Instance() */
690 void* _ZN9XilCommNS14CPortResources8InstanceEv() {
691 static void* (*func
) (void) = NULL
;
692 char *filename
= NULL
;
697 struct sigaction act
, oldact
;
698 int sighand_installed
= 0;
700 func
= (void* (*) (void)) dlsym(RTLD_NEXT
, "_ZN9XilCommNS14CPortResources8InstanceEv");
702 DPRINTF("Installing signal-handler for SIGSEGV\n");
703 bzero(&act
, sizeof(struct sigaction
));
704 act
.sa_sigaction
= cpr_segv_handler
;
705 act
.sa_flags
= SA_SIGINFO
;
706 if (sigaction(SIGSEGV
, &act
, &oldact
) == 0) {
707 sighand_installed
= 1;
710 DPRINTF("Searching for filename starting at %p\n", func
);
711 for(i
= 0; i
< 32768; i
++) {
712 if (!strcmp(((char*)func
)+i
, "/proc/sys/dev/parport/%s/base-addr")) {
713 filename
= ((char*)func
)+i
;
714 DPRINTF("Filename found at offset %p\n", (void*)(filename
- ((char*)func
)));
718 if (sighand_installed
) {
719 DPRINTF("Restoring signal-handler for SIGSEGV\n");
720 sigaction(SIGSEGV
, &oldact
, NULL
);
723 fprintf(stderr
, "libusb-driver.so: Can't find memory to patch, parallel cables will probably not work!\n");
730 int len
= strlen(filename
) + 1;
733 pagesize
= sysconf(_SC_PAGE_SIZE
);
734 DPRINTF("You have %lu bytes sized pages!\n", pagesize
);
736 start
= (void*)((long)filename
& (~(pagesize
-1)));
738 protectlen
= pagesize
;
739 if ((long)(filename
+ len
) > (long)(start
+ protectlen
))
740 protectlen
+= pagesize
;
742 DPRINTF("Unprotecting %zd bytes starting at %p\n", protectlen
, start
);
743 ret
= mprotect(start
, protectlen
, PROT_READ
|PROT_WRITE
);
747 DPRINTF("Replacing %s with /dev/zero\n", filename
);
748 strcpy(filename
, "/dev/zero");
750 DPRINTF("Reprotecting %zd bytes starting at %p\n", protectlen
, start
);
751 ret
= mprotect(start
, protectlen
, PROT_READ
|PROT_EXEC
);
756 DPRINTF("-> XilCommNS::CPortResources::Instance()\n");
760 DPRINTF("<- XilCommNS::CPortResources::Instance()\n");
765 static void __attribute__ ((constructor
)) libusbdriver_init(void) {
770 for (i
= 0; i
< 4; i
++) {
771 snprintf(buf
, sizeof(buf
), "XIL_IMPACT_ENV_LPT%d_BASE_ADDRESS", i
+1);
772 snprintf(buf2
, sizeof(buf2
), "%x", 0x10*i
);
773 setenv(buf
, buf2
, 1);
774 snprintf(buf
, sizeof(buf
), "XIL_IMPACT_ENV_LPT%d_ECP_ADDRESS", i
+1);
775 snprintf(buf2
, sizeof(buf2
), "%x", (0x10*i
)+0x400);
776 setenv(buf
, buf2
, 1);
779 setenv("XIL_IMPACT_USE_LIBUSB", "0", 1);
780 setenv("XIL_IMPACT_USE_WINDRIVER", "1", 1);
789 if (ret
== 0 && (!strcmp(un
.machine
, "x86_64"))) {
790 DPRINTF("setting 32bit personality\n");
791 (long)syscall(SYS_personality
, PER_LINUX32
);