blob: 8dfa624990633181282cf7616423b3becc7d2694 [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
Tarun Gupta616d2ea2015-09-08 16:58:20 +053062/* Extra buffer size to allocate for tx */
63#define EXTRA_ALLOCATION_SIZE_U_ETH 128
64
David Brownell2b3d9422008-06-19 18:19:28 -070065struct eth_dev {
66 /* lock is held while accessing port_usb
David Brownell2b3d9422008-06-19 18:19:28 -070067 */
68 spinlock_t lock;
69 struct gether *port_usb;
70
71 struct net_device *net;
72 struct usb_gadget *gadget;
73
74 spinlock_t req_lock; /* guard {rx,tx}_reqs */
75 struct list_head tx_reqs, rx_reqs;
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -070076 unsigned tx_qlen;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -070077/* Minimum number of TX USB request queued to UDC */
Sujeet Kumardf2a5022015-02-04 16:38:33 +053078#define MAX_TX_REQ_WITH_NO_INT 5
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -070079 int no_tx_req_used;
80 int tx_skb_hold_count;
81 u32 tx_req_bufsize;
David Brownell2b3d9422008-06-19 18:19:28 -070082
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050083 struct sk_buff_head rx_frames;
84
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020085 unsigned qmult;
86
David Brownell2b3d9422008-06-19 18:19:28 -070087 unsigned header_len;
xerox_lin87bebf82014-08-14 14:48:44 +080088 unsigned ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +080089 unsigned dl_max_pkts_per_xfer;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050090 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
91 int (*unwrap)(struct gether *,
92 struct sk_buff *skb,
93 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070094
95 struct work_struct work;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -070096 struct work_struct rx_work;
David Brownell2b3d9422008-06-19 18:19:28 -070097
98 unsigned long todo;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -080099 unsigned long flags;
100 unsigned short rx_needed_headroom;
David Brownell2b3d9422008-06-19 18:19:28 -0700101#define WORK_RX_MEMORY 0
102
103 bool zlp;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900104 bool no_skb_reserve;
David Brownell2b3d9422008-06-19 18:19:28 -0700105 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200106 u8 dev_mac[ETH_ALEN];
Saket Saurabh6481d882013-09-27 15:52:36 +0530107 unsigned long tx_throttle;
Manu Gautam354be032014-05-15 13:46:33 +0530108 unsigned long rx_throttle;
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700109 unsigned int tx_pkts_rcvd;
Tarun Gupta616d2ea2015-09-08 16:58:20 +0530110 unsigned long skb_expand_cnt;
Saket Saurabh6481d882013-09-27 15:52:36 +0530111 struct dentry *uether_dent;
112 struct dentry *uether_dfile;
David Brownell2b3d9422008-06-19 18:19:28 -0700113};
114
Saket Saurabh6481d882013-09-27 15:52:36 +0530115static void uether_debugfs_init(struct eth_dev *dev);
116static void uether_debugfs_exit(struct eth_dev *dev);
117
David Brownell2b3d9422008-06-19 18:19:28 -0700118/*-------------------------------------------------------------------------*/
119
120#define RX_EXTRA 20 /* bytes guarding against rx overflows */
121
122#define DEFAULT_QLEN 2 /* double buffering by default */
123
Vamsi Krishna11bdb3b2014-05-14 10:57:08 -0700124/*
125 * Usually downlink rates are higher than uplink rates and it
126 * deserve higher number of requests. For CAT-6 data rates of
127 * 300Mbps (~30 packets per milli-sec) 40 usb request may not
128 * be sufficient. At this rate and with interrupt moderation
129 * of interconnect, data can be very bursty. tx_qmult is the
130 * additional multipler on qmult.
131 */
Manu Gautam59559f22015-11-05 14:27:18 +0530132static unsigned int tx_qmult = 2;
Vamsi Krishna11bdb3b2014-05-14 10:57:08 -0700133module_param(tx_qmult, uint, 0644);
134MODULE_PARM_DESC(tx_qmult, "Additional queue length multiplier for tx");
135
Paul Zimmerman04617db2011-06-27 14:13:18 -0700136/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200137static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700138{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700139 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
140 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700141 return qmult * DEFAULT_QLEN;
142 else
143 return DEFAULT_QLEN;
144}
145
146/*-------------------------------------------------------------------------*/
Manu Gautam354be032014-05-15 13:46:33 +0530147#define U_ETHER_RX_PENDING_TSHOLD 500
148
149static unsigned int u_ether_rx_pending_thld = U_ETHER_RX_PENDING_TSHOLD;
150module_param(u_ether_rx_pending_thld, uint, 0644);
David Brownell2b3d9422008-06-19 18:19:28 -0700151
152/* REVISIT there must be a better way than having two sets
153 * of debug calls ...
154 */
155
156#undef DBG
157#undef VDBG
158#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700159#undef INFO
160
161#define xprintk(d, level, fmt, args...) \
162 printk(level "%s: " fmt , (d)->net->name , ## args)
163
164#ifdef DEBUG
165#undef DEBUG
166#define DBG(dev, fmt, args...) \
167 xprintk(dev , KERN_DEBUG , fmt , ## args)
168#else
169#define DBG(dev, fmt, args...) \
170 do { } while (0)
171#endif /* DEBUG */
172
173#ifdef VERBOSE_DEBUG
174#define VDBG DBG
175#else
176#define VDBG(dev, fmt, args...) \
177 do { } while (0)
178#endif /* DEBUG */
179
180#define ERROR(dev, fmt, args...) \
181 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700182#define INFO(dev, fmt, args...) \
183 xprintk(dev , KERN_INFO , fmt , ## args)
184
185/*-------------------------------------------------------------------------*/
186
187/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
188
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800189static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700190{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100191 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
192 return -ERANGE;
193 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700194
Mike Looijmansab738ff2015-11-30 12:18:23 +0100195 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700196}
197
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800198static int ueth_change_mtu_ip(struct net_device *net, int new_mtu)
199{
200 struct eth_dev *dev = netdev_priv(net);
201 unsigned long flags;
202 int status = 0;
203
204 spin_lock_irqsave(&dev->lock, flags);
205 if (new_mtu <= 0)
206 status = -EINVAL;
207 else
208 net->mtu = new_mtu;
209
210 DBG(dev, "[%s] MTU change: old=%d new=%d\n", net->name,
211 net->mtu, new_mtu);
212 spin_unlock_irqrestore(&dev->lock, flags);
213
214 return status;
215}
216
David Brownell2b3d9422008-06-19 18:19:28 -0700217static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
218{
Jiri Pirko7826d432013-01-06 00:44:26 +0000219 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700220
Jiri Pirko7826d432013-01-06 00:44:26 +0000221 strlcpy(p->driver, "g_ether", sizeof(p->driver));
222 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
223 strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
224 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
David Brownell2b3d9422008-06-19 18:19:28 -0700225}
226
David Brownell2b3d9422008-06-19 18:19:28 -0700227/* REVISIT can also support:
228 * - WOL (by tracking suspends and issuing remote wakeup)
229 * - msglevel (implies updated messaging)
230 * - ... probably more ethtool ops
231 */
232
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700233static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700234 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700235 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700236};
237
238static void defer_kevent(struct eth_dev *dev, int flag)
239{
240 if (test_and_set_bit(flag, &dev->todo))
241 return;
242 if (!schedule_work(&dev->work))
243 ERROR(dev, "kevent %d may have been dropped\n", flag);
244 else
245 DBG(dev, "kevent %d scheduled\n", flag);
246}
247
248static void rx_complete(struct usb_ep *ep, struct usb_request *req);
249
250static int
251rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
252{
253 struct sk_buff *skb;
254 int retval = -ENOMEM;
255 size_t size = 0;
256 struct usb_ep *out;
257 unsigned long flags;
258
259 spin_lock_irqsave(&dev->lock, flags);
260 if (dev->port_usb)
261 out = dev->port_usb->out_ep;
262 else
263 out = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -0700264
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530265 if (!out) {
266 spin_unlock_irqrestore(&dev->lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700267 return -ENOTCONN;
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530268 }
David Brownell2b3d9422008-06-19 18:19:28 -0700269
270 /* Padding up to RX_EXTRA handles minor disagreements with host.
271 * Normally we use the USB "terminate on short read" convention;
272 * so allow up to (N*maxpacket), since that memory is normally
273 * already allocated. Some hardware doesn't deal well with short
274 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
275 * byte off the end (to force hardware errors on overflow).
276 *
277 * RNDIS uses internal framing, and explicitly allows senders to
278 * pad to end-of-packet. That's potentially nice for speed, but
279 * means receivers can't recover lost synch on their own (because
280 * new packets don't only start after a short RX).
281 */
282 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
283 size += dev->port_usb->header_len;
284 size += out->maxpacket - 1;
285 size -= size % out->maxpacket;
286
xerox_lin87bebf82014-08-14 14:48:44 +0800287 if (dev->ul_max_pkts_per_xfer)
288 size *= dev->ul_max_pkts_per_xfer;
289
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200290 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800291 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Saket Saurabh3cdaa362014-05-09 17:06:00 +0530292 spin_unlock_irqrestore(&dev->lock, flags);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200293
Amit Pundir5ff0eb22016-01-08 19:36:02 +0530294 DBG(dev, "%s: size: %zd\n", __func__, size);
Sujeet Kumar9a3cf922015-02-04 15:58:30 +0530295 skb = alloc_skb(size, gfp_flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700296 if (skb == NULL) {
297 DBG(dev, "no rx skb\n");
298 goto enomem;
299 }
300
301 /* Some platforms perform better when IP packets are aligned,
302 * but on at least one, checksumming fails otherwise. Note:
303 * RNDIS headers involve variable numbers of LE32 values.
304 */
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900305 if (likely(!dev->no_skb_reserve))
Sujeet Kumar9a3cf922015-02-04 15:58:30 +0530306 skb_reserve(skb, 0);
David Brownell2b3d9422008-06-19 18:19:28 -0700307
308 req->buf = skb->data;
309 req->length = size;
310 req->complete = rx_complete;
311 req->context = skb;
312
313 retval = usb_ep_queue(out, req, gfp_flags);
314 if (retval == -ENOMEM)
315enomem:
316 defer_kevent(dev, WORK_RX_MEMORY);
317 if (retval) {
318 DBG(dev, "rx submit --> %d\n", retval);
319 if (skb)
320 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700321 }
322 return retval;
323}
324
325static void rx_complete(struct usb_ep *ep, struct usb_request *req)
326{
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700327 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700328 struct eth_dev *dev = ep->driver_data;
329 int status = req->status;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700330 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700331
332 switch (status) {
333
334 /* normal completion */
335 case 0:
336 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500337
338 if (dev->unwrap) {
339 unsigned long flags;
340
341 spin_lock_irqsave(&dev->lock, flags);
342 if (dev->port_usb) {
343 status = dev->unwrap(dev->port_usb,
344 skb,
345 &dev->rx_frames);
Badhri Jagan Sridharan8424b3e2014-09-18 10:48:48 -0700346 if (status == -EINVAL)
347 dev->net->stats.rx_errors++;
348 else if (status == -EOVERFLOW)
349 dev->net->stats.rx_over_errors++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500350 } else {
351 dev_kfree_skb_any(skb);
352 status = -ENOTCONN;
353 }
354 spin_unlock_irqrestore(&dev->lock, flags);
355 } else {
356 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700357 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700358 if (!status)
359 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700360 break;
361
362 /* software-driven interface shutdown */
363 case -ECONNRESET: /* unlink */
364 case -ESHUTDOWN: /* disconnect etc */
365 VDBG(dev, "rx shutdown, code %d\n", status);
366 goto quiesce;
367
368 /* for hardware automagic (such as pxa) */
369 case -ECONNABORTED: /* endpoint reset */
370 DBG(dev, "rx %s reset\n", ep->name);
371 defer_kevent(dev, WORK_RX_MEMORY);
372quiesce:
373 dev_kfree_skb_any(skb);
374 goto clean;
375
376 /* data overrun */
377 case -EOVERFLOW:
378 dev->net->stats.rx_over_errors++;
379 /* FALLTHROUGH */
380
381 default:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700382 queue = 1;
383 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700384 dev->net->stats.rx_errors++;
385 DBG(dev, "rx status %d\n", status);
386 break;
387 }
388
David Brownell2b3d9422008-06-19 18:19:28 -0700389clean:
Manu Gautam354be032014-05-15 13:46:33 +0530390 if (queue && dev->rx_frames.qlen <= u_ether_rx_pending_thld) {
391 if (rx_submit(dev, req, GFP_ATOMIC) < 0) {
392 spin_lock(&dev->req_lock);
393 list_add(&req->list, &dev->rx_reqs);
394 spin_unlock(&dev->req_lock);
395 }
396 } else {
397 /* rx buffers draining is delayed,defer further queuing to wq */
398 if (queue)
399 dev->rx_throttle++;
400 spin_lock(&dev->req_lock);
401 list_add(&req->list, &dev->rx_reqs);
402 spin_unlock(&dev->req_lock);
403 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700404
405 if (queue)
406 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700407}
408
409static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
410{
411 unsigned i;
412 struct usb_request *req;
413
414 if (!n)
415 return -ENOMEM;
416
417 /* queue/recycle up to N requests */
418 i = n;
419 list_for_each_entry(req, list, list) {
420 if (i-- == 0)
421 goto extra;
422 }
423 while (i--) {
424 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
425 if (!req)
426 return list_empty(list) ? -ENOMEM : 0;
427 list_add(&req->list, list);
428 }
429 return 0;
430
431extra:
432 /* free extras */
433 for (;;) {
434 struct list_head *next;
435
436 next = req->list.next;
437 list_del(&req->list);
438 usb_ep_free_request(ep, req);
439
440 if (next == list)
441 break;
442
443 req = container_of(next, struct usb_request, list);
444 }
445 return 0;
446}
447
448static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
449{
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800450 int status = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700451
452 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800453 if (link->in_ep) {
Vamsi Krishna11bdb3b2014-05-14 10:57:08 -0700454 status = prealloc(&dev->tx_reqs, link->in_ep, n * tx_qmult);
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800455 if (status < 0)
456 goto fail;
457 }
458
459 if (link->out_ep) {
460 status = prealloc(&dev->rx_reqs, link->out_ep, n);
461 if (status < 0)
462 goto fail;
463 }
David Brownell2b3d9422008-06-19 18:19:28 -0700464 goto done;
465fail:
466 DBG(dev, "can't alloc requests\n");
467done:
468 spin_unlock(&dev->req_lock);
469 return status;
470}
471
472static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
473{
474 struct usb_request *req;
475 unsigned long flags;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700476 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700477
478 /* fill unused rxq slots with some skb */
479 spin_lock_irqsave(&dev->req_lock, flags);
480 while (!list_empty(&dev->rx_reqs)) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700481 /* break the nexus of continuous completion and re-submission*/
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600482 if (++req_cnt > qlen(dev->gadget, dev->qmult))
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700483 break;
484
David Brownell2b3d9422008-06-19 18:19:28 -0700485 req = container_of(dev->rx_reqs.next,
486 struct usb_request, list);
487 list_del_init(&req->list);
488 spin_unlock_irqrestore(&dev->req_lock, flags);
489
490 if (rx_submit(dev, req, gfp_flags) < 0) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700491 spin_lock_irqsave(&dev->req_lock, flags);
492 list_add(&req->list, &dev->rx_reqs);
493 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700494 defer_kevent(dev, WORK_RX_MEMORY);
495 return;
496 }
497
498 spin_lock_irqsave(&dev->req_lock, flags);
499 }
500 spin_unlock_irqrestore(&dev->req_lock, flags);
501}
502
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700503static void process_rx_w(struct work_struct *work)
504{
505 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
506 struct sk_buff *skb;
507 int status = 0;
508
509 if (!dev->port_usb)
510 return;
511
512 while ((skb = skb_dequeue(&dev->rx_frames))) {
513 if (status < 0
514 || ETH_HLEN > skb->len
515 || skb->len > ETH_FRAME_LEN) {
516 dev->net->stats.rx_errors++;
517 dev->net->stats.rx_length_errors++;
518 DBG(dev, "rx length %d\n", skb->len);
519 dev_kfree_skb_any(skb);
520 continue;
521 }
522 skb->protocol = eth_type_trans(skb, dev->net);
523 dev->net->stats.rx_packets++;
524 dev->net->stats.rx_bytes += skb->len;
525
526 status = netif_rx_ni(skb);
527 }
528
529 if (netif_running(dev->net))
530 rx_fill(dev, GFP_KERNEL);
531}
532
David Brownell2b3d9422008-06-19 18:19:28 -0700533static void eth_work(struct work_struct *work)
534{
535 struct eth_dev *dev = container_of(work, struct eth_dev, work);
536
537 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
538 if (netif_running(dev->net))
539 rx_fill(dev, GFP_KERNEL);
540 }
541
542 if (dev->todo)
543 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
544}
545
546static void tx_complete(struct usb_ep *ep, struct usb_request *req)
547{
548 struct sk_buff *skb = req->context;
549 struct eth_dev *dev = ep->driver_data;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700550 struct net_device *net = dev->net;
551 struct usb_request *new_req;
552 struct usb_ep *in;
553 int length;
554 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700555
Rajkumar Raghupathyd8682452013-01-28 11:48:47 +0530556 if (!dev->port_usb) {
557 usb_ep_free_request(ep, req);
558 return;
559 }
560
David Brownell2b3d9422008-06-19 18:19:28 -0700561 switch (req->status) {
562 default:
563 dev->net->stats.tx_errors++;
564 VDBG(dev, "tx err %d\n", req->status);
565 /* FALLTHROUGH */
566 case -ECONNRESET: /* unlink */
567 case -ESHUTDOWN: /* disconnect etc */
568 break;
569 case 0:
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700570 if (!req->zero)
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700571 dev->net->stats.tx_bytes += req->actual-1;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700572 else
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700573 dev->net->stats.tx_bytes += req->actual;
David Brownell2b3d9422008-06-19 18:19:28 -0700574 }
575 dev->net->stats.tx_packets++;
576
577 spin_lock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700578
Manu Gautamf8afc312014-01-29 16:59:21 +0530579 if (dev->port_usb->multi_pkt_xfer && !req->context) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700580 dev->no_tx_req_used--;
581 req->length = 0;
582 in = dev->port_usb->in_ep;
583
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530584 /* Do not process further if no_interrupt is set */
585 if (!req->no_interrupt && !list_empty(&dev->tx_reqs)) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700586 new_req = container_of(dev->tx_reqs.next,
587 struct usb_request, list);
588 list_del(&new_req->list);
589 spin_unlock(&dev->req_lock);
590 if (new_req->length > 0) {
591 length = new_req->length;
592
593 /* NCM requires no zlp if transfer is
594 * dwNtbInMaxSize */
595 if (dev->port_usb->is_fixed &&
596 length == dev->port_usb->fixed_in_len &&
597 (length % in->maxpacket) == 0)
598 new_req->zero = 0;
599 else
600 new_req->zero = 1;
601
602 /* use zlp framing on tx for strict CDC-Ether
603 * conformance, though any robust network rx
604 * path ignores extra padding. and some hardware
605 * doesn't like to write zlps.
606 */
607 if (new_req->zero && !dev->zlp &&
608 (length % in->maxpacket) == 0) {
609 new_req->zero = 0;
610 length++;
611 }
612
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530613 /* set when tx completion interrupt needed */
614 spin_lock(&dev->req_lock);
615 dev->tx_qlen++;
616 if (dev->tx_qlen == MAX_TX_REQ_WITH_NO_INT) {
617 new_req->no_interrupt = 0;
618 dev->tx_qlen = 0;
619 } else {
620 new_req->no_interrupt = 1;
621 }
622 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700623 new_req->length = length;
Tarun Gupta091d4ff2015-07-30 11:59:11 +0530624 new_req->complete = tx_complete;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700625 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
626 switch (retval) {
627 default:
628 DBG(dev, "tx queue err %d\n", retval);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530629 new_req->length = 0;
630 spin_lock(&dev->req_lock);
631 list_add_tail(&new_req->list,
632 &dev->tx_reqs);
633 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700634 break;
635 case 0:
636 spin_lock(&dev->req_lock);
637 dev->no_tx_req_used++;
638 spin_unlock(&dev->req_lock);
Amit Pundir09172082016-05-30 15:19:21 +0530639 netif_trans_update(net);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700640 }
641 } else {
642 spin_lock(&dev->req_lock);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530643 /*
644 * Put the idle request at the back of the
645 * queue. The xmit function will put the
646 * unfinished request at the beginning of the
647 * queue.
648 */
649 list_add_tail(&new_req->list, &dev->tx_reqs);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700650 spin_unlock(&dev->req_lock);
651 }
652 } else {
653 spin_unlock(&dev->req_lock);
654 }
655 } else {
Manu Gautamf8afc312014-01-29 16:59:21 +0530656 /* Is aggregation already enabled and buffers allocated ? */
657 if (dev->port_usb->multi_pkt_xfer && dev->tx_req_bufsize) {
ChandanaKishori Chiluveru2b5ed572015-08-05 15:30:40 +0530658 req->buf = kzalloc(dev->tx_req_bufsize
659 + dev->gadget->extra_buf_alloc, GFP_ATOMIC);
Manu Gautamf8afc312014-01-29 16:59:21 +0530660 req->context = NULL;
661 } else {
662 req->buf = NULL;
663 }
664
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700665 spin_unlock(&dev->req_lock);
666 dev_kfree_skb_any(skb);
667 }
David Brownell2b3d9422008-06-19 18:19:28 -0700668
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530669 /* put the completed req back to tx_reqs tail pool */
670 spin_lock(&dev->req_lock);
671 list_add_tail(&req->list, &dev->tx_reqs);
672 spin_unlock(&dev->req_lock);
673
David Brownell2b3d9422008-06-19 18:19:28 -0700674 if (netif_carrier_ok(dev->net))
675 netif_wake_queue(dev->net);
676}
677
678static inline int is_promisc(u16 cdc_filter)
679{
680 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
681}
682
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530683static int alloc_tx_buffer(struct eth_dev *dev)
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700684{
685 struct list_head *act;
686 struct usb_request *req;
687
xerox_lincdffcb82014-09-04 16:01:59 +0800688 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700689 (dev->net->mtu
690 + sizeof(struct ethhdr)
691 /* size of rndis_packet_msg_type */
692 + 44
693 + 22));
694
695 list_for_each(act, &dev->tx_reqs) {
696 req = container_of(act, struct usb_request, list);
697 if (!req->buf)
ChandanaKishori Chiluveru2b5ed572015-08-05 15:30:40 +0530698 req->buf = kmalloc(dev->tx_req_bufsize
699 + dev->gadget->extra_buf_alloc, GFP_ATOMIC);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530700
701 if (!req->buf)
702 goto free_buf;
Manu Gautamf8afc312014-01-29 16:59:21 +0530703
704 /* req->context is not used for multi_pkt_xfers */
705 req->context = NULL;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700706 }
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +0530707 return 0;
708
709free_buf:
710 /* tx_req_bufsize = 0 retries mem alloc on next eth_start_xmit */
711 dev->tx_req_bufsize = 0;
712 list_for_each(act, &dev->tx_reqs) {
713 req = container_of(act, struct usb_request, list);
714 kfree(req->buf);
715 req->buf = NULL;
716 }
717 return -ENOMEM;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700718}
719
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000720static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
721 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700722{
723 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100724 int length = 0;
Tarun Gupta616d2ea2015-09-08 16:58:20 +0530725 int tail_room = 0;
726 int extra_alloc = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700727 int retval;
728 struct usb_request *req = NULL;
Tarun Gupta616d2ea2015-09-08 16:58:20 +0530729 struct sk_buff *new_skb;
David Brownell2b3d9422008-06-19 18:19:28 -0700730 unsigned long flags;
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700731 struct usb_ep *in = NULL;
732 u16 cdc_filter = 0;
Mayank Rana68f74742013-02-15 14:55:30 +0530733 bool multi_pkt_xfer = false;
ChandanaKishori Chiluveru0c4403e2016-01-20 11:54:22 +0530734 u32 fixed_in_len = 0;
735 bool is_fixed = false;
David Brownell2b3d9422008-06-19 18:19:28 -0700736
737 spin_lock_irqsave(&dev->lock, flags);
738 if (dev->port_usb) {
739 in = dev->port_usb->in_ep;
740 cdc_filter = dev->port_usb->cdc_filter;
ChandanaKishori Chiluveru0c4403e2016-01-20 11:54:22 +0530741 is_fixed = dev->port_usb->is_fixed;
742 fixed_in_len = dev->port_usb->fixed_in_len;
Mayank Rana68f74742013-02-15 14:55:30 +0530743 multi_pkt_xfer = dev->port_usb->multi_pkt_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -0700744 }
745 spin_unlock_irqrestore(&dev->lock, flags);
746
Jim Baxter6d3865f2014-07-07 18:33:18 +0100747 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700748 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000749 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700750 }
751
752 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100753 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700754 u8 *dest = skb->data;
755
756 if (is_multicast_ether_addr(dest)) {
757 u16 type;
758
759 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
760 * SET_ETHERNET_MULTICAST_FILTERS requests
761 */
762 if (is_broadcast_ether_addr(dest))
763 type = USB_CDC_PACKET_TYPE_BROADCAST;
764 else
765 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
766 if (!(cdc_filter & type)) {
767 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000768 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700769 }
770 }
771 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
772 }
773
Vamsi Krishna41fc4952014-06-01 20:20:15 -0700774 dev->tx_pkts_rcvd++;
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700775 /*
776 * no buffer copies needed, unless the network stack did it
777 * or the hardware can't use skb buffers.
778 * or there's not enough space for extra headers we need
779 */
780 spin_lock_irqsave(&dev->lock, flags);
781 if (dev->wrap && dev->port_usb)
782 skb = dev->wrap(dev->port_usb, skb);
783 spin_unlock_irqrestore(&dev->lock, flags);
784
785 if (!skb) {
786 if (dev->port_usb && dev->port_usb->supports_multi_frame) {
787 /*
788 * Multi frame CDC protocols may store the frame for
789 * later which is not a dropped frame.
790 */
791 } else {
792 dev->net->stats.tx_dropped++;
793 }
794
795 /* no error code for dropped packets */
796 return NETDEV_TX_OK;
797 }
798
799 /* Allocate memory for tx_reqs to support multi packet transfer */
David Brownell2b3d9422008-06-19 18:19:28 -0700800 spin_lock_irqsave(&dev->req_lock, flags);
Vamsi Krishnafdafb972014-04-02 15:18:55 -0700801 if (multi_pkt_xfer && !dev->tx_req_bufsize) {
802 retval = alloc_tx_buffer(dev);
803 if (retval < 0) {
804 spin_unlock_irqrestore(&dev->req_lock, flags);
805 return -ENOMEM;
806 }
807 }
808
David Brownell2b3d9422008-06-19 18:19:28 -0700809 /*
810 * this freelist can be empty if an interrupt triggered disconnect()
811 * and reconfigured the gadget (shutting down this queue) after the
812 * network stack decided to xmit but before we got the spinlock.
813 */
814 if (list_empty(&dev->tx_reqs)) {
815 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000816 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700817 }
818
819 req = container_of(dev->tx_reqs.next, struct usb_request, list);
820 list_del(&req->list);
821
822 /* temporarily stop TX queue when the freelist empties */
Saket Saurabh6481d882013-09-27 15:52:36 +0530823 if (list_empty(&dev->tx_reqs)) {
824 /*
825 * tx_throttle gives info about number of times u_ether
826 * asked network layer to stop queueing packets to it
827 * when transmit resources are unavailable
828 */
829 dev->tx_throttle++;
David Brownell2b3d9422008-06-19 18:19:28 -0700830 netif_stop_queue(net);
Saket Saurabh6481d882013-09-27 15:52:36 +0530831 }
David Brownell2b3d9422008-06-19 18:19:28 -0700832
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700833 dev->tx_skb_hold_count++;
834 spin_unlock_irqrestore(&dev->req_lock, flags);
835
Mayank Rana68f74742013-02-15 14:55:30 +0530836 if (multi_pkt_xfer) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700837 memcpy(req->buf + req->length, skb->data, skb->len);
838 req->length = req->length + skb->len;
839 length = req->length;
840 dev_kfree_skb_any(skb);
841
842 spin_lock_irqsave(&dev->req_lock, flags);
xerox_lincdffcb82014-09-04 16:01:59 +0800843 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530844 /*
845 * should allow aggregation only, if the number of
846 * requests queued more than the tx requests that can
847 * be queued with no interrupt flag set sequentially.
848 * Otherwise, packets may be blocked forever.
849 */
850 if (dev->no_tx_req_used > MAX_TX_REQ_WITH_NO_INT) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700851 list_add(&req->list, &dev->tx_reqs);
852 spin_unlock_irqrestore(&dev->req_lock, flags);
853 goto success;
854 }
855 }
856
857 dev->no_tx_req_used++;
858 spin_unlock_irqrestore(&dev->req_lock, flags);
859
860 spin_lock_irqsave(&dev->lock, flags);
861 dev->tx_skb_hold_count = 0;
862 spin_unlock_irqrestore(&dev->lock, flags);
863 } else {
Manu Gautam59559f22015-11-05 14:27:18 +0530864 bool do_align = false;
865
866 /* Check if TX buffer should be aligned before queuing to hw */
867 if (dev->gadget->is_chipidea &&
868 !IS_ALIGNED((size_t)skb->data, 4))
869 do_align = true;
870
Tarun Gupta616d2ea2015-09-08 16:58:20 +0530871 /*
872 * Some UDC requires allocation of some extra bytes for
873 * TX buffer due to hardware requirement. Check if extra
874 * bytes are already there, otherwise allocate new buffer
Manu Gautam59559f22015-11-05 14:27:18 +0530875 * with extra bytes and do memcpy to align skb as well.
Tarun Gupta616d2ea2015-09-08 16:58:20 +0530876 */
Tarun Gupta616d2ea2015-09-08 16:58:20 +0530877 if (dev->gadget->extra_buf_alloc)
878 extra_alloc = EXTRA_ALLOCATION_SIZE_U_ETH;
879 tail_room = skb_tailroom(skb);
Manu Gautam59559f22015-11-05 14:27:18 +0530880 if (do_align || tail_room < extra_alloc) {
881 pr_debug("%s:align skb and update tail_room %d to %d\n",
882 __func__, tail_room, extra_alloc);
883 tail_room = extra_alloc;
884 new_skb = skb_copy_expand(skb, 0, tail_room,
885 GFP_ATOMIC);
Tarun Gupta616d2ea2015-09-08 16:58:20 +0530886 if (!new_skb)
887 return -ENOMEM;
888 dev_kfree_skb_any(skb);
889 skb = new_skb;
890 dev->skb_expand_cnt++;
891 }
892
Manu Gautam59559f22015-11-05 14:27:18 +0530893 length = skb->len;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700894 req->buf = skb->data;
895 req->context = skb;
896 }
897
David Brownell2b3d9422008-06-19 18:19:28 -0700898 req->complete = tx_complete;
899
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200900 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
ChandanaKishori Chiluveru0c4403e2016-01-20 11:54:22 +0530901 if (is_fixed && length == fixed_in_len &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200902 (length % in->maxpacket) == 0)
903 req->zero = 0;
904 else
905 req->zero = 1;
906
David Brownell2b3d9422008-06-19 18:19:28 -0700907 /* use zlp framing on tx for strict CDC-Ether conformance,
908 * though any robust network rx path ignores extra padding.
909 * and some hardware doesn't like to write zlps.
910 */
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700911 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
912 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700913 length++;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700914 }
David Brownell2b3d9422008-06-19 18:19:28 -0700915
916 req->length = length;
917
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700918 /* throttle highspeed IRQ rate back slightly */
919 if (gadget_is_dualspeed(dev->gadget) &&
920 (dev->gadget->speed == USB_SPEED_HIGH)) {
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530921 spin_lock_irqsave(&dev->req_lock, flags);
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700922 dev->tx_qlen++;
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530923 if (dev->tx_qlen == MAX_TX_REQ_WITH_NO_INT) {
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700924 req->no_interrupt = 0;
925 dev->tx_qlen = 0;
926 } else {
927 req->no_interrupt = 1;
928 }
Sujeet Kumardf2a5022015-02-04 16:38:33 +0530929 spin_unlock_irqrestore(&dev->req_lock, flags);
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700930 } else {
931 req->no_interrupt = 0;
932 }
David Brownell2b3d9422008-06-19 18:19:28 -0700933
934 retval = usb_ep_queue(in, req, GFP_ATOMIC);
935 switch (retval) {
936 default:
937 DBG(dev, "tx queue err %d\n", retval);
938 break;
939 case 0:
Florian Westphal860e9532016-05-03 16:33:13 +0200940 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700941 }
942
943 if (retval) {
Mayank Rana68f74742013-02-15 14:55:30 +0530944 if (!multi_pkt_xfer)
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700945 dev_kfree_skb_any(skb);
Pavankumar Kondetib8a33622013-04-01 18:13:32 +0530946 else
947 req->length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700948 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700949 spin_lock_irqsave(&dev->req_lock, flags);
950 if (list_empty(&dev->tx_reqs))
951 netif_start_queue(net);
952 list_add(&req->list, &dev->tx_reqs);
953 spin_unlock_irqrestore(&dev->req_lock, flags);
954 }
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700955success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000956 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700957}
958
959/*-------------------------------------------------------------------------*/
960
961static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
962{
963 DBG(dev, "%s\n", __func__);
964
965 /* fill the rx queue */
966 rx_fill(dev, gfp_flags);
967
968 /* and open the tx floodgates */
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700969 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700970 netif_wake_queue(dev->net);
971}
972
973static int eth_open(struct net_device *net)
974{
975 struct eth_dev *dev = netdev_priv(net);
976 struct gether *link;
977
978 DBG(dev, "%s\n", __func__);
979 if (netif_carrier_ok(dev->net))
980 eth_start(dev, GFP_KERNEL);
981
982 spin_lock_irq(&dev->lock);
983 link = dev->port_usb;
984 if (link && link->open)
985 link->open(link);
986 spin_unlock_irq(&dev->lock);
987
988 return 0;
989}
990
991static int eth_stop(struct net_device *net)
992{
993 struct eth_dev *dev = netdev_priv(net);
994 unsigned long flags;
995
996 VDBG(dev, "%s\n", __func__);
997 netif_stop_queue(net);
998
999 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
1000 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
1001 dev->net->stats.rx_errors, dev->net->stats.tx_errors
1002 );
1003
1004 /* ensure there are no more active requests */
1005 spin_lock_irqsave(&dev->lock, flags);
1006 if (dev->port_usb) {
1007 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +02001008 const struct usb_endpoint_descriptor *in;
1009 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -07001010
1011 if (link->close)
1012 link->close(link);
1013
1014 /* NOTE: we have no abort-queue primitive we could use
1015 * to cancel all pending I/O. Instead, we disable then
1016 * reenable the endpoints ... this idiom may leave toggle
1017 * wrong, but that's a self-correcting error.
1018 *
1019 * REVISIT: we *COULD* just let the transfers complete at
1020 * their own pace; the network stack can handle old packets.
1021 * For the moment we leave this here, since it works.
1022 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001023 if (link->in_ep) {
1024 in = link->in_ep->desc;
1025 usb_ep_disable(link->in_ep);
1026 if (netif_carrier_ok(net)) {
1027 DBG(dev, "host still using in endpoints\n");
1028 link->in_ep->desc = in;
1029 usb_ep_enable(link->in_ep);
1030 }
1031 }
1032
1033 if (link->out_ep) {
1034 out = link->out_ep->desc;
1035 usb_ep_disable(link->out_ep);
1036 if (netif_carrier_ok(net)) {
1037 DBG(dev, "host still using out endpoints\n");
1038 link->out_ep->desc = out;
1039 usb_ep_enable(link->out_ep);
1040 }
David Brownell2b3d9422008-06-19 18:19:28 -07001041 }
1042 }
1043 spin_unlock_irqrestore(&dev->lock, flags);
1044
1045 return 0;
1046}
1047
1048/*-------------------------------------------------------------------------*/
1049
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001050static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -07001051{
1052 if (str) {
1053 unsigned i;
1054
1055 for (i = 0; i < 6; i++) {
1056 unsigned char num;
1057
1058 if ((*str == '.') || (*str == ':'))
1059 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +03001060 num = hex_to_bin(*str++) << 4;
1061 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -07001062 dev_addr [i] = num;
1063 }
1064 if (is_valid_ether_addr(dev_addr))
1065 return 0;
1066 }
Joe Perches006c9132012-07-12 22:33:11 -07001067 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -07001068 return 1;
1069}
1070
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001071static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
1072{
1073 if (len < 18)
1074 return -EINVAL;
1075
Andy Shevchenko27f38702015-01-15 13:40:04 +02001076 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001077 return 18;
1078}
1079
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001080static int ether_ioctl(struct net_device *, struct ifreq *, int);
1081
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001082static const struct net_device_ops eth_netdev_ops = {
1083 .ndo_open = eth_open,
1084 .ndo_stop = eth_stop,
1085 .ndo_start_xmit = eth_start_xmit,
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001086 .ndo_do_ioctl = ether_ioctl,
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001087 .ndo_change_mtu = ueth_change_mtu,
1088 .ndo_set_mac_address = eth_mac_addr,
1089 .ndo_validate_addr = eth_validate_addr,
1090};
David Brownell2b3d9422008-06-19 18:19:28 -07001091
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001092static const struct net_device_ops eth_netdev_ops_ip = {
1093 .ndo_open = eth_open,
1094 .ndo_stop = eth_stop,
1095 .ndo_start_xmit = eth_start_xmit,
1096 .ndo_do_ioctl = ether_ioctl,
1097 .ndo_change_mtu = ueth_change_mtu_ip,
1098 .ndo_set_mac_address = NULL,
1099 .ndo_validate_addr = NULL,
1100};
1101
1102static int rmnet_ioctl_extended(struct net_device *dev, struct ifreq *ifr)
1103{
1104 struct rmnet_ioctl_extended_s ext_cmd;
1105 struct eth_dev *eth_dev = netdev_priv(dev);
1106 int rc = 0;
1107
1108 rc = copy_from_user(&ext_cmd, ifr->ifr_ifru.ifru_data,
1109 sizeof(struct rmnet_ioctl_extended_s));
1110
1111 if (rc) {
1112 DBG(eth_dev, "%s(): copy_from_user() failed\n", __func__);
1113 return rc;
1114 }
1115
1116 switch (ext_cmd.extended_ioctl) {
1117 case RMNET_IOCTL_GET_SUPPORTED_FEATURES:
1118 ext_cmd.u.data = 0;
1119 break;
1120
1121 case RMNET_IOCTL_SET_MRU:
1122 if (netif_running(dev))
1123 return -EBUSY;
1124
1125 /* 16K max */
1126 if ((size_t)ext_cmd.u.data > 0x4000)
1127 return -EINVAL;
1128
1129 if (eth_dev->port_usb) {
1130 eth_dev->port_usb->is_fixed = true;
1131 eth_dev->port_usb->fixed_out_len =
1132 (size_t) ext_cmd.u.data;
1133 DBG(eth_dev, "[%s] rmnet_ioctl(): SET MRU to %u\n",
1134 dev->name, eth_dev->port_usb->fixed_out_len);
1135 } else {
1136 pr_err("[%s]: %s: SET MRU failed. Cable disconnected\n",
1137 dev->name, __func__);
1138 return -ENODEV;
1139 }
1140 break;
1141
1142 case RMNET_IOCTL_GET_MRU:
1143 if (eth_dev->port_usb) {
1144 ext_cmd.u.data = eth_dev->port_usb->is_fixed ?
1145 eth_dev->port_usb->fixed_out_len :
1146 dev->mtu;
1147 } else {
1148 pr_err("[%s]: %s: GET MRU failed. Cable disconnected\n",
1149 dev->name, __func__);
1150 return -ENODEV;
1151 }
1152 break;
1153
1154 case RMNET_IOCTL_GET_DRIVER_NAME:
1155 strlcpy(ext_cmd.u.if_name, dev->name,
1156 sizeof(ext_cmd.u.if_name));
1157 break;
1158
1159 default:
1160 break;
1161 }
1162
1163 rc = copy_to_user(ifr->ifr_ifru.ifru_data, &ext_cmd,
1164 sizeof(struct rmnet_ioctl_extended_s));
1165
1166 if (rc)
1167 DBG(eth_dev, "%s(): copy_to_user() failed\n", __func__);
1168 return rc;
1169}
1170
1171static int ether_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1172{
1173 struct eth_dev *eth_dev = netdev_priv(dev);
1174 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1175 int prev_mtu = dev->mtu;
1176 u32 state, old_opmode;
1177 int rc = -EFAULT;
1178
1179 old_opmode = eth_dev->flags;
1180 /* Process IOCTL command */
1181 switch (cmd) {
1182 case RMNET_IOCTL_SET_LLP_ETHERNET: /*Set Ethernet protocol*/
1183 /* Perform Ethernet config only if in IP mode currently*/
1184 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags)) {
1185 ether_setup(dev);
1186 dev->mtu = prev_mtu;
1187 dev->netdev_ops = &eth_netdev_ops;
1188 clear_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1189 set_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1190 DBG(eth_dev, "[%s] ioctl(): set Ethernet proto mode\n",
1191 dev->name);
1192 }
1193 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags))
1194 rc = 0;
1195 break;
1196
1197 case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol*/
1198 /* Perform IP config only if in Ethernet mode currently*/
1199 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags)) {
1200 /* Undo config done in ether_setup() */
1201 dev->header_ops = NULL; /* No header */
1202 dev->type = ARPHRD_RAWIP;
1203 dev->hard_header_len = 0;
1204 dev->mtu = prev_mtu;
1205 dev->addr_len = 0;
1206 dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
1207 dev->netdev_ops = &eth_netdev_ops_ip;
1208 clear_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1209 set_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1210 DBG(eth_dev, "[%s] ioctl(): set IP protocol mode\n",
1211 dev->name);
1212 }
1213 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags))
1214 rc = 0;
1215 break;
1216
1217 case RMNET_IOCTL_GET_LLP: /* Get link protocol state */
1218 state = eth_dev->flags & (RMNET_MODE_LLP_ETH
1219 | RMNET_MODE_LLP_IP);
1220 if (copy_to_user(addr, &state, sizeof(state)))
1221 break;
1222 rc = 0;
1223 break;
1224
1225 case RMNET_IOCTL_SET_RX_HEADROOM: /* Set RX headroom */
1226 if (copy_from_user(&eth_dev->rx_needed_headroom, addr,
1227 sizeof(eth_dev->rx_needed_headroom)))
1228 break;
1229 DBG(eth_dev, "[%s] ioctl(): set RX HEADROOM: %x\n",
1230 dev->name, eth_dev->rx_needed_headroom);
1231 rc = 0;
1232 break;
1233
1234 case RMNET_IOCTL_EXTENDED:
1235 rc = rmnet_ioctl_extended(dev, ifr);
1236 break;
1237
1238 default:
1239 pr_err("[%s] error: ioctl called for unsupported cmd[%d]",
1240 dev->name, cmd);
1241 rc = -EINVAL;
1242 }
1243
1244 DBG(eth_dev, "[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08lx\n",
1245 dev->name, __func__, cmd, old_opmode, eth_dev->flags);
1246
1247 return rc;
1248}
1249
Marcel Holtmannaa790742010-01-15 22:13:58 -08001250static struct device_type gadget_type = {
1251 .name = "gadget",
1252};
1253
David Brownell2b3d9422008-06-19 18:19:28 -07001254/**
Mike Lockwood036e98b2012-05-10 10:08:02 +02001255 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -07001256 * @g: gadget to associated with these links
1257 * @ethaddr: NULL, or a buffer in which the ethernet address of the
1258 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +02001259 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -07001260 * Context: may sleep
1261 *
1262 * This sets up the single network link that may be exported by a
1263 * gadget driver using this framework. The link layer addresses are
1264 * set up using module parameters.
1265 *
Dan Carpenter574f24f2013-11-14 11:42:11 +03001266 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -07001267 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001268struct eth_dev *gether_setup_name(struct usb_gadget *g,
1269 const char *dev_addr, const char *host_addr,
1270 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -07001271{
1272 struct eth_dev *dev;
1273 struct net_device *net;
1274 int status;
1275
David Brownell2b3d9422008-06-19 18:19:28 -07001276 net = alloc_etherdev(sizeof *dev);
1277 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001278 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -07001279
1280 dev = netdev_priv(net);
1281 spin_lock_init(&dev->lock);
1282 spin_lock_init(&dev->req_lock);
1283 INIT_WORK(&dev->work, eth_work);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001284 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -07001285 INIT_LIST_HEAD(&dev->tx_reqs);
1286 INIT_LIST_HEAD(&dev->rx_reqs);
1287
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001288 skb_queue_head_init(&dev->rx_frames);
1289
David Brownell2b3d9422008-06-19 18:19:28 -07001290 /* network device setup */
1291 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001292 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +02001293 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -07001294
1295 if (get_ether_addr(dev_addr, net->dev_addr))
1296 dev_warn(&g->dev,
1297 "using random %s ethernet address\n", "self");
1298 if (get_ether_addr(host_addr, dev->host_mac))
1299 dev_warn(&g->dev,
1300 "using random %s ethernet address\n", "host");
1301
1302 if (ethaddr)
1303 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
1304
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001305 net->netdev_ops = &eth_netdev_ops;
1306
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001307 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -07001308
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001309 /* set operation mode to eth by default */
1310 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1311
David Brownell2b3d9422008-06-19 18:19:28 -07001312 dev->gadget = g;
1313 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -08001314 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -07001315
1316 status = register_netdev(net);
1317 if (status < 0) {
1318 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1319 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001320 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -07001321 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001322 INFO(dev, "MAC %pM\n", net->dev_addr);
1323 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001324
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001325 /*
1326 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -07001327 * - iff DATA transfer is active, carrier is "on"
1328 * - tx queueing enabled if open *and* carrier is "on"
1329 */
1330 netif_carrier_off(net);
Saket Saurabh6481d882013-09-27 15:52:36 +05301331 uether_debugfs_init(dev);
David Brownell2b3d9422008-06-19 18:19:28 -07001332 }
1333
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001334 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -07001335}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001336EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -07001337
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001338struct net_device *gether_setup_name_default(const char *netname)
1339{
1340 struct net_device *net;
1341 struct eth_dev *dev;
1342
1343 net = alloc_etherdev(sizeof(*dev));
1344 if (!net)
1345 return ERR_PTR(-ENOMEM);
1346
1347 dev = netdev_priv(net);
1348 spin_lock_init(&dev->lock);
1349 spin_lock_init(&dev->req_lock);
1350 INIT_WORK(&dev->work, eth_work);
Matthew Moeller5df32222016-03-09 20:19:25 -06001351 INIT_WORK(&dev->rx_work, process_rx_w);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001352 INIT_LIST_HEAD(&dev->tx_reqs);
1353 INIT_LIST_HEAD(&dev->rx_reqs);
1354
1355 skb_queue_head_init(&dev->rx_frames);
1356
1357 /* network device setup */
1358 dev->net = net;
1359 dev->qmult = QMULT_DEFAULT;
1360 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
1361
1362 eth_random_addr(dev->dev_mac);
1363 pr_warn("using random %s ethernet address\n", "self");
1364 eth_random_addr(dev->host_mac);
1365 pr_warn("using random %s ethernet address\n", "host");
1366
1367 net->netdev_ops = &eth_netdev_ops;
1368
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001369 net->ethtool_ops = &ops;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001370
1371 /* set operation mode to eth by default */
1372 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1373
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001374 SET_NETDEV_DEVTYPE(net, &gadget_type);
1375
1376 return net;
1377}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001378EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001379
1380int gether_register_netdev(struct net_device *net)
1381{
1382 struct eth_dev *dev;
1383 struct usb_gadget *g;
1384 struct sockaddr sa;
1385 int status;
1386
1387 if (!net->dev.parent)
1388 return -EINVAL;
1389 dev = netdev_priv(net);
1390 g = dev->gadget;
1391 status = register_netdev(net);
1392 if (status < 0) {
1393 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1394 return status;
1395 } else {
1396 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
1397
1398 /* two kinds of host-initiated state changes:
1399 * - iff DATA transfer is active, carrier is "on"
1400 * - tx queueing enabled if open *and* carrier is "on"
1401 */
1402 netif_carrier_off(net);
1403 }
1404 sa.sa_family = net->type;
1405 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
1406 rtnl_lock();
1407 status = dev_set_mac_address(net, &sa);
1408 rtnl_unlock();
1409 if (status)
1410 pr_warn("cannot set self ethernet address: %d\n", status);
1411 else
1412 INFO(dev, "MAC %pM\n", dev->dev_mac);
1413
1414 return status;
1415}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001416EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001417
1418void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
1419{
1420 struct eth_dev *dev;
1421
1422 dev = netdev_priv(net);
1423 dev->gadget = g;
1424 SET_NETDEV_DEV(net, &g->dev);
1425}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001426EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001427
1428int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
1429{
1430 struct eth_dev *dev;
1431 u8 new_addr[ETH_ALEN];
1432
1433 dev = netdev_priv(net);
1434 if (get_ether_addr(dev_addr, new_addr))
1435 return -EINVAL;
1436 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
1437 return 0;
1438}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001439EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001440
1441int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
1442{
1443 struct eth_dev *dev;
1444
1445 dev = netdev_priv(net);
1446 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
1447}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001448EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001449
1450int gether_set_host_addr(struct net_device *net, const char *host_addr)
1451{
1452 struct eth_dev *dev;
1453 u8 new_addr[ETH_ALEN];
1454
1455 dev = netdev_priv(net);
1456 if (get_ether_addr(host_addr, new_addr))
1457 return -EINVAL;
1458 memcpy(dev->host_mac, new_addr, ETH_ALEN);
1459 return 0;
1460}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001461EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001462
1463int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
1464{
1465 struct eth_dev *dev;
1466
1467 dev = netdev_priv(net);
1468 return get_ether_addr_str(dev->host_mac, host_addr, len);
1469}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001470EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001471
1472int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
1473{
1474 struct eth_dev *dev;
1475
1476 if (len < 13)
1477 return -EINVAL;
1478
1479 dev = netdev_priv(net);
1480 snprintf(host_addr, len, "%pm", dev->host_mac);
1481
1482 return strlen(host_addr);
1483}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001484EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001485
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001486void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
1487{
1488 struct eth_dev *dev;
1489
1490 dev = netdev_priv(net);
1491 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1492}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001493EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001494
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001495void gether_set_qmult(struct net_device *net, unsigned qmult)
1496{
1497 struct eth_dev *dev;
1498
1499 dev = netdev_priv(net);
1500 dev->qmult = qmult;
1501}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001502EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001503
1504unsigned gether_get_qmult(struct net_device *net)
1505{
1506 struct eth_dev *dev;
1507
1508 dev = netdev_priv(net);
1509 return dev->qmult;
1510}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001511EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001512
1513int gether_get_ifname(struct net_device *net, char *name, int len)
1514{
1515 rtnl_lock();
1516 strlcpy(name, netdev_name(net), len);
1517 rtnl_unlock();
1518 return strlen(name);
1519}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001520EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001521
David Brownell2b3d9422008-06-19 18:19:28 -07001522/**
1523 * gether_cleanup - remove Ethernet-over-USB device
1524 * Context: may sleep
1525 *
1526 * This is called to free all resources allocated by @gether_setup().
1527 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001528void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001529{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001530 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001531 return;
1532
Saket Saurabh6481d882013-09-27 15:52:36 +05301533 uether_debugfs_exit(dev);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001534 unregister_netdev(dev->net);
1535 flush_work(&dev->work);
Vijayavardhan Vennapusad251f8b2016-03-17 10:39:56 +05301536 cancel_work_sync(&dev->rx_work);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001537 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001538}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001539EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001540
David Brownell2b3d9422008-06-19 18:19:28 -07001541/**
1542 * gether_connect - notify network layer that USB link is active
1543 * @link: the USB link, set up with endpoints, descriptors matching
1544 * current device speed, and any framing wrapper(s) set up.
1545 * Context: irqs blocked
1546 *
1547 * This is called to activate endpoints and let the network layer know
1548 * the connection is active ("carrier detect"). It may cause the I/O
1549 * queues to open and start letting network packets flow, but will in
1550 * any case activate the endpoints so that they respond properly to the
1551 * USB host.
1552 *
1553 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1554 * indicate some error code (negative errno), ep->driver_data values
1555 * have been overwritten.
1556 */
1557struct net_device *gether_connect(struct gether *link)
1558{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001559 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001560 int result = 0;
1561
1562 if (!dev)
1563 return ERR_PTR(-EINVAL);
1564
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001565 if (link->in_ep) {
1566 link->in_ep->driver_data = dev;
1567 result = usb_ep_enable(link->in_ep);
1568 if (result != 0) {
1569 DBG(dev, "enable %s --> %d\n",
1570 link->in_ep->name, result);
1571 goto fail0;
1572 }
David Brownell2b3d9422008-06-19 18:19:28 -07001573 }
1574
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001575 if (link->out_ep) {
1576 link->out_ep->driver_data = dev;
1577 result = usb_ep_enable(link->out_ep);
1578 if (result != 0) {
1579 DBG(dev, "enable %s --> %d\n",
1580 link->out_ep->name, result);
1581 goto fail1;
1582 }
David Brownell2b3d9422008-06-19 18:19:28 -07001583 }
1584
1585 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001586 result = alloc_requests(dev, link, qlen(dev->gadget,
1587 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001588
1589 if (result == 0) {
1590 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001591 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001592 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001593
1594 dev->header_len = link->header_len;
1595 dev->unwrap = link->unwrap;
1596 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001597 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +08001598 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001599
1600 spin_lock(&dev->lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -07001601 dev->tx_skb_hold_count = 0;
1602 dev->no_tx_req_used = 0;
1603 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001604 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001605 if (netif_running(dev->net)) {
1606 if (link->open)
1607 link->open(link);
1608 } else {
1609 if (link->close)
1610 link->close(link);
1611 }
David Brownell2b3d9422008-06-19 18:19:28 -07001612 spin_unlock(&dev->lock);
1613
1614 netif_carrier_on(dev->net);
1615 if (netif_running(dev->net))
1616 eth_start(dev, GFP_ATOMIC);
1617
1618 /* on error, disable any endpoints */
1619 } else {
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001620 if (link->out_ep)
1621 (void) usb_ep_disable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001622fail1:
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001623 if (link->in_ep)
1624 (void) usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001625 }
1626fail0:
1627 /* caller is responsible for cleanup on error */
1628 if (result < 0)
1629 return ERR_PTR(result);
1630 return dev->net;
1631}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001632EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001633
1634/**
1635 * gether_disconnect - notify network layer that USB link is inactive
1636 * @link: the USB link, on which gether_connect() was called
1637 * Context: irqs blocked
1638 *
1639 * This is called to deactivate endpoints and let the network layer know
1640 * the connection went inactive ("no carrier").
1641 *
1642 * On return, the state is as if gether_connect() had never been called.
1643 * The endpoints are inactive, and accordingly without active USB I/O.
1644 * Pointers to endpoint descriptors and endpoint private data are nulled.
1645 */
1646void gether_disconnect(struct gether *link)
1647{
1648 struct eth_dev *dev = link->ioport;
1649 struct usb_request *req;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001650 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001651
1652 WARN_ON(!dev);
1653 if (!dev)
1654 return;
1655
1656 DBG(dev, "%s\n", __func__);
1657
1658 netif_stop_queue(dev->net);
1659 netif_carrier_off(dev->net);
1660
1661 /* disable endpoints, forcing (synchronous) completion
1662 * of all pending i/o. then free the request objects
1663 * and forget about the endpoints.
1664 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001665 if (link->in_ep) {
1666 usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001667 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001668 while (!list_empty(&dev->tx_reqs)) {
1669 req = container_of(dev->tx_reqs.next,
1670 struct usb_request, list);
1671 list_del(&req->list);
David Brownell2b3d9422008-06-19 18:19:28 -07001672
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001673 spin_unlock(&dev->req_lock);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +05301674 if (link->multi_pkt_xfer) {
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001675 kfree(req->buf);
Rajkumar Raghupathy7d4a6cb2013-05-23 11:37:41 +05301676 req->buf = NULL;
1677 }
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001678 usb_ep_free_request(link->in_ep, req);
1679 spin_lock(&dev->req_lock);
1680 }
David Brownell2b3d9422008-06-19 18:19:28 -07001681 spin_unlock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001682 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001683 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001684
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001685 if (link->out_ep) {
1686 usb_ep_disable(link->out_ep);
1687 spin_lock(&dev->req_lock);
1688 while (!list_empty(&dev->rx_reqs)) {
1689 req = container_of(dev->rx_reqs.next,
1690 struct usb_request, list);
1691 list_del(&req->list);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001692
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001693 spin_unlock(&dev->req_lock);
1694 usb_ep_free_request(link->out_ep, req);
1695 spin_lock(&dev->req_lock);
1696 }
1697 spin_unlock(&dev->req_lock);
1698
1699 spin_lock(&dev->rx_frames.lock);
1700 while ((skb = __skb_dequeue(&dev->rx_frames)))
1701 dev_kfree_skb_any(skb);
1702 spin_unlock(&dev->rx_frames.lock);
1703
1704 link->out_ep->desc = NULL;
1705 }
David Brownell2b3d9422008-06-19 18:19:28 -07001706
Saket Saurabh6481d882013-09-27 15:52:36 +05301707 pr_debug("%s(): tx_throttle count= %lu", __func__,
1708 dev->tx_throttle);
1709 /* reset tx_throttle count */
1710 dev->tx_throttle = 0;
Manu Gautam354be032014-05-15 13:46:33 +05301711 dev->rx_throttle = 0;
Saket Saurabh6481d882013-09-27 15:52:36 +05301712
David Brownell2b3d9422008-06-19 18:19:28 -07001713 /* finish forgetting about this USB link episode */
1714 dev->header_len = 0;
1715 dev->unwrap = NULL;
1716 dev->wrap = NULL;
1717
1718 spin_lock(&dev->lock);
1719 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001720 spin_unlock(&dev->lock);
1721}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001722EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001723
Saket Saurabh6481d882013-09-27 15:52:36 +05301724static int uether_stat_show(struct seq_file *s, void *unused)
1725{
1726 struct eth_dev *dev = s->private;
1727 int ret = 0;
1728
Manu Gautam354be032014-05-15 13:46:33 +05301729 if (dev) {
Saket Saurabh6481d882013-09-27 15:52:36 +05301730 seq_printf(s, "tx_throttle = %lu\n", dev->tx_throttle);
Vamsi Krishna41fc4952014-06-01 20:20:15 -07001731 seq_printf(s, "tx_pkts_rcvd=%u\n", dev->tx_pkts_rcvd);
Manu Gautam354be032014-05-15 13:46:33 +05301732 seq_printf(s, "rx_throttle = %lu\n", dev->rx_throttle);
Tarun Gupta616d2ea2015-09-08 16:58:20 +05301733 seq_printf(s, "skb_expand_cnt = %lu\n",
1734 dev->skb_expand_cnt);
Manu Gautam354be032014-05-15 13:46:33 +05301735 }
Saket Saurabh6481d882013-09-27 15:52:36 +05301736 return ret;
1737}
1738
1739static int uether_open(struct inode *inode, struct file *file)
1740{
1741 return single_open(file, uether_stat_show, inode->i_private);
1742}
1743
1744static ssize_t uether_stat_reset(struct file *file,
1745 const char __user *ubuf, size_t count, loff_t *ppos)
1746{
1747 struct seq_file *s = file->private_data;
1748 struct eth_dev *dev = s->private;
1749 unsigned long flags;
1750
1751 spin_lock_irqsave(&dev->lock, flags);
1752 /* Reset tx_throttle */
1753 dev->tx_throttle = 0;
Manu Gautam354be032014-05-15 13:46:33 +05301754 dev->rx_throttle = 0;
Tarun Gupta616d2ea2015-09-08 16:58:20 +05301755 dev->skb_expand_cnt = 0;
Saket Saurabh6481d882013-09-27 15:52:36 +05301756 spin_unlock_irqrestore(&dev->lock, flags);
1757 return count;
1758}
1759
1760static const struct file_operations uether_stats_ops = {
1761 .open = uether_open,
1762 .read = seq_read,
1763 .write = uether_stat_reset,
1764};
1765
1766static void uether_debugfs_init(struct eth_dev *dev)
1767{
1768 struct dentry *uether_dent;
1769 struct dentry *uether_dfile;
1770
1771 uether_dent = debugfs_create_dir("uether_rndis", NULL);
1772 if (IS_ERR(uether_dent))
1773 return;
1774 dev->uether_dent = uether_dent;
1775
1776 uether_dfile = debugfs_create_file("status", 0644,
1777 uether_dent, dev, &uether_stats_ops);
1778 if (!uether_dfile || IS_ERR(uether_dfile))
1779 debugfs_remove(uether_dent);
1780 dev->uether_dfile = uether_dfile;
1781}
1782
1783static void uether_debugfs_exit(struct eth_dev *dev)
1784{
1785 debugfs_remove(dev->uether_dfile);
1786 debugfs_remove(dev->uether_dent);
1787 dev->uether_dent = NULL;
1788 dev->uether_dfile = NULL;
1789}
1790
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001791static int __init gether_init(void)
1792{
1793 uether_wq = create_singlethread_workqueue("uether");
1794 if (!uether_wq) {
1795 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1796 return -ENOMEM;
1797 }
1798 return 0;
1799}
1800module_init(gether_init);
1801
1802static void __exit gether_exit(void)
1803{
1804 destroy_workqueue(uether_wq);
1805
1806}
1807module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001808MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001809MODULE_DESCRIPTION("ethernet over USB driver");
1810MODULE_LICENSE("GPL v2");