Merge branch 'for-linus' of git://neil.brown.name/md
[deliverable/linux.git] / drivers / usb / gadget / f_rndis.c
CommitLineData
45fe3b8e
DB
1/*
2 * f_rndis.c -- RNDIS link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 *
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.
12 *
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.
17 *
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
21 */
22
23/* #define VERBOSE_DEBUG */
24
25#include <linux/kernel.h>
26#include <linux/device.h>
27#include <linux/etherdevice.h>
28
29#include <asm/atomic.h>
30
31#include "u_ether.h"
32#include "rndis.h"
33
34
35/*
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.
40 *
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.
44 *
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.
52 *
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".
58 *
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.
64 *
65 * Omissions from the RNDIS 1.0 specification include:
66 *
67 * - Power management ... references data that's scattered around lots
68 * of other documentation, which is incorrect/incomplete there too.
69 *
70 * - There are various undocumented protocol requirements, like the need
71 * to send garbage in some control-OUT messages.
72 *
73 * - MS-Windows drivers sometimes emit undocumented requests.
74 */
75
76struct rndis_ep_descs {
77 struct usb_endpoint_descriptor *in;
78 struct usb_endpoint_descriptor *out;
79 struct usb_endpoint_descriptor *notify;
80};
81
82struct f_rndis {
83 struct gether port;
84 u8 ctrl_id, data_id;
85 u8 ethaddr[ETH_ALEN];
86 int config;
87
45fe3b8e 88 struct rndis_ep_descs fs;
45fe3b8e
DB
89 struct rndis_ep_descs hs;
90
91 struct usb_ep *notify;
92 struct usb_endpoint_descriptor *notify_desc;
93 struct usb_request *notify_req;
94 atomic_t notify_count;
95};
96
97static inline struct f_rndis *func_to_rndis(struct usb_function *f)
98{
99 return container_of(f, struct f_rndis, port.func);
100}
101
102/* peak (theoretical) bulk transfer rate in bits-per-second */
103static unsigned int bitrate(struct usb_gadget *g)
104{
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;
109}
110
111/*-------------------------------------------------------------------------*/
112
113/*
114 */
115
116#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
117#define STATUS_BYTECOUNT 8 /* 8 bytes data */
118
119
120/* interface descriptor: */
121
122static struct usb_interface_descriptor rndis_control_intf __initdata = {
123 .bLength = sizeof rndis_control_intf,
124 .bDescriptorType = USB_DT_INTERFACE,
125
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 */
133};
134
135static 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,
139
551509d2 140 .bcdCDC = cpu_to_le16(0x0110),
45fe3b8e
DB
141};
142
143static 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,
147
148 .bmCapabilities = 0x00,
149 .bDataInterface = 0x01,
150};
151
152static 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,
156
157 .bmCapabilities = 0x00,
158};
159
160static 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 */
166};
167
168/* the data interface has two bulk endpoints */
169
170static struct usb_interface_descriptor rndis_data_intf __initdata = {
171 .bLength = sizeof rndis_data_intf,
172 .bDescriptorType = USB_DT_INTERFACE,
173
174 /* .bInterfaceNumber = DYNAMIC */
45fe3b8e
DB
175 .bNumEndpoints = 2,
176 .bInterfaceClass = USB_CLASS_CDC_DATA,
177 .bInterfaceSubClass = 0,
178 .bInterfaceProtocol = 0,
179 /* .iInterface = DYNAMIC */
180};
181
182/* full speed support: */
183
184static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_INT,
551509d2 190 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
45fe3b8e
DB
191 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
192};
193
194static struct usb_endpoint_descriptor fs_in_desc __initdata = {
195 .bLength = USB_DT_ENDPOINT_SIZE,
196 .bDescriptorType = USB_DT_ENDPOINT,
197
198 .bEndpointAddress = USB_DIR_IN,
199 .bmAttributes = USB_ENDPOINT_XFER_BULK,
200};
201
202static struct usb_endpoint_descriptor fs_out_desc __initdata = {
203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
205
206 .bEndpointAddress = USB_DIR_OUT,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
208};
209
210static 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,
223};
224
225/* high speed support: */
226
227static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
230
231 .bEndpointAddress = USB_DIR_IN,
232 .bmAttributes = USB_ENDPOINT_XFER_INT,
551509d2 233 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
45fe3b8e
DB
234 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
235};
236static struct usb_endpoint_descriptor hs_in_desc __initdata = {
237 .bLength = USB_DT_ENDPOINT_SIZE,
238 .bDescriptorType = USB_DT_ENDPOINT,
239
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 242 .wMaxPacketSize = cpu_to_le16(512),
45fe3b8e
DB
243};
244
245static struct usb_endpoint_descriptor hs_out_desc __initdata = {
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_OUT,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
551509d2 251 .wMaxPacketSize = cpu_to_le16(512),
45fe3b8e
DB
252};
253
254static 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,
267};
268
269/* string descriptors: */
270
271static struct usb_string rndis_string_defs[] = {
272 [0].s = "RNDIS Communications Control",
273 [1].s = "RNDIS Ethernet Data",
274 { } /* end of list */
275};
276
277static struct usb_gadget_strings rndis_string_table = {
278 .language = 0x0409, /* en-us */
279 .strings = rndis_string_defs,
280};
281
282static struct usb_gadget_strings *rndis_strings[] = {
283 &rndis_string_table,
284 NULL,
285};
286
287/*-------------------------------------------------------------------------*/
288
289static struct sk_buff *rndis_add_header(struct sk_buff *skb)
290{
291 skb = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
292 if (skb)
293 rndis_add_hdr(skb);
294 return skb;
295}
296
297static void rndis_response_available(void *_rndis)
298{
299 struct f_rndis *rndis = _rndis;
300 struct usb_request *req = rndis->notify_req;
301 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
302 __le32 *data = req->buf;
303 int status;
304
ff349505 305 if (atomic_inc_return(&rndis->notify_count) != 1)
45fe3b8e
DB
306 return;
307
308 /* Send RNDIS RESPONSE_AVAILABLE notification; a
309 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
310 *
311 * This is the only notification defined by RNDIS.
312 */
313 data[0] = cpu_to_le32(1);
314 data[1] = cpu_to_le32(0);
315
316 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
317 if (status) {
318 atomic_dec(&rndis->notify_count);
319 DBG(cdev, "notify/0 --> %d\n", status);
320 }
321}
322
323static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
324{
325 struct f_rndis *rndis = req->context;
326 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
327 int status = req->status;
328
329 /* after TX:
330 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
331 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
332 */
333 switch (status) {
334 case -ECONNRESET:
335 case -ESHUTDOWN:
336 /* connection gone */
337 atomic_set(&rndis->notify_count, 0);
338 break;
339 default:
340 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
341 ep->name, status,
342 req->actual, req->length);
343 /* FALLTHROUGH */
344 case 0:
345 if (ep != rndis->notify)
346 break;
347
348 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
349 * notifications by resending until we're done
350 */
351 if (atomic_dec_and_test(&rndis->notify_count))
352 break;
353 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
354 if (status) {
355 atomic_dec(&rndis->notify_count);
356 DBG(cdev, "notify/1 --> %d\n", status);
357 }
358 break;
359 }
360}
361
362static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
363{
364 struct f_rndis *rndis = req->context;
365 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
366 int status;
367
368 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
369// spin_lock(&dev->lock);
370 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
371 if (status < 0)
372 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
373 status, req->actual, req->length);
374// spin_unlock(&dev->lock);
375}
376
377static int
378rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
379{
380 struct f_rndis *rndis = func_to_rndis(f);
381 struct usb_composite_dev *cdev = f->config->cdev;
382 struct usb_request *req = cdev->req;
383 int value = -EOPNOTSUPP;
384 u16 w_index = le16_to_cpu(ctrl->wIndex);
385 u16 w_value = le16_to_cpu(ctrl->wValue);
386 u16 w_length = le16_to_cpu(ctrl->wLength);
387
388 /* composite driver infrastructure handles everything except
389 * CDC class messages; interface activation uses set_alt().
390 */
391 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
392
393 /* RNDIS uses the CDC command encapsulation mechanism to implement
394 * an RPC scheme, with much getting/setting of attributes by OID.
395 */
396 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
397 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
398 if (w_length > req->length || w_value
399 || w_index != rndis->ctrl_id)
400 goto invalid;
401 /* read the request; process it later */
402 value = w_length;
403 req->complete = rndis_command_complete;
404 req->context = rndis;
405 /* later, rndis_response_available() sends a notification */
406 break;
407
408 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
409 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
410 if (w_value || w_index != rndis->ctrl_id)
411 goto invalid;
412 else {
413 u8 *buf;
414 u32 n;
415
416 /* return the result */
417 buf = rndis_get_next_response(rndis->config, &n);
418 if (buf) {
419 memcpy(req->buf, buf, n);
420 req->complete = rndis_response_complete;
421 rndis_free_response(rndis->config, buf);
422 value = n;
423 }
424 /* else stalls ... spec says to avoid that */
425 }
426 break;
427
428 default:
429invalid:
430 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
431 ctrl->bRequestType, ctrl->bRequest,
432 w_value, w_index, w_length);
433 }
434
435 /* respond with data transfer or status phase? */
436 if (value >= 0) {
437 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
438 ctrl->bRequestType, ctrl->bRequest,
439 w_value, w_index, w_length);
090b9011 440 req->zero = (value < w_length);
45fe3b8e
DB
441 req->length = value;
442 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
443 if (value < 0)
444 ERROR(cdev, "rndis response on err %d\n", value);
445 }
446
447 /* device either stalls (value < 0) or reports success */
448 return value;
449}
450
451
452static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
453{
454 struct f_rndis *rndis = func_to_rndis(f);
455 struct usb_composite_dev *cdev = f->config->cdev;
456
457 /* we know alt == 0 */
458
459 if (intf == rndis->ctrl_id) {
460 if (rndis->notify->driver_data) {
461 VDBG(cdev, "reset rndis control %d\n", intf);
462 usb_ep_disable(rndis->notify);
463 } else {
464 VDBG(cdev, "init rndis ctrl %d\n", intf);
465 rndis->notify_desc = ep_choose(cdev->gadget,
466 rndis->hs.notify,
467 rndis->fs.notify);
468 }
469 usb_ep_enable(rndis->notify, rndis->notify_desc);
470 rndis->notify->driver_data = rndis;
471
472 } else if (intf == rndis->data_id) {
473 struct net_device *net;
474
475 if (rndis->port.in_ep->driver_data) {
476 DBG(cdev, "reset rndis\n");
477 gether_disconnect(&rndis->port);
830d1b18
MM
478 }
479
480 if (!rndis->port.in) {
45fe3b8e
DB
481 DBG(cdev, "init rndis\n");
482 rndis->port.in = ep_choose(cdev->gadget,
483 rndis->hs.in, rndis->fs.in);
484 rndis->port.out = ep_choose(cdev->gadget,
485 rndis->hs.out, rndis->fs.out);
486 }
487
488 /* Avoid ZLPs; they can be troublesome. */
489 rndis->port.is_zlp_ok = false;
490
491 /* RNDIS should be in the "RNDIS uninitialized" state,
492 * either never activated or after rndis_uninit().
493 *
494 * We don't want data to flow here until a nonzero packet
495 * filter is set, at which point it enters "RNDIS data
496 * initialized" state ... but we do want the endpoints
497 * to be activated. It's a strange little state.
498 *
499 * REVISIT the RNDIS gadget code has done this wrong for a
500 * very long time. We need another call to the link layer
501 * code -- gether_updown(...bool) maybe -- to do it right.
502 */
503 rndis->port.cdc_filter = 0;
504
505 DBG(cdev, "RNDIS RX/TX early activation ... \n");
506 net = gether_connect(&rndis->port);
507 if (IS_ERR(net))
508 return PTR_ERR(net);
509
510 rndis_set_param_dev(rndis->config, net,
511 &rndis->port.cdc_filter);
512 } else
513 goto fail;
514
515 return 0;
516fail:
517 return -EINVAL;
518}
519
520static void rndis_disable(struct usb_function *f)
521{
522 struct f_rndis *rndis = func_to_rndis(f);
523 struct usb_composite_dev *cdev = f->config->cdev;
524
525 if (!rndis->notify->driver_data)
526 return;
527
528 DBG(cdev, "rndis deactivated\n");
529
530 rndis_uninit(rndis->config);
531 gether_disconnect(&rndis->port);
532
533 usb_ep_disable(rndis->notify);
534 rndis->notify->driver_data = NULL;
535}
536
537/*-------------------------------------------------------------------------*/
538
539/*
540 * This isn't quite the same mechanism as CDC Ethernet, since the
541 * notification scheme passes less data, but the same set of link
542 * states must be tested. A key difference is that altsettings are
543 * not used to tell whether the link should send packets or not.
544 */
545
546static void rndis_open(struct gether *geth)
547{
548 struct f_rndis *rndis = func_to_rndis(&geth->func);
549 struct usb_composite_dev *cdev = geth->func.config->cdev;
550
551 DBG(cdev, "%s\n", __func__);
552
553 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
554 bitrate(cdev->gadget) / 100);
555 rndis_signal_connect(rndis->config);
556}
557
558static void rndis_close(struct gether *geth)
559{
560 struct f_rndis *rndis = func_to_rndis(&geth->func);
561
562 DBG(geth->func.config->cdev, "%s\n", __func__);
563
564 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
565 rndis_signal_disconnect(rndis->config);
566}
567
568/*-------------------------------------------------------------------------*/
569
570/* ethernet function driver setup/binding */
571
572static int __init
573rndis_bind(struct usb_configuration *c, struct usb_function *f)
574{
575 struct usb_composite_dev *cdev = c->cdev;
576 struct f_rndis *rndis = func_to_rndis(f);
577 int status;
578 struct usb_ep *ep;
579
580 /* allocate instance-specific interface IDs */
581 status = usb_interface_id(c, f);
582 if (status < 0)
583 goto fail;
584 rndis->ctrl_id = status;
585
586 rndis_control_intf.bInterfaceNumber = status;
587 rndis_union_desc.bMasterInterface0 = status;
588
589 status = usb_interface_id(c, f);
590 if (status < 0)
591 goto fail;
592 rndis->data_id = status;
593
594 rndis_data_intf.bInterfaceNumber = status;
595 rndis_union_desc.bSlaveInterface0 = status;
596
597 status = -ENODEV;
598
599 /* allocate instance-specific endpoints */
600 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
601 if (!ep)
602 goto fail;
603 rndis->port.in_ep = ep;
604 ep->driver_data = cdev; /* claim */
605
606 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
607 if (!ep)
608 goto fail;
609 rndis->port.out_ep = ep;
610 ep->driver_data = cdev; /* claim */
611
612 /* NOTE: a status/notification endpoint is, strictly speaking,
613 * optional. We don't treat it that way though! It's simpler,
614 * and some newer profiles don't treat it as optional.
615 */
616 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
617 if (!ep)
618 goto fail;
619 rndis->notify = ep;
620 ep->driver_data = cdev; /* claim */
621
622 status = -ENOMEM;
623
624 /* allocate notification request and buffer */
625 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
626 if (!rndis->notify_req)
627 goto fail;
628 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
629 if (!rndis->notify_req->buf)
630 goto fail;
631 rndis->notify_req->length = STATUS_BYTECOUNT;
632 rndis->notify_req->context = rndis;
633 rndis->notify_req->complete = rndis_response_complete;
634
635 /* copy descriptors, and track endpoint copies */
636 f->descriptors = usb_copy_descriptors(eth_fs_function);
637 if (!f->descriptors)
638 goto fail;
639
640 rndis->fs.in = usb_find_endpoint(eth_fs_function,
641 f->descriptors, &fs_in_desc);
642 rndis->fs.out = usb_find_endpoint(eth_fs_function,
643 f->descriptors, &fs_out_desc);
644 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
645 f->descriptors, &fs_notify_desc);
646
647 /* support all relevant hardware speeds... we expect that when
648 * hardware is dual speed, all bulk-capable endpoints work at
649 * both speeds
650 */
651 if (gadget_is_dualspeed(c->cdev->gadget)) {
652 hs_in_desc.bEndpointAddress =
653 fs_in_desc.bEndpointAddress;
654 hs_out_desc.bEndpointAddress =
655 fs_out_desc.bEndpointAddress;
7c124149
DB
656 hs_notify_desc.bEndpointAddress =
657 fs_notify_desc.bEndpointAddress;
45fe3b8e
DB
658
659 /* copy descriptors, and track endpoint copies */
660 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
661
662 if (!f->hs_descriptors)
663 goto fail;
664
665 rndis->hs.in = usb_find_endpoint(eth_hs_function,
666 f->hs_descriptors, &hs_in_desc);
667 rndis->hs.out = usb_find_endpoint(eth_hs_function,
668 f->hs_descriptors, &hs_out_desc);
7c124149
DB
669 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
670 f->hs_descriptors, &hs_notify_desc);
45fe3b8e
DB
671 }
672
673 rndis->port.open = rndis_open;
674 rndis->port.close = rndis_close;
675
676 status = rndis_register(rndis_response_available, rndis);
677 if (status < 0)
678 goto fail;
679 rndis->config = status;
680
681 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
682 rndis_set_host_mac(rndis->config, rndis->ethaddr);
683
684#if 0
685// FIXME
686 if (rndis_set_param_vendor(rndis->config, vendorID,
687 manufacturer))
688 goto fail0;
689#endif
690
691 /* NOTE: all that is done without knowing or caring about
692 * the network link ... which is unavailable to this code
693 * until we're activated via set_alt().
694 */
695
696 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
697 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
698 rndis->port.in_ep->name, rndis->port.out_ep->name,
699 rndis->notify->name);
700 return 0;
701
702fail:
703 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
704 usb_free_descriptors(f->hs_descriptors);
705 if (f->descriptors)
706 usb_free_descriptors(f->descriptors);
707
708 if (rndis->notify_req) {
709 kfree(rndis->notify_req->buf);
710 usb_ep_free_request(rndis->notify, rndis->notify_req);
711 }
712
713 /* we might as well release our claims on endpoints */
714 if (rndis->notify)
715 rndis->notify->driver_data = NULL;
716 if (rndis->port.out)
717 rndis->port.out_ep->driver_data = NULL;
718 if (rndis->port.in)
719 rndis->port.in_ep->driver_data = NULL;
720
721 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
722
723 return status;
724}
725
726static void
727rndis_unbind(struct usb_configuration *c, struct usb_function *f)
728{
729 struct f_rndis *rndis = func_to_rndis(f);
730
731 rndis_deregister(rndis->config);
732 rndis_exit();
733
734 if (gadget_is_dualspeed(c->cdev->gadget))
735 usb_free_descriptors(f->hs_descriptors);
736 usb_free_descriptors(f->descriptors);
737
738 kfree(rndis->notify_req->buf);
739 usb_ep_free_request(rndis->notify, rndis->notify_req);
740
741 kfree(rndis);
742}
743
744/* Some controllers can't support RNDIS ... */
745static inline bool can_support_rndis(struct usb_configuration *c)
746{
747 /* only two endpoints on sa1100 */
748 if (gadget_is_sa1100(c->cdev->gadget))
749 return false;
750
751 /* everything else is *presumably* fine */
752 return true;
753}
754
755/**
756 * rndis_bind_config - add RNDIS network link to a configuration
757 * @c: the configuration to support the network link
758 * @ethaddr: a buffer in which the ethernet address of the host side
759 * side of the link was recorded
760 * Context: single threaded during gadget setup
761 *
762 * Returns zero on success, else negative errno.
763 *
764 * Caller must have called @gether_setup(). Caller is also responsible
765 * for calling @gether_cleanup() before module unload.
766 */
767int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
768{
769 struct f_rndis *rndis;
770 int status;
771
772 if (!can_support_rndis(c) || !ethaddr)
773 return -EINVAL;
774
775 /* maybe allocate device-global string IDs */
776 if (rndis_string_defs[0].id == 0) {
777
778 /* ... and setup RNDIS itself */
779 status = rndis_init();
780 if (status < 0)
781 return status;
782
783 /* control interface label */
784 status = usb_string_id(c->cdev);
785 if (status < 0)
786 return status;
787 rndis_string_defs[0].id = status;
788 rndis_control_intf.iInterface = status;
789
790 /* data interface label */
791 status = usb_string_id(c->cdev);
792 if (status < 0)
793 return status;
794 rndis_string_defs[1].id = status;
795 rndis_data_intf.iInterface = status;
796 }
797
798 /* allocate and initialize one new instance */
799 status = -ENOMEM;
800 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
801 if (!rndis)
802 goto fail;
803
804 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
805
806 /* RNDIS activates when the host changes this filter */
807 rndis->port.cdc_filter = 0;
808
809 /* RNDIS has special (and complex) framing */
810 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
811 rndis->port.wrap = rndis_add_header;
812 rndis->port.unwrap = rndis_rm_hdr;
813
814 rndis->port.func.name = "rndis";
815 rndis->port.func.strings = rndis_strings;
816 /* descriptors are per-instance copies */
817 rndis->port.func.bind = rndis_bind;
818 rndis->port.func.unbind = rndis_unbind;
819 rndis->port.func.set_alt = rndis_set_alt;
820 rndis->port.func.setup = rndis_setup;
821 rndis->port.func.disable = rndis_disable;
822
823 status = usb_add_function(c, &rndis->port.func);
824 if (status) {
825 kfree(rndis);
826fail:
827 rndis_exit();
828 }
829 return status;
830}
This page took 0.180318 seconds and 5 git commands to generate.