config.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. static int find_next_descriptor(unsigned char *buffer, int size,
  18. int dt1, int dt2, int *num_skipped)
  19. {
  20. struct usb_descriptor_header *h;
  21. int n = 0;
  22. unsigned char *buffer0 = buffer;
  23. /* Find the next descriptor of type dt1 or dt2 */
  24. while (size > 0) {
  25. h = (struct usb_descriptor_header *) buffer;
  26. if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
  27. break;
  28. buffer += h->bLength;
  29. size -= h->bLength;
  30. ++n;
  31. }
  32. /* Store the number of descriptors skipped and return the
  33. * number of bytes skipped */
  34. if (num_skipped)
  35. *num_skipped = n;
  36. return buffer - buffer0;
  37. }
  38. static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
  39. int asnum, struct usb_host_interface *ifp, int num_ep,
  40. unsigned char *buffer, int size)
  41. {
  42. unsigned char *buffer0 = buffer;
  43. struct usb_endpoint_descriptor *d;
  44. struct usb_host_endpoint *endpoint;
  45. int n, i, j;
  46. d = (struct usb_endpoint_descriptor *) buffer;
  47. buffer += d->bLength;
  48. size -= d->bLength;
  49. if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
  50. n = USB_DT_ENDPOINT_AUDIO_SIZE;
  51. else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
  52. n = USB_DT_ENDPOINT_SIZE;
  53. else {
  54. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  55. "invalid endpoint descriptor of length %d, skipping\n",
  56. cfgno, inum, asnum, d->bLength);
  57. goto skip_to_next_endpoint_or_interface_descriptor;
  58. }
  59. i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
  60. if (i >= 16 || i == 0) {
  61. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  62. "invalid endpoint with address 0x%X, skipping\n",
  63. cfgno, inum, asnum, d->bEndpointAddress);
  64. goto skip_to_next_endpoint_or_interface_descriptor;
  65. }
  66. /* Only store as many endpoints as we have room for */
  67. if (ifp->desc.bNumEndpoints >= num_ep)
  68. goto skip_to_next_endpoint_or_interface_descriptor;
  69. endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
  70. ++ifp->desc.bNumEndpoints;
  71. memcpy(&endpoint->desc, d, n);
  72. INIT_LIST_HEAD(&endpoint->urb_list);
  73. /* If the bInterval value is outside the legal range,
  74. * set it to a default value: 32 ms */
  75. i = 0; /* i = min, j = max, n = default */
  76. j = 255;
  77. if (usb_endpoint_xfer_int(d)) {
  78. i = 1;
  79. switch (to_usb_device(ddev)->speed) {
  80. case USB_SPEED_HIGH:
  81. n = 9; /* 32 ms = 2^(9-1) uframes */
  82. j = 16;
  83. break;
  84. default: /* USB_SPEED_FULL or _LOW */
  85. /* For low-speed, 10 ms is the official minimum.
  86. * But some "overclocked" devices might want faster
  87. * polling so we'll allow it. */
  88. n = 32;
  89. break;
  90. }
  91. } else if (usb_endpoint_xfer_isoc(d)) {
  92. i = 1;
  93. j = 16;
  94. switch (to_usb_device(ddev)->speed) {
  95. case USB_SPEED_HIGH:
  96. n = 9; /* 32 ms = 2^(9-1) uframes */
  97. break;
  98. default: /* USB_SPEED_FULL */
  99. n = 6; /* 32 ms = 2^(6-1) frames */
  100. break;
  101. }
  102. }
  103. if (d->bInterval < i || d->bInterval > j) {
  104. dev_warn(ddev, "config %d interface %d altsetting %d "
  105. "endpoint 0x%X has an invalid bInterval %d, "
  106. "changing to %d\n",
  107. cfgno, inum, asnum,
  108. d->bEndpointAddress, d->bInterval, n);
  109. endpoint->desc.bInterval = n;
  110. }
  111. /* Some buggy low-speed devices have Bulk endpoints, which is
  112. * explicitly forbidden by the USB spec. In an attempt to make
  113. * them usable, we will try treating them as Interrupt endpoints.
  114. */
  115. if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
  116. usb_endpoint_xfer_bulk(d)) {
  117. dev_warn(ddev, "config %d interface %d altsetting %d "
  118. "endpoint 0x%X is Bulk; changing to Interrupt\n",
  119. cfgno, inum, asnum, d->bEndpointAddress);
  120. endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
  121. endpoint->desc.bInterval = 1;
  122. if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
  123. endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
  124. }
  125. /* Skip over any Class Specific or Vendor Specific descriptors;
  126. * find the next endpoint or interface descriptor */
  127. endpoint->extra = buffer;
  128. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  129. USB_DT_INTERFACE, &n);
  130. endpoint->extralen = i;
  131. if (n > 0)
  132. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  133. n, plural(n), "endpoint");
  134. return buffer - buffer0 + i;
  135. skip_to_next_endpoint_or_interface_descriptor:
  136. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  137. USB_DT_INTERFACE, NULL);
  138. return buffer - buffer0 + i;
  139. }
  140. void usb_release_interface_cache(struct kref *ref)
  141. {
  142. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  143. int j;
  144. for (j = 0; j < intfc->num_altsetting; j++) {
  145. struct usb_host_interface *alt = &intfc->altsetting[j];
  146. kfree(alt->endpoint);
  147. kfree(alt->string);
  148. }
  149. kfree(intfc);
  150. }
  151. static int usb_parse_interface(struct device *ddev, int cfgno,
  152. struct usb_host_config *config, unsigned char *buffer, int size,
  153. u8 inums[], u8 nalts[])
  154. {
  155. unsigned char *buffer0 = buffer;
  156. struct usb_interface_descriptor *d;
  157. int inum, asnum;
  158. struct usb_interface_cache *intfc;
  159. struct usb_host_interface *alt;
  160. int i, n;
  161. int len, retval;
  162. int num_ep, num_ep_orig;
  163. d = (struct usb_interface_descriptor *) buffer;
  164. buffer += d->bLength;
  165. size -= d->bLength;
  166. if (d->bLength < USB_DT_INTERFACE_SIZE)
  167. goto skip_to_next_interface_descriptor;
  168. /* Which interface entry is this? */
  169. intfc = NULL;
  170. inum = d->bInterfaceNumber;
  171. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  172. if (inums[i] == inum) {
  173. intfc = config->intf_cache[i];
  174. break;
  175. }
  176. }
  177. if (!intfc || intfc->num_altsetting >= nalts[i])
  178. goto skip_to_next_interface_descriptor;
  179. /* Check for duplicate altsetting entries */
  180. asnum = d->bAlternateSetting;
  181. for ((i = 0, alt = &intfc->altsetting[0]);
  182. i < intfc->num_altsetting;
  183. (++i, ++alt)) {
  184. if (alt->desc.bAlternateSetting == asnum) {
  185. dev_warn(ddev, "Duplicate descriptor for config %d "
  186. "interface %d altsetting %d, skipping\n",
  187. cfgno, inum, asnum);
  188. goto skip_to_next_interface_descriptor;
  189. }
  190. }
  191. ++intfc->num_altsetting;
  192. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  193. /* Skip over any Class Specific or Vendor Specific descriptors;
  194. * find the first endpoint or interface descriptor */
  195. alt->extra = buffer;
  196. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  197. USB_DT_INTERFACE, &n);
  198. alt->extralen = i;
  199. if (n > 0)
  200. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  201. n, plural(n), "interface");
  202. buffer += i;
  203. size -= i;
  204. /* Allocate space for the right(?) number of endpoints */
  205. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  206. alt->desc.bNumEndpoints = 0; // Use as a counter
  207. if (num_ep > USB_MAXENDPOINTS) {
  208. dev_warn(ddev, "too many endpoints for config %d interface %d "
  209. "altsetting %d: %d, using maximum allowed: %d\n",
  210. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  211. num_ep = USB_MAXENDPOINTS;
  212. }
  213. if (num_ep > 0) { /* Can't allocate 0 bytes */
  214. len = sizeof(struct usb_host_endpoint) * num_ep;
  215. alt->endpoint = kzalloc(len, GFP_KERNEL);
  216. if (!alt->endpoint)
  217. return -ENOMEM;
  218. }
  219. /* Parse all the endpoint descriptors */
  220. n = 0;
  221. while (size > 0) {
  222. if (((struct usb_descriptor_header *) buffer)->bDescriptorType
  223. == USB_DT_INTERFACE)
  224. break;
  225. retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
  226. num_ep, buffer, size);
  227. if (retval < 0)
  228. return retval;
  229. ++n;
  230. buffer += retval;
  231. size -= retval;
  232. }
  233. if (n != num_ep_orig)
  234. dev_warn(ddev, "config %d interface %d altsetting %d has %d "
  235. "endpoint descriptor%s, different from the interface "
  236. "descriptor's value: %d\n",
  237. cfgno, inum, asnum, n, plural(n), num_ep_orig);
  238. return buffer - buffer0;
  239. skip_to_next_interface_descriptor:
  240. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  241. USB_DT_INTERFACE, NULL);
  242. return buffer - buffer0 + i;
  243. }
  244. static int usb_parse_configuration(struct device *ddev, int cfgidx,
  245. struct usb_host_config *config, unsigned char *buffer, int size)
  246. {
  247. unsigned char *buffer0 = buffer;
  248. int cfgno;
  249. int nintf, nintf_orig;
  250. int i, j, n;
  251. struct usb_interface_cache *intfc;
  252. unsigned char *buffer2;
  253. int size2;
  254. struct usb_descriptor_header *header;
  255. int len, retval;
  256. u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
  257. memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
  258. if (config->desc.bDescriptorType != USB_DT_CONFIG ||
  259. config->desc.bLength < USB_DT_CONFIG_SIZE) {
  260. dev_err(ddev, "invalid descriptor for config index %d: "
  261. "type = 0x%X, length = %d\n", cfgidx,
  262. config->desc.bDescriptorType, config->desc.bLength);
  263. return -EINVAL;
  264. }
  265. cfgno = config->desc.bConfigurationValue;
  266. buffer += config->desc.bLength;
  267. size -= config->desc.bLength;
  268. nintf = nintf_orig = config->desc.bNumInterfaces;
  269. if (nintf > USB_MAXINTERFACES) {
  270. dev_warn(ddev, "config %d has too many interfaces: %d, "
  271. "using maximum allowed: %d\n",
  272. cfgno, nintf, USB_MAXINTERFACES);
  273. nintf = USB_MAXINTERFACES;
  274. }
  275. /* Go through the descriptors, checking their length and counting the
  276. * number of altsettings for each interface */
  277. n = 0;
  278. for ((buffer2 = buffer, size2 = size);
  279. size2 > 0;
  280. (buffer2 += header->bLength, size2 -= header->bLength)) {
  281. if (size2 < sizeof(struct usb_descriptor_header)) {
  282. dev_warn(ddev, "config %d descriptor has %d excess "
  283. "byte%s, ignoring\n",
  284. cfgno, size2, plural(size2));
  285. break;
  286. }
  287. header = (struct usb_descriptor_header *) buffer2;
  288. if ((header->bLength > size2) || (header->bLength < 2)) {
  289. dev_warn(ddev, "config %d has an invalid descriptor "
  290. "of length %d, skipping remainder of the config\n",
  291. cfgno, header->bLength);
  292. break;
  293. }
  294. if (header->bDescriptorType == USB_DT_INTERFACE) {
  295. struct usb_interface_descriptor *d;
  296. int inum;
  297. d = (struct usb_interface_descriptor *) header;
  298. if (d->bLength < USB_DT_INTERFACE_SIZE) {
  299. dev_warn(ddev, "config %d has an invalid "
  300. "interface descriptor of length %d, "
  301. "skipping\n", cfgno, d->bLength);
  302. continue;
  303. }
  304. inum = d->bInterfaceNumber;
  305. if (inum >= nintf_orig)
  306. dev_warn(ddev, "config %d has an invalid "
  307. "interface number: %d but max is %d\n",
  308. cfgno, inum, nintf_orig - 1);
  309. /* Have we already encountered this interface?
  310. * Count its altsettings */
  311. for (i = 0; i < n; ++i) {
  312. if (inums[i] == inum)
  313. break;
  314. }
  315. if (i < n) {
  316. if (nalts[i] < 255)
  317. ++nalts[i];
  318. } else if (n < USB_MAXINTERFACES) {
  319. inums[n] = inum;
  320. nalts[n] = 1;
  321. ++n;
  322. }
  323. } else if (header->bDescriptorType == USB_DT_DEVICE ||
  324. header->bDescriptorType == USB_DT_CONFIG)
  325. dev_warn(ddev, "config %d contains an unexpected "
  326. "descriptor of type 0x%X, skipping\n",
  327. cfgno, header->bDescriptorType);
  328. } /* for ((buffer2 = buffer, size2 = size); ...) */
  329. size = buffer2 - buffer;
  330. config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
  331. if (n != nintf)
  332. dev_warn(ddev, "config %d has %d interface%s, different from "
  333. "the descriptor's value: %d\n",
  334. cfgno, n, plural(n), nintf_orig);
  335. else if (n == 0)
  336. dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
  337. config->desc.bNumInterfaces = nintf = n;
  338. /* Check for missing interface numbers */
  339. for (i = 0; i < nintf; ++i) {
  340. for (j = 0; j < nintf; ++j) {
  341. if (inums[j] == i)
  342. break;
  343. }
  344. if (j >= nintf)
  345. dev_warn(ddev, "config %d has no interface number "
  346. "%d\n", cfgno, i);
  347. }
  348. /* Allocate the usb_interface_caches and altsetting arrays */
  349. for (i = 0; i < nintf; ++i) {
  350. j = nalts[i];
  351. if (j > USB_MAXALTSETTING) {
  352. dev_warn(ddev, "too many alternate settings for "
  353. "config %d interface %d: %d, "
  354. "using maximum allowed: %d\n",
  355. cfgno, inums[i], j, USB_MAXALTSETTING);
  356. nalts[i] = j = USB_MAXALTSETTING;
  357. }
  358. len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
  359. config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
  360. if (!intfc)
  361. return -ENOMEM;
  362. kref_init(&intfc->ref);
  363. }
  364. /* Skip over any Class Specific or Vendor Specific descriptors;
  365. * find the first interface descriptor */
  366. config->extra = buffer;
  367. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  368. USB_DT_INTERFACE, &n);
  369. config->extralen = i;
  370. if (n > 0)
  371. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  372. n, plural(n), "configuration");
  373. buffer += i;
  374. size -= i;
  375. /* Parse all the interface/altsetting descriptors */
  376. while (size > 0) {
  377. retval = usb_parse_interface(ddev, cfgno, config,
  378. buffer, size, inums, nalts);
  379. if (retval < 0)
  380. return retval;
  381. buffer += retval;
  382. size -= retval;
  383. }
  384. /* Check for missing altsettings */
  385. for (i = 0; i < nintf; ++i) {
  386. intfc = config->intf_cache[i];
  387. for (j = 0; j < intfc->num_altsetting; ++j) {
  388. for (n = 0; n < intfc->num_altsetting; ++n) {
  389. if (intfc->altsetting[n].desc.
  390. bAlternateSetting == j)
  391. break;
  392. }
  393. if (n >= intfc->num_altsetting)
  394. dev_warn(ddev, "config %d interface %d has no "
  395. "altsetting %d\n", cfgno, inums[i], j);
  396. }
  397. }
  398. return 0;
  399. }
  400. // hub-only!! ... and only exported for reset/reinit path.
  401. // otherwise used internally on disconnect/destroy path
  402. void usb_destroy_configuration(struct usb_device *dev)
  403. {
  404. int c, i;
  405. if (!dev->config)
  406. return;
  407. if (dev->rawdescriptors) {
  408. for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
  409. kfree(dev->rawdescriptors[i]);
  410. kfree(dev->rawdescriptors);
  411. dev->rawdescriptors = NULL;
  412. }
  413. for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
  414. struct usb_host_config *cf = &dev->config[c];
  415. kfree(cf->string);
  416. for (i = 0; i < cf->desc.bNumInterfaces; i++) {
  417. if (cf->intf_cache[i])
  418. kref_put(&cf->intf_cache[i]->ref,
  419. usb_release_interface_cache);
  420. }
  421. }
  422. kfree(dev->config);
  423. dev->config = NULL;
  424. }
  425. // hub-only!! ... and only in reset path, or usb_new_device()
  426. // (used by real hubs and virtual root hubs)
  427. int usb_get_configuration(struct usb_device *dev)
  428. {
  429. struct device *ddev = &dev->dev;
  430. int ncfg = dev->descriptor.bNumConfigurations;
  431. int result = -ENOMEM;
  432. unsigned int cfgno, length;
  433. unsigned char *buffer;
  434. unsigned char *bigbuffer;
  435. struct usb_config_descriptor *desc;
  436. if (ncfg > USB_MAXCONFIG) {
  437. dev_warn(ddev, "too many configurations: %d, "
  438. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  439. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  440. }
  441. if (ncfg < 1) {
  442. dev_err(ddev, "no configurations\n");
  443. return -EINVAL;
  444. }
  445. length = ncfg * sizeof(struct usb_host_config);
  446. dev->config = kzalloc(length, GFP_KERNEL);
  447. if (!dev->config)
  448. goto err2;
  449. length = ncfg * sizeof(char *);
  450. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  451. if (!dev->rawdescriptors)
  452. goto err2;
  453. buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  454. if (!buffer)
  455. goto err2;
  456. desc = (struct usb_config_descriptor *)buffer;
  457. for (cfgno = 0; cfgno < ncfg; cfgno++) {
  458. /* We grab just the first descriptor so we know how long
  459. * the whole configuration is */
  460. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  461. buffer, USB_DT_CONFIG_SIZE);
  462. if (result < 0) {
  463. dev_err(ddev, "unable to read config index %d "
  464. "descriptor/%s\n", cfgno, "start");
  465. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  466. dev->descriptor.bNumConfigurations = cfgno;
  467. break;
  468. } else if (result < 4) {
  469. dev_err(ddev, "config index %d descriptor too short "
  470. "(expected %i, got %i)\n", cfgno,
  471. USB_DT_CONFIG_SIZE, result);
  472. result = -EINVAL;
  473. goto err;
  474. }
  475. length = max((int) le16_to_cpu(desc->wTotalLength),
  476. USB_DT_CONFIG_SIZE);
  477. /* Now that we know the length, get the whole thing */
  478. bigbuffer = kmalloc(length, GFP_KERNEL);
  479. if (!bigbuffer) {
  480. result = -ENOMEM;
  481. goto err;
  482. }
  483. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  484. bigbuffer, length);
  485. if (result < 0) {
  486. dev_err(ddev, "unable to read config index %d "
  487. "descriptor/%s\n", cfgno, "all");
  488. kfree(bigbuffer);
  489. goto err;
  490. }
  491. if (result < length) {
  492. dev_warn(ddev, "config index %d descriptor too short "
  493. "(expected %i, got %i)\n", cfgno, length, result);
  494. length = result;
  495. }
  496. dev->rawdescriptors[cfgno] = bigbuffer;
  497. result = usb_parse_configuration(&dev->dev, cfgno,
  498. &dev->config[cfgno], bigbuffer, length);
  499. if (result < 0) {
  500. ++cfgno;
  501. goto err;
  502. }
  503. }
  504. result = 0;
  505. err:
  506. kfree(buffer);
  507. dev->descriptor.bNumConfigurations = cfgno;
  508. err2:
  509. if (result == -ENOMEM)
  510. dev_err(ddev, "out of memory\n");
  511. return result;
  512. }