blob: 9b496ba718aa117dc7b3b3a81a0275ef160b6776 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
David Brownell2b3d9422008-06-19 18:19:28 -070018#include <linux/device.h>
19#include <linux/ctype.h>
20#include <linux/etherdevice.h>
21#include <linux/ethtool.h>
22
23#include "u_ether.h"
24
25
26/*
27 * This component encapsulates the Ethernet link glue needed to provide
28 * one (!) network link through the USB gadget stack, normally "usb0".
29 *
30 * The control and data models are handled by the function driver which
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050031 * connects to this code; such as CDC Ethernet (ECM or EEM),
32 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
33 * management.
David Brownell2b3d9422008-06-19 18:19:28 -070034 *
35 * Link level addressing is handled by this component using module
36 * parameters; if no such parameters are provided, random link level
37 * addresses are used. Each end of the link uses one address. The
38 * host end address is exported in various ways, and is often recorded
39 * in configuration databases.
40 *
41 * The driver which assembles each configuration using such a link is
42 * responsible for ensuring that each configuration includes at most one
43 * instance of is network link. (The network layer provides ways for
44 * this single "physical" link to be used by multiple virtual links.)
45 */
46
David Brownell8a1ce2c2008-08-18 17:43:56 -070047#define UETH__VERSION "29-May-2008"
David Brownell2b3d9422008-06-19 18:19:28 -070048
Hemant Kumar08eb78e2012-07-30 20:38:47 -070049static struct workqueue_struct *uether_wq;
50
David Brownell2b3d9422008-06-19 18:19:28 -070051struct eth_dev {
52 /* lock is held while accessing port_usb
53 * or updating its backlink port_usb->ioport
54 */
55 spinlock_t lock;
56 struct gether *port_usb;
57
58 struct net_device *net;
59 struct usb_gadget *gadget;
60
61 spinlock_t req_lock; /* guard {rx,tx}_reqs */
62 struct list_head tx_reqs, rx_reqs;
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +053063 unsigned tx_qlen;
Mayank Rana5b165cb2012-05-25 19:52:45 +053064/* Minimum number of TX USB request queued to UDC */
65#define TX_REQ_THRESHOLD 5
66 int no_tx_req_used;
67 int tx_skb_hold_count;
68 u32 tx_req_bufsize;
David Brownell2b3d9422008-06-19 18:19:28 -070069
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050070 struct sk_buff_head rx_frames;
71
David Brownell2b3d9422008-06-19 18:19:28 -070072 unsigned header_len;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050073 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
74 int (*unwrap)(struct gether *,
75 struct sk_buff *skb,
76 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070077
78 struct work_struct work;
Hemant Kumar08eb78e2012-07-30 20:38:47 -070079 struct work_struct rx_work;
David Brownell2b3d9422008-06-19 18:19:28 -070080
81 unsigned long todo;
82#define WORK_RX_MEMORY 0
83
84 bool zlp;
85 u8 host_mac[ETH_ALEN];
86};
87
88/*-------------------------------------------------------------------------*/
89
90#define RX_EXTRA 20 /* bytes guarding against rx overflows */
91
92#define DEFAULT_QLEN 2 /* double buffering by default */
93
David Brownell2b3d9422008-06-19 18:19:28 -070094#ifdef CONFIG_USB_GADGET_DUALSPEED
95
Mayank Rana5b165cb2012-05-25 19:52:45 +053096static unsigned qmult = 10;
David Brownell2b3d9422008-06-19 18:19:28 -070097module_param(qmult, uint, S_IRUGO|S_IWUSR);
Paul Zimmerman04617db2011-06-27 14:13:18 -070098MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");
David Brownell2b3d9422008-06-19 18:19:28 -070099
100#else /* full speed (low speed doesn't do bulk) */
101#define qmult 1
102#endif
103
Paul Zimmerman04617db2011-06-27 14:13:18 -0700104/* for dual-speed hardware, use deeper queues at high/super speed */
David Brownell2b3d9422008-06-19 18:19:28 -0700105static inline int qlen(struct usb_gadget *gadget)
106{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700107 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
108 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700109 return qmult * DEFAULT_QLEN;
110 else
111 return DEFAULT_QLEN;
112}
113
114/*-------------------------------------------------------------------------*/
115
116/* REVISIT there must be a better way than having two sets
117 * of debug calls ...
118 */
119
120#undef DBG
121#undef VDBG
122#undef ERROR
Anh Nguyen64376432012-10-17 16:08:48 -0700123#undef DEBUG
David Brownell2b3d9422008-06-19 18:19:28 -0700124#undef INFO
125
126#define xprintk(d, level, fmt, args...) \
127 printk(level "%s: " fmt , (d)->net->name , ## args)
128
129#ifdef DEBUG
130#undef DEBUG
131#define DBG(dev, fmt, args...) \
132 xprintk(dev , KERN_DEBUG , fmt , ## args)
133#else
134#define DBG(dev, fmt, args...) \
135 do { } while (0)
136#endif /* DEBUG */
137
138#ifdef VERBOSE_DEBUG
139#define VDBG DBG
140#else
141#define VDBG(dev, fmt, args...) \
142 do { } while (0)
143#endif /* DEBUG */
144
145#define ERROR(dev, fmt, args...) \
146 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700147#define INFO(dev, fmt, args...) \
148 xprintk(dev , KERN_INFO , fmt , ## args)
149
150/*-------------------------------------------------------------------------*/
151
152/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
153
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800154static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700155{
156 struct eth_dev *dev = netdev_priv(net);
157 unsigned long flags;
158 int status = 0;
159
160 /* don't change MTU on "live" link (peer won't know) */
161 spin_lock_irqsave(&dev->lock, flags);
162 if (dev->port_usb)
163 status = -EBUSY;
164 else if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
165 status = -ERANGE;
166 else
167 net->mtu = new_mtu;
168 spin_unlock_irqrestore(&dev->lock, flags);
169
170 return status;
171}
172
173static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
174{
175 struct eth_dev *dev = netdev_priv(net);
176
177 strlcpy(p->driver, "g_ether", sizeof p->driver);
David Brownell8a1ce2c2008-08-18 17:43:56 -0700178 strlcpy(p->version, UETH__VERSION, sizeof p->version);
David Brownell2b3d9422008-06-19 18:19:28 -0700179 strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
180 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof p->bus_info);
181}
182
David Brownell2b3d9422008-06-19 18:19:28 -0700183/* REVISIT can also support:
184 * - WOL (by tracking suspends and issuing remote wakeup)
185 * - msglevel (implies updated messaging)
186 * - ... probably more ethtool ops
187 */
188
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700189static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700190 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700191 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700192};
193
194static void defer_kevent(struct eth_dev *dev, int flag)
195{
196 if (test_and_set_bit(flag, &dev->todo))
197 return;
198 if (!schedule_work(&dev->work))
199 ERROR(dev, "kevent %d may have been dropped\n", flag);
200 else
201 DBG(dev, "kevent %d scheduled\n", flag);
202}
203
204static void rx_complete(struct usb_ep *ep, struct usb_request *req);
205
206static int
207rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
208{
209 struct sk_buff *skb;
210 int retval = -ENOMEM;
211 size_t size = 0;
212 struct usb_ep *out;
213 unsigned long flags;
214
215 spin_lock_irqsave(&dev->lock, flags);
216 if (dev->port_usb)
217 out = dev->port_usb->out_ep;
218 else
219 out = NULL;
220 spin_unlock_irqrestore(&dev->lock, flags);
221
222 if (!out)
223 return -ENOTCONN;
224
225
226 /* Padding up to RX_EXTRA handles minor disagreements with host.
227 * Normally we use the USB "terminate on short read" convention;
228 * so allow up to (N*maxpacket), since that memory is normally
229 * already allocated. Some hardware doesn't deal well with short
230 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
231 * byte off the end (to force hardware errors on overflow).
232 *
233 * RNDIS uses internal framing, and explicitly allows senders to
234 * pad to end-of-packet. That's potentially nice for speed, but
235 * means receivers can't recover lost synch on their own (because
236 * new packets don't only start after a short RX).
237 */
238 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
239 size += dev->port_usb->header_len;
240 size += out->maxpacket - 1;
241 size -= size % out->maxpacket;
242
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200243 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800244 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200245
David Brownell2b3d9422008-06-19 18:19:28 -0700246 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
247 if (skb == NULL) {
248 DBG(dev, "no rx skb\n");
249 goto enomem;
250 }
251
252 /* Some platforms perform better when IP packets are aligned,
253 * but on at least one, checksumming fails otherwise. Note:
254 * RNDIS headers involve variable numbers of LE32 values.
255 */
256 skb_reserve(skb, NET_IP_ALIGN);
257
258 req->buf = skb->data;
259 req->length = size;
260 req->complete = rx_complete;
261 req->context = skb;
262
263 retval = usb_ep_queue(out, req, gfp_flags);
264 if (retval == -ENOMEM)
265enomem:
266 defer_kevent(dev, WORK_RX_MEMORY);
267 if (retval) {
268 DBG(dev, "rx submit --> %d\n", retval);
269 if (skb)
270 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700271 }
272 return retval;
273}
274
275static void rx_complete(struct usb_ep *ep, struct usb_request *req)
276{
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700277 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700278 struct eth_dev *dev = ep->driver_data;
279 int status = req->status;
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700280 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700281
282 switch (status) {
283
284 /* normal completion */
285 case 0:
286 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500287
288 if (dev->unwrap) {
289 unsigned long flags;
290
291 spin_lock_irqsave(&dev->lock, flags);
292 if (dev->port_usb) {
293 status = dev->unwrap(dev->port_usb,
294 skb,
295 &dev->rx_frames);
296 } else {
297 dev_kfree_skb_any(skb);
298 status = -ENOTCONN;
299 }
300 spin_unlock_irqrestore(&dev->lock, flags);
301 } else {
302 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700303 }
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500304
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700305 if (!status)
306 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700307 break;
308
309 /* software-driven interface shutdown */
310 case -ECONNRESET: /* unlink */
311 case -ESHUTDOWN: /* disconnect etc */
312 VDBG(dev, "rx shutdown, code %d\n", status);
313 goto quiesce;
314
315 /* for hardware automagic (such as pxa) */
316 case -ECONNABORTED: /* endpoint reset */
317 DBG(dev, "rx %s reset\n", ep->name);
318 defer_kevent(dev, WORK_RX_MEMORY);
319quiesce:
320 dev_kfree_skb_any(skb);
321 goto clean;
322
323 /* data overrun */
324 case -EOVERFLOW:
325 dev->net->stats.rx_over_errors++;
326 /* FALLTHROUGH */
327
328 default:
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700329 queue = 1;
330 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700331 dev->net->stats.rx_errors++;
332 DBG(dev, "rx status %d\n", status);
333 break;
334 }
335
David Brownell2b3d9422008-06-19 18:19:28 -0700336clean:
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700337 spin_lock(&dev->req_lock);
338 list_add(&req->list, &dev->rx_reqs);
339 spin_unlock(&dev->req_lock);
340
341 if (queue)
342 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700343}
344
345static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
346{
347 unsigned i;
348 struct usb_request *req;
349
350 if (!n)
351 return -ENOMEM;
352
353 /* queue/recycle up to N requests */
354 i = n;
355 list_for_each_entry(req, list, list) {
356 if (i-- == 0)
357 goto extra;
358 }
359 while (i--) {
360 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
361 if (!req)
362 return list_empty(list) ? -ENOMEM : 0;
363 list_add(&req->list, list);
364 }
365 return 0;
366
367extra:
368 /* free extras */
369 for (;;) {
370 struct list_head *next;
371
372 next = req->list.next;
373 list_del(&req->list);
374 usb_ep_free_request(ep, req);
375
376 if (next == list)
377 break;
378
379 req = container_of(next, struct usb_request, list);
380 }
381 return 0;
382}
383
384static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
385{
386 int status;
387
388 spin_lock(&dev->req_lock);
389 status = prealloc(&dev->tx_reqs, link->in_ep, n);
390 if (status < 0)
391 goto fail;
392 status = prealloc(&dev->rx_reqs, link->out_ep, n);
393 if (status < 0)
394 goto fail;
395 goto done;
396fail:
397 DBG(dev, "can't alloc requests\n");
398done:
399 spin_unlock(&dev->req_lock);
400 return status;
401}
402
403static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
404{
405 struct usb_request *req;
406 unsigned long flags;
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700407 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700408
409 /* fill unused rxq slots with some skb */
410 spin_lock_irqsave(&dev->req_lock, flags);
411 while (!list_empty(&dev->rx_reqs)) {
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700412 /* break the nexus of continuous completion and re-submission*/
413 if (++req_cnt > qlen(dev->gadget))
414 break;
415
David Brownell2b3d9422008-06-19 18:19:28 -0700416 req = container_of(dev->rx_reqs.next,
417 struct usb_request, list);
418 list_del_init(&req->list);
419 spin_unlock_irqrestore(&dev->req_lock, flags);
420
421 if (rx_submit(dev, req, gfp_flags) < 0) {
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700422 spin_lock_irqsave(&dev->req_lock, flags);
423 list_add(&req->list, &dev->rx_reqs);
424 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700425 defer_kevent(dev, WORK_RX_MEMORY);
426 return;
427 }
428
429 spin_lock_irqsave(&dev->req_lock, flags);
430 }
431 spin_unlock_irqrestore(&dev->req_lock, flags);
432}
433
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700434static void process_rx_w(struct work_struct *work)
435{
436 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
437 struct sk_buff *skb;
438 int status = 0;
439
440 if (!dev->port_usb)
441 return;
442
443 while ((skb = skb_dequeue(&dev->rx_frames))) {
444 if (status < 0
445 || ETH_HLEN > skb->len
446 || skb->len > ETH_FRAME_LEN) {
447 dev->net->stats.rx_errors++;
448 dev->net->stats.rx_length_errors++;
449 DBG(dev, "rx length %d\n", skb->len);
450 dev_kfree_skb_any(skb);
451 continue;
452 }
453 skb->protocol = eth_type_trans(skb, dev->net);
454 dev->net->stats.rx_packets++;
455 dev->net->stats.rx_bytes += skb->len;
456
457 status = netif_rx_ni(skb);
458 }
459
460 if (netif_running(dev->net))
461 rx_fill(dev, GFP_KERNEL);
462}
463
David Brownell2b3d9422008-06-19 18:19:28 -0700464static void eth_work(struct work_struct *work)
465{
466 struct eth_dev *dev = container_of(work, struct eth_dev, work);
467
468 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
469 if (netif_running(dev->net))
470 rx_fill(dev, GFP_KERNEL);
471 }
472
473 if (dev->todo)
474 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
475}
476
477static void tx_complete(struct usb_ep *ep, struct usb_request *req)
478{
479 struct sk_buff *skb = req->context;
480 struct eth_dev *dev = ep->driver_data;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530481 struct net_device *net = dev->net;
482 struct usb_request *new_req;
483 struct usb_ep *in;
484 int length;
485 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700486
487 switch (req->status) {
488 default:
489 dev->net->stats.tx_errors++;
490 VDBG(dev, "tx err %d\n", req->status);
491 /* FALLTHROUGH */
492 case -ECONNRESET: /* unlink */
493 case -ESHUTDOWN: /* disconnect etc */
494 break;
495 case 0:
Mayank Rana5b165cb2012-05-25 19:52:45 +0530496 if (!req->zero)
497 dev->net->stats.tx_bytes += req->length-1;
498 else
499 dev->net->stats.tx_bytes += req->length;
David Brownell2b3d9422008-06-19 18:19:28 -0700500 }
501 dev->net->stats.tx_packets++;
502
503 spin_lock(&dev->req_lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530504 list_add_tail(&req->list, &dev->tx_reqs);
505
506 if (dev->port_usb->multi_pkt_xfer) {
507 dev->no_tx_req_used--;
508 req->length = 0;
509 in = dev->port_usb->in_ep;
510
511 if (!list_empty(&dev->tx_reqs)) {
512 new_req = container_of(dev->tx_reqs.next,
513 struct usb_request, list);
514 list_del(&new_req->list);
515 spin_unlock(&dev->req_lock);
516 if (new_req->length > 0) {
517 length = new_req->length;
518
519 /* NCM requires no zlp if transfer is
520 * dwNtbInMaxSize */
521 if (dev->port_usb->is_fixed &&
522 length == dev->port_usb->fixed_in_len &&
523 (length % in->maxpacket) == 0)
524 new_req->zero = 0;
525 else
526 new_req->zero = 1;
527
528 /* use zlp framing on tx for strict CDC-Ether
529 * conformance, though any robust network rx
530 * path ignores extra padding. and some hardware
531 * doesn't like to write zlps.
532 */
533 if (new_req->zero && !dev->zlp &&
534 (length % in->maxpacket) == 0) {
535 new_req->zero = 0;
536 length++;
537 }
538
539 new_req->length = length;
540 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
541 switch (retval) {
542 default:
543 DBG(dev, "tx queue err %d\n", retval);
544 break;
545 case 0:
546 spin_lock(&dev->req_lock);
547 dev->no_tx_req_used++;
548 spin_unlock(&dev->req_lock);
549 net->trans_start = jiffies;
550 }
551 } else {
552 spin_lock(&dev->req_lock);
553 list_add(&new_req->list, &dev->tx_reqs);
554 spin_unlock(&dev->req_lock);
555 }
556 } else {
557 spin_unlock(&dev->req_lock);
558 }
559 } else {
560 spin_unlock(&dev->req_lock);
561 dev_kfree_skb_any(skb);
562 }
David Brownell2b3d9422008-06-19 18:19:28 -0700563
David Brownell2b3d9422008-06-19 18:19:28 -0700564 if (netif_carrier_ok(dev->net))
565 netif_wake_queue(dev->net);
566}
567
568static inline int is_promisc(u16 cdc_filter)
569{
570 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
571}
572
Mayank Rana5b165cb2012-05-25 19:52:45 +0530573static void alloc_tx_buffer(struct eth_dev *dev)
574{
575 struct list_head *act;
576 struct usb_request *req;
577
578 dev->tx_req_bufsize = (TX_SKB_HOLD_THRESHOLD *
579 (dev->net->mtu
580 + sizeof(struct ethhdr)
581 /* size of rndis_packet_msg_type */
582 + 44
583 + 22));
584
585 list_for_each(act, &dev->tx_reqs) {
586 req = container_of(act, struct usb_request, list);
587 if (!req->buf)
588 req->buf = kmalloc(dev->tx_req_bufsize,
589 GFP_ATOMIC);
590 }
591}
592
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000593static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
594 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700595{
596 struct eth_dev *dev = netdev_priv(net);
597 int length = skb->len;
598 int retval;
599 struct usb_request *req = NULL;
600 unsigned long flags;
601 struct usb_ep *in;
602 u16 cdc_filter;
603
604 spin_lock_irqsave(&dev->lock, flags);
605 if (dev->port_usb) {
606 in = dev->port_usb->in_ep;
607 cdc_filter = dev->port_usb->cdc_filter;
608 } else {
609 in = NULL;
610 cdc_filter = 0;
611 }
612 spin_unlock_irqrestore(&dev->lock, flags);
613
614 if (!in) {
615 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000616 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700617 }
618
Mayank Rana5b165cb2012-05-25 19:52:45 +0530619 /* Allocate memory for tx_reqs to support multi packet transfer */
620 if (dev->port_usb->multi_pkt_xfer && !dev->tx_req_bufsize)
621 alloc_tx_buffer(dev);
622
David Brownell2b3d9422008-06-19 18:19:28 -0700623 /* apply outgoing CDC or RNDIS filters */
624 if (!is_promisc(cdc_filter)) {
625 u8 *dest = skb->data;
626
627 if (is_multicast_ether_addr(dest)) {
628 u16 type;
629
630 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
631 * SET_ETHERNET_MULTICAST_FILTERS requests
632 */
633 if (is_broadcast_ether_addr(dest))
634 type = USB_CDC_PACKET_TYPE_BROADCAST;
635 else
636 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
637 if (!(cdc_filter & type)) {
638 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000639 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700640 }
641 }
642 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
643 }
644
645 spin_lock_irqsave(&dev->req_lock, flags);
646 /*
647 * this freelist can be empty if an interrupt triggered disconnect()
648 * and reconfigured the gadget (shutting down this queue) after the
649 * network stack decided to xmit but before we got the spinlock.
650 */
651 if (list_empty(&dev->tx_reqs)) {
652 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000653 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700654 }
655
656 req = container_of(dev->tx_reqs.next, struct usb_request, list);
657 list_del(&req->list);
658
659 /* temporarily stop TX queue when the freelist empties */
660 if (list_empty(&dev->tx_reqs))
661 netif_stop_queue(net);
662 spin_unlock_irqrestore(&dev->req_lock, flags);
663
664 /* no buffer copies needed, unless the network stack did it
665 * or the hardware can't use skb buffers.
666 * or there's not enough space for extra headers we need
667 */
668 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500669 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700670
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500671 spin_lock_irqsave(&dev->lock, flags);
672 if (dev->port_usb)
673 skb = dev->wrap(dev->port_usb, skb);
674 spin_unlock_irqrestore(&dev->lock, flags);
675 if (!skb)
David Brownell2b3d9422008-06-19 18:19:28 -0700676 goto drop;
David Brownell2b3d9422008-06-19 18:19:28 -0700677 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530678
679 spin_lock_irqsave(&dev->req_lock, flags);
680 dev->tx_skb_hold_count++;
681 spin_unlock_irqrestore(&dev->req_lock, flags);
682
683 if (dev->port_usb->multi_pkt_xfer) {
684 memcpy(req->buf + req->length, skb->data, skb->len);
685 req->length = req->length + skb->len;
686 length = req->length;
687 dev_kfree_skb_any(skb);
688
689 spin_lock_irqsave(&dev->req_lock, flags);
690 if (dev->tx_skb_hold_count < TX_SKB_HOLD_THRESHOLD) {
691 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
692 list_add(&req->list, &dev->tx_reqs);
693 spin_unlock_irqrestore(&dev->req_lock, flags);
694 goto success;
695 }
696 }
697
698 dev->no_tx_req_used++;
699 spin_unlock_irqrestore(&dev->req_lock, flags);
700
701 spin_lock_irqsave(&dev->lock, flags);
702 dev->tx_skb_hold_count = 0;
703 spin_unlock_irqrestore(&dev->lock, flags);
704 } else {
705 length = skb->len;
706 req->buf = skb->data;
707 req->context = skb;
708 }
709
David Brownell2b3d9422008-06-19 18:19:28 -0700710 req->complete = tx_complete;
711
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200712 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
713 if (dev->port_usb->is_fixed &&
714 length == dev->port_usb->fixed_in_len &&
715 (length % in->maxpacket) == 0)
716 req->zero = 0;
717 else
718 req->zero = 1;
719
David Brownell2b3d9422008-06-19 18:19:28 -0700720 /* use zlp framing on tx for strict CDC-Ether conformance,
721 * though any robust network rx path ignores extra padding.
722 * and some hardware doesn't like to write zlps.
723 */
Mayank Rana5b165cb2012-05-25 19:52:45 +0530724 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
725 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700726 length++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530727 }
David Brownell2b3d9422008-06-19 18:19:28 -0700728
729 req->length = length;
730
731 /* throttle highspeed IRQ rate back slightly */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530732 if (gadget_is_dualspeed(dev->gadget) &&
733 (dev->gadget->speed == USB_SPEED_HIGH)) {
734 dev->tx_qlen++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530735 if (dev->tx_qlen == (qmult/2)) {
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530736 req->no_interrupt = 0;
737 dev->tx_qlen = 0;
738 } else {
739 req->no_interrupt = 1;
740 }
741 } else {
742 req->no_interrupt = 0;
743 }
David Brownell2b3d9422008-06-19 18:19:28 -0700744
745 retval = usb_ep_queue(in, req, GFP_ATOMIC);
746 switch (retval) {
747 default:
748 DBG(dev, "tx queue err %d\n", retval);
749 break;
750 case 0:
751 net->trans_start = jiffies;
David Brownell2b3d9422008-06-19 18:19:28 -0700752 }
753
754 if (retval) {
Mayank Rana5b165cb2012-05-25 19:52:45 +0530755 if (!dev->port_usb->multi_pkt_xfer)
756 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700757drop:
758 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700759 spin_lock_irqsave(&dev->req_lock, flags);
760 if (list_empty(&dev->tx_reqs))
761 netif_start_queue(net);
762 list_add(&req->list, &dev->tx_reqs);
763 spin_unlock_irqrestore(&dev->req_lock, flags);
764 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530765success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000766 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700767}
768
769/*-------------------------------------------------------------------------*/
770
771static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
772{
773 DBG(dev, "%s\n", __func__);
774
775 /* fill the rx queue */
776 rx_fill(dev, gfp_flags);
777
778 /* and open the tx floodgates */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530779 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700780 netif_wake_queue(dev->net);
781}
782
783static int eth_open(struct net_device *net)
784{
785 struct eth_dev *dev = netdev_priv(net);
786 struct gether *link;
787
788 DBG(dev, "%s\n", __func__);
789 if (netif_carrier_ok(dev->net))
790 eth_start(dev, GFP_KERNEL);
791
792 spin_lock_irq(&dev->lock);
793 link = dev->port_usb;
794 if (link && link->open)
795 link->open(link);
796 spin_unlock_irq(&dev->lock);
797
798 return 0;
799}
800
801static int eth_stop(struct net_device *net)
802{
803 struct eth_dev *dev = netdev_priv(net);
804 unsigned long flags;
805
806 VDBG(dev, "%s\n", __func__);
807 netif_stop_queue(net);
808
809 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
810 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
811 dev->net->stats.rx_errors, dev->net->stats.tx_errors
812 );
813
814 /* ensure there are no more active requests */
815 spin_lock_irqsave(&dev->lock, flags);
816 if (dev->port_usb) {
817 struct gether *link = dev->port_usb;
818
819 if (link->close)
820 link->close(link);
821
822 /* NOTE: we have no abort-queue primitive we could use
823 * to cancel all pending I/O. Instead, we disable then
824 * reenable the endpoints ... this idiom may leave toggle
825 * wrong, but that's a self-correcting error.
826 *
827 * REVISIT: we *COULD* just let the transfers complete at
828 * their own pace; the network stack can handle old packets.
829 * For the moment we leave this here, since it works.
830 */
831 usb_ep_disable(link->in_ep);
832 usb_ep_disable(link->out_ep);
833 if (netif_carrier_ok(net)) {
Rajkumar Raghupathye7c42322012-06-13 19:31:15 +0530834 if (config_ep_by_speed(dev->gadget, &link->func,
835 link->in_ep) ||
836 config_ep_by_speed(dev->gadget, &link->func,
837 link->out_ep)) {
838 link->in_ep->desc = NULL;
839 link->out_ep->desc = NULL;
840 return -EINVAL;
841 }
David Brownell2b3d9422008-06-19 18:19:28 -0700842 DBG(dev, "host still using in/out endpoints\n");
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300843 usb_ep_enable(link->in_ep);
844 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700845 }
846 }
847 spin_unlock_irqrestore(&dev->lock, flags);
848
849 return 0;
850}
851
852/*-------------------------------------------------------------------------*/
853
taeju.parkda2f5f22012-09-14 14:09:03 +0900854static u8 host_ethaddr[ETH_ALEN];
855
David Brownell2b3d9422008-06-19 18:19:28 -0700856/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
857static char *dev_addr;
858module_param(dev_addr, charp, S_IRUGO);
859MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
860
861/* this address is invisible to ifconfig */
862static char *host_addr;
863module_param(host_addr, charp, S_IRUGO);
864MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
865
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200866static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700867{
868 if (str) {
869 unsigned i;
870
871 for (i = 0; i < 6; i++) {
872 unsigned char num;
873
874 if ((*str == '.') || (*str == ':'))
875 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300876 num = hex_to_bin(*str++) << 4;
877 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700878 dev_addr [i] = num;
879 }
880 if (is_valid_ether_addr(dev_addr))
881 return 0;
882 }
883 random_ether_addr(dev_addr);
884 return 1;
885}
886
taeju.parkda2f5f22012-09-14 14:09:03 +0900887static int get_host_ether_addr(u8 *str, u8 *dev_addr)
888{
889 memcpy(dev_addr, str, ETH_ALEN);
890 if (is_valid_ether_addr(dev_addr))
891 return 0;
892
893 random_ether_addr(dev_addr);
894 memcpy(str, dev_addr, ETH_ALEN);
895 return 1;
896}
897
David Brownell2b3d9422008-06-19 18:19:28 -0700898static struct eth_dev *the_dev;
899
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800900static const struct net_device_ops eth_netdev_ops = {
901 .ndo_open = eth_open,
902 .ndo_stop = eth_stop,
903 .ndo_start_xmit = eth_start_xmit,
904 .ndo_change_mtu = ueth_change_mtu,
905 .ndo_set_mac_address = eth_mac_addr,
906 .ndo_validate_addr = eth_validate_addr,
907};
David Brownell2b3d9422008-06-19 18:19:28 -0700908
Marcel Holtmannaa790742010-01-15 22:13:58 -0800909static struct device_type gadget_type = {
910 .name = "gadget",
911};
912
David Brownell2b3d9422008-06-19 18:19:28 -0700913/**
914 * gether_setup - initialize one ethernet-over-usb link
915 * @g: gadget to associated with these links
916 * @ethaddr: NULL, or a buffer in which the ethernet address of the
917 * host side of the link is recorded
918 * Context: may sleep
919 *
920 * This sets up the single network link that may be exported by a
921 * gadget driver using this framework. The link layer addresses are
922 * set up using module parameters.
923 *
924 * Returns negative errno, or zero on success
925 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200926int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
David Brownell2b3d9422008-06-19 18:19:28 -0700927{
Mike Lockwood735d20e2011-08-12 14:35:42 -0700928 return gether_setup_name(g, ethaddr, "usb");
929}
930
931/**
932 * gether_setup_name - initialize one ethernet-over-usb link
933 * @g: gadget to associated with these links
934 * @ethaddr: NULL, or a buffer in which the ethernet address of the
935 * host side of the link is recorded
936 * @netname: name for network device (for example, "usb")
937 * Context: may sleep
938 *
939 * This sets up the single network link that may be exported by a
940 * gadget driver using this framework. The link layer addresses are
941 * set up using module parameters.
942 *
943 * Returns negative errno, or zero on success
944 */
945int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
946 const char *netname)
947{
David Brownell2b3d9422008-06-19 18:19:28 -0700948 struct eth_dev *dev;
949 struct net_device *net;
950 int status;
951
952 if (the_dev)
953 return -EBUSY;
954
955 net = alloc_etherdev(sizeof *dev);
956 if (!net)
957 return -ENOMEM;
958
959 dev = netdev_priv(net);
960 spin_lock_init(&dev->lock);
961 spin_lock_init(&dev->req_lock);
962 INIT_WORK(&dev->work, eth_work);
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700963 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700964 INIT_LIST_HEAD(&dev->tx_reqs);
965 INIT_LIST_HEAD(&dev->rx_reqs);
966
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500967 skb_queue_head_init(&dev->rx_frames);
968
David Brownell2b3d9422008-06-19 18:19:28 -0700969 /* network device setup */
970 dev->net = net;
Mike Lockwood735d20e2011-08-12 14:35:42 -0700971 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700972
973 if (get_ether_addr(dev_addr, net->dev_addr))
974 dev_warn(&g->dev,
975 "using random %s ethernet address\n", "self");
taeju.parkda2f5f22012-09-14 14:09:03 +0900976
977 if (get_host_ether_addr(host_ethaddr, dev->host_mac))
978 dev_warn(&g->dev, "using random %s ethernet address\n", "host");
979 else
980 dev_warn(&g->dev, "using previous %s ethernet address\n", "host");
David Brownell2b3d9422008-06-19 18:19:28 -0700981
982 if (ethaddr)
983 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
984
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800985 net->netdev_ops = &eth_netdev_ops;
986
David Brownell2b3d9422008-06-19 18:19:28 -0700987 SET_ETHTOOL_OPS(net, &ops);
988
David Brownell2b3d9422008-06-19 18:19:28 -0700989 dev->gadget = g;
990 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800991 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700992
993 status = register_netdev(net);
994 if (status < 0) {
995 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
996 free_netdev(net);
997 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700998 INFO(dev, "MAC %pM\n", net->dev_addr);
999 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001000
1001 the_dev = dev;
taeju.park5c17cb62012-09-22 14:32:26 +09001002
1003 /* two kinds of host-initiated state changes:
1004 * - iff DATA transfer is active, carrier is "on"
1005 * - tx queueing enabled if open *and* carrier is "on"
1006 */
1007 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -07001008 }
1009
1010 return status;
1011}
1012
1013/**
1014 * gether_cleanup - remove Ethernet-over-USB device
1015 * Context: may sleep
1016 *
1017 * This is called to free all resources allocated by @gether_setup().
1018 */
1019void gether_cleanup(void)
1020{
1021 if (!the_dev)
1022 return;
1023
1024 unregister_netdev(the_dev->net);
Tejun Heo569ff2d2010-12-24 16:14:20 +01001025 flush_work_sync(&the_dev->work);
David Brownell2b3d9422008-06-19 18:19:28 -07001026 free_netdev(the_dev->net);
1027
David Brownell2b3d9422008-06-19 18:19:28 -07001028 the_dev = NULL;
1029}
1030
1031
1032/**
1033 * gether_connect - notify network layer that USB link is active
1034 * @link: the USB link, set up with endpoints, descriptors matching
1035 * current device speed, and any framing wrapper(s) set up.
1036 * Context: irqs blocked
1037 *
1038 * This is called to activate endpoints and let the network layer know
1039 * the connection is active ("carrier detect"). It may cause the I/O
1040 * queues to open and start letting network packets flow, but will in
1041 * any case activate the endpoints so that they respond properly to the
1042 * USB host.
1043 *
1044 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1045 * indicate some error code (negative errno), ep->driver_data values
1046 * have been overwritten.
1047 */
1048struct net_device *gether_connect(struct gether *link)
1049{
1050 struct eth_dev *dev = the_dev;
1051 int result = 0;
1052
1053 if (!dev)
1054 return ERR_PTR(-EINVAL);
1055
1056 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001057 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001058 if (result != 0) {
1059 DBG(dev, "enable %s --> %d\n",
1060 link->in_ep->name, result);
1061 goto fail0;
1062 }
1063
1064 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001065 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001066 if (result != 0) {
1067 DBG(dev, "enable %s --> %d\n",
1068 link->out_ep->name, result);
1069 goto fail1;
1070 }
1071
1072 if (result == 0)
1073 result = alloc_requests(dev, link, qlen(dev->gadget));
1074
1075 if (result == 0) {
1076 dev->zlp = link->is_zlp_ok;
1077 DBG(dev, "qlen %d\n", qlen(dev->gadget));
1078
1079 dev->header_len = link->header_len;
1080 dev->unwrap = link->unwrap;
1081 dev->wrap = link->wrap;
1082
1083 spin_lock(&dev->lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301084 dev->tx_skb_hold_count = 0;
1085 dev->no_tx_req_used = 0;
1086 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001087 dev->port_usb = link;
1088 link->ioport = dev;
David Brownell29bac7b2008-09-06 21:33:49 -07001089 if (netif_running(dev->net)) {
1090 if (link->open)
1091 link->open(link);
1092 } else {
1093 if (link->close)
1094 link->close(link);
1095 }
David Brownell2b3d9422008-06-19 18:19:28 -07001096 spin_unlock(&dev->lock);
1097
1098 netif_carrier_on(dev->net);
1099 if (netif_running(dev->net))
1100 eth_start(dev, GFP_ATOMIC);
1101
1102 /* on error, disable any endpoints */
1103 } else {
1104 (void) usb_ep_disable(link->out_ep);
1105fail1:
1106 (void) usb_ep_disable(link->in_ep);
1107 }
1108fail0:
1109 /* caller is responsible for cleanup on error */
1110 if (result < 0)
1111 return ERR_PTR(result);
1112 return dev->net;
1113}
1114
1115/**
1116 * gether_disconnect - notify network layer that USB link is inactive
1117 * @link: the USB link, on which gether_connect() was called
1118 * Context: irqs blocked
1119 *
1120 * This is called to deactivate endpoints and let the network layer know
1121 * the connection went inactive ("no carrier").
1122 *
1123 * On return, the state is as if gether_connect() had never been called.
1124 * The endpoints are inactive, and accordingly without active USB I/O.
1125 * Pointers to endpoint descriptors and endpoint private data are nulled.
1126 */
1127void gether_disconnect(struct gether *link)
1128{
1129 struct eth_dev *dev = link->ioport;
1130 struct usb_request *req;
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001131 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001132
David Brownell2b3d9422008-06-19 18:19:28 -07001133 if (!dev)
1134 return;
1135
1136 DBG(dev, "%s\n", __func__);
1137
1138 netif_stop_queue(dev->net);
1139 netif_carrier_off(dev->net);
1140
1141 /* disable endpoints, forcing (synchronous) completion
1142 * of all pending i/o. then free the request objects
1143 * and forget about the endpoints.
1144 */
1145 usb_ep_disable(link->in_ep);
1146 spin_lock(&dev->req_lock);
1147 while (!list_empty(&dev->tx_reqs)) {
1148 req = container_of(dev->tx_reqs.next,
1149 struct usb_request, list);
1150 list_del(&req->list);
1151
1152 spin_unlock(&dev->req_lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301153 if (link->multi_pkt_xfer)
1154 kfree(req->buf);
David Brownell2b3d9422008-06-19 18:19:28 -07001155 usb_ep_free_request(link->in_ep, req);
1156 spin_lock(&dev->req_lock);
1157 }
1158 spin_unlock(&dev->req_lock);
1159 link->in_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001160 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001161
1162 usb_ep_disable(link->out_ep);
1163 spin_lock(&dev->req_lock);
1164 while (!list_empty(&dev->rx_reqs)) {
1165 req = container_of(dev->rx_reqs.next,
1166 struct usb_request, list);
1167 list_del(&req->list);
1168
1169 spin_unlock(&dev->req_lock);
1170 usb_ep_free_request(link->out_ep, req);
1171 spin_lock(&dev->req_lock);
1172 }
1173 spin_unlock(&dev->req_lock);
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001174
1175 spin_lock(&dev->rx_frames.lock);
1176 while ((skb = __skb_dequeue(&dev->rx_frames)))
1177 dev_kfree_skb_any(skb);
1178 spin_unlock(&dev->rx_frames.lock);
1179
David Brownell2b3d9422008-06-19 18:19:28 -07001180 link->out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001181 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001182
1183 /* finish forgetting about this USB link episode */
1184 dev->header_len = 0;
1185 dev->unwrap = NULL;
1186 dev->wrap = NULL;
1187
1188 spin_lock(&dev->lock);
1189 dev->port_usb = NULL;
1190 link->ioport = NULL;
1191 spin_unlock(&dev->lock);
1192}
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001193
1194static int __init gether_init(void)
1195{
1196 uether_wq = create_singlethread_workqueue("uether");
1197 if (!uether_wq) {
1198 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1199 return -ENOMEM;
1200 }
1201 return 0;
1202}
1203module_init(gether_init);
1204
1205static void __exit gether_exit(void)
1206{
1207 destroy_workqueue(uether_wq);
1208
1209}
1210module_exit(gether_exit);
1211MODULE_DESCRIPTION("ethernet over USB driver");
1212MODULE_LICENSE("GPL v2");