981 lines · 26.9 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * f_ecm.c -- USB CDC Ethernet (ECM) link function driver * * Copyright (C) 2003-2005,2008 David Brownell * Copyright (C) 2008 Nokia Corporation */ /* #define VERBOSE_DEBUG */ #include <linux/cleanup.h>#include <linux/slab.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/device.h>#include <linux/etherdevice.h>#include <linux/string_choices.h> #include <linux/usb/gadget.h> #include "u_ether.h"#include "u_ether_configfs.h"#include "u_ecm.h"  /* * This function is a "CDC Ethernet Networking Control Model" (CDC ECM) * Ethernet link.  The data transfer model is simple (packets sent and * received over bulk endpoints using normal short packet termination), * and the control model exposes various data and optional notifications. * * ECM is well standardized and (except for Microsoft) supported by most * operating systems with USB host support.  It's the preferred interop * solution for Ethernet over USB, at least for firmware based solutions. * (Hardware solutions tend to be more minimalist.)  A newer and simpler * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on. * * Note that ECM requires the use of "alternate settings" for its data * interface.  This means that the set_alt() method has real work to do, * and also means that a get_alt() method is required. */  enum ecm_notify_state {	ECM_NOTIFY_NONE,		/* don't notify */	ECM_NOTIFY_CONNECT,		/* issue CONNECT next */	ECM_NOTIFY_SPEED,		/* issue SPEED_CHANGE next */}; struct f_ecm {	struct gether			port;	u8				ctrl_id, data_id; 	char				ethaddr[14]; 	struct usb_ep			*notify;	struct usb_request		*notify_req;	u8				notify_state;	atomic_t			notify_count;	bool				is_open; 	/* FIXME is_open needs some irq-ish locking	 * ... possibly the same as port.ioport	 */}; static inline struct f_ecm *func_to_ecm(struct usb_function *f){	return container_of(f, struct f_ecm, port.func);} /*-------------------------------------------------------------------------*/ /* * Include the status endpoint if we can, even though it's optional. * * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one * packet, to simplify cancellation; and a big transfer interval, to * waste less bandwidth. * * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even * if they ignore the connect/disconnect notifications that real aether * can provide.  More advanced cdc configurations might want to support * encapsulated commands (vendor-specific, using control-OUT). */ #define ECM_STATUS_INTERVAL_MS		32#define ECM_STATUS_BYTECOUNT		16	/* 8 byte header + data */  /* interface descriptor: */ static struct usb_interface_assoc_descriptorecm_iad_descriptor = {	.bLength =		sizeof ecm_iad_descriptor,	.bDescriptorType =	USB_DT_INTERFACE_ASSOCIATION, 	/* .bFirstInterface =	DYNAMIC, */	.bInterfaceCount =	2,	/* control + data */	.bFunctionClass =	USB_CLASS_COMM,	.bFunctionSubClass =	USB_CDC_SUBCLASS_ETHERNET,	.bFunctionProtocol =	USB_CDC_PROTO_NONE,	/* .iFunction =		DYNAMIC */};  static struct usb_interface_descriptor ecm_control_intf = {	.bLength =		sizeof ecm_control_intf,	.bDescriptorType =	USB_DT_INTERFACE, 	/* .bInterfaceNumber = DYNAMIC */	/* status endpoint is optional; this could be patched later */	.bNumEndpoints =	1,	.bInterfaceClass =	USB_CLASS_COMM,	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,	/* .iInterface = DYNAMIC */}; static struct usb_cdc_header_desc ecm_header_desc = {	.bLength =		sizeof ecm_header_desc,	.bDescriptorType =	USB_DT_CS_INTERFACE,	.bDescriptorSubType =	USB_CDC_HEADER_TYPE, 	.bcdCDC =		cpu_to_le16(0x0110),}; static struct usb_cdc_union_desc ecm_union_desc = {	.bLength =		sizeof(ecm_union_desc),	.bDescriptorType =	USB_DT_CS_INTERFACE,	.bDescriptorSubType =	USB_CDC_UNION_TYPE,	/* .bMasterInterface0 =	DYNAMIC */	/* .bSlaveInterface0 =	DYNAMIC */}; static struct usb_cdc_ether_desc ecm_desc = {	.bLength =		sizeof ecm_desc,	.bDescriptorType =	USB_DT_CS_INTERFACE,	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE, 	/* this descriptor actually adds value, surprise! */	/* .iMACAddress = DYNAMIC */	.bmEthernetStatistics =	cpu_to_le32(0), /* no statistics */	.wMaxSegmentSize =	cpu_to_le16(ETH_FRAME_LEN),	.wNumberMCFilters =	cpu_to_le16(0),	.bNumberPowerFilters =	0,}; /* the default data interface has no endpoints ... */ static struct usb_interface_descriptor ecm_data_nop_intf = {	.bLength =		sizeof ecm_data_nop_intf,	.bDescriptorType =	USB_DT_INTERFACE, 	.bInterfaceNumber =	1,	.bAlternateSetting =	0,	.bNumEndpoints =	0,	.bInterfaceClass =	USB_CLASS_CDC_DATA,	.bInterfaceSubClass =	0,	.bInterfaceProtocol =	0,	/* .iInterface = DYNAMIC */}; /* ... but the "real" data interface has two bulk endpoints */ static struct usb_interface_descriptor ecm_data_intf = {	.bLength =		sizeof ecm_data_intf,	.bDescriptorType =	USB_DT_INTERFACE, 	.bInterfaceNumber =	1,	.bAlternateSetting =	1,	.bNumEndpoints =	2,	.bInterfaceClass =	USB_CLASS_CDC_DATA,	.bInterfaceSubClass =	0,	.bInterfaceProtocol =	0,	/* .iInterface = DYNAMIC */}; /* full speed support: */ static struct usb_endpoint_descriptor fs_ecm_notify_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_IN,	.bmAttributes =		USB_ENDPOINT_XFER_INT,	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),	.bInterval =		ECM_STATUS_INTERVAL_MS,}; static struct usb_endpoint_descriptor fs_ecm_in_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_IN,	.bmAttributes =		USB_ENDPOINT_XFER_BULK,}; static struct usb_endpoint_descriptor fs_ecm_out_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_OUT,	.bmAttributes =		USB_ENDPOINT_XFER_BULK,}; static struct usb_descriptor_header *ecm_fs_function[] = {	/* CDC ECM control descriptors */	(struct usb_descriptor_header *) &ecm_iad_descriptor,	(struct usb_descriptor_header *) &ecm_control_intf,	(struct usb_descriptor_header *) &ecm_header_desc,	(struct usb_descriptor_header *) &ecm_union_desc,	(struct usb_descriptor_header *) &ecm_desc, 	/* NOTE: status endpoint might need to be removed */	(struct usb_descriptor_header *) &fs_ecm_notify_desc, 	/* data interface, altsettings 0 and 1 */	(struct usb_descriptor_header *) &ecm_data_nop_intf,	(struct usb_descriptor_header *) &ecm_data_intf,	(struct usb_descriptor_header *) &fs_ecm_in_desc,	(struct usb_descriptor_header *) &fs_ecm_out_desc,	NULL,}; /* high speed support: */ static struct usb_endpoint_descriptor hs_ecm_notify_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_IN,	.bmAttributes =		USB_ENDPOINT_XFER_INT,	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),	.bInterval =		USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),}; static struct usb_endpoint_descriptor hs_ecm_in_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_IN,	.bmAttributes =		USB_ENDPOINT_XFER_BULK,	.wMaxPacketSize =	cpu_to_le16(512),}; static struct usb_endpoint_descriptor hs_ecm_out_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_OUT,	.bmAttributes =		USB_ENDPOINT_XFER_BULK,	.wMaxPacketSize =	cpu_to_le16(512),}; static struct usb_descriptor_header *ecm_hs_function[] = {	/* CDC ECM control descriptors */	(struct usb_descriptor_header *) &ecm_iad_descriptor,	(struct usb_descriptor_header *) &ecm_control_intf,	(struct usb_descriptor_header *) &ecm_header_desc,	(struct usb_descriptor_header *) &ecm_union_desc,	(struct usb_descriptor_header *) &ecm_desc, 	/* NOTE: status endpoint might need to be removed */	(struct usb_descriptor_header *) &hs_ecm_notify_desc, 	/* data interface, altsettings 0 and 1 */	(struct usb_descriptor_header *) &ecm_data_nop_intf,	(struct usb_descriptor_header *) &ecm_data_intf,	(struct usb_descriptor_header *) &hs_ecm_in_desc,	(struct usb_descriptor_header *) &hs_ecm_out_desc,	NULL,}; /* super speed support: */ static struct usb_endpoint_descriptor ss_ecm_notify_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_IN,	.bmAttributes =		USB_ENDPOINT_XFER_INT,	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),	.bInterval =		USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),}; static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {	.bLength =		sizeof ss_ecm_intr_comp_desc,	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP, 	/* the following 3 values can be tweaked if necessary */	/* .bMaxBurst =		0, */	/* .bmAttributes =	0, */	.wBytesPerInterval =	cpu_to_le16(ECM_STATUS_BYTECOUNT),}; static struct usb_endpoint_descriptor ss_ecm_in_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_IN,	.bmAttributes =		USB_ENDPOINT_XFER_BULK,	.wMaxPacketSize =	cpu_to_le16(1024),}; static struct usb_endpoint_descriptor ss_ecm_out_desc = {	.bLength =		USB_DT_ENDPOINT_SIZE,	.bDescriptorType =	USB_DT_ENDPOINT, 	.bEndpointAddress =	USB_DIR_OUT,	.bmAttributes =		USB_ENDPOINT_XFER_BULK,	.wMaxPacketSize =	cpu_to_le16(1024),}; static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {	.bLength =		sizeof ss_ecm_bulk_comp_desc,	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP, 	/* the following 2 values can be tweaked if necessary */	/* .bMaxBurst =		0, */	/* .bmAttributes =	0, */}; static struct usb_descriptor_header *ecm_ss_function[] = {	/* CDC ECM control descriptors */	(struct usb_descriptor_header *) &ecm_iad_descriptor,	(struct usb_descriptor_header *) &ecm_control_intf,	(struct usb_descriptor_header *) &ecm_header_desc,	(struct usb_descriptor_header *) &ecm_union_desc,	(struct usb_descriptor_header *) &ecm_desc, 	/* NOTE: status endpoint might need to be removed */	(struct usb_descriptor_header *) &ss_ecm_notify_desc,	(struct usb_descriptor_header *) &ss_ecm_intr_comp_desc, 	/* data interface, altsettings 0 and 1 */	(struct usb_descriptor_header *) &ecm_data_nop_intf,	(struct usb_descriptor_header *) &ecm_data_intf,	(struct usb_descriptor_header *) &ss_ecm_in_desc,	(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,	(struct usb_descriptor_header *) &ss_ecm_out_desc,	(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,	NULL,}; /* string descriptors: */ static struct usb_string ecm_string_defs[] = {	[0].s = "CDC Ethernet Control Model (ECM)",	[1].s = "",	[2].s = "CDC Ethernet Data",	[3].s = "CDC ECM",	{  } /* end of list */}; static struct usb_gadget_strings ecm_string_table = {	.language =		0x0409,	/* en-us */	.strings =		ecm_string_defs,}; static struct usb_gadget_strings *ecm_strings[] = {	&ecm_string_table,	NULL,}; /*-------------------------------------------------------------------------*/ static void ecm_do_notify(struct f_ecm *ecm){	struct usb_request		*req = ecm->notify_req;	struct usb_cdc_notification	*event;	struct usb_composite_dev	*cdev = ecm->port.func.config->cdev;	__le32				*data;	int				status; 	/* notification already in flight? */	if (atomic_read(&ecm->notify_count))		return; 	event = req->buf;	switch (ecm->notify_state) {	case ECM_NOTIFY_NONE:		return; 	case ECM_NOTIFY_CONNECT:		event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;		if (ecm->is_open)			event->wValue = cpu_to_le16(1);		else			event->wValue = cpu_to_le16(0);		event->wLength = 0;		req->length = sizeof *event; 		DBG(cdev, "notify connect %s\n", str_true_false(ecm->is_open));		ecm->notify_state = ECM_NOTIFY_SPEED;		break; 	case ECM_NOTIFY_SPEED:		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;		event->wValue = cpu_to_le16(0);		event->wLength = cpu_to_le16(8);		req->length = ECM_STATUS_BYTECOUNT; 		/* SPEED_CHANGE data is up/down speeds in bits/sec */		data = req->buf + sizeof *event;		data[0] = cpu_to_le32(gether_bitrate(cdev->gadget));		data[1] = data[0]; 		DBG(cdev, "notify speed %d\n", gether_bitrate(cdev->gadget));		ecm->notify_state = ECM_NOTIFY_NONE;		break;	}	event->bmRequestType = 0xA1;	event->wIndex = cpu_to_le16(ecm->ctrl_id); 	atomic_inc(&ecm->notify_count);	status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);	if (status < 0) {		atomic_dec(&ecm->notify_count);		DBG(cdev, "notify --> %d\n", status);	}} static void ecm_notify(struct f_ecm *ecm){	/* NOTE on most versions of Linux, host side cdc-ethernet	 * won't listen for notifications until its netdevice opens.	 * The first notification then sits in the FIFO for a long	 * time, and the second one is queued.	 */	ecm->notify_state = ECM_NOTIFY_CONNECT;	ecm_do_notify(ecm);} static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req){	struct f_ecm			*ecm = req->context;	struct usb_composite_dev	*cdev = ecm->port.func.config->cdev;	struct usb_cdc_notification	*event = req->buf; 	switch (req->status) {	case 0:		/* no fault */		atomic_dec(&ecm->notify_count);		break;	case -ECONNRESET:	case -ESHUTDOWN:		atomic_set(&ecm->notify_count, 0);		ecm->notify_state = ECM_NOTIFY_NONE;		break;	default:		DBG(cdev, "event %02x --> %d\n",			event->bNotificationType, req->status);		atomic_dec(&ecm->notify_count);		break;	}	ecm_do_notify(ecm);} static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl){	struct f_ecm		*ecm = func_to_ecm(f);	struct usb_composite_dev *cdev = f->config->cdev;	struct usb_request	*req = cdev->req;	int			value = -EOPNOTSUPP;	u16			w_index = le16_to_cpu(ctrl->wIndex);	u16			w_value = le16_to_cpu(ctrl->wValue);	u16			w_length = le16_to_cpu(ctrl->wLength); 	/* composite driver infrastructure handles everything except	 * CDC class messages; interface activation uses set_alt().	 */	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)			| USB_CDC_SET_ETHERNET_PACKET_FILTER:		/* see 6.2.30: no data, wIndex = interface,		 * wValue = packet filter bitmap		 */		if (w_length != 0 || w_index != ecm->ctrl_id)			goto invalid;		DBG(cdev, "packet filter %02x\n", w_value);		/* REVISIT locking of cdc_filter.  This assumes the UDC		 * driver won't have a concurrent packet TX irq running on		 * another CPU; or that if it does, this write is atomic...		 */		ecm->port.cdc_filter = w_value;		value = 0;		break; 	/* and optionally:	 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:	 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:	 * case USB_CDC_GET_ETHERNET_STATISTIC:	 */ 	default:invalid:

Lines 1–500 of 981.