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
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.
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.
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
23 /* #define VERBOSE_DEBUG */
25 #include <linux/kernel.h>
26 #include <linux/platform_device.h>
27 #include <linux/etherdevice.h>
28 #include <linux/usb/android_composite.h>
30 #include <asm/atomic.h>
37 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
38 * been promoted instead of the standard CDC Ethernet. The published RNDIS
39 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
40 * ActiveSync have even worse status in terms of specification.
42 * In short: it's a protocol controlled by (and for) Microsoft, not for an
43 * Open ecosystem or markets. Linux supports it *only* because Microsoft
44 * doesn't support the CDC Ethernet standard.
46 * The RNDIS data transfer model is complex, with multiple Ethernet packets
47 * per USB message, and out of band data. The control model is built around
48 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
49 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
50 * useless (they're ignored). RNDIS expects to be the only function in its
51 * configuration, so it's no real help if you need composite devices; and
52 * it expects to be the first configuration too.
54 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
55 * discount the fluff that its RPC can be made to deliver: it doesn't need
56 * a NOP altsetting for the data interface. That lets it work on some of the
57 * "so smart it's stupid" hardware which takes over configuration changes
58 * from the software, and adds restrictions like "no altsettings".
60 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
61 * have all sorts of contrary-to-specification oddities that can prevent
62 * them from working sanely. Since bugfixes (or accurate specs, letting
63 * Linux work around those bugs) are unlikely to ever come from MSFT, you
64 * may want to avoid using RNDIS on purely operational grounds.
66 * Omissions from the RNDIS 1.0 specification include:
68 * - Power management ... references data that's scattered around lots
69 * of other documentation, which is incorrect/incomplete there too.
71 * - There are various undocumented protocol requirements, like the need
72 * to send garbage in some control-OUT messages.
74 * - MS-Windows drivers sometimes emit undocumented requests.
77 struct rndis_ep_descs {
78 struct usb_endpoint_descriptor *in;
79 struct usb_endpoint_descriptor *out;
80 struct usb_endpoint_descriptor *notify;
89 struct rndis_ep_descs fs;
90 struct rndis_ep_descs hs;
92 struct usb_ep *notify;
93 struct usb_endpoint_descriptor *notify_desc;
94 struct usb_request *notify_req;
95 atomic_t notify_count;
98 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
100 return container_of(f, struct f_rndis, port.func);
103 /* peak (theoretical) bulk transfer rate in bits-per-second */
104 static unsigned int bitrate(struct usb_gadget *g)
106 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
107 return 13 * 512 * 8 * 1000 * 8;
109 return 19 * 64 * 1 * 1000 * 8;
112 /*-------------------------------------------------------------------------*/
117 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
118 #define STATUS_BYTECOUNT 8 /* 8 bytes data */
121 /* interface descriptor: */
123 static struct usb_interface_descriptor rndis_control_intf __initdata = {
124 .bLength = sizeof rndis_control_intf,
125 .bDescriptorType = USB_DT_INTERFACE,
127 /* .bInterfaceNumber = DYNAMIC */
128 /* status endpoint is optional; this could be patched later */
130 #ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
131 /* "Wireless" RNDIS; auto-detected by Windows */
132 .bInterfaceClass = USB_CLASS_WIRELESS_CONTROLLER,
133 .bInterfaceSubClass = 1,
134 .bInterfaceProtocol = 3,
136 .bInterfaceClass = USB_CLASS_COMM,
137 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
138 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
140 /* .iInterface = DYNAMIC */
143 static struct usb_cdc_header_desc header_desc __initdata = {
144 .bLength = sizeof header_desc,
145 .bDescriptorType = USB_DT_CS_INTERFACE,
146 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
148 .bcdCDC = cpu_to_le16(0x0110),
151 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
152 .bLength = sizeof call_mgmt_descriptor,
153 .bDescriptorType = USB_DT_CS_INTERFACE,
154 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
156 .bmCapabilities = 0x00,
157 .bDataInterface = 0x01,
160 static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
161 .bLength = sizeof acm_descriptor,
162 .bDescriptorType = USB_DT_CS_INTERFACE,
163 .bDescriptorSubType = USB_CDC_ACM_TYPE,
165 .bmCapabilities = 0x00,
168 static struct usb_cdc_union_desc rndis_union_desc __initdata = {
169 .bLength = sizeof(rndis_union_desc),
170 .bDescriptorType = USB_DT_CS_INTERFACE,
171 .bDescriptorSubType = USB_CDC_UNION_TYPE,
172 /* .bMasterInterface0 = DYNAMIC */
173 /* .bSlaveInterface0 = DYNAMIC */
176 /* the data interface has two bulk endpoints */
178 static struct usb_interface_descriptor rndis_data_intf __initdata = {
179 .bLength = sizeof rndis_data_intf,
180 .bDescriptorType = USB_DT_INTERFACE,
182 /* .bInterfaceNumber = DYNAMIC */
184 .bInterfaceClass = USB_CLASS_CDC_DATA,
185 .bInterfaceSubClass = 0,
186 .bInterfaceProtocol = 0,
187 /* .iInterface = DYNAMIC */
190 /* full speed support: */
192 static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
193 .bLength = USB_DT_ENDPOINT_SIZE,
194 .bDescriptorType = USB_DT_ENDPOINT,
196 .bEndpointAddress = USB_DIR_IN,
197 .bmAttributes = USB_ENDPOINT_XFER_INT,
198 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
199 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
202 static struct usb_endpoint_descriptor fs_in_desc __initdata = {
203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
210 static struct usb_endpoint_descriptor fs_out_desc __initdata = {
211 .bLength = USB_DT_ENDPOINT_SIZE,
212 .bDescriptorType = USB_DT_ENDPOINT,
214 .bEndpointAddress = USB_DIR_OUT,
215 .bmAttributes = USB_ENDPOINT_XFER_BULK,
218 static struct usb_descriptor_header *eth_fs_function[] __initdata = {
219 /* 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,
223 (struct usb_descriptor_header *) &acm_descriptor,
224 (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,
233 /* high speed support: */
235 static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
236 .bLength = USB_DT_ENDPOINT_SIZE,
237 .bDescriptorType = USB_DT_ENDPOINT,
239 .bEndpointAddress = USB_DIR_IN,
240 .bmAttributes = USB_ENDPOINT_XFER_INT,
241 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
242 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
244 static struct usb_endpoint_descriptor hs_in_desc __initdata = {
245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
248 .bEndpointAddress = USB_DIR_IN,
249 .bmAttributes = USB_ENDPOINT_XFER_BULK,
250 .wMaxPacketSize = cpu_to_le16(512),
253 static struct usb_endpoint_descriptor hs_out_desc __initdata = {
254 .bLength = USB_DT_ENDPOINT_SIZE,
255 .bDescriptorType = USB_DT_ENDPOINT,
257 .bEndpointAddress = USB_DIR_OUT,
258 .bmAttributes = USB_ENDPOINT_XFER_BULK,
259 .wMaxPacketSize = cpu_to_le16(512),
262 static struct usb_descriptor_header *eth_hs_function[] __initdata = {
263 /* control interface matches ACM, not Ethernet */
264 (struct usb_descriptor_header *) &rndis_control_intf,
265 (struct usb_descriptor_header *) &header_desc,
266 (struct usb_descriptor_header *) &call_mgmt_descriptor,
267 (struct usb_descriptor_header *) &acm_descriptor,
268 (struct usb_descriptor_header *) &rndis_union_desc,
269 (struct usb_descriptor_header *) &hs_notify_desc,
270 /* data interface has no altsetting */
271 (struct usb_descriptor_header *) &rndis_data_intf,
272 (struct usb_descriptor_header *) &hs_in_desc,
273 (struct usb_descriptor_header *) &hs_out_desc,
277 /* string descriptors: */
279 static struct usb_string rndis_string_defs[] = {
280 [0].s = "RNDIS Communications Control",
281 [1].s = "RNDIS Ethernet Data",
282 { } /* end of list */
285 static struct usb_gadget_strings rndis_string_table = {
286 .language = 0x0409, /* en-us */
287 .strings = rndis_string_defs,
290 static struct usb_gadget_strings *rndis_strings[] = {
295 #ifdef CONFIG_USB_ANDROID_RNDIS
296 static struct usb_ether_platform_data *rndis_pdata;
299 /*-------------------------------------------------------------------------*/
301 static struct sk_buff *rndis_add_header(struct gether *port,
304 struct sk_buff *skb2;
306 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
310 dev_kfree_skb_any(skb);
314 static void rndis_response_available(void *_rndis)
316 struct f_rndis *rndis = _rndis;
317 struct usb_request *req = rndis->notify_req;
318 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
319 __le32 *data = req->buf;
322 if (atomic_inc_return(&rndis->notify_count) != 1)
325 /* Send RNDIS RESPONSE_AVAILABLE notification; a
326 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
328 * This is the only notification defined by RNDIS.
330 data[0] = cpu_to_le32(1);
331 data[1] = cpu_to_le32(0);
333 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
335 atomic_dec(&rndis->notify_count);
336 DBG(cdev, "notify/0 --> %d\n", status);
340 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
342 struct f_rndis *rndis = req->context;
343 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
344 int status = req->status;
347 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
348 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
353 /* connection gone */
354 atomic_set(&rndis->notify_count, 0);
357 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
359 req->actual, req->length);
362 if (ep != rndis->notify)
365 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
366 * notifications by resending until we're done
368 if (atomic_dec_and_test(&rndis->notify_count))
370 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
372 atomic_dec(&rndis->notify_count);
373 DBG(cdev, "notify/1 --> %d\n", status);
379 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
381 struct f_rndis *rndis = req->context;
382 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
385 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
386 // spin_lock(&dev->lock);
387 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
389 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
390 status, req->actual, req->length);
391 // spin_unlock(&dev->lock);
395 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
397 struct f_rndis *rndis = func_to_rndis(f);
398 struct usb_composite_dev *cdev = f->config->cdev;
399 struct usb_request *req = cdev->req;
400 int value = -EOPNOTSUPP;
401 u16 w_index = le16_to_cpu(ctrl->wIndex);
402 u16 w_value = le16_to_cpu(ctrl->wValue);
403 u16 w_length = le16_to_cpu(ctrl->wLength);
405 /* composite driver infrastructure handles everything except
406 * CDC class messages; interface activation uses set_alt().
408 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
410 /* RNDIS uses the CDC command encapsulation mechanism to implement
411 * an RPC scheme, with much getting/setting of attributes by OID.
413 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
414 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
415 if (w_value || w_index != rndis->ctrl_id)
417 /* read the request; process it later */
419 req->complete = rndis_command_complete;
420 req->context = rndis;
421 /* later, rndis_response_available() sends a notification */
424 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
425 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
426 if (w_value || w_index != rndis->ctrl_id)
432 /* return the result */
433 buf = rndis_get_next_response(rndis->config, &n);
435 memcpy(req->buf, buf, n);
436 req->complete = rndis_response_complete;
437 rndis_free_response(rndis->config, buf);
440 /* else stalls ... spec says to avoid that */
446 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
447 ctrl->bRequestType, ctrl->bRequest,
448 w_value, w_index, w_length);
451 /* respond with data transfer or status phase? */
453 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
454 ctrl->bRequestType, ctrl->bRequest,
455 w_value, w_index, w_length);
456 req->zero = (value < w_length);
458 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
460 ERROR(cdev, "rndis response on err %d\n", value);
463 /* device either stalls (value < 0) or reports success */
468 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
470 struct f_rndis *rndis = func_to_rndis(f);
471 struct usb_composite_dev *cdev = f->config->cdev;
473 /* we know alt == 0 */
475 if (intf == rndis->ctrl_id) {
476 if (rndis->notify->driver_data) {
477 VDBG(cdev, "reset rndis control %d\n", intf);
478 usb_ep_disable(rndis->notify);
480 VDBG(cdev, "init rndis ctrl %d\n", intf);
482 rndis->notify_desc = ep_choose(cdev->gadget,
485 usb_ep_enable(rndis->notify, rndis->notify_desc);
486 rndis->notify->driver_data = rndis;
488 } else if (intf == rndis->data_id) {
489 struct net_device *net;
491 if (rndis->port.in_ep->driver_data) {
492 DBG(cdev, "reset rndis\n");
493 gether_disconnect(&rndis->port);
496 if (!rndis->port.in) {
497 DBG(cdev, "init rndis\n");
499 rndis->port.in = ep_choose(cdev->gadget,
500 rndis->hs.in, rndis->fs.in);
501 rndis->port.out = ep_choose(cdev->gadget,
502 rndis->hs.out, rndis->fs.out);
504 /* Avoid ZLPs; they can be troublesome. */
505 rndis->port.is_zlp_ok = false;
507 /* RNDIS should be in the "RNDIS uninitialized" state,
508 * either never activated or after rndis_uninit().
510 * We don't want data to flow here until a nonzero packet
511 * filter is set, at which point it enters "RNDIS data
512 * initialized" state ... but we do want the endpoints
513 * to be activated. It's a strange little state.
515 * REVISIT the RNDIS gadget code has done this wrong for a
516 * very long time. We need another call to the link layer
517 * code -- gether_updown(...bool) maybe -- to do it right.
519 rndis->port.cdc_filter = 0;
521 DBG(cdev, "RNDIS RX/TX early activation ... \n");
522 net = gether_connect(&rndis->port);
526 rndis_set_param_dev(rndis->config, net,
527 &rndis->port.cdc_filter);
536 static void rndis_disable(struct usb_function *f)
538 struct f_rndis *rndis = func_to_rndis(f);
539 struct usb_composite_dev *cdev = f->config->cdev;
541 if (!rndis->notify->driver_data)
544 DBG(cdev, "rndis deactivated\n");
546 rndis_uninit(rndis->config);
547 gether_disconnect(&rndis->port);
549 usb_ep_disable(rndis->notify);
550 rndis->notify->driver_data = NULL;
553 /*-------------------------------------------------------------------------*/
556 * This isn't quite the same mechanism as CDC Ethernet, since the
557 * notification scheme passes less data, but the same set of link
558 * states must be tested. A key difference is that altsettings are
559 * not used to tell whether the link should send packets or not.
562 static void rndis_open(struct gether *geth)
564 struct f_rndis *rndis = func_to_rndis(&geth->func);
565 struct usb_composite_dev *cdev = geth->func.config->cdev;
567 DBG(cdev, "%s\n", __func__);
569 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
570 bitrate(cdev->gadget) / 100);
571 rndis_signal_connect(rndis->config);
574 static void rndis_close(struct gether *geth)
576 struct f_rndis *rndis = func_to_rndis(&geth->func);
578 DBG(geth->func.config->cdev, "%s\n", __func__);
580 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
581 rndis_signal_disconnect(rndis->config);
584 /*-------------------------------------------------------------------------*/
586 /* ethernet function driver setup/binding */
589 rndis_bind(struct usb_configuration *c, struct usb_function *f)
591 struct usb_composite_dev *cdev = c->cdev;
592 struct f_rndis *rndis = func_to_rndis(f);
596 /* allocate instance-specific interface IDs */
597 status = usb_interface_id(c, f);
600 rndis->ctrl_id = status;
602 rndis_control_intf.bInterfaceNumber = status;
603 rndis_union_desc.bMasterInterface0 = status;
605 status = usb_interface_id(c, f);
608 rndis->data_id = status;
610 rndis_data_intf.bInterfaceNumber = status;
611 rndis_union_desc.bSlaveInterface0 = status;
615 /* allocate instance-specific endpoints */
616 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
619 rndis->port.in_ep = ep;
620 ep->driver_data = cdev; /* claim */
622 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
625 rndis->port.out_ep = ep;
626 ep->driver_data = cdev; /* claim */
628 /* NOTE: a status/notification endpoint is, strictly speaking,
629 * optional. We don't treat it that way though! It's simpler,
630 * and some newer profiles don't treat it as optional.
632 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
636 ep->driver_data = cdev; /* claim */
640 /* allocate notification request and buffer */
641 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
642 if (!rndis->notify_req)
644 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
645 if (!rndis->notify_req->buf)
647 rndis->notify_req->length = STATUS_BYTECOUNT;
648 rndis->notify_req->context = rndis;
649 rndis->notify_req->complete = rndis_response_complete;
651 /* copy descriptors, and track endpoint copies */
652 f->descriptors = usb_copy_descriptors(eth_fs_function);
656 rndis->fs.in = usb_find_endpoint(eth_fs_function,
657 f->descriptors, &fs_in_desc);
658 rndis->fs.out = usb_find_endpoint(eth_fs_function,
659 f->descriptors, &fs_out_desc);
660 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
661 f->descriptors, &fs_notify_desc);
663 /* support all relevant hardware speeds... we expect that when
664 * hardware is dual speed, all bulk-capable endpoints work at
667 if (gadget_is_dualspeed(c->cdev->gadget)) {
668 hs_in_desc.bEndpointAddress =
669 fs_in_desc.bEndpointAddress;
670 hs_out_desc.bEndpointAddress =
671 fs_out_desc.bEndpointAddress;
672 hs_notify_desc.bEndpointAddress =
673 fs_notify_desc.bEndpointAddress;
675 /* copy descriptors, and track endpoint copies */
676 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
678 if (!f->hs_descriptors)
681 rndis->hs.in = usb_find_endpoint(eth_hs_function,
682 f->hs_descriptors, &hs_in_desc);
683 rndis->hs.out = usb_find_endpoint(eth_hs_function,
684 f->hs_descriptors, &hs_out_desc);
685 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
686 f->hs_descriptors, &hs_notify_desc);
689 rndis->port.open = rndis_open;
690 rndis->port.close = rndis_close;
692 status = rndis_register(rndis_response_available, rndis);
695 rndis->config = status;
697 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
698 rndis_set_host_mac(rndis->config, rndis->ethaddr);
700 #ifdef CONFIG_USB_ANDROID_RNDIS
702 if (rndis_set_param_vendor(rndis->config, rndis_pdata->vendorID,
703 rndis_pdata->vendorDescr))
708 /* NOTE: all that is done without knowing or caring about
709 * the network link ... which is unavailable to this code
710 * until we're activated via set_alt().
713 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
714 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
715 rndis->port.in_ep->name, rndis->port.out_ep->name,
716 rndis->notify->name);
720 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
721 usb_free_descriptors(f->hs_descriptors);
723 usb_free_descriptors(f->descriptors);
725 if (rndis->notify_req) {
726 kfree(rndis->notify_req->buf);
727 usb_ep_free_request(rndis->notify, rndis->notify_req);
730 /* we might as well release our claims on endpoints */
732 rndis->notify->driver_data = NULL;
734 rndis->port.out_ep->driver_data = NULL;
736 rndis->port.in_ep->driver_data = NULL;
738 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
744 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
746 struct f_rndis *rndis = func_to_rndis(f);
748 rndis_deregister(rndis->config);
751 if (gadget_is_dualspeed(c->cdev->gadget))
752 usb_free_descriptors(f->hs_descriptors);
753 usb_free_descriptors(f->descriptors);
755 kfree(rndis->notify_req->buf);
756 usb_ep_free_request(rndis->notify, rndis->notify_req);
761 /* Some controllers can't support RNDIS ... */
762 static inline bool can_support_rndis(struct usb_configuration *c)
764 /* only two endpoints on sa1100 */
765 if (gadget_is_sa1100(c->cdev->gadget))
768 /* everything else is *presumably* fine */
773 * rndis_bind_config - add RNDIS network link to a configuration
774 * @c: the configuration to support the network link
775 * @ethaddr: a buffer in which the ethernet address of the host side
776 * side of the link was recorded
777 * Context: single threaded during gadget setup
779 * Returns zero on success, else negative errno.
781 * Caller must have called @gether_setup(). Caller is also responsible
782 * for calling @gether_cleanup() before module unload.
784 int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
786 struct f_rndis *rndis;
789 if (!can_support_rndis(c) || !ethaddr)
792 /* maybe allocate device-global string IDs */
793 if (rndis_string_defs[0].id == 0) {
795 /* ... and setup RNDIS itself */
796 status = rndis_init();
800 /* control interface label */
801 status = usb_string_id(c->cdev);
804 rndis_string_defs[0].id = status;
805 rndis_control_intf.iInterface = status;
807 /* data interface label */
808 status = usb_string_id(c->cdev);
811 rndis_string_defs[1].id = status;
812 rndis_data_intf.iInterface = status;
815 /* allocate and initialize one new instance */
817 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
821 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
823 /* RNDIS activates when the host changes this filter */
824 rndis->port.cdc_filter = 0;
826 /* RNDIS has special (and complex) framing */
827 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
828 rndis->port.wrap = rndis_add_header;
829 rndis->port.unwrap = rndis_rm_hdr;
831 rndis->port.func.name = "rndis";
832 rndis->port.func.strings = rndis_strings;
833 /* descriptors are per-instance copies */
834 rndis->port.func.bind = rndis_bind;
835 rndis->port.func.unbind = rndis_unbind;
836 rndis->port.func.set_alt = rndis_set_alt;
837 rndis->port.func.setup = rndis_setup;
838 rndis->port.func.disable = rndis_disable;
840 #ifdef CONFIG_USB_ANDROID_RNDIS
842 rndis->port.func.disabled = 1;
845 status = usb_add_function(c, &rndis->port.func);
854 #ifdef CONFIG_USB_ANDROID_RNDIS
857 static int __init rndis_probe(struct platform_device *pdev)
859 rndis_pdata = pdev->dev.platform_data;
863 static struct platform_driver rndis_platform_driver = {
864 .driver = { .name = "rndis", },
865 .probe = rndis_probe,
868 int rndis_function_bind_config(struct usb_configuration *c)
873 printk(KERN_ERR "rndis_pdata null in rndis_function_bind_config\n");
878 "rndis_function_bind_config MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
879 rndis_pdata->ethaddr[0], rndis_pdata->ethaddr[1],
880 rndis_pdata->ethaddr[2], rndis_pdata->ethaddr[3],
881 rndis_pdata->ethaddr[4], rndis_pdata->ethaddr[5]);
883 ret = gether_setup(c->cdev->gadget, rndis_pdata->ethaddr);
885 ret = rndis_bind_config(c, rndis_pdata->ethaddr);
889 static struct android_usb_function rndis_function = {
891 .bind_config = rndis_function_bind_config,
894 static int __init init(void)
896 printk(KERN_INFO "f_rndis init\n");
897 platform_driver_register(&rndis_platform_driver);
898 android_register_function(&rndis_function);
903 #endif /* CONFIG_USB_ANDROID_RNDIS */