1 // SPDX-License-Identifier: GPL-2.0
5 * Authors: Benedikt Spranger, Pengutronix
6 * Robert Schwebel, Pengutronix
8 * This software was originally developed in conformance with
9 * Microsoft's Remote NDIS Specification License Agreement.
11 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
12 * Fixed message length bug in init_response
14 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
15 * Fixed rndis_rm_hdr length bug.
17 * Copyright (C) 2004 by David Brownell
18 * updates to merge with Linux 2.6, better match RNDIS spec
25 #include <linux/types.h>
26 #include <linux/list.h>
27 #include <linux/netdevice.h>
29 #include <asm/byteorder.h>
30 #include <asm/unaligned.h>
31 #include <linux/errno.h>
40 * The driver for your USB chip needs to support ep0 OUT to work with
41 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
43 * Windows hosts need an INF file like Documentation/usb/linux.inf
44 * and will be happier if you provide the host_addr module parameter.
47 #define RNDIS_MAX_CONFIGS 1
49 static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
52 static const __le32 rndis_driver_version = __constant_cpu_to_le32(1);
54 /* Function Prototypes */
55 static rndis_resp_t *rndis_add_response(int configNr, u32 length);
59 static const u32 oid_supported_list[] = {
60 /* the general stuff */
61 OID_GEN_SUPPORTED_LIST,
62 OID_GEN_HARDWARE_STATUS,
63 OID_GEN_MEDIA_SUPPORTED,
65 OID_GEN_MAXIMUM_FRAME_SIZE,
67 OID_GEN_TRANSMIT_BLOCK_SIZE,
68 OID_GEN_RECEIVE_BLOCK_SIZE,
70 OID_GEN_VENDOR_DESCRIPTION,
71 OID_GEN_VENDOR_DRIVER_VERSION,
72 OID_GEN_CURRENT_PACKET_FILTER,
73 OID_GEN_MAXIMUM_TOTAL_SIZE,
74 OID_GEN_MEDIA_CONNECT_STATUS,
75 OID_GEN_PHYSICAL_MEDIUM,
77 OID_GEN_RNDIS_CONFIG_PARAMETER,
80 /* the statistical stuff */
85 OID_GEN_RCV_NO_BUFFER,
86 #ifdef RNDIS_OPTIONAL_STATS
87 OID_GEN_DIRECTED_BYTES_XMIT,
88 OID_GEN_DIRECTED_FRAMES_XMIT,
89 OID_GEN_MULTICAST_BYTES_XMIT,
90 OID_GEN_MULTICAST_FRAMES_XMIT,
91 OID_GEN_BROADCAST_BYTES_XMIT,
92 OID_GEN_BROADCAST_FRAMES_XMIT,
93 OID_GEN_DIRECTED_BYTES_RCV,
94 OID_GEN_DIRECTED_FRAMES_RCV,
95 OID_GEN_MULTICAST_BYTES_RCV,
96 OID_GEN_MULTICAST_FRAMES_RCV,
97 OID_GEN_BROADCAST_BYTES_RCV,
98 OID_GEN_BROADCAST_FRAMES_RCV,
99 OID_GEN_RCV_CRC_ERROR,
100 OID_GEN_TRANSMIT_QUEUE_LENGTH,
101 #endif /* RNDIS_OPTIONAL_STATS */
103 /* mandatory 802.3 */
104 /* the general stuff */
105 OID_802_3_PERMANENT_ADDRESS,
106 OID_802_3_CURRENT_ADDRESS,
107 OID_802_3_MULTICAST_LIST,
108 OID_802_3_MAC_OPTIONS,
109 OID_802_3_MAXIMUM_LIST_SIZE,
111 /* the statistical stuff */
112 OID_802_3_RCV_ERROR_ALIGNMENT,
113 OID_802_3_XMIT_ONE_COLLISION,
114 OID_802_3_XMIT_MORE_COLLISIONS,
115 #ifdef RNDIS_OPTIONAL_STATS
116 OID_802_3_XMIT_DEFERRED,
117 OID_802_3_XMIT_MAX_COLLISIONS,
118 OID_802_3_RCV_OVERRUN,
119 OID_802_3_XMIT_UNDERRUN,
120 OID_802_3_XMIT_HEARTBEAT_FAILURE,
121 OID_802_3_XMIT_TIMES_CRS_LOST,
122 OID_802_3_XMIT_LATE_COLLISIONS,
123 #endif /* RNDIS_OPTIONAL_STATS */
126 /* PM and wakeup are mandatory for USB: */
128 /* power management */
129 OID_PNP_CAPABILITIES,
135 OID_PNP_ENABLE_WAKE_UP,
136 OID_PNP_ADD_WAKE_UP_PATTERN,
137 OID_PNP_REMOVE_WAKE_UP_PATTERN,
138 #endif /* RNDIS_WAKEUP */
139 #endif /* RNDIS_PM */
144 static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
145 unsigned buf_len, rndis_resp_t *r)
147 int retval = -ENOTSUPP;
148 u32 length = 4; /* usually */
151 rndis_query_cmplt_type *resp;
152 rndis_params *params;
156 resp = (rndis_query_cmplt_type *) r->buf;
161 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
163 debug("query OID %08x value, len %d:\n", OID, buf_len);
164 for (i = 0; i < buf_len; i += 16) {
165 debug("%03d: %08x %08x %08x %08x\n", i,
166 get_unaligned_le32(&buf[i]),
167 get_unaligned_le32(&buf[i + 4]),
168 get_unaligned_le32(&buf[i + 8]),
169 get_unaligned_le32(&buf[i + 12]));
174 /* response goes here, right after the header */
175 outbuf = (__le32 *) &resp[1];
176 resp->InformationBufferOffset = __constant_cpu_to_le32(16);
178 params = &rndis_per_dev_params[configNr];
181 /* general oids (table 4-1) */
184 case OID_GEN_SUPPORTED_LIST:
185 debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
186 length = sizeof(oid_supported_list);
187 count = length / sizeof(u32);
188 for (i = 0; i < count; i++)
189 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
194 case OID_GEN_HARDWARE_STATUS:
195 debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
198 * Hardware must be ready to receive high level protocols.
200 * reddite ergo quae sunt Caesaris Caesari
201 * et quae sunt Dei Deo!
203 *outbuf = __constant_cpu_to_le32(0);
208 case OID_GEN_MEDIA_SUPPORTED:
209 debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
210 *outbuf = cpu_to_le32(params->medium);
215 case OID_GEN_MEDIA_IN_USE:
216 debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
217 /* one medium, one transport... (maybe you do it better) */
218 *outbuf = cpu_to_le32(params->medium);
223 case OID_GEN_MAXIMUM_FRAME_SIZE:
224 debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
226 *outbuf = cpu_to_le32(params->mtu);
232 case OID_GEN_LINK_SPEED:
233 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
234 debug("%s: OID_GEN_LINK_SPEED\n", __func__);
236 if (params->media_state == NDIS_MEDIA_STATE_DISCONNECTED)
237 *outbuf = __constant_cpu_to_le32(0);
239 *outbuf = cpu_to_le32(params->speed);
244 case OID_GEN_TRANSMIT_BLOCK_SIZE:
245 debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
247 *outbuf = cpu_to_le32(params->mtu);
253 case OID_GEN_RECEIVE_BLOCK_SIZE:
254 debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
256 *outbuf = cpu_to_le32(params->mtu);
262 case OID_GEN_VENDOR_ID:
263 debug("%s: OID_GEN_VENDOR_ID\n", __func__);
264 *outbuf = cpu_to_le32(params->vendorID);
269 case OID_GEN_VENDOR_DESCRIPTION:
270 debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
271 length = strlen(params->vendorDescr);
272 memcpy(outbuf, params->vendorDescr, length);
276 case OID_GEN_VENDOR_DRIVER_VERSION:
277 debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
279 *outbuf = rndis_driver_version;
284 case OID_GEN_CURRENT_PACKET_FILTER:
285 debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
286 *outbuf = cpu_to_le32(*params->filter);
291 case OID_GEN_MAXIMUM_TOTAL_SIZE:
292 debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
293 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
298 case OID_GEN_MEDIA_CONNECT_STATUS:
299 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
300 debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
302 *outbuf = cpu_to_le32(params->media_state);
306 case OID_GEN_PHYSICAL_MEDIUM:
307 debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
308 *outbuf = __constant_cpu_to_le32(0);
313 * The RNDIS specification is incomplete/wrong. Some versions
314 * of MS-Windows expect OIDs that aren't specified there. Other
315 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
317 case OID_GEN_MAC_OPTIONS: /* from WinME */
318 debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
319 *outbuf = __constant_cpu_to_le32(
320 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
321 | NDIS_MAC_OPTION_FULL_DUPLEX);
325 /* statistics OIDs (table 4-2) */
328 case OID_GEN_XMIT_OK:
329 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
330 debug("%s: OID_GEN_XMIT_OK\n", __func__);
333 *outbuf = cpu_to_le32(
334 params->stats->tx_packets -
335 params->stats->tx_errors -
336 params->stats->tx_dropped);
343 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
344 debug("%s: OID_GEN_RCV_OK\n", __func__);
347 *outbuf = cpu_to_le32(
348 params->stats->rx_packets -
349 params->stats->rx_errors -
350 params->stats->rx_dropped);
356 case OID_GEN_XMIT_ERROR:
357 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
358 debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
361 *outbuf = cpu_to_le32(params->stats->tx_errors);
367 case OID_GEN_RCV_ERROR:
368 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
369 debug("%s: OID_GEN_RCV_ERROR\n", __func__);
372 *outbuf = cpu_to_le32(params->stats->rx_errors);
378 case OID_GEN_RCV_NO_BUFFER:
379 debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
381 *outbuf = cpu_to_le32(params->stats->rx_dropped);
386 #ifdef RNDIS_OPTIONAL_STATS
387 case OID_GEN_DIRECTED_BYTES_XMIT:
388 debug("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __func__);
390 * Aunt Tilly's size of shoes
391 * minus antarctica count of penguins
392 * divided by weight of Alpha Centauri
395 *outbuf = cpu_to_le32(
396 (params->stats->tx_packets -
397 params->stats->tx_errors -
398 params->stats->tx_dropped)
404 case OID_GEN_DIRECTED_FRAMES_XMIT:
405 debug("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __func__);
408 *outbuf = cpu_to_le32(
409 (params->stats->tx_packets -
410 params->stats->tx_errors -
411 params->stats->tx_dropped)
417 case OID_GEN_MULTICAST_BYTES_XMIT:
418 debug("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __func__);
420 *outbuf = cpu_to_le32(params->stats->multicast * 1234);
425 case OID_GEN_MULTICAST_FRAMES_XMIT:
426 debug("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __func__);
428 *outbuf = cpu_to_le32(params->stats->multicast);
433 case OID_GEN_BROADCAST_BYTES_XMIT:
434 debug("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __func__);
436 *outbuf = cpu_to_le32(params->stats->tx_packets/42*255);
441 case OID_GEN_BROADCAST_FRAMES_XMIT:
442 debug("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __func__);
444 *outbuf = cpu_to_le32(params->stats->tx_packets / 42);
449 case OID_GEN_DIRECTED_BYTES_RCV:
450 debug("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __func__);
451 *outbuf = __constant_cpu_to_le32(0);
455 case OID_GEN_DIRECTED_FRAMES_RCV:
456 debug("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __func__);
457 *outbuf = __constant_cpu_to_le32(0);
461 case OID_GEN_MULTICAST_BYTES_RCV:
462 debug("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __func__);
464 *outbuf = cpu_to_le32(params->stats->multicast * 1111);
469 case OID_GEN_MULTICAST_FRAMES_RCV:
470 debug("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __func__);
472 *outbuf = cpu_to_le32(params->stats->multicast);
477 case OID_GEN_BROADCAST_BYTES_RCV:
478 debug("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __func__);
480 *outbuf = cpu_to_le32(params->stats->rx_packets/42*255);
485 case OID_GEN_BROADCAST_FRAMES_RCV:
486 debug("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __func__);
488 *outbuf = cpu_to_le32(params->stats->rx_packets / 42);
493 case OID_GEN_RCV_CRC_ERROR:
494 debug("%s: OID_GEN_RCV_CRC_ERROR\n", __func__);
496 *outbuf = cpu_to_le32(params->stats->rx_crc_errors);
501 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
502 debug("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __func__);
503 *outbuf = __constant_cpu_to_le32(0);
506 #endif /* RNDIS_OPTIONAL_STATS */
508 /* ieee802.3 OIDs (table 4-3) */
511 case OID_802_3_PERMANENT_ADDRESS:
512 debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
515 memcpy(outbuf, params->host_mac, length);
521 case OID_802_3_CURRENT_ADDRESS:
522 debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
525 memcpy(outbuf, params->host_mac, length);
531 case OID_802_3_MULTICAST_LIST:
532 debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
533 /* Multicast base address only */
534 *outbuf = __constant_cpu_to_le32(0xE0000000);
539 case OID_802_3_MAXIMUM_LIST_SIZE:
540 debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
541 /* Multicast base address only */
542 *outbuf = __constant_cpu_to_le32(1);
546 case OID_802_3_MAC_OPTIONS:
547 debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
550 /* ieee802.3 statistics OIDs (table 4-4) */
553 case OID_802_3_RCV_ERROR_ALIGNMENT:
554 debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
556 *outbuf = cpu_to_le32(params->stats->rx_frame_errors);
562 case OID_802_3_XMIT_ONE_COLLISION:
563 debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
564 *outbuf = __constant_cpu_to_le32(0);
569 case OID_802_3_XMIT_MORE_COLLISIONS:
570 debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
571 *outbuf = __constant_cpu_to_le32(0);
575 #ifdef RNDIS_OPTIONAL_STATS
576 case OID_802_3_XMIT_DEFERRED:
577 debug("%s: OID_802_3_XMIT_DEFERRED\n", __func__);
581 case OID_802_3_XMIT_MAX_COLLISIONS:
582 debug("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __func__);
586 case OID_802_3_RCV_OVERRUN:
587 debug("%s: OID_802_3_RCV_OVERRUN\n", __func__);
591 case OID_802_3_XMIT_UNDERRUN:
592 debug("%s: OID_802_3_XMIT_UNDERRUN\n", __func__);
596 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
597 debug("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __func__);
601 case OID_802_3_XMIT_TIMES_CRS_LOST:
602 debug("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __func__);
606 case OID_802_3_XMIT_LATE_COLLISIONS:
607 debug("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __func__);
610 #endif /* RNDIS_OPTIONAL_STATS */
613 /* power management OIDs (table 4-5) */
614 case OID_PNP_CAPABILITIES:
615 debug("%s: OID_PNP_CAPABILITIES\n", __func__);
617 /* for now, no wakeup capabilities */
618 length = sizeof(struct NDIS_PNP_CAPABILITIES);
619 memset(outbuf, 0, length);
622 case OID_PNP_QUERY_POWER:
623 debug("%s: OID_PNP_QUERY_POWER D%d\n", __func__,
624 get_unaligned_le32(buf) - 1);
626 * only suspend is a real power state, and
627 * it can't be entered by OID_PNP_SET_POWER...
635 debug("%s: query unknown OID 0x%08X\n", __func__, OID);
640 resp->InformationBufferLength = cpu_to_le32(length);
641 r->length = length + sizeof *resp;
642 resp->MessageLength = cpu_to_le32(r->length);
646 static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
649 rndis_set_cmplt_type *resp;
650 int retval = -ENOTSUPP;
651 struct rndis_params *params;
652 #if (defined(DEBUG) && defined(DEBUG_VERBOSE)) || defined(RNDIS_PM)
658 resp = (rndis_set_cmplt_type *) r->buf;
662 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
664 debug("set OID %08x value, len %d:\n", OID, buf_len);
665 for (i = 0; i < buf_len; i += 16) {
666 debug("%03d: %08x %08x %08x %08x\n", i,
667 get_unaligned_le32(&buf[i]),
668 get_unaligned_le32(&buf[i + 4]),
669 get_unaligned_le32(&buf[i + 8]),
670 get_unaligned_le32(&buf[i + 12]));
675 params = &rndis_per_dev_params[configNr];
677 case OID_GEN_CURRENT_PACKET_FILTER:
680 * these NDIS_PACKET_TYPE_* bitflags are shared with
681 * cdc_filter; it's not RNDIS-specific
682 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
683 * PROMISCUOUS, DIRECTED,
684 * MULTICAST, ALL_MULTICAST, BROADCAST
686 *params->filter = (u16) get_unaligned_le32(buf);
687 debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
688 __func__, *params->filter);
691 * this call has a significant side effect: it's
692 * what makes the packet flow start and stop, like
693 * activating the CDC Ethernet altsetting.
700 params->state = RNDIS_DATA_INITIALIZED;
702 params->state = RNDIS_INITIALIZED;
705 case OID_802_3_MULTICAST_LIST:
706 /* I think we can ignore this */
707 debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
711 case OID_GEN_RNDIS_CONFIG_PARAMETER:
713 struct rndis_config_parameter *param;
714 param = (struct rndis_config_parameter *) buf;
715 debug("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
717 min(cpu_to_le32(param->ParameterNameLength), 80),
718 buf + param->ParameterNameOffset);
725 case OID_PNP_SET_POWER:
727 * The only real power state is USB suspend, and RNDIS requests
728 * can't enter it; this one isn't really about power. After
729 * resuming, Windows forces a reset, and then SET_POWER D0.
730 * FIXME ... then things go batty; Windows wedges itself.
732 i = get_unaligned_le32(buf);
733 debug("%s: OID_PNP_SET_POWER D%d\n", __func__, i - 1);
735 case NdisDeviceStateD0:
736 *params->filter = params->saved_filter;
737 goto update_linkstate;
738 case NdisDeviceStateD3:
739 case NdisDeviceStateD2:
740 case NdisDeviceStateD1:
741 params->saved_filter = *params->filter;
749 * no wakeup support advertised, so wakeup OIDs always fail:
750 * - OID_PNP_ENABLE_WAKE_UP
751 * - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
755 #endif /* RNDIS_PM */
758 debug("%s: set unknown OID 0x%08X, size %d\n",
759 __func__, OID, buf_len);
769 static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
771 rndis_init_cmplt_type *resp;
774 if (!rndis_per_dev_params[configNr].dev)
777 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
780 resp = (rndis_init_cmplt_type *) r->buf;
782 resp->MessageType = __constant_cpu_to_le32(
783 REMOTE_NDIS_INITIALIZE_CMPLT);
784 resp->MessageLength = __constant_cpu_to_le32(52);
785 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
786 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
787 resp->MajorVersion = __constant_cpu_to_le32(RNDIS_MAJOR_VERSION);
788 resp->MinorVersion = __constant_cpu_to_le32(RNDIS_MINOR_VERSION);
789 resp->DeviceFlags = __constant_cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
790 resp->Medium = __constant_cpu_to_le32(RNDIS_MEDIUM_802_3);
791 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32(1);
792 resp->MaxTransferSize = cpu_to_le32(
793 rndis_per_dev_params[configNr].mtu
795 + sizeof(struct rndis_packet_msg_type)
797 resp->PacketAlignmentFactor = __constant_cpu_to_le32(0);
798 resp->AFListOffset = __constant_cpu_to_le32(0);
799 resp->AFListSize = __constant_cpu_to_le32(0);
801 if (rndis_per_dev_params[configNr].ack)
802 rndis_per_dev_params[configNr].ack(
803 rndis_per_dev_params[configNr].dev);
808 static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
810 rndis_query_cmplt_type *resp;
813 debug("%s: OID = %08X\n", __func__, get_unaligned_le32(&buf->OID));
814 if (!rndis_per_dev_params[configNr].dev)
818 * we need more memory:
819 * gen_ndis_query_resp expects enough space for
820 * rndis_query_cmplt_type followed by data.
821 * oid_supported_list is the largest data reply
823 r = rndis_add_response(configNr,
824 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
827 resp = (rndis_query_cmplt_type *) r->buf;
829 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
830 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
832 if (gen_ndis_query_resp(configNr, get_unaligned_le32(&buf->OID),
833 get_unaligned_le32(&buf->InformationBufferOffset)
835 get_unaligned_le32(&buf->InformationBufferLength),
837 /* OID not supported */
838 resp->Status = __constant_cpu_to_le32(
839 RNDIS_STATUS_NOT_SUPPORTED);
840 resp->MessageLength = __constant_cpu_to_le32(sizeof *resp);
841 resp->InformationBufferLength = __constant_cpu_to_le32(0);
842 resp->InformationBufferOffset = __constant_cpu_to_le32(0);
844 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
846 if (rndis_per_dev_params[configNr].ack)
847 rndis_per_dev_params[configNr].ack(
848 rndis_per_dev_params[configNr].dev);
852 static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
854 u32 BufLength, BufOffset;
855 rndis_set_cmplt_type *resp;
858 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
861 resp = (rndis_set_cmplt_type *) r->buf;
863 BufLength = get_unaligned_le32(&buf->InformationBufferLength);
864 BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
867 debug("%s: Length: %d\n", __func__, BufLength);
868 debug("%s: Offset: %d\n", __func__, BufOffset);
869 debug("%s: InfoBuffer: ", __func__);
871 for (i = 0; i < BufLength; i++)
872 debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
877 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
878 resp->MessageLength = __constant_cpu_to_le32(16);
879 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
880 if (gen_ndis_set_resp(configNr, get_unaligned_le32(&buf->OID),
881 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
882 resp->Status = __constant_cpu_to_le32(
883 RNDIS_STATUS_NOT_SUPPORTED);
885 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
887 if (rndis_per_dev_params[configNr].ack)
888 rndis_per_dev_params[configNr].ack(
889 rndis_per_dev_params[configNr].dev);
894 static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
896 rndis_reset_cmplt_type *resp;
899 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
902 resp = (rndis_reset_cmplt_type *) r->buf;
904 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
905 resp->MessageLength = __constant_cpu_to_le32(16);
906 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
907 /* resent information */
908 resp->AddressingReset = __constant_cpu_to_le32(1);
910 if (rndis_per_dev_params[configNr].ack)
911 rndis_per_dev_params[configNr].ack(
912 rndis_per_dev_params[configNr].dev);
917 static int rndis_keepalive_response(int configNr,
918 rndis_keepalive_msg_type *buf)
920 rndis_keepalive_cmplt_type *resp;
923 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
925 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
928 resp = (rndis_keepalive_cmplt_type *) r->buf;
930 resp->MessageType = __constant_cpu_to_le32(
931 REMOTE_NDIS_KEEPALIVE_CMPLT);
932 resp->MessageLength = __constant_cpu_to_le32(16);
933 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
934 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
936 if (rndis_per_dev_params[configNr].ack)
937 rndis_per_dev_params[configNr].ack(
938 rndis_per_dev_params[configNr].dev);
945 * Device to Host Comunication
947 static int rndis_indicate_status_msg(int configNr, u32 status)
949 rndis_indicate_status_msg_type *resp;
952 if (rndis_per_dev_params[configNr].state == RNDIS_UNINITIALIZED)
955 r = rndis_add_response(configNr,
956 sizeof(rndis_indicate_status_msg_type));
959 resp = (rndis_indicate_status_msg_type *) r->buf;
961 resp->MessageType = __constant_cpu_to_le32(
962 REMOTE_NDIS_INDICATE_STATUS_MSG);
963 resp->MessageLength = __constant_cpu_to_le32(20);
964 resp->Status = cpu_to_le32(status);
965 resp->StatusBufferLength = __constant_cpu_to_le32(0);
966 resp->StatusBufferOffset = __constant_cpu_to_le32(0);
968 if (rndis_per_dev_params[configNr].ack)
969 rndis_per_dev_params[configNr].ack(
970 rndis_per_dev_params[configNr].dev);
974 int rndis_signal_connect(int configNr)
976 rndis_per_dev_params[configNr].media_state
977 = NDIS_MEDIA_STATE_CONNECTED;
978 return rndis_indicate_status_msg(configNr,
979 RNDIS_STATUS_MEDIA_CONNECT);
982 int rndis_signal_disconnect(int configNr)
984 rndis_per_dev_params[configNr].media_state
985 = NDIS_MEDIA_STATE_DISCONNECTED;
987 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
988 return rndis_indicate_status_msg(configNr,
989 RNDIS_STATUS_MEDIA_DISCONNECT);
995 void rndis_uninit(int configNr)
1000 if (configNr >= RNDIS_MAX_CONFIGS)
1002 rndis_per_dev_params[configNr].used = 0;
1003 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
1005 /* drain the response queue */
1006 while ((buf = rndis_get_next_response(configNr, &length)))
1007 rndis_free_response(configNr, buf);
1010 void rndis_set_host_mac(int configNr, const u8 *addr)
1012 rndis_per_dev_params[configNr].host_mac = addr;
1015 enum rndis_state rndis_get_state(int configNr)
1017 if (configNr >= RNDIS_MAX_CONFIGS || configNr < 0)
1019 return rndis_per_dev_params[configNr].state;
1025 int rndis_msg_parser(u8 configNr, u8 *buf)
1027 u32 MsgType, MsgLength;
1029 struct rndis_params *params;
1031 debug("%s: configNr = %d, %p\n", __func__, configNr, buf);
1036 tmp = (__le32 *) buf;
1037 MsgType = get_unaligned_le32(tmp++);
1038 MsgLength = get_unaligned_le32(tmp++);
1040 if (configNr >= RNDIS_MAX_CONFIGS)
1042 params = &rndis_per_dev_params[configNr];
1045 * NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1046 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1047 * and normal HC level polling to see if there's any IN traffic.
1050 /* For USB: responses may take up to 10 seconds */
1052 case REMOTE_NDIS_INITIALIZE_MSG:
1053 debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n", __func__);
1054 params->state = RNDIS_INITIALIZED;
1055 return rndis_init_response(configNr,
1056 (rndis_init_msg_type *) buf);
1058 case REMOTE_NDIS_HALT_MSG:
1059 debug("%s: REMOTE_NDIS_HALT_MSG\n", __func__);
1060 params->state = RNDIS_UNINITIALIZED;
1063 case REMOTE_NDIS_QUERY_MSG:
1064 return rndis_query_response(configNr,
1065 (rndis_query_msg_type *) buf);
1067 case REMOTE_NDIS_SET_MSG:
1068 return rndis_set_response(configNr,
1069 (rndis_set_msg_type *) buf);
1071 case REMOTE_NDIS_RESET_MSG:
1072 debug("%s: REMOTE_NDIS_RESET_MSG\n", __func__);
1073 return rndis_reset_response(configNr,
1074 (rndis_reset_msg_type *) buf);
1076 case REMOTE_NDIS_KEEPALIVE_MSG:
1077 /* For USB: host does this every 5 seconds */
1078 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
1079 debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n", __func__);
1081 return rndis_keepalive_response(configNr,
1082 (rndis_keepalive_msg_type *) buf);
1086 * At least Windows XP emits some undefined RNDIS messages.
1087 * In one case those messages seemed to relate to the host
1088 * suspending itself.
1090 debug("%s: unknown RNDIS message 0x%08X len %d\n",
1091 __func__ , MsgType, MsgLength);
1094 for (i = 0; i < MsgLength; i += 16) {
1096 " %02x %02x %02x %02x"
1097 " %02x %02x %02x %02x"
1098 " %02x %02x %02x %02x"
1099 " %02x %02x %02x %02x"
1107 buf[i+10], buf[i+11],
1108 buf[i+12], buf[i+13],
1109 buf[i+14], buf[i+15]);
1118 #ifndef CONFIG_DM_ETH
1119 int rndis_register(int (*rndis_control_ack)(struct eth_device *))
1121 int rndis_register(int (*rndis_control_ack)(struct udevice *))
1126 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1127 if (!rndis_per_dev_params[i].used) {
1128 rndis_per_dev_params[i].used = 1;
1129 rndis_per_dev_params[i].ack = rndis_control_ack;
1130 debug("%s: configNr = %d\n", __func__, i);
1134 debug("%s failed\n", __func__);
1139 void rndis_deregister(int configNr)
1141 debug("%s: configNr = %d\n", __func__, configNr);
1143 if (configNr >= RNDIS_MAX_CONFIGS)
1145 rndis_per_dev_params[configNr].used = 0;
1150 #ifndef CONFIG_DM_ETH
1151 int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
1152 struct net_device_stats *stats, u16 *cdc_filter)
1154 int rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
1155 struct net_device_stats *stats, u16 *cdc_filter)
1158 debug("%s: configNr = %d\n", __func__, configNr);
1161 if (configNr >= RNDIS_MAX_CONFIGS)
1164 rndis_per_dev_params[configNr].dev = dev;
1165 rndis_per_dev_params[configNr].stats = stats;
1166 rndis_per_dev_params[configNr].mtu = mtu;
1167 rndis_per_dev_params[configNr].filter = cdc_filter;
1172 int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
1174 debug("%s: configNr = %d\n", __func__, configNr);
1177 if (configNr >= RNDIS_MAX_CONFIGS)
1180 rndis_per_dev_params[configNr].vendorID = vendorID;
1181 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
1186 int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
1188 debug("%s: configNr = %d, %u %u\n", __func__, configNr, medium, speed);
1189 if (configNr >= RNDIS_MAX_CONFIGS)
1192 rndis_per_dev_params[configNr].medium = medium;
1193 rndis_per_dev_params[configNr].speed = speed;
1198 void rndis_add_hdr(void *buf, int length)
1200 struct rndis_packet_msg_type *header;
1203 memset(header, 0, sizeof *header);
1204 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1205 header->MessageLength = cpu_to_le32(length + sizeof *header);
1206 header->DataOffset = __constant_cpu_to_le32(36);
1207 header->DataLength = cpu_to_le32(length);
1210 void rndis_free_response(int configNr, u8 *buf)
1213 struct list_head *act, *tmp;
1215 list_for_each_safe(act, tmp,
1216 &(rndis_per_dev_params[configNr].resp_queue))
1218 r = list_entry(act, rndis_resp_t, list);
1219 if (r && r->buf == buf) {
1226 u8 *rndis_get_next_response(int configNr, u32 *length)
1229 struct list_head *act, *tmp;
1234 list_for_each_safe(act, tmp,
1235 &(rndis_per_dev_params[configNr].resp_queue))
1237 r = list_entry(act, rndis_resp_t, list);
1240 *length = r->length;
1248 static rndis_resp_t *rndis_add_response(int configNr, u32 length)
1252 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1253 r = malloc(sizeof(rndis_resp_t) + length);
1257 r->buf = (u8 *) (r + 1);
1261 list_add_tail(&r->list,
1262 &(rndis_per_dev_params[configNr].resp_queue));
1266 int rndis_rm_hdr(void *buf, int length)
1268 /* tmp points to a struct rndis_packet_msg_type */
1272 /* MessageType, MessageLength */
1273 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1274 != get_unaligned(tmp++))
1278 /* DataOffset, DataLength */
1279 offs = get_unaligned_le32(tmp++) + 8 /* offset of DataOffset */;
1280 if (offs != sizeof(struct rndis_packet_msg_type))
1281 debug("%s: unexpected DataOffset: %d\n", __func__, offs);
1285 len = get_unaligned_le32(tmp++);
1286 if (len + sizeof(struct rndis_packet_msg_type) != length)
1287 debug("%s: unexpected DataLength: %d, packet length=%d\n",
1288 __func__, len, length);
1290 memmove(buf, buf + offs, len);
1295 int rndis_init(void)
1299 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1300 rndis_per_dev_params[i].confignr = i;
1301 rndis_per_dev_params[i].used = 0;
1302 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1303 rndis_per_dev_params[i].media_state
1304 = NDIS_MEDIA_STATE_DISCONNECTED;
1305 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
1311 void rndis_exit(void)