LCOV - code coverage report
Current view: top level - drivers/usb/core - config.c (source / functions) Hit Total Coverage
Test: Real Lines: 184 396 46.5 %
Date: 2020-10-17 15:46:16 Functions: 0 11 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14