config.c 24 KB

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