blob: cf5fa90f4ea7508f266834b646e905518d87394a [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
xerox_lin72ffa182014-08-14 14:48:44 +080045int rndis_ul_max_pkt_per_xfer_rcvd;
46module_param(rndis_ul_max_pkt_per_xfer_rcvd, int, S_IRUGO);
47MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer_rcvd,
48 "Max num of REMOTE_NDIS_PACKET_MSGs received in a single transfer");
49
50int rndis_ul_max_xfer_size_rcvd;
51module_param(rndis_ul_max_xfer_size_rcvd, int, S_IRUGO);
52MODULE_PARM_DESC(rndis_ul_max_xfer_size_rcvd,
53 "Max size of bus transfer received");
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* The driver for your USB chip needs to support ep0 OUT to work with
57 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
58 *
59 * Windows hosts need an INF file like Documentation/usb/linux.inf
60 * and will be happier if you provide the host_addr module parameter.
61 */
62
63#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int rndis_debug = 0;
David Brownell340600a2005-04-28 13:45:25 -070065module_param (rndis_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_PARM_DESC (rndis_debug, "enable debugging");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define rndis_debug 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#endif
70
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010071#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010073#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010075#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
76
77static DEFINE_IDA(rndis_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Driver Version */
Mihai Donțua1df4e42010-09-08 02:54:02 +030080static const __le32 rndis_driver_version = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* Function Prototypes */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +010083static rndis_resp_t *rndis_add_response(struct rndis_params *params,
84 u32 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +010086#ifdef CONFIG_USB_GADGET_DEBUG_FILES
87
88static const struct file_operations rndis_proc_fops;
89
90#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
David Brownell340600a2005-04-28 13:45:25 -070092/* supported OIDs */
Mihai Donțua1df4e42010-09-08 02:54:02 +030093static const u32 oid_supported_list[] =
David Brownell340600a2005-04-28 13:45:25 -070094{
95 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +000096 RNDIS_OID_GEN_SUPPORTED_LIST,
97 RNDIS_OID_GEN_HARDWARE_STATUS,
98 RNDIS_OID_GEN_MEDIA_SUPPORTED,
99 RNDIS_OID_GEN_MEDIA_IN_USE,
100 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
101 RNDIS_OID_GEN_LINK_SPEED,
102 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
103 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
104 RNDIS_OID_GEN_VENDOR_ID,
105 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
106 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
107 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
108 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
109 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
110 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
David Brownell7e27f182006-06-13 09:54:40 -0700111
David Brownell340600a2005-04-28 13:45:25 -0700112 /* the statistical stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000113 RNDIS_OID_GEN_XMIT_OK,
114 RNDIS_OID_GEN_RCV_OK,
115 RNDIS_OID_GEN_XMIT_ERROR,
116 RNDIS_OID_GEN_RCV_ERROR,
117 RNDIS_OID_GEN_RCV_NO_BUFFER,
David Brownell340600a2005-04-28 13:45:25 -0700118#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij8cdddc32012-05-11 22:16:08 +0000119 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
120 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
121 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
122 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
123 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
124 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
125 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
126 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
127 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
128 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
129 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
130 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
131 RNDIS_OID_GEN_RCV_CRC_ERROR,
132 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
David Brownell340600a2005-04-28 13:45:25 -0700133#endif /* RNDIS_OPTIONAL_STATS */
134
David Brownell7e27f182006-06-13 09:54:40 -0700135 /* mandatory 802.3 */
David Brownell340600a2005-04-28 13:45:25 -0700136 /* the general stuff */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000137 RNDIS_OID_802_3_PERMANENT_ADDRESS,
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000138 RNDIS_OID_802_3_CURRENT_ADDRESS,
139 RNDIS_OID_802_3_MULTICAST_LIST,
140 RNDIS_OID_802_3_MAC_OPTIONS,
141 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
David Brownell7e27f182006-06-13 09:54:40 -0700142
David Brownell340600a2005-04-28 13:45:25 -0700143 /* the statistical stuff */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000144 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
145 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
146 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700147#ifdef RNDIS_OPTIONAL_STATS
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000148 RNDIS_OID_802_3_XMIT_DEFERRED,
149 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
150 RNDIS_OID_802_3_RCV_OVERRUN,
151 RNDIS_OID_802_3_XMIT_UNDERRUN,
152 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
153 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
154 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
David Brownell340600a2005-04-28 13:45:25 -0700155#endif /* RNDIS_OPTIONAL_STATS */
156
157#ifdef RNDIS_PM
David Brownell15b2d2b2008-06-19 18:19:16 -0700158 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
159 * don't say what they mean ... and the NDIS specs are often
160 * confusing and/or ambiguous in this context. (That is, more
161 * so than their specs for the other OIDs.)
162 *
163 * FIXME someone who knows what these should do, please
164 * implement them!
165 */
David Brownell340600a2005-04-28 13:45:25 -0700166
167 /* power management */
168 OID_PNP_CAPABILITIES,
169 OID_PNP_QUERY_POWER,
170 OID_PNP_SET_POWER,
171
172#ifdef RNDIS_WAKEUP
173 /* wake up host */
174 OID_PNP_ENABLE_WAKE_UP,
175 OID_PNP_ADD_WAKE_UP_PATTERN,
176 OID_PNP_REMOVE_WAKE_UP_PATTERN,
177#endif /* RNDIS_WAKEUP */
178#endif /* RNDIS_PM */
179};
180
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* NDIS Functions */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100183static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300184 unsigned buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300186 int retval = -ENOTSUPP;
187 u32 length = 4; /* usually */
188 __le32 *outbuf;
189 int i, count;
190 rndis_query_cmplt_type *resp;
191 struct net_device *net;
Eric Dumazet28172732010-07-07 14:58:56 -0700192 struct rtnl_link_stats64 temp;
David S. Millerfdb93f8a2010-06-15 21:50:14 -0700193 const struct rtnl_link_stats64 *stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (!r) return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300196 resp = (rndis_query_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!resp) return -ENOMEM;
David Brownell340600a2005-04-28 13:45:25 -0700199
200 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700201 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700202 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700203 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700204 get_unaligned_le32(&buf[i]),
205 get_unaligned_le32(&buf[i + 4]),
206 get_unaligned_le32(&buf[i + 8]),
207 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700208 }
209 }
210
211 /* response goes here, right after the header */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300212 outbuf = (__le32 *)&resp[1];
213 resp->InformationBufferOffset = cpu_to_le32(16);
David Brownell340600a2005-04-28 13:45:25 -0700214
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100215 net = params->dev;
Eric Dumazet28172732010-07-07 14:58:56 -0700216 stats = dev_get_stats(net, &temp);
David Brownell15b2d2b2008-06-19 18:19:16 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 switch (OID) {
219
220 /* general oids (table 4-1) */
221
222 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000223 case RNDIS_OID_GEN_SUPPORTED_LIST:
224 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300225 length = sizeof(oid_supported_list);
226 count = length / sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 for (i = 0; i < count; i++)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300228 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 retval = 0;
230 break;
David Brownell7e27f182006-06-13 09:54:40 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000233 case RNDIS_OID_GEN_HARDWARE_STATUS:
234 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700235 /* Bogus question!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Hardware must be ready to receive high level protocols.
David Brownell7e27f182006-06-13 09:54:40 -0700237 * BTW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * reddite ergo quae sunt Caesaris Caesari
239 * et quae sunt Dei Deo!
240 */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300241 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 retval = 0;
243 break;
David Brownell7e27f182006-06-13 09:54:40 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000246 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
247 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100248 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 retval = 0;
250 break;
David Brownell7e27f182006-06-13 09:54:40 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000253 case RNDIS_OID_GEN_MEDIA_IN_USE:
254 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* one medium, one transport... (maybe you do it better) */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100256 *outbuf = cpu_to_le32(params->medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 retval = 0;
258 break;
David Brownell7e27f182006-06-13 09:54:40 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000261 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
262 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100263 if (params->dev) {
264 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 break;
David Brownell7e27f182006-06-13 09:54:40 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000270 case RNDIS_OID_GEN_LINK_SPEED:
David Brownell340600a2005-04-28 13:45:25 -0700271 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000272 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100273 if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300274 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 else
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100276 *outbuf = cpu_to_le32(params->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 retval = 0;
278 break;
279
280 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000281 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
282 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100283 if (params->dev) {
284 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 retval = 0;
286 }
287 break;
David Brownell7e27f182006-06-13 09:54:40 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000290 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
291 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100292 if (params->dev) {
293 *outbuf = cpu_to_le32(params->dev->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 retval = 0;
295 }
296 break;
David Brownell7e27f182006-06-13 09:54:40 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000299 case RNDIS_OID_GEN_VENDOR_ID:
300 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100301 *outbuf = cpu_to_le32(params->vendorID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 retval = 0;
303 break;
David Brownell7e27f182006-06-13 09:54:40 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000306 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
307 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100308 if (params->vendorDescr) {
309 length = strlen(params->vendorDescr);
310 memcpy(outbuf, params->vendorDescr, length);
Maxim Osipov037d3652010-08-21 14:54:06 +0400311 } else {
312 outbuf[0] = 0;
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 retval = 0;
315 break;
316
Linus Walleij8cdddc32012-05-11 22:16:08 +0000317 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
318 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Created as LE */
David Brownell340600a2005-04-28 13:45:25 -0700320 *outbuf = rndis_driver_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 retval = 0;
322 break;
323
324 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000325 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
326 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100327 *outbuf = cpu_to_le32(*params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 retval = 0;
329 break;
330
331 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000332 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
333 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800334 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 retval = 0;
336 break;
337
338 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000339 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
David Brownell340600a2005-04-28 13:45:25 -0700340 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000341 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100342 *outbuf = cpu_to_le32(params->media_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 retval = 0;
344 break;
345
Linus Walleij8cdddc32012-05-11 22:16:08 +0000346 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
347 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300348 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 retval = 0;
350 break;
351
352 /* The RNDIS specification is incomplete/wrong. Some versions
353 * of MS-Windows expect OIDs that aren't specified there. Other
354 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
355 */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000356 case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
357 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
Harvey Harrison35c26c22009-02-14 22:56:56 -0800358 *outbuf = cpu_to_le32(
Linus Walleije20289e2012-05-11 22:17:19 +0000359 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
360 | RNDIS_MAC_OPTION_FULL_DUPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 retval = 0;
362 break;
363
364 /* statistics OIDs (table 4-2) */
365
366 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000367 case RNDIS_OID_GEN_XMIT_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_XMIT_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700370 if (stats) {
371 *outbuf = cpu_to_le32(stats->tx_packets
372 - stats->tx_errors - stats->tx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 break;
376
377 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000378 case RNDIS_OID_GEN_RCV_OK:
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_RCV_OK\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700381 if (stats) {
382 *outbuf = cpu_to_le32(stats->rx_packets
383 - stats->rx_errors - stats->rx_dropped);
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_XMIT_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_XMIT_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700392 if (stats) {
393 *outbuf = cpu_to_le32(stats->tx_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_ERROR:
David Brownell340600a2005-04-28 13:45:25 -0700400 if (rndis_debug > 1)
Linus Walleij8cdddc32012-05-11 22:16:08 +0000401 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700402 if (stats) {
403 *outbuf = cpu_to_le32(stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 break;
David Brownell7e27f182006-06-13 09:54:40 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000409 case RNDIS_OID_GEN_RCV_NO_BUFFER:
410 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700411 if (stats) {
412 *outbuf = cpu_to_le32(stats->rx_dropped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 break;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* ieee802.3 OIDs (table 4-3) */
418
419 /* mandatory */
Linus Walleij8cdddc32012-05-11 22:16:08 +0000420 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000421 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
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_CURRENT_ADDRESS:
431 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100432 if (params->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 length = ETH_ALEN;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100434 memcpy(outbuf, params->host_mac, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 retval = 0;
436 }
437 break;
David Brownell7e27f182006-06-13 09:54:40 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000440 case RNDIS_OID_802_3_MULTICAST_LIST:
441 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300443 *outbuf = cpu_to_le32(0xE0000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 retval = 0;
445 break;
David Brownell7e27f182006-06-13 09:54:40 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000448 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
449 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Multicast base address only */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300451 *outbuf = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 retval = 0;
453 break;
David Brownell7e27f182006-06-13 09:54:40 -0700454
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000455 case RNDIS_OID_802_3_MAC_OPTIONS:
456 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
Qiuping Chen6bc21462009-07-01 03:49:29 -0700457 *outbuf = cpu_to_le32(0);
458 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460
461 /* ieee802.3 statistics OIDs (table 4-4) */
462
463 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000464 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
465 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700466 if (stats) {
467 *outbuf = cpu_to_le32(stats->rx_frame_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 retval = 0;
469 }
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_ONE_COLLISION:
474 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\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 /* mandatory */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000480 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
481 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300482 *outbuf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 retval = 0;
484 break;
David Brownell7e27f182006-06-13 09:54:40 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 default:
David Brownell00274922007-11-19 12:58:36 -0800487 pr_warning("%s: query unknown OID 0x%08X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800488 __func__, OID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
David Brownell340600a2005-04-28 13:45:25 -0700490 if (retval < 0)
491 length = 0;
David Brownell7e27f182006-06-13 09:54:40 -0700492
Mihai Donțua1df4e42010-09-08 02:54:02 +0300493 resp->InformationBufferLength = cpu_to_le32(length);
494 r->length = length + sizeof(*resp);
495 resp->MessageLength = cpu_to_le32(r->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return retval;
497}
498
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100499static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
500 u8 *buf, u32 buf_len, rndis_resp_t *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300502 rndis_set_cmplt_type *resp;
503 int i, retval = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (!r)
506 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300507 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!resp)
509 return -ENOMEM;
510
David Brownell340600a2005-04-28 13:45:25 -0700511 if (buf_len && rndis_debug > 1) {
David Brownell33376c12008-08-18 17:45:07 -0700512 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
David Brownell340600a2005-04-28 13:45:25 -0700513 for (i = 0; i < buf_len; i += 16) {
David Brownell33376c12008-08-18 17:45:07 -0700514 pr_debug("%03d: %08x %08x %08x %08x\n", i,
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700515 get_unaligned_le32(&buf[i]),
516 get_unaligned_le32(&buf[i + 4]),
517 get_unaligned_le32(&buf[i + 8]),
518 get_unaligned_le32(&buf[i + 12]));
David Brownell340600a2005-04-28 13:45:25 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 switch (OID) {
Linus Walleij8cdddc32012-05-11 22:16:08 +0000523 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
David Brownell340600a2005-04-28 13:45:25 -0700525 /* these NDIS_PACKET_TYPE_* bitflags are shared with
526 * cdc_filter; it's not RNDIS-specific
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
528 * PROMISCUOUS, DIRECTED,
529 * MULTICAST, ALL_MULTICAST, BROADCAST
530 */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700531 *params->filter = (u16)get_unaligned_le32(buf);
Linus Walleij8cdddc32012-05-11 22:16:08 +0000532 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800533 __func__, *params->filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* this call has a significant side effect: it's
536 * what makes the packet flow start and stop, like
537 * activating the CDC Ethernet altsetting.
538 */
David Brownell340600a2005-04-28 13:45:25 -0700539 retval = 0;
540 if (*params->filter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 params->state = RNDIS_DATA_INITIALIZED;
542 netif_carrier_on(params->dev);
543 if (netif_running(params->dev))
Mihai Donțua1df4e42010-09-08 02:54:02 +0300544 netif_wake_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 } else {
546 params->state = RNDIS_INITIALIZED;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300547 netif_carrier_off(params->dev);
548 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 break;
David Brownell7e27f182006-06-13 09:54:40 -0700551
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000552 case RNDIS_OID_802_3_MULTICAST_LIST:
David Brownell7e27f182006-06-13 09:54:40 -0700553 /* I think we can ignore this */
Linus Walleij4cc6c4d2012-05-11 22:16:16 +0000554 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 retval = 0;
556 break;
David Brownell340600a2005-04-28 13:45:25 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 default:
David Brownell00274922007-11-19 12:58:36 -0800559 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800560 __func__, OID, buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
David Brownell7e27f182006-06-13 09:54:40 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return retval;
564}
565
David Brownell7e27f182006-06-13 09:54:40 -0700566/*
567 * Response Functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
569
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100570static int rndis_init_response(struct rndis_params *params,
571 rndis_init_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300573 rndis_init_cmplt_type *resp;
574 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700575
David Brownell15b2d2b2008-06-19 18:19:16 -0700576 if (!params->dev)
577 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700578
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100579 r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700580 if (!r)
581 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300582 resp = (rndis_init_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700583
Linus Walleij51491162012-05-11 22:17:07 +0000584 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300585 resp->MessageLength = cpu_to_le32(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300587 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
588 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
589 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
590 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
591 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
xerox_lin72ffa182014-08-14 14:48:44 +0800592 resp->MaxPacketsPerTransfer = cpu_to_le32(params->max_pkt_per_xfer);
593 resp->MaxTransferSize = cpu_to_le32(params->max_pkt_per_xfer *
594 (params->dev->mtu
Mihai Donțua1df4e42010-09-08 02:54:02 +0300595 + sizeof(struct ethhdr)
596 + sizeof(struct rndis_packet_msg_type)
xerox_lin72ffa182014-08-14 14:48:44 +0800597 + 22));
Mihai Donțua1df4e42010-09-08 02:54:02 +0300598 resp->PacketAlignmentFactor = cpu_to_le32(0);
599 resp->AFListOffset = cpu_to_le32(0);
600 resp->AFListSize = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700601
David Brownell15b2d2b2008-06-19 18:19:16 -0700602 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100606static int rndis_query_response(struct rndis_params *params,
607 rndis_query_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 rndis_query_cmplt_type *resp;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300610 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700611
David Brownell33376c12008-08-18 17:45:07 -0700612 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
David Brownell15b2d2b2008-06-19 18:19:16 -0700613 if (!params->dev)
614 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700615
Shaun Tancheff87637162006-02-22 19:47:19 -0800616 /*
617 * we need more memory:
618 * gen_ndis_query_resp expects enough space for
619 * rndis_query_cmplt_type followed by data.
620 * oid_supported_list is the largest data reply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100622 r = rndis_add_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300623 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700624 if (!r)
625 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300626 resp = (rndis_query_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700627
Linus Walleij51491162012-05-11 22:17:07 +0000628 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
David Brownell7e27f182006-06-13 09:54:40 -0700630
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100631 if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
David Brownell340600a2005-04-28 13:45:25 -0700632 le32_to_cpu(buf->InformationBufferOffset)
Mihai Donțua1df4e42010-09-08 02:54:02 +0300633 + 8 + (u8 *)buf,
David Brownell340600a2005-04-28 13:45:25 -0700634 le32_to_cpu(buf->InformationBufferLength),
635 r)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* OID not supported */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300637 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
638 resp->MessageLength = cpu_to_le32(sizeof *resp);
639 resp->InformationBufferLength = cpu_to_le32(0);
640 resp->InformationBufferOffset = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 } else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300642 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700643
David Brownell15b2d2b2008-06-19 18:19:16 -0700644 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
647
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100648static int rndis_set_response(struct rndis_params *params,
649 rndis_set_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300651 u32 BufLength, BufOffset;
652 rndis_set_cmplt_type *resp;
653 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700654
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100655 r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700656 if (!r)
657 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300658 resp = (rndis_set_cmplt_type *)r->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Mihai Donțua1df4e42010-09-08 02:54:02 +0300660 BufLength = le32_to_cpu(buf->InformationBufferLength);
661 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
David Brownell15b2d2b2008-06-19 18:19:16 -0700663#ifdef VERBOSE_DEBUG
David Brownell33376c12008-08-18 17:45:07 -0700664 pr_debug("%s: Length: %d\n", __func__, BufLength);
665 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
666 pr_debug("%s: InfoBuffer: ", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 for (i = 0; i < BufLength; i++) {
David Brownell33376c12008-08-18 17:45:07 -0700669 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
David Brownell7e27f182006-06-13 09:54:40 -0700671
David Brownell33376c12008-08-18 17:45:07 -0700672 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#endif
David Brownell7e27f182006-06-13 09:54:40 -0700674
Linus Walleij51491162012-05-11 22:17:07 +0000675 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300676 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100678 if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
Mihai Donțua1df4e42010-09-08 02:54:02 +0300679 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
680 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
David Brownell7e27f182006-06-13 09:54:40 -0700681 else
Mihai Donțua1df4e42010-09-08 02:54:02 +0300682 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700683
David Brownell15b2d2b2008-06-19 18:19:16 -0700684 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 0;
686}
687
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100688static int rndis_reset_response(struct rndis_params *params,
689 rndis_reset_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300691 rndis_reset_cmplt_type *resp;
692 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700693
xerox_lin90de7402014-08-18 21:54:23 +0800694 u32 length;
695 u8 *xbuf;
696
697 /* drain the response queue */
Amit Pundird29237a2015-12-21 14:03:15 +0530698 while ((xbuf = rndis_get_next_response(params, &length)))
699 rndis_free_response(params, xbuf);
xerox_lin90de7402014-08-18 21:54:23 +0800700
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100701 r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700702 if (!r)
703 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300704 resp = (rndis_reset_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700705
Linus Walleij51491162012-05-11 22:17:07 +0000706 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300707 resp->MessageLength = cpu_to_le32(16);
708 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* resent information */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300710 resp->AddressingReset = cpu_to_le32(1);
David Brownell7e27f182006-06-13 09:54:40 -0700711
David Brownell15b2d2b2008-06-19 18:19:16 -0700712 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714}
715
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100716static int rndis_keepalive_response(struct rndis_params *params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300717 rndis_keepalive_msg_type *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300719 rndis_keepalive_cmplt_type *resp;
720 rndis_resp_t *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
723
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100724 r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
David Brownell340600a2005-04-28 13:45:25 -0700725 if (!r)
726 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300727 resp = (rndis_keepalive_cmplt_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700728
Linus Walleij51491162012-05-11 22:17:07 +0000729 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300730 resp->MessageLength = cpu_to_le32(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
Mihai Donțua1df4e42010-09-08 02:54:02 +0300732 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
David Brownell7e27f182006-06-13 09:54:40 -0700733
David Brownell15b2d2b2008-06-19 18:19:16 -0700734 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736}
737
738
David Brownell7e27f182006-06-13 09:54:40 -0700739/*
740 * Device to Host Comunication
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100742static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Mihai Donțua1df4e42010-09-08 02:54:02 +0300744 rndis_indicate_status_msg_type *resp;
745 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -0700746
David Brownell15b2d2b2008-06-19 18:19:16 -0700747 if (params->state == RNDIS_UNINITIALIZED)
David Brownell7e27f182006-06-13 09:54:40 -0700748 return -ENOTSUPP;
749
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100750 r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
David Brownell340600a2005-04-28 13:45:25 -0700751 if (!r)
752 return -ENOMEM;
Mihai Donțua1df4e42010-09-08 02:54:02 +0300753 resp = (rndis_indicate_status_msg_type *)r->buf;
David Brownell7e27f182006-06-13 09:54:40 -0700754
Linus Walleij51491162012-05-11 22:17:07 +0000755 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
Mihai Donțua1df4e42010-09-08 02:54:02 +0300756 resp->MessageLength = cpu_to_le32(20);
757 resp->Status = cpu_to_le32(status);
758 resp->StatusBufferLength = cpu_to_le32(0);
759 resp->StatusBufferOffset = cpu_to_le32(0);
David Brownell7e27f182006-06-13 09:54:40 -0700760
David Brownell15b2d2b2008-06-19 18:19:16 -0700761 params->resp_avail(params->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 0;
763}
764
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100765int rndis_signal_connect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100767 params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
768 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500770EXPORT_SYMBOL_GPL(rndis_signal_connect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100772int rndis_signal_disconnect(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100774 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
775 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500777EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100779void rndis_uninit(struct rndis_params *params)
David Brownell340600a2005-04-28 13:45:25 -0700780{
David Brownell486e2df2005-05-24 17:51:52 -0700781 u8 *buf;
782 u32 length;
783
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100784 if (!params)
David Brownell340600a2005-04-28 13:45:25 -0700785 return;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100786 params->state = RNDIS_UNINITIALIZED;
David Brownell486e2df2005-05-24 17:51:52 -0700787
788 /* drain the response queue */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100789 while ((buf = rndis_get_next_response(params, &length)))
790 rndis_free_response(params, buf);
David Brownell340600a2005-04-28 13:45:25 -0700791}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500792EXPORT_SYMBOL_GPL(rndis_uninit);
David Brownell340600a2005-04-28 13:45:25 -0700793
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100794void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100796 params->host_mac = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500798EXPORT_SYMBOL_GPL(rndis_set_host_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
David Brownell7e27f182006-06-13 09:54:40 -0700800/*
801 * Message Parser
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100803int rndis_msg_parser(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 u32 MsgType, MsgLength;
806 __le32 *tmp;
David Brownell7e27f182006-06-13 09:54:40 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!buf)
809 return -ENOMEM;
David Brownell7e27f182006-06-13 09:54:40 -0700810
Mihai Donțua1df4e42010-09-08 02:54:02 +0300811 tmp = (__le32 *)buf;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700812 MsgType = get_unaligned_le32(tmp++);
813 MsgLength = get_unaligned_le32(tmp++);
David Brownell7e27f182006-06-13 09:54:40 -0700814
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100815 if (!params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -ENOTSUPP;
David Brownell7e27f182006-06-13 09:54:40 -0700817
David Brownell340600a2005-04-28 13:45:25 -0700818 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
819 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
820 * and normal HC level polling to see if there's any IN traffic.
821 */
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* For USB: responses may take up to 10 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700824 switch (MsgType) {
Linus Walleij51491162012-05-11 22:17:07 +0000825 case RNDIS_MSG_INIT:
826 pr_debug("%s: RNDIS_MSG_INIT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300827 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 params->state = RNDIS_INITIALIZED;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100829 return rndis_init_response(params, (rndis_init_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700830
Linus Walleij51491162012-05-11 22:17:07 +0000831 case RNDIS_MSG_HALT:
832 pr_debug("%s: RNDIS_MSG_HALT\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300833 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 params->state = RNDIS_UNINITIALIZED;
835 if (params->dev) {
Mihai Donțua1df4e42010-09-08 02:54:02 +0300836 netif_carrier_off(params->dev);
837 netif_stop_queue(params->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 return 0;
David Brownell7e27f182006-06-13 09:54:40 -0700840
Linus Walleij51491162012-05-11 22:17:07 +0000841 case RNDIS_MSG_QUERY:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100842 return rndis_query_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300843 (rndis_query_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700844
Linus Walleij51491162012-05-11 22:17:07 +0000845 case RNDIS_MSG_SET:
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100846 return rndis_set_response(params, (rndis_set_msg_type *)buf);
David Brownell7e27f182006-06-13 09:54:40 -0700847
Linus Walleij51491162012-05-11 22:17:07 +0000848 case RNDIS_MSG_RESET:
849 pr_debug("%s: RNDIS_MSG_RESET\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300850 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100851 return rndis_reset_response(params,
Mihai Donțua1df4e42010-09-08 02:54:02 +0300852 (rndis_reset_msg_type *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Linus Walleij51491162012-05-11 22:17:07 +0000854 case RNDIS_MSG_KEEPALIVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* For USB: host does this every 5 seconds */
David Brownell340600a2005-04-28 13:45:25 -0700856 if (rndis_debug > 1)
Linus Walleij51491162012-05-11 22:17:07 +0000857 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300858 __func__);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100859 return rndis_keepalive_response(params,
David Brownell7e27f182006-06-13 09:54:40 -0700860 (rndis_keepalive_msg_type *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 buf);
David Brownell7e27f182006-06-13 09:54:40 -0700862
863 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* At least Windows XP emits some undefined RNDIS messages.
865 * In one case those messages seemed to relate to the host
866 * suspending itself.
867 */
David Brownell00274922007-11-19 12:58:36 -0800868 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
Mihai Donțua1df4e42010-09-08 02:54:02 +0300869 __func__, MsgType, MsgLength);
Andy Shevchenkod3091cf2012-08-07 19:07:58 +0300870 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
871 buf, MsgLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
873 }
David Brownell7e27f182006-06-13 09:54:40 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return -ENOTSUPP;
876}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500877EXPORT_SYMBOL_GPL(rndis_msg_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100879static inline int rndis_get_nr(void)
880{
881 return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL);
882}
883
884static inline void rndis_put_nr(int nr)
885{
886 ida_simple_remove(&rndis_ida, nr);
887}
888
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100889struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100891 struct rndis_params *params;
Andrzej Pietrasiewicz81dff862015-05-18 17:40:04 +0200892 int i;
David Brownell7e27f182006-06-13 09:54:40 -0700893
David Brownell15b2d2b2008-06-19 18:19:16 -0700894 if (!resp_avail)
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100895 return ERR_PTR(-EINVAL);
David Brownell15b2d2b2008-06-19 18:19:16 -0700896
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100897 i = rndis_get_nr();
898 if (i < 0) {
899 pr_debug("failed\n");
900
901 return ERR_PTR(-ENODEV);
902 }
903
904 params = kzalloc(sizeof(*params), GFP_KERNEL);
905 if (!params) {
906 rndis_put_nr(i);
907
908 return ERR_PTR(-ENOMEM);
909 }
910
911#ifdef CONFIG_USB_GADGET_DEBUG_FILES
912 {
913 struct proc_dir_entry *proc_entry;
914 char name[20];
915
916 sprintf(name, NAME_TEMPLATE, i);
917 proc_entry = proc_create_data(name, 0660, NULL,
918 &rndis_proc_fops, params);
919 if (!proc_entry) {
920 kfree(params);
921 rndis_put_nr(i);
922
923 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925 }
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100926#endif
David Brownell7e27f182006-06-13 09:54:40 -0700927
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100928 params->confignr = i;
929 params->used = 1;
930 params->state = RNDIS_UNINITIALIZED;
931 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
932 params->resp_avail = resp_avail;
933 params->v = v;
Geliang Tangf6281af2015-12-19 00:34:33 +0800934 INIT_LIST_HEAD(&params->resp_queue);
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100935 pr_debug("%s: configNr = %d\n", __func__, i);
936
937 return params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500939EXPORT_SYMBOL_GPL(rndis_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100941void rndis_deregister(struct rndis_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Andrzej Pietrasiewicz81dff862015-05-18 17:40:04 +0200943 int i;
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100944
Mihai Donțua1df4e42010-09-08 02:54:02 +0300945 pr_debug("%s:\n", __func__);
David Brownell7e27f182006-06-13 09:54:40 -0700946
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100947 if (!params)
948 return;
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100949
950 i = params->confignr;
951
952#ifdef CONFIG_USB_GADGET_DEBUG_FILES
953 {
Andrzej Pietrasiewiczd6d22922015-02-06 13:43:30 +0100954 char name[20];
955
956 sprintf(name, NAME_TEMPLATE, i);
957 remove_proc_entry(name, NULL);
958 }
959#endif
960
961 kfree(params);
962 rndis_put_nr(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500964EXPORT_SYMBOL_GPL(rndis_deregister);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100965int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
966 u16 *cdc_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
David Brownell33376c12008-08-18 17:45:07 -0700968 pr_debug("%s:\n", __func__);
David Brownell15b2d2b2008-06-19 18:19:16 -0700969 if (!dev)
970 return -EINVAL;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100971 if (!params)
972 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700973
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100974 params->dev = dev;
975 params->filter = cdc_filter;
David Brownell7e27f182006-06-13 09:54:40 -0700976
xerox_lin72ffa182014-08-14 14:48:44 +0800977 rndis_ul_max_xfer_size_rcvd = 0;
978 rndis_ul_max_pkt_per_xfer_rcvd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500981EXPORT_SYMBOL_GPL(rndis_set_param_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100983int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
984 const char *vendorDescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
David Brownell33376c12008-08-18 17:45:07 -0700986 pr_debug("%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (!vendorDescr) return -1;
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100988 if (!params)
989 return -1;
David Brownell7e27f182006-06-13 09:54:40 -0700990
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100991 params->vendorID = vendorID;
992 params->vendorDescr = vendorDescr;
David Brownell7e27f182006-06-13 09:54:40 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500996EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +0100998int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
David Brownell33376c12008-08-18 17:45:07 -07001000 pr_debug("%s: %u %u\n", __func__, medium, speed);
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001001 if (!params)
1002 return -1;
David Brownell7e27f182006-06-13 09:54:40 -07001003
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001004 params->medium = medium;
1005 params->speed = speed;
David Brownell7e27f182006-06-13 09:54:40 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return 0;
1008}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001009EXPORT_SYMBOL_GPL(rndis_set_param_medium);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Amit Pundird29237a2015-12-21 14:03:15 +05301011void rndis_set_max_pkt_xfer(struct rndis_params *params, u8 max_pkt_per_xfer)
xerox_lin72ffa182014-08-14 14:48:44 +08001012{
1013 pr_debug("%s:\n", __func__);
1014
Amit Pundird29237a2015-12-21 14:03:15 +05301015 params->max_pkt_per_xfer = max_pkt_per_xfer;
xerox_lin72ffa182014-08-14 14:48:44 +08001016}
1017
Mihai Donțua1df4e42010-09-08 02:54:02 +03001018void rndis_add_hdr(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001020 struct rndis_packet_msg_type *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (!skb)
1023 return;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001024 header = (void *)skb_push(skb, sizeof(*header));
1025 memset(header, 0, sizeof *header);
Linus Walleij51491162012-05-11 22:17:07 +00001026 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 header->MessageLength = cpu_to_le32(skb->len);
Mihai Donțua1df4e42010-09-08 02:54:02 +03001028 header->DataOffset = cpu_to_le32(36);
1029 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001031EXPORT_SYMBOL_GPL(rndis_add_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001033void rndis_free_response(struct rndis_params *params, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Geliang Tangf6281af2015-12-19 00:34:33 +08001035 rndis_resp_t *r, *n;
David Brownell7e27f182006-06-13 09:54:40 -07001036
Geliang Tangf6281af2015-12-19 00:34:33 +08001037 list_for_each_entry_safe(r, n, &params->resp_queue, list) {
Julia Lawall1c17a352016-01-25 16:21:54 +01001038 if (r->buf == buf) {
Mihai Donțua1df4e42010-09-08 02:54:02 +03001039 list_del(&r->list);
1040 kfree(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 }
1043}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001044EXPORT_SYMBOL_GPL(rndis_free_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001046u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Geliang Tangf6281af2015-12-19 00:34:33 +08001048 rndis_resp_t *r, *n;
David Brownell7e27f182006-06-13 09:54:40 -07001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!length) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001051
Geliang Tangf6281af2015-12-19 00:34:33 +08001052 list_for_each_entry_safe(r, n, &params->resp_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (!r->send) {
1054 r->send = 1;
1055 *length = r->length;
1056 return r->buf;
1057 }
1058 }
David Brownell7e27f182006-06-13 09:54:40 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return NULL;
1061}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001062EXPORT_SYMBOL_GPL(rndis_get_next_response);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Andrzej Pietrasiewicz83210e52015-03-20 08:18:47 +01001064static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Mihai Donțua1df4e42010-09-08 02:54:02 +03001066 rndis_resp_t *r;
David Brownell7e27f182006-06-13 09:54:40 -07001067
Mihai Donțua1df4e42010-09-08 02:54:02 +03001068 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1069 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (!r) return NULL;
David Brownell7e27f182006-06-13 09:54:40 -07001071
Mihai Donțua1df4e42010-09-08 02:54:02 +03001072 r->buf = (u8 *)(r + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 r->length = length;
1074 r->send = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001075
Geliang Tangf6281af2015-12-19 00:34:33 +08001076 list_add_tail(&r->list, &params->resp_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return r;
1078}
1079
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001080int rndis_rm_hdr(struct gether *port,
1081 struct sk_buff *skb,
1082 struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
xerox_lin72ffa182014-08-14 14:48:44 +08001084 int num_pkts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
xerox_lin72ffa182014-08-14 14:48:44 +08001086 if (skb->len > rndis_ul_max_xfer_size_rcvd)
1087 rndis_ul_max_xfer_size_rcvd = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
xerox_lin72ffa182014-08-14 14:48:44 +08001089 while (skb->len) {
1090 struct rndis_packet_msg_type *hdr;
1091 struct sk_buff *skb2;
1092 u32 msg_len, data_offset, data_len;
1093
1094 /* some rndis hosts send extra byte to avoid zlp, ignore it */
1095 if (skb->len == 1) {
1096 dev_kfree_skb_any(skb);
1097 return 0;
1098 }
1099
1100 if (skb->len < sizeof *hdr) {
Amit Pundir928b59a2015-12-30 03:05:44 +05301101 pr_err("invalid rndis pkt: skblen:%u hdr_len:%zu",
xerox_lin72ffa182014-08-14 14:48:44 +08001102 skb->len, sizeof *hdr);
1103 dev_kfree_skb_any(skb);
1104 return -EINVAL;
1105 }
1106
1107 hdr = (void *)skb->data;
1108 msg_len = le32_to_cpu(hdr->MessageLength);
1109 data_offset = le32_to_cpu(hdr->DataOffset);
1110 data_len = le32_to_cpu(hdr->DataLength);
1111
1112 if (skb->len < msg_len ||
1113 ((data_offset + data_len + 8) > msg_len)) {
1114 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1115 le32_to_cpu(hdr->MessageType),
1116 msg_len, data_offset, data_len, skb->len);
1117 dev_kfree_skb_any(skb);
1118 return -EOVERFLOW;
1119 }
1120 if (le32_to_cpu(hdr->MessageType) != RNDIS_MSG_PACKET) {
1121 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1122 le32_to_cpu(hdr->MessageType),
1123 msg_len, data_offset, data_len, skb->len);
1124 dev_kfree_skb_any(skb);
1125 return -EINVAL;
1126 }
1127
1128 skb_pull(skb, data_offset + 8);
1129
1130 if (msg_len == skb->len) {
1131 skb_trim(skb, data_len);
1132 break;
1133 }
1134
1135 skb2 = skb_clone(skb, GFP_ATOMIC);
1136 if (!skb2) {
1137 pr_err("%s:skb clone failed\n", __func__);
1138 dev_kfree_skb_any(skb);
1139 return -ENOMEM;
1140 }
1141
1142 skb_pull(skb, msg_len - sizeof *hdr);
1143 skb_trim(skb2, data_len);
1144 skb_queue_tail(list, skb2);
1145
1146 num_pkts++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001147 }
xerox_lin72ffa182014-08-14 14:48:44 +08001148
1149 if (num_pkts > rndis_ul_max_pkt_per_xfer_rcvd)
1150 rndis_ul_max_pkt_per_xfer_rcvd = num_pkts;
David Brownell6cdee102005-04-18 17:39:34 -07001151
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001152 skb_queue_tail(list, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return 0;
1154}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001155EXPORT_SYMBOL_GPL(rndis_rm_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Mihai Donțua1df4e42010-09-08 02:54:02 +03001157#ifdef CONFIG_USB_GADGET_DEBUG_FILES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001159static int rndis_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001161 rndis_params *param = m->private;
David Brownell7e27f182006-06-13 09:54:40 -07001162
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001163 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 "Config Nr. %d\n"
1165 "used : %s\n"
1166 "state : %s\n"
1167 "medium : 0x%08X\n"
1168 "speed : %d\n"
1169 "cable : %s\n"
1170 "vendor ID : 0x%08X\n"
David Brownell7e27f182006-06-13 09:54:40 -07001171 "vendor : %s\n",
1172 param->confignr, (param->used) ? "y" : "n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 ({ char *s = "?";
1174 switch (param->state) {
1175 case RNDIS_UNINITIALIZED:
1176 s = "RNDIS_UNINITIALIZED"; break;
1177 case RNDIS_INITIALIZED:
1178 s = "RNDIS_INITIALIZED"; break;
1179 case RNDIS_DATA_INITIALIZED:
1180 s = "RNDIS_DATA_INITIALIZED"; break;
Joe Perches2b84f922013-10-08 16:01:37 -07001181 } s; }),
David Brownell7e27f182006-06-13 09:54:40 -07001182 param->medium,
1183 (param->media_state) ? 0 : param->speed*100,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 (param->media_state) ? "disconnected" : "connected",
David Brownell7e27f182006-06-13 09:54:40 -07001185 param->vendorID, param->vendorDescr);
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001189static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
Mihai Donțua1df4e42010-09-08 02:54:02 +03001190 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Al Virod9dda782013-03-31 18:16:14 -04001192 rndis_params *p = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 u32 speed = 0;
1194 int i, fl_speed = 0;
David Brownell7e27f182006-06-13 09:54:40 -07001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 for (i = 0; i < count; i++) {
1197 char c;
1198 if (get_user(c, buffer))
1199 return -EFAULT;
1200 switch (c) {
1201 case '0':
1202 case '1':
1203 case '2':
1204 case '3':
1205 case '4':
1206 case '5':
1207 case '6':
1208 case '7':
1209 case '8':
1210 case '9':
1211 fl_speed = 1;
Mihai Donțua1df4e42010-09-08 02:54:02 +03001212 speed = speed * 10 + c - '0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
1214 case 'C':
1215 case 'c':
Andrzej Pietrasiewicz868055f2015-05-18 17:40:02 +02001216 rndis_signal_connect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218 case 'D':
1219 case 'd':
Andrzej Pietrasiewicz868055f2015-05-18 17:40:02 +02001220 rndis_signal_disconnect(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 break;
David Brownell7e27f182006-06-13 09:54:40 -07001222 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (fl_speed) p->speed = speed;
David Brownell33376c12008-08-18 17:45:07 -07001224 else pr_debug("%c is not valid\n", c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
1226 }
David Brownell7e27f182006-06-13 09:54:40 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 buffer++;
1229 }
David Brownell7e27f182006-06-13 09:54:40 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return count;
1232}
1233
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001234static int rndis_proc_open(struct inode *inode, struct file *file)
1235{
Al Virod9dda782013-03-31 18:16:14 -04001236 return single_open(file, rndis_proc_show, PDE_DATA(inode));
Alexey Dobriyane184d5f2008-05-14 16:25:13 -07001237}
1238
1239static const struct file_operations rndis_proc_fops = {
1240 .owner = THIS_MODULE,
1241 .open = rndis_proc_open,
1242 .read = seq_read,
1243 .llseek = seq_lseek,
1244 .release = single_release,
1245 .write = rndis_proc_write,
1246};
1247
Mihai Donțua1df4e42010-09-08 02:54:02 +03001248#define NAME_TEMPLATE "driver/rndis-%03d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Mihai Donțua1df4e42010-09-08 02:54:02 +03001250#endif /* CONFIG_USB_GADGET_DEBUG_FILES */