blob: c0c3280a0b619f7807573941619258e42a4084b3 [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,
136 .bInterfaceSubClass = 1,
137 .bInterfaceProtocol = 3,
138#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
201 .bFunctionClass = USB_CLASS_COMM,
202 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
203 .bFunctionProtocol = USB_CDC_PROTO_NONE,
204 /* .iFunction = DYNAMIC */
205};
206
David Brownell45fe3b82008-06-19 18:20:04 -0700207/* full speed support: */
208
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200209static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212
213 .bEndpointAddress = USB_DIR_IN,
214 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800215 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700216 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
217};
218
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200219static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700220 .bLength = USB_DT_ENDPOINT_SIZE,
221 .bDescriptorType = USB_DT_ENDPOINT,
222
223 .bEndpointAddress = USB_DIR_IN,
224 .bmAttributes = USB_ENDPOINT_XFER_BULK,
225};
226
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200227static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
230
231 .bEndpointAddress = USB_DIR_OUT,
232 .bmAttributes = USB_ENDPOINT_XFER_BULK,
233};
234
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200235static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100236 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700237 /* control interface matches ACM, not Ethernet */
238 (struct usb_descriptor_header *) &rndis_control_intf,
239 (struct usb_descriptor_header *) &header_desc,
240 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100241 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700242 (struct usb_descriptor_header *) &rndis_union_desc,
243 (struct usb_descriptor_header *) &fs_notify_desc,
244 /* data interface has no altsetting */
245 (struct usb_descriptor_header *) &rndis_data_intf,
246 (struct usb_descriptor_header *) &fs_in_desc,
247 (struct usb_descriptor_header *) &fs_out_desc,
248 NULL,
249};
250
251/* high speed support: */
252
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200253static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
256
257 .bEndpointAddress = USB_DIR_IN,
258 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800259 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700260 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
261};
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200262static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700263 .bLength = USB_DT_ENDPOINT_SIZE,
264 .bDescriptorType = USB_DT_ENDPOINT,
265
266 .bEndpointAddress = USB_DIR_IN,
267 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800268 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700269};
270
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200271static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700272 .bLength = USB_DT_ENDPOINT_SIZE,
273 .bDescriptorType = USB_DT_ENDPOINT,
274
275 .bEndpointAddress = USB_DIR_OUT,
276 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800277 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700278};
279
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200280static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100281 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700282 /* control interface matches ACM, not Ethernet */
283 (struct usb_descriptor_header *) &rndis_control_intf,
284 (struct usb_descriptor_header *) &header_desc,
285 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100286 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700287 (struct usb_descriptor_header *) &rndis_union_desc,
288 (struct usb_descriptor_header *) &hs_notify_desc,
289 /* data interface has no altsetting */
290 (struct usb_descriptor_header *) &rndis_data_intf,
291 (struct usb_descriptor_header *) &hs_in_desc,
292 (struct usb_descriptor_header *) &hs_out_desc,
293 NULL,
294};
295
296/* string descriptors: */
297
298static struct usb_string rndis_string_defs[] = {
299 [0].s = "RNDIS Communications Control",
300 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100301 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700302 { } /* end of list */
303};
304
305static struct usb_gadget_strings rndis_string_table = {
306 .language = 0x0409, /* en-us */
307 .strings = rndis_string_defs,
308};
309
310static struct usb_gadget_strings *rndis_strings[] = {
311 &rndis_string_table,
312 NULL,
313};
314
Mike Lockwood0d511c42010-03-10 17:05:03 -0500315#ifdef CONFIG_USB_ANDROID_RNDIS
316static struct usb_ether_platform_data *rndis_pdata;
317#endif
318
David Brownell45fe3b82008-06-19 18:20:04 -0700319/*-------------------------------------------------------------------------*/
320
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500321static struct sk_buff *rndis_add_header(struct gether *port,
322 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700323{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500324 struct sk_buff *skb2;
325
326 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
327 if (skb2)
328 rndis_add_hdr(skb2);
329
330 dev_kfree_skb_any(skb);
331 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700332}
333
334static void rndis_response_available(void *_rndis)
335{
336 struct f_rndis *rndis = _rndis;
337 struct usb_request *req = rndis->notify_req;
338 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
339 __le32 *data = req->buf;
340 int status;
341
Richard Röjforsff349502008-11-15 19:53:24 -0800342 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700343 return;
344
345 /* Send RNDIS RESPONSE_AVAILABLE notification; a
346 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
347 *
348 * This is the only notification defined by RNDIS.
349 */
350 data[0] = cpu_to_le32(1);
351 data[1] = cpu_to_le32(0);
352
353 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
354 if (status) {
355 atomic_dec(&rndis->notify_count);
356 DBG(cdev, "notify/0 --> %d\n", status);
357 }
358}
359
360static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
361{
362 struct f_rndis *rndis = req->context;
363 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
364 int status = req->status;
365
366 /* after TX:
367 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
368 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
369 */
370 switch (status) {
371 case -ECONNRESET:
372 case -ESHUTDOWN:
373 /* connection gone */
374 atomic_set(&rndis->notify_count, 0);
375 break;
376 default:
377 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
378 ep->name, status,
379 req->actual, req->length);
380 /* FALLTHROUGH */
381 case 0:
382 if (ep != rndis->notify)
383 break;
384
385 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
386 * notifications by resending until we're done
387 */
388 if (atomic_dec_and_test(&rndis->notify_count))
389 break;
390 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
391 if (status) {
392 atomic_dec(&rndis->notify_count);
393 DBG(cdev, "notify/1 --> %d\n", status);
394 }
395 break;
396 }
397}
398
399static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
400{
401 struct f_rndis *rndis = req->context;
402 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
403 int status;
404
405 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
406// spin_lock(&dev->lock);
407 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
408 if (status < 0)
409 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
410 status, req->actual, req->length);
411// spin_unlock(&dev->lock);
412}
413
414static int
415rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
416{
417 struct f_rndis *rndis = func_to_rndis(f);
418 struct usb_composite_dev *cdev = f->config->cdev;
419 struct usb_request *req = cdev->req;
420 int value = -EOPNOTSUPP;
421 u16 w_index = le16_to_cpu(ctrl->wIndex);
422 u16 w_value = le16_to_cpu(ctrl->wValue);
423 u16 w_length = le16_to_cpu(ctrl->wLength);
424
425 /* composite driver infrastructure handles everything except
426 * CDC class messages; interface activation uses set_alt().
427 */
428 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
429
430 /* RNDIS uses the CDC command encapsulation mechanism to implement
431 * an RPC scheme, with much getting/setting of attributes by OID.
432 */
433 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
434 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300435 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700436 goto invalid;
437 /* read the request; process it later */
438 value = w_length;
439 req->complete = rndis_command_complete;
440 req->context = rndis;
441 /* later, rndis_response_available() sends a notification */
442 break;
443
444 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
445 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
446 if (w_value || w_index != rndis->ctrl_id)
447 goto invalid;
448 else {
449 u8 *buf;
450 u32 n;
451
452 /* return the result */
453 buf = rndis_get_next_response(rndis->config, &n);
454 if (buf) {
455 memcpy(req->buf, buf, n);
456 req->complete = rndis_response_complete;
457 rndis_free_response(rndis->config, buf);
458 value = n;
459 }
460 /* else stalls ... spec says to avoid that */
461 }
462 break;
463
464 default:
465invalid:
466 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
467 ctrl->bRequestType, ctrl->bRequest,
468 w_value, w_index, w_length);
469 }
470
471 /* respond with data transfer or status phase? */
472 if (value >= 0) {
473 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
474 ctrl->bRequestType, ctrl->bRequest,
475 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700476 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700477 req->length = value;
478 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
479 if (value < 0)
480 ERROR(cdev, "rndis response on err %d\n", value);
481 }
482
483 /* device either stalls (value < 0) or reports success */
484 return value;
485}
486
487
488static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
489{
490 struct f_rndis *rndis = func_to_rndis(f);
491 struct usb_composite_dev *cdev = f->config->cdev;
492
493 /* we know alt == 0 */
494
495 if (intf == rndis->ctrl_id) {
496 if (rndis->notify->driver_data) {
497 VDBG(cdev, "reset rndis control %d\n", intf);
498 usb_ep_disable(rndis->notify);
499 } else {
500 VDBG(cdev, "init rndis ctrl %d\n", intf);
David Brownell45fe3b82008-06-19 18:20:04 -0700501 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800502 rndis->notify_desc = ep_choose(cdev->gadget,
503 rndis->hs.notify,
504 rndis->fs.notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700505 usb_ep_enable(rndis->notify, rndis->notify_desc);
506 rndis->notify->driver_data = rndis;
507
508 } else if (intf == rndis->data_id) {
509 struct net_device *net;
510
511 if (rndis->port.in_ep->driver_data) {
512 DBG(cdev, "reset rndis\n");
513 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530514 }
515
516 if (!rndis->port.in) {
David Brownell45fe3b82008-06-19 18:20:04 -0700517 DBG(cdev, "init rndis\n");
David Brownell45fe3b82008-06-19 18:20:04 -0700518 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800519 rndis->port.in = ep_choose(cdev->gadget,
520 rndis->hs.in, rndis->fs.in);
521 rndis->port.out = ep_choose(cdev->gadget,
522 rndis->hs.out, rndis->fs.out);
David Brownell45fe3b82008-06-19 18:20:04 -0700523
524 /* Avoid ZLPs; they can be troublesome. */
525 rndis->port.is_zlp_ok = false;
526
527 /* RNDIS should be in the "RNDIS uninitialized" state,
528 * either never activated or after rndis_uninit().
529 *
530 * We don't want data to flow here until a nonzero packet
531 * filter is set, at which point it enters "RNDIS data
532 * initialized" state ... but we do want the endpoints
533 * to be activated. It's a strange little state.
534 *
535 * REVISIT the RNDIS gadget code has done this wrong for a
536 * very long time. We need another call to the link layer
537 * code -- gether_updown(...bool) maybe -- to do it right.
538 */
539 rndis->port.cdc_filter = 0;
540
541 DBG(cdev, "RNDIS RX/TX early activation ... \n");
542 net = gether_connect(&rndis->port);
543 if (IS_ERR(net))
544 return PTR_ERR(net);
545
546 rndis_set_param_dev(rndis->config, net,
547 &rndis->port.cdc_filter);
548 } else
549 goto fail;
550
551 return 0;
552fail:
553 return -EINVAL;
554}
555
556static void rndis_disable(struct usb_function *f)
557{
558 struct f_rndis *rndis = func_to_rndis(f);
559 struct usb_composite_dev *cdev = f->config->cdev;
560
561 if (!rndis->notify->driver_data)
562 return;
563
564 DBG(cdev, "rndis deactivated\n");
565
566 rndis_uninit(rndis->config);
567 gether_disconnect(&rndis->port);
568
569 usb_ep_disable(rndis->notify);
570 rndis->notify->driver_data = NULL;
571}
572
573/*-------------------------------------------------------------------------*/
574
575/*
576 * This isn't quite the same mechanism as CDC Ethernet, since the
577 * notification scheme passes less data, but the same set of link
578 * states must be tested. A key difference is that altsettings are
579 * not used to tell whether the link should send packets or not.
580 */
581
582static void rndis_open(struct gether *geth)
583{
584 struct f_rndis *rndis = func_to_rndis(&geth->func);
585 struct usb_composite_dev *cdev = geth->func.config->cdev;
586
587 DBG(cdev, "%s\n", __func__);
588
589 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
590 bitrate(cdev->gadget) / 100);
591 rndis_signal_connect(rndis->config);
592}
593
594static void rndis_close(struct gether *geth)
595{
596 struct f_rndis *rndis = func_to_rndis(&geth->func);
597
598 DBG(geth->func.config->cdev, "%s\n", __func__);
599
600 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
601 rndis_signal_disconnect(rndis->config);
602}
603
604/*-------------------------------------------------------------------------*/
605
606/* ethernet function driver setup/binding */
607
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200608static int
David Brownell45fe3b82008-06-19 18:20:04 -0700609rndis_bind(struct usb_configuration *c, struct usb_function *f)
610{
611 struct usb_composite_dev *cdev = c->cdev;
612 struct f_rndis *rndis = func_to_rndis(f);
613 int status;
614 struct usb_ep *ep;
615
616 /* allocate instance-specific interface IDs */
617 status = usb_interface_id(c, f);
618 if (status < 0)
619 goto fail;
620 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100621 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700622
623 rndis_control_intf.bInterfaceNumber = status;
624 rndis_union_desc.bMasterInterface0 = status;
625
626 status = usb_interface_id(c, f);
627 if (status < 0)
628 goto fail;
629 rndis->data_id = status;
630
631 rndis_data_intf.bInterfaceNumber = status;
632 rndis_union_desc.bSlaveInterface0 = status;
633
634 status = -ENODEV;
635
636 /* allocate instance-specific endpoints */
637 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
638 if (!ep)
639 goto fail;
640 rndis->port.in_ep = ep;
641 ep->driver_data = cdev; /* claim */
642
643 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
644 if (!ep)
645 goto fail;
646 rndis->port.out_ep = ep;
647 ep->driver_data = cdev; /* claim */
648
649 /* NOTE: a status/notification endpoint is, strictly speaking,
650 * optional. We don't treat it that way though! It's simpler,
651 * and some newer profiles don't treat it as optional.
652 */
653 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
654 if (!ep)
655 goto fail;
656 rndis->notify = ep;
657 ep->driver_data = cdev; /* claim */
658
659 status = -ENOMEM;
660
661 /* allocate notification request and buffer */
662 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
663 if (!rndis->notify_req)
664 goto fail;
665 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
666 if (!rndis->notify_req->buf)
667 goto fail;
668 rndis->notify_req->length = STATUS_BYTECOUNT;
669 rndis->notify_req->context = rndis;
670 rndis->notify_req->complete = rndis_response_complete;
671
672 /* copy descriptors, and track endpoint copies */
673 f->descriptors = usb_copy_descriptors(eth_fs_function);
674 if (!f->descriptors)
675 goto fail;
676
677 rndis->fs.in = usb_find_endpoint(eth_fs_function,
678 f->descriptors, &fs_in_desc);
679 rndis->fs.out = usb_find_endpoint(eth_fs_function,
680 f->descriptors, &fs_out_desc);
681 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
682 f->descriptors, &fs_notify_desc);
683
684 /* support all relevant hardware speeds... we expect that when
685 * hardware is dual speed, all bulk-capable endpoints work at
686 * both speeds
687 */
688 if (gadget_is_dualspeed(c->cdev->gadget)) {
689 hs_in_desc.bEndpointAddress =
690 fs_in_desc.bEndpointAddress;
691 hs_out_desc.bEndpointAddress =
692 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800693 hs_notify_desc.bEndpointAddress =
694 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700695
696 /* copy descriptors, and track endpoint copies */
697 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
698
699 if (!f->hs_descriptors)
700 goto fail;
701
702 rndis->hs.in = usb_find_endpoint(eth_hs_function,
703 f->hs_descriptors, &hs_in_desc);
704 rndis->hs.out = usb_find_endpoint(eth_hs_function,
705 f->hs_descriptors, &hs_out_desc);
David Brownell7c124142008-11-24 23:11:03 -0800706 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
707 f->hs_descriptors, &hs_notify_desc);
David Brownell45fe3b82008-06-19 18:20:04 -0700708 }
709
710 rndis->port.open = rndis_open;
711 rndis->port.close = rndis_close;
712
713 status = rndis_register(rndis_response_available, rndis);
714 if (status < 0)
715 goto fail;
716 rndis->config = status;
717
718 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
719 rndis_set_host_mac(rndis->config, rndis->ethaddr);
720
Mike Lockwood0d511c42010-03-10 17:05:03 -0500721#ifdef CONFIG_USB_ANDROID_RNDIS
722 if (rndis_pdata) {
723 if (rndis_set_param_vendor(rndis->config, rndis_pdata->vendorID,
724 rndis_pdata->vendorDescr))
725 goto fail;
726 }
David Brownell45fe3b82008-06-19 18:20:04 -0700727#endif
728
729 /* NOTE: all that is done without knowing or caring about
730 * the network link ... which is unavailable to this code
731 * until we're activated via set_alt().
732 */
733
734 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
735 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
736 rndis->port.in_ep->name, rndis->port.out_ep->name,
737 rndis->notify->name);
738 return 0;
739
740fail:
741 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
742 usb_free_descriptors(f->hs_descriptors);
743 if (f->descriptors)
744 usb_free_descriptors(f->descriptors);
745
746 if (rndis->notify_req) {
747 kfree(rndis->notify_req->buf);
748 usb_ep_free_request(rndis->notify, rndis->notify_req);
749 }
750
751 /* we might as well release our claims on endpoints */
752 if (rndis->notify)
753 rndis->notify->driver_data = NULL;
754 if (rndis->port.out)
755 rndis->port.out_ep->driver_data = NULL;
756 if (rndis->port.in)
757 rndis->port.in_ep->driver_data = NULL;
758
759 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
760
761 return status;
762}
763
764static void
765rndis_unbind(struct usb_configuration *c, struct usb_function *f)
766{
767 struct f_rndis *rndis = func_to_rndis(f);
768
769 rndis_deregister(rndis->config);
770 rndis_exit();
771
772 if (gadget_is_dualspeed(c->cdev->gadget))
773 usb_free_descriptors(f->hs_descriptors);
774 usb_free_descriptors(f->descriptors);
775
776 kfree(rndis->notify_req->buf);
777 usb_ep_free_request(rndis->notify, rndis->notify_req);
778
779 kfree(rndis);
780}
781
782/* Some controllers can't support RNDIS ... */
783static inline bool can_support_rndis(struct usb_configuration *c)
784{
David Brownell45fe3b82008-06-19 18:20:04 -0700785 /* everything else is *presumably* fine */
786 return true;
787}
788
789/**
790 * rndis_bind_config - add RNDIS network link to a configuration
791 * @c: the configuration to support the network link
792 * @ethaddr: a buffer in which the ethernet address of the host side
793 * side of the link was recorded
794 * Context: single threaded during gadget setup
795 *
796 * Returns zero on success, else negative errno.
797 *
798 * Caller must have called @gether_setup(). Caller is also responsible
799 * for calling @gether_cleanup() before module unload.
800 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200801int
802rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700803{
804 struct f_rndis *rndis;
805 int status;
806
807 if (!can_support_rndis(c) || !ethaddr)
808 return -EINVAL;
809
810 /* maybe allocate device-global string IDs */
811 if (rndis_string_defs[0].id == 0) {
812
813 /* ... and setup RNDIS itself */
814 status = rndis_init();
815 if (status < 0)
816 return status;
817
818 /* control interface label */
819 status = usb_string_id(c->cdev);
820 if (status < 0)
821 return status;
822 rndis_string_defs[0].id = status;
823 rndis_control_intf.iInterface = status;
824
825 /* data interface label */
826 status = usb_string_id(c->cdev);
827 if (status < 0)
828 return status;
829 rndis_string_defs[1].id = status;
830 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100831
832 /* IAD iFunction label */
833 status = usb_string_id(c->cdev);
834 if (status < 0)
835 return status;
836 rndis_string_defs[2].id = status;
837 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700838 }
839
840 /* allocate and initialize one new instance */
841 status = -ENOMEM;
842 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
843 if (!rndis)
844 goto fail;
845
846 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
847
848 /* RNDIS activates when the host changes this filter */
849 rndis->port.cdc_filter = 0;
850
851 /* RNDIS has special (and complex) framing */
852 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
853 rndis->port.wrap = rndis_add_header;
854 rndis->port.unwrap = rndis_rm_hdr;
855
856 rndis->port.func.name = "rndis";
857 rndis->port.func.strings = rndis_strings;
858 /* descriptors are per-instance copies */
859 rndis->port.func.bind = rndis_bind;
860 rndis->port.func.unbind = rndis_unbind;
861 rndis->port.func.set_alt = rndis_set_alt;
862 rndis->port.func.setup = rndis_setup;
863 rndis->port.func.disable = rndis_disable;
864
Mike Lockwoodfe8ae892010-02-08 05:33:54 -0500865#ifdef CONFIG_USB_ANDROID_RNDIS
866 /* start disabled */
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400867 rndis->port.func.disabled = 1;
Mike Lockwoodfe8ae892010-02-08 05:33:54 -0500868#endif
869
David Brownell45fe3b82008-06-19 18:20:04 -0700870 status = usb_add_function(c, &rndis->port.func);
871 if (status) {
872 kfree(rndis);
873fail:
874 rndis_exit();
875 }
876 return status;
877}
Mike Lockwood789ef232010-01-12 10:33:59 -0800878
879#ifdef CONFIG_USB_ANDROID_RNDIS
880#include "rndis.c"
881
Mike Lockwood0d511c42010-03-10 17:05:03 -0500882static int __init rndis_probe(struct platform_device *pdev)
883{
884 rndis_pdata = pdev->dev.platform_data;
885 return 0;
886}
Mike Lockwood789ef232010-01-12 10:33:59 -0800887
Mike Lockwood0d511c42010-03-10 17:05:03 -0500888static struct platform_driver rndis_platform_driver = {
889 .driver = { .name = "rndis", },
890 .probe = rndis_probe,
891};
Mike Lockwood789ef232010-01-12 10:33:59 -0800892
893int rndis_function_bind_config(struct usb_configuration *c)
894{
Mike Lockwood0d511c42010-03-10 17:05:03 -0500895 int ret;
896
897 if (!rndis_pdata) {
898 printk(KERN_ERR "rndis_pdata null in rndis_function_bind_config\n");
899 return -1;
900 }
901
902 printk(KERN_INFO
903 "rndis_function_bind_config MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
904 rndis_pdata->ethaddr[0], rndis_pdata->ethaddr[1],
905 rndis_pdata->ethaddr[2], rndis_pdata->ethaddr[3],
906 rndis_pdata->ethaddr[4], rndis_pdata->ethaddr[5]);
907
908 ret = gether_setup(c->cdev->gadget, rndis_pdata->ethaddr);
Mike Lockwood789ef232010-01-12 10:33:59 -0800909 if (ret == 0)
Mike Lockwood0d511c42010-03-10 17:05:03 -0500910 ret = rndis_bind_config(c, rndis_pdata->ethaddr);
Mike Lockwood789ef232010-01-12 10:33:59 -0800911 return ret;
912}
913
914static struct android_usb_function rndis_function = {
915 .name = "rndis",
916 .bind_config = rndis_function_bind_config,
917};
918
919static int __init init(void)
920{
921 printk(KERN_INFO "f_rndis init\n");
Mike Lockwood0d511c42010-03-10 17:05:03 -0500922 platform_driver_register(&rndis_platform_driver);
Mike Lockwood789ef232010-01-12 10:33:59 -0800923 android_register_function(&rndis_function);
924 return 0;
925}
926module_init(init);
927
928#endif /* CONFIG_USB_ANDROID_RNDIS */