multi.c 7.6 KB

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