cdc2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #include "u_ecm.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. USB_GADGET_COMPOSITE_OPTIONS();
  29. USB_ETHERNET_MODULE_PARAMETERS();
  30. /*-------------------------------------------------------------------------*/
  31. static struct usb_device_descriptor device_desc = {
  32. .bLength = sizeof device_desc,
  33. .bDescriptorType = USB_DT_DEVICE,
  34. .bcdUSB = cpu_to_le16(0x0200),
  35. .bDeviceClass = USB_CLASS_COMM,
  36. .bDeviceSubClass = 0,
  37. .bDeviceProtocol = 0,
  38. /* .bMaxPacketSize0 = f(hardware) */
  39. /* Vendor and product id can be overridden by module parameters. */
  40. .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
  41. .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
  42. /* .bcdDevice = f(hardware) */
  43. /* .iManufacturer = DYNAMIC */
  44. /* .iProduct = DYNAMIC */
  45. /* NO SERIAL NUMBER */
  46. .bNumConfigurations = 1,
  47. };
  48. static struct usb_otg_descriptor otg_descriptor = {
  49. .bLength = sizeof otg_descriptor,
  50. .bDescriptorType = USB_DT_OTG,
  51. /* REVISIT SRP-only hardware is possible, although
  52. * it would not be called "OTG" ...
  53. */
  54. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  55. };
  56. static const struct usb_descriptor_header *otg_desc[] = {
  57. (struct usb_descriptor_header *) &otg_descriptor,
  58. NULL,
  59. };
  60. /* string IDs are assigned dynamically */
  61. static struct usb_string strings_dev[] = {
  62. [USB_GADGET_MANUFACTURER_IDX].s = "",
  63. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  64. [USB_GADGET_SERIAL_IDX].s = "",
  65. { } /* end of list */
  66. };
  67. static struct usb_gadget_strings stringtab_dev = {
  68. .language = 0x0409, /* en-us */
  69. .strings = strings_dev,
  70. };
  71. static struct usb_gadget_strings *dev_strings[] = {
  72. &stringtab_dev,
  73. NULL,
  74. };
  75. /*-------------------------------------------------------------------------*/
  76. static struct usb_function *f_acm;
  77. static struct usb_function_instance *fi_serial;
  78. static struct usb_function *f_ecm;
  79. static struct usb_function_instance *fi_ecm;
  80. /*
  81. * We _always_ have both CDC ECM and CDC ACM functions.
  82. */
  83. static int __init cdc_do_config(struct usb_configuration *c)
  84. {
  85. int status;
  86. if (gadget_is_otg(c->cdev->gadget)) {
  87. c->descriptors = otg_desc;
  88. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  89. }
  90. f_ecm = usb_get_function(fi_ecm);
  91. if (IS_ERR(f_ecm)) {
  92. status = PTR_ERR(f_ecm);
  93. goto err_get_ecm;
  94. }
  95. status = usb_add_function(c, f_ecm);
  96. if (status)
  97. goto err_add_ecm;
  98. f_acm = usb_get_function(fi_serial);
  99. if (IS_ERR(f_acm)) {
  100. status = PTR_ERR(f_acm);
  101. goto err_get_acm;
  102. }
  103. status = usb_add_function(c, f_acm);
  104. if (status)
  105. goto err_add_acm;
  106. return 0;
  107. err_add_acm:
  108. usb_put_function(f_acm);
  109. err_get_acm:
  110. usb_remove_function(c, f_ecm);
  111. err_add_ecm:
  112. usb_put_function(f_ecm);
  113. err_get_ecm:
  114. return status;
  115. }
  116. static struct usb_configuration cdc_config_driver = {
  117. .label = "CDC Composite (ECM + ACM)",
  118. .bConfigurationValue = 1,
  119. /* .iConfiguration = DYNAMIC */
  120. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  121. };
  122. /*-------------------------------------------------------------------------*/
  123. static int __init cdc_bind(struct usb_composite_dev *cdev)
  124. {
  125. struct usb_gadget *gadget = cdev->gadget;
  126. struct f_ecm_opts *ecm_opts;
  127. int status;
  128. if (!can_support_ecm(cdev->gadget)) {
  129. dev_err(&gadget->dev, "controller '%s' not usable\n",
  130. gadget->name);
  131. return -EINVAL;
  132. }
  133. fi_ecm = usb_get_function_instance("ecm");
  134. if (IS_ERR(fi_ecm))
  135. return PTR_ERR(fi_ecm);
  136. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  137. gether_set_qmult(ecm_opts->net, qmult);
  138. if (!gether_set_host_addr(ecm_opts->net, host_addr))
  139. pr_info("using host ethernet address: %s", host_addr);
  140. if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
  141. pr_info("using self ethernet address: %s", dev_addr);
  142. fi_serial = usb_get_function_instance("acm");
  143. if (IS_ERR(fi_serial)) {
  144. status = PTR_ERR(fi_serial);
  145. goto fail;
  146. }
  147. /* Allocate string descriptor numbers ... note that string
  148. * contents can be overridden by the composite_dev glue.
  149. */
  150. status = usb_string_ids_tab(cdev, strings_dev);
  151. if (status < 0)
  152. goto fail1;
  153. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  154. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  155. /* register our configuration */
  156. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  157. if (status < 0)
  158. goto fail1;
  159. usb_composite_overwrite_options(cdev, &coverwrite);
  160. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  161. DRIVER_DESC);
  162. return 0;
  163. fail1:
  164. usb_put_function_instance(fi_serial);
  165. fail:
  166. usb_put_function_instance(fi_ecm);
  167. return status;
  168. }
  169. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  170. {
  171. usb_put_function(f_acm);
  172. usb_put_function_instance(fi_serial);
  173. if (!IS_ERR_OR_NULL(f_ecm))
  174. usb_put_function(f_ecm);
  175. if (!IS_ERR_OR_NULL(fi_ecm))
  176. usb_put_function_instance(fi_ecm);
  177. return 0;
  178. }
  179. static __refdata struct usb_composite_driver cdc_driver = {
  180. .name = "g_cdc",
  181. .dev = &device_desc,
  182. .strings = dev_strings,
  183. .max_speed = USB_SPEED_HIGH,
  184. .bind = cdc_bind,
  185. .unbind = __exit_p(cdc_unbind),
  186. };
  187. MODULE_DESCRIPTION(DRIVER_DESC);
  188. MODULE_AUTHOR("David Brownell");
  189. MODULE_LICENSE("GPL");
  190. static int __init init(void)
  191. {
  192. return usb_composite_probe(&cdc_driver);
  193. }
  194. module_init(init);
  195. static void __exit cleanup(void)
  196. {
  197. usb_composite_unregister(&cdc_driver);
  198. }
  199. module_exit(cleanup);