blob: 02f044eb7abd2e85e4b8335b5a4d9e0d2d0f3cf5 [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
Michal Nazarewicz54b83602012-01-13 15:05:16 +01008 * Author: Michal Nazarewicz (mina86@mina86.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
Mayank Rana93df2752012-09-12 18:12:46 +053028static bool rndis_multipacket_dl_disable;
29module_param(rndis_multipacket_dl_disable, bool, S_IRUGO|S_IWUSR);
30MODULE_PARM_DESC(rndis_multipacket_dl_disable,
31 "Disable RNDIS Multi-packet support in DownLink");
David Brownell45fe3b82008-06-19 18:20:04 -070032
33/*
34 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
35 * been promoted instead of the standard CDC Ethernet. The published RNDIS
36 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
37 * ActiveSync have even worse status in terms of specification.
38 *
39 * In short: it's a protocol controlled by (and for) Microsoft, not for an
40 * Open ecosystem or markets. Linux supports it *only* because Microsoft
41 * doesn't support the CDC Ethernet standard.
42 *
43 * The RNDIS data transfer model is complex, with multiple Ethernet packets
44 * per USB message, and out of band data. The control model is built around
45 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
46 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
47 * useless (they're ignored). RNDIS expects to be the only function in its
48 * configuration, so it's no real help if you need composite devices; and
49 * it expects to be the first configuration too.
50 *
51 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
52 * discount the fluff that its RPC can be made to deliver: it doesn't need
53 * a NOP altsetting for the data interface. That lets it work on some of the
54 * "so smart it's stupid" hardware which takes over configuration changes
55 * from the software, and adds restrictions like "no altsettings".
56 *
57 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
58 * have all sorts of contrary-to-specification oddities that can prevent
59 * them from working sanely. Since bugfixes (or accurate specs, letting
60 * Linux work around those bugs) are unlikely to ever come from MSFT, you
61 * may want to avoid using RNDIS on purely operational grounds.
62 *
63 * Omissions from the RNDIS 1.0 specification include:
64 *
65 * - Power management ... references data that's scattered around lots
66 * of other documentation, which is incorrect/incomplete there too.
67 *
68 * - There are various undocumented protocol requirements, like the need
69 * to send garbage in some control-OUT messages.
70 *
71 * - MS-Windows drivers sometimes emit undocumented requests.
72 */
73
David Brownell45fe3b82008-06-19 18:20:04 -070074struct f_rndis {
75 struct gether port;
76 u8 ctrl_id, data_id;
77 u8 ethaddr[ETH_ALEN];
Benoit Gobyda8a44a2011-12-16 20:00:01 -080078 u32 vendorID;
79 const char *manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -070080 int config;
81
David Brownell45fe3b82008-06-19 18:20:04 -070082 struct usb_ep *notify;
David Brownell45fe3b82008-06-19 18:20:04 -070083 struct usb_request *notify_req;
84 atomic_t notify_count;
85};
86
87static inline struct f_rndis *func_to_rndis(struct usb_function *f)
88{
89 return container_of(f, struct f_rndis, port.func);
90}
91
92/* peak (theoretical) bulk transfer rate in bits-per-second */
93static unsigned int bitrate(struct usb_gadget *g)
94{
Paul Zimmerman04617db2011-06-27 14:13:18 -070095 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
96 return 13 * 1024 * 8 * 1000 * 8;
97 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
David Brownell45fe3b82008-06-19 18:20:04 -070098 return 13 * 512 * 8 * 1000 * 8;
99 else
Paul Zimmerman04617db2011-06-27 14:13:18 -0700100 return 19 * 64 * 1 * 1000 * 8;
David Brownell45fe3b82008-06-19 18:20:04 -0700101}
102
103/*-------------------------------------------------------------------------*/
104
105/*
106 */
107
108#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
109#define STATUS_BYTECOUNT 8 /* 8 bytes data */
110
111
112/* interface descriptor: */
113
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200114static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700115 .bLength = sizeof rndis_control_intf,
116 .bDescriptorType = USB_DT_INTERFACE,
117
118 /* .bInterfaceNumber = DYNAMIC */
119 /* status endpoint is optional; this could be patched later */
120 .bNumEndpoints = 1,
121 .bInterfaceClass = USB_CLASS_COMM,
122 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
123 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
124 /* .iInterface = DYNAMIC */
125};
126
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200127static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700128 .bLength = sizeof header_desc,
129 .bDescriptorType = USB_DT_CS_INTERFACE,
130 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
131
Harvey Harrison551509d2009-02-11 14:11:36 -0800132 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700133};
134
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200135static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700136 .bLength = sizeof call_mgmt_descriptor,
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
139
140 .bmCapabilities = 0x00,
141 .bDataInterface = 0x01,
142};
143
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200144static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100145 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700146 .bDescriptorType = USB_DT_CS_INTERFACE,
147 .bDescriptorSubType = USB_CDC_ACM_TYPE,
148
149 .bmCapabilities = 0x00,
150};
151
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200152static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700153 .bLength = sizeof(rndis_union_desc),
154 .bDescriptorType = USB_DT_CS_INTERFACE,
155 .bDescriptorSubType = USB_CDC_UNION_TYPE,
156 /* .bMasterInterface0 = DYNAMIC */
157 /* .bSlaveInterface0 = DYNAMIC */
158};
159
160/* the data interface has two bulk endpoints */
161
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200162static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700163 .bLength = sizeof rndis_data_intf,
164 .bDescriptorType = USB_DT_INTERFACE,
165
166 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700167 .bNumEndpoints = 2,
168 .bInterfaceClass = USB_CLASS_CDC_DATA,
169 .bInterfaceSubClass = 0,
170 .bInterfaceProtocol = 0,
171 /* .iInterface = DYNAMIC */
172};
173
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100174
175static struct usb_interface_assoc_descriptor
176rndis_iad_descriptor = {
177 .bLength = sizeof rndis_iad_descriptor,
178 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
179
180 .bFirstInterface = 0, /* XXX, hardcoded */
181 .bInterfaceCount = 2, // control + data
182 .bFunctionClass = USB_CLASS_COMM,
183 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
184 .bFunctionProtocol = USB_CDC_PROTO_NONE,
185 /* .iFunction = DYNAMIC */
186};
187
David Brownell45fe3b82008-06-19 18:20:04 -0700188/* full speed support: */
189
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200190static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700191 .bLength = USB_DT_ENDPOINT_SIZE,
192 .bDescriptorType = USB_DT_ENDPOINT,
193
194 .bEndpointAddress = USB_DIR_IN,
195 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800196 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700197 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
198};
199
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200200static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT,
203
204 .bEndpointAddress = USB_DIR_IN,
205 .bmAttributes = USB_ENDPOINT_XFER_BULK,
206};
207
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200208static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700209 .bLength = USB_DT_ENDPOINT_SIZE,
210 .bDescriptorType = USB_DT_ENDPOINT,
211
212 .bEndpointAddress = USB_DIR_OUT,
213 .bmAttributes = USB_ENDPOINT_XFER_BULK,
214};
215
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200216static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100217 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700218
David Brownell45fe3b82008-06-19 18:20:04 -0700219 /* 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,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100223 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700224 (struct usb_descriptor_header *) &rndis_union_desc,
225 (struct usb_descriptor_header *) &fs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700226
David Brownell45fe3b82008-06-19 18:20:04 -0700227 /* data interface has no altsetting */
228 (struct usb_descriptor_header *) &rndis_data_intf,
229 (struct usb_descriptor_header *) &fs_in_desc,
230 (struct usb_descriptor_header *) &fs_out_desc,
231 NULL,
232};
233
234/* high speed support: */
235
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200236static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700237 .bLength = USB_DT_ENDPOINT_SIZE,
238 .bDescriptorType = USB_DT_ENDPOINT,
239
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800242 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
David Brownell45fe3b82008-06-19 18:20:04 -0700243 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
244};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700245
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200246static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700247 .bLength = USB_DT_ENDPOINT_SIZE,
248 .bDescriptorType = USB_DT_ENDPOINT,
249
250 .bEndpointAddress = USB_DIR_IN,
251 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800252 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700253};
254
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200255static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700256 .bLength = USB_DT_ENDPOINT_SIZE,
257 .bDescriptorType = USB_DT_ENDPOINT,
258
259 .bEndpointAddress = USB_DIR_OUT,
260 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800261 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700262};
263
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200264static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100265 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700266
David Brownell45fe3b82008-06-19 18:20:04 -0700267 /* control interface matches ACM, not Ethernet */
268 (struct usb_descriptor_header *) &rndis_control_intf,
269 (struct usb_descriptor_header *) &header_desc,
270 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100271 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700272 (struct usb_descriptor_header *) &rndis_union_desc,
273 (struct usb_descriptor_header *) &hs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700274
David Brownell45fe3b82008-06-19 18:20:04 -0700275 /* data interface has no altsetting */
276 (struct usb_descriptor_header *) &rndis_data_intf,
277 (struct usb_descriptor_header *) &hs_in_desc,
278 (struct usb_descriptor_header *) &hs_out_desc,
279 NULL,
280};
281
Paul Zimmerman04617db2011-06-27 14:13:18 -0700282/* super speed support: */
283
284static struct usb_endpoint_descriptor ss_notify_desc = {
285 .bLength = USB_DT_ENDPOINT_SIZE,
286 .bDescriptorType = USB_DT_ENDPOINT,
287
288 .bEndpointAddress = USB_DIR_IN,
289 .bmAttributes = USB_ENDPOINT_XFER_INT,
290 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
291 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
292};
293
294static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
295 .bLength = sizeof ss_intr_comp_desc,
296 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
297
298 /* the following 3 values can be tweaked if necessary */
299 /* .bMaxBurst = 0, */
300 /* .bmAttributes = 0, */
301 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
302};
303
304static struct usb_endpoint_descriptor ss_in_desc = {
305 .bLength = USB_DT_ENDPOINT_SIZE,
306 .bDescriptorType = USB_DT_ENDPOINT,
307
308 .bEndpointAddress = USB_DIR_IN,
309 .bmAttributes = USB_ENDPOINT_XFER_BULK,
310 .wMaxPacketSize = cpu_to_le16(1024),
311};
312
313static struct usb_endpoint_descriptor ss_out_desc = {
314 .bLength = USB_DT_ENDPOINT_SIZE,
315 .bDescriptorType = USB_DT_ENDPOINT,
316
317 .bEndpointAddress = USB_DIR_OUT,
318 .bmAttributes = USB_ENDPOINT_XFER_BULK,
319 .wMaxPacketSize = cpu_to_le16(1024),
320};
321
322static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
323 .bLength = sizeof ss_bulk_comp_desc,
324 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
325
326 /* the following 2 values can be tweaked if necessary */
327 /* .bMaxBurst = 0, */
328 /* .bmAttributes = 0, */
329};
330
331static struct usb_descriptor_header *eth_ss_function[] = {
332 (struct usb_descriptor_header *) &rndis_iad_descriptor,
333
334 /* control interface matches ACM, not Ethernet */
335 (struct usb_descriptor_header *) &rndis_control_intf,
336 (struct usb_descriptor_header *) &header_desc,
337 (struct usb_descriptor_header *) &call_mgmt_descriptor,
338 (struct usb_descriptor_header *) &rndis_acm_descriptor,
339 (struct usb_descriptor_header *) &rndis_union_desc,
340 (struct usb_descriptor_header *) &ss_notify_desc,
341 (struct usb_descriptor_header *) &ss_intr_comp_desc,
342
343 /* data interface has no altsetting */
344 (struct usb_descriptor_header *) &rndis_data_intf,
345 (struct usb_descriptor_header *) &ss_in_desc,
346 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
347 (struct usb_descriptor_header *) &ss_out_desc,
348 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
349 NULL,
350};
351
David Brownell45fe3b82008-06-19 18:20:04 -0700352/* string descriptors: */
353
354static struct usb_string rndis_string_defs[] = {
355 [0].s = "RNDIS Communications Control",
356 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100357 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700358 { } /* end of list */
359};
360
361static struct usb_gadget_strings rndis_string_table = {
362 .language = 0x0409, /* en-us */
363 .strings = rndis_string_defs,
364};
365
366static struct usb_gadget_strings *rndis_strings[] = {
367 &rndis_string_table,
368 NULL,
369};
370
371/*-------------------------------------------------------------------------*/
372
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500373static struct sk_buff *rndis_add_header(struct gether *port,
374 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700375{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500376 struct sk_buff *skb2;
377
378 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
379 if (skb2)
380 rndis_add_hdr(skb2);
381
382 dev_kfree_skb_any(skb);
383 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700384}
385
386static void rndis_response_available(void *_rndis)
387{
388 struct f_rndis *rndis = _rndis;
389 struct usb_request *req = rndis->notify_req;
390 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
391 __le32 *data = req->buf;
392 int status;
393
Richard Röjforsff349502008-11-15 19:53:24 -0800394 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700395 return;
396
397 /* Send RNDIS RESPONSE_AVAILABLE notification; a
398 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
399 *
400 * This is the only notification defined by RNDIS.
401 */
402 data[0] = cpu_to_le32(1);
403 data[1] = cpu_to_le32(0);
404
405 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
406 if (status) {
407 atomic_dec(&rndis->notify_count);
408 DBG(cdev, "notify/0 --> %d\n", status);
409 }
410}
411
412static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
413{
414 struct f_rndis *rndis = req->context;
415 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
416 int status = req->status;
417
418 /* after TX:
419 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
420 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
421 */
422 switch (status) {
423 case -ECONNRESET:
424 case -ESHUTDOWN:
425 /* connection gone */
426 atomic_set(&rndis->notify_count, 0);
427 break;
428 default:
429 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
430 ep->name, status,
431 req->actual, req->length);
432 /* FALLTHROUGH */
433 case 0:
434 if (ep != rndis->notify)
435 break;
436
437 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
438 * notifications by resending until we're done
439 */
440 if (atomic_dec_and_test(&rndis->notify_count))
441 break;
442 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
443 if (status) {
444 atomic_dec(&rndis->notify_count);
445 DBG(cdev, "notify/1 --> %d\n", status);
446 }
447 break;
448 }
449}
450
451static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
452{
453 struct f_rndis *rndis = req->context;
454 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
455 int status;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530456 rndis_init_msg_type *buf;
David Brownell45fe3b82008-06-19 18:20:04 -0700457
458 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
459// spin_lock(&dev->lock);
460 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
461 if (status < 0)
462 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
463 status, req->actual, req->length);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530464
465 buf = (rndis_init_msg_type *)req->buf;
466
467 if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) {
468 if (buf->MaxTransferSize > 2048)
469 rndis->port.multi_pkt_xfer = 1;
470 else
471 rndis->port.multi_pkt_xfer = 0;
472 DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
473 __func__, buf->MaxTransferSize,
474 rndis->port.multi_pkt_xfer ? "enabled" :
475 "disabled");
Mayank Rana93df2752012-09-12 18:12:46 +0530476 if (rndis_multipacket_dl_disable)
477 rndis->port.multi_pkt_xfer = 0;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530478 }
David Brownell45fe3b82008-06-19 18:20:04 -0700479// spin_unlock(&dev->lock);
480}
481
482static int
483rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
484{
485 struct f_rndis *rndis = func_to_rndis(f);
486 struct usb_composite_dev *cdev = f->config->cdev;
487 struct usb_request *req = cdev->req;
488 int value = -EOPNOTSUPP;
489 u16 w_index = le16_to_cpu(ctrl->wIndex);
490 u16 w_value = le16_to_cpu(ctrl->wValue);
491 u16 w_length = le16_to_cpu(ctrl->wLength);
492
493 /* composite driver infrastructure handles everything except
494 * CDC class messages; interface activation uses set_alt().
495 */
496 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
497
498 /* RNDIS uses the CDC command encapsulation mechanism to implement
499 * an RPC scheme, with much getting/setting of attributes by OID.
500 */
501 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
502 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300503 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700504 goto invalid;
505 /* read the request; process it later */
506 value = w_length;
507 req->complete = rndis_command_complete;
508 req->context = rndis;
509 /* later, rndis_response_available() sends a notification */
510 break;
511
512 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
513 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
514 if (w_value || w_index != rndis->ctrl_id)
515 goto invalid;
516 else {
517 u8 *buf;
518 u32 n;
519
520 /* return the result */
521 buf = rndis_get_next_response(rndis->config, &n);
522 if (buf) {
523 memcpy(req->buf, buf, n);
524 req->complete = rndis_response_complete;
Lukasz Majewskif1356172012-02-10 09:54:51 +0100525 req->context = rndis;
David Brownell45fe3b82008-06-19 18:20:04 -0700526 rndis_free_response(rndis->config, buf);
527 value = n;
528 }
529 /* else stalls ... spec says to avoid that */
530 }
531 break;
532
533 default:
534invalid:
535 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
536 ctrl->bRequestType, ctrl->bRequest,
537 w_value, w_index, w_length);
538 }
539
540 /* respond with data transfer or status phase? */
541 if (value >= 0) {
542 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
543 ctrl->bRequestType, ctrl->bRequest,
544 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700545 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700546 req->length = value;
547 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
548 if (value < 0)
549 ERROR(cdev, "rndis response on err %d\n", value);
550 }
551
552 /* device either stalls (value < 0) or reports success */
553 return value;
554}
555
556
557static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
558{
559 struct f_rndis *rndis = func_to_rndis(f);
560 struct usb_composite_dev *cdev = f->config->cdev;
561
562 /* we know alt == 0 */
563
564 if (intf == rndis->ctrl_id) {
565 if (rndis->notify->driver_data) {
566 VDBG(cdev, "reset rndis control %d\n", intf);
567 usb_ep_disable(rndis->notify);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300568 }
569 if (!rndis->notify->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700570 VDBG(cdev, "init rndis ctrl %d\n", intf);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300571 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
572 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700573 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300574 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700575 rndis->notify->driver_data = rndis;
576
577 } else if (intf == rndis->data_id) {
578 struct net_device *net;
579
580 if (rndis->port.in_ep->driver_data) {
581 DBG(cdev, "reset rndis\n");
582 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530583 }
584
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300585 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700586 DBG(cdev, "init rndis\n");
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300587 if (config_ep_by_speed(cdev->gadget, f,
588 rndis->port.in_ep) ||
589 config_ep_by_speed(cdev->gadget, f,
590 rndis->port.out_ep)) {
591 rndis->port.in_ep->desc = NULL;
592 rndis->port.out_ep->desc = NULL;
593 goto fail;
594 }
David Brownell45fe3b82008-06-19 18:20:04 -0700595 }
596
597 /* Avoid ZLPs; they can be troublesome. */
598 rndis->port.is_zlp_ok = false;
599
600 /* RNDIS should be in the "RNDIS uninitialized" state,
601 * either never activated or after rndis_uninit().
602 *
603 * We don't want data to flow here until a nonzero packet
604 * filter is set, at which point it enters "RNDIS data
605 * initialized" state ... but we do want the endpoints
606 * to be activated. It's a strange little state.
607 *
608 * REVISIT the RNDIS gadget code has done this wrong for a
609 * very long time. We need another call to the link layer
610 * code -- gether_updown(...bool) maybe -- to do it right.
611 */
612 rndis->port.cdc_filter = 0;
613
614 DBG(cdev, "RNDIS RX/TX early activation ... \n");
615 net = gether_connect(&rndis->port);
616 if (IS_ERR(net))
617 return PTR_ERR(net);
618
619 rndis_set_param_dev(rndis->config, net,
620 &rndis->port.cdc_filter);
621 } else
622 goto fail;
623
624 return 0;
625fail:
626 return -EINVAL;
627}
628
629static void rndis_disable(struct usb_function *f)
630{
631 struct f_rndis *rndis = func_to_rndis(f);
632 struct usb_composite_dev *cdev = f->config->cdev;
633
634 if (!rndis->notify->driver_data)
635 return;
636
637 DBG(cdev, "rndis deactivated\n");
638
639 rndis_uninit(rndis->config);
640 gether_disconnect(&rndis->port);
641
642 usb_ep_disable(rndis->notify);
643 rndis->notify->driver_data = NULL;
644}
645
646/*-------------------------------------------------------------------------*/
647
648/*
649 * This isn't quite the same mechanism as CDC Ethernet, since the
650 * notification scheme passes less data, but the same set of link
651 * states must be tested. A key difference is that altsettings are
652 * not used to tell whether the link should send packets or not.
653 */
654
655static void rndis_open(struct gether *geth)
656{
657 struct f_rndis *rndis = func_to_rndis(&geth->func);
658 struct usb_composite_dev *cdev = geth->func.config->cdev;
659
660 DBG(cdev, "%s\n", __func__);
661
662 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
663 bitrate(cdev->gadget) / 100);
664 rndis_signal_connect(rndis->config);
665}
666
667static void rndis_close(struct gether *geth)
668{
669 struct f_rndis *rndis = func_to_rndis(&geth->func);
670
671 DBG(geth->func.config->cdev, "%s\n", __func__);
672
673 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
674 rndis_signal_disconnect(rndis->config);
675}
676
677/*-------------------------------------------------------------------------*/
678
679/* ethernet function driver setup/binding */
680
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200681static int
David Brownell45fe3b82008-06-19 18:20:04 -0700682rndis_bind(struct usb_configuration *c, struct usb_function *f)
683{
684 struct usb_composite_dev *cdev = c->cdev;
685 struct f_rndis *rndis = func_to_rndis(f);
686 int status;
687 struct usb_ep *ep;
688
689 /* allocate instance-specific interface IDs */
690 status = usb_interface_id(c, f);
691 if (status < 0)
692 goto fail;
693 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100694 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700695
696 rndis_control_intf.bInterfaceNumber = status;
697 rndis_union_desc.bMasterInterface0 = status;
698
699 status = usb_interface_id(c, f);
700 if (status < 0)
701 goto fail;
702 rndis->data_id = status;
703
704 rndis_data_intf.bInterfaceNumber = status;
705 rndis_union_desc.bSlaveInterface0 = status;
706
707 status = -ENODEV;
708
709 /* allocate instance-specific endpoints */
710 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
711 if (!ep)
712 goto fail;
713 rndis->port.in_ep = ep;
714 ep->driver_data = cdev; /* claim */
715
716 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
717 if (!ep)
718 goto fail;
719 rndis->port.out_ep = ep;
720 ep->driver_data = cdev; /* claim */
721
722 /* NOTE: a status/notification endpoint is, strictly speaking,
723 * optional. We don't treat it that way though! It's simpler,
724 * and some newer profiles don't treat it as optional.
725 */
726 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
727 if (!ep)
728 goto fail;
729 rndis->notify = ep;
730 ep->driver_data = cdev; /* claim */
731
732 status = -ENOMEM;
733
734 /* allocate notification request and buffer */
735 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
736 if (!rndis->notify_req)
737 goto fail;
738 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
739 if (!rndis->notify_req->buf)
740 goto fail;
741 rndis->notify_req->length = STATUS_BYTECOUNT;
742 rndis->notify_req->context = rndis;
743 rndis->notify_req->complete = rndis_response_complete;
744
745 /* copy descriptors, and track endpoint copies */
746 f->descriptors = usb_copy_descriptors(eth_fs_function);
747 if (!f->descriptors)
748 goto fail;
749
David Brownell45fe3b82008-06-19 18:20:04 -0700750 /* support all relevant hardware speeds... we expect that when
751 * hardware is dual speed, all bulk-capable endpoints work at
752 * both speeds
753 */
754 if (gadget_is_dualspeed(c->cdev->gadget)) {
755 hs_in_desc.bEndpointAddress =
756 fs_in_desc.bEndpointAddress;
757 hs_out_desc.bEndpointAddress =
758 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800759 hs_notify_desc.bEndpointAddress =
760 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700761
762 /* copy descriptors, and track endpoint copies */
763 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
David Brownell45fe3b82008-06-19 18:20:04 -0700764 if (!f->hs_descriptors)
765 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700766 }
767
Paul Zimmerman04617db2011-06-27 14:13:18 -0700768 if (gadget_is_superspeed(c->cdev->gadget)) {
769 ss_in_desc.bEndpointAddress =
770 fs_in_desc.bEndpointAddress;
771 ss_out_desc.bEndpointAddress =
772 fs_out_desc.bEndpointAddress;
773 ss_notify_desc.bEndpointAddress =
774 fs_notify_desc.bEndpointAddress;
775
776 /* copy descriptors, and track endpoint copies */
777 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
778 if (!f->ss_descriptors)
779 goto fail;
780 }
781
David Brownell45fe3b82008-06-19 18:20:04 -0700782 rndis->port.open = rndis_open;
783 rndis->port.close = rndis_close;
784
785 status = rndis_register(rndis_response_available, rndis);
786 if (status < 0)
787 goto fail;
788 rndis->config = status;
789
790 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
791 rndis_set_host_mac(rndis->config, rndis->ethaddr);
792
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800793 if (rndis->manufacturer && rndis->vendorID &&
794 rndis_set_param_vendor(rndis->config, rndis->vendorID,
795 rndis->manufacturer))
796 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700797
798 /* NOTE: all that is done without knowing or caring about
799 * the network link ... which is unavailable to this code
800 * until we're activated via set_alt().
801 */
802
803 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700804 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell45fe3b82008-06-19 18:20:04 -0700805 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
806 rndis->port.in_ep->name, rndis->port.out_ep->name,
807 rndis->notify->name);
808 return 0;
809
810fail:
Paul Zimmerman04617db2011-06-27 14:13:18 -0700811 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
812 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700813 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
814 usb_free_descriptors(f->hs_descriptors);
815 if (f->descriptors)
816 usb_free_descriptors(f->descriptors);
817
818 if (rndis->notify_req) {
819 kfree(rndis->notify_req->buf);
820 usb_ep_free_request(rndis->notify, rndis->notify_req);
821 }
822
823 /* we might as well release our claims on endpoints */
824 if (rndis->notify)
825 rndis->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300826 if (rndis->port.out_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700827 rndis->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300828 if (rndis->port.in_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700829 rndis->port.in_ep->driver_data = NULL;
830
831 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
832
833 return status;
834}
835
836static void
837rndis_unbind(struct usb_configuration *c, struct usb_function *f)
838{
839 struct f_rndis *rndis = func_to_rndis(f);
840
841 rndis_deregister(rndis->config);
842 rndis_exit();
Benoit Goby7f141c52011-12-16 20:00:38 -0800843 rndis_string_defs[0].id = 0;
David Brownell45fe3b82008-06-19 18:20:04 -0700844
Paul Zimmerman04617db2011-06-27 14:13:18 -0700845 if (gadget_is_superspeed(c->cdev->gadget))
846 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700847 if (gadget_is_dualspeed(c->cdev->gadget))
848 usb_free_descriptors(f->hs_descriptors);
849 usb_free_descriptors(f->descriptors);
850
851 kfree(rndis->notify_req->buf);
852 usb_ep_free_request(rndis->notify, rndis->notify_req);
853
854 kfree(rndis);
855}
856
857/* Some controllers can't support RNDIS ... */
858static inline bool can_support_rndis(struct usb_configuration *c)
859{
David Brownell45fe3b82008-06-19 18:20:04 -0700860 /* everything else is *presumably* fine */
861 return true;
862}
863
864/**
865 * rndis_bind_config - add RNDIS network link to a configuration
866 * @c: the configuration to support the network link
867 * @ethaddr: a buffer in which the ethernet address of the host side
868 * side of the link was recorded
869 * Context: single threaded during gadget setup
870 *
871 * Returns zero on success, else negative errno.
872 *
873 * Caller must have called @gether_setup(). Caller is also responsible
874 * for calling @gether_cleanup() before module unload.
875 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200876int
877rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700878{
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800879 return rndis_bind_config_vendor(c, ethaddr, 0, NULL);
880}
881
882int
883rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
884 u32 vendorID, const char *manufacturer)
885{
David Brownell45fe3b82008-06-19 18:20:04 -0700886 struct f_rndis *rndis;
887 int status;
888
889 if (!can_support_rndis(c) || !ethaddr)
890 return -EINVAL;
891
Benoit Gobya6ccb732012-01-20 14:42:41 -0800892 /* setup RNDIS itself */
893 status = rndis_init();
894 if (status < 0)
895 return status;
896
David Brownell45fe3b82008-06-19 18:20:04 -0700897 /* maybe allocate device-global string IDs */
898 if (rndis_string_defs[0].id == 0) {
899
David Brownell45fe3b82008-06-19 18:20:04 -0700900 /* control interface label */
901 status = usb_string_id(c->cdev);
902 if (status < 0)
903 return status;
904 rndis_string_defs[0].id = status;
905 rndis_control_intf.iInterface = status;
906
907 /* data interface label */
908 status = usb_string_id(c->cdev);
909 if (status < 0)
910 return status;
911 rndis_string_defs[1].id = status;
912 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100913
914 /* IAD iFunction label */
915 status = usb_string_id(c->cdev);
916 if (status < 0)
917 return status;
918 rndis_string_defs[2].id = status;
919 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700920 }
921
922 /* allocate and initialize one new instance */
923 status = -ENOMEM;
924 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
925 if (!rndis)
926 goto fail;
927
928 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800929 rndis->vendorID = vendorID;
930 rndis->manufacturer = manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -0700931
932 /* RNDIS activates when the host changes this filter */
933 rndis->port.cdc_filter = 0;
934
935 /* RNDIS has special (and complex) framing */
936 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
937 rndis->port.wrap = rndis_add_header;
938 rndis->port.unwrap = rndis_rm_hdr;
939
940 rndis->port.func.name = "rndis";
941 rndis->port.func.strings = rndis_strings;
942 /* descriptors are per-instance copies */
943 rndis->port.func.bind = rndis_bind;
944 rndis->port.func.unbind = rndis_unbind;
945 rndis->port.func.set_alt = rndis_set_alt;
946 rndis->port.func.setup = rndis_setup;
947 rndis->port.func.disable = rndis_disable;
948
949 status = usb_add_function(c, &rndis->port.func);
950 if (status) {
951 kfree(rndis);
952fail:
953 rndis_exit();
954 }
955 return status;
956}