ncm.c 5.3 KB

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