1
0

implement support for multiple XPCU cables for ISE 10.1

this also solves the crash of impact 10.1 which was previously
attributed to a mutex deadlock, but impact itself leaks filedescriptors.
This commit is contained in:
Michael Gernoth 2008-04-12 17:20:15 +02:00
parent 0a2341bb36
commit 9bfaca620d
2 changed files with 93 additions and 44 deletions

View File

@ -44,7 +44,8 @@
#include "xpcu.h" #include "xpcu.h"
static int (*ioctl_func) (int, int, void *) = NULL; static int (*ioctl_func) (int, int, void *) = NULL;
static int windrvrfd = -1; static int *windrvrfds = NULL;
static int windrvrfds_count = 0;
static unsigned long ppbase = 0; static unsigned long ppbase = 0;
static unsigned long ecpbase = 0; static unsigned long ecpbase = 0;
static struct parport_config *pport = NULL; static struct parport_config *pport = NULL;
@ -451,7 +452,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
int ioctl(int fd, unsigned long int request, ...) { int ioctl(int fd, unsigned long int request, ...) {
va_list args; va_list args;
void *argp; void *argp;
int ret; int i;
if (!ioctl_func) if (!ioctl_func)
ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl"); ioctl_func = (int (*) (int, int, void *)) dlsym (RTLD_NEXT, "ioctl");
@ -460,12 +461,12 @@ int ioctl(int fd, unsigned long int request, ...) {
argp = va_arg (args, void *); argp = va_arg (args, void *);
va_end (args); va_end (args);
if (fd == windrvrfd) for (i = 0; i < windrvrfds_count; i++) {
ret = do_wdioctl(fd, request, argp); if (fd == windrvrfds[i])
else return do_wdioctl(fd, request, argp);
ret = (*ioctl_func) (fd, request, argp); }
return ret; return (*ioctl_func) (fd, request, argp);
} }
int open (const char *pathname, int flags, ...) { int open (const char *pathname, int flags, ...) {
@ -484,11 +485,15 @@ int open (const char *pathname, int flags, ...) {
} }
if (!strcmp (pathname, "/dev/windrvr6")) { if (!strcmp (pathname, "/dev/windrvr6")) {
DPRINTF("opening windrvr6\n"); DPRINTF("opening windrvr6 (%d)\n", windrvrfds_count);
windrvrfds = realloc(windrvrfds, sizeof(int) * (++windrvrfds_count));
if (!windrvrfds)
return -ENOMEM;
#ifdef NO_WINDRVR #ifdef NO_WINDRVR
windrvrfd = fd = (*func) ("/dev/null", flags, mode); windrvrfds[windrvrfds_count-1] = fd = (*func) ("/dev/null", flags, mode);
#else #else
windrvrfd = fd = (*func) (pathname, flags, mode); windrvrfds[windrvrfds_count-1] = fd = (*func) (pathname, flags, mode);
#endif #endif
return fd; return fd;
@ -499,13 +504,22 @@ int open (const char *pathname, int flags, ...) {
int close(int fd) { int close(int fd) {
static int (*func) (int) = NULL; static int (*func) (int) = NULL;
int i;
if (!func) if (!func)
func = (int (*) (int)) dlsym(RTLD_NEXT, "close"); func = (int (*) (int)) dlsym(RTLD_NEXT, "close");
if (fd == windrvrfd && windrvrfd >= 0) { for (i = 0; i < windrvrfds_count; i++) {
DPRINTF("close windrvrfd\n"); if (fd == windrvrfds[i] && windrvrfds[i] >= 0) {
windrvrfd = -1; int remaining = windrvrfds_count - (i + 1);
DPRINTF("close windrvr6 (%d)\n", i);
if (remaining)
memmove(&(windrvrfds[i]), &(windrvrfds[i+1]), remaining * sizeof(int));
windrvrfds = realloc(windrvrfds, sizeof(int) * --windrvrfds_count);
if (!windrvrfds_count)
windrvrfds = NULL;
break;
}
} }
return (*func) (fd); return (*func) (fd);

97
xpcu.c
View File

@ -15,6 +15,12 @@ struct xpcu_s {
int interface; int interface;
int alternate; int alternate;
unsigned long card_type; unsigned long card_type;
};
struct xpcu_event_s {
struct xpcu_s *xpcu;
int count;
int interrupt_count;
pthread_mutex_t interrupt; pthread_mutex_t interrupt;
}; };
@ -300,14 +306,27 @@ static void xpcu_init(void) {
int xpcu_find(struct event *e) { int xpcu_find(struct event *e) {
struct xpcu_event_s *xpcu_event = NULL;
struct xpcu_s *xpcu = NULL; struct xpcu_s *xpcu = NULL;
char* devpos; char* devpos;
struct usb_bus *bus; struct usb_bus *bus;
int busnum = -1, devnum = -1; int busnum = -1, devnum = -1;
int i; int i;
e->handle = (unsigned long)NULL;
xpcu_init(); xpcu_init();
xpcu_event = malloc(sizeof(struct xpcu_event_s));
if (!xpcu_event)
return -ENOMEM;
bzero(xpcu_event, sizeof(struct xpcu_event_s));
xpcu_event->xpcu = NULL;
xpcu_event->count = 0;
xpcu_event->interrupt_count = 0;
pthread_mutex_init(&xpcu_event->interrupt, NULL);
devpos = getenv("XILINX_USB_DEV"); devpos = getenv("XILINX_USB_DEV");
if (devpos != NULL) { if (devpos != NULL) {
int j; int j;
@ -378,20 +397,24 @@ int xpcu_find(struct event *e) {
if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) && if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
(interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){ (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
int n = xpcu_event->count;
/* TODO: check interfaceClass! */ /* TODO: check interfaceClass! */
DPRINTF("found device with libusb\n"); DPRINTF("found device with libusb\n");
xpcu = malloc(sizeof(struct xpcu_s)); xpcu = realloc(xpcu, sizeof(struct xpcu_s) * (++xpcu_event->count));
if (!xpcu) if (!xpcu) {
free(xpcu_event);
return -ENOMEM; return -ENOMEM;
}
bzero(xpcu, sizeof(struct xpcu_s)); bzero(&(xpcu[n]), sizeof(struct xpcu_s));
xpcu->interface = -1; xpcu[n].interface = -1;
xpcu->alternate = -1; xpcu[n].alternate = -1;
xpcu->dev = dev; xpcu[n].dev = dev;
xpcu->card_type = e->dwCardType; xpcu[n].card_type = e->dwCardType;
pthread_mutex_init(&xpcu->interrupt, NULL);
e->handle = (unsigned long)&xpcu; xpcu_event->xpcu = xpcu;
} }
} }
} }
@ -400,13 +423,17 @@ int xpcu_find(struct event *e) {
} }
} }
e->handle = (unsigned long)xpcu; e->handle = (unsigned long)xpcu_event;
return 0; return 0;
} }
int xpcu_found(struct event *e) { int xpcu_found(struct event *e) {
struct xpcu_s *xpcu = (struct xpcu_s*)e->handle; struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
struct xpcu_s *xpcu = NULL;
if (xpcu_event && xpcu_event->count && (xpcu_event->interrupt_count <= xpcu_event->count))
xpcu = &(xpcu_event->xpcu[xpcu_event->interrupt_count-1]);
if (xpcu && xpcu->dev) { if (xpcu && xpcu->dev) {
struct usb_interface *interface = xpcu->dev->config->interface; struct usb_interface *interface = xpcu->dev->config->interface;
@ -414,7 +441,7 @@ int xpcu_found(struct event *e) {
e->dwCardType = xpcu->card_type; e->dwCardType = xpcu->card_type;
e->dwAction = 1; e->dwAction = 1;
e->dwEventId = 1; e->dwEventId = 1;
e->u.Usb.dwUniqueID = e->handle; e->u.Usb.dwUniqueID = (unsigned long)xpcu;
e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor; e->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor;
e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct; e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct;
e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass; e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
@ -428,34 +455,44 @@ int xpcu_found(struct event *e) {
} }
int xpcu_close(struct event *e) { int xpcu_close(struct event *e) {
struct xpcu_s *xpcu = (struct xpcu_s*)e->handle; struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
if (!xpcu) if (!xpcu_event)
return -ENODEV; return -ENODEV;
if(xpcu) { if(xpcu_event) {
if (xpcu->handle) { struct xpcu_s *xpcu;
xpcu_claim(xpcu, XPCU_RELEASE); int i;
usb_close(xpcu->handle);
for (i = 0; i < xpcu_event->count; i++) {
xpcu = &(xpcu_event->xpcu[i]);
if (xpcu->handle) {
xpcu_claim(xpcu, XPCU_RELEASE);
usb_close(xpcu->handle);
}
} }
if (xpcu_event->xpcu)
free(xpcu_event->xpcu);
busses = NULL; busses = NULL;
free(xpcu); free(xpcu_event);
} }
return 0; return 0;
} }
int xpcu_int_state(struct interrupt *it, int enable) { int xpcu_int_state(struct interrupt *it, int enable) {
struct xpcu_s *xpcu = (struct xpcu_s*)it->hInterrupt; struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
pthread_mutex_t *interrupt = &dummy_interrupt; pthread_mutex_t *interrupt = &dummy_interrupt;
if (xpcu) if (xpcu_event)
interrupt = &xpcu->interrupt; interrupt = &xpcu_event->interrupt;
if (enable == ENABLE_INTERRUPT) { if (enable == ENABLE_INTERRUPT) {
it->fEnableOk = 1; it->fEnableOk = 1;
it->fStopped = 0; it->fStopped = 0;
it->dwCounter = 0;
pthread_mutex_trylock(interrupt); pthread_mutex_trylock(interrupt);
} else { } else {
it->dwCounter = 0; it->dwCounter = 0;
@ -468,18 +505,16 @@ int xpcu_int_state(struct interrupt *it, int enable) {
} }
int xpcu_int_wait(struct interrupt *it) { int xpcu_int_wait(struct interrupt *it) {
struct xpcu_s *xpcu = (struct xpcu_s*)it->hInterrupt; struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
if (it->hInterrupt != (unsigned long)xpcu) if (xpcu_event) {
return -ENODEV; if (it->dwCounter < xpcu_event->count) {
it->dwCounter++;
if (xpcu) {
if (it->dwCounter == 0) {
it->dwCounter = 1;
} else { } else {
pthread_mutex_lock(&xpcu->interrupt); pthread_mutex_lock(&xpcu_event->interrupt);
pthread_mutex_unlock(&xpcu->interrupt); pthread_mutex_unlock(&xpcu_event->interrupt);
} }
xpcu_event->interrupt_count++;
} else { } else {
pthread_mutex_lock(&dummy_interrupt); pthread_mutex_lock(&dummy_interrupt);
pthread_mutex_unlock(&dummy_interrupt); pthread_mutex_unlock(&dummy_interrupt);