ether.c 13 KB

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