config.c 31 KB

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