From e186c5cf0753ae3418bd79ed386774858b233063 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 7 Jun 2013 00:38:10 +1000 Subject: [PATCH] Revert "Protect all usb writes reads and unplugs with a mutex lock and check libusb release has succeeded before proceeding with a libusb close." This reverts commit beb5f8889de4f5491c831a4bd8824b9c514dbafc. --- usbutils.c | 95 ++++++++++++++---------------------------------------- usbutils.h | 2 +- 2 files changed, 25 insertions(+), 72 deletions(-) diff --git a/usbutils.c b/usbutils.c index 1478b6ed..323bd7c3 100644 --- a/usbutils.c +++ b/usbutils.c @@ -1319,32 +1319,23 @@ static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb) return NULL; } -/* Called with cgpu->usbinfo.lock held */ -static void __usb_uninit(struct cgpu_info *cgpu) -{ - // May have happened already during a failed initialisation - // if __release_cgpu() was called due to a USB NODEV(err) - if (!cgpu->usbdev) - return; - if (!libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface)) - libusb_close(cgpu->usbdev->handle); - cgpu->usbdev = free_cgusb(cgpu->usbdev); -} - void usb_uninit(struct cgpu_info *cgpu) { applog(LOG_DEBUG, "USB uninit %s%i", cgpu->drv->name, cgpu->device_id); - mutex_lock(&cgpu->usbinfo.lock); - __usb_uninit(cgpu); - mutex_unlock(&cgpu->usbinfo.lock); + // May have happened already during a failed initialisation + // if release_cgpu() was called due to a USB NODEV(err) + if (!cgpu->usbdev) + return; + libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface); + libusb_close(cgpu->usbdev->handle); + cgpu->usbdev = free_cgusb(cgpu->usbdev); } -/* Called with cgpu->usbinfo.lock held */ -static void __release_cgpu(struct cgpu_info *cgpu) +static void release_cgpu(struct cgpu_info *cgpu) { - struct cg_usb_device *cgusb; + struct cg_usb_device *cgusb = cgpu->usbdev; struct cgpu_info *lookcgpu; int i; @@ -1355,7 +1346,6 @@ static void __release_cgpu(struct cgpu_info *cgpu) if (cgpu->usbinfo.nodev) return; - cgusb = cgpu->usbdev; total_count--; drv_count[cgpu->drv->drv_id].count--; @@ -1379,20 +1369,11 @@ static void __release_cgpu(struct cgpu_info *cgpu) } } - applog(LOG_DEBUG, "USB uninit %s%i", - cgpu->drv->name, cgpu->device_id); - __usb_uninit(cgpu); + usb_uninit(cgpu); cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address); } -static void release_cgpu(struct cgpu_info *cgpu) -{ - mutex_lock(&cgpu->usbinfo.lock); - __release_cgpu(cgpu); - mutex_unlock(&cgpu->usbinfo.lock); -} - #define USB_INIT_FAIL 0 #define USB_INIT_OK 1 #define USB_INIT_IGNORE 2 @@ -1640,8 +1621,6 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u cgpu->drv->name = (char *)(found->name); } - mutex_init(&cgpu->usbinfo.lock); - return USB_INIT_OK; cldame: @@ -2082,7 +2061,7 @@ static char *find_end(unsigned char *buf, unsigned char *ptr, int ptrlen, int to int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, __maybe_unused enum usb_cmds cmd, bool readonce) { - struct cg_usb_device *usbdev; + struct cg_usb_device *usbdev = cgpu->usbdev; bool ftdi; #if DO_USB_STATS struct timeval tv_start; @@ -2099,17 +2078,14 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro unsigned char usbbuf[USB_MAX_READ+4], *ptr; size_t usbbufread; - mutex_lock(&cgpu->usbinfo.lock); if (cgpu->usbinfo.nodev) { *buf = '\0'; *processed = 0; USB_REJECT(cgpu, MODE_BULK_READ); - err = LIBUSB_ERROR_NO_DEVICE; - goto out_unlock; + return LIBUSB_ERROR_NO_DEVICE; } - usbdev = cgpu->usbdev; ftdi = (usbdev->usb_type == USB_TYPE_FTDI); USBDEBUG("USB debug: _usb_read(%s (nodev=%s),ep=%d,buf=%p,bufsiz=%zu,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, buf, bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce)); @@ -2206,9 +2182,9 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz); if (NODEV(err)) - __release_cgpu(cgpu); + release_cgpu(cgpu); - goto out_unlock; + return err; } if (usbdev->buffer && usbdev->bufamt) { @@ -2321,17 +2297,14 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz); if (NODEV(err)) - __release_cgpu(cgpu); - -out_unlock: - mutex_unlock(&cgpu->usbinfo.lock); + release_cgpu(cgpu); return err; } int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, __maybe_unused enum usb_cmds cmd) { - struct cg_usb_device *usbdev; + struct cg_usb_device *usbdev = cgpu->usbdev; #if DO_USB_STATS struct timeval tv_start; #endif @@ -2345,16 +2318,12 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr *processed = 0; - mutex_lock(&cgpu->usbinfo.lock); if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_BULK_WRITE); - err = LIBUSB_ERROR_NO_DEVICE; - goto out_unlock; + return LIBUSB_ERROR_NO_DEVICE; } - usbdev = cgpu->usbdev; - if (timeout == DEVTIMEOUT) timeout = usbdev->found->timeout; @@ -2400,17 +2369,14 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr *processed = tot; if (NODEV(err)) - __release_cgpu(cgpu); - -out_unlock: - mutex_unlock(&cgpu->usbinfo.lock); + release_cgpu(cgpu); return err; } int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, __maybe_unused enum usb_cmds cmd) { - struct cg_usb_device *usbdev; + struct cg_usb_device *usbdev = cgpu->usbdev; #if DO_USB_STATS struct timeval tv_start, tv_finish; #endif @@ -2419,12 +2385,10 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",siz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, siz, timeout, usb_cmdname(cmd)); - mutex_lock(&cgpu->usbinfo.lock); if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_CTRL_WRITE); - err = LIBUSB_ERROR_NO_DEVICE; - goto out_unlock; + return LIBUSB_ERROR_NO_DEVICE; } USBDEBUG("USB debug: @_usb_transfer() data=%s", bin2hex((unsigned char *)data, (size_t)siz)); @@ -2442,8 +2406,6 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex((unsigned char *)buf, (size_t)siz)); - usbdev = cgpu->usbdev; - STATS_TIMEVAL(&tv_start); err = libusb_control_transfer(usbdev->handle, request_type, bRequest, wValue, wIndex, (unsigned char *)buf, (uint16_t)siz, @@ -2459,16 +2421,14 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest free(buf); if (NODEV(err)) - __release_cgpu(cgpu); -out_unlock: - mutex_unlock(&cgpu->usbinfo.lock); + release_cgpu(cgpu); return err; } int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, __maybe_unused enum usb_cmds cmd) { - struct cg_usb_device *usbdev; + struct cg_usb_device *usbdev = cgpu->usbdev; #if DO_USB_STATS struct timeval tv_start, tv_finish; #endif @@ -2476,18 +2436,14 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd)); - mutex_lock(&cgpu->usbinfo.lock); if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_CTRL_READ); - err = LIBUSB_ERROR_NO_DEVICE; - goto out_unlock; + return LIBUSB_ERROR_NO_DEVICE; } *amount = 0; - usbdev = cgpu->usbdev; - STATS_TIMEVAL(&tv_start); err = libusb_control_transfer(usbdev->handle, request_type, bRequest, wValue, wIndex, @@ -2504,10 +2460,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe *amount = err; err = 0; } else if (NODEV(err)) - __release_cgpu(cgpu); - -out_unlock: - mutex_unlock(&cgpu->usbinfo.lock); + release_cgpu(cgpu); return err; } diff --git a/usbutils.h b/usbutils.h index e2f3a82d..8cc8922a 100644 --- a/usbutils.h +++ b/usbutils.h @@ -156,6 +156,7 @@ enum usb_types { struct cg_usb_device { struct usb_find_devices *found; libusb_device_handle *handle; + pthread_mutex_t *mutex; struct libusb_device_descriptor *descriptor; enum usb_types usb_type; enum sub_ident ident; @@ -172,7 +173,6 @@ struct cg_usb_device { }; struct cg_usb_info { - pthread_mutex_t lock; uint8_t bus_number; uint8_t device_address; int usbstat;