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