PageRenderTime 113ms CodeModel.GetById 39ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/usb/gadget/f_rndis.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 933 lines | 582 code | 162 blank | 189 comment | 77 complexity | 5406940dda1f4480ba18a9e71f607e62 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1/*
  2 * f_rndis.c -- RNDIS link function driver
  3 *
  4 * Copyright (C) 2003-2005,2008 David Brownell
  5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  6 * Copyright (C) 2008 Nokia Corporation
  7 * Copyright (C) 2009 Samsung Electronics
  8 *                    Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/* #define VERBOSE_DEBUG */
 26
 27#include <linux/slab.h>
 28#include <linux/kernel.h>
 29#include <linux/platform_device.h>
 30#include <linux/etherdevice.h>
 31
 32#include <linux/atomic.h>
 33
 34#include "u_ether.h"
 35#include "rndis.h"
 36
 37
 38/*
 39 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
 40 * been promoted instead of the standard CDC Ethernet.  The published RNDIS
 41 * spec is ambiguous, incomplete, and needlessly complex.  Variants such as
 42 * ActiveSync have even worse status in terms of specification.
 43 *
 44 * In short:  it's a protocol controlled by (and for) Microsoft, not for an
 45 * Open ecosystem or markets.  Linux supports it *only* because Microsoft
 46 * doesn't support the CDC Ethernet standard.
 47 *
 48 * The RNDIS data transfer model is complex, with multiple Ethernet packets
 49 * per USB message, and out of band data.  The control model is built around
 50 * what's essentially an "RNDIS RPC" protocol.  It's all wrapped in a CDC ACM
 51 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
 52 * useless (they're ignored).  RNDIS expects to be the only function in its
 53 * configuration, so it's no real help if you need composite devices; and
 54 * it expects to be the first configuration too.
 55 *
 56 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
 57 * discount the fluff that its RPC can be made to deliver: it doesn't need
 58 * a NOP altsetting for the data interface.  That lets it work on some of the
 59 * "so smart it's stupid" hardware which takes over configuration changes
 60 * from the software, and adds restrictions like "no altsettings".
 61 *
 62 * Unfortunately MSFT's RNDIS drivers are buggy.  They hang or oops, and
 63 * have all sorts of contrary-to-specification oddities that can prevent
 64 * them from working sanely.  Since bugfixes (or accurate specs, letting
 65 * Linux work around those bugs) are unlikely to ever come from MSFT, you
 66 * may want to avoid using RNDIS on purely operational grounds.
 67 *
 68 * Omissions from the RNDIS 1.0 specification include:
 69 *
 70 *   - Power management ... references data that's scattered around lots
 71 *     of other documentation, which is incorrect/incomplete there too.
 72 *
 73 *   - There are various undocumented protocol requirements, like the need
 74 *     to send garbage in some control-OUT messages.
 75 *
 76 *   - MS-Windows drivers sometimes emit undocumented requests.
 77 */
 78
 79struct f_rndis {
 80	struct gether			port;
 81	u8				ctrl_id, data_id;
 82	u8				ethaddr[ETH_ALEN];
 83	u32				vendorID;
 84	const char			*manufacturer;
 85	int				config;
 86
 87	struct usb_ep			*notify;
 88	struct usb_request		*notify_req;
 89	atomic_t			notify_count;
 90};
 91
 92static inline struct f_rndis *func_to_rndis(struct usb_function *f)
 93{
 94	return container_of(f, struct f_rndis, port.func);
 95}
 96
 97/* peak (theoretical) bulk transfer rate in bits-per-second */
 98static unsigned int bitrate(struct usb_gadget *g)
 99{
100	if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
101		return 13 * 1024 * 8 * 1000 * 8;
102	else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
103		return 13 * 512 * 8 * 1000 * 8;
104	else
105		return 19 * 64 * 1 * 1000 * 8;
106}
107
108/*-------------------------------------------------------------------------*/
109
110/*
111 */
112
113#define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */
114#define STATUS_BYTECOUNT		8	/* 8 bytes data */
115
116
117/* interface descriptor: */
118
119static struct usb_interface_descriptor rndis_control_intf = {
120	.bLength =		sizeof rndis_control_intf,
121	.bDescriptorType =	USB_DT_INTERFACE,
122
123	/* .bInterfaceNumber = DYNAMIC */
124	/* status endpoint is optional; this could be patched later */
125	.bNumEndpoints =	1,
126	.bInterfaceClass =	USB_CLASS_COMM,
127	.bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
128	.bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR,
129	/* .iInterface = DYNAMIC */
130};
131
132static struct usb_cdc_header_desc header_desc = {
133	.bLength =		sizeof header_desc,
134	.bDescriptorType =	USB_DT_CS_INTERFACE,
135	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
136
137	.bcdCDC =		cpu_to_le16(0x0110),
138};
139
140static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
141	.bLength =		sizeof call_mgmt_descriptor,
142	.bDescriptorType =	USB_DT_CS_INTERFACE,
143	.bDescriptorSubType =	USB_CDC_CALL_MANAGEMENT_TYPE,
144
145	.bmCapabilities =	0x00,
146	.bDataInterface =	0x01,
147};
148
149static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
150	.bLength =		sizeof rndis_acm_descriptor,
151	.bDescriptorType =	USB_DT_CS_INTERFACE,
152	.bDescriptorSubType =	USB_CDC_ACM_TYPE,
153
154	.bmCapabilities =	0x00,
155};
156
157static struct usb_cdc_union_desc rndis_union_desc = {
158	.bLength =		sizeof(rndis_union_desc),
159	.bDescriptorType =	USB_DT_CS_INTERFACE,
160	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
161	/* .bMasterInterface0 =	DYNAMIC */
162	/* .bSlaveInterface0 =	DYNAMIC */
163};
164
165/* the data interface has two bulk endpoints */
166
167static struct usb_interface_descriptor rndis_data_intf = {
168	.bLength =		sizeof rndis_data_intf,
169	.bDescriptorType =	USB_DT_INTERFACE,
170
171	/* .bInterfaceNumber = DYNAMIC */
172	.bNumEndpoints =	2,
173	.bInterfaceClass =	USB_CLASS_CDC_DATA,
174	.bInterfaceSubClass =	0,
175	.bInterfaceProtocol =	0,
176	/* .iInterface = DYNAMIC */
177};
178
179
180static struct usb_interface_assoc_descriptor
181rndis_iad_descriptor = {
182	.bLength =		sizeof rndis_iad_descriptor,
183	.bDescriptorType =	USB_DT_INTERFACE_ASSOCIATION,
184	.bFirstInterface =	0, /* XXX, hardcoded */
185	.bInterfaceCount = 	2,	// control + data
186	.bFunctionClass =	USB_CLASS_COMM,
187	.bFunctionSubClass =	USB_CDC_SUBCLASS_ETHERNET,
188	.bFunctionProtocol =	USB_CDC_ACM_PROTO_VENDOR,
189	/* .iFunction = DYNAMIC */
190};
191
192/* full speed support: */
193
194static struct usb_endpoint_descriptor fs_notify_desc = {
195	.bLength =		USB_DT_ENDPOINT_SIZE,
196	.bDescriptorType =	USB_DT_ENDPOINT,
197
198	.bEndpointAddress =	USB_DIR_IN,
199	.bmAttributes =		USB_ENDPOINT_XFER_INT,
200	.wMaxPacketSize =	cpu_to_le16(STATUS_BYTECOUNT),
201	.bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC,
202};
203
204static struct usb_endpoint_descriptor fs_in_desc = {
205	.bLength =		USB_DT_ENDPOINT_SIZE,
206	.bDescriptorType =	USB_DT_ENDPOINT,
207
208	.bEndpointAddress =	USB_DIR_IN,
209	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
210};
211
212static struct usb_endpoint_descriptor fs_out_desc = {
213	.bLength =		USB_DT_ENDPOINT_SIZE,
214	.bDescriptorType =	USB_DT_ENDPOINT,
215
216	.bEndpointAddress =	USB_DIR_OUT,
217	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
218};
219
220static struct usb_descriptor_header *eth_fs_function[] = {
221	(struct usb_descriptor_header *) &rndis_iad_descriptor,
222
223	/* control interface matches ACM, not Ethernet */
224	(struct usb_descriptor_header *) &rndis_control_intf,
225	(struct usb_descriptor_header *) &header_desc,
226	(struct usb_descriptor_header *) &call_mgmt_descriptor,
227	(struct usb_descriptor_header *) &rndis_acm_descriptor,
228	(struct usb_descriptor_header *) &rndis_union_desc,
229	(struct usb_descriptor_header *) &fs_notify_desc,
230
231	/* data interface has no altsetting */
232	(struct usb_descriptor_header *) &rndis_data_intf,
233	(struct usb_descriptor_header *) &fs_in_desc,
234	(struct usb_descriptor_header *) &fs_out_desc,
235	NULL,
236};
237
238/* high speed support: */
239
240static struct usb_endpoint_descriptor hs_notify_desc = {
241	.bLength =		USB_DT_ENDPOINT_SIZE,
242	.bDescriptorType =	USB_DT_ENDPOINT,
243
244	.bEndpointAddress =	USB_DIR_IN,
245	.bmAttributes =		USB_ENDPOINT_XFER_INT,
246	.wMaxPacketSize =	cpu_to_le16(STATUS_BYTECOUNT),
247	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4,
248};
249
250static struct usb_endpoint_descriptor hs_in_desc = {
251	.bLength =		USB_DT_ENDPOINT_SIZE,
252	.bDescriptorType =	USB_DT_ENDPOINT,
253
254	.bEndpointAddress =	USB_DIR_IN,
255	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
256	.wMaxPacketSize =	cpu_to_le16(512),
257};
258
259static struct usb_endpoint_descriptor hs_out_desc = {
260	.bLength =		USB_DT_ENDPOINT_SIZE,
261	.bDescriptorType =	USB_DT_ENDPOINT,
262
263	.bEndpointAddress =	USB_DIR_OUT,
264	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
265	.wMaxPacketSize =	cpu_to_le16(512),
266};
267
268static struct usb_descriptor_header *eth_hs_function[] = {
269	(struct usb_descriptor_header *) &rndis_iad_descriptor,
270
271	/* control interface matches ACM, not Ethernet */
272	(struct usb_descriptor_header *) &rndis_control_intf,
273	(struct usb_descriptor_header *) &header_desc,
274	(struct usb_descriptor_header *) &call_mgmt_descriptor,
275	(struct usb_descriptor_header *) &rndis_acm_descriptor,
276	(struct usb_descriptor_header *) &rndis_union_desc,
277	(struct usb_descriptor_header *) &hs_notify_desc,
278
279	/* data interface has no altsetting */
280	(struct usb_descriptor_header *) &rndis_data_intf,
281	(struct usb_descriptor_header *) &hs_in_desc,
282	(struct usb_descriptor_header *) &hs_out_desc,
283	NULL,
284};
285
286/* super speed support: */
287
288static struct usb_endpoint_descriptor ss_notify_desc = {
289	.bLength =		USB_DT_ENDPOINT_SIZE,
290	.bDescriptorType =	USB_DT_ENDPOINT,
291
292	.bEndpointAddress =	USB_DIR_IN,
293	.bmAttributes =		USB_ENDPOINT_XFER_INT,
294	.wMaxPacketSize =	cpu_to_le16(STATUS_BYTECOUNT),
295	.bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4,
296};
297
298static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
299	.bLength =		sizeof ss_intr_comp_desc,
300	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
301
302	/* the following 3 values can be tweaked if necessary */
303	/* .bMaxBurst =		0, */
304	/* .bmAttributes =	0, */
305	.wBytesPerInterval =	cpu_to_le16(STATUS_BYTECOUNT),
306};
307
308static struct usb_endpoint_descriptor ss_in_desc = {
309	.bLength =		USB_DT_ENDPOINT_SIZE,
310	.bDescriptorType =	USB_DT_ENDPOINT,
311
312	.bEndpointAddress =	USB_DIR_IN,
313	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
314	.wMaxPacketSize =	cpu_to_le16(1024),
315};
316
317static struct usb_endpoint_descriptor ss_out_desc = {
318	.bLength =		USB_DT_ENDPOINT_SIZE,
319	.bDescriptorType =	USB_DT_ENDPOINT,
320
321	.bEndpointAddress =	USB_DIR_OUT,
322	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
323	.wMaxPacketSize =	cpu_to_le16(1024),
324};
325
326static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
327	.bLength =		sizeof ss_bulk_comp_desc,
328	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
329
330	/* the following 2 values can be tweaked if necessary */
331	/* .bMaxBurst =		0, */
332	/* .bmAttributes =	0, */
333};
334
335static struct usb_descriptor_header *eth_ss_function[] = {
336	(struct usb_descriptor_header *) &rndis_iad_descriptor,
337
338	/* control interface matches ACM, not Ethernet */
339	(struct usb_descriptor_header *) &rndis_control_intf,
340	(struct usb_descriptor_header *) &header_desc,
341	(struct usb_descriptor_header *) &call_mgmt_descriptor,
342	(struct usb_descriptor_header *) &rndis_acm_descriptor,
343	(struct usb_descriptor_header *) &rndis_union_desc,
344	(struct usb_descriptor_header *) &ss_notify_desc,
345	(struct usb_descriptor_header *) &ss_intr_comp_desc,
346
347	/* data interface has no altsetting */
348	(struct usb_descriptor_header *) &rndis_data_intf,
349	(struct usb_descriptor_header *) &ss_in_desc,
350	(struct usb_descriptor_header *) &ss_bulk_comp_desc,
351	(struct usb_descriptor_header *) &ss_out_desc,
352	(struct usb_descriptor_header *) &ss_bulk_comp_desc,
353	NULL,
354};
355
356/* string descriptors: */
357
358static struct usb_string rndis_string_defs[] = {
359	[0].s = "ASUS RNDIS Communications Control",
360	[1].s = "ASUS RNDIS Ethernet Data",
361	[2].s = "ASUS Android USB Ethernet/RNDIS",
362	{  } /* end of list */
363};
364
365static struct usb_gadget_strings rndis_string_table = {
366	.language =		0x0409,	/* en-us */
367	.strings =		rndis_string_defs,
368};
369
370static struct usb_gadget_strings *rndis_strings[] = {
371	&rndis_string_table,
372	NULL,
373};
374
375/*-------------------------------------------------------------------------*/
376
377static struct sk_buff *rndis_add_header(struct gether *port,
378					struct sk_buff *skb)
379{
380	struct sk_buff *skb2;
381
382	skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
383	if (skb2)
384		rndis_add_hdr(skb2);
385
386	dev_kfree_skb_any(skb);
387	return skb2;
388}
389
390static void rndis_response_available(void *_rndis)
391{
392	struct f_rndis			*rndis = _rndis;
393	struct usb_request		*req = rndis->notify_req;
394	__le32				*data = req->buf;
395	int				status;
396
397	if (atomic_inc_return(&rndis->notify_count) != 1)
398		return;
399
400	/* Send RNDIS RESPONSE_AVAILABLE notification; a
401	 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
402	 *
403	 * This is the only notification defined by RNDIS.
404	 */
405	data[0] = cpu_to_le32(1);
406	data[1] = cpu_to_le32(0);
407
408	status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
409	if (status) {
410		atomic_dec(&rndis->notify_count);
411		pr_err("notify/0 --> %d\n", status);
412	}
413}
414
415static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
416{
417	struct f_rndis			*rndis = req->context;
418	int				status = req->status;
419
420	/* after TX:
421	 *  - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
422	 *  - RNDIS_RESPONSE_AVAILABLE (status/irq)
423	 */
424	switch (status) {
425	case -ECONNRESET:
426	case -ESHUTDOWN:
427		/* connection gone */
428		atomic_set(&rndis->notify_count, 0);
429		break;
430	default:
431		pr_err("RNDIS %s response error %d, %d/%d\n",
432			ep->name, status,
433			req->actual, req->length);
434		/* FALLTHROUGH */
435	case 0:
436		if (ep != rndis->notify)
437			break;
438
439		/* handle multiple pending RNDIS_RESPONSE_AVAILABLE
440		 * notifications by resending until we're done
441		 */
442		if (atomic_dec_and_test(&rndis->notify_count))
443			break;
444		status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
445		if (status) {
446			atomic_dec(&rndis->notify_count);
447			pr_err("notify/1 --> %d\n", status);
448		}
449		break;
450	}
451}
452
453static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
454{
455	struct f_rndis			*rndis = req->context;
456	int				status;
457
458	/* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
459//	spin_lock(&dev->lock);
460	status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
461	if (status < 0)
462		pr_err("RNDIS command error %d, %d/%d\n",
463			status, req->actual, req->length);
464//	spin_unlock(&dev->lock);
465}
466
467static int
468rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
469{
470	struct f_rndis		*rndis = func_to_rndis(f);
471	struct usb_composite_dev *cdev = f->config->cdev;
472	struct usb_request	*req = cdev->req;
473	int			value = -EOPNOTSUPP;
474	u16			w_index = le16_to_cpu(ctrl->wIndex);
475	u16			w_value = le16_to_cpu(ctrl->wValue);
476	u16			w_length = le16_to_cpu(ctrl->wLength);
477
478	/* composite driver infrastructure handles everything except
479	 * CDC class messages; interface activation uses set_alt().
480	 */
481	switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
482
483	/* RNDIS uses the CDC command encapsulation mechanism to implement
484	 * an RPC scheme, with much getting/setting of attributes by OID.
485	 */
486	case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
487			| USB_CDC_SEND_ENCAPSULATED_COMMAND:
488		if (w_value || w_index != rndis->ctrl_id)
489			goto invalid;
490		/* read the request; process it later */
491		value = w_length;
492		req->complete = rndis_command_complete;
493		req->context = rndis;
494		/* later, rndis_response_available() sends a notification */
495		break;
496
497	case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
498			| USB_CDC_GET_ENCAPSULATED_RESPONSE:
499		if (w_value || w_index != rndis->ctrl_id)
500			goto invalid;
501		else {
502			u8 *buf;
503			u32 n;
504
505			/* return the result */
506			buf = rndis_get_next_response(rndis->config, &n);
507			if (buf) {
508				memcpy(req->buf, buf, n);
509				req->complete = rndis_response_complete;
510				rndis_free_response(rndis->config, buf);
511				value = n;
512			}
513			/* else stalls ... spec says to avoid that */
514		}
515		break;
516
517	default:
518invalid:
519		VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
520			ctrl->bRequestType, ctrl->bRequest,
521			w_value, w_index, w_length);
522	}
523
524	/* respond with data transfer or status phase? */
525	if (value >= 0) {
526		DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
527			ctrl->bRequestType, ctrl->bRequest,
528			w_value, w_index, w_length);
529		req->zero = (value < w_length);
530		req->length = value;
531		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
532		if (value < 0)
533			ERROR(cdev, "rndis response on err %d\n", value);
534	}
535
536	/* device either stalls (value < 0) or reports success */
537	return value;
538}
539
540
541static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
542{
543	struct f_rndis		*rndis = func_to_rndis(f);
544	struct usb_composite_dev *cdev = f->config->cdev;
545
546	/* we know alt == 0 */
547
548	if (intf == rndis->ctrl_id) {
549		if (rndis->notify->driver_data) {
550			VDBG(cdev, "reset rndis control %d\n", intf);
551			usb_ep_disable(rndis->notify);
552		}
553		if (!rndis->notify->desc) {
554			VDBG(cdev, "init rndis ctrl %d\n", intf);
555			if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
556				goto fail;
557		}
558		usb_ep_enable(rndis->notify);
559		rndis->notify->driver_data = rndis;
560
561	} else if (intf == rndis->data_id) {
562		struct net_device	*net;
563
564		if (rndis->port.in_ep->driver_data) {
565			DBG(cdev, "reset rndis\n");
566			gether_disconnect(&rndis->port);
567		}
568
569		if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
570			DBG(cdev, "init rndis\n");
571			if (config_ep_by_speed(cdev->gadget, f,
572					       rndis->port.in_ep) ||
573			    config_ep_by_speed(cdev->gadget, f,
574					       rndis->port.out_ep)) {
575				rndis->port.in_ep->desc = NULL;
576				rndis->port.out_ep->desc = NULL;
577				goto fail;
578			}
579		}
580
581		/* Avoid ZLPs; they can be troublesome. */
582		rndis->port.is_zlp_ok = false;
583
584		/* RNDIS should be in the "RNDIS uninitialized" state,
585		 * either never activated or after rndis_uninit().
586		 *
587		 * We don't want data to flow here until a nonzero packet
588		 * filter is set, at which point it enters "RNDIS data
589		 * initialized" state ... but we do want the endpoints
590		 * to be activated.  It's a strange little state.
591		 *
592		 * REVISIT the RNDIS gadget code has done this wrong for a
593		 * very long time.  We need another call to the link layer
594		 * code -- gether_updown(...bool) maybe -- to do it right.
595		 */
596		rndis->port.cdc_filter = 0;
597
598		DBG(cdev, "RNDIS RX/TX early activation ... \n");
599		net = gether_connect(&rndis->port);
600		if (IS_ERR(net))
601			return PTR_ERR(net);
602
603		rndis_set_param_dev(rndis->config, net,
604				&rndis->port.cdc_filter);
605	} else
606		goto fail;
607
608	return 0;
609fail:
610	return -EINVAL;
611}
612
613static void rndis_disable(struct usb_function *f)
614{
615	struct f_rndis		*rndis = func_to_rndis(f);
616	struct usb_composite_dev *cdev = f->config->cdev;
617
618	if (!rndis->notify->driver_data)
619		return;
620
621	DBG(cdev, "rndis deactivated\n");
622
623	rndis_uninit(rndis->config);
624	gether_disconnect(&rndis->port);
625
626	usb_ep_disable(rndis->notify);
627	rndis->notify->driver_data = NULL;
628}
629
630/*-------------------------------------------------------------------------*/
631
632/*
633 * This isn't quite the same mechanism as CDC Ethernet, since the
634 * notification scheme passes less data, but the same set of link
635 * states must be tested.  A key difference is that altsettings are
636 * not used to tell whether the link should send packets or not.
637 */
638
639static void rndis_open(struct gether *geth)
640{
641	struct f_rndis		*rndis = func_to_rndis(&geth->func);
642	struct usb_composite_dev *cdev = geth->func.config->cdev;
643
644	DBG(cdev, "%s\n", __func__);
645
646	rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
647				bitrate(cdev->gadget) / 100);
648	rndis_signal_connect(rndis->config);
649}
650
651static void rndis_close(struct gether *geth)
652{
653	struct f_rndis		*rndis = func_to_rndis(&geth->func);
654
655	DBG(geth->func.config->cdev, "%s\n", __func__);
656
657	rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
658	rndis_signal_disconnect(rndis->config);
659}
660
661/*-------------------------------------------------------------------------*/
662
663/* ethernet function driver setup/binding */
664
665static int
666rndis_bind(struct usb_configuration *c, struct usb_function *f)
667{
668	struct usb_composite_dev *cdev = c->cdev;
669	struct f_rndis		*rndis = func_to_rndis(f);
670	int			status;
671	struct usb_ep		*ep;
672
673	/* allocate instance-specific interface IDs */
674	status = usb_interface_id(c, f);
675	if (status < 0)
676		goto fail;
677	rndis->ctrl_id = status;
678	rndis_iad_descriptor.bFirstInterface = status;
679
680	rndis_control_intf.bInterfaceNumber = status;
681	rndis_union_desc.bMasterInterface0 = status;
682
683	status = usb_interface_id(c, f);
684	if (status < 0)
685		goto fail;
686	rndis->data_id = status;
687
688	rndis_data_intf.bInterfaceNumber = status;
689	rndis_union_desc.bSlaveInterface0 = status;
690
691	status = -ENODEV;
692
693	/* allocate instance-specific endpoints */
694	ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
695	if (!ep)
696		goto fail;
697	rndis->port.in_ep = ep;
698	ep->driver_data = cdev;	/* claim */
699
700	ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
701	if (!ep)
702		goto fail;
703	rndis->port.out_ep = ep;
704	ep->driver_data = cdev;	/* claim */
705
706	/* NOTE:  a status/notification endpoint is, strictly speaking,
707	 * optional.  We don't treat it that way though!  It's simpler,
708	 * and some newer profiles don't treat it as optional.
709	 */
710	ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
711	if (!ep)
712		goto fail;
713	rndis->notify = ep;
714	ep->driver_data = cdev;	/* claim */
715
716	status = -ENOMEM;
717
718	/* allocate notification request and buffer */
719	rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
720	if (!rndis->notify_req)
721		goto fail;
722	rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
723	if (!rndis->notify_req->buf)
724		goto fail;
725	rndis->notify_req->length = STATUS_BYTECOUNT;
726	rndis->notify_req->context = rndis;
727	rndis->notify_req->complete = rndis_response_complete;
728
729	/* copy descriptors, and track endpoint copies */
730	f->descriptors = usb_copy_descriptors(eth_fs_function);
731	if (!f->descriptors)
732		goto fail;
733
734	/* support all relevant hardware speeds... we expect that when
735	 * hardware is dual speed, all bulk-capable endpoints work at
736	 * both speeds
737	 */
738	if (gadget_is_dualspeed(c->cdev->gadget)) {
739		hs_in_desc.bEndpointAddress =
740				fs_in_desc.bEndpointAddress;
741		hs_out_desc.bEndpointAddress =
742				fs_out_desc.bEndpointAddress;
743		hs_notify_desc.bEndpointAddress =
744				fs_notify_desc.bEndpointAddress;
745
746		/* copy descriptors, and track endpoint copies */
747		f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
748		if (!f->hs_descriptors)
749			goto fail;
750	}
751
752	if (gadget_is_superspeed(c->cdev->gadget)) {
753		ss_in_desc.bEndpointAddress =
754				fs_in_desc.bEndpointAddress;
755		ss_out_desc.bEndpointAddress =
756				fs_out_desc.bEndpointAddress;
757		ss_notify_desc.bEndpointAddress =
758				fs_notify_desc.bEndpointAddress;
759
760		/* copy descriptors, and track endpoint copies */
761		f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
762		if (!f->ss_descriptors)
763			goto fail;
764	}
765
766	rndis->port.open = rndis_open;
767	rndis->port.close = rndis_close;
768
769	status = rndis_register(rndis_response_available, rndis);
770	if (status < 0)
771		goto fail;
772	rndis->config = status;
773
774	rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
775	rndis_set_host_mac(rndis->config, rndis->ethaddr);
776
777	if (rndis_set_param_vendor(rndis->config, rndis->vendorID,
778				   rndis->manufacturer))
779			goto fail;
780
781	/* NOTE:  all that is done without knowing or caring about
782	 * the network link ... which is unavailable to this code
783	 * until we're activated via set_alt().
784	 */
785
786	DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
787			gadget_is_superspeed(c->cdev->gadget) ? "super" :
788			gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
789			rndis->port.in_ep->name, rndis->port.out_ep->name,
790			rndis->notify->name);
791	return 0;
792
793fail:
794	if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
795		usb_free_descriptors(f->ss_descriptors);
796	if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
797		usb_free_descriptors(f->hs_descriptors);
798	if (f->descriptors)
799		usb_free_descriptors(f->descriptors);
800
801	if (rndis->notify_req) {
802		kfree(rndis->notify_req->buf);
803		usb_ep_free_request(rndis->notify, rndis->notify_req);
804	}
805
806	/* we might as well release our claims on endpoints */
807	if (rndis->notify)
808		rndis->notify->driver_data = NULL;
809	if (rndis->port.out_ep->desc)
810		rndis->port.out_ep->driver_data = NULL;
811	if (rndis->port.in_ep->desc)
812		rndis->port.in_ep->driver_data = NULL;
813
814	ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
815
816	return status;
817}
818
819static void
820rndis_unbind(struct usb_configuration *c, struct usb_function *f)
821{
822	struct f_rndis		*rndis = func_to_rndis(f);
823
824	rndis_deregister(rndis->config);
825	rndis_exit();
826
827	if (gadget_is_superspeed(c->cdev->gadget))
828		usb_free_descriptors(f->ss_descriptors);
829
830	if (gadget_is_dualspeed(c->cdev->gadget))
831		usb_free_descriptors(f->hs_descriptors);
832	usb_free_descriptors(f->descriptors);
833
834	kfree(rndis->notify_req->buf);
835	usb_ep_free_request(rndis->notify, rndis->notify_req);
836
837	kfree(rndis);
838}
839
840/* Some controllers can't support RNDIS ... */
841static inline bool can_support_rndis(struct usb_configuration *c)
842{
843	/* everything else is *presumably* fine */
844	return true;
845}
846
847/**
848 * rndis_bind_config - add RNDIS network link to a configuration
849 * @c: the configuration to support the network link
850 * @ethaddr: a buffer in which the ethernet address of the host side
851 *	side of the link was recorded
852 * Context: single threaded during gadget setup
853 *
854 * Returns zero on success, else negative errno.
855 *
856 * Caller must have called @gether_setup().  Caller is also responsible
857 * for calling @gether_cleanup() before module unload.
858 */
859int
860rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
861				u32 vendorID, const char *manufacturer)
862{
863	struct f_rndis	*rndis;
864	int		status;
865
866	if (!can_support_rndis(c) || !ethaddr)
867		return -EINVAL;
868
869	/* setup RNDIS itself */
870	status = rndis_init();
871	if (status < 0)
872		return status;
873
874	/* maybe allocate device-global string IDs */
875	if (rndis_string_defs[0].id == 0) {
876
877		/* control interface label */
878		status = usb_string_id(c->cdev);
879		if (status < 0)
880			return status;
881		rndis_string_defs[0].id = status;
882		rndis_control_intf.iInterface = status;
883
884		/* data interface label */
885		status = usb_string_id(c->cdev);
886		if (status < 0)
887			return status;
888		rndis_string_defs[1].id = status;
889		rndis_data_intf.iInterface = status;
890
891		/* IAD iFunction label */
892		status = usb_string_id(c->cdev);
893		if (status < 0)
894			return status;
895		rndis_string_defs[2].id = status;
896		rndis_iad_descriptor.iFunction = status;
897	}
898
899	/* allocate and initialize one new instance */
900	status = -ENOMEM;
901	rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
902	if (!rndis)
903		goto fail;
904
905	memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
906	rndis->vendorID = vendorID;
907	rndis->manufacturer = manufacturer;
908
909	/* RNDIS activates when the host changes this filter */
910	rndis->port.cdc_filter = 0;
911
912	/* RNDIS has special (and complex) framing */
913	rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
914	rndis->port.wrap = rndis_add_header;
915	rndis->port.unwrap = rndis_rm_hdr;
916
917	rndis->port.func.name = "rndis";
918	rndis->port.func.strings = rndis_strings;
919	/* descriptors are per-instance copies */
920	rndis->port.func.bind = rndis_bind;
921	rndis->port.func.unbind = rndis_unbind;
922	rndis->port.func.set_alt = rndis_set_alt;
923	rndis->port.func.setup = rndis_setup;
924	rndis->port.func.disable = rndis_disable;
925
926	status = usb_add_function(c, &rndis->port.func);
927	if (status) {
928		kfree(rndis);
929fail:
930		rndis_exit();
931	}
932	return status;
933}