blob: 39d2df570e88415948e158def55a5de6bb6b9e7f [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>
29#include <linux/device.h>
30#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,
133 .bInterfaceClass = USB_CLASS_COMM,
134 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
135 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
136 /* .iInterface = DYNAMIC */
137};
138
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200139static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700140 .bLength = sizeof header_desc,
141 .bDescriptorType = USB_DT_CS_INTERFACE,
142 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
143
Harvey Harrison551509d2009-02-11 14:11:36 -0800144 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700145};
146
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200147static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700148 .bLength = sizeof call_mgmt_descriptor,
149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
151
152 .bmCapabilities = 0x00,
153 .bDataInterface = 0x01,
154};
155
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200156static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100157 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700158 .bDescriptorType = USB_DT_CS_INTERFACE,
159 .bDescriptorSubType = USB_CDC_ACM_TYPE,
160
161 .bmCapabilities = 0x00,
162};
163
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200164static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700165 .bLength = sizeof(rndis_union_desc),
166 .bDescriptorType = USB_DT_CS_INTERFACE,
167 .bDescriptorSubType = USB_CDC_UNION_TYPE,
168 /* .bMasterInterface0 = DYNAMIC */
169 /* .bSlaveInterface0 = DYNAMIC */
170};
171
172/* the data interface has two bulk endpoints */
173
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200174static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700175 .bLength = sizeof rndis_data_intf,
176 .bDescriptorType = USB_DT_INTERFACE,
177
178 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700179 .bNumEndpoints = 2,
180 .bInterfaceClass = USB_CLASS_CDC_DATA,
181 .bInterfaceSubClass = 0,
182 .bInterfaceProtocol = 0,
183 /* .iInterface = DYNAMIC */
184};
185
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100186
187static struct usb_interface_assoc_descriptor
188rndis_iad_descriptor = {
189 .bLength = sizeof rndis_iad_descriptor,
190 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
191
192 .bFirstInterface = 0, /* XXX, hardcoded */
193 .bInterfaceCount = 2, // control + data
194 .bFunctionClass = USB_CLASS_COMM,
195 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
196 .bFunctionProtocol = USB_CDC_PROTO_NONE,
197 /* .iFunction = DYNAMIC */
198};
199
David Brownell45fe3b82008-06-19 18:20:04 -0700200/* full speed support: */
201
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200202static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
205
206 .bEndpointAddress = USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800208 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700209 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
210};
211
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200212static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700213 .bLength = USB_DT_ENDPOINT_SIZE,
214 .bDescriptorType = USB_DT_ENDPOINT,
215
216 .bEndpointAddress = USB_DIR_IN,
217 .bmAttributes = USB_ENDPOINT_XFER_BULK,
218};
219
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200220static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700221 .bLength = USB_DT_ENDPOINT_SIZE,
222 .bDescriptorType = USB_DT_ENDPOINT,
223
224 .bEndpointAddress = USB_DIR_OUT,
225 .bmAttributes = USB_ENDPOINT_XFER_BULK,
226};
227
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200228static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100229 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700230 /* control interface matches ACM, not Ethernet */
231 (struct usb_descriptor_header *) &rndis_control_intf,
232 (struct usb_descriptor_header *) &header_desc,
233 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100234 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700235 (struct usb_descriptor_header *) &rndis_union_desc,
236 (struct usb_descriptor_header *) &fs_notify_desc,
237 /* data interface has no altsetting */
238 (struct usb_descriptor_header *) &rndis_data_intf,
239 (struct usb_descriptor_header *) &fs_in_desc,
240 (struct usb_descriptor_header *) &fs_out_desc,
241 NULL,
242};
243
244/* high speed support: */
245
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200246static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700247 .bLength = USB_DT_ENDPOINT_SIZE,
248 .bDescriptorType = USB_DT_ENDPOINT,
249
250 .bEndpointAddress = USB_DIR_IN,
251 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800252 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700253 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
254};
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200255static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
258
259 .bEndpointAddress = USB_DIR_IN,
260 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800261 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700262};
263
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200264static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700265 .bLength = USB_DT_ENDPOINT_SIZE,
266 .bDescriptorType = USB_DT_ENDPOINT,
267
268 .bEndpointAddress = USB_DIR_OUT,
269 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800270 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700271};
272
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200273static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100274 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700275 /* control interface matches ACM, not Ethernet */
276 (struct usb_descriptor_header *) &rndis_control_intf,
277 (struct usb_descriptor_header *) &header_desc,
278 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100279 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700280 (struct usb_descriptor_header *) &rndis_union_desc,
281 (struct usb_descriptor_header *) &hs_notify_desc,
282 /* data interface has no altsetting */
283 (struct usb_descriptor_header *) &rndis_data_intf,
284 (struct usb_descriptor_header *) &hs_in_desc,
285 (struct usb_descriptor_header *) &hs_out_desc,
286 NULL,
287};
288
289/* string descriptors: */
290
291static struct usb_string rndis_string_defs[] = {
292 [0].s = "RNDIS Communications Control",
293 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100294 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700295 { } /* end of list */
296};
297
298static struct usb_gadget_strings rndis_string_table = {
299 .language = 0x0409, /* en-us */
300 .strings = rndis_string_defs,
301};
302
303static struct usb_gadget_strings *rndis_strings[] = {
304 &rndis_string_table,
305 NULL,
306};
307
308/*-------------------------------------------------------------------------*/
309
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500310static struct sk_buff *rndis_add_header(struct gether *port,
311 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700312{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500313 struct sk_buff *skb2;
314
315 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
316 if (skb2)
317 rndis_add_hdr(skb2);
318
319 dev_kfree_skb_any(skb);
320 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700321}
322
323static void rndis_response_available(void *_rndis)
324{
325 struct f_rndis *rndis = _rndis;
326 struct usb_request *req = rndis->notify_req;
327 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
328 __le32 *data = req->buf;
329 int status;
330
Richard Röjforsff349502008-11-15 19:53:24 -0800331 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700332 return;
333
334 /* Send RNDIS RESPONSE_AVAILABLE notification; a
335 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
336 *
337 * This is the only notification defined by RNDIS.
338 */
339 data[0] = cpu_to_le32(1);
340 data[1] = cpu_to_le32(0);
341
342 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
343 if (status) {
344 atomic_dec(&rndis->notify_count);
345 DBG(cdev, "notify/0 --> %d\n", status);
346 }
347}
348
349static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
350{
351 struct f_rndis *rndis = req->context;
352 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
353 int status = req->status;
354
355 /* after TX:
356 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
357 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
358 */
359 switch (status) {
360 case -ECONNRESET:
361 case -ESHUTDOWN:
362 /* connection gone */
363 atomic_set(&rndis->notify_count, 0);
364 break;
365 default:
366 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
367 ep->name, status,
368 req->actual, req->length);
369 /* FALLTHROUGH */
370 case 0:
371 if (ep != rndis->notify)
372 break;
373
374 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
375 * notifications by resending until we're done
376 */
377 if (atomic_dec_and_test(&rndis->notify_count))
378 break;
379 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
380 if (status) {
381 atomic_dec(&rndis->notify_count);
382 DBG(cdev, "notify/1 --> %d\n", status);
383 }
384 break;
385 }
386}
387
388static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
389{
390 struct f_rndis *rndis = req->context;
391 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
392 int status;
393
394 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
395// spin_lock(&dev->lock);
396 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
397 if (status < 0)
398 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
399 status, req->actual, req->length);
400// spin_unlock(&dev->lock);
401}
402
403static int
404rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
405{
406 struct f_rndis *rndis = func_to_rndis(f);
407 struct usb_composite_dev *cdev = f->config->cdev;
408 struct usb_request *req = cdev->req;
409 int value = -EOPNOTSUPP;
410 u16 w_index = le16_to_cpu(ctrl->wIndex);
411 u16 w_value = le16_to_cpu(ctrl->wValue);
412 u16 w_length = le16_to_cpu(ctrl->wLength);
413
414 /* composite driver infrastructure handles everything except
415 * CDC class messages; interface activation uses set_alt().
416 */
417 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
418
419 /* RNDIS uses the CDC command encapsulation mechanism to implement
420 * an RPC scheme, with much getting/setting of attributes by OID.
421 */
422 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
423 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300424 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700425 goto invalid;
426 /* read the request; process it later */
427 value = w_length;
428 req->complete = rndis_command_complete;
429 req->context = rndis;
430 /* later, rndis_response_available() sends a notification */
431 break;
432
433 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
434 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
435 if (w_value || w_index != rndis->ctrl_id)
436 goto invalid;
437 else {
438 u8 *buf;
439 u32 n;
440
441 /* return the result */
442 buf = rndis_get_next_response(rndis->config, &n);
443 if (buf) {
444 memcpy(req->buf, buf, n);
445 req->complete = rndis_response_complete;
446 rndis_free_response(rndis->config, buf);
447 value = n;
448 }
449 /* else stalls ... spec says to avoid that */
450 }
451 break;
452
453 default:
454invalid:
455 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
456 ctrl->bRequestType, ctrl->bRequest,
457 w_value, w_index, w_length);
458 }
459
460 /* respond with data transfer or status phase? */
461 if (value >= 0) {
462 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
463 ctrl->bRequestType, ctrl->bRequest,
464 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700465 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700466 req->length = value;
467 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
468 if (value < 0)
469 ERROR(cdev, "rndis response on err %d\n", value);
470 }
471
472 /* device either stalls (value < 0) or reports success */
473 return value;
474}
475
476
477static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
478{
479 struct f_rndis *rndis = func_to_rndis(f);
480 struct usb_composite_dev *cdev = f->config->cdev;
481
482 /* we know alt == 0 */
483
484 if (intf == rndis->ctrl_id) {
485 if (rndis->notify->driver_data) {
486 VDBG(cdev, "reset rndis control %d\n", intf);
487 usb_ep_disable(rndis->notify);
488 } else {
489 VDBG(cdev, "init rndis ctrl %d\n", intf);
David Brownell45fe3b82008-06-19 18:20:04 -0700490 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800491 rndis->notify_desc = ep_choose(cdev->gadget,
492 rndis->hs.notify,
493 rndis->fs.notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700494 usb_ep_enable(rndis->notify, rndis->notify_desc);
495 rndis->notify->driver_data = rndis;
496
497 } else if (intf == rndis->data_id) {
498 struct net_device *net;
499
500 if (rndis->port.in_ep->driver_data) {
501 DBG(cdev, "reset rndis\n");
502 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530503 }
504
505 if (!rndis->port.in) {
David Brownell45fe3b82008-06-19 18:20:04 -0700506 DBG(cdev, "init rndis\n");
David Brownell45fe3b82008-06-19 18:20:04 -0700507 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800508 rndis->port.in = ep_choose(cdev->gadget,
509 rndis->hs.in, rndis->fs.in);
510 rndis->port.out = ep_choose(cdev->gadget,
511 rndis->hs.out, rndis->fs.out);
David Brownell45fe3b82008-06-19 18:20:04 -0700512
513 /* Avoid ZLPs; they can be troublesome. */
514 rndis->port.is_zlp_ok = false;
515
516 /* RNDIS should be in the "RNDIS uninitialized" state,
517 * either never activated or after rndis_uninit().
518 *
519 * We don't want data to flow here until a nonzero packet
520 * filter is set, at which point it enters "RNDIS data
521 * initialized" state ... but we do want the endpoints
522 * to be activated. It's a strange little state.
523 *
524 * REVISIT the RNDIS gadget code has done this wrong for a
525 * very long time. We need another call to the link layer
526 * code -- gether_updown(...bool) maybe -- to do it right.
527 */
528 rndis->port.cdc_filter = 0;
529
530 DBG(cdev, "RNDIS RX/TX early activation ... \n");
531 net = gether_connect(&rndis->port);
532 if (IS_ERR(net))
533 return PTR_ERR(net);
534
535 rndis_set_param_dev(rndis->config, net,
536 &rndis->port.cdc_filter);
537 } else
538 goto fail;
539
540 return 0;
541fail:
542 return -EINVAL;
543}
544
545static void rndis_disable(struct usb_function *f)
546{
547 struct f_rndis *rndis = func_to_rndis(f);
548 struct usb_composite_dev *cdev = f->config->cdev;
549
550 if (!rndis->notify->driver_data)
551 return;
552
553 DBG(cdev, "rndis deactivated\n");
554
555 rndis_uninit(rndis->config);
556 gether_disconnect(&rndis->port);
557
558 usb_ep_disable(rndis->notify);
559 rndis->notify->driver_data = NULL;
560}
561
562/*-------------------------------------------------------------------------*/
563
564/*
565 * This isn't quite the same mechanism as CDC Ethernet, since the
566 * notification scheme passes less data, but the same set of link
567 * states must be tested. A key difference is that altsettings are
568 * not used to tell whether the link should send packets or not.
569 */
570
571static void rndis_open(struct gether *geth)
572{
573 struct f_rndis *rndis = func_to_rndis(&geth->func);
574 struct usb_composite_dev *cdev = geth->func.config->cdev;
575
576 DBG(cdev, "%s\n", __func__);
577
578 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
579 bitrate(cdev->gadget) / 100);
580 rndis_signal_connect(rndis->config);
581}
582
583static void rndis_close(struct gether *geth)
584{
585 struct f_rndis *rndis = func_to_rndis(&geth->func);
586
587 DBG(geth->func.config->cdev, "%s\n", __func__);
588
589 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
590 rndis_signal_disconnect(rndis->config);
591}
592
593/*-------------------------------------------------------------------------*/
594
595/* ethernet function driver setup/binding */
596
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200597static int
David Brownell45fe3b82008-06-19 18:20:04 -0700598rndis_bind(struct usb_configuration *c, struct usb_function *f)
599{
600 struct usb_composite_dev *cdev = c->cdev;
601 struct f_rndis *rndis = func_to_rndis(f);
602 int status;
603 struct usb_ep *ep;
604
605 /* allocate instance-specific interface IDs */
606 status = usb_interface_id(c, f);
607 if (status < 0)
608 goto fail;
609 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100610 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700611
612 rndis_control_intf.bInterfaceNumber = status;
613 rndis_union_desc.bMasterInterface0 = status;
614
615 status = usb_interface_id(c, f);
616 if (status < 0)
617 goto fail;
618 rndis->data_id = status;
619
620 rndis_data_intf.bInterfaceNumber = status;
621 rndis_union_desc.bSlaveInterface0 = status;
622
623 status = -ENODEV;
624
625 /* allocate instance-specific endpoints */
626 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
627 if (!ep)
628 goto fail;
629 rndis->port.in_ep = ep;
630 ep->driver_data = cdev; /* claim */
631
632 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
633 if (!ep)
634 goto fail;
635 rndis->port.out_ep = ep;
636 ep->driver_data = cdev; /* claim */
637
638 /* NOTE: a status/notification endpoint is, strictly speaking,
639 * optional. We don't treat it that way though! It's simpler,
640 * and some newer profiles don't treat it as optional.
641 */
642 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
643 if (!ep)
644 goto fail;
645 rndis->notify = ep;
646 ep->driver_data = cdev; /* claim */
647
648 status = -ENOMEM;
649
650 /* allocate notification request and buffer */
651 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
652 if (!rndis->notify_req)
653 goto fail;
654 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
655 if (!rndis->notify_req->buf)
656 goto fail;
657 rndis->notify_req->length = STATUS_BYTECOUNT;
658 rndis->notify_req->context = rndis;
659 rndis->notify_req->complete = rndis_response_complete;
660
661 /* copy descriptors, and track endpoint copies */
662 f->descriptors = usb_copy_descriptors(eth_fs_function);
663 if (!f->descriptors)
664 goto fail;
665
666 rndis->fs.in = usb_find_endpoint(eth_fs_function,
667 f->descriptors, &fs_in_desc);
668 rndis->fs.out = usb_find_endpoint(eth_fs_function,
669 f->descriptors, &fs_out_desc);
670 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
671 f->descriptors, &fs_notify_desc);
672
673 /* support all relevant hardware speeds... we expect that when
674 * hardware is dual speed, all bulk-capable endpoints work at
675 * both speeds
676 */
677 if (gadget_is_dualspeed(c->cdev->gadget)) {
678 hs_in_desc.bEndpointAddress =
679 fs_in_desc.bEndpointAddress;
680 hs_out_desc.bEndpointAddress =
681 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800682 hs_notify_desc.bEndpointAddress =
683 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700684
685 /* copy descriptors, and track endpoint copies */
686 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
687
688 if (!f->hs_descriptors)
689 goto fail;
690
691 rndis->hs.in = usb_find_endpoint(eth_hs_function,
692 f->hs_descriptors, &hs_in_desc);
693 rndis->hs.out = usb_find_endpoint(eth_hs_function,
694 f->hs_descriptors, &hs_out_desc);
David Brownell7c124142008-11-24 23:11:03 -0800695 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
696 f->hs_descriptors, &hs_notify_desc);
David Brownell45fe3b82008-06-19 18:20:04 -0700697 }
698
699 rndis->port.open = rndis_open;
700 rndis->port.close = rndis_close;
701
702 status = rndis_register(rndis_response_available, rndis);
703 if (status < 0)
704 goto fail;
705 rndis->config = status;
706
707 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
708 rndis_set_host_mac(rndis->config, rndis->ethaddr);
709
710#if 0
711// FIXME
712 if (rndis_set_param_vendor(rndis->config, vendorID,
713 manufacturer))
714 goto fail0;
715#endif
716
717 /* NOTE: all that is done without knowing or caring about
718 * the network link ... which is unavailable to this code
719 * until we're activated via set_alt().
720 */
721
722 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
723 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
724 rndis->port.in_ep->name, rndis->port.out_ep->name,
725 rndis->notify->name);
726 return 0;
727
728fail:
729 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
730 usb_free_descriptors(f->hs_descriptors);
731 if (f->descriptors)
732 usb_free_descriptors(f->descriptors);
733
734 if (rndis->notify_req) {
735 kfree(rndis->notify_req->buf);
736 usb_ep_free_request(rndis->notify, rndis->notify_req);
737 }
738
739 /* we might as well release our claims on endpoints */
740 if (rndis->notify)
741 rndis->notify->driver_data = NULL;
742 if (rndis->port.out)
743 rndis->port.out_ep->driver_data = NULL;
744 if (rndis->port.in)
745 rndis->port.in_ep->driver_data = NULL;
746
747 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
748
749 return status;
750}
751
752static void
753rndis_unbind(struct usb_configuration *c, struct usb_function *f)
754{
755 struct f_rndis *rndis = func_to_rndis(f);
756
757 rndis_deregister(rndis->config);
758 rndis_exit();
759
760 if (gadget_is_dualspeed(c->cdev->gadget))
761 usb_free_descriptors(f->hs_descriptors);
762 usb_free_descriptors(f->descriptors);
763
764 kfree(rndis->notify_req->buf);
765 usb_ep_free_request(rndis->notify, rndis->notify_req);
766
767 kfree(rndis);
768}
769
770/* Some controllers can't support RNDIS ... */
771static inline bool can_support_rndis(struct usb_configuration *c)
772{
David Brownell45fe3b82008-06-19 18:20:04 -0700773 /* everything else is *presumably* fine */
774 return true;
775}
776
777/**
778 * rndis_bind_config - add RNDIS network link to a configuration
779 * @c: the configuration to support the network link
780 * @ethaddr: a buffer in which the ethernet address of the host side
781 * side of the link was recorded
782 * Context: single threaded during gadget setup
783 *
784 * Returns zero on success, else negative errno.
785 *
786 * Caller must have called @gether_setup(). Caller is also responsible
787 * for calling @gether_cleanup() before module unload.
788 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200789int
790rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700791{
792 struct f_rndis *rndis;
793 int status;
794
795 if (!can_support_rndis(c) || !ethaddr)
796 return -EINVAL;
797
798 /* maybe allocate device-global string IDs */
799 if (rndis_string_defs[0].id == 0) {
800
801 /* ... and setup RNDIS itself */
802 status = rndis_init();
803 if (status < 0)
804 return status;
805
806 /* control interface label */
807 status = usb_string_id(c->cdev);
808 if (status < 0)
809 return status;
810 rndis_string_defs[0].id = status;
811 rndis_control_intf.iInterface = status;
812
813 /* data interface label */
814 status = usb_string_id(c->cdev);
815 if (status < 0)
816 return status;
817 rndis_string_defs[1].id = status;
818 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100819
820 /* IAD iFunction label */
821 status = usb_string_id(c->cdev);
822 if (status < 0)
823 return status;
824 rndis_string_defs[2].id = status;
825 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700826 }
827
828 /* allocate and initialize one new instance */
829 status = -ENOMEM;
830 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
831 if (!rndis)
832 goto fail;
833
834 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
835
836 /* RNDIS activates when the host changes this filter */
837 rndis->port.cdc_filter = 0;
838
839 /* RNDIS has special (and complex) framing */
840 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
841 rndis->port.wrap = rndis_add_header;
842 rndis->port.unwrap = rndis_rm_hdr;
843
844 rndis->port.func.name = "rndis";
845 rndis->port.func.strings = rndis_strings;
846 /* descriptors are per-instance copies */
847 rndis->port.func.bind = rndis_bind;
848 rndis->port.func.unbind = rndis_unbind;
849 rndis->port.func.set_alt = rndis_set_alt;
850 rndis->port.func.setup = rndis_setup;
851 rndis->port.func.disable = rndis_disable;
852
853 status = usb_add_function(c, &rndis->port.func);
854 if (status) {
855 kfree(rndis);
856fail:
857 rndis_exit();
858 }
859 return status;
860}
Mike Lockwood789ef232010-01-12 10:33:59 -0800861
862#ifdef CONFIG_USB_ANDROID_RNDIS
863#include "rndis.c"
864
865// FIXME - using bogus MAC address for now
866
867static u8 ethaddr[ETH_ALEN] = { 11, 22, 33, 44, 55, 66 };
868
869int rndis_function_bind_config(struct usb_configuration *c)
870{
871 int ret = gether_setup(c->cdev->gadget, ethaddr);
872 if (ret == 0)
873 ret = rndis_bind_config(c, ethaddr);
874 return ret;
875}
876
877static struct android_usb_function rndis_function = {
878 .name = "rndis",
879 .bind_config = rndis_function_bind_config,
880};
881
882static int __init init(void)
883{
884 printk(KERN_INFO "f_rndis init\n");
885 android_register_function(&rndis_function);
886 return 0;
887}
888module_init(init);
889
890#endif /* CONFIG_USB_ANDROID_RNDIS */