changeset 87:4393e1b4b245

cp2102-decode-ee-desc: decode config descriptor
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 27 Sep 2023 19:15:29 +0000
parents b7397959ae68
children ea7b411aad27
files cp2102/decode_usb_desc.c
diffstat 1 files changed, 49 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/cp2102/decode_usb_desc.c	Wed Sep 27 18:46:10 2023 +0000
+++ b/cp2102/decode_usb_desc.c	Wed Sep 27 19:15:29 2023 +0000
@@ -10,6 +10,52 @@
 
 u_char eeprom[SIZE_EEPROM];
 
+static void
+print_device_desc(desc)
+	u_char *desc;
+{
+	printf("  bLength:            %u", desc[0]);
+	if (desc[0] != 18)
+		fputs(" (WRONG!)", stdout);
+	putchar('\n');
+	printf("  bDescriptorType:    0x%02X", desc[1]);
+	if (desc[1] != 0x01)
+		fputs(" (WRONG!)", stdout);
+	putchar('\n');
+	printf("  bcdUSB:             0x%02X%02X\n", desc[3], desc[2]);
+	printf("  bDeviceClass:       0x%02X\n", desc[4]);
+	printf("  bDeviceSubClass:    0x%02X\n", desc[5]);
+	printf("  bDeviceProtocol:    0x%02X\n", desc[6]);
+	printf("  bMaxPacketSize0:    %u\n", desc[7]);
+	printf("  idVendor:           0x%02X%02X\n", desc[9], desc[8]);
+	printf("  idProduct:          0x%02X%02X\n", desc[11], desc[10]);
+	printf("  bcdDevice:          0x%02X%02X\n", desc[13], desc[12]);
+	printf("  iManufacturer:      %u\n", desc[14]);
+	printf("  iProduct:           %u\n", desc[15]);
+	printf("  iSerialNumber:      %u\n", desc[16]);
+	printf("  bNumConfigurations: %u\n", desc[17]);
+}
+
+static void
+print_config_desc(desc)
+	u_char *desc;
+{
+	printf("  bLength:             %u", desc[0]);
+	if (desc[0] != 9)
+		fputs(" (WRONG!)", stdout);
+	putchar('\n');
+	printf("  bDescriptorType:     0x%02X", desc[1]);
+	if (desc[1] != 0x02)
+		fputs(" (WRONG!)", stdout);
+	putchar('\n');
+	printf("  wTotalLength:        %u\n", desc[2] | (desc[3] << 8));
+	printf("  bNumInterfaces:      %u\n", desc[4]);
+	printf("  bConfigurationValue: 0x%02X\n", desc[5]);
+	printf("  iConfiguration:      %u\n", desc[6]);
+	printf("  bmAttributes:        0x%02X\n", desc[7]);
+	printf("  bMaxPower:           0x%02X (%u mA)\n", desc[8], desc[8] * 2);
+}
+
 main(argc, argv)
 	char **argv;
 {
@@ -19,30 +65,9 @@
 	}
 	read_intel_hex(argv[1]);
 	printf("USB device descriptor at 0x3988:\n");
-	printf("  bLength:            %u", eeprom[0x388]);
-	if (eeprom[0x388] != 18)
-		fputs(" (WRONG!)", stdout);
-	putchar('\n');
-	printf("  bDescriptorType:    0x%02X", eeprom[0x389]);
-	if (eeprom[0x389] != 0x01)
-		fputs(" (WRONG!)", stdout);
-	putchar('\n');
-	printf("  bcdUSB:             0x%02X%02X\n",
-		eeprom[0x38B], eeprom[0x38A]);
-	printf("  bDeviceClass:       0x%02X\n", eeprom[0x38C]);
-	printf("  bDeviceSubClass:    0x%02X\n", eeprom[0x38D]);
-	printf("  bDeviceProtocol:    0x%02X\n", eeprom[0x38E]);
-	printf("  bMaxPacketSize0:    %u\n", eeprom[0x38F]);
-	printf("  idVendor:           0x%02X%02X\n",
-		eeprom[0x391], eeprom[0x390]);
-	printf("  idProduct:          0x%02X%02X\n",
-		eeprom[0x393], eeprom[0x392]);
-	printf("  bcdDevice:          0x%02X%02X\n",
-		eeprom[0x395], eeprom[0x394]);
-	printf("  iManufacturer:      %u\n", eeprom[0x396]);
-	printf("  iProduct:           %u\n", eeprom[0x397]);
-	printf("  iSerialNumber:      %u\n", eeprom[0x398]);
-	printf("  bNumConfigurations: %u\n", eeprom[0x399]);
+	print_device_desc(eeprom + 0x388);
+	printf("USB configuration descriptor at 0x399A:\n");
+	print_config_desc(eeprom + 0x39A);
 	/* decoding of other descriptors remains to be implemented */
 	exit(0);
 }