blob: 4c36694a01326a5eb3ad6889bdd11ee7c9ff7f8c [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/list.h>
29#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Alexey Dobriyane184d5f2008-05-14 16:25:13 -070031#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/netdevice.h>
33
34#include <asm/io.h>
35#include <asm/byteorder.h>
David Brownell6cdee102005-04-18 17:39:34 -070036#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
David Brownell15b2d2b2008-06-19 18:19:16 -070039#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "rndis.h"
42
43
44/* The driver for your USB chip needs to support ep0 OUT to work with
45 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
46 *
47 * Windows hosts need an INF file like Documentation/usb/linux.inf
48 * and will be happier if you provide the host_addr module parameter.
49 */
50
51#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070053module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
59#define RNDIS_MAX_CONFIGS 1
60
61
Mihai Donțua1df4e42010-09-08 02:54:02 +030062static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030065static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* Function Prototypes */
Mihai Donțua1df4e42010-09-08 02:54:02 +030068static rndis_resp_t *rndis_add_response(int configNr, u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
David Brownell340600a2005-04-28 13:45:25 -070071/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030072static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070073{
74 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000075 RNDIS_OID_GEN_SUPPORTED_LIST,
76 RNDIS_OID_GEN_HARDWARE_STATUS,
77 RNDIS_OID_GEN_MEDIA_SUPPORTED,
78 RNDIS_OID_GEN_MEDIA_IN_USE,
79 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
80 RNDIS_OID_GEN_LINK_SPEED,
81 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
82 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
83 RNDIS_OID_GEN_VENDOR_ID,
84 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
85 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
86 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
87 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
88 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
89 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -070090
David Brownell340600a2005-04-28 13:45:25 -070091 /* the statistical stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000092 RNDIS_OID_GEN_XMIT_OK,
93 RNDIS_OID_GEN_RCV_OK,
94 RNDIS_OID_GEN_XMIT_ERROR,
95 RNDIS_OID_GEN_RCV_ERROR,
96 RNDIS_OID_GEN_RCV_NO_BUFFER,
David Brownell340600a2005-04-28 13:45:25 -070097#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij8cdddc32012-05-11 22:16:08 +000098 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
99 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
100 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
101 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
102 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
103 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
104 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
105 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
106 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
107 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
108 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
109 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
110 RNDIS_OID_GEN_RCV_CRC_ERROR,
111 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
David Brownell340600a2005-04-28 13:45:25 -0700112#endif /* RNDIS_OPTIONAL_STATS */
113
David Brownell7e27f182006-06-13 09:54:40 -0700114 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700115 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000116 RNDIS_OID_802_3_PERMANENT_ADDRESS,
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000117 RNDIS_OID_802_3_CURRENT_ADDRESS,
118 RNDIS_OID_802_3_MULTICAST_LIST,
119 RNDIS_OID_802_3_MAC_OPTIONS,
120 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700121
David Brownell340600a2005-04-28 13:45:25 -0700122 /* the statistical stuff */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000123 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
124 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
125 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700126#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000127 RNDIS_OID_802_3_XMIT_DEFERRED,
128 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
129 RNDIS_OID_802_3_RCV_OVERRUN,
130 RNDIS_OID_802_3_XMIT_UNDERRUN,
131 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
132 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
133 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700134#endif /* RNDIS_OPTIONAL_STATS */
135
136#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700137 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
138 * don't say what they mean ... and the NDIS specs are often
139 * confusing and/or ambiguous in this context. (That is, more
140 * so than their specs for the other OIDs.)
141 *
142 * FIXME someone who knows what these should do, please
143 * implement them!
144 */
David Brownell340600a2005-04-28 13:45:25 -0700145
146 /* power management */
147 OID_PNP_CAPABILITIES,
148 OID_PNP_QUERY_POWER,
149 OID_PNP_SET_POWER,
150
151#ifdef RNDIS_WAKEUP
152 /* wake up host */
153 OID_PNP_ENABLE_WAKE_UP,
154 OID_PNP_ADD_WAKE_UP_PATTERN,
155 OID_PNP_REMOVE_WAKE_UP_PATTERN,
156#endif /* RNDIS_WAKEUP */
157#endif /* RNDIS_PM */
158};
159
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* NDIS Functions */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300162static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
163 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300165 int retval = -ENOTSUPP;
166 u32 length = 4; /* usually */
167 __le32 *outbuf;
168 int i, count;
169 rndis_query_cmplt_type *resp;
170 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700171 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700172 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300175 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700178
179 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700180 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700181 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700182 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700183 get_unaligned_le32(&buf[i]),
184 get_unaligned_le32(&buf[i + 4]),
185 get_unaligned_le32(&buf[i + 8]),
186 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700187 }
188 }
189
190 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300191 outbuf = (__le32 *)&resp[1];
192 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700193
David Brownell15b2d2b2008-06-19 18:19:16 -0700194 net = rndis_per_dev_params[configNr].dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700195 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 switch (OID) {
198
199 /* general oids (table 4-1) */
200
201 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000202 case RNDIS_OID_GEN_SUPPORTED_LIST:
203 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300204 length = sizeof(oid_supported_list);
205 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300207 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 retval = 0;
209 break;
David Brownell7e27f182006-06-13 09:54:40 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000212 case RNDIS_OID_GEN_HARDWARE_STATUS:
213 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700214 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700216 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * reddite ergo quae sunt Caesaris Caesari
218 * et quae sunt Dei Deo!
219 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300220 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 retval = 0;
222 break;
David Brownell7e27f182006-06-13 09:54:40 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000225 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
226 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300227 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 retval = 0;
229 break;
David Brownell7e27f182006-06-13 09:54:40 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000232 case RNDIS_OID_GEN_MEDIA_IN_USE:
233 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* one medium, one transport... (maybe you do it better) */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300235 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 retval = 0;
237 break;
David Brownell7e27f182006-06-13 09:54:40 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000240 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
241 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300242 if (rndis_per_dev_params[configNr].dev) {
243 *outbuf = cpu_to_le32(
244 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 break;
David Brownell7e27f182006-06-13 09:54:40 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000250 case RNDIS_OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700251 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000252 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300253 if (rndis_per_dev_params[configNr].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +0000254 == RNDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300255 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300257 *outbuf = cpu_to_le32(
258 rndis_per_dev_params[configNr].speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 retval = 0;
260 break;
261
262 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000263 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
264 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300265 if (rndis_per_dev_params[configNr].dev) {
266 *outbuf = cpu_to_le32(
267 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 retval = 0;
269 }
270 break;
David Brownell7e27f182006-06-13 09:54:40 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000273 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
274 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300275 if (rndis_per_dev_params[configNr].dev) {
276 *outbuf = cpu_to_le32(
277 rndis_per_dev_params[configNr].dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 retval = 0;
279 }
280 break;
David Brownell7e27f182006-06-13 09:54:40 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000283 case RNDIS_OID_GEN_VENDOR_ID:
284 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300285 *outbuf = cpu_to_le32(
286 rndis_per_dev_params[configNr].vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 retval = 0;
288 break;
David Brownell7e27f182006-06-13 09:54:40 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000291 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
292 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300293 if (rndis_per_dev_params[configNr].vendorDescr) {
294 length = strlen(rndis_per_dev_params[configNr].
295 vendorDescr);
296 memcpy(outbuf,
297 rndis_per_dev_params[configNr].vendorDescr,
298 length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400299 } else {
300 outbuf[0] = 0;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 retval = 0;
303 break;
304
Linus Walleij8cdddc32012-05-11 22:16:08 +0000305 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
306 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700308 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 retval = 0;
310 break;
311
312 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000313 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
314 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300315 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 retval = 0;
317 break;
318
319 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000320 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
321 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800322 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 retval = 0;
324 break;
325
326 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000327 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700328 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000329 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300330 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 .media_state);
332 retval = 0;
333 break;
334
Linus Walleij8cdddc32012-05-11 22:16:08 +0000335 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
336 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300337 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 retval = 0;
339 break;
340
341 /* The RNDIS specification is incomplete/wrong. Some versions
342 * of MS-Windows expect OIDs that aren't specified there. Other
343 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
344 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000345 case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
346 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800347 *outbuf = cpu_to_le32(
Linus Walleije20289e2012-05-11 22:17:19 +0000348 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
349 | RNDIS_MAC_OPTION_FULL_DUPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 retval = 0;
351 break;
352
353 /* statistics OIDs (table 4-2) */
354
355 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000356 case RNDIS_OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700357 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000358 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700359 if (stats) {
360 *outbuf = cpu_to_le32(stats->tx_packets
361 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 break;
365
366 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000367 case RNDIS_OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700368 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000369 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700370 if (stats) {
371 *outbuf = cpu_to_le32(stats->rx_packets
372 - stats->rx_errors - stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 break;
David Brownell7e27f182006-06-13 09:54:40 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000378 case RNDIS_OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700379 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000380 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700381 if (stats) {
382 *outbuf = cpu_to_le32(stats->tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385 break;
David Brownell7e27f182006-06-13 09:54:40 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000388 case RNDIS_OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700389 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000390 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700391 if (stats) {
392 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 break;
David Brownell7e27f182006-06-13 09:54:40 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000398 case RNDIS_OID_GEN_RCV_NO_BUFFER:
399 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700400 if (stats) {
401 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 break;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* ieee802.3 OIDs (table 4-3) */
407
408 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000409 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000410 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300411 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300413 memcpy(outbuf,
414 rndis_per_dev_params[configNr].host_mac,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 length);
416 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 break;
David Brownell7e27f182006-06-13 09:54:40 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000421 case RNDIS_OID_802_3_CURRENT_ADDRESS:
422 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300423 if (rndis_per_dev_params[configNr].dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 length = ETH_ALEN;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300425 memcpy(outbuf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 rndis_per_dev_params [configNr].host_mac,
427 length);
428 retval = 0;
429 }
430 break;
David Brownell7e27f182006-06-13 09:54:40 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000433 case RNDIS_OID_802_3_MULTICAST_LIST:
434 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300436 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 retval = 0;
438 break;
David Brownell7e27f182006-06-13 09:54:40 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000441 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
442 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300444 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 retval = 0;
446 break;
David Brownell7e27f182006-06-13 09:54:40 -0700447
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000448 case RNDIS_OID_802_3_MAC_OPTIONS:
449 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700450 *outbuf = cpu_to_le32(0);
451 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453
454 /* ieee802.3 statistics OIDs (table 4-4) */
455
456 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000457 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
458 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700459 if (stats) {
460 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 retval = 0;
462 }
463 break;
David Brownell7e27f182006-06-13 09:54:40 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000466 case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
467 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300468 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 retval = 0;
470 break;
David Brownell7e27f182006-06-13 09:54:40 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000473 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
474 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300475 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 retval = 0;
477 break;
David Brownell7e27f182006-06-13 09:54:40 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 default:
David Brownell00274922007-11-19 12:58:36 -0800480 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800481 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
David Brownell340600a2005-04-28 13:45:25 -0700483 if (retval < 0)
484 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700485
Mihai Donțua1df4e42010-09-08 02:54:02 +0300486 resp->InformationBufferLength = cpu_to_le32(length);
487 r->length = length + sizeof(*resp);
488 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return retval;
490}
491
Mihai Donțua1df4e42010-09-08 02:54:02 +0300492static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
493 rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300495 rndis_set_cmplt_type *resp;
496 int i, retval = -ENOTSUPP;
497 struct rndis_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (!r)
500 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300501 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!resp)
503 return -ENOMEM;
504
David Brownell340600a2005-04-28 13:45:25 -0700505 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700506 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700507 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700508 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700509 get_unaligned_le32(&buf[i]),
510 get_unaligned_le32(&buf[i + 4]),
511 get_unaligned_le32(&buf[i + 8]),
512 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Mihai Donțua1df4e42010-09-08 02:54:02 +0300516 params = &rndis_per_dev_params[configNr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 switch (OID) {
Linus Walleij8cdddc32012-05-11 22:16:08 +0000518 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
David Brownell340600a2005-04-28 13:45:25 -0700520 /* these NDIS_PACKET_TYPE_* bitflags are shared with
521 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
523 * PROMISCUOUS, DIRECTED,
524 * MULTICAST, ALL_MULTICAST, BROADCAST
525 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700526 *params->filter = (u16)get_unaligned_le32(buf);
Linus Walleij8cdddc32012-05-11 22:16:08 +0000527 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800528 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* this call has a significant side effect: it's
531 * what makes the packet flow start and stop, like
532 * activating the CDC Ethernet altsetting.
533 */
David Brownell340600a2005-04-28 13:45:25 -0700534 retval = 0;
535 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 params->state = RNDIS_DATA_INITIALIZED;
537 netif_carrier_on(params->dev);
538 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300539 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 } else {
541 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300542 netif_carrier_off(params->dev);
543 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 break;
David Brownell7e27f182006-06-13 09:54:40 -0700546
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000547 case RNDIS_OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700548 /* I think we can ignore this */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000549 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 retval = 0;
551 break;
David Brownell340600a2005-04-28 13:45:25 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 default:
David Brownell00274922007-11-19 12:58:36 -0800554 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800555 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
David Brownell7e27f182006-06-13 09:54:40 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return retval;
559}
560
David Brownell7e27f182006-06-13 09:54:40 -0700561/*
562 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
564
Mihai Donțua1df4e42010-09-08 02:54:02 +0300565static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300567 rndis_init_cmplt_type *resp;
568 rndis_resp_t *r;
569 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700570
David Brownell15b2d2b2008-06-19 18:19:16 -0700571 if (!params->dev)
572 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700573
Mihai Donțua1df4e42010-09-08 02:54:02 +0300574 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700575 if (!r)
576 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300577 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700578
Linus Walleij51491162012-05-11 22:17:07 +0000579 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300580 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300582 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
583 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
584 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
585 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
586 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
587 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
588 resp->MaxTransferSize = cpu_to_le32(
David Brownell15b2d2b2008-06-19 18:19:16 -0700589 params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300590 + sizeof(struct ethhdr)
591 + sizeof(struct rndis_packet_msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 + 22);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300593 resp->PacketAlignmentFactor = cpu_to_le32(0);
594 resp->AFListOffset = cpu_to_le32(0);
595 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700596
David Brownell15b2d2b2008-06-19 18:19:16 -0700597 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599}
600
Mihai Donțua1df4e42010-09-08 02:54:02 +0300601static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300604 rndis_resp_t *r;
605 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700606
David Brownell33376c12008-08-18 17:45:07 -0700607 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700608 if (!params->dev)
609 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700610
Shaun Tancheff87637162006-02-22 19:47:19 -0800611 /*
612 * we need more memory:
613 * gen_ndis_query_resp expects enough space for
614 * rndis_query_cmplt_type followed by data.
615 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300617 r = rndis_add_response(configNr,
618 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700619 if (!r)
620 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300621 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700622
Linus Walleij51491162012-05-11 22:17:07 +0000623 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700625
Mihai Donțua1df4e42010-09-08 02:54:02 +0300626 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700627 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300628 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700629 le32_to_cpu(buf->InformationBufferLength),
630 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300632 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
633 resp->MessageLength = cpu_to_le32(sizeof *resp);
634 resp->InformationBufferLength = cpu_to_le32(0);
635 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300637 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700638
David Brownell15b2d2b2008-06-19 18:19:16 -0700639 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return 0;
641}
642
Mihai Donțua1df4e42010-09-08 02:54:02 +0300643static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300645 u32 BufLength, BufOffset;
646 rndis_set_cmplt_type *resp;
647 rndis_resp_t *r;
648 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700649
Mihai Donțua1df4e42010-09-08 02:54:02 +0300650 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700651 if (!r)
652 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300653 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Mihai Donțua1df4e42010-09-08 02:54:02 +0300655 BufLength = le32_to_cpu(buf->InformationBufferLength);
656 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
David Brownell15b2d2b2008-06-19 18:19:16 -0700658#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700659 pr_debug("%s: Length: %d\n", __func__, BufLength);
660 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
661 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700664 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
David Brownell7e27f182006-06-13 09:54:40 -0700666
David Brownell33376c12008-08-18 17:45:07 -0700667 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#endif
David Brownell7e27f182006-06-13 09:54:40 -0700669
Linus Walleij51491162012-05-11 22:17:07 +0000670 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300671 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300673 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
674 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
675 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700676 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300677 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700678
David Brownell15b2d2b2008-06-19 18:19:16 -0700679 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681}
682
Mihai Donțua1df4e42010-09-08 02:54:02 +0300683static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300685 rndis_reset_cmplt_type *resp;
686 rndis_resp_t *r;
687 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700688
Mihai Donțua1df4e42010-09-08 02:54:02 +0300689 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700690 if (!r)
691 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300692 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700693
Linus Walleij51491162012-05-11 22:17:07 +0000694 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300695 resp->MessageLength = cpu_to_le32(16);
696 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300698 resp->AddressingReset = cpu_to_le32(1);
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_keepalive_response(int configNr,
705 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300707 rndis_keepalive_cmplt_type *resp;
708 rndis_resp_t *r;
709 struct rndis_params *params = rndis_per_dev_params + configNr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
712
Mihai Donțua1df4e42010-09-08 02:54:02 +0300713 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700714 if (!r)
715 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300716 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700717
Linus Walleij51491162012-05-11 22:17:07 +0000718 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300719 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300721 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700722
David Brownell15b2d2b2008-06-19 18:19:16 -0700723 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725}
726
727
David Brownell7e27f182006-06-13 09:54:40 -0700728/*
729 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300731static int rndis_indicate_status_msg(int configNr, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300733 rndis_indicate_status_msg_type *resp;
734 rndis_resp_t *r;
735 struct rndis_params *params = rndis_per_dev_params + configNr;
David Brownell7e27f182006-06-13 09:54:40 -0700736
David Brownell15b2d2b2008-06-19 18:19:16 -0700737 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700738 return -ENOTSUPP;
739
Mihai Donțua1df4e42010-09-08 02:54:02 +0300740 r = rndis_add_response(configNr,
741 sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700742 if (!r)
743 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300744 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700745
Linus Walleij51491162012-05-11 22:17:07 +0000746 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300747 resp->MessageLength = cpu_to_le32(20);
748 resp->Status = cpu_to_le32(status);
749 resp->StatusBufferLength = cpu_to_le32(0);
750 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700751
David Brownell15b2d2b2008-06-19 18:19:16 -0700752 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754}
755
Mihai Donțua1df4e42010-09-08 02:54:02 +0300756int rndis_signal_connect(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300758 rndis_per_dev_params[configNr].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +0000759 = RNDIS_MEDIA_STATE_CONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300760 return rndis_indicate_status_msg(configNr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 RNDIS_STATUS_MEDIA_CONNECT);
762}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500763EXPORT_SYMBOL_GPL(rndis_signal_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Mihai Donțua1df4e42010-09-08 02:54:02 +0300765int rndis_signal_disconnect(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 Walleij17c51b62012-05-11 22:16:39 +0000768 = RNDIS_MEDIA_STATE_DISCONNECTED;
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_DISCONNECT);
771}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500772EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Mihai Donțua1df4e42010-09-08 02:54:02 +0300774void rndis_uninit(int configNr)
David Brownell340600a2005-04-28 13:45:25 -0700775{
David Brownell486e2df2005-05-24 17:51:52 -0700776 u8 *buf;
777 u32 length;
778
David Brownell340600a2005-04-28 13:45:25 -0700779 if (configNr >= RNDIS_MAX_CONFIGS)
780 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300781 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700782
783 /* drain the response queue */
784 while ((buf = rndis_get_next_response(configNr, &length)))
785 rndis_free_response(configNr, buf);
David Brownell340600a2005-04-28 13:45:25 -0700786}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500787EXPORT_SYMBOL_GPL(rndis_uninit);
David Brownell340600a2005-04-28 13:45:25 -0700788
Mihai Donțua1df4e42010-09-08 02:54:02 +0300789void rndis_set_host_mac(int configNr, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300791 rndis_per_dev_params[configNr].host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500793EXPORT_SYMBOL_GPL(rndis_set_host_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David Brownell7e27f182006-06-13 09:54:40 -0700795/*
796 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300798int rndis_msg_parser(u8 configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 u32 MsgType, MsgLength;
801 __le32 *tmp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300802 struct rndis_params *params;
David Brownell7e27f182006-06-13 09:54:40 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!buf)
805 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700806
Mihai Donțua1df4e42010-09-08 02:54:02 +0300807 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700808 MsgType = get_unaligned_le32(tmp++);
809 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (configNr >= RNDIS_MAX_CONFIGS)
812 return -ENOTSUPP;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300813 params = &rndis_per_dev_params[configNr];
David Brownell7e27f182006-06-13 09:54:40 -0700814
David Brownell340600a2005-04-28 13:45:25 -0700815 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
816 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
817 * and normal HC level polling to see if there's any IN traffic.
818 */
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700821 switch (MsgType) {
Linus Walleij51491162012-05-11 22:17:07 +0000822 case RNDIS_MSG_INIT:
823 pr_debug("%s: RNDIS_MSG_INIT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300824 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300826 return rndis_init_response(configNr,
827 (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700828
Linus Walleij51491162012-05-11 22:17:07 +0000829 case RNDIS_MSG_HALT:
830 pr_debug("%s: RNDIS_MSG_HALT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300831 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 params->state = RNDIS_UNINITIALIZED;
833 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300834 netif_carrier_off(params->dev);
835 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700838
Linus Walleij51491162012-05-11 22:17:07 +0000839 case RNDIS_MSG_QUERY:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300840 return rndis_query_response(configNr,
841 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700842
Linus Walleij51491162012-05-11 22:17:07 +0000843 case RNDIS_MSG_SET:
Mihai Donțua1df4e42010-09-08 02:54:02 +0300844 return rndis_set_response(configNr,
845 (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700846
Linus Walleij51491162012-05-11 22:17:07 +0000847 case RNDIS_MSG_RESET:
848 pr_debug("%s: RNDIS_MSG_RESET\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300849 __func__);
850 return rndis_reset_response(configNr,
851 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Walleij51491162012-05-11 22:17:07 +0000853 case RNDIS_MSG_KEEPALIVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700855 if (rndis_debug > 1)
Linus Walleij51491162012-05-11 22:17:07 +0000856 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300857 __func__);
858 return rndis_keepalive_response(configNr,
David Brownell7e27f182006-06-13 09:54:40 -0700859 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700861
862 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* At least Windows XP emits some undefined RNDIS messages.
864 * In one case those messages seemed to relate to the host
865 * suspending itself.
866 */
David Brownell00274922007-11-19 12:58:36 -0800867 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300868 __func__, MsgType, MsgLength);
Andy Shevchenkod3091cf2012-08-07 19:07:58 +0300869 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
870 buf, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872 }
David Brownell7e27f182006-06-13 09:54:40 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return -ENOTSUPP;
875}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500876EXPORT_SYMBOL_GPL(rndis_msg_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
David Brownell15b2d2b2008-06-19 18:19:16 -0700878int rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 u8 i;
David Brownell7e27f182006-06-13 09:54:40 -0700881
David Brownell15b2d2b2008-06-19 18:19:16 -0700882 if (!resp_avail)
883 return -EINVAL;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300886 if (!rndis_per_dev_params[i].used) {
887 rndis_per_dev_params[i].used = 1;
888 rndis_per_dev_params[i].resp_avail = resp_avail;
889 rndis_per_dev_params[i].v = v;
David Brownell33376c12008-08-18 17:45:07 -0700890 pr_debug("%s: configNr = %d\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return i;
892 }
893 }
David Brownell33376c12008-08-18 17:45:07 -0700894 pr_debug("failed\n");
David Brownell7e27f182006-06-13 09:54:40 -0700895
David Brownell15b2d2b2008-06-19 18:19:16 -0700896 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500898EXPORT_SYMBOL_GPL(rndis_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Mihai Donțua1df4e42010-09-08 02:54:02 +0300900void rndis_deregister(int configNr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300902 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (configNr >= RNDIS_MAX_CONFIGS) return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300905 rndis_per_dev_params[configNr].used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500907EXPORT_SYMBOL_GPL(rndis_deregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
David Brownell15b2d2b2008-06-19 18:19:16 -0700909int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
David Brownell33376c12008-08-18 17:45:07 -0700911 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700912 if (!dev)
913 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700915
Mihai Donțua1df4e42010-09-08 02:54:02 +0300916 rndis_per_dev_params[configNr].dev = dev;
917 rndis_per_dev_params[configNr].filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
920}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500921EXPORT_SYMBOL_GPL(rndis_set_param_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Mihai Donțua1df4e42010-09-08 02:54:02 +0300923int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
David Brownell33376c12008-08-18 17:45:07 -0700925 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (!vendorDescr) return -1;
927 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700928
Mihai Donțua1df4e42010-09-08 02:54:02 +0300929 rndis_per_dev_params[configNr].vendorID = vendorID;
930 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500934EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Mihai Donțua1df4e42010-09-08 02:54:02 +0300936int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
David Brownell33376c12008-08-18 17:45:07 -0700938 pr_debug("%s: %u %u\n", __func__, medium, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700940
Mihai Donțua1df4e42010-09-08 02:54:02 +0300941 rndis_per_dev_params[configNr].medium = medium;
942 rndis_per_dev_params[configNr].speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500946EXPORT_SYMBOL_GPL(rndis_set_param_medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Mihai Donțua1df4e42010-09-08 02:54:02 +0300948void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300950 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 if (!skb)
953 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300954 header = (void *)skb_push(skb, sizeof(*header));
955 memset(header, 0, sizeof *header);
Linus Walleij51491162012-05-11 22:17:07 +0000956 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300958 header->DataOffset = cpu_to_le32(36);
959 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500961EXPORT_SYMBOL_GPL(rndis_add_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Mihai Donțua1df4e42010-09-08 02:54:02 +0300963void rndis_free_response(int configNr, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300965 rndis_resp_t *r;
966 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700967
Mihai Donțua1df4e42010-09-08 02:54:02 +0300968 list_for_each_safe(act, tmp,
969 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300971 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (r && r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300973 list_del(&r->list);
974 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 }
977}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500978EXPORT_SYMBOL_GPL(rndis_free_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Mihai Donțua1df4e42010-09-08 02:54:02 +0300980u8 *rndis_get_next_response(int configNr, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300982 rndis_resp_t *r;
983 struct list_head *act, *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -0700986
Mihai Donțua1df4e42010-09-08 02:54:02 +0300987 list_for_each_safe(act, tmp,
988 &(rndis_per_dev_params[configNr].resp_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300990 r = list_entry(act, rndis_resp_t, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (!r->send) {
992 r->send = 1;
993 *length = r->length;
994 return r->buf;
995 }
996 }
David Brownell7e27f182006-06-13 09:54:40 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return NULL;
999}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001000EXPORT_SYMBOL_GPL(rndis_get_next_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Mihai Donțua1df4e42010-09-08 02:54:02 +03001002static rndis_resp_t *rndis_add_response(int configNr, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001004 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001005
Mihai Donțua1df4e42010-09-08 02:54:02 +03001006 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1007 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001009
Mihai Donțua1df4e42010-09-08 02:54:02 +03001010 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 r->length = length;
1012 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001013
Mihai Donțua1df4e42010-09-08 02:54:02 +03001014 list_add_tail(&r->list,
1015 &(rndis_per_dev_params[configNr].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return r;
1017}
1018
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001019int rndis_rm_hdr(struct gether *port,
1020 struct sk_buff *skb,
1021 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
David Brownell6cdee102005-04-18 17:39:34 -07001023 /* tmp points to a struct rndis_packet_msg_type */
Mihai Donțua1df4e42010-09-08 02:54:02 +03001024 __le32 *tmp = (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
David Brownell6cdee102005-04-18 17:39:34 -07001026 /* MessageType, MessageLength */
Linus Walleij51491162012-05-11 22:17:07 +00001027 if (cpu_to_le32(RNDIS_MSG_PACKET)
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001028 != get_unaligned(tmp++)) {
1029 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001030 return -EINVAL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001031 }
David Brownell6cdee102005-04-18 17:39:34 -07001032 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David Brownell6cdee102005-04-18 17:39:34 -07001034 /* DataOffset, DataLength */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001035 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1036 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001037 return -EOVERFLOW;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001038 }
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001039 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001040
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001041 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 0;
1043}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001044EXPORT_SYMBOL_GPL(rndis_rm_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Mihai Donțua1df4e42010-09-08 02:54:02 +03001046#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001048static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001050 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001051
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001052 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 "Config Nr. %d\n"
1054 "used : %s\n"
1055 "state : %s\n"
1056 "medium : 0x%08X\n"
1057 "speed : %d\n"
1058 "cable : %s\n"
1059 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001060 "vendor : %s\n",
1061 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 ({ char *s = "?";
1063 switch (param->state) {
1064 case RNDIS_UNINITIALIZED:
1065 s = "RNDIS_UNINITIALIZED"; break;
1066 case RNDIS_INITIALIZED:
1067 s = "RNDIS_INITIALIZED"; break;
1068 case RNDIS_DATA_INITIALIZED:
1069 s = "RNDIS_DATA_INITIALIZED"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001070 } s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001071 param->medium,
1072 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001074 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001078static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001079 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Al Virod9dda782013-03-31 18:16:14 -04001081 rndis_params *p = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 u32 speed = 0;
1083 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 for (i = 0; i < count; i++) {
1086 char c;
1087 if (get_user(c, buffer))
1088 return -EFAULT;
1089 switch (c) {
1090 case '0':
1091 case '1':
1092 case '2':
1093 case '3':
1094 case '4':
1095 case '5':
1096 case '6':
1097 case '7':
1098 case '8':
1099 case '9':
1100 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001101 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
1103 case 'C':
1104 case 'c':
Mihai Donțua1df4e42010-09-08 02:54:02 +03001105 rndis_signal_connect(p->confignr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 break;
1107 case 'D':
1108 case 'd':
1109 rndis_signal_disconnect(p->confignr);
1110 break;
David Brownell7e27f182006-06-13 09:54:40 -07001111 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001113 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115 }
David Brownell7e27f182006-06-13 09:54:40 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 buffer++;
1118 }
David Brownell7e27f182006-06-13 09:54:40 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return count;
1121}
1122
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001123static int rndis_proc_open(struct inode *inode, struct file *file)
1124{
Al Virod9dda782013-03-31 18:16:14 -04001125 return single_open(file, rndis_proc_show, PDE_DATA(inode));
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001126}
1127
1128static const struct file_operations rndis_proc_fops = {
1129 .owner = THIS_MODULE,
1130 .open = rndis_proc_open,
1131 .read = seq_read,
1132 .llseek = seq_lseek,
1133 .release = single_release,
1134 .write = rndis_proc_write,
1135};
1136
Mihai Donțua1df4e42010-09-08 02:54:02 +03001137#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1140
Mihai Donțua1df4e42010-09-08 02:54:02 +03001141#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143
Andrzej Pietrasiewicz9c2b85f2013-12-03 15:15:29 +01001144int rndis_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 u8 i;
1147
1148 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1149#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1150 char name [20];
1151
Mihai Donțua1df4e42010-09-08 02:54:02 +03001152 sprintf(name, NAME_TEMPLATE, i);
1153 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001154 &rndis_proc_fops,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001155 (void *)(rndis_per_dev_params + i));
1156 if (!rndis_connect_state[i]) {
1157 pr_debug("%s: remove entries", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 while (i) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001159 sprintf(name, NAME_TEMPLATE, --i);
1160 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
David Brownell33376c12008-08-18 17:45:07 -07001162 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return -EIO;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165#endif
Mihai Donțua1df4e42010-09-08 02:54:02 +03001166 rndis_per_dev_params[i].confignr = i;
1167 rndis_per_dev_params[i].used = 0;
1168 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1169 rndis_per_dev_params[i].media_state
Linus Walleij17c51b62012-05-11 22:16:39 +00001170 = RNDIS_MEDIA_STATE_DISCONNECTED;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001171 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
David Brownell7e27f182006-06-13 09:54:40 -07001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 0;
1175}
1176
Andrzej Pietrasiewicz9c2b85f2013-12-03 15:15:29 +01001177void rndis_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001179#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 u8 i;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001181 char name[20];
David Brownell7e27f182006-06-13 09:54:40 -07001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001184 sprintf(name, NAME_TEMPLATE, i);
1185 remove_proc_entry(name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187#endif
1188}
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +02001189