blob: 704d1d94f72a6c441c702ba1a3e2065a8ac92166 [file] [log] [blame]
David Brownell45fe3b82008-06-19 18:20:04 -07001/*
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
Michal Nazarewiczb97503f2009-10-28 16:57:30 +01007 * Copyright (C) 2009 Samsung Electronics
8 * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
David Brownell45fe3b82008-06-19 18:20:04 -07009 *
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.
David Brownell45fe3b82008-06-19 18:20:04 -070014 */
15
16/* #define VERBOSE_DEBUG */
17
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Brownell45fe3b82008-06-19 18:20:04 -070019#include <linux/kernel.h>
20#include <linux/device.h>
21#include <linux/etherdevice.h>
22
Arun Sharma60063492011-07-26 16:09:06 -070023#include <linux/atomic.h>
David Brownell45fe3b82008-06-19 18:20:04 -070024
25#include "u_ether.h"
26#include "rndis.h"
27
28
29/*
30 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
31 * been promoted instead of the standard CDC Ethernet. The published RNDIS
32 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
33 * ActiveSync have even worse status in terms of specification.
34 *
35 * In short: it's a protocol controlled by (and for) Microsoft, not for an
36 * Open ecosystem or markets. Linux supports it *only* because Microsoft
37 * doesn't support the CDC Ethernet standard.
38 *
39 * The RNDIS data transfer model is complex, with multiple Ethernet packets
40 * per USB message, and out of band data. The control model is built around
41 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
42 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
43 * useless (they're ignored). RNDIS expects to be the only function in its
44 * configuration, so it's no real help if you need composite devices; and
45 * it expects to be the first configuration too.
46 *
47 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
48 * discount the fluff that its RPC can be made to deliver: it doesn't need
49 * a NOP altsetting for the data interface. That lets it work on some of the
50 * "so smart it's stupid" hardware which takes over configuration changes
51 * from the software, and adds restrictions like "no altsettings".
52 *
53 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
54 * have all sorts of contrary-to-specification oddities that can prevent
55 * them from working sanely. Since bugfixes (or accurate specs, letting
56 * Linux work around those bugs) are unlikely to ever come from MSFT, you
57 * may want to avoid using RNDIS on purely operational grounds.
58 *
59 * Omissions from the RNDIS 1.0 specification include:
60 *
61 * - Power management ... references data that's scattered around lots
62 * of other documentation, which is incorrect/incomplete there too.
63 *
64 * - There are various undocumented protocol requirements, like the need
65 * to send garbage in some control-OUT messages.
66 *
67 * - MS-Windows drivers sometimes emit undocumented requests.
68 */
69
David Brownell45fe3b82008-06-19 18:20:04 -070070struct f_rndis {
71 struct gether port;
72 u8 ctrl_id, data_id;
73 u8 ethaddr[ETH_ALEN];
74 int config;
75
David Brownell45fe3b82008-06-19 18:20:04 -070076 struct usb_ep *notify;
David Brownell45fe3b82008-06-19 18:20:04 -070077 struct usb_request *notify_req;
78 atomic_t notify_count;
79};
80
81static inline struct f_rndis *func_to_rndis(struct usb_function *f)
82{
83 return container_of(f, struct f_rndis, port.func);
84}
85
86/* peak (theoretical) bulk transfer rate in bits-per-second */
87static unsigned int bitrate(struct usb_gadget *g)
88{
Paul Zimmerman04617db2011-06-27 14:13:18 -070089 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
90 return 13 * 1024 * 8 * 1000 * 8;
91 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
David Brownell45fe3b82008-06-19 18:20:04 -070092 return 13 * 512 * 8 * 1000 * 8;
93 else
Paul Zimmerman04617db2011-06-27 14:13:18 -070094 return 19 * 64 * 1 * 1000 * 8;
David Brownell45fe3b82008-06-19 18:20:04 -070095}
96
97/*-------------------------------------------------------------------------*/
98
99/*
100 */
101
102#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
103#define STATUS_BYTECOUNT 8 /* 8 bytes data */
104
105
106/* interface descriptor: */
107
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200108static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700109 .bLength = sizeof rndis_control_intf,
110 .bDescriptorType = USB_DT_INTERFACE,
111
112 /* .bInterfaceNumber = DYNAMIC */
113 /* status endpoint is optional; this could be patched later */
114 .bNumEndpoints = 1,
115 .bInterfaceClass = USB_CLASS_COMM,
116 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
117 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
118 /* .iInterface = DYNAMIC */
119};
120
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200121static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700122 .bLength = sizeof header_desc,
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
125
Harvey Harrison551509d2009-02-11 14:11:36 -0800126 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700127};
128
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200129static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700130 .bLength = sizeof call_mgmt_descriptor,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
133
134 .bmCapabilities = 0x00,
135 .bDataInterface = 0x01,
136};
137
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200138static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100139 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_ACM_TYPE,
142
143 .bmCapabilities = 0x00,
144};
145
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200146static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700147 .bLength = sizeof(rndis_union_desc),
148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_UNION_TYPE,
150 /* .bMasterInterface0 = DYNAMIC */
151 /* .bSlaveInterface0 = DYNAMIC */
152};
153
154/* the data interface has two bulk endpoints */
155
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200156static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700157 .bLength = sizeof rndis_data_intf,
158 .bDescriptorType = USB_DT_INTERFACE,
159
160 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700161 .bNumEndpoints = 2,
162 .bInterfaceClass = USB_CLASS_CDC_DATA,
163 .bInterfaceSubClass = 0,
164 .bInterfaceProtocol = 0,
165 /* .iInterface = DYNAMIC */
166};
167
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100168
169static struct usb_interface_assoc_descriptor
170rndis_iad_descriptor = {
171 .bLength = sizeof rndis_iad_descriptor,
172 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
173
174 .bFirstInterface = 0, /* XXX, hardcoded */
175 .bInterfaceCount = 2, // control + data
176 .bFunctionClass = USB_CLASS_COMM,
177 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
178 .bFunctionProtocol = USB_CDC_PROTO_NONE,
179 /* .iFunction = DYNAMIC */
180};
181
David Brownell45fe3b82008-06-19 18:20:04 -0700182/* full speed support: */
183
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200184static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800190 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700191 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
192};
193
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200194static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700195 .bLength = USB_DT_ENDPOINT_SIZE,
196 .bDescriptorType = USB_DT_ENDPOINT,
197
198 .bEndpointAddress = USB_DIR_IN,
199 .bmAttributes = USB_ENDPOINT_XFER_BULK,
200};
201
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200202static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
205
206 .bEndpointAddress = USB_DIR_OUT,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
208};
209
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200210static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100211 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700212
David Brownell45fe3b82008-06-19 18:20:04 -0700213 /* control interface matches ACM, not Ethernet */
214 (struct usb_descriptor_header *) &rndis_control_intf,
215 (struct usb_descriptor_header *) &header_desc,
216 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100217 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700218 (struct usb_descriptor_header *) &rndis_union_desc,
219 (struct usb_descriptor_header *) &fs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700220
David Brownell45fe3b82008-06-19 18:20:04 -0700221 /* data interface has no altsetting */
222 (struct usb_descriptor_header *) &rndis_data_intf,
223 (struct usb_descriptor_header *) &fs_in_desc,
224 (struct usb_descriptor_header *) &fs_out_desc,
225 NULL,
226};
227
228/* high speed support: */
229
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200230static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233
234 .bEndpointAddress = USB_DIR_IN,
235 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800236 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700237 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
238};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700239
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200240static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700241 .bLength = USB_DT_ENDPOINT_SIZE,
242 .bDescriptorType = USB_DT_ENDPOINT,
243
244 .bEndpointAddress = USB_DIR_IN,
245 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800246 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700247};
248
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200249static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700250 .bLength = USB_DT_ENDPOINT_SIZE,
251 .bDescriptorType = USB_DT_ENDPOINT,
252
253 .bEndpointAddress = USB_DIR_OUT,
254 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800255 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700256};
257
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200258static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100259 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700260
David Brownell45fe3b82008-06-19 18:20:04 -0700261 /* control interface matches ACM, not Ethernet */
262 (struct usb_descriptor_header *) &rndis_control_intf,
263 (struct usb_descriptor_header *) &header_desc,
264 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100265 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700266 (struct usb_descriptor_header *) &rndis_union_desc,
267 (struct usb_descriptor_header *) &hs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700268
David Brownell45fe3b82008-06-19 18:20:04 -0700269 /* data interface has no altsetting */
270 (struct usb_descriptor_header *) &rndis_data_intf,
271 (struct usb_descriptor_header *) &hs_in_desc,
272 (struct usb_descriptor_header *) &hs_out_desc,
273 NULL,
274};
275
Paul Zimmerman04617db2011-06-27 14:13:18 -0700276/* super speed support: */
277
278static struct usb_endpoint_descriptor ss_notify_desc = {
279 .bLength = USB_DT_ENDPOINT_SIZE,
280 .bDescriptorType = USB_DT_ENDPOINT,
281
282 .bEndpointAddress = USB_DIR_IN,
283 .bmAttributes = USB_ENDPOINT_XFER_INT,
284 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
285 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
286};
287
288static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
289 .bLength = sizeof ss_intr_comp_desc,
290 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
291
292 /* the following 3 values can be tweaked if necessary */
293 /* .bMaxBurst = 0, */
294 /* .bmAttributes = 0, */
295 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
296};
297
298static struct usb_endpoint_descriptor ss_in_desc = {
299 .bLength = USB_DT_ENDPOINT_SIZE,
300 .bDescriptorType = USB_DT_ENDPOINT,
301
302 .bEndpointAddress = USB_DIR_IN,
303 .bmAttributes = USB_ENDPOINT_XFER_BULK,
304 .wMaxPacketSize = cpu_to_le16(1024),
305};
306
307static struct usb_endpoint_descriptor ss_out_desc = {
308 .bLength = USB_DT_ENDPOINT_SIZE,
309 .bDescriptorType = USB_DT_ENDPOINT,
310
311 .bEndpointAddress = USB_DIR_OUT,
312 .bmAttributes = USB_ENDPOINT_XFER_BULK,
313 .wMaxPacketSize = cpu_to_le16(1024),
314};
315
316static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
317 .bLength = sizeof ss_bulk_comp_desc,
318 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
319
320 /* the following 2 values can be tweaked if necessary */
321 /* .bMaxBurst = 0, */
322 /* .bmAttributes = 0, */
323};
324
325static struct usb_descriptor_header *eth_ss_function[] = {
326 (struct usb_descriptor_header *) &rndis_iad_descriptor,
327
328 /* control interface matches ACM, not Ethernet */
329 (struct usb_descriptor_header *) &rndis_control_intf,
330 (struct usb_descriptor_header *) &header_desc,
331 (struct usb_descriptor_header *) &call_mgmt_descriptor,
332 (struct usb_descriptor_header *) &rndis_acm_descriptor,
333 (struct usb_descriptor_header *) &rndis_union_desc,
334 (struct usb_descriptor_header *) &ss_notify_desc,
335 (struct usb_descriptor_header *) &ss_intr_comp_desc,
336
337 /* data interface has no altsetting */
338 (struct usb_descriptor_header *) &rndis_data_intf,
339 (struct usb_descriptor_header *) &ss_in_desc,
340 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
341 (struct usb_descriptor_header *) &ss_out_desc,
342 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
343 NULL,
344};
345
David Brownell45fe3b82008-06-19 18:20:04 -0700346/* string descriptors: */
347
348static struct usb_string rndis_string_defs[] = {
349 [0].s = "RNDIS Communications Control",
350 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100351 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700352 { } /* end of list */
353};
354
355static struct usb_gadget_strings rndis_string_table = {
356 .language = 0x0409, /* en-us */
357 .strings = rndis_string_defs,
358};
359
360static struct usb_gadget_strings *rndis_strings[] = {
361 &rndis_string_table,
362 NULL,
363};
364
365/*-------------------------------------------------------------------------*/
366
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500367static struct sk_buff *rndis_add_header(struct gether *port,
368 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700369{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500370 struct sk_buff *skb2;
371
372 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
373 if (skb2)
374 rndis_add_hdr(skb2);
375
376 dev_kfree_skb_any(skb);
377 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700378}
379
380static void rndis_response_available(void *_rndis)
381{
382 struct f_rndis *rndis = _rndis;
383 struct usb_request *req = rndis->notify_req;
384 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
385 __le32 *data = req->buf;
386 int status;
387
Richard Röjforsff349502008-11-15 19:53:24 -0800388 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700389 return;
390
391 /* Send RNDIS RESPONSE_AVAILABLE notification; a
392 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
393 *
394 * This is the only notification defined by RNDIS.
395 */
396 data[0] = cpu_to_le32(1);
397 data[1] = cpu_to_le32(0);
398
399 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
400 if (status) {
401 atomic_dec(&rndis->notify_count);
402 DBG(cdev, "notify/0 --> %d\n", status);
403 }
404}
405
406static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
407{
408 struct f_rndis *rndis = req->context;
409 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
410 int status = req->status;
411
412 /* after TX:
413 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
414 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
415 */
416 switch (status) {
417 case -ECONNRESET:
418 case -ESHUTDOWN:
419 /* connection gone */
420 atomic_set(&rndis->notify_count, 0);
421 break;
422 default:
423 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
424 ep->name, status,
425 req->actual, req->length);
426 /* FALLTHROUGH */
427 case 0:
428 if (ep != rndis->notify)
429 break;
430
431 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
432 * notifications by resending until we're done
433 */
434 if (atomic_dec_and_test(&rndis->notify_count))
435 break;
436 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
437 if (status) {
438 atomic_dec(&rndis->notify_count);
439 DBG(cdev, "notify/1 --> %d\n", status);
440 }
441 break;
442 }
443}
444
445static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
446{
447 struct f_rndis *rndis = req->context;
448 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
449 int status;
450
451 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
452// spin_lock(&dev->lock);
453 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
454 if (status < 0)
455 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
456 status, req->actual, req->length);
457// spin_unlock(&dev->lock);
458}
459
460static int
461rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
462{
463 struct f_rndis *rndis = func_to_rndis(f);
464 struct usb_composite_dev *cdev = f->config->cdev;
465 struct usb_request *req = cdev->req;
466 int value = -EOPNOTSUPP;
467 u16 w_index = le16_to_cpu(ctrl->wIndex);
468 u16 w_value = le16_to_cpu(ctrl->wValue);
469 u16 w_length = le16_to_cpu(ctrl->wLength);
470
471 /* composite driver infrastructure handles everything except
472 * CDC class messages; interface activation uses set_alt().
473 */
474 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
475
476 /* RNDIS uses the CDC command encapsulation mechanism to implement
477 * an RPC scheme, with much getting/setting of attributes by OID.
478 */
479 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
480 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300481 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700482 goto invalid;
483 /* read the request; process it later */
484 value = w_length;
485 req->complete = rndis_command_complete;
486 req->context = rndis;
487 /* later, rndis_response_available() sends a notification */
488 break;
489
490 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
491 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
492 if (w_value || w_index != rndis->ctrl_id)
493 goto invalid;
494 else {
495 u8 *buf;
496 u32 n;
497
498 /* return the result */
499 buf = rndis_get_next_response(rndis->config, &n);
500 if (buf) {
501 memcpy(req->buf, buf, n);
502 req->complete = rndis_response_complete;
503 rndis_free_response(rndis->config, buf);
504 value = n;
505 }
506 /* else stalls ... spec says to avoid that */
507 }
508 break;
509
510 default:
511invalid:
512 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
513 ctrl->bRequestType, ctrl->bRequest,
514 w_value, w_index, w_length);
515 }
516
517 /* respond with data transfer or status phase? */
518 if (value >= 0) {
519 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
520 ctrl->bRequestType, ctrl->bRequest,
521 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700522 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700523 req->length = value;
524 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
525 if (value < 0)
526 ERROR(cdev, "rndis response on err %d\n", value);
527 }
528
529 /* device either stalls (value < 0) or reports success */
530 return value;
531}
532
533
534static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
535{
536 struct f_rndis *rndis = func_to_rndis(f);
537 struct usb_composite_dev *cdev = f->config->cdev;
538
539 /* we know alt == 0 */
540
541 if (intf == rndis->ctrl_id) {
542 if (rndis->notify->driver_data) {
543 VDBG(cdev, "reset rndis control %d\n", intf);
544 usb_ep_disable(rndis->notify);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300545 }
546 if (!rndis->notify->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700547 VDBG(cdev, "init rndis ctrl %d\n", intf);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300548 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
549 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700550 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300551 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700552 rndis->notify->driver_data = rndis;
553
554 } else if (intf == rndis->data_id) {
555 struct net_device *net;
556
557 if (rndis->port.in_ep->driver_data) {
558 DBG(cdev, "reset rndis\n");
559 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530560 }
561
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300562 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700563 DBG(cdev, "init rndis\n");
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300564 if (config_ep_by_speed(cdev->gadget, f,
565 rndis->port.in_ep) ||
566 config_ep_by_speed(cdev->gadget, f,
567 rndis->port.out_ep)) {
568 rndis->port.in_ep->desc = NULL;
569 rndis->port.out_ep->desc = NULL;
570 goto fail;
571 }
David Brownell45fe3b82008-06-19 18:20:04 -0700572 }
573
574 /* Avoid ZLPs; they can be troublesome. */
575 rndis->port.is_zlp_ok = false;
576
577 /* RNDIS should be in the "RNDIS uninitialized" state,
578 * either never activated or after rndis_uninit().
579 *
580 * We don't want data to flow here until a nonzero packet
581 * filter is set, at which point it enters "RNDIS data
582 * initialized" state ... but we do want the endpoints
583 * to be activated. It's a strange little state.
584 *
585 * REVISIT the RNDIS gadget code has done this wrong for a
586 * very long time. We need another call to the link layer
587 * code -- gether_updown(...bool) maybe -- to do it right.
588 */
589 rndis->port.cdc_filter = 0;
590
591 DBG(cdev, "RNDIS RX/TX early activation ... \n");
592 net = gether_connect(&rndis->port);
593 if (IS_ERR(net))
594 return PTR_ERR(net);
595
596 rndis_set_param_dev(rndis->config, net,
597 &rndis->port.cdc_filter);
598 } else
599 goto fail;
600
601 return 0;
602fail:
603 return -EINVAL;
604}
605
606static void rndis_disable(struct usb_function *f)
607{
608 struct f_rndis *rndis = func_to_rndis(f);
609 struct usb_composite_dev *cdev = f->config->cdev;
610
611 if (!rndis->notify->driver_data)
612 return;
613
614 DBG(cdev, "rndis deactivated\n");
615
616 rndis_uninit(rndis->config);
617 gether_disconnect(&rndis->port);
618
619 usb_ep_disable(rndis->notify);
620 rndis->notify->driver_data = NULL;
621}
622
623/*-------------------------------------------------------------------------*/
624
625/*
626 * This isn't quite the same mechanism as CDC Ethernet, since the
627 * notification scheme passes less data, but the same set of link
628 * states must be tested. A key difference is that altsettings are
629 * not used to tell whether the link should send packets or not.
630 */
631
632static void rndis_open(struct gether *geth)
633{
634 struct f_rndis *rndis = func_to_rndis(&geth->func);
635 struct usb_composite_dev *cdev = geth->func.config->cdev;
636
637 DBG(cdev, "%s\n", __func__);
638
639 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
640 bitrate(cdev->gadget) / 100);
641 rndis_signal_connect(rndis->config);
642}
643
644static void rndis_close(struct gether *geth)
645{
646 struct f_rndis *rndis = func_to_rndis(&geth->func);
647
648 DBG(geth->func.config->cdev, "%s\n", __func__);
649
650 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
651 rndis_signal_disconnect(rndis->config);
652}
653
654/*-------------------------------------------------------------------------*/
655
656/* ethernet function driver setup/binding */
657
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200658static int
David Brownell45fe3b82008-06-19 18:20:04 -0700659rndis_bind(struct usb_configuration *c, struct usb_function *f)
660{
661 struct usb_composite_dev *cdev = c->cdev;
662 struct f_rndis *rndis = func_to_rndis(f);
663 int status;
664 struct usb_ep *ep;
665
666 /* allocate instance-specific interface IDs */
667 status = usb_interface_id(c, f);
668 if (status < 0)
669 goto fail;
670 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100671 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700672
673 rndis_control_intf.bInterfaceNumber = status;
674 rndis_union_desc.bMasterInterface0 = status;
675
676 status = usb_interface_id(c, f);
677 if (status < 0)
678 goto fail;
679 rndis->data_id = status;
680
681 rndis_data_intf.bInterfaceNumber = status;
682 rndis_union_desc.bSlaveInterface0 = status;
683
684 status = -ENODEV;
685
686 /* allocate instance-specific endpoints */
687 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
688 if (!ep)
689 goto fail;
690 rndis->port.in_ep = ep;
691 ep->driver_data = cdev; /* claim */
692
693 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
694 if (!ep)
695 goto fail;
696 rndis->port.out_ep = ep;
697 ep->driver_data = cdev; /* claim */
698
699 /* NOTE: a status/notification endpoint is, strictly speaking,
700 * optional. We don't treat it that way though! It's simpler,
701 * and some newer profiles don't treat it as optional.
702 */
703 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
704 if (!ep)
705 goto fail;
706 rndis->notify = ep;
707 ep->driver_data = cdev; /* claim */
708
709 status = -ENOMEM;
710
711 /* allocate notification request and buffer */
712 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
713 if (!rndis->notify_req)
714 goto fail;
715 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
716 if (!rndis->notify_req->buf)
717 goto fail;
718 rndis->notify_req->length = STATUS_BYTECOUNT;
719 rndis->notify_req->context = rndis;
720 rndis->notify_req->complete = rndis_response_complete;
721
722 /* copy descriptors, and track endpoint copies */
723 f->descriptors = usb_copy_descriptors(eth_fs_function);
724 if (!f->descriptors)
725 goto fail;
726
David Brownell45fe3b82008-06-19 18:20:04 -0700727 /* support all relevant hardware speeds... we expect that when
728 * hardware is dual speed, all bulk-capable endpoints work at
729 * both speeds
730 */
731 if (gadget_is_dualspeed(c->cdev->gadget)) {
732 hs_in_desc.bEndpointAddress =
733 fs_in_desc.bEndpointAddress;
734 hs_out_desc.bEndpointAddress =
735 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800736 hs_notify_desc.bEndpointAddress =
737 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700738
739 /* copy descriptors, and track endpoint copies */
740 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
David Brownell45fe3b82008-06-19 18:20:04 -0700741 if (!f->hs_descriptors)
742 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700743 }
744
Paul Zimmerman04617db2011-06-27 14:13:18 -0700745 if (gadget_is_superspeed(c->cdev->gadget)) {
746 ss_in_desc.bEndpointAddress =
747 fs_in_desc.bEndpointAddress;
748 ss_out_desc.bEndpointAddress =
749 fs_out_desc.bEndpointAddress;
750 ss_notify_desc.bEndpointAddress =
751 fs_notify_desc.bEndpointAddress;
752
753 /* copy descriptors, and track endpoint copies */
754 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
755 if (!f->ss_descriptors)
756 goto fail;
757 }
758
David Brownell45fe3b82008-06-19 18:20:04 -0700759 rndis->port.open = rndis_open;
760 rndis->port.close = rndis_close;
761
762 status = rndis_register(rndis_response_available, rndis);
763 if (status < 0)
764 goto fail;
765 rndis->config = status;
766
767 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
768 rndis_set_host_mac(rndis->config, rndis->ethaddr);
769
770#if 0
771// FIXME
772 if (rndis_set_param_vendor(rndis->config, vendorID,
773 manufacturer))
774 goto fail0;
775#endif
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",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700783 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell45fe3b82008-06-19 18:20:04 -0700784 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
789fail:
Paul Zimmerman04617db2011-06-27 14:13:18 -0700790 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
791 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700792 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
793 usb_free_descriptors(f->hs_descriptors);
794 if (f->descriptors)
795 usb_free_descriptors(f->descriptors);
796
797 if (rndis->notify_req) {
798 kfree(rndis->notify_req->buf);
799 usb_ep_free_request(rndis->notify, rndis->notify_req);
800 }
801
802 /* we might as well release our claims on endpoints */
803 if (rndis->notify)
804 rndis->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300805 if (rndis->port.out_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700806 rndis->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300807 if (rndis->port.in_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700808 rndis->port.in_ep->driver_data = NULL;
809
810 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
811
812 return status;
813}
814
815static void
816rndis_unbind(struct usb_configuration *c, struct usb_function *f)
817{
818 struct f_rndis *rndis = func_to_rndis(f);
819
820 rndis_deregister(rndis->config);
821 rndis_exit();
822
Paul Zimmerman04617db2011-06-27 14:13:18 -0700823 if (gadget_is_superspeed(c->cdev->gadget))
824 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700825 if (gadget_is_dualspeed(c->cdev->gadget))
826 usb_free_descriptors(f->hs_descriptors);
827 usb_free_descriptors(f->descriptors);
828
829 kfree(rndis->notify_req->buf);
830 usb_ep_free_request(rndis->notify, rndis->notify_req);
831
832 kfree(rndis);
833}
834
835/* Some controllers can't support RNDIS ... */
836static inline bool can_support_rndis(struct usb_configuration *c)
837{
David Brownell45fe3b82008-06-19 18:20:04 -0700838 /* everything else is *presumably* fine */
839 return true;
840}
841
842/**
843 * rndis_bind_config - add RNDIS network link to a configuration
844 * @c: the configuration to support the network link
845 * @ethaddr: a buffer in which the ethernet address of the host side
846 * side of the link was recorded
847 * Context: single threaded during gadget setup
848 *
849 * Returns zero on success, else negative errno.
850 *
851 * Caller must have called @gether_setup(). Caller is also responsible
852 * for calling @gether_cleanup() before module unload.
853 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200854int
855rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700856{
857 struct f_rndis *rndis;
858 int status;
859
860 if (!can_support_rndis(c) || !ethaddr)
861 return -EINVAL;
862
863 /* maybe allocate device-global string IDs */
864 if (rndis_string_defs[0].id == 0) {
865
866 /* ... and setup RNDIS itself */
867 status = rndis_init();
868 if (status < 0)
869 return status;
870
871 /* control interface label */
872 status = usb_string_id(c->cdev);
873 if (status < 0)
874 return status;
875 rndis_string_defs[0].id = status;
876 rndis_control_intf.iInterface = status;
877
878 /* data interface label */
879 status = usb_string_id(c->cdev);
880 if (status < 0)
881 return status;
882 rndis_string_defs[1].id = status;
883 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100884
885 /* IAD iFunction label */
886 status = usb_string_id(c->cdev);
887 if (status < 0)
888 return status;
889 rndis_string_defs[2].id = status;
890 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700891 }
892
893 /* allocate and initialize one new instance */
894 status = -ENOMEM;
895 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
896 if (!rndis)
897 goto fail;
898
899 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
900
901 /* RNDIS activates when the host changes this filter */
902 rndis->port.cdc_filter = 0;
903
904 /* RNDIS has special (and complex) framing */
905 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
906 rndis->port.wrap = rndis_add_header;
907 rndis->port.unwrap = rndis_rm_hdr;
908
909 rndis->port.func.name = "rndis";
910 rndis->port.func.strings = rndis_strings;
911 /* descriptors are per-instance copies */
912 rndis->port.func.bind = rndis_bind;
913 rndis->port.func.unbind = rndis_unbind;
914 rndis->port.func.set_alt = rndis_set_alt;
915 rndis->port.func.setup = rndis_setup;
916 rndis->port.func.disable = rndis_disable;
917
918 status = usb_add_function(c, &rndis->port.func);
919 if (status) {
920 kfree(rndis);
921fail:
922 rndis_exit();
923 }
924 return status;
925}