config.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. /* Skip over any Class Specific or Vendor Specific descriptors;
  132. * find the next endpoint or interface descriptor */
  133. endpoint->extra = buffer;
  134. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  135. USB_DT_INTERFACE, &n);
  136. endpoint->extralen = i;
  137. if (n > 0)
  138. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  139. n, plural(n), "endpoint");
  140. return buffer - buffer0 + i;
  141. skip_to_next_endpoint_or_interface_descriptor:
  142. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  143. USB_DT_INTERFACE, NULL);
  144. return buffer - buffer0 + i;
  145. }
  146. void usb_release_interface_cache(struct kref *ref)
  147. {
  148. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  149. int j;
  150. for (j = 0; j < intfc->num_altsetting; j++) {
  151. struct usb_host_interface *alt = &intfc->altsetting[j];
  152. kfree(alt->endpoint);
  153. kfree(alt->string);
  154. }
  155. kfree(intfc);
  156. }
  157. static int usb_parse_interface(struct device *ddev, int cfgno,
  158. struct usb_host_config *config, unsigned char *buffer, int size,
  159. u8 inums[], u8 nalts[])
  160. {
  161. unsigned char *buffer0 = buffer;
  162. struct usb_interface_descriptor *d;
  163. int inum, asnum;
  164. struct usb_interface_cache *intfc;
  165. struct usb_host_interface *alt;
  166. int i, n;
  167. int len, retval;
  168. int num_ep, num_ep_orig;
  169. d = (struct usb_interface_descriptor *) buffer;
  170. buffer += d->bLength;
  171. size -= d->bLength;
  172. if (d->bLength < USB_DT_INTERFACE_SIZE)
  173. goto skip_to_next_interface_descriptor;
  174. /* Which interface entry is this? */
  175. intfc = NULL;
  176. inum = d->bInterfaceNumber;
  177. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  178. if (inums[i] == inum) {
  179. intfc = config->intf_cache[i];
  180. break;
  181. }
  182. }
  183. if (!intfc || intfc->num_altsetting >= nalts[i])
  184. goto skip_to_next_interface_descriptor;
  185. /* Check for duplicate altsetting entries */
  186. asnum = d->bAlternateSetting;
  187. for ((i = 0, alt = &intfc->altsetting[0]);
  188. i < intfc->num_altsetting;
  189. (++i, ++alt)) {
  190. if (alt->desc.bAlternateSetting == asnum) {
  191. dev_warn(ddev, "Duplicate descriptor for config %d "
  192. "interface %d altsetting %d, skipping\n",
  193. cfgno, inum, asnum);
  194. goto skip_to_next_interface_descriptor;
  195. }
  196. }
  197. ++intfc->num_altsetting;
  198. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  199. /* Skip over any Class Specific or Vendor Specific descriptors;
  200. * find the first endpoint or interface descriptor */
  201. alt->extra = buffer;
  202. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  203. USB_DT_INTERFACE, &n);
  204. alt->extralen = i;
  205. if (n > 0)
  206. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  207. n, plural(n), "interface");
  208. buffer += i;
  209. size -= i;
  210. /* Allocate space for the right(?) number of endpoints */
  211. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  212. alt->desc.bNumEndpoints = 0; // Use as a counter
  213. if (num_ep > USB_MAXENDPOINTS) {
  214. dev_warn(ddev, "too many endpoints for config %d interface %d "
  215. "altsetting %d: %d, using maximum allowed: %d\n",
  216. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  217. num_ep = USB_MAXENDPOINTS;
  218. }
  219. if (num_ep > 0) { /* Can't allocate 0 bytes */
  220. len = sizeof(struct usb_host_endpoint) * num_ep;
  221. alt->endpoint = kzalloc(len, GFP_KERNEL);
  222. if (!alt->endpoint)
  223. return -ENOMEM;
  224. }
  225. /* Parse all the endpoint descriptors */
  226. n = 0;
  227. while (size > 0) {
  228. if (((struct usb_descriptor_header *) buffer)->bDescriptorType
  229. == USB_DT_INTERFACE)
  230. break;
  231. retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
  232. num_ep, buffer, size);
  233. if (retval < 0)
  234. return retval;
  235. ++n;
  236. buffer += retval;
  237. size -= retval;
  238. }
  239. if (n != num_ep_orig)
  240. dev_warn(ddev, "config %d interface %d altsetting %d has %d "
  241. "endpoint descriptor%s, different from the interface "
  242. "descriptor's value: %d\n",
  243. cfgno, inum, asnum, n, plural(n), num_ep_orig);
  244. return buffer - buffer0;
  245. skip_to_next_interface_descriptor:
  246. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  247. USB_DT_INTERFACE, NULL);
  248. return buffer - buffer0 + i;
  249. }
  250. static int usb_parse_configuration(struct device *ddev, int cfgidx,
  251. struct usb_host_config *config, unsigned char *buffer, int size)
  252. {
  253. unsigned char *buffer0 = buffer;
  254. int cfgno;
  255. int nintf, nintf_orig;
  256. int i, j, n;
  257. struct usb_interface_cache *intfc;
  258. unsigned char *buffer2;
  259. int size2;
  260. struct usb_descriptor_header *header;
  261. int len, retval;
  262. u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
  263. unsigned iad_num = 0;
  264. memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
  265. if (config->desc.bDescriptorType != USB_DT_CONFIG ||
  266. config->desc.bLength < USB_DT_CONFIG_SIZE) {
  267. dev_err(ddev, "invalid descriptor for config index %d: "
  268. "type = 0x%X, length = %d\n", cfgidx,
  269. config->desc.bDescriptorType, config->desc.bLength);
  270. return -EINVAL;
  271. }
  272. cfgno = config->desc.bConfigurationValue;
  273. buffer += config->desc.bLength;
  274. size -= config->desc.bLength;
  275. nintf = nintf_orig = config->desc.bNumInterfaces;
  276. if (nintf > USB_MAXINTERFACES) {
  277. dev_warn(ddev, "config %d has too many interfaces: %d, "
  278. "using maximum allowed: %d\n",
  279. cfgno, nintf, USB_MAXINTERFACES);
  280. nintf = USB_MAXINTERFACES;
  281. }
  282. /* Go through the descriptors, checking their length and counting the
  283. * number of altsettings for each interface */
  284. n = 0;
  285. for ((buffer2 = buffer, size2 = size);
  286. size2 > 0;
  287. (buffer2 += header->bLength, size2 -= header->bLength)) {
  288. if (size2 < sizeof(struct usb_descriptor_header)) {
  289. dev_warn(ddev, "config %d descriptor has %d excess "
  290. "byte%s, ignoring\n",
  291. cfgno, size2, plural(size2));
  292. break;
  293. }
  294. header = (struct usb_descriptor_header *) buffer2;
  295. if ((header->bLength > size2) || (header->bLength < 2)) {
  296. dev_warn(ddev, "config %d has an invalid descriptor "
  297. "of length %d, skipping remainder of the config\n",
  298. cfgno, header->bLength);
  299. break;
  300. }
  301. if (header->bDescriptorType == USB_DT_INTERFACE) {
  302. struct usb_interface_descriptor *d;
  303. int inum;
  304. d = (struct usb_interface_descriptor *) header;
  305. if (d->bLength < USB_DT_INTERFACE_SIZE) {
  306. dev_warn(ddev, "config %d has an invalid "
  307. "interface descriptor of length %d, "
  308. "skipping\n", cfgno, d->bLength);
  309. continue;
  310. }
  311. inum = d->bInterfaceNumber;
  312. if (inum >= nintf_orig)
  313. dev_warn(ddev, "config %d has an invalid "
  314. "interface number: %d but max is %d\n",
  315. cfgno, inum, nintf_orig - 1);
  316. /* Have we already encountered this interface?
  317. * Count its altsettings */
  318. for (i = 0; i < n; ++i) {
  319. if (inums[i] == inum)
  320. break;
  321. }
  322. if (i < n) {
  323. if (nalts[i] < 255)
  324. ++nalts[i];
  325. } else if (n < USB_MAXINTERFACES) {
  326. inums[n] = inum;
  327. nalts[n] = 1;
  328. ++n;
  329. }
  330. } else if (header->bDescriptorType ==
  331. USB_DT_INTERFACE_ASSOCIATION) {
  332. if (iad_num == USB_MAXIADS) {
  333. dev_warn(ddev, "found more Interface "
  334. "Association Descriptors "
  335. "than allocated for in "
  336. "configuration %d\n", cfgno);
  337. } else {
  338. config->intf_assoc[iad_num] =
  339. (struct usb_interface_assoc_descriptor
  340. *)header;
  341. iad_num++;
  342. }
  343. } else if (header->bDescriptorType == USB_DT_DEVICE ||
  344. header->bDescriptorType == USB_DT_CONFIG)
  345. dev_warn(ddev, "config %d contains an unexpected "
  346. "descriptor of type 0x%X, skipping\n",
  347. cfgno, header->bDescriptorType);
  348. } /* for ((buffer2 = buffer, size2 = size); ...) */
  349. size = buffer2 - buffer;
  350. config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
  351. if (n != nintf)
  352. dev_warn(ddev, "config %d has %d interface%s, different from "
  353. "the descriptor's value: %d\n",
  354. cfgno, n, plural(n), nintf_orig);
  355. else if (n == 0)
  356. dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
  357. config->desc.bNumInterfaces = nintf = n;
  358. /* Check for missing interface numbers */
  359. for (i = 0; i < nintf; ++i) {
  360. for (j = 0; j < nintf; ++j) {
  361. if (inums[j] == i)
  362. break;
  363. }
  364. if (j >= nintf)
  365. dev_warn(ddev, "config %d has no interface number "
  366. "%d\n", cfgno, i);
  367. }
  368. /* Allocate the usb_interface_caches and altsetting arrays */
  369. for (i = 0; i < nintf; ++i) {
  370. j = nalts[i];
  371. if (j > USB_MAXALTSETTING) {
  372. dev_warn(ddev, "too many alternate settings for "
  373. "config %d interface %d: %d, "
  374. "using maximum allowed: %d\n",
  375. cfgno, inums[i], j, USB_MAXALTSETTING);
  376. nalts[i] = j = USB_MAXALTSETTING;
  377. }
  378. len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
  379. config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
  380. if (!intfc)
  381. return -ENOMEM;
  382. kref_init(&intfc->ref);
  383. }
  384. /* Skip over any Class Specific or Vendor Specific descriptors;
  385. * find the first interface descriptor */
  386. config->extra = buffer;
  387. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  388. USB_DT_INTERFACE, &n);
  389. config->extralen = i;
  390. if (n > 0)
  391. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  392. n, plural(n), "configuration");
  393. buffer += i;
  394. size -= i;
  395. /* Parse all the interface/altsetting descriptors */
  396. while (size > 0) {
  397. retval = usb_parse_interface(ddev, cfgno, config,
  398. buffer, size, inums, nalts);
  399. if (retval < 0)
  400. return retval;
  401. buffer += retval;
  402. size -= retval;
  403. }
  404. /* Check for missing altsettings */
  405. for (i = 0; i < nintf; ++i) {
  406. intfc = config->intf_cache[i];
  407. for (j = 0; j < intfc->num_altsetting; ++j) {
  408. for (n = 0; n < intfc->num_altsetting; ++n) {
  409. if (intfc->altsetting[n].desc.
  410. bAlternateSetting == j)
  411. break;
  412. }
  413. if (n >= intfc->num_altsetting)
  414. dev_warn(ddev, "config %d interface %d has no "
  415. "altsetting %d\n", cfgno, inums[i], j);
  416. }
  417. }
  418. return 0;
  419. }
  420. // hub-only!! ... and only exported for reset/reinit path.
  421. // otherwise used internally on disconnect/destroy path
  422. void usb_destroy_configuration(struct usb_device *dev)
  423. {
  424. int c, i;
  425. if (!dev->config)
  426. return;
  427. if (dev->rawdescriptors) {
  428. for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
  429. kfree(dev->rawdescriptors[i]);
  430. kfree(dev->rawdescriptors);
  431. dev->rawdescriptors = NULL;
  432. }
  433. for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
  434. struct usb_host_config *cf = &dev->config[c];
  435. kfree(cf->string);
  436. for (i = 0; i < cf->desc.bNumInterfaces; i++) {
  437. if (cf->intf_cache[i])
  438. kref_put(&cf->intf_cache[i]->ref,
  439. usb_release_interface_cache);
  440. }
  441. }
  442. kfree(dev->config);
  443. dev->config = NULL;
  444. }
  445. /*
  446. * Get the USB config descriptors, cache and parse'em
  447. *
  448. * hub-only!! ... and only in reset path, or usb_new_device()
  449. * (used by real hubs and virtual root hubs)
  450. *
  451. * NOTE: if this is a WUSB device and is not authorized, we skip the
  452. * whole thing. A non-authorized USB device has no
  453. * configurations.
  454. */
  455. int usb_get_configuration(struct usb_device *dev)
  456. {
  457. struct device *ddev = &dev->dev;
  458. int ncfg = dev->descriptor.bNumConfigurations;
  459. int result = 0;
  460. unsigned int cfgno, length;
  461. unsigned char *buffer;
  462. unsigned char *bigbuffer;
  463. struct usb_config_descriptor *desc;
  464. cfgno = 0;
  465. if (dev->authorized == 0) /* Not really an error */
  466. goto out_not_authorized;
  467. result = -ENOMEM;
  468. if (ncfg > USB_MAXCONFIG) {
  469. dev_warn(ddev, "too many configurations: %d, "
  470. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  471. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  472. }
  473. if (ncfg < 1) {
  474. dev_err(ddev, "no configurations\n");
  475. return -EINVAL;
  476. }
  477. length = ncfg * sizeof(struct usb_host_config);
  478. dev->config = kzalloc(length, GFP_KERNEL);
  479. if (!dev->config)
  480. goto err2;
  481. length = ncfg * sizeof(char *);
  482. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  483. if (!dev->rawdescriptors)
  484. goto err2;
  485. buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  486. if (!buffer)
  487. goto err2;
  488. desc = (struct usb_config_descriptor *)buffer;
  489. result = 0;
  490. for (; cfgno < ncfg; cfgno++) {
  491. /* We grab just the first descriptor so we know how long
  492. * the whole configuration is */
  493. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  494. buffer, USB_DT_CONFIG_SIZE);
  495. if (result < 0) {
  496. dev_err(ddev, "unable to read config index %d "
  497. "descriptor/%s: %d\n", cfgno, "start", result);
  498. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  499. dev->descriptor.bNumConfigurations = cfgno;
  500. break;
  501. } else if (result < 4) {
  502. dev_err(ddev, "config index %d descriptor too short "
  503. "(expected %i, got %i)\n", cfgno,
  504. USB_DT_CONFIG_SIZE, result);
  505. result = -EINVAL;
  506. goto err;
  507. }
  508. length = max((int) le16_to_cpu(desc->wTotalLength),
  509. USB_DT_CONFIG_SIZE);
  510. /* Now that we know the length, get the whole thing */
  511. bigbuffer = kmalloc(length, GFP_KERNEL);
  512. if (!bigbuffer) {
  513. result = -ENOMEM;
  514. goto err;
  515. }
  516. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  517. bigbuffer, length);
  518. if (result < 0) {
  519. dev_err(ddev, "unable to read config index %d "
  520. "descriptor/%s\n", cfgno, "all");
  521. kfree(bigbuffer);
  522. goto err;
  523. }
  524. if (result < length) {
  525. dev_warn(ddev, "config index %d descriptor too short "
  526. "(expected %i, got %i)\n", cfgno, length, result);
  527. length = result;
  528. }
  529. dev->rawdescriptors[cfgno] = bigbuffer;
  530. result = usb_parse_configuration(&dev->dev, cfgno,
  531. &dev->config[cfgno], bigbuffer, length);
  532. if (result < 0) {
  533. ++cfgno;
  534. goto err;
  535. }
  536. }
  537. result = 0;
  538. err:
  539. kfree(buffer);
  540. out_not_authorized:
  541. dev->descriptor.bNumConfigurations = cfgno;
  542. err2:
  543. if (result == -ENOMEM)
  544. dev_err(ddev, "out of memory\n");
  545. return result;
  546. }