Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
  3 *
  4 * Copyright (C) 2003-2005,2008 David Brownell
  5 * Copyright (C) 2008 Nokia Corporation
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation; either version 2 of the License, or
 10 * (at your option) any later version.
 11 */
 12
 13/* #define VERBOSE_DEBUG */
 14
 15#include <linux/slab.h>
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/device.h>
 19#include <linux/etherdevice.h>
 20
 21#include "u_ether.h"
 22#include "u_ether_configfs.h"
 23#include "u_ecm.h"
 24
 25
 26/*
 27 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
 28 * Ethernet link.  The data transfer model is simple (packets sent and
 29 * received over bulk endpoints using normal short packet termination),
 30 * and the control model exposes various data and optional notifications.
 31 *
 32 * ECM is well standardized and (except for Microsoft) supported by most
 33 * operating systems with USB host support.  It's the preferred interop
 34 * solution for Ethernet over USB, at least for firmware based solutions.
 35 * (Hardware solutions tend to be more minimalist.)  A newer and simpler
 36 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
 37 *
 38 * Note that ECM requires the use of "alternate settings" for its data
 39 * interface.  This means that the set_alt() method has real work to do,
 40 * and also means that a get_alt() method is required.
 41 */
 42
 43
 44enum ecm_notify_state {
 45	ECM_NOTIFY_NONE,		/* don't notify */
 46	ECM_NOTIFY_CONNECT,		/* issue CONNECT next */
 47	ECM_NOTIFY_SPEED,		/* issue SPEED_CHANGE next */
 48};
 49
 50struct f_ecm {
 51	struct gether			port;
 52	u8				ctrl_id, data_id;
 53
 54	char				ethaddr[14];
 55
 56	struct usb_ep			*notify;
 57	struct usb_request		*notify_req;
 58	u8				notify_state;
 59	bool				is_open;
 60
 61	/* FIXME is_open needs some irq-ish locking
 62	 * ... possibly the same as port.ioport
 63	 */
 64};
 65
 66static inline struct f_ecm *func_to_ecm(struct usb_function *f)
 67{
 68	return container_of(f, struct f_ecm, port.func);
 69}
 70
 71/* peak (theoretical) bulk transfer rate in bits-per-second */
 72static inline unsigned ecm_bitrate(struct usb_gadget *g)
 73{
 74	if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
 75		return 13 * 1024 * 8 * 1000 * 8;
 76	else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
 77		return 13 * 512 * 8 * 1000 * 8;
 78	else
 79		return 19 * 64 * 1 * 1000 * 8;
 80}
 81
 82/*-------------------------------------------------------------------------*/
 83
 84/*
 85 * Include the status endpoint if we can, even though it's optional.
 86 *
 87 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
 88 * packet, to simplify cancellation; and a big transfer interval, to
 89 * waste less bandwidth.
 90 *
 91 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
 92 * if they ignore the connect/disconnect notifications that real aether
 93 * can provide.  More advanced cdc configurations might want to support
 94 * encapsulated commands (vendor-specific, using control-OUT).
 95 */
 96
 97#define ECM_STATUS_INTERVAL_MS		32
 98#define ECM_STATUS_BYTECOUNT		16	/* 8 byte header + data */
 99
100
101/* interface descriptor: */
102
103static struct usb_interface_assoc_descriptor
104ecm_iad_descriptor = {
105	.bLength =		sizeof ecm_iad_descriptor,
106	.bDescriptorType =	USB_DT_INTERFACE_ASSOCIATION,
107
108	/* .bFirstInterface =	DYNAMIC, */
109	.bInterfaceCount =	2,	/* control + data */
110	.bFunctionClass =	USB_CLASS_COMM,
111	.bFunctionSubClass =	USB_CDC_SUBCLASS_ETHERNET,
112	.bFunctionProtocol =	USB_CDC_PROTO_NONE,
113	/* .iFunction =		DYNAMIC */
114};
115
116
117static struct usb_interface_descriptor ecm_control_intf = {
118	.bLength =		sizeof ecm_control_intf,
119	.bDescriptorType =	USB_DT_INTERFACE,
120
121	/* .bInterfaceNumber = DYNAMIC */
122	/* status endpoint is optional; this could be patched later */
123	.bNumEndpoints =	1,
124	.bInterfaceClass =	USB_CLASS_COMM,
125	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,
126	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,
127	/* .iInterface = DYNAMIC */
128};
129
130static struct usb_cdc_header_desc ecm_header_desc = {
131	.bLength =		sizeof ecm_header_desc,
132	.bDescriptorType =	USB_DT_CS_INTERFACE,
133	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
134
135	.bcdCDC =		cpu_to_le16(0x0110),
136};
137
138static struct usb_cdc_union_desc ecm_union_desc = {
139	.bLength =		sizeof(ecm_union_desc),
140	.bDescriptorType =	USB_DT_CS_INTERFACE,
141	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
142	/* .bMasterInterface0 =	DYNAMIC */
143	/* .bSlaveInterface0 =	DYNAMIC */
144};
145
146static struct usb_cdc_ether_desc ecm_desc = {
147	.bLength =		sizeof ecm_desc,
148	.bDescriptorType =	USB_DT_CS_INTERFACE,
149	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,
150
151	/* this descriptor actually adds value, surprise! */
152	/* .iMACAddress = DYNAMIC */
153	.bmEthernetStatistics =	cpu_to_le32(0), /* no statistics */
154	.wMaxSegmentSize =	cpu_to_le16(ETH_FRAME_LEN),
155	.wNumberMCFilters =	cpu_to_le16(0),
156	.bNumberPowerFilters =	0,
157};
158
159/* the default data interface has no endpoints ... */
160
161static struct usb_interface_descriptor ecm_data_nop_intf = {
162	.bLength =		sizeof ecm_data_nop_intf,
163	.bDescriptorType =	USB_DT_INTERFACE,
164
165	.bInterfaceNumber =	1,
166	.bAlternateSetting =	0,
167	.bNumEndpoints =	0,
168	.bInterfaceClass =	USB_CLASS_CDC_DATA,
169	.bInterfaceSubClass =	0,
170	.bInterfaceProtocol =	0,
171	/* .iInterface = DYNAMIC */
172};
173
174/* ... but the "real" data interface has two bulk endpoints */
175
176static struct usb_interface_descriptor ecm_data_intf = {
177	.bLength =		sizeof ecm_data_intf,
178	.bDescriptorType =	USB_DT_INTERFACE,
179
180	.bInterfaceNumber =	1,
181	.bAlternateSetting =	1,
182	.bNumEndpoints =	2,
183	.bInterfaceClass =	USB_CLASS_CDC_DATA,
184	.bInterfaceSubClass =	0,
185	.bInterfaceProtocol =	0,
186	/* .iInterface = DYNAMIC */
187};
188
189/* full speed support: */
190
191static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
192	.bLength =		USB_DT_ENDPOINT_SIZE,
193	.bDescriptorType =	USB_DT_ENDPOINT,
194
195	.bEndpointAddress =	USB_DIR_IN,
196	.bmAttributes =		USB_ENDPOINT_XFER_INT,
197	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
198	.bInterval =		ECM_STATUS_INTERVAL_MS,
199};
200
201static struct usb_endpoint_descriptor fs_ecm_in_desc = {
202	.bLength =		USB_DT_ENDPOINT_SIZE,
203	.bDescriptorType =	USB_DT_ENDPOINT,
204
205	.bEndpointAddress =	USB_DIR_IN,
206	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
207};
208
209static struct usb_endpoint_descriptor fs_ecm_out_desc = {
210	.bLength =		USB_DT_ENDPOINT_SIZE,
211	.bDescriptorType =	USB_DT_ENDPOINT,
212
213	.bEndpointAddress =	USB_DIR_OUT,
214	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
215};
216
217static 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	/* data interface, altsettings 0 and 1 */
229	(struct usb_descriptor_header *) &ecm_data_nop_intf,
230	(struct usb_descriptor_header *) &ecm_data_intf,
231	(struct usb_descriptor_header *) &fs_ecm_in_desc,
232	(struct usb_descriptor_header *) &fs_ecm_out_desc,
233	NULL,
234};
235
236/* high speed support: */
237
238static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
239	.bLength =		USB_DT_ENDPOINT_SIZE,
240	.bDescriptorType =	USB_DT_ENDPOINT,
241
242	.bEndpointAddress =	USB_DIR_IN,
243	.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
248static struct usb_endpoint_descriptor hs_ecm_in_desc = {
249	.bLength =		USB_DT_ENDPOINT_SIZE,
250	.bDescriptorType =	USB_DT_ENDPOINT,
251
252	.bEndpointAddress =	USB_DIR_IN,
253	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
254	.wMaxPacketSize =	cpu_to_le16(512),
255};
256
257static struct usb_endpoint_descriptor hs_ecm_out_desc = {
258	.bLength =		USB_DT_ENDPOINT_SIZE,
259	.bDescriptorType =	USB_DT_ENDPOINT,
260
261	.bEndpointAddress =	USB_DIR_OUT,
262	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
263	.wMaxPacketSize =	cpu_to_le16(512),
264};
265
266static 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	/* data interface, altsettings 0 and 1 */
278	(struct usb_descriptor_header *) &ecm_data_nop_intf,
279	(struct usb_descriptor_header *) &ecm_data_intf,
280	(struct usb_descriptor_header *) &hs_ecm_in_desc,
281	(struct usb_descriptor_header *) &hs_ecm_out_desc,
282	NULL,
283};
284
285/* super speed support: */
286
287static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
288	.bLength =		USB_DT_ENDPOINT_SIZE,
289	.bDescriptorType =	USB_DT_ENDPOINT,
290
291	.bEndpointAddress =	USB_DIR_IN,
292	.bmAttributes =		USB_ENDPOINT_XFER_INT,
293	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
294	.bInterval =		USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
295};
296
297static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
298	.bLength =		sizeof ss_ecm_intr_comp_desc,
299	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
300
301	/* the following 3 values can be tweaked if necessary */
302	/* .bMaxBurst =		0, */
303	/* .bmAttributes =	0, */
304	.wBytesPerInterval =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
305};
306
307static struct usb_endpoint_descriptor ss_ecm_in_desc = {
308	.bLength =		USB_DT_ENDPOINT_SIZE,
309	.bDescriptorType =	USB_DT_ENDPOINT,
310
311	.bEndpointAddress =	USB_DIR_IN,
312	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
313	.wMaxPacketSize =	cpu_to_le16(1024),
314};
315
316static struct usb_endpoint_descriptor ss_ecm_out_desc = {
317	.bLength =		USB_DT_ENDPOINT_SIZE,
318	.bDescriptorType =	USB_DT_ENDPOINT,
319
320	.bEndpointAddress =	USB_DIR_OUT,
321	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
322	.wMaxPacketSize =	cpu_to_le16(1024),
323};
324
325static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
326	.bLength =		sizeof ss_ecm_bulk_comp_desc,
327	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
328
329	/* the following 2 values can be tweaked if necessary */
330	/* .bMaxBurst =		0, */
331	/* .bmAttributes =	0, */
332};
333
334static struct usb_descriptor_header *ecm_ss_function[] = {
335	/* CDC ECM control descriptors */
336	(struct usb_descriptor_header *) &ecm_iad_descriptor,
337	(struct usb_descriptor_header *) &ecm_control_intf,
338	(struct usb_descriptor_header *) &ecm_header_desc,
339	(struct usb_descriptor_header *) &ecm_union_desc,
340	(struct usb_descriptor_header *) &ecm_desc,
341
342	/* NOTE: status endpoint might need to be removed */
343	(struct usb_descriptor_header *) &ss_ecm_notify_desc,
344	(struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
345
346	/* data interface, altsettings 0 and 1 */
347	(struct usb_descriptor_header *) &ecm_data_nop_intf,
348	(struct usb_descriptor_header *) &ecm_data_intf,
349	(struct usb_descriptor_header *) &ss_ecm_in_desc,
350	(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
351	(struct usb_descriptor_header *) &ss_ecm_out_desc,
352	(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
353	NULL,
354};
355
356/* string descriptors: */
357
358static struct usb_string ecm_string_defs[] = {
359	[0].s = "CDC Ethernet Control Model (ECM)",
360	[1].s = "",
361	[2].s = "CDC Ethernet Data",
362	[3].s = "CDC ECM",
363	{  } /* end of list */
364};
365
366static struct usb_gadget_strings ecm_string_table = {
367	.language =		0x0409,	/* en-us */
368	.strings =		ecm_string_defs,
369};
370
371static struct usb_gadget_strings *ecm_strings[] = {
372	&ecm_string_table,
373	NULL,
374};
375
376/*-------------------------------------------------------------------------*/
377
378static void ecm_do_notify(struct f_ecm *ecm)
379{
380	struct usb_request		*req = ecm->notify_req;
381	struct usb_cdc_notification	*event;
382	struct usb_composite_dev	*cdev = ecm->port.func.config->cdev;
383	__le32				*data;
384	int				status;
385
386	/* notification already in flight? */
387	if (!req)
388		return;
389
390	event = req->buf;
391	switch (ecm->notify_state) {
392	case ECM_NOTIFY_NONE:
393		return;
394
395	case ECM_NOTIFY_CONNECT:
396		event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
397		if (ecm->is_open)
398			event->wValue = cpu_to_le16(1);
399		else
400			event->wValue = cpu_to_le16(0);
401		event->wLength = 0;
402		req->length = sizeof *event;
403
404		DBG(cdev, "notify connect %s\n",
405				ecm->is_open ? "true" : "false");
406		ecm->notify_state = ECM_NOTIFY_SPEED;
407		break;
408
409	case ECM_NOTIFY_SPEED:
410		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
411		event->wValue = cpu_to_le16(0);
412		event->wLength = cpu_to_le16(8);
413		req->length = ECM_STATUS_BYTECOUNT;
414
415		/* SPEED_CHANGE data is up/down speeds in bits/sec */
416		data = req->buf + sizeof *event;
417		data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
418		data[1] = data[0];
419
420		DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
421		ecm->notify_state = ECM_NOTIFY_NONE;
422		break;
423	}
424	event->bmRequestType = 0xA1;
425	event->wIndex = cpu_to_le16(ecm->ctrl_id);
426
427	ecm->notify_req = NULL;
428	status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
429	if (status < 0) {
430		ecm->notify_req = req;
431		DBG(cdev, "notify --> %d\n", status);
432	}
433}
434
435static void ecm_notify(struct f_ecm *ecm)
436{
437	/* NOTE on most versions of Linux, host side cdc-ethernet
438	 * won't listen for notifications until its netdevice opens.
439	 * The first notification then sits in the FIFO for a long
440	 * time, and the second one is queued.
441	 */
442	ecm->notify_state = ECM_NOTIFY_CONNECT;
443	ecm_do_notify(ecm);
444}
445
446static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
447{
448	struct f_ecm			*ecm = req->context;
449	struct usb_composite_dev	*cdev = ecm->port.func.config->cdev;
450	struct usb_cdc_notification	*event = req->buf;
451
452	switch (req->status) {
453	case 0:
454		/* no fault */
455		break;
456	case -ECONNRESET:
457	case -ESHUTDOWN:
458		ecm->notify_state = ECM_NOTIFY_NONE;
459		break;
460	default:
461		DBG(cdev, "event %02x --> %d\n",
462			event->bNotificationType, req->status);
463		break;
464	}
465	ecm->notify_req = req;
466	ecm_do_notify(ecm);
467}
468
469static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
470{
471	struct f_ecm		*ecm = func_to_ecm(f);
472	struct usb_composite_dev *cdev = f->config->cdev;
473	struct usb_request	*req = cdev->req;
474	int			value = -EOPNOTSUPP;
475	u16			w_index = le16_to_cpu(ctrl->wIndex);
476	u16			w_value = le16_to_cpu(ctrl->wValue);
477	u16			w_length = le16_to_cpu(ctrl->wLength);
478
479	/* composite driver infrastructure handles everything except
480	 * CDC class messages; interface activation uses set_alt().
481	 */
482	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
483	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
484			| USB_CDC_SET_ETHERNET_PACKET_FILTER:
485		/* see 6.2.30: no data, wIndex = interface,
486		 * wValue = packet filter bitmap
487		 */
488		if (w_length != 0 || w_index != ecm->ctrl_id)
489			goto invalid;
490		DBG(cdev, "packet filter %02x\n", w_value);
491		/* REVISIT locking of cdc_filter.  This assumes the UDC
492		 * driver won't have a concurrent packet TX irq running on
493		 * another CPU; or that if it does, this write is atomic...
494		 */
495		ecm->port.cdc_filter = w_value;
496		value = 0;
497		break;
498
499	/* and optionally:
500	 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
501	 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
502	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
503	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
504	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
505	 * case USB_CDC_GET_ETHERNET_STATISTIC:
506	 */
507
508	default:
509invalid:
510		DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
511			ctrl->bRequestType, ctrl->bRequest,
512			w_value, w_index, w_length);
513	}
514
515	/* respond with data transfer or status phase? */
516	if (value >= 0) {
517		DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
518			ctrl->bRequestType, ctrl->bRequest,
519			w_value, w_index, w_length);
520		req->zero = 0;
521		req->length = value;
522		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
523		if (value < 0)
524			ERROR(cdev, "ecm req %02x.%02x response err %d\n",
525					ctrl->bRequestType, ctrl->bRequest,
526					value);
527	}
528
529	/* device either stalls (value < 0) or reports success */
530	return value;
531}
532
533
534static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
535{
536	struct f_ecm		*ecm = func_to_ecm(f);
537	struct usb_composite_dev *cdev = f->config->cdev;
538
539	/* Control interface has only altsetting 0 */
540	if (intf == ecm->ctrl_id) {
541		if (alt != 0)
542			goto fail;
543
544		VDBG(cdev, "reset ecm control %d\n", intf);
545		usb_ep_disable(ecm->notify);
546		if (!(ecm->notify->desc)) {
547			VDBG(cdev, "init ecm ctrl %d\n", intf);
548			if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
549				goto fail;
550		}
551		usb_ep_enable(ecm->notify);
552
553	/* Data interface has two altsettings, 0 and 1 */
554	} else if (intf == ecm->data_id) {
555		if (alt > 1)
556			goto fail;
557
558		if (ecm->port.in_ep->enabled) {
559			DBG(cdev, "reset ecm\n");
560			gether_disconnect(&ecm->port);
561		}
562
563		if (!ecm->port.in_ep->desc ||
564		    !ecm->port.out_ep->desc) {
565			DBG(cdev, "init ecm\n");
566			if (config_ep_by_speed(cdev->gadget, f,
567					       ecm->port.in_ep) ||
568			    config_ep_by_speed(cdev->gadget, f,
569					       ecm->port.out_ep)) {
570				ecm->port.in_ep->desc = NULL;
571				ecm->port.out_ep->desc = NULL;
572				goto fail;
573			}
574		}
575
576		/* CDC Ethernet only sends data in non-default altsettings.
577		 * Changing altsettings resets filters, statistics, etc.
578		 */
579		if (alt == 1) {
580			struct net_device	*net;
581
582			/* Enable zlps by default for ECM conformance;
583			 * override for musb_hdrc (avoids txdma ovhead).
584			 */
585			ecm->port.is_zlp_ok =
586				gadget_is_zlp_supported(cdev->gadget);
587			ecm->port.cdc_filter = DEFAULT_FILTER;
588			DBG(cdev, "activate ecm\n");
589			net = gether_connect(&ecm->port);
590			if (IS_ERR(net))
591				return PTR_ERR(net);
592		}
593
594		/* NOTE this can be a minor disagreement with the ECM spec,
595		 * which says speed notifications will "always" follow
596		 * connection notifications.  But we allow one connect to
597		 * follow another (if the first is in flight), and instead
598		 * just guarantee that a speed notification is always sent.
599		 */
600		ecm_notify(ecm);
601	} else
602		goto fail;
603
604	return 0;
605fail:
606	return -EINVAL;
607}
608
609/* Because the data interface supports multiple altsettings,
610 * this ECM function *MUST* implement a get_alt() method.
611 */
612static int ecm_get_alt(struct usb_function *f, unsigned intf)
613{
614	struct f_ecm		*ecm = func_to_ecm(f);
615
616	if (intf == ecm->ctrl_id)
617		return 0;
618	return ecm->port.in_ep->enabled ? 1 : 0;
619}
620
621static void ecm_disable(struct usb_function *f)
622{
623	struct f_ecm		*ecm = func_to_ecm(f);
624	struct usb_composite_dev *cdev = f->config->cdev;
625
626	DBG(cdev, "ecm deactivated\n");
627
628	if (ecm->port.in_ep->enabled)
629		gether_disconnect(&ecm->port);
630
631	usb_ep_disable(ecm->notify);
632	ecm->notify->desc = NULL;
633}
634
635/*-------------------------------------------------------------------------*/
636
637/*
638 * Callbacks let us notify the host about connect/disconnect when the
639 * net device is opened or closed.
640 *
641 * For testing, note that link states on this side include both opened
642 * and closed variants of:
643 *
644 *   - disconnected/unconfigured
645 *   - configured but inactive (data alt 0)
646 *   - configured and active (data alt 1)
647 *
648 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
649 * SET_INTERFACE (altsetting).  Remember also that "configured" doesn't
650 * imply the host is actually polling the notification endpoint, and
651 * likewise that "active" doesn't imply it's actually using the data
652 * endpoints for traffic.
653 */
654
655static void ecm_open(struct gether *geth)
656{
657	struct f_ecm		*ecm = func_to_ecm(&geth->func);
658
659	DBG(ecm->port.func.config->cdev, "%s\n", __func__);
660
661	ecm->is_open = true;
662	ecm_notify(ecm);
663}
664
665static void ecm_close(struct gether *geth)
666{
667	struct f_ecm		*ecm = func_to_ecm(&geth->func);
668
669	DBG(ecm->port.func.config->cdev, "%s\n", __func__);
670
671	ecm->is_open = false;
672	ecm_notify(ecm);
673}
674
675/*-------------------------------------------------------------------------*/
676
677/* ethernet function driver setup/binding */
678
679static int
680ecm_bind(struct usb_configuration *c, struct usb_function *f)
681{
682	struct usb_composite_dev *cdev = c->cdev;
683	struct f_ecm		*ecm = func_to_ecm(f);
684	struct usb_string	*us;
685	int			status;
686	struct usb_ep		*ep;
687
688	struct f_ecm_opts	*ecm_opts;
689
690	if (!can_support_ecm(cdev->gadget))
691		return -EINVAL;
692
693	ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
694
695	/*
696	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
697	 * configurations are bound in sequence with list_for_each_entry,
698	 * in each configuration its functions are bound in sequence
699	 * with list_for_each_entry, so we assume no race condition
700	 * with regard to ecm_opts->bound access
701	 */
702	if (!ecm_opts->bound) {
703		mutex_lock(&ecm_opts->lock);
704		gether_set_gadget(ecm_opts->net, cdev->gadget);
705		status = gether_register_netdev(ecm_opts->net);
706		mutex_unlock(&ecm_opts->lock);
707		if (status)
708			return status;
709		ecm_opts->bound = true;
710	}
711
712	us = usb_gstrings_attach(cdev, ecm_strings,
713				 ARRAY_SIZE(ecm_string_defs));
714	if (IS_ERR(us))
715		return PTR_ERR(us);
716	ecm_control_intf.iInterface = us[0].id;
717	ecm_data_intf.iInterface = us[2].id;
718	ecm_desc.iMACAddress = us[1].id;
719	ecm_iad_descriptor.iFunction = us[3].id;
720
721	/* allocate instance-specific interface IDs */
722	status = usb_interface_id(c, f);
723	if (status < 0)
724		goto fail;
725	ecm->ctrl_id = status;
726	ecm_iad_descriptor.bFirstInterface = status;
727
728	ecm_control_intf.bInterfaceNumber = status;
729	ecm_union_desc.bMasterInterface0 = status;
730
731	status = usb_interface_id(c, f);
732	if (status < 0)
733		goto fail;
734	ecm->data_id = status;
735
736	ecm_data_nop_intf.bInterfaceNumber = status;
737	ecm_data_intf.bInterfaceNumber = status;
738	ecm_union_desc.bSlaveInterface0 = status;
739
740	status = -ENODEV;
741
742	/* allocate instance-specific endpoints */
743	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
744	if (!ep)
745		goto fail;
746	ecm->port.in_ep = ep;
747
748	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
749	if (!ep)
750		goto fail;
751	ecm->port.out_ep = ep;
752
753	/* NOTE:  a status/notification endpoint is *OPTIONAL* but we
754	 * don't treat it that way.  It's simpler, and some newer CDC
755	 * profiles (wireless handsets) no longer treat it as optional.
756	 */
757	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
758	if (!ep)
759		goto fail;
760	ecm->notify = ep;
761
762	status = -ENOMEM;
763
764	/* allocate notification request and buffer */
765	ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
766	if (!ecm->notify_req)
767		goto fail;
768	ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
769	if (!ecm->notify_req->buf)
770		goto fail;
771	ecm->notify_req->context = ecm;
772	ecm->notify_req->complete = ecm_notify_complete;
773
774	/* support all relevant hardware speeds... we expect that when
775	 * hardware is dual speed, all bulk-capable endpoints work at
776	 * both speeds
777	 */
778	hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
779	hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
780	hs_ecm_notify_desc.bEndpointAddress =
781		fs_ecm_notify_desc.bEndpointAddress;
782
783	ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
784	ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
785	ss_ecm_notify_desc.bEndpointAddress =
786		fs_ecm_notify_desc.bEndpointAddress;
787
788	status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
789			ecm_ss_function, NULL);
790	if (status)
791		goto fail;
792
793	/* NOTE:  all that is done without knowing or caring about
794	 * the network link ... which is unavailable to this code
795	 * until we're activated via set_alt().
796	 */
797
798	ecm->port.open = ecm_open;
799	ecm->port.close = ecm_close;
800
801	DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
802			gadget_is_superspeed(c->cdev->gadget) ? "super" :
803			gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
804			ecm->port.in_ep->name, ecm->port.out_ep->name,
805			ecm->notify->name);
806	return 0;
807
808fail:
809	if (ecm->notify_req) {
810		kfree(ecm->notify_req->buf);
811		usb_ep_free_request(ecm->notify, ecm->notify_req);
812	}
813
814	ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
815
816	return status;
817}
818
819static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
820{
821	return container_of(to_config_group(item), struct f_ecm_opts,
822			    func_inst.group);
823}
824
825/* f_ecm_item_ops */
826USB_ETHERNET_CONFIGFS_ITEM(ecm);
827
828/* f_ecm_opts_dev_addr */
829USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);
830
831/* f_ecm_opts_host_addr */
832USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);
833
834/* f_ecm_opts_qmult */
835USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);
836
837/* f_ecm_opts_ifname */
838USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);
839
840static struct configfs_attribute *ecm_attrs[] = {
841	&ecm_opts_attr_dev_addr,
842	&ecm_opts_attr_host_addr,
843	&ecm_opts_attr_qmult,
844	&ecm_opts_attr_ifname,
845	NULL,
846};
847
848static struct config_item_type ecm_func_type = {
849	.ct_item_ops	= &ecm_item_ops,
850	.ct_attrs	= ecm_attrs,
851	.ct_owner	= THIS_MODULE,
852};
853
854static void ecm_free_inst(struct usb_function_instance *f)
855{
856	struct f_ecm_opts *opts;
857
858	opts = container_of(f, struct f_ecm_opts, func_inst);
859	if (opts->bound)
860		gether_cleanup(netdev_priv(opts->net));
861	else
862		free_netdev(opts->net);
863	kfree(opts);
864}
865
866static struct usb_function_instance *ecm_alloc_inst(void)
867{
868	struct f_ecm_opts *opts;
869
870	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
871	if (!opts)
872		return ERR_PTR(-ENOMEM);
873	mutex_init(&opts->lock);
874	opts->func_inst.free_func_inst = ecm_free_inst;
875	opts->net = gether_setup_default();
876	if (IS_ERR(opts->net)) {
877		struct net_device *net = opts->net;
878		kfree(opts);
879		return ERR_CAST(net);
880	}
881
882	config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);
883
884	return &opts->func_inst;
885}
886
887static void ecm_free(struct usb_function *f)
888{
889	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	mutex_lock(&opts->lock);
896	opts->refcnt--;
897	mutex_unlock(&opts->lock);
898}
899
900static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
901{
902	struct f_ecm		*ecm = func_to_ecm(f);
903
904	DBG(c->cdev, "ecm unbind\n");
905
906	usb_free_all_descriptors(f);
907
908	kfree(ecm->notify_req->buf);
909	usb_ep_free_request(ecm->notify, ecm->notify_req);
910}
911
912static struct usb_function *ecm_alloc(struct usb_function_instance *fi)
913{
914	struct f_ecm	*ecm;
915	struct f_ecm_opts *opts;
916	int status;
917
918	/* allocate and initialize one new instance */
919	ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
920	if (!ecm)
921		return ERR_PTR(-ENOMEM);
922
923	opts = container_of(fi, struct f_ecm_opts, func_inst);
924	mutex_lock(&opts->lock);
925	opts->refcnt++;
926
927	/* export host's Ethernet address in CDC format */
928	status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
929					  sizeof(ecm->ethaddr));
930	if (status < 12) {
931		kfree(ecm);
932		mutex_unlock(&opts->lock);
933		return ERR_PTR(-EINVAL);
934	}
935	ecm_string_defs[1].s = ecm->ethaddr;
936
937	ecm->port.ioport = netdev_priv(opts->net);
938	mutex_unlock(&opts->lock);
939	ecm->port.cdc_filter = DEFAULT_FILTER;
940
941	ecm->port.func.name = "cdc_ethernet";
942	/* descriptors are per-instance copies */
943	ecm->port.func.bind = ecm_bind;
944	ecm->port.func.unbind = ecm_unbind;
945	ecm->port.func.set_alt = ecm_set_alt;
946	ecm->port.func.get_alt = ecm_get_alt;
947	ecm->port.func.setup = ecm_setup;
948	ecm->port.func.disable = ecm_disable;
949	ecm->port.func.free_func = ecm_free;
950
951	return &ecm->port.func;
952}
953
954DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
955MODULE_LICENSE("GPL");
956MODULE_AUTHOR("David Brownell");
v4.17
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
  4 *
  5 * Copyright (C) 2003-2005,2008 David Brownell
  6 * Copyright (C) 2008 Nokia Corporation
 
 
 
 
 
  7 */
  8
  9/* #define VERBOSE_DEBUG */
 10
 11#include <linux/slab.h>
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14#include <linux/device.h>
 15#include <linux/etherdevice.h>
 16
 17#include "u_ether.h"
 18#include "u_ether_configfs.h"
 19#include "u_ecm.h"
 20
 21
 22/*
 23 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
 24 * Ethernet link.  The data transfer model is simple (packets sent and
 25 * received over bulk endpoints using normal short packet termination),
 26 * and the control model exposes various data and optional notifications.
 27 *
 28 * ECM is well standardized and (except for Microsoft) supported by most
 29 * operating systems with USB host support.  It's the preferred interop
 30 * solution for Ethernet over USB, at least for firmware based solutions.
 31 * (Hardware solutions tend to be more minimalist.)  A newer and simpler
 32 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
 33 *
 34 * Note that ECM requires the use of "alternate settings" for its data
 35 * interface.  This means that the set_alt() method has real work to do,
 36 * and also means that a get_alt() method is required.
 37 */
 38
 39
 40enum ecm_notify_state {
 41	ECM_NOTIFY_NONE,		/* don't notify */
 42	ECM_NOTIFY_CONNECT,		/* issue CONNECT next */
 43	ECM_NOTIFY_SPEED,		/* issue SPEED_CHANGE next */
 44};
 45
 46struct f_ecm {
 47	struct gether			port;
 48	u8				ctrl_id, data_id;
 49
 50	char				ethaddr[14];
 51
 52	struct usb_ep			*notify;
 53	struct usb_request		*notify_req;
 54	u8				notify_state;
 55	bool				is_open;
 56
 57	/* FIXME is_open needs some irq-ish locking
 58	 * ... possibly the same as port.ioport
 59	 */
 60};
 61
 62static inline struct f_ecm *func_to_ecm(struct usb_function *f)
 63{
 64	return container_of(f, struct f_ecm, port.func);
 65}
 66
 67/* peak (theoretical) bulk transfer rate in bits-per-second */
 68static inline unsigned ecm_bitrate(struct usb_gadget *g)
 69{
 70	if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
 71		return 13 * 1024 * 8 * 1000 * 8;
 72	else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
 73		return 13 * 512 * 8 * 1000 * 8;
 74	else
 75		return 19 * 64 * 1 * 1000 * 8;
 76}
 77
 78/*-------------------------------------------------------------------------*/
 79
 80/*
 81 * Include the status endpoint if we can, even though it's optional.
 82 *
 83 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
 84 * packet, to simplify cancellation; and a big transfer interval, to
 85 * waste less bandwidth.
 86 *
 87 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
 88 * if they ignore the connect/disconnect notifications that real aether
 89 * can provide.  More advanced cdc configurations might want to support
 90 * encapsulated commands (vendor-specific, using control-OUT).
 91 */
 92
 93#define ECM_STATUS_INTERVAL_MS		32
 94#define ECM_STATUS_BYTECOUNT		16	/* 8 byte header + data */
 95
 96
 97/* interface descriptor: */
 98
 99static struct usb_interface_assoc_descriptor
100ecm_iad_descriptor = {
101	.bLength =		sizeof ecm_iad_descriptor,
102	.bDescriptorType =	USB_DT_INTERFACE_ASSOCIATION,
103
104	/* .bFirstInterface =	DYNAMIC, */
105	.bInterfaceCount =	2,	/* control + data */
106	.bFunctionClass =	USB_CLASS_COMM,
107	.bFunctionSubClass =	USB_CDC_SUBCLASS_ETHERNET,
108	.bFunctionProtocol =	USB_CDC_PROTO_NONE,
109	/* .iFunction =		DYNAMIC */
110};
111
112
113static struct usb_interface_descriptor ecm_control_intf = {
114	.bLength =		sizeof ecm_control_intf,
115	.bDescriptorType =	USB_DT_INTERFACE,
116
117	/* .bInterfaceNumber = DYNAMIC */
118	/* status endpoint is optional; this could be patched later */
119	.bNumEndpoints =	1,
120	.bInterfaceClass =	USB_CLASS_COMM,
121	.bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET,
122	.bInterfaceProtocol =	USB_CDC_PROTO_NONE,
123	/* .iInterface = DYNAMIC */
124};
125
126static struct usb_cdc_header_desc ecm_header_desc = {
127	.bLength =		sizeof ecm_header_desc,
128	.bDescriptorType =	USB_DT_CS_INTERFACE,
129	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
130
131	.bcdCDC =		cpu_to_le16(0x0110),
132};
133
134static struct usb_cdc_union_desc ecm_union_desc = {
135	.bLength =		sizeof(ecm_union_desc),
136	.bDescriptorType =	USB_DT_CS_INTERFACE,
137	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
138	/* .bMasterInterface0 =	DYNAMIC */
139	/* .bSlaveInterface0 =	DYNAMIC */
140};
141
142static struct usb_cdc_ether_desc ecm_desc = {
143	.bLength =		sizeof ecm_desc,
144	.bDescriptorType =	USB_DT_CS_INTERFACE,
145	.bDescriptorSubType =	USB_CDC_ETHERNET_TYPE,
146
147	/* this descriptor actually adds value, surprise! */
148	/* .iMACAddress = DYNAMIC */
149	.bmEthernetStatistics =	cpu_to_le32(0), /* no statistics */
150	.wMaxSegmentSize =	cpu_to_le16(ETH_FRAME_LEN),
151	.wNumberMCFilters =	cpu_to_le16(0),
152	.bNumberPowerFilters =	0,
153};
154
155/* the default data interface has no endpoints ... */
156
157static struct usb_interface_descriptor ecm_data_nop_intf = {
158	.bLength =		sizeof ecm_data_nop_intf,
159	.bDescriptorType =	USB_DT_INTERFACE,
160
161	.bInterfaceNumber =	1,
162	.bAlternateSetting =	0,
163	.bNumEndpoints =	0,
164	.bInterfaceClass =	USB_CLASS_CDC_DATA,
165	.bInterfaceSubClass =	0,
166	.bInterfaceProtocol =	0,
167	/* .iInterface = DYNAMIC */
168};
169
170/* ... but the "real" data interface has two bulk endpoints */
171
172static struct usb_interface_descriptor ecm_data_intf = {
173	.bLength =		sizeof ecm_data_intf,
174	.bDescriptorType =	USB_DT_INTERFACE,
175
176	.bInterfaceNumber =	1,
177	.bAlternateSetting =	1,
178	.bNumEndpoints =	2,
179	.bInterfaceClass =	USB_CLASS_CDC_DATA,
180	.bInterfaceSubClass =	0,
181	.bInterfaceProtocol =	0,
182	/* .iInterface = DYNAMIC */
183};
184
185/* full speed support: */
186
187static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
188	.bLength =		USB_DT_ENDPOINT_SIZE,
189	.bDescriptorType =	USB_DT_ENDPOINT,
190
191	.bEndpointAddress =	USB_DIR_IN,
192	.bmAttributes =		USB_ENDPOINT_XFER_INT,
193	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
194	.bInterval =		ECM_STATUS_INTERVAL_MS,
195};
196
197static struct usb_endpoint_descriptor fs_ecm_in_desc = {
198	.bLength =		USB_DT_ENDPOINT_SIZE,
199	.bDescriptorType =	USB_DT_ENDPOINT,
200
201	.bEndpointAddress =	USB_DIR_IN,
202	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
203};
204
205static struct usb_endpoint_descriptor fs_ecm_out_desc = {
206	.bLength =		USB_DT_ENDPOINT_SIZE,
207	.bDescriptorType =	USB_DT_ENDPOINT,
208
209	.bEndpointAddress =	USB_DIR_OUT,
210	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
211};
212
213static struct usb_descriptor_header *ecm_fs_function[] = {
214	/* CDC ECM control descriptors */
215	(struct usb_descriptor_header *) &ecm_iad_descriptor,
216	(struct usb_descriptor_header *) &ecm_control_intf,
217	(struct usb_descriptor_header *) &ecm_header_desc,
218	(struct usb_descriptor_header *) &ecm_union_desc,
219	(struct usb_descriptor_header *) &ecm_desc,
220
221	/* NOTE: status endpoint might need to be removed */
222	(struct usb_descriptor_header *) &fs_ecm_notify_desc,
223
224	/* data interface, altsettings 0 and 1 */
225	(struct usb_descriptor_header *) &ecm_data_nop_intf,
226	(struct usb_descriptor_header *) &ecm_data_intf,
227	(struct usb_descriptor_header *) &fs_ecm_in_desc,
228	(struct usb_descriptor_header *) &fs_ecm_out_desc,
229	NULL,
230};
231
232/* high speed support: */
233
234static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
235	.bLength =		USB_DT_ENDPOINT_SIZE,
236	.bDescriptorType =	USB_DT_ENDPOINT,
237
238	.bEndpointAddress =	USB_DIR_IN,
239	.bmAttributes =		USB_ENDPOINT_XFER_INT,
240	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
241	.bInterval =		USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
242};
243
244static struct usb_endpoint_descriptor hs_ecm_in_desc = {
245	.bLength =		USB_DT_ENDPOINT_SIZE,
246	.bDescriptorType =	USB_DT_ENDPOINT,
247
248	.bEndpointAddress =	USB_DIR_IN,
249	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
250	.wMaxPacketSize =	cpu_to_le16(512),
251};
252
253static struct usb_endpoint_descriptor hs_ecm_out_desc = {
254	.bLength =		USB_DT_ENDPOINT_SIZE,
255	.bDescriptorType =	USB_DT_ENDPOINT,
256
257	.bEndpointAddress =	USB_DIR_OUT,
258	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
259	.wMaxPacketSize =	cpu_to_le16(512),
260};
261
262static struct usb_descriptor_header *ecm_hs_function[] = {
263	/* CDC ECM control descriptors */
264	(struct usb_descriptor_header *) &ecm_iad_descriptor,
265	(struct usb_descriptor_header *) &ecm_control_intf,
266	(struct usb_descriptor_header *) &ecm_header_desc,
267	(struct usb_descriptor_header *) &ecm_union_desc,
268	(struct usb_descriptor_header *) &ecm_desc,
269
270	/* NOTE: status endpoint might need to be removed */
271	(struct usb_descriptor_header *) &hs_ecm_notify_desc,
272
273	/* data interface, altsettings 0 and 1 */
274	(struct usb_descriptor_header *) &ecm_data_nop_intf,
275	(struct usb_descriptor_header *) &ecm_data_intf,
276	(struct usb_descriptor_header *) &hs_ecm_in_desc,
277	(struct usb_descriptor_header *) &hs_ecm_out_desc,
278	NULL,
279};
280
281/* super speed support: */
282
283static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
284	.bLength =		USB_DT_ENDPOINT_SIZE,
285	.bDescriptorType =	USB_DT_ENDPOINT,
286
287	.bEndpointAddress =	USB_DIR_IN,
288	.bmAttributes =		USB_ENDPOINT_XFER_INT,
289	.wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
290	.bInterval =		USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
291};
292
293static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
294	.bLength =		sizeof ss_ecm_intr_comp_desc,
295	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
296
297	/* the following 3 values can be tweaked if necessary */
298	/* .bMaxBurst =		0, */
299	/* .bmAttributes =	0, */
300	.wBytesPerInterval =	cpu_to_le16(ECM_STATUS_BYTECOUNT),
301};
302
303static struct usb_endpoint_descriptor ss_ecm_in_desc = {
304	.bLength =		USB_DT_ENDPOINT_SIZE,
305	.bDescriptorType =	USB_DT_ENDPOINT,
306
307	.bEndpointAddress =	USB_DIR_IN,
308	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
309	.wMaxPacketSize =	cpu_to_le16(1024),
310};
311
312static struct usb_endpoint_descriptor ss_ecm_out_desc = {
313	.bLength =		USB_DT_ENDPOINT_SIZE,
314	.bDescriptorType =	USB_DT_ENDPOINT,
315
316	.bEndpointAddress =	USB_DIR_OUT,
317	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
318	.wMaxPacketSize =	cpu_to_le16(1024),
319};
320
321static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
322	.bLength =		sizeof ss_ecm_bulk_comp_desc,
323	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
324
325	/* the following 2 values can be tweaked if necessary */
326	/* .bMaxBurst =		0, */
327	/* .bmAttributes =	0, */
328};
329
330static struct usb_descriptor_header *ecm_ss_function[] = {
331	/* CDC ECM control descriptors */
332	(struct usb_descriptor_header *) &ecm_iad_descriptor,
333	(struct usb_descriptor_header *) &ecm_control_intf,
334	(struct usb_descriptor_header *) &ecm_header_desc,
335	(struct usb_descriptor_header *) &ecm_union_desc,
336	(struct usb_descriptor_header *) &ecm_desc,
337
338	/* NOTE: status endpoint might need to be removed */
339	(struct usb_descriptor_header *) &ss_ecm_notify_desc,
340	(struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
341
342	/* data interface, altsettings 0 and 1 */
343	(struct usb_descriptor_header *) &ecm_data_nop_intf,
344	(struct usb_descriptor_header *) &ecm_data_intf,
345	(struct usb_descriptor_header *) &ss_ecm_in_desc,
346	(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
347	(struct usb_descriptor_header *) &ss_ecm_out_desc,
348	(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
349	NULL,
350};
351
352/* string descriptors: */
353
354static struct usb_string ecm_string_defs[] = {
355	[0].s = "CDC Ethernet Control Model (ECM)",
356	[1].s = "",
357	[2].s = "CDC Ethernet Data",
358	[3].s = "CDC ECM",
359	{  } /* end of list */
360};
361
362static struct usb_gadget_strings ecm_string_table = {
363	.language =		0x0409,	/* en-us */
364	.strings =		ecm_string_defs,
365};
366
367static struct usb_gadget_strings *ecm_strings[] = {
368	&ecm_string_table,
369	NULL,
370};
371
372/*-------------------------------------------------------------------------*/
373
374static void ecm_do_notify(struct f_ecm *ecm)
375{
376	struct usb_request		*req = ecm->notify_req;
377	struct usb_cdc_notification	*event;
378	struct usb_composite_dev	*cdev = ecm->port.func.config->cdev;
379	__le32				*data;
380	int				status;
381
382	/* notification already in flight? */
383	if (!req)
384		return;
385
386	event = req->buf;
387	switch (ecm->notify_state) {
388	case ECM_NOTIFY_NONE:
389		return;
390
391	case ECM_NOTIFY_CONNECT:
392		event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
393		if (ecm->is_open)
394			event->wValue = cpu_to_le16(1);
395		else
396			event->wValue = cpu_to_le16(0);
397		event->wLength = 0;
398		req->length = sizeof *event;
399
400		DBG(cdev, "notify connect %s\n",
401				ecm->is_open ? "true" : "false");
402		ecm->notify_state = ECM_NOTIFY_SPEED;
403		break;
404
405	case ECM_NOTIFY_SPEED:
406		event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
407		event->wValue = cpu_to_le16(0);
408		event->wLength = cpu_to_le16(8);
409		req->length = ECM_STATUS_BYTECOUNT;
410
411		/* SPEED_CHANGE data is up/down speeds in bits/sec */
412		data = req->buf + sizeof *event;
413		data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
414		data[1] = data[0];
415
416		DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
417		ecm->notify_state = ECM_NOTIFY_NONE;
418		break;
419	}
420	event->bmRequestType = 0xA1;
421	event->wIndex = cpu_to_le16(ecm->ctrl_id);
422
423	ecm->notify_req = NULL;
424	status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
425	if (status < 0) {
426		ecm->notify_req = req;
427		DBG(cdev, "notify --> %d\n", status);
428	}
429}
430
431static void ecm_notify(struct f_ecm *ecm)
432{
433	/* NOTE on most versions of Linux, host side cdc-ethernet
434	 * won't listen for notifications until its netdevice opens.
435	 * The first notification then sits in the FIFO for a long
436	 * time, and the second one is queued.
437	 */
438	ecm->notify_state = ECM_NOTIFY_CONNECT;
439	ecm_do_notify(ecm);
440}
441
442static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
443{
444	struct f_ecm			*ecm = req->context;
445	struct usb_composite_dev	*cdev = ecm->port.func.config->cdev;
446	struct usb_cdc_notification	*event = req->buf;
447
448	switch (req->status) {
449	case 0:
450		/* no fault */
451		break;
452	case -ECONNRESET:
453	case -ESHUTDOWN:
454		ecm->notify_state = ECM_NOTIFY_NONE;
455		break;
456	default:
457		DBG(cdev, "event %02x --> %d\n",
458			event->bNotificationType, req->status);
459		break;
460	}
461	ecm->notify_req = req;
462	ecm_do_notify(ecm);
463}
464
465static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
466{
467	struct f_ecm		*ecm = func_to_ecm(f);
468	struct usb_composite_dev *cdev = f->config->cdev;
469	struct usb_request	*req = cdev->req;
470	int			value = -EOPNOTSUPP;
471	u16			w_index = le16_to_cpu(ctrl->wIndex);
472	u16			w_value = le16_to_cpu(ctrl->wValue);
473	u16			w_length = le16_to_cpu(ctrl->wLength);
474
475	/* composite driver infrastructure handles everything except
476	 * CDC class messages; interface activation uses set_alt().
477	 */
478	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
479	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
480			| USB_CDC_SET_ETHERNET_PACKET_FILTER:
481		/* see 6.2.30: no data, wIndex = interface,
482		 * wValue = packet filter bitmap
483		 */
484		if (w_length != 0 || w_index != ecm->ctrl_id)
485			goto invalid;
486		DBG(cdev, "packet filter %02x\n", w_value);
487		/* REVISIT locking of cdc_filter.  This assumes the UDC
488		 * driver won't have a concurrent packet TX irq running on
489		 * another CPU; or that if it does, this write is atomic...
490		 */
491		ecm->port.cdc_filter = w_value;
492		value = 0;
493		break;
494
495	/* and optionally:
496	 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
497	 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
498	 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
499	 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
500	 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
501	 * case USB_CDC_GET_ETHERNET_STATISTIC:
502	 */
503
504	default:
505invalid:
506		DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
507			ctrl->bRequestType, ctrl->bRequest,
508			w_value, w_index, w_length);
509	}
510
511	/* respond with data transfer or status phase? */
512	if (value >= 0) {
513		DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
514			ctrl->bRequestType, ctrl->bRequest,
515			w_value, w_index, w_length);
516		req->zero = 0;
517		req->length = value;
518		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
519		if (value < 0)
520			ERROR(cdev, "ecm req %02x.%02x response err %d\n",
521					ctrl->bRequestType, ctrl->bRequest,
522					value);
523	}
524
525	/* device either stalls (value < 0) or reports success */
526	return value;
527}
528
529
530static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
531{
532	struct f_ecm		*ecm = func_to_ecm(f);
533	struct usb_composite_dev *cdev = f->config->cdev;
534
535	/* Control interface has only altsetting 0 */
536	if (intf == ecm->ctrl_id) {
537		if (alt != 0)
538			goto fail;
539
540		VDBG(cdev, "reset ecm control %d\n", intf);
541		usb_ep_disable(ecm->notify);
542		if (!(ecm->notify->desc)) {
543			VDBG(cdev, "init ecm ctrl %d\n", intf);
544			if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
545				goto fail;
546		}
547		usb_ep_enable(ecm->notify);
548
549	/* Data interface has two altsettings, 0 and 1 */
550	} else if (intf == ecm->data_id) {
551		if (alt > 1)
552			goto fail;
553
554		if (ecm->port.in_ep->enabled) {
555			DBG(cdev, "reset ecm\n");
556			gether_disconnect(&ecm->port);
557		}
558
559		if (!ecm->port.in_ep->desc ||
560		    !ecm->port.out_ep->desc) {
561			DBG(cdev, "init ecm\n");
562			if (config_ep_by_speed(cdev->gadget, f,
563					       ecm->port.in_ep) ||
564			    config_ep_by_speed(cdev->gadget, f,
565					       ecm->port.out_ep)) {
566				ecm->port.in_ep->desc = NULL;
567				ecm->port.out_ep->desc = NULL;
568				goto fail;
569			}
570		}
571
572		/* CDC Ethernet only sends data in non-default altsettings.
573		 * Changing altsettings resets filters, statistics, etc.
574		 */
575		if (alt == 1) {
576			struct net_device	*net;
577
578			/* Enable zlps by default for ECM conformance;
579			 * override for musb_hdrc (avoids txdma ovhead).
580			 */
581			ecm->port.is_zlp_ok =
582				gadget_is_zlp_supported(cdev->gadget);
583			ecm->port.cdc_filter = DEFAULT_FILTER;
584			DBG(cdev, "activate ecm\n");
585			net = gether_connect(&ecm->port);
586			if (IS_ERR(net))
587				return PTR_ERR(net);
588		}
589
590		/* NOTE this can be a minor disagreement with the ECM spec,
591		 * which says speed notifications will "always" follow
592		 * connection notifications.  But we allow one connect to
593		 * follow another (if the first is in flight), and instead
594		 * just guarantee that a speed notification is always sent.
595		 */
596		ecm_notify(ecm);
597	} else
598		goto fail;
599
600	return 0;
601fail:
602	return -EINVAL;
603}
604
605/* Because the data interface supports multiple altsettings,
606 * this ECM function *MUST* implement a get_alt() method.
607 */
608static int ecm_get_alt(struct usb_function *f, unsigned intf)
609{
610	struct f_ecm		*ecm = func_to_ecm(f);
611
612	if (intf == ecm->ctrl_id)
613		return 0;
614	return ecm->port.in_ep->enabled ? 1 : 0;
615}
616
617static void ecm_disable(struct usb_function *f)
618{
619	struct f_ecm		*ecm = func_to_ecm(f);
620	struct usb_composite_dev *cdev = f->config->cdev;
621
622	DBG(cdev, "ecm deactivated\n");
623
624	if (ecm->port.in_ep->enabled)
625		gether_disconnect(&ecm->port);
626
627	usb_ep_disable(ecm->notify);
628	ecm->notify->desc = NULL;
629}
630
631/*-------------------------------------------------------------------------*/
632
633/*
634 * Callbacks let us notify the host about connect/disconnect when the
635 * net device is opened or closed.
636 *
637 * For testing, note that link states on this side include both opened
638 * and closed variants of:
639 *
640 *   - disconnected/unconfigured
641 *   - configured but inactive (data alt 0)
642 *   - configured and active (data alt 1)
643 *
644 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
645 * SET_INTERFACE (altsetting).  Remember also that "configured" doesn't
646 * imply the host is actually polling the notification endpoint, and
647 * likewise that "active" doesn't imply it's actually using the data
648 * endpoints for traffic.
649 */
650
651static void ecm_open(struct gether *geth)
652{
653	struct f_ecm		*ecm = func_to_ecm(&geth->func);
654
655	DBG(ecm->port.func.config->cdev, "%s\n", __func__);
656
657	ecm->is_open = true;
658	ecm_notify(ecm);
659}
660
661static void ecm_close(struct gether *geth)
662{
663	struct f_ecm		*ecm = func_to_ecm(&geth->func);
664
665	DBG(ecm->port.func.config->cdev, "%s\n", __func__);
666
667	ecm->is_open = false;
668	ecm_notify(ecm);
669}
670
671/*-------------------------------------------------------------------------*/
672
673/* ethernet function driver setup/binding */
674
675static int
676ecm_bind(struct usb_configuration *c, struct usb_function *f)
677{
678	struct usb_composite_dev *cdev = c->cdev;
679	struct f_ecm		*ecm = func_to_ecm(f);
680	struct usb_string	*us;
681	int			status;
682	struct usb_ep		*ep;
683
684	struct f_ecm_opts	*ecm_opts;
685
686	if (!can_support_ecm(cdev->gadget))
687		return -EINVAL;
688
689	ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
690
691	/*
692	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
693	 * configurations are bound in sequence with list_for_each_entry,
694	 * in each configuration its functions are bound in sequence
695	 * with list_for_each_entry, so we assume no race condition
696	 * with regard to ecm_opts->bound access
697	 */
698	if (!ecm_opts->bound) {
699		mutex_lock(&ecm_opts->lock);
700		gether_set_gadget(ecm_opts->net, cdev->gadget);
701		status = gether_register_netdev(ecm_opts->net);
702		mutex_unlock(&ecm_opts->lock);
703		if (status)
704			return status;
705		ecm_opts->bound = true;
706	}
707
708	us = usb_gstrings_attach(cdev, ecm_strings,
709				 ARRAY_SIZE(ecm_string_defs));
710	if (IS_ERR(us))
711		return PTR_ERR(us);
712	ecm_control_intf.iInterface = us[0].id;
713	ecm_data_intf.iInterface = us[2].id;
714	ecm_desc.iMACAddress = us[1].id;
715	ecm_iad_descriptor.iFunction = us[3].id;
716
717	/* allocate instance-specific interface IDs */
718	status = usb_interface_id(c, f);
719	if (status < 0)
720		goto fail;
721	ecm->ctrl_id = status;
722	ecm_iad_descriptor.bFirstInterface = status;
723
724	ecm_control_intf.bInterfaceNumber = status;
725	ecm_union_desc.bMasterInterface0 = status;
726
727	status = usb_interface_id(c, f);
728	if (status < 0)
729		goto fail;
730	ecm->data_id = status;
731
732	ecm_data_nop_intf.bInterfaceNumber = status;
733	ecm_data_intf.bInterfaceNumber = status;
734	ecm_union_desc.bSlaveInterface0 = status;
735
736	status = -ENODEV;
737
738	/* allocate instance-specific endpoints */
739	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
740	if (!ep)
741		goto fail;
742	ecm->port.in_ep = ep;
743
744	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
745	if (!ep)
746		goto fail;
747	ecm->port.out_ep = ep;
748
749	/* NOTE:  a status/notification endpoint is *OPTIONAL* but we
750	 * don't treat it that way.  It's simpler, and some newer CDC
751	 * profiles (wireless handsets) no longer treat it as optional.
752	 */
753	ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
754	if (!ep)
755		goto fail;
756	ecm->notify = ep;
757
758	status = -ENOMEM;
759
760	/* allocate notification request and buffer */
761	ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
762	if (!ecm->notify_req)
763		goto fail;
764	ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
765	if (!ecm->notify_req->buf)
766		goto fail;
767	ecm->notify_req->context = ecm;
768	ecm->notify_req->complete = ecm_notify_complete;
769
770	/* support all relevant hardware speeds... we expect that when
771	 * hardware is dual speed, all bulk-capable endpoints work at
772	 * both speeds
773	 */
774	hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
775	hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
776	hs_ecm_notify_desc.bEndpointAddress =
777		fs_ecm_notify_desc.bEndpointAddress;
778
779	ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
780	ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
781	ss_ecm_notify_desc.bEndpointAddress =
782		fs_ecm_notify_desc.bEndpointAddress;
783
784	status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
785			ecm_ss_function, NULL);
786	if (status)
787		goto fail;
788
789	/* NOTE:  all that is done without knowing or caring about
790	 * the network link ... which is unavailable to this code
791	 * until we're activated via set_alt().
792	 */
793
794	ecm->port.open = ecm_open;
795	ecm->port.close = ecm_close;
796
797	DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
798			gadget_is_superspeed(c->cdev->gadget) ? "super" :
799			gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
800			ecm->port.in_ep->name, ecm->port.out_ep->name,
801			ecm->notify->name);
802	return 0;
803
804fail:
805	if (ecm->notify_req) {
806		kfree(ecm->notify_req->buf);
807		usb_ep_free_request(ecm->notify, ecm->notify_req);
808	}
809
810	ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
811
812	return status;
813}
814
815static inline struct f_ecm_opts *to_f_ecm_opts(struct config_item *item)
816{
817	return container_of(to_config_group(item), struct f_ecm_opts,
818			    func_inst.group);
819}
820
821/* f_ecm_item_ops */
822USB_ETHERNET_CONFIGFS_ITEM(ecm);
823
824/* f_ecm_opts_dev_addr */
825USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(ecm);
826
827/* f_ecm_opts_host_addr */
828USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(ecm);
829
830/* f_ecm_opts_qmult */
831USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(ecm);
832
833/* f_ecm_opts_ifname */
834USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(ecm);
835
836static struct configfs_attribute *ecm_attrs[] = {
837	&ecm_opts_attr_dev_addr,
838	&ecm_opts_attr_host_addr,
839	&ecm_opts_attr_qmult,
840	&ecm_opts_attr_ifname,
841	NULL,
842};
843
844static const struct config_item_type ecm_func_type = {
845	.ct_item_ops	= &ecm_item_ops,
846	.ct_attrs	= ecm_attrs,
847	.ct_owner	= THIS_MODULE,
848};
849
850static void ecm_free_inst(struct usb_function_instance *f)
851{
852	struct f_ecm_opts *opts;
853
854	opts = container_of(f, struct f_ecm_opts, func_inst);
855	if (opts->bound)
856		gether_cleanup(netdev_priv(opts->net));
857	else
858		free_netdev(opts->net);
859	kfree(opts);
860}
861
862static struct usb_function_instance *ecm_alloc_inst(void)
863{
864	struct f_ecm_opts *opts;
865
866	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
867	if (!opts)
868		return ERR_PTR(-ENOMEM);
869	mutex_init(&opts->lock);
870	opts->func_inst.free_func_inst = ecm_free_inst;
871	opts->net = gether_setup_default();
872	if (IS_ERR(opts->net)) {
873		struct net_device *net = opts->net;
874		kfree(opts);
875		return ERR_CAST(net);
876	}
877
878	config_group_init_type_name(&opts->func_inst.group, "", &ecm_func_type);
879
880	return &opts->func_inst;
881}
882
883static void ecm_free(struct usb_function *f)
884{
885	struct f_ecm *ecm;
886	struct f_ecm_opts *opts;
887
888	ecm = func_to_ecm(f);
889	opts = container_of(f->fi, struct f_ecm_opts, func_inst);
890	kfree(ecm);
891	mutex_lock(&opts->lock);
892	opts->refcnt--;
893	mutex_unlock(&opts->lock);
894}
895
896static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
897{
898	struct f_ecm		*ecm = func_to_ecm(f);
899
900	DBG(c->cdev, "ecm unbind\n");
901
902	usb_free_all_descriptors(f);
903
904	kfree(ecm->notify_req->buf);
905	usb_ep_free_request(ecm->notify, ecm->notify_req);
906}
907
908static struct usb_function *ecm_alloc(struct usb_function_instance *fi)
909{
910	struct f_ecm	*ecm;
911	struct f_ecm_opts *opts;
912	int status;
913
914	/* allocate and initialize one new instance */
915	ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
916	if (!ecm)
917		return ERR_PTR(-ENOMEM);
918
919	opts = container_of(fi, struct f_ecm_opts, func_inst);
920	mutex_lock(&opts->lock);
921	opts->refcnt++;
922
923	/* export host's Ethernet address in CDC format */
924	status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
925					  sizeof(ecm->ethaddr));
926	if (status < 12) {
927		kfree(ecm);
928		mutex_unlock(&opts->lock);
929		return ERR_PTR(-EINVAL);
930	}
931	ecm_string_defs[1].s = ecm->ethaddr;
932
933	ecm->port.ioport = netdev_priv(opts->net);
934	mutex_unlock(&opts->lock);
935	ecm->port.cdc_filter = DEFAULT_FILTER;
936
937	ecm->port.func.name = "cdc_ethernet";
938	/* descriptors are per-instance copies */
939	ecm->port.func.bind = ecm_bind;
940	ecm->port.func.unbind = ecm_unbind;
941	ecm->port.func.set_alt = ecm_set_alt;
942	ecm->port.func.get_alt = ecm_get_alt;
943	ecm->port.func.setup = ecm_setup;
944	ecm->port.func.disable = ecm_disable;
945	ecm->port.func.free_func = ecm_free;
946
947	return &ecm->port.func;
948}
949
950DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
951MODULE_LICENSE("GPL");
952MODULE_AUTHOR("David Brownell");