ether.c 13 KB

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