source: mainline/uspace/lib/usb/src/dump.c@ cc63815

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cc63815 was 7f70d1c, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

libusb: print also superspeed ep companion descriptor

  • Property mode set to 100644
File size: 13.2 KB
Line 
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 libusb
30 * @{
31 */
32/** @file
33 * Descriptor dumping.
34 */
35#include <stdlib.h>
36#include <stdio.h>
37#include <stddef.h>
38#include <usb/debug.h>
39#include <usb/descriptor.h>
40#include <usb/classes/classes.h>
41#include <usb/classes/hub.h>
42#include <usb/usb.h>
43
44/** Mapping between descriptor id and dumping function. */
45typedef struct {
46 /** Descriptor id. */
47 int id;
48 /** Dumping function. */
49 void (*dump)(FILE *, const char *, const char *,
50 const uint8_t *, size_t);
51} descriptor_dump_t;
52
53static void usb_dump_descriptor_device(FILE *, const char *, const char *,
54 const uint8_t *, size_t);
55static void usb_dump_descriptor_configuration(FILE *, const char *, const char *,
56 const uint8_t *, size_t);
57static void usb_dump_descriptor_interface(FILE *, const char *, const char *,
58 const uint8_t *, size_t);
59static void usb_dump_descriptor_string(FILE *, const char *, const char *,
60 const uint8_t *, size_t);
61static void usb_dump_descriptor_endpoint(FILE *, const char *, const char *,
62 const uint8_t *, size_t);
63static void usb_dump_descriptor_superspeed_endpoint_companion(FILE *, const char *, const char *,
64 const uint8_t *, size_t);
65static void usb_dump_descriptor_hid(FILE *, const char *, const char *,
66 const uint8_t *, size_t);
67static void usb_dump_descriptor_hub(FILE *, const char *, const char *,
68 const uint8_t *, size_t);
69static void usb_dump_descriptor_generic(FILE *, const char *, const char *,
70 const uint8_t *, size_t);
71
72/** Descriptor dumpers mapping. */
73static descriptor_dump_t descriptor_dumpers[] = {
74 { USB_DESCTYPE_DEVICE, usb_dump_descriptor_device },
75 { USB_DESCTYPE_CONFIGURATION, usb_dump_descriptor_configuration },
76 { USB_DESCTYPE_STRING, usb_dump_descriptor_string },
77 { USB_DESCTYPE_INTERFACE, usb_dump_descriptor_interface },
78 { USB_DESCTYPE_ENDPOINT, usb_dump_descriptor_endpoint },
79 { USB_DESCTYPE_SSPEED_EP_COMPANION, usb_dump_descriptor_superspeed_endpoint_companion },
80 { USB_DESCTYPE_HID, usb_dump_descriptor_hid },
81 { USB_DESCTYPE_HUB, usb_dump_descriptor_hub },
82 { -1, usb_dump_descriptor_generic },
83 { -1, NULL }
84};
85
86/** Dumps standard USB descriptor.
87 * The @p line_suffix must contain the newline <code>\\n</code> character.
88 * When @p line_suffix or @p line_prefix is NULL, they are substitued with
89 * default values
90 * (<code> - </code> for prefix and line termination for suffix).
91 *
92 * @param output Output file stream to dump descriptor to.
93 * @param line_prefix Prefix for each line of output.
94 * @param line_suffix Suffix of each line of output.
95 * @param descriptor Actual descriptor.
96 * @param descriptor_length Descriptor size.
97 */
98void usb_dump_standard_descriptor(FILE *output,
99 const char *line_prefix, const char *line_suffix,
100 const uint8_t *descriptor, size_t descriptor_length)
101{
102 if (descriptor_length < 2) {
103 return;
104 }
105 int type = descriptor[1];
106
107 descriptor_dump_t *dumper = descriptor_dumpers;
108 while (dumper->dump != NULL) {
109 if ((dumper->id == type) || (dumper->id < 0)) {
110 dumper->dump(output, line_prefix, line_suffix,
111 descriptor, descriptor_length);
112 return;
113 }
114 dumper++;
115 }
116}
117
118/** Prints single line of USB descriptor dump.
119 * @warning This macro abuses heavily the naming conventions used
120 * by all dumping functions (i.e. names for output file stream (@c output) and
121 * line prefix and suffix (@c line_prefix and @c line_suffix respectively))-
122 *
123 * @param fmt Formatting string.
124 */
125#define PRINTLINE(fmt, ...) \
126 fprintf(output, "%s" fmt "%s", \
127 line_prefix ? line_prefix : " - ", \
128 __VA_ARGS__, \
129 line_suffix ? line_suffix : "\n")
130
131#define BCD_INT(a) (((unsigned int)(a)) / 256)
132#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
133
134#define BCD_FMT "%x.%x"
135#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
136
137static void usb_dump_descriptor_device(FILE *output,
138 const char *line_prefix, const char *line_suffix,
139 const uint8_t *descriptor, size_t descriptor_length)
140{
141 usb_standard_device_descriptor_t *d
142 = (usb_standard_device_descriptor_t *) descriptor;
143 if (descriptor_length < sizeof(*d)) {
144 return;
145 }
146
147 PRINTLINE("bLength = %d", d->length);
148 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
149 PRINTLINE("bcdUSB = %d (" BCD_FMT ")", d->usb_spec_version,
150 BCD_ARGS(d->usb_spec_version));
151 PRINTLINE("bDeviceClass = 0x%02x", d->device_class);
152 PRINTLINE("bDeviceSubClass = 0x%02x", d->device_subclass);
153 PRINTLINE("bDeviceProtocol = 0x%02x", d->device_protocol);
154 PRINTLINE("bMaxPacketSize0 = %d", d->max_packet_size);
155 PRINTLINE("idVendor = 0x%04x", d->vendor_id);
156 PRINTLINE("idProduct = 0x%04x", d->product_id);
157 PRINTLINE("bcdDevice = %d", d->device_version);
158 PRINTLINE("iManufacturer = %d", d->str_manufacturer);
159 PRINTLINE("iProduct = %d", d->str_product);
160 PRINTLINE("iSerialNumber = %d", d->str_serial_number);
161 PRINTLINE("bNumConfigurations = %d", d->configuration_count);
162}
163
164static void usb_dump_descriptor_configuration(FILE *output,
165 const char *line_prefix, const char *line_suffix,
166 const uint8_t *descriptor, size_t descriptor_length)
167{
168 usb_standard_configuration_descriptor_t *d
169 = (usb_standard_configuration_descriptor_t *) descriptor;
170 if (descriptor_length < sizeof(*d)) {
171 return;
172 }
173
174 bool self_powered = d->attributes & 64;
175 bool remote_wakeup = d->attributes & 32;
176
177 PRINTLINE("bLength = %d", d->length);
178 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
179 PRINTLINE("wTotalLength = %d", d->total_length);
180 PRINTLINE("bNumInterfaces = %d", d->interface_count);
181 PRINTLINE("bConfigurationValue = %d", d->configuration_number);
182 PRINTLINE("iConfiguration = %d", d->str_configuration);
183 PRINTLINE("bmAttributes = %d [%s%s%s]", d->attributes,
184 self_powered ? "self-powered" : "",
185 (self_powered & remote_wakeup) ? ", " : "",
186 remote_wakeup ? "remote-wakeup" : "");
187 PRINTLINE("MaxPower = %d (%dmA)", d->max_power,
188 2 * d->max_power);
189}
190
191static void usb_dump_descriptor_interface(FILE *output,
192 const char *line_prefix, const char *line_suffix,
193 const uint8_t *descriptor, size_t descriptor_length)
194{
195 usb_standard_interface_descriptor_t *d
196 = (usb_standard_interface_descriptor_t *) descriptor;
197 if (descriptor_length < sizeof(*d)) {
198 return;
199 }
200
201 PRINTLINE("bLength = %d", d->length);
202 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
203 PRINTLINE("bInterfaceNumber = %d", d->interface_number);
204 PRINTLINE("bAlternateSetting = %d", d->alternate_setting);
205 PRINTLINE("bNumEndpoints = %d", d->endpoint_count);
206 PRINTLINE("bInterfaceClass = %s", d->interface_class == 0
207 ? "reserved (0)" : usb_str_class(d->interface_class));
208 PRINTLINE("bInterfaceSubClass = %d", d->interface_subclass);
209 PRINTLINE("bInterfaceProtocol = %d", d->interface_protocol);
210 PRINTLINE("iInterface = %d", d->str_interface);
211}
212
213static void usb_dump_descriptor_string(FILE *output,
214 const char *line_prefix, const char *line_suffix,
215 const uint8_t *descriptor, size_t descriptor_length)
216{
217}
218
219static void usb_dump_descriptor_endpoint(FILE *output,
220 const char *line_prefix, const char *line_suffix,
221 const uint8_t *descriptor, size_t descriptor_length)
222{
223 usb_standard_endpoint_descriptor_t *d
224 = (usb_standard_endpoint_descriptor_t *) descriptor;
225 if (descriptor_length < sizeof(*d)) {
226 return;
227 }
228
229 int endpoint = d->endpoint_address & 15;
230 usb_direction_t direction = d->endpoint_address & 128
231 ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
232 usb_transfer_type_t transfer_type = d->attributes & 3;
233
234 PRINTLINE("bLength = %d", d->length);
235 PRINTLINE("bDescriptorType = 0x%02X", d->descriptor_type);
236 PRINTLINE("bEndpointAddress = 0x%02X [%d, %s]",
237 d->endpoint_address, endpoint,
238 direction == USB_DIRECTION_IN ? "in" : "out");
239 PRINTLINE("bmAttributes = %d [%s]", d->attributes,
240 usb_str_transfer_type(transfer_type));
241 PRINTLINE("wMaxPacketSize = %d", d->max_packet_size);
242 PRINTLINE("bInterval = %dms", d->poll_interval);
243}
244
245static void usb_dump_descriptor_superspeed_endpoint_companion(FILE *output,
246 const char *line_prefix, const char *line_suffix,
247 const uint8_t *descriptor, size_t descriptor_length)
248{
249 usb_superspeed_endpoint_companion_descriptor_t *d
250 = (usb_superspeed_endpoint_companion_descriptor_t *) descriptor;
251 if (descriptor_length < sizeof(*d)) {
252 return;
253 }
254
255 PRINTLINE("bLength = %u", d->length);
256 PRINTLINE("bDescriptorType = 0x%02X", d->descriptor_type);
257 PRINTLINE("bMaxBurst = %u", d->max_burst);
258 PRINTLINE("bmAttributes = %d", d->attributes);
259 PRINTLINE("wBytesPerInterval = %u", d->bytes_per_interval);
260}
261
262static void usb_dump_descriptor_hid(FILE *output,
263 const char *line_prefix, const char *line_suffix,
264 const uint8_t *descriptor, size_t descriptor_length)
265{
266 usb_standard_hid_descriptor_t *d
267 = (usb_standard_hid_descriptor_t *) descriptor;
268 if (descriptor_length < sizeof(*d)) {
269 return;
270 }
271
272 PRINTLINE("bLength = %d", d->length);
273 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
274 PRINTLINE("bcdHID = %d (" BCD_FMT ")", d->spec_release,
275 BCD_ARGS(d->spec_release));
276 PRINTLINE("bCountryCode = %d", d->country_code);
277 PRINTLINE("bNumDescriptors = %d", d->class_desc_count);
278 PRINTLINE("bDescriptorType = %d", d->report_desc_info.type);
279 PRINTLINE("wDescriptorLength = %d", d->report_desc_info.length);
280
281 /* Print info about report descriptors. */
282 size_t i;
283 size_t count = (descriptor_length - sizeof(*d))
284 / sizeof(usb_standard_hid_class_descriptor_info_t);
285 usb_standard_hid_class_descriptor_info_t *d2
286 = (usb_standard_hid_class_descriptor_info_t *)
287 (descriptor + sizeof(*d));
288 for (i = 0; i < count; i++, d2++) {
289 PRINTLINE("bDescriptorType = %d", d2->type);
290 PRINTLINE("wDescriptorLength = %d", d2->length);
291 }
292}
293
294static void usb_dump_descriptor_hub(FILE *output,
295 const char *line_prefix, const char *line_suffix,
296 const uint8_t *descriptor, size_t descriptor_length)
297{
298 usb_hub_descriptor_header_t *d =
299 (usb_hub_descriptor_header_t *) descriptor;
300 if (descriptor_length < sizeof(d))
301 return;
302
303 PRINTLINE("bDescLength: = %d", d->length);
304 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
305 PRINTLINE("bNbrPorts = %d", d->port_count);
306 PRINTLINE("bHubCharacteristics = 0x%02x%02x (%s;%s%s)",
307 d->characteristics_reserved, d->characteristics,
308 (d->characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG) ?
309 "No Power Switching" :
310 ((d->characteristics & HUB_CHAR_POWER_PER_PORT_FLAG) ?
311 "Per-Port Switching" : "Ganged Power Switching"),
312 (d->characteristics & HUB_CHAR_COMPOUND_DEVICE) ?
313 "Compound Device;" : "",
314 (d->characteristics & HUB_CHAR_NO_OC_FLAG) ?
315 "No OC Protection" :
316 ((d->characteristics & HUB_CHAR_OC_PER_PORT_FLAG) ?
317 "Individual Port OC Protection" :
318 "Global OC Protection")
319 );
320 PRINTLINE("bPwrOn2PwrGood = %d (%d ms)",
321 d->power_good_time, d->power_good_time * 2);
322 PRINTLINE("bHubContrCurrent = %d (%d mA)",
323 d->max_current, d->max_current);
324 const size_t port_bytes = (descriptor_length - sizeof(*d)) / 2;
325 const uint8_t *removable_mask = descriptor + sizeof(*d);
326 const uint8_t *powered_mask = descriptor + sizeof(*d) + port_bytes;
327
328 if (port_bytes == 0
329 || port_bytes > (((d->port_count / (unsigned)8) + 1) * 2)) {
330 PRINTLINE("::CORRUPTED DESCRIPTOR:: (%zu bytes remain)",
331 port_bytes * 2);
332 }
333
334 fprintf(output, "%sDeviceRemovable = 0x",
335 line_prefix ? line_prefix : " - ");
336 for (unsigned i = port_bytes; i > 0; --i)
337 fprintf(output, "%02x", removable_mask[i - 1]);
338 fprintf(output, " (0b1 - Device non-removable)%s",
339 line_suffix ? line_suffix : "\n");
340
341 fprintf(output, "%sPortPwrCtrlMask = 0x",
342 line_prefix ? line_prefix : " - ");
343 for (unsigned i = port_bytes; i > 0; --i)
344 fprintf(output, "%02x", powered_mask[i - 1]);
345 fprintf(output, " (Legacy - All should be 0b1)%s",
346 line_suffix ? line_suffix : "\n");
347}
348
349static void usb_dump_descriptor_generic(FILE *output,
350 const char *line_prefix, const char *line_suffix,
351 const uint8_t *descriptor, size_t descriptor_length)
352{
353 /* TODO */
354}
355
356
357/**
358 * @}
359 */
Note: See TracBrowser for help on using the repository browser.