blob: a939918e9735cef3f2cf232fc5d74fdeae666518 [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>
Simon Glass9fb625c2019-08-01 09:46:51 -060012#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060014#include <part.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090015#include <linux/errno.h>
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +030016#include <linux/netdevice.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020017#include <linux/usb/ch9.h>
18#include <linux/usb/cdc.h>
19#include <linux/usb/gadget.h>
20#include <net.h>
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +053021#include <usb.h>
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030022#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060023#include <memalign.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020024#include <linux/ctype.h>
25
26#include "gadget_chips.h"
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030027#include "rndis.h"
Remy Bohmer23cd1382009-07-29 18:18:43 +020028
Mugunthan V Nd4345ae2016-11-18 10:49:12 +053029#include <dm.h>
Mugunthan V Nd4a37552016-11-18 11:09:15 +053030#include <dm/lists.h>
Mugunthan V Nd4345ae2016-11-18 10:49:12 +053031#include <dm/uclass-internal.h>
32#include <dm/device-internal.h>
33
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +030034#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040035
Remy Bohmer23cd1382009-07-29 18:18:43 +020036#define atomic_read
37extern struct platform_data brd;
Remy Bohmer23cd1382009-07-29 18:18:43 +020038
39
40unsigned packet_received, packet_sent;
41
Remy Bohmer23cd1382009-07-29 18:18:43 +020042/*
43 * Ethernet gadget driver -- with CDC and non-CDC options
44 * Builds on hardware support for a full duplex link.
45 *
46 * CDC Ethernet is the standard USB solution for sending Ethernet frames
47 * using USB. Real hardware tends to use the same framing protocol but look
48 * different for control features. This driver strongly prefers to use
49 * this USB-IF standard as its open-systems interoperability solution;
50 * most host side USB stacks (except from Microsoft) support it.
51 *
52 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
53 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
54 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
55 *
56 * There's some hardware that can't talk CDC ECM. We make that hardware
57 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
58 * link-level setup only requires activating the configuration. Only the
59 * endpoint descriptors, and product/vendor IDs, are relevant; no control
60 * operations are available. Linux supports it, but other host operating
61 * systems may not. (This is a subset of CDC Ethernet.)
62 *
63 * It turns out that if you add a few descriptors to that "CDC Subset",
64 * (Windows) host side drivers from MCCI can treat it as one submode of
65 * a proprietary scheme called "SAFE" ... without needing to know about
66 * specific product/vendor IDs. So we do that, making it easier to use
67 * those MS-Windows drivers. Those added descriptors make it resemble a
68 * CDC MDLM device, but they don't change device behavior at all. (See
69 * MCCI Engineering report 950198 "SAFE Networking Functions".)
70 *
71 * A third option is also in use. Rather than CDC Ethernet, or something
72 * simpler, Microsoft pushes their own approach: RNDIS. The published
73 * RNDIS specs are ambiguous and appear to be incomplete, and are also
74 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
75 */
Remy Bohmer23cd1382009-07-29 18:18:43 +020076
77#define DRIVER_DESC "Ethernet Gadget"
78/* Based on linux 2.6.27 version */
79#define DRIVER_VERSION "May Day 2005"
80
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040081static const char driver_desc[] = DRIVER_DESC;
Remy Bohmer23cd1382009-07-29 18:18:43 +020082
83#define RX_EXTRA 20 /* guard against rx overflows */
84
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030085#ifndef CONFIG_USB_ETH_RNDIS
86#define rndis_uninit(x) do {} while (0)
87#define rndis_deregister(c) do {} while (0)
88#define rndis_exit() do {} while (0)
89#endif
90
91/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmer23cd1382009-07-29 18:18:43 +020092#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
93 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
94 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
95 |USB_CDC_PACKET_TYPE_DIRECTED)
96
97#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
98
99/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300100
101struct eth_dev {
102 struct usb_gadget *gadget;
103 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300104 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300105
106 u8 config;
107 struct usb_ep *in_ep, *out_ep, *status_ep;
108 const struct usb_endpoint_descriptor
109 *in, *out, *status;
110
111 struct usb_request *tx_req, *rx_req;
112
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530113#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300114 struct eth_device *net;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530115#else
116 struct udevice *net;
117#endif
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300118 struct net_device_stats stats;
119 unsigned int tx_qlen;
120
121 unsigned zlp:1;
122 unsigned cdc:1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300123 unsigned rndis:1;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300124 unsigned suspended:1;
125 unsigned network_started:1;
126 u16 cdc_filter;
127 unsigned long todo;
128 int mtu;
129#define WORK_RX_MEMORY 0
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300130 int rndis_config;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300131 u8 host_mac[ETH_ALEN];
132};
133
134/*
135 * This version autoconfigures as much as possible at run-time.
136 *
137 * It also ASSUMES a self-powered device, without remote wakeup,
138 * although remote wakeup support would make sense.
139 */
140
141/*-------------------------------------------------------------------------*/
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +0530142struct ether_priv {
143 struct eth_dev ethdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530144#ifndef CONFIG_DM_ETH
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +0530145 struct eth_device netdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530146#else
147 struct udevice *netdev;
148#endif
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +0530149 struct usb_gadget_driver eth_driver;
150};
151
152struct ether_priv eth_priv;
153struct ether_priv *l_priv = &eth_priv;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200154
155/*-------------------------------------------------------------------------*/
156
157/* "main" config is either CDC, or its simple subset */
158static inline int is_cdc(struct eth_dev *dev)
159{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200160#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200161 return 1; /* only cdc possible */
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200162#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200163 return 0; /* only subset possible */
164#else
165 return dev->cdc; /* depends on what hardware we found */
166#endif
167}
168
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300169/* "secondary" RNDIS config may sometimes be activated */
170static inline int rndis_active(struct eth_dev *dev)
171{
172#ifdef CONFIG_USB_ETH_RNDIS
173 return dev->rndis;
174#else
175 return 0;
176#endif
177}
178
179#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
180#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200181
182#define DEFAULT_QLEN 2 /* double buffering by default */
183
184/* peak bulk transfer bits-per-second */
185#define HS_BPS (13 * 512 * 8 * 1000 * 8)
186#define FS_BPS (19 * 64 * 1 * 1000 * 8)
187
188#ifdef CONFIG_USB_GADGET_DUALSPEED
189#define DEVSPEED USB_SPEED_HIGH
190
Vitaly Kuzmichev2721dbf2010-08-12 16:44:40 +0400191#ifdef CONFIG_USB_ETH_QMULT
192#define qmult CONFIG_USB_ETH_QMULT
193#else
194#define qmult 5
195#endif
196
Remy Bohmer23cd1382009-07-29 18:18:43 +0200197/* for dual-speed hardware, use deeper queues at highspeed */
198#define qlen(gadget) \
199 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
200
201static inline int BITRATE(struct usb_gadget *g)
202{
203 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
204}
205
206#else /* full speed (low speed doesn't do bulk) */
207
208#define qmult 1
209
210#define DEVSPEED USB_SPEED_FULL
211
212#define qlen(gadget) DEFAULT_QLEN
213
214static inline int BITRATE(struct usb_gadget *g)
215{
216 return FS_BPS;
217}
218#endif
219
Remy Bohmer23cd1382009-07-29 18:18:43 +0200220/*-------------------------------------------------------------------------*/
221
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400222/*
223 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmer23cd1382009-07-29 18:18:43 +0200224 * Instead: allocate your own, using normal USB-IF procedures.
225 */
226
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400227/*
228 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200229 * It's for devices with only CDC Ethernet configurations.
230 */
231#define CDC_VENDOR_NUM 0x0525 /* NetChip */
232#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
233
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400234/*
235 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmer23cd1382009-07-29 18:18:43 +0200236 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
237 * with pxa250. We're protocol-compatible, if the host-side drivers
238 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
239 * drivers that need to hard-wire endpoint numbers have a hook.
240 *
241 * The protocol is a minimal subset of CDC Ether, which works on any bulk
242 * hardware that's not deeply broken ... even on hardware that can't talk
243 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
244 * doesn't handle control-OUT).
245 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300246#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
247#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
248
249/*
250 * For hardware that can talk RNDIS and either of the above protocols,
251 * use this ID ... the windows INF files will know it. Unless it's
252 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
253 * the non-RNDIS configuration.
254 */
255#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
256#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200257
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400258/*
259 * Some systems will want different product identifers published in the
Remy Bohmer23cd1382009-07-29 18:18:43 +0200260 * device descriptor, either numbers or strings or both. These string
261 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
262 */
263
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300264/*
265 * Emulating them in eth_bind:
266 * static ushort idVendor;
267 * static ushort idProduct;
268 */
269
Maxime Ripard10ac57f2017-09-07 09:15:08 +0200270#if defined(CONFIG_USB_GADGET_MANUFACTURER)
271static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200272#else
Bin Menga1875592016-02-05 19:30:11 -0800273static char *iManufacturer = "U-Boot";
Remy Bohmer23cd1382009-07-29 18:18:43 +0200274#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300275
276/* These probably need to be configurable. */
277static ushort bcdDevice;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200278static char *iProduct;
279static char *iSerialNumber;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300280
Remy Bohmer23cd1382009-07-29 18:18:43 +0200281static char dev_addr[18];
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300282
Remy Bohmer23cd1382009-07-29 18:18:43 +0200283static char host_addr[18];
284
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300285
Remy Bohmer23cd1382009-07-29 18:18:43 +0200286/*-------------------------------------------------------------------------*/
287
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400288/*
289 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmer23cd1382009-07-29 18:18:43 +0200290 * ep0 implementation: descriptors, config management, setup().
291 * also optional class-specific notification interrupt transfer.
292 */
293
294/*
295 * DESCRIPTORS ... most are static, but strings and (full) configuration
296 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300297 * our simple subset, with RNDIS as an optional second configuration.
298 *
299 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
300 * the class descriptors match a modem (they're ignored; it's really just
301 * Ethernet functionality), they don't need the NOP altsetting, and the
302 * status transfer endpoint isn't optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200303 */
304
305#define STRING_MANUFACTURER 1
306#define STRING_PRODUCT 2
307#define STRING_ETHADDR 3
308#define STRING_DATA 4
309#define STRING_CONTROL 5
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300310#define STRING_RNDIS_CONTROL 6
Remy Bohmer23cd1382009-07-29 18:18:43 +0200311#define STRING_CDC 7
312#define STRING_SUBSET 8
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300313#define STRING_RNDIS 9
Remy Bohmer23cd1382009-07-29 18:18:43 +0200314#define STRING_SERIALNUMBER 10
315
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300316/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200317#define USB_BUFSIZ 256
318
319/*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300320 * This device advertises one configuration, eth_config, unless RNDIS
321 * is enabled (rndis_config) on hardware supporting at least two configs.
322 *
323 * NOTE: Controllers like superh_udc should probably be able to use
324 * an RNDIS-only configuration.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200325 *
326 * FIXME define some higher-powered configurations to make it easier
327 * to recharge batteries ...
328 */
329
330#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300331#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200332
333static struct usb_device_descriptor
334device_desc = {
335 .bLength = sizeof device_desc,
336 .bDescriptorType = USB_DT_DEVICE,
337
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400338 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200339
340 .bDeviceClass = USB_CLASS_COMM,
341 .bDeviceSubClass = 0,
342 .bDeviceProtocol = 0,
343
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400344 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
345 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200346 .iManufacturer = STRING_MANUFACTURER,
347 .iProduct = STRING_PRODUCT,
348 .bNumConfigurations = 1,
349};
350
351static struct usb_otg_descriptor
352otg_descriptor = {
353 .bLength = sizeof otg_descriptor,
354 .bDescriptorType = USB_DT_OTG,
355
356 .bmAttributes = USB_OTG_SRP,
357};
358
359static struct usb_config_descriptor
360eth_config = {
361 .bLength = sizeof eth_config,
362 .bDescriptorType = USB_DT_CONFIG,
363
364 /* compute wTotalLength on the fly */
365 .bNumInterfaces = 2,
366 .bConfigurationValue = DEV_CONFIG_VALUE,
367 .iConfiguration = STRING_CDC,
368 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
369 .bMaxPower = 1,
370};
371
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300372#ifdef CONFIG_USB_ETH_RNDIS
373static struct usb_config_descriptor
374rndis_config = {
375 .bLength = sizeof rndis_config,
376 .bDescriptorType = USB_DT_CONFIG,
377
378 /* compute wTotalLength on the fly */
379 .bNumInterfaces = 2,
380 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
381 .iConfiguration = STRING_RNDIS,
382 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
383 .bMaxPower = 1,
384};
385#endif
386
Remy Bohmer23cd1382009-07-29 18:18:43 +0200387/*
388 * Compared to the simple CDC subset, the full CDC Ethernet model adds
389 * three class descriptors, two interface descriptors, optional status
390 * endpoint. Both have a "data" interface and two bulk endpoints.
391 * There are also differences in how control requests are handled.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300392 *
393 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
394 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
395 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
396 * work around those bugs) are unlikely to ever come from MSFT, you may
397 * wish to avoid using RNDIS.
398 *
399 * MCCI offers an alternative to RNDIS if you need to connect to Windows
400 * but have hardware that can't support CDC Ethernet. We add descriptors
401 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
402 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
403 * get those drivers from MCCI, or bundled with various products.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200404 */
405
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200406#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200407static struct usb_interface_descriptor
408control_intf = {
409 .bLength = sizeof control_intf,
410 .bDescriptorType = USB_DT_INTERFACE,
411
412 .bInterfaceNumber = 0,
413 /* status endpoint is optional; this may be patched later */
414 .bNumEndpoints = 1,
415 .bInterfaceClass = USB_CLASS_COMM,
416 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
417 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
418 .iInterface = STRING_CONTROL,
419};
420#endif
421
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300422#ifdef CONFIG_USB_ETH_RNDIS
423static const struct usb_interface_descriptor
424rndis_control_intf = {
425 .bLength = sizeof rndis_control_intf,
426 .bDescriptorType = USB_DT_INTERFACE,
427
428 .bInterfaceNumber = 0,
429 .bNumEndpoints = 1,
430 .bInterfaceClass = USB_CLASS_COMM,
431 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
432 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
433 .iInterface = STRING_RNDIS_CONTROL,
434};
435#endif
436
Remy Bohmer23cd1382009-07-29 18:18:43 +0200437static const struct usb_cdc_header_desc header_desc = {
438 .bLength = sizeof header_desc,
439 .bDescriptorType = USB_DT_CS_INTERFACE,
440 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
441
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400442 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200443};
444
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200445#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200446
447static const struct usb_cdc_union_desc union_desc = {
448 .bLength = sizeof union_desc,
449 .bDescriptorType = USB_DT_CS_INTERFACE,
450 .bDescriptorSubType = USB_CDC_UNION_TYPE,
451
452 .bMasterInterface0 = 0, /* index of control interface */
453 .bSlaveInterface0 = 1, /* index of DATA interface */
454};
455
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300456#endif /* CDC || RNDIS */
457
458#ifdef CONFIG_USB_ETH_RNDIS
459
460static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
461 .bLength = sizeof call_mgmt_descriptor,
462 .bDescriptorType = USB_DT_CS_INTERFACE,
463 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
464
465 .bmCapabilities = 0x00,
466 .bDataInterface = 0x01,
467};
468
469static const struct usb_cdc_acm_descriptor acm_descriptor = {
470 .bLength = sizeof acm_descriptor,
471 .bDescriptorType = USB_DT_CS_INTERFACE,
472 .bDescriptorSubType = USB_CDC_ACM_TYPE,
473
474 .bmCapabilities = 0x00,
475};
476
477#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200478
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200479#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200480
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400481/*
482 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmer23cd1382009-07-29 18:18:43 +0200483 * ways: data endpoints live in the control interface, there's no data
484 * interface, and it's not used to talk to a cell phone radio.
485 */
486
487static const struct usb_cdc_mdlm_desc mdlm_desc = {
488 .bLength = sizeof mdlm_desc,
489 .bDescriptorType = USB_DT_CS_INTERFACE,
490 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
491
492 .bcdVersion = __constant_cpu_to_le16(0x0100),
493 .bGUID = {
494 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
495 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
496 },
497};
498
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400499/*
500 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmer23cd1382009-07-29 18:18:43 +0200501 * can't really use its struct. All we do here is say that we're using
502 * the submode of "SAFE" which directly matches the CDC Subset.
503 */
Lokesh Vutla65c389d2017-01-17 08:51:22 +0530504#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200505static const u8 mdlm_detail_desc[] = {
506 6,
507 USB_DT_CS_INTERFACE,
508 USB_CDC_MDLM_DETAIL_TYPE,
509
510 0, /* "SAFE" */
511 0, /* network control capabilities (none) */
512 0, /* network data capabilities ("raw" encapsulation) */
513};
Lokesh Vutla65c389d2017-01-17 08:51:22 +0530514#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200515
516#endif
517
Remy Bohmer23cd1382009-07-29 18:18:43 +0200518static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400519 .bLength = sizeof(ether_desc),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200520 .bDescriptorType = USB_DT_CS_INTERFACE,
521 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
522
523 /* this descriptor actually adds value, surprise! */
524 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400525 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
Bin Mengdda52512018-07-31 02:55:22 -0700526 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400527 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200528 .bNumberPowerFilters = 0,
529};
530
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200531#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200532
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400533/*
534 * include the status endpoint if we can, even where it's optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200535 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
536 * packet, to simplify cancellation; and a big transfer interval, to
537 * waste less bandwidth.
538 *
539 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
540 * if they ignore the connect/disconnect notifications that real aether
541 * can provide. more advanced cdc configurations might want to support
542 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300543 *
544 * RNDIS requires the status endpoint, since it uses that encapsulation
545 * mechanism for its funky RPC scheme.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200546 */
547
548#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
549#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
550
551static struct usb_endpoint_descriptor
552fs_status_desc = {
553 .bLength = USB_DT_ENDPOINT_SIZE,
554 .bDescriptorType = USB_DT_ENDPOINT,
555
556 .bEndpointAddress = USB_DIR_IN,
557 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400558 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200559 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
560};
561#endif
562
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200563#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200564
565/* the default data interface has no endpoints ... */
566
567static const struct usb_interface_descriptor
568data_nop_intf = {
569 .bLength = sizeof data_nop_intf,
570 .bDescriptorType = USB_DT_INTERFACE,
571
572 .bInterfaceNumber = 1,
573 .bAlternateSetting = 0,
574 .bNumEndpoints = 0,
575 .bInterfaceClass = USB_CLASS_CDC_DATA,
576 .bInterfaceSubClass = 0,
577 .bInterfaceProtocol = 0,
578};
579
580/* ... but the "real" data interface has two bulk endpoints */
581
582static const struct usb_interface_descriptor
583data_intf = {
584 .bLength = sizeof data_intf,
585 .bDescriptorType = USB_DT_INTERFACE,
586
587 .bInterfaceNumber = 1,
588 .bAlternateSetting = 1,
589 .bNumEndpoints = 2,
590 .bInterfaceClass = USB_CLASS_CDC_DATA,
591 .bInterfaceSubClass = 0,
592 .bInterfaceProtocol = 0,
593 .iInterface = STRING_DATA,
594};
595
596#endif
597
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300598#ifdef CONFIG_USB_ETH_RNDIS
599
600/* RNDIS doesn't activate by changing to the "real" altsetting */
601
602static const struct usb_interface_descriptor
603rndis_data_intf = {
604 .bLength = sizeof rndis_data_intf,
605 .bDescriptorType = USB_DT_INTERFACE,
606
607 .bInterfaceNumber = 1,
608 .bAlternateSetting = 0,
609 .bNumEndpoints = 2,
610 .bInterfaceClass = USB_CLASS_CDC_DATA,
611 .bInterfaceSubClass = 0,
612 .bInterfaceProtocol = 0,
613 .iInterface = STRING_DATA,
614};
615
616#endif
617
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200618#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200619
620/*
621 * "Simple" CDC-subset option is a simple vendor-neutral model that most
622 * full speed controllers can handle: one interface, two bulk endpoints.
623 *
624 * To assist host side drivers, we fancy it up a bit, and add descriptors
625 * so some host side drivers will understand it as a "SAFE" variant.
626 */
627
628static const struct usb_interface_descriptor
629subset_data_intf = {
630 .bLength = sizeof subset_data_intf,
631 .bDescriptorType = USB_DT_INTERFACE,
632
633 .bInterfaceNumber = 0,
634 .bAlternateSetting = 0,
635 .bNumEndpoints = 2,
636 .bInterfaceClass = USB_CLASS_COMM,
637 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
638 .bInterfaceProtocol = 0,
639 .iInterface = STRING_DATA,
640};
641
642#endif /* SUBSET */
643
Remy Bohmer23cd1382009-07-29 18:18:43 +0200644static struct usb_endpoint_descriptor
645fs_source_desc = {
646 .bLength = USB_DT_ENDPOINT_SIZE,
647 .bDescriptorType = USB_DT_ENDPOINT,
648
649 .bEndpointAddress = USB_DIR_IN,
650 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700651 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200652};
653
654static struct usb_endpoint_descriptor
655fs_sink_desc = {
656 .bLength = USB_DT_ENDPOINT_SIZE,
657 .bDescriptorType = USB_DT_ENDPOINT,
658
659 .bEndpointAddress = USB_DIR_OUT,
660 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700661 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200662};
663
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400664static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200665 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200666#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200667 /* "cdc" mode descriptors */
668 (struct usb_descriptor_header *) &control_intf,
669 (struct usb_descriptor_header *) &header_desc,
670 (struct usb_descriptor_header *) &union_desc,
671 (struct usb_descriptor_header *) &ether_desc,
672 /* NOTE: status endpoint may need to be removed */
673 (struct usb_descriptor_header *) &fs_status_desc,
674 /* data interface, with altsetting */
675 (struct usb_descriptor_header *) &data_nop_intf,
676 (struct usb_descriptor_header *) &data_intf,
677 (struct usb_descriptor_header *) &fs_source_desc,
678 (struct usb_descriptor_header *) &fs_sink_desc,
679 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200680#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200681};
682
683static inline void fs_subset_descriptors(void)
684{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200685#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200686 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
687 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
688 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
689 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
690 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
691 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
692 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
693 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
694 fs_eth_function[8] = NULL;
695#else
696 fs_eth_function[1] = NULL;
697#endif
698}
699
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300700#ifdef CONFIG_USB_ETH_RNDIS
701static const struct usb_descriptor_header *fs_rndis_function[] = {
702 (struct usb_descriptor_header *) &otg_descriptor,
703 /* control interface matches ACM, not Ethernet */
704 (struct usb_descriptor_header *) &rndis_control_intf,
705 (struct usb_descriptor_header *) &header_desc,
706 (struct usb_descriptor_header *) &call_mgmt_descriptor,
707 (struct usb_descriptor_header *) &acm_descriptor,
708 (struct usb_descriptor_header *) &union_desc,
709 (struct usb_descriptor_header *) &fs_status_desc,
710 /* data interface has no altsetting */
711 (struct usb_descriptor_header *) &rndis_data_intf,
712 (struct usb_descriptor_header *) &fs_source_desc,
713 (struct usb_descriptor_header *) &fs_sink_desc,
714 NULL,
715};
716#endif
717
Remy Bohmer23cd1382009-07-29 18:18:43 +0200718/*
719 * usb 2.0 devices need to expose both high speed and full speed
720 * descriptors, unless they only run at full speed.
721 */
722
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200723#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200724static struct usb_endpoint_descriptor
725hs_status_desc = {
726 .bLength = USB_DT_ENDPOINT_SIZE,
727 .bDescriptorType = USB_DT_ENDPOINT,
728
729 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400730 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200731 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
732};
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200733#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200734
735static struct usb_endpoint_descriptor
736hs_source_desc = {
737 .bLength = USB_DT_ENDPOINT_SIZE,
738 .bDescriptorType = USB_DT_ENDPOINT,
739
740 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400741 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200742};
743
744static struct usb_endpoint_descriptor
745hs_sink_desc = {
746 .bLength = USB_DT_ENDPOINT_SIZE,
747 .bDescriptorType = USB_DT_ENDPOINT,
748
749 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400750 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200751};
752
753static struct usb_qualifier_descriptor
754dev_qualifier = {
755 .bLength = sizeof dev_qualifier,
756 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
757
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400758 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200759 .bDeviceClass = USB_CLASS_COMM,
760
761 .bNumConfigurations = 1,
762};
763
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400764static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200765 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200766#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200767 /* "cdc" mode descriptors */
768 (struct usb_descriptor_header *) &control_intf,
769 (struct usb_descriptor_header *) &header_desc,
770 (struct usb_descriptor_header *) &union_desc,
771 (struct usb_descriptor_header *) &ether_desc,
772 /* NOTE: status endpoint may need to be removed */
773 (struct usb_descriptor_header *) &hs_status_desc,
774 /* data interface, with altsetting */
775 (struct usb_descriptor_header *) &data_nop_intf,
776 (struct usb_descriptor_header *) &data_intf,
777 (struct usb_descriptor_header *) &hs_source_desc,
778 (struct usb_descriptor_header *) &hs_sink_desc,
779 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200780#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200781};
782
783static inline void hs_subset_descriptors(void)
784{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200785#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200786 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
787 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
788 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
789 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
790 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
791 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
792 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
793 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
794 hs_eth_function[8] = NULL;
795#else
796 hs_eth_function[1] = NULL;
797#endif
798}
799
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300800#ifdef CONFIG_USB_ETH_RNDIS
801static const struct usb_descriptor_header *hs_rndis_function[] = {
802 (struct usb_descriptor_header *) &otg_descriptor,
803 /* control interface matches ACM, not Ethernet */
804 (struct usb_descriptor_header *) &rndis_control_intf,
805 (struct usb_descriptor_header *) &header_desc,
806 (struct usb_descriptor_header *) &call_mgmt_descriptor,
807 (struct usb_descriptor_header *) &acm_descriptor,
808 (struct usb_descriptor_header *) &union_desc,
809 (struct usb_descriptor_header *) &hs_status_desc,
810 /* data interface has no altsetting */
811 (struct usb_descriptor_header *) &rndis_data_intf,
812 (struct usb_descriptor_header *) &hs_source_desc,
813 (struct usb_descriptor_header *) &hs_sink_desc,
814 NULL,
815};
816#endif
817
818
Remy Bohmer23cd1382009-07-29 18:18:43 +0200819/* maxpacket and other transfer characteristics vary by speed. */
820static inline struct usb_endpoint_descriptor *
821ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
822 struct usb_endpoint_descriptor *fs)
823{
824 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
825 return hs;
826 return fs;
827}
828
Remy Bohmer23cd1382009-07-29 18:18:43 +0200829/*-------------------------------------------------------------------------*/
830
831/* descriptors that are built on-demand */
832
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400833static char manufacturer[50];
834static char product_desc[40] = DRIVER_DESC;
835static char serial_number[20];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200836
837/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400838static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200839
840/* static strings, in UTF-8 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400841static struct usb_string strings[] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200842 { STRING_MANUFACTURER, manufacturer, },
843 { STRING_PRODUCT, product_desc, },
844 { STRING_SERIALNUMBER, serial_number, },
845 { STRING_DATA, "Ethernet Data", },
846 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200847#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200848 { STRING_CDC, "CDC Ethernet", },
849 { STRING_CONTROL, "CDC Communications Control", },
850#endif
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200851#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200852 { STRING_SUBSET, "CDC Ethernet Subset", },
853#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300854#ifdef CONFIG_USB_ETH_RNDIS
855 { STRING_RNDIS, "RNDIS", },
856 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
857#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200858 { } /* end of list */
859};
860
861static struct usb_gadget_strings stringtab = {
862 .language = 0x0409, /* en-us */
863 .strings = strings,
864};
865
Remy Bohmer23cd1382009-07-29 18:18:43 +0200866/*============================================================================*/
Joel Fernandes52907592013-09-04 18:55:14 -0500867DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
868
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200869#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes52907592013-09-04 18:55:14 -0500870DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek563aed22012-10-02 17:04:29 +0200871#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200872
Remy Bohmer23cd1382009-07-29 18:18:43 +0200873/*============================================================================*/
874
875/*
876 * one config, two interfaces: control, data.
877 * complications: class descriptors, and an altsetting.
878 */
879static int
880config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
881{
882 int len;
883 const struct usb_config_descriptor *config;
884 const struct usb_descriptor_header **function;
885 int hs = 0;
886
887 if (gadget_is_dualspeed(g)) {
888 hs = (g->speed == USB_SPEED_HIGH);
889 if (type == USB_DT_OTHER_SPEED_CONFIG)
890 hs = !hs;
891 }
892#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
893
894 if (index >= device_desc.bNumConfigurations)
895 return -EINVAL;
896
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300897#ifdef CONFIG_USB_ETH_RNDIS
898 /*
899 * list the RNDIS config first, to make Microsoft's drivers
900 * happy. DOCSIS 1.0 needs this too.
901 */
902 if (device_desc.bNumConfigurations == 2 && index == 0) {
903 config = &rndis_config;
904 function = which_fn(rndis);
905 } else
906#endif
907 {
908 config = &eth_config;
909 function = which_fn(eth);
910 }
Remy Bohmer23cd1382009-07-29 18:18:43 +0200911
912 /* for now, don't advertise srp-only devices */
913 if (!is_otg)
914 function++;
915
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400916 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200917 if (len < 0)
918 return len;
919 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
920 return len;
921}
922
923/*-------------------------------------------------------------------------*/
924
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300925static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400926static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200927
928static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400929set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200930{
931 int result = 0;
932 struct usb_gadget *gadget = dev->gadget;
933
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200934#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300935 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200936 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400937 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmer23cd1382009-07-29 18:18:43 +0200938 &fs_status_desc);
939 dev->status_ep->driver_data = dev;
940
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400941 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200942 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400943 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200944 dev->status_ep->name, result);
945 goto done;
946 }
947 }
948#endif
949
950 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
951 dev->in_ep->driver_data = dev;
952
953 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
954 dev->out_ep->driver_data = dev;
955
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400956 /*
957 * With CDC, the host isn't allowed to use these two data
Remy Bohmer23cd1382009-07-29 18:18:43 +0200958 * endpoints in the default altsetting for the interface.
959 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300960 *
961 * Strictly speaking RNDIS should work the same: activation is
962 * a side effect of setting a packet filter. Deactivation is
963 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200964 */
965 if (!cdc_active(dev)) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400966 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200967 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400968 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200969 dev->in_ep->name, result);
970 goto done;
971 }
972
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400973 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200974 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400975 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200976 dev->out_ep->name, result);
977 goto done;
978 }
979 }
980
981done:
982 if (result == 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400983 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200984
985 /* on error, disable any endpoints */
986 if (result < 0) {
Vitaly Kuzmichevd5292c12010-08-13 17:02:29 +0400987 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400988 (void) usb_ep_disable(dev->status_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200989 dev->status = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400990 (void) usb_ep_disable(dev->in_ep);
991 (void) usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200992 dev->in = NULL;
993 dev->out = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300994 } else if (!cdc_active(dev)) {
995 /*
996 * activate non-CDC configs right away
997 * this isn't strictly according to the RNDIS spec
998 */
999 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001000 }
1001
1002 /* caller is responsible for cleanup on error */
1003 return result;
1004}
1005
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001006static void eth_reset_config(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001007{
1008 if (dev->config == 0)
1009 return;
1010
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001011 debug("%s\n", __func__);
1012
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001013 rndis_uninit(dev->rndis_config);
1014
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001015 /*
1016 * disable endpoints, forcing (synchronous) completion of
Remy Bohmer23cd1382009-07-29 18:18:43 +02001017 * pending i/o. then free the requests.
1018 */
1019
1020 if (dev->in) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001021 usb_ep_disable(dev->in_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001022 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001023 usb_ep_free_request(dev->in_ep, dev->tx_req);
1024 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001025 }
1026 }
1027 if (dev->out) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001028 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001029 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001030 usb_ep_free_request(dev->out_ep, dev->rx_req);
1031 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001032 }
1033 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001034 if (dev->status)
1035 usb_ep_disable(dev->status_ep);
1036
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001037 dev->rndis = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001038 dev->cdc_filter = 0;
1039 dev->config = 0;
1040}
1041
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001042/*
1043 * change our operational config. must agree with the code
Remy Bohmer23cd1382009-07-29 18:18:43 +02001044 * that returns config descriptors, and altsetting code.
1045 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001046static int eth_set_config(struct eth_dev *dev, unsigned number,
1047 gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001048{
1049 int result = 0;
1050 struct usb_gadget *gadget = dev->gadget;
1051
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001052 if (gadget_is_sa1100(gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001053 && dev->config
1054 && dev->tx_qlen != 0) {
1055 /* tx fifo is full, but we can't clear it...*/
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001056 pr_err("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001057 return -ESPIPE;
1058 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001059 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001060
1061 switch (number) {
1062 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001063 result = set_ether_config(dev, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001064 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001065#ifdef CONFIG_USB_ETH_RNDIS
1066 case DEV_RNDIS_CONFIG_VALUE:
1067 dev->rndis = 1;
1068 result = set_ether_config(dev, gfp_flags);
1069 break;
1070#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02001071 default:
1072 result = -EINVAL;
1073 /* FALL THROUGH */
1074 case 0:
1075 break;
1076 }
1077
1078 if (result) {
1079 if (number)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001080 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001081 usb_gadget_vbus_draw(dev->gadget,
1082 gadget_is_otg(dev->gadget) ? 8 : 100);
1083 } else {
1084 char *speed;
1085 unsigned power;
1086
1087 power = 2 * eth_config.bMaxPower;
1088 usb_gadget_vbus_draw(dev->gadget, power);
1089
1090 switch (gadget->speed) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001091 case USB_SPEED_FULL:
1092 speed = "full"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001093#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001094 case USB_SPEED_HIGH:
1095 speed = "high"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001096#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001097 default:
1098 speed = "?"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001099 }
1100
1101 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001102 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001103 speed, number, power, driver_desc,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001104 rndis_active(dev)
1105 ? "RNDIS"
1106 : (cdc_active(dev)
1107 ? "CDC Ethernet"
Remy Bohmer23cd1382009-07-29 18:18:43 +02001108 : "CDC Ethernet Subset"));
1109 }
1110 return result;
1111}
1112
1113/*-------------------------------------------------------------------------*/
1114
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001115#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001116
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001117/*
1118 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001119 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001120 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmer23cd1382009-07-29 18:18:43 +02001121 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1122 * command mechanism; and only one status request is ever queued.
1123 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001124static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001125{
1126 struct usb_cdc_notification *event = req->buf;
1127 int value = req->status;
1128 struct eth_dev *dev = ep->driver_data;
1129
1130 /* issue the second notification if host reads the first */
1131 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1132 && value == 0) {
1133 __le32 *data = req->buf + sizeof *event;
1134
1135 event->bmRequestType = 0xA1;
1136 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001137 event->wValue = __constant_cpu_to_le16(0);
1138 event->wIndex = __constant_cpu_to_le16(1);
1139 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001140
1141 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001142 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmer23cd1382009-07-29 18:18:43 +02001143
1144 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001145 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001146 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001147 if (value == 0)
1148 return;
1149 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001150 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001151 event->bNotificationType, value);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001152 if (event->bNotificationType ==
1153 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N17b4f302016-11-18 10:49:13 +05301154 dev->network_started = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001155 printf("USB network up!\n");
1156 }
1157 }
1158 req->context = NULL;
1159}
1160
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001161static void issue_start_status(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001162{
1163 struct usb_request *req = dev->stat_req;
1164 struct usb_cdc_notification *event;
1165 int value;
1166
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001167 /*
1168 * flush old status
Remy Bohmer23cd1382009-07-29 18:18:43 +02001169 *
1170 * FIXME ugly idiom, maybe we'd be better with just
1171 * a "cancel the whole queue" primitive since any
1172 * unlink-one primitive has way too many error modes.
1173 * here, we "know" toggle is already clear...
1174 *
1175 * FIXME iff req->context != null just dequeue it
1176 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001177 usb_ep_disable(dev->status_ep);
1178 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001179
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001180 /*
1181 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmer23cd1382009-07-29 18:18:43 +02001182 * a SPEED_CHANGE. could be useful in some configs.
1183 */
1184 event = req->buf;
1185 event->bmRequestType = 0xA1;
1186 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001187 event->wValue = __constant_cpu_to_le16(1); /* connected */
1188 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001189 event->wLength = 0;
1190
1191 req->length = sizeof *event;
1192 req->complete = eth_status_complete;
1193 req->context = dev;
1194
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001195 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001196 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001197 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001198}
1199
1200#endif
1201
1202/*-------------------------------------------------------------------------*/
1203
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001204static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001205{
1206 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001207 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001208 req->status, req->actual, req->length);
1209}
1210
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001211#ifdef CONFIG_USB_ETH_RNDIS
1212
1213static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1214{
1215 if (req->status || req->actual != req->length)
1216 debug("rndis response complete --> %d, %d/%d\n",
1217 req->status, req->actual, req->length);
1218
1219 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1220}
1221
1222static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1223{
1224 struct eth_dev *dev = ep->driver_data;
1225 int status;
1226
1227 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1228 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1229 if (status < 0)
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001230 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001231}
1232
1233#endif /* RNDIS */
1234
Remy Bohmer23cd1382009-07-29 18:18:43 +02001235/*
1236 * The setup() callback implements all the ep0 functionality that's not
1237 * handled lower down. CDC has a number of less-common features:
1238 *
1239 * - two interfaces: control, and ethernet data
1240 * - Ethernet data interface has two altsettings: default, and active
1241 * - class-specific descriptors for the control interface
1242 * - class-specific control requests
1243 */
1244static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001245eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001246{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001247 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001248 struct usb_request *req = dev->req;
1249 int value = -EOPNOTSUPP;
1250 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1251 u16 wValue = le16_to_cpu(ctrl->wValue);
1252 u16 wLength = le16_to_cpu(ctrl->wLength);
1253
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001254 /*
1255 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001256 * while config change events may enable network traffic.
1257 */
1258
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001259 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001260
1261 req->complete = eth_setup_complete;
1262 switch (ctrl->bRequest) {
1263
1264 case USB_REQ_GET_DESCRIPTOR:
1265 if (ctrl->bRequestType != USB_DIR_IN)
1266 break;
1267 switch (wValue >> 8) {
1268
1269 case USB_DT_DEVICE:
Kishon Vijay Abraham I04afd5b2015-02-23 18:40:17 +05301270 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001271 value = min(wLength, (u16) sizeof device_desc);
1272 memcpy(req->buf, &device_desc, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001273 break;
1274 case USB_DT_DEVICE_QUALIFIER:
1275 if (!gadget_is_dualspeed(gadget))
1276 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001277 value = min(wLength, (u16) sizeof dev_qualifier);
1278 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001279 break;
1280
1281 case USB_DT_OTHER_SPEED_CONFIG:
1282 if (!gadget_is_dualspeed(gadget))
1283 break;
1284 /* FALLTHROUGH */
1285 case USB_DT_CONFIG:
1286 value = config_buf(gadget, req->buf,
1287 wValue >> 8,
1288 wValue & 0xff,
1289 gadget_is_otg(gadget));
1290 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001291 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001292 break;
1293
1294 case USB_DT_STRING:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001295 value = usb_gadget_get_string(&stringtab,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001296 wValue & 0xff, req->buf);
1297
1298 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001299 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001300
1301 break;
1302 }
1303 break;
1304
1305 case USB_REQ_SET_CONFIGURATION:
1306 if (ctrl->bRequestType != 0)
1307 break;
1308 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001309 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001310 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001311 debug("HNP needs a different root port\n");
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001312 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001313 break;
1314 case USB_REQ_GET_CONFIGURATION:
1315 if (ctrl->bRequestType != USB_DIR_IN)
1316 break;
1317 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001318 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001319 break;
1320
1321 case USB_REQ_SET_INTERFACE:
1322 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1323 || !dev->config
1324 || wIndex > 1)
1325 break;
1326 if (!cdc_active(dev) && wIndex != 0)
1327 break;
1328
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001329 /*
1330 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001331 * we need to kluge around that interference.
1332 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001333 if (gadget_is_pxa(gadget)) {
1334 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001335 GFP_ATOMIC);
Lukasz Dalek563aed22012-10-02 17:04:29 +02001336 /*
1337 * PXA25x driver use non-CDC ethernet gadget.
1338 * But only _CDC and _RNDIS code can signalize
1339 * that network is working. So we signalize it
1340 * here.
1341 */
Mugunthan V N17b4f302016-11-18 10:49:13 +05301342 dev->network_started = 1;
Lukasz Dalek563aed22012-10-02 17:04:29 +02001343 debug("USB network up!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001344 goto done_set_intf;
1345 }
1346
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001347#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001348 switch (wIndex) {
1349 case 0: /* control/master intf */
1350 if (wValue != 0)
1351 break;
1352 if (dev->status) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001353 usb_ep_disable(dev->status_ep);
1354 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001355 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001356
Remy Bohmer23cd1382009-07-29 18:18:43 +02001357 value = 0;
1358 break;
1359 case 1: /* data intf */
1360 if (wValue > 1)
1361 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001362 usb_ep_disable(dev->in_ep);
1363 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001364
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001365 /*
1366 * CDC requires the data transfers not be done from
Remy Bohmer23cd1382009-07-29 18:18:43 +02001367 * the default interface setting ... also, setting
1368 * the non-default interface resets filters etc.
1369 */
1370 if (wValue == 1) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001371 if (!cdc_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001372 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001373 usb_ep_enable(dev->in_ep, dev->in);
1374 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001375 dev->cdc_filter = DEFAULT_FILTER;
1376 if (dev->status)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001377 issue_start_status(dev);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001378 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001379 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001380 value = 0;
1381 break;
1382 }
1383#else
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001384 /*
1385 * FIXME this is wrong, as is the assumption that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001386 * all non-PXA hardware talks real CDC ...
1387 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001388 debug("set_interface ignored!\n");
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001389#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001390
1391done_set_intf:
1392 break;
1393 case USB_REQ_GET_INTERFACE:
1394 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1395 || !dev->config
1396 || wIndex > 1)
1397 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001398 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001399 break;
1400
1401 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001402 if (rndis_active(dev) || wIndex != 1)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001403 *(u8 *)req->buf = 0;
1404 else {
1405 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1406 /* carrier always ok ...*/
1407 *(u8 *)req->buf = 1 ;
1408 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001409 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001410 break;
1411
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001412#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001413 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001414 /*
1415 * see 6.2.30: no data, wIndex = interface,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001416 * wValue = packet filter bitmap
1417 */
1418 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1419 || !cdc_active(dev)
1420 || wLength != 0
1421 || wIndex > 1)
1422 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001423 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001424 dev->cdc_filter = wValue;
1425 value = 0;
1426 break;
1427
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001428 /*
1429 * and potentially:
Remy Bohmer23cd1382009-07-29 18:18:43 +02001430 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1431 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1432 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1433 * case USB_CDC_GET_ETHERNET_STATISTIC:
1434 */
1435
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001436#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001437
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001438#ifdef CONFIG_USB_ETH_RNDIS
1439 /*
1440 * RNDIS uses the CDC command encapsulation mechanism to implement
1441 * an RPC scheme, with much getting/setting of attributes by OID.
1442 */
1443 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1444 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1445 || !rndis_active(dev)
1446 || wLength > USB_BUFSIZ
1447 || wValue
1448 || rndis_control_intf.bInterfaceNumber
1449 != wIndex)
1450 break;
1451 /* read the request, then process it */
1452 value = wLength;
1453 req->complete = rndis_command_complete;
1454 /* later, rndis_control_ack () sends a notification */
1455 break;
1456
1457 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1458 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1459 == ctrl->bRequestType
1460 && rndis_active(dev)
1461 /* && wLength >= 0x0400 */
1462 && !wValue
1463 && rndis_control_intf.bInterfaceNumber
1464 == wIndex) {
1465 u8 *buf;
1466 u32 n;
1467
1468 /* return the result */
1469 buf = rndis_get_next_response(dev->rndis_config, &n);
1470 if (buf) {
1471 memcpy(req->buf, buf, n);
1472 req->complete = rndis_response_complete;
1473 rndis_free_response(dev->rndis_config, buf);
1474 value = n;
1475 }
1476 /* else stalls ... spec says to avoid that */
1477 }
1478 break;
1479#endif /* RNDIS */
1480
Remy Bohmer23cd1382009-07-29 18:18:43 +02001481 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001482 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001483 ctrl->bRequestType, ctrl->bRequest,
1484 wValue, wIndex, wLength);
1485 }
1486
1487 /* respond with data transfer before status phase? */
1488 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001489 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001490 req->length = value;
1491 req->zero = value < wLength
1492 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001493 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001494 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001495 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001496 req->status = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001497 eth_setup_complete(gadget->ep0, req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001498 }
1499 }
1500
1501 /* host either stalls (value < 0) or reports success */
1502 return value;
1503}
1504
Remy Bohmer23cd1382009-07-29 18:18:43 +02001505/*-------------------------------------------------------------------------*/
1506
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001507static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001508
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001509static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001510 gfp_t gfp_flags)
1511{
1512 int retval = -ENOMEM;
1513 size_t size;
1514
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001515 /*
1516 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001517 * Normally we use the USB "terminate on short read" convention;
1518 * so allow up to (N*maxpacket), since that memory is normally
1519 * already allocated. Some hardware doesn't deal well with short
1520 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1521 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001522 *
1523 * RNDIS uses internal framing, and explicitly allows senders to
1524 * pad to end-of-packet. That's potentially nice for speed,
1525 * but means receivers can't recover synch on their own.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001526 */
1527
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001528 debug("%s\n", __func__);
Troy Kisky71fc5f92013-09-25 18:41:05 -07001529 if (!req)
1530 return -EINVAL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001531
1532 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1533 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001534 if (rndis_active(dev))
1535 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001536 size -= size % dev->out_ep->maxpacket;
1537
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001538 /*
1539 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001540 * but on at least one, checksumming fails otherwise. Note:
1541 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001542 */
1543
Joe Hershberger1fd92db2015-04-08 01:41:06 -05001544 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmer23cd1382009-07-29 18:18:43 +02001545 req->length = size;
1546 req->complete = rx_complete;
1547
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001548 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001549
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001550 if (retval)
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001551 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001552
Remy Bohmer23cd1382009-07-29 18:18:43 +02001553 return retval;
1554}
1555
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001556static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001557{
1558 struct eth_dev *dev = ep->driver_data;
1559
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001560 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001561 switch (req->status) {
1562 /* normal completion */
1563 case 0:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001564 if (rndis_active(dev)) {
1565 /* we know MaxPacketsPerTransfer == 1 here */
1566 int length = rndis_rm_hdr(req->buf, req->actual);
1567 if (length < 0)
1568 goto length_err;
1569 req->length -= length;
1570 req->actual -= length;
1571 }
Bin Mengdda52512018-07-31 02:55:22 -07001572 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001573length_err:
1574 dev->stats.rx_errors++;
1575 dev->stats.rx_length_errors++;
1576 debug("rx length %d\n", req->length);
1577 break;
1578 }
1579
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001580 dev->stats.rx_packets++;
1581 dev->stats.rx_bytes += req->length;
1582 break;
1583
1584 /* software-driven interface shutdown */
1585 case -ECONNRESET: /* unlink */
1586 case -ESHUTDOWN: /* disconnect etc */
1587 /* for hardware automagic (such as pxa) */
1588 case -ECONNABORTED: /* endpoint reset */
1589 break;
1590
1591 /* data overrun */
1592 case -EOVERFLOW:
1593 dev->stats.rx_over_errors++;
1594 /* FALLTHROUGH */
1595 default:
1596 dev->stats.rx_errors++;
1597 break;
1598 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001599
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001600 packet_received = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001601}
1602
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001603static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001604{
1605
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001606 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001607
1608 if (!dev->tx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001609 goto fail1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001610
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001611 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001612
1613 if (!dev->rx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001614 goto fail2;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001615
1616 return 0;
1617
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001618fail2:
1619 usb_ep_free_request(dev->in_ep, dev->tx_req);
1620fail1:
Masahiro Yamada9b643e32017-09-16 14:10:41 +09001621 pr_err("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001622 return -1;
1623}
1624
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001625static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001626{
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001627 struct eth_dev *dev = ep->driver_data;
1628
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001629 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001630 switch (req->status) {
1631 default:
1632 dev->stats.tx_errors++;
1633 debug("tx err %d\n", req->status);
1634 /* FALLTHROUGH */
1635 case -ECONNRESET: /* unlink */
1636 case -ESHUTDOWN: /* disconnect etc */
1637 break;
1638 case 0:
1639 dev->stats.tx_bytes += req->length;
1640 }
1641 dev->stats.tx_packets++;
1642
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001643 packet_sent = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001644}
1645
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001646static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001647{
1648 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001649 if (subset_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001650 return 1;
1651 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1652}
1653
1654#if 0
1655static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1656{
1657 struct eth_dev *dev = netdev_priv(net);
1658 int length = skb->len;
1659 int retval;
1660 struct usb_request *req = NULL;
1661 unsigned long flags;
1662
1663 /* apply outgoing CDC or RNDIS filters */
1664 if (!eth_is_promisc (dev)) {
1665 u8 *dest = skb->data;
1666
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001667 if (is_multicast_ethaddr(dest)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02001668 u16 type;
1669
1670 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1671 * SET_ETHERNET_MULTICAST_FILTERS requests
1672 */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001673 if (is_broadcast_ethaddr(dest))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001674 type = USB_CDC_PACKET_TYPE_BROADCAST;
1675 else
1676 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1677 if (!(dev->cdc_filter & type)) {
1678 dev_kfree_skb_any (skb);
1679 return 0;
1680 }
1681 }
1682 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1683 }
1684
1685 spin_lock_irqsave(&dev->req_lock, flags);
1686 /*
1687 * this freelist can be empty if an interrupt triggered disconnect()
1688 * and reconfigured the gadget (shutting down this queue) after the
1689 * network stack decided to xmit but before we got the spinlock.
1690 */
1691 if (list_empty(&dev->tx_reqs)) {
1692 spin_unlock_irqrestore(&dev->req_lock, flags);
1693 return 1;
1694 }
1695
1696 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1697 list_del (&req->list);
1698
1699 /* temporarily stop TX queue when the freelist empties */
1700 if (list_empty (&dev->tx_reqs))
1701 netif_stop_queue (net);
1702 spin_unlock_irqrestore(&dev->req_lock, flags);
1703
1704 /* no buffer copies needed, unless the network stack did it
1705 * or the hardware can't use skb buffers.
1706 * or there's not enough space for any RNDIS headers we need
1707 */
1708 if (rndis_active(dev)) {
1709 struct sk_buff *skb_rndis;
1710
1711 skb_rndis = skb_realloc_headroom (skb,
1712 sizeof (struct rndis_packet_msg_type));
1713 if (!skb_rndis)
1714 goto drop;
1715
1716 dev_kfree_skb_any (skb);
1717 skb = skb_rndis;
1718 rndis_add_hdr (skb);
1719 length = skb->len;
1720 }
1721 req->buf = skb->data;
1722 req->context = skb;
1723 req->complete = tx_complete;
1724
1725 /* use zlp framing on tx for strict CDC-Ether conformance,
1726 * though any robust network rx path ignores extra padding.
1727 * and some hardware doesn't like to write zlps.
1728 */
1729 req->zero = 1;
1730 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1731 length++;
1732
1733 req->length = length;
1734
1735 /* throttle highspeed IRQ rate back slightly */
1736 if (gadget_is_dualspeed(dev->gadget))
1737 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1738 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1739 : 0;
1740
1741 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1742 switch (retval) {
1743 default:
1744 DEBUG (dev, "tx queue err %d\n", retval);
1745 break;
1746 case 0:
1747 net->trans_start = jiffies;
1748 atomic_inc (&dev->tx_qlen);
1749 }
1750
1751 if (retval) {
1752drop:
1753 dev->stats.tx_dropped++;
1754 dev_kfree_skb_any (skb);
1755 spin_lock_irqsave(&dev->req_lock, flags);
1756 if (list_empty (&dev->tx_reqs))
1757 netif_start_queue (net);
1758 list_add (&req->list, &dev->tx_reqs);
1759 spin_unlock_irqrestore(&dev->req_lock, flags);
1760 }
1761 return 0;
1762}
1763
1764/*-------------------------------------------------------------------------*/
1765#endif
1766
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001767static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001768{
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001769 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001770
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001771 debug("%s...\n", __func__);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001772 rndis_deregister(dev->rndis_config);
1773 rndis_exit();
Remy Bohmer23cd1382009-07-29 18:18:43 +02001774
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001775 /* we've already been disconnected ... no i/o is active */
1776 if (dev->req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001777 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001778 dev->req = NULL;
1779 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001780 if (dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001781 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001782 dev->stat_req = NULL;
1783 }
1784
1785 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001786 usb_ep_free_request(dev->in_ep, dev->tx_req);
1787 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001788 }
1789
1790 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001791 usb_ep_free_request(dev->out_ep, dev->rx_req);
1792 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001793 }
1794
1795/* unregister_netdev (dev->net);*/
1796/* free_netdev(dev->net);*/
1797
Lei Wen988ee3e2010-12-01 23:43:43 +08001798 dev->gadget = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001799 set_gadget_data(gadget, NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001800}
1801
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001802static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001803{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001804 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001805 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001806}
1807
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001808static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001809{
1810 /* Not used */
1811}
1812
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001813static void eth_resume(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001814{
1815 /* Not used */
1816}
1817
1818/*-------------------------------------------------------------------------*/
1819
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001820#ifdef CONFIG_USB_ETH_RNDIS
1821
1822/*
1823 * The interrupt endpoint is used in RNDIS to notify the host when messages
1824 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1825 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1826 * REMOTE_NDIS_KEEPALIVE_MSG.
1827 *
1828 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1829 * normally just one notification will be queued.
1830 */
1831
1832static void rndis_control_ack_complete(struct usb_ep *ep,
1833 struct usb_request *req)
1834{
1835 struct eth_dev *dev = ep->driver_data;
1836
1837 debug("%s...\n", __func__);
1838 if (req->status || req->actual != req->length)
1839 debug("rndis control ack complete --> %d, %d/%d\n",
1840 req->status, req->actual, req->length);
1841
Mugunthan V N17b4f302016-11-18 10:49:13 +05301842 if (!dev->network_started) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001843 if (rndis_get_state(dev->rndis_config)
1844 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N17b4f302016-11-18 10:49:13 +05301845 dev->network_started = 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001846 printf("USB RNDIS network up!\n");
1847 }
1848 }
1849
1850 req->context = NULL;
1851
1852 if (req != dev->stat_req)
1853 usb_ep_free_request(ep, req);
1854}
1855
1856static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1857
Mugunthan V Nd4a37552016-11-18 11:09:15 +05301858#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001859static int rndis_control_ack(struct eth_device *net)
Mugunthan V Nd4a37552016-11-18 11:09:15 +05301860#else
1861static int rndis_control_ack(struct udevice *net)
1862#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001863{
Mugunthan V Nae701002016-11-18 11:07:18 +05301864 struct ether_priv *priv = (struct ether_priv *)net->priv;
1865 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001866 int length;
1867 struct usb_request *resp = dev->stat_req;
1868
1869 /* in case RNDIS calls this after disconnect */
1870 if (!dev->status) {
1871 debug("status ENODEV\n");
1872 return -ENODEV;
1873 }
1874
1875 /* in case queue length > 1 */
1876 if (resp->context) {
1877 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1878 if (!resp)
1879 return -ENOMEM;
1880 resp->buf = rndis_resp_buf;
1881 }
1882
1883 /*
1884 * Send RNDIS RESPONSE_AVAILABLE notification;
1885 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1886 */
1887 resp->length = 8;
1888 resp->complete = rndis_control_ack_complete;
1889 resp->context = dev;
1890
1891 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1892 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1893
1894 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1895 if (length < 0) {
1896 resp->status = 0;
1897 rndis_control_ack_complete(dev->status_ep, resp);
1898 }
1899
1900 return 0;
1901}
1902
1903#else
1904
1905#define rndis_control_ack NULL
1906
1907#endif /* RNDIS */
1908
1909static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1910{
1911 if (rndis_active(dev)) {
1912 rndis_set_param_medium(dev->rndis_config,
1913 NDIS_MEDIUM_802_3,
1914 BITRATE(dev->gadget)/100);
1915 rndis_signal_connect(dev->rndis_config);
1916 }
1917}
1918
1919static int eth_stop(struct eth_dev *dev)
1920{
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001921#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1922 unsigned long ts;
1923 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1924#endif
1925
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001926 if (rndis_active(dev)) {
1927 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1928 rndis_signal_disconnect(dev->rndis_config);
1929
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001930#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1931 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1932 ts = get_timer(0);
1933 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05301934 usb_gadget_handle_interrupts(0);
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001935#endif
1936
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001937 rndis_uninit(dev->rndis_config);
1938 dev->rndis = 0;
1939 }
1940
1941 return 0;
1942}
1943
1944/*-------------------------------------------------------------------------*/
1945
Remy Bohmer23cd1382009-07-29 18:18:43 +02001946static int is_eth_addr_valid(char *str)
1947{
1948 if (strlen(str) == 17) {
1949 int i;
1950 char *p, *q;
1951 uchar ea[6];
1952
1953 /* see if it looks like an ethernet address */
1954
1955 p = str;
1956
1957 for (i = 0; i < 6; i++) {
1958 char term = (i == 5 ? '\0' : ':');
1959
1960 ea[i] = simple_strtol(p, &q, 16);
1961
1962 if ((q - p) != 2 || *q++ != term)
1963 break;
1964
1965 p = q;
1966 }
1967
Tom Rini57a87a22012-10-31 13:30:41 +00001968 /* Now check the contents. */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001969 return is_valid_ethaddr(ea);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001970 }
1971 return 0;
1972}
1973
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001974static u8 nibble(unsigned char c)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001975{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001976 if (likely(isdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001977 return c - '0';
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001978 c = toupper(c);
1979 if (likely(isxdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001980 return 10 + c - 'A';
1981 return 0;
1982}
1983
1984static int get_ether_addr(const char *str, u8 *dev_addr)
1985{
1986 if (str) {
1987 unsigned i;
1988
1989 for (i = 0; i < 6; i++) {
1990 unsigned char num;
1991
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001992 if ((*str == '.') || (*str == ':'))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001993 str++;
1994 num = nibble(*str++) << 4;
1995 num |= (nibble(*str++));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001996 dev_addr[i] = num;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001997 }
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001998 if (is_valid_ethaddr(dev_addr))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001999 return 0;
2000 }
2001 return 1;
2002}
2003
2004static int eth_bind(struct usb_gadget *gadget)
2005{
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302006 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002007 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002008 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002009 int status = -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002010 int gcnum;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002011 u8 tmp[7];
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302012#ifdef CONFIG_DM_ETH
2013 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2014#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002015
2016 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002017#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002018 cdc = 0;
2019#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002020#ifndef CONFIG_USB_ETH_RNDIS
2021 rndis = 0;
2022#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002023 /*
2024 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmer23cd1382009-07-29 18:18:43 +02002025 * standard protocol is _strongly_ preferred for interop purposes.
2026 * (By everyone except Microsoft.)
2027 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002028 if (gadget_is_pxa(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002029 /* pxa doesn't support altsettings */
2030 cdc = 0;
2031 } else if (gadget_is_musbhdrc(gadget)) {
2032 /* reduce tx dma overhead by avoiding special cases */
2033 zlp = 0;
2034 } else if (gadget_is_sh(gadget)) {
2035 /* sh doesn't support multiple interfaces or configs */
2036 cdc = 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002037 rndis = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002038 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002039 /* hardware can't write zlps */
2040 zlp = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002041 /*
2042 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmer23cd1382009-07-29 18:18:43 +02002043 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2044 */
2045 cdc = 0;
2046 }
2047
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002048 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002049 if (gcnum >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002050 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002051 else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002052 /*
2053 * can't assume CDC works. don't want to default to
Remy Bohmer23cd1382009-07-29 18:18:43 +02002054 * anything less functional on CDC-capable hardware,
2055 * so we fail in this case.
2056 */
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002057 pr_err("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002058 gadget->name);
2059 return -ENODEV;
2060 }
2061
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002062 /*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002063 * If there's an RNDIS configuration, that's what Windows wants to
2064 * be using ... so use these product IDs here and in the "linux.inf"
2065 * needed to install MSFT drivers. Current Linux kernels will use
2066 * the second configuration if it's CDC Ethernet, and need some help
2067 * to choose the right configuration otherwise.
2068 */
2069 if (rndis) {
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002070#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002071 device_desc.idVendor =
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002072 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002073 device_desc.idProduct =
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002074 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002075#else
2076 device_desc.idVendor =
2077 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2078 device_desc.idProduct =
2079 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2080#endif
2081 sprintf(product_desc, "RNDIS/%s", driver_desc);
2082
2083 /*
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002084 * CDC subset ... recognized by Linux since 2.4.10, but Windows
Remy Bohmer23cd1382009-07-29 18:18:43 +02002085 * drivers aren't widely available. (That may be improved by
2086 * supporting one submode of the "SAFE" variant of MDLM.)
2087 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002088 } else {
Maxime Ripard10ac57f2017-09-07 09:15:08 +02002089#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2090 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2091 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002092#else
2093 if (!cdc) {
2094 device_desc.idVendor =
2095 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2096 device_desc.idProduct =
2097 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2098 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002099#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002100 }
2101 /* support optional vendor/distro customization */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002102 if (bcdDevice)
2103 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2104 if (iManufacturer)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002105 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002106 if (iProduct)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002107 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002108 if (iSerialNumber) {
2109 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev2e12abe2010-08-13 17:00:45 +04002110 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002111 }
2112
2113 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002114 usb_ep_autoconfig_reset(gadget);
2115 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002116 if (!in_ep) {
2117autoconf_fail:
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002118 pr_err("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002119 gadget->name);
2120 return -ENODEV;
2121 }
2122 in_ep->driver_data = in_ep; /* claim */
2123
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002124 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002125 if (!out_ep)
2126 goto autoconf_fail;
2127 out_ep->driver_data = out_ep; /* claim */
2128
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002129#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002130 /*
2131 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmer23cd1382009-07-29 18:18:43 +02002132 * Since some hosts expect one, try to allocate one anyway.
2133 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002134 if (cdc || rndis) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002135 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002136 if (status_ep) {
2137 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002138 } else if (rndis) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002139 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002140 return -ENODEV;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002141#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002142 } else if (cdc) {
2143 control_intf.bNumEndpoints = 0;
2144 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002145#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002146 }
2147 }
2148#endif
2149
2150 /* one config: cdc, else minimal subset */
2151 if (!cdc) {
2152 eth_config.bNumInterfaces = 1;
2153 eth_config.iConfiguration = STRING_SUBSET;
2154
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002155 /*
2156 * use functions to set these up, in case we're built to work
Remy Bohmer23cd1382009-07-29 18:18:43 +02002157 * with multiple controllers and must override CDC Ethernet.
2158 */
2159 fs_subset_descriptors();
2160 hs_subset_descriptors();
2161 }
2162
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002163 usb_gadget_set_selfpowered(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002164
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002165 /* For now RNDIS is always a second config */
2166 if (rndis)
2167 device_desc.bNumConfigurations = 2;
2168
Remy Bohmer23cd1382009-07-29 18:18:43 +02002169 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002170 if (rndis)
2171 dev_qualifier.bNumConfigurations = 2;
2172 else if (!cdc)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002173 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2174
2175 /* assumes ep0 uses the same value for both speeds ... */
2176 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2177
2178 /* and that all endpoints are dual-speed */
2179 hs_source_desc.bEndpointAddress =
2180 fs_source_desc.bEndpointAddress;
2181 hs_sink_desc.bEndpointAddress =
2182 fs_sink_desc.bEndpointAddress;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002183#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002184 if (status_ep)
2185 hs_status_desc.bEndpointAddress =
2186 fs_status_desc.bEndpointAddress;
2187#endif
2188 }
2189
2190 if (gadget_is_otg(gadget)) {
2191 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2192 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2193 eth_config.bMaxPower = 4;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002194#ifdef CONFIG_USB_ETH_RNDIS
2195 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2196 rndis_config.bMaxPower = 4;
2197#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002198 }
2199
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002200
2201 /* network device setup */
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302202#ifndef CONFIG_DM_ETH
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302203 dev->net = &l_priv->netdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302204#else
2205 dev->net = l_priv->netdev;
2206#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002207
2208 dev->cdc = cdc;
2209 dev->zlp = zlp;
2210
2211 dev->in_ep = in_ep;
2212 dev->out_ep = out_ep;
2213 dev->status_ep = status_ep;
2214
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302215 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002216 /*
2217 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002218 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmer23cd1382009-07-29 18:18:43 +02002219 * ends up in a persistent config database. It's not clear if
2220 * host side code for the SAFE thing cares -- its original BLAN
2221 * thing didn't, Sharp never assigned those addresses on Zaurii.
2222 */
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302223#ifndef CONFIG_DM_ETH
Remy Bohmer23cd1382009-07-29 18:18:43 +02002224 get_ether_addr(dev_addr, dev->net->enetaddr);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002225 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302226#else
2227 get_ether_addr(dev_addr, pdata->enetaddr);
2228 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2229#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002230
2231 get_ether_addr(host_addr, dev->host_mac);
2232
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002233 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2234 dev->host_mac[0], dev->host_mac[1],
2235 dev->host_mac[2], dev->host_mac[3],
2236 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002237
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002238 if (rndis) {
2239 status = rndis_init();
2240 if (status < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002241 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002242 goto fail;
2243 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002244 }
2245
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002246 /*
2247 * use PKTSIZE (or aligned... from u-boot) and set
2248 * wMaxSegmentSize accordingly
2249 */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002250 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2251
2252 /* preallocate control message data and buffer */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002253 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002254 if (!dev->req)
2255 goto fail;
2256 dev->req->buf = control_req;
2257 dev->req->complete = eth_setup_complete;
2258
2259 /* ... and maybe likewise for status transfer */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002260#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002261 if (dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002262 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2263 GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002264 if (!dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002265 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002266
2267 goto fail;
2268 }
Vitaly Kuzmichevdf559c12010-08-13 17:01:06 +04002269 dev->stat_req->buf = status_req;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002270 dev->stat_req->context = NULL;
2271 }
2272#endif
2273
2274 /* finish hookup to lower layer ... */
2275 dev->gadget = gadget;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002276 set_gadget_data(gadget, dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002277 gadget->ep0->driver_data = dev;
2278
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002279 /*
2280 * two kinds of host-initiated state changes:
Remy Bohmer23cd1382009-07-29 18:18:43 +02002281 * - iff DATA transfer is active, carrier is "on"
2282 * - tx queueing enabled if open *and* carrier is "on"
2283 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002284
2285 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2286 out_ep->name, in_ep->name,
2287 status_ep ? " STATUS " : "",
2288 status_ep ? status_ep->name : ""
2289 );
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302290#ifndef CONFIG_DM_ETH
2291 printf("MAC %pM\n", dev->net->enetaddr);
2292#else
2293 printf("MAC %pM\n", pdata->enetaddr);
2294#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002295
2296 if (cdc || rndis)
2297 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2298 dev->host_mac[0], dev->host_mac[1],
2299 dev->host_mac[2], dev->host_mac[3],
2300 dev->host_mac[4], dev->host_mac[5]);
2301
2302 if (rndis) {
2303 u32 vendorID = 0;
2304
2305 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2306
2307 dev->rndis_config = rndis_register(rndis_control_ack);
2308 if (dev->rndis_config < 0) {
2309fail0:
2310 eth_unbind(gadget);
2311 debug("RNDIS setup failed\n");
2312 status = -ENODEV;
2313 goto fail;
2314 }
2315
2316 /* these set up a lot of the OIDs that RNDIS needs */
2317 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2318 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2319 &dev->stats, &dev->cdc_filter))
2320 goto fail0;
2321 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2322 manufacturer))
2323 goto fail0;
2324 if (rndis_set_param_medium(dev->rndis_config,
2325 NDIS_MEDIUM_802_3, 0))
2326 goto fail0;
2327 printf("RNDIS ready\n");
2328 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002329 return 0;
2330
2331fail:
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002332 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002333 eth_unbind(gadget);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002334 return status;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002335}
2336
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002337/*-------------------------------------------------------------------------*/
Jean-Jacques Hiblot1c1464c2019-01-22 16:48:16 +01002338static void _usb_eth_halt(struct ether_priv *priv);
2339
Mugunthan V N8269ee42016-11-18 11:08:27 +05302340static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002341{
Mugunthan V Nae701002016-11-18 11:07:18 +05302342 struct eth_dev *dev = &priv->ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002343 struct usb_gadget *gadget;
2344 unsigned long ts;
Jean-Jacques Hiblota06955a2018-11-29 10:52:41 +01002345 int ret;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002346 unsigned long timeout = USB_CONNECT_TIMEOUT;
2347
Jean-Jacques Hiblota06955a2018-11-29 10:52:41 +01002348 ret = usb_gadget_initialize(0);
2349 if (ret)
2350 return ret;
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302351
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002352 /* Configure default mac-addresses for the USB ethernet device */
2353#ifdef CONFIG_USBNET_DEV_ADDR
2354 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2355#endif
2356#ifdef CONFIG_USBNET_HOST_ADDR
2357 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2358#endif
2359 /* Check if the user overruled the MAC addresses */
Simon Glass00caae62017-08-03 12:22:12 -06002360 if (env_get("usbnet_devaddr"))
2361 strlcpy(dev_addr, env_get("usbnet_devaddr"),
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002362 sizeof(dev_addr));
2363
Simon Glass00caae62017-08-03 12:22:12 -06002364 if (env_get("usbnet_hostaddr"))
2365 strlcpy(host_addr, env_get("usbnet_hostaddr"),
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002366 sizeof(host_addr));
2367
2368 if (!is_eth_addr_valid(dev_addr)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002369 pr_err("Need valid 'usbnet_devaddr' to be set");
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002370 goto fail;
2371 }
2372 if (!is_eth_addr_valid(host_addr)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002373 pr_err("Need valid 'usbnet_hostaddr' to be set");
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002374 goto fail;
2375 }
2376
Mugunthan V Nae701002016-11-18 11:07:18 +05302377 priv->eth_driver.speed = DEVSPEED;
2378 priv->eth_driver.bind = eth_bind;
2379 priv->eth_driver.unbind = eth_unbind;
2380 priv->eth_driver.setup = eth_setup;
2381 priv->eth_driver.reset = eth_disconnect;
2382 priv->eth_driver.disconnect = eth_disconnect;
2383 priv->eth_driver.suspend = eth_suspend;
2384 priv->eth_driver.resume = eth_resume;
2385 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen988ee3e2010-12-01 23:43:43 +08002386 goto fail;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002387
2388 dev->network_started = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002389
2390 packet_received = 0;
2391 packet_sent = 0;
2392
2393 gadget = dev->gadget;
2394 usb_gadget_connect(gadget);
2395
Simon Glass00caae62017-08-03 12:22:12 -06002396 if (env_get("cdc_connect_timeout"))
2397 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
Remy Bohmer23cd1382009-07-29 18:18:43 +02002398 NULL, 10) * CONFIG_SYS_HZ;
2399 ts = get_timer(0);
Mugunthan V N17b4f302016-11-18 10:49:13 +05302400 while (!dev->network_started) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002401 /* Handle control-c and timeouts */
2402 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002403 pr_err("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002404 goto fail;
2405 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302406 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002407 }
2408
Vitaly Kuzmichev98fae972010-09-22 13:13:56 +04002409 packet_received = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002410 rx_submit(dev, dev->rx_req, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002411 return 0;
2412fail:
Jean-Jacques Hiblot1c1464c2019-01-22 16:48:16 +01002413 _usb_eth_halt(priv);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002414 return -1;
2415}
2416
Mugunthan V N8269ee42016-11-18 11:08:27 +05302417static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002418{
2419 int retval;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002420 void *rndis_pkt = NULL;
Mugunthan V Nae701002016-11-18 11:07:18 +05302421 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002422 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002423 unsigned long ts;
2424 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002425
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002426 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002427
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002428 /* new buffer is needed to include RNDIS header */
2429 if (rndis_active(dev)) {
2430 rndis_pkt = malloc(length +
2431 sizeof(struct rndis_packet_msg_type));
2432 if (!rndis_pkt) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002433 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002434 goto drop;
2435 }
2436 rndis_add_hdr(rndis_pkt, length);
2437 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002438 packet, length);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002439 packet = rndis_pkt;
2440 length += sizeof(struct rndis_packet_msg_type);
2441 }
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002442 req->buf = packet;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002443 req->context = NULL;
2444 req->complete = tx_complete;
2445
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002446 /*
2447 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002448 * though any robust network rx path ignores extra padding.
2449 * and some hardware doesn't like to write zlps.
2450 */
2451 req->zero = 1;
2452 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2453 length++;
2454
2455 req->length = length;
2456#if 0
2457 /* throttle highspeed IRQ rate back slightly */
2458 if (gadget_is_dualspeed(dev->gadget))
2459 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2460 ? ((dev->tx_qlen % qmult) != 0) : 0;
2461#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002462 dev->tx_qlen = 1;
2463 ts = get_timer(0);
2464 packet_sent = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002465
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002466 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002467
2468 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002469 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002470 while (!packet_sent) {
2471 if (get_timer(ts) > timeout) {
2472 printf("timeout sending packets to usb ethernet\n");
2473 return -1;
2474 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302475 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002476 }
Heinrich Schuchardt3c425fc2020-04-19 12:11:12 +02002477 free(rndis_pkt);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002478
2479 return 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002480drop:
2481 dev->stats.tx_dropped++;
2482 return -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002483}
2484
Mugunthan V N8269ee42016-11-18 11:08:27 +05302485static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002486{
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302487 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002488
Remy Bohmer23cd1382009-07-29 18:18:43 +02002489 return 0;
2490}
2491
Jean-Jacques Hiblot1c1464c2019-01-22 16:48:16 +01002492static void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002493{
Mugunthan V Nae701002016-11-18 11:07:18 +05302494 struct eth_dev *dev = &priv->ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002495
Lei Wen988ee3e2010-12-01 23:43:43 +08002496 /* If the gadget not registered, simple return */
2497 if (!dev->gadget)
2498 return;
2499
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03002500 /*
2501 * Some USB controllers may need additional deinitialization here
2502 * before dropping pull-up (also due to hardware issues).
2503 * For example: unhandled interrupt with status stage started may
2504 * bring the controller to fully broken state (until board reset).
2505 * There are some variants to debug and fix such cases:
2506 * 1) In the case of RNDIS connection eth_stop can perform additional
2507 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2508 * 2) 'pullup' callback in your UDC driver can be improved to perform
2509 * this deinitialization.
2510 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002511 eth_stop(dev);
2512
Remy Bohmer23cd1382009-07-29 18:18:43 +02002513 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002514
2515 /* Clear pending interrupt */
2516 if (dev->network_started) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302517 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002518 dev->network_started = 0;
2519 }
2520
Mugunthan V Nae701002016-11-18 11:07:18 +05302521 usb_gadget_unregister_driver(&priv->eth_driver);
Jean-Jacques Hiblota06955a2018-11-29 10:52:41 +01002522 usb_gadget_release(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002523}
2524
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302525#ifndef CONFIG_DM_ETH
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09002526static int usb_eth_init(struct eth_device *netdev, struct bd_info *bd)
Mugunthan V N8269ee42016-11-18 11:08:27 +05302527{
2528 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2529
2530 return _usb_eth_init(priv);
2531}
2532
2533static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2534{
2535 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2536
2537 return _usb_eth_send(priv, packet, length);
2538}
2539
2540static int usb_eth_recv(struct eth_device *netdev)
2541{
2542 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2543 struct eth_dev *dev = &priv->ethdev;
2544 int ret;
2545
2546 ret = _usb_eth_recv(priv);
2547 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002548 pr_err("error packet receive\n");
Mugunthan V N8269ee42016-11-18 11:08:27 +05302549 return ret;
2550 }
2551
2552 if (!packet_received)
2553 return 0;
2554
2555 if (dev->rx_req) {
2556 net_process_received_packet(net_rx_packets[0],
2557 dev->rx_req->length);
2558 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002559 pr_err("dev->rx_req invalid");
Mugunthan V N8269ee42016-11-18 11:08:27 +05302560 }
2561 packet_received = 0;
2562 rx_submit(dev, dev->rx_req, 0);
2563
2564 return 0;
2565}
2566
2567void usb_eth_halt(struct eth_device *netdev)
2568{
2569 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2570
2571 _usb_eth_halt(priv);
2572}
2573
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +09002574int usb_eth_initialize(struct bd_info *bi)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002575{
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302576 struct eth_device *netdev = &l_priv->netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002577
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +03002578 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmer23cd1382009-07-29 18:18:43 +02002579
2580 netdev->init = usb_eth_init;
2581 netdev->send = usb_eth_send;
2582 netdev->recv = usb_eth_recv;
2583 netdev->halt = usb_eth_halt;
Mugunthan V Nae701002016-11-18 11:07:18 +05302584 netdev->priv = l_priv;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002585
Remy Bohmer23cd1382009-07-29 18:18:43 +02002586 eth_register(netdev);
2587 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002588}
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302589#else
2590static int usb_eth_start(struct udevice *dev)
2591{
2592 struct ether_priv *priv = dev_get_priv(dev);
2593
2594 return _usb_eth_init(priv);
2595}
2596
2597static int usb_eth_send(struct udevice *dev, void *packet, int length)
2598{
2599 struct ether_priv *priv = dev_get_priv(dev);
2600
2601 return _usb_eth_send(priv, packet, length);
2602}
2603
2604static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2605{
2606 struct ether_priv *priv = dev_get_priv(dev);
2607 struct eth_dev *ethdev = &priv->ethdev;
2608 int ret;
2609
2610 ret = _usb_eth_recv(priv);
2611 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002612 pr_err("error packet receive\n");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302613 return ret;
2614 }
2615
2616 if (packet_received) {
2617 if (ethdev->rx_req) {
2618 *packetp = (uchar *)net_rx_packets[0];
2619 return ethdev->rx_req->length;
2620 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002621 pr_err("dev->rx_req invalid");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302622 return -EFAULT;
2623 }
2624 }
2625
2626 return -EAGAIN;
2627}
2628
2629static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2630 int length)
2631{
2632 struct ether_priv *priv = dev_get_priv(dev);
2633 struct eth_dev *ethdev = &priv->ethdev;
2634
2635 packet_received = 0;
2636
2637 return rx_submit(ethdev, ethdev->rx_req, 0);
2638}
2639
2640static void usb_eth_stop(struct udevice *dev)
2641{
2642 struct ether_priv *priv = dev_get_priv(dev);
2643
2644 _usb_eth_halt(priv);
2645}
2646
2647static int usb_eth_probe(struct udevice *dev)
2648{
2649 struct ether_priv *priv = dev_get_priv(dev);
2650 struct eth_pdata *pdata = dev_get_platdata(dev);
2651
2652 priv->netdev = dev;
2653 l_priv = priv;
2654
2655 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
Simon Glassfd1e9592017-08-03 12:22:11 -06002656 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302657
2658 return 0;
2659}
2660
2661static const struct eth_ops usb_eth_ops = {
2662 .start = usb_eth_start,
2663 .send = usb_eth_send,
2664 .recv = usb_eth_recv,
2665 .free_pkt = usb_eth_free_pkt,
2666 .stop = usb_eth_stop,
2667};
2668
2669int usb_ether_init(void)
2670{
2671 struct udevice *dev;
2672 struct udevice *usb_dev;
2673 int ret;
2674
Jean-Jacques Hiblot01311622018-11-29 10:52:46 +01002675 ret = uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302676 if (!usb_dev || ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002677 pr_err("No USB device found\n");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302678 return ret;
2679 }
2680
2681 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2682 if (!dev || ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +09002683 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302684 return ret;
2685 }
2686
2687 return 0;
2688}
2689
2690U_BOOT_DRIVER(eth_usb) = {
2691 .name = "usb_ether",
2692 .id = UCLASS_ETH,
2693 .probe = usb_eth_probe,
2694 .ops = &usb_eth_ops,
2695 .priv_auto_alloc_size = sizeof(struct ether_priv),
2696 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2697 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2698};
2699#endif /* CONFIG_DM_ETH */