From: Michael Gernoth Date: Fri, 11 Apr 2008 22:30:12 +0000 (+0200) Subject: move interrupt functions to xpcu.c X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/usb-driver/commitdiff_plain/501f1c2134564edacc35526b86968703d5485bcd?ds=inline move interrupt functions to xpcu.c --- diff --git a/usb-driver.c b/usb-driver.c index 2f61e2b..0383d12 100644 --- a/usb-driver.c +++ b/usb-driver.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -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 f92173a..9cea2c1 100644 --- a/xpcu.c +++ b/xpcu.c @@ -5,10 +5,12 @@ #include #include #include +#include #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 c189d82..cde1421 100644 --- 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);