Index: uspace/app/usbinfo/dump.c
===================================================================
--- uspace/app/usbinfo/dump.c	(revision 5ccb15c85083b046d590fe4d2fb51f864ce74ab4)
+++ uspace/app/usbinfo/dump.c	(revision c9d5577cfba2ae0264c71b27709c24b1f5f95541)
@@ -43,4 +43,5 @@
 #include <usb/usb.h>
 #include <usb/descriptor.h>
+#include <usb/classes/classes.h>
 
 #include "usbinfo.h"
@@ -48,4 +49,5 @@
 
 #define INDENT "  "
+#define PRINTLINE(indent, fmt, ...) printf("%s - " fmt, get_indent(indent), __VA_ARGS__)
 #define BYTES_PER_LINE 12
 
@@ -56,17 +58,207 @@
 #define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
 
-void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
-{
-	printf("%s\n", msg);
+static void dump_descriptor_by_type(size_t, uint8_t *, size_t);
+
+typedef struct {
+	int descriptor;
+	void (*dump)(size_t indent, uint8_t *descriptor, size_t length);
+} descriptor_dump_t;
+
+static void dump_descriptor_device(size_t, uint8_t *, size_t);
+static void dump_descriptor_configuration(size_t, uint8_t *, size_t);
+static void dump_descriptor_interface(size_t, uint8_t *, size_t);
+static void dump_descriptor_string(size_t, uint8_t *, size_t);
+static void dump_descriptor_endpoint(size_t, uint8_t *, size_t);
+static void dump_descriptor_hid(size_t, uint8_t *, size_t);
+static void dump_descriptor_hub(size_t, uint8_t *, size_t);
+static void dump_descriptor_generic(size_t, uint8_t *, size_t);
+
+static descriptor_dump_t descriptor_dumpers[] = {
+	{ USB_DESCTYPE_DEVICE, dump_descriptor_device },
+	{ USB_DESCTYPE_CONFIGURATION, dump_descriptor_configuration },
+	{ USB_DESCTYPE_STRING, dump_descriptor_string },
+	{ USB_DESCTYPE_INTERFACE, dump_descriptor_interface },
+	{ USB_DESCTYPE_ENDPOINT, dump_descriptor_endpoint },
+	{ USB_DESCTYPE_HID, dump_descriptor_hid },
+	{ USB_DESCTYPE_HUB, dump_descriptor_hub },
+	{ -1, dump_descriptor_generic },
+	{ -1, NULL }
+};
+
+static const char *get_indent(size_t level)
+{
+	static const char *indents[] = {
+		INDENT,
+		INDENT INDENT,
+		INDENT INDENT INDENT,
+		INDENT INDENT INDENT INDENT,
+		INDENT INDENT INDENT INDENT INDENT
+	};
+	static size_t indents_count = sizeof(indents)/sizeof(indents[0]);
+	if (level >= indents_count) {
+		return indents[indents_count - 1];
+	}
+	return indents[level];
+}
+
+void dump_buffer(const char *msg, size_t indent,
+    const uint8_t *buffer, size_t length)
+{
+	if (msg != NULL) {
+		printf("%s\n", msg);
+	}
 
 	size_t i;
+	if (length > 0) {
+		printf("%s", get_indent(indent));
+	}
 	for (i = 0; i < length; i++) {
-		printf("  0x%02X", buffer[i]);
+		printf("0x%02X", buffer[i]);
 		if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
 		    || (i + 1 == length)) {
 			printf("\n");
+			if (i + 1 < length) {
+				printf("%s", get_indent(indent));
+			}
+		} else {
+			printf("  ");
 		}
 	}
 }
+
+void dump_descriptor_by_type(size_t indent, uint8_t *d, size_t length)
+{
+	if (length < 2) {
+		return;
+	}
+	int type = d[1];
+	
+	descriptor_dump_t *dumper = descriptor_dumpers;
+	while (dumper->dump != NULL) {
+		if ((dumper->descriptor == type) || (dumper->descriptor < 0)) {
+			dumper->dump(indent, d, length);
+			return;
+		}
+		dumper++;
+	}			
+}
+
+void dump_usb_descriptor(uint8_t *descriptor, size_t size)
+{
+	dump_descriptor_by_type(0, descriptor, size);
+}
+
+void dump_descriptor_device(size_t indent, uint8_t *descr, size_t size)
+{
+	usb_standard_device_descriptor_t *d
+	    = (usb_standard_device_descriptor_t *) descr;
+	if (size != sizeof(*d)) {
+		return;
+	}
+	
+	PRINTLINE(indent, "bLength = %d\n", d->length);
+	PRINTLINE(indent, "bDescriptorType = 0x%02x\n", d->descriptor_type);
+	PRINTLINE(indent, "bcdUSB = %d (" BCD_FMT ")\n", d->usb_spec_version,
+	    BCD_ARGS(d->usb_spec_version));
+	PRINTLINE(indent, "bDeviceClass = 0x%02x\n", d->device_class);
+	PRINTLINE(indent, "bDeviceSubClass = 0x%02x\n", d->device_subclass);
+	PRINTLINE(indent, "bDeviceProtocol = 0x%02x\n", d->device_protocol);
+	PRINTLINE(indent, "bMaxPacketSize0 = %d\n", d->max_packet_size);
+	PRINTLINE(indent, "idVendor = %d\n", d->vendor_id);
+	PRINTLINE(indent, "idProduct = %d\n", d->product_id);
+	PRINTLINE(indent, "bcdDevice = %d\n", d->device_version);
+	PRINTLINE(indent, "iManufacturer = %d\n", d->str_manufacturer);
+	PRINTLINE(indent, "iProduct = %d\n", d->str_product);
+	PRINTLINE(indent, "iSerialNumber = %d\n", d->str_serial_number);
+	PRINTLINE(indent, "bNumConfigurations = %d\n", d->configuration_count);
+}
+
+void dump_descriptor_configuration(size_t indent, uint8_t *descr, size_t size)
+{
+	usb_standard_configuration_descriptor_t *d
+	    = (usb_standard_configuration_descriptor_t *) descr;
+	if (size != sizeof(*d)) {
+		return;
+	}
+	
+	bool self_powered = d->attributes & 64;
+	bool remote_wakeup = d->attributes & 32;
+	
+	PRINTLINE(indent, "bLength = %d\n", d->length);
+	PRINTLINE(indent, "bDescriptorType = 0x%02x\n", d->descriptor_type);
+	PRINTLINE(indent, "wTotalLength = %d\n", d->total_length);
+	PRINTLINE(indent, "bNumInterfaces = %d\n", d->interface_count);
+	PRINTLINE(indent, "bConfigurationValue = %d\n", d->configuration_number);
+	PRINTLINE(indent, "iConfiguration = %d\n", d->str_configuration);
+	PRINTLINE(indent, "bmAttributes = %d [%s%s%s]\n", d->attributes,
+	    self_powered ? "self-powered" : "",
+	    (self_powered & remote_wakeup) ? ", " : "",
+	    remote_wakeup ? "remote-wakeup" : "");
+	PRINTLINE(indent, "MaxPower = %d (%dmA)\n", d->max_power,
+	    2 * d->max_power);
+}
+
+void dump_descriptor_interface(size_t indent, uint8_t *descr, size_t size)
+{
+	usb_standard_interface_descriptor_t *d
+	    = (usb_standard_interface_descriptor_t *) descr;
+	if (size != sizeof(*d)) {
+		return;
+	}
+	
+	PRINTLINE(indent, "bLength = %d\n", d->length);
+	PRINTLINE(indent, "bDescriptorType = 0x%02x\n", d->descriptor_type);
+	PRINTLINE(indent, "bInterfaceNumber = %d\n", d->interface_number);
+	PRINTLINE(indent, "bAlternateSetting = %d\n", d->alternate_setting);
+	PRINTLINE(indent, "bNumEndpoints = %d\n", d->endpoint_count);
+	PRINTLINE(indent, "bInterfaceClass = %s\n", d->interface_class == 0
+	    ? "reserved (0)" : usb_str_class(d->interface_class));
+	PRINTLINE(indent, "bInterfaceSubClass = %d\n", d->interface_subclass);
+	PRINTLINE(indent, "bInterfaceProtocol = %d\n", d->interface_protocol);
+	PRINTLINE(indent, "iInterface = %d\n", d->str_interface);
+}
+
+void dump_descriptor_string(size_t indent, uint8_t *descr, size_t size)
+{
+	dump_descriptor_generic(indent, descr, size);
+}
+
+void dump_descriptor_endpoint(size_t indent, uint8_t *descr, size_t size)
+{
+	usb_standard_endpoint_descriptor_t *d
+	   = (usb_standard_endpoint_descriptor_t *) descr;
+	if (size != sizeof(*d)) {
+		return;
+	}
+	
+	int endpoint = d->endpoint_address & 15;
+	usb_direction_t direction = d->endpoint_address & 128
+	    ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
+	
+	PRINTLINE(indent, "bLength = %d\n", d->length);
+	PRINTLINE(indent, "bDescriptorType = 0x%02X\n", d->descriptor_type);
+	PRINTLINE(indent, "bEndpointAddress = 0x%02X [%d, %s]\n",
+	    d->endpoint_address, endpoint,
+	    direction == USB_DIRECTION_IN ? "in" : "out");
+	PRINTLINE(indent, "bmAttributes = %d\n", d->attributes);
+	PRINTLINE(indent, "wMaxPacketSize = %d\n", d->max_packet_size);
+	PRINTLINE(indent, "bInterval = %dms\n", d->poll_interval);
+}
+
+void dump_descriptor_hid(size_t indent, uint8_t *descr, size_t size)
+{
+	dump_descriptor_generic(indent, descr, size);
+}
+
+void dump_descriptor_hub(size_t indent, uint8_t *descr, size_t size)
+{
+	dump_descriptor_generic(indent, descr, size);
+}
+
+void dump_descriptor_generic(size_t indent, uint8_t *descr, size_t size)
+{
+	dump_buffer(NULL, indent, descr, size);
+}
+
 
 void dump_match_ids(match_id_list_t *matches)
@@ -83,55 +275,8 @@
 }
 
-void dump_standard_device_descriptor(usb_standard_device_descriptor_t *d)
-{
-	printf("Standard device descriptor:\n");
-
-	printf(INDENT "bLength = %d\n", d->length);
-	printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
-	printf(INDENT "bcdUSB = %d (" BCD_FMT ")\n", d->usb_spec_version,
-	    BCD_ARGS(d->usb_spec_version));
-	printf(INDENT "bDeviceClass = 0x%02x\n", d->device_class);
-	printf(INDENT "bDeviceSubClass = 0x%02x\n", d->device_subclass);
-	printf(INDENT "bDeviceProtocol = 0x%02x\n", d->device_protocol);
-	printf(INDENT "bMaxPacketSize0 = %d\n", d->max_packet_size);
-	printf(INDENT "idVendor = %d\n", d->vendor_id);
-	printf(INDENT "idProduct = %d\n", d->product_id);
-	printf(INDENT "bcdDevice = %d\n", d->device_version);
-	printf(INDENT "iManufacturer = %d\n", d->str_manufacturer);
-	printf(INDENT "iProduct = %d\n", d->str_product);
-	printf(INDENT "iSerialNumber = %d\n", d->str_serial_number);
-	printf(INDENT "bNumConfigurations = %d\n", d->configuration_count);
-}
-
-void dump_standard_configuration_descriptor(
-    int index, usb_standard_configuration_descriptor_t *d)
-{
-	bool self_powered = d->attributes & 64;
-	bool remote_wakeup = d->attributes & 32;
-	
-	printf("Standard configuration descriptor #%d\n", index);
-	printf(INDENT "bLength = %d\n", d->length);
-	printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
-	printf(INDENT "wTotalLength = %d\n", d->total_length);
-	printf(INDENT "bNumInterfaces = %d\n", d->interface_count);
-	printf(INDENT "bConfigurationValue = %d\n", d->configuration_number);
-	printf(INDENT "iConfiguration = %d\n", d->str_configuration);
-	printf(INDENT "bmAttributes = %d [%s%s%s]\n", d->attributes,
-	    self_powered ? "self-powered" : "",
-	    (self_powered & remote_wakeup) ? ", " : "",
-	    remote_wakeup ? "remote-wakeup" : "");
-	printf(INDENT "MaxPower = %d (%dmA)\n", d->max_power,
-	    2 * d->max_power);
-	// printf(INDENT " = %d\n", d->);
-}
-
 static void dump_tree_descriptor(uint8_t *descriptor, size_t depth)
 {
 	if (descriptor == NULL) {
 		return;
-	}
-	while (depth > 0) {
-		printf("  ");
-		depth--;
 	}
 	int type = (int) *(descriptor + 1);
@@ -151,5 +296,7 @@
 #undef _TYPE
 	}
-	printf("0x%02x (%s)\n", type, name);
+	printf("%s%s (0x%02X):\n", get_indent(depth), name, type);
+	dump_descriptor_by_type(depth, descriptor, descriptor[0]);
+	
 }
 
@@ -172,5 +319,5 @@
 	uint8_t *ptr = data->data;
 	printf("Descriptor tree:\n");
-	dump_tree_internal(parser, data, ptr, 1);
+	dump_tree_internal(parser, data, ptr, 0);
 }
 
