usb: gadget: r8a66597-udc: fix cannot connect after rmmod gadget driver
[linux/fpc-iii.git] / drivers / usb / gadget / f_rndis.c
blobb324efa0773382c90e70bdc009cce3d8522b5ae7
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.
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>
32 #include <asm/atomic.h>
34 #include "u_ether.h"
35 #include "rndis.h"
39 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
40 * been promoted instead of the standard CDC Ethernet. The published RNDIS
41 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
42 * ActiveSync have even worse status in terms of specification.
44 * In short: it's a protocol controlled by (and for) Microsoft, not for an
45 * Open ecosystem or markets. Linux supports it *only* because Microsoft
46 * doesn't support the CDC Ethernet standard.
48 * The RNDIS data transfer model is complex, with multiple Ethernet packets
49 * per USB message, and out of band data. The control model is built around
50 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
51 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
52 * useless (they're ignored). RNDIS expects to be the only function in its
53 * configuration, so it's no real help if you need composite devices; and
54 * it expects to be the first configuration too.
56 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
57 * discount the fluff that its RPC can be made to deliver: it doesn't need
58 * a NOP altsetting for the data interface. That lets it work on some of the
59 * "so smart it's stupid" hardware which takes over configuration changes
60 * from the software, and adds restrictions like "no altsettings".
62 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
63 * have all sorts of contrary-to-specification oddities that can prevent
64 * them from working sanely. Since bugfixes (or accurate specs, letting
65 * Linux work around those bugs) are unlikely to ever come from MSFT, you
66 * may want to avoid using RNDIS on purely operational grounds.
68 * Omissions from the RNDIS 1.0 specification include:
70 * - Power management ... references data that's scattered around lots
71 * of other documentation, which is incorrect/incomplete there too.
73 * - There are various undocumented protocol requirements, like the need
74 * to send garbage in some control-OUT messages.
76 * - MS-Windows drivers sometimes emit undocumented requests.
79 struct f_rndis {
80 struct gether port;
81 u8 ctrl_id, data_id;
82 u8 ethaddr[ETH_ALEN];
83 int config;
85 struct usb_ep *notify;
86 struct usb_request *notify_req;
87 atomic_t notify_count;
90 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
92 return container_of(f, struct f_rndis, port.func);
95 /* peak (theoretical) bulk transfer rate in bits-per-second */
96 static unsigned int bitrate(struct usb_gadget *g)
98 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
99 return 13 * 512 * 8 * 1000 * 8;
100 else
101 return 19 * 64 * 1 * 1000 * 8;
104 /*-------------------------------------------------------------------------*/
109 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
110 #define STATUS_BYTECOUNT 8 /* 8 bytes data */
113 /* interface descriptor: */
115 static struct usb_interface_descriptor rndis_control_intf = {
116 .bLength = sizeof rndis_control_intf,
117 .bDescriptorType = USB_DT_INTERFACE,
119 /* .bInterfaceNumber = DYNAMIC */
120 /* status endpoint is optional; this could be patched later */
121 .bNumEndpoints = 1,
122 .bInterfaceClass = USB_CLASS_COMM,
123 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
124 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
125 /* .iInterface = DYNAMIC */
128 static struct usb_cdc_header_desc header_desc = {
129 .bLength = sizeof header_desc,
130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
133 .bcdCDC = cpu_to_le16(0x0110),
136 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
137 .bLength = sizeof call_mgmt_descriptor,
138 .bDescriptorType = USB_DT_CS_INTERFACE,
139 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
141 .bmCapabilities = 0x00,
142 .bDataInterface = 0x01,
145 static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
146 .bLength = sizeof rndis_acm_descriptor,
147 .bDescriptorType = USB_DT_CS_INTERFACE,
148 .bDescriptorSubType = USB_CDC_ACM_TYPE,
150 .bmCapabilities = 0x00,
153 static struct usb_cdc_union_desc rndis_union_desc = {
154 .bLength = sizeof(rndis_union_desc),
155 .bDescriptorType = USB_DT_CS_INTERFACE,
156 .bDescriptorSubType = USB_CDC_UNION_TYPE,
157 /* .bMasterInterface0 = DYNAMIC */
158 /* .bSlaveInterface0 = DYNAMIC */
161 /* the data interface has two bulk endpoints */
163 static struct usb_interface_descriptor rndis_data_intf = {
164 .bLength = sizeof rndis_data_intf,
165 .bDescriptorType = USB_DT_INTERFACE,
167 /* .bInterfaceNumber = DYNAMIC */
168 .bNumEndpoints = 2,
169 .bInterfaceClass = USB_CLASS_CDC_DATA,
170 .bInterfaceSubClass = 0,
171 .bInterfaceProtocol = 0,
172 /* .iInterface = DYNAMIC */
176 static struct usb_interface_assoc_descriptor
177 rndis_iad_descriptor = {
178 .bLength = sizeof rndis_iad_descriptor,
179 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
181 .bFirstInterface = 0, /* XXX, hardcoded */
182 .bInterfaceCount = 2, // control + data
183 .bFunctionClass = USB_CLASS_COMM,
184 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
185 .bFunctionProtocol = USB_CDC_PROTO_NONE,
186 /* .iFunction = DYNAMIC */
189 /* full speed support: */
191 static struct usb_endpoint_descriptor fs_notify_desc = {
192 .bLength = USB_DT_ENDPOINT_SIZE,
193 .bDescriptorType = USB_DT_ENDPOINT,
195 .bEndpointAddress = USB_DIR_IN,
196 .bmAttributes = USB_ENDPOINT_XFER_INT,
197 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
198 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
201 static struct usb_endpoint_descriptor fs_in_desc = {
202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT,
205 .bEndpointAddress = USB_DIR_IN,
206 .bmAttributes = USB_ENDPOINT_XFER_BULK,
209 static struct usb_endpoint_descriptor fs_out_desc = {
210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
213 .bEndpointAddress = USB_DIR_OUT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
217 static struct usb_descriptor_header *eth_fs_function[] = {
218 (struct usb_descriptor_header *) &rndis_iad_descriptor,
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 *) &rndis_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,
230 NULL,
233 /* high speed support: */
235 static struct usb_endpoint_descriptor hs_notify_desc = {
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 = {
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 = {
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[] = {
263 (struct usb_descriptor_header *) &rndis_iad_descriptor,
264 /* control interface matches ACM, not Ethernet */
265 (struct usb_descriptor_header *) &rndis_control_intf,
266 (struct usb_descriptor_header *) &header_desc,
267 (struct usb_descriptor_header *) &call_mgmt_descriptor,
268 (struct usb_descriptor_header *) &rndis_acm_descriptor,
269 (struct usb_descriptor_header *) &rndis_union_desc,
270 (struct usb_descriptor_header *) &hs_notify_desc,
271 /* data interface has no altsetting */
272 (struct usb_descriptor_header *) &rndis_data_intf,
273 (struct usb_descriptor_header *) &hs_in_desc,
274 (struct usb_descriptor_header *) &hs_out_desc,
275 NULL,
278 /* string descriptors: */
280 static struct usb_string rndis_string_defs[] = {
281 [0].s = "RNDIS Communications Control",
282 [1].s = "RNDIS Ethernet Data",
283 [2].s = "RNDIS",
284 { } /* end of list */
287 static struct usb_gadget_strings rndis_string_table = {
288 .language = 0x0409, /* en-us */
289 .strings = rndis_string_defs,
292 static struct usb_gadget_strings *rndis_strings[] = {
293 &rndis_string_table,
294 NULL,
297 /*-------------------------------------------------------------------------*/
299 static struct sk_buff *rndis_add_header(struct gether *port,
300 struct sk_buff *skb)
302 struct sk_buff *skb2;
304 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
305 if (skb2)
306 rndis_add_hdr(skb2);
308 dev_kfree_skb_any(skb);
309 return skb2;
312 static void rndis_response_available(void *_rndis)
314 struct f_rndis *rndis = _rndis;
315 struct usb_request *req = rndis->notify_req;
316 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
317 __le32 *data = req->buf;
318 int status;
320 if (atomic_inc_return(&rndis->notify_count) != 1)
321 return;
323 /* Send RNDIS RESPONSE_AVAILABLE notification; a
324 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
326 * This is the only notification defined by RNDIS.
328 data[0] = cpu_to_le32(1);
329 data[1] = cpu_to_le32(0);
331 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
332 if (status) {
333 atomic_dec(&rndis->notify_count);
334 DBG(cdev, "notify/0 --> %d\n", status);
338 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
340 struct f_rndis *rndis = req->context;
341 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
342 int status = req->status;
344 /* after TX:
345 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
346 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
348 switch (status) {
349 case -ECONNRESET:
350 case -ESHUTDOWN:
351 /* connection gone */
352 atomic_set(&rndis->notify_count, 0);
353 break;
354 default:
355 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
356 ep->name, status,
357 req->actual, req->length);
358 /* FALLTHROUGH */
359 case 0:
360 if (ep != rndis->notify)
361 break;
363 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
364 * notifications by resending until we're done
366 if (atomic_dec_and_test(&rndis->notify_count))
367 break;
368 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
369 if (status) {
370 atomic_dec(&rndis->notify_count);
371 DBG(cdev, "notify/1 --> %d\n", status);
373 break;
377 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
379 struct f_rndis *rndis = req->context;
380 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
381 int status;
383 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
384 // spin_lock(&dev->lock);
385 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
386 if (status < 0)
387 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
388 status, req->actual, req->length);
389 // spin_unlock(&dev->lock);
392 static int
393 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
395 struct f_rndis *rndis = func_to_rndis(f);
396 struct usb_composite_dev *cdev = f->config->cdev;
397 struct usb_request *req = cdev->req;
398 int value = -EOPNOTSUPP;
399 u16 w_index = le16_to_cpu(ctrl->wIndex);
400 u16 w_value = le16_to_cpu(ctrl->wValue);
401 u16 w_length = le16_to_cpu(ctrl->wLength);
403 /* composite driver infrastructure handles everything except
404 * CDC class messages; interface activation uses set_alt().
406 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
408 /* RNDIS uses the CDC command encapsulation mechanism to implement
409 * an RPC scheme, with much getting/setting of attributes by OID.
411 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
412 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
413 if (w_value || w_index != rndis->ctrl_id)
414 goto invalid;
415 /* read the request; process it later */
416 value = w_length;
417 req->complete = rndis_command_complete;
418 req->context = rndis;
419 /* later, rndis_response_available() sends a notification */
420 break;
422 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
423 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
424 if (w_value || w_index != rndis->ctrl_id)
425 goto invalid;
426 else {
427 u8 *buf;
428 u32 n;
430 /* return the result */
431 buf = rndis_get_next_response(rndis->config, &n);
432 if (buf) {
433 memcpy(req->buf, buf, n);
434 req->complete = rndis_response_complete;
435 rndis_free_response(rndis->config, buf);
436 value = n;
438 /* else stalls ... spec says to avoid that */
440 break;
442 default:
443 invalid:
444 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
445 ctrl->bRequestType, ctrl->bRequest,
446 w_value, w_index, w_length);
449 /* respond with data transfer or status phase? */
450 if (value >= 0) {
451 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
452 ctrl->bRequestType, ctrl->bRequest,
453 w_value, w_index, w_length);
454 req->zero = (value < w_length);
455 req->length = value;
456 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
457 if (value < 0)
458 ERROR(cdev, "rndis response on err %d\n", value);
461 /* device either stalls (value < 0) or reports success */
462 return value;
466 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
468 struct f_rndis *rndis = func_to_rndis(f);
469 struct usb_composite_dev *cdev = f->config->cdev;
471 /* we know alt == 0 */
473 if (intf == rndis->ctrl_id) {
474 if (rndis->notify->driver_data) {
475 VDBG(cdev, "reset rndis control %d\n", intf);
476 usb_ep_disable(rndis->notify);
478 if (!rndis->notify->desc) {
479 VDBG(cdev, "init rndis ctrl %d\n", intf);
480 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
481 goto fail;
483 usb_ep_enable(rndis->notify);
484 rndis->notify->driver_data = rndis;
486 } else if (intf == rndis->data_id) {
487 struct net_device *net;
489 if (rndis->port.in_ep->driver_data) {
490 DBG(cdev, "reset rndis\n");
491 gether_disconnect(&rndis->port);
494 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
495 DBG(cdev, "init rndis\n");
496 if (config_ep_by_speed(cdev->gadget, f,
497 rndis->port.in_ep) ||
498 config_ep_by_speed(cdev->gadget, f,
499 rndis->port.out_ep)) {
500 rndis->port.in_ep->desc = NULL;
501 rndis->port.out_ep->desc = NULL;
502 goto fail;
506 /* Avoid ZLPs; they can be troublesome. */
507 rndis->port.is_zlp_ok = false;
509 /* RNDIS should be in the "RNDIS uninitialized" state,
510 * either never activated or after rndis_uninit().
512 * We don't want data to flow here until a nonzero packet
513 * filter is set, at which point it enters "RNDIS data
514 * initialized" state ... but we do want the endpoints
515 * to be activated. It's a strange little state.
517 * REVISIT the RNDIS gadget code has done this wrong for a
518 * very long time. We need another call to the link layer
519 * code -- gether_updown(...bool) maybe -- to do it right.
521 rndis->port.cdc_filter = 0;
523 DBG(cdev, "RNDIS RX/TX early activation ... \n");
524 net = gether_connect(&rndis->port);
525 if (IS_ERR(net))
526 return PTR_ERR(net);
528 rndis_set_param_dev(rndis->config, net,
529 &rndis->port.cdc_filter);
530 } else
531 goto fail;
533 return 0;
534 fail:
535 return -EINVAL;
538 static void rndis_disable(struct usb_function *f)
540 struct f_rndis *rndis = func_to_rndis(f);
541 struct usb_composite_dev *cdev = f->config->cdev;
543 if (!rndis->notify->driver_data)
544 return;
546 DBG(cdev, "rndis deactivated\n");
548 rndis_uninit(rndis->config);
549 gether_disconnect(&rndis->port);
551 usb_ep_disable(rndis->notify);
552 rndis->notify->driver_data = NULL;
555 /*-------------------------------------------------------------------------*/
558 * This isn't quite the same mechanism as CDC Ethernet, since the
559 * notification scheme passes less data, but the same set of link
560 * states must be tested. A key difference is that altsettings are
561 * not used to tell whether the link should send packets or not.
564 static void rndis_open(struct gether *geth)
566 struct f_rndis *rndis = func_to_rndis(&geth->func);
567 struct usb_composite_dev *cdev = geth->func.config->cdev;
569 DBG(cdev, "%s\n", __func__);
571 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
572 bitrate(cdev->gadget) / 100);
573 rndis_signal_connect(rndis->config);
576 static void rndis_close(struct gether *geth)
578 struct f_rndis *rndis = func_to_rndis(&geth->func);
580 DBG(geth->func.config->cdev, "%s\n", __func__);
582 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
583 rndis_signal_disconnect(rndis->config);
586 /*-------------------------------------------------------------------------*/
588 /* ethernet function driver setup/binding */
590 static int
591 rndis_bind(struct usb_configuration *c, struct usb_function *f)
593 struct usb_composite_dev *cdev = c->cdev;
594 struct f_rndis *rndis = func_to_rndis(f);
595 int status;
596 struct usb_ep *ep;
598 /* allocate instance-specific interface IDs */
599 status = usb_interface_id(c, f);
600 if (status < 0)
601 goto fail;
602 rndis->ctrl_id = status;
603 rndis_iad_descriptor.bFirstInterface = status;
605 rndis_control_intf.bInterfaceNumber = status;
606 rndis_union_desc.bMasterInterface0 = status;
608 status = usb_interface_id(c, f);
609 if (status < 0)
610 goto fail;
611 rndis->data_id = status;
613 rndis_data_intf.bInterfaceNumber = status;
614 rndis_union_desc.bSlaveInterface0 = status;
616 status = -ENODEV;
618 /* allocate instance-specific endpoints */
619 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
620 if (!ep)
621 goto fail;
622 rndis->port.in_ep = ep;
623 ep->driver_data = cdev; /* claim */
625 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
626 if (!ep)
627 goto fail;
628 rndis->port.out_ep = ep;
629 ep->driver_data = cdev; /* claim */
631 /* NOTE: a status/notification endpoint is, strictly speaking,
632 * optional. We don't treat it that way though! It's simpler,
633 * and some newer profiles don't treat it as optional.
635 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
636 if (!ep)
637 goto fail;
638 rndis->notify = ep;
639 ep->driver_data = cdev; /* claim */
641 status = -ENOMEM;
643 /* allocate notification request and buffer */
644 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
645 if (!rndis->notify_req)
646 goto fail;
647 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
648 if (!rndis->notify_req->buf)
649 goto fail;
650 rndis->notify_req->length = STATUS_BYTECOUNT;
651 rndis->notify_req->context = rndis;
652 rndis->notify_req->complete = rndis_response_complete;
654 /* copy descriptors, and track endpoint copies */
655 f->descriptors = usb_copy_descriptors(eth_fs_function);
656 if (!f->descriptors)
657 goto fail;
659 /* support all relevant hardware speeds... we expect that when
660 * hardware is dual speed, all bulk-capable endpoints work at
661 * both speeds
663 if (gadget_is_dualspeed(c->cdev->gadget)) {
664 hs_in_desc.bEndpointAddress =
665 fs_in_desc.bEndpointAddress;
666 hs_out_desc.bEndpointAddress =
667 fs_out_desc.bEndpointAddress;
668 hs_notify_desc.bEndpointAddress =
669 fs_notify_desc.bEndpointAddress;
671 /* copy descriptors, and track endpoint copies */
672 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
674 if (!f->hs_descriptors)
675 goto fail;
678 rndis->port.open = rndis_open;
679 rndis->port.close = rndis_close;
681 status = rndis_register(rndis_response_available, rndis);
682 if (status < 0)
683 goto fail;
684 rndis->config = status;
686 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
687 rndis_set_host_mac(rndis->config, rndis->ethaddr);
689 #if 0
690 // FIXME
691 if (rndis_set_param_vendor(rndis->config, vendorID,
692 manufacturer))
693 goto fail0;
694 #endif
696 /* NOTE: all that is done without knowing or caring about
697 * the network link ... which is unavailable to this code
698 * until we're activated via set_alt().
701 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
702 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
703 rndis->port.in_ep->name, rndis->port.out_ep->name,
704 rndis->notify->name);
705 return 0;
707 fail:
708 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
709 usb_free_descriptors(f->hs_descriptors);
710 if (f->descriptors)
711 usb_free_descriptors(f->descriptors);
713 if (rndis->notify_req) {
714 kfree(rndis->notify_req->buf);
715 usb_ep_free_request(rndis->notify, rndis->notify_req);
718 /* we might as well release our claims on endpoints */
719 if (rndis->notify)
720 rndis->notify->driver_data = NULL;
721 if (rndis->port.out_ep->desc)
722 rndis->port.out_ep->driver_data = NULL;
723 if (rndis->port.in_ep->desc)
724 rndis->port.in_ep->driver_data = NULL;
726 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
728 return status;
731 static void
732 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
734 struct f_rndis *rndis = func_to_rndis(f);
736 rndis_deregister(rndis->config);
737 rndis_exit();
739 if (gadget_is_dualspeed(c->cdev->gadget))
740 usb_free_descriptors(f->hs_descriptors);
741 usb_free_descriptors(f->descriptors);
743 kfree(rndis->notify_req->buf);
744 usb_ep_free_request(rndis->notify, rndis->notify_req);
746 kfree(rndis);
749 /* Some controllers can't support RNDIS ... */
750 static inline bool can_support_rndis(struct usb_configuration *c)
752 /* everything else is *presumably* fine */
753 return true;
757 * rndis_bind_config - add RNDIS network link to a configuration
758 * @c: the configuration to support the network link
759 * @ethaddr: a buffer in which the ethernet address of the host side
760 * side of the link was recorded
761 * Context: single threaded during gadget setup
763 * Returns zero on success, else negative errno.
765 * Caller must have called @gether_setup(). Caller is also responsible
766 * for calling @gether_cleanup() before module unload.
769 rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
771 struct f_rndis *rndis;
772 int status;
774 if (!can_support_rndis(c) || !ethaddr)
775 return -EINVAL;
777 /* maybe allocate device-global string IDs */
778 if (rndis_string_defs[0].id == 0) {
780 /* ... and setup RNDIS itself */
781 status = rndis_init();
782 if (status < 0)
783 return status;
785 /* control interface label */
786 status = usb_string_id(c->cdev);
787 if (status < 0)
788 return status;
789 rndis_string_defs[0].id = status;
790 rndis_control_intf.iInterface = status;
792 /* data interface label */
793 status = usb_string_id(c->cdev);
794 if (status < 0)
795 return status;
796 rndis_string_defs[1].id = status;
797 rndis_data_intf.iInterface = status;
799 /* IAD iFunction label */
800 status = usb_string_id(c->cdev);
801 if (status < 0)
802 return status;
803 rndis_string_defs[2].id = status;
804 rndis_iad_descriptor.iFunction = status;
807 /* allocate and initialize one new instance */
808 status = -ENOMEM;
809 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
810 if (!rndis)
811 goto fail;
813 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
815 /* RNDIS activates when the host changes this filter */
816 rndis->port.cdc_filter = 0;
818 /* RNDIS has special (and complex) framing */
819 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
820 rndis->port.wrap = rndis_add_header;
821 rndis->port.unwrap = rndis_rm_hdr;
823 rndis->port.func.name = "rndis";
824 rndis->port.func.strings = rndis_strings;
825 /* descriptors are per-instance copies */
826 rndis->port.func.bind = rndis_bind;
827 rndis->port.func.unbind = rndis_unbind;
828 rndis->port.func.set_alt = rndis_set_alt;
829 rndis->port.func.setup = rndis_setup;
830 rndis->port.func.disable = rndis_disable;
832 status = usb_add_function(c, &rndis->port.func);
833 if (status) {
834 kfree(rndis);
835 fail:
836 rndis_exit();
838 return status;