config.c 24 KB

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