cdc2.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * cdc2.c -- CDC Composite driver, with ECM and ACM support
  3. *
  4. * Copyright (C) 2008 David Brownell
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/utsname.h>
  23. #include "u_ether.h"
  24. #include "u_serial.h"
  25. #define DRIVER_DESC "CDC Composite Gadget"
  26. #define DRIVER_VERSION "King Kamehameha Day 2008"
  27. /*-------------------------------------------------------------------------*/
  28. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  29. * Instead: allocate your own, using normal USB-IF procedures.
  30. */
  31. /* Thanks to NetChip Technologies for donating this product ID.
  32. * It's for devices with only this composite CDC configuration.
  33. */
  34. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  35. #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
  36. /*-------------------------------------------------------------------------*/
  37. /*
  38. * Kbuild is not very cooperative with respect to linking separately
  39. * compiled library objects into one module. So for now we won't use
  40. * separate compilation ... ensuring init/exit sections work to shrink
  41. * the runtime footprint, and giving us at least some parts of what
  42. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  43. */
  44. #include "composite.c"
  45. #include "usbstring.c"
  46. #include "config.c"
  47. #include "epautoconf.c"
  48. #include "u_serial.c"
  49. #include "f_acm.c"
  50. #include "f_ecm.c"
  51. #include "u_ether.c"
  52. /*-------------------------------------------------------------------------*/
  53. static struct usb_device_descriptor device_desc = {
  54. .bLength = sizeof device_desc,
  55. .bDescriptorType = USB_DT_DEVICE,
  56. .bcdUSB = cpu_to_le16(0x0200),
  57. .bDeviceClass = USB_CLASS_COMM,
  58. .bDeviceSubClass = 0,
  59. .bDeviceProtocol = 0,
  60. /* .bMaxPacketSize0 = f(hardware) */
  61. /* Vendor and product id can be overridden by module parameters. */
  62. .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
  63. .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
  64. /* .bcdDevice = f(hardware) */
  65. /* .iManufacturer = DYNAMIC */
  66. /* .iProduct = DYNAMIC */
  67. /* NO SERIAL NUMBER */
  68. .bNumConfigurations = 1,
  69. };
  70. static struct usb_otg_descriptor otg_descriptor = {
  71. .bLength = sizeof otg_descriptor,
  72. .bDescriptorType = USB_DT_OTG,
  73. /* REVISIT SRP-only hardware is possible, although
  74. * it would not be called "OTG" ...
  75. */
  76. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  77. };
  78. static const struct usb_descriptor_header *otg_desc[] = {
  79. (struct usb_descriptor_header *) &otg_descriptor,
  80. NULL,
  81. };
  82. /* string IDs are assigned dynamically */
  83. #define STRING_MANUFACTURER_IDX 0
  84. #define STRING_PRODUCT_IDX 1
  85. static char manufacturer[50];
  86. static struct usb_string strings_dev[] = {
  87. [STRING_MANUFACTURER_IDX].s = manufacturer,
  88. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  89. { } /* end of list */
  90. };
  91. static struct usb_gadget_strings stringtab_dev = {
  92. .language = 0x0409, /* en-us */
  93. .strings = strings_dev,
  94. };
  95. static struct usb_gadget_strings *dev_strings[] = {
  96. &stringtab_dev,
  97. NULL,
  98. };
  99. static u8 hostaddr[ETH_ALEN];
  100. /*-------------------------------------------------------------------------*/
  101. /*
  102. * We _always_ have both CDC ECM and CDC ACM functions.
  103. */
  104. static int __init cdc_do_config(struct usb_configuration *c)
  105. {
  106. int status;
  107. if (gadget_is_otg(c->cdev->gadget)) {
  108. c->descriptors = otg_desc;
  109. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  110. }
  111. status = ecm_bind_config(c, hostaddr);
  112. if (status < 0)
  113. return status;
  114. status = acm_bind_config(c, 0);
  115. if (status < 0)
  116. return status;
  117. return 0;
  118. }
  119. static struct usb_configuration cdc_config_driver = {
  120. .label = "CDC Composite (ECM + ACM)",
  121. .bConfigurationValue = 1,
  122. /* .iConfiguration = DYNAMIC */
  123. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  124. };
  125. /*-------------------------------------------------------------------------*/
  126. static int __init cdc_bind(struct usb_composite_dev *cdev)
  127. {
  128. int gcnum;
  129. struct usb_gadget *gadget = cdev->gadget;
  130. int status;
  131. if (!can_support_ecm(cdev->gadget)) {
  132. dev_err(&gadget->dev, "controller '%s' not usable\n",
  133. gadget->name);
  134. return -EINVAL;
  135. }
  136. /* set up network link layer */
  137. status = gether_setup(cdev->gadget, hostaddr);
  138. if (status < 0)
  139. return status;
  140. /* set up serial link layer */
  141. status = gserial_setup(cdev->gadget, 1);
  142. if (status < 0)
  143. goto fail0;
  144. gcnum = usb_gadget_controller_number(gadget);
  145. if (gcnum >= 0)
  146. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  147. else {
  148. /* We assume that can_support_ecm() tells the truth;
  149. * but if the controller isn't recognized at all then
  150. * that assumption is a bit more likely to be wrong.
  151. */
  152. WARNING(cdev, "controller '%s' not recognized; trying %s\n",
  153. gadget->name,
  154. cdc_config_driver.label);
  155. device_desc.bcdDevice =
  156. cpu_to_le16(0x0300 | 0x0099);
  157. }
  158. /* Allocate string descriptor numbers ... note that string
  159. * contents can be overridden by the composite_dev glue.
  160. */
  161. /* device descriptor strings: manufacturer, product */
  162. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  163. init_utsname()->sysname, init_utsname()->release,
  164. gadget->name);
  165. status = usb_string_id(cdev);
  166. if (status < 0)
  167. goto fail1;
  168. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  169. device_desc.iManufacturer = status;
  170. status = usb_string_id(cdev);
  171. if (status < 0)
  172. goto fail1;
  173. strings_dev[STRING_PRODUCT_IDX].id = status;
  174. device_desc.iProduct = status;
  175. /* register our configuration */
  176. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  177. if (status < 0)
  178. goto fail1;
  179. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  180. DRIVER_DESC);
  181. return 0;
  182. fail1:
  183. gserial_cleanup();
  184. fail0:
  185. gether_cleanup();
  186. return status;
  187. }
  188. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  189. {
  190. gserial_cleanup();
  191. gether_cleanup();
  192. return 0;
  193. }
  194. static struct usb_composite_driver cdc_driver = {
  195. .name = "g_cdc",
  196. .dev = &device_desc,
  197. .strings = dev_strings,
  198. .unbind = __exit_p(cdc_unbind),
  199. };
  200. MODULE_DESCRIPTION(DRIVER_DESC);
  201. MODULE_AUTHOR("David Brownell");
  202. MODULE_LICENSE("GPL");
  203. static int __init init(void)
  204. {
  205. return usb_composite_probe(&cdc_driver, cdc_bind);
  206. }
  207. module_init(init);
  208. static void __exit cleanup(void)
  209. {
  210. usb_composite_unregister(&cdc_driver);
  211. }
  212. module_exit(cleanup);