xref: /freebsd/sys/dev/usb/usb_parse.c (revision d2b99310b17979e99c03251de3f9bc08dd9219d1)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
27*d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
28*d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
29*d2b99310SHans Petter Selasky #else
30ed6d949aSAndrew Thompson #include <sys/stdint.h>
31ed6d949aSAndrew Thompson #include <sys/stddef.h>
32ed6d949aSAndrew Thompson #include <sys/param.h>
33ed6d949aSAndrew Thompson #include <sys/queue.h>
34ed6d949aSAndrew Thompson #include <sys/types.h>
35ed6d949aSAndrew Thompson #include <sys/systm.h>
36ed6d949aSAndrew Thompson #include <sys/kernel.h>
37ed6d949aSAndrew Thompson #include <sys/bus.h>
38ed6d949aSAndrew Thompson #include <sys/module.h>
39ed6d949aSAndrew Thompson #include <sys/lock.h>
40ed6d949aSAndrew Thompson #include <sys/mutex.h>
41ed6d949aSAndrew Thompson #include <sys/condvar.h>
42ed6d949aSAndrew Thompson #include <sys/sysctl.h>
43ed6d949aSAndrew Thompson #include <sys/sx.h>
44ed6d949aSAndrew Thompson #include <sys/unistd.h>
45ed6d949aSAndrew Thompson #include <sys/callout.h>
46ed6d949aSAndrew Thompson #include <sys/malloc.h>
47ed6d949aSAndrew Thompson #include <sys/priv.h>
4802ac6454SAndrew Thompson 
49ed6d949aSAndrew Thompson #include <dev/usb/usb.h>
50ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
51ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
52*d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
5302ac6454SAndrew Thompson 
5402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
55a593f6b8SAndrew Thompson  *	usb_desc_foreach
5602ac6454SAndrew Thompson  *
5702ac6454SAndrew Thompson  * This function is the safe way to iterate across the USB config
5802ac6454SAndrew Thompson  * descriptor. It contains several checks against invalid
5902ac6454SAndrew Thompson  * descriptors. If the "desc" argument passed to this function is
6002ac6454SAndrew Thompson  * "NULL" the first descriptor, if any, will be returned.
6102ac6454SAndrew Thompson  *
6202ac6454SAndrew Thompson  * Return values:
6302ac6454SAndrew Thompson  *   NULL: End of descriptors
6402ac6454SAndrew Thompson  *   Else: Next descriptor after "desc"
6502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
66760bc48eSAndrew Thompson struct usb_descriptor *
67a593f6b8SAndrew Thompson usb_desc_foreach(struct usb_config_descriptor *cd,
68760bc48eSAndrew Thompson     struct usb_descriptor *_desc)
6902ac6454SAndrew Thompson {
7002ac6454SAndrew Thompson 	uint8_t *desc_next;
7102ac6454SAndrew Thompson 	uint8_t *start;
7202ac6454SAndrew Thompson 	uint8_t *end;
7302ac6454SAndrew Thompson 	uint8_t *desc;
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson 	/* be NULL safe */
7602ac6454SAndrew Thompson 	if (cd == NULL)
7702ac6454SAndrew Thompson 		return (NULL);
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson 	/* We assume that the "wTotalLength" has been checked. */
8002ac6454SAndrew Thompson 	start = (uint8_t *)cd;
8102ac6454SAndrew Thompson 	end = start + UGETW(cd->wTotalLength);
8202ac6454SAndrew Thompson 	desc = (uint8_t *)_desc;
8302ac6454SAndrew Thompson 
8402ac6454SAndrew Thompson 	/* Get start of next USB descriptor. */
8502ac6454SAndrew Thompson 	if (desc == NULL)
8602ac6454SAndrew Thompson 		desc = start;
8702ac6454SAndrew Thompson 	else
8802ac6454SAndrew Thompson 		desc = desc + desc[0];
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson 	/* Check that the next USB descriptor is within the range. */
9102ac6454SAndrew Thompson 	if ((desc < start) || (desc >= end))
9202ac6454SAndrew Thompson 		return (NULL);		/* out of range, or EOD */
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson 	/* Check that the second next USB descriptor is within range. */
9502ac6454SAndrew Thompson 	desc_next = desc + desc[0];
9602ac6454SAndrew Thompson 	if ((desc_next < start) || (desc_next > end))
9702ac6454SAndrew Thompson 		return (NULL);		/* out of range */
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson 	/* Check minimum descriptor length. */
10002ac6454SAndrew Thompson 	if (desc[0] < 3)
10102ac6454SAndrew Thompson 		return (NULL);		/* too short descriptor */
10202ac6454SAndrew Thompson 
10302ac6454SAndrew Thompson 	/* Return start of next descriptor. */
104760bc48eSAndrew Thompson 	return ((struct usb_descriptor *)desc);
10502ac6454SAndrew Thompson }
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
108a593f6b8SAndrew Thompson  *	usb_idesc_foreach
10902ac6454SAndrew Thompson  *
110bdd41206SAndrew Thompson  * This function will iterate the interface descriptors in the config
111bdd41206SAndrew Thompson  * descriptor. The parse state structure should be zeroed before
112bdd41206SAndrew Thompson  * calling this function the first time.
11302ac6454SAndrew Thompson  *
11402ac6454SAndrew Thompson  * Return values:
11502ac6454SAndrew Thompson  *   NULL: End of descriptors
11602ac6454SAndrew Thompson  *   Else: A valid interface descriptor
11702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
118760bc48eSAndrew Thompson struct usb_interface_descriptor *
119a593f6b8SAndrew Thompson usb_idesc_foreach(struct usb_config_descriptor *cd,
120760bc48eSAndrew Thompson     struct usb_idesc_parse_state *ps)
12102ac6454SAndrew Thompson {
122760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
123bdd41206SAndrew Thompson 	uint8_t new_iface;
12402ac6454SAndrew Thompson 
125bdd41206SAndrew Thompson 	/* retrieve current descriptor */
126760bc48eSAndrew Thompson 	id = (struct usb_interface_descriptor *)ps->desc;
127bdd41206SAndrew Thompson 	/* default is to start a new interface */
128bdd41206SAndrew Thompson 	new_iface = 1;
12902ac6454SAndrew Thompson 
130bdd41206SAndrew Thompson 	while (1) {
131760bc48eSAndrew Thompson 		id = (struct usb_interface_descriptor *)
132a593f6b8SAndrew Thompson 		    usb_desc_foreach(cd, (struct usb_descriptor *)id);
133bdd41206SAndrew Thompson 		if (id == NULL)
134bdd41206SAndrew Thompson 			break;
135bdd41206SAndrew Thompson 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
136bdd41206SAndrew Thompson 		    (id->bLength >= sizeof(*id))) {
137bdd41206SAndrew Thompson 			if (ps->iface_no_last == id->bInterfaceNumber)
138bdd41206SAndrew Thompson 				new_iface = 0;
139bdd41206SAndrew Thompson 			ps->iface_no_last = id->bInterfaceNumber;
140bdd41206SAndrew Thompson 			break;
141bdd41206SAndrew Thompson 		}
142bdd41206SAndrew Thompson 	}
14302ac6454SAndrew Thompson 
144bdd41206SAndrew Thompson 	if (ps->desc == NULL) {
145bdd41206SAndrew Thompson 		/* first time */
146bdd41206SAndrew Thompson 	} else if (new_iface) {
147bdd41206SAndrew Thompson 		/* new interface */
148bdd41206SAndrew Thompson 		ps->iface_index ++;
149bdd41206SAndrew Thompson 		ps->iface_index_alt = 0;
15002ac6454SAndrew Thompson 	} else {
151bdd41206SAndrew Thompson 		/* new alternate interface */
152bdd41206SAndrew Thompson 		ps->iface_index_alt ++;
15302ac6454SAndrew Thompson 	}
15402ac6454SAndrew Thompson 
155bdd41206SAndrew Thompson 	/* store and return current descriptor */
156760bc48eSAndrew Thompson 	ps->desc = (struct usb_descriptor *)id;
15702ac6454SAndrew Thompson 	return (id);
15802ac6454SAndrew Thompson }
15902ac6454SAndrew Thompson 
16002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
161a593f6b8SAndrew Thompson  *	usb_edesc_foreach
16202ac6454SAndrew Thompson  *
163bdd41206SAndrew Thompson  * This function will iterate all the endpoint descriptors within an
164bdd41206SAndrew Thompson  * interface descriptor. Starting value for the "ped" argument should
165bdd41206SAndrew Thompson  * be a valid interface descriptor.
16602ac6454SAndrew Thompson  *
16702ac6454SAndrew Thompson  * Return values:
16802ac6454SAndrew Thompson  *   NULL: End of descriptors
16902ac6454SAndrew Thompson  *   Else: A valid endpoint descriptor
17002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
171760bc48eSAndrew Thompson struct usb_endpoint_descriptor *
172a593f6b8SAndrew Thompson usb_edesc_foreach(struct usb_config_descriptor *cd,
173760bc48eSAndrew Thompson     struct usb_endpoint_descriptor *ped)
17402ac6454SAndrew Thompson {
175760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
17602ac6454SAndrew Thompson 
177760bc48eSAndrew Thompson 	desc = ((struct usb_descriptor *)ped);
17802ac6454SAndrew Thompson 
179a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
18002ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
18102ac6454SAndrew Thompson 			break;
18202ac6454SAndrew Thompson 		}
18302ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_ENDPOINT) {
184bdd41206SAndrew Thompson 			if (desc->bLength < sizeof(*ped)) {
185963169b4SHans Petter Selasky 				/* endpoint descriptor is invalid */
18602ac6454SAndrew Thompson 				break;
18702ac6454SAndrew Thompson 			}
188760bc48eSAndrew Thompson 			return ((struct usb_endpoint_descriptor *)desc);
18902ac6454SAndrew Thompson 		}
19002ac6454SAndrew Thompson 	}
19102ac6454SAndrew Thompson 	return (NULL);
19202ac6454SAndrew Thompson }
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
195963169b4SHans Petter Selasky  *	usb_ed_comp_foreach
196963169b4SHans Petter Selasky  *
197963169b4SHans Petter Selasky  * This function will iterate all the endpoint companion descriptors
198963169b4SHans Petter Selasky  * within an endpoint descriptor in an interface descriptor. Starting
199963169b4SHans Petter Selasky  * value for the "ped" argument should be a valid endpoint companion
200963169b4SHans Petter Selasky  * descriptor.
201963169b4SHans Petter Selasky  *
202963169b4SHans Petter Selasky  * Return values:
203963169b4SHans Petter Selasky  *   NULL: End of descriptors
204963169b4SHans Petter Selasky  *   Else: A valid endpoint companion descriptor
205963169b4SHans Petter Selasky  *------------------------------------------------------------------------*/
206963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *
207963169b4SHans Petter Selasky usb_ed_comp_foreach(struct usb_config_descriptor *cd,
208963169b4SHans Petter Selasky     struct usb_endpoint_ss_comp_descriptor *ped)
209963169b4SHans Petter Selasky {
210963169b4SHans Petter Selasky 	struct usb_descriptor *desc;
211963169b4SHans Petter Selasky 
212963169b4SHans Petter Selasky 	desc = ((struct usb_descriptor *)ped);
213963169b4SHans Petter Selasky 
214963169b4SHans Petter Selasky 	while ((desc = usb_desc_foreach(cd, desc))) {
215963169b4SHans Petter Selasky 		if (desc->bDescriptorType == UDESC_INTERFACE)
216963169b4SHans Petter Selasky 			break;
217963169b4SHans Petter Selasky 		if (desc->bDescriptorType == UDESC_ENDPOINT)
218963169b4SHans Petter Selasky 			break;
219963169b4SHans Petter Selasky 		if (desc->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
220963169b4SHans Petter Selasky 			if (desc->bLength < sizeof(*ped)) {
221963169b4SHans Petter Selasky 				/* endpoint companion descriptor is invalid */
222963169b4SHans Petter Selasky 				break;
223963169b4SHans Petter Selasky 			}
224963169b4SHans Petter Selasky 			return ((struct usb_endpoint_ss_comp_descriptor *)desc);
225963169b4SHans Petter Selasky 		}
226963169b4SHans Petter Selasky 	}
227963169b4SHans Petter Selasky 	return (NULL);
228963169b4SHans Petter Selasky }
229963169b4SHans Petter Selasky 
230963169b4SHans Petter Selasky /*------------------------------------------------------------------------*
231a593f6b8SAndrew Thompson  *	usbd_get_no_descriptors
23202ac6454SAndrew Thompson  *
233bdd41206SAndrew Thompson  * This function will count the total number of descriptors in the
234bdd41206SAndrew Thompson  * configuration descriptor of type "type".
23502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
236bdd41206SAndrew Thompson uint8_t
237a593f6b8SAndrew Thompson usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type)
23802ac6454SAndrew Thompson {
239760bc48eSAndrew Thompson 	struct usb_descriptor *desc = NULL;
240bdd41206SAndrew Thompson 	uint8_t count = 0;
24102ac6454SAndrew Thompson 
242a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
243bdd41206SAndrew Thompson 		if (desc->bDescriptorType == type) {
24402ac6454SAndrew Thompson 			count++;
245bdd41206SAndrew Thompson 			if (count == 0xFF)
246bdd41206SAndrew Thompson 				break;			/* crazy */
24702ac6454SAndrew Thompson 		}
24802ac6454SAndrew Thompson 	}
24902ac6454SAndrew Thompson 	return (count);
25002ac6454SAndrew Thompson }
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
253a593f6b8SAndrew Thompson  *	usbd_get_no_alts
25402ac6454SAndrew Thompson  *
25502ac6454SAndrew Thompson  * Return value:
256bd73b187SAlfred Perlstein  *   Number of alternate settings for the given interface descriptor
257bd73b187SAlfred Perlstein  *   pointer. If the USB descriptor is corrupt, the returned value can
258bd73b187SAlfred Perlstein  *   be greater than the actual number of alternate settings.
25902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
260bdd41206SAndrew Thompson uint8_t
261a593f6b8SAndrew Thompson usbd_get_no_alts(struct usb_config_descriptor *cd,
262760bc48eSAndrew Thompson     struct usb_interface_descriptor *id)
26302ac6454SAndrew Thompson {
264760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
265bd73b187SAlfred Perlstein 	uint8_t n;
266bdd41206SAndrew Thompson 	uint8_t ifaceno;
267bdd41206SAndrew Thompson 
268bd73b187SAlfred Perlstein 	/* Reset interface count */
269bd73b187SAlfred Perlstein 
270bd73b187SAlfred Perlstein 	n = 0;
271bd73b187SAlfred Perlstein 
272bd73b187SAlfred Perlstein 	/* Get the interface number */
273bd73b187SAlfred Perlstein 
274bdd41206SAndrew Thompson 	ifaceno = id->bInterfaceNumber;
275bdd41206SAndrew Thompson 
276bd73b187SAlfred Perlstein 	/* Iterate all the USB descriptors */
27702ac6454SAndrew Thompson 
278bd73b187SAlfred Perlstein 	desc = NULL;
279a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
28002ac6454SAndrew Thompson 		if ((desc->bDescriptorType == UDESC_INTERFACE) &&
28102ac6454SAndrew Thompson 		    (desc->bLength >= sizeof(*id))) {
282760bc48eSAndrew Thompson 			id = (struct usb_interface_descriptor *)desc;
28302ac6454SAndrew Thompson 			if (id->bInterfaceNumber == ifaceno) {
28402ac6454SAndrew Thompson 				n++;
285bdd41206SAndrew Thompson 				if (n == 0xFF)
286bdd41206SAndrew Thompson 					break;		/* crazy */
287bd73b187SAlfred Perlstein 			}
28802ac6454SAndrew Thompson 		}
28902ac6454SAndrew Thompson 	}
29002ac6454SAndrew Thompson 	return (n);
29102ac6454SAndrew Thompson }
292