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