GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / usb / rndis_host.c
blob3929a3674b103405cdb9658f58b739021e9a8ea0
1 /*
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
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/ethtool.h>
24 #include <linux/workqueue.h>
25 #include <linux/slab.h>
26 #include <linux/mii.h>
27 #include <linux/usb.h>
28 #include <linux/usb/cdc.h>
29 #include <linux/usb/usbnet.h>
30 #include <linux/usb/rndis_host.h>
34 * RNDIS is NDIS remoted over USB. It's a MSFT variant of CDC ACM ... of
35 * course ACM was intended for modems, not Ethernet links! USB's standard
36 * for Ethernet links is "CDC Ethernet", which is significantly simpler.
38 * NOTE that Microsoft's "RNDIS 1.0" specification is incomplete. Issues
39 * include:
40 * - Power management in particular relies on information that's scattered
41 * through other documentation, and which is incomplete or incorrect even
42 * there.
43 * - There are various undocumented protocol requirements, such as the
44 * need to send unused garbage in control-OUT messages.
45 * - In some cases, MS-Windows will emit undocumented requests; this
46 * matters more to peripheral implementations than host ones.
48 * Moreover there's a no-open-specs variant of RNDIS called "ActiveSync".
50 * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in
51 * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and
52 * currently rare) "Ethernet Emulation Model" (EEM).
56 * RNDIS notifications from device: command completion; "reverse"
57 * keepalives; etc
59 void rndis_status(struct usbnet *dev, struct urb *urb)
61 netdev_dbg(dev->net, "rndis status urb, len %d stat %d\n",
62 urb->actual_length, urb->status);
63 // if not an RNDIS status, do like cdc_status(dev,urb) does
65 EXPORT_SYMBOL_GPL(rndis_status);
68 * RNDIS indicate messages.
70 static void rndis_msg_indicate(struct usbnet *dev, struct rndis_indicate *msg,
71 int buflen)
73 struct cdc_state *info = (void *)&dev->data;
74 struct device *udev = &info->control->dev;
76 if (dev->driver_info->indication) {
77 dev->driver_info->indication(dev, msg, buflen);
78 } else {
79 switch (msg->status) {
80 case RNDIS_STATUS_MEDIA_CONNECT:
81 dev_info(udev, "rndis media connect\n");
82 break;
83 case RNDIS_STATUS_MEDIA_DISCONNECT:
84 dev_info(udev, "rndis media disconnect\n");
85 break;
86 default:
87 dev_info(udev, "rndis indication: 0x%08x\n",
88 le32_to_cpu(msg->status));
94 * RPC done RNDIS-style. Caller guarantees:
95 * - message is properly byteswapped
96 * - there's no other request pending
97 * - buf can hold up to 1KB response (required by RNDIS spec)
98 * On return, the first few entries are already byteswapped.
100 * Call context is likely probe(), before interface name is known,
101 * which is why we won't try to use it in the diagnostics.
103 int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf, int buflen)
105 struct cdc_state *info = (void *) &dev->data;
106 int master_ifnum;
107 int retval;
108 unsigned count;
109 __le32 rsp;
110 u32 xid = 0, msg_len, request_id;
112 /* REVISIT when this gets called from contexts other than probe() or
113 * disconnect(): either serialize, or dispatch responses on xid
116 /* Issue the request; xid is unique, don't bother byteswapping it */
117 if (likely(buf->msg_type != RNDIS_MSG_HALT &&
118 buf->msg_type != RNDIS_MSG_RESET)) {
119 xid = dev->xid++;
120 if (!xid)
121 xid = dev->xid++;
122 buf->request_id = (__force __le32) xid;
124 master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber;
125 retval = usb_control_msg(dev->udev,
126 usb_sndctrlpipe(dev->udev, 0),
127 USB_CDC_SEND_ENCAPSULATED_COMMAND,
128 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
129 0, master_ifnum,
130 buf, le32_to_cpu(buf->msg_len),
131 RNDIS_CONTROL_TIMEOUT_MS);
132 if (unlikely(retval < 0 || xid == 0))
133 return retval;
135 // we time out and cancel our "get response" requests...
136 // so, this is fragile. Probably need to poll for status.
138 /* ignore status endpoint, just poll the control channel;
139 * the request probably completed immediately
141 rsp = buf->msg_type | RNDIS_MSG_COMPLETION;
142 for (count = 0; count < 10; count++) {
143 memset(buf, 0, CONTROL_BUFFER_SIZE);
144 retval = usb_control_msg(dev->udev,
145 usb_rcvctrlpipe(dev->udev, 0),
146 USB_CDC_GET_ENCAPSULATED_RESPONSE,
147 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
148 0, master_ifnum,
149 buf, buflen,
150 RNDIS_CONTROL_TIMEOUT_MS);
151 if (likely(retval >= 8)) {
152 msg_len = le32_to_cpu(buf->msg_len);
153 request_id = (__force u32) buf->request_id;
154 if (likely(buf->msg_type == rsp)) {
155 if (likely(request_id == xid)) {
156 if (unlikely(rsp == RNDIS_MSG_RESET_C))
157 return 0;
158 if (likely(RNDIS_STATUS_SUCCESS
159 == buf->status))
160 return 0;
161 dev_dbg(&info->control->dev,
162 "rndis reply status %08x\n",
163 le32_to_cpu(buf->status));
164 return -EL3RST;
166 dev_dbg(&info->control->dev,
167 "rndis reply id %d expected %d\n",
168 request_id, xid);
169 /* then likely retry */
170 } else switch (buf->msg_type) {
171 case RNDIS_MSG_INDICATE: /* fault/event */
172 rndis_msg_indicate(dev, (void *)buf, buflen);
174 break;
175 case RNDIS_MSG_KEEPALIVE: { /* ping */
176 struct rndis_keepalive_c *msg = (void *)buf;
178 msg->msg_type = RNDIS_MSG_KEEPALIVE_C;
179 msg->msg_len = cpu_to_le32(sizeof *msg);
180 msg->status = RNDIS_STATUS_SUCCESS;
181 retval = usb_control_msg(dev->udev,
182 usb_sndctrlpipe(dev->udev, 0),
183 USB_CDC_SEND_ENCAPSULATED_COMMAND,
184 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
185 0, master_ifnum,
186 msg, sizeof *msg,
187 RNDIS_CONTROL_TIMEOUT_MS);
188 if (unlikely(retval < 0))
189 dev_dbg(&info->control->dev,
190 "rndis keepalive err %d\n",
191 retval);
193 break;
194 default:
195 dev_dbg(&info->control->dev,
196 "unexpected rndis msg %08x len %d\n",
197 le32_to_cpu(buf->msg_type), msg_len);
199 } else {
200 /* device probably issued a protocol stall; ignore */
201 dev_dbg(&info->control->dev,
202 "rndis response error, code %d\n", retval);
204 msleep(20);
206 dev_dbg(&info->control->dev, "rndis response timeout\n");
207 return -ETIMEDOUT;
209 EXPORT_SYMBOL_GPL(rndis_command);
212 * rndis_query:
214 * Performs a query for @oid along with 0 or more bytes of payload as
215 * specified by @in_len. If @reply_len is not set to -1 then the reply
216 * length is checked against this value, resulting in an error if it
217 * doesn't match.
219 * NOTE: Adding a payload exactly or greater than the size of the expected
220 * response payload is an evident requirement MSFT added for ActiveSync.
222 * The only exception is for OIDs that return a variably sized response,
223 * in which case no payload should be added. This undocumented (and
224 * nonsensical!) issue was found by sniffing protocol requests from the
225 * ActiveSync 4.1 Windows driver.
227 static int rndis_query(struct usbnet *dev, struct usb_interface *intf,
228 void *buf, __le32 oid, u32 in_len,
229 void **reply, int *reply_len)
231 int retval;
232 union {
233 void *buf;
234 struct rndis_msg_hdr *header;
235 struct rndis_query *get;
236 struct rndis_query_c *get_c;
237 } u;
238 u32 off, len;
240 u.buf = buf;
242 memset(u.get, 0, sizeof *u.get + in_len);
243 u.get->msg_type = RNDIS_MSG_QUERY;
244 u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len);
245 u.get->oid = oid;
246 u.get->len = cpu_to_le32(in_len);
247 u.get->offset = cpu_to_le32(20);
249 retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
250 if (unlikely(retval < 0)) {
251 dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n",
252 oid, retval);
253 return retval;
256 off = le32_to_cpu(u.get_c->offset);
257 len = le32_to_cpu(u.get_c->len);
258 if (unlikely((8 + off + len) > CONTROL_BUFFER_SIZE))
259 goto response_error;
261 if (*reply_len != -1 && len != *reply_len)
262 goto response_error;
264 *reply = (unsigned char *) &u.get_c->request_id + off;
265 *reply_len = len;
267 return retval;
269 response_error:
270 dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) "
271 "invalid response - off %d len %d\n",
272 oid, off, len);
273 return -EDOM;
276 /* same as usbnet_netdev_ops but MTU change not allowed */
277 static const struct net_device_ops rndis_netdev_ops = {
278 .ndo_open = usbnet_open,
279 .ndo_stop = usbnet_stop,
280 .ndo_start_xmit = usbnet_start_xmit,
281 .ndo_tx_timeout = usbnet_tx_timeout,
282 .ndo_set_mac_address = eth_mac_addr,
283 .ndo_validate_addr = eth_validate_addr,
287 generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags)
289 int retval;
290 struct net_device *net = dev->net;
291 struct cdc_state *info = (void *) &dev->data;
292 union {
293 void *buf;
294 struct rndis_msg_hdr *header;
295 struct rndis_init *init;
296 struct rndis_init_c *init_c;
297 struct rndis_query *get;
298 struct rndis_query_c *get_c;
299 struct rndis_set *set;
300 struct rndis_set_c *set_c;
301 struct rndis_halt *halt;
302 } u;
303 u32 tmp;
304 __le32 phym_unspec, *phym;
305 int reply_len;
306 unsigned char *bp;
308 /* we can't rely on i/o from stack working, or stack allocation */
309 u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
310 if (!u.buf)
311 return -ENOMEM;
312 retval = usbnet_generic_cdc_bind(dev, intf);
313 if (retval < 0)
314 goto fail;
316 u.init->msg_type = RNDIS_MSG_INIT;
317 u.init->msg_len = cpu_to_le32(sizeof *u.init);
318 u.init->major_version = cpu_to_le32(1);
319 u.init->minor_version = cpu_to_le32(0);
321 /* max transfer (in spec) is 0x4000 at full speed, but for
322 * TX we'll stick to one Ethernet packet plus RNDIS framing.
323 * For RX we handle drivers that zero-pad to end-of-packet.
324 * Don't let userspace change these settings.
326 * NOTE: there still seems to be wierdness here, as if we need
327 * to do some more things to make sure WinCE targets accept this.
328 * They default to jumbograms of 8KB or 16KB, which is absurd
329 * for such low data rates and which is also more than Linux
330 * can usually expect to allocate for SKB data...
332 net->hard_header_len += sizeof (struct rndis_data_hdr);
333 dev->hard_mtu = net->mtu + net->hard_header_len;
335 dev->maxpacket = usb_maxpacket(dev->udev, dev->out, 1);
336 if (dev->maxpacket == 0) {
337 netif_dbg(dev, probe, dev->net,
338 "dev->maxpacket can't be 0\n");
339 retval = -EINVAL;
340 goto fail_and_release;
343 dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1);
344 dev->rx_urb_size &= ~(dev->maxpacket - 1);
345 u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size);
347 net->netdev_ops = &rndis_netdev_ops;
349 retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
350 if (unlikely(retval < 0)) {
351 /* it might not even be an RNDIS device!! */
352 dev_err(&intf->dev, "RNDIS init failed, %d\n", retval);
353 goto fail_and_release;
355 tmp = le32_to_cpu(u.init_c->max_transfer_size);
356 if (tmp < dev->hard_mtu) {
357 if (tmp <= net->hard_header_len) {
358 dev_err(&intf->dev,
359 "dev can't take %u byte packets (max %u)\n",
360 dev->hard_mtu, tmp);
361 retval = -EINVAL;
362 goto halt_fail_and_release;
364 dev_warn(&intf->dev,
365 "dev can't take %u byte packets (max %u), "
366 "adjusting MTU to %u\n",
367 dev->hard_mtu, tmp, tmp - net->hard_header_len);
368 dev->hard_mtu = tmp;
369 net->mtu = dev->hard_mtu - net->hard_header_len;
372 /* REVISIT: peripheral "alignment" request is ignored ... */
373 dev_dbg(&intf->dev,
374 "hard mtu %u (%u from dev), rx buflen %Zu, align %d\n",
375 dev->hard_mtu, tmp, dev->rx_urb_size,
376 1 << le32_to_cpu(u.init_c->packet_alignment));
378 /* module has some device initialization code needs to be done right
379 * after RNDIS_INIT */
380 if (dev->driver_info->early_init &&
381 dev->driver_info->early_init(dev) != 0)
382 goto halt_fail_and_release;
384 /* Check physical medium */
385 phym = NULL;
386 reply_len = sizeof *phym;
387 retval = rndis_query(dev, intf, u.buf, OID_GEN_PHYSICAL_MEDIUM,
388 0, (void **) &phym, &reply_len);
389 if (retval != 0 || !phym) {
390 /* OID is optional so don't fail here. */
391 phym_unspec = RNDIS_PHYSICAL_MEDIUM_UNSPECIFIED;
392 phym = &phym_unspec;
394 if ((flags & FLAG_RNDIS_PHYM_WIRELESS) &&
395 *phym != RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
396 netif_dbg(dev, probe, dev->net,
397 "driver requires wireless physical medium, but device is not\n");
398 retval = -ENODEV;
399 goto halt_fail_and_release;
401 if ((flags & FLAG_RNDIS_PHYM_NOT_WIRELESS) &&
402 *phym == RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN) {
403 netif_dbg(dev, probe, dev->net,
404 "driver requires non-wireless physical medium, but device is wireless.\n");
405 retval = -ENODEV;
406 goto halt_fail_and_release;
409 /* Get designated host ethernet address */
410 reply_len = ETH_ALEN;
411 retval = rndis_query(dev, intf, u.buf, OID_802_3_PERMANENT_ADDRESS,
412 48, (void **) &bp, &reply_len);
413 if (unlikely(retval< 0)) {
414 dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval);
415 goto halt_fail_and_release;
417 memcpy(net->dev_addr, bp, ETH_ALEN);
418 memcpy(net->perm_addr, bp, ETH_ALEN);
420 /* set a nonzero filter to enable data transfers */
421 memset(u.set, 0, sizeof *u.set);
422 u.set->msg_type = RNDIS_MSG_SET;
423 u.set->msg_len = cpu_to_le32(4 + sizeof *u.set);
424 u.set->oid = OID_GEN_CURRENT_PACKET_FILTER;
425 u.set->len = cpu_to_le32(4);
426 u.set->offset = cpu_to_le32((sizeof *u.set) - 8);
427 *(__le32 *)(u.buf + sizeof *u.set) = RNDIS_DEFAULT_FILTER;
429 retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE);
430 if (unlikely(retval < 0)) {
431 dev_err(&intf->dev, "rndis set packet filter, %d\n", retval);
432 goto halt_fail_and_release;
435 retval = 0;
437 kfree(u.buf);
438 return retval;
440 halt_fail_and_release:
441 memset(u.halt, 0, sizeof *u.halt);
442 u.halt->msg_type = RNDIS_MSG_HALT;
443 u.halt->msg_len = cpu_to_le32(sizeof *u.halt);
444 (void) rndis_command(dev, (void *)u.halt, CONTROL_BUFFER_SIZE);
445 fail_and_release:
446 usb_set_intfdata(info->data, NULL);
447 usb_driver_release_interface(driver_of(intf), info->data);
448 info->data = NULL;
449 fail:
450 kfree(u.buf);
451 return retval;
453 EXPORT_SYMBOL_GPL(generic_rndis_bind);
455 static int rndis_bind(struct usbnet *dev, struct usb_interface *intf)
457 return generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_NOT_WIRELESS);
460 void rndis_unbind(struct usbnet *dev, struct usb_interface *intf)
462 struct rndis_halt *halt;
464 /* try to clear any rndis state/activity (no i/o from stack!) */
465 halt = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
466 if (halt) {
467 halt->msg_type = RNDIS_MSG_HALT;
468 halt->msg_len = cpu_to_le32(sizeof *halt);
469 (void) rndis_command(dev, (void *)halt, CONTROL_BUFFER_SIZE);
470 kfree(halt);
473 usbnet_cdc_unbind(dev, intf);
475 EXPORT_SYMBOL_GPL(rndis_unbind);
478 * DATA -- host must not write zlps
480 int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
482 /* peripheral may have batched packets to us... */
483 while (likely(skb->len)) {
484 struct rndis_data_hdr *hdr = (void *)skb->data;
485 struct sk_buff *skb2;
486 u32 msg_len, data_offset, data_len;
488 msg_len = le32_to_cpu(hdr->msg_len);
489 data_offset = le32_to_cpu(hdr->data_offset);
490 data_len = le32_to_cpu(hdr->data_len);
492 /* don't choke if we see oob, per-packet data, etc */
493 if (unlikely(hdr->msg_type != RNDIS_MSG_PACKET ||
494 skb->len < msg_len ||
495 (data_offset + data_len + 8) > msg_len)) {
496 dev->net->stats.rx_frame_errors++;
497 netdev_dbg(dev->net, "bad rndis message %d/%d/%d/%d, len %d\n",
498 le32_to_cpu(hdr->msg_type),
499 msg_len, data_offset, data_len, skb->len);
500 return 0;
502 skb_pull(skb, 8 + data_offset);
504 /* at most one packet left? */
505 if (likely((data_len - skb->len) <= sizeof *hdr)) {
506 skb_trim(skb, data_len);
507 break;
510 /* try to return all the packets in the batch */
511 skb2 = skb_clone(skb, GFP_ATOMIC);
512 if (unlikely(!skb2))
513 break;
514 skb_pull(skb, msg_len - sizeof *hdr);
515 skb_trim(skb2, data_len);
516 usbnet_skb_return(dev, skb2);
519 /* caller will usbnet_skb_return the remaining packet */
520 return 1;
522 EXPORT_SYMBOL_GPL(rndis_rx_fixup);
524 struct sk_buff *
525 rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
527 struct rndis_data_hdr *hdr;
528 struct sk_buff *skb2;
529 unsigned len = skb->len;
531 if (likely(!skb_cloned(skb))) {
532 int room = skb_headroom(skb);
534 /* enough head room as-is? */
535 if (unlikely((sizeof *hdr) <= room))
536 goto fill;
538 /* enough room, but needs to be readjusted? */
539 room += skb_tailroom(skb);
540 if (likely((sizeof *hdr) <= room)) {
541 skb->data = memmove(skb->head + sizeof *hdr,
542 skb->data, len);
543 skb_set_tail_pointer(skb, len);
544 goto fill;
548 /* create a new skb, with the correct size (and tailpad) */
549 skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags);
550 dev_kfree_skb_any(skb);
551 if (unlikely(!skb2))
552 return skb2;
553 skb = skb2;
555 /* fill out the RNDIS header. we won't bother trying to batch
556 * packets; Linux minimizes wasted bandwidth through tx queues.
558 fill:
559 hdr = (void *) __skb_push(skb, sizeof *hdr);
560 memset(hdr, 0, sizeof *hdr);
561 hdr->msg_type = RNDIS_MSG_PACKET;
562 hdr->msg_len = cpu_to_le32(skb->len);
563 hdr->data_offset = cpu_to_le32(sizeof(*hdr) - 8);
564 hdr->data_len = cpu_to_le32(len);
566 return skb;
568 EXPORT_SYMBOL_GPL(rndis_tx_fixup);
571 static const struct driver_info rndis_info = {
572 .description = "RNDIS device",
573 .flags = FLAG_ETHER | FLAG_FRAMING_RN | FLAG_NO_SETINT,
574 .bind = rndis_bind,
575 .unbind = rndis_unbind,
576 .status = rndis_status,
577 .rx_fixup = rndis_rx_fixup,
578 .tx_fixup = rndis_tx_fixup,
581 /*-------------------------------------------------------------------------*/
583 static const struct usb_device_id products [] = {
585 /* RNDIS is MSFT's un-official variant of CDC ACM */
586 USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
587 .driver_info = (unsigned long) &rndis_info,
588 }, {
589 /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
590 USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
591 .driver_info = (unsigned long) &rndis_info,
592 }, {
593 /* RNDIS for tethering */
594 USB_INTERFACE_INFO(USB_CLASS_WIRELESS_CONTROLLER, 1, 3),
595 .driver_info = (unsigned long) &rndis_info,
597 { }, // END
599 MODULE_DEVICE_TABLE(usb, products);
601 static struct usb_driver rndis_driver = {
602 .name = "rndis_host",
603 .id_table = products,
604 .probe = usbnet_probe,
605 .disconnect = usbnet_disconnect,
606 .suspend = usbnet_suspend,
607 .resume = usbnet_resume,
610 static int __init rndis_init(void)
612 return usb_register(&rndis_driver);
614 module_init(rndis_init);
616 static void __exit rndis_exit(void)
618 usb_deregister(&rndis_driver);
620 module_exit(rndis_exit);
622 MODULE_AUTHOR("David Brownell");
623 MODULE_DESCRIPTION("USB Host side RNDIS driver");
624 MODULE_LICENSE("GPL");