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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e4f2e0 was 3e4f2e0, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

libusb divided into sublibraries

Also removed address keeper test from tester as it is useless.

Directory reorganization of the include/ will follow.

  • Property mode set to 100644
File size: 10.4 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 <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
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_hid(FILE *, const char *, const char *,
64 const uint8_t *, size_t);
65static void usb_dump_descriptor_hub(FILE *, const char *, const char *,
66 const uint8_t *, size_t);
67static void usb_dump_descriptor_generic(FILE *, const char *, const char *,
68 const uint8_t *, size_t);
69
70/** Descriptor dumpers mapping. */
71static descriptor_dump_t descriptor_dumpers[] = {
72 { USB_DESCTYPE_DEVICE, usb_dump_descriptor_device },
73 { USB_DESCTYPE_CONFIGURATION, usb_dump_descriptor_configuration },
74 { USB_DESCTYPE_STRING, usb_dump_descriptor_string },
75 { USB_DESCTYPE_INTERFACE, usb_dump_descriptor_interface },
76 { USB_DESCTYPE_ENDPOINT, usb_dump_descriptor_endpoint },
77 { USB_DESCTYPE_HID, usb_dump_descriptor_hid },
78 { USB_DESCTYPE_HUB, usb_dump_descriptor_hub },
79 { -1, usb_dump_descriptor_generic },
80 { -1, NULL }
81};
82
83/** Dumps standard USB descriptor.
84 * The @p line_suffix must contain the newline <code>\\n</code> character.
85 * When @p line_suffix or @p line_prefix is NULL, they are substitued with
86 * default values
87 * (<code> - </code> for prefix and line termination for suffix).
88 *
89 * @param output Output file stream to dump descriptor to.
90 * @param line_prefix Prefix for each line of output.
91 * @param line_suffix Suffix of each line of output.
92 * @param descriptor Actual descriptor.
93 * @param descriptor_length Descriptor size.
94 */
95void usb_dump_standard_descriptor(FILE *output,
96 const char *line_prefix, const char *line_suffix,
97 const uint8_t *descriptor, size_t descriptor_length)
98{
99 if (descriptor_length < 2) {
100 return;
101 }
102 int type = descriptor[1];
103
104 descriptor_dump_t *dumper = descriptor_dumpers;
105 while (dumper->dump != NULL) {
106 if ((dumper->id == type) || (dumper->id < 0)) {
107 dumper->dump(output, line_prefix, line_suffix,
108 descriptor, descriptor_length);
109 return;
110 }
111 dumper++;
112 }
113}
114
115/** Prints single line of USB descriptor dump.
116 * @warning This macro abuses heavily the naming conventions used
117 * by all dumping functions (i.e. names for output file stream (@c output) and
118 * line prefix and suffix (@c line_prefix and @c line_suffix respectively))-
119 *
120 * @param fmt Formatting string.
121 */
122#define PRINTLINE(fmt, ...) \
123 fprintf(output, "%s" fmt "%s", \
124 line_prefix ? line_prefix : " - ", \
125 __VA_ARGS__, \
126 line_suffix ? line_suffix : "\n")
127
128#define BCD_INT(a) (((unsigned int)(a)) / 256)
129#define BCD_FRAC(a) (((unsigned int)(a)) % 256)
130
131#define BCD_FMT "%x.%x"
132#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
133
134static void usb_dump_descriptor_device(FILE *output,
135 const char *line_prefix, const char *line_suffix,
136 const uint8_t *descriptor, size_t descriptor_length)
137{
138 usb_standard_device_descriptor_t *d
139 = (usb_standard_device_descriptor_t *) descriptor;
140 if (descriptor_length < sizeof(*d)) {
141 return;
142 }
143
144 PRINTLINE("bLength = %d", d->length);
145 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
146 PRINTLINE("bcdUSB = %d (" BCD_FMT ")", d->usb_spec_version,
147 BCD_ARGS(d->usb_spec_version));
148 PRINTLINE("bDeviceClass = 0x%02x", d->device_class);
149 PRINTLINE("bDeviceSubClass = 0x%02x", d->device_subclass);
150 PRINTLINE("bDeviceProtocol = 0x%02x", d->device_protocol);
151 PRINTLINE("bMaxPacketSize0 = %d", d->max_packet_size);
152 PRINTLINE("idVendor = 0x%04x", d->vendor_id);
153 PRINTLINE("idProduct = 0x%04x", d->product_id);
154 PRINTLINE("bcdDevice = %d", d->device_version);
155 PRINTLINE("iManufacturer = %d", d->str_manufacturer);
156 PRINTLINE("iProduct = %d", d->str_product);
157 PRINTLINE("iSerialNumber = %d", d->str_serial_number);
158 PRINTLINE("bNumConfigurations = %d", d->configuration_count);
159}
160
161static void usb_dump_descriptor_configuration(FILE *output,
162 const char *line_prefix, const char *line_suffix,
163 const uint8_t *descriptor, size_t descriptor_length)
164{
165 usb_standard_configuration_descriptor_t *d
166 = (usb_standard_configuration_descriptor_t *) descriptor;
167 if (descriptor_length < sizeof(*d)) {
168 return;
169 }
170
171 bool self_powered = d->attributes & 64;
172 bool remote_wakeup = d->attributes & 32;
173
174 PRINTLINE("bLength = %d", d->length);
175 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
176 PRINTLINE("wTotalLength = %d", d->total_length);
177 PRINTLINE("bNumInterfaces = %d", d->interface_count);
178 PRINTLINE("bConfigurationValue = %d", d->configuration_number);
179 PRINTLINE("iConfiguration = %d", d->str_configuration);
180 PRINTLINE("bmAttributes = %d [%s%s%s]", d->attributes,
181 self_powered ? "self-powered" : "",
182 (self_powered & remote_wakeup) ? ", " : "",
183 remote_wakeup ? "remote-wakeup" : "");
184 PRINTLINE("MaxPower = %d (%dmA)", d->max_power,
185 2 * d->max_power);
186}
187
188static void usb_dump_descriptor_interface(FILE *output,
189 const char *line_prefix, const char *line_suffix,
190 const uint8_t *descriptor, size_t descriptor_length)
191{
192 usb_standard_interface_descriptor_t *d
193 = (usb_standard_interface_descriptor_t *) descriptor;
194 if (descriptor_length < sizeof(*d)) {
195 return;
196 }
197
198 PRINTLINE("bLength = %d", d->length);
199 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
200 PRINTLINE("bInterfaceNumber = %d", d->interface_number);
201 PRINTLINE("bAlternateSetting = %d", d->alternate_setting);
202 PRINTLINE("bNumEndpoints = %d", d->endpoint_count);
203 PRINTLINE("bInterfaceClass = %s", d->interface_class == 0
204 ? "reserved (0)" : usb_str_class(d->interface_class));
205 PRINTLINE("bInterfaceSubClass = %d", d->interface_subclass);
206 PRINTLINE("bInterfaceProtocol = %d", d->interface_protocol);
207 PRINTLINE("iInterface = %d", d->str_interface);
208}
209
210static void usb_dump_descriptor_string(FILE *output,
211 const char *line_prefix, const char *line_suffix,
212 const uint8_t *descriptor, size_t descriptor_length)
213{
214}
215
216static void usb_dump_descriptor_endpoint(FILE *output,
217 const char *line_prefix, const char *line_suffix,
218 const uint8_t *descriptor, size_t descriptor_length)
219{
220 usb_standard_endpoint_descriptor_t *d
221 = (usb_standard_endpoint_descriptor_t *) descriptor;
222 if (descriptor_length < sizeof(*d)) {
223 return;
224 }
225
226 int endpoint = d->endpoint_address & 15;
227 usb_direction_t direction = d->endpoint_address & 128
228 ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
229 usb_transfer_type_t transfer_type = d->attributes & 3;
230
231 PRINTLINE("bLength = %d", d->length);
232 PRINTLINE("bDescriptorType = 0x%02X", d->descriptor_type);
233 PRINTLINE("bEndpointAddress = 0x%02X [%d, %s]",
234 d->endpoint_address, endpoint,
235 direction == USB_DIRECTION_IN ? "in" : "out");
236 PRINTLINE("bmAttributes = %d [%s]", d->attributes,
237 usb_str_transfer_type(transfer_type));
238 PRINTLINE("wMaxPacketSize = %d", d->max_packet_size);
239 PRINTLINE("bInterval = %dms", d->poll_interval);
240}
241
242static void usb_dump_descriptor_hid(FILE *output,
243 const char *line_prefix, const char *line_suffix,
244 const uint8_t *descriptor, size_t descriptor_length)
245{
246 usb_standard_hid_descriptor_t *d
247 = (usb_standard_hid_descriptor_t *) descriptor;
248 if (descriptor_length < sizeof(*d)) {
249 return;
250 }
251
252 PRINTLINE("bLength = %d", d->length);
253 PRINTLINE("bDescriptorType = 0x%02x", d->descriptor_type);
254 PRINTLINE("bcdHID = %d (" BCD_FMT ")", d->spec_release,
255 BCD_ARGS(d->spec_release));
256 PRINTLINE("bCountryCode = %d", d->country_code);
257 PRINTLINE("bNumDescriptors = %d", d->class_desc_count);
258 PRINTLINE("bDescriptorType = %d", d->report_desc_info.type);
259 PRINTLINE("wDescriptorLength = %d", d->report_desc_info.length);
260
261 /* Print info about report descriptors. */
262 size_t i;
263 size_t count = (descriptor_length - sizeof(*d))
264 / sizeof(usb_standard_hid_class_descriptor_info_t);
265 usb_standard_hid_class_descriptor_info_t *d2
266 = (usb_standard_hid_class_descriptor_info_t *)
267 (descriptor + sizeof(*d));
268 for (i = 0; i < count; i++, d2++) {
269 PRINTLINE("bDescriptorType = %d", d2->type);
270 PRINTLINE("wDescriptorLength = %d", d2->length);
271 }
272}
273
274static void usb_dump_descriptor_hub(FILE *output,
275 const char *line_prefix, const char *line_suffix,
276 const uint8_t *descriptor, size_t descriptor_length)
277{
278 /* TODO */
279}
280
281static void usb_dump_descriptor_generic(FILE *output,
282 const char *line_prefix, const char *line_suffix,
283 const uint8_t *descriptor, size_t descriptor_length)
284{
285 /* TODO */
286}
287
288
289/**
290 * @}
291 */
Note: See TracBrowser for help on using the repository browser.