1 /* $FreeBSD: head/sys/dev/usb/usb_parse.c 250204 2013-05-03 09:23:06Z hselasky $ */
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/stdint.h>
28 #include <sys/param.h>
29 #include <sys/queue.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/unistd.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
44 #include <bus/u4b/usb.h>
45 #include <bus/u4b/usbdi.h>
46 #include <bus/u4b/usbdi_util.h>
48 #define USB_DEBUG_VAR usb_debug
50 #include <bus/u4b/usb_core.h>
51 #include <bus/u4b/usb_debug.h>
53 /*------------------------------------------------------------------------*
56 * This function is the safe way to iterate across the USB config
57 * descriptor. It contains several checks against invalid
58 * descriptors. If the "desc" argument passed to this function is
59 * "NULL" the first descriptor, if any, will be returned.
62 * NULL: End of descriptors
63 * Else: Next descriptor after "desc"
64 *------------------------------------------------------------------------*/
65 struct usb_descriptor
*
66 usb_desc_foreach(struct usb_config_descriptor
*cd
,
67 struct usb_descriptor
*_desc
)
78 /* We assume that the "wTotalLength" has been checked. */
79 start
= (uint8_t *)cd
;
80 end
= start
+ UGETW(cd
->wTotalLength
);
81 desc
= (uint8_t *)_desc
;
83 /* Get start of next USB descriptor. */
87 desc
= desc
+ desc
[0];
89 /* Check that the next USB descriptor is within the range. */
90 if ((desc
< start
) || (desc
>= end
))
91 return (NULL
); /* out of range, or EOD */
93 /* Check that the second next USB descriptor is within range. */
94 desc_next
= desc
+ desc
[0];
95 if ((desc_next
< start
) || (desc_next
> end
))
96 return (NULL
); /* out of range */
98 /* Check minimum descriptor length. */
100 return (NULL
); /* too short descriptor */
102 /* Return start of next descriptor. */
103 return ((struct usb_descriptor
*)desc
);
106 /*------------------------------------------------------------------------*
109 * This function will iterate the interface descriptors in the config
110 * descriptor. The parse state structure should be zeroed before
111 * calling this function the first time.
114 * NULL: End of descriptors
115 * Else: A valid interface descriptor
116 *------------------------------------------------------------------------*/
117 struct usb_interface_descriptor
*
118 usb_idesc_foreach(struct usb_config_descriptor
*cd
,
119 struct usb_idesc_parse_state
*ps
)
121 struct usb_interface_descriptor
*id
;
124 /* retrieve current descriptor */
125 id
= (struct usb_interface_descriptor
*)ps
->desc
;
126 /* default is to start a new interface */
130 id
= (struct usb_interface_descriptor
*)
131 usb_desc_foreach(cd
, (struct usb_descriptor
*)id
);
134 if ((id
->bDescriptorType
== UDESC_INTERFACE
) &&
135 (id
->bLength
>= sizeof(*id
))) {
136 if (ps
->iface_no_last
== id
->bInterfaceNumber
)
138 ps
->iface_no_last
= id
->bInterfaceNumber
;
143 if (ps
->desc
== NULL
) {
144 /* first time or zero descriptors */
145 } else if (new_iface
) {
148 ps
->iface_index_alt
= 0;
150 /* new alternate interface */
151 ps
->iface_index_alt
++;
153 #if (USB_IFACE_MAX <= 0)
154 #error "USB_IFACE_MAX must be defined greater than zero"
156 /* check for too many interfaces */
157 if (ps
->iface_index
>= USB_IFACE_MAX
) {
158 DPRINTF("Interface limit reached\n");
162 /* store and return current descriptor */
163 ps
->desc
= (struct usb_descriptor
*)id
;
167 /*------------------------------------------------------------------------*
170 * This function will iterate all the endpoint descriptors within an
171 * interface descriptor. Starting value for the "ped" argument should
172 * be a valid interface descriptor.
175 * NULL: End of descriptors
176 * Else: A valid endpoint descriptor
177 *------------------------------------------------------------------------*/
178 struct usb_endpoint_descriptor
*
179 usb_edesc_foreach(struct usb_config_descriptor
*cd
,
180 struct usb_endpoint_descriptor
*ped
)
182 struct usb_descriptor
*desc
;
184 desc
= ((struct usb_descriptor
*)ped
);
186 while ((desc
= usb_desc_foreach(cd
, desc
))) {
187 if (desc
->bDescriptorType
== UDESC_INTERFACE
) {
190 if (desc
->bDescriptorType
== UDESC_ENDPOINT
) {
191 if (desc
->bLength
< sizeof(*ped
)) {
192 /* endpoint descriptor is invalid */
195 return ((struct usb_endpoint_descriptor
*)desc
);
201 /*------------------------------------------------------------------------*
202 * usb_ed_comp_foreach
204 * This function will iterate all the endpoint companion descriptors
205 * within an endpoint descriptor in an interface descriptor. Starting
206 * value for the "ped" argument should be a valid endpoint companion
210 * NULL: End of descriptors
211 * Else: A valid endpoint companion descriptor
212 *------------------------------------------------------------------------*/
213 struct usb_endpoint_ss_comp_descriptor
*
214 usb_ed_comp_foreach(struct usb_config_descriptor
*cd
,
215 struct usb_endpoint_ss_comp_descriptor
*ped
)
217 struct usb_descriptor
*desc
;
219 desc
= ((struct usb_descriptor
*)ped
);
221 while ((desc
= usb_desc_foreach(cd
, desc
))) {
222 if (desc
->bDescriptorType
== UDESC_INTERFACE
)
224 if (desc
->bDescriptorType
== UDESC_ENDPOINT
)
226 if (desc
->bDescriptorType
== UDESC_ENDPOINT_SS_COMP
) {
227 if (desc
->bLength
< sizeof(*ped
)) {
228 /* endpoint companion descriptor is invalid */
231 return ((struct usb_endpoint_ss_comp_descriptor
*)desc
);
237 /*------------------------------------------------------------------------*
238 * usbd_get_no_descriptors
240 * This function will count the total number of descriptors in the
241 * configuration descriptor of type "type".
242 *------------------------------------------------------------------------*/
244 usbd_get_no_descriptors(struct usb_config_descriptor
*cd
, uint8_t type
)
246 struct usb_descriptor
*desc
= NULL
;
249 while ((desc
= usb_desc_foreach(cd
, desc
))) {
250 if (desc
->bDescriptorType
== type
) {
259 /*------------------------------------------------------------------------*
263 * Number of alternate settings for the given interface descriptor
264 * pointer. If the USB descriptor is corrupt, the returned value can
265 * be greater than the actual number of alternate settings.
266 *------------------------------------------------------------------------*/
268 usbd_get_no_alts(struct usb_config_descriptor
*cd
,
269 struct usb_interface_descriptor
*id
)
271 struct usb_descriptor
*desc
;
275 /* Reset interface count */
279 /* Get the interface number */
281 ifaceno
= id
->bInterfaceNumber
;
283 /* Iterate all the USB descriptors */
286 while ((desc
= usb_desc_foreach(cd
, desc
))) {
287 if ((desc
->bDescriptorType
== UDESC_INTERFACE
) &&
288 (desc
->bLength
>= sizeof(*id
))) {
289 id
= (struct usb_interface_descriptor
*)desc
;
290 if (id
->bInterfaceNumber
== ifaceno
) {