blob: d3cdffea9c8a33e71dd8fb5bacaf41c7aaf240cd [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>
37#include <asm/system.h>
David Brownell6cdee102005-04-18 17:39:34 -070038#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
David Brownell15b2d2b2008-06-19 18:19:16 -070041#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "rndis.h"
44
45
46/* The driver for your USB chip needs to support ep0 OUT to work with
47 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
48 *
49 * Windows hosts need an INF file like Documentation/usb/linux.inf
50 * and will be happier if you provide the host_addr module parameter.
51 */
52
53#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070055module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
61#define RNDIS_MAX_CONFIGS 1
62
63
Mihai Donțua1df4e42010-09-08 02:54:02 +030064static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030067static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* Function Prototypes */
Mihai Donțua1df4e42010-09-08 02:54:02 +030070static rndis_resp_t *rndis_add_response(int configNr, u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72
David Brownell340600a2005-04-28 13:45:25 -070073/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030074static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070075{
76 /* the general stuff */
77 OID_GEN_SUPPORTED_LIST,
78 OID_GEN_HARDWARE_STATUS,
79 OID_GEN_MEDIA_SUPPORTED,
80 OID_GEN_MEDIA_IN_USE,
81 OID_GEN_MAXIMUM_FRAME_SIZE,
82 OID_GEN_LINK_SPEED,
83 OID_GEN_TRANSMIT_BLOCK_SIZE,
84 OID_GEN_RECEIVE_BLOCK_SIZE,
85 OID_GEN_VENDOR_ID,
86 OID_GEN_VENDOR_DESCRIPTION,
87 OID_GEN_VENDOR_DRIVER_VERSION,
88 OID_GEN_CURRENT_PACKET_FILTER,
89 OID_GEN_MAXIMUM_TOTAL_SIZE,
90 OID_GEN_MEDIA_CONNECT_STATUS,
91 OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070092
David Brownell340600a2005-04-28 13:45:25 -070093 /* the statistical stuff */
94 OID_GEN_XMIT_OK,
95 OID_GEN_RCV_OK,
96 OID_GEN_XMIT_ERROR,
97 OID_GEN_RCV_ERROR,
98 OID_GEN_RCV_NO_BUFFER,
99#ifdef RNDIS_OPTIONAL_STATS
100 OID_GEN_DIRECTED_BYTES_XMIT,
101 OID_GEN_DIRECTED_FRAMES_XMIT,
102 OID_GEN_MULTICAST_BYTES_XMIT,
103 OID_GEN_MULTICAST_FRAMES_XMIT,
104 OID_GEN_BROADCAST_BYTES_XMIT,
105 OID_GEN_BROADCAST_FRAMES_XMIT,
106 OID_GEN_DIRECTED_BYTES_RCV,
107 OID_GEN_DIRECTED_FRAMES_RCV,
108 OID_GEN_MULTICAST_BYTES_RCV,
109 OID_GEN_MULTICAST_FRAMES_RCV,
110 OID_GEN_BROADCAST_BYTES_RCV,
111 OID_GEN_BROADCAST_FRAMES_RCV,
112 OID_GEN_RCV_CRC_ERROR,
113 OID_GEN_TRANSMIT_QUEUE_LENGTH,
114#endif /* RNDIS_OPTIONAL_STATS */
115
David Brownell7e27f182006-06-13 09:54:40 -0700116 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700117 /* the general stuff */
118 OID_802_3_PERMANENT_ADDRESS,
119 OID_802_3_CURRENT_ADDRESS,
120 OID_802_3_MULTICAST_LIST,
121 OID_802_3_MAC_OPTIONS,
122 OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700123
David Brownell340600a2005-04-28 13:45:25 -0700124 /* the statistical stuff */
125 OID_802_3_RCV_ERROR_ALIGNMENT,
126 OID_802_3_XMIT_ONE_COLLISION,
127 OID_802_3_XMIT_MORE_COLLISIONS,
128#ifdef RNDIS_OPTIONAL_STATS
129 OID_802_3_XMIT_DEFERRED,
130 OID_802_3_XMIT_MAX_COLLISIONS,
131 OID_802_3_RCV_OVERRUN,
132 OID_802_3_XMIT_UNDERRUN,
133 OID_802_3_XMIT_HEARTBEAT_FAILURE,
134 OID_802_3_XMIT_TIMES_CRS_LOST,
135 OID_802_3_XMIT_LATE_COLLISIONS,
136#endif /* RNDIS_OPTIONAL_STATS */
137
138#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700139 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
140 * don't say what they mean ... and the NDIS specs are often
141 * confusing and/or ambiguous in this context. (That is, more
142 * so than their specs for the other OIDs.)
143 *
144 * FIXME someone who knows what these should do, please
145 * implement them!
146 */
David Brownell340600a2005-04-28 13:45:25 -0700147
148 /* power management */
149 OID_PNP_CAPABILITIES,
150 OID_PNP_QUERY_POWER,
151 OID_PNP_SET_POWER,
152
153#ifdef RNDIS_WAKEUP
154 /* wake up host */
155 OID_PNP_ENABLE_WAKE_UP,
156 OID_PNP_ADD_WAKE_UP_PATTERN,
157 OID_PNP_REMOVE_WAKE_UP_PATTERN,
158#endif /* RNDIS_WAKEUP */
159#endif /* RNDIS_PM */
160};
161
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* NDIS Functions */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300164static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
165 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300167 int retval = -ENOTSUPP;
168 u32 length = 4; /* usually */
169 __le32 *outbuf;
170 int i, count;
171 rndis_query_cmplt_type *resp;
172 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700173 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700174 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300177 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700180
181 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700182 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700183 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700184 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700185 get_unaligned_le32(&buf[i]),
186 get_unaligned_le32(&buf[i + 4]),
187 get_unaligned_le32(&buf[i + 8]),
188 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700189 }
190 }
191
192 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300193 outbuf = (__le32 *)&resp[1];
194 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700195
David Brownell15b2d2b2008-06-19 18:19:16 -0700196 net = rndis_per_dev_params[configNr].dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700197 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 switch (OID) {
200
201 /* general oids (table 4-1) */
202
203 /* mandatory */
204 case OID_GEN_SUPPORTED_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700205 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300206 length = sizeof(oid_supported_list);
207 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300209 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 retval = 0;
211 break;
David Brownell7e27f182006-06-13 09:54:40 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* mandatory */
214 case OID_GEN_HARDWARE_STATUS:
David Brownell33376c12008-08-18 17:45:07 -0700215 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700216 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700218 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * reddite ergo quae sunt Caesaris Caesari
220 * et quae sunt Dei Deo!
221 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300222 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 retval = 0;
224 break;
David Brownell7e27f182006-06-13 09:54:40 -0700225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* mandatory */
227 case OID_GEN_MEDIA_SUPPORTED:
David Brownell33376c12008-08-18 17:45:07 -0700228 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300229 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 retval = 0;
231 break;
David Brownell7e27f182006-06-13 09:54:40 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* mandatory */
234 case OID_GEN_MEDIA_IN_USE:
David Brownell33376c12008-08-18 17:45:07 -0700235 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* one medium, one transport... (maybe you do it better) */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300237 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 retval = 0;
239 break;
David Brownell7e27f182006-06-13 09:54:40 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* mandatory */
242 case OID_GEN_MAXIMUM_FRAME_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700243 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300244 if (rndis_per_dev_params[configNr].dev) {
245 *outbuf = cpu_to_le32(
246 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 break;
David Brownell7e27f182006-06-13 09:54:40 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* mandatory */
252 case OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700253 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700254 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300255 if (rndis_per_dev_params[configNr].media_state
David Brownell340600a2005-04-28 13:45:25 -0700256 == NDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300257 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300259 *outbuf = cpu_to_le32(
260 rndis_per_dev_params[configNr].speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 retval = 0;
262 break;
263
264 /* mandatory */
265 case OID_GEN_TRANSMIT_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700266 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300267 if (rndis_per_dev_params[configNr].dev) {
268 *outbuf = cpu_to_le32(
269 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 retval = 0;
271 }
272 break;
David Brownell7e27f182006-06-13 09:54:40 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* mandatory */
275 case OID_GEN_RECEIVE_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700276 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300277 if (rndis_per_dev_params[configNr].dev) {
278 *outbuf = cpu_to_le32(
279 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 retval = 0;
281 }
282 break;
David Brownell7e27f182006-06-13 09:54:40 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* mandatory */
285 case OID_GEN_VENDOR_ID:
David Brownell33376c12008-08-18 17:45:07 -0700286 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300287 *outbuf = cpu_to_le32(
288 rndis_per_dev_params[configNr].vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 retval = 0;
290 break;
David Brownell7e27f182006-06-13 09:54:40 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* mandatory */
293 case OID_GEN_VENDOR_DESCRIPTION:
David Brownell33376c12008-08-18 17:45:07 -0700294 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300295 if (rndis_per_dev_params[configNr].vendorDescr) {
296 length = strlen(rndis_per_dev_params[configNr].
297 vendorDescr);
298 memcpy(outbuf,
299 rndis_per_dev_params[configNr].vendorDescr,
300 length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400301 } else {
302 outbuf[0] = 0;
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 retval = 0;
305 break;
306
307 case OID_GEN_VENDOR_DRIVER_VERSION:
David Brownell33376c12008-08-18 17:45:07 -0700308 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700310 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 retval = 0;
312 break;
313
314 /* mandatory */
315 case OID_GEN_CURRENT_PACKET_FILTER:
David Brownell33376c12008-08-18 17:45:07 -0700316 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300317 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 retval = 0;
319 break;
320
321 /* mandatory */
322 case OID_GEN_MAXIMUM_TOTAL_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700323 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800324 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 retval = 0;
326 break;
327
328 /* mandatory */
329 case OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700330 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700331 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300332 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .media_state);
334 retval = 0;
335 break;
336
337 case OID_GEN_PHYSICAL_MEDIUM:
David Brownell33376c12008-08-18 17:45:07 -0700338 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300339 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 retval = 0;
341 break;
342
343 /* The RNDIS specification is incomplete/wrong. Some versions
344 * of MS-Windows expect OIDs that aren't specified there. Other
345 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
346 */
347 case OID_GEN_MAC_OPTIONS: /* from WinME */
David Brownell33376c12008-08-18 17:45:07 -0700348 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800349 *outbuf = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
351 | NDIS_MAC_OPTION_FULL_DUPLEX);
352 retval = 0;
353 break;
354
355 /* statistics OIDs (table 4-2) */
356
357 /* mandatory */
358 case OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700359 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700360 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700361 if (stats) {
362 *outbuf = cpu_to_le32(stats->tx_packets
363 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 break;
367
368 /* mandatory */
369 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700370 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700371 pr_debug("%s: OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700372 if (stats) {
373 *outbuf = cpu_to_le32(stats->rx_packets
374 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 break;
David Brownell7e27f182006-06-13 09:54:40 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* mandatory */
380 case OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700381 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700382 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700383 if (stats) {
384 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 break;
David Brownell7e27f182006-06-13 09:54:40 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* mandatory */
390 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700391 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700392 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700393 if (stats) {
394 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 break;
David Brownell7e27f182006-06-13 09:54:40 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* mandatory */
400 case OID_GEN_RCV_NO_BUFFER:
David Brownell33376c12008-08-18 17:45:07 -0700401 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700402 if (stats) {
403 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 break;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* ieee802.3 OIDs (table 4-3) */
409
410 /* mandatory */
411 case OID_802_3_PERMANENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700412 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300413 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300415 memcpy(outbuf,
416 rndis_per_dev_params[configNr].host_mac,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 length);
418 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 break;
David Brownell7e27f182006-06-13 09:54:40 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* mandatory */
423 case OID_802_3_CURRENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700424 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300425 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300427 memcpy(outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 rndis_per_dev_params [configNr].host_mac,
429 length);
430 retval = 0;
431 }
432 break;
David Brownell7e27f182006-06-13 09:54:40 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* mandatory */
435 case OID_802_3_MULTICAST_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700436 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300438 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 retval = 0;
440 break;
David Brownell7e27f182006-06-13 09:54:40 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* mandatory */
443 case OID_802_3_MAXIMUM_LIST_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700444 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300446 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 retval = 0;
448 break;
David Brownell7e27f182006-06-13 09:54:40 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case OID_802_3_MAC_OPTIONS:
David Brownell33376c12008-08-18 17:45:07 -0700451 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700452 *outbuf = cpu_to_le32(0);
453 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455
456 /* ieee802.3 statistics OIDs (table 4-4) */
457
458 /* mandatory */
459 case OID_802_3_RCV_ERROR_ALIGNMENT:
David Brownell33376c12008-08-18 17:45:07 -0700460 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700461 if (stats) {
462 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 retval = 0;
464 }
465 break;
David Brownell7e27f182006-06-13 09:54:40 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* mandatory */
468 case OID_802_3_XMIT_ONE_COLLISION:
David Brownell33376c12008-08-18 17:45:07 -0700469 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300470 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 retval = 0;
472 break;
David Brownell7e27f182006-06-13 09:54:40 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* mandatory */
475 case OID_802_3_XMIT_MORE_COLLISIONS:
David Brownell33376c12008-08-18 17:45:07 -0700476 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300477 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 retval = 0;
479 break;
David Brownell7e27f182006-06-13 09:54:40 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 default:
David Brownell00274922007-11-19 12:58:36 -0800482 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800483 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
David Brownell340600a2005-04-28 13:45:25 -0700485 if (retval < 0)
486 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700487
Mihai Donțua1df4e42010-09-08 02:54:02 +0300488 resp->InformationBufferLength = cpu_to_le32(length);
489 r->length = length + sizeof(*resp);
490 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return retval;
492}
493
Mihai Donțua1df4e42010-09-08 02:54:02 +0300494static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
495 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300497 rndis_set_cmplt_type *resp;
498 int i, retval = -ENOTSUPP;
499 struct rndis_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (!r)
502 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300503 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!resp)
505 return -ENOMEM;
506
David Brownell340600a2005-04-28 13:45:25 -0700507 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700508 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700509 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700510 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700511 get_unaligned_le32(&buf[i]),
512 get_unaligned_le32(&buf[i + 4]),
513 get_unaligned_le32(&buf[i + 8]),
514 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
Mihai Donțua1df4e42010-09-08 02:54:02 +0300518 params = &rndis_per_dev_params[configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 switch (OID) {
520 case OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David Brownell340600a2005-04-28 13:45:25 -0700522 /* these NDIS_PACKET_TYPE_* bitflags are shared with
523 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
525 * PROMISCUOUS, DIRECTED,
526 * MULTICAST, ALL_MULTICAST, BROADCAST
527 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700528 *params->filter = (u16)get_unaligned_le32(buf);
David Brownell33376c12008-08-18 17:45:07 -0700529 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800530 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* this call has a significant side effect: it's
533 * what makes the packet flow start and stop, like
534 * activating the CDC Ethernet altsetting.
535 */
David Brownell340600a2005-04-28 13:45:25 -0700536 retval = 0;
537 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 params->state = RNDIS_DATA_INITIALIZED;
539 netif_carrier_on(params->dev);
540 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300541 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
543 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300544 netif_carrier_off(params->dev);
545 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 break;
David Brownell7e27f182006-06-13 09:54:40 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 case OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700550 /* I think we can ignore this */
David Brownell33376c12008-08-18 17:45:07 -0700551 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 retval = 0;
553 break;
David Brownell340600a2005-04-28 13:45:25 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 default:
David Brownell00274922007-11-19 12:58:36 -0800556 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800557 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
David Brownell7e27f182006-06-13 09:54:40 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return retval;
561}
562
David Brownell7e27f182006-06-13 09:54:40 -0700563/*
564 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566
Mihai Donțua1df4e42010-09-08 02:54:02 +0300567static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300569 rndis_init_cmplt_type *resp;
570 rndis_resp_t *r;
571 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700572
David Brownell15b2d2b2008-06-19 18:19:16 -0700573 if (!params->dev)
574 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700575
Mihai Donțua1df4e42010-09-08 02:54:02 +0300576 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700577 if (!r)
578 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300579 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700580
Mihai Donțua1df4e42010-09-08 02:54:02 +0300581 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INITIALIZE_CMPLT);
582 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300584 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
585 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
586 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
587 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
588 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
589 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
590 resp->MaxTransferSize = cpu_to_le32(
David Brownell15b2d2b2008-06-19 18:19:16 -0700591 params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300592 + sizeof(struct ethhdr)
593 + sizeof(struct rndis_packet_msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 + 22);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300595 resp->PacketAlignmentFactor = cpu_to_le32(0);
596 resp->AFListOffset = cpu_to_le32(0);
597 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700598
David Brownell15b2d2b2008-06-19 18:19:16 -0700599 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return 0;
601}
602
Mihai Donțua1df4e42010-09-08 02:54:02 +0300603static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300606 rndis_resp_t *r;
607 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700608
David Brownell33376c12008-08-18 17:45:07 -0700609 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700610 if (!params->dev)
611 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700612
Shaun Tancheff87637162006-02-22 19:47:19 -0800613 /*
614 * we need more memory:
615 * gen_ndis_query_resp expects enough space for
616 * rndis_query_cmplt_type followed by data.
617 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300619 r = rndis_add_response(configNr,
620 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700621 if (!r)
622 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300623 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700624
Mihai Donțua1df4e42010-09-08 02:54:02 +0300625 resp->MessageType = cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700627
Mihai Donțua1df4e42010-09-08 02:54:02 +0300628 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700629 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300630 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700631 le32_to_cpu(buf->InformationBufferLength),
632 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300634 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
635 resp->MessageLength = cpu_to_le32(sizeof *resp);
636 resp->InformationBufferLength = cpu_to_le32(0);
637 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300639 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700640
David Brownell15b2d2b2008-06-19 18:19:16 -0700641 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
643}
644
Mihai Donțua1df4e42010-09-08 02:54:02 +0300645static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300647 u32 BufLength, BufOffset;
648 rndis_set_cmplt_type *resp;
649 rndis_resp_t *r;
650 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700651
Mihai Donțua1df4e42010-09-08 02:54:02 +0300652 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700653 if (!r)
654 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300655 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Mihai Donțua1df4e42010-09-08 02:54:02 +0300657 BufLength = le32_to_cpu(buf->InformationBufferLength);
658 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
David Brownell15b2d2b2008-06-19 18:19:16 -0700660#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700661 pr_debug("%s: Length: %d\n", __func__, BufLength);
662 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
663 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700666 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
David Brownell7e27f182006-06-13 09:54:40 -0700668
David Brownell33376c12008-08-18 17:45:07 -0700669 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#endif
David Brownell7e27f182006-06-13 09:54:40 -0700671
Mihai Donțua1df4e42010-09-08 02:54:02 +0300672 resp->MessageType = cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
673 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300675 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
676 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
677 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700678 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300679 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700680
David Brownell15b2d2b2008-06-19 18:19:16 -0700681 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683}
684
Mihai Donțua1df4e42010-09-08 02:54:02 +0300685static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300687 rndis_reset_cmplt_type *resp;
688 rndis_resp_t *r;
689 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700690
Mihai Donțua1df4e42010-09-08 02:54:02 +0300691 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700692 if (!r)
693 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300694 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700695
Mihai Donțua1df4e42010-09-08 02:54:02 +0300696 resp->MessageType = cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
697 resp->MessageLength = cpu_to_le32(16);
698 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300700 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700701
David Brownell15b2d2b2008-06-19 18:19:16 -0700702 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704}
705
Mihai Donțua1df4e42010-09-08 02:54:02 +0300706static int rndis_keepalive_response(int configNr,
707 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300709 rndis_keepalive_cmplt_type *resp;
710 rndis_resp_t *r;
711 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
714
Mihai Donțua1df4e42010-09-08 02:54:02 +0300715 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700716 if (!r)
717 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300718 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700719
Mihai Donțua1df4e42010-09-08 02:54:02 +0300720 resp->MessageType = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 REMOTE_NDIS_KEEPALIVE_CMPLT);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300722 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300724 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700725
David Brownell15b2d2b2008-06-19 18:19:16 -0700726 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
728}
729
730
David Brownell7e27f182006-06-13 09:54:40 -0700731/*
732 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300734static int rndis_indicate_status_msg(int configNr, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300736 rndis_indicate_status_msg_type *resp;
737 rndis_resp_t *r;
738 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700739
David Brownell15b2d2b2008-06-19 18:19:16 -0700740 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700741 return -ENOTSUPP;
742
Mihai Donțua1df4e42010-09-08 02:54:02 +0300743 r = rndis_add_response(configNr,
744 sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700745 if (!r)
746 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300747 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700748
Mihai Donțua1df4e42010-09-08 02:54:02 +0300749 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INDICATE_STATUS_MSG);
750 resp->MessageLength = cpu_to_le32(20);
751 resp->Status = cpu_to_le32(status);
752 resp->StatusBufferLength = cpu_to_le32(0);
753 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700754
David Brownell15b2d2b2008-06-19 18:19:16 -0700755 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return 0;
757}
758
Mihai Donțua1df4e42010-09-08 02:54:02 +0300759int rndis_signal_connect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300761 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 = NDIS_MEDIA_STATE_CONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300763 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 RNDIS_STATUS_MEDIA_CONNECT);
765}
766
Mihai Donțua1df4e42010-09-08 02:54:02 +0300767int rndis_signal_disconnect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300769 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300771 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 RNDIS_STATUS_MEDIA_DISCONNECT);
773}
774
Mihai Donțua1df4e42010-09-08 02:54:02 +0300775void rndis_uninit(int configNr)
David Brownell340600a2005-04-28 13:45:25 -0700776{
David Brownell486e2df2005-05-24 17:51:52 -0700777 u8 *buf;
778 u32 length;
779
David Brownell340600a2005-04-28 13:45:25 -0700780 if (configNr >= RNDIS_MAX_CONFIGS)
781 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300782 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700783
784 /* drain the response queue */
785 while ((buf = rndis_get_next_response(configNr, &length)))
786 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700787}
788
Mihai Donțua1df4e42010-09-08 02:54:02 +0300789void rndis_set_host_mac(int configNr, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300791 rndis_per_dev_params[configNr].host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
David Brownell7e27f182006-06-13 09:54:40 -0700794/*
795 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300797int rndis_msg_parser(u8 configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 u32 MsgType, MsgLength;
800 __le32 *tmp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300801 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!buf)
804 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700805
Mihai Donțua1df4e42010-09-08 02:54:02 +0300806 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700807 MsgType = get_unaligned_le32(tmp++);
808 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (configNr >= RNDIS_MAX_CONFIGS)
811 return -ENOTSUPP;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300812 params = &rndis_per_dev_params[configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700813
David Brownell340600a2005-04-28 13:45:25 -0700814 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
815 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
816 * and normal HC level polling to see if there's any IN traffic.
817 */
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700820 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700822 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300823 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300825 return rndis_init_response(configNr,
826 (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 case REMOTE_NDIS_HALT_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700829 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300830 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 params->state = RNDIS_UNINITIALIZED;
832 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300833 netif_carrier_off(params->dev);
834 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 case REMOTE_NDIS_QUERY_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300839 return rndis_query_response(configNr,
840 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 case REMOTE_NDIS_SET_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300843 return rndis_set_response(configNr,
844 (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 case REMOTE_NDIS_RESET_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700847 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300848 __func__);
849 return rndis_reset_response(configNr,
850 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 case REMOTE_NDIS_KEEPALIVE_MSG:
853 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700854 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700855 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300856 __func__);
857 return rndis_keepalive_response(configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700858 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700860
861 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* At least Windows XP emits some undefined RNDIS messages.
863 * In one case those messages seemed to relate to the host
864 * suspending itself.
865 */
David Brownell00274922007-11-19 12:58:36 -0800866 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300867 __func__, MsgType, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 {
869 unsigned i;
870 for (i = 0; i < MsgLength; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700871 pr_debug("%03d: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 " %02x %02x %02x %02x"
873 " %02x %02x %02x %02x"
874 " %02x %02x %02x %02x"
875 " %02x %02x %02x %02x"
876 "\n",
877 i,
878 buf[i], buf [i+1],
879 buf[i+2], buf[i+3],
880 buf[i+4], buf [i+5],
881 buf[i+6], buf[i+7],
882 buf[i+8], buf [i+9],
883 buf[i+10], buf[i+11],
884 buf[i+12], buf [i+13],
885 buf[i+14], buf[i+15]);
886 }
887 }
888 break;
889 }
David Brownell7e27f182006-06-13 09:54:40 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -ENOTSUPP;
892}
893
David Brownell15b2d2b2008-06-19 18:19:16 -0700894int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700897
David Brownell15b2d2b2008-06-19 18:19:16 -0700898 if (!resp_avail)
899 return -EINVAL;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300902 if (!rndis_per_dev_params[i].used) {
903 rndis_per_dev_params[i].used = 1;
904 rndis_per_dev_params[i].resp_avail = resp_avail;
905 rndis_per_dev_params[i].v = v;
David Brownell33376c12008-08-18 17:45:07 -0700906 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return i;
908 }
909 }
David Brownell33376c12008-08-18 17:45:07 -0700910 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700911
David Brownell15b2d2b2008-06-19 18:19:16 -0700912 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Mihai Donțua1df4e42010-09-08 02:54:02 +0300915void rndis_deregister(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300917 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (configNr >= RNDIS_MAX_CONFIGS) return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300920 rndis_per_dev_params[configNr].used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
David Brownell15b2d2b2008-06-19 18:19:16 -0700923int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
David Brownell33376c12008-08-18 17:45:07 -0700925 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700926 if (!dev)
927 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700929
Mihai Donțua1df4e42010-09-08 02:54:02 +0300930 rndis_per_dev_params[configNr].dev = dev;
931 rndis_per_dev_params[configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
934}
935
Mihai Donțua1df4e42010-09-08 02:54:02 +0300936int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
David Brownell33376c12008-08-18 17:45:07 -0700938 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!vendorDescr) return -1;
940 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700941
Mihai Donțua1df4e42010-09-08 02:54:02 +0300942 rndis_per_dev_params[configNr].vendorID = vendorID;
943 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946}
947
Mihai Donțua1df4e42010-09-08 02:54:02 +0300948int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
David Brownell33376c12008-08-18 17:45:07 -0700950 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700952
Mihai Donțua1df4e42010-09-08 02:54:02 +0300953 rndis_per_dev_params[configNr].medium = medium;
954 rndis_per_dev_params[configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957}
958
Mihai Donțua1df4e42010-09-08 02:54:02 +0300959void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300961 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (!skb)
964 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300965 header = (void *)skb_push(skb, sizeof(*header));
966 memset(header, 0, sizeof *header);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800967 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300969 header->DataOffset = cpu_to_le32(36);
970 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Mihai Donțua1df4e42010-09-08 02:54:02 +0300973void rndis_free_response(int configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300975 rndis_resp_t *r;
976 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700977
Mihai Donțua1df4e42010-09-08 02:54:02 +0300978 list_for_each_safe(act, tmp,
979 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300981 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (r && r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300983 list_del(&r->list);
984 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 }
987}
988
Mihai Donțua1df4e42010-09-08 02:54:02 +0300989u8 *rndis_get_next_response(int configNr, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300991 rndis_resp_t *r;
992 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700995
Mihai Donțua1df4e42010-09-08 02:54:02 +0300996 list_for_each_safe(act, tmp,
997 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300999 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!r->send) {
1001 r->send = 1;
1002 *length = r->length;
1003 return r->buf;
1004 }
1005 }
David Brownell7e27f182006-06-13 09:54:40 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return NULL;
1008}
1009
Mihai Donțua1df4e42010-09-08 02:54:02 +03001010static rndis_resp_t *rndis_add_response(int configNr, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001012 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001013
Mihai Donțua1df4e42010-09-08 02:54:02 +03001014 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1015 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001017
Mihai Donțua1df4e42010-09-08 02:54:02 +03001018 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 r->length = length;
1020 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001021
Mihai Donțua1df4e42010-09-08 02:54:02 +03001022 list_add_tail(&r->list,
1023 &(rndis_per_dev_params[configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return r;
1025}
1026
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001027int rndis_rm_hdr(struct gether *port,
1028 struct sk_buff *skb,
1029 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
David Brownell6cdee102005-04-18 17:39:34 -07001031 /* tmp points to a struct rndis_packet_msg_type */
Mihai Donțua1df4e42010-09-08 02:54:02 +03001032 __le32 *tmp = (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David Brownell6cdee102005-04-18 17:39:34 -07001034 /* MessageType, MessageLength */
Harvey Harrison35c26c22009-02-14 22:56:56 -08001035 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001036 != get_unaligned(tmp++)) {
1037 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001038 return -EINVAL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001039 }
David Brownell6cdee102005-04-18 17:39:34 -07001040 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
David Brownell6cdee102005-04-18 17:39:34 -07001042 /* DataOffset, DataLength */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001043 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1044 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001045 return -EOVERFLOW;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001046 }
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001047 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001048
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001049 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return 0;
1051}
1052
Mihai Donțua1df4e42010-09-08 02:54:02 +03001053#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001055static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001057 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001058
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001059 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 "Config Nr. %d\n"
1061 "used : %s\n"
1062 "state : %s\n"
1063 "medium : 0x%08X\n"
1064 "speed : %d\n"
1065 "cable : %s\n"
1066 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001067 "vendor : %s\n",
1068 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ({ char *s = "?";
1070 switch (param->state) {
1071 case RNDIS_UNINITIALIZED:
1072 s = "RNDIS_UNINITIALIZED"; break;
1073 case RNDIS_INITIALIZED:
1074 s = "RNDIS_INITIALIZED"; break;
1075 case RNDIS_DATA_INITIALIZED:
1076 s = "RNDIS_DATA_INITIALIZED"; break;
1077 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001078 param->medium,
1079 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001081 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001085static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001086 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001088 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 u32 speed = 0;
1090 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 for (i = 0; i < count; i++) {
1093 char c;
1094 if (get_user(c, buffer))
1095 return -EFAULT;
1096 switch (c) {
1097 case '0':
1098 case '1':
1099 case '2':
1100 case '3':
1101 case '4':
1102 case '5':
1103 case '6':
1104 case '7':
1105 case '8':
1106 case '9':
1107 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001108 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 break;
1110 case 'C':
1111 case 'c':
Mihai Donțua1df4e42010-09-08 02:54:02 +03001112 rndis_signal_connect(p->confignr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114 case 'D':
1115 case 'd':
1116 rndis_signal_disconnect(p->confignr);
1117 break;
David Brownell7e27f182006-06-13 09:54:40 -07001118 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001120 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122 }
David Brownell7e27f182006-06-13 09:54:40 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 buffer++;
1125 }
David Brownell7e27f182006-06-13 09:54:40 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return count;
1128}
1129
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001130static int rndis_proc_open(struct inode *inode, struct file *file)
1131{
1132 return single_open(file, rndis_proc_show, PDE(inode)->data);
1133}
1134
1135static const struct file_operations rndis_proc_fops = {
1136 .owner = THIS_MODULE,
1137 .open = rndis_proc_open,
1138 .read = seq_read,
1139 .llseek = seq_lseek,
1140 .release = single_release,
1141 .write = rndis_proc_write,
1142};
1143
Mihai Donțua1df4e42010-09-08 02:54:02 +03001144#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1147
Mihai Donțua1df4e42010-09-08 02:54:02 +03001148#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150
Henrik Kretzschmar793f03a2010-08-20 19:57:50 +02001151int rndis_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 u8 i;
1154
1155 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1156#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1157 char name [20];
1158
Mihai Donțua1df4e42010-09-08 02:54:02 +03001159 sprintf(name, NAME_TEMPLATE, i);
1160 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001161 &rndis_proc_fops,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001162 (void *)(rndis_per_dev_params + i));
1163 if (!rndis_connect_state[i]) {
1164 pr_debug("%s: remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 while (i) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001166 sprintf(name, NAME_TEMPLATE, --i);
1167 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
David Brownell33376c12008-08-18 17:45:07 -07001169 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return -EIO;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#endif
Mihai Donțua1df4e42010-09-08 02:54:02 +03001173 rndis_per_dev_params[i].confignr = i;
1174 rndis_per_dev_params[i].used = 0;
1175 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1176 rndis_per_dev_params[i].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001178 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
David Brownell7e27f182006-06-13 09:54:40 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return 0;
1182}
1183
Mihai Donțua1df4e42010-09-08 02:54:02 +03001184void rndis_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001186#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 u8 i;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001188 char name[20];
David Brownell7e27f182006-06-13 09:54:40 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001191 sprintf(name, NAME_TEMPLATE, i);
1192 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194#endif
1195}