config.c 29 KB

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