Merge remote-tracking branch 'linux-2.6.32.y/master' into develop
[firefly-linux-kernel-4.4.55.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  *
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/platform_device.h>
27 #include <linux/etherdevice.h>
28 #include <linux/usb/android_composite.h>
29
30 #include <asm/atomic.h>
31
32 #include "u_ether.h"
33 #include "rndis.h"
34
35
36 /*
37  * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
38  * been promoted instead of the standard CDC Ethernet.  The published RNDIS
39  * spec is ambiguous, incomplete, and needlessly complex.  Variants such as
40  * ActiveSync have even worse status in terms of specification.
41  *
42  * In short:  it's a protocol controlled by (and for) Microsoft, not for an
43  * Open ecosystem or markets.  Linux supports it *only* because Microsoft
44  * doesn't support the CDC Ethernet standard.
45  *
46  * The RNDIS data transfer model is complex, with multiple Ethernet packets
47  * per USB message, and out of band data.  The control model is built around
48  * what's essentially an "RNDIS RPC" protocol.  It's all wrapped in a CDC ACM
49  * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
50  * useless (they're ignored).  RNDIS expects to be the only function in its
51  * configuration, so it's no real help if you need composite devices; and
52  * it expects to be the first configuration too.
53  *
54  * There is a single technical advantage of RNDIS over CDC Ethernet, if you
55  * discount the fluff that its RPC can be made to deliver: it doesn't need
56  * a NOP altsetting for the data interface.  That lets it work on some of the
57  * "so smart it's stupid" hardware which takes over configuration changes
58  * from the software, and adds restrictions like "no altsettings".
59  *
60  * Unfortunately MSFT's RNDIS drivers are buggy.  They hang or oops, and
61  * have all sorts of contrary-to-specification oddities that can prevent
62  * them from working sanely.  Since bugfixes (or accurate specs, letting
63  * Linux work around those bugs) are unlikely to ever come from MSFT, you
64  * may want to avoid using RNDIS on purely operational grounds.
65  *
66  * Omissions from the RNDIS 1.0 specification include:
67  *
68  *   - Power management ... references data that's scattered around lots
69  *     of other documentation, which is incorrect/incomplete there too.
70  *
71  *   - There are various undocumented protocol requirements, like the need
72  *     to send garbage in some control-OUT messages.
73  *
74  *   - MS-Windows drivers sometimes emit undocumented requests.
75  */
76
77 struct rndis_ep_descs {
78         struct usb_endpoint_descriptor  *in;
79         struct usb_endpoint_descriptor  *out;
80         struct usb_endpoint_descriptor  *notify;
81 };
82
83 struct f_rndis {
84         struct gether                   port;
85         u8                              ctrl_id, data_id;
86         u8                              ethaddr[ETH_ALEN];
87         int                             config;
88
89         struct rndis_ep_descs           fs;
90         struct rndis_ep_descs           hs;
91
92         struct usb_ep                   *notify;
93         struct usb_endpoint_descriptor  *notify_desc;
94         struct usb_request              *notify_req;
95         atomic_t                        notify_count;
96 };
97
98 static inline struct f_rndis *func_to_rndis(struct usb_function *f)
99 {
100         return container_of(f, struct f_rndis, port.func);
101 }
102
103 /* peak (theoretical) bulk transfer rate in bits-per-second */
104 static unsigned int bitrate(struct usb_gadget *g)
105 {
106         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
107                 return 13 * 512 * 8 * 1000 * 8;
108         else
109                 return 19 *  64 * 1 * 1000 * 8;
110 }
111
112 /*-------------------------------------------------------------------------*/
113
114 /*
115  */
116
117 #define LOG2_STATUS_INTERVAL_MSEC       5       /* 1 << 5 == 32 msec */
118 #define STATUS_BYTECOUNT                8       /* 8 bytes data */
119
120
121 /* interface descriptor: */
122
123 static struct usb_interface_descriptor rndis_control_intf __initdata = {
124         .bLength =              sizeof rndis_control_intf,
125         .bDescriptorType =      USB_DT_INTERFACE,
126
127         /* .bInterfaceNumber = DYNAMIC */
128         /* status endpoint is optional; this could be patched later */
129         .bNumEndpoints =        1,
130 #ifdef CONFIG_USB_ANDROID_RNDIS_WCEIS
131         /* "Wireless" RNDIS; auto-detected by Windows */
132         .bInterfaceClass =      USB_CLASS_WIRELESS_CONTROLLER,
133         .bInterfaceSubClass = 1,
134         .bInterfaceProtocol =   3,
135 #else
136         .bInterfaceClass =      USB_CLASS_COMM,
137         .bInterfaceSubClass =   USB_CDC_SUBCLASS_ACM,
138         .bInterfaceProtocol =   USB_CDC_ACM_PROTO_VENDOR,
139 #endif
140         /* .iInterface = DYNAMIC */
141 };
142
143 static struct usb_cdc_header_desc header_desc __initdata = {
144         .bLength =              sizeof header_desc,
145         .bDescriptorType =      USB_DT_CS_INTERFACE,
146         .bDescriptorSubType =   USB_CDC_HEADER_TYPE,
147
148         .bcdCDC =               cpu_to_le16(0x0110),
149 };
150
151 static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
152         .bLength =              sizeof call_mgmt_descriptor,
153         .bDescriptorType =      USB_DT_CS_INTERFACE,
154         .bDescriptorSubType =   USB_CDC_CALL_MANAGEMENT_TYPE,
155
156         .bmCapabilities =       0x00,
157         .bDataInterface =       0x01,
158 };
159
160 static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
161         .bLength =              sizeof acm_descriptor,
162         .bDescriptorType =      USB_DT_CS_INTERFACE,
163         .bDescriptorSubType =   USB_CDC_ACM_TYPE,
164
165         .bmCapabilities =       0x00,
166 };
167
168 static struct usb_cdc_union_desc rndis_union_desc __initdata = {
169         .bLength =              sizeof(rndis_union_desc),
170         .bDescriptorType =      USB_DT_CS_INTERFACE,
171         .bDescriptorSubType =   USB_CDC_UNION_TYPE,
172         /* .bMasterInterface0 = DYNAMIC */
173         /* .bSlaveInterface0 =  DYNAMIC */
174 };
175
176 /* the data interface has two bulk endpoints */
177
178 static struct usb_interface_descriptor rndis_data_intf __initdata = {
179         .bLength =              sizeof rndis_data_intf,
180         .bDescriptorType =      USB_DT_INTERFACE,
181
182         /* .bInterfaceNumber = DYNAMIC */
183         .bNumEndpoints =        2,
184         .bInterfaceClass =      USB_CLASS_CDC_DATA,
185         .bInterfaceSubClass =   0,
186         .bInterfaceProtocol =   0,
187         /* .iInterface = DYNAMIC */
188 };
189
190 /* full speed support: */
191
192 static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
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 =            1 << LOG2_STATUS_INTERVAL_MSEC,
200 };
201
202 static struct usb_endpoint_descriptor fs_in_desc __initdata = {
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 __initdata = {
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[] __initdata = {
219         /* control interface matches ACM, not Ethernet */
220         (struct usb_descriptor_header *) &rndis_control_intf,
221         (struct usb_descriptor_header *) &header_desc,
222         (struct usb_descriptor_header *) &call_mgmt_descriptor,
223         (struct usb_descriptor_header *) &acm_descriptor,
224         (struct usb_descriptor_header *) &rndis_union_desc,
225         (struct usb_descriptor_header *) &fs_notify_desc,
226         /* data interface has no altsetting */
227         (struct usb_descriptor_header *) &rndis_data_intf,
228         (struct usb_descriptor_header *) &fs_in_desc,
229         (struct usb_descriptor_header *) &fs_out_desc,
230         NULL,
231 };
232
233 /* high speed support: */
234
235 static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
236         .bLength =              USB_DT_ENDPOINT_SIZE,
237         .bDescriptorType =      USB_DT_ENDPOINT,
238
239         .bEndpointAddress =     USB_DIR_IN,
240         .bmAttributes =         USB_ENDPOINT_XFER_INT,
241         .wMaxPacketSize =       cpu_to_le16(STATUS_BYTECOUNT),
242         .bInterval =            LOG2_STATUS_INTERVAL_MSEC + 4,
243 };
244 static struct usb_endpoint_descriptor hs_in_desc __initdata = {
245         .bLength =              USB_DT_ENDPOINT_SIZE,
246         .bDescriptorType =      USB_DT_ENDPOINT,
247
248         .bEndpointAddress =     USB_DIR_IN,
249         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
250         .wMaxPacketSize =       cpu_to_le16(512),
251 };
252
253 static struct usb_endpoint_descriptor hs_out_desc __initdata = {
254         .bLength =              USB_DT_ENDPOINT_SIZE,
255         .bDescriptorType =      USB_DT_ENDPOINT,
256
257         .bEndpointAddress =     USB_DIR_OUT,
258         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
259         .wMaxPacketSize =       cpu_to_le16(512),
260 };
261
262 static struct usb_descriptor_header *eth_hs_function[] __initdata = {
263         /* control interface matches ACM, not Ethernet */
264         (struct usb_descriptor_header *) &rndis_control_intf,
265         (struct usb_descriptor_header *) &header_desc,
266         (struct usb_descriptor_header *) &call_mgmt_descriptor,
267         (struct usb_descriptor_header *) &acm_descriptor,
268         (struct usb_descriptor_header *) &rndis_union_desc,
269         (struct usb_descriptor_header *) &hs_notify_desc,
270         /* data interface has no altsetting */
271         (struct usb_descriptor_header *) &rndis_data_intf,
272         (struct usb_descriptor_header *) &hs_in_desc,
273         (struct usb_descriptor_header *) &hs_out_desc,
274         NULL,
275 };
276
277 /* string descriptors: */
278
279 static struct usb_string rndis_string_defs[] = {
280         [0].s = "RNDIS Communications Control",
281         [1].s = "RNDIS Ethernet Data",
282         {  } /* end of list */
283 };
284
285 static struct usb_gadget_strings rndis_string_table = {
286         .language =             0x0409, /* en-us */
287         .strings =              rndis_string_defs,
288 };
289
290 static struct usb_gadget_strings *rndis_strings[] = {
291         &rndis_string_table,
292         NULL,
293 };
294
295 #ifdef CONFIG_USB_ANDROID_RNDIS
296 static struct usb_ether_platform_data *rndis_pdata;
297 #endif
298
299 /*-------------------------------------------------------------------------*/
300
301 static struct sk_buff *rndis_add_header(struct gether *port,
302                                         struct sk_buff *skb)
303 {
304         struct sk_buff *skb2;
305
306         skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
307         if (skb2)
308                 rndis_add_hdr(skb2);
309
310         dev_kfree_skb_any(skb);
311         return skb2;
312 }
313
314 static void rndis_response_available(void *_rndis)
315 {
316         struct f_rndis                  *rndis = _rndis;
317         struct usb_request              *req = rndis->notify_req;
318         struct usb_composite_dev        *cdev = rndis->port.func.config->cdev;
319         __le32                          *data = req->buf;
320         int                             status;
321
322         if (atomic_inc_return(&rndis->notify_count) != 1)
323                 return;
324
325         /* Send RNDIS RESPONSE_AVAILABLE notification; a
326          * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
327          *
328          * This is the only notification defined by RNDIS.
329          */
330         data[0] = cpu_to_le32(1);
331         data[1] = cpu_to_le32(0);
332
333         status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
334         if (status) {
335                 atomic_dec(&rndis->notify_count);
336                 DBG(cdev, "notify/0 --> %d\n", status);
337         }
338 }
339
340 static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
341 {
342         struct f_rndis                  *rndis = req->context;
343         struct usb_composite_dev        *cdev = rndis->port.func.config->cdev;
344         int                             status = req->status;
345
346         /* after TX:
347          *  - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
348          *  - RNDIS_RESPONSE_AVAILABLE (status/irq)
349          */
350         switch (status) {
351         case -ECONNRESET:
352         case -ESHUTDOWN:
353                 /* connection gone */
354                 atomic_set(&rndis->notify_count, 0);
355                 break;
356         default:
357                 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
358                         ep->name, status,
359                         req->actual, req->length);
360                 /* FALLTHROUGH */
361         case 0:
362                 if (ep != rndis->notify)
363                         break;
364
365                 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
366                  * notifications by resending until we're done
367                  */
368                 if (atomic_dec_and_test(&rndis->notify_count))
369                         break;
370                 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
371                 if (status) {
372                         atomic_dec(&rndis->notify_count);
373                         DBG(cdev, "notify/1 --> %d\n", status);
374                 }
375                 break;
376         }
377 }
378
379 static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
380 {
381         struct f_rndis                  *rndis = req->context;
382         struct usb_composite_dev        *cdev = rndis->port.func.config->cdev;
383         int                             status;
384
385         /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
386 //      spin_lock(&dev->lock);
387         status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
388         if (status < 0)
389                 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
390                         status, req->actual, req->length);
391 //      spin_unlock(&dev->lock);
392 }
393
394 static int
395 rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
396 {
397         struct f_rndis          *rndis = func_to_rndis(f);
398         struct usb_composite_dev *cdev = f->config->cdev;
399         struct usb_request      *req = cdev->req;
400         int                     value = -EOPNOTSUPP;
401         u16                     w_index = le16_to_cpu(ctrl->wIndex);
402         u16                     w_value = le16_to_cpu(ctrl->wValue);
403         u16                     w_length = le16_to_cpu(ctrl->wLength);
404
405         /* composite driver infrastructure handles everything except
406          * CDC class messages; interface activation uses set_alt().
407          */
408         switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
409
410         /* RNDIS uses the CDC command encapsulation mechanism to implement
411          * an RPC scheme, with much getting/setting of attributes by OID.
412          */
413         case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
414                         | USB_CDC_SEND_ENCAPSULATED_COMMAND:
415                 if (w_value || w_index != rndis->ctrl_id)
416                         goto invalid;
417                 /* read the request; process it later */
418                 value = w_length;
419                 req->complete = rndis_command_complete;
420                 req->context = rndis;
421                 /* later, rndis_response_available() sends a notification */
422                 break;
423
424         case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
425                         | USB_CDC_GET_ENCAPSULATED_RESPONSE:
426                 if (w_value || w_index != rndis->ctrl_id)
427                         goto invalid;
428                 else {
429                         u8 *buf;
430                         u32 n;
431
432                         /* return the result */
433                         buf = rndis_get_next_response(rndis->config, &n);
434                         if (buf) {
435                                 memcpy(req->buf, buf, n);
436                                 req->complete = rndis_response_complete;
437                                 rndis_free_response(rndis->config, buf);
438                                 value = n;
439                         }
440                         /* else stalls ... spec says to avoid that */
441                 }
442                 break;
443
444         default:
445 invalid:
446                 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
447                         ctrl->bRequestType, ctrl->bRequest,
448                         w_value, w_index, w_length);
449         }
450
451         /* respond with data transfer or status phase? */
452         if (value >= 0) {
453                 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
454                         ctrl->bRequestType, ctrl->bRequest,
455                         w_value, w_index, w_length);
456                 req->zero = (value < w_length);
457                 req->length = value;
458                 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
459                 if (value < 0)
460                         ERROR(cdev, "rndis response on err %d\n", value);
461         }
462
463         /* device either stalls (value < 0) or reports success */
464         return value;
465 }
466
467
468 static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
469 {
470         struct f_rndis          *rndis = func_to_rndis(f);
471         struct usb_composite_dev *cdev = f->config->cdev;
472
473         /* we know alt == 0 */
474
475         if (intf == rndis->ctrl_id) {
476                 if (rndis->notify->driver_data) {
477                         VDBG(cdev, "reset rndis control %d\n", intf);
478                         usb_ep_disable(rndis->notify);
479                 } else {
480                         VDBG(cdev, "init rndis ctrl %d\n", intf);
481                 }
482                 rndis->notify_desc = ep_choose(cdev->gadget,
483                                 rndis->hs.notify,
484                                 rndis->fs.notify);
485                 usb_ep_enable(rndis->notify, rndis->notify_desc);
486                 rndis->notify->driver_data = rndis;
487
488         } else if (intf == rndis->data_id) {
489                 struct net_device       *net;
490
491                 if (rndis->port.in_ep->driver_data) {
492                         DBG(cdev, "reset rndis\n");
493                         gether_disconnect(&rndis->port);
494                 }
495
496                 if (!rndis->port.in) {
497                         DBG(cdev, "init rndis\n");
498                 }
499                 rndis->port.in = ep_choose(cdev->gadget,
500                                 rndis->hs.in, rndis->fs.in);
501                 rndis->port.out = ep_choose(cdev->gadget,
502                                 rndis->hs.out, rndis->fs.out);
503
504                 /* Avoid ZLPs; they can be troublesome. */
505                 rndis->port.is_zlp_ok = false;
506
507                 /* RNDIS should be in the "RNDIS uninitialized" state,
508                  * either never activated or after rndis_uninit().
509                  *
510                  * We don't want data to flow here until a nonzero packet
511                  * filter is set, at which point it enters "RNDIS data
512                  * initialized" state ... but we do want the endpoints
513                  * to be activated.  It's a strange little state.
514                  *
515                  * REVISIT the RNDIS gadget code has done this wrong for a
516                  * very long time.  We need another call to the link layer
517                  * code -- gether_updown(...bool) maybe -- to do it right.
518                  */
519                 rndis->port.cdc_filter = 0;
520
521                 DBG(cdev, "RNDIS RX/TX early activation ... \n");
522                 net = gether_connect(&rndis->port);
523                 if (IS_ERR(net))
524                         return PTR_ERR(net);
525
526                 rndis_set_param_dev(rndis->config, net,
527                                 &rndis->port.cdc_filter);
528         } else
529                 goto fail;
530
531         return 0;
532 fail:
533         return -EINVAL;
534 }
535
536 static void rndis_disable(struct usb_function *f)
537 {
538         struct f_rndis          *rndis = func_to_rndis(f);
539         struct usb_composite_dev *cdev = f->config->cdev;
540
541         if (!rndis->notify->driver_data)
542                 return;
543
544         DBG(cdev, "rndis deactivated\n");
545
546         rndis_uninit(rndis->config);
547         gether_disconnect(&rndis->port);
548
549         usb_ep_disable(rndis->notify);
550         rndis->notify->driver_data = NULL;
551 }
552
553 /*-------------------------------------------------------------------------*/
554
555 /*
556  * This isn't quite the same mechanism as CDC Ethernet, since the
557  * notification scheme passes less data, but the same set of link
558  * states must be tested.  A key difference is that altsettings are
559  * not used to tell whether the link should send packets or not.
560  */
561
562 static void rndis_open(struct gether *geth)
563 {
564         struct f_rndis          *rndis = func_to_rndis(&geth->func);
565         struct usb_composite_dev *cdev = geth->func.config->cdev;
566
567         DBG(cdev, "%s\n", __func__);
568
569         rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
570                                 bitrate(cdev->gadget) / 100);
571         rndis_signal_connect(rndis->config);
572 }
573
574 static void rndis_close(struct gether *geth)
575 {
576         struct f_rndis          *rndis = func_to_rndis(&geth->func);
577
578         DBG(geth->func.config->cdev, "%s\n", __func__);
579
580         rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
581         rndis_signal_disconnect(rndis->config);
582 }
583
584 /*-------------------------------------------------------------------------*/
585
586 /* ethernet function driver setup/binding */
587
588 static int __init
589 rndis_bind(struct usb_configuration *c, struct usb_function *f)
590 {
591         struct usb_composite_dev *cdev = c->cdev;
592         struct f_rndis          *rndis = func_to_rndis(f);
593         int                     status;
594         struct usb_ep           *ep;
595
596         /* allocate instance-specific interface IDs */
597         status = usb_interface_id(c, f);
598         if (status < 0)
599                 goto fail;
600         rndis->ctrl_id = status;
601
602         rndis_control_intf.bInterfaceNumber = status;
603         rndis_union_desc.bMasterInterface0 = status;
604
605         status = usb_interface_id(c, f);
606         if (status < 0)
607                 goto fail;
608         rndis->data_id = status;
609
610         rndis_data_intf.bInterfaceNumber = status;
611         rndis_union_desc.bSlaveInterface0 = status;
612
613         status = -ENODEV;
614
615         /* allocate instance-specific endpoints */
616         ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
617         if (!ep)
618                 goto fail;
619         rndis->port.in_ep = ep;
620         ep->driver_data = cdev; /* claim */
621
622         ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
623         if (!ep)
624                 goto fail;
625         rndis->port.out_ep = ep;
626         ep->driver_data = cdev; /* claim */
627
628         /* NOTE:  a status/notification endpoint is, strictly speaking,
629          * optional.  We don't treat it that way though!  It's simpler,
630          * and some newer profiles don't treat it as optional.
631          */
632         ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
633         if (!ep)
634                 goto fail;
635         rndis->notify = ep;
636         ep->driver_data = cdev; /* claim */
637
638         status = -ENOMEM;
639
640         /* allocate notification request and buffer */
641         rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
642         if (!rndis->notify_req)
643                 goto fail;
644         rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
645         if (!rndis->notify_req->buf)
646                 goto fail;
647         rndis->notify_req->length = STATUS_BYTECOUNT;
648         rndis->notify_req->context = rndis;
649         rndis->notify_req->complete = rndis_response_complete;
650
651         /* copy descriptors, and track endpoint copies */
652         f->descriptors = usb_copy_descriptors(eth_fs_function);
653         if (!f->descriptors)
654                 goto fail;
655
656         rndis->fs.in = usb_find_endpoint(eth_fs_function,
657                         f->descriptors, &fs_in_desc);
658         rndis->fs.out = usb_find_endpoint(eth_fs_function,
659                         f->descriptors, &fs_out_desc);
660         rndis->fs.notify = usb_find_endpoint(eth_fs_function,
661                         f->descriptors, &fs_notify_desc);
662
663         /* support all relevant hardware speeds... we expect that when
664          * hardware is dual speed, all bulk-capable endpoints work at
665          * both speeds
666          */
667         if (gadget_is_dualspeed(c->cdev->gadget)) {
668                 hs_in_desc.bEndpointAddress =
669                                 fs_in_desc.bEndpointAddress;
670                 hs_out_desc.bEndpointAddress =
671                                 fs_out_desc.bEndpointAddress;
672                 hs_notify_desc.bEndpointAddress =
673                                 fs_notify_desc.bEndpointAddress;
674
675                 /* copy descriptors, and track endpoint copies */
676                 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
677
678                 if (!f->hs_descriptors)
679                         goto fail;
680
681                 rndis->hs.in = usb_find_endpoint(eth_hs_function,
682                                 f->hs_descriptors, &hs_in_desc);
683                 rndis->hs.out = usb_find_endpoint(eth_hs_function,
684                                 f->hs_descriptors, &hs_out_desc);
685                 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
686                                 f->hs_descriptors, &hs_notify_desc);
687         }
688
689         rndis->port.open = rndis_open;
690         rndis->port.close = rndis_close;
691
692         status = rndis_register(rndis_response_available, rndis);
693         if (status < 0)
694                 goto fail;
695         rndis->config = status;
696
697         rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
698         rndis_set_host_mac(rndis->config, rndis->ethaddr);
699
700 #ifdef CONFIG_USB_ANDROID_RNDIS
701         if (rndis_pdata) {
702                 if (rndis_set_param_vendor(rndis->config, rndis_pdata->vendorID,
703                                         rndis_pdata->vendorDescr))
704                         goto fail;
705         }
706 #endif
707
708         /* NOTE:  all that is done without knowing or caring about
709          * the network link ... which is unavailable to this code
710          * until we're activated via set_alt().
711          */
712
713         DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
714                         gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
715                         rndis->port.in_ep->name, rndis->port.out_ep->name,
716                         rndis->notify->name);
717         return 0;
718
719 fail:
720         if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
721                 usb_free_descriptors(f->hs_descriptors);
722         if (f->descriptors)
723                 usb_free_descriptors(f->descriptors);
724
725         if (rndis->notify_req) {
726                 kfree(rndis->notify_req->buf);
727                 usb_ep_free_request(rndis->notify, rndis->notify_req);
728         }
729
730         /* we might as well release our claims on endpoints */
731         if (rndis->notify)
732                 rndis->notify->driver_data = NULL;
733         if (rndis->port.out)
734                 rndis->port.out_ep->driver_data = NULL;
735         if (rndis->port.in)
736                 rndis->port.in_ep->driver_data = NULL;
737
738         ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
739
740         return status;
741 }
742
743 static void
744 rndis_unbind(struct usb_configuration *c, struct usb_function *f)
745 {
746         struct f_rndis          *rndis = func_to_rndis(f);
747
748         rndis_deregister(rndis->config);
749         rndis_exit();
750
751         if (gadget_is_dualspeed(c->cdev->gadget))
752                 usb_free_descriptors(f->hs_descriptors);
753         usb_free_descriptors(f->descriptors);
754
755         kfree(rndis->notify_req->buf);
756         usb_ep_free_request(rndis->notify, rndis->notify_req);
757
758         kfree(rndis);
759 }
760
761 /* Some controllers can't support RNDIS ... */
762 static inline bool can_support_rndis(struct usb_configuration *c)
763 {
764         /* only two endpoints on sa1100 */
765         if (gadget_is_sa1100(c->cdev->gadget))
766                 return false;
767
768         /* everything else is *presumably* fine */
769         return true;
770 }
771
772 /**
773  * rndis_bind_config - add RNDIS network link to a configuration
774  * @c: the configuration to support the network link
775  * @ethaddr: a buffer in which the ethernet address of the host side
776  *      side of the link was recorded
777  * Context: single threaded during gadget setup
778  *
779  * Returns zero on success, else negative errno.
780  *
781  * Caller must have called @gether_setup().  Caller is also responsible
782  * for calling @gether_cleanup() before module unload.
783  */
784 int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
785 {
786         struct f_rndis  *rndis;
787         int             status;
788
789         if (!can_support_rndis(c) || !ethaddr)
790                 return -EINVAL;
791
792         /* maybe allocate device-global string IDs */
793         if (rndis_string_defs[0].id == 0) {
794
795                 /* ... and setup RNDIS itself */
796                 status = rndis_init();
797                 if (status < 0)
798                         return status;
799
800                 /* control interface label */
801                 status = usb_string_id(c->cdev);
802                 if (status < 0)
803                         return status;
804                 rndis_string_defs[0].id = status;
805                 rndis_control_intf.iInterface = status;
806
807                 /* data interface label */
808                 status = usb_string_id(c->cdev);
809                 if (status < 0)
810                         return status;
811                 rndis_string_defs[1].id = status;
812                 rndis_data_intf.iInterface = status;
813         }
814
815         /* allocate and initialize one new instance */
816         status = -ENOMEM;
817         rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
818         if (!rndis)
819                 goto fail;
820
821         memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
822
823         /* RNDIS activates when the host changes this filter */
824         rndis->port.cdc_filter = 0;
825
826         /* RNDIS has special (and complex) framing */
827         rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
828         rndis->port.wrap = rndis_add_header;
829         rndis->port.unwrap = rndis_rm_hdr;
830
831         rndis->port.func.name = "rndis";
832         rndis->port.func.strings = rndis_strings;
833         /* descriptors are per-instance copies */
834         rndis->port.func.bind = rndis_bind;
835         rndis->port.func.unbind = rndis_unbind;
836         rndis->port.func.set_alt = rndis_set_alt;
837         rndis->port.func.setup = rndis_setup;
838         rndis->port.func.disable = rndis_disable;
839
840 #ifdef CONFIG_USB_ANDROID_RNDIS
841         /* start disabled */
842         rndis->port.func.disabled = 1;
843 #endif
844
845         status = usb_add_function(c, &rndis->port.func);
846         if (status) {
847                 kfree(rndis);
848 fail:
849                 rndis_exit();
850         }
851         return status;
852 }
853
854 #ifdef CONFIG_USB_ANDROID_RNDIS
855 #include "rndis.c"
856
857 static int __init rndis_probe(struct platform_device *pdev)
858 {
859         rndis_pdata = pdev->dev.platform_data;
860         return 0;
861 }
862
863 static struct platform_driver rndis_platform_driver = {
864         .driver = { .name = "rndis", },
865         .probe = rndis_probe,
866 };
867
868 int rndis_function_bind_config(struct usb_configuration *c)
869 {
870         int ret;
871
872         if (!rndis_pdata) {
873                 printk(KERN_ERR "rndis_pdata null in rndis_function_bind_config\n");
874                 return -1;
875         }
876
877         printk(KERN_INFO
878                 "rndis_function_bind_config MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
879                 rndis_pdata->ethaddr[0], rndis_pdata->ethaddr[1],
880                 rndis_pdata->ethaddr[2], rndis_pdata->ethaddr[3],
881                 rndis_pdata->ethaddr[4], rndis_pdata->ethaddr[5]);
882
883         ret = gether_setup(c->cdev->gadget, rndis_pdata->ethaddr);
884         if (ret == 0)
885                 ret = rndis_bind_config(c, rndis_pdata->ethaddr);
886         return ret;
887 }
888
889 static struct android_usb_function rndis_function = {
890         .name = "rndis",
891         .bind_config = rndis_function_bind_config,
892 };
893
894 static int __init init(void)
895 {
896         printk(KERN_INFO "f_rndis init\n");
897         platform_driver_register(&rndis_platform_driver);
898         android_register_function(&rndis_function);
899         return 0;
900 }
901 module_init(init);
902
903 #endif /* CONFIG_USB_ANDROID_RNDIS */