4 * Authors: Benedikt Spranger, Pengutronix
5 * Robert Schwebel, Pengutronix
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2, as published by the Free Software Foundation.
11 * This software was originally developed in conformance with
12 * Microsoft's Remote NDIS Specification License Agreement.
14 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
15 * Fixed message length bug in init_response
17 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
18 * Fixed rndis_rm_hdr length bug.
20 * Copyright (C) 2004 by David Brownell
21 * updates to merge with Linux 2.6, better match RNDIS spec
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/netdevice.h>
35 #include <asm/byteorder.h>
36 #include <asm/system.h>
37 #include <asm/unaligned.h>
47 /* The driver for your USB chip needs to support ep0 OUT to work with
48 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
50 * Windows hosts need an INF file like Documentation/usb/linux.inf
51 * and will be happier if you provide the host_addr module parameter.
55 static int rndis_debug
= 0;
56 module_param (rndis_debug
, int, 0);
57 MODULE_PARM_DESC (rndis_debug
, "enable debugging");
62 #define DBG(str,args...) do { \
64 pr_debug(str , ## args); \
67 #define RNDIS_MAX_CONFIGS 1
70 static rndis_params rndis_per_dev_params
[RNDIS_MAX_CONFIGS
];
73 static const __le32 rndis_driver_version
= __constant_cpu_to_le32 (1);
75 /* Function Prototypes */
76 static rndis_resp_t
*rndis_add_response (int configNr
, u32 length
);
80 static const u32 oid_supported_list
[] =
82 /* the general stuff */
83 OID_GEN_SUPPORTED_LIST
,
84 OID_GEN_HARDWARE_STATUS
,
85 OID_GEN_MEDIA_SUPPORTED
,
87 OID_GEN_MAXIMUM_FRAME_SIZE
,
89 OID_GEN_TRANSMIT_BLOCK_SIZE
,
90 OID_GEN_RECEIVE_BLOCK_SIZE
,
92 OID_GEN_VENDOR_DESCRIPTION
,
93 OID_GEN_VENDOR_DRIVER_VERSION
,
94 OID_GEN_CURRENT_PACKET_FILTER
,
95 OID_GEN_MAXIMUM_TOTAL_SIZE
,
96 OID_GEN_MEDIA_CONNECT_STATUS
,
97 OID_GEN_PHYSICAL_MEDIUM
,
99 OID_GEN_RNDIS_CONFIG_PARAMETER
,
102 /* the statistical stuff */
107 OID_GEN_RCV_NO_BUFFER
,
108 #ifdef RNDIS_OPTIONAL_STATS
109 OID_GEN_DIRECTED_BYTES_XMIT
,
110 OID_GEN_DIRECTED_FRAMES_XMIT
,
111 OID_GEN_MULTICAST_BYTES_XMIT
,
112 OID_GEN_MULTICAST_FRAMES_XMIT
,
113 OID_GEN_BROADCAST_BYTES_XMIT
,
114 OID_GEN_BROADCAST_FRAMES_XMIT
,
115 OID_GEN_DIRECTED_BYTES_RCV
,
116 OID_GEN_DIRECTED_FRAMES_RCV
,
117 OID_GEN_MULTICAST_BYTES_RCV
,
118 OID_GEN_MULTICAST_FRAMES_RCV
,
119 OID_GEN_BROADCAST_BYTES_RCV
,
120 OID_GEN_BROADCAST_FRAMES_RCV
,
121 OID_GEN_RCV_CRC_ERROR
,
122 OID_GEN_TRANSMIT_QUEUE_LENGTH
,
123 #endif /* RNDIS_OPTIONAL_STATS */
125 /* mandatory 802.3 */
126 /* the general stuff */
127 OID_802_3_PERMANENT_ADDRESS
,
128 OID_802_3_CURRENT_ADDRESS
,
129 OID_802_3_MULTICAST_LIST
,
130 OID_802_3_MAC_OPTIONS
,
131 OID_802_3_MAXIMUM_LIST_SIZE
,
133 /* the statistical stuff */
134 OID_802_3_RCV_ERROR_ALIGNMENT
,
135 OID_802_3_XMIT_ONE_COLLISION
,
136 OID_802_3_XMIT_MORE_COLLISIONS
,
137 #ifdef RNDIS_OPTIONAL_STATS
138 OID_802_3_XMIT_DEFERRED
,
139 OID_802_3_XMIT_MAX_COLLISIONS
,
140 OID_802_3_RCV_OVERRUN
,
141 OID_802_3_XMIT_UNDERRUN
,
142 OID_802_3_XMIT_HEARTBEAT_FAILURE
,
143 OID_802_3_XMIT_TIMES_CRS_LOST
,
144 OID_802_3_XMIT_LATE_COLLISIONS
,
145 #endif /* RNDIS_OPTIONAL_STATS */
148 /* PM and wakeup are mandatory for USB: */
150 /* power management */
151 OID_PNP_CAPABILITIES
,
157 OID_PNP_ENABLE_WAKE_UP
,
158 OID_PNP_ADD_WAKE_UP_PATTERN
,
159 OID_PNP_REMOVE_WAKE_UP_PATTERN
,
160 #endif /* RNDIS_WAKEUP */
161 #endif /* RNDIS_PM */
167 gen_ndis_query_resp (int configNr
, u32 OID
, u8
*buf
, unsigned buf_len
,
170 int retval
= -ENOTSUPP
;
171 u32 length
= 4; /* usually */
174 rndis_query_cmplt_type
*resp
;
176 if (!r
) return -ENOMEM
;
177 resp
= (rndis_query_cmplt_type
*) r
->buf
;
179 if (!resp
) return -ENOMEM
;
181 if (buf_len
&& rndis_debug
> 1) {
182 DBG("query OID %08x value, len %d:\n", OID
, buf_len
);
183 for (i
= 0; i
< buf_len
; i
+= 16) {
184 DBG("%03d: %08x %08x %08x %08x\n", i
,
185 get_unaligned_le32(&buf
[i
]),
186 get_unaligned_le32(&buf
[i
+ 4]),
187 get_unaligned_le32(&buf
[i
+ 8]),
188 get_unaligned_le32(&buf
[i
+ 12]));
192 /* response goes here, right after the header */
193 outbuf
= (__le32
*) &resp
[1];
194 resp
->InformationBufferOffset
= __constant_cpu_to_le32 (16);
198 /* general oids (table 4-1) */
201 case OID_GEN_SUPPORTED_LIST
:
202 DBG("%s: OID_GEN_SUPPORTED_LIST\n", __func__
);
203 length
= sizeof (oid_supported_list
);
204 count
= length
/ sizeof (u32
);
205 for (i
= 0; i
< count
; i
++)
206 outbuf
[i
] = cpu_to_le32 (oid_supported_list
[i
]);
211 case OID_GEN_HARDWARE_STATUS
:
212 DBG("%s: OID_GEN_HARDWARE_STATUS\n", __func__
);
214 * Hardware must be ready to receive high level protocols.
216 * reddite ergo quae sunt Caesaris Caesari
217 * et quae sunt Dei Deo!
219 *outbuf
= __constant_cpu_to_le32 (0);
224 case OID_GEN_MEDIA_SUPPORTED
:
225 DBG("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__
);
226 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
].medium
);
231 case OID_GEN_MEDIA_IN_USE
:
232 DBG("%s: OID_GEN_MEDIA_IN_USE\n", __func__
);
233 /* one medium, one transport... (maybe you do it better) */
234 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
].medium
);
239 case OID_GEN_MAXIMUM_FRAME_SIZE
:
240 DBG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__
);
241 if (rndis_per_dev_params
[configNr
].dev
) {
242 *outbuf
= cpu_to_le32 (
243 rndis_per_dev_params
[configNr
].dev
->mtu
);
249 case OID_GEN_LINK_SPEED
:
251 DBG("%s: OID_GEN_LINK_SPEED\n", __func__
);
252 if (rndis_per_dev_params
[configNr
].media_state
253 == NDIS_MEDIA_STATE_DISCONNECTED
)
254 *outbuf
= __constant_cpu_to_le32 (0);
256 *outbuf
= cpu_to_le32 (
257 rndis_per_dev_params
[configNr
].speed
);
262 case OID_GEN_TRANSMIT_BLOCK_SIZE
:
263 DBG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__
);
264 if (rndis_per_dev_params
[configNr
].dev
) {
265 *outbuf
= cpu_to_le32 (
266 rndis_per_dev_params
[configNr
].dev
->mtu
);
272 case OID_GEN_RECEIVE_BLOCK_SIZE
:
273 DBG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__
);
274 if (rndis_per_dev_params
[configNr
].dev
) {
275 *outbuf
= cpu_to_le32 (
276 rndis_per_dev_params
[configNr
].dev
->mtu
);
282 case OID_GEN_VENDOR_ID
:
283 DBG("%s: OID_GEN_VENDOR_ID\n", __func__
);
284 *outbuf
= cpu_to_le32 (
285 rndis_per_dev_params
[configNr
].vendorID
);
290 case OID_GEN_VENDOR_DESCRIPTION
:
291 DBG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__
);
292 length
= strlen (rndis_per_dev_params
[configNr
].vendorDescr
);
294 rndis_per_dev_params
[configNr
].vendorDescr
, length
);
298 case OID_GEN_VENDOR_DRIVER_VERSION
:
299 DBG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__
);
301 *outbuf
= rndis_driver_version
;
306 case OID_GEN_CURRENT_PACKET_FILTER
:
307 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__
);
308 *outbuf
= cpu_to_le32 (*rndis_per_dev_params
[configNr
].filter
);
313 case OID_GEN_MAXIMUM_TOTAL_SIZE
:
314 DBG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__
);
315 *outbuf
= __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE
);
320 case OID_GEN_MEDIA_CONNECT_STATUS
:
322 DBG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__
);
323 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
328 case OID_GEN_PHYSICAL_MEDIUM
:
329 DBG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__
);
330 *outbuf
= __constant_cpu_to_le32 (0);
334 /* The RNDIS specification is incomplete/wrong. Some versions
335 * of MS-Windows expect OIDs that aren't specified there. Other
336 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
338 case OID_GEN_MAC_OPTIONS
: /* from WinME */
339 DBG("%s: OID_GEN_MAC_OPTIONS\n", __func__
);
340 *outbuf
= __constant_cpu_to_le32(
341 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
342 | NDIS_MAC_OPTION_FULL_DUPLEX
);
346 /* statistics OIDs (table 4-2) */
349 case OID_GEN_XMIT_OK
:
351 DBG("%s: OID_GEN_XMIT_OK\n", __func__
);
352 if (rndis_per_dev_params
[configNr
].stats
) {
353 *outbuf
= cpu_to_le32 (
354 rndis_per_dev_params
[configNr
].stats
->tx_packets
-
355 rndis_per_dev_params
[configNr
].stats
->tx_errors
-
356 rndis_per_dev_params
[configNr
].stats
->tx_dropped
);
364 DBG("%s: OID_GEN_RCV_OK\n", __func__
);
365 if (rndis_per_dev_params
[configNr
].stats
) {
366 *outbuf
= cpu_to_le32 (
367 rndis_per_dev_params
[configNr
].stats
->rx_packets
-
368 rndis_per_dev_params
[configNr
].stats
->rx_errors
-
369 rndis_per_dev_params
[configNr
].stats
->rx_dropped
);
375 case OID_GEN_XMIT_ERROR
:
377 DBG("%s: OID_GEN_XMIT_ERROR\n", __func__
);
378 if (rndis_per_dev_params
[configNr
].stats
) {
379 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
386 case OID_GEN_RCV_ERROR
:
388 DBG("%s: OID_GEN_RCV_ERROR\n", __func__
);
389 if (rndis_per_dev_params
[configNr
].stats
) {
390 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
397 case OID_GEN_RCV_NO_BUFFER
:
398 DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __func__
);
399 if (rndis_per_dev_params
[configNr
].stats
) {
400 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
406 #ifdef RNDIS_OPTIONAL_STATS
407 case OID_GEN_DIRECTED_BYTES_XMIT
:
408 DBG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __func__
);
410 * Aunt Tilly's size of shoes
411 * minus antarctica count of penguins
412 * divided by weight of Alpha Centauri
414 if (rndis_per_dev_params
[configNr
].stats
) {
415 *outbuf
= cpu_to_le32 (
416 (rndis_per_dev_params
[configNr
]
418 rndis_per_dev_params
[configNr
]
420 rndis_per_dev_params
[configNr
]
427 case OID_GEN_DIRECTED_FRAMES_XMIT
:
428 DBG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __func__
);
430 if (rndis_per_dev_params
[configNr
].stats
) {
431 *outbuf
= cpu_to_le32 (
432 (rndis_per_dev_params
[configNr
]
434 rndis_per_dev_params
[configNr
]
436 rndis_per_dev_params
[configNr
]
443 case OID_GEN_MULTICAST_BYTES_XMIT
:
444 DBG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __func__
);
445 if (rndis_per_dev_params
[configNr
].stats
) {
446 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
447 .stats
->multicast
*1234);
452 case OID_GEN_MULTICAST_FRAMES_XMIT
:
453 DBG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __func__
);
454 if (rndis_per_dev_params
[configNr
].stats
) {
455 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
461 case OID_GEN_BROADCAST_BYTES_XMIT
:
462 DBG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __func__
);
463 if (rndis_per_dev_params
[configNr
].stats
) {
464 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
465 .stats
->tx_packets
/42*255);
470 case OID_GEN_BROADCAST_FRAMES_XMIT
:
471 DBG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __func__
);
472 if (rndis_per_dev_params
[configNr
].stats
) {
473 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
474 .stats
->tx_packets
/42);
479 case OID_GEN_DIRECTED_BYTES_RCV
:
480 DBG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __func__
);
481 *outbuf
= __constant_cpu_to_le32 (0);
485 case OID_GEN_DIRECTED_FRAMES_RCV
:
486 DBG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __func__
);
487 *outbuf
= __constant_cpu_to_le32 (0);
491 case OID_GEN_MULTICAST_BYTES_RCV
:
492 DBG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __func__
);
493 if (rndis_per_dev_params
[configNr
].stats
) {
494 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
495 .stats
->multicast
* 1111);
500 case OID_GEN_MULTICAST_FRAMES_RCV
:
501 DBG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __func__
);
502 if (rndis_per_dev_params
[configNr
].stats
) {
503 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
509 case OID_GEN_BROADCAST_BYTES_RCV
:
510 DBG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __func__
);
511 if (rndis_per_dev_params
[configNr
].stats
) {
512 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
513 .stats
->rx_packets
/42*255);
518 case OID_GEN_BROADCAST_FRAMES_RCV
:
519 DBG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __func__
);
520 if (rndis_per_dev_params
[configNr
].stats
) {
521 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
522 .stats
->rx_packets
/42);
527 case OID_GEN_RCV_CRC_ERROR
:
528 DBG("%s: OID_GEN_RCV_CRC_ERROR\n", __func__
);
529 if (rndis_per_dev_params
[configNr
].stats
) {
530 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
531 .stats
->rx_crc_errors
);
536 case OID_GEN_TRANSMIT_QUEUE_LENGTH
:
537 DBG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __func__
);
538 *outbuf
= __constant_cpu_to_le32 (0);
541 #endif /* RNDIS_OPTIONAL_STATS */
543 /* ieee802.3 OIDs (table 4-3) */
546 case OID_802_3_PERMANENT_ADDRESS
:
547 DBG("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__
);
548 if (rndis_per_dev_params
[configNr
].dev
) {
551 rndis_per_dev_params
[configNr
].host_mac
,
558 case OID_802_3_CURRENT_ADDRESS
:
559 DBG("%s: OID_802_3_CURRENT_ADDRESS\n", __func__
);
560 if (rndis_per_dev_params
[configNr
].dev
) {
563 rndis_per_dev_params
[configNr
].host_mac
,
570 case OID_802_3_MULTICAST_LIST
:
571 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__
);
572 /* Multicast base address only */
573 *outbuf
= __constant_cpu_to_le32 (0xE0000000);
578 case OID_802_3_MAXIMUM_LIST_SIZE
:
579 DBG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__
);
580 /* Multicast base address only */
581 *outbuf
= __constant_cpu_to_le32 (1);
585 case OID_802_3_MAC_OPTIONS
:
586 DBG("%s: OID_802_3_MAC_OPTIONS\n", __func__
);
589 /* ieee802.3 statistics OIDs (table 4-4) */
592 case OID_802_3_RCV_ERROR_ALIGNMENT
:
593 DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__
);
594 if (rndis_per_dev_params
[configNr
].stats
) {
595 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
596 .stats
->rx_frame_errors
);
602 case OID_802_3_XMIT_ONE_COLLISION
:
603 DBG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__
);
604 *outbuf
= __constant_cpu_to_le32 (0);
609 case OID_802_3_XMIT_MORE_COLLISIONS
:
610 DBG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__
);
611 *outbuf
= __constant_cpu_to_le32 (0);
615 #ifdef RNDIS_OPTIONAL_STATS
616 case OID_802_3_XMIT_DEFERRED
:
617 DBG("%s: OID_802_3_XMIT_DEFERRED\n", __func__
);
621 case OID_802_3_XMIT_MAX_COLLISIONS
:
622 DBG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __func__
);
626 case OID_802_3_RCV_OVERRUN
:
627 DBG("%s: OID_802_3_RCV_OVERRUN\n", __func__
);
631 case OID_802_3_XMIT_UNDERRUN
:
632 DBG("%s: OID_802_3_XMIT_UNDERRUN\n", __func__
);
636 case OID_802_3_XMIT_HEARTBEAT_FAILURE
:
637 DBG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __func__
);
641 case OID_802_3_XMIT_TIMES_CRS_LOST
:
642 DBG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __func__
);
646 case OID_802_3_XMIT_LATE_COLLISIONS
:
647 DBG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __func__
);
650 #endif /* RNDIS_OPTIONAL_STATS */
653 /* power management OIDs (table 4-5) */
654 case OID_PNP_CAPABILITIES
:
655 DBG("%s: OID_PNP_CAPABILITIES\n", __func__
);
657 /* for now, no wakeup capabilities */
658 length
= sizeof (struct NDIS_PNP_CAPABILITIES
);
659 memset(outbuf
, 0, length
);
662 case OID_PNP_QUERY_POWER
:
663 DBG("%s: OID_PNP_QUERY_POWER D%d\n", __func__
,
664 get_unaligned_le32(buf
) - 1);
665 /* only suspend is a real power state, and
666 * it can't be entered by OID_PNP_SET_POWER...
674 pr_warning("%s: query unknown OID 0x%08X\n",
680 resp
->InformationBufferLength
= cpu_to_le32 (length
);
681 r
->length
= length
+ sizeof *resp
;
682 resp
->MessageLength
= cpu_to_le32 (r
->length
);
686 static int gen_ndis_set_resp (u8 configNr
, u32 OID
, u8
*buf
, u32 buf_len
,
689 rndis_set_cmplt_type
*resp
;
690 int i
, retval
= -ENOTSUPP
;
691 struct rndis_params
*params
;
695 resp
= (rndis_set_cmplt_type
*) r
->buf
;
699 if (buf_len
&& rndis_debug
> 1) {
700 DBG("set OID %08x value, len %d:\n", OID
, buf_len
);
701 for (i
= 0; i
< buf_len
; i
+= 16) {
702 DBG("%03d: %08x %08x %08x %08x\n", i
,
703 get_unaligned_le32(&buf
[i
]),
704 get_unaligned_le32(&buf
[i
+ 4]),
705 get_unaligned_le32(&buf
[i
+ 8]),
706 get_unaligned_le32(&buf
[i
+ 12]));
710 params
= &rndis_per_dev_params
[configNr
];
712 case OID_GEN_CURRENT_PACKET_FILTER
:
714 /* these NDIS_PACKET_TYPE_* bitflags are shared with
715 * cdc_filter; it's not RNDIS-specific
716 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
717 * PROMISCUOUS, DIRECTED,
718 * MULTICAST, ALL_MULTICAST, BROADCAST
720 *params
->filter
= (u16
)get_unaligned_le32(buf
);
721 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
722 __func__
, *params
->filter
);
724 /* this call has a significant side effect: it's
725 * what makes the packet flow start and stop, like
726 * activating the CDC Ethernet altsetting.
732 if (*params
->filter
) {
733 params
->state
= RNDIS_DATA_INITIALIZED
;
734 netif_carrier_on(params
->dev
);
735 if (netif_running(params
->dev
))
736 netif_wake_queue (params
->dev
);
738 params
->state
= RNDIS_INITIALIZED
;
739 netif_carrier_off (params
->dev
);
740 netif_stop_queue (params
->dev
);
744 case OID_802_3_MULTICAST_LIST
:
745 /* I think we can ignore this */
746 DBG("%s: OID_802_3_MULTICAST_LIST\n", __func__
);
750 case OID_GEN_RNDIS_CONFIG_PARAMETER
:
752 struct rndis_config_parameter
*param
;
753 param
= (struct rndis_config_parameter
*) buf
;
754 DBG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
756 min(cpu_to_le32(param
->ParameterNameLength
),80),
757 buf
+ param
->ParameterNameOffset
);
764 case OID_PNP_SET_POWER
:
765 /* The only real power state is USB suspend, and RNDIS requests
766 * can't enter it; this one isn't really about power. After
767 * resuming, Windows forces a reset, and then SET_POWER D0.
768 * FIXME ... then things go batty; Windows wedges itself.
770 i
= get_unaligned_le32(buf
);
771 DBG("%s: OID_PNP_SET_POWER D%d\n", __func__
, i
- 1);
773 case NdisDeviceStateD0
:
774 *params
->filter
= params
->saved_filter
;
775 goto update_linkstate
;
776 case NdisDeviceStateD3
:
777 case NdisDeviceStateD2
:
778 case NdisDeviceStateD1
:
779 params
->saved_filter
= *params
->filter
;
786 // no wakeup support advertised, so wakeup OIDs always fail:
787 // - OID_PNP_ENABLE_WAKE_UP
788 // - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
791 #endif /* RNDIS_PM */
794 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
795 __func__
, OID
, buf_len
);
805 static int rndis_init_response (int configNr
, rndis_init_msg_type
*buf
)
807 rndis_init_cmplt_type
*resp
;
810 if (!rndis_per_dev_params
[configNr
].dev
) return -ENOTSUPP
;
812 r
= rndis_add_response (configNr
, sizeof (rndis_init_cmplt_type
));
815 resp
= (rndis_init_cmplt_type
*) r
->buf
;
817 resp
->MessageType
= __constant_cpu_to_le32 (
818 REMOTE_NDIS_INITIALIZE_CMPLT
);
819 resp
->MessageLength
= __constant_cpu_to_le32 (52);
820 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
821 resp
->Status
= __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
822 resp
->MajorVersion
= __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION
);
823 resp
->MinorVersion
= __constant_cpu_to_le32 (RNDIS_MINOR_VERSION
);
824 resp
->DeviceFlags
= __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS
);
825 resp
->Medium
= __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3
);
826 resp
->MaxPacketsPerTransfer
= __constant_cpu_to_le32 (1);
827 resp
->MaxTransferSize
= cpu_to_le32 (
828 rndis_per_dev_params
[configNr
].dev
->mtu
829 + sizeof (struct ethhdr
)
830 + sizeof (struct rndis_packet_msg_type
)
832 resp
->PacketAlignmentFactor
= __constant_cpu_to_le32 (0);
833 resp
->AFListOffset
= __constant_cpu_to_le32 (0);
834 resp
->AFListSize
= __constant_cpu_to_le32 (0);
836 if (rndis_per_dev_params
[configNr
].ack
)
837 rndis_per_dev_params
[configNr
].ack (
838 rndis_per_dev_params
[configNr
].dev
);
843 static int rndis_query_response (int configNr
, rndis_query_msg_type
*buf
)
845 rndis_query_cmplt_type
*resp
;
848 // DBG("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID));
849 if (!rndis_per_dev_params
[configNr
].dev
) return -ENOTSUPP
;
852 * we need more memory:
853 * gen_ndis_query_resp expects enough space for
854 * rndis_query_cmplt_type followed by data.
855 * oid_supported_list is the largest data reply
857 r
= rndis_add_response (configNr
,
858 sizeof (oid_supported_list
) + sizeof(rndis_query_cmplt_type
));
861 resp
= (rndis_query_cmplt_type
*) r
->buf
;
863 resp
->MessageType
= __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT
);
864 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
866 if (gen_ndis_query_resp (configNr
, le32_to_cpu (buf
->OID
),
867 le32_to_cpu(buf
->InformationBufferOffset
)
869 le32_to_cpu(buf
->InformationBufferLength
),
871 /* OID not supported */
872 resp
->Status
= __constant_cpu_to_le32 (
873 RNDIS_STATUS_NOT_SUPPORTED
);
874 resp
->MessageLength
= __constant_cpu_to_le32 (sizeof *resp
);
875 resp
->InformationBufferLength
= __constant_cpu_to_le32 (0);
876 resp
->InformationBufferOffset
= __constant_cpu_to_le32 (0);
878 resp
->Status
= __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
880 if (rndis_per_dev_params
[configNr
].ack
)
881 rndis_per_dev_params
[configNr
].ack (
882 rndis_per_dev_params
[configNr
].dev
);
886 static int rndis_set_response (int configNr
, rndis_set_msg_type
*buf
)
888 u32 BufLength
, BufOffset
;
889 rndis_set_cmplt_type
*resp
;
892 r
= rndis_add_response (configNr
, sizeof (rndis_set_cmplt_type
));
895 resp
= (rndis_set_cmplt_type
*) r
->buf
;
897 BufLength
= le32_to_cpu (buf
->InformationBufferLength
);
898 BufOffset
= le32_to_cpu (buf
->InformationBufferOffset
);
901 DBG("%s: Length: %d\n", __func__
, BufLength
);
902 DBG("%s: Offset: %d\n", __func__
, BufOffset
);
903 DBG("%s: InfoBuffer: ", __func__
);
905 for (i
= 0; i
< BufLength
; i
++) {
906 DBG("%02x ", *(((u8
*) buf
) + i
+ 8 + BufOffset
));
912 resp
->MessageType
= __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT
);
913 resp
->MessageLength
= __constant_cpu_to_le32 (16);
914 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
915 if (gen_ndis_set_resp (configNr
, le32_to_cpu (buf
->OID
),
916 ((u8
*) buf
) + 8 + BufOffset
, BufLength
, r
))
917 resp
->Status
= __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED
);
919 resp
->Status
= __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
921 if (rndis_per_dev_params
[configNr
].ack
)
922 rndis_per_dev_params
[configNr
].ack (
923 rndis_per_dev_params
[configNr
].dev
);
928 static int rndis_reset_response (int configNr
, rndis_reset_msg_type
*buf
)
930 rndis_reset_cmplt_type
*resp
;
933 r
= rndis_add_response (configNr
, sizeof (rndis_reset_cmplt_type
));
936 resp
= (rndis_reset_cmplt_type
*) r
->buf
;
938 resp
->MessageType
= __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT
);
939 resp
->MessageLength
= __constant_cpu_to_le32 (16);
940 resp
->Status
= __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
941 /* resent information */
942 resp
->AddressingReset
= __constant_cpu_to_le32 (1);
944 if (rndis_per_dev_params
[configNr
].ack
)
945 rndis_per_dev_params
[configNr
].ack (
946 rndis_per_dev_params
[configNr
].dev
);
951 static int rndis_keepalive_response (int configNr
,
952 rndis_keepalive_msg_type
*buf
)
954 rndis_keepalive_cmplt_type
*resp
;
957 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
959 r
= rndis_add_response (configNr
, sizeof (rndis_keepalive_cmplt_type
));
962 resp
= (rndis_keepalive_cmplt_type
*) r
->buf
;
964 resp
->MessageType
= __constant_cpu_to_le32 (
965 REMOTE_NDIS_KEEPALIVE_CMPLT
);
966 resp
->MessageLength
= __constant_cpu_to_le32 (16);
967 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
968 resp
->Status
= __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
970 if (rndis_per_dev_params
[configNr
].ack
)
971 rndis_per_dev_params
[configNr
].ack (
972 rndis_per_dev_params
[configNr
].dev
);
979 * Device to Host Comunication
981 static int rndis_indicate_status_msg (int configNr
, u32 status
)
983 rndis_indicate_status_msg_type
*resp
;
986 if (rndis_per_dev_params
[configNr
].state
== RNDIS_UNINITIALIZED
)
989 r
= rndis_add_response (configNr
,
990 sizeof (rndis_indicate_status_msg_type
));
993 resp
= (rndis_indicate_status_msg_type
*) r
->buf
;
995 resp
->MessageType
= __constant_cpu_to_le32 (
996 REMOTE_NDIS_INDICATE_STATUS_MSG
);
997 resp
->MessageLength
= __constant_cpu_to_le32 (20);
998 resp
->Status
= cpu_to_le32 (status
);
999 resp
->StatusBufferLength
= __constant_cpu_to_le32 (0);
1000 resp
->StatusBufferOffset
= __constant_cpu_to_le32 (0);
1002 if (rndis_per_dev_params
[configNr
].ack
)
1003 rndis_per_dev_params
[configNr
].ack (
1004 rndis_per_dev_params
[configNr
].dev
);
1008 int rndis_signal_connect (int configNr
)
1010 rndis_per_dev_params
[configNr
].media_state
1011 = NDIS_MEDIA_STATE_CONNECTED
;
1012 return rndis_indicate_status_msg (configNr
,
1013 RNDIS_STATUS_MEDIA_CONNECT
);
1016 int rndis_signal_disconnect (int configNr
)
1018 rndis_per_dev_params
[configNr
].media_state
1019 = NDIS_MEDIA_STATE_DISCONNECTED
;
1020 return rndis_indicate_status_msg (configNr
,
1021 RNDIS_STATUS_MEDIA_DISCONNECT
);
1024 void rndis_uninit (int configNr
)
1029 if (configNr
>= RNDIS_MAX_CONFIGS
)
1031 rndis_per_dev_params
[configNr
].used
= 0;
1032 rndis_per_dev_params
[configNr
].state
= RNDIS_UNINITIALIZED
;
1034 /* drain the response queue */
1035 while ((buf
= rndis_get_next_response(configNr
, &length
)))
1036 rndis_free_response(configNr
, buf
);
1039 void rndis_set_host_mac (int configNr
, const u8
*addr
)
1041 rndis_per_dev_params
[configNr
].host_mac
= addr
;
1047 int rndis_msg_parser (u8 configNr
, u8
*buf
)
1049 u32 MsgType
, MsgLength
;
1051 struct rndis_params
*params
;
1056 tmp
= (__le32
*) buf
;
1057 MsgType
= get_unaligned_le32(tmp
++);
1058 MsgLength
= get_unaligned_le32(tmp
++);
1060 if (configNr
>= RNDIS_MAX_CONFIGS
)
1062 params
= &rndis_per_dev_params
[configNr
];
1064 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1065 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1066 * and normal HC level polling to see if there's any IN traffic.
1069 /* For USB: responses may take up to 10 seconds */
1071 case REMOTE_NDIS_INITIALIZE_MSG
:
1072 DBG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
1074 params
->state
= RNDIS_INITIALIZED
;
1075 return rndis_init_response (configNr
,
1076 (rndis_init_msg_type
*) buf
);
1078 case REMOTE_NDIS_HALT_MSG
:
1079 DBG("%s: REMOTE_NDIS_HALT_MSG\n",
1081 params
->state
= RNDIS_UNINITIALIZED
;
1083 netif_carrier_off (params
->dev
);
1084 netif_stop_queue (params
->dev
);
1088 case REMOTE_NDIS_QUERY_MSG
:
1089 return rndis_query_response (configNr
,
1090 (rndis_query_msg_type
*) buf
);
1092 case REMOTE_NDIS_SET_MSG
:
1093 return rndis_set_response (configNr
,
1094 (rndis_set_msg_type
*) buf
);
1096 case REMOTE_NDIS_RESET_MSG
:
1097 DBG("%s: REMOTE_NDIS_RESET_MSG\n",
1099 return rndis_reset_response (configNr
,
1100 (rndis_reset_msg_type
*) buf
);
1102 case REMOTE_NDIS_KEEPALIVE_MSG
:
1103 /* For USB: host does this every 5 seconds */
1104 if (rndis_debug
> 1)
1105 DBG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
1107 return rndis_keepalive_response (configNr
,
1108 (rndis_keepalive_msg_type
*)
1112 /* At least Windows XP emits some undefined RNDIS messages.
1113 * In one case those messages seemed to relate to the host
1114 * suspending itself.
1116 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
1117 __func__
, MsgType
, MsgLength
);
1120 for (i
= 0; i
< MsgLength
; i
+= 16) {
1122 " %02x %02x %02x %02x"
1123 " %02x %02x %02x %02x"
1124 " %02x %02x %02x %02x"
1125 " %02x %02x %02x %02x"
1130 buf
[i
+4], buf
[i
+5],
1132 buf
[i
+8], buf
[i
+9],
1133 buf
[i
+10], buf
[i
+11],
1134 buf
[i
+12], buf
[i
+13],
1135 buf
[i
+14], buf
[i
+15]);
1144 int rndis_register (int (* rndis_control_ack
) (struct net_device
*))
1148 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1149 if (!rndis_per_dev_params
[i
].used
) {
1150 rndis_per_dev_params
[i
].used
= 1;
1151 rndis_per_dev_params
[i
].ack
= rndis_control_ack
;
1152 DBG("%s: configNr = %d\n", __func__
, i
);
1161 void rndis_deregister (int configNr
)
1163 DBG("%s: \n", __func__
);
1165 if (configNr
>= RNDIS_MAX_CONFIGS
) return;
1166 rndis_per_dev_params
[configNr
].used
= 0;
1171 int rndis_set_param_dev (u8 configNr
, struct net_device
*dev
,
1172 struct net_device_stats
*stats
,
1175 DBG("%s:\n", __func__
);
1176 if (!dev
|| !stats
) return -1;
1177 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
1179 rndis_per_dev_params
[configNr
].dev
= dev
;
1180 rndis_per_dev_params
[configNr
].stats
= stats
;
1181 rndis_per_dev_params
[configNr
].filter
= cdc_filter
;
1186 int rndis_set_param_vendor (u8 configNr
, u32 vendorID
, const char *vendorDescr
)
1188 DBG("%s:\n", __func__
);
1189 if (!vendorDescr
) return -1;
1190 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
1192 rndis_per_dev_params
[configNr
].vendorID
= vendorID
;
1193 rndis_per_dev_params
[configNr
].vendorDescr
= vendorDescr
;
1198 int rndis_set_param_medium (u8 configNr
, u32 medium
, u32 speed
)
1200 DBG("%s: %u %u\n", __func__
, medium
, speed
);
1201 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
1203 rndis_per_dev_params
[configNr
].medium
= medium
;
1204 rndis_per_dev_params
[configNr
].speed
= speed
;
1209 void rndis_add_hdr (struct sk_buff
*skb
)
1211 struct rndis_packet_msg_type
*header
;
1215 header
= (void *) skb_push (skb
, sizeof *header
);
1216 memset (header
, 0, sizeof *header
);
1217 header
->MessageType
= __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG
);
1218 header
->MessageLength
= cpu_to_le32(skb
->len
);
1219 header
->DataOffset
= __constant_cpu_to_le32 (36);
1220 header
->DataLength
= cpu_to_le32(skb
->len
- sizeof *header
);
1223 void rndis_free_response (int configNr
, u8
*buf
)
1226 struct list_head
*act
, *tmp
;
1228 list_for_each_safe (act
, tmp
,
1229 &(rndis_per_dev_params
[configNr
].resp_queue
))
1231 r
= list_entry (act
, rndis_resp_t
, list
);
1232 if (r
&& r
->buf
== buf
) {
1233 list_del (&r
->list
);
1239 u8
*rndis_get_next_response (int configNr
, u32
*length
)
1242 struct list_head
*act
, *tmp
;
1244 if (!length
) return NULL
;
1246 list_for_each_safe (act
, tmp
,
1247 &(rndis_per_dev_params
[configNr
].resp_queue
))
1249 r
= list_entry (act
, rndis_resp_t
, list
);
1252 *length
= r
->length
;
1260 static rndis_resp_t
*rndis_add_response (int configNr
, u32 length
)
1264 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1265 r
= kmalloc (sizeof (rndis_resp_t
) + length
, GFP_ATOMIC
);
1266 if (!r
) return NULL
;
1268 r
->buf
= (u8
*) (r
+ 1);
1272 list_add_tail (&r
->list
,
1273 &(rndis_per_dev_params
[configNr
].resp_queue
));
1277 int rndis_rm_hdr(struct sk_buff
*skb
)
1279 /* tmp points to a struct rndis_packet_msg_type */
1280 __le32
*tmp
= (void *) skb
->data
;
1282 /* MessageType, MessageLength */
1283 if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG
)
1284 != get_unaligned(tmp
++))
1288 /* DataOffset, DataLength */
1289 if (!skb_pull(skb
, get_unaligned_le32(tmp
++) + 8))
1291 skb_trim(skb
, get_unaligned_le32(tmp
++));
1296 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1298 static int rndis_proc_show(struct seq_file
*m
, void *v
)
1300 rndis_params
*param
= m
->private;
1309 "vendor ID : 0x%08X\n"
1311 param
->confignr
, (param
->used
) ? "y" : "n",
1313 switch (param
->state
) {
1314 case RNDIS_UNINITIALIZED
:
1315 s
= "RNDIS_UNINITIALIZED"; break;
1316 case RNDIS_INITIALIZED
:
1317 s
= "RNDIS_INITIALIZED"; break;
1318 case RNDIS_DATA_INITIALIZED
:
1319 s
= "RNDIS_DATA_INITIALIZED"; break;
1322 (param
->media_state
) ? 0 : param
->speed
*100,
1323 (param
->media_state
) ? "disconnected" : "connected",
1324 param
->vendorID
, param
->vendorDescr
);
1328 static ssize_t
rndis_proc_write(struct file
*file
, const char __user
*buffer
,
1329 size_t count
, loff_t
*ppos
)
1331 rndis_params
*p
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1333 int i
, fl_speed
= 0;
1335 for (i
= 0; i
< count
; i
++) {
1337 if (get_user(c
, buffer
))
1351 speed
= speed
*10 + c
- '0';
1355 rndis_signal_connect (p
->confignr
);
1359 rndis_signal_disconnect(p
->confignr
);
1362 if (fl_speed
) p
->speed
= speed
;
1363 else DBG("%c is not valid\n", c
);
1373 static int rndis_proc_open(struct inode
*inode
, struct file
*file
)
1375 return single_open(file
, rndis_proc_show
, PDE(inode
)->data
);
1378 static const struct file_operations rndis_proc_fops
= {
1379 .owner
= THIS_MODULE
,
1380 .open
= rndis_proc_open
,
1382 .llseek
= seq_lseek
,
1383 .release
= single_release
,
1384 .write
= rndis_proc_write
,
1387 #define NAME_TEMPLATE "driver/rndis-%03d"
1389 static struct proc_dir_entry
*rndis_connect_state
[RNDIS_MAX_CONFIGS
];
1391 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1394 int __init
rndis_init (void)
1398 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1399 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1402 sprintf (name
, NAME_TEMPLATE
, i
);
1403 if (!(rndis_connect_state
[i
]
1404 = proc_create_data(name
, 0660, NULL
,
1406 (void *)(rndis_per_dev_params
+ i
))))
1408 DBG("%s :remove entries", __func__
);
1410 sprintf (name
, NAME_TEMPLATE
, --i
);
1411 remove_proc_entry (name
, NULL
);
1417 rndis_per_dev_params
[i
].confignr
= i
;
1418 rndis_per_dev_params
[i
].used
= 0;
1419 rndis_per_dev_params
[i
].state
= RNDIS_UNINITIALIZED
;
1420 rndis_per_dev_params
[i
].media_state
1421 = NDIS_MEDIA_STATE_DISCONNECTED
;
1422 INIT_LIST_HEAD (&(rndis_per_dev_params
[i
].resp_queue
));
1428 void rndis_exit (void)
1430 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1434 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1435 sprintf (name
, NAME_TEMPLATE
, i
);
1436 remove_proc_entry (name
, NULL
);