blob: bff7eb1995b8bbe450e58f71fb3661ff0e4bec79 [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/init.h>
29#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
39
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
Vamsi Krishna7f21ac82013-04-17 21:41:33 -070062int rndis_ul_max_pkt_per_xfer_rcvd;
63module_param(rndis_ul_max_pkt_per_xfer_rcvd, int, S_IRUGO);
64MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer_rcvd,
65 "Max num of REMOTE_NDIS_PACKET_MSGs received in a single transfer");
66
67int rndis_ul_max_xfer_size_rcvd;
68module_param(rndis_ul_max_xfer_size_rcvd, int, S_IRUGO);
69MODULE_PARM_DESC(rndis_ul_max_xfer_size_rcvd,
70 "Max size of bus transfer received");
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Mihai Donțua1df4e42010-09-08 02:54:02 +030073static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030076static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/* Function Prototypes */
Mihai Donțua1df4e42010-09-08 02:54:02 +030079static rndis_resp_t *rndis_add_response(int configNr, u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81
David Brownell340600a2005-04-28 13:45:25 -070082/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030083static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070084{
85 /* the general stuff */
86 OID_GEN_SUPPORTED_LIST,
87 OID_GEN_HARDWARE_STATUS,
88 OID_GEN_MEDIA_SUPPORTED,
89 OID_GEN_MEDIA_IN_USE,
90 OID_GEN_MAXIMUM_FRAME_SIZE,
91 OID_GEN_LINK_SPEED,
92 OID_GEN_TRANSMIT_BLOCK_SIZE,
93 OID_GEN_RECEIVE_BLOCK_SIZE,
94 OID_GEN_VENDOR_ID,
95 OID_GEN_VENDOR_DESCRIPTION,
96 OID_GEN_VENDOR_DRIVER_VERSION,
97 OID_GEN_CURRENT_PACKET_FILTER,
98 OID_GEN_MAXIMUM_TOTAL_SIZE,
99 OID_GEN_MEDIA_CONNECT_STATUS,
100 OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -0700101
David Brownell340600a2005-04-28 13:45:25 -0700102 /* the statistical stuff */
103 OID_GEN_XMIT_OK,
104 OID_GEN_RCV_OK,
105 OID_GEN_XMIT_ERROR,
106 OID_GEN_RCV_ERROR,
107 OID_GEN_RCV_NO_BUFFER,
108#ifdef RNDIS_OPTIONAL_STATS
109 OID_GEN_DIRECTED_BYTES_XMIT,
110 OID_GEN_DIRECTED_FRAMES_XMIT,
111 OID_GEN_MULTICAST_BYTES_XMIT,
112 OID_GEN_MULTICAST_FRAMES_XMIT,
113 OID_GEN_BROADCAST_BYTES_XMIT,
114 OID_GEN_BROADCAST_FRAMES_XMIT,
115 OID_GEN_DIRECTED_BYTES_RCV,
116 OID_GEN_DIRECTED_FRAMES_RCV,
117 OID_GEN_MULTICAST_BYTES_RCV,
118 OID_GEN_MULTICAST_FRAMES_RCV,
119 OID_GEN_BROADCAST_BYTES_RCV,
120 OID_GEN_BROADCAST_FRAMES_RCV,
121 OID_GEN_RCV_CRC_ERROR,
122 OID_GEN_TRANSMIT_QUEUE_LENGTH,
123#endif /* RNDIS_OPTIONAL_STATS */
124
David Brownell7e27f182006-06-13 09:54:40 -0700125 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700126 /* the general stuff */
127 OID_802_3_PERMANENT_ADDRESS,
128 OID_802_3_CURRENT_ADDRESS,
129 OID_802_3_MULTICAST_LIST,
130 OID_802_3_MAC_OPTIONS,
131 OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700132
David Brownell340600a2005-04-28 13:45:25 -0700133 /* the statistical stuff */
134 OID_802_3_RCV_ERROR_ALIGNMENT,
135 OID_802_3_XMIT_ONE_COLLISION,
136 OID_802_3_XMIT_MORE_COLLISIONS,
137#ifdef RNDIS_OPTIONAL_STATS
138 OID_802_3_XMIT_DEFERRED,
139 OID_802_3_XMIT_MAX_COLLISIONS,
140 OID_802_3_RCV_OVERRUN,
141 OID_802_3_XMIT_UNDERRUN,
142 OID_802_3_XMIT_HEARTBEAT_FAILURE,
143 OID_802_3_XMIT_TIMES_CRS_LOST,
144 OID_802_3_XMIT_LATE_COLLISIONS,
145#endif /* RNDIS_OPTIONAL_STATS */
146
147#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700148 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
149 * don't say what they mean ... and the NDIS specs are often
150 * confusing and/or ambiguous in this context. (That is, more
151 * so than their specs for the other OIDs.)
152 *
153 * FIXME someone who knows what these should do, please
154 * implement them!
155 */
David Brownell340600a2005-04-28 13:45:25 -0700156
157 /* power management */
158 OID_PNP_CAPABILITIES,
159 OID_PNP_QUERY_POWER,
160 OID_PNP_SET_POWER,
161
162#ifdef RNDIS_WAKEUP
163 /* wake up host */
164 OID_PNP_ENABLE_WAKE_UP,
165 OID_PNP_ADD_WAKE_UP_PATTERN,
166 OID_PNP_REMOVE_WAKE_UP_PATTERN,
167#endif /* RNDIS_WAKEUP */
168#endif /* RNDIS_PM */
169};
170
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* NDIS Functions */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300173static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
174 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300176 int retval = -ENOTSUPP;
177 u32 length = 4; /* usually */
178 __le32 *outbuf;
179 int i, count;
180 rndis_query_cmplt_type *resp;
181 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700182 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700183 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300186 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700189
190 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700191 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700192 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700193 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700194 get_unaligned_le32(&buf[i]),
195 get_unaligned_le32(&buf[i + 4]),
196 get_unaligned_le32(&buf[i + 8]),
197 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700198 }
199 }
200
201 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300202 outbuf = (__le32 *)&resp[1];
203 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700204
David Brownell15b2d2b2008-06-19 18:19:16 -0700205 net = rndis_per_dev_params[configNr].dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700206 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 switch (OID) {
209
210 /* general oids (table 4-1) */
211
212 /* mandatory */
213 case OID_GEN_SUPPORTED_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700214 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300215 length = sizeof(oid_supported_list);
216 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300218 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 retval = 0;
220 break;
David Brownell7e27f182006-06-13 09:54:40 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* mandatory */
223 case OID_GEN_HARDWARE_STATUS:
David Brownell33376c12008-08-18 17:45:07 -0700224 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700225 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700227 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * reddite ergo quae sunt Caesaris Caesari
229 * et quae sunt Dei Deo!
230 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300231 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 retval = 0;
233 break;
David Brownell7e27f182006-06-13 09:54:40 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* mandatory */
236 case OID_GEN_MEDIA_SUPPORTED:
David Brownell33376c12008-08-18 17:45:07 -0700237 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300238 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 retval = 0;
240 break;
David Brownell7e27f182006-06-13 09:54:40 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* mandatory */
243 case OID_GEN_MEDIA_IN_USE:
David Brownell33376c12008-08-18 17:45:07 -0700244 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* one medium, one transport... (maybe you do it better) */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300246 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 retval = 0;
248 break;
David Brownell7e27f182006-06-13 09:54:40 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* mandatory */
251 case OID_GEN_MAXIMUM_FRAME_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700252 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300253 if (rndis_per_dev_params[configNr].dev) {
254 *outbuf = cpu_to_le32(
255 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 break;
David Brownell7e27f182006-06-13 09:54:40 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* mandatory */
261 case OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700262 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700263 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300264 if (rndis_per_dev_params[configNr].media_state
David Brownell340600a2005-04-28 13:45:25 -0700265 == NDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300266 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300268 *outbuf = cpu_to_le32(
269 rndis_per_dev_params[configNr].speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 retval = 0;
271 break;
272
273 /* mandatory */
274 case OID_GEN_TRANSMIT_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700275 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300276 if (rndis_per_dev_params[configNr].dev) {
277 *outbuf = cpu_to_le32(
278 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 retval = 0;
280 }
281 break;
David Brownell7e27f182006-06-13 09:54:40 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* mandatory */
284 case OID_GEN_RECEIVE_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700285 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300286 if (rndis_per_dev_params[configNr].dev) {
287 *outbuf = cpu_to_le32(
288 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 retval = 0;
290 }
291 break;
David Brownell7e27f182006-06-13 09:54:40 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* mandatory */
294 case OID_GEN_VENDOR_ID:
David Brownell33376c12008-08-18 17:45:07 -0700295 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300296 *outbuf = cpu_to_le32(
297 rndis_per_dev_params[configNr].vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 retval = 0;
299 break;
David Brownell7e27f182006-06-13 09:54:40 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* mandatory */
302 case OID_GEN_VENDOR_DESCRIPTION:
David Brownell33376c12008-08-18 17:45:07 -0700303 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300304 if (rndis_per_dev_params[configNr].vendorDescr) {
305 length = strlen(rndis_per_dev_params[configNr].
306 vendorDescr);
307 memcpy(outbuf,
308 rndis_per_dev_params[configNr].vendorDescr,
309 length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400310 } else {
311 outbuf[0] = 0;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 retval = 0;
314 break;
315
316 case OID_GEN_VENDOR_DRIVER_VERSION:
David Brownell33376c12008-08-18 17:45:07 -0700317 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700319 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 retval = 0;
321 break;
322
323 /* mandatory */
324 case OID_GEN_CURRENT_PACKET_FILTER:
David Brownell33376c12008-08-18 17:45:07 -0700325 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300326 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 retval = 0;
328 break;
329
330 /* mandatory */
331 case OID_GEN_MAXIMUM_TOTAL_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700332 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800333 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 retval = 0;
335 break;
336
337 /* mandatory */
338 case OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700339 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700340 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300341 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 .media_state);
343 retval = 0;
344 break;
345
346 case OID_GEN_PHYSICAL_MEDIUM:
David Brownell33376c12008-08-18 17:45:07 -0700347 pr_debug("%s: 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 */
356 case OID_GEN_MAC_OPTIONS: /* from WinME */
David Brownell33376c12008-08-18 17:45:07 -0700357 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800358 *outbuf = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
360 | NDIS_MAC_OPTION_FULL_DUPLEX);
361 retval = 0;
362 break;
363
364 /* statistics OIDs (table 4-2) */
365
366 /* mandatory */
367 case OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700368 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700369 pr_debug("%s: 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 */
378 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700379 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700380 pr_debug("%s: 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 */
389 case OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700390 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700391 pr_debug("%s: 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 */
399 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700400 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700401 pr_debug("%s: 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 */
409 case OID_GEN_RCV_NO_BUFFER:
David Brownell33376c12008-08-18 17:45:07 -0700410 pr_debug("%s: 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 */
420 case OID_802_3_PERMANENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700421 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300422 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300424 memcpy(outbuf,
425 rndis_per_dev_params[configNr].host_mac,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 length);
427 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 break;
David Brownell7e27f182006-06-13 09:54:40 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* mandatory */
432 case OID_802_3_CURRENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700433 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300434 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300436 memcpy(outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 rndis_per_dev_params [configNr].host_mac,
438 length);
439 retval = 0;
440 }
441 break;
David Brownell7e27f182006-06-13 09:54:40 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* mandatory */
444 case OID_802_3_MULTICAST_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700445 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300447 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 retval = 0;
449 break;
David Brownell7e27f182006-06-13 09:54:40 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* mandatory */
452 case OID_802_3_MAXIMUM_LIST_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700453 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300455 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 retval = 0;
457 break;
David Brownell7e27f182006-06-13 09:54:40 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case OID_802_3_MAC_OPTIONS:
David Brownell33376c12008-08-18 17:45:07 -0700460 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700461 *outbuf = cpu_to_le32(0);
462 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464
465 /* ieee802.3 statistics OIDs (table 4-4) */
466
467 /* mandatory */
468 case OID_802_3_RCV_ERROR_ALIGNMENT:
David Brownell33376c12008-08-18 17:45:07 -0700469 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700470 if (stats) {
471 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 retval = 0;
473 }
474 break;
David Brownell7e27f182006-06-13 09:54:40 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* mandatory */
477 case OID_802_3_XMIT_ONE_COLLISION:
David Brownell33376c12008-08-18 17:45:07 -0700478 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300479 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 retval = 0;
481 break;
David Brownell7e27f182006-06-13 09:54:40 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* mandatory */
484 case OID_802_3_XMIT_MORE_COLLISIONS:
David Brownell33376c12008-08-18 17:45:07 -0700485 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300486 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 retval = 0;
488 break;
David Brownell7e27f182006-06-13 09:54:40 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 default:
David Brownell00274922007-11-19 12:58:36 -0800491 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800492 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
David Brownell340600a2005-04-28 13:45:25 -0700494 if (retval < 0)
495 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700496
Mihai Donțua1df4e42010-09-08 02:54:02 +0300497 resp->InformationBufferLength = cpu_to_le32(length);
498 r->length = length + sizeof(*resp);
499 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return retval;
501}
502
Mihai Donțua1df4e42010-09-08 02:54:02 +0300503static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
504 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300506 rndis_set_cmplt_type *resp;
507 int i, retval = -ENOTSUPP;
508 struct rndis_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (!r)
511 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300512 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!resp)
514 return -ENOMEM;
515
David Brownell340600a2005-04-28 13:45:25 -0700516 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700517 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700518 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700519 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700520 get_unaligned_le32(&buf[i]),
521 get_unaligned_le32(&buf[i + 4]),
522 get_unaligned_le32(&buf[i + 8]),
523 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Mihai Donțua1df4e42010-09-08 02:54:02 +0300527 params = &rndis_per_dev_params[configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 switch (OID) {
529 case OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
David Brownell340600a2005-04-28 13:45:25 -0700531 /* these NDIS_PACKET_TYPE_* bitflags are shared with
532 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
534 * PROMISCUOUS, DIRECTED,
535 * MULTICAST, ALL_MULTICAST, BROADCAST
536 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700537 *params->filter = (u16)get_unaligned_le32(buf);
David Brownell33376c12008-08-18 17:45:07 -0700538 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800539 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* this call has a significant side effect: it's
542 * what makes the packet flow start and stop, like
543 * activating the CDC Ethernet altsetting.
544 */
David Brownell340600a2005-04-28 13:45:25 -0700545 retval = 0;
546 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 params->state = RNDIS_DATA_INITIALIZED;
548 netif_carrier_on(params->dev);
549 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300550 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 } else {
552 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300553 netif_carrier_off(params->dev);
554 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 break;
David Brownell7e27f182006-06-13 09:54:40 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 case OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700559 /* I think we can ignore this */
David Brownell33376c12008-08-18 17:45:07 -0700560 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 retval = 0;
562 break;
David Brownell340600a2005-04-28 13:45:25 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 default:
David Brownell00274922007-11-19 12:58:36 -0800565 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800566 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
David Brownell7e27f182006-06-13 09:54:40 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return retval;
570}
571
David Brownell7e27f182006-06-13 09:54:40 -0700572/*
573 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575
Mihai Donțua1df4e42010-09-08 02:54:02 +0300576static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300578 rndis_init_cmplt_type *resp;
579 rndis_resp_t *r;
580 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700581
David Brownell15b2d2b2008-06-19 18:19:16 -0700582 if (!params->dev)
583 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700584
Mihai Donțua1df4e42010-09-08 02:54:02 +0300585 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700586 if (!r)
587 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300588 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700589
Mihai Donțua1df4e42010-09-08 02:54:02 +0300590 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INITIALIZE_CMPLT);
591 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300593 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
594 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
595 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
596 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
597 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
Ofir Cohenaef90b72012-07-31 12:37:04 +0200598 resp->MaxPacketsPerTransfer = cpu_to_le32(params->max_pkt_per_xfer);
Ofir Cohen76624ed2012-09-09 10:27:58 +0300599 resp->MaxTransferSize = cpu_to_le32(params->max_pkt_per_xfer *
600 (params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300601 + sizeof(struct ethhdr)
602 + sizeof(struct rndis_packet_msg_type)
Ofir Cohen76624ed2012-09-09 10:27:58 +0300603 + 22));
604 resp->PacketAlignmentFactor = cpu_to_le32(params->pkt_alignment_factor);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300605 resp->AFListOffset = cpu_to_le32(0);
606 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700607
David Brownell15b2d2b2008-06-19 18:19:16 -0700608 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610}
611
Mihai Donțua1df4e42010-09-08 02:54:02 +0300612static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300615 rndis_resp_t *r;
616 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700617
David Brownell33376c12008-08-18 17:45:07 -0700618 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700619 if (!params->dev)
620 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700621
Shaun Tancheff87637162006-02-22 19:47:19 -0800622 /*
623 * we need more memory:
624 * gen_ndis_query_resp expects enough space for
625 * rndis_query_cmplt_type followed by data.
626 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300628 r = rndis_add_response(configNr,
629 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700630 if (!r)
631 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300632 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700633
Mihai Donțua1df4e42010-09-08 02:54:02 +0300634 resp->MessageType = cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700636
Mihai Donțua1df4e42010-09-08 02:54:02 +0300637 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700638 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300639 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700640 le32_to_cpu(buf->InformationBufferLength),
641 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300643 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
644 resp->MessageLength = cpu_to_le32(sizeof *resp);
645 resp->InformationBufferLength = cpu_to_le32(0);
646 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300648 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700649
David Brownell15b2d2b2008-06-19 18:19:16 -0700650 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652}
653
Mihai Donțua1df4e42010-09-08 02:54:02 +0300654static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300656 u32 BufLength, BufOffset;
657 rndis_set_cmplt_type *resp;
658 rndis_resp_t *r;
659 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700660
Mihai Donțua1df4e42010-09-08 02:54:02 +0300661 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700662 if (!r)
663 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300664 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Mihai Donțua1df4e42010-09-08 02:54:02 +0300666 BufLength = le32_to_cpu(buf->InformationBufferLength);
667 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
David Brownell15b2d2b2008-06-19 18:19:16 -0700669#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700670 pr_debug("%s: Length: %d\n", __func__, BufLength);
671 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
672 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700675 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
David Brownell7e27f182006-06-13 09:54:40 -0700677
David Brownell33376c12008-08-18 17:45:07 -0700678 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679#endif
David Brownell7e27f182006-06-13 09:54:40 -0700680
Mihai Donțua1df4e42010-09-08 02:54:02 +0300681 resp->MessageType = cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
682 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300684 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
685 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
686 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700687 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300688 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700689
David Brownell15b2d2b2008-06-19 18:19:16 -0700690 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692}
693
Mihai Donțua1df4e42010-09-08 02:54:02 +0300694static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300696 rndis_reset_cmplt_type *resp;
697 rndis_resp_t *r;
698 struct rndis_params *params = rndis_per_dev_params + configNr;
Rajkumar Raghupathy0a9a44c2013-02-22 19:57:36 +0530699 u32 length;
700 u8 *xbuf;
701
702 /* drain the response queue */
703 while ((xbuf = rndis_get_next_response(configNr, &length)))
704 rndis_free_response(configNr, xbuf);
David Brownell7e27f182006-06-13 09:54:40 -0700705
Mihai Donțua1df4e42010-09-08 02:54:02 +0300706 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700707 if (!r)
708 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300709 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700710
Mihai Donțua1df4e42010-09-08 02:54:02 +0300711 resp->MessageType = cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
712 resp->MessageLength = cpu_to_le32(16);
713 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300715 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700716
David Brownell15b2d2b2008-06-19 18:19:16 -0700717 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719}
720
Mihai Donțua1df4e42010-09-08 02:54:02 +0300721static int rndis_keepalive_response(int configNr,
722 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300724 rndis_keepalive_cmplt_type *resp;
725 rndis_resp_t *r;
726 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
729
Mihai Donțua1df4e42010-09-08 02:54:02 +0300730 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700731 if (!r)
732 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300733 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700734
Mihai Donțua1df4e42010-09-08 02:54:02 +0300735 resp->MessageType = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 REMOTE_NDIS_KEEPALIVE_CMPLT);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300737 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300739 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700740
David Brownell15b2d2b2008-06-19 18:19:16 -0700741 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return 0;
743}
744
745
David Brownell7e27f182006-06-13 09:54:40 -0700746/*
747 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300749static int rndis_indicate_status_msg(int configNr, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300751 rndis_indicate_status_msg_type *resp;
752 rndis_resp_t *r;
753 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700754
David Brownell15b2d2b2008-06-19 18:19:16 -0700755 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700756 return -ENOTSUPP;
757
Mihai Donțua1df4e42010-09-08 02:54:02 +0300758 r = rndis_add_response(configNr,
759 sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700760 if (!r)
761 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300762 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700763
Mihai Donțua1df4e42010-09-08 02:54:02 +0300764 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INDICATE_STATUS_MSG);
765 resp->MessageLength = cpu_to_le32(20);
766 resp->Status = cpu_to_le32(status);
767 resp->StatusBufferLength = cpu_to_le32(0);
768 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700769
David Brownell15b2d2b2008-06-19 18:19:16 -0700770 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
772}
773
Mihai Donțua1df4e42010-09-08 02:54:02 +0300774int rndis_signal_connect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300776 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 = NDIS_MEDIA_STATE_CONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300778 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 RNDIS_STATUS_MEDIA_CONNECT);
780}
781
Mihai Donțua1df4e42010-09-08 02:54:02 +0300782int rndis_signal_disconnect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300784 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300786 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 RNDIS_STATUS_MEDIA_DISCONNECT);
788}
789
Mihai Donțua1df4e42010-09-08 02:54:02 +0300790void rndis_uninit(int configNr)
David Brownell340600a2005-04-28 13:45:25 -0700791{
David Brownell486e2df2005-05-24 17:51:52 -0700792 u8 *buf;
793 u32 length;
794
David Brownell340600a2005-04-28 13:45:25 -0700795 if (configNr >= RNDIS_MAX_CONFIGS)
796 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300797 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700798
799 /* drain the response queue */
800 while ((buf = rndis_get_next_response(configNr, &length)))
801 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700802}
803
Mihai Donțua1df4e42010-09-08 02:54:02 +0300804void rndis_set_host_mac(int configNr, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300806 rndis_per_dev_params[configNr].host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
David Brownell7e27f182006-06-13 09:54:40 -0700809/*
810 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300812int rndis_msg_parser(u8 configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 u32 MsgType, MsgLength;
815 __le32 *tmp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300816 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!buf)
819 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700820
Mihai Donțua1df4e42010-09-08 02:54:02 +0300821 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700822 MsgType = get_unaligned_le32(tmp++);
823 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (configNr >= RNDIS_MAX_CONFIGS)
826 return -ENOTSUPP;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300827 params = &rndis_per_dev_params[configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700828
David Brownell340600a2005-04-28 13:45:25 -0700829 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
830 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
831 * and normal HC level polling to see if there's any IN traffic.
832 */
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700835 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700837 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300838 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300840 return rndis_init_response(configNr,
841 (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 case REMOTE_NDIS_HALT_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700844 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300845 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 params->state = RNDIS_UNINITIALIZED;
847 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300848 netif_carrier_off(params->dev);
849 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 case REMOTE_NDIS_QUERY_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300854 return rndis_query_response(configNr,
855 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 case REMOTE_NDIS_SET_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300858 return rndis_set_response(configNr,
859 (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 case REMOTE_NDIS_RESET_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700862 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300863 __func__);
864 return rndis_reset_response(configNr,
865 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 case REMOTE_NDIS_KEEPALIVE_MSG:
868 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700869 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700870 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300871 __func__);
872 return rndis_keepalive_response(configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700873 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700875
876 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* At least Windows XP emits some undefined RNDIS messages.
878 * In one case those messages seemed to relate to the host
879 * suspending itself.
880 */
David Brownell00274922007-11-19 12:58:36 -0800881 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300882 __func__, MsgType, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 {
884 unsigned i;
885 for (i = 0; i < MsgLength; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700886 pr_debug("%03d: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 " %02x %02x %02x %02x"
888 " %02x %02x %02x %02x"
889 " %02x %02x %02x %02x"
890 " %02x %02x %02x %02x"
891 "\n",
892 i,
893 buf[i], buf [i+1],
894 buf[i+2], buf[i+3],
895 buf[i+4], buf [i+5],
896 buf[i+6], buf[i+7],
897 buf[i+8], buf [i+9],
898 buf[i+10], buf[i+11],
899 buf[i+12], buf [i+13],
900 buf[i+14], buf[i+15]);
901 }
902 }
903 break;
904 }
David Brownell7e27f182006-06-13 09:54:40 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -ENOTSUPP;
907}
908
David Brownell15b2d2b2008-06-19 18:19:16 -0700909int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700912
David Brownell15b2d2b2008-06-19 18:19:16 -0700913 if (!resp_avail)
914 return -EINVAL;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300917 if (!rndis_per_dev_params[i].used) {
918 rndis_per_dev_params[i].used = 1;
919 rndis_per_dev_params[i].resp_avail = resp_avail;
920 rndis_per_dev_params[i].v = v;
Mayank Ranae5d2b3d2012-09-05 13:12:26 +0530921 rndis_per_dev_params[i].max_pkt_per_xfer = 1;
Ofir Cohen76624ed2012-09-09 10:27:58 +0300922 rndis_per_dev_params[i].pkt_alignment_factor = 0;
David Brownell33376c12008-08-18 17:45:07 -0700923 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return i;
925 }
926 }
David Brownell33376c12008-08-18 17:45:07 -0700927 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700928
David Brownell15b2d2b2008-06-19 18:19:16 -0700929 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Mihai Donțua1df4e42010-09-08 02:54:02 +0300932void rndis_deregister(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300934 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (configNr >= RNDIS_MAX_CONFIGS) return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300937 rndis_per_dev_params[configNr].used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
David Brownell15b2d2b2008-06-19 18:19:16 -0700940int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
David Brownell33376c12008-08-18 17:45:07 -0700942 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700943 if (!dev)
944 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700946
Mihai Donțua1df4e42010-09-08 02:54:02 +0300947 rndis_per_dev_params[configNr].dev = dev;
948 rndis_per_dev_params[configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700949
Vamsi Krishna915ae192013-05-13 15:39:56 -0700950 /* reset aggregation stats for every set_alt */
951 rndis_ul_max_xfer_size_rcvd = 0;
952 rndis_ul_max_pkt_per_xfer_rcvd = 0;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955}
956
Mihai Donțua1df4e42010-09-08 02:54:02 +0300957int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
David Brownell33376c12008-08-18 17:45:07 -0700959 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (!vendorDescr) return -1;
961 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700962
Mihai Donțua1df4e42010-09-08 02:54:02 +0300963 rndis_per_dev_params[configNr].vendorID = vendorID;
964 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
967}
968
Mihai Donțua1df4e42010-09-08 02:54:02 +0300969int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
David Brownell33376c12008-08-18 17:45:07 -0700971 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700973
Mihai Donțua1df4e42010-09-08 02:54:02 +0300974 rndis_per_dev_params[configNr].medium = medium;
975 rndis_per_dev_params[configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 0;
978}
979
Ofir Cohenaef90b72012-07-31 12:37:04 +0200980void rndis_set_max_pkt_xfer(u8 configNr, u8 max_pkt_per_xfer)
981{
982 pr_debug("%s:\n", __func__);
983
984 rndis_per_dev_params[configNr].max_pkt_per_xfer = max_pkt_per_xfer;
985}
986
Ofir Cohen76624ed2012-09-09 10:27:58 +0300987void rndis_set_pkt_alignment_factor(u8 configNr, u8 pkt_alignment_factor)
988{
989 pr_debug("%s:\n", __func__);
990
991 rndis_per_dev_params[configNr].pkt_alignment_factor =
992 pkt_alignment_factor;
993}
994
Mihai Donțua1df4e42010-09-08 02:54:02 +0300995void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300997 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (!skb)
1000 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001001 header = (void *)skb_push(skb, sizeof(*header));
1002 memset(header, 0, sizeof *header);
Harvey Harrison35c26c22009-02-14 22:56:56 -08001003 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +03001005 header->DataOffset = cpu_to_le32(36);
1006 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Mihai Donțua1df4e42010-09-08 02:54:02 +03001009void rndis_free_response(int configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001011 rndis_resp_t *r;
1012 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -07001013
Mihai Donțua1df4e42010-09-08 02:54:02 +03001014 list_for_each_safe(act, tmp,
1015 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001017 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (r && r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001019 list_del(&r->list);
1020 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 }
1023}
1024
Mihai Donțua1df4e42010-09-08 02:54:02 +03001025u8 *rndis_get_next_response(int configNr, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001027 rndis_resp_t *r;
1028 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001031
Mihai Donțua1df4e42010-09-08 02:54:02 +03001032 list_for_each_safe(act, tmp,
1033 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001035 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (!r->send) {
1037 r->send = 1;
1038 *length = r->length;
1039 return r->buf;
1040 }
1041 }
David Brownell7e27f182006-06-13 09:54:40 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return NULL;
1044}
1045
Mihai Donțua1df4e42010-09-08 02:54:02 +03001046static rndis_resp_t *rndis_add_response(int configNr, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001048 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001049
Mihai Donțua1df4e42010-09-08 02:54:02 +03001050 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1051 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001053
Mihai Donțua1df4e42010-09-08 02:54:02 +03001054 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 r->length = length;
1056 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001057
Mihai Donțua1df4e42010-09-08 02:54:02 +03001058 list_add_tail(&r->list,
1059 &(rndis_per_dev_params[configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return r;
1061}
1062
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001063int rndis_rm_hdr(struct gether *port,
1064 struct sk_buff *skb,
1065 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Vamsi Krishna915ae192013-05-13 15:39:56 -07001067 int num_pkts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Vamsi Krishna7f21ac82013-04-17 21:41:33 -07001069 if (skb->len > rndis_ul_max_xfer_size_rcvd)
1070 rndis_ul_max_xfer_size_rcvd = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Vamsi Krishna7f21ac82013-04-17 21:41:33 -07001072 while (skb->len) {
1073 struct rndis_packet_msg_type *hdr;
1074 struct sk_buff *skb2;
1075 u32 msg_len, data_offset, data_len;
1076
1077 /* some rndis hosts send extra byte to avoid zlp, ignore it */
1078 if (skb->len == 1) {
Vamsi Krishna915ae192013-05-13 15:39:56 -07001079 if (num_pkts > rndis_ul_max_pkt_per_xfer_rcvd)
1080 rndis_ul_max_pkt_per_xfer_rcvd = num_pkts;
Vamsi Krishna7f21ac82013-04-17 21:41:33 -07001081 dev_kfree_skb_any(skb);
1082 return 0;
1083 }
1084
1085 if (skb->len < sizeof *hdr) {
1086 pr_err("invalid rndis pkt: skblen:%u hdr_len:%u",
1087 skb->len, sizeof *hdr);
1088 dev_kfree_skb_any(skb);
1089 return -EINVAL;
1090 }
1091
1092 hdr = (void *)skb->data;
1093 msg_len = le32_to_cpu(hdr->MessageLength);
1094 data_offset = le32_to_cpu(hdr->DataOffset);
1095 data_len = le32_to_cpu(hdr->DataLength);
1096
1097 if (skb->len < msg_len ||
1098 ((data_offset + data_len + 8) > msg_len)) {
1099 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1100 le32_to_cpu(hdr->MessageType),
1101 msg_len, data_offset, data_len, skb->len);
1102 dev_kfree_skb_any(skb);
1103 return -EOVERFLOW;
1104 }
1105
1106 if (le32_to_cpu(hdr->MessageType) != REMOTE_NDIS_PACKET_MSG) {
1107 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1108 le32_to_cpu(hdr->MessageType),
1109 msg_len, data_offset, data_len, skb->len);
1110 dev_kfree_skb_any(skb);
1111 return -EINVAL;
1112 }
1113
Vamsi Krishna915ae192013-05-13 15:39:56 -07001114 num_pkts++;
1115
Vamsi Krishna7f21ac82013-04-17 21:41:33 -07001116 skb_pull(skb, data_offset + 8);
1117
1118 if (msg_len == skb->len) {
1119 skb_trim(skb, data_len);
1120 break;
1121 }
1122
1123 skb2 = skb_clone(skb, GFP_ATOMIC);
1124 if (!skb2) {
1125 pr_err("%s:skb clone failed\n", __func__);
1126 dev_kfree_skb_any(skb);
1127 return -ENOMEM;
1128 }
1129
1130 skb_pull(skb, msg_len - sizeof *hdr);
1131 skb_trim(skb2, data_len);
1132 skb_queue_tail(list, skb2);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001133 }
Vamsi Krishna7f21ac82013-04-17 21:41:33 -07001134
1135 if (num_pkts > rndis_ul_max_pkt_per_xfer_rcvd)
1136 rndis_ul_max_pkt_per_xfer_rcvd = num_pkts;
David Brownell6cdee102005-04-18 17:39:34 -07001137
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001138 skb_queue_tail(list, skb);
Vamsi Krishna7f21ac82013-04-17 21:41:33 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return 0;
1141}
1142
Mihai Donțua1df4e42010-09-08 02:54:02 +03001143#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001145static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001147 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001148
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001149 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 "Config Nr. %d\n"
1151 "used : %s\n"
1152 "state : %s\n"
1153 "medium : 0x%08X\n"
1154 "speed : %d\n"
1155 "cable : %s\n"
1156 "vendor ID : 0x%08X\n"
Vamsi Krishna915ae192013-05-13 15:39:56 -07001157 "vendor : %s\n"
1158 "ul-max-xfer-size:%d max-xfer-size-rcvd: %d\n"
1159 "ul-max-pkts-per-xfer:%d max-pkts-per-xfer-rcvd:%d\n",
David Brownell7e27f182006-06-13 09:54:40 -07001160 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 ({ char *s = "?";
1162 switch (param->state) {
1163 case RNDIS_UNINITIALIZED:
1164 s = "RNDIS_UNINITIALIZED"; break;
1165 case RNDIS_INITIALIZED:
1166 s = "RNDIS_INITIALIZED"; break;
1167 case RNDIS_DATA_INITIALIZED:
1168 s = "RNDIS_DATA_INITIALIZED"; break;
1169 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001170 param->medium,
1171 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 (param->media_state) ? "disconnected" : "connected",
Vamsi Krishna915ae192013-05-13 15:39:56 -07001173 param->vendorID, param->vendorDescr,
1174 param->max_pkt_per_xfer *
1175 (param->dev->mtu + sizeof(struct ethhdr) +
1176 sizeof(struct rndis_packet_msg_type) + 22),
1177 rndis_ul_max_xfer_size_rcvd,
1178 param->max_pkt_per_xfer,
1179 rndis_ul_max_pkt_per_xfer_rcvd);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001183static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001184 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001186 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 u32 speed = 0;
1188 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 for (i = 0; i < count; i++) {
1191 char c;
1192 if (get_user(c, buffer))
1193 return -EFAULT;
1194 switch (c) {
1195 case '0':
1196 case '1':
1197 case '2':
1198 case '3':
1199 case '4':
1200 case '5':
1201 case '6':
1202 case '7':
1203 case '8':
1204 case '9':
1205 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001206 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 break;
1208 case 'C':
1209 case 'c':
Mihai Donțua1df4e42010-09-08 02:54:02 +03001210 rndis_signal_connect(p->confignr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
1212 case 'D':
1213 case 'd':
1214 rndis_signal_disconnect(p->confignr);
1215 break;
David Brownell7e27f182006-06-13 09:54:40 -07001216 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001218 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
1220 }
David Brownell7e27f182006-06-13 09:54:40 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 buffer++;
1223 }
David Brownell7e27f182006-06-13 09:54:40 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return count;
1226}
1227
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001228static int rndis_proc_open(struct inode *inode, struct file *file)
1229{
1230 return single_open(file, rndis_proc_show, PDE(inode)->data);
1231}
1232
1233static const struct file_operations rndis_proc_fops = {
1234 .owner = THIS_MODULE,
1235 .open = rndis_proc_open,
1236 .read = seq_read,
1237 .llseek = seq_lseek,
1238 .release = single_release,
1239 .write = rndis_proc_write,
1240};
1241
Mihai Donțua1df4e42010-09-08 02:54:02 +03001242#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1245
Mihai Donțua1df4e42010-09-08 02:54:02 +03001246#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Benoit Gobya6ccb732012-01-20 14:42:41 -08001248static bool rndis_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Henrik Kretzschmar793f03a2010-08-20 19:57:50 +02001250int rndis_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 u8 i;
1253
Benoit Gobya6ccb732012-01-20 14:42:41 -08001254 if (rndis_initialized)
1255 return 0;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1258#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1259 char name [20];
1260
Mihai Donțua1df4e42010-09-08 02:54:02 +03001261 sprintf(name, NAME_TEMPLATE, i);
1262 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001263 &rndis_proc_fops,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001264 (void *)(rndis_per_dev_params + i));
1265 if (!rndis_connect_state[i]) {
1266 pr_debug("%s: remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 while (i) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001268 sprintf(name, NAME_TEMPLATE, --i);
1269 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
David Brownell33376c12008-08-18 17:45:07 -07001271 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -EIO;
1273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274#endif
Mihai Donțua1df4e42010-09-08 02:54:02 +03001275 rndis_per_dev_params[i].confignr = i;
1276 rndis_per_dev_params[i].used = 0;
1277 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1278 rndis_per_dev_params[i].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001280 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
David Brownell7e27f182006-06-13 09:54:40 -07001282
Benoit Gobya6ccb732012-01-20 14:42:41 -08001283 rndis_initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
1286
Mihai Donțua1df4e42010-09-08 02:54:02 +03001287void rndis_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001289#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 u8 i;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001291 char name[20];
Benoit Gobya6ccb732012-01-20 14:42:41 -08001292#endif
David Brownell7e27f182006-06-13 09:54:40 -07001293
Benoit Gobya6ccb732012-01-20 14:42:41 -08001294 if (!rndis_initialized)
1295 return;
1296 rndis_initialized = false;
1297
1298#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001300 sprintf(name, NAME_TEMPLATE, i);
1301 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303#endif
1304}