cdc2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. static struct usb_device_descriptor device_desc = {
  38. .bLength = sizeof device_desc,
  39. .bDescriptorType = USB_DT_DEVICE,
  40. .bcdUSB = __constant_cpu_to_le16(0x0200),
  41. .bDeviceClass = USB_CLASS_COMM,
  42. .bDeviceSubClass = 0,
  43. .bDeviceProtocol = 0,
  44. /* .bMaxPacketSize0 = f(hardware) */
  45. /* Vendor and product id can be overridden by module parameters. */
  46. .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
  47. .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
  48. /* .bcdDevice = f(hardware) */
  49. /* .iManufacturer = DYNAMIC */
  50. /* .iProduct = DYNAMIC */
  51. /* NO SERIAL NUMBER */
  52. .bNumConfigurations = 1,
  53. };
  54. static struct usb_otg_descriptor otg_descriptor = {
  55. .bLength = sizeof otg_descriptor,
  56. .bDescriptorType = USB_DT_OTG,
  57. /* REVISIT SRP-only hardware is possible, although
  58. * it would not be called "OTG" ...
  59. */
  60. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  61. };
  62. static const struct usb_descriptor_header *otg_desc[] = {
  63. (struct usb_descriptor_header *) &otg_descriptor,
  64. NULL,
  65. };
  66. /* string IDs are assigned dynamically */
  67. #define STRING_MANUFACTURER_IDX 0
  68. #define STRING_PRODUCT_IDX 1
  69. static char manufacturer[50];
  70. static struct usb_string strings_dev[] = {
  71. [STRING_MANUFACTURER_IDX].s = manufacturer,
  72. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  73. { } /* end of list */
  74. };
  75. static struct usb_gadget_strings stringtab_dev = {
  76. .language = 0x0409, /* en-us */
  77. .strings = strings_dev,
  78. };
  79. static struct usb_gadget_strings *dev_strings[] = {
  80. &stringtab_dev,
  81. NULL,
  82. };
  83. static u8 hostaddr[ETH_ALEN];
  84. /*-------------------------------------------------------------------------*/
  85. /*
  86. * We _always_ have both CDC ECM and CDC ACM functions.
  87. */
  88. static int __init cdc_do_config(struct usb_configuration *c)
  89. {
  90. int status;
  91. if (gadget_is_otg(c->cdev->gadget)) {
  92. c->descriptors = otg_desc;
  93. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  94. }
  95. status = ecm_bind_config(c, hostaddr);
  96. if (status < 0)
  97. return status;
  98. status = acm_bind_config(c, 0);
  99. if (status < 0)
  100. return status;
  101. return 0;
  102. }
  103. static struct usb_configuration cdc_config_driver = {
  104. .label = "CDC Composite (ECM + ACM)",
  105. .bind = cdc_do_config,
  106. .bConfigurationValue = 1,
  107. /* .iConfiguration = DYNAMIC */
  108. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  109. .bMaxPower = 1, /* 2 mA, minimal */
  110. };
  111. /*-------------------------------------------------------------------------*/
  112. static int __init cdc_bind(struct usb_composite_dev *cdev)
  113. {
  114. int gcnum;
  115. struct usb_gadget *gadget = cdev->gadget;
  116. int status;
  117. if (!can_support_ecm(cdev->gadget)) {
  118. ERROR(cdev, "controller '%s' not usable\n", gadget->name);
  119. return -EINVAL;
  120. }
  121. /* set up network link layer */
  122. status = gether_setup(cdev->gadget, hostaddr);
  123. if (status < 0)
  124. return status;
  125. /* set up serial link layer */
  126. status = gserial_setup(cdev->gadget, 1);
  127. if (status < 0)
  128. goto fail0;
  129. gcnum = usb_gadget_controller_number(gadget);
  130. if (gcnum >= 0)
  131. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  132. else {
  133. /* We assume that can_support_ecm() tells the truth;
  134. * but if the controller isn't recognized at all then
  135. * that assumption is a bit more likely to be wrong.
  136. */
  137. WARNING(cdev, "controller '%s' not recognized; trying %s\n",
  138. gadget->name,
  139. cdc_config_driver.label);
  140. device_desc.bcdDevice =
  141. __constant_cpu_to_le16(0x0300 | 0x0099);
  142. }
  143. /* Allocate string descriptor numbers ... note that string
  144. * contents can be overridden by the composite_dev glue.
  145. */
  146. /* device descriptor strings: manufacturer, product */
  147. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  148. init_utsname()->sysname, init_utsname()->release,
  149. gadget->name);
  150. status = usb_string_id(cdev);
  151. if (status < 0)
  152. goto fail1;
  153. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  154. device_desc.iManufacturer = status;
  155. status = usb_string_id(cdev);
  156. if (status < 0)
  157. goto fail1;
  158. strings_dev[STRING_PRODUCT_IDX].id = status;
  159. device_desc.iProduct = status;
  160. /* register our configuration */
  161. status = usb_add_config(cdev, &cdc_config_driver);
  162. if (status < 0)
  163. goto fail1;
  164. INFO(cdev, "%s, version: " DRIVER_VERSION "\n", DRIVER_DESC);
  165. return 0;
  166. fail1:
  167. gserial_cleanup();
  168. fail0:
  169. gether_cleanup();
  170. return status;
  171. }
  172. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  173. {
  174. gserial_cleanup();
  175. gether_cleanup();
  176. return 0;
  177. }
  178. static struct usb_composite_driver cdc_driver = {
  179. .name = "g_cdc",
  180. .dev = &device_desc,
  181. .strings = dev_strings,
  182. .bind = cdc_bind,
  183. .unbind = __exit_p(cdc_unbind),
  184. };
  185. MODULE_DESCRIPTION(DRIVER_DESC);
  186. MODULE_AUTHOR("David Brownell");
  187. MODULE_LICENSE("GPL");
  188. static int __init init(void)
  189. {
  190. return usb_composite_register(&cdc_driver);
  191. }
  192. module_init(init);
  193. static void __exit cleanup(void)
  194. {
  195. usb_composite_unregister(&cdc_driver);
  196. }
  197. module_exit(cleanup);