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.
12 * Fixed message length bug in init_response
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 BufLength = get_unaligned_le32(&buf->InformationBufferLength);
858 BufOffset = get_unaligned_le32(&buf->InformationBufferOffset);
859 if ((BufOffset > RNDIS_MAX_TOTAL_SIZE - 8) ||
860 (BufLength > RNDIS_MAX_TOTAL_SIZE - 8 - BufOffset))
863 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
866 resp = (rndis_set_cmplt_type *) r->buf;
869 debug("%s: Length: %d\n", __func__, BufLength);
870 debug("%s: Offset: %d\n", __func__, BufOffset);
871 debug("%s: InfoBuffer: ", __func__);
873 for (i = 0; i < BufLength; i++)
874 debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
879 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
880 resp->MessageLength = __constant_cpu_to_le32(16);
881 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
882 if (gen_ndis_set_resp(configNr, get_unaligned_le32(&buf->OID),
883 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
884 resp->Status = __constant_cpu_to_le32(
885 RNDIS_STATUS_NOT_SUPPORTED);
887 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
889 if (rndis_per_dev_params[configNr].ack)
890 rndis_per_dev_params[configNr].ack(
891 rndis_per_dev_params[configNr].dev);
896 static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
898 rndis_reset_cmplt_type *resp;
901 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
904 resp = (rndis_reset_cmplt_type *) r->buf;
906 resp->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
907 resp->MessageLength = __constant_cpu_to_le32(16);
908 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
909 /* resent information */
910 resp->AddressingReset = __constant_cpu_to_le32(1);
912 if (rndis_per_dev_params[configNr].ack)
913 rndis_per_dev_params[configNr].ack(
914 rndis_per_dev_params[configNr].dev);
919 static int rndis_keepalive_response(int configNr,
920 rndis_keepalive_msg_type *buf)
922 rndis_keepalive_cmplt_type *resp;
925 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
927 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
930 resp = (rndis_keepalive_cmplt_type *) r->buf;
932 resp->MessageType = __constant_cpu_to_le32(
933 REMOTE_NDIS_KEEPALIVE_CMPLT);
934 resp->MessageLength = __constant_cpu_to_le32(16);
935 resp->RequestID = get_unaligned(&buf->RequestID); /* Still LE in msg buffer */
936 resp->Status = __constant_cpu_to_le32(RNDIS_STATUS_SUCCESS);
938 if (rndis_per_dev_params[configNr].ack)
939 rndis_per_dev_params[configNr].ack(
940 rndis_per_dev_params[configNr].dev);
947 * Device to Host Comunication
949 static int rndis_indicate_status_msg(int configNr, u32 status)
951 rndis_indicate_status_msg_type *resp;
954 if (rndis_per_dev_params[configNr].state == RNDIS_UNINITIALIZED)
957 r = rndis_add_response(configNr,
958 sizeof(rndis_indicate_status_msg_type));
961 resp = (rndis_indicate_status_msg_type *) r->buf;
963 resp->MessageType = __constant_cpu_to_le32(
964 REMOTE_NDIS_INDICATE_STATUS_MSG);
965 resp->MessageLength = __constant_cpu_to_le32(20);
966 resp->Status = cpu_to_le32(status);
967 resp->StatusBufferLength = __constant_cpu_to_le32(0);
968 resp->StatusBufferOffset = __constant_cpu_to_le32(0);
970 if (rndis_per_dev_params[configNr].ack)
971 rndis_per_dev_params[configNr].ack(
972 rndis_per_dev_params[configNr].dev);
976 int rndis_signal_connect(int configNr)
978 rndis_per_dev_params[configNr].media_state
979 = NDIS_MEDIA_STATE_CONNECTED;
980 return rndis_indicate_status_msg(configNr,
981 RNDIS_STATUS_MEDIA_CONNECT);
984 int rndis_signal_disconnect(int configNr)
986 rndis_per_dev_params[configNr].media_state
987 = NDIS_MEDIA_STATE_DISCONNECTED;
989 #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
990 return rndis_indicate_status_msg(configNr,
991 RNDIS_STATUS_MEDIA_DISCONNECT);
997 void rndis_uninit(int configNr)
1002 if (configNr >= RNDIS_MAX_CONFIGS)
1004 rndis_per_dev_params[configNr].used = 0;
1005 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
1007 /* drain the response queue */
1008 while ((buf = rndis_get_next_response(configNr, &length)))
1009 rndis_free_response(configNr, buf);
1012 void rndis_set_host_mac(int configNr, const u8 *addr)
1014 rndis_per_dev_params[configNr].host_mac = addr;
1017 enum rndis_state rndis_get_state(int configNr)
1019 if (configNr >= RNDIS_MAX_CONFIGS || configNr < 0)
1021 return rndis_per_dev_params[configNr].state;
1027 int rndis_msg_parser(u8 configNr, u8 *buf)
1029 u32 MsgType, MsgLength;
1031 struct rndis_params *params;
1033 debug("%s: configNr = %d, %p\n", __func__, configNr, buf);
1038 tmp = (__le32 *) buf;
1039 MsgType = get_unaligned_le32(tmp++);
1040 MsgLength = get_unaligned_le32(tmp++);
1042 if (configNr >= RNDIS_MAX_CONFIGS)
1044 params = &rndis_per_dev_params[configNr];
1047 * NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1048 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1049 * and normal HC level polling to see if there's any IN traffic.
1052 /* For USB: responses may take up to 10 seconds */
1054 case REMOTE_NDIS_INITIALIZE_MSG:
1055 debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n", __func__);
1056 params->state = RNDIS_INITIALIZED;
1057 return rndis_init_response(configNr,
1058 (rndis_init_msg_type *) buf);
1060 case REMOTE_NDIS_HALT_MSG:
1061 debug("%s: REMOTE_NDIS_HALT_MSG\n", __func__);
1062 params->state = RNDIS_UNINITIALIZED;
1065 case REMOTE_NDIS_QUERY_MSG:
1066 return rndis_query_response(configNr,
1067 (rndis_query_msg_type *) buf);
1069 case REMOTE_NDIS_SET_MSG:
1070 return rndis_set_response(configNr,
1071 (rndis_set_msg_type *) buf);
1073 case REMOTE_NDIS_RESET_MSG:
1074 debug("%s: REMOTE_NDIS_RESET_MSG\n", __func__);
1075 return rndis_reset_response(configNr,
1076 (rndis_reset_msg_type *) buf);
1078 case REMOTE_NDIS_KEEPALIVE_MSG:
1079 /* For USB: host does this every 5 seconds */
1080 #if defined(DEBUG) && defined(DEBUG_VERBOSE)
1081 debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n", __func__);
1083 return rndis_keepalive_response(configNr,
1084 (rndis_keepalive_msg_type *) buf);
1088 * At least Windows XP emits some undefined RNDIS messages.
1089 * In one case those messages seemed to relate to the host
1090 * suspending itself.
1092 debug("%s: unknown RNDIS message 0x%08X len %d\n",
1093 __func__ , MsgType, MsgLength);
1096 for (i = 0; i < MsgLength; i += 16) {
1098 " %02x %02x %02x %02x"
1099 " %02x %02x %02x %02x"
1100 " %02x %02x %02x %02x"
1101 " %02x %02x %02x %02x"
1109 buf[i+10], buf[i+11],
1110 buf[i+12], buf[i+13],
1111 buf[i+14], buf[i+15]);
1120 int rndis_register(int (*rndis_control_ack)(struct udevice *))
1124 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1125 if (!rndis_per_dev_params[i].used) {
1126 rndis_per_dev_params[i].used = 1;
1127 rndis_per_dev_params[i].ack = rndis_control_ack;
1128 debug("%s: configNr = %d\n", __func__, i);
1132 debug("%s failed\n", __func__);
1137 void rndis_deregister(int configNr)
1139 debug("%s: configNr = %d\n", __func__, configNr);
1141 if (configNr >= RNDIS_MAX_CONFIGS)
1143 rndis_per_dev_params[configNr].used = 0;
1148 int rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
1149 struct net_device_stats *stats, u16 *cdc_filter)
1151 debug("%s: configNr = %d\n", __func__, configNr);
1154 if (configNr >= RNDIS_MAX_CONFIGS)
1157 rndis_per_dev_params[configNr].dev = dev;
1158 rndis_per_dev_params[configNr].stats = stats;
1159 rndis_per_dev_params[configNr].mtu = mtu;
1160 rndis_per_dev_params[configNr].filter = cdc_filter;
1165 int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
1167 debug("%s: configNr = %d\n", __func__, configNr);
1170 if (configNr >= RNDIS_MAX_CONFIGS)
1173 rndis_per_dev_params[configNr].vendorID = vendorID;
1174 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
1179 int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
1181 debug("%s: configNr = %d, %u %u\n", __func__, configNr, medium, speed);
1182 if (configNr >= RNDIS_MAX_CONFIGS)
1185 rndis_per_dev_params[configNr].medium = medium;
1186 rndis_per_dev_params[configNr].speed = speed;
1191 void rndis_add_hdr(void *buf, int length)
1193 struct rndis_packet_msg_type *header;
1196 memset(header, 0, sizeof *header);
1197 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1198 header->MessageLength = cpu_to_le32(length + sizeof *header);
1199 header->DataOffset = __constant_cpu_to_le32(36);
1200 header->DataLength = cpu_to_le32(length);
1203 void rndis_free_response(int configNr, u8 *buf)
1206 struct list_head *act, *tmp;
1208 list_for_each_safe(act, tmp,
1209 &(rndis_per_dev_params[configNr].resp_queue))
1211 r = list_entry(act, rndis_resp_t, list);
1212 if (r && r->buf == buf) {
1219 u8 *rndis_get_next_response(int configNr, u32 *length)
1222 struct list_head *act, *tmp;
1227 list_for_each_safe(act, tmp,
1228 &(rndis_per_dev_params[configNr].resp_queue))
1230 r = list_entry(act, rndis_resp_t, list);
1233 *length = r->length;
1241 static rndis_resp_t *rndis_add_response(int configNr, u32 length)
1245 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1246 r = malloc(sizeof(rndis_resp_t) + length);
1250 r->buf = (u8 *) (r + 1);
1254 list_add_tail(&r->list,
1255 &(rndis_per_dev_params[configNr].resp_queue));
1259 int rndis_rm_hdr(void *buf, int length)
1261 /* tmp points to a struct rndis_packet_msg_type */
1265 /* MessageType, MessageLength */
1266 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1267 != get_unaligned(tmp++))
1271 /* DataOffset, DataLength */
1272 offs = get_unaligned_le32(tmp++) + 8 /* offset of DataOffset */;
1273 if (offs != sizeof(struct rndis_packet_msg_type))
1274 debug("%s: unexpected DataOffset: %d\n", __func__, offs);
1278 len = get_unaligned_le32(tmp++);
1279 if (len + sizeof(struct rndis_packet_msg_type) != length)
1280 debug("%s: unexpected DataLength: %d, packet length=%d\n",
1281 __func__, len, length);
1283 memmove(buf, buf + offs, len);
1288 int rndis_init(void)
1292 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1293 rndis_per_dev_params[i].confignr = i;
1294 rndis_per_dev_params[i].used = 0;
1295 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1296 rndis_per_dev_params[i].media_state
1297 = NDIS_MEDIA_STATE_DISCONNECTED;
1298 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
1304 void rndis_exit(void)