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