[466b120] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libusb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief Descriptor dumping.
|
---|
| 34 | */
|
---|
| 35 | #include <adt/list.h>
|
---|
| 36 | #include <fibril_synch.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <usb/debug.h>
|
---|
| 41 | #include <usb/descriptor.h>
|
---|
| 42 | #include <usb/classes/classes.h>
|
---|
| 43 | #include <usb/classes/hid.h>
|
---|
| 44 |
|
---|
| 45 | typedef struct {
|
---|
| 46 | int id;
|
---|
| 47 | void (*dump)(FILE *, const char *, const char *,
|
---|
| 48 | const uint8_t *, size_t);
|
---|
| 49 | } descriptor_dump_t;
|
---|
| 50 |
|
---|
| 51 | static void usb_dump_descriptor_device(FILE *, const char *, const char *,
|
---|
| 52 | const uint8_t *, size_t);
|
---|
| 53 | static void usb_dump_descriptor_configuration(FILE *, const char *, const char *,
|
---|
| 54 | const uint8_t *, size_t);
|
---|
| 55 | static void usb_dump_descriptor_interface(FILE *, const char *, const char *,
|
---|
| 56 | const uint8_t *, size_t);
|
---|
| 57 | static void usb_dump_descriptor_string(FILE *, const char *, const char *,
|
---|
| 58 | const uint8_t *, size_t);
|
---|
| 59 | static void usb_dump_descriptor_endpoint(FILE *, const char *, const char *,
|
---|
| 60 | const uint8_t *, size_t);
|
---|
| 61 | static void usb_dump_descriptor_hid(FILE *, const char *, const char *,
|
---|
| 62 | const uint8_t *, size_t);
|
---|
| 63 | static void usb_dump_descriptor_hub(FILE *, const char *, const char *,
|
---|
| 64 | const uint8_t *, size_t);
|
---|
| 65 | static void usb_dump_descriptor_generic(FILE *, const char *, const char *,
|
---|
| 66 | const uint8_t *, size_t);
|
---|
| 67 |
|
---|
| 68 | static descriptor_dump_t descriptor_dumpers[] = {
|
---|
| 69 | { USB_DESCTYPE_DEVICE, usb_dump_descriptor_device },
|
---|
| 70 | { USB_DESCTYPE_CONFIGURATION, usb_dump_descriptor_configuration },
|
---|
| 71 | { USB_DESCTYPE_STRING, usb_dump_descriptor_string },
|
---|
| 72 | { USB_DESCTYPE_INTERFACE, usb_dump_descriptor_interface },
|
---|
| 73 | { USB_DESCTYPE_ENDPOINT, usb_dump_descriptor_endpoint },
|
---|
| 74 | { USB_DESCTYPE_HID, usb_dump_descriptor_hid },
|
---|
| 75 | { USB_DESCTYPE_HUB, usb_dump_descriptor_hub },
|
---|
| 76 | { -1, usb_dump_descriptor_generic },
|
---|
| 77 | { -1, NULL }
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | /** Dumps standard USB descriptor.
|
---|
| 81 | * The @p line_suffix must contain the newline <code>\\n</code> character.
|
---|
| 82 | * When @p line_suffix or @p line_prefix is NULL, they are substitued with
|
---|
| 83 | * default values
|
---|
| 84 | * (<code> - </code> for prefix and line termination for suffix).
|
---|
| 85 | *
|
---|
| 86 | * @param output Output file stream to dump descriptor to.
|
---|
| 87 | * @param line_prefix Prefix for each line of output.
|
---|
| 88 | * @param line_suffix Suffix of each line of output.
|
---|
| 89 | * @param descriptor Actual descriptor.
|
---|
| 90 | * @param descriptor_length Descriptor size.
|
---|
| 91 | */
|
---|
| 92 | void usb_dump_standard_descriptor(FILE *output,
|
---|
| 93 | const char *line_prefix, const char *line_suffix,
|
---|
| 94 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 95 | {
|
---|
| 96 | if (descriptor_length < 2) {
|
---|
| 97 | return;
|
---|
| 98 | }
|
---|
| 99 | int type = descriptor[1];
|
---|
| 100 |
|
---|
| 101 | descriptor_dump_t *dumper = descriptor_dumpers;
|
---|
| 102 | while (dumper->dump != NULL) {
|
---|
| 103 | if ((dumper->id == type) || (dumper->id < 0)) {
|
---|
| 104 | dumper->dump(output, line_prefix, line_suffix,
|
---|
| 105 | descriptor, descriptor_length);
|
---|
| 106 | return;
|
---|
| 107 | }
|
---|
| 108 | dumper++;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /** Prints single line of USB descriptor dump.
|
---|
| 113 | * @warning This macro abuses heavily the naming conventions used
|
---|
| 114 | * by all dumping functions (i.e. names for output file stream (@c output) and
|
---|
| 115 | * line prefix and suffix (@c line_prefix and @c line_suffix respectively))-
|
---|
| 116 | *
|
---|
| 117 | * @param fmt Formatting string.
|
---|
| 118 | */
|
---|
| 119 | #define PRINTLINE(fmt, ...) \
|
---|
| 120 | fprintf(output, "%s" fmt "%s", \
|
---|
| 121 | line_prefix ? line_prefix : " - ", \
|
---|
| 122 | __VA_ARGS__, \
|
---|
| 123 | line_suffix ? line_suffix : "\n")
|
---|
| 124 |
|
---|
| 125 | #define BCD_INT(a) (((unsigned int)(a)) / 256)
|
---|
| 126 | #define BCD_FRAC(a) (((unsigned int)(a)) % 256)
|
---|
| 127 |
|
---|
| 128 | #define BCD_FMT "%x.%x"
|
---|
| 129 | #define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
|
---|
| 130 |
|
---|
| 131 | static void usb_dump_descriptor_device(FILE *output,
|
---|
| 132 | const char *line_prefix, const char *line_suffix,
|
---|
| 133 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 134 | {
|
---|
| 135 | usb_standard_device_descriptor_t *d
|
---|
| 136 | = (usb_standard_device_descriptor_t *) descriptor;
|
---|
| 137 | if (descriptor_length < sizeof(*d)) {
|
---|
| 138 | return;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | PRINTLINE("bLength = %d", d->length);
|
---|
| 142 | PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
|
---|
| 143 | PRINTLINE("bcdUSB = %d (" BCD_FMT ")", d->usb_spec_version,
|
---|
| 144 | BCD_ARGS(d->usb_spec_version));
|
---|
| 145 | PRINTLINE("bDeviceClass = 0x%02x", d->device_class);
|
---|
| 146 | PRINTLINE("bDeviceSubClass = 0x%02x", d->device_subclass);
|
---|
| 147 | PRINTLINE("bDeviceProtocol = 0x%02x", d->device_protocol);
|
---|
| 148 | PRINTLINE("bMaxPacketSize0 = %d", d->max_packet_size);
|
---|
[0d36c20] | 149 | PRINTLINE("idVendor = 0x%04x", d->vendor_id);
|
---|
| 150 | PRINTLINE("idProduct = 0x%04x", d->product_id);
|
---|
[466b120] | 151 | PRINTLINE("bcdDevice = %d", d->device_version);
|
---|
| 152 | PRINTLINE("iManufacturer = %d", d->str_manufacturer);
|
---|
| 153 | PRINTLINE("iProduct = %d", d->str_product);
|
---|
| 154 | PRINTLINE("iSerialNumber = %d", d->str_serial_number);
|
---|
| 155 | PRINTLINE("bNumConfigurations = %d", d->configuration_count);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | static void usb_dump_descriptor_configuration(FILE *output,
|
---|
| 159 | const char *line_prefix, const char *line_suffix,
|
---|
| 160 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 161 | {
|
---|
| 162 | usb_standard_configuration_descriptor_t *d
|
---|
| 163 | = (usb_standard_configuration_descriptor_t *) descriptor;
|
---|
| 164 | if (descriptor_length < sizeof(*d)) {
|
---|
| 165 | return;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | bool self_powered = d->attributes & 64;
|
---|
| 169 | bool remote_wakeup = d->attributes & 32;
|
---|
| 170 |
|
---|
| 171 | PRINTLINE("bLength = %d", d->length);
|
---|
| 172 | PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
|
---|
| 173 | PRINTLINE("wTotalLength = %d", d->total_length);
|
---|
| 174 | PRINTLINE("bNumInterfaces = %d", d->interface_count);
|
---|
| 175 | PRINTLINE("bConfigurationValue = %d", d->configuration_number);
|
---|
| 176 | PRINTLINE("iConfiguration = %d", d->str_configuration);
|
---|
| 177 | PRINTLINE("bmAttributes = %d [%s%s%s]", d->attributes,
|
---|
| 178 | self_powered ? "self-powered" : "",
|
---|
| 179 | (self_powered & remote_wakeup) ? ", " : "",
|
---|
| 180 | remote_wakeup ? "remote-wakeup" : "");
|
---|
| 181 | PRINTLINE("MaxPower = %d (%dmA)", d->max_power,
|
---|
| 182 | 2 * d->max_power);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | static void usb_dump_descriptor_interface(FILE *output,
|
---|
| 186 | const char *line_prefix, const char *line_suffix,
|
---|
| 187 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 188 | {
|
---|
| 189 | usb_standard_interface_descriptor_t *d
|
---|
| 190 | = (usb_standard_interface_descriptor_t *) descriptor;
|
---|
| 191 | if (descriptor_length < sizeof(*d)) {
|
---|
| 192 | return;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | PRINTLINE("bLength = %d", d->length);
|
---|
| 196 | PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
|
---|
| 197 | PRINTLINE("bInterfaceNumber = %d", d->interface_number);
|
---|
| 198 | PRINTLINE("bAlternateSetting = %d", d->alternate_setting);
|
---|
| 199 | PRINTLINE("bNumEndpoints = %d", d->endpoint_count);
|
---|
| 200 | PRINTLINE("bInterfaceClass = %s", d->interface_class == 0
|
---|
| 201 | ? "reserved (0)" : usb_str_class(d->interface_class));
|
---|
| 202 | PRINTLINE("bInterfaceSubClass = %d", d->interface_subclass);
|
---|
| 203 | PRINTLINE("bInterfaceProtocol = %d", d->interface_protocol);
|
---|
| 204 | PRINTLINE("iInterface = %d", d->str_interface);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | static void usb_dump_descriptor_string(FILE *output,
|
---|
| 208 | const char *line_prefix, const char *line_suffix,
|
---|
| 209 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 210 | {
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | static void usb_dump_descriptor_endpoint(FILE *output,
|
---|
| 214 | const char *line_prefix, const char *line_suffix,
|
---|
| 215 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 216 | {
|
---|
| 217 | usb_standard_endpoint_descriptor_t *d
|
---|
| 218 | = (usb_standard_endpoint_descriptor_t *) descriptor;
|
---|
| 219 | if (descriptor_length < sizeof(*d)) {
|
---|
| 220 | return;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | int endpoint = d->endpoint_address & 15;
|
---|
| 224 | usb_direction_t direction = d->endpoint_address & 128
|
---|
| 225 | ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
|
---|
| 226 | usb_transfer_type_t transfer_type = d->attributes & 3;
|
---|
| 227 |
|
---|
| 228 | PRINTLINE("bLength = %d", d->length);
|
---|
| 229 | PRINTLINE("bDescriptorType = 0x%02X", d->descriptor_type);
|
---|
| 230 | PRINTLINE("bEndpointAddress = 0x%02X [%d, %s]",
|
---|
| 231 | d->endpoint_address, endpoint,
|
---|
| 232 | direction == USB_DIRECTION_IN ? "in" : "out");
|
---|
| 233 | PRINTLINE("bmAttributes = %d [%s]", d->attributes,
|
---|
| 234 | usb_str_transfer_type(transfer_type));
|
---|
| 235 | PRINTLINE("wMaxPacketSize = %d", d->max_packet_size);
|
---|
| 236 | PRINTLINE("bInterval = %dms", d->poll_interval);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | static void usb_dump_descriptor_hid(FILE *output,
|
---|
| 240 | const char *line_prefix, const char *line_suffix,
|
---|
| 241 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 242 | {
|
---|
| 243 | usb_standard_hid_descriptor_t *d
|
---|
| 244 | = (usb_standard_hid_descriptor_t *) descriptor;
|
---|
| 245 | if (descriptor_length < sizeof(*d)) {
|
---|
| 246 | return;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | PRINTLINE("bLength = %d", d->length);
|
---|
| 250 | PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
|
---|
| 251 | PRINTLINE("bcdHID = %d (" BCD_FMT ")", d->spec_release,
|
---|
| 252 | BCD_ARGS(d->spec_release));
|
---|
| 253 | PRINTLINE("bCountryCode = %d", d->country_code);
|
---|
| 254 | PRINTLINE("bNumDescriptors = %d", d->class_desc_count);
|
---|
| 255 | PRINTLINE("bDescriptorType = %d", d->report_desc_info.type);
|
---|
| 256 | PRINTLINE("wDescriptorLength = %d", d->report_desc_info.length);
|
---|
| 257 |
|
---|
| 258 | /* Print info about report descriptors. */
|
---|
| 259 | size_t i;
|
---|
| 260 | size_t count = (descriptor_length - sizeof(*d))
|
---|
| 261 | / sizeof(usb_standard_hid_class_descriptor_info_t);
|
---|
| 262 | usb_standard_hid_class_descriptor_info_t *d2
|
---|
| 263 | = (usb_standard_hid_class_descriptor_info_t *)
|
---|
| 264 | (descriptor + sizeof(*d));
|
---|
| 265 | for (i = 0; i < count; i++, d2++) {
|
---|
| 266 | PRINTLINE("bDescriptorType = %d", d2->type);
|
---|
| 267 | PRINTLINE("wDescriptorLength = %d", d2->length);
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | static void usb_dump_descriptor_hub(FILE *output,
|
---|
| 272 | const char *line_prefix, const char *line_suffix,
|
---|
| 273 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 274 | {
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | static void usb_dump_descriptor_generic(FILE *output,
|
---|
| 278 | const char *line_prefix, const char *line_suffix,
|
---|
| 279 | const uint8_t *descriptor, size_t descriptor_length)
|
---|
| 280 | {
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 |
|
---|
| 284 | /**
|
---|
| 285 | * @}
|
---|
| 286 | */
|
---|