Linux Audio

Check our new training course

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