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/system.h>
38 #include <asm/unaligned.h>
46 /* The driver for your USB chip needs to support ep0 OUT to work with
47 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
49 * Windows hosts need an INF file like Documentation/usb/linux.inf
50 * and will be happier if you provide the host_addr module parameter.
54 static int rndis_debug
= 0;
55 module_param (rndis_debug
, int, 0);
56 MODULE_PARM_DESC (rndis_debug
, "enable debugging");
61 #define RNDIS_MAX_CONFIGS 1
64 static rndis_params rndis_per_dev_params
[RNDIS_MAX_CONFIGS
];
67 static const __le32 rndis_driver_version
= cpu_to_le32 (1);
69 /* Function Prototypes */
70 static rndis_resp_t
*rndis_add_response (int configNr
, u32 length
);
74 static const u32 oid_supported_list
[] =
76 /* the general stuff */
77 OID_GEN_SUPPORTED_LIST
,
78 OID_GEN_HARDWARE_STATUS
,
79 OID_GEN_MEDIA_SUPPORTED
,
81 OID_GEN_MAXIMUM_FRAME_SIZE
,
83 OID_GEN_TRANSMIT_BLOCK_SIZE
,
84 OID_GEN_RECEIVE_BLOCK_SIZE
,
86 OID_GEN_VENDOR_DESCRIPTION
,
87 OID_GEN_VENDOR_DRIVER_VERSION
,
88 OID_GEN_CURRENT_PACKET_FILTER
,
89 OID_GEN_MAXIMUM_TOTAL_SIZE
,
90 OID_GEN_MEDIA_CONNECT_STATUS
,
91 OID_GEN_PHYSICAL_MEDIUM
,
93 /* the statistical stuff */
98 OID_GEN_RCV_NO_BUFFER
,
99 #ifdef RNDIS_OPTIONAL_STATS
100 OID_GEN_DIRECTED_BYTES_XMIT
,
101 OID_GEN_DIRECTED_FRAMES_XMIT
,
102 OID_GEN_MULTICAST_BYTES_XMIT
,
103 OID_GEN_MULTICAST_FRAMES_XMIT
,
104 OID_GEN_BROADCAST_BYTES_XMIT
,
105 OID_GEN_BROADCAST_FRAMES_XMIT
,
106 OID_GEN_DIRECTED_BYTES_RCV
,
107 OID_GEN_DIRECTED_FRAMES_RCV
,
108 OID_GEN_MULTICAST_BYTES_RCV
,
109 OID_GEN_MULTICAST_FRAMES_RCV
,
110 OID_GEN_BROADCAST_BYTES_RCV
,
111 OID_GEN_BROADCAST_FRAMES_RCV
,
112 OID_GEN_RCV_CRC_ERROR
,
113 OID_GEN_TRANSMIT_QUEUE_LENGTH
,
114 #endif /* RNDIS_OPTIONAL_STATS */
116 /* mandatory 802.3 */
117 /* the general stuff */
118 OID_802_3_PERMANENT_ADDRESS
,
119 OID_802_3_CURRENT_ADDRESS
,
120 OID_802_3_MULTICAST_LIST
,
121 OID_802_3_MAC_OPTIONS
,
122 OID_802_3_MAXIMUM_LIST_SIZE
,
124 /* the statistical stuff */
125 OID_802_3_RCV_ERROR_ALIGNMENT
,
126 OID_802_3_XMIT_ONE_COLLISION
,
127 OID_802_3_XMIT_MORE_COLLISIONS
,
128 #ifdef RNDIS_OPTIONAL_STATS
129 OID_802_3_XMIT_DEFERRED
,
130 OID_802_3_XMIT_MAX_COLLISIONS
,
131 OID_802_3_RCV_OVERRUN
,
132 OID_802_3_XMIT_UNDERRUN
,
133 OID_802_3_XMIT_HEARTBEAT_FAILURE
,
134 OID_802_3_XMIT_TIMES_CRS_LOST
,
135 OID_802_3_XMIT_LATE_COLLISIONS
,
136 #endif /* RNDIS_OPTIONAL_STATS */
139 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
140 * don't say what they mean ... and the NDIS specs are often
141 * confusing and/or ambiguous in this context. (That is, more
142 * so than their specs for the other OIDs.)
144 * FIXME someone who knows what these should do, please
148 /* power management */
149 OID_PNP_CAPABILITIES
,
155 OID_PNP_ENABLE_WAKE_UP
,
156 OID_PNP_ADD_WAKE_UP_PATTERN
,
157 OID_PNP_REMOVE_WAKE_UP_PATTERN
,
158 #endif /* RNDIS_WAKEUP */
159 #endif /* RNDIS_PM */
165 gen_ndis_query_resp (int configNr
, u32 OID
, u8
*buf
, unsigned buf_len
,
168 int retval
= -ENOTSUPP
;
169 u32 length
= 4; /* usually */
172 rndis_query_cmplt_type
*resp
;
173 struct net_device
*net
;
174 const struct net_device_stats
*stats
;
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 pr_debug("query OID %08x value, len %d:\n", OID
, buf_len
);
183 for (i
= 0; i
< buf_len
; i
+= 16) {
184 pr_debug("%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
= cpu_to_le32 (16);
196 net
= rndis_per_dev_params
[configNr
].dev
;
197 stats
= dev_get_stats(net
);
201 /* general oids (table 4-1) */
204 case OID_GEN_SUPPORTED_LIST
:
205 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__
);
206 length
= sizeof (oid_supported_list
);
207 count
= length
/ sizeof (u32
);
208 for (i
= 0; i
< count
; i
++)
209 outbuf
[i
] = cpu_to_le32 (oid_supported_list
[i
]);
214 case OID_GEN_HARDWARE_STATUS
:
215 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__
);
217 * Hardware must be ready to receive high level protocols.
219 * reddite ergo quae sunt Caesaris Caesari
220 * et quae sunt Dei Deo!
222 *outbuf
= cpu_to_le32 (0);
227 case OID_GEN_MEDIA_SUPPORTED
:
228 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__
);
229 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
].medium
);
234 case OID_GEN_MEDIA_IN_USE
:
235 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__
);
236 /* one medium, one transport... (maybe you do it better) */
237 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
].medium
);
242 case OID_GEN_MAXIMUM_FRAME_SIZE
:
243 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__
);
244 if (rndis_per_dev_params
[configNr
].dev
) {
245 *outbuf
= cpu_to_le32 (
246 rndis_per_dev_params
[configNr
].dev
->mtu
);
252 case OID_GEN_LINK_SPEED
:
254 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__
);
255 if (rndis_per_dev_params
[configNr
].media_state
256 == NDIS_MEDIA_STATE_DISCONNECTED
)
257 *outbuf
= cpu_to_le32 (0);
259 *outbuf
= cpu_to_le32 (
260 rndis_per_dev_params
[configNr
].speed
);
265 case OID_GEN_TRANSMIT_BLOCK_SIZE
:
266 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__
);
267 if (rndis_per_dev_params
[configNr
].dev
) {
268 *outbuf
= cpu_to_le32 (
269 rndis_per_dev_params
[configNr
].dev
->mtu
);
275 case OID_GEN_RECEIVE_BLOCK_SIZE
:
276 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__
);
277 if (rndis_per_dev_params
[configNr
].dev
) {
278 *outbuf
= cpu_to_le32 (
279 rndis_per_dev_params
[configNr
].dev
->mtu
);
285 case OID_GEN_VENDOR_ID
:
286 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__
);
287 *outbuf
= cpu_to_le32 (
288 rndis_per_dev_params
[configNr
].vendorID
);
293 case OID_GEN_VENDOR_DESCRIPTION
:
294 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__
);
295 length
= strlen (rndis_per_dev_params
[configNr
].vendorDescr
);
297 rndis_per_dev_params
[configNr
].vendorDescr
, length
);
301 case OID_GEN_VENDOR_DRIVER_VERSION
:
302 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__
);
304 *outbuf
= rndis_driver_version
;
309 case OID_GEN_CURRENT_PACKET_FILTER
:
310 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__
);
311 *outbuf
= cpu_to_le32 (*rndis_per_dev_params
[configNr
].filter
);
316 case OID_GEN_MAXIMUM_TOTAL_SIZE
:
317 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__
);
318 *outbuf
= cpu_to_le32(RNDIS_MAX_TOTAL_SIZE
);
323 case OID_GEN_MEDIA_CONNECT_STATUS
:
325 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__
);
326 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
331 case OID_GEN_PHYSICAL_MEDIUM
:
332 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__
);
333 *outbuf
= cpu_to_le32 (0);
337 /* The RNDIS specification is incomplete/wrong. Some versions
338 * of MS-Windows expect OIDs that aren't specified there. Other
339 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
341 case OID_GEN_MAC_OPTIONS
: /* from WinME */
342 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__
);
343 *outbuf
= cpu_to_le32(
344 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
345 | NDIS_MAC_OPTION_FULL_DUPLEX
);
349 /* statistics OIDs (table 4-2) */
352 case OID_GEN_XMIT_OK
:
354 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__
);
356 *outbuf
= cpu_to_le32(stats
->tx_packets
357 - stats
->tx_errors
- stats
->tx_dropped
);
365 pr_debug("%s: OID_GEN_RCV_OK\n", __func__
);
367 *outbuf
= cpu_to_le32(stats
->rx_packets
368 - stats
->rx_errors
- stats
->rx_dropped
);
374 case OID_GEN_XMIT_ERROR
:
376 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__
);
378 *outbuf
= cpu_to_le32(stats
->tx_errors
);
384 case OID_GEN_RCV_ERROR
:
386 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__
);
388 *outbuf
= cpu_to_le32(stats
->rx_errors
);
394 case OID_GEN_RCV_NO_BUFFER
:
395 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__
);
397 *outbuf
= cpu_to_le32(stats
->rx_dropped
);
402 /* ieee802.3 OIDs (table 4-3) */
405 case OID_802_3_PERMANENT_ADDRESS
:
406 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__
);
407 if (rndis_per_dev_params
[configNr
].dev
) {
410 rndis_per_dev_params
[configNr
].host_mac
,
417 case OID_802_3_CURRENT_ADDRESS
:
418 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__
);
419 if (rndis_per_dev_params
[configNr
].dev
) {
422 rndis_per_dev_params
[configNr
].host_mac
,
429 case OID_802_3_MULTICAST_LIST
:
430 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__
);
431 /* Multicast base address only */
432 *outbuf
= cpu_to_le32 (0xE0000000);
437 case OID_802_3_MAXIMUM_LIST_SIZE
:
438 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__
);
439 /* Multicast base address only */
440 *outbuf
= cpu_to_le32 (1);
444 case OID_802_3_MAC_OPTIONS
:
445 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__
);
446 *outbuf
= cpu_to_le32(0);
450 /* ieee802.3 statistics OIDs (table 4-4) */
453 case OID_802_3_RCV_ERROR_ALIGNMENT
:
454 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__
);
456 *outbuf
= cpu_to_le32(stats
->rx_frame_errors
);
462 case OID_802_3_XMIT_ONE_COLLISION
:
463 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__
);
464 *outbuf
= cpu_to_le32 (0);
469 case OID_802_3_XMIT_MORE_COLLISIONS
:
470 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__
);
471 *outbuf
= cpu_to_le32 (0);
476 pr_warning("%s: query unknown OID 0x%08X\n",
482 resp
->InformationBufferLength
= cpu_to_le32 (length
);
483 r
->length
= length
+ sizeof *resp
;
484 resp
->MessageLength
= cpu_to_le32 (r
->length
);
488 static int gen_ndis_set_resp (u8 configNr
, u32 OID
, u8
*buf
, u32 buf_len
,
491 rndis_set_cmplt_type
*resp
;
492 int i
, retval
= -ENOTSUPP
;
493 struct rndis_params
*params
;
497 resp
= (rndis_set_cmplt_type
*) r
->buf
;
501 if (buf_len
&& rndis_debug
> 1) {
502 pr_debug("set OID %08x value, len %d:\n", OID
, buf_len
);
503 for (i
= 0; i
< buf_len
; i
+= 16) {
504 pr_debug("%03d: %08x %08x %08x %08x\n", i
,
505 get_unaligned_le32(&buf
[i
]),
506 get_unaligned_le32(&buf
[i
+ 4]),
507 get_unaligned_le32(&buf
[i
+ 8]),
508 get_unaligned_le32(&buf
[i
+ 12]));
512 params
= &rndis_per_dev_params
[configNr
];
514 case OID_GEN_CURRENT_PACKET_FILTER
:
516 /* these NDIS_PACKET_TYPE_* bitflags are shared with
517 * cdc_filter; it's not RNDIS-specific
518 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
519 * PROMISCUOUS, DIRECTED,
520 * MULTICAST, ALL_MULTICAST, BROADCAST
522 *params
->filter
= (u16
)get_unaligned_le32(buf
);
523 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
524 __func__
, *params
->filter
);
526 /* this call has a significant side effect: it's
527 * what makes the packet flow start and stop, like
528 * activating the CDC Ethernet altsetting.
531 if (*params
->filter
) {
532 params
->state
= RNDIS_DATA_INITIALIZED
;
533 netif_carrier_on(params
->dev
);
534 if (netif_running(params
->dev
))
535 netif_wake_queue (params
->dev
);
537 params
->state
= RNDIS_INITIALIZED
;
538 netif_carrier_off (params
->dev
);
539 netif_stop_queue (params
->dev
);
543 case OID_802_3_MULTICAST_LIST
:
544 /* I think we can ignore this */
545 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__
);
550 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
551 __func__
, OID
, buf_len
);
561 static int rndis_init_response (int configNr
, rndis_init_msg_type
*buf
)
563 rndis_init_cmplt_type
*resp
;
565 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
570 r
= rndis_add_response (configNr
, sizeof (rndis_init_cmplt_type
));
573 resp
= (rndis_init_cmplt_type
*) r
->buf
;
575 resp
->MessageType
= cpu_to_le32 (
576 REMOTE_NDIS_INITIALIZE_CMPLT
);
577 resp
->MessageLength
= cpu_to_le32 (52);
578 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
579 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
580 resp
->MajorVersion
= cpu_to_le32 (RNDIS_MAJOR_VERSION
);
581 resp
->MinorVersion
= cpu_to_le32 (RNDIS_MINOR_VERSION
);
582 resp
->DeviceFlags
= cpu_to_le32 (RNDIS_DF_CONNECTIONLESS
);
583 resp
->Medium
= cpu_to_le32 (RNDIS_MEDIUM_802_3
);
584 resp
->MaxPacketsPerTransfer
= cpu_to_le32 (1);
585 resp
->MaxTransferSize
= cpu_to_le32 (
587 + sizeof (struct ethhdr
)
588 + sizeof (struct rndis_packet_msg_type
)
590 resp
->PacketAlignmentFactor
= cpu_to_le32 (0);
591 resp
->AFListOffset
= cpu_to_le32 (0);
592 resp
->AFListSize
= cpu_to_le32 (0);
594 params
->resp_avail(params
->v
);
598 static int rndis_query_response (int configNr
, rndis_query_msg_type
*buf
)
600 rndis_query_cmplt_type
*resp
;
602 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
604 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
609 * we need more memory:
610 * gen_ndis_query_resp expects enough space for
611 * rndis_query_cmplt_type followed by data.
612 * oid_supported_list is the largest data reply
614 r
= rndis_add_response (configNr
,
615 sizeof (oid_supported_list
) + sizeof(rndis_query_cmplt_type
));
618 resp
= (rndis_query_cmplt_type
*) r
->buf
;
620 resp
->MessageType
= cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT
);
621 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
623 if (gen_ndis_query_resp (configNr
, le32_to_cpu (buf
->OID
),
624 le32_to_cpu(buf
->InformationBufferOffset
)
626 le32_to_cpu(buf
->InformationBufferLength
),
628 /* OID not supported */
629 resp
->Status
= cpu_to_le32 (
630 RNDIS_STATUS_NOT_SUPPORTED
);
631 resp
->MessageLength
= cpu_to_le32 (sizeof *resp
);
632 resp
->InformationBufferLength
= cpu_to_le32 (0);
633 resp
->InformationBufferOffset
= cpu_to_le32 (0);
635 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
637 params
->resp_avail(params
->v
);
641 static int rndis_set_response (int configNr
, rndis_set_msg_type
*buf
)
643 u32 BufLength
, BufOffset
;
644 rndis_set_cmplt_type
*resp
;
646 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
648 r
= rndis_add_response (configNr
, sizeof (rndis_set_cmplt_type
));
651 resp
= (rndis_set_cmplt_type
*) r
->buf
;
653 BufLength
= le32_to_cpu (buf
->InformationBufferLength
);
654 BufOffset
= le32_to_cpu (buf
->InformationBufferOffset
);
657 pr_debug("%s: Length: %d\n", __func__
, BufLength
);
658 pr_debug("%s: Offset: %d\n", __func__
, BufOffset
);
659 pr_debug("%s: InfoBuffer: ", __func__
);
661 for (i
= 0; i
< BufLength
; i
++) {
662 pr_debug("%02x ", *(((u8
*) buf
) + i
+ 8 + BufOffset
));
668 resp
->MessageType
= cpu_to_le32 (REMOTE_NDIS_SET_CMPLT
);
669 resp
->MessageLength
= cpu_to_le32 (16);
670 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
671 if (gen_ndis_set_resp (configNr
, le32_to_cpu (buf
->OID
),
672 ((u8
*) buf
) + 8 + BufOffset
, BufLength
, r
))
673 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED
);
675 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
677 params
->resp_avail(params
->v
);
681 static int rndis_reset_response (int configNr
, rndis_reset_msg_type
*buf
)
683 rndis_reset_cmplt_type
*resp
;
685 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
687 r
= rndis_add_response (configNr
, sizeof (rndis_reset_cmplt_type
));
690 resp
= (rndis_reset_cmplt_type
*) r
->buf
;
692 resp
->MessageType
= cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT
);
693 resp
->MessageLength
= cpu_to_le32 (16);
694 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
695 /* resent information */
696 resp
->AddressingReset
= cpu_to_le32 (1);
698 params
->resp_avail(params
->v
);
702 static int rndis_keepalive_response (int configNr
,
703 rndis_keepalive_msg_type
*buf
)
705 rndis_keepalive_cmplt_type
*resp
;
707 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
709 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
711 r
= rndis_add_response (configNr
, sizeof (rndis_keepalive_cmplt_type
));
714 resp
= (rndis_keepalive_cmplt_type
*) r
->buf
;
716 resp
->MessageType
= cpu_to_le32 (
717 REMOTE_NDIS_KEEPALIVE_CMPLT
);
718 resp
->MessageLength
= cpu_to_le32 (16);
719 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
720 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
722 params
->resp_avail(params
->v
);
728 * Device to Host Comunication
730 static int rndis_indicate_status_msg (int configNr
, u32 status
)
732 rndis_indicate_status_msg_type
*resp
;
734 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
736 if (params
->state
== RNDIS_UNINITIALIZED
)
739 r
= rndis_add_response (configNr
,
740 sizeof (rndis_indicate_status_msg_type
));
743 resp
= (rndis_indicate_status_msg_type
*) r
->buf
;
745 resp
->MessageType
= cpu_to_le32 (
746 REMOTE_NDIS_INDICATE_STATUS_MSG
);
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 = NDIS_MEDIA_STATE_CONNECTED
;
760 return rndis_indicate_status_msg (configNr
,
761 RNDIS_STATUS_MEDIA_CONNECT
);
764 int rndis_signal_disconnect (int configNr
)
766 rndis_per_dev_params
[configNr
].media_state
767 = NDIS_MEDIA_STATE_DISCONNECTED
;
768 return rndis_indicate_status_msg (configNr
,
769 RNDIS_STATUS_MEDIA_DISCONNECT
);
772 void rndis_uninit (int configNr
)
777 if (configNr
>= RNDIS_MAX_CONFIGS
)
779 rndis_per_dev_params
[configNr
].state
= RNDIS_UNINITIALIZED
;
781 /* drain the response queue */
782 while ((buf
= rndis_get_next_response(configNr
, &length
)))
783 rndis_free_response(configNr
, buf
);
786 void rndis_set_host_mac (int configNr
, const u8
*addr
)
788 rndis_per_dev_params
[configNr
].host_mac
= addr
;
794 int rndis_msg_parser (u8 configNr
, u8
*buf
)
796 u32 MsgType
, MsgLength
;
798 struct rndis_params
*params
;
803 tmp
= (__le32
*) buf
;
804 MsgType
= get_unaligned_le32(tmp
++);
805 MsgLength
= get_unaligned_le32(tmp
++);
807 if (configNr
>= RNDIS_MAX_CONFIGS
)
809 params
= &rndis_per_dev_params
[configNr
];
811 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
812 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
813 * and normal HC level polling to see if there's any IN traffic.
816 /* For USB: responses may take up to 10 seconds */
818 case REMOTE_NDIS_INITIALIZE_MSG
:
819 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
821 params
->state
= RNDIS_INITIALIZED
;
822 return rndis_init_response (configNr
,
823 (rndis_init_msg_type
*) buf
);
825 case REMOTE_NDIS_HALT_MSG
:
826 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
828 params
->state
= RNDIS_UNINITIALIZED
;
830 netif_carrier_off (params
->dev
);
831 netif_stop_queue (params
->dev
);
835 case REMOTE_NDIS_QUERY_MSG
:
836 return rndis_query_response (configNr
,
837 (rndis_query_msg_type
*) buf
);
839 case REMOTE_NDIS_SET_MSG
:
840 return rndis_set_response (configNr
,
841 (rndis_set_msg_type
*) buf
);
843 case REMOTE_NDIS_RESET_MSG
:
844 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
846 return rndis_reset_response (configNr
,
847 (rndis_reset_msg_type
*) buf
);
849 case REMOTE_NDIS_KEEPALIVE_MSG
:
850 /* For USB: host does this every 5 seconds */
852 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
854 return rndis_keepalive_response (configNr
,
855 (rndis_keepalive_msg_type
*)
859 /* At least Windows XP emits some undefined RNDIS messages.
860 * In one case those messages seemed to relate to the host
863 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
864 __func__
, MsgType
, MsgLength
);
867 for (i
= 0; i
< MsgLength
; i
+= 16) {
869 " %02x %02x %02x %02x"
870 " %02x %02x %02x %02x"
871 " %02x %02x %02x %02x"
872 " %02x %02x %02x %02x"
880 buf
[i
+10], buf
[i
+11],
881 buf
[i
+12], buf
[i
+13],
882 buf
[i
+14], buf
[i
+15]);
891 int rndis_register(void (*resp_avail
)(void *v
), void *v
)
898 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
899 if (!rndis_per_dev_params
[i
].used
) {
900 rndis_per_dev_params
[i
].used
= 1;
901 rndis_per_dev_params
[i
].resp_avail
= resp_avail
;
902 rndis_per_dev_params
[i
].v
= v
;
903 pr_debug("%s: configNr = %d\n", __func__
, i
);
907 pr_debug("failed\n");
912 void rndis_deregister (int configNr
)
914 pr_debug("%s: \n", __func__
);
916 if (configNr
>= RNDIS_MAX_CONFIGS
) return;
917 rndis_per_dev_params
[configNr
].used
= 0;
922 int rndis_set_param_dev(u8 configNr
, struct net_device
*dev
, u16
*cdc_filter
)
924 pr_debug("%s:\n", __func__
);
927 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
929 rndis_per_dev_params
[configNr
].dev
= dev
;
930 rndis_per_dev_params
[configNr
].filter
= cdc_filter
;
935 int rndis_set_param_vendor (u8 configNr
, u32 vendorID
, const char *vendorDescr
)
937 pr_debug("%s:\n", __func__
);
938 if (!vendorDescr
) return -1;
939 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
941 rndis_per_dev_params
[configNr
].vendorID
= vendorID
;
942 rndis_per_dev_params
[configNr
].vendorDescr
= vendorDescr
;
947 int rndis_set_param_medium (u8 configNr
, u32 medium
, u32 speed
)
949 pr_debug("%s: %u %u\n", __func__
, medium
, speed
);
950 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
952 rndis_per_dev_params
[configNr
].medium
= medium
;
953 rndis_per_dev_params
[configNr
].speed
= speed
;
958 void rndis_add_hdr (struct sk_buff
*skb
)
960 struct rndis_packet_msg_type
*header
;
964 header
= (void *) skb_push (skb
, sizeof *header
);
965 memset (header
, 0, sizeof *header
);
966 header
->MessageType
= cpu_to_le32(REMOTE_NDIS_PACKET_MSG
);
967 header
->MessageLength
= cpu_to_le32(skb
->len
);
968 header
->DataOffset
= cpu_to_le32 (36);
969 header
->DataLength
= cpu_to_le32(skb
->len
- sizeof *header
);
972 void rndis_free_response (int configNr
, u8
*buf
)
975 struct list_head
*act
, *tmp
;
977 list_for_each_safe (act
, tmp
,
978 &(rndis_per_dev_params
[configNr
].resp_queue
))
980 r
= list_entry (act
, rndis_resp_t
, list
);
981 if (r
&& r
->buf
== buf
) {
988 u8
*rndis_get_next_response (int configNr
, u32
*length
)
991 struct list_head
*act
, *tmp
;
993 if (!length
) return NULL
;
995 list_for_each_safe (act
, tmp
,
996 &(rndis_per_dev_params
[configNr
].resp_queue
))
998 r
= list_entry (act
, rndis_resp_t
, list
);
1001 *length
= r
->length
;
1009 static rndis_resp_t
*rndis_add_response (int configNr
, u32 length
)
1013 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1014 r
= kmalloc (sizeof (rndis_resp_t
) + length
, GFP_ATOMIC
);
1015 if (!r
) return NULL
;
1017 r
->buf
= (u8
*) (r
+ 1);
1021 list_add_tail (&r
->list
,
1022 &(rndis_per_dev_params
[configNr
].resp_queue
));
1026 int rndis_rm_hdr(struct gether
*port
,
1027 struct sk_buff
*skb
,
1028 struct sk_buff_head
*list
)
1030 /* tmp points to a struct rndis_packet_msg_type */
1031 __le32
*tmp
= (void *) skb
->data
;
1033 /* MessageType, MessageLength */
1034 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG
)
1035 != get_unaligned(tmp
++)) {
1036 dev_kfree_skb_any(skb
);
1041 /* DataOffset, DataLength */
1042 if (!skb_pull(skb
, get_unaligned_le32(tmp
++) + 8)) {
1043 dev_kfree_skb_any(skb
);
1046 skb_trim(skb
, get_unaligned_le32(tmp
++));
1048 skb_queue_tail(list
, skb
);
1052 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1054 static int rndis_proc_show(struct seq_file
*m
, void *v
)
1056 rndis_params
*param
= m
->private;
1065 "vendor ID : 0x%08X\n"
1067 param
->confignr
, (param
->used
) ? "y" : "n",
1069 switch (param
->state
) {
1070 case RNDIS_UNINITIALIZED
:
1071 s
= "RNDIS_UNINITIALIZED"; break;
1072 case RNDIS_INITIALIZED
:
1073 s
= "RNDIS_INITIALIZED"; break;
1074 case RNDIS_DATA_INITIALIZED
:
1075 s
= "RNDIS_DATA_INITIALIZED"; break;
1078 (param
->media_state
) ? 0 : param
->speed
*100,
1079 (param
->media_state
) ? "disconnected" : "connected",
1080 param
->vendorID
, param
->vendorDescr
);
1084 static ssize_t
rndis_proc_write(struct file
*file
, const char __user
*buffer
,
1085 size_t count
, loff_t
*ppos
)
1087 rndis_params
*p
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1089 int i
, fl_speed
= 0;
1091 for (i
= 0; i
< count
; i
++) {
1093 if (get_user(c
, buffer
))
1107 speed
= speed
*10 + c
- '0';
1111 rndis_signal_connect (p
->confignr
);
1115 rndis_signal_disconnect(p
->confignr
);
1118 if (fl_speed
) p
->speed
= speed
;
1119 else pr_debug("%c is not valid\n", c
);
1129 static int rndis_proc_open(struct inode
*inode
, struct file
*file
)
1131 return single_open(file
, rndis_proc_show
, PDE(inode
)->data
);
1134 static const struct file_operations rndis_proc_fops
= {
1135 .owner
= THIS_MODULE
,
1136 .open
= rndis_proc_open
,
1138 .llseek
= seq_lseek
,
1139 .release
= single_release
,
1140 .write
= rndis_proc_write
,
1143 #define NAME_TEMPLATE "driver/rndis-%03d"
1145 static struct proc_dir_entry
*rndis_connect_state
[RNDIS_MAX_CONFIGS
];
1147 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1150 int __init
rndis_init (void)
1154 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1155 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1158 sprintf (name
, NAME_TEMPLATE
, i
);
1159 if (!(rndis_connect_state
[i
]
1160 = proc_create_data(name
, 0660, NULL
,
1162 (void *)(rndis_per_dev_params
+ i
))))
1164 pr_debug("%s :remove entries", __func__
);
1166 sprintf (name
, NAME_TEMPLATE
, --i
);
1167 remove_proc_entry (name
, NULL
);
1173 rndis_per_dev_params
[i
].confignr
= i
;
1174 rndis_per_dev_params
[i
].used
= 0;
1175 rndis_per_dev_params
[i
].state
= RNDIS_UNINITIALIZED
;
1176 rndis_per_dev_params
[i
].media_state
1177 = NDIS_MEDIA_STATE_DISCONNECTED
;
1178 INIT_LIST_HEAD (&(rndis_per_dev_params
[i
].resp_queue
));
1184 void rndis_exit (void)
1186 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1190 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1191 sprintf (name
, NAME_TEMPLATE
, i
);
1192 remove_proc_entry (name
, NULL
);