multi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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.c"
  41. #endif
  42. #include "u_ether.c"
  43. USB_GADGET_COMPOSITE_OPTIONS();
  44. /***************************** Device Descriptor ****************************/
  45. #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
  46. #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
  47. enum {
  48. __MULTI_NO_CONFIG,
  49. #ifdef CONFIG_USB_G_MULTI_RNDIS
  50. MULTI_RNDIS_CONFIG_NUM,
  51. #endif
  52. #ifdef CONFIG_USB_G_MULTI_CDC
  53. MULTI_CDC_CONFIG_NUM,
  54. #endif
  55. };
  56. static struct usb_device_descriptor device_desc = {
  57. .bLength = sizeof device_desc,
  58. .bDescriptorType = USB_DT_DEVICE,
  59. .bcdUSB = cpu_to_le16(0x0200),
  60. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  61. .bDeviceSubClass = 2,
  62. .bDeviceProtocol = 1,
  63. /* Vendor and product id can be overridden by module parameters. */
  64. .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
  65. .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
  66. };
  67. static const struct usb_descriptor_header *otg_desc[] = {
  68. (struct usb_descriptor_header *) &(struct usb_otg_descriptor){
  69. .bLength = sizeof(struct usb_otg_descriptor),
  70. .bDescriptorType = USB_DT_OTG,
  71. /*
  72. * REVISIT SRP-only hardware is possible, although
  73. * it would not be called "OTG" ...
  74. */
  75. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  76. },
  77. NULL,
  78. };
  79. enum {
  80. MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
  81. MULTI_STRING_CDC_CONFIG_IDX,
  82. };
  83. static struct usb_string strings_dev[] = {
  84. [USB_GADGET_MANUFACTURER_IDX].s = "",
  85. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  86. [USB_GADGET_SERIAL_IDX].s = "",
  87. [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
  88. [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
  89. { } /* end of list */
  90. };
  91. static struct usb_gadget_strings *dev_strings[] = {
  92. &(struct usb_gadget_strings){
  93. .language = 0x0409, /* en-us */
  94. .strings = strings_dev,
  95. },
  96. NULL,
  97. };
  98. /****************************** Configurations ******************************/
  99. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  100. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  101. static struct fsg_common fsg_common;
  102. static u8 hostaddr[ETH_ALEN];
  103. static unsigned char tty_line;
  104. static struct usb_function_instance *fi_acm;
  105. /********** RNDIS **********/
  106. #ifdef USB_ETH_RNDIS
  107. static struct usb_function *f_acm_rndis;
  108. static __init int rndis_do_config(struct usb_configuration *c)
  109. {
  110. int ret;
  111. if (gadget_is_otg(c->cdev->gadget)) {
  112. c->descriptors = otg_desc;
  113. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  114. }
  115. ret = rndis_bind_config(c, hostaddr);
  116. if (ret < 0)
  117. return ret;
  118. f_acm_rndis = usb_get_function(fi_acm);
  119. if (IS_ERR(f_acm_rndis))
  120. goto err_func_acm;
  121. ret = usb_add_function(c, f_acm_rndis);
  122. if (ret)
  123. goto err_conf;
  124. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  125. if (ret < 0)
  126. goto err_fsg;
  127. return 0;
  128. err_fsg:
  129. usb_remove_function(c, f_acm_rndis);
  130. err_conf:
  131. usb_put_function(f_acm_rndis);
  132. err_func_acm:
  133. return ret;
  134. }
  135. static int rndis_config_register(struct usb_composite_dev *cdev)
  136. {
  137. static struct usb_configuration config = {
  138. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  139. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  140. };
  141. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  142. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  143. return usb_add_config(cdev, &config, rndis_do_config);
  144. }
  145. #else
  146. static int rndis_config_register(struct usb_composite_dev *cdev)
  147. {
  148. return 0;
  149. }
  150. #endif
  151. /********** CDC ECM **********/
  152. #ifdef CONFIG_USB_G_MULTI_CDC
  153. static struct usb_function *f_acm_multi;
  154. static __init int cdc_do_config(struct usb_configuration *c)
  155. {
  156. int ret;
  157. if (gadget_is_otg(c->cdev->gadget)) {
  158. c->descriptors = otg_desc;
  159. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  160. }
  161. ret = ecm_bind_config(c, hostaddr);
  162. if (ret < 0)
  163. return ret;
  164. /* implicit port_num is zero */
  165. f_acm_multi = usb_get_function(fi_acm);
  166. if (IS_ERR(f_acm_multi))
  167. goto err_func_acm;
  168. ret = usb_add_function(c, f_acm_multi);
  169. if (ret)
  170. goto err_conf;
  171. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  172. if (ret < 0)
  173. goto err_fsg;
  174. return 0;
  175. err_fsg:
  176. usb_remove_function(c, f_acm_multi);
  177. err_conf:
  178. usb_put_function(f_acm_multi);
  179. err_func_acm:
  180. return ret;
  181. }
  182. static int cdc_config_register(struct usb_composite_dev *cdev)
  183. {
  184. static struct usb_configuration config = {
  185. .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
  186. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  187. };
  188. config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
  189. config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
  190. return usb_add_config(cdev, &config, cdc_do_config);
  191. }
  192. #else
  193. static int cdc_config_register(struct usb_composite_dev *cdev)
  194. {
  195. return 0;
  196. }
  197. #endif
  198. /****************************** Gadget Bind ******************************/
  199. static int __ref multi_bind(struct usb_composite_dev *cdev)
  200. {
  201. struct usb_gadget *gadget = cdev->gadget;
  202. struct f_serial_opts *opts;
  203. int status;
  204. if (!can_support_ecm(cdev->gadget)) {
  205. dev_err(&gadget->dev, "controller '%s' not usable\n",
  206. gadget->name);
  207. return -EINVAL;
  208. }
  209. /* set up network link layer */
  210. status = gether_setup(cdev->gadget, hostaddr);
  211. if (status < 0)
  212. return status;
  213. /* set up serial link layer */
  214. status = gserial_alloc_line(&tty_line);
  215. if (status < 0)
  216. goto fail0;
  217. fi_acm = usb_get_function_instance("acm");
  218. if (IS_ERR(fi_acm)) {
  219. status = PTR_ERR(fi_acm);
  220. goto fail0dot5;
  221. }
  222. opts = container_of(fi_acm, struct f_serial_opts, func_inst);
  223. opts->port_num = tty_line;
  224. /* set up mass storage function */
  225. {
  226. void *retp;
  227. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  228. if (IS_ERR(retp)) {
  229. status = PTR_ERR(retp);
  230. goto fail1;
  231. }
  232. }
  233. /* allocate string IDs */
  234. status = usb_string_ids_tab(cdev, strings_dev);
  235. if (unlikely(status < 0))
  236. goto fail2;
  237. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  238. /* register configurations */
  239. status = rndis_config_register(cdev);
  240. if (unlikely(status < 0))
  241. goto fail2;
  242. status = cdc_config_register(cdev);
  243. if (unlikely(status < 0))
  244. goto fail2;
  245. usb_composite_overwrite_options(cdev, &coverwrite);
  246. /* we're done */
  247. dev_info(&gadget->dev, DRIVER_DESC "\n");
  248. fsg_common_put(&fsg_common);
  249. return 0;
  250. /* error recovery */
  251. fail2:
  252. fsg_common_put(&fsg_common);
  253. fail1:
  254. usb_put_function_instance(fi_acm);
  255. fail0dot5:
  256. gserial_free_line(tty_line);
  257. fail0:
  258. gether_cleanup();
  259. return status;
  260. }
  261. static int __exit multi_unbind(struct usb_composite_dev *cdev)
  262. {
  263. #ifdef CONFIG_USB_G_MULTI_CDC
  264. usb_put_function(f_acm_multi);
  265. #endif
  266. #ifdef USB_ETH_RNDIS
  267. usb_put_function(f_acm_rndis);
  268. #endif
  269. usb_put_function_instance(fi_acm);
  270. gserial_free_line(tty_line);
  271. gether_cleanup();
  272. return 0;
  273. }
  274. /****************************** Some noise ******************************/
  275. static __refdata struct usb_composite_driver multi_driver = {
  276. .name = "g_multi",
  277. .dev = &device_desc,
  278. .strings = dev_strings,
  279. .max_speed = USB_SPEED_HIGH,
  280. .bind = multi_bind,
  281. .unbind = __exit_p(multi_unbind),
  282. .needs_serial = 1,
  283. };
  284. static int __init multi_init(void)
  285. {
  286. return usb_composite_probe(&multi_driver);
  287. }
  288. module_init(multi_init);
  289. static void __exit multi_exit(void)
  290. {
  291. usb_composite_unregister(&multi_driver);
  292. }
  293. module_exit(multi_exit);