xref: /dragonfly/sys/bus/u4b/usb_parse.c (revision a9783bc6)
1 /* $FreeBSD: head/sys/dev/usb/usb_parse.c 250204 2013-05-03 09:23:06Z hselasky $ */
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/param.h>
29 #include <sys/queue.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35 #include <sys/lock.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>
42 #include <sys/priv.h>
43 
44 #include <bus/u4b/usb.h>
45 #include <bus/u4b/usbdi.h>
46 #include <bus/u4b/usbdi_util.h>
47 
48 #define USB_DEBUG_VAR usb_debug
49 
50 #include <bus/u4b/usb_core.h>
51 #include <bus/u4b/usb_debug.h>
52 
53 /*------------------------------------------------------------------------*
54  *	usb_desc_foreach
55  *
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.
60  *
61  * Return values:
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)
68 {
69 	uint8_t *desc_next;
70 	uint8_t *start;
71 	uint8_t *end;
72 	uint8_t *desc;
73 
74 	/* be NULL safe */
75 	if (cd == NULL)
76 		return (NULL);
77 
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;
82 
83 	/* Get start of next USB descriptor. */
84 	if (desc == NULL)
85 		desc = start;
86 	else
87 		desc = desc + desc[0];
88 
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 */
92 
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 */
97 
98 	/* Check minimum descriptor length. */
99 	if (desc[0] < 3)
100 		return (NULL);		/* too short descriptor */
101 
102 	/* Return start of next descriptor. */
103 	return ((struct usb_descriptor *)desc);
104 }
105 
106 /*------------------------------------------------------------------------*
107  *	usb_idesc_foreach
108  *
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.
112  *
113  * Return values:
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)
120 {
121 	struct usb_interface_descriptor *id;
122 	uint8_t new_iface;
123 
124 	/* retrieve current descriptor */
125 	id = (struct usb_interface_descriptor *)ps->desc;
126 	/* default is to start a new interface */
127 	new_iface = 1;
128 
129 	while (1) {
130 		id = (struct usb_interface_descriptor *)
131 		    usb_desc_foreach(cd, (struct usb_descriptor *)id);
132 		if (id == NULL)
133 			break;
134 		if ((id->bDescriptorType == UDESC_INTERFACE) &&
135 		    (id->bLength >= sizeof(*id))) {
136 			if (ps->iface_no_last == id->bInterfaceNumber)
137 				new_iface = 0;
138 			ps->iface_no_last = id->bInterfaceNumber;
139 			break;
140 		}
141 	}
142 
143 	if (ps->desc == NULL) {
144 		/* first time or zero descriptors */
145 	} else if (new_iface) {
146 		/* new interface */
147 		ps->iface_index ++;
148 		ps->iface_index_alt = 0;
149 	} else {
150 		/* new alternate interface */
151 		ps->iface_index_alt ++;
152 	}
153 #if (USB_IFACE_MAX <= 0)
154 #error "USB_IFACE_MAX must be defined greater than zero"
155 #endif
156 	/* check for too many interfaces */
157 	if (ps->iface_index >= USB_IFACE_MAX) {
158 		DPRINTF("Interface limit reached\n");
159 		id = NULL;
160 	}
161 
162 	/* store and return current descriptor */
163 	ps->desc = (struct usb_descriptor *)id;
164 	return (id);
165 }
166 
167 /*------------------------------------------------------------------------*
168  *	usb_edesc_foreach
169  *
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.
173  *
174  * Return values:
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)
181 {
182 	struct usb_descriptor *desc;
183 
184 	desc = ((struct usb_descriptor *)ped);
185 
186 	while ((desc = usb_desc_foreach(cd, desc))) {
187 		if (desc->bDescriptorType == UDESC_INTERFACE) {
188 			break;
189 		}
190 		if (desc->bDescriptorType == UDESC_ENDPOINT) {
191 			if (desc->bLength < sizeof(*ped)) {
192 				/* endpoint descriptor is invalid */
193 				break;
194 			}
195 			return ((struct usb_endpoint_descriptor *)desc);
196 		}
197 	}
198 	return (NULL);
199 }
200 
201 /*------------------------------------------------------------------------*
202  *	usb_ed_comp_foreach
203  *
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
207  * descriptor.
208  *
209  * Return values:
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)
216 {
217 	struct usb_descriptor *desc;
218 
219 	desc = ((struct usb_descriptor *)ped);
220 
221 	while ((desc = usb_desc_foreach(cd, desc))) {
222 		if (desc->bDescriptorType == UDESC_INTERFACE)
223 			break;
224 		if (desc->bDescriptorType == UDESC_ENDPOINT)
225 			break;
226 		if (desc->bDescriptorType == UDESC_ENDPOINT_SS_COMP) {
227 			if (desc->bLength < sizeof(*ped)) {
228 				/* endpoint companion descriptor is invalid */
229 				break;
230 			}
231 			return ((struct usb_endpoint_ss_comp_descriptor *)desc);
232 		}
233 	}
234 	return (NULL);
235 }
236 
237 /*------------------------------------------------------------------------*
238  *	usbd_get_no_descriptors
239  *
240  * This function will count the total number of descriptors in the
241  * configuration descriptor of type "type".
242  *------------------------------------------------------------------------*/
243 uint8_t
244 usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type)
245 {
246 	struct usb_descriptor *desc = NULL;
247 	uint8_t count = 0;
248 
249 	while ((desc = usb_desc_foreach(cd, desc))) {
250 		if (desc->bDescriptorType == type) {
251 			count++;
252 			if (count == 0xFF)
253 				break;			/* crazy */
254 		}
255 	}
256 	return (count);
257 }
258 
259 /*------------------------------------------------------------------------*
260  *	usbd_get_no_alts
261  *
262  * Return value:
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  *------------------------------------------------------------------------*/
267 uint8_t
268 usbd_get_no_alts(struct usb_config_descriptor *cd,
269     struct usb_interface_descriptor *id)
270 {
271 	struct usb_descriptor *desc;
272 	uint8_t n;
273 	uint8_t ifaceno;
274 
275 	/* Reset interface count */
276 
277 	n = 0;
278 
279 	/* Get the interface number */
280 
281 	ifaceno = id->bInterfaceNumber;
282 
283 	/* Iterate all the USB descriptors */
284 
285 	desc = NULL;
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) {
291 				n++;
292 				if (n == 0xFF)
293 					break;		/* crazy */
294 			}
295 		}
296 	}
297 	return (n);
298 }
299