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