ether.c 12 KB

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