blob: 49991ac1bf3b843420543013c14df70dea443526 [file] [log] [blame]
David Brownell64e04912005-08-31 09:54:36 -07001/*
2 * Host Side support for RNDIS Networking Links
3 * Copyright (C) 2005 by David Brownell
4 *
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.
9 *
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.
14 *
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
18 */
19
20// #define DEBUG // error path messages, extra info
21// #define VERBOSE // more; success messages
22
23#include <linux/config.h>
David Brownell64e04912005-08-31 09:54:36 -070024#include <linux/module.h>
25#include <linux/sched.h>
26#include <linux/init.h>
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30#include <linux/workqueue.h>
31#include <linux/mii.h>
32#include <linux/usb.h>
33#include <linux/usb_cdc.h>
34
35#include "usbnet.h"
36
37
38/*
39 * RNDIS is NDIS remoted over USB. It's a MSFT variant of CDC ACM ... of
40 * course ACM was intended for modems, not Ethernet links! USB's standard
41 * for Ethernet links is "CDC Ethernet", which is significantly simpler.
42 */
43
44/*
45 * CONTROL uses CDC "encapsulated commands" with funky notifications.
46 * - control-out: SEND_ENCAPSULATED
47 * - interrupt-in: RESPONSE_AVAILABLE
48 * - control-in: GET_ENCAPSULATED
49 *
50 * We'll try to ignore the RESPONSE_AVAILABLE notifications.
51 */
52struct rndis_msg_hdr {
53 __le32 msg_type; /* RNDIS_MSG_* */
54 __le32 msg_len;
55 // followed by data that varies between messages
56 __le32 request_id;
57 __le32 status;
58 // ... and more
59} __attribute__ ((packed));
60
61/* RNDIS defines this (absurdly huge) control timeout */
62#define RNDIS_CONTROL_TIMEOUT_MS (10 * 1000)
63
64
65#define ccpu2 __constant_cpu_to_le32
66
67#define RNDIS_MSG_COMPLETION ccpu2(0x80000000)
68
69/* codes for "msg_type" field of rndis messages;
70 * only the data channel uses packet messages (maybe batched);
71 * everything else goes on the control channel.
72 */
73#define RNDIS_MSG_PACKET ccpu2(0x00000001) /* 1-N packets */
74#define RNDIS_MSG_INIT ccpu2(0x00000002)
75#define RNDIS_MSG_INIT_C (RNDIS_MSG_INIT|RNDIS_MSG_COMPLETION)
76#define RNDIS_MSG_HALT ccpu2(0x00000003)
77#define RNDIS_MSG_QUERY ccpu2(0x00000004)
78#define RNDIS_MSG_QUERY_C (RNDIS_MSG_QUERY|RNDIS_MSG_COMPLETION)
79#define RNDIS_MSG_SET ccpu2(0x00000005)
80#define RNDIS_MSG_SET_C (RNDIS_MSG_SET|RNDIS_MSG_COMPLETION)
81#define RNDIS_MSG_RESET ccpu2(0x00000006)
82#define RNDIS_MSG_RESET_C (RNDIS_MSG_RESET|RNDIS_MSG_COMPLETION)
83#define RNDIS_MSG_INDICATE ccpu2(0x00000007)
84#define RNDIS_MSG_KEEPALIVE ccpu2(0x00000008)
85#define RNDIS_MSG_KEEPALIVE_C (RNDIS_MSG_KEEPALIVE|RNDIS_MSG_COMPLETION)
86
87/* codes for "status" field of completion messages */
88#define RNDIS_STATUS_SUCCESS ccpu2(0x00000000)
89#define RNDIS_STATUS_FAILURE ccpu2(0xc0000001)
90#define RNDIS_STATUS_INVALID_DATA ccpu2(0xc0010015)
91#define RNDIS_STATUS_NOT_SUPPORTED ccpu2(0xc00000bb)
92#define RNDIS_STATUS_MEDIA_CONNECT ccpu2(0x4001000b)
93#define RNDIS_STATUS_MEDIA_DISCONNECT ccpu2(0x4001000c)
94
95
96struct rndis_data_hdr {
97 __le32 msg_type; /* RNDIS_MSG_PACKET */
98 __le32 msg_len; // rndis_data_hdr + data_len + pad
99 __le32 data_offset; // 36 -- right after header
100 __le32 data_len; // ... real packet size
101
102 __le32 oob_data_offset; // zero
103 __le32 oob_data_len; // zero
104 __le32 num_oob; // zero
105 __le32 packet_data_offset; // zero
106
107 __le32 packet_data_len; // zero
108 __le32 vc_handle; // zero
109 __le32 reserved; // zero
110} __attribute__ ((packed));
111
112struct rndis_init { /* OUT */
113 // header and:
114 __le32 msg_type; /* RNDIS_MSG_INIT */
115 __le32 msg_len; // 24
116 __le32 request_id;
117 __le32 major_version; // of rndis (1.0)
118 __le32 minor_version;
119 __le32 max_transfer_size;
120} __attribute__ ((packed));
121
122struct rndis_init_c { /* IN */
123 // header and:
124 __le32 msg_type; /* RNDIS_MSG_INIT_C */
125 __le32 msg_len;
126 __le32 request_id;
127 __le32 status;
128 __le32 major_version; // of rndis (1.0)
129 __le32 minor_version;
130 __le32 device_flags;
131 __le32 medium; // zero == 802.3
132 __le32 max_packets_per_message;
133 __le32 max_transfer_size;
134 __le32 packet_alignment; // max 7; (1<<n) bytes
135 __le32 af_list_offset; // zero
136 __le32 af_list_size; // zero
137} __attribute__ ((packed));
138
139struct rndis_halt { /* OUT (no reply) */
140 // header and:
141 __le32 msg_type; /* RNDIS_MSG_HALT */
142 __le32 msg_len;
143 __le32 request_id;
144} __attribute__ ((packed));
145
146struct rndis_query { /* OUT */
147 // header and:
148 __le32 msg_type; /* RNDIS_MSG_QUERY */
149 __le32 msg_len;
150 __le32 request_id;
151 __le32 oid;
152 __le32 len;
153 __le32 offset;
154/*?*/ __le32 handle; // zero
155} __attribute__ ((packed));
156
157struct rndis_query_c { /* IN */
158 // header and:
159 __le32 msg_type; /* RNDIS_MSG_QUERY_C */
160 __le32 msg_len;
161 __le32 request_id;
162 __le32 status;
163 __le32 len;
164 __le32 offset;
165} __attribute__ ((packed));
166
167struct rndis_set { /* OUT */
168 // header and:
169 __le32 msg_type; /* RNDIS_MSG_SET */
170 __le32 msg_len;
171 __le32 request_id;
172 __le32 oid;
173 __le32 len;
174 __le32 offset;
175/*?*/ __le32 handle; // zero
176} __attribute__ ((packed));
177
178struct rndis_set_c { /* IN */
179 // header and:
180 __le32 msg_type; /* RNDIS_MSG_SET_C */
181 __le32 msg_len;
182 __le32 request_id;
183 __le32 status;
184} __attribute__ ((packed));
185
186struct rndis_reset { /* IN */
187 // header and:
188 __le32 msg_type; /* RNDIS_MSG_RESET */
189 __le32 msg_len;
190 __le32 reserved;
191} __attribute__ ((packed));
192
193struct rndis_reset_c { /* OUT */
194 // header and:
195 __le32 msg_type; /* RNDIS_MSG_RESET_C */
196 __le32 msg_len;
197 __le32 status;
198 __le32 addressing_lost;
199} __attribute__ ((packed));
200
201struct rndis_indicate { /* IN (unrequested) */
202 // header and:
203 __le32 msg_type; /* RNDIS_MSG_INDICATE */
204 __le32 msg_len;
205 __le32 status;
206 __le32 length;
207 __le32 offset;
208/**/ __le32 diag_status;
209 __le32 error_offset;
210/**/ __le32 message;
211} __attribute__ ((packed));
212
213struct rndis_keepalive { /* OUT (optionally IN) */
214 // header and:
215 __le32 msg_type; /* RNDIS_MSG_KEEPALIVE */
216 __le32 msg_len;
217 __le32 request_id;
218} __attribute__ ((packed));
219
220struct rndis_keepalive_c { /* IN (optionally OUT) */
221 // header and:
222 __le32 msg_type; /* RNDIS_MSG_KEEPALIVE_C */
223 __le32 msg_len;
224 __le32 request_id;
225 __le32 status;
226} __attribute__ ((packed));
227
228/* NOTE: about 30 OIDs are "mandatory" for peripherals to support ... and
229 * there are gobs more that may optionally be supported. We'll avoid as much
230 * of that mess as possible.
231 */
232#define OID_802_3_PERMANENT_ADDRESS ccpu2(0x01010101)
233#define OID_GEN_CURRENT_PACKET_FILTER ccpu2(0x0001010e)
234
235/*
236 * RNDIS notifications from device: command completion; "reverse"
237 * keepalives; etc
238 */
239static void rndis_status(struct usbnet *dev, struct urb *urb)
240{
241 devdbg(dev, "rndis status urb, len %d stat %d",
242 urb->actual_length, urb->status);
243 // FIXME for keepalives, respond immediately (asynchronously)
244 // if not an RNDIS status, do like cdc_status(dev,urb) does
245}
246
247/*
248 * RPC done RNDIS-style. Caller guarantees:
249 * - message is properly byteswapped
250 * - there's no other request pending
251 * - buf can hold up to 1KB response (required by RNDIS spec)
252 * On return, the first few entries are already byteswapped.
253 *
254 * Call context is likely probe(), before interface name is known,
255 * which is why we won't try to use it in the diagnostics.
256 */
257static int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf)
258{
259 struct cdc_state *info = (void *) &dev->data;
260 int retval;
261 unsigned count;
262 __le32 rsp;
263 u32 xid = 0, msg_len, request_id;
264
265 /* REVISIT when this gets called from contexts other than probe() or
266 * disconnect(): either serialize, or dispatch responses on xid
267 */
268
269 /* Issue the request; don't bother byteswapping our xid */
270 if (likely(buf->msg_type != RNDIS_MSG_HALT
271 && buf->msg_type != RNDIS_MSG_RESET)) {
272 xid = dev->xid++;
273 if (!xid)
274 xid = dev->xid++;
275 buf->request_id = (__force __le32) xid;
276 }
277 retval = usb_control_msg(dev->udev,
278 usb_sndctrlpipe(dev->udev, 0),
279 USB_CDC_SEND_ENCAPSULATED_COMMAND,
280 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
281 0, info->u->bMasterInterface0,
282 buf, le32_to_cpu(buf->msg_len),
283 RNDIS_CONTROL_TIMEOUT_MS);
284 if (unlikely(retval < 0 || xid == 0))
285 return retval;
286
287 // FIXME Seems like some devices discard responses when
288 // we time out and cancel our "get response" requests...
289 // so, this is fragile. Probably need to poll for status.
290
291 /* ignore status endpoint, just poll the control channel;
292 * the request probably completed immediately
293 */
294 rsp = buf->msg_type | RNDIS_MSG_COMPLETION;
295 for (count = 0; count < 10; count++) {
296 memset(buf, 0, 1024);
297 retval = usb_control_msg(dev->udev,
298 usb_rcvctrlpipe(dev->udev, 0),
299 USB_CDC_GET_ENCAPSULATED_RESPONSE,
300 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
301 0, info->u->bMasterInterface0,
302 buf, 1024,
303 RNDIS_CONTROL_TIMEOUT_MS);
304 if (likely(retval >= 8)) {
305 msg_len = le32_to_cpu(buf->msg_len);
306 request_id = (__force u32) buf->request_id;
307 if (likely(buf->msg_type == rsp)) {
308 if (likely(request_id == xid)) {
309 if (unlikely(rsp == RNDIS_MSG_RESET_C))
310 return 0;
311 if (likely(RNDIS_STATUS_SUCCESS
312 == buf->status))
313 return 0;
314 dev_dbg(&info->control->dev,
315 "rndis reply status %08x\n",
316 le32_to_cpu(buf->status));
317 return -EL3RST;
318 }
319 dev_dbg(&info->control->dev,
320 "rndis reply id %d expected %d\n",
321 request_id, xid);
322 /* then likely retry */
323 } else switch (buf->msg_type) {
324 case RNDIS_MSG_INDICATE: { /* fault */
325 // struct rndis_indicate *msg = (void *)buf;
326 dev_info(&info->control->dev,
327 "rndis fault indication\n");
328 }
329 break;
330 case RNDIS_MSG_KEEPALIVE: { /* ping */
331 struct rndis_keepalive_c *msg = (void *)buf;
332
333 msg->msg_type = RNDIS_MSG_KEEPALIVE_C;
334 msg->msg_len = ccpu2(sizeof *msg);
335 msg->status = RNDIS_STATUS_SUCCESS;
336 retval = usb_control_msg(dev->udev,
337 usb_sndctrlpipe(dev->udev, 0),
338 USB_CDC_SEND_ENCAPSULATED_COMMAND,
339 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
340 0, info->u->bMasterInterface0,
341 msg, sizeof *msg,
342 RNDIS_CONTROL_TIMEOUT_MS);
343 if (unlikely(retval < 0))
344 dev_dbg(&info->control->dev,
345 "rndis keepalive err %d\n",
346 retval);
347 }
348 break;
349 default:
350 dev_dbg(&info->control->dev,
351 "unexpected rndis msg %08x len %d\n",
352 le32_to_cpu(buf->msg_type), msg_len);
353 }
354 } else {
355 /* device probably issued a protocol stall; ignore */
356 dev_dbg(&info->control->dev,
357 "rndis response error, code %d\n", retval);
358 }
359 msleep(2);
360 }
361 dev_dbg(&info->control->dev, "rndis response timeout\n");
362 return -ETIMEDOUT;
363}
364
365static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
366{
367 int retval;
368 struct net_device *net = dev->net;
369 union {
370 void *buf;
371 struct rndis_msg_hdr *header;
372 struct rndis_init *init;
373 struct rndis_init_c *init_c;
374 struct rndis_query *get;
375 struct rndis_query_c *get_c;
376 struct rndis_set *set;
377 struct rndis_set_c *set_c;
378 } u;
379 u32 tmp;
380
381 /* we can't rely on i/o from stack working, or stack allocation */
382 u.buf = kmalloc(1024, GFP_KERNEL);
383 if (!u.buf)
384 return -ENOMEM;
385 retval = usbnet_generic_cdc_bind(dev, intf);
386 if (retval < 0)
387 goto done;
388
389 net->hard_header_len += sizeof (struct rndis_data_hdr);
390
391 /* initialize; max transfer is 16KB at full speed */
392 u.init->msg_type = RNDIS_MSG_INIT;
393 u.init->msg_len = ccpu2(sizeof *u.init);
394 u.init->major_version = ccpu2(1);
395 u.init->minor_version = ccpu2(0);
396 u.init->max_transfer_size = ccpu2(net->mtu + net->hard_header_len);
397
398 retval = rndis_command(dev, u.header);
399 if (unlikely(retval < 0)) {
400 /* it might not even be an RNDIS device!! */
401 dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
402fail:
403 usb_driver_release_interface(driver_of(intf),
404 ((struct cdc_state *)&(dev->data))->data);
405 goto done;
406 }
407 dev->hard_mtu = le32_to_cpu(u.init_c->max_transfer_size);
408 /* REVISIT: peripheral "alignment" request is ignored ... */
409 dev_dbg(&intf->dev, "hard mtu %u, align %d\n", dev->hard_mtu,
410 1 << le32_to_cpu(u.init_c->packet_alignment));
411
412 /* get designated host ethernet address */
413 memset(u.get, 0, sizeof *u.get);
414 u.get->msg_type = RNDIS_MSG_QUERY;
415 u.get->msg_len = ccpu2(sizeof *u.get);
416 u.get->oid = OID_802_3_PERMANENT_ADDRESS;
417
418 retval = rndis_command(dev, u.header);
419 if (unlikely(retval < 0)) {
420 dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
421 goto fail;
422 }
423 tmp = le32_to_cpu(u.get_c->offset);
424 if (unlikely((tmp + 8) > (1024 - ETH_ALEN)
425 || u.get_c->len != ccpu2(ETH_ALEN))) {
426 dev_err(&intf->dev, "rndis ethaddr off %d len %d ?\n",
427 tmp, le32_to_cpu(u.get_c->len));
428 retval = -EDOM;
429 goto fail;
430 }
431 memcpy(net->dev_addr, tmp + (char *)&u.get_c->request_id, ETH_ALEN);
432
433 /* set a nonzero filter to enable data transfers */
434 memset(u.set, 0, sizeof *u.set);
435 u.set->msg_type = RNDIS_MSG_SET;
436 u.set->msg_len = ccpu2(4 + sizeof *u.set);
437 u.set->oid = OID_GEN_CURRENT_PACKET_FILTER;
438 u.set->len = ccpu2(4);
439 u.set->offset = ccpu2((sizeof *u.set) - 8);
440 *(__le32 *)(u.buf + sizeof *u.set) = ccpu2(DEFAULT_FILTER);
441
442 retval = rndis_command(dev, u.header);
443 if (unlikely(retval < 0)) {
444 dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
445 goto fail;
446 }
447
448 retval = 0;
449done:
450 kfree(u.buf);
451 return retval;
452}
453
454static void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
455{
456 struct rndis_halt *halt;
457
458 /* try to clear any rndis state/activity (no i/o from stack!) */
459 halt = kcalloc(1, sizeof *halt, SLAB_KERNEL);
460 if (halt) {
461 halt->msg_type = RNDIS_MSG_HALT;
462 halt->msg_len = ccpu2(sizeof *halt);
463 (void) rndis_command(dev, (void *)halt);
464 kfree(halt);
465 }
466
467 return usbnet_cdc_unbind(dev, intf);
468}
469
470/*
471 * DATA -- host must not write zlps
472 */
473static int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
474{
475 /* peripheral may have batched packets to us... */
476 while (likely(skb->len)) {
477 struct rndis_data_hdr *hdr = (void *)skb->data;
478 struct sk_buff *skb2;
479 u32 msg_len, data_offset, data_len;
480
481 msg_len = le32_to_cpu(hdr->msg_len);
482 data_offset = le32_to_cpu(hdr->data_offset);
483 data_len = le32_to_cpu(hdr->data_len);
484
485 /* don't choke if we see oob, per-packet data, etc */
486 if (unlikely(hdr->msg_type != RNDIS_MSG_PACKET
487 || skb->len < msg_len
488 || (data_offset + data_len + 8) > msg_len)) {
489 dev->stats.rx_frame_errors++;
490 devdbg(dev, "bad rndis message %d/%d/%d/%d, len %d",
491 le32_to_cpu(hdr->msg_type),
492 msg_len, data_offset, data_len, skb->len);
493 return 0;
494 }
495 skb_pull(skb, 8 + data_offset);
496
497 /* at most one packet left? */
498 if (likely((data_len - skb->len) <= sizeof *hdr)) {
499 skb_trim(skb, data_len);
500 break;
501 }
502
503 /* try to return all the packets in the batch */
504 skb2 = skb_clone(skb, GFP_ATOMIC);
505 if (unlikely(!skb2))
506 break;
507 skb_pull(skb, msg_len - sizeof *hdr);
508 skb_trim(skb2, data_len);
509 usbnet_skb_return(dev, skb2);
510 }
511
512 /* caller will usbnet_skb_return the remaining packet */
513 return 1;
514}
515
516static struct sk_buff *
Al Viro55016f12005-10-21 03:21:58 -0400517rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
David Brownell64e04912005-08-31 09:54:36 -0700518{
519 struct rndis_data_hdr *hdr;
520 struct sk_buff *skb2;
521 unsigned len = skb->len;
522
523 if (likely(!skb_cloned(skb))) {
524 int room = skb_headroom(skb);
525
526 /* enough head room as-is? */
527 if (unlikely((sizeof *hdr) <= room))
528 goto fill;
529
530 /* enough room, but needs to be readjusted? */
531 room += skb_tailroom(skb);
532 if (likely((sizeof *hdr) <= room)) {
533 skb->data = memmove(skb->head + sizeof *hdr,
534 skb->data, len);
535 skb->tail = skb->data + len;
536 goto fill;
537 }
538 }
539
540 /* create a new skb, with the correct size (and tailpad) */
541 skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
542 dev_kfree_skb_any(skb);
543 if (unlikely(!skb2))
544 return skb2;
545 skb = skb2;
546
547 /* fill out the RNDIS header. we won't bother trying to batch
548 * packets; Linux minimizes wasted bandwidth through tx queues.
549 */
550fill:
551 hdr = (void *) __skb_push(skb, sizeof *hdr);
552 memset(hdr, 0, sizeof *hdr);
553 hdr->msg_type = RNDIS_MSG_PACKET;
554 hdr->msg_len = cpu_to_le32(skb->len);
555 hdr->data_offset = ccpu2(sizeof(*hdr) - 8);
556 hdr->data_len = cpu_to_le32(len);
557
558 /* FIXME make the last packet always be short ... */
559 return skb;
560}
561
562
563static const struct driver_info rndis_info = {
564 .description = "RNDIS device",
565 .flags = FLAG_ETHER | FLAG_FRAMING_RN,
566 .bind = rndis_bind,
567 .unbind = rndis_unbind,
568 .status = rndis_status,
569 .rx_fixup = rndis_rx_fixup,
570 .tx_fixup = rndis_tx_fixup,
571};
572
573#undef ccpu2
574
575
576/*-------------------------------------------------------------------------*/
577
578static const struct usb_device_id products [] = {
579{
580 /* RNDIS is MSFT's un-official variant of CDC ACM */
581 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
582 .driver_info = (unsigned long) &rndis_info,
583},
584 { }, // END
585};
586MODULE_DEVICE_TABLE(usb, products);
587
588static struct usb_driver rndis_driver = {
David Brownell64e04912005-08-31 09:54:36 -0700589 .name = "rndis_host",
590 .id_table = products,
591 .probe = usbnet_probe,
592 .disconnect = usbnet_disconnect,
593 .suspend = usbnet_suspend,
594 .resume = usbnet_resume,
595};
596
597static int __init rndis_init(void)
598{
599 return usb_register(&rndis_driver);
600}
601module_init(rndis_init);
602
603static void __exit rndis_exit(void)
604{
605 usb_deregister(&rndis_driver);
606}
607module_exit(rndis_exit);
608
609MODULE_AUTHOR("David Brownell");
610MODULE_DESCRIPTION("USB Host side RNDIS driver");
611MODULE_LICENSE("GPL");