]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/usb/gadget/f_rndis.c
fcdee12863cf945411eccaeead8ffbbcb30406ad
[sojka/nv-tegra/linux-3.10.git] / drivers / usb / gadget / f_rndis.c
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  * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  */
16
17 /* #define VERBOSE_DEBUG */
18
19 #include <linux/slab.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/etherdevice.h>
23 #include <linux/moduleparam.h>
24
25 #include <linux/atomic.h>
26
27 #include "u_ether.h"
28 #include "rndis.h"
29
30 static bool rndis_multipacket_dl_disable;
31 module_param(rndis_multipacket_dl_disable, bool, S_IRUGO|S_IWUSR);
32 MODULE_PARM_DESC(rndis_multipacket_dl_disable,
33         "Disable RNDIS Multi-packet support in DownLink");
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
76 struct f_rndis {
77         struct gether                   port;
78         u8                              ctrl_id, data_id;
79         u8                              ethaddr[ETH_ALEN];
80         u32                             vendorID;
81         const char                      *manufacturer;
82         int                             config;
83
84         struct usb_ep                   *notify;
85         struct usb_request              *notify_req;
86         atomic_t                        notify_count;
87 };
88
89 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
90 {
91         return container_of(f, struct f_rndis, port.func);
92 }
93
94 /* peak (theoretical) bulk transfer rate in bits-per-second */
95 static unsigned int bitrate(struct usb_gadget *g)
96 {
97         if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
98                 return 13 * 1024 * 8 * 1000 * 8;
99         else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
100                 return 13 * 512 * 8 * 1000 * 8;
101         else
102                 return 19 * 64 * 1 * 1000 * 8;
103 }
104
105 /*-------------------------------------------------------------------------*/
106
107 /*
108  */
109
110 #define RNDIS_STATUS_INTERVAL_MS        32
111 #define STATUS_BYTECOUNT                8       /* 8 bytes data */
112
113
114 /* interface descriptor: */
115
116 static struct usb_interface_descriptor rndis_control_intf = {
117         .bLength =              sizeof rndis_control_intf,
118         .bDescriptorType =      USB_DT_INTERFACE,
119
120         /* .bInterfaceNumber = DYNAMIC */
121         /* status endpoint is optional; this could be patched later */
122         .bNumEndpoints =        1,
123         .bInterfaceClass =      USB_CLASS_COMM,
124         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
125         .bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR,
126         /* .iInterface = DYNAMIC */
127 };
128
129 static struct usb_cdc_header_desc header_desc = {
130         .bLength =              sizeof header_desc,
131         .bDescriptorType =      USB_DT_CS_INTERFACE,
132         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
133
134         .bcdCDC =               cpu_to_le16(0x0110),
135 };
136
137 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
138         .bLength =              sizeof call_mgmt_descriptor,
139         .bDescriptorType =      USB_DT_CS_INTERFACE,
140         .bDescriptorSubType =   USB_CDC_CALL_MANAGEMENT_TYPE,
141
142         .bmCapabilities =       0x00,
143         .bDataInterface =       0x01,
144 };
145
146 static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
147         .bLength =              sizeof rndis_acm_descriptor,
148         .bDescriptorType =      USB_DT_CS_INTERFACE,
149         .bDescriptorSubType =   USB_CDC_ACM_TYPE,
150
151         .bmCapabilities =       0x00,
152 };
153
154 static struct usb_cdc_union_desc rndis_union_desc = {
155         .bLength =              sizeof(rndis_union_desc),
156         .bDescriptorType =      USB_DT_CS_INTERFACE,
157         .bDescriptorSubType =   USB_CDC_UNION_TYPE,
158         /* .bMasterInterface0 = DYNAMIC */
159         /* .bSlaveInterface0 =  DYNAMIC */
160 };
161
162 /* the data interface has two bulk endpoints */
163
164 static struct usb_interface_descriptor rndis_data_intf = {
165         .bLength =              sizeof rndis_data_intf,
166         .bDescriptorType =      USB_DT_INTERFACE,
167
168         /* .bInterfaceNumber = DYNAMIC */
169         .bNumEndpoints =        2,
170         .bInterfaceClass =      USB_CLASS_CDC_DATA,
171         .bInterfaceSubClass =   0,
172         .bInterfaceProtocol =   0,
173         /* .iInterface = DYNAMIC */
174 };
175
176
177 static struct usb_interface_assoc_descriptor
178 rndis_iad_descriptor = {
179         .bLength =              sizeof rndis_iad_descriptor,
180         .bDescriptorType =      USB_DT_INTERFACE_ASSOCIATION,
181
182         .bFirstInterface =      0, /* XXX, hardcoded */
183         .bInterfaceCount =      2,      // control + data
184         .bFunctionClass =       USB_CLASS_COMM,
185         .bFunctionSubClass =    USB_CDC_SUBCLASS_ETHERNET,
186         .bFunctionProtocol =    USB_CDC_PROTO_NONE,
187         /* .iFunction = DYNAMIC */
188 };
189
190 /* full speed support: */
191
192 static struct usb_endpoint_descriptor fs_notify_desc = {
193         .bLength =              USB_DT_ENDPOINT_SIZE,
194         .bDescriptorType =      USB_DT_ENDPOINT,
195
196         .bEndpointAddress =     USB_DIR_IN,
197         .bmAttributes =         USB_ENDPOINT_XFER_INT,
198         .wMaxPacketSize =       cpu_to_le16(STATUS_BYTECOUNT),
199         .bInterval =            RNDIS_STATUS_INTERVAL_MS,
200 };
201
202 static struct usb_endpoint_descriptor fs_in_desc = {
203         .bLength =              USB_DT_ENDPOINT_SIZE,
204         .bDescriptorType =      USB_DT_ENDPOINT,
205
206         .bEndpointAddress =     USB_DIR_IN,
207         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
208 };
209
210 static struct usb_endpoint_descriptor fs_out_desc = {
211         .bLength =              USB_DT_ENDPOINT_SIZE,
212         .bDescriptorType =      USB_DT_ENDPOINT,
213
214         .bEndpointAddress =     USB_DIR_OUT,
215         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
216 };
217
218 static struct usb_descriptor_header *eth_fs_function[] = {
219         (struct usb_descriptor_header *) &rndis_iad_descriptor,
220
221         /* control interface matches ACM, not Ethernet */
222         (struct usb_descriptor_header *) &rndis_control_intf,
223         (struct usb_descriptor_header *) &header_desc,
224         (struct usb_descriptor_header *) &call_mgmt_descriptor,
225         (struct usb_descriptor_header *) &rndis_acm_descriptor,
226         (struct usb_descriptor_header *) &rndis_union_desc,
227         (struct usb_descriptor_header *) &fs_notify_desc,
228
229         /* data interface has no altsetting */
230         (struct usb_descriptor_header *) &rndis_data_intf,
231         (struct usb_descriptor_header *) &fs_in_desc,
232         (struct usb_descriptor_header *) &fs_out_desc,
233         NULL,
234 };
235
236 /* high speed support: */
237
238 static struct usb_endpoint_descriptor hs_notify_desc = {
239         .bLength =              USB_DT_ENDPOINT_SIZE,
240         .bDescriptorType =      USB_DT_ENDPOINT,
241
242         .bEndpointAddress =     USB_DIR_IN,
243         .bmAttributes =         USB_ENDPOINT_XFER_INT,
244         .wMaxPacketSize =       cpu_to_le16(STATUS_BYTECOUNT),
245         .bInterval =            USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
246 };
247
248 static struct usb_endpoint_descriptor hs_in_desc = {
249         .bLength =              USB_DT_ENDPOINT_SIZE,
250         .bDescriptorType =      USB_DT_ENDPOINT,
251
252         .bEndpointAddress =     USB_DIR_IN,
253         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
254         .wMaxPacketSize =       cpu_to_le16(512),
255 };
256
257 static struct usb_endpoint_descriptor hs_out_desc = {
258         .bLength =              USB_DT_ENDPOINT_SIZE,
259         .bDescriptorType =      USB_DT_ENDPOINT,
260
261         .bEndpointAddress =     USB_DIR_OUT,
262         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
263         .wMaxPacketSize =       cpu_to_le16(512),
264 };
265
266 static struct usb_descriptor_header *eth_hs_function[] = {
267         (struct usb_descriptor_header *) &rndis_iad_descriptor,
268
269         /* control interface matches ACM, not Ethernet */
270         (struct usb_descriptor_header *) &rndis_control_intf,
271         (struct usb_descriptor_header *) &header_desc,
272         (struct usb_descriptor_header *) &call_mgmt_descriptor,
273         (struct usb_descriptor_header *) &rndis_acm_descriptor,
274         (struct usb_descriptor_header *) &rndis_union_desc,
275         (struct usb_descriptor_header *) &hs_notify_desc,
276
277         /* data interface has no altsetting */
278         (struct usb_descriptor_header *) &rndis_data_intf,
279         (struct usb_descriptor_header *) &hs_in_desc,
280         (struct usb_descriptor_header *) &hs_out_desc,
281         NULL,
282 };
283
284 /* super speed support: */
285
286 static struct usb_endpoint_descriptor ss_notify_desc = {
287         .bLength =              USB_DT_ENDPOINT_SIZE,
288         .bDescriptorType =      USB_DT_ENDPOINT,
289
290         .bEndpointAddress =     USB_DIR_IN,
291         .bmAttributes =         USB_ENDPOINT_XFER_INT,
292         .wMaxPacketSize =       cpu_to_le16(STATUS_BYTECOUNT),
293         .bInterval =            USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
294 };
295
296 static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
297         .bLength =              sizeof ss_intr_comp_desc,
298         .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
299
300         /* the following 3 values can be tweaked if necessary */
301         /* .bMaxBurst =         0, */
302         /* .bmAttributes =      0, */
303         .wBytesPerInterval =    cpu_to_le16(STATUS_BYTECOUNT),
304 };
305
306 static struct usb_endpoint_descriptor ss_in_desc = {
307         .bLength =              USB_DT_ENDPOINT_SIZE,
308         .bDescriptorType =      USB_DT_ENDPOINT,
309
310         .bEndpointAddress =     USB_DIR_IN,
311         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
312         .wMaxPacketSize =       cpu_to_le16(1024),
313 };
314
315 static struct usb_endpoint_descriptor ss_out_desc = {
316         .bLength =              USB_DT_ENDPOINT_SIZE,
317         .bDescriptorType =      USB_DT_ENDPOINT,
318
319         .bEndpointAddress =     USB_DIR_OUT,
320         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
321         .wMaxPacketSize =       cpu_to_le16(1024),
322 };
323
324 static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
325         .bLength =              sizeof ss_bulk_comp_desc,
326         .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
327
328         /* the following 2 values can be tweaked if necessary */
329         /* .bMaxBurst =         0, */
330         /* .bmAttributes =      0, */
331 };
332
333 static struct usb_descriptor_header *eth_ss_function[] = {
334         (struct usb_descriptor_header *) &rndis_iad_descriptor,
335
336         /* control interface matches ACM, not Ethernet */
337         (struct usb_descriptor_header *) &rndis_control_intf,
338         (struct usb_descriptor_header *) &header_desc,
339         (struct usb_descriptor_header *) &call_mgmt_descriptor,
340         (struct usb_descriptor_header *) &rndis_acm_descriptor,
341         (struct usb_descriptor_header *) &rndis_union_desc,
342         (struct usb_descriptor_header *) &ss_notify_desc,
343         (struct usb_descriptor_header *) &ss_intr_comp_desc,
344
345         /* data interface has no altsetting */
346         (struct usb_descriptor_header *) &rndis_data_intf,
347         (struct usb_descriptor_header *) &ss_in_desc,
348         (struct usb_descriptor_header *) &ss_bulk_comp_desc,
349         (struct usb_descriptor_header *) &ss_out_desc,
350         (struct usb_descriptor_header *) &ss_bulk_comp_desc,
351         NULL,
352 };
353
354 /* string descriptors: */
355
356 static struct usb_string rndis_string_defs[] = {
357         [0].s = "RNDIS Communications Control",
358         [1].s = "RNDIS Ethernet Data",
359         [2].s = "RNDIS",
360         {  } /* end of list */
361 };
362
363 static struct usb_gadget_strings rndis_string_table = {
364         .language =             0x0409, /* en-us */
365         .strings =              rndis_string_defs,
366 };
367
368 static struct usb_gadget_strings *rndis_strings[] = {
369         &rndis_string_table,
370         NULL,
371 };
372
373 /*-------------------------------------------------------------------------*/
374
375 static struct sk_buff *rndis_add_header(struct gether *port,
376                                         struct sk_buff *skb)
377 {
378         struct sk_buff *skb2;
379
380         skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
381         if (skb2)
382                 rndis_add_hdr(skb2);
383
384         dev_kfree_skb_any(skb);
385         return skb2;
386 }
387
388 static void rndis_response_available(void *_rndis)
389 {
390         struct f_rndis                  *rndis = _rndis;
391         struct usb_request              *req = rndis->notify_req;
392         __le32                          *data = req->buf;
393         int                             status;
394
395         if (atomic_inc_return(&rndis->notify_count) != 1)
396                 return;
397
398         /* Send RNDIS RESPONSE_AVAILABLE notification; a
399          * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
400          *
401          * This is the only notification defined by RNDIS.
402          */
403         data[0] = cpu_to_le32(1);
404         data[1] = cpu_to_le32(0);
405
406         status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
407         if (status) {
408                 atomic_dec(&rndis->notify_count);
409                 pr_err("notify/0 --> %d\n", status);
410         }
411 }
412
413 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
414 {
415         struct f_rndis                  *rndis = req->context;
416         int                             status = req->status;
417
418         /* after TX:
419          *  - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
420          *  - RNDIS_RESPONSE_AVAILABLE (status/irq)
421          */
422         switch (status) {
423         case -ECONNRESET:
424         case -ESHUTDOWN:
425                 /* connection gone */
426                 atomic_set(&rndis->notify_count, 0);
427                 break;
428         default:
429                 pr_err("RNDIS %s response error %d, %d/%d\n",
430                         ep->name, status,
431                         req->actual, req->length);
432                 /* FALLTHROUGH */
433         case 0:
434                 if (ep != rndis->notify)
435                         break;
436
437                 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
438                  * notifications by resending until we're done
439                  */
440                 if (atomic_dec_and_test(&rndis->notify_count))
441                         break;
442                 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
443                 if (status) {
444                         atomic_dec(&rndis->notify_count);
445                         pr_err("notify/1 --> %d\n", status);
446                 }
447                 break;
448         }
449 }
450
451 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
452 {
453         struct f_rndis                  *rndis = req->context;
454         int                             status;
455         rndis_init_msg_type             *buf;
456
457         /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
458 //      spin_lock(&dev->lock);
459         status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
460         if (status < 0)
461                 pr_err("RNDIS command error %d, %d/%d\n",
462                         status, req->actual, req->length);
463
464         buf = (rndis_init_msg_type *)req->buf;
465
466         if (buf->MessageType == RNDIS_MSG_INIT) {
467                 if (buf->MaxTransferSize > 2048)
468                         rndis->port.multi_pkt_xfer = 1;
469                 else
470                         rndis->port.multi_pkt_xfer = 0;
471                 pr_info("%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
472                                 __func__, buf->MaxTransferSize,
473                                 rndis->port.multi_pkt_xfer ? "enabled" :
474                                                             "disabled");
475                 if (rndis_multipacket_dl_disable)
476                         rndis->port.multi_pkt_xfer = 0;
477         }
478 //      spin_unlock(&dev->lock);
479 }
480
481 static int
482 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
483 {
484         struct f_rndis          *rndis = func_to_rndis(f);
485         struct usb_composite_dev *cdev = f->config->cdev;
486         struct usb_request      *req = cdev->req;
487         int                     value = -EOPNOTSUPP;
488         u16                     w_index = le16_to_cpu(ctrl->wIndex);
489         u16                     w_value = le16_to_cpu(ctrl->wValue);
490         u16                     w_length = le16_to_cpu(ctrl->wLength);
491
492         /* composite driver infrastructure handles everything except
493          * CDC class messages; interface activation uses set_alt().
494          */
495         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
496
497         /* RNDIS uses the CDC command encapsulation mechanism to implement
498          * an RPC scheme, with much getting/setting of attributes by OID.
499          */
500         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
501                         | USB_CDC_SEND_ENCAPSULATED_COMMAND:
502                 if (w_value || w_index != rndis->ctrl_id)
503                         goto invalid;
504                 /* read the request; process it later */
505                 value = w_length;
506                 req->complete = rndis_command_complete;
507                 req->context = rndis;
508                 /* later, rndis_response_available() sends a notification */
509                 break;
510
511         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
512                         | USB_CDC_GET_ENCAPSULATED_RESPONSE:
513                 if (w_value || w_index != rndis->ctrl_id)
514                         goto invalid;
515                 else {
516                         u8 *buf;
517                         u32 n;
518
519                         /* return the result */
520                         buf = rndis_get_next_response(rndis->config, &n);
521                         if (buf) {
522                                 memcpy(req->buf, buf, n);
523                                 req->complete = rndis_response_complete;
524                                 req->context = rndis;
525                                 rndis_free_response(rndis->config, buf);
526                                 value = n;
527                         }
528                         /* else stalls ... spec says to avoid that */
529                 }
530                 break;
531
532         default:
533 invalid:
534                 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
535                         ctrl->bRequestType, ctrl->bRequest,
536                         w_value, w_index, w_length);
537         }
538
539         /* respond with data transfer or status phase? */
540         if (value >= 0) {
541                 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
542                         ctrl->bRequestType, ctrl->bRequest,
543                         w_value, w_index, w_length);
544                 req->zero = (value < w_length);
545                 req->length = value;
546                 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
547                 if (value < 0)
548                         ERROR(cdev, "rndis response on err %d\n", value);
549         }
550
551         /* device either stalls (value < 0) or reports success */
552         return value;
553 }
554
555
556 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
557 {
558         struct f_rndis          *rndis = func_to_rndis(f);
559         struct usb_composite_dev *cdev = f->config->cdev;
560
561         /* we know alt == 0 */
562
563         if (intf == rndis->ctrl_id) {
564                 if (rndis->notify->driver_data) {
565                         VDBG(cdev, "reset rndis control %d\n", intf);
566                         usb_ep_disable(rndis->notify);
567                 }
568                 if (!rndis->notify->desc) {
569                         VDBG(cdev, "init rndis ctrl %d\n", intf);
570                         if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
571                                 goto fail;
572                 }
573                 usb_ep_enable(rndis->notify);
574                 rndis->notify->driver_data = rndis;
575
576         } else if (intf == rndis->data_id) {
577                 struct net_device       *net;
578
579                 if (rndis->port.in_ep->driver_data) {
580                         DBG(cdev, "reset rndis\n");
581                         gether_disconnect(&rndis->port);
582                 }
583
584                 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
585                         DBG(cdev, "init rndis\n");
586                         if (config_ep_by_speed(cdev->gadget, f,
587                                                rndis->port.in_ep) ||
588                             config_ep_by_speed(cdev->gadget, f,
589                                                rndis->port.out_ep)) {
590                                 rndis->port.in_ep->desc = NULL;
591                                 rndis->port.out_ep->desc = NULL;
592                                 goto fail;
593                         }
594                 }
595
596                 /* Avoid ZLPs; they can be troublesome. */
597                 rndis->port.is_zlp_ok = false;
598
599                 /* RNDIS should be in the "RNDIS uninitialized" state,
600                  * either never activated or after rndis_uninit().
601                  *
602                  * We don't want data to flow here until a nonzero packet
603                  * filter is set, at which point it enters "RNDIS data
604                  * initialized" state ... but we do want the endpoints
605                  * to be activated.  It's a strange little state.
606                  *
607                  * REVISIT the RNDIS gadget code has done this wrong for a
608                  * very long time.  We need another call to the link layer
609                  * code -- gether_updown(...bool) maybe -- to do it right.
610                  */
611                 rndis->port.cdc_filter = 0;
612
613                 DBG(cdev, "RNDIS RX/TX early activation ... \n");
614                 net = gether_connect(&rndis->port);
615                 if (IS_ERR(net))
616                         return PTR_ERR(net);
617
618                 rndis_set_param_dev(rndis->config, net,
619                                 &rndis->port.cdc_filter);
620         } else
621                 goto fail;
622
623         return 0;
624 fail:
625         return -EINVAL;
626 }
627
628 static void rndis_disable(struct usb_function *f)
629 {
630         struct f_rndis          *rndis = func_to_rndis(f);
631         struct usb_composite_dev *cdev = f->config->cdev;
632
633         if (!rndis->notify->driver_data)
634                 return;
635
636         DBG(cdev, "rndis deactivated\n");
637
638         rndis_uninit(rndis->config);
639         gether_disconnect(&rndis->port);
640
641         usb_ep_disable(rndis->notify);
642         rndis->notify->driver_data = NULL;
643 }
644
645 /*-------------------------------------------------------------------------*/
646
647 /*
648  * This isn't quite the same mechanism as CDC Ethernet, since the
649  * notification scheme passes less data, but the same set of link
650  * states must be tested.  A key difference is that altsettings are
651  * not used to tell whether the link should send packets or not.
652  */
653
654 static void rndis_open(struct gether *geth)
655 {
656         struct f_rndis          *rndis = func_to_rndis(&geth->func);
657         struct usb_composite_dev *cdev = geth->func.config->cdev;
658
659         DBG(cdev, "%s\n", __func__);
660
661         rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3,
662                                 bitrate(cdev->gadget) / 100);
663         rndis_signal_connect(rndis->config);
664 }
665
666 static void rndis_close(struct gether *geth)
667 {
668         struct f_rndis          *rndis = func_to_rndis(&geth->func);
669
670         DBG(geth->func.config->cdev, "%s\n", __func__);
671
672         rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0);
673         rndis_signal_disconnect(rndis->config);
674 }
675
676 /*-------------------------------------------------------------------------*/
677
678 /* ethernet function driver setup/binding */
679
680 static int
681 rndis_bind(struct usb_configuration *c, struct usb_function *f)
682 {
683         struct usb_composite_dev *cdev = c->cdev;
684         struct f_rndis          *rndis = func_to_rndis(f);
685         int                     status;
686         struct usb_ep           *ep;
687
688         /* allocate instance-specific interface IDs */
689         status = usb_interface_id(c, f);
690         if (status < 0)
691                 goto fail;
692         rndis->ctrl_id = status;
693         rndis_iad_descriptor.bFirstInterface = status;
694
695         rndis_control_intf.bInterfaceNumber = status;
696         rndis_union_desc.bMasterInterface0 = status;
697
698         status = usb_interface_id(c, f);
699         if (status < 0)
700                 goto fail;
701         rndis->data_id = status;
702
703         rndis_data_intf.bInterfaceNumber = status;
704         rndis_union_desc.bSlaveInterface0 = status;
705
706         status = -ENODEV;
707
708         /* allocate instance-specific endpoints */
709         ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
710         if (!ep)
711                 goto fail;
712         rndis->port.in_ep = ep;
713         ep->driver_data = cdev; /* claim */
714
715         ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
716         if (!ep)
717                 goto fail;
718         rndis->port.out_ep = ep;
719         ep->driver_data = cdev; /* claim */
720
721         /* NOTE:  a status/notification endpoint is, strictly speaking,
722          * optional.  We don't treat it that way though!  It's simpler,
723          * and some newer profiles don't treat it as optional.
724          */
725         ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
726         if (!ep)
727                 goto fail;
728         rndis->notify = ep;
729         ep->driver_data = cdev; /* claim */
730
731         status = -ENOMEM;
732
733         /* allocate notification request and buffer */
734         rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
735         if (!rndis->notify_req)
736                 goto fail;
737         rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
738         if (!rndis->notify_req->buf)
739                 goto fail;
740         rndis->notify_req->length = STATUS_BYTECOUNT;
741         rndis->notify_req->context = rndis;
742         rndis->notify_req->complete = rndis_response_complete;
743
744         /* support all relevant hardware speeds... we expect that when
745          * hardware is dual speed, all bulk-capable endpoints work at
746          * both speeds
747          */
748         hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
749         hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
750         hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
751
752         ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
753         ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
754         ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
755
756         status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
757                         eth_ss_function);
758         if (status)
759                 goto fail;
760
761         rndis->port.open = rndis_open;
762         rndis->port.close = rndis_close;
763
764         status = rndis_register(rndis_response_available, rndis);
765         if (status < 0)
766                 goto fail;
767         rndis->config = status;
768
769         rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0);
770         rndis_set_host_mac(rndis->config, rndis->ethaddr);
771
772         if (rndis->manufacturer && rndis->vendorID &&
773                         rndis_set_param_vendor(rndis->config, rndis->vendorID,
774                                                rndis->manufacturer))
775                 goto fail;
776
777         /* NOTE:  all that is done without knowing or caring about
778          * the network link ... which is unavailable to this code
779          * until we're activated via set_alt().
780          */
781
782         DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
783                         gadget_is_superspeed(c->cdev->gadget) ? "super" :
784                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
785                         rndis->port.in_ep->name, rndis->port.out_ep->name,
786                         rndis->notify->name);
787         return 0;
788
789 fail:
790         usb_free_all_descriptors(f);
791
792         if (rndis->notify_req) {
793                 kfree(rndis->notify_req->buf);
794                 usb_ep_free_request(rndis->notify, rndis->notify_req);
795         }
796
797         /* we might as well release our claims on endpoints */
798         if (rndis->notify)
799                 rndis->notify->driver_data = NULL;
800         if (rndis->port.out_ep)
801                 rndis->port.out_ep->driver_data = NULL;
802         if (rndis->port.in_ep)
803                 rndis->port.in_ep->driver_data = NULL;
804
805         ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
806
807         return status;
808 }
809
810 static void
811 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
812 {
813         struct f_rndis          *rndis = func_to_rndis(f);
814
815         rndis_deregister(rndis->config);
816         rndis_exit();
817         rndis_string_defs[0].id = 0;
818
819         rndis_string_defs[0].id = 0;
820         usb_free_all_descriptors(f);
821
822         kfree(rndis->notify_req->buf);
823         usb_ep_free_request(rndis->notify, rndis->notify_req);
824
825         kfree(rndis);
826 }
827
828 /* Some controllers can't support RNDIS ... */
829 static inline bool can_support_rndis(struct usb_configuration *c)
830 {
831         /* everything else is *presumably* fine */
832         return true;
833 }
834
835 int
836 rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
837                 u32 vendorID, const char *manufacturer, struct eth_dev *dev)
838 {
839         struct f_rndis  *rndis;
840         int             status;
841
842         if (!can_support_rndis(c) || !ethaddr)
843                 return -EINVAL;
844
845         /* setup RNDIS itself */
846         status = rndis_init();
847         if (status < 0)
848                 return status;
849
850         status = usb_string_ids_tab(c->cdev, rndis_string_defs);
851         if (status)
852                 return status;
853
854         rndis_control_intf.iInterface = rndis_string_defs[0].id;
855         rndis_data_intf.iInterface = rndis_string_defs[1].id;
856         rndis_iad_descriptor.iFunction = rndis_string_defs[2].id;
857
858         /* allocate and initialize one new instance */
859         status = -ENOMEM;
860         rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
861         if (!rndis)
862                 goto fail;
863
864         memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
865         rndis->vendorID = vendorID;
866         rndis->manufacturer = manufacturer;
867
868         rndis->port.ioport = dev;
869         /* RNDIS activates when the host changes this filter */
870         rndis->port.cdc_filter = 0;
871
872         /* RNDIS has special (and complex) framing */
873         rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
874         rndis->port.wrap = rndis_add_header;
875         rndis->port.unwrap = rndis_rm_hdr;
876
877         rndis->port.func.name = "rndis";
878         rndis->port.func.strings = rndis_strings;
879         /* descriptors are per-instance copies */
880         rndis->port.func.bind = rndis_bind;
881         rndis->port.func.unbind = rndis_unbind;
882         rndis->port.func.set_alt = rndis_set_alt;
883         rndis->port.func.setup = rndis_setup;
884         rndis->port.func.disable = rndis_disable;
885
886         status = usb_add_function(c, &rndis->port.func);
887         if (status) {
888                 kfree(rndis);
889 fail:
890                 rndis_exit();
891         }
892         return status;
893 }