]> git.zerfleddert.de Git - usb-driver/commitdiff
move interrupt functions to xpcu.c
authorMichael Gernoth <michael@gernoth.net>
Fri, 11 Apr 2008 22:30:12 +0000 (00:30 +0200)
committerMichael Gernoth <michael@gernoth.net>
Fri, 11 Apr 2008 22:30:12 +0000 (00:30 +0200)
usb-driver.c
xpcu.c
xpcu.h

index 2f61e2bb3bc6f1068ef087e4798996d2c7b45ea5..0383d123d034d62188bc63fc6c09b86ca1a149f3 100644 (file)
@@ -35,7 +35,6 @@
 #include <stdio.h>
 #include <usb.h>
 #include <signal.h>
-#include <pthread.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <sys/ioctl.h>
@@ -55,8 +54,6 @@ static FILE *modulesfp = NULL;
 static FILE *baseaddrfp = NULL;
 static int baseaddrnum = 0;
 static int modules_read = 0;
-static int ints_enabled = 0;
-static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER;
 
 #define NO_WINDRVR 1
 
@@ -100,13 +97,13 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct card_register* cr = (struct card_register*)(wdheader->data);
 
-                               DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
+                               DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
                                cr->Card.dwItems,
                                (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
                                cr->Card.Item[0].I.IO.dwBytes,
                                cr->Card.Item[0].I.IO.dwBar);
                                
-                               DPRINTF("Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
+                               DPRINTF("-> Items: %lu, Addr: 0x%lx, bytes: %lu, bar: %lu\n",
                                cr->Card.dwItems,
                                (unsigned long)cr->Card.Item[1].I.IO.dwAddr,
                                cr->Card.Item[1].I.IO.dwBytes,
@@ -132,21 +129,21 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                        cr->hCard = 0;
                                }
 #endif
-                               DPRINTF("hCard: %lu\n", cr->hCard);
+                               DPRINTF("<-hCard: %lu\n", cr->hCard);
                        }
                        break;
 
                case USB_TRANSFER:
-                       DPRINTF("in USB_TRANSFER");
+                       DPRINTF("USB_TRANSFER\n");
                        {
                                struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data);
 
 #ifdef DEBUG
-                               DPRINTF(" unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
+                               DPRINTF("-> unique: 0x%lx, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n",
                                ut->dwUniqueID, ut->dwPipeNum, ut->fRead,
                                ut->dwOptions, ut->dwBufferSize, ut->dwTimeout);
                                if (ut->dwPipeNum == 0) {
-                                       DPRINTF("setup packet: ");
+                                       DPRINTF("-> setup packet: ");
                                        hexdump(ut->SetupPacket, 8);
                                }
 
@@ -166,7 +163,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                DPRINTF("Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write"));
                                if (ut->fRead && ut->dwBytesTransferred)
                                {
-                                       DPRINTF("Read: ");
+                                       DPRINTF("<- Read: ");
                                        hexdump(ut->pBuffer, ut->dwBytesTransferred);
                                }
 #endif
@@ -179,15 +176,21 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct interrupt *it = (struct interrupt*)(wdheader->data);
 
-                               DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
+                               DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
                                it->hInterrupt, it->dwOptions,
                                it->dwCmds, it->fEnableOk, it->dwCounter,
                                it->dwLost, it->fStopped);
 
-                               it->fEnableOk = 1;
-                               it->fStopped = 0;
-                               ints_enabled = 1;
-                               pthread_mutex_trylock(&int_wait);
+#ifndef NO_WINDRVR
+                               ret = (*ioctl_func) (fd, request, wdioctl);
+#else
+                               xpcu_int_state(xpcu, it, ENABLE_INTERRUPT);
+#endif
+
+                               DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
+                               it->hInterrupt, it->dwOptions,
+                               it->dwCmds, it->fEnableOk, it->dwCounter,
+                               it->dwLost, it->fStopped);
                        }
 
                        break;
@@ -197,20 +200,16 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct interrupt *it = (struct interrupt*)(wdheader->data);
 
-                               DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
+                               DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
                                it->hInterrupt, it->dwOptions,
                                it->dwCmds, it->fEnableOk, it->dwCounter,
                                it->dwLost, it->fStopped);
 #ifndef NO_WINDRVR
                                ret = (*ioctl_func) (fd, request, wdioctl);
 #else
-                               it->dwCounter = 0;
-                               it->fStopped = 1;
-                               ints_enabled = 0;
-                               if (pthread_mutex_trylock(&int_wait) == EBUSY)
-                                       pthread_mutex_unlock(&int_wait);
+                               xpcu_int_state(xpcu, it, DISABLE_INTERRUPT);
 #endif
-                               DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
+                               DPRINTF("<- Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
                                it->hInterrupt, it->dwOptions,
                                it->dwCmds, it->fEnableOk, it->dwCounter,
                                it->dwLost, it->fStopped);
@@ -222,7 +221,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data);
 
-                               DPRINTF("unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
+                               DPRINTF("-> unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
                                usi->dwUniqueID, usi->dwInterfaceNum,
                                usi->dwAlternateSetting, usi->dwOptions);
 #ifndef NO_WINDRVR
@@ -230,7 +229,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
 #else
                                xpcu_set_interface(xpcu, usi);
 #endif
-                               DPRINTF("unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
+                               DPRINTF("<- unique: 0x%lx, interfacenum: %lu, alternatesetting: %lu, options: %lx\n",
                                usi->dwUniqueID, usi->dwInterfaceNum,
                                usi->dwAlternateSetting, usi->dwOptions);
                        }
@@ -242,7 +241,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data);
 
-                               DPRINTF("unique: 0x%lx, bytes: %lu, options: %lx\n",
+                               DPRINTF("-> unique: 0x%lx, bytes: %lu, options: %lx\n",
                                ugdd->dwUniqueID, ugdd->dwBytes,
                                ugdd->dwOptions);
 
@@ -260,7 +259,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                int i;
 #endif
 
-                               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",
+                               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",
                                e->handle, e->dwAction,
                                e->dwStatus, e->dwEventId, e->dwCardType,
                                e->hKernelPlugIn, e->dwOptions,
@@ -269,15 +268,14 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                e->u.Usb.dwUniqueID, e->dwEventVer,
                                e->dwNumMatchTables);
 
-                               xpcu = xpcu_find(e);
 #ifndef NO_WINDRVR
                                ret = (*ioctl_func) (fd, request, wdioctl);
 #else
-                               e->handle = (unsigned long)xpcu;
+                               xpcu = xpcu_find(e);
 #endif
 
 #ifdef DEBUG
-                               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",
+                               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",
                                e->handle, e->dwAction,
                                e->dwStatus, e->dwEventId, e->dwCardType,
                                e->hKernelPlugIn, e->dwOptions,
@@ -345,7 +343,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct interrupt *it = (struct interrupt*)(wdheader->data);
 
-                               DPRINTF("Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
+                               DPRINTF("-> Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
                                it->hInterrupt, it->dwOptions,
                                it->dwCmds, it->fEnableOk, it->dwCounter,
                                it->dwLost, it->fStopped);
@@ -353,20 +351,10 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
 #ifndef NO_WINDRVR
                                ret = (*ioctl_func) (fd, request, wdioctl);
 #else
-                               if (xpcu) {
-                                       if (it->dwCounter == 0) {
-                                               it->dwCounter = 1;
-                                       } else {
-                                               pthread_mutex_lock(&int_wait);
-                                               pthread_mutex_unlock(&int_wait);
-                                       }
-                               } else {
-                                       pthread_mutex_lock(&int_wait);
-                                       pthread_mutex_unlock(&int_wait);
-                               }
+                               xpcu_int_wait(xpcu, it);
 #endif
 
-                               DPRINTF("INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
+                               DPRINTF("<- INT_WAIT_RETURN: Handle: 0x%lx, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n",
                                it->hInterrupt, it->dwOptions, it->dwCmds,
                                it->fEnableOk, it->dwCounter, it->dwLost,
                                it->fStopped);
@@ -378,12 +366,12 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                        {
                                struct card_register* cr = (struct card_register*)(wdheader->data);
 
-                               DPRINTF("Addr: 0x%lx, bytes: %lu, bar: %lu\n",
+                               DPRINTF("-> Addr: 0x%lx, bytes: %lu, bar: %lu\n",
                                (unsigned long)cr->Card.Item[0].I.IO.dwAddr,
                                cr->Card.Item[0].I.IO.dwBytes,
                                cr->Card.Item[0].I.IO.dwBar);
 
-                               DPRINTF("hCard: %lu\n", cr->hCard);
+                               DPRINTF("-> hCard: %lu\n", cr->hCard);
 
 #ifndef NO_WINDRVR
                                ret = (*ioctl_func) (fd, request, wdioctl);
@@ -403,7 +391,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
 #ifdef DEBUG
                                int i;
 
-                               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",
+                               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",
                                e->handle, e->dwAction, e->dwStatus,
                                e->dwEventId, e->dwCardType, e->hKernelPlugIn,
                                e->dwOptions, e->u.Usb.deviceId.dwVendorId,
@@ -412,7 +400,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                e->dwNumMatchTables);
 
                                for (i = 0; i < e->dwNumMatchTables; i++)
-                                       DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
+                                       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,
@@ -429,7 +417,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
 #endif
 
 #ifdef DEBUG
-                               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",
+                               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",
                                e->handle, e->dwAction, e->dwStatus,
                                e->dwEventId, e->dwCardType, e->hKernelPlugIn,
                                e->dwOptions, e->u.Usb.deviceId.dwVendorId,
@@ -438,7 +426,7 @@ static int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) {
                                e->dwNumMatchTables);
 
                                for (i = 0; i < e->dwNumMatchTables; i++)
-                                       DPRINTF("match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n",
+                                       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,
diff --git a/xpcu.c b/xpcu.c
index f92173a1a6639cae4b55a23462084f7dd8c6cca0..9cea2c1b26d22868eca1b97a47217a0b71731574 100644 (file)
--- a/xpcu.c
+++ b/xpcu.c
@@ -5,10 +5,12 @@
 #include <strings.h>
 #include <usb.h>
 #include <errno.h>
+#include <pthread.h>
 #include "usb-driver.h"
 #include "xpcu.h"
 
 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 i,j,k,l;
@@ -225,7 +227,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) {
@@ -286,9 +288,6 @@ struct xpcu_s *xpcu_find(struct event *e) {
        int i;
 
        bzero(&xpcu, sizeof(xpcu));
-       xpcu.interface = -1;
-       xpcu.alternate = -1;
-
        xpcu_init();
 
        devpos = getenv("XILINX_USB_DEV");
@@ -363,8 +362,13 @@ struct xpcu_s *xpcu_find(struct event *e) {
                                                                        (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){
                                                                /* 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;
                                                        }
                                                }
                                        }
@@ -373,8 +377,10 @@ struct xpcu_s *xpcu_find(struct event *e) {
                }
        }
 
-       if (!xpcu.dev)
+       if (!xpcu.dev) {
+               e->handle = 0;
                return NULL;
+       }
 
        return &xpcu;
 }
@@ -413,3 +419,41 @@ void xpcu_close(struct xpcu_s *xpcu, struct event *e) {
                busses = NULL;
        }
 }
+
+void xpcu_int_state(struct xpcu_s *xpcu, struct interrupt *it, int enable) {
+       static pthread_mutex_t *interrupt = &dummy_interrupt;
+
+       if (it->hInterrupt != (unsigned long)xpcu)
+               return;
+       
+       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);
+       }
+}
+
+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);
+               }
+       } else {
+               pthread_mutex_lock(&dummy_interrupt);
+               pthread_mutex_unlock(&dummy_interrupt);
+       }
+}
diff --git a/xpcu.h b/xpcu.h
index c189d82f7c3fe377c393fdaa04e96914d4979a76..cde14214a2ce63e08a43dbe550fdddda0b4d740d 100644 (file)
--- a/xpcu.h
+++ b/xpcu.h
@@ -1,12 +1,16 @@
 #define XPCU_CLAIM     1
 #define XPCU_RELEASE   0
 
+#define ENABLE_INTERRUPT       1
+#define DISABLE_INTERRUPT      0
+
 struct xpcu_s {
        struct usb_device *dev;
        usb_dev_handle *handle;
        int interface;
        int alternate;
        unsigned long card_type;
+       pthread_mutex_t interrupt;
 };
 
 int __attribute__ ((visibility ("hidden"))) xpcu_deviceinfo(struct xpcu_s *xpcu, struct usb_get_device_data *ugdd);
@@ -15,3 +19,5 @@ void __attribute__ ((visibility ("hidden"))) xpcu_set_interface(struct xpcu_s *x
 struct xpcu_s __attribute__ ((visibility ("hidden"))) *xpcu_find(struct event *e);
 void __attribute__ ((visibility ("hidden"))) xpcu_found(struct xpcu_s *xpcu, struct event *e);
 void __attribute__ ((visibility ("hidden"))) xpcu_close(struct xpcu_s *xpcu, struct event *e);
+void __attribute__ ((visibility ("hidden"))) xpcu_int_state(struct xpcu_s *xpcu, struct interrupt *it, int enable);
+void __attribute__ ((visibility ("hidden"))) xpcu_int_wait(struct xpcu_s *xpcu, struct interrupt *it);
Impressum, Datenschutz