File Index
Files and directories
Sign in
FILE

u_ether.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/gadget/function/u_ether.c C
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
   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
  10/* #define VERBOSE_DEBUG */
  11
  12#include <linux/kernel.h>
  13#include <linux/module.h>
  14#include <linux/gfp.h>
  15#include <linux/device.h>
  16#include <linux/ctype.h>
  17#include <linux/etherdevice.h>
  18#include <linux/ethtool.h>
  19#include <linux/if_vlan.h>
  20#include <linux/string_helpers.h>
  21#include <linux/usb/composite.h>
  22
  23#include "u_ether.h"
  24
  25
  26/*
  27 * This component encapsulates the Ethernet link glue needed to provide
  28 * one (!) network link through the USB gadget stack, normally "usb0".
  29 *
  30 * The control and data models are handled by the function driver which
  31 * connects to this code; such as CDC Ethernet (ECM or EEM),
  32 * "CDC Subset", or RNDIS.  That includes all descriptor and endpoint
  33 * management.
  34 *
  35 * Link level addressing is handled by this component using module
  36 * parameters; if no such parameters are provided, random link level
  37 * addresses are used.  Each end of the link uses one address.  The
  38 * host end address is exported in various ways, and is often recorded
  39 * in configuration databases.
  40 *
  41 * The driver which assembles each configuration using such a link is
  42 * responsible for ensuring that each configuration includes at most one
  43 * instance of is network link.  (The network layer provides ways for
  44 * this single "physical" link to be used by multiple virtual links.)
  45 */
  46
  47#define UETH__VERSION	"29-May-2008"
  48
  49/* Experiments show that both Linux and Windows hosts allow up to 16k
  50 * frame sizes. Set the max MTU size to 15k+52 to prevent allocating 32k
  51 * blocks and still have efficient handling. */
  52#define GETHER_MAX_MTU_SIZE 15412
  53#define GETHER_MAX_ETH_FRAME_LEN (GETHER_MAX_MTU_SIZE + ETH_HLEN)
  54
  55struct eth_dev {
  56	/* lock is held while accessing port_usb
  57	 */
  58	spinlock_t		lock;
  59	struct gether		*port_usb;
  60
  61	struct net_device	*net;
  62	struct usb_gadget	*gadget;
  63
  64	spinlock_t		req_lock;	/* guard {rx,tx}_reqs */
  65	struct list_head	tx_reqs, rx_reqs;
  66	atomic_t		tx_qlen;
  67
  68	struct sk_buff_head	rx_frames;
  69
  70	unsigned		qmult;
  71
  72	unsigned		header_len;
  73	struct sk_buff		*(*wrap)(struct gether *, struct sk_buff *skb);
  74	int			(*unwrap)(struct gether *,
  75						struct sk_buff *skb,
  76						struct sk_buff_head *list);
  77
  78	struct work_struct	work;
  79
  80	unsigned long		todo;
  81#define	WORK_RX_MEMORY		0
  82
  83	bool			zlp;
  84	bool			no_skb_reserve;
  85	bool			ifname_set;
  86	u8			host_mac[ETH_ALEN];
  87	u8			dev_mac[ETH_ALEN];
  88};
  89
  90/*-------------------------------------------------------------------------*/
  91
  92#define RX_EXTRA	20	/* bytes guarding against rx overflows */
  93
  94#define DEFAULT_QLEN	2	/* double buffering by default */
  95
  96/* use deeper queues at high/super speed */
  97static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
  98{
  99	if (gadget->speed == USB_SPEED_HIGH || gadget->speed >= USB_SPEED_SUPER)
 100		return qmult * DEFAULT_QLEN;
 101	else
 102		return DEFAULT_QLEN;
 103}
 104
 105/*-------------------------------------------------------------------------*/
 106
 107/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
 108
 109static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
 110{
 111	struct eth_dev *dev = netdev_priv(net);
 112
 113	strscpy(p->driver, "g_ether", sizeof(p->driver));
 114	strscpy(p->version, UETH__VERSION, sizeof(p->version));
 115	if (dev->gadget) {
 116		strscpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
 117		strscpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
 118	}
 119}
 120
 121/* REVISIT can also support:
 122 *   - WOL (by tracking suspends and issuing remote wakeup)
 123 *   - msglevel (implies updated messaging)
 124 *   - ... probably more ethtool ops
 125 */
 126
 127static const struct ethtool_ops ops = {
 128	.get_drvinfo = eth_get_drvinfo,
 129	.get_link = ethtool_op_get_link,
 130};
 131
 132static void defer_kevent(struct eth_dev *dev, int flag)
 133{
 134	if (test_and_set_bit(flag, &dev->todo))
 135		return;
 136	if (!schedule_work(&dev->work))
 137		ERROR(dev, "kevent %d may have been dropped\n", flag);
 138	else
 139		DBG(dev, "kevent %d scheduled\n", flag);
 140}
 141
 142static void rx_complete(struct usb_ep *ep, struct usb_request *req);
 143
 144static int
 145rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
 146{
 147	struct usb_gadget *g = dev->gadget;
 148	struct sk_buff	*skb;
 149	int		retval = -ENOMEM;
 150	size_t		size = 0;
 151	struct usb_ep	*out;
 152	unsigned long	flags;
 153
 154	spin_lock_irqsave(&dev->lock, flags);
 155	if (dev->port_usb)
 156		out = dev->port_usb->out_ep;
 157	else
 158		out = NULL;
 159
 160	if (!out)
 161	{
 162		spin_unlock_irqrestore(&dev->lock, flags);
 163		return -ENOTCONN;
 164	}
 165
 166	/* Padding up to RX_EXTRA handles minor disagreements with host.
 167	 * Normally we use the USB "terminate on short read" convention;
 168	 * so allow up to (N*maxpacket), since that memory is normally
 169	 * already allocated.  Some hardware doesn't deal well with short
 170	 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
 171	 * byte off the end (to force hardware errors on overflow).
 172	 *
 173	 * RNDIS uses internal framing, and explicitly allows senders to
 174	 * pad to end-of-packet.  That's potentially nice for speed, but
 175	 * means receivers can't recover lost synch on their own (because
 176	 * new packets don't only start after a short RX).
 177	 */
 178	size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
 179	size += dev->port_usb->header_len;
 180
 181	if (g->quirk_ep_out_aligned_size) {
 182		size += out->maxpacket - 1;
 183		size -= size % out->maxpacket;
 184	}
 185
 186	if (dev->port_usb->is_fixed)
 187		size = max_t(size_t, size, dev->port_usb->fixed_out_len);
 188	spin_unlock_irqrestore(&dev->lock, flags);
 189
 190	skb = __netdev_alloc_skb(dev->net, size + NET_IP_ALIGN, gfp_flags);
 191	if (skb == NULL) {
 192		DBG(dev, "no rx skb\n");
 193		goto enomem;
 194	}
 195
 196	/* Some platforms perform better when IP packets are aligned,
 197	 * but on at least one, checksumming fails otherwise.  Note:
 198	 * RNDIS headers involve variable numbers of LE32 values.
 199	 */
 200	if (likely(!dev->no_skb_reserve))
 201		skb_reserve(skb, NET_IP_ALIGN);
 202
 203	req->buf = skb->data;
 204	req->length = size;
 205	req->complete = rx_complete;
 206	req->context = skb;
 207
 208	retval = usb_ep_queue(out, req, gfp_flags);
 209	if (retval == -ENOMEM)
 210enomem:
 211		defer_kevent(dev, WORK_RX_MEMORY);
 212	if (retval) {
 213		DBG(dev, "rx submit --> %d\n", retval);
 214		if (skb)
 215			dev_kfree_skb_any(skb);
 216		spin_lock_irqsave(&dev->req_lock, flags);
 217		list_add(&req->list, &dev->rx_reqs);
 218		spin_unlock_irqrestore(&dev->req_lock, flags);
 219	}
 220	return retval;
 221}
 222
 223static void rx_complete(struct usb_ep *ep, struct usb_request *req)
 224{
 225	struct sk_buff	*skb = req->context, *skb2;
 226	struct eth_dev	*dev = ep->driver_data;
 227	int		status = req->status;
 228
 229	switch (status) {
 230
 231	/* normal completion */
 232	case 0:
 233		skb_put(skb, req->actual);
 234
 235		if (dev->unwrap) {
 236			unsigned long	flags;
 237
 238			spin_lock_irqsave(&dev->lock, flags);
 239			if (dev->port_usb) {
 240				status = dev->unwrap(dev->port_usb,
 241							skb,
 242							&dev->rx_frames);
 243			} else {
 244				dev_kfree_skb_any(skb);
 245				status = -ENOTCONN;
 246			}
 247			spin_unlock_irqrestore(&dev->lock, flags);
 248		} else {
 249			skb_queue_tail(&dev->rx_frames, skb);
 250		}
 251		skb = NULL;
 252
 253		skb2 = skb_dequeue(&dev->rx_frames);
 254		while (skb2) {
 255			if (status < 0
 256					|| ETH_HLEN > skb2->len
 257					|| skb2->len > GETHER_MAX_ETH_FRAME_LEN) {
 258				dev->net->stats.rx_errors++;
 259				dev->net->stats.rx_length_errors++;
 260				DBG(dev, "rx length %d\n", skb2->len);
 261				dev_kfree_skb_any(skb2);
 262				goto next_frame;
 263			}
 264			skb2->protocol = eth_type_trans(skb2, dev->net);
 265			dev->net->stats.rx_packets++;
 266			dev->net->stats.rx_bytes += skb2->len;
 267
 268			/* no buffer copies needed, unless hardware can't
 269			 * use skb buffers.
 270			 */
 271			status = netif_rx(skb2);
 272next_frame:
 273			skb2 = skb_dequeue(&dev->rx_frames);
 274		}
 275		break;
 276
 277	/* software-driven interface shutdown */
 278	case -ECONNRESET:		/* unlink */
 279	case -ESHUTDOWN:		/* disconnect etc */
 280		VDBG(dev, "rx shutdown, code %d\n", status);
 281		goto quiesce;
 282
 283	/* for hardware automagic (such as pxa) */
 284	case -ECONNABORTED:		/* endpoint reset */
 285		DBG(dev, "rx %s reset\n", ep->name);
 286		defer_kevent(dev, WORK_RX_MEMORY);
 287quiesce:
 288		dev_kfree_skb_any(skb);
 289		goto clean;
 290
 291	/* data overrun */
 292	case -EOVERFLOW:
 293		dev->net->stats.rx_over_errors++;
 294		fallthrough;
 295
 296	default:
 297		dev->net->stats.rx_errors++;
 298		DBG(dev, "rx status %d\n", status);
 299		break;
 300	}
 301
 302	if (skb)
 303		dev_kfree_skb_any(skb);
 304	if (!netif_running(dev->net)) {
 305clean:
 306		spin_lock(&dev->req_lock);
 307		list_add(&req->list, &dev->rx_reqs);
 308		spin_unlock(&dev->req_lock);
 309		req = NULL;
 310	}
 311	if (req)
 312		rx_submit(dev, req, GFP_ATOMIC);
 313}
 314
 315static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
 316{
 317	unsigned		i;
 318	struct usb_request	*req;
 319
 320	if (!n)
 321		return -ENOMEM;
 322
 323	/* queue/recycle up to N requests */
 324	i = n;
 325	list_for_each_entry(req, list, list) {
 326		if (i-- == 0)
 327			goto extra;
 328	}
 329	while (i--) {
 330		req = usb_ep_alloc_request(ep, GFP_ATOMIC);
 331		if (!req)
 332			return list_empty(list) ? -ENOMEM : 0;
 333		list_add(&req->list, list);
 334	}
 335	return 0;
 336
 337extra:
 338	/* free extras */
 339	for (;;) {
 340		struct list_head	*next;
 341
 342		next = req->list.next;
 343		list_del(&req->list);
 344		usb_ep_free_request(ep, req);
 345
 346		if (next == list)
 347			break;
 348
 349		req = container_of(next, struct usb_request, list);
 350	}
 351	return 0;
 352}
 353
 354static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
 355{
 356	int	status;
 357
 358	spin_lock(&dev->req_lock);
 359	status = prealloc(&dev->tx_reqs, link->in_ep, n);
 360	if (status < 0)
 361		goto fail;
 362	status = prealloc(&dev->rx_reqs, link->out_ep, n);
 363	if (status < 0)
 364		goto fail;
 365	goto done;
 366fail:
 367	DBG(dev, "can't alloc requests\n");
 368done:
 369	spin_unlock(&dev->req_lock);
 370	return status;
 371}
 372
 373static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
 374{
 375	struct usb_request	*req;
 376	unsigned long		flags;
 377
 378	/* fill unused rxq slots with some skb */
 379	spin_lock_irqsave(&dev->req_lock, flags);
 380	while (!list_empty(&dev->rx_reqs)) {
 381		req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
 382		list_del_init(&req->list);
 383		spin_unlock_irqrestore(&dev->req_lock, flags);
 384
 385		if (rx_submit(dev, req, gfp_flags) < 0) {
 386			defer_kevent(dev, WORK_RX_MEMORY);
 387			return;
 388		}
 389
 390		spin_lock_irqsave(&dev->req_lock, flags);
 391	}
 392	spin_unlock_irqrestore(&dev->req_lock, flags);
 393}
 394
 395static void eth_work(struct work_struct *work)
 396{
 397	struct eth_dev	*dev = container_of(work, struct eth_dev, work);
 398
 399	if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
 400		if (netif_running(dev->net))
 401			rx_fill(dev, GFP_KERNEL);
 402	}
 403
 404	if (dev->todo)
 405		DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
 406}
 407
 408static void tx_complete(struct usb_ep *ep, struct usb_request *req)
 409{
 410	struct sk_buff	*skb = req->context;
 411	struct eth_dev	*dev = ep->driver_data;
 412
 413	switch (req->status) {
 414	default:
 415		dev->net->stats.tx_errors++;
 416		VDBG(dev, "tx err %d\n", req->status);
 417		fallthrough;
 418	case -ECONNRESET:		/* unlink */
 419	case -ESHUTDOWN:		/* disconnect etc */
 420		dev_kfree_skb_any(skb);
 421		break;
 422	case 0:
 423		dev->net->stats.tx_bytes += skb->len;
 424		dev_consume_skb_any(skb);
 425	}
 426	dev->net->stats.tx_packets++;
 427
 428	spin_lock(&dev->req_lock);
 429	list_add(&req->list, &dev->tx_reqs);
 430	spin_unlock(&dev->req_lock);
 431
 432	atomic_dec(&dev->tx_qlen);
 433	if (netif_carrier_ok(dev->net))
 434		netif_wake_queue(dev->net);
 435}
 436
 437static inline int is_promisc(u16 cdc_filter)
 438{
 439	return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
 440}
 441
 442static int ether_wakeup_host(struct gether *port)
 443{
 444	int			ret;
 445	struct usb_function	*func = &port->func;
 446	struct usb_gadget	*gadget = func->config->cdev->gadget;
 447
 448	if (func->func_suspended)
 449		ret = usb_func_wakeup(func);
 450	else
 451		ret = usb_gadget_wakeup(gadget);
 452
 453	return ret;
 454}
 455
 456static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
 457					struct net_device *net)
 458{
 459	struct eth_dev		*dev = netdev_priv(net);
 460	int			length = 0;
 461	int			retval;
 462	struct usb_request	*req = NULL;
 463	unsigned long		flags;
 464	struct usb_ep		*in;
 465	u16			cdc_filter;
 466
 467	spin_lock_irqsave(&dev->lock, flags);
 468	if (dev->port_usb) {
 469		in = dev->port_usb->in_ep;
 470		cdc_filter = dev->port_usb->cdc_filter;
 471	} else {
 472		in = NULL;
 473		cdc_filter = 0;
 474	}
 475
 476	if (dev->port_usb && dev->port_usb->is_suspend) {
 477		DBG(dev, "Port suspended. Triggering wakeup\n");
 478		netif_stop_queue(net);
 479		spin_unlock_irqrestore(&dev->lock, flags);
 480		ether_wakeup_host(dev->port_usb);
 481		return NETDEV_TX_BUSY;
 482	}
 483
 484	spin_unlock_irqrestore(&dev->lock, flags);
 485
 486	if (!in) {
 487		if (skb)
 488			dev_kfree_skb_any(skb);
 489		return NETDEV_TX_OK;
 490	}
 491
 492	/* apply outgoing CDC or RNDIS filters */
 493	if (skb && !is_promisc(cdc_filter)) {
 494		u8		*dest = skb->data;
 495
 496		if (is_multicast_ether_addr(dest)) {
 497			u16	type;
 498
 499			/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
 500			 * SET_ETHERNET_MULTICAST_FILTERS requests
 501			 */
 502			if (is_broadcast_ether_addr(dest))
 503				type = USB_CDC_PACKET_TYPE_BROADCAST;
 504			else
 505				type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
 506			if (!(cdc_filter & type)) {
 507				dev_kfree_skb_any(skb);
 508				return NETDEV_TX_OK;
 509			}
 510		}
 511		/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
 512	}
 513
 514	spin_lock_irqsave(&dev->req_lock, flags);
 515	/*
 516	 * this freelist can be empty if an interrupt triggered disconnect()
 517	 * and reconfigured the gadget (shutting down this queue) after the
 518	 * network stack decided to xmit but before we got the spinlock.
 519	 */
 520	if (list_empty(&dev->tx_reqs)) {
 521		spin_unlock_irqrestore(&dev->req_lock, flags);
 522		return NETDEV_TX_BUSY;
 523	}
 524
 525	req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
 526	list_del(&req->list);
 527
 528	/* temporarily stop TX queue when the freelist empties */
 529	if (list_empty(&dev->tx_reqs))
 530		netif_stop_queue(net);
 531	spin_unlock_irqrestore(&dev->req_lock, flags);
 532
 533	/* no buffer copies needed, unless the network stack did it
 534	 * or the hardware can't use skb buffers.
 535	 * or there's not enough space for extra headers we need
 536	 */
 537	if (dev->wrap) {
 538		unsigned long	flags;
 539
 540		spin_lock_irqsave(&dev->lock, flags);
 541		if (dev->port_usb)
 542			skb = dev->wrap(dev->port_usb, skb);
 543		spin_unlock_irqrestore(&dev->lock, flags);
 544		if (!skb) {
 545			/* Multi frame CDC protocols may store the frame for
 546			 * later which is not a dropped frame.
 547			 */
 548			if (dev->port_usb &&
 549					dev->port_usb->supports_multi_frame)
 550				goto multiframe;
 551			goto drop;
 552		}
 553	}
 554
 555	length = skb->len;
 556	req->buf = skb->data;
 557	req->context = skb;
 558	req->complete = tx_complete;
 559
 560	/* NCM requires no zlp if transfer is dwNtbInMaxSize */
 561	if (dev->port_usb &&
 562	    dev->port_usb->is_fixed &&
 563	    length == dev->port_usb->fixed_in_len &&
 564	    (length % in->maxpacket) == 0)
 565		req->zero = 0;
 566	else
 567		req->zero = 1;
 568
 569	/* use zlp framing on tx for strict CDC-Ether conformance,
 570	 * though any robust network rx path ignores extra padding.
 571	 * and some hardware doesn't like to write zlps.
 572	 */
 573	if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
 574		length++;
 575
 576	req->length = length;
 577
 578	retval = usb_ep_queue(in, req, GFP_ATOMIC);
 579	switch (retval) {
 580	default:
 581		DBG(dev, "tx queue err %d\n", retval);
 582		break;
 583	case 0:
 584		netif_trans_update(net);
 585		atomic_inc(&dev->tx_qlen);
 586	}
 587
 588	if (retval) {
 589		dev_kfree_skb_any(skb);
 590drop:
 591		dev->net->stats.tx_dropped++;
 592multiframe:
 593		spin_lock_irqsave(&dev->req_lock, flags);
 594		if (list_empty(&dev->tx_reqs))
 595			netif_start_queue(net);
 596		list_add(&req->list, &dev->tx_reqs);
 597		spin_unlock_irqrestore(&dev->req_lock, flags);
 598	}
 599	return NETDEV_TX_OK;
 600}
 601
 602/*-------------------------------------------------------------------------*/
 603
 604static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
 605{
 606	DBG(dev, "%s\n", __func__);
 607
 608	/* fill the rx queue */
 609	rx_fill(dev, gfp_flags);
 610
 611	/* and open the tx floodgates */
 612	atomic_set(&dev->tx_qlen, 0);
 613	netif_wake_queue(dev->net);
 614}
 615
 616static int eth_open(struct net_device *net)
 617{
 618	struct eth_dev	*dev = netdev_priv(net);
 619	struct gether	*link;
 620
 621	DBG(dev, "%s\n", __func__);
 622	if (netif_carrier_ok(dev->net))
 623		eth_start(dev, GFP_KERNEL);
 624
 625	spin_lock_irq(&dev->lock);
 626	link = dev->port_usb;
 627	if (link && link->open)
 628		link->open(link);
 629	spin_unlock_irq(&dev->lock);
 630
 631	return 0;
 632}
 633
 634static int eth_stop(struct net_device *net)
 635{
 636	struct eth_dev	*dev = netdev_priv(net);
 637	unsigned long	flags;
 638
 639	VDBG(dev, "%s\n", __func__);
 640	netif_stop_queue(net);
 641
 642	DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
 643		dev->net->stats.rx_packets, dev->net->stats.tx_packets,
 644		dev->net->stats.rx_errors, dev->net->stats.tx_errors
 645		);
 646
 647	/* ensure there are no more active requests */
 648	spin_lock_irqsave(&dev->lock, flags);
 649	if (dev->port_usb) {
 650		struct gether	*link = dev->port_usb;
 651		const struct usb_endpoint_descriptor *in;
 652		const struct usb_endpoint_descriptor *out;
 653
 654		if (link->close)
 655			link->close(link);
 656
 657		/* NOTE:  we have no abort-queue primitive we could use
 658		 * to cancel all pending I/O.  Instead, we disable then
 659		 * reenable the endpoints ... this idiom may leave toggle
 660		 * wrong, but that's a self-correcting error.
 661		 *
 662		 * REVISIT:  we *COULD* just let the transfers complete at
 663		 * their own pace; the network stack can handle old packets.
 664		 * For the moment we leave this here, since it works.
 665		 */
 666		in = link->in_ep->desc;
 667		out = link->out_ep->desc;
 668		usb_ep_disable(link->in_ep);
 669		usb_ep_disable(link->out_ep);
 670		if (netif_carrier_ok(net)) {
 671			DBG(dev, "host still using in/out endpoints\n");
 672			link->in_ep->desc = in;
 673			link->out_ep->desc = out;
 674			usb_ep_enable(link->in_ep);
 675			usb_ep_enable(link->out_ep);
 676		}
 677	}
 678	spin_unlock_irqrestore(&dev->lock, flags);
 679
 680	return 0;
 681}
 682
 683/*-------------------------------------------------------------------------*/
 684
 685static int get_ether_addr(const char *str, u8 *dev_addr)
 686{
 687	if (str) {
 688		unsigned	i;
 689
 690		for (i = 0; i < 6; i++) {
 691			unsigned char num;
 692
 693			if ((*str == '.') || (*str == ':'))
 694				str++;
 695			num = hex_to_bin(*str++) << 4;
 696			num |= hex_to_bin(*str++);
 697			dev_addr [i] = num;
 698		}
 699		if (is_valid_ether_addr(dev_addr))
 700			return 0;
 701	}
 702	eth_random_addr(dev_addr);
 703	return 1;
 704}
 705
 706static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
 707{
 708	if (len < 18)
 709		return -EINVAL;
 710
 711	snprintf(str, len, "%pM", dev_addr);
 712	return 18;
 713}
 714
 715static const struct net_device_ops eth_netdev_ops = {
 716	.ndo_open		= eth_open,
 717	.ndo_stop		= eth_stop,
 718	.ndo_start_xmit		= eth_start_xmit,
 719	.ndo_set_mac_address 	= eth_mac_addr,
 720	.ndo_validate_addr	= eth_validate_addr,
 721};
 722
 723static const struct device_type gadget_type = {
 724	.name	= "gadget",
 725};
 726
 727/*
 728 * gether_setup_name - initialize one ethernet-over-usb link
 729 * @g: gadget to associated with these links
 730 * @ethaddr: NULL, or a buffer in which the ethernet address of the
 731 *	host side of the link is recorded
 732 * @netname: name for network device (for example, "usb")
 733 * Context: may sleep
 734 *
 735 * This sets up the single network link that may be exported by a
 736 * gadget driver using this framework.  The link layer addresses are
 737 * set up using module parameters.
 738 *
 739 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
 740 */
 741struct eth_dev *gether_setup_name(struct usb_gadget *g,
 742		const char *dev_addr, const char *host_addr,
 743		u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
 744{
 745	struct eth_dev		*dev;
 746	struct net_device	*net;
 747	int			status;
 748	u8			addr[ETH_ALEN];
 749
 750	net = alloc_etherdev(sizeof *dev);
 751	if (!net)
 752		return ERR_PTR(-ENOMEM);
 753
 754	dev = netdev_priv(net);
 755	spin_lock_init(&dev->lock);
 756	spin_lock_init(&dev->req_lock);
 757	INIT_WORK(&dev->work, eth_work);
 758	INIT_LIST_HEAD(&dev->tx_reqs);
 759	INIT_LIST_HEAD(&dev->rx_reqs);
 760
 761	skb_queue_head_init(&dev->rx_frames);
 762
 763	/* network device setup */
 764	dev->net = net;
 765	dev->qmult = qmult;
 766	snprintf(net->name, sizeof(net->name), "%s%%d", netname);
 767
 768	if (get_ether_addr(dev_addr, addr)) {
 769		net->addr_assign_type = NET_ADDR_RANDOM;
 770		dev_warn(&g->dev,
 771			"using random %s ethernet address\n", "self");
 772	} else {
 773		net->addr_assign_type = NET_ADDR_SET;
 774	}
 775	eth_hw_addr_set(net, addr);
 776	if (get_ether_addr(host_addr, dev->host_mac))
 777		dev_warn(&g->dev,
 778			"using random %s ethernet address\n", "host");
 779
 780	if (ethaddr)
 781		memcpy(ethaddr, dev->host_mac, ETH_ALEN);
 782
 783	net->netdev_ops = &eth_netdev_ops;
 784
 785	net->ethtool_ops = &ops;
 786
 787	/* MTU range: 14 - 15412 */
 788	net->min_mtu = ETH_HLEN;
 789	net->max_mtu = GETHER_MAX_MTU_SIZE;
 790
 791	dev->gadget = g;
 792	SET_NETDEV_DEV(net, &g->dev);
 793	SET_NETDEV_DEVTYPE(net, &gadget_type);
 794
 795	status = register_netdev(net);
 796	if (status < 0) {
 797		dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
 798		free_netdev(net);
 799		dev = ERR_PTR(status);
 800	} else {
 801		INFO(dev, "MAC %pM\n", net->dev_addr);
 802		INFO(dev, "HOST MAC %pM\n", dev->host_mac);
 803
 804		/*
 805		 * two kinds of host-initiated state changes:
 806		 *  - iff DATA transfer is active, carrier is "on"
 807		 *  - tx queueing enabled if open *and* carrier is "on"
 808		 */
 809		netif_carrier_off(net);
 810	}
 811
 812	return dev;
 813}
 814EXPORT_SYMBOL_GPL(gether_setup_name);
 815
 816struct net_device *gether_setup_name_default(const char *netname)
 817{
 818	struct net_device	*net;
 819	struct eth_dev		*dev;
 820
 821	net = alloc_etherdev(sizeof(*dev));
 822	if (!net)
 823		return ERR_PTR(-ENOMEM);
 824
 825	dev = netdev_priv(net);
 826	spin_lock_init(&dev->lock);
 827	spin_lock_init(&dev->req_lock);
 828	INIT_WORK(&dev->work, eth_work);
 829	INIT_LIST_HEAD(&dev->tx_reqs);
 830	INIT_LIST_HEAD(&dev->rx_reqs);
 831
 832	skb_queue_head_init(&dev->rx_frames);
 833
 834	/* network device setup */
 835	dev->net = net;
 836	dev->qmult = QMULT_DEFAULT;
 837	snprintf(net->name, sizeof(net->name), "%s%%d", netname);
 838
 839	eth_random_addr(dev->dev_mac);
 840
 841	/* by default we always have a random MAC address */
 842	net->addr_assign_type = NET_ADDR_RANDOM;
 843
 844	eth_random_addr(dev->host_mac);
 845
 846	net->netdev_ops = &eth_netdev_ops;
 847
 848	net->ethtool_ops = &ops;
 849	SET_NETDEV_DEVTYPE(net, &gadget_type);
 850
 851	/* MTU range: 14 - 15412 */
 852	net->min_mtu = ETH_HLEN;
 853	net->max_mtu = GETHER_MAX_MTU_SIZE;
 854
 855	return net;
 856}
 857EXPORT_SYMBOL_GPL(gether_setup_name_default);
 858
 859int gether_register_netdev(struct net_device *net)
 860{
 861	struct eth_dev *dev;
 862	struct usb_gadget *g;
 863	int status;
 864
 865	if (!net->dev.parent)
 866		return -EINVAL;
 867	dev = netdev_priv(net);
 868	g = dev->gadget;
 869
 870	eth_hw_addr_set(net, dev->dev_mac);
 871
 872	status = register_netdev(net);
 873	if (status < 0) {
 874		dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
 875		return status;
 876	} else {
 877		INFO(dev, "HOST MAC %pM\n", dev->host_mac);
 878		INFO(dev, "MAC %pM\n", dev->dev_mac);
 879
 880		/* two kinds of host-initiated state changes:
 881		 *  - iff DATA transfer is active, carrier is "on"
 882		 *  - tx queueing enabled if open *and* carrier is "on"
 883		 */
 884		netif_carrier_off(net);
 885	}
 886
 887	return status;
 888}
 889EXPORT_SYMBOL_GPL(gether_register_netdev);
 890
 891void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
 892{
 893	struct eth_dev *dev;
 894
 895	dev = netdev_priv(net);
 896	dev->gadget = g;
 897	SET_NETDEV_DEV(net, &g->dev);
 898}
 899EXPORT_SYMBOL_GPL(gether_set_gadget);
 900
 901int gether_attach_gadget(struct net_device *net, struct usb_gadget *g)
 902{
 903	int ret;
 904
 905	ret = device_move(&net->dev, &g->dev, DPM_ORDER_DEV_AFTER_PARENT);
 906	if (ret)
 907		return ret;
 908
 909	gether_set_gadget(net, g);
 910	return 0;
 911}
 912EXPORT_SYMBOL_GPL(gether_attach_gadget);
 913
 914void gether_detach_gadget(struct net_device *net)
 915{
 916	struct eth_dev *dev = netdev_priv(net);
 917
 918	device_move(&net->dev, NULL, DPM_ORDER_NONE);
 919	dev->gadget = NULL;
 920}
 921EXPORT_SYMBOL_GPL(gether_detach_gadget);
 922
 923int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
 924{
 925	struct eth_dev *dev;
 926	u8 new_addr[ETH_ALEN];
 927
 928	dev = netdev_priv(net);
 929	if (get_ether_addr(dev_addr, new_addr))
 930		return -EINVAL;
 931	memcpy(dev->dev_mac, new_addr, ETH_ALEN);
 932	net->addr_assign_type = NET_ADDR_SET;
 933	return 0;
 934}
 935EXPORT_SYMBOL_GPL(gether_set_dev_addr);
 936
 937int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
 938{
 939	struct eth_dev *dev;
 940	int ret;
 941
 942	dev = netdev_priv(net);
 943	ret = get_ether_addr_str(dev->dev_mac, dev_addr, len);
 944	if (ret + 1 < len) {
 945		dev_addr[ret++] = '\n';
 946		dev_addr[ret] = '\0';
 947	}
 948
 949	return ret;
 950}
 951EXPORT_SYMBOL_GPL(gether_get_dev_addr);
 952
 953int gether_set_host_addr(struct net_device *net, const char *host_addr)
 954{
 955	struct eth_dev *dev;
 956	u8 new_addr[ETH_ALEN];
 957
 958	dev = netdev_priv(net);
 959	if (get_ether_addr(host_addr, new_addr))
 960		return -EINVAL;
 961	memcpy(dev->host_mac, new_addr, ETH_ALEN);
 962	return 0;
 963}
 964EXPORT_SYMBOL_GPL(gether_set_host_addr);
 965
 966int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
 967{
 968	struct eth_dev *dev;
 969	int ret;
 970
 971	dev = netdev_priv(net);
 972	ret = get_ether_addr_str(dev->host_mac, host_addr, len);
 973	if (ret + 1 < len) {
 974		host_addr[ret++] = '\n';
 975		host_addr[ret] = '\0';
 976	}
 977
 978	return ret;
 979}
 980EXPORT_SYMBOL_GPL(gether_get_host_addr);
 981
 982int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
 983{
 984	struct eth_dev *dev;
 985
 986	if (len < 13)
 987		return -EINVAL;
 988
 989	dev = netdev_priv(net);
 990	snprintf(host_addr, len, "%pm", dev->host_mac);
 991
 992	string_upper(host_addr, host_addr);
 993
 994	return strlen(host_addr);
 995}
 996EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
 997
 998void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
 999{
1000	struct eth_dev *dev;
1001
1002	dev = netdev_priv(net);
1003	memcpy(host_mac, dev->host_mac, ETH_ALEN);
1004}
1005EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
1006
1007void gether_set_qmult(struct net_device *net, unsigned qmult)
1008{
1009	struct eth_dev *dev;
1010
1011	dev = netdev_priv(net);
1012	dev->qmult = qmult;
1013}
1014EXPORT_SYMBOL_GPL(gether_set_qmult);
1015
1016unsigned gether_get_qmult(struct net_device *net)
1017{
1018	struct eth_dev *dev;
1019
1020	dev = netdev_priv(net);
1021	return dev->qmult;
1022}
1023EXPORT_SYMBOL_GPL(gether_get_qmult);
1024
1025int gether_get_ifname(struct net_device *net, char *name, int len)
1026{
1027	struct eth_dev *dev = netdev_priv(net);
1028	int ret;
1029
1030	rtnl_lock();
1031	ret = scnprintf(name, len, "%s\n",
1032			dev->ifname_set ? net->name : netdev_name(net));
1033	rtnl_unlock();
1034	return ret;
1035}
1036EXPORT_SYMBOL_GPL(gether_get_ifname);
1037
1038int gether_set_ifname(struct net_device *net, const char *name, int len)
1039{
1040	struct eth_dev *dev = netdev_priv(net);
1041	char tmp[IFNAMSIZ];
1042	const char *p;
1043
1044	if (name[len - 1] == '\n')
1045		len--;
1046
1047	if (len >= sizeof(tmp))
1048		return -E2BIG;
1049
1050	strscpy(tmp, name, len + 1);
1051	if (!dev_valid_name(tmp))
1052		return -EINVAL;
1053
1054	/* Require exactly one %d, so binding will not fail with EEXIST. */
1055	p = strchr(name, '%');
1056	if (!p || p[1] != 'd' || strchr(p + 2, '%'))
1057		return -EINVAL;
1058
1059	strscpy(net->name, tmp);
1060	dev->ifname_set = true;
1061
1062	return 0;
1063}
1064EXPORT_SYMBOL_GPL(gether_set_ifname);
1065
1066void gether_suspend(struct gether *link)
1067{
1068	struct eth_dev *dev = link->ioport;
1069	unsigned long flags;
1070
1071	if (!dev)
1072		return;
1073
1074	if (atomic_read(&dev->tx_qlen)) {
1075		/*
1076		 * There is a transfer in progress. So we trigger a remote
1077		 * wakeup to inform the host.
1078		 */
1079		if (!ether_wakeup_host(dev->port_usb))
1080			return;
1081	}
1082	spin_lock_irqsave(&dev->lock, flags);
1083	link->is_suspend = true;
1084	spin_unlock_irqrestore(&dev->lock, flags);
1085}
1086EXPORT_SYMBOL_GPL(gether_suspend);
1087
1088void gether_resume(struct gether *link)
1089{
1090	struct eth_dev *dev = link->ioport;
1091	unsigned long flags;
1092
1093	if (!dev)
1094		return;
1095
1096	if (netif_queue_stopped(dev->net))
1097		netif_start_queue(dev->net);
1098
1099	spin_lock_irqsave(&dev->lock, flags);
1100	link->is_suspend = false;
1101	spin_unlock_irqrestore(&dev->lock, flags);
1102}
1103EXPORT_SYMBOL_GPL(gether_resume);
1104
1105/*
1106 * gether_cleanup - remove Ethernet-over-USB device
1107 * Context: may sleep
1108 *
1109 * This is called to free all resources allocated by @gether_setup().
1110 */
1111void gether_cleanup(struct eth_dev *dev)
1112{
1113	if (!dev)
1114		return;
1115
1116	unregister_netdev(dev->net);
1117	flush_work(&dev->work);
1118	free_netdev(dev->net);
1119}
1120EXPORT_SYMBOL_GPL(gether_cleanup);
1121
1122/**
1123 * gether_connect - notify network layer that USB link is active
1124 * @link: the USB link, set up with endpoints, descriptors matching
1125 *	current device speed, and any framing wrapper(s) set up.
1126 * Context: irqs blocked
1127 *
1128 * This is called to activate endpoints and let the network layer know
1129 * the connection is active ("carrier detect").  It may cause the I/O
1130 * queues to open and start letting network packets flow, but will in
1131 * any case activate the endpoints so that they respond properly to the
1132 * USB host.
1133 *
1134 * Verify net_device pointer returned using IS_ERR().  If it doesn't
1135 * indicate some error code (negative errno), ep->driver_data values
1136 * have been overwritten.
1137 */
1138struct net_device *gether_connect(struct gether *link)
1139{
1140	struct eth_dev		*dev = link->ioport;
1141	int			result = 0;
1142
1143	if (!dev)
1144		return ERR_PTR(-EINVAL);
1145
1146	link->in_ep->driver_data = dev;
1147	result = usb_ep_enable(link->in_ep);
1148	if (result != 0) {
1149		DBG(dev, "enable %s --> %d\n",
1150			link->in_ep->name, result);
1151		goto fail0;
1152	}
1153
1154	link->out_ep->driver_data = dev;
1155	result = usb_ep_enable(link->out_ep);
1156	if (result != 0) {
1157		DBG(dev, "enable %s --> %d\n",
1158			link->out_ep->name, result);
1159		goto fail1;
1160	}
1161
1162	if (result == 0)
1163		result = alloc_requests(dev, link, qlen(dev->gadget,
1164					dev->qmult));
1165
1166	if (result == 0) {
1167		dev->zlp = link->is_zlp_ok;
1168		dev->no_skb_reserve = gadget_avoids_skb_reserve(dev->gadget);
1169		DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
1170
1171		dev->header_len = link->header_len;
1172		dev->unwrap = link->unwrap;
1173		dev->wrap = link->wrap;
1174
1175		spin_lock(&dev->lock);
1176		dev->port_usb = link;
1177		if (netif_running(dev->net)) {
1178			if (link->open)
1179				link->open(link);
1180		} else {
1181			if (link->close)
1182				link->close(link);
1183		}
1184		spin_unlock(&dev->lock);
1185
1186		netif_carrier_on(dev->net);
1187		if (netif_running(dev->net))
1188			eth_start(dev, GFP_ATOMIC);
1189
1190	/* on error, disable any endpoints  */
1191	} else {
1192		(void) usb_ep_disable(link->out_ep);
1193fail1:
1194		(void) usb_ep_disable(link->in_ep);
1195	}
1196fail0:
1197	/* caller is responsible for cleanup on error */
1198	if (result < 0)
1199		return ERR_PTR(result);
1200	return dev->net;
1201}
1202EXPORT_SYMBOL_GPL(gether_connect);
1203
1204/**
1205 * gether_disconnect - notify network layer that USB link is inactive
1206 * @link: the USB link, on which gether_connect() was called
1207 * Context: irqs blocked
1208 *
1209 * This is called to deactivate endpoints and let the network layer know
1210 * the connection went inactive ("no carrier").
1211 *
1212 * On return, the state is as if gether_connect() had never been called.
1213 * The endpoints are inactive, and accordingly without active USB I/O.
1214 * Pointers to endpoint descriptors and endpoint private data are nulled.
1215 */
1216void gether_disconnect(struct gether *link)
1217{
1218	struct eth_dev		*dev = link->ioport;
1219	struct usb_request	*req;
1220
1221	WARN_ON(!dev);
1222	if (!dev)
1223		return;
1224
1225	DBG(dev, "%s\n", __func__);
1226
1227	spin_lock(&dev->lock);
1228	dev->port_usb = NULL;
1229	link->is_suspend = false;
1230	spin_unlock(&dev->lock);
1231
1232	netif_stop_queue(dev->net);
1233	netif_carrier_off(dev->net);
1234
1235	/* disable endpoints, forcing (synchronous) completion
1236	 * of all pending i/o.  then free the request objects
1237	 * and forget about the endpoints.
1238	 */
1239	usb_ep_disable(link->in_ep);
1240	spin_lock(&dev->req_lock);
1241	while (!list_empty(&dev->tx_reqs)) {
1242		req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
1243		list_del(&req->list);
1244
1245		spin_unlock(&dev->req_lock);
1246		usb_ep_free_request(link->in_ep, req);
1247		spin_lock(&dev->req_lock);
1248	}
1249	spin_unlock(&dev->req_lock);
1250	link->in_ep->desc = NULL;
1251
1252	usb_ep_disable(link->out_ep);
1253	spin_lock(&dev->req_lock);
1254	while (!list_empty(&dev->rx_reqs)) {
1255		req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
1256		list_del(&req->list);
1257
1258		spin_unlock(&dev->req_lock);
1259		usb_ep_free_request(link->out_ep, req);
1260		spin_lock(&dev->req_lock);
1261	}
1262	spin_unlock(&dev->req_lock);
1263	link->out_ep->desc = NULL;
1264
1265	/* finish forgetting about this USB link episode */
1266	dev->header_len = 0;
1267	dev->unwrap = NULL;
1268	dev->wrap = NULL;
1269}
1270EXPORT_SYMBOL_GPL(gether_disconnect);
1271
1272MODULE_DESCRIPTION("Ethernet-over-USB link layer utilities for Gadget stack");
1273MODULE_LICENSE("GPL");
1274MODULE_AUTHOR("David Brownell");
~/ Read-only file preview.