config.c 15 KB

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