[07b9203e] | 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"
|
---|
[5ccb15c] | 47 | #include <usb/dp.h>
|
---|
[07b9203e] | 48 |
|
---|
| 49 | #define INDENT " "
|
---|
[6fe1ab4] | 50 | #define PRINTLINE(indent, fmt, ...) printf("%s" fmt, get_indent(indent), __VA_ARGS__)
|
---|
[2c5cefa] | 51 | #define BYTES_PER_LINE 12
|
---|
[07b9203e] | 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 |
|
---|
[5f635ca] | 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 |
|
---|
[6fe1ab4] | 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)
|
---|
[2c5cefa] | 105 | {
|
---|
[5f635ca] | 106 | if (msg != NULL) {
|
---|
| 107 | printf("%s\n", msg);
|
---|
| 108 | }
|
---|
[2c5cefa] | 109 |
|
---|
| 110 | size_t i;
|
---|
[6fe1ab4] | 111 | if (length > 0) {
|
---|
| 112 | printf("%s", get_indent(indent));
|
---|
| 113 | }
|
---|
[2c5cefa] | 114 | for (i = 0; i < length; i++) {
|
---|
[6fe1ab4] | 115 | printf("0x%02X", buffer[i]);
|
---|
[2c5cefa] | 116 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
---|
| 117 | || (i + 1 == length)) {
|
---|
| 118 | printf("\n");
|
---|
[6fe1ab4] | 119 | if (i + 1 < length) {
|
---|
| 120 | printf("%s", get_indent(indent));
|
---|
| 121 | }
|
---|
| 122 | } else {
|
---|
| 123 | printf(" ");
|
---|
[2c5cefa] | 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[5f635ca] | 128 | void dump_descriptor_by_type(size_t indent, uint8_t *d, size_t length)
|
---|
[03e02248] | 129 | {
|
---|
[5f635ca] | 130 | if (length < 2) {
|
---|
| 131 | return;
|
---|
[03e02248] | 132 | }
|
---|
[5f635ca] | 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 | }
|
---|
[03e02248] | 143 | }
|
---|
| 144 |
|
---|
[5f635ca] | 145 | void dump_usb_descriptor(uint8_t *descriptor, size_t size)
|
---|
[07b9203e] | 146 | {
|
---|
[5f635ca] | 147 | dump_descriptor_by_type(0, descriptor, size);
|
---|
| 148 | }
|
---|
[07b9203e] | 149 |
|
---|
[5f635ca] | 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 |
|
---|
[6fe1ab4] | 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,
|
---|
[07b9203e] | 161 | BCD_ARGS(d->usb_spec_version));
|
---|
[6fe1ab4] | 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);
|
---|
[07b9203e] | 173 | }
|
---|
| 174 |
|
---|
[5f635ca] | 175 | void dump_descriptor_configuration(size_t indent, uint8_t *descr, size_t size)
|
---|
[2c5cefa] | 176 | {
|
---|
[5f635ca] | 177 | usb_standard_configuration_descriptor_t *d
|
---|
| 178 | = (usb_standard_configuration_descriptor_t *) descr;
|
---|
| 179 | if (size != sizeof(*d)) {
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[2c5cefa] | 183 | bool self_powered = d->attributes & 64;
|
---|
| 184 | bool remote_wakeup = d->attributes & 32;
|
---|
| 185 |
|
---|
[6fe1ab4] | 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,
|
---|
[2c5cefa] | 193 | self_powered ? "self-powered" : "",
|
---|
| 194 | (self_powered & remote_wakeup) ? ", " : "",
|
---|
| 195 | remote_wakeup ? "remote-wakeup" : "");
|
---|
[6fe1ab4] | 196 | PRINTLINE(indent, "MaxPower = %d (%dmA)\n", d->max_power,
|
---|
[2c5cefa] | 197 | 2 * d->max_power);
|
---|
[5f635ca] | 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 | dump_descriptor_generic(indent, descr, size);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | void dump_descriptor_hid(size_t indent, uint8_t *descr, size_t size)
|
---|
| 216 | {
|
---|
| 217 | dump_descriptor_generic(indent, descr, size);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | void dump_descriptor_hub(size_t indent, uint8_t *descr, size_t size)
|
---|
| 221 | {
|
---|
| 222 | dump_descriptor_generic(indent, descr, size);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | void dump_descriptor_generic(size_t indent, uint8_t *descr, size_t size)
|
---|
| 226 | {
|
---|
[6fe1ab4] | 227 | dump_buffer(NULL, indent, descr, size);
|
---|
[5f635ca] | 228 | }
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | void dump_match_ids(match_id_list_t *matches)
|
---|
| 232 | {
|
---|
| 233 | printf("Match ids:\n");
|
---|
| 234 | link_t *link;
|
---|
| 235 | for (link = matches->ids.next;
|
---|
| 236 | link != &matches->ids;
|
---|
| 237 | link = link->next) {
|
---|
| 238 | match_id_t *match = list_get_instance(link, match_id_t, link);
|
---|
| 239 |
|
---|
| 240 | printf(INDENT "%d %s\n", match->score, match->id);
|
---|
| 241 | }
|
---|
[2c5cefa] | 242 | }
|
---|
| 243 |
|
---|
[5ccb15c] | 244 | static void dump_tree_descriptor(uint8_t *descriptor, size_t depth)
|
---|
| 245 | {
|
---|
| 246 | if (descriptor == NULL) {
|
---|
| 247 | return;
|
---|
| 248 | }
|
---|
| 249 | int type = (int) *(descriptor + 1);
|
---|
| 250 | const char *name = "unknown";
|
---|
| 251 | switch (type) {
|
---|
| 252 | #define _TYPE(descriptor_type) \
|
---|
| 253 | case USB_DESCTYPE_##descriptor_type: name = #descriptor_type; break
|
---|
| 254 | _TYPE(DEVICE);
|
---|
| 255 | _TYPE(CONFIGURATION);
|
---|
| 256 | _TYPE(STRING);
|
---|
| 257 | _TYPE(INTERFACE);
|
---|
| 258 | _TYPE(ENDPOINT);
|
---|
| 259 | _TYPE(HID);
|
---|
| 260 | _TYPE(HID_REPORT);
|
---|
| 261 | _TYPE(HID_PHYSICAL);
|
---|
| 262 | _TYPE(HUB);
|
---|
| 263 | #undef _TYPE
|
---|
| 264 | }
|
---|
[6fe1ab4] | 265 | printf("%s%s (0x%02X):\n", get_indent(depth), name, type);
|
---|
[5f635ca] | 266 | dump_descriptor_by_type(depth, descriptor, descriptor[0]);
|
---|
| 267 |
|
---|
[5ccb15c] | 268 | }
|
---|
| 269 |
|
---|
| 270 | static void dump_tree_internal(usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
|
---|
| 271 | uint8_t *root, size_t depth)
|
---|
| 272 | {
|
---|
| 273 | if (root == NULL) {
|
---|
| 274 | return;
|
---|
| 275 | }
|
---|
| 276 | dump_tree_descriptor(root, depth);
|
---|
| 277 | uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
|
---|
| 278 | do {
|
---|
| 279 | dump_tree_internal(parser, data, child, depth + 1);
|
---|
| 280 | child = usb_dp_get_sibling_descriptor(parser, data, root, child);
|
---|
| 281 | } while (child != NULL);
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
|
---|
| 285 | {
|
---|
| 286 | uint8_t *ptr = data->data;
|
---|
| 287 | printf("Descriptor tree:\n");
|
---|
[6fe1ab4] | 288 | dump_tree_internal(parser, data, ptr, 0);
|
---|
[5ccb15c] | 289 | }
|
---|
| 290 |
|
---|
| 291 | #define NESTING(parentname, childname) \
|
---|
| 292 | { \
|
---|
| 293 | .child = USB_DESCTYPE_##childname, \
|
---|
| 294 | .parent = USB_DESCTYPE_##parentname, \
|
---|
| 295 | }
|
---|
| 296 | #define LAST_NESTING { -1, -1 }
|
---|
| 297 |
|
---|
| 298 | static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
|
---|
| 299 | NESTING(CONFIGURATION, INTERFACE),
|
---|
| 300 | NESTING(INTERFACE, ENDPOINT),
|
---|
| 301 | NESTING(INTERFACE, HUB),
|
---|
| 302 | NESTING(INTERFACE, HID),
|
---|
| 303 | NESTING(HID, HID_REPORT),
|
---|
| 304 | LAST_NESTING
|
---|
| 305 | };
|
---|
| 306 |
|
---|
| 307 | static usb_dp_parser_t parser = {
|
---|
| 308 | .nesting = descriptor_nesting
|
---|
| 309 | };
|
---|
| 310 |
|
---|
| 311 | void dump_descriptor_tree(uint8_t *descriptors, size_t length)
|
---|
| 312 | {
|
---|
| 313 | usb_dp_parser_data_t data = {
|
---|
| 314 | .data = descriptors,
|
---|
| 315 | .size = length,
|
---|
| 316 | .arg = NULL
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | dump_tree(&parser, &data);
|
---|
| 320 | }
|
---|
[2c5cefa] | 321 |
|
---|
[07b9203e] | 322 | /** @}
|
---|
| 323 | */
|
---|