multi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 0x0525 /* XXX NetChip */
  60. #define MULTI_PRODUCT_NUM 0xa4ab /* XXX */
  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. MULTI_STRING_MANUFACTURER_IDX,
  95. MULTI_STRING_PRODUCT_IDX,
  96. #ifdef CONFIG_USB_G_MULTI_RNDIS
  97. MULTI_STRING_RNDIS_CONFIG_IDX,
  98. #endif
  99. #ifdef CONFIG_USB_G_MULTI_CDC
  100. MULTI_STRING_CDC_CONFIG_IDX,
  101. #endif
  102. };
  103. static char manufacturer[50];
  104. static struct usb_string strings_dev[] = {
  105. [MULTI_STRING_MANUFACTURER_IDX].s = manufacturer,
  106. [MULTI_STRING_PRODUCT_IDX].s = DRIVER_DESC,
  107. #ifdef CONFIG_USB_G_MULTI_RNDIS
  108. [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
  109. #endif
  110. #ifdef CONFIG_USB_G_MULTI_CDC
  111. [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
  112. #endif
  113. { } /* end of list */
  114. };
  115. static struct usb_gadget_strings *dev_strings[] = {
  116. &(struct usb_gadget_strings){
  117. .language = 0x0409, /* en-us */
  118. .strings = strings_dev,
  119. },
  120. NULL,
  121. };
  122. /****************************** Configurations ******************************/
  123. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  124. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  125. static struct fsg_common fsg_common;
  126. static u8 hostaddr[ETH_ALEN];
  127. /********** RNDIS **********/
  128. #ifdef USB_ETH_RNDIS
  129. static __ref int rndis_do_config(struct usb_configuration *c)
  130. {
  131. int ret;
  132. if (gadget_is_otg(c->cdev->gadget)) {
  133. c->descriptors = otg_desc;
  134. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  135. }
  136. ret = rndis_bind_config(c, hostaddr);
  137. if (ret < 0)
  138. return ret;
  139. ret = acm_bind_config(c, 0);
  140. if (ret < 0)
  141. return ret;
  142. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  143. if (ret < 0)
  144. return ret;
  145. return 0;
  146. }
  147. static int rndis_config_register(struct usb_composite_dev *cdev)
  148. {
  149. static struct usb_configuration config = {
  150. .bind = rndis_do_config,
  151. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  152. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  153. };
  154. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  155. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  156. return usb_add_config(cdev, &config);
  157. }
  158. #else
  159. static int rndis_config_register(struct usb_composite_dev *cdev)
  160. {
  161. return 0;
  162. }
  163. #endif
  164. /********** CDC ECM **********/
  165. #ifdef CONFIG_USB_G_MULTI_CDC
  166. static __ref int cdc_do_config(struct usb_configuration *c)
  167. {
  168. int ret;
  169. if (gadget_is_otg(c->cdev->gadget)) {
  170. c->descriptors = otg_desc;
  171. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  172. }
  173. ret = ecm_bind_config(c, hostaddr);
  174. if (ret < 0)
  175. return ret;
  176. ret = acm_bind_config(c, 0);
  177. if (ret < 0)
  178. return ret;
  179. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  180. if (ret < 0)
  181. return ret;
  182. return 0;
  183. }
  184. static int cdc_config_register(struct usb_composite_dev *cdev)
  185. {
  186. static struct usb_configuration config = {
  187. .bind = cdc_do_config,
  188. .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
  189. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  190. };
  191. config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
  192. config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
  193. return usb_add_config(cdev, &config);
  194. }
  195. #else
  196. static int cdc_config_register(struct usb_composite_dev *cdev)
  197. {
  198. return 0;
  199. }
  200. #endif
  201. /****************************** Gadget Bind ******************************/
  202. static int __ref multi_bind(struct usb_composite_dev *cdev)
  203. {
  204. struct usb_gadget *gadget = cdev->gadget;
  205. int status, gcnum;
  206. if (!can_support_ecm(cdev->gadget)) {
  207. dev_err(&gadget->dev, "controller '%s' not usable\n",
  208. gadget->name);
  209. return -EINVAL;
  210. }
  211. /* set up network link layer */
  212. status = gether_setup(cdev->gadget, hostaddr);
  213. if (status < 0)
  214. return status;
  215. /* set up serial link layer */
  216. status = gserial_setup(cdev->gadget, 1);
  217. if (status < 0)
  218. goto fail0;
  219. /* set up mass storage function */
  220. {
  221. void *retp;
  222. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  223. if (IS_ERR(retp)) {
  224. status = PTR_ERR(retp);
  225. goto fail1;
  226. }
  227. }
  228. /* set bcdDevice */
  229. gcnum = usb_gadget_controller_number(gadget);
  230. if (gcnum >= 0) {
  231. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  232. } else {
  233. WARNING(cdev, "controller '%s' not recognized\n", gadget->name);
  234. device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
  235. }
  236. /* allocate string descriptor numbers */
  237. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  238. init_utsname()->sysname, init_utsname()->release,
  239. gadget->name);
  240. status = usb_string_ids_tab(cdev, strings_dev);
  241. if (unlikely(status < 0))
  242. goto fail2;
  243. device_desc.iManufacturer =
  244. strings_dev[MULTI_STRING_MANUFACTURER_IDX].id;
  245. device_desc.iProduct =
  246. strings_dev[MULTI_STRING_PRODUCT_IDX].id;
  247. /* register configurations */
  248. status = rndis_config_register(cdev);
  249. if (unlikely(status < 0))
  250. goto fail2;
  251. status = cdc_config_register(cdev);
  252. if (unlikely(status < 0))
  253. goto fail2;
  254. /* we're done */
  255. dev_info(&gadget->dev, DRIVER_DESC "\n");
  256. fsg_common_put(&fsg_common);
  257. return 0;
  258. /* error recovery */
  259. fail2:
  260. fsg_common_put(&fsg_common);
  261. fail1:
  262. gserial_cleanup();
  263. fail0:
  264. gether_cleanup();
  265. return status;
  266. }
  267. static int __exit multi_unbind(struct usb_composite_dev *cdev)
  268. {
  269. gserial_cleanup();
  270. gether_cleanup();
  271. return 0;
  272. }
  273. /****************************** Some noise ******************************/
  274. static struct usb_composite_driver multi_driver = {
  275. .name = "g_multi",
  276. .dev = &device_desc,
  277. .strings = dev_strings,
  278. .bind = multi_bind,
  279. .unbind = __exit_p(multi_unbind),
  280. };
  281. static int __init multi_init(void)
  282. {
  283. return usb_composite_register(&multi_driver);
  284. }
  285. module_init(multi_init);
  286. static void __exit multi_exit(void)
  287. {
  288. usb_composite_unregister(&multi_driver);
  289. }
  290. module_exit(multi_exit);