config.c 23 KB

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