multi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/utsname.h>
  25. #if defined USB_ETH_RNDIS
  26. # undef USB_ETH_RNDIS
  27. #endif
  28. #ifdef CONFIG_USB_ETH_RNDIS
  29. # define USB_ETH_RNDIS y
  30. #endif
  31. #define DRIVER_DESC "Multifunction Composite Gadget"
  32. #define DRIVER_VERSION "2009/07/21"
  33. /*-------------------------------------------------------------------------*/
  34. #define MULTI_VENDOR_NUM 0x0525 /* XXX NetChip */
  35. #define MULTI_PRODUCT_NUM 0xa4ab /* XXX */
  36. /*-------------------------------------------------------------------------*/
  37. /*
  38. * kbuild is not very cooperative with respect to linking separately
  39. * compiled library objects into one module. So for now we won't use
  40. * separate compilation ... ensuring init/exit sections work to shrink
  41. * the runtime footprint, and giving us at least some parts of what
  42. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  43. */
  44. #include "composite.c"
  45. #include "usbstring.c"
  46. #include "config.c"
  47. #include "epautoconf.c"
  48. #include "u_serial.c"
  49. #include "f_acm.c"
  50. #include "f_ecm.c"
  51. #include "f_subset.c"
  52. #ifdef USB_ETH_RNDIS
  53. # include "f_rndis.c"
  54. # include "rndis.c"
  55. #endif
  56. #include "u_ether.c"
  57. #undef DBG /* u_ether.c has broken idea about macros */
  58. #undef VDBG /* so clean up after it */
  59. #undef ERROR
  60. #undef INFO
  61. #include "f_mass_storage.c"
  62. /*-------------------------------------------------------------------------*/
  63. static struct usb_device_descriptor device_desc = {
  64. .bLength = sizeof device_desc,
  65. .bDescriptorType = USB_DT_DEVICE,
  66. .bcdUSB = cpu_to_le16(0x0200),
  67. /* .bDeviceClass = USB_CLASS_COMM, */
  68. /* .bDeviceSubClass = 0, */
  69. /* .bDeviceProtocol = 0, */
  70. .bDeviceClass = 0xEF,
  71. .bDeviceSubClass = 2,
  72. .bDeviceProtocol = 1,
  73. /* .bMaxPacketSize0 = f(hardware) */
  74. /* Vendor and product id can be overridden by module parameters. */
  75. .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
  76. .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
  77. /* .bcdDevice = f(hardware) */
  78. /* .iManufacturer = DYNAMIC */
  79. /* .iProduct = DYNAMIC */
  80. /* NO SERIAL NUMBER */
  81. .bNumConfigurations = 1,
  82. };
  83. static struct usb_otg_descriptor otg_descriptor = {
  84. .bLength = sizeof otg_descriptor,
  85. .bDescriptorType = USB_DT_OTG,
  86. /* REVISIT SRP-only hardware is possible, although
  87. * it would not be called "OTG" ...
  88. */
  89. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  90. };
  91. static const struct usb_descriptor_header *otg_desc[] = {
  92. (struct usb_descriptor_header *) &otg_descriptor,
  93. NULL,
  94. };
  95. /* string IDs are assigned dynamically */
  96. #define STRING_MANUFACTURER_IDX 0
  97. #define STRING_PRODUCT_IDX 1
  98. static char manufacturer[50];
  99. static struct usb_string strings_dev[] = {
  100. [STRING_MANUFACTURER_IDX].s = manufacturer,
  101. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  102. { } /* end of list */
  103. };
  104. static struct usb_gadget_strings stringtab_dev = {
  105. .language = 0x0409, /* en-us */
  106. .strings = strings_dev,
  107. };
  108. static struct usb_gadget_strings *dev_strings[] = {
  109. &stringtab_dev,
  110. NULL,
  111. };
  112. static u8 hostaddr[ETH_ALEN];
  113. /****************************** Configurations ******************************/
  114. static struct fsg_module_parameters mod_data = {
  115. .stall = 1
  116. };
  117. FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
  118. static struct fsg_common *fsg_common;
  119. #ifdef USB_ETH_RNDIS
  120. static int __init rndis_do_config(struct usb_configuration *c)
  121. {
  122. int ret;
  123. if (gadget_is_otg(c->cdev->gadget)) {
  124. c->descriptors = otg_desc;
  125. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  126. }
  127. ret = rndis_bind_config(c, hostaddr);
  128. if (ret < 0)
  129. return ret;
  130. ret = acm_bind_config(c, 0);
  131. if (ret < 0)
  132. return ret;
  133. ret = fsg_add(c->cdev, c, fsg_common);
  134. if (ret < 0)
  135. return ret;
  136. return 0;
  137. }
  138. static struct usb_configuration rndis_config_driver = {
  139. .label = "Multifunction Composite (RNDIS + MS + ACM)",
  140. .bind = rndis_do_config,
  141. .bConfigurationValue = 2,
  142. /* .iConfiguration = DYNAMIC */
  143. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  144. };
  145. #endif
  146. #ifdef CONFIG_USB_G_MULTI_CDC
  147. static int __init cdc_do_config(struct usb_configuration *c)
  148. {
  149. int ret;
  150. if (gadget_is_otg(c->cdev->gadget)) {
  151. c->descriptors = otg_desc;
  152. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  153. }
  154. ret = ecm_bind_config(c, hostaddr);
  155. if (ret < 0)
  156. return ret;
  157. ret = acm_bind_config(c, 0);
  158. if (ret < 0)
  159. return ret;
  160. ret = fsg_add(c->cdev, c, fsg_common);
  161. if (ret < 0)
  162. return ret;
  163. if (ret < 0)
  164. return ret;
  165. return 0;
  166. }
  167. static struct usb_configuration cdc_config_driver = {
  168. .label = "Multifunction Composite (CDC + MS + ACM)",
  169. .bind = cdc_do_config,
  170. .bConfigurationValue = 1,
  171. /* .iConfiguration = DYNAMIC */
  172. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  173. };
  174. #endif
  175. /****************************** Gadget Bind ******************************/
  176. static int __init multi_bind(struct usb_composite_dev *cdev)
  177. {
  178. struct usb_gadget *gadget = cdev->gadget;
  179. int status, gcnum;
  180. if (!can_support_ecm(cdev->gadget)) {
  181. dev_err(&gadget->dev, "controller '%s' not usable\n",
  182. gadget->name);
  183. return -EINVAL;
  184. }
  185. /* set up network link layer */
  186. status = gether_setup(cdev->gadget, hostaddr);
  187. if (status < 0)
  188. return status;
  189. /* set up serial link layer */
  190. status = gserial_setup(cdev->gadget, 1);
  191. if (status < 0)
  192. goto fail0;
  193. /* set up mass storage function */
  194. fsg_common = fsg_common_from_params(0, cdev, &mod_data);
  195. if (IS_ERR(fsg_common)) {
  196. status = PTR_ERR(fsg_common);
  197. goto fail1;
  198. }
  199. gcnum = usb_gadget_controller_number(gadget);
  200. if (gcnum >= 0)
  201. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  202. else {
  203. /* We assume that can_support_ecm() tells the truth;
  204. * but if the controller isn't recognized at all then
  205. * that assumption is a bit more likely to be wrong.
  206. */
  207. WARNING(cdev, "controller '%s' not recognized\n",
  208. gadget->name);
  209. device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
  210. }
  211. /* Allocate string descriptor numbers ... note that string
  212. * contents can be overridden by the composite_dev glue.
  213. */
  214. /* device descriptor strings: manufacturer, product */
  215. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  216. init_utsname()->sysname, init_utsname()->release,
  217. gadget->name);
  218. status = usb_string_id(cdev);
  219. if (status < 0)
  220. goto fail2;
  221. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  222. device_desc.iManufacturer = status;
  223. status = usb_string_id(cdev);
  224. if (status < 0)
  225. goto fail2;
  226. strings_dev[STRING_PRODUCT_IDX].id = status;
  227. device_desc.iProduct = status;
  228. #ifdef USB_ETH_RNDIS
  229. /* register our first configuration */
  230. status = usb_add_config(cdev, &rndis_config_driver);
  231. if (status < 0)
  232. goto fail2;
  233. #endif
  234. #ifdef CONFIG_USB_G_MULTI_CDC
  235. /* register our second configuration */
  236. status = usb_add_config(cdev, &cdc_config_driver);
  237. if (status < 0)
  238. goto fail2;
  239. #endif
  240. dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  241. fsg_common_put(fsg_common);
  242. return 0;
  243. fail2:
  244. fsg_common_put(fsg_common);
  245. fail1:
  246. gserial_cleanup();
  247. fail0:
  248. gether_cleanup();
  249. return status;
  250. }
  251. static int __exit multi_unbind(struct usb_composite_dev *cdev)
  252. {
  253. gserial_cleanup();
  254. gether_cleanup();
  255. return 0;
  256. }
  257. /****************************** Some noise ******************************/
  258. static struct usb_composite_driver multi_driver = {
  259. .name = "g_multi",
  260. .dev = &device_desc,
  261. .strings = dev_strings,
  262. .bind = multi_bind,
  263. .unbind = __exit_p(multi_unbind),
  264. };
  265. MODULE_DESCRIPTION(DRIVER_DESC);
  266. MODULE_AUTHOR("Michal Nazarewicz");
  267. MODULE_LICENSE("GPL");
  268. static int __init g_multi_init(void)
  269. {
  270. return usb_composite_register(&multi_driver);
  271. }
  272. module_init(g_multi_init);
  273. static void __exit g_multi_cleanup(void)
  274. {
  275. usb_composite_unregister(&multi_driver);
  276. }
  277. module_exit(g_multi_cleanup);