cdc2.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/module.h>
  14. #include "u_ether.h"
  15. #include "u_serial.h"
  16. #define DRIVER_DESC "CDC Composite Gadget"
  17. #define DRIVER_VERSION "King Kamehameha Day 2008"
  18. /*-------------------------------------------------------------------------*/
  19. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  20. * Instead: allocate your own, using normal USB-IF procedures.
  21. */
  22. /* Thanks to NetChip Technologies for donating this product ID.
  23. * It's for devices with only this composite CDC configuration.
  24. */
  25. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  26. #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
  27. /*-------------------------------------------------------------------------*/
  28. USB_GADGET_COMPOSITE_OPTIONS();
  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 "u_serial.c"
  37. #include "f_acm.c"
  38. #include "f_ecm.c"
  39. #include "u_ether.c"
  40. /*-------------------------------------------------------------------------*/
  41. static struct usb_device_descriptor device_desc = {
  42. .bLength = sizeof device_desc,
  43. .bDescriptorType = USB_DT_DEVICE,
  44. .bcdUSB = cpu_to_le16(0x0200),
  45. .bDeviceClass = USB_CLASS_COMM,
  46. .bDeviceSubClass = 0,
  47. .bDeviceProtocol = 0,
  48. /* .bMaxPacketSize0 = f(hardware) */
  49. /* Vendor and product id can be overridden by module parameters. */
  50. .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
  51. .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
  52. /* .bcdDevice = f(hardware) */
  53. /* .iManufacturer = DYNAMIC */
  54. /* .iProduct = DYNAMIC */
  55. /* NO SERIAL NUMBER */
  56. .bNumConfigurations = 1,
  57. };
  58. static struct usb_otg_descriptor otg_descriptor = {
  59. .bLength = sizeof otg_descriptor,
  60. .bDescriptorType = USB_DT_OTG,
  61. /* REVISIT SRP-only hardware is possible, although
  62. * it would not be called "OTG" ...
  63. */
  64. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  65. };
  66. static const struct usb_descriptor_header *otg_desc[] = {
  67. (struct usb_descriptor_header *) &otg_descriptor,
  68. NULL,
  69. };
  70. /* string IDs are assigned dynamically */
  71. static struct usb_string strings_dev[] = {
  72. [USB_GADGET_MANUFACTURER_IDX].s = "",
  73. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  74. [USB_GADGET_SERIAL_IDX].s = "",
  75. { } /* end of list */
  76. };
  77. static struct usb_gadget_strings stringtab_dev = {
  78. .language = 0x0409, /* en-us */
  79. .strings = strings_dev,
  80. };
  81. static struct usb_gadget_strings *dev_strings[] = {
  82. &stringtab_dev,
  83. NULL,
  84. };
  85. static u8 hostaddr[ETH_ALEN];
  86. /*-------------------------------------------------------------------------*/
  87. /*
  88. * We _always_ have both CDC ECM and CDC ACM functions.
  89. */
  90. static int __init cdc_do_config(struct usb_configuration *c)
  91. {
  92. int status;
  93. if (gadget_is_otg(c->cdev->gadget)) {
  94. c->descriptors = otg_desc;
  95. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  96. }
  97. status = ecm_bind_config(c, hostaddr);
  98. if (status < 0)
  99. return status;
  100. status = acm_bind_config(c, 0);
  101. if (status < 0)
  102. return status;
  103. return 0;
  104. }
  105. static struct usb_configuration cdc_config_driver = {
  106. .label = "CDC Composite (ECM + ACM)",
  107. .bConfigurationValue = 1,
  108. /* .iConfiguration = DYNAMIC */
  109. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  110. };
  111. /*-------------------------------------------------------------------------*/
  112. static int __init cdc_bind(struct usb_composite_dev *cdev)
  113. {
  114. struct usb_gadget *gadget = cdev->gadget;
  115. int status;
  116. if (!can_support_ecm(cdev->gadget)) {
  117. dev_err(&gadget->dev, "controller '%s' not usable\n",
  118. 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. /* Allocate string descriptor numbers ... note that string
  130. * contents can be overridden by the composite_dev glue.
  131. */
  132. status = usb_string_ids_tab(cdev, strings_dev);
  133. if (status < 0)
  134. goto fail1;
  135. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  136. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  137. /* register our configuration */
  138. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  139. if (status < 0)
  140. goto fail1;
  141. usb_composite_overwrite_options(cdev, &coverwrite);
  142. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  143. DRIVER_DESC);
  144. return 0;
  145. fail1:
  146. gserial_cleanup();
  147. fail0:
  148. gether_cleanup();
  149. return status;
  150. }
  151. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  152. {
  153. gserial_cleanup();
  154. gether_cleanup();
  155. return 0;
  156. }
  157. static __refdata struct usb_composite_driver cdc_driver = {
  158. .name = "g_cdc",
  159. .dev = &device_desc,
  160. .strings = dev_strings,
  161. .max_speed = USB_SPEED_HIGH,
  162. .bind = cdc_bind,
  163. .unbind = __exit_p(cdc_unbind),
  164. };
  165. MODULE_DESCRIPTION(DRIVER_DESC);
  166. MODULE_AUTHOR("David Brownell");
  167. MODULE_LICENSE("GPL");
  168. static int __init init(void)
  169. {
  170. return usb_composite_probe(&cdc_driver);
  171. }
  172. module_init(init);
  173. static void __exit cleanup(void)
  174. {
  175. usb_composite_unregister(&cdc_driver);
  176. }
  177. module_exit(cleanup);