Commit 2631b55ee2683c3a849d812dd8d30779f032e863

Kano 2013-05-16T21:38:55

usbutils - make FTDI handling automatic

diff --git a/driver-bflsc.c b/driver-bflsc.c
index 0e8c57d..a532e0d 100644
--- a/driver-bflsc.c
+++ b/driver-bflsc.c
@@ -487,7 +487,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
 {
 	char buf[BFLSC_BUFSIZ+1];
 
-	*err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
+	*err = usb_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
 	if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
 		return false;
 	else
@@ -496,7 +496,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
 
 static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz)
 {
-	*err = usb_ftdi_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
+	*err = usb_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
 	if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
 		return false;
 	else {
@@ -529,7 +529,7 @@ static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
 	}
 }
 
-/* return True = attempted usb_ftdi_read_ok()
+/* return True = attempted usb_read_ok()
  * set ignore to true means no applog/ignore errors */
 static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
 {
@@ -550,7 +550,7 @@ static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int de
 		// of course all other I/O must also be failing ...
 	} else {
 		readok = true;
-		*err = usb_ftdi_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
+		*err = usb_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
 		mutex_unlock(&(bflsc->device_mutex));
 
 		if (*err < 0 || *amount < 1) {
@@ -695,7 +695,7 @@ static bool getinfo(struct cgpu_info *bflsc, int dev)
 		return false;
 	}
 
-	err = usb_ftdi_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
+	err = usb_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
 	if (err < 0 || amount < 1) {
 		if (err < 0) {
 			applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
@@ -830,7 +830,7 @@ reinit:
 		goto unshin;
 	}
 
-	err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
+	err = usb_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
 	if (err < 0 || amount < 1) {
 		init_count++;
 		cgtime(&init_now);
@@ -1095,7 +1095,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
 		return false;
 	}
 
-	err = usb_ftdi_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
+	err = usb_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
 	if (err < 0 || amount < 1) {
 		mutex_unlock(&(bflsc->device_mutex));
 		if (err < 0) {
@@ -1117,7 +1117,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
 		return false;
 	}
 
-	err = usb_ftdi_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
+	err = usb_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
 	if (err < 0 || amount < 1) {
 		mutex_unlock(&(bflsc->device_mutex));
 		if (err < 0) {
diff --git a/driver-bitforce.c b/driver-bitforce.c
index f703edc..7b4361b 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -200,7 +200,7 @@ reinit:
 		goto unshin;
 	}
 
-	if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
+	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
 		init_count++;
 		cgtime(&init_now);
 		if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
@@ -395,7 +395,7 @@ static bool bitforce_get_temp(struct cgpu_info *bitforce)
 		return false;
 	}
 
-	if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
+	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
 		mutex_unlock(&bitforce->device_mutex);
 		if (err < 0) {
 			applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)",
@@ -470,7 +470,7 @@ re_send:
 		return false;
 	}
 
-	if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
+	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
 		mutex_unlock(&bitforce->device_mutex);
 		applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)",
 				bitforce->drv->name, bitforce->device_id, amount, err);
@@ -524,7 +524,7 @@ re_send:
 		return false;
 	}
 
-	if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
+	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
 		mutex_unlock(&bitforce->device_mutex);
 		applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)",
 				bitforce->drv->name, bitforce->device_id, amount, err);
@@ -573,7 +573,7 @@ static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 
 		mutex_lock(&bitforce->device_mutex);
 		usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS);
-		usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
+		usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
 		mutex_unlock(&bitforce->device_mutex);
 
 		cgtime(&now);
diff --git a/usbutils.c b/usbutils.c
index f010960..2d286d1 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -115,13 +115,15 @@ static struct usb_endpoints cmr_eps[] = {
 };
 #endif
 
+#define IDVENDOR_FTDI 0x0403
+
 // TODO: Add support for (at least) Isochronous endpoints
 static struct usb_find_devices find_dev[] = {
 #ifdef USE_BFLSC
 	{
 		.drv = DRV_BFLSC,
 		.name = "BAS",
-		.idVendor = 0x0403,
+		.idVendor = IDVENDOR_FTDI,
 		.idProduct = 0x6014,
 		.iManufacturer = "Butterfly Labs",
 		.iProduct = "BitFORCE SHA256 SC",
@@ -136,7 +138,7 @@ static struct usb_find_devices find_dev[] = {
 	{
 		.drv = DRV_BITFORCE,
 		.name = "BFL",
-		.idVendor = 0x0403,
+		.idVendor = IDVENDOR_FTDI,
 		.idProduct = 0x6014,
 		.iManufacturer = "Butterfly Labs Inc.",
 		.iProduct = "BitFORCE SHA256",
@@ -164,7 +166,7 @@ static struct usb_find_devices find_dev[] = {
 	{
 		.drv = DRV_AVALON,
 		.name = "AVA",
-		.idVendor = 0x0403,
+		.idVendor = IDVENDOR_FTDI,
 		.idProduct = 0x6001,
 		.kernel = 0,
 		.config = 1,
@@ -199,7 +201,7 @@ static struct usb_find_devices find_dev[] = {
 	{
 		.drv = DRV_ICARUS,
 		.name = "BLT",
-		.idVendor = 0x0403,
+		.idVendor = IDVENDOR_FTDI,
 		.idProduct = 0x6001,
 		.iProduct = "FT232R USB UART",
 		.kernel = 0,
@@ -212,7 +214,7 @@ static struct usb_find_devices find_dev[] = {
 	{
 		.drv = DRV_ICARUS,
 		.name = "LLT",
-		.idVendor = 0x0403,
+		.idVendor = IDVENDOR_FTDI,
 		.idProduct = 0x6001,
 		.kernel = 0,
 		.config = 1,
@@ -1330,6 +1332,9 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
 	cgusb = calloc(1, sizeof(*cgusb));
 	cgusb->found = found;
 
+	if (found->idVendor == IDVENDOR_FTDI)
+		cgusb->usb_type = USB_TYPE_FTDI;
+
 	cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
 
 	err = libusb_get_device_descriptor(dev, cgusb->descriptor);
@@ -1911,9 +1916,10 @@ static void rejected_inc(struct cgpu_info *cgpu)
 
 #define USB_MAX_READ 8192
 
-int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce)
+int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce)
 {
 	struct cg_usb_device *usbdev = cgpu->usbdev;
+	bool ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
 #if DO_USB_STATS
 	struct timeval tv_start;
 #endif
diff --git a/usbutils.h b/usbutils.h
index 2c4f642..64a4546 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -65,12 +65,18 @@ struct usb_find_devices {
 	struct usb_endpoints *eps;
 };
 
+enum usb_types {
+	USB_TYPE_STD = 0,
+	USB_TYPE_FTDI
+};
+
 struct cg_usb_device {
 	struct usb_find_devices *found;
 	libusb_device_handle *handle;
 	pthread_mutex_t *mutex;
 	struct libusb_device_descriptor *descriptor;
 	uint16_t usbver;
+	enum usb_types usb_type;
 	int speed;
 	char *prod_string;
 	char *manuf_string;
@@ -150,29 +156,32 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
 void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
 struct api_data *api_usb_stats(int *count);
 void update_usb_stats(struct cgpu_info *cgpu);
-int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce);
+int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce);
 int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
 int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned int timeout, enum usb_cmds cmd);
 void usb_cleanup();
 void usb_initialise();
 
 #define usb_read(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
 
 #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, true)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
 
 #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false, false)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
+
+#define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
 
 #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false)
+	_usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
 
 #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false)
+	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
 
 #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
-	_usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false, false)
+	_usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
 
 #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
 	_usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
@@ -186,15 +195,6 @@ void usb_initialise();
 #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
 	_usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
 
-#define usb_ftdi_read(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false)
-
-#define usb_ftdi_read_nl(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, true, false)
-
-#define usb_ftdi_read_ok(cgpu, buf, bufsiz, read, cmd) \
-	_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, true, false)
-
 #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
 	_usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd)