mirror of
https://github.com/GOSTSec/sgminer
synced 2025-01-23 04:54:26 +00:00
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.
This commit is contained in:
parent
beb5f8889d
commit
e186c5cf07
95
usbutils.c
95
usbutils.c
@ -1319,32 +1319,23 @@ static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
|
|||||||
return NULL;
|
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)
|
void usb_uninit(struct cgpu_info *cgpu)
|
||||||
{
|
{
|
||||||
applog(LOG_DEBUG, "USB uninit %s%i",
|
applog(LOG_DEBUG, "USB uninit %s%i",
|
||||||
cgpu->drv->name, cgpu->device_id);
|
cgpu->drv->name, cgpu->device_id);
|
||||||
|
|
||||||
mutex_lock(&cgpu->usbinfo.lock);
|
// May have happened already during a failed initialisation
|
||||||
__usb_uninit(cgpu);
|
// if release_cgpu() was called due to a USB NODEV(err)
|
||||||
mutex_unlock(&cgpu->usbinfo.lock);
|
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;
|
struct cgpu_info *lookcgpu;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -1355,7 +1346,6 @@ static void __release_cgpu(struct cgpu_info *cgpu)
|
|||||||
if (cgpu->usbinfo.nodev)
|
if (cgpu->usbinfo.nodev)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
cgusb = cgpu->usbdev;
|
|
||||||
total_count--;
|
total_count--;
|
||||||
drv_count[cgpu->drv->drv_id].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",
|
usb_uninit(cgpu);
|
||||||
cgpu->drv->name, cgpu->device_id);
|
|
||||||
__usb_uninit(cgpu);
|
|
||||||
|
|
||||||
cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
|
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_FAIL 0
|
||||||
#define USB_INIT_OK 1
|
#define USB_INIT_OK 1
|
||||||
#define USB_INIT_IGNORE 2
|
#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);
|
cgpu->drv->name = (char *)(found->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_init(&cgpu->usbinfo.lock);
|
|
||||||
|
|
||||||
return USB_INIT_OK;
|
return USB_INIT_OK;
|
||||||
|
|
||||||
cldame:
|
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)
|
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;
|
bool ftdi;
|
||||||
#if DO_USB_STATS
|
#if DO_USB_STATS
|
||||||
struct timeval tv_start;
|
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;
|
unsigned char usbbuf[USB_MAX_READ+4], *ptr;
|
||||||
size_t usbbufread;
|
size_t usbbufread;
|
||||||
|
|
||||||
mutex_lock(&cgpu->usbinfo.lock);
|
|
||||||
if (cgpu->usbinfo.nodev) {
|
if (cgpu->usbinfo.nodev) {
|
||||||
*buf = '\0';
|
*buf = '\0';
|
||||||
*processed = 0;
|
*processed = 0;
|
||||||
USB_REJECT(cgpu, MODE_BULK_READ);
|
USB_REJECT(cgpu, MODE_BULK_READ);
|
||||||
|
|
||||||
err = LIBUSB_ERROR_NO_DEVICE;
|
return LIBUSB_ERROR_NO_DEVICE;
|
||||||
goto out_unlock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
usbdev = cgpu->usbdev;
|
|
||||||
ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
|
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));
|
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);
|
memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
|
||||||
|
|
||||||
if (NODEV(err))
|
if (NODEV(err))
|
||||||
__release_cgpu(cgpu);
|
release_cgpu(cgpu);
|
||||||
|
|
||||||
goto out_unlock;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usbdev->buffer && usbdev->bufamt) {
|
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);
|
memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
|
||||||
|
|
||||||
if (NODEV(err))
|
if (NODEV(err))
|
||||||
__release_cgpu(cgpu);
|
release_cgpu(cgpu);
|
||||||
|
|
||||||
out_unlock:
|
|
||||||
mutex_unlock(&cgpu->usbinfo.lock);
|
|
||||||
|
|
||||||
return err;
|
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)
|
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
|
#if DO_USB_STATS
|
||||||
struct timeval tv_start;
|
struct timeval tv_start;
|
||||||
#endif
|
#endif
|
||||||
@ -2345,16 +2318,12 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
|
|||||||
|
|
||||||
*processed = 0;
|
*processed = 0;
|
||||||
|
|
||||||
mutex_lock(&cgpu->usbinfo.lock);
|
|
||||||
if (cgpu->usbinfo.nodev) {
|
if (cgpu->usbinfo.nodev) {
|
||||||
USB_REJECT(cgpu, MODE_BULK_WRITE);
|
USB_REJECT(cgpu, MODE_BULK_WRITE);
|
||||||
|
|
||||||
err = LIBUSB_ERROR_NO_DEVICE;
|
return LIBUSB_ERROR_NO_DEVICE;
|
||||||
goto out_unlock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
usbdev = cgpu->usbdev;
|
|
||||||
|
|
||||||
if (timeout == DEVTIMEOUT)
|
if (timeout == DEVTIMEOUT)
|
||||||
timeout = usbdev->found->timeout;
|
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;
|
*processed = tot;
|
||||||
|
|
||||||
if (NODEV(err))
|
if (NODEV(err))
|
||||||
__release_cgpu(cgpu);
|
release_cgpu(cgpu);
|
||||||
|
|
||||||
out_unlock:
|
|
||||||
mutex_unlock(&cgpu->usbinfo.lock);
|
|
||||||
|
|
||||||
return err;
|
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)
|
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
|
#if DO_USB_STATS
|
||||||
struct timeval tv_start, tv_finish;
|
struct timeval tv_start, tv_finish;
|
||||||
#endif
|
#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));
|
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) {
|
if (cgpu->usbinfo.nodev) {
|
||||||
USB_REJECT(cgpu, MODE_CTRL_WRITE);
|
USB_REJECT(cgpu, MODE_CTRL_WRITE);
|
||||||
|
|
||||||
err = LIBUSB_ERROR_NO_DEVICE;
|
return LIBUSB_ERROR_NO_DEVICE;
|
||||||
goto out_unlock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
USBDEBUG("USB debug: @_usb_transfer() data=%s", bin2hex((unsigned char *)data, (size_t)siz));
|
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));
|
USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex((unsigned char *)buf, (size_t)siz));
|
||||||
|
|
||||||
usbdev = cgpu->usbdev;
|
|
||||||
|
|
||||||
STATS_TIMEVAL(&tv_start);
|
STATS_TIMEVAL(&tv_start);
|
||||||
err = libusb_control_transfer(usbdev->handle, request_type,
|
err = libusb_control_transfer(usbdev->handle, request_type,
|
||||||
bRequest, wValue, wIndex, (unsigned char *)buf, (uint16_t)siz,
|
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);
|
free(buf);
|
||||||
|
|
||||||
if (NODEV(err))
|
if (NODEV(err))
|
||||||
__release_cgpu(cgpu);
|
release_cgpu(cgpu);
|
||||||
out_unlock:
|
|
||||||
mutex_unlock(&cgpu->usbinfo.lock);
|
|
||||||
|
|
||||||
return err;
|
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)
|
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
|
#if DO_USB_STATS
|
||||||
struct timeval tv_start, tv_finish;
|
struct timeval tv_start, tv_finish;
|
||||||
#endif
|
#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));
|
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) {
|
if (cgpu->usbinfo.nodev) {
|
||||||
USB_REJECT(cgpu, MODE_CTRL_READ);
|
USB_REJECT(cgpu, MODE_CTRL_READ);
|
||||||
|
|
||||||
err = LIBUSB_ERROR_NO_DEVICE;
|
return LIBUSB_ERROR_NO_DEVICE;
|
||||||
goto out_unlock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*amount = 0;
|
*amount = 0;
|
||||||
|
|
||||||
usbdev = cgpu->usbdev;
|
|
||||||
|
|
||||||
STATS_TIMEVAL(&tv_start);
|
STATS_TIMEVAL(&tv_start);
|
||||||
err = libusb_control_transfer(usbdev->handle, request_type,
|
err = libusb_control_transfer(usbdev->handle, request_type,
|
||||||
bRequest, wValue, wIndex,
|
bRequest, wValue, wIndex,
|
||||||
@ -2504,10 +2460,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
|
|||||||
*amount = err;
|
*amount = err;
|
||||||
err = 0;
|
err = 0;
|
||||||
} else if (NODEV(err))
|
} else if (NODEV(err))
|
||||||
__release_cgpu(cgpu);
|
release_cgpu(cgpu);
|
||||||
|
|
||||||
out_unlock:
|
|
||||||
mutex_unlock(&cgpu->usbinfo.lock);
|
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
@ -156,6 +156,7 @@ enum usb_types {
|
|||||||
struct cg_usb_device {
|
struct cg_usb_device {
|
||||||
struct usb_find_devices *found;
|
struct usb_find_devices *found;
|
||||||
libusb_device_handle *handle;
|
libusb_device_handle *handle;
|
||||||
|
pthread_mutex_t *mutex;
|
||||||
struct libusb_device_descriptor *descriptor;
|
struct libusb_device_descriptor *descriptor;
|
||||||
enum usb_types usb_type;
|
enum usb_types usb_type;
|
||||||
enum sub_ident ident;
|
enum sub_ident ident;
|
||||||
@ -172,7 +173,6 @@ struct cg_usb_device {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct cg_usb_info {
|
struct cg_usb_info {
|
||||||
pthread_mutex_t lock;
|
|
||||||
uint8_t bus_number;
|
uint8_t bus_number;
|
||||||
uint8_t device_address;
|
uint8_t device_address;
|
||||||
int usbstat;
|
int usbstat;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user