From 9ecef180d13440d9fcc99f63db3795654aaebc68 Mon Sep 17 00:00:00 2001 From: Kano Date: Wed, 26 Jun 2013 12:53:05 +1000 Subject: [PATCH] usbutils/icarus include more locking to usbdev access --- driver-icarus.c | 66 ++++++++++++++++++++++++++----------------------- usbutils.c | 47 +++++++++++++++++++++++++++++++++++ usbutils.h | 3 +++ 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/driver-icarus.c b/driver-icarus.c index 5c86ee71..b6e28172 100644 --- a/driver-icarus.c +++ b/driver-icarus.c @@ -251,6 +251,8 @@ static void _transfer(struct cgpu_info *icarus, uint8_t request_type, uint8_t bR static void icarus_initialise(struct cgpu_info *icarus, int baud) { uint16_t wValue, wIndex; + enum sub_ident ident; + int interface; if (icarus->usbinfo.nodev) return; @@ -258,14 +260,19 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) usb_set_cps(icarus, baud / 10); usb_enable_cps(icarus); - switch (icarus->usbdev->ident) { + interface = usb_interface(icarus); + ident = usb_ident(icarus); + + switch (ident) { case IDENT_BLT: case IDENT_LLT: case IDENT_CMR1: case IDENT_CMR2: + usb_set_pps(icarus, BLT_PREF_PACKET); + // Reset transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET, - icarus->usbdev->found->interface, C_RESET); + interface, C_RESET); if (icarus->usbinfo.nodev) return; @@ -278,7 +285,7 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) // Set data control transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT, - icarus->usbdev->found->interface, C_SETDATA); + interface, C_SETDATA); if (icarus->usbinfo.nodev) return; @@ -287,8 +294,7 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) wValue = FTDI_VALUE_BAUD_BLT; wIndex = FTDI_INDEX_BAUD_BLT; - if (icarus->usbdev->ident == IDENT_CMR1 || - icarus->usbdev->ident == IDENT_CMR2) { + if (ident == IDENT_CMR1 || ident == IDENT_CMR2) { switch (baud) { case 115200: wValue = FTDI_VALUE_BAUD_CMR_115; @@ -306,43 +312,42 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) // Set the baud transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue, - (wIndex & 0xff00) | icarus->usbdev->found->interface, - C_SETBAUD); + (wIndex & 0xff00) | interface, C_SETBAUD); if (icarus->usbinfo.nodev) return; // Set Modem Control transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM, - icarus->usbdev->found->interface, C_SETMODEM); + interface, C_SETMODEM); if (icarus->usbinfo.nodev) return; // Set Flow Control transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW, - icarus->usbdev->found->interface, C_SETFLOW); + interface, C_SETFLOW); if (icarus->usbinfo.nodev) return; // Clear any sent data transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX, - icarus->usbdev->found->interface, C_PURGETX); + interface, C_PURGETX); if (icarus->usbinfo.nodev) return; // Clear any received data transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX, - icarus->usbdev->found->interface, C_PURGERX); - - icarus->usbdev->PrefPacketSize = BLT_PREF_PACKET; + interface, C_PURGERX); break; case IDENT_ICA: + usb_set_pps(icarus, ICA_PREF_PACKET); + // Set Data Control transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL, - icarus->usbdev->found->interface, C_SETDATA); + interface, C_SETDATA); if (icarus->usbinfo.nodev) return; @@ -350,30 +355,29 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) // Set Line Control uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 }; _transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE, - icarus->usbdev->found->interface, - &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE); + interface, &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE); if (icarus->usbinfo.nodev) return; // Vendor transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR, - icarus->usbdev->found->interface, C_VENDOR); - - icarus->usbdev->PrefPacketSize = ICA_PREF_PACKET; + interface, C_VENDOR); break; case IDENT_AMU: + usb_set_pps(icarus, AMU_PREF_PACKET); + // Enable the UART transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE, CP210X_VALUE_UART_ENABLE, - icarus->usbdev->found->interface, C_ENABLE_UART); + interface, C_ENABLE_UART); if (icarus->usbinfo.nodev) return; // Set data control transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA, - icarus->usbdev->found->interface, C_SETDATA); + interface, C_SETDATA); if (icarus->usbinfo.nodev) return; @@ -381,15 +385,11 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) // Set the baud uint32_t data = CP210X_DATA_BAUD; _transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0, - icarus->usbdev->found->interface, - &data, sizeof(data), C_SETBAUD); - - icarus->usbdev->PrefPacketSize = AMU_PREF_PACKET; + interface, &data, sizeof(data), C_SETBAUD); break; default: quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d", - icarus->drv->name, icarus->cgminer_id, - icarus->usbdev->ident); + icarus->drv->name, icarus->cgminer_id, ident); } } @@ -483,6 +483,7 @@ static const char *timing_mode_str(enum timing_mode timing_mode) static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus) { struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); + enum sub_ident ident; double Hs; char buf[BUFSIZ+1]; char *ptr, *comma, *eq; @@ -512,7 +513,8 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus) buf[max] = '\0'; } - switch (icarus->usbdev->ident) { + ident = usb_ident(icarus); + switch (ident) { case IDENT_ICA: info->Hs = ICARUS_REV3_HASH_TIME; break; @@ -530,7 +532,7 @@ static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus) break; default: quit(1, "Icarus get_options() called with invalid %s ident=%d", - icarus->drv->name, icarus->usbdev->ident); + icarus->drv->name, ident); } info->read_time = 0; @@ -616,6 +618,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b { char buf[BUFSIZ+1]; char *ptr, *comma, *colon, *colon2; + enum sub_ident ident; size_t max; int i, tmp; @@ -642,7 +645,8 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b buf[max] = '\0'; } - switch (icarus->usbdev->ident) { + ident = usb_ident(icarus); + switch (ident) { case IDENT_ICA: case IDENT_BLT: case IDENT_LLT: @@ -664,7 +668,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b break; default: quit(1, "Icarus get_options() called with invalid %s ident=%d", - icarus->drv->name, icarus->usbdev->ident); + icarus->drv->name, ident); } if (*buf) { diff --git a/usbutils.c b/usbutils.c index 30b64e65..2f861c9a 100644 --- a/usbutils.c +++ b/usbutils.c @@ -2944,6 +2944,53 @@ void usb_disable_cps(struct cgpu_info *cgpu) DEVUNLOCK(cgpu, pstate); } +/* + * The value returned (0) when usbdev is NULL + * doesn't matter since it also means the next call to + * any usbutils function will fail with a nodev + */ +int usb_interface(struct cgpu_info *cgpu) +{ + int interface = 0; + int pstate; + + DEVLOCK(cgpu, pstate); + + if (cgpu->usbdev) + interface = cgpu->usbdev->found->interface; + + DEVUNLOCK(cgpu, pstate); + + return interface; +} + +enum sub_ident usb_ident(struct cgpu_info *cgpu) +{ + enum sub_ident ident = IDENT_UNK; + int pstate; + + DEVLOCK(cgpu, pstate); + + if (cgpu->usbdev) + ident = cgpu->usbdev->ident; + + DEVUNLOCK(cgpu, pstate); + + return ident; +} + +void usb_set_pps(struct cgpu_info *cgpu, uint16_t PrefPacketSize) +{ + int pstate; + + DEVLOCK(cgpu, pstate); + + if (cgpu->usbdev) + cgpu->usbdev->PrefPacketSize = PrefPacketSize; + + DEVUNLOCK(cgpu, pstate); +} + // Need to set all devices with matching usbdev void usb_set_dev_start(struct cgpu_info *cgpu) { diff --git a/usbutils.h b/usbutils.h index 49058fc5..151d0a30 100644 --- a/usbutils.h +++ b/usbutils.h @@ -309,6 +309,9 @@ uint32_t usb_buffer_size(struct cgpu_info *cgpu); void usb_set_cps(struct cgpu_info *cgpu, int cps); void usb_enable_cps(struct cgpu_info *cgpu); void usb_disable_cps(struct cgpu_info *cgpu); +int usb_interface(struct cgpu_info *cgpu); +enum sub_ident usb_ident(struct cgpu_info *cgpu); +void usb_set_pps(struct cgpu_info *cgpu, uint16_t PrefPacketSize); void usb_set_dev_start(struct cgpu_info *cgpu); void usb_cleanup(); void usb_initialise();