blob: 0c9aa1a4d70f4a9df0b7c3e623839aa64086b488 [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;
607
608 spin_lock_irqsave(&dev->lock, flags);
609 if (dev->port_usb) {
610 in = dev->port_usb->in_ep;
611 cdc_filter = dev->port_usb->cdc_filter;
612 } else {
613 in = NULL;
614 cdc_filter = 0;
615 }
616 spin_unlock_irqrestore(&dev->lock, flags);
617
618 if (!in) {
619 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000620 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700621 }
622
Mayank Rana5b165cb2012-05-25 19:52:45 +0530623 /* Allocate memory for tx_reqs to support multi packet transfer */
624 if (dev->port_usb->multi_pkt_xfer && !dev->tx_req_bufsize)
625 alloc_tx_buffer(dev);
626
David Brownell2b3d9422008-06-19 18:19:28 -0700627 /* apply outgoing CDC or RNDIS filters */
628 if (!is_promisc(cdc_filter)) {
629 u8 *dest = skb->data;
630
631 if (is_multicast_ether_addr(dest)) {
632 u16 type;
633
634 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
635 * SET_ETHERNET_MULTICAST_FILTERS requests
636 */
637 if (is_broadcast_ether_addr(dest))
638 type = USB_CDC_PACKET_TYPE_BROADCAST;
639 else
640 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
641 if (!(cdc_filter & type)) {
642 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 }
646 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
647 }
648
649 spin_lock_irqsave(&dev->req_lock, flags);
650 /*
651 * this freelist can be empty if an interrupt triggered disconnect()
652 * and reconfigured the gadget (shutting down this queue) after the
653 * network stack decided to xmit but before we got the spinlock.
654 */
655 if (list_empty(&dev->tx_reqs)) {
656 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000657 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700658 }
659
660 req = container_of(dev->tx_reqs.next, struct usb_request, list);
661 list_del(&req->list);
662
663 /* temporarily stop TX queue when the freelist empties */
664 if (list_empty(&dev->tx_reqs))
665 netif_stop_queue(net);
666 spin_unlock_irqrestore(&dev->req_lock, flags);
667
668 /* no buffer copies needed, unless the network stack did it
669 * or the hardware can't use skb buffers.
670 * or there's not enough space for extra headers we need
671 */
672 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500673 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700674
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500675 spin_lock_irqsave(&dev->lock, flags);
676 if (dev->port_usb)
677 skb = dev->wrap(dev->port_usb, skb);
678 spin_unlock_irqrestore(&dev->lock, flags);
679 if (!skb)
David Brownell2b3d9422008-06-19 18:19:28 -0700680 goto drop;
David Brownell2b3d9422008-06-19 18:19:28 -0700681 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530682
683 spin_lock_irqsave(&dev->req_lock, flags);
684 dev->tx_skb_hold_count++;
685 spin_unlock_irqrestore(&dev->req_lock, flags);
686
687 if (dev->port_usb->multi_pkt_xfer) {
688 memcpy(req->buf + req->length, skb->data, skb->len);
689 req->length = req->length + skb->len;
690 length = req->length;
691 dev_kfree_skb_any(skb);
692
693 spin_lock_irqsave(&dev->req_lock, flags);
694 if (dev->tx_skb_hold_count < TX_SKB_HOLD_THRESHOLD) {
695 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
696 list_add(&req->list, &dev->tx_reqs);
697 spin_unlock_irqrestore(&dev->req_lock, flags);
698 goto success;
699 }
700 }
701
702 dev->no_tx_req_used++;
703 spin_unlock_irqrestore(&dev->req_lock, flags);
704
705 spin_lock_irqsave(&dev->lock, flags);
706 dev->tx_skb_hold_count = 0;
707 spin_unlock_irqrestore(&dev->lock, flags);
708 } else {
709 length = skb->len;
710 req->buf = skb->data;
711 req->context = skb;
712 }
713
David Brownell2b3d9422008-06-19 18:19:28 -0700714 req->complete = tx_complete;
715
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200716 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
717 if (dev->port_usb->is_fixed &&
718 length == dev->port_usb->fixed_in_len &&
719 (length % in->maxpacket) == 0)
720 req->zero = 0;
721 else
722 req->zero = 1;
723
David Brownell2b3d9422008-06-19 18:19:28 -0700724 /* use zlp framing on tx for strict CDC-Ether conformance,
725 * though any robust network rx path ignores extra padding.
726 * and some hardware doesn't like to write zlps.
727 */
Mayank Rana5b165cb2012-05-25 19:52:45 +0530728 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
729 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700730 length++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530731 }
David Brownell2b3d9422008-06-19 18:19:28 -0700732
733 req->length = length;
734
735 /* throttle highspeed IRQ rate back slightly */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530736 if (gadget_is_dualspeed(dev->gadget) &&
737 (dev->gadget->speed == USB_SPEED_HIGH)) {
738 dev->tx_qlen++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530739 if (dev->tx_qlen == (qmult/2)) {
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530740 req->no_interrupt = 0;
741 dev->tx_qlen = 0;
742 } else {
743 req->no_interrupt = 1;
744 }
745 } else {
746 req->no_interrupt = 0;
747 }
David Brownell2b3d9422008-06-19 18:19:28 -0700748
749 retval = usb_ep_queue(in, req, GFP_ATOMIC);
750 switch (retval) {
751 default:
752 DBG(dev, "tx queue err %d\n", retval);
753 break;
754 case 0:
755 net->trans_start = jiffies;
David Brownell2b3d9422008-06-19 18:19:28 -0700756 }
757
758 if (retval) {
Mayank Rana5b165cb2012-05-25 19:52:45 +0530759 if (!dev->port_usb->multi_pkt_xfer)
760 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700761drop:
762 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700763 spin_lock_irqsave(&dev->req_lock, flags);
764 if (list_empty(&dev->tx_reqs))
765 netif_start_queue(net);
766 list_add(&req->list, &dev->tx_reqs);
767 spin_unlock_irqrestore(&dev->req_lock, flags);
768 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530769success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000770 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700771}
772
773/*-------------------------------------------------------------------------*/
774
775static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
776{
777 DBG(dev, "%s\n", __func__);
778
779 /* fill the rx queue */
780 rx_fill(dev, gfp_flags);
781
782 /* and open the tx floodgates */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530783 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700784 netif_wake_queue(dev->net);
785}
786
787static int eth_open(struct net_device *net)
788{
789 struct eth_dev *dev = netdev_priv(net);
790 struct gether *link;
791
792 DBG(dev, "%s\n", __func__);
793 if (netif_carrier_ok(dev->net))
794 eth_start(dev, GFP_KERNEL);
795
796 spin_lock_irq(&dev->lock);
797 link = dev->port_usb;
798 if (link && link->open)
799 link->open(link);
800 spin_unlock_irq(&dev->lock);
801
802 return 0;
803}
804
805static int eth_stop(struct net_device *net)
806{
807 struct eth_dev *dev = netdev_priv(net);
808 unsigned long flags;
809
810 VDBG(dev, "%s\n", __func__);
811 netif_stop_queue(net);
812
813 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
814 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
815 dev->net->stats.rx_errors, dev->net->stats.tx_errors
816 );
817
818 /* ensure there are no more active requests */
819 spin_lock_irqsave(&dev->lock, flags);
820 if (dev->port_usb) {
821 struct gether *link = dev->port_usb;
822
823 if (link->close)
824 link->close(link);
825
826 /* NOTE: we have no abort-queue primitive we could use
827 * to cancel all pending I/O. Instead, we disable then
828 * reenable the endpoints ... this idiom may leave toggle
829 * wrong, but that's a self-correcting error.
830 *
831 * REVISIT: we *COULD* just let the transfers complete at
832 * their own pace; the network stack can handle old packets.
833 * For the moment we leave this here, since it works.
834 */
835 usb_ep_disable(link->in_ep);
836 usb_ep_disable(link->out_ep);
837 if (netif_carrier_ok(net)) {
Rajkumar Raghupathye7c42322012-06-13 19:31:15 +0530838 if (config_ep_by_speed(dev->gadget, &link->func,
839 link->in_ep) ||
840 config_ep_by_speed(dev->gadget, &link->func,
841 link->out_ep)) {
842 link->in_ep->desc = NULL;
843 link->out_ep->desc = NULL;
844 return -EINVAL;
845 }
David Brownell2b3d9422008-06-19 18:19:28 -0700846 DBG(dev, "host still using in/out endpoints\n");
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300847 usb_ep_enable(link->in_ep);
848 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700849 }
850 }
851 spin_unlock_irqrestore(&dev->lock, flags);
852
853 return 0;
854}
855
856/*-------------------------------------------------------------------------*/
857
taeju.parkda2f5f22012-09-14 14:09:03 +0900858static u8 host_ethaddr[ETH_ALEN];
859
David Brownell2b3d9422008-06-19 18:19:28 -0700860/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
861static char *dev_addr;
862module_param(dev_addr, charp, S_IRUGO);
863MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
864
865/* this address is invisible to ifconfig */
866static char *host_addr;
867module_param(host_addr, charp, S_IRUGO);
868MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
869
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200870static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700871{
872 if (str) {
873 unsigned i;
874
875 for (i = 0; i < 6; i++) {
876 unsigned char num;
877
878 if ((*str == '.') || (*str == ':'))
879 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300880 num = hex_to_bin(*str++) << 4;
881 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700882 dev_addr [i] = num;
883 }
884 if (is_valid_ether_addr(dev_addr))
885 return 0;
886 }
887 random_ether_addr(dev_addr);
888 return 1;
889}
890
taeju.parkda2f5f22012-09-14 14:09:03 +0900891static int get_host_ether_addr(u8 *str, u8 *dev_addr)
892{
893 memcpy(dev_addr, str, ETH_ALEN);
894 if (is_valid_ether_addr(dev_addr))
895 return 0;
896
897 random_ether_addr(dev_addr);
898 memcpy(str, dev_addr, ETH_ALEN);
899 return 1;
900}
901
David Brownell2b3d9422008-06-19 18:19:28 -0700902static struct eth_dev *the_dev;
903
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800904static const struct net_device_ops eth_netdev_ops = {
905 .ndo_open = eth_open,
906 .ndo_stop = eth_stop,
907 .ndo_start_xmit = eth_start_xmit,
908 .ndo_change_mtu = ueth_change_mtu,
909 .ndo_set_mac_address = eth_mac_addr,
910 .ndo_validate_addr = eth_validate_addr,
911};
David Brownell2b3d9422008-06-19 18:19:28 -0700912
Marcel Holtmannaa790742010-01-15 22:13:58 -0800913static struct device_type gadget_type = {
914 .name = "gadget",
915};
916
David Brownell2b3d9422008-06-19 18:19:28 -0700917/**
918 * gether_setup - initialize one ethernet-over-usb link
919 * @g: gadget to associated with these links
920 * @ethaddr: NULL, or a buffer in which the ethernet address of the
921 * host side of the link is recorded
922 * Context: may sleep
923 *
924 * This sets up the single network link that may be exported by a
925 * gadget driver using this framework. The link layer addresses are
926 * set up using module parameters.
927 *
928 * Returns negative errno, or zero on success
929 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200930int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
David Brownell2b3d9422008-06-19 18:19:28 -0700931{
Mike Lockwood735d20e2011-08-12 14:35:42 -0700932 return gether_setup_name(g, ethaddr, "usb");
933}
934
935/**
936 * gether_setup_name - initialize one ethernet-over-usb link
937 * @g: gadget to associated with these links
938 * @ethaddr: NULL, or a buffer in which the ethernet address of the
939 * host side of the link is recorded
940 * @netname: name for network device (for example, "usb")
941 * Context: may sleep
942 *
943 * This sets up the single network link that may be exported by a
944 * gadget driver using this framework. The link layer addresses are
945 * set up using module parameters.
946 *
947 * Returns negative errno, or zero on success
948 */
949int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
950 const char *netname)
951{
David Brownell2b3d9422008-06-19 18:19:28 -0700952 struct eth_dev *dev;
953 struct net_device *net;
954 int status;
955
956 if (the_dev)
957 return -EBUSY;
958
959 net = alloc_etherdev(sizeof *dev);
960 if (!net)
961 return -ENOMEM;
962
963 dev = netdev_priv(net);
964 spin_lock_init(&dev->lock);
965 spin_lock_init(&dev->req_lock);
966 INIT_WORK(&dev->work, eth_work);
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700967 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700968 INIT_LIST_HEAD(&dev->tx_reqs);
969 INIT_LIST_HEAD(&dev->rx_reqs);
970
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500971 skb_queue_head_init(&dev->rx_frames);
972
David Brownell2b3d9422008-06-19 18:19:28 -0700973 /* network device setup */
974 dev->net = net;
Mike Lockwood735d20e2011-08-12 14:35:42 -0700975 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700976
977 if (get_ether_addr(dev_addr, net->dev_addr))
978 dev_warn(&g->dev,
979 "using random %s ethernet address\n", "self");
taeju.parkda2f5f22012-09-14 14:09:03 +0900980
981 if (get_host_ether_addr(host_ethaddr, dev->host_mac))
982 dev_warn(&g->dev, "using random %s ethernet address\n", "host");
983 else
984 dev_warn(&g->dev, "using previous %s ethernet address\n", "host");
David Brownell2b3d9422008-06-19 18:19:28 -0700985
986 if (ethaddr)
987 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
988
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800989 net->netdev_ops = &eth_netdev_ops;
990
David Brownell2b3d9422008-06-19 18:19:28 -0700991 SET_ETHTOOL_OPS(net, &ops);
992
David Brownell2b3d9422008-06-19 18:19:28 -0700993 dev->gadget = g;
994 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800995 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700996
997 status = register_netdev(net);
998 if (status < 0) {
999 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1000 free_netdev(net);
1001 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001002 INFO(dev, "MAC %pM\n", net->dev_addr);
1003 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001004
1005 the_dev = dev;
taeju.park5c17cb62012-09-22 14:32:26 +09001006
1007 /* two kinds of host-initiated state changes:
1008 * - iff DATA transfer is active, carrier is "on"
1009 * - tx queueing enabled if open *and* carrier is "on"
1010 */
1011 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -07001012 }
1013
1014 return status;
1015}
1016
1017/**
1018 * gether_cleanup - remove Ethernet-over-USB device
1019 * Context: may sleep
1020 *
1021 * This is called to free all resources allocated by @gether_setup().
1022 */
1023void gether_cleanup(void)
1024{
1025 if (!the_dev)
1026 return;
1027
1028 unregister_netdev(the_dev->net);
Tejun Heo569ff2d2010-12-24 16:14:20 +01001029 flush_work_sync(&the_dev->work);
David Brownell2b3d9422008-06-19 18:19:28 -07001030 free_netdev(the_dev->net);
1031
David Brownell2b3d9422008-06-19 18:19:28 -07001032 the_dev = NULL;
1033}
1034
1035
1036/**
1037 * gether_connect - notify network layer that USB link is active
1038 * @link: the USB link, set up with endpoints, descriptors matching
1039 * current device speed, and any framing wrapper(s) set up.
1040 * Context: irqs blocked
1041 *
1042 * This is called to activate endpoints and let the network layer know
1043 * the connection is active ("carrier detect"). It may cause the I/O
1044 * queues to open and start letting network packets flow, but will in
1045 * any case activate the endpoints so that they respond properly to the
1046 * USB host.
1047 *
1048 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1049 * indicate some error code (negative errno), ep->driver_data values
1050 * have been overwritten.
1051 */
1052struct net_device *gether_connect(struct gether *link)
1053{
1054 struct eth_dev *dev = the_dev;
1055 int result = 0;
1056
1057 if (!dev)
1058 return ERR_PTR(-EINVAL);
1059
1060 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001061 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001062 if (result != 0) {
1063 DBG(dev, "enable %s --> %d\n",
1064 link->in_ep->name, result);
1065 goto fail0;
1066 }
1067
1068 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001069 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001070 if (result != 0) {
1071 DBG(dev, "enable %s --> %d\n",
1072 link->out_ep->name, result);
1073 goto fail1;
1074 }
1075
1076 if (result == 0)
1077 result = alloc_requests(dev, link, qlen(dev->gadget));
1078
1079 if (result == 0) {
1080 dev->zlp = link->is_zlp_ok;
1081 DBG(dev, "qlen %d\n", qlen(dev->gadget));
1082
1083 dev->header_len = link->header_len;
1084 dev->unwrap = link->unwrap;
1085 dev->wrap = link->wrap;
1086
1087 spin_lock(&dev->lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301088 dev->tx_skb_hold_count = 0;
1089 dev->no_tx_req_used = 0;
1090 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001091 dev->port_usb = link;
1092 link->ioport = dev;
David Brownell29bac7b2008-09-06 21:33:49 -07001093 if (netif_running(dev->net)) {
1094 if (link->open)
1095 link->open(link);
1096 } else {
1097 if (link->close)
1098 link->close(link);
1099 }
David Brownell2b3d9422008-06-19 18:19:28 -07001100 spin_unlock(&dev->lock);
1101
1102 netif_carrier_on(dev->net);
1103 if (netif_running(dev->net))
1104 eth_start(dev, GFP_ATOMIC);
1105
1106 /* on error, disable any endpoints */
1107 } else {
1108 (void) usb_ep_disable(link->out_ep);
1109fail1:
1110 (void) usb_ep_disable(link->in_ep);
1111 }
1112fail0:
1113 /* caller is responsible for cleanup on error */
1114 if (result < 0)
1115 return ERR_PTR(result);
1116 return dev->net;
1117}
1118
1119/**
1120 * gether_disconnect - notify network layer that USB link is inactive
1121 * @link: the USB link, on which gether_connect() was called
1122 * Context: irqs blocked
1123 *
1124 * This is called to deactivate endpoints and let the network layer know
1125 * the connection went inactive ("no carrier").
1126 *
1127 * On return, the state is as if gether_connect() had never been called.
1128 * The endpoints are inactive, and accordingly without active USB I/O.
1129 * Pointers to endpoint descriptors and endpoint private data are nulled.
1130 */
1131void gether_disconnect(struct gether *link)
1132{
1133 struct eth_dev *dev = link->ioport;
1134 struct usb_request *req;
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001135 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001136
David Brownell2b3d9422008-06-19 18:19:28 -07001137 if (!dev)
1138 return;
1139
1140 DBG(dev, "%s\n", __func__);
1141
1142 netif_stop_queue(dev->net);
1143 netif_carrier_off(dev->net);
1144
1145 /* disable endpoints, forcing (synchronous) completion
1146 * of all pending i/o. then free the request objects
1147 * and forget about the endpoints.
1148 */
1149 usb_ep_disable(link->in_ep);
1150 spin_lock(&dev->req_lock);
1151 while (!list_empty(&dev->tx_reqs)) {
1152 req = container_of(dev->tx_reqs.next,
1153 struct usb_request, list);
1154 list_del(&req->list);
1155
1156 spin_unlock(&dev->req_lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301157 if (link->multi_pkt_xfer)
1158 kfree(req->buf);
David Brownell2b3d9422008-06-19 18:19:28 -07001159 usb_ep_free_request(link->in_ep, req);
1160 spin_lock(&dev->req_lock);
1161 }
1162 spin_unlock(&dev->req_lock);
1163 link->in_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001164 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001165
1166 usb_ep_disable(link->out_ep);
1167 spin_lock(&dev->req_lock);
1168 while (!list_empty(&dev->rx_reqs)) {
1169 req = container_of(dev->rx_reqs.next,
1170 struct usb_request, list);
1171 list_del(&req->list);
1172
1173 spin_unlock(&dev->req_lock);
1174 usb_ep_free_request(link->out_ep, req);
1175 spin_lock(&dev->req_lock);
1176 }
1177 spin_unlock(&dev->req_lock);
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001178
1179 spin_lock(&dev->rx_frames.lock);
1180 while ((skb = __skb_dequeue(&dev->rx_frames)))
1181 dev_kfree_skb_any(skb);
1182 spin_unlock(&dev->rx_frames.lock);
1183
David Brownell2b3d9422008-06-19 18:19:28 -07001184 link->out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001185 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001186
1187 /* finish forgetting about this USB link episode */
1188 dev->header_len = 0;
1189 dev->unwrap = NULL;
1190 dev->wrap = NULL;
1191
1192 spin_lock(&dev->lock);
1193 dev->port_usb = NULL;
1194 link->ioport = NULL;
1195 spin_unlock(&dev->lock);
1196}
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001197
1198static int __init gether_init(void)
1199{
1200 uether_wq = create_singlethread_workqueue("uether");
1201 if (!uether_wq) {
1202 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1203 return -ENOMEM;
1204 }
1205 return 0;
1206}
1207module_init(gether_init);
1208
1209static void __exit gether_exit(void)
1210{
1211 destroy_workqueue(uether_wq);
1212
1213}
1214module_exit(gether_exit);
1215MODULE_DESCRIPTION("ethernet over USB driver");
1216MODULE_LICENSE("GPL v2");