Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/usb.h> |
Alan Stern | 615ae11 | 2007-06-08 15:23:27 -0400 | [diff] [blame] | 2 | #include <linux/usb/ch9.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/module.h> |
| 4 | #include <linux/init.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/device.h> |
| 7 | #include <asm/byteorder.h> |
Greg KH | 6d5e825 | 2005-04-18 17:39:24 -0700 | [diff] [blame] | 8 | #include "usb.h" |
| 9 | #include "hcd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | #define USB_MAXALTSETTING 128 /* Hard limit */ |
| 12 | #define USB_MAXENDPOINTS 30 /* Hard limit */ |
| 13 | |
| 14 | #define USB_MAXCONFIG 8 /* Arbitrary limit */ |
| 15 | |
| 16 | |
| 17 | static inline const char *plural(int n) |
| 18 | { |
| 19 | return (n == 1 ? "" : "s"); |
| 20 | } |
| 21 | |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 22 | /* FIXME: this is a kludge */ |
| 23 | static int find_next_descriptor_more(unsigned char *buffer, int size, |
| 24 | int dt1, int dt2, int dt3, int *num_skipped) |
| 25 | { |
| 26 | struct usb_descriptor_header *h; |
| 27 | int n = 0; |
| 28 | unsigned char *buffer0 = buffer; |
| 29 | |
| 30 | /* Find the next descriptor of type dt1 or dt2 or dt3 */ |
| 31 | while (size > 0) { |
| 32 | h = (struct usb_descriptor_header *) buffer; |
| 33 | if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2 || |
| 34 | h->bDescriptorType == dt3) |
| 35 | break; |
| 36 | buffer += h->bLength; |
| 37 | size -= h->bLength; |
| 38 | ++n; |
| 39 | } |
| 40 | |
| 41 | /* Store the number of descriptors skipped and return the |
| 42 | * number of bytes skipped */ |
| 43 | if (num_skipped) |
| 44 | *num_skipped = n; |
| 45 | return buffer - buffer0; |
| 46 | } |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static int find_next_descriptor(unsigned char *buffer, int size, |
| 49 | int dt1, int dt2, int *num_skipped) |
| 50 | { |
| 51 | struct usb_descriptor_header *h; |
| 52 | int n = 0; |
| 53 | unsigned char *buffer0 = buffer; |
| 54 | |
| 55 | /* Find the next descriptor of type dt1 or dt2 */ |
| 56 | while (size > 0) { |
| 57 | h = (struct usb_descriptor_header *) buffer; |
| 58 | if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2) |
| 59 | break; |
| 60 | buffer += h->bLength; |
| 61 | size -= h->bLength; |
| 62 | ++n; |
| 63 | } |
| 64 | |
| 65 | /* Store the number of descriptors skipped and return the |
| 66 | * number of bytes skipped */ |
| 67 | if (num_skipped) |
| 68 | *num_skipped = n; |
| 69 | return buffer - buffer0; |
| 70 | } |
| 71 | |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 72 | static int usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno, |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 73 | int inum, int asnum, struct usb_host_endpoint *ep, |
| 74 | int num_ep, unsigned char *buffer, int size) |
| 75 | { |
| 76 | unsigned char *buffer_start = buffer; |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 77 | struct usb_ss_ep_comp_descriptor *desc; |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 78 | int retval; |
| 79 | int num_skipped; |
| 80 | int max_tx; |
| 81 | int i; |
| 82 | |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 83 | /* Allocate space for the SS endpoint companion descriptor */ |
| 84 | ep->ss_ep_comp = kzalloc(sizeof(struct usb_host_ss_ep_comp), |
| 85 | GFP_KERNEL); |
| 86 | if (!ep->ss_ep_comp) |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 87 | return -ENOMEM; |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 88 | desc = (struct usb_ss_ep_comp_descriptor *) buffer; |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 89 | if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) { |
| 90 | dev_warn(ddev, "No SuperSpeed endpoint companion for config %d " |
| 91 | " interface %d altsetting %d ep %d: " |
| 92 | "using minimum values\n", |
| 93 | cfgno, inum, asnum, ep->desc.bEndpointAddress); |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 94 | ep->ss_ep_comp->desc.bLength = USB_DT_SS_EP_COMP_SIZE; |
| 95 | ep->ss_ep_comp->desc.bDescriptorType = USB_DT_SS_ENDPOINT_COMP; |
| 96 | ep->ss_ep_comp->desc.bMaxBurst = 0; |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Leave bmAttributes as zero, which will mean no streams for |
| 99 | * bulk, and isoc won't support multiple bursts of packets. |
| 100 | * With bursts of only one packet, and a Mult of 1, the max |
| 101 | * amount of data moved per endpoint service interval is one |
| 102 | * packet. |
| 103 | */ |
| 104 | if (usb_endpoint_xfer_isoc(&ep->desc) || |
| 105 | usb_endpoint_xfer_int(&ep->desc)) |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 106 | ep->ss_ep_comp->desc.wBytesPerInterval = |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 107 | ep->desc.wMaxPacketSize; |
| 108 | /* |
| 109 | * The next descriptor is for an Endpoint or Interface, |
| 110 | * no extra descriptors to copy into the companion structure, |
| 111 | * and we didn't eat up any of the buffer. |
| 112 | */ |
| 113 | retval = 0; |
| 114 | goto valid; |
| 115 | } |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 116 | memcpy(&ep->ss_ep_comp->desc, desc, USB_DT_SS_EP_COMP_SIZE); |
| 117 | desc = &ep->ss_ep_comp->desc; |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 118 | buffer += desc->bLength; |
| 119 | size -= desc->bLength; |
| 120 | |
| 121 | /* Eat up the other descriptors we don't care about */ |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 122 | ep->ss_ep_comp->extra = buffer; |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 123 | i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, |
| 124 | USB_DT_INTERFACE, &num_skipped); |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 125 | ep->ss_ep_comp->extralen = i; |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 126 | buffer += i; |
| 127 | size -= i; |
| 128 | retval = buffer - buffer_start + i; |
| 129 | if (num_skipped > 0) |
| 130 | dev_dbg(ddev, "skipped %d descriptor%s after %s\n", |
| 131 | num_skipped, plural(num_skipped), |
| 132 | "SuperSpeed endpoint companion"); |
| 133 | |
| 134 | /* Check the various values */ |
| 135 | if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) { |
| 136 | dev_warn(ddev, "Control endpoint with bMaxBurst = %d in " |
| 137 | "config %d interface %d altsetting %d ep %d: " |
| 138 | "setting to zero\n", desc->bMaxBurst, |
| 139 | cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 140 | desc->bMaxBurst = 0; |
| 141 | } |
| 142 | if (desc->bMaxBurst > 15) { |
| 143 | dev_warn(ddev, "Endpoint with bMaxBurst = %d in " |
| 144 | "config %d interface %d altsetting %d ep %d: " |
| 145 | "setting to 15\n", desc->bMaxBurst, |
| 146 | cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 147 | desc->bMaxBurst = 15; |
| 148 | } |
| 149 | if ((usb_endpoint_xfer_control(&ep->desc) || usb_endpoint_xfer_int(&ep->desc)) |
| 150 | && desc->bmAttributes != 0) { |
| 151 | dev_warn(ddev, "%s endpoint with bmAttributes = %d in " |
| 152 | "config %d interface %d altsetting %d ep %d: " |
| 153 | "setting to zero\n", |
| 154 | usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk", |
| 155 | desc->bmAttributes, |
| 156 | cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 157 | desc->bmAttributes = 0; |
| 158 | } |
| 159 | if (usb_endpoint_xfer_bulk(&ep->desc) && desc->bmAttributes > 16) { |
| 160 | dev_warn(ddev, "Bulk endpoint with more than 65536 streams in " |
| 161 | "config %d interface %d altsetting %d ep %d: " |
| 162 | "setting to max\n", |
| 163 | cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 164 | desc->bmAttributes = 16; |
| 165 | } |
| 166 | if (usb_endpoint_xfer_isoc(&ep->desc) && desc->bmAttributes > 2) { |
| 167 | dev_warn(ddev, "Isoc endpoint has Mult of %d in " |
| 168 | "config %d interface %d altsetting %d ep %d: " |
| 169 | "setting to 3\n", desc->bmAttributes + 1, |
| 170 | cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 171 | desc->bmAttributes = 2; |
| 172 | } |
| 173 | if (usb_endpoint_xfer_isoc(&ep->desc)) { |
| 174 | max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) * |
| 175 | (desc->bmAttributes + 1); |
| 176 | } else if (usb_endpoint_xfer_int(&ep->desc)) { |
| 177 | max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1); |
| 178 | } else { |
| 179 | goto valid; |
| 180 | } |
| 181 | if (desc->wBytesPerInterval > max_tx) { |
| 182 | dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in " |
| 183 | "config %d interface %d altsetting %d ep %d: " |
| 184 | "setting to %d\n", |
| 185 | usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int", |
| 186 | desc->wBytesPerInterval, |
| 187 | cfgno, inum, asnum, ep->desc.bEndpointAddress, |
| 188 | max_tx); |
| 189 | desc->wBytesPerInterval = max_tx; |
| 190 | } |
| 191 | valid: |
| 192 | return retval; |
| 193 | } |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum, |
| 196 | int asnum, struct usb_host_interface *ifp, int num_ep, |
| 197 | unsigned char *buffer, int size) |
| 198 | { |
| 199 | unsigned char *buffer0 = buffer; |
| 200 | struct usb_endpoint_descriptor *d; |
| 201 | struct usb_host_endpoint *endpoint; |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 202 | int n, i, j, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | d = (struct usb_endpoint_descriptor *) buffer; |
| 205 | buffer += d->bLength; |
| 206 | size -= d->bLength; |
| 207 | |
| 208 | if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE) |
| 209 | n = USB_DT_ENDPOINT_AUDIO_SIZE; |
| 210 | else if (d->bLength >= USB_DT_ENDPOINT_SIZE) |
| 211 | n = USB_DT_ENDPOINT_SIZE; |
| 212 | else { |
| 213 | dev_warn(ddev, "config %d interface %d altsetting %d has an " |
| 214 | "invalid endpoint descriptor of length %d, skipping\n", |
| 215 | cfgno, inum, asnum, d->bLength); |
| 216 | goto skip_to_next_endpoint_or_interface_descriptor; |
| 217 | } |
| 218 | |
| 219 | i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK; |
| 220 | if (i >= 16 || i == 0) { |
| 221 | dev_warn(ddev, "config %d interface %d altsetting %d has an " |
| 222 | "invalid endpoint with address 0x%X, skipping\n", |
| 223 | cfgno, inum, asnum, d->bEndpointAddress); |
| 224 | goto skip_to_next_endpoint_or_interface_descriptor; |
| 225 | } |
| 226 | |
| 227 | /* Only store as many endpoints as we have room for */ |
| 228 | if (ifp->desc.bNumEndpoints >= num_ep) |
| 229 | goto skip_to_next_endpoint_or_interface_descriptor; |
| 230 | |
| 231 | endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints]; |
| 232 | ++ifp->desc.bNumEndpoints; |
| 233 | |
| 234 | memcpy(&endpoint->desc, d, n); |
| 235 | INIT_LIST_HEAD(&endpoint->urb_list); |
| 236 | |
Laurent Pinchart | 300871c | 2007-06-12 21:47:17 +0200 | [diff] [blame] | 237 | /* Fix up bInterval values outside the legal range. Use 32 ms if no |
| 238 | * proper value can be guessed. */ |
Alan Stern | 615ae11 | 2007-06-08 15:23:27 -0400 | [diff] [blame] | 239 | i = 0; /* i = min, j = max, n = default */ |
| 240 | j = 255; |
| 241 | if (usb_endpoint_xfer_int(d)) { |
| 242 | i = 1; |
| 243 | switch (to_usb_device(ddev)->speed) { |
Sarah Sharp | 6b403b0 | 2009-04-27 19:54:10 -0700 | [diff] [blame] | 244 | case USB_SPEED_SUPER: |
Alan Stern | 615ae11 | 2007-06-08 15:23:27 -0400 | [diff] [blame] | 245 | case USB_SPEED_HIGH: |
Laurent Pinchart | 300871c | 2007-06-12 21:47:17 +0200 | [diff] [blame] | 246 | /* Many device manufacturers are using full-speed |
| 247 | * bInterval values in high-speed interrupt endpoint |
| 248 | * descriptors. Try to fix those and fall back to a |
| 249 | * 32 ms default value otherwise. */ |
| 250 | n = fls(d->bInterval*8); |
| 251 | if (n == 0) |
| 252 | n = 9; /* 32 ms = 2^(9-1) uframes */ |
Alan Stern | 615ae11 | 2007-06-08 15:23:27 -0400 | [diff] [blame] | 253 | j = 16; |
| 254 | break; |
| 255 | default: /* USB_SPEED_FULL or _LOW */ |
| 256 | /* For low-speed, 10 ms is the official minimum. |
| 257 | * But some "overclocked" devices might want faster |
| 258 | * polling so we'll allow it. */ |
| 259 | n = 32; |
| 260 | break; |
| 261 | } |
| 262 | } else if (usb_endpoint_xfer_isoc(d)) { |
| 263 | i = 1; |
| 264 | j = 16; |
| 265 | switch (to_usb_device(ddev)->speed) { |
| 266 | case USB_SPEED_HIGH: |
| 267 | n = 9; /* 32 ms = 2^(9-1) uframes */ |
| 268 | break; |
| 269 | default: /* USB_SPEED_FULL */ |
| 270 | n = 6; /* 32 ms = 2^(6-1) frames */ |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | if (d->bInterval < i || d->bInterval > j) { |
| 275 | dev_warn(ddev, "config %d interface %d altsetting %d " |
| 276 | "endpoint 0x%X has an invalid bInterval %d, " |
| 277 | "changing to %d\n", |
| 278 | cfgno, inum, asnum, |
| 279 | d->bEndpointAddress, d->bInterval, n); |
| 280 | endpoint->desc.bInterval = n; |
| 281 | } |
| 282 | |
Alan Stern | 60aac1e | 2007-06-08 15:25:02 -0400 | [diff] [blame] | 283 | /* Some buggy low-speed devices have Bulk endpoints, which is |
| 284 | * explicitly forbidden by the USB spec. In an attempt to make |
| 285 | * them usable, we will try treating them as Interrupt endpoints. |
| 286 | */ |
| 287 | if (to_usb_device(ddev)->speed == USB_SPEED_LOW && |
| 288 | usb_endpoint_xfer_bulk(d)) { |
| 289 | dev_warn(ddev, "config %d interface %d altsetting %d " |
| 290 | "endpoint 0x%X is Bulk; changing to Interrupt\n", |
| 291 | cfgno, inum, asnum, d->bEndpointAddress); |
| 292 | endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT; |
| 293 | endpoint->desc.bInterval = 1; |
| 294 | if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8) |
| 295 | endpoint->desc.wMaxPacketSize = cpu_to_le16(8); |
| 296 | } |
| 297 | |
David Brownell | caa9ef6 | 2008-02-08 15:08:44 -0800 | [diff] [blame] | 298 | /* |
| 299 | * Some buggy high speed devices have bulk endpoints using |
| 300 | * maxpacket sizes other than 512. High speed HCDs may not |
| 301 | * be able to handle that particular bug, so let's warn... |
| 302 | */ |
| 303 | if (to_usb_device(ddev)->speed == USB_SPEED_HIGH |
| 304 | && usb_endpoint_xfer_bulk(d)) { |
| 305 | unsigned maxp; |
| 306 | |
| 307 | maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff; |
| 308 | if (maxp != 512) |
| 309 | dev_warn(ddev, "config %d interface %d altsetting %d " |
| 310 | "bulk endpoint 0x%X has invalid maxpacket %d\n", |
| 311 | cfgno, inum, asnum, d->bEndpointAddress, |
| 312 | maxp); |
| 313 | } |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 314 | /* Allocate room for and parse any SS endpoint companion descriptors */ |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 315 | if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) { |
| 316 | endpoint->extra = buffer; |
| 317 | i = find_next_descriptor_more(buffer, size, USB_DT_SS_ENDPOINT_COMP, |
| 318 | USB_DT_ENDPOINT, USB_DT_INTERFACE, &n); |
| 319 | endpoint->extralen = i; |
| 320 | buffer += i; |
| 321 | size -= i; |
David Brownell | caa9ef6 | 2008-02-08 15:08:44 -0800 | [diff] [blame] | 322 | |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 323 | if (size > 0) { |
Sarah Sharp | f0058c6 | 2009-04-29 19:06:20 -0700 | [diff] [blame] | 324 | retval = usb_parse_ss_endpoint_companion(ddev, cfgno, |
| 325 | inum, asnum, endpoint, num_ep, buffer, |
| 326 | size); |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 327 | if (retval >= 0) { |
| 328 | buffer += retval; |
| 329 | retval = buffer - buffer0; |
| 330 | } |
| 331 | } else { |
| 332 | retval = buffer - buffer0; |
| 333 | } |
| 334 | } else { |
| 335 | /* Skip over any Class Specific or Vendor Specific descriptors; |
| 336 | * find the next endpoint or interface descriptor */ |
| 337 | endpoint->extra = buffer; |
| 338 | i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, |
| 339 | USB_DT_INTERFACE, &n); |
| 340 | endpoint->extralen = i; |
| 341 | retval = buffer - buffer0 + i; |
| 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | if (n > 0) |
| 344 | dev_dbg(ddev, "skipped %d descriptor%s after %s\n", |
| 345 | n, plural(n), "endpoint"); |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 346 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | skip_to_next_endpoint_or_interface_descriptor: |
| 349 | i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, |
| 350 | USB_DT_INTERFACE, NULL); |
| 351 | return buffer - buffer0 + i; |
| 352 | } |
| 353 | |
| 354 | void usb_release_interface_cache(struct kref *ref) |
| 355 | { |
| 356 | struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref); |
| 357 | int j; |
| 358 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 359 | for (j = 0; j < intfc->num_altsetting; j++) { |
| 360 | struct usb_host_interface *alt = &intfc->altsetting[j]; |
| 361 | |
| 362 | kfree(alt->endpoint); |
| 363 | kfree(alt->string); |
| 364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | kfree(intfc); |
| 366 | } |
| 367 | |
| 368 | static int usb_parse_interface(struct device *ddev, int cfgno, |
| 369 | struct usb_host_config *config, unsigned char *buffer, int size, |
| 370 | u8 inums[], u8 nalts[]) |
| 371 | { |
| 372 | unsigned char *buffer0 = buffer; |
| 373 | struct usb_interface_descriptor *d; |
| 374 | int inum, asnum; |
| 375 | struct usb_interface_cache *intfc; |
| 376 | struct usb_host_interface *alt; |
| 377 | int i, n; |
| 378 | int len, retval; |
| 379 | int num_ep, num_ep_orig; |
| 380 | |
| 381 | d = (struct usb_interface_descriptor *) buffer; |
| 382 | buffer += d->bLength; |
| 383 | size -= d->bLength; |
| 384 | |
| 385 | if (d->bLength < USB_DT_INTERFACE_SIZE) |
| 386 | goto skip_to_next_interface_descriptor; |
| 387 | |
| 388 | /* Which interface entry is this? */ |
| 389 | intfc = NULL; |
| 390 | inum = d->bInterfaceNumber; |
| 391 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
| 392 | if (inums[i] == inum) { |
| 393 | intfc = config->intf_cache[i]; |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | if (!intfc || intfc->num_altsetting >= nalts[i]) |
| 398 | goto skip_to_next_interface_descriptor; |
| 399 | |
| 400 | /* Check for duplicate altsetting entries */ |
| 401 | asnum = d->bAlternateSetting; |
| 402 | for ((i = 0, alt = &intfc->altsetting[0]); |
| 403 | i < intfc->num_altsetting; |
| 404 | (++i, ++alt)) { |
| 405 | if (alt->desc.bAlternateSetting == asnum) { |
| 406 | dev_warn(ddev, "Duplicate descriptor for config %d " |
| 407 | "interface %d altsetting %d, skipping\n", |
| 408 | cfgno, inum, asnum); |
| 409 | goto skip_to_next_interface_descriptor; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | ++intfc->num_altsetting; |
| 414 | memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE); |
| 415 | |
| 416 | /* Skip over any Class Specific or Vendor Specific descriptors; |
| 417 | * find the first endpoint or interface descriptor */ |
| 418 | alt->extra = buffer; |
| 419 | i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, |
| 420 | USB_DT_INTERFACE, &n); |
| 421 | alt->extralen = i; |
| 422 | if (n > 0) |
| 423 | dev_dbg(ddev, "skipped %d descriptor%s after %s\n", |
| 424 | n, plural(n), "interface"); |
| 425 | buffer += i; |
| 426 | size -= i; |
| 427 | |
| 428 | /* Allocate space for the right(?) number of endpoints */ |
| 429 | num_ep = num_ep_orig = alt->desc.bNumEndpoints; |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 430 | alt->desc.bNumEndpoints = 0; /* Use as a counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (num_ep > USB_MAXENDPOINTS) { |
| 432 | dev_warn(ddev, "too many endpoints for config %d interface %d " |
| 433 | "altsetting %d: %d, using maximum allowed: %d\n", |
| 434 | cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS); |
| 435 | num_ep = USB_MAXENDPOINTS; |
| 436 | } |
| 437 | |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 438 | if (num_ep > 0) { |
| 439 | /* Can't allocate 0 bytes */ |
Alan Stern | 57a21c1 | 2007-05-15 17:40:37 -0400 | [diff] [blame] | 440 | len = sizeof(struct usb_host_endpoint) * num_ep; |
| 441 | alt->endpoint = kzalloc(len, GFP_KERNEL); |
| 442 | if (!alt->endpoint) |
| 443 | return -ENOMEM; |
| 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | /* Parse all the endpoint descriptors */ |
| 447 | n = 0; |
| 448 | while (size > 0) { |
| 449 | if (((struct usb_descriptor_header *) buffer)->bDescriptorType |
| 450 | == USB_DT_INTERFACE) |
| 451 | break; |
| 452 | retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt, |
| 453 | num_ep, buffer, size); |
| 454 | if (retval < 0) |
| 455 | return retval; |
| 456 | ++n; |
| 457 | |
| 458 | buffer += retval; |
| 459 | size -= retval; |
| 460 | } |
| 461 | |
| 462 | if (n != num_ep_orig) |
| 463 | dev_warn(ddev, "config %d interface %d altsetting %d has %d " |
| 464 | "endpoint descriptor%s, different from the interface " |
| 465 | "descriptor's value: %d\n", |
| 466 | cfgno, inum, asnum, n, plural(n), num_ep_orig); |
| 467 | return buffer - buffer0; |
| 468 | |
| 469 | skip_to_next_interface_descriptor: |
| 470 | i = find_next_descriptor(buffer, size, USB_DT_INTERFACE, |
| 471 | USB_DT_INTERFACE, NULL); |
| 472 | return buffer - buffer0 + i; |
| 473 | } |
| 474 | |
| 475 | static int usb_parse_configuration(struct device *ddev, int cfgidx, |
| 476 | struct usb_host_config *config, unsigned char *buffer, int size) |
| 477 | { |
| 478 | unsigned char *buffer0 = buffer; |
| 479 | int cfgno; |
| 480 | int nintf, nintf_orig; |
| 481 | int i, j, n; |
| 482 | struct usb_interface_cache *intfc; |
| 483 | unsigned char *buffer2; |
| 484 | int size2; |
| 485 | struct usb_descriptor_header *header; |
| 486 | int len, retval; |
| 487 | u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES]; |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 488 | unsigned iad_num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE); |
| 491 | if (config->desc.bDescriptorType != USB_DT_CONFIG || |
| 492 | config->desc.bLength < USB_DT_CONFIG_SIZE) { |
| 493 | dev_err(ddev, "invalid descriptor for config index %d: " |
| 494 | "type = 0x%X, length = %d\n", cfgidx, |
| 495 | config->desc.bDescriptorType, config->desc.bLength); |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | cfgno = config->desc.bConfigurationValue; |
| 499 | |
| 500 | buffer += config->desc.bLength; |
| 501 | size -= config->desc.bLength; |
| 502 | |
| 503 | nintf = nintf_orig = config->desc.bNumInterfaces; |
| 504 | if (nintf > USB_MAXINTERFACES) { |
| 505 | dev_warn(ddev, "config %d has too many interfaces: %d, " |
| 506 | "using maximum allowed: %d\n", |
| 507 | cfgno, nintf, USB_MAXINTERFACES); |
| 508 | nintf = USB_MAXINTERFACES; |
| 509 | } |
| 510 | |
| 511 | /* Go through the descriptors, checking their length and counting the |
| 512 | * number of altsettings for each interface */ |
| 513 | n = 0; |
| 514 | for ((buffer2 = buffer, size2 = size); |
| 515 | size2 > 0; |
| 516 | (buffer2 += header->bLength, size2 -= header->bLength)) { |
| 517 | |
| 518 | if (size2 < sizeof(struct usb_descriptor_header)) { |
| 519 | dev_warn(ddev, "config %d descriptor has %d excess " |
| 520 | "byte%s, ignoring\n", |
| 521 | cfgno, size2, plural(size2)); |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | header = (struct usb_descriptor_header *) buffer2; |
| 526 | if ((header->bLength > size2) || (header->bLength < 2)) { |
| 527 | dev_warn(ddev, "config %d has an invalid descriptor " |
| 528 | "of length %d, skipping remainder of the config\n", |
| 529 | cfgno, header->bLength); |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | if (header->bDescriptorType == USB_DT_INTERFACE) { |
| 534 | struct usb_interface_descriptor *d; |
| 535 | int inum; |
| 536 | |
| 537 | d = (struct usb_interface_descriptor *) header; |
| 538 | if (d->bLength < USB_DT_INTERFACE_SIZE) { |
| 539 | dev_warn(ddev, "config %d has an invalid " |
| 540 | "interface descriptor of length %d, " |
| 541 | "skipping\n", cfgno, d->bLength); |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | inum = d->bInterfaceNumber; |
| 546 | if (inum >= nintf_orig) |
| 547 | dev_warn(ddev, "config %d has an invalid " |
| 548 | "interface number: %d but max is %d\n", |
| 549 | cfgno, inum, nintf_orig - 1); |
| 550 | |
| 551 | /* Have we already encountered this interface? |
| 552 | * Count its altsettings */ |
| 553 | for (i = 0; i < n; ++i) { |
| 554 | if (inums[i] == inum) |
| 555 | break; |
| 556 | } |
| 557 | if (i < n) { |
| 558 | if (nalts[i] < 255) |
| 559 | ++nalts[i]; |
| 560 | } else if (n < USB_MAXINTERFACES) { |
| 561 | inums[n] = inum; |
| 562 | nalts[n] = 1; |
| 563 | ++n; |
| 564 | } |
| 565 | |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 566 | } else if (header->bDescriptorType == |
| 567 | USB_DT_INTERFACE_ASSOCIATION) { |
| 568 | if (iad_num == USB_MAXIADS) { |
| 569 | dev_warn(ddev, "found more Interface " |
| 570 | "Association Descriptors " |
| 571 | "than allocated for in " |
| 572 | "configuration %d\n", cfgno); |
| 573 | } else { |
| 574 | config->intf_assoc[iad_num] = |
| 575 | (struct usb_interface_assoc_descriptor |
| 576 | *)header; |
| 577 | iad_num++; |
| 578 | } |
| 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } else if (header->bDescriptorType == USB_DT_DEVICE || |
| 581 | header->bDescriptorType == USB_DT_CONFIG) |
| 582 | dev_warn(ddev, "config %d contains an unexpected " |
| 583 | "descriptor of type 0x%X, skipping\n", |
| 584 | cfgno, header->bDescriptorType); |
| 585 | |
| 586 | } /* for ((buffer2 = buffer, size2 = size); ...) */ |
| 587 | size = buffer2 - buffer; |
| 588 | config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0); |
| 589 | |
| 590 | if (n != nintf) |
| 591 | dev_warn(ddev, "config %d has %d interface%s, different from " |
| 592 | "the descriptor's value: %d\n", |
| 593 | cfgno, n, plural(n), nintf_orig); |
| 594 | else if (n == 0) |
| 595 | dev_warn(ddev, "config %d has no interfaces?\n", cfgno); |
| 596 | config->desc.bNumInterfaces = nintf = n; |
| 597 | |
| 598 | /* Check for missing interface numbers */ |
| 599 | for (i = 0; i < nintf; ++i) { |
| 600 | for (j = 0; j < nintf; ++j) { |
| 601 | if (inums[j] == i) |
| 602 | break; |
| 603 | } |
| 604 | if (j >= nintf) |
| 605 | dev_warn(ddev, "config %d has no interface number " |
| 606 | "%d\n", cfgno, i); |
| 607 | } |
| 608 | |
| 609 | /* Allocate the usb_interface_caches and altsetting arrays */ |
| 610 | for (i = 0; i < nintf; ++i) { |
| 611 | j = nalts[i]; |
| 612 | if (j > USB_MAXALTSETTING) { |
| 613 | dev_warn(ddev, "too many alternate settings for " |
| 614 | "config %d interface %d: %d, " |
| 615 | "using maximum allowed: %d\n", |
| 616 | cfgno, inums[i], j, USB_MAXALTSETTING); |
| 617 | nalts[i] = j = USB_MAXALTSETTING; |
| 618 | } |
| 619 | |
| 620 | len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j; |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 621 | config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (!intfc) |
| 623 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | kref_init(&intfc->ref); |
| 625 | } |
| 626 | |
Sarah Sharp | 663c30d | 2009-04-27 19:58:14 -0700 | [diff] [blame] | 627 | /* FIXME: parse the BOS descriptor */ |
| 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | /* Skip over any Class Specific or Vendor Specific descriptors; |
| 630 | * find the first interface descriptor */ |
| 631 | config->extra = buffer; |
| 632 | i = find_next_descriptor(buffer, size, USB_DT_INTERFACE, |
| 633 | USB_DT_INTERFACE, &n); |
| 634 | config->extralen = i; |
| 635 | if (n > 0) |
| 636 | dev_dbg(ddev, "skipped %d descriptor%s after %s\n", |
| 637 | n, plural(n), "configuration"); |
| 638 | buffer += i; |
| 639 | size -= i; |
| 640 | |
| 641 | /* Parse all the interface/altsetting descriptors */ |
| 642 | while (size > 0) { |
| 643 | retval = usb_parse_interface(ddev, cfgno, config, |
| 644 | buffer, size, inums, nalts); |
| 645 | if (retval < 0) |
| 646 | return retval; |
| 647 | |
| 648 | buffer += retval; |
| 649 | size -= retval; |
| 650 | } |
| 651 | |
| 652 | /* Check for missing altsettings */ |
| 653 | for (i = 0; i < nintf; ++i) { |
| 654 | intfc = config->intf_cache[i]; |
| 655 | for (j = 0; j < intfc->num_altsetting; ++j) { |
| 656 | for (n = 0; n < intfc->num_altsetting; ++n) { |
| 657 | if (intfc->altsetting[n].desc. |
| 658 | bAlternateSetting == j) |
| 659 | break; |
| 660 | } |
| 661 | if (n >= intfc->num_altsetting) |
| 662 | dev_warn(ddev, "config %d interface %d has no " |
| 663 | "altsetting %d\n", cfgno, inums[i], j); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 670 | /* hub-only!! ... and only exported for reset/reinit path. |
| 671 | * otherwise used internally on disconnect/destroy path |
| 672 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | void usb_destroy_configuration(struct usb_device *dev) |
| 674 | { |
| 675 | int c, i; |
| 676 | |
| 677 | if (!dev->config) |
| 678 | return; |
| 679 | |
| 680 | if (dev->rawdescriptors) { |
| 681 | for (i = 0; i < dev->descriptor.bNumConfigurations; i++) |
| 682 | kfree(dev->rawdescriptors[i]); |
| 683 | |
| 684 | kfree(dev->rawdescriptors); |
| 685 | dev->rawdescriptors = NULL; |
| 686 | } |
| 687 | |
| 688 | for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { |
| 689 | struct usb_host_config *cf = &dev->config[c]; |
| 690 | |
| 691 | kfree(cf->string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | for (i = 0; i < cf->desc.bNumInterfaces; i++) { |
| 693 | if (cf->intf_cache[i]) |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 694 | kref_put(&cf->intf_cache[i]->ref, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | usb_release_interface_cache); |
| 696 | } |
| 697 | } |
| 698 | kfree(dev->config); |
| 699 | dev->config = NULL; |
| 700 | } |
| 701 | |
| 702 | |
Inaky Perez-Gonzalez | 1145065 | 2007-07-31 20:34:02 -0700 | [diff] [blame] | 703 | /* |
| 704 | * Get the USB config descriptors, cache and parse'em |
| 705 | * |
| 706 | * hub-only!! ... and only in reset path, or usb_new_device() |
| 707 | * (used by real hubs and virtual root hubs) |
| 708 | * |
| 709 | * NOTE: if this is a WUSB device and is not authorized, we skip the |
| 710 | * whole thing. A non-authorized USB device has no |
| 711 | * configurations. |
| 712 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | int usb_get_configuration(struct usb_device *dev) |
| 714 | { |
| 715 | struct device *ddev = &dev->dev; |
| 716 | int ncfg = dev->descriptor.bNumConfigurations; |
Inaky Perez-Gonzalez | 1145065 | 2007-07-31 20:34:02 -0700 | [diff] [blame] | 717 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | unsigned int cfgno, length; |
| 719 | unsigned char *buffer; |
| 720 | unsigned char *bigbuffer; |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 721 | struct usb_config_descriptor *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Inaky Perez-Gonzalez | 1145065 | 2007-07-31 20:34:02 -0700 | [diff] [blame] | 723 | cfgno = 0; |
| 724 | if (dev->authorized == 0) /* Not really an error */ |
| 725 | goto out_not_authorized; |
| 726 | result = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (ncfg > USB_MAXCONFIG) { |
| 728 | dev_warn(ddev, "too many configurations: %d, " |
| 729 | "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG); |
| 730 | dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG; |
| 731 | } |
| 732 | |
| 733 | if (ncfg < 1) { |
| 734 | dev_err(ddev, "no configurations\n"); |
| 735 | return -EINVAL; |
| 736 | } |
| 737 | |
| 738 | length = ncfg * sizeof(struct usb_host_config); |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 739 | dev->config = kzalloc(length, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (!dev->config) |
| 741 | goto err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
| 743 | length = ncfg * sizeof(char *); |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 744 | dev->rawdescriptors = kzalloc(length, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | if (!dev->rawdescriptors) |
| 746 | goto err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL); |
| 749 | if (!buffer) |
| 750 | goto err2; |
| 751 | desc = (struct usb_config_descriptor *)buffer; |
| 752 | |
Inaky Perez-Gonzalez | 1145065 | 2007-07-31 20:34:02 -0700 | [diff] [blame] | 753 | result = 0; |
| 754 | for (; cfgno < ncfg; cfgno++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | /* We grab just the first descriptor so we know how long |
| 756 | * the whole configuration is */ |
| 757 | result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, |
| 758 | buffer, USB_DT_CONFIG_SIZE); |
| 759 | if (result < 0) { |
| 760 | dev_err(ddev, "unable to read config index %d " |
Inaky Perez-Gonzalez | 1145065 | 2007-07-31 20:34:02 -0700 | [diff] [blame] | 761 | "descriptor/%s: %d\n", cfgno, "start", result); |
Inaky Perez-Gonzalez | cb4c8fe | 2006-08-25 19:35:28 -0700 | [diff] [blame] | 762 | dev_err(ddev, "chopping to %d config(s)\n", cfgno); |
| 763 | dev->descriptor.bNumConfigurations = cfgno; |
| 764 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } else if (result < 4) { |
| 766 | dev_err(ddev, "config index %d descriptor too short " |
| 767 | "(expected %i, got %i)\n", cfgno, |
| 768 | USB_DT_CONFIG_SIZE, result); |
| 769 | result = -EINVAL; |
| 770 | goto err; |
| 771 | } |
| 772 | length = max((int) le16_to_cpu(desc->wTotalLength), |
| 773 | USB_DT_CONFIG_SIZE); |
| 774 | |
| 775 | /* Now that we know the length, get the whole thing */ |
| 776 | bigbuffer = kmalloc(length, GFP_KERNEL); |
| 777 | if (!bigbuffer) { |
| 778 | result = -ENOMEM; |
| 779 | goto err; |
| 780 | } |
| 781 | result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, |
| 782 | bigbuffer, length); |
| 783 | if (result < 0) { |
| 784 | dev_err(ddev, "unable to read config index %d " |
| 785 | "descriptor/%s\n", cfgno, "all"); |
| 786 | kfree(bigbuffer); |
| 787 | goto err; |
| 788 | } |
| 789 | if (result < length) { |
| 790 | dev_warn(ddev, "config index %d descriptor too short " |
| 791 | "(expected %i, got %i)\n", cfgno, length, result); |
| 792 | length = result; |
| 793 | } |
| 794 | |
| 795 | dev->rawdescriptors[cfgno] = bigbuffer; |
| 796 | |
| 797 | result = usb_parse_configuration(&dev->dev, cfgno, |
| 798 | &dev->config[cfgno], bigbuffer, length); |
| 799 | if (result < 0) { |
| 800 | ++cfgno; |
| 801 | goto err; |
| 802 | } |
| 803 | } |
| 804 | result = 0; |
| 805 | |
| 806 | err: |
| 807 | kfree(buffer); |
Inaky Perez-Gonzalez | 1145065 | 2007-07-31 20:34:02 -0700 | [diff] [blame] | 808 | out_not_authorized: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | dev->descriptor.bNumConfigurations = cfgno; |
| 810 | err2: |
| 811 | if (result == -ENOMEM) |
| 812 | dev_err(ddev, "out of memory\n"); |
| 813 | return result; |
| 814 | } |