multi.c 7.6 KB

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