blob: 5d8e6fad5328ec4f7ce4c06f3814c5cad2dfabd9 [file] [log] [blame]
David Brownell7e27f182006-06-13 09:54:40 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * RNDIS MSG parser
David Brownell7e27f182006-06-13 09:54:40 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Benedikt Spranger, Pengutronix
David Brownell7e27f182006-06-13 09:54:40 -07005 * Robert Schwebel, Pengutronix
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
David Brownell7e27f182006-06-13 09:54:40 -07009 * version 2, as published by the Free Software Foundation.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This software was originally developed in conformance with
12 * Microsoft's Remote NDIS Specification License Agreement.
David Brownell7e27f182006-06-13 09:54:40 -070013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
15 * Fixed message length bug in init_response
David Brownell7e27f182006-06-13 09:54:40 -070016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
David Brownell7e27f182006-06-13 09:54:40 -070018 * Fixed rndis_rm_hdr length bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * Copyright (C) 2004 by David Brownell
21 * updates to merge with Linux 2.6, better match RNDIS spec
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010028#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/list.h>
30#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Alexey Dobriyane184d5f2008-05-14 16:25:13 -070032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/netdevice.h>
34
35#include <asm/io.h>
36#include <asm/byteorder.h>
David Brownell6cdee102005-04-18 17:39:34 -070037#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Fabio Estevamae8dd0c2014-04-04 22:27:59 -030039#include "u_rndis.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Brownell15b2d2b2008-06-19 18:19:16 -070041#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "rndis.h"
44
xerox_lin72ffa182014-08-14 14:48:44 +080045int rndis_ul_max_pkt_per_xfer_rcvd;
46module_param(rndis_ul_max_pkt_per_xfer_rcvd, int, S_IRUGO);
47MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer_rcvd,
48 "Max num of REMOTE_NDIS_PACKET_MSGs received in a single transfer");
49
50int rndis_ul_max_xfer_size_rcvd;
51module_param(rndis_ul_max_xfer_size_rcvd, int, S_IRUGO);
52MODULE_PARM_DESC(rndis_ul_max_xfer_size_rcvd,
53 "Max size of bus transfer received");
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* The driver for your USB chip needs to support ep0 OUT to work with
57 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
58 *
59 * Windows hosts need an INF file like Documentation/usb/linux.inf
60 * and will be happier if you provide the host_addr module parameter.
61 */
62
63#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070065module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#endif
70
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010071#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010073#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010075#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
76
77static DEFINE_IDA(rndis_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030080static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* Function Prototypes */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +010083static rndis_resp_t *rndis_add_response(struct rndis_params *params,
84 u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010086#ifdef CONFIG_USB_GADGET_DEBUG_FILES
87
88static const struct file_operations rndis_proc_fops;
89
90#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
David Brownell340600a2005-04-28 13:45:25 -070092/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030093static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070094{
95 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000096 RNDIS_OID_GEN_SUPPORTED_LIST,
97 RNDIS_OID_GEN_HARDWARE_STATUS,
98 RNDIS_OID_GEN_MEDIA_SUPPORTED,
99 RNDIS_OID_GEN_MEDIA_IN_USE,
100 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
101 RNDIS_OID_GEN_LINK_SPEED,
102 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
103 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
104 RNDIS_OID_GEN_VENDOR_ID,
105 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
106 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
107 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
108 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
109 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
110 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -0700111
David Brownell340600a2005-04-28 13:45:25 -0700112 /* the statistical stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000113 RNDIS_OID_GEN_XMIT_OK,
114 RNDIS_OID_GEN_RCV_OK,
115 RNDIS_OID_GEN_XMIT_ERROR,
116 RNDIS_OID_GEN_RCV_ERROR,
117 RNDIS_OID_GEN_RCV_NO_BUFFER,
David Brownell340600a2005-04-28 13:45:25 -0700118#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij8cdddc32012-05-11 22:16:08 +0000119 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
120 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
121 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
122 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
123 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
124 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
125 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
126 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
127 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
128 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
129 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
130 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
131 RNDIS_OID_GEN_RCV_CRC_ERROR,
132 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
David Brownell340600a2005-04-28 13:45:25 -0700133#endif /* RNDIS_OPTIONAL_STATS */
134
David Brownell7e27f182006-06-13 09:54:40 -0700135 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700136 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000137 RNDIS_OID_802_3_PERMANENT_ADDRESS,
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000138 RNDIS_OID_802_3_CURRENT_ADDRESS,
139 RNDIS_OID_802_3_MULTICAST_LIST,
140 RNDIS_OID_802_3_MAC_OPTIONS,
141 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700142
David Brownell340600a2005-04-28 13:45:25 -0700143 /* the statistical stuff */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000144 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
145 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
146 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700147#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000148 RNDIS_OID_802_3_XMIT_DEFERRED,
149 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
150 RNDIS_OID_802_3_RCV_OVERRUN,
151 RNDIS_OID_802_3_XMIT_UNDERRUN,
152 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
153 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
154 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700155#endif /* RNDIS_OPTIONAL_STATS */
156
157#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700158 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
159 * don't say what they mean ... and the NDIS specs are often
160 * confusing and/or ambiguous in this context. (That is, more
161 * so than their specs for the other OIDs.)
162 *
163 * FIXME someone who knows what these should do, please
164 * implement them!
165 */
David Brownell340600a2005-04-28 13:45:25 -0700166
167 /* power management */
168 OID_PNP_CAPABILITIES,
169 OID_PNP_QUERY_POWER,
170 OID_PNP_SET_POWER,
171
172#ifdef RNDIS_WAKEUP
173 /* wake up host */
174 OID_PNP_ENABLE_WAKE_UP,
175 OID_PNP_ADD_WAKE_UP_PATTERN,
176 OID_PNP_REMOVE_WAKE_UP_PATTERN,
177#endif /* RNDIS_WAKEUP */
178#endif /* RNDIS_PM */
179};
180
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* NDIS Functions */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100183static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300184 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300186 int retval = -ENOTSUPP;
187 u32 length = 4; /* usually */
188 __le32 *outbuf;
189 int i, count;
190 rndis_query_cmplt_type *resp;
191 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700192 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700193 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300196 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700199
200 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700201 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700202 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700203 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700204 get_unaligned_le32(&buf[i]),
205 get_unaligned_le32(&buf[i + 4]),
206 get_unaligned_le32(&buf[i + 8]),
207 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700208 }
209 }
210
211 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300212 outbuf = (__le32 *)&resp[1];
213 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700214
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100215 net = params->dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700216 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 switch (OID) {
219
220 /* general oids (table 4-1) */
221
222 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000223 case RNDIS_OID_GEN_SUPPORTED_LIST:
224 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300225 length = sizeof(oid_supported_list);
226 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300228 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 retval = 0;
230 break;
David Brownell7e27f182006-06-13 09:54:40 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000233 case RNDIS_OID_GEN_HARDWARE_STATUS:
234 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700235 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700237 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * reddite ergo quae sunt Caesaris Caesari
239 * et quae sunt Dei Deo!
240 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300241 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 retval = 0;
243 break;
David Brownell7e27f182006-06-13 09:54:40 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000246 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
247 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100248 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 retval = 0;
250 break;
David Brownell7e27f182006-06-13 09:54:40 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000253 case RNDIS_OID_GEN_MEDIA_IN_USE:
254 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* one medium, one transport... (maybe you do it better) */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100256 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 retval = 0;
258 break;
David Brownell7e27f182006-06-13 09:54:40 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000261 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
262 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100263 if (params->dev) {
264 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 break;
David Brownell7e27f182006-06-13 09:54:40 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000270 case RNDIS_OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700271 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000272 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100273 if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300274 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 else
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100276 *outbuf = cpu_to_le32(params->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 retval = 0;
278 break;
279
280 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000281 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
282 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100283 if (params->dev) {
284 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 retval = 0;
286 }
287 break;
David Brownell7e27f182006-06-13 09:54:40 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000290 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
291 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100292 if (params->dev) {
293 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 retval = 0;
295 }
296 break;
David Brownell7e27f182006-06-13 09:54:40 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000299 case RNDIS_OID_GEN_VENDOR_ID:
300 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100301 *outbuf = cpu_to_le32(params->vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 retval = 0;
303 break;
David Brownell7e27f182006-06-13 09:54:40 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000306 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
307 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100308 if (params->vendorDescr) {
309 length = strlen(params->vendorDescr);
310 memcpy(outbuf, params->vendorDescr, length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400311 } else {
312 outbuf[0] = 0;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 retval = 0;
315 break;
316
Linus Walleij8cdddc32012-05-11 22:16:08 +0000317 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
318 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700320 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 retval = 0;
322 break;
323
324 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000325 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
326 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100327 *outbuf = cpu_to_le32(*params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 retval = 0;
329 break;
330
331 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000332 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
333 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800334 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 retval = 0;
336 break;
337
338 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000339 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700340 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000341 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100342 *outbuf = cpu_to_le32(params->media_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 retval = 0;
344 break;
345
Linus Walleij8cdddc32012-05-11 22:16:08 +0000346 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
347 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300348 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 retval = 0;
350 break;
351
352 /* The RNDIS specification is incomplete/wrong. Some versions
353 * of MS-Windows expect OIDs that aren't specified there. Other
354 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
355 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000356 case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
357 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800358 *outbuf = cpu_to_le32(
Linus Walleije20289e2012-05-11 22:17:19 +0000359 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
360 | RNDIS_MAC_OPTION_FULL_DUPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 retval = 0;
362 break;
363
364 /* statistics OIDs (table 4-2) */
365
366 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000367 case RNDIS_OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700368 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000369 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700370 if (stats) {
371 *outbuf = cpu_to_le32(stats->tx_packets
372 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 break;
376
377 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000378 case RNDIS_OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700379 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000380 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700381 if (stats) {
382 *outbuf = cpu_to_le32(stats->rx_packets
383 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 break;
David Brownell7e27f182006-06-13 09:54:40 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000389 case RNDIS_OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700390 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000391 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700392 if (stats) {
393 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 break;
David Brownell7e27f182006-06-13 09:54:40 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000399 case RNDIS_OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700400 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000401 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700402 if (stats) {
403 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 break;
David Brownell7e27f182006-06-13 09:54:40 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000409 case RNDIS_OID_GEN_RCV_NO_BUFFER:
410 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700411 if (stats) {
412 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 break;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* ieee802.3 OIDs (table 4-3) */
418
419 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000420 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000421 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100422 if (params->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 length = ETH_ALEN;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100424 memcpy(outbuf, params->host_mac, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 break;
David Brownell7e27f182006-06-13 09:54:40 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000430 case RNDIS_OID_802_3_CURRENT_ADDRESS:
431 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100432 if (params->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 length = ETH_ALEN;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100434 memcpy(outbuf, params->host_mac, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 retval = 0;
436 }
437 break;
David Brownell7e27f182006-06-13 09:54:40 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000440 case RNDIS_OID_802_3_MULTICAST_LIST:
441 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300443 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 retval = 0;
445 break;
David Brownell7e27f182006-06-13 09:54:40 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000448 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
449 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300451 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 retval = 0;
453 break;
David Brownell7e27f182006-06-13 09:54:40 -0700454
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000455 case RNDIS_OID_802_3_MAC_OPTIONS:
456 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700457 *outbuf = cpu_to_le32(0);
458 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460
461 /* ieee802.3 statistics OIDs (table 4-4) */
462
463 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000464 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
465 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700466 if (stats) {
467 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 retval = 0;
469 }
470 break;
David Brownell7e27f182006-06-13 09:54:40 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000473 case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
474 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300475 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 retval = 0;
477 break;
David Brownell7e27f182006-06-13 09:54:40 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000480 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
481 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300482 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 retval = 0;
484 break;
David Brownell7e27f182006-06-13 09:54:40 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 default:
David Brownell00274922007-11-19 12:58:36 -0800487 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800488 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
David Brownell340600a2005-04-28 13:45:25 -0700490 if (retval < 0)
491 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700492
Mihai Donțua1df4e42010-09-08 02:54:02 +0300493 resp->InformationBufferLength = cpu_to_le32(length);
494 r->length = length + sizeof(*resp);
495 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return retval;
497}
498
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100499static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
500 u8 *buf, u32 buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300502 rndis_set_cmplt_type *resp;
503 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (!r)
506 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300507 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!resp)
509 return -ENOMEM;
510
David Brownell340600a2005-04-28 13:45:25 -0700511 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700512 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700513 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700514 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700515 get_unaligned_le32(&buf[i]),
516 get_unaligned_le32(&buf[i + 4]),
517 get_unaligned_le32(&buf[i + 8]),
518 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 switch (OID) {
Linus Walleij8cdddc32012-05-11 22:16:08 +0000523 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David Brownell340600a2005-04-28 13:45:25 -0700525 /* these NDIS_PACKET_TYPE_* bitflags are shared with
526 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
528 * PROMISCUOUS, DIRECTED,
529 * MULTICAST, ALL_MULTICAST, BROADCAST
530 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700531 *params->filter = (u16)get_unaligned_le32(buf);
Linus Walleij8cdddc32012-05-11 22:16:08 +0000532 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800533 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* this call has a significant side effect: it's
536 * what makes the packet flow start and stop, like
537 * activating the CDC Ethernet altsetting.
538 */
David Brownell340600a2005-04-28 13:45:25 -0700539 retval = 0;
540 if (*params->filter) {
Mayank Rana02ecd422016-05-10 14:11:27 -0700541 pr_debug("%s(): disable flow control\n", __func__);
542 rndis_flow_control(params, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else {
Mayank Rana02ecd422016-05-10 14:11:27 -0700544 pr_err("%s(): enable flow control\n", __func__);
545 rndis_flow_control(params, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 break;
David Brownell7e27f182006-06-13 09:54:40 -0700548
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000549 case RNDIS_OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700550 /* I think we can ignore this */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000551 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 retval = 0;
553 break;
David Brownell340600a2005-04-28 13:45:25 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 default:
David Brownell00274922007-11-19 12:58:36 -0800556 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800557 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
David Brownell7e27f182006-06-13 09:54:40 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return retval;
561}
562
David Brownell7e27f182006-06-13 09:54:40 -0700563/*
564 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100567static int rndis_init_response(struct rndis_params *params,
568 rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300570 rndis_init_cmplt_type *resp;
571 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700572
David Brownell15b2d2b2008-06-19 18:19:16 -0700573 if (!params->dev)
574 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700575
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100576 r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700577 if (!r)
578 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300579 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700580
Linus Walleij51491162012-05-11 22:17:07 +0000581 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300582 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300584 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
585 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
586 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
587 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
588 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
xerox_lin72ffa182014-08-14 14:48:44 +0800589 resp->MaxPacketsPerTransfer = cpu_to_le32(params->max_pkt_per_xfer);
590 resp->MaxTransferSize = cpu_to_le32(params->max_pkt_per_xfer *
591 (params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300592 + sizeof(struct ethhdr)
593 + sizeof(struct rndis_packet_msg_type)
xerox_lin72ffa182014-08-14 14:48:44 +0800594 + 22));
Hemant Kumar6ccd55c2016-02-01 18:54:16 -0800595 resp->PacketAlignmentFactor = cpu_to_le32(params->pkt_alignment_factor);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300596 resp->AFListOffset = cpu_to_le32(0);
597 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700598
Chandana Kishori Chiluveru50a21842017-11-06 14:54:24 +0530599 params->ul_max_xfer_size = le32_to_cpu(resp->MaxTransferSize);
David Brownell15b2d2b2008-06-19 18:19:16 -0700600 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100604static int rndis_query_response(struct rndis_params *params,
605 rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300608 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700609
David Brownell33376c12008-08-18 17:45:07 -0700610 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700611 if (!params->dev)
612 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700613
Shaun Tancheff87637162006-02-22 19:47:19 -0800614 /*
615 * we need more memory:
616 * gen_ndis_query_resp expects enough space for
617 * rndis_query_cmplt_type followed by data.
618 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100620 r = rndis_add_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300621 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700622 if (!r)
623 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300624 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700625
Linus Walleij51491162012-05-11 22:17:07 +0000626 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700628
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100629 if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700630 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300631 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700632 le32_to_cpu(buf->InformationBufferLength),
633 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300635 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
636 resp->MessageLength = cpu_to_le32(sizeof *resp);
637 resp->InformationBufferLength = cpu_to_le32(0);
638 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300640 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700641
David Brownell15b2d2b2008-06-19 18:19:16 -0700642 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100646static int rndis_set_response(struct rndis_params *params,
647 rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300649 u32 BufLength, BufOffset;
650 rndis_set_cmplt_type *resp;
651 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700652
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100653 r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700654 if (!r)
655 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300656 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Mihai Donțua1df4e42010-09-08 02:54:02 +0300658 BufLength = le32_to_cpu(buf->InformationBufferLength);
659 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David Brownell15b2d2b2008-06-19 18:19:16 -0700661#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700662 pr_debug("%s: Length: %d\n", __func__, BufLength);
663 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
664 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700667 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
David Brownell7e27f182006-06-13 09:54:40 -0700669
David Brownell33376c12008-08-18 17:45:07 -0700670 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#endif
David Brownell7e27f182006-06-13 09:54:40 -0700672
Linus Walleij51491162012-05-11 22:17:07 +0000673 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300674 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100676 if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
Mihai Donțua1df4e42010-09-08 02:54:02 +0300677 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
678 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700679 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300680 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700681
David Brownell15b2d2b2008-06-19 18:19:16 -0700682 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100686static int rndis_reset_response(struct rndis_params *params,
687 rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300689 rndis_reset_cmplt_type *resp;
690 rndis_resp_t *r;
xerox_lin90de7402014-08-18 21:54:23 +0800691
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100692 r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700693 if (!r)
694 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300695 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700696
Linus Walleij51491162012-05-11 22:17:07 +0000697 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300698 resp->MessageLength = cpu_to_le32(16);
699 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300701 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700702
David Brownell15b2d2b2008-06-19 18:19:16 -0700703 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705}
706
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100707static int rndis_keepalive_response(struct rndis_params *params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300708 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300710 rndis_keepalive_cmplt_type *resp;
711 rndis_resp_t *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
714
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100715 r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700716 if (!r)
717 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300718 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700719
Linus Walleij51491162012-05-11 22:17:07 +0000720 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300721 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300723 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700724
David Brownell15b2d2b2008-06-19 18:19:16 -0700725 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727}
728
729
David Brownell7e27f182006-06-13 09:54:40 -0700730/*
731 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100733static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300735 rndis_indicate_status_msg_type *resp;
736 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700737
David Brownell15b2d2b2008-06-19 18:19:16 -0700738 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700739 return -ENOTSUPP;
740
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100741 r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700742 if (!r)
743 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300744 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700745
Linus Walleij51491162012-05-11 22:17:07 +0000746 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300747 resp->MessageLength = cpu_to_le32(20);
748 resp->Status = cpu_to_le32(status);
749 resp->StatusBufferLength = cpu_to_le32(0);
750 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700751
David Brownell15b2d2b2008-06-19 18:19:16 -0700752 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754}
755
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100756int rndis_signal_connect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100758 params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
759 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500761EXPORT_SYMBOL_GPL(rndis_signal_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100763int rndis_signal_disconnect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100765 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
766 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500768EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100770void rndis_uninit(struct rndis_params *params)
David Brownell340600a2005-04-28 13:45:25 -0700771{
David Brownell486e2df2005-05-24 17:51:52 -0700772 u8 *buf;
773 u32 length;
774
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100775 if (!params)
David Brownell340600a2005-04-28 13:45:25 -0700776 return;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100777 params->state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700778
779 /* drain the response queue */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100780 while ((buf = rndis_get_next_response(params, &length)))
781 rndis_free_response(params, buf);
David Brownell340600a2005-04-28 13:45:25 -0700782}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500783EXPORT_SYMBOL_GPL(rndis_uninit);
David Brownell340600a2005-04-28 13:45:25 -0700784
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100785void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100787 params->host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500789EXPORT_SYMBOL_GPL(rndis_set_host_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
David Brownell7e27f182006-06-13 09:54:40 -0700791/*
792 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100794int rndis_msg_parser(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 u32 MsgType, MsgLength;
797 __le32 *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!buf)
800 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700801
Mihai Donțua1df4e42010-09-08 02:54:02 +0300802 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700803 MsgType = get_unaligned_le32(tmp++);
804 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700805
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100806 if (!params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700808
David Brownell340600a2005-04-28 13:45:25 -0700809 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
810 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
811 * and normal HC level polling to see if there's any IN traffic.
812 */
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700815 switch (MsgType) {
Linus Walleij51491162012-05-11 22:17:07 +0000816 case RNDIS_MSG_INIT:
Mayank Rana057e4752017-09-20 13:55:11 -0700817 pr_debug("%s: RNDIS_MSG_INIT\n", __func__);
818 tmp++; /* to get RequestID */
819 params->host_rndis_major_ver = get_unaligned_le32(tmp++);
820 params->host_rndis_minor_ver = get_unaligned_le32(tmp++);
821 params->dl_max_xfer_size = get_unaligned_le32(tmp++);
822
823 pr_debug("%s(): RNDIS Host Major:%d Minor:%d version\n",
824 __func__, params->host_rndis_major_ver,
825 params->host_rndis_minor_ver);
826 pr_debug("%s(): DL Max Transfer size:%x\n",
827 __func__, params->dl_max_xfer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 params->state = RNDIS_INITIALIZED;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100829 return rndis_init_response(params, (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700830
Linus Walleij51491162012-05-11 22:17:07 +0000831 case RNDIS_MSG_HALT:
832 pr_debug("%s: RNDIS_MSG_HALT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300833 __func__);
Hemant Kumar8b060292016-12-06 15:58:20 -0800834 if (params->state == RNDIS_DATA_INITIALIZED) {
835 if (params->flow_ctrl_enable) {
836 params->flow_ctrl_enable(true, params);
837 } else {
838 if (params->dev) {
839 netif_carrier_off(params->dev);
840 netif_stop_queue(params->dev);
841 }
842 }
843 params->state = RNDIS_UNINITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700846
Linus Walleij51491162012-05-11 22:17:07 +0000847 case RNDIS_MSG_QUERY:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100848 return rndis_query_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300849 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700850
Linus Walleij51491162012-05-11 22:17:07 +0000851 case RNDIS_MSG_SET:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100852 return rndis_set_response(params, (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700853
Linus Walleij51491162012-05-11 22:17:07 +0000854 case RNDIS_MSG_RESET:
855 pr_debug("%s: RNDIS_MSG_RESET\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300856 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100857 return rndis_reset_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300858 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Walleij51491162012-05-11 22:17:07 +0000860 case RNDIS_MSG_KEEPALIVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700862 if (rndis_debug > 1)
Linus Walleij51491162012-05-11 22:17:07 +0000863 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300864 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100865 return rndis_keepalive_response(params,
David Brownell7e27f182006-06-13 09:54:40 -0700866 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700868
869 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* At least Windows XP emits some undefined RNDIS messages.
871 * In one case those messages seemed to relate to the host
872 * suspending itself.
873 */
David Brownell00274922007-11-19 12:58:36 -0800874 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300875 __func__, MsgType, MsgLength);
Andy Shevchenkod3091cf2012-08-07 19:07:58 +0300876 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
877 buf, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 break;
879 }
David Brownell7e27f182006-06-13 09:54:40 -0700880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -ENOTSUPP;
882}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500883EXPORT_SYMBOL_GPL(rndis_msg_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100885static inline int rndis_get_nr(void)
886{
887 return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL);
888}
889
890static inline void rndis_put_nr(int nr)
891{
892 ida_simple_remove(&rndis_ida, nr);
893}
894
Hemant Kumar8b060292016-12-06 15:58:20 -0800895struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v,
896 void (*flow_ctrl_enable)(bool enable, struct rndis_params *params))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100898 struct rndis_params *params;
Andrzej Pietrasiewicz81dff862015-05-18 17:40:04 +0200899 int i;
David Brownell7e27f182006-06-13 09:54:40 -0700900
David Brownell15b2d2b2008-06-19 18:19:16 -0700901 if (!resp_avail)
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100902 return ERR_PTR(-EINVAL);
David Brownell15b2d2b2008-06-19 18:19:16 -0700903
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100904 i = rndis_get_nr();
905 if (i < 0) {
906 pr_debug("failed\n");
907
908 return ERR_PTR(-ENODEV);
909 }
910
911 params = kzalloc(sizeof(*params), GFP_KERNEL);
912 if (!params) {
913 rndis_put_nr(i);
914
915 return ERR_PTR(-ENOMEM);
916 }
917
918#ifdef CONFIG_USB_GADGET_DEBUG_FILES
919 {
920 struct proc_dir_entry *proc_entry;
921 char name[20];
922
923 sprintf(name, NAME_TEMPLATE, i);
924 proc_entry = proc_create_data(name, 0660, NULL,
925 &rndis_proc_fops, params);
926 if (!proc_entry) {
927 kfree(params);
928 rndis_put_nr(i);
929
930 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 }
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100933#endif
David Brownell7e27f182006-06-13 09:54:40 -0700934
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100935 params->confignr = i;
936 params->used = 1;
937 params->state = RNDIS_UNINITIALIZED;
938 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
939 params->resp_avail = resp_avail;
Hemant Kumar8b060292016-12-06 15:58:20 -0800940 params->flow_ctrl_enable = flow_ctrl_enable;
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100941 params->v = v;
Geliang Tangf6281af2015-12-19 00:34:33 +0800942 INIT_LIST_HEAD(&params->resp_queue);
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100943 pr_debug("%s: configNr = %d\n", __func__, i);
944
945 return params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500947EXPORT_SYMBOL_GPL(rndis_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100949void rndis_deregister(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Andrzej Pietrasiewicz81dff862015-05-18 17:40:04 +0200951 int i;
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100952
Mihai Donțua1df4e42010-09-08 02:54:02 +0300953 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700954
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100955 if (!params)
956 return;
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100957
958 i = params->confignr;
959
960#ifdef CONFIG_USB_GADGET_DEBUG_FILES
961 {
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100962 char name[20];
963
964 sprintf(name, NAME_TEMPLATE, i);
965 remove_proc_entry(name, NULL);
966 }
967#endif
968
969 kfree(params);
970 rndis_put_nr(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500972EXPORT_SYMBOL_GPL(rndis_deregister);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100973int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
974 u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
David Brownell33376c12008-08-18 17:45:07 -0700976 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700977 if (!dev)
978 return -EINVAL;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100979 if (!params)
980 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700981
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100982 params->dev = dev;
983 params->filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700984
xerox_lin72ffa182014-08-14 14:48:44 +0800985 rndis_ul_max_xfer_size_rcvd = 0;
986 rndis_ul_max_pkt_per_xfer_rcvd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return 0;
988}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500989EXPORT_SYMBOL_GPL(rndis_set_param_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100991int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
992 const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
David Brownell33376c12008-08-18 17:45:07 -0700994 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!vendorDescr) return -1;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100996 if (!params)
997 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700998
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100999 params->vendorID = vendorID;
1000 params->vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -07001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001004EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001006int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
David Brownell33376c12008-08-18 17:45:07 -07001008 pr_debug("%s: %u %u\n", __func__, medium, speed);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001009 if (!params)
1010 return -1;
David Brownell7e27f182006-06-13 09:54:40 -07001011
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001012 params->medium = medium;
1013 params->speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001017EXPORT_SYMBOL_GPL(rndis_set_param_medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Chandana Kishori Chiluveru50a21842017-11-06 14:54:24 +05301019u32 rndis_get_dl_max_xfer_size(struct rndis_params *params)
1020{
1021 pr_debug("%s:\n", __func__);
1022 return params->dl_max_xfer_size;
1023}
1024
1025u32 rndis_get_ul_max_xfer_size(struct rndis_params *params)
1026{
1027 pr_debug("%s:\n", __func__);
1028 return params->ul_max_xfer_size;
1029}
1030
Amit Pundird29237a2015-12-21 14:03:15 +05301031void rndis_set_max_pkt_xfer(struct rndis_params *params, u8 max_pkt_per_xfer)
xerox_lin72ffa182014-08-14 14:48:44 +08001032{
1033 pr_debug("%s:\n", __func__);
1034
Amit Pundird29237a2015-12-21 14:03:15 +05301035 params->max_pkt_per_xfer = max_pkt_per_xfer;
xerox_lin72ffa182014-08-14 14:48:44 +08001036}
1037
Hemant Kumar8b060292016-12-06 15:58:20 -08001038/**
1039 * rndis_flow_control: enable/disable flow control with USB RNDIS interface
1040 * params - RNDIS network parameter
1041 * enable_flow_control - true: perform flow control, false: disable flow control
1042 *
1043 * In hw accelerated mode, this function triggers functionality to start/stop
1044 * endless transfers, otherwise it enables/disables RNDIS network interface.
1045 */
1046void rndis_flow_control(struct rndis_params *params, bool enable_flow_control)
1047{
1048 if (!params) {
1049 pr_err("%s: failed, params NULL\n", __func__);
1050 return;
1051 }
1052
1053 pr_debug("%s(): params->state:%x\n", __func__, params->state);
1054
1055 if (enable_flow_control) {
1056 if (params->state == RNDIS_DATA_INITIALIZED) {
1057 if (params->flow_ctrl_enable) {
1058 params->flow_ctrl_enable(enable_flow_control,
1059 params);
1060 } else {
1061 netif_carrier_off(params->dev);
1062 netif_stop_queue(params->dev);
1063 }
1064 }
1065 params->state = RNDIS_INITIALIZED;
1066 } else {
1067 if (params->state != RNDIS_DATA_INITIALIZED) {
1068 if (params->flow_ctrl_enable) {
1069 params->flow_ctrl_enable(enable_flow_control,
1070 params);
1071 } else {
1072 netif_carrier_on(params->dev);
1073 if (netif_running(params->dev))
1074 netif_wake_queue(params->dev);
1075 }
1076 }
1077 params->state = RNDIS_DATA_INITIALIZED;
1078 }
1079}
1080
Mihai Donțua1df4e42010-09-08 02:54:02 +03001081void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001083 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (!skb)
1086 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001087 header = (void *)skb_push(skb, sizeof(*header));
1088 memset(header, 0, sizeof *header);
Linus Walleij51491162012-05-11 22:17:07 +00001089 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +03001091 header->DataOffset = cpu_to_le32(36);
1092 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001094EXPORT_SYMBOL_GPL(rndis_add_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001096void rndis_free_response(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Geliang Tangf6281af2015-12-19 00:34:33 +08001098 rndis_resp_t *r, *n;
David Brownell7e27f182006-06-13 09:54:40 -07001099
Geliang Tangf6281af2015-12-19 00:34:33 +08001100 list_for_each_entry_safe(r, n, &params->resp_queue, list) {
Julia Lawall1c17a352016-01-25 16:21:54 +01001101 if (r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001102 list_del(&r->list);
1103 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 }
1106}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001107EXPORT_SYMBOL_GPL(rndis_free_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001109u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Geliang Tangf6281af2015-12-19 00:34:33 +08001111 rndis_resp_t *r, *n;
David Brownell7e27f182006-06-13 09:54:40 -07001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001114
Geliang Tangf6281af2015-12-19 00:34:33 +08001115 list_for_each_entry_safe(r, n, &params->resp_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!r->send) {
1117 r->send = 1;
1118 *length = r->length;
1119 return r->buf;
1120 }
1121 }
David Brownell7e27f182006-06-13 09:54:40 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return NULL;
1124}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001125EXPORT_SYMBOL_GPL(rndis_get_next_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001127static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001129 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001130
Mihai Donțua1df4e42010-09-08 02:54:02 +03001131 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1132 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001134
Mihai Donțua1df4e42010-09-08 02:54:02 +03001135 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 r->length = length;
1137 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001138
Geliang Tangf6281af2015-12-19 00:34:33 +08001139 list_add_tail(&r->list, &params->resp_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return r;
1141}
1142
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001143int rndis_rm_hdr(struct gether *port,
1144 struct sk_buff *skb,
1145 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
xerox_lin72ffa182014-08-14 14:48:44 +08001147 int num_pkts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
xerox_lin72ffa182014-08-14 14:48:44 +08001149 if (skb->len > rndis_ul_max_xfer_size_rcvd)
1150 rndis_ul_max_xfer_size_rcvd = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
xerox_lin72ffa182014-08-14 14:48:44 +08001152 while (skb->len) {
1153 struct rndis_packet_msg_type *hdr;
1154 struct sk_buff *skb2;
1155 u32 msg_len, data_offset, data_len;
1156
1157 /* some rndis hosts send extra byte to avoid zlp, ignore it */
1158 if (skb->len == 1) {
1159 dev_kfree_skb_any(skb);
1160 return 0;
1161 }
1162
1163 if (skb->len < sizeof *hdr) {
Amit Pundir928b59a2015-12-30 03:05:44 +05301164 pr_err("invalid rndis pkt: skblen:%u hdr_len:%zu",
xerox_lin72ffa182014-08-14 14:48:44 +08001165 skb->len, sizeof *hdr);
1166 dev_kfree_skb_any(skb);
1167 return -EINVAL;
1168 }
1169
1170 hdr = (void *)skb->data;
1171 msg_len = le32_to_cpu(hdr->MessageLength);
1172 data_offset = le32_to_cpu(hdr->DataOffset);
1173 data_len = le32_to_cpu(hdr->DataLength);
1174
1175 if (skb->len < msg_len ||
1176 ((data_offset + data_len + 8) > msg_len)) {
1177 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1178 le32_to_cpu(hdr->MessageType),
1179 msg_len, data_offset, data_len, skb->len);
1180 dev_kfree_skb_any(skb);
1181 return -EOVERFLOW;
1182 }
1183 if (le32_to_cpu(hdr->MessageType) != RNDIS_MSG_PACKET) {
1184 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1185 le32_to_cpu(hdr->MessageType),
1186 msg_len, data_offset, data_len, skb->len);
1187 dev_kfree_skb_any(skb);
1188 return -EINVAL;
1189 }
1190
1191 skb_pull(skb, data_offset + 8);
1192
1193 if (msg_len == skb->len) {
1194 skb_trim(skb, data_len);
1195 break;
1196 }
1197
1198 skb2 = skb_clone(skb, GFP_ATOMIC);
1199 if (!skb2) {
1200 pr_err("%s:skb clone failed\n", __func__);
1201 dev_kfree_skb_any(skb);
1202 return -ENOMEM;
1203 }
1204
1205 skb_pull(skb, msg_len - sizeof *hdr);
1206 skb_trim(skb2, data_len);
1207 skb_queue_tail(list, skb2);
1208
1209 num_pkts++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001210 }
xerox_lin72ffa182014-08-14 14:48:44 +08001211
1212 if (num_pkts > rndis_ul_max_pkt_per_xfer_rcvd)
1213 rndis_ul_max_pkt_per_xfer_rcvd = num_pkts;
David Brownell6cdee102005-04-18 17:39:34 -07001214
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001215 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return 0;
1217}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001218EXPORT_SYMBOL_GPL(rndis_rm_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Hemant Kumar6ccd55c2016-02-01 18:54:16 -08001220void rndis_set_pkt_alignment_factor(struct rndis_params *params,
1221 u8 pkt_alignment_factor)
1222{
1223 pr_debug("%s:\n", __func__);
1224
1225 if (!params) {
1226 pr_err("%s: failed, params NULL\n", __func__);
1227 return;
1228 }
1229
1230 params->pkt_alignment_factor = pkt_alignment_factor;
1231}
1232
Mihai Donțua1df4e42010-09-08 02:54:02 +03001233#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001235static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001237 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001238
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001239 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 "Config Nr. %d\n"
1241 "used : %s\n"
1242 "state : %s\n"
1243 "medium : 0x%08X\n"
1244 "speed : %d\n"
1245 "cable : %s\n"
1246 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001247 "vendor : %s\n",
1248 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 ({ char *s = "?";
1250 switch (param->state) {
1251 case RNDIS_UNINITIALIZED:
1252 s = "RNDIS_UNINITIALIZED"; break;
1253 case RNDIS_INITIALIZED:
1254 s = "RNDIS_INITIALIZED"; break;
1255 case RNDIS_DATA_INITIALIZED:
1256 s = "RNDIS_DATA_INITIALIZED"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001257 } s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001258 param->medium,
1259 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001261 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001265static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001266 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Al Virod9dda782013-03-31 18:16:14 -04001268 rndis_params *p = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 u32 speed = 0;
1270 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 for (i = 0; i < count; i++) {
1273 char c;
1274 if (get_user(c, buffer))
1275 return -EFAULT;
1276 switch (c) {
1277 case '0':
1278 case '1':
1279 case '2':
1280 case '3':
1281 case '4':
1282 case '5':
1283 case '6':
1284 case '7':
1285 case '8':
1286 case '9':
1287 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001288 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
1290 case 'C':
1291 case 'c':
Andrzej Pietrasiewicz868055f2015-05-18 17:40:02 +02001292 rndis_signal_connect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 case 'D':
1295 case 'd':
Andrzej Pietrasiewicz868055f2015-05-18 17:40:02 +02001296 rndis_signal_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
David Brownell7e27f182006-06-13 09:54:40 -07001298 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001300 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 break;
1302 }
David Brownell7e27f182006-06-13 09:54:40 -07001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 buffer++;
1305 }
David Brownell7e27f182006-06-13 09:54:40 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return count;
1308}
1309
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001310static int rndis_proc_open(struct inode *inode, struct file *file)
1311{
Al Virod9dda782013-03-31 18:16:14 -04001312 return single_open(file, rndis_proc_show, PDE_DATA(inode));
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001313}
1314
1315static const struct file_operations rndis_proc_fops = {
1316 .owner = THIS_MODULE,
1317 .open = rndis_proc_open,
1318 .read = seq_read,
1319 .llseek = seq_lseek,
1320 .release = single_release,
1321 .write = rndis_proc_write,
1322};
1323
Mihai Donțua1df4e42010-09-08 02:54:02 +03001324#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Mihai Donțua1df4e42010-09-08 02:54:02 +03001326#endif /* CONFIG_USB_GADGET_DEBUG_FILES */