acm_ms.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "u_serial.h"
  18. #define DRIVER_DESC "Composite Gadget (ACM + MS)"
  19. #define DRIVER_VERSION "2011/10/10"
  20. /*-------------------------------------------------------------------------*/
  21. /*
  22. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  23. * Instead: allocate your own, using normal USB-IF procedures.
  24. */
  25. #define ACM_MS_VENDOR_NUM 0x1d6b /* Linux Foundation */
  26. #define ACM_MS_PRODUCT_NUM 0x0106 /* Composite Gadget: ACM + MS*/
  27. /*-------------------------------------------------------------------------*/
  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 "u_serial.c"
  37. #include "f_acm.c"
  38. #include "f_mass_storage.c"
  39. /*-------------------------------------------------------------------------*/
  40. USB_GADGET_COMPOSITE_OPTIONS();
  41. static struct usb_device_descriptor device_desc = {
  42. .bLength = sizeof device_desc,
  43. .bDescriptorType = USB_DT_DEVICE,
  44. .bcdUSB = cpu_to_le16(0x0200),
  45. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  46. .bDeviceSubClass = 2,
  47. .bDeviceProtocol = 1,
  48. /* .bMaxPacketSize0 = f(hardware) */
  49. /* Vendor and product id can be overridden by module parameters. */
  50. .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
  51. .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
  52. /* .bcdDevice = f(hardware) */
  53. /* .iManufacturer = DYNAMIC */
  54. /* .iProduct = DYNAMIC */
  55. /* NO SERIAL NUMBER */
  56. /*.bNumConfigurations = DYNAMIC*/
  57. };
  58. static struct usb_otg_descriptor otg_descriptor = {
  59. .bLength = sizeof otg_descriptor,
  60. .bDescriptorType = USB_DT_OTG,
  61. /*
  62. * REVISIT SRP-only hardware is possible, although
  63. * it would not be called "OTG" ...
  64. */
  65. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  66. };
  67. static const struct usb_descriptor_header *otg_desc[] = {
  68. (struct usb_descriptor_header *) &otg_descriptor,
  69. NULL,
  70. };
  71. /* string IDs are assigned dynamically */
  72. static struct usb_string strings_dev[] = {
  73. [USB_GADGET_MANUFACTURER_IDX].s = "",
  74. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  75. [USB_GADGET_SERIAL_IDX].s = "",
  76. { } /* end of list */
  77. };
  78. static struct usb_gadget_strings stringtab_dev = {
  79. .language = 0x0409, /* en-us */
  80. .strings = strings_dev,
  81. };
  82. static struct usb_gadget_strings *dev_strings[] = {
  83. &stringtab_dev,
  84. NULL,
  85. };
  86. /****************************** Configurations ******************************/
  87. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  88. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  89. static struct fsg_common fsg_common;
  90. /*-------------------------------------------------------------------------*/
  91. /*
  92. * We _always_ have both ACM and mass storage functions.
  93. */
  94. static int __init acm_ms_do_config(struct usb_configuration *c)
  95. {
  96. int status;
  97. if (gadget_is_otg(c->cdev->gadget)) {
  98. c->descriptors = otg_desc;
  99. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  100. }
  101. status = acm_bind_config(c, 0);
  102. if (status < 0)
  103. return status;
  104. status = fsg_bind_config(c->cdev, c, &fsg_common);
  105. if (status < 0)
  106. return status;
  107. return 0;
  108. }
  109. static struct usb_configuration acm_ms_config_driver = {
  110. .label = DRIVER_DESC,
  111. .bConfigurationValue = 1,
  112. /* .iConfiguration = DYNAMIC */
  113. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  114. };
  115. /*-------------------------------------------------------------------------*/
  116. static int __init acm_ms_bind(struct usb_composite_dev *cdev)
  117. {
  118. int gcnum;
  119. struct usb_gadget *gadget = cdev->gadget;
  120. int status;
  121. void *retp;
  122. /* set up serial link layer */
  123. status = gserial_setup(cdev->gadget, 1);
  124. if (status < 0)
  125. return status;
  126. /* set up mass storage function */
  127. retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
  128. if (IS_ERR(retp)) {
  129. status = PTR_ERR(retp);
  130. goto fail0;
  131. }
  132. /* set bcdDevice */
  133. gcnum = usb_gadget_controller_number(gadget);
  134. if (gcnum >= 0) {
  135. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  136. } else {
  137. WARNING(cdev, "controller '%s' not recognized; trying %s\n",
  138. gadget->name,
  139. acm_ms_config_driver.label);
  140. device_desc.bcdDevice =
  141. cpu_to_le16(0x0300 | 0x0099);
  142. }
  143. /*
  144. * Allocate string descriptor numbers ... note that string
  145. * contents can be overridden by the composite_dev glue.
  146. */
  147. status = usb_string_ids_tab(cdev, strings_dev);
  148. if (status < 0)
  149. goto fail1;
  150. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  151. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  152. /* register our configuration */
  153. status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
  154. if (status < 0)
  155. goto fail1;
  156. usb_composite_overwrite_options(cdev, &coverwrite);
  157. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  158. DRIVER_DESC);
  159. fsg_common_put(&fsg_common);
  160. return 0;
  161. /* error recovery */
  162. fail1:
  163. fsg_common_put(&fsg_common);
  164. fail0:
  165. gserial_cleanup();
  166. return status;
  167. }
  168. static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
  169. {
  170. gserial_cleanup();
  171. return 0;
  172. }
  173. static __refdata struct usb_composite_driver acm_ms_driver = {
  174. .name = "g_acm_ms",
  175. .dev = &device_desc,
  176. .max_speed = USB_SPEED_SUPER,
  177. .strings = dev_strings,
  178. .bind = acm_ms_bind,
  179. .unbind = __exit_p(acm_ms_unbind),
  180. };
  181. MODULE_DESCRIPTION(DRIVER_DESC);
  182. MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
  183. MODULE_LICENSE("GPL v2");
  184. static int __init init(void)
  185. {
  186. return usb_composite_probe(&acm_ms_driver);
  187. }
  188. module_init(init);
  189. static void __exit cleanup(void)
  190. {
  191. usb_composite_unregister(&acm_ms_driver);
  192. }
  193. module_exit(cleanup);