source: mainline/uspace/app/usbinfo/dump.c@ 5f635ca

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

More generic descriptor dumping in `usbinfo'

  • Property mode set to 100644
File size: 8.6 KB
Line 
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 BYTES_PER_LINE 12
51
52#define BCD_INT(a) (((unsigned int)(a)) / 256)
53#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
54
55#define BCD_FMT "%x.%x"
56#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
57
58static void dump_descriptor_by_type(size_t, uint8_t *, size_t);
59
60typedef struct {
61 int descriptor;
62 void (*dump)(size_t indent, uint8_t *descriptor, size_t length);
63} descriptor_dump_t;
64
65static void dump_descriptor_device(size_t, uint8_t *, size_t);
66static void dump_descriptor_configuration(size_t, uint8_t *, size_t);
67static void dump_descriptor_interface(size_t, uint8_t *, size_t);
68static void dump_descriptor_string(size_t, uint8_t *, size_t);
69static void dump_descriptor_endpoint(size_t, uint8_t *, size_t);
70static void dump_descriptor_hid(size_t, uint8_t *, size_t);
71static void dump_descriptor_hub(size_t, uint8_t *, size_t);
72static void dump_descriptor_generic(size_t, uint8_t *, size_t);
73
74static descriptor_dump_t descriptor_dumpers[] = {
75 { USB_DESCTYPE_DEVICE, dump_descriptor_device },
76 { USB_DESCTYPE_CONFIGURATION, dump_descriptor_configuration },
77 { USB_DESCTYPE_STRING, dump_descriptor_string },
78 { USB_DESCTYPE_INTERFACE, dump_descriptor_interface },
79 { USB_DESCTYPE_ENDPOINT, dump_descriptor_endpoint },
80 { USB_DESCTYPE_HID, dump_descriptor_hid },
81 { USB_DESCTYPE_HUB, dump_descriptor_hub },
82 { -1, dump_descriptor_generic },
83 { -1, NULL }
84};
85
86void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
87{
88 if (msg != NULL) {
89 printf("%s\n", msg);
90 }
91
92 size_t i;
93 for (i = 0; i < length; i++) {
94 printf(" 0x%02X", buffer[i]);
95 if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
96 || (i + 1 == length)) {
97 printf("\n");
98 }
99 }
100}
101
102void dump_descriptor_by_type(size_t indent, uint8_t *d, size_t length)
103{
104 if (length < 2) {
105 return;
106 }
107 int type = d[1];
108
109 descriptor_dump_t *dumper = descriptor_dumpers;
110 while (dumper->dump != NULL) {
111 if ((dumper->descriptor == type) || (dumper->descriptor < 0)) {
112 dumper->dump(indent, d, length);
113 return;
114 }
115 dumper++;
116 }
117}
118
119void dump_usb_descriptor(uint8_t *descriptor, size_t size)
120{
121 dump_descriptor_by_type(0, descriptor, size);
122}
123
124void dump_descriptor_device(size_t indent, uint8_t *descr, size_t size)
125{
126 usb_standard_device_descriptor_t *d
127 = (usb_standard_device_descriptor_t *) descr;
128 if (size != sizeof(*d)) {
129 return;
130 }
131
132 printf(INDENT "bLength = %d\n", d->length);
133 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
134 printf(INDENT "bcdUSB = %d (" BCD_FMT ")\n", d->usb_spec_version,
135 BCD_ARGS(d->usb_spec_version));
136 printf(INDENT "bDeviceClass = 0x%02x\n", d->device_class);
137 printf(INDENT "bDeviceSubClass = 0x%02x\n", d->device_subclass);
138 printf(INDENT "bDeviceProtocol = 0x%02x\n", d->device_protocol);
139 printf(INDENT "bMaxPacketSize0 = %d\n", d->max_packet_size);
140 printf(INDENT "idVendor = %d\n", d->vendor_id);
141 printf(INDENT "idProduct = %d\n", d->product_id);
142 printf(INDENT "bcdDevice = %d\n", d->device_version);
143 printf(INDENT "iManufacturer = %d\n", d->str_manufacturer);
144 printf(INDENT "iProduct = %d\n", d->str_product);
145 printf(INDENT "iSerialNumber = %d\n", d->str_serial_number);
146 printf(INDENT "bNumConfigurations = %d\n", d->configuration_count);
147}
148
149void dump_descriptor_configuration(size_t indent, uint8_t *descr, size_t size)
150{
151 usb_standard_configuration_descriptor_t *d
152 = (usb_standard_configuration_descriptor_t *) descr;
153 if (size != sizeof(*d)) {
154 return;
155 }
156
157 bool self_powered = d->attributes & 64;
158 bool remote_wakeup = d->attributes & 32;
159
160 printf(INDENT "bLength = %d\n", d->length);
161 printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
162 printf(INDENT "wTotalLength = %d\n", d->total_length);
163 printf(INDENT "bNumInterfaces = %d\n", d->interface_count);
164 printf(INDENT "bConfigurationValue = %d\n", d->configuration_number);
165 printf(INDENT "iConfiguration = %d\n", d->str_configuration);
166 printf(INDENT "bmAttributes = %d [%s%s%s]\n", d->attributes,
167 self_powered ? "self-powered" : "",
168 (self_powered & remote_wakeup) ? ", " : "",
169 remote_wakeup ? "remote-wakeup" : "");
170 printf(INDENT "MaxPower = %d (%dmA)\n", d->max_power,
171 2 * d->max_power);
172}
173
174void dump_descriptor_interface(size_t indent, uint8_t *descr, size_t size)
175{
176 dump_descriptor_generic(indent, descr, size);
177}
178
179void dump_descriptor_string(size_t indent, uint8_t *descr, size_t size)
180{
181 dump_descriptor_generic(indent, descr, size);
182}
183
184void dump_descriptor_endpoint(size_t indent, uint8_t *descr, size_t size)
185{
186 dump_descriptor_generic(indent, descr, size);
187}
188
189void dump_descriptor_hid(size_t indent, uint8_t *descr, size_t size)
190{
191 dump_descriptor_generic(indent, descr, size);
192}
193
194void dump_descriptor_hub(size_t indent, uint8_t *descr, size_t size)
195{
196 dump_descriptor_generic(indent, descr, size);
197}
198
199void dump_descriptor_generic(size_t indent, uint8_t *descr, size_t size)
200{
201 dump_buffer(NULL, descr, size);
202}
203
204
205void dump_match_ids(match_id_list_t *matches)
206{
207 printf("Match ids:\n");
208 link_t *link;
209 for (link = matches->ids.next;
210 link != &matches->ids;
211 link = link->next) {
212 match_id_t *match = list_get_instance(link, match_id_t, link);
213
214 printf(INDENT "%d %s\n", match->score, match->id);
215 }
216}
217
218static void dump_tree_descriptor(uint8_t *descriptor, size_t depth)
219{
220 if (descriptor == NULL) {
221 return;
222 }
223 while (depth > 0) {
224 printf(" ");
225 depth--;
226 }
227 int type = (int) *(descriptor + 1);
228 const char *name = "unknown";
229 switch (type) {
230#define _TYPE(descriptor_type) \
231 case USB_DESCTYPE_##descriptor_type: name = #descriptor_type; break
232 _TYPE(DEVICE);
233 _TYPE(CONFIGURATION);
234 _TYPE(STRING);
235 _TYPE(INTERFACE);
236 _TYPE(ENDPOINT);
237 _TYPE(HID);
238 _TYPE(HID_REPORT);
239 _TYPE(HID_PHYSICAL);
240 _TYPE(HUB);
241#undef _TYPE
242 }
243 printf("0x%02x (%s)\n", type, name);
244 dump_descriptor_by_type(depth, descriptor, descriptor[0]);
245
246}
247
248static void dump_tree_internal(usb_dp_parser_t *parser, usb_dp_parser_data_t *data,
249 uint8_t *root, size_t depth)
250{
251 if (root == NULL) {
252 return;
253 }
254 dump_tree_descriptor(root, depth);
255 uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
256 do {
257 dump_tree_internal(parser, data, child, depth + 1);
258 child = usb_dp_get_sibling_descriptor(parser, data, root, child);
259 } while (child != NULL);
260}
261
262static void dump_tree(usb_dp_parser_t *parser, usb_dp_parser_data_t *data)
263{
264 uint8_t *ptr = data->data;
265 printf("Descriptor tree:\n");
266 dump_tree_internal(parser, data, ptr, 1);
267}
268
269#define NESTING(parentname, childname) \
270 { \
271 .child = USB_DESCTYPE_##childname, \
272 .parent = USB_DESCTYPE_##parentname, \
273 }
274#define LAST_NESTING { -1, -1 }
275
276static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
277 NESTING(CONFIGURATION, INTERFACE),
278 NESTING(INTERFACE, ENDPOINT),
279 NESTING(INTERFACE, HUB),
280 NESTING(INTERFACE, HID),
281 NESTING(HID, HID_REPORT),
282 LAST_NESTING
283};
284
285static usb_dp_parser_t parser = {
286 .nesting = descriptor_nesting
287};
288
289void dump_descriptor_tree(uint8_t *descriptors, size_t length)
290{
291 usb_dp_parser_data_t data = {
292 .data = descriptors,
293 .size = length,
294 .arg = NULL
295 };
296
297 dump_tree(&parser, &data);
298}
299
300/** @}
301 */
Note: See TracBrowser for help on using the repository browser.