cdc2.c 5.9 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. #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 const struct usb_descriptor_header *otg_desc[2];
  49. /* string IDs are assigned dynamically */
  50. static struct usb_string strings_dev[] = {
  51. [USB_GADGET_MANUFACTURER_IDX].s = "",
  52. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  53. [USB_GADGET_SERIAL_IDX].s = "",
  54. { } /* end of list */
  55. };
  56. static struct usb_gadget_strings stringtab_dev = {
  57. .language = 0x0409, /* en-us */
  58. .strings = strings_dev,
  59. };
  60. static struct usb_gadget_strings *dev_strings[] = {
  61. &stringtab_dev,
  62. NULL,
  63. };
  64. /*-------------------------------------------------------------------------*/
  65. static struct usb_function *f_acm;
  66. static struct usb_function_instance *fi_serial;
  67. static struct usb_function *f_ecm;
  68. static struct usb_function_instance *fi_ecm;
  69. /*
  70. * We _always_ have both CDC ECM and CDC ACM functions.
  71. */
  72. static int cdc_do_config(struct usb_configuration *c)
  73. {
  74. int status;
  75. if (gadget_is_otg(c->cdev->gadget)) {
  76. c->descriptors = otg_desc;
  77. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  78. }
  79. f_ecm = usb_get_function(fi_ecm);
  80. if (IS_ERR(f_ecm)) {
  81. status = PTR_ERR(f_ecm);
  82. goto err_get_ecm;
  83. }
  84. status = usb_add_function(c, f_ecm);
  85. if (status)
  86. goto err_add_ecm;
  87. f_acm = usb_get_function(fi_serial);
  88. if (IS_ERR(f_acm)) {
  89. status = PTR_ERR(f_acm);
  90. goto err_get_acm;
  91. }
  92. status = usb_add_function(c, f_acm);
  93. if (status)
  94. goto err_add_acm;
  95. return 0;
  96. err_add_acm:
  97. usb_put_function(f_acm);
  98. err_get_acm:
  99. usb_remove_function(c, f_ecm);
  100. err_add_ecm:
  101. usb_put_function(f_ecm);
  102. err_get_ecm:
  103. return status;
  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 cdc_bind(struct usb_composite_dev *cdev)
  113. {
  114. struct usb_gadget *gadget = cdev->gadget;
  115. struct f_ecm_opts *ecm_opts;
  116. int status;
  117. if (!can_support_ecm(cdev->gadget)) {
  118. dev_err(&gadget->dev, "controller '%s' not usable\n",
  119. gadget->name);
  120. return -EINVAL;
  121. }
  122. fi_ecm = usb_get_function_instance("ecm");
  123. if (IS_ERR(fi_ecm))
  124. return PTR_ERR(fi_ecm);
  125. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  126. gether_set_qmult(ecm_opts->net, qmult);
  127. if (!gether_set_host_addr(ecm_opts->net, host_addr))
  128. pr_info("using host ethernet address: %s", host_addr);
  129. if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
  130. pr_info("using self ethernet address: %s", dev_addr);
  131. fi_serial = usb_get_function_instance("acm");
  132. if (IS_ERR(fi_serial)) {
  133. status = PTR_ERR(fi_serial);
  134. goto fail;
  135. }
  136. /* Allocate string descriptor numbers ... note that string
  137. * contents can be overridden by the composite_dev glue.
  138. */
  139. status = usb_string_ids_tab(cdev, strings_dev);
  140. if (status < 0)
  141. goto fail1;
  142. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  143. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  144. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  145. struct usb_descriptor_header *usb_desc;
  146. usb_desc = usb_otg_descriptor_alloc(gadget);
  147. if (!usb_desc)
  148. goto fail1;
  149. usb_otg_descriptor_init(gadget, usb_desc);
  150. otg_desc[0] = usb_desc;
  151. otg_desc[1] = NULL;
  152. }
  153. /* register our configuration */
  154. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  155. if (status < 0)
  156. goto fail2;
  157. usb_composite_overwrite_options(cdev, &coverwrite);
  158. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  159. DRIVER_DESC);
  160. return 0;
  161. fail2:
  162. kfree(otg_desc[0]);
  163. otg_desc[0] = NULL;
  164. fail1:
  165. usb_put_function_instance(fi_serial);
  166. fail:
  167. usb_put_function_instance(fi_ecm);
  168. return status;
  169. }
  170. static int cdc_unbind(struct usb_composite_dev *cdev)
  171. {
  172. usb_put_function(f_acm);
  173. usb_put_function_instance(fi_serial);
  174. if (!IS_ERR_OR_NULL(f_ecm))
  175. usb_put_function(f_ecm);
  176. if (!IS_ERR_OR_NULL(fi_ecm))
  177. usb_put_function_instance(fi_ecm);
  178. kfree(otg_desc[0]);
  179. otg_desc[0] = NULL;
  180. return 0;
  181. }
  182. static struct usb_composite_driver cdc_driver = {
  183. .name = "g_cdc",
  184. .dev = &device_desc,
  185. .strings = dev_strings,
  186. .max_speed = USB_SPEED_HIGH,
  187. .bind = cdc_bind,
  188. .unbind = cdc_unbind,
  189. };
  190. module_usb_composite_driver(cdc_driver);
  191. MODULE_DESCRIPTION(DRIVER_DESC);
  192. MODULE_AUTHOR("David Brownell");
  193. MODULE_LICENSE("GPL");