multi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 (mina86@mina86.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. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include "u_serial.h"
  17. #if defined USB_ETH_RNDIS
  18. # undef USB_ETH_RNDIS
  19. #endif
  20. #ifdef CONFIG_USB_G_MULTI_RNDIS
  21. # define USB_ETH_RNDIS y
  22. #endif
  23. #define DRIVER_DESC "Multifunction Composite Gadget"
  24. MODULE_DESCRIPTION(DRIVER_DESC);
  25. MODULE_AUTHOR("Michal Nazarewicz");
  26. MODULE_LICENSE("GPL");
  27. /***************************** All the files... *****************************/
  28. /*
  29. * kbuild is not very cooperative with respect to linking separately
  30. * compiled library objects into one module. So for now we won't use
  31. * separate compilation ... ensuring init/exit sections work to shrink
  32. * the runtime footprint, and giving us at least some parts of what
  33. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  34. */
  35. #include "f_mass_storage.c"
  36. #include "f_ecm.c"
  37. #include "f_subset.c"
  38. #ifdef USB_ETH_RNDIS
  39. # include "f_rndis.c"
  40. # include "rndis.h"
  41. #endif
  42. #include "u_ether.h"
  43. USB_GADGET_COMPOSITE_OPTIONS();
  44. USB_ETHERNET_MODULE_PARAMETERS();
  45. /***************************** Device Descriptor ****************************/
  46. #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
  47. #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
  48. enum {
  49. __MULTI_NO_CONFIG,
  50. #ifdef CONFIG_USB_G_MULTI_RNDIS
  51. MULTI_RNDIS_CONFIG_NUM,
  52. #endif
  53. #ifdef CONFIG_USB_G_MULTI_CDC
  54. MULTI_CDC_CONFIG_NUM,
  55. #endif
  56. };
  57. static struct usb_device_descriptor device_desc = {
  58. .bLength = sizeof device_desc,
  59. .bDescriptorType = USB_DT_DEVICE,
  60. .bcdUSB = cpu_to_le16(0x0200),
  61. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  62. .bDeviceSubClass = 2,
  63. .bDeviceProtocol = 1,
  64. /* Vendor and product id can be overridden by module parameters. */
  65. .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
  66. .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
  67. };
  68. static const struct usb_descriptor_header *otg_desc[] = {
  69. (struct usb_descriptor_header *) &(struct usb_otg_descriptor){
  70. .bLength = sizeof(struct usb_otg_descriptor),
  71. .bDescriptorType = USB_DT_OTG,
  72. /*
  73. * REVISIT SRP-only hardware is possible, although
  74. * it would not be called "OTG" ...
  75. */
  76. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  77. },
  78. NULL,
  79. };
  80. enum {
  81. MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
  82. MULTI_STRING_CDC_CONFIG_IDX,
  83. };
  84. static struct usb_string strings_dev[] = {
  85. [USB_GADGET_MANUFACTURER_IDX].s = "",
  86. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  87. [USB_GADGET_SERIAL_IDX].s = "",
  88. [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
  89. [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
  90. { } /* end of list */
  91. };
  92. static struct usb_gadget_strings *dev_strings[] = {
  93. &(struct usb_gadget_strings){
  94. .language = 0x0409, /* en-us */
  95. .strings = strings_dev,
  96. },
  97. NULL,
  98. };
  99. /****************************** Configurations ******************************/
  100. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  101. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  102. static struct fsg_common fsg_common;
  103. static u8 host_mac[ETH_ALEN];
  104. static struct usb_function_instance *fi_acm;
  105. static struct eth_dev *the_dev;
  106. /********** RNDIS **********/
  107. #ifdef USB_ETH_RNDIS
  108. static struct usb_function *f_acm_rndis;
  109. static __init int rndis_do_config(struct usb_configuration *c)
  110. {
  111. int ret;
  112. if (gadget_is_otg(c->cdev->gadget)) {
  113. c->descriptors = otg_desc;
  114. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  115. }
  116. ret = rndis_bind_config(c, host_mac, the_dev);
  117. if (ret < 0)
  118. return ret;
  119. f_acm_rndis = usb_get_function(fi_acm);
  120. if (IS_ERR(f_acm_rndis)) {
  121. ret = PTR_ERR(f_acm_rndis);
  122. goto err_func_acm;
  123. }
  124. ret = usb_add_function(c, f_acm_rndis);
  125. if (ret)
  126. goto err_conf;
  127. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  128. if (ret < 0)
  129. goto err_fsg;
  130. return 0;
  131. err_fsg:
  132. usb_remove_function(c, f_acm_rndis);
  133. err_conf:
  134. usb_put_function(f_acm_rndis);
  135. err_func_acm:
  136. return ret;
  137. }
  138. static int rndis_config_register(struct usb_composite_dev *cdev)
  139. {
  140. static struct usb_configuration config = {
  141. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  142. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  143. };
  144. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  145. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  146. return usb_add_config(cdev, &config, rndis_do_config);
  147. }
  148. #else
  149. static int rndis_config_register(struct usb_composite_dev *cdev)
  150. {
  151. return 0;
  152. }
  153. #endif
  154. /********** CDC ECM **********/
  155. #ifdef CONFIG_USB_G_MULTI_CDC
  156. static struct usb_function *f_acm_multi;
  157. static __init int cdc_do_config(struct usb_configuration *c)
  158. {
  159. int ret;
  160. if (gadget_is_otg(c->cdev->gadget)) {
  161. c->descriptors = otg_desc;
  162. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  163. }
  164. ret = ecm_bind_config(c, host_mac, the_dev);
  165. if (ret < 0)
  166. return ret;
  167. /* implicit port_num is zero */
  168. f_acm_multi = usb_get_function(fi_acm);
  169. if (IS_ERR(f_acm_multi))
  170. goto err_func_acm;
  171. ret = usb_add_function(c, f_acm_multi);
  172. if (ret)
  173. goto err_conf;
  174. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  175. if (ret < 0)
  176. goto err_fsg;
  177. return 0;
  178. err_fsg:
  179. usb_remove_function(c, f_acm_multi);
  180. err_conf:
  181. usb_put_function(f_acm_multi);
  182. err_func_acm:
  183. return ret;
  184. }
  185. static int cdc_config_register(struct usb_composite_dev *cdev)
  186. {
  187. static struct usb_configuration 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, cdc_do_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;
  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. the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac,
  213. qmult);
  214. if (IS_ERR(the_dev))
  215. return PTR_ERR(the_dev);
  216. /* set up serial link layer */
  217. fi_acm = usb_get_function_instance("acm");
  218. if (IS_ERR(fi_acm)) {
  219. status = PTR_ERR(fi_acm);
  220. goto fail0;
  221. }
  222. /* set up mass storage function */
  223. {
  224. void *retp;
  225. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  226. if (IS_ERR(retp)) {
  227. status = PTR_ERR(retp);
  228. goto fail1;
  229. }
  230. }
  231. /* allocate string IDs */
  232. status = usb_string_ids_tab(cdev, strings_dev);
  233. if (unlikely(status < 0))
  234. goto fail2;
  235. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  236. /* register configurations */
  237. status = rndis_config_register(cdev);
  238. if (unlikely(status < 0))
  239. goto fail2;
  240. status = cdc_config_register(cdev);
  241. if (unlikely(status < 0))
  242. goto fail2;
  243. usb_composite_overwrite_options(cdev, &coverwrite);
  244. /* we're done */
  245. dev_info(&gadget->dev, DRIVER_DESC "\n");
  246. fsg_common_put(&fsg_common);
  247. return 0;
  248. /* error recovery */
  249. fail2:
  250. fsg_common_put(&fsg_common);
  251. fail1:
  252. usb_put_function_instance(fi_acm);
  253. fail0:
  254. gether_cleanup(the_dev);
  255. return status;
  256. }
  257. static int __exit multi_unbind(struct usb_composite_dev *cdev)
  258. {
  259. #ifdef CONFIG_USB_G_MULTI_CDC
  260. usb_put_function(f_acm_multi);
  261. #endif
  262. #ifdef USB_ETH_RNDIS
  263. usb_put_function(f_acm_rndis);
  264. #endif
  265. usb_put_function_instance(fi_acm);
  266. gether_cleanup(the_dev);
  267. return 0;
  268. }
  269. /****************************** Some noise ******************************/
  270. static __refdata struct usb_composite_driver multi_driver = {
  271. .name = "g_multi",
  272. .dev = &device_desc,
  273. .strings = dev_strings,
  274. .max_speed = USB_SPEED_HIGH,
  275. .bind = multi_bind,
  276. .unbind = __exit_p(multi_unbind),
  277. .needs_serial = 1,
  278. };
  279. static int __init multi_init(void)
  280. {
  281. return usb_composite_probe(&multi_driver);
  282. }
  283. module_init(multi_init);
  284. static void __exit multi_exit(void)
  285. {
  286. usb_composite_unregister(&multi_driver);
  287. }
  288. module_exit(multi_exit);