fix for mutex deadlock when closing and reopening the cable connection
[usb-driver] / xpcu.c
diff --git a/xpcu.c b/xpcu.c
index 28770fe6eba169a0690d52952443a593d29c7b54..93d04247c86a644497f48bd753d19fb71ba65ab6 100644 (file)
--- a/xpcu.c
+++ b/xpcu.c
@@ -1,17 +1,40 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <string.h>
 #include <strings.h>
 #include <usb.h>
+#include <errno.h>
+#include <pthread.h>
 #include "usb-driver.h"
 #include "xpcu.h"
 
-int xpcu_deviceinfo(struct xpcu_s *xpcu, unsigned char *buf) {
+struct xpcu_s {
+       struct usb_device *dev;
+       usb_dev_handle *handle;
+       int interface;
+       int alternate;
+       unsigned long card_type;
+       pthread_mutex_t interrupt;
+};
+
+static struct usb_bus *busses = NULL;
+static pthread_mutex_t dummy_interrupt = PTHREAD_MUTEX_INITIALIZER;
+
+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 (!xpcu)
+               return -ENODEV;
+
+       if (ugdd->dwBytes)
+               buf = ugdd->pBuf;
+
        if (buf) {
                struct usb_device_info *udi = (struct usb_device_info*)(buf+len);
 
@@ -163,10 +186,12 @@ int xpcu_deviceinfo(struct xpcu_s *xpcu, unsigned char *buf) {
                }
        }
 
-       return len;
+       ugdd->dwBytes = len;
+
+       return 0;
 }
 
-int xpcu_claim(struct xpcu_s *xpcu, int claim) {
+static int xpcu_claim(struct xpcu_s *xpcu, int claim) {
        int ret = 0;
        static int claimed = 0;
 
@@ -199,9 +224,13 @@ int xpcu_claim(struct xpcu_s *xpcu, int claim) {
        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 (!xpcu)
+               return -ENODEV;
+
        xpcu_claim(xpcu, XPCU_CLAIM);
        /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
        if (ut->dwPipeNum == 0) { /* control pipe */
@@ -211,7 +240,7 @@ int xpcu_transfer(struct xpcu_s *xpcu, struct usb_transfer *ut) {
                value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8);
                index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8);
                size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8);
-               DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
+               DPRINTF("-> requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size);
                ret = usb_control_msg(xpcu->handle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout);
        } else {
                if (ut->fRead) {
@@ -232,18 +261,229 @@ int xpcu_transfer(struct xpcu_s *xpcu, struct usb_transfer *ut) {
        return ret;
 }
 
-struct xpcu_s *xpcu_open(void) {
-       static struct xpcu_s xpcu;
+int xpcu_set_interface(struct usb_set_interface *usi) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)usi->dwUniqueID;
+
+       if (!xpcu)
+               return -ENODEV;
 
-       bzero(&xpcu, sizeof(xpcu));
-       xpcu.interface = -1;
-       xpcu.alternate = -1;
+       if (xpcu->dev) {
+               if (!xpcu->handle) {
+                       xpcu->handle = usb_open(xpcu->dev);
+#ifndef NO_USB_RESET
+                       if (xpcu->handle) {
+                               usb_reset(xpcu->handle);
+                               xpcu->handle = usb_open(xpcu->dev);
+                       }
+#endif
+               }
+
+               xpcu->interface = xpcu->dev->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
+               xpcu->alternate = usi->dwAlternateSetting;
+       }
+
+       return 0;
+}
+
+static void xpcu_init(void) {
+       if (busses)
+               return;
 
        usb_init();
        usb_find_busses();
        usb_find_devices();
 
-       xpcu.busses = usb_get_busses();
+       busses = usb_get_busses();
+
+       pthread_mutex_init(&dummy_interrupt, NULL);
+}
+
+
+int xpcu_find(struct event *e) {
+       struct xpcu_s *xpcu = NULL;
+       char* devpos;
+       struct usb_bus *bus;
+       int busnum = -1, devnum = -1;
+       int i;
+
+       xpcu_init();
+
+       devpos = getenv("XILINX_USB_DEV");
+       if (devpos != NULL) {
+               int j;
+               char *devstr = NULL, *remainder;
+
+               DPRINTF("XILINX_USB_DEV=%s\n", devpos);
+
+               for (j = 0; j < strlen(devpos) && devpos[j] != 0; j++) {
+                       if (devpos[j] == ':') {
+                               devpos[j] = 0;
+                               devstr = &(devpos[j+1]);
+                       }
+               }
+
+               if (devstr && strlen(devstr) > 0) {
+                       busnum = strtol(devpos, &remainder, 10);
+                       if (devpos == remainder) {
+                               busnum = -1;
+                       } else {
+                               devnum = strtol(devstr, &remainder, 10);
+                               if (devstr == remainder) {
+                                       busnum = -1;
+                                       devnum = -1;
+                               } else {
+                                       fprintf(stderr,"Using XILINX platform cable USB at %03d:%03d\n",
+                                                       busnum, devnum);
+                               }
+                       }
+               }
+       }
+
+       for (i = 0; i < e->dwNumMatchTables; i++) {
+
+               DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
+                               e->matchTables[i].VendorId,
+                               e->matchTables[i].ProductId,
+                               e->matchTables[i].bDeviceClass,
+                               e->matchTables[i].bDeviceSubClass,
+                               e->matchTables[i].bInterfaceClass,
+                               e->matchTables[i].bInterfaceSubClass,
+                               e->matchTables[i].bInterfaceProtocol);
+
+               for (bus = busses; bus; bus = bus->next) {
+                       struct usb_device *dev;
+
+                       if ((devnum != -1) && (strtol(bus->dirname, NULL, 10) != busnum))
+                               continue;
+
+                       for (dev = bus->devices; dev; dev = dev->next) {
+                               struct usb_device_descriptor *desc = &(dev->descriptor);
+
+                               if((desc->idVendor == e->matchTables[i].VendorId) &&
+                                               (desc->idProduct == e->matchTables[i].ProductId) &&
+                                               (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
+                                               (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
+                                               ((devnum == -1) || (strtol(dev->filename, NULL, 10) == devnum)) ) {
+                                       int ac;
+                                       for (ac = 0; ac < desc->bNumConfigurations; ac++) {
+                                               struct usb_interface *interface = dev->config[ac].interface;
+                                               int ai;
+
+                                               for (ai = 0; ai < interface->num_altsetting; ai++) {
+
+                                                       DPRINTF("intclass: %x, intsubclass: %x, intproto: %x\n",
+                                                                       interface->altsetting[i].bInterfaceClass,
+                                                                       interface->altsetting[i].bInterfaceSubClass,
+                                                                       interface->altsetting[i].bInterfaceProtocol);
+
+                                                       if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) &&
+                                                                       (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
+                                                               /* TODO: check interfaceClass! */
+                                                               DPRINTF("found device with libusb\n");
+
+                                                               xpcu = malloc(sizeof(struct xpcu_s));
+                                                               if (!xpcu)
+                                                                       return -ENOMEM;
+
+                                                               bzero(xpcu, sizeof(struct xpcu_s));
+                                                               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;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       e->handle = (unsigned long)xpcu;
+
+       return 0;
+}
+
+int xpcu_found(struct event *e) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)e->handle;
+
+       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->matchTables[0].VendorId = xpcu->dev->descriptor.idVendor;
+               e->matchTables[0].ProductId = xpcu->dev->descriptor.idProduct;
+               e->matchTables[0].bDeviceClass = xpcu->dev->descriptor.bDeviceClass;
+               e->matchTables[0].bDeviceSubClass = xpcu->dev->descriptor.bDeviceSubClass;
+               e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass;
+               e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass;
+               e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol;
+       }
+
+       return 0;
+}
+
+int xpcu_close(struct event *e) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)e->handle;
+
+       if (!xpcu)
+               return -ENODEV;
+
+       if(xpcu) {
+               if (xpcu->handle) {
+                       xpcu_claim(xpcu, XPCU_RELEASE);
+                       usb_close(xpcu->handle);
+               }
+
+               busses = NULL;
+               free(xpcu);
+       }
+
+       return 0;
+}
+
+int xpcu_int_state(struct interrupt *it, int enable) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)it->hInterrupt;
+       pthread_mutex_t *interrupt = &dummy_interrupt;
+
+       if (xpcu)
+               interrupt = &xpcu->interrupt;
+       
+       if (enable == ENABLE_INTERRUPT) {
+               it->fEnableOk = 1;
+               it->fStopped = 0;
+               pthread_mutex_trylock(interrupt);
+       } else {
+               it->dwCounter = 0;
+               it->fStopped = 1;
+               if (pthread_mutex_trylock(interrupt) == EBUSY)
+                       pthread_mutex_unlock(interrupt);
+       }
+
+       return 0;
+}
+
+int xpcu_int_wait(struct interrupt *it) {
+       struct xpcu_s *xpcu = (struct xpcu_s*)it->hInterrupt;
+
+       if (it->hInterrupt != (unsigned long)xpcu)
+               return -ENODEV;
+       
+       if (xpcu) {
+               if (it->dwCounter == 0) {
+                       it->dwCounter = 1;
+               } else {
+                       pthread_mutex_lock(&xpcu->interrupt);
+                       pthread_mutex_unlock(&xpcu->interrupt);
+               }
+       } else {
+               pthread_mutex_lock(&dummy_interrupt);
+               pthread_mutex_unlock(&dummy_interrupt);
+       }
 
-       return &xpcu;
+       return 0;
 }
Impressum, Datenschutz