ncm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * ncm.c -- NCM gadget driver
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
  6. *
  7. * The driver borrows from ether.c which is:
  8. *
  9. * Copyright (C) 2003-2005,2008 David Brownell
  10. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  11. * Copyright (C) 2008 Nokia Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /* #define DEBUG */
  28. /* #define VERBOSE_DEBUG */
  29. #include <linux/kernel.h>
  30. #include <linux/utsname.h>
  31. #include "u_ether.h"
  32. #define DRIVER_DESC "NCM Gadget"
  33. /*-------------------------------------------------------------------------*/
  34. /*
  35. * Kbuild is not very cooperative with respect to linking separately
  36. * compiled library objects into one module. So for now we won't use
  37. * separate compilation ... ensuring init/exit sections work to shrink
  38. * the runtime footprint, and giving us at least some parts of what
  39. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  40. */
  41. #include "composite.c"
  42. #include "usbstring.c"
  43. #include "config.c"
  44. #include "epautoconf.c"
  45. #include "f_ncm.c"
  46. #include "u_ether.c"
  47. /*-------------------------------------------------------------------------*/
  48. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  49. * Instead: allocate your own, using normal USB-IF procedures.
  50. */
  51. /* Thanks to NetChip Technologies for donating this product ID.
  52. * It's for devices with only CDC Ethernet configurations.
  53. */
  54. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  55. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  56. /*-------------------------------------------------------------------------*/
  57. static struct usb_device_descriptor device_desc = {
  58. .bLength = sizeof device_desc,
  59. .bDescriptorType = USB_DT_DEVICE,
  60. .bcdUSB = cpu_to_le16 (0x0200),
  61. .bDeviceClass = USB_CLASS_COMM,
  62. .bDeviceSubClass = 0,
  63. .bDeviceProtocol = 0,
  64. /* .bMaxPacketSize0 = f(hardware) */
  65. /* Vendor and product id defaults change according to what configs
  66. * we support. (As does bNumConfigurations.) These values can
  67. * also be overridden by module parameters.
  68. */
  69. .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
  70. .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
  71. /* .bcdDevice = f(hardware) */
  72. /* .iManufacturer = DYNAMIC */
  73. /* .iProduct = DYNAMIC */
  74. /* NO SERIAL NUMBER */
  75. .bNumConfigurations = 1,
  76. };
  77. static struct usb_otg_descriptor otg_descriptor = {
  78. .bLength = sizeof otg_descriptor,
  79. .bDescriptorType = USB_DT_OTG,
  80. /* REVISIT SRP-only hardware is possible, although
  81. * it would not be called "OTG" ...
  82. */
  83. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  84. };
  85. static const struct usb_descriptor_header *otg_desc[] = {
  86. (struct usb_descriptor_header *) &otg_descriptor,
  87. NULL,
  88. };
  89. /* string IDs are assigned dynamically */
  90. #define STRING_MANUFACTURER_IDX 0
  91. #define STRING_PRODUCT_IDX 1
  92. static char manufacturer[50];
  93. static struct usb_string strings_dev[] = {
  94. [STRING_MANUFACTURER_IDX].s = manufacturer,
  95. [STRING_PRODUCT_IDX].s = DRIVER_DESC,
  96. { } /* end of list */
  97. };
  98. static struct usb_gadget_strings stringtab_dev = {
  99. .language = 0x0409, /* en-us */
  100. .strings = strings_dev,
  101. };
  102. static struct usb_gadget_strings *dev_strings[] = {
  103. &stringtab_dev,
  104. NULL,
  105. };
  106. static u8 hostaddr[ETH_ALEN];
  107. /*-------------------------------------------------------------------------*/
  108. static int __init ncm_do_config(struct usb_configuration *c)
  109. {
  110. /* FIXME alloc iConfiguration string, set it in c->strings */
  111. if (gadget_is_otg(c->cdev->gadget)) {
  112. c->descriptors = otg_desc;
  113. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  114. }
  115. return ncm_bind_config(c, hostaddr);
  116. }
  117. static struct usb_configuration ncm_config_driver = {
  118. /* .label = f(hardware) */
  119. .label = "CDC Ethernet (NCM)",
  120. .bConfigurationValue = 1,
  121. /* .iConfiguration = DYNAMIC */
  122. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  123. };
  124. /*-------------------------------------------------------------------------*/
  125. static int __init gncm_bind(struct usb_composite_dev *cdev)
  126. {
  127. int gcnum;
  128. struct usb_gadget *gadget = cdev->gadget;
  129. int status;
  130. /* set up network link layer */
  131. status = gether_setup(cdev->gadget, hostaddr);
  132. if (status < 0)
  133. return status;
  134. gcnum = usb_gadget_controller_number(gadget);
  135. if (gcnum >= 0)
  136. device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
  137. else {
  138. /* We assume that can_support_ecm() tells the truth;
  139. * but if the controller isn't recognized at all then
  140. * that assumption is a bit more likely to be wrong.
  141. */
  142. dev_warn(&gadget->dev,
  143. "controller '%s' not recognized; trying %s\n",
  144. gadget->name,
  145. ncm_config_driver.label);
  146. device_desc.bcdDevice =
  147. cpu_to_le16(0x0300 | 0x0099);
  148. }
  149. /* Allocate string descriptor numbers ... note that string
  150. * contents can be overridden by the composite_dev glue.
  151. */
  152. /* device descriptor strings: manufacturer, product */
  153. snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
  154. init_utsname()->sysname, init_utsname()->release,
  155. gadget->name);
  156. status = usb_string_id(cdev);
  157. if (status < 0)
  158. goto fail;
  159. strings_dev[STRING_MANUFACTURER_IDX].id = status;
  160. device_desc.iManufacturer = status;
  161. status = usb_string_id(cdev);
  162. if (status < 0)
  163. goto fail;
  164. strings_dev[STRING_PRODUCT_IDX].id = status;
  165. device_desc.iProduct = status;
  166. status = usb_add_config(cdev, &ncm_config_driver,
  167. ncm_do_config);
  168. if (status < 0)
  169. goto fail;
  170. dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
  171. return 0;
  172. fail:
  173. gether_cleanup();
  174. return status;
  175. }
  176. static int __exit gncm_unbind(struct usb_composite_dev *cdev)
  177. {
  178. gether_cleanup();
  179. return 0;
  180. }
  181. static struct usb_composite_driver ncm_driver = {
  182. .name = "g_ncm",
  183. .dev = &device_desc,
  184. .strings = dev_strings,
  185. .unbind = __exit_p(gncm_unbind),
  186. };
  187. MODULE_DESCRIPTION(DRIVER_DESC);
  188. MODULE_AUTHOR("Yauheni Kaliuta");
  189. MODULE_LICENSE("GPL");
  190. static int __init init(void)
  191. {
  192. return usb_composite_probe(&ncm_driver, gncm_bind);
  193. }
  194. module_init(init);
  195. static void __exit cleanup(void)
  196. {
  197. usb_composite_unregister(&ncm_driver);
  198. }
  199. module_exit(cleanup);