config.c 28 KB

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