config.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. /* Check for duplicate endpoint addresses */
  218. for (i = 0; i < ifp->desc.bNumEndpoints; ++i) {
  219. if (ifp->endpoint[i].desc.bEndpointAddress ==
  220. d->bEndpointAddress) {
  221. dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
  222. cfgno, inum, asnum, d->bEndpointAddress);
  223. goto skip_to_next_endpoint_or_interface_descriptor;
  224. }
  225. }
  226. endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
  227. ++ifp->desc.bNumEndpoints;
  228. memcpy(&endpoint->desc, d, n);
  229. INIT_LIST_HEAD(&endpoint->urb_list);
  230. /*
  231. * Fix up bInterval values outside the legal range.
  232. * Use 10 or 8 ms if no proper value can be guessed.
  233. */
  234. i = 0; /* i = min, j = max, n = default */
  235. j = 255;
  236. if (usb_endpoint_xfer_int(d)) {
  237. i = 1;
  238. switch (to_usb_device(ddev)->speed) {
  239. case USB_SPEED_SUPER_PLUS:
  240. case USB_SPEED_SUPER:
  241. case USB_SPEED_HIGH:
  242. /*
  243. * Many device manufacturers are using full-speed
  244. * bInterval values in high-speed interrupt endpoint
  245. * descriptors. Try to fix those and fall back to an
  246. * 8-ms default value otherwise.
  247. */
  248. n = fls(d->bInterval*8);
  249. if (n == 0)
  250. n = 7; /* 8 ms = 2^(7-1) uframes */
  251. j = 16;
  252. /*
  253. * Adjust bInterval for quirked devices.
  254. * This quirk fixes bIntervals reported in
  255. * linear microframes.
  256. */
  257. if (to_usb_device(ddev)->quirks &
  258. USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
  259. n = clamp(fls(d->bInterval), i, j);
  260. i = j = n;
  261. }
  262. break;
  263. default: /* USB_SPEED_FULL or _LOW */
  264. /*
  265. * For low-speed, 10 ms is the official minimum.
  266. * But some "overclocked" devices might want faster
  267. * polling so we'll allow it.
  268. */
  269. n = 10;
  270. break;
  271. }
  272. } else if (usb_endpoint_xfer_isoc(d)) {
  273. i = 1;
  274. j = 16;
  275. switch (to_usb_device(ddev)->speed) {
  276. case USB_SPEED_HIGH:
  277. n = 7; /* 8 ms = 2^(7-1) uframes */
  278. break;
  279. default: /* USB_SPEED_FULL */
  280. n = 4; /* 8 ms = 2^(4-1) frames */
  281. break;
  282. }
  283. }
  284. if (d->bInterval < i || d->bInterval > j) {
  285. dev_warn(ddev, "config %d interface %d altsetting %d "
  286. "endpoint 0x%X has an invalid bInterval %d, "
  287. "changing to %d\n",
  288. cfgno, inum, asnum,
  289. d->bEndpointAddress, d->bInterval, n);
  290. endpoint->desc.bInterval = n;
  291. }
  292. /* Some buggy low-speed devices have Bulk endpoints, which is
  293. * explicitly forbidden by the USB spec. In an attempt to make
  294. * them usable, we will try treating them as Interrupt endpoints.
  295. */
  296. if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
  297. usb_endpoint_xfer_bulk(d)) {
  298. dev_warn(ddev, "config %d interface %d altsetting %d "
  299. "endpoint 0x%X is Bulk; changing to Interrupt\n",
  300. cfgno, inum, asnum, d->bEndpointAddress);
  301. endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
  302. endpoint->desc.bInterval = 1;
  303. if (usb_endpoint_maxp(&endpoint->desc) > 8)
  304. endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
  305. }
  306. /* Validate the wMaxPacketSize field */
  307. maxp = usb_endpoint_maxp(&endpoint->desc);
  308. /* Find the highest legal maxpacket size for this endpoint */
  309. i = 0; /* additional transactions per microframe */
  310. switch (to_usb_device(ddev)->speed) {
  311. case USB_SPEED_LOW:
  312. maxpacket_maxes = low_speed_maxpacket_maxes;
  313. break;
  314. case USB_SPEED_FULL:
  315. maxpacket_maxes = full_speed_maxpacket_maxes;
  316. break;
  317. case USB_SPEED_HIGH:
  318. /* Bits 12..11 are allowed only for HS periodic endpoints */
  319. if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
  320. i = maxp & (BIT(12) | BIT(11));
  321. maxp &= ~i;
  322. }
  323. /* fallthrough */
  324. default:
  325. maxpacket_maxes = high_speed_maxpacket_maxes;
  326. break;
  327. case USB_SPEED_SUPER:
  328. case USB_SPEED_SUPER_PLUS:
  329. maxpacket_maxes = super_speed_maxpacket_maxes;
  330. break;
  331. }
  332. j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
  333. if (maxp > j) {
  334. dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
  335. cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
  336. maxp = j;
  337. endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
  338. }
  339. /*
  340. * Some buggy high speed devices have bulk endpoints using
  341. * maxpacket sizes other than 512. High speed HCDs may not
  342. * be able to handle that particular bug, so let's warn...
  343. */
  344. if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
  345. && usb_endpoint_xfer_bulk(d)) {
  346. if (maxp != 512)
  347. dev_warn(ddev, "config %d interface %d altsetting %d "
  348. "bulk endpoint 0x%X has invalid maxpacket %d\n",
  349. cfgno, inum, asnum, d->bEndpointAddress,
  350. maxp);
  351. }
  352. /* Parse a possible SuperSpeed endpoint companion descriptor */
  353. if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
  354. usb_parse_ss_endpoint_companion(ddev, cfgno,
  355. inum, asnum, endpoint, buffer, size);
  356. /* Skip over any Class Specific or Vendor Specific descriptors;
  357. * find the next endpoint or interface descriptor */
  358. endpoint->extra = buffer;
  359. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  360. USB_DT_INTERFACE, &n);
  361. endpoint->extralen = i;
  362. retval = buffer - buffer0 + i;
  363. if (n > 0)
  364. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  365. n, plural(n), "endpoint");
  366. return retval;
  367. skip_to_next_endpoint_or_interface_descriptor:
  368. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  369. USB_DT_INTERFACE, NULL);
  370. return buffer - buffer0 + i;
  371. }
  372. void usb_release_interface_cache(struct kref *ref)
  373. {
  374. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  375. int j;
  376. for (j = 0; j < intfc->num_altsetting; j++) {
  377. struct usb_host_interface *alt = &intfc->altsetting[j];
  378. kfree(alt->endpoint);
  379. kfree(alt->string);
  380. }
  381. kfree(intfc);
  382. }
  383. static int usb_parse_interface(struct device *ddev, int cfgno,
  384. struct usb_host_config *config, unsigned char *buffer, int size,
  385. u8 inums[], u8 nalts[])
  386. {
  387. unsigned char *buffer0 = buffer;
  388. struct usb_interface_descriptor *d;
  389. int inum, asnum;
  390. struct usb_interface_cache *intfc;
  391. struct usb_host_interface *alt;
  392. int i, n;
  393. int len, retval;
  394. int num_ep, num_ep_orig;
  395. d = (struct usb_interface_descriptor *) buffer;
  396. buffer += d->bLength;
  397. size -= d->bLength;
  398. if (d->bLength < USB_DT_INTERFACE_SIZE)
  399. goto skip_to_next_interface_descriptor;
  400. /* Which interface entry is this? */
  401. intfc = NULL;
  402. inum = d->bInterfaceNumber;
  403. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  404. if (inums[i] == inum) {
  405. intfc = config->intf_cache[i];
  406. break;
  407. }
  408. }
  409. if (!intfc || intfc->num_altsetting >= nalts[i])
  410. goto skip_to_next_interface_descriptor;
  411. /* Check for duplicate altsetting entries */
  412. asnum = d->bAlternateSetting;
  413. for ((i = 0, alt = &intfc->altsetting[0]);
  414. i < intfc->num_altsetting;
  415. (++i, ++alt)) {
  416. if (alt->desc.bAlternateSetting == asnum) {
  417. dev_warn(ddev, "Duplicate descriptor for config %d "
  418. "interface %d altsetting %d, skipping\n",
  419. cfgno, inum, asnum);
  420. goto skip_to_next_interface_descriptor;
  421. }
  422. }
  423. ++intfc->num_altsetting;
  424. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  425. /* Skip over any Class Specific or Vendor Specific descriptors;
  426. * find the first endpoint or interface descriptor */
  427. alt->extra = buffer;
  428. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  429. USB_DT_INTERFACE, &n);
  430. alt->extralen = i;
  431. if (n > 0)
  432. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  433. n, plural(n), "interface");
  434. buffer += i;
  435. size -= i;
  436. /* Allocate space for the right(?) number of endpoints */
  437. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  438. alt->desc.bNumEndpoints = 0; /* Use as a counter */
  439. if (num_ep > USB_MAXENDPOINTS) {
  440. dev_warn(ddev, "too many endpoints for config %d interface %d "
  441. "altsetting %d: %d, using maximum allowed: %d\n",
  442. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  443. num_ep = USB_MAXENDPOINTS;
  444. }
  445. if (num_ep > 0) {
  446. /* Can't allocate 0 bytes */
  447. len = sizeof(struct usb_host_endpoint) * num_ep;
  448. alt->endpoint = kzalloc(len, GFP_KERNEL);
  449. if (!alt->endpoint)
  450. return -ENOMEM;
  451. }
  452. /* Parse all the endpoint descriptors */
  453. n = 0;
  454. while (size > 0) {
  455. if (((struct usb_descriptor_header *) buffer)->bDescriptorType
  456. == USB_DT_INTERFACE)
  457. break;
  458. retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
  459. num_ep, buffer, size);
  460. if (retval < 0)
  461. return retval;
  462. ++n;
  463. buffer += retval;
  464. size -= retval;
  465. }
  466. if (n != num_ep_orig)
  467. dev_warn(ddev, "config %d interface %d altsetting %d has %d "
  468. "endpoint descriptor%s, different from the interface "
  469. "descriptor's value: %d\n",
  470. cfgno, inum, asnum, n, plural(n), num_ep_orig);
  471. return buffer - buffer0;
  472. skip_to_next_interface_descriptor:
  473. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  474. USB_DT_INTERFACE, NULL);
  475. return buffer - buffer0 + i;
  476. }
  477. static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
  478. struct usb_host_config *config, unsigned char *buffer, int size)
  479. {
  480. struct device *ddev = &dev->dev;
  481. unsigned char *buffer0 = buffer;
  482. int cfgno;
  483. int nintf, nintf_orig;
  484. int i, j, n;
  485. struct usb_interface_cache *intfc;
  486. unsigned char *buffer2;
  487. int size2;
  488. struct usb_descriptor_header *header;
  489. int len, retval;
  490. u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
  491. unsigned iad_num = 0;
  492. memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
  493. if (config->desc.bDescriptorType != USB_DT_CONFIG ||
  494. config->desc.bLength < USB_DT_CONFIG_SIZE ||
  495. config->desc.bLength > size) {
  496. dev_err(ddev, "invalid descriptor for config index %d: "
  497. "type = 0x%X, length = %d\n", cfgidx,
  498. config->desc.bDescriptorType, config->desc.bLength);
  499. return -EINVAL;
  500. }
  501. cfgno = config->desc.bConfigurationValue;
  502. buffer += config->desc.bLength;
  503. size -= config->desc.bLength;
  504. nintf = nintf_orig = config->desc.bNumInterfaces;
  505. if (nintf > USB_MAXINTERFACES) {
  506. dev_warn(ddev, "config %d has too many interfaces: %d, "
  507. "using maximum allowed: %d\n",
  508. cfgno, nintf, USB_MAXINTERFACES);
  509. nintf = USB_MAXINTERFACES;
  510. }
  511. /* Go through the descriptors, checking their length and counting the
  512. * number of altsettings for each interface */
  513. n = 0;
  514. for ((buffer2 = buffer, size2 = size);
  515. size2 > 0;
  516. (buffer2 += header->bLength, size2 -= header->bLength)) {
  517. if (size2 < sizeof(struct usb_descriptor_header)) {
  518. dev_warn(ddev, "config %d descriptor has %d excess "
  519. "byte%s, ignoring\n",
  520. cfgno, size2, plural(size2));
  521. break;
  522. }
  523. header = (struct usb_descriptor_header *) buffer2;
  524. if ((header->bLength > size2) || (header->bLength < 2)) {
  525. dev_warn(ddev, "config %d has an invalid descriptor "
  526. "of length %d, skipping remainder of the config\n",
  527. cfgno, header->bLength);
  528. break;
  529. }
  530. if (header->bDescriptorType == USB_DT_INTERFACE) {
  531. struct usb_interface_descriptor *d;
  532. int inum;
  533. d = (struct usb_interface_descriptor *) header;
  534. if (d->bLength < USB_DT_INTERFACE_SIZE) {
  535. dev_warn(ddev, "config %d has an invalid "
  536. "interface descriptor of length %d, "
  537. "skipping\n", cfgno, d->bLength);
  538. continue;
  539. }
  540. inum = d->bInterfaceNumber;
  541. if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
  542. n >= nintf_orig) {
  543. dev_warn(ddev, "config %d has more interface "
  544. "descriptors, than it declares in "
  545. "bNumInterfaces, ignoring interface "
  546. "number: %d\n", cfgno, inum);
  547. continue;
  548. }
  549. if (inum >= nintf_orig)
  550. dev_warn(ddev, "config %d has an invalid "
  551. "interface number: %d but max is %d\n",
  552. cfgno, inum, nintf_orig - 1);
  553. /* Have we already encountered this interface?
  554. * Count its altsettings */
  555. for (i = 0; i < n; ++i) {
  556. if (inums[i] == inum)
  557. break;
  558. }
  559. if (i < n) {
  560. if (nalts[i] < 255)
  561. ++nalts[i];
  562. } else if (n < USB_MAXINTERFACES) {
  563. inums[n] = inum;
  564. nalts[n] = 1;
  565. ++n;
  566. }
  567. } else if (header->bDescriptorType ==
  568. USB_DT_INTERFACE_ASSOCIATION) {
  569. if (iad_num == USB_MAXIADS) {
  570. dev_warn(ddev, "found more Interface "
  571. "Association Descriptors "
  572. "than allocated for in "
  573. "configuration %d\n", cfgno);
  574. } else {
  575. config->intf_assoc[iad_num] =
  576. (struct usb_interface_assoc_descriptor
  577. *)header;
  578. iad_num++;
  579. }
  580. } else if (header->bDescriptorType == USB_DT_DEVICE ||
  581. header->bDescriptorType == USB_DT_CONFIG)
  582. dev_warn(ddev, "config %d contains an unexpected "
  583. "descriptor of type 0x%X, skipping\n",
  584. cfgno, header->bDescriptorType);
  585. } /* for ((buffer2 = buffer, size2 = size); ...) */
  586. size = buffer2 - buffer;
  587. config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
  588. if (n != nintf)
  589. dev_warn(ddev, "config %d has %d interface%s, different from "
  590. "the descriptor's value: %d\n",
  591. cfgno, n, plural(n), nintf_orig);
  592. else if (n == 0)
  593. dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
  594. config->desc.bNumInterfaces = nintf = n;
  595. /* Check for missing interface numbers */
  596. for (i = 0; i < nintf; ++i) {
  597. for (j = 0; j < nintf; ++j) {
  598. if (inums[j] == i)
  599. break;
  600. }
  601. if (j >= nintf)
  602. dev_warn(ddev, "config %d has no interface number "
  603. "%d\n", cfgno, i);
  604. }
  605. /* Allocate the usb_interface_caches and altsetting arrays */
  606. for (i = 0; i < nintf; ++i) {
  607. j = nalts[i];
  608. if (j > USB_MAXALTSETTING) {
  609. dev_warn(ddev, "too many alternate settings for "
  610. "config %d interface %d: %d, "
  611. "using maximum allowed: %d\n",
  612. cfgno, inums[i], j, USB_MAXALTSETTING);
  613. nalts[i] = j = USB_MAXALTSETTING;
  614. }
  615. len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
  616. config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
  617. if (!intfc)
  618. return -ENOMEM;
  619. kref_init(&intfc->ref);
  620. }
  621. /* FIXME: parse the BOS descriptor */
  622. /* Skip over any Class Specific or Vendor Specific descriptors;
  623. * find the first interface descriptor */
  624. config->extra = buffer;
  625. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  626. USB_DT_INTERFACE, &n);
  627. config->extralen = i;
  628. if (n > 0)
  629. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  630. n, plural(n), "configuration");
  631. buffer += i;
  632. size -= i;
  633. /* Parse all the interface/altsetting descriptors */
  634. while (size > 0) {
  635. retval = usb_parse_interface(ddev, cfgno, config,
  636. buffer, size, inums, nalts);
  637. if (retval < 0)
  638. return retval;
  639. buffer += retval;
  640. size -= retval;
  641. }
  642. /* Check for missing altsettings */
  643. for (i = 0; i < nintf; ++i) {
  644. intfc = config->intf_cache[i];
  645. for (j = 0; j < intfc->num_altsetting; ++j) {
  646. for (n = 0; n < intfc->num_altsetting; ++n) {
  647. if (intfc->altsetting[n].desc.
  648. bAlternateSetting == j)
  649. break;
  650. }
  651. if (n >= intfc->num_altsetting)
  652. dev_warn(ddev, "config %d interface %d has no "
  653. "altsetting %d\n", cfgno, inums[i], j);
  654. }
  655. }
  656. return 0;
  657. }
  658. /* hub-only!! ... and only exported for reset/reinit path.
  659. * otherwise used internally on disconnect/destroy path
  660. */
  661. void usb_destroy_configuration(struct usb_device *dev)
  662. {
  663. int c, i;
  664. if (!dev->config)
  665. return;
  666. if (dev->rawdescriptors) {
  667. for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
  668. kfree(dev->rawdescriptors[i]);
  669. kfree(dev->rawdescriptors);
  670. dev->rawdescriptors = NULL;
  671. }
  672. for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
  673. struct usb_host_config *cf = &dev->config[c];
  674. kfree(cf->string);
  675. for (i = 0; i < cf->desc.bNumInterfaces; i++) {
  676. if (cf->intf_cache[i])
  677. kref_put(&cf->intf_cache[i]->ref,
  678. usb_release_interface_cache);
  679. }
  680. }
  681. kfree(dev->config);
  682. dev->config = NULL;
  683. }
  684. /*
  685. * Get the USB config descriptors, cache and parse'em
  686. *
  687. * hub-only!! ... and only in reset path, or usb_new_device()
  688. * (used by real hubs and virtual root hubs)
  689. */
  690. int usb_get_configuration(struct usb_device *dev)
  691. {
  692. struct device *ddev = &dev->dev;
  693. int ncfg = dev->descriptor.bNumConfigurations;
  694. int result = 0;
  695. unsigned int cfgno, length;
  696. unsigned char *bigbuffer;
  697. struct usb_config_descriptor *desc;
  698. cfgno = 0;
  699. result = -ENOMEM;
  700. if (ncfg > USB_MAXCONFIG) {
  701. dev_warn(ddev, "too many configurations: %d, "
  702. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  703. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  704. }
  705. if (ncfg < 1) {
  706. dev_err(ddev, "no configurations\n");
  707. return -EINVAL;
  708. }
  709. length = ncfg * sizeof(struct usb_host_config);
  710. dev->config = kzalloc(length, GFP_KERNEL);
  711. if (!dev->config)
  712. goto err2;
  713. length = ncfg * sizeof(char *);
  714. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  715. if (!dev->rawdescriptors)
  716. goto err2;
  717. desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  718. if (!desc)
  719. goto err2;
  720. result = 0;
  721. for (; cfgno < ncfg; cfgno++) {
  722. /* We grab just the first descriptor so we know how long
  723. * the whole configuration is */
  724. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  725. desc, USB_DT_CONFIG_SIZE);
  726. if (result < 0) {
  727. dev_err(ddev, "unable to read config index %d "
  728. "descriptor/%s: %d\n", cfgno, "start", result);
  729. if (result != -EPIPE)
  730. goto err;
  731. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  732. dev->descriptor.bNumConfigurations = cfgno;
  733. break;
  734. } else if (result < 4) {
  735. dev_err(ddev, "config index %d descriptor too short "
  736. "(expected %i, got %i)\n", cfgno,
  737. USB_DT_CONFIG_SIZE, result);
  738. result = -EINVAL;
  739. goto err;
  740. }
  741. length = max((int) le16_to_cpu(desc->wTotalLength),
  742. USB_DT_CONFIG_SIZE);
  743. /* Now that we know the length, get the whole thing */
  744. bigbuffer = kmalloc(length, GFP_KERNEL);
  745. if (!bigbuffer) {
  746. result = -ENOMEM;
  747. goto err;
  748. }
  749. if (dev->quirks & USB_QUIRK_DELAY_INIT)
  750. msleep(100);
  751. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  752. bigbuffer, length);
  753. if (result < 0) {
  754. dev_err(ddev, "unable to read config index %d "
  755. "descriptor/%s\n", cfgno, "all");
  756. kfree(bigbuffer);
  757. goto err;
  758. }
  759. if (result < length) {
  760. dev_warn(ddev, "config index %d descriptor too short "
  761. "(expected %i, got %i)\n", cfgno, length, result);
  762. length = result;
  763. }
  764. dev->rawdescriptors[cfgno] = bigbuffer;
  765. result = usb_parse_configuration(dev, cfgno,
  766. &dev->config[cfgno], bigbuffer, length);
  767. if (result < 0) {
  768. ++cfgno;
  769. goto err;
  770. }
  771. }
  772. result = 0;
  773. err:
  774. kfree(desc);
  775. dev->descriptor.bNumConfigurations = cfgno;
  776. err2:
  777. if (result == -ENOMEM)
  778. dev_err(ddev, "out of memory\n");
  779. return result;
  780. }
  781. void usb_release_bos_descriptor(struct usb_device *dev)
  782. {
  783. if (dev->bos) {
  784. kfree(dev->bos->desc);
  785. kfree(dev->bos);
  786. dev->bos = NULL;
  787. }
  788. }
  789. /* Get BOS descriptor set */
  790. int usb_get_bos_descriptor(struct usb_device *dev)
  791. {
  792. struct device *ddev = &dev->dev;
  793. struct usb_bos_descriptor *bos;
  794. struct usb_dev_cap_header *cap;
  795. unsigned char *buffer;
  796. int length, total_len, num, i;
  797. int ret;
  798. bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
  799. if (!bos)
  800. return -ENOMEM;
  801. /* Get BOS descriptor */
  802. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
  803. if (ret < USB_DT_BOS_SIZE) {
  804. dev_err(ddev, "unable to get BOS descriptor\n");
  805. if (ret >= 0)
  806. ret = -ENOMSG;
  807. kfree(bos);
  808. return ret;
  809. }
  810. length = bos->bLength;
  811. total_len = le16_to_cpu(bos->wTotalLength);
  812. num = bos->bNumDeviceCaps;
  813. kfree(bos);
  814. if (total_len < length)
  815. return -EINVAL;
  816. dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
  817. if (!dev->bos)
  818. return -ENOMEM;
  819. /* Now let's get the whole BOS descriptor set */
  820. buffer = kzalloc(total_len, GFP_KERNEL);
  821. if (!buffer) {
  822. ret = -ENOMEM;
  823. goto err;
  824. }
  825. dev->bos->desc = (struct usb_bos_descriptor *)buffer;
  826. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
  827. if (ret < total_len) {
  828. dev_err(ddev, "unable to get BOS descriptor set\n");
  829. if (ret >= 0)
  830. ret = -ENOMSG;
  831. goto err;
  832. }
  833. total_len -= length;
  834. for (i = 0; i < num; i++) {
  835. buffer += length;
  836. cap = (struct usb_dev_cap_header *)buffer;
  837. length = cap->bLength;
  838. if (total_len < length)
  839. break;
  840. total_len -= length;
  841. if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
  842. dev_warn(ddev, "descriptor type invalid, skip\n");
  843. continue;
  844. }
  845. switch (cap->bDevCapabilityType) {
  846. case USB_CAP_TYPE_WIRELESS_USB:
  847. /* Wireless USB cap descriptor is handled by wusb */
  848. break;
  849. case USB_CAP_TYPE_EXT:
  850. dev->bos->ext_cap =
  851. (struct usb_ext_cap_descriptor *)buffer;
  852. break;
  853. case USB_SS_CAP_TYPE:
  854. dev->bos->ss_cap =
  855. (struct usb_ss_cap_descriptor *)buffer;
  856. break;
  857. case USB_SSP_CAP_TYPE:
  858. dev->bos->ssp_cap =
  859. (struct usb_ssp_cap_descriptor *)buffer;
  860. break;
  861. case CONTAINER_ID_TYPE:
  862. dev->bos->ss_id =
  863. (struct usb_ss_container_id_descriptor *)buffer;
  864. break;
  865. case USB_PTM_CAP_TYPE:
  866. dev->bos->ptm_cap =
  867. (struct usb_ptm_cap_descriptor *)buffer;
  868. default:
  869. break;
  870. }
  871. }
  872. return 0;
  873. err:
  874. usb_release_bos_descriptor(dev);
  875. return ret;
  876. }