4 * Version: $Id: rndis.c,v 1.19 2004/03/25 21:33:46 robert Exp $
6 * Authors: Benedikt Spranger, Pengutronix
7 * Robert Schwebel, Pengutronix
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2, as published by the Free Software Foundation.
13 * This software was originally developed in conformance with
14 * Microsoft's Remote NDIS Specification License Agreement.
16 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
17 * Fixed message length bug in init_response
19 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
20 * Fixed rndis_rm_hdr length bug.
22 * Copyright (C) 2004 by David Brownell
23 * updates to merge with Linux 2.6, better match RNDIS spec
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/version.h>
32 #include <linux/init.h>
33 #include <linux/list.h>
34 #include <linux/proc_fs.h>
35 #include <linux/netdevice.h>
38 #include <asm/byteorder.h>
39 #include <asm/system.h>
40 #include <asm/unaligned.h>
50 /* The driver for your USB chip needs to support ep0 OUT to work with
51 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
53 * Windows hosts need an INF file like Documentation/usb/linux.inf
54 * and will be happier if you provide the host_addr module parameter.
58 #define DEBUG(str,args...) do { \
60 printk(KERN_DEBUG str , ## args ); \
62 static int rndis_debug = 0;
64 module_param (rndis_debug, int, 0);
65 MODULE_PARM_DESC (rndis_debug, "enable debugging");
70 #define DEBUG(str,args...) do{}while(0)
73 #define RNDIS_MAX_CONFIGS 1
76 static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
79 static const __le32 rndis_driver_version = __constant_cpu_to_le32 (1);
81 /* Function Prototypes */
82 static rndis_resp_t *rndis_add_response (int configNr, u32 length);
86 static const u32 oid_supported_list [] =
88 /* the general stuff */
89 OID_GEN_SUPPORTED_LIST,
90 OID_GEN_HARDWARE_STATUS,
91 OID_GEN_MEDIA_SUPPORTED,
93 OID_GEN_MAXIMUM_FRAME_SIZE,
95 OID_GEN_TRANSMIT_BLOCK_SIZE,
96 OID_GEN_RECEIVE_BLOCK_SIZE,
98 OID_GEN_VENDOR_DESCRIPTION,
99 OID_GEN_VENDOR_DRIVER_VERSION,
100 OID_GEN_CURRENT_PACKET_FILTER,
101 OID_GEN_MAXIMUM_TOTAL_SIZE,
102 OID_GEN_MEDIA_CONNECT_STATUS,
103 OID_GEN_PHYSICAL_MEDIUM,
105 OID_GEN_RNDIS_CONFIG_PARAMETER,
108 /* the statistical stuff */
113 OID_GEN_RCV_NO_BUFFER,
114 #ifdef RNDIS_OPTIONAL_STATS
115 OID_GEN_DIRECTED_BYTES_XMIT,
116 OID_GEN_DIRECTED_FRAMES_XMIT,
117 OID_GEN_MULTICAST_BYTES_XMIT,
118 OID_GEN_MULTICAST_FRAMES_XMIT,
119 OID_GEN_BROADCAST_BYTES_XMIT,
120 OID_GEN_BROADCAST_FRAMES_XMIT,
121 OID_GEN_DIRECTED_BYTES_RCV,
122 OID_GEN_DIRECTED_FRAMES_RCV,
123 OID_GEN_MULTICAST_BYTES_RCV,
124 OID_GEN_MULTICAST_FRAMES_RCV,
125 OID_GEN_BROADCAST_BYTES_RCV,
126 OID_GEN_BROADCAST_FRAMES_RCV,
127 OID_GEN_RCV_CRC_ERROR,
128 OID_GEN_TRANSMIT_QUEUE_LENGTH,
129 #endif /* RNDIS_OPTIONAL_STATS */
131 /* mandatory 802.3 */
132 /* the general stuff */
133 OID_802_3_PERMANENT_ADDRESS,
134 OID_802_3_CURRENT_ADDRESS,
135 OID_802_3_MULTICAST_LIST,
136 OID_802_3_MAC_OPTIONS,
137 OID_802_3_MAXIMUM_LIST_SIZE,
139 /* the statistical stuff */
140 OID_802_3_RCV_ERROR_ALIGNMENT,
141 OID_802_3_XMIT_ONE_COLLISION,
142 OID_802_3_XMIT_MORE_COLLISIONS,
143 #ifdef RNDIS_OPTIONAL_STATS
144 OID_802_3_XMIT_DEFERRED,
145 OID_802_3_XMIT_MAX_COLLISIONS,
146 OID_802_3_RCV_OVERRUN,
147 OID_802_3_XMIT_UNDERRUN,
148 OID_802_3_XMIT_HEARTBEAT_FAILURE,
149 OID_802_3_XMIT_TIMES_CRS_LOST,
150 OID_802_3_XMIT_LATE_COLLISIONS,
151 #endif /* RNDIS_OPTIONAL_STATS */
154 /* PM and wakeup are mandatory for USB: */
156 /* power management */
157 OID_PNP_CAPABILITIES,
163 OID_PNP_ENABLE_WAKE_UP,
164 OID_PNP_ADD_WAKE_UP_PATTERN,
165 OID_PNP_REMOVE_WAKE_UP_PATTERN,
166 #endif /* RNDIS_WAKEUP */
167 #endif /* RNDIS_PM */
173 gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
176 int retval = -ENOTSUPP;
177 u32 length = 4; /* usually */
180 rndis_query_cmplt_type *resp;
182 if (!r) return -ENOMEM;
183 resp = (rndis_query_cmplt_type *) r->buf;
185 if (!resp) return -ENOMEM;
187 if (buf_len && rndis_debug > 1) {
188 DEBUG("query OID %08x value, len %d:\n", OID, buf_len);
189 for (i = 0; i < buf_len; i += 16) {
190 DEBUG ("%03d: %08x %08x %08x %08x\n", i,
191 le32_to_cpup((__le32 *)&buf[i]),
192 le32_to_cpup((__le32 *)&buf[i + 4]),
193 le32_to_cpup((__le32 *)&buf[i + 8]),
194 le32_to_cpup((__le32 *)&buf[i + 12]));
198 /* response goes here, right after the header */
199 outbuf = (__le32 *) &resp[1];
200 resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
204 /* general oids (table 4-1) */
207 case OID_GEN_SUPPORTED_LIST:
208 DEBUG ("%s: OID_GEN_SUPPORTED_LIST\n", __FUNCTION__);
209 length = sizeof (oid_supported_list);
210 count = length / sizeof (u32);
211 for (i = 0; i < count; i++)
212 outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
217 case OID_GEN_HARDWARE_STATUS:
218 DEBUG("%s: OID_GEN_HARDWARE_STATUS\n", __FUNCTION__);
220 * Hardware must be ready to receive high level protocols.
222 * reddite ergo quae sunt Caesaris Caesari
223 * et quae sunt Dei Deo!
225 *outbuf = __constant_cpu_to_le32 (0);
230 case OID_GEN_MEDIA_SUPPORTED:
231 DEBUG("%s: OID_GEN_MEDIA_SUPPORTED\n", __FUNCTION__);
232 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
237 case OID_GEN_MEDIA_IN_USE:
238 DEBUG("%s: OID_GEN_MEDIA_IN_USE\n", __FUNCTION__);
239 /* one medium, one transport... (maybe you do it better) */
240 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
245 case OID_GEN_MAXIMUM_FRAME_SIZE:
246 DEBUG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __FUNCTION__);
247 if (rndis_per_dev_params [configNr].dev) {
248 *outbuf = cpu_to_le32 (
249 rndis_per_dev_params [configNr].dev->mtu);
255 case OID_GEN_LINK_SPEED:
257 DEBUG("%s: OID_GEN_LINK_SPEED\n", __FUNCTION__);
258 if (rndis_per_dev_params [configNr].media_state
259 == NDIS_MEDIA_STATE_DISCONNECTED)
260 *outbuf = __constant_cpu_to_le32 (0);
262 *outbuf = cpu_to_le32 (
263 rndis_per_dev_params [configNr].speed);
268 case OID_GEN_TRANSMIT_BLOCK_SIZE:
269 DEBUG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __FUNCTION__);
270 if (rndis_per_dev_params [configNr].dev) {
271 *outbuf = cpu_to_le32 (
272 rndis_per_dev_params [configNr].dev->mtu);
278 case OID_GEN_RECEIVE_BLOCK_SIZE:
279 DEBUG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __FUNCTION__);
280 if (rndis_per_dev_params [configNr].dev) {
281 *outbuf = cpu_to_le32 (
282 rndis_per_dev_params [configNr].dev->mtu);
288 case OID_GEN_VENDOR_ID:
289 DEBUG("%s: OID_GEN_VENDOR_ID\n", __FUNCTION__);
290 *outbuf = cpu_to_le32 (
291 rndis_per_dev_params [configNr].vendorID);
296 case OID_GEN_VENDOR_DESCRIPTION:
297 DEBUG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __FUNCTION__);
298 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
300 rndis_per_dev_params [configNr].vendorDescr, length);
304 case OID_GEN_VENDOR_DRIVER_VERSION:
305 DEBUG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __FUNCTION__);
307 *outbuf = rndis_driver_version;
312 case OID_GEN_CURRENT_PACKET_FILTER:
313 DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __FUNCTION__);
314 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
319 case OID_GEN_MAXIMUM_TOTAL_SIZE:
320 DEBUG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __FUNCTION__);
321 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
326 case OID_GEN_MEDIA_CONNECT_STATUS:
328 DEBUG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __FUNCTION__);
329 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
334 case OID_GEN_PHYSICAL_MEDIUM:
335 DEBUG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __FUNCTION__);
336 *outbuf = __constant_cpu_to_le32 (0);
340 /* The RNDIS specification is incomplete/wrong. Some versions
341 * of MS-Windows expect OIDs that aren't specified there. Other
342 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
344 case OID_GEN_MAC_OPTIONS: /* from WinME */
345 DEBUG("%s: OID_GEN_MAC_OPTIONS\n", __FUNCTION__);
346 *outbuf = __constant_cpu_to_le32(
347 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
348 | NDIS_MAC_OPTION_FULL_DUPLEX);
352 /* statistics OIDs (table 4-2) */
355 case OID_GEN_XMIT_OK:
357 DEBUG("%s: OID_GEN_XMIT_OK\n", __FUNCTION__);
358 if (rndis_per_dev_params [configNr].stats) {
359 *outbuf = cpu_to_le32 (
360 rndis_per_dev_params [configNr].stats->tx_packets -
361 rndis_per_dev_params [configNr].stats->tx_errors -
362 rndis_per_dev_params [configNr].stats->tx_dropped);
370 DEBUG("%s: OID_GEN_RCV_OK\n", __FUNCTION__);
371 if (rndis_per_dev_params [configNr].stats) {
372 *outbuf = cpu_to_le32 (
373 rndis_per_dev_params [configNr].stats->rx_packets -
374 rndis_per_dev_params [configNr].stats->rx_errors -
375 rndis_per_dev_params [configNr].stats->rx_dropped);
381 case OID_GEN_XMIT_ERROR:
383 DEBUG("%s: OID_GEN_XMIT_ERROR\n", __FUNCTION__);
384 if (rndis_per_dev_params [configNr].stats) {
385 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
392 case OID_GEN_RCV_ERROR:
394 DEBUG("%s: OID_GEN_RCV_ERROR\n", __FUNCTION__);
395 if (rndis_per_dev_params [configNr].stats) {
396 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
403 case OID_GEN_RCV_NO_BUFFER:
404 DEBUG("%s: OID_GEN_RCV_NO_BUFFER\n", __FUNCTION__);
405 if (rndis_per_dev_params [configNr].stats) {
406 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
412 #ifdef RNDIS_OPTIONAL_STATS
413 case OID_GEN_DIRECTED_BYTES_XMIT:
414 DEBUG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __FUNCTION__);
416 * Aunt Tilly's size of shoes
417 * minus antarctica count of penguins
418 * divided by weight of Alpha Centauri
420 if (rndis_per_dev_params [configNr].stats) {
421 *outbuf = cpu_to_le32 (
422 (rndis_per_dev_params [configNr]
424 rndis_per_dev_params [configNr]
426 rndis_per_dev_params [configNr]
433 case OID_GEN_DIRECTED_FRAMES_XMIT:
434 DEBUG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __FUNCTION__);
436 if (rndis_per_dev_params [configNr].stats) {
437 *outbuf = cpu_to_le32 (
438 (rndis_per_dev_params [configNr]
440 rndis_per_dev_params [configNr]
442 rndis_per_dev_params [configNr]
449 case OID_GEN_MULTICAST_BYTES_XMIT:
450 DEBUG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __FUNCTION__);
451 if (rndis_per_dev_params [configNr].stats) {
452 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
453 .stats->multicast*1234);
458 case OID_GEN_MULTICAST_FRAMES_XMIT:
459 DEBUG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __FUNCTION__);
460 if (rndis_per_dev_params [configNr].stats) {
461 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
467 case OID_GEN_BROADCAST_BYTES_XMIT:
468 DEBUG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __FUNCTION__);
469 if (rndis_per_dev_params [configNr].stats) {
470 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
471 .stats->tx_packets/42*255);
476 case OID_GEN_BROADCAST_FRAMES_XMIT:
477 DEBUG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __FUNCTION__);
478 if (rndis_per_dev_params [configNr].stats) {
479 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
480 .stats->tx_packets/42);
485 case OID_GEN_DIRECTED_BYTES_RCV:
486 DEBUG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __FUNCTION__);
487 *outbuf = __constant_cpu_to_le32 (0);
491 case OID_GEN_DIRECTED_FRAMES_RCV:
492 DEBUG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __FUNCTION__);
493 *outbuf = __constant_cpu_to_le32 (0);
497 case OID_GEN_MULTICAST_BYTES_RCV:
498 DEBUG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __FUNCTION__);
499 if (rndis_per_dev_params [configNr].stats) {
500 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
501 .stats->multicast * 1111);
506 case OID_GEN_MULTICAST_FRAMES_RCV:
507 DEBUG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __FUNCTION__);
508 if (rndis_per_dev_params [configNr].stats) {
509 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
515 case OID_GEN_BROADCAST_BYTES_RCV:
516 DEBUG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __FUNCTION__);
517 if (rndis_per_dev_params [configNr].stats) {
518 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
519 .stats->rx_packets/42*255);
524 case OID_GEN_BROADCAST_FRAMES_RCV:
525 DEBUG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __FUNCTION__);
526 if (rndis_per_dev_params [configNr].stats) {
527 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
528 .stats->rx_packets/42);
533 case OID_GEN_RCV_CRC_ERROR:
534 DEBUG("%s: OID_GEN_RCV_CRC_ERROR\n", __FUNCTION__);
535 if (rndis_per_dev_params [configNr].stats) {
536 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
537 .stats->rx_crc_errors);
542 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
543 DEBUG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __FUNCTION__);
544 *outbuf = __constant_cpu_to_le32 (0);
547 #endif /* RNDIS_OPTIONAL_STATS */
549 /* ieee802.3 OIDs (table 4-3) */
552 case OID_802_3_PERMANENT_ADDRESS:
553 DEBUG("%s: OID_802_3_PERMANENT_ADDRESS\n", __FUNCTION__);
554 if (rndis_per_dev_params [configNr].dev) {
557 rndis_per_dev_params [configNr].host_mac,
564 case OID_802_3_CURRENT_ADDRESS:
565 DEBUG("%s: OID_802_3_CURRENT_ADDRESS\n", __FUNCTION__);
566 if (rndis_per_dev_params [configNr].dev) {
569 rndis_per_dev_params [configNr].host_mac,
576 case OID_802_3_MULTICAST_LIST:
577 DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
578 /* Multicast base address only */
579 *outbuf = __constant_cpu_to_le32 (0xE0000000);
584 case OID_802_3_MAXIMUM_LIST_SIZE:
585 DEBUG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __FUNCTION__);
586 /* Multicast base address only */
587 *outbuf = __constant_cpu_to_le32 (1);
591 case OID_802_3_MAC_OPTIONS:
592 DEBUG("%s: OID_802_3_MAC_OPTIONS\n", __FUNCTION__);
595 /* ieee802.3 statistics OIDs (table 4-4) */
598 case OID_802_3_RCV_ERROR_ALIGNMENT:
599 DEBUG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __FUNCTION__);
600 if (rndis_per_dev_params [configNr].stats) {
601 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
602 .stats->rx_frame_errors);
608 case OID_802_3_XMIT_ONE_COLLISION:
609 DEBUG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __FUNCTION__);
610 *outbuf = __constant_cpu_to_le32 (0);
615 case OID_802_3_XMIT_MORE_COLLISIONS:
616 DEBUG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __FUNCTION__);
617 *outbuf = __constant_cpu_to_le32 (0);
621 #ifdef RNDIS_OPTIONAL_STATS
622 case OID_802_3_XMIT_DEFERRED:
623 DEBUG("%s: OID_802_3_XMIT_DEFERRED\n", __FUNCTION__);
627 case OID_802_3_XMIT_MAX_COLLISIONS:
628 DEBUG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __FUNCTION__);
632 case OID_802_3_RCV_OVERRUN:
633 DEBUG("%s: OID_802_3_RCV_OVERRUN\n", __FUNCTION__);
637 case OID_802_3_XMIT_UNDERRUN:
638 DEBUG("%s: OID_802_3_XMIT_UNDERRUN\n", __FUNCTION__);
642 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
643 DEBUG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __FUNCTION__);
647 case OID_802_3_XMIT_TIMES_CRS_LOST:
648 DEBUG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __FUNCTION__);
652 case OID_802_3_XMIT_LATE_COLLISIONS:
653 DEBUG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __FUNCTION__);
656 #endif /* RNDIS_OPTIONAL_STATS */
659 /* power management OIDs (table 4-5) */
660 case OID_PNP_CAPABILITIES:
661 DEBUG("%s: OID_PNP_CAPABILITIES\n", __FUNCTION__);
663 /* for now, no wakeup capabilities */
664 length = sizeof (struct NDIS_PNP_CAPABILITIES);
665 memset(outbuf, 0, length);
668 case OID_PNP_QUERY_POWER:
669 DEBUG("%s: OID_PNP_QUERY_POWER D%d\n", __FUNCTION__,
670 le32_to_cpup((__le32 *) buf) - 1);
671 /* only suspend is a real power state, and
672 * it can't be entered by OID_PNP_SET_POWER...
680 printk (KERN_WARNING "%s: query unknown OID 0x%08X\n",
686 resp->InformationBufferLength = cpu_to_le32 (length);
687 r->length = length + sizeof *resp;
688 resp->MessageLength = cpu_to_le32 (r->length);
692 static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
695 rndis_set_cmplt_type *resp;
696 int i, retval = -ENOTSUPP;
697 struct rndis_params *params;
701 resp = (rndis_set_cmplt_type *) r->buf;
705 if (buf_len && rndis_debug > 1) {
706 DEBUG("set OID %08x value, len %d:\n", OID, buf_len);
707 for (i = 0; i < buf_len; i += 16) {
708 DEBUG ("%03d: %08x %08x %08x %08x\n", i,
709 le32_to_cpup((__le32 *)&buf[i]),
710 le32_to_cpup((__le32 *)&buf[i + 4]),
711 le32_to_cpup((__le32 *)&buf[i + 8]),
712 le32_to_cpup((__le32 *)&buf[i + 12]));
716 params = &rndis_per_dev_params [configNr];
718 case OID_GEN_CURRENT_PACKET_FILTER:
720 /* these NDIS_PACKET_TYPE_* bitflags are shared with
721 * cdc_filter; it's not RNDIS-specific
722 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
723 * PROMISCUOUS, DIRECTED,
724 * MULTICAST, ALL_MULTICAST, BROADCAST
726 *params->filter = (u16) le32_to_cpup((__le32 *)buf);
727 DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
728 __FUNCTION__, *params->filter);
730 /* this call has a significant side effect: it's
731 * what makes the packet flow start and stop, like
732 * activating the CDC Ethernet altsetting.
738 if (*params->filter) {
739 params->state = RNDIS_DATA_INITIALIZED;
740 netif_carrier_on(params->dev);
741 if (netif_running(params->dev))
742 netif_wake_queue (params->dev);
744 params->state = RNDIS_INITIALIZED;
745 netif_carrier_off (params->dev);
746 netif_stop_queue (params->dev);
750 case OID_802_3_MULTICAST_LIST:
751 /* I think we can ignore this */
752 DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
756 case OID_GEN_RNDIS_CONFIG_PARAMETER:
758 struct rndis_config_parameter *param;
759 param = (struct rndis_config_parameter *) buf;
760 DEBUG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
762 min(cpu_to_le32(param->ParameterNameLength),80),
763 buf + param->ParameterNameOffset);
770 case OID_PNP_SET_POWER:
771 /* The only real power state is USB suspend, and RNDIS requests
772 * can't enter it; this one isn't really about power. After
773 * resuming, Windows forces a reset, and then SET_POWER D0.
774 * FIXME ... then things go batty; Windows wedges itself.
776 i = le32_to_cpup((__force __le32 *)buf);
777 DEBUG("%s: OID_PNP_SET_POWER D%d\n", __FUNCTION__, i - 1);
779 case NdisDeviceStateD0:
780 *params->filter = params->saved_filter;
781 goto update_linkstate;
782 case NdisDeviceStateD3:
783 case NdisDeviceStateD2:
784 case NdisDeviceStateD1:
785 params->saved_filter = *params->filter;
792 // no wakeup support advertised, so wakeup OIDs always fail:
793 // - OID_PNP_ENABLE_WAKE_UP
794 // - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
797 #endif /* RNDIS_PM */
800 printk (KERN_WARNING "%s: set unknown OID 0x%08X, size %d\n",
801 __FUNCTION__, OID, buf_len);
811 static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
813 rndis_init_cmplt_type *resp;
816 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
818 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
821 resp = (rndis_init_cmplt_type *) r->buf;
823 resp->MessageType = __constant_cpu_to_le32 (
824 REMOTE_NDIS_INITIALIZE_CMPLT);
825 resp->MessageLength = __constant_cpu_to_le32 (52);
826 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
827 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
828 resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
829 resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
830 resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
831 resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
832 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
833 resp->MaxTransferSize = cpu_to_le32 (
834 rndis_per_dev_params [configNr].dev->mtu
835 + sizeof (struct ethhdr)
836 + sizeof (struct rndis_packet_msg_type)
838 resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
839 resp->AFListOffset = __constant_cpu_to_le32 (0);
840 resp->AFListSize = __constant_cpu_to_le32 (0);
842 if (rndis_per_dev_params [configNr].ack)
843 rndis_per_dev_params [configNr].ack (
844 rndis_per_dev_params [configNr].dev);
849 static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
851 rndis_query_cmplt_type *resp;
854 // DEBUG("%s: OID = %08X\n", __FUNCTION__, cpu_to_le32(buf->OID));
855 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
858 * we need more memory:
859 * oid_supported_list is the largest answer
861 r = rndis_add_response (configNr, sizeof (oid_supported_list));
864 resp = (rndis_query_cmplt_type *) r->buf;
866 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
867 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
869 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
870 le32_to_cpu(buf->InformationBufferOffset)
872 le32_to_cpu(buf->InformationBufferLength),
874 /* OID not supported */
875 resp->Status = __constant_cpu_to_le32 (
876 RNDIS_STATUS_NOT_SUPPORTED);
877 resp->MessageLength = __constant_cpu_to_le32 (sizeof *resp);
878 resp->InformationBufferLength = __constant_cpu_to_le32 (0);
879 resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
881 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
883 if (rndis_per_dev_params [configNr].ack)
884 rndis_per_dev_params [configNr].ack (
885 rndis_per_dev_params [configNr].dev);
889 static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
891 u32 BufLength, BufOffset;
892 rndis_set_cmplt_type *resp;
895 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
898 resp = (rndis_set_cmplt_type *) r->buf;
900 BufLength = le32_to_cpu (buf->InformationBufferLength);
901 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
904 DEBUG("%s: Length: %d\n", __FUNCTION__, BufLength);
905 DEBUG("%s: Offset: %d\n", __FUNCTION__, BufOffset);
906 DEBUG("%s: InfoBuffer: ", __FUNCTION__);
908 for (i = 0; i < BufLength; i++) {
909 DEBUG ("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
915 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
916 resp->MessageLength = __constant_cpu_to_le32 (16);
917 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
918 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
919 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
920 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
921 else resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
923 if (rndis_per_dev_params [configNr].ack)
924 rndis_per_dev_params [configNr].ack (
925 rndis_per_dev_params [configNr].dev);
930 static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
932 rndis_reset_cmplt_type *resp;
935 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
938 resp = (rndis_reset_cmplt_type *) r->buf;
940 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
941 resp->MessageLength = __constant_cpu_to_le32 (16);
942 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
943 /* resent information */
944 resp->AddressingReset = __constant_cpu_to_le32 (1);
946 if (rndis_per_dev_params [configNr].ack)
947 rndis_per_dev_params [configNr].ack (
948 rndis_per_dev_params [configNr].dev);
953 static int rndis_keepalive_response (int configNr,
954 rndis_keepalive_msg_type *buf)
956 rndis_keepalive_cmplt_type *resp;
959 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
961 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
964 resp = (rndis_keepalive_cmplt_type *) r->buf;
966 resp->MessageType = __constant_cpu_to_le32 (
967 REMOTE_NDIS_KEEPALIVE_CMPLT);
968 resp->MessageLength = __constant_cpu_to_le32 (16);
969 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
970 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
972 if (rndis_per_dev_params [configNr].ack)
973 rndis_per_dev_params [configNr].ack (
974 rndis_per_dev_params [configNr].dev);
981 * Device to Host Comunication
983 static int rndis_indicate_status_msg (int configNr, u32 status)
985 rndis_indicate_status_msg_type *resp;
988 if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED)
991 r = rndis_add_response (configNr,
992 sizeof (rndis_indicate_status_msg_type));
995 resp = (rndis_indicate_status_msg_type *) r->buf;
997 resp->MessageType = __constant_cpu_to_le32 (
998 REMOTE_NDIS_INDICATE_STATUS_MSG);
999 resp->MessageLength = __constant_cpu_to_le32 (20);
1000 resp->Status = cpu_to_le32 (status);
1001 resp->StatusBufferLength = __constant_cpu_to_le32 (0);
1002 resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
1004 if (rndis_per_dev_params [configNr].ack)
1005 rndis_per_dev_params [configNr].ack (
1006 rndis_per_dev_params [configNr].dev);
1010 int rndis_signal_connect (int configNr)
1012 rndis_per_dev_params [configNr].media_state
1013 = NDIS_MEDIA_STATE_CONNECTED;
1014 return rndis_indicate_status_msg (configNr,
1015 RNDIS_STATUS_MEDIA_CONNECT);
1018 int rndis_signal_disconnect (int configNr)
1020 rndis_per_dev_params [configNr].media_state
1021 = NDIS_MEDIA_STATE_DISCONNECTED;
1022 return rndis_indicate_status_msg (configNr,
1023 RNDIS_STATUS_MEDIA_DISCONNECT);
1026 void rndis_uninit (int configNr)
1028 if (configNr >= RNDIS_MAX_CONFIGS)
1030 rndis_per_dev_params [configNr].used = 0;
1031 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
1035 void rndis_set_host_mac (int configNr, const u8 *addr)
1037 rndis_per_dev_params [configNr].host_mac = addr;
1043 int rndis_msg_parser (u8 configNr, u8 *buf)
1045 u32 MsgType, MsgLength;
1047 struct rndis_params *params;
1052 tmp = (__le32 *) buf;
1053 MsgType = le32_to_cpup(tmp++);
1054 MsgLength = le32_to_cpup(tmp++);
1056 if (configNr >= RNDIS_MAX_CONFIGS)
1058 params = &rndis_per_dev_params [configNr];
1060 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1061 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1062 * and normal HC level polling to see if there's any IN traffic.
1065 /* For USB: responses may take up to 10 seconds */
1067 case REMOTE_NDIS_INITIALIZE_MSG:
1068 DEBUG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
1070 params->state = RNDIS_INITIALIZED;
1071 return rndis_init_response (configNr,
1072 (rndis_init_msg_type *) buf);
1074 case REMOTE_NDIS_HALT_MSG:
1075 DEBUG("%s: REMOTE_NDIS_HALT_MSG\n",
1077 params->state = RNDIS_UNINITIALIZED;
1079 netif_carrier_off (params->dev);
1080 netif_stop_queue (params->dev);
1084 case REMOTE_NDIS_QUERY_MSG:
1085 return rndis_query_response (configNr,
1086 (rndis_query_msg_type *) buf);
1088 case REMOTE_NDIS_SET_MSG:
1089 return rndis_set_response (configNr,
1090 (rndis_set_msg_type *) buf);
1092 case REMOTE_NDIS_RESET_MSG:
1093 DEBUG("%s: REMOTE_NDIS_RESET_MSG\n",
1095 return rndis_reset_response (configNr,
1096 (rndis_reset_msg_type *) buf);
1098 case REMOTE_NDIS_KEEPALIVE_MSG:
1099 /* For USB: host does this every 5 seconds */
1100 if (rndis_debug > 1)
1101 DEBUG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
1103 return rndis_keepalive_response (configNr,
1104 (rndis_keepalive_msg_type *)
1108 /* At least Windows XP emits some undefined RNDIS messages.
1109 * In one case those messages seemed to relate to the host
1110 * suspending itself.
1112 printk (KERN_WARNING
1113 "%s: unknown RNDIS message 0x%08X len %d\n",
1114 __FUNCTION__ , MsgType, MsgLength);
1117 for (i = 0; i < MsgLength; i += 16) {
1119 " %02x %02x %02x %02x"
1120 " %02x %02x %02x %02x"
1121 " %02x %02x %02x %02x"
1122 " %02x %02x %02x %02x"
1127 buf[i+4], buf [i+5],
1129 buf[i+8], buf [i+9],
1130 buf[i+10], buf[i+11],
1131 buf[i+12], buf [i+13],
1132 buf[i+14], buf[i+15]);
1141 int rndis_register (int (* rndis_control_ack) (struct net_device *))
1145 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1146 if (!rndis_per_dev_params [i].used) {
1147 rndis_per_dev_params [i].used = 1;
1148 rndis_per_dev_params [i].ack = rndis_control_ack;
1149 DEBUG("%s: configNr = %d\n", __FUNCTION__, i);
1158 void rndis_deregister (int configNr)
1160 DEBUG("%s: \n", __FUNCTION__ );
1162 if (configNr >= RNDIS_MAX_CONFIGS) return;
1163 rndis_per_dev_params [configNr].used = 0;
1168 int rndis_set_param_dev (u8 configNr, struct net_device *dev,
1169 struct net_device_stats *stats,
1172 DEBUG("%s:\n", __FUNCTION__ );
1173 if (!dev || !stats) return -1;
1174 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1176 rndis_per_dev_params [configNr].dev = dev;
1177 rndis_per_dev_params [configNr].stats = stats;
1178 rndis_per_dev_params [configNr].filter = cdc_filter;
1183 int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
1185 DEBUG("%s:\n", __FUNCTION__ );
1186 if (!vendorDescr) return -1;
1187 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1189 rndis_per_dev_params [configNr].vendorID = vendorID;
1190 rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
1195 int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
1197 DEBUG("%s: %u %u\n", __FUNCTION__, medium, speed);
1198 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1200 rndis_per_dev_params [configNr].medium = medium;
1201 rndis_per_dev_params [configNr].speed = speed;
1206 void rndis_add_hdr (struct sk_buff *skb)
1208 struct rndis_packet_msg_type *header;
1212 header = (void *) skb_push (skb, sizeof *header);
1213 memset (header, 0, sizeof *header);
1214 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1215 header->MessageLength = cpu_to_le32(skb->len);
1216 header->DataOffset = __constant_cpu_to_le32 (36);
1217 header->DataLength = cpu_to_le32(skb->len - sizeof *header);
1220 void rndis_free_response (int configNr, u8 *buf)
1223 struct list_head *act, *tmp;
1225 list_for_each_safe (act, tmp,
1226 &(rndis_per_dev_params [configNr].resp_queue))
1228 r = list_entry (act, rndis_resp_t, list);
1229 if (r && r->buf == buf) {
1230 list_del (&r->list);
1236 u8 *rndis_get_next_response (int configNr, u32 *length)
1239 struct list_head *act, *tmp;
1241 if (!length) return NULL;
1243 list_for_each_safe (act, tmp,
1244 &(rndis_per_dev_params [configNr].resp_queue))
1246 r = list_entry (act, rndis_resp_t, list);
1249 *length = r->length;
1257 static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1261 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1262 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1263 if (!r) return NULL;
1265 r->buf = (u8 *) (r + 1);
1269 list_add_tail (&r->list,
1270 &(rndis_per_dev_params [configNr].resp_queue));
1274 int rndis_rm_hdr(struct sk_buff *skb)
1276 /* tmp points to a struct rndis_packet_msg_type */
1277 __le32 *tmp = (void *) skb->data;
1279 /* MessageType, MessageLength */
1280 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1281 != get_unaligned(tmp++))
1285 /* DataOffset, DataLength */
1286 if (!skb_pull(skb, le32_to_cpu(get_unaligned(tmp++))
1287 + 8 /* offset of DataOffset */))
1289 skb_trim(skb, le32_to_cpu(get_unaligned(tmp++)));
1294 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1296 static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof,
1301 rndis_params *param = (rndis_params *) data;
1303 out += snprintf (out, count,
1310 "vendor ID : 0x%08X\n"
1312 param->confignr, (param->used) ? "y" : "n",
1314 switch (param->state) {
1315 case RNDIS_UNINITIALIZED:
1316 s = "RNDIS_UNINITIALIZED"; break;
1317 case RNDIS_INITIALIZED:
1318 s = "RNDIS_INITIALIZED"; break;
1319 case RNDIS_DATA_INITIALIZED:
1320 s = "RNDIS_DATA_INITIALIZED"; break;
1323 (param->media_state) ? 0 : param->speed*100,
1324 (param->media_state) ? "disconnected" : "connected",
1325 param->vendorID, param->vendorDescr);
1337 *start = page + off;
1341 static int rndis_proc_write (struct file *file, const char __user *buffer,
1342 unsigned long count, void *data)
1344 rndis_params *p = data;
1346 int i, fl_speed = 0;
1348 for (i = 0; i < count; i++) {
1350 if (get_user(c, buffer))
1364 speed = speed*10 + c - '0';
1368 rndis_signal_connect (p->confignr);
1372 rndis_signal_disconnect(p->confignr);
1375 if (fl_speed) p->speed = speed;
1376 else DEBUG ("%c is not valid\n", c);
1386 #define NAME_TEMPLATE "driver/rndis-%03d"
1388 static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1390 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1393 int __init rndis_init (void)
1397 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1398 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1401 sprintf (name, NAME_TEMPLATE, i);
1402 if (!(rndis_connect_state [i]
1403 = create_proc_entry (name, 0660, NULL)))
1405 DEBUG ("%s :remove entries", __FUNCTION__);
1407 sprintf (name, NAME_TEMPLATE, --i);
1408 remove_proc_entry (name, NULL);
1414 rndis_connect_state [i]->nlink = 1;
1415 rndis_connect_state [i]->write_proc = rndis_proc_write;
1416 rndis_connect_state [i]->read_proc = rndis_proc_read;
1417 rndis_connect_state [i]->data = (void *)
1418 (rndis_per_dev_params + i);
1420 rndis_per_dev_params [i].confignr = i;
1421 rndis_per_dev_params [i].used = 0;
1422 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1423 rndis_per_dev_params [i].media_state
1424 = NDIS_MEDIA_STATE_DISCONNECTED;
1425 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1431 void rndis_exit (void)
1433 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1437 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1438 sprintf (name, NAME_TEMPLATE, i);
1439 remove_proc_entry (name, NULL);