add ISE 12 to the list of supported software
[usb-driver] / xpcu.c
diff --git a/xpcu.c b/xpcu.c
index 9cea2c1b26d22868eca1b97a47217a0b71731574..56e584c70af16eeeeee899066b489337e7e0c9f5 100644 (file)
--- a/xpcu.c
+++ b/xpcu.c
@@ -6,20 +6,36 @@
 #include <usb.h>
 #include <errno.h>
 #include <pthread.h>
+#include <stdint.h>
 #include "usb-driver.h"
 #include "xpcu.h"
 
+struct xpcu_s {
+       struct usb_device *dev;
+       usb_dev_handle *handle;
+       int interface;
+       int alternate;
+       unsigned long card_type;
+};
+
+struct xpcu_event_s {
+       struct xpcu_s *xpcu;
+       int count;
+       int interrupt_count;
+       pthread_mutex_t interrupt;
+};
+
 static struct usb_bus *busses = NULL;
-static pthread_mutex_t dummy_interrupt = PTHREAD_MUTEX_INITIALIZER;
 
-int xpcu_deviceinfo(struct xpcu_s *xpcu, struct usb_get_device_data *ugdd) {
+int xpcu_deviceinfo(struct usb_get_device_data *ugdd) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)ugdd->dwUniqueID;
        int i,j,k,l;
        int len = 0;
        unsigned char *buf = NULL;
        WDU_CONFIGURATION **pConfigs, **pActiveConfig;
        WDU_INTERFACE **pActiveInterface;
 
-       if (ugdd->dwUniqueID != (unsigned long)xpcu)
+       if (!xpcu)
                return -ENODEV;
 
        if (ugdd->dwBytes)
@@ -176,7 +192,9 @@ int xpcu_deviceinfo(struct xpcu_s *xpcu, struct usb_get_device_data *ugdd) {
                }
        }
 
-       return len;
+       ugdd->dwBytes = len;
+
+       return 0;
 }
 
 static int xpcu_claim(struct xpcu_s *xpcu, int claim) {
@@ -204,18 +222,21 @@ static int xpcu_claim(struct xpcu_s *xpcu, int claim) {
                if (!claimed)
                        return 0;
 
+#if 0
                ret = usb_release_interface(xpcu->handle, xpcu->interface);
                if (!ret)
                        claimed = 0;
+#endif
        }
 
        return ret;
 }
 
-int xpcu_transfer(struct xpcu_s *xpcu, struct usb_transfer *ut) {
+int xpcu_transfer(struct usb_transfer *ut) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)ut->dwUniqueID;
        int ret = 0;
 
-       if (ut->dwUniqueID != (unsigned long)xpcu)
+       if (!xpcu)
                return -ENODEV;
 
        xpcu_claim(xpcu, XPCU_CLAIM);
@@ -248,9 +269,11 @@ int xpcu_transfer(struct xpcu_s *xpcu, struct usb_transfer *ut) {
        return ret;
 }
 
-void xpcu_set_interface(struct xpcu_s *xpcu, struct usb_set_interface *usi) {
-       if (usi->dwUniqueID != (unsigned long)xpcu)
-               return;
+int xpcu_set_interface(struct usb_set_interface *usi) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)usi->dwUniqueID;
+
+       if (!xpcu)
+               return -ENODEV;
 
        if (xpcu->dev) {
                if (!xpcu->handle) {
@@ -266,6 +289,8 @@ void xpcu_set_interface(struct xpcu_s *xpcu, struct usb_set_interface *usi) {
                xpcu->interface = xpcu->dev->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
                xpcu->alternate = usi->dwAlternateSetting;
        }
+
+       return 0;
 }
 
 static void xpcu_init(void) {
@@ -280,20 +305,27 @@ static void xpcu_init(void) {
 }
 
 
-struct xpcu_s *xpcu_find(struct event *e) {
-       static struct xpcu_s xpcu;
-       char* devpos;
+int xpcu_find(struct event *e) {
+       struct xpcu_event_s *xpcu_event = NULL;
+       struct xpcu_s *xpcu = NULL;
+       char* usbdev;
        struct usb_bus *bus;
        int busnum = -1, devnum = -1;
        int i;
 
-       bzero(&xpcu, sizeof(xpcu));
+       e->handle = (unsigned long)NULL;
+
        xpcu_init();
 
-       devpos = getenv("XILINX_USB_DEV");
-       if (devpos != NULL) {
+       usbdev = getenv("XILINX_USB_DEV");
+       if (usbdev != NULL) {
                int j;
                char *devstr = NULL, *remainder;
+               char *devpos;
+
+               devpos = strdup(usbdev);
+               if (!devpos)
+                       return -ENOMEM;
 
                DPRINTF("XILINX_USB_DEV=%s\n", devpos);
 
@@ -319,8 +351,19 @@ struct xpcu_s *xpcu_find(struct event *e) {
                                }
                        }
                }
+               free(devpos);
        }
 
+       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);
+
        for (i = 0; i < e->dwNumMatchTables; i++) {
 
                DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
@@ -360,15 +403,24 @@ struct xpcu_s *xpcu_find(struct event *e) {
 
                                                        if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
                                                                        (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
+                                                               int n = xpcu_event->count;
+
                                                                /* TODO: check interfaceClass! */
                                                                DPRINTF("found device with libusb\n");
 
-                                                               xpcu.interface = -1;
-                                                               xpcu.alternate = -1;
-                                                               xpcu.dev = dev;
-                                                               xpcu.card_type = e->dwCardType;
-                                                               pthread_mutex_init(&xpcu.interrupt, NULL);
-                                                               e->handle = (unsigned long)&xpcu;
+                                                               xpcu = realloc(xpcu, sizeof(struct xpcu_s) * (++xpcu_event->count));
+                                                               if (!xpcu) {
+                                                                       free(xpcu_event);
+                                                                       return -ENOMEM;
+                                                               }
+
+                                                               bzero(&(xpcu[n]), sizeof(struct xpcu_s));
+                                                               xpcu[n].interface = -1;
+                                                               xpcu[n].alternate = -1;
+                                                               xpcu[n].dev = dev;
+                                                               xpcu[n].card_type = e->dwCardType;
+
+                                                               xpcu_event->xpcu = xpcu;
                                                        }
                                                }
                                        }
@@ -377,22 +429,25 @@ struct xpcu_s *xpcu_find(struct event *e) {
                }
        }
 
-       if (!xpcu.dev) {
-               e->handle = 0;
-               return NULL;
-       }
+       e->handle = (unsigned long)xpcu_event;
 
-       return &xpcu;
+       return 0;
 }
 
-void xpcu_found(struct xpcu_s *xpcu, struct event *e) {
-       if (e->handle && e->handle == (unsigned long)xpcu && xpcu->dev) {
+int xpcu_found(struct event *e) {
+       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) {
                struct usb_interface *interface = xpcu->dev->config->interface;
 
                e->dwCardType = xpcu->card_type;
                e->dwAction = 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].ProductId = xpcu->dev->descriptor.idProduct;
                e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
@@ -401,59 +456,72 @@ void xpcu_found(struct xpcu_s *xpcu, struct event *e) {
                e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
                e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
        }
+
+       return 0;
 }
 
-void xpcu_close(struct xpcu_s *xpcu, struct event *e) {
-       if (e->handle != (unsigned long)xpcu)
-               return;
+int xpcu_close(struct event *e) {
+       struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)e->handle;
 
-       if(xpcu) {
-               if (xpcu->handle) {
-                       xpcu_claim(xpcu, XPCU_RELEASE);
-                       usb_close(xpcu->handle);
+       if (!xpcu_event)
+               return -ENODEV;
+
+       if(xpcu_event) {
+               struct  xpcu_s *xpcu;
+               int i;
+
+               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);
+                       }
                }
 
-               xpcu->handle = NULL;
-               xpcu->interface = -1;
-               xpcu->alternate = -1;
+               if (xpcu_event->xpcu)
+                       free(xpcu_event->xpcu);
+
                busses = NULL;
+               free(xpcu_event);
        }
+
+       return 0;
 }
 
-void xpcu_int_state(struct xpcu_s *xpcu, struct interrupt *it, int enable) {
-       static pthread_mutex_t *interrupt = &dummy_interrupt;
+int xpcu_int_state(struct interrupt *it, int enable) {
+       struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
 
-       if (it->hInterrupt != (unsigned long)xpcu)
-               return;
-       
-       if (xpcu)
-               interrupt = &xpcu->interrupt;
+       if (!xpcu_event)
+               return -ENODEV;
        
        if (enable == ENABLE_INTERRUPT) {
                it->fEnableOk = 1;
                it->fStopped = 0;
-               pthread_mutex_trylock(interrupt);
+               it->dwCounter = 0;
+               pthread_mutex_trylock(&xpcu_event->interrupt);
        } else {
                it->dwCounter = 0;
                it->fStopped = 1;
-               if (pthread_mutex_trylock(interrupt) == EBUSY)
-                       pthread_mutex_unlock(interrupt);
+               if (pthread_mutex_trylock(&xpcu_event->interrupt) == EBUSY)
+                       pthread_mutex_unlock(&xpcu_event->interrupt);
        }
+
+       return 0;
 }
 
-void xpcu_int_wait(struct xpcu_s *xpcu, struct interrupt *it) {
-       if (it->hInterrupt != (unsigned long)xpcu)
-               return;
-       
-       if (xpcu) {
-               if (it->dwCounter == 0) {
-                       it->dwCounter = 1;
-               } else {
-                       pthread_mutex_lock(&xpcu->interrupt);
-                       pthread_mutex_unlock(&xpcu->interrupt);
-               }
+int xpcu_int_wait(struct interrupt *it) {
+       struct xpcu_event_s *xpcu_event = (struct xpcu_event_s*)it->hInterrupt;
+
+       if (!xpcu_event)
+               return -ENODEV;
+
+       if (it->dwCounter < xpcu_event->count) {
+               it->dwCounter++;
        } else {
-               pthread_mutex_lock(&dummy_interrupt);
-               pthread_mutex_unlock(&dummy_interrupt);
+               pthread_mutex_lock(&xpcu_event->interrupt);
+               pthread_mutex_unlock(&xpcu_event->interrupt);
        }
+       xpcu_event->interrupt_count++;
+
+       return 0;
 }
Impressum, Datenschutz