blob: 01a3b589165607c5ed8ace14a60adac91d65a095 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/list.h>
29#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Alexey Dobriyane184d5f2008-05-14 16:25:13 -070031#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/netdevice.h>
33
34#include <asm/io.h>
35#include <asm/byteorder.h>
David Brownell6cdee102005-04-18 17:39:34 -070036#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Fabio Estevamae8dd0c2014-04-04 22:27:59 -030038#include "u_rndis.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
David Brownell15b2d2b2008-06-19 18:19:16 -070040#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "rndis.h"
43
44
45/* The driver for your USB chip needs to support ep0 OUT to work with
46 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
47 *
48 * Windows hosts need an INF file like Documentation/usb/linux.inf
49 * and will be happier if you provide the host_addr module parameter.
50 */
51
52#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070054module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
60#define RNDIS_MAX_CONFIGS 1
61
62
Mihai Donțua1df4e42010-09-08 02:54:02 +030063static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030066static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Function Prototypes */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +010069static rndis_resp_t *rndis_add_response(struct rndis_params *params,
70 u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72
David Brownell340600a2005-04-28 13:45:25 -070073/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030074static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070075{
76 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000077 RNDIS_OID_GEN_SUPPORTED_LIST,
78 RNDIS_OID_GEN_HARDWARE_STATUS,
79 RNDIS_OID_GEN_MEDIA_SUPPORTED,
80 RNDIS_OID_GEN_MEDIA_IN_USE,
81 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
82 RNDIS_OID_GEN_LINK_SPEED,
83 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
84 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
85 RNDIS_OID_GEN_VENDOR_ID,
86 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
87 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
88 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
89 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
90 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
91 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070092
David Brownell340600a2005-04-28 13:45:25 -070093 /* the statistical stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000094 RNDIS_OID_GEN_XMIT_OK,
95 RNDIS_OID_GEN_RCV_OK,
96 RNDIS_OID_GEN_XMIT_ERROR,
97 RNDIS_OID_GEN_RCV_ERROR,
98 RNDIS_OID_GEN_RCV_NO_BUFFER,
David Brownell340600a2005-04-28 13:45:25 -070099#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij8cdddc32012-05-11 22:16:08 +0000100 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
101 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
102 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
103 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
104 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
105 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
106 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
107 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
108 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
109 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
110 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
111 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
112 RNDIS_OID_GEN_RCV_CRC_ERROR,
113 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
David Brownell340600a2005-04-28 13:45:25 -0700114#endif /* RNDIS_OPTIONAL_STATS */
115
David Brownell7e27f182006-06-13 09:54:40 -0700116 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700117 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000118 RNDIS_OID_802_3_PERMANENT_ADDRESS,
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000119 RNDIS_OID_802_3_CURRENT_ADDRESS,
120 RNDIS_OID_802_3_MULTICAST_LIST,
121 RNDIS_OID_802_3_MAC_OPTIONS,
122 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700123
David Brownell340600a2005-04-28 13:45:25 -0700124 /* the statistical stuff */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000125 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
126 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
127 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700128#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000129 RNDIS_OID_802_3_XMIT_DEFERRED,
130 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
131 RNDIS_OID_802_3_RCV_OVERRUN,
132 RNDIS_OID_802_3_XMIT_UNDERRUN,
133 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
134 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
135 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700136#endif /* RNDIS_OPTIONAL_STATS */
137
138#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700139 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
140 * don't say what they mean ... and the NDIS specs are often
141 * confusing and/or ambiguous in this context. (That is, more
142 * so than their specs for the other OIDs.)
143 *
144 * FIXME someone who knows what these should do, please
145 * implement them!
146 */
David Brownell340600a2005-04-28 13:45:25 -0700147
148 /* power management */
149 OID_PNP_CAPABILITIES,
150 OID_PNP_QUERY_POWER,
151 OID_PNP_SET_POWER,
152
153#ifdef RNDIS_WAKEUP
154 /* wake up host */
155 OID_PNP_ENABLE_WAKE_UP,
156 OID_PNP_ADD_WAKE_UP_PATTERN,
157 OID_PNP_REMOVE_WAKE_UP_PATTERN,
158#endif /* RNDIS_WAKEUP */
159#endif /* RNDIS_PM */
160};
161
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* NDIS Functions */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100164static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300165 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300167 int retval = -ENOTSUPP;
168 u32 length = 4; /* usually */
169 __le32 *outbuf;
170 int i, count;
171 rndis_query_cmplt_type *resp;
172 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700173 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700174 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300177 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700180
181 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700182 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700183 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700184 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700185 get_unaligned_le32(&buf[i]),
186 get_unaligned_le32(&buf[i + 4]),
187 get_unaligned_le32(&buf[i + 8]),
188 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700189 }
190 }
191
192 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300193 outbuf = (__le32 *)&resp[1];
194 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700195
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100196 net = params->dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700197 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 switch (OID) {
200
201 /* general oids (table 4-1) */
202
203 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000204 case RNDIS_OID_GEN_SUPPORTED_LIST:
205 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300206 length = sizeof(oid_supported_list);
207 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300209 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 retval = 0;
211 break;
David Brownell7e27f182006-06-13 09:54:40 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000214 case RNDIS_OID_GEN_HARDWARE_STATUS:
215 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700216 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700218 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * reddite ergo quae sunt Caesaris Caesari
220 * et quae sunt Dei Deo!
221 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300222 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 retval = 0;
224 break;
David Brownell7e27f182006-06-13 09:54:40 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000227 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
228 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100229 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 retval = 0;
231 break;
David Brownell7e27f182006-06-13 09:54:40 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000234 case RNDIS_OID_GEN_MEDIA_IN_USE:
235 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* one medium, one transport... (maybe you do it better) */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100237 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 retval = 0;
239 break;
David Brownell7e27f182006-06-13 09:54:40 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000242 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
243 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100244 if (params->dev) {
245 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 break;
David Brownell7e27f182006-06-13 09:54:40 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000251 case RNDIS_OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700252 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000253 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100254 if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300255 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 else
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100257 *outbuf = cpu_to_le32(params->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 retval = 0;
259 break;
260
261 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000262 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
263 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100264 if (params->dev) {
265 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 retval = 0;
267 }
268 break;
David Brownell7e27f182006-06-13 09:54:40 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000271 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
272 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100273 if (params->dev) {
274 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 retval = 0;
276 }
277 break;
David Brownell7e27f182006-06-13 09:54:40 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000280 case RNDIS_OID_GEN_VENDOR_ID:
281 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100282 *outbuf = cpu_to_le32(params->vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 retval = 0;
284 break;
David Brownell7e27f182006-06-13 09:54:40 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000287 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
288 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100289 if (params->vendorDescr) {
290 length = strlen(params->vendorDescr);
291 memcpy(outbuf, params->vendorDescr, length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400292 } else {
293 outbuf[0] = 0;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 retval = 0;
296 break;
297
Linus Walleij8cdddc32012-05-11 22:16:08 +0000298 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
299 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700301 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 retval = 0;
303 break;
304
305 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000306 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
307 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100308 *outbuf = cpu_to_le32(*params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 retval = 0;
310 break;
311
312 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000313 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
314 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800315 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 retval = 0;
317 break;
318
319 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000320 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700321 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000322 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100323 *outbuf = cpu_to_le32(params->media_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 retval = 0;
325 break;
326
Linus Walleij8cdddc32012-05-11 22:16:08 +0000327 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
328 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300329 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 retval = 0;
331 break;
332
333 /* The RNDIS specification is incomplete/wrong. Some versions
334 * of MS-Windows expect OIDs that aren't specified there. Other
335 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
336 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000337 case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
338 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800339 *outbuf = cpu_to_le32(
Linus Walleije20289e2012-05-11 22:17:19 +0000340 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
341 | RNDIS_MAC_OPTION_FULL_DUPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 retval = 0;
343 break;
344
345 /* statistics OIDs (table 4-2) */
346
347 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000348 case RNDIS_OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700349 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000350 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700351 if (stats) {
352 *outbuf = cpu_to_le32(stats->tx_packets
353 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 break;
357
358 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000359 case RNDIS_OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700360 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000361 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700362 if (stats) {
363 *outbuf = cpu_to_le32(stats->rx_packets
364 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 break;
David Brownell7e27f182006-06-13 09:54:40 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000370 case RNDIS_OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700371 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000372 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700373 if (stats) {
374 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 break;
David Brownell7e27f182006-06-13 09:54:40 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000380 case RNDIS_OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700381 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000382 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700383 if (stats) {
384 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 break;
David Brownell7e27f182006-06-13 09:54:40 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000390 case RNDIS_OID_GEN_RCV_NO_BUFFER:
391 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700392 if (stats) {
393 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 break;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* ieee802.3 OIDs (table 4-3) */
399
400 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000401 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000402 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100403 if (params->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 length = ETH_ALEN;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100405 memcpy(outbuf, params->host_mac, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 break;
David Brownell7e27f182006-06-13 09:54:40 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000411 case RNDIS_OID_802_3_CURRENT_ADDRESS:
412 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100413 if (params->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 length = ETH_ALEN;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100415 memcpy(outbuf, params->host_mac, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 retval = 0;
417 }
418 break;
David Brownell7e27f182006-06-13 09:54:40 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000421 case RNDIS_OID_802_3_MULTICAST_LIST:
422 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300424 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 retval = 0;
426 break;
David Brownell7e27f182006-06-13 09:54:40 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000429 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
430 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300432 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 retval = 0;
434 break;
David Brownell7e27f182006-06-13 09:54:40 -0700435
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000436 case RNDIS_OID_802_3_MAC_OPTIONS:
437 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700438 *outbuf = cpu_to_le32(0);
439 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441
442 /* ieee802.3 statistics OIDs (table 4-4) */
443
444 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000445 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
446 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700447 if (stats) {
448 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 retval = 0;
450 }
451 break;
David Brownell7e27f182006-06-13 09:54:40 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000454 case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
455 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300456 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 retval = 0;
458 break;
David Brownell7e27f182006-06-13 09:54:40 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000461 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
462 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300463 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 retval = 0;
465 break;
David Brownell7e27f182006-06-13 09:54:40 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 default:
David Brownell00274922007-11-19 12:58:36 -0800468 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800469 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
David Brownell340600a2005-04-28 13:45:25 -0700471 if (retval < 0)
472 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700473
Mihai Donțua1df4e42010-09-08 02:54:02 +0300474 resp->InformationBufferLength = cpu_to_le32(length);
475 r->length = length + sizeof(*resp);
476 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return retval;
478}
479
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100480static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
481 u8 *buf, u32 buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300483 rndis_set_cmplt_type *resp;
484 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (!r)
487 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300488 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (!resp)
490 return -ENOMEM;
491
David Brownell340600a2005-04-28 13:45:25 -0700492 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700493 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700494 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700495 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700496 get_unaligned_le32(&buf[i]),
497 get_unaligned_le32(&buf[i + 4]),
498 get_unaligned_le32(&buf[i + 8]),
499 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 switch (OID) {
Linus Walleij8cdddc32012-05-11 22:16:08 +0000504 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David Brownell340600a2005-04-28 13:45:25 -0700506 /* these NDIS_PACKET_TYPE_* bitflags are shared with
507 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
509 * PROMISCUOUS, DIRECTED,
510 * MULTICAST, ALL_MULTICAST, BROADCAST
511 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700512 *params->filter = (u16)get_unaligned_le32(buf);
Linus Walleij8cdddc32012-05-11 22:16:08 +0000513 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800514 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* this call has a significant side effect: it's
517 * what makes the packet flow start and stop, like
518 * activating the CDC Ethernet altsetting.
519 */
David Brownell340600a2005-04-28 13:45:25 -0700520 retval = 0;
521 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 params->state = RNDIS_DATA_INITIALIZED;
523 netif_carrier_on(params->dev);
524 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300525 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else {
527 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300528 netif_carrier_off(params->dev);
529 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 break;
David Brownell7e27f182006-06-13 09:54:40 -0700532
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000533 case RNDIS_OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700534 /* I think we can ignore this */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000535 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 retval = 0;
537 break;
David Brownell340600a2005-04-28 13:45:25 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 default:
David Brownell00274922007-11-19 12:58:36 -0800540 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800541 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
David Brownell7e27f182006-06-13 09:54:40 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return retval;
545}
546
David Brownell7e27f182006-06-13 09:54:40 -0700547/*
548 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
550
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100551static int rndis_init_response(struct rndis_params *params,
552 rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300554 rndis_init_cmplt_type *resp;
555 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700556
David Brownell15b2d2b2008-06-19 18:19:16 -0700557 if (!params->dev)
558 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700559
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100560 r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700561 if (!r)
562 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300563 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700564
Linus Walleij51491162012-05-11 22:17:07 +0000565 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300566 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300568 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
569 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
570 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
571 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
572 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
573 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
574 resp->MaxTransferSize = cpu_to_le32(
David Brownell15b2d2b2008-06-19 18:19:16 -0700575 params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300576 + sizeof(struct ethhdr)
577 + sizeof(struct rndis_packet_msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 + 22);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300579 resp->PacketAlignmentFactor = cpu_to_le32(0);
580 resp->AFListOffset = cpu_to_le32(0);
581 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700582
David Brownell15b2d2b2008-06-19 18:19:16 -0700583 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 0;
585}
586
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100587static int rndis_query_response(struct rndis_params *params,
588 rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300591 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700592
David Brownell33376c12008-08-18 17:45:07 -0700593 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700594 if (!params->dev)
595 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700596
Shaun Tancheff87637162006-02-22 19:47:19 -0800597 /*
598 * we need more memory:
599 * gen_ndis_query_resp expects enough space for
600 * rndis_query_cmplt_type followed by data.
601 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100603 r = rndis_add_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300604 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700605 if (!r)
606 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300607 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700608
Linus Walleij51491162012-05-11 22:17:07 +0000609 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700611
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100612 if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700613 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300614 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700615 le32_to_cpu(buf->InformationBufferLength),
616 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300618 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
619 resp->MessageLength = cpu_to_le32(sizeof *resp);
620 resp->InformationBufferLength = cpu_to_le32(0);
621 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300623 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700624
David Brownell15b2d2b2008-06-19 18:19:16 -0700625 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627}
628
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100629static int rndis_set_response(struct rndis_params *params,
630 rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300632 u32 BufLength, BufOffset;
633 rndis_set_cmplt_type *resp;
634 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700635
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100636 r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700637 if (!r)
638 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300639 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Mihai Donțua1df4e42010-09-08 02:54:02 +0300641 BufLength = le32_to_cpu(buf->InformationBufferLength);
642 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
David Brownell15b2d2b2008-06-19 18:19:16 -0700644#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700645 pr_debug("%s: Length: %d\n", __func__, BufLength);
646 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
647 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700650 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
David Brownell7e27f182006-06-13 09:54:40 -0700652
David Brownell33376c12008-08-18 17:45:07 -0700653 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#endif
David Brownell7e27f182006-06-13 09:54:40 -0700655
Linus Walleij51491162012-05-11 22:17:07 +0000656 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300657 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100659 if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
Mihai Donțua1df4e42010-09-08 02:54:02 +0300660 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
661 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700662 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300663 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700664
David Brownell15b2d2b2008-06-19 18:19:16 -0700665 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100669static int rndis_reset_response(struct rndis_params *params,
670 rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300672 rndis_reset_cmplt_type *resp;
673 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700674
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100675 r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700676 if (!r)
677 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300678 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700679
Linus Walleij51491162012-05-11 22:17:07 +0000680 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300681 resp->MessageLength = cpu_to_le32(16);
682 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300684 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700685
David Brownell15b2d2b2008-06-19 18:19:16 -0700686 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return 0;
688}
689
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100690static int rndis_keepalive_response(struct rndis_params *params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300691 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300693 rndis_keepalive_cmplt_type *resp;
694 rndis_resp_t *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
697
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100698 r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700699 if (!r)
700 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300701 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700702
Linus Walleij51491162012-05-11 22:17:07 +0000703 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300704 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300706 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700707
David Brownell15b2d2b2008-06-19 18:19:16 -0700708 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710}
711
712
David Brownell7e27f182006-06-13 09:54:40 -0700713/*
714 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100716static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300718 rndis_indicate_status_msg_type *resp;
719 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700720
David Brownell15b2d2b2008-06-19 18:19:16 -0700721 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700722 return -ENOTSUPP;
723
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100724 r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700725 if (!r)
726 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300727 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700728
Linus Walleij51491162012-05-11 22:17:07 +0000729 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300730 resp->MessageLength = cpu_to_le32(20);
731 resp->Status = cpu_to_le32(status);
732 resp->StatusBufferLength = cpu_to_le32(0);
733 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700734
David Brownell15b2d2b2008-06-19 18:19:16 -0700735 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 0;
737}
738
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100739int rndis_signal_connect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100741 params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
742 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500744EXPORT_SYMBOL_GPL(rndis_signal_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100746int rndis_signal_disconnect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100748 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
749 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500751EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100753void rndis_uninit(struct rndis_params *params)
David Brownell340600a2005-04-28 13:45:25 -0700754{
David Brownell486e2df2005-05-24 17:51:52 -0700755 u8 *buf;
756 u32 length;
757
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100758 if (!params)
David Brownell340600a2005-04-28 13:45:25 -0700759 return;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100760 params->state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700761
762 /* drain the response queue */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100763 while ((buf = rndis_get_next_response(params, &length)))
764 rndis_free_response(params, buf);
David Brownell340600a2005-04-28 13:45:25 -0700765}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500766EXPORT_SYMBOL_GPL(rndis_uninit);
David Brownell340600a2005-04-28 13:45:25 -0700767
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100768void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100770 params->host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500772EXPORT_SYMBOL_GPL(rndis_set_host_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
David Brownell7e27f182006-06-13 09:54:40 -0700774/*
775 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100777int rndis_msg_parser(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 u32 MsgType, MsgLength;
780 __le32 *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!buf)
783 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700784
Mihai Donțua1df4e42010-09-08 02:54:02 +0300785 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700786 MsgType = get_unaligned_le32(tmp++);
787 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700788
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100789 if (!params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700791
David Brownell340600a2005-04-28 13:45:25 -0700792 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
793 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
794 * and normal HC level polling to see if there's any IN traffic.
795 */
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700798 switch (MsgType) {
Linus Walleij51491162012-05-11 22:17:07 +0000799 case RNDIS_MSG_INIT:
800 pr_debug("%s: RNDIS_MSG_INIT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300801 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 params->state = RNDIS_INITIALIZED;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100803 return rndis_init_response(params, (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700804
Linus Walleij51491162012-05-11 22:17:07 +0000805 case RNDIS_MSG_HALT:
806 pr_debug("%s: RNDIS_MSG_HALT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300807 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 params->state = RNDIS_UNINITIALIZED;
809 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300810 netif_carrier_off(params->dev);
811 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700814
Linus Walleij51491162012-05-11 22:17:07 +0000815 case RNDIS_MSG_QUERY:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100816 return rndis_query_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300817 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700818
Linus Walleij51491162012-05-11 22:17:07 +0000819 case RNDIS_MSG_SET:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100820 return rndis_set_response(params, (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700821
Linus Walleij51491162012-05-11 22:17:07 +0000822 case RNDIS_MSG_RESET:
823 pr_debug("%s: RNDIS_MSG_RESET\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300824 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100825 return rndis_reset_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300826 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Linus Walleij51491162012-05-11 22:17:07 +0000828 case RNDIS_MSG_KEEPALIVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700830 if (rndis_debug > 1)
Linus Walleij51491162012-05-11 22:17:07 +0000831 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300832 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100833 return rndis_keepalive_response(params,
David Brownell7e27f182006-06-13 09:54:40 -0700834 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700836
837 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* At least Windows XP emits some undefined RNDIS messages.
839 * In one case those messages seemed to relate to the host
840 * suspending itself.
841 */
David Brownell00274922007-11-19 12:58:36 -0800842 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300843 __func__, MsgType, MsgLength);
Andy Shevchenkod3091cf2012-08-07 19:07:58 +0300844 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
845 buf, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847 }
David Brownell7e27f182006-06-13 09:54:40 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -ENOTSUPP;
850}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500851EXPORT_SYMBOL_GPL(rndis_msg_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100853struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700856
David Brownell15b2d2b2008-06-19 18:19:16 -0700857 if (!resp_avail)
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100858 return ERR_PTR(-EINVAL);
David Brownell15b2d2b2008-06-19 18:19:16 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300861 if (!rndis_per_dev_params[i].used) {
862 rndis_per_dev_params[i].used = 1;
863 rndis_per_dev_params[i].resp_avail = resp_avail;
864 rndis_per_dev_params[i].v = v;
David Brownell33376c12008-08-18 17:45:07 -0700865 pr_debug("%s: configNr = %d\n", __func__, i);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100866 return &rndis_per_dev_params[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868 }
David Brownell33376c12008-08-18 17:45:07 -0700869 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700870
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100871 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500873EXPORT_SYMBOL_GPL(rndis_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100875void rndis_deregister(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300877 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700878
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100879 if (!params)
880 return;
881 params->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500883EXPORT_SYMBOL_GPL(rndis_deregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100885int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
886 u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
David Brownell33376c12008-08-18 17:45:07 -0700888 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700889 if (!dev)
890 return -EINVAL;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100891 if (!params)
892 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700893
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100894 params->dev = dev;
895 params->filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500899EXPORT_SYMBOL_GPL(rndis_set_param_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100901int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
902 const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
David Brownell33376c12008-08-18 17:45:07 -0700904 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!vendorDescr) return -1;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100906 if (!params)
907 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700908
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100909 params->vendorID = vendorID;
910 params->vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500914EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100916int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
David Brownell33376c12008-08-18 17:45:07 -0700918 pr_debug("%s: %u %u\n", __func__, medium, speed);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100919 if (!params)
920 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700921
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100922 params->medium = medium;
923 params->speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500927EXPORT_SYMBOL_GPL(rndis_set_param_medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Mihai Donțua1df4e42010-09-08 02:54:02 +0300929void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300931 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (!skb)
934 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300935 header = (void *)skb_push(skb, sizeof(*header));
936 memset(header, 0, sizeof *header);
Linus Walleij51491162012-05-11 22:17:07 +0000937 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300939 header->DataOffset = cpu_to_le32(36);
940 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500942EXPORT_SYMBOL_GPL(rndis_add_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100944void rndis_free_response(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300946 rndis_resp_t *r;
947 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700948
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100949 list_for_each_safe(act, tmp, &(params->resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300951 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (r && r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300953 list_del(&r->list);
954 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 }
957}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500958EXPORT_SYMBOL_GPL(rndis_free_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100960u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300962 rndis_resp_t *r;
963 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700966
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100967 list_for_each_safe(act, tmp, &(params->resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300969 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (!r->send) {
971 r->send = 1;
972 *length = r->length;
973 return r->buf;
974 }
975 }
David Brownell7e27f182006-06-13 09:54:40 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return NULL;
978}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500979EXPORT_SYMBOL_GPL(rndis_get_next_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100981static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300983 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700984
Mihai Donțua1df4e42010-09-08 02:54:02 +0300985 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
986 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700988
Mihai Donțua1df4e42010-09-08 02:54:02 +0300989 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 r->length = length;
991 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700992
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100993 list_add_tail(&r->list, &(params->resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return r;
995}
996
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500997int rndis_rm_hdr(struct gether *port,
998 struct sk_buff *skb,
999 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
David Brownell6cdee102005-04-18 17:39:34 -07001001 /* tmp points to a struct rndis_packet_msg_type */
Mihai Donțua1df4e42010-09-08 02:54:02 +03001002 __le32 *tmp = (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
David Brownell6cdee102005-04-18 17:39:34 -07001004 /* MessageType, MessageLength */
Linus Walleij51491162012-05-11 22:17:07 +00001005 if (cpu_to_le32(RNDIS_MSG_PACKET)
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001006 != get_unaligned(tmp++)) {
1007 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001008 return -EINVAL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001009 }
David Brownell6cdee102005-04-18 17:39:34 -07001010 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
David Brownell6cdee102005-04-18 17:39:34 -07001012 /* DataOffset, DataLength */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001013 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1014 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001015 return -EOVERFLOW;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001016 }
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001017 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001018
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001019 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 0;
1021}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001022EXPORT_SYMBOL_GPL(rndis_rm_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Mihai Donțua1df4e42010-09-08 02:54:02 +03001024#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001026static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001028 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001029
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001030 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 "Config Nr. %d\n"
1032 "used : %s\n"
1033 "state : %s\n"
1034 "medium : 0x%08X\n"
1035 "speed : %d\n"
1036 "cable : %s\n"
1037 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001038 "vendor : %s\n",
1039 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 ({ char *s = "?";
1041 switch (param->state) {
1042 case RNDIS_UNINITIALIZED:
1043 s = "RNDIS_UNINITIALIZED"; break;
1044 case RNDIS_INITIALIZED:
1045 s = "RNDIS_INITIALIZED"; break;
1046 case RNDIS_DATA_INITIALIZED:
1047 s = "RNDIS_DATA_INITIALIZED"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001048 } s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001049 param->medium,
1050 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001052 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001056static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001057 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Al Virod9dda782013-03-31 18:16:14 -04001059 rndis_params *p = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 u32 speed = 0;
1061 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 for (i = 0; i < count; i++) {
1064 char c;
1065 if (get_user(c, buffer))
1066 return -EFAULT;
1067 switch (c) {
1068 case '0':
1069 case '1':
1070 case '2':
1071 case '3':
1072 case '4':
1073 case '5':
1074 case '6':
1075 case '7':
1076 case '8':
1077 case '9':
1078 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001079 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081 case 'C':
1082 case 'c':
Mihai Donțua1df4e42010-09-08 02:54:02 +03001083 rndis_signal_connect(p->confignr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
1085 case 'D':
1086 case 'd':
1087 rndis_signal_disconnect(p->confignr);
1088 break;
David Brownell7e27f182006-06-13 09:54:40 -07001089 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001091 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
1093 }
David Brownell7e27f182006-06-13 09:54:40 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 buffer++;
1096 }
David Brownell7e27f182006-06-13 09:54:40 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return count;
1099}
1100
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001101static int rndis_proc_open(struct inode *inode, struct file *file)
1102{
Al Virod9dda782013-03-31 18:16:14 -04001103 return single_open(file, rndis_proc_show, PDE_DATA(inode));
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001104}
1105
1106static const struct file_operations rndis_proc_fops = {
1107 .owner = THIS_MODULE,
1108 .open = rndis_proc_open,
1109 .read = seq_read,
1110 .llseek = seq_lseek,
1111 .release = single_release,
1112 .write = rndis_proc_write,
1113};
1114
Mihai Donțua1df4e42010-09-08 02:54:02 +03001115#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1118
Mihai Donțua1df4e42010-09-08 02:54:02 +03001119#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121
Andrzej Pietrasiewicz9c2b85f2013-12-03 15:15:29 +01001122int rndis_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 u8 i;
1125
1126 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1127#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1128 char name [20];
1129
Mihai Donțua1df4e42010-09-08 02:54:02 +03001130 sprintf(name, NAME_TEMPLATE, i);
1131 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001132 &rndis_proc_fops,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001133 (void *)(rndis_per_dev_params + i));
1134 if (!rndis_connect_state[i]) {
1135 pr_debug("%s: remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 while (i) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001137 sprintf(name, NAME_TEMPLATE, --i);
1138 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
David Brownell33376c12008-08-18 17:45:07 -07001140 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return -EIO;
1142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143#endif
Mihai Donțua1df4e42010-09-08 02:54:02 +03001144 rndis_per_dev_params[i].confignr = i;
1145 rndis_per_dev_params[i].used = 0;
1146 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1147 rndis_per_dev_params[i].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +00001148 = RNDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001149 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
David Brownell7e27f182006-06-13 09:54:40 -07001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
1153}
1154
Andrzej Pietrasiewicz9c2b85f2013-12-03 15:15:29 +01001155void rndis_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001157#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 u8 i;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001159 char name[20];
David Brownell7e27f182006-06-13 09:54:40 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001162 sprintf(name, NAME_TEMPLATE, i);
1163 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165#endif
1166}
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +02001167