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/condvar.h> 37 #include <sys/sysctl.h> 38 #include <sys/unistd.h> 39 #include <sys/callout.h> 40 #include <sys/malloc.h> 41 #include <sys/priv.h> 42 43 #include <bus/u4b/usb.h> 44 #include <bus/u4b/usbdi.h> 45 #include <bus/u4b/usbdi_util.h> 46 47 #define USB_DEBUG_VAR usb_debug 48 49 #include <bus/u4b/usb_core.h> 50 #include <bus/u4b/usb_debug.h> 51 52 /*------------------------------------------------------------------------* 53 * usb_desc_foreach 54 * 55 * This function is the safe way to iterate across the USB config 56 * descriptor. It contains several checks against invalid 57 * descriptors. If the "desc" argument passed to this function is 58 * "NULL" the first descriptor, if any, will be returned. 59 * 60 * Return values: 61 * NULL: End of descriptors 62 * Else: Next descriptor after "desc" 63 *------------------------------------------------------------------------*/ 64 struct usb_descriptor * 65 usb_desc_foreach(struct usb_config_descriptor *cd, 66 struct usb_descriptor *_desc) 67 { 68 uint8_t *desc_next; 69 uint8_t *start; 70 uint8_t *end; 71 uint8_t *desc; 72 73 /* be NULL safe */ 74 if (cd == NULL) 75 return (NULL); 76 77 /* We assume that the "wTotalLength" has been checked. */ 78 start = (uint8_t *)cd; 79 end = start + UGETW(cd->wTotalLength); 80 desc = (uint8_t *)_desc; 81 82 /* Get start of next USB descriptor. */ 83 if (desc == NULL) 84 desc = start; 85 else 86 desc = desc + desc[0]; 87 88 /* Check that the next USB descriptor is within the range. */ 89 if ((desc < start) || (desc >= end)) 90 return (NULL); /* out of range, or EOD */ 91 92 /* Check that the second next USB descriptor is within range. */ 93 desc_next = desc + desc[0]; 94 if ((desc_next < start) || (desc_next > end)) 95 return (NULL); /* out of range */ 96 97 /* Check minimum descriptor length. */ 98 if (desc[0] < 3) 99 return (NULL); /* too short descriptor */ 100 101 /* Return start of next descriptor. */ 102 return ((struct usb_descriptor *)desc); 103 } 104 105 /*------------------------------------------------------------------------* 106 * usb_idesc_foreach 107 * 108 * This function will iterate the interface descriptors in the config 109 * descriptor. The parse state structure should be zeroed before 110 * calling this function the first time. 111 * 112 * Return values: 113 * NULL: End of descriptors 114 * Else: A valid interface descriptor 115 *------------------------------------------------------------------------*/ 116 struct usb_interface_descriptor * 117 usb_idesc_foreach(struct usb_config_descriptor *cd, 118 struct usb_idesc_parse_state *ps) 119 { 120 struct usb_interface_descriptor *id; 121 uint8_t new_iface; 122 123 /* retrieve current descriptor */ 124 id = (struct usb_interface_descriptor *)ps->desc; 125 /* default is to start a new interface */ 126 new_iface = 1; 127 128 while (1) { 129 id = (struct usb_interface_descriptor *) 130 usb_desc_foreach(cd, (struct usb_descriptor *)id); 131 if (id == NULL) 132 break; 133 if ((id->bDescriptorType == UDESC_INTERFACE) && 134 (id->bLength >= sizeof(*id))) { 135 if (ps->iface_no_last == id->bInterfaceNumber) 136 new_iface = 0; 137 ps->iface_no_last = id->bInterfaceNumber; 138 break; 139 } 140 } 141 142 if (ps->desc == NULL) { 143 /* first time or zero descriptors */ 144 } else if (new_iface) { 145 /* new interface */ 146 ps->iface_index ++; 147 ps->iface_index_alt = 0; 148 } else { 149 /* new alternate interface */ 150 ps->iface_index_alt ++; 151 } 152 #if (USB_IFACE_MAX <= 0) 153 #error "USB_IFACE_MAX must be defined greater than zero" 154 #endif 155 /* check for too many interfaces */ 156 if (ps->iface_index >= USB_IFACE_MAX) { 157 DPRINTF("Interface limit reached\n"); 158 id = NULL; 159 } 160 161 /* store and return current descriptor */ 162 ps->desc = (struct usb_descriptor *)id; 163 return (id); 164 } 165 166 /*------------------------------------------------------------------------* 167 * usb_edesc_foreach 168 * 169 * This function will iterate all the endpoint descriptors within an 170 * interface descriptor. Starting value for the "ped" argument should 171 * be a valid interface descriptor. 172 * 173 * Return values: 174 * NULL: End of descriptors 175 * Else: A valid endpoint descriptor 176 *------------------------------------------------------------------------*/ 177 struct usb_endpoint_descriptor * 178 usb_edesc_foreach(struct usb_config_descriptor *cd, 179 struct usb_endpoint_descriptor *ped) 180 { 181 struct usb_descriptor *desc; 182 183 desc = ((struct usb_descriptor *)ped); 184 185 while ((desc = usb_desc_foreach(cd, desc))) { 186 if (desc->bDescriptorType == UDESC_INTERFACE) { 187 break; 188 } 189 if (desc->bDescriptorType == UDESC_ENDPOINT) { 190 if (desc->bLength < sizeof(*ped)) { 191 /* endpoint descriptor is invalid */ 192 break; 193 } 194 return ((struct usb_endpoint_descriptor *)desc); 195 } 196 } 197 return (NULL); 198 } 199 200 /*------------------------------------------------------------------------* 201 * usb_ed_comp_foreach 202 * 203 * This function will iterate all the endpoint companion descriptors 204 * within an endpoint descriptor in an interface descriptor. Starting 205 * value for the "ped" argument should be a valid endpoint companion 206 * descriptor. 207 * 208 * Return values: 209 * NULL: End of descriptors 210 * Else: A valid endpoint companion descriptor 211 *------------------------------------------------------------------------*/ 212 struct usb_endpoint_ss_comp_descriptor * 213 usb_ed_comp_foreach(struct usb_config_descriptor *cd, 214 struct usb_endpoint_ss_comp_descriptor *ped) 215 { 216 struct usb_descriptor *desc; 217 218 desc = ((struct usb_descriptor *)ped); 219 220 while ((desc = usb_desc_foreach(cd, desc))) { 221 if (desc->bDescriptorType == UDESC_INTERFACE) 222 break; 223 if (desc->bDescriptorType == UDESC_ENDPOINT) 224 break; 225 if (desc->bDescriptorType == UDESC_ENDPOINT_SS_COMP) { 226 if (desc->bLength < sizeof(*ped)) { 227 /* endpoint companion descriptor is invalid */ 228 break; 229 } 230 return ((struct usb_endpoint_ss_comp_descriptor *)desc); 231 } 232 } 233 return (NULL); 234 } 235 236 /*------------------------------------------------------------------------* 237 * usbd_get_no_descriptors 238 * 239 * This function will count the total number of descriptors in the 240 * configuration descriptor of type "type". 241 *------------------------------------------------------------------------*/ 242 uint8_t 243 usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type) 244 { 245 struct usb_descriptor *desc = NULL; 246 uint8_t count = 0; 247 248 while ((desc = usb_desc_foreach(cd, desc))) { 249 if (desc->bDescriptorType == type) { 250 count++; 251 if (count == 0xFF) 252 break; /* crazy */ 253 } 254 } 255 return (count); 256 } 257 258 /*------------------------------------------------------------------------* 259 * usbd_get_no_alts 260 * 261 * Return value: 262 * Number of alternate settings for the given interface descriptor 263 * pointer. If the USB descriptor is corrupt, the returned value can 264 * be greater than the actual number of alternate settings. 265 *------------------------------------------------------------------------*/ 266 uint8_t 267 usbd_get_no_alts(struct usb_config_descriptor *cd, 268 struct usb_interface_descriptor *id) 269 { 270 struct usb_descriptor *desc; 271 uint8_t n; 272 uint8_t ifaceno; 273 274 /* Reset interface count */ 275 276 n = 0; 277 278 /* Get the interface number */ 279 280 ifaceno = id->bInterfaceNumber; 281 282 /* Iterate all the USB descriptors */ 283 284 desc = NULL; 285 while ((desc = usb_desc_foreach(cd, desc))) { 286 if ((desc->bDescriptorType == UDESC_INTERFACE) && 287 (desc->bLength >= sizeof(*id))) { 288 id = (struct usb_interface_descriptor *)desc; 289 if (id->bInterfaceNumber == ifaceno) { 290 n++; 291 if (n == 0xFF) 292 break; /* crazy */ 293 } 294 } 295 } 296 return (n); 297 } 298