| 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 usbinfo
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Dumping of generic device properties.
|
|---|
| 35 | */
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <usb/pipes.h>
|
|---|
| 40 | #include <usb/recognise.h>
|
|---|
| 41 | #include <usb/request.h>
|
|---|
| 42 | #include <usb/classes/classes.h>
|
|---|
| 43 | #include <usb/classes/hid.h>
|
|---|
| 44 | #include <usb/classes/hub.h>
|
|---|
| 45 | #include "usbinfo.h"
|
|---|
| 46 |
|
|---|
| 47 | void dump_short_device_identification(usbinfo_device_t *dev)
|
|---|
| 48 | {
|
|---|
| 49 | printf("%sDevice 0x%04x by vendor 0x%04x\n", get_indent(0),
|
|---|
| 50 | (int) dev->device_descriptor.product_id,
|
|---|
| 51 | (int) dev->device_descriptor.vendor_id);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | static void dump_match_ids_from_interface(uint8_t *descriptor, size_t depth,
|
|---|
| 55 | void *arg)
|
|---|
| 56 | {
|
|---|
| 57 | if (depth != 1) {
|
|---|
| 58 | return;
|
|---|
| 59 | }
|
|---|
| 60 | size_t descr_size = descriptor[0];
|
|---|
| 61 | if (descr_size < sizeof(usb_standard_interface_descriptor_t)) {
|
|---|
| 62 | return;
|
|---|
| 63 | }
|
|---|
| 64 | int descr_type = descriptor[1];
|
|---|
| 65 | if (descr_type != USB_DESCTYPE_INTERFACE) {
|
|---|
| 66 | return;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | usbinfo_device_t *dev = (usbinfo_device_t *) arg;
|
|---|
| 70 |
|
|---|
| 71 | usb_standard_interface_descriptor_t *iface
|
|---|
| 72 | = (usb_standard_interface_descriptor_t *) descriptor;
|
|---|
| 73 |
|
|---|
| 74 | printf("%sInterface #%d match ids (%s, 0x%02x, 0x%02x)\n",
|
|---|
| 75 | get_indent(0),
|
|---|
| 76 | (int) iface->interface_number,
|
|---|
| 77 | usb_str_class(iface->interface_class),
|
|---|
| 78 | (int) iface->interface_subclass,
|
|---|
| 79 | (int) iface->interface_protocol);
|
|---|
| 80 |
|
|---|
| 81 | match_id_list_t matches;
|
|---|
| 82 | init_match_ids(&matches);
|
|---|
| 83 | usb_device_create_match_ids_from_interface(&dev->device_descriptor,
|
|---|
| 84 | iface, &matches);
|
|---|
| 85 | dump_match_ids(&matches, get_indent(1));
|
|---|
| 86 | clean_match_ids(&matches);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | void dump_device_match_ids(usbinfo_device_t *dev)
|
|---|
| 90 | {
|
|---|
| 91 | match_id_list_t matches;
|
|---|
| 92 | init_match_ids(&matches);
|
|---|
| 93 | usb_device_create_match_ids_from_device_descriptor(
|
|---|
| 94 | &dev->device_descriptor, &matches);
|
|---|
| 95 | printf("%sDevice match ids (0x%04x by 0x%04x, %s)\n", get_indent(0),
|
|---|
| 96 | (int) dev->device_descriptor.product_id,
|
|---|
| 97 | (int) dev->device_descriptor.vendor_id,
|
|---|
| 98 | usb_str_class(dev->device_descriptor.device_class));
|
|---|
| 99 | dump_match_ids(&matches, get_indent(1));
|
|---|
| 100 | clean_match_ids(&matches);
|
|---|
| 101 |
|
|---|
| 102 | usb_dp_walk_simple(dev->full_configuration_descriptor,
|
|---|
| 103 | dev->full_configuration_descriptor_size,
|
|---|
| 104 | usb_dp_standard_descriptor_nesting,
|
|---|
| 105 | dump_match_ids_from_interface,
|
|---|
| 106 | dev);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static void dump_descriptor_tree_brief_device(const char *prefix,
|
|---|
| 110 | usb_standard_device_descriptor_t *descriptor)
|
|---|
| 111 | {
|
|---|
| 112 | printf("%sDevice (0x%04x by 0x%04x, %s, %zu configurations)\n", prefix,
|
|---|
| 113 | (int) descriptor->product_id,
|
|---|
| 114 | (int) descriptor->vendor_id,
|
|---|
| 115 | usb_str_class(descriptor->device_class),
|
|---|
| 116 | (size_t) descriptor->configuration_count);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | static void dump_descriptor_tree_brief_configuration(const char *prefix,
|
|---|
| 120 | usb_standard_configuration_descriptor_t *descriptor)
|
|---|
| 121 | {
|
|---|
| 122 | printf("%sConfiguration #%d (%zu interfaces, total %zuB)\n", prefix,
|
|---|
| 123 | (int) descriptor->configuration_number,
|
|---|
| 124 | (size_t) descriptor->interface_count,
|
|---|
| 125 | (size_t) descriptor->total_length);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | static void dump_descriptor_tree_brief_interface(const char *prefix,
|
|---|
| 129 | usb_standard_interface_descriptor_t *descriptor)
|
|---|
| 130 | {
|
|---|
| 131 | printf("%sInterface #%d (%s, 0x%02x, 0x%02x), alternate %d\n", prefix,
|
|---|
| 132 | (int) descriptor->interface_number,
|
|---|
| 133 | usb_str_class(descriptor->interface_class),
|
|---|
| 134 | (int) descriptor->interface_subclass,
|
|---|
| 135 | (int) descriptor->interface_protocol,
|
|---|
| 136 | (int) descriptor->alternate_setting);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | static void dump_descriptor_tree_brief_endpoint(const char *prefix,
|
|---|
| 140 | usb_standard_endpoint_descriptor_t *descriptor)
|
|---|
| 141 | {
|
|---|
| 142 | usb_endpoint_t endpoint_no = descriptor->endpoint_address & 0xF;
|
|---|
| 143 | usb_transfer_type_t transfer = descriptor->attributes & 0x3;
|
|---|
| 144 | usb_direction_t direction = descriptor->endpoint_address & 0x80
|
|---|
| 145 | ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
|
|---|
| 146 | printf("%sEndpoint #%d (%s %s, %zu)\n", prefix,
|
|---|
| 147 | endpoint_no, usb_str_transfer_type(transfer),
|
|---|
| 148 | direction == USB_DIRECTION_IN ? "in" : "out",
|
|---|
| 149 | (size_t) descriptor->max_packet_size);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | static void dump_descriptor_tree_brief_hid(const char *prefix,
|
|---|
| 153 | usb_standard_hid_descriptor_t *descriptor)
|
|---|
| 154 | {
|
|---|
| 155 | printf("%sHID (country %d, %d descriptors)\n", prefix,
|
|---|
| 156 | (int) descriptor->country_code,
|
|---|
| 157 | (int) descriptor->class_desc_count);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | static void dump_descriptor_tree_brief_hub(const char *prefix,
|
|---|
| 161 | usb_hub_descriptor_header_t *descriptor)
|
|---|
| 162 | {
|
|---|
| 163 | printf("%shub (%d ports)\n", prefix,
|
|---|
| 164 | (int) descriptor->port_count);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | static void dump_descriptor_tree_brief_callback(uint8_t *descriptor,
|
|---|
| 169 | size_t depth, void *arg)
|
|---|
| 170 | {
|
|---|
| 171 | const char *indent = get_indent(depth + 1);
|
|---|
| 172 |
|
|---|
| 173 | int descr_type = -1;
|
|---|
| 174 | size_t descr_size = descriptor[0];
|
|---|
| 175 | if (descr_size > 0) {
|
|---|
| 176 | descr_type = descriptor[1];
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | switch (descr_type) {
|
|---|
| 180 |
|
|---|
| 181 | #define _BRANCH(type_enum, descriptor_type, callback) \
|
|---|
| 182 | case type_enum: \
|
|---|
| 183 | if (descr_size >= sizeof(descriptor_type)) { \
|
|---|
| 184 | callback(indent, (descriptor_type *) descriptor); \
|
|---|
| 185 | } else { \
|
|---|
| 186 | descr_type = -1; \
|
|---|
| 187 | } \
|
|---|
| 188 | break;
|
|---|
| 189 |
|
|---|
| 190 | _BRANCH(USB_DESCTYPE_DEVICE,
|
|---|
| 191 | usb_standard_device_descriptor_t,
|
|---|
| 192 | dump_descriptor_tree_brief_device);
|
|---|
| 193 | _BRANCH(USB_DESCTYPE_CONFIGURATION,
|
|---|
| 194 | usb_standard_configuration_descriptor_t,
|
|---|
| 195 | dump_descriptor_tree_brief_configuration);
|
|---|
| 196 | _BRANCH(USB_DESCTYPE_INTERFACE,
|
|---|
| 197 | usb_standard_interface_descriptor_t,
|
|---|
| 198 | dump_descriptor_tree_brief_interface);
|
|---|
| 199 | _BRANCH(USB_DESCTYPE_ENDPOINT,
|
|---|
| 200 | usb_standard_endpoint_descriptor_t,
|
|---|
| 201 | dump_descriptor_tree_brief_endpoint);
|
|---|
| 202 | _BRANCH(USB_DESCTYPE_HID,
|
|---|
| 203 | usb_standard_hid_descriptor_t,
|
|---|
| 204 | dump_descriptor_tree_brief_hid);
|
|---|
| 205 | /*
|
|---|
| 206 | * Probably useless, hub descriptor shall not be part of
|
|---|
| 207 | * configuration descriptor.
|
|---|
| 208 | */
|
|---|
| 209 | _BRANCH(USB_DESCTYPE_HUB,
|
|---|
| 210 | usb_hub_descriptor_header_t,
|
|---|
| 211 | dump_descriptor_tree_brief_hub);
|
|---|
| 212 |
|
|---|
| 213 | default:
|
|---|
| 214 | break;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | if (descr_type == -1) {
|
|---|
| 218 | printf("%sInvalid descriptor.\n", indent);
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | void dump_descriptor_tree_brief(usbinfo_device_t *dev)
|
|---|
| 223 | {
|
|---|
| 224 | dump_descriptor_tree_brief_callback((uint8_t *)&dev->device_descriptor,
|
|---|
| 225 | (size_t) -1, NULL);
|
|---|
| 226 | usb_dp_walk_simple(dev->full_configuration_descriptor,
|
|---|
| 227 | dev->full_configuration_descriptor_size,
|
|---|
| 228 | usb_dp_standard_descriptor_nesting,
|
|---|
| 229 | dump_descriptor_tree_brief_callback,
|
|---|
| 230 | NULL);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | void dump_strings(usbinfo_device_t *dev)
|
|---|
| 234 | {
|
|---|
| 235 | /* Get supported languages. */
|
|---|
| 236 | l18_win_locales_t *langs;
|
|---|
| 237 | size_t langs_count;
|
|---|
| 238 | int rc = usb_request_get_supported_languages(&dev->ctrl_pipe,
|
|---|
| 239 | &langs, &langs_count);
|
|---|
| 240 | if (rc != EOK) {
|
|---|
| 241 | fprintf(stderr,
|
|---|
| 242 | NAME ": failed to get list of supported languages: %s.\n",
|
|---|
| 243 | str_error(rc));
|
|---|
| 244 | return;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | printf("%sString languages (%zu):", get_indent(0), langs_count);
|
|---|
| 248 | size_t i;
|
|---|
| 249 | for (i = 0; i < langs_count; i++) {
|
|---|
| 250 | printf(" 0x%04x", (int) langs[i]);
|
|---|
| 251 | }
|
|---|
| 252 | printf(".\n");
|
|---|
| 253 |
|
|---|
| 254 | /* Get all strings and dump them. */
|
|---|
| 255 | for (i = 0; i < langs_count; i++) {
|
|---|
| 256 | l18_win_locales_t lang = langs[i];
|
|---|
| 257 |
|
|---|
| 258 | printf("%sStrings in %s:\n", get_indent(0),
|
|---|
| 259 | str_l18_win_locale(lang));
|
|---|
| 260 | /*
|
|---|
| 261 | * Try only the first 15 strings
|
|---|
| 262 | * (typically, device will not have much more anyway).
|
|---|
| 263 | */
|
|---|
| 264 | size_t idx;
|
|---|
| 265 | for (idx = 1; idx < 0x0F; idx++) {
|
|---|
| 266 | char *string;
|
|---|
| 267 | rc = usb_request_get_string(&dev->ctrl_pipe, idx, lang,
|
|---|
| 268 | &string);
|
|---|
| 269 | if (rc != EOK) {
|
|---|
| 270 | continue;
|
|---|
| 271 | }
|
|---|
| 272 | printf("%sString #%zu: \"%s\"\n", get_indent(1),
|
|---|
| 273 | idx, string);
|
|---|
| 274 | free(string);
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /** @}
|
|---|
| 280 | */
|
|---|