2 * f_rndis.c -- RNDIS link function driver
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 * Copyright (C) 2009 Samsung Electronics
8 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
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.
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.
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
25 /* #define VERBOSE_DEBUG */
27 #include <linux/slab.h>
28 #include <linux/kernel.h>
29 #include <linux/device.h>
30 #include <linux/etherdevice.h>
31 #include <linux/usb/android_composite.h>
33 #include <asm/atomic.h>
40 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
41 * been promoted instead of the standard CDC Ethernet. The published RNDIS
42 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
43 * ActiveSync have even worse status in terms of specification.
45 * In short: it's a protocol controlled by (and for) Microsoft, not for an
46 * Open ecosystem or markets. Linux supports it *only* because Microsoft
47 * doesn't support the CDC Ethernet standard.
49 * The RNDIS data transfer model is complex, with multiple Ethernet packets
50 * per USB message, and out of band data. The control model is built around
51 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
52 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
53 * useless (they're ignored). RNDIS expects to be the only function in its
54 * configuration, so it's no real help if you need composite devices; and
55 * it expects to be the first configuration too.
57 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
58 * discount the fluff that its RPC can be made to deliver: it doesn't need
59 * a NOP altsetting for the data interface. That lets it work on some of the
60 * "so smart it's stupid" hardware which takes over configuration changes
61 * from the software, and adds restrictions like "no altsettings".
63 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
64 * have all sorts of contrary-to-specification oddities that can prevent
65 * them from working sanely. Since bugfixes (or accurate specs, letting
66 * Linux work around those bugs) are unlikely to ever come from MSFT, you
67 * may want to avoid using RNDIS on purely operational grounds.
69 * Omissions from the RNDIS 1.0 specification include:
71 * - Power management ... references data that's scattered around lots
72 * of other documentation, which is incorrect/incomplete there too.
74 * - There are various undocumented protocol requirements, like the need
75 * to send garbage in some control-OUT messages.
77 * - MS-Windows drivers sometimes emit undocumented requests.
80 struct rndis_ep_descs {
81 struct usb_endpoint_descriptor *in;
82 struct usb_endpoint_descriptor *out;
83 struct usb_endpoint_descriptor *notify;
92 struct rndis_ep_descs fs;
93 struct rndis_ep_descs hs;
95 struct usb_ep *notify;
96 struct usb_endpoint_descriptor *notify_desc;
97 struct usb_request *notify_req;
98 atomic_t notify_count;
101 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
103 return container_of(f, struct f_rndis, port.func);
106 /* peak (theoretical) bulk transfer rate in bits-per-second */
107 static unsigned int bitrate(struct usb_gadget *g)
109 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
110 return 13 * 512 * 8 * 1000 * 8;
112 return 19 * 64 * 1 * 1000 * 8;
115 /*-------------------------------------------------------------------------*/
120 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
121 #define STATUS_BYTECOUNT 8 /* 8 bytes data */
124 /* interface descriptor: */
126 static struct usb_interface_descriptor rndis_control_intf = {
127 .bLength = sizeof rndis_control_intf,
128 .bDescriptorType = USB_DT_INTERFACE,
130 /* .bInterfaceNumber = DYNAMIC */
131 /* status endpoint is optional; this could be patched later */
133 #ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
134 /* "Wireless" RNDIS; auto-detected by Windows */
135 .bInterfaceClass = USB_CLASS_WIRELESS_CONTROLLER,
136 .bInterfaceSubClass = 1,
137 .bInterfaceProtocol = 3,
139 .bInterfaceClass = USB_CLASS_COMM,
140 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
141 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
143 /* .iInterface = DYNAMIC */
146 static struct usb_cdc_header_desc header_desc = {
147 .bLength = sizeof header_desc,
148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
151 .bcdCDC = cpu_to_le16(0x0110),
154 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
155 .bLength = sizeof call_mgmt_descriptor,
156 .bDescriptorType = USB_DT_CS_INTERFACE,
157 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
159 .bmCapabilities = 0x00,
160 .bDataInterface = 0x01,
163 static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
164 .bLength = sizeof rndis_acm_descriptor,
165 .bDescriptorType = USB_DT_CS_INTERFACE,
166 .bDescriptorSubType = USB_CDC_ACM_TYPE,
168 .bmCapabilities = 0x00,
171 static struct usb_cdc_union_desc rndis_union_desc = {
172 .bLength = sizeof(rndis_union_desc),
173 .bDescriptorType = USB_DT_CS_INTERFACE,
174 .bDescriptorSubType = USB_CDC_UNION_TYPE,
175 /* .bMasterInterface0 = DYNAMIC */
176 /* .bSlaveInterface0 = DYNAMIC */
179 /* the data interface has two bulk endpoints */
181 static struct usb_interface_descriptor rndis_data_intf = {
182 .bLength = sizeof rndis_data_intf,
183 .bDescriptorType = USB_DT_INTERFACE,
185 /* .bInterfaceNumber = DYNAMIC */
187 .bInterfaceClass = USB_CLASS_CDC_DATA,
188 .bInterfaceSubClass = 0,
189 .bInterfaceProtocol = 0,
190 /* .iInterface = DYNAMIC */
194 static struct usb_interface_assoc_descriptor
195 rndis_iad_descriptor = {
196 .bLength = sizeof rndis_iad_descriptor,
197 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
199 .bFirstInterface = 0, /* XXX, hardcoded */
200 .bInterfaceCount = 2, // control + data
201 .bFunctionClass = USB_CLASS_COMM,
202 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
203 .bFunctionProtocol = USB_CDC_PROTO_NONE,
204 /* .iFunction = DYNAMIC */
207 /* full speed support: */
209 static struct usb_endpoint_descriptor fs_notify_desc = {
210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
213 .bEndpointAddress = USB_DIR_IN,
214 .bmAttributes = USB_ENDPOINT_XFER_INT,
215 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
216 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
219 static struct usb_endpoint_descriptor fs_in_desc = {
220 .bLength = USB_DT_ENDPOINT_SIZE,
221 .bDescriptorType = USB_DT_ENDPOINT,
223 .bEndpointAddress = USB_DIR_IN,
224 .bmAttributes = USB_ENDPOINT_XFER_BULK,
227 static struct usb_endpoint_descriptor fs_out_desc = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
231 .bEndpointAddress = USB_DIR_OUT,
232 .bmAttributes = USB_ENDPOINT_XFER_BULK,
235 static struct usb_descriptor_header *eth_fs_function[] = {
236 (struct usb_descriptor_header *) &rndis_iad_descriptor,
237 /* control interface matches ACM, not Ethernet */
238 (struct usb_descriptor_header *) &rndis_control_intf,
239 (struct usb_descriptor_header *) &header_desc,
240 (struct usb_descriptor_header *) &call_mgmt_descriptor,
241 (struct usb_descriptor_header *) &rndis_acm_descriptor,
242 (struct usb_descriptor_header *) &rndis_union_desc,
243 (struct usb_descriptor_header *) &fs_notify_desc,
244 /* data interface has no altsetting */
245 (struct usb_descriptor_header *) &rndis_data_intf,
246 (struct usb_descriptor_header *) &fs_in_desc,
247 (struct usb_descriptor_header *) &fs_out_desc,
251 /* high speed support: */
253 static struct usb_endpoint_descriptor hs_notify_desc = {
254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
257 .bEndpointAddress = USB_DIR_IN,
258 .bmAttributes = USB_ENDPOINT_XFER_INT,
259 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
260 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
262 static struct usb_endpoint_descriptor hs_in_desc = {
263 .bLength = USB_DT_ENDPOINT_SIZE,
264 .bDescriptorType = USB_DT_ENDPOINT,
266 .bEndpointAddress = USB_DIR_IN,
267 .bmAttributes = USB_ENDPOINT_XFER_BULK,
268 .wMaxPacketSize = cpu_to_le16(512),
271 static struct usb_endpoint_descriptor hs_out_desc = {
272 .bLength = USB_DT_ENDPOINT_SIZE,
273 .bDescriptorType = USB_DT_ENDPOINT,
275 .bEndpointAddress = USB_DIR_OUT,
276 .bmAttributes = USB_ENDPOINT_XFER_BULK,
277 .wMaxPacketSize = cpu_to_le16(512),
280 static struct usb_descriptor_header *eth_hs_function[] = {
281 (struct usb_descriptor_header *) &rndis_iad_descriptor,
282 /* control interface matches ACM, not Ethernet */
283 (struct usb_descriptor_header *) &rndis_control_intf,
284 (struct usb_descriptor_header *) &header_desc,
285 (struct usb_descriptor_header *) &call_mgmt_descriptor,
286 (struct usb_descriptor_header *) &rndis_acm_descriptor,
287 (struct usb_descriptor_header *) &rndis_union_desc,
288 (struct usb_descriptor_header *) &hs_notify_desc,
289 /* data interface has no altsetting */
290 (struct usb_descriptor_header *) &rndis_data_intf,
291 (struct usb_descriptor_header *) &hs_in_desc,
292 (struct usb_descriptor_header *) &hs_out_desc,
296 /* string descriptors: */
298 static struct usb_string rndis_string_defs[] = {
299 [0].s = "RNDIS Communications Control",
300 [1].s = "RNDIS Ethernet Data",
302 { } /* end of list */
305 static struct usb_gadget_strings rndis_string_table = {
306 .language = 0x0409, /* en-us */
307 .strings = rndis_string_defs,
310 static struct usb_gadget_strings *rndis_strings[] = {
315 /*-------------------------------------------------------------------------*/
317 static struct sk_buff *rndis_add_header(struct gether *port,
320 struct sk_buff *skb2;
322 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
326 dev_kfree_skb_any(skb);
330 static void rndis_response_available(void *_rndis)
332 struct f_rndis *rndis = _rndis;
333 struct usb_request *req = rndis->notify_req;
334 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
335 __le32 *data = req->buf;
338 if (atomic_inc_return(&rndis->notify_count) != 1)
341 /* Send RNDIS RESPONSE_AVAILABLE notification; a
342 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
344 * This is the only notification defined by RNDIS.
346 data[0] = cpu_to_le32(1);
347 data[1] = cpu_to_le32(0);
349 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
351 atomic_dec(&rndis->notify_count);
352 DBG(cdev, "notify/0 --> %d\n", status);
356 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
358 struct f_rndis *rndis = req->context;
359 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
360 int status = req->status;
363 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
364 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
369 /* connection gone */
370 atomic_set(&rndis->notify_count, 0);
373 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
375 req->actual, req->length);
378 if (ep != rndis->notify)
381 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
382 * notifications by resending until we're done
384 if (atomic_dec_and_test(&rndis->notify_count))
386 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
388 atomic_dec(&rndis->notify_count);
389 DBG(cdev, "notify/1 --> %d\n", status);
395 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
397 struct f_rndis *rndis = req->context;
398 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
401 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
402 // spin_lock(&dev->lock);
403 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
405 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
406 status, req->actual, req->length);
407 // spin_unlock(&dev->lock);
411 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
413 struct f_rndis *rndis = func_to_rndis(f);
414 struct usb_composite_dev *cdev = f->config->cdev;
415 struct usb_request *req = cdev->req;
416 int value = -EOPNOTSUPP;
417 u16 w_index = le16_to_cpu(ctrl->wIndex);
418 u16 w_value = le16_to_cpu(ctrl->wValue);
419 u16 w_length = le16_to_cpu(ctrl->wLength);
421 /* composite driver infrastructure handles everything except
422 * CDC class messages; interface activation uses set_alt().
424 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
426 /* RNDIS uses the CDC command encapsulation mechanism to implement
427 * an RPC scheme, with much getting/setting of attributes by OID.
429 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
430 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
431 if (w_length > req->length || w_value
432 || w_index != rndis->ctrl_id)
434 /* read the request; process it later */
436 req->complete = rndis_command_complete;
437 req->context = rndis;
438 /* later, rndis_response_available() sends a notification */
441 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
442 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
443 if (w_value || w_index != rndis->ctrl_id)
449 /* return the result */
450 buf = rndis_get_next_response(rndis->config, &n);
452 memcpy(req->buf, buf, n);
453 req->complete = rndis_response_complete;
454 rndis_free_response(rndis->config, buf);
457 /* else stalls ... spec says to avoid that */
463 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
464 ctrl->bRequestType, ctrl->bRequest,
465 w_value, w_index, w_length);
468 /* respond with data transfer or status phase? */
470 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
471 ctrl->bRequestType, ctrl->bRequest,
472 w_value, w_index, w_length);
473 req->zero = (value < w_length);
475 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
477 ERROR(cdev, "rndis response on err %d\n", value);
480 /* device either stalls (value < 0) or reports success */
485 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
487 struct f_rndis *rndis = func_to_rndis(f);
488 struct usb_composite_dev *cdev = f->config->cdev;
490 /* we know alt == 0 */
492 if (intf == rndis->ctrl_id) {
493 if (rndis->notify->driver_data) {
494 VDBG(cdev, "reset rndis control %d\n", intf);
495 usb_ep_disable(rndis->notify);
497 VDBG(cdev, "init rndis ctrl %d\n", intf);
499 rndis->notify_desc = ep_choose(cdev->gadget,
502 usb_ep_enable(rndis->notify, rndis->notify_desc);
503 rndis->notify->driver_data = rndis;
505 } else if (intf == rndis->data_id) {
506 struct net_device *net;
508 if (rndis->port.in_ep->driver_data) {
509 DBG(cdev, "reset rndis\n");
510 gether_disconnect(&rndis->port);
513 if (!rndis->port.in) {
514 DBG(cdev, "init rndis\n");
516 rndis->port.in = ep_choose(cdev->gadget,
517 rndis->hs.in, rndis->fs.in);
518 rndis->port.out = ep_choose(cdev->gadget,
519 rndis->hs.out, rndis->fs.out);
521 /* Avoid ZLPs; they can be troublesome. */
522 rndis->port.is_zlp_ok = false;
524 /* RNDIS should be in the "RNDIS uninitialized" state,
525 * either never activated or after rndis_uninit().
527 * We don't want data to flow here until a nonzero packet
528 * filter is set, at which point it enters "RNDIS data
529 * initialized" state ... but we do want the endpoints
530 * to be activated. It's a strange little state.
532 * REVISIT the RNDIS gadget code has done this wrong for a
533 * very long time. We need another call to the link layer
534 * code -- gether_updown(...bool) maybe -- to do it right.
536 rndis->port.cdc_filter = 0;
538 DBG(cdev, "RNDIS RX/TX early activation ... \n");
539 net = gether_connect(&rndis->port);
543 rndis_set_param_dev(rndis->config, net,
544 &rndis->port.cdc_filter);
553 static void rndis_disable(struct usb_function *f)
555 struct f_rndis *rndis = func_to_rndis(f);
556 struct usb_composite_dev *cdev = f->config->cdev;
558 if (!rndis->notify->driver_data)
561 DBG(cdev, "rndis deactivated\n");
563 rndis_uninit(rndis->config);
564 gether_disconnect(&rndis->port);
566 usb_ep_disable(rndis->notify);
567 rndis->notify->driver_data = NULL;
570 /*-------------------------------------------------------------------------*/
573 * This isn't quite the same mechanism as CDC Ethernet, since the
574 * notification scheme passes less data, but the same set of link
575 * states must be tested. A key difference is that altsettings are
576 * not used to tell whether the link should send packets or not.
579 static void rndis_open(struct gether *geth)
581 struct f_rndis *rndis = func_to_rndis(&geth->func);
582 struct usb_composite_dev *cdev = geth->func.config->cdev;
584 DBG(cdev, "%s\n", __func__);
586 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
587 bitrate(cdev->gadget) / 100);
588 rndis_signal_connect(rndis->config);
591 static void rndis_close(struct gether *geth)
593 struct f_rndis *rndis = func_to_rndis(&geth->func);
595 DBG(geth->func.config->cdev, "%s\n", __func__);
597 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
598 rndis_signal_disconnect(rndis->config);
601 /*-------------------------------------------------------------------------*/
603 /* ethernet function driver setup/binding */
606 rndis_bind(struct usb_configuration *c, struct usb_function *f)
608 struct usb_composite_dev *cdev = c->cdev;
609 struct f_rndis *rndis = func_to_rndis(f);
613 /* allocate instance-specific interface IDs */
614 status = usb_interface_id(c, f);
617 rndis->ctrl_id = status;
618 rndis_iad_descriptor.bFirstInterface = status;
620 rndis_control_intf.bInterfaceNumber = status;
621 rndis_union_desc.bMasterInterface0 = status;
623 status = usb_interface_id(c, f);
626 rndis->data_id = status;
628 rndis_data_intf.bInterfaceNumber = status;
629 rndis_union_desc.bSlaveInterface0 = status;
633 /* allocate instance-specific endpoints */
634 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
637 rndis->port.in_ep = ep;
638 ep->driver_data = cdev; /* claim */
640 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
643 rndis->port.out_ep = ep;
644 ep->driver_data = cdev; /* claim */
646 /* NOTE: a status/notification endpoint is, strictly speaking,
647 * optional. We don't treat it that way though! It's simpler,
648 * and some newer profiles don't treat it as optional.
650 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
654 ep->driver_data = cdev; /* claim */
658 /* allocate notification request and buffer */
659 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
660 if (!rndis->notify_req)
662 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
663 if (!rndis->notify_req->buf)
665 rndis->notify_req->length = STATUS_BYTECOUNT;
666 rndis->notify_req->context = rndis;
667 rndis->notify_req->complete = rndis_response_complete;
669 /* copy descriptors, and track endpoint copies */
670 f->descriptors = usb_copy_descriptors(eth_fs_function);
674 rndis->fs.in = usb_find_endpoint(eth_fs_function,
675 f->descriptors, &fs_in_desc);
676 rndis->fs.out = usb_find_endpoint(eth_fs_function,
677 f->descriptors, &fs_out_desc);
678 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
679 f->descriptors, &fs_notify_desc);
681 /* support all relevant hardware speeds... we expect that when
682 * hardware is dual speed, all bulk-capable endpoints work at
685 if (gadget_is_dualspeed(c->cdev->gadget)) {
686 hs_in_desc.bEndpointAddress =
687 fs_in_desc.bEndpointAddress;
688 hs_out_desc.bEndpointAddress =
689 fs_out_desc.bEndpointAddress;
690 hs_notify_desc.bEndpointAddress =
691 fs_notify_desc.bEndpointAddress;
693 /* copy descriptors, and track endpoint copies */
694 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
696 if (!f->hs_descriptors)
699 rndis->hs.in = usb_find_endpoint(eth_hs_function,
700 f->hs_descriptors, &hs_in_desc);
701 rndis->hs.out = usb_find_endpoint(eth_hs_function,
702 f->hs_descriptors, &hs_out_desc);
703 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
704 f->hs_descriptors, &hs_notify_desc);
707 rndis->port.open = rndis_open;
708 rndis->port.close = rndis_close;
710 status = rndis_register(rndis_response_available, rndis);
713 rndis->config = status;
715 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
716 rndis_set_host_mac(rndis->config, rndis->ethaddr);
720 if (rndis_set_param_vendor(rndis->config, vendorID,
725 /* NOTE: all that is done without knowing or caring about
726 * the network link ... which is unavailable to this code
727 * until we're activated via set_alt().
730 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
731 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
732 rndis->port.in_ep->name, rndis->port.out_ep->name,
733 rndis->notify->name);
737 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
738 usb_free_descriptors(f->hs_descriptors);
740 usb_free_descriptors(f->descriptors);
742 if (rndis->notify_req) {
743 kfree(rndis->notify_req->buf);
744 usb_ep_free_request(rndis->notify, rndis->notify_req);
747 /* we might as well release our claims on endpoints */
749 rndis->notify->driver_data = NULL;
751 rndis->port.out_ep->driver_data = NULL;
753 rndis->port.in_ep->driver_data = NULL;
755 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
761 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
763 struct f_rndis *rndis = func_to_rndis(f);
765 rndis_deregister(rndis->config);
768 if (gadget_is_dualspeed(c->cdev->gadget))
769 usb_free_descriptors(f->hs_descriptors);
770 usb_free_descriptors(f->descriptors);
772 kfree(rndis->notify_req->buf);
773 usb_ep_free_request(rndis->notify, rndis->notify_req);
778 /* Some controllers can't support RNDIS ... */
779 static inline bool can_support_rndis(struct usb_configuration *c)
781 /* everything else is *presumably* fine */
786 * rndis_bind_config - add RNDIS network link to a configuration
787 * @c: the configuration to support the network link
788 * @ethaddr: a buffer in which the ethernet address of the host side
789 * side of the link was recorded
790 * Context: single threaded during gadget setup
792 * Returns zero on success, else negative errno.
794 * Caller must have called @gether_setup(). Caller is also responsible
795 * for calling @gether_cleanup() before module unload.
798 rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
800 struct f_rndis *rndis;
803 if (!can_support_rndis(c) || !ethaddr)
806 /* maybe allocate device-global string IDs */
807 if (rndis_string_defs[0].id == 0) {
809 /* ... and setup RNDIS itself */
810 status = rndis_init();
814 /* control interface label */
815 status = usb_string_id(c->cdev);
818 rndis_string_defs[0].id = status;
819 rndis_control_intf.iInterface = status;
821 /* data interface label */
822 status = usb_string_id(c->cdev);
825 rndis_string_defs[1].id = status;
826 rndis_data_intf.iInterface = status;
828 /* IAD iFunction label */
829 status = usb_string_id(c->cdev);
832 rndis_string_defs[2].id = status;
833 rndis_iad_descriptor.iFunction = status;
836 /* allocate and initialize one new instance */
838 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
842 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
844 /* RNDIS activates when the host changes this filter */
845 rndis->port.cdc_filter = 0;
847 /* RNDIS has special (and complex) framing */
848 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
849 rndis->port.wrap = rndis_add_header;
850 rndis->port.unwrap = rndis_rm_hdr;
852 rndis->port.func.name = "rndis";
853 rndis->port.func.strings = rndis_strings;
854 /* descriptors are per-instance copies */
855 rndis->port.func.bind = rndis_bind;
856 rndis->port.func.unbind = rndis_unbind;
857 rndis->port.func.set_alt = rndis_set_alt;
858 rndis->port.func.setup = rndis_setup;
859 rndis->port.func.disable = rndis_disable;
861 #ifdef CONFIG_USB_ANDROID_RNDIS
863 rndis->port.func.hidden = 1;
866 status = usb_add_function(c, &rndis->port.func);
875 #ifdef CONFIG_USB_ANDROID_RNDIS
878 // FIXME - using bogus MAC address for now
880 static u8 ethaddr[ETH_ALEN] = { 11, 22, 33, 44, 55, 66 };
882 int rndis_function_bind_config(struct usb_configuration *c)
884 int ret = gether_setup(c->cdev->gadget, ethaddr);
886 ret = rndis_bind_config(c, ethaddr);
890 static struct android_usb_function rndis_function = {
892 .bind_config = rndis_function_bind_config,
895 static int __init init(void)
897 printk(KERN_INFO "f_rndis init\n");
898 android_register_function(&rndis_function);
903 #endif /* CONFIG_USB_ANDROID_RNDIS */