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/slab.h>
32 #include <linux/seq_file.h>
33 #include <linux/netdevice.h>
36 #include <asm/byteorder.h>
37 #include <asm/unaligned.h>
45 /* The driver for your USB chip needs to support ep0 OUT to work with
46 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
48 * Windows hosts need an INF file like Documentation/usb/linux.inf
49 * and will be happier if you provide the host_addr module parameter.
53 static int rndis_debug
= 0;
54 module_param (rndis_debug
, int, 0);
55 MODULE_PARM_DESC (rndis_debug
, "enable debugging");
60 #define RNDIS_MAX_CONFIGS 1
63 static rndis_params rndis_per_dev_params
[RNDIS_MAX_CONFIGS
];
66 static const __le32 rndis_driver_version
= cpu_to_le32(1);
68 /* Function Prototypes */
69 static rndis_resp_t
*rndis_add_response(int configNr
, u32 length
);
73 static const u32 oid_supported_list
[] =
75 /* the general stuff */
76 RNDIS_OID_GEN_SUPPORTED_LIST
,
77 RNDIS_OID_GEN_HARDWARE_STATUS
,
78 RNDIS_OID_GEN_MEDIA_SUPPORTED
,
79 RNDIS_OID_GEN_MEDIA_IN_USE
,
80 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE
,
81 RNDIS_OID_GEN_LINK_SPEED
,
82 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE
,
83 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE
,
84 RNDIS_OID_GEN_VENDOR_ID
,
85 RNDIS_OID_GEN_VENDOR_DESCRIPTION
,
86 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION
,
87 RNDIS_OID_GEN_CURRENT_PACKET_FILTER
,
88 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE
,
89 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
,
90 RNDIS_OID_GEN_PHYSICAL_MEDIUM
,
92 /* the statistical stuff */
93 RNDIS_OID_GEN_XMIT_OK
,
95 RNDIS_OID_GEN_XMIT_ERROR
,
96 RNDIS_OID_GEN_RCV_ERROR
,
97 RNDIS_OID_GEN_RCV_NO_BUFFER
,
98 #ifdef RNDIS_OPTIONAL_STATS
99 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT
,
100 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT
,
101 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT
,
102 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT
,
103 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT
,
104 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT
,
105 RNDIS_OID_GEN_DIRECTED_BYTES_RCV
,
106 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV
,
107 RNDIS_OID_GEN_MULTICAST_BYTES_RCV
,
108 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV
,
109 RNDIS_OID_GEN_BROADCAST_BYTES_RCV
,
110 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV
,
111 RNDIS_OID_GEN_RCV_CRC_ERROR
,
112 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH
,
113 #endif /* RNDIS_OPTIONAL_STATS */
115 /* mandatory 802.3 */
116 /* the general stuff */
117 RNDIS_OID_802_3_PERMANENT_ADDRESS
,
118 RNDIS_OID_802_3_CURRENT_ADDRESS
,
119 RNDIS_OID_802_3_MULTICAST_LIST
,
120 RNDIS_OID_802_3_MAC_OPTIONS
,
121 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE
,
123 /* the statistical stuff */
124 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT
,
125 RNDIS_OID_802_3_XMIT_ONE_COLLISION
,
126 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS
,
127 #ifdef RNDIS_OPTIONAL_STATS
128 RNDIS_OID_802_3_XMIT_DEFERRED
,
129 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS
,
130 RNDIS_OID_802_3_RCV_OVERRUN
,
131 RNDIS_OID_802_3_XMIT_UNDERRUN
,
132 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE
,
133 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST
,
134 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS
,
135 #endif /* RNDIS_OPTIONAL_STATS */
138 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
139 * don't say what they mean ... and the NDIS specs are often
140 * confusing and/or ambiguous in this context. (That is, more
141 * so than their specs for the other OIDs.)
143 * FIXME someone who knows what these should do, please
147 /* power management */
148 OID_PNP_CAPABILITIES
,
154 OID_PNP_ENABLE_WAKE_UP
,
155 OID_PNP_ADD_WAKE_UP_PATTERN
,
156 OID_PNP_REMOVE_WAKE_UP_PATTERN
,
157 #endif /* RNDIS_WAKEUP */
158 #endif /* RNDIS_PM */
163 static int gen_ndis_query_resp(int configNr
, u32 OID
, u8
*buf
,
164 unsigned buf_len
, rndis_resp_t
*r
)
166 int retval
= -ENOTSUPP
;
167 u32 length
= 4; /* usually */
170 rndis_query_cmplt_type
*resp
;
171 struct net_device
*net
;
172 struct rtnl_link_stats64 temp
;
173 const struct rtnl_link_stats64
*stats
;
175 if (!r
) return -ENOMEM
;
176 resp
= (rndis_query_cmplt_type
*)r
->buf
;
178 if (!resp
) return -ENOMEM
;
180 if (buf_len
&& rndis_debug
> 1) {
181 pr_debug("query OID %08x value, len %d:\n", OID
, buf_len
);
182 for (i
= 0; i
< buf_len
; i
+= 16) {
183 pr_debug("%03d: %08x %08x %08x %08x\n", i
,
184 get_unaligned_le32(&buf
[i
]),
185 get_unaligned_le32(&buf
[i
+ 4]),
186 get_unaligned_le32(&buf
[i
+ 8]),
187 get_unaligned_le32(&buf
[i
+ 12]));
191 /* response goes here, right after the header */
192 outbuf
= (__le32
*)&resp
[1];
193 resp
->InformationBufferOffset
= cpu_to_le32(16);
195 net
= rndis_per_dev_params
[configNr
].dev
;
196 stats
= dev_get_stats(net
, &temp
);
200 /* general oids (table 4-1) */
203 case RNDIS_OID_GEN_SUPPORTED_LIST
:
204 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__
);
205 length
= sizeof(oid_supported_list
);
206 count
= length
/ sizeof(u32
);
207 for (i
= 0; i
< count
; i
++)
208 outbuf
[i
] = cpu_to_le32(oid_supported_list
[i
]);
213 case RNDIS_OID_GEN_HARDWARE_STATUS
:
214 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__
);
216 * Hardware must be ready to receive high level protocols.
218 * reddite ergo quae sunt Caesaris Caesari
219 * et quae sunt Dei Deo!
221 *outbuf
= cpu_to_le32(0);
226 case RNDIS_OID_GEN_MEDIA_SUPPORTED
:
227 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__
);
228 *outbuf
= cpu_to_le32(rndis_per_dev_params
[configNr
].medium
);
233 case RNDIS_OID_GEN_MEDIA_IN_USE
:
234 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__
);
235 /* one medium, one transport... (maybe you do it better) */
236 *outbuf
= cpu_to_le32(rndis_per_dev_params
[configNr
].medium
);
241 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE
:
242 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__
);
243 if (rndis_per_dev_params
[configNr
].dev
) {
244 *outbuf
= cpu_to_le32(
245 rndis_per_dev_params
[configNr
].dev
->mtu
);
251 case RNDIS_OID_GEN_LINK_SPEED
:
253 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__
);
254 if (rndis_per_dev_params
[configNr
].media_state
255 == RNDIS_MEDIA_STATE_DISCONNECTED
)
256 *outbuf
= cpu_to_le32(0);
258 *outbuf
= cpu_to_le32(
259 rndis_per_dev_params
[configNr
].speed
);
264 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE
:
265 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__
);
266 if (rndis_per_dev_params
[configNr
].dev
) {
267 *outbuf
= cpu_to_le32(
268 rndis_per_dev_params
[configNr
].dev
->mtu
);
274 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE
:
275 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__
);
276 if (rndis_per_dev_params
[configNr
].dev
) {
277 *outbuf
= cpu_to_le32(
278 rndis_per_dev_params
[configNr
].dev
->mtu
);
284 case RNDIS_OID_GEN_VENDOR_ID
:
285 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__
);
286 *outbuf
= cpu_to_le32(
287 rndis_per_dev_params
[configNr
].vendorID
);
292 case RNDIS_OID_GEN_VENDOR_DESCRIPTION
:
293 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__
);
294 if (rndis_per_dev_params
[configNr
].vendorDescr
) {
295 length
= strlen(rndis_per_dev_params
[configNr
].
298 rndis_per_dev_params
[configNr
].vendorDescr
,
306 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION
:
307 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__
);
309 *outbuf
= rndis_driver_version
;
314 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER
:
315 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__
);
316 *outbuf
= cpu_to_le32(*rndis_per_dev_params
[configNr
].filter
);
321 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE
:
322 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__
);
323 *outbuf
= cpu_to_le32(RNDIS_MAX_TOTAL_SIZE
);
328 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
:
330 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__
);
331 *outbuf
= cpu_to_le32(rndis_per_dev_params
[configNr
]
336 case RNDIS_OID_GEN_PHYSICAL_MEDIUM
:
337 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__
);
338 *outbuf
= cpu_to_le32(0);
342 /* The RNDIS specification is incomplete/wrong. Some versions
343 * of MS-Windows expect OIDs that aren't specified there. Other
344 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
346 case RNDIS_OID_GEN_MAC_OPTIONS
: /* from WinME */
347 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__
);
348 *outbuf
= cpu_to_le32(
349 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
350 | RNDIS_MAC_OPTION_FULL_DUPLEX
);
354 /* statistics OIDs (table 4-2) */
357 case RNDIS_OID_GEN_XMIT_OK
:
359 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__
);
361 *outbuf
= cpu_to_le32(stats
->tx_packets
362 - stats
->tx_errors
- stats
->tx_dropped
);
368 case RNDIS_OID_GEN_RCV_OK
:
370 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__
);
372 *outbuf
= cpu_to_le32(stats
->rx_packets
373 - stats
->rx_errors
- stats
->rx_dropped
);
379 case RNDIS_OID_GEN_XMIT_ERROR
:
381 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__
);
383 *outbuf
= cpu_to_le32(stats
->tx_errors
);
389 case RNDIS_OID_GEN_RCV_ERROR
:
391 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__
);
393 *outbuf
= cpu_to_le32(stats
->rx_errors
);
399 case RNDIS_OID_GEN_RCV_NO_BUFFER
:
400 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__
);
402 *outbuf
= cpu_to_le32(stats
->rx_dropped
);
407 /* ieee802.3 OIDs (table 4-3) */
410 case RNDIS_OID_802_3_PERMANENT_ADDRESS
:
411 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__
);
412 if (rndis_per_dev_params
[configNr
].dev
) {
415 rndis_per_dev_params
[configNr
].host_mac
,
422 case RNDIS_OID_802_3_CURRENT_ADDRESS
:
423 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__
);
424 if (rndis_per_dev_params
[configNr
].dev
) {
427 rndis_per_dev_params
[configNr
].host_mac
,
434 case RNDIS_OID_802_3_MULTICAST_LIST
:
435 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__
);
436 /* Multicast base address only */
437 *outbuf
= cpu_to_le32(0xE0000000);
442 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE
:
443 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__
);
444 /* Multicast base address only */
445 *outbuf
= cpu_to_le32(1);
449 case RNDIS_OID_802_3_MAC_OPTIONS
:
450 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__
);
451 *outbuf
= cpu_to_le32(0);
455 /* ieee802.3 statistics OIDs (table 4-4) */
458 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT
:
459 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__
);
461 *outbuf
= cpu_to_le32(stats
->rx_frame_errors
);
467 case RNDIS_OID_802_3_XMIT_ONE_COLLISION
:
468 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__
);
469 *outbuf
= cpu_to_le32(0);
474 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS
:
475 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__
);
476 *outbuf
= cpu_to_le32(0);
481 pr_warning("%s: query unknown OID 0x%08X\n",
487 resp
->InformationBufferLength
= cpu_to_le32(length
);
488 r
->length
= length
+ sizeof(*resp
);
489 resp
->MessageLength
= cpu_to_le32(r
->length
);
493 static int gen_ndis_set_resp(u8 configNr
, u32 OID
, u8
*buf
, u32 buf_len
,
496 rndis_set_cmplt_type
*resp
;
497 int i
, retval
= -ENOTSUPP
;
498 struct rndis_params
*params
;
502 resp
= (rndis_set_cmplt_type
*)r
->buf
;
506 if (buf_len
&& rndis_debug
> 1) {
507 pr_debug("set OID %08x value, len %d:\n", OID
, buf_len
);
508 for (i
= 0; i
< buf_len
; i
+= 16) {
509 pr_debug("%03d: %08x %08x %08x %08x\n", i
,
510 get_unaligned_le32(&buf
[i
]),
511 get_unaligned_le32(&buf
[i
+ 4]),
512 get_unaligned_le32(&buf
[i
+ 8]),
513 get_unaligned_le32(&buf
[i
+ 12]));
517 params
= &rndis_per_dev_params
[configNr
];
519 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER
:
521 /* these NDIS_PACKET_TYPE_* bitflags are shared with
522 * cdc_filter; it's not RNDIS-specific
523 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
524 * PROMISCUOUS, DIRECTED,
525 * MULTICAST, ALL_MULTICAST, BROADCAST
527 *params
->filter
= (u16
)get_unaligned_le32(buf
);
528 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
529 __func__
, *params
->filter
);
531 /* this call has a significant side effect: it's
532 * what makes the packet flow start and stop, like
533 * activating the CDC Ethernet altsetting.
536 if (*params
->filter
) {
537 params
->state
= RNDIS_DATA_INITIALIZED
;
538 netif_carrier_on(params
->dev
);
539 if (netif_running(params
->dev
))
540 netif_wake_queue(params
->dev
);
542 params
->state
= RNDIS_INITIALIZED
;
543 netif_carrier_off(params
->dev
);
544 netif_stop_queue(params
->dev
);
548 case RNDIS_OID_802_3_MULTICAST_LIST
:
549 /* I think we can ignore this */
550 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__
);
555 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
556 __func__
, OID
, buf_len
);
566 static int rndis_init_response(int configNr
, rndis_init_msg_type
*buf
)
568 rndis_init_cmplt_type
*resp
;
570 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
575 r
= rndis_add_response(configNr
, sizeof(rndis_init_cmplt_type
));
578 resp
= (rndis_init_cmplt_type
*)r
->buf
;
580 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_INIT_C
);
581 resp
->MessageLength
= cpu_to_le32(52);
582 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
583 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
584 resp
->MajorVersion
= cpu_to_le32(RNDIS_MAJOR_VERSION
);
585 resp
->MinorVersion
= cpu_to_le32(RNDIS_MINOR_VERSION
);
586 resp
->DeviceFlags
= cpu_to_le32(RNDIS_DF_CONNECTIONLESS
);
587 resp
->Medium
= cpu_to_le32(RNDIS_MEDIUM_802_3
);
588 resp
->MaxPacketsPerTransfer
= cpu_to_le32(1);
589 resp
->MaxTransferSize
= cpu_to_le32(
591 + sizeof(struct ethhdr
)
592 + sizeof(struct rndis_packet_msg_type
)
594 resp
->PacketAlignmentFactor
= cpu_to_le32(0);
595 resp
->AFListOffset
= cpu_to_le32(0);
596 resp
->AFListSize
= cpu_to_le32(0);
598 params
->resp_avail(params
->v
);
602 static int rndis_query_response(int configNr
, rndis_query_msg_type
*buf
)
604 rndis_query_cmplt_type
*resp
;
606 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
608 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
613 * we need more memory:
614 * gen_ndis_query_resp expects enough space for
615 * rndis_query_cmplt_type followed by data.
616 * oid_supported_list is the largest data reply
618 r
= rndis_add_response(configNr
,
619 sizeof(oid_supported_list
) + sizeof(rndis_query_cmplt_type
));
622 resp
= (rndis_query_cmplt_type
*)r
->buf
;
624 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_QUERY_C
);
625 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
627 if (gen_ndis_query_resp(configNr
, le32_to_cpu(buf
->OID
),
628 le32_to_cpu(buf
->InformationBufferOffset
)
630 le32_to_cpu(buf
->InformationBufferLength
),
632 /* OID not supported */
633 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
634 resp
->MessageLength
= cpu_to_le32(sizeof *resp
);
635 resp
->InformationBufferLength
= cpu_to_le32(0);
636 resp
->InformationBufferOffset
= cpu_to_le32(0);
638 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
640 params
->resp_avail(params
->v
);
644 static int rndis_set_response(int configNr
, rndis_set_msg_type
*buf
)
646 u32 BufLength
, BufOffset
;
647 rndis_set_cmplt_type
*resp
;
649 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
651 r
= rndis_add_response(configNr
, sizeof(rndis_set_cmplt_type
));
654 resp
= (rndis_set_cmplt_type
*)r
->buf
;
656 BufLength
= le32_to_cpu(buf
->InformationBufferLength
);
657 BufOffset
= le32_to_cpu(buf
->InformationBufferOffset
);
660 pr_debug("%s: Length: %d\n", __func__
, BufLength
);
661 pr_debug("%s: Offset: %d\n", __func__
, BufOffset
);
662 pr_debug("%s: InfoBuffer: ", __func__
);
664 for (i
= 0; i
< BufLength
; i
++) {
665 pr_debug("%02x ", *(((u8
*) buf
) + i
+ 8 + BufOffset
));
671 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_SET_C
);
672 resp
->MessageLength
= cpu_to_le32(16);
673 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
674 if (gen_ndis_set_resp(configNr
, le32_to_cpu(buf
->OID
),
675 ((u8
*)buf
) + 8 + BufOffset
, BufLength
, r
))
676 resp
->Status
= cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED
);
678 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
680 params
->resp_avail(params
->v
);
684 static int rndis_reset_response(int configNr
, rndis_reset_msg_type
*buf
)
686 rndis_reset_cmplt_type
*resp
;
688 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
690 r
= rndis_add_response(configNr
, sizeof(rndis_reset_cmplt_type
));
693 resp
= (rndis_reset_cmplt_type
*)r
->buf
;
695 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_RESET_C
);
696 resp
->MessageLength
= cpu_to_le32(16);
697 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
698 /* resent information */
699 resp
->AddressingReset
= cpu_to_le32(1);
701 params
->resp_avail(params
->v
);
705 static int rndis_keepalive_response(int configNr
,
706 rndis_keepalive_msg_type
*buf
)
708 rndis_keepalive_cmplt_type
*resp
;
710 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
712 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
714 r
= rndis_add_response(configNr
, sizeof(rndis_keepalive_cmplt_type
));
717 resp
= (rndis_keepalive_cmplt_type
*)r
->buf
;
719 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_KEEPALIVE_C
);
720 resp
->MessageLength
= cpu_to_le32(16);
721 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
722 resp
->Status
= cpu_to_le32(RNDIS_STATUS_SUCCESS
);
724 params
->resp_avail(params
->v
);
730 * Device to Host Comunication
732 static int rndis_indicate_status_msg(int configNr
, u32 status
)
734 rndis_indicate_status_msg_type
*resp
;
736 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
738 if (params
->state
== RNDIS_UNINITIALIZED
)
741 r
= rndis_add_response(configNr
,
742 sizeof(rndis_indicate_status_msg_type
));
745 resp
= (rndis_indicate_status_msg_type
*)r
->buf
;
747 resp
->MessageType
= cpu_to_le32(RNDIS_MSG_INDICATE
);
748 resp
->MessageLength
= cpu_to_le32(20);
749 resp
->Status
= cpu_to_le32(status
);
750 resp
->StatusBufferLength
= cpu_to_le32(0);
751 resp
->StatusBufferOffset
= cpu_to_le32(0);
753 params
->resp_avail(params
->v
);
757 int rndis_signal_connect(int configNr
)
759 rndis_per_dev_params
[configNr
].media_state
760 = RNDIS_MEDIA_STATE_CONNECTED
;
761 return rndis_indicate_status_msg(configNr
,
762 RNDIS_STATUS_MEDIA_CONNECT
);
764 EXPORT_SYMBOL(rndis_signal_connect
);
766 int rndis_signal_disconnect(int configNr
)
768 rndis_per_dev_params
[configNr
].media_state
769 = RNDIS_MEDIA_STATE_DISCONNECTED
;
770 return rndis_indicate_status_msg(configNr
,
771 RNDIS_STATUS_MEDIA_DISCONNECT
);
773 EXPORT_SYMBOL(rndis_signal_disconnect
);
775 void rndis_uninit(int configNr
)
780 if (configNr
>= RNDIS_MAX_CONFIGS
)
782 rndis_per_dev_params
[configNr
].state
= RNDIS_UNINITIALIZED
;
784 /* drain the response queue */
785 while ((buf
= rndis_get_next_response(configNr
, &length
)))
786 rndis_free_response(configNr
, buf
);
788 EXPORT_SYMBOL(rndis_uninit
);
790 void rndis_set_host_mac(int configNr
, const u8
*addr
)
792 rndis_per_dev_params
[configNr
].host_mac
= addr
;
794 EXPORT_SYMBOL(rndis_set_host_mac
);
799 int rndis_msg_parser(u8 configNr
, u8
*buf
)
801 u32 MsgType
, MsgLength
;
803 struct rndis_params
*params
;
809 MsgType
= get_unaligned_le32(tmp
++);
810 MsgLength
= get_unaligned_le32(tmp
++);
812 if (configNr
>= RNDIS_MAX_CONFIGS
)
814 params
= &rndis_per_dev_params
[configNr
];
816 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
817 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
818 * and normal HC level polling to see if there's any IN traffic.
821 /* For USB: responses may take up to 10 seconds */
824 pr_debug("%s: RNDIS_MSG_INIT\n",
826 params
->state
= RNDIS_INITIALIZED
;
827 return rndis_init_response(configNr
,
828 (rndis_init_msg_type
*)buf
);
831 pr_debug("%s: RNDIS_MSG_HALT\n",
833 params
->state
= RNDIS_UNINITIALIZED
;
835 netif_carrier_off(params
->dev
);
836 netif_stop_queue(params
->dev
);
840 case RNDIS_MSG_QUERY
:
841 return rndis_query_response(configNr
,
842 (rndis_query_msg_type
*)buf
);
845 return rndis_set_response(configNr
,
846 (rndis_set_msg_type
*)buf
);
848 case RNDIS_MSG_RESET
:
849 pr_debug("%s: RNDIS_MSG_RESET\n",
851 return rndis_reset_response(configNr
,
852 (rndis_reset_msg_type
*)buf
);
854 case RNDIS_MSG_KEEPALIVE
:
855 /* For USB: host does this every 5 seconds */
857 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
859 return rndis_keepalive_response(configNr
,
860 (rndis_keepalive_msg_type
*)
864 /* At least Windows XP emits some undefined RNDIS messages.
865 * In one case those messages seemed to relate to the host
868 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
869 __func__
, MsgType
, MsgLength
);
870 print_hex_dump_bytes(__func__
, DUMP_PREFIX_OFFSET
,
877 EXPORT_SYMBOL(rndis_msg_parser
);
879 int rndis_register(void (*resp_avail
)(void *v
), void *v
)
886 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
887 if (!rndis_per_dev_params
[i
].used
) {
888 rndis_per_dev_params
[i
].used
= 1;
889 rndis_per_dev_params
[i
].resp_avail
= resp_avail
;
890 rndis_per_dev_params
[i
].v
= v
;
891 pr_debug("%s: configNr = %d\n", __func__
, i
);
895 pr_debug("failed\n");
899 EXPORT_SYMBOL(rndis_register
);
901 void rndis_deregister(int configNr
)
903 pr_debug("%s:\n", __func__
);
905 if (configNr
>= RNDIS_MAX_CONFIGS
) return;
906 rndis_per_dev_params
[configNr
].used
= 0;
908 EXPORT_SYMBOL(rndis_deregister
);
910 int rndis_set_param_dev(u8 configNr
, struct net_device
*dev
, u16
*cdc_filter
)
912 pr_debug("%s:\n", __func__
);
915 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
917 rndis_per_dev_params
[configNr
].dev
= dev
;
918 rndis_per_dev_params
[configNr
].filter
= cdc_filter
;
922 EXPORT_SYMBOL(rndis_set_param_dev
);
924 int rndis_set_param_vendor(u8 configNr
, u32 vendorID
, const char *vendorDescr
)
926 pr_debug("%s:\n", __func__
);
927 if (!vendorDescr
) return -1;
928 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
930 rndis_per_dev_params
[configNr
].vendorID
= vendorID
;
931 rndis_per_dev_params
[configNr
].vendorDescr
= vendorDescr
;
935 EXPORT_SYMBOL(rndis_set_param_vendor
);
937 int rndis_set_param_medium(u8 configNr
, u32 medium
, u32 speed
)
939 pr_debug("%s: %u %u\n", __func__
, medium
, speed
);
940 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
942 rndis_per_dev_params
[configNr
].medium
= medium
;
943 rndis_per_dev_params
[configNr
].speed
= speed
;
947 EXPORT_SYMBOL(rndis_set_param_medium
);
949 void rndis_add_hdr(struct sk_buff
*skb
)
951 struct rndis_packet_msg_type
*header
;
955 header
= (void *)skb_push(skb
, sizeof(*header
));
956 memset(header
, 0, sizeof *header
);
957 header
->MessageType
= cpu_to_le32(RNDIS_MSG_PACKET
);
958 header
->MessageLength
= cpu_to_le32(skb
->len
);
959 header
->DataOffset
= cpu_to_le32(36);
960 header
->DataLength
= cpu_to_le32(skb
->len
- sizeof(*header
));
962 EXPORT_SYMBOL(rndis_add_hdr
);
964 void rndis_free_response(int configNr
, u8
*buf
)
967 struct list_head
*act
, *tmp
;
969 list_for_each_safe(act
, tmp
,
970 &(rndis_per_dev_params
[configNr
].resp_queue
))
972 r
= list_entry(act
, rndis_resp_t
, list
);
973 if (r
&& r
->buf
== buf
) {
979 EXPORT_SYMBOL(rndis_free_response
);
981 u8
*rndis_get_next_response(int configNr
, u32
*length
)
984 struct list_head
*act
, *tmp
;
986 if (!length
) return NULL
;
988 list_for_each_safe(act
, tmp
,
989 &(rndis_per_dev_params
[configNr
].resp_queue
))
991 r
= list_entry(act
, rndis_resp_t
, list
);
1001 EXPORT_SYMBOL(rndis_get_next_response
);
1003 static rndis_resp_t
*rndis_add_response(int configNr
, u32 length
)
1007 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1008 r
= kmalloc(sizeof(rndis_resp_t
) + length
, GFP_ATOMIC
);
1009 if (!r
) return NULL
;
1011 r
->buf
= (u8
*)(r
+ 1);
1015 list_add_tail(&r
->list
,
1016 &(rndis_per_dev_params
[configNr
].resp_queue
));
1020 int rndis_rm_hdr(struct gether
*port
,
1021 struct sk_buff
*skb
,
1022 struct sk_buff_head
*list
)
1024 /* tmp points to a struct rndis_packet_msg_type */
1025 __le32
*tmp
= (void *)skb
->data
;
1027 /* MessageType, MessageLength */
1028 if (cpu_to_le32(RNDIS_MSG_PACKET
)
1029 != get_unaligned(tmp
++)) {
1030 dev_kfree_skb_any(skb
);
1035 /* DataOffset, DataLength */
1036 if (!skb_pull(skb
, get_unaligned_le32(tmp
++) + 8)) {
1037 dev_kfree_skb_any(skb
);
1040 skb_trim(skb
, get_unaligned_le32(tmp
++));
1042 skb_queue_tail(list
, skb
);
1045 EXPORT_SYMBOL(rndis_rm_hdr
);
1047 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1049 static int rndis_proc_show(struct seq_file
*m
, void *v
)
1051 rndis_params
*param
= m
->private;
1060 "vendor ID : 0x%08X\n"
1062 param
->confignr
, (param
->used
) ? "y" : "n",
1064 switch (param
->state
) {
1065 case RNDIS_UNINITIALIZED
:
1066 s
= "RNDIS_UNINITIALIZED"; break;
1067 case RNDIS_INITIALIZED
:
1068 s
= "RNDIS_INITIALIZED"; break;
1069 case RNDIS_DATA_INITIALIZED
:
1070 s
= "RNDIS_DATA_INITIALIZED"; break;
1073 (param
->media_state
) ? 0 : param
->speed
*100,
1074 (param
->media_state
) ? "disconnected" : "connected",
1075 param
->vendorID
, param
->vendorDescr
);
1079 static ssize_t
rndis_proc_write(struct file
*file
, const char __user
*buffer
,
1080 size_t count
, loff_t
*ppos
)
1082 rndis_params
*p
= PDE_DATA(file_inode(file
));
1084 int i
, fl_speed
= 0;
1086 for (i
= 0; i
< count
; i
++) {
1088 if (get_user(c
, buffer
))
1102 speed
= speed
* 10 + c
- '0';
1106 rndis_signal_connect(p
->confignr
);
1110 rndis_signal_disconnect(p
->confignr
);
1113 if (fl_speed
) p
->speed
= speed
;
1114 else pr_debug("%c is not valid\n", c
);
1124 static int rndis_proc_open(struct inode
*inode
, struct file
*file
)
1126 return single_open(file
, rndis_proc_show
, PDE_DATA(inode
));
1129 static const struct file_operations rndis_proc_fops
= {
1130 .owner
= THIS_MODULE
,
1131 .open
= rndis_proc_open
,
1133 .llseek
= seq_lseek
,
1134 .release
= single_release
,
1135 .write
= rndis_proc_write
,
1138 #define NAME_TEMPLATE "driver/rndis-%03d"
1140 static struct proc_dir_entry
*rndis_connect_state
[RNDIS_MAX_CONFIGS
];
1142 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1145 int rndis_init(void)
1149 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1150 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1153 sprintf(name
, NAME_TEMPLATE
, i
);
1154 rndis_connect_state
[i
] = proc_create_data(name
, 0660, NULL
,
1156 (void *)(rndis_per_dev_params
+ i
));
1157 if (!rndis_connect_state
[i
]) {
1158 pr_debug("%s: remove entries", __func__
);
1160 sprintf(name
, NAME_TEMPLATE
, --i
);
1161 remove_proc_entry(name
, NULL
);
1167 rndis_per_dev_params
[i
].confignr
= i
;
1168 rndis_per_dev_params
[i
].used
= 0;
1169 rndis_per_dev_params
[i
].state
= RNDIS_UNINITIALIZED
;
1170 rndis_per_dev_params
[i
].media_state
1171 = RNDIS_MEDIA_STATE_DISCONNECTED
;
1172 INIT_LIST_HEAD(&(rndis_per_dev_params
[i
].resp_queue
));
1177 module_init(rndis_init
);
1179 void rndis_exit(void)
1181 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1185 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1186 sprintf(name
, NAME_TEMPLATE
, i
);
1187 remove_proc_entry(name
, NULL
);
1191 module_exit(rndis_exit
);
1193 MODULE_LICENSE("GPL");