composite.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * composite.c - infrastructure for Composite USB Gadgets
  3. *
  4. * Copyright (C) 2006-2008 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* #define VERBOSE_DEBUG */
  21. #include <linux/kallsyms.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/device.h>
  25. #include <linux/usb/composite.h>
  26. /*
  27. * The code in this file is utility code, used to build a gadget driver
  28. * from one or more "function" drivers, one or more "configuration"
  29. * objects, and a "usb_composite_driver" by gluing them together along
  30. * with the relevant device-wide data.
  31. */
  32. /* big enough to hold our biggest descriptor */
  33. #define USB_BUFSIZ 512
  34. static struct usb_composite_driver *composite;
  35. /* Some systems will need runtime overrides for the product identifers
  36. * published in the device descriptor, either numbers or strings or both.
  37. * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  38. */
  39. static ushort idVendor;
  40. module_param(idVendor, ushort, 0);
  41. MODULE_PARM_DESC(idVendor, "USB Vendor ID");
  42. static ushort idProduct;
  43. module_param(idProduct, ushort, 0);
  44. MODULE_PARM_DESC(idProduct, "USB Product ID");
  45. static ushort bcdDevice;
  46. module_param(bcdDevice, ushort, 0);
  47. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
  48. static char *iManufacturer;
  49. module_param(iManufacturer, charp, 0);
  50. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
  51. static char *iProduct;
  52. module_param(iProduct, charp, 0);
  53. MODULE_PARM_DESC(iProduct, "USB Product string");
  54. static char *iSerialNumber;
  55. module_param(iSerialNumber, charp, 0);
  56. MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
  57. /*-------------------------------------------------------------------------*/
  58. /**
  59. * usb_add_function() - add a function to a configuration
  60. * @config: the configuration
  61. * @function: the function being added
  62. * Context: single threaded during gadget setup
  63. *
  64. * After initialization, each configuration must have one or more
  65. * functions added to it. Adding a function involves calling its @bind()
  66. * method to allocate resources such as interface and string identifiers
  67. * and endpoints.
  68. *
  69. * This function returns the value of the function's bind(), which is
  70. * zero for success else a negative errno value.
  71. */
  72. int __init usb_add_function(struct usb_configuration *config,
  73. struct usb_function *function)
  74. {
  75. int value = -EINVAL;
  76. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  77. function->name, function,
  78. config->label, config);
  79. if (!function->set_alt || !function->disable)
  80. goto done;
  81. function->config = config;
  82. list_add_tail(&function->list, &config->functions);
  83. /* REVISIT *require* function->bind? */
  84. if (function->bind) {
  85. value = function->bind(config, function);
  86. if (value < 0) {
  87. list_del(&function->list);
  88. function->config = NULL;
  89. }
  90. } else
  91. value = 0;
  92. /* We allow configurations that don't work at both speeds.
  93. * If we run into a lowspeed Linux system, treat it the same
  94. * as full speed ... it's the function drivers that will need
  95. * to avoid bulk and ISO transfers.
  96. */
  97. if (!config->fullspeed && function->descriptors)
  98. config->fullspeed = true;
  99. if (!config->highspeed && function->hs_descriptors)
  100. config->highspeed = true;
  101. done:
  102. if (value)
  103. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  104. function->name, function, value);
  105. return value;
  106. }
  107. /**
  108. * usb_function_deactivate - prevent function and gadget enumeration
  109. * @function: the function that isn't yet ready to respond
  110. *
  111. * Blocks response of the gadget driver to host enumeration by
  112. * preventing the data line pullup from being activated. This is
  113. * normally called during @bind() processing to change from the
  114. * initial "ready to respond" state, or when a required resource
  115. * becomes available.
  116. *
  117. * For example, drivers that serve as a passthrough to a userspace
  118. * daemon can block enumeration unless that daemon (such as an OBEX,
  119. * MTP, or print server) is ready to handle host requests.
  120. *
  121. * Not all systems support software control of their USB peripheral
  122. * data pullups.
  123. *
  124. * Returns zero on success, else negative errno.
  125. */
  126. int usb_function_deactivate(struct usb_function *function)
  127. {
  128. struct usb_composite_dev *cdev = function->config->cdev;
  129. int status = 0;
  130. spin_lock(&cdev->lock);
  131. if (cdev->deactivations == 0)
  132. status = usb_gadget_disconnect(cdev->gadget);
  133. if (status == 0)
  134. cdev->deactivations++;
  135. spin_unlock(&cdev->lock);
  136. return status;
  137. }
  138. /**
  139. * usb_function_activate - allow function and gadget enumeration
  140. * @function: function on which usb_function_activate() was called
  141. *
  142. * Reverses effect of usb_function_deactivate(). If no more functions
  143. * are delaying their activation, the gadget driver will respond to
  144. * host enumeration procedures.
  145. *
  146. * Returns zero on success, else negative errno.
  147. */
  148. int usb_function_activate(struct usb_function *function)
  149. {
  150. struct usb_composite_dev *cdev = function->config->cdev;
  151. int status = 0;
  152. spin_lock(&cdev->lock);
  153. if (WARN_ON(cdev->deactivations == 0))
  154. status = -EINVAL;
  155. else {
  156. cdev->deactivations--;
  157. if (cdev->deactivations == 0)
  158. status = usb_gadget_connect(cdev->gadget);
  159. }
  160. spin_unlock(&cdev->lock);
  161. return status;
  162. }
  163. /**
  164. * usb_interface_id() - allocate an unused interface ID
  165. * @config: configuration associated with the interface
  166. * @function: function handling the interface
  167. * Context: single threaded during gadget setup
  168. *
  169. * usb_interface_id() is called from usb_function.bind() callbacks to
  170. * allocate new interface IDs. The function driver will then store that
  171. * ID in interface, association, CDC union, and other descriptors. It
  172. * will also handle any control requests targetted at that interface,
  173. * particularly changing its altsetting via set_alt(). There may
  174. * also be class-specific or vendor-specific requests to handle.
  175. *
  176. * All interface identifier should be allocated using this routine, to
  177. * ensure that for example different functions don't wrongly assign
  178. * different meanings to the same identifier. Note that since interface
  179. * identifers are configuration-specific, functions used in more than
  180. * one configuration (or more than once in a given configuration) need
  181. * multiple versions of the relevant descriptors.
  182. *
  183. * Returns the interface ID which was allocated; or -ENODEV if no
  184. * more interface IDs can be allocated.
  185. */
  186. int __init usb_interface_id(struct usb_configuration *config,
  187. struct usb_function *function)
  188. {
  189. unsigned id = config->next_interface_id;
  190. if (id < MAX_CONFIG_INTERFACES) {
  191. config->interface[id] = function;
  192. config->next_interface_id = id + 1;
  193. return id;
  194. }
  195. return -ENODEV;
  196. }
  197. static int config_buf(struct usb_configuration *config,
  198. enum usb_device_speed speed, void *buf, u8 type)
  199. {
  200. struct usb_config_descriptor *c = buf;
  201. void *next = buf + USB_DT_CONFIG_SIZE;
  202. int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
  203. struct usb_function *f;
  204. int status;
  205. /* write the config descriptor */
  206. c = buf;
  207. c->bLength = USB_DT_CONFIG_SIZE;
  208. c->bDescriptorType = type;
  209. /* wTotalLength is written later */
  210. c->bNumInterfaces = config->next_interface_id;
  211. c->bConfigurationValue = config->bConfigurationValue;
  212. c->iConfiguration = config->iConfiguration;
  213. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  214. c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
  215. /* There may be e.g. OTG descriptors */
  216. if (config->descriptors) {
  217. status = usb_descriptor_fillbuf(next, len,
  218. config->descriptors);
  219. if (status < 0)
  220. return status;
  221. len -= status;
  222. next += status;
  223. }
  224. /* add each function's descriptors */
  225. list_for_each_entry(f, &config->functions, list) {
  226. struct usb_descriptor_header **descriptors;
  227. if (speed == USB_SPEED_HIGH)
  228. descriptors = f->hs_descriptors;
  229. else
  230. descriptors = f->descriptors;
  231. if (!descriptors)
  232. continue;
  233. status = usb_descriptor_fillbuf(next, len,
  234. (const struct usb_descriptor_header **) descriptors);
  235. if (status < 0)
  236. return status;
  237. len -= status;
  238. next += status;
  239. }
  240. len = next - buf;
  241. c->wTotalLength = cpu_to_le16(len);
  242. return len;
  243. }
  244. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  245. {
  246. struct usb_gadget *gadget = cdev->gadget;
  247. struct usb_configuration *c;
  248. u8 type = w_value >> 8;
  249. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  250. if (gadget_is_dualspeed(gadget)) {
  251. int hs = 0;
  252. if (gadget->speed == USB_SPEED_HIGH)
  253. hs = 1;
  254. if (type == USB_DT_OTHER_SPEED_CONFIG)
  255. hs = !hs;
  256. if (hs)
  257. speed = USB_SPEED_HIGH;
  258. }
  259. /* This is a lookup by config *INDEX* */
  260. w_value &= 0xff;
  261. list_for_each_entry(c, &cdev->configs, list) {
  262. /* ignore configs that won't work at this speed */
  263. if (speed == USB_SPEED_HIGH) {
  264. if (!c->highspeed)
  265. continue;
  266. } else {
  267. if (!c->fullspeed)
  268. continue;
  269. }
  270. if (w_value == 0)
  271. return config_buf(c, speed, cdev->req->buf, type);
  272. w_value--;
  273. }
  274. return -EINVAL;
  275. }
  276. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  277. {
  278. struct usb_gadget *gadget = cdev->gadget;
  279. struct usb_configuration *c;
  280. unsigned count = 0;
  281. int hs = 0;
  282. if (gadget_is_dualspeed(gadget)) {
  283. if (gadget->speed == USB_SPEED_HIGH)
  284. hs = 1;
  285. if (type == USB_DT_DEVICE_QUALIFIER)
  286. hs = !hs;
  287. }
  288. list_for_each_entry(c, &cdev->configs, list) {
  289. /* ignore configs that won't work at this speed */
  290. if (hs) {
  291. if (!c->highspeed)
  292. continue;
  293. } else {
  294. if (!c->fullspeed)
  295. continue;
  296. }
  297. count++;
  298. }
  299. return count;
  300. }
  301. static void device_qual(struct usb_composite_dev *cdev)
  302. {
  303. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  304. qual->bLength = sizeof(*qual);
  305. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  306. /* POLICY: same bcdUSB and device type info at both speeds */
  307. qual->bcdUSB = cdev->desc.bcdUSB;
  308. qual->bDeviceClass = cdev->desc.bDeviceClass;
  309. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  310. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  311. /* ASSUME same EP0 fifo size at both speeds */
  312. qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
  313. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  314. qual->bRESERVED = 0;
  315. }
  316. /*-------------------------------------------------------------------------*/
  317. static void reset_config(struct usb_composite_dev *cdev)
  318. {
  319. struct usb_function *f;
  320. DBG(cdev, "reset config\n");
  321. list_for_each_entry(f, &cdev->config->functions, list) {
  322. if (f->disable)
  323. f->disable(f);
  324. }
  325. cdev->config = NULL;
  326. }
  327. static int set_config(struct usb_composite_dev *cdev,
  328. const struct usb_ctrlrequest *ctrl, unsigned number)
  329. {
  330. struct usb_gadget *gadget = cdev->gadget;
  331. struct usb_configuration *c = NULL;
  332. int result = -EINVAL;
  333. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  334. int tmp;
  335. if (cdev->config)
  336. reset_config(cdev);
  337. if (number) {
  338. list_for_each_entry(c, &cdev->configs, list) {
  339. if (c->bConfigurationValue == number) {
  340. result = 0;
  341. break;
  342. }
  343. }
  344. if (result < 0)
  345. goto done;
  346. } else
  347. result = 0;
  348. INFO(cdev, "%s speed config #%d: %s\n",
  349. ({ char *speed;
  350. switch (gadget->speed) {
  351. case USB_SPEED_LOW: speed = "low"; break;
  352. case USB_SPEED_FULL: speed = "full"; break;
  353. case USB_SPEED_HIGH: speed = "high"; break;
  354. default: speed = "?"; break;
  355. } ; speed; }), number, c ? c->label : "unconfigured");
  356. if (!c)
  357. goto done;
  358. cdev->config = c;
  359. /* Initialize all interfaces by setting them to altsetting zero. */
  360. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  361. struct usb_function *f = c->interface[tmp];
  362. if (!f)
  363. break;
  364. result = f->set_alt(f, tmp, 0);
  365. if (result < 0) {
  366. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  367. tmp, f->name, f, result);
  368. reset_config(cdev);
  369. goto done;
  370. }
  371. }
  372. /* when we return, be sure our power usage is valid */
  373. power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
  374. done:
  375. usb_gadget_vbus_draw(gadget, power);
  376. return result;
  377. }
  378. /**
  379. * usb_add_config() - add a configuration to a device.
  380. * @cdev: wraps the USB gadget
  381. * @config: the configuration, with bConfigurationValue assigned
  382. * Context: single threaded during gadget setup
  383. *
  384. * One of the main tasks of a composite driver's bind() routine is to
  385. * add each of the configurations it supports, using this routine.
  386. *
  387. * This function returns the value of the configuration's bind(), which
  388. * is zero for success else a negative errno value. Binding configurations
  389. * assigns global resources including string IDs, and per-configuration
  390. * resources such as interface IDs and endpoints.
  391. */
  392. int __init usb_add_config(struct usb_composite_dev *cdev,
  393. struct usb_configuration *config)
  394. {
  395. int status = -EINVAL;
  396. struct usb_configuration *c;
  397. DBG(cdev, "adding config #%u '%s'/%p\n",
  398. config->bConfigurationValue,
  399. config->label, config);
  400. if (!config->bConfigurationValue || !config->bind)
  401. goto done;
  402. /* Prevent duplicate configuration identifiers */
  403. list_for_each_entry(c, &cdev->configs, list) {
  404. if (c->bConfigurationValue == config->bConfigurationValue) {
  405. status = -EBUSY;
  406. goto done;
  407. }
  408. }
  409. config->cdev = cdev;
  410. list_add_tail(&config->list, &cdev->configs);
  411. INIT_LIST_HEAD(&config->functions);
  412. config->next_interface_id = 0;
  413. status = config->bind(config);
  414. if (status < 0) {
  415. list_del(&config->list);
  416. config->cdev = NULL;
  417. } else {
  418. unsigned i;
  419. DBG(cdev, "cfg %d/%p speeds:%s%s\n",
  420. config->bConfigurationValue, config,
  421. config->highspeed ? " high" : "",
  422. config->fullspeed
  423. ? (gadget_is_dualspeed(cdev->gadget)
  424. ? " full"
  425. : " full/low")
  426. : "");
  427. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  428. struct usb_function *f = config->interface[i];
  429. if (!f)
  430. continue;
  431. DBG(cdev, " interface %d = %s/%p\n",
  432. i, f->name, f);
  433. }
  434. }
  435. /* set_alt(), or next config->bind(), sets up
  436. * ep->driver_data as needed.
  437. */
  438. usb_ep_autoconfig_reset(cdev->gadget);
  439. done:
  440. if (status)
  441. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  442. config->bConfigurationValue, status);
  443. return status;
  444. }
  445. /*-------------------------------------------------------------------------*/
  446. /* We support strings in multiple languages ... string descriptor zero
  447. * says which languages are supported. The typical case will be that
  448. * only one language (probably English) is used, with I18N handled on
  449. * the host side.
  450. */
  451. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  452. {
  453. const struct usb_gadget_strings *s;
  454. u16 language;
  455. __le16 *tmp;
  456. while (*sp) {
  457. s = *sp;
  458. language = cpu_to_le16(s->language);
  459. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  460. if (*tmp == language)
  461. goto repeat;
  462. }
  463. *tmp++ = language;
  464. repeat:
  465. sp++;
  466. }
  467. }
  468. static int lookup_string(
  469. struct usb_gadget_strings **sp,
  470. void *buf,
  471. u16 language,
  472. int id
  473. )
  474. {
  475. struct usb_gadget_strings *s;
  476. int value;
  477. while (*sp) {
  478. s = *sp++;
  479. if (s->language != language)
  480. continue;
  481. value = usb_gadget_get_string(s, id, buf);
  482. if (value > 0)
  483. return value;
  484. }
  485. return -EINVAL;
  486. }
  487. static int get_string(struct usb_composite_dev *cdev,
  488. void *buf, u16 language, int id)
  489. {
  490. struct usb_configuration *c;
  491. struct usb_function *f;
  492. int len;
  493. /* Yes, not only is USB's I18N support probably more than most
  494. * folk will ever care about ... also, it's all supported here.
  495. * (Except for UTF8 support for Unicode's "Astral Planes".)
  496. */
  497. /* 0 == report all available language codes */
  498. if (id == 0) {
  499. struct usb_string_descriptor *s = buf;
  500. struct usb_gadget_strings **sp;
  501. memset(s, 0, 256);
  502. s->bDescriptorType = USB_DT_STRING;
  503. sp = composite->strings;
  504. if (sp)
  505. collect_langs(sp, s->wData);
  506. list_for_each_entry(c, &cdev->configs, list) {
  507. sp = c->strings;
  508. if (sp)
  509. collect_langs(sp, s->wData);
  510. list_for_each_entry(f, &c->functions, list) {
  511. sp = f->strings;
  512. if (sp)
  513. collect_langs(sp, s->wData);
  514. }
  515. }
  516. for (len = 0; s->wData[len] && len <= 126; len++)
  517. continue;
  518. if (!len)
  519. return -EINVAL;
  520. s->bLength = 2 * (len + 1);
  521. return s->bLength;
  522. }
  523. /* Otherwise, look up and return a specified string. String IDs
  524. * are device-scoped, so we look up each string table we're told
  525. * about. These lookups are infrequent; simpler-is-better here.
  526. */
  527. if (composite->strings) {
  528. len = lookup_string(composite->strings, buf, language, id);
  529. if (len > 0)
  530. return len;
  531. }
  532. list_for_each_entry(c, &cdev->configs, list) {
  533. if (c->strings) {
  534. len = lookup_string(c->strings, buf, language, id);
  535. if (len > 0)
  536. return len;
  537. }
  538. list_for_each_entry(f, &c->functions, list) {
  539. if (!f->strings)
  540. continue;
  541. len = lookup_string(f->strings, buf, language, id);
  542. if (len > 0)
  543. return len;
  544. }
  545. }
  546. return -EINVAL;
  547. }
  548. /**
  549. * usb_string_id() - allocate an unused string ID
  550. * @cdev: the device whose string descriptor IDs are being allocated
  551. * Context: single threaded during gadget setup
  552. *
  553. * @usb_string_id() is called from bind() callbacks to allocate
  554. * string IDs. Drivers for functions, configurations, or gadgets will
  555. * then store that ID in the appropriate descriptors and string table.
  556. *
  557. * All string identifier should be allocated using this routine, to
  558. * ensure that for example different functions don't wrongly assign
  559. * different meanings to the same identifier.
  560. */
  561. int __init usb_string_id(struct usb_composite_dev *cdev)
  562. {
  563. if (cdev->next_string_id < 254) {
  564. /* string id 0 is reserved */
  565. cdev->next_string_id++;
  566. return cdev->next_string_id;
  567. }
  568. return -ENODEV;
  569. }
  570. /*-------------------------------------------------------------------------*/
  571. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  572. {
  573. if (req->status || req->actual != req->length)
  574. DBG((struct usb_composite_dev *) ep->driver_data,
  575. "setup complete --> %d, %d/%d\n",
  576. req->status, req->actual, req->length);
  577. }
  578. /*
  579. * The setup() callback implements all the ep0 functionality that's
  580. * not handled lower down, in hardware or the hardware driver(like
  581. * device and endpoint feature flags, and their status). It's all
  582. * housekeeping for the gadget function we're implementing. Most of
  583. * the work is in config and function specific setup.
  584. */
  585. static int
  586. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  587. {
  588. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  589. struct usb_request *req = cdev->req;
  590. int value = -EOPNOTSUPP;
  591. u16 w_index = le16_to_cpu(ctrl->wIndex);
  592. u16 w_value = le16_to_cpu(ctrl->wValue);
  593. u16 w_length = le16_to_cpu(ctrl->wLength);
  594. struct usb_function *f = NULL;
  595. /* partial re-init of the response message; the function or the
  596. * gadget might need to intercept e.g. a control-OUT completion
  597. * when we delegate to it.
  598. */
  599. req->zero = 0;
  600. req->complete = composite_setup_complete;
  601. req->length = USB_BUFSIZ;
  602. gadget->ep0->driver_data = cdev;
  603. switch (ctrl->bRequest) {
  604. /* we handle all standard USB descriptors */
  605. case USB_REQ_GET_DESCRIPTOR:
  606. if (ctrl->bRequestType != USB_DIR_IN)
  607. goto unknown;
  608. switch (w_value >> 8) {
  609. case USB_DT_DEVICE:
  610. cdev->desc.bNumConfigurations =
  611. count_configs(cdev, USB_DT_DEVICE);
  612. value = min(w_length, (u16) sizeof cdev->desc);
  613. memcpy(req->buf, &cdev->desc, value);
  614. break;
  615. case USB_DT_DEVICE_QUALIFIER:
  616. if (!gadget_is_dualspeed(gadget))
  617. break;
  618. device_qual(cdev);
  619. value = min_t(int, w_length,
  620. sizeof(struct usb_qualifier_descriptor));
  621. break;
  622. case USB_DT_OTHER_SPEED_CONFIG:
  623. if (!gadget_is_dualspeed(gadget))
  624. break;
  625. /* FALLTHROUGH */
  626. case USB_DT_CONFIG:
  627. value = config_desc(cdev, w_value);
  628. if (value >= 0)
  629. value = min(w_length, (u16) value);
  630. break;
  631. case USB_DT_STRING:
  632. value = get_string(cdev, req->buf,
  633. w_index, w_value & 0xff);
  634. if (value >= 0)
  635. value = min(w_length, (u16) value);
  636. break;
  637. }
  638. break;
  639. /* any number of configs can work */
  640. case USB_REQ_SET_CONFIGURATION:
  641. if (ctrl->bRequestType != 0)
  642. goto unknown;
  643. if (gadget_is_otg(gadget)) {
  644. if (gadget->a_hnp_support)
  645. DBG(cdev, "HNP available\n");
  646. else if (gadget->a_alt_hnp_support)
  647. DBG(cdev, "HNP on another port\n");
  648. else
  649. VDBG(cdev, "HNP inactive\n");
  650. }
  651. spin_lock(&cdev->lock);
  652. value = set_config(cdev, ctrl, w_value);
  653. spin_unlock(&cdev->lock);
  654. break;
  655. case USB_REQ_GET_CONFIGURATION:
  656. if (ctrl->bRequestType != USB_DIR_IN)
  657. goto unknown;
  658. if (cdev->config)
  659. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  660. else
  661. *(u8 *)req->buf = 0;
  662. value = min(w_length, (u16) 1);
  663. break;
  664. /* function drivers must handle get/set altsetting; if there's
  665. * no get() method, we know only altsetting zero works.
  666. */
  667. case USB_REQ_SET_INTERFACE:
  668. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  669. goto unknown;
  670. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  671. break;
  672. f = cdev->config->interface[w_index];
  673. if (!f)
  674. break;
  675. if (w_value && !f->get_alt)
  676. break;
  677. value = f->set_alt(f, w_index, w_value);
  678. break;
  679. case USB_REQ_GET_INTERFACE:
  680. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  681. goto unknown;
  682. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  683. break;
  684. f = cdev->config->interface[w_index];
  685. if (!f)
  686. break;
  687. /* lots of interfaces only need altsetting zero... */
  688. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  689. if (value < 0)
  690. break;
  691. *((u8 *)req->buf) = value;
  692. value = min(w_length, (u16) 1);
  693. break;
  694. default:
  695. unknown:
  696. VDBG(cdev,
  697. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  698. ctrl->bRequestType, ctrl->bRequest,
  699. w_value, w_index, w_length);
  700. /* functions always handle their interfaces ... punt other
  701. * recipients (endpoint, other, WUSB, ...) to the current
  702. * configuration code.
  703. *
  704. * REVISIT it could make sense to let the composite device
  705. * take such requests too, if that's ever needed: to work
  706. * in config 0, etc.
  707. */
  708. if ((ctrl->bRequestType & USB_RECIP_MASK)
  709. == USB_RECIP_INTERFACE) {
  710. f = cdev->config->interface[w_index];
  711. if (f && f->setup)
  712. value = f->setup(f, ctrl);
  713. else
  714. f = NULL;
  715. }
  716. if (value < 0 && !f) {
  717. struct usb_configuration *c;
  718. c = cdev->config;
  719. if (c && c->setup)
  720. value = c->setup(c, ctrl);
  721. }
  722. goto done;
  723. }
  724. /* respond with data transfer before status phase? */
  725. if (value >= 0) {
  726. req->length = value;
  727. req->zero = value < w_length;
  728. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  729. if (value < 0) {
  730. DBG(cdev, "ep_queue --> %d\n", value);
  731. req->status = 0;
  732. composite_setup_complete(gadget->ep0, req);
  733. }
  734. }
  735. done:
  736. /* device either stalls (value < 0) or reports success */
  737. return value;
  738. }
  739. static void composite_disconnect(struct usb_gadget *gadget)
  740. {
  741. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  742. unsigned long flags;
  743. /* REVISIT: should we have config and device level
  744. * disconnect callbacks?
  745. */
  746. spin_lock_irqsave(&cdev->lock, flags);
  747. if (cdev->config)
  748. reset_config(cdev);
  749. spin_unlock_irqrestore(&cdev->lock, flags);
  750. }
  751. /*-------------------------------------------------------------------------*/
  752. static void /* __init_or_exit */
  753. composite_unbind(struct usb_gadget *gadget)
  754. {
  755. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  756. /* composite_disconnect() must already have been called
  757. * by the underlying peripheral controller driver!
  758. * so there's no i/o concurrency that could affect the
  759. * state protected by cdev->lock.
  760. */
  761. WARN_ON(cdev->config);
  762. while (!list_empty(&cdev->configs)) {
  763. struct usb_configuration *c;
  764. c = list_first_entry(&cdev->configs,
  765. struct usb_configuration, list);
  766. while (!list_empty(&c->functions)) {
  767. struct usb_function *f;
  768. f = list_first_entry(&c->functions,
  769. struct usb_function, list);
  770. list_del(&f->list);
  771. if (f->unbind) {
  772. DBG(cdev, "unbind function '%s'/%p\n",
  773. f->name, f);
  774. f->unbind(c, f);
  775. /* may free memory for "f" */
  776. }
  777. }
  778. list_del(&c->list);
  779. if (c->unbind) {
  780. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  781. c->unbind(c);
  782. /* may free memory for "c" */
  783. }
  784. }
  785. if (composite->unbind)
  786. composite->unbind(cdev);
  787. if (cdev->req) {
  788. kfree(cdev->req->buf);
  789. usb_ep_free_request(gadget->ep0, cdev->req);
  790. }
  791. kfree(cdev);
  792. set_gadget_data(gadget, NULL);
  793. composite = NULL;
  794. }
  795. static void __init
  796. string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
  797. {
  798. struct usb_string *str = tab->strings;
  799. for (str = tab->strings; str->s; str++) {
  800. if (str->id == id) {
  801. str->s = s;
  802. return;
  803. }
  804. }
  805. }
  806. static void __init
  807. string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
  808. {
  809. while (*tab) {
  810. string_override_one(*tab, id, s);
  811. tab++;
  812. }
  813. }
  814. static int __init composite_bind(struct usb_gadget *gadget)
  815. {
  816. struct usb_composite_dev *cdev;
  817. int status = -ENOMEM;
  818. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  819. if (!cdev)
  820. return status;
  821. spin_lock_init(&cdev->lock);
  822. cdev->gadget = gadget;
  823. set_gadget_data(gadget, cdev);
  824. INIT_LIST_HEAD(&cdev->configs);
  825. /* preallocate control response and buffer */
  826. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  827. if (!cdev->req)
  828. goto fail;
  829. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  830. if (!cdev->req->buf)
  831. goto fail;
  832. cdev->req->complete = composite_setup_complete;
  833. gadget->ep0->driver_data = cdev;
  834. cdev->bufsiz = USB_BUFSIZ;
  835. cdev->driver = composite;
  836. usb_gadget_set_selfpowered(gadget);
  837. /* interface and string IDs start at zero via kzalloc.
  838. * we force endpoints to start unassigned; few controller
  839. * drivers will zero ep->driver_data.
  840. */
  841. usb_ep_autoconfig_reset(cdev->gadget);
  842. /* composite gadget needs to assign strings for whole device (like
  843. * serial number), register function drivers, potentially update
  844. * power state and consumption, etc
  845. */
  846. status = composite->bind(cdev);
  847. if (status < 0)
  848. goto fail;
  849. cdev->desc = *composite->dev;
  850. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  851. /* standardized runtime overrides for device ID data */
  852. if (idVendor)
  853. cdev->desc.idVendor = cpu_to_le16(idVendor);
  854. if (idProduct)
  855. cdev->desc.idProduct = cpu_to_le16(idProduct);
  856. if (bcdDevice)
  857. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  858. /* strings can't be assigned before bind() allocates the
  859. * releavnt identifiers
  860. */
  861. if (cdev->desc.iManufacturer && iManufacturer)
  862. string_override(composite->strings,
  863. cdev->desc.iManufacturer, iManufacturer);
  864. if (cdev->desc.iProduct && iProduct)
  865. string_override(composite->strings,
  866. cdev->desc.iProduct, iProduct);
  867. if (cdev->desc.iSerialNumber && iSerialNumber)
  868. string_override(composite->strings,
  869. cdev->desc.iSerialNumber, iSerialNumber);
  870. INFO(cdev, "%s ready\n", composite->name);
  871. return 0;
  872. fail:
  873. composite_unbind(gadget);
  874. return status;
  875. }
  876. /*-------------------------------------------------------------------------*/
  877. static void
  878. composite_suspend(struct usb_gadget *gadget)
  879. {
  880. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  881. struct usb_function *f;
  882. /* REVISIT: should we have config and device level
  883. * suspend/resume callbacks?
  884. */
  885. DBG(cdev, "suspend\n");
  886. if (cdev->config) {
  887. list_for_each_entry(f, &cdev->config->functions, list) {
  888. if (f->suspend)
  889. f->suspend(f);
  890. }
  891. }
  892. }
  893. static void
  894. composite_resume(struct usb_gadget *gadget)
  895. {
  896. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  897. struct usb_function *f;
  898. /* REVISIT: should we have config and device level
  899. * suspend/resume callbacks?
  900. */
  901. DBG(cdev, "resume\n");
  902. if (cdev->config) {
  903. list_for_each_entry(f, &cdev->config->functions, list) {
  904. if (f->resume)
  905. f->resume(f);
  906. }
  907. }
  908. }
  909. /*-------------------------------------------------------------------------*/
  910. static struct usb_gadget_driver composite_driver = {
  911. .speed = USB_SPEED_HIGH,
  912. .bind = composite_bind,
  913. .unbind = __exit_p(composite_unbind),
  914. .setup = composite_setup,
  915. .disconnect = composite_disconnect,
  916. .suspend = composite_suspend,
  917. .resume = composite_resume,
  918. .driver = {
  919. .owner = THIS_MODULE,
  920. },
  921. };
  922. /**
  923. * usb_composite_register() - register a composite driver
  924. * @driver: the driver to register
  925. * Context: single threaded during gadget setup
  926. *
  927. * This function is used to register drivers using the composite driver
  928. * framework. The return value is zero, or a negative errno value.
  929. * Those values normally come from the driver's @bind method, which does
  930. * all the work of setting up the driver to match the hardware.
  931. *
  932. * On successful return, the gadget is ready to respond to requests from
  933. * the host, unless one of its components invokes usb_gadget_disconnect()
  934. * while it was binding. That would usually be done in order to wait for
  935. * some userspace participation.
  936. */
  937. int __init usb_composite_register(struct usb_composite_driver *driver)
  938. {
  939. if (!driver || !driver->dev || !driver->bind || composite)
  940. return -EINVAL;
  941. if (!driver->name)
  942. driver->name = "composite";
  943. composite_driver.function = (char *) driver->name;
  944. composite_driver.driver.name = driver->name;
  945. composite = driver;
  946. return usb_gadget_register_driver(&composite_driver);
  947. }
  948. /**
  949. * usb_composite_unregister() - unregister a composite driver
  950. * @driver: the driver to unregister
  951. *
  952. * This function is used to unregister drivers using the composite
  953. * driver framework.
  954. */
  955. void __exit usb_composite_unregister(struct usb_composite_driver *driver)
  956. {
  957. if (composite != driver)
  958. return;
  959. usb_gadget_unregister_driver(&composite_driver);
  960. }