multi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * multi.c -- Multifunction Composite driver
  3. *
  4. * Copyright (C) 2008 David Brownell
  5. * Copyright (C) 2008 Nokia Corporation
  6. * Copyright (C) 2009 Samsung Electronics
  7. * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/utsname.h>
  25. #include <linux/module.h>
  26. #if defined USB_ETH_RNDIS
  27. # undef USB_ETH_RNDIS
  28. #endif
  29. #ifdef CONFIG_USB_G_MULTI_RNDIS
  30. # define USB_ETH_RNDIS y
  31. #endif
  32. #define DRIVER_DESC "Multifunction Composite Gadget"
  33. MODULE_DESCRIPTION(DRIVER_DESC);
  34. MODULE_AUTHOR("Michal Nazarewicz");
  35. MODULE_LICENSE("GPL");
  36. /***************************** All the files... *****************************/
  37. /*
  38. * kbuild is not very cooperative with respect to linking separately
  39. * compiled library objects into one module. So for now we won't use
  40. * separate compilation ... ensuring init/exit sections work to shrink
  41. * the runtime footprint, and giving us at least some parts of what
  42. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  43. */
  44. #include "composite.c"
  45. #include "usbstring.c"
  46. #include "config.c"
  47. #include "epautoconf.c"
  48. #include "f_mass_storage.c"
  49. #include "u_serial.c"
  50. #include "f_acm.c"
  51. #include "f_ecm.c"
  52. #include "f_subset.c"
  53. #ifdef USB_ETH_RNDIS
  54. # include "f_rndis.c"
  55. # include "rndis.c"
  56. #endif
  57. #include "u_ether.c"
  58. /***************************** Device Descriptor ****************************/
  59. #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
  60. #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
  61. enum {
  62. __MULTI_NO_CONFIG,
  63. #ifdef CONFIG_USB_G_MULTI_RNDIS
  64. MULTI_RNDIS_CONFIG_NUM,
  65. #endif
  66. #ifdef CONFIG_USB_G_MULTI_CDC
  67. MULTI_CDC_CONFIG_NUM,
  68. #endif
  69. };
  70. static struct usb_device_descriptor device_desc = {
  71. .bLength = sizeof device_desc,
  72. .bDescriptorType = USB_DT_DEVICE,
  73. .bcdUSB = cpu_to_le16(0x0200),
  74. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  75. .bDeviceSubClass = 2,
  76. .bDeviceProtocol = 1,
  77. /* Vendor and product id can be overridden by module parameters. */
  78. .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
  79. .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
  80. };
  81. static const struct usb_descriptor_header *otg_desc[] = {
  82. (struct usb_descriptor_header *) &(struct usb_otg_descriptor){
  83. .bLength = sizeof(struct usb_otg_descriptor),
  84. .bDescriptorType = USB_DT_OTG,
  85. /*
  86. * REVISIT SRP-only hardware is possible, although
  87. * it would not be called "OTG" ...
  88. */
  89. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  90. },
  91. NULL,
  92. };
  93. enum {
  94. #ifdef CONFIG_USB_G_MULTI_RNDIS
  95. MULTI_STRING_RNDIS_CONFIG_IDX,
  96. #endif
  97. #ifdef CONFIG_USB_G_MULTI_CDC
  98. MULTI_STRING_CDC_CONFIG_IDX,
  99. #endif
  100. };
  101. static struct usb_string strings_dev[] = {
  102. #ifdef CONFIG_USB_G_MULTI_RNDIS
  103. [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
  104. #endif
  105. #ifdef CONFIG_USB_G_MULTI_CDC
  106. [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
  107. #endif
  108. { } /* end of list */
  109. };
  110. static struct usb_gadget_strings *dev_strings[] = {
  111. &(struct usb_gadget_strings){
  112. .language = 0x0409, /* en-us */
  113. .strings = strings_dev,
  114. },
  115. NULL,
  116. };
  117. /****************************** Configurations ******************************/
  118. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  119. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  120. static struct fsg_common fsg_common;
  121. static u8 hostaddr[ETH_ALEN];
  122. /********** RNDIS **********/
  123. #ifdef USB_ETH_RNDIS
  124. static __init int rndis_do_config(struct usb_configuration *c)
  125. {
  126. int ret;
  127. if (gadget_is_otg(c->cdev->gadget)) {
  128. c->descriptors = otg_desc;
  129. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  130. }
  131. ret = rndis_bind_config(c, hostaddr);
  132. if (ret < 0)
  133. return ret;
  134. ret = acm_bind_config(c, 0);
  135. if (ret < 0)
  136. return ret;
  137. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  138. if (ret < 0)
  139. return ret;
  140. return 0;
  141. }
  142. static int rndis_config_register(struct usb_composite_dev *cdev)
  143. {
  144. static struct usb_configuration config = {
  145. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  146. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  147. };
  148. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  149. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  150. return usb_add_config(cdev, &config, rndis_do_config);
  151. }
  152. #else
  153. static int rndis_config_register(struct usb_composite_dev *cdev)
  154. {
  155. return 0;
  156. }
  157. #endif
  158. /********** CDC ECM **********/
  159. #ifdef CONFIG_USB_G_MULTI_CDC
  160. static __init int cdc_do_config(struct usb_configuration *c)
  161. {
  162. int ret;
  163. if (gadget_is_otg(c->cdev->gadget)) {
  164. c->descriptors = otg_desc;
  165. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  166. }
  167. ret = ecm_bind_config(c, hostaddr);
  168. if (ret < 0)
  169. return ret;
  170. ret = acm_bind_config(c, 0);
  171. if (ret < 0)
  172. return ret;
  173. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  174. if (ret < 0)
  175. return ret;
  176. return 0;
  177. }
  178. static int cdc_config_register(struct usb_composite_dev *cdev)
  179. {
  180. static struct usb_configuration config = {
  181. .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
  182. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  183. };
  184. config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
  185. config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
  186. return usb_add_config(cdev, &config, cdc_do_config);
  187. }
  188. #else
  189. static int cdc_config_register(struct usb_composite_dev *cdev)
  190. {
  191. return 0;
  192. }
  193. #endif
  194. /****************************** Gadget Bind ******************************/
  195. static int __ref multi_bind(struct usb_composite_dev *cdev)
  196. {
  197. struct usb_gadget *gadget = cdev->gadget;
  198. int status, gcnum;
  199. if (!can_support_ecm(cdev->gadget)) {
  200. dev_err(&gadget->dev, "controller '%s' not usable\n",
  201. gadget->name);
  202. return -EINVAL;
  203. }
  204. /* set up network link layer */
  205. status = gether_setup(cdev->gadget, hostaddr);
  206. if (status < 0)
  207. return status;
  208. /* set up serial link layer */
  209. status = gserial_setup(cdev->gadget, 1);
  210. if (status < 0)
  211. goto fail0;
  212. /* set up mass storage function */
  213. {
  214. void *retp;
  215. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  216. if (IS_ERR(retp)) {
  217. status = PTR_ERR(retp);
  218. goto fail1;
  219. }
  220. }
  221. /* set bcdDevice */
  222. gcnum = usb_gadget_controller_number(gadget);
  223. if (gcnum >= 0) {
  224. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  225. } else {
  226. WARNING(cdev, "controller '%s' not recognized\n", gadget->name);
  227. device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
  228. }
  229. /* allocate string IDs */
  230. status = usb_string_ids_tab(cdev, strings_dev);
  231. if (unlikely(status < 0))
  232. goto fail2;
  233. /* register configurations */
  234. status = rndis_config_register(cdev);
  235. if (unlikely(status < 0))
  236. goto fail2;
  237. status = cdc_config_register(cdev);
  238. if (unlikely(status < 0))
  239. goto fail2;
  240. /* we're done */
  241. dev_info(&gadget->dev, DRIVER_DESC "\n");
  242. fsg_common_put(&fsg_common);
  243. return 0;
  244. /* error recovery */
  245. fail2:
  246. fsg_common_put(&fsg_common);
  247. fail1:
  248. gserial_cleanup();
  249. fail0:
  250. gether_cleanup();
  251. return status;
  252. }
  253. static int __exit multi_unbind(struct usb_composite_dev *cdev)
  254. {
  255. gserial_cleanup();
  256. gether_cleanup();
  257. return 0;
  258. }
  259. /****************************** Some noise ******************************/
  260. static struct usb_composite_driver multi_driver = {
  261. .name = "g_multi",
  262. .dev = &device_desc,
  263. .strings = dev_strings,
  264. .unbind = __exit_p(multi_unbind),
  265. .iProduct = DRIVER_DESC,
  266. .needs_serial = 1,
  267. };
  268. static int __init multi_init(void)
  269. {
  270. return usb_composite_probe(&multi_driver, multi_bind);
  271. }
  272. module_init(multi_init);
  273. static void __exit multi_exit(void)
  274. {
  275. usb_composite_unregister(&multi_driver);
  276. }
  277. module_exit(multi_exit);