cdc2.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include <linux/kernel.h>
  13. #include <linux/utsname.h>
  14. #include <linux/module.h>
  15. #include "u_ether.h"
  16. #include "u_serial.h"
  17. #define DRIVER_DESC "CDC Composite Gadget"
  18. #define DRIVER_VERSION "King Kamehameha Day 2008"
  19. /*-------------------------------------------------------------------------*/
  20. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  21. * Instead: allocate your own, using normal USB-IF procedures.
  22. */
  23. /* Thanks to NetChip Technologies for donating this product ID.
  24. * It's for devices with only this composite CDC configuration.
  25. */
  26. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  27. #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
  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_ecm.c"
  43. #include "u_ether.c"
  44. /*-------------------------------------------------------------------------*/
  45. static struct usb_device_descriptor device_desc = {
  46. .bLength = sizeof device_desc,
  47. .bDescriptorType = USB_DT_DEVICE,
  48. .bcdUSB = cpu_to_le16(0x0200),
  49. .bDeviceClass = USB_CLASS_COMM,
  50. .bDeviceSubClass = 0,
  51. .bDeviceProtocol = 0,
  52. /* .bMaxPacketSize0 = f(hardware) */
  53. /* Vendor and product id can be overridden by module parameters. */
  54. .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
  55. .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
  56. /* .bcdDevice = f(hardware) */
  57. /* .iManufacturer = DYNAMIC */
  58. /* .iProduct = DYNAMIC */
  59. /* NO SERIAL NUMBER */
  60. .bNumConfigurations = 1,
  61. };
  62. static struct usb_otg_descriptor otg_descriptor = {
  63. .bLength = sizeof otg_descriptor,
  64. .bDescriptorType = USB_DT_OTG,
  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. static u8 hostaddr[ETH_ALEN];
  92. /*-------------------------------------------------------------------------*/
  93. /*
  94. * We _always_ have both CDC ECM and CDC ACM functions.
  95. */
  96. static int __init cdc_do_config(struct usb_configuration *c)
  97. {
  98. int status;
  99. if (gadget_is_otg(c->cdev->gadget)) {
  100. c->descriptors = otg_desc;
  101. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  102. }
  103. status = ecm_bind_config(c, hostaddr);
  104. if (status < 0)
  105. return status;
  106. status = acm_bind_config(c, 0);
  107. if (status < 0)
  108. return status;
  109. return 0;
  110. }
  111. static struct usb_configuration cdc_config_driver = {
  112. .label = "CDC Composite (ECM + ACM)",
  113. .bConfigurationValue = 1,
  114. /* .iConfiguration = DYNAMIC */
  115. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  116. };
  117. /*-------------------------------------------------------------------------*/
  118. static int __init cdc_bind(struct usb_composite_dev *cdev)
  119. {
  120. int gcnum;
  121. struct usb_gadget *gadget = cdev->gadget;
  122. int status;
  123. if (!can_support_ecm(cdev->gadget)) {
  124. dev_err(&gadget->dev, "controller '%s' not usable\n",
  125. gadget->name);
  126. return -EINVAL;
  127. }
  128. /* set up network link layer */
  129. status = gether_setup(cdev->gadget, hostaddr);
  130. if (status < 0)
  131. return status;
  132. /* set up serial link layer */
  133. status = gserial_setup(cdev->gadget, 1);
  134. if (status < 0)
  135. goto fail0;
  136. gcnum = usb_gadget_controller_number(gadget);
  137. if (gcnum >= 0)
  138. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  139. else {
  140. /* We assume that can_support_ecm() tells the truth;
  141. * but if the controller isn't recognized at all then
  142. * that assumption is a bit more likely to be wrong.
  143. */
  144. WARNING(cdev, "controller '%s' not recognized; trying %s\n",
  145. gadget->name,
  146. cdc_config_driver.label);
  147. device_desc.bcdDevice =
  148. cpu_to_le16(0x0300 | 0x0099);
  149. }
  150. /* Allocate string descriptor numbers ... note that string
  151. * contents can be overridden by the composite_dev glue.
  152. */
  153. /* device descriptor strings: manufacturer, product */
  154. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  155. init_utsname()->sysname, init_utsname()->release,
  156. gadget->name);
  157. status = usb_string_id(cdev);
  158. if (status < 0)
  159. goto fail1;
  160. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  161. device_desc.iManufacturer = status;
  162. status = usb_string_id(cdev);
  163. if (status < 0)
  164. goto fail1;
  165. strings_dev[STRING_PRODUCT_IDX].id = status;
  166. device_desc.iProduct = status;
  167. /* register our configuration */
  168. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  169. if (status < 0)
  170. goto fail1;
  171. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  172. DRIVER_DESC);
  173. return 0;
  174. fail1:
  175. gserial_cleanup();
  176. fail0:
  177. gether_cleanup();
  178. return status;
  179. }
  180. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  181. {
  182. gserial_cleanup();
  183. gether_cleanup();
  184. return 0;
  185. }
  186. static struct usb_composite_driver cdc_driver = {
  187. .name = "g_cdc",
  188. .dev = &device_desc,
  189. .strings = dev_strings,
  190. .max_speed = USB_SPEED_HIGH,
  191. .unbind = __exit_p(cdc_unbind),
  192. };
  193. MODULE_DESCRIPTION(DRIVER_DESC);
  194. MODULE_AUTHOR("David Brownell");
  195. MODULE_LICENSE("GPL");
  196. static int __init init(void)
  197. {
  198. return usb_composite_probe(&cdc_driver, cdc_bind);
  199. }
  200. module_init(init);
  201. static void __exit cleanup(void)
  202. {
  203. usb_composite_unregister(&cdc_driver);
  204. }
  205. module_exit(cleanup);