config.c 24 KB

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