blob: 386505d42de5f0e9eac4980191eb6f15496e61d0 [file] [log] [blame]
Remy Bohmerdf063442009-07-29 18:18:43 +02001/*
2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-07-29 18:18:43 +02009 */
10
11#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Alex Kiernan9c215492018-04-01 09:22:38 +000013#include <environment.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090014#include <linux/errno.h>
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +030015#include <linux/netdevice.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020016#include <linux/usb/ch9.h>
17#include <linux/usb/cdc.h>
18#include <linux/usb/gadget.h>
19#include <net.h>
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +053020#include <usb.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030021#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060022#include <memalign.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020023#include <linux/ctype.h>
24
25#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030026#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020027
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053028#include <dm.h>
Mugunthan V N095b7612016-11-18 11:09:15 +053029#include <dm/lists.h>
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053030#include <dm/uclass-internal.h>
31#include <dm/device-internal.h>
32
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030033#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040034
Remy Bohmerdf063442009-07-29 18:18:43 +020035#define atomic_read
36extern struct platform_data brd;
Remy Bohmerdf063442009-07-29 18:18:43 +020037
38
39unsigned packet_received, packet_sent;
40
Remy Bohmerdf063442009-07-29 18:18:43 +020041/*
42 * Ethernet gadget driver -- with CDC and non-CDC options
43 * Builds on hardware support for a full duplex link.
44 *
45 * CDC Ethernet is the standard USB solution for sending Ethernet frames
46 * using USB. Real hardware tends to use the same framing protocol but look
47 * different for control features. This driver strongly prefers to use
48 * this USB-IF standard as its open-systems interoperability solution;
49 * most host side USB stacks (except from Microsoft) support it.
50 *
51 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
52 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
53 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
54 *
55 * There's some hardware that can't talk CDC ECM. We make that hardware
56 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
57 * link-level setup only requires activating the configuration. Only the
58 * endpoint descriptors, and product/vendor IDs, are relevant; no control
59 * operations are available. Linux supports it, but other host operating
60 * systems may not. (This is a subset of CDC Ethernet.)
61 *
62 * It turns out that if you add a few descriptors to that "CDC Subset",
63 * (Windows) host side drivers from MCCI can treat it as one submode of
64 * a proprietary scheme called "SAFE" ... without needing to know about
65 * specific product/vendor IDs. So we do that, making it easier to use
66 * those MS-Windows drivers. Those added descriptors make it resemble a
67 * CDC MDLM device, but they don't change device behavior at all. (See
68 * MCCI Engineering report 950198 "SAFE Networking Functions".)
69 *
70 * A third option is also in use. Rather than CDC Ethernet, or something
71 * simpler, Microsoft pushes their own approach: RNDIS. The published
72 * RNDIS specs are ambiguous and appear to be incomplete, and are also
73 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
74 */
75#define ETH_ALEN 6 /* Octets in one ethernet addr */
76#define ETH_HLEN 14 /* Total octets in header. */
77#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
78#define ETH_DATA_LEN 1500 /* Max. octets in payload */
79#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
Remy Bohmerdf063442009-07-29 18:18:43 +020080
81#define DRIVER_DESC "Ethernet Gadget"
82/* Based on linux 2.6.27 version */
83#define DRIVER_VERSION "May Day 2005"
84
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040085static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020086
87#define RX_EXTRA 20 /* guard against rx overflows */
88
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030089#ifndef CONFIG_USB_ETH_RNDIS
90#define rndis_uninit(x) do {} while (0)
91#define rndis_deregister(c) do {} while (0)
92#define rndis_exit() do {} while (0)
93#endif
94
95/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +020096#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
97 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
98 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
99 |USB_CDC_PACKET_TYPE_DIRECTED)
100
101#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
102
103/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300104
105struct eth_dev {
106 struct usb_gadget *gadget;
107 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300108 struct usb_request *stat_req; /* for cdc & rndis status */
Mugunthan V Nb3d902b2016-11-18 10:49:12 +0530109#ifdef CONFIG_DM_USB
110 struct udevice *usb_udev;
111#endif
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300112
113 u8 config;
114 struct usb_ep *in_ep, *out_ep, *status_ep;
115 const struct usb_endpoint_descriptor
116 *in, *out, *status;
117
118 struct usb_request *tx_req, *rx_req;
119
Mugunthan V N095b7612016-11-18 11:09:15 +0530120#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300121 struct eth_device *net;
Mugunthan V N095b7612016-11-18 11:09:15 +0530122#else
123 struct udevice *net;
124#endif
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300125 struct net_device_stats stats;
126 unsigned int tx_qlen;
127
128 unsigned zlp:1;
129 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300130 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300131 unsigned suspended:1;
132 unsigned network_started:1;
133 u16 cdc_filter;
134 unsigned long todo;
135 int mtu;
136#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300137 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300138 u8 host_mac[ETH_ALEN];
139};
140
141/*
142 * This version autoconfigures as much as possible at run-time.
143 *
144 * It also ASSUMES a self-powered device, without remote wakeup,
145 * although remote wakeup support would make sense.
146 */
147
148/*-------------------------------------------------------------------------*/
Mugunthan V N88e48be2016-11-18 11:06:13 +0530149struct ether_priv {
150 struct eth_dev ethdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530151#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +0530152 struct eth_device netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530153#else
154 struct udevice *netdev;
155#endif
Mugunthan V N88e48be2016-11-18 11:06:13 +0530156 struct usb_gadget_driver eth_driver;
157};
158
159struct ether_priv eth_priv;
160struct ether_priv *l_priv = &eth_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +0200161
162/*-------------------------------------------------------------------------*/
163
164/* "main" config is either CDC, or its simple subset */
165static inline int is_cdc(struct eth_dev *dev)
166{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200167#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200168 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200169#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200170 return 0; /* only subset possible */
171#else
172 return dev->cdc; /* depends on what hardware we found */
173#endif
174}
175
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300176/* "secondary" RNDIS config may sometimes be activated */
177static inline int rndis_active(struct eth_dev *dev)
178{
179#ifdef CONFIG_USB_ETH_RNDIS
180 return dev->rndis;
181#else
182 return 0;
183#endif
184}
185
186#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
187#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200188
189#define DEFAULT_QLEN 2 /* double buffering by default */
190
191/* peak bulk transfer bits-per-second */
192#define HS_BPS (13 * 512 * 8 * 1000 * 8)
193#define FS_BPS (19 * 64 * 1 * 1000 * 8)
194
195#ifdef CONFIG_USB_GADGET_DUALSPEED
196#define DEVSPEED USB_SPEED_HIGH
197
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400198#ifdef CONFIG_USB_ETH_QMULT
199#define qmult CONFIG_USB_ETH_QMULT
200#else
201#define qmult 5
202#endif
203
Remy Bohmerdf063442009-07-29 18:18:43 +0200204/* for dual-speed hardware, use deeper queues at highspeed */
205#define qlen(gadget) \
206 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
207
208static inline int BITRATE(struct usb_gadget *g)
209{
210 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
211}
212
213#else /* full speed (low speed doesn't do bulk) */
214
215#define qmult 1
216
217#define DEVSPEED USB_SPEED_FULL
218
219#define qlen(gadget) DEFAULT_QLEN
220
221static inline int BITRATE(struct usb_gadget *g)
222{
223 return FS_BPS;
224}
225#endif
226
Remy Bohmerdf063442009-07-29 18:18:43 +0200227/*-------------------------------------------------------------------------*/
228
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400229/*
230 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200231 * Instead: allocate your own, using normal USB-IF procedures.
232 */
233
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400234/*
235 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200236 * It's for devices with only CDC Ethernet configurations.
237 */
238#define CDC_VENDOR_NUM 0x0525 /* NetChip */
239#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
240
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400241/*
242 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200243 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
244 * with pxa250. We're protocol-compatible, if the host-side drivers
245 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
246 * drivers that need to hard-wire endpoint numbers have a hook.
247 *
248 * The protocol is a minimal subset of CDC Ether, which works on any bulk
249 * hardware that's not deeply broken ... even on hardware that can't talk
250 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
251 * doesn't handle control-OUT).
252 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300253#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
254#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200255
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400256/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300257 * For hardware that can talk RNDIS and either of the above protocols,
258 * use this ID ... the windows INF files will know it. Unless it's
259 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
260 * the non-RNDIS configuration.
261 */
262#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
263#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
264
265/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400266 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200267 * device descriptor, either numbers or strings or both. These string
268 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
269 */
270
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300271/*
272 * Emulating them in eth_bind:
273 * static ushort idVendor;
274 * static ushort idProduct;
275 */
276
Maxime Ripard48e78f82017-09-07 09:15:08 +0200277#if defined(CONFIG_USB_GADGET_MANUFACTURER)
278static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmerdf063442009-07-29 18:18:43 +0200279#else
Bin Meng75574052016-02-05 19:30:11 -0800280static char *iManufacturer = "U-Boot";
Remy Bohmerdf063442009-07-29 18:18:43 +0200281#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300282
283/* These probably need to be configurable. */
284static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200285static char *iProduct;
286static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300287
Remy Bohmerdf063442009-07-29 18:18:43 +0200288static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300289
Remy Bohmerdf063442009-07-29 18:18:43 +0200290static char host_addr[18];
291
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300292
Remy Bohmerdf063442009-07-29 18:18:43 +0200293/*-------------------------------------------------------------------------*/
294
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400295/*
296 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200297 * ep0 implementation: descriptors, config management, setup().
298 * also optional class-specific notification interrupt transfer.
299 */
300
301/*
302 * DESCRIPTORS ... most are static, but strings and (full) configuration
303 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300304 * our simple subset, with RNDIS as an optional second configuration.
305 *
306 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
307 * the class descriptors match a modem (they're ignored; it's really just
308 * Ethernet functionality), they don't need the NOP altsetting, and the
309 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200310 */
311
312#define STRING_MANUFACTURER 1
313#define STRING_PRODUCT 2
314#define STRING_ETHADDR 3
315#define STRING_DATA 4
316#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300317#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200318#define STRING_CDC 7
319#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300320#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200321#define STRING_SERIALNUMBER 10
322
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300323/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200324#define USB_BUFSIZ 256
325
326/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300327 * This device advertises one configuration, eth_config, unless RNDIS
328 * is enabled (rndis_config) on hardware supporting at least two configs.
329 *
330 * NOTE: Controllers like superh_udc should probably be able to use
331 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200332 *
333 * FIXME define some higher-powered configurations to make it easier
334 * to recharge batteries ...
335 */
336
337#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300338#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200339
340static struct usb_device_descriptor
341device_desc = {
342 .bLength = sizeof device_desc,
343 .bDescriptorType = USB_DT_DEVICE,
344
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400345 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200346
347 .bDeviceClass = USB_CLASS_COMM,
348 .bDeviceSubClass = 0,
349 .bDeviceProtocol = 0,
350
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400351 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
352 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200353 .iManufacturer = STRING_MANUFACTURER,
354 .iProduct = STRING_PRODUCT,
355 .bNumConfigurations = 1,
356};
357
358static struct usb_otg_descriptor
359otg_descriptor = {
360 .bLength = sizeof otg_descriptor,
361 .bDescriptorType = USB_DT_OTG,
362
363 .bmAttributes = USB_OTG_SRP,
364};
365
366static struct usb_config_descriptor
367eth_config = {
368 .bLength = sizeof eth_config,
369 .bDescriptorType = USB_DT_CONFIG,
370
371 /* compute wTotalLength on the fly */
372 .bNumInterfaces = 2,
373 .bConfigurationValue = DEV_CONFIG_VALUE,
374 .iConfiguration = STRING_CDC,
375 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
376 .bMaxPower = 1,
377};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300378
379#ifdef CONFIG_USB_ETH_RNDIS
380static struct usb_config_descriptor
381rndis_config = {
382 .bLength = sizeof rndis_config,
383 .bDescriptorType = USB_DT_CONFIG,
384
385 /* compute wTotalLength on the fly */
386 .bNumInterfaces = 2,
387 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
388 .iConfiguration = STRING_RNDIS,
389 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
390 .bMaxPower = 1,
391};
392#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200393
394/*
395 * Compared to the simple CDC subset, the full CDC Ethernet model adds
396 * three class descriptors, two interface descriptors, optional status
397 * endpoint. Both have a "data" interface and two bulk endpoints.
398 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300399 *
400 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
401 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
402 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
403 * work around those bugs) are unlikely to ever come from MSFT, you may
404 * wish to avoid using RNDIS.
405 *
406 * MCCI offers an alternative to RNDIS if you need to connect to Windows
407 * but have hardware that can't support CDC Ethernet. We add descriptors
408 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
409 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
410 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200411 */
412
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200413#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200414static struct usb_interface_descriptor
415control_intf = {
416 .bLength = sizeof control_intf,
417 .bDescriptorType = USB_DT_INTERFACE,
418
419 .bInterfaceNumber = 0,
420 /* status endpoint is optional; this may be patched later */
421 .bNumEndpoints = 1,
422 .bInterfaceClass = USB_CLASS_COMM,
423 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
424 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
425 .iInterface = STRING_CONTROL,
426};
427#endif
428
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300429#ifdef CONFIG_USB_ETH_RNDIS
430static const struct usb_interface_descriptor
431rndis_control_intf = {
432 .bLength = sizeof rndis_control_intf,
433 .bDescriptorType = USB_DT_INTERFACE,
434
435 .bInterfaceNumber = 0,
436 .bNumEndpoints = 1,
437 .bInterfaceClass = USB_CLASS_COMM,
438 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
439 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
440 .iInterface = STRING_RNDIS_CONTROL,
441};
442#endif
443
Remy Bohmerdf063442009-07-29 18:18:43 +0200444static const struct usb_cdc_header_desc header_desc = {
445 .bLength = sizeof header_desc,
446 .bDescriptorType = USB_DT_CS_INTERFACE,
447 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
448
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400449 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200450};
451
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200452#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200453
454static const struct usb_cdc_union_desc union_desc = {
455 .bLength = sizeof union_desc,
456 .bDescriptorType = USB_DT_CS_INTERFACE,
457 .bDescriptorSubType = USB_CDC_UNION_TYPE,
458
459 .bMasterInterface0 = 0, /* index of control interface */
460 .bSlaveInterface0 = 1, /* index of DATA interface */
461};
462
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300463#endif /* CDC || RNDIS */
464
465#ifdef CONFIG_USB_ETH_RNDIS
466
467static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
468 .bLength = sizeof call_mgmt_descriptor,
469 .bDescriptorType = USB_DT_CS_INTERFACE,
470 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
471
472 .bmCapabilities = 0x00,
473 .bDataInterface = 0x01,
474};
475
476static const struct usb_cdc_acm_descriptor acm_descriptor = {
477 .bLength = sizeof acm_descriptor,
478 .bDescriptorType = USB_DT_CS_INTERFACE,
479 .bDescriptorSubType = USB_CDC_ACM_TYPE,
480
481 .bmCapabilities = 0x00,
482};
483
484#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200485
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200486#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200487
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400488/*
489 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200490 * ways: data endpoints live in the control interface, there's no data
491 * interface, and it's not used to talk to a cell phone radio.
492 */
493
494static const struct usb_cdc_mdlm_desc mdlm_desc = {
495 .bLength = sizeof mdlm_desc,
496 .bDescriptorType = USB_DT_CS_INTERFACE,
497 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
498
499 .bcdVersion = __constant_cpu_to_le16(0x0100),
500 .bGUID = {
501 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
502 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
503 },
504};
505
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400506/*
507 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200508 * can't really use its struct. All we do here is say that we're using
509 * the submode of "SAFE" which directly matches the CDC Subset.
510 */
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530511#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200512static const u8 mdlm_detail_desc[] = {
513 6,
514 USB_DT_CS_INTERFACE,
515 USB_CDC_MDLM_DETAIL_TYPE,
516
517 0, /* "SAFE" */
518 0, /* network control capabilities (none) */
519 0, /* network data capabilities ("raw" encapsulation) */
520};
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530521#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200522
523#endif
524
Remy Bohmerdf063442009-07-29 18:18:43 +0200525static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400526 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200527 .bDescriptorType = USB_DT_CS_INTERFACE,
528 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
529
530 /* this descriptor actually adds value, surprise! */
531 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400532 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
533 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
534 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200535 .bNumberPowerFilters = 0,
536};
537
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200538#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200539
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400540/*
541 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200542 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
543 * packet, to simplify cancellation; and a big transfer interval, to
544 * waste less bandwidth.
545 *
546 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
547 * if they ignore the connect/disconnect notifications that real aether
548 * can provide. more advanced cdc configurations might want to support
549 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300550 *
551 * RNDIS requires the status endpoint, since it uses that encapsulation
552 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200553 */
554
555#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
556#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
557
558static struct usb_endpoint_descriptor
559fs_status_desc = {
560 .bLength = USB_DT_ENDPOINT_SIZE,
561 .bDescriptorType = USB_DT_ENDPOINT,
562
563 .bEndpointAddress = USB_DIR_IN,
564 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400565 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200566 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
567};
568#endif
569
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200570#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200571
572/* the default data interface has no endpoints ... */
573
574static const struct usb_interface_descriptor
575data_nop_intf = {
576 .bLength = sizeof data_nop_intf,
577 .bDescriptorType = USB_DT_INTERFACE,
578
579 .bInterfaceNumber = 1,
580 .bAlternateSetting = 0,
581 .bNumEndpoints = 0,
582 .bInterfaceClass = USB_CLASS_CDC_DATA,
583 .bInterfaceSubClass = 0,
584 .bInterfaceProtocol = 0,
585};
586
587/* ... but the "real" data interface has two bulk endpoints */
588
589static const struct usb_interface_descriptor
590data_intf = {
591 .bLength = sizeof data_intf,
592 .bDescriptorType = USB_DT_INTERFACE,
593
594 .bInterfaceNumber = 1,
595 .bAlternateSetting = 1,
596 .bNumEndpoints = 2,
597 .bInterfaceClass = USB_CLASS_CDC_DATA,
598 .bInterfaceSubClass = 0,
599 .bInterfaceProtocol = 0,
600 .iInterface = STRING_DATA,
601};
602
603#endif
604
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300605#ifdef CONFIG_USB_ETH_RNDIS
606
607/* RNDIS doesn't activate by changing to the "real" altsetting */
608
609static const struct usb_interface_descriptor
610rndis_data_intf = {
611 .bLength = sizeof rndis_data_intf,
612 .bDescriptorType = USB_DT_INTERFACE,
613
614 .bInterfaceNumber = 1,
615 .bAlternateSetting = 0,
616 .bNumEndpoints = 2,
617 .bInterfaceClass = USB_CLASS_CDC_DATA,
618 .bInterfaceSubClass = 0,
619 .bInterfaceProtocol = 0,
620 .iInterface = STRING_DATA,
621};
622
623#endif
624
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200625#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200626
627/*
628 * "Simple" CDC-subset option is a simple vendor-neutral model that most
629 * full speed controllers can handle: one interface, two bulk endpoints.
630 *
631 * To assist host side drivers, we fancy it up a bit, and add descriptors
632 * so some host side drivers will understand it as a "SAFE" variant.
633 */
634
635static const struct usb_interface_descriptor
636subset_data_intf = {
637 .bLength = sizeof subset_data_intf,
638 .bDescriptorType = USB_DT_INTERFACE,
639
640 .bInterfaceNumber = 0,
641 .bAlternateSetting = 0,
642 .bNumEndpoints = 2,
643 .bInterfaceClass = USB_CLASS_COMM,
644 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
645 .bInterfaceProtocol = 0,
646 .iInterface = STRING_DATA,
647};
648
649#endif /* SUBSET */
650
Remy Bohmerdf063442009-07-29 18:18:43 +0200651static struct usb_endpoint_descriptor
652fs_source_desc = {
653 .bLength = USB_DT_ENDPOINT_SIZE,
654 .bDescriptorType = USB_DT_ENDPOINT,
655
656 .bEndpointAddress = USB_DIR_IN,
657 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700658 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200659};
660
661static struct usb_endpoint_descriptor
662fs_sink_desc = {
663 .bLength = USB_DT_ENDPOINT_SIZE,
664 .bDescriptorType = USB_DT_ENDPOINT,
665
666 .bEndpointAddress = USB_DIR_OUT,
667 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700668 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200669};
670
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400671static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200672 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200673#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200674 /* "cdc" mode descriptors */
675 (struct usb_descriptor_header *) &control_intf,
676 (struct usb_descriptor_header *) &header_desc,
677 (struct usb_descriptor_header *) &union_desc,
678 (struct usb_descriptor_header *) &ether_desc,
679 /* NOTE: status endpoint may need to be removed */
680 (struct usb_descriptor_header *) &fs_status_desc,
681 /* data interface, with altsetting */
682 (struct usb_descriptor_header *) &data_nop_intf,
683 (struct usb_descriptor_header *) &data_intf,
684 (struct usb_descriptor_header *) &fs_source_desc,
685 (struct usb_descriptor_header *) &fs_sink_desc,
686 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200687#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200688};
689
690static inline void fs_subset_descriptors(void)
691{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200692#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200693 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
694 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
695 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
696 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
697 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
698 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
699 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
700 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
701 fs_eth_function[8] = NULL;
702#else
703 fs_eth_function[1] = NULL;
704#endif
705}
706
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300707#ifdef CONFIG_USB_ETH_RNDIS
708static const struct usb_descriptor_header *fs_rndis_function[] = {
709 (struct usb_descriptor_header *) &otg_descriptor,
710 /* control interface matches ACM, not Ethernet */
711 (struct usb_descriptor_header *) &rndis_control_intf,
712 (struct usb_descriptor_header *) &header_desc,
713 (struct usb_descriptor_header *) &call_mgmt_descriptor,
714 (struct usb_descriptor_header *) &acm_descriptor,
715 (struct usb_descriptor_header *) &union_desc,
716 (struct usb_descriptor_header *) &fs_status_desc,
717 /* data interface has no altsetting */
718 (struct usb_descriptor_header *) &rndis_data_intf,
719 (struct usb_descriptor_header *) &fs_source_desc,
720 (struct usb_descriptor_header *) &fs_sink_desc,
721 NULL,
722};
723#endif
724
Remy Bohmerdf063442009-07-29 18:18:43 +0200725/*
726 * usb 2.0 devices need to expose both high speed and full speed
727 * descriptors, unless they only run at full speed.
728 */
729
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200730#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200731static struct usb_endpoint_descriptor
732hs_status_desc = {
733 .bLength = USB_DT_ENDPOINT_SIZE,
734 .bDescriptorType = USB_DT_ENDPOINT,
735
736 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400737 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200738 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
739};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200740#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200741
742static struct usb_endpoint_descriptor
743hs_source_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_endpoint_descriptor
752hs_sink_desc = {
753 .bLength = USB_DT_ENDPOINT_SIZE,
754 .bDescriptorType = USB_DT_ENDPOINT,
755
756 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400757 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200758};
759
760static struct usb_qualifier_descriptor
761dev_qualifier = {
762 .bLength = sizeof dev_qualifier,
763 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
764
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400765 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200766 .bDeviceClass = USB_CLASS_COMM,
767
768 .bNumConfigurations = 1,
769};
770
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400771static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200772 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200773#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200774 /* "cdc" mode descriptors */
775 (struct usb_descriptor_header *) &control_intf,
776 (struct usb_descriptor_header *) &header_desc,
777 (struct usb_descriptor_header *) &union_desc,
778 (struct usb_descriptor_header *) &ether_desc,
779 /* NOTE: status endpoint may need to be removed */
780 (struct usb_descriptor_header *) &hs_status_desc,
781 /* data interface, with altsetting */
782 (struct usb_descriptor_header *) &data_nop_intf,
783 (struct usb_descriptor_header *) &data_intf,
784 (struct usb_descriptor_header *) &hs_source_desc,
785 (struct usb_descriptor_header *) &hs_sink_desc,
786 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200787#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200788};
789
790static inline void hs_subset_descriptors(void)
791{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200792#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200793 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
794 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
795 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
796 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
797 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
798 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
799 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
800 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
801 hs_eth_function[8] = NULL;
802#else
803 hs_eth_function[1] = NULL;
804#endif
805}
806
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300807#ifdef CONFIG_USB_ETH_RNDIS
808static const struct usb_descriptor_header *hs_rndis_function[] = {
809 (struct usb_descriptor_header *) &otg_descriptor,
810 /* control interface matches ACM, not Ethernet */
811 (struct usb_descriptor_header *) &rndis_control_intf,
812 (struct usb_descriptor_header *) &header_desc,
813 (struct usb_descriptor_header *) &call_mgmt_descriptor,
814 (struct usb_descriptor_header *) &acm_descriptor,
815 (struct usb_descriptor_header *) &union_desc,
816 (struct usb_descriptor_header *) &hs_status_desc,
817 /* data interface has no altsetting */
818 (struct usb_descriptor_header *) &rndis_data_intf,
819 (struct usb_descriptor_header *) &hs_source_desc,
820 (struct usb_descriptor_header *) &hs_sink_desc,
821 NULL,
822};
823#endif
824
825
Remy Bohmerdf063442009-07-29 18:18:43 +0200826/* maxpacket and other transfer characteristics vary by speed. */
827static inline struct usb_endpoint_descriptor *
828ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
829 struct usb_endpoint_descriptor *fs)
830{
831 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
832 return hs;
833 return fs;
834}
835
Remy Bohmerdf063442009-07-29 18:18:43 +0200836/*-------------------------------------------------------------------------*/
837
838/* descriptors that are built on-demand */
839
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400840static char manufacturer[50];
841static char product_desc[40] = DRIVER_DESC;
842static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200843
844/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400845static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200846
847/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400848static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200849 { STRING_MANUFACTURER, manufacturer, },
850 { STRING_PRODUCT, product_desc, },
851 { STRING_SERIALNUMBER, serial_number, },
852 { STRING_DATA, "Ethernet Data", },
853 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200854#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200855 { STRING_CDC, "CDC Ethernet", },
856 { STRING_CONTROL, "CDC Communications Control", },
857#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200858#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200859 { STRING_SUBSET, "CDC Ethernet Subset", },
860#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300861#ifdef CONFIG_USB_ETH_RNDIS
862 { STRING_RNDIS, "RNDIS", },
863 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
864#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200865 { } /* end of list */
866};
867
868static struct usb_gadget_strings stringtab = {
869 .language = 0x0409, /* en-us */
870 .strings = strings,
871};
872
Remy Bohmerdf063442009-07-29 18:18:43 +0200873/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500874DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
875
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200876#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500877DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200878#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200879
Remy Bohmerdf063442009-07-29 18:18:43 +0200880/*============================================================================*/
881
882/*
883 * one config, two interfaces: control, data.
884 * complications: class descriptors, and an altsetting.
885 */
886static int
887config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
888{
889 int len;
890 const struct usb_config_descriptor *config;
891 const struct usb_descriptor_header **function;
892 int hs = 0;
893
894 if (gadget_is_dualspeed(g)) {
895 hs = (g->speed == USB_SPEED_HIGH);
896 if (type == USB_DT_OTHER_SPEED_CONFIG)
897 hs = !hs;
898 }
899#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
900
901 if (index >= device_desc.bNumConfigurations)
902 return -EINVAL;
903
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300904#ifdef CONFIG_USB_ETH_RNDIS
905 /*
906 * list the RNDIS config first, to make Microsoft's drivers
907 * happy. DOCSIS 1.0 needs this too.
908 */
909 if (device_desc.bNumConfigurations == 2 && index == 0) {
910 config = &rndis_config;
911 function = which_fn(rndis);
912 } else
913#endif
914 {
915 config = &eth_config;
916 function = which_fn(eth);
917 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200918
919 /* for now, don't advertise srp-only devices */
920 if (!is_otg)
921 function++;
922
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400923 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200924 if (len < 0)
925 return len;
926 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
927 return len;
928}
929
930/*-------------------------------------------------------------------------*/
931
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300932static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400933static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200934
935static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400936set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200937{
938 int result = 0;
939 struct usb_gadget *gadget = dev->gadget;
940
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200941#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300942 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200943 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400944 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200945 &fs_status_desc);
946 dev->status_ep->driver_data = dev;
947
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400948 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200949 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400950 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200951 dev->status_ep->name, result);
952 goto done;
953 }
954 }
955#endif
956
957 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
958 dev->in_ep->driver_data = dev;
959
960 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
961 dev->out_ep->driver_data = dev;
962
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400963 /*
964 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200965 * endpoints in the default altsetting for the interface.
966 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300967 *
968 * Strictly speaking RNDIS should work the same: activation is
969 * a side effect of setting a packet filter. Deactivation is
970 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200971 */
972 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400973 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200974 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400975 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200976 dev->in_ep->name, result);
977 goto done;
978 }
979
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400980 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200981 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400982 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200983 dev->out_ep->name, result);
984 goto done;
985 }
986 }
987
988done:
989 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400990 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200991
992 /* on error, disable any endpoints */
993 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400994 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400995 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200996 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400997 (void) usb_ep_disable(dev->in_ep);
998 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200999 dev->in = NULL;
1000 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001001 } else if (!cdc_active(dev)) {
1002 /*
1003 * activate non-CDC configs right away
1004 * this isn't strictly according to the RNDIS spec
1005 */
1006 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001007 }
1008
1009 /* caller is responsible for cleanup on error */
1010 return result;
1011}
1012
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001013static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001014{
1015 if (dev->config == 0)
1016 return;
1017
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001018 debug("%s\n", __func__);
1019
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001020 rndis_uninit(dev->rndis_config);
1021
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001022 /*
1023 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001024 * pending i/o. then free the requests.
1025 */
1026
1027 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001028 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001029 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001030 usb_ep_free_request(dev->in_ep, dev->tx_req);
1031 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001032 }
1033 }
1034 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001035 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001036 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001037 usb_ep_free_request(dev->out_ep, dev->rx_req);
1038 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001039 }
1040 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001041 if (dev->status)
1042 usb_ep_disable(dev->status_ep);
1043
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001044 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001045 dev->cdc_filter = 0;
1046 dev->config = 0;
1047}
1048
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001049/*
1050 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001051 * that returns config descriptors, and altsetting code.
1052 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001053static int eth_set_config(struct eth_dev *dev, unsigned number,
1054 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001055{
1056 int result = 0;
1057 struct usb_gadget *gadget = dev->gadget;
1058
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001059 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001060 && dev->config
1061 && dev->tx_qlen != 0) {
1062 /* tx fifo is full, but we can't clear it...*/
Masahiro Yamada81e10422017-09-16 14:10:41 +09001063 pr_err("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001064 return -ESPIPE;
1065 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001066 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001067
1068 switch (number) {
1069 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001070 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001071 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001072#ifdef CONFIG_USB_ETH_RNDIS
1073 case DEV_RNDIS_CONFIG_VALUE:
1074 dev->rndis = 1;
1075 result = set_ether_config(dev, gfp_flags);
1076 break;
1077#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001078 default:
1079 result = -EINVAL;
1080 /* FALL THROUGH */
1081 case 0:
1082 break;
1083 }
1084
1085 if (result) {
1086 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001087 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001088 usb_gadget_vbus_draw(dev->gadget,
1089 gadget_is_otg(dev->gadget) ? 8 : 100);
1090 } else {
1091 char *speed;
1092 unsigned power;
1093
1094 power = 2 * eth_config.bMaxPower;
1095 usb_gadget_vbus_draw(dev->gadget, power);
1096
1097 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001098 case USB_SPEED_FULL:
1099 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001100#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001101 case USB_SPEED_HIGH:
1102 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001103#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001104 default:
1105 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001106 }
1107
1108 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001109 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001110 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001111 rndis_active(dev)
1112 ? "RNDIS"
1113 : (cdc_active(dev)
1114 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001115 : "CDC Ethernet Subset"));
1116 }
1117 return result;
1118}
1119
1120/*-------------------------------------------------------------------------*/
1121
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001122#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001123
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001124/*
1125 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001126 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001127 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001128 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1129 * command mechanism; and only one status request is ever queued.
1130 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001131static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001132{
1133 struct usb_cdc_notification *event = req->buf;
1134 int value = req->status;
1135 struct eth_dev *dev = ep->driver_data;
1136
1137 /* issue the second notification if host reads the first */
1138 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1139 && value == 0) {
1140 __le32 *data = req->buf + sizeof *event;
1141
1142 event->bmRequestType = 0xA1;
1143 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001144 event->wValue = __constant_cpu_to_le16(0);
1145 event->wIndex = __constant_cpu_to_le16(1);
1146 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001147
1148 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001149 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001150
1151 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001152 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001153 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001154 if (value == 0)
1155 return;
1156 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001157 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001158 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001159 if (event->bNotificationType ==
1160 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301161 dev->network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001162 printf("USB network up!\n");
1163 }
1164 }
1165 req->context = NULL;
1166}
1167
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001168static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001169{
1170 struct usb_request *req = dev->stat_req;
1171 struct usb_cdc_notification *event;
1172 int value;
1173
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001174 /*
1175 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001176 *
1177 * FIXME ugly idiom, maybe we'd be better with just
1178 * a "cancel the whole queue" primitive since any
1179 * unlink-one primitive has way too many error modes.
1180 * here, we "know" toggle is already clear...
1181 *
1182 * FIXME iff req->context != null just dequeue it
1183 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001184 usb_ep_disable(dev->status_ep);
1185 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001186
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001187 /*
1188 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001189 * a SPEED_CHANGE. could be useful in some configs.
1190 */
1191 event = req->buf;
1192 event->bmRequestType = 0xA1;
1193 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001194 event->wValue = __constant_cpu_to_le16(1); /* connected */
1195 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001196 event->wLength = 0;
1197
1198 req->length = sizeof *event;
1199 req->complete = eth_status_complete;
1200 req->context = dev;
1201
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001202 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001203 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001204 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001205}
1206
1207#endif
1208
1209/*-------------------------------------------------------------------------*/
1210
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001211static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001212{
1213 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001214 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001215 req->status, req->actual, req->length);
1216}
1217
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001218#ifdef CONFIG_USB_ETH_RNDIS
1219
1220static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1221{
1222 if (req->status || req->actual != req->length)
1223 debug("rndis response complete --> %d, %d/%d\n",
1224 req->status, req->actual, req->length);
1225
1226 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1227}
1228
1229static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1230{
1231 struct eth_dev *dev = ep->driver_data;
1232 int status;
1233
1234 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1235 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1236 if (status < 0)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001237 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001238}
1239
1240#endif /* RNDIS */
1241
Remy Bohmerdf063442009-07-29 18:18:43 +02001242/*
1243 * The setup() callback implements all the ep0 functionality that's not
1244 * handled lower down. CDC has a number of less-common features:
1245 *
1246 * - two interfaces: control, and ethernet data
1247 * - Ethernet data interface has two altsettings: default, and active
1248 * - class-specific descriptors for the control interface
1249 * - class-specific control requests
1250 */
1251static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001252eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001253{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001254 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001255 struct usb_request *req = dev->req;
1256 int value = -EOPNOTSUPP;
1257 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1258 u16 wValue = le16_to_cpu(ctrl->wValue);
1259 u16 wLength = le16_to_cpu(ctrl->wLength);
1260
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001261 /*
1262 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001263 * while config change events may enable network traffic.
1264 */
1265
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001266 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001267
1268 req->complete = eth_setup_complete;
1269 switch (ctrl->bRequest) {
1270
1271 case USB_REQ_GET_DESCRIPTOR:
1272 if (ctrl->bRequestType != USB_DIR_IN)
1273 break;
1274 switch (wValue >> 8) {
1275
1276 case USB_DT_DEVICE:
Kishon Vijay Abraham I58d701e2015-02-23 18:40:17 +05301277 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001278 value = min(wLength, (u16) sizeof device_desc);
1279 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001280 break;
1281 case USB_DT_DEVICE_QUALIFIER:
1282 if (!gadget_is_dualspeed(gadget))
1283 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001284 value = min(wLength, (u16) sizeof dev_qualifier);
1285 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001286 break;
1287
1288 case USB_DT_OTHER_SPEED_CONFIG:
1289 if (!gadget_is_dualspeed(gadget))
1290 break;
1291 /* FALLTHROUGH */
1292 case USB_DT_CONFIG:
1293 value = config_buf(gadget, req->buf,
1294 wValue >> 8,
1295 wValue & 0xff,
1296 gadget_is_otg(gadget));
1297 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001298 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001299 break;
1300
1301 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001302 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001303 wValue & 0xff, req->buf);
1304
1305 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001306 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001307
1308 break;
1309 }
1310 break;
1311
1312 case USB_REQ_SET_CONFIGURATION:
1313 if (ctrl->bRequestType != 0)
1314 break;
1315 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001316 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001317 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001318 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001319 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001320 break;
1321 case USB_REQ_GET_CONFIGURATION:
1322 if (ctrl->bRequestType != USB_DIR_IN)
1323 break;
1324 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001325 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001326 break;
1327
1328 case USB_REQ_SET_INTERFACE:
1329 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1330 || !dev->config
1331 || wIndex > 1)
1332 break;
1333 if (!cdc_active(dev) && wIndex != 0)
1334 break;
1335
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001336 /*
1337 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001338 * we need to kluge around that interference.
1339 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001340 if (gadget_is_pxa(gadget)) {
1341 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001342 GFP_ATOMIC);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001343 /*
1344 * PXA25x driver use non-CDC ethernet gadget.
1345 * But only _CDC and _RNDIS code can signalize
1346 * that network is working. So we signalize it
1347 * here.
1348 */
Mugunthan V N3963de32016-11-18 10:49:13 +05301349 dev->network_started = 1;
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001350 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001351 goto done_set_intf;
1352 }
1353
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001354#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001355 switch (wIndex) {
1356 case 0: /* control/master intf */
1357 if (wValue != 0)
1358 break;
1359 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001360 usb_ep_disable(dev->status_ep);
1361 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001362 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001363
Remy Bohmerdf063442009-07-29 18:18:43 +02001364 value = 0;
1365 break;
1366 case 1: /* data intf */
1367 if (wValue > 1)
1368 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001369 usb_ep_disable(dev->in_ep);
1370 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001371
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001372 /*
1373 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001374 * the default interface setting ... also, setting
1375 * the non-default interface resets filters etc.
1376 */
1377 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001378 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001379 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001380 usb_ep_enable(dev->in_ep, dev->in);
1381 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001382 dev->cdc_filter = DEFAULT_FILTER;
1383 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001384 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001385 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001386 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001387 value = 0;
1388 break;
1389 }
1390#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001391 /*
1392 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001393 * all non-PXA hardware talks real CDC ...
1394 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001395 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001396#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001397
1398done_set_intf:
1399 break;
1400 case USB_REQ_GET_INTERFACE:
1401 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1402 || !dev->config
1403 || wIndex > 1)
1404 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001405 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001406 break;
1407
1408 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001409 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001410 *(u8 *)req->buf = 0;
1411 else {
1412 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1413 /* carrier always ok ...*/
1414 *(u8 *)req->buf = 1 ;
1415 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001416 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001417 break;
1418
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001419#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001420 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001421 /*
1422 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001423 * wValue = packet filter bitmap
1424 */
1425 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1426 || !cdc_active(dev)
1427 || wLength != 0
1428 || wIndex > 1)
1429 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001430 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001431 dev->cdc_filter = wValue;
1432 value = 0;
1433 break;
1434
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001435 /*
1436 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001437 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1438 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1439 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1440 * case USB_CDC_GET_ETHERNET_STATISTIC:
1441 */
1442
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001443#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001444
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001445#ifdef CONFIG_USB_ETH_RNDIS
1446 /*
1447 * RNDIS uses the CDC command encapsulation mechanism to implement
1448 * an RPC scheme, with much getting/setting of attributes by OID.
1449 */
1450 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1451 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1452 || !rndis_active(dev)
1453 || wLength > USB_BUFSIZ
1454 || wValue
1455 || rndis_control_intf.bInterfaceNumber
1456 != wIndex)
1457 break;
1458 /* read the request, then process it */
1459 value = wLength;
1460 req->complete = rndis_command_complete;
1461 /* later, rndis_control_ack () sends a notification */
1462 break;
1463
1464 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1465 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1466 == ctrl->bRequestType
1467 && rndis_active(dev)
1468 /* && wLength >= 0x0400 */
1469 && !wValue
1470 && rndis_control_intf.bInterfaceNumber
1471 == wIndex) {
1472 u8 *buf;
1473 u32 n;
1474
1475 /* return the result */
1476 buf = rndis_get_next_response(dev->rndis_config, &n);
1477 if (buf) {
1478 memcpy(req->buf, buf, n);
1479 req->complete = rndis_response_complete;
1480 rndis_free_response(dev->rndis_config, buf);
1481 value = n;
1482 }
1483 /* else stalls ... spec says to avoid that */
1484 }
1485 break;
1486#endif /* RNDIS */
1487
Remy Bohmerdf063442009-07-29 18:18:43 +02001488 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001489 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001490 ctrl->bRequestType, ctrl->bRequest,
1491 wValue, wIndex, wLength);
1492 }
1493
1494 /* respond with data transfer before status phase? */
1495 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001496 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001497 req->length = value;
1498 req->zero = value < wLength
1499 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001500 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001501 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001502 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001503 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001504 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001505 }
1506 }
1507
1508 /* host either stalls (value < 0) or reports success */
1509 return value;
1510}
1511
Remy Bohmerdf063442009-07-29 18:18:43 +02001512/*-------------------------------------------------------------------------*/
1513
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001514static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001515
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001516static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001517 gfp_t gfp_flags)
1518{
1519 int retval = -ENOMEM;
1520 size_t size;
1521
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001522 /*
1523 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001524 * Normally we use the USB "terminate on short read" convention;
1525 * so allow up to (N*maxpacket), since that memory is normally
1526 * already allocated. Some hardware doesn't deal well with short
1527 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1528 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001529 *
1530 * RNDIS uses internal framing, and explicitly allows senders to
1531 * pad to end-of-packet. That's potentially nice for speed,
1532 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001533 */
1534
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001535 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001536 if (!req)
1537 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001538
1539 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1540 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001541 if (rndis_active(dev))
1542 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001543 size -= size % dev->out_ep->maxpacket;
1544
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001545 /*
1546 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001547 * but on at least one, checksumming fails otherwise. Note:
1548 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001549 */
1550
Joe Hershberger9f09a362015-04-08 01:41:06 -05001551 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmerdf063442009-07-29 18:18:43 +02001552 req->length = size;
1553 req->complete = rx_complete;
1554
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001555 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001556
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001557 if (retval)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001558 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001559
Remy Bohmerdf063442009-07-29 18:18:43 +02001560 return retval;
1561}
1562
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001563static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001564{
1565 struct eth_dev *dev = ep->driver_data;
1566
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001567 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001568 switch (req->status) {
1569 /* normal completion */
1570 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001571 if (rndis_active(dev)) {
1572 /* we know MaxPacketsPerTransfer == 1 here */
1573 int length = rndis_rm_hdr(req->buf, req->actual);
1574 if (length < 0)
1575 goto length_err;
1576 req->length -= length;
1577 req->actual -= length;
1578 }
1579 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1580length_err:
1581 dev->stats.rx_errors++;
1582 dev->stats.rx_length_errors++;
1583 debug("rx length %d\n", req->length);
1584 break;
1585 }
1586
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001587 dev->stats.rx_packets++;
1588 dev->stats.rx_bytes += req->length;
1589 break;
1590
1591 /* software-driven interface shutdown */
1592 case -ECONNRESET: /* unlink */
1593 case -ESHUTDOWN: /* disconnect etc */
1594 /* for hardware automagic (such as pxa) */
1595 case -ECONNABORTED: /* endpoint reset */
1596 break;
1597
1598 /* data overrun */
1599 case -EOVERFLOW:
1600 dev->stats.rx_over_errors++;
1601 /* FALLTHROUGH */
1602 default:
1603 dev->stats.rx_errors++;
1604 break;
1605 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001606
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001607 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001608}
1609
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001610static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001611{
1612
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001613 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001614
1615 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001616 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001617
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001618 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001619
1620 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001621 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001622
1623 return 0;
1624
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001625fail2:
1626 usb_ep_free_request(dev->in_ep, dev->tx_req);
1627fail1:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001628 pr_err("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001629 return -1;
1630}
1631
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001632static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001633{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001634 struct eth_dev *dev = ep->driver_data;
1635
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001636 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001637 switch (req->status) {
1638 default:
1639 dev->stats.tx_errors++;
1640 debug("tx err %d\n", req->status);
1641 /* FALLTHROUGH */
1642 case -ECONNRESET: /* unlink */
1643 case -ESHUTDOWN: /* disconnect etc */
1644 break;
1645 case 0:
1646 dev->stats.tx_bytes += req->length;
1647 }
1648 dev->stats.tx_packets++;
1649
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001650 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001651}
1652
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001653static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001654{
1655 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001656 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001657 return 1;
1658 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1659}
1660
1661#if 0
1662static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1663{
1664 struct eth_dev *dev = netdev_priv(net);
1665 int length = skb->len;
1666 int retval;
1667 struct usb_request *req = NULL;
1668 unsigned long flags;
1669
1670 /* apply outgoing CDC or RNDIS filters */
1671 if (!eth_is_promisc (dev)) {
1672 u8 *dest = skb->data;
1673
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001674 if (is_multicast_ethaddr(dest)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001675 u16 type;
1676
1677 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1678 * SET_ETHERNET_MULTICAST_FILTERS requests
1679 */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001680 if (is_broadcast_ethaddr(dest))
Remy Bohmerdf063442009-07-29 18:18:43 +02001681 type = USB_CDC_PACKET_TYPE_BROADCAST;
1682 else
1683 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1684 if (!(dev->cdc_filter & type)) {
1685 dev_kfree_skb_any (skb);
1686 return 0;
1687 }
1688 }
1689 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1690 }
1691
1692 spin_lock_irqsave(&dev->req_lock, flags);
1693 /*
1694 * this freelist can be empty if an interrupt triggered disconnect()
1695 * and reconfigured the gadget (shutting down this queue) after the
1696 * network stack decided to xmit but before we got the spinlock.
1697 */
1698 if (list_empty(&dev->tx_reqs)) {
1699 spin_unlock_irqrestore(&dev->req_lock, flags);
1700 return 1;
1701 }
1702
1703 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1704 list_del (&req->list);
1705
1706 /* temporarily stop TX queue when the freelist empties */
1707 if (list_empty (&dev->tx_reqs))
1708 netif_stop_queue (net);
1709 spin_unlock_irqrestore(&dev->req_lock, flags);
1710
1711 /* no buffer copies needed, unless the network stack did it
1712 * or the hardware can't use skb buffers.
1713 * or there's not enough space for any RNDIS headers we need
1714 */
1715 if (rndis_active(dev)) {
1716 struct sk_buff *skb_rndis;
1717
1718 skb_rndis = skb_realloc_headroom (skb,
1719 sizeof (struct rndis_packet_msg_type));
1720 if (!skb_rndis)
1721 goto drop;
1722
1723 dev_kfree_skb_any (skb);
1724 skb = skb_rndis;
1725 rndis_add_hdr (skb);
1726 length = skb->len;
1727 }
1728 req->buf = skb->data;
1729 req->context = skb;
1730 req->complete = tx_complete;
1731
1732 /* use zlp framing on tx for strict CDC-Ether conformance,
1733 * though any robust network rx path ignores extra padding.
1734 * and some hardware doesn't like to write zlps.
1735 */
1736 req->zero = 1;
1737 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1738 length++;
1739
1740 req->length = length;
1741
1742 /* throttle highspeed IRQ rate back slightly */
1743 if (gadget_is_dualspeed(dev->gadget))
1744 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1745 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1746 : 0;
1747
1748 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1749 switch (retval) {
1750 default:
1751 DEBUG (dev, "tx queue err %d\n", retval);
1752 break;
1753 case 0:
1754 net->trans_start = jiffies;
1755 atomic_inc (&dev->tx_qlen);
1756 }
1757
1758 if (retval) {
1759drop:
1760 dev->stats.tx_dropped++;
1761 dev_kfree_skb_any (skb);
1762 spin_lock_irqsave(&dev->req_lock, flags);
1763 if (list_empty (&dev->tx_reqs))
1764 netif_start_queue (net);
1765 list_add (&req->list, &dev->tx_reqs);
1766 spin_unlock_irqrestore(&dev->req_lock, flags);
1767 }
1768 return 0;
1769}
1770
1771/*-------------------------------------------------------------------------*/
1772#endif
1773
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001774static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001775{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001776 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001777
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001778 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001779 rndis_deregister(dev->rndis_config);
1780 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001781
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001782 /* we've already been disconnected ... no i/o is active */
1783 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001784 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001785 dev->req = NULL;
1786 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001787 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001788 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001789 dev->stat_req = NULL;
1790 }
1791
1792 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001793 usb_ep_free_request(dev->in_ep, dev->tx_req);
1794 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001795 }
1796
1797 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001798 usb_ep_free_request(dev->out_ep, dev->rx_req);
1799 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001800 }
1801
1802/* unregister_netdev (dev->net);*/
1803/* free_netdev(dev->net);*/
1804
Lei Wen4af32732010-12-01 23:43:43 +08001805 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001806 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001807}
1808
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001809static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001810{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001811 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001812 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001813}
1814
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001815static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001816{
1817 /* Not used */
1818}
1819
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001820static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001821{
1822 /* Not used */
1823}
1824
1825/*-------------------------------------------------------------------------*/
1826
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001827#ifdef CONFIG_USB_ETH_RNDIS
1828
1829/*
1830 * The interrupt endpoint is used in RNDIS to notify the host when messages
1831 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1832 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1833 * REMOTE_NDIS_KEEPALIVE_MSG.
1834 *
1835 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1836 * normally just one notification will be queued.
1837 */
1838
1839static void rndis_control_ack_complete(struct usb_ep *ep,
1840 struct usb_request *req)
1841{
1842 struct eth_dev *dev = ep->driver_data;
1843
1844 debug("%s...\n", __func__);
1845 if (req->status || req->actual != req->length)
1846 debug("rndis control ack complete --> %d, %d/%d\n",
1847 req->status, req->actual, req->length);
1848
Mugunthan V N3963de32016-11-18 10:49:13 +05301849 if (!dev->network_started) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001850 if (rndis_get_state(dev->rndis_config)
1851 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301852 dev->network_started = 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001853 printf("USB RNDIS network up!\n");
1854 }
1855 }
1856
1857 req->context = NULL;
1858
1859 if (req != dev->stat_req)
1860 usb_ep_free_request(ep, req);
1861}
1862
1863static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1864
Mugunthan V N095b7612016-11-18 11:09:15 +05301865#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001866static int rndis_control_ack(struct eth_device *net)
Mugunthan V N095b7612016-11-18 11:09:15 +05301867#else
1868static int rndis_control_ack(struct udevice *net)
1869#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001870{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05301871 struct ether_priv *priv = (struct ether_priv *)net->priv;
1872 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001873 int length;
1874 struct usb_request *resp = dev->stat_req;
1875
1876 /* in case RNDIS calls this after disconnect */
1877 if (!dev->status) {
1878 debug("status ENODEV\n");
1879 return -ENODEV;
1880 }
1881
1882 /* in case queue length > 1 */
1883 if (resp->context) {
1884 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1885 if (!resp)
1886 return -ENOMEM;
1887 resp->buf = rndis_resp_buf;
1888 }
1889
1890 /*
1891 * Send RNDIS RESPONSE_AVAILABLE notification;
1892 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1893 */
1894 resp->length = 8;
1895 resp->complete = rndis_control_ack_complete;
1896 resp->context = dev;
1897
1898 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1899 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1900
1901 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1902 if (length < 0) {
1903 resp->status = 0;
1904 rndis_control_ack_complete(dev->status_ep, resp);
1905 }
1906
1907 return 0;
1908}
1909
1910#else
1911
1912#define rndis_control_ack NULL
1913
1914#endif /* RNDIS */
1915
1916static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1917{
1918 if (rndis_active(dev)) {
1919 rndis_set_param_medium(dev->rndis_config,
1920 NDIS_MEDIUM_802_3,
1921 BITRATE(dev->gadget)/100);
1922 rndis_signal_connect(dev->rndis_config);
1923 }
1924}
1925
1926static int eth_stop(struct eth_dev *dev)
1927{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001928#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1929 unsigned long ts;
1930 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1931#endif
1932
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001933 if (rndis_active(dev)) {
1934 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1935 rndis_signal_disconnect(dev->rndis_config);
1936
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001937#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1938 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1939 ts = get_timer(0);
1940 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05301941 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001942#endif
1943
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001944 rndis_uninit(dev->rndis_config);
1945 dev->rndis = 0;
1946 }
1947
1948 return 0;
1949}
1950
1951/*-------------------------------------------------------------------------*/
1952
Remy Bohmerdf063442009-07-29 18:18:43 +02001953static int is_eth_addr_valid(char *str)
1954{
1955 if (strlen(str) == 17) {
1956 int i;
1957 char *p, *q;
1958 uchar ea[6];
1959
1960 /* see if it looks like an ethernet address */
1961
1962 p = str;
1963
1964 for (i = 0; i < 6; i++) {
1965 char term = (i == 5 ? '\0' : ':');
1966
1967 ea[i] = simple_strtol(p, &q, 16);
1968
1969 if ((q - p) != 2 || *q++ != term)
1970 break;
1971
1972 p = q;
1973 }
1974
Tom Riniadd21ca2012-10-31 13:30:41 +00001975 /* Now check the contents. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001976 return is_valid_ethaddr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001977 }
1978 return 0;
1979}
1980
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001981static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001982{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001983 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001984 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001985 c = toupper(c);
1986 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001987 return 10 + c - 'A';
1988 return 0;
1989}
1990
1991static int get_ether_addr(const char *str, u8 *dev_addr)
1992{
1993 if (str) {
1994 unsigned i;
1995
1996 for (i = 0; i < 6; i++) {
1997 unsigned char num;
1998
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001999 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02002000 str++;
2001 num = nibble(*str++) << 4;
2002 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002003 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02002004 }
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05002005 if (is_valid_ethaddr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02002006 return 0;
2007 }
2008 return 1;
2009}
2010
2011static int eth_bind(struct usb_gadget *gadget)
2012{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302013 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002014 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002015 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002016 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002017 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002018 u8 tmp[7];
Mugunthan V N095b7612016-11-18 11:09:15 +05302019#ifdef CONFIG_DM_ETH
2020 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2021#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002022
2023 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002024#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002025 cdc = 0;
2026#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002027#ifndef CONFIG_USB_ETH_RNDIS
2028 rndis = 0;
2029#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002030 /*
2031 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002032 * standard protocol is _strongly_ preferred for interop purposes.
2033 * (By everyone except Microsoft.)
2034 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002035 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002036 /* pxa doesn't support altsettings */
2037 cdc = 0;
2038 } else if (gadget_is_musbhdrc(gadget)) {
2039 /* reduce tx dma overhead by avoiding special cases */
2040 zlp = 0;
2041 } else if (gadget_is_sh(gadget)) {
2042 /* sh doesn't support multiple interfaces or configs */
2043 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002044 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002045 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002046 /* hardware can't write zlps */
2047 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002048 /*
2049 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002050 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2051 */
2052 cdc = 0;
2053 }
2054
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002055 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002056 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002057 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002058 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002059 /*
2060 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002061 * anything less functional on CDC-capable hardware,
2062 * so we fail in this case.
2063 */
Masahiro Yamada81e10422017-09-16 14:10:41 +09002064 pr_err("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002065 gadget->name);
2066 return -ENODEV;
2067 }
2068
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002069 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002070 * If there's an RNDIS configuration, that's what Windows wants to
2071 * be using ... so use these product IDs here and in the "linux.inf"
2072 * needed to install MSFT drivers. Current Linux kernels will use
2073 * the second configuration if it's CDC Ethernet, and need some help
2074 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002075 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002076 if (rndis) {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002077#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Remy Bohmerdf063442009-07-29 18:18:43 +02002078 device_desc.idVendor =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002079 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Remy Bohmerdf063442009-07-29 18:18:43 +02002080 device_desc.idProduct =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002081 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002082#else
2083 device_desc.idVendor =
2084 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2085 device_desc.idProduct =
2086 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2087#endif
2088 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002089
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002090 /*
2091 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2092 * drivers aren't widely available. (That may be improved by
2093 * supporting one submode of the "SAFE" variant of MDLM.)
2094 */
2095 } else {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002096#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2097 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2098 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002099#else
2100 if (!cdc) {
2101 device_desc.idVendor =
2102 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2103 device_desc.idProduct =
2104 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2105 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002106#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002107 }
2108 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002109 if (bcdDevice)
2110 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2111 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002112 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002113 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002114 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002115 if (iSerialNumber) {
2116 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002117 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002118 }
2119
2120 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002121 usb_ep_autoconfig_reset(gadget);
2122 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002123 if (!in_ep) {
2124autoconf_fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002125 pr_err("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002126 gadget->name);
2127 return -ENODEV;
2128 }
2129 in_ep->driver_data = in_ep; /* claim */
2130
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002131 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002132 if (!out_ep)
2133 goto autoconf_fail;
2134 out_ep->driver_data = out_ep; /* claim */
2135
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002136#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002137 /*
2138 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002139 * Since some hosts expect one, try to allocate one anyway.
2140 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002141 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002142 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002143 if (status_ep) {
2144 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002145 } else if (rndis) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002146 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002147 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002148#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002149 } else if (cdc) {
2150 control_intf.bNumEndpoints = 0;
2151 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002152#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002153 }
2154 }
2155#endif
2156
2157 /* one config: cdc, else minimal subset */
2158 if (!cdc) {
2159 eth_config.bNumInterfaces = 1;
2160 eth_config.iConfiguration = STRING_SUBSET;
2161
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002162 /*
2163 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002164 * with multiple controllers and must override CDC Ethernet.
2165 */
2166 fs_subset_descriptors();
2167 hs_subset_descriptors();
2168 }
2169
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002170 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002171
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002172 /* For now RNDIS is always a second config */
2173 if (rndis)
2174 device_desc.bNumConfigurations = 2;
2175
Remy Bohmerdf063442009-07-29 18:18:43 +02002176 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002177 if (rndis)
2178 dev_qualifier.bNumConfigurations = 2;
2179 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002180 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2181
2182 /* assumes ep0 uses the same value for both speeds ... */
2183 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2184
2185 /* and that all endpoints are dual-speed */
2186 hs_source_desc.bEndpointAddress =
2187 fs_source_desc.bEndpointAddress;
2188 hs_sink_desc.bEndpointAddress =
2189 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002190#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002191 if (status_ep)
2192 hs_status_desc.bEndpointAddress =
2193 fs_status_desc.bEndpointAddress;
2194#endif
2195 }
2196
2197 if (gadget_is_otg(gadget)) {
2198 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2199 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2200 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002201#ifdef CONFIG_USB_ETH_RNDIS
2202 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2203 rndis_config.bMaxPower = 4;
2204#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002205 }
2206
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002207
2208 /* network device setup */
Mugunthan V N095b7612016-11-18 11:09:15 +05302209#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +05302210 dev->net = &l_priv->netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +05302211#else
2212 dev->net = l_priv->netdev;
2213#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002214
2215 dev->cdc = cdc;
2216 dev->zlp = zlp;
2217
2218 dev->in_ep = in_ep;
2219 dev->out_ep = out_ep;
2220 dev->status_ep = status_ep;
2221
Mugunthan V N095b7612016-11-18 11:09:15 +05302222 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002223 /*
2224 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002225 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002226 * ends up in a persistent config database. It's not clear if
2227 * host side code for the SAFE thing cares -- its original BLAN
2228 * thing didn't, Sharp never assigned those addresses on Zaurii.
2229 */
Mugunthan V N095b7612016-11-18 11:09:15 +05302230#ifndef CONFIG_DM_ETH
Remy Bohmerdf063442009-07-29 18:18:43 +02002231 get_ether_addr(dev_addr, dev->net->enetaddr);
Remy Bohmerdf063442009-07-29 18:18:43 +02002232 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
Mugunthan V N095b7612016-11-18 11:09:15 +05302233#else
2234 get_ether_addr(dev_addr, pdata->enetaddr);
2235 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2236#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002237
2238 get_ether_addr(host_addr, dev->host_mac);
2239
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002240 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2241 dev->host_mac[0], dev->host_mac[1],
2242 dev->host_mac[2], dev->host_mac[3],
2243 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002244
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002245 if (rndis) {
2246 status = rndis_init();
2247 if (status < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002248 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002249 goto fail;
2250 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002251 }
2252
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002253 /*
2254 * use PKTSIZE (or aligned... from u-boot) and set
2255 * wMaxSegmentSize accordingly
2256 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002257 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2258
2259 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002260 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002261 if (!dev->req)
2262 goto fail;
2263 dev->req->buf = control_req;
2264 dev->req->complete = eth_setup_complete;
2265
2266 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002267#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002268 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002269 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2270 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002271 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002272 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002273
2274 goto fail;
2275 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002276 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002277 dev->stat_req->context = NULL;
2278 }
2279#endif
2280
2281 /* finish hookup to lower layer ... */
2282 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002283 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002284 gadget->ep0->driver_data = dev;
2285
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002286 /*
2287 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002288 * - iff DATA transfer is active, carrier is "on"
2289 * - tx queueing enabled if open *and* carrier is "on"
2290 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002291
2292 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2293 out_ep->name, in_ep->name,
2294 status_ep ? " STATUS " : "",
2295 status_ep ? status_ep->name : ""
2296 );
Mugunthan V N095b7612016-11-18 11:09:15 +05302297#ifndef CONFIG_DM_ETH
2298 printf("MAC %pM\n", dev->net->enetaddr);
2299#else
2300 printf("MAC %pM\n", pdata->enetaddr);
2301#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002302
2303 if (cdc || rndis)
2304 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2305 dev->host_mac[0], dev->host_mac[1],
2306 dev->host_mac[2], dev->host_mac[3],
2307 dev->host_mac[4], dev->host_mac[5]);
2308
2309 if (rndis) {
2310 u32 vendorID = 0;
2311
2312 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2313
2314 dev->rndis_config = rndis_register(rndis_control_ack);
2315 if (dev->rndis_config < 0) {
2316fail0:
2317 eth_unbind(gadget);
2318 debug("RNDIS setup failed\n");
2319 status = -ENODEV;
2320 goto fail;
2321 }
2322
2323 /* these set up a lot of the OIDs that RNDIS needs */
2324 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2325 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2326 &dev->stats, &dev->cdc_filter))
2327 goto fail0;
2328 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2329 manufacturer))
2330 goto fail0;
2331 if (rndis_set_param_medium(dev->rndis_config,
2332 NDIS_MEDIUM_802_3, 0))
2333 goto fail0;
2334 printf("RNDIS ready\n");
2335 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002336 return 0;
2337
2338fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002339 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002340 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002341 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002342}
2343
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002344/*-------------------------------------------------------------------------*/
2345
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302346#ifdef CONFIG_DM_USB
2347int dm_usb_init(struct eth_dev *e_dev)
2348{
2349 struct udevice *dev = NULL;
2350 int ret;
2351
2352 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2353 if (!dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002354 pr_err("No USB device found\n");
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302355 return -ENODEV;
2356 }
2357
2358 e_dev->usb_udev = dev;
2359
2360 return ret;
2361}
2362#endif
2363
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302364static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002365{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302366 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002367 struct usb_gadget *gadget;
2368 unsigned long ts;
2369 unsigned long timeout = USB_CONNECT_TIMEOUT;
2370
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302371#ifdef CONFIG_DM_USB
2372 if (dm_usb_init(dev)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002373 pr_err("USB ether not found\n");
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302374 return -ENODEV;
2375 }
2376#else
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302377 board_usb_init(0, USB_INIT_DEVICE);
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302378#endif
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302379
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002380 /* Configure default mac-addresses for the USB ethernet device */
2381#ifdef CONFIG_USBNET_DEV_ADDR
2382 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2383#endif
2384#ifdef CONFIG_USBNET_HOST_ADDR
2385 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2386#endif
2387 /* Check if the user overruled the MAC addresses */
Simon Glass64b723f2017-08-03 12:22:12 -06002388 if (env_get("usbnet_devaddr"))
2389 strlcpy(dev_addr, env_get("usbnet_devaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002390 sizeof(dev_addr));
2391
Simon Glass64b723f2017-08-03 12:22:12 -06002392 if (env_get("usbnet_hostaddr"))
2393 strlcpy(host_addr, env_get("usbnet_hostaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002394 sizeof(host_addr));
2395
2396 if (!is_eth_addr_valid(dev_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002397 pr_err("Need valid 'usbnet_devaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002398 goto fail;
2399 }
2400 if (!is_eth_addr_valid(host_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002401 pr_err("Need valid 'usbnet_hostaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002402 goto fail;
2403 }
2404
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302405 priv->eth_driver.speed = DEVSPEED;
2406 priv->eth_driver.bind = eth_bind;
2407 priv->eth_driver.unbind = eth_unbind;
2408 priv->eth_driver.setup = eth_setup;
2409 priv->eth_driver.reset = eth_disconnect;
2410 priv->eth_driver.disconnect = eth_disconnect;
2411 priv->eth_driver.suspend = eth_suspend;
2412 priv->eth_driver.resume = eth_resume;
2413 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen4af32732010-12-01 23:43:43 +08002414 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002415
2416 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002417
2418 packet_received = 0;
2419 packet_sent = 0;
2420
2421 gadget = dev->gadget;
2422 usb_gadget_connect(gadget);
2423
Simon Glass64b723f2017-08-03 12:22:12 -06002424 if (env_get("cdc_connect_timeout"))
2425 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
Remy Bohmerdf063442009-07-29 18:18:43 +02002426 NULL, 10) * CONFIG_SYS_HZ;
2427 ts = get_timer(0);
Mugunthan V N3963de32016-11-18 10:49:13 +05302428 while (!dev->network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002429 /* Handle control-c and timeouts */
2430 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002431 pr_err("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002432 goto fail;
2433 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302434 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002435 }
2436
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002437 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002438 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002439 return 0;
2440fail:
2441 return -1;
2442}
2443
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302444static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002445{
2446 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002447 void *rndis_pkt = NULL;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302448 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002449 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002450 unsigned long ts;
2451 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002452
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002453 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002454
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002455 /* new buffer is needed to include RNDIS header */
2456 if (rndis_active(dev)) {
2457 rndis_pkt = malloc(length +
2458 sizeof(struct rndis_packet_msg_type));
2459 if (!rndis_pkt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002460 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002461 goto drop;
2462 }
2463 rndis_add_hdr(rndis_pkt, length);
2464 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002465 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002466 packet = rndis_pkt;
2467 length += sizeof(struct rndis_packet_msg_type);
2468 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002469 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002470 req->context = NULL;
2471 req->complete = tx_complete;
2472
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002473 /*
2474 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002475 * though any robust network rx path ignores extra padding.
2476 * and some hardware doesn't like to write zlps.
2477 */
2478 req->zero = 1;
2479 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2480 length++;
2481
2482 req->length = length;
2483#if 0
2484 /* throttle highspeed IRQ rate back slightly */
2485 if (gadget_is_dualspeed(dev->gadget))
2486 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2487 ? ((dev->tx_qlen % qmult) != 0) : 0;
2488#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002489 dev->tx_qlen = 1;
2490 ts = get_timer(0);
2491 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002492
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002493 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002494
2495 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002496 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002497 while (!packet_sent) {
2498 if (get_timer(ts) > timeout) {
2499 printf("timeout sending packets to usb ethernet\n");
2500 return -1;
2501 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302502 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002503 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002504 if (rndis_pkt)
2505 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002506
2507 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002508drop:
2509 dev->stats.tx_dropped++;
2510 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002511}
2512
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302513static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002514{
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302515 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002516
Remy Bohmerdf063442009-07-29 18:18:43 +02002517 return 0;
2518}
2519
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302520void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002521{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302522 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002523
Lei Wen4af32732010-12-01 23:43:43 +08002524 /* If the gadget not registered, simple return */
2525 if (!dev->gadget)
2526 return;
2527
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002528 /*
2529 * Some USB controllers may need additional deinitialization here
2530 * before dropping pull-up (also due to hardware issues).
2531 * For example: unhandled interrupt with status stage started may
2532 * bring the controller to fully broken state (until board reset).
2533 * There are some variants to debug and fix such cases:
2534 * 1) In the case of RNDIS connection eth_stop can perform additional
2535 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2536 * 2) 'pullup' callback in your UDC driver can be improved to perform
2537 * this deinitialization.
2538 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002539 eth_stop(dev);
2540
Remy Bohmerdf063442009-07-29 18:18:43 +02002541 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002542
2543 /* Clear pending interrupt */
2544 if (dev->network_started) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302545 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002546 dev->network_started = 0;
2547 }
2548
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302549 usb_gadget_unregister_driver(&priv->eth_driver);
Mugunthan V N095b7612016-11-18 11:09:15 +05302550#ifndef CONFIG_DM_USB
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302551 board_usb_cleanup(0, USB_INIT_DEVICE);
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302552#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002553}
2554
Mugunthan V N095b7612016-11-18 11:09:15 +05302555#ifndef CONFIG_DM_ETH
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302556static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2557{
2558 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2559
2560 return _usb_eth_init(priv);
2561}
2562
2563static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2564{
2565 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2566
2567 return _usb_eth_send(priv, packet, length);
2568}
2569
2570static int usb_eth_recv(struct eth_device *netdev)
2571{
2572 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2573 struct eth_dev *dev = &priv->ethdev;
2574 int ret;
2575
2576 ret = _usb_eth_recv(priv);
2577 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002578 pr_err("error packet receive\n");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302579 return ret;
2580 }
2581
2582 if (!packet_received)
2583 return 0;
2584
2585 if (dev->rx_req) {
2586 net_process_received_packet(net_rx_packets[0],
2587 dev->rx_req->length);
2588 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002589 pr_err("dev->rx_req invalid");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302590 }
2591 packet_received = 0;
2592 rx_submit(dev, dev->rx_req, 0);
2593
2594 return 0;
2595}
2596
2597void usb_eth_halt(struct eth_device *netdev)
2598{
2599 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2600
2601 _usb_eth_halt(priv);
2602}
2603
Remy Bohmerdf063442009-07-29 18:18:43 +02002604int usb_eth_initialize(bd_t *bi)
2605{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302606 struct eth_device *netdev = &l_priv->netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002607
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002608 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002609
2610 netdev->init = usb_eth_init;
2611 netdev->send = usb_eth_send;
2612 netdev->recv = usb_eth_recv;
2613 netdev->halt = usb_eth_halt;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302614 netdev->priv = l_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +02002615
2616#ifdef CONFIG_MCAST_TFTP
2617 #error not supported
2618#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002619 eth_register(netdev);
2620 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002621}
Mugunthan V N095b7612016-11-18 11:09:15 +05302622#else
2623static int usb_eth_start(struct udevice *dev)
2624{
2625 struct ether_priv *priv = dev_get_priv(dev);
2626
2627 return _usb_eth_init(priv);
2628}
2629
2630static int usb_eth_send(struct udevice *dev, void *packet, int length)
2631{
2632 struct ether_priv *priv = dev_get_priv(dev);
2633
2634 return _usb_eth_send(priv, packet, length);
2635}
2636
2637static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2638{
2639 struct ether_priv *priv = dev_get_priv(dev);
2640 struct eth_dev *ethdev = &priv->ethdev;
2641 int ret;
2642
2643 ret = _usb_eth_recv(priv);
2644 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002645 pr_err("error packet receive\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302646 return ret;
2647 }
2648
2649 if (packet_received) {
2650 if (ethdev->rx_req) {
2651 *packetp = (uchar *)net_rx_packets[0];
2652 return ethdev->rx_req->length;
2653 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002654 pr_err("dev->rx_req invalid");
Mugunthan V N095b7612016-11-18 11:09:15 +05302655 return -EFAULT;
2656 }
2657 }
2658
2659 return -EAGAIN;
2660}
2661
2662static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2663 int length)
2664{
2665 struct ether_priv *priv = dev_get_priv(dev);
2666 struct eth_dev *ethdev = &priv->ethdev;
2667
2668 packet_received = 0;
2669
2670 return rx_submit(ethdev, ethdev->rx_req, 0);
2671}
2672
2673static void usb_eth_stop(struct udevice *dev)
2674{
2675 struct ether_priv *priv = dev_get_priv(dev);
2676
2677 _usb_eth_halt(priv);
2678}
2679
2680static int usb_eth_probe(struct udevice *dev)
2681{
2682 struct ether_priv *priv = dev_get_priv(dev);
2683 struct eth_pdata *pdata = dev_get_platdata(dev);
2684
2685 priv->netdev = dev;
2686 l_priv = priv;
2687
2688 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
Simon Glass8551d552017-08-03 12:22:11 -06002689 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
Mugunthan V N095b7612016-11-18 11:09:15 +05302690
2691 return 0;
2692}
2693
2694static const struct eth_ops usb_eth_ops = {
2695 .start = usb_eth_start,
2696 .send = usb_eth_send,
2697 .recv = usb_eth_recv,
2698 .free_pkt = usb_eth_free_pkt,
2699 .stop = usb_eth_stop,
2700};
2701
2702int usb_ether_init(void)
2703{
2704 struct udevice *dev;
2705 struct udevice *usb_dev;
2706 int ret;
2707
2708 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
2709 if (!usb_dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002710 pr_err("No USB device found\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302711 return ret;
2712 }
2713
2714 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2715 if (!dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002716 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302717 return ret;
2718 }
2719
2720 return 0;
2721}
2722
2723U_BOOT_DRIVER(eth_usb) = {
2724 .name = "usb_ether",
2725 .id = UCLASS_ETH,
2726 .probe = usb_eth_probe,
2727 .ops = &usb_eth_ops,
2728 .priv_auto_alloc_size = sizeof(struct ether_priv),
2729 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2730 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2731};
2732#endif /* CONFIG_DM_ETH */