blob: 96adf45d44c285e0940cf4566972a4e3ea3780ff [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>
31
32#include <asm/atomic.h>
33
34#include "u_ether.h"
35#include "rndis.h"
36
37
38/*
39 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
40 * been promoted instead of the standard CDC Ethernet. The published RNDIS
41 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
42 * ActiveSync have even worse status in terms of specification.
43 *
44 * In short: it's a protocol controlled by (and for) Microsoft, not for an
45 * Open ecosystem or markets. Linux supports it *only* because Microsoft
46 * doesn't support the CDC Ethernet standard.
47 *
48 * The RNDIS data transfer model is complex, with multiple Ethernet packets
49 * per USB message, and out of band data. The control model is built around
50 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
51 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
52 * useless (they're ignored). RNDIS expects to be the only function in its
53 * configuration, so it's no real help if you need composite devices; and
54 * it expects to be the first configuration too.
55 *
56 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
57 * discount the fluff that its RPC can be made to deliver: it doesn't need
58 * a NOP altsetting for the data interface. That lets it work on some of the
59 * "so smart it's stupid" hardware which takes over configuration changes
60 * from the software, and adds restrictions like "no altsettings".
61 *
62 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
63 * have all sorts of contrary-to-specification oddities that can prevent
64 * them from working sanely. Since bugfixes (or accurate specs, letting
65 * Linux work around those bugs) are unlikely to ever come from MSFT, you
66 * may want to avoid using RNDIS on purely operational grounds.
67 *
68 * Omissions from the RNDIS 1.0 specification include:
69 *
70 * - Power management ... references data that's scattered around lots
71 * of other documentation, which is incorrect/incomplete there too.
72 *
73 * - There are various undocumented protocol requirements, like the need
74 * to send garbage in some control-OUT messages.
75 *
76 * - MS-Windows drivers sometimes emit undocumented requests.
77 */
78
79struct rndis_ep_descs {
80 struct usb_endpoint_descriptor *in;
81 struct usb_endpoint_descriptor *out;
82 struct usb_endpoint_descriptor *notify;
83};
84
85struct f_rndis {
86 struct gether port;
87 u8 ctrl_id, data_id;
88 u8 ethaddr[ETH_ALEN];
Benoit Gobyaab96812011-04-19 20:37:33 -070089 u32 vendorID;
90 const char *manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -070091 int config;
92
Benoit Gobyaab96812011-04-19 20:37:33 -070093
David Brownell45fe3b82008-06-19 18:20:04 -070094 struct rndis_ep_descs fs;
David Brownell45fe3b82008-06-19 18:20:04 -070095 struct rndis_ep_descs hs;
96
97 struct usb_ep *notify;
98 struct usb_endpoint_descriptor *notify_desc;
99 struct usb_request *notify_req;
100 atomic_t notify_count;
101};
102
103static inline struct f_rndis *func_to_rndis(struct usb_function *f)
104{
105 return container_of(f, struct f_rndis, port.func);
106}
107
108/* peak (theoretical) bulk transfer rate in bits-per-second */
109static unsigned int bitrate(struct usb_gadget *g)
110{
111 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
112 return 13 * 512 * 8 * 1000 * 8;
113 else
114 return 19 * 64 * 1 * 1000 * 8;
115}
116
117/*-------------------------------------------------------------------------*/
118
119/*
120 */
121
122#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
123#define STATUS_BYTECOUNT 8 /* 8 bytes data */
124
125
126/* interface descriptor: */
127
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200128static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700129 .bLength = sizeof rndis_control_intf,
130 .bDescriptorType = USB_DT_INTERFACE,
131
132 /* .bInterfaceNumber = DYNAMIC */
133 /* status endpoint is optional; this could be patched later */
134 .bNumEndpoints = 1,
135 .bInterfaceClass = USB_CLASS_COMM,
136 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
137 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
138 /* .iInterface = DYNAMIC */
139};
140
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200141static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700142 .bLength = sizeof header_desc,
143 .bDescriptorType = USB_DT_CS_INTERFACE,
144 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
145
Harvey Harrison551509d2009-02-11 14:11:36 -0800146 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700147};
148
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200149static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700150 .bLength = sizeof call_mgmt_descriptor,
151 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
153
154 .bmCapabilities = 0x00,
155 .bDataInterface = 0x01,
156};
157
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200158static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100159 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700160 .bDescriptorType = USB_DT_CS_INTERFACE,
161 .bDescriptorSubType = USB_CDC_ACM_TYPE,
162
163 .bmCapabilities = 0x00,
164};
165
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200166static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700167 .bLength = sizeof(rndis_union_desc),
168 .bDescriptorType = USB_DT_CS_INTERFACE,
169 .bDescriptorSubType = USB_CDC_UNION_TYPE,
170 /* .bMasterInterface0 = DYNAMIC */
171 /* .bSlaveInterface0 = DYNAMIC */
172};
173
174/* the data interface has two bulk endpoints */
175
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200176static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700177 .bLength = sizeof rndis_data_intf,
178 .bDescriptorType = USB_DT_INTERFACE,
179
180 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700181 .bNumEndpoints = 2,
182 .bInterfaceClass = USB_CLASS_CDC_DATA,
183 .bInterfaceSubClass = 0,
184 .bInterfaceProtocol = 0,
185 /* .iInterface = DYNAMIC */
186};
187
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100188
189static struct usb_interface_assoc_descriptor
190rndis_iad_descriptor = {
191 .bLength = sizeof rndis_iad_descriptor,
192 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100193 .bFirstInterface = 0, /* XXX, hardcoded */
194 .bInterfaceCount = 2, // control + data
195 .bFunctionClass = USB_CLASS_COMM,
196 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
John Michelau00bc4082010-12-10 13:51:19 -0600197 .bFunctionProtocol = USB_CDC_ACM_PROTO_VENDOR,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100198 /* .iFunction = DYNAMIC */
199};
200
David Brownell45fe3b82008-06-19 18:20:04 -0700201/* full speed support: */
202
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200203static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700204 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT,
206
207 .bEndpointAddress = USB_DIR_IN,
208 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800209 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700210 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
211};
212
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200213static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216
217 .bEndpointAddress = USB_DIR_IN,
218 .bmAttributes = USB_ENDPOINT_XFER_BULK,
219};
220
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200221static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700222 .bLength = USB_DT_ENDPOINT_SIZE,
223 .bDescriptorType = USB_DT_ENDPOINT,
224
225 .bEndpointAddress = USB_DIR_OUT,
226 .bmAttributes = USB_ENDPOINT_XFER_BULK,
227};
228
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200229static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100230 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700231 /* control interface matches ACM, not Ethernet */
232 (struct usb_descriptor_header *) &rndis_control_intf,
233 (struct usb_descriptor_header *) &header_desc,
234 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100235 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700236 (struct usb_descriptor_header *) &rndis_union_desc,
237 (struct usb_descriptor_header *) &fs_notify_desc,
238 /* data interface has no altsetting */
239 (struct usb_descriptor_header *) &rndis_data_intf,
240 (struct usb_descriptor_header *) &fs_in_desc,
241 (struct usb_descriptor_header *) &fs_out_desc,
242 NULL,
243};
244
245/* high speed support: */
246
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200247static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700248 .bLength = USB_DT_ENDPOINT_SIZE,
249 .bDescriptorType = USB_DT_ENDPOINT,
250
251 .bEndpointAddress = USB_DIR_IN,
252 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800253 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700254 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
255};
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200256static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700257 .bLength = USB_DT_ENDPOINT_SIZE,
258 .bDescriptorType = USB_DT_ENDPOINT,
259
260 .bEndpointAddress = USB_DIR_IN,
261 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800262 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700263};
264
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200265static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700266 .bLength = USB_DT_ENDPOINT_SIZE,
267 .bDescriptorType = USB_DT_ENDPOINT,
268
269 .bEndpointAddress = USB_DIR_OUT,
270 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800271 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700272};
273
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200274static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100275 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700276 /* control interface matches ACM, not Ethernet */
277 (struct usb_descriptor_header *) &rndis_control_intf,
278 (struct usb_descriptor_header *) &header_desc,
279 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100280 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700281 (struct usb_descriptor_header *) &rndis_union_desc,
282 (struct usb_descriptor_header *) &hs_notify_desc,
283 /* data interface has no altsetting */
284 (struct usb_descriptor_header *) &rndis_data_intf,
285 (struct usb_descriptor_header *) &hs_in_desc,
286 (struct usb_descriptor_header *) &hs_out_desc,
287 NULL,
288};
289
290/* string descriptors: */
291
292static struct usb_string rndis_string_defs[] = {
293 [0].s = "RNDIS Communications Control",
294 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100295 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700296 { } /* end of list */
297};
298
299static struct usb_gadget_strings rndis_string_table = {
300 .language = 0x0409, /* en-us */
301 .strings = rndis_string_defs,
302};
303
304static struct usb_gadget_strings *rndis_strings[] = {
305 &rndis_string_table,
306 NULL,
307};
308
309/*-------------------------------------------------------------------------*/
310
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500311static struct sk_buff *rndis_add_header(struct gether *port,
312 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700313{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500314 struct sk_buff *skb2;
315
316 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
317 if (skb2)
318 rndis_add_hdr(skb2);
319
320 dev_kfree_skb_any(skb);
321 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700322}
323
324static void rndis_response_available(void *_rndis)
325{
326 struct f_rndis *rndis = _rndis;
327 struct usb_request *req = rndis->notify_req;
328 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
329 __le32 *data = req->buf;
330 int status;
331
Richard Röjforsff349502008-11-15 19:53:24 -0800332 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700333 return;
334
335 /* Send RNDIS RESPONSE_AVAILABLE notification; a
336 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
337 *
338 * This is the only notification defined by RNDIS.
339 */
340 data[0] = cpu_to_le32(1);
341 data[1] = cpu_to_le32(0);
342
343 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
344 if (status) {
345 atomic_dec(&rndis->notify_count);
346 DBG(cdev, "notify/0 --> %d\n", status);
347 }
348}
349
350static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
351{
352 struct f_rndis *rndis = req->context;
353 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
354 int status = req->status;
355
356 /* after TX:
357 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
358 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
359 */
360 switch (status) {
361 case -ECONNRESET:
362 case -ESHUTDOWN:
363 /* connection gone */
364 atomic_set(&rndis->notify_count, 0);
365 break;
366 default:
367 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
368 ep->name, status,
369 req->actual, req->length);
370 /* FALLTHROUGH */
371 case 0:
372 if (ep != rndis->notify)
373 break;
374
375 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
376 * notifications by resending until we're done
377 */
378 if (atomic_dec_and_test(&rndis->notify_count))
379 break;
380 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
381 if (status) {
382 atomic_dec(&rndis->notify_count);
383 DBG(cdev, "notify/1 --> %d\n", status);
384 }
385 break;
386 }
387}
388
389static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
390{
391 struct f_rndis *rndis = req->context;
392 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
393 int status;
394
395 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
396// spin_lock(&dev->lock);
397 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
398 if (status < 0)
399 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
400 status, req->actual, req->length);
401// spin_unlock(&dev->lock);
402}
403
404static int
405rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
406{
407 struct f_rndis *rndis = func_to_rndis(f);
408 struct usb_composite_dev *cdev = f->config->cdev;
409 struct usb_request *req = cdev->req;
410 int value = -EOPNOTSUPP;
411 u16 w_index = le16_to_cpu(ctrl->wIndex);
412 u16 w_value = le16_to_cpu(ctrl->wValue);
413 u16 w_length = le16_to_cpu(ctrl->wLength);
414
415 /* composite driver infrastructure handles everything except
416 * CDC class messages; interface activation uses set_alt().
417 */
418 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
419
420 /* RNDIS uses the CDC command encapsulation mechanism to implement
421 * an RPC scheme, with much getting/setting of attributes by OID.
422 */
423 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
424 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300425 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700426 goto invalid;
427 /* read the request; process it later */
428 value = w_length;
429 req->complete = rndis_command_complete;
430 req->context = rndis;
431 /* later, rndis_response_available() sends a notification */
432 break;
433
434 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
435 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
436 if (w_value || w_index != rndis->ctrl_id)
437 goto invalid;
438 else {
439 u8 *buf;
440 u32 n;
441
442 /* return the result */
443 buf = rndis_get_next_response(rndis->config, &n);
444 if (buf) {
445 memcpy(req->buf, buf, n);
446 req->complete = rndis_response_complete;
447 rndis_free_response(rndis->config, buf);
448 value = n;
449 }
450 /* else stalls ... spec says to avoid that */
451 }
452 break;
453
454 default:
455invalid:
456 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
457 ctrl->bRequestType, ctrl->bRequest,
458 w_value, w_index, w_length);
459 }
460
461 /* respond with data transfer or status phase? */
462 if (value >= 0) {
463 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
464 ctrl->bRequestType, ctrl->bRequest,
465 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700466 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700467 req->length = value;
468 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
469 if (value < 0)
470 ERROR(cdev, "rndis response on err %d\n", value);
471 }
472
473 /* device either stalls (value < 0) or reports success */
474 return value;
475}
476
477
478static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
479{
480 struct f_rndis *rndis = func_to_rndis(f);
481 struct usb_composite_dev *cdev = f->config->cdev;
482
483 /* we know alt == 0 */
484
485 if (intf == rndis->ctrl_id) {
486 if (rndis->notify->driver_data) {
487 VDBG(cdev, "reset rndis control %d\n", intf);
488 usb_ep_disable(rndis->notify);
489 } else {
490 VDBG(cdev, "init rndis ctrl %d\n", intf);
David Brownell45fe3b82008-06-19 18:20:04 -0700491 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800492 rndis->notify_desc = ep_choose(cdev->gadget,
493 rndis->hs.notify,
494 rndis->fs.notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700495 usb_ep_enable(rndis->notify, rndis->notify_desc);
496 rndis->notify->driver_data = rndis;
497
498 } else if (intf == rndis->data_id) {
499 struct net_device *net;
500
501 if (rndis->port.in_ep->driver_data) {
502 DBG(cdev, "reset rndis\n");
503 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530504 }
505
506 if (!rndis->port.in) {
David Brownell45fe3b82008-06-19 18:20:04 -0700507 DBG(cdev, "init rndis\n");
David Brownell45fe3b82008-06-19 18:20:04 -0700508 }
Mike Lockwood789ef232010-01-12 10:33:59 -0800509 rndis->port.in = ep_choose(cdev->gadget,
510 rndis->hs.in, rndis->fs.in);
511 rndis->port.out = ep_choose(cdev->gadget,
512 rndis->hs.out, rndis->fs.out);
David Brownell45fe3b82008-06-19 18:20:04 -0700513
514 /* Avoid ZLPs; they can be troublesome. */
515 rndis->port.is_zlp_ok = false;
516
517 /* RNDIS should be in the "RNDIS uninitialized" state,
518 * either never activated or after rndis_uninit().
519 *
520 * We don't want data to flow here until a nonzero packet
521 * filter is set, at which point it enters "RNDIS data
522 * initialized" state ... but we do want the endpoints
523 * to be activated. It's a strange little state.
524 *
525 * REVISIT the RNDIS gadget code has done this wrong for a
526 * very long time. We need another call to the link layer
527 * code -- gether_updown(...bool) maybe -- to do it right.
528 */
529 rndis->port.cdc_filter = 0;
530
531 DBG(cdev, "RNDIS RX/TX early activation ... \n");
532 net = gether_connect(&rndis->port);
533 if (IS_ERR(net))
534 return PTR_ERR(net);
535
536 rndis_set_param_dev(rndis->config, net,
537 &rndis->port.cdc_filter);
538 } else
539 goto fail;
540
541 return 0;
542fail:
543 return -EINVAL;
544}
545
546static void rndis_disable(struct usb_function *f)
547{
548 struct f_rndis *rndis = func_to_rndis(f);
549 struct usb_composite_dev *cdev = f->config->cdev;
550
551 if (!rndis->notify->driver_data)
552 return;
553
554 DBG(cdev, "rndis deactivated\n");
555
556 rndis_uninit(rndis->config);
557 gether_disconnect(&rndis->port);
558
559 usb_ep_disable(rndis->notify);
560 rndis->notify->driver_data = NULL;
561}
562
563/*-------------------------------------------------------------------------*/
564
565/*
566 * This isn't quite the same mechanism as CDC Ethernet, since the
567 * notification scheme passes less data, but the same set of link
568 * states must be tested. A key difference is that altsettings are
569 * not used to tell whether the link should send packets or not.
570 */
571
572static void rndis_open(struct gether *geth)
573{
574 struct f_rndis *rndis = func_to_rndis(&geth->func);
575 struct usb_composite_dev *cdev = geth->func.config->cdev;
576
577 DBG(cdev, "%s\n", __func__);
578
579 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
580 bitrate(cdev->gadget) / 100);
581 rndis_signal_connect(rndis->config);
582}
583
584static void rndis_close(struct gether *geth)
585{
586 struct f_rndis *rndis = func_to_rndis(&geth->func);
587
588 DBG(geth->func.config->cdev, "%s\n", __func__);
589
590 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
591 rndis_signal_disconnect(rndis->config);
592}
593
594/*-------------------------------------------------------------------------*/
595
596/* ethernet function driver setup/binding */
597
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200598static int
David Brownell45fe3b82008-06-19 18:20:04 -0700599rndis_bind(struct usb_configuration *c, struct usb_function *f)
600{
601 struct usb_composite_dev *cdev = c->cdev;
602 struct f_rndis *rndis = func_to_rndis(f);
603 int status;
604 struct usb_ep *ep;
605
606 /* allocate instance-specific interface IDs */
607 status = usb_interface_id(c, f);
608 if (status < 0)
609 goto fail;
610 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100611 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700612
613 rndis_control_intf.bInterfaceNumber = status;
614 rndis_union_desc.bMasterInterface0 = status;
615
616 status = usb_interface_id(c, f);
617 if (status < 0)
618 goto fail;
619 rndis->data_id = status;
620
621 rndis_data_intf.bInterfaceNumber = status;
622 rndis_union_desc.bSlaveInterface0 = status;
623
624 status = -ENODEV;
625
626 /* allocate instance-specific endpoints */
627 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
628 if (!ep)
629 goto fail;
630 rndis->port.in_ep = ep;
631 ep->driver_data = cdev; /* claim */
632
633 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
634 if (!ep)
635 goto fail;
636 rndis->port.out_ep = ep;
637 ep->driver_data = cdev; /* claim */
638
639 /* NOTE: a status/notification endpoint is, strictly speaking,
640 * optional. We don't treat it that way though! It's simpler,
641 * and some newer profiles don't treat it as optional.
642 */
643 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
644 if (!ep)
645 goto fail;
646 rndis->notify = ep;
647 ep->driver_data = cdev; /* claim */
648
649 status = -ENOMEM;
650
651 /* allocate notification request and buffer */
652 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
653 if (!rndis->notify_req)
654 goto fail;
655 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
656 if (!rndis->notify_req->buf)
657 goto fail;
658 rndis->notify_req->length = STATUS_BYTECOUNT;
659 rndis->notify_req->context = rndis;
660 rndis->notify_req->complete = rndis_response_complete;
661
662 /* copy descriptors, and track endpoint copies */
663 f->descriptors = usb_copy_descriptors(eth_fs_function);
664 if (!f->descriptors)
665 goto fail;
666
667 rndis->fs.in = usb_find_endpoint(eth_fs_function,
668 f->descriptors, &fs_in_desc);
669 rndis->fs.out = usb_find_endpoint(eth_fs_function,
670 f->descriptors, &fs_out_desc);
671 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
672 f->descriptors, &fs_notify_desc);
673
674 /* support all relevant hardware speeds... we expect that when
675 * hardware is dual speed, all bulk-capable endpoints work at
676 * both speeds
677 */
678 if (gadget_is_dualspeed(c->cdev->gadget)) {
679 hs_in_desc.bEndpointAddress =
680 fs_in_desc.bEndpointAddress;
681 hs_out_desc.bEndpointAddress =
682 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800683 hs_notify_desc.bEndpointAddress =
684 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700685
686 /* copy descriptors, and track endpoint copies */
687 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
688
689 if (!f->hs_descriptors)
690 goto fail;
691
692 rndis->hs.in = usb_find_endpoint(eth_hs_function,
693 f->hs_descriptors, &hs_in_desc);
694 rndis->hs.out = usb_find_endpoint(eth_hs_function,
695 f->hs_descriptors, &hs_out_desc);
David Brownell7c124142008-11-24 23:11:03 -0800696 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
697 f->hs_descriptors, &hs_notify_desc);
David Brownell45fe3b82008-06-19 18:20:04 -0700698 }
699
700 rndis->port.open = rndis_open;
701 rndis->port.close = rndis_close;
702
703 status = rndis_register(rndis_response_available, rndis);
704 if (status < 0)
705 goto fail;
706 rndis->config = status;
707
708 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
709 rndis_set_host_mac(rndis->config, rndis->ethaddr);
710
Benoit Gobyaab96812011-04-19 20:37:33 -0700711 if (rndis_set_param_vendor(rndis->config, rndis->vendorID,
712 rndis->manufacturer))
Mike Lockwood0d511c42010-03-10 17:05:03 -0500713 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700714
715 /* NOTE: all that is done without knowing or caring about
716 * the network link ... which is unavailable to this code
717 * until we're activated via set_alt().
718 */
719
720 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
721 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
722 rndis->port.in_ep->name, rndis->port.out_ep->name,
723 rndis->notify->name);
724 return 0;
725
726fail:
727 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
728 usb_free_descriptors(f->hs_descriptors);
729 if (f->descriptors)
730 usb_free_descriptors(f->descriptors);
731
732 if (rndis->notify_req) {
733 kfree(rndis->notify_req->buf);
734 usb_ep_free_request(rndis->notify, rndis->notify_req);
735 }
736
737 /* we might as well release our claims on endpoints */
738 if (rndis->notify)
739 rndis->notify->driver_data = NULL;
740 if (rndis->port.out)
741 rndis->port.out_ep->driver_data = NULL;
742 if (rndis->port.in)
743 rndis->port.in_ep->driver_data = NULL;
744
745 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
746
747 return status;
748}
749
750static void
751rndis_unbind(struct usb_configuration *c, struct usb_function *f)
752{
753 struct f_rndis *rndis = func_to_rndis(f);
754
755 rndis_deregister(rndis->config);
756 rndis_exit();
757
758 if (gadget_is_dualspeed(c->cdev->gadget))
759 usb_free_descriptors(f->hs_descriptors);
760 usb_free_descriptors(f->descriptors);
761
762 kfree(rndis->notify_req->buf);
763 usb_ep_free_request(rndis->notify, rndis->notify_req);
764
765 kfree(rndis);
766}
767
768/* Some controllers can't support RNDIS ... */
769static inline bool can_support_rndis(struct usb_configuration *c)
770{
David Brownell45fe3b82008-06-19 18:20:04 -0700771 /* everything else is *presumably* fine */
772 return true;
773}
774
775/**
776 * rndis_bind_config - add RNDIS network link to a configuration
777 * @c: the configuration to support the network link
778 * @ethaddr: a buffer in which the ethernet address of the host side
779 * side of the link was recorded
780 * Context: single threaded during gadget setup
781 *
782 * Returns zero on success, else negative errno.
783 *
784 * Caller must have called @gether_setup(). Caller is also responsible
785 * for calling @gether_cleanup() before module unload.
786 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200787int
Benoit Gobyaab96812011-04-19 20:37:33 -0700788rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
789 u32 vendorID, const char *manufacturer)
David Brownell45fe3b82008-06-19 18:20:04 -0700790{
791 struct f_rndis *rndis;
792 int status;
793
794 if (!can_support_rndis(c) || !ethaddr)
795 return -EINVAL;
796
Benoit Gobya6ccb732012-01-20 14:42:41 -0800797 /* setup RNDIS itself */
798 status = rndis_init();
799 if (status < 0)
800 return status;
801
David Brownell45fe3b82008-06-19 18:20:04 -0700802 /* maybe allocate device-global string IDs */
803 if (rndis_string_defs[0].id == 0) {
804
David Brownell45fe3b82008-06-19 18:20:04 -0700805 /* control interface label */
806 status = usb_string_id(c->cdev);
807 if (status < 0)
808 return status;
809 rndis_string_defs[0].id = status;
810 rndis_control_intf.iInterface = status;
811
812 /* data interface label */
813 status = usb_string_id(c->cdev);
814 if (status < 0)
815 return status;
816 rndis_string_defs[1].id = status;
817 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100818
819 /* IAD iFunction label */
820 status = usb_string_id(c->cdev);
821 if (status < 0)
822 return status;
823 rndis_string_defs[2].id = status;
824 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700825 }
826
827 /* allocate and initialize one new instance */
828 status = -ENOMEM;
829 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
830 if (!rndis)
831 goto fail;
832
833 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
Benoit Gobyaab96812011-04-19 20:37:33 -0700834 rndis->vendorID = vendorID;
835 rndis->manufacturer = manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -0700836
837 /* RNDIS activates when the host changes this filter */
838 rndis->port.cdc_filter = 0;
839
840 /* RNDIS has special (and complex) framing */
841 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
842 rndis->port.wrap = rndis_add_header;
843 rndis->port.unwrap = rndis_rm_hdr;
844
845 rndis->port.func.name = "rndis";
846 rndis->port.func.strings = rndis_strings;
847 /* descriptors are per-instance copies */
848 rndis->port.func.bind = rndis_bind;
849 rndis->port.func.unbind = rndis_unbind;
850 rndis->port.func.set_alt = rndis_set_alt;
851 rndis->port.func.setup = rndis_setup;
852 rndis->port.func.disable = rndis_disable;
853
854 status = usb_add_function(c, &rndis->port.func);
855 if (status) {
856 kfree(rndis);
857fail:
858 rndis_exit();
859 }
860 return status;
861}