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