15b2fc499SJeff Garzik /* 25b2fc499SJeff Garzik * Host Side support for RNDIS Networking Links 35b2fc499SJeff Garzik * Copyright (C) 2005 by David Brownell 45b2fc499SJeff Garzik * 55b2fc499SJeff Garzik * This program is free software; you can redistribute it and/or modify 65b2fc499SJeff Garzik * it under the terms of the GNU General Public License as published by 75b2fc499SJeff Garzik * the Free Software Foundation; either version 2 of the License, or 85b2fc499SJeff Garzik * (at your option) any later version. 95b2fc499SJeff Garzik * 105b2fc499SJeff Garzik * This program is distributed in the hope that it will be useful, 115b2fc499SJeff Garzik * but WITHOUT ANY WARRANTY; without even the implied warranty of 125b2fc499SJeff Garzik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 135b2fc499SJeff Garzik * GNU General Public License for more details. 145b2fc499SJeff Garzik * 155b2fc499SJeff Garzik * You should have received a copy of the GNU General Public License 165b2fc499SJeff Garzik * along with this program; if not, write to the Free Software 175b2fc499SJeff Garzik * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 185b2fc499SJeff Garzik */ 195b2fc499SJeff Garzik #include <linux/module.h> 205b2fc499SJeff Garzik #include <linux/init.h> 215b2fc499SJeff Garzik #include <linux/netdevice.h> 225b2fc499SJeff Garzik #include <linux/etherdevice.h> 235b2fc499SJeff Garzik #include <linux/ethtool.h> 245b2fc499SJeff Garzik #include <linux/workqueue.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 265b2fc499SJeff Garzik #include <linux/mii.h> 275b2fc499SJeff Garzik #include <linux/usb.h> 285b2fc499SJeff Garzik #include <linux/usb/cdc.h> 293692e94fSJussi Kivilinna #include <linux/usb/usbnet.h> 303692e94fSJussi Kivilinna #include <linux/usb/rndis_host.h> 315b2fc499SJeff Garzik 325b2fc499SJeff Garzik 335b2fc499SJeff Garzik /* 345b2fc499SJeff Garzik * RNDIS is NDIS remoted over USB. It's a MSFT variant of CDC ACM ... of 355b2fc499SJeff Garzik * course ACM was intended for modems, not Ethernet links! USB's standard 365b2fc499SJeff Garzik * for Ethernet links is "CDC Ethernet", which is significantly simpler. 375b2fc499SJeff Garzik * 385b2fc499SJeff Garzik * NOTE that Microsoft's "RNDIS 1.0" specification is incomplete. Issues 395b2fc499SJeff Garzik * include: 405b2fc499SJeff Garzik * - Power management in particular relies on information that's scattered 415b2fc499SJeff Garzik * through other documentation, and which is incomplete or incorrect even 425b2fc499SJeff Garzik * there. 435b2fc499SJeff Garzik * - There are various undocumented protocol requirements, such as the 445b2fc499SJeff Garzik * need to send unused garbage in control-OUT messages. 455b2fc499SJeff Garzik * - In some cases, MS-Windows will emit undocumented requests; this 465b2fc499SJeff Garzik * matters more to peripheral implementations than host ones. 475b2fc499SJeff Garzik * 485b2fc499SJeff Garzik * Moreover there's a no-open-specs variant of RNDIS called "ActiveSync". 495b2fc499SJeff Garzik * 505b2fc499SJeff Garzik * For these reasons and others, ** USE OF RNDIS IS STRONGLY DISCOURAGED ** in 515b2fc499SJeff Garzik * favor of such non-proprietary alternatives as CDC Ethernet or the newer (and 525b2fc499SJeff Garzik * currently rare) "Ethernet Emulation Model" (EEM). 535b2fc499SJeff Garzik */ 545b2fc499SJeff Garzik 555b2fc499SJeff Garzik /* 565b2fc499SJeff Garzik * RNDIS notifications from device: command completion; "reverse" 575b2fc499SJeff Garzik * keepalives; etc 585b2fc499SJeff Garzik */ 595665998cSJussi Kivilinna void rndis_status(struct usbnet *dev, struct urb *urb) 605b2fc499SJeff Garzik { 6160b86755SJoe Perches netdev_dbg(dev->net, "rndis status urb, len %d stat %d\n", 625b2fc499SJeff Garzik urb->actual_length, urb->status); 635b2fc499SJeff Garzik // FIXME for keepalives, respond immediately (asynchronously) 645b2fc499SJeff Garzik // if not an RNDIS status, do like cdc_status(dev,urb) does 655b2fc499SJeff Garzik } 665665998cSJussi Kivilinna EXPORT_SYMBOL_GPL(rndis_status); 675b2fc499SJeff Garzik 685b2fc499SJeff Garzik /* 692a4901bcSJussi Kivilinna * RNDIS indicate messages. 702a4901bcSJussi Kivilinna */ 712a4901bcSJussi Kivilinna static void rndis_msg_indicate(struct usbnet *dev, struct rndis_indicate *msg, 722a4901bcSJussi Kivilinna int buflen) 732a4901bcSJussi Kivilinna { 742a4901bcSJussi Kivilinna struct cdc_state *info = (void *)&dev->data; 752a4901bcSJussi Kivilinna struct device *udev = &info->control->dev; 762a4901bcSJussi Kivilinna 772a4901bcSJussi Kivilinna if (dev->driver_info->indication) { 782a4901bcSJussi Kivilinna dev->driver_info->indication(dev, msg, buflen); 792a4901bcSJussi Kivilinna } else { 802a4901bcSJussi Kivilinna switch (msg->status) { 817390e8b0SLinus Walleij case cpu_to_le32(RNDIS_STATUS_MEDIA_CONNECT): 822a4901bcSJussi Kivilinna dev_info(udev, "rndis media connect\n"); 832a4901bcSJussi Kivilinna break; 847390e8b0SLinus Walleij case cpu_to_le32(RNDIS_STATUS_MEDIA_DISCONNECT): 852a4901bcSJussi Kivilinna dev_info(udev, "rndis media disconnect\n"); 862a4901bcSJussi Kivilinna break; 872a4901bcSJussi Kivilinna default: 882a4901bcSJussi Kivilinna dev_info(udev, "rndis indication: 0x%08x\n", 892a4901bcSJussi Kivilinna le32_to_cpu(msg->status)); 902a4901bcSJussi Kivilinna } 912a4901bcSJussi Kivilinna } 922a4901bcSJussi Kivilinna } 932a4901bcSJussi Kivilinna 942a4901bcSJussi Kivilinna /* 955b2fc499SJeff Garzik * RPC done RNDIS-style. Caller guarantees: 965b2fc499SJeff Garzik * - message is properly byteswapped 975b2fc499SJeff Garzik * - there's no other request pending 985b2fc499SJeff Garzik * - buf can hold up to 1KB response (required by RNDIS spec) 995b2fc499SJeff Garzik * On return, the first few entries are already byteswapped. 1005b2fc499SJeff Garzik * 1015b2fc499SJeff Garzik * Call context is likely probe(), before interface name is known, 1025b2fc499SJeff Garzik * which is why we won't try to use it in the diagnostics. 1035b2fc499SJeff Garzik */ 104818727baSJussi Kivilinna int rndis_command(struct usbnet *dev, struct rndis_msg_hdr *buf, int buflen) 1055b2fc499SJeff Garzik { 1065b2fc499SJeff Garzik struct cdc_state *info = (void *) &dev->data; 1074d42d417SBen Hutchings struct usb_cdc_notification notification; 1085b2fc499SJeff Garzik int master_ifnum; 1095b2fc499SJeff Garzik int retval; 1104d42d417SBen Hutchings int partial; 1115b2fc499SJeff Garzik unsigned count; 1125b2fc499SJeff Garzik __le32 rsp; 1135b2fc499SJeff Garzik u32 xid = 0, msg_len, request_id; 1145b2fc499SJeff Garzik 1155b2fc499SJeff Garzik /* REVISIT when this gets called from contexts other than probe() or 1165b2fc499SJeff Garzik * disconnect(): either serialize, or dispatch responses on xid 1175b2fc499SJeff Garzik */ 1185b2fc499SJeff Garzik 1195b2fc499SJeff Garzik /* Issue the request; xid is unique, don't bother byteswapping it */ 1207390e8b0SLinus Walleij if (likely(buf->msg_type != cpu_to_le32(RNDIS_MSG_HALT) && 1217390e8b0SLinus Walleij buf->msg_type != cpu_to_le32(RNDIS_MSG_RESET))) { 1225b2fc499SJeff Garzik xid = dev->xid++; 1235b2fc499SJeff Garzik if (!xid) 1245b2fc499SJeff Garzik xid = dev->xid++; 1255b2fc499SJeff Garzik buf->request_id = (__force __le32) xid; 1265b2fc499SJeff Garzik } 1275b2fc499SJeff Garzik master_ifnum = info->control->cur_altsetting->desc.bInterfaceNumber; 1285b2fc499SJeff Garzik retval = usb_control_msg(dev->udev, 1295b2fc499SJeff Garzik usb_sndctrlpipe(dev->udev, 0), 1305b2fc499SJeff Garzik USB_CDC_SEND_ENCAPSULATED_COMMAND, 1315b2fc499SJeff Garzik USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1325b2fc499SJeff Garzik 0, master_ifnum, 1335b2fc499SJeff Garzik buf, le32_to_cpu(buf->msg_len), 1345b2fc499SJeff Garzik RNDIS_CONTROL_TIMEOUT_MS); 1355b2fc499SJeff Garzik if (unlikely(retval < 0 || xid == 0)) 1365b2fc499SJeff Garzik return retval; 1375b2fc499SJeff Garzik 1384d42d417SBen Hutchings /* Some devices don't respond on the control channel until 1394d42d417SBen Hutchings * polled on the status channel, so do that first. */ 1404d42d417SBen Hutchings if (dev->driver_info->data & RNDIS_DRIVER_DATA_POLL_STATUS) { 1414d42d417SBen Hutchings retval = usb_interrupt_msg( 1424d42d417SBen Hutchings dev->udev, 1434d42d417SBen Hutchings usb_rcvintpipe(dev->udev, 1444d42d417SBen Hutchings dev->status->desc.bEndpointAddress), 1454d42d417SBen Hutchings ¬ification, sizeof(notification), &partial, 1464d42d417SBen Hutchings RNDIS_CONTROL_TIMEOUT_MS); 1474d42d417SBen Hutchings if (unlikely(retval < 0)) 1484d42d417SBen Hutchings return retval; 1494d42d417SBen Hutchings } 1505b2fc499SJeff Garzik 1514d42d417SBen Hutchings /* Poll the control channel; the request probably completed immediately */ 1527390e8b0SLinus Walleij rsp = buf->msg_type | cpu_to_le32(RNDIS_MSG_COMPLETION); 1535b2fc499SJeff Garzik for (count = 0; count < 10; count++) { 1545b2fc499SJeff Garzik memset(buf, 0, CONTROL_BUFFER_SIZE); 1555b2fc499SJeff Garzik retval = usb_control_msg(dev->udev, 1565b2fc499SJeff Garzik usb_rcvctrlpipe(dev->udev, 0), 1575b2fc499SJeff Garzik USB_CDC_GET_ENCAPSULATED_RESPONSE, 1585b2fc499SJeff Garzik USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1595b2fc499SJeff Garzik 0, master_ifnum, 160818727baSJussi Kivilinna buf, buflen, 1615b2fc499SJeff Garzik RNDIS_CONTROL_TIMEOUT_MS); 1625b2fc499SJeff Garzik if (likely(retval >= 8)) { 1635b2fc499SJeff Garzik msg_len = le32_to_cpu(buf->msg_len); 1645b2fc499SJeff Garzik request_id = (__force u32) buf->request_id; 1655b2fc499SJeff Garzik if (likely(buf->msg_type == rsp)) { 1665b2fc499SJeff Garzik if (likely(request_id == xid)) { 1677390e8b0SLinus Walleij if (unlikely(rsp == 1687390e8b0SLinus Walleij cpu_to_le32(RNDIS_MSG_RESET_C))) 1695b2fc499SJeff Garzik return 0; 1707390e8b0SLinus Walleij if (likely(cpu_to_le32(RNDIS_STATUS_SUCCESS) 1715b2fc499SJeff Garzik == buf->status)) 1725b2fc499SJeff Garzik return 0; 1735b2fc499SJeff Garzik dev_dbg(&info->control->dev, 1745b2fc499SJeff Garzik "rndis reply status %08x\n", 1755b2fc499SJeff Garzik le32_to_cpu(buf->status)); 1765b2fc499SJeff Garzik return -EL3RST; 1775b2fc499SJeff Garzik } 1785b2fc499SJeff Garzik dev_dbg(&info->control->dev, 1795b2fc499SJeff Garzik "rndis reply id %d expected %d\n", 1805b2fc499SJeff Garzik request_id, xid); 1815b2fc499SJeff Garzik /* then likely retry */ 1825b2fc499SJeff Garzik } else switch (buf->msg_type) { 1837390e8b0SLinus Walleij case cpu_to_le32(RNDIS_MSG_INDICATE): /* fault/event */ 1842a4901bcSJussi Kivilinna rndis_msg_indicate(dev, (void *)buf, buflen); 1855b2fc499SJeff Garzik break; 1867390e8b0SLinus Walleij case cpu_to_le32(RNDIS_MSG_KEEPALIVE): { /* ping */ 1875b2fc499SJeff Garzik struct rndis_keepalive_c *msg = (void *)buf; 1885b2fc499SJeff Garzik 1897390e8b0SLinus Walleij msg->msg_type = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C); 19035c26c2cSHarvey Harrison msg->msg_len = cpu_to_le32(sizeof *msg); 1917390e8b0SLinus Walleij msg->status = cpu_to_le32(RNDIS_STATUS_SUCCESS); 1925b2fc499SJeff Garzik retval = usb_control_msg(dev->udev, 1935b2fc499SJeff Garzik usb_sndctrlpipe(dev->udev, 0), 1945b2fc499SJeff Garzik USB_CDC_SEND_ENCAPSULATED_COMMAND, 1955b2fc499SJeff Garzik USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1965b2fc499SJeff Garzik 0, master_ifnum, 1975b2fc499SJeff Garzik msg, sizeof *msg, 1985b2fc499SJeff Garzik RNDIS_CONTROL_TIMEOUT_MS); 1995b2fc499SJeff Garzik if (unlikely(retval < 0)) 2005b2fc499SJeff Garzik dev_dbg(&info->control->dev, 2015b2fc499SJeff Garzik "rndis keepalive err %d\n", 2025b2fc499SJeff Garzik retval); 2035b2fc499SJeff Garzik } 2045b2fc499SJeff Garzik break; 2055b2fc499SJeff Garzik default: 2065b2fc499SJeff Garzik dev_dbg(&info->control->dev, 2075b2fc499SJeff Garzik "unexpected rndis msg %08x len %d\n", 2085b2fc499SJeff Garzik le32_to_cpu(buf->msg_type), msg_len); 2095b2fc499SJeff Garzik } 2105b2fc499SJeff Garzik } else { 2115b2fc499SJeff Garzik /* device probably issued a protocol stall; ignore */ 2125b2fc499SJeff Garzik dev_dbg(&info->control->dev, 2135b2fc499SJeff Garzik "rndis response error, code %d\n", retval); 2145b2fc499SJeff Garzik } 21574ef5c50SPierre Ynard msleep(20); 2165b2fc499SJeff Garzik } 2175b2fc499SJeff Garzik dev_dbg(&info->control->dev, "rndis response timeout\n"); 2185b2fc499SJeff Garzik return -ETIMEDOUT; 2195b2fc499SJeff Garzik } 2205665998cSJussi Kivilinna EXPORT_SYMBOL_GPL(rndis_command); 2215b2fc499SJeff Garzik 2225b2fc499SJeff Garzik /* 2235b2fc499SJeff Garzik * rndis_query: 2245b2fc499SJeff Garzik * 2255b2fc499SJeff Garzik * Performs a query for @oid along with 0 or more bytes of payload as 2265b2fc499SJeff Garzik * specified by @in_len. If @reply_len is not set to -1 then the reply 2275b2fc499SJeff Garzik * length is checked against this value, resulting in an error if it 2285b2fc499SJeff Garzik * doesn't match. 2295b2fc499SJeff Garzik * 2305b2fc499SJeff Garzik * NOTE: Adding a payload exactly or greater than the size of the expected 2315b2fc499SJeff Garzik * response payload is an evident requirement MSFT added for ActiveSync. 2325b2fc499SJeff Garzik * 2335b2fc499SJeff Garzik * The only exception is for OIDs that return a variably sized response, 2345b2fc499SJeff Garzik * in which case no payload should be added. This undocumented (and 2355b2fc499SJeff Garzik * nonsensical!) issue was found by sniffing protocol requests from the 2365b2fc499SJeff Garzik * ActiveSync 4.1 Windows driver. 2375b2fc499SJeff Garzik */ 2385b2fc499SJeff Garzik static int rndis_query(struct usbnet *dev, struct usb_interface *intf, 239eca1ad82SAl Viro void *buf, __le32 oid, u32 in_len, 2405b2fc499SJeff Garzik void **reply, int *reply_len) 2415b2fc499SJeff Garzik { 2425b2fc499SJeff Garzik int retval; 2435b2fc499SJeff Garzik union { 2445b2fc499SJeff Garzik void *buf; 2455b2fc499SJeff Garzik struct rndis_msg_hdr *header; 2465b2fc499SJeff Garzik struct rndis_query *get; 2475b2fc499SJeff Garzik struct rndis_query_c *get_c; 2485b2fc499SJeff Garzik } u; 2495b2fc499SJeff Garzik u32 off, len; 2505b2fc499SJeff Garzik 2515b2fc499SJeff Garzik u.buf = buf; 2525b2fc499SJeff Garzik 2535b2fc499SJeff Garzik memset(u.get, 0, sizeof *u.get + in_len); 2547390e8b0SLinus Walleij u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY); 2555b2fc499SJeff Garzik u.get->msg_len = cpu_to_le32(sizeof *u.get + in_len); 2565b2fc499SJeff Garzik u.get->oid = oid; 2575b2fc499SJeff Garzik u.get->len = cpu_to_le32(in_len); 25835c26c2cSHarvey Harrison u.get->offset = cpu_to_le32(20); 2595b2fc499SJeff Garzik 260818727baSJussi Kivilinna retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE); 2615b2fc499SJeff Garzik if (unlikely(retval < 0)) { 2625b2fc499SJeff Garzik dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) failed, %d\n", 2635b2fc499SJeff Garzik oid, retval); 2645b2fc499SJeff Garzik return retval; 2655b2fc499SJeff Garzik } 2665b2fc499SJeff Garzik 2675b2fc499SJeff Garzik off = le32_to_cpu(u.get_c->offset); 2685b2fc499SJeff Garzik len = le32_to_cpu(u.get_c->len); 2695b2fc499SJeff Garzik if (unlikely((8 + off + len) > CONTROL_BUFFER_SIZE)) 2705b2fc499SJeff Garzik goto response_error; 2715b2fc499SJeff Garzik 2725b2fc499SJeff Garzik if (*reply_len != -1 && len != *reply_len) 2735b2fc499SJeff Garzik goto response_error; 2745b2fc499SJeff Garzik 2755b2fc499SJeff Garzik *reply = (unsigned char *) &u.get_c->request_id + off; 2765b2fc499SJeff Garzik *reply_len = len; 2775b2fc499SJeff Garzik 2785b2fc499SJeff Garzik return retval; 2795b2fc499SJeff Garzik 2805b2fc499SJeff Garzik response_error: 2815b2fc499SJeff Garzik dev_err(&intf->dev, "RNDIS_MSG_QUERY(0x%08x) " 2825b2fc499SJeff Garzik "invalid response - off %d len %d\n", 2835b2fc499SJeff Garzik oid, off, len); 2845b2fc499SJeff Garzik return -EDOM; 2855b2fc499SJeff Garzik } 2865b2fc499SJeff Garzik 2870f2166dfSStephen Hemminger /* same as usbnet_netdev_ops but MTU change not allowed */ 2880f2166dfSStephen Hemminger static const struct net_device_ops rndis_netdev_ops = { 2890f2166dfSStephen Hemminger .ndo_open = usbnet_open, 2900f2166dfSStephen Hemminger .ndo_stop = usbnet_stop, 2910f2166dfSStephen Hemminger .ndo_start_xmit = usbnet_start_xmit, 2920f2166dfSStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout, 2930f2166dfSStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 2940f2166dfSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 2950f2166dfSStephen Hemminger }; 2960f2166dfSStephen Hemminger 297039ee17dSJussi Kivilinna int 298039ee17dSJussi Kivilinna generic_rndis_bind(struct usbnet *dev, struct usb_interface *intf, int flags) 2995b2fc499SJeff Garzik { 3005b2fc499SJeff Garzik int retval; 3015b2fc499SJeff Garzik struct net_device *net = dev->net; 3025b2fc499SJeff Garzik struct cdc_state *info = (void *) &dev->data; 3035b2fc499SJeff Garzik union { 3045b2fc499SJeff Garzik void *buf; 3055b2fc499SJeff Garzik struct rndis_msg_hdr *header; 3065b2fc499SJeff Garzik struct rndis_init *init; 3075b2fc499SJeff Garzik struct rndis_init_c *init_c; 3085b2fc499SJeff Garzik struct rndis_query *get; 3095b2fc499SJeff Garzik struct rndis_query_c *get_c; 3105b2fc499SJeff Garzik struct rndis_set *set; 3115b2fc499SJeff Garzik struct rndis_set_c *set_c; 3129ff55874SJussi Kivilinna struct rndis_halt *halt; 3135b2fc499SJeff Garzik } u; 314d63ddcecSAl Viro u32 tmp; 315d63ddcecSAl Viro __le32 phym_unspec, *phym; 3165b2fc499SJeff Garzik int reply_len; 3175b2fc499SJeff Garzik unsigned char *bp; 3185b2fc499SJeff Garzik 3195b2fc499SJeff Garzik /* we can't rely on i/o from stack working, or stack allocation */ 3205b2fc499SJeff Garzik u.buf = kmalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL); 3215b2fc499SJeff Garzik if (!u.buf) 3225b2fc499SJeff Garzik return -ENOMEM; 3235b2fc499SJeff Garzik retval = usbnet_generic_cdc_bind(dev, intf); 3245b2fc499SJeff Garzik if (retval < 0) 3255b2fc499SJeff Garzik goto fail; 3265b2fc499SJeff Garzik 3277390e8b0SLinus Walleij u.init->msg_type = cpu_to_le32(RNDIS_MSG_INIT); 32835c26c2cSHarvey Harrison u.init->msg_len = cpu_to_le32(sizeof *u.init); 32935c26c2cSHarvey Harrison u.init->major_version = cpu_to_le32(1); 33035c26c2cSHarvey Harrison u.init->minor_version = cpu_to_le32(0); 3315b2fc499SJeff Garzik 3325b2fc499SJeff Garzik /* max transfer (in spec) is 0x4000 at full speed, but for 3335b2fc499SJeff Garzik * TX we'll stick to one Ethernet packet plus RNDIS framing. 3345b2fc499SJeff Garzik * For RX we handle drivers that zero-pad to end-of-packet. 3355b2fc499SJeff Garzik * Don't let userspace change these settings. 3365b2fc499SJeff Garzik * 3375b2fc499SJeff Garzik * NOTE: there still seems to be wierdness here, as if we need 3385b2fc499SJeff Garzik * to do some more things to make sure WinCE targets accept this. 3395b2fc499SJeff Garzik * They default to jumbograms of 8KB or 16KB, which is absurd 3405b2fc499SJeff Garzik * for such low data rates and which is also more than Linux 3415b2fc499SJeff Garzik * can usually expect to allocate for SKB data... 3425b2fc499SJeff Garzik */ 3435b2fc499SJeff Garzik net->hard_header_len += sizeof (struct rndis_data_hdr); 3445b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 3455b2fc499SJeff Garzik 34610d0f27cSJean-Christophe Dubois dev->maxpacket = usb_maxpacket(dev->udev, dev->out, 1); 34710d0f27cSJean-Christophe Dubois if (dev->maxpacket == 0) { 348a475f603SJoe Perches netif_dbg(dev, probe, dev->net, 349a475f603SJoe Perches "dev->maxpacket can't be 0\n"); 35010d0f27cSJean-Christophe Dubois retval = -EINVAL; 35110d0f27cSJean-Christophe Dubois goto fail_and_release; 35210d0f27cSJean-Christophe Dubois } 35310d0f27cSJean-Christophe Dubois 3545b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu + (dev->maxpacket + 1); 3555b2fc499SJeff Garzik dev->rx_urb_size &= ~(dev->maxpacket - 1); 3565b2fc499SJeff Garzik u.init->max_transfer_size = cpu_to_le32(dev->rx_urb_size); 3575b2fc499SJeff Garzik 3580f2166dfSStephen Hemminger net->netdev_ops = &rndis_netdev_ops; 3590f2166dfSStephen Hemminger 360818727baSJussi Kivilinna retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE); 3615b2fc499SJeff Garzik if (unlikely(retval < 0)) { 3625b2fc499SJeff Garzik /* it might not even be an RNDIS device!! */ 3635b2fc499SJeff Garzik dev_err(&intf->dev, "RNDIS init failed, %d\n", retval); 3645b2fc499SJeff Garzik goto fail_and_release; 3655b2fc499SJeff Garzik } 3665b2fc499SJeff Garzik tmp = le32_to_cpu(u.init_c->max_transfer_size); 3675b2fc499SJeff Garzik if (tmp < dev->hard_mtu) { 368500d2c2fSThomas Sailer if (tmp <= net->hard_header_len) { 3695b2fc499SJeff Garzik dev_err(&intf->dev, 3705b2fc499SJeff Garzik "dev can't take %u byte packets (max %u)\n", 3715b2fc499SJeff Garzik dev->hard_mtu, tmp); 3724149b72eSDavid Brownell retval = -EINVAL; 3739ff55874SJussi Kivilinna goto halt_fail_and_release; 3745b2fc499SJeff Garzik } 375500d2c2fSThomas Sailer dev_warn(&intf->dev, 376500d2c2fSThomas Sailer "dev can't take %u byte packets (max %u), " 377500d2c2fSThomas Sailer "adjusting MTU to %u\n", 37840ac7b62SGeorge Nassar dev->hard_mtu, tmp, tmp - net->hard_header_len); 37940ac7b62SGeorge Nassar dev->hard_mtu = tmp; 38040ac7b62SGeorge Nassar net->mtu = dev->hard_mtu - net->hard_header_len; 381500d2c2fSThomas Sailer } 3825b2fc499SJeff Garzik 3835b2fc499SJeff Garzik /* REVISIT: peripheral "alignment" request is ignored ... */ 3845b2fc499SJeff Garzik dev_dbg(&intf->dev, 3855b2fc499SJeff Garzik "hard mtu %u (%u from dev), rx buflen %Zu, align %d\n", 3865b2fc499SJeff Garzik dev->hard_mtu, tmp, dev->rx_urb_size, 3875b2fc499SJeff Garzik 1 << le32_to_cpu(u.init_c->packet_alignment)); 3885b2fc499SJeff Garzik 3897c39e038SJussi Kivilinna /* module has some device initialization code needs to be done right 3907c39e038SJussi Kivilinna * after RNDIS_INIT */ 3917c39e038SJussi Kivilinna if (dev->driver_info->early_init && 3927c39e038SJussi Kivilinna dev->driver_info->early_init(dev) != 0) 3937c39e038SJussi Kivilinna goto halt_fail_and_release; 3947c39e038SJussi Kivilinna 395039ee17dSJussi Kivilinna /* Check physical medium */ 3969f5e60ddSJussi Kivilinna phym = NULL; 397039ee17dSJussi Kivilinna reply_len = sizeof *phym; 3987390e8b0SLinus Walleij retval = rndis_query(dev, intf, u.buf, 399*8cdddc3fSLinus Walleij cpu_to_le32(RNDIS_OID_GEN_PHYSICAL_MEDIUM), 400039ee17dSJussi Kivilinna 0, (void **) &phym, &reply_len); 4019f5e60ddSJussi Kivilinna if (retval != 0 || !phym) { 402039ee17dSJussi Kivilinna /* OID is optional so don't fail here. */ 4037390e8b0SLinus Walleij phym_unspec = cpu_to_le32(RNDIS_PHYSICAL_MEDIUM_UNSPECIFIED); 4049f5e60ddSJussi Kivilinna phym = &phym_unspec; 4059f5e60ddSJussi Kivilinna } 406039ee17dSJussi Kivilinna if ((flags & FLAG_RNDIS_PHYM_WIRELESS) && 4077390e8b0SLinus Walleij *phym != cpu_to_le32(RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN)) { 408a475f603SJoe Perches netif_dbg(dev, probe, dev->net, 409a475f603SJoe Perches "driver requires wireless physical medium, but device is not\n"); 410039ee17dSJussi Kivilinna retval = -ENODEV; 411039ee17dSJussi Kivilinna goto halt_fail_and_release; 412039ee17dSJussi Kivilinna } 413039ee17dSJussi Kivilinna if ((flags & FLAG_RNDIS_PHYM_NOT_WIRELESS) && 4147390e8b0SLinus Walleij *phym == cpu_to_le32(RNDIS_PHYSICAL_MEDIUM_WIRELESS_LAN)) { 415a475f603SJoe Perches netif_dbg(dev, probe, dev->net, 416a475f603SJoe Perches "driver requires non-wireless physical medium, but device is wireless.\n"); 417039ee17dSJussi Kivilinna retval = -ENODEV; 418039ee17dSJussi Kivilinna goto halt_fail_and_release; 419039ee17dSJussi Kivilinna } 420039ee17dSJussi Kivilinna 4215b2fc499SJeff Garzik /* Get designated host ethernet address */ 4225b2fc499SJeff Garzik reply_len = ETH_ALEN; 4237390e8b0SLinus Walleij retval = rndis_query(dev, intf, u.buf, 424*8cdddc3fSLinus Walleij cpu_to_le32(RNDIS_OID_802_3_PERMANENT_ADDRESS), 4255b2fc499SJeff Garzik 48, (void **) &bp, &reply_len); 4265b2fc499SJeff Garzik if (unlikely(retval< 0)) { 4275b2fc499SJeff Garzik dev_err(&intf->dev, "rndis get ethaddr, %d\n", retval); 4289ff55874SJussi Kivilinna goto halt_fail_and_release; 4295b2fc499SJeff Garzik } 4305b2fc499SJeff Garzik memcpy(net->dev_addr, bp, ETH_ALEN); 431083925d5SJohn W. Linville memcpy(net->perm_addr, bp, ETH_ALEN); 4325b2fc499SJeff Garzik 4335b2fc499SJeff Garzik /* set a nonzero filter to enable data transfers */ 4345b2fc499SJeff Garzik memset(u.set, 0, sizeof *u.set); 4357390e8b0SLinus Walleij u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET); 43635c26c2cSHarvey Harrison u.set->msg_len = cpu_to_le32(4 + sizeof *u.set); 437*8cdddc3fSLinus Walleij u.set->oid = cpu_to_le32(RNDIS_OID_GEN_CURRENT_PACKET_FILTER); 43835c26c2cSHarvey Harrison u.set->len = cpu_to_le32(4); 43935c26c2cSHarvey Harrison u.set->offset = cpu_to_le32((sizeof *u.set) - 8); 4407390e8b0SLinus Walleij *(__le32 *)(u.buf + sizeof *u.set) = cpu_to_le32(RNDIS_DEFAULT_FILTER); 4415b2fc499SJeff Garzik 442818727baSJussi Kivilinna retval = rndis_command(dev, u.header, CONTROL_BUFFER_SIZE); 4435b2fc499SJeff Garzik if (unlikely(retval < 0)) { 4445b2fc499SJeff Garzik dev_err(&intf->dev, "rndis set packet filter, %d\n", retval); 4459ff55874SJussi Kivilinna goto halt_fail_and_release; 4465b2fc499SJeff Garzik } 4475b2fc499SJeff Garzik 4485b2fc499SJeff Garzik retval = 0; 4495b2fc499SJeff Garzik 4505b2fc499SJeff Garzik kfree(u.buf); 4515b2fc499SJeff Garzik return retval; 4525b2fc499SJeff Garzik 4539ff55874SJussi Kivilinna halt_fail_and_release: 4549ff55874SJussi Kivilinna memset(u.halt, 0, sizeof *u.halt); 4557390e8b0SLinus Walleij u.halt->msg_type = cpu_to_le32(RNDIS_MSG_HALT); 45635c26c2cSHarvey Harrison u.halt->msg_len = cpu_to_le32(sizeof *u.halt); 457818727baSJussi Kivilinna (void) rndis_command(dev, (void *)u.halt, CONTROL_BUFFER_SIZE); 4585b2fc499SJeff Garzik fail_and_release: 4595b2fc499SJeff Garzik usb_set_intfdata(info->data, NULL); 4605b2fc499SJeff Garzik usb_driver_release_interface(driver_of(intf), info->data); 4615b2fc499SJeff Garzik info->data = NULL; 4625b2fc499SJeff Garzik fail: 4635b2fc499SJeff Garzik kfree(u.buf); 4645b2fc499SJeff Garzik return retval; 4655b2fc499SJeff Garzik } 4665665998cSJussi Kivilinna EXPORT_SYMBOL_GPL(generic_rndis_bind); 4675b2fc499SJeff Garzik 468039ee17dSJussi Kivilinna static int rndis_bind(struct usbnet *dev, struct usb_interface *intf) 469039ee17dSJussi Kivilinna { 470039ee17dSJussi Kivilinna return generic_rndis_bind(dev, intf, FLAG_RNDIS_PHYM_NOT_WIRELESS); 471039ee17dSJussi Kivilinna } 472039ee17dSJussi Kivilinna 4735665998cSJussi Kivilinna void rndis_unbind(struct usbnet *dev, struct usb_interface *intf) 4745b2fc499SJeff Garzik { 4755b2fc499SJeff Garzik struct rndis_halt *halt; 4765b2fc499SJeff Garzik 4775b2fc499SJeff Garzik /* try to clear any rndis state/activity (no i/o from stack!) */ 47804c3c01aSJussi Kivilinna halt = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL); 4795b2fc499SJeff Garzik if (halt) { 4807390e8b0SLinus Walleij halt->msg_type = cpu_to_le32(RNDIS_MSG_HALT); 48135c26c2cSHarvey Harrison halt->msg_len = cpu_to_le32(sizeof *halt); 482818727baSJussi Kivilinna (void) rndis_command(dev, (void *)halt, CONTROL_BUFFER_SIZE); 4835b2fc499SJeff Garzik kfree(halt); 4845b2fc499SJeff Garzik } 4855b2fc499SJeff Garzik 4862bfa2e1fSBjorge Dijkstra usbnet_cdc_unbind(dev, intf); 4875b2fc499SJeff Garzik } 4885665998cSJussi Kivilinna EXPORT_SYMBOL_GPL(rndis_unbind); 4895b2fc499SJeff Garzik 4905b2fc499SJeff Garzik /* 4915b2fc499SJeff Garzik * DATA -- host must not write zlps 4925b2fc499SJeff Garzik */ 4935665998cSJussi Kivilinna int rndis_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 4945b2fc499SJeff Garzik { 4955b2fc499SJeff Garzik /* peripheral may have batched packets to us... */ 4965b2fc499SJeff Garzik while (likely(skb->len)) { 4975b2fc499SJeff Garzik struct rndis_data_hdr *hdr = (void *)skb->data; 4985b2fc499SJeff Garzik struct sk_buff *skb2; 4995b2fc499SJeff Garzik u32 msg_len, data_offset, data_len; 5005b2fc499SJeff Garzik 5015b2fc499SJeff Garzik msg_len = le32_to_cpu(hdr->msg_len); 5025b2fc499SJeff Garzik data_offset = le32_to_cpu(hdr->data_offset); 5035b2fc499SJeff Garzik data_len = le32_to_cpu(hdr->data_len); 5045b2fc499SJeff Garzik 5055b2fc499SJeff Garzik /* don't choke if we see oob, per-packet data, etc */ 5067390e8b0SLinus Walleij if (unlikely(hdr->msg_type != cpu_to_le32(RNDIS_MSG_PACKET) || 5078e95a202SJoe Perches skb->len < msg_len || 5088e95a202SJoe Perches (data_offset + data_len + 8) > msg_len)) { 50958e2e7d5SHerbert Xu dev->net->stats.rx_frame_errors++; 51060b86755SJoe Perches netdev_dbg(dev->net, "bad rndis message %d/%d/%d/%d, len %d\n", 5115b2fc499SJeff Garzik le32_to_cpu(hdr->msg_type), 5125b2fc499SJeff Garzik msg_len, data_offset, data_len, skb->len); 5135b2fc499SJeff Garzik return 0; 5145b2fc499SJeff Garzik } 5155b2fc499SJeff Garzik skb_pull(skb, 8 + data_offset); 5165b2fc499SJeff Garzik 5175b2fc499SJeff Garzik /* at most one packet left? */ 5185b2fc499SJeff Garzik if (likely((data_len - skb->len) <= sizeof *hdr)) { 5195b2fc499SJeff Garzik skb_trim(skb, data_len); 5205b2fc499SJeff Garzik break; 5215b2fc499SJeff Garzik } 5225b2fc499SJeff Garzik 5235b2fc499SJeff Garzik /* try to return all the packets in the batch */ 5245b2fc499SJeff Garzik skb2 = skb_clone(skb, GFP_ATOMIC); 5255b2fc499SJeff Garzik if (unlikely(!skb2)) 5265b2fc499SJeff Garzik break; 5275b2fc499SJeff Garzik skb_pull(skb, msg_len - sizeof *hdr); 5285b2fc499SJeff Garzik skb_trim(skb2, data_len); 5295b2fc499SJeff Garzik usbnet_skb_return(dev, skb2); 5305b2fc499SJeff Garzik } 5315b2fc499SJeff Garzik 5325b2fc499SJeff Garzik /* caller will usbnet_skb_return the remaining packet */ 5335b2fc499SJeff Garzik return 1; 5345b2fc499SJeff Garzik } 5355665998cSJussi Kivilinna EXPORT_SYMBOL_GPL(rndis_rx_fixup); 5365b2fc499SJeff Garzik 5375665998cSJussi Kivilinna struct sk_buff * 5385b2fc499SJeff Garzik rndis_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags) 5395b2fc499SJeff Garzik { 5405b2fc499SJeff Garzik struct rndis_data_hdr *hdr; 5415b2fc499SJeff Garzik struct sk_buff *skb2; 5425b2fc499SJeff Garzik unsigned len = skb->len; 5435b2fc499SJeff Garzik 5445b2fc499SJeff Garzik if (likely(!skb_cloned(skb))) { 5455b2fc499SJeff Garzik int room = skb_headroom(skb); 5465b2fc499SJeff Garzik 5475b2fc499SJeff Garzik /* enough head room as-is? */ 5485b2fc499SJeff Garzik if (unlikely((sizeof *hdr) <= room)) 5495b2fc499SJeff Garzik goto fill; 5505b2fc499SJeff Garzik 5515b2fc499SJeff Garzik /* enough room, but needs to be readjusted? */ 5525b2fc499SJeff Garzik room += skb_tailroom(skb); 5535b2fc499SJeff Garzik if (likely((sizeof *hdr) <= room)) { 5545b2fc499SJeff Garzik skb->data = memmove(skb->head + sizeof *hdr, 5555b2fc499SJeff Garzik skb->data, len); 5565b2fc499SJeff Garzik skb_set_tail_pointer(skb, len); 5575b2fc499SJeff Garzik goto fill; 5585b2fc499SJeff Garzik } 5595b2fc499SJeff Garzik } 5605b2fc499SJeff Garzik 5615b2fc499SJeff Garzik /* create a new skb, with the correct size (and tailpad) */ 5625b2fc499SJeff Garzik skb2 = skb_copy_expand(skb, sizeof *hdr, 1, flags); 5635b2fc499SJeff Garzik dev_kfree_skb_any(skb); 5645b2fc499SJeff Garzik if (unlikely(!skb2)) 5655b2fc499SJeff Garzik return skb2; 5665b2fc499SJeff Garzik skb = skb2; 5675b2fc499SJeff Garzik 5685b2fc499SJeff Garzik /* fill out the RNDIS header. we won't bother trying to batch 5695b2fc499SJeff Garzik * packets; Linux minimizes wasted bandwidth through tx queues. 5705b2fc499SJeff Garzik */ 5715b2fc499SJeff Garzik fill: 5725b2fc499SJeff Garzik hdr = (void *) __skb_push(skb, sizeof *hdr); 5735b2fc499SJeff Garzik memset(hdr, 0, sizeof *hdr); 5747390e8b0SLinus Walleij hdr->msg_type = cpu_to_le32(RNDIS_MSG_PACKET); 5755b2fc499SJeff Garzik hdr->msg_len = cpu_to_le32(skb->len); 57635c26c2cSHarvey Harrison hdr->data_offset = cpu_to_le32(sizeof(*hdr) - 8); 5775b2fc499SJeff Garzik hdr->data_len = cpu_to_le32(len); 5785b2fc499SJeff Garzik 5795b2fc499SJeff Garzik /* FIXME make the last packet always be short ... */ 5805b2fc499SJeff Garzik return skb; 5815b2fc499SJeff Garzik } 5825665998cSJussi Kivilinna EXPORT_SYMBOL_GPL(rndis_tx_fixup); 5835b2fc499SJeff Garzik 5845b2fc499SJeff Garzik 5855b2fc499SJeff Garzik static const struct driver_info rndis_info = { 5865b2fc499SJeff Garzik .description = "RNDIS device", 587c261344dSArnd Bergmann .flags = FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT, 588039ee17dSJussi Kivilinna .bind = rndis_bind, 5895b2fc499SJeff Garzik .unbind = rndis_unbind, 5905b2fc499SJeff Garzik .status = rndis_status, 5915b2fc499SJeff Garzik .rx_fixup = rndis_rx_fixup, 5925b2fc499SJeff Garzik .tx_fixup = rndis_tx_fixup, 5935b2fc499SJeff Garzik }; 5945b2fc499SJeff Garzik 5954d42d417SBen Hutchings static const struct driver_info rndis_poll_status_info = { 5964d42d417SBen Hutchings .description = "RNDIS device (poll status before control)", 597eb8aa72dSBen Hutchings .flags = FLAG_ETHER | FLAG_POINTTOPOINT | FLAG_FRAMING_RN | FLAG_NO_SETINT, 5984d42d417SBen Hutchings .data = RNDIS_DRIVER_DATA_POLL_STATUS, 5994d42d417SBen Hutchings .bind = rndis_bind, 6004d42d417SBen Hutchings .unbind = rndis_unbind, 6014d42d417SBen Hutchings .status = rndis_status, 6024d42d417SBen Hutchings .rx_fixup = rndis_rx_fixup, 6034d42d417SBen Hutchings .tx_fixup = rndis_tx_fixup, 6044d42d417SBen Hutchings }; 6054d42d417SBen Hutchings 6065b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 6075b2fc499SJeff Garzik 6085b2fc499SJeff Garzik static const struct usb_device_id products [] = { 6095b2fc499SJeff Garzik { 6104d42d417SBen Hutchings /* 2Wire HomePortal 1000SW */ 6114d42d417SBen Hutchings USB_DEVICE_AND_INTERFACE_INFO(0x1630, 0x0042, 6124d42d417SBen Hutchings USB_CLASS_COMM, 2 /* ACM */, 0x0ff), 6134d42d417SBen Hutchings .driver_info = (unsigned long) &rndis_poll_status_info, 6144d42d417SBen Hutchings }, { 6155b2fc499SJeff Garzik /* RNDIS is MSFT's un-official variant of CDC ACM */ 6165b2fc499SJeff Garzik USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff), 6175b2fc499SJeff Garzik .driver_info = (unsigned long) &rndis_info, 6185b2fc499SJeff Garzik }, { 6195b2fc499SJeff Garzik /* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */ 6205b2fc499SJeff Garzik USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1), 6214d42d417SBen Hutchings .driver_info = (unsigned long) &rndis_poll_status_info, 6227e99eeddSThomas Backlund }, { 6237e99eeddSThomas Backlund /* RNDIS for tethering */ 6247e99eeddSThomas Backlund USB_INTERFACE_INFO(USB_CLASS_WIRELESS_CONTROLLER, 1, 3), 6257e99eeddSThomas Backlund .driver_info = (unsigned long) &rndis_info, 6265b2fc499SJeff Garzik }, 6275b2fc499SJeff Garzik { }, // END 6285b2fc499SJeff Garzik }; 6295b2fc499SJeff Garzik MODULE_DEVICE_TABLE(usb, products); 6305b2fc499SJeff Garzik 6315b2fc499SJeff Garzik static struct usb_driver rndis_driver = { 6325b2fc499SJeff Garzik .name = "rndis_host", 6335b2fc499SJeff Garzik .id_table = products, 6345b2fc499SJeff Garzik .probe = usbnet_probe, 6355b2fc499SJeff Garzik .disconnect = usbnet_disconnect, 6365b2fc499SJeff Garzik .suspend = usbnet_suspend, 6375b2fc499SJeff Garzik .resume = usbnet_resume, 6385b2fc499SJeff Garzik }; 6395b2fc499SJeff Garzik 640d632eb1bSGreg Kroah-Hartman module_usb_driver(rndis_driver); 6415b2fc499SJeff Garzik 6425b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell"); 6435b2fc499SJeff Garzik MODULE_DESCRIPTION("USB Host side RNDIS driver"); 6445b2fc499SJeff Garzik MODULE_LICENSE("GPL"); 645