config.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include <linux/config.h>
  2. #ifdef CONFIG_USB_DEBUG
  3. #define DEBUG
  4. #endif
  5. #include <linux/usb.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/device.h>
  10. #include <asm/byteorder.h>
  11. #include "usb.h"
  12. #include "hcd.h"
  13. #define USB_MAXALTSETTING 128 /* Hard limit */
  14. #define USB_MAXENDPOINTS 30 /* Hard limit */
  15. #define USB_MAXCONFIG 8 /* Arbitrary limit */
  16. static inline const char *plural(int n)
  17. {
  18. return (n == 1 ? "" : "s");
  19. }
  20. static int find_next_descriptor(unsigned char *buffer, int size,
  21. int dt1, int dt2, int *num_skipped)
  22. {
  23. struct usb_descriptor_header *h;
  24. int n = 0;
  25. unsigned char *buffer0 = buffer;
  26. /* Find the next descriptor of type dt1 or dt2 */
  27. while (size > 0) {
  28. h = (struct usb_descriptor_header *) buffer;
  29. if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
  30. break;
  31. buffer += h->bLength;
  32. size -= h->bLength;
  33. ++n;
  34. }
  35. /* Store the number of descriptors skipped and return the
  36. * number of bytes skipped */
  37. if (num_skipped)
  38. *num_skipped = n;
  39. return buffer - buffer0;
  40. }
  41. static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
  42. int asnum, struct usb_host_interface *ifp, int num_ep,
  43. unsigned char *buffer, int size)
  44. {
  45. unsigned char *buffer0 = buffer;
  46. struct usb_endpoint_descriptor *d;
  47. struct usb_host_endpoint *endpoint;
  48. int n, i;
  49. d = (struct usb_endpoint_descriptor *) buffer;
  50. buffer += d->bLength;
  51. size -= d->bLength;
  52. if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
  53. n = USB_DT_ENDPOINT_AUDIO_SIZE;
  54. else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
  55. n = USB_DT_ENDPOINT_SIZE;
  56. else {
  57. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  58. "invalid endpoint descriptor of length %d, skipping\n",
  59. cfgno, inum, asnum, d->bLength);
  60. goto skip_to_next_endpoint_or_interface_descriptor;
  61. }
  62. i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
  63. if (i >= 16 || i == 0) {
  64. dev_warn(ddev, "config %d interface %d altsetting %d has an "
  65. "invalid endpoint with address 0x%X, skipping\n",
  66. cfgno, inum, asnum, d->bEndpointAddress);
  67. goto skip_to_next_endpoint_or_interface_descriptor;
  68. }
  69. /* Only store as many endpoints as we have room for */
  70. if (ifp->desc.bNumEndpoints >= num_ep)
  71. goto skip_to_next_endpoint_or_interface_descriptor;
  72. endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
  73. ++ifp->desc.bNumEndpoints;
  74. memcpy(&endpoint->desc, d, n);
  75. INIT_LIST_HEAD(&endpoint->urb_list);
  76. /* Skip over any Class Specific or Vendor Specific descriptors;
  77. * find the next endpoint or interface descriptor */
  78. endpoint->extra = buffer;
  79. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  80. USB_DT_INTERFACE, &n);
  81. endpoint->extralen = i;
  82. if (n > 0)
  83. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  84. n, plural(n), "endpoint");
  85. return buffer - buffer0 + i;
  86. skip_to_next_endpoint_or_interface_descriptor:
  87. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  88. USB_DT_INTERFACE, NULL);
  89. return buffer - buffer0 + i;
  90. }
  91. void usb_release_interface_cache(struct kref *ref)
  92. {
  93. struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
  94. int j;
  95. for (j = 0; j < intfc->num_altsetting; j++)
  96. kfree(intfc->altsetting[j].endpoint);
  97. kfree(intfc);
  98. }
  99. static int usb_parse_interface(struct device *ddev, int cfgno,
  100. struct usb_host_config *config, unsigned char *buffer, int size,
  101. u8 inums[], u8 nalts[])
  102. {
  103. unsigned char *buffer0 = buffer;
  104. struct usb_interface_descriptor *d;
  105. int inum, asnum;
  106. struct usb_interface_cache *intfc;
  107. struct usb_host_interface *alt;
  108. int i, n;
  109. int len, retval;
  110. int num_ep, num_ep_orig;
  111. d = (struct usb_interface_descriptor *) buffer;
  112. buffer += d->bLength;
  113. size -= d->bLength;
  114. if (d->bLength < USB_DT_INTERFACE_SIZE)
  115. goto skip_to_next_interface_descriptor;
  116. /* Which interface entry is this? */
  117. intfc = NULL;
  118. inum = d->bInterfaceNumber;
  119. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  120. if (inums[i] == inum) {
  121. intfc = config->intf_cache[i];
  122. break;
  123. }
  124. }
  125. if (!intfc || intfc->num_altsetting >= nalts[i])
  126. goto skip_to_next_interface_descriptor;
  127. /* Check for duplicate altsetting entries */
  128. asnum = d->bAlternateSetting;
  129. for ((i = 0, alt = &intfc->altsetting[0]);
  130. i < intfc->num_altsetting;
  131. (++i, ++alt)) {
  132. if (alt->desc.bAlternateSetting == asnum) {
  133. dev_warn(ddev, "Duplicate descriptor for config %d "
  134. "interface %d altsetting %d, skipping\n",
  135. cfgno, inum, asnum);
  136. goto skip_to_next_interface_descriptor;
  137. }
  138. }
  139. ++intfc->num_altsetting;
  140. memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
  141. /* Skip over any Class Specific or Vendor Specific descriptors;
  142. * find the first endpoint or interface descriptor */
  143. alt->extra = buffer;
  144. i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
  145. USB_DT_INTERFACE, &n);
  146. alt->extralen = i;
  147. if (n > 0)
  148. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  149. n, plural(n), "interface");
  150. buffer += i;
  151. size -= i;
  152. /* Allocate space for the right(?) number of endpoints */
  153. num_ep = num_ep_orig = alt->desc.bNumEndpoints;
  154. alt->desc.bNumEndpoints = 0; // Use as a counter
  155. if (num_ep > USB_MAXENDPOINTS) {
  156. dev_warn(ddev, "too many endpoints for config %d interface %d "
  157. "altsetting %d: %d, using maximum allowed: %d\n",
  158. cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
  159. num_ep = USB_MAXENDPOINTS;
  160. }
  161. len = sizeof(struct usb_host_endpoint) * num_ep;
  162. alt->endpoint = kmalloc(len, GFP_KERNEL);
  163. if (!alt->endpoint)
  164. return -ENOMEM;
  165. memset(alt->endpoint, 0, len);
  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 = kmalloc(len, GFP_KERNEL);
  307. if (!intfc)
  308. return -ENOMEM;
  309. memset(intfc, 0, len);
  310. kref_init(&intfc->ref);
  311. }
  312. /* Skip over any Class Specific or Vendor Specific descriptors;
  313. * find the first interface descriptor */
  314. config->extra = buffer;
  315. i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
  316. USB_DT_INTERFACE, &n);
  317. config->extralen = i;
  318. if (n > 0)
  319. dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
  320. n, plural(n), "configuration");
  321. buffer += i;
  322. size -= i;
  323. /* Parse all the interface/altsetting descriptors */
  324. while (size > 0) {
  325. retval = usb_parse_interface(ddev, cfgno, config,
  326. buffer, size, inums, nalts);
  327. if (retval < 0)
  328. return retval;
  329. buffer += retval;
  330. size -= retval;
  331. }
  332. /* Check for missing altsettings */
  333. for (i = 0; i < nintf; ++i) {
  334. intfc = config->intf_cache[i];
  335. for (j = 0; j < intfc->num_altsetting; ++j) {
  336. for (n = 0; n < intfc->num_altsetting; ++n) {
  337. if (intfc->altsetting[n].desc.
  338. bAlternateSetting == j)
  339. break;
  340. }
  341. if (n >= intfc->num_altsetting)
  342. dev_warn(ddev, "config %d interface %d has no "
  343. "altsetting %d\n", cfgno, inums[i], j);
  344. }
  345. }
  346. return 0;
  347. }
  348. // hub-only!! ... and only exported for reset/reinit path.
  349. // otherwise used internally on disconnect/destroy path
  350. void usb_destroy_configuration(struct usb_device *dev)
  351. {
  352. int c, i;
  353. if (!dev->config)
  354. return;
  355. if (dev->rawdescriptors) {
  356. for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
  357. kfree(dev->rawdescriptors[i]);
  358. kfree(dev->rawdescriptors);
  359. dev->rawdescriptors = NULL;
  360. }
  361. for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
  362. struct usb_host_config *cf = &dev->config[c];
  363. kfree(cf->string);
  364. cf->string = NULL;
  365. for (i = 0; i < cf->desc.bNumInterfaces; i++) {
  366. if (cf->intf_cache[i])
  367. kref_put(&cf->intf_cache[i]->ref,
  368. usb_release_interface_cache);
  369. }
  370. }
  371. kfree(dev->config);
  372. dev->config = NULL;
  373. }
  374. // hub-only!! ... and only in reset path, or usb_new_device()
  375. // (used by real hubs and virtual root hubs)
  376. int usb_get_configuration(struct usb_device *dev)
  377. {
  378. struct device *ddev = &dev->dev;
  379. int ncfg = dev->descriptor.bNumConfigurations;
  380. int result = -ENOMEM;
  381. unsigned int cfgno, length;
  382. unsigned char *buffer;
  383. unsigned char *bigbuffer;
  384. struct usb_config_descriptor *desc;
  385. if (ncfg > USB_MAXCONFIG) {
  386. dev_warn(ddev, "too many configurations: %d, "
  387. "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
  388. dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
  389. }
  390. if (ncfg < 1) {
  391. dev_err(ddev, "no configurations\n");
  392. return -EINVAL;
  393. }
  394. length = ncfg * sizeof(struct usb_host_config);
  395. dev->config = kmalloc(length, GFP_KERNEL);
  396. if (!dev->config)
  397. goto err2;
  398. memset(dev->config, 0, length);
  399. length = ncfg * sizeof(char *);
  400. dev->rawdescriptors = kmalloc(length, GFP_KERNEL);
  401. if (!dev->rawdescriptors)
  402. goto err2;
  403. memset(dev->rawdescriptors, 0, length);
  404. buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
  405. if (!buffer)
  406. goto err2;
  407. desc = (struct usb_config_descriptor *)buffer;
  408. for (cfgno = 0; cfgno < ncfg; cfgno++) {
  409. /* We grab just the first descriptor so we know how long
  410. * the whole configuration is */
  411. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  412. buffer, USB_DT_CONFIG_SIZE);
  413. if (result < 0) {
  414. dev_err(ddev, "unable to read config index %d "
  415. "descriptor/%s\n", cfgno, "start");
  416. goto err;
  417. } else if (result < 4) {
  418. dev_err(ddev, "config index %d descriptor too short "
  419. "(expected %i, got %i)\n", cfgno,
  420. USB_DT_CONFIG_SIZE, result);
  421. result = -EINVAL;
  422. goto err;
  423. }
  424. length = max((int) le16_to_cpu(desc->wTotalLength),
  425. USB_DT_CONFIG_SIZE);
  426. /* Now that we know the length, get the whole thing */
  427. bigbuffer = kmalloc(length, GFP_KERNEL);
  428. if (!bigbuffer) {
  429. result = -ENOMEM;
  430. goto err;
  431. }
  432. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
  433. bigbuffer, length);
  434. if (result < 0) {
  435. dev_err(ddev, "unable to read config index %d "
  436. "descriptor/%s\n", cfgno, "all");
  437. kfree(bigbuffer);
  438. goto err;
  439. }
  440. if (result < length) {
  441. dev_warn(ddev, "config index %d descriptor too short "
  442. "(expected %i, got %i)\n", cfgno, length, result);
  443. length = result;
  444. }
  445. dev->rawdescriptors[cfgno] = bigbuffer;
  446. result = usb_parse_configuration(&dev->dev, cfgno,
  447. &dev->config[cfgno], bigbuffer, length);
  448. if (result < 0) {
  449. ++cfgno;
  450. goto err;
  451. }
  452. }
  453. result = 0;
  454. err:
  455. kfree(buffer);
  456. dev->descriptor.bNumConfigurations = cfgno;
  457. err2:
  458. if (result == -ENOMEM)
  459. dev_err(ddev, "out of memory\n");
  460. return result;
  461. }