acm_ms.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * acm_ms.c -- Composite driver, with ACM and mass storage support
  3. *
  4. * Copyright (C) 2008 David Brownell
  5. * Copyright (C) 2008 Nokia Corporation
  6. * Author: David Brownell
  7. * Modified: Klaus Schwarzkopf <schwarzkopf@sensortherm.de>
  8. *
  9. * Heavily based on multi.c and cdc2.c
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include "u_serial.h"
  19. #define DRIVER_DESC "Composite Gadget (ACM + MS)"
  20. #define DRIVER_VERSION "2011/10/10"
  21. /*-------------------------------------------------------------------------*/
  22. /*
  23. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  24. * Instead: allocate your own, using normal USB-IF procedures.
  25. */
  26. #define ACM_MS_VENDOR_NUM 0x1d6b /* Linux Foundation */
  27. #define ACM_MS_PRODUCT_NUM 0x0106 /* Composite Gadget: ACM + MS*/
  28. /*-------------------------------------------------------------------------*/
  29. /*
  30. * Kbuild is not very cooperative with respect to linking separately
  31. * compiled library objects into one module. So for now we won't use
  32. * separate compilation ... ensuring init/exit sections work to shrink
  33. * the runtime footprint, and giving us at least some parts of what
  34. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  35. */
  36. #include "f_mass_storage.c"
  37. /*-------------------------------------------------------------------------*/
  38. USB_GADGET_COMPOSITE_OPTIONS();
  39. static struct usb_device_descriptor device_desc = {
  40. .bLength = sizeof device_desc,
  41. .bDescriptorType = USB_DT_DEVICE,
  42. .bcdUSB = cpu_to_le16(0x0200),
  43. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  44. .bDeviceSubClass = 2,
  45. .bDeviceProtocol = 1,
  46. /* .bMaxPacketSize0 = f(hardware) */
  47. /* Vendor and product id can be overridden by module parameters. */
  48. .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
  49. .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
  50. /* .bcdDevice = f(hardware) */
  51. /* .iManufacturer = DYNAMIC */
  52. /* .iProduct = DYNAMIC */
  53. /* NO SERIAL NUMBER */
  54. /*.bNumConfigurations = DYNAMIC*/
  55. };
  56. static struct usb_otg_descriptor otg_descriptor = {
  57. .bLength = sizeof otg_descriptor,
  58. .bDescriptorType = USB_DT_OTG,
  59. /*
  60. * REVISIT SRP-only hardware is possible, although
  61. * it would not be called "OTG" ...
  62. */
  63. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  64. };
  65. static const struct usb_descriptor_header *otg_desc[] = {
  66. (struct usb_descriptor_header *) &otg_descriptor,
  67. NULL,
  68. };
  69. /* string IDs are assigned dynamically */
  70. static struct usb_string strings_dev[] = {
  71. [USB_GADGET_MANUFACTURER_IDX].s = "",
  72. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  73. [USB_GADGET_SERIAL_IDX].s = "",
  74. { } /* end of list */
  75. };
  76. static struct usb_gadget_strings stringtab_dev = {
  77. .language = 0x0409, /* en-us */
  78. .strings = strings_dev,
  79. };
  80. static struct usb_gadget_strings *dev_strings[] = {
  81. &stringtab_dev,
  82. NULL,
  83. };
  84. /****************************** Configurations ******************************/
  85. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  86. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  87. static struct fsg_common fsg_common;
  88. /*-------------------------------------------------------------------------*/
  89. static unsigned char tty_line;
  90. static struct usb_function *f_acm;
  91. static struct usb_function_instance *f_acm_inst;
  92. /*
  93. * We _always_ have both ACM and mass storage functions.
  94. */
  95. static int __init acm_ms_do_config(struct usb_configuration *c)
  96. {
  97. struct f_serial_opts *opts;
  98. int status;
  99. if (gadget_is_otg(c->cdev->gadget)) {
  100. c->descriptors = otg_desc;
  101. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  102. }
  103. f_acm_inst = usb_get_function_instance("acm");
  104. if (IS_ERR(f_acm_inst))
  105. return PTR_ERR(f_acm_inst);
  106. opts = container_of(f_acm_inst, struct f_serial_opts, func_inst);
  107. opts->port_num = tty_line;
  108. f_acm = usb_get_function(f_acm_inst);
  109. if (IS_ERR(f_acm)) {
  110. status = PTR_ERR(f_acm);
  111. goto err_func;
  112. }
  113. status = usb_add_function(c, f_acm);
  114. if (status < 0)
  115. goto err_conf;
  116. status = fsg_bind_config(c->cdev, c, &fsg_common);
  117. if (status < 0)
  118. goto err_fsg;
  119. return 0;
  120. err_fsg:
  121. usb_remove_function(c, f_acm);
  122. err_conf:
  123. usb_put_function(f_acm);
  124. err_func:
  125. usb_put_function_instance(f_acm_inst);
  126. return status;
  127. }
  128. static struct usb_configuration acm_ms_config_driver = {
  129. .label = DRIVER_DESC,
  130. .bConfigurationValue = 1,
  131. /* .iConfiguration = DYNAMIC */
  132. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  133. };
  134. /*-------------------------------------------------------------------------*/
  135. static int __init acm_ms_bind(struct usb_composite_dev *cdev)
  136. {
  137. struct usb_gadget *gadget = cdev->gadget;
  138. int status;
  139. void *retp;
  140. /* set up serial link layer */
  141. status = gserial_alloc_line(&tty_line);
  142. if (status < 0)
  143. return status;
  144. /* set up mass storage function */
  145. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  146. if (IS_ERR(retp)) {
  147. status = PTR_ERR(retp);
  148. goto fail0;
  149. }
  150. /*
  151. * Allocate string descriptor numbers ... note that string
  152. * contents can be overridden by the composite_dev glue.
  153. */
  154. status = usb_string_ids_tab(cdev, strings_dev);
  155. if (status < 0)
  156. goto fail1;
  157. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  158. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  159. /* register our configuration */
  160. status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
  161. if (status < 0)
  162. goto fail1;
  163. usb_composite_overwrite_options(cdev, &coverwrite);
  164. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  165. DRIVER_DESC);
  166. fsg_common_put(&fsg_common);
  167. return 0;
  168. /* error recovery */
  169. fail1:
  170. fsg_common_put(&fsg_common);
  171. fail0:
  172. gserial_free_line(tty_line);
  173. return status;
  174. }
  175. static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
  176. {
  177. usb_put_function(f_acm);
  178. usb_put_function_instance(f_acm_inst);
  179. gserial_free_line(tty_line);
  180. return 0;
  181. }
  182. static __refdata struct usb_composite_driver acm_ms_driver = {
  183. .name = "g_acm_ms",
  184. .dev = &device_desc,
  185. .max_speed = USB_SPEED_SUPER,
  186. .strings = dev_strings,
  187. .bind = acm_ms_bind,
  188. .unbind = __exit_p(acm_ms_unbind),
  189. };
  190. MODULE_DESCRIPTION(DRIVER_DESC);
  191. MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
  192. MODULE_LICENSE("GPL v2");
  193. static int __init init(void)
  194. {
  195. return usb_composite_probe(&acm_ms_driver);
  196. }
  197. module_init(init);
  198. static void __exit cleanup(void)
  199. {
  200. usb_composite_unregister(&acm_ms_driver);
  201. }
  202. module_exit(cleanup);