1 /* $FreeBSD$ */ 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 <dev/usb/usb.h> 28 #include <dev/usb/usb_mfunc.h> 29 30 #include <dev/usb/usb_core.h> 31 #include <dev/usb/usb_parse.h> 32 33 /*------------------------------------------------------------------------* 34 * usb2_desc_foreach 35 * 36 * This function is the safe way to iterate across the USB config 37 * descriptor. It contains several checks against invalid 38 * descriptors. If the "desc" argument passed to this function is 39 * "NULL" the first descriptor, if any, will be returned. 40 * 41 * Return values: 42 * NULL: End of descriptors 43 * Else: Next descriptor after "desc" 44 *------------------------------------------------------------------------*/ 45 struct usb2_descriptor * 46 usb2_desc_foreach(struct usb2_config_descriptor *cd, 47 struct usb2_descriptor *_desc) 48 { 49 uint8_t *desc_next; 50 uint8_t *start; 51 uint8_t *end; 52 uint8_t *desc; 53 54 /* be NULL safe */ 55 if (cd == NULL) 56 return (NULL); 57 58 /* We assume that the "wTotalLength" has been checked. */ 59 start = (uint8_t *)cd; 60 end = start + UGETW(cd->wTotalLength); 61 desc = (uint8_t *)_desc; 62 63 /* Get start of next USB descriptor. */ 64 if (desc == NULL) 65 desc = start; 66 else 67 desc = desc + desc[0]; 68 69 /* Check that the next USB descriptor is within the range. */ 70 if ((desc < start) || (desc >= end)) 71 return (NULL); /* out of range, or EOD */ 72 73 /* Check that the second next USB descriptor is within range. */ 74 desc_next = desc + desc[0]; 75 if ((desc_next < start) || (desc_next > end)) 76 return (NULL); /* out of range */ 77 78 /* Check minimum descriptor length. */ 79 if (desc[0] < 3) 80 return (NULL); /* too short descriptor */ 81 82 /* Return start of next descriptor. */ 83 return ((struct usb2_descriptor *)desc); 84 } 85 86 /*------------------------------------------------------------------------* 87 * usb2_idesc_foreach 88 * 89 * This function will iterate the interface descriptors in the config 90 * descriptor. The parse state structure should be zeroed before 91 * calling this function the first time. 92 * 93 * Return values: 94 * NULL: End of descriptors 95 * Else: A valid interface descriptor 96 *------------------------------------------------------------------------*/ 97 struct usb2_interface_descriptor * 98 usb2_idesc_foreach(struct usb2_config_descriptor *cd, 99 struct usb2_idesc_parse_state *ps) 100 { 101 struct usb2_interface_descriptor *id; 102 uint8_t new_iface; 103 104 /* retrieve current descriptor */ 105 id = (struct usb2_interface_descriptor *)ps->desc; 106 /* default is to start a new interface */ 107 new_iface = 1; 108 109 while (1) { 110 id = (struct usb2_interface_descriptor *) 111 usb2_desc_foreach(cd, (struct usb2_descriptor *)id); 112 if (id == NULL) 113 break; 114 if ((id->bDescriptorType == UDESC_INTERFACE) && 115 (id->bLength >= sizeof(*id))) { 116 if (ps->iface_no_last == id->bInterfaceNumber) 117 new_iface = 0; 118 ps->iface_no_last = id->bInterfaceNumber; 119 break; 120 } 121 } 122 123 if (ps->desc == NULL) { 124 /* first time */ 125 } else if (new_iface) { 126 /* new interface */ 127 ps->iface_index ++; 128 ps->iface_index_alt = 0; 129 } else { 130 /* new alternate interface */ 131 ps->iface_index_alt ++; 132 } 133 134 /* store and return current descriptor */ 135 ps->desc = (struct usb2_descriptor *)id; 136 return (id); 137 } 138 139 /*------------------------------------------------------------------------* 140 * usb2_edesc_foreach 141 * 142 * This function will iterate all the endpoint descriptors within an 143 * interface descriptor. Starting value for the "ped" argument should 144 * be a valid interface descriptor. 145 * 146 * Return values: 147 * NULL: End of descriptors 148 * Else: A valid endpoint descriptor 149 *------------------------------------------------------------------------*/ 150 struct usb2_endpoint_descriptor * 151 usb2_edesc_foreach(struct usb2_config_descriptor *cd, 152 struct usb2_endpoint_descriptor *ped) 153 { 154 struct usb2_descriptor *desc; 155 156 desc = ((struct usb2_descriptor *)ped); 157 158 while ((desc = usb2_desc_foreach(cd, desc))) { 159 if (desc->bDescriptorType == UDESC_INTERFACE) { 160 break; 161 } 162 if (desc->bDescriptorType == UDESC_ENDPOINT) { 163 if (desc->bLength < sizeof(*ped)) { 164 /* endpoint index is invalid */ 165 break; 166 } 167 return ((struct usb2_endpoint_descriptor *)desc); 168 } 169 } 170 return (NULL); 171 } 172 173 /*------------------------------------------------------------------------* 174 * usb2_get_no_descriptors 175 * 176 * This function will count the total number of descriptors in the 177 * configuration descriptor of type "type". 178 *------------------------------------------------------------------------*/ 179 uint8_t 180 usb2_get_no_descriptors(struct usb2_config_descriptor *cd, uint8_t type) 181 { 182 struct usb2_descriptor *desc = NULL; 183 uint8_t count = 0; 184 185 while ((desc = usb2_desc_foreach(cd, desc))) { 186 if (desc->bDescriptorType == type) { 187 count++; 188 if (count == 0xFF) 189 break; /* crazy */ 190 } 191 } 192 return (count); 193 } 194 195 /*------------------------------------------------------------------------* 196 * usb2_get_no_alts 197 * 198 * Return value: 199 * Number of alternate settings for the given interface descriptor pointer. 200 *------------------------------------------------------------------------*/ 201 uint8_t 202 usb2_get_no_alts(struct usb2_config_descriptor *cd, 203 struct usb2_interface_descriptor *id) 204 { 205 struct usb2_descriptor *desc; 206 uint8_t n = 0; 207 uint8_t ifaceno; 208 209 ifaceno = id->bInterfaceNumber; 210 211 desc = (struct usb2_descriptor *)id; 212 213 while ((desc = usb2_desc_foreach(cd, desc))) { 214 if ((desc->bDescriptorType == UDESC_INTERFACE) && 215 (desc->bLength >= sizeof(*id))) { 216 id = (struct usb2_interface_descriptor *)desc; 217 if (id->bInterfaceNumber == ifaceno) { 218 n++; 219 if (n == 0xFF) 220 break; /* crazy */ 221 } else 222 break; /* end */ 223 } 224 } 225 return (n); 226 } 227