ether.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
  4. *
  5. * Copyright (C) 2003-2005,2008 David Brownell
  6. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  7. * Copyright (C) 2008 Nokia Corporation
  8. */
  9. /* #define VERBOSE_DEBUG */
  10. #include <linux/kernel.h>
  11. #include <linux/netdevice.h>
  12. #if defined USB_ETH_RNDIS
  13. # undef USB_ETH_RNDIS
  14. #endif
  15. #ifdef CONFIG_USB_ETH_RNDIS
  16. # define USB_ETH_RNDIS y
  17. #endif
  18. #include "u_ether.h"
  19. /*
  20. * Ethernet gadget driver -- with CDC and non-CDC options
  21. * Builds on hardware support for a full duplex link.
  22. *
  23. * CDC Ethernet is the standard USB solution for sending Ethernet frames
  24. * using USB. Real hardware tends to use the same framing protocol but look
  25. * different for control features. This driver strongly prefers to use
  26. * this USB-IF standard as its open-systems interoperability solution;
  27. * most host side USB stacks (except from Microsoft) support it.
  28. *
  29. * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
  30. * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
  31. * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
  32. *
  33. * There's some hardware that can't talk CDC ECM. We make that hardware
  34. * implement a "minimalist" vendor-agnostic CDC core: same framing, but
  35. * link-level setup only requires activating the configuration. Only the
  36. * endpoint descriptors, and product/vendor IDs, are relevant; no control
  37. * operations are available. Linux supports it, but other host operating
  38. * systems may not. (This is a subset of CDC Ethernet.)
  39. *
  40. * It turns out that if you add a few descriptors to that "CDC Subset",
  41. * (Windows) host side drivers from MCCI can treat it as one submode of
  42. * a proprietary scheme called "SAFE" ... without needing to know about
  43. * specific product/vendor IDs. So we do that, making it easier to use
  44. * those MS-Windows drivers. Those added descriptors make it resemble a
  45. * CDC MDLM device, but they don't change device behavior at all. (See
  46. * MCCI Engineering report 950198 "SAFE Networking Functions".)
  47. *
  48. * A third option is also in use. Rather than CDC Ethernet, or something
  49. * simpler, Microsoft pushes their own approach: RNDIS. The published
  50. * RNDIS specs are ambiguous and appear to be incomplete, and are also
  51. * needlessly complex. They borrow more from CDC ACM than CDC ECM.
  52. */
  53. #define DRIVER_DESC "Ethernet Gadget"
  54. #define DRIVER_VERSION "Memorial Day 2008"
  55. #ifdef USB_ETH_RNDIS
  56. #define PREFIX "RNDIS/"
  57. #else
  58. #define PREFIX ""
  59. #endif
  60. /*
  61. * This driver aims for interoperability by using CDC ECM unless
  62. *
  63. * can_support_ecm()
  64. *
  65. * returns false, in which case it supports the CDC Subset. By default,
  66. * that returns true; most hardware has no problems with CDC ECM, that's
  67. * a good default. Previous versions of this driver had no default; this
  68. * version changes that, removing overhead for new controller support.
  69. *
  70. * IF YOUR HARDWARE CAN'T SUPPORT CDC ECM, UPDATE THAT ROUTINE!
  71. */
  72. static inline bool has_rndis(void)
  73. {
  74. #ifdef USB_ETH_RNDIS
  75. return true;
  76. #else
  77. return false;
  78. #endif
  79. }
  80. #include <linux/module.h>
  81. #include "u_ecm.h"
  82. #include "u_gether.h"
  83. #ifdef USB_ETH_RNDIS
  84. #include "u_rndis.h"
  85. #include "rndis.h"
  86. #else
  87. #define rndis_borrow_net(...) do {} while (0)
  88. #endif
  89. #include "u_eem.h"
  90. /*-------------------------------------------------------------------------*/
  91. USB_GADGET_COMPOSITE_OPTIONS();
  92. USB_ETHERNET_MODULE_PARAMETERS();
  93. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  94. * Instead: allocate your own, using normal USB-IF procedures.
  95. */
  96. /* Thanks to NetChip Technologies for donating this product ID.
  97. * It's for devices with only CDC Ethernet configurations.
  98. */
  99. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  100. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  101. /* For hardware that can't talk CDC, we use the same vendor ID that
  102. * ARM Linux has used for ethernet-over-usb, both with sa1100 and
  103. * with pxa250. We're protocol-compatible, if the host-side drivers
  104. * use the endpoint descriptors. bcdDevice (version) is nonzero, so
  105. * drivers that need to hard-wire endpoint numbers have a hook.
  106. *
  107. * The protocol is a minimal subset of CDC Ether, which works on any bulk
  108. * hardware that's not deeply broken ... even on hardware that can't talk
  109. * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
  110. * doesn't handle control-OUT).
  111. */
  112. #define SIMPLE_VENDOR_NUM 0x049f
  113. #define SIMPLE_PRODUCT_NUM 0x505a
  114. /* For hardware that can talk RNDIS and either of the above protocols,
  115. * use this ID ... the windows INF files will know it. Unless it's
  116. * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
  117. * the non-RNDIS configuration.
  118. */
  119. #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
  120. #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
  121. /* For EEM gadgets */
  122. #define EEM_VENDOR_NUM 0x1d6b /* Linux Foundation */
  123. #define EEM_PRODUCT_NUM 0x0102 /* EEM Gadget */
  124. /*-------------------------------------------------------------------------*/
  125. static struct usb_device_descriptor device_desc = {
  126. .bLength = sizeof device_desc,
  127. .bDescriptorType = USB_DT_DEVICE,
  128. /* .bcdUSB = DYNAMIC */
  129. .bDeviceClass = USB_CLASS_COMM,
  130. .bDeviceSubClass = 0,
  131. .bDeviceProtocol = 0,
  132. /* .bMaxPacketSize0 = f(hardware) */
  133. /* Vendor and product id defaults change according to what configs
  134. * we support. (As does bNumConfigurations.) These values can
  135. * also be overridden by module parameters.
  136. */
  137. .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
  138. .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
  139. /* .bcdDevice = f(hardware) */
  140. /* .iManufacturer = DYNAMIC */
  141. /* .iProduct = DYNAMIC */
  142. /* NO SERIAL NUMBER */
  143. .bNumConfigurations = 1,
  144. };
  145. static const struct usb_descriptor_header *otg_desc[2];
  146. static struct usb_string strings_dev[] = {
  147. [USB_GADGET_MANUFACTURER_IDX].s = "",
  148. [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
  149. [USB_GADGET_SERIAL_IDX].s = "",
  150. { } /* end of list */
  151. };
  152. static struct usb_gadget_strings stringtab_dev = {
  153. .language = 0x0409, /* en-us */
  154. .strings = strings_dev,
  155. };
  156. static struct usb_gadget_strings *dev_strings[] = {
  157. &stringtab_dev,
  158. NULL,
  159. };
  160. static struct usb_function_instance *fi_ecm;
  161. static struct usb_function *f_ecm;
  162. static struct usb_function_instance *fi_eem;
  163. static struct usb_function *f_eem;
  164. static struct usb_function_instance *fi_geth;
  165. static struct usb_function *f_geth;
  166. static struct usb_function_instance *fi_rndis;
  167. static struct usb_function *f_rndis;
  168. /*-------------------------------------------------------------------------*/
  169. /*
  170. * We may not have an RNDIS configuration, but if we do it needs to be
  171. * the first one present. That's to make Microsoft's drivers happy,
  172. * and to follow DOCSIS 1.0 (cable modem standard).
  173. */
  174. static int rndis_do_config(struct usb_configuration *c)
  175. {
  176. int status;
  177. /* FIXME alloc iConfiguration string, set it in c->strings */
  178. if (gadget_is_otg(c->cdev->gadget)) {
  179. c->descriptors = otg_desc;
  180. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  181. }
  182. f_rndis = usb_get_function(fi_rndis);
  183. if (IS_ERR(f_rndis))
  184. return PTR_ERR(f_rndis);
  185. status = usb_add_function(c, f_rndis);
  186. if (status < 0)
  187. usb_put_function(f_rndis);
  188. return status;
  189. }
  190. static struct usb_configuration rndis_config_driver = {
  191. .label = "RNDIS",
  192. .bConfigurationValue = 2,
  193. /* .iConfiguration = DYNAMIC */
  194. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  195. };
  196. /*-------------------------------------------------------------------------*/
  197. #ifdef CONFIG_USB_ETH_EEM
  198. static bool use_eem = 1;
  199. #else
  200. static bool use_eem;
  201. #endif
  202. module_param(use_eem, bool, 0);
  203. MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
  204. /*
  205. * We _always_ have an ECM, CDC Subset, or EEM configuration.
  206. */
  207. static int eth_do_config(struct usb_configuration *c)
  208. {
  209. int status = 0;
  210. /* FIXME alloc iConfiguration string, set it in c->strings */
  211. if (gadget_is_otg(c->cdev->gadget)) {
  212. c->descriptors = otg_desc;
  213. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  214. }
  215. if (use_eem) {
  216. f_eem = usb_get_function(fi_eem);
  217. if (IS_ERR(f_eem))
  218. return PTR_ERR(f_eem);
  219. status = usb_add_function(c, f_eem);
  220. if (status < 0)
  221. usb_put_function(f_eem);
  222. return status;
  223. } else if (can_support_ecm(c->cdev->gadget)) {
  224. f_ecm = usb_get_function(fi_ecm);
  225. if (IS_ERR(f_ecm))
  226. return PTR_ERR(f_ecm);
  227. status = usb_add_function(c, f_ecm);
  228. if (status < 0)
  229. usb_put_function(f_ecm);
  230. return status;
  231. } else {
  232. f_geth = usb_get_function(fi_geth);
  233. if (IS_ERR(f_geth))
  234. return PTR_ERR(f_geth);
  235. status = usb_add_function(c, f_geth);
  236. if (status < 0)
  237. usb_put_function(f_geth);
  238. return status;
  239. }
  240. }
  241. static struct usb_configuration eth_config_driver = {
  242. /* .label = f(hardware) */
  243. .bConfigurationValue = 1,
  244. /* .iConfiguration = DYNAMIC */
  245. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  246. };
  247. /*-------------------------------------------------------------------------*/
  248. static int eth_bind(struct usb_composite_dev *cdev)
  249. {
  250. struct usb_gadget *gadget = cdev->gadget;
  251. struct f_eem_opts *eem_opts = NULL;
  252. struct f_ecm_opts *ecm_opts = NULL;
  253. struct f_gether_opts *geth_opts = NULL;
  254. struct net_device *net;
  255. int status;
  256. /* set up main config label and device descriptor */
  257. if (use_eem) {
  258. /* EEM */
  259. fi_eem = usb_get_function_instance("eem");
  260. if (IS_ERR(fi_eem))
  261. return PTR_ERR(fi_eem);
  262. eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
  263. net = eem_opts->net;
  264. eth_config_driver.label = "CDC Ethernet (EEM)";
  265. device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
  266. device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
  267. } else if (can_support_ecm(gadget)) {
  268. /* ECM */
  269. fi_ecm = usb_get_function_instance("ecm");
  270. if (IS_ERR(fi_ecm))
  271. return PTR_ERR(fi_ecm);
  272. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  273. net = ecm_opts->net;
  274. eth_config_driver.label = "CDC Ethernet (ECM)";
  275. } else {
  276. /* CDC Subset */
  277. fi_geth = usb_get_function_instance("geth");
  278. if (IS_ERR(fi_geth))
  279. return PTR_ERR(fi_geth);
  280. geth_opts = container_of(fi_geth, struct f_gether_opts,
  281. func_inst);
  282. net = geth_opts->net;
  283. eth_config_driver.label = "CDC Subset/SAFE";
  284. device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
  285. device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
  286. if (!has_rndis())
  287. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  288. }
  289. gether_set_qmult(net, qmult);
  290. if (!gether_set_host_addr(net, host_addr))
  291. pr_info("using host ethernet address: %s", host_addr);
  292. if (!gether_set_dev_addr(net, dev_addr))
  293. pr_info("using self ethernet address: %s", dev_addr);
  294. if (has_rndis()) {
  295. /* RNDIS plus ECM-or-Subset */
  296. gether_set_gadget(net, cdev->gadget);
  297. status = gether_register_netdev(net);
  298. if (status)
  299. goto fail;
  300. if (use_eem)
  301. eem_opts->bound = true;
  302. else if (can_support_ecm(gadget))
  303. ecm_opts->bound = true;
  304. else
  305. geth_opts->bound = true;
  306. fi_rndis = usb_get_function_instance("rndis");
  307. if (IS_ERR(fi_rndis)) {
  308. status = PTR_ERR(fi_rndis);
  309. goto fail;
  310. }
  311. rndis_borrow_net(fi_rndis, net);
  312. device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
  313. device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
  314. device_desc.bNumConfigurations = 2;
  315. }
  316. /* Allocate string descriptor numbers ... note that string
  317. * contents can be overridden by the composite_dev glue.
  318. */
  319. status = usb_string_ids_tab(cdev, strings_dev);
  320. if (status < 0)
  321. goto fail1;
  322. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  323. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  324. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  325. struct usb_descriptor_header *usb_desc;
  326. usb_desc = usb_otg_descriptor_alloc(gadget);
  327. if (!usb_desc)
  328. goto fail1;
  329. usb_otg_descriptor_init(gadget, usb_desc);
  330. otg_desc[0] = usb_desc;
  331. otg_desc[1] = NULL;
  332. }
  333. /* register our configuration(s); RNDIS first, if it's used */
  334. if (has_rndis()) {
  335. status = usb_add_config(cdev, &rndis_config_driver,
  336. rndis_do_config);
  337. if (status < 0)
  338. goto fail2;
  339. }
  340. status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
  341. if (status < 0)
  342. goto fail2;
  343. usb_composite_overwrite_options(cdev, &coverwrite);
  344. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  345. DRIVER_DESC);
  346. return 0;
  347. fail2:
  348. kfree(otg_desc[0]);
  349. otg_desc[0] = NULL;
  350. fail1:
  351. if (has_rndis())
  352. usb_put_function_instance(fi_rndis);
  353. fail:
  354. if (use_eem)
  355. usb_put_function_instance(fi_eem);
  356. else if (can_support_ecm(gadget))
  357. usb_put_function_instance(fi_ecm);
  358. else
  359. usb_put_function_instance(fi_geth);
  360. return status;
  361. }
  362. static int eth_unbind(struct usb_composite_dev *cdev)
  363. {
  364. if (has_rndis()) {
  365. usb_put_function(f_rndis);
  366. usb_put_function_instance(fi_rndis);
  367. }
  368. if (use_eem) {
  369. usb_put_function(f_eem);
  370. usb_put_function_instance(fi_eem);
  371. } else if (can_support_ecm(cdev->gadget)) {
  372. usb_put_function(f_ecm);
  373. usb_put_function_instance(fi_ecm);
  374. } else {
  375. usb_put_function(f_geth);
  376. usb_put_function_instance(fi_geth);
  377. }
  378. kfree(otg_desc[0]);
  379. otg_desc[0] = NULL;
  380. return 0;
  381. }
  382. static struct usb_composite_driver eth_driver = {
  383. .name = "g_ether",
  384. .dev = &device_desc,
  385. .strings = dev_strings,
  386. .max_speed = USB_SPEED_SUPER,
  387. .bind = eth_bind,
  388. .unbind = eth_unbind,
  389. };
  390. module_usb_composite_driver(eth_driver);
  391. MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
  392. MODULE_AUTHOR("David Brownell, Benedikt Spanger");
  393. MODULE_LICENSE("GPL");