source: mainline/uspace/lib/usbdev/src/dp.c@ 7d20461

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7d20461 was 3e6a98c5, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Standards-compliant boolean type.

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