config.c 24 KB

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