Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / usb / gadget / f_rndis.c
blob704d1d94f72a6c441c702ba1a3e2065a8ac92166
1 /*
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.
16 /* #define VERBOSE_DEBUG */
18 #include <linux/slab.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/etherdevice.h>
23 #include <linux/atomic.h>
25 #include "u_ether.h"
26 #include "rndis.h"
30 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
31 * been promoted instead of the standard CDC Ethernet. The published RNDIS
32 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
33 * ActiveSync have even worse status in terms of specification.
35 * In short: it's a protocol controlled by (and for) Microsoft, not for an
36 * Open ecosystem or markets. Linux supports it *only* because Microsoft
37 * doesn't support the CDC Ethernet standard.
39 * The RNDIS data transfer model is complex, with multiple Ethernet packets
40 * per USB message, and out of band data. The control model is built around
41 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
42 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
43 * useless (they're ignored). RNDIS expects to be the only function in its
44 * configuration, so it's no real help if you need composite devices; and
45 * it expects to be the first configuration too.
47 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
48 * discount the fluff that its RPC can be made to deliver: it doesn't need
49 * a NOP altsetting for the data interface. That lets it work on some of the
50 * "so smart it's stupid" hardware which takes over configuration changes
51 * from the software, and adds restrictions like "no altsettings".
53 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
54 * have all sorts of contrary-to-specification oddities that can prevent
55 * them from working sanely. Since bugfixes (or accurate specs, letting
56 * Linux work around those bugs) are unlikely to ever come from MSFT, you
57 * may want to avoid using RNDIS on purely operational grounds.
59 * Omissions from the RNDIS 1.0 specification include:
61 * - Power management ... references data that's scattered around lots
62 * of other documentation, which is incorrect/incomplete there too.
64 * - There are various undocumented protocol requirements, like the need
65 * to send garbage in some control-OUT messages.
67 * - MS-Windows drivers sometimes emit undocumented requests.
70 struct f_rndis {
71 struct gether port;
72 u8 ctrl_id, data_id;
73 u8 ethaddr[ETH_ALEN];
74 int config;
76 struct usb_ep *notify;
77 struct usb_request *notify_req;
78 atomic_t notify_count;
81 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
83 return container_of(f, struct f_rndis, port.func);
86 /* peak (theoretical) bulk transfer rate in bits-per-second */
87 static unsigned int bitrate(struct usb_gadget *g)
89 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
90 return 13 * 1024 * 8 * 1000 * 8;
91 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
92 return 13 * 512 * 8 * 1000 * 8;
93 else
94 return 19 * 64 * 1 * 1000 * 8;
97 /*-------------------------------------------------------------------------*/
102 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
103 #define STATUS_BYTECOUNT 8 /* 8 bytes data */
106 /* interface descriptor: */
108 static struct usb_interface_descriptor rndis_control_intf = {
109 .bLength = sizeof rndis_control_intf,
110 .bDescriptorType = USB_DT_INTERFACE,
112 /* .bInterfaceNumber = DYNAMIC */
113 /* status endpoint is optional; this could be patched later */
114 .bNumEndpoints = 1,
115 .bInterfaceClass = USB_CLASS_COMM,
116 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
117 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
118 /* .iInterface = DYNAMIC */
121 static struct usb_cdc_header_desc header_desc = {
122 .bLength = sizeof header_desc,
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
126 .bcdCDC = cpu_to_le16(0x0110),
129 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
130 .bLength = sizeof call_mgmt_descriptor,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
134 .bmCapabilities = 0x00,
135 .bDataInterface = 0x01,
138 static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
139 .bLength = sizeof rndis_acm_descriptor,
140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_ACM_TYPE,
143 .bmCapabilities = 0x00,
146 static struct usb_cdc_union_desc rndis_union_desc = {
147 .bLength = sizeof(rndis_union_desc),
148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_UNION_TYPE,
150 /* .bMasterInterface0 = DYNAMIC */
151 /* .bSlaveInterface0 = DYNAMIC */
154 /* the data interface has two bulk endpoints */
156 static struct usb_interface_descriptor rndis_data_intf = {
157 .bLength = sizeof rndis_data_intf,
158 .bDescriptorType = USB_DT_INTERFACE,
160 /* .bInterfaceNumber = DYNAMIC */
161 .bNumEndpoints = 2,
162 .bInterfaceClass = USB_CLASS_CDC_DATA,
163 .bInterfaceSubClass = 0,
164 .bInterfaceProtocol = 0,
165 /* .iInterface = DYNAMIC */
169 static struct usb_interface_assoc_descriptor
170 rndis_iad_descriptor = {
171 .bLength = sizeof rndis_iad_descriptor,
172 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
174 .bFirstInterface = 0, /* XXX, hardcoded */
175 .bInterfaceCount = 2, // control + data
176 .bFunctionClass = USB_CLASS_COMM,
177 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
178 .bFunctionProtocol = USB_CDC_PROTO_NONE,
179 /* .iFunction = DYNAMIC */
182 /* full speed support: */
184 static struct usb_endpoint_descriptor fs_notify_desc = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_INT,
190 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
191 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
194 static struct usb_endpoint_descriptor fs_in_desc = {
195 .bLength = USB_DT_ENDPOINT_SIZE,
196 .bDescriptorType = USB_DT_ENDPOINT,
198 .bEndpointAddress = USB_DIR_IN,
199 .bmAttributes = USB_ENDPOINT_XFER_BULK,
202 static struct usb_endpoint_descriptor fs_out_desc = {
203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
206 .bEndpointAddress = USB_DIR_OUT,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
210 static struct usb_descriptor_header *eth_fs_function[] = {
211 (struct usb_descriptor_header *) &rndis_iad_descriptor,
213 /* control interface matches ACM, not Ethernet */
214 (struct usb_descriptor_header *) &rndis_control_intf,
215 (struct usb_descriptor_header *) &header_desc,
216 (struct usb_descriptor_header *) &call_mgmt_descriptor,
217 (struct usb_descriptor_header *) &rndis_acm_descriptor,
218 (struct usb_descriptor_header *) &rndis_union_desc,
219 (struct usb_descriptor_header *) &fs_notify_desc,
221 /* data interface has no altsetting */
222 (struct usb_descriptor_header *) &rndis_data_intf,
223 (struct usb_descriptor_header *) &fs_in_desc,
224 (struct usb_descriptor_header *) &fs_out_desc,
225 NULL,
228 /* high speed support: */
230 static struct usb_endpoint_descriptor hs_notify_desc = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
234 .bEndpointAddress = USB_DIR_IN,
235 .bmAttributes = USB_ENDPOINT_XFER_INT,
236 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
237 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
240 static struct usb_endpoint_descriptor hs_in_desc = {
241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
244 .bEndpointAddress = USB_DIR_IN,
245 .bmAttributes = USB_ENDPOINT_XFER_BULK,
246 .wMaxPacketSize = cpu_to_le16(512),
249 static struct usb_endpoint_descriptor hs_out_desc = {
250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
253 .bEndpointAddress = USB_DIR_OUT,
254 .bmAttributes = USB_ENDPOINT_XFER_BULK,
255 .wMaxPacketSize = cpu_to_le16(512),
258 static struct usb_descriptor_header *eth_hs_function[] = {
259 (struct usb_descriptor_header *) &rndis_iad_descriptor,
261 /* control interface matches ACM, not Ethernet */
262 (struct usb_descriptor_header *) &rndis_control_intf,
263 (struct usb_descriptor_header *) &header_desc,
264 (struct usb_descriptor_header *) &call_mgmt_descriptor,
265 (struct usb_descriptor_header *) &rndis_acm_descriptor,
266 (struct usb_descriptor_header *) &rndis_union_desc,
267 (struct usb_descriptor_header *) &hs_notify_desc,
269 /* data interface has no altsetting */
270 (struct usb_descriptor_header *) &rndis_data_intf,
271 (struct usb_descriptor_header *) &hs_in_desc,
272 (struct usb_descriptor_header *) &hs_out_desc,
273 NULL,
276 /* super speed support: */
278 static struct usb_endpoint_descriptor ss_notify_desc = {
279 .bLength = USB_DT_ENDPOINT_SIZE,
280 .bDescriptorType = USB_DT_ENDPOINT,
282 .bEndpointAddress = USB_DIR_IN,
283 .bmAttributes = USB_ENDPOINT_XFER_INT,
284 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
285 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
288 static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
289 .bLength = sizeof ss_intr_comp_desc,
290 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
292 /* the following 3 values can be tweaked if necessary */
293 /* .bMaxBurst = 0, */
294 /* .bmAttributes = 0, */
295 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
298 static struct usb_endpoint_descriptor ss_in_desc = {
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
302 .bEndpointAddress = USB_DIR_IN,
303 .bmAttributes = USB_ENDPOINT_XFER_BULK,
304 .wMaxPacketSize = cpu_to_le16(1024),
307 static struct usb_endpoint_descriptor ss_out_desc = {
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
311 .bEndpointAddress = USB_DIR_OUT,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 .wMaxPacketSize = cpu_to_le16(1024),
316 static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
317 .bLength = sizeof ss_bulk_comp_desc,
318 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
320 /* the following 2 values can be tweaked if necessary */
321 /* .bMaxBurst = 0, */
322 /* .bmAttributes = 0, */
325 static struct usb_descriptor_header *eth_ss_function[] = {
326 (struct usb_descriptor_header *) &rndis_iad_descriptor,
328 /* control interface matches ACM, not Ethernet */
329 (struct usb_descriptor_header *) &rndis_control_intf,
330 (struct usb_descriptor_header *) &header_desc,
331 (struct usb_descriptor_header *) &call_mgmt_descriptor,
332 (struct usb_descriptor_header *) &rndis_acm_descriptor,
333 (struct usb_descriptor_header *) &rndis_union_desc,
334 (struct usb_descriptor_header *) &ss_notify_desc,
335 (struct usb_descriptor_header *) &ss_intr_comp_desc,
337 /* data interface has no altsetting */
338 (struct usb_descriptor_header *) &rndis_data_intf,
339 (struct usb_descriptor_header *) &ss_in_desc,
340 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
341 (struct usb_descriptor_header *) &ss_out_desc,
342 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
343 NULL,
346 /* string descriptors: */
348 static struct usb_string rndis_string_defs[] = {
349 [0].s = "RNDIS Communications Control",
350 [1].s = "RNDIS Ethernet Data",
351 [2].s = "RNDIS",
352 { } /* end of list */
355 static struct usb_gadget_strings rndis_string_table = {
356 .language = 0x0409, /* en-us */
357 .strings = rndis_string_defs,
360 static struct usb_gadget_strings *rndis_strings[] = {
361 &rndis_string_table,
362 NULL,
365 /*-------------------------------------------------------------------------*/
367 static struct sk_buff *rndis_add_header(struct gether *port,
368 struct sk_buff *skb)
370 struct sk_buff *skb2;
372 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
373 if (skb2)
374 rndis_add_hdr(skb2);
376 dev_kfree_skb_any(skb);
377 return skb2;
380 static void rndis_response_available(void *_rndis)
382 struct f_rndis *rndis = _rndis;
383 struct usb_request *req = rndis->notify_req;
384 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
385 __le32 *data = req->buf;
386 int status;
388 if (atomic_inc_return(&rndis->notify_count) != 1)
389 return;
391 /* Send RNDIS RESPONSE_AVAILABLE notification; a
392 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
394 * This is the only notification defined by RNDIS.
396 data[0] = cpu_to_le32(1);
397 data[1] = cpu_to_le32(0);
399 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
400 if (status) {
401 atomic_dec(&rndis->notify_count);
402 DBG(cdev, "notify/0 --> %d\n", status);
406 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
408 struct f_rndis *rndis = req->context;
409 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
410 int status = req->status;
412 /* after TX:
413 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
414 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
416 switch (status) {
417 case -ECONNRESET:
418 case -ESHUTDOWN:
419 /* connection gone */
420 atomic_set(&rndis->notify_count, 0);
421 break;
422 default:
423 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
424 ep->name, status,
425 req->actual, req->length);
426 /* FALLTHROUGH */
427 case 0:
428 if (ep != rndis->notify)
429 break;
431 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
432 * notifications by resending until we're done
434 if (atomic_dec_and_test(&rndis->notify_count))
435 break;
436 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
437 if (status) {
438 atomic_dec(&rndis->notify_count);
439 DBG(cdev, "notify/1 --> %d\n", status);
441 break;
445 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
447 struct f_rndis *rndis = req->context;
448 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
449 int status;
451 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
452 // spin_lock(&dev->lock);
453 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
454 if (status < 0)
455 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
456 status, req->actual, req->length);
457 // spin_unlock(&dev->lock);
460 static int
461 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
463 struct f_rndis *rndis = func_to_rndis(f);
464 struct usb_composite_dev *cdev = f->config->cdev;
465 struct usb_request *req = cdev->req;
466 int value = -EOPNOTSUPP;
467 u16 w_index = le16_to_cpu(ctrl->wIndex);
468 u16 w_value = le16_to_cpu(ctrl->wValue);
469 u16 w_length = le16_to_cpu(ctrl->wLength);
471 /* composite driver infrastructure handles everything except
472 * CDC class messages; interface activation uses set_alt().
474 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
476 /* RNDIS uses the CDC command encapsulation mechanism to implement
477 * an RPC scheme, with much getting/setting of attributes by OID.
479 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
480 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
481 if (w_value || w_index != rndis->ctrl_id)
482 goto invalid;
483 /* read the request; process it later */
484 value = w_length;
485 req->complete = rndis_command_complete;
486 req->context = rndis;
487 /* later, rndis_response_available() sends a notification */
488 break;
490 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
491 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
492 if (w_value || w_index != rndis->ctrl_id)
493 goto invalid;
494 else {
495 u8 *buf;
496 u32 n;
498 /* return the result */
499 buf = rndis_get_next_response(rndis->config, &n);
500 if (buf) {
501 memcpy(req->buf, buf, n);
502 req->complete = rndis_response_complete;
503 rndis_free_response(rndis->config, buf);
504 value = n;
506 /* else stalls ... spec says to avoid that */
508 break;
510 default:
511 invalid:
512 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
513 ctrl->bRequestType, ctrl->bRequest,
514 w_value, w_index, w_length);
517 /* respond with data transfer or status phase? */
518 if (value >= 0) {
519 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
520 ctrl->bRequestType, ctrl->bRequest,
521 w_value, w_index, w_length);
522 req->zero = (value < w_length);
523 req->length = value;
524 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
525 if (value < 0)
526 ERROR(cdev, "rndis response on err %d\n", value);
529 /* device either stalls (value < 0) or reports success */
530 return value;
534 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
536 struct f_rndis *rndis = func_to_rndis(f);
537 struct usb_composite_dev *cdev = f->config->cdev;
539 /* we know alt == 0 */
541 if (intf == rndis->ctrl_id) {
542 if (rndis->notify->driver_data) {
543 VDBG(cdev, "reset rndis control %d\n", intf);
544 usb_ep_disable(rndis->notify);
546 if (!rndis->notify->desc) {
547 VDBG(cdev, "init rndis ctrl %d\n", intf);
548 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
549 goto fail;
551 usb_ep_enable(rndis->notify);
552 rndis->notify->driver_data = rndis;
554 } else if (intf == rndis->data_id) {
555 struct net_device *net;
557 if (rndis->port.in_ep->driver_data) {
558 DBG(cdev, "reset rndis\n");
559 gether_disconnect(&rndis->port);
562 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
563 DBG(cdev, "init rndis\n");
564 if (config_ep_by_speed(cdev->gadget, f,
565 rndis->port.in_ep) ||
566 config_ep_by_speed(cdev->gadget, f,
567 rndis->port.out_ep)) {
568 rndis->port.in_ep->desc = NULL;
569 rndis->port.out_ep->desc = NULL;
570 goto fail;
574 /* Avoid ZLPs; they can be troublesome. */
575 rndis->port.is_zlp_ok = false;
577 /* RNDIS should be in the "RNDIS uninitialized" state,
578 * either never activated or after rndis_uninit().
580 * We don't want data to flow here until a nonzero packet
581 * filter is set, at which point it enters "RNDIS data
582 * initialized" state ... but we do want the endpoints
583 * to be activated. It's a strange little state.
585 * REVISIT the RNDIS gadget code has done this wrong for a
586 * very long time. We need another call to the link layer
587 * code -- gether_updown(...bool) maybe -- to do it right.
589 rndis->port.cdc_filter = 0;
591 DBG(cdev, "RNDIS RX/TX early activation ... \n");
592 net = gether_connect(&rndis->port);
593 if (IS_ERR(net))
594 return PTR_ERR(net);
596 rndis_set_param_dev(rndis->config, net,
597 &rndis->port.cdc_filter);
598 } else
599 goto fail;
601 return 0;
602 fail:
603 return -EINVAL;
606 static void rndis_disable(struct usb_function *f)
608 struct f_rndis *rndis = func_to_rndis(f);
609 struct usb_composite_dev *cdev = f->config->cdev;
611 if (!rndis->notify->driver_data)
612 return;
614 DBG(cdev, "rndis deactivated\n");
616 rndis_uninit(rndis->config);
617 gether_disconnect(&rndis->port);
619 usb_ep_disable(rndis->notify);
620 rndis->notify->driver_data = NULL;
623 /*-------------------------------------------------------------------------*/
626 * This isn't quite the same mechanism as CDC Ethernet, since the
627 * notification scheme passes less data, but the same set of link
628 * states must be tested. A key difference is that altsettings are
629 * not used to tell whether the link should send packets or not.
632 static void rndis_open(struct gether *geth)
634 struct f_rndis *rndis = func_to_rndis(&geth->func);
635 struct usb_composite_dev *cdev = geth->func.config->cdev;
637 DBG(cdev, "%s\n", __func__);
639 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
640 bitrate(cdev->gadget) / 100);
641 rndis_signal_connect(rndis->config);
644 static void rndis_close(struct gether *geth)
646 struct f_rndis *rndis = func_to_rndis(&geth->func);
648 DBG(geth->func.config->cdev, "%s\n", __func__);
650 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
651 rndis_signal_disconnect(rndis->config);
654 /*-------------------------------------------------------------------------*/
656 /* ethernet function driver setup/binding */
658 static int
659 rndis_bind(struct usb_configuration *c, struct usb_function *f)
661 struct usb_composite_dev *cdev = c->cdev;
662 struct f_rndis *rndis = func_to_rndis(f);
663 int status;
664 struct usb_ep *ep;
666 /* allocate instance-specific interface IDs */
667 status = usb_interface_id(c, f);
668 if (status < 0)
669 goto fail;
670 rndis->ctrl_id = status;
671 rndis_iad_descriptor.bFirstInterface = status;
673 rndis_control_intf.bInterfaceNumber = status;
674 rndis_union_desc.bMasterInterface0 = status;
676 status = usb_interface_id(c, f);
677 if (status < 0)
678 goto fail;
679 rndis->data_id = status;
681 rndis_data_intf.bInterfaceNumber = status;
682 rndis_union_desc.bSlaveInterface0 = status;
684 status = -ENODEV;
686 /* allocate instance-specific endpoints */
687 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
688 if (!ep)
689 goto fail;
690 rndis->port.in_ep = ep;
691 ep->driver_data = cdev; /* claim */
693 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
694 if (!ep)
695 goto fail;
696 rndis->port.out_ep = ep;
697 ep->driver_data = cdev; /* claim */
699 /* NOTE: a status/notification endpoint is, strictly speaking,
700 * optional. We don't treat it that way though! It's simpler,
701 * and some newer profiles don't treat it as optional.
703 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
704 if (!ep)
705 goto fail;
706 rndis->notify = ep;
707 ep->driver_data = cdev; /* claim */
709 status = -ENOMEM;
711 /* allocate notification request and buffer */
712 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
713 if (!rndis->notify_req)
714 goto fail;
715 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
716 if (!rndis->notify_req->buf)
717 goto fail;
718 rndis->notify_req->length = STATUS_BYTECOUNT;
719 rndis->notify_req->context = rndis;
720 rndis->notify_req->complete = rndis_response_complete;
722 /* copy descriptors, and track endpoint copies */
723 f->descriptors = usb_copy_descriptors(eth_fs_function);
724 if (!f->descriptors)
725 goto fail;
727 /* support all relevant hardware speeds... we expect that when
728 * hardware is dual speed, all bulk-capable endpoints work at
729 * both speeds
731 if (gadget_is_dualspeed(c->cdev->gadget)) {
732 hs_in_desc.bEndpointAddress =
733 fs_in_desc.bEndpointAddress;
734 hs_out_desc.bEndpointAddress =
735 fs_out_desc.bEndpointAddress;
736 hs_notify_desc.bEndpointAddress =
737 fs_notify_desc.bEndpointAddress;
739 /* copy descriptors, and track endpoint copies */
740 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
741 if (!f->hs_descriptors)
742 goto fail;
745 if (gadget_is_superspeed(c->cdev->gadget)) {
746 ss_in_desc.bEndpointAddress =
747 fs_in_desc.bEndpointAddress;
748 ss_out_desc.bEndpointAddress =
749 fs_out_desc.bEndpointAddress;
750 ss_notify_desc.bEndpointAddress =
751 fs_notify_desc.bEndpointAddress;
753 /* copy descriptors, and track endpoint copies */
754 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
755 if (!f->ss_descriptors)
756 goto fail;
759 rndis->port.open = rndis_open;
760 rndis->port.close = rndis_close;
762 status = rndis_register(rndis_response_available, rndis);
763 if (status < 0)
764 goto fail;
765 rndis->config = status;
767 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
768 rndis_set_host_mac(rndis->config, rndis->ethaddr);
770 #if 0
771 // FIXME
772 if (rndis_set_param_vendor(rndis->config, vendorID,
773 manufacturer))
774 goto fail0;
775 #endif
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);
787 return 0;
789 fail:
790 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
791 usb_free_descriptors(f->ss_descriptors);
792 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
793 usb_free_descriptors(f->hs_descriptors);
794 if (f->descriptors)
795 usb_free_descriptors(f->descriptors);
797 if (rndis->notify_req) {
798 kfree(rndis->notify_req->buf);
799 usb_ep_free_request(rndis->notify, rndis->notify_req);
802 /* we might as well release our claims on endpoints */
803 if (rndis->notify)
804 rndis->notify->driver_data = NULL;
805 if (rndis->port.out_ep->desc)
806 rndis->port.out_ep->driver_data = NULL;
807 if (rndis->port.in_ep->desc)
808 rndis->port.in_ep->driver_data = NULL;
810 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
812 return status;
815 static void
816 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
818 struct f_rndis *rndis = func_to_rndis(f);
820 rndis_deregister(rndis->config);
821 rndis_exit();
823 if (gadget_is_superspeed(c->cdev->gadget))
824 usb_free_descriptors(f->ss_descriptors);
825 if (gadget_is_dualspeed(c->cdev->gadget))
826 usb_free_descriptors(f->hs_descriptors);
827 usb_free_descriptors(f->descriptors);
829 kfree(rndis->notify_req->buf);
830 usb_ep_free_request(rndis->notify, rndis->notify_req);
832 kfree(rndis);
835 /* Some controllers can't support RNDIS ... */
836 static inline bool can_support_rndis(struct usb_configuration *c)
838 /* everything else is *presumably* fine */
839 return true;
843 * rndis_bind_config - add RNDIS network link to a configuration
844 * @c: the configuration to support the network link
845 * @ethaddr: a buffer in which the ethernet address of the host side
846 * side of the link was recorded
847 * Context: single threaded during gadget setup
849 * Returns zero on success, else negative errno.
851 * Caller must have called @gether_setup(). Caller is also responsible
852 * for calling @gether_cleanup() before module unload.
855 rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
857 struct f_rndis *rndis;
858 int status;
860 if (!can_support_rndis(c) || !ethaddr)
861 return -EINVAL;
863 /* maybe allocate device-global string IDs */
864 if (rndis_string_defs[0].id == 0) {
866 /* ... and setup RNDIS itself */
867 status = rndis_init();
868 if (status < 0)
869 return status;
871 /* control interface label */
872 status = usb_string_id(c->cdev);
873 if (status < 0)
874 return status;
875 rndis_string_defs[0].id = status;
876 rndis_control_intf.iInterface = status;
878 /* data interface label */
879 status = usb_string_id(c->cdev);
880 if (status < 0)
881 return status;
882 rndis_string_defs[1].id = status;
883 rndis_data_intf.iInterface = status;
885 /* IAD iFunction label */
886 status = usb_string_id(c->cdev);
887 if (status < 0)
888 return status;
889 rndis_string_defs[2].id = status;
890 rndis_iad_descriptor.iFunction = status;
893 /* allocate and initialize one new instance */
894 status = -ENOMEM;
895 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
896 if (!rndis)
897 goto fail;
899 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
901 /* RNDIS activates when the host changes this filter */
902 rndis->port.cdc_filter = 0;
904 /* RNDIS has special (and complex) framing */
905 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
906 rndis->port.wrap = rndis_add_header;
907 rndis->port.unwrap = rndis_rm_hdr;
909 rndis->port.func.name = "rndis";
910 rndis->port.func.strings = rndis_strings;
911 /* descriptors are per-instance copies */
912 rndis->port.func.bind = rndis_bind;
913 rndis->port.func.unbind = rndis_unbind;
914 rndis->port.func.set_alt = rndis_set_alt;
915 rndis->port.func.setup = rndis_setup;
916 rndis->port.func.disable = rndis_disable;
918 status = usb_add_function(c, &rndis->port.func);
919 if (status) {
920 kfree(rndis);
921 fail:
922 rndis_exit();
924 return status;