xref: /linux/drivers/usb/gadget/function/rndis.c (revision a0efa2f362a69e47b9d8b48f770ef3a0249a7911)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * RNDIS MSG parser
4  *
5  * Authors:	Benedikt Spranger, Pengutronix
6  *		Robert Schwebel, Pengutronix
7  *
8  *		This software was originally developed in conformance with
9  *		Microsoft's Remote NDIS Specification License Agreement.
10  *
11  * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
12  *		Fixed message length bug in init_response
13  *
14  * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
15  *		Fixed rndis_rm_hdr length bug.
16  *
17  * Copyright (C) 2004 by David Brownell
18  *		updates to merge with Linux 2.6, better match RNDIS spec
19  */
20 
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/idr.h>
26 #include <linux/list.h>
27 #include <linux/proc_fs.h>
28 #include <linux/slab.h>
29 #include <linux/seq_file.h>
30 #include <linux/netdevice.h>
31 
32 #include <asm/io.h>
33 #include <asm/byteorder.h>
34 #include <linux/unaligned.h>
35 
36 #include "u_rndis.h"
37 
38 #undef	VERBOSE_DEBUG
39 
40 #include "rndis.h"
41 
42 
43 /* The driver for your USB chip needs to support ep0 OUT to work with
44  * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
45  *
46  * Windows hosts need an INF file like Documentation/usb/linux.inf
47  * and will be happier if you provide the host_addr module parameter.
48  */
49 
50 #if 0
51 static int rndis_debug = 0;
52 module_param (rndis_debug, int, 0);
53 MODULE_PARM_DESC (rndis_debug, "enable debugging");
54 #else
55 #define rndis_debug		0
56 #endif
57 
58 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
59 
60 #define	NAME_TEMPLATE "driver/rndis-%03d"
61 
62 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
63 
64 static DEFINE_IDA(rndis_ida);
65 
66 /* Driver Version */
67 static const __le32 rndis_driver_version = cpu_to_le32(1);
68 
69 /* Function Prototypes */
70 static rndis_resp_t *rndis_add_response(struct rndis_params *params,
71 					u32 length);
72 
73 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
74 
75 static const struct proc_ops rndis_proc_ops;
76 
77 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
78 
79 /* supported OIDs */
80 static const u32 oid_supported_list[] = {
81 	/* the general stuff */
82 	RNDIS_OID_GEN_SUPPORTED_LIST,
83 	RNDIS_OID_GEN_HARDWARE_STATUS,
84 	RNDIS_OID_GEN_MEDIA_SUPPORTED,
85 	RNDIS_OID_GEN_MEDIA_IN_USE,
86 	RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
87 	RNDIS_OID_GEN_LINK_SPEED,
88 	RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
89 	RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
90 	RNDIS_OID_GEN_VENDOR_ID,
91 	RNDIS_OID_GEN_VENDOR_DESCRIPTION,
92 	RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
93 	RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
94 	RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
95 	RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
96 	RNDIS_OID_GEN_PHYSICAL_MEDIUM,
97 
98 	/* the statistical stuff */
99 	RNDIS_OID_GEN_XMIT_OK,
100 	RNDIS_OID_GEN_RCV_OK,
101 	RNDIS_OID_GEN_XMIT_ERROR,
102 	RNDIS_OID_GEN_RCV_ERROR,
103 	RNDIS_OID_GEN_RCV_NO_BUFFER,
104 #ifdef	RNDIS_OPTIONAL_STATS
105 	RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
106 	RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
107 	RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
108 	RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
109 	RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
110 	RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
111 	RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
112 	RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
113 	RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
114 	RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
115 	RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
116 	RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
117 	RNDIS_OID_GEN_RCV_CRC_ERROR,
118 	RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
119 #endif	/* RNDIS_OPTIONAL_STATS */
120 
121 	/* mandatory 802.3 */
122 	/* the general stuff */
123 	RNDIS_OID_802_3_PERMANENT_ADDRESS,
124 	RNDIS_OID_802_3_CURRENT_ADDRESS,
125 	RNDIS_OID_802_3_MULTICAST_LIST,
126 	RNDIS_OID_802_3_MAC_OPTIONS,
127 	RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
128 
129 	/* the statistical stuff */
130 	RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
131 	RNDIS_OID_802_3_XMIT_ONE_COLLISION,
132 	RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
133 #ifdef	RNDIS_OPTIONAL_STATS
134 	RNDIS_OID_802_3_XMIT_DEFERRED,
135 	RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
136 	RNDIS_OID_802_3_RCV_OVERRUN,
137 	RNDIS_OID_802_3_XMIT_UNDERRUN,
138 	RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
139 	RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
140 	RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
141 #endif	/* RNDIS_OPTIONAL_STATS */
142 
143 #ifdef	RNDIS_PM
144 	/* PM and wakeup are "mandatory" for USB, but the RNDIS specs
145 	 * don't say what they mean ... and the NDIS specs are often
146 	 * confusing and/or ambiguous in this context.  (That is, more
147 	 * so than their specs for the other OIDs.)
148 	 *
149 	 * FIXME someone who knows what these should do, please
150 	 * implement them!
151 	 */
152 
153 	/* power management */
154 	OID_PNP_CAPABILITIES,
155 	OID_PNP_QUERY_POWER,
156 	OID_PNP_SET_POWER,
157 
158 #ifdef	RNDIS_WAKEUP
159 	/* wake up host */
160 	OID_PNP_ENABLE_WAKE_UP,
161 	OID_PNP_ADD_WAKE_UP_PATTERN,
162 	OID_PNP_REMOVE_WAKE_UP_PATTERN,
163 #endif	/* RNDIS_WAKEUP */
164 #endif	/* RNDIS_PM */
165 };
166 
167 
168 /* NDIS Functions */
169 static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
170 			       unsigned buf_len, rndis_resp_t *r)
171 {
172 	int retval = -ENOTSUPP;
173 	u32 length = 4;	/* usually */
174 	__le32 *outbuf;
175 	int i, count;
176 	rndis_query_cmplt_type *resp;
177 	struct net_device *net;
178 	struct rtnl_link_stats64 temp;
179 	const struct rtnl_link_stats64 *stats;
180 
181 	if (!r) return -ENOMEM;
182 	resp = (rndis_query_cmplt_type *)r->buf;
183 
184 	if (!resp) return -ENOMEM;
185 
186 	if (buf_len && rndis_debug > 1) {
187 		pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
188 		for (i = 0; i < buf_len; i += 16) {
189 			pr_debug("%03d: %08x %08x %08x %08x\n", i,
190 				get_unaligned_le32(&buf[i]),
191 				get_unaligned_le32(&buf[i + 4]),
192 				get_unaligned_le32(&buf[i + 8]),
193 				get_unaligned_le32(&buf[i + 12]));
194 		}
195 	}
196 
197 	/* response goes here, right after the header */
198 	outbuf = (__le32 *)&resp[1];
199 	resp->InformationBufferOffset = cpu_to_le32(16);
200 
201 	net = params->dev;
202 	stats = dev_get_stats(net, &temp);
203 
204 	switch (OID) {
205 
206 	/* general oids (table 4-1) */
207 
208 	/* mandatory */
209 	case RNDIS_OID_GEN_SUPPORTED_LIST:
210 		pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
211 		length = sizeof(oid_supported_list);
212 		count  = length / sizeof(u32);
213 		for (i = 0; i < count; i++)
214 			outbuf[i] = cpu_to_le32(oid_supported_list[i]);
215 		retval = 0;
216 		break;
217 
218 	/* mandatory */
219 	case RNDIS_OID_GEN_HARDWARE_STATUS:
220 		pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
221 		/* Bogus question!
222 		 * Hardware must be ready to receive high level protocols.
223 		 * BTW:
224 		 * reddite ergo quae sunt Caesaris Caesari
225 		 * et quae sunt Dei Deo!
226 		 */
227 		*outbuf = cpu_to_le32(0);
228 		retval = 0;
229 		break;
230 
231 	/* mandatory */
232 	case RNDIS_OID_GEN_MEDIA_SUPPORTED:
233 		pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
234 		*outbuf = cpu_to_le32(params->medium);
235 		retval = 0;
236 		break;
237 
238 	/* mandatory */
239 	case RNDIS_OID_GEN_MEDIA_IN_USE:
240 		pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
241 		/* one medium, one transport... (maybe you do it better) */
242 		*outbuf = cpu_to_le32(params->medium);
243 		retval = 0;
244 		break;
245 
246 	/* mandatory */
247 	case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
248 		pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
249 		if (params->dev) {
250 			*outbuf = cpu_to_le32(params->dev->mtu);
251 			retval = 0;
252 		}
253 		break;
254 
255 	/* mandatory */
256 	case RNDIS_OID_GEN_LINK_SPEED:
257 		if (rndis_debug > 1)
258 			pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
259 		if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
260 			*outbuf = cpu_to_le32(0);
261 		else
262 			*outbuf = cpu_to_le32(params->speed);
263 		retval = 0;
264 		break;
265 
266 	/* mandatory */
267 	case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
268 		pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
269 		if (params->dev) {
270 			*outbuf = cpu_to_le32(params->dev->mtu);
271 			retval = 0;
272 		}
273 		break;
274 
275 	/* mandatory */
276 	case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
277 		pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
278 		if (params->dev) {
279 			*outbuf = cpu_to_le32(params->dev->mtu);
280 			retval = 0;
281 		}
282 		break;
283 
284 	/* mandatory */
285 	case RNDIS_OID_GEN_VENDOR_ID:
286 		pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
287 		*outbuf = cpu_to_le32(params->vendorID);
288 		retval = 0;
289 		break;
290 
291 	/* mandatory */
292 	case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
293 		pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
294 		if (params->vendorDescr) {
295 			length = strlen(params->vendorDescr);
296 			memcpy(outbuf, params->vendorDescr, length);
297 		} else {
298 			outbuf[0] = 0;
299 		}
300 		retval = 0;
301 		break;
302 
303 	case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
304 		pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
305 		/* Created as LE */
306 		*outbuf = rndis_driver_version;
307 		retval = 0;
308 		break;
309 
310 	/* mandatory */
311 	case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
312 		pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
313 		*outbuf = cpu_to_le32(*params->filter);
314 		retval = 0;
315 		break;
316 
317 	/* mandatory */
318 	case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
319 		pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
320 		*outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
321 		retval = 0;
322 		break;
323 
324 	/* mandatory */
325 	case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
326 		if (rndis_debug > 1)
327 			pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
328 		*outbuf = cpu_to_le32(params->media_state);
329 		retval = 0;
330 		break;
331 
332 	case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
333 		pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
334 		*outbuf = cpu_to_le32(0);
335 		retval = 0;
336 		break;
337 
338 	/* The RNDIS specification is incomplete/wrong.   Some versions
339 	 * of MS-Windows expect OIDs that aren't specified there.  Other
340 	 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
341 	 */
342 	case RNDIS_OID_GEN_MAC_OPTIONS:		/* from WinME */
343 		pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
344 		*outbuf = cpu_to_le32(
345 			  RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
346 			| RNDIS_MAC_OPTION_FULL_DUPLEX);
347 		retval = 0;
348 		break;
349 
350 	/* statistics OIDs (table 4-2) */
351 
352 	/* mandatory */
353 	case RNDIS_OID_GEN_XMIT_OK:
354 		if (rndis_debug > 1)
355 			pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
356 		if (stats) {
357 			*outbuf = cpu_to_le32(stats->tx_packets
358 				- stats->tx_errors - stats->tx_dropped);
359 			retval = 0;
360 		}
361 		break;
362 
363 	/* mandatory */
364 	case RNDIS_OID_GEN_RCV_OK:
365 		if (rndis_debug > 1)
366 			pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
367 		if (stats) {
368 			*outbuf = cpu_to_le32(stats->rx_packets
369 				- stats->rx_errors - stats->rx_dropped);
370 			retval = 0;
371 		}
372 		break;
373 
374 	/* mandatory */
375 	case RNDIS_OID_GEN_XMIT_ERROR:
376 		if (rndis_debug > 1)
377 			pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
378 		if (stats) {
379 			*outbuf = cpu_to_le32(stats->tx_errors);
380 			retval = 0;
381 		}
382 		break;
383 
384 	/* mandatory */
385 	case RNDIS_OID_GEN_RCV_ERROR:
386 		if (rndis_debug > 1)
387 			pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
388 		if (stats) {
389 			*outbuf = cpu_to_le32(stats->rx_errors);
390 			retval = 0;
391 		}
392 		break;
393 
394 	/* mandatory */
395 	case RNDIS_OID_GEN_RCV_NO_BUFFER:
396 		pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
397 		if (stats) {
398 			*outbuf = cpu_to_le32(stats->rx_dropped);
399 			retval = 0;
400 		}
401 		break;
402 
403 	/* ieee802.3 OIDs (table 4-3) */
404 
405 	/* mandatory */
406 	case RNDIS_OID_802_3_PERMANENT_ADDRESS:
407 		pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
408 		if (params->dev) {
409 			length = ETH_ALEN;
410 			memcpy(outbuf, params->host_mac, length);
411 			retval = 0;
412 		}
413 		break;
414 
415 	/* mandatory */
416 	case RNDIS_OID_802_3_CURRENT_ADDRESS:
417 		pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
418 		if (params->dev) {
419 			length = ETH_ALEN;
420 			memcpy(outbuf, params->host_mac, length);
421 			retval = 0;
422 		}
423 		break;
424 
425 	/* mandatory */
426 	case RNDIS_OID_802_3_MULTICAST_LIST:
427 		pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
428 		/* Multicast base address only */
429 		*outbuf = cpu_to_le32(0xE0000000);
430 		retval = 0;
431 		break;
432 
433 	/* mandatory */
434 	case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
435 		pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
436 		/* Multicast base address only */
437 		*outbuf = cpu_to_le32(1);
438 		retval = 0;
439 		break;
440 
441 	case RNDIS_OID_802_3_MAC_OPTIONS:
442 		pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
443 		*outbuf = cpu_to_le32(0);
444 		retval = 0;
445 		break;
446 
447 	/* ieee802.3 statistics OIDs (table 4-4) */
448 
449 	/* mandatory */
450 	case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
451 		pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
452 		if (stats) {
453 			*outbuf = cpu_to_le32(stats->rx_frame_errors);
454 			retval = 0;
455 		}
456 		break;
457 
458 	/* mandatory */
459 	case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
460 		pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
461 		*outbuf = cpu_to_le32(0);
462 		retval = 0;
463 		break;
464 
465 	/* mandatory */
466 	case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
467 		pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
468 		*outbuf = cpu_to_le32(0);
469 		retval = 0;
470 		break;
471 
472 	default:
473 		pr_warn("%s: query unknown OID 0x%08X\n", __func__, OID);
474 	}
475 	if (retval < 0)
476 		length = 0;
477 
478 	resp->InformationBufferLength = cpu_to_le32(length);
479 	r->length = length + sizeof(*resp);
480 	resp->MessageLength = cpu_to_le32(r->length);
481 	return retval;
482 }
483 
484 static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
485 			     u8 *buf, u32 buf_len, rndis_resp_t *r)
486 {
487 	rndis_set_cmplt_type *resp;
488 	int i, retval = -ENOTSUPP;
489 
490 	if (!r)
491 		return -ENOMEM;
492 	resp = (rndis_set_cmplt_type *)r->buf;
493 	if (!resp)
494 		return -ENOMEM;
495 
496 	if (buf_len && rndis_debug > 1) {
497 		pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
498 		for (i = 0; i < buf_len; i += 16) {
499 			pr_debug("%03d: %08x %08x %08x %08x\n", i,
500 				get_unaligned_le32(&buf[i]),
501 				get_unaligned_le32(&buf[i + 4]),
502 				get_unaligned_le32(&buf[i + 8]),
503 				get_unaligned_le32(&buf[i + 12]));
504 		}
505 	}
506 
507 	switch (OID) {
508 	case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
509 
510 		/* these NDIS_PACKET_TYPE_* bitflags are shared with
511 		 * cdc_filter; it's not RNDIS-specific
512 		 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
513 		 *	PROMISCUOUS, DIRECTED,
514 		 *	MULTICAST, ALL_MULTICAST, BROADCAST
515 		 */
516 		*params->filter = (u16)get_unaligned_le32(buf);
517 		pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
518 			__func__, *params->filter);
519 
520 		/* this call has a significant side effect:  it's
521 		 * what makes the packet flow start and stop, like
522 		 * activating the CDC Ethernet altsetting.
523 		 */
524 		retval = 0;
525 		if (*params->filter) {
526 			params->state = RNDIS_DATA_INITIALIZED;
527 			netif_carrier_on(params->dev);
528 			if (netif_running(params->dev))
529 				netif_wake_queue(params->dev);
530 		} else {
531 			params->state = RNDIS_INITIALIZED;
532 			netif_carrier_off(params->dev);
533 			netif_stop_queue(params->dev);
534 		}
535 		break;
536 
537 	case RNDIS_OID_802_3_MULTICAST_LIST:
538 		/* I think we can ignore this */
539 		pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
540 		retval = 0;
541 		break;
542 
543 	default:
544 		pr_warn("%s: set unknown OID 0x%08X, size %d\n",
545 			__func__, OID, buf_len);
546 	}
547 
548 	return retval;
549 }
550 
551 /*
552  * Response Functions
553  */
554 
555 static int rndis_init_response(struct rndis_params *params,
556 			       rndis_init_msg_type *buf)
557 {
558 	rndis_init_cmplt_type *resp;
559 	rndis_resp_t *r;
560 
561 	if (!params->dev)
562 		return -ENOTSUPP;
563 
564 	r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
565 	if (!r)
566 		return -ENOMEM;
567 	resp = (rndis_init_cmplt_type *)r->buf;
568 
569 	resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
570 	resp->MessageLength = cpu_to_le32(52);
571 	resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
572 	resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
573 	resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
574 	resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
575 	resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
576 	resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
577 	resp->MaxPacketsPerTransfer = cpu_to_le32(1);
578 	resp->MaxTransferSize = cpu_to_le32(
579 		  params->dev->mtu
580 		+ sizeof(struct ethhdr)
581 		+ sizeof(struct rndis_packet_msg_type)
582 		+ 22);
583 	resp->PacketAlignmentFactor = cpu_to_le32(0);
584 	resp->AFListOffset = cpu_to_le32(0);
585 	resp->AFListSize = cpu_to_le32(0);
586 
587 	params->resp_avail(params->v);
588 	return 0;
589 }
590 
591 static int rndis_query_response(struct rndis_params *params,
592 				rndis_query_msg_type *buf)
593 {
594 	rndis_query_cmplt_type *resp;
595 	rndis_resp_t *r;
596 
597 	/* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
598 	if (!params->dev)
599 		return -ENOTSUPP;
600 
601 	/*
602 	 * we need more memory:
603 	 * gen_ndis_query_resp expects enough space for
604 	 * rndis_query_cmplt_type followed by data.
605 	 * oid_supported_list is the largest data reply
606 	 */
607 	r = rndis_add_response(params,
608 		sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
609 	if (!r)
610 		return -ENOMEM;
611 	resp = (rndis_query_cmplt_type *)r->buf;
612 
613 	resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
614 	resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
615 
616 	if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
617 			le32_to_cpu(buf->InformationBufferOffset)
618 					+ 8 + (u8 *)buf,
619 			le32_to_cpu(buf->InformationBufferLength),
620 			r)) {
621 		/* OID not supported */
622 		resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
623 		resp->MessageLength = cpu_to_le32(sizeof *resp);
624 		resp->InformationBufferLength = cpu_to_le32(0);
625 		resp->InformationBufferOffset = cpu_to_le32(0);
626 	} else
627 		resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
628 
629 	params->resp_avail(params->v);
630 	return 0;
631 }
632 
633 static int rndis_set_response(struct rndis_params *params,
634 			      rndis_set_msg_type *buf)
635 {
636 	u32 BufLength, BufOffset;
637 	rndis_set_cmplt_type *resp;
638 	rndis_resp_t *r;
639 
640 	BufLength = le32_to_cpu(buf->InformationBufferLength);
641 	BufOffset = le32_to_cpu(buf->InformationBufferOffset);
642 	if ((BufLength > RNDIS_MAX_TOTAL_SIZE) ||
643 	    (BufOffset > RNDIS_MAX_TOTAL_SIZE) ||
644 	    (BufOffset + 8 >= RNDIS_MAX_TOTAL_SIZE))
645 		    return -EINVAL;
646 
647 	r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
648 	if (!r)
649 		return -ENOMEM;
650 	resp = (rndis_set_cmplt_type *)r->buf;
651 
652 #ifdef	VERBOSE_DEBUG
653 	pr_debug("%s: Length: %d\n", __func__, BufLength);
654 	pr_debug("%s: Offset: %d\n", __func__, BufOffset);
655 	pr_debug("%s: InfoBuffer: ", __func__);
656 
657 	for (i = 0; i < BufLength; i++) {
658 		pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
659 	}
660 
661 	pr_debug("\n");
662 #endif
663 
664 	resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
665 	resp->MessageLength = cpu_to_le32(16);
666 	resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
667 	if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
668 			((u8 *)buf) + 8 + BufOffset, BufLength, r))
669 		resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
670 	else
671 		resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
672 
673 	params->resp_avail(params->v);
674 	return 0;
675 }
676 
677 static int rndis_reset_response(struct rndis_params *params,
678 				rndis_reset_msg_type *buf)
679 {
680 	rndis_reset_cmplt_type *resp;
681 	rndis_resp_t *r;
682 	u8 *xbuf;
683 	u32 length;
684 
685 	/* drain the response queue */
686 	while ((xbuf = rndis_get_next_response(params, &length)))
687 		rndis_free_response(params, xbuf);
688 
689 	r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
690 	if (!r)
691 		return -ENOMEM;
692 	resp = (rndis_reset_cmplt_type *)r->buf;
693 
694 	resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
695 	resp->MessageLength = cpu_to_le32(16);
696 	resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
697 	/* resent information */
698 	resp->AddressingReset = cpu_to_le32(1);
699 
700 	params->resp_avail(params->v);
701 	return 0;
702 }
703 
704 static int rndis_keepalive_response(struct rndis_params *params,
705 				    rndis_keepalive_msg_type *buf)
706 {
707 	rndis_keepalive_cmplt_type *resp;
708 	rndis_resp_t *r;
709 
710 	/* host "should" check only in RNDIS_DATA_INITIALIZED state */
711 
712 	r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
713 	if (!r)
714 		return -ENOMEM;
715 	resp = (rndis_keepalive_cmplt_type *)r->buf;
716 
717 	resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
718 	resp->MessageLength = cpu_to_le32(16);
719 	resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
720 	resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
721 
722 	params->resp_avail(params->v);
723 	return 0;
724 }
725 
726 
727 /*
728  * Device to Host Comunication
729  */
730 static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
731 {
732 	rndis_indicate_status_msg_type *resp;
733 	rndis_resp_t *r;
734 
735 	if (params->state == RNDIS_UNINITIALIZED)
736 		return -ENOTSUPP;
737 
738 	r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
739 	if (!r)
740 		return -ENOMEM;
741 	resp = (rndis_indicate_status_msg_type *)r->buf;
742 
743 	resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
744 	resp->MessageLength = cpu_to_le32(20);
745 	resp->Status = cpu_to_le32(status);
746 	resp->StatusBufferLength = cpu_to_le32(0);
747 	resp->StatusBufferOffset = cpu_to_le32(0);
748 
749 	params->resp_avail(params->v);
750 	return 0;
751 }
752 
753 int rndis_signal_connect(struct rndis_params *params)
754 {
755 	params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
756 	return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
757 }
758 EXPORT_SYMBOL_GPL(rndis_signal_connect);
759 
760 int rndis_signal_disconnect(struct rndis_params *params)
761 {
762 	params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
763 	return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
764 }
765 EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
766 
767 void rndis_uninit(struct rndis_params *params)
768 {
769 	u8 *buf;
770 	u32 length;
771 
772 	if (!params)
773 		return;
774 	params->state = RNDIS_UNINITIALIZED;
775 
776 	/* drain the response queue */
777 	while ((buf = rndis_get_next_response(params, &length)))
778 		rndis_free_response(params, buf);
779 }
780 EXPORT_SYMBOL_GPL(rndis_uninit);
781 
782 void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
783 {
784 	params->host_mac = addr;
785 }
786 EXPORT_SYMBOL_GPL(rndis_set_host_mac);
787 
788 /*
789  * Message Parser
790  */
791 int rndis_msg_parser(struct rndis_params *params, u8 *buf)
792 {
793 	u32 MsgType, MsgLength;
794 	__le32 *tmp;
795 
796 	if (!buf)
797 		return -ENOMEM;
798 
799 	tmp = (__le32 *)buf;
800 	MsgType   = get_unaligned_le32(tmp++);
801 	MsgLength = get_unaligned_le32(tmp++);
802 
803 	if (!params)
804 		return -ENOTSUPP;
805 
806 	/* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
807 	 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
808 	 * and normal HC level polling to see if there's any IN traffic.
809 	 */
810 
811 	/* For USB: responses may take up to 10 seconds */
812 	switch (MsgType) {
813 	case RNDIS_MSG_INIT:
814 		pr_debug("%s: RNDIS_MSG_INIT\n",
815 			__func__);
816 		params->state = RNDIS_INITIALIZED;
817 		return rndis_init_response(params, (rndis_init_msg_type *)buf);
818 
819 	case RNDIS_MSG_HALT:
820 		pr_debug("%s: RNDIS_MSG_HALT\n",
821 			__func__);
822 		params->state = RNDIS_UNINITIALIZED;
823 		if (params->dev) {
824 			netif_carrier_off(params->dev);
825 			netif_stop_queue(params->dev);
826 		}
827 		return 0;
828 
829 	case RNDIS_MSG_QUERY:
830 		return rndis_query_response(params,
831 					(rndis_query_msg_type *)buf);
832 
833 	case RNDIS_MSG_SET:
834 		return rndis_set_response(params, (rndis_set_msg_type *)buf);
835 
836 	case RNDIS_MSG_RESET:
837 		pr_debug("%s: RNDIS_MSG_RESET\n",
838 			__func__);
839 		return rndis_reset_response(params,
840 					(rndis_reset_msg_type *)buf);
841 
842 	case RNDIS_MSG_KEEPALIVE:
843 		/* For USB: host does this every 5 seconds */
844 		if (rndis_debug > 1)
845 			pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
846 				__func__);
847 		return rndis_keepalive_response(params,
848 						 (rndis_keepalive_msg_type *)
849 						 buf);
850 
851 	default:
852 		/* At least Windows XP emits some undefined RNDIS messages.
853 		 * In one case those messages seemed to relate to the host
854 		 * suspending itself.
855 		 */
856 		pr_warn("%s: unknown RNDIS message 0x%08X len %d\n",
857 			__func__, MsgType, MsgLength);
858 		/* Garbled message can be huge, so limit what we display */
859 		if (MsgLength > 16)
860 			MsgLength = 16;
861 		print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
862 				     buf, MsgLength);
863 		break;
864 	}
865 
866 	return -ENOTSUPP;
867 }
868 EXPORT_SYMBOL_GPL(rndis_msg_parser);
869 
870 static inline int rndis_get_nr(void)
871 {
872 	return ida_alloc_max(&rndis_ida, 999, GFP_KERNEL);
873 }
874 
875 static inline void rndis_put_nr(int nr)
876 {
877 	ida_free(&rndis_ida, nr);
878 }
879 
880 struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
881 {
882 	struct rndis_params *params;
883 	int i;
884 
885 	if (!resp_avail)
886 		return ERR_PTR(-EINVAL);
887 
888 	i = rndis_get_nr();
889 	if (i < 0) {
890 		pr_debug("failed\n");
891 
892 		return ERR_PTR(-ENODEV);
893 	}
894 
895 	params = kzalloc(sizeof(*params), GFP_KERNEL);
896 	if (!params) {
897 		rndis_put_nr(i);
898 
899 		return ERR_PTR(-ENOMEM);
900 	}
901 
902 #ifdef	CONFIG_USB_GADGET_DEBUG_FILES
903 	{
904 		struct proc_dir_entry *proc_entry;
905 		char name[20];
906 
907 		sprintf(name, NAME_TEMPLATE, i);
908 		proc_entry = proc_create_data(name, 0660, NULL,
909 					      &rndis_proc_ops, params);
910 		if (!proc_entry) {
911 			kfree(params);
912 			rndis_put_nr(i);
913 
914 			return ERR_PTR(-EIO);
915 		}
916 	}
917 #endif
918 
919 	params->confignr = i;
920 	params->used = 1;
921 	params->state = RNDIS_UNINITIALIZED;
922 	params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
923 	params->resp_avail = resp_avail;
924 	params->v = v;
925 	INIT_LIST_HEAD(&params->resp_queue);
926 	spin_lock_init(&params->resp_lock);
927 	pr_debug("%s: configNr = %d\n", __func__, i);
928 
929 	return params;
930 }
931 EXPORT_SYMBOL_GPL(rndis_register);
932 
933 void rndis_deregister(struct rndis_params *params)
934 {
935 	int i;
936 
937 	pr_debug("%s:\n", __func__);
938 
939 	if (!params)
940 		return;
941 
942 	i = params->confignr;
943 
944 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
945 	{
946 		char name[20];
947 
948 		sprintf(name, NAME_TEMPLATE, i);
949 		remove_proc_entry(name, NULL);
950 	}
951 #endif
952 
953 	kfree(params);
954 	rndis_put_nr(i);
955 }
956 EXPORT_SYMBOL_GPL(rndis_deregister);
957 int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
958 			u16 *cdc_filter)
959 {
960 	pr_debug("%s:\n", __func__);
961 	if (!dev)
962 		return -EINVAL;
963 	if (!params)
964 		return -1;
965 
966 	params->dev = dev;
967 	params->filter = cdc_filter;
968 
969 	return 0;
970 }
971 EXPORT_SYMBOL_GPL(rndis_set_param_dev);
972 
973 int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
974 			   const char *vendorDescr)
975 {
976 	pr_debug("%s:\n", __func__);
977 	if (!vendorDescr) return -1;
978 	if (!params)
979 		return -1;
980 
981 	params->vendorID = vendorID;
982 	params->vendorDescr = vendorDescr;
983 
984 	return 0;
985 }
986 EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
987 
988 int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
989 {
990 	pr_debug("%s: %u %u\n", __func__, medium, speed);
991 	if (!params)
992 		return -1;
993 
994 	params->medium = medium;
995 	params->speed = speed;
996 
997 	return 0;
998 }
999 EXPORT_SYMBOL_GPL(rndis_set_param_medium);
1000 
1001 void rndis_add_hdr(struct sk_buff *skb)
1002 {
1003 	struct rndis_packet_msg_type *header;
1004 
1005 	if (!skb)
1006 		return;
1007 	header = skb_push(skb, sizeof(*header));
1008 	memset(header, 0, sizeof *header);
1009 	header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
1010 	header->MessageLength = cpu_to_le32(skb->len);
1011 	header->DataOffset = cpu_to_le32(36);
1012 	header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
1013 }
1014 EXPORT_SYMBOL_GPL(rndis_add_hdr);
1015 
1016 void rndis_free_response(struct rndis_params *params, u8 *buf)
1017 {
1018 	rndis_resp_t *r, *n;
1019 
1020 	spin_lock(&params->resp_lock);
1021 	list_for_each_entry_safe(r, n, &params->resp_queue, list) {
1022 		if (r->buf == buf) {
1023 			list_del(&r->list);
1024 			kfree(r);
1025 		}
1026 	}
1027 	spin_unlock(&params->resp_lock);
1028 }
1029 EXPORT_SYMBOL_GPL(rndis_free_response);
1030 
1031 u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
1032 {
1033 	rndis_resp_t *r, *n;
1034 
1035 	if (!length) return NULL;
1036 
1037 	spin_lock(&params->resp_lock);
1038 	list_for_each_entry_safe(r, n, &params->resp_queue, list) {
1039 		if (!r->send) {
1040 			r->send = 1;
1041 			*length = r->length;
1042 			spin_unlock(&params->resp_lock);
1043 			return r->buf;
1044 		}
1045 	}
1046 
1047 	spin_unlock(&params->resp_lock);
1048 	return NULL;
1049 }
1050 EXPORT_SYMBOL_GPL(rndis_get_next_response);
1051 
1052 static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
1053 {
1054 	rndis_resp_t *r;
1055 
1056 	/* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1057 	r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
1058 	if (!r) return NULL;
1059 
1060 	r->buf = (u8 *)(r + 1);
1061 	r->length = length;
1062 	r->send = 0;
1063 
1064 	spin_lock(&params->resp_lock);
1065 	list_add_tail(&r->list, &params->resp_queue);
1066 	spin_unlock(&params->resp_lock);
1067 	return r;
1068 }
1069 
1070 int rndis_rm_hdr(struct gether *port,
1071 			struct sk_buff *skb,
1072 			struct sk_buff_head *list)
1073 {
1074 	/* tmp points to a struct rndis_packet_msg_type */
1075 	__le32 *tmp = (void *)skb->data;
1076 
1077 	/* MessageType, MessageLength */
1078 	if (cpu_to_le32(RNDIS_MSG_PACKET)
1079 			!= get_unaligned(tmp++)) {
1080 		dev_kfree_skb_any(skb);
1081 		return -EINVAL;
1082 	}
1083 	tmp++;
1084 
1085 	/* DataOffset, DataLength */
1086 	if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1087 		dev_kfree_skb_any(skb);
1088 		return -EOVERFLOW;
1089 	}
1090 	skb_trim(skb, get_unaligned_le32(tmp++));
1091 
1092 	skb_queue_tail(list, skb);
1093 	return 0;
1094 }
1095 EXPORT_SYMBOL_GPL(rndis_rm_hdr);
1096 
1097 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1098 
1099 static int rndis_proc_show(struct seq_file *m, void *v)
1100 {
1101 	rndis_params *param = m->private;
1102 
1103 	seq_printf(m,
1104 			 "Config Nr. %d\n"
1105 			 "used      : %s\n"
1106 			 "state     : %s\n"
1107 			 "medium    : 0x%08X\n"
1108 			 "speed     : %u\n"
1109 			 "cable     : %s\n"
1110 			 "vendor ID : 0x%08X\n"
1111 			 "vendor    : %s\n",
1112 			 param->confignr, (param->used) ? "y" : "n",
1113 			 ({ char *s = "?";
1114 			 switch (param->state) {
1115 			 case RNDIS_UNINITIALIZED:
1116 				s = "RNDIS_UNINITIALIZED"; break;
1117 			 case RNDIS_INITIALIZED:
1118 				s = "RNDIS_INITIALIZED"; break;
1119 			 case RNDIS_DATA_INITIALIZED:
1120 				s = "RNDIS_DATA_INITIALIZED"; break;
1121 			} s; }),
1122 			 param->medium,
1123 			 (param->media_state) ? 0 : param->speed*100,
1124 			 (param->media_state) ? "disconnected" : "connected",
1125 			 param->vendorID, param->vendorDescr);
1126 	return 0;
1127 }
1128 
1129 static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1130 				size_t count, loff_t *ppos)
1131 {
1132 	rndis_params *p = pde_data(file_inode(file));
1133 	u32 speed = 0;
1134 	int i, fl_speed = 0;
1135 
1136 	for (i = 0; i < count; i++) {
1137 		char c;
1138 		if (get_user(c, buffer))
1139 			return -EFAULT;
1140 		switch (c) {
1141 		case '0':
1142 		case '1':
1143 		case '2':
1144 		case '3':
1145 		case '4':
1146 		case '5':
1147 		case '6':
1148 		case '7':
1149 		case '8':
1150 		case '9':
1151 			fl_speed = 1;
1152 			speed = speed * 10 + c - '0';
1153 			break;
1154 		case 'C':
1155 		case 'c':
1156 			rndis_signal_connect(p);
1157 			break;
1158 		case 'D':
1159 		case 'd':
1160 			rndis_signal_disconnect(p);
1161 			break;
1162 		default:
1163 			if (fl_speed) p->speed = speed;
1164 			else pr_debug("%c is not valid\n", c);
1165 			break;
1166 		}
1167 
1168 		buffer++;
1169 	}
1170 
1171 	return count;
1172 }
1173 
1174 static int rndis_proc_open(struct inode *inode, struct file *file)
1175 {
1176 	return single_open(file, rndis_proc_show, pde_data(inode));
1177 }
1178 
1179 static const struct proc_ops rndis_proc_ops = {
1180 	.proc_open	= rndis_proc_open,
1181 	.proc_read	= seq_read,
1182 	.proc_lseek	= seq_lseek,
1183 	.proc_release	= single_release,
1184 	.proc_write	= rndis_proc_write,
1185 };
1186 
1187 #define	NAME_TEMPLATE "driver/rndis-%03d"
1188 
1189 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1190