blob: b324efa0773382c90e70bdc009cce3d8522b5ae7 [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>
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];
83 int config;
84
David Brownell45fe3b82008-06-19 18:20:04 -070085 struct usb_ep *notify;
David Brownell45fe3b82008-06-19 18:20:04 -070086 struct usb_request *notify_req;
87 atomic_t notify_count;
88};
89
90static inline struct f_rndis *func_to_rndis(struct usb_function *f)
91{
92 return container_of(f, struct f_rndis, port.func);
93}
94
95/* peak (theoretical) bulk transfer rate in bits-per-second */
96static unsigned int bitrate(struct usb_gadget *g)
97{
98 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
99 return 13 * 512 * 8 * 1000 * 8;
100 else
101 return 19 * 64 * 1 * 1000 * 8;
102}
103
104/*-------------------------------------------------------------------------*/
105
106/*
107 */
108
109#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
110#define STATUS_BYTECOUNT 8 /* 8 bytes data */
111
112
113/* interface descriptor: */
114
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200115static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700116 .bLength = sizeof rndis_control_intf,
117 .bDescriptorType = USB_DT_INTERFACE,
118
119 /* .bInterfaceNumber = DYNAMIC */
120 /* status endpoint is optional; this could be patched later */
121 .bNumEndpoints = 1,
122 .bInterfaceClass = USB_CLASS_COMM,
123 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
124 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
125 /* .iInterface = DYNAMIC */
126};
127
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200128static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700129 .bLength = sizeof header_desc,
130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
132
Harvey Harrison551509d2009-02-11 14:11:36 -0800133 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700134};
135
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200136static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700137 .bLength = sizeof call_mgmt_descriptor,
138 .bDescriptorType = USB_DT_CS_INTERFACE,
139 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
140
141 .bmCapabilities = 0x00,
142 .bDataInterface = 0x01,
143};
144
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200145static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100146 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700147 .bDescriptorType = USB_DT_CS_INTERFACE,
148 .bDescriptorSubType = USB_CDC_ACM_TYPE,
149
150 .bmCapabilities = 0x00,
151};
152
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200153static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700154 .bLength = sizeof(rndis_union_desc),
155 .bDescriptorType = USB_DT_CS_INTERFACE,
156 .bDescriptorSubType = USB_CDC_UNION_TYPE,
157 /* .bMasterInterface0 = DYNAMIC */
158 /* .bSlaveInterface0 = DYNAMIC */
159};
160
161/* the data interface has two bulk endpoints */
162
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200163static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700164 .bLength = sizeof rndis_data_intf,
165 .bDescriptorType = USB_DT_INTERFACE,
166
167 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700168 .bNumEndpoints = 2,
169 .bInterfaceClass = USB_CLASS_CDC_DATA,
170 .bInterfaceSubClass = 0,
171 .bInterfaceProtocol = 0,
172 /* .iInterface = DYNAMIC */
173};
174
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100175
176static struct usb_interface_assoc_descriptor
177rndis_iad_descriptor = {
178 .bLength = sizeof rndis_iad_descriptor,
179 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
180
181 .bFirstInterface = 0, /* XXX, hardcoded */
182 .bInterfaceCount = 2, // control + data
183 .bFunctionClass = USB_CLASS_COMM,
184 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
185 .bFunctionProtocol = USB_CDC_PROTO_NONE,
186 /* .iFunction = DYNAMIC */
187};
188
David Brownell45fe3b82008-06-19 18:20:04 -0700189/* full speed support: */
190
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200191static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
194
195 .bEndpointAddress = USB_DIR_IN,
196 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800197 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700198 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
199};
200
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200201static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
204
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_BULK,
207};
208
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200209static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212
213 .bEndpointAddress = USB_DIR_OUT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
215};
216
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200217static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100218 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700219 /* control interface matches ACM, not Ethernet */
220 (struct usb_descriptor_header *) &rndis_control_intf,
221 (struct usb_descriptor_header *) &header_desc,
222 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100223 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700224 (struct usb_descriptor_header *) &rndis_union_desc,
225 (struct usb_descriptor_header *) &fs_notify_desc,
226 /* data interface has no altsetting */
227 (struct usb_descriptor_header *) &rndis_data_intf,
228 (struct usb_descriptor_header *) &fs_in_desc,
229 (struct usb_descriptor_header *) &fs_out_desc,
230 NULL,
231};
232
233/* high speed support: */
234
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200235static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
238
239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800241 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700242 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
243};
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200244static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
247
248 .bEndpointAddress = USB_DIR_IN,
249 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800250 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700251};
252
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200253static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
256
257 .bEndpointAddress = USB_DIR_OUT,
258 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800259 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700260};
261
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200262static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100263 (struct usb_descriptor_header *) &rndis_iad_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700264 /* control interface matches ACM, not Ethernet */
265 (struct usb_descriptor_header *) &rndis_control_intf,
266 (struct usb_descriptor_header *) &header_desc,
267 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100268 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700269 (struct usb_descriptor_header *) &rndis_union_desc,
270 (struct usb_descriptor_header *) &hs_notify_desc,
271 /* data interface has no altsetting */
272 (struct usb_descriptor_header *) &rndis_data_intf,
273 (struct usb_descriptor_header *) &hs_in_desc,
274 (struct usb_descriptor_header *) &hs_out_desc,
275 NULL,
276};
277
278/* string descriptors: */
279
280static struct usb_string rndis_string_defs[] = {
281 [0].s = "RNDIS Communications Control",
282 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100283 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700284 { } /* end of list */
285};
286
287static struct usb_gadget_strings rndis_string_table = {
288 .language = 0x0409, /* en-us */
289 .strings = rndis_string_defs,
290};
291
292static struct usb_gadget_strings *rndis_strings[] = {
293 &rndis_string_table,
294 NULL,
295};
296
297/*-------------------------------------------------------------------------*/
298
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500299static struct sk_buff *rndis_add_header(struct gether *port,
300 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700301{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500302 struct sk_buff *skb2;
303
304 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
305 if (skb2)
306 rndis_add_hdr(skb2);
307
308 dev_kfree_skb_any(skb);
309 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700310}
311
312static void rndis_response_available(void *_rndis)
313{
314 struct f_rndis *rndis = _rndis;
315 struct usb_request *req = rndis->notify_req;
316 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
317 __le32 *data = req->buf;
318 int status;
319
Richard Röjforsff349502008-11-15 19:53:24 -0800320 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700321 return;
322
323 /* Send RNDIS RESPONSE_AVAILABLE notification; a
324 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
325 *
326 * This is the only notification defined by RNDIS.
327 */
328 data[0] = cpu_to_le32(1);
329 data[1] = cpu_to_le32(0);
330
331 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
332 if (status) {
333 atomic_dec(&rndis->notify_count);
334 DBG(cdev, "notify/0 --> %d\n", status);
335 }
336}
337
338static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
339{
340 struct f_rndis *rndis = req->context;
341 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
342 int status = req->status;
343
344 /* after TX:
345 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
346 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
347 */
348 switch (status) {
349 case -ECONNRESET:
350 case -ESHUTDOWN:
351 /* connection gone */
352 atomic_set(&rndis->notify_count, 0);
353 break;
354 default:
355 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
356 ep->name, status,
357 req->actual, req->length);
358 /* FALLTHROUGH */
359 case 0:
360 if (ep != rndis->notify)
361 break;
362
363 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
364 * notifications by resending until we're done
365 */
366 if (atomic_dec_and_test(&rndis->notify_count))
367 break;
368 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
369 if (status) {
370 atomic_dec(&rndis->notify_count);
371 DBG(cdev, "notify/1 --> %d\n", status);
372 }
373 break;
374 }
375}
376
377static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
378{
379 struct f_rndis *rndis = req->context;
380 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
381 int status;
382
383 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
384// spin_lock(&dev->lock);
385 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
386 if (status < 0)
387 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
388 status, req->actual, req->length);
389// spin_unlock(&dev->lock);
390}
391
392static int
393rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
394{
395 struct f_rndis *rndis = func_to_rndis(f);
396 struct usb_composite_dev *cdev = f->config->cdev;
397 struct usb_request *req = cdev->req;
398 int value = -EOPNOTSUPP;
399 u16 w_index = le16_to_cpu(ctrl->wIndex);
400 u16 w_value = le16_to_cpu(ctrl->wValue);
401 u16 w_length = le16_to_cpu(ctrl->wLength);
402
403 /* composite driver infrastructure handles everything except
404 * CDC class messages; interface activation uses set_alt().
405 */
406 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
407
408 /* RNDIS uses the CDC command encapsulation mechanism to implement
409 * an RPC scheme, with much getting/setting of attributes by OID.
410 */
411 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
412 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300413 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700414 goto invalid;
415 /* read the request; process it later */
416 value = w_length;
417 req->complete = rndis_command_complete;
418 req->context = rndis;
419 /* later, rndis_response_available() sends a notification */
420 break;
421
422 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
423 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
424 if (w_value || w_index != rndis->ctrl_id)
425 goto invalid;
426 else {
427 u8 *buf;
428 u32 n;
429
430 /* return the result */
431 buf = rndis_get_next_response(rndis->config, &n);
432 if (buf) {
433 memcpy(req->buf, buf, n);
434 req->complete = rndis_response_complete;
435 rndis_free_response(rndis->config, buf);
436 value = n;
437 }
438 /* else stalls ... spec says to avoid that */
439 }
440 break;
441
442 default:
443invalid:
444 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
445 ctrl->bRequestType, ctrl->bRequest,
446 w_value, w_index, w_length);
447 }
448
449 /* respond with data transfer or status phase? */
450 if (value >= 0) {
451 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
452 ctrl->bRequestType, ctrl->bRequest,
453 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700454 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700455 req->length = value;
456 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
457 if (value < 0)
458 ERROR(cdev, "rndis response on err %d\n", value);
459 }
460
461 /* device either stalls (value < 0) or reports success */
462 return value;
463}
464
465
466static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
467{
468 struct f_rndis *rndis = func_to_rndis(f);
469 struct usb_composite_dev *cdev = f->config->cdev;
470
471 /* we know alt == 0 */
472
473 if (intf == rndis->ctrl_id) {
474 if (rndis->notify->driver_data) {
475 VDBG(cdev, "reset rndis control %d\n", intf);
476 usb_ep_disable(rndis->notify);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300477 }
478 if (!rndis->notify->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700479 VDBG(cdev, "init rndis ctrl %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300480 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
481 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700482 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300483 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700484 rndis->notify->driver_data = rndis;
485
486 } else if (intf == rndis->data_id) {
487 struct net_device *net;
488
489 if (rndis->port.in_ep->driver_data) {
490 DBG(cdev, "reset rndis\n");
491 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530492 }
493
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300494 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700495 DBG(cdev, "init rndis\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300496 if (config_ep_by_speed(cdev->gadget, f,
497 rndis->port.in_ep) ||
498 config_ep_by_speed(cdev->gadget, f,
499 rndis->port.out_ep)) {
500 rndis->port.in_ep->desc = NULL;
501 rndis->port.out_ep->desc = NULL;
502 goto fail;
503 }
David Brownell45fe3b82008-06-19 18:20:04 -0700504 }
505
506 /* Avoid ZLPs; they can be troublesome. */
507 rndis->port.is_zlp_ok = false;
508
509 /* RNDIS should be in the "RNDIS uninitialized" state,
510 * either never activated or after rndis_uninit().
511 *
512 * We don't want data to flow here until a nonzero packet
513 * filter is set, at which point it enters "RNDIS data
514 * initialized" state ... but we do want the endpoints
515 * to be activated. It's a strange little state.
516 *
517 * REVISIT the RNDIS gadget code has done this wrong for a
518 * very long time. We need another call to the link layer
519 * code -- gether_updown(...bool) maybe -- to do it right.
520 */
521 rndis->port.cdc_filter = 0;
522
523 DBG(cdev, "RNDIS RX/TX early activation ... \n");
524 net = gether_connect(&rndis->port);
525 if (IS_ERR(net))
526 return PTR_ERR(net);
527
528 rndis_set_param_dev(rndis->config, net,
529 &rndis->port.cdc_filter);
530 } else
531 goto fail;
532
533 return 0;
534fail:
535 return -EINVAL;
536}
537
538static void rndis_disable(struct usb_function *f)
539{
540 struct f_rndis *rndis = func_to_rndis(f);
541 struct usb_composite_dev *cdev = f->config->cdev;
542
543 if (!rndis->notify->driver_data)
544 return;
545
546 DBG(cdev, "rndis deactivated\n");
547
548 rndis_uninit(rndis->config);
549 gether_disconnect(&rndis->port);
550
551 usb_ep_disable(rndis->notify);
552 rndis->notify->driver_data = NULL;
553}
554
555/*-------------------------------------------------------------------------*/
556
557/*
558 * This isn't quite the same mechanism as CDC Ethernet, since the
559 * notification scheme passes less data, but the same set of link
560 * states must be tested. A key difference is that altsettings are
561 * not used to tell whether the link should send packets or not.
562 */
563
564static void rndis_open(struct gether *geth)
565{
566 struct f_rndis *rndis = func_to_rndis(&geth->func);
567 struct usb_composite_dev *cdev = geth->func.config->cdev;
568
569 DBG(cdev, "%s\n", __func__);
570
571 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
572 bitrate(cdev->gadget) / 100);
573 rndis_signal_connect(rndis->config);
574}
575
576static void rndis_close(struct gether *geth)
577{
578 struct f_rndis *rndis = func_to_rndis(&geth->func);
579
580 DBG(geth->func.config->cdev, "%s\n", __func__);
581
582 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
583 rndis_signal_disconnect(rndis->config);
584}
585
586/*-------------------------------------------------------------------------*/
587
588/* ethernet function driver setup/binding */
589
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200590static int
David Brownell45fe3b82008-06-19 18:20:04 -0700591rndis_bind(struct usb_configuration *c, struct usb_function *f)
592{
593 struct usb_composite_dev *cdev = c->cdev;
594 struct f_rndis *rndis = func_to_rndis(f);
595 int status;
596 struct usb_ep *ep;
597
598 /* allocate instance-specific interface IDs */
599 status = usb_interface_id(c, f);
600 if (status < 0)
601 goto fail;
602 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100603 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700604
605 rndis_control_intf.bInterfaceNumber = status;
606 rndis_union_desc.bMasterInterface0 = status;
607
608 status = usb_interface_id(c, f);
609 if (status < 0)
610 goto fail;
611 rndis->data_id = status;
612
613 rndis_data_intf.bInterfaceNumber = status;
614 rndis_union_desc.bSlaveInterface0 = status;
615
616 status = -ENODEV;
617
618 /* allocate instance-specific endpoints */
619 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
620 if (!ep)
621 goto fail;
622 rndis->port.in_ep = ep;
623 ep->driver_data = cdev; /* claim */
624
625 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
626 if (!ep)
627 goto fail;
628 rndis->port.out_ep = ep;
629 ep->driver_data = cdev; /* claim */
630
631 /* NOTE: a status/notification endpoint is, strictly speaking,
632 * optional. We don't treat it that way though! It's simpler,
633 * and some newer profiles don't treat it as optional.
634 */
635 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
636 if (!ep)
637 goto fail;
638 rndis->notify = ep;
639 ep->driver_data = cdev; /* claim */
640
641 status = -ENOMEM;
642
643 /* allocate notification request and buffer */
644 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
645 if (!rndis->notify_req)
646 goto fail;
647 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
648 if (!rndis->notify_req->buf)
649 goto fail;
650 rndis->notify_req->length = STATUS_BYTECOUNT;
651 rndis->notify_req->context = rndis;
652 rndis->notify_req->complete = rndis_response_complete;
653
654 /* copy descriptors, and track endpoint copies */
655 f->descriptors = usb_copy_descriptors(eth_fs_function);
656 if (!f->descriptors)
657 goto fail;
658
David Brownell45fe3b82008-06-19 18:20:04 -0700659 /* support all relevant hardware speeds... we expect that when
660 * hardware is dual speed, all bulk-capable endpoints work at
661 * both speeds
662 */
663 if (gadget_is_dualspeed(c->cdev->gadget)) {
664 hs_in_desc.bEndpointAddress =
665 fs_in_desc.bEndpointAddress;
666 hs_out_desc.bEndpointAddress =
667 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800668 hs_notify_desc.bEndpointAddress =
669 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700670
671 /* copy descriptors, and track endpoint copies */
672 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
673
674 if (!f->hs_descriptors)
675 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700676 }
677
678 rndis->port.open = rndis_open;
679 rndis->port.close = rndis_close;
680
681 status = rndis_register(rndis_response_available, rndis);
682 if (status < 0)
683 goto fail;
684 rndis->config = status;
685
686 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
687 rndis_set_host_mac(rndis->config, rndis->ethaddr);
688
689#if 0
690// FIXME
691 if (rndis_set_param_vendor(rndis->config, vendorID,
692 manufacturer))
693 goto fail0;
694#endif
695
696 /* NOTE: all that is done without knowing or caring about
697 * the network link ... which is unavailable to this code
698 * until we're activated via set_alt().
699 */
700
701 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
702 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
703 rndis->port.in_ep->name, rndis->port.out_ep->name,
704 rndis->notify->name);
705 return 0;
706
707fail:
708 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
709 usb_free_descriptors(f->hs_descriptors);
710 if (f->descriptors)
711 usb_free_descriptors(f->descriptors);
712
713 if (rndis->notify_req) {
714 kfree(rndis->notify_req->buf);
715 usb_ep_free_request(rndis->notify, rndis->notify_req);
716 }
717
718 /* we might as well release our claims on endpoints */
719 if (rndis->notify)
720 rndis->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300721 if (rndis->port.out_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700722 rndis->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300723 if (rndis->port.in_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700724 rndis->port.in_ep->driver_data = NULL;
725
726 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
727
728 return status;
729}
730
731static void
732rndis_unbind(struct usb_configuration *c, struct usb_function *f)
733{
734 struct f_rndis *rndis = func_to_rndis(f);
735
736 rndis_deregister(rndis->config);
737 rndis_exit();
738
739 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
769rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700770{
771 struct f_rndis *rndis;
772 int status;
773
774 if (!can_support_rndis(c) || !ethaddr)
775 return -EINVAL;
776
777 /* maybe allocate device-global string IDs */
778 if (rndis_string_defs[0].id == 0) {
779
780 /* ... and setup RNDIS itself */
781 status = rndis_init();
782 if (status < 0)
783 return status;
784
785 /* control interface label */
786 status = usb_string_id(c->cdev);
787 if (status < 0)
788 return status;
789 rndis_string_defs[0].id = status;
790 rndis_control_intf.iInterface = status;
791
792 /* data interface label */
793 status = usb_string_id(c->cdev);
794 if (status < 0)
795 return status;
796 rndis_string_defs[1].id = status;
797 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100798
799 /* IAD iFunction label */
800 status = usb_string_id(c->cdev);
801 if (status < 0)
802 return status;
803 rndis_string_defs[2].id = status;
804 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700805 }
806
807 /* allocate and initialize one new instance */
808 status = -ENOMEM;
809 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
810 if (!rndis)
811 goto fail;
812
813 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
814
815 /* RNDIS activates when the host changes this filter */
816 rndis->port.cdc_filter = 0;
817
818 /* RNDIS has special (and complex) framing */
819 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
820 rndis->port.wrap = rndis_add_header;
821 rndis->port.unwrap = rndis_rm_hdr;
822
823 rndis->port.func.name = "rndis";
824 rndis->port.func.strings = rndis_strings;
825 /* descriptors are per-instance copies */
826 rndis->port.func.bind = rndis_bind;
827 rndis->port.func.unbind = rndis_unbind;
828 rndis->port.func.set_alt = rndis_set_alt;
829 rndis->port.func.setup = rndis_setup;
830 rndis->port.func.disable = rndis_disable;
831
832 status = usb_add_function(c, &rndis->port.func);
833 if (status) {
834 kfree(rndis);
835fail:
836 rndis_exit();
837 }
838 return status;
839}