config.c 24 KB

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