config.c 32 KB

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