blob: d7a07138b534e6a4776f4d5f1524baaf33ab1d32 [file] [log] [blame]
David Brownell2b3d9422008-06-19 18:19:28 -07001/*
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 Brownell2b3d9422008-06-19 18:19:28 -070012 */
13
14/* #define VERBOSE_DEBUG */
15
16#include <linux/kernel.h>
Sebastian Andrzej Siewior98303172012-09-10 16:30:50 +020017#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
David Brownell2b3d9422008-06-19 18:19:28 -070019#include <linux/device.h>
20#include <linux/ctype.h>
21#include <linux/etherdevice.h>
22#include <linux/ethtool.h>
Ian Coolidge4fe5f072012-11-07 14:39:18 +000023#include <linux/if_vlan.h>
Hemant Kumarfc49dbd2018-01-23 12:08:47 -080024#include <linux/if_arp.h>
25#include <linux/msm_rmnet.h>
Saket Saurabh6481d882013-09-27 15:52:36 +053026#include <linux/debugfs.h>
27#include <linux/seq_file.h>
David Brownell2b3d9422008-06-19 18:19:28 -070028
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 Niebuhr9b39e9d2009-08-14 10:04:22 -050037 * 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 Brownell2b3d9422008-06-19 18:19:28 -070040 *
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 Brownell8a1ce2c2008-08-18 17:43:56 -070053#define UETH__VERSION "29-May-2008"
David Brownell2b3d9422008-06-19 18:19:28 -070054
Mike Looijmansbba787a2015-08-05 08:54:55 +020055/* 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 Sridharana096ba52014-09-24 18:58:23 -070060static struct workqueue_struct *uether_wq;
61
David Brownell2b3d9422008-06-19 18:19:28 -070062struct eth_dev {
63 /* lock is held while accessing port_usb
David Brownell2b3d9422008-06-19 18:19:28 -070064 */
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 Sridharancb6a7862014-09-18 10:42:41 -070073 unsigned tx_qlen;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -070074/* Minimum number of TX USB request queued to UDC */
Sujeet Kumardf2a5022015-02-04 16:38:33 +053075#define MAX_TX_REQ_WITH_NO_INT 5
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -070076 int no_tx_req_used;
77 int tx_skb_hold_count;
78 u32 tx_req_bufsize;
David Brownell2b3d9422008-06-19 18:19:28 -070079
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050080 struct sk_buff_head rx_frames;
81
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020082 unsigned qmult;
83
David Brownell2b3d9422008-06-19 18:19:28 -070084 unsigned header_len;
xerox_lin87bebf82014-08-14 14:48:44 +080085 unsigned ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +080086 unsigned dl_max_pkts_per_xfer;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050087 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 Brownell2b3d9422008-06-19 18:19:28 -070091
92 struct work_struct work;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -070093 struct work_struct rx_work;
David Brownell2b3d9422008-06-19 18:19:28 -070094
95 unsigned long todo;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -080096 unsigned long flags;
97 unsigned short rx_needed_headroom;
David Brownell2b3d9422008-06-19 18:19:28 -070098#define WORK_RX_MEMORY 0
99
100 bool zlp;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900101 bool no_skb_reserve;
David Brownell2b3d9422008-06-19 18:19:28 -0700102 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200103 u8 dev_mac[ETH_ALEN];
Saket Saurabh6481d882013-09-27 15:52:36 +0530104 unsigned long tx_throttle;
Manu Gautam354be032014-05-15 13:46:33 +0530105 unsigned long rx_throttle;
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700106 unsigned int tx_pkts_rcvd;
Saket Saurabh6481d882013-09-27 15:52:36 +0530107 struct dentry *uether_dent;
108 struct dentry *uether_dfile;
David Brownell2b3d9422008-06-19 18:19:28 -0700109};
110
Saket Saurabh6481d882013-09-27 15:52:36 +0530111static void uether_debugfs_init(struct eth_dev *dev);
112static void uether_debugfs_exit(struct eth_dev *dev);
113
David Brownell2b3d9422008-06-19 18:19:28 -0700114/*-------------------------------------------------------------------------*/
115
116#define RX_EXTRA 20 /* bytes guarding against rx overflows */
117
118#define DEFAULT_QLEN 2 /* double buffering by default */
119
Vamsi Krishna11bdb3b2014-05-14 10:57:08 -0700120/*
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 */
128static unsigned int tx_qmult = 1;
129module_param(tx_qmult, uint, 0644);
130MODULE_PARM_DESC(tx_qmult, "Additional queue length multiplier for tx");
131
Paul Zimmerman04617db2011-06-27 14:13:18 -0700132/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200133static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700134{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700135 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
136 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700137 return qmult * DEFAULT_QLEN;
138 else
139 return DEFAULT_QLEN;
140}
141
142/*-------------------------------------------------------------------------*/
Manu Gautam354be032014-05-15 13:46:33 +0530143#define U_ETHER_RX_PENDING_TSHOLD 500
144
145static unsigned int u_ether_rx_pending_thld = U_ETHER_RX_PENDING_TSHOLD;
146module_param(u_ether_rx_pending_thld, uint, 0644);
David Brownell2b3d9422008-06-19 18:19:28 -0700147
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 Brownell2b3d9422008-06-19 18:19:28 -0700155#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 Brownell2b3d9422008-06-19 18:19:28 -0700178#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 Hemmingerccad6372008-11-19 22:42:31 -0800185static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700186{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100187 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
188 return -ERANGE;
189 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700190
Mike Looijmansab738ff2015-11-30 12:18:23 +0100191 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700192}
193
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800194static 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 Brownell2b3d9422008-06-19 18:19:28 -0700213static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
214{
Jiri Pirko7826d432013-01-06 00:44:26 +0000215 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700216
Jiri Pirko7826d432013-01-06 00:44:26 +0000217 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 Brownell2b3d9422008-06-19 18:19:28 -0700221}
222
David Brownell2b3d9422008-06-19 18:19:28 -0700223/* 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 Hemminger0fc0b732009-09-02 01:03:33 -0700229static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700230 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700231 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700232};
233
234static 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
244static void rx_complete(struct usb_ep *ep, struct usb_request *req);
245
246static int
247rx_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 Brownell2b3d9422008-06-19 18:19:28 -0700260
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530261 if (!out) {
262 spin_unlock_irqrestore(&dev->lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700263 return -ENOTCONN;
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530264 }
David Brownell2b3d9422008-06-19 18:19:28 -0700265
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_lin87bebf82014-08-14 14:48:44 +0800283 if (dev->ul_max_pkts_per_xfer)
284 size *= dev->ul_max_pkts_per_xfer;
285
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200286 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800287 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530288 spin_unlock_irqrestore(&dev->lock, flags);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200289
Amit Pundir5ff0eb22016-01-08 19:36:02 +0530290 DBG(dev, "%s: size: %zd\n", __func__, size);
Sujeet Kumar9a3cf922015-02-04 15:58:30 +0530291 skb = alloc_skb(size, gfp_flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700292 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 Shimoda05f6b0f2016-08-22 17:48:26 +0900301 if (likely(!dev->no_skb_reserve))
Sujeet Kumar9a3cf922015-02-04 15:58:30 +0530302 skb_reserve(skb, 0);
David Brownell2b3d9422008-06-19 18:19:28 -0700303
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)
311enomem:
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 Brownell2b3d9422008-06-19 18:19:28 -0700317 }
318 return retval;
319}
320
321static void rx_complete(struct usb_ep *ep, struct usb_request *req)
322{
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700323 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700324 struct eth_dev *dev = ep->driver_data;
325 int status = req->status;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700326 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700327
328 switch (status) {
329
330 /* normal completion */
331 case 0:
332 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500333
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 Sridharan8424b3e2014-09-18 10:48:48 -0700342 if (status == -EINVAL)
343 dev->net->stats.rx_errors++;
344 else if (status == -EOVERFLOW)
345 dev->net->stats.rx_over_errors++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500346 } 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 Brownell2b3d9422008-06-19 18:19:28 -0700353 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700354 if (!status)
355 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700356 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);
368quiesce:
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 Sridharana096ba52014-09-24 18:58:23 -0700378 queue = 1;
379 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700380 dev->net->stats.rx_errors++;
381 DBG(dev, "rx status %d\n", status);
382 break;
383 }
384
David Brownell2b3d9422008-06-19 18:19:28 -0700385clean:
Manu Gautam354be032014-05-15 13:46:33 +0530386 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 Sridharana096ba52014-09-24 18:58:23 -0700400
401 if (queue)
402 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700403}
404
405static 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
427extra:
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
444static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
445{
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800446 int status = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700447
448 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800449 if (link->in_ep) {
Vamsi Krishna11bdb3b2014-05-14 10:57:08 -0700450 status = prealloc(&dev->tx_reqs, link->in_ep, n * tx_qmult);
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800451 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 Brownell2b3d9422008-06-19 18:19:28 -0700460 goto done;
461fail:
462 DBG(dev, "can't alloc requests\n");
463done:
464 spin_unlock(&dev->req_lock);
465 return status;
466}
467
468static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
469{
470 struct usb_request *req;
471 unsigned long flags;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700472 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700473
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 Sridharana096ba52014-09-24 18:58:23 -0700477 /* break the nexus of continuous completion and re-submission*/
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600478 if (++req_cnt > qlen(dev->gadget, dev->qmult))
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700479 break;
480
David Brownell2b3d9422008-06-19 18:19:28 -0700481 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 Sridharana096ba52014-09-24 18:58:23 -0700487 spin_lock_irqsave(&dev->req_lock, flags);
488 list_add(&req->list, &dev->rx_reqs);
489 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700490 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 Sridharana096ba52014-09-24 18:58:23 -0700499static 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 Brownell2b3d9422008-06-19 18:19:28 -0700529static 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
542static 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 Sridharane791ad32014-09-18 10:46:08 -0700546 struct net_device *net = dev->net;
547 struct usb_request *new_req;
548 struct usb_ep *in;
549 int length;
550 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700551
Rajkumar Raghupathyd8682452013-01-28 11:48:47 +0530552 if (!dev->port_usb) {
553 usb_ep_free_request(ep, req);
554 return;
555 }
556
David Brownell2b3d9422008-06-19 18:19:28 -0700557 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 Sridharane791ad32014-09-18 10:46:08 -0700566 if (!req->zero)
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700567 dev->net->stats.tx_bytes += req->actual-1;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700568 else
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700569 dev->net->stats.tx_bytes += req->actual;
David Brownell2b3d9422008-06-19 18:19:28 -0700570 }
571 dev->net->stats.tx_packets++;
572
573 spin_lock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700574
Manu Gautamf8afc312014-01-29 16:59:21 +0530575 if (dev->port_usb->multi_pkt_xfer && !req->context) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700576 dev->no_tx_req_used--;
577 req->length = 0;
578 in = dev->port_usb->in_ep;
579
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530580 /* Do not process further if no_interrupt is set */
581 if (!req->no_interrupt && !list_empty(&dev->tx_reqs)) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700582 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 Kumardf2a5022015-02-04 16:38:33 +0530609 /* 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 Sridharane791ad32014-09-18 10:46:08 -0700619 new_req->length = length;
Tarun Gupta091d4ff2015-07-30 11:59:11 +0530620 new_req->complete = tx_complete;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700621 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
622 switch (retval) {
623 default:
624 DBG(dev, "tx queue err %d\n", retval);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530625 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 Sridharane791ad32014-09-18 10:46:08 -0700630 break;
631 case 0:
632 spin_lock(&dev->req_lock);
633 dev->no_tx_req_used++;
634 spin_unlock(&dev->req_lock);
Amit Pundir09172082016-05-30 15:19:21 +0530635 netif_trans_update(net);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700636 }
637 } else {
638 spin_lock(&dev->req_lock);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530639 /*
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 Sridharane791ad32014-09-18 10:46:08 -0700646 spin_unlock(&dev->req_lock);
647 }
648 } else {
649 spin_unlock(&dev->req_lock);
650 }
651 } else {
Manu Gautamf8afc312014-01-29 16:59:21 +0530652 /* Is aggregation already enabled and buffers allocated ? */
653 if (dev->port_usb->multi_pkt_xfer && dev->tx_req_bufsize) {
ChandanaKishori Chiluveru2b5ed572015-08-05 15:30:40 +0530654 req->buf = kzalloc(dev->tx_req_bufsize
655 + dev->gadget->extra_buf_alloc, GFP_ATOMIC);
Manu Gautamf8afc312014-01-29 16:59:21 +0530656 req->context = NULL;
657 } else {
658 req->buf = NULL;
659 }
660
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700661 spin_unlock(&dev->req_lock);
662 dev_kfree_skb_any(skb);
663 }
David Brownell2b3d9422008-06-19 18:19:28 -0700664
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530665 /* 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 Brownell2b3d9422008-06-19 18:19:28 -0700670 if (netif_carrier_ok(dev->net))
671 netif_wake_queue(dev->net);
672}
673
674static inline int is_promisc(u16 cdc_filter)
675{
676 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
677}
678
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530679static int alloc_tx_buffer(struct eth_dev *dev)
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700680{
681 struct list_head *act;
682 struct usb_request *req;
683
xerox_lincdffcb82014-09-04 16:01:59 +0800684 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700685 (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 Chiluveru2b5ed572015-08-05 15:30:40 +0530694 req->buf = kmalloc(dev->tx_req_bufsize
695 + dev->gadget->extra_buf_alloc, GFP_ATOMIC);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530696
697 if (!req->buf)
698 goto free_buf;
Manu Gautamf8afc312014-01-29 16:59:21 +0530699
700 /* req->context is not used for multi_pkt_xfers */
701 req->context = NULL;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700702 }
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530703 return 0;
704
705free_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 Sridharane791ad32014-09-18 10:46:08 -0700714}
715
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000716static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
717 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700718{
719 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100720 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700721 int retval;
722 struct usb_request *req = NULL;
723 unsigned long flags;
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700724 struct usb_ep *in = NULL;
725 u16 cdc_filter = 0;
Mayank Rana68f74742013-02-15 14:55:30 +0530726 bool multi_pkt_xfer = false;
David Brownell2b3d9422008-06-19 18:19:28 -0700727
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 Rana68f74742013-02-15 14:55:30 +0530732 multi_pkt_xfer = dev->port_usb->multi_pkt_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -0700733 }
734 spin_unlock_irqrestore(&dev->lock, flags);
735
Jim Baxter6d3865f2014-07-07 18:33:18 +0100736 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700737 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000738 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700739 }
740
741 /* apply outgoing CDC or RNDIS filters */
Hemant Kumar301fba02018-03-16 11:26:47 -0700742 if (!test_bit(RMNET_MODE_LLP_IP, &dev->flags) &&
743 !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700744 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 Kumar301fba02018-03-16 11:26:47 -0700757 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700758 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000759 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700760 }
761 }
762 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
763 }
764
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700765 dev->tx_pkts_rcvd++;
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700766 /*
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 Brownell2b3d9422008-06-19 18:19:28 -0700791 spin_lock_irqsave(&dev->req_lock, flags);
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700792 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 Brownell2b3d9422008-06-19 18:19:28 -0700800 /*
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 McHardy5b548142009-06-12 06:22:29 +0000807 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700808 }
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 Saurabh6481d882013-09-27 15:52:36 +0530814 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 Brownell2b3d9422008-06-19 18:19:28 -0700821 netif_stop_queue(net);
Saket Saurabh6481d882013-09-27 15:52:36 +0530822 }
David Brownell2b3d9422008-06-19 18:19:28 -0700823
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700824 dev->tx_skb_hold_count++;
825 spin_unlock_irqrestore(&dev->req_lock, flags);
826
Mayank Rana68f74742013-02-15 14:55:30 +0530827 if (multi_pkt_xfer) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700828 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_lincdffcb82014-09-04 16:01:59 +0800834 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530835 /*
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 Sridharane791ad32014-09-18 10:46:08 -0700842 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 Brownell2b3d9422008-06-19 18:19:28 -0700860 req->complete = tx_complete;
861
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200862 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
Harish Jenny K N79775f42016-09-09 11:30:41 +0200863 if (dev->port_usb &&
864 dev->port_usb->is_fixed &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200865 length == dev->port_usb->fixed_in_len &&
866 (length % in->maxpacket) == 0)
867 req->zero = 0;
868 else
869 req->zero = 1;
870
David Brownell2b3d9422008-06-19 18:19:28 -0700871 /* 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 Sridharane791ad32014-09-18 10:46:08 -0700875 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
876 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700877 length++;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700878 }
David Brownell2b3d9422008-06-19 18:19:28 -0700879
880 req->length = length;
881
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700882 /* throttle highspeed IRQ rate back slightly */
883 if (gadget_is_dualspeed(dev->gadget) &&
884 (dev->gadget->speed == USB_SPEED_HIGH)) {
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530885 spin_lock_irqsave(&dev->req_lock, flags);
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700886 dev->tx_qlen++;
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530887 if (dev->tx_qlen == MAX_TX_REQ_WITH_NO_INT) {
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700888 req->no_interrupt = 0;
889 dev->tx_qlen = 0;
890 } else {
891 req->no_interrupt = 1;
892 }
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530893 spin_unlock_irqrestore(&dev->req_lock, flags);
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700894 } else {
895 req->no_interrupt = 0;
896 }
David Brownell2b3d9422008-06-19 18:19:28 -0700897
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 Westphal860e9532016-05-03 16:33:13 +0200904 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700905 }
906
907 if (retval) {
Mayank Rana68f74742013-02-15 14:55:30 +0530908 if (!multi_pkt_xfer)
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700909 dev_kfree_skb_any(skb);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530910 else
911 req->length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700912 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700913 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 Sridharane791ad32014-09-18 10:46:08 -0700919success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000920 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700921}
922
923/*-------------------------------------------------------------------------*/
924
925static 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 Sridharancb6a7862014-09-18 10:42:41 -0700933 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700934 netif_wake_queue(dev->net);
935}
936
937static 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
955static 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 Grzeschikb1b552a2012-08-08 11:48:10 +0200972 const struct usb_endpoint_descriptor *in;
973 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700974
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 Kumar8a7c8122018-01-05 11:47:46 -0800987 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 Brownell2b3d9422008-06-19 18:19:28 -07001005 }
1006 }
1007 spin_unlock_irqrestore(&dev->lock, flags);
1008
1009 return 0;
1010}
1011
1012/*-------------------------------------------------------------------------*/
1013
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001014static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -07001015{
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 Shevchenkoe6448142010-06-15 17:04:44 +03001024 num = hex_to_bin(*str++) << 4;
1025 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -07001026 dev_addr [i] = num;
1027 }
1028 if (is_valid_ether_addr(dev_addr))
1029 return 0;
1030 }
Joe Perches006c9132012-07-12 22:33:11 -07001031 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -07001032 return 1;
1033}
1034
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001035static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
1036{
1037 if (len < 18)
1038 return -EINVAL;
1039
Andy Shevchenko27f38702015-01-15 13:40:04 +02001040 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001041 return 18;
1042}
1043
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001044static int ether_ioctl(struct net_device *, struct ifreq *, int);
1045
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001046static 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 Kumarfc49dbd2018-01-23 12:08:47 -08001050 .ndo_do_ioctl = ether_ioctl,
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001051 .ndo_change_mtu = ueth_change_mtu,
1052 .ndo_set_mac_address = eth_mac_addr,
1053 .ndo_validate_addr = eth_validate_addr,
1054};
David Brownell2b3d9422008-06-19 18:19:28 -07001055
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001056static 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
1066static 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
1135static 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, &eth_dev->flags)) {
1149 ether_setup(dev);
1150 dev->mtu = prev_mtu;
1151 dev->netdev_ops = &eth_netdev_ops;
1152 clear_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1153 set_bit(RMNET_MODE_LLP_ETH, &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, &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, &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 = &eth_netdev_ops_ip;
1172 clear_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1173 set_bit(RMNET_MODE_LLP_IP, &eth_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, &eth_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(&eth_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 Holtmannaa790742010-01-15 22:13:58 -08001214static struct device_type gadget_type = {
1215 .name = "gadget",
1216};
1217
David Brownell2b3d9422008-06-19 18:19:28 -07001218/**
Mike Lockwood036e98b2012-05-10 10:08:02 +02001219 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -07001220 * @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 Lockwood036e98b2012-05-10 10:08:02 +02001223 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -07001224 * 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 Carpenter574f24f2013-11-14 11:42:11 +03001230 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -07001231 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001232struct 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 Brownell2b3d9422008-06-19 18:19:28 -07001235{
1236 struct eth_dev *dev;
1237 struct net_device *net;
1238 int status;
1239
David Brownell2b3d9422008-06-19 18:19:28 -07001240 net = alloc_etherdev(sizeof *dev);
1241 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001242 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -07001243
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 Sridharana096ba52014-09-24 18:58:23 -07001248 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -07001249 INIT_LIST_HEAD(&dev->tx_reqs);
1250 INIT_LIST_HEAD(&dev->rx_reqs);
1251
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001252 skb_queue_head_init(&dev->rx_frames);
1253
David Brownell2b3d9422008-06-19 18:19:28 -07001254 /* network device setup */
1255 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001256 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +02001257 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -07001258
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 Hemminger5ec38f32009-01-07 18:05:39 -08001269 net->netdev_ops = &eth_netdev_ops;
1270
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001271 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -07001272
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001273 /* set operation mode to eth by default */
1274 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1275
David Brownell2b3d9422008-06-19 18:19:28 -07001276 dev->gadget = g;
1277 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -08001278 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -07001279
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 Siewiord6a01432012-12-23 21:10:12 +01001284 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -07001285 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001286 INFO(dev, "MAC %pM\n", net->dev_addr);
1287 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001288
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001289 /*
1290 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -07001291 * - 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 Saurabh6481d882013-09-27 15:52:36 +05301295 uether_debugfs_init(dev);
David Brownell2b3d9422008-06-19 18:19:28 -07001296 }
1297
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001298 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -07001299}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001300EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -07001301
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001302struct 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 Moeller5df32222016-03-09 20:19:25 -06001315 INIT_WORK(&dev->rx_work, process_rx_w);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001316 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 = &eth_netdev_ops;
1332
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001333 net->ethtool_ops = &ops;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001334
1335 /* set operation mode to eth by default */
1336 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1337
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001338 SET_NETDEV_DEVTYPE(net, &gadget_type);
1339
1340 return net;
1341}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001342EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001343
1344int 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 Balbi0700faa2014-04-01 13:19:32 -05001380EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001381
1382void 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 Balbi0700faa2014-04-01 13:19:32 -05001390EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001391
1392int 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 Balbi0700faa2014-04-01 13:19:32 -05001403EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001404
1405int 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 Balbi0700faa2014-04-01 13:19:32 -05001412EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001413
1414int 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 Balbi0700faa2014-04-01 13:19:32 -05001425EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001426
1427int 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 Balbi0700faa2014-04-01 13:19:32 -05001434EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001435
1436int 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 Balbi0700faa2014-04-01 13:19:32 -05001448EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001449
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001450void 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 Balbi0700faa2014-04-01 13:19:32 -05001457EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001458
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001459void 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 Balbi0700faa2014-04-01 13:19:32 -05001466EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001467
1468unsigned 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 Balbi0700faa2014-04-01 13:19:32 -05001475EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001476
1477int 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 Balbi0700faa2014-04-01 13:19:32 -05001484EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001485
David Brownell2b3d9422008-06-19 18:19:28 -07001486/**
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 Siewiord6a01432012-12-23 21:10:12 +01001492void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001493{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001494 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001495 return;
1496
Saket Saurabh6481d882013-09-27 15:52:36 +05301497 uether_debugfs_exit(dev);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001498 unregister_netdev(dev->net);
1499 flush_work(&dev->work);
1500 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001501}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001502EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001503
David Brownell2b3d9422008-06-19 18:19:28 -07001504/**
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 */
1520struct net_device *gether_connect(struct gether *link)
1521{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001522 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001523 int result = 0;
1524
1525 if (!dev)
1526 return ERR_PTR(-EINVAL);
1527
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001528 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 Brownell2b3d9422008-06-19 18:19:28 -07001536 }
1537
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001538 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 Brownell2b3d9422008-06-19 18:19:28 -07001546 }
1547
1548 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001549 result = alloc_requests(dev, link, qlen(dev->gadget,
1550 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001551
1552 if (result == 0) {
1553 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001554 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001555 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001556
1557 dev->header_len = link->header_len;
1558 dev->unwrap = link->unwrap;
1559 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001560 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +08001561 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001562
1563 spin_lock(&dev->lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -07001564 dev->tx_skb_hold_count = 0;
1565 dev->no_tx_req_used = 0;
1566 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001567 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001568 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 Brownell2b3d9422008-06-19 18:19:28 -07001575 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 Kumar8a7c8122018-01-05 11:47:46 -08001583 if (link->out_ep)
1584 (void) usb_ep_disable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001585fail1:
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001586 if (link->in_ep)
1587 (void) usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001588 }
1589fail0:
1590 /* caller is responsible for cleanup on error */
1591 if (result < 0)
1592 return ERR_PTR(result);
1593 return dev->net;
1594}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001595EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001596
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 */
1609void gether_disconnect(struct gether *link)
1610{
1611 struct eth_dev *dev = link->ioport;
1612 struct usb_request *req;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001613 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001614
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 Kumar8a7c8122018-01-05 11:47:46 -08001628 if (link->in_ep) {
1629 usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001630 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001631 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 Brownell2b3d9422008-06-19 18:19:28 -07001635
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001636 spin_unlock(&dev->req_lock);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +05301637 if (link->multi_pkt_xfer) {
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001638 kfree(req->buf);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +05301639 req->buf = NULL;
1640 }
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001641 usb_ep_free_request(link->in_ep, req);
1642 spin_lock(&dev->req_lock);
1643 }
David Brownell2b3d9422008-06-19 18:19:28 -07001644 spin_unlock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001645 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001646 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001647
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001648 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 Sridharana096ba52014-09-24 18:58:23 -07001655
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001656 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 Brownell2b3d9422008-06-19 18:19:28 -07001669
Saket Saurabh6481d882013-09-27 15:52:36 +05301670 pr_debug("%s(): tx_throttle count= %lu", __func__,
1671 dev->tx_throttle);
1672 /* reset tx_throttle count */
1673 dev->tx_throttle = 0;
Manu Gautam354be032014-05-15 13:46:33 +05301674 dev->rx_throttle = 0;
Saket Saurabh6481d882013-09-27 15:52:36 +05301675
David Brownell2b3d9422008-06-19 18:19:28 -07001676 /* 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 Brownell2b3d9422008-06-19 18:19:28 -07001683 spin_unlock(&dev->lock);
1684}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001685EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001686
Saket Saurabh6481d882013-09-27 15:52:36 +05301687static int uether_stat_show(struct seq_file *s, void *unused)
1688{
1689 struct eth_dev *dev = s->private;
1690 int ret = 0;
1691
Manu Gautam354be032014-05-15 13:46:33 +05301692 if (dev) {
Saket Saurabh6481d882013-09-27 15:52:36 +05301693 seq_printf(s, "tx_throttle = %lu\n", dev->tx_throttle);
Vamsi Krishna41fc4952014-06-01 20:20:15 -07001694 seq_printf(s, "tx_pkts_rcvd=%u\n", dev->tx_pkts_rcvd);
Manu Gautam354be032014-05-15 13:46:33 +05301695 seq_printf(s, "rx_throttle = %lu\n", dev->rx_throttle);
1696 }
Saket Saurabh6481d882013-09-27 15:52:36 +05301697 return ret;
1698}
1699
1700static int uether_open(struct inode *inode, struct file *file)
1701{
1702 return single_open(file, uether_stat_show, inode->i_private);
1703}
1704
1705static 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 Gautam354be032014-05-15 13:46:33 +05301715 dev->rx_throttle = 0;
Saket Saurabh6481d882013-09-27 15:52:36 +05301716 spin_unlock_irqrestore(&dev->lock, flags);
1717 return count;
1718}
1719
1720static const struct file_operations uether_stats_ops = {
1721 .open = uether_open,
1722 .read = seq_read,
1723 .write = uether_stat_reset,
1724};
1725
1726static 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
1743static 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 Sridharana096ba52014-09-24 18:58:23 -07001751static 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}
1760module_init(gether_init);
1761
1762static void __exit gether_exit(void)
1763{
1764 destroy_workqueue(uether_wq);
1765
1766}
1767module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001768MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001769MODULE_DESCRIPTION("ethernet over USB driver");
1770MODULE_LICENSE("GPL v2");