2 #include <linux/usb/ch9.h>
5 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/device.h>
9 #include <asm/byteorder.h>
13 #define USB_MAXALTSETTING 128
14 #define USB_MAXENDPOINTS 30
16 #define USB_MAXCONFIG 8
19 static inline const char *plural(
int n)
21 return (n == 1 ?
"" :
"s");
24 static int find_next_descriptor(
unsigned char *
buffer,
int size,
25 int dt1,
int dt2,
int *num_skipped)
29 unsigned char *buffer0 =
buffer;
45 return buffer - buffer0;
48 static void usb_parse_ss_endpoint_companion(
struct device *ddev,
int cfgno,
49 int inum,
int asnum,
struct usb_host_endpoint *ep,
50 unsigned char *buffer,
int size)
61 dev_warn(ddev,
"No SuperSpeed endpoint companion for config %d "
62 " interface %d altsetting %d ep %d: "
63 "using minimum values\n",
64 cfgno, inum, asnum, ep->
desc.bEndpointAddress);
75 if (usb_endpoint_xfer_isoc(&ep->desc) ||
76 usb_endpoint_xfer_int(&ep->desc))
77 ep->ss_ep_comp.wBytesPerInterval =
78 ep->desc.wMaxPacketSize;
85 if (usb_endpoint_xfer_control(&ep->desc) && desc->
bMaxBurst != 0) {
86 dev_warn(ddev,
"Control endpoint with bMaxBurst = %d in "
87 "config %d interface %d altsetting %d ep %d: "
89 cfgno, inum, asnum, ep->desc.bEndpointAddress);
90 ep->ss_ep_comp.bMaxBurst = 0;
92 dev_warn(ddev,
"Endpoint with bMaxBurst = %d in "
93 "config %d interface %d altsetting %d ep %d: "
95 cfgno, inum, asnum, ep->desc.bEndpointAddress);
96 ep->ss_ep_comp.bMaxBurst = 15;
99 if ((usb_endpoint_xfer_control(&ep->desc) ||
100 usb_endpoint_xfer_int(&ep->desc)) &&
102 dev_warn(ddev,
"%s endpoint with bmAttributes = %d in "
103 "config %d interface %d altsetting %d ep %d: "
105 usb_endpoint_xfer_control(&ep->desc) ?
"Control" :
"Bulk",
107 cfgno, inum, asnum, ep->desc.bEndpointAddress);
108 ep->ss_ep_comp.bmAttributes = 0;
109 }
else if (usb_endpoint_xfer_bulk(&ep->desc) &&
111 dev_warn(ddev,
"Bulk endpoint with more than 65536 streams in "
112 "config %d interface %d altsetting %d ep %d: "
114 cfgno, inum, asnum, ep->
desc.bEndpointAddress);
115 ep->ss_ep_comp.bmAttributes = 16;
116 }
else if (usb_endpoint_xfer_isoc(&ep->desc) &&
118 dev_warn(ddev,
"Isoc endpoint has Mult of %d in "
119 "config %d interface %d altsetting %d ep %d: "
121 cfgno, inum, asnum, ep->desc.bEndpointAddress);
122 ep->ss_ep_comp.bmAttributes = 2;
125 if (usb_endpoint_xfer_isoc(&ep->desc))
127 usb_endpoint_maxp(&ep->desc);
128 else if (usb_endpoint_xfer_int(&ep->desc))
129 max_tx = usb_endpoint_maxp(&ep->desc) *
134 dev_warn(ddev,
"%s endpoint with wBytesPerInterval of %d in "
135 "config %d interface %d altsetting %d ep %d: "
137 usb_endpoint_xfer_isoc(&ep->desc) ?
"Isoc" :
"Int",
139 cfgno, inum, asnum, ep->desc.bEndpointAddress,
141 ep->ss_ep_comp.wBytesPerInterval =
cpu_to_le16(max_tx);
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)
149 unsigned char *buffer0 =
buffer;
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;
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",
174 goto skip_to_next_endpoint_or_interface_descriptor;
178 if (ifp->desc.bNumEndpoints >= num_ep)
179 goto skip_to_next_endpoint_or_interface_descriptor;
181 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
182 ++ifp->desc.bNumEndpoints;
184 memcpy(&endpoint->desc, d, n);
185 INIT_LIST_HEAD(&endpoint->urb_list);
191 if (usb_endpoint_xfer_int(d)) {
193 switch (to_usb_device(ddev)->speed) {
212 }
else if (usb_endpoint_xfer_isoc(d)) {
215 switch (to_usb_device(ddev)->speed) {
225 dev_warn(ddev,
"config %d interface %d altsetting %d "
226 "endpoint 0x%X has an invalid bInterval %d, "
230 endpoint->desc.bInterval =
n;
238 usb_endpoint_xfer_bulk(d)) {
239 dev_warn(ddev,
"config %d interface %d altsetting %d "
240 "endpoint 0x%X is Bulk; changing to Interrupt\n",
243 endpoint->desc.bInterval = 1;
244 if (usb_endpoint_maxp(&endpoint->desc) > 8)
254 && usb_endpoint_xfer_bulk(d)) {
257 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
259 dev_warn(ddev,
"config %d interface %d altsetting %d "
260 "bulk endpoint 0x%X has invalid maxpacket %d\n",
267 usb_parse_ss_endpoint_companion(ddev, cfgno,
268 inum, asnum, endpoint, buffer, size);
275 endpoint->extralen =
i;
276 retval = buffer - buffer0 +
i;
278 dev_dbg(ddev,
"skipped %d descriptor%s after %s\n",
279 n, plural(n),
"endpoint");
282 skip_to_next_endpoint_or_interface_descriptor:
285 return buffer - buffer0 +
i;
290 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
293 for (j = 0; j < intfc->num_altsetting; j++) {
294 struct usb_host_interface *alt = &intfc->altsetting[
j];
296 kfree(alt->endpoint);
302 static int usb_parse_interface(
struct device *ddev,
int cfgno,
303 struct usb_host_config *
config,
unsigned char *buffer,
int size,
304 u8 inums[],
u8 nalts[])
306 unsigned char *buffer0 =
buffer;
309 struct usb_interface_cache *intfc;
310 struct usb_host_interface *alt;
313 int num_ep, num_ep_orig;
320 goto skip_to_next_interface_descriptor;
325 for (i = 0; i < config->desc.bNumInterfaces; ++
i) {
326 if (inums[i] == inum) {
327 intfc = config->intf_cache[
i];
331 if (!intfc || intfc->num_altsetting >= nalts[i])
332 goto skip_to_next_interface_descriptor;
336 for ((i = 0, alt = &intfc->altsetting[0]);
337 i < intfc->num_altsetting;
339 if (alt->desc.bAlternateSetting == asnum) {
340 dev_warn(ddev,
"Duplicate descriptor for config %d "
341 "interface %d altsetting %d, skipping\n",
343 goto skip_to_next_interface_descriptor;
347 ++intfc->num_altsetting;
357 dev_dbg(ddev,
"skipped %d descriptor%s after %s\n",
358 n, plural(n),
"interface");
363 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
364 alt->desc.bNumEndpoints = 0;
366 dev_warn(ddev,
"too many endpoints for config %d interface %d "
367 "altsetting %d: %d, using maximum allowed: %d\n",
374 len =
sizeof(
struct usb_host_endpoint) * num_ep;
386 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
387 num_ep, buffer, size);
396 if (n != num_ep_orig)
397 dev_warn(ddev,
"config %d interface %d altsetting %d has %d "
398 "endpoint descriptor%s, different from the interface "
399 "descriptor's value: %d\n",
400 cfgno, inum, asnum, n, plural(n), num_ep_orig);
401 return buffer - buffer0;
403 skip_to_next_interface_descriptor:
406 return buffer - buffer0 +
i;
409 static int usb_parse_configuration(
struct usb_device *
dev,
int cfgidx,
410 struct usb_host_config *config,
unsigned char *buffer,
int size)
412 struct device *ddev = &dev->dev;
413 unsigned char *buffer0 =
buffer;
415 int nintf, nintf_orig;
417 struct usb_interface_cache *intfc;
422 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
423 unsigned iad_num = 0;
428 dev_err(ddev,
"invalid descriptor for config index %d: "
429 "type = 0x%X, length = %d\n", cfgidx,
430 config->
desc.bDescriptorType, config->desc.bLength);
433 cfgno = config->desc.bConfigurationValue;
435 buffer += config->desc.bLength;
436 size -= config->desc.bLength;
438 nintf = nintf_orig = config->desc.bNumInterfaces;
439 if (nintf > USB_MAXINTERFACES) {
440 dev_warn(ddev,
"config %d has too many interfaces: %d, "
441 "using maximum allowed: %d\n",
442 cfgno, nintf, USB_MAXINTERFACES);
443 nintf = USB_MAXINTERFACES;
449 for ((buffer2 = buffer, size2 = size);
454 dev_warn(ddev,
"config %d descriptor has %d excess "
455 "byte%s, ignoring\n",
456 cfgno, size2, plural(size2));
462 dev_warn(ddev,
"config %d has an invalid descriptor "
463 "of length %d, skipping remainder of the config\n",
474 dev_warn(ddev,
"config %d has an invalid "
475 "interface descriptor of length %d, "
476 "skipping\n", cfgno, d->
bLength);
484 dev_warn(ddev,
"config %d has more interface "
485 "descriptors, than it declares in "
486 "bNumInterfaces, ignoring interface "
487 "number: %d\n", cfgno, inum);
491 if (inum >= nintf_orig)
492 dev_warn(ddev,
"config %d has an invalid "
493 "interface number: %d but max is %d\n",
494 cfgno, inum, nintf_orig - 1);
498 for (i = 0; i <
n; ++
i) {
499 if (inums[i] == inum)
505 }
else if (n < USB_MAXINTERFACES) {
513 if (iad_num == USB_MAXIADS) {
514 dev_warn(ddev,
"found more Interface "
515 "Association Descriptors "
516 "than allocated for in "
517 "configuration %d\n", cfgno);
519 config->intf_assoc[iad_num] =
527 dev_warn(ddev,
"config %d contains an unexpected "
528 "descriptor of type 0x%X, skipping\n",
533 config->desc.wTotalLength =
cpu_to_le16(buffer2 - buffer0);
536 dev_warn(ddev,
"config %d has %d interface%s, different from "
537 "the descriptor's value: %d\n",
538 cfgno, n, plural(n), nintf_orig);
540 dev_warn(ddev,
"config %d has no interfaces?\n", cfgno);
541 config->desc.bNumInterfaces = nintf =
n;
544 for (i = 0; i < nintf; ++
i) {
545 for (j = 0; j < nintf; ++
j) {
550 dev_warn(ddev,
"config %d has no interface number "
555 for (i = 0; i < nintf; ++
i) {
558 dev_warn(ddev,
"too many alternate settings for "
559 "config %d interface %d: %d, "
560 "using maximum allowed: %d\n",
565 len =
sizeof(*intfc) +
sizeof(
struct usb_host_interface) *
j;
566 config->intf_cache[
i] = intfc = kzalloc(len,
GFP_KERNEL);
569 kref_init(&intfc->ref);
579 config->extralen =
i;
581 dev_dbg(ddev,
"skipped %d descriptor%s after %s\n",
582 n, plural(n),
"configuration");
588 retval = usb_parse_interface(ddev, cfgno, config,
589 buffer, size, inums, nalts);
598 for (i = 0; i < nintf; ++
i) {
599 intfc = config->intf_cache[
i];
600 for (j = 0; j < intfc->num_altsetting; ++
j) {
601 for (n = 0; n < intfc->num_altsetting; ++
n) {
602 if (intfc->altsetting[n].desc.
606 if (n >= intfc->num_altsetting)
607 dev_warn(ddev,
"config %d interface %d has no "
608 "altsetting %d\n", cfgno, inums[i], j);
625 if (dev->rawdescriptors) {
626 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
627 kfree(dev->rawdescriptors[i]);
629 kfree(dev->rawdescriptors);
630 dev->rawdescriptors =
NULL;
633 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
634 struct usb_host_config *
cf = &dev->config[
c];
637 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
638 if (cf->intf_cache[i])
639 kref_put(&cf->intf_cache[i]->ref,
660 struct device *ddev = &dev->dev;
661 int ncfg = dev->descriptor.bNumConfigurations;
663 unsigned int cfgno,
length;
664 unsigned char *bigbuffer;
668 if (dev->authorized == 0)
669 goto out_not_authorized;
672 dev_warn(ddev,
"too many configurations: %d, "
678 dev_err(ddev,
"no configurations\n");
682 length = ncfg *
sizeof(
struct usb_host_config);
687 length = ncfg *
sizeof(
char *);
688 dev->rawdescriptors = kzalloc(length,
GFP_KERNEL);
689 if (!dev->rawdescriptors)
697 for (; cfgno < ncfg; cfgno++) {
703 dev_err(ddev,
"unable to read config index %d "
704 "descriptor/%s: %d\n", cfgno,
"start", result);
705 if (result != -
EPIPE)
707 dev_err(ddev,
"chopping to %d config(s)\n", cfgno);
708 dev->descriptor.bNumConfigurations = cfgno;
710 }
else if (result < 4) {
711 dev_err(ddev,
"config index %d descriptor too short "
712 "(expected %i, got %i)\n", cfgno,
729 dev_err(ddev,
"unable to read config index %d "
730 "descriptor/%s\n", cfgno,
"all");
734 if (result < length) {
735 dev_warn(ddev,
"config index %d descriptor too short "
736 "(expected %i, got %i)\n", cfgno, length, result);
740 dev->rawdescriptors[cfgno] = bigbuffer;
742 result = usb_parse_configuration(dev, cfgno,
743 &dev->config[cfgno], bigbuffer, length);
754 dev->descriptor.bNumConfigurations = cfgno;
757 dev_err(ddev,
"out of memory\n");
764 kfree(dev->bos->desc);
773 struct device *ddev = &dev->dev;
787 dev_err(ddev,
"unable to get BOS descriptor\n");
798 if (total_len < length)
801 dev->bos = kzalloc(
sizeof(
struct usb_host_bos),
GFP_KERNEL);
814 if (ret < total_len) {
815 dev_err(ddev,
"unable to get BOS descriptor set\n");
822 for (i = 0; i < num; i++) {
827 if (total_len < length)
832 dev_warn(ddev,
"descriptor type invalid, skip\n");