blob: f3c64810bdebd41ec46db4bf45c4f272102a6418 [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;
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530415 struct usb_composite_dev *cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700416 int status = req->status;
417
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530418 if (!rndis->port.func.config || !rndis->port.func.config->cdev)
419 return;
420 else
421 cdev = rndis->port.func.config->cdev;
422
David Brownell45fe3b82008-06-19 18:20:04 -0700423 /* after TX:
424 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
425 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
426 */
427 switch (status) {
428 case -ECONNRESET:
429 case -ESHUTDOWN:
430 /* connection gone */
431 atomic_set(&rndis->notify_count, 0);
432 break;
433 default:
434 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
435 ep->name, status,
436 req->actual, req->length);
437 /* FALLTHROUGH */
438 case 0:
439 if (ep != rndis->notify)
440 break;
441
442 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
443 * notifications by resending until we're done
444 */
445 if (atomic_dec_and_test(&rndis->notify_count))
446 break;
447 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
448 if (status) {
449 atomic_dec(&rndis->notify_count);
450 DBG(cdev, "notify/1 --> %d\n", status);
451 }
452 break;
453 }
454}
455
456static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
457{
458 struct f_rndis *rndis = req->context;
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530459 struct usb_composite_dev *cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700460 int status;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530461 rndis_init_msg_type *buf;
David Brownell45fe3b82008-06-19 18:20:04 -0700462
Chiranjeevi Velempati46874732013-01-31 14:35:46 +0530463 if (!rndis->port.func.config || !rndis->port.func.config->cdev)
464 return;
465 else
466 cdev = rndis->port.func.config->cdev;
467
David Brownell45fe3b82008-06-19 18:20:04 -0700468 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
469// spin_lock(&dev->lock);
470 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
471 if (status < 0)
472 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
473 status, req->actual, req->length);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530474
475 buf = (rndis_init_msg_type *)req->buf;
476
477 if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) {
478 if (buf->MaxTransferSize > 2048)
479 rndis->port.multi_pkt_xfer = 1;
480 else
481 rndis->port.multi_pkt_xfer = 0;
482 DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
483 __func__, buf->MaxTransferSize,
484 rndis->port.multi_pkt_xfer ? "enabled" :
485 "disabled");
Mayank Rana93df2752012-09-12 18:12:46 +0530486 if (rndis_multipacket_dl_disable)
487 rndis->port.multi_pkt_xfer = 0;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530488 }
David Brownell45fe3b82008-06-19 18:20:04 -0700489// spin_unlock(&dev->lock);
490}
491
492static int
493rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
494{
495 struct f_rndis *rndis = func_to_rndis(f);
496 struct usb_composite_dev *cdev = f->config->cdev;
497 struct usb_request *req = cdev->req;
498 int value = -EOPNOTSUPP;
499 u16 w_index = le16_to_cpu(ctrl->wIndex);
500 u16 w_value = le16_to_cpu(ctrl->wValue);
501 u16 w_length = le16_to_cpu(ctrl->wLength);
502
503 /* composite driver infrastructure handles everything except
504 * CDC class messages; interface activation uses set_alt().
505 */
506 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
507
508 /* RNDIS uses the CDC command encapsulation mechanism to implement
509 * an RPC scheme, with much getting/setting of attributes by OID.
510 */
511 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
512 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300513 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700514 goto invalid;
515 /* read the request; process it later */
516 value = w_length;
517 req->complete = rndis_command_complete;
518 req->context = rndis;
519 /* later, rndis_response_available() sends a notification */
520 break;
521
522 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
523 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
524 if (w_value || w_index != rndis->ctrl_id)
525 goto invalid;
526 else {
527 u8 *buf;
528 u32 n;
529
530 /* return the result */
531 buf = rndis_get_next_response(rndis->config, &n);
532 if (buf) {
533 memcpy(req->buf, buf, n);
534 req->complete = rndis_response_complete;
Lukasz Majewskif1356172012-02-10 09:54:51 +0100535 req->context = rndis;
David Brownell45fe3b82008-06-19 18:20:04 -0700536 rndis_free_response(rndis->config, buf);
537 value = n;
538 }
539 /* else stalls ... spec says to avoid that */
540 }
541 break;
542
543 default:
544invalid:
545 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
546 ctrl->bRequestType, ctrl->bRequest,
547 w_value, w_index, w_length);
548 }
549
550 /* respond with data transfer or status phase? */
551 if (value >= 0) {
552 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
553 ctrl->bRequestType, ctrl->bRequest,
554 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700555 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700556 req->length = value;
557 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
558 if (value < 0)
559 ERROR(cdev, "rndis response on err %d\n", value);
560 }
561
562 /* device either stalls (value < 0) or reports success */
563 return value;
564}
565
566
567static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
568{
569 struct f_rndis *rndis = func_to_rndis(f);
570 struct usb_composite_dev *cdev = f->config->cdev;
571
572 /* we know alt == 0 */
573
574 if (intf == rndis->ctrl_id) {
575 if (rndis->notify->driver_data) {
576 VDBG(cdev, "reset rndis control %d\n", intf);
577 usb_ep_disable(rndis->notify);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300578 }
579 if (!rndis->notify->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700580 VDBG(cdev, "init rndis ctrl %d\n", intf);
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300581 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
582 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700583 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300584 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700585 rndis->notify->driver_data = rndis;
586
587 } else if (intf == rndis->data_id) {
588 struct net_device *net;
589
590 if (rndis->port.in_ep->driver_data) {
591 DBG(cdev, "reset rndis\n");
592 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530593 }
594
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300595 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700596 DBG(cdev, "init rndis\n");
Tatyana Brokhmanea2a1df72011-06-28 16:33:50 +0300597 if (config_ep_by_speed(cdev->gadget, f,
598 rndis->port.in_ep) ||
599 config_ep_by_speed(cdev->gadget, f,
600 rndis->port.out_ep)) {
601 rndis->port.in_ep->desc = NULL;
602 rndis->port.out_ep->desc = NULL;
603 goto fail;
604 }
David Brownell45fe3b82008-06-19 18:20:04 -0700605 }
606
607 /* Avoid ZLPs; they can be troublesome. */
608 rndis->port.is_zlp_ok = false;
609
610 /* RNDIS should be in the "RNDIS uninitialized" state,
611 * either never activated or after rndis_uninit().
612 *
613 * We don't want data to flow here until a nonzero packet
614 * filter is set, at which point it enters "RNDIS data
615 * initialized" state ... but we do want the endpoints
616 * to be activated. It's a strange little state.
617 *
618 * REVISIT the RNDIS gadget code has done this wrong for a
619 * very long time. We need another call to the link layer
620 * code -- gether_updown(...bool) maybe -- to do it right.
621 */
622 rndis->port.cdc_filter = 0;
623
624 DBG(cdev, "RNDIS RX/TX early activation ... \n");
625 net = gether_connect(&rndis->port);
626 if (IS_ERR(net))
627 return PTR_ERR(net);
628
629 rndis_set_param_dev(rndis->config, net,
630 &rndis->port.cdc_filter);
631 } else
632 goto fail;
633
634 return 0;
635fail:
636 return -EINVAL;
637}
638
639static void rndis_disable(struct usb_function *f)
640{
641 struct f_rndis *rndis = func_to_rndis(f);
642 struct usb_composite_dev *cdev = f->config->cdev;
643
644 if (!rndis->notify->driver_data)
645 return;
646
647 DBG(cdev, "rndis deactivated\n");
648
649 rndis_uninit(rndis->config);
650 gether_disconnect(&rndis->port);
651
652 usb_ep_disable(rndis->notify);
653 rndis->notify->driver_data = NULL;
654}
655
656/*-------------------------------------------------------------------------*/
657
658/*
659 * This isn't quite the same mechanism as CDC Ethernet, since the
660 * notification scheme passes less data, but the same set of link
661 * states must be tested. A key difference is that altsettings are
662 * not used to tell whether the link should send packets or not.
663 */
664
665static void rndis_open(struct gether *geth)
666{
667 struct f_rndis *rndis = func_to_rndis(&geth->func);
668 struct usb_composite_dev *cdev = geth->func.config->cdev;
669
670 DBG(cdev, "%s\n", __func__);
671
672 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
673 bitrate(cdev->gadget) / 100);
674 rndis_signal_connect(rndis->config);
675}
676
677static void rndis_close(struct gether *geth)
678{
679 struct f_rndis *rndis = func_to_rndis(&geth->func);
680
681 DBG(geth->func.config->cdev, "%s\n", __func__);
682
683 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
684 rndis_signal_disconnect(rndis->config);
685}
686
687/*-------------------------------------------------------------------------*/
688
689/* ethernet function driver setup/binding */
690
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200691static int
David Brownell45fe3b82008-06-19 18:20:04 -0700692rndis_bind(struct usb_configuration *c, struct usb_function *f)
693{
694 struct usb_composite_dev *cdev = c->cdev;
695 struct f_rndis *rndis = func_to_rndis(f);
696 int status;
697 struct usb_ep *ep;
698
699 /* allocate instance-specific interface IDs */
700 status = usb_interface_id(c, f);
701 if (status < 0)
702 goto fail;
703 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100704 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700705
706 rndis_control_intf.bInterfaceNumber = status;
707 rndis_union_desc.bMasterInterface0 = status;
708
709 status = usb_interface_id(c, f);
710 if (status < 0)
711 goto fail;
712 rndis->data_id = status;
713
714 rndis_data_intf.bInterfaceNumber = status;
715 rndis_union_desc.bSlaveInterface0 = status;
716
717 status = -ENODEV;
718
719 /* allocate instance-specific endpoints */
720 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
721 if (!ep)
722 goto fail;
723 rndis->port.in_ep = ep;
724 ep->driver_data = cdev; /* claim */
725
726 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
727 if (!ep)
728 goto fail;
729 rndis->port.out_ep = ep;
730 ep->driver_data = cdev; /* claim */
731
732 /* NOTE: a status/notification endpoint is, strictly speaking,
733 * optional. We don't treat it that way though! It's simpler,
734 * and some newer profiles don't treat it as optional.
735 */
736 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
737 if (!ep)
738 goto fail;
739 rndis->notify = ep;
740 ep->driver_data = cdev; /* claim */
741
742 status = -ENOMEM;
743
744 /* allocate notification request and buffer */
745 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
746 if (!rndis->notify_req)
747 goto fail;
748 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
749 if (!rndis->notify_req->buf)
750 goto fail;
751 rndis->notify_req->length = STATUS_BYTECOUNT;
752 rndis->notify_req->context = rndis;
753 rndis->notify_req->complete = rndis_response_complete;
754
755 /* copy descriptors, and track endpoint copies */
756 f->descriptors = usb_copy_descriptors(eth_fs_function);
757 if (!f->descriptors)
758 goto fail;
759
David Brownell45fe3b82008-06-19 18:20:04 -0700760 /* support all relevant hardware speeds... we expect that when
761 * hardware is dual speed, all bulk-capable endpoints work at
762 * both speeds
763 */
764 if (gadget_is_dualspeed(c->cdev->gadget)) {
765 hs_in_desc.bEndpointAddress =
766 fs_in_desc.bEndpointAddress;
767 hs_out_desc.bEndpointAddress =
768 fs_out_desc.bEndpointAddress;
David Brownell7c124142008-11-24 23:11:03 -0800769 hs_notify_desc.bEndpointAddress =
770 fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700771
772 /* copy descriptors, and track endpoint copies */
773 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
David Brownell45fe3b82008-06-19 18:20:04 -0700774 if (!f->hs_descriptors)
775 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700776 }
777
Paul Zimmerman04617db2011-06-27 14:13:18 -0700778 if (gadget_is_superspeed(c->cdev->gadget)) {
779 ss_in_desc.bEndpointAddress =
780 fs_in_desc.bEndpointAddress;
781 ss_out_desc.bEndpointAddress =
782 fs_out_desc.bEndpointAddress;
783 ss_notify_desc.bEndpointAddress =
784 fs_notify_desc.bEndpointAddress;
785
786 /* copy descriptors, and track endpoint copies */
787 f->ss_descriptors = usb_copy_descriptors(eth_ss_function);
788 if (!f->ss_descriptors)
789 goto fail;
790 }
791
David Brownell45fe3b82008-06-19 18:20:04 -0700792 rndis->port.open = rndis_open;
793 rndis->port.close = rndis_close;
794
795 status = rndis_register(rndis_response_available, rndis);
796 if (status < 0)
797 goto fail;
798 rndis->config = status;
799
800 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
801 rndis_set_host_mac(rndis->config, rndis->ethaddr);
802
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800803 if (rndis->manufacturer && rndis->vendorID &&
804 rndis_set_param_vendor(rndis->config, rndis->vendorID,
805 rndis->manufacturer))
806 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700807
808 /* NOTE: all that is done without knowing or caring about
809 * the network link ... which is unavailable to this code
810 * until we're activated via set_alt().
811 */
812
813 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700814 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell45fe3b82008-06-19 18:20:04 -0700815 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
816 rndis->port.in_ep->name, rndis->port.out_ep->name,
817 rndis->notify->name);
818 return 0;
819
820fail:
Paul Zimmerman04617db2011-06-27 14:13:18 -0700821 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
822 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700823 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
824 usb_free_descriptors(f->hs_descriptors);
825 if (f->descriptors)
826 usb_free_descriptors(f->descriptors);
827
828 if (rndis->notify_req) {
829 kfree(rndis->notify_req->buf);
830 usb_ep_free_request(rndis->notify, rndis->notify_req);
831 }
832
833 /* we might as well release our claims on endpoints */
834 if (rndis->notify)
835 rndis->notify->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300836 if (rndis->port.out_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700837 rndis->port.out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300838 if (rndis->port.in_ep->desc)
David Brownell45fe3b82008-06-19 18:20:04 -0700839 rndis->port.in_ep->driver_data = NULL;
840
841 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
842
843 return status;
844}
845
846static void
847rndis_unbind(struct usb_configuration *c, struct usb_function *f)
848{
849 struct f_rndis *rndis = func_to_rndis(f);
850
851 rndis_deregister(rndis->config);
852 rndis_exit();
853
Paul Zimmerman04617db2011-06-27 14:13:18 -0700854 if (gadget_is_superspeed(c->cdev->gadget))
855 usb_free_descriptors(f->ss_descriptors);
David Brownell45fe3b82008-06-19 18:20:04 -0700856 if (gadget_is_dualspeed(c->cdev->gadget))
857 usb_free_descriptors(f->hs_descriptors);
858 usb_free_descriptors(f->descriptors);
859
860 kfree(rndis->notify_req->buf);
861 usb_ep_free_request(rndis->notify, rndis->notify_req);
862
863 kfree(rndis);
864}
865
866/* Some controllers can't support RNDIS ... */
867static inline bool can_support_rndis(struct usb_configuration *c)
868{
David Brownell45fe3b82008-06-19 18:20:04 -0700869 /* everything else is *presumably* fine */
870 return true;
871}
872
873/**
874 * rndis_bind_config - add RNDIS network link to a configuration
875 * @c: the configuration to support the network link
876 * @ethaddr: a buffer in which the ethernet address of the host side
877 * side of the link was recorded
878 * Context: single threaded during gadget setup
879 *
880 * Returns zero on success, else negative errno.
881 *
882 * Caller must have called @gether_setup(). Caller is also responsible
883 * for calling @gether_cleanup() before module unload.
884 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200885int
886rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
David Brownell45fe3b82008-06-19 18:20:04 -0700887{
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800888 return rndis_bind_config_vendor(c, ethaddr, 0, NULL);
889}
890
891int
892rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
893 u32 vendorID, const char *manufacturer)
894{
David Brownell45fe3b82008-06-19 18:20:04 -0700895 struct f_rndis *rndis;
896 int status;
897
898 if (!can_support_rndis(c) || !ethaddr)
899 return -EINVAL;
900
Benoit Gobya6ccb732012-01-20 14:42:41 -0800901 /* setup RNDIS itself */
902 status = rndis_init();
903 if (status < 0)
904 return status;
905
David Brownell45fe3b82008-06-19 18:20:04 -0700906 /* maybe allocate device-global string IDs */
907 if (rndis_string_defs[0].id == 0) {
908
David Brownell45fe3b82008-06-19 18:20:04 -0700909 /* control interface label */
910 status = usb_string_id(c->cdev);
911 if (status < 0)
912 return status;
913 rndis_string_defs[0].id = status;
914 rndis_control_intf.iInterface = status;
915
916 /* data interface label */
917 status = usb_string_id(c->cdev);
918 if (status < 0)
919 return status;
920 rndis_string_defs[1].id = status;
921 rndis_data_intf.iInterface = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100922
923 /* IAD iFunction label */
924 status = usb_string_id(c->cdev);
925 if (status < 0)
926 return status;
927 rndis_string_defs[2].id = status;
928 rndis_iad_descriptor.iFunction = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700929 }
930
931 /* allocate and initialize one new instance */
932 status = -ENOMEM;
933 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
934 if (!rndis)
935 goto fail;
936
937 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
Benoit Gobyda8a44a2011-12-16 20:00:01 -0800938 rndis->vendorID = vendorID;
939 rndis->manufacturer = manufacturer;
David Brownell45fe3b82008-06-19 18:20:04 -0700940
941 /* RNDIS activates when the host changes this filter */
942 rndis->port.cdc_filter = 0;
943
944 /* RNDIS has special (and complex) framing */
945 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
946 rndis->port.wrap = rndis_add_header;
947 rndis->port.unwrap = rndis_rm_hdr;
948
949 rndis->port.func.name = "rndis";
950 rndis->port.func.strings = rndis_strings;
951 /* descriptors are per-instance copies */
952 rndis->port.func.bind = rndis_bind;
953 rndis->port.func.unbind = rndis_unbind;
954 rndis->port.func.set_alt = rndis_set_alt;
955 rndis->port.func.setup = rndis_setup;
956 rndis->port.func.disable = rndis_disable;
957
958 status = usb_add_function(c, &rndis->port.func);
959 if (status) {
960 kfree(rndis);
961fail:
962 rndis_exit();
963 }
964 return status;
965}