blob: 6cea2e17b32b2d56adb32d4a8ddfde025d9a1f75 [file] [log] [blame]
David Brownell7e27f182006-06-13 09:54:40 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * RNDIS MSG parser
David Brownell7e27f182006-06-13 09:54:40 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Benedikt Spranger, Pengutronix
David Brownell7e27f182006-06-13 09:54:40 -07005 * Robert Schwebel, Pengutronix
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
David Brownell7e27f182006-06-13 09:54:40 -07009 * version 2, as published by the Free Software Foundation.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This software was originally developed in conformance with
12 * Microsoft's Remote NDIS Specification License Agreement.
David Brownell7e27f182006-06-13 09:54:40 -070013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
15 * Fixed message length bug in init_response
David Brownell7e27f182006-06-13 09:54:40 -070016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
David Brownell7e27f182006-06-13 09:54:40 -070018 * Fixed rndis_rm_hdr length bug.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 *
20 * Copyright (C) 2004 by David Brownell
21 * updates to merge with Linux 2.6, better match RNDIS spec
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Alexey Dobriyane184d5f2008-05-14 16:25:13 -070032#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/netdevice.h>
34
35#include <asm/io.h>
36#include <asm/byteorder.h>
37#include <asm/system.h>
David Brownell6cdee102005-04-18 17:39:34 -070038#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
David Brownell15b2d2b2008-06-19 18:19:16 -070041#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "rndis.h"
44
45
46/* The driver for your USB chip needs to support ep0 OUT to work with
47 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
48 *
49 * Windows hosts need an INF file like Documentation/usb/linux.inf
50 * and will be happier if you provide the host_addr module parameter.
51 */
52
53#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070055module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
61#define RNDIS_MAX_CONFIGS 1
62
63
Mihai Donțua1df4e42010-09-08 02:54:02 +030064static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030067static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* Function Prototypes */
Mihai Donțua1df4e42010-09-08 02:54:02 +030070static rndis_resp_t *rndis_add_response(int configNr, u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72
David Brownell340600a2005-04-28 13:45:25 -070073/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030074static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070075{
76 /* the general stuff */
77 OID_GEN_SUPPORTED_LIST,
78 OID_GEN_HARDWARE_STATUS,
79 OID_GEN_MEDIA_SUPPORTED,
80 OID_GEN_MEDIA_IN_USE,
81 OID_GEN_MAXIMUM_FRAME_SIZE,
82 OID_GEN_LINK_SPEED,
83 OID_GEN_TRANSMIT_BLOCK_SIZE,
84 OID_GEN_RECEIVE_BLOCK_SIZE,
85 OID_GEN_VENDOR_ID,
86 OID_GEN_VENDOR_DESCRIPTION,
87 OID_GEN_VENDOR_DRIVER_VERSION,
88 OID_GEN_CURRENT_PACKET_FILTER,
89 OID_GEN_MAXIMUM_TOTAL_SIZE,
90 OID_GEN_MEDIA_CONNECT_STATUS,
91 OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070092
David Brownell340600a2005-04-28 13:45:25 -070093 /* the statistical stuff */
94 OID_GEN_XMIT_OK,
95 OID_GEN_RCV_OK,
96 OID_GEN_XMIT_ERROR,
97 OID_GEN_RCV_ERROR,
98 OID_GEN_RCV_NO_BUFFER,
99#ifdef RNDIS_OPTIONAL_STATS
100 OID_GEN_DIRECTED_BYTES_XMIT,
101 OID_GEN_DIRECTED_FRAMES_XMIT,
102 OID_GEN_MULTICAST_BYTES_XMIT,
103 OID_GEN_MULTICAST_FRAMES_XMIT,
104 OID_GEN_BROADCAST_BYTES_XMIT,
105 OID_GEN_BROADCAST_FRAMES_XMIT,
106 OID_GEN_DIRECTED_BYTES_RCV,
107 OID_GEN_DIRECTED_FRAMES_RCV,
108 OID_GEN_MULTICAST_BYTES_RCV,
109 OID_GEN_MULTICAST_FRAMES_RCV,
110 OID_GEN_BROADCAST_BYTES_RCV,
111 OID_GEN_BROADCAST_FRAMES_RCV,
112 OID_GEN_RCV_CRC_ERROR,
113 OID_GEN_TRANSMIT_QUEUE_LENGTH,
114#endif /* RNDIS_OPTIONAL_STATS */
115
David Brownell7e27f182006-06-13 09:54:40 -0700116 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700117 /* the general stuff */
118 OID_802_3_PERMANENT_ADDRESS,
119 OID_802_3_CURRENT_ADDRESS,
120 OID_802_3_MULTICAST_LIST,
121 OID_802_3_MAC_OPTIONS,
122 OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700123
David Brownell340600a2005-04-28 13:45:25 -0700124 /* the statistical stuff */
125 OID_802_3_RCV_ERROR_ALIGNMENT,
126 OID_802_3_XMIT_ONE_COLLISION,
127 OID_802_3_XMIT_MORE_COLLISIONS,
128#ifdef RNDIS_OPTIONAL_STATS
129 OID_802_3_XMIT_DEFERRED,
130 OID_802_3_XMIT_MAX_COLLISIONS,
131 OID_802_3_RCV_OVERRUN,
132 OID_802_3_XMIT_UNDERRUN,
133 OID_802_3_XMIT_HEARTBEAT_FAILURE,
134 OID_802_3_XMIT_TIMES_CRS_LOST,
135 OID_802_3_XMIT_LATE_COLLISIONS,
136#endif /* RNDIS_OPTIONAL_STATS */
137
138#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700139 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
140 * don't say what they mean ... and the NDIS specs are often
141 * confusing and/or ambiguous in this context. (That is, more
142 * so than their specs for the other OIDs.)
143 *
144 * FIXME someone who knows what these should do, please
145 * implement them!
146 */
David Brownell340600a2005-04-28 13:45:25 -0700147
148 /* power management */
149 OID_PNP_CAPABILITIES,
150 OID_PNP_QUERY_POWER,
151 OID_PNP_SET_POWER,
152
153#ifdef RNDIS_WAKEUP
154 /* wake up host */
155 OID_PNP_ENABLE_WAKE_UP,
156 OID_PNP_ADD_WAKE_UP_PATTERN,
157 OID_PNP_REMOVE_WAKE_UP_PATTERN,
158#endif /* RNDIS_WAKEUP */
159#endif /* RNDIS_PM */
160};
161
Benoit Gobyffdab0c2010-11-17 14:27:51 -0800162/* HACK: copied from net/core/dev.c to replace dev_get_stats since
163 * dev_get_stats cannot be called from atomic context */
164static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
165 const struct net_device_stats *netdev_stats)
166{
167#if BITS_PER_LONG == 64
168 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
169 memcpy(stats64, netdev_stats, sizeof(*stats64));
170#else
171 size_t i, n = sizeof(*stats64) / sizeof(u64);
172 const unsigned long *src = (const unsigned long *)netdev_stats;
173 u64 *dst = (u64 *)stats64;
174
175 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
176 sizeof(*stats64) / sizeof(u64));
177 for (i = 0; i < n; i++)
178 dst[i] = src[i];
179#endif
180}
David Brownell340600a2005-04-28 13:45:25 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* NDIS Functions */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300183static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
184 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300186 int retval = -ENOTSUPP;
187 u32 length = 4; /* usually */
188 __le32 *outbuf;
189 int i, count;
190 rndis_query_cmplt_type *resp;
191 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700192 struct rtnl_link_stats64 temp;
Benoit Gobyffdab0c2010-11-17 14:27:51 -0800193 struct rtnl_link_stats64 *stats = &temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300196 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700199
200 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700201 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700202 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700203 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700204 get_unaligned_le32(&buf[i]),
205 get_unaligned_le32(&buf[i + 4]),
206 get_unaligned_le32(&buf[i + 8]),
207 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700208 }
209 }
210
211 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300212 outbuf = (__le32 *)&resp[1];
213 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700214
David Brownell15b2d2b2008-06-19 18:19:16 -0700215 net = rndis_per_dev_params[configNr].dev;
Benoit Gobyffdab0c2010-11-17 14:27:51 -0800216 netdev_stats_to_stats64(stats, &net->stats);
David Brownell15b2d2b2008-06-19 18:19:16 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 switch (OID) {
219
220 /* general oids (table 4-1) */
221
222 /* mandatory */
223 case OID_GEN_SUPPORTED_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700224 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300225 length = sizeof(oid_supported_list);
226 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300228 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 retval = 0;
230 break;
David Brownell7e27f182006-06-13 09:54:40 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* mandatory */
233 case OID_GEN_HARDWARE_STATUS:
David Brownell33376c12008-08-18 17:45:07 -0700234 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700235 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700237 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * reddite ergo quae sunt Caesaris Caesari
239 * et quae sunt Dei Deo!
240 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300241 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 retval = 0;
243 break;
David Brownell7e27f182006-06-13 09:54:40 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* mandatory */
246 case OID_GEN_MEDIA_SUPPORTED:
David Brownell33376c12008-08-18 17:45:07 -0700247 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300248 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 retval = 0;
250 break;
David Brownell7e27f182006-06-13 09:54:40 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* mandatory */
253 case OID_GEN_MEDIA_IN_USE:
David Brownell33376c12008-08-18 17:45:07 -0700254 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* one medium, one transport... (maybe you do it better) */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300256 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 retval = 0;
258 break;
David Brownell7e27f182006-06-13 09:54:40 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* mandatory */
261 case OID_GEN_MAXIMUM_FRAME_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700262 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300263 if (rndis_per_dev_params[configNr].dev) {
264 *outbuf = cpu_to_le32(
265 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 break;
David Brownell7e27f182006-06-13 09:54:40 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* mandatory */
271 case OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700272 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700273 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300274 if (rndis_per_dev_params[configNr].media_state
David Brownell340600a2005-04-28 13:45:25 -0700275 == NDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300276 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300278 *outbuf = cpu_to_le32(
279 rndis_per_dev_params[configNr].speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 retval = 0;
281 break;
282
283 /* mandatory */
284 case OID_GEN_TRANSMIT_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700285 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300286 if (rndis_per_dev_params[configNr].dev) {
287 *outbuf = cpu_to_le32(
288 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 retval = 0;
290 }
291 break;
David Brownell7e27f182006-06-13 09:54:40 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* mandatory */
294 case OID_GEN_RECEIVE_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700295 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300296 if (rndis_per_dev_params[configNr].dev) {
297 *outbuf = cpu_to_le32(
298 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 retval = 0;
300 }
301 break;
David Brownell7e27f182006-06-13 09:54:40 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* mandatory */
304 case OID_GEN_VENDOR_ID:
David Brownell33376c12008-08-18 17:45:07 -0700305 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300306 *outbuf = cpu_to_le32(
307 rndis_per_dev_params[configNr].vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 retval = 0;
309 break;
David Brownell7e27f182006-06-13 09:54:40 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* mandatory */
312 case OID_GEN_VENDOR_DESCRIPTION:
David Brownell33376c12008-08-18 17:45:07 -0700313 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300314 if (rndis_per_dev_params[configNr].vendorDescr) {
315 length = strlen(rndis_per_dev_params[configNr].
316 vendorDescr);
317 memcpy(outbuf,
318 rndis_per_dev_params[configNr].vendorDescr,
319 length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400320 } else {
321 outbuf[0] = 0;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 retval = 0;
324 break;
325
326 case OID_GEN_VENDOR_DRIVER_VERSION:
David Brownell33376c12008-08-18 17:45:07 -0700327 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700329 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 retval = 0;
331 break;
332
333 /* mandatory */
334 case OID_GEN_CURRENT_PACKET_FILTER:
David Brownell33376c12008-08-18 17:45:07 -0700335 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300336 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 retval = 0;
338 break;
339
340 /* mandatory */
341 case OID_GEN_MAXIMUM_TOTAL_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700342 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800343 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 retval = 0;
345 break;
346
347 /* mandatory */
348 case OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700349 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700350 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300351 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 .media_state);
353 retval = 0;
354 break;
355
356 case OID_GEN_PHYSICAL_MEDIUM:
David Brownell33376c12008-08-18 17:45:07 -0700357 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300358 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 retval = 0;
360 break;
361
362 /* The RNDIS specification is incomplete/wrong. Some versions
363 * of MS-Windows expect OIDs that aren't specified there. Other
364 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
365 */
366 case OID_GEN_MAC_OPTIONS: /* from WinME */
David Brownell33376c12008-08-18 17:45:07 -0700367 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800368 *outbuf = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
370 | NDIS_MAC_OPTION_FULL_DUPLEX);
371 retval = 0;
372 break;
373
374 /* statistics OIDs (table 4-2) */
375
376 /* mandatory */
377 case OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700378 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700379 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700380 if (stats) {
381 *outbuf = cpu_to_le32(stats->tx_packets
382 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385 break;
386
387 /* mandatory */
388 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700389 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700390 pr_debug("%s: OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700391 if (stats) {
392 *outbuf = cpu_to_le32(stats->rx_packets
393 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 break;
David Brownell7e27f182006-06-13 09:54:40 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* mandatory */
399 case OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700400 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700401 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700402 if (stats) {
403 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 break;
David Brownell7e27f182006-06-13 09:54:40 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* mandatory */
409 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700410 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700411 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700412 if (stats) {
413 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 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_GEN_RCV_NO_BUFFER:
David Brownell33376c12008-08-18 17:45:07 -0700420 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700421 if (stats) {
422 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 break;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* ieee802.3 OIDs (table 4-3) */
428
429 /* mandatory */
430 case OID_802_3_PERMANENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700431 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300432 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300434 memcpy(outbuf,
435 rndis_per_dev_params[configNr].host_mac,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 length);
437 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 break;
David Brownell7e27f182006-06-13 09:54:40 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* mandatory */
442 case OID_802_3_CURRENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700443 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300444 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300446 memcpy(outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 rndis_per_dev_params [configNr].host_mac,
448 length);
449 retval = 0;
450 }
451 break;
David Brownell7e27f182006-06-13 09:54:40 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* mandatory */
454 case OID_802_3_MULTICAST_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700455 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300457 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 retval = 0;
459 break;
David Brownell7e27f182006-06-13 09:54:40 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* mandatory */
462 case OID_802_3_MAXIMUM_LIST_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700463 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300465 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 retval = 0;
467 break;
David Brownell7e27f182006-06-13 09:54:40 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case OID_802_3_MAC_OPTIONS:
David Brownell33376c12008-08-18 17:45:07 -0700470 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700471 *outbuf = cpu_to_le32(0);
472 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474
475 /* ieee802.3 statistics OIDs (table 4-4) */
476
477 /* mandatory */
478 case OID_802_3_RCV_ERROR_ALIGNMENT:
David Brownell33376c12008-08-18 17:45:07 -0700479 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700480 if (stats) {
481 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 retval = 0;
483 }
484 break;
David Brownell7e27f182006-06-13 09:54:40 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* mandatory */
487 case OID_802_3_XMIT_ONE_COLLISION:
David Brownell33376c12008-08-18 17:45:07 -0700488 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300489 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 retval = 0;
491 break;
David Brownell7e27f182006-06-13 09:54:40 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* mandatory */
494 case OID_802_3_XMIT_MORE_COLLISIONS:
David Brownell33376c12008-08-18 17:45:07 -0700495 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300496 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 retval = 0;
498 break;
David Brownell7e27f182006-06-13 09:54:40 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 default:
David Brownell00274922007-11-19 12:58:36 -0800501 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800502 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
David Brownell340600a2005-04-28 13:45:25 -0700504 if (retval < 0)
505 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700506
Mihai Donțua1df4e42010-09-08 02:54:02 +0300507 resp->InformationBufferLength = cpu_to_le32(length);
508 r->length = length + sizeof(*resp);
509 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return retval;
511}
512
Mihai Donțua1df4e42010-09-08 02:54:02 +0300513static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
514 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300516 rndis_set_cmplt_type *resp;
517 int i, retval = -ENOTSUPP;
518 struct rndis_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (!r)
521 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300522 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!resp)
524 return -ENOMEM;
525
David Brownell340600a2005-04-28 13:45:25 -0700526 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700527 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700528 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700529 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700530 get_unaligned_le32(&buf[i]),
531 get_unaligned_le32(&buf[i + 4]),
532 get_unaligned_le32(&buf[i + 8]),
533 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
Mihai Donțua1df4e42010-09-08 02:54:02 +0300537 params = &rndis_per_dev_params[configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 switch (OID) {
539 case OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
David Brownell340600a2005-04-28 13:45:25 -0700541 /* these NDIS_PACKET_TYPE_* bitflags are shared with
542 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
544 * PROMISCUOUS, DIRECTED,
545 * MULTICAST, ALL_MULTICAST, BROADCAST
546 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700547 *params->filter = (u16)get_unaligned_le32(buf);
David Brownell33376c12008-08-18 17:45:07 -0700548 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800549 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* this call has a significant side effect: it's
552 * what makes the packet flow start and stop, like
553 * activating the CDC Ethernet altsetting.
554 */
David Brownell340600a2005-04-28 13:45:25 -0700555 retval = 0;
556 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 params->state = RNDIS_DATA_INITIALIZED;
558 netif_carrier_on(params->dev);
559 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300560 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else {
562 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300563 netif_carrier_off(params->dev);
564 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 break;
David Brownell7e27f182006-06-13 09:54:40 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 case OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700569 /* I think we can ignore this */
David Brownell33376c12008-08-18 17:45:07 -0700570 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 retval = 0;
572 break;
David Brownell340600a2005-04-28 13:45:25 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 default:
David Brownell00274922007-11-19 12:58:36 -0800575 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800576 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
David Brownell7e27f182006-06-13 09:54:40 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return retval;
580}
581
David Brownell7e27f182006-06-13 09:54:40 -0700582/*
583 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
585
Mihai Donțua1df4e42010-09-08 02:54:02 +0300586static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300588 rndis_init_cmplt_type *resp;
589 rndis_resp_t *r;
590 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700591
David Brownell15b2d2b2008-06-19 18:19:16 -0700592 if (!params->dev)
593 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700594
Mihai Donțua1df4e42010-09-08 02:54:02 +0300595 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700596 if (!r)
597 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300598 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700599
Mihai Donțua1df4e42010-09-08 02:54:02 +0300600 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INITIALIZE_CMPLT);
601 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300603 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
604 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
605 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
606 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
607 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
608 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
609 resp->MaxTransferSize = cpu_to_le32(
David Brownell15b2d2b2008-06-19 18:19:16 -0700610 params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300611 + sizeof(struct ethhdr)
612 + sizeof(struct rndis_packet_msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 + 22);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300614 resp->PacketAlignmentFactor = cpu_to_le32(0);
615 resp->AFListOffset = cpu_to_le32(0);
616 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700617
David Brownell15b2d2b2008-06-19 18:19:16 -0700618 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
620}
621
Mihai Donțua1df4e42010-09-08 02:54:02 +0300622static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300625 rndis_resp_t *r;
626 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700627
David Brownell33376c12008-08-18 17:45:07 -0700628 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700629 if (!params->dev)
630 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700631
Shaun Tancheff87637162006-02-22 19:47:19 -0800632 /*
633 * we need more memory:
634 * gen_ndis_query_resp expects enough space for
635 * rndis_query_cmplt_type followed by data.
636 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300638 r = rndis_add_response(configNr,
639 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700640 if (!r)
641 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300642 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700643
Mihai Donțua1df4e42010-09-08 02:54:02 +0300644 resp->MessageType = cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700646
Mihai Donțua1df4e42010-09-08 02:54:02 +0300647 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700648 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300649 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700650 le32_to_cpu(buf->InformationBufferLength),
651 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300653 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
654 resp->MessageLength = cpu_to_le32(sizeof *resp);
655 resp->InformationBufferLength = cpu_to_le32(0);
656 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300658 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700659
David Brownell15b2d2b2008-06-19 18:19:16 -0700660 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return 0;
662}
663
Mihai Donțua1df4e42010-09-08 02:54:02 +0300664static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300666 u32 BufLength, BufOffset;
667 rndis_set_cmplt_type *resp;
668 rndis_resp_t *r;
669 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700670
Mihai Donțua1df4e42010-09-08 02:54:02 +0300671 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700672 if (!r)
673 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300674 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Mihai Donțua1df4e42010-09-08 02:54:02 +0300676 BufLength = le32_to_cpu(buf->InformationBufferLength);
677 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
David Brownell15b2d2b2008-06-19 18:19:16 -0700679#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700680 pr_debug("%s: Length: %d\n", __func__, BufLength);
681 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
682 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700685 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
David Brownell7e27f182006-06-13 09:54:40 -0700687
David Brownell33376c12008-08-18 17:45:07 -0700688 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#endif
David Brownell7e27f182006-06-13 09:54:40 -0700690
Mihai Donțua1df4e42010-09-08 02:54:02 +0300691 resp->MessageType = cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
692 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300694 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
695 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
696 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700697 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300698 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700699
David Brownell15b2d2b2008-06-19 18:19:16 -0700700 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702}
703
Mihai Donțua1df4e42010-09-08 02:54:02 +0300704static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300706 rndis_reset_cmplt_type *resp;
707 rndis_resp_t *r;
708 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700709
Mihai Donțua1df4e42010-09-08 02:54:02 +0300710 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700711 if (!r)
712 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300713 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700714
Mihai Donțua1df4e42010-09-08 02:54:02 +0300715 resp->MessageType = cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
716 resp->MessageLength = cpu_to_le32(16);
717 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300719 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700720
David Brownell15b2d2b2008-06-19 18:19:16 -0700721 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723}
724
Mihai Donțua1df4e42010-09-08 02:54:02 +0300725static int rndis_keepalive_response(int configNr,
726 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300728 rndis_keepalive_cmplt_type *resp;
729 rndis_resp_t *r;
730 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
733
Mihai Donțua1df4e42010-09-08 02:54:02 +0300734 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700735 if (!r)
736 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300737 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700738
Mihai Donțua1df4e42010-09-08 02:54:02 +0300739 resp->MessageType = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 REMOTE_NDIS_KEEPALIVE_CMPLT);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300741 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300743 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700744
David Brownell15b2d2b2008-06-19 18:19:16 -0700745 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747}
748
749
David Brownell7e27f182006-06-13 09:54:40 -0700750/*
751 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300753static int rndis_indicate_status_msg(int configNr, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300755 rndis_indicate_status_msg_type *resp;
756 rndis_resp_t *r;
757 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700758
David Brownell15b2d2b2008-06-19 18:19:16 -0700759 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700760 return -ENOTSUPP;
761
Mihai Donțua1df4e42010-09-08 02:54:02 +0300762 r = rndis_add_response(configNr,
763 sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700764 if (!r)
765 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300766 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700767
Mihai Donțua1df4e42010-09-08 02:54:02 +0300768 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INDICATE_STATUS_MSG);
769 resp->MessageLength = cpu_to_le32(20);
770 resp->Status = cpu_to_le32(status);
771 resp->StatusBufferLength = cpu_to_le32(0);
772 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700773
David Brownell15b2d2b2008-06-19 18:19:16 -0700774 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return 0;
776}
777
Mihai Donțua1df4e42010-09-08 02:54:02 +0300778int rndis_signal_connect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300780 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 = NDIS_MEDIA_STATE_CONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300782 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 RNDIS_STATUS_MEDIA_CONNECT);
784}
785
Mihai Donțua1df4e42010-09-08 02:54:02 +0300786int rndis_signal_disconnect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300788 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300790 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 RNDIS_STATUS_MEDIA_DISCONNECT);
792}
793
Mihai Donțua1df4e42010-09-08 02:54:02 +0300794void rndis_uninit(int configNr)
David Brownell340600a2005-04-28 13:45:25 -0700795{
David Brownell486e2df2005-05-24 17:51:52 -0700796 u8 *buf;
797 u32 length;
798
David Brownell340600a2005-04-28 13:45:25 -0700799 if (configNr >= RNDIS_MAX_CONFIGS)
800 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300801 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700802
803 /* drain the response queue */
804 while ((buf = rndis_get_next_response(configNr, &length)))
805 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700806}
807
Mihai Donțua1df4e42010-09-08 02:54:02 +0300808void rndis_set_host_mac(int configNr, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300810 rndis_per_dev_params[configNr].host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
David Brownell7e27f182006-06-13 09:54:40 -0700813/*
814 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300816int rndis_msg_parser(u8 configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 u32 MsgType, MsgLength;
819 __le32 *tmp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300820 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!buf)
823 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700824
Mihai Donțua1df4e42010-09-08 02:54:02 +0300825 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700826 MsgType = get_unaligned_le32(tmp++);
827 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (configNr >= RNDIS_MAX_CONFIGS)
830 return -ENOTSUPP;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300831 params = &rndis_per_dev_params[configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700832
David Brownell340600a2005-04-28 13:45:25 -0700833 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
834 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
835 * and normal HC level polling to see if there's any IN traffic.
836 */
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700839 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700841 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300842 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300844 return rndis_init_response(configNr,
845 (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 case REMOTE_NDIS_HALT_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700848 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300849 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 params->state = RNDIS_UNINITIALIZED;
851 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300852 netif_carrier_off(params->dev);
853 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 case REMOTE_NDIS_QUERY_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300858 return rndis_query_response(configNr,
859 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 case REMOTE_NDIS_SET_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300862 return rndis_set_response(configNr,
863 (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 case REMOTE_NDIS_RESET_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700866 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300867 __func__);
868 return rndis_reset_response(configNr,
869 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 case REMOTE_NDIS_KEEPALIVE_MSG:
872 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700873 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700874 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300875 __func__);
876 return rndis_keepalive_response(configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700877 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700879
880 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* At least Windows XP emits some undefined RNDIS messages.
882 * In one case those messages seemed to relate to the host
883 * suspending itself.
884 */
David Brownell00274922007-11-19 12:58:36 -0800885 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300886 __func__, MsgType, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 {
888 unsigned i;
889 for (i = 0; i < MsgLength; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700890 pr_debug("%03d: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 " %02x %02x %02x %02x"
892 " %02x %02x %02x %02x"
893 " %02x %02x %02x %02x"
894 " %02x %02x %02x %02x"
895 "\n",
896 i,
897 buf[i], buf [i+1],
898 buf[i+2], buf[i+3],
899 buf[i+4], buf [i+5],
900 buf[i+6], buf[i+7],
901 buf[i+8], buf [i+9],
902 buf[i+10], buf[i+11],
903 buf[i+12], buf [i+13],
904 buf[i+14], buf[i+15]);
905 }
906 }
907 break;
908 }
David Brownell7e27f182006-06-13 09:54:40 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -ENOTSUPP;
911}
912
David Brownell15b2d2b2008-06-19 18:19:16 -0700913int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700916
David Brownell15b2d2b2008-06-19 18:19:16 -0700917 if (!resp_avail)
918 return -EINVAL;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300921 if (!rndis_per_dev_params[i].used) {
922 rndis_per_dev_params[i].used = 1;
923 rndis_per_dev_params[i].resp_avail = resp_avail;
924 rndis_per_dev_params[i].v = v;
David Brownell33376c12008-08-18 17:45:07 -0700925 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return i;
927 }
928 }
David Brownell33376c12008-08-18 17:45:07 -0700929 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700930
David Brownell15b2d2b2008-06-19 18:19:16 -0700931 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Mihai Donțua1df4e42010-09-08 02:54:02 +0300934void rndis_deregister(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300936 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (configNr >= RNDIS_MAX_CONFIGS) return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300939 rndis_per_dev_params[configNr].used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
David Brownell15b2d2b2008-06-19 18:19:16 -0700942int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
David Brownell33376c12008-08-18 17:45:07 -0700944 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700945 if (!dev)
946 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700948
Mihai Donțua1df4e42010-09-08 02:54:02 +0300949 rndis_per_dev_params[configNr].dev = dev;
950 rndis_per_dev_params[configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
Mihai Donțua1df4e42010-09-08 02:54:02 +0300955int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
David Brownell33376c12008-08-18 17:45:07 -0700957 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!vendorDescr) return -1;
959 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700960
Mihai Donțua1df4e42010-09-08 02:54:02 +0300961 rndis_per_dev_params[configNr].vendorID = vendorID;
962 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return 0;
965}
966
Mihai Donțua1df4e42010-09-08 02:54:02 +0300967int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
David Brownell33376c12008-08-18 17:45:07 -0700969 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700971
Mihai Donțua1df4e42010-09-08 02:54:02 +0300972 rndis_per_dev_params[configNr].medium = medium;
973 rndis_per_dev_params[configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976}
977
Mihai Donțua1df4e42010-09-08 02:54:02 +0300978void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300980 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (!skb)
983 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300984 header = (void *)skb_push(skb, sizeof(*header));
985 memset(header, 0, sizeof *header);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800986 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300988 header->DataOffset = cpu_to_le32(36);
989 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Mihai Donțua1df4e42010-09-08 02:54:02 +0300992void rndis_free_response(int configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300994 rndis_resp_t *r;
995 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700996
Mihai Donțua1df4e42010-09-08 02:54:02 +0300997 list_for_each_safe(act, tmp,
998 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001000 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (r && r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001002 list_del(&r->list);
1003 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 }
1006}
1007
Mihai Donțua1df4e42010-09-08 02:54:02 +03001008u8 *rndis_get_next_response(int configNr, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001010 rndis_resp_t *r;
1011 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -07001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001014
Mihai Donțua1df4e42010-09-08 02:54:02 +03001015 list_for_each_safe(act, tmp,
1016 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001018 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (!r->send) {
1020 r->send = 1;
1021 *length = r->length;
1022 return r->buf;
1023 }
1024 }
David Brownell7e27f182006-06-13 09:54:40 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return NULL;
1027}
1028
Mihai Donțua1df4e42010-09-08 02:54:02 +03001029static rndis_resp_t *rndis_add_response(int configNr, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001031 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001032
Mihai Donțua1df4e42010-09-08 02:54:02 +03001033 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1034 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001036
Mihai Donțua1df4e42010-09-08 02:54:02 +03001037 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 r->length = length;
1039 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001040
Mihai Donțua1df4e42010-09-08 02:54:02 +03001041 list_add_tail(&r->list,
1042 &(rndis_per_dev_params[configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return r;
1044}
1045
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001046int rndis_rm_hdr(struct gether *port,
1047 struct sk_buff *skb,
1048 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
David Brownell6cdee102005-04-18 17:39:34 -07001050 /* tmp points to a struct rndis_packet_msg_type */
Mihai Donțua1df4e42010-09-08 02:54:02 +03001051 __le32 *tmp = (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
David Brownell6cdee102005-04-18 17:39:34 -07001053 /* MessageType, MessageLength */
Harvey Harrison35c26c22009-02-14 22:56:56 -08001054 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001055 != get_unaligned(tmp++)) {
1056 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001057 return -EINVAL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001058 }
David Brownell6cdee102005-04-18 17:39:34 -07001059 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
David Brownell6cdee102005-04-18 17:39:34 -07001061 /* DataOffset, DataLength */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001062 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1063 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001064 return -EOVERFLOW;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001065 }
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001066 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001067
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001068 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 0;
1070}
1071
Mihai Donțua1df4e42010-09-08 02:54:02 +03001072#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001074static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001076 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001077
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001078 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 "Config Nr. %d\n"
1080 "used : %s\n"
1081 "state : %s\n"
1082 "medium : 0x%08X\n"
1083 "speed : %d\n"
1084 "cable : %s\n"
1085 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001086 "vendor : %s\n",
1087 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ({ char *s = "?";
1089 switch (param->state) {
1090 case RNDIS_UNINITIALIZED:
1091 s = "RNDIS_UNINITIALIZED"; break;
1092 case RNDIS_INITIALIZED:
1093 s = "RNDIS_INITIALIZED"; break;
1094 case RNDIS_DATA_INITIALIZED:
1095 s = "RNDIS_DATA_INITIALIZED"; break;
1096 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001097 param->medium,
1098 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001100 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001104static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001105 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001107 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 u32 speed = 0;
1109 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 for (i = 0; i < count; i++) {
1112 char c;
1113 if (get_user(c, buffer))
1114 return -EFAULT;
1115 switch (c) {
1116 case '0':
1117 case '1':
1118 case '2':
1119 case '3':
1120 case '4':
1121 case '5':
1122 case '6':
1123 case '7':
1124 case '8':
1125 case '9':
1126 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001127 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
1129 case 'C':
1130 case 'c':
Mihai Donțua1df4e42010-09-08 02:54:02 +03001131 rndis_signal_connect(p->confignr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
1133 case 'D':
1134 case 'd':
1135 rndis_signal_disconnect(p->confignr);
1136 break;
David Brownell7e27f182006-06-13 09:54:40 -07001137 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001139 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 break;
1141 }
David Brownell7e27f182006-06-13 09:54:40 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 buffer++;
1144 }
David Brownell7e27f182006-06-13 09:54:40 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return count;
1147}
1148
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001149static int rndis_proc_open(struct inode *inode, struct file *file)
1150{
1151 return single_open(file, rndis_proc_show, PDE(inode)->data);
1152}
1153
1154static const struct file_operations rndis_proc_fops = {
1155 .owner = THIS_MODULE,
1156 .open = rndis_proc_open,
1157 .read = seq_read,
1158 .llseek = seq_lseek,
1159 .release = single_release,
1160 .write = rndis_proc_write,
1161};
1162
Mihai Donțua1df4e42010-09-08 02:54:02 +03001163#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1166
Mihai Donțua1df4e42010-09-08 02:54:02 +03001167#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169
Henrik Kretzschmar793f03a2010-08-20 19:57:50 +02001170int rndis_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 u8 i;
1173
1174 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1175#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1176 char name [20];
1177
Mihai Donțua1df4e42010-09-08 02:54:02 +03001178 sprintf(name, NAME_TEMPLATE, i);
1179 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001180 &rndis_proc_fops,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001181 (void *)(rndis_per_dev_params + i));
1182 if (!rndis_connect_state[i]) {
1183 pr_debug("%s: remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 while (i) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001185 sprintf(name, NAME_TEMPLATE, --i);
1186 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
David Brownell33376c12008-08-18 17:45:07 -07001188 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return -EIO;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191#endif
Mihai Donțua1df4e42010-09-08 02:54:02 +03001192 rndis_per_dev_params[i].confignr = i;
1193 rndis_per_dev_params[i].used = 0;
1194 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1195 rndis_per_dev_params[i].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001197 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
David Brownell7e27f182006-06-13 09:54:40 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
Mihai Donțua1df4e42010-09-08 02:54:02 +03001203void rndis_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001205#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 u8 i;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001207 char name[20];
David Brownell7e27f182006-06-13 09:54:40 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001210 sprintf(name, NAME_TEMPLATE, i);
1211 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213#endif
1214}