acm_ms.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include "f_mass_storage.h"
  29. /*-------------------------------------------------------------------------*/
  30. USB_GADGET_COMPOSITE_OPTIONS();
  31. static struct usb_device_descriptor device_desc = {
  32. .bLength = sizeof device_desc,
  33. .bDescriptorType = USB_DT_DEVICE,
  34. .bcdUSB = cpu_to_le16(0x0200),
  35. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  36. .bDeviceSubClass = 2,
  37. .bDeviceProtocol = 1,
  38. /* .bMaxPacketSize0 = f(hardware) */
  39. /* Vendor and product id can be overridden by module parameters. */
  40. .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
  41. .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
  42. /* .bcdDevice = f(hardware) */
  43. /* .iManufacturer = DYNAMIC */
  44. /* .iProduct = DYNAMIC */
  45. /* NO SERIAL NUMBER */
  46. /*.bNumConfigurations = DYNAMIC*/
  47. };
  48. static struct usb_otg_descriptor otg_descriptor = {
  49. .bLength = sizeof otg_descriptor,
  50. .bDescriptorType = USB_DT_OTG,
  51. /*
  52. * REVISIT SRP-only hardware is possible, although
  53. * it would not be called "OTG" ...
  54. */
  55. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  56. };
  57. static const struct usb_descriptor_header *otg_desc[] = {
  58. (struct usb_descriptor_header *) &otg_descriptor,
  59. NULL,
  60. };
  61. /* string IDs are assigned dynamically */
  62. static struct usb_string strings_dev[] = {
  63. [USB_GADGET_MANUFACTURER_IDX].s = "",
  64. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  65. [USB_GADGET_SERIAL_IDX].s = "",
  66. { } /* end of list */
  67. };
  68. static struct usb_gadget_strings stringtab_dev = {
  69. .language = 0x0409, /* en-us */
  70. .strings = strings_dev,
  71. };
  72. static struct usb_gadget_strings *dev_strings[] = {
  73. &stringtab_dev,
  74. NULL,
  75. };
  76. /****************************** Configurations ******************************/
  77. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  78. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  79. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  80. #else
  81. /*
  82. * Number of buffers we will use.
  83. * 2 is usually enough for good buffering pipeline
  84. */
  85. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  86. #endif /* CONFIG_USB_DEBUG */
  87. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  88. /*-------------------------------------------------------------------------*/
  89. static struct usb_function *f_acm;
  90. static struct usb_function_instance *f_acm_inst;
  91. static struct usb_function_instance *fi_msg;
  92. static struct usb_function *f_msg;
  93. /*
  94. * We _always_ have both ACM and mass storage functions.
  95. */
  96. static int __init acm_ms_do_config(struct usb_configuration *c)
  97. {
  98. struct fsg_opts *opts;
  99. int status;
  100. if (gadget_is_otg(c->cdev->gadget)) {
  101. c->descriptors = otg_desc;
  102. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  103. }
  104. opts = fsg_opts_from_func_inst(fi_msg);
  105. f_acm = usb_get_function(f_acm_inst);
  106. if (IS_ERR(f_acm))
  107. return PTR_ERR(f_acm);
  108. f_msg = usb_get_function(fi_msg);
  109. if (IS_ERR(f_msg)) {
  110. status = PTR_ERR(f_msg);
  111. goto put_acm;
  112. }
  113. status = usb_add_function(c, f_acm);
  114. if (status < 0)
  115. goto put_msg;
  116. status = fsg_common_run_thread(opts->common);
  117. if (status)
  118. goto remove_acm;
  119. status = usb_add_function(c, f_msg);
  120. if (status)
  121. goto remove_acm;
  122. return 0;
  123. remove_acm:
  124. usb_remove_function(c, f_acm);
  125. put_msg:
  126. usb_put_function(f_msg);
  127. put_acm:
  128. usb_put_function(f_acm);
  129. return status;
  130. }
  131. static struct usb_configuration acm_ms_config_driver = {
  132. .label = DRIVER_DESC,
  133. .bConfigurationValue = 1,
  134. /* .iConfiguration = DYNAMIC */
  135. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  136. };
  137. /*-------------------------------------------------------------------------*/
  138. static int __init acm_ms_bind(struct usb_composite_dev *cdev)
  139. {
  140. struct usb_gadget *gadget = cdev->gadget;
  141. struct fsg_opts *opts;
  142. struct fsg_config config;
  143. int status;
  144. f_acm_inst = usb_get_function_instance("acm");
  145. if (IS_ERR(f_acm_inst))
  146. return PTR_ERR(f_acm_inst);
  147. fi_msg = usb_get_function_instance("mass_storage");
  148. if (IS_ERR(fi_msg)) {
  149. status = PTR_ERR(fi_msg);
  150. goto fail_get_msg;
  151. }
  152. /* set up mass storage function */
  153. fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
  154. opts = fsg_opts_from_func_inst(fi_msg);
  155. opts->no_configfs = true;
  156. status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
  157. if (status)
  158. goto fail;
  159. status = fsg_common_set_nluns(opts->common, config.nluns);
  160. if (status)
  161. goto fail_set_nluns;
  162. status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
  163. if (status)
  164. goto fail_set_cdev;
  165. fsg_common_set_sysfs(opts->common, true);
  166. status = fsg_common_create_luns(opts->common, &config);
  167. if (status)
  168. goto fail_set_cdev;
  169. fsg_common_set_inquiry_string(opts->common, config.vendor_name,
  170. config.product_name);
  171. /*
  172. * Allocate string descriptor numbers ... note that string
  173. * contents can be overridden by the composite_dev glue.
  174. */
  175. status = usb_string_ids_tab(cdev, strings_dev);
  176. if (status < 0)
  177. goto fail_string_ids;
  178. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  179. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  180. /* register our configuration */
  181. status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
  182. if (status < 0)
  183. goto fail_string_ids;
  184. usb_composite_overwrite_options(cdev, &coverwrite);
  185. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  186. DRIVER_DESC);
  187. return 0;
  188. /* error recovery */
  189. fail_string_ids:
  190. fsg_common_remove_luns(opts->common);
  191. fail_set_cdev:
  192. fsg_common_free_luns(opts->common);
  193. fail_set_nluns:
  194. fsg_common_free_buffers(opts->common);
  195. fail:
  196. usb_put_function_instance(fi_msg);
  197. fail_get_msg:
  198. usb_put_function_instance(f_acm_inst);
  199. return status;
  200. }
  201. static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
  202. {
  203. usb_put_function(f_msg);
  204. usb_put_function_instance(fi_msg);
  205. usb_put_function(f_acm);
  206. usb_put_function_instance(f_acm_inst);
  207. return 0;
  208. }
  209. static __refdata struct usb_composite_driver acm_ms_driver = {
  210. .name = "g_acm_ms",
  211. .dev = &device_desc,
  212. .max_speed = USB_SPEED_SUPER,
  213. .strings = dev_strings,
  214. .bind = acm_ms_bind,
  215. .unbind = __exit_p(acm_ms_unbind),
  216. };
  217. MODULE_DESCRIPTION(DRIVER_DESC);
  218. MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
  219. MODULE_LICENSE("GPL v2");
  220. static int __init init(void)
  221. {
  222. return usb_composite_probe(&acm_ms_driver);
  223. }
  224. module_init(init);
  225. static void __exit cleanup(void)
  226. {
  227. usb_composite_unregister(&acm_ms_driver);
  228. }
  229. module_exit(cleanup);