blob: b06ede419bbe3b115de2433aeb1a5495823f8972 [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 Sridharana096ba52014-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 Sridharancb6a7862014-09-18 10:42:41 -070069 unsigned tx_qlen;
Badhri Jagan Sridharane791ad32014-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_lin87bebf82014-08-14 14:48:44 +080081 unsigned ul_max_pkts_per_xfer;
xerox_lincdffcb82014-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 Sridharana096ba52014-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;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +090095 bool no_skb_reserve;
David Brownell2b3d9422008-06-19 18:19:28 -070096 u8 host_mac[ETH_ALEN];
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +020097 u8 dev_mac[ETH_ALEN];
David Brownell2b3d9422008-06-19 18:19:28 -070098};
99
100/*-------------------------------------------------------------------------*/
101
102#define RX_EXTRA 20 /* bytes guarding against rx overflows */
103
104#define DEFAULT_QLEN 2 /* double buffering by default */
105
Paul Zimmerman04617db2011-06-27 14:13:18 -0700106/* for dual-speed hardware, use deeper queues at high/super speed */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200107static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
David Brownell2b3d9422008-06-19 18:19:28 -0700108{
Paul Zimmerman04617db2011-06-27 14:13:18 -0700109 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
110 gadget->speed == USB_SPEED_SUPER))
David Brownell2b3d9422008-06-19 18:19:28 -0700111 return qmult * DEFAULT_QLEN;
112 else
113 return DEFAULT_QLEN;
114}
115
116/*-------------------------------------------------------------------------*/
117
118/* REVISIT there must be a better way than having two sets
119 * of debug calls ...
120 */
121
122#undef DBG
123#undef VDBG
124#undef ERROR
David Brownell2b3d9422008-06-19 18:19:28 -0700125#undef INFO
126
127#define xprintk(d, level, fmt, args...) \
128 printk(level "%s: " fmt , (d)->net->name , ## args)
129
130#ifdef DEBUG
131#undef DEBUG
132#define DBG(dev, fmt, args...) \
133 xprintk(dev , KERN_DEBUG , fmt , ## args)
134#else
135#define DBG(dev, fmt, args...) \
136 do { } while (0)
137#endif /* DEBUG */
138
139#ifdef VERBOSE_DEBUG
140#define VDBG DBG
141#else
142#define VDBG(dev, fmt, args...) \
143 do { } while (0)
144#endif /* DEBUG */
145
146#define ERROR(dev, fmt, args...) \
147 xprintk(dev , KERN_ERR , fmt , ## args)
David Brownell2b3d9422008-06-19 18:19:28 -0700148#define INFO(dev, fmt, args...) \
149 xprintk(dev , KERN_INFO , fmt , ## args)
150
151/*-------------------------------------------------------------------------*/
152
153/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
154
Stephen Hemmingerccad6372008-11-19 22:42:31 -0800155static int ueth_change_mtu(struct net_device *net, int new_mtu)
David Brownell2b3d9422008-06-19 18:19:28 -0700156{
Mike Looijmansab738ff2015-11-30 12:18:23 +0100157 if (new_mtu <= ETH_HLEN || new_mtu > GETHER_MAX_ETH_FRAME_LEN)
158 return -ERANGE;
159 net->mtu = new_mtu;
David Brownell2b3d9422008-06-19 18:19:28 -0700160
Mike Looijmansab738ff2015-11-30 12:18:23 +0100161 return 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700162}
163
164static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
165{
Jiri Pirko7826d432013-01-06 00:44:26 +0000166 struct eth_dev *dev = netdev_priv(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700167
Jiri Pirko7826d432013-01-06 00:44:26 +0000168 strlcpy(p->driver, "g_ether", sizeof(p->driver));
169 strlcpy(p->version, UETH__VERSION, sizeof(p->version));
170 strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
171 strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
David Brownell2b3d9422008-06-19 18:19:28 -0700172}
173
David Brownell2b3d9422008-06-19 18:19:28 -0700174/* REVISIT can also support:
175 * - WOL (by tracking suspends and issuing remote wakeup)
176 * - msglevel (implies updated messaging)
177 * - ... probably more ethtool ops
178 */
179
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700180static const struct ethtool_ops ops = {
David Brownell2b3d9422008-06-19 18:19:28 -0700181 .get_drvinfo = eth_get_drvinfo,
Jonathan McDowell237e75b2009-03-26 00:45:27 -0700182 .get_link = ethtool_op_get_link,
David Brownell2b3d9422008-06-19 18:19:28 -0700183};
184
185static void defer_kevent(struct eth_dev *dev, int flag)
186{
187 if (test_and_set_bit(flag, &dev->todo))
188 return;
189 if (!schedule_work(&dev->work))
190 ERROR(dev, "kevent %d may have been dropped\n", flag);
191 else
192 DBG(dev, "kevent %d scheduled\n", flag);
193}
194
195static void rx_complete(struct usb_ep *ep, struct usb_request *req);
196
197static int
198rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
199{
200 struct sk_buff *skb;
201 int retval = -ENOMEM;
202 size_t size = 0;
203 struct usb_ep *out;
204 unsigned long flags;
205
206 spin_lock_irqsave(&dev->lock, flags);
207 if (dev->port_usb)
208 out = dev->port_usb->out_ep;
209 else
210 out = NULL;
211 spin_unlock_irqrestore(&dev->lock, flags);
212
213 if (!out)
214 return -ENOTCONN;
215
216
217 /* Padding up to RX_EXTRA handles minor disagreements with host.
218 * Normally we use the USB "terminate on short read" convention;
219 * so allow up to (N*maxpacket), since that memory is normally
220 * already allocated. Some hardware doesn't deal well with short
221 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
222 * byte off the end (to force hardware errors on overflow).
223 *
224 * RNDIS uses internal framing, and explicitly allows senders to
225 * pad to end-of-packet. That's potentially nice for speed, but
226 * means receivers can't recover lost synch on their own (because
227 * new packets don't only start after a short RX).
228 */
229 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
230 size += dev->port_usb->header_len;
231 size += out->maxpacket - 1;
232 size -= size % out->maxpacket;
233
xerox_lin87bebf82014-08-14 14:48:44 +0800234 if (dev->ul_max_pkts_per_xfer)
235 size *= dev->ul_max_pkts_per_xfer;
236
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200237 if (dev->port_usb->is_fixed)
Stephen Hemminger45d1b7a2011-03-01 22:40:57 -0800238 size = max_t(size_t, size, dev->port_usb->fixed_out_len);
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200239
Amit Pundir5ff0eb22016-01-08 19:36:02 +0530240 DBG(dev, "%s: size: %zd\n", __func__, size);
David Brownell2b3d9422008-06-19 18:19:28 -0700241 skb = alloc_skb(size + NET_IP_ALIGN, gfp_flags);
242 if (skb == NULL) {
243 DBG(dev, "no rx skb\n");
244 goto enomem;
245 }
246
247 /* Some platforms perform better when IP packets are aligned,
248 * but on at least one, checksumming fails otherwise. Note:
249 * RNDIS headers involve variable numbers of LE32 values.
250 */
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +0900251 if (likely(!dev->no_skb_reserve))
252 skb_reserve(skb, NET_IP_ALIGN);
David Brownell2b3d9422008-06-19 18:19:28 -0700253
254 req->buf = skb->data;
255 req->length = size;
256 req->complete = rx_complete;
257 req->context = skb;
258
259 retval = usb_ep_queue(out, req, gfp_flags);
260 if (retval == -ENOMEM)
261enomem:
262 defer_kevent(dev, WORK_RX_MEMORY);
263 if (retval) {
264 DBG(dev, "rx submit --> %d\n", retval);
265 if (skb)
266 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700267 }
268 return retval;
269}
270
271static void rx_complete(struct usb_ep *ep, struct usb_request *req)
272{
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700273 struct sk_buff *skb = req->context;
David Brownell2b3d9422008-06-19 18:19:28 -0700274 struct eth_dev *dev = ep->driver_data;
275 int status = req->status;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700276 bool queue = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700277
278 switch (status) {
279
280 /* normal completion */
281 case 0:
282 skb_put(skb, req->actual);
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500283
284 if (dev->unwrap) {
285 unsigned long flags;
286
287 spin_lock_irqsave(&dev->lock, flags);
288 if (dev->port_usb) {
289 status = dev->unwrap(dev->port_usb,
290 skb,
291 &dev->rx_frames);
Badhri Jagan Sridharan8424b3e2014-09-18 10:48:48 -0700292 if (status == -EINVAL)
293 dev->net->stats.rx_errors++;
294 else if (status == -EOVERFLOW)
295 dev->net->stats.rx_over_errors++;
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500296 } else {
297 dev_kfree_skb_any(skb);
298 status = -ENOTCONN;
299 }
300 spin_unlock_irqrestore(&dev->lock, flags);
301 } else {
302 skb_queue_tail(&dev->rx_frames, skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700303 }
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700304 if (!status)
305 queue = 1;
David Brownell2b3d9422008-06-19 18:19:28 -0700306 break;
307
308 /* software-driven interface shutdown */
309 case -ECONNRESET: /* unlink */
310 case -ESHUTDOWN: /* disconnect etc */
311 VDBG(dev, "rx shutdown, code %d\n", status);
312 goto quiesce;
313
314 /* for hardware automagic (such as pxa) */
315 case -ECONNABORTED: /* endpoint reset */
316 DBG(dev, "rx %s reset\n", ep->name);
317 defer_kevent(dev, WORK_RX_MEMORY);
318quiesce:
319 dev_kfree_skb_any(skb);
320 goto clean;
321
322 /* data overrun */
323 case -EOVERFLOW:
324 dev->net->stats.rx_over_errors++;
325 /* FALLTHROUGH */
326
327 default:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700328 queue = 1;
329 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700330 dev->net->stats.rx_errors++;
331 DBG(dev, "rx status %d\n", status);
332 break;
333 }
334
David Brownell2b3d9422008-06-19 18:19:28 -0700335clean:
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700336 spin_lock(&dev->req_lock);
337 list_add(&req->list, &dev->rx_reqs);
338 spin_unlock(&dev->req_lock);
339
340 if (queue)
341 queue_work(uether_wq, &dev->rx_work);
David Brownell2b3d9422008-06-19 18:19:28 -0700342}
343
344static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
345{
346 unsigned i;
347 struct usb_request *req;
348
349 if (!n)
350 return -ENOMEM;
351
352 /* queue/recycle up to N requests */
353 i = n;
354 list_for_each_entry(req, list, list) {
355 if (i-- == 0)
356 goto extra;
357 }
358 while (i--) {
359 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
360 if (!req)
361 return list_empty(list) ? -ENOMEM : 0;
362 list_add(&req->list, list);
363 }
364 return 0;
365
366extra:
367 /* free extras */
368 for (;;) {
369 struct list_head *next;
370
371 next = req->list.next;
372 list_del(&req->list);
373 usb_ep_free_request(ep, req);
374
375 if (next == list)
376 break;
377
378 req = container_of(next, struct usb_request, list);
379 }
380 return 0;
381}
382
383static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
384{
385 int status;
386
387 spin_lock(&dev->req_lock);
388 status = prealloc(&dev->tx_reqs, link->in_ep, n);
389 if (status < 0)
390 goto fail;
391 status = prealloc(&dev->rx_reqs, link->out_ep, n);
392 if (status < 0)
393 goto fail;
394 goto done;
395fail:
396 DBG(dev, "can't alloc requests\n");
397done:
398 spin_unlock(&dev->req_lock);
399 return status;
400}
401
402static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
403{
404 struct usb_request *req;
405 unsigned long flags;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700406 int req_cnt = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700407
408 /* fill unused rxq slots with some skb */
409 spin_lock_irqsave(&dev->req_lock, flags);
410 while (!list_empty(&dev->rx_reqs)) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700411 /* break the nexus of continuous completion and re-submission*/
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600412 if (++req_cnt > qlen(dev->gadget, dev->qmult))
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700413 break;
414
David Brownell2b3d9422008-06-19 18:19:28 -0700415 req = container_of(dev->rx_reqs.next,
416 struct usb_request, list);
417 list_del_init(&req->list);
418 spin_unlock_irqrestore(&dev->req_lock, flags);
419
420 if (rx_submit(dev, req, gfp_flags) < 0) {
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700421 spin_lock_irqsave(&dev->req_lock, flags);
422 list_add(&req->list, &dev->rx_reqs);
423 spin_unlock_irqrestore(&dev->req_lock, flags);
David Brownell2b3d9422008-06-19 18:19:28 -0700424 defer_kevent(dev, WORK_RX_MEMORY);
425 return;
426 }
427
428 spin_lock_irqsave(&dev->req_lock, flags);
429 }
430 spin_unlock_irqrestore(&dev->req_lock, flags);
431}
432
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700433static void process_rx_w(struct work_struct *work)
434{
435 struct eth_dev *dev = container_of(work, struct eth_dev, rx_work);
436 struct sk_buff *skb;
437 int status = 0;
438
439 if (!dev->port_usb)
440 return;
441
442 while ((skb = skb_dequeue(&dev->rx_frames))) {
443 if (status < 0
444 || ETH_HLEN > skb->len
445 || skb->len > ETH_FRAME_LEN) {
446 dev->net->stats.rx_errors++;
447 dev->net->stats.rx_length_errors++;
448 DBG(dev, "rx length %d\n", skb->len);
449 dev_kfree_skb_any(skb);
450 continue;
451 }
452 skb->protocol = eth_type_trans(skb, dev->net);
453 dev->net->stats.rx_packets++;
454 dev->net->stats.rx_bytes += skb->len;
455
456 status = netif_rx_ni(skb);
457 }
458
459 if (netif_running(dev->net))
460 rx_fill(dev, GFP_KERNEL);
461}
462
David Brownell2b3d9422008-06-19 18:19:28 -0700463static void eth_work(struct work_struct *work)
464{
465 struct eth_dev *dev = container_of(work, struct eth_dev, work);
466
467 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
468 if (netif_running(dev->net))
469 rx_fill(dev, GFP_KERNEL);
470 }
471
472 if (dev->todo)
473 DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
474}
475
476static void tx_complete(struct usb_ep *ep, struct usb_request *req)
477{
478 struct sk_buff *skb = req->context;
479 struct eth_dev *dev = ep->driver_data;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700480 struct net_device *net = dev->net;
481 struct usb_request *new_req;
482 struct usb_ep *in;
483 int length;
484 int retval;
David Brownell2b3d9422008-06-19 18:19:28 -0700485
486 switch (req->status) {
487 default:
488 dev->net->stats.tx_errors++;
489 VDBG(dev, "tx err %d\n", req->status);
490 /* FALLTHROUGH */
491 case -ECONNRESET: /* unlink */
492 case -ESHUTDOWN: /* disconnect etc */
493 break;
494 case 0:
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700495 if (!req->zero)
496 dev->net->stats.tx_bytes += req->length-1;
497 else
498 dev->net->stats.tx_bytes += req->length;
David Brownell2b3d9422008-06-19 18:19:28 -0700499 }
500 dev->net->stats.tx_packets++;
501
502 spin_lock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700503 list_add_tail(&req->list, &dev->tx_reqs);
504
505 if (dev->port_usb->multi_pkt_xfer) {
506 dev->no_tx_req_used--;
507 req->length = 0;
508 in = dev->port_usb->in_ep;
509
510 if (!list_empty(&dev->tx_reqs)) {
511 new_req = container_of(dev->tx_reqs.next,
512 struct usb_request, list);
513 list_del(&new_req->list);
514 spin_unlock(&dev->req_lock);
515 if (new_req->length > 0) {
516 length = new_req->length;
517
518 /* NCM requires no zlp if transfer is
519 * dwNtbInMaxSize */
520 if (dev->port_usb->is_fixed &&
521 length == dev->port_usb->fixed_in_len &&
522 (length % in->maxpacket) == 0)
523 new_req->zero = 0;
524 else
525 new_req->zero = 1;
526
527 /* use zlp framing on tx for strict CDC-Ether
528 * conformance, though any robust network rx
529 * path ignores extra padding. and some hardware
530 * doesn't like to write zlps.
531 */
532 if (new_req->zero && !dev->zlp &&
533 (length % in->maxpacket) == 0) {
534 new_req->zero = 0;
535 length++;
536 }
537
538 new_req->length = length;
539 retval = usb_ep_queue(in, new_req, GFP_ATOMIC);
540 switch (retval) {
541 default:
542 DBG(dev, "tx queue err %d\n", retval);
543 break;
544 case 0:
545 spin_lock(&dev->req_lock);
546 dev->no_tx_req_used++;
547 spin_unlock(&dev->req_lock);
548 net->trans_start = jiffies;
549 }
550 } else {
551 spin_lock(&dev->req_lock);
552 list_add(&new_req->list, &dev->tx_reqs);
553 spin_unlock(&dev->req_lock);
554 }
555 } else {
556 spin_unlock(&dev->req_lock);
557 }
558 } else {
559 spin_unlock(&dev->req_lock);
560 dev_kfree_skb_any(skb);
561 }
David Brownell2b3d9422008-06-19 18:19:28 -0700562
David Brownell2b3d9422008-06-19 18:19:28 -0700563 if (netif_carrier_ok(dev->net))
564 netif_wake_queue(dev->net);
565}
566
567static inline int is_promisc(u16 cdc_filter)
568{
569 return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
570}
571
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700572static void alloc_tx_buffer(struct eth_dev *dev)
573{
574 struct list_head *act;
575 struct usb_request *req;
576
xerox_lincdffcb82014-09-04 16:01:59 +0800577 dev->tx_req_bufsize = (dev->dl_max_pkts_per_xfer *
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700578 (dev->net->mtu
579 + sizeof(struct ethhdr)
580 /* size of rndis_packet_msg_type */
581 + 44
582 + 22));
583
584 list_for_each(act, &dev->tx_reqs) {
585 req = container_of(act, struct usb_request, list);
586 if (!req->buf)
587 req->buf = kmalloc(dev->tx_req_bufsize,
588 GFP_ATOMIC);
589 }
590}
591
Stephen Hemminger25a79c42009-08-31 19:50:45 +0000592static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
593 struct net_device *net)
David Brownell2b3d9422008-06-19 18:19:28 -0700594{
595 struct eth_dev *dev = netdev_priv(net);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100596 int length = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700597 int retval;
598 struct usb_request *req = NULL;
599 unsigned long flags;
600 struct usb_ep *in;
601 u16 cdc_filter;
602
603 spin_lock_irqsave(&dev->lock, flags);
604 if (dev->port_usb) {
605 in = dev->port_usb->in_ep;
606 cdc_filter = dev->port_usb->cdc_filter;
607 } else {
608 in = NULL;
609 cdc_filter = 0;
610 }
611 spin_unlock_irqrestore(&dev->lock, flags);
612
Jim Baxter6d3865f2014-07-07 18:33:18 +0100613 if (skb && !in) {
David Brownell2b3d9422008-06-19 18:19:28 -0700614 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000615 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700616 }
617
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700618 /* Allocate memory for tx_reqs to support multi packet transfer */
619 if (dev->port_usb->multi_pkt_xfer && !dev->tx_req_bufsize)
620 alloc_tx_buffer(dev);
621
David Brownell2b3d9422008-06-19 18:19:28 -0700622 /* apply outgoing CDC or RNDIS filters */
Jim Baxter6d3865f2014-07-07 18:33:18 +0100623 if (skb && !is_promisc(cdc_filter)) {
David Brownell2b3d9422008-06-19 18:19:28 -0700624 u8 *dest = skb->data;
625
626 if (is_multicast_ether_addr(dest)) {
627 u16 type;
628
629 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
630 * SET_ETHERNET_MULTICAST_FILTERS requests
631 */
632 if (is_broadcast_ether_addr(dest))
633 type = USB_CDC_PACKET_TYPE_BROADCAST;
634 else
635 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
636 if (!(cdc_filter & type)) {
637 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000638 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700639 }
640 }
641 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
642 }
643
644 spin_lock_irqsave(&dev->req_lock, flags);
645 /*
646 * this freelist can be empty if an interrupt triggered disconnect()
647 * and reconfigured the gadget (shutting down this queue) after the
648 * network stack decided to xmit but before we got the spinlock.
649 */
650 if (list_empty(&dev->tx_reqs)) {
651 spin_unlock_irqrestore(&dev->req_lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +0000652 return NETDEV_TX_BUSY;
David Brownell2b3d9422008-06-19 18:19:28 -0700653 }
654
655 req = container_of(dev->tx_reqs.next, struct usb_request, list);
656 list_del(&req->list);
657
658 /* temporarily stop TX queue when the freelist empties */
659 if (list_empty(&dev->tx_reqs))
660 netif_stop_queue(net);
661 spin_unlock_irqrestore(&dev->req_lock, flags);
662
663 /* no buffer copies needed, unless the network stack did it
664 * or the hardware can't use skb buffers.
665 * or there's not enough space for extra headers we need
666 */
667 if (dev->wrap) {
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500668 unsigned long flags;
David Brownell2b3d9422008-06-19 18:19:28 -0700669
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500670 spin_lock_irqsave(&dev->lock, flags);
671 if (dev->port_usb)
672 skb = dev->wrap(dev->port_usb, skb);
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200673 spin_unlock_irqrestore(&dev->lock, flags);
Jim Baxter6d3865f2014-07-07 18:33:18 +0100674 if (!skb) {
675 /* Multi frame CDC protocols may store the frame for
676 * later which is not a dropped frame.
677 */
Peter Chen88c09ea2016-07-01 15:33:29 +0800678 if (dev->port_usb &&
Greg Kroah-Hartman3a383cc2016-09-19 11:05:43 +0200679 dev->port_usb->supports_multi_frame)
Jim Baxter6d3865f2014-07-07 18:33:18 +0100680 goto multiframe;
David Brownell2b3d9422008-06-19 18:19:28 -0700681 goto drop;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100682 }
David Brownell2b3d9422008-06-19 18:19:28 -0700683 }
Jim Baxter6d3865f2014-07-07 18:33:18 +0100684
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700685 spin_lock_irqsave(&dev->req_lock, flags);
686 dev->tx_skb_hold_count++;
687 spin_unlock_irqrestore(&dev->req_lock, flags);
688
689 if (dev->port_usb->multi_pkt_xfer) {
690 memcpy(req->buf + req->length, skb->data, skb->len);
691 req->length = req->length + skb->len;
692 length = req->length;
693 dev_kfree_skb_any(skb);
694
695 spin_lock_irqsave(&dev->req_lock, flags);
xerox_lincdffcb82014-09-04 16:01:59 +0800696 if (dev->tx_skb_hold_count < dev->dl_max_pkts_per_xfer) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700697 if (dev->no_tx_req_used > TX_REQ_THRESHOLD) {
698 list_add(&req->list, &dev->tx_reqs);
699 spin_unlock_irqrestore(&dev->req_lock, flags);
700 goto success;
701 }
702 }
703
704 dev->no_tx_req_used++;
705 spin_unlock_irqrestore(&dev->req_lock, flags);
706
707 spin_lock_irqsave(&dev->lock, flags);
708 dev->tx_skb_hold_count = 0;
709 spin_unlock_irqrestore(&dev->lock, flags);
710 } else {
711 length = skb->len;
712 req->buf = skb->data;
713 req->context = skb;
714 }
715
David Brownell2b3d9422008-06-19 18:19:28 -0700716 req->complete = tx_complete;
717
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200718 /* NCM requires no zlp if transfer is dwNtbInMaxSize */
Harish Jenny K N79775f42016-09-09 11:30:41 +0200719 if (dev->port_usb &&
720 dev->port_usb->is_fixed &&
Yauheni Kaliuta5c1168d2010-12-08 13:12:04 +0200721 length == dev->port_usb->fixed_in_len &&
722 (length % in->maxpacket) == 0)
723 req->zero = 0;
724 else
725 req->zero = 1;
726
David Brownell2b3d9422008-06-19 18:19:28 -0700727 /* use zlp framing on tx for strict CDC-Ether conformance,
728 * though any robust network rx path ignores extra padding.
729 * and some hardware doesn't like to write zlps.
730 */
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700731 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) {
732 req->zero = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700733 length++;
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700734 }
David Brownell2b3d9422008-06-19 18:19:28 -0700735
736 req->length = length;
737
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700738 /* throttle highspeed IRQ rate back slightly */
739 if (gadget_is_dualspeed(dev->gadget) &&
740 (dev->gadget->speed == USB_SPEED_HIGH)) {
741 dev->tx_qlen++;
Praneeth Bajjuri6b4b51c2015-01-22 16:38:56 -0600742 if (dev->tx_qlen == (dev->qmult/2)) {
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700743 req->no_interrupt = 0;
744 dev->tx_qlen = 0;
745 } else {
746 req->no_interrupt = 1;
747 }
748 } else {
749 req->no_interrupt = 0;
750 }
David Brownell2b3d9422008-06-19 18:19:28 -0700751
752 retval = usb_ep_queue(in, req, GFP_ATOMIC);
753 switch (retval) {
754 default:
755 DBG(dev, "tx queue err %d\n", retval);
756 break;
757 case 0:
Florian Westphal860e9532016-05-03 16:33:13 +0200758 netif_trans_update(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700759 }
760
761 if (retval) {
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700762 if (!dev->port_usb->multi_pkt_xfer)
763 dev_kfree_skb_any(skb);
David Brownell2b3d9422008-06-19 18:19:28 -0700764drop:
765 dev->net->stats.tx_dropped++;
Jim Baxter6d3865f2014-07-07 18:33:18 +0100766multiframe:
David Brownell2b3d9422008-06-19 18:19:28 -0700767 spin_lock_irqsave(&dev->req_lock, flags);
768 if (list_empty(&dev->tx_reqs))
769 netif_start_queue(net);
770 list_add(&req->list, &dev->tx_reqs);
771 spin_unlock_irqrestore(&dev->req_lock, flags);
772 }
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -0700773success:
Patrick McHardy6ed10652009-06-23 06:03:08 +0000774 return NETDEV_TX_OK;
David Brownell2b3d9422008-06-19 18:19:28 -0700775}
776
777/*-------------------------------------------------------------------------*/
778
779static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
780{
781 DBG(dev, "%s\n", __func__);
782
783 /* fill the rx queue */
784 rx_fill(dev, gfp_flags);
785
786 /* and open the tx floodgates */
Badhri Jagan Sridharancb6a7862014-09-18 10:42:41 -0700787 dev->tx_qlen = 0;
David Brownell2b3d9422008-06-19 18:19:28 -0700788 netif_wake_queue(dev->net);
789}
790
791static int eth_open(struct net_device *net)
792{
793 struct eth_dev *dev = netdev_priv(net);
794 struct gether *link;
795
796 DBG(dev, "%s\n", __func__);
797 if (netif_carrier_ok(dev->net))
798 eth_start(dev, GFP_KERNEL);
799
800 spin_lock_irq(&dev->lock);
801 link = dev->port_usb;
802 if (link && link->open)
803 link->open(link);
804 spin_unlock_irq(&dev->lock);
805
806 return 0;
807}
808
809static int eth_stop(struct net_device *net)
810{
811 struct eth_dev *dev = netdev_priv(net);
812 unsigned long flags;
813
814 VDBG(dev, "%s\n", __func__);
815 netif_stop_queue(net);
816
817 DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
818 dev->net->stats.rx_packets, dev->net->stats.tx_packets,
819 dev->net->stats.rx_errors, dev->net->stats.tx_errors
820 );
821
822 /* ensure there are no more active requests */
823 spin_lock_irqsave(&dev->lock, flags);
824 if (dev->port_usb) {
825 struct gether *link = dev->port_usb;
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200826 const struct usb_endpoint_descriptor *in;
827 const struct usb_endpoint_descriptor *out;
David Brownell2b3d9422008-06-19 18:19:28 -0700828
829 if (link->close)
830 link->close(link);
831
832 /* NOTE: we have no abort-queue primitive we could use
833 * to cancel all pending I/O. Instead, we disable then
834 * reenable the endpoints ... this idiom may leave toggle
835 * wrong, but that's a self-correcting error.
836 *
837 * REVISIT: we *COULD* just let the transfers complete at
838 * their own pace; the network stack can handle old packets.
839 * For the moment we leave this here, since it works.
840 */
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200841 in = link->in_ep->desc;
842 out = link->out_ep->desc;
David Brownell2b3d9422008-06-19 18:19:28 -0700843 usb_ep_disable(link->in_ep);
844 usb_ep_disable(link->out_ep);
845 if (netif_carrier_ok(net)) {
846 DBG(dev, "host still using in/out endpoints\n");
Michael Grzeschikb1b552a2012-08-08 11:48:10 +0200847 link->in_ep->desc = in;
848 link->out_ep->desc = out;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300849 usb_ep_enable(link->in_ep);
850 usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -0700851 }
852 }
853 spin_unlock_irqrestore(&dev->lock, flags);
854
855 return 0;
856}
857
858/*-------------------------------------------------------------------------*/
859
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200860static int get_ether_addr(const char *str, u8 *dev_addr)
David Brownell2b3d9422008-06-19 18:19:28 -0700861{
862 if (str) {
863 unsigned i;
864
865 for (i = 0; i < 6; i++) {
866 unsigned char num;
867
868 if ((*str == '.') || (*str == ':'))
869 str++;
Andy Shevchenkoe6448142010-06-15 17:04:44 +0300870 num = hex_to_bin(*str++) << 4;
871 num |= hex_to_bin(*str++);
David Brownell2b3d9422008-06-19 18:19:28 -0700872 dev_addr [i] = num;
873 }
874 if (is_valid_ether_addr(dev_addr))
875 return 0;
876 }
Joe Perches006c9132012-07-12 22:33:11 -0700877 eth_random_addr(dev_addr);
David Brownell2b3d9422008-06-19 18:19:28 -0700878 return 1;
879}
880
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200881static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
882{
883 if (len < 18)
884 return -EINVAL;
885
Andy Shevchenko27f38702015-01-15 13:40:04 +0200886 snprintf(str, len, "%pM", dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200887 return 18;
888}
889
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800890static const struct net_device_ops eth_netdev_ops = {
891 .ndo_open = eth_open,
892 .ndo_stop = eth_stop,
893 .ndo_start_xmit = eth_start_xmit,
894 .ndo_change_mtu = ueth_change_mtu,
895 .ndo_set_mac_address = eth_mac_addr,
896 .ndo_validate_addr = eth_validate_addr,
897};
David Brownell2b3d9422008-06-19 18:19:28 -0700898
Marcel Holtmannaa790742010-01-15 22:13:58 -0800899static struct device_type gadget_type = {
900 .name = "gadget",
901};
902
David Brownell2b3d9422008-06-19 18:19:28 -0700903/**
Mike Lockwood036e98b2012-05-10 10:08:02 +0200904 * gether_setup_name - initialize one ethernet-over-usb link
David Brownell2b3d9422008-06-19 18:19:28 -0700905 * @g: gadget to associated with these links
906 * @ethaddr: NULL, or a buffer in which the ethernet address of the
907 * host side of the link is recorded
Mike Lockwood036e98b2012-05-10 10:08:02 +0200908 * @netname: name for network device (for example, "usb")
David Brownell2b3d9422008-06-19 18:19:28 -0700909 * Context: may sleep
910 *
911 * This sets up the single network link that may be exported by a
912 * gadget driver using this framework. The link layer addresses are
913 * set up using module parameters.
914 *
Dan Carpenter574f24f2013-11-14 11:42:11 +0300915 * Returns an eth_dev pointer on success, or an ERR_PTR on failure.
David Brownell2b3d9422008-06-19 18:19:28 -0700916 */
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200917struct eth_dev *gether_setup_name(struct usb_gadget *g,
918 const char *dev_addr, const char *host_addr,
919 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
David Brownell2b3d9422008-06-19 18:19:28 -0700920{
921 struct eth_dev *dev;
922 struct net_device *net;
923 int status;
924
David Brownell2b3d9422008-06-19 18:19:28 -0700925 net = alloc_etherdev(sizeof *dev);
926 if (!net)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100927 return ERR_PTR(-ENOMEM);
David Brownell2b3d9422008-06-19 18:19:28 -0700928
929 dev = netdev_priv(net);
930 spin_lock_init(&dev->lock);
931 spin_lock_init(&dev->req_lock);
932 INIT_WORK(&dev->work, eth_work);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -0700933 INIT_WORK(&dev->rx_work, process_rx_w);
David Brownell2b3d9422008-06-19 18:19:28 -0700934 INIT_LIST_HEAD(&dev->tx_reqs);
935 INIT_LIST_HEAD(&dev->rx_reqs);
936
Brian Niebuhr9b39e9d2009-08-14 10:04:22 -0500937 skb_queue_head_init(&dev->rx_frames);
938
David Brownell2b3d9422008-06-19 18:19:28 -0700939 /* network device setup */
940 dev->net = net;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200941 dev->qmult = qmult;
Mike Lockwood036e98b2012-05-10 10:08:02 +0200942 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
David Brownell2b3d9422008-06-19 18:19:28 -0700943
944 if (get_ether_addr(dev_addr, net->dev_addr))
945 dev_warn(&g->dev,
946 "using random %s ethernet address\n", "self");
947 if (get_ether_addr(host_addr, dev->host_mac))
948 dev_warn(&g->dev,
949 "using random %s ethernet address\n", "host");
950
951 if (ethaddr)
952 memcpy(ethaddr, dev->host_mac, ETH_ALEN);
953
Stephen Hemminger5ec38f32009-01-07 18:05:39 -0800954 net->netdev_ops = &eth_netdev_ops;
955
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000956 net->ethtool_ops = &ops;
David Brownell2b3d9422008-06-19 18:19:28 -0700957
David Brownell2b3d9422008-06-19 18:19:28 -0700958 dev->gadget = g;
959 SET_NETDEV_DEV(net, &g->dev);
Marcel Holtmannaa790742010-01-15 22:13:58 -0800960 SET_NETDEV_DEVTYPE(net, &gadget_type);
David Brownell2b3d9422008-06-19 18:19:28 -0700961
962 status = register_netdev(net);
963 if (status < 0) {
964 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
965 free_netdev(net);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100966 dev = ERR_PTR(status);
David Brownell2b3d9422008-06-19 18:19:28 -0700967 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700968 INFO(dev, "MAC %pM\n", net->dev_addr);
969 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
David Brownell2b3d9422008-06-19 18:19:28 -0700970
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200971 /*
972 * two kinds of host-initiated state changes:
Kevin Cernekee31bde1c2012-06-24 21:11:22 -0700973 * - iff DATA transfer is active, carrier is "on"
974 * - tx queueing enabled if open *and* carrier is "on"
975 */
976 netif_carrier_off(net);
David Brownell2b3d9422008-06-19 18:19:28 -0700977 }
978
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100979 return dev;
David Brownell2b3d9422008-06-19 18:19:28 -0700980}
Felipe Balbi0700faa2014-04-01 13:19:32 -0500981EXPORT_SYMBOL_GPL(gether_setup_name);
David Brownell2b3d9422008-06-19 18:19:28 -0700982
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200983struct net_device *gether_setup_name_default(const char *netname)
984{
985 struct net_device *net;
986 struct eth_dev *dev;
987
988 net = alloc_etherdev(sizeof(*dev));
989 if (!net)
990 return ERR_PTR(-ENOMEM);
991
992 dev = netdev_priv(net);
993 spin_lock_init(&dev->lock);
994 spin_lock_init(&dev->req_lock);
995 INIT_WORK(&dev->work, eth_work);
Matthew Moeller5df32222016-03-09 20:19:25 -0600996 INIT_WORK(&dev->rx_work, process_rx_w);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +0200997 INIT_LIST_HEAD(&dev->tx_reqs);
998 INIT_LIST_HEAD(&dev->rx_reqs);
999
1000 skb_queue_head_init(&dev->rx_frames);
1001
1002 /* network device setup */
1003 dev->net = net;
1004 dev->qmult = QMULT_DEFAULT;
1005 snprintf(net->name, sizeof(net->name), "%s%%d", netname);
1006
1007 eth_random_addr(dev->dev_mac);
1008 pr_warn("using random %s ethernet address\n", "self");
1009 eth_random_addr(dev->host_mac);
1010 pr_warn("using random %s ethernet address\n", "host");
1011
1012 net->netdev_ops = &eth_netdev_ops;
1013
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001014 net->ethtool_ops = &ops;
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001015 SET_NETDEV_DEVTYPE(net, &gadget_type);
1016
1017 return net;
1018}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001019EXPORT_SYMBOL_GPL(gether_setup_name_default);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001020
1021int gether_register_netdev(struct net_device *net)
1022{
1023 struct eth_dev *dev;
1024 struct usb_gadget *g;
1025 struct sockaddr sa;
1026 int status;
1027
1028 if (!net->dev.parent)
1029 return -EINVAL;
1030 dev = netdev_priv(net);
1031 g = dev->gadget;
1032 status = register_netdev(net);
1033 if (status < 0) {
1034 dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
1035 return status;
1036 } else {
1037 INFO(dev, "HOST MAC %pM\n", dev->host_mac);
1038
1039 /* two kinds of host-initiated state changes:
1040 * - iff DATA transfer is active, carrier is "on"
1041 * - tx queueing enabled if open *and* carrier is "on"
1042 */
1043 netif_carrier_off(net);
1044 }
1045 sa.sa_family = net->type;
1046 memcpy(sa.sa_data, dev->dev_mac, ETH_ALEN);
1047 rtnl_lock();
1048 status = dev_set_mac_address(net, &sa);
1049 rtnl_unlock();
1050 if (status)
1051 pr_warn("cannot set self ethernet address: %d\n", status);
1052 else
1053 INFO(dev, "MAC %pM\n", dev->dev_mac);
1054
1055 return status;
1056}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001057EXPORT_SYMBOL_GPL(gether_register_netdev);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001058
1059void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
1060{
1061 struct eth_dev *dev;
1062
1063 dev = netdev_priv(net);
1064 dev->gadget = g;
1065 SET_NETDEV_DEV(net, &g->dev);
1066}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001067EXPORT_SYMBOL_GPL(gether_set_gadget);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001068
1069int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
1070{
1071 struct eth_dev *dev;
1072 u8 new_addr[ETH_ALEN];
1073
1074 dev = netdev_priv(net);
1075 if (get_ether_addr(dev_addr, new_addr))
1076 return -EINVAL;
1077 memcpy(dev->dev_mac, new_addr, ETH_ALEN);
1078 return 0;
1079}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001080EXPORT_SYMBOL_GPL(gether_set_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001081
1082int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
1083{
1084 struct eth_dev *dev;
1085
1086 dev = netdev_priv(net);
1087 return get_ether_addr_str(dev->dev_mac, dev_addr, len);
1088}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001089EXPORT_SYMBOL_GPL(gether_get_dev_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001090
1091int gether_set_host_addr(struct net_device *net, const char *host_addr)
1092{
1093 struct eth_dev *dev;
1094 u8 new_addr[ETH_ALEN];
1095
1096 dev = netdev_priv(net);
1097 if (get_ether_addr(host_addr, new_addr))
1098 return -EINVAL;
1099 memcpy(dev->host_mac, new_addr, ETH_ALEN);
1100 return 0;
1101}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001102EXPORT_SYMBOL_GPL(gether_set_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001103
1104int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
1105{
1106 struct eth_dev *dev;
1107
1108 dev = netdev_priv(net);
1109 return get_ether_addr_str(dev->host_mac, host_addr, len);
1110}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001111EXPORT_SYMBOL_GPL(gether_get_host_addr);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001112
1113int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
1114{
1115 struct eth_dev *dev;
1116
1117 if (len < 13)
1118 return -EINVAL;
1119
1120 dev = netdev_priv(net);
1121 snprintf(host_addr, len, "%pm", dev->host_mac);
1122
1123 return strlen(host_addr);
1124}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001125EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001126
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001127void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
1128{
1129 struct eth_dev *dev;
1130
1131 dev = netdev_priv(net);
1132 memcpy(host_mac, dev->host_mac, ETH_ALEN);
1133}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001134EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
Andrzej Pietrasiewiczbf4277c2013-05-28 09:15:45 +02001135
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001136void gether_set_qmult(struct net_device *net, unsigned qmult)
1137{
1138 struct eth_dev *dev;
1139
1140 dev = netdev_priv(net);
1141 dev->qmult = qmult;
1142}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001143EXPORT_SYMBOL_GPL(gether_set_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001144
1145unsigned gether_get_qmult(struct net_device *net)
1146{
1147 struct eth_dev *dev;
1148
1149 dev = netdev_priv(net);
1150 return dev->qmult;
1151}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001152EXPORT_SYMBOL_GPL(gether_get_qmult);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001153
1154int gether_get_ifname(struct net_device *net, char *name, int len)
1155{
1156 rtnl_lock();
1157 strlcpy(name, netdev_name(net), len);
1158 rtnl_unlock();
1159 return strlen(name);
1160}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001161EXPORT_SYMBOL_GPL(gether_get_ifname);
Andrzej Pietrasiewiczbcd4a1c2013-05-23 09:22:05 +02001162
David Brownell2b3d9422008-06-19 18:19:28 -07001163/**
1164 * gether_cleanup - remove Ethernet-over-USB device
1165 * Context: may sleep
1166 *
1167 * This is called to free all resources allocated by @gether_setup().
1168 */
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001169void gether_cleanup(struct eth_dev *dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001170{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001171 if (!dev)
David Brownell2b3d9422008-06-19 18:19:28 -07001172 return;
1173
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001174 unregister_netdev(dev->net);
1175 flush_work(&dev->work);
1176 free_netdev(dev->net);
David Brownell2b3d9422008-06-19 18:19:28 -07001177}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001178EXPORT_SYMBOL_GPL(gether_cleanup);
David Brownell2b3d9422008-06-19 18:19:28 -07001179
David Brownell2b3d9422008-06-19 18:19:28 -07001180/**
1181 * gether_connect - notify network layer that USB link is active
1182 * @link: the USB link, set up with endpoints, descriptors matching
1183 * current device speed, and any framing wrapper(s) set up.
1184 * Context: irqs blocked
1185 *
1186 * This is called to activate endpoints and let the network layer know
1187 * the connection is active ("carrier detect"). It may cause the I/O
1188 * queues to open and start letting network packets flow, but will in
1189 * any case activate the endpoints so that they respond properly to the
1190 * USB host.
1191 *
1192 * Verify net_device pointer returned using IS_ERR(). If it doesn't
1193 * indicate some error code (negative errno), ep->driver_data values
1194 * have been overwritten.
1195 */
1196struct net_device *gether_connect(struct gether *link)
1197{
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +01001198 struct eth_dev *dev = link->ioport;
David Brownell2b3d9422008-06-19 18:19:28 -07001199 int result = 0;
1200
1201 if (!dev)
1202 return ERR_PTR(-EINVAL);
1203
1204 link->in_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001205 result = usb_ep_enable(link->in_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001206 if (result != 0) {
1207 DBG(dev, "enable %s --> %d\n",
1208 link->in_ep->name, result);
1209 goto fail0;
1210 }
1211
1212 link->out_ep->driver_data = dev;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001213 result = usb_ep_enable(link->out_ep);
David Brownell2b3d9422008-06-19 18:19:28 -07001214 if (result != 0) {
1215 DBG(dev, "enable %s --> %d\n",
1216 link->out_ep->name, result);
1217 goto fail1;
1218 }
1219
1220 if (result == 0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001221 result = alloc_requests(dev, link, qlen(dev->gadget,
1222 dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001223
1224 if (result == 0) {
1225 dev->zlp = link->is_zlp_ok;
Yoshihiro Shimoda05f6b0f2016-08-22 17:48:26 +09001226 dev->no_skb_reserve = link->no_skb_reserve;
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001227 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
David Brownell2b3d9422008-06-19 18:19:28 -07001228
1229 dev->header_len = link->header_len;
1230 dev->unwrap = link->unwrap;
1231 dev->wrap = link->wrap;
xerox_lin87bebf82014-08-14 14:48:44 +08001232 dev->ul_max_pkts_per_xfer = link->ul_max_pkts_per_xfer;
xerox_lincdffcb82014-09-04 16:01:59 +08001233 dev->dl_max_pkts_per_xfer = link->dl_max_pkts_per_xfer;
David Brownell2b3d9422008-06-19 18:19:28 -07001234
1235 spin_lock(&dev->lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -07001236 dev->tx_skb_hold_count = 0;
1237 dev->no_tx_req_used = 0;
1238 dev->tx_req_bufsize = 0;
David Brownell2b3d9422008-06-19 18:19:28 -07001239 dev->port_usb = link;
David Brownell29bac7b2008-09-06 21:33:49 -07001240 if (netif_running(dev->net)) {
1241 if (link->open)
1242 link->open(link);
1243 } else {
1244 if (link->close)
1245 link->close(link);
1246 }
David Brownell2b3d9422008-06-19 18:19:28 -07001247 spin_unlock(&dev->lock);
1248
1249 netif_carrier_on(dev->net);
1250 if (netif_running(dev->net))
1251 eth_start(dev, GFP_ATOMIC);
1252
1253 /* on error, disable any endpoints */
1254 } else {
1255 (void) usb_ep_disable(link->out_ep);
1256fail1:
1257 (void) usb_ep_disable(link->in_ep);
1258 }
1259fail0:
1260 /* caller is responsible for cleanup on error */
1261 if (result < 0)
1262 return ERR_PTR(result);
1263 return dev->net;
1264}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001265EXPORT_SYMBOL_GPL(gether_connect);
David Brownell2b3d9422008-06-19 18:19:28 -07001266
1267/**
1268 * gether_disconnect - notify network layer that USB link is inactive
1269 * @link: the USB link, on which gether_connect() was called
1270 * Context: irqs blocked
1271 *
1272 * This is called to deactivate endpoints and let the network layer know
1273 * the connection went inactive ("no carrier").
1274 *
1275 * On return, the state is as if gether_connect() had never been called.
1276 * The endpoints are inactive, and accordingly without active USB I/O.
1277 * Pointers to endpoint descriptors and endpoint private data are nulled.
1278 */
1279void gether_disconnect(struct gether *link)
1280{
1281 struct eth_dev *dev = link->ioport;
1282 struct usb_request *req;
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001283 struct sk_buff *skb;
David Brownell2b3d9422008-06-19 18:19:28 -07001284
1285 WARN_ON(!dev);
1286 if (!dev)
1287 return;
1288
1289 DBG(dev, "%s\n", __func__);
1290
1291 netif_stop_queue(dev->net);
1292 netif_carrier_off(dev->net);
1293
1294 /* disable endpoints, forcing (synchronous) completion
1295 * of all pending i/o. then free the request objects
1296 * and forget about the endpoints.
1297 */
1298 usb_ep_disable(link->in_ep);
1299 spin_lock(&dev->req_lock);
1300 while (!list_empty(&dev->tx_reqs)) {
1301 req = container_of(dev->tx_reqs.next,
1302 struct usb_request, list);
1303 list_del(&req->list);
1304
1305 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharane791ad32014-09-18 10:46:08 -07001306 if (link->multi_pkt_xfer)
1307 kfree(req->buf);
David Brownell2b3d9422008-06-19 18:19:28 -07001308 usb_ep_free_request(link->in_ep, req);
1309 spin_lock(&dev->req_lock);
1310 }
1311 spin_unlock(&dev->req_lock);
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001312 link->in_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001313
1314 usb_ep_disable(link->out_ep);
1315 spin_lock(&dev->req_lock);
1316 while (!list_empty(&dev->rx_reqs)) {
1317 req = container_of(dev->rx_reqs.next,
1318 struct usb_request, list);
1319 list_del(&req->list);
1320
1321 spin_unlock(&dev->req_lock);
1322 usb_ep_free_request(link->out_ep, req);
1323 spin_lock(&dev->req_lock);
1324 }
1325 spin_unlock(&dev->req_lock);
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001326
1327 spin_lock(&dev->rx_frames.lock);
1328 while ((skb = __skb_dequeue(&dev->rx_frames)))
1329 dev_kfree_skb_any(skb);
1330 spin_unlock(&dev->rx_frames.lock);
1331
Tatyana Brokhman72c973d2011-06-28 16:33:48 +03001332 link->out_ep->desc = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001333
1334 /* finish forgetting about this USB link episode */
1335 dev->header_len = 0;
1336 dev->unwrap = NULL;
1337 dev->wrap = NULL;
1338
1339 spin_lock(&dev->lock);
1340 dev->port_usb = NULL;
David Brownell2b3d9422008-06-19 18:19:28 -07001341 spin_unlock(&dev->lock);
1342}
Felipe Balbi0700faa2014-04-01 13:19:32 -05001343EXPORT_SYMBOL_GPL(gether_disconnect);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001344
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001345static int __init gether_init(void)
1346{
1347 uether_wq = create_singlethread_workqueue("uether");
1348 if (!uether_wq) {
1349 pr_err("%s: Unable to create workqueue: uether\n", __func__);
1350 return -ENOMEM;
1351 }
1352 return 0;
1353}
1354module_init(gether_init);
1355
1356static void __exit gether_exit(void)
1357{
1358 destroy_workqueue(uether_wq);
1359
1360}
1361module_exit(gether_exit);
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +02001362MODULE_AUTHOR("David Brownell");
Badhri Jagan Sridharana096ba52014-09-24 18:58:23 -07001363MODULE_DESCRIPTION("ethernet over USB driver");
1364MODULE_LICENSE("GPL v2");