blob: 31b3d5501bd3a36e25381b78efa5e0ab6339b854 [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>
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010028#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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
Fabio Estevamae8dd0c2014-04-04 22:27:59 -030039#include "u_rndis.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Brownell15b2d2b2008-06-19 18:19:16 -070041#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "rndis.h"
44
45
46/* The driver for your USB chip needs to support ep0 OUT to work with
47 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
48 *
49 * Windows hosts need an INF file like Documentation/usb/linux.inf
50 * and will be happier if you provide the host_addr module parameter.
51 */
52
53#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070055module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010061#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010063#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010065#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
66
67static DEFINE_IDA(rndis_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030070static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* Function Prototypes */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +010073static rndis_resp_t *rndis_add_response(struct rndis_params *params,
74 u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010076#ifdef CONFIG_USB_GADGET_DEBUG_FILES
77
78static const struct file_operations rndis_proc_fops;
79
80#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
David Brownell340600a2005-04-28 13:45:25 -070082/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030083static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070084{
85 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000086 RNDIS_OID_GEN_SUPPORTED_LIST,
87 RNDIS_OID_GEN_HARDWARE_STATUS,
88 RNDIS_OID_GEN_MEDIA_SUPPORTED,
89 RNDIS_OID_GEN_MEDIA_IN_USE,
90 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
91 RNDIS_OID_GEN_LINK_SPEED,
92 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
93 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
94 RNDIS_OID_GEN_VENDOR_ID,
95 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
96 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
97 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
98 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
99 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
100 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -0700101
David Brownell340600a2005-04-28 13:45:25 -0700102 /* the statistical stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000103 RNDIS_OID_GEN_XMIT_OK,
104 RNDIS_OID_GEN_RCV_OK,
105 RNDIS_OID_GEN_XMIT_ERROR,
106 RNDIS_OID_GEN_RCV_ERROR,
107 RNDIS_OID_GEN_RCV_NO_BUFFER,
David Brownell340600a2005-04-28 13:45:25 -0700108#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij8cdddc32012-05-11 22:16:08 +0000109 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
110 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
111 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
112 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
113 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
114 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
115 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
116 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
117 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
118 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
119 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
120 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
121 RNDIS_OID_GEN_RCV_CRC_ERROR,
122 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
David Brownell340600a2005-04-28 13:45:25 -0700123#endif /* RNDIS_OPTIONAL_STATS */
124
David Brownell7e27f182006-06-13 09:54:40 -0700125 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700126 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000127 RNDIS_OID_802_3_PERMANENT_ADDRESS,
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000128 RNDIS_OID_802_3_CURRENT_ADDRESS,
129 RNDIS_OID_802_3_MULTICAST_LIST,
130 RNDIS_OID_802_3_MAC_OPTIONS,
131 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700132
David Brownell340600a2005-04-28 13:45:25 -0700133 /* the statistical stuff */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000134 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
135 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
136 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700137#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000138 RNDIS_OID_802_3_XMIT_DEFERRED,
139 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
140 RNDIS_OID_802_3_RCV_OVERRUN,
141 RNDIS_OID_802_3_XMIT_UNDERRUN,
142 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
143 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
144 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700145#endif /* RNDIS_OPTIONAL_STATS */
146
147#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700148 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
149 * don't say what they mean ... and the NDIS specs are often
150 * confusing and/or ambiguous in this context. (That is, more
151 * so than their specs for the other OIDs.)
152 *
153 * FIXME someone who knows what these should do, please
154 * implement them!
155 */
David Brownell340600a2005-04-28 13:45:25 -0700156
157 /* power management */
158 OID_PNP_CAPABILITIES,
159 OID_PNP_QUERY_POWER,
160 OID_PNP_SET_POWER,
161
162#ifdef RNDIS_WAKEUP
163 /* wake up host */
164 OID_PNP_ENABLE_WAKE_UP,
165 OID_PNP_ADD_WAKE_UP_PATTERN,
166 OID_PNP_REMOVE_WAKE_UP_PATTERN,
167#endif /* RNDIS_WAKEUP */
168#endif /* RNDIS_PM */
169};
170
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* NDIS Functions */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100173static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300174 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300176 int retval = -ENOTSUPP;
177 u32 length = 4; /* usually */
178 __le32 *outbuf;
179 int i, count;
180 rndis_query_cmplt_type *resp;
181 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700182 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700183 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300186 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700189
190 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700191 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700192 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700193 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700194 get_unaligned_le32(&buf[i]),
195 get_unaligned_le32(&buf[i + 4]),
196 get_unaligned_le32(&buf[i + 8]),
197 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700198 }
199 }
200
201 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300202 outbuf = (__le32 *)&resp[1];
203 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700204
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100205 net = params->dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700206 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 switch (OID) {
209
210 /* general oids (table 4-1) */
211
212 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000213 case RNDIS_OID_GEN_SUPPORTED_LIST:
214 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300215 length = sizeof(oid_supported_list);
216 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300218 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 retval = 0;
220 break;
David Brownell7e27f182006-06-13 09:54:40 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000223 case RNDIS_OID_GEN_HARDWARE_STATUS:
224 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700225 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700227 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * reddite ergo quae sunt Caesaris Caesari
229 * et quae sunt Dei Deo!
230 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300231 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 retval = 0;
233 break;
David Brownell7e27f182006-06-13 09:54:40 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000236 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
237 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100238 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 retval = 0;
240 break;
David Brownell7e27f182006-06-13 09:54:40 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000243 case RNDIS_OID_GEN_MEDIA_IN_USE:
244 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* one medium, one transport... (maybe you do it better) */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100246 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 retval = 0;
248 break;
David Brownell7e27f182006-06-13 09:54:40 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000251 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
252 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100253 if (params->dev) {
254 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 break;
David Brownell7e27f182006-06-13 09:54:40 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000260 case RNDIS_OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700261 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000262 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100263 if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300264 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 else
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100266 *outbuf = cpu_to_le32(params->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 retval = 0;
268 break;
269
270 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000271 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
272 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100273 if (params->dev) {
274 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 retval = 0;
276 }
277 break;
David Brownell7e27f182006-06-13 09:54:40 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000280 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
281 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100282 if (params->dev) {
283 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 retval = 0;
285 }
286 break;
David Brownell7e27f182006-06-13 09:54:40 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000289 case RNDIS_OID_GEN_VENDOR_ID:
290 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100291 *outbuf = cpu_to_le32(params->vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 retval = 0;
293 break;
David Brownell7e27f182006-06-13 09:54:40 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000296 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
297 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100298 if (params->vendorDescr) {
299 length = strlen(params->vendorDescr);
300 memcpy(outbuf, params->vendorDescr, length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400301 } else {
302 outbuf[0] = 0;
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 retval = 0;
305 break;
306
Linus Walleij8cdddc32012-05-11 22:16:08 +0000307 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
308 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700310 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 retval = 0;
312 break;
313
314 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000315 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
316 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100317 *outbuf = cpu_to_le32(*params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 retval = 0;
319 break;
320
321 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000322 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
323 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800324 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 retval = 0;
326 break;
327
328 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000329 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700330 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000331 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100332 *outbuf = cpu_to_le32(params->media_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 retval = 0;
334 break;
335
Linus Walleij8cdddc32012-05-11 22:16:08 +0000336 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
337 pr_debug("%s: RNDIS_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 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000346 case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
347 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800348 *outbuf = cpu_to_le32(
Linus Walleije20289e2012-05-11 22:17:19 +0000349 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
350 | RNDIS_MAC_OPTION_FULL_DUPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 retval = 0;
352 break;
353
354 /* statistics OIDs (table 4-2) */
355
356 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000357 case RNDIS_OID_GEN_XMIT_OK:
David Brownell340600a2005-04-28 13:45:25 -0700358 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000359 pr_debug("%s: RNDIS_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 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000368 case RNDIS_OID_GEN_RCV_OK:
David Brownell340600a2005-04-28 13:45:25 -0700369 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000370 pr_debug("%s: RNDIS_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 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000379 case RNDIS_OID_GEN_XMIT_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700380 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000381 pr_debug("%s: RNDIS_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 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000389 case RNDIS_OID_GEN_RCV_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700390 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000391 pr_debug("%s: RNDIS_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 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000399 case RNDIS_OID_GEN_RCV_NO_BUFFER:
400 pr_debug("%s: RNDIS_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 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000410 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000411 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100412 if (params->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 length = ETH_ALEN;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100414 memcpy(outbuf, params->host_mac, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 break;
David Brownell7e27f182006-06-13 09:54:40 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000420 case RNDIS_OID_802_3_CURRENT_ADDRESS:
421 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100422 if (params->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 length = ETH_ALEN;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100424 memcpy(outbuf, params->host_mac, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 retval = 0;
426 }
427 break;
David Brownell7e27f182006-06-13 09:54:40 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000430 case RNDIS_OID_802_3_MULTICAST_LIST:
431 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300433 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 retval = 0;
435 break;
David Brownell7e27f182006-06-13 09:54:40 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000438 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
439 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300441 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 retval = 0;
443 break;
David Brownell7e27f182006-06-13 09:54:40 -0700444
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000445 case RNDIS_OID_802_3_MAC_OPTIONS:
446 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700447 *outbuf = cpu_to_le32(0);
448 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450
451 /* ieee802.3 statistics OIDs (table 4-4) */
452
453 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000454 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
455 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700456 if (stats) {
457 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 retval = 0;
459 }
460 break;
David Brownell7e27f182006-06-13 09:54:40 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000463 case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
464 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300465 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 retval = 0;
467 break;
David Brownell7e27f182006-06-13 09:54:40 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000470 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
471 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300472 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 retval = 0;
474 break;
David Brownell7e27f182006-06-13 09:54:40 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 default:
David Brownell00274922007-11-19 12:58:36 -0800477 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800478 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
David Brownell340600a2005-04-28 13:45:25 -0700480 if (retval < 0)
481 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700482
Mihai Donțua1df4e42010-09-08 02:54:02 +0300483 resp->InformationBufferLength = cpu_to_le32(length);
484 r->length = length + sizeof(*resp);
485 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return retval;
487}
488
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100489static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
490 u8 *buf, u32 buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300492 rndis_set_cmplt_type *resp;
493 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (!r)
496 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300497 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!resp)
499 return -ENOMEM;
500
David Brownell340600a2005-04-28 13:45:25 -0700501 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700502 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700503 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700504 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700505 get_unaligned_le32(&buf[i]),
506 get_unaligned_le32(&buf[i + 4]),
507 get_unaligned_le32(&buf[i + 8]),
508 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 switch (OID) {
Linus Walleij8cdddc32012-05-11 22:16:08 +0000513 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
David Brownell340600a2005-04-28 13:45:25 -0700515 /* these NDIS_PACKET_TYPE_* bitflags are shared with
516 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
518 * PROMISCUOUS, DIRECTED,
519 * MULTICAST, ALL_MULTICAST, BROADCAST
520 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700521 *params->filter = (u16)get_unaligned_le32(buf);
Linus Walleij8cdddc32012-05-11 22:16:08 +0000522 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800523 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* this call has a significant side effect: it's
526 * what makes the packet flow start and stop, like
527 * activating the CDC Ethernet altsetting.
528 */
David Brownell340600a2005-04-28 13:45:25 -0700529 retval = 0;
530 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 params->state = RNDIS_DATA_INITIALIZED;
532 netif_carrier_on(params->dev);
533 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300534 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 } else {
536 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300537 netif_carrier_off(params->dev);
538 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 break;
David Brownell7e27f182006-06-13 09:54:40 -0700541
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000542 case RNDIS_OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700543 /* I think we can ignore this */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000544 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 retval = 0;
546 break;
David Brownell340600a2005-04-28 13:45:25 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 default:
David Brownell00274922007-11-19 12:58:36 -0800549 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800550 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
David Brownell7e27f182006-06-13 09:54:40 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return retval;
554}
555
David Brownell7e27f182006-06-13 09:54:40 -0700556/*
557 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
559
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100560static int rndis_init_response(struct rndis_params *params,
561 rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300563 rndis_init_cmplt_type *resp;
564 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700565
David Brownell15b2d2b2008-06-19 18:19:16 -0700566 if (!params->dev)
567 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700568
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100569 r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700570 if (!r)
571 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300572 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700573
Linus Walleij51491162012-05-11 22:17:07 +0000574 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300575 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300577 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
578 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
579 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
580 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
581 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
582 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
583 resp->MaxTransferSize = cpu_to_le32(
David Brownell15b2d2b2008-06-19 18:19:16 -0700584 params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300585 + sizeof(struct ethhdr)
586 + sizeof(struct rndis_packet_msg_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 + 22);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300588 resp->PacketAlignmentFactor = cpu_to_le32(0);
589 resp->AFListOffset = cpu_to_le32(0);
590 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700591
David Brownell15b2d2b2008-06-19 18:19:16 -0700592 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return 0;
594}
595
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100596static int rndis_query_response(struct rndis_params *params,
597 rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300600 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700601
David Brownell33376c12008-08-18 17:45:07 -0700602 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700603 if (!params->dev)
604 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700605
Shaun Tancheff87637162006-02-22 19:47:19 -0800606 /*
607 * we need more memory:
608 * gen_ndis_query_resp expects enough space for
609 * rndis_query_cmplt_type followed by data.
610 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100612 r = rndis_add_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300613 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700614 if (!r)
615 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300616 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700617
Linus Walleij51491162012-05-11 22:17:07 +0000618 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700620
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100621 if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700622 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300623 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700624 le32_to_cpu(buf->InformationBufferLength),
625 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300627 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
628 resp->MessageLength = cpu_to_le32(sizeof *resp);
629 resp->InformationBufferLength = cpu_to_le32(0);
630 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300632 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700633
David Brownell15b2d2b2008-06-19 18:19:16 -0700634 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636}
637
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100638static int rndis_set_response(struct rndis_params *params,
639 rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300641 u32 BufLength, BufOffset;
642 rndis_set_cmplt_type *resp;
643 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700644
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100645 r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700646 if (!r)
647 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300648 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Mihai Donțua1df4e42010-09-08 02:54:02 +0300650 BufLength = le32_to_cpu(buf->InformationBufferLength);
651 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
David Brownell15b2d2b2008-06-19 18:19:16 -0700653#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700654 pr_debug("%s: Length: %d\n", __func__, BufLength);
655 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
656 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700659 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
David Brownell7e27f182006-06-13 09:54:40 -0700661
David Brownell33376c12008-08-18 17:45:07 -0700662 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#endif
David Brownell7e27f182006-06-13 09:54:40 -0700664
Linus Walleij51491162012-05-11 22:17:07 +0000665 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300666 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100668 if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
Mihai Donțua1df4e42010-09-08 02:54:02 +0300669 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
670 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700671 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300672 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700673
David Brownell15b2d2b2008-06-19 18:19:16 -0700674 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return 0;
676}
677
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100678static int rndis_reset_response(struct rndis_params *params,
679 rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300681 rndis_reset_cmplt_type *resp;
682 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700683
xerox_lin90de7402014-08-18 21:54:23 +0800684 u32 length;
685 u8 *xbuf;
686
687 /* drain the response queue */
688 while ((xbuf = rndis_get_next_response(configNr, &length)))
689 rndis_free_response(configNr, xbuf);
690
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100691 r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700692 if (!r)
693 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300694 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700695
Linus Walleij51491162012-05-11 22:17:07 +0000696 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300697 resp->MessageLength = cpu_to_le32(16);
698 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300700 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700701
David Brownell15b2d2b2008-06-19 18:19:16 -0700702 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704}
705
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100706static int rndis_keepalive_response(struct rndis_params *params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300707 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300709 rndis_keepalive_cmplt_type *resp;
710 rndis_resp_t *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
713
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100714 r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700715 if (!r)
716 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300717 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700718
Linus Walleij51491162012-05-11 22:17:07 +0000719 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300720 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300722 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700723
David Brownell15b2d2b2008-06-19 18:19:16 -0700724 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
728
David Brownell7e27f182006-06-13 09:54:40 -0700729/*
730 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100732static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300734 rndis_indicate_status_msg_type *resp;
735 rndis_resp_t *r;
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
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100740 r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700741 if (!r)
742 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300743 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700744
Linus Walleij51491162012-05-11 22:17:07 +0000745 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300746 resp->MessageLength = cpu_to_le32(20);
747 resp->Status = cpu_to_le32(status);
748 resp->StatusBufferLength = cpu_to_le32(0);
749 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700750
David Brownell15b2d2b2008-06-19 18:19:16 -0700751 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753}
754
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100755int rndis_signal_connect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100757 params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
758 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500760EXPORT_SYMBOL_GPL(rndis_signal_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100762int rndis_signal_disconnect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100764 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
765 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500767EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100769void rndis_uninit(struct rndis_params *params)
David Brownell340600a2005-04-28 13:45:25 -0700770{
David Brownell486e2df2005-05-24 17:51:52 -0700771 u8 *buf;
772 u32 length;
773
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100774 if (!params)
David Brownell340600a2005-04-28 13:45:25 -0700775 return;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100776 params->state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700777
778 /* drain the response queue */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100779 while ((buf = rndis_get_next_response(params, &length)))
780 rndis_free_response(params, buf);
David Brownell340600a2005-04-28 13:45:25 -0700781}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500782EXPORT_SYMBOL_GPL(rndis_uninit);
David Brownell340600a2005-04-28 13:45:25 -0700783
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100784void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100786 params->host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500788EXPORT_SYMBOL_GPL(rndis_set_host_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
David Brownell7e27f182006-06-13 09:54:40 -0700790/*
791 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100793int rndis_msg_parser(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 u32 MsgType, MsgLength;
796 __le32 *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!buf)
799 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700800
Mihai Donțua1df4e42010-09-08 02:54:02 +0300801 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700802 MsgType = get_unaligned_le32(tmp++);
803 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700804
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100805 if (!params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700807
David Brownell340600a2005-04-28 13:45:25 -0700808 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
809 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
810 * and normal HC level polling to see if there's any IN traffic.
811 */
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700814 switch (MsgType) {
Linus Walleij51491162012-05-11 22:17:07 +0000815 case RNDIS_MSG_INIT:
816 pr_debug("%s: RNDIS_MSG_INIT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300817 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 params->state = RNDIS_INITIALIZED;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100819 return rndis_init_response(params, (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700820
Linus Walleij51491162012-05-11 22:17:07 +0000821 case RNDIS_MSG_HALT:
822 pr_debug("%s: RNDIS_MSG_HALT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300823 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 params->state = RNDIS_UNINITIALIZED;
825 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300826 netif_carrier_off(params->dev);
827 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700830
Linus Walleij51491162012-05-11 22:17:07 +0000831 case RNDIS_MSG_QUERY:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100832 return rndis_query_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300833 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700834
Linus Walleij51491162012-05-11 22:17:07 +0000835 case RNDIS_MSG_SET:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100836 return rndis_set_response(params, (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700837
Linus Walleij51491162012-05-11 22:17:07 +0000838 case RNDIS_MSG_RESET:
839 pr_debug("%s: RNDIS_MSG_RESET\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300840 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100841 return rndis_reset_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300842 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Walleij51491162012-05-11 22:17:07 +0000844 case RNDIS_MSG_KEEPALIVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700846 if (rndis_debug > 1)
Linus Walleij51491162012-05-11 22:17:07 +0000847 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300848 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100849 return rndis_keepalive_response(params,
David Brownell7e27f182006-06-13 09:54:40 -0700850 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700852
853 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* At least Windows XP emits some undefined RNDIS messages.
855 * In one case those messages seemed to relate to the host
856 * suspending itself.
857 */
David Brownell00274922007-11-19 12:58:36 -0800858 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300859 __func__, MsgType, MsgLength);
Andy Shevchenkod3091cf2012-08-07 19:07:58 +0300860 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
861 buf, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 }
David Brownell7e27f182006-06-13 09:54:40 -0700864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return -ENOTSUPP;
866}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500867EXPORT_SYMBOL_GPL(rndis_msg_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100869static inline int rndis_get_nr(void)
870{
871 return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL);
872}
873
874static inline void rndis_put_nr(int nr)
875{
876 ida_simple_remove(&rndis_ida, nr);
877}
878
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100879struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100881 struct rndis_params *params;
Andrzej Pietrasiewicz81dff862015-05-18 17:40:04 +0200882 int i;
David Brownell7e27f182006-06-13 09:54:40 -0700883
David Brownell15b2d2b2008-06-19 18:19:16 -0700884 if (!resp_avail)
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100885 return ERR_PTR(-EINVAL);
David Brownell15b2d2b2008-06-19 18:19:16 -0700886
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100887 i = rndis_get_nr();
888 if (i < 0) {
889 pr_debug("failed\n");
890
891 return ERR_PTR(-ENODEV);
892 }
893
894 params = kzalloc(sizeof(*params), GFP_KERNEL);
895 if (!params) {
896 rndis_put_nr(i);
897
898 return ERR_PTR(-ENOMEM);
899 }
900
901#ifdef CONFIG_USB_GADGET_DEBUG_FILES
902 {
903 struct proc_dir_entry *proc_entry;
904 char name[20];
905
906 sprintf(name, NAME_TEMPLATE, i);
907 proc_entry = proc_create_data(name, 0660, NULL,
908 &rndis_proc_fops, params);
909 if (!proc_entry) {
910 kfree(params);
911 rndis_put_nr(i);
912
913 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 }
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100916#endif
David Brownell7e27f182006-06-13 09:54:40 -0700917
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100918 params->confignr = i;
919 params->used = 1;
920 params->state = RNDIS_UNINITIALIZED;
921 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
922 params->resp_avail = resp_avail;
923 params->v = v;
Geliang Tangf6281af2015-12-19 00:34:33 +0800924 INIT_LIST_HEAD(&params->resp_queue);
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100925 pr_debug("%s: configNr = %d\n", __func__, i);
926
927 return params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500929EXPORT_SYMBOL_GPL(rndis_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100931void rndis_deregister(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Andrzej Pietrasiewicz81dff862015-05-18 17:40:04 +0200933 int i;
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100934
Mihai Donțua1df4e42010-09-08 02:54:02 +0300935 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700936
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100937 if (!params)
938 return;
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100939
940 i = params->confignr;
941
942#ifdef CONFIG_USB_GADGET_DEBUG_FILES
943 {
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100944 char name[20];
945
946 sprintf(name, NAME_TEMPLATE, i);
947 remove_proc_entry(name, NULL);
948 }
949#endif
950
951 kfree(params);
952 rndis_put_nr(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500954EXPORT_SYMBOL_GPL(rndis_deregister);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100955int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
956 u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
David Brownell33376c12008-08-18 17:45:07 -0700958 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700959 if (!dev)
960 return -EINVAL;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100961 if (!params)
962 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700963
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100964 params->dev = dev;
965 params->filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500969EXPORT_SYMBOL_GPL(rndis_set_param_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100971int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
972 const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
David Brownell33376c12008-08-18 17:45:07 -0700974 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!vendorDescr) return -1;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100976 if (!params)
977 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700978
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100979 params->vendorID = vendorID;
980 params->vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return 0;
983}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500984EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100986int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
David Brownell33376c12008-08-18 17:45:07 -0700988 pr_debug("%s: %u %u\n", __func__, medium, speed);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100989 if (!params)
990 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700991
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100992 params->medium = medium;
993 params->speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 0;
996}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500997EXPORT_SYMBOL_GPL(rndis_set_param_medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Mihai Donțua1df4e42010-09-08 02:54:02 +0300999void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001001 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (!skb)
1004 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001005 header = (void *)skb_push(skb, sizeof(*header));
1006 memset(header, 0, sizeof *header);
Linus Walleij51491162012-05-11 22:17:07 +00001007 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +03001009 header->DataOffset = cpu_to_le32(36);
1010 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001012EXPORT_SYMBOL_GPL(rndis_add_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001014void rndis_free_response(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Geliang Tangf6281af2015-12-19 00:34:33 +08001016 rndis_resp_t *r, *n;
David Brownell7e27f182006-06-13 09:54:40 -07001017
Geliang Tangf6281af2015-12-19 00:34:33 +08001018 list_for_each_entry_safe(r, n, &params->resp_queue, list) {
Julia Lawall1c17a352016-01-25 16:21:54 +01001019 if (r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001020 list_del(&r->list);
1021 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 }
1024}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001025EXPORT_SYMBOL_GPL(rndis_free_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001027u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Geliang Tangf6281af2015-12-19 00:34:33 +08001029 rndis_resp_t *r, *n;
David Brownell7e27f182006-06-13 09:54:40 -07001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001032
Geliang Tangf6281af2015-12-19 00:34:33 +08001033 list_for_each_entry_safe(r, n, &params->resp_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!r->send) {
1035 r->send = 1;
1036 *length = r->length;
1037 return r->buf;
1038 }
1039 }
David Brownell7e27f182006-06-13 09:54:40 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return NULL;
1042}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001043EXPORT_SYMBOL_GPL(rndis_get_next_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001045static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001047 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001048
Mihai Donțua1df4e42010-09-08 02:54:02 +03001049 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1050 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001052
Mihai Donțua1df4e42010-09-08 02:54:02 +03001053 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 r->length = length;
1055 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001056
Geliang Tangf6281af2015-12-19 00:34:33 +08001057 list_add_tail(&r->list, &params->resp_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return r;
1059}
1060
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001061int rndis_rm_hdr(struct gether *port,
1062 struct sk_buff *skb,
1063 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
David Brownell6cdee102005-04-18 17:39:34 -07001065 /* tmp points to a struct rndis_packet_msg_type */
Mihai Donțua1df4e42010-09-08 02:54:02 +03001066 __le32 *tmp = (void *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
David Brownell6cdee102005-04-18 17:39:34 -07001068 /* MessageType, MessageLength */
Linus Walleij51491162012-05-11 22:17:07 +00001069 if (cpu_to_le32(RNDIS_MSG_PACKET)
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001070 != get_unaligned(tmp++)) {
1071 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001072 return -EINVAL;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001073 }
David Brownell6cdee102005-04-18 17:39:34 -07001074 tmp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
David Brownell6cdee102005-04-18 17:39:34 -07001076 /* DataOffset, DataLength */
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001077 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1078 dev_kfree_skb_any(skb);
David Brownell6cdee102005-04-18 17:39:34 -07001079 return -EOVERFLOW;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001080 }
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001081 skb_trim(skb, get_unaligned_le32(tmp++));
David Brownell6cdee102005-04-18 17:39:34 -07001082
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001083 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 0;
1085}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001086EXPORT_SYMBOL_GPL(rndis_rm_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Mihai Donțua1df4e42010-09-08 02:54:02 +03001088#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001090static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001092 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001093
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001094 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 "Config Nr. %d\n"
1096 "used : %s\n"
1097 "state : %s\n"
1098 "medium : 0x%08X\n"
1099 "speed : %d\n"
1100 "cable : %s\n"
1101 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001102 "vendor : %s\n",
1103 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 ({ char *s = "?";
1105 switch (param->state) {
1106 case RNDIS_UNINITIALIZED:
1107 s = "RNDIS_UNINITIALIZED"; break;
1108 case RNDIS_INITIALIZED:
1109 s = "RNDIS_INITIALIZED"; break;
1110 case RNDIS_DATA_INITIALIZED:
1111 s = "RNDIS_DATA_INITIALIZED"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001112 } s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001113 param->medium,
1114 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001116 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001120static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001121 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Al Virod9dda782013-03-31 18:16:14 -04001123 rndis_params *p = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 u32 speed = 0;
1125 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 for (i = 0; i < count; i++) {
1128 char c;
1129 if (get_user(c, buffer))
1130 return -EFAULT;
1131 switch (c) {
1132 case '0':
1133 case '1':
1134 case '2':
1135 case '3':
1136 case '4':
1137 case '5':
1138 case '6':
1139 case '7':
1140 case '8':
1141 case '9':
1142 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001143 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
1145 case 'C':
1146 case 'c':
Andrzej Pietrasiewicz868055f2015-05-18 17:40:02 +02001147 rndis_signal_connect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 case 'D':
1150 case 'd':
Andrzej Pietrasiewicz868055f2015-05-18 17:40:02 +02001151 rndis_signal_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
David Brownell7e27f182006-06-13 09:54:40 -07001153 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001155 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
1157 }
David Brownell7e27f182006-06-13 09:54:40 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 buffer++;
1160 }
David Brownell7e27f182006-06-13 09:54:40 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return count;
1163}
1164
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001165static int rndis_proc_open(struct inode *inode, struct file *file)
1166{
Al Virod9dda782013-03-31 18:16:14 -04001167 return single_open(file, rndis_proc_show, PDE_DATA(inode));
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001168}
1169
1170static const struct file_operations rndis_proc_fops = {
1171 .owner = THIS_MODULE,
1172 .open = rndis_proc_open,
1173 .read = seq_read,
1174 .llseek = seq_lseek,
1175 .release = single_release,
1176 .write = rndis_proc_write,
1177};
1178
Mihai Donțua1df4e42010-09-08 02:54:02 +03001179#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Mihai Donțua1df4e42010-09-08 02:54:02 +03001181#endif /* CONFIG_USB_GADGET_DEBUG_FILES */