f_ecm.c 26.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
 *
 * Copyright (C) 2003-2005,2008 David Brownell
 * Copyright (C) 2008 Nokia Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/* #define VERBOSE_DEBUG */

15
#include <linux/slab.h>
16
#include <linux/kernel.h>
17
#include <linux/module.h>
18 19 20 21
#include <linux/device.h>
#include <linux/etherdevice.h>

#include "u_ether.h"
22
#include "u_ether_configfs.h"
23
#include "u_ecm.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71


/*
 * 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;
	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);
}

/* peak (theoretical) bulk transfer rate in bits-per-second */
72
static inline unsigned ecm_bitrate(struct usb_gadget *g)
73
{
74 75 76
	if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
		return 13 * 1024 * 8 * 1000 * 8;
	else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
77 78
		return 13 * 512 * 8 * 1000 * 8;
	else
79
		return 19 * 64 * 1 * 1000 * 8;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

/*-------------------------------------------------------------------------*/

/*
 * 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).
 */

97
#define ECM_STATUS_INTERVAL_MS		32
98
#define ECM_STATUS_BYTECOUNT		16	/* 8 byte header + data */
99 100 101 102


/* interface descriptor: */

103 104 105 106 107 108 109 110 111 112 113 114 115 116
static struct usb_interface_assoc_descriptor
ecm_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 */
};


117
static struct usb_interface_descriptor ecm_control_intf = {
118 119 120 121 122 123 124 125 126 127 128 129
	.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 */
};

130
static struct usb_cdc_header_desc ecm_header_desc = {
131
	.bLength =		sizeof ecm_header_desc,
132 133 134
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,

135
	.bcdCDC =		cpu_to_le16(0x0110),
136 137
};

138
static struct usb_cdc_union_desc ecm_union_desc = {
139 140 141 142 143 144 145
	.bLength =		sizeof(ecm_union_desc),
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
	/* .bMasterInterface0 =	DYNAMIC */
	/* .bSlaveInterface0 =	DYNAMIC */
};

146
static struct usb_cdc_ether_desc ecm_desc = {
147
	.bLength =		sizeof ecm_desc,
148 149 150 151 152
	.bDescriptorType =	USB_DT_CS_INTERFACE,
	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,

	/* this descriptor actually adds value, surprise! */
	/* .iMACAddress = DYNAMIC */
153 154 155
	.bmEthernetStatistics =	cpu_to_le32(0), /* no statistics */
	.wMaxSegmentSize =	cpu_to_le16(ETH_FRAME_LEN),
	.wNumberMCFilters =	cpu_to_le16(0),
156 157 158 159 160
	.bNumberPowerFilters =	0,
};

/* the default data interface has no endpoints ... */

161
static struct usb_interface_descriptor ecm_data_nop_intf = {
162 163 164 165 166 167 168 169 170 171 172 173 174 175
	.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 */

176
static struct usb_interface_descriptor ecm_data_intf = {
177 178 179 180 181 182 183 184 185 186 187 188 189 190
	.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: */

191
static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
192 193 194 195 196
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_INT,
197
	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
198
	.bInterval =		ECM_STATUS_INTERVAL_MS,
199 200
};

201
static struct usb_endpoint_descriptor fs_ecm_in_desc = {
202 203 204 205 206 207 208
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
};

209
static struct usb_endpoint_descriptor fs_ecm_out_desc = {
210 211 212 213 214 215 216
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_OUT,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
};

217
static struct usb_descriptor_header *ecm_fs_function[] = {
218
	/* CDC ECM control descriptors */
219
	(struct usb_descriptor_header *) &ecm_iad_descriptor,
220
	(struct usb_descriptor_header *) &ecm_control_intf,
221
	(struct usb_descriptor_header *) &ecm_header_desc,
222
	(struct usb_descriptor_header *) &ecm_union_desc,
223
	(struct usb_descriptor_header *) &ecm_desc,
224

225
	/* NOTE: status endpoint might need to be removed */
226
	(struct usb_descriptor_header *) &fs_ecm_notify_desc,
227

228 229 230
	/* data interface, altsettings 0 and 1 */
	(struct usb_descriptor_header *) &ecm_data_nop_intf,
	(struct usb_descriptor_header *) &ecm_data_intf,
231 232
	(struct usb_descriptor_header *) &fs_ecm_in_desc,
	(struct usb_descriptor_header *) &fs_ecm_out_desc,
233 234 235 236 237
	NULL,
};

/* high speed support: */

238
static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
239 240 241 242 243
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_INT,
244
	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
245
	.bInterval =		USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
246
};
247

248
static struct usb_endpoint_descriptor hs_ecm_in_desc = {
249 250 251 252 253
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
254
	.wMaxPacketSize =	cpu_to_le16(512),
255 256
};

257
static struct usb_endpoint_descriptor hs_ecm_out_desc = {
258 259 260 261 262
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_OUT,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
263
	.wMaxPacketSize =	cpu_to_le16(512),
264 265
};

266
static struct usb_descriptor_header *ecm_hs_function[] = {
267
	/* CDC ECM control descriptors */
268
	(struct usb_descriptor_header *) &ecm_iad_descriptor,
269
	(struct usb_descriptor_header *) &ecm_control_intf,
270
	(struct usb_descriptor_header *) &ecm_header_desc,
271
	(struct usb_descriptor_header *) &ecm_union_desc,
272
	(struct usb_descriptor_header *) &ecm_desc,
273

274
	/* NOTE: status endpoint might need to be removed */
275
	(struct usb_descriptor_header *) &hs_ecm_notify_desc,
276

277 278 279
	/* data interface, altsettings 0 and 1 */
	(struct usb_descriptor_header *) &ecm_data_nop_intf,
	(struct usb_descriptor_header *) &ecm_data_intf,
280 281
	(struct usb_descriptor_header *) &hs_ecm_in_desc,
	(struct usb_descriptor_header *) &hs_ecm_out_desc,
282 283 284
	NULL,
};

285 286 287 288 289 290 291 292 293
/* 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),
294
	.bInterval =		USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
};

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 */
336
	(struct usb_descriptor_header *) &ecm_iad_descriptor,
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	(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,
};

356 357 358 359
/* string descriptors: */

static struct usb_string ecm_string_defs[] = {
	[0].s = "CDC Ethernet Control Model (ECM)",
360
	[1].s = "",
361
	[2].s = "CDC Ethernet Data",
362
	[3].s = "CDC ECM",
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	{  } /* 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 (!req)
		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",
				ecm->is_open ? "true" : "false");
		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);
413
		req->length = ECM_STATUS_BYTECOUNT;
414 415 416

		/* SPEED_CHANGE data is up/down speeds in bits/sec */
		data = req->buf + sizeof *event;
417
		data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
418 419
		data[1] = data[0];

420
		DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		ecm->notify_state = ECM_NOTIFY_NONE;
		break;
	}
	event->bmRequestType = 0xA1;
	event->wIndex = cpu_to_le16(ecm->ctrl_id);

	ecm->notify_req = NULL;
	status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
	if (status < 0) {
		ecm->notify_req = req;
		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 */
		break;
	case -ECONNRESET:
	case -ESHUTDOWN:
		ecm->notify_state = ECM_NOTIFY_NONE;
		break;
	default:
		DBG(cdev, "event %02x --> %d\n",
			event->bNotificationType, req->status);
		break;
	}
	ecm->notify_req = req;
	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:
		DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
			ctrl->bRequestType, ctrl->bRequest,
			w_value, w_index, w_length);
	}

	/* respond with data transfer or status phase? */
	if (value >= 0) {
		DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
			ctrl->bRequestType, ctrl->bRequest,
			w_value, w_index, w_length);
		req->zero = 0;
		req->length = value;
		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
		if (value < 0)
			ERROR(cdev, "ecm req %02x.%02x response err %d\n",
					ctrl->bRequestType, ctrl->bRequest,
					value);
	}

	/* device either stalls (value < 0) or reports success */
	return value;
}


static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
	struct f_ecm		*ecm = func_to_ecm(f);
	struct usb_composite_dev *cdev = f->config->cdev;

	/* Control interface has only altsetting 0 */
	if (intf == ecm->ctrl_id) {
		if (alt != 0)
			goto fail;

544 545
		VDBG(cdev, "reset ecm control %d\n", intf);
		usb_ep_disable(ecm->notify);
546
		if (!(ecm->notify->desc)) {
547
			VDBG(cdev, "init ecm ctrl %d\n", intf);
548 549
			if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
				goto fail;
550
		}
551
		usb_ep_enable(ecm->notify);
552 553 554 555 556 557

	/* Data interface has two altsettings, 0 and 1 */
	} else if (intf == ecm->data_id) {
		if (alt > 1)
			goto fail;

558
		if (ecm->port.in_ep->enabled) {
559 560 561 562
			DBG(cdev, "reset ecm\n");
			gether_disconnect(&ecm->port);
		}

563 564
		if (!ecm->port.in_ep->desc ||
		    !ecm->port.out_ep->desc) {
565
			DBG(cdev, "init ecm\n");
566 567 568 569 570 571 572 573
			if (config_ep_by_speed(cdev->gadget, f,
					       ecm->port.in_ep) ||
			    config_ep_by_speed(cdev->gadget, f,
					       ecm->port.out_ep)) {
				ecm->port.in_ep->desc = NULL;
				ecm->port.out_ep->desc = NULL;
				goto fail;
			}
574 575 576 577 578 579 580 581 582
		}

		/* CDC Ethernet only sends data in non-default altsettings.
		 * Changing altsettings resets filters, statistics, etc.
		 */
		if (alt == 1) {
			struct net_device	*net;

			/* Enable zlps by default for ECM conformance;
583
			 * override for musb_hdrc (avoids txdma ovhead).
584
			 */
585 586
			ecm->port.is_zlp_ok =
				gadget_is_zlp_supported(cdev->gadget);
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
			ecm->port.cdc_filter = DEFAULT_FILTER;
			DBG(cdev, "activate ecm\n");
			net = gether_connect(&ecm->port);
			if (IS_ERR(net))
				return PTR_ERR(net);
		}

		/* NOTE this can be a minor disagreement with the ECM spec,
		 * which says speed notifications will "always" follow
		 * connection notifications.  But we allow one connect to
		 * follow another (if the first is in flight), and instead
		 * just guarantee that a speed notification is always sent.
		 */
		ecm_notify(ecm);
	} else
		goto fail;

	return 0;
fail:
	return -EINVAL;
}

/* Because the data interface supports multiple altsettings,
 * this ECM function *MUST* implement a get_alt() method.
 */
static int ecm_get_alt(struct usb_function *f, unsigned intf)
{
	struct f_ecm		*ecm = func_to_ecm(f);

	if (intf == ecm->ctrl_id)
		return 0;
618
	return ecm->port.in_ep->enabled ? 1 : 0;
619 620 621 622 623 624 625 626 627
}

static void ecm_disable(struct usb_function *f)
{
	struct f_ecm		*ecm = func_to_ecm(f);
	struct usb_composite_dev *cdev = f->config->cdev;

	DBG(cdev, "ecm deactivated\n");

628
	if (ecm->port.in_ep->enabled)
629 630
		gether_disconnect(&ecm->port);

631 632
	usb_ep_disable(ecm->notify);
	ecm->notify->desc = NULL;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
}

/*-------------------------------------------------------------------------*/

/*
 * Callbacks let us notify the host about connect/disconnect when the
 * net device is opened or closed.
 *
 * For testing, note that link states on this side include both opened
 * and closed variants of:
 *
 *   - disconnected/unconfigured
 *   - configured but inactive (data alt 0)
 *   - configured and active (data alt 1)
 *
 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
 * SET_INTERFACE (altsetting).  Remember also that "configured" doesn't
 * imply the host is actually polling the notification endpoint, and
 * likewise that "active" doesn't imply it's actually using the data
 * endpoints for traffic.
 */

static void ecm_open(struct gether *geth)
{
	struct f_ecm		*ecm = func_to_ecm(&geth->func);

	DBG(ecm->port.func.config->cdev, "%s\n", __func__);

	ecm->is_open = true;
	ecm_notify(ecm);
}

static void ecm_close(struct gether *geth)
{
	struct f_ecm		*ecm = func_to_ecm(&geth->func);

	DBG(ecm->port.func.config->cdev, "%s\n", __func__);

	ecm->is_open = false;
	ecm_notify(ecm);
}

/*-------------------------------------------------------------------------*/

/* ethernet function driver setup/binding */

679
static int
680 681 682 683
ecm_bind(struct usb_configuration *c, struct usb_function *f)
{
	struct usb_composite_dev *cdev = c->cdev;
	struct f_ecm		*ecm = func_to_ecm(f);
684
	struct usb_string	*us;
685 686 687
	int			status;
	struct usb_ep		*ep;

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	struct f_ecm_opts	*ecm_opts;

	if (!can_support_ecm(cdev->gadget))
		return -EINVAL;

	ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);

	/*
	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
	 * configurations are bound in sequence with list_for_each_entry,
	 * in each configuration its functions are bound in sequence
	 * with list_for_each_entry, so we assume no race condition
	 * with regard to ecm_opts->bound access
	 */
	if (!ecm_opts->bound) {
703
		mutex_lock(&ecm_opts->lock);
704 705
		gether_set_gadget(ecm_opts->net, cdev->gadget);
		status = gether_register_netdev(ecm_opts->net);
706
		mutex_unlock(&ecm_opts->lock);
707 708 709 710
		if (status)
			return status;
		ecm_opts->bound = true;
	}
711

712 713 714 715 716 717 718 719
	us = usb_gstrings_attach(cdev, ecm_strings,
				 ARRAY_SIZE(ecm_string_defs));
	if (IS_ERR(us))
		return PTR_ERR(us);
	ecm_control_intf.iInterface = us[0].id;
	ecm_data_intf.iInterface = us[2].id;
	ecm_desc.iMACAddress = us[1].id;
	ecm_iad_descriptor.iFunction = us[3].id;
720

721 722 723 724 725
	/* allocate instance-specific interface IDs */
	status = usb_interface_id(c, f);
	if (status < 0)
		goto fail;
	ecm->ctrl_id = status;
726
	ecm_iad_descriptor.bFirstInterface = status;
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

	ecm_control_intf.bInterfaceNumber = status;
	ecm_union_desc.bMasterInterface0 = status;

	status = usb_interface_id(c, f);
	if (status < 0)
		goto fail;
	ecm->data_id = status;

	ecm_data_nop_intf.bInterfaceNumber = status;
	ecm_data_intf.bInterfaceNumber = status;
	ecm_union_desc.bSlaveInterface0 = status;

	status = -ENODEV;

	/* allocate instance-specific endpoints */
743
	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
744 745 746 747
	if (!ep)
		goto fail;
	ecm->port.in_ep = ep;

748
	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
749 750 751 752 753 754 755 756
	if (!ep)
		goto fail;
	ecm->port.out_ep = ep;

	/* NOTE:  a status/notification endpoint is *OPTIONAL* but we
	 * don't treat it that way.  It's simpler, and some newer CDC
	 * profiles (wireless handsets) no longer treat it as optional.
	 */
757
	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
758 759 760 761 762 763 764 765 766 767
	if (!ep)
		goto fail;
	ecm->notify = ep;

	status = -ENOMEM;

	/* allocate notification request and buffer */
	ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
	if (!ecm->notify_req)
		goto fail;
768
	ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
769 770 771 772 773 774 775 776 777
	if (!ecm->notify_req->buf)
		goto fail;
	ecm->notify_req->context = ecm;
	ecm->notify_req->complete = ecm_notify_complete;

	/* support all relevant hardware speeds... we expect that when
	 * hardware is dual speed, all bulk-capable endpoints work at
	 * both speeds
	 */
778 779 780 781 782 783 784 785 786 787 788
	hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
	hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
	hs_ecm_notify_desc.bEndpointAddress =
		fs_ecm_notify_desc.bEndpointAddress;

	ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
	ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
	ss_ecm_notify_desc.bEndpointAddress =
		fs_ecm_notify_desc.bEndpointAddress;

	status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
789
			ecm_ss_function, NULL);
790 791
	if (status)
		goto fail;
792

793 794 795 796 797 798 799 800 801
	/* NOTE:  all that is done without knowing or caring about
	 * the network link ... which is unavailable to this code
	 * until we're activated via set_alt().
	 */

	ecm->port.open = ecm_open;
	ecm->port.close = ecm_close;

	DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
802
			gadget_is_superspeed(c->cdev->gadget) ? "super" :
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
			gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
			ecm->port.in_ep->name, ecm->port.out_ep->name,
			ecm->notify->name);
	return 0;

fail:
	if (ecm->notify_req) {
		kfree(ecm->notify_req->buf);
		usb_ep_free_request(ecm->notify, ecm->notify_req);
	}

	ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);

	return status;
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
{
	return container_of(to_config_group(item), struct f_ecm_opts,
			    func_inst.group);
}

/* f_ecm_item_ops */
USB_ETHERNET_CONFIGFS_ITEM(ecm);

/* f_ecm_opts_dev_addr */
USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);

/* f_ecm_opts_host_addr */
USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);

/* f_ecm_opts_qmult */
USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);

/* f_ecm_opts_ifname */
USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);

static struct configfs_attribute *ecm_attrs[] = {
841 842 843 844
	&ecm_opts_attr_dev_addr,
	&ecm_opts_attr_host_addr,
	&ecm_opts_attr_qmult,
	&ecm_opts_attr_ifname,
845 846 847 848 849 850 851 852 853
	NULL,
};

static struct config_item_type ecm_func_type = {
	.ct_item_ops	= &ecm_item_ops,
	.ct_attrs	= ecm_attrs,
	.ct_owner	= THIS_MODULE,
};

854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
static void ecm_free_inst(struct usb_function_instance *f)
{
	struct f_ecm_opts *opts;

	opts = container_of(f, struct f_ecm_opts, func_inst);
	if (opts->bound)
		gether_cleanup(netdev_priv(opts->net));
	else
		free_netdev(opts->net);
	kfree(opts);
}

static struct usb_function_instance *ecm_alloc_inst(void)
{
	struct f_ecm_opts *opts;

	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
	if (!opts)
		return ERR_PTR(-ENOMEM);
873
	mutex_init(&opts->lock);
874 875
	opts->func_inst.free_func_inst = ecm_free_inst;
	opts->net = gether_setup_default();
876 877 878 879 880
	if (IS_ERR(opts->net)) {
		struct net_device *net = opts->net;
		kfree(opts);
		return ERR_CAST(net);
	}
881

882 883
	config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);

884 885 886 887 888 889
	return &opts->func_inst;
}

static void ecm_free(struct usb_function *f)
{
	struct f_ecm *ecm;
890
	struct f_ecm_opts *opts;
891 892

	ecm = func_to_ecm(f);
893
	opts = container_of(f->fi, struct f_ecm_opts, func_inst);
894
	kfree(ecm);
895 896 897
	mutex_lock(&opts->lock);
	opts->refcnt--;
	mutex_unlock(&opts->lock);
898 899 900 901 902 903 904 905 906 907 908 909 910 911
}

static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
{
	struct f_ecm		*ecm = func_to_ecm(f);

	DBG(c->cdev, "ecm unbind\n");

	usb_free_all_descriptors(f);

	kfree(ecm->notify_req->buf);
	usb_ep_free_request(ecm->notify, ecm->notify_req);
}

912
static struct usb_function *ecm_alloc(struct usb_function_instance *fi)
913 914 915 916 917 918 919 920 921 922 923
{
	struct f_ecm	*ecm;
	struct f_ecm_opts *opts;
	int status;

	/* allocate and initialize one new instance */
	ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
	if (!ecm)
		return ERR_PTR(-ENOMEM);

	opts = container_of(fi, struct f_ecm_opts, func_inst);
924 925
	mutex_lock(&opts->lock);
	opts->refcnt++;
926 927 928 929 930 931

	/* export host's Ethernet address in CDC format */
	status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
					  sizeof(ecm->ethaddr));
	if (status < 12) {
		kfree(ecm);
932
		mutex_unlock(&opts->lock);
933 934 935 936 937
		return ERR_PTR(-EINVAL);
	}
	ecm_string_defs[1].s = ecm->ethaddr;

	ecm->port.ioport = netdev_priv(opts->net);
938
	mutex_unlock(&opts->lock);
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	ecm->port.cdc_filter = DEFAULT_FILTER;

	ecm->port.func.name = "cdc_ethernet";
	/* descriptors are per-instance copies */
	ecm->port.func.bind = ecm_bind;
	ecm->port.func.unbind = ecm_unbind;
	ecm->port.func.set_alt = ecm_set_alt;
	ecm->port.func.get_alt = ecm_get_alt;
	ecm->port.func.setup = ecm_setup;
	ecm->port.func.disable = ecm_disable;
	ecm->port.func.free_func = ecm_free;

	return &ecm->port.func;
}

DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Brownell");