blob: d50510f091484b8702ceb3fcbca1f7559f2d51eb [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
514 switch (req->status) {
515 default:
516 dev->net->stats.tx_errors++;
517 VDBG(dev, "tx err %d\n", req->status);
518 /* FALLTHROUGH */
519 case -ECONNRESET: /* unlink */
520 case -ESHUTDOWN: /* disconnect etc */
521 break;
522 case 0:
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700523 if (!req->zero)
524 dev->net->stats.tx_bytes += req->length-1;
525 else
526 dev->net->stats.tx_bytes += req->length;
David Brownell2b3d9422008-06-19 18:19:28 -0700527 }
528 dev->net->stats.tx_packets++;
529
530 spin_lock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700531 list_add_tail(&req->list, &dev->tx_reqs);
532
533 if (dev->port_usb->multi_pkt_xfer) {
534 dev->no_tx_req_used--;
535 req->length = 0;
536 in = dev->port_usb->in_ep;
537
538 if (!list_empty(&dev->tx_reqs)) {
539 new_req = container_of(dev->tx_reqs.next,
540 struct usb_request, list);
541 list_del(&new_req->list);
542 spin_unlock(&dev->req_lock);
543 if (new_req->length > 0) {
544 length = new_req->length;
545
546 /* NCM requires no zlp if transfer is
547 * dwNtbInMaxSize */
548 if (dev->port_usb->is_fixed &&
549 length == dev->port_usb->fixed_in_len &&
550 (length % in->maxpacket) == 0)
551 new_req->zero = 0;
552 else
553 new_req->zero = 1;
554
555 /* use zlp framing on tx for strict CDC-Ether
556 * conformance, though any robust network rx
557 * path ignores extra padding. and some hardware
558 * doesn't like to write zlps.
559 */
560 if (new_req->zero && !dev->zlp &&
561 (length % in->maxpacket) == 0) {
562 new_req->zero = 0;
563 length++;
564 }
565
566 new_req->length = length;
567 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
568 switch (retval) {
569 default:
570 DBG(dev, "tx queue err %d\n", retval);
571 break;
572 case 0:
573 spin_lock(&dev->req_lock);
574 dev->no_tx_req_used++;
575 spin_unlock(&dev->req_lock);
Amit Pundir09172082016-05-30 15:19:21 +0530576 netif_trans_update(net);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700577 }
578 } else {
579 spin_lock(&dev->req_lock);
580 list_add(&new_req->list, &dev->tx_reqs);
581 spin_unlock(&dev->req_lock);
582 }
583 } else {
584 spin_unlock(&dev->req_lock);
585 }
586 } else {
587 spin_unlock(&dev->req_lock);
588 dev_kfree_skb_any(skb);
589 }
David Brownell2b3d9422008-06-19 18:19:28 -0700590
David Brownell2b3d9422008-06-19 18:19:28 -0700591 if (netif_carrier_ok(dev->net))
592 netif_wake_queue(dev->net);
593}
594
595static inline int is_promisc(u16 cdc_filter)
596{
597 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
598}
599
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700600static void alloc_tx_buffer(struct eth_dev *dev)
601{
602 struct list_head *act;
603 struct usb_request *req;
604
xerox_lincdffcb82014-09-04 16:01:59 +0800605 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700606 (dev->net->mtu
607 + sizeof(struct ethhdr)
608 /* size of rndis_packet_msg_type */
609 + 44
610 + 22));
611
612 list_for_each(act, &dev->tx_reqs) {
613 req = container_of(act, struct usb_request, list);
614 if (!req->buf)
615 req->buf = kmalloc(dev->tx_req_bufsize,
616 GFP_ATOMIC);
617 }
618}
619
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000620static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
621 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700622{
623 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100624 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700625 int retval;
626 struct usb_request *req = NULL;
627 unsigned long flags;
628 struct usb_ep *in;
629 u16 cdc_filter;
630
631 spin_lock_irqsave(&dev->lock, flags);
632 if (dev->port_usb) {
633 in = dev->port_usb->in_ep;
634 cdc_filter = dev->port_usb->cdc_filter;
635 } else {
636 in = NULL;
637 cdc_filter = 0;
638 }
639 spin_unlock_irqrestore(&dev->lock, flags);
640
Jim Baxter6d3865f2014-07-07 18:33:18 +0100641 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700642 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000643 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700644 }
645
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700646 /* Allocate memory for tx_reqs to support multi packet transfer */
647 if (dev->port_usb->multi_pkt_xfer && !dev->tx_req_bufsize)
648 alloc_tx_buffer(dev);
649
David Brownell2b3d9422008-06-19 18:19:28 -0700650 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100651 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700652 u8 *dest = skb->data;
653
654 if (is_multicast_ether_addr(dest)) {
655 u16 type;
656
657 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
658 * SET_ETHERNET_MULTICAST_FILTERS requests
659 */
660 if (is_broadcast_ether_addr(dest))
661 type = USB_CDC_PACKET_TYPE_BROADCAST;
662 else
663 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
664 if (!(cdc_filter & type)) {
665 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000666 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700667 }
668 }
669 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
670 }
671
672 spin_lock_irqsave(&dev->req_lock, flags);
673 /*
674 * this freelist can be empty if an interrupt triggered disconnect()
675 * and reconfigured the gadget (shutting down this queue) after the
676 * network stack decided to xmit but before we got the spinlock.
677 */
678 if (list_empty(&dev->tx_reqs)) {
679 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000680 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700681 }
682
683 req = container_of(dev->tx_reqs.next, struct usb_request, list);
684 list_del(&req->list);
685
686 /* temporarily stop TX queue when the freelist empties */
687 if (list_empty(&dev->tx_reqs))
688 netif_stop_queue(net);
689 spin_unlock_irqrestore(&dev->req_lock, flags);
690
691 /* no buffer copies needed, unless the network stack did it
692 * or the hardware can't use skb buffers.
693 * or there's not enough space for extra headers we need
694 */
695 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500696 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700697
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500698 spin_lock_irqsave(&dev->lock, flags);
699 if (dev->port_usb)
700 skb = dev->wrap(dev->port_usb, skb);
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200701 spin_unlock_irqrestore(&dev->lock, flags);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100702 if (!skb) {
703 /* Multi frame CDC protocols may store the frame for
704 * later which is not a dropped frame.
705 */
Peter Chen88c09ea2016-07-01 15:33:29 +0800706 if (dev->port_usb &&
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200707 dev->port_usb->supports_multi_frame)
Jim Baxter6d3865f2014-07-07 18:33:18 +0100708 goto multiframe;
David Brownell2b3d9422008-06-19 18:19:28 -0700709 goto drop;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100710 }
David Brownell2b3d9422008-06-19 18:19:28 -0700711 }
Jim Baxter6d3865f2014-07-07 18:33:18 +0100712
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700713 spin_lock_irqsave(&dev->req_lock, flags);
714 dev->tx_skb_hold_count++;
715 spin_unlock_irqrestore(&dev->req_lock, flags);
716
717 if (dev->port_usb->multi_pkt_xfer) {
718 memcpy(req->buf + req->length, skb->data, skb->len);
719 req->length = req->length + skb->len;
720 length = req->length;
721 dev_kfree_skb_any(skb);
722
723 spin_lock_irqsave(&dev->req_lock, flags);
xerox_lincdffcb82014-09-04 16:01:59 +0800724 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700725 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
726 list_add(&req->list, &dev->tx_reqs);
727 spin_unlock_irqrestore(&dev->req_lock, flags);
728 goto success;
729 }
730 }
731
732 dev->no_tx_req_used++;
733 spin_unlock_irqrestore(&dev->req_lock, flags);
734
735 spin_lock_irqsave(&dev->lock, flags);
736 dev->tx_skb_hold_count = 0;
737 spin_unlock_irqrestore(&dev->lock, flags);
738 } else {
739 length = skb->len;
740 req->buf = skb->data;
741 req->context = skb;
742 }
743
David Brownell2b3d9422008-06-19 18:19:28 -0700744 req->complete = tx_complete;
745
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200746 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
Harish Jenny K N79775f42016-09-09 11:30:41 +0200747 if (dev->port_usb &&
748 dev->port_usb->is_fixed &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200749 length == dev->port_usb->fixed_in_len &&
750 (length % in->maxpacket) == 0)
751 req->zero = 0;
752 else
753 req->zero = 1;
754
David Brownell2b3d9422008-06-19 18:19:28 -0700755 /* use zlp framing on tx for strict CDC-Ether conformance,
756 * though any robust network rx path ignores extra padding.
757 * and some hardware doesn't like to write zlps.
758 */
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700759 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
760 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700761 length++;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700762 }
David Brownell2b3d9422008-06-19 18:19:28 -0700763
764 req->length = length;
765
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700766 /* throttle highspeed IRQ rate back slightly */
767 if (gadget_is_dualspeed(dev->gadget) &&
768 (dev->gadget->speed == USB_SPEED_HIGH)) {
769 dev->tx_qlen++;
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600770 if (dev->tx_qlen == (dev->qmult/2)) {
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700771 req->no_interrupt = 0;
772 dev->tx_qlen = 0;
773 } else {
774 req->no_interrupt = 1;
775 }
776 } else {
777 req->no_interrupt = 0;
778 }
David Brownell2b3d9422008-06-19 18:19:28 -0700779
780 retval = usb_ep_queue(in, req, GFP_ATOMIC);
781 switch (retval) {
782 default:
783 DBG(dev, "tx queue err %d\n", retval);
784 break;
785 case 0:
Florian Westphal860e9532016-05-03 16:33:13 +0200786 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700787 }
788
789 if (retval) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700790 if (!dev->port_usb->multi_pkt_xfer)
791 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700792drop:
793 dev->net->stats.tx_dropped++;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100794multiframe:
David Brownell2b3d9422008-06-19 18:19:28 -0700795 spin_lock_irqsave(&dev->req_lock, flags);
796 if (list_empty(&dev->tx_reqs))
797 netif_start_queue(net);
798 list_add(&req->list, &dev->tx_reqs);
799 spin_unlock_irqrestore(&dev->req_lock, flags);
800 }
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700801success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000802 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700803}
804
805/*-------------------------------------------------------------------------*/
806
807static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
808{
809 DBG(dev, "%s\n", __func__);
810
811 /* fill the rx queue */
812 rx_fill(dev, gfp_flags);
813
814 /* and open the tx floodgates */
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700815 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700816 netif_wake_queue(dev->net);
817}
818
819static int eth_open(struct net_device *net)
820{
821 struct eth_dev *dev = netdev_priv(net);
822 struct gether *link;
823
824 DBG(dev, "%s\n", __func__);
825 if (netif_carrier_ok(dev->net))
826 eth_start(dev, GFP_KERNEL);
827
828 spin_lock_irq(&dev->lock);
829 link = dev->port_usb;
830 if (link && link->open)
831 link->open(link);
832 spin_unlock_irq(&dev->lock);
833
834 return 0;
835}
836
837static int eth_stop(struct net_device *net)
838{
839 struct eth_dev *dev = netdev_priv(net);
840 unsigned long flags;
841
842 VDBG(dev, "%s\n", __func__);
843 netif_stop_queue(net);
844
845 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
846 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
847 dev->net->stats.rx_errors, dev->net->stats.tx_errors
848 );
849
850 /* ensure there are no more active requests */
851 spin_lock_irqsave(&dev->lock, flags);
852 if (dev->port_usb) {
853 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200854 const struct usb_endpoint_descriptor *in;
855 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700856
857 if (link->close)
858 link->close(link);
859
860 /* NOTE: we have no abort-queue primitive we could use
861 * to cancel all pending I/O. Instead, we disable then
862 * reenable the endpoints ... this idiom may leave toggle
863 * wrong, but that's a self-correcting error.
864 *
865 * REVISIT: we *COULD* just let the transfers complete at
866 * their own pace; the network stack can handle old packets.
867 * For the moment we leave this here, since it works.
868 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -0800869 if (link->in_ep) {
870 in = link->in_ep->desc;
871 usb_ep_disable(link->in_ep);
872 if (netif_carrier_ok(net)) {
873 DBG(dev, "host still using in endpoints\n");
874 link->in_ep->desc = in;
875 usb_ep_enable(link->in_ep);
876 }
877 }
878
879 if (link->out_ep) {
880 out = link->out_ep->desc;
881 usb_ep_disable(link->out_ep);
882 if (netif_carrier_ok(net)) {
883 DBG(dev, "host still using out endpoints\n");
884 link->out_ep->desc = out;
885 usb_ep_enable(link->out_ep);
886 }
David Brownell2b3d9422008-06-19 18:19:28 -0700887 }
888 }
889 spin_unlock_irqrestore(&dev->lock, flags);
890
891 return 0;
892}
893
894/*-------------------------------------------------------------------------*/
895
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200896static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700897{
898 if (str) {
899 unsigned i;
900
901 for (i = 0; i < 6; i++) {
902 unsigned char num;
903
904 if ((*str == '.') || (*str == ':'))
905 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300906 num = hex_to_bin(*str++) << 4;
907 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700908 dev_addr [i] = num;
909 }
910 if (is_valid_ether_addr(dev_addr))
911 return 0;
912 }
Joe Perches006c9132012-07-12 22:33:11 -0700913 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700914 return 1;
915}
916
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200917static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
918{
919 if (len < 18)
920 return -EINVAL;
921
Andy Shevchenko27f38702015-01-15 13:40:04 +0200922 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200923 return 18;
924}
925
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800926static int ether_ioctl(struct net_device *, struct ifreq *, int);
927
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800928static const struct net_device_ops eth_netdev_ops = {
929 .ndo_open = eth_open,
930 .ndo_stop = eth_stop,
931 .ndo_start_xmit = eth_start_xmit,
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800932 .ndo_do_ioctl = ether_ioctl,
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800933 .ndo_change_mtu = ueth_change_mtu,
934 .ndo_set_mac_address = eth_mac_addr,
935 .ndo_validate_addr = eth_validate_addr,
936};
David Brownell2b3d9422008-06-19 18:19:28 -0700937
Hemant Kumarfc49dbd2018-01-23 12:08:47 -0800938static const struct net_device_ops eth_netdev_ops_ip = {
939 .ndo_open = eth_open,
940 .ndo_stop = eth_stop,
941 .ndo_start_xmit = eth_start_xmit,
942 .ndo_do_ioctl = ether_ioctl,
943 .ndo_change_mtu = ueth_change_mtu_ip,
944 .ndo_set_mac_address = NULL,
945 .ndo_validate_addr = NULL,
946};
947
948static int rmnet_ioctl_extended(struct net_device *dev, struct ifreq *ifr)
949{
950 struct rmnet_ioctl_extended_s ext_cmd;
951 struct eth_dev *eth_dev = netdev_priv(dev);
952 int rc = 0;
953
954 rc = copy_from_user(&ext_cmd, ifr->ifr_ifru.ifru_data,
955 sizeof(struct rmnet_ioctl_extended_s));
956
957 if (rc) {
958 DBG(eth_dev, "%s(): copy_from_user() failed\n", __func__);
959 return rc;
960 }
961
962 switch (ext_cmd.extended_ioctl) {
963 case RMNET_IOCTL_GET_SUPPORTED_FEATURES:
964 ext_cmd.u.data = 0;
965 break;
966
967 case RMNET_IOCTL_SET_MRU:
968 if (netif_running(dev))
969 return -EBUSY;
970
971 /* 16K max */
972 if ((size_t)ext_cmd.u.data > 0x4000)
973 return -EINVAL;
974
975 if (eth_dev->port_usb) {
976 eth_dev->port_usb->is_fixed = true;
977 eth_dev->port_usb->fixed_out_len =
978 (size_t) ext_cmd.u.data;
979 DBG(eth_dev, "[%s] rmnet_ioctl(): SET MRU to %u\n",
980 dev->name, eth_dev->port_usb->fixed_out_len);
981 } else {
982 pr_err("[%s]: %s: SET MRU failed. Cable disconnected\n",
983 dev->name, __func__);
984 return -ENODEV;
985 }
986 break;
987
988 case RMNET_IOCTL_GET_MRU:
989 if (eth_dev->port_usb) {
990 ext_cmd.u.data = eth_dev->port_usb->is_fixed ?
991 eth_dev->port_usb->fixed_out_len :
992 dev->mtu;
993 } else {
994 pr_err("[%s]: %s: GET MRU failed. Cable disconnected\n",
995 dev->name, __func__);
996 return -ENODEV;
997 }
998 break;
999
1000 case RMNET_IOCTL_GET_DRIVER_NAME:
1001 strlcpy(ext_cmd.u.if_name, dev->name,
1002 sizeof(ext_cmd.u.if_name));
1003 break;
1004
1005 default:
1006 break;
1007 }
1008
1009 rc = copy_to_user(ifr->ifr_ifru.ifru_data, &ext_cmd,
1010 sizeof(struct rmnet_ioctl_extended_s));
1011
1012 if (rc)
1013 DBG(eth_dev, "%s(): copy_to_user() failed\n", __func__);
1014 return rc;
1015}
1016
1017static int ether_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1018{
1019 struct eth_dev *eth_dev = netdev_priv(dev);
1020 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
1021 int prev_mtu = dev->mtu;
1022 u32 state, old_opmode;
1023 int rc = -EFAULT;
1024
1025 old_opmode = eth_dev->flags;
1026 /* Process IOCTL command */
1027 switch (cmd) {
1028 case RMNET_IOCTL_SET_LLP_ETHERNET: /*Set Ethernet protocol*/
1029 /* Perform Ethernet config only if in IP mode currently*/
1030 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags)) {
1031 ether_setup(dev);
1032 dev->mtu = prev_mtu;
1033 dev->netdev_ops = &eth_netdev_ops;
1034 clear_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1035 set_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1036 DBG(eth_dev, "[%s] ioctl(): set Ethernet proto mode\n",
1037 dev->name);
1038 }
1039 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags))
1040 rc = 0;
1041 break;
1042
1043 case RMNET_IOCTL_SET_LLP_IP: /* Set RAWIP protocol*/
1044 /* Perform IP config only if in Ethernet mode currently*/
1045 if (test_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags)) {
1046 /* Undo config done in ether_setup() */
1047 dev->header_ops = NULL; /* No header */
1048 dev->type = ARPHRD_RAWIP;
1049 dev->hard_header_len = 0;
1050 dev->mtu = prev_mtu;
1051 dev->addr_len = 0;
1052 dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
1053 dev->netdev_ops = &eth_netdev_ops_ip;
1054 clear_bit(RMNET_MODE_LLP_ETH, &eth_dev->flags);
1055 set_bit(RMNET_MODE_LLP_IP, &eth_dev->flags);
1056 DBG(eth_dev, "[%s] ioctl(): set IP protocol mode\n",
1057 dev->name);
1058 }
1059 if (test_bit(RMNET_MODE_LLP_IP, &eth_dev->flags))
1060 rc = 0;
1061 break;
1062
1063 case RMNET_IOCTL_GET_LLP: /* Get link protocol state */
1064 state = eth_dev->flags & (RMNET_MODE_LLP_ETH
1065 | RMNET_MODE_LLP_IP);
1066 if (copy_to_user(addr, &state, sizeof(state)))
1067 break;
1068 rc = 0;
1069 break;
1070
1071 case RMNET_IOCTL_SET_RX_HEADROOM: /* Set RX headroom */
1072 if (copy_from_user(&eth_dev->rx_needed_headroom, addr,
1073 sizeof(eth_dev->rx_needed_headroom)))
1074 break;
1075 DBG(eth_dev, "[%s] ioctl(): set RX HEADROOM: %x\n",
1076 dev->name, eth_dev->rx_needed_headroom);
1077 rc = 0;
1078 break;
1079
1080 case RMNET_IOCTL_EXTENDED:
1081 rc = rmnet_ioctl_extended(dev, ifr);
1082 break;
1083
1084 default:
1085 pr_err("[%s] error: ioctl called for unsupported cmd[%d]",
1086 dev->name, cmd);
1087 rc = -EINVAL;
1088 }
1089
1090 DBG(eth_dev, "[%s] %s: cmd=0x%x opmode old=0x%08x new=0x%08lx\n",
1091 dev->name, __func__, cmd, old_opmode, eth_dev->flags);
1092
1093 return rc;
1094}
1095
Marcel Holtmannaa790742010-01-15 22:13:58 -08001096static struct device_type gadget_type = {
1097 .name = "gadget",
1098};
1099
David Brownell2b3d9422008-06-19 18:19:28 -07001100/**
Mike Lockwood036e98b2012-05-10 10:08:02 +02001101 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -07001102 * @g: gadget to associated with these links
1103 * @ethaddr: NULL, or a buffer in which the ethernet address of the
1104 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +02001105 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -07001106 * Context: may sleep
1107 *
1108 * This sets up the single network link that may be exported by a
1109 * gadget driver using this framework. The link layer addresses are
1110 * set up using module parameters.
1111 *
Dan Carpenter574f24f2013-11-14 11:42:11 +03001112 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -07001113 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001114struct eth_dev *gether_setup_name(struct usb_gadget *g,
1115 const char *dev_addr, const char *host_addr,
1116 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -07001117{
1118 struct eth_dev *dev;
1119 struct net_device *net;
1120 int status;
1121
David Brownell2b3d9422008-06-19 18:19:28 -07001122 net = alloc_etherdev(sizeof *dev);
1123 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001124 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -07001125
1126 dev = netdev_priv(net);
1127 spin_lock_init(&dev->lock);
1128 spin_lock_init(&dev->req_lock);
1129 INIT_WORK(&dev->work, eth_work);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001130 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -07001131 INIT_LIST_HEAD(&dev->tx_reqs);
1132 INIT_LIST_HEAD(&dev->rx_reqs);
1133
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -05001134 skb_queue_head_init(&dev->rx_frames);
1135
David Brownell2b3d9422008-06-19 18:19:28 -07001136 /* network device setup */
1137 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001138 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +02001139 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -07001140
1141 if (get_ether_addr(dev_addr, net->dev_addr))
1142 dev_warn(&g->dev,
1143 "using random %s ethernet address\n", "self");
1144 if (get_ether_addr(host_addr, dev->host_mac))
1145 dev_warn(&g->dev,
1146 "using random %s ethernet address\n", "host");
1147
1148 if (ethaddr)
1149 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
1150
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001151 net->netdev_ops = &eth_netdev_ops;
1152
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001153 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -07001154
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001155 /* set operation mode to eth by default */
1156 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1157
David Brownell2b3d9422008-06-19 18:19:28 -07001158 dev->gadget = g;
1159 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -08001160 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -07001161
1162 status = register_netdev(net);
1163 if (status < 0) {
1164 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1165 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001166 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -07001167 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001168 INFO(dev, "MAC %pM\n", net->dev_addr);
1169 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001170
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001171 /*
1172 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -07001173 * - iff DATA transfer is active, carrier is "on"
1174 * - tx queueing enabled if open *and* carrier is "on"
1175 */
1176 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -07001177 }
1178
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001179 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -07001180}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001181EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -07001182
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001183struct net_device *gether_setup_name_default(const char *netname)
1184{
1185 struct net_device *net;
1186 struct eth_dev *dev;
1187
1188 net = alloc_etherdev(sizeof(*dev));
1189 if (!net)
1190 return ERR_PTR(-ENOMEM);
1191
1192 dev = netdev_priv(net);
1193 spin_lock_init(&dev->lock);
1194 spin_lock_init(&dev->req_lock);
1195 INIT_WORK(&dev->work, eth_work);
Matthew Moeller5df32222016-03-09 20:19:25 -06001196 INIT_WORK(&dev->rx_work, process_rx_w);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001197 INIT_LIST_HEAD(&dev->tx_reqs);
1198 INIT_LIST_HEAD(&dev->rx_reqs);
1199
1200 skb_queue_head_init(&dev->rx_frames);
1201
1202 /* network device setup */
1203 dev->net = net;
1204 dev->qmult = QMULT_DEFAULT;
1205 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
1206
1207 eth_random_addr(dev->dev_mac);
1208 pr_warn("using random %s ethernet address\n", "self");
1209 eth_random_addr(dev->host_mac);
1210 pr_warn("using random %s ethernet address\n", "host");
1211
1212 net->netdev_ops = &eth_netdev_ops;
1213
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001214 net->ethtool_ops = &ops;
Hemant Kumarfc49dbd2018-01-23 12:08:47 -08001215
1216 /* set operation mode to eth by default */
1217 set_bit(RMNET_MODE_LLP_ETH, &dev->flags);
1218
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001219 SET_NETDEV_DEVTYPE(net, &gadget_type);
1220
1221 return net;
1222}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001223EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001224
1225int gether_register_netdev(struct net_device *net)
1226{
1227 struct eth_dev *dev;
1228 struct usb_gadget *g;
1229 struct sockaddr sa;
1230 int status;
1231
1232 if (!net->dev.parent)
1233 return -EINVAL;
1234 dev = netdev_priv(net);
1235 g = dev->gadget;
1236 status = register_netdev(net);
1237 if (status < 0) {
1238 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1239 return status;
1240 } else {
1241 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
1242
1243 /* two kinds of host-initiated state changes:
1244 * - iff DATA transfer is active, carrier is "on"
1245 * - tx queueing enabled if open *and* carrier is "on"
1246 */
1247 netif_carrier_off(net);
1248 }
1249 sa.sa_family = net->type;
1250 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
1251 rtnl_lock();
1252 status = dev_set_mac_address(net, &sa);
1253 rtnl_unlock();
1254 if (status)
1255 pr_warn("cannot set self ethernet address: %d\n", status);
1256 else
1257 INFO(dev, "MAC %pM\n", dev->dev_mac);
1258
1259 return status;
1260}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001261EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001262
1263void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
1264{
1265 struct eth_dev *dev;
1266
1267 dev = netdev_priv(net);
1268 dev->gadget = g;
1269 SET_NETDEV_DEV(net, &g->dev);
1270}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001271EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001272
1273int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
1274{
1275 struct eth_dev *dev;
1276 u8 new_addr[ETH_ALEN];
1277
1278 dev = netdev_priv(net);
1279 if (get_ether_addr(dev_addr, new_addr))
1280 return -EINVAL;
1281 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
1282 return 0;
1283}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001284EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001285
1286int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
1287{
1288 struct eth_dev *dev;
1289
1290 dev = netdev_priv(net);
1291 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
1292}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001293EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001294
1295int gether_set_host_addr(struct net_device *net, const char *host_addr)
1296{
1297 struct eth_dev *dev;
1298 u8 new_addr[ETH_ALEN];
1299
1300 dev = netdev_priv(net);
1301 if (get_ether_addr(host_addr, new_addr))
1302 return -EINVAL;
1303 memcpy(dev->host_mac, new_addr, ETH_ALEN);
1304 return 0;
1305}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001306EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001307
1308int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
1309{
1310 struct eth_dev *dev;
1311
1312 dev = netdev_priv(net);
1313 return get_ether_addr_str(dev->host_mac, host_addr, len);
1314}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001315EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001316
1317int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
1318{
1319 struct eth_dev *dev;
1320
1321 if (len < 13)
1322 return -EINVAL;
1323
1324 dev = netdev_priv(net);
1325 snprintf(host_addr, len, "%pm", dev->host_mac);
1326
1327 return strlen(host_addr);
1328}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001329EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001330
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001331void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
1332{
1333 struct eth_dev *dev;
1334
1335 dev = netdev_priv(net);
1336 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1337}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001338EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001339
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001340void gether_set_qmult(struct net_device *net, unsigned qmult)
1341{
1342 struct eth_dev *dev;
1343
1344 dev = netdev_priv(net);
1345 dev->qmult = qmult;
1346}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001347EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001348
1349unsigned gether_get_qmult(struct net_device *net)
1350{
1351 struct eth_dev *dev;
1352
1353 dev = netdev_priv(net);
1354 return dev->qmult;
1355}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001356EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001357
1358int gether_get_ifname(struct net_device *net, char *name, int len)
1359{
1360 rtnl_lock();
1361 strlcpy(name, netdev_name(net), len);
1362 rtnl_unlock();
1363 return strlen(name);
1364}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001365EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001366
David Brownell2b3d9422008-06-19 18:19:28 -07001367/**
1368 * gether_cleanup - remove Ethernet-over-USB device
1369 * Context: may sleep
1370 *
1371 * This is called to free all resources allocated by @gether_setup().
1372 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001373void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001374{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001375 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001376 return;
1377
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001378 unregister_netdev(dev->net);
1379 flush_work(&dev->work);
1380 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001381}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001382EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001383
David Brownell2b3d9422008-06-19 18:19:28 -07001384/**
1385 * gether_connect - notify network layer that USB link is active
1386 * @link: the USB link, set up with endpoints, descriptors matching
1387 * current device speed, and any framing wrapper(s) set up.
1388 * Context: irqs blocked
1389 *
1390 * This is called to activate endpoints and let the network layer know
1391 * the connection is active ("carrier detect"). It may cause the I/O
1392 * queues to open and start letting network packets flow, but will in
1393 * any case activate the endpoints so that they respond properly to the
1394 * USB host.
1395 *
1396 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1397 * indicate some error code (negative errno), ep->driver_data values
1398 * have been overwritten.
1399 */
1400struct net_device *gether_connect(struct gether *link)
1401{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001402 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001403 int result = 0;
1404
1405 if (!dev)
1406 return ERR_PTR(-EINVAL);
1407
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001408 if (link->in_ep) {
1409 link->in_ep->driver_data = dev;
1410 result = usb_ep_enable(link->in_ep);
1411 if (result != 0) {
1412 DBG(dev, "enable %s --> %d\n",
1413 link->in_ep->name, result);
1414 goto fail0;
1415 }
David Brownell2b3d9422008-06-19 18:19:28 -07001416 }
1417
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001418 if (link->out_ep) {
1419 link->out_ep->driver_data = dev;
1420 result = usb_ep_enable(link->out_ep);
1421 if (result != 0) {
1422 DBG(dev, "enable %s --> %d\n",
1423 link->out_ep->name, result);
1424 goto fail1;
1425 }
David Brownell2b3d9422008-06-19 18:19:28 -07001426 }
1427
1428 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001429 result = alloc_requests(dev, link, qlen(dev->gadget,
1430 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001431
1432 if (result == 0) {
1433 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001434 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001435 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001436
1437 dev->header_len = link->header_len;
1438 dev->unwrap = link->unwrap;
1439 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001440 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +08001441 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001442
1443 spin_lock(&dev->lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -07001444 dev->tx_skb_hold_count = 0;
1445 dev->no_tx_req_used = 0;
1446 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001447 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001448 if (netif_running(dev->net)) {
1449 if (link->open)
1450 link->open(link);
1451 } else {
1452 if (link->close)
1453 link->close(link);
1454 }
David Brownell2b3d9422008-06-19 18:19:28 -07001455 spin_unlock(&dev->lock);
1456
1457 netif_carrier_on(dev->net);
1458 if (netif_running(dev->net))
1459 eth_start(dev, GFP_ATOMIC);
1460
1461 /* on error, disable any endpoints */
1462 } else {
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001463 if (link->out_ep)
1464 (void) usb_ep_disable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001465fail1:
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001466 if (link->in_ep)
1467 (void) usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001468 }
1469fail0:
1470 /* caller is responsible for cleanup on error */
1471 if (result < 0)
1472 return ERR_PTR(result);
1473 return dev->net;
1474}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001475EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001476
1477/**
1478 * gether_disconnect - notify network layer that USB link is inactive
1479 * @link: the USB link, on which gether_connect() was called
1480 * Context: irqs blocked
1481 *
1482 * This is called to deactivate endpoints and let the network layer know
1483 * the connection went inactive ("no carrier").
1484 *
1485 * On return, the state is as if gether_connect() had never been called.
1486 * The endpoints are inactive, and accordingly without active USB I/O.
1487 * Pointers to endpoint descriptors and endpoint private data are nulled.
1488 */
1489void gether_disconnect(struct gether *link)
1490{
1491 struct eth_dev *dev = link->ioport;
1492 struct usb_request *req;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001493 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001494
1495 WARN_ON(!dev);
1496 if (!dev)
1497 return;
1498
1499 DBG(dev, "%s\n", __func__);
1500
1501 netif_stop_queue(dev->net);
1502 netif_carrier_off(dev->net);
1503
1504 /* disable endpoints, forcing (synchronous) completion
1505 * of all pending i/o. then free the request objects
1506 * and forget about the endpoints.
1507 */
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001508 if (link->in_ep) {
1509 usb_ep_disable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001510 spin_lock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001511 while (!list_empty(&dev->tx_reqs)) {
1512 req = container_of(dev->tx_reqs.next,
1513 struct usb_request, list);
1514 list_del(&req->list);
David Brownell2b3d9422008-06-19 18:19:28 -07001515
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001516 spin_unlock(&dev->req_lock);
1517 if (link->multi_pkt_xfer)
1518 kfree(req->buf);
1519 usb_ep_free_request(link->in_ep, req);
1520 spin_lock(&dev->req_lock);
1521 }
David Brownell2b3d9422008-06-19 18:19:28 -07001522 spin_unlock(&dev->req_lock);
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001523 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001524 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001525
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001526 if (link->out_ep) {
1527 usb_ep_disable(link->out_ep);
1528 spin_lock(&dev->req_lock);
1529 while (!list_empty(&dev->rx_reqs)) {
1530 req = container_of(dev->rx_reqs.next,
1531 struct usb_request, list);
1532 list_del(&req->list);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001533
Hemant Kumar8a7c8122018-01-05 11:47:46 -08001534 spin_unlock(&dev->req_lock);
1535 usb_ep_free_request(link->out_ep, req);
1536 spin_lock(&dev->req_lock);
1537 }
1538 spin_unlock(&dev->req_lock);
1539
1540 spin_lock(&dev->rx_frames.lock);
1541 while ((skb = __skb_dequeue(&dev->rx_frames)))
1542 dev_kfree_skb_any(skb);
1543 spin_unlock(&dev->rx_frames.lock);
1544
1545 link->out_ep->desc = NULL;
1546 }
David Brownell2b3d9422008-06-19 18:19:28 -07001547
1548 /* finish forgetting about this USB link episode */
1549 dev->header_len = 0;
1550 dev->unwrap = NULL;
1551 dev->wrap = NULL;
1552
1553 spin_lock(&dev->lock);
1554 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001555 spin_unlock(&dev->lock);
1556}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001557EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001558
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001559static int __init gether_init(void)
1560{
1561 uether_wq = create_singlethread_workqueue("uether");
1562 if (!uether_wq) {
1563 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1564 return -ENOMEM;
1565 }
1566 return 0;
1567}
1568module_init(gether_init);
1569
1570static void __exit gether_exit(void)
1571{
1572 destroy_workqueue(uether_wq);
1573
1574}
1575module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001576MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001577MODULE_DESCRIPTION("ethernet over USB driver");
1578MODULE_LICENSE("GPL v2");