blob: 76dea295612c5e93798b058cbb31624c8f4e860f [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>
David Brownell2b3d9422008-06-19 18:19:28 -070026
27#include "u_ether.h"
28
29
30/*
31 * This component encapsulates the Ethernet link glue needed to provide
32 * one (!) network link through the USB gadget stack, normally "usb0".
33 *
34 * The control and data models are handled by the function driver which
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050035 * connects to this code; such as CDC Ethernet (ECM or EEM),
36 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
37 * management.
David Brownell2b3d9422008-06-19 18:19:28 -070038 *
39 * Link level addressing is handled by this component using module
40 * parameters; if no such parameters are provided, random link level
41 * addresses are used. Each end of the link uses one address. The
42 * host end address is exported in various ways, and is often recorded
43 * in configuration databases.
44 *
45 * The driver which assembles each configuration using such a link is
46 * responsible for ensuring that each configuration includes at most one
47 * instance of is network link. (The network layer provides ways for
48 * this single "physical" link to be used by multiple virtual links.)
49 */
50
David Brownell8a1ce2c2008-08-18 17:43:56 -070051#define UETH__VERSION "29-May-2008"
David Brownell2b3d9422008-06-19 18:19:28 -070052
Mike Looijmansbba787a2015-08-05 08:54:55 +020053/* Experiments show that both Linux and Windows hosts allow up to 16k
54 * frame sizes. Set the max size to 15k+52 to prevent allocating 32k
55 * blocks and still have efficient handling. */
56#define GETHER_MAX_ETH_FRAME_LEN 15412
57
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -070058static struct workqueue_struct *uether_wq;
59
David Brownell2b3d9422008-06-19 18:19:28 -070060struct eth_dev {
61 /* lock is held while accessing port_usb
David Brownell2b3d9422008-06-19 18:19:28 -070062 */
63 spinlock_t lock;
64 struct gether *port_usb;
65
66 struct net_device *net;
67 struct usb_gadget *gadget;
68
69 spinlock_t req_lock; /* guard {rx,tx}_reqs */
70 struct list_head tx_reqs, rx_reqs;
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -070071 unsigned tx_qlen;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -070072/* Minimum number of TX USB request queued to UDC */
73#define TX_REQ_THRESHOLD 5
74 int no_tx_req_used;
75 int tx_skb_hold_count;
76 u32 tx_req_bufsize;
David Brownell2b3d9422008-06-19 18:19:28 -070077
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050078 struct sk_buff_head rx_frames;
79
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020080 unsigned qmult;
81
David Brownell2b3d9422008-06-19 18:19:28 -070082 unsigned header_len;
xerox_lin87bebf82014-08-14 14:48:44 +080083 unsigned ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +080084 unsigned dl_max_pkts_per_xfer;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050085 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
86 int (*unwrap)(struct gether *,
87 struct sk_buff *skb,
88 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070089
90 struct work_struct work;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -070091 struct work_struct rx_work;
David Brownell2b3d9422008-06-19 18:19:28 -070092
93 unsigned long todo;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -080094 unsigned long flags;
95 unsigned short rx_needed_headroom;
David Brownell2b3d9422008-06-19 18:19:28 -070096#define WORK_RX_MEMORY 0
97
98 bool zlp;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +090099 bool no_skb_reserve;
David Brownell2b3d9422008-06-19 18:19:28 -0700100 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200101 u8 dev_mac[ETH_ALEN];
David Brownell2b3d9422008-06-19 18:19:28 -0700102};
103
104/*-------------------------------------------------------------------------*/
105
106#define RX_EXTRA 20 /* bytes guarding against rx overflows */
107
108#define DEFAULT_QLEN 2 /* double buffering by default */
109
Paul Zimmerman04617db2011-06-27 14:13:18 -0700110/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200111static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700112{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700113 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
114 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700115 return qmult * DEFAULT_QLEN;
116 else
117 return DEFAULT_QLEN;
118}
119
120/*-------------------------------------------------------------------------*/
121
122/* REVISIT there must be a better way than having two sets
123 * of debug calls ...
124 */
125
126#undef DBG
127#undef VDBG
128#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700129#undef INFO
130
131#define xprintk(d, level, fmt, args...) \
132 printk(level "%s: " fmt , (d)->net->name , ## args)
133
134#ifdef DEBUG
135#undef DEBUG
136#define DBG(dev, fmt, args...) \
137 xprintk(dev , KERN_DEBUG , fmt , ## args)
138#else
139#define DBG(dev, fmt, args...) \
140 do { } while (0)
141#endif /* DEBUG */
142
143#ifdef VERBOSE_DEBUG
144#define VDBG DBG
145#else
146#define VDBG(dev, fmt, args...) \
147 do { } while (0)
148#endif /* DEBUG */
149
150#define ERROR(dev, fmt, args...) \
151 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700152#define INFO(dev, fmt, args...) \
153 xprintk(dev , KERN_INFO , fmt , ## args)
154
155/*-------------------------------------------------------------------------*/
156
157/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
158
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800159static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700160{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100161 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
162 return -ERANGE;
163 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700164
Mike Looijmansab738ff2015-11-30 12:18:23 +0100165 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700166}
167
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800168static int ueth_change_mtu_ip(struct net_device *net, int new_mtu)
169{
170 struct eth_dev *dev = netdev_priv(net);
171 unsigned long flags;
172 int status = 0;
173
174 spin_lock_irqsave(&dev->lock, flags);
175 if (new_mtu <= 0)
176 status = -EINVAL;
177 else
178 net->mtu = new_mtu;
179
180 DBG(dev, "[%s] MTU change: old=%d new=%d\n", net->name,
181 net->mtu, new_mtu);
182 spin_unlock_irqrestore(&dev->lock, flags);
183
184 return status;
185}
186
David Brownell2b3d9422008-06-19 18:19:28 -0700187static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
188{
Jiri Pirko7826d432013-01-06 00:44:26 +0000189 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700190
Jiri Pirko7826d432013-01-06 00:44:26 +0000191 strlcpy(p->driver, "g_ether", sizeof(p->driver));
192 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
193 strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
194 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
David Brownell2b3d9422008-06-19 18:19:28 -0700195}
196
David Brownell2b3d9422008-06-19 18:19:28 -0700197/* REVISIT can also support:
198 * - WOL (by tracking suspends and issuing remote wakeup)
199 * - msglevel (implies updated messaging)
200 * - ... probably more ethtool ops
201 */
202
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700203static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700204 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700205 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700206};
207
208static void defer_kevent(struct eth_dev *dev, int flag)
209{
210 if (test_and_set_bit(flag, &dev->todo))
211 return;
212 if (!schedule_work(&dev->work))
213 ERROR(dev, "kevent %d may have been dropped\n", flag);
214 else
215 DBG(dev, "kevent %d scheduled\n", flag);
216}
217
218static void rx_complete(struct usb_ep *ep, struct usb_request *req);
219
220static int
221rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
222{
223 struct sk_buff *skb;
224 int retval = -ENOMEM;
225 size_t size = 0;
226 struct usb_ep *out;
227 unsigned long flags;
228
229 spin_lock_irqsave(&dev->lock, flags);
230 if (dev->port_usb)
231 out = dev->port_usb->out_ep;
232 else
233 out = NULL;
234 spin_unlock_irqrestore(&dev->lock, flags);
235
236 if (!out)
237 return -ENOTCONN;
238
239
240 /* Padding up to RX_EXTRA handles minor disagreements with host.
241 * Normally we use the USB "terminate on short read" convention;
242 * so allow up to (N*maxpacket), since that memory is normally
243 * already allocated. Some hardware doesn't deal well with short
244 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
245 * byte off the end (to force hardware errors on overflow).
246 *
247 * RNDIS uses internal framing, and explicitly allows senders to
248 * pad to end-of-packet. That's potentially nice for speed, but
249 * means receivers can't recover lost synch on their own (because
250 * new packets don't only start after a short RX).
251 */
252 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
253 size += dev->port_usb->header_len;
254 size += out->maxpacket - 1;
255 size -= size % out->maxpacket;
256
xerox_lin87bebf82014-08-14 14:48:44 +0800257 if (dev->ul_max_pkts_per_xfer)
258 size *= dev->ul_max_pkts_per_xfer;
259
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200260 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800261 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200262
Amit Pundir5ff0eb22016-01-08 19:36:02 +0530263 DBG(dev, "%s: size: %zd\n", __func__, size);
David Brownell2b3d9422008-06-19 18:19:28 -0700264 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
265 if (skb == NULL) {
266 DBG(dev, "no rx skb\n");
267 goto enomem;
268 }
269
270 /* Some platforms perform better when IP packets are aligned,
271 * but on at least one, checksumming fails otherwise. Note:
272 * RNDIS headers involve variable numbers of LE32 values.
273 */
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900274 if (likely(!dev->no_skb_reserve))
275 skb_reserve(skb, NET_IP_ALIGN);
David Brownell2b3d9422008-06-19 18:19:28 -0700276
277 req->buf = skb->data;
278 req->length = size;
279 req->complete = rx_complete;
280 req->context = skb;
281
282 retval = usb_ep_queue(out, req, gfp_flags);
283 if (retval == -ENOMEM)
284enomem:
285 defer_kevent(dev, WORK_RX_MEMORY);
286 if (retval) {
287 DBG(dev, "rx submit --> %d\n", retval);
288 if (skb)
289 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700290 }
291 return retval;
292}
293
294static void rx_complete(struct usb_ep *ep, struct usb_request *req)
295{
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700296 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700297 struct eth_dev *dev = ep->driver_data;
298 int status = req->status;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700299 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700300
301 switch (status) {
302
303 /* normal completion */
304 case 0:
305 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500306
307 if (dev->unwrap) {
308 unsigned long flags;
309
310 spin_lock_irqsave(&dev->lock, flags);
311 if (dev->port_usb) {
312 status = dev->unwrap(dev->port_usb,
313 skb,
314 &dev->rx_frames);
Badhri Jagan Sridharan8424b3e2014-09-18 10:48:48 -0700315 if (status == -EINVAL)
316 dev->net->stats.rx_errors++;
317 else if (status == -EOVERFLOW)
318 dev->net->stats.rx_over_errors++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500319 } else {
320 dev_kfree_skb_any(skb);
321 status = -ENOTCONN;
322 }
323 spin_unlock_irqrestore(&dev->lock, flags);
324 } else {
325 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700326 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700327 if (!status)
328 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700329 break;
330
331 /* software-driven interface shutdown */
332 case -ECONNRESET: /* unlink */
333 case -ESHUTDOWN: /* disconnect etc */
334 VDBG(dev, "rx shutdown, code %d\n", status);
335 goto quiesce;
336
337 /* for hardware automagic (such as pxa) */
338 case -ECONNABORTED: /* endpoint reset */
339 DBG(dev, "rx %s reset\n", ep->name);
340 defer_kevent(dev, WORK_RX_MEMORY);
341quiesce:
342 dev_kfree_skb_any(skb);
343 goto clean;
344
345 /* data overrun */
346 case -EOVERFLOW:
347 dev->net->stats.rx_over_errors++;
348 /* FALLTHROUGH */
349
350 default:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700351 queue = 1;
352 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700353 dev->net->stats.rx_errors++;
354 DBG(dev, "rx status %d\n", status);
355 break;
356 }
357
David Brownell2b3d9422008-06-19 18:19:28 -0700358clean:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700359 spin_lock(&dev->req_lock);
360 list_add(&req->list, &dev->rx_reqs);
361 spin_unlock(&dev->req_lock);
362
363 if (queue)
364 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700365}
366
367static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
368{
369 unsigned i;
370 struct usb_request *req;
371
372 if (!n)
373 return -ENOMEM;
374
375 /* queue/recycle up to N requests */
376 i = n;
377 list_for_each_entry(req, list, list) {
378 if (i-- == 0)
379 goto extra;
380 }
381 while (i--) {
382 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
383 if (!req)
384 return list_empty(list) ? -ENOMEM : 0;
385 list_add(&req->list, list);
386 }
387 return 0;
388
389extra:
390 /* free extras */
391 for (;;) {
392 struct list_head *next;
393
394 next = req->list.next;
395 list_del(&req->list);
396 usb_ep_free_request(ep, req);
397
398 if (next == list)
399 break;
400
401 req = container_of(next, struct usb_request, list);
402 }
403 return 0;
404}
405
406static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
407{
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800408 int status = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700409
410 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800411 if (link->in_ep) {
412 status = prealloc(&dev->tx_reqs, link->in_ep, n);
413 if (status < 0)
414 goto fail;
415 }
416
417 if (link->out_ep) {
418 status = prealloc(&dev->rx_reqs, link->out_ep, n);
419 if (status < 0)
420 goto fail;
421 }
David Brownell2b3d9422008-06-19 18:19:28 -0700422 goto done;
423fail:
424 DBG(dev, "can't alloc requests\n");
425done:
426 spin_unlock(&dev->req_lock);
427 return status;
428}
429
430static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
431{
432 struct usb_request *req;
433 unsigned long flags;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700434 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700435
436 /* fill unused rxq slots with some skb */
437 spin_lock_irqsave(&dev->req_lock, flags);
438 while (!list_empty(&dev->rx_reqs)) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700439 /* break the nexus of continuous completion and re-submission*/
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600440 if (++req_cnt > qlen(dev->gadget, dev->qmult))
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700441 break;
442
David Brownell2b3d9422008-06-19 18:19:28 -0700443 req = container_of(dev->rx_reqs.next,
444 struct usb_request, list);
445 list_del_init(&req->list);
446 spin_unlock_irqrestore(&dev->req_lock, flags);
447
448 if (rx_submit(dev, req, gfp_flags) < 0) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700449 spin_lock_irqsave(&dev->req_lock, flags);
450 list_add(&req->list, &dev->rx_reqs);
451 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700452 defer_kevent(dev, WORK_RX_MEMORY);
453 return;
454 }
455
456 spin_lock_irqsave(&dev->req_lock, flags);
457 }
458 spin_unlock_irqrestore(&dev->req_lock, flags);
459}
460
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700461static void process_rx_w(struct work_struct *work)
462{
463 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
464 struct sk_buff *skb;
465 int status = 0;
466
467 if (!dev->port_usb)
468 return;
469
470 while ((skb = skb_dequeue(&dev->rx_frames))) {
471 if (status < 0
472 || ETH_HLEN > skb->len
473 || skb->len > ETH_FRAME_LEN) {
474 dev->net->stats.rx_errors++;
475 dev->net->stats.rx_length_errors++;
476 DBG(dev, "rx length %d\n", skb->len);
477 dev_kfree_skb_any(skb);
478 continue;
479 }
480 skb->protocol = eth_type_trans(skb, dev->net);
481 dev->net->stats.rx_packets++;
482 dev->net->stats.rx_bytes += skb->len;
483
484 status = netif_rx_ni(skb);
485 }
486
487 if (netif_running(dev->net))
488 rx_fill(dev, GFP_KERNEL);
489}
490
David Brownell2b3d9422008-06-19 18:19:28 -0700491static void eth_work(struct work_struct *work)
492{
493 struct eth_dev *dev = container_of(work, struct eth_dev, work);
494
495 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
496 if (netif_running(dev->net))
497 rx_fill(dev, GFP_KERNEL);
498 }
499
500 if (dev->todo)
501 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
502}
503
504static void tx_complete(struct usb_ep *ep, struct usb_request *req)
505{
506 struct sk_buff *skb = req->context;
507 struct eth_dev *dev = ep->driver_data;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700508 struct net_device *net = dev->net;
509 struct usb_request *new_req;
510 struct usb_ep *in;
511 int length;
512 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700513
Rajkumar Raghupathyd8682452013-01-28 11:48:47 +0530514 if (!dev->port_usb) {
515 usb_ep_free_request(ep, req);
516 return;
517 }
518
David Brownell2b3d9422008-06-19 18:19:28 -0700519 switch (req->status) {
520 default:
521 dev->net->stats.tx_errors++;
522 VDBG(dev, "tx err %d\n", req->status);
523 /* FALLTHROUGH */
524 case -ECONNRESET: /* unlink */
525 case -ESHUTDOWN: /* disconnect etc */
526 break;
527 case 0:
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700528 if (!req->zero)
529 dev->net->stats.tx_bytes += req->length-1;
530 else
531 dev->net->stats.tx_bytes += req->length;
David Brownell2b3d9422008-06-19 18:19:28 -0700532 }
533 dev->net->stats.tx_packets++;
534
535 spin_lock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700536 list_add_tail(&req->list, &dev->tx_reqs);
537
538 if (dev->port_usb->multi_pkt_xfer) {
539 dev->no_tx_req_used--;
540 req->length = 0;
541 in = dev->port_usb->in_ep;
542
543 if (!list_empty(&dev->tx_reqs)) {
544 new_req = container_of(dev->tx_reqs.next,
545 struct usb_request, list);
546 list_del(&new_req->list);
547 spin_unlock(&dev->req_lock);
548 if (new_req->length > 0) {
549 length = new_req->length;
550
551 /* NCM requires no zlp if transfer is
552 * dwNtbInMaxSize */
553 if (dev->port_usb->is_fixed &&
554 length == dev->port_usb->fixed_in_len &&
555 (length % in->maxpacket) == 0)
556 new_req->zero = 0;
557 else
558 new_req->zero = 1;
559
560 /* use zlp framing on tx for strict CDC-Ether
561 * conformance, though any robust network rx
562 * path ignores extra padding. and some hardware
563 * doesn't like to write zlps.
564 */
565 if (new_req->zero && !dev->zlp &&
566 (length % in->maxpacket) == 0) {
567 new_req->zero = 0;
568 length++;
569 }
570
571 new_req->length = length;
572 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
573 switch (retval) {
574 default:
575 DBG(dev, "tx queue err %d\n", retval);
576 break;
577 case 0:
578 spin_lock(&dev->req_lock);
579 dev->no_tx_req_used++;
580 spin_unlock(&dev->req_lock);
Amit Pundir09172082016-05-30 15:19:21 +0530581 netif_trans_update(net);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700582 }
583 } else {
584 spin_lock(&dev->req_lock);
585 list_add(&new_req->list, &dev->tx_reqs);
586 spin_unlock(&dev->req_lock);
587 }
588 } else {
589 spin_unlock(&dev->req_lock);
590 }
591 } else {
592 spin_unlock(&dev->req_lock);
593 dev_kfree_skb_any(skb);
594 }
David Brownell2b3d9422008-06-19 18:19:28 -0700595
David Brownell2b3d9422008-06-19 18:19:28 -0700596 if (netif_carrier_ok(dev->net))
597 netif_wake_queue(dev->net);
598}
599
600static inline int is_promisc(u16 cdc_filter)
601{
602 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
603}
604
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700605static void alloc_tx_buffer(struct eth_dev *dev)
606{
607 struct list_head *act;
608 struct usb_request *req;
609
xerox_lincdffcb82014-09-04 16:01:59 +0800610 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700611 (dev->net->mtu
612 + sizeof(struct ethhdr)
613 /* size of rndis_packet_msg_type */
614 + 44
615 + 22));
616
617 list_for_each(act, &dev->tx_reqs) {
618 req = container_of(act, struct usb_request, list);
619 if (!req->buf)
620 req->buf = kmalloc(dev->tx_req_bufsize,
621 GFP_ATOMIC);
622 }
623}
624
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000625static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
626 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700627{
628 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100629 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700630 int retval;
631 struct usb_request *req = NULL;
632 unsigned long flags;
633 struct usb_ep *in;
634 u16 cdc_filter;
635
636 spin_lock_irqsave(&dev->lock, flags);
637 if (dev->port_usb) {
638 in = dev->port_usb->in_ep;
639 cdc_filter = dev->port_usb->cdc_filter;
640 } else {
641 in = NULL;
642 cdc_filter = 0;
643 }
644 spin_unlock_irqrestore(&dev->lock, flags);
645
Jim Baxter6d3865f2014-07-07 18:33:18 +0100646 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700647 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000648 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700649 }
650
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700651 /* Allocate memory for tx_reqs to support multi packet transfer */
652 if (dev->port_usb->multi_pkt_xfer && !dev->tx_req_bufsize)
653 alloc_tx_buffer(dev);
654
David Brownell2b3d9422008-06-19 18:19:28 -0700655 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100656 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700657 u8 *dest = skb->data;
658
659 if (is_multicast_ether_addr(dest)) {
660 u16 type;
661
662 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
663 * SET_ETHERNET_MULTICAST_FILTERS requests
664 */
665 if (is_broadcast_ether_addr(dest))
666 type = USB_CDC_PACKET_TYPE_BROADCAST;
667 else
668 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
669 if (!(cdc_filter & type)) {
670 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000671 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700672 }
673 }
674 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
675 }
676
677 spin_lock_irqsave(&dev->req_lock, flags);
678 /*
679 * this freelist can be empty if an interrupt triggered disconnect()
680 * and reconfigured the gadget (shutting down this queue) after the
681 * network stack decided to xmit but before we got the spinlock.
682 */
683 if (list_empty(&dev->tx_reqs)) {
684 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000685 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700686 }
687
688 req = container_of(dev->tx_reqs.next, struct usb_request, list);
689 list_del(&req->list);
690
691 /* temporarily stop TX queue when the freelist empties */
692 if (list_empty(&dev->tx_reqs))
693 netif_stop_queue(net);
694 spin_unlock_irqrestore(&dev->req_lock, flags);
695
696 /* no buffer copies needed, unless the network stack did it
697 * or the hardware can't use skb buffers.
698 * or there's not enough space for extra headers we need
699 */
700 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500701 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700702
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500703 spin_lock_irqsave(&dev->lock, flags);
704 if (dev->port_usb)
705 skb = dev->wrap(dev->port_usb, skb);
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200706 spin_unlock_irqrestore(&dev->lock, flags);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100707 if (!skb) {
708 /* Multi frame CDC protocols may store the frame for
709 * later which is not a dropped frame.
710 */
Peter Chen88c09ea2016-07-01 15:33:29 +0800711 if (dev->port_usb &&
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200712 dev->port_usb->supports_multi_frame)
Jim Baxter6d3865f2014-07-07 18:33:18 +0100713 goto multiframe;
David Brownell2b3d9422008-06-19 18:19:28 -0700714 goto drop;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100715 }
David Brownell2b3d9422008-06-19 18:19:28 -0700716 }
Jim Baxter6d3865f2014-07-07 18:33:18 +0100717
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700718 spin_lock_irqsave(&dev->req_lock, flags);
719 dev->tx_skb_hold_count++;
720 spin_unlock_irqrestore(&dev->req_lock, flags);
721
722 if (dev->port_usb->multi_pkt_xfer) {
723 memcpy(req->buf + req->length, skb->data, skb->len);
724 req->length = req->length + skb->len;
725 length = req->length;
726 dev_kfree_skb_any(skb);
727
728 spin_lock_irqsave(&dev->req_lock, flags);
xerox_lincdffcb82014-09-04 16:01:59 +0800729 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700730 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
731 list_add(&req->list, &dev->tx_reqs);
732 spin_unlock_irqrestore(&dev->req_lock, flags);
733 goto success;
734 }
735 }
736
737 dev->no_tx_req_used++;
738 spin_unlock_irqrestore(&dev->req_lock, flags);
739
740 spin_lock_irqsave(&dev->lock, flags);
741 dev->tx_skb_hold_count = 0;
742 spin_unlock_irqrestore(&dev->lock, flags);
743 } else {
744 length = skb->len;
745 req->buf = skb->data;
746 req->context = skb;
747 }
748
David Brownell2b3d9422008-06-19 18:19:28 -0700749 req->complete = tx_complete;
750
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200751 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
Harish Jenny K N79775f42016-09-09 11:30:41 +0200752 if (dev->port_usb &&
753 dev->port_usb->is_fixed &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200754 length == dev->port_usb->fixed_in_len &&
755 (length % in->maxpacket) == 0)
756 req->zero = 0;
757 else
758 req->zero = 1;
759
David Brownell2b3d9422008-06-19 18:19:28 -0700760 /* use zlp framing on tx for strict CDC-Ether conformance,
761 * though any robust network rx path ignores extra padding.
762 * and some hardware doesn't like to write zlps.
763 */
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700764 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
765 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700766 length++;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700767 }
David Brownell2b3d9422008-06-19 18:19:28 -0700768
769 req->length = length;
770
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700771 /* throttle highspeed IRQ rate back slightly */
772 if (gadget_is_dualspeed(dev->gadget) &&
773 (dev->gadget->speed == USB_SPEED_HIGH)) {
774 dev->tx_qlen++;
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600775 if (dev->tx_qlen == (dev->qmult/2)) {
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700776 req->no_interrupt = 0;
777 dev->tx_qlen = 0;
778 } else {
779 req->no_interrupt = 1;
780 }
781 } else {
782 req->no_interrupt = 0;
783 }
David Brownell2b3d9422008-06-19 18:19:28 -0700784
785 retval = usb_ep_queue(in, req, GFP_ATOMIC);
786 switch (retval) {
787 default:
788 DBG(dev, "tx queue err %d\n", retval);
789 break;
790 case 0:
Florian Westphal860e9532016-05-03 16:33:13 +0200791 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700792 }
793
794 if (retval) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700795 if (!dev->port_usb->multi_pkt_xfer)
796 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700797drop:
798 dev->net->stats.tx_dropped++;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100799multiframe:
David Brownell2b3d9422008-06-19 18:19:28 -0700800 spin_lock_irqsave(&dev->req_lock, flags);
801 if (list_empty(&dev->tx_reqs))
802 netif_start_queue(net);
803 list_add(&req->list, &dev->tx_reqs);
804 spin_unlock_irqrestore(&dev->req_lock, flags);
805 }
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700806success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000807 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700808}
809
810/*-------------------------------------------------------------------------*/
811
812static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
813{
814 DBG(dev, "%s\n", __func__);
815
816 /* fill the rx queue */
817 rx_fill(dev, gfp_flags);
818
819 /* and open the tx floodgates */
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700820 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700821 netif_wake_queue(dev->net);
822}
823
824static int eth_open(struct net_device *net)
825{
826 struct eth_dev *dev = netdev_priv(net);
827 struct gether *link;
828
829 DBG(dev, "%s\n", __func__);
830 if (netif_carrier_ok(dev->net))
831 eth_start(dev, GFP_KERNEL);
832
833 spin_lock_irq(&dev->lock);
834 link = dev->port_usb;
835 if (link && link->open)
836 link->open(link);
837 spin_unlock_irq(&dev->lock);
838
839 return 0;
840}
841
842static int eth_stop(struct net_device *net)
843{
844 struct eth_dev *dev = netdev_priv(net);
845 unsigned long flags;
846
847 VDBG(dev, "%s\n", __func__);
848 netif_stop_queue(net);
849
850 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
851 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
852 dev->net->stats.rx_errors, dev->net->stats.tx_errors
853 );
854
855 /* ensure there are no more active requests */
856 spin_lock_irqsave(&dev->lock, flags);
857 if (dev->port_usb) {
858 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200859 const struct usb_endpoint_descriptor *in;
860 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700861
862 if (link->close)
863 link->close(link);
864
865 /* NOTE: we have no abort-queue primitive we could use
866 * to cancel all pending I/O. Instead, we disable then
867 * reenable the endpoints ... this idiom may leave toggle
868 * wrong, but that's a self-correcting error.
869 *
870 * REVISIT: we *COULD* just let the transfers complete at
871 * their own pace; the network stack can handle old packets.
872 * For the moment we leave this here, since it works.
873 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800874 if (link->in_ep) {
875 in = link->in_ep->desc;
876 usb_ep_disable(link->in_ep);
877 if (netif_carrier_ok(net)) {
878 DBG(dev, "host still using in endpoints\n");
879 link->in_ep->desc = in;
880 usb_ep_enable(link->in_ep);
881 }
882 }
883
884 if (link->out_ep) {
885 out = link->out_ep->desc;
886 usb_ep_disable(link->out_ep);
887 if (netif_carrier_ok(net)) {
888 DBG(dev, "host still using out endpoints\n");
889 link->out_ep->desc = out;
890 usb_ep_enable(link->out_ep);
891 }
David Brownell2b3d9422008-06-19 18:19:28 -0700892 }
893 }
894 spin_unlock_irqrestore(&dev->lock, flags);
895
896 return 0;
897}
898
899/*-------------------------------------------------------------------------*/
900
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200901static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700902{
903 if (str) {
904 unsigned i;
905
906 for (i = 0; i < 6; i++) {
907 unsigned char num;
908
909 if ((*str == '.') || (*str == ':'))
910 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300911 num = hex_to_bin(*str++) << 4;
912 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700913 dev_addr [i] = num;
914 }
915 if (is_valid_ether_addr(dev_addr))
916 return 0;
917 }
Joe Perches006c9132012-07-12 22:33:11 -0700918 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700919 return 1;
920}
921
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200922static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
923{
924 if (len < 18)
925 return -EINVAL;
926
Andy Shevchenko27f38702015-01-15 13:40:04 +0200927 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200928 return 18;
929}
930
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800931static int ether_ioctl(struct net_device *, struct ifreq *, int);
932
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800933static const struct net_device_ops eth_netdev_ops = {
934 .ndo_open = eth_open,
935 .ndo_stop = eth_stop,
936 .ndo_start_xmit = eth_start_xmit,
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800937 .ndo_do_ioctl = ether_ioctl,
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800938 .ndo_change_mtu = ueth_change_mtu,
939 .ndo_set_mac_address = eth_mac_addr,
940 .ndo_validate_addr = eth_validate_addr,
941};
David Brownell2b3d9422008-06-19 18:19:28 -0700942
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800943static const struct net_device_ops eth_netdev_ops_ip = {
944 .ndo_open = eth_open,
945 .ndo_stop = eth_stop,
946 .ndo_start_xmit = eth_start_xmit,
947 .ndo_do_ioctl = ether_ioctl,
948 .ndo_change_mtu = ueth_change_mtu_ip,
949 .ndo_set_mac_address = NULL,
950 .ndo_validate_addr = NULL,
951};
952
953static int rmnet_ioctl_extended(struct net_device *dev, struct ifreq *ifr)
954{
955 struct rmnet_ioctl_extended_s ext_cmd;
956 struct eth_dev *eth_dev = netdev_priv(dev);
957 int rc = 0;
958
959 rc = copy_from_user(&ext_cmd, ifr->ifr_ifru.ifru_data,
960 sizeof(struct rmnet_ioctl_extended_s));
961
962 if (rc) {
963 DBG(eth_dev, "%s(): copy_from_user() failed\n", __func__);
964 return rc;
965 }
966
967 switch (ext_cmd.extended_ioctl) {
968 case RMNET_IOCTL_GET_SUPPORTED_FEATURES:
969 ext_cmd.u.data = 0;
970 break;
971
972 case RMNET_IOCTL_SET_MRU:
973 if (netif_running(dev))
974 return -EBUSY;
975
976 /* 16K max */
977 if ((size_t)ext_cmd.u.data > 0x4000)
978 return -EINVAL;
979
980 if (eth_dev->port_usb) {
981 eth_dev->port_usb->is_fixed = true;
982 eth_dev->port_usb->fixed_out_len =
983 (size_t) ext_cmd.u.data;
984 DBG(eth_dev, "[%s] rmnet_ioctl(): SET MRU to %u\n",
985 dev->name, eth_dev->port_usb->fixed_out_len);
986 } else {
987 pr_err("[%s]: %s: SET MRU failed. Cable disconnected\n",
988 dev->name, __func__);
989 return -ENODEV;
990 }
991 break;
992
993 case RMNET_IOCTL_GET_MRU:
994 if (eth_dev->port_usb) {
995 ext_cmd.u.data = eth_dev->port_usb->is_fixed ?
996 eth_dev->port_usb->fixed_out_len :
997 dev->mtu;
998 } else {
999 pr_err("[%s]: %s: GET MRU failed. Cable disconnected\n",
1000 dev->name, __func__);
1001 return -ENODEV;
1002 }
1003 break;
1004
1005 case RMNET_IOCTL_GET_DRIVER_NAME:
1006 strlcpy(ext_cmd.u.if_name, dev->name,
1007 sizeof(ext_cmd.u.if_name));
1008 break;
1009
1010 default:
1011 break;
1012 }
1013
1014 rc = copy_to_user(ifr->ifr_ifru.ifru_data, &ext_cmd,
1015 sizeof(struct rmnet_ioctl_extended_s));
1016
1017 if (rc)
1018 DBG(eth_dev, "%s(): copy_to_user() failed\n", __func__);
1019 return rc;
1020}
1021
1022static int ether_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1023{
1024 struct eth_dev *eth_dev = netdev_priv(dev);
1025 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1026 int prev_mtu = dev->mtu;
1027 u32 state, old_opmode;
1028 int rc = -EFAULT;
1029
1030 old_opmode = eth_dev->flags;
1031 /* Process IOCTL command */
1032 switch (cmd) {
1033 case RMNET_IOCTL_SET_LLP_ETHERNET: /*Set Ethernet protocol*/
1034 /* Perform Ethernet config only if in IP mode currently*/
1035 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags)) {
1036 ether_setup(dev);
1037 dev->mtu = prev_mtu;
1038 dev->netdev_ops = &eth_netdev_ops;
1039 clear_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1040 set_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1041 DBG(eth_dev, "[%s] ioctl(): set Ethernet proto mode\n",
1042 dev->name);
1043 }
1044 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags))
1045 rc = 0;
1046 break;
1047
1048 case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol*/
1049 /* Perform IP config only if in Ethernet mode currently*/
1050 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags)) {
1051 /* Undo config done in ether_setup() */
1052 dev->header_ops = NULL; /* No header */
1053 dev->type = ARPHRD_RAWIP;
1054 dev->hard_header_len = 0;
1055 dev->mtu = prev_mtu;
1056 dev->addr_len = 0;
1057 dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
1058 dev->netdev_ops = &eth_netdev_ops_ip;
1059 clear_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1060 set_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1061 DBG(eth_dev, "[%s] ioctl(): set IP protocol mode\n",
1062 dev->name);
1063 }
1064 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags))
1065 rc = 0;
1066 break;
1067
1068 case RMNET_IOCTL_GET_LLP: /* Get link protocol state */
1069 state = eth_dev->flags & (RMNET_MODE_LLP_ETH
1070 | RMNET_MODE_LLP_IP);
1071 if (copy_to_user(addr, &state, sizeof(state)))
1072 break;
1073 rc = 0;
1074 break;
1075
1076 case RMNET_IOCTL_SET_RX_HEADROOM: /* Set RX headroom */
1077 if (copy_from_user(&eth_dev->rx_needed_headroom, addr,
1078 sizeof(eth_dev->rx_needed_headroom)))
1079 break;
1080 DBG(eth_dev, "[%s] ioctl(): set RX HEADROOM: %x\n",
1081 dev->name, eth_dev->rx_needed_headroom);
1082 rc = 0;
1083 break;
1084
1085 case RMNET_IOCTL_EXTENDED:
1086 rc = rmnet_ioctl_extended(dev, ifr);
1087 break;
1088
1089 default:
1090 pr_err("[%s] error: ioctl called for unsupported cmd[%d]",
1091 dev->name, cmd);
1092 rc = -EINVAL;
1093 }
1094
1095 DBG(eth_dev, "[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08lx\n",
1096 dev->name, __func__, cmd, old_opmode, eth_dev->flags);
1097
1098 return rc;
1099}
1100
Marcel Holtmannaa790742010-01-15 22:13:58 -08001101static struct device_type gadget_type = {
1102 .name = "gadget",
1103};
1104
David Brownell2b3d9422008-06-19 18:19:28 -07001105/**
Mike Lockwood036e98b2012-05-10 10:08:02 +02001106 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -07001107 * @g: gadget to associated with these links
1108 * @ethaddr: NULL, or a buffer in which the ethernet address of the
1109 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +02001110 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -07001111 * Context: may sleep
1112 *
1113 * This sets up the single network link that may be exported by a
1114 * gadget driver using this framework. The link layer addresses are
1115 * set up using module parameters.
1116 *
Dan Carpenter574f24f2013-11-14 11:42:11 +03001117 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -07001118 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001119struct eth_dev *gether_setup_name(struct usb_gadget *g,
1120 const char *dev_addr, const char *host_addr,
1121 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -07001122{
1123 struct eth_dev *dev;
1124 struct net_device *net;
1125 int status;
1126
David Brownell2b3d9422008-06-19 18:19:28 -07001127 net = alloc_etherdev(sizeof *dev);
1128 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001129 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -07001130
1131 dev = netdev_priv(net);
1132 spin_lock_init(&dev->lock);
1133 spin_lock_init(&dev->req_lock);
1134 INIT_WORK(&dev->work, eth_work);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001135 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -07001136 INIT_LIST_HEAD(&dev->tx_reqs);
1137 INIT_LIST_HEAD(&dev->rx_reqs);
1138
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001139 skb_queue_head_init(&dev->rx_frames);
1140
David Brownell2b3d9422008-06-19 18:19:28 -07001141 /* network device setup */
1142 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001143 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +02001144 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -07001145
1146 if (get_ether_addr(dev_addr, net->dev_addr))
1147 dev_warn(&g->dev,
1148 "using random %s ethernet address\n", "self");
1149 if (get_ether_addr(host_addr, dev->host_mac))
1150 dev_warn(&g->dev,
1151 "using random %s ethernet address\n", "host");
1152
1153 if (ethaddr)
1154 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
1155
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001156 net->netdev_ops = &eth_netdev_ops;
1157
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001158 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -07001159
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001160 /* set operation mode to eth by default */
1161 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1162
David Brownell2b3d9422008-06-19 18:19:28 -07001163 dev->gadget = g;
1164 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -08001165 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -07001166
1167 status = register_netdev(net);
1168 if (status < 0) {
1169 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1170 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001171 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -07001172 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001173 INFO(dev, "MAC %pM\n", net->dev_addr);
1174 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001175
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001176 /*
1177 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -07001178 * - iff DATA transfer is active, carrier is "on"
1179 * - tx queueing enabled if open *and* carrier is "on"
1180 */
1181 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -07001182 }
1183
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001184 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -07001185}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001186EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -07001187
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001188struct net_device *gether_setup_name_default(const char *netname)
1189{
1190 struct net_device *net;
1191 struct eth_dev *dev;
1192
1193 net = alloc_etherdev(sizeof(*dev));
1194 if (!net)
1195 return ERR_PTR(-ENOMEM);
1196
1197 dev = netdev_priv(net);
1198 spin_lock_init(&dev->lock);
1199 spin_lock_init(&dev->req_lock);
1200 INIT_WORK(&dev->work, eth_work);
Matthew Moeller5df32222016-03-09 20:19:25 -06001201 INIT_WORK(&dev->rx_work, process_rx_w);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001202 INIT_LIST_HEAD(&dev->tx_reqs);
1203 INIT_LIST_HEAD(&dev->rx_reqs);
1204
1205 skb_queue_head_init(&dev->rx_frames);
1206
1207 /* network device setup */
1208 dev->net = net;
1209 dev->qmult = QMULT_DEFAULT;
1210 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
1211
1212 eth_random_addr(dev->dev_mac);
1213 pr_warn("using random %s ethernet address\n", "self");
1214 eth_random_addr(dev->host_mac);
1215 pr_warn("using random %s ethernet address\n", "host");
1216
1217 net->netdev_ops = &eth_netdev_ops;
1218
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001219 net->ethtool_ops = &ops;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001220
1221 /* set operation mode to eth by default */
1222 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1223
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001224 SET_NETDEV_DEVTYPE(net, &gadget_type);
1225
1226 return net;
1227}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001228EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001229
1230int gether_register_netdev(struct net_device *net)
1231{
1232 struct eth_dev *dev;
1233 struct usb_gadget *g;
1234 struct sockaddr sa;
1235 int status;
1236
1237 if (!net->dev.parent)
1238 return -EINVAL;
1239 dev = netdev_priv(net);
1240 g = dev->gadget;
1241 status = register_netdev(net);
1242 if (status < 0) {
1243 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1244 return status;
1245 } else {
1246 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
1247
1248 /* two kinds of host-initiated state changes:
1249 * - iff DATA transfer is active, carrier is "on"
1250 * - tx queueing enabled if open *and* carrier is "on"
1251 */
1252 netif_carrier_off(net);
1253 }
1254 sa.sa_family = net->type;
1255 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
1256 rtnl_lock();
1257 status = dev_set_mac_address(net, &sa);
1258 rtnl_unlock();
1259 if (status)
1260 pr_warn("cannot set self ethernet address: %d\n", status);
1261 else
1262 INFO(dev, "MAC %pM\n", dev->dev_mac);
1263
1264 return status;
1265}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001266EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001267
1268void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
1269{
1270 struct eth_dev *dev;
1271
1272 dev = netdev_priv(net);
1273 dev->gadget = g;
1274 SET_NETDEV_DEV(net, &g->dev);
1275}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001276EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001277
1278int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
1279{
1280 struct eth_dev *dev;
1281 u8 new_addr[ETH_ALEN];
1282
1283 dev = netdev_priv(net);
1284 if (get_ether_addr(dev_addr, new_addr))
1285 return -EINVAL;
1286 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
1287 return 0;
1288}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001289EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001290
1291int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
1292{
1293 struct eth_dev *dev;
1294
1295 dev = netdev_priv(net);
1296 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
1297}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001298EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001299
1300int gether_set_host_addr(struct net_device *net, const char *host_addr)
1301{
1302 struct eth_dev *dev;
1303 u8 new_addr[ETH_ALEN];
1304
1305 dev = netdev_priv(net);
1306 if (get_ether_addr(host_addr, new_addr))
1307 return -EINVAL;
1308 memcpy(dev->host_mac, new_addr, ETH_ALEN);
1309 return 0;
1310}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001311EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001312
1313int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
1314{
1315 struct eth_dev *dev;
1316
1317 dev = netdev_priv(net);
1318 return get_ether_addr_str(dev->host_mac, host_addr, len);
1319}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001320EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001321
1322int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
1323{
1324 struct eth_dev *dev;
1325
1326 if (len < 13)
1327 return -EINVAL;
1328
1329 dev = netdev_priv(net);
1330 snprintf(host_addr, len, "%pm", dev->host_mac);
1331
1332 return strlen(host_addr);
1333}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001334EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001335
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001336void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
1337{
1338 struct eth_dev *dev;
1339
1340 dev = netdev_priv(net);
1341 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1342}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001343EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001344
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001345void gether_set_qmult(struct net_device *net, unsigned qmult)
1346{
1347 struct eth_dev *dev;
1348
1349 dev = netdev_priv(net);
1350 dev->qmult = qmult;
1351}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001352EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001353
1354unsigned gether_get_qmult(struct net_device *net)
1355{
1356 struct eth_dev *dev;
1357
1358 dev = netdev_priv(net);
1359 return dev->qmult;
1360}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001361EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001362
1363int gether_get_ifname(struct net_device *net, char *name, int len)
1364{
1365 rtnl_lock();
1366 strlcpy(name, netdev_name(net), len);
1367 rtnl_unlock();
1368 return strlen(name);
1369}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001370EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001371
David Brownell2b3d9422008-06-19 18:19:28 -07001372/**
1373 * gether_cleanup - remove Ethernet-over-USB device
1374 * Context: may sleep
1375 *
1376 * This is called to free all resources allocated by @gether_setup().
1377 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001378void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001379{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001380 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001381 return;
1382
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001383 unregister_netdev(dev->net);
1384 flush_work(&dev->work);
1385 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001386}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001387EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001388
David Brownell2b3d9422008-06-19 18:19:28 -07001389/**
1390 * gether_connect - notify network layer that USB link is active
1391 * @link: the USB link, set up with endpoints, descriptors matching
1392 * current device speed, and any framing wrapper(s) set up.
1393 * Context: irqs blocked
1394 *
1395 * This is called to activate endpoints and let the network layer know
1396 * the connection is active ("carrier detect"). It may cause the I/O
1397 * queues to open and start letting network packets flow, but will in
1398 * any case activate the endpoints so that they respond properly to the
1399 * USB host.
1400 *
1401 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1402 * indicate some error code (negative errno), ep->driver_data values
1403 * have been overwritten.
1404 */
1405struct net_device *gether_connect(struct gether *link)
1406{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001407 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001408 int result = 0;
1409
1410 if (!dev)
1411 return ERR_PTR(-EINVAL);
1412
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001413 if (link->in_ep) {
1414 link->in_ep->driver_data = dev;
1415 result = usb_ep_enable(link->in_ep);
1416 if (result != 0) {
1417 DBG(dev, "enable %s --> %d\n",
1418 link->in_ep->name, result);
1419 goto fail0;
1420 }
David Brownell2b3d9422008-06-19 18:19:28 -07001421 }
1422
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001423 if (link->out_ep) {
1424 link->out_ep->driver_data = dev;
1425 result = usb_ep_enable(link->out_ep);
1426 if (result != 0) {
1427 DBG(dev, "enable %s --> %d\n",
1428 link->out_ep->name, result);
1429 goto fail1;
1430 }
David Brownell2b3d9422008-06-19 18:19:28 -07001431 }
1432
1433 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001434 result = alloc_requests(dev, link, qlen(dev->gadget,
1435 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001436
1437 if (result == 0) {
1438 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001439 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001440 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001441
1442 dev->header_len = link->header_len;
1443 dev->unwrap = link->unwrap;
1444 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001445 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +08001446 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001447
1448 spin_lock(&dev->lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -07001449 dev->tx_skb_hold_count = 0;
1450 dev->no_tx_req_used = 0;
1451 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001452 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001453 if (netif_running(dev->net)) {
1454 if (link->open)
1455 link->open(link);
1456 } else {
1457 if (link->close)
1458 link->close(link);
1459 }
David Brownell2b3d9422008-06-19 18:19:28 -07001460 spin_unlock(&dev->lock);
1461
1462 netif_carrier_on(dev->net);
1463 if (netif_running(dev->net))
1464 eth_start(dev, GFP_ATOMIC);
1465
1466 /* on error, disable any endpoints */
1467 } else {
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001468 if (link->out_ep)
1469 (void) usb_ep_disable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001470fail1:
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001471 if (link->in_ep)
1472 (void) usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001473 }
1474fail0:
1475 /* caller is responsible for cleanup on error */
1476 if (result < 0)
1477 return ERR_PTR(result);
1478 return dev->net;
1479}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001480EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001481
1482/**
1483 * gether_disconnect - notify network layer that USB link is inactive
1484 * @link: the USB link, on which gether_connect() was called
1485 * Context: irqs blocked
1486 *
1487 * This is called to deactivate endpoints and let the network layer know
1488 * the connection went inactive ("no carrier").
1489 *
1490 * On return, the state is as if gether_connect() had never been called.
1491 * The endpoints are inactive, and accordingly without active USB I/O.
1492 * Pointers to endpoint descriptors and endpoint private data are nulled.
1493 */
1494void gether_disconnect(struct gether *link)
1495{
1496 struct eth_dev *dev = link->ioport;
1497 struct usb_request *req;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001498 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001499
1500 WARN_ON(!dev);
1501 if (!dev)
1502 return;
1503
1504 DBG(dev, "%s\n", __func__);
1505
1506 netif_stop_queue(dev->net);
1507 netif_carrier_off(dev->net);
1508
1509 /* disable endpoints, forcing (synchronous) completion
1510 * of all pending i/o. then free the request objects
1511 * and forget about the endpoints.
1512 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001513 if (link->in_ep) {
1514 usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001515 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001516 while (!list_empty(&dev->tx_reqs)) {
1517 req = container_of(dev->tx_reqs.next,
1518 struct usb_request, list);
1519 list_del(&req->list);
David Brownell2b3d9422008-06-19 18:19:28 -07001520
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001521 spin_unlock(&dev->req_lock);
1522 if (link->multi_pkt_xfer)
1523 kfree(req->buf);
1524 usb_ep_free_request(link->in_ep, req);
1525 spin_lock(&dev->req_lock);
1526 }
David Brownell2b3d9422008-06-19 18:19:28 -07001527 spin_unlock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001528 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001529 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001530
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001531 if (link->out_ep) {
1532 usb_ep_disable(link->out_ep);
1533 spin_lock(&dev->req_lock);
1534 while (!list_empty(&dev->rx_reqs)) {
1535 req = container_of(dev->rx_reqs.next,
1536 struct usb_request, list);
1537 list_del(&req->list);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001538
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001539 spin_unlock(&dev->req_lock);
1540 usb_ep_free_request(link->out_ep, req);
1541 spin_lock(&dev->req_lock);
1542 }
1543 spin_unlock(&dev->req_lock);
1544
1545 spin_lock(&dev->rx_frames.lock);
1546 while ((skb = __skb_dequeue(&dev->rx_frames)))
1547 dev_kfree_skb_any(skb);
1548 spin_unlock(&dev->rx_frames.lock);
1549
1550 link->out_ep->desc = NULL;
1551 }
David Brownell2b3d9422008-06-19 18:19:28 -07001552
1553 /* finish forgetting about this USB link episode */
1554 dev->header_len = 0;
1555 dev->unwrap = NULL;
1556 dev->wrap = NULL;
1557
1558 spin_lock(&dev->lock);
1559 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001560 spin_unlock(&dev->lock);
1561}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001562EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001563
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001564static int __init gether_init(void)
1565{
1566 uether_wq = create_singlethread_workqueue("uether");
1567 if (!uether_wq) {
1568 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1569 return -ENOMEM;
1570 }
1571 return 0;
1572}
1573module_init(gether_init);
1574
1575static void __exit gether_exit(void)
1576{
1577 destroy_workqueue(uether_wq);
1578
1579}
1580module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001581MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001582MODULE_DESCRIPTION("ethernet over USB driver");
1583MODULE_LICENSE("GPL v2");