cdc2.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * cdc2.c -- CDC Composite driver, with ECM and ACM support
  4. *
  5. * Copyright (C) 2008 David Brownell
  6. * Copyright (C) 2008 Nokia Corporation
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include "u_ether.h"
  11. #include "u_serial.h"
  12. #include "u_ecm.h"
  13. #define DRIVER_DESC "CDC Composite Gadget"
  14. #define DRIVER_VERSION "King Kamehameha Day 2008"
  15. /*-------------------------------------------------------------------------*/
  16. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  17. * Instead: allocate your own, using normal USB-IF procedures.
  18. */
  19. /* Thanks to NetChip Technologies for donating this product ID.
  20. * It's for devices with only this composite CDC configuration.
  21. */
  22. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  23. #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
  24. USB_GADGET_COMPOSITE_OPTIONS();
  25. USB_ETHERNET_MODULE_PARAMETERS();
  26. /*-------------------------------------------------------------------------*/
  27. static struct usb_device_descriptor device_desc = {
  28. .bLength = sizeof device_desc,
  29. .bDescriptorType = USB_DT_DEVICE,
  30. /* .bcdUSB = DYNAMIC */
  31. .bDeviceClass = USB_CLASS_COMM,
  32. .bDeviceSubClass = 0,
  33. .bDeviceProtocol = 0,
  34. /* .bMaxPacketSize0 = f(hardware) */
  35. /* Vendor and product id can be overridden by module parameters. */
  36. .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
  37. .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
  38. /* .bcdDevice = f(hardware) */
  39. /* .iManufacturer = DYNAMIC */
  40. /* .iProduct = DYNAMIC */
  41. /* NO SERIAL NUMBER */
  42. .bNumConfigurations = 1,
  43. };
  44. static const struct usb_descriptor_header *otg_desc[2];
  45. /* string IDs are assigned dynamically */
  46. static struct usb_string strings_dev[] = {
  47. [USB_GADGET_MANUFACTURER_IDX].s = "",
  48. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  49. [USB_GADGET_SERIAL_IDX].s = "",
  50. { } /* end of list */
  51. };
  52. static struct usb_gadget_strings stringtab_dev = {
  53. .language = 0x0409, /* en-us */
  54. .strings = strings_dev,
  55. };
  56. static struct usb_gadget_strings *dev_strings[] = {
  57. &stringtab_dev,
  58. NULL,
  59. };
  60. /*-------------------------------------------------------------------------*/
  61. static struct usb_function *f_acm;
  62. static struct usb_function_instance *fi_serial;
  63. static struct usb_function *f_ecm;
  64. static struct usb_function_instance *fi_ecm;
  65. /*
  66. * We _always_ have both CDC ECM and CDC ACM functions.
  67. */
  68. static int cdc_do_config(struct usb_configuration *c)
  69. {
  70. int status;
  71. if (gadget_is_otg(c->cdev->gadget)) {
  72. c->descriptors = otg_desc;
  73. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  74. }
  75. f_ecm = usb_get_function(fi_ecm);
  76. if (IS_ERR(f_ecm)) {
  77. status = PTR_ERR(f_ecm);
  78. goto err_get_ecm;
  79. }
  80. status = usb_add_function(c, f_ecm);
  81. if (status)
  82. goto err_add_ecm;
  83. f_acm = usb_get_function(fi_serial);
  84. if (IS_ERR(f_acm)) {
  85. status = PTR_ERR(f_acm);
  86. goto err_get_acm;
  87. }
  88. status = usb_add_function(c, f_acm);
  89. if (status)
  90. goto err_add_acm;
  91. return 0;
  92. err_add_acm:
  93. usb_put_function(f_acm);
  94. err_get_acm:
  95. usb_remove_function(c, f_ecm);
  96. err_add_ecm:
  97. usb_put_function(f_ecm);
  98. err_get_ecm:
  99. return status;
  100. }
  101. static struct usb_configuration cdc_config_driver = {
  102. .label = "CDC Composite (ECM + ACM)",
  103. .bConfigurationValue = 1,
  104. /* .iConfiguration = DYNAMIC */
  105. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  106. };
  107. /*-------------------------------------------------------------------------*/
  108. static int cdc_bind(struct usb_composite_dev *cdev)
  109. {
  110. struct usb_gadget *gadget = cdev->gadget;
  111. struct f_ecm_opts *ecm_opts;
  112. int status;
  113. if (!can_support_ecm(cdev->gadget)) {
  114. dev_err(&gadget->dev, "controller '%s' not usable\n",
  115. gadget->name);
  116. return -EINVAL;
  117. }
  118. fi_ecm = usb_get_function_instance("ecm");
  119. if (IS_ERR(fi_ecm))
  120. return PTR_ERR(fi_ecm);
  121. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  122. gether_set_qmult(ecm_opts->net, qmult);
  123. if (!gether_set_host_addr(ecm_opts->net, host_addr))
  124. pr_info("using host ethernet address: %s", host_addr);
  125. if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
  126. pr_info("using self ethernet address: %s", dev_addr);
  127. fi_serial = usb_get_function_instance("acm");
  128. if (IS_ERR(fi_serial)) {
  129. status = PTR_ERR(fi_serial);
  130. goto fail;
  131. }
  132. /* Allocate string descriptor numbers ... note that string
  133. * contents can be overridden by the composite_dev glue.
  134. */
  135. status = usb_string_ids_tab(cdev, strings_dev);
  136. if (status < 0)
  137. goto fail1;
  138. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  139. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  140. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  141. struct usb_descriptor_header *usb_desc;
  142. usb_desc = usb_otg_descriptor_alloc(gadget);
  143. if (!usb_desc)
  144. goto fail1;
  145. usb_otg_descriptor_init(gadget, usb_desc);
  146. otg_desc[0] = usb_desc;
  147. otg_desc[1] = NULL;
  148. }
  149. /* register our configuration */
  150. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  151. if (status < 0)
  152. goto fail2;
  153. usb_composite_overwrite_options(cdev, &coverwrite);
  154. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  155. DRIVER_DESC);
  156. return 0;
  157. fail2:
  158. kfree(otg_desc[0]);
  159. otg_desc[0] = NULL;
  160. fail1:
  161. usb_put_function_instance(fi_serial);
  162. fail:
  163. usb_put_function_instance(fi_ecm);
  164. return status;
  165. }
  166. static int cdc_unbind(struct usb_composite_dev *cdev)
  167. {
  168. usb_put_function(f_acm);
  169. usb_put_function_instance(fi_serial);
  170. if (!IS_ERR_OR_NULL(f_ecm))
  171. usb_put_function(f_ecm);
  172. if (!IS_ERR_OR_NULL(fi_ecm))
  173. usb_put_function_instance(fi_ecm);
  174. kfree(otg_desc[0]);
  175. otg_desc[0] = NULL;
  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. .max_speed = USB_SPEED_SUPER,
  183. .bind = cdc_bind,
  184. .unbind = cdc_unbind,
  185. };
  186. module_usb_composite_driver(cdc_driver);
  187. MODULE_DESCRIPTION(DRIVER_DESC);
  188. MODULE_AUTHOR("David Brownell");
  189. MODULE_LICENSE("GPL");