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 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #endif /* USB_GLOBAL_INCLUDE_FILE */
54 /*------------------------------------------------------------------------*
57 * This function is the safe way to iterate across the USB config
58 * descriptor. It contains several checks against invalid
59 * descriptors. If the "desc" argument passed to this function is
60 * "NULL" the first descriptor, if any, will be returned.
63 * NULL: End of descriptors
64 * Else: Next descriptor after "desc"
65 *------------------------------------------------------------------------*/
66 struct usb_descriptor *
67 usb_desc_foreach(struct usb_config_descriptor *cd,
68 struct usb_descriptor *_desc)
79 /* We assume that the "wTotalLength" has been checked. */
80 start = (uint8_t *)cd;
81 end = start + UGETW(cd->wTotalLength);
82 desc = (uint8_t *)_desc;
84 /* Get start of next USB descriptor. */
88 desc = desc + desc[0];
90 /* Check that the next USB descriptor is within the range. */
91 if ((desc < start) || (desc >= end))
92 return (NULL); /* out of range, or EOD */
94 /* Check that the second next USB descriptor is within range. */
95 desc_next = desc + desc[0];
96 if ((desc_next < start) || (desc_next > end))
97 return (NULL); /* out of range */
99 /* Check minimum descriptor length. */
101 return (NULL); /* too short descriptor */
103 /* Return start of next descriptor. */
104 return ((struct usb_descriptor *)desc);
107 /*------------------------------------------------------------------------*
110 * This function will iterate the interface descriptors in the config
111 * descriptor. The parse state structure should be zeroed before
112 * calling this function the first time.
115 * NULL: End of descriptors
116 * Else: A valid interface descriptor
117 *------------------------------------------------------------------------*/
118 struct usb_interface_descriptor *
119 usb_idesc_foreach(struct usb_config_descriptor *cd,
120 struct usb_idesc_parse_state *ps)
122 struct usb_interface_descriptor *id;
125 /* retrieve current descriptor */
126 id = (struct usb_interface_descriptor *)ps->desc;
127 /* default is to start a new interface */
131 id = (struct usb_interface_descriptor *)
132 usb_desc_foreach(cd, (struct usb_descriptor *)id);
135 if ((id->bDescriptorType == UDESC_INTERFACE) &&
136 (id->bLength >= sizeof(*id))) {
137 if (ps->iface_no_last == id->bInterfaceNumber)
139 ps->iface_no_last = id->bInterfaceNumber;
144 if (ps->desc == NULL) {
146 } else if (new_iface) {
149 ps->iface_index_alt = 0;
151 /* new alternate interface */
152 ps->iface_index_alt ++;
155 /* store and return current descriptor */
156 ps->desc = (struct usb_descriptor *)id;
160 /*------------------------------------------------------------------------*
163 * This function will iterate all the endpoint descriptors within an
164 * interface descriptor. Starting value for the "ped" argument should
165 * be a valid interface descriptor.
168 * NULL: End of descriptors
169 * Else: A valid endpoint descriptor
170 *------------------------------------------------------------------------*/
171 struct usb_endpoint_descriptor *
172 usb_edesc_foreach(struct usb_config_descriptor *cd,
173 struct usb_endpoint_descriptor *ped)
175 struct usb_descriptor *desc;
177 desc = ((struct usb_descriptor *)ped);
179 while ((desc = usb_desc_foreach(cd, desc))) {
180 if (desc->bDescriptorType == UDESC_INTERFACE) {
183 if (desc->bDescriptorType == UDESC_ENDPOINT) {
184 if (desc->bLength < sizeof(*ped)) {
185 /* endpoint descriptor is invalid */
188 return ((struct usb_endpoint_descriptor *)desc);
194 /*------------------------------------------------------------------------*
195 * usb_ed_comp_foreach
197 * This function will iterate all the endpoint companion descriptors
198 * within an endpoint descriptor in an interface descriptor. Starting
199 * value for the "ped" argument should be a valid endpoint companion
203 * NULL: End of descriptors
204 * Else: A valid endpoint companion descriptor
205 *------------------------------------------------------------------------*/
206 struct usb_endpoint_ss_comp_descriptor *
207 usb_ed_comp_foreach(struct usb_config_descriptor *cd,
208 struct usb_endpoint_ss_comp_descriptor *ped)
210 struct usb_descriptor *desc;
212 desc = ((struct usb_descriptor *)ped);
214 while ((desc = usb_desc_foreach(cd, desc))) {
215 if (desc->bDescriptorType == UDESC_INTERFACE)
217 if (desc->bDescriptorType == UDESC_ENDPOINT)
219 if (desc->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
220 if (desc->bLength < sizeof(*ped)) {
221 /* endpoint companion descriptor is invalid */
224 return ((struct usb_endpoint_ss_comp_descriptor *)desc);
230 /*------------------------------------------------------------------------*
231 * usbd_get_no_descriptors
233 * This function will count the total number of descriptors in the
234 * configuration descriptor of type "type".
235 *------------------------------------------------------------------------*/
237 usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type)
239 struct usb_descriptor *desc = NULL;
242 while ((desc = usb_desc_foreach(cd, desc))) {
243 if (desc->bDescriptorType == type) {
252 /*------------------------------------------------------------------------*
256 * Number of alternate settings for the given interface descriptor
257 * pointer. If the USB descriptor is corrupt, the returned value can
258 * be greater than the actual number of alternate settings.
259 *------------------------------------------------------------------------*/
261 usbd_get_no_alts(struct usb_config_descriptor *cd,
262 struct usb_interface_descriptor *id)
264 struct usb_descriptor *desc;
268 /* Reset interface count */
272 /* Get the interface number */
274 ifaceno = id->bInterfaceNumber;
276 /* Iterate all the USB descriptors */
279 while ((desc = usb_desc_foreach(cd, desc))) {
280 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
281 (desc->bLength >= sizeof(*id))) {
282 id = (struct usb_interface_descriptor *)desc;
283 if (id->bInterfaceNumber == ifaceno) {