Protect all usb writes reads and unplugs with a mutex lock and check libusb release has succeeded before proceeding with a libusb close.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
diff --git a/usbutils.c b/usbutils.c
index 323bd7c..1478b6e 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1319,23 +1319,32 @@ static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
return NULL;
}
-void usb_uninit(struct cgpu_info *cgpu)
+/* Called with cgpu->usbinfo.lock held */
+static void __usb_uninit(struct cgpu_info *cgpu)
{
- applog(LOG_DEBUG, "USB uninit %s%i",
- cgpu->drv->name, cgpu->device_id);
-
// May have happened already during a failed initialisation
- // if release_cgpu() was called due to a USB NODEV(err)
+ // 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);
+ if (!libusb_release_interface(cgpu->usbdev->handle, cgpu->usbdev->found->interface))
+ libusb_close(cgpu->usbdev->handle);
cgpu->usbdev = free_cgusb(cgpu->usbdev);
}
-static void release_cgpu(struct cgpu_info *cgpu)
+void usb_uninit(struct cgpu_info *cgpu)
{
- struct cg_usb_device *cgusb = cgpu->usbdev;
+ 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);
+}
+
+/* Called with cgpu->usbinfo.lock held */
+static void __release_cgpu(struct cgpu_info *cgpu)
+{
+ struct cg_usb_device *cgusb;
struct cgpu_info *lookcgpu;
int i;
@@ -1346,6 +1355,7 @@ 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--;
@@ -1369,11 +1379,20 @@ static void release_cgpu(struct cgpu_info *cgpu)
}
}
- usb_uninit(cgpu);
+ applog(LOG_DEBUG, "USB uninit %s%i",
+ cgpu->drv->name, cgpu->device_id);
+ __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
@@ -1621,6 +1640,8 @@ 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:
@@ -2061,7 +2082,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 = cgpu->usbdev;
+ struct cg_usb_device *usbdev;
bool ftdi;
#if DO_USB_STATS
struct timeval tv_start;
@@ -2078,14 +2099,17 @@ 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);
- return LIBUSB_ERROR_NO_DEVICE;
+ err = LIBUSB_ERROR_NO_DEVICE;
+ goto out_unlock;
}
+ 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));
@@ -2182,9 +2206,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);
- return err;
+ goto out_unlock;
}
if (usbdev->buffer && usbdev->bufamt) {
@@ -2297,14 +2321,17 @@ 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);
+
+out_unlock:
+ mutex_unlock(&cgpu->usbinfo.lock);
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 = cgpu->usbdev;
+ struct cg_usb_device *usbdev;
#if DO_USB_STATS
struct timeval tv_start;
#endif
@@ -2318,12 +2345,16 @@ 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);
- return LIBUSB_ERROR_NO_DEVICE;
+ err = LIBUSB_ERROR_NO_DEVICE;
+ goto out_unlock;
}
+ usbdev = cgpu->usbdev;
+
if (timeout == DEVTIMEOUT)
timeout = usbdev->found->timeout;
@@ -2369,14 +2400,17 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
*processed = tot;
if (NODEV(err))
- release_cgpu(cgpu);
+ __release_cgpu(cgpu);
+
+out_unlock:
+ mutex_unlock(&cgpu->usbinfo.lock);
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 = cgpu->usbdev;
+ struct cg_usb_device *usbdev;
#if DO_USB_STATS
struct timeval tv_start, tv_finish;
#endif
@@ -2385,10 +2419,12 @@ 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);
- return LIBUSB_ERROR_NO_DEVICE;
+ err = LIBUSB_ERROR_NO_DEVICE;
+ goto out_unlock;
}
USBDEBUG("USB debug: @_usb_transfer() data=%s", bin2hex((unsigned char *)data, (size_t)siz));
@@ -2406,6 +2442,8 @@ 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,
@@ -2421,14 +2459,16 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
free(buf);
if (NODEV(err))
- release_cgpu(cgpu);
+ __release_cgpu(cgpu);
+out_unlock:
+ mutex_unlock(&cgpu->usbinfo.lock);
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 = cgpu->usbdev;
+ struct cg_usb_device *usbdev;
#if DO_USB_STATS
struct timeval tv_start, tv_finish;
#endif
@@ -2436,14 +2476,18 @@ 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);
- return LIBUSB_ERROR_NO_DEVICE;
+ err = LIBUSB_ERROR_NO_DEVICE;
+ goto out_unlock;
}
*amount = 0;
+ usbdev = cgpu->usbdev;
+
STATS_TIMEVAL(&tv_start);
err = libusb_control_transfer(usbdev->handle, request_type,
bRequest, wValue, wIndex,
@@ -2460,7 +2504,10 @@ 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);
+ __release_cgpu(cgpu);
+
+out_unlock:
+ mutex_unlock(&cgpu->usbinfo.lock);
return err;
}
diff --git a/usbutils.h b/usbutils.h
index 8cc8922..e2f3a82 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -156,7 +156,6 @@ 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;
@@ -173,6 +172,7 @@ struct cg_usb_device {
};
struct cg_usb_info {
+ pthread_mutex_t lock;
uint8_t bus_number;
uint8_t device_address;
int usbstat;