2 * Host Side support for RNDIS Networking Links
3 * Copyright (C) 2005 by David Brownell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // #define DEBUG // error path messages, extra info
21 // #define VERBOSE // more; success messages
23 #include <linux/config.h>
24 #ifdef CONFIG_USB_DEBUG
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/init.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/ethtool.h>
33 #include <linux/workqueue.h>
34 #include <linux/mii.h>
35 #include <linux/usb.h>
36 #include <linux/usb_cdc.h>
42 * RNDIS is NDIS remoted over USB. It's a MSFT variant of CDC ACM ... of
43 * course ACM was intended for modems, not Ethernet links! USB's standard
44 * for Ethernet links is "CDC Ethernet", which is significantly simpler.
48 * CONTROL uses CDC "encapsulated commands" with funky notifications.
49 * - control-out: SEND_ENCAPSULATED
50 * - interrupt-in: RESPONSE_AVAILABLE
51 * - control-in: GET_ENCAPSULATED
53 * We'll try to ignore the RESPONSE_AVAILABLE notifications.
55 struct rndis_msg_hdr
{
56 __le32 msg_type
; /* RNDIS_MSG_* */
58 // followed by data that varies between messages
62 } __attribute__ ((packed
));
64 /* RNDIS defines this (absurdly huge) control timeout */
65 #define RNDIS_CONTROL_TIMEOUT_MS (10 * 1000)
68 #define ccpu2 __constant_cpu_to_le32
70 #define RNDIS_MSG_COMPLETION ccpu2(0x80000000)
72 /* codes for "msg_type" field of rndis messages;
73 * only the data channel uses packet messages (maybe batched);
74 * everything else goes on the control channel.
76 #define RNDIS_MSG_PACKET ccpu2(0x00000001) /* 1-N packets */
77 #define RNDIS_MSG_INIT ccpu2(0x00000002)
78 #define RNDIS_MSG_INIT_C (RNDIS_MSG_INIT|RNDIS_MSG_COMPLETION)
79 #define RNDIS_MSG_HALT ccpu2(0x00000003)
80 #define RNDIS_MSG_QUERY ccpu2(0x00000004)
81 #define RNDIS_MSG_QUERY_C (RNDIS_MSG_QUERY|RNDIS_MSG_COMPLETION)
82 #define RNDIS_MSG_SET ccpu2(0x00000005)
83 #define RNDIS_MSG_SET_C (RNDIS_MSG_SET|RNDIS_MSG_COMPLETION)
84 #define RNDIS_MSG_RESET ccpu2(0x00000006)
85 #define RNDIS_MSG_RESET_C (RNDIS_MSG_RESET|RNDIS_MSG_COMPLETION)
86 #define RNDIS_MSG_INDICATE ccpu2(0x00000007)
87 #define RNDIS_MSG_KEEPALIVE ccpu2(0x00000008)
88 #define RNDIS_MSG_KEEPALIVE_C (RNDIS_MSG_KEEPALIVE|RNDIS_MSG_COMPLETION)
90 /* codes for "status" field of completion messages */
91 #define RNDIS_STATUS_SUCCESS ccpu2(0x00000000)
92 #define RNDIS_STATUS_FAILURE ccpu2(0xc0000001)
93 #define RNDIS_STATUS_INVALID_DATA ccpu2(0xc0010015)
94 #define RNDIS_STATUS_NOT_SUPPORTED ccpu2(0xc00000bb)
95 #define RNDIS_STATUS_MEDIA_CONNECT ccpu2(0x4001000b)
96 #define RNDIS_STATUS_MEDIA_DISCONNECT ccpu2(0x4001000c)
99 struct rndis_data_hdr
{
100 __le32 msg_type
; /* RNDIS_MSG_PACKET */
101 __le32 msg_len
; // rndis_data_hdr + data_len + pad
102 __le32 data_offset
; // 36 -- right after header
103 __le32 data_len
; // ... real packet size
105 __le32 oob_data_offset
; // zero
106 __le32 oob_data_len
; // zero
107 __le32 num_oob
; // zero
108 __le32 packet_data_offset
; // zero
110 __le32 packet_data_len
; // zero
111 __le32 vc_handle
; // zero
112 __le32 reserved
; // zero
113 } __attribute__ ((packed
));
115 struct rndis_init
{ /* OUT */
117 __le32 msg_type
; /* RNDIS_MSG_INIT */
118 __le32 msg_len
; // 24
120 __le32 major_version
; // of rndis (1.0)
121 __le32 minor_version
;
122 __le32 max_transfer_size
;
123 } __attribute__ ((packed
));
125 struct rndis_init_c
{ /* IN */
127 __le32 msg_type
; /* RNDIS_MSG_INIT_C */
131 __le32 major_version
; // of rndis (1.0)
132 __le32 minor_version
;
134 __le32 medium
; // zero == 802.3
135 __le32 max_packets_per_message
;
136 __le32 max_transfer_size
;
137 __le32 packet_alignment
; // max 7; (1<<n) bytes
138 __le32 af_list_offset
; // zero
139 __le32 af_list_size
; // zero
140 } __attribute__ ((packed
));
142 struct rndis_halt
{ /* OUT (no reply) */
144 __le32 msg_type
; /* RNDIS_MSG_HALT */
147 } __attribute__ ((packed
));
149 struct rndis_query
{ /* OUT */
151 __le32 msg_type
; /* RNDIS_MSG_QUERY */
157 /*?*/ __le32 handle
; // zero
158 } __attribute__ ((packed
));
160 struct rndis_query_c
{ /* IN */
162 __le32 msg_type
; /* RNDIS_MSG_QUERY_C */
168 } __attribute__ ((packed
));
170 struct rndis_set
{ /* OUT */
172 __le32 msg_type
; /* RNDIS_MSG_SET */
178 /*?*/ __le32 handle
; // zero
179 } __attribute__ ((packed
));
181 struct rndis_set_c
{ /* IN */
183 __le32 msg_type
; /* RNDIS_MSG_SET_C */
187 } __attribute__ ((packed
));
189 struct rndis_reset
{ /* IN */
191 __le32 msg_type
; /* RNDIS_MSG_RESET */
194 } __attribute__ ((packed
));
196 struct rndis_reset_c
{ /* OUT */
198 __le32 msg_type
; /* RNDIS_MSG_RESET_C */
201 __le32 addressing_lost
;
202 } __attribute__ ((packed
));
204 struct rndis_indicate
{ /* IN (unrequested) */
206 __le32 msg_type
; /* RNDIS_MSG_INDICATE */
211 /**/ __le32 diag_status
;
214 } __attribute__ ((packed
));
216 struct rndis_keepalive
{ /* OUT (optionally IN) */
218 __le32 msg_type
; /* RNDIS_MSG_KEEPALIVE */
221 } __attribute__ ((packed
));
223 struct rndis_keepalive_c
{ /* IN (optionally OUT) */
225 __le32 msg_type
; /* RNDIS_MSG_KEEPALIVE_C */
229 } __attribute__ ((packed
));
231 /* NOTE: about 30 OIDs are "mandatory" for peripherals to support ... and
232 * there are gobs more that may optionally be supported. We'll avoid as much
233 * of that mess as possible.
235 #define OID_802_3_PERMANENT_ADDRESS ccpu2(0x01010101)
236 #define OID_GEN_CURRENT_PACKET_FILTER ccpu2(0x0001010e)
239 * RNDIS notifications from device: command completion; "reverse"
242 static void rndis_status(struct usbnet
*dev
, struct urb
*urb
)
244 devdbg(dev
, "rndis status urb, len %d stat %d",
245 urb
->actual_length
, urb
->status
);
246 // FIXME for keepalives, respond immediately (asynchronously)
247 // if not an RNDIS status, do like cdc_status(dev,urb) does
251 * RPC done RNDIS-style. Caller guarantees:
252 * - message is properly byteswapped
253 * - there's no other request pending
254 * - buf can hold up to 1KB response (required by RNDIS spec)
255 * On return, the first few entries are already byteswapped.
257 * Call context is likely probe(), before interface name is known,
258 * which is why we won't try to use it in the diagnostics.
260 static int rndis_command(struct usbnet
*dev
, struct rndis_msg_hdr
*buf
)
262 struct cdc_state
*info
= (void *) &dev
->data
;
266 u32 xid
= 0, msg_len
, request_id
;
268 /* REVISIT when this gets called from contexts other than probe() or
269 * disconnect(): either serialize, or dispatch responses on xid
272 /* Issue the request; don't bother byteswapping our xid */
273 if (likely(buf
->msg_type
!= RNDIS_MSG_HALT
274 && buf
->msg_type
!= RNDIS_MSG_RESET
)) {
278 buf
->request_id
= (__force __le32
) xid
;
280 retval
= usb_control_msg(dev
->udev
,
281 usb_sndctrlpipe(dev
->udev
, 0),
282 USB_CDC_SEND_ENCAPSULATED_COMMAND
,
283 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
284 0, info
->u
->bMasterInterface0
,
285 buf
, le32_to_cpu(buf
->msg_len
),
286 RNDIS_CONTROL_TIMEOUT_MS
);
287 if (unlikely(retval
< 0 || xid
== 0))
290 // FIXME Seems like some devices discard responses when
291 // we time out and cancel our "get response" requests...
292 // so, this is fragile. Probably need to poll for status.
294 /* ignore status endpoint, just poll the control channel;
295 * the request probably completed immediately
297 rsp
= buf
->msg_type
| RNDIS_MSG_COMPLETION
;
298 for (count
= 0; count
< 10; count
++) {
299 memset(buf
, 0, 1024);
300 retval
= usb_control_msg(dev
->udev
,
301 usb_rcvctrlpipe(dev
->udev
, 0),
302 USB_CDC_GET_ENCAPSULATED_RESPONSE
,
303 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
304 0, info
->u
->bMasterInterface0
,
306 RNDIS_CONTROL_TIMEOUT_MS
);
307 if (likely(retval
>= 8)) {
308 msg_len
= le32_to_cpu(buf
->msg_len
);
309 request_id
= (__force u32
) buf
->request_id
;
310 if (likely(buf
->msg_type
== rsp
)) {
311 if (likely(request_id
== xid
)) {
312 if (unlikely(rsp
== RNDIS_MSG_RESET_C
))
314 if (likely(RNDIS_STATUS_SUCCESS
317 dev_dbg(&info
->control
->dev
,
318 "rndis reply status %08x\n",
319 le32_to_cpu(buf
->status
));
322 dev_dbg(&info
->control
->dev
,
323 "rndis reply id %d expected %d\n",
325 /* then likely retry */
326 } else switch (buf
->msg_type
) {
327 case RNDIS_MSG_INDICATE
: { /* fault */
328 // struct rndis_indicate *msg = (void *)buf;
329 dev_info(&info
->control
->dev
,
330 "rndis fault indication\n");
333 case RNDIS_MSG_KEEPALIVE
: { /* ping */
334 struct rndis_keepalive_c
*msg
= (void *)buf
;
336 msg
->msg_type
= RNDIS_MSG_KEEPALIVE_C
;
337 msg
->msg_len
= ccpu2(sizeof *msg
);
338 msg
->status
= RNDIS_STATUS_SUCCESS
;
339 retval
= usb_control_msg(dev
->udev
,
340 usb_sndctrlpipe(dev
->udev
, 0),
341 USB_CDC_SEND_ENCAPSULATED_COMMAND
,
342 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
343 0, info
->u
->bMasterInterface0
,
345 RNDIS_CONTROL_TIMEOUT_MS
);
346 if (unlikely(retval
< 0))
347 dev_dbg(&info
->control
->dev
,
348 "rndis keepalive err %d\n",
353 dev_dbg(&info
->control
->dev
,
354 "unexpected rndis msg %08x len %d\n",
355 le32_to_cpu(buf
->msg_type
), msg_len
);
358 /* device probably issued a protocol stall; ignore */
359 dev_dbg(&info
->control
->dev
,
360 "rndis response error, code %d\n", retval
);
364 dev_dbg(&info
->control
->dev
, "rndis response timeout\n");
368 static int rndis_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
371 struct net_device
*net
= dev
->net
;
374 struct rndis_msg_hdr
*header
;
375 struct rndis_init
*init
;
376 struct rndis_init_c
*init_c
;
377 struct rndis_query
*get
;
378 struct rndis_query_c
*get_c
;
379 struct rndis_set
*set
;
380 struct rndis_set_c
*set_c
;
384 /* we can't rely on i/o from stack working, or stack allocation */
385 u
.buf
= kmalloc(1024, GFP_KERNEL
);
388 retval
= usbnet_generic_cdc_bind(dev
, intf
);
392 net
->hard_header_len
+= sizeof (struct rndis_data_hdr
);
394 /* initialize; max transfer is 16KB at full speed */
395 u
.init
->msg_type
= RNDIS_MSG_INIT
;
396 u
.init
->msg_len
= ccpu2(sizeof *u
.init
);
397 u
.init
->major_version
= ccpu2(1);
398 u
.init
->minor_version
= ccpu2(0);
399 u
.init
->max_transfer_size
= ccpu2(net
->mtu
+ net
->hard_header_len
);
401 retval
= rndis_command(dev
, u
.header
);
402 if (unlikely(retval
< 0)) {
403 /* it might not even be an RNDIS device!! */
404 dev_err(&intf
->dev
, "RNDIS init failed, %d\n", retval
);
406 usb_driver_release_interface(driver_of(intf
),
407 ((struct cdc_state
*)&(dev
->data
))->data
);
410 dev
->hard_mtu
= le32_to_cpu(u
.init_c
->max_transfer_size
);
411 /* REVISIT: peripheral "alignment" request is ignored ... */
412 dev_dbg(&intf
->dev
, "hard mtu %u, align %d\n", dev
->hard_mtu
,
413 1 << le32_to_cpu(u
.init_c
->packet_alignment
));
415 /* get designated host ethernet address */
416 memset(u
.get
, 0, sizeof *u
.get
);
417 u
.get
->msg_type
= RNDIS_MSG_QUERY
;
418 u
.get
->msg_len
= ccpu2(sizeof *u
.get
);
419 u
.get
->oid
= OID_802_3_PERMANENT_ADDRESS
;
421 retval
= rndis_command(dev
, u
.header
);
422 if (unlikely(retval
< 0)) {
423 dev_err(&intf
->dev
, "rndis get ethaddr, %d\n", retval
);
426 tmp
= le32_to_cpu(u
.get_c
->offset
);
427 if (unlikely((tmp
+ 8) > (1024 - ETH_ALEN
)
428 || u
.get_c
->len
!= ccpu2(ETH_ALEN
))) {
429 dev_err(&intf
->dev
, "rndis ethaddr off %d len %d ?\n",
430 tmp
, le32_to_cpu(u
.get_c
->len
));
434 memcpy(net
->dev_addr
, tmp
+ (char *)&u
.get_c
->request_id
, ETH_ALEN
);
436 /* set a nonzero filter to enable data transfers */
437 memset(u
.set
, 0, sizeof *u
.set
);
438 u
.set
->msg_type
= RNDIS_MSG_SET
;
439 u
.set
->msg_len
= ccpu2(4 + sizeof *u
.set
);
440 u
.set
->oid
= OID_GEN_CURRENT_PACKET_FILTER
;
441 u
.set
->len
= ccpu2(4);
442 u
.set
->offset
= ccpu2((sizeof *u
.set
) - 8);
443 *(__le32
*)(u
.buf
+ sizeof *u
.set
) = ccpu2(DEFAULT_FILTER
);
445 retval
= rndis_command(dev
, u
.header
);
446 if (unlikely(retval
< 0)) {
447 dev_err(&intf
->dev
, "rndis set packet filter, %d\n", retval
);
457 static void rndis_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
459 struct rndis_halt
*halt
;
461 /* try to clear any rndis state/activity (no i/o from stack!) */
462 halt
= kcalloc(1, sizeof *halt
, SLAB_KERNEL
);
464 halt
->msg_type
= RNDIS_MSG_HALT
;
465 halt
->msg_len
= ccpu2(sizeof *halt
);
466 (void) rndis_command(dev
, (void *)halt
);
470 return usbnet_cdc_unbind(dev
, intf
);
474 * DATA -- host must not write zlps
476 static int rndis_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
478 /* peripheral may have batched packets to us... */
479 while (likely(skb
->len
)) {
480 struct rndis_data_hdr
*hdr
= (void *)skb
->data
;
481 struct sk_buff
*skb2
;
482 u32 msg_len
, data_offset
, data_len
;
484 msg_len
= le32_to_cpu(hdr
->msg_len
);
485 data_offset
= le32_to_cpu(hdr
->data_offset
);
486 data_len
= le32_to_cpu(hdr
->data_len
);
488 /* don't choke if we see oob, per-packet data, etc */
489 if (unlikely(hdr
->msg_type
!= RNDIS_MSG_PACKET
490 || skb
->len
< msg_len
491 || (data_offset
+ data_len
+ 8) > msg_len
)) {
492 dev
->stats
.rx_frame_errors
++;
493 devdbg(dev
, "bad rndis message %d/%d/%d/%d, len %d",
494 le32_to_cpu(hdr
->msg_type
),
495 msg_len
, data_offset
, data_len
, skb
->len
);
498 skb_pull(skb
, 8 + data_offset
);
500 /* at most one packet left? */
501 if (likely((data_len
- skb
->len
) <= sizeof *hdr
)) {
502 skb_trim(skb
, data_len
);
506 /* try to return all the packets in the batch */
507 skb2
= skb_clone(skb
, GFP_ATOMIC
);
510 skb_pull(skb
, msg_len
- sizeof *hdr
);
511 skb_trim(skb2
, data_len
);
512 usbnet_skb_return(dev
, skb2
);
515 /* caller will usbnet_skb_return the remaining packet */
519 static struct sk_buff
*
520 rndis_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
, gfp_t flags
)
522 struct rndis_data_hdr
*hdr
;
523 struct sk_buff
*skb2
;
524 unsigned len
= skb
->len
;
526 if (likely(!skb_cloned(skb
))) {
527 int room
= skb_headroom(skb
);
529 /* enough head room as-is? */
530 if (unlikely((sizeof *hdr
) <= room
))
533 /* enough room, but needs to be readjusted? */
534 room
+= skb_tailroom(skb
);
535 if (likely((sizeof *hdr
) <= room
)) {
536 skb
->data
= memmove(skb
->head
+ sizeof *hdr
,
538 skb
->tail
= skb
->data
+ len
;
543 /* create a new skb, with the correct size (and tailpad) */
544 skb2
= skb_copy_expand(skb
, sizeof *hdr
, 1, flags
);
545 dev_kfree_skb_any(skb
);
550 /* fill out the RNDIS header. we won't bother trying to batch
551 * packets; Linux minimizes wasted bandwidth through tx queues.
554 hdr
= (void *) __skb_push(skb
, sizeof *hdr
);
555 memset(hdr
, 0, sizeof *hdr
);
556 hdr
->msg_type
= RNDIS_MSG_PACKET
;
557 hdr
->msg_len
= cpu_to_le32(skb
->len
);
558 hdr
->data_offset
= ccpu2(sizeof(*hdr
) - 8);
559 hdr
->data_len
= cpu_to_le32(len
);
561 /* FIXME make the last packet always be short ... */
566 static const struct driver_info rndis_info
= {
567 .description
= "RNDIS device",
568 .flags
= FLAG_ETHER
| FLAG_FRAMING_RN
,
570 .unbind
= rndis_unbind
,
571 .status
= rndis_status
,
572 .rx_fixup
= rndis_rx_fixup
,
573 .tx_fixup
= rndis_tx_fixup
,
579 /*-------------------------------------------------------------------------*/
581 static const struct usb_device_id products
[] = {
583 /* RNDIS is MSFT's un-official variant of CDC ACM */
584 USB_INTERFACE_INFO(USB_CLASS_COMM
, 2 /* ACM */, 0x0ff),
585 .driver_info
= (unsigned long) &rndis_info
,
589 MODULE_DEVICE_TABLE(usb
, products
);
591 static struct usb_driver rndis_driver
= {
592 .owner
= THIS_MODULE
,
593 .name
= "rndis_host",
594 .id_table
= products
,
595 .probe
= usbnet_probe
,
596 .disconnect
= usbnet_disconnect
,
597 .suspend
= usbnet_suspend
,
598 .resume
= usbnet_resume
,
601 static int __init
rndis_init(void)
603 return usb_register(&rndis_driver
);
605 module_init(rndis_init
);
607 static void __exit
rndis_exit(void)
609 usb_deregister(&rndis_driver
);
611 module_exit(rndis_exit
);
613 MODULE_AUTHOR("David Brownell");
614 MODULE_DESCRIPTION("USB Host side RNDIS driver");
615 MODULE_LICENSE("GPL");