config.c 26 KB

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