config.c 28 KB

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