acm_ms.c 6.4 KB

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