ncm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include "u_ncm.h"
  25. #define DRIVER_DESC "NCM Gadget"
  26. /*-------------------------------------------------------------------------*/
  27. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  28. * Instead: allocate your own, using normal USB-IF procedures.
  29. */
  30. /* Thanks to NetChip Technologies for donating this product ID.
  31. * It's for devices with only CDC Ethernet configurations.
  32. */
  33. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  34. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  35. /*-------------------------------------------------------------------------*/
  36. USB_GADGET_COMPOSITE_OPTIONS();
  37. USB_ETHERNET_MODULE_PARAMETERS();
  38. static struct usb_device_descriptor device_desc = {
  39. .bLength = sizeof device_desc,
  40. .bDescriptorType = USB_DT_DEVICE,
  41. .bcdUSB = cpu_to_le16 (0x0200),
  42. .bDeviceClass = USB_CLASS_COMM,
  43. .bDeviceSubClass = 0,
  44. .bDeviceProtocol = 0,
  45. /* .bMaxPacketSize0 = f(hardware) */
  46. /* Vendor and product id defaults change according to what configs
  47. * we support. (As does bNumConfigurations.) These values can
  48. * also be overridden by module parameters.
  49. */
  50. .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
  51. .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
  52. /* .bcdDevice = f(hardware) */
  53. /* .iManufacturer = DYNAMIC */
  54. /* .iProduct = DYNAMIC */
  55. /* NO SERIAL NUMBER */
  56. .bNumConfigurations = 1,
  57. };
  58. static struct usb_otg_descriptor otg_descriptor = {
  59. .bLength = sizeof otg_descriptor,
  60. .bDescriptorType = USB_DT_OTG,
  61. /* REVISIT SRP-only hardware is possible, although
  62. * it would not be called "OTG" ...
  63. */
  64. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  65. };
  66. static const struct usb_descriptor_header *otg_desc[] = {
  67. (struct usb_descriptor_header *) &otg_descriptor,
  68. NULL,
  69. };
  70. /* string IDs are assigned dynamically */
  71. static struct usb_string strings_dev[] = {
  72. [USB_GADGET_MANUFACTURER_IDX].s = "",
  73. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  74. [USB_GADGET_SERIAL_IDX].s = "",
  75. { } /* end of list */
  76. };
  77. static struct usb_gadget_strings stringtab_dev = {
  78. .language = 0x0409, /* en-us */
  79. .strings = strings_dev,
  80. };
  81. static struct usb_gadget_strings *dev_strings[] = {
  82. &stringtab_dev,
  83. NULL,
  84. };
  85. static struct usb_function_instance *f_ncm_inst;
  86. static struct usb_function *f_ncm;
  87. /*-------------------------------------------------------------------------*/
  88. static int __init ncm_do_config(struct usb_configuration *c)
  89. {
  90. int status;
  91. /* FIXME alloc iConfiguration string, set it in c->strings */
  92. if (gadget_is_otg(c->cdev->gadget)) {
  93. c->descriptors = otg_desc;
  94. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  95. }
  96. f_ncm = usb_get_function(f_ncm_inst);
  97. if (IS_ERR(f_ncm)) {
  98. status = PTR_ERR(f_ncm);
  99. return status;
  100. }
  101. status = usb_add_function(c, f_ncm);
  102. if (status < 0) {
  103. usb_put_function(f_ncm);
  104. return status;
  105. }
  106. return 0;
  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. struct usb_gadget *gadget = cdev->gadget;
  119. struct f_ncm_opts *ncm_opts;
  120. int status;
  121. f_ncm_inst = usb_get_function_instance("ncm");
  122. if (IS_ERR(f_ncm_inst))
  123. return PTR_ERR(f_ncm_inst);
  124. ncm_opts = container_of(f_ncm_inst, struct f_ncm_opts, func_inst);
  125. gether_set_qmult(ncm_opts->net, qmult);
  126. if (!gether_set_host_addr(ncm_opts->net, host_addr))
  127. pr_info("using host ethernet address: %s", host_addr);
  128. if (!gether_set_dev_addr(ncm_opts->net, dev_addr))
  129. pr_info("using self ethernet address: %s", dev_addr);
  130. /* Allocate string descriptor numbers ... note that string
  131. * contents can be overridden by the composite_dev glue.
  132. */
  133. status = usb_string_ids_tab(cdev, strings_dev);
  134. if (status < 0)
  135. goto fail;
  136. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  137. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  138. status = usb_add_config(cdev, &ncm_config_driver,
  139. ncm_do_config);
  140. if (status < 0)
  141. goto fail;
  142. usb_composite_overwrite_options(cdev, &coverwrite);
  143. dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
  144. return 0;
  145. fail:
  146. usb_put_function_instance(f_ncm_inst);
  147. return status;
  148. }
  149. static int __exit gncm_unbind(struct usb_composite_dev *cdev)
  150. {
  151. if (!IS_ERR_OR_NULL(f_ncm))
  152. usb_put_function(f_ncm);
  153. if (!IS_ERR_OR_NULL(f_ncm_inst))
  154. usb_put_function_instance(f_ncm_inst);
  155. return 0;
  156. }
  157. static __refdata struct usb_composite_driver ncm_driver = {
  158. .name = "g_ncm",
  159. .dev = &device_desc,
  160. .strings = dev_strings,
  161. .max_speed = USB_SPEED_HIGH,
  162. .bind = gncm_bind,
  163. .unbind = __exit_p(gncm_unbind),
  164. };
  165. MODULE_DESCRIPTION(DRIVER_DESC);
  166. MODULE_AUTHOR("Yauheni Kaliuta");
  167. MODULE_LICENSE("GPL");
  168. static int __init init(void)
  169. {
  170. return usb_composite_probe(&ncm_driver);
  171. }
  172. module_init(init);
  173. static void __exit cleanup(void)
  174. {
  175. usb_composite_unregister(&ncm_driver);
  176. }
  177. module_exit(cleanup);