blob: d2860a823680065859ee105872732f65445f94b1 [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;
173 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;
196 if (net->get_stats)
197 stats = net->get_stats(net);
198 else
199 stats = NULL;
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:
David Brownell33376c12008-08-18 17:45:07 -0700207 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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:
David Brownell33376c12008-08-18 17:45:07 -0700217 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
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 */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800224 *outbuf = 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:
David Brownell33376c12008-08-18 17:45:07 -0700230 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
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:
David Brownell33376c12008-08-18 17:45:07 -0700237 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
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:
David Brownell33376c12008-08-18 17:45:07 -0700245 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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)
David Brownell33376c12008-08-18 17:45:07 -0700256 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
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)
Harvey Harrison35c26c22009-02-14 22:56:56 -0800259 *outbuf = 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:
David Brownell33376c12008-08-18 17:45:07 -0700268 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 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:
David Brownell33376c12008-08-18 17:45:07 -0700278 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 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:
David Brownell33376c12008-08-18 17:45:07 -0700288 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
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:
David Brownell33376c12008-08-18 17:45:07 -0700296 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 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:
David Brownell33376c12008-08-18 17:45:07 -0700304 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
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:
David Brownell33376c12008-08-18 17:45:07 -0700312 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
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:
David Brownell33376c12008-08-18 17:45:07 -0700319 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800320 *outbuf = 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)
David Brownell33376c12008-08-18 17:45:07 -0700327 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700328 *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:
David Brownell33376c12008-08-18 17:45:07 -0700334 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800335 *outbuf = 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 */
David Brownell33376c12008-08-18 17:45:07 -0700344 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800345 *outbuf = 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)
David Brownell33376c12008-08-18 17:45:07 -0700356 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700357 if (stats) {
358 *outbuf = cpu_to_le32(stats->tx_packets
359 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 break;
363
364 /* mandatory */
365 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700366 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700367 pr_debug("%s: OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700368 if (stats) {
369 *outbuf = cpu_to_le32(stats->rx_packets
370 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 break;
David Brownell7e27f182006-06-13 09:54:40 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* mandatory */
376 case OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700377 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700378 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700379 if (stats) {
380 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 break;
David Brownell7e27f182006-06-13 09:54:40 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* mandatory */
386 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700387 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700388 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700389 if (stats) {
390 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 break;
David Brownell7e27f182006-06-13 09:54:40 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* mandatory */
396 case OID_GEN_RCV_NO_BUFFER:
David Brownell33376c12008-08-18 17:45:07 -0700397 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700398 if (stats) {
399 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 break;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* ieee802.3 OIDs (table 4-3) */
405
406 /* mandatory */
407 case OID_802_3_PERMANENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700408 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (rndis_per_dev_params [configNr].dev) {
410 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700411 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 rndis_per_dev_params [configNr].host_mac,
413 length);
414 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 break;
David Brownell7e27f182006-06-13 09:54:40 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* mandatory */
419 case OID_802_3_CURRENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700420 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (rndis_per_dev_params [configNr].dev) {
422 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700423 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 rndis_per_dev_params [configNr].host_mac,
425 length);
426 retval = 0;
427 }
428 break;
David Brownell7e27f182006-06-13 09:54:40 -0700429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* mandatory */
431 case OID_802_3_MULTICAST_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700432 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* Multicast base address only */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800434 *outbuf = cpu_to_le32 (0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 retval = 0;
436 break;
David Brownell7e27f182006-06-13 09:54:40 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* mandatory */
439 case OID_802_3_MAXIMUM_LIST_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700440 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* Multicast base address only */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800442 *outbuf = cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 retval = 0;
444 break;
David Brownell7e27f182006-06-13 09:54:40 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 case OID_802_3_MAC_OPTIONS:
David Brownell33376c12008-08-18 17:45:07 -0700447 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449
450 /* ieee802.3 statistics OIDs (table 4-4) */
451
452 /* mandatory */
453 case OID_802_3_RCV_ERROR_ALIGNMENT:
David Brownell33376c12008-08-18 17:45:07 -0700454 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700455 if (stats) {
456 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 retval = 0;
458 }
459 break;
David Brownell7e27f182006-06-13 09:54:40 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* mandatory */
462 case OID_802_3_XMIT_ONE_COLLISION:
David Brownell33376c12008-08-18 17:45:07 -0700463 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800464 *outbuf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 retval = 0;
466 break;
David Brownell7e27f182006-06-13 09:54:40 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* mandatory */
469 case OID_802_3_XMIT_MORE_COLLISIONS:
David Brownell33376c12008-08-18 17:45:07 -0700470 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800471 *outbuf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 retval = 0;
473 break;
David Brownell7e27f182006-06-13 09:54:40 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 default:
David Brownell00274922007-11-19 12:58:36 -0800476 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800477 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
David Brownell340600a2005-04-28 13:45:25 -0700479 if (retval < 0)
480 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 resp->InformationBufferLength = cpu_to_le32 (length);
David Brownell340600a2005-04-28 13:45:25 -0700483 r->length = length + sizeof *resp;
484 resp->MessageLength = cpu_to_le32 (r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return retval;
486}
487
David Brownell7e27f182006-06-13 09:54:40 -0700488static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
489 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 rndis_set_cmplt_type *resp;
David Brownell7e27f182006-06-13 09:54:40 -0700492 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct rndis_params *params;
494
495 if (!r)
496 return -ENOMEM;
497 resp = (rndis_set_cmplt_type *) r->buf;
498 if (!resp)
499 return -ENOMEM;
500
David Brownell340600a2005-04-28 13:45:25 -0700501 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700502 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700503 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700504 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700505 get_unaligned_le32(&buf[i]),
506 get_unaligned_le32(&buf[i + 4]),
507 get_unaligned_le32(&buf[i + 8]),
508 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
David Brownell340600a2005-04-28 13:45:25 -0700512 params = &rndis_per_dev_params [configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 switch (OID) {
514 case OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
David Brownell340600a2005-04-28 13:45:25 -0700516 /* these NDIS_PACKET_TYPE_* bitflags are shared with
517 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
519 * PROMISCUOUS, DIRECTED,
520 * MULTICAST, ALL_MULTICAST, BROADCAST
521 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700522 *params->filter = (u16)get_unaligned_le32(buf);
David Brownell33376c12008-08-18 17:45:07 -0700523 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800524 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* this call has a significant side effect: it's
527 * what makes the packet flow start and stop, like
528 * activating the CDC Ethernet altsetting.
529 */
David Brownell340600a2005-04-28 13:45:25 -0700530 retval = 0;
531 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 params->state = RNDIS_DATA_INITIALIZED;
533 netif_carrier_on(params->dev);
534 if (netif_running(params->dev))
535 netif_wake_queue (params->dev);
536 } else {
537 params->state = RNDIS_INITIALIZED;
538 netif_carrier_off (params->dev);
539 netif_stop_queue (params->dev);
540 }
541 break;
David Brownell7e27f182006-06-13 09:54:40 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 case OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700544 /* I think we can ignore this */
David Brownell33376c12008-08-18 17:45:07 -0700545 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 retval = 0;
547 break;
David Brownell340600a2005-04-28 13:45:25 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 default:
David Brownell00274922007-11-19 12:58:36 -0800550 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800551 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
David Brownell7e27f182006-06-13 09:54:40 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return retval;
555}
556
David Brownell7e27f182006-06-13 09:54:40 -0700557/*
558 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
560
561static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
562{
David Brownell7e27f182006-06-13 09:54:40 -0700563 rndis_init_cmplt_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700565 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700566
David Brownell15b2d2b2008-06-19 18:19:16 -0700567 if (!params->dev)
568 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700571 if (!r)
572 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 resp = (rndis_init_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700574
Harvey Harrison35c26c22009-02-14 22:56:56 -0800575 resp->MessageType = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 REMOTE_NDIS_INITIALIZE_CMPLT);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800577 resp->MessageLength = cpu_to_le32 (52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800579 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
580 resp->MajorVersion = cpu_to_le32 (RNDIS_MAJOR_VERSION);
581 resp->MinorVersion = cpu_to_le32 (RNDIS_MINOR_VERSION);
582 resp->DeviceFlags = cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
583 resp->Medium = cpu_to_le32 (RNDIS_MEDIUM_802_3);
584 resp->MaxPacketsPerTransfer = cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 resp->MaxTransferSize = cpu_to_le32 (
David Brownell15b2d2b2008-06-19 18:19:16 -0700586 params->dev->mtu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 + sizeof (struct ethhdr)
588 + sizeof (struct rndis_packet_msg_type)
589 + 22);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800590 resp->PacketAlignmentFactor = cpu_to_le32 (0);
591 resp->AFListOffset = cpu_to_le32 (0);
592 resp->AFListSize = cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -0700593
David Brownell15b2d2b2008-06-19 18:19:16 -0700594 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596}
597
598static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
599{
600 rndis_query_cmplt_type *resp;
601 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700602 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700603
David Brownell33376c12008-08-18 17:45:07 -0700604 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700605 if (!params->dev)
606 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700607
Shaun Tancheff87637162006-02-22 19:47:19 -0800608 /*
609 * we need more memory:
610 * gen_ndis_query_resp expects enough space for
611 * rndis_query_cmplt_type followed by data.
612 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Shaun Tancheff87637162006-02-22 19:47:19 -0800614 r = rndis_add_response (configNr,
615 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700616 if (!r)
617 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 resp = (rndis_query_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700619
Harvey Harrison35c26c22009-02-14 22:56:56 -0800620 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700622
David Brownell340600a2005-04-28 13:45:25 -0700623 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
624 le32_to_cpu(buf->InformationBufferOffset)
625 + 8 + (u8 *) buf,
626 le32_to_cpu(buf->InformationBufferLength),
627 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* OID not supported */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800629 resp->Status = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 RNDIS_STATUS_NOT_SUPPORTED);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800631 resp->MessageLength = cpu_to_le32 (sizeof *resp);
632 resp->InformationBufferLength = cpu_to_le32 (0);
633 resp->InformationBufferOffset = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 } else
Harvey Harrison35c26c22009-02-14 22:56:56 -0800635 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700636
David Brownell15b2d2b2008-06-19 18:19:16 -0700637 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 0;
639}
640
641static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
642{
643 u32 BufLength, BufOffset;
644 rndis_set_cmplt_type *resp;
645 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700646 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700649 if (!r)
650 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 resp = (rndis_set_cmplt_type *) r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 BufLength = le32_to_cpu (buf->InformationBufferLength);
654 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
655
David Brownell15b2d2b2008-06-19 18:19:16 -0700656#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700657 pr_debug("%s: Length: %d\n", __func__, BufLength);
658 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
659 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700662 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
David Brownell7e27f182006-06-13 09:54:40 -0700664
David Brownell33376c12008-08-18 17:45:07 -0700665 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#endif
David Brownell7e27f182006-06-13 09:54:40 -0700667
Harvey Harrison35c26c22009-02-14 22:56:56 -0800668 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
669 resp->MessageLength = cpu_to_le32 (16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700671 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
672 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
Harvey Harrison35c26c22009-02-14 22:56:56 -0800673 resp->Status = cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700674 else
Harvey Harrison35c26c22009-02-14 22:56:56 -0800675 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700676
David Brownell15b2d2b2008-06-19 18:19:16 -0700677 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
679}
680
681static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
682{
683 rndis_reset_cmplt_type *resp;
684 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700685 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700688 if (!r)
689 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 resp = (rndis_reset_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700691
Harvey Harrison35c26c22009-02-14 22:56:56 -0800692 resp->MessageType = cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
693 resp->MessageLength = cpu_to_le32 (16);
694 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* resent information */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800696 resp->AddressingReset = cpu_to_le32 (1);
David Brownell7e27f182006-06-13 09:54:40 -0700697
David Brownell15b2d2b2008-06-19 18:19:16 -0700698 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
700}
701
702static int rndis_keepalive_response (int configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700703 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 rndis_keepalive_cmplt_type *resp;
706 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700707 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
710
711 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700712 if (!r)
713 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 resp = (rndis_keepalive_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700715
Harvey Harrison35c26c22009-02-14 22:56:56 -0800716 resp->MessageType = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 REMOTE_NDIS_KEEPALIVE_CMPLT);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800718 resp->MessageLength = cpu_to_le32 (16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Harvey Harrison35c26c22009-02-14 22:56:56 -0800720 resp->Status = cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700721
David Brownell15b2d2b2008-06-19 18:19:16 -0700722 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 0;
724}
725
726
David Brownell7e27f182006-06-13 09:54:40 -0700727/*
728 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
730static int rndis_indicate_status_msg (int configNr, u32 status)
731{
David Brownell7e27f182006-06-13 09:54:40 -0700732 rndis_indicate_status_msg_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700734 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700735
David Brownell15b2d2b2008-06-19 18:19:16 -0700736 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700737 return -ENOTSUPP;
738
739 r = rndis_add_response (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 sizeof (rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700741 if (!r)
742 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 resp = (rndis_indicate_status_msg_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700744
Harvey Harrison35c26c22009-02-14 22:56:56 -0800745 resp->MessageType = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 REMOTE_NDIS_INDICATE_STATUS_MSG);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800747 resp->MessageLength = cpu_to_le32 (20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 resp->Status = cpu_to_le32 (status);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800749 resp->StatusBufferLength = cpu_to_le32 (0);
750 resp->StatusBufferOffset = cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -0700751
David Brownell15b2d2b2008-06-19 18:19:16 -0700752 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754}
755
756int rndis_signal_connect (int configNr)
757{
758 rndis_per_dev_params [configNr].media_state
759 = NDIS_MEDIA_STATE_CONNECTED;
David Brownell7e27f182006-06-13 09:54:40 -0700760 return rndis_indicate_status_msg (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 RNDIS_STATUS_MEDIA_CONNECT);
762}
763
764int rndis_signal_disconnect (int configNr)
765{
766 rndis_per_dev_params [configNr].media_state
767 = NDIS_MEDIA_STATE_DISCONNECTED;
768 return rndis_indicate_status_msg (configNr,
769 RNDIS_STATUS_MEDIA_DISCONNECT);
770}
771
David Brownell340600a2005-04-28 13:45:25 -0700772void rndis_uninit (int configNr)
773{
David Brownell486e2df2005-05-24 17:51:52 -0700774 u8 *buf;
775 u32 length;
776
David Brownell340600a2005-04-28 13:45:25 -0700777 if (configNr >= RNDIS_MAX_CONFIGS)
778 return;
David Brownell340600a2005-04-28 13:45:25 -0700779 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700780
781 /* drain the response queue */
782 while ((buf = rndis_get_next_response(configNr, &length)))
783 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786void rndis_set_host_mac (int configNr, const u8 *addr)
787{
788 rndis_per_dev_params [configNr].host_mac = addr;
789}
790
David Brownell7e27f182006-06-13 09:54:40 -0700791/*
792 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
794int rndis_msg_parser (u8 configNr, u8 *buf)
795{
796 u32 MsgType, MsgLength;
797 __le32 *tmp;
798 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!buf)
801 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700802
803 tmp = (__le32 *) buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700804 MsgType = get_unaligned_le32(tmp++);
805 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (configNr >= RNDIS_MAX_CONFIGS)
808 return -ENOTSUPP;
809 params = &rndis_per_dev_params [configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700810
David Brownell340600a2005-04-28 13:45:25 -0700811 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
812 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
813 * and normal HC level polling to see if there's any IN traffic.
814 */
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700817 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700819 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800820 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 params->state = RNDIS_INITIALIZED;
822 return rndis_init_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700823 (rndis_init_msg_type *) buf);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 case REMOTE_NDIS_HALT_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700826 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800827 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 params->state = RNDIS_UNINITIALIZED;
829 if (params->dev) {
830 netif_carrier_off (params->dev);
831 netif_stop_queue (params->dev);
832 }
833 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 case REMOTE_NDIS_QUERY_MSG:
David Brownell7e27f182006-06-13 09:54:40 -0700836 return rndis_query_response (configNr,
837 (rndis_query_msg_type *) buf);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 case REMOTE_NDIS_SET_MSG:
David Brownell7e27f182006-06-13 09:54:40 -0700840 return rndis_set_response (configNr,
841 (rndis_set_msg_type *) buf);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 case REMOTE_NDIS_RESET_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700844 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800845 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return rndis_reset_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700847 (rndis_reset_msg_type *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 case REMOTE_NDIS_KEEPALIVE_MSG:
850 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700851 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700852 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800853 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return rndis_keepalive_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700855 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700857
858 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /* At least Windows XP emits some undefined RNDIS messages.
860 * In one case those messages seemed to relate to the host
861 * suspending itself.
862 */
David Brownell00274922007-11-19 12:58:36 -0800863 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 __func__ , MsgType, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 {
866 unsigned i;
867 for (i = 0; i < MsgLength; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700868 pr_debug("%03d: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 " %02x %02x %02x %02x"
870 " %02x %02x %02x %02x"
871 " %02x %02x %02x %02x"
872 " %02x %02x %02x %02x"
873 "\n",
874 i,
875 buf[i], buf [i+1],
876 buf[i+2], buf[i+3],
877 buf[i+4], buf [i+5],
878 buf[i+6], buf[i+7],
879 buf[i+8], buf [i+9],
880 buf[i+10], buf[i+11],
881 buf[i+12], buf [i+13],
882 buf[i+14], buf[i+15]);
883 }
884 }
885 break;
886 }
David Brownell7e27f182006-06-13 09:54:40 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -ENOTSUPP;
889}
890
David Brownell15b2d2b2008-06-19 18:19:16 -0700891int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700894
David Brownell15b2d2b2008-06-19 18:19:16 -0700895 if (!resp_avail)
896 return -EINVAL;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
899 if (!rndis_per_dev_params [i].used) {
900 rndis_per_dev_params [i].used = 1;
David Brownell15b2d2b2008-06-19 18:19:16 -0700901 rndis_per_dev_params [i].resp_avail = resp_avail;
902 rndis_per_dev_params [i].v = v;
David Brownell33376c12008-08-18 17:45:07 -0700903 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return i;
905 }
906 }
David Brownell33376c12008-08-18 17:45:07 -0700907 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700908
David Brownell15b2d2b2008-06-19 18:19:16 -0700909 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912void rndis_deregister (int configNr)
913{
David Brownell33376c12008-08-18 17:45:07 -0700914 pr_debug("%s: \n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (configNr >= RNDIS_MAX_CONFIGS) return;
917 rndis_per_dev_params [configNr].used = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return;
920}
921
David Brownell15b2d2b2008-06-19 18:19:16 -0700922int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
David Brownell33376c12008-08-18 17:45:07 -0700924 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700925 if (!dev)
926 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 rndis_per_dev_params [configNr].dev = dev;
David Brownell340600a2005-04-28 13:45:25 -0700930 rndis_per_dev_params [configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
934
935int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
936{
David Brownell33376c12008-08-18 17:45:07 -0700937 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!vendorDescr) return -1;
939 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 rndis_per_dev_params [configNr].vendorID = vendorID;
942 rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945}
946
947int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
948{
David Brownell33376c12008-08-18 17:45:07 -0700949 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 rndis_per_dev_params [configNr].medium = medium;
953 rndis_per_dev_params [configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956}
957
958void rndis_add_hdr (struct sk_buff *skb)
959{
960 struct rndis_packet_msg_type *header;
961
962 if (!skb)
963 return;
964 header = (void *) skb_push (skb, sizeof *header);
965 memset (header, 0, sizeof *header);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800966 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 header->MessageLength = cpu_to_le32(skb->len);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800968 header->DataOffset = cpu_to_le32 (36);
David Brownell6cdee102005-04-18 17:39:34 -0700969 header->DataLength = cpu_to_le32(skb->len - sizeof *header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972void rndis_free_response (int configNr, u8 *buf)
973{
974 rndis_resp_t *r;
975 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700976
977 list_for_each_safe (act, tmp,
978 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 {
980 r = list_entry (act, rndis_resp_t, list);
981 if (r && r->buf == buf) {
982 list_del (&r->list);
983 kfree (r);
984 }
985 }
986}
987
988u8 *rndis_get_next_response (int configNr, u32 *length)
989{
990 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700991 struct list_head *act, *tmp;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700994
995 list_for_each_safe (act, tmp,
996 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 {
998 r = list_entry (act, rndis_resp_t, list);
999 if (!r->send) {
1000 r->send = 1;
1001 *length = r->length;
1002 return r->buf;
1003 }
1004 }
David Brownell7e27f182006-06-13 09:54:40 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return NULL;
1007}
1008
1009static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1010{
1011 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001012
David Brownell340600a2005-04-28 13:45:25 -07001013 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1015 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 r->buf = (u8 *) (r + 1);
1018 r->length = length;
1019 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001020
1021 list_add_tail (&r->list,
1022 &(rndis_per_dev_params [configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return r;
1024}
1025
David Brownell6cdee102005-04-18 17:39:34 -07001026int rndis_rm_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
David Brownell6cdee102005-04-18 17:39:34 -07001028 /* tmp points to a struct rndis_packet_msg_type */
1029 __le32 *tmp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
David Brownell6cdee102005-04-18 17:39:34 -07001031 /* MessageType, MessageLength */
Harvey Harrison35c26c22009-02-14 22:56:56 -08001032 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
David Brownell6cdee102005-04-18 17:39:34 -07001033 != get_unaligned(tmp++))
1034 return -EINVAL;
1035 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
David Brownell6cdee102005-04-18 17:39:34 -07001037 /* DataOffset, DataLength */
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001038 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8))
David Brownell6cdee102005-04-18 17:39:34 -07001039 return -EOVERFLOW;
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001040 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 0;
1043}
1044
1045#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1046
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001047static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001049 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001050
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001051 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 "Config Nr. %d\n"
1053 "used : %s\n"
1054 "state : %s\n"
1055 "medium : 0x%08X\n"
1056 "speed : %d\n"
1057 "cable : %s\n"
1058 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001059 "vendor : %s\n",
1060 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ({ char *s = "?";
1062 switch (param->state) {
1063 case RNDIS_UNINITIALIZED:
1064 s = "RNDIS_UNINITIALIZED"; break;
1065 case RNDIS_INITIALIZED:
1066 s = "RNDIS_INITIALIZED"; break;
1067 case RNDIS_DATA_INITIALIZED:
1068 s = "RNDIS_DATA_INITIALIZED"; break;
1069 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001070 param->medium,
1071 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001073 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001077static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1078 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001080 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 u32 speed = 0;
1082 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 for (i = 0; i < count; i++) {
1085 char c;
1086 if (get_user(c, buffer))
1087 return -EFAULT;
1088 switch (c) {
1089 case '0':
1090 case '1':
1091 case '2':
1092 case '3':
1093 case '4':
1094 case '5':
1095 case '6':
1096 case '7':
1097 case '8':
1098 case '9':
1099 fl_speed = 1;
1100 speed = speed*10 + c - '0';
1101 break;
1102 case 'C':
1103 case 'c':
1104 rndis_signal_connect (p->confignr);
1105 break;
1106 case 'D':
1107 case 'd':
1108 rndis_signal_disconnect(p->confignr);
1109 break;
David Brownell7e27f182006-06-13 09:54:40 -07001110 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001112 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114 }
David Brownell7e27f182006-06-13 09:54:40 -07001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 buffer++;
1117 }
David Brownell7e27f182006-06-13 09:54:40 -07001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return count;
1120}
1121
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001122static int rndis_proc_open(struct inode *inode, struct file *file)
1123{
1124 return single_open(file, rndis_proc_show, PDE(inode)->data);
1125}
1126
1127static const struct file_operations rndis_proc_fops = {
1128 .owner = THIS_MODULE,
1129 .open = rndis_proc_open,
1130 .read = seq_read,
1131 .llseek = seq_lseek,
1132 .release = single_release,
1133 .write = rndis_proc_write,
1134};
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136#define NAME_TEMPLATE "driver/rndis-%03d"
1137
1138static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1139
1140#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1141
1142
David Brownell0e530b42008-04-05 14:17:14 -07001143int __init rndis_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 u8 i;
1146
1147 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1148#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1149 char name [20];
1150
1151 sprintf (name, NAME_TEMPLATE, i);
1152 if (!(rndis_connect_state [i]
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001153 = proc_create_data(name, 0660, NULL,
1154 &rndis_proc_fops,
1155 (void *)(rndis_per_dev_params + i))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 {
David Brownell33376c12008-08-18 17:45:07 -07001157 pr_debug("%s :remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 while (i) {
1159 sprintf (name, NAME_TEMPLATE, --i);
1160 remove_proc_entry (name, NULL);
1161 }
David Brownell33376c12008-08-18 17:45:07 -07001162 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return -EIO;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165#endif
1166 rndis_per_dev_params [i].confignr = i;
1167 rndis_per_dev_params [i].used = 0;
1168 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1169 rndis_per_dev_params [i].media_state
1170 = NDIS_MEDIA_STATE_DISCONNECTED;
1171 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1172 }
David Brownell7e27f182006-06-13 09:54:40 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 0;
1175}
1176
1177void rndis_exit (void)
1178{
1179#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1180 u8 i;
1181 char name [20];
David Brownell7e27f182006-06-13 09:54:40 -07001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1184 sprintf (name, NAME_TEMPLATE, i);
1185 remove_proc_entry (name, NULL);
1186 }
1187#endif
1188}
1189