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