blob: 7228e856223626dab7e568eb0f1252391758a95b [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
David Brownell00274922007-11-19 12:58:36 -080060#define DBG(str,args...) do { \
61 if (rndis_debug) \
62 pr_debug(str , ## args); \
63 } while (0)
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define RNDIS_MAX_CONFIGS 1
66
67
68static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
69
70/* Driver Version */
71static const __le32 rndis_driver_version = __constant_cpu_to_le32 (1);
72
73/* Function Prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static rndis_resp_t *rndis_add_response (int configNr, u32 length);
75
76
David Brownell340600a2005-04-28 13:45:25 -070077/* supported OIDs */
David Brownell7e27f182006-06-13 09:54:40 -070078static const u32 oid_supported_list [] =
David Brownell340600a2005-04-28 13:45:25 -070079{
80 /* the general stuff */
81 OID_GEN_SUPPORTED_LIST,
82 OID_GEN_HARDWARE_STATUS,
83 OID_GEN_MEDIA_SUPPORTED,
84 OID_GEN_MEDIA_IN_USE,
85 OID_GEN_MAXIMUM_FRAME_SIZE,
86 OID_GEN_LINK_SPEED,
87 OID_GEN_TRANSMIT_BLOCK_SIZE,
88 OID_GEN_RECEIVE_BLOCK_SIZE,
89 OID_GEN_VENDOR_ID,
90 OID_GEN_VENDOR_DESCRIPTION,
91 OID_GEN_VENDOR_DRIVER_VERSION,
92 OID_GEN_CURRENT_PACKET_FILTER,
93 OID_GEN_MAXIMUM_TOTAL_SIZE,
94 OID_GEN_MEDIA_CONNECT_STATUS,
95 OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070096
David Brownell340600a2005-04-28 13:45:25 -070097 /* the statistical stuff */
98 OID_GEN_XMIT_OK,
99 OID_GEN_RCV_OK,
100 OID_GEN_XMIT_ERROR,
101 OID_GEN_RCV_ERROR,
102 OID_GEN_RCV_NO_BUFFER,
103#ifdef RNDIS_OPTIONAL_STATS
104 OID_GEN_DIRECTED_BYTES_XMIT,
105 OID_GEN_DIRECTED_FRAMES_XMIT,
106 OID_GEN_MULTICAST_BYTES_XMIT,
107 OID_GEN_MULTICAST_FRAMES_XMIT,
108 OID_GEN_BROADCAST_BYTES_XMIT,
109 OID_GEN_BROADCAST_FRAMES_XMIT,
110 OID_GEN_DIRECTED_BYTES_RCV,
111 OID_GEN_DIRECTED_FRAMES_RCV,
112 OID_GEN_MULTICAST_BYTES_RCV,
113 OID_GEN_MULTICAST_FRAMES_RCV,
114 OID_GEN_BROADCAST_BYTES_RCV,
115 OID_GEN_BROADCAST_FRAMES_RCV,
116 OID_GEN_RCV_CRC_ERROR,
117 OID_GEN_TRANSMIT_QUEUE_LENGTH,
118#endif /* RNDIS_OPTIONAL_STATS */
119
David Brownell7e27f182006-06-13 09:54:40 -0700120 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700121 /* the general stuff */
122 OID_802_3_PERMANENT_ADDRESS,
123 OID_802_3_CURRENT_ADDRESS,
124 OID_802_3_MULTICAST_LIST,
125 OID_802_3_MAC_OPTIONS,
126 OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700127
David Brownell340600a2005-04-28 13:45:25 -0700128 /* the statistical stuff */
129 OID_802_3_RCV_ERROR_ALIGNMENT,
130 OID_802_3_XMIT_ONE_COLLISION,
131 OID_802_3_XMIT_MORE_COLLISIONS,
132#ifdef RNDIS_OPTIONAL_STATS
133 OID_802_3_XMIT_DEFERRED,
134 OID_802_3_XMIT_MAX_COLLISIONS,
135 OID_802_3_RCV_OVERRUN,
136 OID_802_3_XMIT_UNDERRUN,
137 OID_802_3_XMIT_HEARTBEAT_FAILURE,
138 OID_802_3_XMIT_TIMES_CRS_LOST,
139 OID_802_3_XMIT_LATE_COLLISIONS,
140#endif /* RNDIS_OPTIONAL_STATS */
141
142#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700143 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
144 * don't say what they mean ... and the NDIS specs are often
145 * confusing and/or ambiguous in this context. (That is, more
146 * so than their specs for the other OIDs.)
147 *
148 * FIXME someone who knows what these should do, please
149 * implement them!
150 */
David Brownell340600a2005-04-28 13:45:25 -0700151
152 /* power management */
153 OID_PNP_CAPABILITIES,
154 OID_PNP_QUERY_POWER,
155 OID_PNP_SET_POWER,
156
157#ifdef RNDIS_WAKEUP
158 /* wake up host */
159 OID_PNP_ENABLE_WAKE_UP,
160 OID_PNP_ADD_WAKE_UP_PATTERN,
161 OID_PNP_REMOVE_WAKE_UP_PATTERN,
162#endif /* RNDIS_WAKEUP */
163#endif /* RNDIS_PM */
164};
165
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* NDIS Functions */
David Brownell340600a2005-04-28 13:45:25 -0700168static int
169gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
170 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
David Brownell7e27f182006-06-13 09:54:40 -0700172 int retval = -ENOTSUPP;
173 u32 length = 4; /* usually */
David Brownell340600a2005-04-28 13:45:25 -0700174 __le32 *outbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int i, count;
176 rndis_query_cmplt_type *resp;
David Brownell15b2d2b2008-06-19 18:19:16 -0700177 struct net_device *net;
178 struct net_device_stats *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (!r) return -ENOMEM;
181 resp = (rndis_query_cmplt_type *) r->buf;
182
183 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700184
185 if (buf_len && rndis_debug > 1) {
David Brownell70790f62007-07-01 17:35:28 -0700186 DBG("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700187 for (i = 0; i < buf_len; i += 16) {
David Brownell70790f62007-07-01 17:35:28 -0700188 DBG("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700189 get_unaligned_le32(&buf[i]),
190 get_unaligned_le32(&buf[i + 4]),
191 get_unaligned_le32(&buf[i + 8]),
192 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700193 }
194 }
195
196 /* response goes here, right after the header */
197 outbuf = (__le32 *) &resp[1];
198 resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
199
David Brownell15b2d2b2008-06-19 18:19:16 -0700200 net = rndis_per_dev_params[configNr].dev;
201 if (net->get_stats)
202 stats = net->get_stats(net);
203 else
204 stats = NULL;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 switch (OID) {
207
208 /* general oids (table 4-1) */
209
210 /* mandatory */
211 case OID_GEN_SUPPORTED_LIST:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800212 DBG("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 length = sizeof (oid_supported_list);
214 count = length / sizeof (u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 for (i = 0; i < count; i++)
David Brownell340600a2005-04-28 13:45:25 -0700216 outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 retval = 0;
218 break;
David Brownell7e27f182006-06-13 09:54:40 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* mandatory */
221 case OID_GEN_HARDWARE_STATUS:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800222 DBG("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700223 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700225 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * reddite ergo quae sunt Caesaris Caesari
227 * et quae sunt Dei Deo!
228 */
David Brownell340600a2005-04-28 13:45:25 -0700229 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 retval = 0;
231 break;
David Brownell7e27f182006-06-13 09:54:40 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* mandatory */
234 case OID_GEN_MEDIA_SUPPORTED:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800235 DBG("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
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_MEDIA_IN_USE:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800242 DBG("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* one medium, one transport... (maybe you do it better) */
David Brownell340600a2005-04-28 13:45:25 -0700244 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 retval = 0;
246 break;
David Brownell7e27f182006-06-13 09:54:40 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* mandatory */
249 case OID_GEN_MAXIMUM_FRAME_SIZE:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800250 DBG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700252 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 rndis_per_dev_params [configNr].dev->mtu);
254 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 break;
David Brownell7e27f182006-06-13 09:54:40 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* mandatory */
259 case OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700260 if (rndis_debug > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800261 DBG("%s: OID_GEN_LINK_SPEED\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (rndis_per_dev_params [configNr].media_state
David Brownell340600a2005-04-28 13:45:25 -0700263 == NDIS_MEDIA_STATE_DISCONNECTED)
264 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 else
David Brownell340600a2005-04-28 13:45:25 -0700266 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 rndis_per_dev_params [configNr].speed);
268 retval = 0;
269 break;
270
271 /* mandatory */
272 case OID_GEN_TRANSMIT_BLOCK_SIZE:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800273 DBG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700275 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 rndis_per_dev_params [configNr].dev->mtu);
277 retval = 0;
278 }
279 break;
David Brownell7e27f182006-06-13 09:54:40 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* mandatory */
282 case OID_GEN_RECEIVE_BLOCK_SIZE:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800283 DBG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (rndis_per_dev_params [configNr].dev) {
David Brownell340600a2005-04-28 13:45:25 -0700285 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 rndis_per_dev_params [configNr].dev->mtu);
287 retval = 0;
288 }
289 break;
David Brownell7e27f182006-06-13 09:54:40 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* mandatory */
292 case OID_GEN_VENDOR_ID:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800293 DBG("%s: OID_GEN_VENDOR_ID\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700294 *outbuf = cpu_to_le32 (
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 rndis_per_dev_params [configNr].vendorID);
296 retval = 0;
297 break;
David Brownell7e27f182006-06-13 09:54:40 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* mandatory */
300 case OID_GEN_VENDOR_DESCRIPTION:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800301 DBG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
David Brownell340600a2005-04-28 13:45:25 -0700303 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 rndis_per_dev_params [configNr].vendorDescr, length);
305 retval = 0;
306 break;
307
308 case OID_GEN_VENDOR_DRIVER_VERSION:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800309 DBG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700311 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 retval = 0;
313 break;
314
315 /* mandatory */
316 case OID_GEN_CURRENT_PACKET_FILTER:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800317 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700318 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 retval = 0;
320 break;
321
322 /* mandatory */
323 case OID_GEN_MAXIMUM_TOTAL_SIZE:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800324 DBG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700325 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 retval = 0;
327 break;
328
329 /* mandatory */
330 case OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700331 if (rndis_debug > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800332 DBG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700333 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 .media_state);
335 retval = 0;
336 break;
337
338 case OID_GEN_PHYSICAL_MEDIUM:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800339 DBG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700340 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 retval = 0;
342 break;
343
344 /* The RNDIS specification is incomplete/wrong. Some versions
345 * of MS-Windows expect OIDs that aren't specified there. Other
346 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
347 */
348 case OID_GEN_MAC_OPTIONS: /* from WinME */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800349 DBG("%s: OID_GEN_MAC_OPTIONS\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700350 *outbuf = __constant_cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
352 | NDIS_MAC_OPTION_FULL_DUPLEX);
353 retval = 0;
354 break;
355
356 /* statistics OIDs (table 4-2) */
357
358 /* mandatory */
359 case OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700360 if (rndis_debug > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800361 DBG("%s: OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700362 if (stats) {
363 *outbuf = cpu_to_le32(stats->tx_packets
364 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 break;
368
369 /* mandatory */
370 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700371 if (rndis_debug > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800372 DBG("%s: OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700373 if (stats) {
374 *outbuf = cpu_to_le32(stats->rx_packets
375 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 break;
David Brownell7e27f182006-06-13 09:54:40 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* mandatory */
381 case OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700382 if (rndis_debug > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800383 DBG("%s: OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700384 if (stats) {
385 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 break;
David Brownell7e27f182006-06-13 09:54:40 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* mandatory */
391 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700392 if (rndis_debug > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800393 DBG("%s: OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700394 if (stats) {
395 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 break;
David Brownell7e27f182006-06-13 09:54:40 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* mandatory */
401 case OID_GEN_RCV_NO_BUFFER:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800402 DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700403 if (stats) {
404 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407 break;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* ieee802.3 OIDs (table 4-3) */
410
411 /* mandatory */
412 case OID_802_3_PERMANENT_ADDRESS:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800413 DBG("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (rndis_per_dev_params [configNr].dev) {
415 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700416 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 rndis_per_dev_params [configNr].host_mac,
418 length);
419 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 break;
David Brownell7e27f182006-06-13 09:54:40 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* mandatory */
424 case OID_802_3_CURRENT_ADDRESS:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800425 DBG("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (rndis_per_dev_params [configNr].dev) {
427 length = ETH_ALEN;
David Brownell340600a2005-04-28 13:45:25 -0700428 memcpy (outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 rndis_per_dev_params [configNr].host_mac,
430 length);
431 retval = 0;
432 }
433 break;
David Brownell7e27f182006-06-13 09:54:40 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* mandatory */
436 case OID_802_3_MULTICAST_LIST:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* Multicast base address only */
David Brownell340600a2005-04-28 13:45:25 -0700439 *outbuf = __constant_cpu_to_le32 (0xE0000000);
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 /* mandatory */
444 case OID_802_3_MAXIMUM_LIST_SIZE:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800445 DBG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* Multicast base address only */
David Brownell340600a2005-04-28 13:45:25 -0700447 *outbuf = __constant_cpu_to_le32 (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 retval = 0;
449 break;
David Brownell7e27f182006-06-13 09:54:40 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 case OID_802_3_MAC_OPTIONS:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800452 DBG("%s: OID_802_3_MAC_OPTIONS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454
455 /* ieee802.3 statistics OIDs (table 4-4) */
456
457 /* mandatory */
458 case OID_802_3_RCV_ERROR_ALIGNMENT:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800459 DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700460 if (stats) {
461 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 retval = 0;
463 }
464 break;
David Brownell7e27f182006-06-13 09:54:40 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* mandatory */
467 case OID_802_3_XMIT_ONE_COLLISION:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800468 DBG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700469 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 retval = 0;
471 break;
David Brownell7e27f182006-06-13 09:54:40 -0700472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* mandatory */
474 case OID_802_3_XMIT_MORE_COLLISIONS:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800475 DBG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
David Brownell340600a2005-04-28 13:45:25 -0700476 *outbuf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 retval = 0;
478 break;
David Brownell7e27f182006-06-13 09:54:40 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 default:
David Brownell00274922007-11-19 12:58:36 -0800481 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800482 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
David Brownell340600a2005-04-28 13:45:25 -0700484 if (retval < 0)
485 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 resp->InformationBufferLength = cpu_to_le32 (length);
David Brownell340600a2005-04-28 13:45:25 -0700488 r->length = length + sizeof *resp;
489 resp->MessageLength = cpu_to_le32 (r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return retval;
491}
492
David Brownell7e27f182006-06-13 09:54:40 -0700493static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
494 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 rndis_set_cmplt_type *resp;
David Brownell7e27f182006-06-13 09:54:40 -0700497 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct rndis_params *params;
499
500 if (!r)
501 return -ENOMEM;
502 resp = (rndis_set_cmplt_type *) r->buf;
503 if (!resp)
504 return -ENOMEM;
505
David Brownell340600a2005-04-28 13:45:25 -0700506 if (buf_len && rndis_debug > 1) {
David Brownell70790f62007-07-01 17:35:28 -0700507 DBG("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700508 for (i = 0; i < buf_len; i += 16) {
David Brownell70790f62007-07-01 17:35:28 -0700509 DBG("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700510 get_unaligned_le32(&buf[i]),
511 get_unaligned_le32(&buf[i + 4]),
512 get_unaligned_le32(&buf[i + 8]),
513 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
David Brownell340600a2005-04-28 13:45:25 -0700517 params = &rndis_per_dev_params [configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 switch (OID) {
519 case OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David Brownell340600a2005-04-28 13:45:25 -0700521 /* these NDIS_PACKET_TYPE_* bitflags are shared with
522 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
524 * PROMISCUOUS, DIRECTED,
525 * MULTICAST, ALL_MULTICAST, BROADCAST
526 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700527 *params->filter = (u16)get_unaligned_le32(buf);
David Brownell70790f62007-07-01 17:35:28 -0700528 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800529 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* this call has a significant side effect: it's
532 * what makes the packet flow start and stop, like
533 * activating the CDC Ethernet altsetting.
534 */
David Brownell340600a2005-04-28 13:45:25 -0700535 retval = 0;
536 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 params->state = RNDIS_DATA_INITIALIZED;
538 netif_carrier_on(params->dev);
539 if (netif_running(params->dev))
540 netif_wake_queue (params->dev);
541 } else {
542 params->state = RNDIS_INITIALIZED;
543 netif_carrier_off (params->dev);
544 netif_stop_queue (params->dev);
545 }
546 break;
David Brownell7e27f182006-06-13 09:54:40 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 case OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700549 /* I think we can ignore this */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800550 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 retval = 0;
552 break;
David Brownell340600a2005-04-28 13:45:25 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 default:
David Brownell00274922007-11-19 12:58:36 -0800555 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800556 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
David Brownell7e27f182006-06-13 09:54:40 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return retval;
560}
561
David Brownell7e27f182006-06-13 09:54:40 -0700562/*
563 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
565
566static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
567{
David Brownell7e27f182006-06-13 09:54:40 -0700568 rndis_init_cmplt_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700570 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700571
David Brownell15b2d2b2008-06-19 18:19:16 -0700572 if (!params->dev)
573 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700576 if (!r)
577 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 resp = (rndis_init_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 resp->MessageType = __constant_cpu_to_le32 (
581 REMOTE_NDIS_INITIALIZE_CMPLT);
582 resp->MessageLength = __constant_cpu_to_le32 (52);
583 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
584 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
585 resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
586 resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
587 resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
588 resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
589 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
590 resp->MaxTransferSize = cpu_to_le32 (
David Brownell15b2d2b2008-06-19 18:19:16 -0700591 params->dev->mtu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 + sizeof (struct ethhdr)
593 + sizeof (struct rndis_packet_msg_type)
594 + 22);
595 resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
596 resp->AFListOffset = __constant_cpu_to_le32 (0);
597 resp->AFListSize = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -0700598
David Brownell15b2d2b2008-06-19 18:19:16 -0700599 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return 0;
601}
602
603static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
604{
605 rndis_query_cmplt_type *resp;
606 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700607 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700608
Harvey Harrison441b62c2008-03-03 16:08:34 -0800609 // DBG("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID));
David Brownell15b2d2b2008-06-19 18:19:16 -0700610 if (!params->dev)
611 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700612
Shaun Tancheff87637162006-02-22 19:47:19 -0800613 /*
614 * we need more memory:
615 * gen_ndis_query_resp expects enough space for
616 * rndis_query_cmplt_type followed by data.
617 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Shaun Tancheff87637162006-02-22 19:47:19 -0800619 r = rndis_add_response (configNr,
620 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700621 if (!r)
622 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 resp = (rndis_query_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700627
David Brownell340600a2005-04-28 13:45:25 -0700628 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
629 le32_to_cpu(buf->InformationBufferOffset)
630 + 8 + (u8 *) buf,
631 le32_to_cpu(buf->InformationBufferLength),
632 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* OID not supported */
634 resp->Status = __constant_cpu_to_le32 (
635 RNDIS_STATUS_NOT_SUPPORTED);
David Brownell340600a2005-04-28 13:45:25 -0700636 resp->MessageLength = __constant_cpu_to_le32 (sizeof *resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 resp->InformationBufferLength = __constant_cpu_to_le32 (0);
638 resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
639 } else
640 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700641
David Brownell15b2d2b2008-06-19 18:19:16 -0700642 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
644}
645
646static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
647{
648 u32 BufLength, BufOffset;
649 rndis_set_cmplt_type *resp;
650 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700651 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700654 if (!r)
655 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 resp = (rndis_set_cmplt_type *) r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 BufLength = le32_to_cpu (buf->InformationBufferLength);
659 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
660
David Brownell15b2d2b2008-06-19 18:19:16 -0700661#ifdef VERBOSE_DEBUG
Harvey Harrison441b62c2008-03-03 16:08:34 -0800662 DBG("%s: Length: %d\n", __func__, BufLength);
663 DBG("%s: Offset: %d\n", __func__, BufOffset);
664 DBG("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 for (i = 0; i < BufLength; i++) {
David Brownell70790f62007-07-01 17:35:28 -0700667 DBG("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
David Brownell7e27f182006-06-13 09:54:40 -0700669
David Brownell70790f62007-07-01 17:35:28 -0700670 DBG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#endif
David Brownell7e27f182006-06-13 09:54:40 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
674 resp->MessageLength = __constant_cpu_to_le32 (16);
675 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700676 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
677 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
678 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
679 else
680 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
681
David Brownell15b2d2b2008-06-19 18:19:16 -0700682 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
685
686static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
687{
688 rndis_reset_cmplt_type *resp;
689 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700690 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700693 if (!r)
694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 resp = (rndis_reset_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
698 resp->MessageLength = __constant_cpu_to_le32 (16);
699 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
700 /* resent information */
701 resp->AddressingReset = __constant_cpu_to_le32 (1);
David Brownell7e27f182006-06-13 09:54:40 -0700702
David Brownell15b2d2b2008-06-19 18:19:16 -0700703 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705}
706
707static int rndis_keepalive_response (int configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700708 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 rndis_keepalive_cmplt_type *resp;
711 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700712 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
715
716 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700717 if (!r)
718 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 resp = (rndis_keepalive_cmplt_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 resp->MessageType = __constant_cpu_to_le32 (
722 REMOTE_NDIS_KEEPALIVE_CMPLT);
723 resp->MessageLength = __constant_cpu_to_le32 (16);
724 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
725 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700726
David Brownell15b2d2b2008-06-19 18:19:16 -0700727 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729}
730
731
David Brownell7e27f182006-06-13 09:54:40 -0700732/*
733 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
735static int rndis_indicate_status_msg (int configNr, u32 status)
736{
David Brownell7e27f182006-06-13 09:54:40 -0700737 rndis_indicate_status_msg_type *resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 rndis_resp_t *r;
David Brownell15b2d2b2008-06-19 18:19:16 -0700739 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700740
David Brownell15b2d2b2008-06-19 18:19:16 -0700741 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700742 return -ENOTSUPP;
743
744 r = rndis_add_response (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 sizeof (rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700746 if (!r)
747 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 resp = (rndis_indicate_status_msg_type *) r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 resp->MessageType = __constant_cpu_to_le32 (
751 REMOTE_NDIS_INDICATE_STATUS_MSG);
752 resp->MessageLength = __constant_cpu_to_le32 (20);
753 resp->Status = cpu_to_le32 (status);
754 resp->StatusBufferLength = __constant_cpu_to_le32 (0);
755 resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
David Brownell7e27f182006-06-13 09:54:40 -0700756
David Brownell15b2d2b2008-06-19 18:19:16 -0700757 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759}
760
761int rndis_signal_connect (int configNr)
762{
763 rndis_per_dev_params [configNr].media_state
764 = NDIS_MEDIA_STATE_CONNECTED;
David Brownell7e27f182006-06-13 09:54:40 -0700765 return rndis_indicate_status_msg (configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 RNDIS_STATUS_MEDIA_CONNECT);
767}
768
769int rndis_signal_disconnect (int configNr)
770{
771 rndis_per_dev_params [configNr].media_state
772 = NDIS_MEDIA_STATE_DISCONNECTED;
773 return rndis_indicate_status_msg (configNr,
774 RNDIS_STATUS_MEDIA_DISCONNECT);
775}
776
David Brownell340600a2005-04-28 13:45:25 -0700777void rndis_uninit (int configNr)
778{
David Brownell486e2df2005-05-24 17:51:52 -0700779 u8 *buf;
780 u32 length;
781
David Brownell340600a2005-04-28 13:45:25 -0700782 if (configNr >= RNDIS_MAX_CONFIGS)
783 return;
David Brownell340600a2005-04-28 13:45:25 -0700784 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700785
786 /* drain the response queue */
787 while ((buf = rndis_get_next_response(configNr, &length)))
788 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791void rndis_set_host_mac (int configNr, const u8 *addr)
792{
793 rndis_per_dev_params [configNr].host_mac = addr;
794}
795
David Brownell7e27f182006-06-13 09:54:40 -0700796/*
797 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
799int rndis_msg_parser (u8 configNr, u8 *buf)
800{
801 u32 MsgType, MsgLength;
802 __le32 *tmp;
803 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!buf)
806 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700807
808 tmp = (__le32 *) buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700809 MsgType = get_unaligned_le32(tmp++);
810 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (configNr >= RNDIS_MAX_CONFIGS)
813 return -ENOTSUPP;
814 params = &rndis_per_dev_params [configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700815
David Brownell340600a2005-04-28 13:45:25 -0700816 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
817 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
818 * and normal HC level polling to see if there's any IN traffic.
819 */
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700822 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell70790f62007-07-01 17:35:28 -0700824 DBG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800825 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 params->state = RNDIS_INITIALIZED;
827 return rndis_init_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700828 (rndis_init_msg_type *) buf);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 case REMOTE_NDIS_HALT_MSG:
David Brownell70790f62007-07-01 17:35:28 -0700831 DBG("%s: REMOTE_NDIS_HALT_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800832 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 params->state = RNDIS_UNINITIALIZED;
834 if (params->dev) {
835 netif_carrier_off (params->dev);
836 netif_stop_queue (params->dev);
837 }
838 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 case REMOTE_NDIS_QUERY_MSG:
David Brownell7e27f182006-06-13 09:54:40 -0700841 return rndis_query_response (configNr,
842 (rndis_query_msg_type *) buf);
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 case REMOTE_NDIS_SET_MSG:
David Brownell7e27f182006-06-13 09:54:40 -0700845 return rndis_set_response (configNr,
846 (rndis_set_msg_type *) buf);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 case REMOTE_NDIS_RESET_MSG:
David Brownell70790f62007-07-01 17:35:28 -0700849 DBG("%s: REMOTE_NDIS_RESET_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800850 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return rndis_reset_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700852 (rndis_reset_msg_type *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 case REMOTE_NDIS_KEEPALIVE_MSG:
855 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700856 if (rndis_debug > 1)
David Brownell70790f62007-07-01 17:35:28 -0700857 DBG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800858 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return rndis_keepalive_response (configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700860 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700862
863 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* At least Windows XP emits some undefined RNDIS messages.
865 * In one case those messages seemed to relate to the host
866 * suspending itself.
867 */
David Brownell00274922007-11-19 12:58:36 -0800868 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800869 __func__ , MsgType, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 {
871 unsigned i;
872 for (i = 0; i < MsgLength; i += 16) {
David Brownell70790f62007-07-01 17:35:28 -0700873 DBG("%03d: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 " %02x %02x %02x %02x"
875 " %02x %02x %02x %02x"
876 " %02x %02x %02x %02x"
877 " %02x %02x %02x %02x"
878 "\n",
879 i,
880 buf[i], buf [i+1],
881 buf[i+2], buf[i+3],
882 buf[i+4], buf [i+5],
883 buf[i+6], buf[i+7],
884 buf[i+8], buf [i+9],
885 buf[i+10], buf[i+11],
886 buf[i+12], buf [i+13],
887 buf[i+14], buf[i+15]);
888 }
889 }
890 break;
891 }
David Brownell7e27f182006-06-13 09:54:40 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -ENOTSUPP;
894}
895
David Brownell15b2d2b2008-06-19 18:19:16 -0700896int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700899
David Brownell15b2d2b2008-06-19 18:19:16 -0700900 if (!resp_avail)
901 return -EINVAL;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
904 if (!rndis_per_dev_params [i].used) {
905 rndis_per_dev_params [i].used = 1;
David Brownell15b2d2b2008-06-19 18:19:16 -0700906 rndis_per_dev_params [i].resp_avail = resp_avail;
907 rndis_per_dev_params [i].v = v;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800908 DBG("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return i;
910 }
911 }
David Brownell70790f62007-07-01 17:35:28 -0700912 DBG("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700913
David Brownell15b2d2b2008-06-19 18:19:16 -0700914 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917void rndis_deregister (int configNr)
918{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800919 DBG("%s: \n", __func__ );
David Brownell7e27f182006-06-13 09:54:40 -0700920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (configNr >= RNDIS_MAX_CONFIGS) return;
922 rndis_per_dev_params [configNr].used = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return;
925}
926
David Brownell15b2d2b2008-06-19 18:19:16 -0700927int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800929 DBG("%s:\n", __func__ );
David Brownell15b2d2b2008-06-19 18:19:16 -0700930 if (!dev)
931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 rndis_per_dev_params [configNr].dev = dev;
David Brownell340600a2005-04-28 13:45:25 -0700935 rndis_per_dev_params [configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
939
940int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
941{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 DBG("%s:\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!vendorDescr) return -1;
944 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 rndis_per_dev_params [configNr].vendorID = vendorID;
947 rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950}
951
952int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
953{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800954 DBG("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 rndis_per_dev_params [configNr].medium = medium;
958 rndis_per_dev_params [configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961}
962
963void rndis_add_hdr (struct sk_buff *skb)
964{
965 struct rndis_packet_msg_type *header;
966
967 if (!skb)
968 return;
969 header = (void *) skb_push (skb, sizeof *header);
970 memset (header, 0, sizeof *header);
David Brownell6cdee102005-04-18 17:39:34 -0700971 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 header->MessageLength = cpu_to_le32(skb->len);
973 header->DataOffset = __constant_cpu_to_le32 (36);
David Brownell6cdee102005-04-18 17:39:34 -0700974 header->DataLength = cpu_to_le32(skb->len - sizeof *header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977void rndis_free_response (int configNr, u8 *buf)
978{
979 rndis_resp_t *r;
980 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700981
982 list_for_each_safe (act, tmp,
983 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 {
985 r = list_entry (act, rndis_resp_t, list);
986 if (r && r->buf == buf) {
987 list_del (&r->list);
988 kfree (r);
989 }
990 }
991}
992
993u8 *rndis_get_next_response (int configNr, u32 *length)
994{
995 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700996 struct list_head *act, *tmp;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700999
1000 list_for_each_safe (act, tmp,
1001 &(rndis_per_dev_params [configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 {
1003 r = list_entry (act, rndis_resp_t, list);
1004 if (!r->send) {
1005 r->send = 1;
1006 *length = r->length;
1007 return r->buf;
1008 }
1009 }
David Brownell7e27f182006-06-13 09:54:40 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return NULL;
1012}
1013
1014static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1015{
1016 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001017
David Brownell340600a2005-04-28 13:45:25 -07001018 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1020 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 r->buf = (u8 *) (r + 1);
1023 r->length = length;
1024 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001025
1026 list_add_tail (&r->list,
1027 &(rndis_per_dev_params [configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return r;
1029}
1030
David Brownell6cdee102005-04-18 17:39:34 -07001031int rndis_rm_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
David Brownell6cdee102005-04-18 17:39:34 -07001033 /* tmp points to a struct rndis_packet_msg_type */
1034 __le32 *tmp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
David Brownell6cdee102005-04-18 17:39:34 -07001036 /* MessageType, MessageLength */
1037 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1038 != get_unaligned(tmp++))
1039 return -EINVAL;
1040 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
David Brownell6cdee102005-04-18 17:39:34 -07001042 /* DataOffset, DataLength */
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001043 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8))
David Brownell6cdee102005-04-18 17:39:34 -07001044 return -EOVERFLOW;
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001045 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
1048}
1049
1050#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1051
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001052static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001054 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001055
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001056 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 "Config Nr. %d\n"
1058 "used : %s\n"
1059 "state : %s\n"
1060 "medium : 0x%08X\n"
1061 "speed : %d\n"
1062 "cable : %s\n"
1063 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001064 "vendor : %s\n",
1065 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ({ char *s = "?";
1067 switch (param->state) {
1068 case RNDIS_UNINITIALIZED:
1069 s = "RNDIS_UNINITIALIZED"; break;
1070 case RNDIS_INITIALIZED:
1071 s = "RNDIS_INITIALIZED"; break;
1072 case RNDIS_DATA_INITIALIZED:
1073 s = "RNDIS_DATA_INITIALIZED"; break;
1074 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001075 param->medium,
1076 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001078 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001082static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1083 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001085 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 u32 speed = 0;
1087 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 for (i = 0; i < count; i++) {
1090 char c;
1091 if (get_user(c, buffer))
1092 return -EFAULT;
1093 switch (c) {
1094 case '0':
1095 case '1':
1096 case '2':
1097 case '3':
1098 case '4':
1099 case '5':
1100 case '6':
1101 case '7':
1102 case '8':
1103 case '9':
1104 fl_speed = 1;
1105 speed = speed*10 + c - '0';
1106 break;
1107 case 'C':
1108 case 'c':
1109 rndis_signal_connect (p->confignr);
1110 break;
1111 case 'D':
1112 case 'd':
1113 rndis_signal_disconnect(p->confignr);
1114 break;
David Brownell7e27f182006-06-13 09:54:40 -07001115 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (fl_speed) p->speed = speed;
David Brownell70790f62007-07-01 17:35:28 -07001117 else DBG("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
1119 }
David Brownell7e27f182006-06-13 09:54:40 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 buffer++;
1122 }
David Brownell7e27f182006-06-13 09:54:40 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return count;
1125}
1126
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001127static int rndis_proc_open(struct inode *inode, struct file *file)
1128{
1129 return single_open(file, rndis_proc_show, PDE(inode)->data);
1130}
1131
1132static const struct file_operations rndis_proc_fops = {
1133 .owner = THIS_MODULE,
1134 .open = rndis_proc_open,
1135 .read = seq_read,
1136 .llseek = seq_lseek,
1137 .release = single_release,
1138 .write = rndis_proc_write,
1139};
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141#define NAME_TEMPLATE "driver/rndis-%03d"
1142
1143static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1144
1145#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1146
1147
David Brownell0e530b42008-04-05 14:17:14 -07001148int __init rndis_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 u8 i;
1151
1152 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1153#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1154 char name [20];
1155
1156 sprintf (name, NAME_TEMPLATE, i);
1157 if (!(rndis_connect_state [i]
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001158 = proc_create_data(name, 0660, NULL,
1159 &rndis_proc_fops,
1160 (void *)(rndis_per_dev_params + i))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001162 DBG("%s :remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 while (i) {
1164 sprintf (name, NAME_TEMPLATE, --i);
1165 remove_proc_entry (name, NULL);
1166 }
David Brownell70790f62007-07-01 17:35:28 -07001167 DBG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return -EIO;
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170#endif
1171 rndis_per_dev_params [i].confignr = i;
1172 rndis_per_dev_params [i].used = 0;
1173 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1174 rndis_per_dev_params [i].media_state
1175 = NDIS_MEDIA_STATE_DISCONNECTED;
1176 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1177 }
David Brownell7e27f182006-06-13 09:54:40 -07001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 0;
1180}
1181
1182void rndis_exit (void)
1183{
1184#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1185 u8 i;
1186 char name [20];
David Brownell7e27f182006-06-13 09:54:40 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1189 sprintf (name, NAME_TEMPLATE, i);
1190 remove_proc_entry (name, NULL);
1191 }
1192#endif
1193}
1194