3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef USB_GLOBAL_INCLUDE_FILE
30 #include USB_GLOBAL_INCLUDE_FILE
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
55 #define USB_DEBUG_VAR usb_debug
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_debug.h>
59 #endif /* USB_GLOBAL_INCLUDE_FILE */
61 /*------------------------------------------------------------------------*
64 * This function is the safe way to iterate across the USB config
65 * descriptor. It contains several checks against invalid
66 * descriptors. If the "desc" argument passed to this function is
67 * "NULL" the first descriptor, if any, will be returned.
70 * NULL: End of descriptors
71 * Else: Next descriptor after "desc"
72 *------------------------------------------------------------------------*/
73 struct usb_descriptor *
74 usb_desc_foreach(struct usb_config_descriptor *cd,
75 struct usb_descriptor *_desc)
86 /* We assume that the "wTotalLength" has been checked. */
87 start = (uint8_t *)cd;
88 end = start + UGETW(cd->wTotalLength);
89 desc = (uint8_t *)_desc;
91 /* Get start of next USB descriptor. */
95 desc = desc + desc[0];
97 /* Check that the next USB descriptor is within the range. */
98 if ((desc < start) || (desc >= end))
99 return (NULL); /* out of range, or EOD */
101 /* Check that the second next USB descriptor is within range. */
102 desc_next = desc + desc[0];
103 if ((desc_next < start) || (desc_next > end))
104 return (NULL); /* out of range */
106 /* Check minimum descriptor length. */
108 return (NULL); /* too short descriptor */
110 /* Return start of next descriptor. */
111 return ((struct usb_descriptor *)desc);
114 /*------------------------------------------------------------------------*
117 * This function will iterate the interface descriptors in the config
118 * descriptor. The parse state structure should be zeroed before
119 * calling this function the first time.
122 * NULL: End of descriptors
123 * Else: A valid interface descriptor
124 *------------------------------------------------------------------------*/
125 struct usb_interface_descriptor *
126 usb_idesc_foreach(struct usb_config_descriptor *cd,
127 struct usb_idesc_parse_state *ps)
129 struct usb_interface_descriptor *id;
132 /* retrieve current descriptor */
133 id = (struct usb_interface_descriptor *)ps->desc;
134 /* default is to start a new interface */
138 id = (struct usb_interface_descriptor *)
139 usb_desc_foreach(cd, (struct usb_descriptor *)id);
142 if ((id->bDescriptorType == UDESC_INTERFACE) &&
143 (id->bLength >= sizeof(*id))) {
144 if (ps->iface_no_last == id->bInterfaceNumber)
146 ps->iface_no_last = id->bInterfaceNumber;
151 if (ps->desc == NULL) {
152 /* first time or zero descriptors */
153 } else if (new_iface) {
156 ps->iface_index_alt = 0;
158 /* new alternate interface */
159 ps->iface_index_alt ++;
161 #if (USB_IFACE_MAX <= 0)
162 #error "USB_IFACE_MAX must be defined greater than zero"
164 /* check for too many interfaces */
165 if (ps->iface_index >= USB_IFACE_MAX) {
166 DPRINTF("Interface limit reached\n");
170 /* store and return current descriptor */
171 ps->desc = (struct usb_descriptor *)id;
175 /*------------------------------------------------------------------------*
178 * This function will iterate all the endpoint descriptors within an
179 * interface descriptor. Starting value for the "ped" argument should
180 * be a valid interface descriptor.
183 * NULL: End of descriptors
184 * Else: A valid endpoint descriptor
185 *------------------------------------------------------------------------*/
186 struct usb_endpoint_descriptor *
187 usb_edesc_foreach(struct usb_config_descriptor *cd,
188 struct usb_endpoint_descriptor *ped)
190 struct usb_descriptor *desc;
192 desc = ((struct usb_descriptor *)ped);
194 while ((desc = usb_desc_foreach(cd, desc))) {
195 if (desc->bDescriptorType == UDESC_INTERFACE) {
198 if (desc->bDescriptorType == UDESC_ENDPOINT) {
199 if (desc->bLength < sizeof(*ped)) {
200 /* endpoint descriptor is invalid */
203 return ((struct usb_endpoint_descriptor *)desc);
209 /*------------------------------------------------------------------------*
210 * usb_ed_comp_foreach
212 * This function will iterate all the endpoint companion descriptors
213 * within an endpoint descriptor in an interface descriptor. Starting
214 * value for the "ped" argument should be a valid endpoint companion
218 * NULL: End of descriptors
219 * Else: A valid endpoint companion descriptor
220 *------------------------------------------------------------------------*/
221 struct usb_endpoint_ss_comp_descriptor *
222 usb_ed_comp_foreach(struct usb_config_descriptor *cd,
223 struct usb_endpoint_ss_comp_descriptor *ped)
225 struct usb_descriptor *desc;
227 desc = ((struct usb_descriptor *)ped);
229 while ((desc = usb_desc_foreach(cd, desc))) {
230 if (desc->bDescriptorType == UDESC_INTERFACE)
232 if (desc->bDescriptorType == UDESC_ENDPOINT)
234 if (desc->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
235 if (desc->bLength < sizeof(*ped)) {
236 /* endpoint companion descriptor is invalid */
239 return ((struct usb_endpoint_ss_comp_descriptor *)desc);
245 /*------------------------------------------------------------------------*
246 * usbd_get_no_descriptors
248 * This function will count the total number of descriptors in the
249 * configuration descriptor of type "type".
250 *------------------------------------------------------------------------*/
252 usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type)
254 struct usb_descriptor *desc = NULL;
257 while ((desc = usb_desc_foreach(cd, desc))) {
258 if (desc->bDescriptorType == type) {
267 /*------------------------------------------------------------------------*
271 * Number of alternate settings for the given interface descriptor
272 * pointer. If the USB descriptor is corrupt, the returned value can
273 * be greater than the actual number of alternate settings.
274 *------------------------------------------------------------------------*/
276 usbd_get_no_alts(struct usb_config_descriptor *cd,
277 struct usb_interface_descriptor *id)
279 struct usb_descriptor *desc;
283 /* Reset interface count */
287 /* Get the interface number */
289 ifaceno = id->bInterfaceNumber;
291 /* Iterate all the USB descriptors */
294 while ((desc = usb_desc_foreach(cd, desc))) {
295 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
296 (desc->bLength >= sizeof(*id))) {
297 id = (struct usb_interface_descriptor *)desc;
298 if (id->bInterfaceNumber == ifaceno) {