Commit e5a5be005a77a364e47217f27717ecd2f26ebdf7

Con Kolivas 2013-03-14T06:35:06

Merge pull request #405 from kanoi/bflsc USB modify -n and --usb-dump to only show known devices or use new --usb-list-all option to see all

diff --git a/README b/README
index 29b3105..37b18c7 100644
--- a/README
+++ b/README
@@ -252,10 +252,11 @@ which means any devices on USB bus_number 1
 This is useful if you unplug a device then plug it back in the same port,
 it usually reappears with the same bus_number but a different device_address
 
-You can see the list of USB devices on linux with 'sudo lsusb'
-Cgminer will list the USB devices with the '--usb-dump 0' option
+You can see the list of all USB devices on linux with 'sudo lsusb'
+Cgminer will list the recognised USB devices with the '--usb-dump 0' option
 The '--usb-dump N' option with a value of N greater than 0 will dump a lot
-of details about each USB device
+of details about each recognised USB device
+If you wish to see all USB devices, include the --usb-list-all option
 
 The second version
   --usb BAS:1,BFL:1,MMQ:0
diff --git a/cgminer.c b/cgminer.c
index 0e70007..39bb11a 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -139,6 +139,7 @@ bool opt_worktime;
 #ifdef USE_USBUTILS
 char *opt_usb_select = NULL;
 int opt_usbdump = -1;
+bool opt_usb_list_all;
 #endif
 
 char *opt_kernel_path;
@@ -1174,6 +1175,9 @@ static struct opt_table opt_config_table[] = {
 	OPT_WITH_ARG("--usb-dump",
 		     set_int_0_to_10, opt_show_intval, &opt_usbdump,
 		     opt_hidden),
+	OPT_WITHOUT_ARG("--usb-list-all",
+			opt_set_bool, &opt_usb_list_all,
+			opt_hidden),
 #endif
 #ifdef HAVE_OPENCL
 	OPT_WITH_ARG("--vectors|-v",
diff --git a/miner.h b/miner.h
index 20ef31c..cd15b40 100644
--- a/miner.h
+++ b/miner.h
@@ -781,6 +781,7 @@ extern bool opt_worktime;
 #ifdef USE_USBUTILS
 extern char *opt_usb_select;
 extern int opt_usbdump;
+extern bool opt_usb_list_all;
 #endif
 #ifdef USE_BITFORCE
 extern bool opt_bfl_noncerange;
diff --git a/usbutils.c b/usbutils.c
index e7567ef..151d3cd 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -411,7 +411,7 @@ static bool setgetdes(ssize_t count, libusb_device *dev, struct libusb_device_ha
 	return true;
 }
 
-static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off, size_t *len, int level)
+static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off, size_t *len, int level)
 {
 	struct libusb_device_descriptor desc;
 	uint8_t bus_number;
@@ -427,9 +427,9 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 	int err, i, j, k;
 
 	err = libusb_get_device_descriptor(dev, &desc);
-	if (err) {
+	if (opt_usb_list_all && err) {
 		sprintf(tmp, EOL ".USB dev %d: Failed to get descriptor, err %d",
-					(int)count, err);
+					(int)(++(*count)), err);
 		append(buf, tmp, off, len);
 		return;
 	}
@@ -437,9 +437,25 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 	bus_number = libusb_get_bus_number(dev);
 	device_address = libusb_get_device_address(dev);
 
+	if (!opt_usb_list_all) {
+		bool known = false;
+
+		for (i = 0; find_dev[i].drv != DRV_LAST; i++)
+			if ((find_dev[i].idVendor == desc.idVendor) &&
+			    (find_dev[i].idProduct == desc.idProduct)) {
+				known = true;
+				break;
+			}
+
+		if (!known)
+			return;
+	}
+
+	(*count)++;
+
 	if (level == 0) {
 		sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
-				(int)count, (int)bus_number, (int)device_address,
+				(int)(*count), (int)bus_number, (int)device_address,
 				desc.idVendor, desc.idProduct);
 	} else {
 		sprintf(tmp, EOL ".USB dev %d: Bus %d Device %d Device Descriptor:" EOL "\tLength: %d" EOL
@@ -447,7 +463,7 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 			"\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
 			"\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
 			"\tNumConfigurations: %d",
-				(int)count, (int)bus_number, (int)device_address,
+				(int)(*count), (int)bus_number, (int)device_address,
 				(int)(desc.bLength), destype(desc.bDescriptorType),
 				desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
 				(int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
@@ -458,7 +474,7 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 
 	err = libusb_open(dev, &handle);
 	if (err) {
-		sprintf(tmp, EOL "  ** dev %d: Failed to open, err %d", (int)count, err);
+		sprintf(tmp, EOL "  ** dev %d: Failed to open, err %d", (int)(*count), err);
 		append(buf, tmp, off, len);
 		return;
 	}
@@ -479,17 +495,17 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 	}
 
 	if (libusb_kernel_driver_active(handle, 0) == 1) {
-		sprintf(tmp, EOL "   * dev %d: kernel attached", (int)count);
+		sprintf(tmp, EOL "   * dev %d: kernel attached", (int)(*count));
 		append(buf, tmp, off, len);
 	}
 
 	err = libusb_get_active_config_descriptor(dev, &config);
 	if (err) {
-		if (!setgetdes(count, dev, handle, &config, 1, buf, off, len)
-		&&  !setgetdes(count, dev, handle, &config, 0, buf, off, len)) {
+		if (!setgetdes(*count, dev, handle, &config, 1, buf, off, len)
+		&&  !setgetdes(*count, dev, handle, &config, 0, buf, off, len)) {
 			libusb_close(handle);
 			sprintf(tmp, EOL "  ** dev %d: Failed to set config descriptor to %d or %d",
-					(int)count, 1, 0);
+					(int)(*count), 1, 0);
 			append(buf, tmp, off, len);
 			return;
 		}
@@ -498,7 +514,7 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 	sprintf(tmp, EOL "     dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
 			"\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
 			"\tAttributes: %d" EOL "\tMaxPower: %d",
-				(int)count, destype(config->bDescriptorType),
+				(int)(*count), destype(config->bDescriptorType),
 				(int)(config->bNumInterfaces), (int)(config->iConfiguration),
 				(int)(config->bmAttributes), (int)(config->MaxPower));
 	append(buf, tmp, off, len);
@@ -511,7 +527,7 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 					"\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
 					"\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
 					"\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
-						(int)count, j, destype(idesc->bDescriptorType),
+						(int)(*count), j, destype(idesc->bDescriptorType),
 						(int)(idesc->bInterfaceNumber),
 						(int)(idesc->bNumEndpoints),
 						(int)(idesc->bInterfaceClass),
@@ -527,7 +543,7 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 						"\tEndpointAddress: %s0x%x" EOL
 						"\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
 						"\tInterval: %d" EOL "\tRefresh: %d",
-							(int)count, (int)(idesc->bInterfaceNumber), k,
+							(int)(*count), (int)(idesc->bInterfaceNumber), k,
 							destype(epdesc->bDescriptorType),
 							epdir(epdesc->bEndpointAddress),
 							(int)(epdesc->bEndpointAddress),
@@ -549,7 +565,7 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 
 	sprintf(tmp, EOL "     dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
 			"\tProduct: '%s'" EOL "\tSerial '%s'",
-				(int)count, man, prod, ser);
+				(int)(*count), man, prod, ser);
 	append(buf, tmp, off, len);
 
 	libusb_close(handle);
@@ -559,7 +575,7 @@ static void usb_full(ssize_t count, libusb_device *dev, char **buf, size_t *off,
 void usb_all(int level)
 {
 	libusb_device **list;
-	ssize_t count, i;
+	ssize_t count, i, j;
 	char *buf;
 	size_t len, off;
 
@@ -577,15 +593,25 @@ void usb_all(int level)
 		buf = malloc(len+1);
 
 		sprintf(buf, "USB all: found %d devices", (int)count);
-
 		off = strlen(buf);
 
+		if (!opt_usb_list_all)
+			append(&buf, " - listing known devices", &off, &len);
+
+		j = -1;
 		for (i = 0; i < count; i++)
-			usb_full(i, list[i], &buf, &off, &len, level);
+			usb_full(&j, list[i], &buf, &off, &len, level);
 
 		applog(LOG_WARNING, "%s", buf);
 
 		free(buf);
+
+		if (j == -1)
+			applog(LOG_WARNING, "No known USB devices");
+		else
+			applog(LOG_WARNING, "%d %sUSB devices",
+				(int)(++j), opt_usb_list_all ? BLANK : "known ");
+
 	}
 
 	libusb_free_device_list(list, 1);