config.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) *
  112. (desc->bmAttributes + 1);
  113. else if (usb_endpoint_xfer_int(&ep->desc))
  114. max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1);
  115. else
  116. max_tx = 999999;
  117. if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
  118. dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
  119. "config %d interface %d altsetting %d ep %d: "
  120. "setting to %d\n",
  121. usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
  122. desc->wBytesPerInterval,
  123. cfgno, inum, asnum, ep->desc.bEndpointAddress,
  124. max_tx);
  125. ep->ss_ep_comp.wBytesPerInterval = max_tx;
  126. }
  127. }
  128. static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
  129. int asnum, struct usb_host_interface *ifp, int num_ep,
  130. unsigned char *buffer, int size)
  131. {
  132. unsigned char *buffer0 = buffer;
  133. struct usb_endpoint_descriptor *d;
  134. struct usb_host_endpoint *endpoint;
  135. int n, i, j, retval;
  136. d = (struct usb_endpoint_descriptor *) buffer;
  137. buffer += d->bLength;
  138. size -= d->bLength;
  139. if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
  140. n = USB_DT_ENDPOINT_AUDIO_SIZE;
  141. else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
  142. n = USB_DT_ENDPOINT_SIZE;
  143. else {
  144. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  145. "invalid endpoint descriptor of length %d, skipping\n",
  146. cfgno, inum, asnum, d->bLength);
  147. goto skip_to_next_endpoint_or_interface_descriptor;
  148. }
  149. i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
  150. if (i >= 16 || i == 0) {
  151. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  152. "invalid endpoint with address 0x%X, skipping\n",
  153. cfgno, inum, asnum, d->bEndpointAddress);
  154. goto skip_to_next_endpoint_or_interface_descriptor;
  155. }
  156. /* Only store as many endpoints as we have room for */
  157. if (ifp->desc.bNumEndpoints >= num_ep)
  158. goto skip_to_next_endpoint_or_interface_descriptor;
  159. endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
  160. ++ifp->desc.bNumEndpoints;
  161. memcpy(&endpoint->desc, d, n);
  162. INIT_LIST_HEAD(&endpoint->urb_list);
  163. /* Fix up bInterval values outside the legal range. Use 32 ms if no
  164. * proper value can be guessed. */
  165. i = 0; /* i = min, j = max, n = default */
  166. j = 255;
  167. if (usb_endpoint_xfer_int(d)) {
  168. i = 1;
  169. switch (to_usb_device(ddev)->speed) {
  170. case USB_SPEED_SUPER:
  171. case USB_SPEED_HIGH:
  172. /* Many device manufacturers are using full-speed
  173. * bInterval values in high-speed interrupt endpoint
  174. * descriptors. Try to fix those and fall back to a
  175. * 32 ms default value otherwise. */
  176. n = fls(d->bInterval*8);
  177. if (n == 0)
  178. n = 9; /* 32 ms = 2^(9-1) uframes */
  179. j = 16;
  180. break;
  181. default: /* USB_SPEED_FULL or _LOW */
  182. /* For low-speed, 10 ms is the official minimum.
  183. * But some "overclocked" devices might want faster
  184. * polling so we'll allow it. */
  185. n = 32;
  186. break;
  187. }
  188. } else if (usb_endpoint_xfer_isoc(d)) {
  189. i = 1;
  190. j = 16;
  191. switch (to_usb_device(ddev)->speed) {
  192. case USB_SPEED_HIGH:
  193. n = 9; /* 32 ms = 2^(9-1) uframes */
  194. break;
  195. default: /* USB_SPEED_FULL */
  196. n = 6; /* 32 ms = 2^(6-1) frames */
  197. break;
  198. }
  199. }
  200. if (d->bInterval < i || d->bInterval > j) {
  201. dev_warn(ddev, "config %d interface %d altsetting %d "
  202. "endpoint 0x%X has an invalid bInterval %d, "
  203. "changing to %d\n",
  204. cfgno, inum, asnum,
  205. d->bEndpointAddress, d->bInterval, n);
  206. endpoint->desc.bInterval = n;
  207. }
  208. /* Some buggy low-speed devices have Bulk endpoints, which is
  209. * explicitly forbidden by the USB spec. In an attempt to make
  210. * them usable, we will try treating them as Interrupt endpoints.
  211. */
  212. if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
  213. usb_endpoint_xfer_bulk(d)) {
  214. dev_warn(ddev, "config %d interface %d altsetting %d "
  215. "endpoint 0x%X is Bulk; changing to Interrupt\n",
  216. cfgno, inum, asnum, d->bEndpointAddress);
  217. endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
  218. endpoint->desc.bInterval = 1;
  219. if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
  220. endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
  221. }
  222. /*
  223. * Some buggy high speed devices have bulk endpoints using
  224. * maxpacket sizes other than 512. High speed HCDs may not
  225. * be able to handle that particular bug, so let's warn...
  226. */
  227. if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
  228. && usb_endpoint_xfer_bulk(d)) {
  229. unsigned maxp;
  230. maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
  231. if (maxp != 512)
  232. dev_warn(ddev, "config %d interface %d altsetting %d "
  233. "bulk endpoint 0x%X has invalid maxpacket %d\n",
  234. cfgno, inum, asnum, d->bEndpointAddress,
  235. maxp);
  236. }
  237. /* Parse a possible SuperSpeed endpoint companion descriptor */
  238. if (to_usb_device(ddev)->speed == USB_SPEED_SUPER)
  239. usb_parse_ss_endpoint_companion(ddev, cfgno,
  240. inum, asnum, endpoint, buffer, size);
  241. /* Skip over any Class Specific or Vendor Specific descriptors;
  242. * find the next endpoint or interface descriptor */
  243. endpoint->extra = buffer;
  244. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  245. USB_DT_INTERFACE, &n);
  246. endpoint->extralen = i;
  247. retval = buffer - buffer0 + i;
  248. if (n > 0)
  249. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  250. n, plural(n), "endpoint");
  251. return retval;
  252. skip_to_next_endpoint_or_interface_descriptor:
  253. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  254. USB_DT_INTERFACE, NULL);
  255. return buffer - buffer0 + i;
  256. }
  257. void usb_release_interface_cache(struct kref *ref)
  258. {
  259. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  260. int j;
  261. for (j = 0; j < intfc->num_altsetting; j++) {
  262. struct usb_host_interface *alt = &intfc->altsetting[j];
  263. kfree(alt->endpoint);
  264. kfree(alt->string);
  265. }
  266. kfree(intfc);
  267. }
  268. static int usb_parse_interface(struct device *ddev, int cfgno,
  269. struct usb_host_config *config, unsigned char *buffer, int size,
  270. u8 inums[], u8 nalts[])
  271. {
  272. unsigned char *buffer0 = buffer;
  273. struct usb_interface_descriptor *d;
  274. int inum, asnum;
  275. struct usb_interface_cache *intfc;
  276. struct usb_host_interface *alt;
  277. int i, n;
  278. int len, retval;
  279. int num_ep, num_ep_orig;
  280. d = (struct usb_interface_descriptor *) buffer;
  281. buffer += d->bLength;
  282. size -= d->bLength;
  283. if (d->bLength < USB_DT_INTERFACE_SIZE)
  284. goto skip_to_next_interface_descriptor;
  285. /* Which interface entry is this? */
  286. intfc = NULL;
  287. inum = d->bInterfaceNumber;
  288. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  289. if (inums[i] == inum) {
  290. intfc = config->intf_cache[i];
  291. break;
  292. }
  293. }
  294. if (!intfc || intfc->num_altsetting >= nalts[i])
  295. goto skip_to_next_interface_descriptor;
  296. /* Check for duplicate altsetting entries */
  297. asnum = d->bAlternateSetting;
  298. for ((i = 0, alt = &intfc->altsetting[0]);
  299. i < intfc->num_altsetting;
  300. (++i, ++alt)) {
  301. if (alt->desc.bAlternateSetting == asnum) {
  302. dev_warn(ddev, "Duplicate descriptor for config %d "
  303. "interface %d altsetting %d, skipping\n",
  304. cfgno, inum, asnum);
  305. goto skip_to_next_interface_descriptor;
  306. }
  307. }
  308. ++intfc->num_altsetting;
  309. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  310. /* Skip over any Class Specific or Vendor Specific descriptors;
  311. * find the first endpoint or interface descriptor */
  312. alt->extra = buffer;
  313. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  314. USB_DT_INTERFACE, &n);
  315. alt->extralen = i;
  316. if (n > 0)
  317. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  318. n, plural(n), "interface");
  319. buffer += i;
  320. size -= i;
  321. /* Allocate space for the right(?) number of endpoints */
  322. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  323. alt->desc.bNumEndpoints = 0; /* Use as a counter */
  324. if (num_ep > USB_MAXENDPOINTS) {
  325. dev_warn(ddev, "too many endpoints for config %d interface %d "
  326. "altsetting %d: %d, using maximum allowed: %d\n",
  327. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  328. num_ep = USB_MAXENDPOINTS;
  329. }
  330. if (num_ep > 0) {
  331. /* Can't allocate 0 bytes */
  332. len = sizeof(struct usb_host_endpoint) * num_ep;
  333. alt->endpoint = kzalloc(len, GFP_KERNEL);
  334. if (!alt->endpoint)
  335. return -ENOMEM;
  336. }
  337. /* Parse all the endpoint descriptors */
  338. n = 0;
  339. while (size > 0) {
  340. if (((struct usb_descriptor_header *) buffer)->bDescriptorType
  341. == USB_DT_INTERFACE)
  342. break;
  343. retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
  344. num_ep, buffer, size);
  345. if (retval < 0)
  346. return retval;
  347. ++n;
  348. buffer += retval;
  349. size -= retval;
  350. }
  351. if (n != num_ep_orig)
  352. dev_warn(ddev, "config %d interface %d altsetting %d has %d "
  353. "endpoint descriptor%s, different from the interface "
  354. "descriptor's value: %d\n",
  355. cfgno, inum, asnum, n, plural(n), num_ep_orig);
  356. return buffer - buffer0;
  357. skip_to_next_interface_descriptor:
  358. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  359. USB_DT_INTERFACE, NULL);
  360. return buffer - buffer0 + i;
  361. }
  362. static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
  363. struct usb_host_config *config, unsigned char *buffer, int size)
  364. {
  365. struct device *ddev = &dev->dev;
  366. unsigned char *buffer0 = buffer;
  367. int cfgno;
  368. int nintf, nintf_orig;
  369. int i, j, n;
  370. struct usb_interface_cache *intfc;
  371. unsigned char *buffer2;
  372. int size2;
  373. struct usb_descriptor_header *header;
  374. int len, retval;
  375. u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
  376. unsigned iad_num = 0;
  377. memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
  378. if (config->desc.bDescriptorType != USB_DT_CONFIG ||
  379. config->desc.bLength < USB_DT_CONFIG_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. * NOTE: if this is a WUSB device and is not authorized, we skip the
  575. * whole thing. A non-authorized USB device has no
  576. * configurations.
  577. */
  578. int usb_get_configuration(struct usb_device *dev)
  579. {
  580. struct device *ddev = &dev->dev;
  581. int ncfg = dev->descriptor.bNumConfigurations;
  582. int result = 0;
  583. unsigned int cfgno, length;
  584. unsigned char *bigbuffer;
  585. struct usb_config_descriptor *desc;
  586. cfgno = 0;
  587. if (dev->authorized == 0) /* Not really an error */
  588. goto out_not_authorized;
  589. result = -ENOMEM;
  590. if (ncfg > USB_MAXCONFIG) {
  591. dev_warn(ddev, "too many configurations: %d, "
  592. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  593. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  594. }
  595. if (ncfg < 1) {
  596. dev_err(ddev, "no configurations\n");
  597. return -EINVAL;
  598. }
  599. length = ncfg * sizeof(struct usb_host_config);
  600. dev->config = kzalloc(length, GFP_KERNEL);
  601. if (!dev->config)
  602. goto err2;
  603. length = ncfg * sizeof(char *);
  604. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  605. if (!dev->rawdescriptors)
  606. goto err2;
  607. desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  608. if (!desc)
  609. goto err2;
  610. result = 0;
  611. for (; cfgno < ncfg; cfgno++) {
  612. /* We grab just the first descriptor so we know how long
  613. * the whole configuration is */
  614. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  615. desc, USB_DT_CONFIG_SIZE);
  616. if (result < 0) {
  617. dev_err(ddev, "unable to read config index %d "
  618. "descriptor/%s: %d\n", cfgno, "start", result);
  619. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  620. dev->descriptor.bNumConfigurations = cfgno;
  621. break;
  622. } else if (result < 4) {
  623. dev_err(ddev, "config index %d descriptor too short "
  624. "(expected %i, got %i)\n", cfgno,
  625. USB_DT_CONFIG_SIZE, result);
  626. result = -EINVAL;
  627. goto err;
  628. }
  629. length = max((int) le16_to_cpu(desc->wTotalLength),
  630. USB_DT_CONFIG_SIZE);
  631. /* Now that we know the length, get the whole thing */
  632. bigbuffer = kmalloc(length, GFP_KERNEL);
  633. if (!bigbuffer) {
  634. result = -ENOMEM;
  635. goto err;
  636. }
  637. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  638. bigbuffer, length);
  639. if (result < 0) {
  640. dev_err(ddev, "unable to read config index %d "
  641. "descriptor/%s\n", cfgno, "all");
  642. kfree(bigbuffer);
  643. goto err;
  644. }
  645. if (result < length) {
  646. dev_warn(ddev, "config index %d descriptor too short "
  647. "(expected %i, got %i)\n", cfgno, length, result);
  648. length = result;
  649. }
  650. dev->rawdescriptors[cfgno] = bigbuffer;
  651. result = usb_parse_configuration(dev, cfgno,
  652. &dev->config[cfgno], bigbuffer, length);
  653. if (result < 0) {
  654. ++cfgno;
  655. goto err;
  656. }
  657. }
  658. result = 0;
  659. err:
  660. kfree(desc);
  661. out_not_authorized:
  662. dev->descriptor.bNumConfigurations = cfgno;
  663. err2:
  664. if (result == -ENOMEM)
  665. dev_err(ddev, "out of memory\n");
  666. return result;
  667. }