Commit cd79fbd45860ba29f5618f584b97a795b62928d6

Con Kolivas 2014-02-15T16:51:32

Use a unique usb_list function displaying only pertinent information when listing usb devices from the menu.

diff --git a/cgminer.c b/cgminer.c
index 6491a26..43d894b 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -5334,7 +5334,7 @@ retry:
 		hotplug_time = selected;
 		goto retry;
 	} else if (!strncasecmp(&input, "l", 1)) {
-		usb_all(0);
+		usb_list();
 		goto retry;
 	} else
 		clear_logwin();
diff --git a/usbutils.c b/usbutils.c
index 21bc653..c7b378d 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1274,6 +1274,80 @@ static bool is_in_use(libusb_device *dev)
 	return is_in_use_bd(libusb_get_bus_number(dev), libusb_get_device_address(dev));
 }
 
+static bool how_in_use(uint8_t bus_number, uint8_t device_address, bool *blacklisted)
+{
+	bool ret;
+	mutex_lock(&cgusb_lock);
+	ret = _in_use(in_use_head, bus_number, device_address);
+	if (!ret) {
+		if (_in_use(blacklist_head, bus_number, device_address))
+			*blacklisted = true;
+	}
+	mutex_unlock(&cgusb_lock);
+
+	return ret;
+}
+
+void usb_list(void)
+{
+	struct libusb_device_descriptor desc;
+	struct libusb_device_handle *handle;
+	uint8_t bus_number;
+	uint8_t device_address;
+	libusb_device **list;
+	ssize_t count, i, j;
+	int err, total = 0;
+
+	count = libusb_get_device_list(NULL, &list);
+	if (count < 0) {
+		applog(LOG_ERR, "USB list: failed, err:(%d) %s", (int)count, libusb_error_name((int)count));
+		return;
+	}
+	if (count == 0) {
+		applog(LOG_WARNING, "USB list: found no devices");
+		return;
+	}
+	for (i = 0; i < count; i++) {
+		bool known = false, blacklisted = false, active;
+		unsigned char manuf[256], prod[256];
+		libusb_device *dev = list[i];
+
+		err = libusb_get_device_descriptor(dev, &desc);
+		if (err) {
+			applog(LOG_WARNING, "USB list: Failed to get descriptor %d", (int)i);
+			break;
+		}
+
+		bus_number = libusb_get_bus_number(dev);
+		device_address = libusb_get_device_address(dev);
+
+		for (j = 0; find_dev[j].drv != DRIVER_MAX; j++) {
+			if ((find_dev[j].idVendor == desc.idVendor) &&
+			    (find_dev[j].idProduct == desc.idProduct)) {
+				known = true;
+				break;
+			}
+		}
+		if (!known)
+			continue;
+
+		err = libusb_open(dev, &handle);
+		if (err) {
+			applog(LOG_WARNING, "USB list: Failed to open %d", (int)i);
+			break;
+		}
+		libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, manuf, 255);
+		libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, 255);
+		total++;
+		active = how_in_use(bus_number, device_address, &blacklisted);
+		applog(LOG_WARNING, "Bus %u Device %u ID: %04x:%04x %s %s %sactive %s",
+		       bus_number, device_address, desc.idVendor, desc.idProduct,
+		       manuf, prod, active ? "" : "in", blacklisted ? "blacklisted" : "");
+	}
+	libusb_free_device_list(list, 1);
+	applog(LOG_WARNING, "%d total known USB device%s", total, total > 1 ? "s": "");
+}
+
 static void add_in_use(uint8_t bus_number, uint8_t device_address, bool blacklist)
 {
 	struct usb_in_use_list *in_use_tmp, **head;
diff --git a/usbutils.h b/usbutils.h
index c92d64d..14c5605 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -418,6 +418,7 @@ struct cgpu_info;
 bool async_usb_transfers(void);
 void cancel_usb_transfers(void);
 void usb_all(int level);
+void usb_list(void);
 const char *usb_cmdname(enum usb_cmds cmd);
 void usb_applog(struct cgpu_info *cgpu, enum usb_cmds cmd, char *msg, int amount, int err);
 void blacklist_cgpu(struct cgpu_info *cgpu);