David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack |
| 3 | * |
| 4 | * Copyright (C) 2003-2005,2008 David Brownell |
| 5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 6 | * Copyright (C) 2008 Nokia Corporation |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* #define VERBOSE_DEBUG */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
Sebastian Andrzej Siewior | 9830317 | 2012-09-10 16:30:50 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/gfp.h> |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 19 | #include <linux/device.h> |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/etherdevice.h> |
| 22 | #include <linux/ethtool.h> |
Ian Coolidge | 4fe5f07 | 2012-11-07 14:39:18 +0000 | [diff] [blame] | 23 | #include <linux/if_vlan.h> |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 24 | #include <linux/if_arp.h> |
| 25 | #include <linux/msm_rmnet.h> |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/seq_file.h> |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 28 | |
| 29 | #include "u_ether.h" |
| 30 | |
| 31 | |
| 32 | /* |
| 33 | * This component encapsulates the Ethernet link glue needed to provide |
| 34 | * one (!) network link through the USB gadget stack, normally "usb0". |
| 35 | * |
| 36 | * The control and data models are handled by the function driver which |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 37 | * connects to this code; such as CDC Ethernet (ECM or EEM), |
| 38 | * "CDC Subset", or RNDIS. That includes all descriptor and endpoint |
| 39 | * management. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 40 | * |
| 41 | * Link level addressing is handled by this component using module |
| 42 | * parameters; if no such parameters are provided, random link level |
| 43 | * addresses are used. Each end of the link uses one address. The |
| 44 | * host end address is exported in various ways, and is often recorded |
| 45 | * in configuration databases. |
| 46 | * |
| 47 | * The driver which assembles each configuration using such a link is |
| 48 | * responsible for ensuring that each configuration includes at most one |
| 49 | * instance of is network link. (The network layer provides ways for |
| 50 | * this single "physical" link to be used by multiple virtual links.) |
| 51 | */ |
| 52 | |
David Brownell | 8a1ce2c | 2008-08-18 17:43:56 -0700 | [diff] [blame] | 53 | #define UETH__VERSION "29-May-2008" |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 54 | |
Mike Looijmans | bba787a | 2015-08-05 08:54:55 +0200 | [diff] [blame] | 55 | /* Experiments show that both Linux and Windows hosts allow up to 16k |
| 56 | * frame sizes. Set the max size to 15k+52 to prevent allocating 32k |
| 57 | * blocks and still have efficient handling. */ |
| 58 | #define GETHER_MAX_ETH_FRAME_LEN 15412 |
| 59 | |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 60 | static struct workqueue_struct *uether_wq; |
| 61 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 62 | struct eth_dev { |
| 63 | /* lock is held while accessing port_usb |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 64 | */ |
| 65 | spinlock_t lock; |
| 66 | struct gether *port_usb; |
| 67 | |
| 68 | struct net_device *net; |
| 69 | struct usb_gadget *gadget; |
| 70 | |
| 71 | spinlock_t req_lock; /* guard {rx,tx}_reqs */ |
| 72 | struct list_head tx_reqs, rx_reqs; |
Badhri Jagan Sridharan | cb6a786 | 2014-09-18 10:42:41 -0700 | [diff] [blame] | 73 | unsigned tx_qlen; |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 74 | /* Minimum number of TX USB request queued to UDC */ |
| 75 | #define TX_REQ_THRESHOLD 5 |
| 76 | int no_tx_req_used; |
| 77 | int tx_skb_hold_count; |
| 78 | u32 tx_req_bufsize; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 79 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 80 | struct sk_buff_head rx_frames; |
| 81 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 82 | unsigned qmult; |
| 83 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 84 | unsigned header_len; |
xerox_lin | 87bebf8 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 85 | unsigned ul_max_pkts_per_xfer; |
xerox_lin | cdffcb8 | 2014-09-04 16:01:59 +0800 | [diff] [blame] | 86 | unsigned dl_max_pkts_per_xfer; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 87 | struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb); |
| 88 | int (*unwrap)(struct gether *, |
| 89 | struct sk_buff *skb, |
| 90 | struct sk_buff_head *list); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 91 | |
| 92 | struct work_struct work; |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 93 | struct work_struct rx_work; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 94 | |
| 95 | unsigned long todo; |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 96 | unsigned long flags; |
| 97 | unsigned short rx_needed_headroom; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 98 | #define WORK_RX_MEMORY 0 |
| 99 | |
| 100 | bool zlp; |
Yoshihiro Shimoda | 05f6b0f | 2016-08-22 17:48:26 +0900 | [diff] [blame] | 101 | bool no_skb_reserve; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 102 | u8 host_mac[ETH_ALEN]; |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 103 | u8 dev_mac[ETH_ALEN]; |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 104 | unsigned long tx_throttle; |
| 105 | struct dentry *uether_dent; |
| 106 | struct dentry *uether_dfile; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 109 | static void uether_debugfs_init(struct eth_dev *dev); |
| 110 | static void uether_debugfs_exit(struct eth_dev *dev); |
| 111 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 112 | /*-------------------------------------------------------------------------*/ |
| 113 | |
| 114 | #define RX_EXTRA 20 /* bytes guarding against rx overflows */ |
| 115 | |
| 116 | #define DEFAULT_QLEN 2 /* double buffering by default */ |
| 117 | |
Vamsi Krishna | 11bdb3b | 2014-05-14 10:57:08 -0700 | [diff] [blame^] | 118 | /* |
| 119 | * Usually downlink rates are higher than uplink rates and it |
| 120 | * deserve higher number of requests. For CAT-6 data rates of |
| 121 | * 300Mbps (~30 packets per milli-sec) 40 usb request may not |
| 122 | * be sufficient. At this rate and with interrupt moderation |
| 123 | * of interconnect, data can be very bursty. tx_qmult is the |
| 124 | * additional multipler on qmult. |
| 125 | */ |
| 126 | static unsigned int tx_qmult = 1; |
| 127 | module_param(tx_qmult, uint, 0644); |
| 128 | MODULE_PARM_DESC(tx_qmult, "Additional queue length multiplier for tx"); |
| 129 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 130 | /* for dual-speed hardware, use deeper queues at high/super speed */ |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 131 | static inline int qlen(struct usb_gadget *gadget, unsigned qmult) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 132 | { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 133 | if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH || |
| 134 | gadget->speed == USB_SPEED_SUPER)) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 135 | return qmult * DEFAULT_QLEN; |
| 136 | else |
| 137 | return DEFAULT_QLEN; |
| 138 | } |
| 139 | |
| 140 | /*-------------------------------------------------------------------------*/ |
| 141 | |
| 142 | /* REVISIT there must be a better way than having two sets |
| 143 | * of debug calls ... |
| 144 | */ |
| 145 | |
| 146 | #undef DBG |
| 147 | #undef VDBG |
| 148 | #undef ERROR |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 149 | #undef INFO |
| 150 | |
| 151 | #define xprintk(d, level, fmt, args...) \ |
| 152 | printk(level "%s: " fmt , (d)->net->name , ## args) |
| 153 | |
| 154 | #ifdef DEBUG |
| 155 | #undef DEBUG |
| 156 | #define DBG(dev, fmt, args...) \ |
| 157 | xprintk(dev , KERN_DEBUG , fmt , ## args) |
| 158 | #else |
| 159 | #define DBG(dev, fmt, args...) \ |
| 160 | do { } while (0) |
| 161 | #endif /* DEBUG */ |
| 162 | |
| 163 | #ifdef VERBOSE_DEBUG |
| 164 | #define VDBG DBG |
| 165 | #else |
| 166 | #define VDBG(dev, fmt, args...) \ |
| 167 | do { } while (0) |
| 168 | #endif /* DEBUG */ |
| 169 | |
| 170 | #define ERROR(dev, fmt, args...) \ |
| 171 | xprintk(dev , KERN_ERR , fmt , ## args) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 172 | #define INFO(dev, fmt, args...) \ |
| 173 | xprintk(dev , KERN_INFO , fmt , ## args) |
| 174 | |
| 175 | /*-------------------------------------------------------------------------*/ |
| 176 | |
| 177 | /* NETWORK DRIVER HOOKUP (to the layer above this driver) */ |
| 178 | |
Stephen Hemminger | ccad637 | 2008-11-19 22:42:31 -0800 | [diff] [blame] | 179 | static int ueth_change_mtu(struct net_device *net, int new_mtu) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 180 | { |
Mike Looijmans | ab738ff | 2015-11-30 12:18:23 +0100 | [diff] [blame] | 181 | if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN) |
| 182 | return -ERANGE; |
| 183 | net->mtu = new_mtu; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 184 | |
Mike Looijmans | ab738ff | 2015-11-30 12:18:23 +0100 | [diff] [blame] | 185 | return 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 188 | static int ueth_change_mtu_ip(struct net_device *net, int new_mtu) |
| 189 | { |
| 190 | struct eth_dev *dev = netdev_priv(net); |
| 191 | unsigned long flags; |
| 192 | int status = 0; |
| 193 | |
| 194 | spin_lock_irqsave(&dev->lock, flags); |
| 195 | if (new_mtu <= 0) |
| 196 | status = -EINVAL; |
| 197 | else |
| 198 | net->mtu = new_mtu; |
| 199 | |
| 200 | DBG(dev, "[%s] MTU change: old=%d new=%d\n", net->name, |
| 201 | net->mtu, new_mtu); |
| 202 | spin_unlock_irqrestore(&dev->lock, flags); |
| 203 | |
| 204 | return status; |
| 205 | } |
| 206 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 207 | static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p) |
| 208 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 209 | struct eth_dev *dev = netdev_priv(net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 210 | |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 211 | strlcpy(p->driver, "g_ether", sizeof(p->driver)); |
| 212 | strlcpy(p->version, UETH__VERSION, sizeof(p->version)); |
| 213 | strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version)); |
| 214 | strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info)); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 215 | } |
| 216 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 217 | /* REVISIT can also support: |
| 218 | * - WOL (by tracking suspends and issuing remote wakeup) |
| 219 | * - msglevel (implies updated messaging) |
| 220 | * - ... probably more ethtool ops |
| 221 | */ |
| 222 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 223 | static const struct ethtool_ops ops = { |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 224 | .get_drvinfo = eth_get_drvinfo, |
Jonathan McDowell | 237e75b | 2009-03-26 00:45:27 -0700 | [diff] [blame] | 225 | .get_link = ethtool_op_get_link, |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | static void defer_kevent(struct eth_dev *dev, int flag) |
| 229 | { |
| 230 | if (test_and_set_bit(flag, &dev->todo)) |
| 231 | return; |
| 232 | if (!schedule_work(&dev->work)) |
| 233 | ERROR(dev, "kevent %d may have been dropped\n", flag); |
| 234 | else |
| 235 | DBG(dev, "kevent %d scheduled\n", flag); |
| 236 | } |
| 237 | |
| 238 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); |
| 239 | |
| 240 | static int |
| 241 | rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags) |
| 242 | { |
| 243 | struct sk_buff *skb; |
| 244 | int retval = -ENOMEM; |
| 245 | size_t size = 0; |
| 246 | struct usb_ep *out; |
| 247 | unsigned long flags; |
| 248 | |
| 249 | spin_lock_irqsave(&dev->lock, flags); |
| 250 | if (dev->port_usb) |
| 251 | out = dev->port_usb->out_ep; |
| 252 | else |
| 253 | out = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 254 | |
Saket Saurabh | 3cdaa36 | 2014-05-09 17:06:00 +0530 | [diff] [blame] | 255 | if (!out) { |
| 256 | spin_unlock_irqrestore(&dev->lock, flags); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 257 | return -ENOTCONN; |
Saket Saurabh | 3cdaa36 | 2014-05-09 17:06:00 +0530 | [diff] [blame] | 258 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 259 | |
| 260 | /* Padding up to RX_EXTRA handles minor disagreements with host. |
| 261 | * Normally we use the USB "terminate on short read" convention; |
| 262 | * so allow up to (N*maxpacket), since that memory is normally |
| 263 | * already allocated. Some hardware doesn't deal well with short |
| 264 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a |
| 265 | * byte off the end (to force hardware errors on overflow). |
| 266 | * |
| 267 | * RNDIS uses internal framing, and explicitly allows senders to |
| 268 | * pad to end-of-packet. That's potentially nice for speed, but |
| 269 | * means receivers can't recover lost synch on their own (because |
| 270 | * new packets don't only start after a short RX). |
| 271 | */ |
| 272 | size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA; |
| 273 | size += dev->port_usb->header_len; |
| 274 | size += out->maxpacket - 1; |
| 275 | size -= size % out->maxpacket; |
| 276 | |
xerox_lin | 87bebf8 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 277 | if (dev->ul_max_pkts_per_xfer) |
| 278 | size *= dev->ul_max_pkts_per_xfer; |
| 279 | |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 280 | if (dev->port_usb->is_fixed) |
Stephen Hemminger | 45d1b7a | 2011-03-01 22:40:57 -0800 | [diff] [blame] | 281 | size = max_t(size_t, size, dev->port_usb->fixed_out_len); |
Saket Saurabh | 3cdaa36 | 2014-05-09 17:06:00 +0530 | [diff] [blame] | 282 | spin_unlock_irqrestore(&dev->lock, flags); |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 283 | |
Amit Pundir | 5ff0eb2 | 2016-01-08 19:36:02 +0530 | [diff] [blame] | 284 | DBG(dev, "%s: size: %zd\n", __func__, size); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 285 | skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags); |
| 286 | if (skb == NULL) { |
| 287 | DBG(dev, "no rx skb\n"); |
| 288 | goto enomem; |
| 289 | } |
| 290 | |
| 291 | /* Some platforms perform better when IP packets are aligned, |
| 292 | * but on at least one, checksumming fails otherwise. Note: |
| 293 | * RNDIS headers involve variable numbers of LE32 values. |
| 294 | */ |
Yoshihiro Shimoda | 05f6b0f | 2016-08-22 17:48:26 +0900 | [diff] [blame] | 295 | if (likely(!dev->no_skb_reserve)) |
| 296 | skb_reserve(skb, NET_IP_ALIGN); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 297 | |
| 298 | req->buf = skb->data; |
| 299 | req->length = size; |
| 300 | req->complete = rx_complete; |
| 301 | req->context = skb; |
| 302 | |
| 303 | retval = usb_ep_queue(out, req, gfp_flags); |
| 304 | if (retval == -ENOMEM) |
| 305 | enomem: |
| 306 | defer_kevent(dev, WORK_RX_MEMORY); |
| 307 | if (retval) { |
| 308 | DBG(dev, "rx submit --> %d\n", retval); |
| 309 | if (skb) |
| 310 | dev_kfree_skb_any(skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 311 | } |
| 312 | return retval; |
| 313 | } |
| 314 | |
| 315 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) |
| 316 | { |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 317 | struct sk_buff *skb = req->context; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 318 | struct eth_dev *dev = ep->driver_data; |
| 319 | int status = req->status; |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 320 | bool queue = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 321 | |
| 322 | switch (status) { |
| 323 | |
| 324 | /* normal completion */ |
| 325 | case 0: |
| 326 | skb_put(skb, req->actual); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 327 | |
| 328 | if (dev->unwrap) { |
| 329 | unsigned long flags; |
| 330 | |
| 331 | spin_lock_irqsave(&dev->lock, flags); |
| 332 | if (dev->port_usb) { |
| 333 | status = dev->unwrap(dev->port_usb, |
| 334 | skb, |
| 335 | &dev->rx_frames); |
Badhri Jagan Sridharan | 8424b3e | 2014-09-18 10:48:48 -0700 | [diff] [blame] | 336 | if (status == -EINVAL) |
| 337 | dev->net->stats.rx_errors++; |
| 338 | else if (status == -EOVERFLOW) |
| 339 | dev->net->stats.rx_over_errors++; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 340 | } else { |
| 341 | dev_kfree_skb_any(skb); |
| 342 | status = -ENOTCONN; |
| 343 | } |
| 344 | spin_unlock_irqrestore(&dev->lock, flags); |
| 345 | } else { |
| 346 | skb_queue_tail(&dev->rx_frames, skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 347 | } |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 348 | if (!status) |
| 349 | queue = 1; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 350 | break; |
| 351 | |
| 352 | /* software-driven interface shutdown */ |
| 353 | case -ECONNRESET: /* unlink */ |
| 354 | case -ESHUTDOWN: /* disconnect etc */ |
| 355 | VDBG(dev, "rx shutdown, code %d\n", status); |
| 356 | goto quiesce; |
| 357 | |
| 358 | /* for hardware automagic (such as pxa) */ |
| 359 | case -ECONNABORTED: /* endpoint reset */ |
| 360 | DBG(dev, "rx %s reset\n", ep->name); |
| 361 | defer_kevent(dev, WORK_RX_MEMORY); |
| 362 | quiesce: |
| 363 | dev_kfree_skb_any(skb); |
| 364 | goto clean; |
| 365 | |
| 366 | /* data overrun */ |
| 367 | case -EOVERFLOW: |
| 368 | dev->net->stats.rx_over_errors++; |
| 369 | /* FALLTHROUGH */ |
| 370 | |
| 371 | default: |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 372 | queue = 1; |
| 373 | dev_kfree_skb_any(skb); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 374 | dev->net->stats.rx_errors++; |
| 375 | DBG(dev, "rx status %d\n", status); |
| 376 | break; |
| 377 | } |
| 378 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 379 | clean: |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 380 | spin_lock(&dev->req_lock); |
| 381 | list_add(&req->list, &dev->rx_reqs); |
| 382 | spin_unlock(&dev->req_lock); |
| 383 | |
| 384 | if (queue) |
| 385 | queue_work(uether_wq, &dev->rx_work); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n) |
| 389 | { |
| 390 | unsigned i; |
| 391 | struct usb_request *req; |
| 392 | |
| 393 | if (!n) |
| 394 | return -ENOMEM; |
| 395 | |
| 396 | /* queue/recycle up to N requests */ |
| 397 | i = n; |
| 398 | list_for_each_entry(req, list, list) { |
| 399 | if (i-- == 0) |
| 400 | goto extra; |
| 401 | } |
| 402 | while (i--) { |
| 403 | req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
| 404 | if (!req) |
| 405 | return list_empty(list) ? -ENOMEM : 0; |
| 406 | list_add(&req->list, list); |
| 407 | } |
| 408 | return 0; |
| 409 | |
| 410 | extra: |
| 411 | /* free extras */ |
| 412 | for (;;) { |
| 413 | struct list_head *next; |
| 414 | |
| 415 | next = req->list.next; |
| 416 | list_del(&req->list); |
| 417 | usb_ep_free_request(ep, req); |
| 418 | |
| 419 | if (next == list) |
| 420 | break; |
| 421 | |
| 422 | req = container_of(next, struct usb_request, list); |
| 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n) |
| 428 | { |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 429 | int status = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 430 | |
| 431 | spin_lock(&dev->req_lock); |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 432 | if (link->in_ep) { |
Vamsi Krishna | 11bdb3b | 2014-05-14 10:57:08 -0700 | [diff] [blame^] | 433 | status = prealloc(&dev->tx_reqs, link->in_ep, n * tx_qmult); |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 434 | if (status < 0) |
| 435 | goto fail; |
| 436 | } |
| 437 | |
| 438 | if (link->out_ep) { |
| 439 | status = prealloc(&dev->rx_reqs, link->out_ep, n); |
| 440 | if (status < 0) |
| 441 | goto fail; |
| 442 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 443 | goto done; |
| 444 | fail: |
| 445 | DBG(dev, "can't alloc requests\n"); |
| 446 | done: |
| 447 | spin_unlock(&dev->req_lock); |
| 448 | return status; |
| 449 | } |
| 450 | |
| 451 | static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags) |
| 452 | { |
| 453 | struct usb_request *req; |
| 454 | unsigned long flags; |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 455 | int req_cnt = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 456 | |
| 457 | /* fill unused rxq slots with some skb */ |
| 458 | spin_lock_irqsave(&dev->req_lock, flags); |
| 459 | while (!list_empty(&dev->rx_reqs)) { |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 460 | /* break the nexus of continuous completion and re-submission*/ |
Praneeth Bajjuri | 6b4b51c | 2015-01-22 16:38:56 -0600 | [diff] [blame] | 461 | if (++req_cnt > qlen(dev->gadget, dev->qmult)) |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 462 | break; |
| 463 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 464 | req = container_of(dev->rx_reqs.next, |
| 465 | struct usb_request, list); |
| 466 | list_del_init(&req->list); |
| 467 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 468 | |
| 469 | if (rx_submit(dev, req, gfp_flags) < 0) { |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 470 | spin_lock_irqsave(&dev->req_lock, flags); |
| 471 | list_add(&req->list, &dev->rx_reqs); |
| 472 | spin_unlock_irqrestore(&dev->req_lock, flags); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 473 | defer_kevent(dev, WORK_RX_MEMORY); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | spin_lock_irqsave(&dev->req_lock, flags); |
| 478 | } |
| 479 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 480 | } |
| 481 | |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 482 | static void process_rx_w(struct work_struct *work) |
| 483 | { |
| 484 | struct eth_dev *dev = container_of(work, struct eth_dev, rx_work); |
| 485 | struct sk_buff *skb; |
| 486 | int status = 0; |
| 487 | |
| 488 | if (!dev->port_usb) |
| 489 | return; |
| 490 | |
| 491 | while ((skb = skb_dequeue(&dev->rx_frames))) { |
| 492 | if (status < 0 |
| 493 | || ETH_HLEN > skb->len |
| 494 | || skb->len > ETH_FRAME_LEN) { |
| 495 | dev->net->stats.rx_errors++; |
| 496 | dev->net->stats.rx_length_errors++; |
| 497 | DBG(dev, "rx length %d\n", skb->len); |
| 498 | dev_kfree_skb_any(skb); |
| 499 | continue; |
| 500 | } |
| 501 | skb->protocol = eth_type_trans(skb, dev->net); |
| 502 | dev->net->stats.rx_packets++; |
| 503 | dev->net->stats.rx_bytes += skb->len; |
| 504 | |
| 505 | status = netif_rx_ni(skb); |
| 506 | } |
| 507 | |
| 508 | if (netif_running(dev->net)) |
| 509 | rx_fill(dev, GFP_KERNEL); |
| 510 | } |
| 511 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 512 | static void eth_work(struct work_struct *work) |
| 513 | { |
| 514 | struct eth_dev *dev = container_of(work, struct eth_dev, work); |
| 515 | |
| 516 | if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) { |
| 517 | if (netif_running(dev->net)) |
| 518 | rx_fill(dev, GFP_KERNEL); |
| 519 | } |
| 520 | |
| 521 | if (dev->todo) |
| 522 | DBG(dev, "work done, flags = 0x%lx\n", dev->todo); |
| 523 | } |
| 524 | |
| 525 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 526 | { |
| 527 | struct sk_buff *skb = req->context; |
| 528 | struct eth_dev *dev = ep->driver_data; |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 529 | struct net_device *net = dev->net; |
| 530 | struct usb_request *new_req; |
| 531 | struct usb_ep *in; |
| 532 | int length; |
| 533 | int retval; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 534 | |
Rajkumar Raghupathy | d868245 | 2013-01-28 11:48:47 +0530 | [diff] [blame] | 535 | if (!dev->port_usb) { |
| 536 | usb_ep_free_request(ep, req); |
| 537 | return; |
| 538 | } |
| 539 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 540 | switch (req->status) { |
| 541 | default: |
| 542 | dev->net->stats.tx_errors++; |
| 543 | VDBG(dev, "tx err %d\n", req->status); |
| 544 | /* FALLTHROUGH */ |
| 545 | case -ECONNRESET: /* unlink */ |
| 546 | case -ESHUTDOWN: /* disconnect etc */ |
| 547 | break; |
| 548 | case 0: |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 549 | if (!req->zero) |
| 550 | dev->net->stats.tx_bytes += req->length-1; |
| 551 | else |
| 552 | dev->net->stats.tx_bytes += req->length; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 553 | } |
| 554 | dev->net->stats.tx_packets++; |
| 555 | |
| 556 | spin_lock(&dev->req_lock); |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 557 | list_add_tail(&req->list, &dev->tx_reqs); |
| 558 | |
Manu Gautam | f8afc31 | 2014-01-29 16:59:21 +0530 | [diff] [blame] | 559 | if (dev->port_usb->multi_pkt_xfer && !req->context) { |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 560 | dev->no_tx_req_used--; |
| 561 | req->length = 0; |
| 562 | in = dev->port_usb->in_ep; |
| 563 | |
| 564 | if (!list_empty(&dev->tx_reqs)) { |
| 565 | new_req = container_of(dev->tx_reqs.next, |
| 566 | struct usb_request, list); |
| 567 | list_del(&new_req->list); |
| 568 | spin_unlock(&dev->req_lock); |
| 569 | if (new_req->length > 0) { |
| 570 | length = new_req->length; |
| 571 | |
| 572 | /* NCM requires no zlp if transfer is |
| 573 | * dwNtbInMaxSize */ |
| 574 | if (dev->port_usb->is_fixed && |
| 575 | length == dev->port_usb->fixed_in_len && |
| 576 | (length % in->maxpacket) == 0) |
| 577 | new_req->zero = 0; |
| 578 | else |
| 579 | new_req->zero = 1; |
| 580 | |
| 581 | /* use zlp framing on tx for strict CDC-Ether |
| 582 | * conformance, though any robust network rx |
| 583 | * path ignores extra padding. and some hardware |
| 584 | * doesn't like to write zlps. |
| 585 | */ |
| 586 | if (new_req->zero && !dev->zlp && |
| 587 | (length % in->maxpacket) == 0) { |
| 588 | new_req->zero = 0; |
| 589 | length++; |
| 590 | } |
| 591 | |
| 592 | new_req->length = length; |
| 593 | retval = usb_ep_queue(in, new_req, GFP_ATOMIC); |
| 594 | switch (retval) { |
| 595 | default: |
| 596 | DBG(dev, "tx queue err %d\n", retval); |
Pavankumar Kondeti | b8a3362 | 2013-04-01 18:13:32 +0530 | [diff] [blame] | 597 | new_req->length = 0; |
| 598 | spin_lock(&dev->req_lock); |
| 599 | list_add_tail(&new_req->list, |
| 600 | &dev->tx_reqs); |
| 601 | spin_unlock(&dev->req_lock); |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 602 | break; |
| 603 | case 0: |
| 604 | spin_lock(&dev->req_lock); |
| 605 | dev->no_tx_req_used++; |
| 606 | spin_unlock(&dev->req_lock); |
Amit Pundir | 0917208 | 2016-05-30 15:19:21 +0530 | [diff] [blame] | 607 | netif_trans_update(net); |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 608 | } |
| 609 | } else { |
| 610 | spin_lock(&dev->req_lock); |
Pavankumar Kondeti | b8a3362 | 2013-04-01 18:13:32 +0530 | [diff] [blame] | 611 | /* |
| 612 | * Put the idle request at the back of the |
| 613 | * queue. The xmit function will put the |
| 614 | * unfinished request at the beginning of the |
| 615 | * queue. |
| 616 | */ |
| 617 | list_add_tail(&new_req->list, &dev->tx_reqs); |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 618 | spin_unlock(&dev->req_lock); |
| 619 | } |
| 620 | } else { |
| 621 | spin_unlock(&dev->req_lock); |
| 622 | } |
| 623 | } else { |
Manu Gautam | f8afc31 | 2014-01-29 16:59:21 +0530 | [diff] [blame] | 624 | /* Is aggregation already enabled and buffers allocated ? */ |
| 625 | if (dev->port_usb->multi_pkt_xfer && dev->tx_req_bufsize) { |
| 626 | req->buf = kzalloc(dev->tx_req_bufsize, GFP_ATOMIC); |
| 627 | req->context = NULL; |
| 628 | } else { |
| 629 | req->buf = NULL; |
| 630 | } |
| 631 | |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 632 | spin_unlock(&dev->req_lock); |
| 633 | dev_kfree_skb_any(skb); |
| 634 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 635 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 636 | if (netif_carrier_ok(dev->net)) |
| 637 | netif_wake_queue(dev->net); |
| 638 | } |
| 639 | |
| 640 | static inline int is_promisc(u16 cdc_filter) |
| 641 | { |
| 642 | return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; |
| 643 | } |
| 644 | |
Rajkumar Raghupathy | 7d4a6cb | 2013-05-23 11:37:41 +0530 | [diff] [blame] | 645 | static int alloc_tx_buffer(struct eth_dev *dev) |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 646 | { |
| 647 | struct list_head *act; |
| 648 | struct usb_request *req; |
| 649 | |
xerox_lin | cdffcb8 | 2014-09-04 16:01:59 +0800 | [diff] [blame] | 650 | dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer * |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 651 | (dev->net->mtu |
| 652 | + sizeof(struct ethhdr) |
| 653 | /* size of rndis_packet_msg_type */ |
| 654 | + 44 |
| 655 | + 22)); |
| 656 | |
| 657 | list_for_each(act, &dev->tx_reqs) { |
| 658 | req = container_of(act, struct usb_request, list); |
| 659 | if (!req->buf) |
| 660 | req->buf = kmalloc(dev->tx_req_bufsize, |
| 661 | GFP_ATOMIC); |
Rajkumar Raghupathy | 7d4a6cb | 2013-05-23 11:37:41 +0530 | [diff] [blame] | 662 | |
| 663 | if (!req->buf) |
| 664 | goto free_buf; |
Manu Gautam | f8afc31 | 2014-01-29 16:59:21 +0530 | [diff] [blame] | 665 | |
| 666 | /* req->context is not used for multi_pkt_xfers */ |
| 667 | req->context = NULL; |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 668 | } |
Rajkumar Raghupathy | 7d4a6cb | 2013-05-23 11:37:41 +0530 | [diff] [blame] | 669 | return 0; |
| 670 | |
| 671 | free_buf: |
| 672 | /* tx_req_bufsize = 0 retries mem alloc on next eth_start_xmit */ |
| 673 | dev->tx_req_bufsize = 0; |
| 674 | list_for_each(act, &dev->tx_reqs) { |
| 675 | req = container_of(act, struct usb_request, list); |
| 676 | kfree(req->buf); |
| 677 | req->buf = NULL; |
| 678 | } |
| 679 | return -ENOMEM; |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 682 | static netdev_tx_t eth_start_xmit(struct sk_buff *skb, |
| 683 | struct net_device *net) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 684 | { |
| 685 | struct eth_dev *dev = netdev_priv(net); |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 686 | int length = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 687 | int retval; |
| 688 | struct usb_request *req = NULL; |
| 689 | unsigned long flags; |
Vamsi Krishna | fdafb97 | 2014-04-02 15:18:55 -0700 | [diff] [blame] | 690 | struct usb_ep *in = NULL; |
| 691 | u16 cdc_filter = 0; |
Mayank Rana | 68f7474 | 2013-02-15 14:55:30 +0530 | [diff] [blame] | 692 | bool multi_pkt_xfer = false; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 693 | |
| 694 | spin_lock_irqsave(&dev->lock, flags); |
| 695 | if (dev->port_usb) { |
| 696 | in = dev->port_usb->in_ep; |
| 697 | cdc_filter = dev->port_usb->cdc_filter; |
Mayank Rana | 68f7474 | 2013-02-15 14:55:30 +0530 | [diff] [blame] | 698 | multi_pkt_xfer = dev->port_usb->multi_pkt_xfer; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 699 | } |
| 700 | spin_unlock_irqrestore(&dev->lock, flags); |
| 701 | |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 702 | if (skb && !in) { |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 703 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 704 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* apply outgoing CDC or RNDIS filters */ |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 708 | if (skb && !is_promisc(cdc_filter)) { |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 709 | u8 *dest = skb->data; |
| 710 | |
| 711 | if (is_multicast_ether_addr(dest)) { |
| 712 | u16 type; |
| 713 | |
| 714 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host |
| 715 | * SET_ETHERNET_MULTICAST_FILTERS requests |
| 716 | */ |
| 717 | if (is_broadcast_ether_addr(dest)) |
| 718 | type = USB_CDC_PACKET_TYPE_BROADCAST; |
| 719 | else |
| 720 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; |
| 721 | if (!(cdc_filter & type)) { |
| 722 | dev_kfree_skb_any(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 723 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ |
| 727 | } |
| 728 | |
Vamsi Krishna | fdafb97 | 2014-04-02 15:18:55 -0700 | [diff] [blame] | 729 | /* |
| 730 | * no buffer copies needed, unless the network stack did it |
| 731 | * or the hardware can't use skb buffers. |
| 732 | * or there's not enough space for extra headers we need |
| 733 | */ |
| 734 | spin_lock_irqsave(&dev->lock, flags); |
| 735 | if (dev->wrap && dev->port_usb) |
| 736 | skb = dev->wrap(dev->port_usb, skb); |
| 737 | spin_unlock_irqrestore(&dev->lock, flags); |
| 738 | |
| 739 | if (!skb) { |
| 740 | if (dev->port_usb && dev->port_usb->supports_multi_frame) { |
| 741 | /* |
| 742 | * Multi frame CDC protocols may store the frame for |
| 743 | * later which is not a dropped frame. |
| 744 | */ |
| 745 | } else { |
| 746 | dev->net->stats.tx_dropped++; |
| 747 | } |
| 748 | |
| 749 | /* no error code for dropped packets */ |
| 750 | return NETDEV_TX_OK; |
| 751 | } |
| 752 | |
| 753 | /* Allocate memory for tx_reqs to support multi packet transfer */ |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 754 | spin_lock_irqsave(&dev->req_lock, flags); |
Vamsi Krishna | fdafb97 | 2014-04-02 15:18:55 -0700 | [diff] [blame] | 755 | if (multi_pkt_xfer && !dev->tx_req_bufsize) { |
| 756 | retval = alloc_tx_buffer(dev); |
| 757 | if (retval < 0) { |
| 758 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 759 | return -ENOMEM; |
| 760 | } |
| 761 | } |
| 762 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 763 | /* |
| 764 | * this freelist can be empty if an interrupt triggered disconnect() |
| 765 | * and reconfigured the gadget (shutting down this queue) after the |
| 766 | * network stack decided to xmit but before we got the spinlock. |
| 767 | */ |
| 768 | if (list_empty(&dev->tx_reqs)) { |
| 769 | spin_unlock_irqrestore(&dev->req_lock, flags); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 770 | return NETDEV_TX_BUSY; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | req = container_of(dev->tx_reqs.next, struct usb_request, list); |
| 774 | list_del(&req->list); |
| 775 | |
| 776 | /* temporarily stop TX queue when the freelist empties */ |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 777 | if (list_empty(&dev->tx_reqs)) { |
| 778 | /* |
| 779 | * tx_throttle gives info about number of times u_ether |
| 780 | * asked network layer to stop queueing packets to it |
| 781 | * when transmit resources are unavailable |
| 782 | */ |
| 783 | dev->tx_throttle++; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 784 | netif_stop_queue(net); |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 785 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 786 | |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 787 | dev->tx_skb_hold_count++; |
| 788 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 789 | |
Mayank Rana | 68f7474 | 2013-02-15 14:55:30 +0530 | [diff] [blame] | 790 | if (multi_pkt_xfer) { |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 791 | memcpy(req->buf + req->length, skb->data, skb->len); |
| 792 | req->length = req->length + skb->len; |
| 793 | length = req->length; |
| 794 | dev_kfree_skb_any(skb); |
| 795 | |
| 796 | spin_lock_irqsave(&dev->req_lock, flags); |
xerox_lin | cdffcb8 | 2014-09-04 16:01:59 +0800 | [diff] [blame] | 797 | if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) { |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 798 | if (dev->no_tx_req_used > TX_REQ_THRESHOLD) { |
| 799 | list_add(&req->list, &dev->tx_reqs); |
| 800 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 801 | goto success; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | dev->no_tx_req_used++; |
| 806 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 807 | |
| 808 | spin_lock_irqsave(&dev->lock, flags); |
| 809 | dev->tx_skb_hold_count = 0; |
| 810 | spin_unlock_irqrestore(&dev->lock, flags); |
| 811 | } else { |
| 812 | length = skb->len; |
| 813 | req->buf = skb->data; |
| 814 | req->context = skb; |
| 815 | } |
| 816 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 817 | req->complete = tx_complete; |
| 818 | |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 819 | /* NCM requires no zlp if transfer is dwNtbInMaxSize */ |
Harish Jenny K N | 79775f4 | 2016-09-09 11:30:41 +0200 | [diff] [blame] | 820 | if (dev->port_usb && |
| 821 | dev->port_usb->is_fixed && |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 822 | length == dev->port_usb->fixed_in_len && |
| 823 | (length % in->maxpacket) == 0) |
| 824 | req->zero = 0; |
| 825 | else |
| 826 | req->zero = 1; |
| 827 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 828 | /* use zlp framing on tx for strict CDC-Ether conformance, |
| 829 | * though any robust network rx path ignores extra padding. |
| 830 | * and some hardware doesn't like to write zlps. |
| 831 | */ |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 832 | if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) { |
| 833 | req->zero = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 834 | length++; |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 835 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 836 | |
| 837 | req->length = length; |
| 838 | |
Badhri Jagan Sridharan | cb6a786 | 2014-09-18 10:42:41 -0700 | [diff] [blame] | 839 | /* throttle highspeed IRQ rate back slightly */ |
| 840 | if (gadget_is_dualspeed(dev->gadget) && |
| 841 | (dev->gadget->speed == USB_SPEED_HIGH)) { |
| 842 | dev->tx_qlen++; |
Praneeth Bajjuri | 6b4b51c | 2015-01-22 16:38:56 -0600 | [diff] [blame] | 843 | if (dev->tx_qlen == (dev->qmult/2)) { |
Badhri Jagan Sridharan | cb6a786 | 2014-09-18 10:42:41 -0700 | [diff] [blame] | 844 | req->no_interrupt = 0; |
| 845 | dev->tx_qlen = 0; |
| 846 | } else { |
| 847 | req->no_interrupt = 1; |
| 848 | } |
| 849 | } else { |
| 850 | req->no_interrupt = 0; |
| 851 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 852 | |
| 853 | retval = usb_ep_queue(in, req, GFP_ATOMIC); |
| 854 | switch (retval) { |
| 855 | default: |
| 856 | DBG(dev, "tx queue err %d\n", retval); |
| 857 | break; |
| 858 | case 0: |
Florian Westphal | 860e953 | 2016-05-03 16:33:13 +0200 | [diff] [blame] | 859 | netif_trans_update(net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | if (retval) { |
Mayank Rana | 68f7474 | 2013-02-15 14:55:30 +0530 | [diff] [blame] | 863 | if (!multi_pkt_xfer) |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 864 | dev_kfree_skb_any(skb); |
Pavankumar Kondeti | b8a3362 | 2013-04-01 18:13:32 +0530 | [diff] [blame] | 865 | else |
| 866 | req->length = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 867 | dev->net->stats.tx_dropped++; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 868 | spin_lock_irqsave(&dev->req_lock, flags); |
| 869 | if (list_empty(&dev->tx_reqs)) |
| 870 | netif_start_queue(net); |
| 871 | list_add(&req->list, &dev->tx_reqs); |
| 872 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 873 | } |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 874 | success: |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 875 | return NETDEV_TX_OK; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /*-------------------------------------------------------------------------*/ |
| 879 | |
| 880 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) |
| 881 | { |
| 882 | DBG(dev, "%s\n", __func__); |
| 883 | |
| 884 | /* fill the rx queue */ |
| 885 | rx_fill(dev, gfp_flags); |
| 886 | |
| 887 | /* and open the tx floodgates */ |
Badhri Jagan Sridharan | cb6a786 | 2014-09-18 10:42:41 -0700 | [diff] [blame] | 888 | dev->tx_qlen = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 889 | netif_wake_queue(dev->net); |
| 890 | } |
| 891 | |
| 892 | static int eth_open(struct net_device *net) |
| 893 | { |
| 894 | struct eth_dev *dev = netdev_priv(net); |
| 895 | struct gether *link; |
| 896 | |
| 897 | DBG(dev, "%s\n", __func__); |
| 898 | if (netif_carrier_ok(dev->net)) |
| 899 | eth_start(dev, GFP_KERNEL); |
| 900 | |
| 901 | spin_lock_irq(&dev->lock); |
| 902 | link = dev->port_usb; |
| 903 | if (link && link->open) |
| 904 | link->open(link); |
| 905 | spin_unlock_irq(&dev->lock); |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | static int eth_stop(struct net_device *net) |
| 911 | { |
| 912 | struct eth_dev *dev = netdev_priv(net); |
| 913 | unsigned long flags; |
| 914 | |
| 915 | VDBG(dev, "%s\n", __func__); |
| 916 | netif_stop_queue(net); |
| 917 | |
| 918 | DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n", |
| 919 | dev->net->stats.rx_packets, dev->net->stats.tx_packets, |
| 920 | dev->net->stats.rx_errors, dev->net->stats.tx_errors |
| 921 | ); |
| 922 | |
| 923 | /* ensure there are no more active requests */ |
| 924 | spin_lock_irqsave(&dev->lock, flags); |
| 925 | if (dev->port_usb) { |
| 926 | struct gether *link = dev->port_usb; |
Michael Grzeschik | b1b552a | 2012-08-08 11:48:10 +0200 | [diff] [blame] | 927 | const struct usb_endpoint_descriptor *in; |
| 928 | const struct usb_endpoint_descriptor *out; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 929 | |
| 930 | if (link->close) |
| 931 | link->close(link); |
| 932 | |
| 933 | /* NOTE: we have no abort-queue primitive we could use |
| 934 | * to cancel all pending I/O. Instead, we disable then |
| 935 | * reenable the endpoints ... this idiom may leave toggle |
| 936 | * wrong, but that's a self-correcting error. |
| 937 | * |
| 938 | * REVISIT: we *COULD* just let the transfers complete at |
| 939 | * their own pace; the network stack can handle old packets. |
| 940 | * For the moment we leave this here, since it works. |
| 941 | */ |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 942 | if (link->in_ep) { |
| 943 | in = link->in_ep->desc; |
| 944 | usb_ep_disable(link->in_ep); |
| 945 | if (netif_carrier_ok(net)) { |
| 946 | DBG(dev, "host still using in endpoints\n"); |
| 947 | link->in_ep->desc = in; |
| 948 | usb_ep_enable(link->in_ep); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | if (link->out_ep) { |
| 953 | out = link->out_ep->desc; |
| 954 | usb_ep_disable(link->out_ep); |
| 955 | if (netif_carrier_ok(net)) { |
| 956 | DBG(dev, "host still using out endpoints\n"); |
| 957 | link->out_ep->desc = out; |
| 958 | usb_ep_enable(link->out_ep); |
| 959 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | spin_unlock_irqrestore(&dev->lock, flags); |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | /*-------------------------------------------------------------------------*/ |
| 968 | |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 969 | static int get_ether_addr(const char *str, u8 *dev_addr) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 970 | { |
| 971 | if (str) { |
| 972 | unsigned i; |
| 973 | |
| 974 | for (i = 0; i < 6; i++) { |
| 975 | unsigned char num; |
| 976 | |
| 977 | if ((*str == '.') || (*str == ':')) |
| 978 | str++; |
Andy Shevchenko | e644814 | 2010-06-15 17:04:44 +0300 | [diff] [blame] | 979 | num = hex_to_bin(*str++) << 4; |
| 980 | num |= hex_to_bin(*str++); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 981 | dev_addr [i] = num; |
| 982 | } |
| 983 | if (is_valid_ether_addr(dev_addr)) |
| 984 | return 0; |
| 985 | } |
Joe Perches | 006c913 | 2012-07-12 22:33:11 -0700 | [diff] [blame] | 986 | eth_random_addr(dev_addr); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 987 | return 1; |
| 988 | } |
| 989 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 990 | static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len) |
| 991 | { |
| 992 | if (len < 18) |
| 993 | return -EINVAL; |
| 994 | |
Andy Shevchenko | 27f3870 | 2015-01-15 13:40:04 +0200 | [diff] [blame] | 995 | snprintf(str, len, "%pM", dev_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 996 | return 18; |
| 997 | } |
| 998 | |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 999 | static int ether_ioctl(struct net_device *, struct ifreq *, int); |
| 1000 | |
Stephen Hemminger | 5ec38f3 | 2009-01-07 18:05:39 -0800 | [diff] [blame] | 1001 | static const struct net_device_ops eth_netdev_ops = { |
| 1002 | .ndo_open = eth_open, |
| 1003 | .ndo_stop = eth_stop, |
| 1004 | .ndo_start_xmit = eth_start_xmit, |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 1005 | .ndo_do_ioctl = ether_ioctl, |
Stephen Hemminger | 5ec38f3 | 2009-01-07 18:05:39 -0800 | [diff] [blame] | 1006 | .ndo_change_mtu = ueth_change_mtu, |
| 1007 | .ndo_set_mac_address = eth_mac_addr, |
| 1008 | .ndo_validate_addr = eth_validate_addr, |
| 1009 | }; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1010 | |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 1011 | static const struct net_device_ops eth_netdev_ops_ip = { |
| 1012 | .ndo_open = eth_open, |
| 1013 | .ndo_stop = eth_stop, |
| 1014 | .ndo_start_xmit = eth_start_xmit, |
| 1015 | .ndo_do_ioctl = ether_ioctl, |
| 1016 | .ndo_change_mtu = ueth_change_mtu_ip, |
| 1017 | .ndo_set_mac_address = NULL, |
| 1018 | .ndo_validate_addr = NULL, |
| 1019 | }; |
| 1020 | |
| 1021 | static int rmnet_ioctl_extended(struct net_device *dev, struct ifreq *ifr) |
| 1022 | { |
| 1023 | struct rmnet_ioctl_extended_s ext_cmd; |
| 1024 | struct eth_dev *eth_dev = netdev_priv(dev); |
| 1025 | int rc = 0; |
| 1026 | |
| 1027 | rc = copy_from_user(&ext_cmd, ifr->ifr_ifru.ifru_data, |
| 1028 | sizeof(struct rmnet_ioctl_extended_s)); |
| 1029 | |
| 1030 | if (rc) { |
| 1031 | DBG(eth_dev, "%s(): copy_from_user() failed\n", __func__); |
| 1032 | return rc; |
| 1033 | } |
| 1034 | |
| 1035 | switch (ext_cmd.extended_ioctl) { |
| 1036 | case RMNET_IOCTL_GET_SUPPORTED_FEATURES: |
| 1037 | ext_cmd.u.data = 0; |
| 1038 | break; |
| 1039 | |
| 1040 | case RMNET_IOCTL_SET_MRU: |
| 1041 | if (netif_running(dev)) |
| 1042 | return -EBUSY; |
| 1043 | |
| 1044 | /* 16K max */ |
| 1045 | if ((size_t)ext_cmd.u.data > 0x4000) |
| 1046 | return -EINVAL; |
| 1047 | |
| 1048 | if (eth_dev->port_usb) { |
| 1049 | eth_dev->port_usb->is_fixed = true; |
| 1050 | eth_dev->port_usb->fixed_out_len = |
| 1051 | (size_t) ext_cmd.u.data; |
| 1052 | DBG(eth_dev, "[%s] rmnet_ioctl(): SET MRU to %u\n", |
| 1053 | dev->name, eth_dev->port_usb->fixed_out_len); |
| 1054 | } else { |
| 1055 | pr_err("[%s]: %s: SET MRU failed. Cable disconnected\n", |
| 1056 | dev->name, __func__); |
| 1057 | return -ENODEV; |
| 1058 | } |
| 1059 | break; |
| 1060 | |
| 1061 | case RMNET_IOCTL_GET_MRU: |
| 1062 | if (eth_dev->port_usb) { |
| 1063 | ext_cmd.u.data = eth_dev->port_usb->is_fixed ? |
| 1064 | eth_dev->port_usb->fixed_out_len : |
| 1065 | dev->mtu; |
| 1066 | } else { |
| 1067 | pr_err("[%s]: %s: GET MRU failed. Cable disconnected\n", |
| 1068 | dev->name, __func__); |
| 1069 | return -ENODEV; |
| 1070 | } |
| 1071 | break; |
| 1072 | |
| 1073 | case RMNET_IOCTL_GET_DRIVER_NAME: |
| 1074 | strlcpy(ext_cmd.u.if_name, dev->name, |
| 1075 | sizeof(ext_cmd.u.if_name)); |
| 1076 | break; |
| 1077 | |
| 1078 | default: |
| 1079 | break; |
| 1080 | } |
| 1081 | |
| 1082 | rc = copy_to_user(ifr->ifr_ifru.ifru_data, &ext_cmd, |
| 1083 | sizeof(struct rmnet_ioctl_extended_s)); |
| 1084 | |
| 1085 | if (rc) |
| 1086 | DBG(eth_dev, "%s(): copy_to_user() failed\n", __func__); |
| 1087 | return rc; |
| 1088 | } |
| 1089 | |
| 1090 | static int ether_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 1091 | { |
| 1092 | struct eth_dev *eth_dev = netdev_priv(dev); |
| 1093 | void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data; |
| 1094 | int prev_mtu = dev->mtu; |
| 1095 | u32 state, old_opmode; |
| 1096 | int rc = -EFAULT; |
| 1097 | |
| 1098 | old_opmode = eth_dev->flags; |
| 1099 | /* Process IOCTL command */ |
| 1100 | switch (cmd) { |
| 1101 | case RMNET_IOCTL_SET_LLP_ETHERNET: /*Set Ethernet protocol*/ |
| 1102 | /* Perform Ethernet config only if in IP mode currently*/ |
| 1103 | if (test_bit(RMNET_MODE_LLP_IP, ð_dev->flags)) { |
| 1104 | ether_setup(dev); |
| 1105 | dev->mtu = prev_mtu; |
| 1106 | dev->netdev_ops = ð_netdev_ops; |
| 1107 | clear_bit(RMNET_MODE_LLP_IP, ð_dev->flags); |
| 1108 | set_bit(RMNET_MODE_LLP_ETH, ð_dev->flags); |
| 1109 | DBG(eth_dev, "[%s] ioctl(): set Ethernet proto mode\n", |
| 1110 | dev->name); |
| 1111 | } |
| 1112 | if (test_bit(RMNET_MODE_LLP_ETH, ð_dev->flags)) |
| 1113 | rc = 0; |
| 1114 | break; |
| 1115 | |
| 1116 | case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol*/ |
| 1117 | /* Perform IP config only if in Ethernet mode currently*/ |
| 1118 | if (test_bit(RMNET_MODE_LLP_ETH, ð_dev->flags)) { |
| 1119 | /* Undo config done in ether_setup() */ |
| 1120 | dev->header_ops = NULL; /* No header */ |
| 1121 | dev->type = ARPHRD_RAWIP; |
| 1122 | dev->hard_header_len = 0; |
| 1123 | dev->mtu = prev_mtu; |
| 1124 | dev->addr_len = 0; |
| 1125 | dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST); |
| 1126 | dev->netdev_ops = ð_netdev_ops_ip; |
| 1127 | clear_bit(RMNET_MODE_LLP_ETH, ð_dev->flags); |
| 1128 | set_bit(RMNET_MODE_LLP_IP, ð_dev->flags); |
| 1129 | DBG(eth_dev, "[%s] ioctl(): set IP protocol mode\n", |
| 1130 | dev->name); |
| 1131 | } |
| 1132 | if (test_bit(RMNET_MODE_LLP_IP, ð_dev->flags)) |
| 1133 | rc = 0; |
| 1134 | break; |
| 1135 | |
| 1136 | case RMNET_IOCTL_GET_LLP: /* Get link protocol state */ |
| 1137 | state = eth_dev->flags & (RMNET_MODE_LLP_ETH |
| 1138 | | RMNET_MODE_LLP_IP); |
| 1139 | if (copy_to_user(addr, &state, sizeof(state))) |
| 1140 | break; |
| 1141 | rc = 0; |
| 1142 | break; |
| 1143 | |
| 1144 | case RMNET_IOCTL_SET_RX_HEADROOM: /* Set RX headroom */ |
| 1145 | if (copy_from_user(ð_dev->rx_needed_headroom, addr, |
| 1146 | sizeof(eth_dev->rx_needed_headroom))) |
| 1147 | break; |
| 1148 | DBG(eth_dev, "[%s] ioctl(): set RX HEADROOM: %x\n", |
| 1149 | dev->name, eth_dev->rx_needed_headroom); |
| 1150 | rc = 0; |
| 1151 | break; |
| 1152 | |
| 1153 | case RMNET_IOCTL_EXTENDED: |
| 1154 | rc = rmnet_ioctl_extended(dev, ifr); |
| 1155 | break; |
| 1156 | |
| 1157 | default: |
| 1158 | pr_err("[%s] error: ioctl called for unsupported cmd[%d]", |
| 1159 | dev->name, cmd); |
| 1160 | rc = -EINVAL; |
| 1161 | } |
| 1162 | |
| 1163 | DBG(eth_dev, "[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08lx\n", |
| 1164 | dev->name, __func__, cmd, old_opmode, eth_dev->flags); |
| 1165 | |
| 1166 | return rc; |
| 1167 | } |
| 1168 | |
Marcel Holtmann | aa79074 | 2010-01-15 22:13:58 -0800 | [diff] [blame] | 1169 | static struct device_type gadget_type = { |
| 1170 | .name = "gadget", |
| 1171 | }; |
| 1172 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1173 | /** |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 1174 | * gether_setup_name - initialize one ethernet-over-usb link |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1175 | * @g: gadget to associated with these links |
| 1176 | * @ethaddr: NULL, or a buffer in which the ethernet address of the |
| 1177 | * host side of the link is recorded |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 1178 | * @netname: name for network device (for example, "usb") |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1179 | * Context: may sleep |
| 1180 | * |
| 1181 | * This sets up the single network link that may be exported by a |
| 1182 | * gadget driver using this framework. The link layer addresses are |
| 1183 | * set up using module parameters. |
| 1184 | * |
Dan Carpenter | 574f24f | 2013-11-14 11:42:11 +0300 | [diff] [blame] | 1185 | * Returns an eth_dev pointer on success, or an ERR_PTR on failure. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1186 | */ |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1187 | struct eth_dev *gether_setup_name(struct usb_gadget *g, |
| 1188 | const char *dev_addr, const char *host_addr, |
| 1189 | u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1190 | { |
| 1191 | struct eth_dev *dev; |
| 1192 | struct net_device *net; |
| 1193 | int status; |
| 1194 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1195 | net = alloc_etherdev(sizeof *dev); |
| 1196 | if (!net) |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1197 | return ERR_PTR(-ENOMEM); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1198 | |
| 1199 | dev = netdev_priv(net); |
| 1200 | spin_lock_init(&dev->lock); |
| 1201 | spin_lock_init(&dev->req_lock); |
| 1202 | INIT_WORK(&dev->work, eth_work); |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 1203 | INIT_WORK(&dev->rx_work, process_rx_w); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1204 | INIT_LIST_HEAD(&dev->tx_reqs); |
| 1205 | INIT_LIST_HEAD(&dev->rx_reqs); |
| 1206 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 1207 | skb_queue_head_init(&dev->rx_frames); |
| 1208 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1209 | /* network device setup */ |
| 1210 | dev->net = net; |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1211 | dev->qmult = qmult; |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 1212 | snprintf(net->name, sizeof(net->name), "%s%%d", netname); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1213 | |
| 1214 | if (get_ether_addr(dev_addr, net->dev_addr)) |
| 1215 | dev_warn(&g->dev, |
| 1216 | "using random %s ethernet address\n", "self"); |
| 1217 | if (get_ether_addr(host_addr, dev->host_mac)) |
| 1218 | dev_warn(&g->dev, |
| 1219 | "using random %s ethernet address\n", "host"); |
| 1220 | |
| 1221 | if (ethaddr) |
| 1222 | memcpy(ethaddr, dev->host_mac, ETH_ALEN); |
| 1223 | |
Stephen Hemminger | 5ec38f3 | 2009-01-07 18:05:39 -0800 | [diff] [blame] | 1224 | net->netdev_ops = ð_netdev_ops; |
| 1225 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1226 | net->ethtool_ops = &ops; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1227 | |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 1228 | /* set operation mode to eth by default */ |
| 1229 | set_bit(RMNET_MODE_LLP_ETH, &dev->flags); |
| 1230 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1231 | dev->gadget = g; |
| 1232 | SET_NETDEV_DEV(net, &g->dev); |
Marcel Holtmann | aa79074 | 2010-01-15 22:13:58 -0800 | [diff] [blame] | 1233 | SET_NETDEV_DEVTYPE(net, &gadget_type); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1234 | |
| 1235 | status = register_netdev(net); |
| 1236 | if (status < 0) { |
| 1237 | dev_dbg(&g->dev, "register_netdev failed, %d\n", status); |
| 1238 | free_netdev(net); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1239 | dev = ERR_PTR(status); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1240 | } else { |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 1241 | INFO(dev, "MAC %pM\n", net->dev_addr); |
| 1242 | INFO(dev, "HOST MAC %pM\n", dev->host_mac); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1243 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1244 | /* |
| 1245 | * two kinds of host-initiated state changes: |
Kevin Cernekee | 31bde1c | 2012-06-24 21:11:22 -0700 | [diff] [blame] | 1246 | * - iff DATA transfer is active, carrier is "on" |
| 1247 | * - tx queueing enabled if open *and* carrier is "on" |
| 1248 | */ |
| 1249 | netif_carrier_off(net); |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 1250 | uether_debugfs_init(dev); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1253 | return dev; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1254 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1255 | EXPORT_SYMBOL_GPL(gether_setup_name); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1256 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1257 | struct net_device *gether_setup_name_default(const char *netname) |
| 1258 | { |
| 1259 | struct net_device *net; |
| 1260 | struct eth_dev *dev; |
| 1261 | |
| 1262 | net = alloc_etherdev(sizeof(*dev)); |
| 1263 | if (!net) |
| 1264 | return ERR_PTR(-ENOMEM); |
| 1265 | |
| 1266 | dev = netdev_priv(net); |
| 1267 | spin_lock_init(&dev->lock); |
| 1268 | spin_lock_init(&dev->req_lock); |
| 1269 | INIT_WORK(&dev->work, eth_work); |
Matthew Moeller | 5df3222 | 2016-03-09 20:19:25 -0600 | [diff] [blame] | 1270 | INIT_WORK(&dev->rx_work, process_rx_w); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1271 | INIT_LIST_HEAD(&dev->tx_reqs); |
| 1272 | INIT_LIST_HEAD(&dev->rx_reqs); |
| 1273 | |
| 1274 | skb_queue_head_init(&dev->rx_frames); |
| 1275 | |
| 1276 | /* network device setup */ |
| 1277 | dev->net = net; |
| 1278 | dev->qmult = QMULT_DEFAULT; |
| 1279 | snprintf(net->name, sizeof(net->name), "%s%%d", netname); |
| 1280 | |
| 1281 | eth_random_addr(dev->dev_mac); |
| 1282 | pr_warn("using random %s ethernet address\n", "self"); |
| 1283 | eth_random_addr(dev->host_mac); |
| 1284 | pr_warn("using random %s ethernet address\n", "host"); |
| 1285 | |
| 1286 | net->netdev_ops = ð_netdev_ops; |
| 1287 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1288 | net->ethtool_ops = &ops; |
Hemant Kumar | fc49dbd | 2018-01-23 12:08:47 -0800 | [diff] [blame] | 1289 | |
| 1290 | /* set operation mode to eth by default */ |
| 1291 | set_bit(RMNET_MODE_LLP_ETH, &dev->flags); |
| 1292 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1293 | SET_NETDEV_DEVTYPE(net, &gadget_type); |
| 1294 | |
| 1295 | return net; |
| 1296 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1297 | EXPORT_SYMBOL_GPL(gether_setup_name_default); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1298 | |
| 1299 | int gether_register_netdev(struct net_device *net) |
| 1300 | { |
| 1301 | struct eth_dev *dev; |
| 1302 | struct usb_gadget *g; |
| 1303 | struct sockaddr sa; |
| 1304 | int status; |
| 1305 | |
| 1306 | if (!net->dev.parent) |
| 1307 | return -EINVAL; |
| 1308 | dev = netdev_priv(net); |
| 1309 | g = dev->gadget; |
| 1310 | status = register_netdev(net); |
| 1311 | if (status < 0) { |
| 1312 | dev_dbg(&g->dev, "register_netdev failed, %d\n", status); |
| 1313 | return status; |
| 1314 | } else { |
| 1315 | INFO(dev, "HOST MAC %pM\n", dev->host_mac); |
| 1316 | |
| 1317 | /* two kinds of host-initiated state changes: |
| 1318 | * - iff DATA transfer is active, carrier is "on" |
| 1319 | * - tx queueing enabled if open *and* carrier is "on" |
| 1320 | */ |
| 1321 | netif_carrier_off(net); |
| 1322 | } |
| 1323 | sa.sa_family = net->type; |
| 1324 | memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN); |
| 1325 | rtnl_lock(); |
| 1326 | status = dev_set_mac_address(net, &sa); |
| 1327 | rtnl_unlock(); |
| 1328 | if (status) |
| 1329 | pr_warn("cannot set self ethernet address: %d\n", status); |
| 1330 | else |
| 1331 | INFO(dev, "MAC %pM\n", dev->dev_mac); |
| 1332 | |
| 1333 | return status; |
| 1334 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1335 | EXPORT_SYMBOL_GPL(gether_register_netdev); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1336 | |
| 1337 | void gether_set_gadget(struct net_device *net, struct usb_gadget *g) |
| 1338 | { |
| 1339 | struct eth_dev *dev; |
| 1340 | |
| 1341 | dev = netdev_priv(net); |
| 1342 | dev->gadget = g; |
| 1343 | SET_NETDEV_DEV(net, &g->dev); |
| 1344 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1345 | EXPORT_SYMBOL_GPL(gether_set_gadget); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1346 | |
| 1347 | int gether_set_dev_addr(struct net_device *net, const char *dev_addr) |
| 1348 | { |
| 1349 | struct eth_dev *dev; |
| 1350 | u8 new_addr[ETH_ALEN]; |
| 1351 | |
| 1352 | dev = netdev_priv(net); |
| 1353 | if (get_ether_addr(dev_addr, new_addr)) |
| 1354 | return -EINVAL; |
| 1355 | memcpy(dev->dev_mac, new_addr, ETH_ALEN); |
| 1356 | return 0; |
| 1357 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1358 | EXPORT_SYMBOL_GPL(gether_set_dev_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1359 | |
| 1360 | int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len) |
| 1361 | { |
| 1362 | struct eth_dev *dev; |
| 1363 | |
| 1364 | dev = netdev_priv(net); |
| 1365 | return get_ether_addr_str(dev->dev_mac, dev_addr, len); |
| 1366 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1367 | EXPORT_SYMBOL_GPL(gether_get_dev_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1368 | |
| 1369 | int gether_set_host_addr(struct net_device *net, const char *host_addr) |
| 1370 | { |
| 1371 | struct eth_dev *dev; |
| 1372 | u8 new_addr[ETH_ALEN]; |
| 1373 | |
| 1374 | dev = netdev_priv(net); |
| 1375 | if (get_ether_addr(host_addr, new_addr)) |
| 1376 | return -EINVAL; |
| 1377 | memcpy(dev->host_mac, new_addr, ETH_ALEN); |
| 1378 | return 0; |
| 1379 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1380 | EXPORT_SYMBOL_GPL(gether_set_host_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1381 | |
| 1382 | int gether_get_host_addr(struct net_device *net, char *host_addr, int len) |
| 1383 | { |
| 1384 | struct eth_dev *dev; |
| 1385 | |
| 1386 | dev = netdev_priv(net); |
| 1387 | return get_ether_addr_str(dev->host_mac, host_addr, len); |
| 1388 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1389 | EXPORT_SYMBOL_GPL(gether_get_host_addr); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1390 | |
| 1391 | int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len) |
| 1392 | { |
| 1393 | struct eth_dev *dev; |
| 1394 | |
| 1395 | if (len < 13) |
| 1396 | return -EINVAL; |
| 1397 | |
| 1398 | dev = netdev_priv(net); |
| 1399 | snprintf(host_addr, len, "%pm", dev->host_mac); |
| 1400 | |
| 1401 | return strlen(host_addr); |
| 1402 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1403 | EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1404 | |
Andrzej Pietrasiewicz | bf4277c | 2013-05-28 09:15:45 +0200 | [diff] [blame] | 1405 | void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]) |
| 1406 | { |
| 1407 | struct eth_dev *dev; |
| 1408 | |
| 1409 | dev = netdev_priv(net); |
| 1410 | memcpy(host_mac, dev->host_mac, ETH_ALEN); |
| 1411 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1412 | EXPORT_SYMBOL_GPL(gether_get_host_addr_u8); |
Andrzej Pietrasiewicz | bf4277c | 2013-05-28 09:15:45 +0200 | [diff] [blame] | 1413 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1414 | void gether_set_qmult(struct net_device *net, unsigned qmult) |
| 1415 | { |
| 1416 | struct eth_dev *dev; |
| 1417 | |
| 1418 | dev = netdev_priv(net); |
| 1419 | dev->qmult = qmult; |
| 1420 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1421 | EXPORT_SYMBOL_GPL(gether_set_qmult); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1422 | |
| 1423 | unsigned gether_get_qmult(struct net_device *net) |
| 1424 | { |
| 1425 | struct eth_dev *dev; |
| 1426 | |
| 1427 | dev = netdev_priv(net); |
| 1428 | return dev->qmult; |
| 1429 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1430 | EXPORT_SYMBOL_GPL(gether_get_qmult); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1431 | |
| 1432 | int gether_get_ifname(struct net_device *net, char *name, int len) |
| 1433 | { |
| 1434 | rtnl_lock(); |
| 1435 | strlcpy(name, netdev_name(net), len); |
| 1436 | rtnl_unlock(); |
| 1437 | return strlen(name); |
| 1438 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1439 | EXPORT_SYMBOL_GPL(gether_get_ifname); |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 1440 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1441 | /** |
| 1442 | * gether_cleanup - remove Ethernet-over-USB device |
| 1443 | * Context: may sleep |
| 1444 | * |
| 1445 | * This is called to free all resources allocated by @gether_setup(). |
| 1446 | */ |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1447 | void gether_cleanup(struct eth_dev *dev) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1448 | { |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1449 | if (!dev) |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1450 | return; |
| 1451 | |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 1452 | uether_debugfs_exit(dev); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1453 | unregister_netdev(dev->net); |
| 1454 | flush_work(&dev->work); |
| 1455 | free_netdev(dev->net); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1456 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1457 | EXPORT_SYMBOL_GPL(gether_cleanup); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1458 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1459 | /** |
| 1460 | * gether_connect - notify network layer that USB link is active |
| 1461 | * @link: the USB link, set up with endpoints, descriptors matching |
| 1462 | * current device speed, and any framing wrapper(s) set up. |
| 1463 | * Context: irqs blocked |
| 1464 | * |
| 1465 | * This is called to activate endpoints and let the network layer know |
| 1466 | * the connection is active ("carrier detect"). It may cause the I/O |
| 1467 | * queues to open and start letting network packets flow, but will in |
| 1468 | * any case activate the endpoints so that they respond properly to the |
| 1469 | * USB host. |
| 1470 | * |
| 1471 | * Verify net_device pointer returned using IS_ERR(). If it doesn't |
| 1472 | * indicate some error code (negative errno), ep->driver_data values |
| 1473 | * have been overwritten. |
| 1474 | */ |
| 1475 | struct net_device *gether_connect(struct gether *link) |
| 1476 | { |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 1477 | struct eth_dev *dev = link->ioport; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1478 | int result = 0; |
| 1479 | |
| 1480 | if (!dev) |
| 1481 | return ERR_PTR(-EINVAL); |
| 1482 | |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1483 | if (link->in_ep) { |
| 1484 | link->in_ep->driver_data = dev; |
| 1485 | result = usb_ep_enable(link->in_ep); |
| 1486 | if (result != 0) { |
| 1487 | DBG(dev, "enable %s --> %d\n", |
| 1488 | link->in_ep->name, result); |
| 1489 | goto fail0; |
| 1490 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1493 | if (link->out_ep) { |
| 1494 | link->out_ep->driver_data = dev; |
| 1495 | result = usb_ep_enable(link->out_ep); |
| 1496 | if (result != 0) { |
| 1497 | DBG(dev, "enable %s --> %d\n", |
| 1498 | link->out_ep->name, result); |
| 1499 | goto fail1; |
| 1500 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | if (result == 0) |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1504 | result = alloc_requests(dev, link, qlen(dev->gadget, |
| 1505 | dev->qmult)); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1506 | |
| 1507 | if (result == 0) { |
| 1508 | dev->zlp = link->is_zlp_ok; |
Yoshihiro Shimoda | 05f6b0f | 2016-08-22 17:48:26 +0900 | [diff] [blame] | 1509 | dev->no_skb_reserve = link->no_skb_reserve; |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1510 | DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult)); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1511 | |
| 1512 | dev->header_len = link->header_len; |
| 1513 | dev->unwrap = link->unwrap; |
| 1514 | dev->wrap = link->wrap; |
xerox_lin | 87bebf8 | 2014-08-14 14:48:44 +0800 | [diff] [blame] | 1515 | dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer; |
xerox_lin | cdffcb8 | 2014-09-04 16:01:59 +0800 | [diff] [blame] | 1516 | dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1517 | |
| 1518 | spin_lock(&dev->lock); |
Badhri Jagan Sridharan | e791ad3 | 2014-09-18 10:46:08 -0700 | [diff] [blame] | 1519 | dev->tx_skb_hold_count = 0; |
| 1520 | dev->no_tx_req_used = 0; |
| 1521 | dev->tx_req_bufsize = 0; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1522 | dev->port_usb = link; |
David Brownell | 29bac7b | 2008-09-06 21:33:49 -0700 | [diff] [blame] | 1523 | if (netif_running(dev->net)) { |
| 1524 | if (link->open) |
| 1525 | link->open(link); |
| 1526 | } else { |
| 1527 | if (link->close) |
| 1528 | link->close(link); |
| 1529 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1530 | spin_unlock(&dev->lock); |
| 1531 | |
| 1532 | netif_carrier_on(dev->net); |
| 1533 | if (netif_running(dev->net)) |
| 1534 | eth_start(dev, GFP_ATOMIC); |
| 1535 | |
| 1536 | /* on error, disable any endpoints */ |
| 1537 | } else { |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1538 | if (link->out_ep) |
| 1539 | (void) usb_ep_disable(link->out_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1540 | fail1: |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1541 | if (link->in_ep) |
| 1542 | (void) usb_ep_disable(link->in_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1543 | } |
| 1544 | fail0: |
| 1545 | /* caller is responsible for cleanup on error */ |
| 1546 | if (result < 0) |
| 1547 | return ERR_PTR(result); |
| 1548 | return dev->net; |
| 1549 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1550 | EXPORT_SYMBOL_GPL(gether_connect); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1551 | |
| 1552 | /** |
| 1553 | * gether_disconnect - notify network layer that USB link is inactive |
| 1554 | * @link: the USB link, on which gether_connect() was called |
| 1555 | * Context: irqs blocked |
| 1556 | * |
| 1557 | * This is called to deactivate endpoints and let the network layer know |
| 1558 | * the connection went inactive ("no carrier"). |
| 1559 | * |
| 1560 | * On return, the state is as if gether_connect() had never been called. |
| 1561 | * The endpoints are inactive, and accordingly without active USB I/O. |
| 1562 | * Pointers to endpoint descriptors and endpoint private data are nulled. |
| 1563 | */ |
| 1564 | void gether_disconnect(struct gether *link) |
| 1565 | { |
| 1566 | struct eth_dev *dev = link->ioport; |
| 1567 | struct usb_request *req; |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 1568 | struct sk_buff *skb; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1569 | |
| 1570 | WARN_ON(!dev); |
| 1571 | if (!dev) |
| 1572 | return; |
| 1573 | |
| 1574 | DBG(dev, "%s\n", __func__); |
| 1575 | |
| 1576 | netif_stop_queue(dev->net); |
| 1577 | netif_carrier_off(dev->net); |
| 1578 | |
| 1579 | /* disable endpoints, forcing (synchronous) completion |
| 1580 | * of all pending i/o. then free the request objects |
| 1581 | * and forget about the endpoints. |
| 1582 | */ |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1583 | if (link->in_ep) { |
| 1584 | usb_ep_disable(link->in_ep); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1585 | spin_lock(&dev->req_lock); |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1586 | while (!list_empty(&dev->tx_reqs)) { |
| 1587 | req = container_of(dev->tx_reqs.next, |
| 1588 | struct usb_request, list); |
| 1589 | list_del(&req->list); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1590 | |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1591 | spin_unlock(&dev->req_lock); |
Rajkumar Raghupathy | 7d4a6cb | 2013-05-23 11:37:41 +0530 | [diff] [blame] | 1592 | if (link->multi_pkt_xfer) { |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1593 | kfree(req->buf); |
Rajkumar Raghupathy | 7d4a6cb | 2013-05-23 11:37:41 +0530 | [diff] [blame] | 1594 | req->buf = NULL; |
| 1595 | } |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1596 | usb_ep_free_request(link->in_ep, req); |
| 1597 | spin_lock(&dev->req_lock); |
| 1598 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1599 | spin_unlock(&dev->req_lock); |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1600 | link->in_ep->desc = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1601 | } |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 1602 | |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1603 | if (link->out_ep) { |
| 1604 | usb_ep_disable(link->out_ep); |
| 1605 | spin_lock(&dev->req_lock); |
| 1606 | while (!list_empty(&dev->rx_reqs)) { |
| 1607 | req = container_of(dev->rx_reqs.next, |
| 1608 | struct usb_request, list); |
| 1609 | list_del(&req->list); |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 1610 | |
Hemant Kumar | 8a7c812 | 2018-01-05 11:47:46 -0800 | [diff] [blame] | 1611 | spin_unlock(&dev->req_lock); |
| 1612 | usb_ep_free_request(link->out_ep, req); |
| 1613 | spin_lock(&dev->req_lock); |
| 1614 | } |
| 1615 | spin_unlock(&dev->req_lock); |
| 1616 | |
| 1617 | spin_lock(&dev->rx_frames.lock); |
| 1618 | while ((skb = __skb_dequeue(&dev->rx_frames))) |
| 1619 | dev_kfree_skb_any(skb); |
| 1620 | spin_unlock(&dev->rx_frames.lock); |
| 1621 | |
| 1622 | link->out_ep->desc = NULL; |
| 1623 | } |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1624 | |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 1625 | pr_debug("%s(): tx_throttle count= %lu", __func__, |
| 1626 | dev->tx_throttle); |
| 1627 | /* reset tx_throttle count */ |
| 1628 | dev->tx_throttle = 0; |
| 1629 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1630 | /* finish forgetting about this USB link episode */ |
| 1631 | dev->header_len = 0; |
| 1632 | dev->unwrap = NULL; |
| 1633 | dev->wrap = NULL; |
| 1634 | |
| 1635 | spin_lock(&dev->lock); |
| 1636 | dev->port_usb = NULL; |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 1637 | spin_unlock(&dev->lock); |
| 1638 | } |
Felipe Balbi | 0700faa | 2014-04-01 13:19:32 -0500 | [diff] [blame] | 1639 | EXPORT_SYMBOL_GPL(gether_disconnect); |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1640 | |
Saket Saurabh | 6481d88 | 2013-09-27 15:52:36 +0530 | [diff] [blame] | 1641 | static int uether_stat_show(struct seq_file *s, void *unused) |
| 1642 | { |
| 1643 | struct eth_dev *dev = s->private; |
| 1644 | int ret = 0; |
| 1645 | |
| 1646 | if (dev) |
| 1647 | seq_printf(s, "tx_throttle = %lu\n", dev->tx_throttle); |
| 1648 | return ret; |
| 1649 | } |
| 1650 | |
| 1651 | static int uether_open(struct inode *inode, struct file *file) |
| 1652 | { |
| 1653 | return single_open(file, uether_stat_show, inode->i_private); |
| 1654 | } |
| 1655 | |
| 1656 | static ssize_t uether_stat_reset(struct file *file, |
| 1657 | const char __user *ubuf, size_t count, loff_t *ppos) |
| 1658 | { |
| 1659 | struct seq_file *s = file->private_data; |
| 1660 | struct eth_dev *dev = s->private; |
| 1661 | unsigned long flags; |
| 1662 | |
| 1663 | spin_lock_irqsave(&dev->lock, flags); |
| 1664 | /* Reset tx_throttle */ |
| 1665 | dev->tx_throttle = 0; |
| 1666 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1667 | return count; |
| 1668 | } |
| 1669 | |
| 1670 | static const struct file_operations uether_stats_ops = { |
| 1671 | .open = uether_open, |
| 1672 | .read = seq_read, |
| 1673 | .write = uether_stat_reset, |
| 1674 | }; |
| 1675 | |
| 1676 | static void uether_debugfs_init(struct eth_dev *dev) |
| 1677 | { |
| 1678 | struct dentry *uether_dent; |
| 1679 | struct dentry *uether_dfile; |
| 1680 | |
| 1681 | uether_dent = debugfs_create_dir("uether_rndis", NULL); |
| 1682 | if (IS_ERR(uether_dent)) |
| 1683 | return; |
| 1684 | dev->uether_dent = uether_dent; |
| 1685 | |
| 1686 | uether_dfile = debugfs_create_file("status", 0644, |
| 1687 | uether_dent, dev, &uether_stats_ops); |
| 1688 | if (!uether_dfile || IS_ERR(uether_dfile)) |
| 1689 | debugfs_remove(uether_dent); |
| 1690 | dev->uether_dfile = uether_dfile; |
| 1691 | } |
| 1692 | |
| 1693 | static void uether_debugfs_exit(struct eth_dev *dev) |
| 1694 | { |
| 1695 | debugfs_remove(dev->uether_dfile); |
| 1696 | debugfs_remove(dev->uether_dent); |
| 1697 | dev->uether_dent = NULL; |
| 1698 | dev->uether_dfile = NULL; |
| 1699 | } |
| 1700 | |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 1701 | static int __init gether_init(void) |
| 1702 | { |
| 1703 | uether_wq = create_singlethread_workqueue("uether"); |
| 1704 | if (!uether_wq) { |
| 1705 | pr_err("%s: Unable to create workqueue: uether\n", __func__); |
| 1706 | return -ENOMEM; |
| 1707 | } |
| 1708 | return 0; |
| 1709 | } |
| 1710 | module_init(gether_init); |
| 1711 | |
| 1712 | static void __exit gether_exit(void) |
| 1713 | { |
| 1714 | destroy_workqueue(uether_wq); |
| 1715 | |
| 1716 | } |
| 1717 | module_exit(gether_exit); |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 1718 | MODULE_AUTHOR("David Brownell"); |
Badhri Jagan Sridharan | a096ba5 | 2014-09-24 18:58:23 -0700 | [diff] [blame] | 1719 | MODULE_DESCRIPTION("ethernet over USB driver"); |
| 1720 | MODULE_LICENSE("GPL v2"); |