Linux Audio

Check our new training course

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