config.c 15 KB

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