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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b4b534ac was 534dee89, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusb: Sanitize includes

Include what you use.
https://code.google.com/p/include-what-you-use/

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