blob: 2b4660e08c4db367af0e6c5159855b675616eb9a [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>
Alexey Dobriyane184d5f2008-05-14 16:25:13 -070031#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/netdevice.h>
33
34#include <asm/io.h>
35#include <asm/byteorder.h>
36#include <asm/system.h>
David Brownell6cdee102005-04-18 17:39:34 -070037#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39
David Brownell15b2d2b2008-06-19 18:19:16 -070040#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "rndis.h"
43
44
45/* The driver for your USB chip needs to support ep0 OUT to work with
46 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
47 *
48 * Windows hosts need an INF file like Documentation/usb/linux.inf
49 * and will be happier if you provide the host_addr module parameter.
50 */
51
52#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070054module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
60#define RNDIS_MAX_CONFIGS 1
61
62
63static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
64
65/* Driver Version */
Harvey Harrison35c26c22009-02-14 22:56:56 -080066static const __le32 rndis_driver_version = cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Function Prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static rndis_resp_t *rndis_add_response (int configNr, u32 length);
70
71
David Brownell340600a2005-04-28 13:45:25 -070072/* supported OIDs */
David Brownell7e27f182006-06-13 09:54:40 -070073static const u32 oid_supported_list [] =
David Brownell340600a2005-04-28 13:45:25 -070074{
75 /* the general stuff */
76 OID_GEN_SUPPORTED_LIST,
77 OID_GEN_HARDWARE_STATUS,
78 OID_GEN_MEDIA_SUPPORTED,
79 OID_GEN_MEDIA_IN_USE,
80 OID_GEN_MAXIMUM_FRAME_SIZE,
81 OID_GEN_LINK_SPEED,
82 OID_GEN_TRANSMIT_BLOCK_SIZE,
83 OID_GEN_RECEIVE_BLOCK_SIZE,
84 OID_GEN_VENDOR_ID,
85 OID_GEN_VENDOR_DESCRIPTION,
86 OID_GEN_VENDOR_DRIVER_VERSION,
87 OID_GEN_CURRENT_PACKET_FILTER,
88 OID_GEN_MAXIMUM_TOTAL_SIZE,
89 OID_GEN_MEDIA_CONNECT_STATUS,
90 OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070091
David Brownell340600a2005-04-28 13:45:25 -070092 /* the statistical stuff */
93 OID_GEN_XMIT_OK,
94 OID_GEN_RCV_OK,
95 OID_GEN_XMIT_ERROR,
96 OID_GEN_RCV_ERROR,
97 OID_GEN_RCV_NO_BUFFER,
98#ifdef RNDIS_OPTIONAL_STATS
99 OID_GEN_DIRECTED_BYTES_XMIT,
100 OID_GEN_DIRECTED_FRAMES_XMIT,
101 OID_GEN_MULTICAST_BYTES_XMIT,
102 OID_GEN_MULTICAST_FRAMES_XMIT,
103 OID_GEN_BROADCAST_BYTES_XMIT,
104 OID_GEN_BROADCAST_FRAMES_XMIT,
105 OID_GEN_DIRECTED_BYTES_RCV,
106 OID_GEN_DIRECTED_FRAMES_RCV,
107 OID_GEN_MULTICAST_BYTES_RCV,
108 OID_GEN_MULTICAST_FRAMES_RCV,
109 OID_GEN_BROADCAST_BYTES_RCV,
110 OID_GEN_BROADCAST_FRAMES_RCV,
111 OID_GEN_RCV_CRC_ERROR,
112 OID_GEN_TRANSMIT_QUEUE_LENGTH,
113#endif /* RNDIS_OPTIONAL_STATS */
114
David Brownell7e27f182006-06-13 09:54:40 -0700115 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700116 /* the general stuff */
117 OID_802_3_PERMANENT_ADDRESS,
118 OID_802_3_CURRENT_ADDRESS,
119 OID_802_3_MULTICAST_LIST,
120 OID_802_3_MAC_OPTIONS,
121 OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700122
David Brownell340600a2005-04-28 13:45:25 -0700123 /* the statistical stuff */
124 OID_802_3_RCV_ERROR_ALIGNMENT,
125 OID_802_3_XMIT_ONE_COLLISION,
126 OID_802_3_XMIT_MORE_COLLISIONS,
127#ifdef RNDIS_OPTIONAL_STATS
128 OID_802_3_XMIT_DEFERRED,
129 OID_802_3_XMIT_MAX_COLLISIONS,
130 OID_802_3_RCV_OVERRUN,
131 OID_802_3_XMIT_UNDERRUN,
132 OID_802_3_XMIT_HEARTBEAT_FAILURE,
133 OID_802_3_XMIT_TIMES_CRS_LOST,
134 OID_802_3_XMIT_LATE_COLLISIONS,
135#endif /* RNDIS_OPTIONAL_STATS */
136
137#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700138 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
139 * don't say what they mean ... and the NDIS specs are often
140 * confusing and/or ambiguous in this context. (That is, more
141 * so than their specs for the other OIDs.)
142 *
143 * FIXME someone who knows what these should do, please
144 * implement them!
145 */
David Brownell340600a2005-04-28 13:45:25 -0700146
147 /* power management */
148 OID_PNP_CAPABILITIES,
149 OID_PNP_QUERY_POWER,
150 OID_PNP_SET_POWER,
151
152#ifdef RNDIS_WAKEUP
153 /* wake up host */
154 OID_PNP_ENABLE_WAKE_UP,
155 OID_PNP_ADD_WAKE_UP_PATTERN,
156 OID_PNP_REMOVE_WAKE_UP_PATTERN,
157#endif /* RNDIS_WAKEUP */
158#endif /* RNDIS_PM */
159};
160
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* NDIS Functions */
David Brownell340600a2005-04-28 13:45:25 -0700163static int
164gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
165 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
David Brownell7e27f182006-06-13 09:54:40 -0700167 int retval = -ENOTSUPP;
168 u32 length = 4; /* usually */
David Brownell340600a2005-04-28 13:45:25 -0700169 __le32 *outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int i, count;
171 rndis_query_cmplt_type *resp;
David Brownell15b2d2b2008-06-19 18:19:16 -0700172 struct net_device *net;
Stephen Hemmingerb5556492009-03-20 19:35:59 +0000173 const struct net_device_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!r) return -ENOMEM;
176 resp = (rndis_query_cmplt_type *) r->buf;
177
178 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700179
180 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700181 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700182 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700183 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700184 get_unaligned_le32(&buf[i]),
185 get_unaligned_le32(&buf[i + 4]),
186 get_unaligned_le32(&buf[i + 8]),
187 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700188 }
189 }
190
191 /* response goes here, right after the header */
192 outbuf = (__le32 *) &resp[1];
Harvey Harrison35c26c22009-02-14 22:56:56 -0800193 resp->InformationBufferOffset = cpu_to_le32 (16);
David Brownell340600a2005-04-28 13:45:25 -0700194
David Brownell15b2d2b2008-06-19 18:19:16 -0700195 net = rndis_per_dev_params[configNr].dev;
Stephen Hemmingerb5556492009-03-20 19:35:59 +0000196 stats = dev_get_stats(net);
David Brownell15b2d2b2008-06-19 18:19:16 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 switch (OID) {
199
200 /* general oids (table 4-1) */
201
202 /* mandatory */
203 case OID_GEN_SUPPORTED_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700204 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 length = sizeof (oid_supported_list);
206 count = length / sizeof (u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 for (i = 0; i < count; i++)
David Brownell340600a2005-04-28 13:45:25 -0700208 outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 retval = 0;
210 break;
David Brownell7e27f182006-06-13 09:54:40 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* mandatory */
213 case OID_GEN_HARDWARE_STATUS:
David Brownell33376c12008-08-18 17:45:07 -0700214 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700215 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700217 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * reddite ergo quae sunt Caesaris Caesari
219 * et quae sunt Dei Deo!
220 */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800221 *outbuf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 retval = 0;
223 break;
David Brownell7e27f182006-06-13 09:54:40 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* mandatory */
226 case OID_GEN_MEDIA_SUPPORTED:
David Brownell33376c12008-08-18 17:45:07 -0700227 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700228 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 retval = 0;
230 break;
David Brownell7e27f182006-06-13 09:54:40 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* mandatory */
233 case OID_GEN_MEDIA_IN_USE:
David Brownell33376c12008-08-18 17:45:07 -0700234 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* one medium, one transport... (maybe you do it better) */
David Brownell340600a2005-04-28 13:45:25 -0700236 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 retval = 0;
238 break;
David Brownell7e27f182006-06-13 09:54:40 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* mandatory */
241 case OID_GEN_MAXIMUM_FRAME_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700242 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700244 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 rndis_per_dev_params [configNr].dev->mtu);
246 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 break;
David Brownell7e27f182006-06-13 09:54:40 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* mandatory */
251 case OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700252 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700253 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (rndis_per_dev_params [configNr].media_state
David Brownell340600a2005-04-28 13:45:25 -0700255 == NDIS_MEDIA_STATE_DISCONNECTED)
Harvey Harrison35c26c22009-02-14 22:56:56 -0800256 *outbuf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 else
David Brownell340600a2005-04-28 13:45:25 -0700258 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 rndis_per_dev_params [configNr].speed);
260 retval = 0;
261 break;
262
263 /* mandatory */
264 case OID_GEN_TRANSMIT_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700265 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700267 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 rndis_per_dev_params [configNr].dev->mtu);
269 retval = 0;
270 }
271 break;
David Brownell7e27f182006-06-13 09:54:40 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* mandatory */
274 case OID_GEN_RECEIVE_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700275 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700277 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 rndis_per_dev_params [configNr].dev->mtu);
279 retval = 0;
280 }
281 break;
David Brownell7e27f182006-06-13 09:54:40 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* mandatory */
284 case OID_GEN_VENDOR_ID:
David Brownell33376c12008-08-18 17:45:07 -0700285 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700286 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 rndis_per_dev_params [configNr].vendorID);
288 retval = 0;
289 break;
David Brownell7e27f182006-06-13 09:54:40 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* mandatory */
292 case OID_GEN_VENDOR_DESCRIPTION:
David Brownell33376c12008-08-18 17:45:07 -0700293 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
David Brownell340600a2005-04-28 13:45:25 -0700295 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 rndis_per_dev_params [configNr].vendorDescr, length);
297 retval = 0;
298 break;
299
300 case OID_GEN_VENDOR_DRIVER_VERSION:
David Brownell33376c12008-08-18 17:45:07 -0700301 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700303 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 retval = 0;
305 break;
306
307 /* mandatory */
308 case OID_GEN_CURRENT_PACKET_FILTER:
David Brownell33376c12008-08-18 17:45:07 -0700309 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700310 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 retval = 0;
312 break;
313
314 /* mandatory */
315 case OID_GEN_MAXIMUM_TOTAL_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700316 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800317 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 retval = 0;
319 break;
320
321 /* mandatory */
322 case OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700323 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700324 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700325 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .media_state);
327 retval = 0;
328 break;
329
330 case OID_GEN_PHYSICAL_MEDIUM:
David Brownell33376c12008-08-18 17:45:07 -0700331 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800332 *outbuf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 retval = 0;
334 break;
335
336 /* The RNDIS specification is incomplete/wrong. Some versions
337 * of MS-Windows expect OIDs that aren't specified there. Other
338 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
339 */
340 case OID_GEN_MAC_OPTIONS: /* from WinME */
David Brownell33376c12008-08-18 17:45:07 -0700341 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800342 *outbuf = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
344 | NDIS_MAC_OPTION_FULL_DUPLEX);
345 retval = 0;
346 break;
347
348 /* statistics OIDs (table 4-2) */
349
350 /* mandatory */
351 case OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700352 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700353 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700354 if (stats) {
355 *outbuf = cpu_to_le32(stats->tx_packets
356 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 break;
360
361 /* mandatory */
362 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700363 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700364 pr_debug("%s: OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700365 if (stats) {
366 *outbuf = cpu_to_le32(stats->rx_packets
367 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 break;
David Brownell7e27f182006-06-13 09:54:40 -0700371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* mandatory */
373 case OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700374 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700375 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700376 if (stats) {
377 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 break;
David Brownell7e27f182006-06-13 09:54:40 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* mandatory */
383 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700384 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700385 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700386 if (stats) {
387 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 break;
David Brownell7e27f182006-06-13 09:54:40 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* mandatory */
393 case OID_GEN_RCV_NO_BUFFER:
David Brownell33376c12008-08-18 17:45:07 -0700394 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700395 if (stats) {
396 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 break;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* ieee802.3 OIDs (table 4-3) */
402
403 /* mandatory */
404 case OID_802_3_PERMANENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700405 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (rndis_per_dev_params [configNr].dev) {
407 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700408 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 rndis_per_dev_params [configNr].host_mac,
410 length);
411 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 break;
David Brownell7e27f182006-06-13 09:54:40 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* mandatory */
416 case OID_802_3_CURRENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700417 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (rndis_per_dev_params [configNr].dev) {
419 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700420 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 rndis_per_dev_params [configNr].host_mac,
422 length);
423 retval = 0;
424 }
425 break;
David Brownell7e27f182006-06-13 09:54:40 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* mandatory */
428 case OID_802_3_MULTICAST_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700429 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* Multicast base address only */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800431 *outbuf = cpu_to_le32 (0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 retval = 0;
433 break;
David Brownell7e27f182006-06-13 09:54:40 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* mandatory */
436 case OID_802_3_MAXIMUM_LIST_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700437 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* Multicast base address only */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800439 *outbuf = cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 retval = 0;
441 break;
David Brownell7e27f182006-06-13 09:54:40 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 case OID_802_3_MAC_OPTIONS:
David Brownell33376c12008-08-18 17:45:07 -0700444 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446
447 /* ieee802.3 statistics OIDs (table 4-4) */
448
449 /* mandatory */
450 case OID_802_3_RCV_ERROR_ALIGNMENT:
David Brownell33376c12008-08-18 17:45:07 -0700451 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700452 if (stats) {
453 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 retval = 0;
455 }
456 break;
David Brownell7e27f182006-06-13 09:54:40 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* mandatory */
459 case OID_802_3_XMIT_ONE_COLLISION:
David Brownell33376c12008-08-18 17:45:07 -0700460 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800461 *outbuf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 retval = 0;
463 break;
David Brownell7e27f182006-06-13 09:54:40 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* mandatory */
466 case OID_802_3_XMIT_MORE_COLLISIONS:
David Brownell33376c12008-08-18 17:45:07 -0700467 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800468 *outbuf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 retval = 0;
470 break;
David Brownell7e27f182006-06-13 09:54:40 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 default:
David Brownell00274922007-11-19 12:58:36 -0800473 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800474 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
David Brownell340600a2005-04-28 13:45:25 -0700476 if (retval < 0)
477 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 resp->InformationBufferLength = cpu_to_le32 (length);
David Brownell340600a2005-04-28 13:45:25 -0700480 r->length = length + sizeof *resp;
481 resp->MessageLength = cpu_to_le32 (r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return retval;
483}
484
David Brownell7e27f182006-06-13 09:54:40 -0700485static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
486 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 rndis_set_cmplt_type *resp;
David Brownell7e27f182006-06-13 09:54:40 -0700489 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct rndis_params *params;
491
492 if (!r)
493 return -ENOMEM;
494 resp = (rndis_set_cmplt_type *) r->buf;
495 if (!resp)
496 return -ENOMEM;
497
David Brownell340600a2005-04-28 13:45:25 -0700498 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700499 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700500 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700501 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700502 get_unaligned_le32(&buf[i]),
503 get_unaligned_le32(&buf[i + 4]),
504 get_unaligned_le32(&buf[i + 8]),
505 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
David Brownell340600a2005-04-28 13:45:25 -0700509 params = &rndis_per_dev_params [configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 switch (OID) {
511 case OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
David Brownell340600a2005-04-28 13:45:25 -0700513 /* these NDIS_PACKET_TYPE_* bitflags are shared with
514 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
516 * PROMISCUOUS, DIRECTED,
517 * MULTICAST, ALL_MULTICAST, BROADCAST
518 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700519 *params->filter = (u16)get_unaligned_le32(buf);
David Brownell33376c12008-08-18 17:45:07 -0700520 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800521 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* this call has a significant side effect: it's
524 * what makes the packet flow start and stop, like
525 * activating the CDC Ethernet altsetting.
526 */
David Brownell340600a2005-04-28 13:45:25 -0700527 retval = 0;
528 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 params->state = RNDIS_DATA_INITIALIZED;
530 netif_carrier_on(params->dev);
531 if (netif_running(params->dev))
532 netif_wake_queue (params->dev);
533 } else {
534 params->state = RNDIS_INITIALIZED;
535 netif_carrier_off (params->dev);
536 netif_stop_queue (params->dev);
537 }
538 break;
David Brownell7e27f182006-06-13 09:54:40 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 case OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700541 /* I think we can ignore this */
David Brownell33376c12008-08-18 17:45:07 -0700542 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 retval = 0;
544 break;
David Brownell340600a2005-04-28 13:45:25 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 default:
David Brownell00274922007-11-19 12:58:36 -0800547 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800548 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
David Brownell7e27f182006-06-13 09:54:40 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return retval;
552}
553
David Brownell7e27f182006-06-13 09:54:40 -0700554/*
555 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
557
558static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
559{
David Brownell7e27f182006-06-13 09:54:40 -0700560 rndis_init_cmplt_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700562 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700563
David Brownell15b2d2b2008-06-19 18:19:16 -0700564 if (!params->dev)
565 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700568 if (!r)
569 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 resp = (rndis_init_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700571
Harvey Harrison35c26c22009-02-14 22:56:56 -0800572 resp->MessageType = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 REMOTE_NDIS_INITIALIZE_CMPLT);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800574 resp->MessageLength = cpu_to_le32 (52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800576 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
577 resp->MajorVersion = cpu_to_le32 (RNDIS_MAJOR_VERSION);
578 resp->MinorVersion = cpu_to_le32 (RNDIS_MINOR_VERSION);
579 resp->DeviceFlags = cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
580 resp->Medium = cpu_to_le32 (RNDIS_MEDIUM_802_3);
581 resp->MaxPacketsPerTransfer = cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 resp->MaxTransferSize = cpu_to_le32 (
David Brownell15b2d2b2008-06-19 18:19:16 -0700583 params->dev->mtu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 + sizeof (struct ethhdr)
585 + sizeof (struct rndis_packet_msg_type)
586 + 22);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800587 resp->PacketAlignmentFactor = cpu_to_le32 (0);
588 resp->AFListOffset = cpu_to_le32 (0);
589 resp->AFListSize = cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -0700590
David Brownell15b2d2b2008-06-19 18:19:16 -0700591 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593}
594
595static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
596{
597 rndis_query_cmplt_type *resp;
598 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700599 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700600
David Brownell33376c12008-08-18 17:45:07 -0700601 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700602 if (!params->dev)
603 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700604
Shaun Tancheff87637162006-02-22 19:47:19 -0800605 /*
606 * we need more memory:
607 * gen_ndis_query_resp expects enough space for
608 * rndis_query_cmplt_type followed by data.
609 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Shaun Tancheff87637162006-02-22 19:47:19 -0800611 r = rndis_add_response (configNr,
612 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700613 if (!r)
614 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 resp = (rndis_query_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700616
Harvey Harrison35c26c22009-02-14 22:56:56 -0800617 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700619
David Brownell340600a2005-04-28 13:45:25 -0700620 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
621 le32_to_cpu(buf->InformationBufferOffset)
622 + 8 + (u8 *) buf,
623 le32_to_cpu(buf->InformationBufferLength),
624 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* OID not supported */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800626 resp->Status = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 RNDIS_STATUS_NOT_SUPPORTED);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800628 resp->MessageLength = cpu_to_le32 (sizeof *resp);
629 resp->InformationBufferLength = cpu_to_le32 (0);
630 resp->InformationBufferOffset = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 } else
Harvey Harrison35c26c22009-02-14 22:56:56 -0800632 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700633
David Brownell15b2d2b2008-06-19 18:19:16 -0700634 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636}
637
638static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
639{
640 u32 BufLength, BufOffset;
641 rndis_set_cmplt_type *resp;
642 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700643 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700646 if (!r)
647 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 resp = (rndis_set_cmplt_type *) r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 BufLength = le32_to_cpu (buf->InformationBufferLength);
651 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
652
David Brownell15b2d2b2008-06-19 18:19:16 -0700653#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700654 pr_debug("%s: Length: %d\n", __func__, BufLength);
655 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
656 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700659 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
David Brownell7e27f182006-06-13 09:54:40 -0700661
David Brownell33376c12008-08-18 17:45:07 -0700662 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#endif
David Brownell7e27f182006-06-13 09:54:40 -0700664
Harvey Harrison35c26c22009-02-14 22:56:56 -0800665 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
666 resp->MessageLength = cpu_to_le32 (16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700668 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
669 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
Harvey Harrison35c26c22009-02-14 22:56:56 -0800670 resp->Status = cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700671 else
Harvey Harrison35c26c22009-02-14 22:56:56 -0800672 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700673
David Brownell15b2d2b2008-06-19 18:19:16 -0700674 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return 0;
676}
677
678static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
679{
680 rndis_reset_cmplt_type *resp;
681 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700682 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700685 if (!r)
686 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 resp = (rndis_reset_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700688
Harvey Harrison35c26c22009-02-14 22:56:56 -0800689 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
690 resp->MessageLength = cpu_to_le32 (16);
691 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* resent information */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800693 resp->AddressingReset = cpu_to_le32 (1);
David Brownell7e27f182006-06-13 09:54:40 -0700694
David Brownell15b2d2b2008-06-19 18:19:16 -0700695 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697}
698
699static int rndis_keepalive_response (int configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700700 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 rndis_keepalive_cmplt_type *resp;
703 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700704 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
707
708 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700709 if (!r)
710 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 resp = (rndis_keepalive_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700712
Harvey Harrison35c26c22009-02-14 22:56:56 -0800713 resp->MessageType = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 REMOTE_NDIS_KEEPALIVE_CMPLT);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800715 resp->MessageLength = cpu_to_le32 (16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800717 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700718
David Brownell15b2d2b2008-06-19 18:19:16 -0700719 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 0;
721}
722
723
David Brownell7e27f182006-06-13 09:54:40 -0700724/*
725 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
727static int rndis_indicate_status_msg (int configNr, u32 status)
728{
David Brownell7e27f182006-06-13 09:54:40 -0700729 rndis_indicate_status_msg_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700731 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700732
David Brownell15b2d2b2008-06-19 18:19:16 -0700733 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700734 return -ENOTSUPP;
735
736 r = rndis_add_response (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 sizeof (rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700738 if (!r)
739 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 resp = (rndis_indicate_status_msg_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700741
Harvey Harrison35c26c22009-02-14 22:56:56 -0800742 resp->MessageType = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 REMOTE_NDIS_INDICATE_STATUS_MSG);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800744 resp->MessageLength = cpu_to_le32 (20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 resp->Status = cpu_to_le32 (status);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800746 resp->StatusBufferLength = cpu_to_le32 (0);
747 resp->StatusBufferOffset = cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -0700748
David Brownell15b2d2b2008-06-19 18:19:16 -0700749 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751}
752
753int rndis_signal_connect (int configNr)
754{
755 rndis_per_dev_params [configNr].media_state
756 = NDIS_MEDIA_STATE_CONNECTED;
David Brownell7e27f182006-06-13 09:54:40 -0700757 return rndis_indicate_status_msg (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 RNDIS_STATUS_MEDIA_CONNECT);
759}
760
761int rndis_signal_disconnect (int configNr)
762{
763 rndis_per_dev_params [configNr].media_state
764 = NDIS_MEDIA_STATE_DISCONNECTED;
765 return rndis_indicate_status_msg (configNr,
766 RNDIS_STATUS_MEDIA_DISCONNECT);
767}
768
David Brownell340600a2005-04-28 13:45:25 -0700769void rndis_uninit (int configNr)
770{
David Brownell486e2df2005-05-24 17:51:52 -0700771 u8 *buf;
772 u32 length;
773
David Brownell340600a2005-04-28 13:45:25 -0700774 if (configNr >= RNDIS_MAX_CONFIGS)
775 return;
David Brownell340600a2005-04-28 13:45:25 -0700776 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700777
778 /* drain the response queue */
779 while ((buf = rndis_get_next_response(configNr, &length)))
780 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783void rndis_set_host_mac (int configNr, const u8 *addr)
784{
785 rndis_per_dev_params [configNr].host_mac = addr;
786}
787
David Brownell7e27f182006-06-13 09:54:40 -0700788/*
789 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
791int rndis_msg_parser (u8 configNr, u8 *buf)
792{
793 u32 MsgType, MsgLength;
794 __le32 *tmp;
795 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!buf)
798 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700799
800 tmp = (__le32 *) buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700801 MsgType = get_unaligned_le32(tmp++);
802 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (configNr >= RNDIS_MAX_CONFIGS)
805 return -ENOTSUPP;
806 params = &rndis_per_dev_params [configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700807
David Brownell340600a2005-04-28 13:45:25 -0700808 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
809 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
810 * and normal HC level polling to see if there's any IN traffic.
811 */
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700814 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700816 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800817 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 params->state = RNDIS_INITIALIZED;
819 return rndis_init_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700820 (rndis_init_msg_type *) buf);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 case REMOTE_NDIS_HALT_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700823 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800824 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 params->state = RNDIS_UNINITIALIZED;
826 if (params->dev) {
827 netif_carrier_off (params->dev);
828 netif_stop_queue (params->dev);
829 }
830 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 case REMOTE_NDIS_QUERY_MSG:
David Brownell7e27f182006-06-13 09:54:40 -0700833 return rndis_query_response (configNr,
834 (rndis_query_msg_type *) buf);
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 case REMOTE_NDIS_SET_MSG:
David Brownell7e27f182006-06-13 09:54:40 -0700837 return rndis_set_response (configNr,
838 (rndis_set_msg_type *) buf);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 case REMOTE_NDIS_RESET_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700841 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800842 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return rndis_reset_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700844 (rndis_reset_msg_type *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 case REMOTE_NDIS_KEEPALIVE_MSG:
847 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700848 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700849 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800850 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return rndis_keepalive_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700852 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700854
855 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* At least Windows XP emits some undefined RNDIS messages.
857 * In one case those messages seemed to relate to the host
858 * suspending itself.
859 */
David Brownell00274922007-11-19 12:58:36 -0800860 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800861 __func__ , MsgType, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 {
863 unsigned i;
864 for (i = 0; i < MsgLength; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700865 pr_debug("%03d: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 " %02x %02x %02x %02x"
867 " %02x %02x %02x %02x"
868 " %02x %02x %02x %02x"
869 " %02x %02x %02x %02x"
870 "\n",
871 i,
872 buf[i], buf [i+1],
873 buf[i+2], buf[i+3],
874 buf[i+4], buf [i+5],
875 buf[i+6], buf[i+7],
876 buf[i+8], buf [i+9],
877 buf[i+10], buf[i+11],
878 buf[i+12], buf [i+13],
879 buf[i+14], buf[i+15]);
880 }
881 }
882 break;
883 }
David Brownell7e27f182006-06-13 09:54:40 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return -ENOTSUPP;
886}
887
David Brownell15b2d2b2008-06-19 18:19:16 -0700888int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700891
David Brownell15b2d2b2008-06-19 18:19:16 -0700892 if (!resp_avail)
893 return -EINVAL;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
896 if (!rndis_per_dev_params [i].used) {
897 rndis_per_dev_params [i].used = 1;
David Brownell15b2d2b2008-06-19 18:19:16 -0700898 rndis_per_dev_params [i].resp_avail = resp_avail;
899 rndis_per_dev_params [i].v = v;
David Brownell33376c12008-08-18 17:45:07 -0700900 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return i;
902 }
903 }
David Brownell33376c12008-08-18 17:45:07 -0700904 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700905
David Brownell15b2d2b2008-06-19 18:19:16 -0700906 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909void rndis_deregister (int configNr)
910{
David Brownell33376c12008-08-18 17:45:07 -0700911 pr_debug("%s: \n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (configNr >= RNDIS_MAX_CONFIGS) return;
914 rndis_per_dev_params [configNr].used = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return;
917}
918
David Brownell15b2d2b2008-06-19 18:19:16 -0700919int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
David Brownell33376c12008-08-18 17:45:07 -0700921 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700922 if (!dev)
923 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 rndis_per_dev_params [configNr].dev = dev;
David Brownell340600a2005-04-28 13:45:25 -0700927 rndis_per_dev_params [configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
932int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
933{
David Brownell33376c12008-08-18 17:45:07 -0700934 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (!vendorDescr) return -1;
936 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 rndis_per_dev_params [configNr].vendorID = vendorID;
939 rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return 0;
942}
943
944int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
945{
David Brownell33376c12008-08-18 17:45:07 -0700946 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 rndis_per_dev_params [configNr].medium = medium;
950 rndis_per_dev_params [configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
955void rndis_add_hdr (struct sk_buff *skb)
956{
957 struct rndis_packet_msg_type *header;
958
959 if (!skb)
960 return;
961 header = (void *) skb_push (skb, sizeof *header);
962 memset (header, 0, sizeof *header);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800963 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 header->MessageLength = cpu_to_le32(skb->len);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800965 header->DataOffset = cpu_to_le32 (36);
David Brownell6cdee102005-04-18 17:39:34 -0700966 header->DataLength = cpu_to_le32(skb->len - sizeof *header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969void rndis_free_response (int configNr, u8 *buf)
970{
971 rndis_resp_t *r;
972 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700973
974 list_for_each_safe (act, tmp,
975 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 {
977 r = list_entry (act, rndis_resp_t, list);
978 if (r && r->buf == buf) {
979 list_del (&r->list);
980 kfree (r);
981 }
982 }
983}
984
985u8 *rndis_get_next_response (int configNr, u32 *length)
986{
987 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700988 struct list_head *act, *tmp;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700991
992 list_for_each_safe (act, tmp,
993 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 {
995 r = list_entry (act, rndis_resp_t, list);
996 if (!r->send) {
997 r->send = 1;
998 *length = r->length;
999 return r->buf;
1000 }
1001 }
David Brownell7e27f182006-06-13 09:54:40 -07001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return NULL;
1004}
1005
1006static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1007{
1008 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001009
David Brownell340600a2005-04-28 13:45:25 -07001010 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1012 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 r->buf = (u8 *) (r + 1);
1015 r->length = length;
1016 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001017
1018 list_add_tail (&r->list,
1019 &(rndis_per_dev_params [configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return r;
1021}
1022
David Brownell6cdee102005-04-18 17:39:34 -07001023int rndis_rm_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
David Brownell6cdee102005-04-18 17:39:34 -07001025 /* tmp points to a struct rndis_packet_msg_type */
1026 __le32 *tmp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
David Brownell6cdee102005-04-18 17:39:34 -07001028 /* MessageType, MessageLength */
Harvey Harrison35c26c22009-02-14 22:56:56 -08001029 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
David Brownell6cdee102005-04-18 17:39:34 -07001030 != get_unaligned(tmp++))
1031 return -EINVAL;
1032 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David Brownell6cdee102005-04-18 17:39:34 -07001034 /* DataOffset, DataLength */
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001035 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8))
David Brownell6cdee102005-04-18 17:39:34 -07001036 return -EOVERFLOW;
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001037 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return 0;
1040}
1041
1042#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1043
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001044static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001046 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001047
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001048 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 "Config Nr. %d\n"
1050 "used : %s\n"
1051 "state : %s\n"
1052 "medium : 0x%08X\n"
1053 "speed : %d\n"
1054 "cable : %s\n"
1055 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001056 "vendor : %s\n",
1057 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ({ char *s = "?";
1059 switch (param->state) {
1060 case RNDIS_UNINITIALIZED:
1061 s = "RNDIS_UNINITIALIZED"; break;
1062 case RNDIS_INITIALIZED:
1063 s = "RNDIS_INITIALIZED"; break;
1064 case RNDIS_DATA_INITIALIZED:
1065 s = "RNDIS_DATA_INITIALIZED"; break;
1066 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001067 param->medium,
1068 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001070 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001074static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1075 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001077 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 u32 speed = 0;
1079 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 for (i = 0; i < count; i++) {
1082 char c;
1083 if (get_user(c, buffer))
1084 return -EFAULT;
1085 switch (c) {
1086 case '0':
1087 case '1':
1088 case '2':
1089 case '3':
1090 case '4':
1091 case '5':
1092 case '6':
1093 case '7':
1094 case '8':
1095 case '9':
1096 fl_speed = 1;
1097 speed = speed*10 + c - '0';
1098 break;
1099 case 'C':
1100 case 'c':
1101 rndis_signal_connect (p->confignr);
1102 break;
1103 case 'D':
1104 case 'd':
1105 rndis_signal_disconnect(p->confignr);
1106 break;
David Brownell7e27f182006-06-13 09:54:40 -07001107 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001109 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111 }
David Brownell7e27f182006-06-13 09:54:40 -07001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 buffer++;
1114 }
David Brownell7e27f182006-06-13 09:54:40 -07001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return count;
1117}
1118
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001119static int rndis_proc_open(struct inode *inode, struct file *file)
1120{
1121 return single_open(file, rndis_proc_show, PDE(inode)->data);
1122}
1123
1124static const struct file_operations rndis_proc_fops = {
1125 .owner = THIS_MODULE,
1126 .open = rndis_proc_open,
1127 .read = seq_read,
1128 .llseek = seq_lseek,
1129 .release = single_release,
1130 .write = rndis_proc_write,
1131};
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133#define NAME_TEMPLATE "driver/rndis-%03d"
1134
1135static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1136
1137#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1138
1139
David Brownell0e530b42008-04-05 14:17:14 -07001140int __init rndis_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 u8 i;
1143
1144 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1145#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1146 char name [20];
1147
1148 sprintf (name, NAME_TEMPLATE, i);
1149 if (!(rndis_connect_state [i]
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001150 = proc_create_data(name, 0660, NULL,
1151 &rndis_proc_fops,
1152 (void *)(rndis_per_dev_params + i))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 {
David Brownell33376c12008-08-18 17:45:07 -07001154 pr_debug("%s :remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 while (i) {
1156 sprintf (name, NAME_TEMPLATE, --i);
1157 remove_proc_entry (name, NULL);
1158 }
David Brownell33376c12008-08-18 17:45:07 -07001159 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -EIO;
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162#endif
1163 rndis_per_dev_params [i].confignr = i;
1164 rndis_per_dev_params [i].used = 0;
1165 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1166 rndis_per_dev_params [i].media_state
1167 = NDIS_MEDIA_STATE_DISCONNECTED;
1168 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1169 }
David Brownell7e27f182006-06-13 09:54:40 -07001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172}
1173
1174void rndis_exit (void)
1175{
1176#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1177 u8 i;
1178 char name [20];
David Brownell7e27f182006-06-13 09:54:40 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1181 sprintf (name, NAME_TEMPLATE, i);
1182 remove_proc_entry (name, NULL);
1183 }
1184#endif
1185}
1186