config.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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; i++)
  684. kfree(dev->rawdescriptors[i]);
  685. kfree(dev->rawdescriptors);
  686. dev->rawdescriptors = NULL;
  687. }
  688. for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
  689. struct usb_host_config *cf = &dev->config[c];
  690. kfree(cf->string);
  691. for (i = 0; i < cf->desc.bNumInterfaces; i++) {
  692. if (cf->intf_cache[i])
  693. kref_put(&cf->intf_cache[i]->ref,
  694. usb_release_interface_cache);
  695. }
  696. }
  697. kfree(dev->config);
  698. dev->config = NULL;
  699. }
  700. /*
  701. * Get the USB config descriptors, cache and parse'em
  702. *
  703. * hub-only!! ... and only in reset path, or usb_new_device()
  704. * (used by real hubs and virtual root hubs)
  705. */
  706. int usb_get_configuration(struct usb_device *dev)
  707. {
  708. struct device *ddev = &dev->dev;
  709. int ncfg = dev->descriptor.bNumConfigurations;
  710. int result = 0;
  711. unsigned int cfgno, length;
  712. unsigned char *bigbuffer;
  713. struct usb_config_descriptor *desc;
  714. cfgno = 0;
  715. result = -ENOMEM;
  716. if (ncfg > USB_MAXCONFIG) {
  717. dev_warn(ddev, "too many configurations: %d, "
  718. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  719. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  720. }
  721. if (ncfg < 1) {
  722. dev_err(ddev, "no configurations\n");
  723. return -EINVAL;
  724. }
  725. length = ncfg * sizeof(struct usb_host_config);
  726. dev->config = kzalloc(length, GFP_KERNEL);
  727. if (!dev->config)
  728. goto err2;
  729. length = ncfg * sizeof(char *);
  730. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  731. if (!dev->rawdescriptors)
  732. goto err2;
  733. desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  734. if (!desc)
  735. goto err2;
  736. result = 0;
  737. for (; cfgno < ncfg; cfgno++) {
  738. /* We grab just the first descriptor so we know how long
  739. * the whole configuration is */
  740. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  741. desc, USB_DT_CONFIG_SIZE);
  742. if (result < 0) {
  743. dev_err(ddev, "unable to read config index %d "
  744. "descriptor/%s: %d\n", cfgno, "start", result);
  745. if (result != -EPIPE)
  746. goto err;
  747. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  748. dev->descriptor.bNumConfigurations = cfgno;
  749. break;
  750. } else if (result < 4) {
  751. dev_err(ddev, "config index %d descriptor too short "
  752. "(expected %i, got %i)\n", cfgno,
  753. USB_DT_CONFIG_SIZE, result);
  754. result = -EINVAL;
  755. goto err;
  756. }
  757. length = max((int) le16_to_cpu(desc->wTotalLength),
  758. USB_DT_CONFIG_SIZE);
  759. /* Now that we know the length, get the whole thing */
  760. bigbuffer = kmalloc(length, GFP_KERNEL);
  761. if (!bigbuffer) {
  762. result = -ENOMEM;
  763. goto err;
  764. }
  765. if (dev->quirks & USB_QUIRK_DELAY_INIT)
  766. msleep(200);
  767. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  768. bigbuffer, length);
  769. if (result < 0) {
  770. dev_err(ddev, "unable to read config index %d "
  771. "descriptor/%s\n", cfgno, "all");
  772. kfree(bigbuffer);
  773. goto err;
  774. }
  775. if (result < length) {
  776. dev_warn(ddev, "config index %d descriptor too short "
  777. "(expected %i, got %i)\n", cfgno, length, result);
  778. length = result;
  779. }
  780. dev->rawdescriptors[cfgno] = bigbuffer;
  781. result = usb_parse_configuration(dev, cfgno,
  782. &dev->config[cfgno], bigbuffer, length);
  783. if (result < 0) {
  784. ++cfgno;
  785. goto err;
  786. }
  787. }
  788. result = 0;
  789. err:
  790. kfree(desc);
  791. dev->descriptor.bNumConfigurations = cfgno;
  792. err2:
  793. if (result == -ENOMEM)
  794. dev_err(ddev, "out of memory\n");
  795. return result;
  796. }
  797. void usb_release_bos_descriptor(struct usb_device *dev)
  798. {
  799. if (dev->bos) {
  800. kfree(dev->bos->desc);
  801. kfree(dev->bos);
  802. dev->bos = NULL;
  803. }
  804. }
  805. /* Get BOS descriptor set */
  806. int usb_get_bos_descriptor(struct usb_device *dev)
  807. {
  808. struct device *ddev = &dev->dev;
  809. struct usb_bos_descriptor *bos;
  810. struct usb_dev_cap_header *cap;
  811. unsigned char *buffer;
  812. int length, total_len, num, i;
  813. int ret;
  814. bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
  815. if (!bos)
  816. return -ENOMEM;
  817. /* Get BOS descriptor */
  818. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
  819. if (ret < USB_DT_BOS_SIZE) {
  820. dev_err(ddev, "unable to get BOS descriptor\n");
  821. if (ret >= 0)
  822. ret = -ENOMSG;
  823. kfree(bos);
  824. return ret;
  825. }
  826. length = bos->bLength;
  827. total_len = le16_to_cpu(bos->wTotalLength);
  828. num = bos->bNumDeviceCaps;
  829. kfree(bos);
  830. if (total_len < length)
  831. return -EINVAL;
  832. dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
  833. if (!dev->bos)
  834. return -ENOMEM;
  835. /* Now let's get the whole BOS descriptor set */
  836. buffer = kzalloc(total_len, GFP_KERNEL);
  837. if (!buffer) {
  838. ret = -ENOMEM;
  839. goto err;
  840. }
  841. dev->bos->desc = (struct usb_bos_descriptor *)buffer;
  842. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
  843. if (ret < total_len) {
  844. dev_err(ddev, "unable to get BOS descriptor set\n");
  845. if (ret >= 0)
  846. ret = -ENOMSG;
  847. goto err;
  848. }
  849. total_len -= length;
  850. for (i = 0; i < num; i++) {
  851. buffer += length;
  852. cap = (struct usb_dev_cap_header *)buffer;
  853. if (total_len < sizeof(*cap) || total_len < cap->bLength) {
  854. dev->bos->desc->bNumDeviceCaps = i;
  855. break;
  856. }
  857. length = cap->bLength;
  858. total_len -= length;
  859. if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
  860. dev_warn(ddev, "descriptor type invalid, skip\n");
  861. continue;
  862. }
  863. switch (cap->bDevCapabilityType) {
  864. case USB_CAP_TYPE_WIRELESS_USB:
  865. /* Wireless USB cap descriptor is handled by wusb */
  866. break;
  867. case USB_CAP_TYPE_EXT:
  868. dev->bos->ext_cap =
  869. (struct usb_ext_cap_descriptor *)buffer;
  870. break;
  871. case USB_SS_CAP_TYPE:
  872. dev->bos->ss_cap =
  873. (struct usb_ss_cap_descriptor *)buffer;
  874. break;
  875. case USB_SSP_CAP_TYPE:
  876. dev->bos->ssp_cap =
  877. (struct usb_ssp_cap_descriptor *)buffer;
  878. break;
  879. case CONTAINER_ID_TYPE:
  880. dev->bos->ss_id =
  881. (struct usb_ss_container_id_descriptor *)buffer;
  882. break;
  883. case USB_PTM_CAP_TYPE:
  884. dev->bos->ptm_cap =
  885. (struct usb_ptm_cap_descriptor *)buffer;
  886. default:
  887. break;
  888. }
  889. }
  890. return 0;
  891. err:
  892. usb_release_bos_descriptor(dev);
  893. return ret;
  894. }