2294 lines · 60.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * USB Network driver infrastructure * Copyright (C) 2000-2005 by David Brownell * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> */ /* * This is a generic "USB networking" framework that works with several * kinds of full and high speed networking devices:  host-to-host cables, * smart usb peripherals, and actual Ethernet adapters. * * These devices usually differ in terms of control protocols (if they * even have one!) and sometimes they define new framing to wrap or batch * Ethernet packets.  Otherwise, they talk to USB pretty much the same, * so interface (un)binding, endpoint I/O queues, fault handling, and other * issues can usefully be addressed by this framework. */ #include <linux/module.h>#include <linux/hex.h>#include <linux/init.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/ctype.h>#include <linux/ethtool.h>#include <linux/workqueue.h>#include <linux/mii.h>#include <linux/usb.h>#include <linux/usb/usbnet.h>#include <linux/slab.h>#include <linux/kernel.h>#include <linux/pm_runtime.h> /*-------------------------------------------------------------------------*/ /* * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. * Several dozen bytes of IPv4 data can fit in two such transactions. * One maximum size Ethernet packet takes twenty four of them. * For high speed, each frame comfortably fits almost 36 max size * Ethernet packets (so queues should be bigger). * * The goal is to let the USB host controller be busy for 5msec or * more before an irq is required, under load.  Jumbograms change * the equation. */#define	MAX_QUEUE_MEMORY	(60 * 1518)#define	RX_QLEN(dev)		((dev)->rx_qlen)#define	TX_QLEN(dev)		((dev)->tx_qlen) // reawaken network queue this soon after stopping; else watchdog barks#define TX_TIMEOUT_JIFFIES	(5*HZ) /* throttle rx/tx briefly after some faults, so hub_wq might disconnect() * us (it polls at HZ/4 usually) before we report too many false errors. */#define THROTTLE_JIFFIES	(HZ/8) // between wakeups#define UNLINK_TIMEOUT_MS	3 /*-------------------------------------------------------------------------*/ /* use ethtool to change the level for any given device */static int msg_level = -1;module_param (msg_level, int, 0);MODULE_PARM_DESC (msg_level, "Override default message level"); /*-------------------------------------------------------------------------*/ static const char * const usbnet_event_names[] = {	[EVENT_TX_HALT]		   = "EVENT_TX_HALT",	[EVENT_RX_HALT]		   = "EVENT_RX_HALT",	[EVENT_RX_MEMORY]	   = "EVENT_RX_MEMORY",	[EVENT_STS_SPLIT]	   = "EVENT_STS_SPLIT",	[EVENT_LINK_RESET]	   = "EVENT_LINK_RESET",	[EVENT_RX_PAUSED]	   = "EVENT_RX_PAUSED",	[EVENT_DEV_ASLEEP]	   = "EVENT_DEV_ASLEEP",	[EVENT_DEV_OPEN]	   = "EVENT_DEV_OPEN",	[EVENT_DEVICE_REPORT_IDLE] = "EVENT_DEVICE_REPORT_IDLE",	[EVENT_NO_RUNTIME_PM]	   = "EVENT_NO_RUNTIME_PM",	[EVENT_RX_KILL]		   = "EVENT_RX_KILL",	[EVENT_LINK_CHANGE]	   = "EVENT_LINK_CHANGE",	[EVENT_SET_RX_MODE]	   = "EVENT_SET_RX_MODE",	[EVENT_NO_IP_ALIGN]	   = "EVENT_NO_IP_ALIGN",}; /* handles CDC Ethernet and many other network "bulk data" interfaces */int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf){	int				tmp;	struct usb_host_interface	*alt = NULL;	struct usb_host_endpoint	*in = NULL, *out = NULL;	struct usb_host_endpoint	*status = NULL; 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {		unsigned	ep; 		in = out = status = NULL;		alt = intf->altsetting + tmp; 		/* take the first altsetting with in-bulk + out-bulk;		 * remember any status endpoint, just in case;		 * ignore other endpoints and altsettings.		 */		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {			struct usb_host_endpoint	*e;			int				intr = 0; 			e = alt->endpoint + ep; 			/* ignore endpoints which cannot transfer data */			if (!usb_endpoint_maxp(&e->desc))				continue; 			switch (e->desc.bmAttributes) {			case USB_ENDPOINT_XFER_INT:				if (!usb_endpoint_dir_in(&e->desc))					continue;				intr = 1;				fallthrough;			case USB_ENDPOINT_XFER_BULK:				break;			default:				continue;			}			if (usb_endpoint_dir_in(&e->desc)) {				if (!intr && !in)					in = e;				else if (intr && !status)					status = e;			} else {				if (!out)					out = e;			}		}		if (in && out)			break;	}	if (!alt || !in || !out)		return -EINVAL; 	if (alt->desc.bAlternateSetting != 0 ||	    !(dev->driver_info->flags & FLAG_NO_SETINT)) {		tmp = usb_set_interface(dev->udev, alt->desc.bInterfaceNumber,					alt->desc.bAlternateSetting);		if (tmp < 0)			return tmp;	} 	dev->in = usb_rcvbulkpipe(dev->udev, usb_endpoint_num(&in->desc));	dev->out = usb_sndbulkpipe(dev->udev, usb_endpoint_num(&out->desc));	dev->status = status;	return 0;}EXPORT_SYMBOL_GPL(usbnet_get_endpoints); int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress){	u8		addr[ETH_ALEN];	int 		tmp = -1, ret;	unsigned char	buf [13]; 	ret = usb_string(dev->udev, iMACAddress, buf, sizeof(buf));	if (ret == 12)		tmp = hex2bin(addr, buf, 6);	if (tmp < 0) {		dev_dbg(&dev->udev->dev,			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);		if (ret >= 0)			ret = -EINVAL;		return ret;	}	eth_hw_addr_set(dev->net, addr);	return 0;}EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr); static bool usbnet_needs_usb_name_format(struct usbnet *dev, struct net_device *net){	/* Point to point devices which don't have a real MAC address	 * (or report a fake local one) have historically used the usb%d	 * naming. Preserve this..	 */	return (dev->driver_info->flags & FLAG_POINTTOPOINT) != 0 &&		(is_zero_ether_addr(net->dev_addr) ||		 is_local_ether_addr(net->dev_addr));} static void intr_complete(struct urb *urb){	struct usbnet	*dev = urb->context;	int		status = urb->status; 	switch (status) {	/* success */	case 0:		dev->driver_info->status(dev, urb);		break; 	/* software-driven interface shutdown */	case -ENOENT:		/* urb killed */	case -ESHUTDOWN:	/* hardware gone */		netif_dbg(dev, ifdown, dev->net,			  "intr shutdown, code %d\n", status);		return; 	/* NOTE:  not throttling like RX/TX, since this endpoint	 * already polls infrequently	 */	default:		netdev_dbg(dev->net, "intr status %d\n", status);		break;	} 	status = usb_submit_urb(urb, GFP_ATOMIC);	if (status != 0)		netif_err(dev, timer, dev->net,			  "intr resubmit --> %d\n", status);} static int init_status(struct usbnet *dev, struct usb_interface *intf){	char		*buf = NULL;	unsigned	pipe = 0;	unsigned	maxp;	unsigned	period; 	if (!dev->driver_info->status)		return 0; 	pipe = usb_rcvintpipe(dev->udev, usb_endpoint_num(&dev->status->desc));	maxp = usb_maxpacket(dev->udev, pipe); 	/* avoid 1 msec chatter:  min 8 msec poll rate */	period = max ((int) dev->status->desc.bInterval,		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); 	buf = kmalloc(maxp, GFP_KERNEL);	if (buf) {		dev->interrupt = usb_alloc_urb(0, GFP_KERNEL);		if (!dev->interrupt) {			kfree(buf);			return -ENOMEM;		} else {			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,					 buf, maxp, intr_complete, dev, period);			dev->interrupt->transfer_flags |= URB_FREE_BUFFER;			dev_dbg(&intf->dev,				"status ep%din, %d bytes period %d\n",				usb_pipeendpoint(pipe), maxp, period);		}	}	return 0;} /* Submit the interrupt URB if not previously submitted, increasing refcount */int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags){	int ret = 0; 	WARN_ON_ONCE(dev->interrupt == NULL);	if (dev->interrupt) {		mutex_lock(&dev->interrupt_mutex); 		if (++dev->interrupt_count == 1)			ret = usb_submit_urb(dev->interrupt, mem_flags); 		dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",			dev->interrupt_count);		mutex_unlock(&dev->interrupt_mutex);	}	return ret;}EXPORT_SYMBOL_GPL(usbnet_status_start); /* For resume; submit interrupt URB if previously submitted */static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags){	int ret = 0; 	mutex_lock(&dev->interrupt_mutex);	if (dev->interrupt_count) {		ret = usb_submit_urb(dev->interrupt, mem_flags);		dev_dbg(&dev->udev->dev,			"submitted interrupt URB for resume\n");	}	mutex_unlock(&dev->interrupt_mutex);	return ret;} /* Kill the interrupt URB if all submitters want it killed */void usbnet_status_stop(struct usbnet *dev){	if (dev->interrupt) {		mutex_lock(&dev->interrupt_mutex);		WARN_ON(dev->interrupt_count == 0); 		if (dev->interrupt_count && --dev->interrupt_count == 0)			usb_kill_urb(dev->interrupt); 		dev_dbg(&dev->udev->dev,			"decremented interrupt URB count to %d\n",			dev->interrupt_count);		mutex_unlock(&dev->interrupt_mutex);	}}EXPORT_SYMBOL_GPL(usbnet_status_stop); /* For suspend; always kill interrupt URB */static void __usbnet_status_stop_force(struct usbnet *dev){	if (dev->interrupt) {		mutex_lock(&dev->interrupt_mutex);		usb_kill_urb(dev->interrupt);		dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");		mutex_unlock(&dev->interrupt_mutex);	}} /* Passes this packet up the stack, updating its accounting. * Some link protocols batch packets, so their rx_fixup paths * can return clones as well as just modify the original skb. */void usbnet_skb_return(struct usbnet *dev, struct sk_buff *skb){	struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats);	unsigned long flags;	int	status; 	if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {		skb_queue_tail(&dev->rxq_pause, skb);		return;	} 	/* only update if unset to allow minidriver rx_fixup override */	if (skb->protocol == 0)		skb->protocol = eth_type_trans(skb, dev->net); 	flags = u64_stats_update_begin_irqsave(&stats64->syncp);	u64_stats_inc(&stats64->rx_packets);	u64_stats_add(&stats64->rx_bytes, skb->len);	u64_stats_update_end_irqrestore(&stats64->syncp, flags); 	netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",		  skb->len + sizeof(struct ethhdr), skb->protocol);	memset(skb->cb, 0, sizeof(struct skb_data)); 	if (skb_defer_rx_timestamp(skb))		return; 	status = netif_rx (skb);	if (status != NET_RX_SUCCESS)		netif_dbg(dev, rx_err, dev->net,			  "netif_rx status %d\n", status);}EXPORT_SYMBOL_GPL(usbnet_skb_return); /* must be called if hard_mtu or rx_urb_size changed */void usbnet_update_max_qlen(struct usbnet *dev){	enum usb_device_speed speed = dev->udev->speed; 	if (!dev->rx_urb_size || !dev->hard_mtu)		goto insanity;	switch (speed) {	case USB_SPEED_HIGH:		dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;		dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;		break;	case USB_SPEED_SUPER:	case USB_SPEED_SUPER_PLUS:		/*		 * Not take default 5ms qlen for super speed HC to		 * save memory, and iperf tests show 2.5ms qlen can		 * work well		 */		dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;		dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;		break;	default:insanity:		dev->rx_qlen = dev->tx_qlen = 4;	}}EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);  /*------------------------------------------------------------------------- * * Network Device Driver (peer link to "Host Device", from USB host) * *-------------------------------------------------------------------------*/ int usbnet_change_mtu(struct net_device *net, int new_mtu){	struct usbnet	*dev = netdev_priv(net);	int		ll_mtu = new_mtu + net->hard_header_len;	int		old_hard_mtu = dev->hard_mtu;	int		old_rx_urb_size = dev->rx_urb_size; 	// no second zero-length packet read wanted after mtu-sized packets	if ((ll_mtu % dev->maxpacket) == 0)		return -EDOM;	WRITE_ONCE(net->mtu, new_mtu); 	dev->hard_mtu = net->mtu + net->hard_header_len;	if (dev->rx_urb_size == old_hard_mtu) {		dev->rx_urb_size = dev->hard_mtu;		if (dev->rx_urb_size > old_rx_urb_size) {			usbnet_pause_rx(dev);			usbnet_unlink_rx_urbs(dev);			usbnet_resume_rx(dev);		}	} 	/* max qlen depend on hard_mtu and rx_urb_size */	usbnet_update_max_qlen(dev); 	return 0;}EXPORT_SYMBOL_GPL(usbnet_change_mtu); /* The caller must hold list->lock */static void __usbnet_queue_skb(struct sk_buff_head *list,			struct sk_buff *newsk, enum skb_state state){	struct skb_data *entry = (struct skb_data *) newsk->cb; 	__skb_queue_tail(list, newsk);	entry->state = state;} /*-------------------------------------------------------------------------*/ /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from * completion callbacks.  2.5 should have fixed those bugs... */ static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,		struct sk_buff_head *list, enum skb_state state){	unsigned long		flags;	enum skb_state 		old_state;	struct skb_data *entry = (struct skb_data *) skb->cb; 	spin_lock_irqsave(&list->lock, flags);	old_state = entry->state;	entry->state = state;	__skb_unlink(skb, list); 	/* defer_bh() is never called with list == &dev->done.	 * spin_lock_nested() tells lockdep that it is OK to take	 * dev->done.lock here with list->lock held.	 */	spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING); 	__skb_queue_tail(&dev->done, skb);	if (dev->done.qlen == 1)		queue_work(system_bh_wq, &dev->bh_work);	spin_unlock(&dev->done.lock);	spin_unlock_irqrestore(&list->lock, flags);	return old_state;} /* some work can't be done in tasklets, so we use keventd * * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails, * but tasklet_schedule() doesn't.  hope the failure is rare. */void usbnet_defer_kevent(struct usbnet *dev, int work){	set_bit (work, &dev->flags);	if (!usbnet_going_away(dev)) {		if (!schedule_work(&dev->kevent))			netdev_dbg(dev->net,				   "kevent %s may have been dropped\n",				   usbnet_event_names[work]);		else			netdev_dbg(dev->net,				   "kevent %s scheduled\n", usbnet_event_names[work]);	}}EXPORT_SYMBOL_GPL(usbnet_defer_kevent); /*-------------------------------------------------------------------------*/ static void rx_complete(struct urb *urb); static int rx_submit(struct usbnet *dev, struct urb *urb, gfp_t flags){	struct sk_buff		*skb;	struct skb_data		*entry;	int			retval = 0;	unsigned long		lockflags;	size_t			size = dev->rx_urb_size; 	/* prevent rx skb allocation when error ratio is high */	if (test_bit(EVENT_RX_KILL, &dev->flags)) {

Lines 1–500 of 2294.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t