multi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #if defined USB_ETH_RNDIS
  17. # undef USB_ETH_RNDIS
  18. #endif
  19. #ifdef CONFIG_USB_G_MULTI_RNDIS
  20. # define USB_ETH_RNDIS y
  21. #endif
  22. #define DRIVER_DESC "Multifunction Composite Gadget"
  23. MODULE_DESCRIPTION(DRIVER_DESC);
  24. MODULE_AUTHOR("Michal Nazarewicz");
  25. MODULE_LICENSE("GPL");
  26. /***************************** All the files... *****************************/
  27. /*
  28. * kbuild is not very cooperative with respect to linking separately
  29. * compiled library objects into one module. So for now we won't use
  30. * separate compilation ... ensuring init/exit sections work to shrink
  31. * the runtime footprint, and giving us at least some parts of what
  32. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  33. */
  34. #include "f_mass_storage.c"
  35. #include "u_serial.c"
  36. #include "f_acm.c"
  37. #include "f_ecm.c"
  38. #include "f_subset.c"
  39. #ifdef USB_ETH_RNDIS
  40. # include "f_rndis.c"
  41. # include "rndis.c"
  42. #endif
  43. #include "u_ether.c"
  44. USB_GADGET_COMPOSITE_OPTIONS();
  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 hostaddr[ETH_ALEN];
  104. /********** RNDIS **********/
  105. #ifdef USB_ETH_RNDIS
  106. static __init int rndis_do_config(struct usb_configuration *c)
  107. {
  108. int ret;
  109. if (gadget_is_otg(c->cdev->gadget)) {
  110. c->descriptors = otg_desc;
  111. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  112. }
  113. ret = rndis_bind_config(c, hostaddr);
  114. if (ret < 0)
  115. return ret;
  116. ret = acm_bind_config(c, 0);
  117. if (ret < 0)
  118. return ret;
  119. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  120. if (ret < 0)
  121. return ret;
  122. return 0;
  123. }
  124. static int rndis_config_register(struct usb_composite_dev *cdev)
  125. {
  126. static struct usb_configuration config = {
  127. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  128. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  129. };
  130. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  131. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  132. return usb_add_config(cdev, &config, rndis_do_config);
  133. }
  134. #else
  135. static int rndis_config_register(struct usb_composite_dev *cdev)
  136. {
  137. return 0;
  138. }
  139. #endif
  140. /********** CDC ECM **********/
  141. #ifdef CONFIG_USB_G_MULTI_CDC
  142. static __init int cdc_do_config(struct usb_configuration *c)
  143. {
  144. int ret;
  145. if (gadget_is_otg(c->cdev->gadget)) {
  146. c->descriptors = otg_desc;
  147. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  148. }
  149. ret = ecm_bind_config(c, hostaddr);
  150. if (ret < 0)
  151. return ret;
  152. ret = acm_bind_config(c, 0);
  153. if (ret < 0)
  154. return ret;
  155. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  156. if (ret < 0)
  157. return ret;
  158. return 0;
  159. }
  160. static int cdc_config_register(struct usb_composite_dev *cdev)
  161. {
  162. static struct usb_configuration config = {
  163. .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
  164. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  165. };
  166. config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
  167. config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
  168. return usb_add_config(cdev, &config, cdc_do_config);
  169. }
  170. #else
  171. static int cdc_config_register(struct usb_composite_dev *cdev)
  172. {
  173. return 0;
  174. }
  175. #endif
  176. /****************************** Gadget Bind ******************************/
  177. static int __ref multi_bind(struct usb_composite_dev *cdev)
  178. {
  179. struct usb_gadget *gadget = cdev->gadget;
  180. int status;
  181. if (!can_support_ecm(cdev->gadget)) {
  182. dev_err(&gadget->dev, "controller '%s' not usable\n",
  183. gadget->name);
  184. return -EINVAL;
  185. }
  186. /* set up network link layer */
  187. status = gether_setup(cdev->gadget, hostaddr);
  188. if (status < 0)
  189. return status;
  190. /* set up serial link layer */
  191. status = gserial_setup(cdev->gadget, 1);
  192. if (status < 0)
  193. goto fail0;
  194. /* set up mass storage function */
  195. {
  196. void *retp;
  197. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  198. if (IS_ERR(retp)) {
  199. status = PTR_ERR(retp);
  200. goto fail1;
  201. }
  202. }
  203. /* allocate string IDs */
  204. status = usb_string_ids_tab(cdev, strings_dev);
  205. if (unlikely(status < 0))
  206. goto fail2;
  207. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  208. /* register configurations */
  209. status = rndis_config_register(cdev);
  210. if (unlikely(status < 0))
  211. goto fail2;
  212. status = cdc_config_register(cdev);
  213. if (unlikely(status < 0))
  214. goto fail2;
  215. usb_composite_overwrite_options(cdev, &coverwrite);
  216. /* we're done */
  217. dev_info(&gadget->dev, DRIVER_DESC "\n");
  218. fsg_common_put(&fsg_common);
  219. return 0;
  220. /* error recovery */
  221. fail2:
  222. fsg_common_put(&fsg_common);
  223. fail1:
  224. gserial_cleanup();
  225. fail0:
  226. gether_cleanup();
  227. return status;
  228. }
  229. static int __exit multi_unbind(struct usb_composite_dev *cdev)
  230. {
  231. gserial_cleanup();
  232. gether_cleanup();
  233. return 0;
  234. }
  235. /****************************** Some noise ******************************/
  236. static __refdata struct usb_composite_driver multi_driver = {
  237. .name = "g_multi",
  238. .dev = &device_desc,
  239. .strings = dev_strings,
  240. .max_speed = USB_SPEED_HIGH,
  241. .bind = multi_bind,
  242. .unbind = __exit_p(multi_unbind),
  243. .needs_serial = 1,
  244. };
  245. static int __init multi_init(void)
  246. {
  247. return usb_composite_probe(&multi_driver);
  248. }
  249. module_init(multi_init);
  250. static void __exit multi_exit(void)
  251. {
  252. usb_composite_unregister(&multi_driver);
  253. }
  254. module_exit(multi_exit);