blob: 7ac5b64c2f5226ecc1ea4c3e4b36bac5c77e639a [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>
David Brownell6cdee102005-04-18 17:39:34 -070037#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39
David Brownell15b2d2b2008-06-19 18:19:16 -070040#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "rndis.h"
43
44
45/* The driver for your USB chip needs to support ep0 OUT to work with
46 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
47 *
48 * Windows hosts need an INF file like Documentation/usb/linux.inf
49 * and will be happier if you provide the host_addr module parameter.
50 */
51
52#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070054module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
60#define RNDIS_MAX_CONFIGS 1
61
62
Mihai Donțua1df4e42010-09-08 02:54:02 +030063static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030066static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Function Prototypes */
Mihai Donțua1df4e42010-09-08 02:54:02 +030069static rndis_resp_t *rndis_add_response(int configNr, u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71
David Brownell340600a2005-04-28 13:45:25 -070072/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030073static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070074{
75 /* the general stuff */
76 OID_GEN_SUPPORTED_LIST,
77 OID_GEN_HARDWARE_STATUS,
78 OID_GEN_MEDIA_SUPPORTED,
79 OID_GEN_MEDIA_IN_USE,
80 OID_GEN_MAXIMUM_FRAME_SIZE,
81 OID_GEN_LINK_SPEED,
82 OID_GEN_TRANSMIT_BLOCK_SIZE,
83 OID_GEN_RECEIVE_BLOCK_SIZE,
84 OID_GEN_VENDOR_ID,
85 OID_GEN_VENDOR_DESCRIPTION,
86 OID_GEN_VENDOR_DRIVER_VERSION,
87 OID_GEN_CURRENT_PACKET_FILTER,
88 OID_GEN_MAXIMUM_TOTAL_SIZE,
89 OID_GEN_MEDIA_CONNECT_STATUS,
90 OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070091
David Brownell340600a2005-04-28 13:45:25 -070092 /* the statistical stuff */
93 OID_GEN_XMIT_OK,
94 OID_GEN_RCV_OK,
95 OID_GEN_XMIT_ERROR,
96 OID_GEN_RCV_ERROR,
97 OID_GEN_RCV_NO_BUFFER,
98#ifdef RNDIS_OPTIONAL_STATS
99 OID_GEN_DIRECTED_BYTES_XMIT,
100 OID_GEN_DIRECTED_FRAMES_XMIT,
101 OID_GEN_MULTICAST_BYTES_XMIT,
102 OID_GEN_MULTICAST_FRAMES_XMIT,
103 OID_GEN_BROADCAST_BYTES_XMIT,
104 OID_GEN_BROADCAST_FRAMES_XMIT,
105 OID_GEN_DIRECTED_BYTES_RCV,
106 OID_GEN_DIRECTED_FRAMES_RCV,
107 OID_GEN_MULTICAST_BYTES_RCV,
108 OID_GEN_MULTICAST_FRAMES_RCV,
109 OID_GEN_BROADCAST_BYTES_RCV,
110 OID_GEN_BROADCAST_FRAMES_RCV,
111 OID_GEN_RCV_CRC_ERROR,
112 OID_GEN_TRANSMIT_QUEUE_LENGTH,
113#endif /* RNDIS_OPTIONAL_STATS */
114
David Brownell7e27f182006-06-13 09:54:40 -0700115 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700116 /* the general stuff */
117 OID_802_3_PERMANENT_ADDRESS,
118 OID_802_3_CURRENT_ADDRESS,
119 OID_802_3_MULTICAST_LIST,
120 OID_802_3_MAC_OPTIONS,
121 OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700122
David Brownell340600a2005-04-28 13:45:25 -0700123 /* the statistical stuff */
124 OID_802_3_RCV_ERROR_ALIGNMENT,
125 OID_802_3_XMIT_ONE_COLLISION,
126 OID_802_3_XMIT_MORE_COLLISIONS,
127#ifdef RNDIS_OPTIONAL_STATS
128 OID_802_3_XMIT_DEFERRED,
129 OID_802_3_XMIT_MAX_COLLISIONS,
130 OID_802_3_RCV_OVERRUN,
131 OID_802_3_XMIT_UNDERRUN,
132 OID_802_3_XMIT_HEARTBEAT_FAILURE,
133 OID_802_3_XMIT_TIMES_CRS_LOST,
134 OID_802_3_XMIT_LATE_COLLISIONS,
135#endif /* RNDIS_OPTIONAL_STATS */
136
137#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700138 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
139 * don't say what they mean ... and the NDIS specs are often
140 * confusing and/or ambiguous in this context. (That is, more
141 * so than their specs for the other OIDs.)
142 *
143 * FIXME someone who knows what these should do, please
144 * implement them!
145 */
David Brownell340600a2005-04-28 13:45:25 -0700146
147 /* power management */
148 OID_PNP_CAPABILITIES,
149 OID_PNP_QUERY_POWER,
150 OID_PNP_SET_POWER,
151
152#ifdef RNDIS_WAKEUP
153 /* wake up host */
154 OID_PNP_ENABLE_WAKE_UP,
155 OID_PNP_ADD_WAKE_UP_PATTERN,
156 OID_PNP_REMOVE_WAKE_UP_PATTERN,
157#endif /* RNDIS_WAKEUP */
158#endif /* RNDIS_PM */
159};
160
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* NDIS Functions */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300163static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
164 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300166 int retval = -ENOTSUPP;
167 u32 length = 4; /* usually */
168 __le32 *outbuf;
169 int i, count;
170 rndis_query_cmplt_type *resp;
171 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700172 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700173 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300176 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700179
180 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700181 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700182 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700183 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700184 get_unaligned_le32(&buf[i]),
185 get_unaligned_le32(&buf[i + 4]),
186 get_unaligned_le32(&buf[i + 8]),
187 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700188 }
189 }
190
191 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300192 outbuf = (__le32 *)&resp[1];
193 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700194
David Brownell15b2d2b2008-06-19 18:19:16 -0700195 net = rndis_per_dev_params[configNr].dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700196 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 switch (OID) {
199
200 /* general oids (table 4-1) */
201
202 /* mandatory */
203 case OID_GEN_SUPPORTED_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700204 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300205 length = sizeof(oid_supported_list);
206 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300208 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 retval = 0;
210 break;
David Brownell7e27f182006-06-13 09:54:40 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* mandatory */
213 case OID_GEN_HARDWARE_STATUS:
David Brownell33376c12008-08-18 17:45:07 -0700214 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700215 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700217 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * reddite ergo quae sunt Caesaris Caesari
219 * et quae sunt Dei Deo!
220 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300221 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 retval = 0;
223 break;
David Brownell7e27f182006-06-13 09:54:40 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* mandatory */
226 case OID_GEN_MEDIA_SUPPORTED:
David Brownell33376c12008-08-18 17:45:07 -0700227 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300228 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 retval = 0;
230 break;
David Brownell7e27f182006-06-13 09:54:40 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* mandatory */
233 case OID_GEN_MEDIA_IN_USE:
David Brownell33376c12008-08-18 17:45:07 -0700234 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* one medium, one transport... (maybe you do it better) */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300236 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 retval = 0;
238 break;
David Brownell7e27f182006-06-13 09:54:40 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* mandatory */
241 case OID_GEN_MAXIMUM_FRAME_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700242 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300243 if (rndis_per_dev_params[configNr].dev) {
244 *outbuf = cpu_to_le32(
245 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 break;
David Brownell7e27f182006-06-13 09:54:40 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* mandatory */
251 case OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700252 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700253 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300254 if (rndis_per_dev_params[configNr].media_state
David Brownell340600a2005-04-28 13:45:25 -0700255 == NDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300256 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300258 *outbuf = cpu_to_le32(
259 rndis_per_dev_params[configNr].speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 retval = 0;
261 break;
262
263 /* mandatory */
264 case OID_GEN_TRANSMIT_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700265 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300266 if (rndis_per_dev_params[configNr].dev) {
267 *outbuf = cpu_to_le32(
268 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 retval = 0;
270 }
271 break;
David Brownell7e27f182006-06-13 09:54:40 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* mandatory */
274 case OID_GEN_RECEIVE_BLOCK_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700275 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300276 if (rndis_per_dev_params[configNr].dev) {
277 *outbuf = cpu_to_le32(
278 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 retval = 0;
280 }
281 break;
David Brownell7e27f182006-06-13 09:54:40 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* mandatory */
284 case OID_GEN_VENDOR_ID:
David Brownell33376c12008-08-18 17:45:07 -0700285 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300286 *outbuf = cpu_to_le32(
287 rndis_per_dev_params[configNr].vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 retval = 0;
289 break;
David Brownell7e27f182006-06-13 09:54:40 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* mandatory */
292 case OID_GEN_VENDOR_DESCRIPTION:
David Brownell33376c12008-08-18 17:45:07 -0700293 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300294 if (rndis_per_dev_params[configNr].vendorDescr) {
295 length = strlen(rndis_per_dev_params[configNr].
296 vendorDescr);
297 memcpy(outbuf,
298 rndis_per_dev_params[configNr].vendorDescr,
299 length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400300 } else {
301 outbuf[0] = 0;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 retval = 0;
304 break;
305
306 case OID_GEN_VENDOR_DRIVER_VERSION:
David Brownell33376c12008-08-18 17:45:07 -0700307 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700309 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 retval = 0;
311 break;
312
313 /* mandatory */
314 case OID_GEN_CURRENT_PACKET_FILTER:
David Brownell33376c12008-08-18 17:45:07 -0700315 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300316 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 retval = 0;
318 break;
319
320 /* mandatory */
321 case OID_GEN_MAXIMUM_TOTAL_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700322 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800323 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 retval = 0;
325 break;
326
327 /* mandatory */
328 case OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700329 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700330 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300331 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .media_state);
333 retval = 0;
334 break;
335
336 case OID_GEN_PHYSICAL_MEDIUM:
David Brownell33376c12008-08-18 17:45:07 -0700337 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300338 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 retval = 0;
340 break;
341
342 /* The RNDIS specification is incomplete/wrong. Some versions
343 * of MS-Windows expect OIDs that aren't specified there. Other
344 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
345 */
346 case OID_GEN_MAC_OPTIONS: /* from WinME */
David Brownell33376c12008-08-18 17:45:07 -0700347 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800348 *outbuf = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
350 | NDIS_MAC_OPTION_FULL_DUPLEX);
351 retval = 0;
352 break;
353
354 /* statistics OIDs (table 4-2) */
355
356 /* mandatory */
357 case OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700358 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700359 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700360 if (stats) {
361 *outbuf = cpu_to_le32(stats->tx_packets
362 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 break;
366
367 /* mandatory */
368 case OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700369 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700370 pr_debug("%s: OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700371 if (stats) {
372 *outbuf = cpu_to_le32(stats->rx_packets
373 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 break;
David Brownell7e27f182006-06-13 09:54:40 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* mandatory */
379 case OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700380 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700381 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700382 if (stats) {
383 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 break;
David Brownell7e27f182006-06-13 09:54:40 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* mandatory */
389 case OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700390 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700391 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700392 if (stats) {
393 *outbuf = cpu_to_le32(stats->rx_errors);
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_RCV_NO_BUFFER:
David Brownell33376c12008-08-18 17:45:07 -0700400 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700401 if (stats) {
402 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 break;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* ieee802.3 OIDs (table 4-3) */
408
409 /* mandatory */
410 case OID_802_3_PERMANENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700411 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300412 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300414 memcpy(outbuf,
415 rndis_per_dev_params[configNr].host_mac,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 length);
417 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 break;
David Brownell7e27f182006-06-13 09:54:40 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* mandatory */
422 case OID_802_3_CURRENT_ADDRESS:
David Brownell33376c12008-08-18 17:45:07 -0700423 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300424 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300426 memcpy(outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 rndis_per_dev_params [configNr].host_mac,
428 length);
429 retval = 0;
430 }
431 break;
David Brownell7e27f182006-06-13 09:54:40 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* mandatory */
434 case OID_802_3_MULTICAST_LIST:
David Brownell33376c12008-08-18 17:45:07 -0700435 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300437 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 retval = 0;
439 break;
David Brownell7e27f182006-06-13 09:54:40 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* mandatory */
442 case OID_802_3_MAXIMUM_LIST_SIZE:
David Brownell33376c12008-08-18 17:45:07 -0700443 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300445 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 retval = 0;
447 break;
David Brownell7e27f182006-06-13 09:54:40 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 case OID_802_3_MAC_OPTIONS:
David Brownell33376c12008-08-18 17:45:07 -0700450 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700451 *outbuf = cpu_to_le32(0);
452 retval = 0;
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:
David Brownell33376c12008-08-18 17:45:07 -0700459 pr_debug("%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:
David Brownell33376c12008-08-18 17:45:07 -0700468 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300469 *outbuf = 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:
David Brownell33376c12008-08-18 17:45:07 -0700475 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300476 *outbuf = 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
Mihai Donțua1df4e42010-09-08 02:54:02 +0300487 resp->InformationBufferLength = cpu_to_le32(length);
488 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
Mihai Donțua1df4e42010-09-08 02:54:02 +0300493static 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{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300496 rndis_set_cmplt_type *resp;
497 int i, retval = -ENOTSUPP;
498 struct rndis_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (!r)
501 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300502 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!resp)
504 return -ENOMEM;
505
David Brownell340600a2005-04-28 13:45:25 -0700506 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700507 pr_debug("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 Brownell33376c12008-08-18 17:45:07 -0700509 pr_debug("%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
Mihai Donțua1df4e42010-09-08 02:54:02 +0300517 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 Brownell33376c12008-08-18 17:45:07 -0700528 pr_debug("%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))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300540 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 } else {
542 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300543 netif_carrier_off(params->dev);
544 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
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 */
David Brownell33376c12008-08-18 17:45:07 -0700550 pr_debug("%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
Mihai Donțua1df4e42010-09-08 02:54:02 +0300566static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300568 rndis_init_cmplt_type *resp;
569 rndis_resp_t *r;
570 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
Mihai Donțua1df4e42010-09-08 02:54:02 +0300575 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700576 if (!r)
577 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300578 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700579
Mihai Donțua1df4e42010-09-08 02:54:02 +0300580 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INITIALIZE_CMPLT);
581 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300583 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
584 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
585 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
586 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
587 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
Ofir Cohenaef90b72012-07-31 12:37:04 +0200588 resp->MaxPacketsPerTransfer = cpu_to_le32(params->max_pkt_per_xfer);
Ofir Cohen76624ed2012-09-09 10:27:58 +0300589 resp->MaxTransferSize = cpu_to_le32(params->max_pkt_per_xfer *
590 (params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300591 + sizeof(struct ethhdr)
592 + sizeof(struct rndis_packet_msg_type)
Ofir Cohen76624ed2012-09-09 10:27:58 +0300593
594 + 22));
595 resp->PacketAlignmentFactor = cpu_to_le32(params->pkt_alignment_factor);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300596 resp->AFListOffset = cpu_to_le32(0);
597 resp->AFListSize = 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
Mihai Donțua1df4e42010-09-08 02:54:02 +0300603static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300606 rndis_resp_t *r;
607 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700608
David Brownell33376c12008-08-18 17:45:07 -0700609 /* pr_debug("%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 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300619 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;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300623 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700624
Mihai Donțua1df4e42010-09-08 02:54:02 +0300625 resp->MessageType = 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
Mihai Donțua1df4e42010-09-08 02:54:02 +0300628 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700629 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300630 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700631 le32_to_cpu(buf->InformationBufferLength),
632 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300634 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
635 resp->MessageLength = cpu_to_le32(sizeof *resp);
636 resp->InformationBufferLength = cpu_to_le32(0);
637 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300639 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700640
David Brownell15b2d2b2008-06-19 18:19:16 -0700641 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
643}
644
Mihai Donțua1df4e42010-09-08 02:54:02 +0300645static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300647 u32 BufLength, BufOffset;
648 rndis_set_cmplt_type *resp;
649 rndis_resp_t *r;
650 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700651
Mihai Donțua1df4e42010-09-08 02:54:02 +0300652 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700653 if (!r)
654 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300655 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Mihai Donțua1df4e42010-09-08 02:54:02 +0300657 BufLength = le32_to_cpu(buf->InformationBufferLength);
658 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
David Brownell15b2d2b2008-06-19 18:19:16 -0700660#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700661 pr_debug("%s: Length: %d\n", __func__, BufLength);
662 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
663 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700666 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
David Brownell7e27f182006-06-13 09:54:40 -0700668
David Brownell33376c12008-08-18 17:45:07 -0700669 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#endif
David Brownell7e27f182006-06-13 09:54:40 -0700671
Mihai Donțua1df4e42010-09-08 02:54:02 +0300672 resp->MessageType = cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
673 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300675 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
676 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
677 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700678 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300679 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700680
David Brownell15b2d2b2008-06-19 18:19:16 -0700681 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683}
684
Mihai Donțua1df4e42010-09-08 02:54:02 +0300685static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300687 rndis_reset_cmplt_type *resp;
688 rndis_resp_t *r;
689 struct rndis_params *params = rndis_per_dev_params + configNr;
Rajkumar Raghupathy0a9a44c2013-02-22 19:57:36 +0530690 u32 length;
691 u8 *xbuf;
692
693 /* drain the response queue */
694 while ((xbuf = rndis_get_next_response(configNr, &length)))
695 rndis_free_response(configNr, xbuf);
David Brownell7e27f182006-06-13 09:54:40 -0700696
Mihai Donțua1df4e42010-09-08 02:54:02 +0300697 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700698 if (!r)
699 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300700 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700701
Mihai Donțua1df4e42010-09-08 02:54:02 +0300702 resp->MessageType = cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
703 resp->MessageLength = cpu_to_le32(16);
704 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300706 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700707
David Brownell15b2d2b2008-06-19 18:19:16 -0700708 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710}
711
Mihai Donțua1df4e42010-09-08 02:54:02 +0300712static int rndis_keepalive_response(int configNr,
713 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300715 rndis_keepalive_cmplt_type *resp;
716 rndis_resp_t *r;
717 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
720
Mihai Donțua1df4e42010-09-08 02:54:02 +0300721 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700722 if (!r)
723 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300724 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700725
Mihai Donțua1df4e42010-09-08 02:54:02 +0300726 resp->MessageType = cpu_to_le32(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 REMOTE_NDIS_KEEPALIVE_CMPLT);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300728 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300730 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700731
David Brownell15b2d2b2008-06-19 18:19:16 -0700732 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734}
735
736
David Brownell7e27f182006-06-13 09:54:40 -0700737/*
738 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300740static int rndis_indicate_status_msg(int configNr, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300742 rndis_indicate_status_msg_type *resp;
743 rndis_resp_t *r;
744 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700745
David Brownell15b2d2b2008-06-19 18:19:16 -0700746 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700747 return -ENOTSUPP;
748
Mihai Donțua1df4e42010-09-08 02:54:02 +0300749 r = rndis_add_response(configNr,
750 sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700751 if (!r)
752 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300753 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700754
Mihai Donțua1df4e42010-09-08 02:54:02 +0300755 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INDICATE_STATUS_MSG);
756 resp->MessageLength = cpu_to_le32(20);
757 resp->Status = cpu_to_le32(status);
758 resp->StatusBufferLength = cpu_to_le32(0);
759 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700760
David Brownell15b2d2b2008-06-19 18:19:16 -0700761 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 0;
763}
764
Mihai Donțua1df4e42010-09-08 02:54:02 +0300765int rndis_signal_connect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300767 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 = NDIS_MEDIA_STATE_CONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300769 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 RNDIS_STATUS_MEDIA_CONNECT);
771}
772
Mihai Donțua1df4e42010-09-08 02:54:02 +0300773int rndis_signal_disconnect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300775 rndis_per_dev_params[configNr].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300777 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 RNDIS_STATUS_MEDIA_DISCONNECT);
779}
780
Mihai Donțua1df4e42010-09-08 02:54:02 +0300781void rndis_uninit(int configNr)
David Brownell340600a2005-04-28 13:45:25 -0700782{
David Brownell486e2df2005-05-24 17:51:52 -0700783 u8 *buf;
784 u32 length;
785
David Brownell340600a2005-04-28 13:45:25 -0700786 if (configNr >= RNDIS_MAX_CONFIGS)
787 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300788 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700789
790 /* drain the response queue */
791 while ((buf = rndis_get_next_response(configNr, &length)))
792 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700793}
794
Mihai Donțua1df4e42010-09-08 02:54:02 +0300795void rndis_set_host_mac(int configNr, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300797 rndis_per_dev_params[configNr].host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
David Brownell7e27f182006-06-13 09:54:40 -0700800/*
801 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300803int rndis_msg_parser(u8 configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 u32 MsgType, MsgLength;
806 __le32 *tmp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300807 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!buf)
810 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700811
Mihai Donțua1df4e42010-09-08 02:54:02 +0300812 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700813 MsgType = get_unaligned_le32(tmp++);
814 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (configNr >= RNDIS_MAX_CONFIGS)
817 return -ENOTSUPP;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300818 params = &rndis_per_dev_params[configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700819
David Brownell340600a2005-04-28 13:45:25 -0700820 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
821 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
822 * and normal HC level polling to see if there's any IN traffic.
823 */
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700826 switch (MsgType) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 case REMOTE_NDIS_INITIALIZE_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700828 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300829 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300831 return rndis_init_response(configNr,
832 (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 case REMOTE_NDIS_HALT_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700835 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300836 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 params->state = RNDIS_UNINITIALIZED;
838 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300839 netif_carrier_off(params->dev);
840 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 case REMOTE_NDIS_QUERY_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300845 return rndis_query_response(configNr,
846 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 case REMOTE_NDIS_SET_MSG:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300849 return rndis_set_response(configNr,
850 (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 case REMOTE_NDIS_RESET_MSG:
David Brownell33376c12008-08-18 17:45:07 -0700853 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300854 __func__);
855 return rndis_reset_response(configNr,
856 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 case REMOTE_NDIS_KEEPALIVE_MSG:
859 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700860 if (rndis_debug > 1)
David Brownell33376c12008-08-18 17:45:07 -0700861 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300862 __func__);
863 return rndis_keepalive_response(configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700864 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700866
867 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* At least Windows XP emits some undefined RNDIS messages.
869 * In one case those messages seemed to relate to the host
870 * suspending itself.
871 */
David Brownell00274922007-11-19 12:58:36 -0800872 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300873 __func__, MsgType, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 {
875 unsigned i;
876 for (i = 0; i < MsgLength; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700877 pr_debug("%03d: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 " %02x %02x %02x %02x"
879 " %02x %02x %02x %02x"
880 " %02x %02x %02x %02x"
881 " %02x %02x %02x %02x"
882 "\n",
883 i,
884 buf[i], buf [i+1],
885 buf[i+2], buf[i+3],
886 buf[i+4], buf [i+5],
887 buf[i+6], buf[i+7],
888 buf[i+8], buf [i+9],
889 buf[i+10], buf[i+11],
890 buf[i+12], buf [i+13],
891 buf[i+14], buf[i+15]);
892 }
893 }
894 break;
895 }
David Brownell7e27f182006-06-13 09:54:40 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -ENOTSUPP;
898}
899
David Brownell15b2d2b2008-06-19 18:19:16 -0700900int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700903
David Brownell15b2d2b2008-06-19 18:19:16 -0700904 if (!resp_avail)
905 return -EINVAL;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300908 if (!rndis_per_dev_params[i].used) {
909 rndis_per_dev_params[i].used = 1;
910 rndis_per_dev_params[i].resp_avail = resp_avail;
911 rndis_per_dev_params[i].v = v;
Mayank Ranae5d2b3d2012-09-05 13:12:26 +0530912 rndis_per_dev_params[i].max_pkt_per_xfer = 1;
Ofir Cohen76624ed2012-09-09 10:27:58 +0300913 rndis_per_dev_params[i].pkt_alignment_factor = 0;
David Brownell33376c12008-08-18 17:45:07 -0700914 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return i;
916 }
917 }
David Brownell33376c12008-08-18 17:45:07 -0700918 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700919
David Brownell15b2d2b2008-06-19 18:19:16 -0700920 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Mihai Donțua1df4e42010-09-08 02:54:02 +0300923void rndis_deregister(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300925 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (configNr >= RNDIS_MAX_CONFIGS) return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300928 rndis_per_dev_params[configNr].used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
David Brownell15b2d2b2008-06-19 18:19:16 -0700931int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
David Brownell33376c12008-08-18 17:45:07 -0700933 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700934 if (!dev)
935 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700937
Mihai Donțua1df4e42010-09-08 02:54:02 +0300938 rndis_per_dev_params[configNr].dev = dev;
939 rndis_per_dev_params[configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return 0;
942}
943
Mihai Donțua1df4e42010-09-08 02:54:02 +0300944int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
David Brownell33376c12008-08-18 17:45:07 -0700946 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!vendorDescr) return -1;
948 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700949
Mihai Donțua1df4e42010-09-08 02:54:02 +0300950 rndis_per_dev_params[configNr].vendorID = vendorID;
951 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954}
955
Mihai Donțua1df4e42010-09-08 02:54:02 +0300956int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
David Brownell33376c12008-08-18 17:45:07 -0700958 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 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].medium = medium;
962 rndis_per_dev_params[configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return 0;
965}
966
Ofir Cohenaef90b72012-07-31 12:37:04 +0200967void rndis_set_max_pkt_xfer(u8 configNr, u8 max_pkt_per_xfer)
968{
969 pr_debug("%s:\n", __func__);
970
971 rndis_per_dev_params[configNr].max_pkt_per_xfer = max_pkt_per_xfer;
972}
973
Ofir Cohen76624ed2012-09-09 10:27:58 +0300974void rndis_set_pkt_alignment_factor(u8 configNr, u8 pkt_alignment_factor)
975{
976 pr_debug("%s:\n", __func__);
977
978 rndis_per_dev_params[configNr].pkt_alignment_factor =
979 pkt_alignment_factor;
980}
981
Mihai Donțua1df4e42010-09-08 02:54:02 +0300982void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300984 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (!skb)
987 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300988 header = (void *)skb_push(skb, sizeof(*header));
989 memset(header, 0, sizeof *header);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800990 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300992 header->DataOffset = cpu_to_le32(36);
993 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Mihai Donțua1df4e42010-09-08 02:54:02 +0300996void rndis_free_response(int configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300998 rndis_resp_t *r;
999 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -07001000
Mihai Donțua1df4e42010-09-08 02:54:02 +03001001 list_for_each_safe(act, tmp,
1002 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001004 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (r && r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001006 list_del(&r->list);
1007 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 }
1010}
1011
Mihai Donțua1df4e42010-09-08 02:54:02 +03001012u8 *rndis_get_next_response(int configNr, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001014 rndis_resp_t *r;
1015 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001018
Mihai Donțua1df4e42010-09-08 02:54:02 +03001019 list_for_each_safe(act, tmp,
1020 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001022 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (!r->send) {
1024 r->send = 1;
1025 *length = r->length;
1026 return r->buf;
1027 }
1028 }
David Brownell7e27f182006-06-13 09:54:40 -07001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return NULL;
1031}
1032
Mihai Donțua1df4e42010-09-08 02:54:02 +03001033static rndis_resp_t *rndis_add_response(int configNr, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001035 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001036
Mihai Donțua1df4e42010-09-08 02:54:02 +03001037 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1038 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001040
Mihai Donțua1df4e42010-09-08 02:54:02 +03001041 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 r->length = length;
1043 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001044
Mihai Donțua1df4e42010-09-08 02:54:02 +03001045 list_add_tail(&r->list,
1046 &(rndis_per_dev_params[configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return r;
1048}
1049
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001050int rndis_rm_hdr(struct gether *port,
1051 struct sk_buff *skb,
1052 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
David Brownell6cdee102005-04-18 17:39:34 -07001054 /* tmp points to a struct rndis_packet_msg_type */
Mihai Donțua1df4e42010-09-08 02:54:02 +03001055 __le32 *tmp = (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
David Brownell6cdee102005-04-18 17:39:34 -07001057 /* MessageType, MessageLength */
Harvey Harrison35c26c22009-02-14 22:56:56 -08001058 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001059 != get_unaligned(tmp++)) {
1060 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001061 return -EINVAL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001062 }
David Brownell6cdee102005-04-18 17:39:34 -07001063 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
David Brownell6cdee102005-04-18 17:39:34 -07001065 /* DataOffset, DataLength */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001066 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1067 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001068 return -EOVERFLOW;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001069 }
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001070 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001071
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001072 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0;
1074}
1075
Mihai Donțua1df4e42010-09-08 02:54:02 +03001076#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001078static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001080 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001081
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001082 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 "Config Nr. %d\n"
1084 "used : %s\n"
1085 "state : %s\n"
1086 "medium : 0x%08X\n"
1087 "speed : %d\n"
1088 "cable : %s\n"
1089 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001090 "vendor : %s\n",
1091 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ({ char *s = "?";
1093 switch (param->state) {
1094 case RNDIS_UNINITIALIZED:
1095 s = "RNDIS_UNINITIALIZED"; break;
1096 case RNDIS_INITIALIZED:
1097 s = "RNDIS_INITIALIZED"; break;
1098 case RNDIS_DATA_INITIALIZED:
1099 s = "RNDIS_DATA_INITIALIZED"; break;
1100 }; s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001101 param->medium,
1102 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001104 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001108static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001109 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001111 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 u32 speed = 0;
1113 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 for (i = 0; i < count; i++) {
1116 char c;
1117 if (get_user(c, buffer))
1118 return -EFAULT;
1119 switch (c) {
1120 case '0':
1121 case '1':
1122 case '2':
1123 case '3':
1124 case '4':
1125 case '5':
1126 case '6':
1127 case '7':
1128 case '8':
1129 case '9':
1130 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001131 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
1133 case 'C':
1134 case 'c':
Mihai Donțua1df4e42010-09-08 02:54:02 +03001135 rndis_signal_connect(p->confignr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
1137 case 'D':
1138 case 'd':
1139 rndis_signal_disconnect(p->confignr);
1140 break;
David Brownell7e27f182006-06-13 09:54:40 -07001141 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001143 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
1145 }
David Brownell7e27f182006-06-13 09:54:40 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 buffer++;
1148 }
David Brownell7e27f182006-06-13 09:54:40 -07001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return count;
1151}
1152
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001153static int rndis_proc_open(struct inode *inode, struct file *file)
1154{
1155 return single_open(file, rndis_proc_show, PDE(inode)->data);
1156}
1157
1158static const struct file_operations rndis_proc_fops = {
1159 .owner = THIS_MODULE,
1160 .open = rndis_proc_open,
1161 .read = seq_read,
1162 .llseek = seq_lseek,
1163 .release = single_release,
1164 .write = rndis_proc_write,
1165};
1166
Mihai Donțua1df4e42010-09-08 02:54:02 +03001167#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1170
Mihai Donțua1df4e42010-09-08 02:54:02 +03001171#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Benoit Gobya6ccb732012-01-20 14:42:41 -08001173static bool rndis_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Henrik Kretzschmar793f03a2010-08-20 19:57:50 +02001175int rndis_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 u8 i;
1178
Benoit Gobya6ccb732012-01-20 14:42:41 -08001179 if (rndis_initialized)
1180 return 0;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1183#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1184 char name [20];
1185
Mihai Donțua1df4e42010-09-08 02:54:02 +03001186 sprintf(name, NAME_TEMPLATE, i);
1187 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001188 &rndis_proc_fops,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001189 (void *)(rndis_per_dev_params + i));
1190 if (!rndis_connect_state[i]) {
1191 pr_debug("%s: remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 while (i) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001193 sprintf(name, NAME_TEMPLATE, --i);
1194 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
David Brownell33376c12008-08-18 17:45:07 -07001196 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return -EIO;
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#endif
Mihai Donțua1df4e42010-09-08 02:54:02 +03001200 rndis_per_dev_params[i].confignr = i;
1201 rndis_per_dev_params[i].used = 0;
1202 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1203 rndis_per_dev_params[i].media_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 = NDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001205 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
David Brownell7e27f182006-06-13 09:54:40 -07001207
Benoit Gobya6ccb732012-01-20 14:42:41 -08001208 rndis_initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return 0;
1210}
1211
Mihai Donțua1df4e42010-09-08 02:54:02 +03001212void rndis_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001214#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 u8 i;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001216 char name[20];
Benoit Gobya6ccb732012-01-20 14:42:41 -08001217#endif
David Brownell7e27f182006-06-13 09:54:40 -07001218
Benoit Gobya6ccb732012-01-20 14:42:41 -08001219 if (!rndis_initialized)
1220 return;
1221 rndis_initialized = false;
1222
1223#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001225 sprintf(name, NAME_TEMPLATE, i);
1226 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228#endif
1229}