ether.c 12 KB

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