]> git.zerfleddert.de Git - usb-driver/blobdiff - usb-driver.c
fix usb_reset. sharing the cable between applications is still possible.
[usb-driver] / usb-driver.c
index 1fcfae19d172872a5de2c89e87898cfbfbdcec54..942cdc15a01c8ed59a3d86eb12b26813b1d1799e 100644 (file)
@@ -39,6 +39,8 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <sys/ioctl.h>
+#include <sys/utsname.h>
+#include <bits/wordsize.h>
 #include "usb-driver.h"
 #include "config.h"
 
@@ -47,14 +49,15 @@ static int windrvrfd = -1;
 static unsigned long ppbase = 0;
 static unsigned long ecpbase = 0;
 static struct parport_config *pport = NULL;
-FILE *modulesfp = NULL;
-FILE *baseaddrfp = NULL;
-int baseaddrnum = 0;
+static FILE *modulesfp = NULL;
+static FILE *baseaddrfp = NULL;
+static int baseaddrnum = 0;
 static int modules_read = 0;
 static struct usb_bus *busses = NULL;
 static struct usb_device *usbdevice;
 static usb_dev_handle *usb_devhandle = NULL;
 static int usbinterface = -1;
+static int usbalternate = -1;
 static unsigned long card_type;
 static int ints_enabled = 0;
 static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
@@ -72,7 +75,7 @@ void hexdump(unsigned char *buf, int len) {
        fprintf(stderr,"\n");
 }
 
-int usb_deviceinfo(unsigned char *buf) {
+static int usb_deviceinfo(unsigned char *buf) {
        int i,j,k,l;
        int len = 0;
        WDU_CONFIGURATION **pConfigs, **pActiveConfig;
@@ -232,7 +235,40 @@ int usb_deviceinfo(unsigned char *buf) {
        return len;
 }
 
-int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
+static int usb_claim(int claim) {
+       int ret = 0;
+       static int claimed = 0;
+
+       if (usbinterface < 0)
+               return -1;
+       
+       if (claim) {
+               if (claimed)
+                       return 0;
+
+               ret = usb_claim_interface(usb_devhandle, usbinterface);
+               if (!ret) {
+                       claimed = 1;
+                       ret = usb_set_altinterface(usb_devhandle, usbalternate);
+                       if (ret)
+                               fprintf(stderr, "usb_set_altinterface: %d\n", ret);
+               } else {
+                       fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
+                                       usbinterface, ret, usb_strerror());
+               }
+       } else {
+               if (!claimed)
+                       return 0;
+
+               ret = usb_release_interface(usb_devhandle, usbinterface);
+               if (!ret)
+                       claimed = 0;
+       }
+
+       return ret;
+}
+
+static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
        struct header_struct* wdheader = (struct header_struct*)wdioctl;
        struct version_struct *version;
        int ret = 0;
@@ -305,8 +341,10 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                DPRINTF(" unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
                                ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
                                ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
-                               DPRINTF("setup packet: ");
-                               hexdump(ut->SetupPacket, 8);
+                               if (ut->dwPipeNum == 0) {
+                                       DPRINTF("setup packet: ");
+                                       hexdump(ut->SetupPacket, 8);
+                               }
 
                                if (!ut->fRead && ut->dwBufferSize)
                                {
@@ -317,6 +355,7 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
 #ifndef NO_WINDRVR
                                ret = (*ioctl_func) (fd, request, wdioctl);
 #else
+                               usb_claim(1);
                                /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
                                if (ut->dwPipeNum == 0) { /* control pipe */
                                        int requesttype, request, value, index, size;
@@ -330,10 +369,10 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                } else {
                                        if (ut->fRead) {
                                                ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
-
                                        } else {
                                                ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout);
                                        }
+                                       usb_claim(0);
                                }
 
                                if (ret < 0) {
@@ -411,25 +450,18 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                ret = (*ioctl_func) (fd, request, wdioctl);
 #else
                                if (usbdevice) {
-                                       if (!usb_devhandle)
+                                       if (!usb_devhandle) {
                                                usb_devhandle = usb_open(usbdevice);
-
-                                       /* FIXME: Select right interface! */
-                                       ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber);
-                                       if (!ret) {
-                                               if(!ret) {
-                                                       usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
-                                                       ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting);
-                                                       if (ret)
-                                                               fprintf(stderr, "usb_set_altinterface: %d\n", ret);
-                                               } else {
-                                                       fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror());
+#ifndef NO_USB_RESET
+                                               if (usb_devhandle) {
+                                                       usb_reset(usb_devhandle);
+                                                       usb_devhandle = usb_open(usbdevice);
                                                }
-                                       } else {
-                                               fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n",
-                                               usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber,
-                                               ret, usb_strerror());
+#endif
                                        }
+
+                                       usbinterface = usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber;
+                                       usbalternate = usi->dwAlternateSetting;
                                }
 #endif
                                DPRINTF("unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
@@ -466,9 +498,8 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct event *e = (struct event*)(wdheader->data);
                                struct usb_bus *bus;
-                               char* device_num;
-                               char* remainder;
-                               int devnum = -1;
+                               char* devpos;
+                               int busnum = -1, devnum = -1;
                                int i;
 
                                DPRINTF("handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n",
@@ -480,14 +511,36 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                e->u.Usb.dwUniqueID, e->dwEventVer,
                                e->dwNumMatchTables);
 
-                               device_num=getenv("XILINX_USB_DEVNUM");
-                               if (device_num!=NULL) {
-                                       DPRINTF("XILINX_USB_BUS=%s\n",device_num);
-                                       devnum=strtol(device_num,&remainder,10);
-                                       if (device_num==remainder) /* no integer in env variable */
-                                               devnum=-1;
-                               }
+                               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++) {
 
@@ -503,6 +556,9 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                        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);
 
@@ -510,7 +566,7 @@ int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                                           (desc->idProduct == e->matchTables[i].ProductId) &&
                                                           (desc->bDeviceClass == e->matchTables[i].bDeviceClass) &&
                                                           (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass) &&
-                                                          ((devnum == -1) || (dev->devnum == devnum)) ) {
+                                                          ((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;
@@ -801,8 +857,6 @@ int close(int fd) {
        
        if (fd == windrvrfd && windrvrfd >= 0) {
                DPRINTF("close windrvrfd\n");
-               if (usbinterface >= 0)
-                       usb_release_interface(usb_devhandle, usbinterface);
 
                if (usb_devhandle)
                        usb_close(usb_devhandle);
@@ -913,3 +967,21 @@ int access(const char *pathname, int mode) {
                return (*func)(pathname, mode);
        }
 }
+
+#if __WORDSIZE == 32
+int uname (struct utsname *__name) {
+       static int (*func) (struct utsname*);
+       int ret;
+
+       if (!func)
+               func = (int (*) (struct utsname*)) dlsym(RTLD_NEXT, "uname");
+       
+       ret = (*func)(__name);
+
+       if (ret == 0 && (!strcmp(__name->machine, "x86_64"))) {
+               strcpy(__name->machine, "i686");
+       }
+       
+       return ret;
+}
+#endif
Impressum, Datenschutz