config.c 27 KB

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