blob: a4c8fbf3e8ed2d78ce8bc2f3fddeb52dc8119de4 [file] [log] [blame]
David Brownell2b3d9422008-06-19 18:19:28 -07001/*
2 * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
David Brownell2b3d9422008-06-19 18:19:28 -070012 */
13
14/* #define VERBOSE_DEBUG */
15
16#include <linux/kernel.h>
Sebastian Andrzej Siewior98303172012-09-10 16:30:50 +020017#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
David Brownell2b3d9422008-06-19 18:19:28 -070019#include <linux/device.h>
20#include <linux/ctype.h>
21#include <linux/etherdevice.h>
22#include <linux/ethtool.h>
Ian Coolidge4fe5f072012-11-07 14:39:18 +000023#include <linux/if_vlan.h>
David Brownell2b3d9422008-06-19 18:19:28 -070024
25#include "u_ether.h"
26
27
28/*
29 * This component encapsulates the Ethernet link glue needed to provide
30 * one (!) network link through the USB gadget stack, normally "usb0".
31 *
32 * The control and data models are handled by the function driver which
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050033 * connects to this code; such as CDC Ethernet (ECM or EEM),
34 * "CDC Subset", or RNDIS. That includes all descriptor and endpoint
35 * management.
David Brownell2b3d9422008-06-19 18:19:28 -070036 *
37 * Link level addressing is handled by this component using module
38 * parameters; if no such parameters are provided, random link level
39 * addresses are used. Each end of the link uses one address. The
40 * host end address is exported in various ways, and is often recorded
41 * in configuration databases.
42 *
43 * The driver which assembles each configuration using such a link is
44 * responsible for ensuring that each configuration includes at most one
45 * instance of is network link. (The network layer provides ways for
46 * this single "physical" link to be used by multiple virtual links.)
47 */
48
David Brownell8a1ce2c2008-08-18 17:43:56 -070049#define UETH__VERSION "29-May-2008"
David Brownell2b3d9422008-06-19 18:19:28 -070050
Mike Looijmansbba787a2015-08-05 08:54:55 +020051/* Experiments show that both Linux and Windows hosts allow up to 16k
52 * frame sizes. Set the max size to 15k+52 to prevent allocating 32k
53 * blocks and still have efficient handling. */
54#define GETHER_MAX_ETH_FRAME_LEN 15412
55
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -070056static struct workqueue_struct *uether_wq;
57
David Brownell2b3d9422008-06-19 18:19:28 -070058struct eth_dev {
59 /* lock is held while accessing port_usb
David Brownell2b3d9422008-06-19 18:19:28 -070060 */
61 spinlock_t lock;
62 struct gether *port_usb;
63
64 struct net_device *net;
65 struct usb_gadget *gadget;
66
67 spinlock_t req_lock; /* guard {rx,tx}_reqs */
68 struct list_head tx_reqs, rx_reqs;
Badhri Jagan Sridharan3c0f2882014-09-18 10:42:41 -070069 unsigned tx_qlen;
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -070070/* Minimum number of TX USB request queued to UDC */
71#define TX_REQ_THRESHOLD 5
72 int no_tx_req_used;
73 int tx_skb_hold_count;
74 u32 tx_req_bufsize;
David Brownell2b3d9422008-06-19 18:19:28 -070075
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050076 struct sk_buff_head rx_frames;
77
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020078 unsigned qmult;
79
David Brownell2b3d9422008-06-19 18:19:28 -070080 unsigned header_len;
xerox_lin72ffa182014-08-14 14:48:44 +080081 unsigned ul_max_pkts_per_xfer;
xerox_linf0539002014-09-04 16:01:59 +080082 unsigned dl_max_pkts_per_xfer;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -050083 struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
84 int (*unwrap)(struct gether *,
85 struct sk_buff *skb,
86 struct sk_buff_head *list);
David Brownell2b3d9422008-06-19 18:19:28 -070087
88 struct work_struct work;
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -070089 struct work_struct rx_work;
David Brownell2b3d9422008-06-19 18:19:28 -070090
91 unsigned long todo;
92#define WORK_RX_MEMORY 0
93
94 bool zlp;
95 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +020096 u8 dev_mac[ETH_ALEN];
David Brownell2b3d9422008-06-19 18:19:28 -070097};
98
99/*-------------------------------------------------------------------------*/
100
101#define RX_EXTRA 20 /* bytes guarding against rx overflows */
102
103#define DEFAULT_QLEN 2 /* double buffering by default */
104
Paul Zimmerman04617db2011-06-27 14:13:18 -0700105/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200106static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700107{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700108 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
109 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700110 return qmult * DEFAULT_QLEN;
111 else
112 return DEFAULT_QLEN;
113}
114
115/*-------------------------------------------------------------------------*/
116
117/* REVISIT there must be a better way than having two sets
118 * of debug calls ...
119 */
120
121#undef DBG
122#undef VDBG
123#undef ERROR
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{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100156 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
157 return -ERANGE;
158 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700159
Mike Looijmansab738ff2015-11-30 12:18:23 +0100160 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700161}
162
163static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
164{
Jiri Pirko7826d432013-01-06 00:44:26 +0000165 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700166
Jiri Pirko7826d432013-01-06 00:44:26 +0000167 strlcpy(p->driver, "g_ether", sizeof(p->driver));
168 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
169 strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
170 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
David Brownell2b3d9422008-06-19 18:19:28 -0700171}
172
David Brownell2b3d9422008-06-19 18:19:28 -0700173/* REVISIT can also support:
174 * - WOL (by tracking suspends and issuing remote wakeup)
175 * - msglevel (implies updated messaging)
176 * - ... probably more ethtool ops
177 */
178
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700179static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700180 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700181 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700182};
183
184static void defer_kevent(struct eth_dev *dev, int flag)
185{
186 if (test_and_set_bit(flag, &dev->todo))
187 return;
188 if (!schedule_work(&dev->work))
189 ERROR(dev, "kevent %d may have been dropped\n", flag);
190 else
191 DBG(dev, "kevent %d scheduled\n", flag);
192}
193
194static void rx_complete(struct usb_ep *ep, struct usb_request *req);
195
196static int
197rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
198{
199 struct sk_buff *skb;
200 int retval = -ENOMEM;
201 size_t size = 0;
202 struct usb_ep *out;
203 unsigned long flags;
204
205 spin_lock_irqsave(&dev->lock, flags);
206 if (dev->port_usb)
207 out = dev->port_usb->out_ep;
208 else
209 out = NULL;
210 spin_unlock_irqrestore(&dev->lock, flags);
211
212 if (!out)
213 return -ENOTCONN;
214
215
216 /* Padding up to RX_EXTRA handles minor disagreements with host.
217 * Normally we use the USB "terminate on short read" convention;
218 * so allow up to (N*maxpacket), since that memory is normally
219 * already allocated. Some hardware doesn't deal well with short
220 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
221 * byte off the end (to force hardware errors on overflow).
222 *
223 * RNDIS uses internal framing, and explicitly allows senders to
224 * pad to end-of-packet. That's potentially nice for speed, but
225 * means receivers can't recover lost synch on their own (because
226 * new packets don't only start after a short RX).
227 */
228 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
229 size += dev->port_usb->header_len;
230 size += out->maxpacket - 1;
231 size -= size % out->maxpacket;
232
xerox_lin72ffa182014-08-14 14:48:44 +0800233 if (dev->ul_max_pkts_per_xfer)
234 size *= dev->ul_max_pkts_per_xfer;
235
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200236 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800237 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200238
Amit Pundir246bfed2016-01-08 19:36:02 +0530239 DBG(dev, "%s: size: %zd\n", __func__, size);
David Brownell2b3d9422008-06-19 18:19:28 -0700240 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
241 if (skb == NULL) {
242 DBG(dev, "no rx skb\n");
243 goto enomem;
244 }
245
246 /* Some platforms perform better when IP packets are aligned,
247 * but on at least one, checksumming fails otherwise. Note:
248 * RNDIS headers involve variable numbers of LE32 values.
249 */
250 skb_reserve(skb, NET_IP_ALIGN);
251
252 req->buf = skb->data;
253 req->length = size;
254 req->complete = rx_complete;
255 req->context = skb;
256
257 retval = usb_ep_queue(out, req, gfp_flags);
258 if (retval == -ENOMEM)
259enomem:
260 defer_kevent(dev, WORK_RX_MEMORY);
261 if (retval) {
262 DBG(dev, "rx submit --> %d\n", retval);
263 if (skb)
264 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700265 }
266 return retval;
267}
268
269static void rx_complete(struct usb_ep *ep, struct usb_request *req)
270{
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700271 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700272 struct eth_dev *dev = ep->driver_data;
273 int status = req->status;
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700274 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700275
276 switch (status) {
277
278 /* normal completion */
279 case 0:
280 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500281
282 if (dev->unwrap) {
283 unsigned long flags;
284
285 spin_lock_irqsave(&dev->lock, flags);
286 if (dev->port_usb) {
287 status = dev->unwrap(dev->port_usb,
288 skb,
289 &dev->rx_frames);
Badhri Jagan Sridharanccf77072014-09-18 10:48:48 -0700290 if (status == -EINVAL)
291 dev->net->stats.rx_errors++;
292 else if (status == -EOVERFLOW)
293 dev->net->stats.rx_over_errors++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500294 } else {
295 dev_kfree_skb_any(skb);
296 status = -ENOTCONN;
297 }
298 spin_unlock_irqrestore(&dev->lock, flags);
299 } else {
300 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700301 }
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700302 if (!status)
303 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700304 break;
305
306 /* software-driven interface shutdown */
307 case -ECONNRESET: /* unlink */
308 case -ESHUTDOWN: /* disconnect etc */
309 VDBG(dev, "rx shutdown, code %d\n", status);
310 goto quiesce;
311
312 /* for hardware automagic (such as pxa) */
313 case -ECONNABORTED: /* endpoint reset */
314 DBG(dev, "rx %s reset\n", ep->name);
315 defer_kevent(dev, WORK_RX_MEMORY);
316quiesce:
317 dev_kfree_skb_any(skb);
318 goto clean;
319
320 /* data overrun */
321 case -EOVERFLOW:
322 dev->net->stats.rx_over_errors++;
323 /* FALLTHROUGH */
324
325 default:
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700326 queue = 1;
327 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700328 dev->net->stats.rx_errors++;
329 DBG(dev, "rx status %d\n", status);
330 break;
331 }
332
David Brownell2b3d9422008-06-19 18:19:28 -0700333clean:
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700334 spin_lock(&dev->req_lock);
335 list_add(&req->list, &dev->rx_reqs);
336 spin_unlock(&dev->req_lock);
337
338 if (queue)
339 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700340}
341
342static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
343{
344 unsigned i;
345 struct usb_request *req;
346
347 if (!n)
348 return -ENOMEM;
349
350 /* queue/recycle up to N requests */
351 i = n;
352 list_for_each_entry(req, list, list) {
353 if (i-- == 0)
354 goto extra;
355 }
356 while (i--) {
357 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
358 if (!req)
359 return list_empty(list) ? -ENOMEM : 0;
360 list_add(&req->list, list);
361 }
362 return 0;
363
364extra:
365 /* free extras */
366 for (;;) {
367 struct list_head *next;
368
369 next = req->list.next;
370 list_del(&req->list);
371 usb_ep_free_request(ep, req);
372
373 if (next == list)
374 break;
375
376 req = container_of(next, struct usb_request, list);
377 }
378 return 0;
379}
380
381static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
382{
383 int status;
384
385 spin_lock(&dev->req_lock);
386 status = prealloc(&dev->tx_reqs, link->in_ep, n);
387 if (status < 0)
388 goto fail;
389 status = prealloc(&dev->rx_reqs, link->out_ep, n);
390 if (status < 0)
391 goto fail;
392 goto done;
393fail:
394 DBG(dev, "can't alloc requests\n");
395done:
396 spin_unlock(&dev->req_lock);
397 return status;
398}
399
400static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
401{
402 struct usb_request *req;
403 unsigned long flags;
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700404 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700405
406 /* fill unused rxq slots with some skb */
407 spin_lock_irqsave(&dev->req_lock, flags);
408 while (!list_empty(&dev->rx_reqs)) {
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700409 /* break the nexus of continuous completion and re-submission*/
Praneeth Bajjuriaba84392015-01-22 16:38:56 -0600410 if (++req_cnt > qlen(dev->gadget, dev->qmult))
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700411 break;
412
David Brownell2b3d9422008-06-19 18:19:28 -0700413 req = container_of(dev->rx_reqs.next,
414 struct usb_request, list);
415 list_del_init(&req->list);
416 spin_unlock_irqrestore(&dev->req_lock, flags);
417
418 if (rx_submit(dev, req, gfp_flags) < 0) {
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700419 spin_lock_irqsave(&dev->req_lock, flags);
420 list_add(&req->list, &dev->rx_reqs);
421 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700422 defer_kevent(dev, WORK_RX_MEMORY);
423 return;
424 }
425
426 spin_lock_irqsave(&dev->req_lock, flags);
427 }
428 spin_unlock_irqrestore(&dev->req_lock, flags);
429}
430
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700431static void process_rx_w(struct work_struct *work)
432{
433 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
434 struct sk_buff *skb;
435 int status = 0;
436
437 if (!dev->port_usb)
438 return;
439
440 while ((skb = skb_dequeue(&dev->rx_frames))) {
441 if (status < 0
442 || ETH_HLEN > skb->len
443 || skb->len > ETH_FRAME_LEN) {
444 dev->net->stats.rx_errors++;
445 dev->net->stats.rx_length_errors++;
446 DBG(dev, "rx length %d\n", skb->len);
447 dev_kfree_skb_any(skb);
448 continue;
449 }
450 skb->protocol = eth_type_trans(skb, dev->net);
451 dev->net->stats.rx_packets++;
452 dev->net->stats.rx_bytes += skb->len;
453
454 status = netif_rx_ni(skb);
455 }
456
457 if (netif_running(dev->net))
458 rx_fill(dev, GFP_KERNEL);
459}
460
David Brownell2b3d9422008-06-19 18:19:28 -0700461static void eth_work(struct work_struct *work)
462{
463 struct eth_dev *dev = container_of(work, struct eth_dev, work);
464
465 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
466 if (netif_running(dev->net))
467 rx_fill(dev, GFP_KERNEL);
468 }
469
470 if (dev->todo)
471 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
472}
473
474static void tx_complete(struct usb_ep *ep, struct usb_request *req)
475{
476 struct sk_buff *skb = req->context;
477 struct eth_dev *dev = ep->driver_data;
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700478 struct net_device *net = dev->net;
479 struct usb_request *new_req;
480 struct usb_ep *in;
481 int length;
482 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700483
484 switch (req->status) {
485 default:
486 dev->net->stats.tx_errors++;
487 VDBG(dev, "tx err %d\n", req->status);
488 /* FALLTHROUGH */
489 case -ECONNRESET: /* unlink */
490 case -ESHUTDOWN: /* disconnect etc */
491 break;
492 case 0:
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700493 if (!req->zero)
494 dev->net->stats.tx_bytes += req->length-1;
495 else
496 dev->net->stats.tx_bytes += req->length;
David Brownell2b3d9422008-06-19 18:19:28 -0700497 }
498 dev->net->stats.tx_packets++;
499
500 spin_lock(&dev->req_lock);
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700501 list_add_tail(&req->list, &dev->tx_reqs);
502
503 if (dev->port_usb->multi_pkt_xfer) {
504 dev->no_tx_req_used--;
505 req->length = 0;
506 in = dev->port_usb->in_ep;
507
508 if (!list_empty(&dev->tx_reqs)) {
509 new_req = container_of(dev->tx_reqs.next,
510 struct usb_request, list);
511 list_del(&new_req->list);
512 spin_unlock(&dev->req_lock);
513 if (new_req->length > 0) {
514 length = new_req->length;
515
516 /* NCM requires no zlp if transfer is
517 * dwNtbInMaxSize */
518 if (dev->port_usb->is_fixed &&
519 length == dev->port_usb->fixed_in_len &&
520 (length % in->maxpacket) == 0)
521 new_req->zero = 0;
522 else
523 new_req->zero = 1;
524
525 /* use zlp framing on tx for strict CDC-Ether
526 * conformance, though any robust network rx
527 * path ignores extra padding. and some hardware
528 * doesn't like to write zlps.
529 */
530 if (new_req->zero && !dev->zlp &&
531 (length % in->maxpacket) == 0) {
532 new_req->zero = 0;
533 length++;
534 }
535
536 new_req->length = length;
537 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
538 switch (retval) {
539 default:
540 DBG(dev, "tx queue err %d\n", retval);
541 break;
542 case 0:
543 spin_lock(&dev->req_lock);
544 dev->no_tx_req_used++;
545 spin_unlock(&dev->req_lock);
Amit Pundirc7b5efb2016-05-30 15:19:21 +0530546 netif_trans_update(net);
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700547 }
548 } else {
549 spin_lock(&dev->req_lock);
550 list_add(&new_req->list, &dev->tx_reqs);
551 spin_unlock(&dev->req_lock);
552 }
553 } else {
554 spin_unlock(&dev->req_lock);
555 }
556 } else {
557 spin_unlock(&dev->req_lock);
558 dev_kfree_skb_any(skb);
559 }
David Brownell2b3d9422008-06-19 18:19:28 -0700560
David Brownell2b3d9422008-06-19 18:19:28 -0700561 if (netif_carrier_ok(dev->net))
562 netif_wake_queue(dev->net);
563}
564
565static inline int is_promisc(u16 cdc_filter)
566{
567 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
568}
569
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700570static void alloc_tx_buffer(struct eth_dev *dev)
571{
572 struct list_head *act;
573 struct usb_request *req;
574
xerox_linf0539002014-09-04 16:01:59 +0800575 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700576 (dev->net->mtu
577 + sizeof(struct ethhdr)
578 /* size of rndis_packet_msg_type */
579 + 44
580 + 22));
581
582 list_for_each(act, &dev->tx_reqs) {
583 req = container_of(act, struct usb_request, list);
584 if (!req->buf)
585 req->buf = kmalloc(dev->tx_req_bufsize,
586 GFP_ATOMIC);
587 }
588}
589
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000590static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
591 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700592{
593 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100594 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700595 int retval;
596 struct usb_request *req = NULL;
597 unsigned long flags;
598 struct usb_ep *in;
599 u16 cdc_filter;
600
601 spin_lock_irqsave(&dev->lock, flags);
602 if (dev->port_usb) {
603 in = dev->port_usb->in_ep;
604 cdc_filter = dev->port_usb->cdc_filter;
605 } else {
606 in = NULL;
607 cdc_filter = 0;
608 }
609 spin_unlock_irqrestore(&dev->lock, flags);
610
Jim Baxter6d3865f2014-07-07 18:33:18 +0100611 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700612 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000613 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700614 }
615
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700616 /* Allocate memory for tx_reqs to support multi packet transfer */
617 if (dev->port_usb->multi_pkt_xfer && !dev->tx_req_bufsize)
618 alloc_tx_buffer(dev);
619
David Brownell2b3d9422008-06-19 18:19:28 -0700620 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100621 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700622 u8 *dest = skb->data;
623
624 if (is_multicast_ether_addr(dest)) {
625 u16 type;
626
627 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
628 * SET_ETHERNET_MULTICAST_FILTERS requests
629 */
630 if (is_broadcast_ether_addr(dest))
631 type = USB_CDC_PACKET_TYPE_BROADCAST;
632 else
633 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
634 if (!(cdc_filter & type)) {
635 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000636 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700637 }
638 }
639 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
640 }
641
642 spin_lock_irqsave(&dev->req_lock, flags);
643 /*
644 * this freelist can be empty if an interrupt triggered disconnect()
645 * and reconfigured the gadget (shutting down this queue) after the
646 * network stack decided to xmit but before we got the spinlock.
647 */
648 if (list_empty(&dev->tx_reqs)) {
649 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000650 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700651 }
652
653 req = container_of(dev->tx_reqs.next, struct usb_request, list);
654 list_del(&req->list);
655
656 /* temporarily stop TX queue when the freelist empties */
657 if (list_empty(&dev->tx_reqs))
658 netif_stop_queue(net);
659 spin_unlock_irqrestore(&dev->req_lock, flags);
660
661 /* no buffer copies needed, unless the network stack did it
662 * or the hardware can't use skb buffers.
663 * or there's not enough space for extra headers we need
664 */
665 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500666 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700667
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500668 spin_lock_irqsave(&dev->lock, flags);
669 if (dev->port_usb)
670 skb = dev->wrap(dev->port_usb, skb);
671 spin_unlock_irqrestore(&dev->lock, flags);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100672 if (!skb) {
673 /* Multi frame CDC protocols may store the frame for
674 * later which is not a dropped frame.
675 */
Peter Chen88c09ea2016-07-01 15:33:29 +0800676 if (dev->port_usb &&
677 dev->port_usb->supports_multi_frame)
Jim Baxter6d3865f2014-07-07 18:33:18 +0100678 goto multiframe;
David Brownell2b3d9422008-06-19 18:19:28 -0700679 goto drop;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100680 }
David Brownell2b3d9422008-06-19 18:19:28 -0700681 }
Jim Baxter6d3865f2014-07-07 18:33:18 +0100682
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700683 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);
xerox_linf0539002014-09-04 16:01:59 +0800694 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700695 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 */
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700728 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
729 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700730 length++;
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700731 }
David Brownell2b3d9422008-06-19 18:19:28 -0700732
733 req->length = length;
734
Badhri Jagan Sridharan3c0f2882014-09-18 10:42:41 -0700735 /* throttle highspeed IRQ rate back slightly */
736 if (gadget_is_dualspeed(dev->gadget) &&
737 (dev->gadget->speed == USB_SPEED_HIGH)) {
738 dev->tx_qlen++;
Praneeth Bajjuriaba84392015-01-22 16:38:56 -0600739 if (dev->tx_qlen == (dev->qmult/2)) {
Badhri Jagan Sridharan3c0f2882014-09-18 10:42:41 -0700740 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:
Florian Westphal860e9532016-05-03 16:33:13 +0200755 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700756 }
757
758 if (retval) {
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700759 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++;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100763multiframe:
David Brownell2b3d9422008-06-19 18:19:28 -0700764 spin_lock_irqsave(&dev->req_lock, flags);
765 if (list_empty(&dev->tx_reqs))
766 netif_start_queue(net);
767 list_add(&req->list, &dev->tx_reqs);
768 spin_unlock_irqrestore(&dev->req_lock, flags);
769 }
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -0700770success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000771 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700772}
773
774/*-------------------------------------------------------------------------*/
775
776static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
777{
778 DBG(dev, "%s\n", __func__);
779
780 /* fill the rx queue */
781 rx_fill(dev, gfp_flags);
782
783 /* and open the tx floodgates */
Badhri Jagan Sridharan3c0f2882014-09-18 10:42:41 -0700784 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700785 netif_wake_queue(dev->net);
786}
787
788static int eth_open(struct net_device *net)
789{
790 struct eth_dev *dev = netdev_priv(net);
791 struct gether *link;
792
793 DBG(dev, "%s\n", __func__);
794 if (netif_carrier_ok(dev->net))
795 eth_start(dev, GFP_KERNEL);
796
797 spin_lock_irq(&dev->lock);
798 link = dev->port_usb;
799 if (link && link->open)
800 link->open(link);
801 spin_unlock_irq(&dev->lock);
802
803 return 0;
804}
805
806static int eth_stop(struct net_device *net)
807{
808 struct eth_dev *dev = netdev_priv(net);
809 unsigned long flags;
810
811 VDBG(dev, "%s\n", __func__);
812 netif_stop_queue(net);
813
814 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
815 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
816 dev->net->stats.rx_errors, dev->net->stats.tx_errors
817 );
818
819 /* ensure there are no more active requests */
820 spin_lock_irqsave(&dev->lock, flags);
821 if (dev->port_usb) {
822 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200823 const struct usb_endpoint_descriptor *in;
824 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700825
826 if (link->close)
827 link->close(link);
828
829 /* NOTE: we have no abort-queue primitive we could use
830 * to cancel all pending I/O. Instead, we disable then
831 * reenable the endpoints ... this idiom may leave toggle
832 * wrong, but that's a self-correcting error.
833 *
834 * REVISIT: we *COULD* just let the transfers complete at
835 * their own pace; the network stack can handle old packets.
836 * For the moment we leave this here, since it works.
837 */
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200838 in = link->in_ep->desc;
839 out = link->out_ep->desc;
David Brownell2b3d9422008-06-19 18:19:28 -0700840 usb_ep_disable(link->in_ep);
841 usb_ep_disable(link->out_ep);
842 if (netif_carrier_ok(net)) {
843 DBG(dev, "host still using in/out endpoints\n");
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200844 link->in_ep->desc = in;
845 link->out_ep->desc = out;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300846 usb_ep_enable(link->in_ep);
847 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700848 }
849 }
850 spin_unlock_irqrestore(&dev->lock, flags);
851
852 return 0;
853}
854
855/*-------------------------------------------------------------------------*/
856
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200857static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700858{
859 if (str) {
860 unsigned i;
861
862 for (i = 0; i < 6; i++) {
863 unsigned char num;
864
865 if ((*str == '.') || (*str == ':'))
866 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300867 num = hex_to_bin(*str++) << 4;
868 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700869 dev_addr [i] = num;
870 }
871 if (is_valid_ether_addr(dev_addr))
872 return 0;
873 }
Joe Perches006c9132012-07-12 22:33:11 -0700874 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700875 return 1;
876}
877
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200878static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
879{
880 if (len < 18)
881 return -EINVAL;
882
Andy Shevchenko27f38702015-01-15 13:40:04 +0200883 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200884 return 18;
885}
886
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800887static const struct net_device_ops eth_netdev_ops = {
888 .ndo_open = eth_open,
889 .ndo_stop = eth_stop,
890 .ndo_start_xmit = eth_start_xmit,
891 .ndo_change_mtu = ueth_change_mtu,
892 .ndo_set_mac_address = eth_mac_addr,
893 .ndo_validate_addr = eth_validate_addr,
894};
David Brownell2b3d9422008-06-19 18:19:28 -0700895
Marcel Holtmannaa790742010-01-15 22:13:58 -0800896static struct device_type gadget_type = {
897 .name = "gadget",
898};
899
David Brownell2b3d9422008-06-19 18:19:28 -0700900/**
Mike Lockwood036e98b2012-05-10 10:08:02 +0200901 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -0700902 * @g: gadget to associated with these links
903 * @ethaddr: NULL, or a buffer in which the ethernet address of the
904 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +0200905 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -0700906 * Context: may sleep
907 *
908 * This sets up the single network link that may be exported by a
909 * gadget driver using this framework. The link layer addresses are
910 * set up using module parameters.
911 *
Dan Carpenter574f24f2013-11-14 11:42:11 +0300912 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -0700913 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200914struct eth_dev *gether_setup_name(struct usb_gadget *g,
915 const char *dev_addr, const char *host_addr,
916 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -0700917{
918 struct eth_dev *dev;
919 struct net_device *net;
920 int status;
921
David Brownell2b3d9422008-06-19 18:19:28 -0700922 net = alloc_etherdev(sizeof *dev);
923 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100924 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -0700925
926 dev = netdev_priv(net);
927 spin_lock_init(&dev->lock);
928 spin_lock_init(&dev->req_lock);
929 INIT_WORK(&dev->work, eth_work);
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -0700930 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700931 INIT_LIST_HEAD(&dev->tx_reqs);
932 INIT_LIST_HEAD(&dev->rx_reqs);
933
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500934 skb_queue_head_init(&dev->rx_frames);
935
David Brownell2b3d9422008-06-19 18:19:28 -0700936 /* network device setup */
937 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200938 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +0200939 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700940
941 if (get_ether_addr(dev_addr, net->dev_addr))
942 dev_warn(&g->dev,
943 "using random %s ethernet address\n", "self");
944 if (get_ether_addr(host_addr, dev->host_mac))
945 dev_warn(&g->dev,
946 "using random %s ethernet address\n", "host");
947
948 if (ethaddr)
949 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
950
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800951 net->netdev_ops = &eth_netdev_ops;
952
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000953 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -0700954
David Brownell2b3d9422008-06-19 18:19:28 -0700955 dev->gadget = g;
956 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800957 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700958
959 status = register_netdev(net);
960 if (status < 0) {
961 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
962 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100963 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -0700964 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700965 INFO(dev, "MAC %pM\n", net->dev_addr);
966 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -0700967
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200968 /*
969 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -0700970 * - iff DATA transfer is active, carrier is "on"
971 * - tx queueing enabled if open *and* carrier is "on"
972 */
973 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700974 }
975
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100976 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -0700977}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500978EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -0700979
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200980struct net_device *gether_setup_name_default(const char *netname)
981{
982 struct net_device *net;
983 struct eth_dev *dev;
984
985 net = alloc_etherdev(sizeof(*dev));
986 if (!net)
987 return ERR_PTR(-ENOMEM);
988
989 dev = netdev_priv(net);
990 spin_lock_init(&dev->lock);
991 spin_lock_init(&dev->req_lock);
992 INIT_WORK(&dev->work, eth_work);
Matthew Moeller8083c7c2016-03-09 20:19:25 -0600993 INIT_WORK(&dev->rx_work, process_rx_w);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200994 INIT_LIST_HEAD(&dev->tx_reqs);
995 INIT_LIST_HEAD(&dev->rx_reqs);
996
997 skb_queue_head_init(&dev->rx_frames);
998
999 /* network device setup */
1000 dev->net = net;
1001 dev->qmult = QMULT_DEFAULT;
1002 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
1003
1004 eth_random_addr(dev->dev_mac);
1005 pr_warn("using random %s ethernet address\n", "self");
1006 eth_random_addr(dev->host_mac);
1007 pr_warn("using random %s ethernet address\n", "host");
1008
1009 net->netdev_ops = &eth_netdev_ops;
1010
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001011 net->ethtool_ops = &ops;
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001012 SET_NETDEV_DEVTYPE(net, &gadget_type);
1013
1014 return net;
1015}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001016EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001017
1018int gether_register_netdev(struct net_device *net)
1019{
1020 struct eth_dev *dev;
1021 struct usb_gadget *g;
1022 struct sockaddr sa;
1023 int status;
1024
1025 if (!net->dev.parent)
1026 return -EINVAL;
1027 dev = netdev_priv(net);
1028 g = dev->gadget;
1029 status = register_netdev(net);
1030 if (status < 0) {
1031 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1032 return status;
1033 } else {
1034 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
1035
1036 /* two kinds of host-initiated state changes:
1037 * - iff DATA transfer is active, carrier is "on"
1038 * - tx queueing enabled if open *and* carrier is "on"
1039 */
1040 netif_carrier_off(net);
1041 }
1042 sa.sa_family = net->type;
1043 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
1044 rtnl_lock();
1045 status = dev_set_mac_address(net, &sa);
1046 rtnl_unlock();
1047 if (status)
1048 pr_warn("cannot set self ethernet address: %d\n", status);
1049 else
1050 INFO(dev, "MAC %pM\n", dev->dev_mac);
1051
1052 return status;
1053}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001054EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001055
1056void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
1057{
1058 struct eth_dev *dev;
1059
1060 dev = netdev_priv(net);
1061 dev->gadget = g;
1062 SET_NETDEV_DEV(net, &g->dev);
1063}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001064EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001065
1066int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
1067{
1068 struct eth_dev *dev;
1069 u8 new_addr[ETH_ALEN];
1070
1071 dev = netdev_priv(net);
1072 if (get_ether_addr(dev_addr, new_addr))
1073 return -EINVAL;
1074 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
1075 return 0;
1076}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001077EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001078
1079int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
1080{
1081 struct eth_dev *dev;
1082
1083 dev = netdev_priv(net);
1084 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
1085}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001086EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001087
1088int gether_set_host_addr(struct net_device *net, const char *host_addr)
1089{
1090 struct eth_dev *dev;
1091 u8 new_addr[ETH_ALEN];
1092
1093 dev = netdev_priv(net);
1094 if (get_ether_addr(host_addr, new_addr))
1095 return -EINVAL;
1096 memcpy(dev->host_mac, new_addr, ETH_ALEN);
1097 return 0;
1098}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001099EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001100
1101int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
1102{
1103 struct eth_dev *dev;
1104
1105 dev = netdev_priv(net);
1106 return get_ether_addr_str(dev->host_mac, host_addr, len);
1107}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001108EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001109
1110int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
1111{
1112 struct eth_dev *dev;
1113
1114 if (len < 13)
1115 return -EINVAL;
1116
1117 dev = netdev_priv(net);
1118 snprintf(host_addr, len, "%pm", dev->host_mac);
1119
1120 return strlen(host_addr);
1121}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001122EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001123
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001124void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
1125{
1126 struct eth_dev *dev;
1127
1128 dev = netdev_priv(net);
1129 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1130}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001131EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001132
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001133void gether_set_qmult(struct net_device *net, unsigned qmult)
1134{
1135 struct eth_dev *dev;
1136
1137 dev = netdev_priv(net);
1138 dev->qmult = qmult;
1139}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001140EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001141
1142unsigned gether_get_qmult(struct net_device *net)
1143{
1144 struct eth_dev *dev;
1145
1146 dev = netdev_priv(net);
1147 return dev->qmult;
1148}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001149EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001150
1151int gether_get_ifname(struct net_device *net, char *name, int len)
1152{
1153 rtnl_lock();
1154 strlcpy(name, netdev_name(net), len);
1155 rtnl_unlock();
1156 return strlen(name);
1157}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001158EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001159
David Brownell2b3d9422008-06-19 18:19:28 -07001160/**
1161 * gether_cleanup - remove Ethernet-over-USB device
1162 * Context: may sleep
1163 *
1164 * This is called to free all resources allocated by @gether_setup().
1165 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001166void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001167{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001168 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001169 return;
1170
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001171 unregister_netdev(dev->net);
1172 flush_work(&dev->work);
1173 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001174}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001175EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001176
David Brownell2b3d9422008-06-19 18:19:28 -07001177/**
1178 * gether_connect - notify network layer that USB link is active
1179 * @link: the USB link, set up with endpoints, descriptors matching
1180 * current device speed, and any framing wrapper(s) set up.
1181 * Context: irqs blocked
1182 *
1183 * This is called to activate endpoints and let the network layer know
1184 * the connection is active ("carrier detect"). It may cause the I/O
1185 * queues to open and start letting network packets flow, but will in
1186 * any case activate the endpoints so that they respond properly to the
1187 * USB host.
1188 *
1189 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1190 * indicate some error code (negative errno), ep->driver_data values
1191 * have been overwritten.
1192 */
1193struct net_device *gether_connect(struct gether *link)
1194{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001195 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001196 int result = 0;
1197
1198 if (!dev)
1199 return ERR_PTR(-EINVAL);
1200
1201 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001202 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001203 if (result != 0) {
1204 DBG(dev, "enable %s --> %d\n",
1205 link->in_ep->name, result);
1206 goto fail0;
1207 }
1208
1209 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001210 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001211 if (result != 0) {
1212 DBG(dev, "enable %s --> %d\n",
1213 link->out_ep->name, result);
1214 goto fail1;
1215 }
1216
1217 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001218 result = alloc_requests(dev, link, qlen(dev->gadget,
1219 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001220
1221 if (result == 0) {
1222 dev->zlp = link->is_zlp_ok;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001223 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001224
1225 dev->header_len = link->header_len;
1226 dev->unwrap = link->unwrap;
1227 dev->wrap = link->wrap;
xerox_lin72ffa182014-08-14 14:48:44 +08001228 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
xerox_linf0539002014-09-04 16:01:59 +08001229 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001230
1231 spin_lock(&dev->lock);
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -07001232 dev->tx_skb_hold_count = 0;
1233 dev->no_tx_req_used = 0;
1234 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001235 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001236 if (netif_running(dev->net)) {
1237 if (link->open)
1238 link->open(link);
1239 } else {
1240 if (link->close)
1241 link->close(link);
1242 }
David Brownell2b3d9422008-06-19 18:19:28 -07001243 spin_unlock(&dev->lock);
1244
1245 netif_carrier_on(dev->net);
1246 if (netif_running(dev->net))
1247 eth_start(dev, GFP_ATOMIC);
1248
1249 /* on error, disable any endpoints */
1250 } else {
1251 (void) usb_ep_disable(link->out_ep);
1252fail1:
1253 (void) usb_ep_disable(link->in_ep);
1254 }
1255fail0:
1256 /* caller is responsible for cleanup on error */
1257 if (result < 0)
1258 return ERR_PTR(result);
1259 return dev->net;
1260}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001261EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001262
1263/**
1264 * gether_disconnect - notify network layer that USB link is inactive
1265 * @link: the USB link, on which gether_connect() was called
1266 * Context: irqs blocked
1267 *
1268 * This is called to deactivate endpoints and let the network layer know
1269 * the connection went inactive ("no carrier").
1270 *
1271 * On return, the state is as if gether_connect() had never been called.
1272 * The endpoints are inactive, and accordingly without active USB I/O.
1273 * Pointers to endpoint descriptors and endpoint private data are nulled.
1274 */
1275void gether_disconnect(struct gether *link)
1276{
1277 struct eth_dev *dev = link->ioport;
1278 struct usb_request *req;
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -07001279 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001280
1281 WARN_ON(!dev);
1282 if (!dev)
1283 return;
1284
1285 DBG(dev, "%s\n", __func__);
1286
1287 netif_stop_queue(dev->net);
1288 netif_carrier_off(dev->net);
1289
1290 /* disable endpoints, forcing (synchronous) completion
1291 * of all pending i/o. then free the request objects
1292 * and forget about the endpoints.
1293 */
1294 usb_ep_disable(link->in_ep);
1295 spin_lock(&dev->req_lock);
1296 while (!list_empty(&dev->tx_reqs)) {
1297 req = container_of(dev->tx_reqs.next,
1298 struct usb_request, list);
1299 list_del(&req->list);
1300
1301 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharanb7a343a2014-09-18 10:46:08 -07001302 if (link->multi_pkt_xfer)
1303 kfree(req->buf);
David Brownell2b3d9422008-06-19 18:19:28 -07001304 usb_ep_free_request(link->in_ep, req);
1305 spin_lock(&dev->req_lock);
1306 }
1307 spin_unlock(&dev->req_lock);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001308 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001309
1310 usb_ep_disable(link->out_ep);
1311 spin_lock(&dev->req_lock);
1312 while (!list_empty(&dev->rx_reqs)) {
1313 req = container_of(dev->rx_reqs.next,
1314 struct usb_request, list);
1315 list_del(&req->list);
1316
1317 spin_unlock(&dev->req_lock);
1318 usb_ep_free_request(link->out_ep, req);
1319 spin_lock(&dev->req_lock);
1320 }
1321 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -07001322
1323 spin_lock(&dev->rx_frames.lock);
1324 while ((skb = __skb_dequeue(&dev->rx_frames)))
1325 dev_kfree_skb_any(skb);
1326 spin_unlock(&dev->rx_frames.lock);
1327
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001328 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001329
1330 /* finish forgetting about this USB link episode */
1331 dev->header_len = 0;
1332 dev->unwrap = NULL;
1333 dev->wrap = NULL;
1334
1335 spin_lock(&dev->lock);
1336 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001337 spin_unlock(&dev->lock);
1338}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001339EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001340
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -07001341static int __init gether_init(void)
1342{
1343 uether_wq = create_singlethread_workqueue("uether");
1344 if (!uether_wq) {
1345 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1346 return -ENOMEM;
1347 }
1348 return 0;
1349}
1350module_init(gether_init);
1351
1352static void __exit gether_exit(void)
1353{
1354 destroy_workqueue(uether_wq);
1355
1356}
1357module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001358MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharan16a203f2014-09-24 18:58:23 -07001359MODULE_DESCRIPTION("ethernet over USB driver");
1360MODULE_LICENSE("GPL v2");