cdc2.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. fi_ecm = usb_get_function_instance("ecm");
  91. if (IS_ERR(fi_ecm)) {
  92. status = PTR_ERR(fi_ecm);
  93. goto err_func_ecm;
  94. }
  95. f_ecm = usb_get_function(fi_ecm);
  96. if (IS_ERR(f_ecm)) {
  97. status = PTR_ERR(f_ecm);
  98. goto err_get_ecm;
  99. }
  100. status = usb_add_function(c, f_ecm);
  101. if (status)
  102. goto err_add_ecm;
  103. fi_serial = usb_get_function_instance("acm");
  104. if (IS_ERR(fi_serial)) {
  105. status = PTR_ERR(fi_serial);
  106. goto err_get_acm;
  107. }
  108. f_acm = usb_get_function(fi_serial);
  109. if (IS_ERR(f_acm)) {
  110. status = PTR_ERR(f_acm);
  111. goto err_func_acm;
  112. }
  113. status = usb_add_function(c, f_acm);
  114. if (status)
  115. goto err_add_acm;
  116. return 0;
  117. err_add_acm:
  118. usb_put_function(f_acm);
  119. err_func_acm:
  120. usb_put_function_instance(fi_serial);
  121. err_get_acm:
  122. usb_remove_function(c, f_ecm);
  123. err_add_ecm:
  124. usb_put_function(f_ecm);
  125. err_get_ecm:
  126. usb_put_function_instance(fi_ecm);
  127. err_func_ecm:
  128. return status;
  129. }
  130. static struct usb_configuration cdc_config_driver = {
  131. .label = "CDC Composite (ECM + ACM)",
  132. .bConfigurationValue = 1,
  133. /* .iConfiguration = DYNAMIC */
  134. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  135. };
  136. /*-------------------------------------------------------------------------*/
  137. static int __init cdc_bind(struct usb_composite_dev *cdev)
  138. {
  139. struct usb_gadget *gadget = cdev->gadget;
  140. struct f_ecm_opts *ecm_opts;
  141. int status;
  142. if (!can_support_ecm(cdev->gadget)) {
  143. dev_err(&gadget->dev, "controller '%s' not usable\n",
  144. gadget->name);
  145. return -EINVAL;
  146. }
  147. fi_ecm = usb_get_function_instance("ecm");
  148. if (IS_ERR(fi_ecm))
  149. return PTR_ERR(fi_ecm);
  150. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  151. gether_set_qmult(ecm_opts->net, qmult);
  152. if (!gether_set_host_addr(ecm_opts->net, host_addr))
  153. pr_info("using host ethernet address: %s", host_addr);
  154. if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
  155. pr_info("using self ethernet address: %s", dev_addr);
  156. fi_serial = usb_get_function_instance("acm");
  157. if (IS_ERR(fi_serial)) {
  158. status = PTR_ERR(fi_serial);
  159. goto fail;
  160. }
  161. /* Allocate string descriptor numbers ... note that string
  162. * contents can be overridden by the composite_dev glue.
  163. */
  164. status = usb_string_ids_tab(cdev, strings_dev);
  165. if (status < 0)
  166. goto fail1;
  167. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  168. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  169. /* register our configuration */
  170. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  171. if (status < 0)
  172. goto fail1;
  173. usb_composite_overwrite_options(cdev, &coverwrite);
  174. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  175. DRIVER_DESC);
  176. return 0;
  177. fail1:
  178. usb_put_function_instance(fi_serial);
  179. fail:
  180. usb_put_function_instance(fi_ecm);
  181. return status;
  182. }
  183. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  184. {
  185. usb_put_function(f_acm);
  186. usb_put_function_instance(fi_serial);
  187. if (!IS_ERR_OR_NULL(f_ecm))
  188. usb_put_function(f_ecm);
  189. if (!IS_ERR_OR_NULL(fi_ecm))
  190. usb_put_function_instance(fi_ecm);
  191. return 0;
  192. }
  193. static __refdata struct usb_composite_driver cdc_driver = {
  194. .name = "g_cdc",
  195. .dev = &device_desc,
  196. .strings = dev_strings,
  197. .max_speed = USB_SPEED_HIGH,
  198. .bind = cdc_bind,
  199. .unbind = __exit_p(cdc_unbind),
  200. };
  201. MODULE_DESCRIPTION(DRIVER_DESC);
  202. MODULE_AUTHOR("David Brownell");
  203. MODULE_LICENSE("GPL");
  204. static int __init init(void)
  205. {
  206. return usb_composite_probe(&cdc_driver);
  207. }
  208. module_init(init);
  209. static void __exit cleanup(void)
  210. {
  211. usb_composite_unregister(&cdc_driver);
  212. }
  213. module_exit(cleanup);