blob: 90ef1f055f76bdd128d2d5f2a296c8829a11224d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Remy Bohmer23cd1382009-07-29 18:18:43 +02002/*
3 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 *
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
Remy Bohmer23cd1382009-07-29 18:18:43 +02008 */
9
10#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070011#include <console.h>
Alex Kiernan9925f1d2018-04-01 09:22:38 +000012#include <environment.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +030014#include <linux/netdevice.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020015#include <linux/usb/ch9.h>
16#include <linux/usb/cdc.h>
17#include <linux/usb/gadget.h>
18#include <net.h>
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +053019#include <usb.h>
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030020#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060021#include <memalign.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020022#include <linux/ctype.h>
23
24#include "gadget_chips.h"
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030025#include "rndis.h"
Remy Bohmer23cd1382009-07-29 18:18:43 +020026
Mugunthan V Nd4345ae2016-11-18 10:49:12 +053027#include <dm.h>
Mugunthan V Nd4a37552016-11-18 11:09:15 +053028#include <dm/lists.h>
Mugunthan V Nd4345ae2016-11-18 10:49:12 +053029#include <dm/uclass-internal.h>
30#include <dm/device-internal.h>
31
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +030032#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040033
Remy Bohmer23cd1382009-07-29 18:18:43 +020034#define atomic_read
35extern struct platform_data brd;
Remy Bohmer23cd1382009-07-29 18:18:43 +020036
37
38unsigned packet_received, packet_sent;
39
Remy Bohmer23cd1382009-07-29 18:18:43 +020040/*
41 * Ethernet gadget driver -- with CDC and non-CDC options
42 * Builds on hardware support for a full duplex link.
43 *
44 * CDC Ethernet is the standard USB solution for sending Ethernet frames
45 * using USB. Real hardware tends to use the same framing protocol but look
46 * different for control features. This driver strongly prefers to use
47 * this USB-IF standard as its open-systems interoperability solution;
48 * most host side USB stacks (except from Microsoft) support it.
49 *
50 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
51 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
52 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
53 *
54 * There's some hardware that can't talk CDC ECM. We make that hardware
55 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
56 * link-level setup only requires activating the configuration. Only the
57 * endpoint descriptors, and product/vendor IDs, are relevant; no control
58 * operations are available. Linux supports it, but other host operating
59 * systems may not. (This is a subset of CDC Ethernet.)
60 *
61 * It turns out that if you add a few descriptors to that "CDC Subset",
62 * (Windows) host side drivers from MCCI can treat it as one submode of
63 * a proprietary scheme called "SAFE" ... without needing to know about
64 * specific product/vendor IDs. So we do that, making it easier to use
65 * those MS-Windows drivers. Those added descriptors make it resemble a
66 * CDC MDLM device, but they don't change device behavior at all. (See
67 * MCCI Engineering report 950198 "SAFE Networking Functions".)
68 *
69 * A third option is also in use. Rather than CDC Ethernet, or something
70 * simpler, Microsoft pushes their own approach: RNDIS. The published
71 * RNDIS specs are ambiguous and appear to be incomplete, and are also
72 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
73 */
Remy Bohmer23cd1382009-07-29 18:18:43 +020074
75#define DRIVER_DESC "Ethernet Gadget"
76/* Based on linux 2.6.27 version */
77#define DRIVER_VERSION "May Day 2005"
78
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040079static const char driver_desc[] = DRIVER_DESC;
Remy Bohmer23cd1382009-07-29 18:18:43 +020080
81#define RX_EXTRA 20 /* guard against rx overflows */
82
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030083#ifndef CONFIG_USB_ETH_RNDIS
84#define rndis_uninit(x) do {} while (0)
85#define rndis_deregister(c) do {} while (0)
86#define rndis_exit() do {} while (0)
87#endif
88
89/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmer23cd1382009-07-29 18:18:43 +020090#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
91 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
92 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
93 |USB_CDC_PACKET_TYPE_DIRECTED)
94
95#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
96
97/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +030098
99struct eth_dev {
100 struct usb_gadget *gadget;
101 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300102 struct usb_request *stat_req; /* for cdc & rndis status */
Mugunthan V Nd4345ae2016-11-18 10:49:12 +0530103#ifdef CONFIG_DM_USB
104 struct udevice *usb_udev;
105#endif
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300106
107 u8 config;
108 struct usb_ep *in_ep, *out_ep, *status_ep;
109 const struct usb_endpoint_descriptor
110 *in, *out, *status;
111
112 struct usb_request *tx_req, *rx_req;
113
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530114#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300115 struct eth_device *net;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530116#else
117 struct udevice *net;
118#endif
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300119 struct net_device_stats stats;
120 unsigned int tx_qlen;
121
122 unsigned zlp:1;
123 unsigned cdc:1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300124 unsigned rndis:1;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300125 unsigned suspended:1;
126 unsigned network_started:1;
127 u16 cdc_filter;
128 unsigned long todo;
129 int mtu;
130#define WORK_RX_MEMORY 0
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300131 int rndis_config;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300132 u8 host_mac[ETH_ALEN];
133};
134
135/*
136 * This version autoconfigures as much as possible at run-time.
137 *
138 * It also ASSUMES a self-powered device, without remote wakeup,
139 * although remote wakeup support would make sense.
140 */
141
142/*-------------------------------------------------------------------------*/
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +0530143struct ether_priv {
144 struct eth_dev ethdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530145#ifndef CONFIG_DM_ETH
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +0530146 struct eth_device netdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530147#else
148 struct udevice *netdev;
149#endif
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +0530150 struct usb_gadget_driver eth_driver;
151};
152
153struct ether_priv eth_priv;
154struct ether_priv *l_priv = &eth_priv;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200155
156/*-------------------------------------------------------------------------*/
157
158/* "main" config is either CDC, or its simple subset */
159static inline int is_cdc(struct eth_dev *dev)
160{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200161#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200162 return 1; /* only cdc possible */
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200163#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200164 return 0; /* only subset possible */
165#else
166 return dev->cdc; /* depends on what hardware we found */
167#endif
168}
169
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300170/* "secondary" RNDIS config may sometimes be activated */
171static inline int rndis_active(struct eth_dev *dev)
172{
173#ifdef CONFIG_USB_ETH_RNDIS
174 return dev->rndis;
175#else
176 return 0;
177#endif
178}
179
180#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
181#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200182
183#define DEFAULT_QLEN 2 /* double buffering by default */
184
185/* peak bulk transfer bits-per-second */
186#define HS_BPS (13 * 512 * 8 * 1000 * 8)
187#define FS_BPS (19 * 64 * 1 * 1000 * 8)
188
189#ifdef CONFIG_USB_GADGET_DUALSPEED
190#define DEVSPEED USB_SPEED_HIGH
191
Vitaly Kuzmichev2721dbf2010-08-12 16:44:40 +0400192#ifdef CONFIG_USB_ETH_QMULT
193#define qmult CONFIG_USB_ETH_QMULT
194#else
195#define qmult 5
196#endif
197
Remy Bohmer23cd1382009-07-29 18:18:43 +0200198/* for dual-speed hardware, use deeper queues at highspeed */
199#define qlen(gadget) \
200 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
201
202static inline int BITRATE(struct usb_gadget *g)
203{
204 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
205}
206
207#else /* full speed (low speed doesn't do bulk) */
208
209#define qmult 1
210
211#define DEVSPEED USB_SPEED_FULL
212
213#define qlen(gadget) DEFAULT_QLEN
214
215static inline int BITRATE(struct usb_gadget *g)
216{
217 return FS_BPS;
218}
219#endif
220
Remy Bohmer23cd1382009-07-29 18:18:43 +0200221/*-------------------------------------------------------------------------*/
222
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400223/*
224 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmer23cd1382009-07-29 18:18:43 +0200225 * Instead: allocate your own, using normal USB-IF procedures.
226 */
227
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400228/*
229 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200230 * It's for devices with only CDC Ethernet configurations.
231 */
232#define CDC_VENDOR_NUM 0x0525 /* NetChip */
233#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
234
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400235/*
236 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmer23cd1382009-07-29 18:18:43 +0200237 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
238 * with pxa250. We're protocol-compatible, if the host-side drivers
239 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
240 * drivers that need to hard-wire endpoint numbers have a hook.
241 *
242 * The protocol is a minimal subset of CDC Ether, which works on any bulk
243 * hardware that's not deeply broken ... even on hardware that can't talk
244 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
245 * doesn't handle control-OUT).
246 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300247#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
248#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
249
250/*
251 * For hardware that can talk RNDIS and either of the above protocols,
252 * use this ID ... the windows INF files will know it. Unless it's
253 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
254 * the non-RNDIS configuration.
255 */
256#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
257#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200258
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400259/*
260 * Some systems will want different product identifers published in the
Remy Bohmer23cd1382009-07-29 18:18:43 +0200261 * device descriptor, either numbers or strings or both. These string
262 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
263 */
264
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300265/*
266 * Emulating them in eth_bind:
267 * static ushort idVendor;
268 * static ushort idProduct;
269 */
270
Maxime Ripard10ac57f2017-09-07 09:15:08 +0200271#if defined(CONFIG_USB_GADGET_MANUFACTURER)
272static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200273#else
Bin Menga1875592016-02-05 19:30:11 -0800274static char *iManufacturer = "U-Boot";
Remy Bohmer23cd1382009-07-29 18:18:43 +0200275#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300276
277/* These probably need to be configurable. */
278static ushort bcdDevice;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200279static char *iProduct;
280static char *iSerialNumber;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300281
Remy Bohmer23cd1382009-07-29 18:18:43 +0200282static char dev_addr[18];
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300283
Remy Bohmer23cd1382009-07-29 18:18:43 +0200284static char host_addr[18];
285
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300286
Remy Bohmer23cd1382009-07-29 18:18:43 +0200287/*-------------------------------------------------------------------------*/
288
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400289/*
290 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmer23cd1382009-07-29 18:18:43 +0200291 * ep0 implementation: descriptors, config management, setup().
292 * also optional class-specific notification interrupt transfer.
293 */
294
295/*
296 * DESCRIPTORS ... most are static, but strings and (full) configuration
297 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300298 * our simple subset, with RNDIS as an optional second configuration.
299 *
300 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
301 * the class descriptors match a modem (they're ignored; it's really just
302 * Ethernet functionality), they don't need the NOP altsetting, and the
303 * status transfer endpoint isn't optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200304 */
305
306#define STRING_MANUFACTURER 1
307#define STRING_PRODUCT 2
308#define STRING_ETHADDR 3
309#define STRING_DATA 4
310#define STRING_CONTROL 5
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300311#define STRING_RNDIS_CONTROL 6
Remy Bohmer23cd1382009-07-29 18:18:43 +0200312#define STRING_CDC 7
313#define STRING_SUBSET 8
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300314#define STRING_RNDIS 9
Remy Bohmer23cd1382009-07-29 18:18:43 +0200315#define STRING_SERIALNUMBER 10
316
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300317/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200318#define USB_BUFSIZ 256
319
320/*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300321 * This device advertises one configuration, eth_config, unless RNDIS
322 * is enabled (rndis_config) on hardware supporting at least two configs.
323 *
324 * NOTE: Controllers like superh_udc should probably be able to use
325 * an RNDIS-only configuration.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200326 *
327 * FIXME define some higher-powered configurations to make it easier
328 * to recharge batteries ...
329 */
330
331#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300332#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200333
334static struct usb_device_descriptor
335device_desc = {
336 .bLength = sizeof device_desc,
337 .bDescriptorType = USB_DT_DEVICE,
338
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400339 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200340
341 .bDeviceClass = USB_CLASS_COMM,
342 .bDeviceSubClass = 0,
343 .bDeviceProtocol = 0,
344
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400345 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
346 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200347 .iManufacturer = STRING_MANUFACTURER,
348 .iProduct = STRING_PRODUCT,
349 .bNumConfigurations = 1,
350};
351
352static struct usb_otg_descriptor
353otg_descriptor = {
354 .bLength = sizeof otg_descriptor,
355 .bDescriptorType = USB_DT_OTG,
356
357 .bmAttributes = USB_OTG_SRP,
358};
359
360static struct usb_config_descriptor
361eth_config = {
362 .bLength = sizeof eth_config,
363 .bDescriptorType = USB_DT_CONFIG,
364
365 /* compute wTotalLength on the fly */
366 .bNumInterfaces = 2,
367 .bConfigurationValue = DEV_CONFIG_VALUE,
368 .iConfiguration = STRING_CDC,
369 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
370 .bMaxPower = 1,
371};
372
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300373#ifdef CONFIG_USB_ETH_RNDIS
374static struct usb_config_descriptor
375rndis_config = {
376 .bLength = sizeof rndis_config,
377 .bDescriptorType = USB_DT_CONFIG,
378
379 /* compute wTotalLength on the fly */
380 .bNumInterfaces = 2,
381 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
382 .iConfiguration = STRING_RNDIS,
383 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
384 .bMaxPower = 1,
385};
386#endif
387
Remy Bohmer23cd1382009-07-29 18:18:43 +0200388/*
389 * Compared to the simple CDC subset, the full CDC Ethernet model adds
390 * three class descriptors, two interface descriptors, optional status
391 * endpoint. Both have a "data" interface and two bulk endpoints.
392 * There are also differences in how control requests are handled.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300393 *
394 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
395 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
396 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
397 * work around those bugs) are unlikely to ever come from MSFT, you may
398 * wish to avoid using RNDIS.
399 *
400 * MCCI offers an alternative to RNDIS if you need to connect to Windows
401 * but have hardware that can't support CDC Ethernet. We add descriptors
402 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
403 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
404 * get those drivers from MCCI, or bundled with various products.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200405 */
406
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200407#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200408static struct usb_interface_descriptor
409control_intf = {
410 .bLength = sizeof control_intf,
411 .bDescriptorType = USB_DT_INTERFACE,
412
413 .bInterfaceNumber = 0,
414 /* status endpoint is optional; this may be patched later */
415 .bNumEndpoints = 1,
416 .bInterfaceClass = USB_CLASS_COMM,
417 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
418 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
419 .iInterface = STRING_CONTROL,
420};
421#endif
422
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300423#ifdef CONFIG_USB_ETH_RNDIS
424static const struct usb_interface_descriptor
425rndis_control_intf = {
426 .bLength = sizeof rndis_control_intf,
427 .bDescriptorType = USB_DT_INTERFACE,
428
429 .bInterfaceNumber = 0,
430 .bNumEndpoints = 1,
431 .bInterfaceClass = USB_CLASS_COMM,
432 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
433 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
434 .iInterface = STRING_RNDIS_CONTROL,
435};
436#endif
437
Remy Bohmer23cd1382009-07-29 18:18:43 +0200438static const struct usb_cdc_header_desc header_desc = {
439 .bLength = sizeof header_desc,
440 .bDescriptorType = USB_DT_CS_INTERFACE,
441 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
442
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400443 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200444};
445
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200446#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200447
448static const struct usb_cdc_union_desc union_desc = {
449 .bLength = sizeof union_desc,
450 .bDescriptorType = USB_DT_CS_INTERFACE,
451 .bDescriptorSubType = USB_CDC_UNION_TYPE,
452
453 .bMasterInterface0 = 0, /* index of control interface */
454 .bSlaveInterface0 = 1, /* index of DATA interface */
455};
456
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300457#endif /* CDC || RNDIS */
458
459#ifdef CONFIG_USB_ETH_RNDIS
460
461static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
462 .bLength = sizeof call_mgmt_descriptor,
463 .bDescriptorType = USB_DT_CS_INTERFACE,
464 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
465
466 .bmCapabilities = 0x00,
467 .bDataInterface = 0x01,
468};
469
470static const struct usb_cdc_acm_descriptor acm_descriptor = {
471 .bLength = sizeof acm_descriptor,
472 .bDescriptorType = USB_DT_CS_INTERFACE,
473 .bDescriptorSubType = USB_CDC_ACM_TYPE,
474
475 .bmCapabilities = 0x00,
476};
477
478#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200479
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200480#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200481
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400482/*
483 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmer23cd1382009-07-29 18:18:43 +0200484 * ways: data endpoints live in the control interface, there's no data
485 * interface, and it's not used to talk to a cell phone radio.
486 */
487
488static const struct usb_cdc_mdlm_desc mdlm_desc = {
489 .bLength = sizeof mdlm_desc,
490 .bDescriptorType = USB_DT_CS_INTERFACE,
491 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
492
493 .bcdVersion = __constant_cpu_to_le16(0x0100),
494 .bGUID = {
495 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
496 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
497 },
498};
499
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400500/*
501 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmer23cd1382009-07-29 18:18:43 +0200502 * can't really use its struct. All we do here is say that we're using
503 * the submode of "SAFE" which directly matches the CDC Subset.
504 */
Lokesh Vutla65c389d2017-01-17 08:51:22 +0530505#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200506static const u8 mdlm_detail_desc[] = {
507 6,
508 USB_DT_CS_INTERFACE,
509 USB_CDC_MDLM_DETAIL_TYPE,
510
511 0, /* "SAFE" */
512 0, /* network control capabilities (none) */
513 0, /* network data capabilities ("raw" encapsulation) */
514};
Lokesh Vutla65c389d2017-01-17 08:51:22 +0530515#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200516
517#endif
518
Remy Bohmer23cd1382009-07-29 18:18:43 +0200519static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400520 .bLength = sizeof(ether_desc),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200521 .bDescriptorType = USB_DT_CS_INTERFACE,
522 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
523
524 /* this descriptor actually adds value, surprise! */
525 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400526 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
Bin Mengdda52512018-07-31 02:55:22 -0700527 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400528 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200529 .bNumberPowerFilters = 0,
530};
531
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200532#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200533
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400534/*
535 * include the status endpoint if we can, even where it's optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200536 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
537 * packet, to simplify cancellation; and a big transfer interval, to
538 * waste less bandwidth.
539 *
540 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
541 * if they ignore the connect/disconnect notifications that real aether
542 * can provide. more advanced cdc configurations might want to support
543 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300544 *
545 * RNDIS requires the status endpoint, since it uses that encapsulation
546 * mechanism for its funky RPC scheme.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200547 */
548
549#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
550#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
551
552static struct usb_endpoint_descriptor
553fs_status_desc = {
554 .bLength = USB_DT_ENDPOINT_SIZE,
555 .bDescriptorType = USB_DT_ENDPOINT,
556
557 .bEndpointAddress = USB_DIR_IN,
558 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400559 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200560 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
561};
562#endif
563
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200564#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200565
566/* the default data interface has no endpoints ... */
567
568static const struct usb_interface_descriptor
569data_nop_intf = {
570 .bLength = sizeof data_nop_intf,
571 .bDescriptorType = USB_DT_INTERFACE,
572
573 .bInterfaceNumber = 1,
574 .bAlternateSetting = 0,
575 .bNumEndpoints = 0,
576 .bInterfaceClass = USB_CLASS_CDC_DATA,
577 .bInterfaceSubClass = 0,
578 .bInterfaceProtocol = 0,
579};
580
581/* ... but the "real" data interface has two bulk endpoints */
582
583static const struct usb_interface_descriptor
584data_intf = {
585 .bLength = sizeof data_intf,
586 .bDescriptorType = USB_DT_INTERFACE,
587
588 .bInterfaceNumber = 1,
589 .bAlternateSetting = 1,
590 .bNumEndpoints = 2,
591 .bInterfaceClass = USB_CLASS_CDC_DATA,
592 .bInterfaceSubClass = 0,
593 .bInterfaceProtocol = 0,
594 .iInterface = STRING_DATA,
595};
596
597#endif
598
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300599#ifdef CONFIG_USB_ETH_RNDIS
600
601/* RNDIS doesn't activate by changing to the "real" altsetting */
602
603static const struct usb_interface_descriptor
604rndis_data_intf = {
605 .bLength = sizeof rndis_data_intf,
606 .bDescriptorType = USB_DT_INTERFACE,
607
608 .bInterfaceNumber = 1,
609 .bAlternateSetting = 0,
610 .bNumEndpoints = 2,
611 .bInterfaceClass = USB_CLASS_CDC_DATA,
612 .bInterfaceSubClass = 0,
613 .bInterfaceProtocol = 0,
614 .iInterface = STRING_DATA,
615};
616
617#endif
618
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200619#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200620
621/*
622 * "Simple" CDC-subset option is a simple vendor-neutral model that most
623 * full speed controllers can handle: one interface, two bulk endpoints.
624 *
625 * To assist host side drivers, we fancy it up a bit, and add descriptors
626 * so some host side drivers will understand it as a "SAFE" variant.
627 */
628
629static const struct usb_interface_descriptor
630subset_data_intf = {
631 .bLength = sizeof subset_data_intf,
632 .bDescriptorType = USB_DT_INTERFACE,
633
634 .bInterfaceNumber = 0,
635 .bAlternateSetting = 0,
636 .bNumEndpoints = 2,
637 .bInterfaceClass = USB_CLASS_COMM,
638 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
639 .bInterfaceProtocol = 0,
640 .iInterface = STRING_DATA,
641};
642
643#endif /* SUBSET */
644
Remy Bohmer23cd1382009-07-29 18:18:43 +0200645static struct usb_endpoint_descriptor
646fs_source_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
649
650 .bEndpointAddress = USB_DIR_IN,
651 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700652 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200653};
654
655static struct usb_endpoint_descriptor
656fs_sink_desc = {
657 .bLength = USB_DT_ENDPOINT_SIZE,
658 .bDescriptorType = USB_DT_ENDPOINT,
659
660 .bEndpointAddress = USB_DIR_OUT,
661 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700662 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200663};
664
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400665static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200666 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200667#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200668 /* "cdc" mode descriptors */
669 (struct usb_descriptor_header *) &control_intf,
670 (struct usb_descriptor_header *) &header_desc,
671 (struct usb_descriptor_header *) &union_desc,
672 (struct usb_descriptor_header *) &ether_desc,
673 /* NOTE: status endpoint may need to be removed */
674 (struct usb_descriptor_header *) &fs_status_desc,
675 /* data interface, with altsetting */
676 (struct usb_descriptor_header *) &data_nop_intf,
677 (struct usb_descriptor_header *) &data_intf,
678 (struct usb_descriptor_header *) &fs_source_desc,
679 (struct usb_descriptor_header *) &fs_sink_desc,
680 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200681#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200682};
683
684static inline void fs_subset_descriptors(void)
685{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200686#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200687 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
688 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
689 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
690 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
691 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
692 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
693 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
694 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
695 fs_eth_function[8] = NULL;
696#else
697 fs_eth_function[1] = NULL;
698#endif
699}
700
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300701#ifdef CONFIG_USB_ETH_RNDIS
702static const struct usb_descriptor_header *fs_rndis_function[] = {
703 (struct usb_descriptor_header *) &otg_descriptor,
704 /* control interface matches ACM, not Ethernet */
705 (struct usb_descriptor_header *) &rndis_control_intf,
706 (struct usb_descriptor_header *) &header_desc,
707 (struct usb_descriptor_header *) &call_mgmt_descriptor,
708 (struct usb_descriptor_header *) &acm_descriptor,
709 (struct usb_descriptor_header *) &union_desc,
710 (struct usb_descriptor_header *) &fs_status_desc,
711 /* data interface has no altsetting */
712 (struct usb_descriptor_header *) &rndis_data_intf,
713 (struct usb_descriptor_header *) &fs_source_desc,
714 (struct usb_descriptor_header *) &fs_sink_desc,
715 NULL,
716};
717#endif
718
Remy Bohmer23cd1382009-07-29 18:18:43 +0200719/*
720 * usb 2.0 devices need to expose both high speed and full speed
721 * descriptors, unless they only run at full speed.
722 */
723
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200724#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200725static struct usb_endpoint_descriptor
726hs_status_desc = {
727 .bLength = USB_DT_ENDPOINT_SIZE,
728 .bDescriptorType = USB_DT_ENDPOINT,
729
730 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400731 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200732 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
733};
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200734#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200735
736static struct usb_endpoint_descriptor
737hs_source_desc = {
738 .bLength = USB_DT_ENDPOINT_SIZE,
739 .bDescriptorType = USB_DT_ENDPOINT,
740
741 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400742 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200743};
744
745static struct usb_endpoint_descriptor
746hs_sink_desc = {
747 .bLength = USB_DT_ENDPOINT_SIZE,
748 .bDescriptorType = USB_DT_ENDPOINT,
749
750 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400751 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200752};
753
754static struct usb_qualifier_descriptor
755dev_qualifier = {
756 .bLength = sizeof dev_qualifier,
757 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
758
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400759 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200760 .bDeviceClass = USB_CLASS_COMM,
761
762 .bNumConfigurations = 1,
763};
764
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400765static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200766 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200767#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200768 /* "cdc" mode descriptors */
769 (struct usb_descriptor_header *) &control_intf,
770 (struct usb_descriptor_header *) &header_desc,
771 (struct usb_descriptor_header *) &union_desc,
772 (struct usb_descriptor_header *) &ether_desc,
773 /* NOTE: status endpoint may need to be removed */
774 (struct usb_descriptor_header *) &hs_status_desc,
775 /* data interface, with altsetting */
776 (struct usb_descriptor_header *) &data_nop_intf,
777 (struct usb_descriptor_header *) &data_intf,
778 (struct usb_descriptor_header *) &hs_source_desc,
779 (struct usb_descriptor_header *) &hs_sink_desc,
780 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200781#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200782};
783
784static inline void hs_subset_descriptors(void)
785{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200786#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200787 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
788 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
789 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
790 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
791 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
792 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
793 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
794 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
795 hs_eth_function[8] = NULL;
796#else
797 hs_eth_function[1] = NULL;
798#endif
799}
800
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300801#ifdef CONFIG_USB_ETH_RNDIS
802static const struct usb_descriptor_header *hs_rndis_function[] = {
803 (struct usb_descriptor_header *) &otg_descriptor,
804 /* control interface matches ACM, not Ethernet */
805 (struct usb_descriptor_header *) &rndis_control_intf,
806 (struct usb_descriptor_header *) &header_desc,
807 (struct usb_descriptor_header *) &call_mgmt_descriptor,
808 (struct usb_descriptor_header *) &acm_descriptor,
809 (struct usb_descriptor_header *) &union_desc,
810 (struct usb_descriptor_header *) &hs_status_desc,
811 /* data interface has no altsetting */
812 (struct usb_descriptor_header *) &rndis_data_intf,
813 (struct usb_descriptor_header *) &hs_source_desc,
814 (struct usb_descriptor_header *) &hs_sink_desc,
815 NULL,
816};
817#endif
818
819
Remy Bohmer23cd1382009-07-29 18:18:43 +0200820/* maxpacket and other transfer characteristics vary by speed. */
821static inline struct usb_endpoint_descriptor *
822ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
823 struct usb_endpoint_descriptor *fs)
824{
825 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
826 return hs;
827 return fs;
828}
829
Remy Bohmer23cd1382009-07-29 18:18:43 +0200830/*-------------------------------------------------------------------------*/
831
832/* descriptors that are built on-demand */
833
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400834static char manufacturer[50];
835static char product_desc[40] = DRIVER_DESC;
836static char serial_number[20];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200837
838/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400839static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200840
841/* static strings, in UTF-8 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400842static struct usb_string strings[] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200843 { STRING_MANUFACTURER, manufacturer, },
844 { STRING_PRODUCT, product_desc, },
845 { STRING_SERIALNUMBER, serial_number, },
846 { STRING_DATA, "Ethernet Data", },
847 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200848#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200849 { STRING_CDC, "CDC Ethernet", },
850 { STRING_CONTROL, "CDC Communications Control", },
851#endif
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200852#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200853 { STRING_SUBSET, "CDC Ethernet Subset", },
854#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300855#ifdef CONFIG_USB_ETH_RNDIS
856 { STRING_RNDIS, "RNDIS", },
857 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
858#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200859 { } /* end of list */
860};
861
862static struct usb_gadget_strings stringtab = {
863 .language = 0x0409, /* en-us */
864 .strings = strings,
865};
866
Remy Bohmer23cd1382009-07-29 18:18:43 +0200867/*============================================================================*/
Joel Fernandes52907592013-09-04 18:55:14 -0500868DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
869
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200870#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes52907592013-09-04 18:55:14 -0500871DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek563aed22012-10-02 17:04:29 +0200872#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200873
Remy Bohmer23cd1382009-07-29 18:18:43 +0200874/*============================================================================*/
875
876/*
877 * one config, two interfaces: control, data.
878 * complications: class descriptors, and an altsetting.
879 */
880static int
881config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
882{
883 int len;
884 const struct usb_config_descriptor *config;
885 const struct usb_descriptor_header **function;
886 int hs = 0;
887
888 if (gadget_is_dualspeed(g)) {
889 hs = (g->speed == USB_SPEED_HIGH);
890 if (type == USB_DT_OTHER_SPEED_CONFIG)
891 hs = !hs;
892 }
893#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
894
895 if (index >= device_desc.bNumConfigurations)
896 return -EINVAL;
897
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300898#ifdef CONFIG_USB_ETH_RNDIS
899 /*
900 * list the RNDIS config first, to make Microsoft's drivers
901 * happy. DOCSIS 1.0 needs this too.
902 */
903 if (device_desc.bNumConfigurations == 2 && index == 0) {
904 config = &rndis_config;
905 function = which_fn(rndis);
906 } else
907#endif
908 {
909 config = &eth_config;
910 function = which_fn(eth);
911 }
Remy Bohmer23cd1382009-07-29 18:18:43 +0200912
913 /* for now, don't advertise srp-only devices */
914 if (!is_otg)
915 function++;
916
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400917 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200918 if (len < 0)
919 return len;
920 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
921 return len;
922}
923
924/*-------------------------------------------------------------------------*/
925
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300926static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400927static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200928
929static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400930set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200931{
932 int result = 0;
933 struct usb_gadget *gadget = dev->gadget;
934
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200935#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300936 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200937 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400938 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmer23cd1382009-07-29 18:18:43 +0200939 &fs_status_desc);
940 dev->status_ep->driver_data = dev;
941
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400942 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200943 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400944 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200945 dev->status_ep->name, result);
946 goto done;
947 }
948 }
949#endif
950
951 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
952 dev->in_ep->driver_data = dev;
953
954 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
955 dev->out_ep->driver_data = dev;
956
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400957 /*
958 * With CDC, the host isn't allowed to use these two data
Remy Bohmer23cd1382009-07-29 18:18:43 +0200959 * endpoints in the default altsetting for the interface.
960 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300961 *
962 * Strictly speaking RNDIS should work the same: activation is
963 * a side effect of setting a packet filter. Deactivation is
964 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200965 */
966 if (!cdc_active(dev)) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400967 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200968 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400969 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200970 dev->in_ep->name, result);
971 goto done;
972 }
973
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400974 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200975 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400976 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200977 dev->out_ep->name, result);
978 goto done;
979 }
980 }
981
982done:
983 if (result == 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400984 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200985
986 /* on error, disable any endpoints */
987 if (result < 0) {
Vitaly Kuzmichevd5292c12010-08-13 17:02:29 +0400988 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400989 (void) usb_ep_disable(dev->status_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200990 dev->status = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400991 (void) usb_ep_disable(dev->in_ep);
992 (void) usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200993 dev->in = NULL;
994 dev->out = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300995 } else if (!cdc_active(dev)) {
996 /*
997 * activate non-CDC configs right away
998 * this isn't strictly according to the RNDIS spec
999 */
1000 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001001 }
1002
1003 /* caller is responsible for cleanup on error */
1004 return result;
1005}
1006
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001007static void eth_reset_config(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001008{
1009 if (dev->config == 0)
1010 return;
1011
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001012 debug("%s\n", __func__);
1013
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001014 rndis_uninit(dev->rndis_config);
1015
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001016 /*
1017 * disable endpoints, forcing (synchronous) completion of
Remy Bohmer23cd1382009-07-29 18:18:43 +02001018 * pending i/o. then free the requests.
1019 */
1020
1021 if (dev->in) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001022 usb_ep_disable(dev->in_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001023 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001024 usb_ep_free_request(dev->in_ep, dev->tx_req);
1025 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001026 }
1027 }
1028 if (dev->out) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001029 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001030 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001031 usb_ep_free_request(dev->out_ep, dev->rx_req);
1032 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001033 }
1034 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001035 if (dev->status)
1036 usb_ep_disable(dev->status_ep);
1037
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001038 dev->rndis = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001039 dev->cdc_filter = 0;
1040 dev->config = 0;
1041}
1042
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001043/*
1044 * change our operational config. must agree with the code
Remy Bohmer23cd1382009-07-29 18:18:43 +02001045 * that returns config descriptors, and altsetting code.
1046 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001047static int eth_set_config(struct eth_dev *dev, unsigned number,
1048 gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001049{
1050 int result = 0;
1051 struct usb_gadget *gadget = dev->gadget;
1052
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001053 if (gadget_is_sa1100(gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001054 && dev->config
1055 && dev->tx_qlen != 0) {
1056 /* tx fifo is full, but we can't clear it...*/
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001057 pr_err("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001058 return -ESPIPE;
1059 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001060 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001061
1062 switch (number) {
1063 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001064 result = set_ether_config(dev, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001065 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001066#ifdef CONFIG_USB_ETH_RNDIS
1067 case DEV_RNDIS_CONFIG_VALUE:
1068 dev->rndis = 1;
1069 result = set_ether_config(dev, gfp_flags);
1070 break;
1071#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02001072 default:
1073 result = -EINVAL;
1074 /* FALL THROUGH */
1075 case 0:
1076 break;
1077 }
1078
1079 if (result) {
1080 if (number)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001081 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001082 usb_gadget_vbus_draw(dev->gadget,
1083 gadget_is_otg(dev->gadget) ? 8 : 100);
1084 } else {
1085 char *speed;
1086 unsigned power;
1087
1088 power = 2 * eth_config.bMaxPower;
1089 usb_gadget_vbus_draw(dev->gadget, power);
1090
1091 switch (gadget->speed) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001092 case USB_SPEED_FULL:
1093 speed = "full"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001094#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001095 case USB_SPEED_HIGH:
1096 speed = "high"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001097#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001098 default:
1099 speed = "?"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001100 }
1101
1102 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001103 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001104 speed, number, power, driver_desc,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001105 rndis_active(dev)
1106 ? "RNDIS"
1107 : (cdc_active(dev)
1108 ? "CDC Ethernet"
Remy Bohmer23cd1382009-07-29 18:18:43 +02001109 : "CDC Ethernet Subset"));
1110 }
1111 return result;
1112}
1113
1114/*-------------------------------------------------------------------------*/
1115
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001116#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001117
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001118/*
1119 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001120 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001121 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmer23cd1382009-07-29 18:18:43 +02001122 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1123 * command mechanism; and only one status request is ever queued.
1124 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001125static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001126{
1127 struct usb_cdc_notification *event = req->buf;
1128 int value = req->status;
1129 struct eth_dev *dev = ep->driver_data;
1130
1131 /* issue the second notification if host reads the first */
1132 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1133 && value == 0) {
1134 __le32 *data = req->buf + sizeof *event;
1135
1136 event->bmRequestType = 0xA1;
1137 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001138 event->wValue = __constant_cpu_to_le16(0);
1139 event->wIndex = __constant_cpu_to_le16(1);
1140 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001141
1142 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001143 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmer23cd1382009-07-29 18:18:43 +02001144
1145 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001146 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001147 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001148 if (value == 0)
1149 return;
1150 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001151 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001152 event->bNotificationType, value);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001153 if (event->bNotificationType ==
1154 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N17b4f302016-11-18 10:49:13 +05301155 dev->network_started = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001156 printf("USB network up!\n");
1157 }
1158 }
1159 req->context = NULL;
1160}
1161
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001162static void issue_start_status(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001163{
1164 struct usb_request *req = dev->stat_req;
1165 struct usb_cdc_notification *event;
1166 int value;
1167
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001168 /*
1169 * flush old status
Remy Bohmer23cd1382009-07-29 18:18:43 +02001170 *
1171 * FIXME ugly idiom, maybe we'd be better with just
1172 * a "cancel the whole queue" primitive since any
1173 * unlink-one primitive has way too many error modes.
1174 * here, we "know" toggle is already clear...
1175 *
1176 * FIXME iff req->context != null just dequeue it
1177 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001178 usb_ep_disable(dev->status_ep);
1179 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001180
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001181 /*
1182 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmer23cd1382009-07-29 18:18:43 +02001183 * a SPEED_CHANGE. could be useful in some configs.
1184 */
1185 event = req->buf;
1186 event->bmRequestType = 0xA1;
1187 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001188 event->wValue = __constant_cpu_to_le16(1); /* connected */
1189 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001190 event->wLength = 0;
1191
1192 req->length = sizeof *event;
1193 req->complete = eth_status_complete;
1194 req->context = dev;
1195
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001196 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001197 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001198 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001199}
1200
1201#endif
1202
1203/*-------------------------------------------------------------------------*/
1204
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001205static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001206{
1207 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001208 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001209 req->status, req->actual, req->length);
1210}
1211
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001212#ifdef CONFIG_USB_ETH_RNDIS
1213
1214static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1215{
1216 if (req->status || req->actual != req->length)
1217 debug("rndis response complete --> %d, %d/%d\n",
1218 req->status, req->actual, req->length);
1219
1220 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1221}
1222
1223static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1224{
1225 struct eth_dev *dev = ep->driver_data;
1226 int status;
1227
1228 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1229 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1230 if (status < 0)
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001231 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001232}
1233
1234#endif /* RNDIS */
1235
Remy Bohmer23cd1382009-07-29 18:18:43 +02001236/*
1237 * The setup() callback implements all the ep0 functionality that's not
1238 * handled lower down. CDC has a number of less-common features:
1239 *
1240 * - two interfaces: control, and ethernet data
1241 * - Ethernet data interface has two altsettings: default, and active
1242 * - class-specific descriptors for the control interface
1243 * - class-specific control requests
1244 */
1245static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001246eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001247{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001248 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001249 struct usb_request *req = dev->req;
1250 int value = -EOPNOTSUPP;
1251 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1252 u16 wValue = le16_to_cpu(ctrl->wValue);
1253 u16 wLength = le16_to_cpu(ctrl->wLength);
1254
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001255 /*
1256 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001257 * while config change events may enable network traffic.
1258 */
1259
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001260 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001261
1262 req->complete = eth_setup_complete;
1263 switch (ctrl->bRequest) {
1264
1265 case USB_REQ_GET_DESCRIPTOR:
1266 if (ctrl->bRequestType != USB_DIR_IN)
1267 break;
1268 switch (wValue >> 8) {
1269
1270 case USB_DT_DEVICE:
Kishon Vijay Abraham I04afd5b2015-02-23 18:40:17 +05301271 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001272 value = min(wLength, (u16) sizeof device_desc);
1273 memcpy(req->buf, &device_desc, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001274 break;
1275 case USB_DT_DEVICE_QUALIFIER:
1276 if (!gadget_is_dualspeed(gadget))
1277 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001278 value = min(wLength, (u16) sizeof dev_qualifier);
1279 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001280 break;
1281
1282 case USB_DT_OTHER_SPEED_CONFIG:
1283 if (!gadget_is_dualspeed(gadget))
1284 break;
1285 /* FALLTHROUGH */
1286 case USB_DT_CONFIG:
1287 value = config_buf(gadget, req->buf,
1288 wValue >> 8,
1289 wValue & 0xff,
1290 gadget_is_otg(gadget));
1291 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001292 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001293 break;
1294
1295 case USB_DT_STRING:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001296 value = usb_gadget_get_string(&stringtab,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001297 wValue & 0xff, req->buf);
1298
1299 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001300 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001301
1302 break;
1303 }
1304 break;
1305
1306 case USB_REQ_SET_CONFIGURATION:
1307 if (ctrl->bRequestType != 0)
1308 break;
1309 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001310 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001311 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001312 debug("HNP needs a different root port\n");
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001313 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001314 break;
1315 case USB_REQ_GET_CONFIGURATION:
1316 if (ctrl->bRequestType != USB_DIR_IN)
1317 break;
1318 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001319 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001320 break;
1321
1322 case USB_REQ_SET_INTERFACE:
1323 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1324 || !dev->config
1325 || wIndex > 1)
1326 break;
1327 if (!cdc_active(dev) && wIndex != 0)
1328 break;
1329
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001330 /*
1331 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001332 * we need to kluge around that interference.
1333 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001334 if (gadget_is_pxa(gadget)) {
1335 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001336 GFP_ATOMIC);
Lukasz Dalek563aed22012-10-02 17:04:29 +02001337 /*
1338 * PXA25x driver use non-CDC ethernet gadget.
1339 * But only _CDC and _RNDIS code can signalize
1340 * that network is working. So we signalize it
1341 * here.
1342 */
Mugunthan V N17b4f302016-11-18 10:49:13 +05301343 dev->network_started = 1;
Lukasz Dalek563aed22012-10-02 17:04:29 +02001344 debug("USB network up!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001345 goto done_set_intf;
1346 }
1347
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001348#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001349 switch (wIndex) {
1350 case 0: /* control/master intf */
1351 if (wValue != 0)
1352 break;
1353 if (dev->status) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001354 usb_ep_disable(dev->status_ep);
1355 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001356 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001357
Remy Bohmer23cd1382009-07-29 18:18:43 +02001358 value = 0;
1359 break;
1360 case 1: /* data intf */
1361 if (wValue > 1)
1362 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001363 usb_ep_disable(dev->in_ep);
1364 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001365
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001366 /*
1367 * CDC requires the data transfers not be done from
Remy Bohmer23cd1382009-07-29 18:18:43 +02001368 * the default interface setting ... also, setting
1369 * the non-default interface resets filters etc.
1370 */
1371 if (wValue == 1) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001372 if (!cdc_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001373 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001374 usb_ep_enable(dev->in_ep, dev->in);
1375 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001376 dev->cdc_filter = DEFAULT_FILTER;
1377 if (dev->status)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001378 issue_start_status(dev);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001379 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001380 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001381 value = 0;
1382 break;
1383 }
1384#else
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001385 /*
1386 * FIXME this is wrong, as is the assumption that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001387 * all non-PXA hardware talks real CDC ...
1388 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001389 debug("set_interface ignored!\n");
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001390#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001391
1392done_set_intf:
1393 break;
1394 case USB_REQ_GET_INTERFACE:
1395 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1396 || !dev->config
1397 || wIndex > 1)
1398 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001399 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001400 break;
1401
1402 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001403 if (rndis_active(dev) || wIndex != 1)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001404 *(u8 *)req->buf = 0;
1405 else {
1406 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1407 /* carrier always ok ...*/
1408 *(u8 *)req->buf = 1 ;
1409 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001410 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001411 break;
1412
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001413#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001414 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001415 /*
1416 * see 6.2.30: no data, wIndex = interface,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001417 * wValue = packet filter bitmap
1418 */
1419 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1420 || !cdc_active(dev)
1421 || wLength != 0
1422 || wIndex > 1)
1423 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001424 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001425 dev->cdc_filter = wValue;
1426 value = 0;
1427 break;
1428
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001429 /*
1430 * and potentially:
Remy Bohmer23cd1382009-07-29 18:18:43 +02001431 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1432 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1433 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1434 * case USB_CDC_GET_ETHERNET_STATISTIC:
1435 */
1436
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001437#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001438
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001439#ifdef CONFIG_USB_ETH_RNDIS
1440 /*
1441 * RNDIS uses the CDC command encapsulation mechanism to implement
1442 * an RPC scheme, with much getting/setting of attributes by OID.
1443 */
1444 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1445 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1446 || !rndis_active(dev)
1447 || wLength > USB_BUFSIZ
1448 || wValue
1449 || rndis_control_intf.bInterfaceNumber
1450 != wIndex)
1451 break;
1452 /* read the request, then process it */
1453 value = wLength;
1454 req->complete = rndis_command_complete;
1455 /* later, rndis_control_ack () sends a notification */
1456 break;
1457
1458 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1459 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1460 == ctrl->bRequestType
1461 && rndis_active(dev)
1462 /* && wLength >= 0x0400 */
1463 && !wValue
1464 && rndis_control_intf.bInterfaceNumber
1465 == wIndex) {
1466 u8 *buf;
1467 u32 n;
1468
1469 /* return the result */
1470 buf = rndis_get_next_response(dev->rndis_config, &n);
1471 if (buf) {
1472 memcpy(req->buf, buf, n);
1473 req->complete = rndis_response_complete;
1474 rndis_free_response(dev->rndis_config, buf);
1475 value = n;
1476 }
1477 /* else stalls ... spec says to avoid that */
1478 }
1479 break;
1480#endif /* RNDIS */
1481
Remy Bohmer23cd1382009-07-29 18:18:43 +02001482 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001483 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001484 ctrl->bRequestType, ctrl->bRequest,
1485 wValue, wIndex, wLength);
1486 }
1487
1488 /* respond with data transfer before status phase? */
1489 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001490 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001491 req->length = value;
1492 req->zero = value < wLength
1493 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001494 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001495 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001496 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001497 req->status = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001498 eth_setup_complete(gadget->ep0, req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001499 }
1500 }
1501
1502 /* host either stalls (value < 0) or reports success */
1503 return value;
1504}
1505
Remy Bohmer23cd1382009-07-29 18:18:43 +02001506/*-------------------------------------------------------------------------*/
1507
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001508static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001509
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001510static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001511 gfp_t gfp_flags)
1512{
1513 int retval = -ENOMEM;
1514 size_t size;
1515
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001516 /*
1517 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001518 * Normally we use the USB "terminate on short read" convention;
1519 * so allow up to (N*maxpacket), since that memory is normally
1520 * already allocated. Some hardware doesn't deal well with short
1521 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1522 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001523 *
1524 * RNDIS uses internal framing, and explicitly allows senders to
1525 * pad to end-of-packet. That's potentially nice for speed,
1526 * but means receivers can't recover synch on their own.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001527 */
1528
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001529 debug("%s\n", __func__);
Troy Kisky71fc5f92013-09-25 18:41:05 -07001530 if (!req)
1531 return -EINVAL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001532
1533 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1534 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001535 if (rndis_active(dev))
1536 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001537 size -= size % dev->out_ep->maxpacket;
1538
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001539 /*
1540 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001541 * but on at least one, checksumming fails otherwise. Note:
1542 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001543 */
1544
Joe Hershberger1fd92db2015-04-08 01:41:06 -05001545 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmer23cd1382009-07-29 18:18:43 +02001546 req->length = size;
1547 req->complete = rx_complete;
1548
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001549 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001550
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001551 if (retval)
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001552 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001553
Remy Bohmer23cd1382009-07-29 18:18:43 +02001554 return retval;
1555}
1556
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001557static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001558{
1559 struct eth_dev *dev = ep->driver_data;
1560
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001561 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001562 switch (req->status) {
1563 /* normal completion */
1564 case 0:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001565 if (rndis_active(dev)) {
1566 /* we know MaxPacketsPerTransfer == 1 here */
1567 int length = rndis_rm_hdr(req->buf, req->actual);
1568 if (length < 0)
1569 goto length_err;
1570 req->length -= length;
1571 req->actual -= length;
1572 }
Bin Mengdda52512018-07-31 02:55:22 -07001573 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001574length_err:
1575 dev->stats.rx_errors++;
1576 dev->stats.rx_length_errors++;
1577 debug("rx length %d\n", req->length);
1578 break;
1579 }
1580
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001581 dev->stats.rx_packets++;
1582 dev->stats.rx_bytes += req->length;
1583 break;
1584
1585 /* software-driven interface shutdown */
1586 case -ECONNRESET: /* unlink */
1587 case -ESHUTDOWN: /* disconnect etc */
1588 /* for hardware automagic (such as pxa) */
1589 case -ECONNABORTED: /* endpoint reset */
1590 break;
1591
1592 /* data overrun */
1593 case -EOVERFLOW:
1594 dev->stats.rx_over_errors++;
1595 /* FALLTHROUGH */
1596 default:
1597 dev->stats.rx_errors++;
1598 break;
1599 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001600
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001601 packet_received = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001602}
1603
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001604static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001605{
1606
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001607 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001608
1609 if (!dev->tx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001610 goto fail1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001611
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001612 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001613
1614 if (!dev->rx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001615 goto fail2;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001616
1617 return 0;
1618
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001619fail2:
1620 usb_ep_free_request(dev->in_ep, dev->tx_req);
1621fail1:
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001622 pr_err("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001623 return -1;
1624}
1625
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001626static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001627{
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001628 struct eth_dev *dev = ep->driver_data;
1629
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001630 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001631 switch (req->status) {
1632 default:
1633 dev->stats.tx_errors++;
1634 debug("tx err %d\n", req->status);
1635 /* FALLTHROUGH */
1636 case -ECONNRESET: /* unlink */
1637 case -ESHUTDOWN: /* disconnect etc */
1638 break;
1639 case 0:
1640 dev->stats.tx_bytes += req->length;
1641 }
1642 dev->stats.tx_packets++;
1643
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001644 packet_sent = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001645}
1646
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001647static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001648{
1649 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001650 if (subset_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001651 return 1;
1652 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1653}
1654
1655#if 0
1656static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1657{
1658 struct eth_dev *dev = netdev_priv(net);
1659 int length = skb->len;
1660 int retval;
1661 struct usb_request *req = NULL;
1662 unsigned long flags;
1663
1664 /* apply outgoing CDC or RNDIS filters */
1665 if (!eth_is_promisc (dev)) {
1666 u8 *dest = skb->data;
1667
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001668 if (is_multicast_ethaddr(dest)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02001669 u16 type;
1670
1671 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1672 * SET_ETHERNET_MULTICAST_FILTERS requests
1673 */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001674 if (is_broadcast_ethaddr(dest))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001675 type = USB_CDC_PACKET_TYPE_BROADCAST;
1676 else
1677 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1678 if (!(dev->cdc_filter & type)) {
1679 dev_kfree_skb_any (skb);
1680 return 0;
1681 }
1682 }
1683 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1684 }
1685
1686 spin_lock_irqsave(&dev->req_lock, flags);
1687 /*
1688 * this freelist can be empty if an interrupt triggered disconnect()
1689 * and reconfigured the gadget (shutting down this queue) after the
1690 * network stack decided to xmit but before we got the spinlock.
1691 */
1692 if (list_empty(&dev->tx_reqs)) {
1693 spin_unlock_irqrestore(&dev->req_lock, flags);
1694 return 1;
1695 }
1696
1697 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1698 list_del (&req->list);
1699
1700 /* temporarily stop TX queue when the freelist empties */
1701 if (list_empty (&dev->tx_reqs))
1702 netif_stop_queue (net);
1703 spin_unlock_irqrestore(&dev->req_lock, flags);
1704
1705 /* no buffer copies needed, unless the network stack did it
1706 * or the hardware can't use skb buffers.
1707 * or there's not enough space for any RNDIS headers we need
1708 */
1709 if (rndis_active(dev)) {
1710 struct sk_buff *skb_rndis;
1711
1712 skb_rndis = skb_realloc_headroom (skb,
1713 sizeof (struct rndis_packet_msg_type));
1714 if (!skb_rndis)
1715 goto drop;
1716
1717 dev_kfree_skb_any (skb);
1718 skb = skb_rndis;
1719 rndis_add_hdr (skb);
1720 length = skb->len;
1721 }
1722 req->buf = skb->data;
1723 req->context = skb;
1724 req->complete = tx_complete;
1725
1726 /* use zlp framing on tx for strict CDC-Ether conformance,
1727 * though any robust network rx path ignores extra padding.
1728 * and some hardware doesn't like to write zlps.
1729 */
1730 req->zero = 1;
1731 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1732 length++;
1733
1734 req->length = length;
1735
1736 /* throttle highspeed IRQ rate back slightly */
1737 if (gadget_is_dualspeed(dev->gadget))
1738 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1739 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1740 : 0;
1741
1742 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1743 switch (retval) {
1744 default:
1745 DEBUG (dev, "tx queue err %d\n", retval);
1746 break;
1747 case 0:
1748 net->trans_start = jiffies;
1749 atomic_inc (&dev->tx_qlen);
1750 }
1751
1752 if (retval) {
1753drop:
1754 dev->stats.tx_dropped++;
1755 dev_kfree_skb_any (skb);
1756 spin_lock_irqsave(&dev->req_lock, flags);
1757 if (list_empty (&dev->tx_reqs))
1758 netif_start_queue (net);
1759 list_add (&req->list, &dev->tx_reqs);
1760 spin_unlock_irqrestore(&dev->req_lock, flags);
1761 }
1762 return 0;
1763}
1764
1765/*-------------------------------------------------------------------------*/
1766#endif
1767
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001768static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001769{
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001770 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001771
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001772 debug("%s...\n", __func__);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001773 rndis_deregister(dev->rndis_config);
1774 rndis_exit();
Remy Bohmer23cd1382009-07-29 18:18:43 +02001775
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001776 /* we've already been disconnected ... no i/o is active */
1777 if (dev->req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001778 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001779 dev->req = NULL;
1780 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001781 if (dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001782 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001783 dev->stat_req = NULL;
1784 }
1785
1786 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001787 usb_ep_free_request(dev->in_ep, dev->tx_req);
1788 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001789 }
1790
1791 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001792 usb_ep_free_request(dev->out_ep, dev->rx_req);
1793 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001794 }
1795
1796/* unregister_netdev (dev->net);*/
1797/* free_netdev(dev->net);*/
1798
Lei Wen988ee3e2010-12-01 23:43:43 +08001799 dev->gadget = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001800 set_gadget_data(gadget, NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001801}
1802
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001803static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001804{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001805 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001806 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001807}
1808
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001809static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001810{
1811 /* Not used */
1812}
1813
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001814static void eth_resume(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001815{
1816 /* Not used */
1817}
1818
1819/*-------------------------------------------------------------------------*/
1820
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001821#ifdef CONFIG_USB_ETH_RNDIS
1822
1823/*
1824 * The interrupt endpoint is used in RNDIS to notify the host when messages
1825 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1826 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1827 * REMOTE_NDIS_KEEPALIVE_MSG.
1828 *
1829 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1830 * normally just one notification will be queued.
1831 */
1832
1833static void rndis_control_ack_complete(struct usb_ep *ep,
1834 struct usb_request *req)
1835{
1836 struct eth_dev *dev = ep->driver_data;
1837
1838 debug("%s...\n", __func__);
1839 if (req->status || req->actual != req->length)
1840 debug("rndis control ack complete --> %d, %d/%d\n",
1841 req->status, req->actual, req->length);
1842
Mugunthan V N17b4f302016-11-18 10:49:13 +05301843 if (!dev->network_started) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001844 if (rndis_get_state(dev->rndis_config)
1845 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N17b4f302016-11-18 10:49:13 +05301846 dev->network_started = 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001847 printf("USB RNDIS network up!\n");
1848 }
1849 }
1850
1851 req->context = NULL;
1852
1853 if (req != dev->stat_req)
1854 usb_ep_free_request(ep, req);
1855}
1856
1857static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1858
Mugunthan V Nd4a37552016-11-18 11:09:15 +05301859#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001860static int rndis_control_ack(struct eth_device *net)
Mugunthan V Nd4a37552016-11-18 11:09:15 +05301861#else
1862static int rndis_control_ack(struct udevice *net)
1863#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001864{
Mugunthan V Nae701002016-11-18 11:07:18 +05301865 struct ether_priv *priv = (struct ether_priv *)net->priv;
1866 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001867 int length;
1868 struct usb_request *resp = dev->stat_req;
1869
1870 /* in case RNDIS calls this after disconnect */
1871 if (!dev->status) {
1872 debug("status ENODEV\n");
1873 return -ENODEV;
1874 }
1875
1876 /* in case queue length > 1 */
1877 if (resp->context) {
1878 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1879 if (!resp)
1880 return -ENOMEM;
1881 resp->buf = rndis_resp_buf;
1882 }
1883
1884 /*
1885 * Send RNDIS RESPONSE_AVAILABLE notification;
1886 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1887 */
1888 resp->length = 8;
1889 resp->complete = rndis_control_ack_complete;
1890 resp->context = dev;
1891
1892 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1893 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1894
1895 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1896 if (length < 0) {
1897 resp->status = 0;
1898 rndis_control_ack_complete(dev->status_ep, resp);
1899 }
1900
1901 return 0;
1902}
1903
1904#else
1905
1906#define rndis_control_ack NULL
1907
1908#endif /* RNDIS */
1909
1910static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1911{
1912 if (rndis_active(dev)) {
1913 rndis_set_param_medium(dev->rndis_config,
1914 NDIS_MEDIUM_802_3,
1915 BITRATE(dev->gadget)/100);
1916 rndis_signal_connect(dev->rndis_config);
1917 }
1918}
1919
1920static int eth_stop(struct eth_dev *dev)
1921{
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001922#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1923 unsigned long ts;
1924 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1925#endif
1926
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001927 if (rndis_active(dev)) {
1928 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1929 rndis_signal_disconnect(dev->rndis_config);
1930
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001931#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1932 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1933 ts = get_timer(0);
1934 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05301935 usb_gadget_handle_interrupts(0);
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001936#endif
1937
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001938 rndis_uninit(dev->rndis_config);
1939 dev->rndis = 0;
1940 }
1941
1942 return 0;
1943}
1944
1945/*-------------------------------------------------------------------------*/
1946
Remy Bohmer23cd1382009-07-29 18:18:43 +02001947static int is_eth_addr_valid(char *str)
1948{
1949 if (strlen(str) == 17) {
1950 int i;
1951 char *p, *q;
1952 uchar ea[6];
1953
1954 /* see if it looks like an ethernet address */
1955
1956 p = str;
1957
1958 for (i = 0; i < 6; i++) {
1959 char term = (i == 5 ? '\0' : ':');
1960
1961 ea[i] = simple_strtol(p, &q, 16);
1962
1963 if ((q - p) != 2 || *q++ != term)
1964 break;
1965
1966 p = q;
1967 }
1968
Tom Rini57a87a22012-10-31 13:30:41 +00001969 /* Now check the contents. */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001970 return is_valid_ethaddr(ea);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001971 }
1972 return 0;
1973}
1974
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001975static u8 nibble(unsigned char c)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001976{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001977 if (likely(isdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001978 return c - '0';
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001979 c = toupper(c);
1980 if (likely(isxdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001981 return 10 + c - 'A';
1982 return 0;
1983}
1984
1985static int get_ether_addr(const char *str, u8 *dev_addr)
1986{
1987 if (str) {
1988 unsigned i;
1989
1990 for (i = 0; i < 6; i++) {
1991 unsigned char num;
1992
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001993 if ((*str == '.') || (*str == ':'))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001994 str++;
1995 num = nibble(*str++) << 4;
1996 num |= (nibble(*str++));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001997 dev_addr[i] = num;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001998 }
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001999 if (is_valid_ethaddr(dev_addr))
Remy Bohmer23cd1382009-07-29 18:18:43 +02002000 return 0;
2001 }
2002 return 1;
2003}
2004
2005static int eth_bind(struct usb_gadget *gadget)
2006{
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302007 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002008 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002009 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002010 int status = -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002011 int gcnum;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002012 u8 tmp[7];
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302013#ifdef CONFIG_DM_ETH
2014 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2015#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002016
2017 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002018#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002019 cdc = 0;
2020#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002021#ifndef CONFIG_USB_ETH_RNDIS
2022 rndis = 0;
2023#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002024 /*
2025 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmer23cd1382009-07-29 18:18:43 +02002026 * standard protocol is _strongly_ preferred for interop purposes.
2027 * (By everyone except Microsoft.)
2028 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002029 if (gadget_is_pxa(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002030 /* pxa doesn't support altsettings */
2031 cdc = 0;
2032 } else if (gadget_is_musbhdrc(gadget)) {
2033 /* reduce tx dma overhead by avoiding special cases */
2034 zlp = 0;
2035 } else if (gadget_is_sh(gadget)) {
2036 /* sh doesn't support multiple interfaces or configs */
2037 cdc = 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002038 rndis = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002039 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002040 /* hardware can't write zlps */
2041 zlp = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002042 /*
2043 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmer23cd1382009-07-29 18:18:43 +02002044 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2045 */
2046 cdc = 0;
2047 }
2048
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002049 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002050 if (gcnum >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002051 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002052 else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002053 /*
2054 * can't assume CDC works. don't want to default to
Remy Bohmer23cd1382009-07-29 18:18:43 +02002055 * anything less functional on CDC-capable hardware,
2056 * so we fail in this case.
2057 */
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002058 pr_err("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002059 gadget->name);
2060 return -ENODEV;
2061 }
2062
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002063 /*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002064 * If there's an RNDIS configuration, that's what Windows wants to
2065 * be using ... so use these product IDs here and in the "linux.inf"
2066 * needed to install MSFT drivers. Current Linux kernels will use
2067 * the second configuration if it's CDC Ethernet, and need some help
2068 * to choose the right configuration otherwise.
2069 */
2070 if (rndis) {
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002071#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002072 device_desc.idVendor =
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002073 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002074 device_desc.idProduct =
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002075 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002076#else
2077 device_desc.idVendor =
2078 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2079 device_desc.idProduct =
2080 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2081#endif
2082 sprintf(product_desc, "RNDIS/%s", driver_desc);
2083
2084 /*
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002085 * CDC subset ... recognized by Linux since 2.4.10, but Windows
Remy Bohmer23cd1382009-07-29 18:18:43 +02002086 * drivers aren't widely available. (That may be improved by
2087 * supporting one submode of the "SAFE" variant of MDLM.)
2088 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002089 } else {
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002090#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2091 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2092 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002093#else
2094 if (!cdc) {
2095 device_desc.idVendor =
2096 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2097 device_desc.idProduct =
2098 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2099 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002100#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002101 }
2102 /* support optional vendor/distro customization */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002103 if (bcdDevice)
2104 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2105 if (iManufacturer)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002106 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002107 if (iProduct)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002108 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002109 if (iSerialNumber) {
2110 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev2e12abe2010-08-13 17:00:45 +04002111 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002112 }
2113
2114 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002115 usb_ep_autoconfig_reset(gadget);
2116 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002117 if (!in_ep) {
2118autoconf_fail:
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002119 pr_err("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002120 gadget->name);
2121 return -ENODEV;
2122 }
2123 in_ep->driver_data = in_ep; /* claim */
2124
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002125 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002126 if (!out_ep)
2127 goto autoconf_fail;
2128 out_ep->driver_data = out_ep; /* claim */
2129
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002130#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002131 /*
2132 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmer23cd1382009-07-29 18:18:43 +02002133 * Since some hosts expect one, try to allocate one anyway.
2134 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002135 if (cdc || rndis) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002136 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002137 if (status_ep) {
2138 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002139 } else if (rndis) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002140 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002141 return -ENODEV;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002142#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002143 } else if (cdc) {
2144 control_intf.bNumEndpoints = 0;
2145 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002146#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002147 }
2148 }
2149#endif
2150
2151 /* one config: cdc, else minimal subset */
2152 if (!cdc) {
2153 eth_config.bNumInterfaces = 1;
2154 eth_config.iConfiguration = STRING_SUBSET;
2155
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002156 /*
2157 * use functions to set these up, in case we're built to work
Remy Bohmer23cd1382009-07-29 18:18:43 +02002158 * with multiple controllers and must override CDC Ethernet.
2159 */
2160 fs_subset_descriptors();
2161 hs_subset_descriptors();
2162 }
2163
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002164 usb_gadget_set_selfpowered(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002165
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002166 /* For now RNDIS is always a second config */
2167 if (rndis)
2168 device_desc.bNumConfigurations = 2;
2169
Remy Bohmer23cd1382009-07-29 18:18:43 +02002170 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002171 if (rndis)
2172 dev_qualifier.bNumConfigurations = 2;
2173 else if (!cdc)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002174 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2175
2176 /* assumes ep0 uses the same value for both speeds ... */
2177 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2178
2179 /* and that all endpoints are dual-speed */
2180 hs_source_desc.bEndpointAddress =
2181 fs_source_desc.bEndpointAddress;
2182 hs_sink_desc.bEndpointAddress =
2183 fs_sink_desc.bEndpointAddress;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002184#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002185 if (status_ep)
2186 hs_status_desc.bEndpointAddress =
2187 fs_status_desc.bEndpointAddress;
2188#endif
2189 }
2190
2191 if (gadget_is_otg(gadget)) {
2192 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2193 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2194 eth_config.bMaxPower = 4;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002195#ifdef CONFIG_USB_ETH_RNDIS
2196 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2197 rndis_config.bMaxPower = 4;
2198#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002199 }
2200
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002201
2202 /* network device setup */
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302203#ifndef CONFIG_DM_ETH
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302204 dev->net = &l_priv->netdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302205#else
2206 dev->net = l_priv->netdev;
2207#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002208
2209 dev->cdc = cdc;
2210 dev->zlp = zlp;
2211
2212 dev->in_ep = in_ep;
2213 dev->out_ep = out_ep;
2214 dev->status_ep = status_ep;
2215
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302216 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002217 /*
2218 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002219 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmer23cd1382009-07-29 18:18:43 +02002220 * ends up in a persistent config database. It's not clear if
2221 * host side code for the SAFE thing cares -- its original BLAN
2222 * thing didn't, Sharp never assigned those addresses on Zaurii.
2223 */
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302224#ifndef CONFIG_DM_ETH
Remy Bohmer23cd1382009-07-29 18:18:43 +02002225 get_ether_addr(dev_addr, dev->net->enetaddr);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002226 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302227#else
2228 get_ether_addr(dev_addr, pdata->enetaddr);
2229 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2230#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002231
2232 get_ether_addr(host_addr, dev->host_mac);
2233
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002234 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2235 dev->host_mac[0], dev->host_mac[1],
2236 dev->host_mac[2], dev->host_mac[3],
2237 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002238
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002239 if (rndis) {
2240 status = rndis_init();
2241 if (status < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002242 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002243 goto fail;
2244 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002245 }
2246
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002247 /*
2248 * use PKTSIZE (or aligned... from u-boot) and set
2249 * wMaxSegmentSize accordingly
2250 */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002251 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2252
2253 /* preallocate control message data and buffer */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002254 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002255 if (!dev->req)
2256 goto fail;
2257 dev->req->buf = control_req;
2258 dev->req->complete = eth_setup_complete;
2259
2260 /* ... and maybe likewise for status transfer */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002261#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002262 if (dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002263 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2264 GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002265 if (!dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002266 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002267
2268 goto fail;
2269 }
Vitaly Kuzmichevdf559c12010-08-13 17:01:06 +04002270 dev->stat_req->buf = status_req;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002271 dev->stat_req->context = NULL;
2272 }
2273#endif
2274
2275 /* finish hookup to lower layer ... */
2276 dev->gadget = gadget;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002277 set_gadget_data(gadget, dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002278 gadget->ep0->driver_data = dev;
2279
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002280 /*
2281 * two kinds of host-initiated state changes:
Remy Bohmer23cd1382009-07-29 18:18:43 +02002282 * - iff DATA transfer is active, carrier is "on"
2283 * - tx queueing enabled if open *and* carrier is "on"
2284 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002285
2286 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2287 out_ep->name, in_ep->name,
2288 status_ep ? " STATUS " : "",
2289 status_ep ? status_ep->name : ""
2290 );
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302291#ifndef CONFIG_DM_ETH
2292 printf("MAC %pM\n", dev->net->enetaddr);
2293#else
2294 printf("MAC %pM\n", pdata->enetaddr);
2295#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002296
2297 if (cdc || rndis)
2298 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2299 dev->host_mac[0], dev->host_mac[1],
2300 dev->host_mac[2], dev->host_mac[3],
2301 dev->host_mac[4], dev->host_mac[5]);
2302
2303 if (rndis) {
2304 u32 vendorID = 0;
2305
2306 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2307
2308 dev->rndis_config = rndis_register(rndis_control_ack);
2309 if (dev->rndis_config < 0) {
2310fail0:
2311 eth_unbind(gadget);
2312 debug("RNDIS setup failed\n");
2313 status = -ENODEV;
2314 goto fail;
2315 }
2316
2317 /* these set up a lot of the OIDs that RNDIS needs */
2318 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2319 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2320 &dev->stats, &dev->cdc_filter))
2321 goto fail0;
2322 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2323 manufacturer))
2324 goto fail0;
2325 if (rndis_set_param_medium(dev->rndis_config,
2326 NDIS_MEDIUM_802_3, 0))
2327 goto fail0;
2328 printf("RNDIS ready\n");
2329 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002330 return 0;
2331
2332fail:
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002333 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002334 eth_unbind(gadget);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002335 return status;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002336}
2337
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002338/*-------------------------------------------------------------------------*/
2339
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302340#ifdef CONFIG_DM_USB
2341int dm_usb_init(struct eth_dev *e_dev)
2342{
2343 struct udevice *dev = NULL;
2344 int ret;
2345
2346 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2347 if (!dev || ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002348 pr_err("No USB device found\n");
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302349 return -ENODEV;
2350 }
2351
2352 e_dev->usb_udev = dev;
2353
2354 return ret;
2355}
2356#endif
2357
Mugunthan V N8269ee42016-11-18 11:08:27 +05302358static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002359{
Mugunthan V Nae701002016-11-18 11:07:18 +05302360 struct eth_dev *dev = &priv->ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002361 struct usb_gadget *gadget;
2362 unsigned long ts;
2363 unsigned long timeout = USB_CONNECT_TIMEOUT;
2364
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302365#ifdef CONFIG_DM_USB
2366 if (dm_usb_init(dev)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002367 pr_err("USB ether not found\n");
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302368 return -ENODEV;
2369 }
2370#else
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302371 board_usb_init(0, USB_INIT_DEVICE);
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302372#endif
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302373
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002374 /* Configure default mac-addresses for the USB ethernet device */
2375#ifdef CONFIG_USBNET_DEV_ADDR
2376 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2377#endif
2378#ifdef CONFIG_USBNET_HOST_ADDR
2379 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2380#endif
2381 /* Check if the user overruled the MAC addresses */
Simon Glass00caae62017-08-03 12:22:12 -06002382 if (env_get("usbnet_devaddr"))
2383 strlcpy(dev_addr, env_get("usbnet_devaddr"),
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002384 sizeof(dev_addr));
2385
Simon Glass00caae62017-08-03 12:22:12 -06002386 if (env_get("usbnet_hostaddr"))
2387 strlcpy(host_addr, env_get("usbnet_hostaddr"),
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002388 sizeof(host_addr));
2389
2390 if (!is_eth_addr_valid(dev_addr)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002391 pr_err("Need valid 'usbnet_devaddr' to be set");
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002392 goto fail;
2393 }
2394 if (!is_eth_addr_valid(host_addr)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002395 pr_err("Need valid 'usbnet_hostaddr' to be set");
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002396 goto fail;
2397 }
2398
Mugunthan V Nae701002016-11-18 11:07:18 +05302399 priv->eth_driver.speed = DEVSPEED;
2400 priv->eth_driver.bind = eth_bind;
2401 priv->eth_driver.unbind = eth_unbind;
2402 priv->eth_driver.setup = eth_setup;
2403 priv->eth_driver.reset = eth_disconnect;
2404 priv->eth_driver.disconnect = eth_disconnect;
2405 priv->eth_driver.suspend = eth_suspend;
2406 priv->eth_driver.resume = eth_resume;
2407 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen988ee3e2010-12-01 23:43:43 +08002408 goto fail;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002409
2410 dev->network_started = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002411
2412 packet_received = 0;
2413 packet_sent = 0;
2414
2415 gadget = dev->gadget;
2416 usb_gadget_connect(gadget);
2417
Simon Glass00caae62017-08-03 12:22:12 -06002418 if (env_get("cdc_connect_timeout"))
2419 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
Remy Bohmer23cd1382009-07-29 18:18:43 +02002420 NULL, 10) * CONFIG_SYS_HZ;
2421 ts = get_timer(0);
Mugunthan V N17b4f302016-11-18 10:49:13 +05302422 while (!dev->network_started) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002423 /* Handle control-c and timeouts */
2424 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002425 pr_err("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002426 goto fail;
2427 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302428 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002429 }
2430
Vitaly Kuzmichev98fae972010-09-22 13:13:56 +04002431 packet_received = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002432 rx_submit(dev, dev->rx_req, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002433 return 0;
2434fail:
2435 return -1;
2436}
2437
Mugunthan V N8269ee42016-11-18 11:08:27 +05302438static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002439{
2440 int retval;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002441 void *rndis_pkt = NULL;
Mugunthan V Nae701002016-11-18 11:07:18 +05302442 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002443 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002444 unsigned long ts;
2445 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002446
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002447 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002448
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002449 /* new buffer is needed to include RNDIS header */
2450 if (rndis_active(dev)) {
2451 rndis_pkt = malloc(length +
2452 sizeof(struct rndis_packet_msg_type));
2453 if (!rndis_pkt) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002454 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002455 goto drop;
2456 }
2457 rndis_add_hdr(rndis_pkt, length);
2458 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002459 packet, length);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002460 packet = rndis_pkt;
2461 length += sizeof(struct rndis_packet_msg_type);
2462 }
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002463 req->buf = packet;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002464 req->context = NULL;
2465 req->complete = tx_complete;
2466
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002467 /*
2468 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002469 * though any robust network rx path ignores extra padding.
2470 * and some hardware doesn't like to write zlps.
2471 */
2472 req->zero = 1;
2473 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2474 length++;
2475
2476 req->length = length;
2477#if 0
2478 /* throttle highspeed IRQ rate back slightly */
2479 if (gadget_is_dualspeed(dev->gadget))
2480 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2481 ? ((dev->tx_qlen % qmult) != 0) : 0;
2482#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002483 dev->tx_qlen = 1;
2484 ts = get_timer(0);
2485 packet_sent = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002486
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002487 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002488
2489 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002490 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002491 while (!packet_sent) {
2492 if (get_timer(ts) > timeout) {
2493 printf("timeout sending packets to usb ethernet\n");
2494 return -1;
2495 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302496 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002497 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002498 if (rndis_pkt)
2499 free(rndis_pkt);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002500
2501 return 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002502drop:
2503 dev->stats.tx_dropped++;
2504 return -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002505}
2506
Mugunthan V N8269ee42016-11-18 11:08:27 +05302507static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002508{
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302509 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002510
Remy Bohmer23cd1382009-07-29 18:18:43 +02002511 return 0;
2512}
2513
Mugunthan V N8269ee42016-11-18 11:08:27 +05302514void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002515{
Mugunthan V Nae701002016-11-18 11:07:18 +05302516 struct eth_dev *dev = &priv->ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002517
Lei Wen988ee3e2010-12-01 23:43:43 +08002518 /* If the gadget not registered, simple return */
2519 if (!dev->gadget)
2520 return;
2521
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03002522 /*
2523 * Some USB controllers may need additional deinitialization here
2524 * before dropping pull-up (also due to hardware issues).
2525 * For example: unhandled interrupt with status stage started may
2526 * bring the controller to fully broken state (until board reset).
2527 * There are some variants to debug and fix such cases:
2528 * 1) In the case of RNDIS connection eth_stop can perform additional
2529 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2530 * 2) 'pullup' callback in your UDC driver can be improved to perform
2531 * this deinitialization.
2532 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002533 eth_stop(dev);
2534
Remy Bohmer23cd1382009-07-29 18:18:43 +02002535 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002536
2537 /* Clear pending interrupt */
2538 if (dev->network_started) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302539 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002540 dev->network_started = 0;
2541 }
2542
Mugunthan V Nae701002016-11-18 11:07:18 +05302543 usb_gadget_unregister_driver(&priv->eth_driver);
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302544#ifndef CONFIG_DM_USB
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302545 board_usb_cleanup(0, USB_INIT_DEVICE);
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302546#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002547}
2548
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302549#ifndef CONFIG_DM_ETH
Mugunthan V N8269ee42016-11-18 11:08:27 +05302550static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2551{
2552 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2553
2554 return _usb_eth_init(priv);
2555}
2556
2557static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2558{
2559 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2560
2561 return _usb_eth_send(priv, packet, length);
2562}
2563
2564static int usb_eth_recv(struct eth_device *netdev)
2565{
2566 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2567 struct eth_dev *dev = &priv->ethdev;
2568 int ret;
2569
2570 ret = _usb_eth_recv(priv);
2571 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002572 pr_err("error packet receive\n");
Mugunthan V N8269ee42016-11-18 11:08:27 +05302573 return ret;
2574 }
2575
2576 if (!packet_received)
2577 return 0;
2578
2579 if (dev->rx_req) {
2580 net_process_received_packet(net_rx_packets[0],
2581 dev->rx_req->length);
2582 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002583 pr_err("dev->rx_req invalid");
Mugunthan V N8269ee42016-11-18 11:08:27 +05302584 }
2585 packet_received = 0;
2586 rx_submit(dev, dev->rx_req, 0);
2587
2588 return 0;
2589}
2590
2591void usb_eth_halt(struct eth_device *netdev)
2592{
2593 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2594
2595 _usb_eth_halt(priv);
2596}
2597
Remy Bohmer23cd1382009-07-29 18:18:43 +02002598int usb_eth_initialize(bd_t *bi)
2599{
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302600 struct eth_device *netdev = &l_priv->netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002601
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +03002602 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmer23cd1382009-07-29 18:18:43 +02002603
2604 netdev->init = usb_eth_init;
2605 netdev->send = usb_eth_send;
2606 netdev->recv = usb_eth_recv;
2607 netdev->halt = usb_eth_halt;
Mugunthan V Nae701002016-11-18 11:07:18 +05302608 netdev->priv = l_priv;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002609
2610#ifdef CONFIG_MCAST_TFTP
2611 #error not supported
2612#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002613 eth_register(netdev);
2614 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002615}
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302616#else
2617static int usb_eth_start(struct udevice *dev)
2618{
2619 struct ether_priv *priv = dev_get_priv(dev);
2620
2621 return _usb_eth_init(priv);
2622}
2623
2624static int usb_eth_send(struct udevice *dev, void *packet, int length)
2625{
2626 struct ether_priv *priv = dev_get_priv(dev);
2627
2628 return _usb_eth_send(priv, packet, length);
2629}
2630
2631static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2632{
2633 struct ether_priv *priv = dev_get_priv(dev);
2634 struct eth_dev *ethdev = &priv->ethdev;
2635 int ret;
2636
2637 ret = _usb_eth_recv(priv);
2638 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002639 pr_err("error packet receive\n");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302640 return ret;
2641 }
2642
2643 if (packet_received) {
2644 if (ethdev->rx_req) {
2645 *packetp = (uchar *)net_rx_packets[0];
2646 return ethdev->rx_req->length;
2647 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002648 pr_err("dev->rx_req invalid");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302649 return -EFAULT;
2650 }
2651 }
2652
2653 return -EAGAIN;
2654}
2655
2656static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2657 int length)
2658{
2659 struct ether_priv *priv = dev_get_priv(dev);
2660 struct eth_dev *ethdev = &priv->ethdev;
2661
2662 packet_received = 0;
2663
2664 return rx_submit(ethdev, ethdev->rx_req, 0);
2665}
2666
2667static void usb_eth_stop(struct udevice *dev)
2668{
2669 struct ether_priv *priv = dev_get_priv(dev);
2670
2671 _usb_eth_halt(priv);
2672}
2673
2674static int usb_eth_probe(struct udevice *dev)
2675{
2676 struct ether_priv *priv = dev_get_priv(dev);
2677 struct eth_pdata *pdata = dev_get_platdata(dev);
2678
2679 priv->netdev = dev;
2680 l_priv = priv;
2681
2682 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
Simon Glassfd1e9592017-08-03 12:22:11 -06002683 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302684
2685 return 0;
2686}
2687
2688static const struct eth_ops usb_eth_ops = {
2689 .start = usb_eth_start,
2690 .send = usb_eth_send,
2691 .recv = usb_eth_recv,
2692 .free_pkt = usb_eth_free_pkt,
2693 .stop = usb_eth_stop,
2694};
2695
2696int usb_ether_init(void)
2697{
2698 struct udevice *dev;
2699 struct udevice *usb_dev;
2700 int ret;
2701
2702 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
2703 if (!usb_dev || ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002704 pr_err("No USB device found\n");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302705 return ret;
2706 }
2707
2708 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2709 if (!dev || ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002710 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302711 return ret;
2712 }
2713
2714 return 0;
2715}
2716
2717U_BOOT_DRIVER(eth_usb) = {
2718 .name = "usb_ether",
2719 .id = UCLASS_ETH,
2720 .probe = usb_eth_probe,
2721 .ops = &usb_eth_ops,
2722 .priv_auto_alloc_size = sizeof(struct ether_priv),
2723 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2724 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2725};
2726#endif /* CONFIG_DM_ETH */