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
24 #include <linux/types.h>
25 #include <linux/list.h>
26 #include <linux/netdevice.h>
28 #include <asm/byteorder.h>
29 #include <asm/unaligned.h>
30 #include <linux/errno.h>
38 #define ETH_ALEN 6 /* Octets in one ethernet addr */
39 #define ETH_HLEN 14 /* Total octets in header. */
40 #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
41 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
42 #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
45 * The driver for your USB chip needs to support ep0 OUT to work with
46 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
48 * Windows hosts need an INF file like Documentation/usb/linux.inf
49 * and will be happier if you provide the host_addr module parameter.
52 #define RNDIS_MAX_CONFIGS 1
54 static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
57 static const __le32 rndis_driver_version = __constant_cpu_to_le32(1);
59 /* Function Prototypes */
60 static rndis_resp_t *rndis_add_response(int configNr, u32 length);
64 static const u32 oid_supported_list[] = {
65 /* the general stuff */
66 OID_GEN_SUPPORTED_LIST,
67 OID_GEN_HARDWARE_STATUS,
68 OID_GEN_MEDIA_SUPPORTED,
70 OID_GEN_MAXIMUM_FRAME_SIZE,
72 OID_GEN_TRANSMIT_BLOCK_SIZE,
73 OID_GEN_RECEIVE_BLOCK_SIZE,
75 OID_GEN_VENDOR_DESCRIPTION,
76 OID_GEN_VENDOR_DRIVER_VERSION,
77 OID_GEN_CURRENT_PACKET_FILTER,
78 OID_GEN_MAXIMUM_TOTAL_SIZE,
79 OID_GEN_MEDIA_CONNECT_STATUS,
80 OID_GEN_PHYSICAL_MEDIUM,
82 OID_GEN_RNDIS_CONFIG_PARAMETER,
85 /* the statistical stuff */
90 OID_GEN_RCV_NO_BUFFER,
91 #ifdef RNDIS_OPTIONAL_STATS
92 OID_GEN_DIRECTED_BYTES_XMIT,
93 OID_GEN_DIRECTED_FRAMES_XMIT,
94 OID_GEN_MULTICAST_BYTES_XMIT,
95 OID_GEN_MULTICAST_FRAMES_XMIT,
96 OID_GEN_BROADCAST_BYTES_XMIT,
97 OID_GEN_BROADCAST_FRAMES_XMIT,
98 OID_GEN_DIRECTED_BYTES_RCV,
99 OID_GEN_DIRECTED_FRAMES_RCV,
100 OID_GEN_MULTICAST_BYTES_RCV,
101 OID_GEN_MULTICAST_FRAMES_RCV,
102 OID_GEN_BROADCAST_BYTES_RCV,
103 OID_GEN_BROADCAST_FRAMES_RCV,
104 OID_GEN_RCV_CRC_ERROR,
105 OID_GEN_TRANSMIT_QUEUE_LENGTH,
106 #endif /* RNDIS_OPTIONAL_STATS */
108 /* mandatory 802.3 */
109 /* the general stuff */
110 OID_802_3_PERMANENT_ADDRESS,
111 OID_802_3_CURRENT_ADDRESS,
112 OID_802_3_MULTICAST_LIST,
113 OID_802_3_MAC_OPTIONS,
114 OID_802_3_MAXIMUM_LIST_SIZE,
116 /* the statistical stuff */
117 OID_802_3_RCV_ERROR_ALIGNMENT,
118 OID_802_3_XMIT_ONE_COLLISION,
119 OID_802_3_XMIT_MORE_COLLISIONS,
120 #ifdef RNDIS_OPTIONAL_STATS
121 OID_802_3_XMIT_DEFERRED,
122 OID_802_3_XMIT_MAX_COLLISIONS,
123 OID_802_3_RCV_OVERRUN,
124 OID_802_3_XMIT_UNDERRUN,
125 OID_802_3_XMIT_HEARTBEAT_FAILURE,
126 OID_802_3_XMIT_TIMES_CRS_LOST,
127 OID_802_3_XMIT_LATE_COLLISIONS,
128 #endif /* RNDIS_OPTIONAL_STATS */
131 /* PM and wakeup are mandatory for USB: */
133 /* power management */
134 OID_PNP_CAPABILITIES,
140 OID_PNP_ENABLE_WAKE_UP,
141 OID_PNP_ADD_WAKE_UP_PATTERN,
142 OID_PNP_REMOVE_WAKE_UP_PATTERN,
143 #endif /* RNDIS_WAKEUP */
144 #endif /* RNDIS_PM */
149 static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
150 unsigned buf_len, rndis_resp_t *r)
152 int retval = -ENOTSUPP;
153 u32 length = 4; /* usually */
156 rndis_query_cmplt_type *resp;
157 rndis_params *params;
161 resp = (rndis_query_cmplt_type *) r->buf;
166 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
168 debug("query OID %08x value, len %d:\n", OID, buf_len);
169 for (i = 0; i < buf_len; i += 16) {
170 debug("%03d: %08x %08x %08x %08x\n", i,
171 get_unaligned_le32(&buf[i]),
172 get_unaligned_le32(&buf[i + 4]),
173 get_unaligned_le32(&buf[i + 8]),
174 get_unaligned_le32(&buf[i + 12]));
179 /* response goes here, right after the header */
180 outbuf = (__le32 *) &resp[1];
181 resp->InformationBufferOffset = __constant_cpu_to_le32(16);
183 params = &rndis_per_dev_params[configNr];
186 /* general oids (table 4-1) */
189 case OID_GEN_SUPPORTED_LIST:
190 debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
191 length = sizeof(oid_supported_list);
192 count = length / sizeof(u32);
193 for (i = 0; i < count; i++)
194 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
199 case OID_GEN_HARDWARE_STATUS:
200 debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
203 * Hardware must be ready to receive high level protocols.
205 * reddite ergo quae sunt Caesaris Caesari
206 * et quae sunt Dei Deo!
208 *outbuf = __constant_cpu_to_le32(0);
213 case OID_GEN_MEDIA_SUPPORTED:
214 debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
215 *outbuf = cpu_to_le32(params->medium);
220 case OID_GEN_MEDIA_IN_USE:
221 debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
222 /* one medium, one transport... (maybe you do it better) */
223 *outbuf = cpu_to_le32(params->medium);
228 case OID_GEN_MAXIMUM_FRAME_SIZE:
229 debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
231 *outbuf = cpu_to_le32(params->mtu);
237 case OID_GEN_LINK_SPEED:
238 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
239 debug("%s: OID_GEN_LINK_SPEED\n", __func__);
241 if (params->media_state == NDIS_MEDIA_STATE_DISCONNECTED)
242 *outbuf = __constant_cpu_to_le32(0);
244 *outbuf = cpu_to_le32(params->speed);
249 case OID_GEN_TRANSMIT_BLOCK_SIZE:
250 debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
252 *outbuf = cpu_to_le32(params->mtu);
258 case OID_GEN_RECEIVE_BLOCK_SIZE:
259 debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
261 *outbuf = cpu_to_le32(params->mtu);
267 case OID_GEN_VENDOR_ID:
268 debug("%s: OID_GEN_VENDOR_ID\n", __func__);
269 *outbuf = cpu_to_le32(params->vendorID);
274 case OID_GEN_VENDOR_DESCRIPTION:
275 debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
276 length = strlen(params->vendorDescr);
277 memcpy(outbuf, params->vendorDescr, length);
281 case OID_GEN_VENDOR_DRIVER_VERSION:
282 debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
284 *outbuf = rndis_driver_version;
289 case OID_GEN_CURRENT_PACKET_FILTER:
290 debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
291 *outbuf = cpu_to_le32(*params->filter);
296 case OID_GEN_MAXIMUM_TOTAL_SIZE:
297 debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
298 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
303 case OID_GEN_MEDIA_CONNECT_STATUS:
304 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
305 debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
307 *outbuf = cpu_to_le32(params->media_state);
311 case OID_GEN_PHYSICAL_MEDIUM:
312 debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
313 *outbuf = __constant_cpu_to_le32(0);
318 * The RNDIS specification is incomplete/wrong. Some versions
319 * of MS-Windows expect OIDs that aren't specified there. Other
320 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
322 case OID_GEN_MAC_OPTIONS: /* from WinME */
323 debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
324 *outbuf = __constant_cpu_to_le32(
325 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
326 | NDIS_MAC_OPTION_FULL_DUPLEX);
330 /* statistics OIDs (table 4-2) */
333 case OID_GEN_XMIT_OK:
334 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
335 debug("%s: OID_GEN_XMIT_OK\n", __func__);
338 *outbuf = cpu_to_le32(
339 params->stats->tx_packets -
340 params->stats->tx_errors -
341 params->stats->tx_dropped);
348 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
349 debug("%s: OID_GEN_RCV_OK\n", __func__);
352 *outbuf = cpu_to_le32(
353 params->stats->rx_packets -
354 params->stats->rx_errors -
355 params->stats->rx_dropped);
361 case OID_GEN_XMIT_ERROR:
362 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
363 debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
366 *outbuf = cpu_to_le32(params->stats->tx_errors);
372 case OID_GEN_RCV_ERROR:
373 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
374 debug("%s: OID_GEN_RCV_ERROR\n", __func__);
377 *outbuf = cpu_to_le32(params->stats->rx_errors);
383 case OID_GEN_RCV_NO_BUFFER:
384 debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
386 *outbuf = cpu_to_le32(params->stats->rx_dropped);
391 #ifdef RNDIS_OPTIONAL_STATS
392 case OID_GEN_DIRECTED_BYTES_XMIT:
393 debug("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __func__);
395 * Aunt Tilly's size of shoes
396 * minus antarctica count of penguins
397 * divided by weight of Alpha Centauri
400 *outbuf = cpu_to_le32(
401 (params->stats->tx_packets -
402 params->stats->tx_errors -
403 params->stats->tx_dropped)
409 case OID_GEN_DIRECTED_FRAMES_XMIT:
410 debug("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __func__);
413 *outbuf = cpu_to_le32(
414 (params->stats->tx_packets -
415 params->stats->tx_errors -
416 params->stats->tx_dropped)
422 case OID_GEN_MULTICAST_BYTES_XMIT:
423 debug("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __func__);
425 *outbuf = cpu_to_le32(params->stats->multicast * 1234);
430 case OID_GEN_MULTICAST_FRAMES_XMIT:
431 debug("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __func__);
433 *outbuf = cpu_to_le32(params->stats->multicast);
438 case OID_GEN_BROADCAST_BYTES_XMIT:
439 debug("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __func__);
441 *outbuf = cpu_to_le32(params->stats->tx_packets/42*255);
446 case OID_GEN_BROADCAST_FRAMES_XMIT:
447 debug("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __func__);
449 *outbuf = cpu_to_le32(params->stats->tx_packets / 42);
454 case OID_GEN_DIRECTED_BYTES_RCV:
455 debug("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __func__);
456 *outbuf = __constant_cpu_to_le32(0);
460 case OID_GEN_DIRECTED_FRAMES_RCV:
461 debug("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __func__);
462 *outbuf = __constant_cpu_to_le32(0);
466 case OID_GEN_MULTICAST_BYTES_RCV:
467 debug("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __func__);
469 *outbuf = cpu_to_le32(params->stats->multicast * 1111);
474 case OID_GEN_MULTICAST_FRAMES_RCV:
475 debug("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __func__);
477 *outbuf = cpu_to_le32(params->stats->multicast);
482 case OID_GEN_BROADCAST_BYTES_RCV:
483 debug("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __func__);
485 *outbuf = cpu_to_le32(params->stats->rx_packets/42*255);
490 case OID_GEN_BROADCAST_FRAMES_RCV:
491 debug("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __func__);
493 *outbuf = cpu_to_le32(params->stats->rx_packets / 42);
498 case OID_GEN_RCV_CRC_ERROR:
499 debug("%s: OID_GEN_RCV_CRC_ERROR\n", __func__);
501 *outbuf = cpu_to_le32(params->stats->rx_crc_errors);
506 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
507 debug("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __func__);
508 *outbuf = __constant_cpu_to_le32(0);
511 #endif /* RNDIS_OPTIONAL_STATS */
513 /* ieee802.3 OIDs (table 4-3) */
516 case OID_802_3_PERMANENT_ADDRESS:
517 debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
520 memcpy(outbuf, params->host_mac, length);
526 case OID_802_3_CURRENT_ADDRESS:
527 debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
530 memcpy(outbuf, params->host_mac, length);
536 case OID_802_3_MULTICAST_LIST:
537 debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
538 /* Multicast base address only */
539 *outbuf = __constant_cpu_to_le32(0xE0000000);
544 case OID_802_3_MAXIMUM_LIST_SIZE:
545 debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
546 /* Multicast base address only */
547 *outbuf = __constant_cpu_to_le32(1);
551 case OID_802_3_MAC_OPTIONS:
552 debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
555 /* ieee802.3 statistics OIDs (table 4-4) */
558 case OID_802_3_RCV_ERROR_ALIGNMENT:
559 debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
561 *outbuf = cpu_to_le32(params->stats->rx_frame_errors);
567 case OID_802_3_XMIT_ONE_COLLISION:
568 debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
569 *outbuf = __constant_cpu_to_le32(0);
574 case OID_802_3_XMIT_MORE_COLLISIONS:
575 debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
576 *outbuf = __constant_cpu_to_le32(0);
580 #ifdef RNDIS_OPTIONAL_STATS
581 case OID_802_3_XMIT_DEFERRED:
582 debug("%s: OID_802_3_XMIT_DEFERRED\n", __func__);
586 case OID_802_3_XMIT_MAX_COLLISIONS:
587 debug("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __func__);
591 case OID_802_3_RCV_OVERRUN:
592 debug("%s: OID_802_3_RCV_OVERRUN\n", __func__);
596 case OID_802_3_XMIT_UNDERRUN:
597 debug("%s: OID_802_3_XMIT_UNDERRUN\n", __func__);
601 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
602 debug("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __func__);
606 case OID_802_3_XMIT_TIMES_CRS_LOST:
607 debug("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __func__);
611 case OID_802_3_XMIT_LATE_COLLISIONS:
612 debug("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __func__);
615 #endif /* RNDIS_OPTIONAL_STATS */
618 /* power management OIDs (table 4-5) */
619 case OID_PNP_CAPABILITIES:
620 debug("%s: OID_PNP_CAPABILITIES\n", __func__);
622 /* for now, no wakeup capabilities */
623 length = sizeof(struct NDIS_PNP_CAPABILITIES);
624 memset(outbuf, 0, length);
627 case OID_PNP_QUERY_POWER:
628 debug("%s: OID_PNP_QUERY_POWER D%d\n", __func__,
629 get_unaligned_le32(buf) - 1);
631 * only suspend is a real power state, and
632 * it can't be entered by OID_PNP_SET_POWER...
640 debug("%s: query unknown OID 0x%08X\n", __func__, OID);
645 resp->InformationBufferLength = cpu_to_le32(length);
646 r->length = length + sizeof *resp;
647 resp->MessageLength = cpu_to_le32(r->length);
651 static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
654 rndis_set_cmplt_type *resp;
655 int retval = -ENOTSUPP;
656 struct rndis_params *params;
657 #if (defined(DEBUG) && defined(DEBUG_VERBOSE)) || defined(RNDIS_PM)
663 resp = (rndis_set_cmplt_type *) r->buf;
667 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
669 debug("set OID %08x value, len %d:\n", OID, buf_len);
670 for (i = 0; i < buf_len; i += 16) {
671 debug("%03d: %08x %08x %08x %08x\n", i,
672 get_unaligned_le32(&buf[i]),
673 get_unaligned_le32(&buf[i + 4]),
674 get_unaligned_le32(&buf[i + 8]),
675 get_unaligned_le32(&buf[i + 12]));
680 params = &rndis_per_dev_params[configNr];
682 case OID_GEN_CURRENT_PACKET_FILTER:
685 * these NDIS_PACKET_TYPE_* bitflags are shared with
686 * cdc_filter; it's not RNDIS-specific
687 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
688 * PROMISCUOUS, DIRECTED,
689 * MULTICAST, ALL_MULTICAST, BROADCAST
691 *params->filter = (u16) get_unaligned_le32(buf);
692 debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
693 __func__, *params->filter);
696 * this call has a significant side effect: it's
697 * what makes the packet flow start and stop, like
698 * activating the CDC Ethernet altsetting.
705 params->state = RNDIS_DATA_INITIALIZED;
707 params->state = RNDIS_INITIALIZED;
710 case OID_802_3_MULTICAST_LIST:
711 /* I think we can ignore this */
712 debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
716 case OID_GEN_RNDIS_CONFIG_PARAMETER:
718 struct rndis_config_parameter *param;
719 param = (struct rndis_config_parameter *) buf;
720 debug("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
722 min(cpu_to_le32(param->ParameterNameLength), 80),
723 buf + param->ParameterNameOffset);
730 case OID_PNP_SET_POWER:
732 * The only real power state is USB suspend, and RNDIS requests
733 * can't enter it; this one isn't really about power. After
734 * resuming, Windows forces a reset, and then SET_POWER D0.
735 * FIXME ... then things go batty; Windows wedges itself.
737 i = get_unaligned_le32(buf);
738 debug("%s: OID_PNP_SET_POWER D%d\n", __func__, i - 1);
740 case NdisDeviceStateD0:
741 *params->filter = params->saved_filter;
742 goto update_linkstate;
743 case NdisDeviceStateD3:
744 case NdisDeviceStateD2:
745 case NdisDeviceStateD1:
746 params->saved_filter = *params->filter;
754 * no wakeup support advertised, so wakeup OIDs always fail:
755 * - OID_PNP_ENABLE_WAKE_UP
756 * - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
760 #endif /* RNDIS_PM */
763 debug("%s: set unknown OID 0x%08X, size %d\n",
764 __func__, OID, buf_len);
774 static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
776 rndis_init_cmplt_type *resp;
779 if (!rndis_per_dev_params[configNr].dev)
782 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
785 resp = (rndis_init_cmplt_type *) r->buf;
787 resp->MessageType = __constant_cpu_to_le32(
788 REMOTE_NDIS_INITIALIZE_CMPLT);
789 resp->MessageLength = __constant_cpu_to_le32(52);
790 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
791 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
792 resp->MajorVersion = __constant_cpu_to_le32(RNDIS_MAJOR_VERSION);
793 resp->MinorVersion = __constant_cpu_to_le32(RNDIS_MINOR_VERSION);
794 resp->DeviceFlags = __constant_cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
795 resp->Medium = __constant_cpu_to_le32(RNDIS_MEDIUM_802_3);
796 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32(1);
797 resp->MaxTransferSize = cpu_to_le32(
798 rndis_per_dev_params[configNr].mtu
800 + sizeof(struct rndis_packet_msg_type)
802 resp->PacketAlignmentFactor = __constant_cpu_to_le32(0);
803 resp->AFListOffset = __constant_cpu_to_le32(0);
804 resp->AFListSize = __constant_cpu_to_le32(0);
806 if (rndis_per_dev_params[configNr].ack)
807 rndis_per_dev_params[configNr].ack(
808 rndis_per_dev_params[configNr].dev);
813 static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
815 rndis_query_cmplt_type *resp;
818 debug("%s: OID = %08X\n", __func__, get_unaligned_le32(&buf->OID));
819 if (!rndis_per_dev_params[configNr].dev)
823 * we need more memory:
824 * gen_ndis_query_resp expects enough space for
825 * rndis_query_cmplt_type followed by data.
826 * oid_supported_list is the largest data reply
828 r = rndis_add_response(configNr,
829 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
832 resp = (rndis_query_cmplt_type *) r->buf;
834 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
835 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
837 if (gen_ndis_query_resp(configNr, get_unaligned_le32(&buf->OID),
838 get_unaligned_le32(&buf->InformationBufferOffset)
840 get_unaligned_le32(&buf->InformationBufferLength),
842 /* OID not supported */
843 resp->Status = __constant_cpu_to_le32(
844 RNDIS_STATUS_NOT_SUPPORTED);
845 resp->MessageLength = __constant_cpu_to_le32(sizeof *resp);
846 resp->InformationBufferLength = __constant_cpu_to_le32(0);
847 resp->InformationBufferOffset = __constant_cpu_to_le32(0);
849 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
851 if (rndis_per_dev_params[configNr].ack)
852 rndis_per_dev_params[configNr].ack(
853 rndis_per_dev_params[configNr].dev);
857 static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
859 u32 BufLength, BufOffset;
860 rndis_set_cmplt_type *resp;
863 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
866 resp = (rndis_set_cmplt_type *) r->buf;
868 BufLength = get_unaligned_le32(&buf->InformationBufferLength);
869 BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
872 debug("%s: Length: %d\n", __func__, BufLength);
873 debug("%s: Offset: %d\n", __func__, BufOffset);
874 debug("%s: InfoBuffer: ", __func__);
876 for (i = 0; i < BufLength; i++)
877 debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
882 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
883 resp->MessageLength = __constant_cpu_to_le32(16);
884 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
885 if (gen_ndis_set_resp(configNr, get_unaligned_le32(&buf->OID),
886 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
887 resp->Status = __constant_cpu_to_le32(
888 RNDIS_STATUS_NOT_SUPPORTED);
890 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
892 if (rndis_per_dev_params[configNr].ack)
893 rndis_per_dev_params[configNr].ack(
894 rndis_per_dev_params[configNr].dev);
899 static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
901 rndis_reset_cmplt_type *resp;
904 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
907 resp = (rndis_reset_cmplt_type *) r->buf;
909 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
910 resp->MessageLength = __constant_cpu_to_le32(16);
911 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
912 /* resent information */
913 resp->AddressingReset = __constant_cpu_to_le32(1);
915 if (rndis_per_dev_params[configNr].ack)
916 rndis_per_dev_params[configNr].ack(
917 rndis_per_dev_params[configNr].dev);
922 static int rndis_keepalive_response(int configNr,
923 rndis_keepalive_msg_type *buf)
925 rndis_keepalive_cmplt_type *resp;
928 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
930 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
933 resp = (rndis_keepalive_cmplt_type *) r->buf;
935 resp->MessageType = __constant_cpu_to_le32(
936 REMOTE_NDIS_KEEPALIVE_CMPLT);
937 resp->MessageLength = __constant_cpu_to_le32(16);
938 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
939 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
941 if (rndis_per_dev_params[configNr].ack)
942 rndis_per_dev_params[configNr].ack(
943 rndis_per_dev_params[configNr].dev);
950 * Device to Host Comunication
952 static int rndis_indicate_status_msg(int configNr, u32 status)
954 rndis_indicate_status_msg_type *resp;
957 if (rndis_per_dev_params[configNr].state == RNDIS_UNINITIALIZED)
960 r = rndis_add_response(configNr,
961 sizeof(rndis_indicate_status_msg_type));
964 resp = (rndis_indicate_status_msg_type *) r->buf;
966 resp->MessageType = __constant_cpu_to_le32(
967 REMOTE_NDIS_INDICATE_STATUS_MSG);
968 resp->MessageLength = __constant_cpu_to_le32(20);
969 resp->Status = cpu_to_le32(status);
970 resp->StatusBufferLength = __constant_cpu_to_le32(0);
971 resp->StatusBufferOffset = __constant_cpu_to_le32(0);
973 if (rndis_per_dev_params[configNr].ack)
974 rndis_per_dev_params[configNr].ack(
975 rndis_per_dev_params[configNr].dev);
979 int rndis_signal_connect(int configNr)
981 rndis_per_dev_params[configNr].media_state
982 = NDIS_MEDIA_STATE_CONNECTED;
983 return rndis_indicate_status_msg(configNr,
984 RNDIS_STATUS_MEDIA_CONNECT);
987 int rndis_signal_disconnect(int configNr)
989 rndis_per_dev_params[configNr].media_state
990 = NDIS_MEDIA_STATE_DISCONNECTED;
992 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
993 return rndis_indicate_status_msg(configNr,
994 RNDIS_STATUS_MEDIA_DISCONNECT);
1000 void rndis_uninit(int configNr)
1005 if (configNr >= RNDIS_MAX_CONFIGS)
1007 rndis_per_dev_params[configNr].used = 0;
1008 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
1010 /* drain the response queue */
1011 while ((buf = rndis_get_next_response(configNr, &length)))
1012 rndis_free_response(configNr, buf);
1015 void rndis_set_host_mac(int configNr, const u8 *addr)
1017 rndis_per_dev_params[configNr].host_mac = addr;
1020 enum rndis_state rndis_get_state(int configNr)
1022 if (configNr >= RNDIS_MAX_CONFIGS || configNr < 0)
1024 return rndis_per_dev_params[configNr].state;
1030 int rndis_msg_parser(u8 configNr, u8 *buf)
1032 u32 MsgType, MsgLength;
1034 struct rndis_params *params;
1036 debug("%s: configNr = %d, %p\n", __func__, configNr, buf);
1041 tmp = (__le32 *) buf;
1042 MsgType = get_unaligned_le32(tmp++);
1043 MsgLength = get_unaligned_le32(tmp++);
1045 if (configNr >= RNDIS_MAX_CONFIGS)
1047 params = &rndis_per_dev_params[configNr];
1050 * NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1051 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1052 * and normal HC level polling to see if there's any IN traffic.
1055 /* For USB: responses may take up to 10 seconds */
1057 case REMOTE_NDIS_INITIALIZE_MSG:
1058 debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n", __func__);
1059 params->state = RNDIS_INITIALIZED;
1060 return rndis_init_response(configNr,
1061 (rndis_init_msg_type *) buf);
1063 case REMOTE_NDIS_HALT_MSG:
1064 debug("%s: REMOTE_NDIS_HALT_MSG\n", __func__);
1065 params->state = RNDIS_UNINITIALIZED;
1068 case REMOTE_NDIS_QUERY_MSG:
1069 return rndis_query_response(configNr,
1070 (rndis_query_msg_type *) buf);
1072 case REMOTE_NDIS_SET_MSG:
1073 return rndis_set_response(configNr,
1074 (rndis_set_msg_type *) buf);
1076 case REMOTE_NDIS_RESET_MSG:
1077 debug("%s: REMOTE_NDIS_RESET_MSG\n", __func__);
1078 return rndis_reset_response(configNr,
1079 (rndis_reset_msg_type *) buf);
1081 case REMOTE_NDIS_KEEPALIVE_MSG:
1082 /* For USB: host does this every 5 seconds */
1083 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
1084 debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n", __func__);
1086 return rndis_keepalive_response(configNr,
1087 (rndis_keepalive_msg_type *) buf);
1091 * At least Windows XP emits some undefined RNDIS messages.
1092 * In one case those messages seemed to relate to the host
1093 * suspending itself.
1095 debug("%s: unknown RNDIS message 0x%08X len %d\n",
1096 __func__ , MsgType, MsgLength);
1099 for (i = 0; i < MsgLength; i += 16) {
1101 " %02x %02x %02x %02x"
1102 " %02x %02x %02x %02x"
1103 " %02x %02x %02x %02x"
1104 " %02x %02x %02x %02x"
1112 buf[i+10], buf[i+11],
1113 buf[i+12], buf[i+13],
1114 buf[i+14], buf[i+15]);
1123 #ifndef CONFIG_DM_ETH
1124 int rndis_register(int (*rndis_control_ack)(struct eth_device *))
1126 int rndis_register(int (*rndis_control_ack)(struct udevice *))
1131 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1132 if (!rndis_per_dev_params[i].used) {
1133 rndis_per_dev_params[i].used = 1;
1134 rndis_per_dev_params[i].ack = rndis_control_ack;
1135 debug("%s: configNr = %d\n", __func__, i);
1139 debug("%s failed\n", __func__);
1144 void rndis_deregister(int configNr)
1146 debug("%s: configNr = %d\n", __func__, configNr);
1148 if (configNr >= RNDIS_MAX_CONFIGS)
1150 rndis_per_dev_params[configNr].used = 0;
1155 #ifndef CONFIG_DM_ETH
1156 int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
1157 struct net_device_stats *stats, u16 *cdc_filter)
1159 int rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
1160 struct net_device_stats *stats, u16 *cdc_filter)
1163 debug("%s: configNr = %d\n", __func__, configNr);
1166 if (configNr >= RNDIS_MAX_CONFIGS)
1169 rndis_per_dev_params[configNr].dev = dev;
1170 rndis_per_dev_params[configNr].stats = stats;
1171 rndis_per_dev_params[configNr].mtu = mtu;
1172 rndis_per_dev_params[configNr].filter = cdc_filter;
1177 int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
1179 debug("%s: configNr = %d\n", __func__, configNr);
1182 if (configNr >= RNDIS_MAX_CONFIGS)
1185 rndis_per_dev_params[configNr].vendorID = vendorID;
1186 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
1191 int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
1193 debug("%s: configNr = %d, %u %u\n", __func__, configNr, medium, speed);
1194 if (configNr >= RNDIS_MAX_CONFIGS)
1197 rndis_per_dev_params[configNr].medium = medium;
1198 rndis_per_dev_params[configNr].speed = speed;
1203 void rndis_add_hdr(void *buf, int length)
1205 struct rndis_packet_msg_type *header;
1208 memset(header, 0, sizeof *header);
1209 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1210 header->MessageLength = cpu_to_le32(length + sizeof *header);
1211 header->DataOffset = __constant_cpu_to_le32(36);
1212 header->DataLength = cpu_to_le32(length);
1215 void rndis_free_response(int configNr, u8 *buf)
1218 struct list_head *act, *tmp;
1220 list_for_each_safe(act, tmp,
1221 &(rndis_per_dev_params[configNr].resp_queue))
1223 r = list_entry(act, rndis_resp_t, list);
1224 if (r && r->buf == buf) {
1231 u8 *rndis_get_next_response(int configNr, u32 *length)
1234 struct list_head *act, *tmp;
1239 list_for_each_safe(act, tmp,
1240 &(rndis_per_dev_params[configNr].resp_queue))
1242 r = list_entry(act, rndis_resp_t, list);
1245 *length = r->length;
1253 static rndis_resp_t *rndis_add_response(int configNr, u32 length)
1257 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1258 r = malloc(sizeof(rndis_resp_t) + length);
1262 r->buf = (u8 *) (r + 1);
1266 list_add_tail(&r->list,
1267 &(rndis_per_dev_params[configNr].resp_queue));
1271 int rndis_rm_hdr(void *buf, int length)
1273 /* tmp points to a struct rndis_packet_msg_type */
1277 /* MessageType, MessageLength */
1278 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1279 != get_unaligned(tmp++))
1283 /* DataOffset, DataLength */
1284 offs = get_unaligned_le32(tmp++) + 8 /* offset of DataOffset */;
1285 if (offs != sizeof(struct rndis_packet_msg_type))
1286 debug("%s: unexpected DataOffset: %d\n", __func__, offs);
1290 len = get_unaligned_le32(tmp++);
1291 if (len + sizeof(struct rndis_packet_msg_type) != length)
1292 debug("%s: unexpected DataLength: %d, packet length=%d\n",
1293 __func__, len, length);
1295 memmove(buf, buf + offs, len);
1300 int rndis_init(void)
1304 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1305 rndis_per_dev_params[i].confignr = i;
1306 rndis_per_dev_params[i].used = 0;
1307 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1308 rndis_per_dev_params[i].media_state
1309 = NDIS_MEDIA_STATE_DISCONNECTED;
1310 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
1316 void rndis_exit(void)