| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Released under the GPLv2 only. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <linux/usb.h> |
| 7 |
#include <linux/usb/ch9.h> |
| 8 |
#include <linux/usb/hcd.h> |
| 9 |
#include <linux/usb/quirks.h> |
| 10 |
#include <linux/module.h> |
| 11 |
#include <linux/slab.h> |
| 12 |
#include <linux/string_choices.h> |
| 13 |
#include <linux/device.h> |
| 14 |
#include <asm/byteorder.h> |
| 15 |
#include "usb.h" |
| 16 |
|
| 17 |
|
| 18 |
#define USB_MAXALTSETTING 128 /* Hard limit */ |
| 19 |
|
| 20 |
#define USB_MAXCONFIG 8 /* Arbitrary limit */ |
| 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 |
|
| 46 |
static void usb_parse_ssp_isoc_endpoint_companion(struct device *ddev, |
| 47 |
int cfgno, int inum, int asnum, struct usb_host_endpoint *ep, |
| 48 |
unsigned char *buffer, int size) |
| 49 |
{ |
| 50 |
struct usb_ssp_isoc_ep_comp_descriptor *desc; |
| 51 |
|
| 52 |
/* |
| 53 |
* The SuperSpeedPlus Isoc endpoint companion descriptor immediately |
| 54 |
* follows the SuperSpeed Endpoint Companion descriptor |
| 55 |
*/ |
| 56 |
desc = (struct usb_ssp_isoc_ep_comp_descriptor *) buffer; |
| 57 |
if (desc->bDescriptorType != USB_DT_SSP_ISOC_ENDPOINT_COMP || |
| 58 |
size < USB_DT_SSP_ISOC_EP_COMP_SIZE) { |
| 59 |
dev_notice(ddev, "Invalid SuperSpeedPlus isoc endpoint companion" |
| 60 |
"for config %d interface %d altsetting %d ep %d.\n", |
| 61 |
cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 62 |
return; |
| 63 |
} |
| 64 |
memcpy(&ep->ssp_isoc_ep_comp, desc, USB_DT_SSP_ISOC_EP_COMP_SIZE); |
| 65 |
} |
| 66 |
|
| 67 |
static void usb_parse_eusb2_isoc_endpoint_companion(struct device *ddev, |
| 68 |
int cfgno, int inum, int asnum, struct usb_host_endpoint *ep, |
| 69 |
unsigned char *buffer, int size) |
| 70 |
{ |
| 71 |
struct usb_eusb2_isoc_ep_comp_descriptor *desc; |
| 72 |
struct usb_descriptor_header *h; |
| 73 |
|
| 74 |
/* |
| 75 |
* eUSB2 isochronous endpoint companion descriptor for this endpoint |
| 76 |
* shall be declared before the next endpoint or interface descriptor |
| 77 |
*/ |
| 78 |
while (size >= USB_DT_EUSB2_ISOC_EP_COMP_SIZE) { |
| 79 |
h = (struct usb_descriptor_header *)buffer; |
| 80 |
|
| 81 |
if (h->bDescriptorType == USB_DT_EUSB2_ISOC_ENDPOINT_COMP) { |
| 82 |
desc = (struct usb_eusb2_isoc_ep_comp_descriptor *)buffer; |
| 83 |
ep->eusb2_isoc_ep_comp = *desc; |
| 84 |
return; |
| 85 |
} |
| 86 |
if (h->bDescriptorType == USB_DT_ENDPOINT || |
| 87 |
h->bDescriptorType == USB_DT_INTERFACE) |
| 88 |
break; |
| 89 |
|
| 90 |
buffer += h->bLength; |
| 91 |
size -= h->bLength; |
| 92 |
} |
| 93 |
|
| 94 |
dev_notice(ddev, "No eUSB2 isoc ep %d companion for config %d interface %d altsetting %d\n", |
| 95 |
ep->desc.bEndpointAddress, cfgno, inum, asnum); |
| 96 |
} |
| 97 |
|
| 98 |
static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno, |
| 99 |
int inum, int asnum, struct usb_host_endpoint *ep, |
| 100 |
unsigned char *buffer, int size) |
| 101 |
{ |
| 102 |
struct usb_ss_ep_comp_descriptor *desc; |
| 103 |
int max_tx; |
| 104 |
|
| 105 |
/* The SuperSpeed endpoint companion descriptor is supposed to |
| 106 |
* be the first thing immediately following the endpoint descriptor. |
| 107 |
*/ |
| 108 |
desc = (struct usb_ss_ep_comp_descriptor *) buffer; |
| 109 |
|
| 110 |
if (size < USB_DT_SS_EP_COMP_SIZE) { |
| 111 |
dev_notice(ddev, |
| 112 |
"invalid SuperSpeed endpoint companion descriptor " |
| 113 |
"of length %d, skipping\n", size); |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) { |
| 118 |
dev_notice(ddev, "No SuperSpeed endpoint companion for config %d " |
| 119 |
" interface %d altsetting %d ep %d: " |
| 120 |
"using minimum values\n", |
| 121 |
cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 122 |
|
| 123 |
/* Fill in some default values. |
| 124 |
* Leave bmAttributes as zero, which will mean no streams for |
| 125 |
* bulk, and isoc won't support multiple bursts of packets. |
| 126 |
* With bursts of only one packet, and a Mult of 1, the max |
| 127 |
* amount of data moved per endpoint service interval is one |
| 128 |
* packet. |
| 129 |
*/ |
| 130 |
ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE; |
| 131 |
ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP; |
| 132 |
if (usb_endpoint_xfer_isoc(&ep->desc) || |
| 133 |
usb_endpoint_xfer_int(&ep->desc)) |
| 134 |
ep->ss_ep_comp.wBytesPerInterval = |
| 135 |
ep->desc.wMaxPacketSize; |
| 136 |
return; |
| 137 |
} |
| 138 |
buffer += desc->bLength; |
| 139 |
size -= desc->bLength; |
| 140 |
memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE); |
| 141 |
|
| 142 |
/* Check the various values */ |
| 143 |
if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) { |
| 144 |
dev_notice(ddev, "Control endpoint with bMaxBurst = %d in " |
| 145 |
"config %d interface %d altsetting %d ep %d: " |
| 146 |
"setting to zero\n", desc->bMaxBurst, |
| 147 |
cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 148 |
ep->ss_ep_comp.bMaxBurst = 0; |
| 149 |
} else if (desc->bMaxBurst > 15) { |
| 150 |
dev_notice(ddev, "Endpoint with bMaxBurst = %d in " |
| 151 |
"config %d interface %d altsetting %d ep %d: " |
| 152 |
"setting to 15\n", desc->bMaxBurst, |
| 153 |
cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 154 |
ep->ss_ep_comp.bMaxBurst = 15; |
| 155 |
} |
| 156 |
|
| 157 |
if ((usb_endpoint_xfer_control(&ep->desc) || |
| 158 |
usb_endpoint_xfer_int(&ep->desc)) && |
| 159 |
desc->bmAttributes != 0) { |
| 160 |
dev_notice(ddev, "%s endpoint with bmAttributes = %d in " |
| 161 |
"config %d interface %d altsetting %d ep %d: " |
| 162 |
"setting to zero\n", |
| 163 |
usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk", |
| 164 |
desc->bmAttributes, |
| 165 |
cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 166 |
ep->ss_ep_comp.bmAttributes = 0; |
| 167 |
} else if (usb_endpoint_xfer_bulk(&ep->desc) && |
| 168 |
desc->bmAttributes > 16) { |
| 169 |
dev_notice(ddev, "Bulk endpoint with more than 65536 streams in " |
| 170 |
"config %d interface %d altsetting %d ep %d: " |
| 171 |
"setting to max\n", |
| 172 |
cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 173 |
ep->ss_ep_comp.bmAttributes = 16; |
| 174 |
} else if (usb_endpoint_xfer_isoc(&ep->desc) && |
| 175 |
!USB_SS_SSP_ISOC_COMP(desc->bmAttributes) && |
| 176 |
USB_SS_MULT(desc->bmAttributes) > 3) { |
| 177 |
dev_notice(ddev, "Isoc endpoint has Mult of %d in " |
| 178 |
"config %d interface %d altsetting %d ep %d: " |
| 179 |
"setting to 3\n", |
| 180 |
USB_SS_MULT(desc->bmAttributes), |
| 181 |
cfgno, inum, asnum, ep->desc.bEndpointAddress); |
| 182 |
ep->ss_ep_comp.bmAttributes = 2; |
| 183 |
} |
| 184 |
|
| 185 |
if (usb_endpoint_xfer_isoc(&ep->desc)) |
| 186 |
max_tx = (desc->bMaxBurst + 1) * |
| 187 |
(USB_SS_MULT(desc->bmAttributes)) * |
| 188 |
usb_endpoint_maxp(&ep->desc); |
| 189 |
else if (usb_endpoint_xfer_int(&ep->desc)) |
| 190 |
max_tx = usb_endpoint_maxp(&ep->desc) * |
| 191 |
(desc->bMaxBurst + 1); |
| 192 |
else |
| 193 |
max_tx = 999999; |
| 194 |
/* |
| 195 |
* wBytesPerInterval > max_tx is bogus, but USB3 spec doesn't forbid the opposite. |
| 196 |
* Experience shows that wBytesPerInterval < wMaxPacketSize on common interrupt IN |
| 197 |
* endpoints is usually bogus too, and recent HCs enforce interrupt BW limits. |
| 198 |
*/ |
| 199 |
if (le16_to_cpu(desc->wBytesPerInterval) > max_tx || |
| 200 |
(le16_to_cpu(desc->wBytesPerInterval) < usb_endpoint_maxp(&ep->desc) && |
| 201 |
usb_endpoint_is_int_in(&ep->desc))) { |
| 202 |
dev_notice(ddev, "%s endpoint with wBytesPerInterval of %d in " |
| 203 |
"config %d interface %d altsetting %d ep %d: " |
| 204 |
"setting to %d\n", |
| 205 |
usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int", |
| 206 |
le16_to_cpu(desc->wBytesPerInterval), |
| 207 |
cfgno, inum, asnum, ep->desc.bEndpointAddress, |
| 208 |
max_tx); |
| 209 |
ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx); |
| 210 |
} |
| 211 |
/* Parse a possible SuperSpeedPlus isoc ep companion descriptor */ |
| 212 |
if (usb_endpoint_xfer_isoc(&ep->desc) && |
| 213 |
USB_SS_SSP_ISOC_COMP(desc->bmAttributes)) |
| 214 |
usb_parse_ssp_isoc_endpoint_companion(ddev, cfgno, inum, asnum, |
| 215 |
ep, buffer, size); |
| 216 |
} |
| 217 |
|
| 218 |
static const unsigned short low_speed_maxpacket_maxes[4] = { |
| 219 |
[USB_ENDPOINT_XFER_CONTROL] = 8, |
| 220 |
[USB_ENDPOINT_XFER_ISOC] = 0, |
| 221 |
[USB_ENDPOINT_XFER_BULK] = 0, |
| 222 |
[USB_ENDPOINT_XFER_INT] = 8, |
| 223 |
}; |
| 224 |
static const unsigned short full_speed_maxpacket_maxes[4] = { |
| 225 |
[USB_ENDPOINT_XFER_CONTROL] = 64, |
| 226 |
[USB_ENDPOINT_XFER_ISOC] = 1023, |
| 227 |
[USB_ENDPOINT_XFER_BULK] = 64, |
| 228 |
[USB_ENDPOINT_XFER_INT] = 64, |
| 229 |
}; |
| 230 |
static const unsigned short high_speed_maxpacket_maxes[4] = { |
| 231 |
[USB_ENDPOINT_XFER_CONTROL] = 64, |
| 232 |
[USB_ENDPOINT_XFER_ISOC] = 1024, |
| 233 |
|
| 234 |
/* Bulk should be 512, but some devices use 1024: we will warn below */ |
| 235 |
[USB_ENDPOINT_XFER_BULK] = 1024, |
| 236 |
[USB_ENDPOINT_XFER_INT] = 1024, |
| 237 |
}; |
| 238 |
static const unsigned short super_speed_maxpacket_maxes[4] = { |
| 239 |
[USB_ENDPOINT_XFER_CONTROL] = 512, |
| 240 |
[USB_ENDPOINT_XFER_ISOC] = 1024, |
| 241 |
[USB_ENDPOINT_XFER_BULK] = 1024, |
| 242 |
[USB_ENDPOINT_XFER_INT] = 1024, |
| 243 |
}; |
| 244 |
|
| 245 |
static bool endpoint_is_duplicate(struct usb_endpoint_descriptor *e1, |
| 246 |
struct usb_endpoint_descriptor *e2) |
| 247 |
{ |
| 248 |
if (e1->bEndpointAddress == e2->bEndpointAddress) |
| 249 |
return true; |
| 250 |
|
| 251 |
if (usb_endpoint_xfer_control(e1) || usb_endpoint_xfer_control(e2)) { |
| 252 |
if (usb_endpoint_num(e1) == usb_endpoint_num(e2)) |
| 253 |
return true; |
| 254 |
} |
| 255 |
|
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* Check for duplicate endpoint addresses in other interfaces and in the |
| 261 |
* altsetting currently being parsed. |
| 262 |
*/ |
| 263 |
static bool config_endpoint_is_duplicate(struct usb_host_config *config, |
| 264 |
int inum, int asnum, struct usb_endpoint_descriptor *d) |
| 265 |
{ |
| 266 |
struct usb_endpoint_descriptor *epd; |
| 267 |
struct usb_interface_cache *intfc; |
| 268 |
struct usb_host_interface *alt; |
| 269 |
int i, j, k; |
| 270 |
|
| 271 |
for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
| 272 |
intfc = config->intf_cache[i]; |
| 273 |
|
| 274 |
for (j = 0; j < intfc->num_altsetting; ++j) { |
| 275 |
alt = &intfc->altsetting[j]; |
| 276 |
|
| 277 |
if (alt->desc.bInterfaceNumber == inum && |
| 278 |
alt->desc.bAlternateSetting != asnum) |
| 279 |
continue; |
| 280 |
|
| 281 |
for (k = 0; k < alt->desc.bNumEndpoints; ++k) { |
| 282 |
epd = &alt->endpoint[k].desc; |
| 283 |
|
| 284 |
if (endpoint_is_duplicate(epd, d)) |
| 285 |
return true; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
static int usb_parse_endpoint(struct device *ddev, int cfgno, |
| 294 |
struct usb_host_config *config, int inum, int asnum, |
| 295 |
struct usb_host_interface *ifp, int num_ep, |
| 296 |
unsigned char *buffer, int size) |
| 297 |
{ |
| 298 |
struct usb_device *udev = to_usb_device(ddev); |
| 299 |
unsigned char *buffer0 = buffer; |
| 300 |
struct usb_endpoint_descriptor *d; |
| 301 |
struct usb_host_endpoint *endpoint; |
| 302 |
int n, i, j, retval; |
| 303 |
unsigned int maxp; |
| 304 |
const unsigned short *maxpacket_maxes; |
| 305 |
u16 bcdUSB; |
| 306 |
|
| 307 |
d = (struct usb_endpoint_descriptor *) buffer; |
| 308 |
bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB); |
| 309 |
buffer += d->bLength; |
| 310 |
size -= d->bLength; |
| 311 |
|
| 312 |
if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE) |
| 313 |
n = USB_DT_ENDPOINT_AUDIO_SIZE; |
| 314 |
else if (d->bLength >= USB_DT_ENDPOINT_SIZE) |
| 315 |
n = USB_DT_ENDPOINT_SIZE; |
| 316 |
else { |
| 317 |
dev_notice(ddev, "config %d interface %d altsetting %d has an " |
| 318 |
"invalid endpoint descriptor of length %d, skipping\n", |
| 319 |
cfgno, inum, asnum, d->bLength); |
| 320 |
goto skip_to_next_endpoint_or_interface_descriptor; |
| 321 |
} |
| 322 |
|
| 323 |
i = usb_endpoint_num(d); |
| 324 |
if (i == 0) { |
| 325 |
dev_notice(ddev, "config %d interface %d altsetting %d has an " |
| 326 |
"invalid descriptor for endpoint zero, skipping\n", |
| 327 |
cfgno, inum, asnum); |
| 328 |
goto skip_to_next_endpoint_or_interface_descriptor; |
| 329 |
} |
| 330 |
|
| 331 |
/* Only store as many endpoints as we have room for */ |
| 332 |
if (ifp->desc.bNumEndpoints >= num_ep) |
| 333 |
goto skip_to_next_endpoint_or_interface_descriptor; |
| 334 |
|
| 335 |
/* Save a copy of the descriptor and use it instead of the original */ |
| 336 |
endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints]; |
| 337 |
memcpy(&endpoint->desc, d, n); |
| 338 |
d = &endpoint->desc; |
| 339 |
|
| 340 |
/* Clear the reserved bits in bEndpointAddress */ |
| 341 |
i = d->bEndpointAddress & |
| 342 |
(USB_ENDPOINT_DIR_MASK | USB_ENDPOINT_NUMBER_MASK); |
| 343 |
if (i != d->bEndpointAddress) { |
| 344 |
dev_notice(ddev, "config %d interface %d altsetting %d has an endpoint descriptor with address 0x%X, changing to 0x%X\n", |
| 345 |
cfgno, inum, asnum, d->bEndpointAddress, i); |
| 346 |
endpoint->desc.bEndpointAddress = i; |
| 347 |
} |
| 348 |
|
| 349 |
/* Check for duplicate endpoint addresses */ |
| 350 |
if (config_endpoint_is_duplicate(config, inum, asnum, d)) { |
| 351 |
dev_notice(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n", |
| 352 |
cfgno, inum, asnum, d->bEndpointAddress); |
| 353 |
goto skip_to_next_endpoint_or_interface_descriptor; |
| 354 |
} |
| 355 |
|
| 356 |
/* Ignore some endpoints */ |
| 357 |
if (udev->quirks & USB_QUIRK_ENDPOINT_IGNORE) { |
| 358 |
if (usb_endpoint_is_ignored(udev, ifp, d)) { |
| 359 |
dev_notice(ddev, "config %d interface %d altsetting %d has an ignored endpoint with address 0x%X, skipping\n", |
| 360 |
cfgno, inum, asnum, |
| 361 |
d->bEndpointAddress); |
| 362 |
goto skip_to_next_endpoint_or_interface_descriptor; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/* Accept this endpoint */ |
| 367 |
++ifp->desc.bNumEndpoints; |
| 368 |
INIT_LIST_HEAD(&endpoint->urb_list); |
| 369 |
|
| 370 |
/* |
| 371 |
* Fix up bInterval values outside the legal range. |
| 372 |
* Use 10 or 8 ms if no proper value can be guessed. |
| 373 |
*/ |
| 374 |
i = 0; /* i = min, j = max, n = default */ |
| 375 |
j = 255; |
| 376 |
if (usb_endpoint_xfer_int(d)) { |
| 377 |
i = 1; |
| 378 |
switch (udev->speed) { |
| 379 |
case USB_SPEED_SUPER_PLUS: |
| 380 |
case USB_SPEED_SUPER: |
| 381 |
case USB_SPEED_HIGH: |
| 382 |
/* |
| 383 |
* Many device manufacturers are using full-speed |
| 384 |
* bInterval values in high-speed interrupt endpoint |
| 385 |
* descriptors. Try to fix those and fall back to an |
| 386 |
* 8-ms default value otherwise. |
| 387 |
*/ |
| 388 |
n = fls(d->bInterval*8); |
| 389 |
if (n == 0) |
| 390 |
n = 7; /* 8 ms = 2^(7-1) uframes */ |
| 391 |
j = 16; |
| 392 |
|
| 393 |
/* |
| 394 |
* Adjust bInterval for quirked devices. |
| 395 |
*/ |
| 396 |
/* |
| 397 |
* This quirk fixes bIntervals reported in ms. |
| 398 |
*/ |
| 399 |
if (udev->quirks & USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) { |
| 400 |
n = clamp(fls(d->bInterval) + 3, i, j); |
| 401 |
i = j = n; |
| 402 |
} |
| 403 |
/* |
| 404 |
* This quirk fixes bIntervals reported in |
| 405 |
* linear microframes. |
| 406 |
*/ |
| 407 |
if (udev->quirks & USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) { |
| 408 |
n = clamp(fls(d->bInterval), i, j); |
| 409 |
i = j = n; |
| 410 |
} |
| 411 |
break; |
| 412 |
default: /* USB_SPEED_FULL or _LOW */ |
| 413 |
/* |
| 414 |
* For low-speed, 10 ms is the official minimum. |
| 415 |
* But some "overclocked" devices might want faster |
| 416 |
* polling so we'll allow it. |
| 417 |
*/ |
| 418 |
n = 10; |
| 419 |
break; |
| 420 |
} |
| 421 |
} else if (usb_endpoint_xfer_isoc(d)) { |
| 422 |
i = 1; |
| 423 |
j = 16; |
| 424 |
switch (udev->speed) { |
| 425 |
case USB_SPEED_HIGH: |
| 426 |
n = 7; /* 8 ms = 2^(7-1) uframes */ |
| 427 |
break; |
| 428 |
default: /* USB_SPEED_FULL */ |
| 429 |
n = 4; /* 8 ms = 2^(4-1) frames */ |
| 430 |
break; |
| 431 |
} |
| 432 |
} |
| 433 |
if (d->bInterval < i || d->bInterval > j) { |
| 434 |
dev_notice(ddev, "config %d interface %d altsetting %d " |
| 435 |
"endpoint 0x%X has an invalid bInterval %d, " |
| 436 |
"changing to %d\n", |
| 437 |
cfgno, inum, asnum, |
| 438 |
d->bEndpointAddress, d->bInterval, n); |
| 439 |
endpoint->desc.bInterval = n; |
| 440 |
} |
| 441 |
|
| 442 |
/* Some buggy low-speed devices have Bulk endpoints, which is |
| 443 |
* explicitly forbidden by the USB spec. In an attempt to make |
| 444 |
* them usable, we will try treating them as Interrupt endpoints. |
| 445 |
*/ |
| 446 |
if (udev->speed == USB_SPEED_LOW && usb_endpoint_xfer_bulk(d)) { |
| 447 |
dev_notice(ddev, "config %d interface %d altsetting %d " |
| 448 |
"endpoint 0x%X is Bulk; changing to Interrupt\n", |
| 449 |
cfgno, inum, asnum, d->bEndpointAddress); |
| 450 |
endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT; |
| 451 |
endpoint->desc.bInterval = 1; |
| 452 |
if (usb_endpoint_maxp(&endpoint->desc) > 8) |
| 453 |
endpoint->desc.wMaxPacketSize = cpu_to_le16(8); |
| 454 |
} |
| 455 |
|
| 456 |
/* |
| 457 |
* Validate the wMaxPacketSize field. |
| 458 |
* eUSB2 devices (see USB 2.0 Double Isochronous IN ECN 9.6.6 Endpoint) |
| 459 |
* and devices with isochronous endpoints in altsetting 0 (see USB 2.0 |
| 460 |
* end of section 5.6.3) have wMaxPacketSize = 0. |
| 461 |
* So don't warn about those. |
| 462 |
*/ |
| 463 |
maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize); |
| 464 |
|
| 465 |
if (maxp == 0 && bcdUSB != 0x0220 && |
| 466 |
!(usb_endpoint_xfer_isoc(d) && asnum == 0)) |
| 467 |
dev_notice(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid wMaxPacketSize 0\n", |
| 468 |
cfgno, inum, asnum, d->bEndpointAddress); |
| 469 |
|
| 470 |
/* Find the highest legal maxpacket size for this endpoint */ |
| 471 |
i = 0; /* additional transactions per microframe */ |
| 472 |
switch (udev->speed) { |
| 473 |
case USB_SPEED_LOW: |
| 474 |
maxpacket_maxes = low_speed_maxpacket_maxes; |
| 475 |
break; |
| 476 |
case USB_SPEED_FULL: |
| 477 |
maxpacket_maxes = full_speed_maxpacket_maxes; |
| 478 |
break; |
| 479 |
case USB_SPEED_HIGH: |
| 480 |
/* Multiple-transactions bits are allowed only for HS periodic endpoints */ |
| 481 |
if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) { |
| 482 |
i = maxp & USB_EP_MAXP_MULT_MASK; |
| 483 |
maxp &= ~i; |
| 484 |
} |
| 485 |
fallthrough; |
| 486 |
default: |
| 487 |
maxpacket_maxes = high_speed_maxpacket_maxes; |
| 488 |
break; |
| 489 |
case USB_SPEED_SUPER: |
| 490 |
case USB_SPEED_SUPER_PLUS: |
| 491 |
maxpacket_maxes = super_speed_maxpacket_maxes; |
| 492 |
break; |
| 493 |
} |
| 494 |
j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)]; |
| 495 |
|
| 496 |
if (maxp > j) { |
| 497 |
dev_notice(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n", |
| 498 |
cfgno, inum, asnum, d->bEndpointAddress, maxp, j); |
| 499 |
maxp = j; |
| 500 |
endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp); |
| 501 |
} |
| 502 |
|
| 503 |
/* |
| 504 |
* Some buggy high speed devices have bulk endpoints using |
| 505 |
* maxpacket sizes other than 512. High speed HCDs may not |
| 506 |
* be able to handle that particular bug, so let's warn... |
| 507 |
*/ |
| 508 |
if (udev->speed == USB_SPEED_HIGH && usb_endpoint_xfer_bulk(d)) { |
| 509 |
if (maxp != 512) |
| 510 |
dev_notice(ddev, "config %d interface %d altsetting %d " |
| 511 |
"bulk endpoint 0x%X has invalid maxpacket %d\n", |
| 512 |
cfgno, inum, asnum, d->bEndpointAddress, |
| 513 |
maxp); |
| 514 |
} |
| 515 |
|
| 516 |
/* Parse a possible eUSB2 periodic endpoint companion descriptor */ |
| 517 |
if (udev->speed == USB_SPEED_HIGH && bcdUSB == 0x0220 && |
| 518 |
!le16_to_cpu(d->wMaxPacketSize) && usb_endpoint_is_isoc_in(d)) |
| 519 |
usb_parse_eusb2_isoc_endpoint_companion(ddev, cfgno, inum, asnum, |
| 520 |
endpoint, buffer, size); |
| 521 |
|
| 522 |
/* Parse a possible SuperSpeed endpoint companion descriptor */ |
| 523 |
if (udev->speed >= USB_SPEED_SUPER) |
| 524 |
usb_parse_ss_endpoint_companion(ddev, cfgno, |
| 525 |
inum, asnum, endpoint, buffer, size); |
| 526 |
|
| 527 |
/* Skip over any Class Specific or Vendor Specific descriptors; |
| 528 |
* find the next endpoint or interface descriptor */ |
| 529 |
endpoint->extra = buffer; |
| 530 |
i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, |
| 531 |
USB_DT_INTERFACE, &n); |
| 532 |
endpoint->extralen = i; |
| 533 |
retval = buffer - buffer0 + i; |
| 534 |
if (n > 0) |
| 535 |
dev_dbg(ddev, "skipped %d descriptor%s after %s\n", |
| 536 |
n, str_plural(n), "endpoint"); |
| 537 |
return retval; |
| 538 |
|
| 539 |
skip_to_next_endpoint_or_interface_descriptor: |
| 540 |
i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, |
| 541 |
USB_DT_INTERFACE, NULL); |
| 542 |
return buffer - buffer0 + i; |
| 543 |
} |
| 544 |
|
| 545 |
void usb_release_interface_cache(struct kref *ref) |
| 546 |
{ |
| 547 |
struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref); |
| 548 |
int j; |
| 549 |
|
| 550 |
for (j = 0; j < intfc->num_altsetting; j++) { |
| 551 |
struct usb_host_interface *alt = &intfc->altsetting[j]; |
| 552 |
|
| 553 |
kfree(alt->endpoint); |
| 554 |
kfree(alt->string); |
| 555 |
} |
| 556 |
kfree(intfc); |
| 557 |
} |
| 558 |
|
| 559 |
static int usb_parse_interface(struct device *ddev, int cfgno, |
| 560 |
struct usb_host_config *config, unsigned char *buffer, int size, |
| 561 |
u8 inums[], u8 nalts[]) |
| 562 |
{ |
| 563 |
unsigned char *buffer0 = buffer; |
| 564 |
struct usb_interface_descriptor *d; |
| 565 |
int inum, asnum; |
| 566 |
struct usb_interface_cache *intfc; |
| 567 |
struct usb_host_interface *alt; |
| 568 |
int i, n; |
| 569 |
int len, retval; |
| 570 |
int num_ep, num_ep_orig; |
| 571 |
|
| 572 |
d = (struct usb_interface_descriptor *) buffer; |
| 573 |
buffer += d->bLength; |
| 574 |
size -= d->bLength; |
| 575 |
|
| 576 |
if (d->bLength < USB_DT_INTERFACE_SIZE) |
| 577 |
goto skip_to_next_interface_descriptor; |
| 578 |
|
| 579 |
/* Which interface entry is this? */ |
| 580 |
intfc = NULL; |
| 581 |
inum = d->bInterfaceNumber; |
| 582 |
for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
| 583 |
if (inums[i] == inum) { |
| 584 |
intfc = config->intf_cache[i]; |
| 585 |
break; |
| 586 |
} |
| 587 |
} |
| 588 |
if (!intfc || intfc->num_altsetting >= nalts[i]) |
| 589 |
goto skip_to_next_interface_descriptor; |
| 590 |
|
| 591 |
/* Check for duplicate altsetting entries */ |
| 592 |
asnum = d->bAlternateSetting; |
| 593 |
for ((i = 0, alt = &intfc->altsetting[0]); |
| 594 |
i < intfc->num_altsetting; |
| 595 |
(++i, ++alt)) { |
| 596 |
if (alt->desc.bAlternateSetting == asnum) { |
| 597 |
dev_notice(ddev, "Duplicate descriptor for config %d " |
| 598 |
"interface %d altsetting %d, skipping\n", |
| 599 |
cfgno, inum, asnum); |
| 600 |
goto skip_to_next_interface_descriptor; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
++intfc->num_altsetting; |
| 605 |
memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE); |
| 606 |
|
| 607 |
/* Skip over any Class Specific or Vendor Specific descriptors; |
| 608 |
* find the first endpoint or interface descriptor */ |
| 609 |
alt->extra = buffer; |
| 610 |
i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, |
| 611 |
USB_DT_INTERFACE, &n); |
| 612 |
alt->extralen = i; |
| 613 |
if (n > 0) |
| 614 |
dev_dbg(ddev, "skipped %d descriptor%s after %s\n", |
| 615 |
n, str_plural(n), "interface"); |
| 616 |
buffer += i; |
| 617 |
size -= i; |
| 618 |
|
| 619 |
/* Allocate space for the right(?) number of endpoints */ |
| 620 |
num_ep = num_ep_orig = alt->desc.bNumEndpoints; |
| 621 |
alt->desc.bNumEndpoints = 0; /* Use as a counter */ |
| 622 |
if (num_ep > USB_MAXENDPOINTS) { |
| 623 |
dev_notice(ddev, "too many endpoints for config %d interface %d " |
| 624 |
"altsetting %d: %d, using maximum allowed: %d\n", |
| 625 |
cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS); |
| 626 |
num_ep = USB_MAXENDPOINTS; |
| 627 |
} |
| 628 |
|
| 629 |
if (num_ep > 0) { |
| 630 |
/* Can't allocate 0 bytes */ |
| 631 |
len = sizeof(struct usb_host_endpoint) * num_ep; |
| 632 |
alt->endpoint = kzalloc(len, GFP_KERNEL); |
| 633 |
if (!alt->endpoint) |
| 634 |
return -ENOMEM; |
| 635 |
} |
| 636 |
|
| 637 |
/* Parse all the endpoint descriptors */ |
| 638 |
n = 0; |
| 639 |
while (size > 0) { |
| 640 |
if (((struct usb_descriptor_header *) buffer)->bDescriptorType |
| 641 |
== USB_DT_INTERFACE) |
| 642 |
break; |
| 643 |
retval = usb_parse_endpoint(ddev, cfgno, config, inum, asnum, |
| 644 |
alt, num_ep, buffer, size); |
| 645 |
if (retval < 0) |
| 646 |
return retval; |
| 647 |
++n; |
| 648 |
|
| 649 |
buffer += retval; |
| 650 |
size -= retval; |
| 651 |
} |
| 652 |
|
| 653 |
if (n != num_ep_orig) |
| 654 |
dev_notice(ddev, "config %d interface %d altsetting %d has %d " |
| 655 |
"endpoint descriptor%s, different from the interface " |
| 656 |
"descriptor's value: %d\n", |
| 657 |
cfgno, inum, asnum, n, str_plural(n), num_ep_orig); |
| 658 |
return buffer - buffer0; |
| 659 |
|
| 660 |
skip_to_next_interface_descriptor: |
| 661 |
i = find_next_descriptor(buffer, size, USB_DT_INTERFACE, |
| 662 |
USB_DT_INTERFACE, NULL); |
| 663 |
return buffer - buffer0 + i; |
| 664 |
} |
| 665 |
|
| 666 |
static int usb_parse_configuration(struct usb_device *dev, int cfgidx, |
| 667 |
struct usb_host_config *config, unsigned char *buffer, int size) |
| 668 |
{ |
| 669 |
struct device *ddev = &dev->dev; |
| 670 |
unsigned char *buffer0 = buffer; |
| 671 |
int cfgno; |
| 672 |
int nintf, nintf_orig; |
| 673 |
int i, j, n; |
| 674 |
struct usb_interface_cache *intfc; |
| 675 |
unsigned char *buffer2; |
| 676 |
int size2; |
| 677 |
struct usb_descriptor_header *header; |
| 678 |
int retval; |
| 679 |
u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES]; |
| 680 |
unsigned iad_num = 0; |
| 681 |
|
| 682 |
memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE); |
| 683 |
nintf = nintf_orig = config->desc.bNumInterfaces; |
| 684 |
config->desc.bNumInterfaces = 0; // Adjusted later |
| 685 |
|
| 686 |
if (config->desc.bDescriptorType != USB_DT_CONFIG || |
| 687 |
config->desc.bLength < USB_DT_CONFIG_SIZE || |
| 688 |
config->desc.bLength > size) { |
| 689 |
dev_notice(ddev, "invalid descriptor for config index %d: " |
| 690 |
"type = 0x%X, length = %d\n", cfgidx, |
| 691 |
config->desc.bDescriptorType, config->desc.bLength); |
| 692 |
return -EINVAL; |
| 693 |
} |
| 694 |
cfgno = config->desc.bConfigurationValue; |
| 695 |
|
| 696 |
buffer += config->desc.bLength; |
| 697 |
size -= config->desc.bLength; |
| 698 |
|
| 699 |
if (nintf > USB_MAXINTERFACES) { |
| 700 |
dev_notice(ddev, "config %d has too many interfaces: %d, " |
| 701 |
"using maximum allowed: %d\n", |
| 702 |
cfgno, nintf, USB_MAXINTERFACES); |
| 703 |
nintf = USB_MAXINTERFACES; |
| 704 |
} |
| 705 |
|
| 706 |
/* Go through the descriptors, checking their length and counting the |
| 707 |
* number of altsettings for each interface */ |
| 708 |
n = 0; |
| 709 |
for ((buffer2 = buffer, size2 = size); |
| 710 |
size2 > 0; |
| 711 |
(buffer2 += header->bLength, size2 -= header->bLength)) { |
| 712 |
|
| 713 |
if (size2 < sizeof(struct usb_descriptor_header)) { |
| 714 |
dev_notice(ddev, "config %d descriptor has %d excess " |
| 715 |
"byte%s, ignoring\n", |
| 716 |
cfgno, size2, str_plural(size2)); |
| 717 |
break; |
| 718 |
} |
| 719 |
|
| 720 |
header = (struct usb_descriptor_header *) buffer2; |
| 721 |
if ((header->bLength > size2) || (header->bLength < 2)) { |
| 722 |
dev_notice(ddev, "config %d has an invalid descriptor " |
| 723 |
"of length %d, skipping remainder of the config\n", |
| 724 |
cfgno, header->bLength); |
| 725 |
break; |
| 726 |
} |
| 727 |
|
| 728 |
if (header->bDescriptorType == USB_DT_INTERFACE) { |
| 729 |
struct usb_interface_descriptor *d; |
| 730 |
int inum; |
| 731 |
|
| 732 |
d = (struct usb_interface_descriptor *) header; |
| 733 |
if (d->bLength < USB_DT_INTERFACE_SIZE) { |
| 734 |
dev_notice(ddev, "config %d has an invalid " |
| 735 |
"interface descriptor of length %d, " |
| 736 |
"skipping\n", cfgno, d->bLength); |
| 737 |
continue; |
| 738 |
} |
| 739 |
|
| 740 |
inum = d->bInterfaceNumber; |
| 741 |
|
| 742 |
if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) && |
| 743 |
n >= nintf_orig) { |
| 744 |
dev_notice(ddev, "config %d has more interface " |
| 745 |
"descriptors, than it declares in " |
| 746 |
"bNumInterfaces, ignoring interface " |
| 747 |
"number: %d\n", cfgno, inum); |
| 748 |
continue; |
| 749 |
} |
| 750 |
|
| 751 |
if (inum >= nintf_orig) |
| 752 |
dev_notice(ddev, "config %d has an invalid " |
| 753 |
"interface number: %d but max is %d\n", |
| 754 |
cfgno, inum, nintf_orig - 1); |
| 755 |
|
| 756 |
/* Have we already encountered this interface? |
| 757 |
* Count its altsettings */ |
| 758 |
for (i = 0; i < n; ++i) { |
| 759 |
if (inums[i] == inum) |
| 760 |
break; |
| 761 |
} |
| 762 |
if (i < n) { |
| 763 |
if (nalts[i] < 255) |
| 764 |
++nalts[i]; |
| 765 |
} else if (n < USB_MAXINTERFACES) { |
| 766 |
inums[n] = inum; |
| 767 |
nalts[n] = 1; |
| 768 |
++n; |
| 769 |
} |
| 770 |
|
| 771 |
} else if (header->bDescriptorType == |
| 772 |
USB_DT_INTERFACE_ASSOCIATION) { |
| 773 |
struct usb_interface_assoc_descriptor *d; |
| 774 |
|
| 775 |
d = (struct usb_interface_assoc_descriptor *)header; |
| 776 |
if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) { |
| 777 |
dev_notice(ddev, |
| 778 |
"config %d has an invalid interface association descriptor of length %d, skipping\n", |
| 779 |
cfgno, d->bLength); |
| 780 |
continue; |
| 781 |
} |
| 782 |
|
| 783 |
if (iad_num == USB_MAXIADS) { |
| 784 |
dev_notice(ddev, "found more Interface " |
| 785 |
"Association Descriptors " |
| 786 |
"than allocated for in " |
| 787 |
"configuration %d\n", cfgno); |
| 788 |
} else { |
| 789 |
config->intf_assoc[iad_num] = d; |
| 790 |
iad_num++; |
| 791 |
} |
| 792 |
|
| 793 |
} else if (header->bDescriptorType == USB_DT_DEVICE || |
| 794 |
header->bDescriptorType == USB_DT_CONFIG) |
| 795 |
dev_notice(ddev, "config %d contains an unexpected " |
| 796 |
"descriptor of type 0x%X, skipping\n", |
| 797 |
cfgno, header->bDescriptorType); |
| 798 |
|
| 799 |
} /* for ((buffer2 = buffer, size2 = size); ...) */ |
| 800 |
size = buffer2 - buffer; |
| 801 |
config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0); |
| 802 |
|
| 803 |
if (n != nintf) |
| 804 |
dev_notice(ddev, "config %d has %d interface%s, different from " |
| 805 |
"the descriptor's value: %d\n", |
| 806 |
cfgno, n, str_plural(n), nintf_orig); |
| 807 |
else if (n == 0) |
| 808 |
dev_notice(ddev, "config %d has no interfaces?\n", cfgno); |
| 809 |
config->desc.bNumInterfaces = nintf = n; |
| 810 |
|
| 811 |
/* Check for missing interface numbers */ |
| 812 |
for (i = 0; i < nintf; ++i) { |
| 813 |
for (j = 0; j < nintf; ++j) { |
| 814 |
if (inums[j] == i) |
| 815 |
break; |
| 816 |
} |
| 817 |
if (j >= nintf) |
| 818 |
dev_notice(ddev, "config %d has no interface number " |
| 819 |
"%d\n", cfgno, i); |
| 820 |
} |
| 821 |
|
| 822 |
/* Allocate the usb_interface_caches and altsetting arrays */ |
| 823 |
for (i = 0; i < nintf; ++i) { |
| 824 |
j = nalts[i]; |
| 825 |
if (j > USB_MAXALTSETTING) { |
| 826 |
dev_notice(ddev, "too many alternate settings for " |
| 827 |
"config %d interface %d: %d, " |
| 828 |
"using maximum allowed: %d\n", |
| 829 |
cfgno, inums[i], j, USB_MAXALTSETTING); |
| 830 |
nalts[i] = j = USB_MAXALTSETTING; |
| 831 |
} |
| 832 |
|
| 833 |
intfc = kzalloc_flex(*intfc, altsetting, j); |
| 834 |
config->intf_cache[i] = intfc; |
| 835 |
if (!intfc) |
| 836 |
return -ENOMEM; |
| 837 |
kref_init(&intfc->ref); |
| 838 |
} |
| 839 |
|
| 840 |
/* FIXME: parse the BOS descriptor */ |
| 841 |
|
| 842 |
/* Skip over any Class Specific or Vendor Specific descriptors; |
| 843 |
* find the first interface descriptor */ |
| 844 |
config->extra = buffer; |
| 845 |
i = find_next_descriptor(buffer, size, USB_DT_INTERFACE, |
| 846 |
USB_DT_INTERFACE, &n); |
| 847 |
config->extralen = i; |
| 848 |
if (n > 0) |
| 849 |
dev_dbg(ddev, "skipped %d descriptor%s after %s\n", |
| 850 |
n, str_plural(n), "configuration"); |
| 851 |
buffer += i; |
| 852 |
size -= i; |
| 853 |
|
| 854 |
/* Parse all the interface/altsetting descriptors */ |
| 855 |
while (size > 0) { |
| 856 |
retval = usb_parse_interface(ddev, cfgno, config, |
| 857 |
buffer, size, inums, nalts); |
| 858 |
if (retval < 0) |
| 859 |
return retval; |
| 860 |
|
| 861 |
buffer += retval; |
| 862 |
size -= retval; |
| 863 |
} |
| 864 |
|
| 865 |
/* Check for missing altsettings */ |
| 866 |
for (i = 0; i < nintf; ++i) { |
| 867 |
intfc = config->intf_cache[i]; |
| 868 |
for (j = 0; j < intfc->num_altsetting; ++j) { |
| 869 |
for (n = 0; n < intfc->num_altsetting; ++n) { |
| 870 |
if (intfc->altsetting[n].desc. |
| 871 |
bAlternateSetting == j) |
| 872 |
break; |
| 873 |
} |
| 874 |
if (n >= intfc->num_altsetting) |
| 875 |
dev_notice(ddev, "config %d interface %d has no " |
| 876 |
"altsetting %d\n", cfgno, inums[i], j); |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
return 0; |
| 881 |
} |
| 882 |
|
| 883 |
/* hub-only!! ... and only exported for reset/reinit path. |
| 884 |
* otherwise used internally on disconnect/destroy path |
| 885 |
*/ |
| 886 |
void usb_destroy_configuration(struct usb_device *dev) |
| 887 |
{ |
| 888 |
int c, i; |
| 889 |
|
| 890 |
if (!dev->config) |
| 891 |
return; |
| 892 |
|
| 893 |
if (dev->rawdescriptors) { |
| 894 |
for (i = 0; i < dev->descriptor.bNumConfigurations; i++) |
| 895 |
kfree(dev->rawdescriptors[i]); |
| 896 |
|
| 897 |
kfree(dev->rawdescriptors); |
| 898 |
dev->rawdescriptors = NULL; |
| 899 |
} |
| 900 |
|
| 901 |
for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { |
| 902 |
struct usb_host_config *cf = &dev->config[c]; |
| 903 |
|
| 904 |
kfree(cf->string); |
| 905 |
for (i = 0; i < cf->desc.bNumInterfaces; i++) { |
| 906 |
if (cf->intf_cache[i]) |
| 907 |
kref_put(&cf->intf_cache[i]->ref, |
| 908 |
usb_release_interface_cache); |
| 909 |
} |
| 910 |
} |
| 911 |
kfree(dev->config); |
| 912 |
dev->config = NULL; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
/* |
| 917 |
* Get the USB config descriptors, cache and parse'em |
| 918 |
* |
| 919 |
* hub-only!! ... and only in reset path, or usb_new_device() |
| 920 |
* (used by real hubs and virtual root hubs) |
| 921 |
*/ |
| 922 |
int usb_get_configuration(struct usb_device *dev) |
| 923 |
{ |
| 924 |
struct device *ddev = &dev->dev; |
| 925 |
int ncfg = dev->descriptor.bNumConfigurations; |
| 926 |
unsigned int cfgno, length; |
| 927 |
unsigned char *bigbuffer; |
| 928 |
struct usb_config_descriptor *desc; |
| 929 |
int result; |
| 930 |
|
| 931 |
if (ncfg > USB_MAXCONFIG) { |
| 932 |
dev_notice(ddev, "too many configurations: %d, " |
| 933 |
"using maximum allowed: %d\n", ncfg, USB_MAXCONFIG); |
| 934 |
dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG; |
| 935 |
} |
| 936 |
|
| 937 |
if (ncfg < 1 && dev->quirks & USB_QUIRK_FORCE_ONE_CONFIG) { |
| 938 |
dev_info(ddev, "Device claims zero configurations, forcing to 1\n"); |
| 939 |
dev->descriptor.bNumConfigurations = 1; |
| 940 |
ncfg = 1; |
| 941 |
} else if (ncfg < 1) { |
| 942 |
dev_err(ddev, "no configurations\n"); |
| 943 |
return -EINVAL; |
| 944 |
} |
| 945 |
|
| 946 |
length = ncfg * sizeof(struct usb_host_config); |
| 947 |
dev->config = kzalloc(length, GFP_KERNEL); |
| 948 |
if (!dev->config) |
| 949 |
return -ENOMEM; |
| 950 |
|
| 951 |
length = ncfg * sizeof(char *); |
| 952 |
dev->rawdescriptors = kzalloc(length, GFP_KERNEL); |
| 953 |
if (!dev->rawdescriptors) |
| 954 |
return -ENOMEM; |
| 955 |
|
| 956 |
desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL); |
| 957 |
if (!desc) |
| 958 |
return -ENOMEM; |
| 959 |
|
| 960 |
for (cfgno = 0; cfgno < ncfg; cfgno++) { |
| 961 |
/* We grab just the first descriptor so we know how long |
| 962 |
* the whole configuration is */ |
| 963 |
result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, |
| 964 |
desc, USB_DT_CONFIG_SIZE); |
| 965 |
if (result < 0) { |
| 966 |
dev_err(ddev, "unable to read config index %d " |
| 967 |
"descriptor/%s: %d\n", cfgno, "start", result); |
| 968 |
if (result != -EPIPE) |
| 969 |
goto err; |
| 970 |
dev_notice(ddev, "chopping to %d config(s)\n", cfgno); |
| 971 |
dev->descriptor.bNumConfigurations = cfgno; |
| 972 |
break; |
| 973 |
} else if (result < 4) { |
| 974 |
dev_err(ddev, "config index %d descriptor too short " |
| 975 |
"(expected %i, got %i)\n", cfgno, |
| 976 |
USB_DT_CONFIG_SIZE, result); |
| 977 |
result = -EINVAL; |
| 978 |
goto err; |
| 979 |
} |
| 980 |
length = max_t(int, le16_to_cpu(desc->wTotalLength), |
| 981 |
USB_DT_CONFIG_SIZE); |
| 982 |
|
| 983 |
/* Now that we know the length, get the whole thing */ |
| 984 |
bigbuffer = kmalloc(length, GFP_KERNEL); |
| 985 |
if (!bigbuffer) { |
| 986 |
result = -ENOMEM; |
| 987 |
goto err; |
| 988 |
} |
| 989 |
|
| 990 |
if (dev->quirks & USB_QUIRK_DELAY_INIT) |
| 991 |
msleep(200); |
| 992 |
|
| 993 |
result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, |
| 994 |
bigbuffer, length); |
| 995 |
if (result < 0) { |
| 996 |
dev_err(ddev, "unable to read config index %d " |
| 997 |
"descriptor/%s\n", cfgno, "all"); |
| 998 |
kfree(bigbuffer); |
| 999 |
goto err; |
| 1000 |
} |
| 1001 |
if (result < length) { |
| 1002 |
dev_notice(ddev, "config index %d descriptor too short " |
| 1003 |
"(expected %i, got %i)\n", cfgno, length, result); |
| 1004 |
length = result; |
| 1005 |
} |
| 1006 |
|
| 1007 |
dev->rawdescriptors[cfgno] = bigbuffer; |
| 1008 |
|
| 1009 |
result = usb_parse_configuration(dev, cfgno, |
| 1010 |
&dev->config[cfgno], bigbuffer, length); |
| 1011 |
if (result < 0) { |
| 1012 |
++cfgno; |
| 1013 |
goto err; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
err: |
| 1018 |
kfree(desc); |
| 1019 |
dev->descriptor.bNumConfigurations = cfgno; |
| 1020 |
|
| 1021 |
return result; |
| 1022 |
} |
| 1023 |
|
| 1024 |
void usb_release_bos_descriptor(struct usb_device *dev) |
| 1025 |
{ |
| 1026 |
if (dev->bos) { |
| 1027 |
kfree(dev->bos->desc); |
| 1028 |
kfree(dev->bos); |
| 1029 |
dev->bos = NULL; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
static const __u8 bos_desc_len[256] = { |
| 1034 |
[USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE, |
| 1035 |
[USB_CAP_TYPE_EXT] = USB_DT_USB_EXT_CAP_SIZE, |
| 1036 |
[USB_SS_CAP_TYPE] = USB_DT_USB_SS_CAP_SIZE, |
| 1037 |
[USB_SSP_CAP_TYPE] = USB_DT_USB_SSP_CAP_SIZE(1), |
| 1038 |
[CONTAINER_ID_TYPE] = USB_DT_USB_SS_CONTN_ID_SIZE, |
| 1039 |
[USB_PTM_CAP_TYPE] = USB_DT_USB_PTM_ID_SIZE, |
| 1040 |
}; |
| 1041 |
|
| 1042 |
/* Get BOS descriptor set */ |
| 1043 |
int usb_get_bos_descriptor(struct usb_device *dev) |
| 1044 |
{ |
| 1045 |
struct device *ddev = &dev->dev; |
| 1046 |
struct usb_bos_descriptor *bos; |
| 1047 |
struct usb_dev_cap_header *cap; |
| 1048 |
struct usb_ssp_cap_descriptor *ssp_cap; |
| 1049 |
unsigned char *buffer, *buffer0; |
| 1050 |
int length, total_len, num, i, ssac; |
| 1051 |
__u8 cap_type; |
| 1052 |
int ret; |
| 1053 |
|
| 1054 |
if (dev->quirks & USB_QUIRK_NO_BOS) { |
| 1055 |
dev_dbg(ddev, "skipping BOS descriptor\n"); |
| 1056 |
return -ENOMSG; |
| 1057 |
} |
| 1058 |
|
| 1059 |
bos = kzalloc_obj(*bos); |
| 1060 |
if (!bos) |
| 1061 |
return -ENOMEM; |
| 1062 |
|
| 1063 |
/* Get BOS descriptor */ |
| 1064 |
ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE); |
| 1065 |
if (ret < USB_DT_BOS_SIZE || bos->bLength < USB_DT_BOS_SIZE) { |
| 1066 |
dev_notice(ddev, "unable to get BOS descriptor or descriptor too short\n"); |
| 1067 |
if (ret >= 0) |
| 1068 |
ret = -ENOMSG; |
| 1069 |
kfree(bos); |
| 1070 |
return ret; |
| 1071 |
} |
| 1072 |
|
| 1073 |
length = bos->bLength; |
| 1074 |
total_len = le16_to_cpu(bos->wTotalLength); |
| 1075 |
num = bos->bNumDeviceCaps; |
| 1076 |
kfree(bos); |
| 1077 |
if (total_len < length) |
| 1078 |
return -EINVAL; |
| 1079 |
|
| 1080 |
dev->bos = kzalloc_obj(*dev->bos); |
| 1081 |
if (!dev->bos) |
| 1082 |
return -ENOMEM; |
| 1083 |
|
| 1084 |
/* Now let's get the whole BOS descriptor set */ |
| 1085 |
buffer = kzalloc(total_len, GFP_KERNEL); |
| 1086 |
if (!buffer) { |
| 1087 |
ret = -ENOMEM; |
| 1088 |
goto err; |
| 1089 |
} |
| 1090 |
dev->bos->desc = (struct usb_bos_descriptor *)buffer; |
| 1091 |
|
| 1092 |
ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len); |
| 1093 |
if (ret < total_len) { |
| 1094 |
dev_notice(ddev, "unable to get BOS descriptor set\n"); |
| 1095 |
if (ret >= 0) |
| 1096 |
ret = -ENOMSG; |
| 1097 |
goto err; |
| 1098 |
} |
| 1099 |
|
| 1100 |
buffer0 = buffer; |
| 1101 |
total_len -= length; |
| 1102 |
buffer += length; |
| 1103 |
|
| 1104 |
for (i = 0; i < num; i++) { |
| 1105 |
cap = (struct usb_dev_cap_header *)buffer; |
| 1106 |
|
| 1107 |
if (total_len < sizeof(*cap) || total_len < cap->bLength) { |
| 1108 |
dev->bos->desc->bNumDeviceCaps = i; |
| 1109 |
break; |
| 1110 |
} |
| 1111 |
cap_type = cap->bDevCapabilityType; |
| 1112 |
length = cap->bLength; |
| 1113 |
if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) { |
| 1114 |
dev->bos->desc->bNumDeviceCaps = i; |
| 1115 |
break; |
| 1116 |
} |
| 1117 |
|
| 1118 |
if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) { |
| 1119 |
dev_notice(ddev, "descriptor type invalid, skip\n"); |
| 1120 |
goto skip_to_next_descriptor; |
| 1121 |
} |
| 1122 |
|
| 1123 |
switch (cap_type) { |
| 1124 |
case USB_CAP_TYPE_EXT: |
| 1125 |
dev->bos->ext_cap = |
| 1126 |
(struct usb_ext_cap_descriptor *)buffer; |
| 1127 |
break; |
| 1128 |
case USB_SS_CAP_TYPE: |
| 1129 |
dev->bos->ss_cap = |
| 1130 |
(struct usb_ss_cap_descriptor *)buffer; |
| 1131 |
break; |
| 1132 |
case USB_SSP_CAP_TYPE: |
| 1133 |
ssp_cap = (struct usb_ssp_cap_descriptor *)buffer; |
| 1134 |
ssac = (le32_to_cpu(ssp_cap->bmAttributes) & |
| 1135 |
USB_SSP_SUBLINK_SPEED_ATTRIBS); |
| 1136 |
if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac)) |
| 1137 |
dev->bos->ssp_cap = ssp_cap; |
| 1138 |
break; |
| 1139 |
case CONTAINER_ID_TYPE: |
| 1140 |
dev->bos->ss_id = |
| 1141 |
(struct usb_ss_container_id_descriptor *)buffer; |
| 1142 |
break; |
| 1143 |
case USB_PTM_CAP_TYPE: |
| 1144 |
dev->bos->ptm_cap = |
| 1145 |
(struct usb_ptm_cap_descriptor *)buffer; |
| 1146 |
break; |
| 1147 |
default: |
| 1148 |
break; |
| 1149 |
} |
| 1150 |
|
| 1151 |
skip_to_next_descriptor: |
| 1152 |
total_len -= length; |
| 1153 |
buffer += length; |
| 1154 |
} |
| 1155 |
dev->bos->desc->wTotalLength = cpu_to_le16(buffer - buffer0); |
| 1156 |
|
| 1157 |
return 0; |
| 1158 |
|
| 1159 |
err: |
| 1160 |
usb_release_bos_descriptor(dev); |
| 1161 |
return ret; |
| 1162 |
} |