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 * Copyright (C) 2009 Samsung Electronics
8 * Author: Michal Nazarewicz (mina86@mina86.com)
9 *
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.
14 */
15
16 /* #define VERBOSE_DEBUG */
17
18 #include <linux/slab.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/etherdevice.h>
22
23 #include <linux/atomic.h>
24
25 #include "u_ether.h"
26 #include "rndis.h"
27
28
29 /*
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.
34 *
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.
38 *
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.
46 *
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".
52 *
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.
58 *
59 * Omissions from the RNDIS 1.0 specification include:
60 *
61 * - Power management ... references data that's scattered around lots
62 * of other documentation, which is incorrect/incomplete there too.
63 *
64 * - There are various undocumented protocol requirements, like the need
65 * to send garbage in some control-OUT messages.
66 *
67 * - MS-Windows drivers sometimes emit undocumented requests.
68 */
69
70 struct f_rndis {
71 struct gether port;
72 u8 ctrl_id, data_id;
73 u8 ethaddr[ETH_ALEN];
74 int config;
75
76 struct usb_ep *notify;
77 struct usb_request *notify_req;
78 atomic_t notify_count;
79 };
80
func_to_rndis(struct usb_function * f)81 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
82 {
83 return container_of(f, struct f_rndis, port.func);
84 }
85
86 /* peak (theoretical) bulk transfer rate in bits-per-second */
bitrate(struct usb_gadget * g)87 static unsigned int bitrate(struct usb_gadget *g)
88 {
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;
95 }
96
97 /*-------------------------------------------------------------------------*/
98
99 /*
100 */
101
102 #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
103 #define STATUS_BYTECOUNT 8 /* 8 bytes data */
104
105
106 /* interface descriptor: */
107
108 static struct usb_interface_descriptor rndis_control_intf = {
109 .bLength = sizeof rndis_control_intf,
110 .bDescriptorType = USB_DT_INTERFACE,
111
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 */
119 };
120
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,
125
126 .bcdCDC = cpu_to_le16(0x0110),
127 };
128
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,
133
134 .bmCapabilities = 0x00,
135 .bDataInterface = 0x01,
136 };
137
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,
142
143 .bmCapabilities = 0x00,
144 };
145
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 */
152 };
153
154 /* the data interface has two bulk endpoints */
155
156 static struct usb_interface_descriptor rndis_data_intf = {
157 .bLength = sizeof rndis_data_intf,
158 .bDescriptorType = USB_DT_INTERFACE,
159
160 /* .bInterfaceNumber = DYNAMIC */
161 .bNumEndpoints = 2,
162 .bInterfaceClass = USB_CLASS_CDC_DATA,
163 .bInterfaceSubClass = 0,
164 .bInterfaceProtocol = 0,
165 /* .iInterface = DYNAMIC */
166 };
167
168
169 static struct usb_interface_assoc_descriptor
170 rndis_iad_descriptor = {
171 .bLength = sizeof rndis_iad_descriptor,
172 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
173
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 */
180 };
181
182 /* full speed support: */
183
184 static struct usb_endpoint_descriptor fs_notify_desc = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187
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,
192 };
193
194 static struct usb_endpoint_descriptor fs_in_desc = {
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
202 static struct usb_endpoint_descriptor fs_out_desc = {
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
210 static struct usb_descriptor_header *eth_fs_function[] = {
211 (struct usb_descriptor_header *) &rndis_iad_descriptor,
212
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,
220
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,
226 };
227
228 /* high speed support: */
229
230 static struct usb_endpoint_descriptor hs_notify_desc = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233
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,
238 };
239
240 static struct usb_endpoint_descriptor hs_in_desc = {
241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
243
244 .bEndpointAddress = USB_DIR_IN,
245 .bmAttributes = USB_ENDPOINT_XFER_BULK,
246 .wMaxPacketSize = cpu_to_le16(512),
247 };
248
249 static struct usb_endpoint_descriptor hs_out_desc = {
250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
252
253 .bEndpointAddress = USB_DIR_OUT,
254 .bmAttributes = USB_ENDPOINT_XFER_BULK,
255 .wMaxPacketSize = cpu_to_le16(512),
256 };
257
258 static struct usb_descriptor_header *eth_hs_function[] = {
259 (struct usb_descriptor_header *) &rndis_iad_descriptor,
260
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,
268
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,
274 };
275
276 /* super speed support: */
277
278 static struct usb_endpoint_descriptor ss_notify_desc = {
279 .bLength = USB_DT_ENDPOINT_SIZE,
280 .bDescriptorType = USB_DT_ENDPOINT,
281
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,
286 };
287
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,
291
292 /* the following 3 values can be tweaked if necessary */
293 /* .bMaxBurst = 0, */
294 /* .bmAttributes = 0, */
295 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
296 };
297
298 static struct usb_endpoint_descriptor ss_in_desc = {
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
301
302 .bEndpointAddress = USB_DIR_IN,
303 .bmAttributes = USB_ENDPOINT_XFER_BULK,
304 .wMaxPacketSize = cpu_to_le16(1024),
305 };
306
307 static struct usb_endpoint_descriptor ss_out_desc = {
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
310
311 .bEndpointAddress = USB_DIR_OUT,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 .wMaxPacketSize = cpu_to_le16(1024),
314 };
315
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,
319
320 /* the following 2 values can be tweaked if necessary */
321 /* .bMaxBurst = 0, */
322 /* .bmAttributes = 0, */
323 };
324
325 static struct usb_descriptor_header *eth_ss_function[] = {
326 (struct usb_descriptor_header *) &rndis_iad_descriptor,
327
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,
336
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,
344 };
345
346 /* string descriptors: */
347
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 */
353 };
354
355 static struct usb_gadget_strings rndis_string_table = {
356 .language = 0x0409, /* en-us */
357 .strings = rndis_string_defs,
358 };
359
360 static struct usb_gadget_strings *rndis_strings[] = {
361 &rndis_string_table,
362 NULL,
363 };
364
365 /*-------------------------------------------------------------------------*/
366
rndis_add_header(struct gether * port,struct sk_buff * skb)367 static struct sk_buff *rndis_add_header(struct gether *port,
368 struct sk_buff *skb)
369 {
370 struct sk_buff *skb2;
371
372 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
373 if (skb2)
374 rndis_add_hdr(skb2);
375
376 dev_kfree_skb_any(skb);
377 return skb2;
378 }
379
rndis_response_available(void * _rndis)380 static void rndis_response_available(void *_rndis)
381 {
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;
387
388 if (atomic_inc_return(&rndis->notify_count) != 1)
389 return;
390
391 /* Send RNDIS RESPONSE_AVAILABLE notification; a
392 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
393 *
394 * This is the only notification defined by RNDIS.
395 */
396 data[0] = cpu_to_le32(1);
397 data[1] = cpu_to_le32(0);
398
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);
403 }
404 }
405
rndis_response_complete(struct usb_ep * ep,struct usb_request * req)406 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
407 {
408 struct f_rndis *rndis = req->context;
409 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
410 int status = req->status;
411
412 /* after TX:
413 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
414 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
415 */
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;
430
431 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
432 * notifications by resending until we're done
433 */
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);
440 }
441 break;
442 }
443 }
444
rndis_command_complete(struct usb_ep * ep,struct usb_request * req)445 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
446 {
447 struct f_rndis *rndis = req->context;
448 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
449 int status;
450
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);
458 }
459
460 static int
rndis_setup(struct usb_function * f,const struct usb_ctrlrequest * ctrl)461 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
462 {
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);
470
471 /* composite driver infrastructure handles everything except
472 * CDC class messages; interface activation uses set_alt().
473 */
474 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
475
476 /* RNDIS uses the CDC command encapsulation mechanism to implement
477 * an RPC scheme, with much getting/setting of attributes by OID.
478 */
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;
489
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;
497
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 req->context = rndis;
504 rndis_free_response(rndis->config, buf);
505 value = n;
506 }
507 /* else stalls ... spec says to avoid that */
508 }
509 break;
510
511 default:
512 invalid:
513 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
514 ctrl->bRequestType, ctrl->bRequest,
515 w_value, w_index, w_length);
516 }
517
518 /* respond with data transfer or status phase? */
519 if (value >= 0) {
520 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
521 ctrl->bRequestType, ctrl->bRequest,
522 w_value, w_index, w_length);
523 req->zero = (value < w_length);
524 req->length = value;
525 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
526 if (value < 0)
527 ERROR(cdev, "rndis response on err %d\n", value);
528 }
529
530 /* device either stalls (value < 0) or reports success */
531 return value;
532 }
533
534
rndis_set_alt(struct usb_function * f,unsigned intf,unsigned alt)535 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
536 {
537 struct f_rndis *rndis = func_to_rndis(f);
538 struct usb_composite_dev *cdev = f->config->cdev;
539
540 /* we know alt == 0 */
541
542 if (intf == rndis->ctrl_id) {
543 if (rndis->notify->driver_data) {
544 VDBG(cdev, "reset rndis control %d\n", intf);
545 usb_ep_disable(rndis->notify);
546 }
547 if (!rndis->notify->desc) {
548 VDBG(cdev, "init rndis ctrl %d\n", intf);
549 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
550 goto fail;
551 }
552 usb_ep_enable(rndis->notify);
553 rndis->notify->driver_data = rndis;
554
555 } else if (intf == rndis->data_id) {
556 struct net_device *net;
557
558 if (rndis->port.in_ep->driver_data) {
559 DBG(cdev, "reset rndis\n");
560 gether_disconnect(&rndis->port);
561 }
562
563 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
564 DBG(cdev, "init rndis\n");
565 if (config_ep_by_speed(cdev->gadget, f,
566 rndis->port.in_ep) ||
567 config_ep_by_speed(cdev->gadget, f,
568 rndis->port.out_ep)) {
569 rndis->port.in_ep->desc = NULL;
570 rndis->port.out_ep->desc = NULL;
571 goto fail;
572 }
573 }
574
575 /* Avoid ZLPs; they can be troublesome. */
576 rndis->port.is_zlp_ok = false;
577
578 /* RNDIS should be in the "RNDIS uninitialized" state,
579 * either never activated or after rndis_uninit().
580 *
581 * We don't want data to flow here until a nonzero packet
582 * filter is set, at which point it enters "RNDIS data
583 * initialized" state ... but we do want the endpoints
584 * to be activated. It's a strange little state.
585 *
586 * REVISIT the RNDIS gadget code has done this wrong for a
587 * very long time. We need another call to the link layer
588 * code -- gether_updown(...bool) maybe -- to do it right.
589 */
590 rndis->port.cdc_filter = 0;
591
592 DBG(cdev, "RNDIS RX/TX early activation ... \n");
593 net = gether_connect(&rndis->port);
594 if (IS_ERR(net))
595 return PTR_ERR(net);
596
597 rndis_set_param_dev(rndis->config, net,
598 &rndis->port.cdc_filter);
599 } else
600 goto fail;
601
602 return 0;
603 fail:
604 return -EINVAL;
605 }
606
rndis_disable(struct usb_function * f)607 static void rndis_disable(struct usb_function *f)
608 {
609 struct f_rndis *rndis = func_to_rndis(f);
610 struct usb_composite_dev *cdev = f->config->cdev;
611
612 if (!rndis->notify->driver_data)
613 return;
614
615 DBG(cdev, "rndis deactivated\n");
616
617 rndis_uninit(rndis->config);
618 gether_disconnect(&rndis->port);
619
620 usb_ep_disable(rndis->notify);
621 rndis->notify->driver_data = NULL;
622 }
623
624 /*-------------------------------------------------------------------------*/
625
626 /*
627 * This isn't quite the same mechanism as CDC Ethernet, since the
628 * notification scheme passes less data, but the same set of link
629 * states must be tested. A key difference is that altsettings are
630 * not used to tell whether the link should send packets or not.
631 */
632
rndis_open(struct gether * geth)633 static void rndis_open(struct gether *geth)
634 {
635 struct f_rndis *rndis = func_to_rndis(&geth->func);
636 struct usb_composite_dev *cdev = geth->func.config->cdev;
637
638 DBG(cdev, "%s\n", __func__);
639
640 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
641 bitrate(cdev->gadget) / 100);
642 rndis_signal_connect(rndis->config);
643 }
644
rndis_close(struct gether * geth)645 static void rndis_close(struct gether *geth)
646 {
647 struct f_rndis *rndis = func_to_rndis(&geth->func);
648
649 DBG(geth->func.config->cdev, "%s\n", __func__);
650
651 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
652 rndis_signal_disconnect(rndis->config);
653 }
654
655 /*-------------------------------------------------------------------------*/
656
657 /* ethernet function driver setup/binding */
658
659 static int
rndis_bind(struct usb_configuration * c,struct usb_function * f)660 rndis_bind(struct usb_configuration *c, struct usb_function *f)
661 {
662 struct usb_composite_dev *cdev = c->cdev;
663 struct f_rndis *rndis = func_to_rndis(f);
664 int status;
665 struct usb_ep *ep;
666
667 /* allocate instance-specific interface IDs */
668 status = usb_interface_id(c, f);
669 if (status < 0)
670 goto fail;
671 rndis->ctrl_id = status;
672 rndis_iad_descriptor.bFirstInterface = status;
673
674 rndis_control_intf.bInterfaceNumber = status;
675 rndis_union_desc.bMasterInterface0 = status;
676
677 status = usb_interface_id(c, f);
678 if (status < 0)
679 goto fail;
680 rndis->data_id = status;
681
682 rndis_data_intf.bInterfaceNumber = status;
683 rndis_union_desc.bSlaveInterface0 = status;
684
685 status = -ENODEV;
686
687 /* allocate instance-specific endpoints */
688 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
689 if (!ep)
690 goto fail;
691 rndis->port.in_ep = ep;
692 ep->driver_data = cdev; /* claim */
693
694 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
695 if (!ep)
696 goto fail;
697 rndis->port.out_ep = ep;
698 ep->driver_data = cdev; /* claim */
699
700 /* NOTE: a status/notification endpoint is, strictly speaking,
701 * optional. We don't treat it that way though! It's simpler,
702 * and some newer profiles don't treat it as optional.
703 */
704 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
705 if (!ep)
706 goto fail;
707 rndis->notify = ep;
708 ep->driver_data = cdev; /* claim */
709
710 status = -ENOMEM;
711
712 /* allocate notification request and buffer */
713 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
714 if (!rndis->notify_req)
715 goto fail;
716 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
717 if (!rndis->notify_req->buf)
718 goto fail;
719 rndis->notify_req->length = STATUS_BYTECOUNT;
720 rndis->notify_req->context = rndis;
721 rndis->notify_req->complete = rndis_response_complete;
722
723 /* copy descriptors, and track endpoint copies */
724 f->descriptors = usb_copy_descriptors(eth_fs_function);
725 if (!f->descriptors)
726 goto fail;
727
728 /* support all relevant hardware speeds... we expect that when
729 * hardware is dual speed, all bulk-capable endpoints work at
730 * both speeds
731 */
732 if (gadget_is_dualspeed(c->cdev->gadget)) {
733 hs_in_desc.bEndpointAddress =
734 fs_in_desc.bEndpointAddress;
735 hs_out_desc.bEndpointAddress =
736 fs_out_desc.bEndpointAddress;
737 hs_notify_desc.bEndpointAddress =
738 fs_notify_desc.bEndpointAddress;
739
740 /* copy descriptors, and track endpoint copies */
741 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
742 if (!f->hs_descriptors)
743 goto fail;
744 }
745
746 if (gadget_is_superspeed(c->cdev->gadget)) {
747 ss_in_desc.bEndpointAddress =
748 fs_in_desc.bEndpointAddress;
749 ss_out_desc.bEndpointAddress =
750 fs_out_desc.bEndpointAddress;
751 ss_notify_desc.bEndpointAddress =
752 fs_notify_desc.bEndpointAddress;
753
754 /* copy descriptors, and track endpoint copies */
755 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
756 if (!f->ss_descriptors)
757 goto fail;
758 }
759
760 rndis->port.open = rndis_open;
761 rndis->port.close = rndis_close;
762
763 status = rndis_register(rndis_response_available, rndis);
764 if (status < 0)
765 goto fail;
766 rndis->config = status;
767
768 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
769 rndis_set_host_mac(rndis->config, rndis->ethaddr);
770
771 #if 0
772 // FIXME
773 if (rndis_set_param_vendor(rndis->config, vendorID,
774 manufacturer))
775 goto fail0;
776 #endif
777
778 /* NOTE: all that is done without knowing or caring about
779 * the network link ... which is unavailable to this code
780 * until we're activated via set_alt().
781 */
782
783 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
784 gadget_is_superspeed(c->cdev->gadget) ? "super" :
785 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
786 rndis->port.in_ep->name, rndis->port.out_ep->name,
787 rndis->notify->name);
788 return 0;
789
790 fail:
791 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
792 usb_free_descriptors(f->ss_descriptors);
793 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
794 usb_free_descriptors(f->hs_descriptors);
795 if (f->descriptors)
796 usb_free_descriptors(f->descriptors);
797
798 if (rndis->notify_req) {
799 kfree(rndis->notify_req->buf);
800 usb_ep_free_request(rndis->notify, rndis->notify_req);
801 }
802
803 /* we might as well release our claims on endpoints */
804 if (rndis->notify)
805 rndis->notify->driver_data = NULL;
806 if (rndis->port.out_ep)
807 rndis->port.out_ep->driver_data = NULL;
808 if (rndis->port.in_ep)
809 rndis->port.in_ep->driver_data = NULL;
810
811 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
812
813 return status;
814 }
815
816 static void
rndis_unbind(struct usb_configuration * c,struct usb_function * f)817 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
818 {
819 struct f_rndis *rndis = func_to_rndis(f);
820
821 rndis_deregister(rndis->config);
822 rndis_exit();
823
824 if (gadget_is_superspeed(c->cdev->gadget))
825 usb_free_descriptors(f->ss_descriptors);
826 if (gadget_is_dualspeed(c->cdev->gadget))
827 usb_free_descriptors(f->hs_descriptors);
828 usb_free_descriptors(f->descriptors);
829
830 kfree(rndis->notify_req->buf);
831 usb_ep_free_request(rndis->notify, rndis->notify_req);
832
833 kfree(rndis);
834 }
835
836 /* Some controllers can't support RNDIS ... */
can_support_rndis(struct usb_configuration * c)837 static inline bool can_support_rndis(struct usb_configuration *c)
838 {
839 /* everything else is *presumably* fine */
840 return true;
841 }
842
843 /**
844 * rndis_bind_config - add RNDIS network link to a configuration
845 * @c: the configuration to support the network link
846 * @ethaddr: a buffer in which the ethernet address of the host side
847 * side of the link was recorded
848 * Context: single threaded during gadget setup
849 *
850 * Returns zero on success, else negative errno.
851 *
852 * Caller must have called @gether_setup(). Caller is also responsible
853 * for calling @gether_cleanup() before module unload.
854 */
855 int
rndis_bind_config(struct usb_configuration * c,u8 ethaddr[ETH_ALEN])856 rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
857 {
858 struct f_rndis *rndis;
859 int status;
860
861 if (!can_support_rndis(c) || !ethaddr)
862 return -EINVAL;
863
864 /* maybe allocate device-global string IDs */
865 if (rndis_string_defs[0].id == 0) {
866
867 /* ... and setup RNDIS itself */
868 status = rndis_init();
869 if (status < 0)
870 return status;
871
872 /* control interface label */
873 status = usb_string_id(c->cdev);
874 if (status < 0)
875 return status;
876 rndis_string_defs[0].id = status;
877 rndis_control_intf.iInterface = status;
878
879 /* data interface label */
880 status = usb_string_id(c->cdev);
881 if (status < 0)
882 return status;
883 rndis_string_defs[1].id = status;
884 rndis_data_intf.iInterface = status;
885
886 /* IAD iFunction label */
887 status = usb_string_id(c->cdev);
888 if (status < 0)
889 return status;
890 rndis_string_defs[2].id = status;
891 rndis_iad_descriptor.iFunction = status;
892 }
893
894 /* allocate and initialize one new instance */
895 status = -ENOMEM;
896 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
897 if (!rndis)
898 goto fail;
899
900 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
901
902 /* RNDIS activates when the host changes this filter */
903 rndis->port.cdc_filter = 0;
904
905 /* RNDIS has special (and complex) framing */
906 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
907 rndis->port.wrap = rndis_add_header;
908 rndis->port.unwrap = rndis_rm_hdr;
909
910 rndis->port.func.name = "rndis";
911 rndis->port.func.strings = rndis_strings;
912 /* descriptors are per-instance copies */
913 rndis->port.func.bind = rndis_bind;
914 rndis->port.func.unbind = rndis_unbind;
915 rndis->port.func.set_alt = rndis_set_alt;
916 rndis->port.func.setup = rndis_setup;
917 rndis->port.func.disable = rndis_disable;
918
919 status = usb_add_function(c, &rndis->port.func);
920 if (status) {
921 kfree(rndis);
922 fail:
923 rndis_exit();
924 }
925 return status;
926 }
927