config.c 29 KB

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