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