multi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "composite.c"
  35. #include "f_mass_storage.c"
  36. #include "u_serial.c"
  37. #include "f_acm.c"
  38. #include "f_ecm.c"
  39. #include "f_subset.c"
  40. #ifdef USB_ETH_RNDIS
  41. # include "f_rndis.c"
  42. # include "rndis.c"
  43. #endif
  44. #include "u_ether.c"
  45. USB_GADGET_COMPOSITE_OPTIONS();
  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. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  103. static struct fsg_common fsg_common;
  104. static u8 hostaddr[ETH_ALEN];
  105. /********** RNDIS **********/
  106. #ifdef USB_ETH_RNDIS
  107. static __init int rndis_do_config(struct usb_configuration *c)
  108. {
  109. int ret;
  110. if (gadget_is_otg(c->cdev->gadget)) {
  111. c->descriptors = otg_desc;
  112. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  113. }
  114. ret = rndis_bind_config(c, hostaddr);
  115. if (ret < 0)
  116. return ret;
  117. ret = acm_bind_config(c, 0);
  118. if (ret < 0)
  119. return ret;
  120. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  121. if (ret < 0)
  122. return ret;
  123. return 0;
  124. }
  125. static int rndis_config_register(struct usb_composite_dev *cdev)
  126. {
  127. static struct usb_configuration config = {
  128. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  129. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  130. };
  131. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  132. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  133. return usb_add_config(cdev, &config, rndis_do_config);
  134. }
  135. #else
  136. static int rndis_config_register(struct usb_composite_dev *cdev)
  137. {
  138. return 0;
  139. }
  140. #endif
  141. /********** CDC ECM **********/
  142. #ifdef CONFIG_USB_G_MULTI_CDC
  143. static __init int cdc_do_config(struct usb_configuration *c)
  144. {
  145. int ret;
  146. if (gadget_is_otg(c->cdev->gadget)) {
  147. c->descriptors = otg_desc;
  148. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  149. }
  150. ret = ecm_bind_config(c, hostaddr);
  151. if (ret < 0)
  152. return ret;
  153. ret = acm_bind_config(c, 0);
  154. if (ret < 0)
  155. return ret;
  156. ret = fsg_bind_config(c->cdev, c, &fsg_common);
  157. if (ret < 0)
  158. return ret;
  159. return 0;
  160. }
  161. static int cdc_config_register(struct usb_composite_dev *cdev)
  162. {
  163. static struct usb_configuration config = {
  164. .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
  165. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  166. };
  167. config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
  168. config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
  169. return usb_add_config(cdev, &config, cdc_do_config);
  170. }
  171. #else
  172. static int cdc_config_register(struct usb_composite_dev *cdev)
  173. {
  174. return 0;
  175. }
  176. #endif
  177. /****************************** Gadget Bind ******************************/
  178. static int __ref multi_bind(struct usb_composite_dev *cdev)
  179. {
  180. struct usb_gadget *gadget = cdev->gadget;
  181. int status, gcnum;
  182. if (!can_support_ecm(cdev->gadget)) {
  183. dev_err(&gadget->dev, "controller '%s' not usable\n",
  184. gadget->name);
  185. return -EINVAL;
  186. }
  187. /* set up network link layer */
  188. status = gether_setup(cdev->gadget, hostaddr);
  189. if (status < 0)
  190. return status;
  191. /* set up serial link layer */
  192. status = gserial_setup(cdev->gadget, 1);
  193. if (status < 0)
  194. goto fail0;
  195. /* set up mass storage function */
  196. {
  197. void *retp;
  198. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  199. if (IS_ERR(retp)) {
  200. status = PTR_ERR(retp);
  201. goto fail1;
  202. }
  203. }
  204. /* set bcdDevice */
  205. gcnum = usb_gadget_controller_number(gadget);
  206. if (gcnum >= 0) {
  207. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  208. } else {
  209. WARNING(cdev, "controller '%s' not recognized\n", gadget->name);
  210. device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
  211. }
  212. /* allocate string IDs */
  213. status = usb_string_ids_tab(cdev, strings_dev);
  214. if (unlikely(status < 0))
  215. goto fail2;
  216. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  217. /* register configurations */
  218. status = rndis_config_register(cdev);
  219. if (unlikely(status < 0))
  220. goto fail2;
  221. status = cdc_config_register(cdev);
  222. if (unlikely(status < 0))
  223. goto fail2;
  224. usb_composite_overwrite_options(cdev, &coverwrite);
  225. /* we're done */
  226. dev_info(&gadget->dev, DRIVER_DESC "\n");
  227. fsg_common_put(&fsg_common);
  228. return 0;
  229. /* error recovery */
  230. fail2:
  231. fsg_common_put(&fsg_common);
  232. fail1:
  233. gserial_cleanup();
  234. fail0:
  235. gether_cleanup();
  236. return status;
  237. }
  238. static int __exit multi_unbind(struct usb_composite_dev *cdev)
  239. {
  240. gserial_cleanup();
  241. gether_cleanup();
  242. return 0;
  243. }
  244. /****************************** Some noise ******************************/
  245. static __refdata struct usb_composite_driver multi_driver = {
  246. .name = "g_multi",
  247. .dev = &device_desc,
  248. .strings = dev_strings,
  249. .max_speed = USB_SPEED_HIGH,
  250. .bind = multi_bind,
  251. .unbind = __exit_p(multi_unbind),
  252. .needs_serial = 1,
  253. };
  254. static int __init multi_init(void)
  255. {
  256. return usb_composite_probe(&multi_driver);
  257. }
  258. module_init(multi_init);
  259. static void __exit multi_exit(void)
  260. {
  261. usb_composite_unregister(&multi_driver);
  262. }
  263. module_exit(multi_exit);