source: mainline/uspace/app/usbinfo/dump.c@ c9d5577

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c9d5577 was c9d5577, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

`usbinfo' dumps interface descriptor nicely

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