blob: ad99345cef244b493a30e02be6e520c7a515210d [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);
Mayank Ranab8c88c62012-09-12 18:12:46 +0530296 if (status == -EINVAL)
297 dev->net->stats.rx_errors++;
298 else if (status == -EOVERFLOW)
299 dev->net->stats.rx_over_errors++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500300 } else {
301 dev_kfree_skb_any(skb);
302 status = -ENOTCONN;
303 }
304 spin_unlock_irqrestore(&dev->lock, flags);
305 } else {
306 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700307 }
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500308
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700309 if (!status)
310 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700311 break;
312
313 /* software-driven interface shutdown */
314 case -ECONNRESET: /* unlink */
315 case -ESHUTDOWN: /* disconnect etc */
316 VDBG(dev, "rx shutdown, code %d\n", status);
317 goto quiesce;
318
319 /* for hardware automagic (such as pxa) */
320 case -ECONNABORTED: /* endpoint reset */
321 DBG(dev, "rx %s reset\n", ep->name);
322 defer_kevent(dev, WORK_RX_MEMORY);
323quiesce:
324 dev_kfree_skb_any(skb);
325 goto clean;
326
327 /* data overrun */
328 case -EOVERFLOW:
329 dev->net->stats.rx_over_errors++;
330 /* FALLTHROUGH */
331
332 default:
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700333 queue = 1;
334 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700335 dev->net->stats.rx_errors++;
336 DBG(dev, "rx status %d\n", status);
337 break;
338 }
339
David Brownell2b3d9422008-06-19 18:19:28 -0700340clean:
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700341 spin_lock(&dev->req_lock);
342 list_add(&req->list, &dev->rx_reqs);
343 spin_unlock(&dev->req_lock);
344
345 if (queue)
346 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700347}
348
349static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
350{
351 unsigned i;
352 struct usb_request *req;
353
354 if (!n)
355 return -ENOMEM;
356
357 /* queue/recycle up to N requests */
358 i = n;
359 list_for_each_entry(req, list, list) {
360 if (i-- == 0)
361 goto extra;
362 }
363 while (i--) {
364 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
365 if (!req)
366 return list_empty(list) ? -ENOMEM : 0;
367 list_add(&req->list, list);
368 }
369 return 0;
370
371extra:
372 /* free extras */
373 for (;;) {
374 struct list_head *next;
375
376 next = req->list.next;
377 list_del(&req->list);
378 usb_ep_free_request(ep, req);
379
380 if (next == list)
381 break;
382
383 req = container_of(next, struct usb_request, list);
384 }
385 return 0;
386}
387
388static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
389{
390 int status;
391
392 spin_lock(&dev->req_lock);
393 status = prealloc(&dev->tx_reqs, link->in_ep, n);
394 if (status < 0)
395 goto fail;
396 status = prealloc(&dev->rx_reqs, link->out_ep, n);
397 if (status < 0)
398 goto fail;
399 goto done;
400fail:
401 DBG(dev, "can't alloc requests\n");
402done:
403 spin_unlock(&dev->req_lock);
404 return status;
405}
406
407static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
408{
409 struct usb_request *req;
410 unsigned long flags;
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700411 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700412
413 /* fill unused rxq slots with some skb */
414 spin_lock_irqsave(&dev->req_lock, flags);
415 while (!list_empty(&dev->rx_reqs)) {
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700416 /* break the nexus of continuous completion and re-submission*/
417 if (++req_cnt > qlen(dev->gadget))
418 break;
419
David Brownell2b3d9422008-06-19 18:19:28 -0700420 req = container_of(dev->rx_reqs.next,
421 struct usb_request, list);
422 list_del_init(&req->list);
423 spin_unlock_irqrestore(&dev->req_lock, flags);
424
425 if (rx_submit(dev, req, gfp_flags) < 0) {
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700426 spin_lock_irqsave(&dev->req_lock, flags);
427 list_add(&req->list, &dev->rx_reqs);
428 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700429 defer_kevent(dev, WORK_RX_MEMORY);
430 return;
431 }
432
433 spin_lock_irqsave(&dev->req_lock, flags);
434 }
435 spin_unlock_irqrestore(&dev->req_lock, flags);
436}
437
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700438static void process_rx_w(struct work_struct *work)
439{
440 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
441 struct sk_buff *skb;
442 int status = 0;
443
444 if (!dev->port_usb)
445 return;
446
447 while ((skb = skb_dequeue(&dev->rx_frames))) {
448 if (status < 0
449 || ETH_HLEN > skb->len
450 || skb->len > ETH_FRAME_LEN) {
451 dev->net->stats.rx_errors++;
452 dev->net->stats.rx_length_errors++;
453 DBG(dev, "rx length %d\n", skb->len);
454 dev_kfree_skb_any(skb);
455 continue;
456 }
457 skb->protocol = eth_type_trans(skb, dev->net);
458 dev->net->stats.rx_packets++;
459 dev->net->stats.rx_bytes += skb->len;
460
461 status = netif_rx_ni(skb);
462 }
463
464 if (netif_running(dev->net))
465 rx_fill(dev, GFP_KERNEL);
466}
467
David Brownell2b3d9422008-06-19 18:19:28 -0700468static void eth_work(struct work_struct *work)
469{
470 struct eth_dev *dev = container_of(work, struct eth_dev, work);
471
472 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
473 if (netif_running(dev->net))
474 rx_fill(dev, GFP_KERNEL);
475 }
476
477 if (dev->todo)
478 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
479}
480
481static void tx_complete(struct usb_ep *ep, struct usb_request *req)
482{
483 struct sk_buff *skb = req->context;
484 struct eth_dev *dev = ep->driver_data;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530485 struct net_device *net = dev->net;
486 struct usb_request *new_req;
487 struct usb_ep *in;
488 int length;
489 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700490
491 switch (req->status) {
492 default:
493 dev->net->stats.tx_errors++;
494 VDBG(dev, "tx err %d\n", req->status);
495 /* FALLTHROUGH */
496 case -ECONNRESET: /* unlink */
497 case -ESHUTDOWN: /* disconnect etc */
498 break;
499 case 0:
Mayank Rana5b165cb2012-05-25 19:52:45 +0530500 if (!req->zero)
501 dev->net->stats.tx_bytes += req->length-1;
502 else
503 dev->net->stats.tx_bytes += req->length;
David Brownell2b3d9422008-06-19 18:19:28 -0700504 }
505 dev->net->stats.tx_packets++;
506
507 spin_lock(&dev->req_lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530508 list_add_tail(&req->list, &dev->tx_reqs);
509
510 if (dev->port_usb->multi_pkt_xfer) {
511 dev->no_tx_req_used--;
512 req->length = 0;
513 in = dev->port_usb->in_ep;
514
515 if (!list_empty(&dev->tx_reqs)) {
516 new_req = container_of(dev->tx_reqs.next,
517 struct usb_request, list);
518 list_del(&new_req->list);
519 spin_unlock(&dev->req_lock);
520 if (new_req->length > 0) {
521 length = new_req->length;
522
523 /* NCM requires no zlp if transfer is
524 * dwNtbInMaxSize */
525 if (dev->port_usb->is_fixed &&
526 length == dev->port_usb->fixed_in_len &&
527 (length % in->maxpacket) == 0)
528 new_req->zero = 0;
529 else
530 new_req->zero = 1;
531
532 /* use zlp framing on tx for strict CDC-Ether
533 * conformance, though any robust network rx
534 * path ignores extra padding. and some hardware
535 * doesn't like to write zlps.
536 */
537 if (new_req->zero && !dev->zlp &&
538 (length % in->maxpacket) == 0) {
539 new_req->zero = 0;
540 length++;
541 }
542
543 new_req->length = length;
544 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
545 switch (retval) {
546 default:
547 DBG(dev, "tx queue err %d\n", retval);
548 break;
549 case 0:
550 spin_lock(&dev->req_lock);
551 dev->no_tx_req_used++;
552 spin_unlock(&dev->req_lock);
553 net->trans_start = jiffies;
554 }
555 } else {
556 spin_lock(&dev->req_lock);
557 list_add(&new_req->list, &dev->tx_reqs);
558 spin_unlock(&dev->req_lock);
559 }
560 } else {
561 spin_unlock(&dev->req_lock);
562 }
563 } else {
564 spin_unlock(&dev->req_lock);
565 dev_kfree_skb_any(skb);
566 }
David Brownell2b3d9422008-06-19 18:19:28 -0700567
David Brownell2b3d9422008-06-19 18:19:28 -0700568 if (netif_carrier_ok(dev->net))
569 netif_wake_queue(dev->net);
570}
571
572static inline int is_promisc(u16 cdc_filter)
573{
574 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
575}
576
Mayank Rana5b165cb2012-05-25 19:52:45 +0530577static void alloc_tx_buffer(struct eth_dev *dev)
578{
579 struct list_head *act;
580 struct usb_request *req;
581
582 dev->tx_req_bufsize = (TX_SKB_HOLD_THRESHOLD *
583 (dev->net->mtu
584 + sizeof(struct ethhdr)
585 /* size of rndis_packet_msg_type */
586 + 44
587 + 22));
588
589 list_for_each(act, &dev->tx_reqs) {
590 req = container_of(act, struct usb_request, list);
591 if (!req->buf)
592 req->buf = kmalloc(dev->tx_req_bufsize,
593 GFP_ATOMIC);
594 }
595}
596
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000597static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
598 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700599{
600 struct eth_dev *dev = netdev_priv(net);
601 int length = skb->len;
602 int retval;
603 struct usb_request *req = NULL;
604 unsigned long flags;
605 struct usb_ep *in;
606 u16 cdc_filter;
Mayank Rana7e70c222013-02-15 14:55:30 +0530607 bool multi_pkt_xfer = false;
David Brownell2b3d9422008-06-19 18:19:28 -0700608
609 spin_lock_irqsave(&dev->lock, flags);
610 if (dev->port_usb) {
611 in = dev->port_usb->in_ep;
612 cdc_filter = dev->port_usb->cdc_filter;
Mayank Rana7e70c222013-02-15 14:55:30 +0530613 multi_pkt_xfer = dev->port_usb->multi_pkt_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -0700614 } else {
615 in = NULL;
616 cdc_filter = 0;
617 }
618 spin_unlock_irqrestore(&dev->lock, flags);
619
620 if (!in) {
621 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000622 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700623 }
624
Mayank Rana5b165cb2012-05-25 19:52:45 +0530625 /* Allocate memory for tx_reqs to support multi packet transfer */
Mayank Rana7e70c222013-02-15 14:55:30 +0530626 if (multi_pkt_xfer && !dev->tx_req_bufsize)
Mayank Rana5b165cb2012-05-25 19:52:45 +0530627 alloc_tx_buffer(dev);
628
David Brownell2b3d9422008-06-19 18:19:28 -0700629 /* apply outgoing CDC or RNDIS filters */
630 if (!is_promisc(cdc_filter)) {
631 u8 *dest = skb->data;
632
633 if (is_multicast_ether_addr(dest)) {
634 u16 type;
635
636 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
637 * SET_ETHERNET_MULTICAST_FILTERS requests
638 */
639 if (is_broadcast_ether_addr(dest))
640 type = USB_CDC_PACKET_TYPE_BROADCAST;
641 else
642 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
643 if (!(cdc_filter & type)) {
644 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000645 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700646 }
647 }
648 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
649 }
650
651 spin_lock_irqsave(&dev->req_lock, flags);
652 /*
653 * this freelist can be empty if an interrupt triggered disconnect()
654 * and reconfigured the gadget (shutting down this queue) after the
655 * network stack decided to xmit but before we got the spinlock.
656 */
657 if (list_empty(&dev->tx_reqs)) {
658 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000659 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700660 }
661
662 req = container_of(dev->tx_reqs.next, struct usb_request, list);
663 list_del(&req->list);
664
665 /* temporarily stop TX queue when the freelist empties */
666 if (list_empty(&dev->tx_reqs))
667 netif_stop_queue(net);
668 spin_unlock_irqrestore(&dev->req_lock, flags);
669
670 /* no buffer copies needed, unless the network stack did it
671 * or the hardware can't use skb buffers.
672 * or there's not enough space for extra headers we need
673 */
674 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500675 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700676
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500677 spin_lock_irqsave(&dev->lock, flags);
678 if (dev->port_usb)
679 skb = dev->wrap(dev->port_usb, skb);
680 spin_unlock_irqrestore(&dev->lock, flags);
681 if (!skb)
David Brownell2b3d9422008-06-19 18:19:28 -0700682 goto drop;
David Brownell2b3d9422008-06-19 18:19:28 -0700683 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530684
685 spin_lock_irqsave(&dev->req_lock, flags);
686 dev->tx_skb_hold_count++;
687 spin_unlock_irqrestore(&dev->req_lock, flags);
688
Mayank Rana7e70c222013-02-15 14:55:30 +0530689 if (multi_pkt_xfer) {
Mayank Rana5b165cb2012-05-25 19:52:45 +0530690 memcpy(req->buf + req->length, skb->data, skb->len);
691 req->length = req->length + skb->len;
692 length = req->length;
693 dev_kfree_skb_any(skb);
694
695 spin_lock_irqsave(&dev->req_lock, flags);
696 if (dev->tx_skb_hold_count < TX_SKB_HOLD_THRESHOLD) {
697 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
698 list_add(&req->list, &dev->tx_reqs);
699 spin_unlock_irqrestore(&dev->req_lock, flags);
700 goto success;
701 }
702 }
703
704 dev->no_tx_req_used++;
705 spin_unlock_irqrestore(&dev->req_lock, flags);
706
707 spin_lock_irqsave(&dev->lock, flags);
708 dev->tx_skb_hold_count = 0;
709 spin_unlock_irqrestore(&dev->lock, flags);
710 } else {
711 length = skb->len;
712 req->buf = skb->data;
713 req->context = skb;
714 }
715
David Brownell2b3d9422008-06-19 18:19:28 -0700716 req->complete = tx_complete;
717
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200718 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
719 if (dev->port_usb->is_fixed &&
720 length == dev->port_usb->fixed_in_len &&
721 (length % in->maxpacket) == 0)
722 req->zero = 0;
723 else
724 req->zero = 1;
725
David Brownell2b3d9422008-06-19 18:19:28 -0700726 /* use zlp framing on tx for strict CDC-Ether conformance,
727 * though any robust network rx path ignores extra padding.
728 * and some hardware doesn't like to write zlps.
729 */
Mayank Rana5b165cb2012-05-25 19:52:45 +0530730 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
731 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700732 length++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530733 }
David Brownell2b3d9422008-06-19 18:19:28 -0700734
735 req->length = length;
736
737 /* throttle highspeed IRQ rate back slightly */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530738 if (gadget_is_dualspeed(dev->gadget) &&
739 (dev->gadget->speed == USB_SPEED_HIGH)) {
740 dev->tx_qlen++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530741 if (dev->tx_qlen == (qmult/2)) {
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530742 req->no_interrupt = 0;
743 dev->tx_qlen = 0;
744 } else {
745 req->no_interrupt = 1;
746 }
747 } else {
748 req->no_interrupt = 0;
749 }
David Brownell2b3d9422008-06-19 18:19:28 -0700750
751 retval = usb_ep_queue(in, req, GFP_ATOMIC);
752 switch (retval) {
753 default:
754 DBG(dev, "tx queue err %d\n", retval);
755 break;
756 case 0:
757 net->trans_start = jiffies;
David Brownell2b3d9422008-06-19 18:19:28 -0700758 }
759
760 if (retval) {
Mayank Rana7e70c222013-02-15 14:55:30 +0530761 if (!multi_pkt_xfer)
Mayank Rana5b165cb2012-05-25 19:52:45 +0530762 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700763drop:
764 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700765 spin_lock_irqsave(&dev->req_lock, flags);
766 if (list_empty(&dev->tx_reqs))
767 netif_start_queue(net);
768 list_add(&req->list, &dev->tx_reqs);
769 spin_unlock_irqrestore(&dev->req_lock, flags);
770 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530771success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000772 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700773}
774
775/*-------------------------------------------------------------------------*/
776
777static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
778{
779 DBG(dev, "%s\n", __func__);
780
781 /* fill the rx queue */
782 rx_fill(dev, gfp_flags);
783
784 /* and open the tx floodgates */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530785 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700786 netif_wake_queue(dev->net);
787}
788
789static int eth_open(struct net_device *net)
790{
791 struct eth_dev *dev = netdev_priv(net);
792 struct gether *link;
793
794 DBG(dev, "%s\n", __func__);
795 if (netif_carrier_ok(dev->net))
796 eth_start(dev, GFP_KERNEL);
797
798 spin_lock_irq(&dev->lock);
799 link = dev->port_usb;
800 if (link && link->open)
801 link->open(link);
802 spin_unlock_irq(&dev->lock);
803
804 return 0;
805}
806
807static int eth_stop(struct net_device *net)
808{
809 struct eth_dev *dev = netdev_priv(net);
810 unsigned long flags;
811
812 VDBG(dev, "%s\n", __func__);
813 netif_stop_queue(net);
814
815 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
816 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
817 dev->net->stats.rx_errors, dev->net->stats.tx_errors
818 );
819
820 /* ensure there are no more active requests */
821 spin_lock_irqsave(&dev->lock, flags);
822 if (dev->port_usb) {
823 struct gether *link = dev->port_usb;
824
825 if (link->close)
826 link->close(link);
827
828 /* NOTE: we have no abort-queue primitive we could use
829 * to cancel all pending I/O. Instead, we disable then
830 * reenable the endpoints ... this idiom may leave toggle
831 * wrong, but that's a self-correcting error.
832 *
833 * REVISIT: we *COULD* just let the transfers complete at
834 * their own pace; the network stack can handle old packets.
835 * For the moment we leave this here, since it works.
836 */
837 usb_ep_disable(link->in_ep);
838 usb_ep_disable(link->out_ep);
839 if (netif_carrier_ok(net)) {
Rajkumar Raghupathye7c42322012-06-13 19:31:15 +0530840 if (config_ep_by_speed(dev->gadget, &link->func,
841 link->in_ep) ||
842 config_ep_by_speed(dev->gadget, &link->func,
843 link->out_ep)) {
844 link->in_ep->desc = NULL;
845 link->out_ep->desc = NULL;
846 return -EINVAL;
847 }
David Brownell2b3d9422008-06-19 18:19:28 -0700848 DBG(dev, "host still using in/out endpoints\n");
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300849 usb_ep_enable(link->in_ep);
850 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700851 }
852 }
853 spin_unlock_irqrestore(&dev->lock, flags);
854
855 return 0;
856}
857
858/*-------------------------------------------------------------------------*/
859
taeju.parkda2f5f22012-09-14 14:09:03 +0900860static u8 host_ethaddr[ETH_ALEN];
861
David Brownell2b3d9422008-06-19 18:19:28 -0700862/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
863static char *dev_addr;
864module_param(dev_addr, charp, S_IRUGO);
865MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
866
867/* this address is invisible to ifconfig */
868static char *host_addr;
869module_param(host_addr, charp, S_IRUGO);
870MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
871
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200872static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700873{
874 if (str) {
875 unsigned i;
876
877 for (i = 0; i < 6; i++) {
878 unsigned char num;
879
880 if ((*str == '.') || (*str == ':'))
881 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300882 num = hex_to_bin(*str++) << 4;
883 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700884 dev_addr [i] = num;
885 }
886 if (is_valid_ether_addr(dev_addr))
887 return 0;
888 }
889 random_ether_addr(dev_addr);
890 return 1;
891}
892
taeju.parkda2f5f22012-09-14 14:09:03 +0900893static int get_host_ether_addr(u8 *str, u8 *dev_addr)
894{
895 memcpy(dev_addr, str, ETH_ALEN);
896 if (is_valid_ether_addr(dev_addr))
897 return 0;
898
899 random_ether_addr(dev_addr);
900 memcpy(str, dev_addr, ETH_ALEN);
901 return 1;
902}
903
David Brownell2b3d9422008-06-19 18:19:28 -0700904static struct eth_dev *the_dev;
905
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800906static const struct net_device_ops eth_netdev_ops = {
907 .ndo_open = eth_open,
908 .ndo_stop = eth_stop,
909 .ndo_start_xmit = eth_start_xmit,
910 .ndo_change_mtu = ueth_change_mtu,
911 .ndo_set_mac_address = eth_mac_addr,
912 .ndo_validate_addr = eth_validate_addr,
913};
David Brownell2b3d9422008-06-19 18:19:28 -0700914
Marcel Holtmannaa790742010-01-15 22:13:58 -0800915static struct device_type gadget_type = {
916 .name = "gadget",
917};
918
David Brownell2b3d9422008-06-19 18:19:28 -0700919/**
920 * gether_setup - initialize one ethernet-over-usb link
921 * @g: gadget to associated with these links
922 * @ethaddr: NULL, or a buffer in which the ethernet address of the
923 * host side of the link is recorded
924 * Context: may sleep
925 *
926 * This sets up the single network link that may be exported by a
927 * gadget driver using this framework. The link layer addresses are
928 * set up using module parameters.
929 *
930 * Returns negative errno, or zero on success
931 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200932int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
David Brownell2b3d9422008-06-19 18:19:28 -0700933{
Mike Lockwood735d20e2011-08-12 14:35:42 -0700934 return gether_setup_name(g, ethaddr, "usb");
935}
936
937/**
938 * gether_setup_name - initialize one ethernet-over-usb link
939 * @g: gadget to associated with these links
940 * @ethaddr: NULL, or a buffer in which the ethernet address of the
941 * host side of the link is recorded
942 * @netname: name for network device (for example, "usb")
943 * Context: may sleep
944 *
945 * This sets up the single network link that may be exported by a
946 * gadget driver using this framework. The link layer addresses are
947 * set up using module parameters.
948 *
949 * Returns negative errno, or zero on success
950 */
951int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
952 const char *netname)
953{
David Brownell2b3d9422008-06-19 18:19:28 -0700954 struct eth_dev *dev;
955 struct net_device *net;
956 int status;
957
958 if (the_dev)
959 return -EBUSY;
960
961 net = alloc_etherdev(sizeof *dev);
962 if (!net)
963 return -ENOMEM;
964
965 dev = netdev_priv(net);
966 spin_lock_init(&dev->lock);
967 spin_lock_init(&dev->req_lock);
968 INIT_WORK(&dev->work, eth_work);
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700969 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700970 INIT_LIST_HEAD(&dev->tx_reqs);
971 INIT_LIST_HEAD(&dev->rx_reqs);
972
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500973 skb_queue_head_init(&dev->rx_frames);
974
David Brownell2b3d9422008-06-19 18:19:28 -0700975 /* network device setup */
976 dev->net = net;
Mike Lockwood735d20e2011-08-12 14:35:42 -0700977 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700978
979 if (get_ether_addr(dev_addr, net->dev_addr))
980 dev_warn(&g->dev,
981 "using random %s ethernet address\n", "self");
taeju.parkda2f5f22012-09-14 14:09:03 +0900982
983 if (get_host_ether_addr(host_ethaddr, dev->host_mac))
984 dev_warn(&g->dev, "using random %s ethernet address\n", "host");
985 else
986 dev_warn(&g->dev, "using previous %s ethernet address\n", "host");
David Brownell2b3d9422008-06-19 18:19:28 -0700987
988 if (ethaddr)
989 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
990
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800991 net->netdev_ops = &eth_netdev_ops;
992
David Brownell2b3d9422008-06-19 18:19:28 -0700993 SET_ETHTOOL_OPS(net, &ops);
994
David Brownell2b3d9422008-06-19 18:19:28 -0700995 dev->gadget = g;
996 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800997 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700998
999 status = register_netdev(net);
1000 if (status < 0) {
1001 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1002 free_netdev(net);
1003 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001004 INFO(dev, "MAC %pM\n", net->dev_addr);
1005 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001006
1007 the_dev = dev;
taeju.park5c17cb62012-09-22 14:32:26 +09001008
1009 /* two kinds of host-initiated state changes:
1010 * - iff DATA transfer is active, carrier is "on"
1011 * - tx queueing enabled if open *and* carrier is "on"
1012 */
1013 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -07001014 }
1015
1016 return status;
1017}
1018
1019/**
1020 * gether_cleanup - remove Ethernet-over-USB device
1021 * Context: may sleep
1022 *
1023 * This is called to free all resources allocated by @gether_setup().
1024 */
1025void gether_cleanup(void)
1026{
1027 if (!the_dev)
1028 return;
1029
1030 unregister_netdev(the_dev->net);
Tejun Heo569ff2d2010-12-24 16:14:20 +01001031 flush_work_sync(&the_dev->work);
David Brownell2b3d9422008-06-19 18:19:28 -07001032 free_netdev(the_dev->net);
1033
David Brownell2b3d9422008-06-19 18:19:28 -07001034 the_dev = NULL;
1035}
1036
1037
1038/**
1039 * gether_connect - notify network layer that USB link is active
1040 * @link: the USB link, set up with endpoints, descriptors matching
1041 * current device speed, and any framing wrapper(s) set up.
1042 * Context: irqs blocked
1043 *
1044 * This is called to activate endpoints and let the network layer know
1045 * the connection is active ("carrier detect"). It may cause the I/O
1046 * queues to open and start letting network packets flow, but will in
1047 * any case activate the endpoints so that they respond properly to the
1048 * USB host.
1049 *
1050 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1051 * indicate some error code (negative errno), ep->driver_data values
1052 * have been overwritten.
1053 */
1054struct net_device *gether_connect(struct gether *link)
1055{
1056 struct eth_dev *dev = the_dev;
1057 int result = 0;
1058
1059 if (!dev)
1060 return ERR_PTR(-EINVAL);
1061
1062 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001063 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001064 if (result != 0) {
1065 DBG(dev, "enable %s --> %d\n",
1066 link->in_ep->name, result);
1067 goto fail0;
1068 }
1069
1070 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001071 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001072 if (result != 0) {
1073 DBG(dev, "enable %s --> %d\n",
1074 link->out_ep->name, result);
1075 goto fail1;
1076 }
1077
1078 if (result == 0)
1079 result = alloc_requests(dev, link, qlen(dev->gadget));
1080
1081 if (result == 0) {
1082 dev->zlp = link->is_zlp_ok;
1083 DBG(dev, "qlen %d\n", qlen(dev->gadget));
1084
1085 dev->header_len = link->header_len;
1086 dev->unwrap = link->unwrap;
1087 dev->wrap = link->wrap;
1088
1089 spin_lock(&dev->lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301090 dev->tx_skb_hold_count = 0;
1091 dev->no_tx_req_used = 0;
1092 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001093 dev->port_usb = link;
1094 link->ioport = dev;
David Brownell29bac7b2008-09-06 21:33:49 -07001095 if (netif_running(dev->net)) {
1096 if (link->open)
1097 link->open(link);
1098 } else {
1099 if (link->close)
1100 link->close(link);
1101 }
David Brownell2b3d9422008-06-19 18:19:28 -07001102 spin_unlock(&dev->lock);
1103
1104 netif_carrier_on(dev->net);
1105 if (netif_running(dev->net))
1106 eth_start(dev, GFP_ATOMIC);
1107
1108 /* on error, disable any endpoints */
1109 } else {
1110 (void) usb_ep_disable(link->out_ep);
1111fail1:
1112 (void) usb_ep_disable(link->in_ep);
1113 }
1114fail0:
1115 /* caller is responsible for cleanup on error */
1116 if (result < 0)
1117 return ERR_PTR(result);
1118 return dev->net;
1119}
1120
1121/**
1122 * gether_disconnect - notify network layer that USB link is inactive
1123 * @link: the USB link, on which gether_connect() was called
1124 * Context: irqs blocked
1125 *
1126 * This is called to deactivate endpoints and let the network layer know
1127 * the connection went inactive ("no carrier").
1128 *
1129 * On return, the state is as if gether_connect() had never been called.
1130 * The endpoints are inactive, and accordingly without active USB I/O.
1131 * Pointers to endpoint descriptors and endpoint private data are nulled.
1132 */
1133void gether_disconnect(struct gether *link)
1134{
1135 struct eth_dev *dev = link->ioport;
1136 struct usb_request *req;
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001137 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001138
David Brownell2b3d9422008-06-19 18:19:28 -07001139 if (!dev)
1140 return;
1141
1142 DBG(dev, "%s\n", __func__);
1143
1144 netif_stop_queue(dev->net);
1145 netif_carrier_off(dev->net);
1146
1147 /* disable endpoints, forcing (synchronous) completion
1148 * of all pending i/o. then free the request objects
1149 * and forget about the endpoints.
1150 */
1151 usb_ep_disable(link->in_ep);
1152 spin_lock(&dev->req_lock);
1153 while (!list_empty(&dev->tx_reqs)) {
1154 req = container_of(dev->tx_reqs.next,
1155 struct usb_request, list);
1156 list_del(&req->list);
1157
1158 spin_unlock(&dev->req_lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301159 if (link->multi_pkt_xfer)
1160 kfree(req->buf);
David Brownell2b3d9422008-06-19 18:19:28 -07001161 usb_ep_free_request(link->in_ep, req);
1162 spin_lock(&dev->req_lock);
1163 }
1164 spin_unlock(&dev->req_lock);
1165 link->in_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001166 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001167
1168 usb_ep_disable(link->out_ep);
1169 spin_lock(&dev->req_lock);
1170 while (!list_empty(&dev->rx_reqs)) {
1171 req = container_of(dev->rx_reqs.next,
1172 struct usb_request, list);
1173 list_del(&req->list);
1174
1175 spin_unlock(&dev->req_lock);
1176 usb_ep_free_request(link->out_ep, req);
1177 spin_lock(&dev->req_lock);
1178 }
1179 spin_unlock(&dev->req_lock);
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001180
1181 spin_lock(&dev->rx_frames.lock);
1182 while ((skb = __skb_dequeue(&dev->rx_frames)))
1183 dev_kfree_skb_any(skb);
1184 spin_unlock(&dev->rx_frames.lock);
1185
David Brownell2b3d9422008-06-19 18:19:28 -07001186 link->out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001187 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001188
1189 /* finish forgetting about this USB link episode */
1190 dev->header_len = 0;
1191 dev->unwrap = NULL;
1192 dev->wrap = NULL;
1193
1194 spin_lock(&dev->lock);
1195 dev->port_usb = NULL;
1196 link->ioport = NULL;
1197 spin_unlock(&dev->lock);
1198}
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001199
1200static int __init gether_init(void)
1201{
1202 uether_wq = create_singlethread_workqueue("uether");
1203 if (!uether_wq) {
1204 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1205 return -ENOMEM;
1206 }
1207 return 0;
1208}
1209module_init(gether_init);
1210
1211static void __exit gether_exit(void)
1212{
1213 destroy_workqueue(uether_wq);
1214
1215}
1216module_exit(gether_exit);
1217MODULE_DESCRIPTION("ethernet over USB driver");
1218MODULE_LICENSE("GPL v2");