blob: 94fe2fab267c7f7eba60decd03e127db25e2d659 [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 */
75#define TX_REQ_THRESHOLD 5
76 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;
105 struct dentry *uether_dent;
106 struct dentry *uether_dfile;
David Brownell2b3d9422008-06-19 18:19:28 -0700107};
108
Saket Saurabh6481d882013-09-27 15:52:36 +0530109static void uether_debugfs_init(struct eth_dev *dev);
110static void uether_debugfs_exit(struct eth_dev *dev);
111
David Brownell2b3d9422008-06-19 18:19:28 -0700112/*-------------------------------------------------------------------------*/
113
114#define RX_EXTRA 20 /* bytes guarding against rx overflows */
115
116#define DEFAULT_QLEN 2 /* double buffering by default */
117
Vamsi Krishna11bdb3b2014-05-14 10:57:08 -0700118/*
119 * Usually downlink rates are higher than uplink rates and it
120 * deserve higher number of requests. For CAT-6 data rates of
121 * 300Mbps (~30 packets per milli-sec) 40 usb request may not
122 * be sufficient. At this rate and with interrupt moderation
123 * of interconnect, data can be very bursty. tx_qmult is the
124 * additional multipler on qmult.
125 */
126static unsigned int tx_qmult = 1;
127module_param(tx_qmult, uint, 0644);
128MODULE_PARM_DESC(tx_qmult, "Additional queue length multiplier for tx");
129
Paul Zimmerman04617db2011-06-27 14:13:18 -0700130/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200131static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700132{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700133 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
134 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700135 return qmult * DEFAULT_QLEN;
136 else
137 return DEFAULT_QLEN;
138}
139
140/*-------------------------------------------------------------------------*/
141
142/* REVISIT there must be a better way than having two sets
143 * of debug calls ...
144 */
145
146#undef DBG
147#undef VDBG
148#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700149#undef INFO
150
151#define xprintk(d, level, fmt, args...) \
152 printk(level "%s: " fmt , (d)->net->name , ## args)
153
154#ifdef DEBUG
155#undef DEBUG
156#define DBG(dev, fmt, args...) \
157 xprintk(dev , KERN_DEBUG , fmt , ## args)
158#else
159#define DBG(dev, fmt, args...) \
160 do { } while (0)
161#endif /* DEBUG */
162
163#ifdef VERBOSE_DEBUG
164#define VDBG DBG
165#else
166#define VDBG(dev, fmt, args...) \
167 do { } while (0)
168#endif /* DEBUG */
169
170#define ERROR(dev, fmt, args...) \
171 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700172#define INFO(dev, fmt, args...) \
173 xprintk(dev , KERN_INFO , fmt , ## args)
174
175/*-------------------------------------------------------------------------*/
176
177/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
178
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800179static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700180{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100181 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
182 return -ERANGE;
183 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700184
Mike Looijmansab738ff2015-11-30 12:18:23 +0100185 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700186}
187
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800188static int ueth_change_mtu_ip(struct net_device *net, int new_mtu)
189{
190 struct eth_dev *dev = netdev_priv(net);
191 unsigned long flags;
192 int status = 0;
193
194 spin_lock_irqsave(&dev->lock, flags);
195 if (new_mtu <= 0)
196 status = -EINVAL;
197 else
198 net->mtu = new_mtu;
199
200 DBG(dev, "[%s] MTU change: old=%d new=%d\n", net->name,
201 net->mtu, new_mtu);
202 spin_unlock_irqrestore(&dev->lock, flags);
203
204 return status;
205}
206
David Brownell2b3d9422008-06-19 18:19:28 -0700207static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
208{
Jiri Pirko7826d432013-01-06 00:44:26 +0000209 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700210
Jiri Pirko7826d432013-01-06 00:44:26 +0000211 strlcpy(p->driver, "g_ether", sizeof(p->driver));
212 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
213 strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
214 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
David Brownell2b3d9422008-06-19 18:19:28 -0700215}
216
David Brownell2b3d9422008-06-19 18:19:28 -0700217/* REVISIT can also support:
218 * - WOL (by tracking suspends and issuing remote wakeup)
219 * - msglevel (implies updated messaging)
220 * - ... probably more ethtool ops
221 */
222
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700223static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700224 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700225 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700226};
227
228static void defer_kevent(struct eth_dev *dev, int flag)
229{
230 if (test_and_set_bit(flag, &dev->todo))
231 return;
232 if (!schedule_work(&dev->work))
233 ERROR(dev, "kevent %d may have been dropped\n", flag);
234 else
235 DBG(dev, "kevent %d scheduled\n", flag);
236}
237
238static void rx_complete(struct usb_ep *ep, struct usb_request *req);
239
240static int
241rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
242{
243 struct sk_buff *skb;
244 int retval = -ENOMEM;
245 size_t size = 0;
246 struct usb_ep *out;
247 unsigned long flags;
248
249 spin_lock_irqsave(&dev->lock, flags);
250 if (dev->port_usb)
251 out = dev->port_usb->out_ep;
252 else
253 out = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -0700254
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530255 if (!out) {
256 spin_unlock_irqrestore(&dev->lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700257 return -ENOTCONN;
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530258 }
David Brownell2b3d9422008-06-19 18:19:28 -0700259
260 /* Padding up to RX_EXTRA handles minor disagreements with host.
261 * Normally we use the USB "terminate on short read" convention;
262 * so allow up to (N*maxpacket), since that memory is normally
263 * already allocated. Some hardware doesn't deal well with short
264 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
265 * byte off the end (to force hardware errors on overflow).
266 *
267 * RNDIS uses internal framing, and explicitly allows senders to
268 * pad to end-of-packet. That's potentially nice for speed, but
269 * means receivers can't recover lost synch on their own (because
270 * new packets don't only start after a short RX).
271 */
272 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
273 size += dev->port_usb->header_len;
274 size += out->maxpacket - 1;
275 size -= size % out->maxpacket;
276
xerox_lin87bebf82014-08-14 14:48:44 +0800277 if (dev->ul_max_pkts_per_xfer)
278 size *= dev->ul_max_pkts_per_xfer;
279
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200280 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800281 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530282 spin_unlock_irqrestore(&dev->lock, flags);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200283
Amit Pundir5ff0eb22016-01-08 19:36:02 +0530284 DBG(dev, "%s: size: %zd\n", __func__, size);
David Brownell2b3d9422008-06-19 18:19:28 -0700285 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
286 if (skb == NULL) {
287 DBG(dev, "no rx skb\n");
288 goto enomem;
289 }
290
291 /* Some platforms perform better when IP packets are aligned,
292 * but on at least one, checksumming fails otherwise. Note:
293 * RNDIS headers involve variable numbers of LE32 values.
294 */
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900295 if (likely(!dev->no_skb_reserve))
296 skb_reserve(skb, NET_IP_ALIGN);
David Brownell2b3d9422008-06-19 18:19:28 -0700297
298 req->buf = skb->data;
299 req->length = size;
300 req->complete = rx_complete;
301 req->context = skb;
302
303 retval = usb_ep_queue(out, req, gfp_flags);
304 if (retval == -ENOMEM)
305enomem:
306 defer_kevent(dev, WORK_RX_MEMORY);
307 if (retval) {
308 DBG(dev, "rx submit --> %d\n", retval);
309 if (skb)
310 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700311 }
312 return retval;
313}
314
315static void rx_complete(struct usb_ep *ep, struct usb_request *req)
316{
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700317 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700318 struct eth_dev *dev = ep->driver_data;
319 int status = req->status;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700320 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700321
322 switch (status) {
323
324 /* normal completion */
325 case 0:
326 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500327
328 if (dev->unwrap) {
329 unsigned long flags;
330
331 spin_lock_irqsave(&dev->lock, flags);
332 if (dev->port_usb) {
333 status = dev->unwrap(dev->port_usb,
334 skb,
335 &dev->rx_frames);
Badhri Jagan Sridharan8424b3e2014-09-18 10:48:48 -0700336 if (status == -EINVAL)
337 dev->net->stats.rx_errors++;
338 else if (status == -EOVERFLOW)
339 dev->net->stats.rx_over_errors++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500340 } else {
341 dev_kfree_skb_any(skb);
342 status = -ENOTCONN;
343 }
344 spin_unlock_irqrestore(&dev->lock, flags);
345 } else {
346 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700347 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700348 if (!status)
349 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700350 break;
351
352 /* software-driven interface shutdown */
353 case -ECONNRESET: /* unlink */
354 case -ESHUTDOWN: /* disconnect etc */
355 VDBG(dev, "rx shutdown, code %d\n", status);
356 goto quiesce;
357
358 /* for hardware automagic (such as pxa) */
359 case -ECONNABORTED: /* endpoint reset */
360 DBG(dev, "rx %s reset\n", ep->name);
361 defer_kevent(dev, WORK_RX_MEMORY);
362quiesce:
363 dev_kfree_skb_any(skb);
364 goto clean;
365
366 /* data overrun */
367 case -EOVERFLOW:
368 dev->net->stats.rx_over_errors++;
369 /* FALLTHROUGH */
370
371 default:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700372 queue = 1;
373 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700374 dev->net->stats.rx_errors++;
375 DBG(dev, "rx status %d\n", status);
376 break;
377 }
378
David Brownell2b3d9422008-06-19 18:19:28 -0700379clean:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700380 spin_lock(&dev->req_lock);
381 list_add(&req->list, &dev->rx_reqs);
382 spin_unlock(&dev->req_lock);
383
384 if (queue)
385 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700386}
387
388static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
389{
390 unsigned i;
391 struct usb_request *req;
392
393 if (!n)
394 return -ENOMEM;
395
396 /* queue/recycle up to N requests */
397 i = n;
398 list_for_each_entry(req, list, list) {
399 if (i-- == 0)
400 goto extra;
401 }
402 while (i--) {
403 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
404 if (!req)
405 return list_empty(list) ? -ENOMEM : 0;
406 list_add(&req->list, list);
407 }
408 return 0;
409
410extra:
411 /* free extras */
412 for (;;) {
413 struct list_head *next;
414
415 next = req->list.next;
416 list_del(&req->list);
417 usb_ep_free_request(ep, req);
418
419 if (next == list)
420 break;
421
422 req = container_of(next, struct usb_request, list);
423 }
424 return 0;
425}
426
427static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
428{
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800429 int status = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700430
431 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800432 if (link->in_ep) {
Vamsi Krishna11bdb3b2014-05-14 10:57:08 -0700433 status = prealloc(&dev->tx_reqs, link->in_ep, n * tx_qmult);
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800434 if (status < 0)
435 goto fail;
436 }
437
438 if (link->out_ep) {
439 status = prealloc(&dev->rx_reqs, link->out_ep, n);
440 if (status < 0)
441 goto fail;
442 }
David Brownell2b3d9422008-06-19 18:19:28 -0700443 goto done;
444fail:
445 DBG(dev, "can't alloc requests\n");
446done:
447 spin_unlock(&dev->req_lock);
448 return status;
449}
450
451static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
452{
453 struct usb_request *req;
454 unsigned long flags;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700455 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700456
457 /* fill unused rxq slots with some skb */
458 spin_lock_irqsave(&dev->req_lock, flags);
459 while (!list_empty(&dev->rx_reqs)) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700460 /* break the nexus of continuous completion and re-submission*/
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600461 if (++req_cnt > qlen(dev->gadget, dev->qmult))
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700462 break;
463
David Brownell2b3d9422008-06-19 18:19:28 -0700464 req = container_of(dev->rx_reqs.next,
465 struct usb_request, list);
466 list_del_init(&req->list);
467 spin_unlock_irqrestore(&dev->req_lock, flags);
468
469 if (rx_submit(dev, req, gfp_flags) < 0) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700470 spin_lock_irqsave(&dev->req_lock, flags);
471 list_add(&req->list, &dev->rx_reqs);
472 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700473 defer_kevent(dev, WORK_RX_MEMORY);
474 return;
475 }
476
477 spin_lock_irqsave(&dev->req_lock, flags);
478 }
479 spin_unlock_irqrestore(&dev->req_lock, flags);
480}
481
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700482static void process_rx_w(struct work_struct *work)
483{
484 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
485 struct sk_buff *skb;
486 int status = 0;
487
488 if (!dev->port_usb)
489 return;
490
491 while ((skb = skb_dequeue(&dev->rx_frames))) {
492 if (status < 0
493 || ETH_HLEN > skb->len
494 || skb->len > ETH_FRAME_LEN) {
495 dev->net->stats.rx_errors++;
496 dev->net->stats.rx_length_errors++;
497 DBG(dev, "rx length %d\n", skb->len);
498 dev_kfree_skb_any(skb);
499 continue;
500 }
501 skb->protocol = eth_type_trans(skb, dev->net);
502 dev->net->stats.rx_packets++;
503 dev->net->stats.rx_bytes += skb->len;
504
505 status = netif_rx_ni(skb);
506 }
507
508 if (netif_running(dev->net))
509 rx_fill(dev, GFP_KERNEL);
510}
511
David Brownell2b3d9422008-06-19 18:19:28 -0700512static void eth_work(struct work_struct *work)
513{
514 struct eth_dev *dev = container_of(work, struct eth_dev, work);
515
516 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
517 if (netif_running(dev->net))
518 rx_fill(dev, GFP_KERNEL);
519 }
520
521 if (dev->todo)
522 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
523}
524
525static void tx_complete(struct usb_ep *ep, struct usb_request *req)
526{
527 struct sk_buff *skb = req->context;
528 struct eth_dev *dev = ep->driver_data;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700529 struct net_device *net = dev->net;
530 struct usb_request *new_req;
531 struct usb_ep *in;
532 int length;
533 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700534
Rajkumar Raghupathyd8682452013-01-28 11:48:47 +0530535 if (!dev->port_usb) {
536 usb_ep_free_request(ep, req);
537 return;
538 }
539
David Brownell2b3d9422008-06-19 18:19:28 -0700540 switch (req->status) {
541 default:
542 dev->net->stats.tx_errors++;
543 VDBG(dev, "tx err %d\n", req->status);
544 /* FALLTHROUGH */
545 case -ECONNRESET: /* unlink */
546 case -ESHUTDOWN: /* disconnect etc */
547 break;
548 case 0:
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700549 if (!req->zero)
550 dev->net->stats.tx_bytes += req->length-1;
551 else
552 dev->net->stats.tx_bytes += req->length;
David Brownell2b3d9422008-06-19 18:19:28 -0700553 }
554 dev->net->stats.tx_packets++;
555
556 spin_lock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700557 list_add_tail(&req->list, &dev->tx_reqs);
558
Manu Gautamf8afc312014-01-29 16:59:21 +0530559 if (dev->port_usb->multi_pkt_xfer && !req->context) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700560 dev->no_tx_req_used--;
561 req->length = 0;
562 in = dev->port_usb->in_ep;
563
564 if (!list_empty(&dev->tx_reqs)) {
565 new_req = container_of(dev->tx_reqs.next,
566 struct usb_request, list);
567 list_del(&new_req->list);
568 spin_unlock(&dev->req_lock);
569 if (new_req->length > 0) {
570 length = new_req->length;
571
572 /* NCM requires no zlp if transfer is
573 * dwNtbInMaxSize */
574 if (dev->port_usb->is_fixed &&
575 length == dev->port_usb->fixed_in_len &&
576 (length % in->maxpacket) == 0)
577 new_req->zero = 0;
578 else
579 new_req->zero = 1;
580
581 /* use zlp framing on tx for strict CDC-Ether
582 * conformance, though any robust network rx
583 * path ignores extra padding. and some hardware
584 * doesn't like to write zlps.
585 */
586 if (new_req->zero && !dev->zlp &&
587 (length % in->maxpacket) == 0) {
588 new_req->zero = 0;
589 length++;
590 }
591
592 new_req->length = length;
593 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
594 switch (retval) {
595 default:
596 DBG(dev, "tx queue err %d\n", retval);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530597 new_req->length = 0;
598 spin_lock(&dev->req_lock);
599 list_add_tail(&new_req->list,
600 &dev->tx_reqs);
601 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700602 break;
603 case 0:
604 spin_lock(&dev->req_lock);
605 dev->no_tx_req_used++;
606 spin_unlock(&dev->req_lock);
Amit Pundir09172082016-05-30 15:19:21 +0530607 netif_trans_update(net);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700608 }
609 } else {
610 spin_lock(&dev->req_lock);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530611 /*
612 * Put the idle request at the back of the
613 * queue. The xmit function will put the
614 * unfinished request at the beginning of the
615 * queue.
616 */
617 list_add_tail(&new_req->list, &dev->tx_reqs);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700618 spin_unlock(&dev->req_lock);
619 }
620 } else {
621 spin_unlock(&dev->req_lock);
622 }
623 } else {
Manu Gautamf8afc312014-01-29 16:59:21 +0530624 /* Is aggregation already enabled and buffers allocated ? */
625 if (dev->port_usb->multi_pkt_xfer && dev->tx_req_bufsize) {
626 req->buf = kzalloc(dev->tx_req_bufsize, GFP_ATOMIC);
627 req->context = NULL;
628 } else {
629 req->buf = NULL;
630 }
631
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700632 spin_unlock(&dev->req_lock);
633 dev_kfree_skb_any(skb);
634 }
David Brownell2b3d9422008-06-19 18:19:28 -0700635
David Brownell2b3d9422008-06-19 18:19:28 -0700636 if (netif_carrier_ok(dev->net))
637 netif_wake_queue(dev->net);
638}
639
640static inline int is_promisc(u16 cdc_filter)
641{
642 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
643}
644
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530645static int alloc_tx_buffer(struct eth_dev *dev)
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700646{
647 struct list_head *act;
648 struct usb_request *req;
649
xerox_lincdffcb82014-09-04 16:01:59 +0800650 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700651 (dev->net->mtu
652 + sizeof(struct ethhdr)
653 /* size of rndis_packet_msg_type */
654 + 44
655 + 22));
656
657 list_for_each(act, &dev->tx_reqs) {
658 req = container_of(act, struct usb_request, list);
659 if (!req->buf)
660 req->buf = kmalloc(dev->tx_req_bufsize,
661 GFP_ATOMIC);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530662
663 if (!req->buf)
664 goto free_buf;
Manu Gautamf8afc312014-01-29 16:59:21 +0530665
666 /* req->context is not used for multi_pkt_xfers */
667 req->context = NULL;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700668 }
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530669 return 0;
670
671free_buf:
672 /* tx_req_bufsize = 0 retries mem alloc on next eth_start_xmit */
673 dev->tx_req_bufsize = 0;
674 list_for_each(act, &dev->tx_reqs) {
675 req = container_of(act, struct usb_request, list);
676 kfree(req->buf);
677 req->buf = NULL;
678 }
679 return -ENOMEM;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700680}
681
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000682static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
683 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700684{
685 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100686 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700687 int retval;
688 struct usb_request *req = NULL;
689 unsigned long flags;
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700690 struct usb_ep *in = NULL;
691 u16 cdc_filter = 0;
Mayank Rana68f74742013-02-15 14:55:30 +0530692 bool multi_pkt_xfer = false;
David Brownell2b3d9422008-06-19 18:19:28 -0700693
694 spin_lock_irqsave(&dev->lock, flags);
695 if (dev->port_usb) {
696 in = dev->port_usb->in_ep;
697 cdc_filter = dev->port_usb->cdc_filter;
Mayank Rana68f74742013-02-15 14:55:30 +0530698 multi_pkt_xfer = dev->port_usb->multi_pkt_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -0700699 }
700 spin_unlock_irqrestore(&dev->lock, flags);
701
Jim Baxter6d3865f2014-07-07 18:33:18 +0100702 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700703 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000704 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700705 }
706
707 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100708 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700709 u8 *dest = skb->data;
710
711 if (is_multicast_ether_addr(dest)) {
712 u16 type;
713
714 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
715 * SET_ETHERNET_MULTICAST_FILTERS requests
716 */
717 if (is_broadcast_ether_addr(dest))
718 type = USB_CDC_PACKET_TYPE_BROADCAST;
719 else
720 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
721 if (!(cdc_filter & type)) {
722 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000723 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700724 }
725 }
726 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
727 }
728
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700729 /*
730 * no buffer copies needed, unless the network stack did it
731 * or the hardware can't use skb buffers.
732 * or there's not enough space for extra headers we need
733 */
734 spin_lock_irqsave(&dev->lock, flags);
735 if (dev->wrap && dev->port_usb)
736 skb = dev->wrap(dev->port_usb, skb);
737 spin_unlock_irqrestore(&dev->lock, flags);
738
739 if (!skb) {
740 if (dev->port_usb && dev->port_usb->supports_multi_frame) {
741 /*
742 * Multi frame CDC protocols may store the frame for
743 * later which is not a dropped frame.
744 */
745 } else {
746 dev->net->stats.tx_dropped++;
747 }
748
749 /* no error code for dropped packets */
750 return NETDEV_TX_OK;
751 }
752
753 /* Allocate memory for tx_reqs to support multi packet transfer */
David Brownell2b3d9422008-06-19 18:19:28 -0700754 spin_lock_irqsave(&dev->req_lock, flags);
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700755 if (multi_pkt_xfer && !dev->tx_req_bufsize) {
756 retval = alloc_tx_buffer(dev);
757 if (retval < 0) {
758 spin_unlock_irqrestore(&dev->req_lock, flags);
759 return -ENOMEM;
760 }
761 }
762
David Brownell2b3d9422008-06-19 18:19:28 -0700763 /*
764 * this freelist can be empty if an interrupt triggered disconnect()
765 * and reconfigured the gadget (shutting down this queue) after the
766 * network stack decided to xmit but before we got the spinlock.
767 */
768 if (list_empty(&dev->tx_reqs)) {
769 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000770 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700771 }
772
773 req = container_of(dev->tx_reqs.next, struct usb_request, list);
774 list_del(&req->list);
775
776 /* temporarily stop TX queue when the freelist empties */
Saket Saurabh6481d882013-09-27 15:52:36 +0530777 if (list_empty(&dev->tx_reqs)) {
778 /*
779 * tx_throttle gives info about number of times u_ether
780 * asked network layer to stop queueing packets to it
781 * when transmit resources are unavailable
782 */
783 dev->tx_throttle++;
David Brownell2b3d9422008-06-19 18:19:28 -0700784 netif_stop_queue(net);
Saket Saurabh6481d882013-09-27 15:52:36 +0530785 }
David Brownell2b3d9422008-06-19 18:19:28 -0700786
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700787 dev->tx_skb_hold_count++;
788 spin_unlock_irqrestore(&dev->req_lock, flags);
789
Mayank Rana68f74742013-02-15 14:55:30 +0530790 if (multi_pkt_xfer) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700791 memcpy(req->buf + req->length, skb->data, skb->len);
792 req->length = req->length + skb->len;
793 length = req->length;
794 dev_kfree_skb_any(skb);
795
796 spin_lock_irqsave(&dev->req_lock, flags);
xerox_lincdffcb82014-09-04 16:01:59 +0800797 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700798 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
799 list_add(&req->list, &dev->tx_reqs);
800 spin_unlock_irqrestore(&dev->req_lock, flags);
801 goto success;
802 }
803 }
804
805 dev->no_tx_req_used++;
806 spin_unlock_irqrestore(&dev->req_lock, flags);
807
808 spin_lock_irqsave(&dev->lock, flags);
809 dev->tx_skb_hold_count = 0;
810 spin_unlock_irqrestore(&dev->lock, flags);
811 } else {
812 length = skb->len;
813 req->buf = skb->data;
814 req->context = skb;
815 }
816
David Brownell2b3d9422008-06-19 18:19:28 -0700817 req->complete = tx_complete;
818
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200819 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
Harish Jenny K N79775f42016-09-09 11:30:41 +0200820 if (dev->port_usb &&
821 dev->port_usb->is_fixed &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200822 length == dev->port_usb->fixed_in_len &&
823 (length % in->maxpacket) == 0)
824 req->zero = 0;
825 else
826 req->zero = 1;
827
David Brownell2b3d9422008-06-19 18:19:28 -0700828 /* use zlp framing on tx for strict CDC-Ether conformance,
829 * though any robust network rx path ignores extra padding.
830 * and some hardware doesn't like to write zlps.
831 */
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700832 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
833 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700834 length++;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700835 }
David Brownell2b3d9422008-06-19 18:19:28 -0700836
837 req->length = length;
838
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700839 /* throttle highspeed IRQ rate back slightly */
840 if (gadget_is_dualspeed(dev->gadget) &&
841 (dev->gadget->speed == USB_SPEED_HIGH)) {
842 dev->tx_qlen++;
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600843 if (dev->tx_qlen == (dev->qmult/2)) {
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700844 req->no_interrupt = 0;
845 dev->tx_qlen = 0;
846 } else {
847 req->no_interrupt = 1;
848 }
849 } else {
850 req->no_interrupt = 0;
851 }
David Brownell2b3d9422008-06-19 18:19:28 -0700852
853 retval = usb_ep_queue(in, req, GFP_ATOMIC);
854 switch (retval) {
855 default:
856 DBG(dev, "tx queue err %d\n", retval);
857 break;
858 case 0:
Florian Westphal860e9532016-05-03 16:33:13 +0200859 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700860 }
861
862 if (retval) {
Mayank Rana68f74742013-02-15 14:55:30 +0530863 if (!multi_pkt_xfer)
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700864 dev_kfree_skb_any(skb);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530865 else
866 req->length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700867 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700868 spin_lock_irqsave(&dev->req_lock, flags);
869 if (list_empty(&dev->tx_reqs))
870 netif_start_queue(net);
871 list_add(&req->list, &dev->tx_reqs);
872 spin_unlock_irqrestore(&dev->req_lock, flags);
873 }
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700874success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000875 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700876}
877
878/*-------------------------------------------------------------------------*/
879
880static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
881{
882 DBG(dev, "%s\n", __func__);
883
884 /* fill the rx queue */
885 rx_fill(dev, gfp_flags);
886
887 /* and open the tx floodgates */
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700888 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700889 netif_wake_queue(dev->net);
890}
891
892static int eth_open(struct net_device *net)
893{
894 struct eth_dev *dev = netdev_priv(net);
895 struct gether *link;
896
897 DBG(dev, "%s\n", __func__);
898 if (netif_carrier_ok(dev->net))
899 eth_start(dev, GFP_KERNEL);
900
901 spin_lock_irq(&dev->lock);
902 link = dev->port_usb;
903 if (link && link->open)
904 link->open(link);
905 spin_unlock_irq(&dev->lock);
906
907 return 0;
908}
909
910static int eth_stop(struct net_device *net)
911{
912 struct eth_dev *dev = netdev_priv(net);
913 unsigned long flags;
914
915 VDBG(dev, "%s\n", __func__);
916 netif_stop_queue(net);
917
918 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
919 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
920 dev->net->stats.rx_errors, dev->net->stats.tx_errors
921 );
922
923 /* ensure there are no more active requests */
924 spin_lock_irqsave(&dev->lock, flags);
925 if (dev->port_usb) {
926 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200927 const struct usb_endpoint_descriptor *in;
928 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700929
930 if (link->close)
931 link->close(link);
932
933 /* NOTE: we have no abort-queue primitive we could use
934 * to cancel all pending I/O. Instead, we disable then
935 * reenable the endpoints ... this idiom may leave toggle
936 * wrong, but that's a self-correcting error.
937 *
938 * REVISIT: we *COULD* just let the transfers complete at
939 * their own pace; the network stack can handle old packets.
940 * For the moment we leave this here, since it works.
941 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800942 if (link->in_ep) {
943 in = link->in_ep->desc;
944 usb_ep_disable(link->in_ep);
945 if (netif_carrier_ok(net)) {
946 DBG(dev, "host still using in endpoints\n");
947 link->in_ep->desc = in;
948 usb_ep_enable(link->in_ep);
949 }
950 }
951
952 if (link->out_ep) {
953 out = link->out_ep->desc;
954 usb_ep_disable(link->out_ep);
955 if (netif_carrier_ok(net)) {
956 DBG(dev, "host still using out endpoints\n");
957 link->out_ep->desc = out;
958 usb_ep_enable(link->out_ep);
959 }
David Brownell2b3d9422008-06-19 18:19:28 -0700960 }
961 }
962 spin_unlock_irqrestore(&dev->lock, flags);
963
964 return 0;
965}
966
967/*-------------------------------------------------------------------------*/
968
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200969static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700970{
971 if (str) {
972 unsigned i;
973
974 for (i = 0; i < 6; i++) {
975 unsigned char num;
976
977 if ((*str == '.') || (*str == ':'))
978 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300979 num = hex_to_bin(*str++) << 4;
980 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700981 dev_addr [i] = num;
982 }
983 if (is_valid_ether_addr(dev_addr))
984 return 0;
985 }
Joe Perches006c9132012-07-12 22:33:11 -0700986 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700987 return 1;
988}
989
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200990static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
991{
992 if (len < 18)
993 return -EINVAL;
994
Andy Shevchenko27f38702015-01-15 13:40:04 +0200995 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200996 return 18;
997}
998
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800999static int ether_ioctl(struct net_device *, struct ifreq *, int);
1000
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001001static const struct net_device_ops eth_netdev_ops = {
1002 .ndo_open = eth_open,
1003 .ndo_stop = eth_stop,
1004 .ndo_start_xmit = eth_start_xmit,
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001005 .ndo_do_ioctl = ether_ioctl,
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001006 .ndo_change_mtu = ueth_change_mtu,
1007 .ndo_set_mac_address = eth_mac_addr,
1008 .ndo_validate_addr = eth_validate_addr,
1009};
David Brownell2b3d9422008-06-19 18:19:28 -07001010
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001011static const struct net_device_ops eth_netdev_ops_ip = {
1012 .ndo_open = eth_open,
1013 .ndo_stop = eth_stop,
1014 .ndo_start_xmit = eth_start_xmit,
1015 .ndo_do_ioctl = ether_ioctl,
1016 .ndo_change_mtu = ueth_change_mtu_ip,
1017 .ndo_set_mac_address = NULL,
1018 .ndo_validate_addr = NULL,
1019};
1020
1021static int rmnet_ioctl_extended(struct net_device *dev, struct ifreq *ifr)
1022{
1023 struct rmnet_ioctl_extended_s ext_cmd;
1024 struct eth_dev *eth_dev = netdev_priv(dev);
1025 int rc = 0;
1026
1027 rc = copy_from_user(&ext_cmd, ifr->ifr_ifru.ifru_data,
1028 sizeof(struct rmnet_ioctl_extended_s));
1029
1030 if (rc) {
1031 DBG(eth_dev, "%s(): copy_from_user() failed\n", __func__);
1032 return rc;
1033 }
1034
1035 switch (ext_cmd.extended_ioctl) {
1036 case RMNET_IOCTL_GET_SUPPORTED_FEATURES:
1037 ext_cmd.u.data = 0;
1038 break;
1039
1040 case RMNET_IOCTL_SET_MRU:
1041 if (netif_running(dev))
1042 return -EBUSY;
1043
1044 /* 16K max */
1045 if ((size_t)ext_cmd.u.data > 0x4000)
1046 return -EINVAL;
1047
1048 if (eth_dev->port_usb) {
1049 eth_dev->port_usb->is_fixed = true;
1050 eth_dev->port_usb->fixed_out_len =
1051 (size_t) ext_cmd.u.data;
1052 DBG(eth_dev, "[%s] rmnet_ioctl(): SET MRU to %u\n",
1053 dev->name, eth_dev->port_usb->fixed_out_len);
1054 } else {
1055 pr_err("[%s]: %s: SET MRU failed. Cable disconnected\n",
1056 dev->name, __func__);
1057 return -ENODEV;
1058 }
1059 break;
1060
1061 case RMNET_IOCTL_GET_MRU:
1062 if (eth_dev->port_usb) {
1063 ext_cmd.u.data = eth_dev->port_usb->is_fixed ?
1064 eth_dev->port_usb->fixed_out_len :
1065 dev->mtu;
1066 } else {
1067 pr_err("[%s]: %s: GET MRU failed. Cable disconnected\n",
1068 dev->name, __func__);
1069 return -ENODEV;
1070 }
1071 break;
1072
1073 case RMNET_IOCTL_GET_DRIVER_NAME:
1074 strlcpy(ext_cmd.u.if_name, dev->name,
1075 sizeof(ext_cmd.u.if_name));
1076 break;
1077
1078 default:
1079 break;
1080 }
1081
1082 rc = copy_to_user(ifr->ifr_ifru.ifru_data, &ext_cmd,
1083 sizeof(struct rmnet_ioctl_extended_s));
1084
1085 if (rc)
1086 DBG(eth_dev, "%s(): copy_to_user() failed\n", __func__);
1087 return rc;
1088}
1089
1090static int ether_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1091{
1092 struct eth_dev *eth_dev = netdev_priv(dev);
1093 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1094 int prev_mtu = dev->mtu;
1095 u32 state, old_opmode;
1096 int rc = -EFAULT;
1097
1098 old_opmode = eth_dev->flags;
1099 /* Process IOCTL command */
1100 switch (cmd) {
1101 case RMNET_IOCTL_SET_LLP_ETHERNET: /*Set Ethernet protocol*/
1102 /* Perform Ethernet config only if in IP mode currently*/
1103 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags)) {
1104 ether_setup(dev);
1105 dev->mtu = prev_mtu;
1106 dev->netdev_ops = &eth_netdev_ops;
1107 clear_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1108 set_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1109 DBG(eth_dev, "[%s] ioctl(): set Ethernet proto mode\n",
1110 dev->name);
1111 }
1112 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags))
1113 rc = 0;
1114 break;
1115
1116 case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol*/
1117 /* Perform IP config only if in Ethernet mode currently*/
1118 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags)) {
1119 /* Undo config done in ether_setup() */
1120 dev->header_ops = NULL; /* No header */
1121 dev->type = ARPHRD_RAWIP;
1122 dev->hard_header_len = 0;
1123 dev->mtu = prev_mtu;
1124 dev->addr_len = 0;
1125 dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
1126 dev->netdev_ops = &eth_netdev_ops_ip;
1127 clear_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1128 set_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1129 DBG(eth_dev, "[%s] ioctl(): set IP protocol mode\n",
1130 dev->name);
1131 }
1132 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags))
1133 rc = 0;
1134 break;
1135
1136 case RMNET_IOCTL_GET_LLP: /* Get link protocol state */
1137 state = eth_dev->flags & (RMNET_MODE_LLP_ETH
1138 | RMNET_MODE_LLP_IP);
1139 if (copy_to_user(addr, &state, sizeof(state)))
1140 break;
1141 rc = 0;
1142 break;
1143
1144 case RMNET_IOCTL_SET_RX_HEADROOM: /* Set RX headroom */
1145 if (copy_from_user(&eth_dev->rx_needed_headroom, addr,
1146 sizeof(eth_dev->rx_needed_headroom)))
1147 break;
1148 DBG(eth_dev, "[%s] ioctl(): set RX HEADROOM: %x\n",
1149 dev->name, eth_dev->rx_needed_headroom);
1150 rc = 0;
1151 break;
1152
1153 case RMNET_IOCTL_EXTENDED:
1154 rc = rmnet_ioctl_extended(dev, ifr);
1155 break;
1156
1157 default:
1158 pr_err("[%s] error: ioctl called for unsupported cmd[%d]",
1159 dev->name, cmd);
1160 rc = -EINVAL;
1161 }
1162
1163 DBG(eth_dev, "[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08lx\n",
1164 dev->name, __func__, cmd, old_opmode, eth_dev->flags);
1165
1166 return rc;
1167}
1168
Marcel Holtmannaa790742010-01-15 22:13:58 -08001169static struct device_type gadget_type = {
1170 .name = "gadget",
1171};
1172
David Brownell2b3d9422008-06-19 18:19:28 -07001173/**
Mike Lockwood036e98b2012-05-10 10:08:02 +02001174 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -07001175 * @g: gadget to associated with these links
1176 * @ethaddr: NULL, or a buffer in which the ethernet address of the
1177 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +02001178 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -07001179 * Context: may sleep
1180 *
1181 * This sets up the single network link that may be exported by a
1182 * gadget driver using this framework. The link layer addresses are
1183 * set up using module parameters.
1184 *
Dan Carpenter574f24f2013-11-14 11:42:11 +03001185 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -07001186 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001187struct eth_dev *gether_setup_name(struct usb_gadget *g,
1188 const char *dev_addr, const char *host_addr,
1189 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -07001190{
1191 struct eth_dev *dev;
1192 struct net_device *net;
1193 int status;
1194
David Brownell2b3d9422008-06-19 18:19:28 -07001195 net = alloc_etherdev(sizeof *dev);
1196 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001197 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -07001198
1199 dev = netdev_priv(net);
1200 spin_lock_init(&dev->lock);
1201 spin_lock_init(&dev->req_lock);
1202 INIT_WORK(&dev->work, eth_work);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001203 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -07001204 INIT_LIST_HEAD(&dev->tx_reqs);
1205 INIT_LIST_HEAD(&dev->rx_reqs);
1206
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001207 skb_queue_head_init(&dev->rx_frames);
1208
David Brownell2b3d9422008-06-19 18:19:28 -07001209 /* network device setup */
1210 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001211 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +02001212 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -07001213
1214 if (get_ether_addr(dev_addr, net->dev_addr))
1215 dev_warn(&g->dev,
1216 "using random %s ethernet address\n", "self");
1217 if (get_ether_addr(host_addr, dev->host_mac))
1218 dev_warn(&g->dev,
1219 "using random %s ethernet address\n", "host");
1220
1221 if (ethaddr)
1222 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
1223
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001224 net->netdev_ops = &eth_netdev_ops;
1225
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001226 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -07001227
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001228 /* set operation mode to eth by default */
1229 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1230
David Brownell2b3d9422008-06-19 18:19:28 -07001231 dev->gadget = g;
1232 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -08001233 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -07001234
1235 status = register_netdev(net);
1236 if (status < 0) {
1237 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1238 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001239 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -07001240 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001241 INFO(dev, "MAC %pM\n", net->dev_addr);
1242 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001243
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001244 /*
1245 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -07001246 * - iff DATA transfer is active, carrier is "on"
1247 * - tx queueing enabled if open *and* carrier is "on"
1248 */
1249 netif_carrier_off(net);
Saket Saurabh6481d882013-09-27 15:52:36 +05301250 uether_debugfs_init(dev);
David Brownell2b3d9422008-06-19 18:19:28 -07001251 }
1252
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001253 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -07001254}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001255EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -07001256
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001257struct net_device *gether_setup_name_default(const char *netname)
1258{
1259 struct net_device *net;
1260 struct eth_dev *dev;
1261
1262 net = alloc_etherdev(sizeof(*dev));
1263 if (!net)
1264 return ERR_PTR(-ENOMEM);
1265
1266 dev = netdev_priv(net);
1267 spin_lock_init(&dev->lock);
1268 spin_lock_init(&dev->req_lock);
1269 INIT_WORK(&dev->work, eth_work);
Matthew Moeller5df32222016-03-09 20:19:25 -06001270 INIT_WORK(&dev->rx_work, process_rx_w);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001271 INIT_LIST_HEAD(&dev->tx_reqs);
1272 INIT_LIST_HEAD(&dev->rx_reqs);
1273
1274 skb_queue_head_init(&dev->rx_frames);
1275
1276 /* network device setup */
1277 dev->net = net;
1278 dev->qmult = QMULT_DEFAULT;
1279 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
1280
1281 eth_random_addr(dev->dev_mac);
1282 pr_warn("using random %s ethernet address\n", "self");
1283 eth_random_addr(dev->host_mac);
1284 pr_warn("using random %s ethernet address\n", "host");
1285
1286 net->netdev_ops = &eth_netdev_ops;
1287
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001288 net->ethtool_ops = &ops;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001289
1290 /* set operation mode to eth by default */
1291 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1292
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001293 SET_NETDEV_DEVTYPE(net, &gadget_type);
1294
1295 return net;
1296}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001297EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001298
1299int gether_register_netdev(struct net_device *net)
1300{
1301 struct eth_dev *dev;
1302 struct usb_gadget *g;
1303 struct sockaddr sa;
1304 int status;
1305
1306 if (!net->dev.parent)
1307 return -EINVAL;
1308 dev = netdev_priv(net);
1309 g = dev->gadget;
1310 status = register_netdev(net);
1311 if (status < 0) {
1312 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1313 return status;
1314 } else {
1315 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
1316
1317 /* two kinds of host-initiated state changes:
1318 * - iff DATA transfer is active, carrier is "on"
1319 * - tx queueing enabled if open *and* carrier is "on"
1320 */
1321 netif_carrier_off(net);
1322 }
1323 sa.sa_family = net->type;
1324 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
1325 rtnl_lock();
1326 status = dev_set_mac_address(net, &sa);
1327 rtnl_unlock();
1328 if (status)
1329 pr_warn("cannot set self ethernet address: %d\n", status);
1330 else
1331 INFO(dev, "MAC %pM\n", dev->dev_mac);
1332
1333 return status;
1334}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001335EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001336
1337void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
1338{
1339 struct eth_dev *dev;
1340
1341 dev = netdev_priv(net);
1342 dev->gadget = g;
1343 SET_NETDEV_DEV(net, &g->dev);
1344}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001345EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001346
1347int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
1348{
1349 struct eth_dev *dev;
1350 u8 new_addr[ETH_ALEN];
1351
1352 dev = netdev_priv(net);
1353 if (get_ether_addr(dev_addr, new_addr))
1354 return -EINVAL;
1355 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
1356 return 0;
1357}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001358EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001359
1360int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
1361{
1362 struct eth_dev *dev;
1363
1364 dev = netdev_priv(net);
1365 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
1366}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001367EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001368
1369int gether_set_host_addr(struct net_device *net, const char *host_addr)
1370{
1371 struct eth_dev *dev;
1372 u8 new_addr[ETH_ALEN];
1373
1374 dev = netdev_priv(net);
1375 if (get_ether_addr(host_addr, new_addr))
1376 return -EINVAL;
1377 memcpy(dev->host_mac, new_addr, ETH_ALEN);
1378 return 0;
1379}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001380EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001381
1382int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
1383{
1384 struct eth_dev *dev;
1385
1386 dev = netdev_priv(net);
1387 return get_ether_addr_str(dev->host_mac, host_addr, len);
1388}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001389EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001390
1391int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
1392{
1393 struct eth_dev *dev;
1394
1395 if (len < 13)
1396 return -EINVAL;
1397
1398 dev = netdev_priv(net);
1399 snprintf(host_addr, len, "%pm", dev->host_mac);
1400
1401 return strlen(host_addr);
1402}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001403EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001404
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001405void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
1406{
1407 struct eth_dev *dev;
1408
1409 dev = netdev_priv(net);
1410 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1411}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001412EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001413
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001414void gether_set_qmult(struct net_device *net, unsigned qmult)
1415{
1416 struct eth_dev *dev;
1417
1418 dev = netdev_priv(net);
1419 dev->qmult = qmult;
1420}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001421EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001422
1423unsigned gether_get_qmult(struct net_device *net)
1424{
1425 struct eth_dev *dev;
1426
1427 dev = netdev_priv(net);
1428 return dev->qmult;
1429}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001430EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001431
1432int gether_get_ifname(struct net_device *net, char *name, int len)
1433{
1434 rtnl_lock();
1435 strlcpy(name, netdev_name(net), len);
1436 rtnl_unlock();
1437 return strlen(name);
1438}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001439EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001440
David Brownell2b3d9422008-06-19 18:19:28 -07001441/**
1442 * gether_cleanup - remove Ethernet-over-USB device
1443 * Context: may sleep
1444 *
1445 * This is called to free all resources allocated by @gether_setup().
1446 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001447void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001448{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001449 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001450 return;
1451
Saket Saurabh6481d882013-09-27 15:52:36 +05301452 uether_debugfs_exit(dev);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001453 unregister_netdev(dev->net);
1454 flush_work(&dev->work);
1455 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001456}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001457EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001458
David Brownell2b3d9422008-06-19 18:19:28 -07001459/**
1460 * gether_connect - notify network layer that USB link is active
1461 * @link: the USB link, set up with endpoints, descriptors matching
1462 * current device speed, and any framing wrapper(s) set up.
1463 * Context: irqs blocked
1464 *
1465 * This is called to activate endpoints and let the network layer know
1466 * the connection is active ("carrier detect"). It may cause the I/O
1467 * queues to open and start letting network packets flow, but will in
1468 * any case activate the endpoints so that they respond properly to the
1469 * USB host.
1470 *
1471 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1472 * indicate some error code (negative errno), ep->driver_data values
1473 * have been overwritten.
1474 */
1475struct net_device *gether_connect(struct gether *link)
1476{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001477 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001478 int result = 0;
1479
1480 if (!dev)
1481 return ERR_PTR(-EINVAL);
1482
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001483 if (link->in_ep) {
1484 link->in_ep->driver_data = dev;
1485 result = usb_ep_enable(link->in_ep);
1486 if (result != 0) {
1487 DBG(dev, "enable %s --> %d\n",
1488 link->in_ep->name, result);
1489 goto fail0;
1490 }
David Brownell2b3d9422008-06-19 18:19:28 -07001491 }
1492
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001493 if (link->out_ep) {
1494 link->out_ep->driver_data = dev;
1495 result = usb_ep_enable(link->out_ep);
1496 if (result != 0) {
1497 DBG(dev, "enable %s --> %d\n",
1498 link->out_ep->name, result);
1499 goto fail1;
1500 }
David Brownell2b3d9422008-06-19 18:19:28 -07001501 }
1502
1503 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001504 result = alloc_requests(dev, link, qlen(dev->gadget,
1505 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001506
1507 if (result == 0) {
1508 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001509 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001510 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001511
1512 dev->header_len = link->header_len;
1513 dev->unwrap = link->unwrap;
1514 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001515 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +08001516 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001517
1518 spin_lock(&dev->lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -07001519 dev->tx_skb_hold_count = 0;
1520 dev->no_tx_req_used = 0;
1521 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001522 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001523 if (netif_running(dev->net)) {
1524 if (link->open)
1525 link->open(link);
1526 } else {
1527 if (link->close)
1528 link->close(link);
1529 }
David Brownell2b3d9422008-06-19 18:19:28 -07001530 spin_unlock(&dev->lock);
1531
1532 netif_carrier_on(dev->net);
1533 if (netif_running(dev->net))
1534 eth_start(dev, GFP_ATOMIC);
1535
1536 /* on error, disable any endpoints */
1537 } else {
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001538 if (link->out_ep)
1539 (void) usb_ep_disable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001540fail1:
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001541 if (link->in_ep)
1542 (void) usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001543 }
1544fail0:
1545 /* caller is responsible for cleanup on error */
1546 if (result < 0)
1547 return ERR_PTR(result);
1548 return dev->net;
1549}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001550EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001551
1552/**
1553 * gether_disconnect - notify network layer that USB link is inactive
1554 * @link: the USB link, on which gether_connect() was called
1555 * Context: irqs blocked
1556 *
1557 * This is called to deactivate endpoints and let the network layer know
1558 * the connection went inactive ("no carrier").
1559 *
1560 * On return, the state is as if gether_connect() had never been called.
1561 * The endpoints are inactive, and accordingly without active USB I/O.
1562 * Pointers to endpoint descriptors and endpoint private data are nulled.
1563 */
1564void gether_disconnect(struct gether *link)
1565{
1566 struct eth_dev *dev = link->ioport;
1567 struct usb_request *req;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001568 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001569
1570 WARN_ON(!dev);
1571 if (!dev)
1572 return;
1573
1574 DBG(dev, "%s\n", __func__);
1575
1576 netif_stop_queue(dev->net);
1577 netif_carrier_off(dev->net);
1578
1579 /* disable endpoints, forcing (synchronous) completion
1580 * of all pending i/o. then free the request objects
1581 * and forget about the endpoints.
1582 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001583 if (link->in_ep) {
1584 usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001585 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001586 while (!list_empty(&dev->tx_reqs)) {
1587 req = container_of(dev->tx_reqs.next,
1588 struct usb_request, list);
1589 list_del(&req->list);
David Brownell2b3d9422008-06-19 18:19:28 -07001590
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001591 spin_unlock(&dev->req_lock);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +05301592 if (link->multi_pkt_xfer) {
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001593 kfree(req->buf);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +05301594 req->buf = NULL;
1595 }
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001596 usb_ep_free_request(link->in_ep, req);
1597 spin_lock(&dev->req_lock);
1598 }
David Brownell2b3d9422008-06-19 18:19:28 -07001599 spin_unlock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001600 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001601 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001602
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001603 if (link->out_ep) {
1604 usb_ep_disable(link->out_ep);
1605 spin_lock(&dev->req_lock);
1606 while (!list_empty(&dev->rx_reqs)) {
1607 req = container_of(dev->rx_reqs.next,
1608 struct usb_request, list);
1609 list_del(&req->list);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001610
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001611 spin_unlock(&dev->req_lock);
1612 usb_ep_free_request(link->out_ep, req);
1613 spin_lock(&dev->req_lock);
1614 }
1615 spin_unlock(&dev->req_lock);
1616
1617 spin_lock(&dev->rx_frames.lock);
1618 while ((skb = __skb_dequeue(&dev->rx_frames)))
1619 dev_kfree_skb_any(skb);
1620 spin_unlock(&dev->rx_frames.lock);
1621
1622 link->out_ep->desc = NULL;
1623 }
David Brownell2b3d9422008-06-19 18:19:28 -07001624
Saket Saurabh6481d882013-09-27 15:52:36 +05301625 pr_debug("%s(): tx_throttle count= %lu", __func__,
1626 dev->tx_throttle);
1627 /* reset tx_throttle count */
1628 dev->tx_throttle = 0;
1629
David Brownell2b3d9422008-06-19 18:19:28 -07001630 /* finish forgetting about this USB link episode */
1631 dev->header_len = 0;
1632 dev->unwrap = NULL;
1633 dev->wrap = NULL;
1634
1635 spin_lock(&dev->lock);
1636 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001637 spin_unlock(&dev->lock);
1638}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001639EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001640
Saket Saurabh6481d882013-09-27 15:52:36 +05301641static int uether_stat_show(struct seq_file *s, void *unused)
1642{
1643 struct eth_dev *dev = s->private;
1644 int ret = 0;
1645
1646 if (dev)
1647 seq_printf(s, "tx_throttle = %lu\n", dev->tx_throttle);
1648 return ret;
1649}
1650
1651static int uether_open(struct inode *inode, struct file *file)
1652{
1653 return single_open(file, uether_stat_show, inode->i_private);
1654}
1655
1656static ssize_t uether_stat_reset(struct file *file,
1657 const char __user *ubuf, size_t count, loff_t *ppos)
1658{
1659 struct seq_file *s = file->private_data;
1660 struct eth_dev *dev = s->private;
1661 unsigned long flags;
1662
1663 spin_lock_irqsave(&dev->lock, flags);
1664 /* Reset tx_throttle */
1665 dev->tx_throttle = 0;
1666 spin_unlock_irqrestore(&dev->lock, flags);
1667 return count;
1668}
1669
1670static const struct file_operations uether_stats_ops = {
1671 .open = uether_open,
1672 .read = seq_read,
1673 .write = uether_stat_reset,
1674};
1675
1676static void uether_debugfs_init(struct eth_dev *dev)
1677{
1678 struct dentry *uether_dent;
1679 struct dentry *uether_dfile;
1680
1681 uether_dent = debugfs_create_dir("uether_rndis", NULL);
1682 if (IS_ERR(uether_dent))
1683 return;
1684 dev->uether_dent = uether_dent;
1685
1686 uether_dfile = debugfs_create_file("status", 0644,
1687 uether_dent, dev, &uether_stats_ops);
1688 if (!uether_dfile || IS_ERR(uether_dfile))
1689 debugfs_remove(uether_dent);
1690 dev->uether_dfile = uether_dfile;
1691}
1692
1693static void uether_debugfs_exit(struct eth_dev *dev)
1694{
1695 debugfs_remove(dev->uether_dfile);
1696 debugfs_remove(dev->uether_dent);
1697 dev->uether_dent = NULL;
1698 dev->uether_dfile = NULL;
1699}
1700
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001701static int __init gether_init(void)
1702{
1703 uether_wq = create_singlethread_workqueue("uether");
1704 if (!uether_wq) {
1705 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1706 return -ENOMEM;
1707 }
1708 return 0;
1709}
1710module_init(gether_init);
1711
1712static void __exit gether_exit(void)
1713{
1714 destroy_workqueue(uether_wq);
1715
1716}
1717module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001718MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001719MODULE_DESCRIPTION("ethernet over USB driver");
1720MODULE_LICENSE("GPL v2");