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 const struct usb_descriptor_header *otg_desc[2];
  150. static struct usb_string strings_dev[] = {
  151. [USB_GADGET_MANUFACTURER_IDX].s = "",
  152. [USB_GADGET_PRODUCT_IDX].s = PREFIX DRIVER_DESC,
  153. [USB_GADGET_SERIAL_IDX].s = "",
  154. { } /* end of list */
  155. };
  156. static struct usb_gadget_strings stringtab_dev = {
  157. .language = 0x0409, /* en-us */
  158. .strings = strings_dev,
  159. };
  160. static struct usb_gadget_strings *dev_strings[] = {
  161. &stringtab_dev,
  162. NULL,
  163. };
  164. static struct usb_function_instance *fi_ecm;
  165. static struct usb_function *f_ecm;
  166. static struct usb_function_instance *fi_eem;
  167. static struct usb_function *f_eem;
  168. static struct usb_function_instance *fi_geth;
  169. static struct usb_function *f_geth;
  170. static struct usb_function_instance *fi_rndis;
  171. static struct usb_function *f_rndis;
  172. /*-------------------------------------------------------------------------*/
  173. /*
  174. * We may not have an RNDIS configuration, but if we do it needs to be
  175. * the first one present. That's to make Microsoft's drivers happy,
  176. * and to follow DOCSIS 1.0 (cable modem standard).
  177. */
  178. static int rndis_do_config(struct usb_configuration *c)
  179. {
  180. int status;
  181. /* FIXME alloc iConfiguration string, set it in c->strings */
  182. if (gadget_is_otg(c->cdev->gadget)) {
  183. c->descriptors = otg_desc;
  184. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  185. }
  186. f_rndis = usb_get_function(fi_rndis);
  187. if (IS_ERR(f_rndis))
  188. return PTR_ERR(f_rndis);
  189. status = usb_add_function(c, f_rndis);
  190. if (status < 0)
  191. usb_put_function(f_rndis);
  192. return status;
  193. }
  194. static struct usb_configuration rndis_config_driver = {
  195. .label = "RNDIS",
  196. .bConfigurationValue = 2,
  197. /* .iConfiguration = DYNAMIC */
  198. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  199. };
  200. /*-------------------------------------------------------------------------*/
  201. #ifdef CONFIG_USB_ETH_EEM
  202. static bool use_eem = 1;
  203. #else
  204. static bool use_eem;
  205. #endif
  206. module_param(use_eem, bool, 0);
  207. MODULE_PARM_DESC(use_eem, "use CDC EEM mode");
  208. /*
  209. * We _always_ have an ECM, CDC Subset, or EEM configuration.
  210. */
  211. static int eth_do_config(struct usb_configuration *c)
  212. {
  213. int status = 0;
  214. /* FIXME alloc iConfiguration string, set it in c->strings */
  215. if (gadget_is_otg(c->cdev->gadget)) {
  216. c->descriptors = otg_desc;
  217. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  218. }
  219. if (use_eem) {
  220. f_eem = usb_get_function(fi_eem);
  221. if (IS_ERR(f_eem))
  222. return PTR_ERR(f_eem);
  223. status = usb_add_function(c, f_eem);
  224. if (status < 0)
  225. usb_put_function(f_eem);
  226. return status;
  227. } else if (can_support_ecm(c->cdev->gadget)) {
  228. f_ecm = usb_get_function(fi_ecm);
  229. if (IS_ERR(f_ecm))
  230. return PTR_ERR(f_ecm);
  231. status = usb_add_function(c, f_ecm);
  232. if (status < 0)
  233. usb_put_function(f_ecm);
  234. return status;
  235. } else {
  236. f_geth = usb_get_function(fi_geth);
  237. if (IS_ERR(f_geth))
  238. return PTR_ERR(f_geth);
  239. status = usb_add_function(c, f_geth);
  240. if (status < 0)
  241. usb_put_function(f_geth);
  242. return status;
  243. }
  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 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. struct f_gether_opts *geth_opts = NULL;
  258. struct net_device *net;
  259. int status;
  260. /* set up main config label and device descriptor */
  261. if (use_eem) {
  262. /* EEM */
  263. fi_eem = usb_get_function_instance("eem");
  264. if (IS_ERR(fi_eem))
  265. return PTR_ERR(fi_eem);
  266. eem_opts = container_of(fi_eem, struct f_eem_opts, func_inst);
  267. net = eem_opts->net;
  268. eth_config_driver.label = "CDC Ethernet (EEM)";
  269. device_desc.idVendor = cpu_to_le16(EEM_VENDOR_NUM);
  270. device_desc.idProduct = cpu_to_le16(EEM_PRODUCT_NUM);
  271. } else if (can_support_ecm(gadget)) {
  272. /* ECM */
  273. fi_ecm = usb_get_function_instance("ecm");
  274. if (IS_ERR(fi_ecm))
  275. return PTR_ERR(fi_ecm);
  276. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  277. net = ecm_opts->net;
  278. eth_config_driver.label = "CDC Ethernet (ECM)";
  279. } else {
  280. /* CDC Subset */
  281. fi_geth = usb_get_function_instance("geth");
  282. if (IS_ERR(fi_geth))
  283. return PTR_ERR(fi_geth);
  284. geth_opts = container_of(fi_geth, struct f_gether_opts,
  285. func_inst);
  286. net = geth_opts->net;
  287. eth_config_driver.label = "CDC Subset/SAFE";
  288. device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
  289. device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
  290. if (!has_rndis())
  291. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  292. }
  293. gether_set_qmult(net, qmult);
  294. if (!gether_set_host_addr(net, host_addr))
  295. pr_info("using host ethernet address: %s", host_addr);
  296. if (!gether_set_dev_addr(net, dev_addr))
  297. pr_info("using self ethernet address: %s", dev_addr);
  298. if (has_rndis()) {
  299. /* RNDIS plus ECM-or-Subset */
  300. gether_set_gadget(net, cdev->gadget);
  301. status = gether_register_netdev(net);
  302. if (status)
  303. goto fail;
  304. if (use_eem)
  305. eem_opts->bound = true;
  306. else if (can_support_ecm(gadget))
  307. ecm_opts->bound = true;
  308. else
  309. geth_opts->bound = true;
  310. fi_rndis = usb_get_function_instance("rndis");
  311. if (IS_ERR(fi_rndis)) {
  312. status = PTR_ERR(fi_rndis);
  313. goto fail;
  314. }
  315. rndis_borrow_net(fi_rndis, net);
  316. device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
  317. device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
  318. device_desc.bNumConfigurations = 2;
  319. }
  320. /* Allocate string descriptor numbers ... note that string
  321. * contents can be overridden by the composite_dev glue.
  322. */
  323. status = usb_string_ids_tab(cdev, strings_dev);
  324. if (status < 0)
  325. goto fail1;
  326. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  327. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  328. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  329. struct usb_descriptor_header *usb_desc;
  330. usb_desc = usb_otg_descriptor_alloc(gadget);
  331. if (!usb_desc)
  332. goto fail1;
  333. usb_otg_descriptor_init(gadget, usb_desc);
  334. otg_desc[0] = usb_desc;
  335. otg_desc[1] = NULL;
  336. }
  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 fail2;
  343. }
  344. status = usb_add_config(cdev, &eth_config_driver, eth_do_config);
  345. if (status < 0)
  346. goto fail2;
  347. usb_composite_overwrite_options(cdev, &coverwrite);
  348. dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
  349. DRIVER_DESC);
  350. return 0;
  351. fail2:
  352. kfree(otg_desc[0]);
  353. otg_desc[0] = NULL;
  354. fail1:
  355. if (has_rndis())
  356. usb_put_function_instance(fi_rndis);
  357. fail:
  358. if (use_eem)
  359. usb_put_function_instance(fi_eem);
  360. else if (can_support_ecm(gadget))
  361. usb_put_function_instance(fi_ecm);
  362. else
  363. usb_put_function_instance(fi_geth);
  364. return status;
  365. }
  366. static int eth_unbind(struct usb_composite_dev *cdev)
  367. {
  368. if (has_rndis()) {
  369. usb_put_function(f_rndis);
  370. usb_put_function_instance(fi_rndis);
  371. }
  372. if (use_eem) {
  373. usb_put_function(f_eem);
  374. usb_put_function_instance(fi_eem);
  375. } else if (can_support_ecm(cdev->gadget)) {
  376. usb_put_function(f_ecm);
  377. usb_put_function_instance(fi_ecm);
  378. } else {
  379. usb_put_function(f_geth);
  380. usb_put_function_instance(fi_geth);
  381. }
  382. kfree(otg_desc[0]);
  383. otg_desc[0] = NULL;
  384. return 0;
  385. }
  386. static struct usb_composite_driver eth_driver = {
  387. .name = "g_ether",
  388. .dev = &device_desc,
  389. .strings = dev_strings,
  390. .max_speed = USB_SPEED_SUPER,
  391. .bind = eth_bind,
  392. .unbind = eth_unbind,
  393. };
  394. module_usb_composite_driver(eth_driver);
  395. MODULE_DESCRIPTION(PREFIX DRIVER_DESC);
  396. MODULE_AUTHOR("David Brownell, Benedikt Spanger");
  397. MODULE_LICENSE("GPL");