[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 |
|
---|
[632ed68] | 29 | /** @addtogroup usbinfo
|
---|
[07b9203e] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[632ed68] | 34 | * USB querying.
|
---|
[07b9203e] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
[3e6a98c5] | 41 | #include <stdbool.h>
|
---|
[07b9203e] | 42 |
|
---|
| 43 | #include <usb/usb.h>
|
---|
| 44 | #include <usb/descriptor.h>
|
---|
[466b120] | 45 | #include <usb/debug.h>
|
---|
[c9d5577] | 46 | #include <usb/classes/classes.h>
|
---|
[07b9203e] | 47 |
|
---|
| 48 | #include "usbinfo.h"
|
---|
[7d521e24] | 49 | #include <usb/dev/dp.h>
|
---|
[07b9203e] | 50 |
|
---|
| 51 | #define INDENT " "
|
---|
[2c5cefa] | 52 | #define BYTES_PER_LINE 12
|
---|
[07b9203e] | 53 |
|
---|
[5f635ca] | 54 |
|
---|
[b1c6e58] | 55 | const char *get_indent(size_t level)
|
---|
[6fe1ab4] | 56 | {
|
---|
| 57 | static const char *indents[] = {
|
---|
| 58 | INDENT,
|
---|
| 59 | INDENT INDENT,
|
---|
| 60 | INDENT INDENT INDENT,
|
---|
| 61 | INDENT INDENT INDENT INDENT,
|
---|
| 62 | INDENT INDENT INDENT INDENT INDENT
|
---|
| 63 | };
|
---|
| 64 | static size_t indents_count = sizeof(indents)/sizeof(indents[0]);
|
---|
| 65 | if (level >= indents_count) {
|
---|
| 66 | return indents[indents_count - 1];
|
---|
| 67 | }
|
---|
| 68 | return indents[level];
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void dump_buffer(const char *msg, size_t indent,
|
---|
| 72 | const uint8_t *buffer, size_t length)
|
---|
[2c5cefa] | 73 | {
|
---|
[5f635ca] | 74 | if (msg != NULL) {
|
---|
| 75 | printf("%s\n", msg);
|
---|
| 76 | }
|
---|
[2c5cefa] | 77 |
|
---|
| 78 | size_t i;
|
---|
[6fe1ab4] | 79 | if (length > 0) {
|
---|
| 80 | printf("%s", get_indent(indent));
|
---|
| 81 | }
|
---|
[2c5cefa] | 82 | for (i = 0; i < length; i++) {
|
---|
[6fe1ab4] | 83 | printf("0x%02X", buffer[i]);
|
---|
[2c5cefa] | 84 | if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
|
---|
| 85 | || (i + 1 == length)) {
|
---|
| 86 | printf("\n");
|
---|
[6fe1ab4] | 87 | if (i + 1 < length) {
|
---|
| 88 | printf("%s", get_indent(indent));
|
---|
| 89 | }
|
---|
| 90 | } else {
|
---|
| 91 | printf(" ");
|
---|
[2c5cefa] | 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[5f635ca] | 96 | void dump_usb_descriptor(uint8_t *descriptor, size_t size)
|
---|
[07b9203e] | 97 | {
|
---|
[a12917e] | 98 | printf("Device descriptor:\n");
|
---|
[466b120] | 99 | usb_dump_standard_descriptor(stdout, get_indent(0), "\n",
|
---|
| 100 | descriptor, size);
|
---|
[5f635ca] | 101 | }
|
---|
| 102 |
|
---|
[3100ebe] | 103 | void dump_match_ids(match_id_list_t *matches, const char *line_prefix)
|
---|
[5f635ca] | 104 | {
|
---|
[b72efe8] | 105 | list_foreach(matches->ids, link) {
|
---|
[5f635ca] | 106 | match_id_t *match = list_get_instance(link, match_id_t, link);
|
---|
| 107 |
|
---|
[7b13d8e] | 108 | printf("%s%3d %s\n", line_prefix, match->score, match->id);
|
---|
[5f635ca] | 109 | }
|
---|
[2c5cefa] | 110 | }
|
---|
| 111 |
|
---|
[8a121b1] | 112 | static void dump_tree_descriptor(const uint8_t *descriptor, size_t depth)
|
---|
[5ccb15c] | 113 | {
|
---|
| 114 | if (descriptor == NULL) {
|
---|
| 115 | return;
|
---|
| 116 | }
|
---|
[8a121b1] | 117 | int type = descriptor[1];
|
---|
[5ccb15c] | 118 | const char *name = "unknown";
|
---|
| 119 | switch (type) {
|
---|
| 120 | #define _TYPE(descriptor_type) \
|
---|
| 121 | case USB_DESCTYPE_##descriptor_type: name = #descriptor_type; break
|
---|
| 122 | _TYPE(DEVICE);
|
---|
| 123 | _TYPE(CONFIGURATION);
|
---|
| 124 | _TYPE(STRING);
|
---|
| 125 | _TYPE(INTERFACE);
|
---|
| 126 | _TYPE(ENDPOINT);
|
---|
| 127 | _TYPE(HID);
|
---|
| 128 | _TYPE(HID_REPORT);
|
---|
| 129 | _TYPE(HID_PHYSICAL);
|
---|
| 130 | _TYPE(HUB);
|
---|
| 131 | #undef _TYPE
|
---|
| 132 | }
|
---|
[6fe1ab4] | 133 | printf("%s%s (0x%02X):\n", get_indent(depth), name, type);
|
---|
[466b120] | 134 | usb_dump_standard_descriptor(stdout, get_indent(depth), "\n",
|
---|
| 135 | descriptor, descriptor[0]);
|
---|
[5ccb15c] | 136 | }
|
---|
| 137 |
|
---|
[8a121b1] | 138 | static void dump_tree_internal(
|
---|
| 139 | usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
|
---|
| 140 | const uint8_t *root, size_t depth)
|
---|
[5ccb15c] | 141 | {
|
---|
| 142 | if (root == NULL) {
|
---|
| 143 | return;
|
---|
| 144 | }
|
---|
| 145 | dump_tree_descriptor(root, depth);
|
---|
[8a121b1] | 146 | const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
|
---|
[5ccb15c] | 147 | do {
|
---|
| 148 | dump_tree_internal(parser, data, child, depth + 1);
|
---|
| 149 | child = usb_dp_get_sibling_descriptor(parser, data, root, child);
|
---|
| 150 | } while (child != NULL);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
|
---|
| 154 | {
|
---|
[7c95d6f5] | 155 | const uint8_t *ptr = data->data;
|
---|
[5ccb15c] | 156 | printf("Descriptor tree:\n");
|
---|
[6fe1ab4] | 157 | dump_tree_internal(parser, data, ptr, 0);
|
---|
[5ccb15c] | 158 | }
|
---|
| 159 |
|
---|
| 160 | #define NESTING(parentname, childname) \
|
---|
| 161 | { \
|
---|
| 162 | .child = USB_DESCTYPE_##childname, \
|
---|
| 163 | .parent = USB_DESCTYPE_##parentname, \
|
---|
| 164 | }
|
---|
| 165 | #define LAST_NESTING { -1, -1 }
|
---|
| 166 |
|
---|
| 167 | static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
|
---|
| 168 | NESTING(CONFIGURATION, INTERFACE),
|
---|
| 169 | NESTING(INTERFACE, ENDPOINT),
|
---|
| 170 | NESTING(INTERFACE, HUB),
|
---|
| 171 | NESTING(INTERFACE, HID),
|
---|
| 172 | NESTING(HID, HID_REPORT),
|
---|
| 173 | LAST_NESTING
|
---|
| 174 | };
|
---|
| 175 |
|
---|
| 176 | static usb_dp_parser_t parser = {
|
---|
| 177 | .nesting = descriptor_nesting
|
---|
| 178 | };
|
---|
| 179 |
|
---|
| 180 | void dump_descriptor_tree(uint8_t *descriptors, size_t length)
|
---|
| 181 | {
|
---|
| 182 | usb_dp_parser_data_t data = {
|
---|
| 183 | .data = descriptors,
|
---|
| 184 | .size = length,
|
---|
| 185 | .arg = NULL
|
---|
| 186 | };
|
---|
| 187 |
|
---|
| 188 | dump_tree(&parser, &data);
|
---|
| 189 | }
|
---|
[2c5cefa] | 190 |
|
---|
[07b9203e] | 191 | /** @}
|
---|
| 192 | */
|
---|