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>
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 OID_GEN_SUPPORTED_LIST
,
77 OID_GEN_HARDWARE_STATUS
,
78 OID_GEN_MEDIA_SUPPORTED
,
80 OID_GEN_MAXIMUM_FRAME_SIZE
,
82 OID_GEN_TRANSMIT_BLOCK_SIZE
,
83 OID_GEN_RECEIVE_BLOCK_SIZE
,
85 OID_GEN_VENDOR_DESCRIPTION
,
86 OID_GEN_VENDOR_DRIVER_VERSION
,
87 OID_GEN_CURRENT_PACKET_FILTER
,
88 OID_GEN_MAXIMUM_TOTAL_SIZE
,
89 OID_GEN_MEDIA_CONNECT_STATUS
,
90 OID_GEN_PHYSICAL_MEDIUM
,
92 /* the statistical stuff */
97 OID_GEN_RCV_NO_BUFFER
,
98 #ifdef RNDIS_OPTIONAL_STATS
99 OID_GEN_DIRECTED_BYTES_XMIT
,
100 OID_GEN_DIRECTED_FRAMES_XMIT
,
101 OID_GEN_MULTICAST_BYTES_XMIT
,
102 OID_GEN_MULTICAST_FRAMES_XMIT
,
103 OID_GEN_BROADCAST_BYTES_XMIT
,
104 OID_GEN_BROADCAST_FRAMES_XMIT
,
105 OID_GEN_DIRECTED_BYTES_RCV
,
106 OID_GEN_DIRECTED_FRAMES_RCV
,
107 OID_GEN_MULTICAST_BYTES_RCV
,
108 OID_GEN_MULTICAST_FRAMES_RCV
,
109 OID_GEN_BROADCAST_BYTES_RCV
,
110 OID_GEN_BROADCAST_FRAMES_RCV
,
111 OID_GEN_RCV_CRC_ERROR
,
112 OID_GEN_TRANSMIT_QUEUE_LENGTH
,
113 #endif /* RNDIS_OPTIONAL_STATS */
115 /* mandatory 802.3 */
116 /* the general stuff */
117 OID_802_3_PERMANENT_ADDRESS
,
118 OID_802_3_CURRENT_ADDRESS
,
119 OID_802_3_MULTICAST_LIST
,
120 OID_802_3_MAC_OPTIONS
,
121 OID_802_3_MAXIMUM_LIST_SIZE
,
123 /* the statistical stuff */
124 OID_802_3_RCV_ERROR_ALIGNMENT
,
125 OID_802_3_XMIT_ONE_COLLISION
,
126 OID_802_3_XMIT_MORE_COLLISIONS
,
127 #ifdef RNDIS_OPTIONAL_STATS
128 OID_802_3_XMIT_DEFERRED
,
129 OID_802_3_XMIT_MAX_COLLISIONS
,
130 OID_802_3_RCV_OVERRUN
,
131 OID_802_3_XMIT_UNDERRUN
,
132 OID_802_3_XMIT_HEARTBEAT_FAILURE
,
133 OID_802_3_XMIT_TIMES_CRS_LOST
,
134 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 */
164 gen_ndis_query_resp (int configNr
, u32 OID
, u8
*buf
, unsigned buf_len
,
167 int retval
= -ENOTSUPP
;
168 u32 length
= 4; /* usually */
171 rndis_query_cmplt_type
*resp
;
172 struct net_device
*net
;
173 const struct net_device_stats
*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
);
200 /* general oids (table 4-1) */
203 case OID_GEN_SUPPORTED_LIST
:
204 pr_debug("%s: 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 OID_GEN_HARDWARE_STATUS
:
214 pr_debug("%s: 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 OID_GEN_MEDIA_SUPPORTED
:
227 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__
);
228 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
].medium
);
233 case OID_GEN_MEDIA_IN_USE
:
234 pr_debug("%s: 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 OID_GEN_MAXIMUM_FRAME_SIZE
:
242 pr_debug("%s: 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 OID_GEN_LINK_SPEED
:
253 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__
);
254 if (rndis_per_dev_params
[configNr
].media_state
255 == NDIS_MEDIA_STATE_DISCONNECTED
)
256 *outbuf
= cpu_to_le32 (0);
258 *outbuf
= cpu_to_le32 (
259 rndis_per_dev_params
[configNr
].speed
);
264 case OID_GEN_TRANSMIT_BLOCK_SIZE
:
265 pr_debug("%s: 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 OID_GEN_RECEIVE_BLOCK_SIZE
:
275 pr_debug("%s: 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 OID_GEN_VENDOR_ID
:
285 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__
);
286 *outbuf
= cpu_to_le32 (
287 rndis_per_dev_params
[configNr
].vendorID
);
292 case OID_GEN_VENDOR_DESCRIPTION
:
293 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__
);
294 length
= strlen (rndis_per_dev_params
[configNr
].vendorDescr
);
296 rndis_per_dev_params
[configNr
].vendorDescr
, length
);
300 case OID_GEN_VENDOR_DRIVER_VERSION
:
301 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__
);
303 *outbuf
= rndis_driver_version
;
308 case OID_GEN_CURRENT_PACKET_FILTER
:
309 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__
);
310 *outbuf
= cpu_to_le32 (*rndis_per_dev_params
[configNr
].filter
);
315 case OID_GEN_MAXIMUM_TOTAL_SIZE
:
316 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__
);
317 *outbuf
= cpu_to_le32(RNDIS_MAX_TOTAL_SIZE
);
322 case OID_GEN_MEDIA_CONNECT_STATUS
:
324 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__
);
325 *outbuf
= cpu_to_le32 (rndis_per_dev_params
[configNr
]
330 case OID_GEN_PHYSICAL_MEDIUM
:
331 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__
);
332 *outbuf
= cpu_to_le32 (0);
336 /* The RNDIS specification is incomplete/wrong. Some versions
337 * of MS-Windows expect OIDs that aren't specified there. Other
338 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
340 case OID_GEN_MAC_OPTIONS
: /* from WinME */
341 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__
);
342 *outbuf
= cpu_to_le32(
343 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
344 | NDIS_MAC_OPTION_FULL_DUPLEX
);
348 /* statistics OIDs (table 4-2) */
351 case OID_GEN_XMIT_OK
:
353 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__
);
355 *outbuf
= cpu_to_le32(stats
->tx_packets
356 - stats
->tx_errors
- stats
->tx_dropped
);
364 pr_debug("%s: OID_GEN_RCV_OK\n", __func__
);
366 *outbuf
= cpu_to_le32(stats
->rx_packets
367 - stats
->rx_errors
- stats
->rx_dropped
);
373 case OID_GEN_XMIT_ERROR
:
375 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__
);
377 *outbuf
= cpu_to_le32(stats
->tx_errors
);
383 case OID_GEN_RCV_ERROR
:
385 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__
);
387 *outbuf
= cpu_to_le32(stats
->rx_errors
);
393 case OID_GEN_RCV_NO_BUFFER
:
394 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__
);
396 *outbuf
= cpu_to_le32(stats
->rx_dropped
);
401 /* ieee802.3 OIDs (table 4-3) */
404 case OID_802_3_PERMANENT_ADDRESS
:
405 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__
);
406 if (rndis_per_dev_params
[configNr
].dev
) {
409 rndis_per_dev_params
[configNr
].host_mac
,
416 case OID_802_3_CURRENT_ADDRESS
:
417 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__
);
418 if (rndis_per_dev_params
[configNr
].dev
) {
421 rndis_per_dev_params
[configNr
].host_mac
,
428 case OID_802_3_MULTICAST_LIST
:
429 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__
);
430 /* Multicast base address only */
431 *outbuf
= cpu_to_le32 (0xE0000000);
436 case OID_802_3_MAXIMUM_LIST_SIZE
:
437 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__
);
438 /* Multicast base address only */
439 *outbuf
= cpu_to_le32 (1);
443 case OID_802_3_MAC_OPTIONS
:
444 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__
);
447 /* ieee802.3 statistics OIDs (table 4-4) */
450 case OID_802_3_RCV_ERROR_ALIGNMENT
:
451 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__
);
453 *outbuf
= cpu_to_le32(stats
->rx_frame_errors
);
459 case OID_802_3_XMIT_ONE_COLLISION
:
460 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__
);
461 *outbuf
= cpu_to_le32 (0);
466 case OID_802_3_XMIT_MORE_COLLISIONS
:
467 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__
);
468 *outbuf
= cpu_to_le32 (0);
473 pr_warning("%s: query unknown OID 0x%08X\n",
479 resp
->InformationBufferLength
= cpu_to_le32 (length
);
480 r
->length
= length
+ sizeof *resp
;
481 resp
->MessageLength
= cpu_to_le32 (r
->length
);
485 static int gen_ndis_set_resp (u8 configNr
, u32 OID
, u8
*buf
, u32 buf_len
,
488 rndis_set_cmplt_type
*resp
;
489 int i
, retval
= -ENOTSUPP
;
490 struct rndis_params
*params
;
494 resp
= (rndis_set_cmplt_type
*) r
->buf
;
498 if (buf_len
&& rndis_debug
> 1) {
499 pr_debug("set OID %08x value, len %d:\n", OID
, buf_len
);
500 for (i
= 0; i
< buf_len
; i
+= 16) {
501 pr_debug("%03d: %08x %08x %08x %08x\n", i
,
502 get_unaligned_le32(&buf
[i
]),
503 get_unaligned_le32(&buf
[i
+ 4]),
504 get_unaligned_le32(&buf
[i
+ 8]),
505 get_unaligned_le32(&buf
[i
+ 12]));
509 params
= &rndis_per_dev_params
[configNr
];
511 case OID_GEN_CURRENT_PACKET_FILTER
:
513 /* these NDIS_PACKET_TYPE_* bitflags are shared with
514 * cdc_filter; it's not RNDIS-specific
515 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
516 * PROMISCUOUS, DIRECTED,
517 * MULTICAST, ALL_MULTICAST, BROADCAST
519 *params
->filter
= (u16
)get_unaligned_le32(buf
);
520 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
521 __func__
, *params
->filter
);
523 /* this call has a significant side effect: it's
524 * what makes the packet flow start and stop, like
525 * activating the CDC Ethernet altsetting.
528 if (*params
->filter
) {
529 params
->state
= RNDIS_DATA_INITIALIZED
;
530 netif_carrier_on(params
->dev
);
531 if (netif_running(params
->dev
))
532 netif_wake_queue (params
->dev
);
534 params
->state
= RNDIS_INITIALIZED
;
535 netif_carrier_off (params
->dev
);
536 netif_stop_queue (params
->dev
);
540 case OID_802_3_MULTICAST_LIST
:
541 /* I think we can ignore this */
542 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__
);
547 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
548 __func__
, OID
, buf_len
);
558 static int rndis_init_response (int configNr
, rndis_init_msg_type
*buf
)
560 rndis_init_cmplt_type
*resp
;
562 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
567 r
= rndis_add_response (configNr
, sizeof (rndis_init_cmplt_type
));
570 resp
= (rndis_init_cmplt_type
*) r
->buf
;
572 resp
->MessageType
= cpu_to_le32 (
573 REMOTE_NDIS_INITIALIZE_CMPLT
);
574 resp
->MessageLength
= cpu_to_le32 (52);
575 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
576 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
577 resp
->MajorVersion
= cpu_to_le32 (RNDIS_MAJOR_VERSION
);
578 resp
->MinorVersion
= cpu_to_le32 (RNDIS_MINOR_VERSION
);
579 resp
->DeviceFlags
= cpu_to_le32 (RNDIS_DF_CONNECTIONLESS
);
580 resp
->Medium
= cpu_to_le32 (RNDIS_MEDIUM_802_3
);
581 resp
->MaxPacketsPerTransfer
= cpu_to_le32 (1);
582 resp
->MaxTransferSize
= cpu_to_le32 (
584 + sizeof (struct ethhdr
)
585 + sizeof (struct rndis_packet_msg_type
)
587 resp
->PacketAlignmentFactor
= cpu_to_le32 (0);
588 resp
->AFListOffset
= cpu_to_le32 (0);
589 resp
->AFListSize
= cpu_to_le32 (0);
591 params
->resp_avail(params
->v
);
595 static int rndis_query_response (int configNr
, rndis_query_msg_type
*buf
)
597 rndis_query_cmplt_type
*resp
;
599 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
601 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
606 * we need more memory:
607 * gen_ndis_query_resp expects enough space for
608 * rndis_query_cmplt_type followed by data.
609 * oid_supported_list is the largest data reply
611 r
= rndis_add_response (configNr
,
612 sizeof (oid_supported_list
) + sizeof(rndis_query_cmplt_type
));
615 resp
= (rndis_query_cmplt_type
*) r
->buf
;
617 resp
->MessageType
= cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT
);
618 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
620 if (gen_ndis_query_resp (configNr
, le32_to_cpu (buf
->OID
),
621 le32_to_cpu(buf
->InformationBufferOffset
)
623 le32_to_cpu(buf
->InformationBufferLength
),
625 /* OID not supported */
626 resp
->Status
= cpu_to_le32 (
627 RNDIS_STATUS_NOT_SUPPORTED
);
628 resp
->MessageLength
= cpu_to_le32 (sizeof *resp
);
629 resp
->InformationBufferLength
= cpu_to_le32 (0);
630 resp
->InformationBufferOffset
= cpu_to_le32 (0);
632 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
634 params
->resp_avail(params
->v
);
638 static int rndis_set_response (int configNr
, rndis_set_msg_type
*buf
)
640 u32 BufLength
, BufOffset
;
641 rndis_set_cmplt_type
*resp
;
643 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
645 r
= rndis_add_response (configNr
, sizeof (rndis_set_cmplt_type
));
648 resp
= (rndis_set_cmplt_type
*) r
->buf
;
650 BufLength
= le32_to_cpu (buf
->InformationBufferLength
);
651 BufOffset
= le32_to_cpu (buf
->InformationBufferOffset
);
654 pr_debug("%s: Length: %d\n", __func__
, BufLength
);
655 pr_debug("%s: Offset: %d\n", __func__
, BufOffset
);
656 pr_debug("%s: InfoBuffer: ", __func__
);
658 for (i
= 0; i
< BufLength
; i
++) {
659 pr_debug("%02x ", *(((u8
*) buf
) + i
+ 8 + BufOffset
));
665 resp
->MessageType
= cpu_to_le32 (REMOTE_NDIS_SET_CMPLT
);
666 resp
->MessageLength
= cpu_to_le32 (16);
667 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
668 if (gen_ndis_set_resp (configNr
, le32_to_cpu (buf
->OID
),
669 ((u8
*) buf
) + 8 + BufOffset
, BufLength
, r
))
670 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED
);
672 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
674 params
->resp_avail(params
->v
);
678 static int rndis_reset_response (int configNr
, rndis_reset_msg_type
*buf
)
680 rndis_reset_cmplt_type
*resp
;
682 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
684 r
= rndis_add_response (configNr
, sizeof (rndis_reset_cmplt_type
));
687 resp
= (rndis_reset_cmplt_type
*) r
->buf
;
689 resp
->MessageType
= cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT
);
690 resp
->MessageLength
= cpu_to_le32 (16);
691 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
692 /* resent information */
693 resp
->AddressingReset
= cpu_to_le32 (1);
695 params
->resp_avail(params
->v
);
699 static int rndis_keepalive_response (int configNr
,
700 rndis_keepalive_msg_type
*buf
)
702 rndis_keepalive_cmplt_type
*resp
;
704 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
706 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
708 r
= rndis_add_response (configNr
, sizeof (rndis_keepalive_cmplt_type
));
711 resp
= (rndis_keepalive_cmplt_type
*) r
->buf
;
713 resp
->MessageType
= cpu_to_le32 (
714 REMOTE_NDIS_KEEPALIVE_CMPLT
);
715 resp
->MessageLength
= cpu_to_le32 (16);
716 resp
->RequestID
= buf
->RequestID
; /* Still LE in msg buffer */
717 resp
->Status
= cpu_to_le32 (RNDIS_STATUS_SUCCESS
);
719 params
->resp_avail(params
->v
);
725 * Device to Host Comunication
727 static int rndis_indicate_status_msg (int configNr
, u32 status
)
729 rndis_indicate_status_msg_type
*resp
;
731 struct rndis_params
*params
= rndis_per_dev_params
+ configNr
;
733 if (params
->state
== RNDIS_UNINITIALIZED
)
736 r
= rndis_add_response (configNr
,
737 sizeof (rndis_indicate_status_msg_type
));
740 resp
= (rndis_indicate_status_msg_type
*) r
->buf
;
742 resp
->MessageType
= cpu_to_le32 (
743 REMOTE_NDIS_INDICATE_STATUS_MSG
);
744 resp
->MessageLength
= cpu_to_le32 (20);
745 resp
->Status
= cpu_to_le32 (status
);
746 resp
->StatusBufferLength
= cpu_to_le32 (0);
747 resp
->StatusBufferOffset
= cpu_to_le32 (0);
749 params
->resp_avail(params
->v
);
753 int rndis_signal_connect (int configNr
)
755 rndis_per_dev_params
[configNr
].media_state
756 = NDIS_MEDIA_STATE_CONNECTED
;
757 return rndis_indicate_status_msg (configNr
,
758 RNDIS_STATUS_MEDIA_CONNECT
);
761 int rndis_signal_disconnect (int configNr
)
763 rndis_per_dev_params
[configNr
].media_state
764 = NDIS_MEDIA_STATE_DISCONNECTED
;
765 return rndis_indicate_status_msg (configNr
,
766 RNDIS_STATUS_MEDIA_DISCONNECT
);
769 void rndis_uninit (int configNr
)
774 if (configNr
>= RNDIS_MAX_CONFIGS
)
776 rndis_per_dev_params
[configNr
].state
= RNDIS_UNINITIALIZED
;
778 /* drain the response queue */
779 while ((buf
= rndis_get_next_response(configNr
, &length
)))
780 rndis_free_response(configNr
, buf
);
783 void rndis_set_host_mac (int configNr
, const u8
*addr
)
785 rndis_per_dev_params
[configNr
].host_mac
= addr
;
791 int rndis_msg_parser (u8 configNr
, u8
*buf
)
793 u32 MsgType
, MsgLength
;
795 struct rndis_params
*params
;
800 tmp
= (__le32
*) buf
;
801 MsgType
= get_unaligned_le32(tmp
++);
802 MsgLength
= get_unaligned_le32(tmp
++);
804 if (configNr
>= RNDIS_MAX_CONFIGS
)
806 params
= &rndis_per_dev_params
[configNr
];
808 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
809 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
810 * and normal HC level polling to see if there's any IN traffic.
813 /* For USB: responses may take up to 10 seconds */
815 case REMOTE_NDIS_INITIALIZE_MSG
:
816 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
818 params
->state
= RNDIS_INITIALIZED
;
819 return rndis_init_response (configNr
,
820 (rndis_init_msg_type
*) buf
);
822 case REMOTE_NDIS_HALT_MSG
:
823 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
825 params
->state
= RNDIS_UNINITIALIZED
;
827 netif_carrier_off (params
->dev
);
828 netif_stop_queue (params
->dev
);
832 case REMOTE_NDIS_QUERY_MSG
:
833 return rndis_query_response (configNr
,
834 (rndis_query_msg_type
*) buf
);
836 case REMOTE_NDIS_SET_MSG
:
837 return rndis_set_response (configNr
,
838 (rndis_set_msg_type
*) buf
);
840 case REMOTE_NDIS_RESET_MSG
:
841 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
843 return rndis_reset_response (configNr
,
844 (rndis_reset_msg_type
*) buf
);
846 case REMOTE_NDIS_KEEPALIVE_MSG
:
847 /* For USB: host does this every 5 seconds */
849 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
851 return rndis_keepalive_response (configNr
,
852 (rndis_keepalive_msg_type
*)
856 /* At least Windows XP emits some undefined RNDIS messages.
857 * In one case those messages seemed to relate to the host
860 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
861 __func__
, MsgType
, MsgLength
);
864 for (i
= 0; i
< MsgLength
; i
+= 16) {
866 " %02x %02x %02x %02x"
867 " %02x %02x %02x %02x"
868 " %02x %02x %02x %02x"
869 " %02x %02x %02x %02x"
877 buf
[i
+10], buf
[i
+11],
878 buf
[i
+12], buf
[i
+13],
879 buf
[i
+14], buf
[i
+15]);
888 int rndis_register(void (*resp_avail
)(void *v
), void *v
)
895 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
896 if (!rndis_per_dev_params
[i
].used
) {
897 rndis_per_dev_params
[i
].used
= 1;
898 rndis_per_dev_params
[i
].resp_avail
= resp_avail
;
899 rndis_per_dev_params
[i
].v
= v
;
900 pr_debug("%s: configNr = %d\n", __func__
, i
);
904 pr_debug("failed\n");
909 void rndis_deregister (int configNr
)
911 pr_debug("%s: \n", __func__
);
913 if (configNr
>= RNDIS_MAX_CONFIGS
) return;
914 rndis_per_dev_params
[configNr
].used
= 0;
919 int rndis_set_param_dev(u8 configNr
, struct net_device
*dev
, u16
*cdc_filter
)
921 pr_debug("%s:\n", __func__
);
924 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
926 rndis_per_dev_params
[configNr
].dev
= dev
;
927 rndis_per_dev_params
[configNr
].filter
= cdc_filter
;
932 int rndis_set_param_vendor (u8 configNr
, u32 vendorID
, const char *vendorDescr
)
934 pr_debug("%s:\n", __func__
);
935 if (!vendorDescr
) return -1;
936 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
938 rndis_per_dev_params
[configNr
].vendorID
= vendorID
;
939 rndis_per_dev_params
[configNr
].vendorDescr
= vendorDescr
;
944 int rndis_set_param_medium (u8 configNr
, u32 medium
, u32 speed
)
946 pr_debug("%s: %u %u\n", __func__
, medium
, speed
);
947 if (configNr
>= RNDIS_MAX_CONFIGS
) return -1;
949 rndis_per_dev_params
[configNr
].medium
= medium
;
950 rndis_per_dev_params
[configNr
].speed
= speed
;
955 void rndis_add_hdr (struct sk_buff
*skb
)
957 struct rndis_packet_msg_type
*header
;
961 header
= (void *) skb_push (skb
, sizeof *header
);
962 memset (header
, 0, sizeof *header
);
963 header
->MessageType
= cpu_to_le32(REMOTE_NDIS_PACKET_MSG
);
964 header
->MessageLength
= cpu_to_le32(skb
->len
);
965 header
->DataOffset
= cpu_to_le32 (36);
966 header
->DataLength
= cpu_to_le32(skb
->len
- sizeof *header
);
969 void rndis_free_response (int configNr
, u8
*buf
)
972 struct list_head
*act
, *tmp
;
974 list_for_each_safe (act
, tmp
,
975 &(rndis_per_dev_params
[configNr
].resp_queue
))
977 r
= list_entry (act
, rndis_resp_t
, list
);
978 if (r
&& r
->buf
== buf
) {
985 u8
*rndis_get_next_response (int configNr
, u32
*length
)
988 struct list_head
*act
, *tmp
;
990 if (!length
) return NULL
;
992 list_for_each_safe (act
, tmp
,
993 &(rndis_per_dev_params
[configNr
].resp_queue
))
995 r
= list_entry (act
, rndis_resp_t
, list
);
1006 static rndis_resp_t
*rndis_add_response (int configNr
, u32 length
)
1010 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1011 r
= kmalloc (sizeof (rndis_resp_t
) + length
, GFP_ATOMIC
);
1012 if (!r
) return NULL
;
1014 r
->buf
= (u8
*) (r
+ 1);
1018 list_add_tail (&r
->list
,
1019 &(rndis_per_dev_params
[configNr
].resp_queue
));
1023 int rndis_rm_hdr(struct sk_buff
*skb
)
1025 /* tmp points to a struct rndis_packet_msg_type */
1026 __le32
*tmp
= (void *) skb
->data
;
1028 /* MessageType, MessageLength */
1029 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG
)
1030 != get_unaligned(tmp
++))
1034 /* DataOffset, DataLength */
1035 if (!skb_pull(skb
, get_unaligned_le32(tmp
++) + 8))
1037 skb_trim(skb
, get_unaligned_le32(tmp
++));
1042 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1044 static int rndis_proc_show(struct seq_file
*m
, void *v
)
1046 rndis_params
*param
= m
->private;
1055 "vendor ID : 0x%08X\n"
1057 param
->confignr
, (param
->used
) ? "y" : "n",
1059 switch (param
->state
) {
1060 case RNDIS_UNINITIALIZED
:
1061 s
= "RNDIS_UNINITIALIZED"; break;
1062 case RNDIS_INITIALIZED
:
1063 s
= "RNDIS_INITIALIZED"; break;
1064 case RNDIS_DATA_INITIALIZED
:
1065 s
= "RNDIS_DATA_INITIALIZED"; break;
1068 (param
->media_state
) ? 0 : param
->speed
*100,
1069 (param
->media_state
) ? "disconnected" : "connected",
1070 param
->vendorID
, param
->vendorDescr
);
1074 static ssize_t
rndis_proc_write(struct file
*file
, const char __user
*buffer
,
1075 size_t count
, loff_t
*ppos
)
1077 rndis_params
*p
= PDE(file
->f_path
.dentry
->d_inode
)->data
;
1079 int i
, fl_speed
= 0;
1081 for (i
= 0; i
< count
; i
++) {
1083 if (get_user(c
, buffer
))
1097 speed
= speed
*10 + c
- '0';
1101 rndis_signal_connect (p
->confignr
);
1105 rndis_signal_disconnect(p
->confignr
);
1108 if (fl_speed
) p
->speed
= speed
;
1109 else pr_debug("%c is not valid\n", c
);
1119 static int rndis_proc_open(struct inode
*inode
, struct file
*file
)
1121 return single_open(file
, rndis_proc_show
, PDE(inode
)->data
);
1124 static const struct file_operations rndis_proc_fops
= {
1125 .owner
= THIS_MODULE
,
1126 .open
= rndis_proc_open
,
1128 .llseek
= seq_lseek
,
1129 .release
= single_release
,
1130 .write
= rndis_proc_write
,
1133 #define NAME_TEMPLATE "driver/rndis-%03d"
1135 static struct proc_dir_entry
*rndis_connect_state
[RNDIS_MAX_CONFIGS
];
1137 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1140 int __init
rndis_init (void)
1144 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1145 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1148 sprintf (name
, NAME_TEMPLATE
, i
);
1149 if (!(rndis_connect_state
[i
]
1150 = proc_create_data(name
, 0660, NULL
,
1152 (void *)(rndis_per_dev_params
+ i
))))
1154 pr_debug("%s :remove entries", __func__
);
1156 sprintf (name
, NAME_TEMPLATE
, --i
);
1157 remove_proc_entry (name
, NULL
);
1163 rndis_per_dev_params
[i
].confignr
= i
;
1164 rndis_per_dev_params
[i
].used
= 0;
1165 rndis_per_dev_params
[i
].state
= RNDIS_UNINITIALIZED
;
1166 rndis_per_dev_params
[i
].media_state
1167 = NDIS_MEDIA_STATE_DISCONNECTED
;
1168 INIT_LIST_HEAD (&(rndis_per_dev_params
[i
].resp_queue
));
1174 void rndis_exit (void)
1176 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1180 for (i
= 0; i
< RNDIS_MAX_CONFIGS
; i
++) {
1181 sprintf (name
, NAME_TEMPLATE
, i
);
1182 remove_proc_entry (name
, NULL
);