ether.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  6. * Copyright (C) 2008 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. /* #define VERBOSE_DEBUG */
  14. #include <linux/kernel.h>
  15. #if defined USB_ETH_RNDIS
  16. # undef USB_ETH_RNDIS
  17. #endif
  18. #ifdef CONFIG_USB_ETH_RNDIS
  19. # define USB_ETH_RNDIS y
  20. #endif
  21. #include "u_ether.h"
  22. /*
  23. * Ethernet gadget driver -- with CDC and non-CDC options
  24. * Builds on hardware support for a full duplex link.
  25. *
  26. * CDC Ethernet is the standard USB solution for sending Ethernet frames
  27. * using USB. Real hardware tends to use the same framing protocol but look
  28. * different for control features. This driver strongly prefers to use
  29. * this USB-IF standard as its open-systems interoperability solution;
  30. * most host side USB stacks (except from Microsoft) support it.
  31. *
  32. * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
  33. * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
  34. * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
  35. *
  36. * There's some hardware that can't talk CDC ECM. We make that hardware
  37. * implement a "minimalist" vendor-agnostic CDC core: same framing, but
  38. * link-level setup only requires activating the configuration. Only the
  39. * endpoint descriptors, and product/vendor IDs, are relevant; no control
  40. * operations are available. Linux supports it, but other host operating
  41. * systems may not. (This is a subset of CDC Ethernet.)
  42. *
  43. * It turns out that if you add a few descriptors to that "CDC Subset",
  44. * (Windows) host side drivers from MCCI can treat it as one submode of
  45. * a proprietary scheme called "SAFE" ... without needing to know about
  46. * specific product/vendor IDs. So we do that, making it easier to use
  47. * those MS-Windows drivers. Those added descriptors make it resemble a
  48. * CDC MDLM device, but they don't change device behavior at all. (See
  49. * MCCI Engineering report 950198 "SAFE Networking Functions".)
  50. *
  51. * A third option is also in use. Rather than CDC Ethernet, or something
  52. * simpler, Microsoft pushes their own approach: RNDIS. The published
  53. * RNDIS specs are ambiguous and appear to be incomplete, and are also
  54. * needlessly complex. They borrow more from CDC ACM than CDC ECM.
  55. */
  56. #define DRIVER_DESC "Ethernet Gadget"
  57. #define DRIVER_VERSION "Memorial Day 2008"
  58. #ifdef USB_ETH_RNDIS
  59. #define PREFIX "RNDIS/"
  60. #else
  61. #define PREFIX ""
  62. #endif
  63. /*
  64. * This driver aims for interoperability by using CDC ECM unless
  65. *
  66. * can_support_ecm()
  67. *
  68. * returns false, in which case it supports the CDC Subset. By default,
  69. * that returns true; most hardware has no problems with CDC ECM, that's
  70. * a good default. Previous versions of this driver had no default; this
  71. * version changes that, removing overhead for new controller support.
  72. *
  73. * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
  74. */
  75. static inline bool has_rndis(void)
  76. {
  77. #ifdef USB_ETH_RNDIS
  78. return true;
  79. #else
  80. return false;
  81. #endif
  82. }
  83. /*-------------------------------------------------------------------------*/
  84. /*
  85. * Kbuild is not very cooperative with respect to linking separately
  86. * compiled library objects into one module. So for now we won't use
  87. * separate compilation ... ensuring init/exit sections work to shrink
  88. * the runtime footprint, and giving us at least some parts of what
  89. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  90. */
  91. #include "f_ecm.c"
  92. #include "f_subset.c"
  93. #ifdef USB_ETH_RNDIS
  94. #include "f_rndis.c"
  95. #include "rndis.c"
  96. #endif
  97. #include "f_eem.c"
  98. #include "u_ether.c"
  99. /*-------------------------------------------------------------------------*/
  100. USB_GADGET_COMPOSITE_OPTIONS();
  101. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  102. * Instead: allocate your own, using normal USB-IF procedures.
  103. */
  104. /* Thanks to NetChip Technologies for donating this product ID.
  105. * It's for devices with only CDC Ethernet configurations.
  106. */
  107. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  108. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  109. /* For hardware that can't talk CDC, we use the same vendor ID that
  110. * ARM Linux has used for ethernet-over-usb, both with sa1100 and
  111. * with pxa250. We're protocol-compatible, if the host-side drivers
  112. * use the endpoint descriptors. bcdDevice (version) is nonzero, so
  113. * drivers that need to hard-wire endpoint numbers have a hook.
  114. *
  115. * The protocol is a minimal subset of CDC Ether, which works on any bulk
  116. * hardware that's not deeply broken ... even on hardware that can't talk
  117. * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
  118. * doesn't handle control-OUT).
  119. */
  120. #define SIMPLE_VENDOR_NUM 0x049f
  121. #define SIMPLE_PRODUCT_NUM 0x505a
  122. /* For hardware that can talk RNDIS and either of the above protocols,
  123. * use this ID ... the windows INF files will know it. Unless it's
  124. * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
  125. * the non-RNDIS configuration.
  126. */
  127. #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
  128. #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
  129. /* For EEM gadgets */
  130. #define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */
  131. #define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */
  132. /*-------------------------------------------------------------------------*/
  133. static struct usb_device_descriptor device_desc = {
  134. .bLength = sizeof device_desc,
  135. .bDescriptorType = USB_DT_DEVICE,
  136. .bcdUSB = cpu_to_le16 (0x0200),
  137. .bDeviceClass = USB_CLASS_COMM,
  138. .bDeviceSubClass = 0,
  139. .bDeviceProtocol = 0,
  140. /* .bMaxPacketSize0 = f(hardware) */
  141. /* Vendor and product id defaults change according to what configs
  142. * we support. (As does bNumConfigurations.) These values can
  143. * also be overridden by module parameters.
  144. */
  145. .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
  146. .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
  147. /* .bcdDevice = f(hardware) */
  148. /* .iManufacturer = DYNAMIC */
  149. /* .iProduct = DYNAMIC */
  150. /* NO SERIAL NUMBER */
  151. .bNumConfigurations = 1,
  152. };
  153. static struct usb_otg_descriptor otg_descriptor = {
  154. .bLength = sizeof otg_descriptor,
  155. .bDescriptorType = USB_DT_OTG,
  156. /* REVISIT SRP-only hardware is possible, although
  157. * it would not be called "OTG" ...
  158. */
  159. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  160. };
  161. static const struct usb_descriptor_header *otg_desc[] = {
  162. (struct usb_descriptor_header *) &otg_descriptor,
  163. NULL,
  164. };
  165. static struct usb_string strings_dev[] = {
  166. [USB_GADGET_MANUFACTURER_IDX].s = "",
  167. [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
  168. [USB_GADGET_SERIAL_IDX].s = "",
  169. { } /* end of list */
  170. };
  171. static struct usb_gadget_strings stringtab_dev = {
  172. .language = 0x0409, /* en-us */
  173. .strings = strings_dev,
  174. };
  175. static struct usb_gadget_strings *dev_strings[] = {
  176. &stringtab_dev,
  177. NULL,
  178. };
  179. static u8 hostaddr[ETH_ALEN];
  180. /*-------------------------------------------------------------------------*/
  181. /*
  182. * We may not have an RNDIS configuration, but if we do it needs to be
  183. * the first one present. That's to make Microsoft's drivers happy,
  184. * and to follow DOCSIS 1.0 (cable modem standard).
  185. */
  186. static int __init rndis_do_config(struct usb_configuration *c)
  187. {
  188. /* FIXME alloc iConfiguration string, set it in c->strings */
  189. if (gadget_is_otg(c->cdev->gadget)) {
  190. c->descriptors = otg_desc;
  191. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  192. }
  193. return rndis_bind_config(c, hostaddr);
  194. }
  195. static struct usb_configuration rndis_config_driver = {
  196. .label = "RNDIS",
  197. .bConfigurationValue = 2,
  198. /* .iConfiguration = DYNAMIC */
  199. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  200. };
  201. /*-------------------------------------------------------------------------*/
  202. #ifdef CONFIG_USB_ETH_EEM
  203. static bool use_eem = 1;
  204. #else
  205. static bool use_eem;
  206. #endif
  207. module_param(use_eem, bool, 0);
  208. MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
  209. /*
  210. * We _always_ have an ECM, CDC Subset, or EEM configuration.
  211. */
  212. static int __init eth_do_config(struct usb_configuration *c)
  213. {
  214. /* FIXME alloc iConfiguration string, set it in c->strings */
  215. if (gadget_is_otg(c->cdev->gadget)) {
  216. c->descriptors = otg_desc;
  217. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  218. }
  219. if (use_eem)
  220. return eem_bind_config(c);
  221. else if (can_support_ecm(c->cdev->gadget))
  222. return ecm_bind_config(c, hostaddr);
  223. else
  224. return geth_bind_config(c, hostaddr);
  225. }
  226. static struct usb_configuration eth_config_driver = {
  227. /* .label = f(hardware) */
  228. .bConfigurationValue = 1,
  229. /* .iConfiguration = DYNAMIC */
  230. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  231. };
  232. /*-------------------------------------------------------------------------*/
  233. static int __init eth_bind(struct usb_composite_dev *cdev)
  234. {
  235. struct usb_gadget *gadget = cdev->gadget;
  236. int status;
  237. /* set up network link layer */
  238. status = gether_setup(cdev->gadget, hostaddr);
  239. if (status < 0)
  240. return status;
  241. /* set up main config label and device descriptor */
  242. if (use_eem) {
  243. /* EEM */
  244. eth_config_driver.label = "CDC Ethernet (EEM)";
  245. device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
  246. device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
  247. } else if (can_support_ecm(cdev->gadget)) {
  248. /* ECM */
  249. eth_config_driver.label = "CDC Ethernet (ECM)";
  250. } else {
  251. /* CDC Subset */
  252. eth_config_driver.label = "CDC Subset/SAFE";
  253. device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
  254. device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
  255. if (!has_rndis())
  256. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  257. }
  258. if (has_rndis()) {
  259. /* RNDIS plus ECM-or-Subset */
  260. device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
  261. device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
  262. device_desc.bNumConfigurations = 2;
  263. }
  264. /* Allocate string descriptor numbers ... note that string
  265. * contents can be overridden by the composite_dev glue.
  266. */
  267. status = usb_string_ids_tab(cdev, strings_dev);
  268. if (status < 0)
  269. goto fail;
  270. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  271. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  272. /* register our configuration(s); RNDIS first, if it's used */
  273. if (has_rndis()) {
  274. status = usb_add_config(cdev, &rndis_config_driver,
  275. rndis_do_config);
  276. if (status < 0)
  277. goto fail;
  278. }
  279. status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
  280. if (status < 0)
  281. goto fail;
  282. usb_composite_overwrite_options(cdev, &coverwrite);
  283. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  284. DRIVER_DESC);
  285. return 0;
  286. fail:
  287. gether_cleanup();
  288. return status;
  289. }
  290. static int __exit eth_unbind(struct usb_composite_dev *cdev)
  291. {
  292. gether_cleanup();
  293. return 0;
  294. }
  295. static __refdata struct usb_composite_driver eth_driver = {
  296. .name = "g_ether",
  297. .dev = &device_desc,
  298. .strings = dev_strings,
  299. .max_speed = USB_SPEED_SUPER,
  300. .bind = eth_bind,
  301. .unbind = __exit_p(eth_unbind),
  302. };
  303. MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
  304. MODULE_AUTHOR("David Brownell, Benedikt Spanger");
  305. MODULE_LICENSE("GPL");
  306. static int __init init(void)
  307. {
  308. return usb_composite_probe(&eth_driver);
  309. }
  310. module_init(init);
  311. static void __exit cleanup(void)
  312. {
  313. usb_composite_unregister(&eth_driver);
  314. }
  315. module_exit(cleanup);