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