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>
39 * The driver for your USB chip needs to support ep0 OUT to work with
40 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
42 * Windows hosts need an INF file like Documentation/usb/linux.inf
43 * and will be happier if you provide the host_addr module parameter.
46 #define RNDIS_MAX_CONFIGS 1
48 static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
51 static const __le32 rndis_driver_version = __constant_cpu_to_le32(1);
53 /* Function Prototypes */
54 static rndis_resp_t *rndis_add_response(int configNr, u32 length);
58 static const u32 oid_supported_list[] = {
59 /* the general stuff */
60 OID_GEN_SUPPORTED_LIST,
61 OID_GEN_HARDWARE_STATUS,
62 OID_GEN_MEDIA_SUPPORTED,
64 OID_GEN_MAXIMUM_FRAME_SIZE,
66 OID_GEN_TRANSMIT_BLOCK_SIZE,
67 OID_GEN_RECEIVE_BLOCK_SIZE,
69 OID_GEN_VENDOR_DESCRIPTION,
70 OID_GEN_VENDOR_DRIVER_VERSION,
71 OID_GEN_CURRENT_PACKET_FILTER,
72 OID_GEN_MAXIMUM_TOTAL_SIZE,
73 OID_GEN_MEDIA_CONNECT_STATUS,
74 OID_GEN_PHYSICAL_MEDIUM,
76 OID_GEN_RNDIS_CONFIG_PARAMETER,
79 /* the statistical stuff */
84 OID_GEN_RCV_NO_BUFFER,
85 #ifdef RNDIS_OPTIONAL_STATS
86 OID_GEN_DIRECTED_BYTES_XMIT,
87 OID_GEN_DIRECTED_FRAMES_XMIT,
88 OID_GEN_MULTICAST_BYTES_XMIT,
89 OID_GEN_MULTICAST_FRAMES_XMIT,
90 OID_GEN_BROADCAST_BYTES_XMIT,
91 OID_GEN_BROADCAST_FRAMES_XMIT,
92 OID_GEN_DIRECTED_BYTES_RCV,
93 OID_GEN_DIRECTED_FRAMES_RCV,
94 OID_GEN_MULTICAST_BYTES_RCV,
95 OID_GEN_MULTICAST_FRAMES_RCV,
96 OID_GEN_BROADCAST_BYTES_RCV,
97 OID_GEN_BROADCAST_FRAMES_RCV,
98 OID_GEN_RCV_CRC_ERROR,
99 OID_GEN_TRANSMIT_QUEUE_LENGTH,
100 #endif /* RNDIS_OPTIONAL_STATS */
102 /* mandatory 802.3 */
103 /* the general stuff */
104 OID_802_3_PERMANENT_ADDRESS,
105 OID_802_3_CURRENT_ADDRESS,
106 OID_802_3_MULTICAST_LIST,
107 OID_802_3_MAC_OPTIONS,
108 OID_802_3_MAXIMUM_LIST_SIZE,
110 /* the statistical stuff */
111 OID_802_3_RCV_ERROR_ALIGNMENT,
112 OID_802_3_XMIT_ONE_COLLISION,
113 OID_802_3_XMIT_MORE_COLLISIONS,
114 #ifdef RNDIS_OPTIONAL_STATS
115 OID_802_3_XMIT_DEFERRED,
116 OID_802_3_XMIT_MAX_COLLISIONS,
117 OID_802_3_RCV_OVERRUN,
118 OID_802_3_XMIT_UNDERRUN,
119 OID_802_3_XMIT_HEARTBEAT_FAILURE,
120 OID_802_3_XMIT_TIMES_CRS_LOST,
121 OID_802_3_XMIT_LATE_COLLISIONS,
122 #endif /* RNDIS_OPTIONAL_STATS */
125 /* PM and wakeup are mandatory for USB: */
127 /* power management */
128 OID_PNP_CAPABILITIES,
134 OID_PNP_ENABLE_WAKE_UP,
135 OID_PNP_ADD_WAKE_UP_PATTERN,
136 OID_PNP_REMOVE_WAKE_UP_PATTERN,
137 #endif /* RNDIS_WAKEUP */
138 #endif /* RNDIS_PM */
143 static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
144 unsigned buf_len, rndis_resp_t *r)
146 int retval = -ENOTSUPP;
147 u32 length = 4; /* usually */
150 rndis_query_cmplt_type *resp;
151 rndis_params *params;
155 resp = (rndis_query_cmplt_type *) r->buf;
160 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
162 debug("query OID %08x value, len %d:\n", OID, buf_len);
163 for (i = 0; i < buf_len; i += 16) {
164 debug("%03d: %08x %08x %08x %08x\n", i,
165 get_unaligned_le32(&buf[i]),
166 get_unaligned_le32(&buf[i + 4]),
167 get_unaligned_le32(&buf[i + 8]),
168 get_unaligned_le32(&buf[i + 12]));
173 /* response goes here, right after the header */
174 outbuf = (__le32 *) &resp[1];
175 resp->InformationBufferOffset = __constant_cpu_to_le32(16);
177 params = &rndis_per_dev_params[configNr];
180 /* general oids (table 4-1) */
183 case OID_GEN_SUPPORTED_LIST:
184 debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
185 length = sizeof(oid_supported_list);
186 count = length / sizeof(u32);
187 for (i = 0; i < count; i++)
188 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
193 case OID_GEN_HARDWARE_STATUS:
194 debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
197 * Hardware must be ready to receive high level protocols.
199 * reddite ergo quae sunt Caesaris Caesari
200 * et quae sunt Dei Deo!
202 *outbuf = __constant_cpu_to_le32(0);
207 case OID_GEN_MEDIA_SUPPORTED:
208 debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
209 *outbuf = cpu_to_le32(params->medium);
214 case OID_GEN_MEDIA_IN_USE:
215 debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
216 /* one medium, one transport... (maybe you do it better) */
217 *outbuf = cpu_to_le32(params->medium);
222 case OID_GEN_MAXIMUM_FRAME_SIZE:
223 debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
225 *outbuf = cpu_to_le32(params->mtu);
231 case OID_GEN_LINK_SPEED:
232 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
233 debug("%s: OID_GEN_LINK_SPEED\n", __func__);
235 if (params->media_state == NDIS_MEDIA_STATE_DISCONNECTED)
236 *outbuf = __constant_cpu_to_le32(0);
238 *outbuf = cpu_to_le32(params->speed);
243 case OID_GEN_TRANSMIT_BLOCK_SIZE:
244 debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
246 *outbuf = cpu_to_le32(params->mtu);
252 case OID_GEN_RECEIVE_BLOCK_SIZE:
253 debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
255 *outbuf = cpu_to_le32(params->mtu);
261 case OID_GEN_VENDOR_ID:
262 debug("%s: OID_GEN_VENDOR_ID\n", __func__);
263 *outbuf = cpu_to_le32(params->vendorID);
268 case OID_GEN_VENDOR_DESCRIPTION:
269 debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
270 length = strlen(params->vendorDescr);
271 memcpy(outbuf, params->vendorDescr, length);
275 case OID_GEN_VENDOR_DRIVER_VERSION:
276 debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
278 *outbuf = rndis_driver_version;
283 case OID_GEN_CURRENT_PACKET_FILTER:
284 debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
285 *outbuf = cpu_to_le32(*params->filter);
290 case OID_GEN_MAXIMUM_TOTAL_SIZE:
291 debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
292 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
297 case OID_GEN_MEDIA_CONNECT_STATUS:
298 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
299 debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
301 *outbuf = cpu_to_le32(params->media_state);
305 case OID_GEN_PHYSICAL_MEDIUM:
306 debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
307 *outbuf = __constant_cpu_to_le32(0);
312 * The RNDIS specification is incomplete/wrong. Some versions
313 * of MS-Windows expect OIDs that aren't specified there. Other
314 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
316 case OID_GEN_MAC_OPTIONS: /* from WinME */
317 debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
318 *outbuf = __constant_cpu_to_le32(
319 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
320 | NDIS_MAC_OPTION_FULL_DUPLEX);
324 /* statistics OIDs (table 4-2) */
327 case OID_GEN_XMIT_OK:
328 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
329 debug("%s: OID_GEN_XMIT_OK\n", __func__);
332 *outbuf = cpu_to_le32(
333 params->stats->tx_packets -
334 params->stats->tx_errors -
335 params->stats->tx_dropped);
342 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
343 debug("%s: OID_GEN_RCV_OK\n", __func__);
346 *outbuf = cpu_to_le32(
347 params->stats->rx_packets -
348 params->stats->rx_errors -
349 params->stats->rx_dropped);
355 case OID_GEN_XMIT_ERROR:
356 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
357 debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
360 *outbuf = cpu_to_le32(params->stats->tx_errors);
366 case OID_GEN_RCV_ERROR:
367 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
368 debug("%s: OID_GEN_RCV_ERROR\n", __func__);
371 *outbuf = cpu_to_le32(params->stats->rx_errors);
377 case OID_GEN_RCV_NO_BUFFER:
378 debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
380 *outbuf = cpu_to_le32(params->stats->rx_dropped);
385 #ifdef RNDIS_OPTIONAL_STATS
386 case OID_GEN_DIRECTED_BYTES_XMIT:
387 debug("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __func__);
389 * Aunt Tilly's size of shoes
390 * minus antarctica count of penguins
391 * divided by weight of Alpha Centauri
394 *outbuf = cpu_to_le32(
395 (params->stats->tx_packets -
396 params->stats->tx_errors -
397 params->stats->tx_dropped)
403 case OID_GEN_DIRECTED_FRAMES_XMIT:
404 debug("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __func__);
407 *outbuf = cpu_to_le32(
408 (params->stats->tx_packets -
409 params->stats->tx_errors -
410 params->stats->tx_dropped)
416 case OID_GEN_MULTICAST_BYTES_XMIT:
417 debug("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __func__);
419 *outbuf = cpu_to_le32(params->stats->multicast * 1234);
424 case OID_GEN_MULTICAST_FRAMES_XMIT:
425 debug("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __func__);
427 *outbuf = cpu_to_le32(params->stats->multicast);
432 case OID_GEN_BROADCAST_BYTES_XMIT:
433 debug("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __func__);
435 *outbuf = cpu_to_le32(params->stats->tx_packets/42*255);
440 case OID_GEN_BROADCAST_FRAMES_XMIT:
441 debug("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __func__);
443 *outbuf = cpu_to_le32(params->stats->tx_packets / 42);
448 case OID_GEN_DIRECTED_BYTES_RCV:
449 debug("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __func__);
450 *outbuf = __constant_cpu_to_le32(0);
454 case OID_GEN_DIRECTED_FRAMES_RCV:
455 debug("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __func__);
456 *outbuf = __constant_cpu_to_le32(0);
460 case OID_GEN_MULTICAST_BYTES_RCV:
461 debug("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __func__);
463 *outbuf = cpu_to_le32(params->stats->multicast * 1111);
468 case OID_GEN_MULTICAST_FRAMES_RCV:
469 debug("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __func__);
471 *outbuf = cpu_to_le32(params->stats->multicast);
476 case OID_GEN_BROADCAST_BYTES_RCV:
477 debug("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __func__);
479 *outbuf = cpu_to_le32(params->stats->rx_packets/42*255);
484 case OID_GEN_BROADCAST_FRAMES_RCV:
485 debug("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __func__);
487 *outbuf = cpu_to_le32(params->stats->rx_packets / 42);
492 case OID_GEN_RCV_CRC_ERROR:
493 debug("%s: OID_GEN_RCV_CRC_ERROR\n", __func__);
495 *outbuf = cpu_to_le32(params->stats->rx_crc_errors);
500 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
501 debug("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __func__);
502 *outbuf = __constant_cpu_to_le32(0);
505 #endif /* RNDIS_OPTIONAL_STATS */
507 /* ieee802.3 OIDs (table 4-3) */
510 case OID_802_3_PERMANENT_ADDRESS:
511 debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
514 memcpy(outbuf, params->host_mac, length);
520 case OID_802_3_CURRENT_ADDRESS:
521 debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
524 memcpy(outbuf, params->host_mac, length);
530 case OID_802_3_MULTICAST_LIST:
531 debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
532 /* Multicast base address only */
533 *outbuf = __constant_cpu_to_le32(0xE0000000);
538 case OID_802_3_MAXIMUM_LIST_SIZE:
539 debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
540 /* Multicast base address only */
541 *outbuf = __constant_cpu_to_le32(1);
545 case OID_802_3_MAC_OPTIONS:
546 debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
549 /* ieee802.3 statistics OIDs (table 4-4) */
552 case OID_802_3_RCV_ERROR_ALIGNMENT:
553 debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
555 *outbuf = cpu_to_le32(params->stats->rx_frame_errors);
561 case OID_802_3_XMIT_ONE_COLLISION:
562 debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
563 *outbuf = __constant_cpu_to_le32(0);
568 case OID_802_3_XMIT_MORE_COLLISIONS:
569 debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
570 *outbuf = __constant_cpu_to_le32(0);
574 #ifdef RNDIS_OPTIONAL_STATS
575 case OID_802_3_XMIT_DEFERRED:
576 debug("%s: OID_802_3_XMIT_DEFERRED\n", __func__);
580 case OID_802_3_XMIT_MAX_COLLISIONS:
581 debug("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __func__);
585 case OID_802_3_RCV_OVERRUN:
586 debug("%s: OID_802_3_RCV_OVERRUN\n", __func__);
590 case OID_802_3_XMIT_UNDERRUN:
591 debug("%s: OID_802_3_XMIT_UNDERRUN\n", __func__);
595 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
596 debug("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __func__);
600 case OID_802_3_XMIT_TIMES_CRS_LOST:
601 debug("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __func__);
605 case OID_802_3_XMIT_LATE_COLLISIONS:
606 debug("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __func__);
609 #endif /* RNDIS_OPTIONAL_STATS */
612 /* power management OIDs (table 4-5) */
613 case OID_PNP_CAPABILITIES:
614 debug("%s: OID_PNP_CAPABILITIES\n", __func__);
616 /* for now, no wakeup capabilities */
617 length = sizeof(struct NDIS_PNP_CAPABILITIES);
618 memset(outbuf, 0, length);
621 case OID_PNP_QUERY_POWER:
622 debug("%s: OID_PNP_QUERY_POWER D%d\n", __func__,
623 get_unaligned_le32(buf) - 1);
625 * only suspend is a real power state, and
626 * it can't be entered by OID_PNP_SET_POWER...
634 debug("%s: query unknown OID 0x%08X\n", __func__, OID);
639 resp->InformationBufferLength = cpu_to_le32(length);
640 r->length = length + sizeof *resp;
641 resp->MessageLength = cpu_to_le32(r->length);
645 static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
648 rndis_set_cmplt_type *resp;
649 int retval = -ENOTSUPP;
650 struct rndis_params *params;
651 #if (defined(DEBUG) && defined(DEBUG_VERBOSE)) || defined(RNDIS_PM)
657 resp = (rndis_set_cmplt_type *) r->buf;
661 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
663 debug("set OID %08x value, len %d:\n", OID, buf_len);
664 for (i = 0; i < buf_len; i += 16) {
665 debug("%03d: %08x %08x %08x %08x\n", i,
666 get_unaligned_le32(&buf[i]),
667 get_unaligned_le32(&buf[i + 4]),
668 get_unaligned_le32(&buf[i + 8]),
669 get_unaligned_le32(&buf[i + 12]));
674 params = &rndis_per_dev_params[configNr];
676 case OID_GEN_CURRENT_PACKET_FILTER:
679 * these NDIS_PACKET_TYPE_* bitflags are shared with
680 * cdc_filter; it's not RNDIS-specific
681 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
682 * PROMISCUOUS, DIRECTED,
683 * MULTICAST, ALL_MULTICAST, BROADCAST
685 *params->filter = (u16) get_unaligned_le32(buf);
686 debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
687 __func__, *params->filter);
690 * this call has a significant side effect: it's
691 * what makes the packet flow start and stop, like
692 * activating the CDC Ethernet altsetting.
699 params->state = RNDIS_DATA_INITIALIZED;
701 params->state = RNDIS_INITIALIZED;
704 case OID_802_3_MULTICAST_LIST:
705 /* I think we can ignore this */
706 debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
710 case OID_GEN_RNDIS_CONFIG_PARAMETER:
712 struct rndis_config_parameter *param;
713 param = (struct rndis_config_parameter *) buf;
714 debug("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
716 min(cpu_to_le32(param->ParameterNameLength), 80),
717 buf + param->ParameterNameOffset);
724 case OID_PNP_SET_POWER:
726 * The only real power state is USB suspend, and RNDIS requests
727 * can't enter it; this one isn't really about power. After
728 * resuming, Windows forces a reset, and then SET_POWER D0.
729 * FIXME ... then things go batty; Windows wedges itself.
731 i = get_unaligned_le32(buf);
732 debug("%s: OID_PNP_SET_POWER D%d\n", __func__, i - 1);
734 case NdisDeviceStateD0:
735 *params->filter = params->saved_filter;
736 goto update_linkstate;
737 case NdisDeviceStateD3:
738 case NdisDeviceStateD2:
739 case NdisDeviceStateD1:
740 params->saved_filter = *params->filter;
748 * no wakeup support advertised, so wakeup OIDs always fail:
749 * - OID_PNP_ENABLE_WAKE_UP
750 * - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
754 #endif /* RNDIS_PM */
757 debug("%s: set unknown OID 0x%08X, size %d\n",
758 __func__, OID, buf_len);
768 static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
770 rndis_init_cmplt_type *resp;
773 if (!rndis_per_dev_params[configNr].dev)
776 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
779 resp = (rndis_init_cmplt_type *) r->buf;
781 resp->MessageType = __constant_cpu_to_le32(
782 REMOTE_NDIS_INITIALIZE_CMPLT);
783 resp->MessageLength = __constant_cpu_to_le32(52);
784 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
785 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
786 resp->MajorVersion = __constant_cpu_to_le32(RNDIS_MAJOR_VERSION);
787 resp->MinorVersion = __constant_cpu_to_le32(RNDIS_MINOR_VERSION);
788 resp->DeviceFlags = __constant_cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
789 resp->Medium = __constant_cpu_to_le32(RNDIS_MEDIUM_802_3);
790 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32(1);
791 resp->MaxTransferSize = cpu_to_le32(
792 rndis_per_dev_params[configNr].mtu
794 + sizeof(struct rndis_packet_msg_type)
796 resp->PacketAlignmentFactor = __constant_cpu_to_le32(0);
797 resp->AFListOffset = __constant_cpu_to_le32(0);
798 resp->AFListSize = __constant_cpu_to_le32(0);
800 if (rndis_per_dev_params[configNr].ack)
801 rndis_per_dev_params[configNr].ack(
802 rndis_per_dev_params[configNr].dev);
807 static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
809 rndis_query_cmplt_type *resp;
812 debug("%s: OID = %08X\n", __func__, get_unaligned_le32(&buf->OID));
813 if (!rndis_per_dev_params[configNr].dev)
817 * we need more memory:
818 * gen_ndis_query_resp expects enough space for
819 * rndis_query_cmplt_type followed by data.
820 * oid_supported_list is the largest data reply
822 r = rndis_add_response(configNr,
823 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
826 resp = (rndis_query_cmplt_type *) r->buf;
828 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
829 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
831 if (gen_ndis_query_resp(configNr, get_unaligned_le32(&buf->OID),
832 get_unaligned_le32(&buf->InformationBufferOffset)
834 get_unaligned_le32(&buf->InformationBufferLength),
836 /* OID not supported */
837 resp->Status = __constant_cpu_to_le32(
838 RNDIS_STATUS_NOT_SUPPORTED);
839 resp->MessageLength = __constant_cpu_to_le32(sizeof *resp);
840 resp->InformationBufferLength = __constant_cpu_to_le32(0);
841 resp->InformationBufferOffset = __constant_cpu_to_le32(0);
843 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
845 if (rndis_per_dev_params[configNr].ack)
846 rndis_per_dev_params[configNr].ack(
847 rndis_per_dev_params[configNr].dev);
851 static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
853 u32 BufLength, BufOffset;
854 rndis_set_cmplt_type *resp;
857 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
860 resp = (rndis_set_cmplt_type *) r->buf;
862 BufLength = get_unaligned_le32(&buf->InformationBufferLength);
863 BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
866 debug("%s: Length: %d\n", __func__, BufLength);
867 debug("%s: Offset: %d\n", __func__, BufOffset);
868 debug("%s: InfoBuffer: ", __func__);
870 for (i = 0; i < BufLength; i++)
871 debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
876 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
877 resp->MessageLength = __constant_cpu_to_le32(16);
878 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
879 if (gen_ndis_set_resp(configNr, get_unaligned_le32(&buf->OID),
880 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
881 resp->Status = __constant_cpu_to_le32(
882 RNDIS_STATUS_NOT_SUPPORTED);
884 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
886 if (rndis_per_dev_params[configNr].ack)
887 rndis_per_dev_params[configNr].ack(
888 rndis_per_dev_params[configNr].dev);
893 static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
895 rndis_reset_cmplt_type *resp;
898 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
901 resp = (rndis_reset_cmplt_type *) r->buf;
903 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
904 resp->MessageLength = __constant_cpu_to_le32(16);
905 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
906 /* resent information */
907 resp->AddressingReset = __constant_cpu_to_le32(1);
909 if (rndis_per_dev_params[configNr].ack)
910 rndis_per_dev_params[configNr].ack(
911 rndis_per_dev_params[configNr].dev);
916 static int rndis_keepalive_response(int configNr,
917 rndis_keepalive_msg_type *buf)
919 rndis_keepalive_cmplt_type *resp;
922 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
924 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
927 resp = (rndis_keepalive_cmplt_type *) r->buf;
929 resp->MessageType = __constant_cpu_to_le32(
930 REMOTE_NDIS_KEEPALIVE_CMPLT);
931 resp->MessageLength = __constant_cpu_to_le32(16);
932 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
933 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
935 if (rndis_per_dev_params[configNr].ack)
936 rndis_per_dev_params[configNr].ack(
937 rndis_per_dev_params[configNr].dev);
944 * Device to Host Comunication
946 static int rndis_indicate_status_msg(int configNr, u32 status)
948 rndis_indicate_status_msg_type *resp;
951 if (rndis_per_dev_params[configNr].state == RNDIS_UNINITIALIZED)
954 r = rndis_add_response(configNr,
955 sizeof(rndis_indicate_status_msg_type));
958 resp = (rndis_indicate_status_msg_type *) r->buf;
960 resp->MessageType = __constant_cpu_to_le32(
961 REMOTE_NDIS_INDICATE_STATUS_MSG);
962 resp->MessageLength = __constant_cpu_to_le32(20);
963 resp->Status = cpu_to_le32(status);
964 resp->StatusBufferLength = __constant_cpu_to_le32(0);
965 resp->StatusBufferOffset = __constant_cpu_to_le32(0);
967 if (rndis_per_dev_params[configNr].ack)
968 rndis_per_dev_params[configNr].ack(
969 rndis_per_dev_params[configNr].dev);
973 int rndis_signal_connect(int configNr)
975 rndis_per_dev_params[configNr].media_state
976 = NDIS_MEDIA_STATE_CONNECTED;
977 return rndis_indicate_status_msg(configNr,
978 RNDIS_STATUS_MEDIA_CONNECT);
981 int rndis_signal_disconnect(int configNr)
983 rndis_per_dev_params[configNr].media_state
984 = NDIS_MEDIA_STATE_DISCONNECTED;
986 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
987 return rndis_indicate_status_msg(configNr,
988 RNDIS_STATUS_MEDIA_DISCONNECT);
994 void rndis_uninit(int configNr)
999 if (configNr >= RNDIS_MAX_CONFIGS)
1001 rndis_per_dev_params[configNr].used = 0;
1002 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
1004 /* drain the response queue */
1005 while ((buf = rndis_get_next_response(configNr, &length)))
1006 rndis_free_response(configNr, buf);
1009 void rndis_set_host_mac(int configNr, const u8 *addr)
1011 rndis_per_dev_params[configNr].host_mac = addr;
1014 enum rndis_state rndis_get_state(int configNr)
1016 if (configNr >= RNDIS_MAX_CONFIGS || configNr < 0)
1018 return rndis_per_dev_params[configNr].state;
1024 int rndis_msg_parser(u8 configNr, u8 *buf)
1026 u32 MsgType, MsgLength;
1028 struct rndis_params *params;
1030 debug("%s: configNr = %d, %p\n", __func__, configNr, buf);
1035 tmp = (__le32 *) buf;
1036 MsgType = get_unaligned_le32(tmp++);
1037 MsgLength = get_unaligned_le32(tmp++);
1039 if (configNr >= RNDIS_MAX_CONFIGS)
1041 params = &rndis_per_dev_params[configNr];
1044 * NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1045 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1046 * and normal HC level polling to see if there's any IN traffic.
1049 /* For USB: responses may take up to 10 seconds */
1051 case REMOTE_NDIS_INITIALIZE_MSG:
1052 debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n", __func__);
1053 params->state = RNDIS_INITIALIZED;
1054 return rndis_init_response(configNr,
1055 (rndis_init_msg_type *) buf);
1057 case REMOTE_NDIS_HALT_MSG:
1058 debug("%s: REMOTE_NDIS_HALT_MSG\n", __func__);
1059 params->state = RNDIS_UNINITIALIZED;
1062 case REMOTE_NDIS_QUERY_MSG:
1063 return rndis_query_response(configNr,
1064 (rndis_query_msg_type *) buf);
1066 case REMOTE_NDIS_SET_MSG:
1067 return rndis_set_response(configNr,
1068 (rndis_set_msg_type *) buf);
1070 case REMOTE_NDIS_RESET_MSG:
1071 debug("%s: REMOTE_NDIS_RESET_MSG\n", __func__);
1072 return rndis_reset_response(configNr,
1073 (rndis_reset_msg_type *) buf);
1075 case REMOTE_NDIS_KEEPALIVE_MSG:
1076 /* For USB: host does this every 5 seconds */
1077 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
1078 debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n", __func__);
1080 return rndis_keepalive_response(configNr,
1081 (rndis_keepalive_msg_type *) buf);
1085 * At least Windows XP emits some undefined RNDIS messages.
1086 * In one case those messages seemed to relate to the host
1087 * suspending itself.
1089 debug("%s: unknown RNDIS message 0x%08X len %d\n",
1090 __func__ , MsgType, MsgLength);
1093 for (i = 0; i < MsgLength; i += 16) {
1095 " %02x %02x %02x %02x"
1096 " %02x %02x %02x %02x"
1097 " %02x %02x %02x %02x"
1098 " %02x %02x %02x %02x"
1106 buf[i+10], buf[i+11],
1107 buf[i+12], buf[i+13],
1108 buf[i+14], buf[i+15]);
1117 #ifndef CONFIG_DM_ETH
1118 int rndis_register(int (*rndis_control_ack)(struct eth_device *))
1120 int rndis_register(int (*rndis_control_ack)(struct udevice *))
1125 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1126 if (!rndis_per_dev_params[i].used) {
1127 rndis_per_dev_params[i].used = 1;
1128 rndis_per_dev_params[i].ack = rndis_control_ack;
1129 debug("%s: configNr = %d\n", __func__, i);
1133 debug("%s failed\n", __func__);
1138 void rndis_deregister(int configNr)
1140 debug("%s: configNr = %d\n", __func__, configNr);
1142 if (configNr >= RNDIS_MAX_CONFIGS)
1144 rndis_per_dev_params[configNr].used = 0;
1149 #ifndef CONFIG_DM_ETH
1150 int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
1151 struct net_device_stats *stats, u16 *cdc_filter)
1153 int rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
1154 struct net_device_stats *stats, u16 *cdc_filter)
1157 debug("%s: configNr = %d\n", __func__, configNr);
1160 if (configNr >= RNDIS_MAX_CONFIGS)
1163 rndis_per_dev_params[configNr].dev = dev;
1164 rndis_per_dev_params[configNr].stats = stats;
1165 rndis_per_dev_params[configNr].mtu = mtu;
1166 rndis_per_dev_params[configNr].filter = cdc_filter;
1171 int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
1173 debug("%s: configNr = %d\n", __func__, configNr);
1176 if (configNr >= RNDIS_MAX_CONFIGS)
1179 rndis_per_dev_params[configNr].vendorID = vendorID;
1180 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
1185 int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
1187 debug("%s: configNr = %d, %u %u\n", __func__, configNr, medium, speed);
1188 if (configNr >= RNDIS_MAX_CONFIGS)
1191 rndis_per_dev_params[configNr].medium = medium;
1192 rndis_per_dev_params[configNr].speed = speed;
1197 void rndis_add_hdr(void *buf, int length)
1199 struct rndis_packet_msg_type *header;
1202 memset(header, 0, sizeof *header);
1203 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1204 header->MessageLength = cpu_to_le32(length + sizeof *header);
1205 header->DataOffset = __constant_cpu_to_le32(36);
1206 header->DataLength = cpu_to_le32(length);
1209 void rndis_free_response(int configNr, u8 *buf)
1212 struct list_head *act, *tmp;
1214 list_for_each_safe(act, tmp,
1215 &(rndis_per_dev_params[configNr].resp_queue))
1217 r = list_entry(act, rndis_resp_t, list);
1218 if (r && r->buf == buf) {
1225 u8 *rndis_get_next_response(int configNr, u32 *length)
1228 struct list_head *act, *tmp;
1233 list_for_each_safe(act, tmp,
1234 &(rndis_per_dev_params[configNr].resp_queue))
1236 r = list_entry(act, rndis_resp_t, list);
1239 *length = r->length;
1247 static rndis_resp_t *rndis_add_response(int configNr, u32 length)
1251 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1252 r = malloc(sizeof(rndis_resp_t) + length);
1256 r->buf = (u8 *) (r + 1);
1260 list_add_tail(&r->list,
1261 &(rndis_per_dev_params[configNr].resp_queue));
1265 int rndis_rm_hdr(void *buf, int length)
1267 /* tmp points to a struct rndis_packet_msg_type */
1271 /* MessageType, MessageLength */
1272 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1273 != get_unaligned(tmp++))
1277 /* DataOffset, DataLength */
1278 offs = get_unaligned_le32(tmp++) + 8 /* offset of DataOffset */;
1279 if (offs != sizeof(struct rndis_packet_msg_type))
1280 debug("%s: unexpected DataOffset: %d\n", __func__, offs);
1284 len = get_unaligned_le32(tmp++);
1285 if (len + sizeof(struct rndis_packet_msg_type) != length)
1286 debug("%s: unexpected DataLength: %d, packet length=%d\n",
1287 __func__, len, length);
1289 memmove(buf, buf + offs, len);
1294 int rndis_init(void)
1298 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1299 rndis_per_dev_params[i].confignr = i;
1300 rndis_per_dev_params[i].used = 0;
1301 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1302 rndis_per_dev_params[i].media_state
1303 = NDIS_MEDIA_STATE_DISCONNECTED;
1304 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
1310 void rndis_exit(void)