config.c 23 KB

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