config.c 24 KB

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