1 /* $FreeBSD: releng/8.3/sys/dev/usb/usb_parse.c 221125 2011-04-27 18:00:46Z jhb $ */
2 /*-
3 * Copyright (c) 2008 Hans Petter Selasky. 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 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/sx.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
44 #include <sys/priv.h>
45
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdi_util.h>
49
50
51 /*------------------------------------------------------------------------*
52 * usb_desc_foreach
53 *
54 * This function is the safe way to iterate across the USB config
55 * descriptor. It contains several checks against invalid
56 * descriptors. If the "desc" argument passed to this function is
57 * "NULL" the first descriptor, if any, will be returned.
58 *
59 * Return values:
60 * NULL: End of descriptors
61 * Else: Next descriptor after "desc"
62 *------------------------------------------------------------------------*/
63 struct usb_descriptor *
64 usb_desc_foreach(struct usb_config_descriptor *cd,
65 struct usb_descriptor *_desc)
66 {
67 uint8_t *desc_next;
68 uint8_t *start;
69 uint8_t *end;
70 uint8_t *desc;
71
72 /* be NULL safe */
73 if (cd == NULL)
74 return (NULL);
75
76 /* We assume that the "wTotalLength" has been checked. */
77 start = (uint8_t *)cd;
78 end = start + UGETW(cd->wTotalLength);
79 desc = (uint8_t *)_desc;
80
81 /* Get start of next USB descriptor. */
82 if (desc == NULL)
83 desc = start;
84 else
85 desc = desc + desc[0];
86
87 /* Check that the next USB descriptor is within the range. */
88 if ((desc < start) || (desc >= end))
89 return (NULL); /* out of range, or EOD */
90
91 /* Check that the second next USB descriptor is within range. */
92 desc_next = desc + desc[0];
93 if ((desc_next < start) || (desc_next > end))
94 return (NULL); /* out of range */
95
96 /* Check minimum descriptor length. */
97 if (desc[0] < 3)
98 return (NULL); /* too short descriptor */
99
100 /* Return start of next descriptor. */
101 return ((struct usb_descriptor *)desc);
102 }
103
104 /*------------------------------------------------------------------------*
105 * usb_idesc_foreach
106 *
107 * This function will iterate the interface descriptors in the config
108 * descriptor. The parse state structure should be zeroed before
109 * calling this function the first time.
110 *
111 * Return values:
112 * NULL: End of descriptors
113 * Else: A valid interface descriptor
114 *------------------------------------------------------------------------*/
115 struct usb_interface_descriptor *
116 usb_idesc_foreach(struct usb_config_descriptor *cd,
117 struct usb_idesc_parse_state *ps)
118 {
119 struct usb_interface_descriptor *id;
120 uint8_t new_iface;
121
122 /* retrieve current descriptor */
123 id = (struct usb_interface_descriptor *)ps->desc;
124 /* default is to start a new interface */
125 new_iface = 1;
126
127 while (1) {
128 id = (struct usb_interface_descriptor *)
129 usb_desc_foreach(cd, (struct usb_descriptor *)id);
130 if (id == NULL)
131 break;
132 if ((id->bDescriptorType == UDESC_INTERFACE) &&
133 (id->bLength >= sizeof(*id))) {
134 if (ps->iface_no_last == id->bInterfaceNumber)
135 new_iface = 0;
136 ps->iface_no_last = id->bInterfaceNumber;
137 break;
138 }
139 }
140
141 if (ps->desc == NULL) {
142 /* first time */
143 } else if (new_iface) {
144 /* new interface */
145 ps->iface_index ++;
146 ps->iface_index_alt = 0;
147 } else {
148 /* new alternate interface */
149 ps->iface_index_alt ++;
150 }
151
152 /* store and return current descriptor */
153 ps->desc = (struct usb_descriptor *)id;
154 return (id);
155 }
156
157 /*------------------------------------------------------------------------*
158 * usb_edesc_foreach
159 *
160 * This function will iterate all the endpoint descriptors within an
161 * interface descriptor. Starting value for the "ped" argument should
162 * be a valid interface descriptor.
163 *
164 * Return values:
165 * NULL: End of descriptors
166 * Else: A valid endpoint descriptor
167 *------------------------------------------------------------------------*/
168 struct usb_endpoint_descriptor *
169 usb_edesc_foreach(struct usb_config_descriptor *cd,
170 struct usb_endpoint_descriptor *ped)
171 {
172 struct usb_descriptor *desc;
173
174 desc = ((struct usb_descriptor *)ped);
175
176 while ((desc = usb_desc_foreach(cd, desc))) {
177 if (desc->bDescriptorType == UDESC_INTERFACE) {
178 break;
179 }
180 if (desc->bDescriptorType == UDESC_ENDPOINT) {
181 if (desc->bLength < sizeof(*ped)) {
182 /* endpoint descriptor is invalid */
183 break;
184 }
185 return ((struct usb_endpoint_descriptor *)desc);
186 }
187 }
188 return (NULL);
189 }
190
191 /*------------------------------------------------------------------------*
192 * usb_ed_comp_foreach
193 *
194 * This function will iterate all the endpoint companion descriptors
195 * within an endpoint descriptor in an interface descriptor. Starting
196 * value for the "ped" argument should be a valid endpoint companion
197 * descriptor.
198 *
199 * Return values:
200 * NULL: End of descriptors
201 * Else: A valid endpoint companion descriptor
202 *------------------------------------------------------------------------*/
203 struct usb_endpoint_ss_comp_descriptor *
204 usb_ed_comp_foreach(struct usb_config_descriptor *cd,
205 struct usb_endpoint_ss_comp_descriptor *ped)
206 {
207 struct usb_descriptor *desc;
208
209 desc = ((struct usb_descriptor *)ped);
210
211 while ((desc = usb_desc_foreach(cd, desc))) {
212 if (desc->bDescriptorType == UDESC_INTERFACE)
213 break;
214 if (desc->bDescriptorType == UDESC_ENDPOINT)
215 break;
216 if (desc->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
217 if (desc->bLength < sizeof(*ped)) {
218 /* endpoint companion descriptor is invalid */
219 break;
220 }
221 return ((struct usb_endpoint_ss_comp_descriptor *)desc);
222 }
223 }
224 return (NULL);
225 }
226
227 /*------------------------------------------------------------------------*
228 * usbd_get_no_descriptors
229 *
230 * This function will count the total number of descriptors in the
231 * configuration descriptor of type "type".
232 *------------------------------------------------------------------------*/
233 uint8_t
234 usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type)
235 {
236 struct usb_descriptor *desc = NULL;
237 uint8_t count = 0;
238
239 while ((desc = usb_desc_foreach(cd, desc))) {
240 if (desc->bDescriptorType == type) {
241 count++;
242 if (count == 0xFF)
243 break; /* crazy */
244 }
245 }
246 return (count);
247 }
248
249 /*------------------------------------------------------------------------*
250 * usbd_get_no_alts
251 *
252 * Return value:
253 * Number of alternate settings for the given interface descriptor
254 * pointer. If the USB descriptor is corrupt, the returned value can
255 * be greater than the actual number of alternate settings.
256 *------------------------------------------------------------------------*/
257 uint8_t
258 usbd_get_no_alts(struct usb_config_descriptor *cd,
259 struct usb_interface_descriptor *id)
260 {
261 struct usb_descriptor *desc;
262 uint8_t n;
263 uint8_t ifaceno;
264
265 /* Reset interface count */
266
267 n = 0;
268
269 /* Get the interface number */
270
271 ifaceno = id->bInterfaceNumber;
272
273 /* Iterate all the USB descriptors */
274
275 desc = NULL;
276 while ((desc = usb_desc_foreach(cd, desc))) {
277 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
278 (desc->bLength >= sizeof(*id))) {
279 id = (struct usb_interface_descriptor *)desc;
280 if (id->bInterfaceNumber == ifaceno) {
281 n++;
282 if (n == 0xFF)
283 break; /* crazy */
284 }
285 }
286 }
287 return (n);
288 }
Cache object: f63d1586f311317cb831bb0841308960
|