blob: 814b4a3e4a5e4c6febe3e6ab3c9305a52f99b45a [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>
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +020020#include <linux/module.h>
David Brownell45fe3b82008-06-19 18:20:04 -070021#include <linux/device.h>
22#include <linux/etherdevice.h>
23
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
David Brownell45fe3b82008-06-19 18:20:04 -070025
26#include "u_ether.h"
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +020027#include "u_ether_configfs.h"
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +020028#include "u_rndis.h"
David Brownell45fe3b82008-06-19 18:20:04 -070029#include "rndis.h"
Andrzej Pietrasiewicza747b092014-05-08 14:06:27 +020030#include "configfs.h"
David Brownell45fe3b82008-06-19 18:20:04 -070031
David Brownell45fe3b82008-06-19 18:20:04 -070032/*
33 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
34 * been promoted instead of the standard CDC Ethernet. The published RNDIS
35 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
36 * ActiveSync have even worse status in terms of specification.
37 *
38 * In short: it's a protocol controlled by (and for) Microsoft, not for an
39 * Open ecosystem or markets. Linux supports it *only* because Microsoft
40 * doesn't support the CDC Ethernet standard.
41 *
42 * The RNDIS data transfer model is complex, with multiple Ethernet packets
43 * per USB message, and out of band data. The control model is built around
44 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
45 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
46 * useless (they're ignored). RNDIS expects to be the only function in its
47 * configuration, so it's no real help if you need composite devices; and
48 * it expects to be the first configuration too.
49 *
50 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
51 * discount the fluff that its RPC can be made to deliver: it doesn't need
52 * a NOP altsetting for the data interface. That lets it work on some of the
53 * "so smart it's stupid" hardware which takes over configuration changes
54 * from the software, and adds restrictions like "no altsettings".
55 *
56 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
57 * have all sorts of contrary-to-specification oddities that can prevent
58 * them from working sanely. Since bugfixes (or accurate specs, letting
59 * Linux work around those bugs) are unlikely to ever come from MSFT, you
60 * may want to avoid using RNDIS on purely operational grounds.
61 *
62 * Omissions from the RNDIS 1.0 specification include:
63 *
64 * - Power management ... references data that's scattered around lots
65 * of other documentation, which is incorrect/incomplete there too.
66 *
67 * - There are various undocumented protocol requirements, like the need
68 * to send garbage in some control-OUT messages.
69 *
70 * - MS-Windows drivers sometimes emit undocumented requests.
71 */
72
xerox_linf0539002014-09-04 16:01:59 +080073static unsigned int rndis_dl_max_pkt_per_xfer = 3;
74module_param(rndis_dl_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
75MODULE_PARM_DESC(rndis_dl_max_pkt_per_xfer,
76 "Maximum packets per transfer for DL aggregation");
xerox_lin72ffa182014-08-14 14:48:44 +080077
78static unsigned int rndis_ul_max_pkt_per_xfer = 3;
79module_param(rndis_ul_max_pkt_per_xfer, uint, S_IRUGO | S_IWUSR);
80MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer,
81 "Maximum packets per transfer for UL aggregation");
82
David Brownell45fe3b82008-06-19 18:20:04 -070083struct f_rndis {
84 struct gether port;
85 u8 ctrl_id, data_id;
86 u8 ethaddr[ETH_ALEN];
Benoit Goby1fbfeff2012-05-10 10:08:04 +020087 u32 vendorID;
88 const char *manufacturer;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +010089 struct rndis_params *params;
David Brownell45fe3b82008-06-19 18:20:04 -070090
David Brownell45fe3b82008-06-19 18:20:04 -070091 struct usb_ep *notify;
David Brownell45fe3b82008-06-19 18:20:04 -070092 struct usb_request *notify_req;
93 atomic_t notify_count;
94};
95
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +053096static struct f_rndis *__rndis;
97static spinlock_t _rndis_lock;
98
David Brownell45fe3b82008-06-19 18:20:04 -070099static inline struct f_rndis *func_to_rndis(struct usb_function *f)
100{
101 return container_of(f, struct f_rndis, port.func);
102}
103
104/* peak (theoretical) bulk transfer rate in bits-per-second */
105static unsigned int bitrate(struct usb_gadget *g)
106{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700107 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
108 return 13 * 1024 * 8 * 1000 * 8;
109 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
David Brownell45fe3b82008-06-19 18:20:04 -0700110 return 13 * 512 * 8 * 1000 * 8;
111 else
Paul Zimmerman04617db2011-06-27 14:13:18 -0700112 return 19 * 64 * 1 * 1000 * 8;
David Brownell45fe3b82008-06-19 18:20:04 -0700113}
114
115/*-------------------------------------------------------------------------*/
116
117/*
118 */
119
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200120#define RNDIS_STATUS_INTERVAL_MS 32
David Brownell45fe3b82008-06-19 18:20:04 -0700121#define STATUS_BYTECOUNT 8 /* 8 bytes data */
122
123
124/* interface descriptor: */
125
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200126static struct usb_interface_descriptor rndis_control_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700127 .bLength = sizeof rndis_control_intf,
128 .bDescriptorType = USB_DT_INTERFACE,
129
130 /* .bInterfaceNumber = DYNAMIC */
131 /* status endpoint is optional; this could be patched later */
132 .bNumEndpoints = 1,
133 .bInterfaceClass = USB_CLASS_COMM,
134 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
135 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
136 /* .iInterface = DYNAMIC */
137};
138
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200139static struct usb_cdc_header_desc header_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700140 .bLength = sizeof header_desc,
141 .bDescriptorType = USB_DT_CS_INTERFACE,
142 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
143
Harvey Harrison551509d2009-02-11 14:11:36 -0800144 .bcdCDC = cpu_to_le16(0x0110),
David Brownell45fe3b82008-06-19 18:20:04 -0700145};
146
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200147static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
David Brownell45fe3b82008-06-19 18:20:04 -0700148 .bLength = sizeof call_mgmt_descriptor,
149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
151
152 .bmCapabilities = 0x00,
153 .bDataInterface = 0x01,
154};
155
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200156static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100157 .bLength = sizeof rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700158 .bDescriptorType = USB_DT_CS_INTERFACE,
159 .bDescriptorSubType = USB_CDC_ACM_TYPE,
160
161 .bmCapabilities = 0x00,
162};
163
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200164static struct usb_cdc_union_desc rndis_union_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700165 .bLength = sizeof(rndis_union_desc),
166 .bDescriptorType = USB_DT_CS_INTERFACE,
167 .bDescriptorSubType = USB_CDC_UNION_TYPE,
168 /* .bMasterInterface0 = DYNAMIC */
169 /* .bSlaveInterface0 = DYNAMIC */
170};
171
172/* the data interface has two bulk endpoints */
173
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200174static struct usb_interface_descriptor rndis_data_intf = {
David Brownell45fe3b82008-06-19 18:20:04 -0700175 .bLength = sizeof rndis_data_intf,
176 .bDescriptorType = USB_DT_INTERFACE,
177
178 /* .bInterfaceNumber = DYNAMIC */
David Brownell45fe3b82008-06-19 18:20:04 -0700179 .bNumEndpoints = 2,
180 .bInterfaceClass = USB_CLASS_CDC_DATA,
181 .bInterfaceSubClass = 0,
182 .bInterfaceProtocol = 0,
183 /* .iInterface = DYNAMIC */
184};
185
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100186
187static struct usb_interface_assoc_descriptor
188rndis_iad_descriptor = {
189 .bLength = sizeof rndis_iad_descriptor,
190 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
191
192 .bFirstInterface = 0, /* XXX, hardcoded */
193 .bInterfaceCount = 2, // control + data
194 .bFunctionClass = USB_CLASS_COMM,
195 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
196 .bFunctionProtocol = USB_CDC_PROTO_NONE,
197 /* .iFunction = DYNAMIC */
198};
199
David Brownell45fe3b82008-06-19 18:20:04 -0700200/* full speed support: */
201
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200202static struct usb_endpoint_descriptor fs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
205
206 .bEndpointAddress = USB_DIR_IN,
207 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800208 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200209 .bInterval = RNDIS_STATUS_INTERVAL_MS,
David Brownell45fe3b82008-06-19 18:20:04 -0700210};
211
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200212static struct usb_endpoint_descriptor fs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700213 .bLength = USB_DT_ENDPOINT_SIZE,
214 .bDescriptorType = USB_DT_ENDPOINT,
215
216 .bEndpointAddress = USB_DIR_IN,
217 .bmAttributes = USB_ENDPOINT_XFER_BULK,
218};
219
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200220static struct usb_endpoint_descriptor fs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700221 .bLength = USB_DT_ENDPOINT_SIZE,
222 .bDescriptorType = USB_DT_ENDPOINT,
223
224 .bEndpointAddress = USB_DIR_OUT,
225 .bmAttributes = USB_ENDPOINT_XFER_BULK,
226};
227
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200228static struct usb_descriptor_header *eth_fs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100229 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700230
David Brownell45fe3b82008-06-19 18:20:04 -0700231 /* control interface matches ACM, not Ethernet */
232 (struct usb_descriptor_header *) &rndis_control_intf,
233 (struct usb_descriptor_header *) &header_desc,
234 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100235 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700236 (struct usb_descriptor_header *) &rndis_union_desc,
237 (struct usb_descriptor_header *) &fs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700238
David Brownell45fe3b82008-06-19 18:20:04 -0700239 /* data interface has no altsetting */
240 (struct usb_descriptor_header *) &rndis_data_intf,
241 (struct usb_descriptor_header *) &fs_in_desc,
242 (struct usb_descriptor_header *) &fs_out_desc,
243 NULL,
244};
245
246/* high speed support: */
247
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200248static struct usb_endpoint_descriptor hs_notify_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251
252 .bEndpointAddress = USB_DIR_IN,
253 .bmAttributes = USB_ENDPOINT_XFER_INT,
Harvey Harrison551509d2009-02-11 14:11:36 -0800254 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200255 .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
David Brownell45fe3b82008-06-19 18:20:04 -0700256};
Paul Zimmerman04617db2011-06-27 14:13:18 -0700257
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200258static struct usb_endpoint_descriptor hs_in_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700259 .bLength = USB_DT_ENDPOINT_SIZE,
260 .bDescriptorType = USB_DT_ENDPOINT,
261
262 .bEndpointAddress = USB_DIR_IN,
263 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800264 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700265};
266
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200267static struct usb_endpoint_descriptor hs_out_desc = {
David Brownell45fe3b82008-06-19 18:20:04 -0700268 .bLength = USB_DT_ENDPOINT_SIZE,
269 .bDescriptorType = USB_DT_ENDPOINT,
270
271 .bEndpointAddress = USB_DIR_OUT,
272 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Harvey Harrison551509d2009-02-11 14:11:36 -0800273 .wMaxPacketSize = cpu_to_le16(512),
David Brownell45fe3b82008-06-19 18:20:04 -0700274};
275
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200276static struct usb_descriptor_header *eth_hs_function[] = {
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100277 (struct usb_descriptor_header *) &rndis_iad_descriptor,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700278
David Brownell45fe3b82008-06-19 18:20:04 -0700279 /* control interface matches ACM, not Ethernet */
280 (struct usb_descriptor_header *) &rndis_control_intf,
281 (struct usb_descriptor_header *) &header_desc,
282 (struct usb_descriptor_header *) &call_mgmt_descriptor,
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100283 (struct usb_descriptor_header *) &rndis_acm_descriptor,
David Brownell45fe3b82008-06-19 18:20:04 -0700284 (struct usb_descriptor_header *) &rndis_union_desc,
285 (struct usb_descriptor_header *) &hs_notify_desc,
Paul Zimmerman04617db2011-06-27 14:13:18 -0700286
David Brownell45fe3b82008-06-19 18:20:04 -0700287 /* data interface has no altsetting */
288 (struct usb_descriptor_header *) &rndis_data_intf,
289 (struct usb_descriptor_header *) &hs_in_desc,
290 (struct usb_descriptor_header *) &hs_out_desc,
291 NULL,
292};
293
Paul Zimmerman04617db2011-06-27 14:13:18 -0700294/* super speed support: */
295
296static struct usb_endpoint_descriptor ss_notify_desc = {
297 .bLength = USB_DT_ENDPOINT_SIZE,
298 .bDescriptorType = USB_DT_ENDPOINT,
299
300 .bEndpointAddress = USB_DIR_IN,
301 .bmAttributes = USB_ENDPOINT_XFER_INT,
302 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
Sebastian Andrzej Siewiorbcb2f992012-10-22 22:14:57 +0200303 .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS)
Paul Zimmerman04617db2011-06-27 14:13:18 -0700304};
305
306static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = {
307 .bLength = sizeof ss_intr_comp_desc,
308 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
309
310 /* the following 3 values can be tweaked if necessary */
311 /* .bMaxBurst = 0, */
312 /* .bmAttributes = 0, */
313 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
314};
315
316static struct usb_endpoint_descriptor ss_in_desc = {
317 .bLength = USB_DT_ENDPOINT_SIZE,
318 .bDescriptorType = USB_DT_ENDPOINT,
319
320 .bEndpointAddress = USB_DIR_IN,
321 .bmAttributes = USB_ENDPOINT_XFER_BULK,
322 .wMaxPacketSize = cpu_to_le16(1024),
323};
324
325static struct usb_endpoint_descriptor ss_out_desc = {
326 .bLength = USB_DT_ENDPOINT_SIZE,
327 .bDescriptorType = USB_DT_ENDPOINT,
328
329 .bEndpointAddress = USB_DIR_OUT,
330 .bmAttributes = USB_ENDPOINT_XFER_BULK,
331 .wMaxPacketSize = cpu_to_le16(1024),
332};
333
334static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = {
335 .bLength = sizeof ss_bulk_comp_desc,
336 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
337
338 /* the following 2 values can be tweaked if necessary */
339 /* .bMaxBurst = 0, */
340 /* .bmAttributes = 0, */
341};
342
343static struct usb_descriptor_header *eth_ss_function[] = {
344 (struct usb_descriptor_header *) &rndis_iad_descriptor,
345
346 /* control interface matches ACM, not Ethernet */
347 (struct usb_descriptor_header *) &rndis_control_intf,
348 (struct usb_descriptor_header *) &header_desc,
349 (struct usb_descriptor_header *) &call_mgmt_descriptor,
350 (struct usb_descriptor_header *) &rndis_acm_descriptor,
351 (struct usb_descriptor_header *) &rndis_union_desc,
352 (struct usb_descriptor_header *) &ss_notify_desc,
353 (struct usb_descriptor_header *) &ss_intr_comp_desc,
354
355 /* data interface has no altsetting */
356 (struct usb_descriptor_header *) &rndis_data_intf,
357 (struct usb_descriptor_header *) &ss_in_desc,
358 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
359 (struct usb_descriptor_header *) &ss_out_desc,
360 (struct usb_descriptor_header *) &ss_bulk_comp_desc,
361 NULL,
362};
363
David Brownell45fe3b82008-06-19 18:20:04 -0700364/* string descriptors: */
365
366static struct usb_string rndis_string_defs[] = {
367 [0].s = "RNDIS Communications Control",
368 [1].s = "RNDIS Ethernet Data",
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100369 [2].s = "RNDIS",
David Brownell45fe3b82008-06-19 18:20:04 -0700370 { } /* end of list */
371};
372
373static struct usb_gadget_strings rndis_string_table = {
374 .language = 0x0409, /* en-us */
375 .strings = rndis_string_defs,
376};
377
378static struct usb_gadget_strings *rndis_strings[] = {
379 &rndis_string_table,
380 NULL,
381};
382
383/*-------------------------------------------------------------------------*/
384
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500385static struct sk_buff *rndis_add_header(struct gether *port,
386 struct sk_buff *skb)
David Brownell45fe3b82008-06-19 18:20:04 -0700387{
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500388 struct sk_buff *skb2;
389
Peter Chen80d16422016-08-11 15:51:46 +0800390 if (!skb)
391 return NULL;
392
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500393 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
Markus Elfring7b0f0002014-11-21 14:51:43 +0100394 rndis_add_hdr(skb2);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500395
Macpaul Lin2656c9e2014-04-15 16:09:33 +0800396 dev_kfree_skb(skb);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500397 return skb2;
David Brownell45fe3b82008-06-19 18:20:04 -0700398}
399
400static void rndis_response_available(void *_rndis)
401{
402 struct f_rndis *rndis = _rndis;
403 struct usb_request *req = rndis->notify_req;
404 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
405 __le32 *data = req->buf;
406 int status;
407
Richard Röjforsff349502008-11-15 19:53:24 -0800408 if (atomic_inc_return(&rndis->notify_count) != 1)
David Brownell45fe3b82008-06-19 18:20:04 -0700409 return;
410
411 /* Send RNDIS RESPONSE_AVAILABLE notification; a
412 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
413 *
414 * This is the only notification defined by RNDIS.
415 */
416 data[0] = cpu_to_le32(1);
417 data[1] = cpu_to_le32(0);
418
419 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
420 if (status) {
421 atomic_dec(&rndis->notify_count);
422 DBG(cdev, "notify/0 --> %d\n", status);
423 }
424}
425
426static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
427{
Vijayavardhan Vennapusad251f8b2016-03-17 10:39:56 +0530428 struct f_rndis *rndis;
429 struct usb_composite_dev *cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700430 int status = req->status;
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530431 struct usb_ep *notify_ep;
432
433 spin_lock(&_rndis_lock);
Vijayavardhan Vennapusad251f8b2016-03-17 10:39:56 +0530434 rndis = __rndis;
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530435 if (!rndis || !rndis->notify) {
436 spin_unlock(&_rndis_lock);
437 return;
438 }
David Brownell45fe3b82008-06-19 18:20:04 -0700439
Vijayavardhan Vennapusad251f8b2016-03-17 10:39:56 +0530440 cdev = rndis->port.func.config->cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700441 /* after TX:
442 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
443 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
444 */
445 switch (status) {
446 case -ECONNRESET:
447 case -ESHUTDOWN:
448 /* connection gone */
449 atomic_set(&rndis->notify_count, 0);
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530450 goto out;
David Brownell45fe3b82008-06-19 18:20:04 -0700451 default:
452 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
453 ep->name, status,
454 req->actual, req->length);
455 /* FALLTHROUGH */
456 case 0:
457 if (ep != rndis->notify)
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530458 goto out;
David Brownell45fe3b82008-06-19 18:20:04 -0700459
460 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
461 * notifications by resending until we're done
462 */
463 if (atomic_dec_and_test(&rndis->notify_count))
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530464 goto out;
465 notify_ep = rndis->notify;
466 spin_unlock(&_rndis_lock);
467 status = usb_ep_queue(notify_ep, req, GFP_ATOMIC);
David Brownell45fe3b82008-06-19 18:20:04 -0700468 if (status) {
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530469 spin_lock(&_rndis_lock);
470 if (!__rndis)
471 goto out;
472 atomic_dec(&__rndis->notify_count);
David Brownell45fe3b82008-06-19 18:20:04 -0700473 DBG(cdev, "notify/1 --> %d\n", status);
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530474 spin_unlock(&_rndis_lock);
David Brownell45fe3b82008-06-19 18:20:04 -0700475 }
David Brownell45fe3b82008-06-19 18:20:04 -0700476 }
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530477
478 return;
479
480out:
481 spin_unlock(&_rndis_lock);
David Brownell45fe3b82008-06-19 18:20:04 -0700482}
483
484static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
485{
Vijayavardhan Vennapusad251f8b2016-03-17 10:39:56 +0530486 struct f_rndis *rndis;
487 struct usb_composite_dev *cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700488 int status;
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700489 rndis_init_msg_type *buf;
David Brownell45fe3b82008-06-19 18:20:04 -0700490
Sriharsha Allenkie1cc61d2017-07-27 11:12:09 +0530491 if (req->status != 0) {
492 pr_err("%s: RNDIS command completion error:%d\n",
493 __func__, req->status);
494 return;
495 }
496
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530497 spin_lock(&_rndis_lock);
Vijayavardhan Vennapusad251f8b2016-03-17 10:39:56 +0530498 rndis = __rndis;
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530499 if (!rndis || !rndis->notify) {
500 spin_unlock(&_rndis_lock);
Vijayavardhan Vennapusac23003a2016-03-04 10:09:39 +0530501 return;
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530502 }
Vijayavardhan Vennapusac23003a2016-03-04 10:09:39 +0530503
Vijayavardhan Vennapusad251f8b2016-03-17 10:39:56 +0530504 cdev = rndis->port.func.config->cdev;
David Brownell45fe3b82008-06-19 18:20:04 -0700505 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
506// spin_lock(&dev->lock);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100507 status = rndis_msg_parser(rndis->params, (u8 *) req->buf);
David Brownell45fe3b82008-06-19 18:20:04 -0700508 if (status < 0)
Truls Bengtsson967baed2013-03-20 14:02:25 +0100509 pr_err("RNDIS command error %d, %d/%d\n",
David Brownell45fe3b82008-06-19 18:20:04 -0700510 status, req->actual, req->length);
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700511
512 buf = (rndis_init_msg_type *)req->buf;
513
514 if (buf->MessageType == RNDIS_MSG_INIT) {
515 if (buf->MaxTransferSize > 2048)
516 rndis->port.multi_pkt_xfer = 1;
517 else
518 rndis->port.multi_pkt_xfer = 0;
519 DBG(cdev, "%s: MaxTransferSize: %d : Multi_pkt_txr: %s\n",
520 __func__, buf->MaxTransferSize,
521 rndis->port.multi_pkt_xfer ? "enabled" :
522 "disabled");
xerox_linf0539002014-09-04 16:01:59 +0800523 if (rndis_dl_max_pkt_per_xfer <= 1)
Badhri Jagan Sridharanccf77072014-09-18 10:48:48 -0700524 rndis->port.multi_pkt_xfer = 0;
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700525 }
David Brownell45fe3b82008-06-19 18:20:04 -0700526// spin_unlock(&dev->lock);
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530527 spin_unlock(&_rndis_lock);
David Brownell45fe3b82008-06-19 18:20:04 -0700528}
529
530static int
531rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
532{
533 struct f_rndis *rndis = func_to_rndis(f);
534 struct usb_composite_dev *cdev = f->config->cdev;
535 struct usb_request *req = cdev->req;
536 int value = -EOPNOTSUPP;
537 u16 w_index = le16_to_cpu(ctrl->wIndex);
538 u16 w_value = le16_to_cpu(ctrl->wValue);
539 u16 w_length = le16_to_cpu(ctrl->wLength);
540
541 /* composite driver infrastructure handles everything except
542 * CDC class messages; interface activation uses set_alt().
543 */
544 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
545
546 /* RNDIS uses the CDC command encapsulation mechanism to implement
547 * an RPC scheme, with much getting/setting of attributes by OID.
548 */
549 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
550 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
Felipe Balbi472b9122011-05-13 16:53:48 +0300551 if (w_value || w_index != rndis->ctrl_id)
David Brownell45fe3b82008-06-19 18:20:04 -0700552 goto invalid;
553 /* read the request; process it later */
554 value = w_length;
555 req->complete = rndis_command_complete;
556 req->context = rndis;
557 /* later, rndis_response_available() sends a notification */
558 break;
559
560 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
561 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
562 if (w_value || w_index != rndis->ctrl_id)
563 goto invalid;
564 else {
565 u8 *buf;
566 u32 n;
567
568 /* return the result */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100569 buf = rndis_get_next_response(rndis->params, &n);
David Brownell45fe3b82008-06-19 18:20:04 -0700570 if (buf) {
571 memcpy(req->buf, buf, n);
572 req->complete = rndis_response_complete;
Lukasz Majewskif1356172012-02-10 09:54:51 +0100573 req->context = rndis;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100574 rndis_free_response(rndis->params, buf);
David Brownell45fe3b82008-06-19 18:20:04 -0700575 value = n;
576 }
577 /* else stalls ... spec says to avoid that */
578 }
579 break;
580
581 default:
582invalid:
583 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
584 ctrl->bRequestType, ctrl->bRequest,
585 w_value, w_index, w_length);
586 }
587
588 /* respond with data transfer or status phase? */
589 if (value >= 0) {
590 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
591 ctrl->bRequestType, ctrl->bRequest,
592 w_value, w_index, w_length);
David Brownell090b9012009-03-20 01:08:20 -0700593 req->zero = (value < w_length);
David Brownell45fe3b82008-06-19 18:20:04 -0700594 req->length = value;
595 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
596 if (value < 0)
597 ERROR(cdev, "rndis response on err %d\n", value);
598 }
599
600 /* device either stalls (value < 0) or reports success */
601 return value;
602}
603
604
605static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
606{
607 struct f_rndis *rndis = func_to_rndis(f);
608 struct usb_composite_dev *cdev = f->config->cdev;
609
610 /* we know alt == 0 */
611
612 if (intf == rndis->ctrl_id) {
Robert Baldygabbc474f2015-09-16 12:10:54 +0200613 VDBG(cdev, "reset rndis control %d\n", intf);
614 usb_ep_disable(rndis->notify);
615
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300616 if (!rndis->notify->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700617 VDBG(cdev, "init rndis ctrl %d\n", intf);
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300618 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
619 goto fail;
David Brownell45fe3b82008-06-19 18:20:04 -0700620 }
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300621 usb_ep_enable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700622
623 } else if (intf == rndis->data_id) {
624 struct net_device *net;
625
Robert Baldygabbc474f2015-09-16 12:10:54 +0200626 if (rndis->port.in_ep->enabled) {
David Brownell45fe3b82008-06-19 18:20:04 -0700627 DBG(cdev, "reset rndis\n");
628 gether_disconnect(&rndis->port);
Maulik Mankad830d1b12009-05-29 18:34:40 +0530629 }
630
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300631 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
David Brownell45fe3b82008-06-19 18:20:04 -0700632 DBG(cdev, "init rndis\n");
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300633 if (config_ep_by_speed(cdev->gadget, f,
634 rndis->port.in_ep) ||
635 config_ep_by_speed(cdev->gadget, f,
636 rndis->port.out_ep)) {
637 rndis->port.in_ep->desc = NULL;
638 rndis->port.out_ep->desc = NULL;
639 goto fail;
640 }
David Brownell45fe3b82008-06-19 18:20:04 -0700641 }
642
643 /* Avoid ZLPs; they can be troublesome. */
644 rndis->port.is_zlp_ok = false;
645
646 /* RNDIS should be in the "RNDIS uninitialized" state,
647 * either never activated or after rndis_uninit().
648 *
649 * We don't want data to flow here until a nonzero packet
650 * filter is set, at which point it enters "RNDIS data
651 * initialized" state ... but we do want the endpoints
652 * to be activated. It's a strange little state.
653 *
654 * REVISIT the RNDIS gadget code has done this wrong for a
655 * very long time. We need another call to the link layer
656 * code -- gether_updown(...bool) maybe -- to do it right.
657 */
658 rndis->port.cdc_filter = 0;
659
660 DBG(cdev, "RNDIS RX/TX early activation ... \n");
661 net = gether_connect(&rndis->port);
662 if (IS_ERR(net))
663 return PTR_ERR(net);
664
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100665 rndis_set_param_dev(rndis->params, net,
David Brownell45fe3b82008-06-19 18:20:04 -0700666 &rndis->port.cdc_filter);
667 } else
668 goto fail;
669
670 return 0;
671fail:
672 return -EINVAL;
673}
674
675static void rndis_disable(struct usb_function *f)
676{
677 struct f_rndis *rndis = func_to_rndis(f);
678 struct usb_composite_dev *cdev = f->config->cdev;
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530679 unsigned long flags;
David Brownell45fe3b82008-06-19 18:20:04 -0700680
Robert Baldygabbc474f2015-09-16 12:10:54 +0200681 if (!rndis->notify->enabled)
David Brownell45fe3b82008-06-19 18:20:04 -0700682 return;
683
684 DBG(cdev, "rndis deactivated\n");
685
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530686 spin_lock_irqsave(&_rndis_lock, flags);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100687 rndis_uninit(rndis->params);
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530688 spin_unlock_irqrestore(&_rndis_lock, flags);
David Brownell45fe3b82008-06-19 18:20:04 -0700689 gether_disconnect(&rndis->port);
690
691 usb_ep_disable(rndis->notify);
David Brownell45fe3b82008-06-19 18:20:04 -0700692}
693
694/*-------------------------------------------------------------------------*/
695
696/*
697 * This isn't quite the same mechanism as CDC Ethernet, since the
698 * notification scheme passes less data, but the same set of link
699 * states must be tested. A key difference is that altsettings are
700 * not used to tell whether the link should send packets or not.
701 */
702
703static void rndis_open(struct gether *geth)
704{
705 struct f_rndis *rndis = func_to_rndis(&geth->func);
706 struct usb_composite_dev *cdev = geth->func.config->cdev;
707
708 DBG(cdev, "%s\n", __func__);
709
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100710 rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3,
David Brownell45fe3b82008-06-19 18:20:04 -0700711 bitrate(cdev->gadget) / 100);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100712 rndis_signal_connect(rndis->params);
David Brownell45fe3b82008-06-19 18:20:04 -0700713}
714
715static void rndis_close(struct gether *geth)
716{
717 struct f_rndis *rndis = func_to_rndis(&geth->func);
718
719 DBG(geth->func.config->cdev, "%s\n", __func__);
720
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100721 rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3, 0);
722 rndis_signal_disconnect(rndis->params);
David Brownell45fe3b82008-06-19 18:20:04 -0700723}
724
725/*-------------------------------------------------------------------------*/
726
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200727/* Some controllers can't support RNDIS ... */
728static inline bool can_support_rndis(struct usb_configuration *c)
729{
730 /* everything else is *presumably* fine */
731 return true;
732}
733
David Brownell45fe3b82008-06-19 18:20:04 -0700734/* ethernet function driver setup/binding */
735
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200736static int
David Brownell45fe3b82008-06-19 18:20:04 -0700737rndis_bind(struct usb_configuration *c, struct usb_function *f)
738{
739 struct usb_composite_dev *cdev = c->cdev;
740 struct f_rndis *rndis = func_to_rndis(f);
Andrzej Pietrasiewicz4e75e722013-05-28 09:16:00 +0200741 struct usb_string *us;
David Brownell45fe3b82008-06-19 18:20:04 -0700742 int status;
743 struct usb_ep *ep;
744
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200745 struct f_rndis_opts *rndis_opts;
746
747 if (!can_support_rndis(c))
748 return -EINVAL;
749
750 rndis_opts = container_of(f->fi, struct f_rndis_opts, func_inst);
751
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +0200752 if (cdev->use_os_string) {
753 f->os_desc_table = kzalloc(sizeof(*f->os_desc_table),
754 GFP_KERNEL);
755 if (!f->os_desc_table)
Dan Carpenter4683ae82014-05-21 15:26:55 +0300756 return -ENOMEM;
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +0200757 f->os_desc_n = 1;
758 f->os_desc_table[0].os_desc = &rndis_opts->rndis_os_desc;
759 }
760
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200761 /*
762 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
763 * configurations are bound in sequence with list_for_each_entry,
764 * in each configuration its functions are bound in sequence
765 * with list_for_each_entry, so we assume no race condition
766 * with regard to rndis_opts->bound access
767 */
768 if (!rndis_opts->bound) {
Ajay Agarwal200f4842018-04-26 16:44:27 +0530769 mutex_lock(&rndis_opts->lock);
Ajay Agarwala3cd9802018-05-15 14:47:49 +0530770 rndis_opts->net = gether_setup_name_default("rndis");
Ajay Agarwal200f4842018-04-26 16:44:27 +0530771 if (IS_ERR(rndis_opts->net)) {
772 status = PTR_ERR(rndis_opts->net);
773 mutex_unlock(&rndis_opts->lock);
774 goto error;
775 }
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200776 gether_set_gadget(rndis_opts->net, cdev->gadget);
777 status = gether_register_netdev(rndis_opts->net);
Ajay Agarwal200f4842018-04-26 16:44:27 +0530778 mutex_unlock(&rndis_opts->lock);
779 if (status) {
780 free_netdev(rndis_opts->net);
781 goto error;
782 }
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200783 rndis_opts->bound = true;
784 }
Andrzej Pietrasiewicz31f89db2013-12-03 15:15:28 +0100785
Ajay Agarwal200f4842018-04-26 16:44:27 +0530786 gether_get_host_addr_u8(rndis_opts->net, rndis->ethaddr);
787 rndis->port.ioport = netdev_priv(rndis_opts->net);
788
Andrzej Pietrasiewicz4e75e722013-05-28 09:16:00 +0200789 us = usb_gstrings_attach(cdev, rndis_strings,
790 ARRAY_SIZE(rndis_string_defs));
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +0200791 if (IS_ERR(us)) {
792 status = PTR_ERR(us);
Ajay Agarwal200f4842018-04-26 16:44:27 +0530793 goto netdev_cleanup;
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +0200794 }
Andrzej Pietrasiewicz4e75e722013-05-28 09:16:00 +0200795 rndis_control_intf.iInterface = us[0].id;
796 rndis_data_intf.iInterface = us[1].id;
797 rndis_iad_descriptor.iFunction = us[2].id;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200798
David Brownell45fe3b82008-06-19 18:20:04 -0700799 /* allocate instance-specific interface IDs */
800 status = usb_interface_id(c, f);
801 if (status < 0)
802 goto fail;
803 rndis->ctrl_id = status;
Michal Nazarewiczb97503f2009-10-28 16:57:30 +0100804 rndis_iad_descriptor.bFirstInterface = status;
David Brownell45fe3b82008-06-19 18:20:04 -0700805
806 rndis_control_intf.bInterfaceNumber = status;
807 rndis_union_desc.bMasterInterface0 = status;
808
Andrzej Pietrasiewicz45465272014-07-01 15:47:47 +0200809 if (cdev->use_os_string)
810 f->os_desc_table[0].if_id =
811 rndis_iad_descriptor.bFirstInterface;
812
David Brownell45fe3b82008-06-19 18:20:04 -0700813 status = usb_interface_id(c, f);
814 if (status < 0)
815 goto fail;
816 rndis->data_id = status;
817
818 rndis_data_intf.bInterfaceNumber = status;
819 rndis_union_desc.bSlaveInterface0 = status;
820
821 status = -ENODEV;
822
823 /* allocate instance-specific endpoints */
824 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
825 if (!ep)
826 goto fail;
827 rndis->port.in_ep = ep;
David Brownell45fe3b82008-06-19 18:20:04 -0700828
829 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
830 if (!ep)
831 goto fail;
832 rndis->port.out_ep = ep;
David Brownell45fe3b82008-06-19 18:20:04 -0700833
834 /* NOTE: a status/notification endpoint is, strictly speaking,
835 * optional. We don't treat it that way though! It's simpler,
836 * and some newer profiles don't treat it as optional.
837 */
838 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
839 if (!ep)
840 goto fail;
841 rndis->notify = ep;
David Brownell45fe3b82008-06-19 18:20:04 -0700842
843 status = -ENOMEM;
844
845 /* allocate notification request and buffer */
846 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
847 if (!rndis->notify_req)
848 goto fail;
ChandanaKishori Chiluveru2b5ed572015-08-05 15:30:40 +0530849 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT +
850 cdev->gadget->extra_buf_alloc, GFP_KERNEL);
David Brownell45fe3b82008-06-19 18:20:04 -0700851 if (!rndis->notify_req->buf)
852 goto fail;
853 rndis->notify_req->length = STATUS_BYTECOUNT;
854 rndis->notify_req->context = rndis;
855 rndis->notify_req->complete = rndis_response_complete;
856
David Brownell45fe3b82008-06-19 18:20:04 -0700857 /* support all relevant hardware speeds... we expect that when
858 * hardware is dual speed, all bulk-capable endpoints work at
859 * both speeds
860 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200861 hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
862 hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
863 hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700864
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200865 ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress;
866 ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress;
867 ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress;
David Brownell45fe3b82008-06-19 18:20:04 -0700868
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200869 status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function,
John Youneaef50c2016-02-05 17:06:07 -0800870 eth_ss_function, NULL);
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200871 if (status)
872 goto fail;
Paul Zimmerman04617db2011-06-27 14:13:18 -0700873
David Brownell45fe3b82008-06-19 18:20:04 -0700874 rndis->port.open = rndis_open;
875 rndis->port.close = rndis_close;
876
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100877 rndis_set_param_medium(rndis->params, RNDIS_MEDIUM_802_3, 0);
878 rndis_set_host_mac(rndis->params, rndis->ethaddr);
Amit Pundird29237a2015-12-21 14:03:15 +0530879 rndis_set_max_pkt_xfer(rndis->params, rndis_ul_max_pkt_per_xfer);
David Brownell45fe3b82008-06-19 18:20:04 -0700880
Benoit Goby1fbfeff2012-05-10 10:08:04 +0200881 if (rndis->manufacturer && rndis->vendorID &&
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100882 rndis_set_param_vendor(rndis->params, rndis->vendorID,
Pavitrakumar Managutte9b176352014-10-22 19:33:22 +0530883 rndis->manufacturer)) {
884 status = -EINVAL;
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +0530885 goto fail_free_descs;
Pavitrakumar Managutte9b176352014-10-22 19:33:22 +0530886 }
David Brownell45fe3b82008-06-19 18:20:04 -0700887
888 /* NOTE: all that is done without knowing or caring about
889 * the network link ... which is unavailable to this code
890 * until we're activated via set_alt().
891 */
892
893 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
Paul Zimmerman04617db2011-06-27 14:13:18 -0700894 gadget_is_superspeed(c->cdev->gadget) ? "super" :
David Brownell45fe3b82008-06-19 18:20:04 -0700895 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
896 rndis->port.in_ep->name, rndis->port.out_ep->name,
897 rndis->notify->name);
898 return 0;
899
Pavitrakumar Managutted12a8722014-10-22 19:24:58 +0530900fail_free_descs:
901 usb_free_all_descriptors(f);
David Brownell45fe3b82008-06-19 18:20:04 -0700902fail:
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +0200903 kfree(f->os_desc_table);
904 f->os_desc_n = 0;
David Brownell45fe3b82008-06-19 18:20:04 -0700905
906 if (rndis->notify_req) {
907 kfree(rndis->notify_req->buf);
908 usb_ep_free_request(rndis->notify, rndis->notify_req);
909 }
Ajay Agarwal200f4842018-04-26 16:44:27 +0530910netdev_cleanup:
911 gether_cleanup(rndis->port.ioport);
912error:
David Brownell45fe3b82008-06-19 18:20:04 -0700913 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
914
915 return status;
916}
917
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200918void rndis_borrow_net(struct usb_function_instance *f, struct net_device *net)
919{
920 struct f_rndis_opts *opts;
921
922 opts = container_of(f, struct f_rndis_opts, func_inst);
923 if (opts->bound)
924 gether_cleanup(netdev_priv(opts->net));
925 else
926 free_netdev(opts->net);
927 opts->borrowed_net = opts->bound = true;
928 opts->net = net;
929}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500930EXPORT_SYMBOL_GPL(rndis_borrow_net);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200931
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +0200932static inline struct f_rndis_opts *to_f_rndis_opts(struct config_item *item)
933{
934 return container_of(to_config_group(item), struct f_rndis_opts,
935 func_inst.group);
936}
937
938/* f_rndis_item_ops */
939USB_ETHERNET_CONFIGFS_ITEM(rndis);
940
941/* f_rndis_opts_dev_addr */
942USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(rndis);
943
944/* f_rndis_opts_host_addr */
945USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(rndis);
946
947/* f_rndis_opts_qmult */
948USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(rndis);
949
950/* f_rndis_opts_ifname */
951USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(rndis);
952
953static struct configfs_attribute *rndis_attrs[] = {
Christoph Hellwigf9a63da2015-10-03 15:32:42 +0200954 &rndis_opts_attr_dev_addr,
955 &rndis_opts_attr_host_addr,
956 &rndis_opts_attr_qmult,
957 &rndis_opts_attr_ifname,
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +0200958 NULL,
959};
960
961static struct config_item_type rndis_func_type = {
962 .ct_item_ops = &rndis_item_ops,
963 .ct_attrs = rndis_attrs,
964 .ct_owner = THIS_MODULE,
965};
966
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200967static void rndis_free_inst(struct usb_function_instance *f)
968{
969 struct f_rndis_opts *opts;
970
971 opts = container_of(f, struct f_rndis_opts, func_inst);
972 if (!opts->borrowed_net) {
973 if (opts->bound)
974 gether_cleanup(netdev_priv(opts->net));
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200975 }
Andrzej Pietrasiewicza747b092014-05-08 14:06:27 +0200976
Andrew Gabbasov3c57f9d2017-09-30 08:54:52 -0700977 kfree(opts->rndis_interf_group); /* single VLA chunk */
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200978 kfree(opts);
979}
980
981static struct usb_function_instance *rndis_alloc_inst(void)
982{
983 struct f_rndis_opts *opts;
Andrzej Pietrasiewicza747b092014-05-08 14:06:27 +0200984 struct usb_os_desc *descs[1];
Andrzej Pietrasiewicz14574b52014-06-18 14:24:49 +0200985 char *names[1];
Andrew Gabbasov3c57f9d2017-09-30 08:54:52 -0700986 struct config_group *rndis_interf_group;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200987
988 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
989 if (!opts)
990 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +0200991 opts->rndis_os_desc.ext_compat_id = opts->rndis_ext_compat_id;
992
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +0200993 mutex_init(&opts->lock);
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +0530994 spin_lock_init(&_rndis_lock);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200995 opts->func_inst.free_func_inst = rndis_free_inst;
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +0200996 INIT_LIST_HEAD(&opts->rndis_os_desc.ext_prop);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +0200997
Andrzej Pietrasiewicza747b092014-05-08 14:06:27 +0200998 descs[0] = &opts->rndis_os_desc;
Andrzej Pietrasiewicz14574b52014-06-18 14:24:49 +0200999 names[0] = "rndis";
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +02001000 config_group_init_type_name(&opts->func_inst.group, "",
1001 &rndis_func_type);
Andrew Gabbasov3c57f9d2017-09-30 08:54:52 -07001002 rndis_interf_group =
1003 usb_os_desc_prepare_interf_dir(&opts->func_inst.group, 1, descs,
1004 names, THIS_MODULE);
1005 if (IS_ERR(rndis_interf_group)) {
1006 rndis_free_inst(&opts->func_inst);
1007 return ERR_CAST(rndis_interf_group);
1008 }
1009 opts->rndis_interf_group = rndis_interf_group;
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +02001010
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001011 return &opts->func_inst;
1012}
1013
1014static void rndis_free(struct usb_function *f)
1015{
1016 struct f_rndis *rndis;
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +02001017 struct f_rndis_opts *opts;
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +05301018 unsigned long flags;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001019
1020 rndis = func_to_rndis(f);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001021 rndis_deregister(rndis->params);
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +02001022 opts = container_of(f->fi, struct f_rndis_opts, func_inst);
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +05301023 spin_lock_irqsave(&_rndis_lock, flags);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001024 kfree(rndis);
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +05301025 __rndis = NULL;
1026 spin_unlock_irqrestore(&_rndis_lock, flags);
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +02001027 mutex_lock(&opts->lock);
1028 opts->refcnt--;
1029 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001030}
1031
1032static void rndis_unbind(struct usb_configuration *c, struct usb_function *f)
1033{
1034 struct f_rndis *rndis = func_to_rndis(f);
Ajay Agarwal200f4842018-04-26 16:44:27 +05301035 struct f_rndis_opts *opts = container_of(f->fi, struct f_rndis_opts,
1036 func_inst);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001037
Andrzej Pietrasiewiczde7a8d22014-05-08 14:06:24 +02001038 kfree(f->os_desc_table);
1039 f->os_desc_n = 0;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001040 usb_free_all_descriptors(f);
1041
1042 kfree(rndis->notify_req->buf);
1043 usb_ep_free_request(rndis->notify, rndis->notify_req);
Ajay Agarwal200f4842018-04-26 16:44:27 +05301044 gether_cleanup(rndis->port.ioport);
1045 opts->bound = false;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001046}
1047
1048static struct usb_function *rndis_alloc(struct usb_function_instance *fi)
1049{
1050 struct f_rndis *rndis;
1051 struct f_rndis_opts *opts;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001052 struct rndis_params *params;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001053
1054 /* allocate and initialize one new instance */
1055 rndis = kzalloc(sizeof(*rndis), GFP_KERNEL);
Andrzej Pietrasiewicz76da66d2013-05-28 09:15:59 +02001056 if (!rndis)
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001057 return ERR_PTR(-ENOMEM);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001058
Vijayavardhan Vennapusa341fa072016-03-09 13:57:02 +05301059 __rndis = rndis;
1060
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001061 opts = container_of(fi, struct f_rndis_opts, func_inst);
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +02001062 mutex_lock(&opts->lock);
1063 opts->refcnt++;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001064
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001065 rndis->vendorID = opts->vendor_id;
1066 rndis->manufacturer = opts->manufacturer;
1067
Andrzej Pietrasiewiczb3df2fa2013-05-28 09:16:01 +02001068 mutex_unlock(&opts->lock);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001069 /* RNDIS activates when the host changes this filter */
1070 rndis->port.cdc_filter = 0;
1071
1072 /* RNDIS has special (and complex) framing */
1073 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
1074 rndis->port.wrap = rndis_add_header;
1075 rndis->port.unwrap = rndis_rm_hdr;
xerox_lin72ffa182014-08-14 14:48:44 +08001076 rndis->port.ul_max_pkts_per_xfer = rndis_ul_max_pkt_per_xfer;
xerox_linf0539002014-09-04 16:01:59 +08001077 rndis->port.dl_max_pkts_per_xfer = rndis_dl_max_pkt_per_xfer;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001078
1079 rndis->port.func.name = "rndis";
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001080 /* descriptors are per-instance copies */
1081 rndis->port.func.bind = rndis_bind;
1082 rndis->port.func.unbind = rndis_unbind;
1083 rndis->port.func.set_alt = rndis_set_alt;
1084 rndis->port.func.setup = rndis_setup;
1085 rndis->port.func.disable = rndis_disable;
1086 rndis->port.func.free_func = rndis_free;
1087
Hemant Kumar8b060292016-12-06 15:58:20 -08001088 params = rndis_register(rndis_response_available, rndis, NULL);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001089 if (IS_ERR(params)) {
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001090 kfree(rndis);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001091 return ERR_CAST(params);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001092 }
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001093 rndis->params = params;
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001094
1095 return &rndis->port.func;
1096}
1097
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +01001098DECLARE_USB_FUNCTION_INIT(rndis, rndis_alloc_inst, rndis_alloc);
Andrzej Pietrasiewiczf466c632013-05-28 09:15:57 +02001099MODULE_LICENSE("GPL");
1100MODULE_AUTHOR("David Brownell");