blob: 549c5e058c163a5e53a0b9e157bfef804cd63731 [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);
Pavankumar Kondetid15dbb12013-04-01 18:13:32 +0530548 new_req->length = 0;
549 spin_lock(&dev->req_lock);
550 list_add_tail(&new_req->list,
551 &dev->tx_reqs);
552 spin_unlock(&dev->req_lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530553 break;
554 case 0:
555 spin_lock(&dev->req_lock);
556 dev->no_tx_req_used++;
557 spin_unlock(&dev->req_lock);
558 net->trans_start = jiffies;
559 }
560 } else {
561 spin_lock(&dev->req_lock);
Pavankumar Kondetid15dbb12013-04-01 18:13:32 +0530562 /*
563 * Put the idle request at the back of the
564 * queue. The xmit function will put the
565 * unfinished request at the beginning of the
566 * queue.
567 */
568 list_add_tail(&new_req->list, &dev->tx_reqs);
Mayank Rana5b165cb2012-05-25 19:52:45 +0530569 spin_unlock(&dev->req_lock);
570 }
571 } else {
572 spin_unlock(&dev->req_lock);
573 }
574 } else {
575 spin_unlock(&dev->req_lock);
576 dev_kfree_skb_any(skb);
577 }
David Brownell2b3d9422008-06-19 18:19:28 -0700578
David Brownell2b3d9422008-06-19 18:19:28 -0700579 if (netif_carrier_ok(dev->net))
580 netif_wake_queue(dev->net);
581}
582
583static inline int is_promisc(u16 cdc_filter)
584{
585 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
586}
587
Mayank Rana5b165cb2012-05-25 19:52:45 +0530588static void alloc_tx_buffer(struct eth_dev *dev)
589{
590 struct list_head *act;
591 struct usb_request *req;
592
593 dev->tx_req_bufsize = (TX_SKB_HOLD_THRESHOLD *
594 (dev->net->mtu
595 + sizeof(struct ethhdr)
596 /* size of rndis_packet_msg_type */
597 + 44
598 + 22));
599
600 list_for_each(act, &dev->tx_reqs) {
601 req = container_of(act, struct usb_request, list);
602 if (!req->buf)
603 req->buf = kmalloc(dev->tx_req_bufsize,
604 GFP_ATOMIC);
605 }
606}
607
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000608static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
609 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700610{
611 struct eth_dev *dev = netdev_priv(net);
612 int length = skb->len;
613 int retval;
614 struct usb_request *req = NULL;
615 unsigned long flags;
616 struct usb_ep *in;
617 u16 cdc_filter;
Mayank Rana7e70c222013-02-15 14:55:30 +0530618 bool multi_pkt_xfer = false;
David Brownell2b3d9422008-06-19 18:19:28 -0700619
620 spin_lock_irqsave(&dev->lock, flags);
621 if (dev->port_usb) {
622 in = dev->port_usb->in_ep;
623 cdc_filter = dev->port_usb->cdc_filter;
Mayank Rana7e70c222013-02-15 14:55:30 +0530624 multi_pkt_xfer = dev->port_usb->multi_pkt_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -0700625 } else {
626 in = NULL;
627 cdc_filter = 0;
628 }
629 spin_unlock_irqrestore(&dev->lock, flags);
630
631 if (!in) {
632 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000633 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700634 }
635
Mayank Rana5b165cb2012-05-25 19:52:45 +0530636 /* Allocate memory for tx_reqs to support multi packet transfer */
Mayank Rana7e70c222013-02-15 14:55:30 +0530637 if (multi_pkt_xfer && !dev->tx_req_bufsize)
Mayank Rana5b165cb2012-05-25 19:52:45 +0530638 alloc_tx_buffer(dev);
639
David Brownell2b3d9422008-06-19 18:19:28 -0700640 /* apply outgoing CDC or RNDIS filters */
641 if (!is_promisc(cdc_filter)) {
642 u8 *dest = skb->data;
643
644 if (is_multicast_ether_addr(dest)) {
645 u16 type;
646
647 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
648 * SET_ETHERNET_MULTICAST_FILTERS requests
649 */
650 if (is_broadcast_ether_addr(dest))
651 type = USB_CDC_PACKET_TYPE_BROADCAST;
652 else
653 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
654 if (!(cdc_filter & type)) {
655 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000656 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700657 }
658 }
659 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
660 }
661
662 spin_lock_irqsave(&dev->req_lock, flags);
663 /*
664 * this freelist can be empty if an interrupt triggered disconnect()
665 * and reconfigured the gadget (shutting down this queue) after the
666 * network stack decided to xmit but before we got the spinlock.
667 */
668 if (list_empty(&dev->tx_reqs)) {
669 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000670 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700671 }
672
673 req = container_of(dev->tx_reqs.next, struct usb_request, list);
674 list_del(&req->list);
675
676 /* temporarily stop TX queue when the freelist empties */
677 if (list_empty(&dev->tx_reqs))
678 netif_stop_queue(net);
679 spin_unlock_irqrestore(&dev->req_lock, flags);
680
681 /* no buffer copies needed, unless the network stack did it
682 * or the hardware can't use skb buffers.
683 * or there's not enough space for extra headers we need
684 */
685 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500686 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700687
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500688 spin_lock_irqsave(&dev->lock, flags);
689 if (dev->port_usb)
690 skb = dev->wrap(dev->port_usb, skb);
691 spin_unlock_irqrestore(&dev->lock, flags);
692 if (!skb)
David Brownell2b3d9422008-06-19 18:19:28 -0700693 goto drop;
David Brownell2b3d9422008-06-19 18:19:28 -0700694 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530695
696 spin_lock_irqsave(&dev->req_lock, flags);
697 dev->tx_skb_hold_count++;
698 spin_unlock_irqrestore(&dev->req_lock, flags);
699
Mayank Rana7e70c222013-02-15 14:55:30 +0530700 if (multi_pkt_xfer) {
Mayank Rana5b165cb2012-05-25 19:52:45 +0530701 memcpy(req->buf + req->length, skb->data, skb->len);
702 req->length = req->length + skb->len;
703 length = req->length;
704 dev_kfree_skb_any(skb);
705
706 spin_lock_irqsave(&dev->req_lock, flags);
707 if (dev->tx_skb_hold_count < TX_SKB_HOLD_THRESHOLD) {
708 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
709 list_add(&req->list, &dev->tx_reqs);
710 spin_unlock_irqrestore(&dev->req_lock, flags);
711 goto success;
712 }
713 }
714
715 dev->no_tx_req_used++;
716 spin_unlock_irqrestore(&dev->req_lock, flags);
717
718 spin_lock_irqsave(&dev->lock, flags);
719 dev->tx_skb_hold_count = 0;
720 spin_unlock_irqrestore(&dev->lock, flags);
721 } else {
722 length = skb->len;
723 req->buf = skb->data;
724 req->context = skb;
725 }
726
David Brownell2b3d9422008-06-19 18:19:28 -0700727 req->complete = tx_complete;
728
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200729 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
730 if (dev->port_usb->is_fixed &&
731 length == dev->port_usb->fixed_in_len &&
732 (length % in->maxpacket) == 0)
733 req->zero = 0;
734 else
735 req->zero = 1;
736
David Brownell2b3d9422008-06-19 18:19:28 -0700737 /* use zlp framing on tx for strict CDC-Ether conformance,
738 * though any robust network rx path ignores extra padding.
739 * and some hardware doesn't like to write zlps.
740 */
Mayank Rana5b165cb2012-05-25 19:52:45 +0530741 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
742 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700743 length++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530744 }
David Brownell2b3d9422008-06-19 18:19:28 -0700745
746 req->length = length;
747
748 /* throttle highspeed IRQ rate back slightly */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530749 if (gadget_is_dualspeed(dev->gadget) &&
750 (dev->gadget->speed == USB_SPEED_HIGH)) {
751 dev->tx_qlen++;
Mayank Rana5b165cb2012-05-25 19:52:45 +0530752 if (dev->tx_qlen == (qmult/2)) {
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530753 req->no_interrupt = 0;
754 dev->tx_qlen = 0;
755 } else {
756 req->no_interrupt = 1;
757 }
758 } else {
759 req->no_interrupt = 0;
760 }
David Brownell2b3d9422008-06-19 18:19:28 -0700761
762 retval = usb_ep_queue(in, req, GFP_ATOMIC);
763 switch (retval) {
764 default:
765 DBG(dev, "tx queue err %d\n", retval);
766 break;
767 case 0:
768 net->trans_start = jiffies;
David Brownell2b3d9422008-06-19 18:19:28 -0700769 }
770
771 if (retval) {
Mayank Rana7e70c222013-02-15 14:55:30 +0530772 if (!multi_pkt_xfer)
Mayank Rana5b165cb2012-05-25 19:52:45 +0530773 dev_kfree_skb_any(skb);
Pavankumar Kondetid15dbb12013-04-01 18:13:32 +0530774 else
775 req->length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700776drop:
777 dev->net->stats.tx_dropped++;
David Brownell2b3d9422008-06-19 18:19:28 -0700778 spin_lock_irqsave(&dev->req_lock, flags);
779 if (list_empty(&dev->tx_reqs))
780 netif_start_queue(net);
781 list_add(&req->list, &dev->tx_reqs);
782 spin_unlock_irqrestore(&dev->req_lock, flags);
783 }
Mayank Rana5b165cb2012-05-25 19:52:45 +0530784success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000785 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700786}
787
788/*-------------------------------------------------------------------------*/
789
790static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
791{
792 DBG(dev, "%s\n", __func__);
793
794 /* fill the rx queue */
795 rx_fill(dev, gfp_flags);
796
797 /* and open the tx floodgates */
Vijayavardhan Vennapusa20b627e2012-03-06 14:26:11 +0530798 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700799 netif_wake_queue(dev->net);
800}
801
802static int eth_open(struct net_device *net)
803{
804 struct eth_dev *dev = netdev_priv(net);
805 struct gether *link;
806
807 DBG(dev, "%s\n", __func__);
808 if (netif_carrier_ok(dev->net))
809 eth_start(dev, GFP_KERNEL);
810
811 spin_lock_irq(&dev->lock);
812 link = dev->port_usb;
813 if (link && link->open)
814 link->open(link);
815 spin_unlock_irq(&dev->lock);
816
817 return 0;
818}
819
820static int eth_stop(struct net_device *net)
821{
822 struct eth_dev *dev = netdev_priv(net);
823 unsigned long flags;
824
825 VDBG(dev, "%s\n", __func__);
826 netif_stop_queue(net);
827
828 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
829 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
830 dev->net->stats.rx_errors, dev->net->stats.tx_errors
831 );
832
833 /* ensure there are no more active requests */
834 spin_lock_irqsave(&dev->lock, flags);
835 if (dev->port_usb) {
836 struct gether *link = dev->port_usb;
837
838 if (link->close)
839 link->close(link);
840
841 /* NOTE: we have no abort-queue primitive we could use
842 * to cancel all pending I/O. Instead, we disable then
843 * reenable the endpoints ... this idiom may leave toggle
844 * wrong, but that's a self-correcting error.
845 *
846 * REVISIT: we *COULD* just let the transfers complete at
847 * their own pace; the network stack can handle old packets.
848 * For the moment we leave this here, since it works.
849 */
850 usb_ep_disable(link->in_ep);
851 usb_ep_disable(link->out_ep);
852 if (netif_carrier_ok(net)) {
Rajkumar Raghupathye7c42322012-06-13 19:31:15 +0530853 if (config_ep_by_speed(dev->gadget, &link->func,
854 link->in_ep) ||
855 config_ep_by_speed(dev->gadget, &link->func,
856 link->out_ep)) {
857 link->in_ep->desc = NULL;
858 link->out_ep->desc = NULL;
859 return -EINVAL;
860 }
David Brownell2b3d9422008-06-19 18:19:28 -0700861 DBG(dev, "host still using in/out endpoints\n");
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300862 usb_ep_enable(link->in_ep);
863 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700864 }
865 }
866 spin_unlock_irqrestore(&dev->lock, flags);
867
868 return 0;
869}
870
871/*-------------------------------------------------------------------------*/
872
taeju.parkda2f5f22012-09-14 14:09:03 +0900873static u8 host_ethaddr[ETH_ALEN];
874
David Brownell2b3d9422008-06-19 18:19:28 -0700875/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
876static char *dev_addr;
877module_param(dev_addr, charp, S_IRUGO);
878MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
879
880/* this address is invisible to ifconfig */
881static char *host_addr;
882module_param(host_addr, charp, S_IRUGO);
883MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
884
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200885static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700886{
887 if (str) {
888 unsigned i;
889
890 for (i = 0; i < 6; i++) {
891 unsigned char num;
892
893 if ((*str == '.') || (*str == ':'))
894 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300895 num = hex_to_bin(*str++) << 4;
896 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700897 dev_addr [i] = num;
898 }
899 if (is_valid_ether_addr(dev_addr))
900 return 0;
901 }
902 random_ether_addr(dev_addr);
903 return 1;
904}
905
taeju.parkda2f5f22012-09-14 14:09:03 +0900906static int get_host_ether_addr(u8 *str, u8 *dev_addr)
907{
908 memcpy(dev_addr, str, ETH_ALEN);
909 if (is_valid_ether_addr(dev_addr))
910 return 0;
911
912 random_ether_addr(dev_addr);
913 memcpy(str, dev_addr, ETH_ALEN);
914 return 1;
915}
916
David Brownell2b3d9422008-06-19 18:19:28 -0700917static struct eth_dev *the_dev;
918
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800919static const struct net_device_ops eth_netdev_ops = {
920 .ndo_open = eth_open,
921 .ndo_stop = eth_stop,
922 .ndo_start_xmit = eth_start_xmit,
923 .ndo_change_mtu = ueth_change_mtu,
924 .ndo_set_mac_address = eth_mac_addr,
925 .ndo_validate_addr = eth_validate_addr,
926};
David Brownell2b3d9422008-06-19 18:19:28 -0700927
Marcel Holtmannaa790742010-01-15 22:13:58 -0800928static struct device_type gadget_type = {
929 .name = "gadget",
930};
931
David Brownell2b3d9422008-06-19 18:19:28 -0700932/**
933 * gether_setup - initialize one ethernet-over-usb link
934 * @g: gadget to associated with these links
935 * @ethaddr: NULL, or a buffer in which the ethernet address of the
936 * host side of the link is recorded
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 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200945int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
David Brownell2b3d9422008-06-19 18:19:28 -0700946{
Mike Lockwood735d20e2011-08-12 14:35:42 -0700947 return gether_setup_name(g, ethaddr, "usb");
948}
949
950/**
951 * gether_setup_name - initialize one ethernet-over-usb link
952 * @g: gadget to associated with these links
953 * @ethaddr: NULL, or a buffer in which the ethernet address of the
954 * host side of the link is recorded
955 * @netname: name for network device (for example, "usb")
956 * Context: may sleep
957 *
958 * This sets up the single network link that may be exported by a
959 * gadget driver using this framework. The link layer addresses are
960 * set up using module parameters.
961 *
962 * Returns negative errno, or zero on success
963 */
964int gether_setup_name(struct usb_gadget *g, u8 ethaddr[ETH_ALEN],
965 const char *netname)
966{
David Brownell2b3d9422008-06-19 18:19:28 -0700967 struct eth_dev *dev;
968 struct net_device *net;
969 int status;
970
971 if (the_dev)
972 return -EBUSY;
973
974 net = alloc_etherdev(sizeof *dev);
975 if (!net)
976 return -ENOMEM;
977
978 dev = netdev_priv(net);
979 spin_lock_init(&dev->lock);
980 spin_lock_init(&dev->req_lock);
981 INIT_WORK(&dev->work, eth_work);
Hemant Kumar08eb78e2012-07-30 20:38:47 -0700982 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700983 INIT_LIST_HEAD(&dev->tx_reqs);
984 INIT_LIST_HEAD(&dev->rx_reqs);
985
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500986 skb_queue_head_init(&dev->rx_frames);
987
David Brownell2b3d9422008-06-19 18:19:28 -0700988 /* network device setup */
989 dev->net = net;
Mike Lockwood735d20e2011-08-12 14:35:42 -0700990 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700991
992 if (get_ether_addr(dev_addr, net->dev_addr))
993 dev_warn(&g->dev,
994 "using random %s ethernet address\n", "self");
taeju.parkda2f5f22012-09-14 14:09:03 +0900995
996 if (get_host_ether_addr(host_ethaddr, dev->host_mac))
997 dev_warn(&g->dev, "using random %s ethernet address\n", "host");
998 else
999 dev_warn(&g->dev, "using previous %s ethernet address\n", "host");
David Brownell2b3d9422008-06-19 18:19:28 -07001000
1001 if (ethaddr)
1002 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
1003
Stephen Hemminger5ec38f32009-01-07 18:05:39 -08001004 net->netdev_ops = &eth_netdev_ops;
1005
David Brownell2b3d9422008-06-19 18:19:28 -07001006 SET_ETHTOOL_OPS(net, &ops);
1007
David Brownell2b3d9422008-06-19 18:19:28 -07001008 dev->gadget = g;
1009 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -08001010 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -07001011
1012 status = register_netdev(net);
1013 if (status < 0) {
1014 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1015 free_netdev(net);
1016 } else {
Johannes Berge1749612008-10-27 15:59:26 -07001017 INFO(dev, "MAC %pM\n", net->dev_addr);
1018 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -07001019
1020 the_dev = dev;
taeju.park5c17cb62012-09-22 14:32:26 +09001021
1022 /* two kinds of host-initiated state changes:
1023 * - iff DATA transfer is active, carrier is "on"
1024 * - tx queueing enabled if open *and* carrier is "on"
1025 */
1026 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -07001027 }
1028
1029 return status;
1030}
1031
1032/**
1033 * gether_cleanup - remove Ethernet-over-USB device
1034 * Context: may sleep
1035 *
1036 * This is called to free all resources allocated by @gether_setup().
1037 */
1038void gether_cleanup(void)
1039{
1040 if (!the_dev)
1041 return;
1042
1043 unregister_netdev(the_dev->net);
Tejun Heo569ff2d2010-12-24 16:14:20 +01001044 flush_work_sync(&the_dev->work);
David Brownell2b3d9422008-06-19 18:19:28 -07001045 free_netdev(the_dev->net);
1046
David Brownell2b3d9422008-06-19 18:19:28 -07001047 the_dev = NULL;
1048}
1049
1050
1051/**
1052 * gether_connect - notify network layer that USB link is active
1053 * @link: the USB link, set up with endpoints, descriptors matching
1054 * current device speed, and any framing wrapper(s) set up.
1055 * Context: irqs blocked
1056 *
1057 * This is called to activate endpoints and let the network layer know
1058 * the connection is active ("carrier detect"). It may cause the I/O
1059 * queues to open and start letting network packets flow, but will in
1060 * any case activate the endpoints so that they respond properly to the
1061 * USB host.
1062 *
1063 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1064 * indicate some error code (negative errno), ep->driver_data values
1065 * have been overwritten.
1066 */
1067struct net_device *gether_connect(struct gether *link)
1068{
1069 struct eth_dev *dev = the_dev;
1070 int result = 0;
1071
1072 if (!dev)
1073 return ERR_PTR(-EINVAL);
1074
1075 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001076 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001077 if (result != 0) {
1078 DBG(dev, "enable %s --> %d\n",
1079 link->in_ep->name, result);
1080 goto fail0;
1081 }
1082
1083 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001084 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001085 if (result != 0) {
1086 DBG(dev, "enable %s --> %d\n",
1087 link->out_ep->name, result);
1088 goto fail1;
1089 }
1090
1091 if (result == 0)
1092 result = alloc_requests(dev, link, qlen(dev->gadget));
1093
1094 if (result == 0) {
1095 dev->zlp = link->is_zlp_ok;
1096 DBG(dev, "qlen %d\n", qlen(dev->gadget));
1097
1098 dev->header_len = link->header_len;
1099 dev->unwrap = link->unwrap;
1100 dev->wrap = link->wrap;
1101
1102 spin_lock(&dev->lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301103 dev->tx_skb_hold_count = 0;
1104 dev->no_tx_req_used = 0;
1105 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001106 dev->port_usb = link;
1107 link->ioport = dev;
David Brownell29bac7b2008-09-06 21:33:49 -07001108 if (netif_running(dev->net)) {
1109 if (link->open)
1110 link->open(link);
1111 } else {
1112 if (link->close)
1113 link->close(link);
1114 }
David Brownell2b3d9422008-06-19 18:19:28 -07001115 spin_unlock(&dev->lock);
1116
1117 netif_carrier_on(dev->net);
1118 if (netif_running(dev->net))
1119 eth_start(dev, GFP_ATOMIC);
1120
1121 /* on error, disable any endpoints */
1122 } else {
1123 (void) usb_ep_disable(link->out_ep);
1124fail1:
1125 (void) usb_ep_disable(link->in_ep);
1126 }
1127fail0:
1128 /* caller is responsible for cleanup on error */
1129 if (result < 0)
1130 return ERR_PTR(result);
1131 return dev->net;
1132}
1133
1134/**
1135 * gether_disconnect - notify network layer that USB link is inactive
1136 * @link: the USB link, on which gether_connect() was called
1137 * Context: irqs blocked
1138 *
1139 * This is called to deactivate endpoints and let the network layer know
1140 * the connection went inactive ("no carrier").
1141 *
1142 * On return, the state is as if gether_connect() had never been called.
1143 * The endpoints are inactive, and accordingly without active USB I/O.
1144 * Pointers to endpoint descriptors and endpoint private data are nulled.
1145 */
1146void gether_disconnect(struct gether *link)
1147{
1148 struct eth_dev *dev = link->ioport;
1149 struct usb_request *req;
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001150 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001151
David Brownell2b3d9422008-06-19 18:19:28 -07001152 if (!dev)
1153 return;
1154
1155 DBG(dev, "%s\n", __func__);
1156
1157 netif_stop_queue(dev->net);
1158 netif_carrier_off(dev->net);
1159
1160 /* disable endpoints, forcing (synchronous) completion
1161 * of all pending i/o. then free the request objects
1162 * and forget about the endpoints.
1163 */
1164 usb_ep_disable(link->in_ep);
1165 spin_lock(&dev->req_lock);
1166 while (!list_empty(&dev->tx_reqs)) {
1167 req = container_of(dev->tx_reqs.next,
1168 struct usb_request, list);
1169 list_del(&req->list);
1170
1171 spin_unlock(&dev->req_lock);
Mayank Rana5b165cb2012-05-25 19:52:45 +05301172 if (link->multi_pkt_xfer)
1173 kfree(req->buf);
David Brownell2b3d9422008-06-19 18:19:28 -07001174 usb_ep_free_request(link->in_ep, req);
1175 spin_lock(&dev->req_lock);
1176 }
1177 spin_unlock(&dev->req_lock);
1178 link->in_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001179 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001180
1181 usb_ep_disable(link->out_ep);
1182 spin_lock(&dev->req_lock);
1183 while (!list_empty(&dev->rx_reqs)) {
1184 req = container_of(dev->rx_reqs.next,
1185 struct usb_request, list);
1186 list_del(&req->list);
1187
1188 spin_unlock(&dev->req_lock);
1189 usb_ep_free_request(link->out_ep, req);
1190 spin_lock(&dev->req_lock);
1191 }
1192 spin_unlock(&dev->req_lock);
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001193
1194 spin_lock(&dev->rx_frames.lock);
1195 while ((skb = __skb_dequeue(&dev->rx_frames)))
1196 dev_kfree_skb_any(skb);
1197 spin_unlock(&dev->rx_frames.lock);
1198
David Brownell2b3d9422008-06-19 18:19:28 -07001199 link->out_ep->driver_data = NULL;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001200 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001201
1202 /* finish forgetting about this USB link episode */
1203 dev->header_len = 0;
1204 dev->unwrap = NULL;
1205 dev->wrap = NULL;
1206
1207 spin_lock(&dev->lock);
1208 dev->port_usb = NULL;
1209 link->ioport = NULL;
1210 spin_unlock(&dev->lock);
1211}
Hemant Kumar08eb78e2012-07-30 20:38:47 -07001212
1213static int __init gether_init(void)
1214{
1215 uether_wq = create_singlethread_workqueue("uether");
1216 if (!uether_wq) {
1217 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1218 return -ENOMEM;
1219 }
1220 return 0;
1221}
1222module_init(gether_init);
1223
1224static void __exit gether_exit(void)
1225{
1226 destroy_workqueue(uether_wq);
1227
1228}
1229module_exit(gether_exit);
1230MODULE_DESCRIPTION("ethernet over USB driver");
1231MODULE_LICENSE("GPL v2");