multi.c 8.6 KB

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