ether.c 13 KB

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