cdc2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/utsname.h>
  14. #include <linux/module.h>
  15. #include "u_ether.h"
  16. #include "u_serial.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. /*-------------------------------------------------------------------------*/
  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 "composite.c"
  37. #include "u_serial.c"
  38. #include "f_acm.c"
  39. #include "f_ecm.c"
  40. #include "u_ether.c"
  41. /*-------------------------------------------------------------------------*/
  42. static struct usb_device_descriptor device_desc = {
  43. .bLength = sizeof device_desc,
  44. .bDescriptorType = USB_DT_DEVICE,
  45. .bcdUSB = cpu_to_le16(0x0200),
  46. .bDeviceClass = USB_CLASS_COMM,
  47. .bDeviceSubClass = 0,
  48. .bDeviceProtocol = 0,
  49. /* .bMaxPacketSize0 = f(hardware) */
  50. /* Vendor and product id can be overridden by module parameters. */
  51. .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
  52. .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
  53. /* .bcdDevice = f(hardware) */
  54. /* .iManufacturer = DYNAMIC */
  55. /* .iProduct = DYNAMIC */
  56. /* NO SERIAL NUMBER */
  57. .bNumConfigurations = 1,
  58. };
  59. static struct usb_otg_descriptor otg_descriptor = {
  60. .bLength = sizeof otg_descriptor,
  61. .bDescriptorType = USB_DT_OTG,
  62. /* REVISIT SRP-only hardware is possible, although
  63. * it would not be called "OTG" ...
  64. */
  65. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  66. };
  67. static const struct usb_descriptor_header *otg_desc[] = {
  68. (struct usb_descriptor_header *) &otg_descriptor,
  69. NULL,
  70. };
  71. /* string IDs are assigned dynamically */
  72. #define STRING_MANUFACTURER_IDX 0
  73. #define STRING_PRODUCT_IDX 1
  74. static char manufacturer[50];
  75. static struct usb_string strings_dev[] = {
  76. [STRING_MANUFACTURER_IDX].s = manufacturer,
  77. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  78. { } /* end of list */
  79. };
  80. static struct usb_gadget_strings stringtab_dev = {
  81. .language = 0x0409, /* en-us */
  82. .strings = strings_dev,
  83. };
  84. static struct usb_gadget_strings *dev_strings[] = {
  85. &stringtab_dev,
  86. NULL,
  87. };
  88. static u8 hostaddr[ETH_ALEN];
  89. /*-------------------------------------------------------------------------*/
  90. /*
  91. * We _always_ have both CDC ECM and CDC ACM functions.
  92. */
  93. static int __init cdc_do_config(struct usb_configuration *c)
  94. {
  95. int status;
  96. if (gadget_is_otg(c->cdev->gadget)) {
  97. c->descriptors = otg_desc;
  98. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  99. }
  100. status = ecm_bind_config(c, hostaddr);
  101. if (status < 0)
  102. return status;
  103. status = acm_bind_config(c, 0);
  104. if (status < 0)
  105. return status;
  106. return 0;
  107. }
  108. static struct usb_configuration cdc_config_driver = {
  109. .label = "CDC Composite (ECM + ACM)",
  110. .bConfigurationValue = 1,
  111. /* .iConfiguration = DYNAMIC */
  112. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  113. };
  114. /*-------------------------------------------------------------------------*/
  115. static int __init cdc_bind(struct usb_composite_dev *cdev)
  116. {
  117. int gcnum;
  118. struct usb_gadget *gadget = cdev->gadget;
  119. int status;
  120. if (!can_support_ecm(cdev->gadget)) {
  121. dev_err(&gadget->dev, "controller '%s' not usable\n",
  122. gadget->name);
  123. return -EINVAL;
  124. }
  125. /* set up network link layer */
  126. status = gether_setup(cdev->gadget, hostaddr);
  127. if (status < 0)
  128. return status;
  129. /* set up serial link layer */
  130. status = gserial_setup(cdev->gadget, 1);
  131. if (status < 0)
  132. goto fail0;
  133. gcnum = usb_gadget_controller_number(gadget);
  134. if (gcnum >= 0)
  135. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  136. else {
  137. /* We assume that can_support_ecm() tells the truth;
  138. * but if the controller isn't recognized at all then
  139. * that assumption is a bit more likely to be wrong.
  140. */
  141. WARNING(cdev, "controller '%s' not recognized; trying %s\n",
  142. gadget->name,
  143. cdc_config_driver.label);
  144. device_desc.bcdDevice =
  145. cpu_to_le16(0x0300 | 0x0099);
  146. }
  147. /* Allocate string descriptor numbers ... note that string
  148. * contents can be overridden by the composite_dev glue.
  149. */
  150. /* device descriptor strings: manufacturer, product */
  151. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  152. init_utsname()->sysname, init_utsname()->release,
  153. gadget->name);
  154. status = usb_string_ids_tab(cdev, strings_dev);
  155. if (status < 0)
  156. goto fail1;
  157. device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
  158. device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
  159. /* register our configuration */
  160. status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
  161. if (status < 0)
  162. goto fail1;
  163. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  164. DRIVER_DESC);
  165. return 0;
  166. fail1:
  167. gserial_cleanup();
  168. fail0:
  169. gether_cleanup();
  170. return status;
  171. }
  172. static int __exit cdc_unbind(struct usb_composite_dev *cdev)
  173. {
  174. gserial_cleanup();
  175. gether_cleanup();
  176. return 0;
  177. }
  178. static __refdata struct usb_composite_driver cdc_driver = {
  179. .name = "g_cdc",
  180. .dev = &device_desc,
  181. .strings = dev_strings,
  182. .max_speed = USB_SPEED_HIGH,
  183. .bind = cdc_bind,
  184. .unbind = __exit_p(cdc_unbind),
  185. };
  186. MODULE_DESCRIPTION(DRIVER_DESC);
  187. MODULE_AUTHOR("David Brownell");
  188. MODULE_LICENSE("GPL");
  189. static int __init init(void)
  190. {
  191. return usb_composite_probe(&cdc_driver);
  192. }
  193. module_init(init);
  194. static void __exit cleanup(void)
  195. {
  196. usb_composite_unregister(&cdc_driver);
  197. }
  198. module_exit(cleanup);