blob: 4a74dcf9a59fa53b55f989777d49aa945625b4d8 [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
David Brownell45fe3b82008-06-19 18:20:04 -070079struct f_rndis {
80 struct gether port;
81 u8 ctrl_id, data_id;
82 u8 ethaddr[ETH_ALEN];
Benoit Gobyaab96812011-04-19 20:37:33 -070083 u32 vendorID;
84 const char *manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -070085 int config;
86
David Brownell45fe3b82008-06-19 18:20:04 -070087 struct usb_ep *notify;
David Brownell45fe3b82008-06-19 18:20:04 -070088 struct usb_request *notify_req;
89 atomic_t notify_count;
90};
91
92static inline struct f_rndis *func_to_rndis(struct usb_function *f)
93{
94 return container_of(f, struct f_rndis, port.func);
95}
96
97/* peak (theoretical) bulk transfer rate in bits-per-second */
98static unsigned int bitrate(struct usb_gadget *g)
99{
100 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
101 return 13 * 512 * 8 * 1000 * 8;
102 else
103 return 19 * 64 * 1 * 1000 * 8;
104}
105
106/*-------------------------------------------------------------------------*/
107
108/*
109 */
110
111#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
112#define STATUS_BYTECOUNT 8 /* 8 bytes data */
113
114
115/* interface descriptor: */
116
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200117static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700118 .bLength = sizeof rndis_control_intf,
119 .bDescriptorType = USB_DT_INTERFACE,
120
121 /* .bInterfaceNumber = DYNAMIC */
122 /* status endpoint is optional; this could be patched later */
123 .bNumEndpoints = 1,
124 .bInterfaceClass = USB_CLASS_COMM,
125 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
126 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
127 /* .iInterface = DYNAMIC */
128};
129
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200130static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700131 .bLength = sizeof header_desc,
132 .bDescriptorType = USB_DT_CS_INTERFACE,
133 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
134
Harvey Harrison551509d2009-02-11 14:11:36 -0800135 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700136};
137
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200138static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700139 .bLength = sizeof call_mgmt_descriptor,
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
142
143 .bmCapabilities = 0x00,
144 .bDataInterface = 0x01,
145};
146
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200147static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100148 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_ACM_TYPE,
151
152 .bmCapabilities = 0x00,
153};
154
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200155static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700156 .bLength = sizeof(rndis_union_desc),
157 .bDescriptorType = USB_DT_CS_INTERFACE,
158 .bDescriptorSubType = USB_CDC_UNION_TYPE,
159 /* .bMasterInterface0 = DYNAMIC */
160 /* .bSlaveInterface0 = DYNAMIC */
161};
162
163/* the data interface has two bulk endpoints */
164
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200165static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700166 .bLength = sizeof rndis_data_intf,
167 .bDescriptorType = USB_DT_INTERFACE,
168
169 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700170 .bNumEndpoints = 2,
171 .bInterfaceClass = USB_CLASS_CDC_DATA,
172 .bInterfaceSubClass = 0,
173 .bInterfaceProtocol = 0,
174 /* .iInterface = DYNAMIC */
175};
176
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100177
178static struct usb_interface_assoc_descriptor
179rndis_iad_descriptor = {
180 .bLength = sizeof rndis_iad_descriptor,
181 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100182 .bFirstInterface = 0, /* XXX, hardcoded */
183 .bInterfaceCount = 2, // control + data
184 .bFunctionClass = USB_CLASS_COMM,
185 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
John Michelau00bc4082010-12-10 13:51:19 -0600186 .bFunctionProtocol = USB_CDC_ACM_PROTO_VENDOR,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100187 /* .iFunction = DYNAMIC */
188};
189
David Brownell45fe3b82008-06-19 18:20:04 -0700190/* full speed support: */
191
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200192static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
195
196 .bEndpointAddress = USB_DIR_IN,
197 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800198 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700199 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
200};
201
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200202static struct usb_endpoint_descriptor fs_in_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_BULK,
208};
209
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200210static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700211 .bLength = USB_DT_ENDPOINT_SIZE,
212 .bDescriptorType = USB_DT_ENDPOINT,
213
214 .bEndpointAddress = USB_DIR_OUT,
215 .bmAttributes = USB_ENDPOINT_XFER_BULK,
216};
217
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200218static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100219 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700220 /* control interface matches ACM, not Ethernet */
221 (struct usb_descriptor_header *) &rndis_control_intf,
222 (struct usb_descriptor_header *) &header_desc,
223 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100224 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700225 (struct usb_descriptor_header *) &rndis_union_desc,
226 (struct usb_descriptor_header *) &fs_notify_desc,
227 /* data interface has no altsetting */
228 (struct usb_descriptor_header *) &rndis_data_intf,
229 (struct usb_descriptor_header *) &fs_in_desc,
230 (struct usb_descriptor_header *) &fs_out_desc,
231 NULL,
232};
233
234/* high speed support: */
235
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200236static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700237 .bLength = USB_DT_ENDPOINT_SIZE,
238 .bDescriptorType = USB_DT_ENDPOINT,
239
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800242 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700243 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
244};
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200245static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_IN,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800251 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700252};
253
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200254static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT,
257
258 .bEndpointAddress = USB_DIR_OUT,
259 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800260 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700261};
262
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200263static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100264 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700265 /* control interface matches ACM, not Ethernet */
266 (struct usb_descriptor_header *) &rndis_control_intf,
267 (struct usb_descriptor_header *) &header_desc,
268 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100269 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700270 (struct usb_descriptor_header *) &rndis_union_desc,
271 (struct usb_descriptor_header *) &hs_notify_desc,
272 /* data interface has no altsetting */
273 (struct usb_descriptor_header *) &rndis_data_intf,
274 (struct usb_descriptor_header *) &hs_in_desc,
275 (struct usb_descriptor_header *) &hs_out_desc,
276 NULL,
277};
278
279/* string descriptors: */
280
281static struct usb_string rndis_string_defs[] = {
282 [0].s = "RNDIS Communications Control",
283 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100284 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700285 { } /* end of list */
286};
287
288static struct usb_gadget_strings rndis_string_table = {
289 .language = 0x0409, /* en-us */
290 .strings = rndis_string_defs,
291};
292
293static struct usb_gadget_strings *rndis_strings[] = {
294 &rndis_string_table,
295 NULL,
296};
297
298/*-------------------------------------------------------------------------*/
299
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500300static struct sk_buff *rndis_add_header(struct gether *port,
301 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700302{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500303 struct sk_buff *skb2;
304
305 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
306 if (skb2)
307 rndis_add_hdr(skb2);
308
309 dev_kfree_skb_any(skb);
310 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700311}
312
313static void rndis_response_available(void *_rndis)
314{
315 struct f_rndis *rndis = _rndis;
316 struct usb_request *req = rndis->notify_req;
317 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
318 __le32 *data = req->buf;
319 int status;
320
Richard Röjforsff349502008-11-15 19:53:24 -0800321 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700322 return;
323
324 /* Send RNDIS RESPONSE_AVAILABLE notification; a
325 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
326 *
327 * This is the only notification defined by RNDIS.
328 */
329 data[0] = cpu_to_le32(1);
330 data[1] = cpu_to_le32(0);
331
332 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
333 if (status) {
334 atomic_dec(&rndis->notify_count);
335 DBG(cdev, "notify/0 --> %d\n", status);
336 }
337}
338
339static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
340{
341 struct f_rndis *rndis = req->context;
342 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
343 int status = req->status;
344
345 /* after TX:
346 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
347 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
348 */
349 switch (status) {
350 case -ECONNRESET:
351 case -ESHUTDOWN:
352 /* connection gone */
353 atomic_set(&rndis->notify_count, 0);
354 break;
355 default:
356 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
357 ep->name, status,
358 req->actual, req->length);
359 /* FALLTHROUGH */
360 case 0:
361 if (ep != rndis->notify)
362 break;
363
364 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
365 * notifications by resending until we're done
366 */
367 if (atomic_dec_and_test(&rndis->notify_count))
368 break;
369 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
370 if (status) {
371 atomic_dec(&rndis->notify_count);
372 DBG(cdev, "notify/1 --> %d\n", status);
373 }
374 break;
375 }
376}
377
378static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
379{
380 struct f_rndis *rndis = req->context;
381 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
382 int status;
383
384 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
385// spin_lock(&dev->lock);
386 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
387 if (status < 0)
388 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
389 status, req->actual, req->length);
390// spin_unlock(&dev->lock);
391}
392
393static int
394rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
395{
396 struct f_rndis *rndis = func_to_rndis(f);
397 struct usb_composite_dev *cdev = f->config->cdev;
398 struct usb_request *req = cdev->req;
399 int value = -EOPNOTSUPP;
400 u16 w_index = le16_to_cpu(ctrl->wIndex);
401 u16 w_value = le16_to_cpu(ctrl->wValue);
402 u16 w_length = le16_to_cpu(ctrl->wLength);
403
404 /* composite driver infrastructure handles everything except
405 * CDC class messages; interface activation uses set_alt().
406 */
407 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
408
409 /* RNDIS uses the CDC command encapsulation mechanism to implement
410 * an RPC scheme, with much getting/setting of attributes by OID.
411 */
412 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
413 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300414 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700415 goto invalid;
416 /* read the request; process it later */
417 value = w_length;
418 req->complete = rndis_command_complete;
419 req->context = rndis;
420 /* later, rndis_response_available() sends a notification */
421 break;
422
423 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
424 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
425 if (w_value || w_index != rndis->ctrl_id)
426 goto invalid;
427 else {
428 u8 *buf;
429 u32 n;
430
431 /* return the result */
432 buf = rndis_get_next_response(rndis->config, &n);
433 if (buf) {
434 memcpy(req->buf, buf, n);
435 req->complete = rndis_response_complete;
436 rndis_free_response(rndis->config, buf);
437 value = n;
438 }
439 /* else stalls ... spec says to avoid that */
440 }
441 break;
442
443 default:
444invalid:
445 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
446 ctrl->bRequestType, ctrl->bRequest,
447 w_value, w_index, w_length);
448 }
449
450 /* respond with data transfer or status phase? */
451 if (value >= 0) {
452 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
453 ctrl->bRequestType, ctrl->bRequest,
454 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700455 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700456 req->length = value;
457 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
458 if (value < 0)
459 ERROR(cdev, "rndis response on err %d\n", value);
460 }
461
462 /* device either stalls (value < 0) or reports success */
463 return value;
464}
465
466
467static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
468{
469 struct f_rndis *rndis = func_to_rndis(f);
470 struct usb_composite_dev *cdev = f->config->cdev;
471
472 /* we know alt == 0 */
473
474 if (intf == rndis->ctrl_id) {
475 if (rndis->notify->driver_data) {
476 VDBG(cdev, "reset rndis control %d\n", intf);
477 usb_ep_disable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700478 }
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300479 if (!rndis->notify->desc) {
480 VDBG(cdev, "init rndis ctrl %d\n", intf);
481 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
482 goto fail;
483 }
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300484 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700485 rndis->notify->driver_data = rndis;
486
487 } else if (intf == rndis->data_id) {
488 struct net_device *net;
489
490 if (rndis->port.in_ep->driver_data) {
491 DBG(cdev, "reset rndis\n");
492 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530493 }
494
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300495 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700496 DBG(cdev, "init rndis\n");
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300497 if (config_ep_by_speed(cdev->gadget, f,
498 rndis->port.in_ep) ||
499 config_ep_by_speed(cdev->gadget, f,
500 rndis->port.out_ep)) {
501 rndis->port.in_ep->desc = NULL;
502 rndis->port.out_ep->desc = NULL;
503 goto fail;
504 }
David Brownell45fe3b82008-06-19 18:20:04 -0700505 }
506
507 /* Avoid ZLPs; they can be troublesome. */
508 rndis->port.is_zlp_ok = false;
509
510 /* RNDIS should be in the "RNDIS uninitialized" state,
511 * either never activated or after rndis_uninit().
512 *
513 * We don't want data to flow here until a nonzero packet
514 * filter is set, at which point it enters "RNDIS data
515 * initialized" state ... but we do want the endpoints
516 * to be activated. It's a strange little state.
517 *
518 * REVISIT the RNDIS gadget code has done this wrong for a
519 * very long time. We need another call to the link layer
520 * code -- gether_updown(...bool) maybe -- to do it right.
521 */
522 rndis->port.cdc_filter = 0;
523
524 DBG(cdev, "RNDIS RX/TX early activation ... \n");
525 net = gether_connect(&rndis->port);
526 if (IS_ERR(net))
527 return PTR_ERR(net);
528
529 rndis_set_param_dev(rndis->config, net,
530 &rndis->port.cdc_filter);
531 } else
532 goto fail;
533
534 return 0;
535fail:
536 return -EINVAL;
537}
538
539static void rndis_disable(struct usb_function *f)
540{
541 struct f_rndis *rndis = func_to_rndis(f);
542 struct usb_composite_dev *cdev = f->config->cdev;
543
544 if (!rndis->notify->driver_data)
545 return;
546
547 DBG(cdev, "rndis deactivated\n");
548
549 rndis_uninit(rndis->config);
550 gether_disconnect(&rndis->port);
551
552 usb_ep_disable(rndis->notify);
553 rndis->notify->driver_data = NULL;
554}
555
556/*-------------------------------------------------------------------------*/
557
558/*
559 * This isn't quite the same mechanism as CDC Ethernet, since the
560 * notification scheme passes less data, but the same set of link
561 * states must be tested. A key difference is that altsettings are
562 * not used to tell whether the link should send packets or not.
563 */
564
565static void rndis_open(struct gether *geth)
566{
567 struct f_rndis *rndis = func_to_rndis(&geth->func);
568 struct usb_composite_dev *cdev = geth->func.config->cdev;
569
570 DBG(cdev, "%s\n", __func__);
571
572 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
573 bitrate(cdev->gadget) / 100);
574 rndis_signal_connect(rndis->config);
575}
576
577static void rndis_close(struct gether *geth)
578{
579 struct f_rndis *rndis = func_to_rndis(&geth->func);
580
581 DBG(geth->func.config->cdev, "%s\n", __func__);
582
583 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
584 rndis_signal_disconnect(rndis->config);
585}
586
587/*-------------------------------------------------------------------------*/
588
589/* ethernet function driver setup/binding */
590
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200591static int
David Brownell45fe3b82008-06-19 18:20:04 -0700592rndis_bind(struct usb_configuration *c, struct usb_function *f)
593{
594 struct usb_composite_dev *cdev = c->cdev;
595 struct f_rndis *rndis = func_to_rndis(f);
596 int status;
597 struct usb_ep *ep;
598
599 /* allocate instance-specific interface IDs */
600 status = usb_interface_id(c, f);
601 if (status < 0)
602 goto fail;
603 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100604 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700605
606 rndis_control_intf.bInterfaceNumber = status;
607 rndis_union_desc.bMasterInterface0 = status;
608
609 status = usb_interface_id(c, f);
610 if (status < 0)
611 goto fail;
612 rndis->data_id = status;
613
614 rndis_data_intf.bInterfaceNumber = status;
615 rndis_union_desc.bSlaveInterface0 = status;
616
617 status = -ENODEV;
618
619 /* allocate instance-specific endpoints */
620 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
621 if (!ep)
622 goto fail;
623 rndis->port.in_ep = ep;
624 ep->driver_data = cdev; /* claim */
625
626 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
627 if (!ep)
628 goto fail;
629 rndis->port.out_ep = ep;
630 ep->driver_data = cdev; /* claim */
631
632 /* NOTE: a status/notification endpoint is, strictly speaking,
633 * optional. We don't treat it that way though! It's simpler,
634 * and some newer profiles don't treat it as optional.
635 */
636 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
637 if (!ep)
638 goto fail;
639 rndis->notify = ep;
640 ep->driver_data = cdev; /* claim */
641
642 status = -ENOMEM;
643
644 /* allocate notification request and buffer */
645 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
646 if (!rndis->notify_req)
647 goto fail;
648 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
649 if (!rndis->notify_req->buf)
650 goto fail;
651 rndis->notify_req->length = STATUS_BYTECOUNT;
652 rndis->notify_req->context = rndis;
653 rndis->notify_req->complete = rndis_response_complete;
654
655 /* copy descriptors, and track endpoint copies */
656 f->descriptors = usb_copy_descriptors(eth_fs_function);
657 if (!f->descriptors)
658 goto fail;
659
David Brownell45fe3b82008-06-19 18:20:04 -0700660 /* support all relevant hardware speeds... we expect that when
661 * hardware is dual speed, all bulk-capable endpoints work at
662 * both speeds
663 */
664 if (gadget_is_dualspeed(c->cdev->gadget)) {
665 hs_in_desc.bEndpointAddress =
666 fs_in_desc.bEndpointAddress;
667 hs_out_desc.bEndpointAddress =
668 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800669 hs_notify_desc.bEndpointAddress =
670 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700671
672 /* copy descriptors, and track endpoint copies */
673 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
674
675 if (!f->hs_descriptors)
676 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700677 }
678
679 rndis->port.open = rndis_open;
680 rndis->port.close = rndis_close;
681
682 status = rndis_register(rndis_response_available, rndis);
683 if (status < 0)
684 goto fail;
685 rndis->config = status;
686
687 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
688 rndis_set_host_mac(rndis->config, rndis->ethaddr);
689
Benoit Gobyaab96812011-04-19 20:37:33 -0700690 if (rndis_set_param_vendor(rndis->config, rndis->vendorID,
691 rndis->manufacturer))
Mike Lockwood0d511c42010-03-10 17:05:03 -0500692 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700693
694 /* NOTE: all that is done without knowing or caring about
695 * the network link ... which is unavailable to this code
696 * until we're activated via set_alt().
697 */
698
699 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
700 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
701 rndis->port.in_ep->name, rndis->port.out_ep->name,
702 rndis->notify->name);
703 return 0;
704
705fail:
706 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
707 usb_free_descriptors(f->hs_descriptors);
708 if (f->descriptors)
709 usb_free_descriptors(f->descriptors);
710
711 if (rndis->notify_req) {
712 kfree(rndis->notify_req->buf);
713 usb_ep_free_request(rndis->notify, rndis->notify_req);
714 }
715
716 /* we might as well release our claims on endpoints */
717 if (rndis->notify)
718 rndis->notify->driver_data = NULL;
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300719 if (rndis->port.out_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700720 rndis->port.out_ep->driver_data = NULL;
Tatyana Brokhman9168a352011-06-28 16:33:48 +0300721 if (rndis->port.in_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700722 rndis->port.in_ep->driver_data = NULL;
723
724 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
725
726 return status;
727}
728
729static void
730rndis_unbind(struct usb_configuration *c, struct usb_function *f)
731{
732 struct f_rndis *rndis = func_to_rndis(f);
733
734 rndis_deregister(rndis->config);
735 rndis_exit();
736
Benoit Gobyf6e7d492011-05-31 18:25:06 -0700737 rndis_string_defs[0].id = 0;
738
David Brownell45fe3b82008-06-19 18:20:04 -0700739 if (gadget_is_dualspeed(c->cdev->gadget))
740 usb_free_descriptors(f->hs_descriptors);
741 usb_free_descriptors(f->descriptors);
742
743 kfree(rndis->notify_req->buf);
744 usb_ep_free_request(rndis->notify, rndis->notify_req);
745
746 kfree(rndis);
747}
748
749/* Some controllers can't support RNDIS ... */
750static inline bool can_support_rndis(struct usb_configuration *c)
751{
David Brownell45fe3b82008-06-19 18:20:04 -0700752 /* everything else is *presumably* fine */
753 return true;
754}
755
756/**
757 * rndis_bind_config - add RNDIS network link to a configuration
758 * @c: the configuration to support the network link
759 * @ethaddr: a buffer in which the ethernet address of the host side
760 * side of the link was recorded
761 * Context: single threaded during gadget setup
762 *
763 * Returns zero on success, else negative errno.
764 *
765 * Caller must have called @gether_setup(). Caller is also responsible
766 * for calling @gether_cleanup() before module unload.
767 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200768int
Benoit Gobyaab96812011-04-19 20:37:33 -0700769rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
770 u32 vendorID, const char *manufacturer)
David Brownell45fe3b82008-06-19 18:20:04 -0700771{
772 struct f_rndis *rndis;
773 int status;
774
775 if (!can_support_rndis(c) || !ethaddr)
776 return -EINVAL;
777
778 /* maybe allocate device-global string IDs */
779 if (rndis_string_defs[0].id == 0) {
780
781 /* ... and setup RNDIS itself */
782 status = rndis_init();
783 if (status < 0)
784 return status;
785
786 /* control interface label */
787 status = usb_string_id(c->cdev);
788 if (status < 0)
789 return status;
790 rndis_string_defs[0].id = status;
791 rndis_control_intf.iInterface = status;
792
793 /* data interface label */
794 status = usb_string_id(c->cdev);
795 if (status < 0)
796 return status;
797 rndis_string_defs[1].id = status;
798 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100799
800 /* IAD iFunction label */
801 status = usb_string_id(c->cdev);
802 if (status < 0)
803 return status;
804 rndis_string_defs[2].id = status;
805 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700806 }
807
808 /* allocate and initialize one new instance */
809 status = -ENOMEM;
810 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
811 if (!rndis)
812 goto fail;
813
814 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
Benoit Gobyaab96812011-04-19 20:37:33 -0700815 rndis->vendorID = vendorID;
816 rndis->manufacturer = manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -0700817
818 /* RNDIS activates when the host changes this filter */
819 rndis->port.cdc_filter = 0;
820
821 /* RNDIS has special (and complex) framing */
822 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
823 rndis->port.wrap = rndis_add_header;
824 rndis->port.unwrap = rndis_rm_hdr;
825
826 rndis->port.func.name = "rndis";
827 rndis->port.func.strings = rndis_strings;
828 /* descriptors are per-instance copies */
829 rndis->port.func.bind = rndis_bind;
830 rndis->port.func.unbind = rndis_unbind;
831 rndis->port.func.set_alt = rndis_set_alt;
832 rndis->port.func.setup = rndis_setup;
833 rndis->port.func.disable = rndis_disable;
834
835 status = usb_add_function(c, &rndis->port.func);
836 if (status) {
837 kfree(rndis);
838fail:
839 rndis_exit();
840 }
841 return status;
842}