4 * Version: $Id: rndis.c,v 1.19 2004/03/25 21:33:46 robert Exp $
6 * Authors: Benedikt Spranger, Pengutronix
7 * Robert Schwebel, Pengutronix
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2, as published by the Free Software Foundation.
13 * This software was originally developed in conformance with
14 * Microsoft's Remote NDIS Specification License Agreement.
16 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
17 * Fixed message length bug in init_response
19 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
20 * Fixed rndis_rm_hdr length bug.
22 * Copyright (C) 2004 by David Brownell
23 * updates to merge with Linux 2.6, better match RNDIS spec
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/proc_fs.h>
33 #include <linux/netdevice.h>
36 #include <asm/byteorder.h>
37 #include <asm/system.h>
38 #include <asm/unaligned.h>
48 /* The driver for your USB chip needs to support ep0 OUT to work with
49 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
51 * Windows hosts need an INF file like Documentation/usb/linux.inf
52 * and will be happier if you provide the host_addr module parameter.
56 #define DBG(str,args...) do { \
58 printk(KERN_DEBUG str , ## args ); \
60 static int rndis_debug = 0;
62 module_param (rndis_debug, int, 0);
63 MODULE_PARM_DESC (rndis_debug, "enable debugging");
68 #define DBG(str,args...) do{}while(0)
71 #define RNDIS_MAX_CONFIGS 1
74 static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
77 static const __le32 rndis_driver_version = __constant_cpu_to_le32 (1);
79 /* Function Prototypes */
80 static rndis_resp_t *rndis_add_response (int configNr, u32 length);
84 static const u32 oid_supported_list [] =
86 /* the general stuff */
87 OID_GEN_SUPPORTED_LIST,
88 OID_GEN_HARDWARE_STATUS,
89 OID_GEN_MEDIA_SUPPORTED,
91 OID_GEN_MAXIMUM_FRAME_SIZE,
93 OID_GEN_TRANSMIT_BLOCK_SIZE,
94 OID_GEN_RECEIVE_BLOCK_SIZE,
96 OID_GEN_VENDOR_DESCRIPTION,
97 OID_GEN_VENDOR_DRIVER_VERSION,
98 OID_GEN_CURRENT_PACKET_FILTER,
99 OID_GEN_MAXIMUM_TOTAL_SIZE,
100 OID_GEN_MEDIA_CONNECT_STATUS,
101 OID_GEN_PHYSICAL_MEDIUM,
103 OID_GEN_RNDIS_CONFIG_PARAMETER,
106 /* the statistical stuff */
111 OID_GEN_RCV_NO_BUFFER,
112 #ifdef RNDIS_OPTIONAL_STATS
113 OID_GEN_DIRECTED_BYTES_XMIT,
114 OID_GEN_DIRECTED_FRAMES_XMIT,
115 OID_GEN_MULTICAST_BYTES_XMIT,
116 OID_GEN_MULTICAST_FRAMES_XMIT,
117 OID_GEN_BROADCAST_BYTES_XMIT,
118 OID_GEN_BROADCAST_FRAMES_XMIT,
119 OID_GEN_DIRECTED_BYTES_RCV,
120 OID_GEN_DIRECTED_FRAMES_RCV,
121 OID_GEN_MULTICAST_BYTES_RCV,
122 OID_GEN_MULTICAST_FRAMES_RCV,
123 OID_GEN_BROADCAST_BYTES_RCV,
124 OID_GEN_BROADCAST_FRAMES_RCV,
125 OID_GEN_RCV_CRC_ERROR,
126 OID_GEN_TRANSMIT_QUEUE_LENGTH,
127 #endif /* RNDIS_OPTIONAL_STATS */
129 /* mandatory 802.3 */
130 /* the general stuff */
131 OID_802_3_PERMANENT_ADDRESS,
132 OID_802_3_CURRENT_ADDRESS,
133 OID_802_3_MULTICAST_LIST,
134 OID_802_3_MAC_OPTIONS,
135 OID_802_3_MAXIMUM_LIST_SIZE,
137 /* the statistical stuff */
138 OID_802_3_RCV_ERROR_ALIGNMENT,
139 OID_802_3_XMIT_ONE_COLLISION,
140 OID_802_3_XMIT_MORE_COLLISIONS,
141 #ifdef RNDIS_OPTIONAL_STATS
142 OID_802_3_XMIT_DEFERRED,
143 OID_802_3_XMIT_MAX_COLLISIONS,
144 OID_802_3_RCV_OVERRUN,
145 OID_802_3_XMIT_UNDERRUN,
146 OID_802_3_XMIT_HEARTBEAT_FAILURE,
147 OID_802_3_XMIT_TIMES_CRS_LOST,
148 OID_802_3_XMIT_LATE_COLLISIONS,
149 #endif /* RNDIS_OPTIONAL_STATS */
152 /* PM and wakeup are mandatory for USB: */
154 /* power management */
155 OID_PNP_CAPABILITIES,
161 OID_PNP_ENABLE_WAKE_UP,
162 OID_PNP_ADD_WAKE_UP_PATTERN,
163 OID_PNP_REMOVE_WAKE_UP_PATTERN,
164 #endif /* RNDIS_WAKEUP */
165 #endif /* RNDIS_PM */
171 gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
174 int retval = -ENOTSUPP;
175 u32 length = 4; /* usually */
178 rndis_query_cmplt_type *resp;
180 if (!r) return -ENOMEM;
181 resp = (rndis_query_cmplt_type *) r->buf;
183 if (!resp) return -ENOMEM;
185 if (buf_len && rndis_debug > 1) {
186 DBG("query OID %08x value, len %d:\n", OID, buf_len);
187 for (i = 0; i < buf_len; i += 16) {
188 DBG("%03d: %08x %08x %08x %08x\n", i,
189 le32_to_cpu(get_unaligned((__le32 *)
191 le32_to_cpu(get_unaligned((__le32 *)
193 le32_to_cpu(get_unaligned((__le32 *)
195 le32_to_cpu(get_unaligned((__le32 *)
200 /* response goes here, right after the header */
201 outbuf = (__le32 *) &resp[1];
202 resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
206 /* general oids (table 4-1) */
209 case OID_GEN_SUPPORTED_LIST:
210 DBG("%s: OID_GEN_SUPPORTED_LIST\n", __FUNCTION__);
211 length = sizeof (oid_supported_list);
212 count = length / sizeof (u32);
213 for (i = 0; i < count; i++)
214 outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
219 case OID_GEN_HARDWARE_STATUS:
220 DBG("%s: OID_GEN_HARDWARE_STATUS\n", __FUNCTION__);
222 * Hardware must be ready to receive high level protocols.
224 * reddite ergo quae sunt Caesaris Caesari
225 * et quae sunt Dei Deo!
227 *outbuf = __constant_cpu_to_le32 (0);
232 case OID_GEN_MEDIA_SUPPORTED:
233 DBG("%s: OID_GEN_MEDIA_SUPPORTED\n", __FUNCTION__);
234 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
239 case OID_GEN_MEDIA_IN_USE:
240 DBG("%s: OID_GEN_MEDIA_IN_USE\n", __FUNCTION__);
241 /* one medium, one transport... (maybe you do it better) */
242 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
247 case OID_GEN_MAXIMUM_FRAME_SIZE:
248 DBG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __FUNCTION__);
249 if (rndis_per_dev_params [configNr].dev) {
250 *outbuf = cpu_to_le32 (
251 rndis_per_dev_params [configNr].dev->mtu);
257 case OID_GEN_LINK_SPEED:
259 DBG("%s: OID_GEN_LINK_SPEED\n", __FUNCTION__);
260 if (rndis_per_dev_params [configNr].media_state
261 == NDIS_MEDIA_STATE_DISCONNECTED)
262 *outbuf = __constant_cpu_to_le32 (0);
264 *outbuf = cpu_to_le32 (
265 rndis_per_dev_params [configNr].speed);
270 case OID_GEN_TRANSMIT_BLOCK_SIZE:
271 DBG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __FUNCTION__);
272 if (rndis_per_dev_params [configNr].dev) {
273 *outbuf = cpu_to_le32 (
274 rndis_per_dev_params [configNr].dev->mtu);
280 case OID_GEN_RECEIVE_BLOCK_SIZE:
281 DBG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __FUNCTION__);
282 if (rndis_per_dev_params [configNr].dev) {
283 *outbuf = cpu_to_le32 (
284 rndis_per_dev_params [configNr].dev->mtu);
290 case OID_GEN_VENDOR_ID:
291 DBG("%s: OID_GEN_VENDOR_ID\n", __FUNCTION__);
292 *outbuf = cpu_to_le32 (
293 rndis_per_dev_params [configNr].vendorID);
298 case OID_GEN_VENDOR_DESCRIPTION:
299 DBG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __FUNCTION__);
300 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
302 rndis_per_dev_params [configNr].vendorDescr, length);
306 case OID_GEN_VENDOR_DRIVER_VERSION:
307 DBG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __FUNCTION__);
309 *outbuf = rndis_driver_version;
314 case OID_GEN_CURRENT_PACKET_FILTER:
315 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __FUNCTION__);
316 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
321 case OID_GEN_MAXIMUM_TOTAL_SIZE:
322 DBG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __FUNCTION__);
323 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
328 case OID_GEN_MEDIA_CONNECT_STATUS:
330 DBG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __FUNCTION__);
331 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
336 case OID_GEN_PHYSICAL_MEDIUM:
337 DBG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __FUNCTION__);
338 *outbuf = __constant_cpu_to_le32 (0);
342 /* The RNDIS specification is incomplete/wrong. Some versions
343 * of MS-Windows expect OIDs that aren't specified there. Other
344 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
346 case OID_GEN_MAC_OPTIONS: /* from WinME */
347 DBG("%s: OID_GEN_MAC_OPTIONS\n", __FUNCTION__);
348 *outbuf = __constant_cpu_to_le32(
349 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
350 | NDIS_MAC_OPTION_FULL_DUPLEX);
354 /* statistics OIDs (table 4-2) */
357 case OID_GEN_XMIT_OK:
359 DBG("%s: OID_GEN_XMIT_OK\n", __FUNCTION__);
360 if (rndis_per_dev_params [configNr].stats) {
361 *outbuf = cpu_to_le32 (
362 rndis_per_dev_params [configNr].stats->tx_packets -
363 rndis_per_dev_params [configNr].stats->tx_errors -
364 rndis_per_dev_params [configNr].stats->tx_dropped);
372 DBG("%s: OID_GEN_RCV_OK\n", __FUNCTION__);
373 if (rndis_per_dev_params [configNr].stats) {
374 *outbuf = cpu_to_le32 (
375 rndis_per_dev_params [configNr].stats->rx_packets -
376 rndis_per_dev_params [configNr].stats->rx_errors -
377 rndis_per_dev_params [configNr].stats->rx_dropped);
383 case OID_GEN_XMIT_ERROR:
385 DBG("%s: OID_GEN_XMIT_ERROR\n", __FUNCTION__);
386 if (rndis_per_dev_params [configNr].stats) {
387 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
394 case OID_GEN_RCV_ERROR:
396 DBG("%s: OID_GEN_RCV_ERROR\n", __FUNCTION__);
397 if (rndis_per_dev_params [configNr].stats) {
398 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
405 case OID_GEN_RCV_NO_BUFFER:
406 DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __FUNCTION__);
407 if (rndis_per_dev_params [configNr].stats) {
408 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
414 #ifdef RNDIS_OPTIONAL_STATS
415 case OID_GEN_DIRECTED_BYTES_XMIT:
416 DBG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __FUNCTION__);
418 * Aunt Tilly's size of shoes
419 * minus antarctica count of penguins
420 * divided by weight of Alpha Centauri
422 if (rndis_per_dev_params [configNr].stats) {
423 *outbuf = cpu_to_le32 (
424 (rndis_per_dev_params [configNr]
426 rndis_per_dev_params [configNr]
428 rndis_per_dev_params [configNr]
435 case OID_GEN_DIRECTED_FRAMES_XMIT:
436 DBG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __FUNCTION__);
438 if (rndis_per_dev_params [configNr].stats) {
439 *outbuf = cpu_to_le32 (
440 (rndis_per_dev_params [configNr]
442 rndis_per_dev_params [configNr]
444 rndis_per_dev_params [configNr]
451 case OID_GEN_MULTICAST_BYTES_XMIT:
452 DBG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __FUNCTION__);
453 if (rndis_per_dev_params [configNr].stats) {
454 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
455 .stats->multicast*1234);
460 case OID_GEN_MULTICAST_FRAMES_XMIT:
461 DBG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __FUNCTION__);
462 if (rndis_per_dev_params [configNr].stats) {
463 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
469 case OID_GEN_BROADCAST_BYTES_XMIT:
470 DBG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __FUNCTION__);
471 if (rndis_per_dev_params [configNr].stats) {
472 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
473 .stats->tx_packets/42*255);
478 case OID_GEN_BROADCAST_FRAMES_XMIT:
479 DBG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __FUNCTION__);
480 if (rndis_per_dev_params [configNr].stats) {
481 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
482 .stats->tx_packets/42);
487 case OID_GEN_DIRECTED_BYTES_RCV:
488 DBG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __FUNCTION__);
489 *outbuf = __constant_cpu_to_le32 (0);
493 case OID_GEN_DIRECTED_FRAMES_RCV:
494 DBG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __FUNCTION__);
495 *outbuf = __constant_cpu_to_le32 (0);
499 case OID_GEN_MULTICAST_BYTES_RCV:
500 DBG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __FUNCTION__);
501 if (rndis_per_dev_params [configNr].stats) {
502 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
503 .stats->multicast * 1111);
508 case OID_GEN_MULTICAST_FRAMES_RCV:
509 DBG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __FUNCTION__);
510 if (rndis_per_dev_params [configNr].stats) {
511 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
517 case OID_GEN_BROADCAST_BYTES_RCV:
518 DBG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __FUNCTION__);
519 if (rndis_per_dev_params [configNr].stats) {
520 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
521 .stats->rx_packets/42*255);
526 case OID_GEN_BROADCAST_FRAMES_RCV:
527 DBG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __FUNCTION__);
528 if (rndis_per_dev_params [configNr].stats) {
529 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
530 .stats->rx_packets/42);
535 case OID_GEN_RCV_CRC_ERROR:
536 DBG("%s: OID_GEN_RCV_CRC_ERROR\n", __FUNCTION__);
537 if (rndis_per_dev_params [configNr].stats) {
538 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
539 .stats->rx_crc_errors);
544 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
545 DBG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __FUNCTION__);
546 *outbuf = __constant_cpu_to_le32 (0);
549 #endif /* RNDIS_OPTIONAL_STATS */
551 /* ieee802.3 OIDs (table 4-3) */
554 case OID_802_3_PERMANENT_ADDRESS:
555 DBG("%s: OID_802_3_PERMANENT_ADDRESS\n", __FUNCTION__);
556 if (rndis_per_dev_params [configNr].dev) {
559 rndis_per_dev_params [configNr].host_mac,
566 case OID_802_3_CURRENT_ADDRESS:
567 DBG("%s: OID_802_3_CURRENT_ADDRESS\n", __FUNCTION__);
568 if (rndis_per_dev_params [configNr].dev) {
571 rndis_per_dev_params [configNr].host_mac,
578 case OID_802_3_MULTICAST_LIST:
579 DBG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
580 /* Multicast base address only */
581 *outbuf = __constant_cpu_to_le32 (0xE0000000);
586 case OID_802_3_MAXIMUM_LIST_SIZE:
587 DBG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __FUNCTION__);
588 /* Multicast base address only */
589 *outbuf = __constant_cpu_to_le32 (1);
593 case OID_802_3_MAC_OPTIONS:
594 DBG("%s: OID_802_3_MAC_OPTIONS\n", __FUNCTION__);
597 /* ieee802.3 statistics OIDs (table 4-4) */
600 case OID_802_3_RCV_ERROR_ALIGNMENT:
601 DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __FUNCTION__);
602 if (rndis_per_dev_params [configNr].stats) {
603 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
604 .stats->rx_frame_errors);
610 case OID_802_3_XMIT_ONE_COLLISION:
611 DBG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __FUNCTION__);
612 *outbuf = __constant_cpu_to_le32 (0);
617 case OID_802_3_XMIT_MORE_COLLISIONS:
618 DBG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __FUNCTION__);
619 *outbuf = __constant_cpu_to_le32 (0);
623 #ifdef RNDIS_OPTIONAL_STATS
624 case OID_802_3_XMIT_DEFERRED:
625 DBG("%s: OID_802_3_XMIT_DEFERRED\n", __FUNCTION__);
629 case OID_802_3_XMIT_MAX_COLLISIONS:
630 DBG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __FUNCTION__);
634 case OID_802_3_RCV_OVERRUN:
635 DBG("%s: OID_802_3_RCV_OVERRUN\n", __FUNCTION__);
639 case OID_802_3_XMIT_UNDERRUN:
640 DBG("%s: OID_802_3_XMIT_UNDERRUN\n", __FUNCTION__);
644 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
645 DBG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __FUNCTION__);
649 case OID_802_3_XMIT_TIMES_CRS_LOST:
650 DBG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __FUNCTION__);
654 case OID_802_3_XMIT_LATE_COLLISIONS:
655 DBG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __FUNCTION__);
658 #endif /* RNDIS_OPTIONAL_STATS */
661 /* power management OIDs (table 4-5) */
662 case OID_PNP_CAPABILITIES:
663 DBG("%s: OID_PNP_CAPABILITIES\n", __FUNCTION__);
665 /* for now, no wakeup capabilities */
666 length = sizeof (struct NDIS_PNP_CAPABILITIES);
667 memset(outbuf, 0, length);
670 case OID_PNP_QUERY_POWER:
671 DBG("%s: OID_PNP_QUERY_POWER D%d\n", __FUNCTION__,
672 le32_to_cpu(get_unaligned((__le32 *)buf)) - 1);
673 /* only suspend is a real power state, and
674 * it can't be entered by OID_PNP_SET_POWER...
682 printk (KERN_WARNING "%s: query unknown OID 0x%08X\n",
688 resp->InformationBufferLength = cpu_to_le32 (length);
689 r->length = length + sizeof *resp;
690 resp->MessageLength = cpu_to_le32 (r->length);
694 static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
697 rndis_set_cmplt_type *resp;
698 int i, retval = -ENOTSUPP;
699 struct rndis_params *params;
703 resp = (rndis_set_cmplt_type *) r->buf;
707 if (buf_len && rndis_debug > 1) {
708 DBG("set OID %08x value, len %d:\n", OID, buf_len);
709 for (i = 0; i < buf_len; i += 16) {
710 DBG("%03d: %08x %08x %08x %08x\n", i,
711 le32_to_cpu(get_unaligned((__le32 *)
713 le32_to_cpu(get_unaligned((__le32 *)
715 le32_to_cpu(get_unaligned((__le32 *)
717 le32_to_cpu(get_unaligned((__le32 *)
722 params = &rndis_per_dev_params [configNr];
724 case OID_GEN_CURRENT_PACKET_FILTER:
726 /* these NDIS_PACKET_TYPE_* bitflags are shared with
727 * cdc_filter; it's not RNDIS-specific
728 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
729 * PROMISCUOUS, DIRECTED,
730 * MULTICAST, ALL_MULTICAST, BROADCAST
732 *params->filter = (u16) le32_to_cpu(get_unaligned(
734 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
735 __FUNCTION__, *params->filter);
737 /* this call has a significant side effect: it's
738 * what makes the packet flow start and stop, like
739 * activating the CDC Ethernet altsetting.
745 if (*params->filter) {
746 params->state = RNDIS_DATA_INITIALIZED;
747 netif_carrier_on(params->dev);
748 if (netif_running(params->dev))
749 netif_wake_queue (params->dev);
751 params->state = RNDIS_INITIALIZED;
752 netif_carrier_off (params->dev);
753 netif_stop_queue (params->dev);
757 case OID_802_3_MULTICAST_LIST:
758 /* I think we can ignore this */
759 DBG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
763 case OID_GEN_RNDIS_CONFIG_PARAMETER:
765 struct rndis_config_parameter *param;
766 param = (struct rndis_config_parameter *) buf;
767 DBG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
769 min(cpu_to_le32(param->ParameterNameLength),80),
770 buf + param->ParameterNameOffset);
777 case OID_PNP_SET_POWER:
778 /* The only real power state is USB suspend, and RNDIS requests
779 * can't enter it; this one isn't really about power. After
780 * resuming, Windows forces a reset, and then SET_POWER D0.
781 * FIXME ... then things go batty; Windows wedges itself.
783 i = le32_to_cpu(get_unaligned((__le32 *)buf));
784 DBG("%s: OID_PNP_SET_POWER D%d\n", __FUNCTION__, i - 1);
786 case NdisDeviceStateD0:
787 *params->filter = params->saved_filter;
788 goto update_linkstate;
789 case NdisDeviceStateD3:
790 case NdisDeviceStateD2:
791 case NdisDeviceStateD1:
792 params->saved_filter = *params->filter;
799 // no wakeup support advertised, so wakeup OIDs always fail:
800 // - OID_PNP_ENABLE_WAKE_UP
801 // - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
804 #endif /* RNDIS_PM */
807 printk (KERN_WARNING "%s: set unknown OID 0x%08X, size %d\n",
808 __FUNCTION__, OID, buf_len);
818 static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
820 rndis_init_cmplt_type *resp;
823 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
825 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
828 resp = (rndis_init_cmplt_type *) r->buf;
830 resp->MessageType = __constant_cpu_to_le32 (
831 REMOTE_NDIS_INITIALIZE_CMPLT);
832 resp->MessageLength = __constant_cpu_to_le32 (52);
833 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
834 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
835 resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
836 resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
837 resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
838 resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
839 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
840 resp->MaxTransferSize = cpu_to_le32 (
841 rndis_per_dev_params [configNr].dev->mtu
842 + sizeof (struct ethhdr)
843 + sizeof (struct rndis_packet_msg_type)
845 resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
846 resp->AFListOffset = __constant_cpu_to_le32 (0);
847 resp->AFListSize = __constant_cpu_to_le32 (0);
849 if (rndis_per_dev_params [configNr].ack)
850 rndis_per_dev_params [configNr].ack (
851 rndis_per_dev_params [configNr].dev);
856 static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
858 rndis_query_cmplt_type *resp;
861 // DBG("%s: OID = %08X\n", __FUNCTION__, cpu_to_le32(buf->OID));
862 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
865 * we need more memory:
866 * gen_ndis_query_resp expects enough space for
867 * rndis_query_cmplt_type followed by data.
868 * oid_supported_list is the largest data reply
870 r = rndis_add_response (configNr,
871 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
874 resp = (rndis_query_cmplt_type *) r->buf;
876 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
877 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
879 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
880 le32_to_cpu(buf->InformationBufferOffset)
882 le32_to_cpu(buf->InformationBufferLength),
884 /* OID not supported */
885 resp->Status = __constant_cpu_to_le32 (
886 RNDIS_STATUS_NOT_SUPPORTED);
887 resp->MessageLength = __constant_cpu_to_le32 (sizeof *resp);
888 resp->InformationBufferLength = __constant_cpu_to_le32 (0);
889 resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
891 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
893 if (rndis_per_dev_params [configNr].ack)
894 rndis_per_dev_params [configNr].ack (
895 rndis_per_dev_params [configNr].dev);
899 static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
901 u32 BufLength, BufOffset;
902 rndis_set_cmplt_type *resp;
905 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
908 resp = (rndis_set_cmplt_type *) r->buf;
910 BufLength = le32_to_cpu (buf->InformationBufferLength);
911 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
914 DBG("%s: Length: %d\n", __FUNCTION__, BufLength);
915 DBG("%s: Offset: %d\n", __FUNCTION__, BufOffset);
916 DBG("%s: InfoBuffer: ", __FUNCTION__);
918 for (i = 0; i < BufLength; i++) {
919 DBG("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
925 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
926 resp->MessageLength = __constant_cpu_to_le32 (16);
927 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
928 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
929 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
930 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
932 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
934 if (rndis_per_dev_params [configNr].ack)
935 rndis_per_dev_params [configNr].ack (
936 rndis_per_dev_params [configNr].dev);
941 static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
943 rndis_reset_cmplt_type *resp;
946 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
949 resp = (rndis_reset_cmplt_type *) r->buf;
951 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
952 resp->MessageLength = __constant_cpu_to_le32 (16);
953 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
954 /* resent information */
955 resp->AddressingReset = __constant_cpu_to_le32 (1);
957 if (rndis_per_dev_params [configNr].ack)
958 rndis_per_dev_params [configNr].ack (
959 rndis_per_dev_params [configNr].dev);
964 static int rndis_keepalive_response (int configNr,
965 rndis_keepalive_msg_type *buf)
967 rndis_keepalive_cmplt_type *resp;
970 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
972 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
975 resp = (rndis_keepalive_cmplt_type *) r->buf;
977 resp->MessageType = __constant_cpu_to_le32 (
978 REMOTE_NDIS_KEEPALIVE_CMPLT);
979 resp->MessageLength = __constant_cpu_to_le32 (16);
980 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
981 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
983 if (rndis_per_dev_params [configNr].ack)
984 rndis_per_dev_params [configNr].ack (
985 rndis_per_dev_params [configNr].dev);
992 * Device to Host Comunication
994 static int rndis_indicate_status_msg (int configNr, u32 status)
996 rndis_indicate_status_msg_type *resp;
999 if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED)
1002 r = rndis_add_response (configNr,
1003 sizeof (rndis_indicate_status_msg_type));
1006 resp = (rndis_indicate_status_msg_type *) r->buf;
1008 resp->MessageType = __constant_cpu_to_le32 (
1009 REMOTE_NDIS_INDICATE_STATUS_MSG);
1010 resp->MessageLength = __constant_cpu_to_le32 (20);
1011 resp->Status = cpu_to_le32 (status);
1012 resp->StatusBufferLength = __constant_cpu_to_le32 (0);
1013 resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
1015 if (rndis_per_dev_params [configNr].ack)
1016 rndis_per_dev_params [configNr].ack (
1017 rndis_per_dev_params [configNr].dev);
1021 int rndis_signal_connect (int configNr)
1023 rndis_per_dev_params [configNr].media_state
1024 = NDIS_MEDIA_STATE_CONNECTED;
1025 return rndis_indicate_status_msg (configNr,
1026 RNDIS_STATUS_MEDIA_CONNECT);
1029 int rndis_signal_disconnect (int configNr)
1031 rndis_per_dev_params [configNr].media_state
1032 = NDIS_MEDIA_STATE_DISCONNECTED;
1033 return rndis_indicate_status_msg (configNr,
1034 RNDIS_STATUS_MEDIA_DISCONNECT);
1037 void rndis_uninit (int configNr)
1042 if (configNr >= RNDIS_MAX_CONFIGS)
1044 rndis_per_dev_params [configNr].used = 0;
1045 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
1047 /* drain the response queue */
1048 while ((buf = rndis_get_next_response(configNr, &length)))
1049 rndis_free_response(configNr, buf);
1052 void rndis_set_host_mac (int configNr, const u8 *addr)
1054 rndis_per_dev_params [configNr].host_mac = addr;
1060 int rndis_msg_parser (u8 configNr, u8 *buf)
1062 u32 MsgType, MsgLength;
1064 struct rndis_params *params;
1069 tmp = (__le32 *) buf;
1070 MsgType = le32_to_cpu(get_unaligned(tmp++));
1071 MsgLength = le32_to_cpu(get_unaligned(tmp++));
1073 if (configNr >= RNDIS_MAX_CONFIGS)
1075 params = &rndis_per_dev_params [configNr];
1077 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1078 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1079 * and normal HC level polling to see if there's any IN traffic.
1082 /* For USB: responses may take up to 10 seconds */
1084 case REMOTE_NDIS_INITIALIZE_MSG:
1085 DBG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
1087 params->state = RNDIS_INITIALIZED;
1088 return rndis_init_response (configNr,
1089 (rndis_init_msg_type *) buf);
1091 case REMOTE_NDIS_HALT_MSG:
1092 DBG("%s: REMOTE_NDIS_HALT_MSG\n",
1094 params->state = RNDIS_UNINITIALIZED;
1096 netif_carrier_off (params->dev);
1097 netif_stop_queue (params->dev);
1101 case REMOTE_NDIS_QUERY_MSG:
1102 return rndis_query_response (configNr,
1103 (rndis_query_msg_type *) buf);
1105 case REMOTE_NDIS_SET_MSG:
1106 return rndis_set_response (configNr,
1107 (rndis_set_msg_type *) buf);
1109 case REMOTE_NDIS_RESET_MSG:
1110 DBG("%s: REMOTE_NDIS_RESET_MSG\n",
1112 return rndis_reset_response (configNr,
1113 (rndis_reset_msg_type *) buf);
1115 case REMOTE_NDIS_KEEPALIVE_MSG:
1116 /* For USB: host does this every 5 seconds */
1117 if (rndis_debug > 1)
1118 DBG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
1120 return rndis_keepalive_response (configNr,
1121 (rndis_keepalive_msg_type *)
1125 /* At least Windows XP emits some undefined RNDIS messages.
1126 * In one case those messages seemed to relate to the host
1127 * suspending itself.
1129 printk (KERN_WARNING
1130 "%s: unknown RNDIS message 0x%08X len %d\n",
1131 __FUNCTION__ , MsgType, MsgLength);
1134 for (i = 0; i < MsgLength; i += 16) {
1136 " %02x %02x %02x %02x"
1137 " %02x %02x %02x %02x"
1138 " %02x %02x %02x %02x"
1139 " %02x %02x %02x %02x"
1144 buf[i+4], buf [i+5],
1146 buf[i+8], buf [i+9],
1147 buf[i+10], buf[i+11],
1148 buf[i+12], buf [i+13],
1149 buf[i+14], buf[i+15]);
1158 int rndis_register (int (* rndis_control_ack) (struct net_device *))
1162 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1163 if (!rndis_per_dev_params [i].used) {
1164 rndis_per_dev_params [i].used = 1;
1165 rndis_per_dev_params [i].ack = rndis_control_ack;
1166 DBG("%s: configNr = %d\n", __FUNCTION__, i);
1175 void rndis_deregister (int configNr)
1177 DBG("%s: \n", __FUNCTION__ );
1179 if (configNr >= RNDIS_MAX_CONFIGS) return;
1180 rndis_per_dev_params [configNr].used = 0;
1185 int rndis_set_param_dev (u8 configNr, struct net_device *dev,
1186 struct net_device_stats *stats,
1189 DBG("%s:\n", __FUNCTION__ );
1190 if (!dev || !stats) return -1;
1191 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1193 rndis_per_dev_params [configNr].dev = dev;
1194 rndis_per_dev_params [configNr].stats = stats;
1195 rndis_per_dev_params [configNr].filter = cdc_filter;
1200 int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
1202 DBG("%s:\n", __FUNCTION__ );
1203 if (!vendorDescr) return -1;
1204 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1206 rndis_per_dev_params [configNr].vendorID = vendorID;
1207 rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
1212 int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
1214 DBG("%s: %u %u\n", __FUNCTION__, medium, speed);
1215 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1217 rndis_per_dev_params [configNr].medium = medium;
1218 rndis_per_dev_params [configNr].speed = speed;
1223 void rndis_add_hdr (struct sk_buff *skb)
1225 struct rndis_packet_msg_type *header;
1229 header = (void *) skb_push (skb, sizeof *header);
1230 memset (header, 0, sizeof *header);
1231 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1232 header->MessageLength = cpu_to_le32(skb->len);
1233 header->DataOffset = __constant_cpu_to_le32 (36);
1234 header->DataLength = cpu_to_le32(skb->len - sizeof *header);
1237 void rndis_free_response (int configNr, u8 *buf)
1240 struct list_head *act, *tmp;
1242 list_for_each_safe (act, tmp,
1243 &(rndis_per_dev_params [configNr].resp_queue))
1245 r = list_entry (act, rndis_resp_t, list);
1246 if (r && r->buf == buf) {
1247 list_del (&r->list);
1253 u8 *rndis_get_next_response (int configNr, u32 *length)
1256 struct list_head *act, *tmp;
1258 if (!length) return NULL;
1260 list_for_each_safe (act, tmp,
1261 &(rndis_per_dev_params [configNr].resp_queue))
1263 r = list_entry (act, rndis_resp_t, list);
1266 *length = r->length;
1274 static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1278 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1279 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1280 if (!r) return NULL;
1282 r->buf = (u8 *) (r + 1);
1286 list_add_tail (&r->list,
1287 &(rndis_per_dev_params [configNr].resp_queue));
1291 int rndis_rm_hdr(struct sk_buff *skb)
1293 /* tmp points to a struct rndis_packet_msg_type */
1294 __le32 *tmp = (void *) skb->data;
1296 /* MessageType, MessageLength */
1297 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1298 != get_unaligned(tmp++))
1302 /* DataOffset, DataLength */
1303 if (!skb_pull(skb, le32_to_cpu(get_unaligned(tmp++))
1304 + 8 /* offset of DataOffset */))
1306 skb_trim(skb, le32_to_cpu(get_unaligned(tmp++)));
1311 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1313 static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof,
1318 rndis_params *param = (rndis_params *) data;
1320 out += snprintf (out, count,
1327 "vendor ID : 0x%08X\n"
1329 param->confignr, (param->used) ? "y" : "n",
1331 switch (param->state) {
1332 case RNDIS_UNINITIALIZED:
1333 s = "RNDIS_UNINITIALIZED"; break;
1334 case RNDIS_INITIALIZED:
1335 s = "RNDIS_INITIALIZED"; break;
1336 case RNDIS_DATA_INITIALIZED:
1337 s = "RNDIS_DATA_INITIALIZED"; break;
1340 (param->media_state) ? 0 : param->speed*100,
1341 (param->media_state) ? "disconnected" : "connected",
1342 param->vendorID, param->vendorDescr);
1354 *start = page + off;
1358 static int rndis_proc_write (struct file *file, const char __user *buffer,
1359 unsigned long count, void *data)
1361 rndis_params *p = data;
1363 int i, fl_speed = 0;
1365 for (i = 0; i < count; i++) {
1367 if (get_user(c, buffer))
1381 speed = speed*10 + c - '0';
1385 rndis_signal_connect (p->confignr);
1389 rndis_signal_disconnect(p->confignr);
1392 if (fl_speed) p->speed = speed;
1393 else DBG("%c is not valid\n", c);
1403 #define NAME_TEMPLATE "driver/rndis-%03d"
1405 static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1407 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1410 int __devinit rndis_init (void)
1414 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1415 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1418 sprintf (name, NAME_TEMPLATE, i);
1419 if (!(rndis_connect_state [i]
1420 = create_proc_entry (name, 0660, NULL)))
1422 DBG("%s :remove entries", __FUNCTION__);
1424 sprintf (name, NAME_TEMPLATE, --i);
1425 remove_proc_entry (name, NULL);
1431 rndis_connect_state [i]->write_proc = rndis_proc_write;
1432 rndis_connect_state [i]->read_proc = rndis_proc_read;
1433 rndis_connect_state [i]->data = (void *)
1434 (rndis_per_dev_params + i);
1436 rndis_per_dev_params [i].confignr = i;
1437 rndis_per_dev_params [i].used = 0;
1438 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1439 rndis_per_dev_params [i].media_state
1440 = NDIS_MEDIA_STATE_DISCONNECTED;
1441 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1447 void rndis_exit (void)
1449 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1453 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1454 sprintf (name, NAME_TEMPLATE, i);
1455 remove_proc_entry (name, NULL);