config.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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_ss_endpoint_companion(struct device *ddev, int cfgno,
  38. int inum, int asnum, struct usb_host_endpoint *ep,
  39. unsigned char *buffer, int size)
  40. {
  41. struct usb_ss_ep_comp_descriptor *desc;
  42. int max_tx;
  43. /* The SuperSpeed endpoint companion descriptor is supposed to
  44. * be the first thing immediately following the endpoint descriptor.
  45. */
  46. desc = (struct usb_ss_ep_comp_descriptor *) buffer;
  47. if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
  48. size < USB_DT_SS_EP_COMP_SIZE) {
  49. dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
  50. " interface %d altsetting %d ep %d: "
  51. "using minimum values\n",
  52. cfgno, inum, asnum, ep->desc.bEndpointAddress);
  53. /* Fill in some default values.
  54. * Leave bmAttributes as zero, which will mean no streams for
  55. * bulk, and isoc won't support multiple bursts of packets.
  56. * With bursts of only one packet, and a Mult of 1, the max
  57. * amount of data moved per endpoint service interval is one
  58. * packet.
  59. */
  60. ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
  61. ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
  62. if (usb_endpoint_xfer_isoc(&ep->desc) ||
  63. usb_endpoint_xfer_int(&ep->desc))
  64. ep->ss_ep_comp.wBytesPerInterval =
  65. ep->desc.wMaxPacketSize;
  66. return;
  67. }
  68. memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
  69. /* Check the various values */
  70. if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
  71. dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
  72. "config %d interface %d altsetting %d ep %d: "
  73. "setting to zero\n", desc->bMaxBurst,
  74. cfgno, inum, asnum, ep->desc.bEndpointAddress);
  75. ep->ss_ep_comp.bMaxBurst = 0;
  76. } else if (desc->bMaxBurst > 15) {
  77. dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
  78. "config %d interface %d altsetting %d ep %d: "
  79. "setting to 15\n", desc->bMaxBurst,
  80. cfgno, inum, asnum, ep->desc.bEndpointAddress);
  81. ep->ss_ep_comp.bMaxBurst = 15;
  82. }
  83. if ((usb_endpoint_xfer_control(&ep->desc) ||
  84. usb_endpoint_xfer_int(&ep->desc)) &&
  85. desc->bmAttributes != 0) {
  86. dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
  87. "config %d interface %d altsetting %d ep %d: "
  88. "setting to zero\n",
  89. usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
  90. desc->bmAttributes,
  91. cfgno, inum, asnum, ep->desc.bEndpointAddress);
  92. ep->ss_ep_comp.bmAttributes = 0;
  93. } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
  94. desc->bmAttributes > 16) {
  95. dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
  96. "config %d interface %d altsetting %d ep %d: "
  97. "setting to max\n",
  98. cfgno, inum, asnum, ep->desc.bEndpointAddress);
  99. ep->ss_ep_comp.bmAttributes = 16;
  100. } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
  101. USB_SS_MULT(desc->bmAttributes) > 3) {
  102. dev_warn(ddev, "Isoc endpoint has Mult of %d in "
  103. "config %d interface %d altsetting %d ep %d: "
  104. "setting to 3\n",
  105. USB_SS_MULT(desc->bmAttributes),
  106. cfgno, inum, asnum, ep->desc.bEndpointAddress);
  107. ep->ss_ep_comp.bmAttributes = 2;
  108. }
  109. if (usb_endpoint_xfer_isoc(&ep->desc))
  110. max_tx = (desc->bMaxBurst + 1) *
  111. (USB_SS_MULT(desc->bmAttributes)) *
  112. usb_endpoint_maxp(&ep->desc);
  113. else if (usb_endpoint_xfer_int(&ep->desc))
  114. max_tx = usb_endpoint_maxp(&ep->desc) *
  115. (desc->bMaxBurst + 1);
  116. else
  117. max_tx = 999999;
  118. if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
  119. dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
  120. "config %d interface %d altsetting %d ep %d: "
  121. "setting to %d\n",
  122. usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
  123. le16_to_cpu(desc->wBytesPerInterval),
  124. cfgno, inum, asnum, ep->desc.bEndpointAddress,
  125. max_tx);
  126. ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
  127. }
  128. }
  129. static const unsigned short low_speed_maxpacket_maxes[4] = {
  130. [USB_ENDPOINT_XFER_CONTROL] = 8,
  131. [USB_ENDPOINT_XFER_ISOC] = 0,
  132. [USB_ENDPOINT_XFER_BULK] = 0,
  133. [USB_ENDPOINT_XFER_INT] = 8,
  134. };
  135. static const unsigned short full_speed_maxpacket_maxes[4] = {
  136. [USB_ENDPOINT_XFER_CONTROL] = 64,
  137. [USB_ENDPOINT_XFER_ISOC] = 1023,
  138. [USB_ENDPOINT_XFER_BULK] = 64,
  139. [USB_ENDPOINT_XFER_INT] = 64,
  140. };
  141. static const unsigned short high_speed_maxpacket_maxes[4] = {
  142. [USB_ENDPOINT_XFER_CONTROL] = 64,
  143. [USB_ENDPOINT_XFER_ISOC] = 1024,
  144. /* Bulk should be 512, but some devices use 1024: we will warn below */
  145. [USB_ENDPOINT_XFER_BULK] = 1024,
  146. [USB_ENDPOINT_XFER_INT] = 1024,
  147. };
  148. static const unsigned short super_speed_maxpacket_maxes[4] = {
  149. [USB_ENDPOINT_XFER_CONTROL] = 512,
  150. [USB_ENDPOINT_XFER_ISOC] = 1024,
  151. [USB_ENDPOINT_XFER_BULK] = 1024,
  152. [USB_ENDPOINT_XFER_INT] = 1024,
  153. };
  154. static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
  155. int asnum, struct usb_host_interface *ifp, int num_ep,
  156. unsigned char *buffer, int size)
  157. {
  158. unsigned char *buffer0 = buffer;
  159. struct usb_endpoint_descriptor *d;
  160. struct usb_host_endpoint *endpoint;
  161. int n, i, j, retval;
  162. unsigned int maxp;
  163. const unsigned short *maxpacket_maxes;
  164. d = (struct usb_endpoint_descriptor *) buffer;
  165. buffer += d->bLength;
  166. size -= d->bLength;
  167. if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
  168. n = USB_DT_ENDPOINT_AUDIO_SIZE;
  169. else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
  170. n = USB_DT_ENDPOINT_SIZE;
  171. else {
  172. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  173. "invalid endpoint descriptor of length %d, skipping\n",
  174. cfgno, inum, asnum, d->bLength);
  175. goto skip_to_next_endpoint_or_interface_descriptor;
  176. }
  177. i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
  178. if (i >= 16 || i == 0) {
  179. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  180. "invalid endpoint with address 0x%X, skipping\n",
  181. cfgno, inum, asnum, d->bEndpointAddress);
  182. goto skip_to_next_endpoint_or_interface_descriptor;
  183. }
  184. /* Only store as many endpoints as we have room for */
  185. if (ifp->desc.bNumEndpoints >= num_ep)
  186. goto skip_to_next_endpoint_or_interface_descriptor;
  187. /* Check for duplicate endpoint addresses */
  188. for (i = 0; i < ifp->desc.bNumEndpoints; ++i) {
  189. if (ifp->endpoint[i].desc.bEndpointAddress ==
  190. d->bEndpointAddress) {
  191. dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
  192. cfgno, inum, asnum, d->bEndpointAddress);
  193. goto skip_to_next_endpoint_or_interface_descriptor;
  194. }
  195. }
  196. endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
  197. ++ifp->desc.bNumEndpoints;
  198. memcpy(&endpoint->desc, d, n);
  199. INIT_LIST_HEAD(&endpoint->urb_list);
  200. /*
  201. * Fix up bInterval values outside the legal range.
  202. * Use 10 or 8 ms if no proper value can be guessed.
  203. */
  204. i = 0; /* i = min, j = max, n = default */
  205. j = 255;
  206. if (usb_endpoint_xfer_int(d)) {
  207. i = 1;
  208. switch (to_usb_device(ddev)->speed) {
  209. case USB_SPEED_SUPER_PLUS:
  210. case USB_SPEED_SUPER:
  211. case USB_SPEED_HIGH:
  212. /*
  213. * Many device manufacturers are using full-speed
  214. * bInterval values in high-speed interrupt endpoint
  215. * descriptors. Try to fix those and fall back to an
  216. * 8-ms default value otherwise.
  217. */
  218. n = fls(d->bInterval*8);
  219. if (n == 0)
  220. n = 7; /* 8 ms = 2^(7-1) uframes */
  221. j = 16;
  222. /*
  223. * Adjust bInterval for quirked devices.
  224. */
  225. /*
  226. * This quirk fixes bIntervals reported in ms.
  227. */
  228. if (to_usb_device(ddev)->quirks &
  229. USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) {
  230. n = clamp(fls(d->bInterval) + 3, i, j);
  231. i = j = n;
  232. }
  233. /*
  234. * This quirk fixes bIntervals reported in
  235. * linear microframes.
  236. */
  237. if (to_usb_device(ddev)->quirks &
  238. USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
  239. n = clamp(fls(d->bInterval), i, j);
  240. i = j = n;
  241. }
  242. break;
  243. default: /* USB_SPEED_FULL or _LOW */
  244. /*
  245. * For low-speed, 10 ms is the official minimum.
  246. * But some "overclocked" devices might want faster
  247. * polling so we'll allow it.
  248. */
  249. n = 10;
  250. break;
  251. }
  252. } else if (usb_endpoint_xfer_isoc(d)) {
  253. i = 1;
  254. j = 16;
  255. switch (to_usb_device(ddev)->speed) {
  256. case USB_SPEED_HIGH:
  257. n = 7; /* 8 ms = 2^(7-1) uframes */
  258. break;
  259. default: /* USB_SPEED_FULL */
  260. n = 4; /* 8 ms = 2^(4-1) frames */
  261. break;
  262. }
  263. }
  264. if (d->bInterval < i || d->bInterval > j) {
  265. dev_warn(ddev, "config %d interface %d altsetting %d "
  266. "endpoint 0x%X has an invalid bInterval %d, "
  267. "changing to %d\n",
  268. cfgno, inum, asnum,
  269. d->bEndpointAddress, d->bInterval, n);
  270. endpoint->desc.bInterval = n;
  271. }
  272. /* Some buggy low-speed devices have Bulk endpoints, which is
  273. * explicitly forbidden by the USB spec. In an attempt to make
  274. * them usable, we will try treating them as Interrupt endpoints.
  275. */
  276. if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
  277. usb_endpoint_xfer_bulk(d)) {
  278. dev_warn(ddev, "config %d interface %d altsetting %d "
  279. "endpoint 0x%X is Bulk; changing to Interrupt\n",
  280. cfgno, inum, asnum, d->bEndpointAddress);
  281. endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
  282. endpoint->desc.bInterval = 1;
  283. if (usb_endpoint_maxp(&endpoint->desc) > 8)
  284. endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
  285. }
  286. /* Validate the wMaxPacketSize field */
  287. maxp = usb_endpoint_maxp(&endpoint->desc);
  288. /* Find the highest legal maxpacket size for this endpoint */
  289. i = 0; /* additional transactions per microframe */
  290. switch (to_usb_device(ddev)->speed) {
  291. case USB_SPEED_LOW:
  292. maxpacket_maxes = low_speed_maxpacket_maxes;
  293. break;
  294. case USB_SPEED_FULL:
  295. maxpacket_maxes = full_speed_maxpacket_maxes;
  296. break;
  297. case USB_SPEED_HIGH:
  298. /* Bits 12..11 are allowed only for HS periodic endpoints */
  299. if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
  300. i = maxp & (BIT(12) | BIT(11));
  301. maxp &= ~i;
  302. }
  303. /* fallthrough */
  304. default:
  305. maxpacket_maxes = high_speed_maxpacket_maxes;
  306. break;
  307. case USB_SPEED_SUPER:
  308. case USB_SPEED_SUPER_PLUS:
  309. maxpacket_maxes = super_speed_maxpacket_maxes;
  310. break;
  311. }
  312. j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
  313. if (maxp > j) {
  314. dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
  315. cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
  316. maxp = j;
  317. endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
  318. }
  319. /*
  320. * Some buggy high speed devices have bulk endpoints using
  321. * maxpacket sizes other than 512. High speed HCDs may not
  322. * be able to handle that particular bug, so let's warn...
  323. */
  324. if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
  325. && usb_endpoint_xfer_bulk(d)) {
  326. if (maxp != 512)
  327. dev_warn(ddev, "config %d interface %d altsetting %d "
  328. "bulk endpoint 0x%X has invalid maxpacket %d\n",
  329. cfgno, inum, asnum, d->bEndpointAddress,
  330. maxp);
  331. }
  332. /* Parse a possible SuperSpeed endpoint companion descriptor */
  333. if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
  334. usb_parse_ss_endpoint_companion(ddev, cfgno,
  335. inum, asnum, endpoint, buffer, size);
  336. /* Skip over any Class Specific or Vendor Specific descriptors;
  337. * find the next endpoint or interface descriptor */
  338. endpoint->extra = buffer;
  339. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  340. USB_DT_INTERFACE, &n);
  341. endpoint->extralen = i;
  342. retval = buffer - buffer0 + i;
  343. if (n > 0)
  344. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  345. n, plural(n), "endpoint");
  346. return retval;
  347. skip_to_next_endpoint_or_interface_descriptor:
  348. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  349. USB_DT_INTERFACE, NULL);
  350. return buffer - buffer0 + i;
  351. }
  352. void usb_release_interface_cache(struct kref *ref)
  353. {
  354. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  355. int j;
  356. for (j = 0; j < intfc->num_altsetting; j++) {
  357. struct usb_host_interface *alt = &intfc->altsetting[j];
  358. kfree(alt->endpoint);
  359. kfree(alt->string);
  360. }
  361. kfree(intfc);
  362. }
  363. static int usb_parse_interface(struct device *ddev, int cfgno,
  364. struct usb_host_config *config, unsigned char *buffer, int size,
  365. u8 inums[], u8 nalts[])
  366. {
  367. unsigned char *buffer0 = buffer;
  368. struct usb_interface_descriptor *d;
  369. int inum, asnum;
  370. struct usb_interface_cache *intfc;
  371. struct usb_host_interface *alt;
  372. int i, n;
  373. int len, retval;
  374. int num_ep, num_ep_orig;
  375. d = (struct usb_interface_descriptor *) buffer;
  376. buffer += d->bLength;
  377. size -= d->bLength;
  378. if (d->bLength < USB_DT_INTERFACE_SIZE)
  379. goto skip_to_next_interface_descriptor;
  380. /* Which interface entry is this? */
  381. intfc = NULL;
  382. inum = d->bInterfaceNumber;
  383. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  384. if (inums[i] == inum) {
  385. intfc = config->intf_cache[i];
  386. break;
  387. }
  388. }
  389. if (!intfc || intfc->num_altsetting >= nalts[i])
  390. goto skip_to_next_interface_descriptor;
  391. /* Check for duplicate altsetting entries */
  392. asnum = d->bAlternateSetting;
  393. for ((i = 0, alt = &intfc->altsetting[0]);
  394. i < intfc->num_altsetting;
  395. (++i, ++alt)) {
  396. if (alt->desc.bAlternateSetting == asnum) {
  397. dev_warn(ddev, "Duplicate descriptor for config %d "
  398. "interface %d altsetting %d, skipping\n",
  399. cfgno, inum, asnum);
  400. goto skip_to_next_interface_descriptor;
  401. }
  402. }
  403. ++intfc->num_altsetting;
  404. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  405. /* Skip over any Class Specific or Vendor Specific descriptors;
  406. * find the first endpoint or interface descriptor */
  407. alt->extra = buffer;
  408. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  409. USB_DT_INTERFACE, &n);
  410. alt->extralen = i;
  411. if (n > 0)
  412. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  413. n, plural(n), "interface");
  414. buffer += i;
  415. size -= i;
  416. /* Allocate space for the right(?) number of endpoints */
  417. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  418. alt->desc.bNumEndpoints = 0; /* Use as a counter */
  419. if (num_ep > USB_MAXENDPOINTS) {
  420. dev_warn(ddev, "too many endpoints for config %d interface %d "
  421. "altsetting %d: %d, using maximum allowed: %d\n",
  422. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  423. num_ep = USB_MAXENDPOINTS;
  424. }
  425. if (num_ep > 0) {
  426. /* Can't allocate 0 bytes */
  427. len = sizeof(struct usb_host_endpoint) * num_ep;
  428. alt->endpoint = kzalloc(len, GFP_KERNEL);
  429. if (!alt->endpoint)
  430. return -ENOMEM;
  431. }
  432. /* Parse all the endpoint descriptors */
  433. n = 0;
  434. while (size > 0) {
  435. if (((struct usb_descriptor_header *) buffer)->bDescriptorType
  436. == USB_DT_INTERFACE)
  437. break;
  438. retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
  439. num_ep, buffer, size);
  440. if (retval < 0)
  441. return retval;
  442. ++n;
  443. buffer += retval;
  444. size -= retval;
  445. }
  446. if (n != num_ep_orig)
  447. dev_warn(ddev, "config %d interface %d altsetting %d has %d "
  448. "endpoint descriptor%s, different from the interface "
  449. "descriptor's value: %d\n",
  450. cfgno, inum, asnum, n, plural(n), num_ep_orig);
  451. return buffer - buffer0;
  452. skip_to_next_interface_descriptor:
  453. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  454. USB_DT_INTERFACE, NULL);
  455. return buffer - buffer0 + i;
  456. }
  457. static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
  458. struct usb_host_config *config, unsigned char *buffer, int size)
  459. {
  460. struct device *ddev = &dev->dev;
  461. unsigned char *buffer0 = buffer;
  462. int cfgno;
  463. int nintf, nintf_orig;
  464. int i, j, n;
  465. struct usb_interface_cache *intfc;
  466. unsigned char *buffer2;
  467. int size2;
  468. struct usb_descriptor_header *header;
  469. int len, retval;
  470. u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
  471. unsigned iad_num = 0;
  472. memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
  473. nintf = nintf_orig = config->desc.bNumInterfaces;
  474. config->desc.bNumInterfaces = 0; // Adjusted later
  475. if (config->desc.bDescriptorType != USB_DT_CONFIG ||
  476. config->desc.bLength < USB_DT_CONFIG_SIZE ||
  477. config->desc.bLength > size) {
  478. dev_err(ddev, "invalid descriptor for config index %d: "
  479. "type = 0x%X, length = %d\n", cfgidx,
  480. config->desc.bDescriptorType, config->desc.bLength);
  481. return -EINVAL;
  482. }
  483. cfgno = config->desc.bConfigurationValue;
  484. buffer += config->desc.bLength;
  485. size -= config->desc.bLength;
  486. if (nintf > USB_MAXINTERFACES) {
  487. dev_warn(ddev, "config %d has too many interfaces: %d, "
  488. "using maximum allowed: %d\n",
  489. cfgno, nintf, USB_MAXINTERFACES);
  490. nintf = USB_MAXINTERFACES;
  491. }
  492. /* Go through the descriptors, checking their length and counting the
  493. * number of altsettings for each interface */
  494. n = 0;
  495. for ((buffer2 = buffer, size2 = size);
  496. size2 > 0;
  497. (buffer2 += header->bLength, size2 -= header->bLength)) {
  498. if (size2 < sizeof(struct usb_descriptor_header)) {
  499. dev_warn(ddev, "config %d descriptor has %d excess "
  500. "byte%s, ignoring\n",
  501. cfgno, size2, plural(size2));
  502. break;
  503. }
  504. header = (struct usb_descriptor_header *) buffer2;
  505. if ((header->bLength > size2) || (header->bLength < 2)) {
  506. dev_warn(ddev, "config %d has an invalid descriptor "
  507. "of length %d, skipping remainder of the config\n",
  508. cfgno, header->bLength);
  509. break;
  510. }
  511. if (header->bDescriptorType == USB_DT_INTERFACE) {
  512. struct usb_interface_descriptor *d;
  513. int inum;
  514. d = (struct usb_interface_descriptor *) header;
  515. if (d->bLength < USB_DT_INTERFACE_SIZE) {
  516. dev_warn(ddev, "config %d has an invalid "
  517. "interface descriptor of length %d, "
  518. "skipping\n", cfgno, d->bLength);
  519. continue;
  520. }
  521. inum = d->bInterfaceNumber;
  522. if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
  523. n >= nintf_orig) {
  524. dev_warn(ddev, "config %d has more interface "
  525. "descriptors, than it declares in "
  526. "bNumInterfaces, ignoring interface "
  527. "number: %d\n", cfgno, inum);
  528. continue;
  529. }
  530. if (inum >= nintf_orig)
  531. dev_warn(ddev, "config %d has an invalid "
  532. "interface number: %d but max is %d\n",
  533. cfgno, inum, nintf_orig - 1);
  534. /* Have we already encountered this interface?
  535. * Count its altsettings */
  536. for (i = 0; i < n; ++i) {
  537. if (inums[i] == inum)
  538. break;
  539. }
  540. if (i < n) {
  541. if (nalts[i] < 255)
  542. ++nalts[i];
  543. } else if (n < USB_MAXINTERFACES) {
  544. inums[n] = inum;
  545. nalts[n] = 1;
  546. ++n;
  547. }
  548. } else if (header->bDescriptorType ==
  549. USB_DT_INTERFACE_ASSOCIATION) {
  550. struct usb_interface_assoc_descriptor *d;
  551. d = (struct usb_interface_assoc_descriptor *)header;
  552. if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
  553. dev_warn(ddev,
  554. "config %d has an invalid interface association descriptor of length %d, skipping\n",
  555. cfgno, d->bLength);
  556. continue;
  557. }
  558. if (iad_num == USB_MAXIADS) {
  559. dev_warn(ddev, "found more Interface "
  560. "Association Descriptors "
  561. "than allocated for in "
  562. "configuration %d\n", cfgno);
  563. } else {
  564. config->intf_assoc[iad_num] = d;
  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(200);
  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. static const __u8 bos_desc_len[256] = {
  777. [USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
  778. [USB_CAP_TYPE_EXT] = USB_DT_USB_EXT_CAP_SIZE,
  779. [USB_SS_CAP_TYPE] = USB_DT_USB_SS_CAP_SIZE,
  780. [USB_SSP_CAP_TYPE] = USB_DT_USB_SSP_CAP_SIZE(1),
  781. [CONTAINER_ID_TYPE] = USB_DT_USB_SS_CONTN_ID_SIZE,
  782. [USB_PTM_CAP_TYPE] = USB_DT_USB_PTM_ID_SIZE,
  783. };
  784. /* Get BOS descriptor set */
  785. int usb_get_bos_descriptor(struct usb_device *dev)
  786. {
  787. struct device *ddev = &dev->dev;
  788. struct usb_bos_descriptor *bos;
  789. struct usb_dev_cap_header *cap;
  790. struct usb_ssp_cap_descriptor *ssp_cap;
  791. unsigned char *buffer;
  792. int length, total_len, num, i, ssac;
  793. __u8 cap_type;
  794. int ret;
  795. bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
  796. if (!bos)
  797. return -ENOMEM;
  798. /* Get BOS descriptor */
  799. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
  800. if (ret < USB_DT_BOS_SIZE) {
  801. dev_err(ddev, "unable to get BOS descriptor\n");
  802. if (ret >= 0)
  803. ret = -ENOMSG;
  804. kfree(bos);
  805. return ret;
  806. }
  807. length = bos->bLength;
  808. total_len = le16_to_cpu(bos->wTotalLength);
  809. num = bos->bNumDeviceCaps;
  810. kfree(bos);
  811. if (total_len < length)
  812. return -EINVAL;
  813. dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
  814. if (!dev->bos)
  815. return -ENOMEM;
  816. /* Now let's get the whole BOS descriptor set */
  817. buffer = kzalloc(total_len, GFP_KERNEL);
  818. if (!buffer) {
  819. ret = -ENOMEM;
  820. goto err;
  821. }
  822. dev->bos->desc = (struct usb_bos_descriptor *)buffer;
  823. ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
  824. if (ret < total_len) {
  825. dev_err(ddev, "unable to get BOS descriptor set\n");
  826. if (ret >= 0)
  827. ret = -ENOMSG;
  828. goto err;
  829. }
  830. total_len -= length;
  831. for (i = 0; i < num; i++) {
  832. buffer += length;
  833. cap = (struct usb_dev_cap_header *)buffer;
  834. if (total_len < sizeof(*cap) || total_len < cap->bLength) {
  835. dev->bos->desc->bNumDeviceCaps = i;
  836. break;
  837. }
  838. cap_type = cap->bDevCapabilityType;
  839. length = cap->bLength;
  840. if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
  841. dev->bos->desc->bNumDeviceCaps = i;
  842. break;
  843. }
  844. total_len -= length;
  845. if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
  846. dev_warn(ddev, "descriptor type invalid, skip\n");
  847. continue;
  848. }
  849. switch (cap_type) {
  850. case USB_CAP_TYPE_WIRELESS_USB:
  851. /* Wireless USB cap descriptor is handled by wusb */
  852. break;
  853. case USB_CAP_TYPE_EXT:
  854. dev->bos->ext_cap =
  855. (struct usb_ext_cap_descriptor *)buffer;
  856. break;
  857. case USB_SS_CAP_TYPE:
  858. dev->bos->ss_cap =
  859. (struct usb_ss_cap_descriptor *)buffer;
  860. break;
  861. case USB_SSP_CAP_TYPE:
  862. ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
  863. ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
  864. USB_SSP_SUBLINK_SPEED_ATTRIBS);
  865. if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
  866. dev->bos->ssp_cap = ssp_cap;
  867. break;
  868. case CONTAINER_ID_TYPE:
  869. dev->bos->ss_id =
  870. (struct usb_ss_container_id_descriptor *)buffer;
  871. break;
  872. case USB_PTM_CAP_TYPE:
  873. dev->bos->ptm_cap =
  874. (struct usb_ptm_cap_descriptor *)buffer;
  875. default:
  876. break;
  877. }
  878. }
  879. return 0;
  880. err:
  881. usb_release_bos_descriptor(dev);
  882. return ret;
  883. }