u_qc_ether.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * u_qc_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
  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. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* #define VERBOSE_DEBUG */
  22. #include <linux/kernel.h>
  23. #include <linux/gfp.h>
  24. #include <linux/device.h>
  25. #include <linux/ctype.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ethtool.h>
  28. #include "u_ether.h"
  29. /*
  30. * This component encapsulates the Ethernet link glue needed to provide
  31. * one (!) network link through the USB gadget stack, normally "usb0".
  32. *
  33. * The control and data models are handled by the function driver which
  34. * connects to this code; such as CDC Ethernet (ECM or EEM),
  35. * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
  36. * management.
  37. *
  38. * Link level addressing is handled by this component using module
  39. * parameters; if no such parameters are provided, random link level
  40. * addresses are used. Each end of the link uses one address. The
  41. * host end address is exported in various ways, and is often recorded
  42. * in configuration databases.
  43. *
  44. * The driver which assembles each configuration using such a link is
  45. * responsible for ensuring that each configuration includes at most one
  46. * instance of is network link. (The network layer provides ways for
  47. * this single "physical" link to be used by multiple virtual links.)
  48. *
  49. * This utilities is based on Ethernet-over-USB link layer utilities and
  50. * contains MSM specific implementation.
  51. */
  52. #define UETH__VERSION "29-May-2008"
  53. struct eth_qc_dev {
  54. /* lock is held while accessing port_usb
  55. * or updating its backlink port_usb->ioport
  56. */
  57. spinlock_t lock;
  58. struct qc_gether *port_usb;
  59. struct net_device *net;
  60. struct usb_gadget *gadget;
  61. unsigned header_len;
  62. bool zlp;
  63. u8 host_mac[ETH_ALEN];
  64. };
  65. /*-------------------------------------------------------------------------*/
  66. #undef DBG
  67. #undef VDBG
  68. #undef ERROR
  69. #undef INFO
  70. #define xprintk(d, level, fmt, args...) \
  71. printk(level "%s: " fmt , (d)->net->name , ## args)
  72. #ifdef DEBUG
  73. #undef DEBUG
  74. #define DBG(dev, fmt, args...) \
  75. xprintk(dev , KERN_DEBUG , fmt , ## args)
  76. #else
  77. #define DBG(dev, fmt, args...) \
  78. do { } while (0)
  79. #endif /* DEBUG */
  80. #ifdef VERBOSE_DEBUG
  81. #define VDBG DBG
  82. #else
  83. #define VDBG(dev, fmt, args...) \
  84. do { } while (0)
  85. #endif /* DEBUG */
  86. #define ERROR(dev, fmt, args...) \
  87. xprintk(dev , KERN_ERR , fmt , ## args)
  88. #define INFO(dev, fmt, args...) \
  89. xprintk(dev , KERN_INFO , fmt , ## args)
  90. /*-------------------------------------------------------------------------*/
  91. /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
  92. static int ueth_qc_change_mtu(struct net_device *net, int new_mtu)
  93. {
  94. struct eth_qc_dev *dev = netdev_priv(net);
  95. unsigned long flags;
  96. int status = 0;
  97. /* don't change MTU on "live" link (peer won't know) */
  98. spin_lock_irqsave(&dev->lock, flags);
  99. if (dev->port_usb)
  100. status = -EBUSY;
  101. else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
  102. status = -ERANGE;
  103. else
  104. net->mtu = new_mtu;
  105. spin_unlock_irqrestore(&dev->lock, flags);
  106. return status;
  107. }
  108. static void eth_qc_get_drvinfo(struct net_device *net,
  109. struct ethtool_drvinfo *p)
  110. {
  111. struct eth_qc_dev *dev = netdev_priv(net);
  112. strlcpy(p->driver, "g_qc_ether", sizeof p->driver);
  113. strlcpy(p->version, UETH__VERSION, sizeof p->version);
  114. strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
  115. strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof p->bus_info);
  116. }
  117. static const struct ethtool_ops qc_ethtool_ops = {
  118. .get_drvinfo = eth_qc_get_drvinfo,
  119. .get_link = ethtool_op_get_link,
  120. };
  121. static netdev_tx_t eth_qc_start_xmit(struct sk_buff *skb,
  122. struct net_device *net)
  123. {
  124. return NETDEV_TX_OK;
  125. }
  126. static int eth_qc_open(struct net_device *net)
  127. {
  128. struct eth_qc_dev *dev = netdev_priv(net);
  129. struct qc_gether *link;
  130. DBG(dev, "%s\n", __func__);
  131. if (netif_carrier_ok(dev->net)) {
  132. /* Force the netif to send the RTM_NEWLINK event
  133. * that in use to notify on the USB cable status.
  134. */
  135. netif_carrier_off(dev->net);
  136. netif_carrier_on(dev->net);
  137. netif_wake_queue(dev->net);
  138. }
  139. spin_lock_irq(&dev->lock);
  140. link = dev->port_usb;
  141. if (link && link->open)
  142. link->open(link);
  143. spin_unlock_irq(&dev->lock);
  144. return 0;
  145. }
  146. static int eth_qc_stop(struct net_device *net)
  147. {
  148. struct eth_qc_dev *dev = netdev_priv(net);
  149. unsigned long flags;
  150. struct qc_gether *link = dev->port_usb;
  151. VDBG(dev, "%s\n", __func__);
  152. netif_stop_queue(net);
  153. spin_lock_irqsave(&dev->lock, flags);
  154. if (dev->port_usb && link->close)
  155. link->close(link);
  156. spin_unlock_irqrestore(&dev->lock, flags);
  157. return 0;
  158. }
  159. /*-------------------------------------------------------------------------*/
  160. /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
  161. static char *qc_dev_addr;
  162. module_param(qc_dev_addr, charp, S_IRUGO);
  163. MODULE_PARM_DESC(qc_dev_addr, "QC Device Ethernet Address");
  164. /* this address is invisible to ifconfig */
  165. static char *qc_host_addr;
  166. module_param(qc_host_addr, charp, S_IRUGO);
  167. MODULE_PARM_DESC(qc_host_addr, "QC Host Ethernet Address");
  168. static int get_qc_ether_addr(const char *str, u8 *dev_addr)
  169. {
  170. if (str) {
  171. unsigned i;
  172. for (i = 0; i < 6; i++) {
  173. unsigned char num;
  174. if ((*str == '.') || (*str == ':'))
  175. str++;
  176. num = hex_to_bin(*str++) << 4;
  177. num |= hex_to_bin(*str++);
  178. dev_addr[i] = num;
  179. }
  180. if (is_valid_ether_addr(dev_addr))
  181. return 0;
  182. }
  183. random_ether_addr(dev_addr);
  184. return 1;
  185. }
  186. static const struct net_device_ops eth_qc_netdev_ops = {
  187. .ndo_open = eth_qc_open,
  188. .ndo_stop = eth_qc_stop,
  189. .ndo_start_xmit = eth_qc_start_xmit,
  190. .ndo_change_mtu = ueth_qc_change_mtu,
  191. .ndo_set_mac_address = eth_mac_addr,
  192. .ndo_validate_addr = eth_validate_addr,
  193. };
  194. static struct device_type qc_gadget_type = {
  195. .name = "gadget",
  196. };
  197. void gether_qc_get_macs(u8 dev_mac[ETH_ALEN], u8 host_mac[ETH_ALEN])
  198. {
  199. if (get_qc_ether_addr(qc_dev_addr, dev_mac))
  200. pr_debug("using random dev_mac ethernet address\n");
  201. if (get_qc_ether_addr(qc_host_addr, host_mac))
  202. pr_debug("using random host_mac ethernet address\n");
  203. }
  204. /**
  205. * gether_qc_setup - initialize one ethernet-over-usb link
  206. * @g: gadget to associated with these links
  207. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  208. * host side of the link is recorded
  209. * Context: may sleep
  210. *
  211. * This sets up the single network link that may be exported by a
  212. * gadget driver using this framework. The link layer addresses are
  213. * set up using module parameters.
  214. *
  215. * Returns negative errno, or zero on success
  216. */
  217. int gether_qc_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
  218. {
  219. return gether_qc_setup_name(g, ethaddr, "usb");
  220. }
  221. /**
  222. * gether_qc_setup_name - initialize one ethernet-over-usb link
  223. * @g: gadget to associated with these links
  224. * @ethaddr: NULL, or a buffer in which the ethernet address of the
  225. * host side of the link is recorded
  226. * @netname: name for network device (for example, "usb")
  227. * Context: may sleep
  228. *
  229. * This sets up the single network link that may be exported by a
  230. * gadget driver using this framework. The link layer addresses are
  231. * set up using module parameters.
  232. *
  233. * Returns negative errno, or zero on success
  234. */
  235. int gether_qc_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
  236. const char *netname)
  237. {
  238. struct eth_qc_dev *dev;
  239. struct net_device *net;
  240. int status;
  241. net = alloc_etherdev(sizeof *dev);
  242. if (!net)
  243. return -ENOMEM;
  244. dev = netdev_priv(net);
  245. spin_lock_init(&dev->lock);
  246. /* network device setup */
  247. dev->net = net;
  248. snprintf(net->name, sizeof(net->name), "%s%%d", netname);
  249. if (get_qc_ether_addr(qc_dev_addr, net->dev_addr))
  250. dev_warn(&g->dev,
  251. "using random %s ethernet address\n", "self");
  252. if (get_qc_ether_addr(qc_host_addr, dev->host_mac))
  253. dev_warn(&g->dev,
  254. "using random %s ethernet address\n", "host");
  255. if (ethaddr)
  256. memcpy(ethaddr, dev->host_mac, ETH_ALEN);
  257. net->netdev_ops = &eth_qc_netdev_ops;
  258. SET_ETHTOOL_OPS(net, &qc_ethtool_ops);
  259. netif_carrier_off(net);
  260. dev->gadget = g;
  261. SET_NETDEV_DEV(net, &g->dev);
  262. SET_NETDEV_DEVTYPE(net, &qc_gadget_type);
  263. status = register_netdev(net);
  264. if (status < 0) {
  265. dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
  266. free_netdev(net);
  267. } else {
  268. INFO(dev, "MAC %pM\n", net->dev_addr);
  269. INFO(dev, "HOST MAC %pM\n", dev->host_mac);
  270. }
  271. return status;
  272. }
  273. /**
  274. * gether_qc_cleanup_name - remove Ethernet-over-USB device
  275. * @netname: name for network device (for example, "usb")
  276. * Context: may sleep
  277. *
  278. * This is called to free all resources allocated by @gether_qc_setup().
  279. */
  280. void gether_qc_cleanup_name(const char *netname)
  281. {
  282. struct net_device *net_dev;
  283. /* Extract the eth_qc_dev from the net device */
  284. net_dev = dev_get_by_name(&init_net, netname);
  285. if (net_dev) {
  286. dev_put(net_dev);
  287. unregister_netdev(net_dev);
  288. free_netdev(net_dev);
  289. }
  290. }
  291. /**
  292. * gether_qc_connect_name - notify network layer that USB link
  293. * is active
  294. * @link: the USB link, set up with endpoints, descriptors matching
  295. * current device speed, and any framing wrapper(s) set up.
  296. * @netname: name for network device (for example, "usb")
  297. * Context: irqs blocked
  298. * @netif_enable: if true, net interface will be turned on
  299. *
  300. * This is called to let the network layer know the connection
  301. * is active ("carrier detect").
  302. */
  303. struct net_device *gether_qc_connect_name(struct qc_gether *link,
  304. const char *netname, bool netif_enable)
  305. {
  306. struct net_device *net_dev;
  307. struct eth_qc_dev *dev;
  308. /* Extract the eth_qc_dev from the net device */
  309. net_dev = dev_get_by_name(&init_net, netname);
  310. if (!net_dev)
  311. return ERR_PTR(-EINVAL);
  312. dev_put(net_dev);
  313. dev = netdev_priv(net_dev);
  314. if (!dev)
  315. return ERR_PTR(-EINVAL);
  316. dev->zlp = link->is_zlp_ok;
  317. dev->header_len = link->header_len;
  318. spin_lock(&dev->lock);
  319. dev->port_usb = link;
  320. link->ioport = dev;
  321. if (netif_running(dev->net)) {
  322. if (link->open)
  323. link->open(link);
  324. } else {
  325. if (link->close)
  326. link->close(link);
  327. }
  328. spin_unlock(&dev->lock);
  329. if (netif_enable) {
  330. netif_carrier_on(dev->net);
  331. if (netif_running(dev->net))
  332. netif_wake_queue(dev->net);
  333. }
  334. return dev->net;
  335. }
  336. /**
  337. * gether_qc_disconnect_name - notify network layer that USB
  338. * link is inactive
  339. * @link: the USB link, on which gether_connect() was called
  340. * @netname: name for network device (for example, "usb")
  341. * Context: irqs blocked
  342. *
  343. * This is called to let the network layer know the connection
  344. * went inactive ("no carrier").
  345. *
  346. * On return, the state is as if gether_connect() had never been called.
  347. */
  348. void gether_qc_disconnect_name(struct qc_gether *link, const char *netname)
  349. {
  350. struct net_device *net_dev;
  351. struct eth_qc_dev *dev;
  352. /* Extract the eth_qc_dev from the net device */
  353. net_dev = dev_get_by_name(&init_net, netname);
  354. if (!net_dev)
  355. return;
  356. dev_put(net_dev);
  357. dev = netdev_priv(net_dev);
  358. if (!dev)
  359. return;
  360. DBG(dev, "%s\n", __func__);
  361. netif_stop_queue(dev->net);
  362. netif_carrier_off(dev->net);
  363. spin_lock(&dev->lock);
  364. dev->port_usb = NULL;
  365. link->ioport = NULL;
  366. spin_unlock(&dev->lock);
  367. }