toolbox: add verbose option for lsusb
Change-Id: If9c51d96af7d69395917af55bb337352576f9422
diff --git a/toolbox/lsusb.c b/toolbox/lsusb.c
index 90ccf4e..236e74b 100644
--- a/toolbox/lsusb.c
+++ b/toolbox/lsusb.c
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
@@ -21,29 +22,166 @@
#include <usbhost/usbhost.h>
+static int verbose = 0;
+static char str_buff[4096];
+
+static const char *get_str(struct usb_device *dev, int id)
+{
+ char *str = usb_device_get_string(dev, id);
+
+ if (id && str) {
+ strlcpy(str_buff, str, sizeof(str_buff));
+ free(str);
+ } else {
+ snprintf(str_buff, sizeof(str_buff), "%02x", id);
+ }
+
+ return str_buff;
+}
+
+
+static void lsusb_parse_device_descriptor(struct usb_device *dev,
+ struct usb_device_descriptor *desc)
+{
+ printf(" Device Descriptor\n");
+ printf("\tbcdUSB: %04x\n", letoh16(desc->bcdUSB));
+ printf("\tbDeviceClass: %02x\n", desc->bDeviceClass);
+ printf("\tbDeviceSubClass: %02x\n", desc->bDeviceSubClass);
+ printf("\tbDeviceProtocol: %02x\n", desc->bDeviceProtocol);
+ printf("\tbMaxPacketSize0: %02x\n", desc->bMaxPacketSize0);
+ printf("\tidVendor: %04x\n", letoh16(desc->idVendor));
+ printf("\tidProduct: %04x\n", letoh16(desc->idProduct));
+ printf("\tbcdDevice: %04x\n", letoh16(desc->bcdDevice));
+ printf("\tiManufacturer: %s\n", get_str(dev, desc->iManufacturer));
+ printf("\tiProduct: %s\n", get_str(dev, desc->iProduct));
+ printf("\tiSerialNumber: %s\n", get_str(dev,desc->iSerialNumber));
+ printf("\tbNumConfiguration: %02x\n", desc->bNumConfigurations);
+ printf("\n");
+}
+
+static void lsusb_parse_config_descriptor(struct usb_device *dev,
+ struct usb_config_descriptor *desc)
+{
+ printf(" Config Descriptor\n");
+ printf("\twTotalLength: %04x\n", letoh16(desc->wTotalLength));
+ printf("\tbNumInterfaces: %02x\n", desc->bNumInterfaces);
+ printf("\tbConfigurationValue: %02x\n", desc->bConfigurationValue);
+ printf("\tiConfiguration: %s\n", get_str(dev, desc->iConfiguration));
+ printf("\tbmAttributes: %02x\n", desc->bmAttributes);
+ printf("\tbMaxPower: %d mA\n", desc->bMaxPower * 2);
+ printf("\n");
+}
+
+static void lsusb_parse_interface_descriptor(struct usb_device *dev,
+ struct usb_interface_descriptor *desc)
+{
+ printf(" Interface Descriptor\n");
+ printf("\tbInterfaceNumber: %02x\n", desc->bInterfaceNumber);
+ printf("\tbAlternateSetting: %02x\n", desc->bAlternateSetting);
+ printf("\tbNumEndpoints: %02x\n", desc->bNumEndpoints);
+ printf("\tbInterfaceClass: %02x\n", desc->bInterfaceClass);
+ printf("\tbInterfaceSubClass: %02x\n", desc->bInterfaceSubClass);
+ printf("\tbInterfaceProtocol: %02x\n", desc->bInterfaceProtocol);
+ printf("\tiInterface: %s\n", get_str(dev, desc->iInterface));
+ printf("\n");
+}
+
+static void lsusb_parse_endpoint_descriptor(struct usb_device *dev,
+ struct usb_endpoint_descriptor *desc)
+{
+ printf(" Endpoint Descriptor\n");
+ printf("\tbEndpointAddress: %02x\n", desc->bEndpointAddress);
+ printf("\tbmAttributes: %02x\n", desc->bmAttributes);
+ printf("\twMaxPacketSize: %02x\n", letoh16(desc->wMaxPacketSize));
+ printf("\tbInterval: %02x\n", desc->bInterval);
+ printf("\tbRefresh: %02x\n", desc->bRefresh);
+ printf("\tbSynchAddress: %02x\n", desc->bSynchAddress);
+ printf("\n");
+}
+
+static void lsusb_dump_descriptor(struct usb_device *dev,
+ struct usb_descriptor_header *desc)
+{
+ int i;
+ printf(" Descriptor type %02x\n", desc->bDescriptorType);
+
+ for (i = 0; i < desc->bLength; i++ ) {
+ if ((i % 16) == 0)
+ printf("\t%02x:", i);
+ printf(" %02x", ((uint8_t *)desc)[i]);
+ if ((i % 16) == 15)
+ printf("\n");
+ }
+
+ if ((i % 16) != 0)
+ printf("\n");
+ printf("\n");
+}
+
+static void lsusb_parse_descriptor(struct usb_device *dev,
+ struct usb_descriptor_header *desc)
+{
+ switch (desc->bDescriptorType) {
+ case USB_DT_DEVICE:
+ lsusb_parse_device_descriptor(dev, (struct usb_device_descriptor *) desc);
+ break;
+
+ case USB_DT_CONFIG:
+ lsusb_parse_config_descriptor(dev, (struct usb_config_descriptor *) desc);
+ break;
+
+ case USB_DT_INTERFACE:
+ lsusb_parse_interface_descriptor(dev, (struct usb_interface_descriptor *) desc);
+ break;
+
+ case USB_DT_ENDPOINT:
+ lsusb_parse_endpoint_descriptor(dev, (struct usb_endpoint_descriptor *) desc);
+ break;
+
+ default:
+ lsusb_dump_descriptor(dev, desc);
+
+ break;
+ }
+}
+
static int lsusb_device_added(const char *dev_name, void *client_data)
{
struct usb_device *dev = usb_device_open(dev_name);
- uint16_t vid, pid;
- char *mfg_name, *product_name, *serial;
if (!dev) {
fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
return 0;
}
- vid = usb_device_get_vendor_id(dev);
- pid = usb_device_get_product_id(dev);
- mfg_name = usb_device_get_manufacturer_name(dev);
- product_name = usb_device_get_product_name(dev);
- serial = usb_device_get_serial(dev);
+ if (verbose) {
+ struct usb_descriptor_iter iter;
+ struct usb_descriptor_header *desc;
- printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
- mfg_name, product_name, serial);
+ printf("%s:\n", dev_name);
- free(mfg_name);
- free(product_name);
- free(serial);
+ usb_descriptor_iter_init(dev, &iter);
+
+ while ((desc = usb_descriptor_iter_next(&iter)) != NULL)
+ lsusb_parse_descriptor(dev, desc);
+
+ } else {
+ uint16_t vid, pid;
+ char *mfg_name, *product_name, *serial;
+
+ vid = usb_device_get_vendor_id(dev);
+ pid = usb_device_get_product_id(dev);
+ mfg_name = usb_device_get_manufacturer_name(dev);
+ product_name = usb_device_get_product_name(dev);
+ serial = usb_device_get_serial(dev);
+
+ printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
+ mfg_name, product_name, serial);
+
+ free(mfg_name);
+ free(product_name);
+ free(serial);
+ }
usb_device_close(dev);
@@ -65,7 +203,12 @@
int lsusb_main(int argc, char **argv)
{
- struct usb_host_context *ctx = usb_host_init();
+ struct usb_host_context *ctx;
+
+ if (argc == 2 && !strcmp(argv[1], "-v"))
+ verbose = 1;
+
+ ctx = usb_host_init();
if (!ctx) {
perror("usb_host_init:");
return 1;