blob: e4192b887de98014c6eb2d6d3090f36607600a4f [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
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 */
Mihai Donțua1df4e42010-09-08 02:54:02 +030069static rndis_resp_t *rndis_add_response(int configNr, u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71
David Brownell340600a2005-04-28 13:45:25 -070072/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030073static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070074{
75 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000076 RNDIS_OID_GEN_SUPPORTED_LIST,
77 RNDIS_OID_GEN_HARDWARE_STATUS,
78 RNDIS_OID_GEN_MEDIA_SUPPORTED,
79 RNDIS_OID_GEN_MEDIA_IN_USE,
80 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
81 RNDIS_OID_GEN_LINK_SPEED,
82 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
83 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
84 RNDIS_OID_GEN_VENDOR_ID,
85 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
86 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
87 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
88 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
89 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
90 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070091
David Brownell340600a2005-04-28 13:45:25 -070092 /* the statistical stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000093 RNDIS_OID_GEN_XMIT_OK,
94 RNDIS_OID_GEN_RCV_OK,
95 RNDIS_OID_GEN_XMIT_ERROR,
96 RNDIS_OID_GEN_RCV_ERROR,
97 RNDIS_OID_GEN_RCV_NO_BUFFER,
David Brownell340600a2005-04-28 13:45:25 -070098#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij8cdddc32012-05-11 22:16:08 +000099 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
100 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
101 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
102 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
103 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
104 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
105 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
106 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
107 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
108 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
109 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
110 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
111 RNDIS_OID_GEN_RCV_CRC_ERROR,
112 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
David Brownell340600a2005-04-28 13:45:25 -0700113#endif /* RNDIS_OPTIONAL_STATS */
114
David Brownell7e27f182006-06-13 09:54:40 -0700115 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700116 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000117 RNDIS_OID_802_3_PERMANENT_ADDRESS,
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000118 RNDIS_OID_802_3_CURRENT_ADDRESS,
119 RNDIS_OID_802_3_MULTICAST_LIST,
120 RNDIS_OID_802_3_MAC_OPTIONS,
121 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700122
David Brownell340600a2005-04-28 13:45:25 -0700123 /* the statistical stuff */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000124 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
125 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
126 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700127#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000128 RNDIS_OID_802_3_XMIT_DEFERRED,
129 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
130 RNDIS_OID_802_3_RCV_OVERRUN,
131 RNDIS_OID_802_3_XMIT_UNDERRUN,
132 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
133 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
134 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700135#endif /* RNDIS_OPTIONAL_STATS */
136
137#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700138 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
139 * don't say what they mean ... and the NDIS specs are often
140 * confusing and/or ambiguous in this context. (That is, more
141 * so than their specs for the other OIDs.)
142 *
143 * FIXME someone who knows what these should do, please
144 * implement them!
145 */
David Brownell340600a2005-04-28 13:45:25 -0700146
147 /* power management */
148 OID_PNP_CAPABILITIES,
149 OID_PNP_QUERY_POWER,
150 OID_PNP_SET_POWER,
151
152#ifdef RNDIS_WAKEUP
153 /* wake up host */
154 OID_PNP_ENABLE_WAKE_UP,
155 OID_PNP_ADD_WAKE_UP_PATTERN,
156 OID_PNP_REMOVE_WAKE_UP_PATTERN,
157#endif /* RNDIS_WAKEUP */
158#endif /* RNDIS_PM */
159};
160
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* NDIS Functions */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300163static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
164 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300166 int retval = -ENOTSUPP;
167 u32 length = 4; /* usually */
168 __le32 *outbuf;
169 int i, count;
170 rndis_query_cmplt_type *resp;
171 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700172 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700173 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300176 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700179
180 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700181 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700182 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700183 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700184 get_unaligned_le32(&buf[i]),
185 get_unaligned_le32(&buf[i + 4]),
186 get_unaligned_le32(&buf[i + 8]),
187 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700188 }
189 }
190
191 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300192 outbuf = (__le32 *)&resp[1];
193 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700194
David Brownell15b2d2b2008-06-19 18:19:16 -0700195 net = rndis_per_dev_params[configNr].dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700196 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 switch (OID) {
199
200 /* general oids (table 4-1) */
201
202 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000203 case RNDIS_OID_GEN_SUPPORTED_LIST:
204 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300205 length = sizeof(oid_supported_list);
206 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300208 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 retval = 0;
210 break;
David Brownell7e27f182006-06-13 09:54:40 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000213 case RNDIS_OID_GEN_HARDWARE_STATUS:
214 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700215 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700217 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * reddite ergo quae sunt Caesaris Caesari
219 * et quae sunt Dei Deo!
220 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300221 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 retval = 0;
223 break;
David Brownell7e27f182006-06-13 09:54:40 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000226 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
227 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300228 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 retval = 0;
230 break;
David Brownell7e27f182006-06-13 09:54:40 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000233 case RNDIS_OID_GEN_MEDIA_IN_USE:
234 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* one medium, one transport... (maybe you do it better) */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300236 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 retval = 0;
238 break;
David Brownell7e27f182006-06-13 09:54:40 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000241 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
242 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300243 if (rndis_per_dev_params[configNr].dev) {
244 *outbuf = cpu_to_le32(
245 rndis_per_dev_params[configNr].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__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300254 if (rndis_per_dev_params[configNr].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +0000255 == RNDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300256 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300258 *outbuf = cpu_to_le32(
259 rndis_per_dev_params[configNr].speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 retval = 0;
261 break;
262
263 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000264 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
265 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300266 if (rndis_per_dev_params[configNr].dev) {
267 *outbuf = cpu_to_le32(
268 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 retval = 0;
270 }
271 break;
David Brownell7e27f182006-06-13 09:54:40 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000274 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
275 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_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 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000284 case RNDIS_OID_GEN_VENDOR_ID:
285 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300286 *outbuf = cpu_to_le32(
287 rndis_per_dev_params[configNr].vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 retval = 0;
289 break;
David Brownell7e27f182006-06-13 09:54:40 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000292 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
293 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300294 if (rndis_per_dev_params[configNr].vendorDescr) {
295 length = strlen(rndis_per_dev_params[configNr].
296 vendorDescr);
297 memcpy(outbuf,
298 rndis_per_dev_params[configNr].vendorDescr,
299 length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400300 } else {
301 outbuf[0] = 0;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 retval = 0;
304 break;
305
Linus Walleij8cdddc32012-05-11 22:16:08 +0000306 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
307 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700309 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 retval = 0;
311 break;
312
313 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000314 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
315 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300316 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 retval = 0;
318 break;
319
320 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000321 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
322 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800323 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 retval = 0;
325 break;
326
327 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000328 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700329 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000330 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300331 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .media_state);
333 retval = 0;
334 break;
335
Linus Walleij8cdddc32012-05-11 22:16:08 +0000336 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
337 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300338 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 retval = 0;
340 break;
341
342 /* The RNDIS specification is incomplete/wrong. Some versions
343 * of MS-Windows expect OIDs that aren't specified there. Other
344 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
345 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000346 case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
347 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800348 *outbuf = cpu_to_le32(
Linus Walleije20289e2012-05-11 22:17:19 +0000349 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
350 | RNDIS_MAC_OPTION_FULL_DUPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 retval = 0;
352 break;
353
354 /* statistics OIDs (table 4-2) */
355
356 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000357 case RNDIS_OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700358 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000359 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700360 if (stats) {
361 *outbuf = cpu_to_le32(stats->tx_packets
362 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 break;
366
367 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000368 case RNDIS_OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700369 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000370 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700371 if (stats) {
372 *outbuf = cpu_to_le32(stats->rx_packets
373 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 break;
David Brownell7e27f182006-06-13 09:54:40 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000379 case RNDIS_OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700380 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000381 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700382 if (stats) {
383 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 break;
David Brownell7e27f182006-06-13 09:54:40 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000389 case RNDIS_OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700390 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000391 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700392 if (stats) {
393 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 break;
David Brownell7e27f182006-06-13 09:54:40 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000399 case RNDIS_OID_GEN_RCV_NO_BUFFER:
400 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700401 if (stats) {
402 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 break;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* ieee802.3 OIDs (table 4-3) */
408
409 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000410 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000411 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300412 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300414 memcpy(outbuf,
415 rndis_per_dev_params[configNr].host_mac,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 length);
417 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 break;
David Brownell7e27f182006-06-13 09:54:40 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000422 case RNDIS_OID_802_3_CURRENT_ADDRESS:
423 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300424 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300426 memcpy(outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 rndis_per_dev_params [configNr].host_mac,
428 length);
429 retval = 0;
430 }
431 break;
David Brownell7e27f182006-06-13 09:54:40 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000434 case RNDIS_OID_802_3_MULTICAST_LIST:
435 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300437 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 retval = 0;
439 break;
David Brownell7e27f182006-06-13 09:54:40 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000442 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
443 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300445 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 retval = 0;
447 break;
David Brownell7e27f182006-06-13 09:54:40 -0700448
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000449 case RNDIS_OID_802_3_MAC_OPTIONS:
450 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700451 *outbuf = cpu_to_le32(0);
452 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454
455 /* ieee802.3 statistics OIDs (table 4-4) */
456
457 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000458 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
459 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700460 if (stats) {
461 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 retval = 0;
463 }
464 break;
David Brownell7e27f182006-06-13 09:54:40 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000467 case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
468 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300469 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 retval = 0;
471 break;
David Brownell7e27f182006-06-13 09:54:40 -0700472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000474 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
475 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300476 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 retval = 0;
478 break;
David Brownell7e27f182006-06-13 09:54:40 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 default:
David Brownell00274922007-11-19 12:58:36 -0800481 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800482 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
David Brownell340600a2005-04-28 13:45:25 -0700484 if (retval < 0)
485 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700486
Mihai Donțua1df4e42010-09-08 02:54:02 +0300487 resp->InformationBufferLength = cpu_to_le32(length);
488 r->length = length + sizeof(*resp);
489 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return retval;
491}
492
Mihai Donțua1df4e42010-09-08 02:54:02 +0300493static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
494 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300496 rndis_set_cmplt_type *resp;
497 int i, retval = -ENOTSUPP;
498 struct rndis_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (!r)
501 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300502 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!resp)
504 return -ENOMEM;
505
David Brownell340600a2005-04-28 13:45:25 -0700506 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700507 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700508 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700509 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700510 get_unaligned_le32(&buf[i]),
511 get_unaligned_le32(&buf[i + 4]),
512 get_unaligned_le32(&buf[i + 8]),
513 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
Mihai Donțua1df4e42010-09-08 02:54:02 +0300517 params = &rndis_per_dev_params[configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 switch (OID) {
Linus Walleij8cdddc32012-05-11 22:16:08 +0000519 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David Brownell340600a2005-04-28 13:45:25 -0700521 /* these NDIS_PACKET_TYPE_* bitflags are shared with
522 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
524 * PROMISCUOUS, DIRECTED,
525 * MULTICAST, ALL_MULTICAST, BROADCAST
526 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700527 *params->filter = (u16)get_unaligned_le32(buf);
Linus Walleij8cdddc32012-05-11 22:16:08 +0000528 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800529 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* this call has a significant side effect: it's
532 * what makes the packet flow start and stop, like
533 * activating the CDC Ethernet altsetting.
534 */
David Brownell340600a2005-04-28 13:45:25 -0700535 retval = 0;
536 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 params->state = RNDIS_DATA_INITIALIZED;
538 netif_carrier_on(params->dev);
539 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300540 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 } else {
542 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300543 netif_carrier_off(params->dev);
544 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 break;
David Brownell7e27f182006-06-13 09:54:40 -0700547
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000548 case RNDIS_OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700549 /* I think we can ignore this */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000550 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 retval = 0;
552 break;
David Brownell340600a2005-04-28 13:45:25 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 default:
David Brownell00274922007-11-19 12:58:36 -0800555 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800556 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
David Brownell7e27f182006-06-13 09:54:40 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return retval;
560}
561
David Brownell7e27f182006-06-13 09:54:40 -0700562/*
563 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
565
Mihai Donțua1df4e42010-09-08 02:54:02 +0300566static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300568 rndis_init_cmplt_type *resp;
569 rndis_resp_t *r;
570 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700571
David Brownell15b2d2b2008-06-19 18:19:16 -0700572 if (!params->dev)
573 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700574
Mihai Donțua1df4e42010-09-08 02:54:02 +0300575 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700576 if (!r)
577 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300578 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700579
Linus Walleij51491162012-05-11 22:17:07 +0000580 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300581 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300583 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
584 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
585 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
586 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
587 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
588 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
589 resp->MaxTransferSize = cpu_to_le32(
David Brownell15b2d2b2008-06-19 18:19:16 -0700590 params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300591 + sizeof(struct ethhdr)
592 + sizeof(struct rndis_packet_msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 + 22);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300594 resp->PacketAlignmentFactor = cpu_to_le32(0);
595 resp->AFListOffset = cpu_to_le32(0);
596 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700597
David Brownell15b2d2b2008-06-19 18:19:16 -0700598 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600}
601
Mihai Donțua1df4e42010-09-08 02:54:02 +0300602static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300605 rndis_resp_t *r;
606 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700607
David Brownell33376c12008-08-18 17:45:07 -0700608 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700609 if (!params->dev)
610 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700611
Shaun Tancheff87637162006-02-22 19:47:19 -0800612 /*
613 * we need more memory:
614 * gen_ndis_query_resp expects enough space for
615 * rndis_query_cmplt_type followed by data.
616 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300618 r = rndis_add_response(configNr,
619 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700620 if (!r)
621 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300622 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700623
Linus Walleij51491162012-05-11 22:17:07 +0000624 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700626
Mihai Donțua1df4e42010-09-08 02:54:02 +0300627 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700628 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300629 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700630 le32_to_cpu(buf->InformationBufferLength),
631 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300633 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
634 resp->MessageLength = cpu_to_le32(sizeof *resp);
635 resp->InformationBufferLength = cpu_to_le32(0);
636 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300638 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700639
David Brownell15b2d2b2008-06-19 18:19:16 -0700640 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return 0;
642}
643
Mihai Donțua1df4e42010-09-08 02:54:02 +0300644static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300646 u32 BufLength, BufOffset;
647 rndis_set_cmplt_type *resp;
648 rndis_resp_t *r;
649 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700650
Mihai Donțua1df4e42010-09-08 02:54:02 +0300651 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700652 if (!r)
653 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300654 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Mihai Donțua1df4e42010-09-08 02:54:02 +0300656 BufLength = le32_to_cpu(buf->InformationBufferLength);
657 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
David Brownell15b2d2b2008-06-19 18:19:16 -0700659#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700660 pr_debug("%s: Length: %d\n", __func__, BufLength);
661 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
662 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700665 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
David Brownell7e27f182006-06-13 09:54:40 -0700667
David Brownell33376c12008-08-18 17:45:07 -0700668 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669#endif
David Brownell7e27f182006-06-13 09:54:40 -0700670
Linus Walleij51491162012-05-11 22:17:07 +0000671 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300672 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300674 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
675 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
676 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700677 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300678 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700679
David Brownell15b2d2b2008-06-19 18:19:16 -0700680 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682}
683
Mihai Donțua1df4e42010-09-08 02:54:02 +0300684static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300686 rndis_reset_cmplt_type *resp;
687 rndis_resp_t *r;
688 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700689
Mihai Donțua1df4e42010-09-08 02:54:02 +0300690 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700691 if (!r)
692 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300693 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700694
Linus Walleij51491162012-05-11 22:17:07 +0000695 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300696 resp->MessageLength = cpu_to_le32(16);
697 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300699 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700700
David Brownell15b2d2b2008-06-19 18:19:16 -0700701 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
703}
704
Mihai Donțua1df4e42010-09-08 02:54:02 +0300705static int rndis_keepalive_response(int configNr,
706 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300708 rndis_keepalive_cmplt_type *resp;
709 rndis_resp_t *r;
710 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
713
Mihai Donțua1df4e42010-09-08 02:54:02 +0300714 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700715 if (!r)
716 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300717 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700718
Linus Walleij51491162012-05-11 22:17:07 +0000719 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300720 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300722 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700723
David Brownell15b2d2b2008-06-19 18:19:16 -0700724 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
728
David Brownell7e27f182006-06-13 09:54:40 -0700729/*
730 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300732static int rndis_indicate_status_msg(int configNr, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300734 rndis_indicate_status_msg_type *resp;
735 rndis_resp_t *r;
736 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700737
David Brownell15b2d2b2008-06-19 18:19:16 -0700738 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700739 return -ENOTSUPP;
740
Mihai Donțua1df4e42010-09-08 02:54:02 +0300741 r = rndis_add_response(configNr,
742 sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700743 if (!r)
744 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300745 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700746
Linus Walleij51491162012-05-11 22:17:07 +0000747 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300748 resp->MessageLength = cpu_to_le32(20);
749 resp->Status = cpu_to_le32(status);
750 resp->StatusBufferLength = cpu_to_le32(0);
751 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700752
David Brownell15b2d2b2008-06-19 18:19:16 -0700753 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755}
756
Mihai Donțua1df4e42010-09-08 02:54:02 +0300757int rndis_signal_connect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300759 rndis_per_dev_params[configNr].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +0000760 = RNDIS_MEDIA_STATE_CONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300761 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 RNDIS_STATUS_MEDIA_CONNECT);
763}
764
Mihai Donțua1df4e42010-09-08 02:54:02 +0300765int rndis_signal_disconnect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300767 rndis_per_dev_params[configNr].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +0000768 = RNDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300769 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 RNDIS_STATUS_MEDIA_DISCONNECT);
771}
772
Mihai Donțua1df4e42010-09-08 02:54:02 +0300773void rndis_uninit(int configNr)
David Brownell340600a2005-04-28 13:45:25 -0700774{
David Brownell486e2df2005-05-24 17:51:52 -0700775 u8 *buf;
776 u32 length;
777
David Brownell340600a2005-04-28 13:45:25 -0700778 if (configNr >= RNDIS_MAX_CONFIGS)
779 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300780 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700781
782 /* drain the response queue */
783 while ((buf = rndis_get_next_response(configNr, &length)))
784 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700785}
786
Mihai Donțua1df4e42010-09-08 02:54:02 +0300787void rndis_set_host_mac(int configNr, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300789 rndis_per_dev_params[configNr].host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
David Brownell7e27f182006-06-13 09:54:40 -0700792/*
793 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300795int rndis_msg_parser(u8 configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 u32 MsgType, MsgLength;
798 __le32 *tmp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300799 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!buf)
802 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700803
Mihai Donțua1df4e42010-09-08 02:54:02 +0300804 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700805 MsgType = get_unaligned_le32(tmp++);
806 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (configNr >= RNDIS_MAX_CONFIGS)
809 return -ENOTSUPP;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300810 params = &rndis_per_dev_params[configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700811
David Brownell340600a2005-04-28 13:45:25 -0700812 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
813 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
814 * and normal HC level polling to see if there's any IN traffic.
815 */
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700818 switch (MsgType) {
Linus Walleij51491162012-05-11 22:17:07 +0000819 case RNDIS_MSG_INIT:
820 pr_debug("%s: RNDIS_MSG_INIT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300821 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300823 return rndis_init_response(configNr,
824 (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700825
Linus Walleij51491162012-05-11 22:17:07 +0000826 case RNDIS_MSG_HALT:
827 pr_debug("%s: RNDIS_MSG_HALT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300828 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 params->state = RNDIS_UNINITIALIZED;
830 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300831 netif_carrier_off(params->dev);
832 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700835
Linus Walleij51491162012-05-11 22:17:07 +0000836 case RNDIS_MSG_QUERY:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300837 return rndis_query_response(configNr,
838 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700839
Linus Walleij51491162012-05-11 22:17:07 +0000840 case RNDIS_MSG_SET:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300841 return rndis_set_response(configNr,
842 (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700843
Linus Walleij51491162012-05-11 22:17:07 +0000844 case RNDIS_MSG_RESET:
845 pr_debug("%s: RNDIS_MSG_RESET\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300846 __func__);
847 return rndis_reset_response(configNr,
848 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Walleij51491162012-05-11 22:17:07 +0000850 case RNDIS_MSG_KEEPALIVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700852 if (rndis_debug > 1)
Linus Walleij51491162012-05-11 22:17:07 +0000853 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300854 __func__);
855 return rndis_keepalive_response(configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700856 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700858
859 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* At least Windows XP emits some undefined RNDIS messages.
861 * In one case those messages seemed to relate to the host
862 * suspending itself.
863 */
David Brownell00274922007-11-19 12:58:36 -0800864 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300865 __func__, MsgType, MsgLength);
Andy Shevchenkod3091cf2012-08-07 19:07:58 +0300866 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
867 buf, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 break;
869 }
David Brownell7e27f182006-06-13 09:54:40 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return -ENOTSUPP;
872}
873
David Brownell15b2d2b2008-06-19 18:19:16 -0700874int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700877
David Brownell15b2d2b2008-06-19 18:19:16 -0700878 if (!resp_avail)
879 return -EINVAL;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300882 if (!rndis_per_dev_params[i].used) {
883 rndis_per_dev_params[i].used = 1;
884 rndis_per_dev_params[i].resp_avail = resp_avail;
885 rndis_per_dev_params[i].v = v;
David Brownell33376c12008-08-18 17:45:07 -0700886 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return i;
888 }
889 }
David Brownell33376c12008-08-18 17:45:07 -0700890 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700891
David Brownell15b2d2b2008-06-19 18:19:16 -0700892 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Mihai Donțua1df4e42010-09-08 02:54:02 +0300895void rndis_deregister(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300897 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (configNr >= RNDIS_MAX_CONFIGS) return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300900 rndis_per_dev_params[configNr].used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
David Brownell15b2d2b2008-06-19 18:19:16 -0700903int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
David Brownell33376c12008-08-18 17:45:07 -0700905 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700906 if (!dev)
907 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700909
Mihai Donțua1df4e42010-09-08 02:54:02 +0300910 rndis_per_dev_params[configNr].dev = dev;
911 rndis_per_dev_params[configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return 0;
914}
915
Mihai Donțua1df4e42010-09-08 02:54:02 +0300916int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
David Brownell33376c12008-08-18 17:45:07 -0700918 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (!vendorDescr) return -1;
920 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700921
Mihai Donțua1df4e42010-09-08 02:54:02 +0300922 rndis_per_dev_params[configNr].vendorID = vendorID;
923 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
927
Mihai Donțua1df4e42010-09-08 02:54:02 +0300928int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
David Brownell33376c12008-08-18 17:45:07 -0700930 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700932
Mihai Donțua1df4e42010-09-08 02:54:02 +0300933 rndis_per_dev_params[configNr].medium = medium;
934 rndis_per_dev_params[configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
Mihai Donțua1df4e42010-09-08 02:54:02 +0300939void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300941 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (!skb)
944 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300945 header = (void *)skb_push(skb, sizeof(*header));
946 memset(header, 0, sizeof *header);
Linus Walleij51491162012-05-11 22:17:07 +0000947 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300949 header->DataOffset = cpu_to_le32(36);
950 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Mihai Donțua1df4e42010-09-08 02:54:02 +0300953void rndis_free_response(int configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300955 rndis_resp_t *r;
956 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700957
Mihai Donțua1df4e42010-09-08 02:54:02 +0300958 list_for_each_safe(act, tmp,
959 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300961 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (r && r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300963 list_del(&r->list);
964 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 }
967}
968
Mihai Donțua1df4e42010-09-08 02:54:02 +0300969u8 *rndis_get_next_response(int configNr, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300971 rndis_resp_t *r;
972 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700975
Mihai Donțua1df4e42010-09-08 02:54:02 +0300976 list_for_each_safe(act, tmp,
977 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300979 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (!r->send) {
981 r->send = 1;
982 *length = r->length;
983 return r->buf;
984 }
985 }
David Brownell7e27f182006-06-13 09:54:40 -0700986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return NULL;
988}
989
Mihai Donțua1df4e42010-09-08 02:54:02 +0300990static rndis_resp_t *rndis_add_response(int configNr, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300992 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700993
Mihai Donțua1df4e42010-09-08 02:54:02 +0300994 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
995 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700997
Mihai Donțua1df4e42010-09-08 02:54:02 +0300998 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 r->length = length;
1000 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001001
Mihai Donțua1df4e42010-09-08 02:54:02 +03001002 list_add_tail(&r->list,
1003 &(rndis_per_dev_params[configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return r;
1005}
1006
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001007int rndis_rm_hdr(struct gether *port,
1008 struct sk_buff *skb,
1009 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
David Brownell6cdee102005-04-18 17:39:34 -07001011 /* tmp points to a struct rndis_packet_msg_type */
Mihai Donțua1df4e42010-09-08 02:54:02 +03001012 __le32 *tmp = (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David Brownell6cdee102005-04-18 17:39:34 -07001014 /* MessageType, MessageLength */
Linus Walleij51491162012-05-11 22:17:07 +00001015 if (cpu_to_le32(RNDIS_MSG_PACKET)
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001016 != get_unaligned(tmp++)) {
1017 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001018 return -EINVAL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001019 }
David Brownell6cdee102005-04-18 17:39:34 -07001020 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
David Brownell6cdee102005-04-18 17:39:34 -07001022 /* DataOffset, DataLength */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001023 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1024 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001025 return -EOVERFLOW;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001026 }
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001027 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001028
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001029 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 0;
1031}
1032
Mihai Donțua1df4e42010-09-08 02:54:02 +03001033#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001035static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001037 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001038
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001039 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 "Config Nr. %d\n"
1041 "used : %s\n"
1042 "state : %s\n"
1043 "medium : 0x%08X\n"
1044 "speed : %d\n"
1045 "cable : %s\n"
1046 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001047 "vendor : %s\n",
1048 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ({ char *s = "?";
1050 switch (param->state) {
1051 case RNDIS_UNINITIALIZED:
1052 s = "RNDIS_UNINITIALIZED"; break;
1053 case RNDIS_INITIALIZED:
1054 s = "RNDIS_INITIALIZED"; break;
1055 case RNDIS_DATA_INITIALIZED:
1056 s = "RNDIS_DATA_INITIALIZED"; break;
1057 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001058 param->medium,
1059 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001061 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001065static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001066 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001068 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 u32 speed = 0;
1070 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 for (i = 0; i < count; i++) {
1073 char c;
1074 if (get_user(c, buffer))
1075 return -EFAULT;
1076 switch (c) {
1077 case '0':
1078 case '1':
1079 case '2':
1080 case '3':
1081 case '4':
1082 case '5':
1083 case '6':
1084 case '7':
1085 case '8':
1086 case '9':
1087 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001088 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 break;
1090 case 'C':
1091 case 'c':
Mihai Donțua1df4e42010-09-08 02:54:02 +03001092 rndis_signal_connect(p->confignr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 break;
1094 case 'D':
1095 case 'd':
1096 rndis_signal_disconnect(p->confignr);
1097 break;
David Brownell7e27f182006-06-13 09:54:40 -07001098 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001100 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
1102 }
David Brownell7e27f182006-06-13 09:54:40 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 buffer++;
1105 }
David Brownell7e27f182006-06-13 09:54:40 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return count;
1108}
1109
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001110static int rndis_proc_open(struct inode *inode, struct file *file)
1111{
1112 return single_open(file, rndis_proc_show, PDE(inode)->data);
1113}
1114
1115static const struct file_operations rndis_proc_fops = {
1116 .owner = THIS_MODULE,
1117 .open = rndis_proc_open,
1118 .read = seq_read,
1119 .llseek = seq_lseek,
1120 .release = single_release,
1121 .write = rndis_proc_write,
1122};
1123
Mihai Donțua1df4e42010-09-08 02:54:02 +03001124#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1127
Mihai Donțua1df4e42010-09-08 02:54:02 +03001128#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130
Henrik Kretzschmar793f03a2010-08-20 19:57:50 +02001131int rndis_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 u8 i;
1134
1135 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1136#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1137 char name [20];
1138
Mihai Donțua1df4e42010-09-08 02:54:02 +03001139 sprintf(name, NAME_TEMPLATE, i);
1140 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001141 &rndis_proc_fops,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001142 (void *)(rndis_per_dev_params + i));
1143 if (!rndis_connect_state[i]) {
1144 pr_debug("%s: remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 while (i) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001146 sprintf(name, NAME_TEMPLATE, --i);
1147 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
David Brownell33376c12008-08-18 17:45:07 -07001149 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EIO;
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152#endif
Mihai Donțua1df4e42010-09-08 02:54:02 +03001153 rndis_per_dev_params[i].confignr = i;
1154 rndis_per_dev_params[i].used = 0;
1155 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1156 rndis_per_dev_params[i].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +00001157 = RNDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001158 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
David Brownell7e27f182006-06-13 09:54:40 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162}
1163
Mihai Donțua1df4e42010-09-08 02:54:02 +03001164void rndis_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001166#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 u8 i;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001168 char name[20];
David Brownell7e27f182006-06-13 09:54:40 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001171 sprintf(name, NAME_TEMPLATE, i);
1172 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174#endif
1175}