acm_ms.c 6.4 KB

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