ncm.c 6.1 KB

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