config.c 14 KB

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