blob: 0b5a1a4796691eaebf3afd47843273057a65e161 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-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 Bohmerdf063442009-07-29 18:18:43 +02008 */
9
10#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Alex Kiernan9c215492018-04-01 09:22:38 +000012#include <environment.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +030014#include <linux/netdevice.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020015#include <linux/usb/ch9.h>
16#include <linux/usb/cdc.h>
17#include <linux/usb/gadget.h>
18#include <net.h>
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +053019#include <usb.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030020#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060021#include <memalign.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020022#include <linux/ctype.h>
23
24#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030025#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020026
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053027#include <dm.h>
Mugunthan V N095b7612016-11-18 11:09:15 +053028#include <dm/lists.h>
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053029#include <dm/uclass-internal.h>
30#include <dm/device-internal.h>
31
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030032#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040033
Remy Bohmerdf063442009-07-29 18:18:43 +020034#define atomic_read
35extern struct platform_data brd;
Remy Bohmerdf063442009-07-29 18:18:43 +020036
37
38unsigned packet_received, packet_sent;
39
Remy Bohmerdf063442009-07-29 18:18:43 +020040/*
41 * Ethernet gadget driver -- with CDC and non-CDC options
42 * Builds on hardware support for a full duplex link.
43 *
44 * CDC Ethernet is the standard USB solution for sending Ethernet frames
45 * using USB. Real hardware tends to use the same framing protocol but look
46 * different for control features. This driver strongly prefers to use
47 * this USB-IF standard as its open-systems interoperability solution;
48 * most host side USB stacks (except from Microsoft) support it.
49 *
50 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
51 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
52 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
53 *
54 * There's some hardware that can't talk CDC ECM. We make that hardware
55 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
56 * link-level setup only requires activating the configuration. Only the
57 * endpoint descriptors, and product/vendor IDs, are relevant; no control
58 * operations are available. Linux supports it, but other host operating
59 * systems may not. (This is a subset of CDC Ethernet.)
60 *
61 * It turns out that if you add a few descriptors to that "CDC Subset",
62 * (Windows) host side drivers from MCCI can treat it as one submode of
63 * a proprietary scheme called "SAFE" ... without needing to know about
64 * specific product/vendor IDs. So we do that, making it easier to use
65 * those MS-Windows drivers. Those added descriptors make it resemble a
66 * CDC MDLM device, but they don't change device behavior at all. (See
67 * MCCI Engineering report 950198 "SAFE Networking Functions".)
68 *
69 * A third option is also in use. Rather than CDC Ethernet, or something
70 * simpler, Microsoft pushes their own approach: RNDIS. The published
71 * RNDIS specs are ambiguous and appear to be incomplete, and are also
72 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
73 */
Remy Bohmerdf063442009-07-29 18:18:43 +020074
75#define DRIVER_DESC "Ethernet Gadget"
76/* Based on linux 2.6.27 version */
77#define DRIVER_VERSION "May Day 2005"
78
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040079static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020080
81#define RX_EXTRA 20 /* guard against rx overflows */
82
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030083#ifndef CONFIG_USB_ETH_RNDIS
84#define rndis_uninit(x) do {} while (0)
85#define rndis_deregister(c) do {} while (0)
86#define rndis_exit() do {} while (0)
87#endif
88
89/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +020090#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
91 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
92 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
93 |USB_CDC_PACKET_TYPE_DIRECTED)
94
95#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
96
97/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +030098
99struct eth_dev {
100 struct usb_gadget *gadget;
101 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300102 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300103
104 u8 config;
105 struct usb_ep *in_ep, *out_ep, *status_ep;
106 const struct usb_endpoint_descriptor
107 *in, *out, *status;
108
109 struct usb_request *tx_req, *rx_req;
110
Mugunthan V N095b7612016-11-18 11:09:15 +0530111#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300112 struct eth_device *net;
Mugunthan V N095b7612016-11-18 11:09:15 +0530113#else
114 struct udevice *net;
115#endif
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300116 struct net_device_stats stats;
117 unsigned int tx_qlen;
118
119 unsigned zlp:1;
120 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300121 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300122 unsigned suspended:1;
123 unsigned network_started:1;
124 u16 cdc_filter;
125 unsigned long todo;
126 int mtu;
127#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300128 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300129 u8 host_mac[ETH_ALEN];
130};
131
132/*
133 * This version autoconfigures as much as possible at run-time.
134 *
135 * It also ASSUMES a self-powered device, without remote wakeup,
136 * although remote wakeup support would make sense.
137 */
138
139/*-------------------------------------------------------------------------*/
Mugunthan V N88e48be2016-11-18 11:06:13 +0530140struct ether_priv {
141 struct eth_dev ethdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530142#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +0530143 struct eth_device netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530144#else
145 struct udevice *netdev;
146#endif
Mugunthan V N88e48be2016-11-18 11:06:13 +0530147 struct usb_gadget_driver eth_driver;
148};
149
150struct ether_priv eth_priv;
151struct ether_priv *l_priv = &eth_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +0200152
153/*-------------------------------------------------------------------------*/
154
155/* "main" config is either CDC, or its simple subset */
156static inline int is_cdc(struct eth_dev *dev)
157{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200158#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200159 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200160#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200161 return 0; /* only subset possible */
162#else
163 return dev->cdc; /* depends on what hardware we found */
164#endif
165}
166
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300167/* "secondary" RNDIS config may sometimes be activated */
168static inline int rndis_active(struct eth_dev *dev)
169{
170#ifdef CONFIG_USB_ETH_RNDIS
171 return dev->rndis;
172#else
173 return 0;
174#endif
175}
176
177#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
178#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200179
180#define DEFAULT_QLEN 2 /* double buffering by default */
181
182/* peak bulk transfer bits-per-second */
183#define HS_BPS (13 * 512 * 8 * 1000 * 8)
184#define FS_BPS (19 * 64 * 1 * 1000 * 8)
185
186#ifdef CONFIG_USB_GADGET_DUALSPEED
187#define DEVSPEED USB_SPEED_HIGH
188
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400189#ifdef CONFIG_USB_ETH_QMULT
190#define qmult CONFIG_USB_ETH_QMULT
191#else
192#define qmult 5
193#endif
194
Remy Bohmerdf063442009-07-29 18:18:43 +0200195/* for dual-speed hardware, use deeper queues at highspeed */
196#define qlen(gadget) \
197 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
198
199static inline int BITRATE(struct usb_gadget *g)
200{
201 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
202}
203
204#else /* full speed (low speed doesn't do bulk) */
205
206#define qmult 1
207
208#define DEVSPEED USB_SPEED_FULL
209
210#define qlen(gadget) DEFAULT_QLEN
211
212static inline int BITRATE(struct usb_gadget *g)
213{
214 return FS_BPS;
215}
216#endif
217
Remy Bohmerdf063442009-07-29 18:18:43 +0200218/*-------------------------------------------------------------------------*/
219
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400220/*
221 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200222 * Instead: allocate your own, using normal USB-IF procedures.
223 */
224
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400225/*
226 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200227 * It's for devices with only CDC Ethernet configurations.
228 */
229#define CDC_VENDOR_NUM 0x0525 /* NetChip */
230#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
231
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400232/*
233 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200234 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
235 * with pxa250. We're protocol-compatible, if the host-side drivers
236 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
237 * drivers that need to hard-wire endpoint numbers have a hook.
238 *
239 * The protocol is a minimal subset of CDC Ether, which works on any bulk
240 * hardware that's not deeply broken ... even on hardware that can't talk
241 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
242 * doesn't handle control-OUT).
243 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300244#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
245#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200246
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400247/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300248 * For hardware that can talk RNDIS and either of the above protocols,
249 * use this ID ... the windows INF files will know it. Unless it's
250 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
251 * the non-RNDIS configuration.
252 */
253#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
254#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
255
256/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400257 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200258 * device descriptor, either numbers or strings or both. These string
259 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
260 */
261
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300262/*
263 * Emulating them in eth_bind:
264 * static ushort idVendor;
265 * static ushort idProduct;
266 */
267
Maxime Ripard48e78f82017-09-07 09:15:08 +0200268#if defined(CONFIG_USB_GADGET_MANUFACTURER)
269static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmerdf063442009-07-29 18:18:43 +0200270#else
Bin Meng75574052016-02-05 19:30:11 -0800271static char *iManufacturer = "U-Boot";
Remy Bohmerdf063442009-07-29 18:18:43 +0200272#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300273
274/* These probably need to be configurable. */
275static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200276static char *iProduct;
277static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300278
Remy Bohmerdf063442009-07-29 18:18:43 +0200279static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300280
Remy Bohmerdf063442009-07-29 18:18:43 +0200281static char host_addr[18];
282
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300283
Remy Bohmerdf063442009-07-29 18:18:43 +0200284/*-------------------------------------------------------------------------*/
285
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400286/*
287 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200288 * ep0 implementation: descriptors, config management, setup().
289 * also optional class-specific notification interrupt transfer.
290 */
291
292/*
293 * DESCRIPTORS ... most are static, but strings and (full) configuration
294 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300295 * our simple subset, with RNDIS as an optional second configuration.
296 *
297 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
298 * the class descriptors match a modem (they're ignored; it's really just
299 * Ethernet functionality), they don't need the NOP altsetting, and the
300 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200301 */
302
303#define STRING_MANUFACTURER 1
304#define STRING_PRODUCT 2
305#define STRING_ETHADDR 3
306#define STRING_DATA 4
307#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300308#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200309#define STRING_CDC 7
310#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300311#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200312#define STRING_SERIALNUMBER 10
313
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300314/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200315#define USB_BUFSIZ 256
316
317/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300318 * This device advertises one configuration, eth_config, unless RNDIS
319 * is enabled (rndis_config) on hardware supporting at least two configs.
320 *
321 * NOTE: Controllers like superh_udc should probably be able to use
322 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200323 *
324 * FIXME define some higher-powered configurations to make it easier
325 * to recharge batteries ...
326 */
327
328#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300329#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200330
331static struct usb_device_descriptor
332device_desc = {
333 .bLength = sizeof device_desc,
334 .bDescriptorType = USB_DT_DEVICE,
335
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400336 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200337
338 .bDeviceClass = USB_CLASS_COMM,
339 .bDeviceSubClass = 0,
340 .bDeviceProtocol = 0,
341
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400342 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
343 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200344 .iManufacturer = STRING_MANUFACTURER,
345 .iProduct = STRING_PRODUCT,
346 .bNumConfigurations = 1,
347};
348
349static struct usb_otg_descriptor
350otg_descriptor = {
351 .bLength = sizeof otg_descriptor,
352 .bDescriptorType = USB_DT_OTG,
353
354 .bmAttributes = USB_OTG_SRP,
355};
356
357static struct usb_config_descriptor
358eth_config = {
359 .bLength = sizeof eth_config,
360 .bDescriptorType = USB_DT_CONFIG,
361
362 /* compute wTotalLength on the fly */
363 .bNumInterfaces = 2,
364 .bConfigurationValue = DEV_CONFIG_VALUE,
365 .iConfiguration = STRING_CDC,
366 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
367 .bMaxPower = 1,
368};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300369
370#ifdef CONFIG_USB_ETH_RNDIS
371static struct usb_config_descriptor
372rndis_config = {
373 .bLength = sizeof rndis_config,
374 .bDescriptorType = USB_DT_CONFIG,
375
376 /* compute wTotalLength on the fly */
377 .bNumInterfaces = 2,
378 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
379 .iConfiguration = STRING_RNDIS,
380 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
381 .bMaxPower = 1,
382};
383#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200384
385/*
386 * Compared to the simple CDC subset, the full CDC Ethernet model adds
387 * three class descriptors, two interface descriptors, optional status
388 * endpoint. Both have a "data" interface and two bulk endpoints.
389 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300390 *
391 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
392 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
393 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
394 * work around those bugs) are unlikely to ever come from MSFT, you may
395 * wish to avoid using RNDIS.
396 *
397 * MCCI offers an alternative to RNDIS if you need to connect to Windows
398 * but have hardware that can't support CDC Ethernet. We add descriptors
399 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
400 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
401 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200402 */
403
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200404#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200405static struct usb_interface_descriptor
406control_intf = {
407 .bLength = sizeof control_intf,
408 .bDescriptorType = USB_DT_INTERFACE,
409
410 .bInterfaceNumber = 0,
411 /* status endpoint is optional; this may be patched later */
412 .bNumEndpoints = 1,
413 .bInterfaceClass = USB_CLASS_COMM,
414 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
415 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
416 .iInterface = STRING_CONTROL,
417};
418#endif
419
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300420#ifdef CONFIG_USB_ETH_RNDIS
421static const struct usb_interface_descriptor
422rndis_control_intf = {
423 .bLength = sizeof rndis_control_intf,
424 .bDescriptorType = USB_DT_INTERFACE,
425
426 .bInterfaceNumber = 0,
427 .bNumEndpoints = 1,
428 .bInterfaceClass = USB_CLASS_COMM,
429 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
430 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
431 .iInterface = STRING_RNDIS_CONTROL,
432};
433#endif
434
Remy Bohmerdf063442009-07-29 18:18:43 +0200435static const struct usb_cdc_header_desc header_desc = {
436 .bLength = sizeof header_desc,
437 .bDescriptorType = USB_DT_CS_INTERFACE,
438 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
439
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400440 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200441};
442
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200443#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200444
445static const struct usb_cdc_union_desc union_desc = {
446 .bLength = sizeof union_desc,
447 .bDescriptorType = USB_DT_CS_INTERFACE,
448 .bDescriptorSubType = USB_CDC_UNION_TYPE,
449
450 .bMasterInterface0 = 0, /* index of control interface */
451 .bSlaveInterface0 = 1, /* index of DATA interface */
452};
453
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300454#endif /* CDC || RNDIS */
455
456#ifdef CONFIG_USB_ETH_RNDIS
457
458static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
459 .bLength = sizeof call_mgmt_descriptor,
460 .bDescriptorType = USB_DT_CS_INTERFACE,
461 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
462
463 .bmCapabilities = 0x00,
464 .bDataInterface = 0x01,
465};
466
467static const struct usb_cdc_acm_descriptor acm_descriptor = {
468 .bLength = sizeof acm_descriptor,
469 .bDescriptorType = USB_DT_CS_INTERFACE,
470 .bDescriptorSubType = USB_CDC_ACM_TYPE,
471
472 .bmCapabilities = 0x00,
473};
474
475#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200476
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200477#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200478
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400479/*
480 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200481 * ways: data endpoints live in the control interface, there's no data
482 * interface, and it's not used to talk to a cell phone radio.
483 */
484
485static const struct usb_cdc_mdlm_desc mdlm_desc = {
486 .bLength = sizeof mdlm_desc,
487 .bDescriptorType = USB_DT_CS_INTERFACE,
488 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
489
490 .bcdVersion = __constant_cpu_to_le16(0x0100),
491 .bGUID = {
492 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
493 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
494 },
495};
496
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400497/*
498 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200499 * can't really use its struct. All we do here is say that we're using
500 * the submode of "SAFE" which directly matches the CDC Subset.
501 */
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530502#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200503static const u8 mdlm_detail_desc[] = {
504 6,
505 USB_DT_CS_INTERFACE,
506 USB_CDC_MDLM_DETAIL_TYPE,
507
508 0, /* "SAFE" */
509 0, /* network control capabilities (none) */
510 0, /* network data capabilities ("raw" encapsulation) */
511};
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530512#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200513
514#endif
515
Remy Bohmerdf063442009-07-29 18:18:43 +0200516static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400517 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200518 .bDescriptorType = USB_DT_CS_INTERFACE,
519 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
520
521 /* this descriptor actually adds value, surprise! */
522 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400523 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
Bin Meng2ccb8b02018-07-31 02:55:22 -0700524 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400525 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200526 .bNumberPowerFilters = 0,
527};
528
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200529#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200530
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400531/*
532 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200533 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
534 * packet, to simplify cancellation; and a big transfer interval, to
535 * waste less bandwidth.
536 *
537 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
538 * if they ignore the connect/disconnect notifications that real aether
539 * can provide. more advanced cdc configurations might want to support
540 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300541 *
542 * RNDIS requires the status endpoint, since it uses that encapsulation
543 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200544 */
545
546#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
547#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
548
549static struct usb_endpoint_descriptor
550fs_status_desc = {
551 .bLength = USB_DT_ENDPOINT_SIZE,
552 .bDescriptorType = USB_DT_ENDPOINT,
553
554 .bEndpointAddress = USB_DIR_IN,
555 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400556 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200557 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
558};
559#endif
560
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200561#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200562
563/* the default data interface has no endpoints ... */
564
565static const struct usb_interface_descriptor
566data_nop_intf = {
567 .bLength = sizeof data_nop_intf,
568 .bDescriptorType = USB_DT_INTERFACE,
569
570 .bInterfaceNumber = 1,
571 .bAlternateSetting = 0,
572 .bNumEndpoints = 0,
573 .bInterfaceClass = USB_CLASS_CDC_DATA,
574 .bInterfaceSubClass = 0,
575 .bInterfaceProtocol = 0,
576};
577
578/* ... but the "real" data interface has two bulk endpoints */
579
580static const struct usb_interface_descriptor
581data_intf = {
582 .bLength = sizeof data_intf,
583 .bDescriptorType = USB_DT_INTERFACE,
584
585 .bInterfaceNumber = 1,
586 .bAlternateSetting = 1,
587 .bNumEndpoints = 2,
588 .bInterfaceClass = USB_CLASS_CDC_DATA,
589 .bInterfaceSubClass = 0,
590 .bInterfaceProtocol = 0,
591 .iInterface = STRING_DATA,
592};
593
594#endif
595
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300596#ifdef CONFIG_USB_ETH_RNDIS
597
598/* RNDIS doesn't activate by changing to the "real" altsetting */
599
600static const struct usb_interface_descriptor
601rndis_data_intf = {
602 .bLength = sizeof rndis_data_intf,
603 .bDescriptorType = USB_DT_INTERFACE,
604
605 .bInterfaceNumber = 1,
606 .bAlternateSetting = 0,
607 .bNumEndpoints = 2,
608 .bInterfaceClass = USB_CLASS_CDC_DATA,
609 .bInterfaceSubClass = 0,
610 .bInterfaceProtocol = 0,
611 .iInterface = STRING_DATA,
612};
613
614#endif
615
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200616#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200617
618/*
619 * "Simple" CDC-subset option is a simple vendor-neutral model that most
620 * full speed controllers can handle: one interface, two bulk endpoints.
621 *
622 * To assist host side drivers, we fancy it up a bit, and add descriptors
623 * so some host side drivers will understand it as a "SAFE" variant.
624 */
625
626static const struct usb_interface_descriptor
627subset_data_intf = {
628 .bLength = sizeof subset_data_intf,
629 .bDescriptorType = USB_DT_INTERFACE,
630
631 .bInterfaceNumber = 0,
632 .bAlternateSetting = 0,
633 .bNumEndpoints = 2,
634 .bInterfaceClass = USB_CLASS_COMM,
635 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
636 .bInterfaceProtocol = 0,
637 .iInterface = STRING_DATA,
638};
639
640#endif /* SUBSET */
641
Remy Bohmerdf063442009-07-29 18:18:43 +0200642static struct usb_endpoint_descriptor
643fs_source_desc = {
644 .bLength = USB_DT_ENDPOINT_SIZE,
645 .bDescriptorType = USB_DT_ENDPOINT,
646
647 .bEndpointAddress = USB_DIR_IN,
648 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700649 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200650};
651
652static struct usb_endpoint_descriptor
653fs_sink_desc = {
654 .bLength = USB_DT_ENDPOINT_SIZE,
655 .bDescriptorType = USB_DT_ENDPOINT,
656
657 .bEndpointAddress = USB_DIR_OUT,
658 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700659 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200660};
661
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400662static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200663 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200664#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200665 /* "cdc" mode descriptors */
666 (struct usb_descriptor_header *) &control_intf,
667 (struct usb_descriptor_header *) &header_desc,
668 (struct usb_descriptor_header *) &union_desc,
669 (struct usb_descriptor_header *) &ether_desc,
670 /* NOTE: status endpoint may need to be removed */
671 (struct usb_descriptor_header *) &fs_status_desc,
672 /* data interface, with altsetting */
673 (struct usb_descriptor_header *) &data_nop_intf,
674 (struct usb_descriptor_header *) &data_intf,
675 (struct usb_descriptor_header *) &fs_source_desc,
676 (struct usb_descriptor_header *) &fs_sink_desc,
677 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200678#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200679};
680
681static inline void fs_subset_descriptors(void)
682{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200683#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200684 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
685 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
686 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
687 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
688 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
689 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
690 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
691 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
692 fs_eth_function[8] = NULL;
693#else
694 fs_eth_function[1] = NULL;
695#endif
696}
697
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300698#ifdef CONFIG_USB_ETH_RNDIS
699static const struct usb_descriptor_header *fs_rndis_function[] = {
700 (struct usb_descriptor_header *) &otg_descriptor,
701 /* control interface matches ACM, not Ethernet */
702 (struct usb_descriptor_header *) &rndis_control_intf,
703 (struct usb_descriptor_header *) &header_desc,
704 (struct usb_descriptor_header *) &call_mgmt_descriptor,
705 (struct usb_descriptor_header *) &acm_descriptor,
706 (struct usb_descriptor_header *) &union_desc,
707 (struct usb_descriptor_header *) &fs_status_desc,
708 /* data interface has no altsetting */
709 (struct usb_descriptor_header *) &rndis_data_intf,
710 (struct usb_descriptor_header *) &fs_source_desc,
711 (struct usb_descriptor_header *) &fs_sink_desc,
712 NULL,
713};
714#endif
715
Remy Bohmerdf063442009-07-29 18:18:43 +0200716/*
717 * usb 2.0 devices need to expose both high speed and full speed
718 * descriptors, unless they only run at full speed.
719 */
720
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200721#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200722static struct usb_endpoint_descriptor
723hs_status_desc = {
724 .bLength = USB_DT_ENDPOINT_SIZE,
725 .bDescriptorType = USB_DT_ENDPOINT,
726
727 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400728 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200729 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
730};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200731#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200732
733static struct usb_endpoint_descriptor
734hs_source_desc = {
735 .bLength = USB_DT_ENDPOINT_SIZE,
736 .bDescriptorType = USB_DT_ENDPOINT,
737
738 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400739 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200740};
741
742static struct usb_endpoint_descriptor
743hs_sink_desc = {
744 .bLength = USB_DT_ENDPOINT_SIZE,
745 .bDescriptorType = USB_DT_ENDPOINT,
746
747 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400748 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200749};
750
751static struct usb_qualifier_descriptor
752dev_qualifier = {
753 .bLength = sizeof dev_qualifier,
754 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
755
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400756 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200757 .bDeviceClass = USB_CLASS_COMM,
758
759 .bNumConfigurations = 1,
760};
761
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400762static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200763 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200764#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200765 /* "cdc" mode descriptors */
766 (struct usb_descriptor_header *) &control_intf,
767 (struct usb_descriptor_header *) &header_desc,
768 (struct usb_descriptor_header *) &union_desc,
769 (struct usb_descriptor_header *) &ether_desc,
770 /* NOTE: status endpoint may need to be removed */
771 (struct usb_descriptor_header *) &hs_status_desc,
772 /* data interface, with altsetting */
773 (struct usb_descriptor_header *) &data_nop_intf,
774 (struct usb_descriptor_header *) &data_intf,
775 (struct usb_descriptor_header *) &hs_source_desc,
776 (struct usb_descriptor_header *) &hs_sink_desc,
777 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200778#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200779};
780
781static inline void hs_subset_descriptors(void)
782{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200783#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200784 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
785 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
786 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
787 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
788 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
789 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
790 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
791 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
792 hs_eth_function[8] = NULL;
793#else
794 hs_eth_function[1] = NULL;
795#endif
796}
797
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300798#ifdef CONFIG_USB_ETH_RNDIS
799static const struct usb_descriptor_header *hs_rndis_function[] = {
800 (struct usb_descriptor_header *) &otg_descriptor,
801 /* control interface matches ACM, not Ethernet */
802 (struct usb_descriptor_header *) &rndis_control_intf,
803 (struct usb_descriptor_header *) &header_desc,
804 (struct usb_descriptor_header *) &call_mgmt_descriptor,
805 (struct usb_descriptor_header *) &acm_descriptor,
806 (struct usb_descriptor_header *) &union_desc,
807 (struct usb_descriptor_header *) &hs_status_desc,
808 /* data interface has no altsetting */
809 (struct usb_descriptor_header *) &rndis_data_intf,
810 (struct usb_descriptor_header *) &hs_source_desc,
811 (struct usb_descriptor_header *) &hs_sink_desc,
812 NULL,
813};
814#endif
815
816
Remy Bohmerdf063442009-07-29 18:18:43 +0200817/* maxpacket and other transfer characteristics vary by speed. */
818static inline struct usb_endpoint_descriptor *
819ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
820 struct usb_endpoint_descriptor *fs)
821{
822 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
823 return hs;
824 return fs;
825}
826
Remy Bohmerdf063442009-07-29 18:18:43 +0200827/*-------------------------------------------------------------------------*/
828
829/* descriptors that are built on-demand */
830
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400831static char manufacturer[50];
832static char product_desc[40] = DRIVER_DESC;
833static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200834
835/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400836static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200837
838/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400839static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200840 { STRING_MANUFACTURER, manufacturer, },
841 { STRING_PRODUCT, product_desc, },
842 { STRING_SERIALNUMBER, serial_number, },
843 { STRING_DATA, "Ethernet Data", },
844 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200845#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200846 { STRING_CDC, "CDC Ethernet", },
847 { STRING_CONTROL, "CDC Communications Control", },
848#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200849#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200850 { STRING_SUBSET, "CDC Ethernet Subset", },
851#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300852#ifdef CONFIG_USB_ETH_RNDIS
853 { STRING_RNDIS, "RNDIS", },
854 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
855#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200856 { } /* end of list */
857};
858
859static struct usb_gadget_strings stringtab = {
860 .language = 0x0409, /* en-us */
861 .strings = strings,
862};
863
Remy Bohmerdf063442009-07-29 18:18:43 +0200864/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500865DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
866
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200867#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500868DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200869#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200870
Remy Bohmerdf063442009-07-29 18:18:43 +0200871/*============================================================================*/
872
873/*
874 * one config, two interfaces: control, data.
875 * complications: class descriptors, and an altsetting.
876 */
877static int
878config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
879{
880 int len;
881 const struct usb_config_descriptor *config;
882 const struct usb_descriptor_header **function;
883 int hs = 0;
884
885 if (gadget_is_dualspeed(g)) {
886 hs = (g->speed == USB_SPEED_HIGH);
887 if (type == USB_DT_OTHER_SPEED_CONFIG)
888 hs = !hs;
889 }
890#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
891
892 if (index >= device_desc.bNumConfigurations)
893 return -EINVAL;
894
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300895#ifdef CONFIG_USB_ETH_RNDIS
896 /*
897 * list the RNDIS config first, to make Microsoft's drivers
898 * happy. DOCSIS 1.0 needs this too.
899 */
900 if (device_desc.bNumConfigurations == 2 && index == 0) {
901 config = &rndis_config;
902 function = which_fn(rndis);
903 } else
904#endif
905 {
906 config = &eth_config;
907 function = which_fn(eth);
908 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200909
910 /* for now, don't advertise srp-only devices */
911 if (!is_otg)
912 function++;
913
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400914 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200915 if (len < 0)
916 return len;
917 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
918 return len;
919}
920
921/*-------------------------------------------------------------------------*/
922
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300923static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400924static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200925
926static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400927set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200928{
929 int result = 0;
930 struct usb_gadget *gadget = dev->gadget;
931
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200932#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300933 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200934 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400935 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200936 &fs_status_desc);
937 dev->status_ep->driver_data = dev;
938
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400939 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200940 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400941 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200942 dev->status_ep->name, result);
943 goto done;
944 }
945 }
946#endif
947
948 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
949 dev->in_ep->driver_data = dev;
950
951 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
952 dev->out_ep->driver_data = dev;
953
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400954 /*
955 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200956 * endpoints in the default altsetting for the interface.
957 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300958 *
959 * Strictly speaking RNDIS should work the same: activation is
960 * a side effect of setting a packet filter. Deactivation is
961 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200962 */
963 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400964 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200965 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400966 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200967 dev->in_ep->name, result);
968 goto done;
969 }
970
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400971 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200972 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400973 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200974 dev->out_ep->name, result);
975 goto done;
976 }
977 }
978
979done:
980 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400981 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200982
983 /* on error, disable any endpoints */
984 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400985 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400986 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200987 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400988 (void) usb_ep_disable(dev->in_ep);
989 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200990 dev->in = NULL;
991 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300992 } else if (!cdc_active(dev)) {
993 /*
994 * activate non-CDC configs right away
995 * this isn't strictly according to the RNDIS spec
996 */
997 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +0200998 }
999
1000 /* caller is responsible for cleanup on error */
1001 return result;
1002}
1003
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001004static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001005{
1006 if (dev->config == 0)
1007 return;
1008
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001009 debug("%s\n", __func__);
1010
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001011 rndis_uninit(dev->rndis_config);
1012
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001013 /*
1014 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001015 * pending i/o. then free the requests.
1016 */
1017
1018 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001019 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001020 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001021 usb_ep_free_request(dev->in_ep, dev->tx_req);
1022 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001023 }
1024 }
1025 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001026 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001027 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001028 usb_ep_free_request(dev->out_ep, dev->rx_req);
1029 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001030 }
1031 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001032 if (dev->status)
1033 usb_ep_disable(dev->status_ep);
1034
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001035 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001036 dev->cdc_filter = 0;
1037 dev->config = 0;
1038}
1039
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001040/*
1041 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001042 * that returns config descriptors, and altsetting code.
1043 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001044static int eth_set_config(struct eth_dev *dev, unsigned number,
1045 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001046{
1047 int result = 0;
1048 struct usb_gadget *gadget = dev->gadget;
1049
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001050 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001051 && dev->config
1052 && dev->tx_qlen != 0) {
1053 /* tx fifo is full, but we can't clear it...*/
Masahiro Yamada81e10422017-09-16 14:10:41 +09001054 pr_err("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001055 return -ESPIPE;
1056 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001057 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001058
1059 switch (number) {
1060 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001061 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001062 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001063#ifdef CONFIG_USB_ETH_RNDIS
1064 case DEV_RNDIS_CONFIG_VALUE:
1065 dev->rndis = 1;
1066 result = set_ether_config(dev, gfp_flags);
1067 break;
1068#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001069 default:
1070 result = -EINVAL;
1071 /* FALL THROUGH */
1072 case 0:
1073 break;
1074 }
1075
1076 if (result) {
1077 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001078 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001079 usb_gadget_vbus_draw(dev->gadget,
1080 gadget_is_otg(dev->gadget) ? 8 : 100);
1081 } else {
1082 char *speed;
1083 unsigned power;
1084
1085 power = 2 * eth_config.bMaxPower;
1086 usb_gadget_vbus_draw(dev->gadget, power);
1087
1088 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001089 case USB_SPEED_FULL:
1090 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001091#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001092 case USB_SPEED_HIGH:
1093 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001094#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001095 default:
1096 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001097 }
1098
1099 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001100 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001101 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001102 rndis_active(dev)
1103 ? "RNDIS"
1104 : (cdc_active(dev)
1105 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001106 : "CDC Ethernet Subset"));
1107 }
1108 return result;
1109}
1110
1111/*-------------------------------------------------------------------------*/
1112
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001113#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001114
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001115/*
1116 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001117 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001118 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001119 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1120 * command mechanism; and only one status request is ever queued.
1121 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001122static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001123{
1124 struct usb_cdc_notification *event = req->buf;
1125 int value = req->status;
1126 struct eth_dev *dev = ep->driver_data;
1127
1128 /* issue the second notification if host reads the first */
1129 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1130 && value == 0) {
1131 __le32 *data = req->buf + sizeof *event;
1132
1133 event->bmRequestType = 0xA1;
1134 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001135 event->wValue = __constant_cpu_to_le16(0);
1136 event->wIndex = __constant_cpu_to_le16(1);
1137 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001138
1139 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001140 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001141
1142 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001143 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001144 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001145 if (value == 0)
1146 return;
1147 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001148 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001149 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001150 if (event->bNotificationType ==
1151 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301152 dev->network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001153 printf("USB network up!\n");
1154 }
1155 }
1156 req->context = NULL;
1157}
1158
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001159static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001160{
1161 struct usb_request *req = dev->stat_req;
1162 struct usb_cdc_notification *event;
1163 int value;
1164
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001165 /*
1166 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001167 *
1168 * FIXME ugly idiom, maybe we'd be better with just
1169 * a "cancel the whole queue" primitive since any
1170 * unlink-one primitive has way too many error modes.
1171 * here, we "know" toggle is already clear...
1172 *
1173 * FIXME iff req->context != null just dequeue it
1174 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001175 usb_ep_disable(dev->status_ep);
1176 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001177
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001178 /*
1179 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001180 * a SPEED_CHANGE. could be useful in some configs.
1181 */
1182 event = req->buf;
1183 event->bmRequestType = 0xA1;
1184 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001185 event->wValue = __constant_cpu_to_le16(1); /* connected */
1186 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001187 event->wLength = 0;
1188
1189 req->length = sizeof *event;
1190 req->complete = eth_status_complete;
1191 req->context = dev;
1192
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001193 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001194 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001195 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001196}
1197
1198#endif
1199
1200/*-------------------------------------------------------------------------*/
1201
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001202static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001203{
1204 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001205 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001206 req->status, req->actual, req->length);
1207}
1208
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001209#ifdef CONFIG_USB_ETH_RNDIS
1210
1211static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1212{
1213 if (req->status || req->actual != req->length)
1214 debug("rndis response complete --> %d, %d/%d\n",
1215 req->status, req->actual, req->length);
1216
1217 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1218}
1219
1220static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1221{
1222 struct eth_dev *dev = ep->driver_data;
1223 int status;
1224
1225 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1226 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1227 if (status < 0)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001228 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001229}
1230
1231#endif /* RNDIS */
1232
Remy Bohmerdf063442009-07-29 18:18:43 +02001233/*
1234 * The setup() callback implements all the ep0 functionality that's not
1235 * handled lower down. CDC has a number of less-common features:
1236 *
1237 * - two interfaces: control, and ethernet data
1238 * - Ethernet data interface has two altsettings: default, and active
1239 * - class-specific descriptors for the control interface
1240 * - class-specific control requests
1241 */
1242static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001243eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001244{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001245 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001246 struct usb_request *req = dev->req;
1247 int value = -EOPNOTSUPP;
1248 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1249 u16 wValue = le16_to_cpu(ctrl->wValue);
1250 u16 wLength = le16_to_cpu(ctrl->wLength);
1251
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001252 /*
1253 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001254 * while config change events may enable network traffic.
1255 */
1256
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001257 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001258
1259 req->complete = eth_setup_complete;
1260 switch (ctrl->bRequest) {
1261
1262 case USB_REQ_GET_DESCRIPTOR:
1263 if (ctrl->bRequestType != USB_DIR_IN)
1264 break;
1265 switch (wValue >> 8) {
1266
1267 case USB_DT_DEVICE:
Kishon Vijay Abraham I58d701e2015-02-23 18:40:17 +05301268 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001269 value = min(wLength, (u16) sizeof device_desc);
1270 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001271 break;
1272 case USB_DT_DEVICE_QUALIFIER:
1273 if (!gadget_is_dualspeed(gadget))
1274 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001275 value = min(wLength, (u16) sizeof dev_qualifier);
1276 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001277 break;
1278
1279 case USB_DT_OTHER_SPEED_CONFIG:
1280 if (!gadget_is_dualspeed(gadget))
1281 break;
1282 /* FALLTHROUGH */
1283 case USB_DT_CONFIG:
1284 value = config_buf(gadget, req->buf,
1285 wValue >> 8,
1286 wValue & 0xff,
1287 gadget_is_otg(gadget));
1288 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001289 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001290 break;
1291
1292 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001293 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001294 wValue & 0xff, req->buf);
1295
1296 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001297 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001298
1299 break;
1300 }
1301 break;
1302
1303 case USB_REQ_SET_CONFIGURATION:
1304 if (ctrl->bRequestType != 0)
1305 break;
1306 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001307 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001308 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001309 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001310 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001311 break;
1312 case USB_REQ_GET_CONFIGURATION:
1313 if (ctrl->bRequestType != USB_DIR_IN)
1314 break;
1315 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001316 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001317 break;
1318
1319 case USB_REQ_SET_INTERFACE:
1320 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1321 || !dev->config
1322 || wIndex > 1)
1323 break;
1324 if (!cdc_active(dev) && wIndex != 0)
1325 break;
1326
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001327 /*
1328 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001329 * we need to kluge around that interference.
1330 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001331 if (gadget_is_pxa(gadget)) {
1332 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001333 GFP_ATOMIC);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001334 /*
1335 * PXA25x driver use non-CDC ethernet gadget.
1336 * But only _CDC and _RNDIS code can signalize
1337 * that network is working. So we signalize it
1338 * here.
1339 */
Mugunthan V N3963de32016-11-18 10:49:13 +05301340 dev->network_started = 1;
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001341 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001342 goto done_set_intf;
1343 }
1344
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001345#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001346 switch (wIndex) {
1347 case 0: /* control/master intf */
1348 if (wValue != 0)
1349 break;
1350 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001351 usb_ep_disable(dev->status_ep);
1352 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001353 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001354
Remy Bohmerdf063442009-07-29 18:18:43 +02001355 value = 0;
1356 break;
1357 case 1: /* data intf */
1358 if (wValue > 1)
1359 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001360 usb_ep_disable(dev->in_ep);
1361 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001362
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001363 /*
1364 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001365 * the default interface setting ... also, setting
1366 * the non-default interface resets filters etc.
1367 */
1368 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001369 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001370 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001371 usb_ep_enable(dev->in_ep, dev->in);
1372 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001373 dev->cdc_filter = DEFAULT_FILTER;
1374 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001375 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001376 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001377 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001378 value = 0;
1379 break;
1380 }
1381#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001382 /*
1383 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001384 * all non-PXA hardware talks real CDC ...
1385 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001386 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001387#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001388
1389done_set_intf:
1390 break;
1391 case USB_REQ_GET_INTERFACE:
1392 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1393 || !dev->config
1394 || wIndex > 1)
1395 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001396 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001397 break;
1398
1399 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001400 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001401 *(u8 *)req->buf = 0;
1402 else {
1403 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1404 /* carrier always ok ...*/
1405 *(u8 *)req->buf = 1 ;
1406 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001407 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001408 break;
1409
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001410#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001411 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001412 /*
1413 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001414 * wValue = packet filter bitmap
1415 */
1416 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1417 || !cdc_active(dev)
1418 || wLength != 0
1419 || wIndex > 1)
1420 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001421 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001422 dev->cdc_filter = wValue;
1423 value = 0;
1424 break;
1425
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001426 /*
1427 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001428 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1429 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1430 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1431 * case USB_CDC_GET_ETHERNET_STATISTIC:
1432 */
1433
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001434#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001435
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001436#ifdef CONFIG_USB_ETH_RNDIS
1437 /*
1438 * RNDIS uses the CDC command encapsulation mechanism to implement
1439 * an RPC scheme, with much getting/setting of attributes by OID.
1440 */
1441 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1442 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1443 || !rndis_active(dev)
1444 || wLength > USB_BUFSIZ
1445 || wValue
1446 || rndis_control_intf.bInterfaceNumber
1447 != wIndex)
1448 break;
1449 /* read the request, then process it */
1450 value = wLength;
1451 req->complete = rndis_command_complete;
1452 /* later, rndis_control_ack () sends a notification */
1453 break;
1454
1455 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1456 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1457 == ctrl->bRequestType
1458 && rndis_active(dev)
1459 /* && wLength >= 0x0400 */
1460 && !wValue
1461 && rndis_control_intf.bInterfaceNumber
1462 == wIndex) {
1463 u8 *buf;
1464 u32 n;
1465
1466 /* return the result */
1467 buf = rndis_get_next_response(dev->rndis_config, &n);
1468 if (buf) {
1469 memcpy(req->buf, buf, n);
1470 req->complete = rndis_response_complete;
1471 rndis_free_response(dev->rndis_config, buf);
1472 value = n;
1473 }
1474 /* else stalls ... spec says to avoid that */
1475 }
1476 break;
1477#endif /* RNDIS */
1478
Remy Bohmerdf063442009-07-29 18:18:43 +02001479 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001480 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001481 ctrl->bRequestType, ctrl->bRequest,
1482 wValue, wIndex, wLength);
1483 }
1484
1485 /* respond with data transfer before status phase? */
1486 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001487 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001488 req->length = value;
1489 req->zero = value < wLength
1490 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001491 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001492 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001493 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001494 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001495 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001496 }
1497 }
1498
1499 /* host either stalls (value < 0) or reports success */
1500 return value;
1501}
1502
Remy Bohmerdf063442009-07-29 18:18:43 +02001503/*-------------------------------------------------------------------------*/
1504
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001505static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001506
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001507static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001508 gfp_t gfp_flags)
1509{
1510 int retval = -ENOMEM;
1511 size_t size;
1512
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001513 /*
1514 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001515 * Normally we use the USB "terminate on short read" convention;
1516 * so allow up to (N*maxpacket), since that memory is normally
1517 * already allocated. Some hardware doesn't deal well with short
1518 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1519 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001520 *
1521 * RNDIS uses internal framing, and explicitly allows senders to
1522 * pad to end-of-packet. That's potentially nice for speed,
1523 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001524 */
1525
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001526 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001527 if (!req)
1528 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001529
1530 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1531 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001532 if (rndis_active(dev))
1533 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001534 size -= size % dev->out_ep->maxpacket;
1535
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001536 /*
1537 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001538 * but on at least one, checksumming fails otherwise. Note:
1539 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001540 */
1541
Joe Hershberger9f09a362015-04-08 01:41:06 -05001542 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmerdf063442009-07-29 18:18:43 +02001543 req->length = size;
1544 req->complete = rx_complete;
1545
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001546 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001547
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001548 if (retval)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001549 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001550
Remy Bohmerdf063442009-07-29 18:18:43 +02001551 return retval;
1552}
1553
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001554static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001555{
1556 struct eth_dev *dev = ep->driver_data;
1557
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001558 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001559 switch (req->status) {
1560 /* normal completion */
1561 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001562 if (rndis_active(dev)) {
1563 /* we know MaxPacketsPerTransfer == 1 here */
1564 int length = rndis_rm_hdr(req->buf, req->actual);
1565 if (length < 0)
1566 goto length_err;
1567 req->length -= length;
1568 req->actual -= length;
1569 }
Bin Meng2ccb8b02018-07-31 02:55:22 -07001570 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001571length_err:
1572 dev->stats.rx_errors++;
1573 dev->stats.rx_length_errors++;
1574 debug("rx length %d\n", req->length);
1575 break;
1576 }
1577
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001578 dev->stats.rx_packets++;
1579 dev->stats.rx_bytes += req->length;
1580 break;
1581
1582 /* software-driven interface shutdown */
1583 case -ECONNRESET: /* unlink */
1584 case -ESHUTDOWN: /* disconnect etc */
1585 /* for hardware automagic (such as pxa) */
1586 case -ECONNABORTED: /* endpoint reset */
1587 break;
1588
1589 /* data overrun */
1590 case -EOVERFLOW:
1591 dev->stats.rx_over_errors++;
1592 /* FALLTHROUGH */
1593 default:
1594 dev->stats.rx_errors++;
1595 break;
1596 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001597
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001598 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001599}
1600
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001601static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001602{
1603
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001604 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001605
1606 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001607 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001608
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001609 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001610
1611 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001612 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001613
1614 return 0;
1615
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001616fail2:
1617 usb_ep_free_request(dev->in_ep, dev->tx_req);
1618fail1:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001619 pr_err("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001620 return -1;
1621}
1622
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001623static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001624{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001625 struct eth_dev *dev = ep->driver_data;
1626
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001627 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001628 switch (req->status) {
1629 default:
1630 dev->stats.tx_errors++;
1631 debug("tx err %d\n", req->status);
1632 /* FALLTHROUGH */
1633 case -ECONNRESET: /* unlink */
1634 case -ESHUTDOWN: /* disconnect etc */
1635 break;
1636 case 0:
1637 dev->stats.tx_bytes += req->length;
1638 }
1639 dev->stats.tx_packets++;
1640
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001641 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001642}
1643
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001644static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001645{
1646 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001647 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001648 return 1;
1649 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1650}
1651
1652#if 0
1653static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1654{
1655 struct eth_dev *dev = netdev_priv(net);
1656 int length = skb->len;
1657 int retval;
1658 struct usb_request *req = NULL;
1659 unsigned long flags;
1660
1661 /* apply outgoing CDC or RNDIS filters */
1662 if (!eth_is_promisc (dev)) {
1663 u8 *dest = skb->data;
1664
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001665 if (is_multicast_ethaddr(dest)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001666 u16 type;
1667
1668 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1669 * SET_ETHERNET_MULTICAST_FILTERS requests
1670 */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001671 if (is_broadcast_ethaddr(dest))
Remy Bohmerdf063442009-07-29 18:18:43 +02001672 type = USB_CDC_PACKET_TYPE_BROADCAST;
1673 else
1674 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1675 if (!(dev->cdc_filter & type)) {
1676 dev_kfree_skb_any (skb);
1677 return 0;
1678 }
1679 }
1680 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1681 }
1682
1683 spin_lock_irqsave(&dev->req_lock, flags);
1684 /*
1685 * this freelist can be empty if an interrupt triggered disconnect()
1686 * and reconfigured the gadget (shutting down this queue) after the
1687 * network stack decided to xmit but before we got the spinlock.
1688 */
1689 if (list_empty(&dev->tx_reqs)) {
1690 spin_unlock_irqrestore(&dev->req_lock, flags);
1691 return 1;
1692 }
1693
1694 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1695 list_del (&req->list);
1696
1697 /* temporarily stop TX queue when the freelist empties */
1698 if (list_empty (&dev->tx_reqs))
1699 netif_stop_queue (net);
1700 spin_unlock_irqrestore(&dev->req_lock, flags);
1701
1702 /* no buffer copies needed, unless the network stack did it
1703 * or the hardware can't use skb buffers.
1704 * or there's not enough space for any RNDIS headers we need
1705 */
1706 if (rndis_active(dev)) {
1707 struct sk_buff *skb_rndis;
1708
1709 skb_rndis = skb_realloc_headroom (skb,
1710 sizeof (struct rndis_packet_msg_type));
1711 if (!skb_rndis)
1712 goto drop;
1713
1714 dev_kfree_skb_any (skb);
1715 skb = skb_rndis;
1716 rndis_add_hdr (skb);
1717 length = skb->len;
1718 }
1719 req->buf = skb->data;
1720 req->context = skb;
1721 req->complete = tx_complete;
1722
1723 /* use zlp framing on tx for strict CDC-Ether conformance,
1724 * though any robust network rx path ignores extra padding.
1725 * and some hardware doesn't like to write zlps.
1726 */
1727 req->zero = 1;
1728 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1729 length++;
1730
1731 req->length = length;
1732
1733 /* throttle highspeed IRQ rate back slightly */
1734 if (gadget_is_dualspeed(dev->gadget))
1735 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1736 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1737 : 0;
1738
1739 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1740 switch (retval) {
1741 default:
1742 DEBUG (dev, "tx queue err %d\n", retval);
1743 break;
1744 case 0:
1745 net->trans_start = jiffies;
1746 atomic_inc (&dev->tx_qlen);
1747 }
1748
1749 if (retval) {
1750drop:
1751 dev->stats.tx_dropped++;
1752 dev_kfree_skb_any (skb);
1753 spin_lock_irqsave(&dev->req_lock, flags);
1754 if (list_empty (&dev->tx_reqs))
1755 netif_start_queue (net);
1756 list_add (&req->list, &dev->tx_reqs);
1757 spin_unlock_irqrestore(&dev->req_lock, flags);
1758 }
1759 return 0;
1760}
1761
1762/*-------------------------------------------------------------------------*/
1763#endif
1764
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001765static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001766{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001767 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001768
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001769 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001770 rndis_deregister(dev->rndis_config);
1771 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001772
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001773 /* we've already been disconnected ... no i/o is active */
1774 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001775 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001776 dev->req = NULL;
1777 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001778 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001779 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001780 dev->stat_req = NULL;
1781 }
1782
1783 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001784 usb_ep_free_request(dev->in_ep, dev->tx_req);
1785 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001786 }
1787
1788 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001789 usb_ep_free_request(dev->out_ep, dev->rx_req);
1790 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001791 }
1792
1793/* unregister_netdev (dev->net);*/
1794/* free_netdev(dev->net);*/
1795
Lei Wen4af32732010-12-01 23:43:43 +08001796 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001797 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001798}
1799
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001800static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001801{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001802 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001803 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001804}
1805
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001806static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001807{
1808 /* Not used */
1809}
1810
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001811static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001812{
1813 /* Not used */
1814}
1815
1816/*-------------------------------------------------------------------------*/
1817
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001818#ifdef CONFIG_USB_ETH_RNDIS
1819
1820/*
1821 * The interrupt endpoint is used in RNDIS to notify the host when messages
1822 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1823 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1824 * REMOTE_NDIS_KEEPALIVE_MSG.
1825 *
1826 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1827 * normally just one notification will be queued.
1828 */
1829
1830static void rndis_control_ack_complete(struct usb_ep *ep,
1831 struct usb_request *req)
1832{
1833 struct eth_dev *dev = ep->driver_data;
1834
1835 debug("%s...\n", __func__);
1836 if (req->status || req->actual != req->length)
1837 debug("rndis control ack complete --> %d, %d/%d\n",
1838 req->status, req->actual, req->length);
1839
Mugunthan V N3963de32016-11-18 10:49:13 +05301840 if (!dev->network_started) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001841 if (rndis_get_state(dev->rndis_config)
1842 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301843 dev->network_started = 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001844 printf("USB RNDIS network up!\n");
1845 }
1846 }
1847
1848 req->context = NULL;
1849
1850 if (req != dev->stat_req)
1851 usb_ep_free_request(ep, req);
1852}
1853
1854static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1855
Mugunthan V N095b7612016-11-18 11:09:15 +05301856#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001857static int rndis_control_ack(struct eth_device *net)
Mugunthan V N095b7612016-11-18 11:09:15 +05301858#else
1859static int rndis_control_ack(struct udevice *net)
1860#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001861{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05301862 struct ether_priv *priv = (struct ether_priv *)net->priv;
1863 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001864 int length;
1865 struct usb_request *resp = dev->stat_req;
1866
1867 /* in case RNDIS calls this after disconnect */
1868 if (!dev->status) {
1869 debug("status ENODEV\n");
1870 return -ENODEV;
1871 }
1872
1873 /* in case queue length > 1 */
1874 if (resp->context) {
1875 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1876 if (!resp)
1877 return -ENOMEM;
1878 resp->buf = rndis_resp_buf;
1879 }
1880
1881 /*
1882 * Send RNDIS RESPONSE_AVAILABLE notification;
1883 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1884 */
1885 resp->length = 8;
1886 resp->complete = rndis_control_ack_complete;
1887 resp->context = dev;
1888
1889 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1890 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1891
1892 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1893 if (length < 0) {
1894 resp->status = 0;
1895 rndis_control_ack_complete(dev->status_ep, resp);
1896 }
1897
1898 return 0;
1899}
1900
1901#else
1902
1903#define rndis_control_ack NULL
1904
1905#endif /* RNDIS */
1906
1907static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1908{
1909 if (rndis_active(dev)) {
1910 rndis_set_param_medium(dev->rndis_config,
1911 NDIS_MEDIUM_802_3,
1912 BITRATE(dev->gadget)/100);
1913 rndis_signal_connect(dev->rndis_config);
1914 }
1915}
1916
1917static int eth_stop(struct eth_dev *dev)
1918{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001919#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1920 unsigned long ts;
1921 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1922#endif
1923
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001924 if (rndis_active(dev)) {
1925 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1926 rndis_signal_disconnect(dev->rndis_config);
1927
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001928#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1929 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1930 ts = get_timer(0);
1931 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05301932 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001933#endif
1934
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001935 rndis_uninit(dev->rndis_config);
1936 dev->rndis = 0;
1937 }
1938
1939 return 0;
1940}
1941
1942/*-------------------------------------------------------------------------*/
1943
Remy Bohmerdf063442009-07-29 18:18:43 +02001944static int is_eth_addr_valid(char *str)
1945{
1946 if (strlen(str) == 17) {
1947 int i;
1948 char *p, *q;
1949 uchar ea[6];
1950
1951 /* see if it looks like an ethernet address */
1952
1953 p = str;
1954
1955 for (i = 0; i < 6; i++) {
1956 char term = (i == 5 ? '\0' : ':');
1957
1958 ea[i] = simple_strtol(p, &q, 16);
1959
1960 if ((q - p) != 2 || *q++ != term)
1961 break;
1962
1963 p = q;
1964 }
1965
Tom Riniadd21ca2012-10-31 13:30:41 +00001966 /* Now check the contents. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001967 return is_valid_ethaddr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001968 }
1969 return 0;
1970}
1971
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001972static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001973{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001974 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001975 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001976 c = toupper(c);
1977 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001978 return 10 + c - 'A';
1979 return 0;
1980}
1981
1982static int get_ether_addr(const char *str, u8 *dev_addr)
1983{
1984 if (str) {
1985 unsigned i;
1986
1987 for (i = 0; i < 6; i++) {
1988 unsigned char num;
1989
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001990 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001991 str++;
1992 num = nibble(*str++) << 4;
1993 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001994 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02001995 }
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001996 if (is_valid_ethaddr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02001997 return 0;
1998 }
1999 return 1;
2000}
2001
2002static int eth_bind(struct usb_gadget *gadget)
2003{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302004 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002005 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002006 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002007 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002008 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002009 u8 tmp[7];
Mugunthan V N095b7612016-11-18 11:09:15 +05302010#ifdef CONFIG_DM_ETH
2011 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2012#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002013
2014 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002015#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002016 cdc = 0;
2017#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002018#ifndef CONFIG_USB_ETH_RNDIS
2019 rndis = 0;
2020#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002021 /*
2022 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002023 * standard protocol is _strongly_ preferred for interop purposes.
2024 * (By everyone except Microsoft.)
2025 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002026 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002027 /* pxa doesn't support altsettings */
2028 cdc = 0;
2029 } else if (gadget_is_musbhdrc(gadget)) {
2030 /* reduce tx dma overhead by avoiding special cases */
2031 zlp = 0;
2032 } else if (gadget_is_sh(gadget)) {
2033 /* sh doesn't support multiple interfaces or configs */
2034 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002035 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002036 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002037 /* hardware can't write zlps */
2038 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002039 /*
2040 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002041 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2042 */
2043 cdc = 0;
2044 }
2045
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002046 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002047 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002048 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002049 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002050 /*
2051 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002052 * anything less functional on CDC-capable hardware,
2053 * so we fail in this case.
2054 */
Masahiro Yamada81e10422017-09-16 14:10:41 +09002055 pr_err("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002056 gadget->name);
2057 return -ENODEV;
2058 }
2059
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002060 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002061 * If there's an RNDIS configuration, that's what Windows wants to
2062 * be using ... so use these product IDs here and in the "linux.inf"
2063 * needed to install MSFT drivers. Current Linux kernels will use
2064 * the second configuration if it's CDC Ethernet, and need some help
2065 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002066 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002067 if (rndis) {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002068#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Remy Bohmerdf063442009-07-29 18:18:43 +02002069 device_desc.idVendor =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002070 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Remy Bohmerdf063442009-07-29 18:18:43 +02002071 device_desc.idProduct =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002072 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002073#else
2074 device_desc.idVendor =
2075 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2076 device_desc.idProduct =
2077 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2078#endif
2079 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002080
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002081 /*
2082 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2083 * drivers aren't widely available. (That may be improved by
2084 * supporting one submode of the "SAFE" variant of MDLM.)
2085 */
2086 } else {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002087#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2088 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2089 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002090#else
2091 if (!cdc) {
2092 device_desc.idVendor =
2093 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2094 device_desc.idProduct =
2095 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2096 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002097#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002098 }
2099 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002100 if (bcdDevice)
2101 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2102 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002103 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002104 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002105 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002106 if (iSerialNumber) {
2107 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002108 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002109 }
2110
2111 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002112 usb_ep_autoconfig_reset(gadget);
2113 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002114 if (!in_ep) {
2115autoconf_fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002116 pr_err("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002117 gadget->name);
2118 return -ENODEV;
2119 }
2120 in_ep->driver_data = in_ep; /* claim */
2121
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002122 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002123 if (!out_ep)
2124 goto autoconf_fail;
2125 out_ep->driver_data = out_ep; /* claim */
2126
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002127#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002128 /*
2129 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002130 * Since some hosts expect one, try to allocate one anyway.
2131 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002132 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002133 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002134 if (status_ep) {
2135 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002136 } else if (rndis) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002137 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002138 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002139#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002140 } else if (cdc) {
2141 control_intf.bNumEndpoints = 0;
2142 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002143#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002144 }
2145 }
2146#endif
2147
2148 /* one config: cdc, else minimal subset */
2149 if (!cdc) {
2150 eth_config.bNumInterfaces = 1;
2151 eth_config.iConfiguration = STRING_SUBSET;
2152
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002153 /*
2154 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002155 * with multiple controllers and must override CDC Ethernet.
2156 */
2157 fs_subset_descriptors();
2158 hs_subset_descriptors();
2159 }
2160
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002161 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002162
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002163 /* For now RNDIS is always a second config */
2164 if (rndis)
2165 device_desc.bNumConfigurations = 2;
2166
Remy Bohmerdf063442009-07-29 18:18:43 +02002167 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002168 if (rndis)
2169 dev_qualifier.bNumConfigurations = 2;
2170 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002171 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2172
2173 /* assumes ep0 uses the same value for both speeds ... */
2174 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2175
2176 /* and that all endpoints are dual-speed */
2177 hs_source_desc.bEndpointAddress =
2178 fs_source_desc.bEndpointAddress;
2179 hs_sink_desc.bEndpointAddress =
2180 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002181#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002182 if (status_ep)
2183 hs_status_desc.bEndpointAddress =
2184 fs_status_desc.bEndpointAddress;
2185#endif
2186 }
2187
2188 if (gadget_is_otg(gadget)) {
2189 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2190 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2191 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002192#ifdef CONFIG_USB_ETH_RNDIS
2193 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2194 rndis_config.bMaxPower = 4;
2195#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002196 }
2197
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002198
2199 /* network device setup */
Mugunthan V N095b7612016-11-18 11:09:15 +05302200#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +05302201 dev->net = &l_priv->netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +05302202#else
2203 dev->net = l_priv->netdev;
2204#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002205
2206 dev->cdc = cdc;
2207 dev->zlp = zlp;
2208
2209 dev->in_ep = in_ep;
2210 dev->out_ep = out_ep;
2211 dev->status_ep = status_ep;
2212
Mugunthan V N095b7612016-11-18 11:09:15 +05302213 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002214 /*
2215 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002216 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002217 * ends up in a persistent config database. It's not clear if
2218 * host side code for the SAFE thing cares -- its original BLAN
2219 * thing didn't, Sharp never assigned those addresses on Zaurii.
2220 */
Mugunthan V N095b7612016-11-18 11:09:15 +05302221#ifndef CONFIG_DM_ETH
Remy Bohmerdf063442009-07-29 18:18:43 +02002222 get_ether_addr(dev_addr, dev->net->enetaddr);
Remy Bohmerdf063442009-07-29 18:18:43 +02002223 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
Mugunthan V N095b7612016-11-18 11:09:15 +05302224#else
2225 get_ether_addr(dev_addr, pdata->enetaddr);
2226 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2227#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002228
2229 get_ether_addr(host_addr, dev->host_mac);
2230
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002231 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2232 dev->host_mac[0], dev->host_mac[1],
2233 dev->host_mac[2], dev->host_mac[3],
2234 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002235
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002236 if (rndis) {
2237 status = rndis_init();
2238 if (status < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002239 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002240 goto fail;
2241 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002242 }
2243
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002244 /*
2245 * use PKTSIZE (or aligned... from u-boot) and set
2246 * wMaxSegmentSize accordingly
2247 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002248 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2249
2250 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002251 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002252 if (!dev->req)
2253 goto fail;
2254 dev->req->buf = control_req;
2255 dev->req->complete = eth_setup_complete;
2256
2257 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002258#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002259 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002260 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2261 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002262 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002263 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002264
2265 goto fail;
2266 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002267 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002268 dev->stat_req->context = NULL;
2269 }
2270#endif
2271
2272 /* finish hookup to lower layer ... */
2273 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002274 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002275 gadget->ep0->driver_data = dev;
2276
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002277 /*
2278 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002279 * - iff DATA transfer is active, carrier is "on"
2280 * - tx queueing enabled if open *and* carrier is "on"
2281 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002282
2283 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2284 out_ep->name, in_ep->name,
2285 status_ep ? " STATUS " : "",
2286 status_ep ? status_ep->name : ""
2287 );
Mugunthan V N095b7612016-11-18 11:09:15 +05302288#ifndef CONFIG_DM_ETH
2289 printf("MAC %pM\n", dev->net->enetaddr);
2290#else
2291 printf("MAC %pM\n", pdata->enetaddr);
2292#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002293
2294 if (cdc || rndis)
2295 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2296 dev->host_mac[0], dev->host_mac[1],
2297 dev->host_mac[2], dev->host_mac[3],
2298 dev->host_mac[4], dev->host_mac[5]);
2299
2300 if (rndis) {
2301 u32 vendorID = 0;
2302
2303 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2304
2305 dev->rndis_config = rndis_register(rndis_control_ack);
2306 if (dev->rndis_config < 0) {
2307fail0:
2308 eth_unbind(gadget);
2309 debug("RNDIS setup failed\n");
2310 status = -ENODEV;
2311 goto fail;
2312 }
2313
2314 /* these set up a lot of the OIDs that RNDIS needs */
2315 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2316 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2317 &dev->stats, &dev->cdc_filter))
2318 goto fail0;
2319 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2320 manufacturer))
2321 goto fail0;
2322 if (rndis_set_param_medium(dev->rndis_config,
2323 NDIS_MEDIUM_802_3, 0))
2324 goto fail0;
2325 printf("RNDIS ready\n");
2326 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002327 return 0;
2328
2329fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002330 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002331 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002332 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002333}
2334
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002335/*-------------------------------------------------------------------------*/
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002336static void _usb_eth_halt(struct ether_priv *priv);
2337
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302338static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002339{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302340 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002341 struct usb_gadget *gadget;
2342 unsigned long ts;
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002343 int ret;
Remy Bohmerdf063442009-07-29 18:18:43 +02002344 unsigned long timeout = USB_CONNECT_TIMEOUT;
2345
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002346 ret = usb_gadget_initialize(0);
2347 if (ret)
2348 return ret;
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302349
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002350 /* Configure default mac-addresses for the USB ethernet device */
2351#ifdef CONFIG_USBNET_DEV_ADDR
2352 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2353#endif
2354#ifdef CONFIG_USBNET_HOST_ADDR
2355 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2356#endif
2357 /* Check if the user overruled the MAC addresses */
Simon Glass64b723f2017-08-03 12:22:12 -06002358 if (env_get("usbnet_devaddr"))
2359 strlcpy(dev_addr, env_get("usbnet_devaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002360 sizeof(dev_addr));
2361
Simon Glass64b723f2017-08-03 12:22:12 -06002362 if (env_get("usbnet_hostaddr"))
2363 strlcpy(host_addr, env_get("usbnet_hostaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002364 sizeof(host_addr));
2365
2366 if (!is_eth_addr_valid(dev_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002367 pr_err("Need valid 'usbnet_devaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002368 goto fail;
2369 }
2370 if (!is_eth_addr_valid(host_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002371 pr_err("Need valid 'usbnet_hostaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002372 goto fail;
2373 }
2374
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302375 priv->eth_driver.speed = DEVSPEED;
2376 priv->eth_driver.bind = eth_bind;
2377 priv->eth_driver.unbind = eth_unbind;
2378 priv->eth_driver.setup = eth_setup;
2379 priv->eth_driver.reset = eth_disconnect;
2380 priv->eth_driver.disconnect = eth_disconnect;
2381 priv->eth_driver.suspend = eth_suspend;
2382 priv->eth_driver.resume = eth_resume;
2383 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen4af32732010-12-01 23:43:43 +08002384 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002385
2386 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002387
2388 packet_received = 0;
2389 packet_sent = 0;
2390
2391 gadget = dev->gadget;
2392 usb_gadget_connect(gadget);
2393
Simon Glass64b723f2017-08-03 12:22:12 -06002394 if (env_get("cdc_connect_timeout"))
2395 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
Remy Bohmerdf063442009-07-29 18:18:43 +02002396 NULL, 10) * CONFIG_SYS_HZ;
2397 ts = get_timer(0);
Mugunthan V N3963de32016-11-18 10:49:13 +05302398 while (!dev->network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002399 /* Handle control-c and timeouts */
2400 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002401 pr_err("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002402 goto fail;
2403 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302404 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002405 }
2406
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002407 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002408 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002409 return 0;
2410fail:
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002411 _usb_eth_halt(priv);
Remy Bohmerdf063442009-07-29 18:18:43 +02002412 return -1;
2413}
2414
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302415static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002416{
2417 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002418 void *rndis_pkt = NULL;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302419 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002420 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002421 unsigned long ts;
2422 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002423
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002424 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002425
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002426 /* new buffer is needed to include RNDIS header */
2427 if (rndis_active(dev)) {
2428 rndis_pkt = malloc(length +
2429 sizeof(struct rndis_packet_msg_type));
2430 if (!rndis_pkt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002431 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002432 goto drop;
2433 }
2434 rndis_add_hdr(rndis_pkt, length);
2435 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002436 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002437 packet = rndis_pkt;
2438 length += sizeof(struct rndis_packet_msg_type);
2439 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002440 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002441 req->context = NULL;
2442 req->complete = tx_complete;
2443
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002444 /*
2445 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002446 * though any robust network rx path ignores extra padding.
2447 * and some hardware doesn't like to write zlps.
2448 */
2449 req->zero = 1;
2450 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2451 length++;
2452
2453 req->length = length;
2454#if 0
2455 /* throttle highspeed IRQ rate back slightly */
2456 if (gadget_is_dualspeed(dev->gadget))
2457 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2458 ? ((dev->tx_qlen % qmult) != 0) : 0;
2459#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002460 dev->tx_qlen = 1;
2461 ts = get_timer(0);
2462 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002463
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002464 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002465
2466 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002467 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002468 while (!packet_sent) {
2469 if (get_timer(ts) > timeout) {
2470 printf("timeout sending packets to usb ethernet\n");
2471 return -1;
2472 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302473 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002474 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002475 if (rndis_pkt)
2476 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002477
2478 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002479drop:
2480 dev->stats.tx_dropped++;
2481 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002482}
2483
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302484static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002485{
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302486 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002487
Remy Bohmerdf063442009-07-29 18:18:43 +02002488 return 0;
2489}
2490
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002491static void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002492{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302493 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002494
Lei Wen4af32732010-12-01 23:43:43 +08002495 /* If the gadget not registered, simple return */
2496 if (!dev->gadget)
2497 return;
2498
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002499 /*
2500 * Some USB controllers may need additional deinitialization here
2501 * before dropping pull-up (also due to hardware issues).
2502 * For example: unhandled interrupt with status stage started may
2503 * bring the controller to fully broken state (until board reset).
2504 * There are some variants to debug and fix such cases:
2505 * 1) In the case of RNDIS connection eth_stop can perform additional
2506 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2507 * 2) 'pullup' callback in your UDC driver can be improved to perform
2508 * this deinitialization.
2509 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002510 eth_stop(dev);
2511
Remy Bohmerdf063442009-07-29 18:18:43 +02002512 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002513
2514 /* Clear pending interrupt */
2515 if (dev->network_started) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302516 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002517 dev->network_started = 0;
2518 }
2519
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302520 usb_gadget_unregister_driver(&priv->eth_driver);
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002521 usb_gadget_release(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002522}
2523
Mugunthan V N095b7612016-11-18 11:09:15 +05302524#ifndef CONFIG_DM_ETH
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302525static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2526{
2527 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2528
2529 return _usb_eth_init(priv);
2530}
2531
2532static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2533{
2534 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2535
2536 return _usb_eth_send(priv, packet, length);
2537}
2538
2539static int usb_eth_recv(struct eth_device *netdev)
2540{
2541 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2542 struct eth_dev *dev = &priv->ethdev;
2543 int ret;
2544
2545 ret = _usb_eth_recv(priv);
2546 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002547 pr_err("error packet receive\n");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302548 return ret;
2549 }
2550
2551 if (!packet_received)
2552 return 0;
2553
2554 if (dev->rx_req) {
2555 net_process_received_packet(net_rx_packets[0],
2556 dev->rx_req->length);
2557 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002558 pr_err("dev->rx_req invalid");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302559 }
2560 packet_received = 0;
2561 rx_submit(dev, dev->rx_req, 0);
2562
2563 return 0;
2564}
2565
2566void usb_eth_halt(struct eth_device *netdev)
2567{
2568 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2569
2570 _usb_eth_halt(priv);
2571}
2572
Remy Bohmerdf063442009-07-29 18:18:43 +02002573int usb_eth_initialize(bd_t *bi)
2574{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302575 struct eth_device *netdev = &l_priv->netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002576
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002577 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002578
2579 netdev->init = usb_eth_init;
2580 netdev->send = usb_eth_send;
2581 netdev->recv = usb_eth_recv;
2582 netdev->halt = usb_eth_halt;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302583 netdev->priv = l_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +02002584
Remy Bohmerdf063442009-07-29 18:18:43 +02002585 eth_register(netdev);
2586 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002587}
Mugunthan V N095b7612016-11-18 11:09:15 +05302588#else
2589static int usb_eth_start(struct udevice *dev)
2590{
2591 struct ether_priv *priv = dev_get_priv(dev);
2592
2593 return _usb_eth_init(priv);
2594}
2595
2596static int usb_eth_send(struct udevice *dev, void *packet, int length)
2597{
2598 struct ether_priv *priv = dev_get_priv(dev);
2599
2600 return _usb_eth_send(priv, packet, length);
2601}
2602
2603static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2604{
2605 struct ether_priv *priv = dev_get_priv(dev);
2606 struct eth_dev *ethdev = &priv->ethdev;
2607 int ret;
2608
2609 ret = _usb_eth_recv(priv);
2610 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002611 pr_err("error packet receive\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302612 return ret;
2613 }
2614
2615 if (packet_received) {
2616 if (ethdev->rx_req) {
2617 *packetp = (uchar *)net_rx_packets[0];
2618 return ethdev->rx_req->length;
2619 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002620 pr_err("dev->rx_req invalid");
Mugunthan V N095b7612016-11-18 11:09:15 +05302621 return -EFAULT;
2622 }
2623 }
2624
2625 return -EAGAIN;
2626}
2627
2628static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2629 int length)
2630{
2631 struct ether_priv *priv = dev_get_priv(dev);
2632 struct eth_dev *ethdev = &priv->ethdev;
2633
2634 packet_received = 0;
2635
2636 return rx_submit(ethdev, ethdev->rx_req, 0);
2637}
2638
2639static void usb_eth_stop(struct udevice *dev)
2640{
2641 struct ether_priv *priv = dev_get_priv(dev);
2642
2643 _usb_eth_halt(priv);
2644}
2645
2646static int usb_eth_probe(struct udevice *dev)
2647{
2648 struct ether_priv *priv = dev_get_priv(dev);
2649 struct eth_pdata *pdata = dev_get_platdata(dev);
2650
2651 priv->netdev = dev;
2652 l_priv = priv;
2653
2654 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
Simon Glass8551d552017-08-03 12:22:11 -06002655 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
Mugunthan V N095b7612016-11-18 11:09:15 +05302656
2657 return 0;
2658}
2659
2660static const struct eth_ops usb_eth_ops = {
2661 .start = usb_eth_start,
2662 .send = usb_eth_send,
2663 .recv = usb_eth_recv,
2664 .free_pkt = usb_eth_free_pkt,
2665 .stop = usb_eth_stop,
2666};
2667
2668int usb_ether_init(void)
2669{
2670 struct udevice *dev;
2671 struct udevice *usb_dev;
2672 int ret;
2673
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +01002674 ret = uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
Mugunthan V N095b7612016-11-18 11:09:15 +05302675 if (!usb_dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002676 pr_err("No USB device found\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302677 return ret;
2678 }
2679
2680 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2681 if (!dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002682 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302683 return ret;
2684 }
2685
2686 return 0;
2687}
2688
2689U_BOOT_DRIVER(eth_usb) = {
2690 .name = "usb_ether",
2691 .id = UCLASS_ETH,
2692 .probe = usb_eth_probe,
2693 .ops = &usb_eth_ops,
2694 .priv_auto_alloc_size = sizeof(struct ether_priv),
2695 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2696 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2697};
2698#endif /* CONFIG_DM_ETH */