| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 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 usb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief USB querying.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <bool.h>
|
|---|
| 42 |
|
|---|
| 43 | #include <usb/usb.h>
|
|---|
| 44 | #include <usb/descriptor.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "usbinfo.h"
|
|---|
| 47 | #include <usb/dp.h>
|
|---|
| 48 |
|
|---|
| 49 | #define INDENT " "
|
|---|
| 50 | #define PRINTLINE(indent, fmt, ...) printf("%s" fmt, get_indent(indent), __VA_ARGS__)
|
|---|
| 51 | #define BYTES_PER_LINE 12
|
|---|
| 52 |
|
|---|
| 53 | #define BCD_INT(a) (((unsigned int)(a)) / 256)
|
|---|
| 54 | #define BCD_FRAC(a) (((unsigned int)(a)) % 256)
|
|---|
| 55 |
|
|---|
| 56 | #define BCD_FMT "%x.%x"
|
|---|
| 57 | #define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
|
|---|
| 58 |
|
|---|
| 59 | static void dump_descriptor_by_type(size_t, uint8_t *, size_t);
|
|---|
| 60 |
|
|---|
| 61 | typedef struct {
|
|---|
| 62 | int descriptor;
|
|---|
| 63 | void (*dump)(size_t indent, uint8_t *descriptor, size_t length);
|
|---|
| 64 | } descriptor_dump_t;
|
|---|
| 65 |
|
|---|
| 66 | static void dump_descriptor_device(size_t, uint8_t *, size_t);
|
|---|
| 67 | static void dump_descriptor_configuration(size_t, uint8_t *, size_t);
|
|---|
| 68 | static void dump_descriptor_interface(size_t, uint8_t *, size_t);
|
|---|
| 69 | static void dump_descriptor_string(size_t, uint8_t *, size_t);
|
|---|
| 70 | static void dump_descriptor_endpoint(size_t, uint8_t *, size_t);
|
|---|
| 71 | static void dump_descriptor_hid(size_t, uint8_t *, size_t);
|
|---|
| 72 | static void dump_descriptor_hub(size_t, uint8_t *, size_t);
|
|---|
| 73 | static void dump_descriptor_generic(size_t, uint8_t *, size_t);
|
|---|
| 74 |
|
|---|
| 75 | static descriptor_dump_t descriptor_dumpers[] = {
|
|---|
| 76 | { USB_DESCTYPE_DEVICE, dump_descriptor_device },
|
|---|
| 77 | { USB_DESCTYPE_CONFIGURATION, dump_descriptor_configuration },
|
|---|
| 78 | { USB_DESCTYPE_STRING, dump_descriptor_string },
|
|---|
| 79 | { USB_DESCTYPE_INTERFACE, dump_descriptor_interface },
|
|---|
| 80 | { USB_DESCTYPE_ENDPOINT, dump_descriptor_endpoint },
|
|---|
| 81 | { USB_DESCTYPE_HID, dump_descriptor_hid },
|
|---|
| 82 | { USB_DESCTYPE_HUB, dump_descriptor_hub },
|
|---|
| 83 | { -1, dump_descriptor_generic },
|
|---|
| 84 | { -1, NULL }
|
|---|
| 85 | };
|
|---|
| 86 |
|
|---|
| 87 | static const char *get_indent(size_t level)
|
|---|
| 88 | {
|
|---|
| 89 | static const char *indents[] = {
|
|---|
| 90 | INDENT,
|
|---|
| 91 | INDENT INDENT,
|
|---|
| 92 | INDENT INDENT INDENT,
|
|---|
| 93 | INDENT INDENT INDENT INDENT,
|
|---|
| 94 | INDENT INDENT INDENT INDENT INDENT
|
|---|
| 95 | };
|
|---|
| 96 | static size_t indents_count = sizeof(indents)/sizeof(indents[0]);
|
|---|
| 97 | if (level >= indents_count) {
|
|---|
| 98 | return indents[indents_count - 1];
|
|---|
| 99 | }
|
|---|
| 100 | return indents[level];
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void dump_buffer(const char *msg, size_t indent,
|
|---|
| 104 | const uint8_t *buffer, size_t length)
|
|---|
| 105 | {
|
|---|
| 106 | if (msg != NULL) {
|
|---|
| 107 | printf("%s\n", msg);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | size_t i;
|
|---|
| 111 | if (length > 0) {
|
|---|
| 112 | printf("%s", get_indent(indent));
|
|---|
| 113 | }
|
|---|
| 114 | for (i = 0; i < length; i++) {
|
|---|
| 115 | printf("0x%02X", buffer[i]);
|
|---|
| 116 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
|---|
| 117 | || (i + 1 == length)) {
|
|---|
| 118 | printf("\n");
|
|---|
| 119 | if (i + 1 < length) {
|
|---|
| 120 | printf("%s", get_indent(indent));
|
|---|
| 121 | }
|
|---|
| 122 | } else {
|
|---|
| 123 | printf(" ");
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | void dump_descriptor_by_type(size_t indent, uint8_t *d, size_t length)
|
|---|
| 129 | {
|
|---|
| 130 | if (length < 2) {
|
|---|
| 131 | return;
|
|---|
| 132 | }
|
|---|
| 133 | int type = d[1];
|
|---|
| 134 |
|
|---|
| 135 | descriptor_dump_t *dumper = descriptor_dumpers;
|
|---|
| 136 | while (dumper->dump != NULL) {
|
|---|
| 137 | if ((dumper->descriptor == type) || (dumper->descriptor < 0)) {
|
|---|
| 138 | dumper->dump(indent, d, length);
|
|---|
| 139 | return;
|
|---|
| 140 | }
|
|---|
| 141 | dumper++;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | void dump_usb_descriptor(uint8_t *descriptor, size_t size)
|
|---|
| 146 | {
|
|---|
| 147 | dump_descriptor_by_type(0, descriptor, size);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | void dump_descriptor_device(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 151 | {
|
|---|
| 152 | usb_standard_device_descriptor_t *d
|
|---|
| 153 | = (usb_standard_device_descriptor_t *) descr;
|
|---|
| 154 | if (size != sizeof(*d)) {
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | PRINTLINE(indent, "bLength = %d\n", d->length);
|
|---|
| 159 | PRINTLINE(indent, "bDescriptorType = 0x%02x\n", d->descriptor_type);
|
|---|
| 160 | PRINTLINE(indent, "bcdUSB = %d (" BCD_FMT ")\n", d->usb_spec_version,
|
|---|
| 161 | BCD_ARGS(d->usb_spec_version));
|
|---|
| 162 | PRINTLINE(indent, "bDeviceClass = 0x%02x\n", d->device_class);
|
|---|
| 163 | PRINTLINE(indent, "bDeviceSubClass = 0x%02x\n", d->device_subclass);
|
|---|
| 164 | PRINTLINE(indent, "bDeviceProtocol = 0x%02x\n", d->device_protocol);
|
|---|
| 165 | PRINTLINE(indent, "bMaxPacketSize0 = %d\n", d->max_packet_size);
|
|---|
| 166 | PRINTLINE(indent, "idVendor = %d\n", d->vendor_id);
|
|---|
| 167 | PRINTLINE(indent, "idProduct = %d\n", d->product_id);
|
|---|
| 168 | PRINTLINE(indent, "bcdDevice = %d\n", d->device_version);
|
|---|
| 169 | PRINTLINE(indent, "iManufacturer = %d\n", d->str_manufacturer);
|
|---|
| 170 | PRINTLINE(indent, "iProduct = %d\n", d->str_product);
|
|---|
| 171 | PRINTLINE(indent, "iSerialNumber = %d\n", d->str_serial_number);
|
|---|
| 172 | PRINTLINE(indent, "bNumConfigurations = %d\n", d->configuration_count);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | void dump_descriptor_configuration(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 176 | {
|
|---|
| 177 | usb_standard_configuration_descriptor_t *d
|
|---|
| 178 | = (usb_standard_configuration_descriptor_t *) descr;
|
|---|
| 179 | if (size != sizeof(*d)) {
|
|---|
| 180 | return;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | bool self_powered = d->attributes & 64;
|
|---|
| 184 | bool remote_wakeup = d->attributes & 32;
|
|---|
| 185 |
|
|---|
| 186 | PRINTLINE(indent, "bLength = %d\n", d->length);
|
|---|
| 187 | PRINTLINE(indent, "bDescriptorType = 0x%02x\n", d->descriptor_type);
|
|---|
| 188 | PRINTLINE(indent, "wTotalLength = %d\n", d->total_length);
|
|---|
| 189 | PRINTLINE(indent, "bNumInterfaces = %d\n", d->interface_count);
|
|---|
| 190 | PRINTLINE(indent, "bConfigurationValue = %d\n", d->configuration_number);
|
|---|
| 191 | PRINTLINE(indent, "iConfiguration = %d\n", d->str_configuration);
|
|---|
| 192 | PRINTLINE(indent, "bmAttributes = %d [%s%s%s]\n", d->attributes,
|
|---|
| 193 | self_powered ? "self-powered" : "",
|
|---|
| 194 | (self_powered & remote_wakeup) ? ", " : "",
|
|---|
| 195 | remote_wakeup ? "remote-wakeup" : "");
|
|---|
| 196 | PRINTLINE(indent, "MaxPower = %d (%dmA)\n", d->max_power,
|
|---|
| 197 | 2 * d->max_power);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | void dump_descriptor_interface(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 201 | {
|
|---|
| 202 | dump_descriptor_generic(indent, descr, size);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | void dump_descriptor_string(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 206 | {
|
|---|
| 207 | dump_descriptor_generic(indent, descr, size);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void dump_descriptor_endpoint(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 211 | {
|
|---|
| 212 | usb_standard_endpoint_descriptor_t *d
|
|---|
| 213 | = (usb_standard_endpoint_descriptor_t *) descr;
|
|---|
| 214 |
|
|---|
| 215 | int endpoint = d->endpoint_address & 15;
|
|---|
| 216 | usb_direction_t direction = d->endpoint_address & 128
|
|---|
| 217 | ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
|
|---|
| 218 |
|
|---|
| 219 | PRINTLINE(indent, "bLength = %d\n", d->length);
|
|---|
| 220 | PRINTLINE(indent, "bDescriptorType = 0x%02X\n", d->descriptor_type);
|
|---|
| 221 | PRINTLINE(indent, "bEndpointAddress = 0x%02X [%d, %s]\n",
|
|---|
| 222 | d->endpoint_address, endpoint,
|
|---|
| 223 | direction == USB_DIRECTION_IN ? "in" : "out");
|
|---|
| 224 | PRINTLINE(indent, "bmAttributes = %d\n", d->attributes);
|
|---|
| 225 | PRINTLINE(indent, "wMaxPacketSize = %d\n", d->max_packet_size);
|
|---|
| 226 | PRINTLINE(indent, "bInterval = %dms\n", d->poll_interval);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void dump_descriptor_hid(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 230 | {
|
|---|
| 231 | dump_descriptor_generic(indent, descr, size);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | void dump_descriptor_hub(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 235 | {
|
|---|
| 236 | dump_descriptor_generic(indent, descr, size);
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | void dump_descriptor_generic(size_t indent, uint8_t *descr, size_t size)
|
|---|
| 240 | {
|
|---|
| 241 | dump_buffer(NULL, indent, descr, size);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 | void dump_match_ids(match_id_list_t *matches)
|
|---|
| 246 | {
|
|---|
| 247 | printf("Match ids:\n");
|
|---|
| 248 | link_t *link;
|
|---|
| 249 | for (link = matches->ids.next;
|
|---|
| 250 | link != &matches->ids;
|
|---|
| 251 | link = link->next) {
|
|---|
| 252 | match_id_t *match = list_get_instance(link, match_id_t, link);
|
|---|
| 253 |
|
|---|
| 254 | printf(INDENT "%d %s\n", match->score, match->id);
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | static void dump_tree_descriptor(uint8_t *descriptor, size_t depth)
|
|---|
| 259 | {
|
|---|
| 260 | if (descriptor == NULL) {
|
|---|
| 261 | return;
|
|---|
| 262 | }
|
|---|
| 263 | int type = (int) *(descriptor + 1);
|
|---|
| 264 | const char *name = "unknown";
|
|---|
| 265 | switch (type) {
|
|---|
| 266 | #define _TYPE(descriptor_type) \
|
|---|
| 267 | case USB_DESCTYPE_##descriptor_type: name = #descriptor_type; break
|
|---|
| 268 | _TYPE(DEVICE);
|
|---|
| 269 | _TYPE(CONFIGURATION);
|
|---|
| 270 | _TYPE(STRING);
|
|---|
| 271 | _TYPE(INTERFACE);
|
|---|
| 272 | _TYPE(ENDPOINT);
|
|---|
| 273 | _TYPE(HID);
|
|---|
| 274 | _TYPE(HID_REPORT);
|
|---|
| 275 | _TYPE(HID_PHYSICAL);
|
|---|
| 276 | _TYPE(HUB);
|
|---|
| 277 | #undef _TYPE
|
|---|
| 278 | }
|
|---|
| 279 | printf("%s%s (0x%02X):\n", get_indent(depth), name, type);
|
|---|
| 280 | dump_descriptor_by_type(depth, descriptor, descriptor[0]);
|
|---|
| 281 |
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | static void dump_tree_internal(usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
|
|---|
| 285 | uint8_t *root, size_t depth)
|
|---|
| 286 | {
|
|---|
| 287 | if (root == NULL) {
|
|---|
| 288 | return;
|
|---|
| 289 | }
|
|---|
| 290 | dump_tree_descriptor(root, depth);
|
|---|
| 291 | uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
|
|---|
| 292 | do {
|
|---|
| 293 | dump_tree_internal(parser, data, child, depth + 1);
|
|---|
| 294 | child = usb_dp_get_sibling_descriptor(parser, data, root, child);
|
|---|
| 295 | } while (child != NULL);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
|
|---|
| 299 | {
|
|---|
| 300 | uint8_t *ptr = data->data;
|
|---|
| 301 | printf("Descriptor tree:\n");
|
|---|
| 302 | dump_tree_internal(parser, data, ptr, 0);
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | #define NESTING(parentname, childname) \
|
|---|
| 306 | { \
|
|---|
| 307 | .child = USB_DESCTYPE_##childname, \
|
|---|
| 308 | .parent = USB_DESCTYPE_##parentname, \
|
|---|
| 309 | }
|
|---|
| 310 | #define LAST_NESTING { -1, -1 }
|
|---|
| 311 |
|
|---|
| 312 | static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
|
|---|
| 313 | NESTING(CONFIGURATION, INTERFACE),
|
|---|
| 314 | NESTING(INTERFACE, ENDPOINT),
|
|---|
| 315 | NESTING(INTERFACE, HUB),
|
|---|
| 316 | NESTING(INTERFACE, HID),
|
|---|
| 317 | NESTING(HID, HID_REPORT),
|
|---|
| 318 | LAST_NESTING
|
|---|
| 319 | };
|
|---|
| 320 |
|
|---|
| 321 | static usb_dp_parser_t parser = {
|
|---|
| 322 | .nesting = descriptor_nesting
|
|---|
| 323 | };
|
|---|
| 324 |
|
|---|
| 325 | void dump_descriptor_tree(uint8_t *descriptors, size_t length)
|
|---|
| 326 | {
|
|---|
| 327 | usb_dp_parser_data_t data = {
|
|---|
| 328 | .data = descriptors,
|
|---|
| 329 | .size = length,
|
|---|
| 330 | .arg = NULL
|
|---|
| 331 | };
|
|---|
| 332 |
|
|---|
| 333 | dump_tree(&parser, &data);
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /** @}
|
|---|
| 337 | */
|
|---|