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