config.c 22 KB

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