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 usbinfo
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Dumping of generic device properties.
|
---|
35 | */
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <usb/debug.h>
|
---|
40 | #include <usb/dev/pipes.h>
|
---|
41 | #include <usb/dev/recognise.h>
|
---|
42 | #include <usb/dev/request.h>
|
---|
43 | #include <usb/classes/classes.h>
|
---|
44 | #include <usb/classes/hub.h>
|
---|
45 | #include "usbinfo.h"
|
---|
46 |
|
---|
47 | void dump_short_device_identification(usb_device_t *usb_dev)
|
---|
48 | {
|
---|
49 | printf("%sDevice 0x%04x by vendor 0x%04x\n", get_indent(0),
|
---|
50 | usb_device_descriptors(usb_dev)->device.product_id,
|
---|
51 | usb_device_descriptors(usb_dev)->device.vendor_id);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static void dump_match_ids_from_interface(
|
---|
55 | const uint8_t *descriptor, size_t depth, void *arg)
|
---|
56 | {
|
---|
57 | if (depth != 1) {
|
---|
58 | return;
|
---|
59 | }
|
---|
60 | size_t descr_size = descriptor[0];
|
---|
61 | if (descr_size < sizeof(usb_standard_interface_descriptor_t)) {
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | int descr_type = descriptor[1];
|
---|
65 | if (descr_type != USB_DESCTYPE_INTERFACE) {
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | usb_device_t *usb_dev = arg;
|
---|
70 | assert(usb_dev);
|
---|
71 |
|
---|
72 | usb_standard_interface_descriptor_t *iface
|
---|
73 | = (usb_standard_interface_descriptor_t *) descriptor;
|
---|
74 |
|
---|
75 | printf("%sInterface #%d match ids (%s, 0x%02x, 0x%02x)\n",
|
---|
76 | get_indent(0),
|
---|
77 | (int) iface->interface_number,
|
---|
78 | usb_str_class(iface->interface_class),
|
---|
79 | (int) iface->interface_subclass,
|
---|
80 | (int) iface->interface_protocol);
|
---|
81 |
|
---|
82 | match_id_list_t matches;
|
---|
83 | init_match_ids(&matches);
|
---|
84 | usb_device_create_match_ids_from_interface(
|
---|
85 | &usb_device_descriptors(usb_dev)->device, iface, &matches);
|
---|
86 | dump_match_ids(&matches, get_indent(1));
|
---|
87 | clean_match_ids(&matches);
|
---|
88 | }
|
---|
89 |
|
---|
90 | void dump_device_match_ids(usb_device_t *usb_dev)
|
---|
91 | {
|
---|
92 | match_id_list_t matches;
|
---|
93 | init_match_ids(&matches);
|
---|
94 | usb_device_create_match_ids_from_device_descriptor(
|
---|
95 | &usb_device_descriptors(usb_dev)->device, &matches);
|
---|
96 | printf("%sDevice match ids (0x%04x by 0x%04x, %s)\n", get_indent(0),
|
---|
97 | usb_device_descriptors(usb_dev)->device.product_id,
|
---|
98 | usb_device_descriptors(usb_dev)->device.vendor_id,
|
---|
99 | usb_str_class(usb_device_descriptors(usb_dev)->device.device_class));
|
---|
100 | dump_match_ids(&matches, get_indent(1));
|
---|
101 | clean_match_ids(&matches);
|
---|
102 |
|
---|
103 | usb_dp_walk_simple(
|
---|
104 | usb_device_descriptors(usb_dev)->full_config,
|
---|
105 | usb_device_descriptors(usb_dev)->full_config_size,
|
---|
106 | usb_dp_standard_descriptor_nesting,
|
---|
107 | dump_match_ids_from_interface, usb_dev);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void dump_descriptor_tree_brief_device(const char *prefix,
|
---|
111 | usb_standard_device_descriptor_t *descriptor)
|
---|
112 | {
|
---|
113 | printf("%sDevice (0x%04x by 0x%04x, %s, %zu configurations)\n", prefix,
|
---|
114 | (int) descriptor->product_id,
|
---|
115 | (int) descriptor->vendor_id,
|
---|
116 | usb_str_class(descriptor->device_class),
|
---|
117 | (size_t) descriptor->configuration_count);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static void dump_descriptor_tree_brief_configuration(const char *prefix,
|
---|
121 | usb_standard_configuration_descriptor_t *descriptor)
|
---|
122 | {
|
---|
123 | printf("%sConfiguration #%d (%zu interfaces, total %zuB)\n", prefix,
|
---|
124 | (int) descriptor->configuration_number,
|
---|
125 | (size_t) descriptor->interface_count,
|
---|
126 | (size_t) descriptor->total_length);
|
---|
127 | }
|
---|
128 |
|
---|
129 | static void dump_descriptor_tree_brief_interface(const char *prefix,
|
---|
130 | usb_standard_interface_descriptor_t *descriptor)
|
---|
131 | {
|
---|
132 | printf("%sInterface #%d (%s, 0x%02x, 0x%02x), alternate %d\n", prefix,
|
---|
133 | (int) descriptor->interface_number,
|
---|
134 | usb_str_class(descriptor->interface_class),
|
---|
135 | (int) descriptor->interface_subclass,
|
---|
136 | (int) descriptor->interface_protocol,
|
---|
137 | (int) descriptor->alternate_setting);
|
---|
138 | }
|
---|
139 |
|
---|
140 | static void dump_descriptor_tree_brief_endpoint(const char *prefix,
|
---|
141 | usb_standard_endpoint_descriptor_t *descriptor)
|
---|
142 | {
|
---|
143 | usb_endpoint_t endpoint_no = descriptor->endpoint_address & 0xF;
|
---|
144 | usb_transfer_type_t transfer = descriptor->attributes & 0x3;
|
---|
145 | usb_direction_t direction = descriptor->endpoint_address & 0x80
|
---|
146 | ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
|
---|
147 | printf("%sEndpoint #%d (%s %s, %zu)\n", prefix,
|
---|
148 | endpoint_no, usb_str_transfer_type(transfer),
|
---|
149 | direction == USB_DIRECTION_IN ? "in" : "out",
|
---|
150 | (size_t) descriptor->max_packet_size);
|
---|
151 | }
|
---|
152 |
|
---|
153 | static void dump_descriptor_tree_brief_hid(const char *prefix,
|
---|
154 | usb_standard_hid_descriptor_t *descriptor)
|
---|
155 | {
|
---|
156 | printf("%sHID (country %d, %d descriptors)\n", prefix,
|
---|
157 | (int) descriptor->country_code,
|
---|
158 | (int) descriptor->class_desc_count);
|
---|
159 | }
|
---|
160 |
|
---|
161 | static void dump_descriptor_tree_brief_hub(const char *prefix,
|
---|
162 | usb_hub_descriptor_header_t *descriptor)
|
---|
163 | {
|
---|
164 | printf("%shub (%d ports)\n", prefix,
|
---|
165 | (int) descriptor->port_count);
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | static void dump_descriptor_tree_callback(
|
---|
170 | const uint8_t *descriptor, size_t depth, void *arg)
|
---|
171 | {
|
---|
172 | const char *indent = get_indent(depth + 1);
|
---|
173 |
|
---|
174 | int descr_type = -1;
|
---|
175 | size_t descr_size = descriptor[0];
|
---|
176 | if (descr_size > 0) {
|
---|
177 | descr_type = descriptor[1];
|
---|
178 | }
|
---|
179 |
|
---|
180 | switch (descr_type) {
|
---|
181 |
|
---|
182 | #define _BRANCH(type_enum, descriptor_type, callback) \
|
---|
183 | case type_enum: \
|
---|
184 | if (descr_size >= sizeof(descriptor_type)) { \
|
---|
185 | callback(indent, (descriptor_type *) descriptor); \
|
---|
186 | if (arg != NULL) { \
|
---|
187 | usb_dump_standard_descriptor(stdout, \
|
---|
188 | get_indent(depth +2), "\n", \
|
---|
189 | descriptor, descr_size); \
|
---|
190 | } \
|
---|
191 | } else { \
|
---|
192 | descr_type = -1; \
|
---|
193 | } \
|
---|
194 | break;
|
---|
195 |
|
---|
196 | _BRANCH(USB_DESCTYPE_DEVICE,
|
---|
197 | usb_standard_device_descriptor_t,
|
---|
198 | dump_descriptor_tree_brief_device);
|
---|
199 | _BRANCH(USB_DESCTYPE_CONFIGURATION,
|
---|
200 | usb_standard_configuration_descriptor_t,
|
---|
201 | dump_descriptor_tree_brief_configuration);
|
---|
202 | _BRANCH(USB_DESCTYPE_INTERFACE,
|
---|
203 | usb_standard_interface_descriptor_t,
|
---|
204 | dump_descriptor_tree_brief_interface);
|
---|
205 | _BRANCH(USB_DESCTYPE_ENDPOINT,
|
---|
206 | usb_standard_endpoint_descriptor_t,
|
---|
207 | dump_descriptor_tree_brief_endpoint);
|
---|
208 | _BRANCH(USB_DESCTYPE_HID,
|
---|
209 | usb_standard_hid_descriptor_t,
|
---|
210 | dump_descriptor_tree_brief_hid);
|
---|
211 | /*
|
---|
212 | * Probably useless, hub descriptor shall not be part of
|
---|
213 | * configuration descriptor.
|
---|
214 | */
|
---|
215 | _BRANCH(USB_DESCTYPE_HUB,
|
---|
216 | usb_hub_descriptor_header_t,
|
---|
217 | dump_descriptor_tree_brief_hub);
|
---|
218 |
|
---|
219 | default:
|
---|
220 | break;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (descr_type == -1) {
|
---|
224 | printf("%sInvalid descriptor.\n", indent);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | void dump_descriptor_tree_brief(usb_device_t *usb_dev)
|
---|
229 | {
|
---|
230 | dump_descriptor_tree_callback(
|
---|
231 | (const uint8_t *)&usb_device_descriptors(usb_dev)->device,
|
---|
232 | (size_t) -1, NULL);
|
---|
233 |
|
---|
234 | usb_dp_walk_simple(
|
---|
235 | usb_device_descriptors(usb_dev)->full_config,
|
---|
236 | usb_device_descriptors(usb_dev)->full_config_size,
|
---|
237 | usb_dp_standard_descriptor_nesting,
|
---|
238 | dump_descriptor_tree_callback, NULL);
|
---|
239 | }
|
---|
240 |
|
---|
241 | void dump_descriptor_tree_full(usb_device_t *usb_dev)
|
---|
242 | {
|
---|
243 | dump_descriptor_tree_callback(
|
---|
244 | (const uint8_t *)&usb_device_descriptors(usb_dev)->device,
|
---|
245 | (size_t) -1, usb_dev);
|
---|
246 |
|
---|
247 | usb_dp_walk_simple(
|
---|
248 | usb_device_descriptors(usb_dev)->full_config,
|
---|
249 | usb_device_descriptors(usb_dev)->full_config_size,
|
---|
250 | usb_dp_standard_descriptor_nesting,
|
---|
251 | dump_descriptor_tree_callback, usb_dev);
|
---|
252 | }
|
---|
253 |
|
---|
254 | static void find_string_indexes_callback(
|
---|
255 | const uint8_t *descriptor, size_t depth, void *arg)
|
---|
256 | {
|
---|
257 | size_t descriptor_length = descriptor[0];
|
---|
258 | if (descriptor_length <= 1) {
|
---|
259 | return;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*printf("Found string in %s->%s: %zu\n",
|
---|
263 | #descr_struct, #descr_item, __str_index); */
|
---|
264 | #define SET_STRING_INDEX(descr, mask, descr_type, descr_struct, descr_item) \
|
---|
265 | do { \
|
---|
266 | if ((descr)[1] == (descr_type)) { \
|
---|
267 | descr_struct *__type_descr = (descr_struct *) (descr); \
|
---|
268 | size_t __str_index = __type_descr->descr_item; \
|
---|
269 | if ((__str_index > 0) && (__str_index < 64)) { \
|
---|
270 | mask = (mask) | (1 << __str_index); \
|
---|
271 | } \
|
---|
272 | } \
|
---|
273 | } while (0)
|
---|
274 |
|
---|
275 | uint64_t *mask = arg;
|
---|
276 |
|
---|
277 | #define SET_STR(descr_type, descr_struct, descr_item) \
|
---|
278 | SET_STRING_INDEX(descriptor, (*mask), descr_type, descr_struct, descr_item)
|
---|
279 |
|
---|
280 | SET_STR(USB_DESCTYPE_DEVICE, usb_standard_device_descriptor_t,
|
---|
281 | str_manufacturer);
|
---|
282 | SET_STR(USB_DESCTYPE_DEVICE, usb_standard_device_descriptor_t,
|
---|
283 | str_product);
|
---|
284 | SET_STR(USB_DESCTYPE_DEVICE, usb_standard_device_descriptor_t,
|
---|
285 | str_serial_number);
|
---|
286 | SET_STR(USB_DESCTYPE_CONFIGURATION, usb_standard_configuration_descriptor_t,
|
---|
287 | str_configuration);
|
---|
288 | SET_STR(USB_DESCTYPE_INTERFACE, usb_standard_interface_descriptor_t,
|
---|
289 | str_interface);
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | void dump_strings(usb_device_t *usb_dev)
|
---|
294 | {
|
---|
295 | /* Find used indexes. Devices with more than 64 strings are very rare.*/
|
---|
296 | uint64_t str_mask = 0;
|
---|
297 | find_string_indexes_callback(
|
---|
298 | (const uint8_t *)&usb_device_descriptors(usb_dev)->device, 0,
|
---|
299 | &str_mask);
|
---|
300 |
|
---|
301 | usb_dp_walk_simple(
|
---|
302 | usb_device_descriptors(usb_dev)->full_config,
|
---|
303 | usb_device_descriptors(usb_dev)->full_config_size,
|
---|
304 | usb_dp_standard_descriptor_nesting,
|
---|
305 | find_string_indexes_callback, &str_mask);
|
---|
306 |
|
---|
307 | if (str_mask == 0) {
|
---|
308 | printf("Device does not support string descriptors.\n");
|
---|
309 | return;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* Get supported languages. */
|
---|
313 | l18_win_locales_t *langs;
|
---|
314 | size_t langs_count;
|
---|
315 | int rc = usb_request_get_supported_languages(
|
---|
316 | usb_device_get_default_pipe(usb_dev), &langs, &langs_count);
|
---|
317 | if (rc != EOK) {
|
---|
318 | fprintf(stderr,
|
---|
319 | NAME ": failed to get list of supported languages: %s.\n",
|
---|
320 | str_error(rc));
|
---|
321 | return;
|
---|
322 | }
|
---|
323 |
|
---|
324 | printf("%sString languages (%zu):", get_indent(0), langs_count);
|
---|
325 | size_t i;
|
---|
326 | for (i = 0; i < langs_count; i++) {
|
---|
327 | printf(" 0x%04x", (int) langs[i]);
|
---|
328 | }
|
---|
329 | printf(".\n");
|
---|
330 |
|
---|
331 | /* Get all strings and dump them. */
|
---|
332 | for (i = 0; i < langs_count; i++) {
|
---|
333 | l18_win_locales_t lang = langs[i];
|
---|
334 |
|
---|
335 | printf("%sStrings in %s:\n", get_indent(0),
|
---|
336 | str_l18_win_locale(lang));
|
---|
337 |
|
---|
338 | size_t idx;
|
---|
339 | for (idx = 1; idx < 64; idx++) {
|
---|
340 | if ((str_mask & ((uint64_t)1 << idx)) == 0) {
|
---|
341 | continue;
|
---|
342 | }
|
---|
343 | char *string = NULL;
|
---|
344 | rc = usb_request_get_string(
|
---|
345 | usb_device_get_default_pipe(usb_dev), idx, lang,
|
---|
346 | &string);
|
---|
347 | if ((rc != EOK) && (rc != EEMPTY)) {
|
---|
348 | printf("%sWarn: failed to retrieve string #%zu: %s.\n",
|
---|
349 | get_indent(1), idx, str_error(rc));
|
---|
350 | continue;
|
---|
351 | }
|
---|
352 | printf("%sString #%zu: \"%s\"\n", get_indent(1),
|
---|
353 | idx, rc == EOK ? string : "");
|
---|
354 | if (string != NULL) {
|
---|
355 | free(string);
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | void dump_status(usb_device_t *usb_dev)
|
---|
363 | {
|
---|
364 | int rc;
|
---|
365 | uint16_t status = 0;
|
---|
366 |
|
---|
367 | /* Device status first. */
|
---|
368 | rc = usb_request_get_status(usb_device_get_default_pipe(usb_dev),
|
---|
369 | USB_REQUEST_RECIPIENT_DEVICE, 0, &status);
|
---|
370 | if (rc != EOK) {
|
---|
371 | printf("%sFailed to get device status: %s.\n",
|
---|
372 | get_indent(0), str_error(rc));
|
---|
373 | } else {
|
---|
374 | printf("%sDevice status 0x%04x: power=%s, remote-wakeup=%s.\n",
|
---|
375 | get_indent(0), status,
|
---|
376 | status & USB_DEVICE_STATUS_SELF_POWERED ? "self" : "bus",
|
---|
377 | status & USB_DEVICE_STATUS_REMOTE_WAKEUP ? "yes" : "no");
|
---|
378 | }
|
---|
379 |
|
---|
380 | /* Interface is not interesting, skipping ;-). */
|
---|
381 |
|
---|
382 | /* Control endpoint zero. */
|
---|
383 | status = 0;
|
---|
384 | rc = usb_request_get_status(usb_device_get_default_pipe(usb_dev),
|
---|
385 | USB_REQUEST_RECIPIENT_ENDPOINT, 0, &status);
|
---|
386 | if (rc != EOK) {
|
---|
387 | printf("%sFailed to get control endpoint status: %s.\n",
|
---|
388 | get_indent(0), str_error(rc));
|
---|
389 | } else {
|
---|
390 | printf("%sControl endpoint zero status %04X: halted=%s.\n",
|
---|
391 | get_indent(0), status,
|
---|
392 | status & USB_ENDPOINT_STATUS_HALTED ? "yes" : "no");
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | /** @}
|
---|
397 | */
|
---|