usb: gadget: rndis: don't test against req->length
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / f_rndis.c
blob5201374ae13cdae8d5328aa2815cc6d8e133782c
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
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/device.h>
27 #include <linux/etherdevice.h>
29 #include <asm/atomic.h>
31 #include "u_ether.h"
32 #include "rndis.h"
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.
76 struct rndis_ep_descs {
77 struct usb_endpoint_descriptor *in;
78 struct usb_endpoint_descriptor *out;
79 struct usb_endpoint_descriptor *notify;
82 struct f_rndis {
83 struct gether port;
84 u8 ctrl_id, data_id;
85 u8 ethaddr[ETH_ALEN];
86 int config;
88 struct rndis_ep_descs fs;
89 struct rndis_ep_descs hs;
91 struct usb_ep *notify;
92 struct usb_endpoint_descriptor *notify_desc;
93 struct usb_request *notify_req;
94 atomic_t notify_count;
97 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
99 return container_of(f, struct f_rndis, port.func);
102 /* peak (theoretical) bulk transfer rate in bits-per-second */
103 static unsigned int bitrate(struct usb_gadget *g)
105 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
106 return 13 * 512 * 8 * 1000 * 8;
107 else
108 return 19 * 64 * 1 * 1000 * 8;
111 /*-------------------------------------------------------------------------*/
116 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
117 #define STATUS_BYTECOUNT 8 /* 8 bytes data */
120 /* interface descriptor: */
122 static struct usb_interface_descriptor rndis_control_intf __initdata = {
123 .bLength = sizeof rndis_control_intf,
124 .bDescriptorType = USB_DT_INTERFACE,
126 /* .bInterfaceNumber = DYNAMIC */
127 /* status endpoint is optional; this could be patched later */
128 .bNumEndpoints = 1,
129 .bInterfaceClass = USB_CLASS_COMM,
130 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
131 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
132 /* .iInterface = DYNAMIC */
135 static struct usb_cdc_header_desc header_desc __initdata = {
136 .bLength = sizeof header_desc,
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
140 .bcdCDC = cpu_to_le16(0x0110),
143 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
144 .bLength = sizeof call_mgmt_descriptor,
145 .bDescriptorType = USB_DT_CS_INTERFACE,
146 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
148 .bmCapabilities = 0x00,
149 .bDataInterface = 0x01,
152 static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
153 .bLength = sizeof acm_descriptor,
154 .bDescriptorType = USB_DT_CS_INTERFACE,
155 .bDescriptorSubType = USB_CDC_ACM_TYPE,
157 .bmCapabilities = 0x00,
160 static struct usb_cdc_union_desc rndis_union_desc __initdata = {
161 .bLength = sizeof(rndis_union_desc),
162 .bDescriptorType = USB_DT_CS_INTERFACE,
163 .bDescriptorSubType = USB_CDC_UNION_TYPE,
164 /* .bMasterInterface0 = DYNAMIC */
165 /* .bSlaveInterface0 = DYNAMIC */
168 /* the data interface has two bulk endpoints */
170 static struct usb_interface_descriptor rndis_data_intf __initdata = {
171 .bLength = sizeof rndis_data_intf,
172 .bDescriptorType = USB_DT_INTERFACE,
174 /* .bInterfaceNumber = DYNAMIC */
175 .bNumEndpoints = 2,
176 .bInterfaceClass = USB_CLASS_CDC_DATA,
177 .bInterfaceSubClass = 0,
178 .bInterfaceProtocol = 0,
179 /* .iInterface = DYNAMIC */
182 /* full speed support: */
184 static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
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 __initdata = {
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 __initdata = {
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[] __initdata = {
211 /* control interface matches ACM, not Ethernet */
212 (struct usb_descriptor_header *) &rndis_control_intf,
213 (struct usb_descriptor_header *) &header_desc,
214 (struct usb_descriptor_header *) &call_mgmt_descriptor,
215 (struct usb_descriptor_header *) &acm_descriptor,
216 (struct usb_descriptor_header *) &rndis_union_desc,
217 (struct usb_descriptor_header *) &fs_notify_desc,
218 /* data interface has no altsetting */
219 (struct usb_descriptor_header *) &rndis_data_intf,
220 (struct usb_descriptor_header *) &fs_in_desc,
221 (struct usb_descriptor_header *) &fs_out_desc,
222 NULL,
225 /* high speed support: */
227 static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
231 .bEndpointAddress = USB_DIR_IN,
232 .bmAttributes = USB_ENDPOINT_XFER_INT,
233 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
234 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
236 static struct usb_endpoint_descriptor hs_in_desc __initdata = {
237 .bLength = USB_DT_ENDPOINT_SIZE,
238 .bDescriptorType = USB_DT_ENDPOINT,
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_XFER_BULK,
242 .wMaxPacketSize = cpu_to_le16(512),
245 static struct usb_endpoint_descriptor hs_out_desc __initdata = {
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
249 .bEndpointAddress = USB_DIR_OUT,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
251 .wMaxPacketSize = cpu_to_le16(512),
254 static struct usb_descriptor_header *eth_hs_function[] __initdata = {
255 /* control interface matches ACM, not Ethernet */
256 (struct usb_descriptor_header *) &rndis_control_intf,
257 (struct usb_descriptor_header *) &header_desc,
258 (struct usb_descriptor_header *) &call_mgmt_descriptor,
259 (struct usb_descriptor_header *) &acm_descriptor,
260 (struct usb_descriptor_header *) &rndis_union_desc,
261 (struct usb_descriptor_header *) &hs_notify_desc,
262 /* data interface has no altsetting */
263 (struct usb_descriptor_header *) &rndis_data_intf,
264 (struct usb_descriptor_header *) &hs_in_desc,
265 (struct usb_descriptor_header *) &hs_out_desc,
266 NULL,
269 /* string descriptors: */
271 static struct usb_string rndis_string_defs[] = {
272 [0].s = "RNDIS Communications Control",
273 [1].s = "RNDIS Ethernet Data",
274 { } /* end of list */
277 static struct usb_gadget_strings rndis_string_table = {
278 .language = 0x0409, /* en-us */
279 .strings = rndis_string_defs,
282 static struct usb_gadget_strings *rndis_strings[] = {
283 &rndis_string_table,
284 NULL,
287 /*-------------------------------------------------------------------------*/
289 static struct sk_buff *rndis_add_header(struct gether *port,
290 struct sk_buff *skb)
292 struct sk_buff *skb2;
294 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
295 if (skb2)
296 rndis_add_hdr(skb2);
298 dev_kfree_skb_any(skb);
299 return skb2;
302 static void rndis_response_available(void *_rndis)
304 struct f_rndis *rndis = _rndis;
305 struct usb_request *req = rndis->notify_req;
306 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
307 __le32 *data = req->buf;
308 int status;
310 if (atomic_inc_return(&rndis->notify_count) != 1)
311 return;
313 /* Send RNDIS RESPONSE_AVAILABLE notification; a
314 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
316 * This is the only notification defined by RNDIS.
318 data[0] = cpu_to_le32(1);
319 data[1] = cpu_to_le32(0);
321 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
322 if (status) {
323 atomic_dec(&rndis->notify_count);
324 DBG(cdev, "notify/0 --> %d\n", status);
328 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
330 struct f_rndis *rndis = req->context;
331 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
332 int status = req->status;
334 /* after TX:
335 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
336 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
338 switch (status) {
339 case -ECONNRESET:
340 case -ESHUTDOWN:
341 /* connection gone */
342 atomic_set(&rndis->notify_count, 0);
343 break;
344 default:
345 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
346 ep->name, status,
347 req->actual, req->length);
348 /* FALLTHROUGH */
349 case 0:
350 if (ep != rndis->notify)
351 break;
353 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
354 * notifications by resending until we're done
356 if (atomic_dec_and_test(&rndis->notify_count))
357 break;
358 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
359 if (status) {
360 atomic_dec(&rndis->notify_count);
361 DBG(cdev, "notify/1 --> %d\n", status);
363 break;
367 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
369 struct f_rndis *rndis = req->context;
370 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
371 int status;
373 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
374 // spin_lock(&dev->lock);
375 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
376 if (status < 0)
377 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
378 status, req->actual, req->length);
379 // spin_unlock(&dev->lock);
382 static int
383 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
385 struct f_rndis *rndis = func_to_rndis(f);
386 struct usb_composite_dev *cdev = f->config->cdev;
387 struct usb_request *req = cdev->req;
388 int value = -EOPNOTSUPP;
389 u16 w_index = le16_to_cpu(ctrl->wIndex);
390 u16 w_value = le16_to_cpu(ctrl->wValue);
391 u16 w_length = le16_to_cpu(ctrl->wLength);
393 /* composite driver infrastructure handles everything except
394 * CDC class messages; interface activation uses set_alt().
396 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
398 /* RNDIS uses the CDC command encapsulation mechanism to implement
399 * an RPC scheme, with much getting/setting of attributes by OID.
401 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
402 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
403 if (w_value || w_index != rndis->ctrl_id)
404 goto invalid;
405 /* read the request; process it later */
406 value = w_length;
407 req->complete = rndis_command_complete;
408 req->context = rndis;
409 /* later, rndis_response_available() sends a notification */
410 break;
412 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
413 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
414 if (w_value || w_index != rndis->ctrl_id)
415 goto invalid;
416 else {
417 u8 *buf;
418 u32 n;
420 /* return the result */
421 buf = rndis_get_next_response(rndis->config, &n);
422 if (buf) {
423 memcpy(req->buf, buf, n);
424 req->complete = rndis_response_complete;
425 rndis_free_response(rndis->config, buf);
426 value = n;
428 /* else stalls ... spec says to avoid that */
430 break;
432 default:
433 invalid:
434 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
435 ctrl->bRequestType, ctrl->bRequest,
436 w_value, w_index, w_length);
439 /* respond with data transfer or status phase? */
440 if (value >= 0) {
441 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
442 ctrl->bRequestType, ctrl->bRequest,
443 w_value, w_index, w_length);
444 req->zero = (value < w_length);
445 req->length = value;
446 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
447 if (value < 0)
448 ERROR(cdev, "rndis response on err %d\n", value);
451 /* device either stalls (value < 0) or reports success */
452 return value;
456 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
458 struct f_rndis *rndis = func_to_rndis(f);
459 struct usb_composite_dev *cdev = f->config->cdev;
461 /* we know alt == 0 */
463 if (intf == rndis->ctrl_id) {
464 if (rndis->notify->driver_data) {
465 VDBG(cdev, "reset rndis control %d\n", intf);
466 usb_ep_disable(rndis->notify);
467 } else {
468 VDBG(cdev, "init rndis ctrl %d\n", intf);
469 rndis->notify_desc = ep_choose(cdev->gadget,
470 rndis->hs.notify,
471 rndis->fs.notify);
473 usb_ep_enable(rndis->notify, rndis->notify_desc);
474 rndis->notify->driver_data = rndis;
476 } else if (intf == rndis->data_id) {
477 struct net_device *net;
479 if (rndis->port.in_ep->driver_data) {
480 DBG(cdev, "reset rndis\n");
481 gether_disconnect(&rndis->port);
484 if (!rndis->port.in) {
485 DBG(cdev, "init rndis\n");
486 rndis->port.in = ep_choose(cdev->gadget,
487 rndis->hs.in, rndis->fs.in);
488 rndis->port.out = ep_choose(cdev->gadget,
489 rndis->hs.out, rndis->fs.out);
492 /* Avoid ZLPs; they can be troublesome. */
493 rndis->port.is_zlp_ok = false;
495 /* RNDIS should be in the "RNDIS uninitialized" state,
496 * either never activated or after rndis_uninit().
498 * We don't want data to flow here until a nonzero packet
499 * filter is set, at which point it enters "RNDIS data
500 * initialized" state ... but we do want the endpoints
501 * to be activated. It's a strange little state.
503 * REVISIT the RNDIS gadget code has done this wrong for a
504 * very long time. We need another call to the link layer
505 * code -- gether_updown(...bool) maybe -- to do it right.
507 rndis->port.cdc_filter = 0;
509 DBG(cdev, "RNDIS RX/TX early activation ... \n");
510 net = gether_connect(&rndis->port);
511 if (IS_ERR(net))
512 return PTR_ERR(net);
514 rndis_set_param_dev(rndis->config, net,
515 &rndis->port.cdc_filter);
516 } else
517 goto fail;
519 return 0;
520 fail:
521 return -EINVAL;
524 static void rndis_disable(struct usb_function *f)
526 struct f_rndis *rndis = func_to_rndis(f);
527 struct usb_composite_dev *cdev = f->config->cdev;
529 if (!rndis->notify->driver_data)
530 return;
532 DBG(cdev, "rndis deactivated\n");
534 rndis_uninit(rndis->config);
535 gether_disconnect(&rndis->port);
537 usb_ep_disable(rndis->notify);
538 rndis->notify->driver_data = NULL;
541 /*-------------------------------------------------------------------------*/
544 * This isn't quite the same mechanism as CDC Ethernet, since the
545 * notification scheme passes less data, but the same set of link
546 * states must be tested. A key difference is that altsettings are
547 * not used to tell whether the link should send packets or not.
550 static void rndis_open(struct gether *geth)
552 struct f_rndis *rndis = func_to_rndis(&geth->func);
553 struct usb_composite_dev *cdev = geth->func.config->cdev;
555 DBG(cdev, "%s\n", __func__);
557 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
558 bitrate(cdev->gadget) / 100);
559 rndis_signal_connect(rndis->config);
562 static void rndis_close(struct gether *geth)
564 struct f_rndis *rndis = func_to_rndis(&geth->func);
566 DBG(geth->func.config->cdev, "%s\n", __func__);
568 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
569 rndis_signal_disconnect(rndis->config);
572 /*-------------------------------------------------------------------------*/
574 /* ethernet function driver setup/binding */
576 static int __init
577 rndis_bind(struct usb_configuration *c, struct usb_function *f)
579 struct usb_composite_dev *cdev = c->cdev;
580 struct f_rndis *rndis = func_to_rndis(f);
581 int status;
582 struct usb_ep *ep;
584 /* allocate instance-specific interface IDs */
585 status = usb_interface_id(c, f);
586 if (status < 0)
587 goto fail;
588 rndis->ctrl_id = status;
590 rndis_control_intf.bInterfaceNumber = status;
591 rndis_union_desc.bMasterInterface0 = status;
593 status = usb_interface_id(c, f);
594 if (status < 0)
595 goto fail;
596 rndis->data_id = status;
598 rndis_data_intf.bInterfaceNumber = status;
599 rndis_union_desc.bSlaveInterface0 = status;
601 status = -ENODEV;
603 /* allocate instance-specific endpoints */
604 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
605 if (!ep)
606 goto fail;
607 rndis->port.in_ep = ep;
608 ep->driver_data = cdev; /* claim */
610 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
611 if (!ep)
612 goto fail;
613 rndis->port.out_ep = ep;
614 ep->driver_data = cdev; /* claim */
616 /* NOTE: a status/notification endpoint is, strictly speaking,
617 * optional. We don't treat it that way though! It's simpler,
618 * and some newer profiles don't treat it as optional.
620 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
621 if (!ep)
622 goto fail;
623 rndis->notify = ep;
624 ep->driver_data = cdev; /* claim */
626 status = -ENOMEM;
628 /* allocate notification request and buffer */
629 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
630 if (!rndis->notify_req)
631 goto fail;
632 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
633 if (!rndis->notify_req->buf)
634 goto fail;
635 rndis->notify_req->length = STATUS_BYTECOUNT;
636 rndis->notify_req->context = rndis;
637 rndis->notify_req->complete = rndis_response_complete;
639 /* copy descriptors, and track endpoint copies */
640 f->descriptors = usb_copy_descriptors(eth_fs_function);
641 if (!f->descriptors)
642 goto fail;
644 rndis->fs.in = usb_find_endpoint(eth_fs_function,
645 f->descriptors, &fs_in_desc);
646 rndis->fs.out = usb_find_endpoint(eth_fs_function,
647 f->descriptors, &fs_out_desc);
648 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
649 f->descriptors, &fs_notify_desc);
651 /* support all relevant hardware speeds... we expect that when
652 * hardware is dual speed, all bulk-capable endpoints work at
653 * both speeds
655 if (gadget_is_dualspeed(c->cdev->gadget)) {
656 hs_in_desc.bEndpointAddress =
657 fs_in_desc.bEndpointAddress;
658 hs_out_desc.bEndpointAddress =
659 fs_out_desc.bEndpointAddress;
660 hs_notify_desc.bEndpointAddress =
661 fs_notify_desc.bEndpointAddress;
663 /* copy descriptors, and track endpoint copies */
664 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
666 if (!f->hs_descriptors)
667 goto fail;
669 rndis->hs.in = usb_find_endpoint(eth_hs_function,
670 f->hs_descriptors, &hs_in_desc);
671 rndis->hs.out = usb_find_endpoint(eth_hs_function,
672 f->hs_descriptors, &hs_out_desc);
673 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
674 f->hs_descriptors, &hs_notify_desc);
677 rndis->port.open = rndis_open;
678 rndis->port.close = rndis_close;
680 status = rndis_register(rndis_response_available, rndis);
681 if (status < 0)
682 goto fail;
683 rndis->config = status;
685 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
686 rndis_set_host_mac(rndis->config, rndis->ethaddr);
688 #if 0
689 // FIXME
690 if (rndis_set_param_vendor(rndis->config, vendorID,
691 manufacturer))
692 goto fail0;
693 #endif
695 /* NOTE: all that is done without knowing or caring about
696 * the network link ... which is unavailable to this code
697 * until we're activated via set_alt().
700 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
701 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
702 rndis->port.in_ep->name, rndis->port.out_ep->name,
703 rndis->notify->name);
704 return 0;
706 fail:
707 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
708 usb_free_descriptors(f->hs_descriptors);
709 if (f->descriptors)
710 usb_free_descriptors(f->descriptors);
712 if (rndis->notify_req) {
713 kfree(rndis->notify_req->buf);
714 usb_ep_free_request(rndis->notify, rndis->notify_req);
717 /* we might as well release our claims on endpoints */
718 if (rndis->notify)
719 rndis->notify->driver_data = NULL;
720 if (rndis->port.out)
721 rndis->port.out_ep->driver_data = NULL;
722 if (rndis->port.in)
723 rndis->port.in_ep->driver_data = NULL;
725 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
727 return status;
730 static void
731 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
733 struct f_rndis *rndis = func_to_rndis(f);
735 rndis_deregister(rndis->config);
736 rndis_exit();
738 if (gadget_is_dualspeed(c->cdev->gadget))
739 usb_free_descriptors(f->hs_descriptors);
740 usb_free_descriptors(f->descriptors);
742 kfree(rndis->notify_req->buf);
743 usb_ep_free_request(rndis->notify, rndis->notify_req);
745 kfree(rndis);
748 /* Some controllers can't support RNDIS ... */
749 static inline bool can_support_rndis(struct usb_configuration *c)
751 /* only two endpoints on sa1100 */
752 if (gadget_is_sa1100(c->cdev->gadget))
753 return false;
755 /* everything else is *presumably* fine */
756 return true;
760 * rndis_bind_config - add RNDIS network link to a configuration
761 * @c: the configuration to support the network link
762 * @ethaddr: a buffer in which the ethernet address of the host side
763 * side of the link was recorded
764 * Context: single threaded during gadget setup
766 * Returns zero on success, else negative errno.
768 * Caller must have called @gether_setup(). Caller is also responsible
769 * for calling @gether_cleanup() before module unload.
771 int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
773 struct f_rndis *rndis;
774 int status;
776 if (!can_support_rndis(c) || !ethaddr)
777 return -EINVAL;
779 /* maybe allocate device-global string IDs */
780 if (rndis_string_defs[0].id == 0) {
782 /* ... and setup RNDIS itself */
783 status = rndis_init();
784 if (status < 0)
785 return status;
787 /* control interface label */
788 status = usb_string_id(c->cdev);
789 if (status < 0)
790 return status;
791 rndis_string_defs[0].id = status;
792 rndis_control_intf.iInterface = status;
794 /* data interface label */
795 status = usb_string_id(c->cdev);
796 if (status < 0)
797 return status;
798 rndis_string_defs[1].id = status;
799 rndis_data_intf.iInterface = status;
802 /* allocate and initialize one new instance */
803 status = -ENOMEM;
804 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
805 if (!rndis)
806 goto fail;
808 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
810 /* RNDIS activates when the host changes this filter */
811 rndis->port.cdc_filter = 0;
813 /* RNDIS has special (and complex) framing */
814 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
815 rndis->port.wrap = rndis_add_header;
816 rndis->port.unwrap = rndis_rm_hdr;
818 rndis->port.func.name = "rndis";
819 rndis->port.func.strings = rndis_strings;
820 /* descriptors are per-instance copies */
821 rndis->port.func.bind = rndis_bind;
822 rndis->port.func.unbind = rndis_unbind;
823 rndis->port.func.set_alt = rndis_set_alt;
824 rndis->port.func.setup = rndis_setup;
825 rndis->port.func.disable = rndis_disable;
827 status = usb_add_function(c, &rndis->port.func);
828 if (status) {
829 kfree(rndis);
830 fail:
831 rndis_exit();
833 return status;