cdc2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "f_ecm.c"
  37. #include "u_ether.c"
  38. /*-------------------------------------------------------------------------*/
  39. static struct usb_device_descriptor device_desc = {
  40. .bLength = sizeof device_desc,
  41. .bDescriptorType = USB_DT_DEVICE,
  42. .bcdUSB = cpu_to_le16(0x0200),
  43. .bDeviceClass = USB_CLASS_COMM,
  44. .bDeviceSubClass = 0,
  45. .bDeviceProtocol = 0,
  46. /* .bMaxPacketSize0 = f(hardware) */
  47. /* Vendor and product id can be overridden by module parameters. */
  48. .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
  49. .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
  50. /* .bcdDevice = f(hardware) */
  51. /* .iManufacturer = DYNAMIC */
  52. /* .iProduct = DYNAMIC */
  53. /* NO SERIAL NUMBER */
  54. .bNumConfigurations = 1,
  55. };
  56. static struct usb_otg_descriptor otg_descriptor = {
  57. .bLength = sizeof otg_descriptor,
  58. .bDescriptorType = USB_DT_OTG,
  59. /* REVISIT SRP-only hardware is possible, although
  60. * it would not be called "OTG" ...
  61. */
  62. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  63. };
  64. static const struct usb_descriptor_header *otg_desc[] = {
  65. (struct usb_descriptor_header *) &otg_descriptor,
  66. NULL,
  67. };
  68. /* string IDs are assigned dynamically */
  69. static struct usb_string strings_dev[] = {
  70. [USB_GADGET_MANUFACTURER_IDX].s = "",
  71. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  72. [USB_GADGET_SERIAL_IDX].s = "",
  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. static struct usb_function *f_acm;
  86. static struct usb_function_instance *fi_serial;
  87. static unsigned char tty_line;
  88. /*
  89. * We _always_ have both CDC ECM and CDC ACM functions.
  90. */
  91. static int __init cdc_do_config(struct usb_configuration *c)
  92. {
  93. struct f_serial_opts *opts;
  94. int status;
  95. if (gadget_is_otg(c->cdev->gadget)) {
  96. c->descriptors = otg_desc;
  97. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  98. }
  99. status = ecm_bind_config(c, hostaddr);
  100. if (status < 0)
  101. return status;
  102. fi_serial = usb_get_function_instance("acm");
  103. if (IS_ERR(fi_serial))
  104. return PTR_ERR(fi_serial);
  105. opts = container_of(fi_serial, struct f_serial_opts, func_inst);
  106. opts->port_num = tty_line;
  107. f_acm = usb_get_function(fi_serial);
  108. if (IS_ERR(f_acm))
  109. goto err_func_acm;
  110. status = usb_add_function(c, f_acm);
  111. if (status)
  112. goto err_conf;
  113. return 0;
  114. err_conf:
  115. usb_put_function(f_acm);
  116. err_func_acm:
  117. usb_put_function_instance(fi_serial);
  118. return status;
  119. }
  120. static struct usb_configuration cdc_config_driver = {
  121. .label = "CDC Composite (ECM + ACM)",
  122. .bConfigurationValue = 1,
  123. /* .iConfiguration = DYNAMIC */
  124. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  125. };
  126. /*-------------------------------------------------------------------------*/
  127. static int __init cdc_bind(struct usb_composite_dev *cdev)
  128. {
  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_alloc_line(&tty_line);
  142. if (status < 0)
  143. goto fail0;
  144. /* Allocate string descriptor numbers ... note that string
  145. * contents can be overridden by the composite_dev glue.
  146. */
  147. status = usb_string_ids_tab(cdev, strings_dev);
  148. if (status < 0)
  149. goto fail1;
  150. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  151. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  152. /* register our configuration */
  153. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  154. if (status < 0)
  155. goto fail1;
  156. usb_composite_overwrite_options(cdev, &coverwrite);
  157. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  158. DRIVER_DESC);
  159. return 0;
  160. fail1:
  161. gserial_free_line(tty_line);
  162. fail0:
  163. gether_cleanup();
  164. return status;
  165. }
  166. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  167. {
  168. usb_put_function(f_acm);
  169. usb_put_function_instance(fi_serial);
  170. gserial_free_line(tty_line);
  171. gether_cleanup();
  172. return 0;
  173. }
  174. static __refdata struct usb_composite_driver cdc_driver = {
  175. .name = "g_cdc",
  176. .dev = &device_desc,
  177. .strings = dev_strings,
  178. .max_speed = USB_SPEED_HIGH,
  179. .bind = cdc_bind,
  180. .unbind = __exit_p(cdc_unbind),
  181. };
  182. MODULE_DESCRIPTION(DRIVER_DESC);
  183. MODULE_AUTHOR("David Brownell");
  184. MODULE_LICENSE("GPL");
  185. static int __init init(void)
  186. {
  187. return usb_composite_probe(&cdc_driver);
  188. }
  189. module_init(init);
  190. static void __exit cleanup(void)
  191. {
  192. usb_composite_unregister(&cdc_driver);
  193. }
  194. module_exit(cleanup);