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 DEBUG(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 DEBUG(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 DEBUG("query OID %08x value, len %d:\n", OID, buf_len);
187 for (i = 0; i < buf_len; i += 16) {
188 DEBUG ("%03d: %08x %08x %08x %08x\n", i,
189 le32_to_cpup((__le32 *)&buf[i]),
190 le32_to_cpup((__le32 *)&buf[i + 4]),
191 le32_to_cpup((__le32 *)&buf[i + 8]),
192 le32_to_cpup((__le32 *)&buf[i + 12]));
196 /* response goes here, right after the header */
197 outbuf = (__le32 *) &resp[1];
198 resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
202 /* general oids (table 4-1) */
205 case OID_GEN_SUPPORTED_LIST:
206 DEBUG ("%s: OID_GEN_SUPPORTED_LIST\n", __FUNCTION__);
207 length = sizeof (oid_supported_list);
208 count = length / sizeof (u32);
209 for (i = 0; i < count; i++)
210 outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
215 case OID_GEN_HARDWARE_STATUS:
216 DEBUG("%s: OID_GEN_HARDWARE_STATUS\n", __FUNCTION__);
218 * Hardware must be ready to receive high level protocols.
220 * reddite ergo quae sunt Caesaris Caesari
221 * et quae sunt Dei Deo!
223 *outbuf = __constant_cpu_to_le32 (0);
228 case OID_GEN_MEDIA_SUPPORTED:
229 DEBUG("%s: OID_GEN_MEDIA_SUPPORTED\n", __FUNCTION__);
230 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
235 case OID_GEN_MEDIA_IN_USE:
236 DEBUG("%s: OID_GEN_MEDIA_IN_USE\n", __FUNCTION__);
237 /* one medium, one transport... (maybe you do it better) */
238 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
243 case OID_GEN_MAXIMUM_FRAME_SIZE:
244 DEBUG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __FUNCTION__);
245 if (rndis_per_dev_params [configNr].dev) {
246 *outbuf = cpu_to_le32 (
247 rndis_per_dev_params [configNr].dev->mtu);
253 case OID_GEN_LINK_SPEED:
255 DEBUG("%s: OID_GEN_LINK_SPEED\n", __FUNCTION__);
256 if (rndis_per_dev_params [configNr].media_state
257 == NDIS_MEDIA_STATE_DISCONNECTED)
258 *outbuf = __constant_cpu_to_le32 (0);
260 *outbuf = cpu_to_le32 (
261 rndis_per_dev_params [configNr].speed);
266 case OID_GEN_TRANSMIT_BLOCK_SIZE:
267 DEBUG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __FUNCTION__);
268 if (rndis_per_dev_params [configNr].dev) {
269 *outbuf = cpu_to_le32 (
270 rndis_per_dev_params [configNr].dev->mtu);
276 case OID_GEN_RECEIVE_BLOCK_SIZE:
277 DEBUG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __FUNCTION__);
278 if (rndis_per_dev_params [configNr].dev) {
279 *outbuf = cpu_to_le32 (
280 rndis_per_dev_params [configNr].dev->mtu);
286 case OID_GEN_VENDOR_ID:
287 DEBUG("%s: OID_GEN_VENDOR_ID\n", __FUNCTION__);
288 *outbuf = cpu_to_le32 (
289 rndis_per_dev_params [configNr].vendorID);
294 case OID_GEN_VENDOR_DESCRIPTION:
295 DEBUG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __FUNCTION__);
296 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
298 rndis_per_dev_params [configNr].vendorDescr, length);
302 case OID_GEN_VENDOR_DRIVER_VERSION:
303 DEBUG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __FUNCTION__);
305 *outbuf = rndis_driver_version;
310 case OID_GEN_CURRENT_PACKET_FILTER:
311 DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __FUNCTION__);
312 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
317 case OID_GEN_MAXIMUM_TOTAL_SIZE:
318 DEBUG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __FUNCTION__);
319 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
324 case OID_GEN_MEDIA_CONNECT_STATUS:
326 DEBUG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __FUNCTION__);
327 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
332 case OID_GEN_PHYSICAL_MEDIUM:
333 DEBUG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __FUNCTION__);
334 *outbuf = __constant_cpu_to_le32 (0);
338 /* The RNDIS specification is incomplete/wrong. Some versions
339 * of MS-Windows expect OIDs that aren't specified there. Other
340 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
342 case OID_GEN_MAC_OPTIONS: /* from WinME */
343 DEBUG("%s: OID_GEN_MAC_OPTIONS\n", __FUNCTION__);
344 *outbuf = __constant_cpu_to_le32(
345 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
346 | NDIS_MAC_OPTION_FULL_DUPLEX);
350 /* statistics OIDs (table 4-2) */
353 case OID_GEN_XMIT_OK:
355 DEBUG("%s: OID_GEN_XMIT_OK\n", __FUNCTION__);
356 if (rndis_per_dev_params [configNr].stats) {
357 *outbuf = cpu_to_le32 (
358 rndis_per_dev_params [configNr].stats->tx_packets -
359 rndis_per_dev_params [configNr].stats->tx_errors -
360 rndis_per_dev_params [configNr].stats->tx_dropped);
368 DEBUG("%s: OID_GEN_RCV_OK\n", __FUNCTION__);
369 if (rndis_per_dev_params [configNr].stats) {
370 *outbuf = cpu_to_le32 (
371 rndis_per_dev_params [configNr].stats->rx_packets -
372 rndis_per_dev_params [configNr].stats->rx_errors -
373 rndis_per_dev_params [configNr].stats->rx_dropped);
379 case OID_GEN_XMIT_ERROR:
381 DEBUG("%s: OID_GEN_XMIT_ERROR\n", __FUNCTION__);
382 if (rndis_per_dev_params [configNr].stats) {
383 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
390 case OID_GEN_RCV_ERROR:
392 DEBUG("%s: OID_GEN_RCV_ERROR\n", __FUNCTION__);
393 if (rndis_per_dev_params [configNr].stats) {
394 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
401 case OID_GEN_RCV_NO_BUFFER:
402 DEBUG("%s: OID_GEN_RCV_NO_BUFFER\n", __FUNCTION__);
403 if (rndis_per_dev_params [configNr].stats) {
404 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
410 #ifdef RNDIS_OPTIONAL_STATS
411 case OID_GEN_DIRECTED_BYTES_XMIT:
412 DEBUG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __FUNCTION__);
414 * Aunt Tilly's size of shoes
415 * minus antarctica count of penguins
416 * divided by weight of Alpha Centauri
418 if (rndis_per_dev_params [configNr].stats) {
419 *outbuf = cpu_to_le32 (
420 (rndis_per_dev_params [configNr]
422 rndis_per_dev_params [configNr]
424 rndis_per_dev_params [configNr]
431 case OID_GEN_DIRECTED_FRAMES_XMIT:
432 DEBUG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __FUNCTION__);
434 if (rndis_per_dev_params [configNr].stats) {
435 *outbuf = cpu_to_le32 (
436 (rndis_per_dev_params [configNr]
438 rndis_per_dev_params [configNr]
440 rndis_per_dev_params [configNr]
447 case OID_GEN_MULTICAST_BYTES_XMIT:
448 DEBUG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __FUNCTION__);
449 if (rndis_per_dev_params [configNr].stats) {
450 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
451 .stats->multicast*1234);
456 case OID_GEN_MULTICAST_FRAMES_XMIT:
457 DEBUG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __FUNCTION__);
458 if (rndis_per_dev_params [configNr].stats) {
459 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
465 case OID_GEN_BROADCAST_BYTES_XMIT:
466 DEBUG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __FUNCTION__);
467 if (rndis_per_dev_params [configNr].stats) {
468 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
469 .stats->tx_packets/42*255);
474 case OID_GEN_BROADCAST_FRAMES_XMIT:
475 DEBUG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __FUNCTION__);
476 if (rndis_per_dev_params [configNr].stats) {
477 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
478 .stats->tx_packets/42);
483 case OID_GEN_DIRECTED_BYTES_RCV:
484 DEBUG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __FUNCTION__);
485 *outbuf = __constant_cpu_to_le32 (0);
489 case OID_GEN_DIRECTED_FRAMES_RCV:
490 DEBUG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __FUNCTION__);
491 *outbuf = __constant_cpu_to_le32 (0);
495 case OID_GEN_MULTICAST_BYTES_RCV:
496 DEBUG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __FUNCTION__);
497 if (rndis_per_dev_params [configNr].stats) {
498 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
499 .stats->multicast * 1111);
504 case OID_GEN_MULTICAST_FRAMES_RCV:
505 DEBUG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __FUNCTION__);
506 if (rndis_per_dev_params [configNr].stats) {
507 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
513 case OID_GEN_BROADCAST_BYTES_RCV:
514 DEBUG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __FUNCTION__);
515 if (rndis_per_dev_params [configNr].stats) {
516 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
517 .stats->rx_packets/42*255);
522 case OID_GEN_BROADCAST_FRAMES_RCV:
523 DEBUG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __FUNCTION__);
524 if (rndis_per_dev_params [configNr].stats) {
525 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
526 .stats->rx_packets/42);
531 case OID_GEN_RCV_CRC_ERROR:
532 DEBUG("%s: OID_GEN_RCV_CRC_ERROR\n", __FUNCTION__);
533 if (rndis_per_dev_params [configNr].stats) {
534 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
535 .stats->rx_crc_errors);
540 case OID_GEN_TRANSMIT_QUEUE_LENGTH:
541 DEBUG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __FUNCTION__);
542 *outbuf = __constant_cpu_to_le32 (0);
545 #endif /* RNDIS_OPTIONAL_STATS */
547 /* ieee802.3 OIDs (table 4-3) */
550 case OID_802_3_PERMANENT_ADDRESS:
551 DEBUG("%s: OID_802_3_PERMANENT_ADDRESS\n", __FUNCTION__);
552 if (rndis_per_dev_params [configNr].dev) {
555 rndis_per_dev_params [configNr].host_mac,
562 case OID_802_3_CURRENT_ADDRESS:
563 DEBUG("%s: OID_802_3_CURRENT_ADDRESS\n", __FUNCTION__);
564 if (rndis_per_dev_params [configNr].dev) {
567 rndis_per_dev_params [configNr].host_mac,
574 case OID_802_3_MULTICAST_LIST:
575 DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
576 /* Multicast base address only */
577 *outbuf = __constant_cpu_to_le32 (0xE0000000);
582 case OID_802_3_MAXIMUM_LIST_SIZE:
583 DEBUG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __FUNCTION__);
584 /* Multicast base address only */
585 *outbuf = __constant_cpu_to_le32 (1);
589 case OID_802_3_MAC_OPTIONS:
590 DEBUG("%s: OID_802_3_MAC_OPTIONS\n", __FUNCTION__);
593 /* ieee802.3 statistics OIDs (table 4-4) */
596 case OID_802_3_RCV_ERROR_ALIGNMENT:
597 DEBUG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __FUNCTION__);
598 if (rndis_per_dev_params [configNr].stats) {
599 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
600 .stats->rx_frame_errors);
606 case OID_802_3_XMIT_ONE_COLLISION:
607 DEBUG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __FUNCTION__);
608 *outbuf = __constant_cpu_to_le32 (0);
613 case OID_802_3_XMIT_MORE_COLLISIONS:
614 DEBUG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __FUNCTION__);
615 *outbuf = __constant_cpu_to_le32 (0);
619 #ifdef RNDIS_OPTIONAL_STATS
620 case OID_802_3_XMIT_DEFERRED:
621 DEBUG("%s: OID_802_3_XMIT_DEFERRED\n", __FUNCTION__);
625 case OID_802_3_XMIT_MAX_COLLISIONS:
626 DEBUG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __FUNCTION__);
630 case OID_802_3_RCV_OVERRUN:
631 DEBUG("%s: OID_802_3_RCV_OVERRUN\n", __FUNCTION__);
635 case OID_802_3_XMIT_UNDERRUN:
636 DEBUG("%s: OID_802_3_XMIT_UNDERRUN\n", __FUNCTION__);
640 case OID_802_3_XMIT_HEARTBEAT_FAILURE:
641 DEBUG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __FUNCTION__);
645 case OID_802_3_XMIT_TIMES_CRS_LOST:
646 DEBUG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __FUNCTION__);
650 case OID_802_3_XMIT_LATE_COLLISIONS:
651 DEBUG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __FUNCTION__);
654 #endif /* RNDIS_OPTIONAL_STATS */
657 /* power management OIDs (table 4-5) */
658 case OID_PNP_CAPABILITIES:
659 DEBUG("%s: OID_PNP_CAPABILITIES\n", __FUNCTION__);
661 /* for now, no wakeup capabilities */
662 length = sizeof (struct NDIS_PNP_CAPABILITIES);
663 memset(outbuf, 0, length);
666 case OID_PNP_QUERY_POWER:
667 DEBUG("%s: OID_PNP_QUERY_POWER D%d\n", __FUNCTION__,
668 le32_to_cpup((__le32 *) buf) - 1);
669 /* only suspend is a real power state, and
670 * it can't be entered by OID_PNP_SET_POWER...
678 printk (KERN_WARNING "%s: query unknown OID 0x%08X\n",
684 resp->InformationBufferLength = cpu_to_le32 (length);
685 r->length = length + sizeof *resp;
686 resp->MessageLength = cpu_to_le32 (r->length);
690 static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
693 rndis_set_cmplt_type *resp;
694 int i, retval = -ENOTSUPP;
695 struct rndis_params *params;
699 resp = (rndis_set_cmplt_type *) r->buf;
703 if (buf_len && rndis_debug > 1) {
704 DEBUG("set OID %08x value, len %d:\n", OID, buf_len);
705 for (i = 0; i < buf_len; i += 16) {
706 DEBUG ("%03d: %08x %08x %08x %08x\n", i,
707 le32_to_cpup((__le32 *)&buf[i]),
708 le32_to_cpup((__le32 *)&buf[i + 4]),
709 le32_to_cpup((__le32 *)&buf[i + 8]),
710 le32_to_cpup((__le32 *)&buf[i + 12]));
714 params = &rndis_per_dev_params [configNr];
716 case OID_GEN_CURRENT_PACKET_FILTER:
718 /* these NDIS_PACKET_TYPE_* bitflags are shared with
719 * cdc_filter; it's not RNDIS-specific
720 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
721 * PROMISCUOUS, DIRECTED,
722 * MULTICAST, ALL_MULTICAST, BROADCAST
724 *params->filter = (u16) le32_to_cpup((__le32 *)buf);
725 DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
726 __FUNCTION__, *params->filter);
728 /* this call has a significant side effect: it's
729 * what makes the packet flow start and stop, like
730 * activating the CDC Ethernet altsetting.
736 if (*params->filter) {
737 params->state = RNDIS_DATA_INITIALIZED;
738 netif_carrier_on(params->dev);
739 if (netif_running(params->dev))
740 netif_wake_queue (params->dev);
742 params->state = RNDIS_INITIALIZED;
743 netif_carrier_off (params->dev);
744 netif_stop_queue (params->dev);
748 case OID_802_3_MULTICAST_LIST:
749 /* I think we can ignore this */
750 DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
754 case OID_GEN_RNDIS_CONFIG_PARAMETER:
756 struct rndis_config_parameter *param;
757 param = (struct rndis_config_parameter *) buf;
758 DEBUG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
760 min(cpu_to_le32(param->ParameterNameLength),80),
761 buf + param->ParameterNameOffset);
768 case OID_PNP_SET_POWER:
769 /* The only real power state is USB suspend, and RNDIS requests
770 * can't enter it; this one isn't really about power. After
771 * resuming, Windows forces a reset, and then SET_POWER D0.
772 * FIXME ... then things go batty; Windows wedges itself.
774 i = le32_to_cpup((__force __le32 *)buf);
775 DEBUG("%s: OID_PNP_SET_POWER D%d\n", __FUNCTION__, i - 1);
777 case NdisDeviceStateD0:
778 *params->filter = params->saved_filter;
779 goto update_linkstate;
780 case NdisDeviceStateD3:
781 case NdisDeviceStateD2:
782 case NdisDeviceStateD1:
783 params->saved_filter = *params->filter;
790 // no wakeup support advertised, so wakeup OIDs always fail:
791 // - OID_PNP_ENABLE_WAKE_UP
792 // - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
795 #endif /* RNDIS_PM */
798 printk (KERN_WARNING "%s: set unknown OID 0x%08X, size %d\n",
799 __FUNCTION__, OID, buf_len);
809 static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
811 rndis_init_cmplt_type *resp;
814 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
816 r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
819 resp = (rndis_init_cmplt_type *) r->buf;
821 resp->MessageType = __constant_cpu_to_le32 (
822 REMOTE_NDIS_INITIALIZE_CMPLT);
823 resp->MessageLength = __constant_cpu_to_le32 (52);
824 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
825 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
826 resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
827 resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
828 resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
829 resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
830 resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
831 resp->MaxTransferSize = cpu_to_le32 (
832 rndis_per_dev_params [configNr].dev->mtu
833 + sizeof (struct ethhdr)
834 + sizeof (struct rndis_packet_msg_type)
836 resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
837 resp->AFListOffset = __constant_cpu_to_le32 (0);
838 resp->AFListSize = __constant_cpu_to_le32 (0);
840 if (rndis_per_dev_params [configNr].ack)
841 rndis_per_dev_params [configNr].ack (
842 rndis_per_dev_params [configNr].dev);
847 static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
849 rndis_query_cmplt_type *resp;
852 // DEBUG("%s: OID = %08X\n", __FUNCTION__, cpu_to_le32(buf->OID));
853 if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
856 * we need more memory:
857 * gen_ndis_query_resp expects enough space for
858 * rndis_query_cmplt_type followed by data.
859 * oid_supported_list is the largest data reply
861 r = rndis_add_response (configNr,
862 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
865 resp = (rndis_query_cmplt_type *) r->buf;
867 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
868 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
870 if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
871 le32_to_cpu(buf->InformationBufferOffset)
873 le32_to_cpu(buf->InformationBufferLength),
875 /* OID not supported */
876 resp->Status = __constant_cpu_to_le32 (
877 RNDIS_STATUS_NOT_SUPPORTED);
878 resp->MessageLength = __constant_cpu_to_le32 (sizeof *resp);
879 resp->InformationBufferLength = __constant_cpu_to_le32 (0);
880 resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
882 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
884 if (rndis_per_dev_params [configNr].ack)
885 rndis_per_dev_params [configNr].ack (
886 rndis_per_dev_params [configNr].dev);
890 static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
892 u32 BufLength, BufOffset;
893 rndis_set_cmplt_type *resp;
896 r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
899 resp = (rndis_set_cmplt_type *) r->buf;
901 BufLength = le32_to_cpu (buf->InformationBufferLength);
902 BufOffset = le32_to_cpu (buf->InformationBufferOffset);
905 DEBUG("%s: Length: %d\n", __FUNCTION__, BufLength);
906 DEBUG("%s: Offset: %d\n", __FUNCTION__, BufOffset);
907 DEBUG("%s: InfoBuffer: ", __FUNCTION__);
909 for (i = 0; i < BufLength; i++) {
910 DEBUG ("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
916 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
917 resp->MessageLength = __constant_cpu_to_le32 (16);
918 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
919 if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
920 ((u8 *) buf) + 8 + BufOffset, BufLength, r))
921 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
923 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
925 if (rndis_per_dev_params [configNr].ack)
926 rndis_per_dev_params [configNr].ack (
927 rndis_per_dev_params [configNr].dev);
932 static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
934 rndis_reset_cmplt_type *resp;
937 r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
940 resp = (rndis_reset_cmplt_type *) r->buf;
942 resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
943 resp->MessageLength = __constant_cpu_to_le32 (16);
944 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
945 /* resent information */
946 resp->AddressingReset = __constant_cpu_to_le32 (1);
948 if (rndis_per_dev_params [configNr].ack)
949 rndis_per_dev_params [configNr].ack (
950 rndis_per_dev_params [configNr].dev);
955 static int rndis_keepalive_response (int configNr,
956 rndis_keepalive_msg_type *buf)
958 rndis_keepalive_cmplt_type *resp;
961 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
963 r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
966 resp = (rndis_keepalive_cmplt_type *) r->buf;
968 resp->MessageType = __constant_cpu_to_le32 (
969 REMOTE_NDIS_KEEPALIVE_CMPLT);
970 resp->MessageLength = __constant_cpu_to_le32 (16);
971 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
972 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
974 if (rndis_per_dev_params [configNr].ack)
975 rndis_per_dev_params [configNr].ack (
976 rndis_per_dev_params [configNr].dev);
983 * Device to Host Comunication
985 static int rndis_indicate_status_msg (int configNr, u32 status)
987 rndis_indicate_status_msg_type *resp;
990 if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED)
993 r = rndis_add_response (configNr,
994 sizeof (rndis_indicate_status_msg_type));
997 resp = (rndis_indicate_status_msg_type *) r->buf;
999 resp->MessageType = __constant_cpu_to_le32 (
1000 REMOTE_NDIS_INDICATE_STATUS_MSG);
1001 resp->MessageLength = __constant_cpu_to_le32 (20);
1002 resp->Status = cpu_to_le32 (status);
1003 resp->StatusBufferLength = __constant_cpu_to_le32 (0);
1004 resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
1006 if (rndis_per_dev_params [configNr].ack)
1007 rndis_per_dev_params [configNr].ack (
1008 rndis_per_dev_params [configNr].dev);
1012 int rndis_signal_connect (int configNr)
1014 rndis_per_dev_params [configNr].media_state
1015 = NDIS_MEDIA_STATE_CONNECTED;
1016 return rndis_indicate_status_msg (configNr,
1017 RNDIS_STATUS_MEDIA_CONNECT);
1020 int rndis_signal_disconnect (int configNr)
1022 rndis_per_dev_params [configNr].media_state
1023 = NDIS_MEDIA_STATE_DISCONNECTED;
1024 return rndis_indicate_status_msg (configNr,
1025 RNDIS_STATUS_MEDIA_DISCONNECT);
1028 void rndis_uninit (int configNr)
1033 if (configNr >= RNDIS_MAX_CONFIGS)
1035 rndis_per_dev_params [configNr].used = 0;
1036 rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
1038 /* drain the response queue */
1039 while ((buf = rndis_get_next_response(configNr, &length)))
1040 rndis_free_response(configNr, buf);
1043 void rndis_set_host_mac (int configNr, const u8 *addr)
1045 rndis_per_dev_params [configNr].host_mac = addr;
1051 int rndis_msg_parser (u8 configNr, u8 *buf)
1053 u32 MsgType, MsgLength;
1055 struct rndis_params *params;
1060 tmp = (__le32 *) buf;
1061 MsgType = le32_to_cpup(tmp++);
1062 MsgLength = le32_to_cpup(tmp++);
1064 if (configNr >= RNDIS_MAX_CONFIGS)
1066 params = &rndis_per_dev_params [configNr];
1068 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1069 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1070 * and normal HC level polling to see if there's any IN traffic.
1073 /* For USB: responses may take up to 10 seconds */
1075 case REMOTE_NDIS_INITIALIZE_MSG:
1076 DEBUG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
1078 params->state = RNDIS_INITIALIZED;
1079 return rndis_init_response (configNr,
1080 (rndis_init_msg_type *) buf);
1082 case REMOTE_NDIS_HALT_MSG:
1083 DEBUG("%s: REMOTE_NDIS_HALT_MSG\n",
1085 params->state = RNDIS_UNINITIALIZED;
1087 netif_carrier_off (params->dev);
1088 netif_stop_queue (params->dev);
1092 case REMOTE_NDIS_QUERY_MSG:
1093 return rndis_query_response (configNr,
1094 (rndis_query_msg_type *) buf);
1096 case REMOTE_NDIS_SET_MSG:
1097 return rndis_set_response (configNr,
1098 (rndis_set_msg_type *) buf);
1100 case REMOTE_NDIS_RESET_MSG:
1101 DEBUG("%s: REMOTE_NDIS_RESET_MSG\n",
1103 return rndis_reset_response (configNr,
1104 (rndis_reset_msg_type *) buf);
1106 case REMOTE_NDIS_KEEPALIVE_MSG:
1107 /* For USB: host does this every 5 seconds */
1108 if (rndis_debug > 1)
1109 DEBUG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
1111 return rndis_keepalive_response (configNr,
1112 (rndis_keepalive_msg_type *)
1116 /* At least Windows XP emits some undefined RNDIS messages.
1117 * In one case those messages seemed to relate to the host
1118 * suspending itself.
1120 printk (KERN_WARNING
1121 "%s: unknown RNDIS message 0x%08X len %d\n",
1122 __FUNCTION__ , MsgType, MsgLength);
1125 for (i = 0; i < MsgLength; i += 16) {
1127 " %02x %02x %02x %02x"
1128 " %02x %02x %02x %02x"
1129 " %02x %02x %02x %02x"
1130 " %02x %02x %02x %02x"
1135 buf[i+4], buf [i+5],
1137 buf[i+8], buf [i+9],
1138 buf[i+10], buf[i+11],
1139 buf[i+12], buf [i+13],
1140 buf[i+14], buf[i+15]);
1149 int rndis_register (int (* rndis_control_ack) (struct net_device *))
1153 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1154 if (!rndis_per_dev_params [i].used) {
1155 rndis_per_dev_params [i].used = 1;
1156 rndis_per_dev_params [i].ack = rndis_control_ack;
1157 DEBUG("%s: configNr = %d\n", __FUNCTION__, i);
1166 void rndis_deregister (int configNr)
1168 DEBUG("%s: \n", __FUNCTION__ );
1170 if (configNr >= RNDIS_MAX_CONFIGS) return;
1171 rndis_per_dev_params [configNr].used = 0;
1176 int rndis_set_param_dev (u8 configNr, struct net_device *dev,
1177 struct net_device_stats *stats,
1180 DEBUG("%s:\n", __FUNCTION__ );
1181 if (!dev || !stats) return -1;
1182 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1184 rndis_per_dev_params [configNr].dev = dev;
1185 rndis_per_dev_params [configNr].stats = stats;
1186 rndis_per_dev_params [configNr].filter = cdc_filter;
1191 int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
1193 DEBUG("%s:\n", __FUNCTION__ );
1194 if (!vendorDescr) return -1;
1195 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1197 rndis_per_dev_params [configNr].vendorID = vendorID;
1198 rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
1203 int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
1205 DEBUG("%s: %u %u\n", __FUNCTION__, medium, speed);
1206 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1208 rndis_per_dev_params [configNr].medium = medium;
1209 rndis_per_dev_params [configNr].speed = speed;
1214 void rndis_add_hdr (struct sk_buff *skb)
1216 struct rndis_packet_msg_type *header;
1220 header = (void *) skb_push (skb, sizeof *header);
1221 memset (header, 0, sizeof *header);
1222 header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1223 header->MessageLength = cpu_to_le32(skb->len);
1224 header->DataOffset = __constant_cpu_to_le32 (36);
1225 header->DataLength = cpu_to_le32(skb->len - sizeof *header);
1228 void rndis_free_response (int configNr, u8 *buf)
1231 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);
1237 if (r && r->buf == buf) {
1238 list_del (&r->list);
1244 u8 *rndis_get_next_response (int configNr, u32 *length)
1247 struct list_head *act, *tmp;
1249 if (!length) return NULL;
1251 list_for_each_safe (act, tmp,
1252 &(rndis_per_dev_params [configNr].resp_queue))
1254 r = list_entry (act, rndis_resp_t, list);
1257 *length = r->length;
1265 static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1269 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1270 r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1271 if (!r) return NULL;
1273 r->buf = (u8 *) (r + 1);
1277 list_add_tail (&r->list,
1278 &(rndis_per_dev_params [configNr].resp_queue));
1282 int rndis_rm_hdr(struct sk_buff *skb)
1284 /* tmp points to a struct rndis_packet_msg_type */
1285 __le32 *tmp = (void *) skb->data;
1287 /* MessageType, MessageLength */
1288 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1289 != get_unaligned(tmp++))
1293 /* DataOffset, DataLength */
1294 if (!skb_pull(skb, le32_to_cpu(get_unaligned(tmp++))
1295 + 8 /* offset of DataOffset */))
1297 skb_trim(skb, le32_to_cpu(get_unaligned(tmp++)));
1302 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1304 static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof,
1309 rndis_params *param = (rndis_params *) data;
1311 out += snprintf (out, count,
1318 "vendor ID : 0x%08X\n"
1320 param->confignr, (param->used) ? "y" : "n",
1322 switch (param->state) {
1323 case RNDIS_UNINITIALIZED:
1324 s = "RNDIS_UNINITIALIZED"; break;
1325 case RNDIS_INITIALIZED:
1326 s = "RNDIS_INITIALIZED"; break;
1327 case RNDIS_DATA_INITIALIZED:
1328 s = "RNDIS_DATA_INITIALIZED"; break;
1331 (param->media_state) ? 0 : param->speed*100,
1332 (param->media_state) ? "disconnected" : "connected",
1333 param->vendorID, param->vendorDescr);
1345 *start = page + off;
1349 static int rndis_proc_write (struct file *file, const char __user *buffer,
1350 unsigned long count, void *data)
1352 rndis_params *p = data;
1354 int i, fl_speed = 0;
1356 for (i = 0; i < count; i++) {
1358 if (get_user(c, buffer))
1372 speed = speed*10 + c - '0';
1376 rndis_signal_connect (p->confignr);
1380 rndis_signal_disconnect(p->confignr);
1383 if (fl_speed) p->speed = speed;
1384 else DEBUG ("%c is not valid\n", c);
1394 #define NAME_TEMPLATE "driver/rndis-%03d"
1396 static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1398 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1401 int __devinit rndis_init (void)
1405 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1406 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1409 sprintf (name, NAME_TEMPLATE, i);
1410 if (!(rndis_connect_state [i]
1411 = create_proc_entry (name, 0660, NULL)))
1413 DEBUG ("%s :remove entries", __FUNCTION__);
1415 sprintf (name, NAME_TEMPLATE, --i);
1416 remove_proc_entry (name, NULL);
1422 rndis_connect_state [i]->write_proc = rndis_proc_write;
1423 rndis_connect_state [i]->read_proc = rndis_proc_read;
1424 rndis_connect_state [i]->data = (void *)
1425 (rndis_per_dev_params + i);
1427 rndis_per_dev_params [i].confignr = i;
1428 rndis_per_dev_params [i].used = 0;
1429 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1430 rndis_per_dev_params [i].media_state
1431 = NDIS_MEDIA_STATE_DISCONNECTED;
1432 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1438 void rndis_exit (void)
1440 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1444 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1445 sprintf (name, NAME_TEMPLATE, i);
1446 remove_proc_entry (name, NULL);