aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
authorDavid Brownell <dbrownell@users.sourceforge.net>2008-06-19 21:20:04 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2008-07-21 18:16:15 -0400
commit45fe3b8e5342cd1ce307099459c74011d8e01986 (patch)
tree7a8c3653592cac85c1da6edb2e86ddb2c70f2224 /drivers/usb/gadget
parentda741b8c56d612b5dd26ffa31341911a5fea23ee (diff)
usb ethernet gadget: split RNDIS function
This is a RNDIS function driver, extracted from the all-in-one Ethernet gadget driver. Lightly tested ... there seems to be a pre-existing problem when talking to Windows XP SP2, not quite sure what's up with that yet. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Makefile2
-rw-r--r--drivers/usb/gadget/f_rndis.c827
-rw-r--r--drivers/usb/gadget/u_ether.h23
3 files changed, 848 insertions, 4 deletions
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 564065253643..392487ddf902 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -35,7 +35,7 @@ g_printer-objs := printer.o usbstring.o config.o \
35 epautoconf.o 35 epautoconf.o
36 36
37ifeq ($(CONFIG_USB_ETH_RNDIS),y) 37ifeq ($(CONFIG_USB_ETH_RNDIS),y)
38 g_ether-objs += rndis.o 38 g_ether-objs += f_rndis.o rndis.o
39endif 39endif
40 40
41obj-$(CONFIG_USB_ZERO) += g_zero.o 41obj-$(CONFIG_USB_ZERO) += g_zero.o
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
new file mode 100644
index 000000000000..61652f0f13fd
--- /dev/null
+++ b/drivers/usb/gadget/f_rndis.c
@@ -0,0 +1,827 @@
1/*
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
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23/* #define VERBOSE_DEBUG */
24
25#include <linux/kernel.h>
26#include <linux/device.h>
27#include <linux/etherdevice.h>
28
29#include <asm/atomic.h>
30
31#include "u_ether.h"
32#include "rndis.h"
33
34
35/*
36 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
37 * been promoted instead of the standard CDC Ethernet. The published RNDIS
38 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
39 * ActiveSync have even worse status in terms of specification.
40 *
41 * In short: it's a protocol controlled by (and for) Microsoft, not for an
42 * Open ecosystem or markets. Linux supports it *only* because Microsoft
43 * doesn't support the CDC Ethernet standard.
44 *
45 * The RNDIS data transfer model is complex, with multiple Ethernet packets
46 * per USB message, and out of band data. The control model is built around
47 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
48 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
49 * useless (they're ignored). RNDIS expects to be the only function in its
50 * configuration, so it's no real help if you need composite devices; and
51 * it expects to be the first configuration too.
52 *
53 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
54 * discount the fluff that its RPC can be made to deliver: it doesn't need
55 * a NOP altsetting for the data interface. That lets it work on some of the
56 * "so smart it's stupid" hardware which takes over configuration changes
57 * from the software, and adds restrictions like "no altsettings".
58 *
59 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
60 * have all sorts of contrary-to-specification oddities that can prevent
61 * them from working sanely. Since bugfixes (or accurate specs, letting
62 * Linux work around those bugs) are unlikely to ever come from MSFT, you
63 * may want to avoid using RNDIS on purely operational grounds.
64 *
65 * Omissions from the RNDIS 1.0 specification include:
66 *
67 * - Power management ... references data that's scattered around lots
68 * of other documentation, which is incorrect/incomplete there too.
69 *
70 * - There are various undocumented protocol requirements, like the need
71 * to send garbage in some control-OUT messages.
72 *
73 * - MS-Windows drivers sometimes emit undocumented requests.
74 */
75
76struct rndis_ep_descs {
77 struct usb_endpoint_descriptor *in;
78 struct usb_endpoint_descriptor *out;
79 struct usb_endpoint_descriptor *notify;
80};
81
82struct f_rndis {
83 struct gether port;
84 u8 ctrl_id, data_id;
85 u8 ethaddr[ETH_ALEN];
86 int config;
87
88 struct usb_descriptor_header **fs_function;
89 struct rndis_ep_descs fs;
90 struct usb_descriptor_header **hs_function;
91 struct rndis_ep_descs hs;
92
93 struct usb_ep *notify;
94 struct usb_endpoint_descriptor *notify_desc;
95 struct usb_request *notify_req;
96 atomic_t notify_count;
97};
98
99static inline struct f_rndis *func_to_rndis(struct usb_function *f)
100{
101 return container_of(f, struct f_rndis, port.func);
102}
103
104/* peak (theoretical) bulk transfer rate in bits-per-second */
105static unsigned int bitrate(struct usb_gadget *g)
106{
107 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
108 return 13 * 512 * 8 * 1000 * 8;
109 else
110 return 19 * 64 * 1 * 1000 * 8;
111}
112
113/*-------------------------------------------------------------------------*/
114
115/*
116 */
117
118#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
119#define STATUS_BYTECOUNT 8 /* 8 bytes data */
120
121
122/* interface descriptor: */
123
124static struct usb_interface_descriptor rndis_control_intf __initdata = {
125 .bLength = sizeof rndis_control_intf,
126 .bDescriptorType = USB_DT_INTERFACE,
127
128 /* .bInterfaceNumber = DYNAMIC */
129 /* status endpoint is optional; this could be patched later */
130 .bNumEndpoints = 1,
131 .bInterfaceClass = USB_CLASS_COMM,
132 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
133 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
134 /* .iInterface = DYNAMIC */
135};
136
137static struct usb_cdc_header_desc header_desc __initdata = {
138 .bLength = sizeof header_desc,
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
141
142 .bcdCDC = __constant_cpu_to_le16(0x0110),
143};
144
145static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
146 .bLength = sizeof call_mgmt_descriptor,
147 .bDescriptorType = USB_DT_CS_INTERFACE,
148 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
149
150 .bmCapabilities = 0x00,
151 .bDataInterface = 0x01,
152};
153
154static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
155 .bLength = sizeof acm_descriptor,
156 .bDescriptorType = USB_DT_CS_INTERFACE,
157 .bDescriptorSubType = USB_CDC_ACM_TYPE,
158
159 .bmCapabilities = 0x00,
160};
161
162static struct usb_cdc_union_desc rndis_union_desc __initdata = {
163 .bLength = sizeof(rndis_union_desc),
164 .bDescriptorType = USB_DT_CS_INTERFACE,
165 .bDescriptorSubType = USB_CDC_UNION_TYPE,
166 /* .bMasterInterface0 = DYNAMIC */
167 /* .bSlaveInterface0 = DYNAMIC */
168};
169
170/* the data interface has two bulk endpoints */
171
172static struct usb_interface_descriptor rndis_data_intf __initdata = {
173 .bLength = sizeof rndis_data_intf,
174 .bDescriptorType = USB_DT_INTERFACE,
175
176 /* .bInterfaceNumber = DYNAMIC */
177 .bAlternateSetting = 1,
178 .bNumEndpoints = 2,
179 .bInterfaceClass = USB_CLASS_CDC_DATA,
180 .bInterfaceSubClass = 0,
181 .bInterfaceProtocol = 0,
182 /* .iInterface = DYNAMIC */
183};
184
185/* full speed support: */
186
187static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
188 .bLength = USB_DT_ENDPOINT_SIZE,
189 .bDescriptorType = USB_DT_ENDPOINT,
190
191 .bEndpointAddress = USB_DIR_IN,
192 .bmAttributes = USB_ENDPOINT_XFER_INT,
193 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
194 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
195};
196
197static struct usb_endpoint_descriptor fs_in_desc __initdata = {
198 .bLength = USB_DT_ENDPOINT_SIZE,
199 .bDescriptorType = USB_DT_ENDPOINT,
200
201 .bEndpointAddress = USB_DIR_IN,
202 .bmAttributes = USB_ENDPOINT_XFER_BULK,
203};
204
205static struct usb_endpoint_descriptor fs_out_desc __initdata = {
206 .bLength = USB_DT_ENDPOINT_SIZE,
207 .bDescriptorType = USB_DT_ENDPOINT,
208
209 .bEndpointAddress = USB_DIR_OUT,
210 .bmAttributes = USB_ENDPOINT_XFER_BULK,
211};
212
213static struct usb_descriptor_header *eth_fs_function[] __initdata = {
214 /* control interface matches ACM, not Ethernet */
215 (struct usb_descriptor_header *) &rndis_control_intf,
216 (struct usb_descriptor_header *) &header_desc,
217 (struct usb_descriptor_header *) &call_mgmt_descriptor,
218 (struct usb_descriptor_header *) &acm_descriptor,
219 (struct usb_descriptor_header *) &rndis_union_desc,
220 (struct usb_descriptor_header *) &fs_notify_desc,
221 /* data interface has no altsetting */
222 (struct usb_descriptor_header *) &rndis_data_intf,
223 (struct usb_descriptor_header *) &fs_in_desc,
224 (struct usb_descriptor_header *) &fs_out_desc,
225 NULL,
226};
227
228/* high speed support: */
229
230static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233
234 .bEndpointAddress = USB_DIR_IN,
235 .bmAttributes = USB_ENDPOINT_XFER_INT,
236 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
237 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
238};
239static struct usb_endpoint_descriptor hs_in_desc __initdata = {
240 .bLength = USB_DT_ENDPOINT_SIZE,
241 .bDescriptorType = USB_DT_ENDPOINT,
242
243 .bEndpointAddress = USB_DIR_IN,
244 .bmAttributes = USB_ENDPOINT_XFER_BULK,
245 .wMaxPacketSize = __constant_cpu_to_le16(512),
246};
247
248static struct usb_endpoint_descriptor hs_out_desc __initdata = {
249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251
252 .bEndpointAddress = USB_DIR_OUT,
253 .bmAttributes = USB_ENDPOINT_XFER_BULK,
254 .wMaxPacketSize = __constant_cpu_to_le16(512),
255};
256
257static struct usb_descriptor_header *eth_hs_function[] __initdata = {
258 /* control interface matches ACM, not Ethernet */
259 (struct usb_descriptor_header *) &rndis_control_intf,
260 (struct usb_descriptor_header *) &header_desc,
261 (struct usb_descriptor_header *) &call_mgmt_descriptor,
262 (struct usb_descriptor_header *) &acm_descriptor,
263 (struct usb_descriptor_header *) &rndis_union_desc,
264 (struct usb_descriptor_header *) &hs_notify_desc,
265 /* data interface has no altsetting */
266 (struct usb_descriptor_header *) &rndis_data_intf,
267 (struct usb_descriptor_header *) &hs_in_desc,
268 (struct usb_descriptor_header *) &hs_out_desc,
269 NULL,
270};
271
272/* string descriptors: */
273
274static struct usb_string rndis_string_defs[] = {
275 [0].s = "RNDIS Communications Control",
276 [1].s = "RNDIS Ethernet Data",
277 { } /* end of list */
278};
279
280static struct usb_gadget_strings rndis_string_table = {
281 .language = 0x0409, /* en-us */
282 .strings = rndis_string_defs,
283};
284
285static struct usb_gadget_strings *rndis_strings[] = {
286 &rndis_string_table,
287 NULL,
288};
289
290/*-------------------------------------------------------------------------*/
291
292static struct sk_buff *rndis_add_header(struct sk_buff *skb)
293{
294 skb = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
295 if (skb)
296 rndis_add_hdr(skb);
297 return skb;
298}
299
300static void rndis_response_available(void *_rndis)
301{
302 struct f_rndis *rndis = _rndis;
303 struct usb_request *req = rndis->notify_req;
304 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
305 __le32 *data = req->buf;
306 int status;
307
308 if (atomic_inc_return(&rndis->notify_count))
309 return;
310
311 /* Send RNDIS RESPONSE_AVAILABLE notification; a
312 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
313 *
314 * This is the only notification defined by RNDIS.
315 */
316 data[0] = cpu_to_le32(1);
317 data[1] = cpu_to_le32(0);
318
319 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
320 if (status) {
321 atomic_dec(&rndis->notify_count);
322 DBG(cdev, "notify/0 --> %d\n", status);
323 }
324}
325
326static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
327{
328 struct f_rndis *rndis = req->context;
329 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
330 int status = req->status;
331
332 /* after TX:
333 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
334 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
335 */
336 switch (status) {
337 case -ECONNRESET:
338 case -ESHUTDOWN:
339 /* connection gone */
340 atomic_set(&rndis->notify_count, 0);
341 break;
342 default:
343 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
344 ep->name, status,
345 req->actual, req->length);
346 /* FALLTHROUGH */
347 case 0:
348 if (ep != rndis->notify)
349 break;
350
351 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
352 * notifications by resending until we're done
353 */
354 if (atomic_dec_and_test(&rndis->notify_count))
355 break;
356 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
357 if (status) {
358 atomic_dec(&rndis->notify_count);
359 DBG(cdev, "notify/1 --> %d\n", status);
360 }
361 break;
362 }
363}
364
365static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
366{
367 struct f_rndis *rndis = req->context;
368 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
369 int status;
370
371 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
372// spin_lock(&dev->lock);
373 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
374 if (status < 0)
375 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
376 status, req->actual, req->length);
377// spin_unlock(&dev->lock);
378}
379
380static int
381rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
382{
383 struct f_rndis *rndis = func_to_rndis(f);
384 struct usb_composite_dev *cdev = f->config->cdev;
385 struct usb_request *req = cdev->req;
386 int value = -EOPNOTSUPP;
387 u16 w_index = le16_to_cpu(ctrl->wIndex);
388 u16 w_value = le16_to_cpu(ctrl->wValue);
389 u16 w_length = le16_to_cpu(ctrl->wLength);
390
391 /* composite driver infrastructure handles everything except
392 * CDC class messages; interface activation uses set_alt().
393 */
394 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
395
396 /* RNDIS uses the CDC command encapsulation mechanism to implement
397 * an RPC scheme, with much getting/setting of attributes by OID.
398 */
399 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
400 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
401 if (w_length > req->length || w_value
402 || w_index != rndis->ctrl_id)
403 goto invalid;
404 /* read the request; process it later */
405 value = w_length;
406 req->complete = rndis_command_complete;
407 req->context = rndis;
408 /* later, rndis_response_available() sends a notification */
409 break;
410
411 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
412 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
413 if (w_value || w_index != rndis->ctrl_id)
414 goto invalid;
415 else {
416 u8 *buf;
417 u32 n;
418
419 /* return the result */
420 buf = rndis_get_next_response(rndis->config, &n);
421 if (buf) {
422 memcpy(req->buf, buf, n);
423 req->complete = rndis_response_complete;
424 rndis_free_response(rndis->config, buf);
425 value = n;
426 }
427 /* else stalls ... spec says to avoid that */
428 }
429 break;
430
431 default:
432invalid:
433 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
434 ctrl->bRequestType, ctrl->bRequest,
435 w_value, w_index, w_length);
436 }
437
438 /* respond with data transfer or status phase? */
439 if (value >= 0) {
440 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
441 ctrl->bRequestType, ctrl->bRequest,
442 w_value, w_index, w_length);
443 req->zero = 0;
444 req->length = value;
445 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
446 if (value < 0)
447 ERROR(cdev, "rndis response on err %d\n", value);
448 }
449
450 /* device either stalls (value < 0) or reports success */
451 return value;
452}
453
454
455static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
456{
457 struct f_rndis *rndis = func_to_rndis(f);
458 struct usb_composite_dev *cdev = f->config->cdev;
459
460 /* we know alt == 0 */
461
462 if (intf == rndis->ctrl_id) {
463 if (rndis->notify->driver_data) {
464 VDBG(cdev, "reset rndis control %d\n", intf);
465 usb_ep_disable(rndis->notify);
466 } else {
467 VDBG(cdev, "init rndis ctrl %d\n", intf);
468 rndis->notify_desc = ep_choose(cdev->gadget,
469 rndis->hs.notify,
470 rndis->fs.notify);
471 }
472 usb_ep_enable(rndis->notify, rndis->notify_desc);
473 rndis->notify->driver_data = rndis;
474
475 } else if (intf == rndis->data_id) {
476 struct net_device *net;
477
478 if (rndis->port.in_ep->driver_data) {
479 DBG(cdev, "reset rndis\n");
480 gether_disconnect(&rndis->port);
481 } else {
482 DBG(cdev, "init rndis\n");
483 rndis->port.in = ep_choose(cdev->gadget,
484 rndis->hs.in, rndis->fs.in);
485 rndis->port.out = ep_choose(cdev->gadget,
486 rndis->hs.out, rndis->fs.out);
487 }
488
489 /* Avoid ZLPs; they can be troublesome. */
490 rndis->port.is_zlp_ok = false;
491
492 /* RNDIS should be in the "RNDIS uninitialized" state,
493 * either never activated or after rndis_uninit().
494 *
495 * We don't want data to flow here until a nonzero packet
496 * filter is set, at which point it enters "RNDIS data
497 * initialized" state ... but we do want the endpoints
498 * to be activated. It's a strange little state.
499 *
500 * REVISIT the RNDIS gadget code has done this wrong for a
501 * very long time. We need another call to the link layer
502 * code -- gether_updown(...bool) maybe -- to do it right.
503 */
504 rndis->port.cdc_filter = 0;
505
506 DBG(cdev, "RNDIS RX/TX early activation ... \n");
507 net = gether_connect(&rndis->port);
508 if (IS_ERR(net))
509 return PTR_ERR(net);
510
511 rndis_set_param_dev(rndis->config, net,
512 &rndis->port.cdc_filter);
513 } else
514 goto fail;
515
516 return 0;
517fail:
518 return -EINVAL;
519}
520
521static void rndis_disable(struct usb_function *f)
522{
523 struct f_rndis *rndis = func_to_rndis(f);
524 struct usb_composite_dev *cdev = f->config->cdev;
525
526 if (!rndis->notify->driver_data)
527 return;
528
529 DBG(cdev, "rndis deactivated\n");
530
531 rndis_uninit(rndis->config);
532 gether_disconnect(&rndis->port);
533
534 usb_ep_disable(rndis->notify);
535 rndis->notify->driver_data = NULL;
536}
537
538/*-------------------------------------------------------------------------*/
539
540/*
541 * This isn't quite the same mechanism as CDC Ethernet, since the
542 * notification scheme passes less data, but the same set of link
543 * states must be tested. A key difference is that altsettings are
544 * not used to tell whether the link should send packets or not.
545 */
546
547static void rndis_open(struct gether *geth)
548{
549 struct f_rndis *rndis = func_to_rndis(&geth->func);
550 struct usb_composite_dev *cdev = geth->func.config->cdev;
551
552 DBG(cdev, "%s\n", __func__);
553
554 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
555 bitrate(cdev->gadget) / 100);
556 rndis_signal_connect(rndis->config);
557}
558
559static void rndis_close(struct gether *geth)
560{
561 struct f_rndis *rndis = func_to_rndis(&geth->func);
562
563 DBG(geth->func.config->cdev, "%s\n", __func__);
564
565 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
566 rndis_signal_disconnect(rndis->config);
567}
568
569/*-------------------------------------------------------------------------*/
570
571/* ethernet function driver setup/binding */
572
573static int __init
574rndis_bind(struct usb_configuration *c, struct usb_function *f)
575{
576 struct usb_composite_dev *cdev = c->cdev;
577 struct f_rndis *rndis = func_to_rndis(f);
578 int status;
579 struct usb_ep *ep;
580
581 /* allocate instance-specific interface IDs */
582 status = usb_interface_id(c, f);
583 if (status < 0)
584 goto fail;
585 rndis->ctrl_id = status;
586
587 rndis_control_intf.bInterfaceNumber = status;
588 rndis_union_desc.bMasterInterface0 = status;
589
590 status = usb_interface_id(c, f);
591 if (status < 0)
592 goto fail;
593 rndis->data_id = status;
594
595 rndis_data_intf.bInterfaceNumber = status;
596 rndis_union_desc.bSlaveInterface0 = status;
597
598 status = -ENODEV;
599
600 /* allocate instance-specific endpoints */
601 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
602 if (!ep)
603 goto fail;
604 rndis->port.in_ep = ep;
605 ep->driver_data = cdev; /* claim */
606
607 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
608 if (!ep)
609 goto fail;
610 rndis->port.out_ep = ep;
611 ep->driver_data = cdev; /* claim */
612
613 /* NOTE: a status/notification endpoint is, strictly speaking,
614 * optional. We don't treat it that way though! It's simpler,
615 * and some newer profiles don't treat it as optional.
616 */
617 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
618 if (!ep)
619 goto fail;
620 rndis->notify = ep;
621 ep->driver_data = cdev; /* claim */
622
623 status = -ENOMEM;
624
625 /* allocate notification request and buffer */
626 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
627 if (!rndis->notify_req)
628 goto fail;
629 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
630 if (!rndis->notify_req->buf)
631 goto fail;
632 rndis->notify_req->length = STATUS_BYTECOUNT;
633 rndis->notify_req->context = rndis;
634 rndis->notify_req->complete = rndis_response_complete;
635
636 /* copy descriptors, and track endpoint copies */
637 f->descriptors = usb_copy_descriptors(eth_fs_function);
638 if (!f->descriptors)
639 goto fail;
640
641 rndis->fs.in = usb_find_endpoint(eth_fs_function,
642 f->descriptors, &fs_in_desc);
643 rndis->fs.out = usb_find_endpoint(eth_fs_function,
644 f->descriptors, &fs_out_desc);
645 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
646 f->descriptors, &fs_notify_desc);
647
648 /* support all relevant hardware speeds... we expect that when
649 * hardware is dual speed, all bulk-capable endpoints work at
650 * both speeds
651 */
652 if (gadget_is_dualspeed(c->cdev->gadget)) {
653 hs_in_desc.bEndpointAddress =
654 fs_in_desc.bEndpointAddress;
655 hs_out_desc.bEndpointAddress =
656 fs_out_desc.bEndpointAddress;
657
658 /* copy descriptors, and track endpoint copies */
659 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
660
661 if (!f->hs_descriptors)
662 goto fail;
663
664 rndis->hs.in = usb_find_endpoint(eth_hs_function,
665 f->hs_descriptors, &hs_in_desc);
666 rndis->hs.out = usb_find_endpoint(eth_hs_function,
667 f->hs_descriptors, &hs_out_desc);
668 }
669
670 rndis->port.open = rndis_open;
671 rndis->port.close = rndis_close;
672
673 status = rndis_register(rndis_response_available, rndis);
674 if (status < 0)
675 goto fail;
676 rndis->config = status;
677
678 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
679 rndis_set_host_mac(rndis->config, rndis->ethaddr);
680
681#if 0
682// FIXME
683 if (rndis_set_param_vendor(rndis->config, vendorID,
684 manufacturer))
685 goto fail0;
686#endif
687
688 /* NOTE: all that is done without knowing or caring about
689 * the network link ... which is unavailable to this code
690 * until we're activated via set_alt().
691 */
692
693 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
694 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
695 rndis->port.in_ep->name, rndis->port.out_ep->name,
696 rndis->notify->name);
697 return 0;
698
699fail:
700 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
701 usb_free_descriptors(f->hs_descriptors);
702 if (f->descriptors)
703 usb_free_descriptors(f->descriptors);
704
705 if (rndis->notify_req) {
706 kfree(rndis->notify_req->buf);
707 usb_ep_free_request(rndis->notify, rndis->notify_req);
708 }
709
710 /* we might as well release our claims on endpoints */
711 if (rndis->notify)
712 rndis->notify->driver_data = NULL;
713 if (rndis->port.out)
714 rndis->port.out_ep->driver_data = NULL;
715 if (rndis->port.in)
716 rndis->port.in_ep->driver_data = NULL;
717
718 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
719
720 return status;
721}
722
723static void
724rndis_unbind(struct usb_configuration *c, struct usb_function *f)
725{
726 struct f_rndis *rndis = func_to_rndis(f);
727
728 rndis_deregister(rndis->config);
729 rndis_exit();
730
731 if (gadget_is_dualspeed(c->cdev->gadget))
732 usb_free_descriptors(f->hs_descriptors);
733 usb_free_descriptors(f->descriptors);
734
735 kfree(rndis->notify_req->buf);
736 usb_ep_free_request(rndis->notify, rndis->notify_req);
737
738 kfree(rndis);
739}
740
741/* Some controllers can't support RNDIS ... */
742static inline bool can_support_rndis(struct usb_configuration *c)
743{
744 /* only two endpoints on sa1100 */
745 if (gadget_is_sa1100(c->cdev->gadget))
746 return false;
747
748 /* everything else is *presumably* fine */
749 return true;
750}
751
752/**
753 * rndis_bind_config - add RNDIS network link to a configuration
754 * @c: the configuration to support the network link
755 * @ethaddr: a buffer in which the ethernet address of the host side
756 * side of the link was recorded
757 * Context: single threaded during gadget setup
758 *
759 * Returns zero on success, else negative errno.
760 *
761 * Caller must have called @gether_setup(). Caller is also responsible
762 * for calling @gether_cleanup() before module unload.
763 */
764int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
765{
766 struct f_rndis *rndis;
767 int status;
768
769 if (!can_support_rndis(c) || !ethaddr)
770 return -EINVAL;
771
772 /* maybe allocate device-global string IDs */
773 if (rndis_string_defs[0].id == 0) {
774
775 /* ... and setup RNDIS itself */
776 status = rndis_init();
777 if (status < 0)
778 return status;
779
780 /* control interface label */
781 status = usb_string_id(c->cdev);
782 if (status < 0)
783 return status;
784 rndis_string_defs[0].id = status;
785 rndis_control_intf.iInterface = status;
786
787 /* data interface label */
788 status = usb_string_id(c->cdev);
789 if (status < 0)
790 return status;
791 rndis_string_defs[1].id = status;
792 rndis_data_intf.iInterface = status;
793 }
794
795 /* allocate and initialize one new instance */
796 status = -ENOMEM;
797 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
798 if (!rndis)
799 goto fail;
800
801 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
802
803 /* RNDIS activates when the host changes this filter */
804 rndis->port.cdc_filter = 0;
805
806 /* RNDIS has special (and complex) framing */
807 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
808 rndis->port.wrap = rndis_add_header;
809 rndis->port.unwrap = rndis_rm_hdr;
810
811 rndis->port.func.name = "rndis";
812 rndis->port.func.strings = rndis_strings;
813 /* descriptors are per-instance copies */
814 rndis->port.func.bind = rndis_bind;
815 rndis->port.func.unbind = rndis_unbind;
816 rndis->port.func.set_alt = rndis_set_alt;
817 rndis->port.func.setup = rndis_setup;
818 rndis->port.func.disable = rndis_disable;
819
820 status = usb_add_function(c, &rndis->port.func);
821 if (status) {
822 kfree(rndis);
823fail:
824 rndis_exit();
825 }
826 return status;
827}
diff --git a/drivers/usb/gadget/u_ether.h b/drivers/usb/gadget/u_ether.h
index 8abf197ab402..0d1f7ae3b071 100644
--- a/drivers/usb/gadget/u_ether.h
+++ b/drivers/usb/gadget/u_ether.h
@@ -34,10 +34,13 @@
34/* 34/*
35 * This represents the USB side of an "ethernet" link, managed by a USB 35 * This represents the USB side of an "ethernet" link, managed by a USB
36 * function which provides control and (maybe) framing. Two functions 36 * function which provides control and (maybe) framing. Two functions
37 * in different configurations could share the same ethernet link/netdev. 37 * in different configurations could share the same ethernet link/netdev,
38 * using different host interaction models.
38 * 39 *
39 * There is currently a limitation that one instance of this function 40 * There is a current limitation that only one instance of this link may
40 * may be present in any given configuration. 41 * be present in any given configuration. When that's a problem, network
42 * layer facilities can be used to package multiple logical links on this
43 * single "physical" one.
41 */ 44 */
42struct gether { 45struct gether {
43 struct usb_function func; 46 struct usb_function func;
@@ -107,4 +110,18 @@ static inline bool can_support_ecm(struct usb_gadget *gadget)
107int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 110int geth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
108int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]); 111int ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
109 112
113#ifdef CONFIG_USB_ETH_RNDIS
114
115int rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
116
117#else
118
119static inline int
120rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
121{
122 return 0;
123}
124
125#endif
126
110#endif /* __U_ETHER_H */ 127#endif /* __U_ETHER_H */