config.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #include <linux/usb.h>
  2. #include <linux/usb/ch9.h>
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/slab.h>
  6. #include <linux/device.h>
  7. #include <asm/byteorder.h>
  8. #include "usb.h"
  9. #include "hcd.h"
  10. #define USB_MAXALTSETTING 128 /* Hard limit */
  11. #define USB_MAXENDPOINTS 30 /* Hard limit */
  12. #define USB_MAXCONFIG 8 /* Arbitrary limit */
  13. static inline const char *plural(int n)
  14. {
  15. return (n == 1 ? "" : "s");
  16. }
  17. static int find_next_descriptor(unsigned char *buffer, int size,
  18. int dt1, int dt2, int *num_skipped)
  19. {
  20. struct usb_descriptor_header *h;
  21. int n = 0;
  22. unsigned char *buffer0 = buffer;
  23. /* Find the next descriptor of type dt1 or dt2 */
  24. while (size > 0) {
  25. h = (struct usb_descriptor_header *) buffer;
  26. if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
  27. break;
  28. buffer += h->bLength;
  29. size -= h->bLength;
  30. ++n;
  31. }
  32. /* Store the number of descriptors skipped and return the
  33. * number of bytes skipped */
  34. if (num_skipped)
  35. *num_skipped = n;
  36. return buffer - buffer0;
  37. }
  38. static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
  39. int asnum, struct usb_host_interface *ifp, int num_ep,
  40. unsigned char *buffer, int size)
  41. {
  42. unsigned char *buffer0 = buffer;
  43. struct usb_endpoint_descriptor *d;
  44. struct usb_host_endpoint *endpoint;
  45. int n, i, j;
  46. d = (struct usb_endpoint_descriptor *) buffer;
  47. buffer += d->bLength;
  48. size -= d->bLength;
  49. if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
  50. n = USB_DT_ENDPOINT_AUDIO_SIZE;
  51. else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
  52. n = USB_DT_ENDPOINT_SIZE;
  53. else {
  54. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  55. "invalid endpoint descriptor of length %d, skipping\n",
  56. cfgno, inum, asnum, d->bLength);
  57. goto skip_to_next_endpoint_or_interface_descriptor;
  58. }
  59. i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
  60. if (i >= 16 || i == 0) {
  61. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  62. "invalid endpoint with address 0x%X, skipping\n",
  63. cfgno, inum, asnum, d->bEndpointAddress);
  64. goto skip_to_next_endpoint_or_interface_descriptor;
  65. }
  66. /* Only store as many endpoints as we have room for */
  67. if (ifp->desc.bNumEndpoints >= num_ep)
  68. goto skip_to_next_endpoint_or_interface_descriptor;
  69. endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
  70. ++ifp->desc.bNumEndpoints;
  71. memcpy(&endpoint->desc, d, n);
  72. INIT_LIST_HEAD(&endpoint->urb_list);
  73. /* Fix up bInterval values outside the legal range. Use 32 ms if no
  74. * proper value can be guessed. */
  75. i = 0; /* i = min, j = max, n = default */
  76. j = 255;
  77. if (usb_endpoint_xfer_int(d)) {
  78. i = 1;
  79. switch (to_usb_device(ddev)->speed) {
  80. case USB_SPEED_HIGH:
  81. /* Many device manufacturers are using full-speed
  82. * bInterval values in high-speed interrupt endpoint
  83. * descriptors. Try to fix those and fall back to a
  84. * 32 ms default value otherwise. */
  85. n = fls(d->bInterval*8);
  86. if (n == 0)
  87. n = 9; /* 32 ms = 2^(9-1) uframes */
  88. j = 16;
  89. break;
  90. default: /* USB_SPEED_FULL or _LOW */
  91. /* For low-speed, 10 ms is the official minimum.
  92. * But some "overclocked" devices might want faster
  93. * polling so we'll allow it. */
  94. n = 32;
  95. break;
  96. }
  97. } else if (usb_endpoint_xfer_isoc(d)) {
  98. i = 1;
  99. j = 16;
  100. switch (to_usb_device(ddev)->speed) {
  101. case USB_SPEED_HIGH:
  102. n = 9; /* 32 ms = 2^(9-1) uframes */
  103. break;
  104. default: /* USB_SPEED_FULL */
  105. n = 6; /* 32 ms = 2^(6-1) frames */
  106. break;
  107. }
  108. }
  109. if (d->bInterval < i || d->bInterval > j) {
  110. dev_warn(ddev, "config %d interface %d altsetting %d "
  111. "endpoint 0x%X has an invalid bInterval %d, "
  112. "changing to %d\n",
  113. cfgno, inum, asnum,
  114. d->bEndpointAddress, d->bInterval, n);
  115. endpoint->desc.bInterval = n;
  116. }
  117. /* Some buggy low-speed devices have Bulk endpoints, which is
  118. * explicitly forbidden by the USB spec. In an attempt to make
  119. * them usable, we will try treating them as Interrupt endpoints.
  120. */
  121. if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
  122. usb_endpoint_xfer_bulk(d)) {
  123. dev_warn(ddev, "config %d interface %d altsetting %d "
  124. "endpoint 0x%X is Bulk; changing to Interrupt\n",
  125. cfgno, inum, asnum, d->bEndpointAddress);
  126. endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
  127. endpoint->desc.bInterval = 1;
  128. if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
  129. endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
  130. }
  131. /*
  132. * Some buggy high speed devices have bulk endpoints using
  133. * maxpacket sizes other than 512. High speed HCDs may not
  134. * be able to handle that particular bug, so let's warn...
  135. */
  136. if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
  137. && usb_endpoint_xfer_bulk(d)) {
  138. unsigned maxp;
  139. maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
  140. if (maxp != 512)
  141. dev_warn(ddev, "config %d interface %d altsetting %d "
  142. "bulk endpoint 0x%X has invalid maxpacket %d\n",
  143. cfgno, inum, asnum, d->bEndpointAddress,
  144. maxp);
  145. }
  146. /* Skip over any Class Specific or Vendor Specific descriptors;
  147. * find the next endpoint or interface descriptor */
  148. endpoint->extra = buffer;
  149. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  150. USB_DT_INTERFACE, &n);
  151. endpoint->extralen = i;
  152. if (n > 0)
  153. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  154. n, plural(n), "endpoint");
  155. return buffer - buffer0 + i;
  156. skip_to_next_endpoint_or_interface_descriptor:
  157. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  158. USB_DT_INTERFACE, NULL);
  159. return buffer - buffer0 + i;
  160. }
  161. void usb_release_interface_cache(struct kref *ref)
  162. {
  163. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  164. int j;
  165. for (j = 0; j < intfc->num_altsetting; j++) {
  166. struct usb_host_interface *alt = &intfc->altsetting[j];
  167. kfree(alt->endpoint);
  168. kfree(alt->string);
  169. }
  170. kfree(intfc);
  171. }
  172. static int usb_parse_interface(struct device *ddev, int cfgno,
  173. struct usb_host_config *config, unsigned char *buffer, int size,
  174. u8 inums[], u8 nalts[])
  175. {
  176. unsigned char *buffer0 = buffer;
  177. struct usb_interface_descriptor *d;
  178. int inum, asnum;
  179. struct usb_interface_cache *intfc;
  180. struct usb_host_interface *alt;
  181. int i, n;
  182. int len, retval;
  183. int num_ep, num_ep_orig;
  184. d = (struct usb_interface_descriptor *) buffer;
  185. buffer += d->bLength;
  186. size -= d->bLength;
  187. if (d->bLength < USB_DT_INTERFACE_SIZE)
  188. goto skip_to_next_interface_descriptor;
  189. /* Which interface entry is this? */
  190. intfc = NULL;
  191. inum = d->bInterfaceNumber;
  192. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  193. if (inums[i] == inum) {
  194. intfc = config->intf_cache[i];
  195. break;
  196. }
  197. }
  198. if (!intfc || intfc->num_altsetting >= nalts[i])
  199. goto skip_to_next_interface_descriptor;
  200. /* Check for duplicate altsetting entries */
  201. asnum = d->bAlternateSetting;
  202. for ((i = 0, alt = &intfc->altsetting[0]);
  203. i < intfc->num_altsetting;
  204. (++i, ++alt)) {
  205. if (alt->desc.bAlternateSetting == asnum) {
  206. dev_warn(ddev, "Duplicate descriptor for config %d "
  207. "interface %d altsetting %d, skipping\n",
  208. cfgno, inum, asnum);
  209. goto skip_to_next_interface_descriptor;
  210. }
  211. }
  212. ++intfc->num_altsetting;
  213. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  214. /* Skip over any Class Specific or Vendor Specific descriptors;
  215. * find the first endpoint or interface descriptor */
  216. alt->extra = buffer;
  217. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  218. USB_DT_INTERFACE, &n);
  219. alt->extralen = i;
  220. if (n > 0)
  221. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  222. n, plural(n), "interface");
  223. buffer += i;
  224. size -= i;
  225. /* Allocate space for the right(?) number of endpoints */
  226. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  227. alt->desc.bNumEndpoints = 0; /* Use as a counter */
  228. if (num_ep > USB_MAXENDPOINTS) {
  229. dev_warn(ddev, "too many endpoints for config %d interface %d "
  230. "altsetting %d: %d, using maximum allowed: %d\n",
  231. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  232. num_ep = USB_MAXENDPOINTS;
  233. }
  234. if (num_ep > 0) {
  235. /* Can't allocate 0 bytes */
  236. len = sizeof(struct usb_host_endpoint) * num_ep;
  237. alt->endpoint = kzalloc(len, GFP_KERNEL);
  238. if (!alt->endpoint)
  239. return -ENOMEM;
  240. }
  241. /* Parse all the endpoint descriptors */
  242. n = 0;
  243. while (size > 0) {
  244. if (((struct usb_descriptor_header *) buffer)->bDescriptorType
  245. == USB_DT_INTERFACE)
  246. break;
  247. retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
  248. num_ep, buffer, size);
  249. if (retval < 0)
  250. return retval;
  251. ++n;
  252. buffer += retval;
  253. size -= retval;
  254. }
  255. if (n != num_ep_orig)
  256. dev_warn(ddev, "config %d interface %d altsetting %d has %d "
  257. "endpoint descriptor%s, different from the interface "
  258. "descriptor's value: %d\n",
  259. cfgno, inum, asnum, n, plural(n), num_ep_orig);
  260. return buffer - buffer0;
  261. skip_to_next_interface_descriptor:
  262. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  263. USB_DT_INTERFACE, NULL);
  264. return buffer - buffer0 + i;
  265. }
  266. static int usb_parse_configuration(struct device *ddev, int cfgidx,
  267. struct usb_host_config *config, unsigned char *buffer, int size)
  268. {
  269. unsigned char *buffer0 = buffer;
  270. int cfgno;
  271. int nintf, nintf_orig;
  272. int i, j, n;
  273. struct usb_interface_cache *intfc;
  274. unsigned char *buffer2;
  275. int size2;
  276. struct usb_descriptor_header *header;
  277. int len, retval;
  278. u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
  279. unsigned iad_num = 0;
  280. memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
  281. if (config->desc.bDescriptorType != USB_DT_CONFIG ||
  282. config->desc.bLength < USB_DT_CONFIG_SIZE) {
  283. dev_err(ddev, "invalid descriptor for config index %d: "
  284. "type = 0x%X, length = %d\n", cfgidx,
  285. config->desc.bDescriptorType, config->desc.bLength);
  286. return -EINVAL;
  287. }
  288. cfgno = config->desc.bConfigurationValue;
  289. buffer += config->desc.bLength;
  290. size -= config->desc.bLength;
  291. nintf = nintf_orig = config->desc.bNumInterfaces;
  292. if (nintf > USB_MAXINTERFACES) {
  293. dev_warn(ddev, "config %d has too many interfaces: %d, "
  294. "using maximum allowed: %d\n",
  295. cfgno, nintf, USB_MAXINTERFACES);
  296. nintf = USB_MAXINTERFACES;
  297. }
  298. /* Go through the descriptors, checking their length and counting the
  299. * number of altsettings for each interface */
  300. n = 0;
  301. for ((buffer2 = buffer, size2 = size);
  302. size2 > 0;
  303. (buffer2 += header->bLength, size2 -= header->bLength)) {
  304. if (size2 < sizeof(struct usb_descriptor_header)) {
  305. dev_warn(ddev, "config %d descriptor has %d excess "
  306. "byte%s, ignoring\n",
  307. cfgno, size2, plural(size2));
  308. break;
  309. }
  310. header = (struct usb_descriptor_header *) buffer2;
  311. if ((header->bLength > size2) || (header->bLength < 2)) {
  312. dev_warn(ddev, "config %d has an invalid descriptor "
  313. "of length %d, skipping remainder of the config\n",
  314. cfgno, header->bLength);
  315. break;
  316. }
  317. if (header->bDescriptorType == USB_DT_INTERFACE) {
  318. struct usb_interface_descriptor *d;
  319. int inum;
  320. d = (struct usb_interface_descriptor *) header;
  321. if (d->bLength < USB_DT_INTERFACE_SIZE) {
  322. dev_warn(ddev, "config %d has an invalid "
  323. "interface descriptor of length %d, "
  324. "skipping\n", cfgno, d->bLength);
  325. continue;
  326. }
  327. inum = d->bInterfaceNumber;
  328. if (inum >= nintf_orig)
  329. dev_warn(ddev, "config %d has an invalid "
  330. "interface number: %d but max is %d\n",
  331. cfgno, inum, nintf_orig - 1);
  332. /* Have we already encountered this interface?
  333. * Count its altsettings */
  334. for (i = 0; i < n; ++i) {
  335. if (inums[i] == inum)
  336. break;
  337. }
  338. if (i < n) {
  339. if (nalts[i] < 255)
  340. ++nalts[i];
  341. } else if (n < USB_MAXINTERFACES) {
  342. inums[n] = inum;
  343. nalts[n] = 1;
  344. ++n;
  345. }
  346. } else if (header->bDescriptorType ==
  347. USB_DT_INTERFACE_ASSOCIATION) {
  348. if (iad_num == USB_MAXIADS) {
  349. dev_warn(ddev, "found more Interface "
  350. "Association Descriptors "
  351. "than allocated for in "
  352. "configuration %d\n", cfgno);
  353. } else {
  354. config->intf_assoc[iad_num] =
  355. (struct usb_interface_assoc_descriptor
  356. *)header;
  357. iad_num++;
  358. }
  359. } else if (header->bDescriptorType == USB_DT_DEVICE ||
  360. header->bDescriptorType == USB_DT_CONFIG)
  361. dev_warn(ddev, "config %d contains an unexpected "
  362. "descriptor of type 0x%X, skipping\n",
  363. cfgno, header->bDescriptorType);
  364. } /* for ((buffer2 = buffer, size2 = size); ...) */
  365. size = buffer2 - buffer;
  366. config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
  367. if (n != nintf)
  368. dev_warn(ddev, "config %d has %d interface%s, different from "
  369. "the descriptor's value: %d\n",
  370. cfgno, n, plural(n), nintf_orig);
  371. else if (n == 0)
  372. dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
  373. config->desc.bNumInterfaces = nintf = n;
  374. /* Check for missing interface numbers */
  375. for (i = 0; i < nintf; ++i) {
  376. for (j = 0; j < nintf; ++j) {
  377. if (inums[j] == i)
  378. break;
  379. }
  380. if (j >= nintf)
  381. dev_warn(ddev, "config %d has no interface number "
  382. "%d\n", cfgno, i);
  383. }
  384. /* Allocate the usb_interface_caches and altsetting arrays */
  385. for (i = 0; i < nintf; ++i) {
  386. j = nalts[i];
  387. if (j > USB_MAXALTSETTING) {
  388. dev_warn(ddev, "too many alternate settings for "
  389. "config %d interface %d: %d, "
  390. "using maximum allowed: %d\n",
  391. cfgno, inums[i], j, USB_MAXALTSETTING);
  392. nalts[i] = j = USB_MAXALTSETTING;
  393. }
  394. len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
  395. config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
  396. if (!intfc)
  397. return -ENOMEM;
  398. kref_init(&intfc->ref);
  399. }
  400. /* Skip over any Class Specific or Vendor Specific descriptors;
  401. * find the first interface descriptor */
  402. config->extra = buffer;
  403. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  404. USB_DT_INTERFACE, &n);
  405. config->extralen = i;
  406. if (n > 0)
  407. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  408. n, plural(n), "configuration");
  409. buffer += i;
  410. size -= i;
  411. /* Parse all the interface/altsetting descriptors */
  412. while (size > 0) {
  413. retval = usb_parse_interface(ddev, cfgno, config,
  414. buffer, size, inums, nalts);
  415. if (retval < 0)
  416. return retval;
  417. buffer += retval;
  418. size -= retval;
  419. }
  420. /* Check for missing altsettings */
  421. for (i = 0; i < nintf; ++i) {
  422. intfc = config->intf_cache[i];
  423. for (j = 0; j < intfc->num_altsetting; ++j) {
  424. for (n = 0; n < intfc->num_altsetting; ++n) {
  425. if (intfc->altsetting[n].desc.
  426. bAlternateSetting == j)
  427. break;
  428. }
  429. if (n >= intfc->num_altsetting)
  430. dev_warn(ddev, "config %d interface %d has no "
  431. "altsetting %d\n", cfgno, inums[i], j);
  432. }
  433. }
  434. return 0;
  435. }
  436. /* hub-only!! ... and only exported for reset/reinit path.
  437. * otherwise used internally on disconnect/destroy path
  438. */
  439. void usb_destroy_configuration(struct usb_device *dev)
  440. {
  441. int c, i;
  442. if (!dev->config)
  443. return;
  444. if (dev->rawdescriptors) {
  445. for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
  446. kfree(dev->rawdescriptors[i]);
  447. kfree(dev->rawdescriptors);
  448. dev->rawdescriptors = NULL;
  449. }
  450. for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
  451. struct usb_host_config *cf = &dev->config[c];
  452. kfree(cf->string);
  453. for (i = 0; i < cf->desc.bNumInterfaces; i++) {
  454. if (cf->intf_cache[i])
  455. kref_put(&cf->intf_cache[i]->ref,
  456. usb_release_interface_cache);
  457. }
  458. }
  459. kfree(dev->config);
  460. dev->config = NULL;
  461. }
  462. /*
  463. * Get the USB config descriptors, cache and parse'em
  464. *
  465. * hub-only!! ... and only in reset path, or usb_new_device()
  466. * (used by real hubs and virtual root hubs)
  467. *
  468. * NOTE: if this is a WUSB device and is not authorized, we skip the
  469. * whole thing. A non-authorized USB device has no
  470. * configurations.
  471. */
  472. int usb_get_configuration(struct usb_device *dev)
  473. {
  474. struct device *ddev = &dev->dev;
  475. int ncfg = dev->descriptor.bNumConfigurations;
  476. int result = 0;
  477. unsigned int cfgno, length;
  478. unsigned char *buffer;
  479. unsigned char *bigbuffer;
  480. struct usb_config_descriptor *desc;
  481. cfgno = 0;
  482. if (dev->authorized == 0) /* Not really an error */
  483. goto out_not_authorized;
  484. result = -ENOMEM;
  485. if (ncfg > USB_MAXCONFIG) {
  486. dev_warn(ddev, "too many configurations: %d, "
  487. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  488. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  489. }
  490. if (ncfg < 1) {
  491. dev_err(ddev, "no configurations\n");
  492. return -EINVAL;
  493. }
  494. length = ncfg * sizeof(struct usb_host_config);
  495. dev->config = kzalloc(length, GFP_KERNEL);
  496. if (!dev->config)
  497. goto err2;
  498. length = ncfg * sizeof(char *);
  499. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  500. if (!dev->rawdescriptors)
  501. goto err2;
  502. buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  503. if (!buffer)
  504. goto err2;
  505. desc = (struct usb_config_descriptor *)buffer;
  506. result = 0;
  507. for (; cfgno < ncfg; cfgno++) {
  508. /* We grab just the first descriptor so we know how long
  509. * the whole configuration is */
  510. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  511. buffer, USB_DT_CONFIG_SIZE);
  512. if (result < 0) {
  513. dev_err(ddev, "unable to read config index %d "
  514. "descriptor/%s: %d\n", cfgno, "start", result);
  515. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  516. dev->descriptor.bNumConfigurations = cfgno;
  517. break;
  518. } else if (result < 4) {
  519. dev_err(ddev, "config index %d descriptor too short "
  520. "(expected %i, got %i)\n", cfgno,
  521. USB_DT_CONFIG_SIZE, result);
  522. result = -EINVAL;
  523. goto err;
  524. }
  525. length = max((int) le16_to_cpu(desc->wTotalLength),
  526. USB_DT_CONFIG_SIZE);
  527. /* Now that we know the length, get the whole thing */
  528. bigbuffer = kmalloc(length, GFP_KERNEL);
  529. if (!bigbuffer) {
  530. result = -ENOMEM;
  531. goto err;
  532. }
  533. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  534. bigbuffer, length);
  535. if (result < 0) {
  536. dev_err(ddev, "unable to read config index %d "
  537. "descriptor/%s\n", cfgno, "all");
  538. kfree(bigbuffer);
  539. goto err;
  540. }
  541. if (result < length) {
  542. dev_warn(ddev, "config index %d descriptor too short "
  543. "(expected %i, got %i)\n", cfgno, length, result);
  544. length = result;
  545. }
  546. dev->rawdescriptors[cfgno] = bigbuffer;
  547. result = usb_parse_configuration(&dev->dev, cfgno,
  548. &dev->config[cfgno], bigbuffer, length);
  549. if (result < 0) {
  550. ++cfgno;
  551. goto err;
  552. }
  553. }
  554. result = 0;
  555. err:
  556. kfree(buffer);
  557. out_not_authorized:
  558. dev->descriptor.bNumConfigurations = cfgno;
  559. err2:
  560. if (result == -ENOMEM)
  561. dev_err(ddev, "out of memory\n");
  562. return result;
  563. }