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