blob: 659b3d9671c4fe3f0f31220cb4b3bde514d88d2b [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
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
David Brownell45fe3b82008-06-19 18:20:04 -070088 struct rndis_ep_descs fs;
David Brownell45fe3b82008-06-19 18:20:04 -070089 struct rndis_ep_descs hs;
90
91 struct usb_ep *notify;
92 struct usb_endpoint_descriptor *notify_desc;
93 struct usb_request *notify_req;
94 atomic_t notify_count;
95};
96
97static inline struct f_rndis *func_to_rndis(struct usb_function *f)
98{
99 return container_of(f, struct f_rndis, port.func);
100}
101
102/* peak (theoretical) bulk transfer rate in bits-per-second */
103static unsigned int bitrate(struct usb_gadget *g)
104{
105 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
106 return 13 * 512 * 8 * 1000 * 8;
107 else
108 return 19 * 64 * 1 * 1000 * 8;
109}
110
111/*-------------------------------------------------------------------------*/
112
113/*
114 */
115
116#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
117#define STATUS_BYTECOUNT 8 /* 8 bytes data */
118
119
120/* interface descriptor: */
121
122static struct usb_interface_descriptor rndis_control_intf __initdata = {
123 .bLength = sizeof rndis_control_intf,
124 .bDescriptorType = USB_DT_INTERFACE,
125
126 /* .bInterfaceNumber = DYNAMIC */
127 /* status endpoint is optional; this could be patched later */
128 .bNumEndpoints = 1,
129 .bInterfaceClass = USB_CLASS_COMM,
130 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
131 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
132 /* .iInterface = DYNAMIC */
133};
134
135static struct usb_cdc_header_desc header_desc __initdata = {
136 .bLength = sizeof header_desc,
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
139
140 .bcdCDC = __constant_cpu_to_le16(0x0110),
141};
142
143static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
144 .bLength = sizeof call_mgmt_descriptor,
145 .bDescriptorType = USB_DT_CS_INTERFACE,
146 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
147
148 .bmCapabilities = 0x00,
149 .bDataInterface = 0x01,
150};
151
152static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
153 .bLength = sizeof acm_descriptor,
154 .bDescriptorType = USB_DT_CS_INTERFACE,
155 .bDescriptorSubType = USB_CDC_ACM_TYPE,
156
157 .bmCapabilities = 0x00,
158};
159
160static struct usb_cdc_union_desc rndis_union_desc __initdata = {
161 .bLength = sizeof(rndis_union_desc),
162 .bDescriptorType = USB_DT_CS_INTERFACE,
163 .bDescriptorSubType = USB_CDC_UNION_TYPE,
164 /* .bMasterInterface0 = DYNAMIC */
165 /* .bSlaveInterface0 = DYNAMIC */
166};
167
168/* the data interface has two bulk endpoints */
169
170static struct usb_interface_descriptor rndis_data_intf __initdata = {
171 .bLength = sizeof rndis_data_intf,
172 .bDescriptorType = USB_DT_INTERFACE,
173
174 /* .bInterfaceNumber = DYNAMIC */
175 .bAlternateSetting = 1,
176 .bNumEndpoints = 2,
177 .bInterfaceClass = USB_CLASS_CDC_DATA,
178 .bInterfaceSubClass = 0,
179 .bInterfaceProtocol = 0,
180 /* .iInterface = DYNAMIC */
181};
182
183/* full speed support: */
184
185static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
186 .bLength = USB_DT_ENDPOINT_SIZE,
187 .bDescriptorType = USB_DT_ENDPOINT,
188
189 .bEndpointAddress = USB_DIR_IN,
190 .bmAttributes = USB_ENDPOINT_XFER_INT,
191 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
192 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
193};
194
195static struct usb_endpoint_descriptor fs_in_desc __initdata = {
196 .bLength = USB_DT_ENDPOINT_SIZE,
197 .bDescriptorType = USB_DT_ENDPOINT,
198
199 .bEndpointAddress = USB_DIR_IN,
200 .bmAttributes = USB_ENDPOINT_XFER_BULK,
201};
202
203static struct usb_endpoint_descriptor fs_out_desc __initdata = {
204 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT,
206
207 .bEndpointAddress = USB_DIR_OUT,
208 .bmAttributes = USB_ENDPOINT_XFER_BULK,
209};
210
211static struct usb_descriptor_header *eth_fs_function[] __initdata = {
212 /* control interface matches ACM, not Ethernet */
213 (struct usb_descriptor_header *) &rndis_control_intf,
214 (struct usb_descriptor_header *) &header_desc,
215 (struct usb_descriptor_header *) &call_mgmt_descriptor,
216 (struct usb_descriptor_header *) &acm_descriptor,
217 (struct usb_descriptor_header *) &rndis_union_desc,
218 (struct usb_descriptor_header *) &fs_notify_desc,
219 /* data interface has no altsetting */
220 (struct usb_descriptor_header *) &rndis_data_intf,
221 (struct usb_descriptor_header *) &fs_in_desc,
222 (struct usb_descriptor_header *) &fs_out_desc,
223 NULL,
224};
225
226/* high speed support: */
227
228static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
229 .bLength = USB_DT_ENDPOINT_SIZE,
230 .bDescriptorType = USB_DT_ENDPOINT,
231
232 .bEndpointAddress = USB_DIR_IN,
233 .bmAttributes = USB_ENDPOINT_XFER_INT,
234 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
235 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
236};
237static struct usb_endpoint_descriptor hs_in_desc __initdata = {
238 .bLength = USB_DT_ENDPOINT_SIZE,
239 .bDescriptorType = USB_DT_ENDPOINT,
240
241 .bEndpointAddress = USB_DIR_IN,
242 .bmAttributes = USB_ENDPOINT_XFER_BULK,
243 .wMaxPacketSize = __constant_cpu_to_le16(512),
244};
245
246static struct usb_endpoint_descriptor hs_out_desc __initdata = {
247 .bLength = USB_DT_ENDPOINT_SIZE,
248 .bDescriptorType = USB_DT_ENDPOINT,
249
250 .bEndpointAddress = USB_DIR_OUT,
251 .bmAttributes = USB_ENDPOINT_XFER_BULK,
252 .wMaxPacketSize = __constant_cpu_to_le16(512),
253};
254
255static struct usb_descriptor_header *eth_hs_function[] __initdata = {
256 /* control interface matches ACM, not Ethernet */
257 (struct usb_descriptor_header *) &rndis_control_intf,
258 (struct usb_descriptor_header *) &header_desc,
259 (struct usb_descriptor_header *) &call_mgmt_descriptor,
260 (struct usb_descriptor_header *) &acm_descriptor,
261 (struct usb_descriptor_header *) &rndis_union_desc,
262 (struct usb_descriptor_header *) &hs_notify_desc,
263 /* data interface has no altsetting */
264 (struct usb_descriptor_header *) &rndis_data_intf,
265 (struct usb_descriptor_header *) &hs_in_desc,
266 (struct usb_descriptor_header *) &hs_out_desc,
267 NULL,
268};
269
270/* string descriptors: */
271
272static struct usb_string rndis_string_defs[] = {
273 [0].s = "RNDIS Communications Control",
274 [1].s = "RNDIS Ethernet Data",
275 { } /* end of list */
276};
277
278static struct usb_gadget_strings rndis_string_table = {
279 .language = 0x0409, /* en-us */
280 .strings = rndis_string_defs,
281};
282
283static struct usb_gadget_strings *rndis_strings[] = {
284 &rndis_string_table,
285 NULL,
286};
287
288/*-------------------------------------------------------------------------*/
289
290static struct sk_buff *rndis_add_header(struct sk_buff *skb)
291{
292 skb = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
293 if (skb)
294 rndis_add_hdr(skb);
295 return skb;
296}
297
298static void rndis_response_available(void *_rndis)
299{
300 struct f_rndis *rndis = _rndis;
301 struct usb_request *req = rndis->notify_req;
302 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
303 __le32 *data = req->buf;
304 int status;
305
306 if (atomic_inc_return(&rndis->notify_count))
307 return;
308
309 /* Send RNDIS RESPONSE_AVAILABLE notification; a
310 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
311 *
312 * This is the only notification defined by RNDIS.
313 */
314 data[0] = cpu_to_le32(1);
315 data[1] = cpu_to_le32(0);
316
317 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
318 if (status) {
319 atomic_dec(&rndis->notify_count);
320 DBG(cdev, "notify/0 --> %d\n", status);
321 }
322}
323
324static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
325{
326 struct f_rndis *rndis = req->context;
327 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
328 int status = req->status;
329
330 /* after TX:
331 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
332 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
333 */
334 switch (status) {
335 case -ECONNRESET:
336 case -ESHUTDOWN:
337 /* connection gone */
338 atomic_set(&rndis->notify_count, 0);
339 break;
340 default:
341 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
342 ep->name, status,
343 req->actual, req->length);
344 /* FALLTHROUGH */
345 case 0:
346 if (ep != rndis->notify)
347 break;
348
349 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
350 * notifications by resending until we're done
351 */
352 if (atomic_dec_and_test(&rndis->notify_count))
353 break;
354 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
355 if (status) {
356 atomic_dec(&rndis->notify_count);
357 DBG(cdev, "notify/1 --> %d\n", status);
358 }
359 break;
360 }
361}
362
363static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
364{
365 struct f_rndis *rndis = req->context;
366 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
367 int status;
368
369 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
370// spin_lock(&dev->lock);
371 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
372 if (status < 0)
373 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
374 status, req->actual, req->length);
375// spin_unlock(&dev->lock);
376}
377
378static int
379rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
380{
381 struct f_rndis *rndis = func_to_rndis(f);
382 struct usb_composite_dev *cdev = f->config->cdev;
383 struct usb_request *req = cdev->req;
384 int value = -EOPNOTSUPP;
385 u16 w_index = le16_to_cpu(ctrl->wIndex);
386 u16 w_value = le16_to_cpu(ctrl->wValue);
387 u16 w_length = le16_to_cpu(ctrl->wLength);
388
389 /* composite driver infrastructure handles everything except
390 * CDC class messages; interface activation uses set_alt().
391 */
392 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
393
394 /* RNDIS uses the CDC command encapsulation mechanism to implement
395 * an RPC scheme, with much getting/setting of attributes by OID.
396 */
397 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
398 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
399 if (w_length > req->length || w_value
400 || w_index != rndis->ctrl_id)
401 goto invalid;
402 /* read the request; process it later */
403 value = w_length;
404 req->complete = rndis_command_complete;
405 req->context = rndis;
406 /* later, rndis_response_available() sends a notification */
407 break;
408
409 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
410 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
411 if (w_value || w_index != rndis->ctrl_id)
412 goto invalid;
413 else {
414 u8 *buf;
415 u32 n;
416
417 /* return the result */
418 buf = rndis_get_next_response(rndis->config, &n);
419 if (buf) {
420 memcpy(req->buf, buf, n);
421 req->complete = rndis_response_complete;
422 rndis_free_response(rndis->config, buf);
423 value = n;
424 }
425 /* else stalls ... spec says to avoid that */
426 }
427 break;
428
429 default:
430invalid:
431 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
432 ctrl->bRequestType, ctrl->bRequest,
433 w_value, w_index, w_length);
434 }
435
436 /* respond with data transfer or status phase? */
437 if (value >= 0) {
438 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
439 ctrl->bRequestType, ctrl->bRequest,
440 w_value, w_index, w_length);
441 req->zero = 0;
442 req->length = value;
443 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
444 if (value < 0)
445 ERROR(cdev, "rndis response on err %d\n", value);
446 }
447
448 /* device either stalls (value < 0) or reports success */
449 return value;
450}
451
452
453static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
454{
455 struct f_rndis *rndis = func_to_rndis(f);
456 struct usb_composite_dev *cdev = f->config->cdev;
457
458 /* we know alt == 0 */
459
460 if (intf == rndis->ctrl_id) {
461 if (rndis->notify->driver_data) {
462 VDBG(cdev, "reset rndis control %d\n", intf);
463 usb_ep_disable(rndis->notify);
464 } else {
465 VDBG(cdev, "init rndis ctrl %d\n", intf);
466 rndis->notify_desc = ep_choose(cdev->gadget,
467 rndis->hs.notify,
468 rndis->fs.notify);
469 }
470 usb_ep_enable(rndis->notify, rndis->notify_desc);
471 rndis->notify->driver_data = rndis;
472
473 } else if (intf == rndis->data_id) {
474 struct net_device *net;
475
476 if (rndis->port.in_ep->driver_data) {
477 DBG(cdev, "reset rndis\n");
478 gether_disconnect(&rndis->port);
479 } else {
480 DBG(cdev, "init rndis\n");
481 rndis->port.in = ep_choose(cdev->gadget,
482 rndis->hs.in, rndis->fs.in);
483 rndis->port.out = ep_choose(cdev->gadget,
484 rndis->hs.out, rndis->fs.out);
485 }
486
487 /* Avoid ZLPs; they can be troublesome. */
488 rndis->port.is_zlp_ok = false;
489
490 /* RNDIS should be in the "RNDIS uninitialized" state,
491 * either never activated or after rndis_uninit().
492 *
493 * We don't want data to flow here until a nonzero packet
494 * filter is set, at which point it enters "RNDIS data
495 * initialized" state ... but we do want the endpoints
496 * to be activated. It's a strange little state.
497 *
498 * REVISIT the RNDIS gadget code has done this wrong for a
499 * very long time. We need another call to the link layer
500 * code -- gether_updown(...bool) maybe -- to do it right.
501 */
502 rndis->port.cdc_filter = 0;
503
504 DBG(cdev, "RNDIS RX/TX early activation ... \n");
505 net = gether_connect(&rndis->port);
506 if (IS_ERR(net))
507 return PTR_ERR(net);
508
509 rndis_set_param_dev(rndis->config, net,
510 &rndis->port.cdc_filter);
511 } else
512 goto fail;
513
514 return 0;
515fail:
516 return -EINVAL;
517}
518
519static void rndis_disable(struct usb_function *f)
520{
521 struct f_rndis *rndis = func_to_rndis(f);
522 struct usb_composite_dev *cdev = f->config->cdev;
523
524 if (!rndis->notify->driver_data)
525 return;
526
527 DBG(cdev, "rndis deactivated\n");
528
529 rndis_uninit(rndis->config);
530 gether_disconnect(&rndis->port);
531
532 usb_ep_disable(rndis->notify);
533 rndis->notify->driver_data = NULL;
534}
535
536/*-------------------------------------------------------------------------*/
537
538/*
539 * This isn't quite the same mechanism as CDC Ethernet, since the
540 * notification scheme passes less data, but the same set of link
541 * states must be tested. A key difference is that altsettings are
542 * not used to tell whether the link should send packets or not.
543 */
544
545static void rndis_open(struct gether *geth)
546{
547 struct f_rndis *rndis = func_to_rndis(&geth->func);
548 struct usb_composite_dev *cdev = geth->func.config->cdev;
549
550 DBG(cdev, "%s\n", __func__);
551
552 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
553 bitrate(cdev->gadget) / 100);
554 rndis_signal_connect(rndis->config);
555}
556
557static void rndis_close(struct gether *geth)
558{
559 struct f_rndis *rndis = func_to_rndis(&geth->func);
560
561 DBG(geth->func.config->cdev, "%s\n", __func__);
562
563 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
564 rndis_signal_disconnect(rndis->config);
565}
566
567/*-------------------------------------------------------------------------*/
568
569/* ethernet function driver setup/binding */
570
571static int __init
572rndis_bind(struct usb_configuration *c, struct usb_function *f)
573{
574 struct usb_composite_dev *cdev = c->cdev;
575 struct f_rndis *rndis = func_to_rndis(f);
576 int status;
577 struct usb_ep *ep;
578
579 /* allocate instance-specific interface IDs */
580 status = usb_interface_id(c, f);
581 if (status < 0)
582 goto fail;
583 rndis->ctrl_id = status;
584
585 rndis_control_intf.bInterfaceNumber = status;
586 rndis_union_desc.bMasterInterface0 = status;
587
588 status = usb_interface_id(c, f);
589 if (status < 0)
590 goto fail;
591 rndis->data_id = status;
592
593 rndis_data_intf.bInterfaceNumber = status;
594 rndis_union_desc.bSlaveInterface0 = status;
595
596 status = -ENODEV;
597
598 /* allocate instance-specific endpoints */
599 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
600 if (!ep)
601 goto fail;
602 rndis->port.in_ep = ep;
603 ep->driver_data = cdev; /* claim */
604
605 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
606 if (!ep)
607 goto fail;
608 rndis->port.out_ep = ep;
609 ep->driver_data = cdev; /* claim */
610
611 /* NOTE: a status/notification endpoint is, strictly speaking,
612 * optional. We don't treat it that way though! It's simpler,
613 * and some newer profiles don't treat it as optional.
614 */
615 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
616 if (!ep)
617 goto fail;
618 rndis->notify = ep;
619 ep->driver_data = cdev; /* claim */
620
621 status = -ENOMEM;
622
623 /* allocate notification request and buffer */
624 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
625 if (!rndis->notify_req)
626 goto fail;
627 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
628 if (!rndis->notify_req->buf)
629 goto fail;
630 rndis->notify_req->length = STATUS_BYTECOUNT;
631 rndis->notify_req->context = rndis;
632 rndis->notify_req->complete = rndis_response_complete;
633
634 /* copy descriptors, and track endpoint copies */
635 f->descriptors = usb_copy_descriptors(eth_fs_function);
636 if (!f->descriptors)
637 goto fail;
638
639 rndis->fs.in = usb_find_endpoint(eth_fs_function,
640 f->descriptors, &fs_in_desc);
641 rndis->fs.out = usb_find_endpoint(eth_fs_function,
642 f->descriptors, &fs_out_desc);
643 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
644 f->descriptors, &fs_notify_desc);
645
646 /* support all relevant hardware speeds... we expect that when
647 * hardware is dual speed, all bulk-capable endpoints work at
648 * both speeds
649 */
650 if (gadget_is_dualspeed(c->cdev->gadget)) {
651 hs_in_desc.bEndpointAddress =
652 fs_in_desc.bEndpointAddress;
653 hs_out_desc.bEndpointAddress =
654 fs_out_desc.bEndpointAddress;
655
656 /* copy descriptors, and track endpoint copies */
657 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
658
659 if (!f->hs_descriptors)
660 goto fail;
661
662 rndis->hs.in = usb_find_endpoint(eth_hs_function,
663 f->hs_descriptors, &hs_in_desc);
664 rndis->hs.out = usb_find_endpoint(eth_hs_function,
665 f->hs_descriptors, &hs_out_desc);
666 }
667
668 rndis->port.open = rndis_open;
669 rndis->port.close = rndis_close;
670
671 status = rndis_register(rndis_response_available, rndis);
672 if (status < 0)
673 goto fail;
674 rndis->config = status;
675
676 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
677 rndis_set_host_mac(rndis->config, rndis->ethaddr);
678
679#if 0
680// FIXME
681 if (rndis_set_param_vendor(rndis->config, vendorID,
682 manufacturer))
683 goto fail0;
684#endif
685
686 /* NOTE: all that is done without knowing or caring about
687 * the network link ... which is unavailable to this code
688 * until we're activated via set_alt().
689 */
690
691 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
692 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
693 rndis->port.in_ep->name, rndis->port.out_ep->name,
694 rndis->notify->name);
695 return 0;
696
697fail:
698 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
699 usb_free_descriptors(f->hs_descriptors);
700 if (f->descriptors)
701 usb_free_descriptors(f->descriptors);
702
703 if (rndis->notify_req) {
704 kfree(rndis->notify_req->buf);
705 usb_ep_free_request(rndis->notify, rndis->notify_req);
706 }
707
708 /* we might as well release our claims on endpoints */
709 if (rndis->notify)
710 rndis->notify->driver_data = NULL;
711 if (rndis->port.out)
712 rndis->port.out_ep->driver_data = NULL;
713 if (rndis->port.in)
714 rndis->port.in_ep->driver_data = NULL;
715
716 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
717
718 return status;
719}
720
721static void
722rndis_unbind(struct usb_configuration *c, struct usb_function *f)
723{
724 struct f_rndis *rndis = func_to_rndis(f);
725
726 rndis_deregister(rndis->config);
727 rndis_exit();
728
729 if (gadget_is_dualspeed(c->cdev->gadget))
730 usb_free_descriptors(f->hs_descriptors);
731 usb_free_descriptors(f->descriptors);
732
733 kfree(rndis->notify_req->buf);
734 usb_ep_free_request(rndis->notify, rndis->notify_req);
735
736 kfree(rndis);
737}
738
739/* Some controllers can't support RNDIS ... */
740static inline bool can_support_rndis(struct usb_configuration *c)
741{
742 /* only two endpoints on sa1100 */
743 if (gadget_is_sa1100(c->cdev->gadget))
744 return false;
745
746 /* everything else is *presumably* fine */
747 return true;
748}
749
750/**
751 * rndis_bind_config - add RNDIS network link to a configuration
752 * @c: the configuration to support the network link
753 * @ethaddr: a buffer in which the ethernet address of the host side
754 * side of the link was recorded
755 * Context: single threaded during gadget setup
756 *
757 * Returns zero on success, else negative errno.
758 *
759 * Caller must have called @gether_setup(). Caller is also responsible
760 * for calling @gether_cleanup() before module unload.
761 */
762int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
763{
764 struct f_rndis *rndis;
765 int status;
766
767 if (!can_support_rndis(c) || !ethaddr)
768 return -EINVAL;
769
770 /* maybe allocate device-global string IDs */
771 if (rndis_string_defs[0].id == 0) {
772
773 /* ... and setup RNDIS itself */
774 status = rndis_init();
775 if (status < 0)
776 return status;
777
778 /* control interface label */
779 status = usb_string_id(c->cdev);
780 if (status < 0)
781 return status;
782 rndis_string_defs[0].id = status;
783 rndis_control_intf.iInterface = status;
784
785 /* data interface label */
786 status = usb_string_id(c->cdev);
787 if (status < 0)
788 return status;
789 rndis_string_defs[1].id = status;
790 rndis_data_intf.iInterface = status;
791 }
792
793 /* allocate and initialize one new instance */
794 status = -ENOMEM;
795 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
796 if (!rndis)
797 goto fail;
798
799 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
800
801 /* RNDIS activates when the host changes this filter */
802 rndis->port.cdc_filter = 0;
803
804 /* RNDIS has special (and complex) framing */
805 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
806 rndis->port.wrap = rndis_add_header;
807 rndis->port.unwrap = rndis_rm_hdr;
808
809 rndis->port.func.name = "rndis";
810 rndis->port.func.strings = rndis_strings;
811 /* descriptors are per-instance copies */
812 rndis->port.func.bind = rndis_bind;
813 rndis->port.func.unbind = rndis_unbind;
814 rndis->port.func.set_alt = rndis_set_alt;
815 rndis->port.func.setup = rndis_setup;
816 rndis->port.func.disable = rndis_disable;
817
818 status = usb_add_function(c, &rndis->port.func);
819 if (status) {
820 kfree(rndis);
821fail:
822 rndis_exit();
823 }
824 return status;
825}