ether.c 13 KB

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