config.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Released under the GPLv2 only.
  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. struct usb_interface_assoc_descriptor *d;
  580. d = (struct usb_interface_assoc_descriptor *)header;
  581. if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
  582. dev_warn(ddev,
  583. "config %d has an invalid interface association descriptor of length %d, skipping\n",
  584. cfgno, d->bLength);
  585. continue;
  586. }
  587. if (iad_num == USB_MAXIADS) {
  588. dev_warn(ddev, "found more Interface "
  589. "Association Descriptors "
  590. "than allocated for in "
  591. "configuration %d\n", cfgno);
  592. } else {
  593. config->intf_assoc[iad_num] = d;
  594. iad_num++;
  595. }
  596. } else if (header->bDescriptorType == USB_DT_DEVICE ||
  597. header->bDescriptorType == USB_DT_CONFIG)
  598. dev_warn(ddev, "config %d contains an unexpected "
  599. "descriptor of type 0x%X, skipping\n",
  600. cfgno, header->bDescriptorType);
  601. } /* for ((buffer2 = buffer, size2 = size); ...) */
  602. size = buffer2 - buffer;
  603. config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
  604. if (n != nintf)
  605. dev_warn(ddev, "config %d has %d interface%s, different from "
  606. "the descriptor's value: %d\n",
  607. cfgno, n, plural(n), nintf_orig);
  608. else if (n == 0)
  609. dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
  610. config->desc.bNumInterfaces = nintf = n;
  611. /* Check for missing interface numbers */
  612. for (i = 0; i < nintf; ++i) {
  613. for (j = 0; j < nintf; ++j) {
  614. if (inums[j] == i)
  615. break;
  616. }
  617. if (j >= nintf)
  618. dev_warn(ddev, "config %d has no interface number "
  619. "%d\n", cfgno, i);
  620. }
  621. /* Allocate the usb_interface_caches and altsetting arrays */
  622. for (i = 0; i < nintf; ++i) {
  623. j = nalts[i];
  624. if (j > USB_MAXALTSETTING) {
  625. dev_warn(ddev, "too many alternate settings for "
  626. "config %d interface %d: %d, "
  627. "using maximum allowed: %d\n",
  628. cfgno, inums[i], j, USB_MAXALTSETTING);
  629. nalts[i] = j = USB_MAXALTSETTING;
  630. }
  631. len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
  632. config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
  633. if (!intfc)
  634. return -ENOMEM;
  635. kref_init(&intfc->ref);
  636. }
  637. /* FIXME: parse the BOS descriptor */
  638. /* Skip over any Class Specific or Vendor Specific descriptors;
  639. * find the first interface descriptor */
  640. config->extra = buffer;
  641. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  642. USB_DT_INTERFACE, &n);
  643. config->extralen = i;
  644. if (n > 0)
  645. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  646. n, plural(n), "configuration");
  647. buffer += i;
  648. size -= i;
  649. /* Parse all the interface/altsetting descriptors */
  650. while (size > 0) {
  651. retval = usb_parse_interface(ddev, cfgno, config,
  652. buffer, size, inums, nalts);
  653. if (retval < 0)
  654. return retval;
  655. buffer += retval;
  656. size -= retval;
  657. }
  658. /* Check for missing altsettings */
  659. for (i = 0; i < nintf; ++i) {
  660. intfc = config->intf_cache[i];
  661. for (j = 0; j < intfc->num_altsetting; ++j) {
  662. for (n = 0; n < intfc->num_altsetting; ++n) {
  663. if (intfc->altsetting[n].desc.
  664. bAlternateSetting == j)
  665. break;
  666. }
  667. if (n >= intfc->num_altsetting)
  668. dev_warn(ddev, "config %d interface %d has no "
  669. "altsetting %d\n", cfgno, inums[i], j);
  670. }
  671. }
  672. return 0;
  673. }
  674. /* hub-only!! ... and only exported for reset/reinit path.
  675. * otherwise used internally on disconnect/destroy path
  676. */
  677. void usb_destroy_configuration(struct usb_device *dev)
  678. {
  679. int c, i;
  680. if (!dev->config)
  681. return;
  682. if (dev->rawdescriptors) {
  683. for (i = 0; i < dev->descriptor.bNumConfigurations &&
  684. i < USB_MAXCONFIG; i++)
  685. kfree(dev->rawdescriptors[i]);
  686. kfree(dev->rawdescriptors);
  687. dev->rawdescriptors = NULL;
  688. }
  689. for (c = 0; c < dev->descriptor.bNumConfigurations &&
  690. c < USB_MAXCONFIG; c++) {
  691. struct usb_host_config *cf = &dev->config[c];
  692. kfree(cf->string);
  693. for (i = 0; i < cf->desc.bNumInterfaces &&
  694. i < USB_MAXINTERFACES; i++) {
  695. if (cf->intf_cache[i])
  696. kref_put(&cf->intf_cache[i]->ref,
  697. usb_release_interface_cache);
  698. }
  699. }
  700. kfree(dev->config);
  701. dev->config = NULL;
  702. }
  703. /*
  704. * Get the USB config descriptors, cache and parse'em
  705. *
  706. * hub-only!! ... and only in reset path, or usb_new_device()
  707. * (used by real hubs and virtual root hubs)
  708. */
  709. int usb_get_configuration(struct usb_device *dev)
  710. {
  711. struct device *ddev = &dev->dev;
  712. int ncfg = dev->descriptor.bNumConfigurations;
  713. int result = 0;
  714. unsigned int cfgno, length;
  715. unsigned char *bigbuffer;
  716. struct usb_config_descriptor *desc;
  717. cfgno = 0;
  718. result = -ENOMEM;
  719. if (ncfg > USB_MAXCONFIG) {
  720. dev_warn(ddev, "too many configurations: %d, "
  721. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  722. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  723. }
  724. if (ncfg < 1) {
  725. dev_err(ddev, "no configurations\n");
  726. return -EINVAL;
  727. }
  728. length = ncfg * sizeof(struct usb_host_config);
  729. dev->config = kzalloc(length, GFP_KERNEL);
  730. if (!dev->config)
  731. goto err2;
  732. length = ncfg * sizeof(char *);
  733. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  734. if (!dev->rawdescriptors)
  735. goto err2;
  736. desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  737. if (!desc)
  738. goto err2;
  739. result = 0;
  740. for (; cfgno < ncfg; cfgno++) {
  741. /* We grab just the first descriptor so we know how long
  742. * the whole configuration is */
  743. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  744. desc, USB_DT_CONFIG_SIZE);
  745. if (result < 0) {
  746. dev_err(ddev, "unable to read config index %d "
  747. "descriptor/%s: %d\n", cfgno, "start", result);
  748. if (result != -EPIPE)
  749. goto err;
  750. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  751. dev->descriptor.bNumConfigurations = cfgno;
  752. break;
  753. } else if (result < 4) {
  754. dev_err(ddev, "config index %d descriptor too short "
  755. "(expected %i, got %i)\n", cfgno,
  756. USB_DT_CONFIG_SIZE, result);
  757. result = -EINVAL;
  758. goto err;
  759. }
  760. length = max((int) le16_to_cpu(desc->wTotalLength),
  761. USB_DT_CONFIG_SIZE);
  762. /* Now that we know the length, get the whole thing */
  763. bigbuffer = kmalloc(length, GFP_KERNEL);
  764. if (!bigbuffer) {
  765. result = -ENOMEM;
  766. goto err;
  767. }
  768. if (dev->quirks & USB_QUIRK_DELAY_INIT)
  769. msleep(200);
  770. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  771. bigbuffer, length);
  772. if (result < 0) {
  773. dev_err(ddev, "unable to read config index %d "
  774. "descriptor/%s\n", cfgno, "all");
  775. kfree(bigbuffer);
  776. goto err;
  777. }
  778. if (result < length) {
  779. dev_warn(ddev, "config index %d descriptor too short "
  780. "(expected %i, got %i)\n", cfgno, length, result);
  781. length = result;
  782. }
  783. dev->rawdescriptors[cfgno] = bigbuffer;
  784. result = usb_parse_configuration(dev, cfgno,
  785. &dev->config[cfgno], bigbuffer, length);
  786. if (result < 0) {
  787. ++cfgno;
  788. goto err;
  789. }
  790. }
  791. result = 0;
  792. err:
  793. kfree(desc);
  794. dev->descriptor.bNumConfigurations = cfgno;
  795. err2:
  796. if (result == -ENOMEM)
  797. dev_err(ddev, "out of memory\n");
  798. return result;
  799. }
  800. void usb_release_bos_descriptor(struct usb_device *dev)
  801. {
  802. if (dev->bos) {
  803. kfree(dev->bos->desc);
  804. kfree(dev->bos);
  805. dev->bos = NULL;
  806. }
  807. }
  808. static const __u8 bos_desc_len[256] = {
  809. [USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
  810. [USB_CAP_TYPE_EXT] = USB_DT_USB_EXT_CAP_SIZE,
  811. [USB_SS_CAP_TYPE] = USB_DT_USB_SS_CAP_SIZE,
  812. [USB_SSP_CAP_TYPE] = USB_DT_USB_SSP_CAP_SIZE(1),
  813. [CONTAINER_ID_TYPE] = USB_DT_USB_SS_CONTN_ID_SIZE,
  814. [USB_PTM_CAP_TYPE] = USB_DT_USB_PTM_ID_SIZE,
  815. };
  816. /* Get BOS descriptor set */
  817. int usb_get_bos_descriptor(struct usb_device *dev)
  818. {
  819. struct device *ddev = &dev->dev;
  820. struct usb_bos_descriptor *bos;
  821. struct usb_dev_cap_header *cap;
  822. struct usb_ssp_cap_descriptor *ssp_cap;
  823. unsigned char *buffer;
  824. int length, total_len, num, i, ssac;
  825. __u8 cap_type;
  826. int ret;
  827. bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
  828. if (!bos)
  829. return -ENOMEM;
  830. /* Get BOS descriptor */
  831. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
  832. if (ret < USB_DT_BOS_SIZE) {
  833. dev_err(ddev, "unable to get BOS descriptor\n");
  834. if (ret >= 0)
  835. ret = -ENOMSG;
  836. kfree(bos);
  837. return ret;
  838. }
  839. length = bos->bLength;
  840. total_len = le16_to_cpu(bos->wTotalLength);
  841. num = bos->bNumDeviceCaps;
  842. kfree(bos);
  843. if (total_len < length)
  844. return -EINVAL;
  845. dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
  846. if (!dev->bos)
  847. return -ENOMEM;
  848. /* Now let's get the whole BOS descriptor set */
  849. buffer = kzalloc(total_len, GFP_KERNEL);
  850. if (!buffer) {
  851. ret = -ENOMEM;
  852. goto err;
  853. }
  854. dev->bos->desc = (struct usb_bos_descriptor *)buffer;
  855. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
  856. if (ret < total_len) {
  857. dev_err(ddev, "unable to get BOS descriptor set\n");
  858. if (ret >= 0)
  859. ret = -ENOMSG;
  860. goto err;
  861. }
  862. total_len -= length;
  863. for (i = 0; i < num; i++) {
  864. buffer += length;
  865. cap = (struct usb_dev_cap_header *)buffer;
  866. if (total_len < sizeof(*cap) || total_len < cap->bLength) {
  867. dev->bos->desc->bNumDeviceCaps = i;
  868. break;
  869. }
  870. cap_type = cap->bDevCapabilityType;
  871. length = cap->bLength;
  872. if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
  873. dev->bos->desc->bNumDeviceCaps = i;
  874. break;
  875. }
  876. total_len -= length;
  877. if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
  878. dev_warn(ddev, "descriptor type invalid, skip\n");
  879. continue;
  880. }
  881. switch (cap_type) {
  882. case USB_CAP_TYPE_WIRELESS_USB:
  883. /* Wireless USB cap descriptor is handled by wusb */
  884. break;
  885. case USB_CAP_TYPE_EXT:
  886. dev->bos->ext_cap =
  887. (struct usb_ext_cap_descriptor *)buffer;
  888. break;
  889. case USB_SS_CAP_TYPE:
  890. dev->bos->ss_cap =
  891. (struct usb_ss_cap_descriptor *)buffer;
  892. break;
  893. case USB_SSP_CAP_TYPE:
  894. ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
  895. ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
  896. USB_SSP_SUBLINK_SPEED_ATTRIBS) + 1;
  897. if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
  898. dev->bos->ssp_cap = ssp_cap;
  899. break;
  900. case CONTAINER_ID_TYPE:
  901. dev->bos->ss_id =
  902. (struct usb_ss_container_id_descriptor *)buffer;
  903. break;
  904. case USB_PTM_CAP_TYPE:
  905. dev->bos->ptm_cap =
  906. (struct usb_ptm_cap_descriptor *)buffer;
  907. default:
  908. break;
  909. }
  910. }
  911. return 0;
  912. err:
  913. usb_release_bos_descriptor(dev);
  914. return ret;
  915. }