ether.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. #include <linux/module.h>
  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 "u_ecm.h"
  93. #include "f_subset.c"
  94. #ifdef USB_ETH_RNDIS
  95. #include "f_rndis.c"
  96. #include "rndis.h"
  97. #endif
  98. #include "u_eem.h"
  99. /*-------------------------------------------------------------------------*/
  100. USB_GADGET_COMPOSITE_OPTIONS();
  101. USB_ETHERNET_MODULE_PARAMETERS();
  102. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  103. * Instead: allocate your own, using normal USB-IF procedures.
  104. */
  105. /* Thanks to NetChip Technologies for donating this product ID.
  106. * It's for devices with only CDC Ethernet configurations.
  107. */
  108. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  109. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  110. /* For hardware that can't talk CDC, we use the same vendor ID that
  111. * ARM Linux has used for ethernet-over-usb, both with sa1100 and
  112. * with pxa250. We're protocol-compatible, if the host-side drivers
  113. * use the endpoint descriptors. bcdDevice (version) is nonzero, so
  114. * drivers that need to hard-wire endpoint numbers have a hook.
  115. *
  116. * The protocol is a minimal subset of CDC Ether, which works on any bulk
  117. * hardware that's not deeply broken ... even on hardware that can't talk
  118. * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
  119. * doesn't handle control-OUT).
  120. */
  121. #define SIMPLE_VENDOR_NUM 0x049f
  122. #define SIMPLE_PRODUCT_NUM 0x505a
  123. /* For hardware that can talk RNDIS and either of the above protocols,
  124. * use this ID ... the windows INF files will know it. Unless it's
  125. * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
  126. * the non-RNDIS configuration.
  127. */
  128. #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
  129. #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
  130. /* For EEM gadgets */
  131. #define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */
  132. #define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */
  133. /*-------------------------------------------------------------------------*/
  134. static struct usb_device_descriptor device_desc = {
  135. .bLength = sizeof device_desc,
  136. .bDescriptorType = USB_DT_DEVICE,
  137. .bcdUSB = cpu_to_le16 (0x0200),
  138. .bDeviceClass = USB_CLASS_COMM,
  139. .bDeviceSubClass = 0,
  140. .bDeviceProtocol = 0,
  141. /* .bMaxPacketSize0 = f(hardware) */
  142. /* Vendor and product id defaults change according to what configs
  143. * we support. (As does bNumConfigurations.) These values can
  144. * also be overridden by module parameters.
  145. */
  146. .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
  147. .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
  148. /* .bcdDevice = f(hardware) */
  149. /* .iManufacturer = DYNAMIC */
  150. /* .iProduct = DYNAMIC */
  151. /* NO SERIAL NUMBER */
  152. .bNumConfigurations = 1,
  153. };
  154. static struct usb_otg_descriptor otg_descriptor = {
  155. .bLength = sizeof otg_descriptor,
  156. .bDescriptorType = USB_DT_OTG,
  157. /* REVISIT SRP-only hardware is possible, although
  158. * it would not be called "OTG" ...
  159. */
  160. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  161. };
  162. static const struct usb_descriptor_header *otg_desc[] = {
  163. (struct usb_descriptor_header *) &otg_descriptor,
  164. NULL,
  165. };
  166. static struct usb_string strings_dev[] = {
  167. [USB_GADGET_MANUFACTURER_IDX].s = "",
  168. [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
  169. [USB_GADGET_SERIAL_IDX].s = "",
  170. { } /* end of list */
  171. };
  172. static struct usb_gadget_strings stringtab_dev = {
  173. .language = 0x0409, /* en-us */
  174. .strings = strings_dev,
  175. };
  176. static struct usb_gadget_strings *dev_strings[] = {
  177. &stringtab_dev,
  178. NULL,
  179. };
  180. static u8 host_mac[ETH_ALEN];
  181. static struct eth_dev *the_dev;
  182. static struct usb_function_instance *fi_ecm;
  183. static struct usb_function *f_ecm;
  184. static struct usb_function_instance *fi_eem;
  185. static struct usb_function *f_eem;
  186. /*-------------------------------------------------------------------------*/
  187. /*
  188. * We may not have an RNDIS configuration, but if we do it needs to be
  189. * the first one present. That's to make Microsoft's drivers happy,
  190. * and to follow DOCSIS 1.0 (cable modem standard).
  191. */
  192. static int __init rndis_do_config(struct usb_configuration *c)
  193. {
  194. /* FIXME alloc iConfiguration string, set it in c->strings */
  195. if (gadget_is_otg(c->cdev->gadget)) {
  196. c->descriptors = otg_desc;
  197. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  198. }
  199. return rndis_bind_config(c, host_mac, the_dev);
  200. }
  201. static struct usb_configuration rndis_config_driver = {
  202. .label = "RNDIS",
  203. .bConfigurationValue = 2,
  204. /* .iConfiguration = DYNAMIC */
  205. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  206. };
  207. /*-------------------------------------------------------------------------*/
  208. #ifdef CONFIG_USB_ETH_EEM
  209. static bool use_eem = 1;
  210. #else
  211. static bool use_eem;
  212. #endif
  213. module_param(use_eem, bool, 0);
  214. MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
  215. /*
  216. * We _always_ have an ECM, CDC Subset, or EEM configuration.
  217. */
  218. static int __init eth_do_config(struct usb_configuration *c)
  219. {
  220. int status = 0;
  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. f_eem = usb_get_function(fi_eem);
  228. if (IS_ERR(f_eem))
  229. return PTR_ERR(f_eem);
  230. status = usb_add_function(c, f_eem);
  231. if (status < 0)
  232. usb_put_function(f_eem);
  233. return status;
  234. } else if (can_support_ecm(c->cdev->gadget)) {
  235. f_ecm = usb_get_function(fi_ecm);
  236. if (IS_ERR(f_ecm))
  237. return PTR_ERR(f_ecm);
  238. status = usb_add_function(c, f_ecm);
  239. if (status < 0)
  240. usb_put_function(f_ecm);
  241. return status;
  242. } else
  243. return geth_bind_config(c, host_mac, the_dev);
  244. }
  245. static struct usb_configuration eth_config_driver = {
  246. /* .label = f(hardware) */
  247. .bConfigurationValue = 1,
  248. /* .iConfiguration = DYNAMIC */
  249. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  250. };
  251. /*-------------------------------------------------------------------------*/
  252. static int __init eth_bind(struct usb_composite_dev *cdev)
  253. {
  254. struct usb_gadget *gadget = cdev->gadget;
  255. struct f_eem_opts *eem_opts = NULL;
  256. struct f_ecm_opts *ecm_opts = NULL;
  257. int status;
  258. if (!use_eem && !can_support_ecm(gadget)) {
  259. /* set up network link layer */
  260. the_dev = gether_setup(cdev->gadget, dev_addr, host_addr,
  261. host_mac, qmult);
  262. if (IS_ERR(the_dev))
  263. return PTR_ERR(the_dev);
  264. }
  265. /* set up main config label and device descriptor */
  266. if (use_eem) {
  267. /* EEM */
  268. fi_eem = usb_get_function_instance("eem");
  269. if (IS_ERR(fi_eem))
  270. return PTR_ERR(fi_eem);
  271. eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
  272. gether_set_qmult(eem_opts->net, qmult);
  273. if (!gether_set_host_addr(eem_opts->net, host_addr))
  274. pr_info("using host ethernet address: %s", host_addr);
  275. if (!gether_set_dev_addr(eem_opts->net, dev_addr))
  276. pr_info("using self ethernet address: %s", dev_addr);
  277. the_dev = netdev_priv(eem_opts->net);
  278. eth_config_driver.label = "CDC Ethernet (EEM)";
  279. device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
  280. device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
  281. } else if (can_support_ecm(gadget)) {
  282. /* ECM */
  283. fi_ecm = usb_get_function_instance("ecm");
  284. if (IS_ERR(fi_ecm))
  285. return PTR_ERR(fi_ecm);
  286. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  287. gether_set_qmult(ecm_opts->net, qmult);
  288. if (!gether_set_host_addr(ecm_opts->net, host_addr))
  289. pr_info("using host ethernet address: %s", host_addr);
  290. if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
  291. pr_info("using self ethernet address: %s", dev_addr);
  292. the_dev = netdev_priv(ecm_opts->net);
  293. eth_config_driver.label = "CDC Ethernet (ECM)";
  294. } else {
  295. /* CDC Subset */
  296. eth_config_driver.label = "CDC Subset/SAFE";
  297. device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
  298. device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
  299. if (!has_rndis())
  300. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  301. }
  302. if (has_rndis()) {
  303. /* RNDIS plus ECM-or-Subset */
  304. if (use_eem) {
  305. gether_set_gadget(eem_opts->net, cdev->gadget);
  306. status = gether_register_netdev(eem_opts->net);
  307. if (status)
  308. goto fail;
  309. eem_opts->bound = true;
  310. gether_get_host_addr_u8(eem_opts->net, host_mac);
  311. } else if (can_support_ecm(gadget)) {
  312. gether_set_gadget(ecm_opts->net, cdev->gadget);
  313. status = gether_register_netdev(ecm_opts->net);
  314. if (status)
  315. goto fail;
  316. ecm_opts->bound = true;
  317. gether_get_host_addr_u8(ecm_opts->net, host_mac);
  318. }
  319. device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
  320. device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
  321. device_desc.bNumConfigurations = 2;
  322. }
  323. /* Allocate string descriptor numbers ... note that string
  324. * contents can be overridden by the composite_dev glue.
  325. */
  326. status = usb_string_ids_tab(cdev, strings_dev);
  327. if (status < 0)
  328. goto fail;
  329. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  330. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  331. /* register our configuration(s); RNDIS first, if it's used */
  332. if (has_rndis()) {
  333. status = usb_add_config(cdev, &rndis_config_driver,
  334. rndis_do_config);
  335. if (status < 0)
  336. goto fail;
  337. }
  338. status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
  339. if (status < 0)
  340. goto fail;
  341. usb_composite_overwrite_options(cdev, &coverwrite);
  342. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  343. DRIVER_DESC);
  344. return 0;
  345. fail:
  346. if (!use_eem && !can_support_ecm(gadget))
  347. gether_cleanup(the_dev);
  348. else if (use_eem)
  349. usb_put_function_instance(fi_eem);
  350. else
  351. usb_put_function_instance(fi_ecm);
  352. return status;
  353. }
  354. static int __exit eth_unbind(struct usb_composite_dev *cdev)
  355. {
  356. if (!use_eem && !can_support_ecm(cdev->gadget))
  357. gether_cleanup(the_dev);
  358. else if (use_eem)
  359. usb_put_function_instance(fi_eem);
  360. else
  361. usb_put_function_instance(fi_ecm);
  362. return 0;
  363. }
  364. static __refdata struct usb_composite_driver eth_driver = {
  365. .name = "g_ether",
  366. .dev = &device_desc,
  367. .strings = dev_strings,
  368. .max_speed = USB_SPEED_SUPER,
  369. .bind = eth_bind,
  370. .unbind = __exit_p(eth_unbind),
  371. };
  372. MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
  373. MODULE_AUTHOR("David Brownell, Benedikt Spanger");
  374. MODULE_LICENSE("GPL");
  375. static int __init init(void)
  376. {
  377. return usb_composite_probe(&eth_driver);
  378. }
  379. module_init(init);
  380. static void __exit cleanup(void)
  381. {
  382. usb_composite_unregister(&eth_driver);
  383. }
  384. module_exit(cleanup);