config.c 24 KB

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