multi.c 8.6 KB

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