Linux Audio

Check our new training course

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