blob: 3ff6db7828a04560eaf963f6ec77eec9d14dd2a3 [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 * Version: $Id: rndis.c,v 1.19 2004/03/25 21:33:46 robert Exp $
David Brownell7e27f182006-06-13 09:54:40 -07005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Authors: Benedikt Spranger, Pengutronix
David Brownell7e27f182006-06-13 09:54:40 -07007 * Robert Schwebel, Pengutronix
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
David Brownell7e27f182006-06-13 09:54:40 -070011 * version 2, as published by the Free Software Foundation.
12 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This software was originally developed in conformance with
14 * Microsoft's Remote NDIS Specification License Agreement.
David Brownell7e27f182006-06-13 09:54:40 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
17 * Fixed message length bug in init_response
David Brownell7e27f182006-06-13 09:54:40 -070018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
David Brownell7e27f182006-06-13 09:54:40 -070020 * Fixed rndis_rm_hdr length bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * Copyright (C) 2004 by David Brownell
23 * updates to merge with Linux 2.6, better match RNDIS spec
24 */
25
26#include <linux/config.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/kernel.h>
30#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/proc_fs.h>
34#include <linux/netdevice.h>
35
36#include <asm/io.h>
37#include <asm/byteorder.h>
38#include <asm/system.h>
David Brownell6cdee102005-04-18 17:39:34 -070039#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41
42#undef RNDIS_PM
David Brownell340600a2005-04-28 13:45:25 -070043#undef RNDIS_WAKEUP
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#undef VERBOSE
45
46#include "rndis.h"
47
48
49/* The driver for your USB chip needs to support ep0 OUT to work with
50 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
51 *
52 * Windows hosts need an INF file like Documentation/usb/linux.inf
53 * and will be happier if you provide the host_addr module parameter.
54 */
55
56#if 0
57#define DEBUG(str,args...) do { \
58 if (rndis_debug) \
59 printk(KERN_DEBUG str , ## args ); \
60 } while (0)
61static int rndis_debug = 0;
62
David Brownell340600a2005-04-28 13:45:25 -070063module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064MODULE_PARM_DESC (rndis_debug, "enable debugging");
65
66#else
67
68#define rndis_debug 0
69#define DEBUG(str,args...) do{}while(0)
70#endif
71
72#define RNDIS_MAX_CONFIGS 1
73
74
75static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
76
77/* Driver Version */
78static const __le32 rndis_driver_version = __constant_cpu_to_le32 (1);
79
80/* Function Prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static rndis_resp_t *rndis_add_response (int configNr, u32 length);
82
83
David Brownell340600a2005-04-28 13:45:25 -070084/* supported OIDs */
David Brownell7e27f182006-06-13 09:54:40 -070085static const u32 oid_supported_list [] =
David Brownell340600a2005-04-28 13:45:25 -070086{
87 /* the general stuff */
88 OID_GEN_SUPPORTED_LIST,
89 OID_GEN_HARDWARE_STATUS,
90 OID_GEN_MEDIA_SUPPORTED,
91 OID_GEN_MEDIA_IN_USE,
92 OID_GEN_MAXIMUM_FRAME_SIZE,
93 OID_GEN_LINK_SPEED,
94 OID_GEN_TRANSMIT_BLOCK_SIZE,
95 OID_GEN_RECEIVE_BLOCK_SIZE,
96 OID_GEN_VENDOR_ID,
97 OID_GEN_VENDOR_DESCRIPTION,
98 OID_GEN_VENDOR_DRIVER_VERSION,
99 OID_GEN_CURRENT_PACKET_FILTER,
100 OID_GEN_MAXIMUM_TOTAL_SIZE,
101 OID_GEN_MEDIA_CONNECT_STATUS,
102 OID_GEN_PHYSICAL_MEDIUM,
103#if 0
104 OID_GEN_RNDIS_CONFIG_PARAMETER,
105#endif
David Brownell7e27f182006-06-13 09:54:40 -0700106
David Brownell340600a2005-04-28 13:45:25 -0700107 /* the statistical stuff */
108 OID_GEN_XMIT_OK,
109 OID_GEN_RCV_OK,
110 OID_GEN_XMIT_ERROR,
111 OID_GEN_RCV_ERROR,
112 OID_GEN_RCV_NO_BUFFER,
113#ifdef RNDIS_OPTIONAL_STATS
114 OID_GEN_DIRECTED_BYTES_XMIT,
115 OID_GEN_DIRECTED_FRAMES_XMIT,
116 OID_GEN_MULTICAST_BYTES_XMIT,
117 OID_GEN_MULTICAST_FRAMES_XMIT,
118 OID_GEN_BROADCAST_BYTES_XMIT,
119 OID_GEN_BROADCAST_FRAMES_XMIT,
120 OID_GEN_DIRECTED_BYTES_RCV,
121 OID_GEN_DIRECTED_FRAMES_RCV,
122 OID_GEN_MULTICAST_BYTES_RCV,
123 OID_GEN_MULTICAST_FRAMES_RCV,
124 OID_GEN_BROADCAST_BYTES_RCV,
125 OID_GEN_BROADCAST_FRAMES_RCV,
126 OID_GEN_RCV_CRC_ERROR,
127 OID_GEN_TRANSMIT_QUEUE_LENGTH,
128#endif /* RNDIS_OPTIONAL_STATS */
129
David Brownell7e27f182006-06-13 09:54:40 -0700130 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700131 /* the general stuff */
132 OID_802_3_PERMANENT_ADDRESS,
133 OID_802_3_CURRENT_ADDRESS,
134 OID_802_3_MULTICAST_LIST,
135 OID_802_3_MAC_OPTIONS,
136 OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700137
David Brownell340600a2005-04-28 13:45:25 -0700138 /* the statistical stuff */
139 OID_802_3_RCV_ERROR_ALIGNMENT,
140 OID_802_3_XMIT_ONE_COLLISION,
141 OID_802_3_XMIT_MORE_COLLISIONS,
142#ifdef RNDIS_OPTIONAL_STATS
143 OID_802_3_XMIT_DEFERRED,
144 OID_802_3_XMIT_MAX_COLLISIONS,
145 OID_802_3_RCV_OVERRUN,
146 OID_802_3_XMIT_UNDERRUN,
147 OID_802_3_XMIT_HEARTBEAT_FAILURE,
148 OID_802_3_XMIT_TIMES_CRS_LOST,
149 OID_802_3_XMIT_LATE_COLLISIONS,
150#endif /* RNDIS_OPTIONAL_STATS */
151
152#ifdef RNDIS_PM
153 /* PM and wakeup are mandatory for USB: */
154
155 /* power management */
156 OID_PNP_CAPABILITIES,
157 OID_PNP_QUERY_POWER,
158 OID_PNP_SET_POWER,
159
160#ifdef RNDIS_WAKEUP
161 /* wake up host */
162 OID_PNP_ENABLE_WAKE_UP,
163 OID_PNP_ADD_WAKE_UP_PATTERN,
164 OID_PNP_REMOVE_WAKE_UP_PATTERN,
165#endif /* RNDIS_WAKEUP */
166#endif /* RNDIS_PM */
167};
168
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* NDIS Functions */
David Brownell340600a2005-04-28 13:45:25 -0700171static int
172gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
173 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
David Brownell7e27f182006-06-13 09:54:40 -0700175 int retval = -ENOTSUPP;
176 u32 length = 4; /* usually */
David Brownell340600a2005-04-28 13:45:25 -0700177 __le32 *outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int i, count;
179 rndis_query_cmplt_type *resp;
180
181 if (!r) return -ENOMEM;
182 resp = (rndis_query_cmplt_type *) r->buf;
183
184 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700185
186 if (buf_len && rndis_debug > 1) {
187 DEBUG("query OID %08x value, len %d:\n", OID, buf_len);
188 for (i = 0; i < buf_len; i += 16) {
189 DEBUG ("%03d: %08x %08x %08x %08x\n", i,
190 le32_to_cpup((__le32 *)&buf[i]),
191 le32_to_cpup((__le32 *)&buf[i + 4]),
192 le32_to_cpup((__le32 *)&buf[i + 8]),
193 le32_to_cpup((__le32 *)&buf[i + 12]));
194 }
195 }
196
197 /* response goes here, right after the header */
198 outbuf = (__le32 *) &resp[1];
199 resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 switch (OID) {
202
203 /* general oids (table 4-1) */
204
205 /* mandatory */
206 case OID_GEN_SUPPORTED_LIST:
207 DEBUG ("%s: OID_GEN_SUPPORTED_LIST\n", __FUNCTION__);
208 length = sizeof (oid_supported_list);
209 count = length / sizeof (u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 for (i = 0; i < count; i++)
David Brownell340600a2005-04-28 13:45:25 -0700211 outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 retval = 0;
213 break;
David Brownell7e27f182006-06-13 09:54:40 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* mandatory */
216 case OID_GEN_HARDWARE_STATUS:
217 DEBUG("%s: OID_GEN_HARDWARE_STATUS\n", __FUNCTION__);
David Brownell7e27f182006-06-13 09:54:40 -0700218 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700220 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * reddite ergo quae sunt Caesaris Caesari
222 * et quae sunt Dei Deo!
223 */
David Brownell340600a2005-04-28 13:45:25 -0700224 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 retval = 0;
226 break;
David Brownell7e27f182006-06-13 09:54:40 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* mandatory */
229 case OID_GEN_MEDIA_SUPPORTED:
230 DEBUG("%s: OID_GEN_MEDIA_SUPPORTED\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700231 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 retval = 0;
233 break;
David Brownell7e27f182006-06-13 09:54:40 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* mandatory */
236 case OID_GEN_MEDIA_IN_USE:
237 DEBUG("%s: OID_GEN_MEDIA_IN_USE\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* one medium, one transport... (maybe you do it better) */
David Brownell340600a2005-04-28 13:45:25 -0700239 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 retval = 0;
241 break;
David Brownell7e27f182006-06-13 09:54:40 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* mandatory */
244 case OID_GEN_MAXIMUM_FRAME_SIZE:
245 DEBUG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __FUNCTION__);
246 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700247 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 rndis_per_dev_params [configNr].dev->mtu);
249 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 break;
David Brownell7e27f182006-06-13 09:54:40 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* mandatory */
254 case OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700255 if (rndis_debug > 1)
256 DEBUG("%s: OID_GEN_LINK_SPEED\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (rndis_per_dev_params [configNr].media_state
David Brownell340600a2005-04-28 13:45:25 -0700258 == NDIS_MEDIA_STATE_DISCONNECTED)
259 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else
David Brownell340600a2005-04-28 13:45:25 -0700261 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 rndis_per_dev_params [configNr].speed);
263 retval = 0;
264 break;
265
266 /* mandatory */
267 case OID_GEN_TRANSMIT_BLOCK_SIZE:
268 DEBUG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __FUNCTION__);
269 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700270 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 rndis_per_dev_params [configNr].dev->mtu);
272 retval = 0;
273 }
274 break;
David Brownell7e27f182006-06-13 09:54:40 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* mandatory */
277 case OID_GEN_RECEIVE_BLOCK_SIZE:
278 DEBUG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __FUNCTION__);
279 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700280 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 rndis_per_dev_params [configNr].dev->mtu);
282 retval = 0;
283 }
284 break;
David Brownell7e27f182006-06-13 09:54:40 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* mandatory */
287 case OID_GEN_VENDOR_ID:
288 DEBUG("%s: OID_GEN_VENDOR_ID\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700289 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 rndis_per_dev_params [configNr].vendorID);
291 retval = 0;
292 break;
David Brownell7e27f182006-06-13 09:54:40 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* mandatory */
295 case OID_GEN_VENDOR_DESCRIPTION:
296 DEBUG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __FUNCTION__);
297 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
David Brownell340600a2005-04-28 13:45:25 -0700298 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 rndis_per_dev_params [configNr].vendorDescr, length);
300 retval = 0;
301 break;
302
303 case OID_GEN_VENDOR_DRIVER_VERSION:
304 DEBUG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700306 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 retval = 0;
308 break;
309
310 /* mandatory */
311 case OID_GEN_CURRENT_PACKET_FILTER:
312 DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700313 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 retval = 0;
315 break;
316
317 /* mandatory */
318 case OID_GEN_MAXIMUM_TOTAL_SIZE:
319 DEBUG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700320 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 retval = 0;
322 break;
323
324 /* mandatory */
325 case OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700326 if (rndis_debug > 1)
327 DEBUG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __FUNCTION__);
328 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 .media_state);
330 retval = 0;
331 break;
332
333 case OID_GEN_PHYSICAL_MEDIUM:
334 DEBUG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700335 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 retval = 0;
337 break;
338
339 /* The RNDIS specification is incomplete/wrong. Some versions
340 * of MS-Windows expect OIDs that aren't specified there. Other
341 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
342 */
343 case OID_GEN_MAC_OPTIONS: /* from WinME */
344 DEBUG("%s: OID_GEN_MAC_OPTIONS\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700345 *outbuf = __constant_cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
347 | NDIS_MAC_OPTION_FULL_DUPLEX);
348 retval = 0;
349 break;
350
351 /* statistics OIDs (table 4-2) */
352
353 /* mandatory */
354 case OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700355 if (rndis_debug > 1)
356 DEBUG("%s: OID_GEN_XMIT_OK\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700358 *outbuf = cpu_to_le32 (
David Brownell7e27f182006-06-13 09:54:40 -0700359 rndis_per_dev_params [configNr].stats->tx_packets -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rndis_per_dev_params [configNr].stats->tx_errors -
361 rndis_per_dev_params [configNr].stats->tx_dropped);
362 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 break;
365
366 /* mandatory */
367 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700368 if (rndis_debug > 1)
369 DEBUG("%s: OID_GEN_RCV_OK\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700371 *outbuf = cpu_to_le32 (
David Brownell7e27f182006-06-13 09:54:40 -0700372 rndis_per_dev_params [configNr].stats->rx_packets -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 rndis_per_dev_params [configNr].stats->rx_errors -
374 rndis_per_dev_params [configNr].stats->rx_dropped);
375 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)
382 DEBUG("%s: OID_GEN_XMIT_ERROR\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700384 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 .stats->tx_errors);
386 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 break;
David Brownell7e27f182006-06-13 09:54:40 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* mandatory */
391 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700392 if (rndis_debug > 1)
393 DEBUG("%s: OID_GEN_RCV_ERROR\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700395 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .stats->rx_errors);
397 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 break;
David Brownell7e27f182006-06-13 09:54:40 -0700400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* mandatory */
402 case OID_GEN_RCV_NO_BUFFER:
403 DEBUG("%s: OID_GEN_RCV_NO_BUFFER\n", __FUNCTION__);
404 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700405 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 .stats->rx_dropped);
407 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 break;
410
411#ifdef RNDIS_OPTIONAL_STATS
412 case OID_GEN_DIRECTED_BYTES_XMIT:
413 DEBUG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __FUNCTION__);
David Brownell7e27f182006-06-13 09:54:40 -0700414 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * Aunt Tilly's size of shoes
416 * minus antarctica count of penguins
417 * divided by weight of Alpha Centauri
418 */
419 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700420 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 (rndis_per_dev_params [configNr]
David Brownell7e27f182006-06-13 09:54:40 -0700422 .stats->tx_packets -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 rndis_per_dev_params [configNr]
424 .stats->tx_errors -
425 rndis_per_dev_params [configNr]
426 .stats->tx_dropped)
427 * 123);
428 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 break;
David Brownell7e27f182006-06-13 09:54:40 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 case OID_GEN_DIRECTED_FRAMES_XMIT:
433 DEBUG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __FUNCTION__);
434 /* dito */
435 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700436 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 (rndis_per_dev_params [configNr]
David Brownell7e27f182006-06-13 09:54:40 -0700438 .stats->tx_packets -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 rndis_per_dev_params [configNr]
440 .stats->tx_errors -
441 rndis_per_dev_params [configNr]
442 .stats->tx_dropped)
443 / 123);
444 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 break;
David Brownell7e27f182006-06-13 09:54:40 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case OID_GEN_MULTICAST_BYTES_XMIT:
449 DEBUG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __FUNCTION__);
450 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700451 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 .stats->multicast*1234);
453 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 break;
David Brownell7e27f182006-06-13 09:54:40 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case OID_GEN_MULTICAST_FRAMES_XMIT:
458 DEBUG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __FUNCTION__);
459 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700460 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .stats->multicast);
462 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 break;
David Brownell7e27f182006-06-13 09:54:40 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 case OID_GEN_BROADCAST_BYTES_XMIT:
467 DEBUG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __FUNCTION__);
468 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700469 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 .stats->tx_packets/42*255);
471 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 break;
David Brownell7e27f182006-06-13 09:54:40 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 case OID_GEN_BROADCAST_FRAMES_XMIT:
476 DEBUG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __FUNCTION__);
477 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700478 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 .stats->tx_packets/42);
480 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 break;
David Brownell7e27f182006-06-13 09:54:40 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 case OID_GEN_DIRECTED_BYTES_RCV:
485 DEBUG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700486 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 retval = 0;
488 break;
David Brownell7e27f182006-06-13 09:54:40 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 case OID_GEN_DIRECTED_FRAMES_RCV:
491 DEBUG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700492 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 retval = 0;
494 break;
David Brownell7e27f182006-06-13 09:54:40 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 case OID_GEN_MULTICAST_BYTES_RCV:
497 DEBUG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __FUNCTION__);
498 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700499 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .stats->multicast * 1111);
501 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 break;
David Brownell7e27f182006-06-13 09:54:40 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 case OID_GEN_MULTICAST_FRAMES_RCV:
506 DEBUG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __FUNCTION__);
507 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700508 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 .stats->multicast);
510 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 break;
David Brownell7e27f182006-06-13 09:54:40 -0700513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 case OID_GEN_BROADCAST_BYTES_RCV:
515 DEBUG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __FUNCTION__);
516 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700517 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 .stats->rx_packets/42*255);
519 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 break;
David Brownell7e27f182006-06-13 09:54:40 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 case OID_GEN_BROADCAST_FRAMES_RCV:
524 DEBUG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __FUNCTION__);
525 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700526 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 .stats->rx_packets/42);
528 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 break;
David Brownell7e27f182006-06-13 09:54:40 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 case OID_GEN_RCV_CRC_ERROR:
533 DEBUG("%s: OID_GEN_RCV_CRC_ERROR\n", __FUNCTION__);
534 if (rndis_per_dev_params [configNr].stats) {
David Brownell340600a2005-04-28 13:45:25 -0700535 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .stats->rx_crc_errors);
537 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 break;
David Brownell7e27f182006-06-13 09:54:40 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
542 DEBUG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700543 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 retval = 0;
545 break;
546#endif /* RNDIS_OPTIONAL_STATS */
547
548 /* ieee802.3 OIDs (table 4-3) */
549
550 /* mandatory */
551 case OID_802_3_PERMANENT_ADDRESS:
552 DEBUG("%s: OID_802_3_PERMANENT_ADDRESS\n", __FUNCTION__);
553 if (rndis_per_dev_params [configNr].dev) {
554 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700555 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 rndis_per_dev_params [configNr].host_mac,
557 length);
558 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 break;
David Brownell7e27f182006-06-13 09:54:40 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* mandatory */
563 case OID_802_3_CURRENT_ADDRESS:
564 DEBUG("%s: OID_802_3_CURRENT_ADDRESS\n", __FUNCTION__);
565 if (rndis_per_dev_params [configNr].dev) {
566 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700567 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 rndis_per_dev_params [configNr].host_mac,
569 length);
570 retval = 0;
571 }
572 break;
David Brownell7e27f182006-06-13 09:54:40 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* mandatory */
575 case OID_802_3_MULTICAST_LIST:
576 DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* Multicast base address only */
David Brownell340600a2005-04-28 13:45:25 -0700578 *outbuf = __constant_cpu_to_le32 (0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 retval = 0;
580 break;
David Brownell7e27f182006-06-13 09:54:40 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* mandatory */
583 case OID_802_3_MAXIMUM_LIST_SIZE:
584 DEBUG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Multicast base address only */
David Brownell340600a2005-04-28 13:45:25 -0700586 *outbuf = __constant_cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 retval = 0;
588 break;
David Brownell7e27f182006-06-13 09:54:40 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 case OID_802_3_MAC_OPTIONS:
591 DEBUG("%s: OID_802_3_MAC_OPTIONS\n", __FUNCTION__);
592 break;
593
594 /* ieee802.3 statistics OIDs (table 4-4) */
595
596 /* mandatory */
597 case OID_802_3_RCV_ERROR_ALIGNMENT:
598 DEBUG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700599 if (rndis_per_dev_params [configNr].stats) {
600 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 .stats->rx_frame_errors);
602 retval = 0;
603 }
604 break;
David Brownell7e27f182006-06-13 09:54:40 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* mandatory */
607 case OID_802_3_XMIT_ONE_COLLISION:
608 DEBUG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700609 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 retval = 0;
611 break;
David Brownell7e27f182006-06-13 09:54:40 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* mandatory */
614 case OID_802_3_XMIT_MORE_COLLISIONS:
615 DEBUG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __FUNCTION__);
David Brownell340600a2005-04-28 13:45:25 -0700616 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 retval = 0;
618 break;
David Brownell7e27f182006-06-13 09:54:40 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#ifdef RNDIS_OPTIONAL_STATS
621 case OID_802_3_XMIT_DEFERRED:
622 DEBUG("%s: OID_802_3_XMIT_DEFERRED\n", __FUNCTION__);
623 /* TODO */
624 break;
David Brownell7e27f182006-06-13 09:54:40 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 case OID_802_3_XMIT_MAX_COLLISIONS:
627 DEBUG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __FUNCTION__);
628 /* TODO */
629 break;
David Brownell7e27f182006-06-13 09:54:40 -0700630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 case OID_802_3_RCV_OVERRUN:
632 DEBUG("%s: OID_802_3_RCV_OVERRUN\n", __FUNCTION__);
633 /* TODO */
634 break;
David Brownell7e27f182006-06-13 09:54:40 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 case OID_802_3_XMIT_UNDERRUN:
637 DEBUG("%s: OID_802_3_XMIT_UNDERRUN\n", __FUNCTION__);
638 /* TODO */
639 break;
David Brownell7e27f182006-06-13 09:54:40 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
642 DEBUG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __FUNCTION__);
643 /* TODO */
644 break;
David Brownell7e27f182006-06-13 09:54:40 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 case OID_802_3_XMIT_TIMES_CRS_LOST:
647 DEBUG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __FUNCTION__);
648 /* TODO */
649 break;
David Brownell7e27f182006-06-13 09:54:40 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 case OID_802_3_XMIT_LATE_COLLISIONS:
652 DEBUG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __FUNCTION__);
653 /* TODO */
David Brownell7e27f182006-06-13 09:54:40 -0700654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655#endif /* RNDIS_OPTIONAL_STATS */
656
657#ifdef RNDIS_PM
658 /* power management OIDs (table 4-5) */
659 case OID_PNP_CAPABILITIES:
660 DEBUG("%s: OID_PNP_CAPABILITIES\n", __FUNCTION__);
661
David Brownell340600a2005-04-28 13:45:25 -0700662 /* for now, no wakeup capabilities */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 length = sizeof (struct NDIS_PNP_CAPABILITIES);
David Brownell340600a2005-04-28 13:45:25 -0700664 memset(outbuf, 0, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 retval = 0;
666 break;
667 case OID_PNP_QUERY_POWER:
David Brownell340600a2005-04-28 13:45:25 -0700668 DEBUG("%s: OID_PNP_QUERY_POWER D%d\n", __FUNCTION__,
669 le32_to_cpup((__le32 *) buf) - 1);
670 /* only suspend is a real power state, and
671 * it can't be entered by OID_PNP_SET_POWER...
672 */
673 length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 retval = 0;
675 break;
676#endif
677
678 default:
David Brownell7e27f182006-06-13 09:54:40 -0700679 printk (KERN_WARNING "%s: query unknown OID 0x%08X\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 __FUNCTION__, OID);
681 }
David Brownell340600a2005-04-28 13:45:25 -0700682 if (retval < 0)
683 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 resp->InformationBufferLength = cpu_to_le32 (length);
David Brownell340600a2005-04-28 13:45:25 -0700686 r->length = length + sizeof *resp;
687 resp->MessageLength = cpu_to_le32 (r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return retval;
689}
690
David Brownell7e27f182006-06-13 09:54:40 -0700691static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
692 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 rndis_set_cmplt_type *resp;
David Brownell7e27f182006-06-13 09:54:40 -0700695 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct rndis_params *params;
697
698 if (!r)
699 return -ENOMEM;
700 resp = (rndis_set_cmplt_type *) r->buf;
701 if (!resp)
702 return -ENOMEM;
703
David Brownell340600a2005-04-28 13:45:25 -0700704 if (buf_len && rndis_debug > 1) {
705 DEBUG("set OID %08x value, len %d:\n", OID, buf_len);
706 for (i = 0; i < buf_len; i += 16) {
707 DEBUG ("%03d: %08x %08x %08x %08x\n", i,
708 le32_to_cpup((__le32 *)&buf[i]),
709 le32_to_cpup((__le32 *)&buf[i + 4]),
710 le32_to_cpup((__le32 *)&buf[i + 8]),
711 le32_to_cpup((__le32 *)&buf[i + 12]));
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
David Brownell340600a2005-04-28 13:45:25 -0700715 params = &rndis_per_dev_params [configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 switch (OID) {
717 case OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
David Brownell340600a2005-04-28 13:45:25 -0700719 /* these NDIS_PACKET_TYPE_* bitflags are shared with
720 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
722 * PROMISCUOUS, DIRECTED,
723 * MULTICAST, ALL_MULTICAST, BROADCAST
724 */
David Brownell340600a2005-04-28 13:45:25 -0700725 *params->filter = (u16) le32_to_cpup((__le32 *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
David Brownell340600a2005-04-28 13:45:25 -0700727 __FUNCTION__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* this call has a significant side effect: it's
730 * what makes the packet flow start and stop, like
731 * activating the CDC Ethernet altsetting.
732 */
David Brownell340600a2005-04-28 13:45:25 -0700733#ifdef RNDIS_PM
734update_linkstate:
735#endif
736 retval = 0;
737 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 params->state = RNDIS_DATA_INITIALIZED;
739 netif_carrier_on(params->dev);
740 if (netif_running(params->dev))
741 netif_wake_queue (params->dev);
742 } else {
743 params->state = RNDIS_INITIALIZED;
744 netif_carrier_off (params->dev);
745 netif_stop_queue (params->dev);
746 }
747 break;
David Brownell7e27f182006-06-13 09:54:40 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 case OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700750 /* I think we can ignore this */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
752 retval = 0;
753 break;
754#if 0
755 case OID_GEN_RNDIS_CONFIG_PARAMETER:
756 {
757 struct rndis_config_parameter *param;
758 param = (struct rndis_config_parameter *) buf;
759 DEBUG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
760 __FUNCTION__,
761 min(cpu_to_le32(param->ParameterNameLength),80),
762 buf + param->ParameterNameOffset);
763 retval = 0;
764 }
765 break;
766#endif
767
768#ifdef RNDIS_PM
769 case OID_PNP_SET_POWER:
David Brownell340600a2005-04-28 13:45:25 -0700770 /* The only real power state is USB suspend, and RNDIS requests
771 * can't enter it; this one isn't really about power. After
772 * resuming, Windows forces a reset, and then SET_POWER D0.
773 * FIXME ... then things go batty; Windows wedges itself.
774 */
775 i = le32_to_cpup((__force __le32 *)buf);
776 DEBUG("%s: OID_PNP_SET_POWER D%d\n", __FUNCTION__, i - 1);
777 switch (i) {
778 case NdisDeviceStateD0:
779 *params->filter = params->saved_filter;
780 goto update_linkstate;
781 case NdisDeviceStateD3:
782 case NdisDeviceStateD2:
783 case NdisDeviceStateD1:
784 params->saved_filter = *params->filter;
785 retval = 0;
786 break;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789
David Brownell340600a2005-04-28 13:45:25 -0700790#ifdef RNDIS_WAKEUP
791 // no wakeup support advertised, so wakeup OIDs always fail:
792 // - OID_PNP_ENABLE_WAKE_UP
793 // - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794#endif
795
David Brownell340600a2005-04-28 13:45:25 -0700796#endif /* RNDIS_PM */
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 default:
David Brownell7e27f182006-06-13 09:54:40 -0700799 printk (KERN_WARNING "%s: set unknown OID 0x%08X, size %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 __FUNCTION__, OID, buf_len);
801 }
David Brownell7e27f182006-06-13 09:54:40 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return retval;
804}
805
David Brownell7e27f182006-06-13 09:54:40 -0700806/*
807 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809
810static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
811{
David Brownell7e27f182006-06-13 09:54:40 -0700812 rndis_init_cmplt_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700818 if (!r)
819 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 resp = (rndis_init_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 resp->MessageType = __constant_cpu_to_le32 (
823 REMOTE_NDIS_INITIALIZE_CMPLT);
824 resp->MessageLength = __constant_cpu_to_le32 (52);
825 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
826 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
827 resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
828 resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
829 resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
830 resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
831 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
832 resp->MaxTransferSize = cpu_to_le32 (
833 rndis_per_dev_params [configNr].dev->mtu
834 + sizeof (struct ethhdr)
835 + sizeof (struct rndis_packet_msg_type)
836 + 22);
837 resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
838 resp->AFListOffset = __constant_cpu_to_le32 (0);
839 resp->AFListSize = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (rndis_per_dev_params [configNr].ack)
David Brownell7e27f182006-06-13 09:54:40 -0700842 rndis_per_dev_params [configNr].ack (
843 rndis_per_dev_params [configNr].dev);
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return 0;
846}
847
848static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
849{
850 rndis_query_cmplt_type *resp;
851 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 // DEBUG("%s: OID = %08X\n", __FUNCTION__, cpu_to_le32(buf->OID));
854 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700855
Shaun Tancheff87637162006-02-22 19:47:19 -0800856 /*
857 * we need more memory:
858 * gen_ndis_query_resp expects enough space for
859 * rndis_query_cmplt_type followed by data.
860 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
Shaun Tancheff87637162006-02-22 19:47:19 -0800862 r = rndis_add_response (configNr,
863 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700864 if (!r)
865 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 resp = (rndis_query_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700870
David Brownell340600a2005-04-28 13:45:25 -0700871 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
872 le32_to_cpu(buf->InformationBufferOffset)
873 + 8 + (u8 *) buf,
874 le32_to_cpu(buf->InformationBufferLength),
875 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* OID not supported */
877 resp->Status = __constant_cpu_to_le32 (
878 RNDIS_STATUS_NOT_SUPPORTED);
David Brownell340600a2005-04-28 13:45:25 -0700879 resp->MessageLength = __constant_cpu_to_le32 (sizeof *resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 resp->InformationBufferLength = __constant_cpu_to_le32 (0);
881 resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
882 } else
883 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (rndis_per_dev_params [configNr].ack)
David Brownell7e27f182006-06-13 09:54:40 -0700886 rndis_per_dev_params [configNr].ack (
887 rndis_per_dev_params [configNr].dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889}
890
891static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
892{
893 u32 BufLength, BufOffset;
894 rndis_set_cmplt_type *resp;
895 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700898 if (!r)
899 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 resp = (rndis_set_cmplt_type *) r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 BufLength = le32_to_cpu (buf->InformationBufferLength);
903 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
904
905#ifdef VERBOSE
906 DEBUG("%s: Length: %d\n", __FUNCTION__, BufLength);
907 DEBUG("%s: Offset: %d\n", __FUNCTION__, BufOffset);
908 DEBUG("%s: InfoBuffer: ", __FUNCTION__);
David Brownell7e27f182006-06-13 09:54:40 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 for (i = 0; i < BufLength; i++) {
911 DEBUG ("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
912 }
David Brownell7e27f182006-06-13 09:54:40 -0700913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 DEBUG ("\n");
915#endif
David Brownell7e27f182006-06-13 09:54:40 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
918 resp->MessageLength = __constant_cpu_to_le32 (16);
919 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700920 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
921 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
922 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
923 else
924 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (rndis_per_dev_params [configNr].ack)
David Brownell7e27f182006-06-13 09:54:40 -0700927 rndis_per_dev_params [configNr].ack (
928 rndis_per_dev_params [configNr].dev);
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931}
932
933static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
934{
935 rndis_reset_cmplt_type *resp;
936 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700939 if (!r)
940 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 resp = (rndis_reset_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
944 resp->MessageLength = __constant_cpu_to_le32 (16);
945 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
946 /* resent information */
947 resp->AddressingReset = __constant_cpu_to_le32 (1);
David Brownell7e27f182006-06-13 09:54:40 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (rndis_per_dev_params [configNr].ack)
David Brownell7e27f182006-06-13 09:54:40 -0700950 rndis_per_dev_params [configNr].ack (
951 rndis_per_dev_params [configNr].dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 return 0;
954}
955
956static int rndis_keepalive_response (int configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700957 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 rndis_keepalive_cmplt_type *resp;
960 rndis_resp_t *r;
961
962 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
963
964 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700965 if (!r)
966 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 resp = (rndis_keepalive_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 resp->MessageType = __constant_cpu_to_le32 (
970 REMOTE_NDIS_KEEPALIVE_CMPLT);
971 resp->MessageLength = __constant_cpu_to_le32 (16);
972 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
973 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (rndis_per_dev_params [configNr].ack)
David Brownell7e27f182006-06-13 09:54:40 -0700976 rndis_per_dev_params [configNr].ack (
977 rndis_per_dev_params [configNr].dev);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
982
David Brownell7e27f182006-06-13 09:54:40 -0700983/*
984 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
986static int rndis_indicate_status_msg (int configNr, u32 status)
987{
David Brownell7e27f182006-06-13 09:54:40 -0700988 rndis_indicate_status_msg_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700992 return -ENOTSUPP;
993
994 r = rndis_add_response (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 sizeof (rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700996 if (!r)
997 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 resp = (rndis_indicate_status_msg_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 resp->MessageType = __constant_cpu_to_le32 (
1001 REMOTE_NDIS_INDICATE_STATUS_MSG);
1002 resp->MessageLength = __constant_cpu_to_le32 (20);
1003 resp->Status = cpu_to_le32 (status);
1004 resp->StatusBufferLength = __constant_cpu_to_le32 (0);
1005 resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -07001006
1007 if (rndis_per_dev_params [configNr].ack)
1008 rndis_per_dev_params [configNr].ack (
1009 rndis_per_dev_params [configNr].dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return 0;
1011}
1012
1013int rndis_signal_connect (int configNr)
1014{
1015 rndis_per_dev_params [configNr].media_state
1016 = NDIS_MEDIA_STATE_CONNECTED;
David Brownell7e27f182006-06-13 09:54:40 -07001017 return rndis_indicate_status_msg (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 RNDIS_STATUS_MEDIA_CONNECT);
1019}
1020
1021int rndis_signal_disconnect (int configNr)
1022{
1023 rndis_per_dev_params [configNr].media_state
1024 = NDIS_MEDIA_STATE_DISCONNECTED;
1025 return rndis_indicate_status_msg (configNr,
1026 RNDIS_STATUS_MEDIA_DISCONNECT);
1027}
1028
David Brownell340600a2005-04-28 13:45:25 -07001029void rndis_uninit (int configNr)
1030{
David Brownell486e2df2005-05-24 17:51:52 -07001031 u8 *buf;
1032 u32 length;
1033
David Brownell340600a2005-04-28 13:45:25 -07001034 if (configNr >= RNDIS_MAX_CONFIGS)
1035 return;
1036 rndis_per_dev_params [configNr].used = 0;
1037 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -07001038
1039 /* drain the response queue */
1040 while ((buf = rndis_get_next_response(configNr, &length)))
1041 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -07001042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044void rndis_set_host_mac (int configNr, const u8 *addr)
1045{
1046 rndis_per_dev_params [configNr].host_mac = addr;
1047}
1048
David Brownell7e27f182006-06-13 09:54:40 -07001049/*
1050 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
1052int rndis_msg_parser (u8 configNr, u8 *buf)
1053{
1054 u32 MsgType, MsgLength;
1055 __le32 *tmp;
1056 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -07001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!buf)
1059 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -07001060
1061 tmp = (__le32 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 MsgType = le32_to_cpup(tmp++);
1063 MsgLength = le32_to_cpup(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -07001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (configNr >= RNDIS_MAX_CONFIGS)
1066 return -ENOTSUPP;
1067 params = &rndis_per_dev_params [configNr];
David Brownell7e27f182006-06-13 09:54:40 -07001068
David Brownell340600a2005-04-28 13:45:25 -07001069 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1070 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1071 * and normal HC level polling to see if there's any IN traffic.
1072 */
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -07001075 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell7e27f182006-06-13 09:54:40 -07001077 DEBUG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 __FUNCTION__ );
1079 params->state = RNDIS_INITIALIZED;
1080 return rndis_init_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -07001081 (rndis_init_msg_type *) buf);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 case REMOTE_NDIS_HALT_MSG:
1084 DEBUG("%s: REMOTE_NDIS_HALT_MSG\n",
1085 __FUNCTION__ );
1086 params->state = RNDIS_UNINITIALIZED;
1087 if (params->dev) {
1088 netif_carrier_off (params->dev);
1089 netif_stop_queue (params->dev);
1090 }
1091 return 0;
David Brownell7e27f182006-06-13 09:54:40 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 case REMOTE_NDIS_QUERY_MSG:
David Brownell7e27f182006-06-13 09:54:40 -07001094 return rndis_query_response (configNr,
1095 (rndis_query_msg_type *) buf);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 case REMOTE_NDIS_SET_MSG:
David Brownell7e27f182006-06-13 09:54:40 -07001098 return rndis_set_response (configNr,
1099 (rndis_set_msg_type *) buf);
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 case REMOTE_NDIS_RESET_MSG:
David Brownell7e27f182006-06-13 09:54:40 -07001102 DEBUG("%s: REMOTE_NDIS_RESET_MSG\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 __FUNCTION__ );
1104 return rndis_reset_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -07001105 (rndis_reset_msg_type *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 case REMOTE_NDIS_KEEPALIVE_MSG:
1108 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -07001109 if (rndis_debug > 1)
David Brownell7e27f182006-06-13 09:54:40 -07001110 DEBUG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
David Brownell340600a2005-04-28 13:45:25 -07001111 __FUNCTION__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return rndis_keepalive_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -07001113 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 buf);
David Brownell7e27f182006-06-13 09:54:40 -07001115
1116 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /* At least Windows XP emits some undefined RNDIS messages.
1118 * In one case those messages seemed to relate to the host
1119 * suspending itself.
1120 */
1121 printk (KERN_WARNING
David Brownell7e27f182006-06-13 09:54:40 -07001122 "%s: unknown RNDIS message 0x%08X len %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 __FUNCTION__ , MsgType, MsgLength);
1124 {
1125 unsigned i;
1126 for (i = 0; i < MsgLength; i += 16) {
1127 DEBUG ("%03d: "
1128 " %02x %02x %02x %02x"
1129 " %02x %02x %02x %02x"
1130 " %02x %02x %02x %02x"
1131 " %02x %02x %02x %02x"
1132 "\n",
1133 i,
1134 buf[i], buf [i+1],
1135 buf[i+2], buf[i+3],
1136 buf[i+4], buf [i+5],
1137 buf[i+6], buf[i+7],
1138 buf[i+8], buf [i+9],
1139 buf[i+10], buf[i+11],
1140 buf[i+12], buf [i+13],
1141 buf[i+14], buf[i+15]);
1142 }
1143 }
1144 break;
1145 }
David Brownell7e27f182006-06-13 09:54:40 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -ENOTSUPP;
1148}
1149
1150int rndis_register (int (* rndis_control_ack) (struct net_device *))
1151{
1152 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1155 if (!rndis_per_dev_params [i].used) {
1156 rndis_per_dev_params [i].used = 1;
1157 rndis_per_dev_params [i].ack = rndis_control_ack;
1158 DEBUG("%s: configNr = %d\n", __FUNCTION__, i);
1159 return i;
1160 }
1161 }
1162 DEBUG("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return -1;
1165}
1166
1167void rndis_deregister (int configNr)
1168{
1169 DEBUG("%s: \n", __FUNCTION__ );
David Brownell7e27f182006-06-13 09:54:40 -07001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (configNr >= RNDIS_MAX_CONFIGS) return;
1172 rndis_per_dev_params [configNr].used = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return;
1175}
1176
David Brownell7e27f182006-06-13 09:54:40 -07001177int rndis_set_param_dev (u8 configNr, struct net_device *dev,
David Brownell340600a2005-04-28 13:45:25 -07001178 struct net_device_stats *stats,
1179 u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 DEBUG("%s:\n", __FUNCTION__ );
1182 if (!dev || !stats) return -1;
1183 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 rndis_per_dev_params [configNr].dev = dev;
1186 rndis_per_dev_params [configNr].stats = stats;
David Brownell340600a2005-04-28 13:45:25 -07001187 rndis_per_dev_params [configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -07001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190}
1191
1192int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
1193{
1194 DEBUG("%s:\n", __FUNCTION__ );
1195 if (!vendorDescr) return -1;
1196 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 rndis_per_dev_params [configNr].vendorID = vendorID;
1199 rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return 0;
1202}
1203
1204int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
1205{
David Brownell340600a2005-04-28 13:45:25 -07001206 DEBUG("%s: %u %u\n", __FUNCTION__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 rndis_per_dev_params [configNr].medium = medium;
1210 rndis_per_dev_params [configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
1213}
1214
1215void rndis_add_hdr (struct sk_buff *skb)
1216{
1217 struct rndis_packet_msg_type *header;
1218
1219 if (!skb)
1220 return;
1221 header = (void *) skb_push (skb, sizeof *header);
1222 memset (header, 0, sizeof *header);
David Brownell6cdee102005-04-18 17:39:34 -07001223 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 header->MessageLength = cpu_to_le32(skb->len);
1225 header->DataOffset = __constant_cpu_to_le32 (36);
David Brownell6cdee102005-04-18 17:39:34 -07001226 header->DataLength = cpu_to_le32(skb->len - sizeof *header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229void rndis_free_response (int configNr, u8 *buf)
1230{
1231 rndis_resp_t *r;
1232 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -07001233
1234 list_for_each_safe (act, tmp,
1235 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 {
1237 r = list_entry (act, rndis_resp_t, list);
1238 if (r && r->buf == buf) {
1239 list_del (&r->list);
1240 kfree (r);
1241 }
1242 }
1243}
1244
1245u8 *rndis_get_next_response (int configNr, u32 *length)
1246{
1247 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001248 struct list_head *act, *tmp;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001251
1252 list_for_each_safe (act, tmp,
1253 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 {
1255 r = list_entry (act, rndis_resp_t, list);
1256 if (!r->send) {
1257 r->send = 1;
1258 *length = r->length;
1259 return r->buf;
1260 }
1261 }
David Brownell7e27f182006-06-13 09:54:40 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return NULL;
1264}
1265
1266static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1267{
1268 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001269
David Brownell340600a2005-04-28 13:45:25 -07001270 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1272 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 r->buf = (u8 *) (r + 1);
1275 r->length = length;
1276 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001277
1278 list_add_tail (&r->list,
1279 &(rndis_per_dev_params [configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return r;
1281}
1282
David Brownell6cdee102005-04-18 17:39:34 -07001283int rndis_rm_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
David Brownell6cdee102005-04-18 17:39:34 -07001285 /* tmp points to a struct rndis_packet_msg_type */
1286 __le32 *tmp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
David Brownell6cdee102005-04-18 17:39:34 -07001288 /* MessageType, MessageLength */
1289 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1290 != get_unaligned(tmp++))
1291 return -EINVAL;
1292 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
David Brownell6cdee102005-04-18 17:39:34 -07001294 /* DataOffset, DataLength */
1295 if (!skb_pull(skb, le32_to_cpu(get_unaligned(tmp++))
1296 + 8 /* offset of DataOffset */))
1297 return -EOVERFLOW;
1298 skb_trim(skb, le32_to_cpu(get_unaligned(tmp++)));
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0;
1301}
1302
1303#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1304
David Brownell7e27f182006-06-13 09:54:40 -07001305static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof,
1306 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 char *out = page;
1309 int len;
1310 rndis_params *param = (rndis_params *) data;
David Brownell7e27f182006-06-13 09:54:40 -07001311
1312 out += snprintf (out, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 "Config Nr. %d\n"
1314 "used : %s\n"
1315 "state : %s\n"
1316 "medium : 0x%08X\n"
1317 "speed : %d\n"
1318 "cable : %s\n"
1319 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001320 "vendor : %s\n",
1321 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 ({ char *s = "?";
1323 switch (param->state) {
1324 case RNDIS_UNINITIALIZED:
1325 s = "RNDIS_UNINITIALIZED"; break;
1326 case RNDIS_INITIALIZED:
1327 s = "RNDIS_INITIALIZED"; break;
1328 case RNDIS_DATA_INITIALIZED:
1329 s = "RNDIS_DATA_INITIALIZED"; break;
1330 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001331 param->medium,
1332 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001334 param->vendorID, param->vendorDescr);
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 len = out - page;
1337 len -= off;
David Brownell7e27f182006-06-13 09:54:40 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (len < count) {
1340 *eof = 1;
1341 if (len <= 0)
1342 return 0;
1343 } else
1344 len = count;
David Brownell7e27f182006-06-13 09:54:40 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *start = page + off;
1347 return len;
1348}
1349
David Brownell7e27f182006-06-13 09:54:40 -07001350static int rndis_proc_write (struct file *file, const char __user *buffer,
1351 unsigned long count, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 rndis_params *p = data;
1354 u32 speed = 0;
1355 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 for (i = 0; i < count; i++) {
1358 char c;
1359 if (get_user(c, buffer))
1360 return -EFAULT;
1361 switch (c) {
1362 case '0':
1363 case '1':
1364 case '2':
1365 case '3':
1366 case '4':
1367 case '5':
1368 case '6':
1369 case '7':
1370 case '8':
1371 case '9':
1372 fl_speed = 1;
1373 speed = speed*10 + c - '0';
1374 break;
1375 case 'C':
1376 case 'c':
1377 rndis_signal_connect (p->confignr);
1378 break;
1379 case 'D':
1380 case 'd':
1381 rndis_signal_disconnect(p->confignr);
1382 break;
David Brownell7e27f182006-06-13 09:54:40 -07001383 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (fl_speed) p->speed = speed;
1385 else DEBUG ("%c is not valid\n", c);
1386 break;
1387 }
David Brownell7e27f182006-06-13 09:54:40 -07001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 buffer++;
1390 }
David Brownell7e27f182006-06-13 09:54:40 -07001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return count;
1393}
1394
1395#define NAME_TEMPLATE "driver/rndis-%03d"
1396
1397static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1398
1399#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1400
1401
1402int __init rndis_init (void)
1403{
1404 u8 i;
1405
1406 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1407#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1408 char name [20];
1409
1410 sprintf (name, NAME_TEMPLATE, i);
1411 if (!(rndis_connect_state [i]
David Brownell7e27f182006-06-13 09:54:40 -07001412 = create_proc_entry (name, 0660, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 {
1414 DEBUG ("%s :remove entries", __FUNCTION__);
1415 while (i) {
1416 sprintf (name, NAME_TEMPLATE, --i);
1417 remove_proc_entry (name, NULL);
1418 }
1419 DEBUG ("\n");
1420 return -EIO;
1421 }
1422
1423 rndis_connect_state [i]->nlink = 1;
1424 rndis_connect_state [i]->write_proc = rndis_proc_write;
1425 rndis_connect_state [i]->read_proc = rndis_proc_read;
1426 rndis_connect_state [i]->data = (void *)
1427 (rndis_per_dev_params + i);
1428#endif
1429 rndis_per_dev_params [i].confignr = i;
1430 rndis_per_dev_params [i].used = 0;
1431 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1432 rndis_per_dev_params [i].media_state
1433 = NDIS_MEDIA_STATE_DISCONNECTED;
1434 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1435 }
David Brownell7e27f182006-06-13 09:54:40 -07001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438}
1439
1440void rndis_exit (void)
1441{
1442#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1443 u8 i;
1444 char name [20];
David Brownell7e27f182006-06-13 09:54:40 -07001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1447 sprintf (name, NAME_TEMPLATE, i);
1448 remove_proc_entry (name, NULL);
1449 }
1450#endif
1451}
1452