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/pipes.h>
|
---|
40 | #include <usb/recognise.h>
|
---|
41 | #include <usb/request.h>
|
---|
42 | #include <usb/classes/classes.h>
|
---|
43 | #include <usb/classes/hid.h>
|
---|
44 | #include "usbinfo.h"
|
---|
45 |
|
---|
46 | void dump_short_device_identification(usbinfo_device_t *dev)
|
---|
47 | {
|
---|
48 | printf("%sDevice 0x%04x by vendor 0x%04x\n", get_indent(0),
|
---|
49 | (int) dev->device_descriptor.product_id,
|
---|
50 | (int) dev->device_descriptor.vendor_id);
|
---|
51 | }
|
---|
52 |
|
---|
53 | static void dump_match_ids_from_interface(uint8_t *descriptor, size_t depth,
|
---|
54 | void *arg)
|
---|
55 | {
|
---|
56 | if (depth != 1) {
|
---|
57 | return;
|
---|
58 | }
|
---|
59 | size_t descr_size = descriptor[0];
|
---|
60 | if (descr_size < sizeof(usb_standard_interface_descriptor_t)) {
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | int descr_type = descriptor[1];
|
---|
64 | if (descr_type != USB_DESCTYPE_INTERFACE) {
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | usbinfo_device_t *dev = (usbinfo_device_t *) arg;
|
---|
69 |
|
---|
70 | usb_standard_interface_descriptor_t *iface
|
---|
71 | = (usb_standard_interface_descriptor_t *) descriptor;
|
---|
72 |
|
---|
73 | printf("%sInterface #%d match ids (%s, 0x%02x, 0x%02x)\n",
|
---|
74 | get_indent(0),
|
---|
75 | (int) iface->interface_number,
|
---|
76 | usb_str_class(iface->interface_class),
|
---|
77 | (int) iface->interface_subclass,
|
---|
78 | (int) iface->interface_protocol);
|
---|
79 |
|
---|
80 | match_id_list_t matches;
|
---|
81 | init_match_ids(&matches);
|
---|
82 | usb_device_create_match_ids_from_interface(&dev->device_descriptor,
|
---|
83 | iface, &matches);
|
---|
84 | dump_match_ids(&matches, get_indent(1));
|
---|
85 | clean_match_ids(&matches);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void dump_device_match_ids(usbinfo_device_t *dev)
|
---|
89 | {
|
---|
90 | match_id_list_t matches;
|
---|
91 | init_match_ids(&matches);
|
---|
92 | usb_device_create_match_ids_from_device_descriptor(
|
---|
93 | &dev->device_descriptor, &matches);
|
---|
94 | printf("%sDevice match ids (0x%04x by 0x%04x, %s)\n", get_indent(0),
|
---|
95 | (int) dev->device_descriptor.product_id,
|
---|
96 | (int) dev->device_descriptor.vendor_id,
|
---|
97 | usb_str_class(dev->device_descriptor.device_class));
|
---|
98 | dump_match_ids(&matches, get_indent(1));
|
---|
99 | clean_match_ids(&matches);
|
---|
100 |
|
---|
101 | usb_dp_walk_simple(dev->full_configuration_descriptor,
|
---|
102 | dev->full_configuration_descriptor_size,
|
---|
103 | usb_dp_standard_descriptor_nesting,
|
---|
104 | dump_match_ids_from_interface,
|
---|
105 | dev);
|
---|
106 | }
|
---|
107 |
|
---|
108 | static void dump_descriptor_tree_brief_device(const char *prefix,
|
---|
109 | usb_standard_device_descriptor_t *descriptor)
|
---|
110 | {
|
---|
111 | printf("%sDevice (0x%04x by 0x%04x, %s, %zu configurations)\n", prefix,
|
---|
112 | (int) descriptor->product_id,
|
---|
113 | (int) descriptor->vendor_id,
|
---|
114 | usb_str_class(descriptor->device_class),
|
---|
115 | (size_t) descriptor->configuration_count);
|
---|
116 | }
|
---|
117 |
|
---|
118 | static void dump_descriptor_tree_brief_configuration(const char *prefix,
|
---|
119 | usb_standard_configuration_descriptor_t *descriptor)
|
---|
120 | {
|
---|
121 | printf("%sConfiguration #%d (%zu interfaces)\n", prefix,
|
---|
122 | (int) descriptor->configuration_number,
|
---|
123 | (size_t) descriptor->interface_count);
|
---|
124 | }
|
---|
125 |
|
---|
126 | static void dump_descriptor_tree_brief_interface(const char *prefix,
|
---|
127 | usb_standard_interface_descriptor_t *descriptor)
|
---|
128 | {
|
---|
129 | printf("%sInterface #%d (%s, 0x%02x, 0x%02x), alternate %d\n", prefix,
|
---|
130 | (int) descriptor->interface_number,
|
---|
131 | usb_str_class(descriptor->interface_class),
|
---|
132 | (int) descriptor->interface_subclass,
|
---|
133 | (int) descriptor->interface_protocol,
|
---|
134 | (int) descriptor->alternate_setting);
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void dump_descriptor_tree_brief_endpoint(const char *prefix,
|
---|
138 | usb_standard_endpoint_descriptor_t *descriptor)
|
---|
139 | {
|
---|
140 | usb_endpoint_t endpoint_no = descriptor->endpoint_address & 0xF;
|
---|
141 | usb_transfer_type_t transfer = descriptor->attributes & 0x3;
|
---|
142 | usb_direction_t direction = descriptor->endpoint_address & 0x80
|
---|
143 | ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
|
---|
144 | printf("%sEndpoint #%d (%s %s, %zu)\n", prefix,
|
---|
145 | endpoint_no, usb_str_transfer_type(transfer),
|
---|
146 | direction == USB_DIRECTION_IN ? "in" : "out",
|
---|
147 | (size_t) descriptor->max_packet_size);
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void dump_descriptor_tree_brief_hid(const char *prefix,
|
---|
151 | usb_standard_hid_descriptor_t *descriptor)
|
---|
152 | {
|
---|
153 | printf("%sHID (country %d, %d descriptors)\n", prefix,
|
---|
154 | (int) descriptor->country_code,
|
---|
155 | (int) descriptor->class_desc_count);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | static void dump_descriptor_tree_brief_callback(uint8_t *descriptor,
|
---|
160 | size_t depth, void *arg)
|
---|
161 | {
|
---|
162 | const char *indent = get_indent(depth + 1);
|
---|
163 |
|
---|
164 | int descr_type = -1;
|
---|
165 | size_t descr_size = descriptor[0];
|
---|
166 | if (descr_size > 0) {
|
---|
167 | descr_type = descriptor[1];
|
---|
168 | }
|
---|
169 |
|
---|
170 | switch (descr_type) {
|
---|
171 |
|
---|
172 | #define _BRANCH(type_enum, descriptor_type, callback) \
|
---|
173 | case type_enum: \
|
---|
174 | if (descr_size >= sizeof(descriptor_type)) { \
|
---|
175 | callback(indent, (descriptor_type *) descriptor); \
|
---|
176 | } else { \
|
---|
177 | descr_type = -1; \
|
---|
178 | } \
|
---|
179 | break;
|
---|
180 |
|
---|
181 | _BRANCH(USB_DESCTYPE_DEVICE,
|
---|
182 | usb_standard_device_descriptor_t,
|
---|
183 | dump_descriptor_tree_brief_device);
|
---|
184 | _BRANCH(USB_DESCTYPE_CONFIGURATION,
|
---|
185 | usb_standard_configuration_descriptor_t,
|
---|
186 | dump_descriptor_tree_brief_configuration);
|
---|
187 | _BRANCH(USB_DESCTYPE_INTERFACE,
|
---|
188 | usb_standard_interface_descriptor_t,
|
---|
189 | dump_descriptor_tree_brief_interface);
|
---|
190 | _BRANCH(USB_DESCTYPE_ENDPOINT,
|
---|
191 | usb_standard_endpoint_descriptor_t,
|
---|
192 | dump_descriptor_tree_brief_endpoint);
|
---|
193 | _BRANCH(USB_DESCTYPE_HID,
|
---|
194 | usb_standard_hid_descriptor_t,
|
---|
195 | dump_descriptor_tree_brief_hid);
|
---|
196 |
|
---|
197 | default:
|
---|
198 | break;
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (descr_type == -1) {
|
---|
202 | printf("%sInvalid descriptor.\n", indent);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | void dump_descriptor_tree_brief(usbinfo_device_t *dev)
|
---|
207 | {
|
---|
208 | dump_descriptor_tree_brief_callback((uint8_t *)&dev->device_descriptor,
|
---|
209 | (size_t) -1, NULL);
|
---|
210 | usb_dp_walk_simple(dev->full_configuration_descriptor,
|
---|
211 | dev->full_configuration_descriptor_size,
|
---|
212 | usb_dp_standard_descriptor_nesting,
|
---|
213 | dump_descriptor_tree_brief_callback,
|
---|
214 | NULL);
|
---|
215 | }
|
---|
216 |
|
---|
217 | void dump_strings(usbinfo_device_t *dev)
|
---|
218 | {
|
---|
219 | /* Get supported languages. */
|
---|
220 | l18_win_locales_t *langs;
|
---|
221 | size_t langs_count;
|
---|
222 | int rc = usb_request_get_supported_languages(&dev->ctrl_pipe,
|
---|
223 | &langs, &langs_count);
|
---|
224 | if (rc != EOK) {
|
---|
225 | fprintf(stderr,
|
---|
226 | NAME ": failed to get list of supported languages: %s.\n",
|
---|
227 | str_error(rc));
|
---|
228 | return;
|
---|
229 | }
|
---|
230 |
|
---|
231 | printf("%sString languages (%zu):", get_indent(0), langs_count);
|
---|
232 | size_t i;
|
---|
233 | for (i = 0; i < langs_count; i++) {
|
---|
234 | printf(" 0x%04x", (int) langs[i]);
|
---|
235 | }
|
---|
236 | printf(".\n");
|
---|
237 |
|
---|
238 | /* Get all strings and dump them. */
|
---|
239 | for (i = 0; i < langs_count; i++) {
|
---|
240 | l18_win_locales_t lang = langs[i];
|
---|
241 |
|
---|
242 | printf("%sStrings in %s:\n", get_indent(0),
|
---|
243 | str_l18_win_locale(lang));
|
---|
244 | /*
|
---|
245 | * Try only the first 15 strings
|
---|
246 | * (typically, device will not have much more anyway).
|
---|
247 | */
|
---|
248 | size_t idx;
|
---|
249 | for (idx = 1; idx < 0x0F; idx++) {
|
---|
250 | char *string;
|
---|
251 | rc = usb_request_get_string(&dev->ctrl_pipe, idx, lang,
|
---|
252 | &string);
|
---|
253 | if (rc != EOK) {
|
---|
254 | continue;
|
---|
255 | }
|
---|
256 | printf("%sString #%zu: \"%s\"\n", get_indent(1),
|
---|
257 | idx, string);
|
---|
258 | free(string);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** @}
|
---|
264 | */
|
---|