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 (mina86@mina86.com)
9 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 /* #define VERBOSE_DEBUG */
19 #include <linux/slab.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/etherdevice.h>
23 #include <linux/moduleparam.h>
25 #include <linux/atomic.h>
30 static bool rndis_multipacket_dl_disable;
31 module_param(rndis_multipacket_dl_disable, bool, S_IRUGO|S_IWUSR);
32 MODULE_PARM_DESC(rndis_multipacket_dl_disable,
33 "Disable RNDIS Multi-packet support in DownLink");
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.
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.
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.
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".
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.
65 * Omissions from the RNDIS 1.0 specification include:
67 * - Power management ... references data that's scattered around lots
68 * of other documentation, which is incorrect/incomplete there too.
70 * - There are various undocumented protocol requirements, like the need
71 * to send garbage in some control-OUT messages.
73 * - MS-Windows drivers sometimes emit undocumented requests.
81 const char *manufacturer;
84 struct usb_ep *notify;
85 struct usb_request *notify_req;
86 atomic_t notify_count;
89 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
91 return container_of(f, struct f_rndis, port.func);
94 /* peak (theoretical) bulk transfer rate in bits-per-second */
95 static unsigned int bitrate(struct usb_gadget *g)
97 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
98 return 13 * 1024 * 8 * 1000 * 8;
99 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
100 return 13 * 512 * 8 * 1000 * 8;
102 return 19 * 64 * 1 * 1000 * 8;
105 /*-------------------------------------------------------------------------*/
110 #define RNDIS_STATUS_INTERVAL_MS 32
111 #define STATUS_BYTECOUNT 8 /* 8 bytes data */
114 /* interface descriptor: */
116 static struct usb_interface_descriptor rndis_control_intf = {
117 .bLength = sizeof rndis_control_intf,
118 .bDescriptorType = USB_DT_INTERFACE,
120 /* .bInterfaceNumber = DYNAMIC */
121 /* status endpoint is optional; this could be patched later */
123 .bInterfaceClass = USB_CLASS_COMM,
124 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
125 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
126 /* .iInterface = DYNAMIC */
129 static struct usb_cdc_header_desc header_desc = {
130 .bLength = sizeof header_desc,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
134 .bcdCDC = cpu_to_le16(0x0110),
137 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
138 .bLength = sizeof call_mgmt_descriptor,
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
142 .bmCapabilities = 0x00,
143 .bDataInterface = 0x01,
146 static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
147 .bLength = sizeof rndis_acm_descriptor,
148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_ACM_TYPE,
151 .bmCapabilities = 0x00,
154 static struct usb_cdc_union_desc rndis_union_desc = {
155 .bLength = sizeof(rndis_union_desc),
156 .bDescriptorType = USB_DT_CS_INTERFACE,
157 .bDescriptorSubType = USB_CDC_UNION_TYPE,
158 /* .bMasterInterface0 = DYNAMIC */
159 /* .bSlaveInterface0 = DYNAMIC */
162 /* the data interface has two bulk endpoints */
164 static struct usb_interface_descriptor rndis_data_intf = {
165 .bLength = sizeof rndis_data_intf,
166 .bDescriptorType = USB_DT_INTERFACE,
168 /* .bInterfaceNumber = DYNAMIC */
170 .bInterfaceClass = USB_CLASS_CDC_DATA,
171 .bInterfaceSubClass = 0,
172 .bInterfaceProtocol = 0,
173 /* .iInterface = DYNAMIC */
177 static struct usb_interface_assoc_descriptor
178 rndis_iad_descriptor = {
179 .bLength = sizeof rndis_iad_descriptor,
180 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
182 .bFirstInterface = 0, /* XXX, hardcoded */
183 .bInterfaceCount = 2, // control + data
184 .bFunctionClass = USB_CLASS_COMM,
185 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
186 .bFunctionProtocol = USB_CDC_PROTO_NONE,
187 /* .iFunction = DYNAMIC */
190 /* full speed support: */
192 static struct usb_endpoint_descriptor fs_notify_desc = {
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 = RNDIS_STATUS_INTERVAL_MS,
202 static struct usb_endpoint_descriptor fs_in_desc = {
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 = {
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[] = {
219 (struct usb_descriptor_header *) &rndis_iad_descriptor,
221 /* control interface matches ACM, not Ethernet */
222 (struct usb_descriptor_header *) &rndis_control_intf,
223 (struct usb_descriptor_header *) &header_desc,
224 (struct usb_descriptor_header *) &call_mgmt_descriptor,
225 (struct usb_descriptor_header *) &rndis_acm_descriptor,
226 (struct usb_descriptor_header *) &rndis_union_desc,
227 (struct usb_descriptor_header *) &fs_notify_desc,
229 /* data interface has no altsetting */
230 (struct usb_descriptor_header *) &rndis_data_intf,
231 (struct usb_descriptor_header *) &fs_in_desc,
232 (struct usb_descriptor_header *) &fs_out_desc,
236 /* high speed support: */
238 static struct usb_endpoint_descriptor hs_notify_desc = {
239 .bLength = USB_DT_ENDPOINT_SIZE,
240 .bDescriptorType = USB_DT_ENDPOINT,
242 .bEndpointAddress = USB_DIR_IN,
243 .bmAttributes = USB_ENDPOINT_XFER_INT,
244 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
245 .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
248 static struct usb_endpoint_descriptor hs_in_desc = {
249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
252 .bEndpointAddress = USB_DIR_IN,
253 .bmAttributes = USB_ENDPOINT_XFER_BULK,
254 .wMaxPacketSize = cpu_to_le16(512),
257 static struct usb_endpoint_descriptor hs_out_desc = {
258 .bLength = USB_DT_ENDPOINT_SIZE,
259 .bDescriptorType = USB_DT_ENDPOINT,
261 .bEndpointAddress = USB_DIR_OUT,
262 .bmAttributes = USB_ENDPOINT_XFER_BULK,
263 .wMaxPacketSize = cpu_to_le16(512),
266 static struct usb_descriptor_header *eth_hs_function[] = {
267 (struct usb_descriptor_header *) &rndis_iad_descriptor,
269 /* control interface matches ACM, not Ethernet */
270 (struct usb_descriptor_header *) &rndis_control_intf,
271 (struct usb_descriptor_header *) &header_desc,
272 (struct usb_descriptor_header *) &call_mgmt_descriptor,
273 (struct usb_descriptor_header *) &rndis_acm_descriptor,
274 (struct usb_descriptor_header *) &rndis_union_desc,
275 (struct usb_descriptor_header *) &hs_notify_desc,
277 /* data interface has no altsetting */
278 (struct usb_descriptor_header *) &rndis_data_intf,
279 (struct usb_descriptor_header *) &hs_in_desc,
280 (struct usb_descriptor_header *) &hs_out_desc,
284 /* super speed support: */
286 static struct usb_endpoint_descriptor ss_notify_desc = {
287 .bLength = USB_DT_ENDPOINT_SIZE,
288 .bDescriptorType = USB_DT_ENDPOINT,
290 .bEndpointAddress = USB_DIR_IN,
291 .bmAttributes = USB_ENDPOINT_XFER_INT,
292 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
293 .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
296 static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
297 .bLength = sizeof ss_intr_comp_desc,
298 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
300 /* the following 3 values can be tweaked if necessary */
301 /* .bMaxBurst = 0, */
302 /* .bmAttributes = 0, */
303 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
306 static struct usb_endpoint_descriptor ss_in_desc = {
307 .bLength = USB_DT_ENDPOINT_SIZE,
308 .bDescriptorType = USB_DT_ENDPOINT,
310 .bEndpointAddress = USB_DIR_IN,
311 .bmAttributes = USB_ENDPOINT_XFER_BULK,
312 .wMaxPacketSize = cpu_to_le16(1024),
315 static struct usb_endpoint_descriptor ss_out_desc = {
316 .bLength = USB_DT_ENDPOINT_SIZE,
317 .bDescriptorType = USB_DT_ENDPOINT,
319 .bEndpointAddress = USB_DIR_OUT,
320 .bmAttributes = USB_ENDPOINT_XFER_BULK,
321 .wMaxPacketSize = cpu_to_le16(1024),
324 static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
325 .bLength = sizeof ss_bulk_comp_desc,
326 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
328 /* the following 2 values can be tweaked if necessary */
329 /* .bMaxBurst = 0, */
330 /* .bmAttributes = 0, */
333 static struct usb_descriptor_header *eth_ss_function[] = {
334 (struct usb_descriptor_header *) &rndis_iad_descriptor,
336 /* control interface matches ACM, not Ethernet */
337 (struct usb_descriptor_header *) &rndis_control_intf,
338 (struct usb_descriptor_header *) &header_desc,
339 (struct usb_descriptor_header *) &call_mgmt_descriptor,
340 (struct usb_descriptor_header *) &rndis_acm_descriptor,
341 (struct usb_descriptor_header *) &rndis_union_desc,
342 (struct usb_descriptor_header *) &ss_notify_desc,
343 (struct usb_descriptor_header *) &ss_intr_comp_desc,
345 /* data interface has no altsetting */
346 (struct usb_descriptor_header *) &rndis_data_intf,
347 (struct usb_descriptor_header *) &ss_in_desc,
348 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
349 (struct usb_descriptor_header *) &ss_out_desc,
350 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
354 /* string descriptors: */
356 static struct usb_string rndis_string_defs[] = {
357 [0].s = "RNDIS Communications Control",
358 [1].s = "RNDIS Ethernet Data",
360 { } /* end of list */
363 static struct usb_gadget_strings rndis_string_table = {
364 .language = 0x0409, /* en-us */
365 .strings = rndis_string_defs,
368 static struct usb_gadget_strings *rndis_strings[] = {
373 /*-------------------------------------------------------------------------*/
375 static struct sk_buff *rndis_add_header(struct gether *port,
378 struct sk_buff *skb2;
380 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
384 dev_kfree_skb_any(skb);
388 static void rndis_response_available(void *_rndis)
390 struct f_rndis *rndis = _rndis;
391 struct usb_request *req = rndis->notify_req;
392 __le32 *data = req->buf;
395 if (atomic_inc_return(&rndis->notify_count) != 1)
398 /* Send RNDIS RESPONSE_AVAILABLE notification; a
399 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
401 * This is the only notification defined by RNDIS.
403 data[0] = cpu_to_le32(1);
404 data[1] = cpu_to_le32(0);
406 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
408 atomic_dec(&rndis->notify_count);
409 pr_err("notify/0 --> %d\n", status);
413 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
415 struct f_rndis *rndis = req->context;
416 int status = req->status;
419 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
420 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
425 /* connection gone */
426 atomic_set(&rndis->notify_count, 0);
429 pr_err("RNDIS %s response error %d, %d/%d\n",
431 req->actual, req->length);
434 if (ep != rndis->notify)
437 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
438 * notifications by resending until we're done
440 if (atomic_dec_and_test(&rndis->notify_count))
442 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
444 atomic_dec(&rndis->notify_count);
445 pr_err("notify/1 --> %d\n", status);
451 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
453 struct f_rndis *rndis = req->context;
455 rndis_init_msg_type *buf;
457 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
458 // spin_lock(&dev->lock);
459 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
461 pr_err("RNDIS command error %d, %d/%d\n",
462 status, req->actual, req->length);
464 buf = (rndis_init_msg_type *)req->buf;
466 if (buf->MessageType == RNDIS_MSG_INIT) {
467 if (buf->MaxTransferSize > 2048)
468 rndis->port.multi_pkt_xfer = 1;
470 rndis->port.multi_pkt_xfer = 0;
471 pr_info("%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
472 __func__, buf->MaxTransferSize,
473 rndis->port.multi_pkt_xfer ? "enabled" :
475 if (rndis_multipacket_dl_disable)
476 rndis->port.multi_pkt_xfer = 0;
478 // spin_unlock(&dev->lock);
482 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
484 struct f_rndis *rndis = func_to_rndis(f);
485 struct usb_composite_dev *cdev = f->config->cdev;
486 struct usb_request *req = cdev->req;
487 int value = -EOPNOTSUPP;
488 u16 w_index = le16_to_cpu(ctrl->wIndex);
489 u16 w_value = le16_to_cpu(ctrl->wValue);
490 u16 w_length = le16_to_cpu(ctrl->wLength);
492 /* composite driver infrastructure handles everything except
493 * CDC class messages; interface activation uses set_alt().
495 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
497 /* RNDIS uses the CDC command encapsulation mechanism to implement
498 * an RPC scheme, with much getting/setting of attributes by OID.
500 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
501 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
502 if (w_value || w_index != rndis->ctrl_id)
504 /* read the request; process it later */
506 req->complete = rndis_command_complete;
507 req->context = rndis;
508 /* later, rndis_response_available() sends a notification */
511 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
512 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
513 if (w_value || w_index != rndis->ctrl_id)
519 /* return the result */
520 buf = rndis_get_next_response(rndis->config, &n);
522 memcpy(req->buf, buf, n);
523 req->complete = rndis_response_complete;
524 req->context = rndis;
525 rndis_free_response(rndis->config, buf);
528 /* else stalls ... spec says to avoid that */
534 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
535 ctrl->bRequestType, ctrl->bRequest,
536 w_value, w_index, w_length);
539 /* respond with data transfer or status phase? */
541 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
542 ctrl->bRequestType, ctrl->bRequest,
543 w_value, w_index, w_length);
544 req->zero = (value < w_length);
546 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
548 ERROR(cdev, "rndis response on err %d\n", value);
551 /* device either stalls (value < 0) or reports success */
556 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
558 struct f_rndis *rndis = func_to_rndis(f);
559 struct usb_composite_dev *cdev = f->config->cdev;
561 /* we know alt == 0 */
563 if (intf == rndis->ctrl_id) {
564 if (rndis->notify->driver_data) {
565 VDBG(cdev, "reset rndis control %d\n", intf);
566 usb_ep_disable(rndis->notify);
568 if (!rndis->notify->desc) {
569 VDBG(cdev, "init rndis ctrl %d\n", intf);
570 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
573 usb_ep_enable(rndis->notify);
574 rndis->notify->driver_data = rndis;
576 } else if (intf == rndis->data_id) {
577 struct net_device *net;
579 if (rndis->port.in_ep->driver_data) {
580 DBG(cdev, "reset rndis\n");
581 gether_disconnect(&rndis->port);
584 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
585 DBG(cdev, "init rndis\n");
586 if (config_ep_by_speed(cdev->gadget, f,
587 rndis->port.in_ep) ||
588 config_ep_by_speed(cdev->gadget, f,
589 rndis->port.out_ep)) {
590 rndis->port.in_ep->desc = NULL;
591 rndis->port.out_ep->desc = NULL;
596 /* Avoid ZLPs; they can be troublesome. */
597 rndis->port.is_zlp_ok = false;
599 /* RNDIS should be in the "RNDIS uninitialized" state,
600 * either never activated or after rndis_uninit().
602 * We don't want data to flow here until a nonzero packet
603 * filter is set, at which point it enters "RNDIS data
604 * initialized" state ... but we do want the endpoints
605 * to be activated. It's a strange little state.
607 * REVISIT the RNDIS gadget code has done this wrong for a
608 * very long time. We need another call to the link layer
609 * code -- gether_updown(...bool) maybe -- to do it right.
611 rndis->port.cdc_filter = 0;
613 DBG(cdev, "RNDIS RX/TX early activation ... \n");
614 net = gether_connect(&rndis->port);
618 rndis_set_param_dev(rndis->config, net,
619 &rndis->port.cdc_filter);
628 static void rndis_disable(struct usb_function *f)
630 struct f_rndis *rndis = func_to_rndis(f);
631 struct usb_composite_dev *cdev = f->config->cdev;
633 if (!rndis->notify->driver_data)
636 DBG(cdev, "rndis deactivated\n");
638 rndis_uninit(rndis->config);
639 gether_disconnect(&rndis->port);
641 usb_ep_disable(rndis->notify);
642 rndis->notify->driver_data = NULL;
645 /*-------------------------------------------------------------------------*/
648 * This isn't quite the same mechanism as CDC Ethernet, since the
649 * notification scheme passes less data, but the same set of link
650 * states must be tested. A key difference is that altsettings are
651 * not used to tell whether the link should send packets or not.
654 static void rndis_open(struct gether *geth)
656 struct f_rndis *rndis = func_to_rndis(&geth->func);
657 struct usb_composite_dev *cdev = geth->func.config->cdev;
659 DBG(cdev, "%s\n", __func__);
661 rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3,
662 bitrate(cdev->gadget) / 100);
663 rndis_signal_connect(rndis->config);
666 static void rndis_close(struct gether *geth)
668 struct f_rndis *rndis = func_to_rndis(&geth->func);
670 DBG(geth->func.config->cdev, "%s\n", __func__);
672 rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0);
673 rndis_signal_disconnect(rndis->config);
676 /*-------------------------------------------------------------------------*/
678 /* ethernet function driver setup/binding */
681 rndis_bind(struct usb_configuration *c, struct usb_function *f)
683 struct usb_composite_dev *cdev = c->cdev;
684 struct f_rndis *rndis = func_to_rndis(f);
688 /* allocate instance-specific interface IDs */
689 status = usb_interface_id(c, f);
692 rndis->ctrl_id = status;
693 rndis_iad_descriptor.bFirstInterface = status;
695 rndis_control_intf.bInterfaceNumber = status;
696 rndis_union_desc.bMasterInterface0 = status;
698 status = usb_interface_id(c, f);
701 rndis->data_id = status;
703 rndis_data_intf.bInterfaceNumber = status;
704 rndis_union_desc.bSlaveInterface0 = status;
708 /* allocate instance-specific endpoints */
709 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
712 rndis->port.in_ep = ep;
713 ep->driver_data = cdev; /* claim */
715 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
718 rndis->port.out_ep = ep;
719 ep->driver_data = cdev; /* claim */
721 /* NOTE: a status/notification endpoint is, strictly speaking,
722 * optional. We don't treat it that way though! It's simpler,
723 * and some newer profiles don't treat it as optional.
725 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
729 ep->driver_data = cdev; /* claim */
733 /* allocate notification request and buffer */
734 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
735 if (!rndis->notify_req)
737 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
738 if (!rndis->notify_req->buf)
740 rndis->notify_req->length = STATUS_BYTECOUNT;
741 rndis->notify_req->context = rndis;
742 rndis->notify_req->complete = rndis_response_complete;
744 /* support all relevant hardware speeds... we expect that when
745 * hardware is dual speed, all bulk-capable endpoints work at
748 hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
749 hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
750 hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
752 ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
753 ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
754 ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
756 status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
761 rndis->port.open = rndis_open;
762 rndis->port.close = rndis_close;
764 status = rndis_register(rndis_response_available, rndis);
767 rndis->config = status;
769 rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0);
770 rndis_set_host_mac(rndis->config, rndis->ethaddr);
772 if (rndis->manufacturer && rndis->vendorID &&
773 rndis_set_param_vendor(rndis->config, rndis->vendorID,
774 rndis->manufacturer))
777 /* NOTE: all that is done without knowing or caring about
778 * the network link ... which is unavailable to this code
779 * until we're activated via set_alt().
782 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
783 gadget_is_superspeed(c->cdev->gadget) ? "super" :
784 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
785 rndis->port.in_ep->name, rndis->port.out_ep->name,
786 rndis->notify->name);
790 usb_free_all_descriptors(f);
792 if (rndis->notify_req) {
793 kfree(rndis->notify_req->buf);
794 usb_ep_free_request(rndis->notify, rndis->notify_req);
797 /* we might as well release our claims on endpoints */
799 rndis->notify->driver_data = NULL;
800 if (rndis->port.out_ep)
801 rndis->port.out_ep->driver_data = NULL;
802 if (rndis->port.in_ep)
803 rndis->port.in_ep->driver_data = NULL;
805 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
811 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
813 struct f_rndis *rndis = func_to_rndis(f);
815 rndis_deregister(rndis->config);
817 rndis_string_defs[0].id = 0;
819 rndis_string_defs[0].id = 0;
820 usb_free_all_descriptors(f);
822 kfree(rndis->notify_req->buf);
823 usb_ep_free_request(rndis->notify, rndis->notify_req);
828 /* Some controllers can't support RNDIS ... */
829 static inline bool can_support_rndis(struct usb_configuration *c)
831 /* everything else is *presumably* fine */
836 rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
837 u32 vendorID, const char *manufacturer, struct eth_dev *dev)
839 struct f_rndis *rndis;
842 if (!can_support_rndis(c) || !ethaddr)
845 /* setup RNDIS itself */
846 status = rndis_init();
850 status = usb_string_ids_tab(c->cdev, rndis_string_defs);
854 rndis_control_intf.iInterface = rndis_string_defs[0].id;
855 rndis_data_intf.iInterface = rndis_string_defs[1].id;
856 rndis_iad_descriptor.iFunction = rndis_string_defs[2].id;
858 /* allocate and initialize one new instance */
860 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
864 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
865 rndis->vendorID = vendorID;
866 rndis->manufacturer = manufacturer;
868 rndis->port.ioport = dev;
869 /* RNDIS activates when the host changes this filter */
870 rndis->port.cdc_filter = 0;
872 /* RNDIS has special (and complex) framing */
873 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
874 rndis->port.wrap = rndis_add_header;
875 rndis->port.unwrap = rndis_rm_hdr;
877 rndis->port.func.name = "rndis";
878 rndis->port.func.strings = rndis_strings;
879 /* descriptors are per-instance copies */
880 rndis->port.func.bind = rndis_bind;
881 rndis->port.func.unbind = rndis_unbind;
882 rndis->port.func.set_alt = rndis_set_alt;
883 rndis->port.func.setup = rndis_setup;
884 rndis->port.func.disable = rndis_disable;
886 status = usb_add_function(c, &rndis->port.func);