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/list.h>
29 #include <linux/proc_fs.h>
30 #include <linux/slab.h>
31 #include <linux/seq_file.h>
32 #include <linux/netdevice.h>
35 #include <asm/byteorder.h>
36 #include <asm/unaligned.h>
44 /* The driver for your USB chip needs to support ep0 OUT to work with
45 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
47 * Windows hosts need an INF file like Documentation/usb/linux.inf
48 * and will be happier if you provide the host_addr module parameter.
52 static int rndis_debug
= 0;
53 module_param (rndis_debug
, int, 0);
54 MODULE_PARM_DESC (rndis_debug
, "enable debugging");
59 #define RNDIS_MAX_CONFIGS 1
62 static rndis_params rndis_per_dev_params
[RNDIS_MAX_CONFIGS
];
65 static const __le32 rndis_driver_version
= cpu_to_le32(1);
67 /* Function Prototypes */
68 static rndis_resp_t
*rndis_add_response(int configNr
, u32 length
);
72 static const u32 oid_supported_list
[] =
74 /* the general stuff */
75 RNDIS_OID_GEN_SUPPORTED_LIST
,
76 RNDIS_OID_GEN_HARDWARE_STATUS
,
77 RNDIS_OID_GEN_MEDIA_SUPPORTED
,
78 RNDIS_OID_GEN_MEDIA_IN_USE
,
79 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE
,
80 RNDIS_OID_GEN_LINK_SPEED
,
81 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE
,
82 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE
,
83 RNDIS_OID_GEN_VENDOR_ID
,
84 RNDIS_OID_GEN_VENDOR_DESCRIPTION
,
85 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION
,
86 RNDIS_OID_GEN_CURRENT_PACKET_FILTER
,
87 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE
,
88 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
,
89 RNDIS_OID_GEN_PHYSICAL_MEDIUM
,
91 /* the statistical stuff */
92 RNDIS_OID_GEN_XMIT_OK
,
94 RNDIS_OID_GEN_XMIT_ERROR
,
95 RNDIS_OID_GEN_RCV_ERROR
,
96 RNDIS_OID_GEN_RCV_NO_BUFFER
,
97 #ifdef RNDIS_OPTIONAL_STATS
98 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT
,
99 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT
,
100 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT
,
101 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT
,
102 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT
,
103 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT
,
104 RNDIS_OID_GEN_DIRECTED_BYTES_RCV
,
105 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV
,
106 RNDIS_OID_GEN_MULTICAST_BYTES_RCV
,
107 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV
,
108 RNDIS_OID_GEN_BROADCAST_BYTES_RCV
,
109 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV
,
110 RNDIS_OID_GEN_RCV_CRC_ERROR
,
111 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH
,
112 #endif /* RNDIS_OPTIONAL_STATS */
114 /* mandatory 802.3 */
115 /* the general stuff */
116 RNDIS_OID_802_3_PERMANENT_ADDRESS
,
117 RNDIS_OID_802_3_CURRENT_ADDRESS
,
118 RNDIS_OID_802_3_MULTICAST_LIST
,
119 RNDIS_OID_802_3_MAC_OPTIONS
,
120 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE
,
122 /* the statistical stuff */
123 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT
,
124 RNDIS_OID_802_3_XMIT_ONE_COLLISION
,
125 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS
,
126 #ifdef RNDIS_OPTIONAL_STATS
127 RNDIS_OID_802_3_XMIT_DEFERRED
,
128 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS
,
129 RNDIS_OID_802_3_RCV_OVERRUN
,
130 RNDIS_OID_802_3_XMIT_UNDERRUN
,
131 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE
,
132 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST
,
133 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS
,
134 #endif /* RNDIS_OPTIONAL_STATS */
137 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
138 * don't say what they mean ... and the NDIS specs are often
139 * confusing and/or ambiguous in this context. (That is, more
140 * so than their specs for the other OIDs.)
142 * FIXME someone who knows what these should do, please
146 /* power management */
147 OID_PNP_CAPABILITIES
,
153 OID_PNP_ENABLE_WAKE_UP
,
154 OID_PNP_ADD_WAKE_UP_PATTERN
,
155 OID_PNP_REMOVE_WAKE_UP_PATTERN
,
156 #endif /* RNDIS_WAKEUP */
157 #endif /* RNDIS_PM */
162 static int gen_ndis_query_resp(int configNr
, u32 OID
, u8
*buf
,
163 unsigned buf_len
, rndis_resp_t
*r
)
165 int retval
= -ENOTSUPP
;
166 u32 length
= 4; /* usually */
169 rndis_query_cmplt_type
*resp
;
170 struct net_device
*net
;
171 struct rtnl_link_stats64 temp
;
172 const struct rtnl_link_stats64
*stats
;
174 if (!r
) return -ENOMEM
;
175 resp
= (rndis_query_cmplt_type
*)r
->buf
;
177 if (!resp
) return -ENOMEM
;
179 if (buf_len
&& rndis_debug
> 1) {
180 pr_debug("query OID %08x value, len %d:\n", OID
, buf_len
);
181 for (i
= 0; i
< buf_len
; i
+= 16) {
182 pr_debug("%03d: %08x %08x %08x %08x\n", i
,
183 get_unaligned_le32(&buf
[i
]),
184 get_unaligned_le32(&buf
[i
+ 4]),
185 get_unaligned_le32(&buf
[i
+ 8]),
186 get_unaligned_le32(&buf
[i
+ 12]));
190 /* response goes here, right after the header */
191 outbuf
= (__le32
*)&resp
[1];
192 resp
->InformationBufferOffset
= cpu_to_le32(16);
194 net
= rndis_per_dev_params
[configNr
].dev
;
195 stats
= dev_get_stats(net
, &temp
);
199 /* general oids (table 4-1) */
202 case RNDIS_OID_GEN_SUPPORTED_LIST
:
203 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__
);
204 length
= sizeof(oid_supported_list
);
205 count
= length
/ sizeof(u32
);
206 for (i
= 0; i
< count
; i
++)
207 outbuf
[i
] = cpu_to_le32(oid_supported_list
[i
]);
212 case RNDIS_OID_GEN_HARDWARE_STATUS
:
213 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__
);
215 * Hardware must be ready to receive high level protocols.
217 * reddite ergo quae sunt Caesaris Caesari
218 * et quae sunt Dei Deo!
220 *outbuf
= cpu_to_le32(0);
225 case RNDIS_OID_GEN_MEDIA_SUPPORTED
:
226 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__
);
227 *outbuf
= cpu_to_le32(rndis_per_dev_params
[configNr
].medium
);
232 case RNDIS_OID_GEN_MEDIA_IN_USE
:
233 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__
);
234 /* one medium, one transport... (maybe you do it better) */
235 *outbuf
= cpu_to_le32(rndis_per_dev_params
[configNr
].medium
);
240 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE
:
241 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__
);
242 if (rndis_per_dev_params
[configNr
].dev
) {
243 *outbuf
= cpu_to_le32(
244 rndis_per_dev_params
[configNr
].dev
->mtu
);
250 case RNDIS_OID_GEN_LINK_SPEED
:
252 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__
);
253 if (rndis_per_dev_params
[configNr
].media_state
254 == RNDIS_MEDIA_STATE_DISCONNECTED
)
255 *outbuf
= cpu_to_le32(0);
257 *outbuf
= cpu_to_le32(
258 rndis_per_dev_params
[configNr
].speed
);
263 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE
:
264 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__
);
265 if (rndis_per_dev_params
[configNr
].dev
) {
266 *outbuf
= cpu_to_le32(
267 rndis_per_dev_params
[configNr
].dev
->mtu
);
273 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE
:
274 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__
);
275 if (rndis_per_dev_params
[configNr
].dev
) {
276 *outbuf
= cpu_to_le32(
277 rndis_per_dev_params
[configNr
].dev
->mtu
);
283 case RNDIS_OID_GEN_VENDOR_ID
:
284 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__
);
285 *outbuf
= cpu_to_le32(
286 rndis_per_dev_params
[configNr
].vendorID
);
291 case RNDIS_OID_GEN_VENDOR_DESCRIPTION
:
292 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__
);
293 if (rndis_per_dev_params
[configNr
].vendorDescr
) {
294 length
= strlen(rndis_per_dev_params
[configNr
].
297 rndis_per_dev_params
[configNr
].vendorDescr
,
305 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION
:
306 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__
);
308 *outbuf
= rndis_driver_version
;
313 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER
:
314 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__
);
315 *outbuf
= cpu_to_le32(*rndis_per_dev_params
[configNr
].filter
);
320 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE
:
321 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__
);
322 *outbuf
= cpu_to_le32(RNDIS_MAX_TOTAL_SIZE
);
327 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
:
329 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__
);
330 *outbuf
= cpu_to_le32(rndis_per_dev_params
[configNr
]
335 case RNDIS_OID_GEN_PHYSICAL_MEDIUM
:
336 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__
);
337 *outbuf
= cpu_to_le32(0);
341 /* The RNDIS specification is incomplete/wrong. Some versions
342 * of MS-Windows expect OIDs that aren't specified there. Other
343 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
345 case RNDIS_OID_GEN_MAC_OPTIONS
: /* from WinME */
346 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__
);
347 *outbuf
= cpu_to_le32(
348 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
349 | RNDIS_MAC_OPTION_FULL_DUPLEX
);
353 /* statistics OIDs (table 4-2) */
356 case RNDIS_OID_GEN_XMIT_OK
:
358 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__
);
360 *outbuf
= cpu_to_le32(stats
->tx_packets
361 - stats
->tx_errors
- stats
->tx_dropped
);
367 case RNDIS_OID_GEN_RCV_OK
:
369 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__
);
371 *outbuf
= cpu_to_le32(stats
->rx_packets
372 - stats
->rx_errors
- stats
->rx_dropped
);
378 case RNDIS_OID_GEN_XMIT_ERROR
:
380 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__
);
382 *outbuf
= cpu_to_le32(stats
->tx_errors
);
388 case RNDIS_OID_GEN_RCV_ERROR
:
390 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__
);
392 *outbuf
= cpu_to_le32(stats
->rx_errors
);
398 case RNDIS_OID_GEN_RCV_NO_BUFFER
:
399 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__
);
401 *outbuf
= cpu_to_le32(stats
->rx_dropped
);
406 /* ieee802.3 OIDs (table 4-3) */
409 case RNDIS_OID_802_3_PERMANENT_ADDRESS
:
410 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__
);
411 if (rndis_per_dev_params
[configNr
].dev
) {
414 rndis_per_dev_params
[configNr
].host_mac
,
421 case RNDIS_OID_802_3_CURRENT_ADDRESS
:
422 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__
);
423 if (rndis_per_dev_params
[configNr
].dev
) {
426 rndis_per_dev_params
[configNr
].host_mac
,
433 case RNDIS_OID_802_3_MULTICAST_LIST
:
434 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__
);
435 /* Multicast base address only */
436 *outbuf
= cpu_to_le32(0xE0000000);
441 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE
:
442 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__
);
443 /* Multicast base address only */
444 *outbuf
= cpu_to_le32(1);
448 case RNDIS_OID_802_3_MAC_OPTIONS
:
449 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__
);
450 *outbuf
= cpu_to_le32(0);
454 /* ieee802.3 statistics OIDs (table 4-4) */
457 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT
:
458 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__
);
460 *outbuf
= cpu_to_le32(stats
->rx_frame_errors
);
466 case RNDIS_OID_802_3_XMIT_ONE_COLLISION
:
467 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__
);
468 *outbuf
= cpu_to_le32(0);
473 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS
:
474 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__
);
475 *outbuf
= cpu_to_le32(0);
480 pr_warning("%s: query unknown OID 0x%08X\n",
486 resp
->InformationBufferLength
= cpu_to_le32(length
);
487 r
->length
= length
+ sizeof(*resp
);
488 resp
->MessageLength
= cpu_to_le32(r
->length
);
492 static int gen_ndis_set_resp(u8 configNr
, u32 OID
, u8
*buf
, u32 buf_len
,
495 rndis_set_cmplt_type
*resp
;
496 int i
, retval
= -ENOTSUPP
;
497 struct rndis_params
*params
;
501 resp
= (rndis_set_cmplt_type
*)r
->buf
;
505 if (buf_len
&& rndis_debug
> 1) {
506 pr_debug("set OID %08x value, len %d:\n", OID
, buf_len
);
507 for (i
= 0; i
< buf_len
; i
+= 16) {
508 pr_debug("%03d: %08x %08x %08x %08x\n", i
,
509 get_unaligned_le32(&buf
[i
]),
510 get_unaligned_le32(&buf
[i
+ 4]),
511 get_unaligned_le32(&buf
[i
+ 8]),
512 get_unaligned_le32(&buf
[i
+ 12]));
516 params
= &rndis_per_dev_params
[configNr
];
518 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER
:
520 /* these NDIS_PACKET_TYPE_* bitflags are shared with
521 * cdc_filter; it's not RNDIS-specific
522 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
523 * PROMISCUOUS, DIRECTED,
524 * MULTICAST, ALL_MULTICAST, BROADCAST
526 *params
->filter
= (u16
)get_unaligned_le32(buf
);
527 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
528 __func__
, *params
->filter
);
530 /* this call has a significant side effect: it's
531 * what makes the packet flow start and stop, like
532 * activating the CDC Ethernet altsetting.
535 if (*params
->filter
) {
536 params
->state
= RNDIS_DATA_INITIALIZED
;
537 netif_carrier_on(params
->dev
);
538 if (netif_running(params
->dev
))
539 netif_wake_queue(params
->dev
);
541 params
->state
= RNDIS_INITIALIZED
;
542 netif_carrier_off(params
->dev
);
543 netif_stop_queue(params
->dev
);
547 case RNDIS_OID_802_3_MULTICAST_LIST
:
548 /* I think we can ignore this */
549 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__
);
554 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
555 __func__
, OID
, buf_len
);
565 static int rndis_init_response(int configNr
, rndis_init_msg_type
*buf
)
567 rndis_init_cmplt_type
*resp
;
569 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
574 r
= rndis_add_response(configNr
, sizeof(rndis_init_cmplt_type
));
577 resp
= (rndis_init_cmplt_type
*)r
->buf
;
579 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_INIT_C
);
580 resp
->MessageLength
= cpu_to_le32(52);
581 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
582 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
583 resp
->MajorVersion
= cpu_to_le32(RNDIS_MAJOR_VERSION
);
584 resp
->MinorVersion
= cpu_to_le32(RNDIS_MINOR_VERSION
);
585 resp
->DeviceFlags
= cpu_to_le32(RNDIS_DF_CONNECTIONLESS
);
586 resp
->Medium
= cpu_to_le32(RNDIS_MEDIUM_802_3
);
587 resp
->MaxPacketsPerTransfer
= cpu_to_le32(1);
588 resp
->MaxTransferSize
= cpu_to_le32(
590 + sizeof(struct ethhdr
)
591 + sizeof(struct rndis_packet_msg_type
)
593 resp
->PacketAlignmentFactor
= cpu_to_le32(0);
594 resp
->AFListOffset
= cpu_to_le32(0);
595 resp
->AFListSize
= cpu_to_le32(0);
597 params
->resp_avail(params
->v
);
601 static int rndis_query_response(int configNr
, rndis_query_msg_type
*buf
)
603 rndis_query_cmplt_type
*resp
;
605 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
607 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
612 * we need more memory:
613 * gen_ndis_query_resp expects enough space for
614 * rndis_query_cmplt_type followed by data.
615 * oid_supported_list is the largest data reply
617 r
= rndis_add_response(configNr
,
618 sizeof(oid_supported_list
) + sizeof(rndis_query_cmplt_type
));
621 resp
= (rndis_query_cmplt_type
*)r
->buf
;
623 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_QUERY_C
);
624 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
626 if (gen_ndis_query_resp(configNr
, le32_to_cpu(buf
->OID
),
627 le32_to_cpu(buf
->InformationBufferOffset
)
629 le32_to_cpu(buf
->InformationBufferLength
),
631 /* OID not supported */
632 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
633 resp
->MessageLength
= cpu_to_le32(sizeof *resp
);
634 resp
->InformationBufferLength
= cpu_to_le32(0);
635 resp
->InformationBufferOffset
= cpu_to_le32(0);
637 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
639 params
->resp_avail(params
->v
);
643 static int rndis_set_response(int configNr
, rndis_set_msg_type
*buf
)
645 u32 BufLength
, BufOffset
;
646 rndis_set_cmplt_type
*resp
;
648 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
650 r
= rndis_add_response(configNr
, sizeof(rndis_set_cmplt_type
));
653 resp
= (rndis_set_cmplt_type
*)r
->buf
;
655 BufLength
= le32_to_cpu(buf
->InformationBufferLength
);
656 BufOffset
= le32_to_cpu(buf
->InformationBufferOffset
);
659 pr_debug("%s: Length: %d\n", __func__
, BufLength
);
660 pr_debug("%s: Offset: %d\n", __func__
, BufOffset
);
661 pr_debug("%s: InfoBuffer: ", __func__
);
663 for (i
= 0; i
< BufLength
; i
++) {
664 pr_debug("%02x ", *(((u8
*) buf
) + i
+ 8 + BufOffset
));
670 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_SET_C
);
671 resp
->MessageLength
= cpu_to_le32(16);
672 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
673 if (gen_ndis_set_resp(configNr
, le32_to_cpu(buf
->OID
),
674 ((u8
*)buf
) + 8 + BufOffset
, BufLength
, r
))
675 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
677 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
679 params
->resp_avail(params
->v
);
683 static int rndis_reset_response(int configNr
, rndis_reset_msg_type
*buf
)
685 rndis_reset_cmplt_type
*resp
;
687 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
689 r
= rndis_add_response(configNr
, sizeof(rndis_reset_cmplt_type
));
692 resp
= (rndis_reset_cmplt_type
*)r
->buf
;
694 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_RESET_C
);
695 resp
->MessageLength
= cpu_to_le32(16);
696 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
697 /* resent information */
698 resp
->AddressingReset
= cpu_to_le32(1);
700 params
->resp_avail(params
->v
);
704 static int rndis_keepalive_response(int configNr
,
705 rndis_keepalive_msg_type
*buf
)
707 rndis_keepalive_cmplt_type
*resp
;
709 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
711 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
713 r
= rndis_add_response(configNr
, sizeof(rndis_keepalive_cmplt_type
));
716 resp
= (rndis_keepalive_cmplt_type
*)r
->buf
;
718 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_KEEPALIVE_C
);
719 resp
->MessageLength
= cpu_to_le32(16);
720 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
721 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
723 params
->resp_avail(params
->v
);
729 * Device to Host Comunication
731 static int rndis_indicate_status_msg(int configNr
, u32 status
)
733 rndis_indicate_status_msg_type
*resp
;
735 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
737 if (params
->state
== RNDIS_UNINITIALIZED
)
740 r
= rndis_add_response(configNr
,
741 sizeof(rndis_indicate_status_msg_type
));
744 resp
= (rndis_indicate_status_msg_type
*)r
->buf
;
746 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_INDICATE
);
747 resp
->MessageLength
= cpu_to_le32(20);
748 resp
->Status
= cpu_to_le32(status
);
749 resp
->StatusBufferLength
= cpu_to_le32(0);
750 resp
->StatusBufferOffset
= cpu_to_le32(0);
752 params
->resp_avail(params
->v
);
756 int rndis_signal_connect(int configNr
)
758 rndis_per_dev_params
[configNr
].media_state
759 = RNDIS_MEDIA_STATE_CONNECTED
;
760 return rndis_indicate_status_msg(configNr
,
761 RNDIS_STATUS_MEDIA_CONNECT
);
763 EXPORT_SYMBOL(rndis_signal_connect
);
765 int rndis_signal_disconnect(int configNr
)
767 rndis_per_dev_params
[configNr
].media_state
768 = RNDIS_MEDIA_STATE_DISCONNECTED
;
769 return rndis_indicate_status_msg(configNr
,
770 RNDIS_STATUS_MEDIA_DISCONNECT
);
772 EXPORT_SYMBOL(rndis_signal_disconnect
);
774 void rndis_uninit(int configNr
)
779 if (configNr
>= RNDIS_MAX_CONFIGS
)
781 rndis_per_dev_params
[configNr
].state
= RNDIS_UNINITIALIZED
;
783 /* drain the response queue */
784 while ((buf
= rndis_get_next_response(configNr
, &length
)))
785 rndis_free_response(configNr
, buf
);
787 EXPORT_SYMBOL(rndis_uninit
);
789 void rndis_set_host_mac(int configNr
, const u8
*addr
)
791 rndis_per_dev_params
[configNr
].host_mac
= addr
;
793 EXPORT_SYMBOL(rndis_set_host_mac
);
798 int rndis_msg_parser(u8 configNr
, u8
*buf
)
800 u32 MsgType
, MsgLength
;
802 struct rndis_params
*params
;
808 MsgType
= get_unaligned_le32(tmp
++);
809 MsgLength
= get_unaligned_le32(tmp
++);
811 if (configNr
>= RNDIS_MAX_CONFIGS
)
813 params
= &rndis_per_dev_params
[configNr
];
815 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
816 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
817 * and normal HC level polling to see if there's any IN traffic.
820 /* For USB: responses may take up to 10 seconds */
823 pr_debug("%s: RNDIS_MSG_INIT\n",
825 params
->state
= RNDIS_INITIALIZED
;
826 return rndis_init_response(configNr
,
827 (rndis_init_msg_type
*)buf
);
830 pr_debug("%s: RNDIS_MSG_HALT\n",
832 params
->state
= RNDIS_UNINITIALIZED
;
834 netif_carrier_off(params
->dev
);
835 netif_stop_queue(params
->dev
);
839 case RNDIS_MSG_QUERY
:
840 return rndis_query_response(configNr
,
841 (rndis_query_msg_type
*)buf
);
844 return rndis_set_response(configNr
,
845 (rndis_set_msg_type
*)buf
);
847 case RNDIS_MSG_RESET
:
848 pr_debug("%s: RNDIS_MSG_RESET\n",
850 return rndis_reset_response(configNr
,
851 (rndis_reset_msg_type
*)buf
);
853 case RNDIS_MSG_KEEPALIVE
:
854 /* For USB: host does this every 5 seconds */
856 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
858 return rndis_keepalive_response(configNr
,
859 (rndis_keepalive_msg_type
*)
863 /* At least Windows XP emits some undefined RNDIS messages.
864 * In one case those messages seemed to relate to the host
867 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
868 __func__
, MsgType
, MsgLength
);
869 print_hex_dump_bytes(__func__
, DUMP_PREFIX_OFFSET
,
876 EXPORT_SYMBOL(rndis_msg_parser
);
878 int rndis_register(void (*resp_avail
)(void *v
), void *v
)
885 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
886 if (!rndis_per_dev_params
[i
].used
) {
887 rndis_per_dev_params
[i
].used
= 1;
888 rndis_per_dev_params
[i
].resp_avail
= resp_avail
;
889 rndis_per_dev_params
[i
].v
= v
;
890 pr_debug("%s: configNr = %d\n", __func__
, i
);
894 pr_debug("failed\n");
898 EXPORT_SYMBOL(rndis_register
);
900 void rndis_deregister(int configNr
)
902 pr_debug("%s:\n", __func__
);
904 if (configNr
>= RNDIS_MAX_CONFIGS
) return;
905 rndis_per_dev_params
[configNr
].used
= 0;
907 EXPORT_SYMBOL(rndis_deregister
);
909 int rndis_set_param_dev(u8 configNr
, struct net_device
*dev
, u16
*cdc_filter
)
911 pr_debug("%s:\n", __func__
);
914 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
916 rndis_per_dev_params
[configNr
].dev
= dev
;
917 rndis_per_dev_params
[configNr
].filter
= cdc_filter
;
921 EXPORT_SYMBOL(rndis_set_param_dev
);
923 int rndis_set_param_vendor(u8 configNr
, u32 vendorID
, const char *vendorDescr
)
925 pr_debug("%s:\n", __func__
);
926 if (!vendorDescr
) return -1;
927 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
929 rndis_per_dev_params
[configNr
].vendorID
= vendorID
;
930 rndis_per_dev_params
[configNr
].vendorDescr
= vendorDescr
;
934 EXPORT_SYMBOL(rndis_set_param_vendor
);
936 int rndis_set_param_medium(u8 configNr
, u32 medium
, u32 speed
)
938 pr_debug("%s: %u %u\n", __func__
, medium
, speed
);
939 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
941 rndis_per_dev_params
[configNr
].medium
= medium
;
942 rndis_per_dev_params
[configNr
].speed
= speed
;
946 EXPORT_SYMBOL(rndis_set_param_medium
);
948 void rndis_add_hdr(struct sk_buff
*skb
)
950 struct rndis_packet_msg_type
*header
;
954 header
= (void *)skb_push(skb
, sizeof(*header
));
955 memset(header
, 0, sizeof *header
);
956 header
->MessageType
= cpu_to_le32(RNDIS_MSG_PACKET
);
957 header
->MessageLength
= cpu_to_le32(skb
->len
);
958 header
->DataOffset
= cpu_to_le32(36);
959 header
->DataLength
= cpu_to_le32(skb
->len
- sizeof(*header
));
961 EXPORT_SYMBOL(rndis_add_hdr
);
963 void rndis_free_response(int configNr
, u8
*buf
)
966 struct list_head
*act
, *tmp
;
968 list_for_each_safe(act
, tmp
,
969 &(rndis_per_dev_params
[configNr
].resp_queue
))
971 r
= list_entry(act
, rndis_resp_t
, list
);
972 if (r
&& r
->buf
== buf
) {
978 EXPORT_SYMBOL(rndis_free_response
);
980 u8
*rndis_get_next_response(int configNr
, u32
*length
)
983 struct list_head
*act
, *tmp
;
985 if (!length
) return NULL
;
987 list_for_each_safe(act
, tmp
,
988 &(rndis_per_dev_params
[configNr
].resp_queue
))
990 r
= list_entry(act
, rndis_resp_t
, list
);
1000 EXPORT_SYMBOL(rndis_get_next_response
);
1002 static rndis_resp_t
*rndis_add_response(int configNr
, u32 length
)
1006 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1007 r
= kmalloc(sizeof(rndis_resp_t
) + length
, GFP_ATOMIC
);
1008 if (!r
) return NULL
;
1010 r
->buf
= (u8
*)(r
+ 1);
1014 list_add_tail(&r
->list
,
1015 &(rndis_per_dev_params
[configNr
].resp_queue
));
1019 int rndis_rm_hdr(struct gether
*port
,
1020 struct sk_buff
*skb
,
1021 struct sk_buff_head
*list
)
1023 /* tmp points to a struct rndis_packet_msg_type */
1024 __le32
*tmp
= (void *)skb
->data
;
1026 /* MessageType, MessageLength */
1027 if (cpu_to_le32(RNDIS_MSG_PACKET
)
1028 != get_unaligned(tmp
++)) {
1029 dev_kfree_skb_any(skb
);
1034 /* DataOffset, DataLength */
1035 if (!skb_pull(skb
, get_unaligned_le32(tmp
++) + 8)) {
1036 dev_kfree_skb_any(skb
);
1039 skb_trim(skb
, get_unaligned_le32(tmp
++));
1041 skb_queue_tail(list
, skb
);
1044 EXPORT_SYMBOL(rndis_rm_hdr
);
1046 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1048 static int rndis_proc_show(struct seq_file
*m
, void *v
)
1050 rndis_params
*param
= m
->private;
1059 "vendor ID : 0x%08X\n"
1061 param
->confignr
, (param
->used
) ? "y" : "n",
1063 switch (param
->state
) {
1064 case RNDIS_UNINITIALIZED
:
1065 s
= "RNDIS_UNINITIALIZED"; break;
1066 case RNDIS_INITIALIZED
:
1067 s
= "RNDIS_INITIALIZED"; break;
1068 case RNDIS_DATA_INITIALIZED
:
1069 s
= "RNDIS_DATA_INITIALIZED"; break;
1072 (param
->media_state
) ? 0 : param
->speed
*100,
1073 (param
->media_state
) ? "disconnected" : "connected",
1074 param
->vendorID
, param
->vendorDescr
);
1078 static ssize_t
rndis_proc_write(struct file
*file
, const char __user
*buffer
,
1079 size_t count
, loff_t
*ppos
)
1081 rndis_params
*p
= PDE_DATA(file_inode(file
));
1083 int i
, fl_speed
= 0;
1085 for (i
= 0; i
< count
; i
++) {
1087 if (get_user(c
, buffer
))
1101 speed
= speed
* 10 + c
- '0';
1105 rndis_signal_connect(p
->confignr
);
1109 rndis_signal_disconnect(p
->confignr
);
1112 if (fl_speed
) p
->speed
= speed
;
1113 else pr_debug("%c is not valid\n", c
);
1123 static int rndis_proc_open(struct inode
*inode
, struct file
*file
)
1125 return single_open(file
, rndis_proc_show
, PDE_DATA(inode
));
1128 static const struct file_operations rndis_proc_fops
= {
1129 .owner
= THIS_MODULE
,
1130 .open
= rndis_proc_open
,
1132 .llseek
= seq_lseek
,
1133 .release
= single_release
,
1134 .write
= rndis_proc_write
,
1137 #define NAME_TEMPLATE "driver/rndis-%03d"
1139 static struct proc_dir_entry
*rndis_connect_state
[RNDIS_MAX_CONFIGS
];
1141 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1144 int rndis_init(void)
1148 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1149 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1152 sprintf(name
, NAME_TEMPLATE
, i
);
1153 rndis_connect_state
[i
] = proc_create_data(name
, 0660, NULL
,
1155 (void *)(rndis_per_dev_params
+ i
));
1156 if (!rndis_connect_state
[i
]) {
1157 pr_debug("%s: remove entries", __func__
);
1159 sprintf(name
, NAME_TEMPLATE
, --i
);
1160 remove_proc_entry(name
, NULL
);
1166 rndis_per_dev_params
[i
].confignr
= i
;
1167 rndis_per_dev_params
[i
].used
= 0;
1168 rndis_per_dev_params
[i
].state
= RNDIS_UNINITIALIZED
;
1169 rndis_per_dev_params
[i
].media_state
1170 = RNDIS_MEDIA_STATE_DISCONNECTED
;
1171 INIT_LIST_HEAD(&(rndis_per_dev_params
[i
].resp_queue
));
1177 void rndis_exit(void)
1179 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1183 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1184 sprintf(name
, NAME_TEMPLATE
, i
);
1185 remove_proc_entry(name
, NULL
);