blob: 7903764348944ac9acca2f56856b11797337aae4 [file] [log] [blame]
David Brownell45fe3b82008-06-19 18:20:04 -07001/*
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
Michal Nazarewiczb97503f2009-10-28 16:57:30 +01007 * Copyright (C) 2009 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01008 * Author: Michal Nazarewicz (mina86@mina86.com)
David Brownell45fe3b82008-06-19 18:20:04 -07009 *
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.
David Brownell45fe3b82008-06-19 18:20:04 -070014 */
15
16/* #define VERBOSE_DEBUG */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Brownell45fe3b82008-06-19 18:20:04 -070019#include <linux/kernel.h>
20#include <linux/device.h>
21#include <linux/etherdevice.h>
22
Arun Sharma60063492011-07-26 16:09:06 -070023#include <linux/atomic.h>
David Brownell45fe3b82008-06-19 18:20:04 -070024
25#include "u_ether.h"
26#include "rndis.h"
27
David Brownell45fe3b82008-06-19 18:20:04 -070028/*
29 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
30 * been promoted instead of the standard CDC Ethernet. The published RNDIS
31 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
32 * ActiveSync have even worse status in terms of specification.
33 *
34 * In short: it's a protocol controlled by (and for) Microsoft, not for an
35 * Open ecosystem or markets. Linux supports it *only* because Microsoft
36 * doesn't support the CDC Ethernet standard.
37 *
38 * The RNDIS data transfer model is complex, with multiple Ethernet packets
39 * per USB message, and out of band data. The control model is built around
40 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
41 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
42 * useless (they're ignored). RNDIS expects to be the only function in its
43 * configuration, so it's no real help if you need composite devices; and
44 * it expects to be the first configuration too.
45 *
46 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
47 * discount the fluff that its RPC can be made to deliver: it doesn't need
48 * a NOP altsetting for the data interface. That lets it work on some of the
49 * "so smart it's stupid" hardware which takes over configuration changes
50 * from the software, and adds restrictions like "no altsettings".
51 *
52 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
53 * have all sorts of contrary-to-specification oddities that can prevent
54 * them from working sanely. Since bugfixes (or accurate specs, letting
55 * Linux work around those bugs) are unlikely to ever come from MSFT, you
56 * may want to avoid using RNDIS on purely operational grounds.
57 *
58 * Omissions from the RNDIS 1.0 specification include:
59 *
60 * - Power management ... references data that's scattered around lots
61 * of other documentation, which is incorrect/incomplete there too.
62 *
63 * - There are various undocumented protocol requirements, like the need
64 * to send garbage in some control-OUT messages.
65 *
66 * - MS-Windows drivers sometimes emit undocumented requests.
67 */
68
Vijayavardhan Vennapusac76dced2013-05-27 15:00:52 +053069static unsigned int rndis_dl_max_pkt_per_xfer = 3;
70module_param(rndis_dl_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
71MODULE_PARM_DESC(rndis_dl_max_pkt_per_xfer,
72 "Maximum packets per transfer for DL aggregation");
Vamsi Krishna7f21ac82013-04-17 21:41:33 -070073
Vamsi Krishna39f8c462013-05-15 14:10:27 -070074static unsigned int rndis_ul_max_pkt_per_xfer = 3;
Vamsi Krishna7f21ac82013-04-17 21:41:33 -070075module_param(rndis_ul_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
76MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer,
Vamsi Krishna915ae192013-05-13 15:39:56 -070077 "Maximum packets per transfer for UL aggregation");
Vamsi Krishna7f21ac82013-04-17 21:41:33 -070078
David Brownell45fe3b82008-06-19 18:20:04 -070079struct f_rndis {
80 struct gether port;
81 u8 ctrl_id, data_id;
82 u8 ethaddr[ETH_ALEN];
Benoit Gobyda8a44a2011-12-16 20:00:01 -080083 u32 vendorID;
84 const char *manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -070085 int config;
86
David Brownell45fe3b82008-06-19 18:20:04 -070087 struct usb_ep *notify;
David Brownell45fe3b82008-06-19 18:20:04 -070088 struct usb_request *notify_req;
89 atomic_t notify_count;
90};
91
92static inline struct f_rndis *func_to_rndis(struct usb_function *f)
93{
94 return container_of(f, struct f_rndis, port.func);
95}
96
97/* peak (theoretical) bulk transfer rate in bits-per-second */
98static unsigned int bitrate(struct usb_gadget *g)
99{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700100 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
101 return 13 * 1024 * 8 * 1000 * 8;
102 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
David Brownell45fe3b82008-06-19 18:20:04 -0700103 return 13 * 512 * 8 * 1000 * 8;
104 else
Paul Zimmerman04617db2011-06-27 14:13:18 -0700105 return 19 * 64 * 1 * 1000 * 8;
David Brownell45fe3b82008-06-19 18:20:04 -0700106}
107
108/*-------------------------------------------------------------------------*/
109
110/*
111 */
112
113#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
114#define STATUS_BYTECOUNT 8 /* 8 bytes data */
115
116
117/* interface descriptor: */
118
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200119static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700120 .bLength = sizeof rndis_control_intf,
121 .bDescriptorType = USB_DT_INTERFACE,
122
123 /* .bInterfaceNumber = DYNAMIC */
124 /* status endpoint is optional; this could be patched later */
125 .bNumEndpoints = 1,
126 .bInterfaceClass = USB_CLASS_COMM,
127 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
128 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
129 /* .iInterface = DYNAMIC */
130};
131
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200132static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700133 .bLength = sizeof header_desc,
134 .bDescriptorType = USB_DT_CS_INTERFACE,
135 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
136
Harvey Harrison551509d2009-02-11 14:11:36 -0800137 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700138};
139
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200140static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700141 .bLength = sizeof call_mgmt_descriptor,
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
144
145 .bmCapabilities = 0x00,
146 .bDataInterface = 0x01,
147};
148
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200149static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100150 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700151 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubType = USB_CDC_ACM_TYPE,
153
154 .bmCapabilities = 0x00,
155};
156
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200157static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700158 .bLength = sizeof(rndis_union_desc),
159 .bDescriptorType = USB_DT_CS_INTERFACE,
160 .bDescriptorSubType = USB_CDC_UNION_TYPE,
161 /* .bMasterInterface0 = DYNAMIC */
162 /* .bSlaveInterface0 = DYNAMIC */
163};
164
165/* the data interface has two bulk endpoints */
166
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200167static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700168 .bLength = sizeof rndis_data_intf,
169 .bDescriptorType = USB_DT_INTERFACE,
170
171 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700172 .bNumEndpoints = 2,
173 .bInterfaceClass = USB_CLASS_CDC_DATA,
174 .bInterfaceSubClass = 0,
175 .bInterfaceProtocol = 0,
176 /* .iInterface = DYNAMIC */
177};
178
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100179
180static struct usb_interface_assoc_descriptor
181rndis_iad_descriptor = {
182 .bLength = sizeof rndis_iad_descriptor,
183 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
184
185 .bFirstInterface = 0, /* XXX, hardcoded */
186 .bInterfaceCount = 2, // control + data
187 .bFunctionClass = USB_CLASS_COMM,
188 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
189 .bFunctionProtocol = USB_CDC_PROTO_NONE,
190 /* .iFunction = DYNAMIC */
191};
192
David Brownell45fe3b82008-06-19 18:20:04 -0700193/* full speed support: */
194
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200195static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700196 .bLength = USB_DT_ENDPOINT_SIZE,
197 .bDescriptorType = USB_DT_ENDPOINT,
198
199 .bEndpointAddress = USB_DIR_IN,
200 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800201 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700202 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
203};
204
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200205static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700206 .bLength = USB_DT_ENDPOINT_SIZE,
207 .bDescriptorType = USB_DT_ENDPOINT,
208
209 .bEndpointAddress = USB_DIR_IN,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
211};
212
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200213static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216
217 .bEndpointAddress = USB_DIR_OUT,
218 .bmAttributes = USB_ENDPOINT_XFER_BULK,
219};
220
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200221static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100222 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700223
David Brownell45fe3b82008-06-19 18:20:04 -0700224 /* control interface matches ACM, not Ethernet */
225 (struct usb_descriptor_header *) &rndis_control_intf,
226 (struct usb_descriptor_header *) &header_desc,
227 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100228 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700229 (struct usb_descriptor_header *) &rndis_union_desc,
230 (struct usb_descriptor_header *) &fs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700231
David Brownell45fe3b82008-06-19 18:20:04 -0700232 /* data interface has no altsetting */
233 (struct usb_descriptor_header *) &rndis_data_intf,
234 (struct usb_descriptor_header *) &fs_in_desc,
235 (struct usb_descriptor_header *) &fs_out_desc,
236 NULL,
237};
238
239/* high speed support: */
240
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200241static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700242 .bLength = USB_DT_ENDPOINT_SIZE,
243 .bDescriptorType = USB_DT_ENDPOINT,
244
245 .bEndpointAddress = USB_DIR_IN,
246 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800247 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700248 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
249};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700250
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200251static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700252 .bLength = USB_DT_ENDPOINT_SIZE,
253 .bDescriptorType = USB_DT_ENDPOINT,
254
255 .bEndpointAddress = USB_DIR_IN,
256 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800257 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700258};
259
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200260static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700261 .bLength = USB_DT_ENDPOINT_SIZE,
262 .bDescriptorType = USB_DT_ENDPOINT,
263
264 .bEndpointAddress = USB_DIR_OUT,
265 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800266 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700267};
268
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200269static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100270 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700271
David Brownell45fe3b82008-06-19 18:20:04 -0700272 /* control interface matches ACM, not Ethernet */
273 (struct usb_descriptor_header *) &rndis_control_intf,
274 (struct usb_descriptor_header *) &header_desc,
275 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100276 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700277 (struct usb_descriptor_header *) &rndis_union_desc,
278 (struct usb_descriptor_header *) &hs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700279
David Brownell45fe3b82008-06-19 18:20:04 -0700280 /* data interface has no altsetting */
281 (struct usb_descriptor_header *) &rndis_data_intf,
282 (struct usb_descriptor_header *) &hs_in_desc,
283 (struct usb_descriptor_header *) &hs_out_desc,
284 NULL,
285};
286
Paul Zimmerman04617db2011-06-27 14:13:18 -0700287/* super speed support: */
288
289static struct usb_endpoint_descriptor ss_notify_desc = {
290 .bLength = USB_DT_ENDPOINT_SIZE,
291 .bDescriptorType = USB_DT_ENDPOINT,
292
293 .bEndpointAddress = USB_DIR_IN,
294 .bmAttributes = USB_ENDPOINT_XFER_INT,
295 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
296 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
297};
298
299static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
300 .bLength = sizeof ss_intr_comp_desc,
301 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
302
303 /* the following 3 values can be tweaked if necessary */
304 /* .bMaxBurst = 0, */
305 /* .bmAttributes = 0, */
306 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
307};
308
309static struct usb_endpoint_descriptor ss_in_desc = {
310 .bLength = USB_DT_ENDPOINT_SIZE,
311 .bDescriptorType = USB_DT_ENDPOINT,
312
313 .bEndpointAddress = USB_DIR_IN,
314 .bmAttributes = USB_ENDPOINT_XFER_BULK,
315 .wMaxPacketSize = cpu_to_le16(1024),
316};
317
318static struct usb_endpoint_descriptor ss_out_desc = {
319 .bLength = USB_DT_ENDPOINT_SIZE,
320 .bDescriptorType = USB_DT_ENDPOINT,
321
322 .bEndpointAddress = USB_DIR_OUT,
323 .bmAttributes = USB_ENDPOINT_XFER_BULK,
324 .wMaxPacketSize = cpu_to_le16(1024),
325};
326
327static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
328 .bLength = sizeof ss_bulk_comp_desc,
329 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
330
331 /* the following 2 values can be tweaked if necessary */
332 /* .bMaxBurst = 0, */
333 /* .bmAttributes = 0, */
334};
335
336static struct usb_descriptor_header *eth_ss_function[] = {
337 (struct usb_descriptor_header *) &rndis_iad_descriptor,
338
339 /* control interface matches ACM, not Ethernet */
340 (struct usb_descriptor_header *) &rndis_control_intf,
341 (struct usb_descriptor_header *) &header_desc,
342 (struct usb_descriptor_header *) &call_mgmt_descriptor,
343 (struct usb_descriptor_header *) &rndis_acm_descriptor,
344 (struct usb_descriptor_header *) &rndis_union_desc,
345 (struct usb_descriptor_header *) &ss_notify_desc,
346 (struct usb_descriptor_header *) &ss_intr_comp_desc,
347
348 /* data interface has no altsetting */
349 (struct usb_descriptor_header *) &rndis_data_intf,
350 (struct usb_descriptor_header *) &ss_in_desc,
351 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
352 (struct usb_descriptor_header *) &ss_out_desc,
353 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
354 NULL,
355};
356
David Brownell45fe3b82008-06-19 18:20:04 -0700357/* string descriptors: */
358
359static struct usb_string rndis_string_defs[] = {
360 [0].s = "RNDIS Communications Control",
361 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100362 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700363 { } /* end of list */
364};
365
366static struct usb_gadget_strings rndis_string_table = {
367 .language = 0x0409, /* en-us */
368 .strings = rndis_string_defs,
369};
370
371static struct usb_gadget_strings *rndis_strings[] = {
372 &rndis_string_table,
373 NULL,
374};
375
376/*-------------------------------------------------------------------------*/
377
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500378static struct sk_buff *rndis_add_header(struct gether *port,
379 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700380{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500381 struct sk_buff *skb2;
382
383 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
384 if (skb2)
385 rndis_add_hdr(skb2);
386
387 dev_kfree_skb_any(skb);
388 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700389}
390
391static void rndis_response_available(void *_rndis)
392{
393 struct f_rndis *rndis = _rndis;
394 struct usb_request *req = rndis->notify_req;
395 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
396 __le32 *data = req->buf;
397 int status;
398
Richard Röjforsff349502008-11-15 19:53:24 -0800399 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700400 return;
401
402 /* Send RNDIS RESPONSE_AVAILABLE notification; a
403 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
404 *
405 * This is the only notification defined by RNDIS.
406 */
407 data[0] = cpu_to_le32(1);
408 data[1] = cpu_to_le32(0);
409
410 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
411 if (status) {
412 atomic_dec(&rndis->notify_count);
413 DBG(cdev, "notify/0 --> %d\n", status);
414 }
415}
416
417static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
418{
419 struct f_rndis *rndis = req->context;
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530420 struct usb_composite_dev *cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700421 int status = req->status;
422
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530423 if (!rndis->port.func.config || !rndis->port.func.config->cdev)
424 return;
425 else
426 cdev = rndis->port.func.config->cdev;
427
David Brownell45fe3b82008-06-19 18:20:04 -0700428 /* 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
461static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
462{
463 struct f_rndis *rndis = req->context;
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530464 struct usb_composite_dev *cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700465 int status;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530466 rndis_init_msg_type *buf;
David Brownell45fe3b82008-06-19 18:20:04 -0700467
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530468 if (!rndis->port.func.config || !rndis->port.func.config->cdev)
469 return;
470 else
471 cdev = rndis->port.func.config->cdev;
472
David Brownell45fe3b82008-06-19 18:20:04 -0700473 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
474// spin_lock(&dev->lock);
475 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
476 if (status < 0)
477 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
478 status, req->actual, req->length);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530479
480 buf = (rndis_init_msg_type *)req->buf;
481
482 if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) {
483 if (buf->MaxTransferSize > 2048)
484 rndis->port.multi_pkt_xfer = 1;
485 else
486 rndis->port.multi_pkt_xfer = 0;
487 DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
488 __func__, buf->MaxTransferSize,
489 rndis->port.multi_pkt_xfer ? "enabled" :
490 "disabled");
Vijayavardhan Vennapusac76dced2013-05-27 15:00:52 +0530491 if (rndis_dl_max_pkt_per_xfer <= 1)
Mayank Rana93df2752012-09-12 18:12:46 +0530492 rndis->port.multi_pkt_xfer = 0;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530493 }
David Brownell45fe3b82008-06-19 18:20:04 -0700494// spin_unlock(&dev->lock);
495}
496
497static int
498rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
499{
500 struct f_rndis *rndis = func_to_rndis(f);
501 struct usb_composite_dev *cdev = f->config->cdev;
502 struct usb_request *req = cdev->req;
503 int value = -EOPNOTSUPP;
504 u16 w_index = le16_to_cpu(ctrl->wIndex);
505 u16 w_value = le16_to_cpu(ctrl->wValue);
506 u16 w_length = le16_to_cpu(ctrl->wLength);
507
508 /* composite driver infrastructure handles everything except
509 * CDC class messages; interface activation uses set_alt().
510 */
511 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
512
513 /* RNDIS uses the CDC command encapsulation mechanism to implement
514 * an RPC scheme, with much getting/setting of attributes by OID.
515 */
516 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
517 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300518 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700519 goto invalid;
520 /* read the request; process it later */
521 value = w_length;
522 req->complete = rndis_command_complete;
523 req->context = rndis;
524 /* later, rndis_response_available() sends a notification */
525 break;
526
527 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
528 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
529 if (w_value || w_index != rndis->ctrl_id)
530 goto invalid;
531 else {
532 u8 *buf;
533 u32 n;
534
535 /* return the result */
536 buf = rndis_get_next_response(rndis->config, &n);
537 if (buf) {
538 memcpy(req->buf, buf, n);
539 req->complete = rndis_response_complete;
Lukasz Majewskif1356172012-02-10 09:54:51 +0100540 req->context = rndis;
David Brownell45fe3b82008-06-19 18:20:04 -0700541 rndis_free_response(rndis->config, buf);
542 value = n;
543 }
544 /* else stalls ... spec says to avoid that */
545 }
546 break;
547
548 default:
549invalid:
550 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
551 ctrl->bRequestType, ctrl->bRequest,
552 w_value, w_index, w_length);
553 }
554
555 /* respond with data transfer or status phase? */
556 if (value >= 0) {
557 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
558 ctrl->bRequestType, ctrl->bRequest,
559 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700560 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700561 req->length = value;
562 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
563 if (value < 0)
564 ERROR(cdev, "rndis response on err %d\n", value);
565 }
566
567 /* device either stalls (value < 0) or reports success */
568 return value;
569}
570
571
572static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
573{
574 struct f_rndis *rndis = func_to_rndis(f);
575 struct usb_composite_dev *cdev = f->config->cdev;
576
577 /* we know alt == 0 */
578
579 if (intf == rndis->ctrl_id) {
580 if (rndis->notify->driver_data) {
581 VDBG(cdev, "reset rndis control %d\n", intf);
582 usb_ep_disable(rndis->notify);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300583 }
584 if (!rndis->notify->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700585 VDBG(cdev, "init rndis ctrl %d\n", intf);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300586 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
587 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700588 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300589 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700590 rndis->notify->driver_data = rndis;
591
592 } else if (intf == rndis->data_id) {
593 struct net_device *net;
594
595 if (rndis->port.in_ep->driver_data) {
596 DBG(cdev, "reset rndis\n");
597 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530598 }
599
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300600 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700601 DBG(cdev, "init rndis\n");
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300602 if (config_ep_by_speed(cdev->gadget, f,
603 rndis->port.in_ep) ||
604 config_ep_by_speed(cdev->gadget, f,
605 rndis->port.out_ep)) {
606 rndis->port.in_ep->desc = NULL;
607 rndis->port.out_ep->desc = NULL;
608 goto fail;
609 }
David Brownell45fe3b82008-06-19 18:20:04 -0700610 }
611
612 /* Avoid ZLPs; they can be troublesome. */
613 rndis->port.is_zlp_ok = false;
614
615 /* RNDIS should be in the "RNDIS uninitialized" state,
616 * either never activated or after rndis_uninit().
617 *
618 * We don't want data to flow here until a nonzero packet
619 * filter is set, at which point it enters "RNDIS data
620 * initialized" state ... but we do want the endpoints
621 * to be activated. It's a strange little state.
622 *
623 * REVISIT the RNDIS gadget code has done this wrong for a
624 * very long time. We need another call to the link layer
625 * code -- gether_updown(...bool) maybe -- to do it right.
626 */
627 rndis->port.cdc_filter = 0;
628
629 DBG(cdev, "RNDIS RX/TX early activation ... \n");
630 net = gether_connect(&rndis->port);
631 if (IS_ERR(net))
632 return PTR_ERR(net);
633
634 rndis_set_param_dev(rndis->config, net,
635 &rndis->port.cdc_filter);
636 } else
637 goto fail;
638
639 return 0;
640fail:
641 return -EINVAL;
642}
643
644static void rndis_disable(struct usb_function *f)
645{
646 struct f_rndis *rndis = func_to_rndis(f);
647 struct usb_composite_dev *cdev = f->config->cdev;
648
649 if (!rndis->notify->driver_data)
650 return;
651
652 DBG(cdev, "rndis deactivated\n");
653
654 rndis_uninit(rndis->config);
655 gether_disconnect(&rndis->port);
656
657 usb_ep_disable(rndis->notify);
658 rndis->notify->driver_data = NULL;
659}
660
661/*-------------------------------------------------------------------------*/
662
663/*
664 * This isn't quite the same mechanism as CDC Ethernet, since the
665 * notification scheme passes less data, but the same set of link
666 * states must be tested. A key difference is that altsettings are
667 * not used to tell whether the link should send packets or not.
668 */
669
670static void rndis_open(struct gether *geth)
671{
672 struct f_rndis *rndis = func_to_rndis(&geth->func);
673 struct usb_composite_dev *cdev = geth->func.config->cdev;
674
675 DBG(cdev, "%s\n", __func__);
676
677 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
678 bitrate(cdev->gadget) / 100);
679 rndis_signal_connect(rndis->config);
680}
681
682static void rndis_close(struct gether *geth)
683{
684 struct f_rndis *rndis = func_to_rndis(&geth->func);
685
686 DBG(geth->func.config->cdev, "%s\n", __func__);
687
688 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
689 rndis_signal_disconnect(rndis->config);
690}
691
692/*-------------------------------------------------------------------------*/
693
694/* ethernet function driver setup/binding */
695
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200696static int
David Brownell45fe3b82008-06-19 18:20:04 -0700697rndis_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 int status;
702 struct usb_ep *ep;
703
704 /* allocate instance-specific interface IDs */
705 status = usb_interface_id(c, f);
706 if (status < 0)
707 goto fail;
708 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100709 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700710
711 rndis_control_intf.bInterfaceNumber = status;
712 rndis_union_desc.bMasterInterface0 = status;
713
714 status = usb_interface_id(c, f);
715 if (status < 0)
716 goto fail;
717 rndis->data_id = status;
718
719 rndis_data_intf.bInterfaceNumber = status;
720 rndis_union_desc.bSlaveInterface0 = status;
721
722 status = -ENODEV;
723
724 /* allocate instance-specific endpoints */
725 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
726 if (!ep)
727 goto fail;
728 rndis->port.in_ep = ep;
729 ep->driver_data = cdev; /* claim */
730
731 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
732 if (!ep)
733 goto fail;
734 rndis->port.out_ep = ep;
735 ep->driver_data = cdev; /* claim */
736
737 /* NOTE: a status/notification endpoint is, strictly speaking,
738 * optional. We don't treat it that way though! It's simpler,
739 * and some newer profiles don't treat it as optional.
740 */
741 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
742 if (!ep)
743 goto fail;
744 rndis->notify = ep;
745 ep->driver_data = cdev; /* claim */
746
747 status = -ENOMEM;
748
749 /* allocate notification request and buffer */
750 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
751 if (!rndis->notify_req)
752 goto fail;
753 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
754 if (!rndis->notify_req->buf)
755 goto fail;
756 rndis->notify_req->length = STATUS_BYTECOUNT;
757 rndis->notify_req->context = rndis;
758 rndis->notify_req->complete = rndis_response_complete;
759
760 /* copy descriptors, and track endpoint copies */
761 f->descriptors = usb_copy_descriptors(eth_fs_function);
762 if (!f->descriptors)
763 goto fail;
764
David Brownell45fe3b82008-06-19 18:20:04 -0700765 /* support all relevant hardware speeds... we expect that when
766 * hardware is dual speed, all bulk-capable endpoints work at
767 * both speeds
768 */
769 if (gadget_is_dualspeed(c->cdev->gadget)) {
770 hs_in_desc.bEndpointAddress =
771 fs_in_desc.bEndpointAddress;
772 hs_out_desc.bEndpointAddress =
773 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800774 hs_notify_desc.bEndpointAddress =
775 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700776
777 /* copy descriptors, and track endpoint copies */
778 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
David Brownell45fe3b82008-06-19 18:20:04 -0700779 if (!f->hs_descriptors)
780 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700781 }
782
Paul Zimmerman04617db2011-06-27 14:13:18 -0700783 if (gadget_is_superspeed(c->cdev->gadget)) {
784 ss_in_desc.bEndpointAddress =
785 fs_in_desc.bEndpointAddress;
786 ss_out_desc.bEndpointAddress =
787 fs_out_desc.bEndpointAddress;
788 ss_notify_desc.bEndpointAddress =
789 fs_notify_desc.bEndpointAddress;
790
791 /* copy descriptors, and track endpoint copies */
792 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
793 if (!f->ss_descriptors)
794 goto fail;
795 }
796
David Brownell45fe3b82008-06-19 18:20:04 -0700797 rndis->port.open = rndis_open;
798 rndis->port.close = rndis_close;
799
800 status = rndis_register(rndis_response_available, rndis);
801 if (status < 0)
802 goto fail;
803 rndis->config = status;
804
805 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
806 rndis_set_host_mac(rndis->config, rndis->ethaddr);
Vamsi Krishna7f21ac82013-04-17 21:41:33 -0700807 rndis_set_max_pkt_xfer(rndis->config, rndis_ul_max_pkt_per_xfer);
David Brownell45fe3b82008-06-19 18:20:04 -0700808
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800809 if (rndis->manufacturer && rndis->vendorID &&
810 rndis_set_param_vendor(rndis->config, rndis->vendorID,
811 rndis->manufacturer))
812 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700813
814 /* NOTE: all that is done without knowing or caring about
815 * the network link ... which is unavailable to this code
816 * until we're activated via set_alt().
817 */
818
819 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700820 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell45fe3b82008-06-19 18:20:04 -0700821 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
822 rndis->port.in_ep->name, rndis->port.out_ep->name,
823 rndis->notify->name);
824 return 0;
825
826fail:
Paul Zimmerman04617db2011-06-27 14:13:18 -0700827 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
828 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700829 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
830 usb_free_descriptors(f->hs_descriptors);
831 if (f->descriptors)
832 usb_free_descriptors(f->descriptors);
833
834 if (rndis->notify_req) {
835 kfree(rndis->notify_req->buf);
836 usb_ep_free_request(rndis->notify, rndis->notify_req);
837 }
838
839 /* we might as well release our claims on endpoints */
840 if (rndis->notify)
841 rndis->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300842 if (rndis->port.out_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700843 rndis->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300844 if (rndis->port.in_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700845 rndis->port.in_ep->driver_data = NULL;
846
847 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
848
849 return status;
850}
851
852static void
853rndis_unbind(struct usb_configuration *c, struct usb_function *f)
854{
855 struct f_rndis *rndis = func_to_rndis(f);
856
857 rndis_deregister(rndis->config);
858 rndis_exit();
859
Paul Zimmerman04617db2011-06-27 14:13:18 -0700860 if (gadget_is_superspeed(c->cdev->gadget))
861 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700862 if (gadget_is_dualspeed(c->cdev->gadget))
863 usb_free_descriptors(f->hs_descriptors);
864 usb_free_descriptors(f->descriptors);
865
866 kfree(rndis->notify_req->buf);
867 usb_ep_free_request(rndis->notify, rndis->notify_req);
868
869 kfree(rndis);
870}
871
872/* Some controllers can't support RNDIS ... */
873static inline bool can_support_rndis(struct usb_configuration *c)
874{
David Brownell45fe3b82008-06-19 18:20:04 -0700875 /* everything else is *presumably* fine */
876 return true;
877}
878
879/**
880 * rndis_bind_config - add RNDIS network link to a configuration
881 * @c: the configuration to support the network link
882 * @ethaddr: a buffer in which the ethernet address of the host side
883 * side of the link was recorded
884 * Context: single threaded during gadget setup
885 *
886 * Returns zero on success, else negative errno.
887 *
888 * Caller must have called @gether_setup(). Caller is also responsible
889 * for calling @gether_cleanup() before module unload.
890 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200891int
892rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700893{
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800894 return rndis_bind_config_vendor(c, ethaddr, 0, NULL);
895}
896
897int
898rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
899 u32 vendorID, const char *manufacturer)
900{
David Brownell45fe3b82008-06-19 18:20:04 -0700901 struct f_rndis *rndis;
902 int status;
903
904 if (!can_support_rndis(c) || !ethaddr)
905 return -EINVAL;
906
Benoit Gobya6ccb732012-01-20 14:42:41 -0800907 /* setup RNDIS itself */
908 status = rndis_init();
909 if (status < 0)
910 return status;
911
David Brownell45fe3b82008-06-19 18:20:04 -0700912 /* maybe allocate device-global string IDs */
913 if (rndis_string_defs[0].id == 0) {
914
David Brownell45fe3b82008-06-19 18:20:04 -0700915 /* control interface label */
916 status = usb_string_id(c->cdev);
917 if (status < 0)
918 return status;
919 rndis_string_defs[0].id = status;
920 rndis_control_intf.iInterface = status;
921
922 /* data interface label */
923 status = usb_string_id(c->cdev);
924 if (status < 0)
925 return status;
926 rndis_string_defs[1].id = status;
927 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100928
929 /* IAD iFunction label */
930 status = usb_string_id(c->cdev);
931 if (status < 0)
932 return status;
933 rndis_string_defs[2].id = status;
934 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700935 }
936
937 /* allocate and initialize one new instance */
938 status = -ENOMEM;
939 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
940 if (!rndis)
941 goto fail;
942
943 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800944 rndis->vendorID = vendorID;
945 rndis->manufacturer = manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -0700946
947 /* RNDIS activates when the host changes this filter */
948 rndis->port.cdc_filter = 0;
949
950 /* RNDIS has special (and complex) framing */
951 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
952 rndis->port.wrap = rndis_add_header;
953 rndis->port.unwrap = rndis_rm_hdr;
Vamsi Krishna7f21ac82013-04-17 21:41:33 -0700954 rndis->port.ul_max_pkts_per_xfer = rndis_ul_max_pkt_per_xfer;
Vijayavardhan Vennapusac76dced2013-05-27 15:00:52 +0530955 rndis->port.dl_max_pkts_per_xfer = rndis_dl_max_pkt_per_xfer;
David Brownell45fe3b82008-06-19 18:20:04 -0700956
957 rndis->port.func.name = "rndis";
958 rndis->port.func.strings = rndis_strings;
959 /* descriptors are per-instance copies */
960 rndis->port.func.bind = rndis_bind;
961 rndis->port.func.unbind = rndis_unbind;
962 rndis->port.func.set_alt = rndis_set_alt;
963 rndis->port.func.setup = rndis_setup;
964 rndis->port.func.disable = rndis_disable;
965
966 status = usb_add_function(c, &rndis->port.func);
967 if (status) {
968 kfree(rndis);
969fail:
970 rndis_exit();
971 }
972 return status;
973}