config.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. /* Skip over any Class Specific or Vendor Specific descriptors;
  112. * find the next endpoint or interface descriptor */
  113. endpoint->extra = buffer;
  114. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  115. USB_DT_INTERFACE, &n);
  116. endpoint->extralen = i;
  117. if (n > 0)
  118. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  119. n, plural(n), "endpoint");
  120. return buffer - buffer0 + i;
  121. skip_to_next_endpoint_or_interface_descriptor:
  122. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  123. USB_DT_INTERFACE, NULL);
  124. return buffer - buffer0 + i;
  125. }
  126. void usb_release_interface_cache(struct kref *ref)
  127. {
  128. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  129. int j;
  130. for (j = 0; j < intfc->num_altsetting; j++) {
  131. struct usb_host_interface *alt = &intfc->altsetting[j];
  132. kfree(alt->endpoint);
  133. kfree(alt->string);
  134. }
  135. kfree(intfc);
  136. }
  137. static int usb_parse_interface(struct device *ddev, int cfgno,
  138. struct usb_host_config *config, unsigned char *buffer, int size,
  139. u8 inums[], u8 nalts[])
  140. {
  141. unsigned char *buffer0 = buffer;
  142. struct usb_interface_descriptor *d;
  143. int inum, asnum;
  144. struct usb_interface_cache *intfc;
  145. struct usb_host_interface *alt;
  146. int i, n;
  147. int len, retval;
  148. int num_ep, num_ep_orig;
  149. d = (struct usb_interface_descriptor *) buffer;
  150. buffer += d->bLength;
  151. size -= d->bLength;
  152. if (d->bLength < USB_DT_INTERFACE_SIZE)
  153. goto skip_to_next_interface_descriptor;
  154. /* Which interface entry is this? */
  155. intfc = NULL;
  156. inum = d->bInterfaceNumber;
  157. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  158. if (inums[i] == inum) {
  159. intfc = config->intf_cache[i];
  160. break;
  161. }
  162. }
  163. if (!intfc || intfc->num_altsetting >= nalts[i])
  164. goto skip_to_next_interface_descriptor;
  165. /* Check for duplicate altsetting entries */
  166. asnum = d->bAlternateSetting;
  167. for ((i = 0, alt = &intfc->altsetting[0]);
  168. i < intfc->num_altsetting;
  169. (++i, ++alt)) {
  170. if (alt->desc.bAlternateSetting == asnum) {
  171. dev_warn(ddev, "Duplicate descriptor for config %d "
  172. "interface %d altsetting %d, skipping\n",
  173. cfgno, inum, asnum);
  174. goto skip_to_next_interface_descriptor;
  175. }
  176. }
  177. ++intfc->num_altsetting;
  178. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  179. /* Skip over any Class Specific or Vendor Specific descriptors;
  180. * find the first endpoint or interface descriptor */
  181. alt->extra = buffer;
  182. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  183. USB_DT_INTERFACE, &n);
  184. alt->extralen = i;
  185. if (n > 0)
  186. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  187. n, plural(n), "interface");
  188. buffer += i;
  189. size -= i;
  190. /* Allocate space for the right(?) number of endpoints */
  191. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  192. alt->desc.bNumEndpoints = 0; // Use as a counter
  193. if (num_ep > USB_MAXENDPOINTS) {
  194. dev_warn(ddev, "too many endpoints for config %d interface %d "
  195. "altsetting %d: %d, using maximum allowed: %d\n",
  196. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  197. num_ep = USB_MAXENDPOINTS;
  198. }
  199. if (num_ep > 0) { /* Can't allocate 0 bytes */
  200. len = sizeof(struct usb_host_endpoint) * num_ep;
  201. alt->endpoint = kzalloc(len, GFP_KERNEL);
  202. if (!alt->endpoint)
  203. return -ENOMEM;
  204. }
  205. /* Parse all the endpoint descriptors */
  206. n = 0;
  207. while (size > 0) {
  208. if (((struct usb_descriptor_header *) buffer)->bDescriptorType
  209. == USB_DT_INTERFACE)
  210. break;
  211. retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
  212. num_ep, buffer, size);
  213. if (retval < 0)
  214. return retval;
  215. ++n;
  216. buffer += retval;
  217. size -= retval;
  218. }
  219. if (n != num_ep_orig)
  220. dev_warn(ddev, "config %d interface %d altsetting %d has %d "
  221. "endpoint descriptor%s, different from the interface "
  222. "descriptor's value: %d\n",
  223. cfgno, inum, asnum, n, plural(n), num_ep_orig);
  224. return buffer - buffer0;
  225. skip_to_next_interface_descriptor:
  226. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  227. USB_DT_INTERFACE, NULL);
  228. return buffer - buffer0 + i;
  229. }
  230. static int usb_parse_configuration(struct device *ddev, int cfgidx,
  231. struct usb_host_config *config, unsigned char *buffer, int size)
  232. {
  233. unsigned char *buffer0 = buffer;
  234. int cfgno;
  235. int nintf, nintf_orig;
  236. int i, j, n;
  237. struct usb_interface_cache *intfc;
  238. unsigned char *buffer2;
  239. int size2;
  240. struct usb_descriptor_header *header;
  241. int len, retval;
  242. u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
  243. memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
  244. if (config->desc.bDescriptorType != USB_DT_CONFIG ||
  245. config->desc.bLength < USB_DT_CONFIG_SIZE) {
  246. dev_err(ddev, "invalid descriptor for config index %d: "
  247. "type = 0x%X, length = %d\n", cfgidx,
  248. config->desc.bDescriptorType, config->desc.bLength);
  249. return -EINVAL;
  250. }
  251. cfgno = config->desc.bConfigurationValue;
  252. buffer += config->desc.bLength;
  253. size -= config->desc.bLength;
  254. nintf = nintf_orig = config->desc.bNumInterfaces;
  255. if (nintf > USB_MAXINTERFACES) {
  256. dev_warn(ddev, "config %d has too many interfaces: %d, "
  257. "using maximum allowed: %d\n",
  258. cfgno, nintf, USB_MAXINTERFACES);
  259. nintf = USB_MAXINTERFACES;
  260. }
  261. /* Go through the descriptors, checking their length and counting the
  262. * number of altsettings for each interface */
  263. n = 0;
  264. for ((buffer2 = buffer, size2 = size);
  265. size2 > 0;
  266. (buffer2 += header->bLength, size2 -= header->bLength)) {
  267. if (size2 < sizeof(struct usb_descriptor_header)) {
  268. dev_warn(ddev, "config %d descriptor has %d excess "
  269. "byte%s, ignoring\n",
  270. cfgno, size2, plural(size2));
  271. break;
  272. }
  273. header = (struct usb_descriptor_header *) buffer2;
  274. if ((header->bLength > size2) || (header->bLength < 2)) {
  275. dev_warn(ddev, "config %d has an invalid descriptor "
  276. "of length %d, skipping remainder of the config\n",
  277. cfgno, header->bLength);
  278. break;
  279. }
  280. if (header->bDescriptorType == USB_DT_INTERFACE) {
  281. struct usb_interface_descriptor *d;
  282. int inum;
  283. d = (struct usb_interface_descriptor *) header;
  284. if (d->bLength < USB_DT_INTERFACE_SIZE) {
  285. dev_warn(ddev, "config %d has an invalid "
  286. "interface descriptor of length %d, "
  287. "skipping\n", cfgno, d->bLength);
  288. continue;
  289. }
  290. inum = d->bInterfaceNumber;
  291. if (inum >= nintf_orig)
  292. dev_warn(ddev, "config %d has an invalid "
  293. "interface number: %d but max is %d\n",
  294. cfgno, inum, nintf_orig - 1);
  295. /* Have we already encountered this interface?
  296. * Count its altsettings */
  297. for (i = 0; i < n; ++i) {
  298. if (inums[i] == inum)
  299. break;
  300. }
  301. if (i < n) {
  302. if (nalts[i] < 255)
  303. ++nalts[i];
  304. } else if (n < USB_MAXINTERFACES) {
  305. inums[n] = inum;
  306. nalts[n] = 1;
  307. ++n;
  308. }
  309. } else if (header->bDescriptorType == USB_DT_DEVICE ||
  310. header->bDescriptorType == USB_DT_CONFIG)
  311. dev_warn(ddev, "config %d contains an unexpected "
  312. "descriptor of type 0x%X, skipping\n",
  313. cfgno, header->bDescriptorType);
  314. } /* for ((buffer2 = buffer, size2 = size); ...) */
  315. size = buffer2 - buffer;
  316. config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
  317. if (n != nintf)
  318. dev_warn(ddev, "config %d has %d interface%s, different from "
  319. "the descriptor's value: %d\n",
  320. cfgno, n, plural(n), nintf_orig);
  321. else if (n == 0)
  322. dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
  323. config->desc.bNumInterfaces = nintf = n;
  324. /* Check for missing interface numbers */
  325. for (i = 0; i < nintf; ++i) {
  326. for (j = 0; j < nintf; ++j) {
  327. if (inums[j] == i)
  328. break;
  329. }
  330. if (j >= nintf)
  331. dev_warn(ddev, "config %d has no interface number "
  332. "%d\n", cfgno, i);
  333. }
  334. /* Allocate the usb_interface_caches and altsetting arrays */
  335. for (i = 0; i < nintf; ++i) {
  336. j = nalts[i];
  337. if (j > USB_MAXALTSETTING) {
  338. dev_warn(ddev, "too many alternate settings for "
  339. "config %d interface %d: %d, "
  340. "using maximum allowed: %d\n",
  341. cfgno, inums[i], j, USB_MAXALTSETTING);
  342. nalts[i] = j = USB_MAXALTSETTING;
  343. }
  344. len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
  345. config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
  346. if (!intfc)
  347. return -ENOMEM;
  348. kref_init(&intfc->ref);
  349. }
  350. /* Skip over any Class Specific or Vendor Specific descriptors;
  351. * find the first interface descriptor */
  352. config->extra = buffer;
  353. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  354. USB_DT_INTERFACE, &n);
  355. config->extralen = i;
  356. if (n > 0)
  357. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  358. n, plural(n), "configuration");
  359. buffer += i;
  360. size -= i;
  361. /* Parse all the interface/altsetting descriptors */
  362. while (size > 0) {
  363. retval = usb_parse_interface(ddev, cfgno, config,
  364. buffer, size, inums, nalts);
  365. if (retval < 0)
  366. return retval;
  367. buffer += retval;
  368. size -= retval;
  369. }
  370. /* Check for missing altsettings */
  371. for (i = 0; i < nintf; ++i) {
  372. intfc = config->intf_cache[i];
  373. for (j = 0; j < intfc->num_altsetting; ++j) {
  374. for (n = 0; n < intfc->num_altsetting; ++n) {
  375. if (intfc->altsetting[n].desc.
  376. bAlternateSetting == j)
  377. break;
  378. }
  379. if (n >= intfc->num_altsetting)
  380. dev_warn(ddev, "config %d interface %d has no "
  381. "altsetting %d\n", cfgno, inums[i], j);
  382. }
  383. }
  384. return 0;
  385. }
  386. // hub-only!! ... and only exported for reset/reinit path.
  387. // otherwise used internally on disconnect/destroy path
  388. void usb_destroy_configuration(struct usb_device *dev)
  389. {
  390. int c, i;
  391. if (!dev->config)
  392. return;
  393. if (dev->rawdescriptors) {
  394. for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
  395. kfree(dev->rawdescriptors[i]);
  396. kfree(dev->rawdescriptors);
  397. dev->rawdescriptors = NULL;
  398. }
  399. for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
  400. struct usb_host_config *cf = &dev->config[c];
  401. kfree(cf->string);
  402. for (i = 0; i < cf->desc.bNumInterfaces; i++) {
  403. if (cf->intf_cache[i])
  404. kref_put(&cf->intf_cache[i]->ref,
  405. usb_release_interface_cache);
  406. }
  407. }
  408. kfree(dev->config);
  409. dev->config = NULL;
  410. }
  411. // hub-only!! ... and only in reset path, or usb_new_device()
  412. // (used by real hubs and virtual root hubs)
  413. int usb_get_configuration(struct usb_device *dev)
  414. {
  415. struct device *ddev = &dev->dev;
  416. int ncfg = dev->descriptor.bNumConfigurations;
  417. int result = -ENOMEM;
  418. unsigned int cfgno, length;
  419. unsigned char *buffer;
  420. unsigned char *bigbuffer;
  421. struct usb_config_descriptor *desc;
  422. if (ncfg > USB_MAXCONFIG) {
  423. dev_warn(ddev, "too many configurations: %d, "
  424. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  425. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  426. }
  427. if (ncfg < 1) {
  428. dev_err(ddev, "no configurations\n");
  429. return -EINVAL;
  430. }
  431. length = ncfg * sizeof(struct usb_host_config);
  432. dev->config = kzalloc(length, GFP_KERNEL);
  433. if (!dev->config)
  434. goto err2;
  435. length = ncfg * sizeof(char *);
  436. dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
  437. if (!dev->rawdescriptors)
  438. goto err2;
  439. buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  440. if (!buffer)
  441. goto err2;
  442. desc = (struct usb_config_descriptor *)buffer;
  443. for (cfgno = 0; cfgno < ncfg; cfgno++) {
  444. /* We grab just the first descriptor so we know how long
  445. * the whole configuration is */
  446. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  447. buffer, USB_DT_CONFIG_SIZE);
  448. if (result < 0) {
  449. dev_err(ddev, "unable to read config index %d "
  450. "descriptor/%s\n", cfgno, "start");
  451. dev_err(ddev, "chopping to %d config(s)\n", cfgno);
  452. dev->descriptor.bNumConfigurations = cfgno;
  453. break;
  454. } else if (result < 4) {
  455. dev_err(ddev, "config index %d descriptor too short "
  456. "(expected %i, got %i)\n", cfgno,
  457. USB_DT_CONFIG_SIZE, result);
  458. result = -EINVAL;
  459. goto err;
  460. }
  461. length = max((int) le16_to_cpu(desc->wTotalLength),
  462. USB_DT_CONFIG_SIZE);
  463. /* Now that we know the length, get the whole thing */
  464. bigbuffer = kmalloc(length, GFP_KERNEL);
  465. if (!bigbuffer) {
  466. result = -ENOMEM;
  467. goto err;
  468. }
  469. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  470. bigbuffer, length);
  471. if (result < 0) {
  472. dev_err(ddev, "unable to read config index %d "
  473. "descriptor/%s\n", cfgno, "all");
  474. kfree(bigbuffer);
  475. goto err;
  476. }
  477. if (result < length) {
  478. dev_warn(ddev, "config index %d descriptor too short "
  479. "(expected %i, got %i)\n", cfgno, length, result);
  480. length = result;
  481. }
  482. dev->rawdescriptors[cfgno] = bigbuffer;
  483. result = usb_parse_configuration(&dev->dev, cfgno,
  484. &dev->config[cfgno], bigbuffer, length);
  485. if (result < 0) {
  486. ++cfgno;
  487. goto err;
  488. }
  489. }
  490. result = 0;
  491. err:
  492. kfree(buffer);
  493. dev->descriptor.bNumConfigurations = cfgno;
  494. err2:
  495. if (result == -ENOMEM)
  496. dev_err(ddev, "out of memory\n");
  497. return result;
  498. }