config.c 29 KB

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