source: mainline/uspace/lib/usbdev/src/dp.c@ 3cdaa7f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3cdaa7f was 7f70d1c, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

libusb: print also superspeed ep companion descriptor

  • Property mode set to 100644
File size: 9.5 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 libusbdev
30 * @{
31 */
32/**
33 * @file
34 * USB descriptor parser (implementation).
35 *
36 * The descriptor parser is a generic parser for structure, where individual
37 * items are stored in single buffer and each item begins with length followed
38 * by type. These types are organized into tree hierarchy.
39 *
40 * The parser is able of only two actions: find first child and find next
41 * sibling.
42 */
43#include <usb/dev/dp.h>
44#include <usb/descriptor.h>
45
46#include <assert.h>
47#include <errno.h>
48#include <stdlib.h>
49#include <stdbool.h>
50#include <stddef.h>
51#include <stdint.h>
52
53#define NESTING(parentname, childname) \
54 { \
55 .child = USB_DESCTYPE_##childname, \
56 .parent = USB_DESCTYPE_##parentname, \
57 }
58#define LAST_NESTING { -1, -1 }
59
60/** Nesting of standard USB descriptors. */
61const usb_dp_descriptor_nesting_t usb_dp_standard_descriptor_nesting[] = {
62 NESTING(CONFIGURATION, INTERFACE),
63 NESTING(INTERFACE, ENDPOINT),
64 NESTING(ENDPOINT, SSPEED_EP_COMPANION),
65 NESTING(INTERFACE, HUB),
66 NESTING(INTERFACE, HID),
67 NESTING(HID, HID_REPORT),
68 LAST_NESTING
69};
70
71#undef NESTING
72#undef LAST_NESTING
73
74/** Tells whether pointer points inside descriptor data.
75 *
76 * @param data Parser data.
77 * @param ptr Pointer to be verified.
78 * @return Whether @p ptr points inside <code>data->data</code> field.
79 */
80static bool is_valid_descriptor_pointer(const usb_dp_parser_data_t *data,
81 const uint8_t *ptr)
82{
83 if (ptr == NULL) {
84 return false;
85 }
86
87 if (ptr < data->data) {
88 return false;
89 }
90
91 if ((size_t)(ptr - data->data) >= data->size) {
92 return false;
93 }
94
95 return true;
96}
97
98/** Get next descriptor regardless of the nesting.
99 *
100 * @param data Parser data.
101 * @param current Pointer to current descriptor.
102 * @return Pointer to start of next descriptor.
103 * @retval NULL Invalid input or no next descriptor.
104 */
105static const uint8_t *get_next_descriptor(const usb_dp_parser_data_t *data,
106 const uint8_t *current)
107{
108 assert(is_valid_descriptor_pointer(data, current));
109
110 const uint8_t current_length = *current;
111 const uint8_t *next = current + current_length;
112
113 if (!is_valid_descriptor_pointer(data, next)) {
114 return NULL;
115 }
116
117 return next;
118}
119
120/** Get descriptor type.
121 *
122 * @see usb_descriptor_type_t
123 *
124 * @param data Parser data.
125 * @param start Pointer to start of the descriptor.
126 * @return Descriptor type.
127 * @retval -1 Invalid input.
128 */
129static int get_descriptor_type(const usb_dp_parser_data_t *data, const uint8_t *start)
130{
131 if (start == NULL) {
132 return -1;
133 }
134
135 start++;
136 if (!is_valid_descriptor_pointer(data, start)) {
137 return -1;
138 } else {
139 return (int) (*start);
140 }
141}
142
143/** Tells whether descriptors could be nested.
144 *
145 * @param parser Parser.
146 * @param child Child descriptor type.
147 * @param parent Parent descriptor type.
148 * @return Whether @p child could be child of @p parent.
149 */
150static bool is_nested_descriptor_type(const usb_dp_parser_t *parser,
151 int child, int parent)
152{
153 const usb_dp_descriptor_nesting_t *nesting = parser->nesting;
154 while ((nesting->child > 0) && (nesting->parent > 0)) {
155 if ((nesting->child == child) && (nesting->parent == parent)) {
156 return true;
157 }
158 nesting++;
159 }
160 return false;
161}
162
163/** Tells whether descriptors could be nested.
164 *
165 * @param parser Parser.
166 * @param data Parser data.
167 * @param child Pointer to child descriptor.
168 * @param parent Pointer to parent descriptor.
169 * @return Whether @p child could be child of @p parent.
170 */
171static bool is_nested_descriptor(const usb_dp_parser_t *parser,
172 const usb_dp_parser_data_t *data, const uint8_t *child, const uint8_t *parent)
173{
174 return is_nested_descriptor_type(parser,
175 get_descriptor_type(data, child),
176 get_descriptor_type(data, parent));
177}
178
179/** Find first nested descriptor of given parent.
180 *
181 * @param parser Parser.
182 * @param data Parser data.
183 * @param parent Pointer to the beginning of parent descriptor.
184 * @return Pointer to the beginning of the first nested (child) descriptor.
185 * @retval NULL No child descriptor found.
186 * @retval NULL Invalid input.
187 */
188const uint8_t *usb_dp_get_nested_descriptor(const usb_dp_parser_t *parser,
189 const usb_dp_parser_data_t *data, const uint8_t *parent)
190{
191 if (!is_valid_descriptor_pointer(data, parent)) {
192 return NULL;
193 }
194
195 const uint8_t *next = get_next_descriptor(data, parent);
196 if (next == NULL) {
197 return NULL;
198 }
199
200 if (is_nested_descriptor(parser, data, next, parent)) {
201 return next;
202 } else {
203 return NULL;
204 }
205}
206
207/** Skip all nested descriptors.
208 *
209 * @param parser Parser.
210 * @param data Parser data.
211 * @param parent Pointer to the beginning of parent descriptor.
212 * @return Pointer to first non-child descriptor.
213 * @retval NULL No next descriptor.
214 * @retval NULL Invalid input.
215 */
216static const uint8_t *skip_nested_descriptors(const usb_dp_parser_t *parser,
217 const usb_dp_parser_data_t *data, const uint8_t *parent)
218{
219 const uint8_t *child =
220 usb_dp_get_nested_descriptor(parser, data, parent);
221 if (child == NULL) {
222 return get_next_descriptor(data, parent);
223 }
224 const uint8_t *next_child =
225 skip_nested_descriptors(parser, data, child);
226 while (is_nested_descriptor(parser, data, next_child, parent)) {
227 next_child = skip_nested_descriptors(parser, data, next_child);
228 }
229
230 return next_child;
231}
232
233/** Get sibling descriptor.
234 *
235 * @param parser Parser.
236 * @param data Parser data.
237 * @param parent Pointer to common parent descriptor.
238 * @param sibling Left sibling.
239 * @return Pointer to first right sibling of @p sibling.
240 * @retval NULL No sibling exist.
241 * @retval NULL Invalid input.
242 */
243const uint8_t *usb_dp_get_sibling_descriptor(
244 const usb_dp_parser_t *parser, const usb_dp_parser_data_t *data,
245 const uint8_t *parent, const uint8_t *sibling)
246{
247 if (!is_valid_descriptor_pointer(data, parent)
248 || !is_valid_descriptor_pointer(data, sibling)) {
249 return NULL;
250 }
251
252 const uint8_t *possible_sibling =
253 skip_nested_descriptors(parser, data, sibling);
254 if (possible_sibling == NULL) {
255 return NULL;
256 }
257
258 int parent_type = get_descriptor_type(data, parent);
259 int possible_sibling_type = get_descriptor_type(data, possible_sibling);
260 if (is_nested_descriptor_type(parser, possible_sibling_type, parent_type)) {
261 return possible_sibling;
262 } else {
263 return NULL;
264 }
265}
266
267/** Browser of the descriptor tree.
268 *
269 * @see usb_dp_walk_simple
270 *
271 * @param parser Descriptor parser.
272 * @param data Data for descriptor parser.
273 * @param root Pointer to current root of the tree.
274 * @param depth Current nesting depth.
275 * @param callback Callback for each found descriptor.
276 * @param arg Custom (user) argument.
277 */
278static void usb_dp_browse_simple_internal(const usb_dp_parser_t *parser,
279 const usb_dp_parser_data_t *data, const uint8_t *root, size_t depth,
280 void (*callback)(const uint8_t *, size_t, void *), void *arg)
281{
282 if (root == NULL) {
283 return;
284 }
285 callback(root, depth, arg);
286 const uint8_t *child = usb_dp_get_nested_descriptor(parser, data, root);
287 do {
288 usb_dp_browse_simple_internal(parser, data, child, depth + 1,
289 callback, arg);
290 child = usb_dp_get_sibling_descriptor(parser, data,
291 root, child);
292 } while (child != NULL);
293}
294
295/** Browse flatten descriptor tree.
296 *
297 * The callback is called with following arguments: pointer to the start
298 * of the descriptor (somewhere inside @p descriptors), depth of the nesting
299 * (starting from 0 for the first descriptor) and the custom argument.
300 * Note that the size of the descriptor is not passed because it can
301 * be read from the first byte of the descriptor.
302 *
303 * @param descriptors Descriptor data.
304 * @param descriptors_size Size of descriptor data (in bytes).
305 * @param descriptor_nesting Possible descriptor nesting.
306 * @param callback Callback for each found descriptor.
307 * @param arg Custom (user) argument.
308 */
309void usb_dp_walk_simple(const uint8_t *descriptors, size_t descriptors_size,
310 const usb_dp_descriptor_nesting_t *descriptor_nesting,
311 walk_callback_t callback, void *arg)
312{
313 if ((descriptors == NULL) || (descriptors_size == 0)
314 || (descriptor_nesting == NULL) || (callback == NULL)) {
315 return;
316 }
317
318 const usb_dp_parser_data_t data = {
319 .data = descriptors,
320 .size = descriptors_size,
321 .arg = NULL
322 };
323
324 const usb_dp_parser_t parser = {
325 .nesting = descriptor_nesting
326 };
327
328 usb_dp_browse_simple_internal(&parser, &data, descriptors,
329 0, callback, arg);
330}
331
332/** @}
333 */
Note: See TracBrowser for help on using the repository browser.