blob: 7830a4aee9422e0b8d103c7965ca0d924871df3c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-07-29 18:18:43 +02002/*
3 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 *
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
Remy Bohmerdf063442009-07-29 18:18:43 +02008 */
9
10#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060012#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <part.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090015#include <linux/errno.h>
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +030016#include <linux/netdevice.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020017#include <linux/usb/ch9.h>
18#include <linux/usb/cdc.h>
19#include <linux/usb/gadget.h>
20#include <net.h>
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +053021#include <usb.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030022#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060023#include <memalign.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020024#include <linux/ctype.h>
25
26#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030027#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020028
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053029#include <dm.h>
Mugunthan V N095b7612016-11-18 11:09:15 +053030#include <dm/lists.h>
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053031#include <dm/uclass-internal.h>
32#include <dm/device-internal.h>
33
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030034#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040035
Remy Bohmerdf063442009-07-29 18:18:43 +020036extern 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 */
Remy Bohmerdf063442009-07-29 18:18:43 +020075
76#define DRIVER_DESC "Ethernet Gadget"
77/* Based on linux 2.6.27 version */
78#define DRIVER_VERSION "May Day 2005"
79
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040080static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020081
82#define RX_EXTRA 20 /* guard against rx overflows */
83
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030084#ifndef CONFIG_USB_ETH_RNDIS
85#define rndis_uninit(x) do {} while (0)
86#define rndis_deregister(c) do {} while (0)
87#define rndis_exit() do {} while (0)
88#endif
89
90/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +020091#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
92 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
93 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
94 |USB_CDC_PACKET_TYPE_DIRECTED)
95
96#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
97
98/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +030099
100struct eth_dev {
101 struct usb_gadget *gadget;
102 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300103 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300104
105 u8 config;
106 struct usb_ep *in_ep, *out_ep, *status_ep;
107 const struct usb_endpoint_descriptor
108 *in, *out, *status;
109
110 struct usb_request *tx_req, *rx_req;
111
Mugunthan V N095b7612016-11-18 11:09:15 +0530112#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300113 struct eth_device *net;
Mugunthan V N095b7612016-11-18 11:09:15 +0530114#else
115 struct udevice *net;
116#endif
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300117 struct net_device_stats stats;
118 unsigned int tx_qlen;
119
120 unsigned zlp:1;
121 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300122 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300123 unsigned suspended:1;
124 unsigned network_started:1;
125 u16 cdc_filter;
126 unsigned long todo;
127 int mtu;
128#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300129 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300130 u8 host_mac[ETH_ALEN];
131};
132
133/*
134 * This version autoconfigures as much as possible at run-time.
135 *
136 * It also ASSUMES a self-powered device, without remote wakeup,
137 * although remote wakeup support would make sense.
138 */
139
140/*-------------------------------------------------------------------------*/
Mugunthan V N88e48be2016-11-18 11:06:13 +0530141struct ether_priv {
142 struct eth_dev ethdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530143#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +0530144 struct eth_device netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530145#else
146 struct udevice *netdev;
147#endif
Mugunthan V N88e48be2016-11-18 11:06:13 +0530148 struct usb_gadget_driver eth_driver;
149};
150
151struct ether_priv eth_priv;
152struct ether_priv *l_priv = &eth_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +0200153
154/*-------------------------------------------------------------------------*/
155
156/* "main" config is either CDC, or its simple subset */
157static inline int is_cdc(struct eth_dev *dev)
158{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200159#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200160 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200161#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200162 return 0; /* only subset possible */
163#else
164 return dev->cdc; /* depends on what hardware we found */
165#endif
166}
167
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300168/* "secondary" RNDIS config may sometimes be activated */
169static inline int rndis_active(struct eth_dev *dev)
170{
171#ifdef CONFIG_USB_ETH_RNDIS
172 return dev->rndis;
173#else
174 return 0;
175#endif
176}
177
178#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
179#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200180
181#define DEFAULT_QLEN 2 /* double buffering by default */
182
183/* peak bulk transfer bits-per-second */
184#define HS_BPS (13 * 512 * 8 * 1000 * 8)
185#define FS_BPS (19 * 64 * 1 * 1000 * 8)
186
187#ifdef CONFIG_USB_GADGET_DUALSPEED
188#define DEVSPEED USB_SPEED_HIGH
189
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400190#ifdef CONFIG_USB_ETH_QMULT
191#define qmult CONFIG_USB_ETH_QMULT
192#else
193#define qmult 5
194#endif
195
Remy Bohmerdf063442009-07-29 18:18:43 +0200196/* for dual-speed hardware, use deeper queues at highspeed */
197#define qlen(gadget) \
198 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
199
200static inline int BITRATE(struct usb_gadget *g)
201{
202 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
203}
204
205#else /* full speed (low speed doesn't do bulk) */
206
207#define qmult 1
208
209#define DEVSPEED USB_SPEED_FULL
210
211#define qlen(gadget) DEFAULT_QLEN
212
213static inline int BITRATE(struct usb_gadget *g)
214{
215 return FS_BPS;
216}
217#endif
218
Remy Bohmerdf063442009-07-29 18:18:43 +0200219/*-------------------------------------------------------------------------*/
220
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400221/*
222 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200223 * Instead: allocate your own, using normal USB-IF procedures.
224 */
225
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400226/*
227 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200228 * It's for devices with only CDC Ethernet configurations.
229 */
230#define CDC_VENDOR_NUM 0x0525 /* NetChip */
231#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
232
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400233/*
234 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200235 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
236 * with pxa250. We're protocol-compatible, if the host-side drivers
237 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
238 * drivers that need to hard-wire endpoint numbers have a hook.
239 *
240 * The protocol is a minimal subset of CDC Ether, which works on any bulk
241 * hardware that's not deeply broken ... even on hardware that can't talk
242 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
243 * doesn't handle control-OUT).
244 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300245#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
246#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200247
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400248/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300249 * For hardware that can talk RNDIS and either of the above protocols,
250 * use this ID ... the windows INF files will know it. Unless it's
251 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
252 * the non-RNDIS configuration.
253 */
254#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
255#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
256
257/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400258 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200259 * device descriptor, either numbers or strings or both. These string
260 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
261 */
262
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300263/*
264 * Emulating them in eth_bind:
265 * static ushort idVendor;
266 * static ushort idProduct;
267 */
268
Maxime Ripard48e78f82017-09-07 09:15:08 +0200269#if defined(CONFIG_USB_GADGET_MANUFACTURER)
270static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmerdf063442009-07-29 18:18:43 +0200271#else
Bin Meng75574052016-02-05 19:30:11 -0800272static char *iManufacturer = "U-Boot";
Remy Bohmerdf063442009-07-29 18:18:43 +0200273#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300274
275/* These probably need to be configurable. */
276static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200277static char *iProduct;
278static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300279
Remy Bohmerdf063442009-07-29 18:18:43 +0200280static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300281
Remy Bohmerdf063442009-07-29 18:18:43 +0200282static char host_addr[18];
283
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300284
Remy Bohmerdf063442009-07-29 18:18:43 +0200285/*-------------------------------------------------------------------------*/
286
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400287/*
288 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200289 * ep0 implementation: descriptors, config management, setup().
290 * also optional class-specific notification interrupt transfer.
291 */
292
293/*
294 * DESCRIPTORS ... most are static, but strings and (full) configuration
295 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300296 * our simple subset, with RNDIS as an optional second configuration.
297 *
298 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
299 * the class descriptors match a modem (they're ignored; it's really just
300 * Ethernet functionality), they don't need the NOP altsetting, and the
301 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200302 */
303
304#define STRING_MANUFACTURER 1
305#define STRING_PRODUCT 2
306#define STRING_ETHADDR 3
307#define STRING_DATA 4
308#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300309#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200310#define STRING_CDC 7
311#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300312#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200313#define STRING_SERIALNUMBER 10
314
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300315/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200316#define USB_BUFSIZ 256
317
318/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300319 * This device advertises one configuration, eth_config, unless RNDIS
320 * is enabled (rndis_config) on hardware supporting at least two configs.
321 *
322 * NOTE: Controllers like superh_udc should probably be able to use
323 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200324 *
325 * FIXME define some higher-powered configurations to make it easier
326 * to recharge batteries ...
327 */
328
329#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300330#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200331
332static struct usb_device_descriptor
333device_desc = {
334 .bLength = sizeof device_desc,
335 .bDescriptorType = USB_DT_DEVICE,
336
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400337 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200338
339 .bDeviceClass = USB_CLASS_COMM,
340 .bDeviceSubClass = 0,
341 .bDeviceProtocol = 0,
342
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400343 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
344 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200345 .iManufacturer = STRING_MANUFACTURER,
346 .iProduct = STRING_PRODUCT,
347 .bNumConfigurations = 1,
348};
349
350static struct usb_otg_descriptor
351otg_descriptor = {
352 .bLength = sizeof otg_descriptor,
353 .bDescriptorType = USB_DT_OTG,
354
355 .bmAttributes = USB_OTG_SRP,
356};
357
358static struct usb_config_descriptor
359eth_config = {
360 .bLength = sizeof eth_config,
361 .bDescriptorType = USB_DT_CONFIG,
362
363 /* compute wTotalLength on the fly */
364 .bNumInterfaces = 2,
365 .bConfigurationValue = DEV_CONFIG_VALUE,
366 .iConfiguration = STRING_CDC,
367 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
368 .bMaxPower = 1,
369};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300370
371#ifdef CONFIG_USB_ETH_RNDIS
372static struct usb_config_descriptor
373rndis_config = {
374 .bLength = sizeof rndis_config,
375 .bDescriptorType = USB_DT_CONFIG,
376
377 /* compute wTotalLength on the fly */
378 .bNumInterfaces = 2,
379 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
380 .iConfiguration = STRING_RNDIS,
381 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
382 .bMaxPower = 1,
383};
384#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200385
386/*
387 * Compared to the simple CDC subset, the full CDC Ethernet model adds
388 * three class descriptors, two interface descriptors, optional status
389 * endpoint. Both have a "data" interface and two bulk endpoints.
390 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300391 *
392 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
393 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
394 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
395 * work around those bugs) are unlikely to ever come from MSFT, you may
396 * wish to avoid using RNDIS.
397 *
398 * MCCI offers an alternative to RNDIS if you need to connect to Windows
399 * but have hardware that can't support CDC Ethernet. We add descriptors
400 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
401 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
402 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200403 */
404
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200405#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200406static struct usb_interface_descriptor
407control_intf = {
408 .bLength = sizeof control_intf,
409 .bDescriptorType = USB_DT_INTERFACE,
410
411 .bInterfaceNumber = 0,
412 /* status endpoint is optional; this may be patched later */
413 .bNumEndpoints = 1,
414 .bInterfaceClass = USB_CLASS_COMM,
415 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
416 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
417 .iInterface = STRING_CONTROL,
418};
419#endif
420
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300421#ifdef CONFIG_USB_ETH_RNDIS
422static const struct usb_interface_descriptor
423rndis_control_intf = {
424 .bLength = sizeof rndis_control_intf,
425 .bDescriptorType = USB_DT_INTERFACE,
426
427 .bInterfaceNumber = 0,
428 .bNumEndpoints = 1,
429 .bInterfaceClass = USB_CLASS_COMM,
430 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
431 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
432 .iInterface = STRING_RNDIS_CONTROL,
433};
434#endif
435
Remy Bohmerdf063442009-07-29 18:18:43 +0200436static const struct usb_cdc_header_desc header_desc = {
437 .bLength = sizeof header_desc,
438 .bDescriptorType = USB_DT_CS_INTERFACE,
439 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
440
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400441 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200442};
443
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200444#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200445
446static const struct usb_cdc_union_desc union_desc = {
447 .bLength = sizeof union_desc,
448 .bDescriptorType = USB_DT_CS_INTERFACE,
449 .bDescriptorSubType = USB_CDC_UNION_TYPE,
450
451 .bMasterInterface0 = 0, /* index of control interface */
452 .bSlaveInterface0 = 1, /* index of DATA interface */
453};
454
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300455#endif /* CDC || RNDIS */
456
457#ifdef CONFIG_USB_ETH_RNDIS
458
459static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
460 .bLength = sizeof call_mgmt_descriptor,
461 .bDescriptorType = USB_DT_CS_INTERFACE,
462 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
463
464 .bmCapabilities = 0x00,
465 .bDataInterface = 0x01,
466};
467
468static const struct usb_cdc_acm_descriptor acm_descriptor = {
469 .bLength = sizeof acm_descriptor,
470 .bDescriptorType = USB_DT_CS_INTERFACE,
471 .bDescriptorSubType = USB_CDC_ACM_TYPE,
472
473 .bmCapabilities = 0x00,
474};
475
476#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200477
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200478#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200479
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400480/*
481 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200482 * ways: data endpoints live in the control interface, there's no data
483 * interface, and it's not used to talk to a cell phone radio.
484 */
485
486static const struct usb_cdc_mdlm_desc mdlm_desc = {
487 .bLength = sizeof mdlm_desc,
488 .bDescriptorType = USB_DT_CS_INTERFACE,
489 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
490
491 .bcdVersion = __constant_cpu_to_le16(0x0100),
492 .bGUID = {
493 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
494 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
495 },
496};
497
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400498/*
499 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200500 * can't really use its struct. All we do here is say that we're using
501 * the submode of "SAFE" which directly matches the CDC Subset.
502 */
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530503#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200504static const u8 mdlm_detail_desc[] = {
505 6,
506 USB_DT_CS_INTERFACE,
507 USB_CDC_MDLM_DETAIL_TYPE,
508
509 0, /* "SAFE" */
510 0, /* network control capabilities (none) */
511 0, /* network data capabilities ("raw" encapsulation) */
512};
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530513#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200514
515#endif
516
Remy Bohmerdf063442009-07-29 18:18:43 +0200517static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400518 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200519 .bDescriptorType = USB_DT_CS_INTERFACE,
520 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
521
522 /* this descriptor actually adds value, surprise! */
523 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400524 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
Bin Meng2ccb8b02018-07-31 02:55:22 -0700525 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400526 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200527 .bNumberPowerFilters = 0,
528};
529
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200530#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200531
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400532/*
533 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200534 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
535 * packet, to simplify cancellation; and a big transfer interval, to
536 * waste less bandwidth.
537 *
538 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
539 * if they ignore the connect/disconnect notifications that real aether
540 * can provide. more advanced cdc configurations might want to support
541 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300542 *
543 * RNDIS requires the status endpoint, since it uses that encapsulation
544 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200545 */
546
547#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
548#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
549
550static struct usb_endpoint_descriptor
551fs_status_desc = {
552 .bLength = USB_DT_ENDPOINT_SIZE,
553 .bDescriptorType = USB_DT_ENDPOINT,
554
555 .bEndpointAddress = USB_DIR_IN,
556 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400557 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200558 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
559};
560#endif
561
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200562#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200563
564/* the default data interface has no endpoints ... */
565
566static const struct usb_interface_descriptor
567data_nop_intf = {
568 .bLength = sizeof data_nop_intf,
569 .bDescriptorType = USB_DT_INTERFACE,
570
571 .bInterfaceNumber = 1,
572 .bAlternateSetting = 0,
573 .bNumEndpoints = 0,
574 .bInterfaceClass = USB_CLASS_CDC_DATA,
575 .bInterfaceSubClass = 0,
576 .bInterfaceProtocol = 0,
577};
578
579/* ... but the "real" data interface has two bulk endpoints */
580
581static const struct usb_interface_descriptor
582data_intf = {
583 .bLength = sizeof data_intf,
584 .bDescriptorType = USB_DT_INTERFACE,
585
586 .bInterfaceNumber = 1,
587 .bAlternateSetting = 1,
588 .bNumEndpoints = 2,
589 .bInterfaceClass = USB_CLASS_CDC_DATA,
590 .bInterfaceSubClass = 0,
591 .bInterfaceProtocol = 0,
592 .iInterface = STRING_DATA,
593};
594
595#endif
596
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300597#ifdef CONFIG_USB_ETH_RNDIS
598
599/* RNDIS doesn't activate by changing to the "real" altsetting */
600
601static const struct usb_interface_descriptor
602rndis_data_intf = {
603 .bLength = sizeof rndis_data_intf,
604 .bDescriptorType = USB_DT_INTERFACE,
605
606 .bInterfaceNumber = 1,
607 .bAlternateSetting = 0,
608 .bNumEndpoints = 2,
609 .bInterfaceClass = USB_CLASS_CDC_DATA,
610 .bInterfaceSubClass = 0,
611 .bInterfaceProtocol = 0,
612 .iInterface = STRING_DATA,
613};
614
615#endif
616
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200617#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200618
619/*
620 * "Simple" CDC-subset option is a simple vendor-neutral model that most
621 * full speed controllers can handle: one interface, two bulk endpoints.
622 *
623 * To assist host side drivers, we fancy it up a bit, and add descriptors
624 * so some host side drivers will understand it as a "SAFE" variant.
625 */
626
627static const struct usb_interface_descriptor
628subset_data_intf = {
629 .bLength = sizeof subset_data_intf,
630 .bDescriptorType = USB_DT_INTERFACE,
631
632 .bInterfaceNumber = 0,
633 .bAlternateSetting = 0,
634 .bNumEndpoints = 2,
635 .bInterfaceClass = USB_CLASS_COMM,
636 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
637 .bInterfaceProtocol = 0,
638 .iInterface = STRING_DATA,
639};
640
641#endif /* SUBSET */
642
Remy Bohmerdf063442009-07-29 18:18:43 +0200643static struct usb_endpoint_descriptor
644fs_source_desc = {
645 .bLength = USB_DT_ENDPOINT_SIZE,
646 .bDescriptorType = USB_DT_ENDPOINT,
647
648 .bEndpointAddress = USB_DIR_IN,
649 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700650 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200651};
652
653static struct usb_endpoint_descriptor
654fs_sink_desc = {
655 .bLength = USB_DT_ENDPOINT_SIZE,
656 .bDescriptorType = USB_DT_ENDPOINT,
657
658 .bEndpointAddress = USB_DIR_OUT,
659 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700660 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200661};
662
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400663static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200664 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200665#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200666 /* "cdc" mode descriptors */
667 (struct usb_descriptor_header *) &control_intf,
668 (struct usb_descriptor_header *) &header_desc,
669 (struct usb_descriptor_header *) &union_desc,
670 (struct usb_descriptor_header *) &ether_desc,
671 /* NOTE: status endpoint may need to be removed */
672 (struct usb_descriptor_header *) &fs_status_desc,
673 /* data interface, with altsetting */
674 (struct usb_descriptor_header *) &data_nop_intf,
675 (struct usb_descriptor_header *) &data_intf,
676 (struct usb_descriptor_header *) &fs_source_desc,
677 (struct usb_descriptor_header *) &fs_sink_desc,
678 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200679#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200680};
681
682static inline void fs_subset_descriptors(void)
683{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200684#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200685 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
686 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
687 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
688 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
689 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
690 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
691 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
692 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
693 fs_eth_function[8] = NULL;
694#else
695 fs_eth_function[1] = NULL;
696#endif
697}
698
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300699#ifdef CONFIG_USB_ETH_RNDIS
700static const struct usb_descriptor_header *fs_rndis_function[] = {
701 (struct usb_descriptor_header *) &otg_descriptor,
702 /* control interface matches ACM, not Ethernet */
703 (struct usb_descriptor_header *) &rndis_control_intf,
704 (struct usb_descriptor_header *) &header_desc,
705 (struct usb_descriptor_header *) &call_mgmt_descriptor,
706 (struct usb_descriptor_header *) &acm_descriptor,
707 (struct usb_descriptor_header *) &union_desc,
708 (struct usb_descriptor_header *) &fs_status_desc,
709 /* data interface has no altsetting */
710 (struct usb_descriptor_header *) &rndis_data_intf,
711 (struct usb_descriptor_header *) &fs_source_desc,
712 (struct usb_descriptor_header *) &fs_sink_desc,
713 NULL,
714};
715#endif
716
Remy Bohmerdf063442009-07-29 18:18:43 +0200717/*
718 * usb 2.0 devices need to expose both high speed and full speed
719 * descriptors, unless they only run at full speed.
720 */
721
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200722#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200723static struct usb_endpoint_descriptor
724hs_status_desc = {
725 .bLength = USB_DT_ENDPOINT_SIZE,
726 .bDescriptorType = USB_DT_ENDPOINT,
727
728 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400729 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200730 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
731};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200732#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200733
734static struct usb_endpoint_descriptor
735hs_source_desc = {
736 .bLength = USB_DT_ENDPOINT_SIZE,
737 .bDescriptorType = USB_DT_ENDPOINT,
738
739 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400740 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200741};
742
743static struct usb_endpoint_descriptor
744hs_sink_desc = {
745 .bLength = USB_DT_ENDPOINT_SIZE,
746 .bDescriptorType = USB_DT_ENDPOINT,
747
748 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400749 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200750};
751
752static struct usb_qualifier_descriptor
753dev_qualifier = {
754 .bLength = sizeof dev_qualifier,
755 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
756
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400757 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200758 .bDeviceClass = USB_CLASS_COMM,
759
760 .bNumConfigurations = 1,
761};
762
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400763static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200764 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200765#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200766 /* "cdc" mode descriptors */
767 (struct usb_descriptor_header *) &control_intf,
768 (struct usb_descriptor_header *) &header_desc,
769 (struct usb_descriptor_header *) &union_desc,
770 (struct usb_descriptor_header *) &ether_desc,
771 /* NOTE: status endpoint may need to be removed */
772 (struct usb_descriptor_header *) &hs_status_desc,
773 /* data interface, with altsetting */
774 (struct usb_descriptor_header *) &data_nop_intf,
775 (struct usb_descriptor_header *) &data_intf,
776 (struct usb_descriptor_header *) &hs_source_desc,
777 (struct usb_descriptor_header *) &hs_sink_desc,
778 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200779#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200780};
781
782static inline void hs_subset_descriptors(void)
783{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200784#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200785 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
786 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
787 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
788 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
789 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
790 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
791 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
792 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
793 hs_eth_function[8] = NULL;
794#else
795 hs_eth_function[1] = NULL;
796#endif
797}
798
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300799#ifdef CONFIG_USB_ETH_RNDIS
800static const struct usb_descriptor_header *hs_rndis_function[] = {
801 (struct usb_descriptor_header *) &otg_descriptor,
802 /* control interface matches ACM, not Ethernet */
803 (struct usb_descriptor_header *) &rndis_control_intf,
804 (struct usb_descriptor_header *) &header_desc,
805 (struct usb_descriptor_header *) &call_mgmt_descriptor,
806 (struct usb_descriptor_header *) &acm_descriptor,
807 (struct usb_descriptor_header *) &union_desc,
808 (struct usb_descriptor_header *) &hs_status_desc,
809 /* data interface has no altsetting */
810 (struct usb_descriptor_header *) &rndis_data_intf,
811 (struct usb_descriptor_header *) &hs_source_desc,
812 (struct usb_descriptor_header *) &hs_sink_desc,
813 NULL,
814};
815#endif
816
817
Remy Bohmerdf063442009-07-29 18:18:43 +0200818/* maxpacket and other transfer characteristics vary by speed. */
819static inline struct usb_endpoint_descriptor *
820ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
821 struct usb_endpoint_descriptor *fs)
822{
823 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
824 return hs;
825 return fs;
826}
827
Remy Bohmerdf063442009-07-29 18:18:43 +0200828/*-------------------------------------------------------------------------*/
829
830/* descriptors that are built on-demand */
831
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400832static char manufacturer[50];
833static char product_desc[40] = DRIVER_DESC;
834static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200835
836/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400837static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200838
839/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400840static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200841 { STRING_MANUFACTURER, manufacturer, },
842 { STRING_PRODUCT, product_desc, },
843 { STRING_SERIALNUMBER, serial_number, },
844 { STRING_DATA, "Ethernet Data", },
845 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200846#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200847 { STRING_CDC, "CDC Ethernet", },
848 { STRING_CONTROL, "CDC Communications Control", },
849#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200850#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200851 { STRING_SUBSET, "CDC Ethernet Subset", },
852#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300853#ifdef CONFIG_USB_ETH_RNDIS
854 { STRING_RNDIS, "RNDIS", },
855 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
856#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200857 { } /* end of list */
858};
859
860static struct usb_gadget_strings stringtab = {
861 .language = 0x0409, /* en-us */
862 .strings = strings,
863};
864
Remy Bohmerdf063442009-07-29 18:18:43 +0200865/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500866DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
867
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200868#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500869DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200870#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200871
Remy Bohmerdf063442009-07-29 18:18:43 +0200872/*============================================================================*/
873
874/*
875 * one config, two interfaces: control, data.
876 * complications: class descriptors, and an altsetting.
877 */
878static int
879config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
880{
881 int len;
882 const struct usb_config_descriptor *config;
883 const struct usb_descriptor_header **function;
884 int hs = 0;
885
886 if (gadget_is_dualspeed(g)) {
887 hs = (g->speed == USB_SPEED_HIGH);
888 if (type == USB_DT_OTHER_SPEED_CONFIG)
889 hs = !hs;
890 }
891#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
892
893 if (index >= device_desc.bNumConfigurations)
894 return -EINVAL;
895
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300896#ifdef CONFIG_USB_ETH_RNDIS
897 /*
898 * list the RNDIS config first, to make Microsoft's drivers
899 * happy. DOCSIS 1.0 needs this too.
900 */
901 if (device_desc.bNumConfigurations == 2 && index == 0) {
902 config = &rndis_config;
903 function = which_fn(rndis);
904 } else
905#endif
906 {
907 config = &eth_config;
908 function = which_fn(eth);
909 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200910
911 /* for now, don't advertise srp-only devices */
912 if (!is_otg)
913 function++;
914
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400915 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200916 if (len < 0)
917 return len;
918 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
919 return len;
920}
921
922/*-------------------------------------------------------------------------*/
923
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300924static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400925static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200926
927static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400928set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200929{
930 int result = 0;
931 struct usb_gadget *gadget = dev->gadget;
932
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200933#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300934 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200935 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400936 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200937 &fs_status_desc);
938 dev->status_ep->driver_data = dev;
939
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400940 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200941 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400942 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200943 dev->status_ep->name, result);
944 goto done;
945 }
946 }
947#endif
948
949 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
950 dev->in_ep->driver_data = dev;
951
952 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
953 dev->out_ep->driver_data = dev;
954
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400955 /*
956 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200957 * endpoints in the default altsetting for the interface.
958 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300959 *
960 * Strictly speaking RNDIS should work the same: activation is
961 * a side effect of setting a packet filter. Deactivation is
962 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200963 */
964 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400965 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200966 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400967 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200968 dev->in_ep->name, result);
969 goto done;
970 }
971
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400972 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200973 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400974 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200975 dev->out_ep->name, result);
976 goto done;
977 }
978 }
979
980done:
981 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400982 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200983
984 /* on error, disable any endpoints */
985 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400986 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400987 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200988 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400989 (void) usb_ep_disable(dev->in_ep);
990 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200991 dev->in = NULL;
992 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300993 } else if (!cdc_active(dev)) {
994 /*
995 * activate non-CDC configs right away
996 * this isn't strictly according to the RNDIS spec
997 */
998 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +0200999 }
1000
1001 /* caller is responsible for cleanup on error */
1002 return result;
1003}
1004
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001005static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001006{
1007 if (dev->config == 0)
1008 return;
1009
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001010 debug("%s\n", __func__);
1011
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001012 rndis_uninit(dev->rndis_config);
1013
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001014 /*
1015 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001016 * pending i/o. then free the requests.
1017 */
1018
1019 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001020 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001021 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001022 usb_ep_free_request(dev->in_ep, dev->tx_req);
1023 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001024 }
1025 }
1026 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001027 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001028 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001029 usb_ep_free_request(dev->out_ep, dev->rx_req);
1030 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001031 }
1032 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001033 if (dev->status)
1034 usb_ep_disable(dev->status_ep);
1035
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001036 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001037 dev->cdc_filter = 0;
1038 dev->config = 0;
1039}
1040
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001041/*
1042 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001043 * that returns config descriptors, and altsetting code.
1044 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001045static int eth_set_config(struct eth_dev *dev, unsigned number,
1046 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001047{
1048 int result = 0;
1049 struct usb_gadget *gadget = dev->gadget;
1050
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001051 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001052 && dev->config
1053 && dev->tx_qlen != 0) {
1054 /* tx fifo is full, but we can't clear it...*/
Masahiro Yamada81e10422017-09-16 14:10:41 +09001055 pr_err("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001056 return -ESPIPE;
1057 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001058 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001059
1060 switch (number) {
1061 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001062 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001063 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001064#ifdef CONFIG_USB_ETH_RNDIS
1065 case DEV_RNDIS_CONFIG_VALUE:
1066 dev->rndis = 1;
1067 result = set_ether_config(dev, gfp_flags);
1068 break;
1069#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001070 default:
1071 result = -EINVAL;
1072 /* FALL THROUGH */
1073 case 0:
1074 break;
1075 }
1076
1077 if (result) {
1078 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001079 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001080 usb_gadget_vbus_draw(dev->gadget,
1081 gadget_is_otg(dev->gadget) ? 8 : 100);
1082 } else {
1083 char *speed;
1084 unsigned power;
1085
1086 power = 2 * eth_config.bMaxPower;
1087 usb_gadget_vbus_draw(dev->gadget, power);
1088
1089 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001090 case USB_SPEED_FULL:
1091 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001092#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001093 case USB_SPEED_HIGH:
1094 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001095#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001096 default:
1097 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001098 }
1099
1100 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001101 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001102 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001103 rndis_active(dev)
1104 ? "RNDIS"
1105 : (cdc_active(dev)
1106 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001107 : "CDC Ethernet Subset"));
1108 }
1109 return result;
1110}
1111
1112/*-------------------------------------------------------------------------*/
1113
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001114#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001115
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001116/*
1117 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001118 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001119 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001120 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1121 * command mechanism; and only one status request is ever queued.
1122 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001123static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001124{
1125 struct usb_cdc_notification *event = req->buf;
1126 int value = req->status;
1127 struct eth_dev *dev = ep->driver_data;
1128
1129 /* issue the second notification if host reads the first */
1130 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1131 && value == 0) {
1132 __le32 *data = req->buf + sizeof *event;
1133
1134 event->bmRequestType = 0xA1;
1135 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001136 event->wValue = __constant_cpu_to_le16(0);
1137 event->wIndex = __constant_cpu_to_le16(1);
1138 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001139
1140 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001141 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001142
1143 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001144 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001145 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001146 if (value == 0)
1147 return;
1148 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001149 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001150 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001151 if (event->bNotificationType ==
1152 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301153 dev->network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001154 printf("USB network up!\n");
1155 }
1156 }
1157 req->context = NULL;
1158}
1159
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001160static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001161{
1162 struct usb_request *req = dev->stat_req;
1163 struct usb_cdc_notification *event;
1164 int value;
1165
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001166 /*
1167 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001168 *
1169 * FIXME ugly idiom, maybe we'd be better with just
1170 * a "cancel the whole queue" primitive since any
1171 * unlink-one primitive has way too many error modes.
1172 * here, we "know" toggle is already clear...
1173 *
1174 * FIXME iff req->context != null just dequeue it
1175 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001176 usb_ep_disable(dev->status_ep);
1177 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001178
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001179 /*
1180 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001181 * a SPEED_CHANGE. could be useful in some configs.
1182 */
1183 event = req->buf;
1184 event->bmRequestType = 0xA1;
1185 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001186 event->wValue = __constant_cpu_to_le16(1); /* connected */
1187 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001188 event->wLength = 0;
1189
1190 req->length = sizeof *event;
1191 req->complete = eth_status_complete;
1192 req->context = dev;
1193
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001194 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001195 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001196 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001197}
1198
1199#endif
1200
1201/*-------------------------------------------------------------------------*/
1202
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001203static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001204{
1205 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001206 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001207 req->status, req->actual, req->length);
1208}
1209
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001210#ifdef CONFIG_USB_ETH_RNDIS
1211
1212static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1213{
1214 if (req->status || req->actual != req->length)
1215 debug("rndis response complete --> %d, %d/%d\n",
1216 req->status, req->actual, req->length);
1217
1218 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1219}
1220
1221static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1222{
1223 struct eth_dev *dev = ep->driver_data;
1224 int status;
1225
1226 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1227 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1228 if (status < 0)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001229 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001230}
1231
1232#endif /* RNDIS */
1233
Remy Bohmerdf063442009-07-29 18:18:43 +02001234/*
1235 * The setup() callback implements all the ep0 functionality that's not
1236 * handled lower down. CDC has a number of less-common features:
1237 *
1238 * - two interfaces: control, and ethernet data
1239 * - Ethernet data interface has two altsettings: default, and active
1240 * - class-specific descriptors for the control interface
1241 * - class-specific control requests
1242 */
1243static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001244eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001245{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001246 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001247 struct usb_request *req = dev->req;
1248 int value = -EOPNOTSUPP;
1249 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1250 u16 wValue = le16_to_cpu(ctrl->wValue);
1251 u16 wLength = le16_to_cpu(ctrl->wLength);
1252
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001253 /*
1254 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001255 * while config change events may enable network traffic.
1256 */
1257
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001258 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001259
1260 req->complete = eth_setup_complete;
1261 switch (ctrl->bRequest) {
1262
1263 case USB_REQ_GET_DESCRIPTOR:
1264 if (ctrl->bRequestType != USB_DIR_IN)
1265 break;
1266 switch (wValue >> 8) {
1267
1268 case USB_DT_DEVICE:
Kishon Vijay Abraham I58d701e2015-02-23 18:40:17 +05301269 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001270 value = min(wLength, (u16) sizeof device_desc);
1271 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001272 break;
1273 case USB_DT_DEVICE_QUALIFIER:
1274 if (!gadget_is_dualspeed(gadget))
1275 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001276 value = min(wLength, (u16) sizeof dev_qualifier);
1277 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001278 break;
1279
1280 case USB_DT_OTHER_SPEED_CONFIG:
1281 if (!gadget_is_dualspeed(gadget))
1282 break;
1283 /* FALLTHROUGH */
1284 case USB_DT_CONFIG:
1285 value = config_buf(gadget, req->buf,
1286 wValue >> 8,
1287 wValue & 0xff,
1288 gadget_is_otg(gadget));
1289 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001290 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001291 break;
1292
1293 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001294 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001295 wValue & 0xff, req->buf);
1296
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
1300 break;
1301 }
1302 break;
1303
1304 case USB_REQ_SET_CONFIGURATION:
1305 if (ctrl->bRequestType != 0)
1306 break;
1307 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001308 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001309 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001310 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001311 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001312 break;
1313 case USB_REQ_GET_CONFIGURATION:
1314 if (ctrl->bRequestType != USB_DIR_IN)
1315 break;
1316 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001317 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001318 break;
1319
1320 case USB_REQ_SET_INTERFACE:
1321 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1322 || !dev->config
1323 || wIndex > 1)
1324 break;
1325 if (!cdc_active(dev) && wIndex != 0)
1326 break;
1327
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001328 /*
1329 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001330 * we need to kluge around that interference.
1331 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001332 if (gadget_is_pxa(gadget)) {
1333 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001334 GFP_ATOMIC);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001335 /*
1336 * PXA25x driver use non-CDC ethernet gadget.
1337 * But only _CDC and _RNDIS code can signalize
1338 * that network is working. So we signalize it
1339 * here.
1340 */
Mugunthan V N3963de32016-11-18 10:49:13 +05301341 dev->network_started = 1;
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001342 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001343 goto done_set_intf;
1344 }
1345
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001346#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001347 switch (wIndex) {
1348 case 0: /* control/master intf */
1349 if (wValue != 0)
1350 break;
1351 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001352 usb_ep_disable(dev->status_ep);
1353 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001354 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001355
Remy Bohmerdf063442009-07-29 18:18:43 +02001356 value = 0;
1357 break;
1358 case 1: /* data intf */
1359 if (wValue > 1)
1360 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001361 usb_ep_disable(dev->in_ep);
1362 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001363
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001364 /*
1365 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001366 * the default interface setting ... also, setting
1367 * the non-default interface resets filters etc.
1368 */
1369 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001370 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001371 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001372 usb_ep_enable(dev->in_ep, dev->in);
1373 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001374 dev->cdc_filter = DEFAULT_FILTER;
1375 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001376 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001377 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001378 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001379 value = 0;
1380 break;
1381 }
1382#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001383 /*
1384 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001385 * all non-PXA hardware talks real CDC ...
1386 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001387 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001388#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001389
1390done_set_intf:
1391 break;
1392 case USB_REQ_GET_INTERFACE:
1393 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1394 || !dev->config
1395 || wIndex > 1)
1396 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001397 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001398 break;
1399
1400 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001401 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001402 *(u8 *)req->buf = 0;
1403 else {
1404 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1405 /* carrier always ok ...*/
1406 *(u8 *)req->buf = 1 ;
1407 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001408 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001409 break;
1410
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001411#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001412 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001413 /*
1414 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001415 * wValue = packet filter bitmap
1416 */
1417 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1418 || !cdc_active(dev)
1419 || wLength != 0
1420 || wIndex > 1)
1421 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001422 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001423 dev->cdc_filter = wValue;
1424 value = 0;
1425 break;
1426
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001427 /*
1428 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001429 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1430 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1431 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1432 * case USB_CDC_GET_ETHERNET_STATISTIC:
1433 */
1434
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001435#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001436
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001437#ifdef CONFIG_USB_ETH_RNDIS
1438 /*
1439 * RNDIS uses the CDC command encapsulation mechanism to implement
1440 * an RPC scheme, with much getting/setting of attributes by OID.
1441 */
1442 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1443 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1444 || !rndis_active(dev)
1445 || wLength > USB_BUFSIZ
1446 || wValue
1447 || rndis_control_intf.bInterfaceNumber
1448 != wIndex)
1449 break;
1450 /* read the request, then process it */
1451 value = wLength;
1452 req->complete = rndis_command_complete;
1453 /* later, rndis_control_ack () sends a notification */
1454 break;
1455
1456 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1457 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1458 == ctrl->bRequestType
1459 && rndis_active(dev)
1460 /* && wLength >= 0x0400 */
1461 && !wValue
1462 && rndis_control_intf.bInterfaceNumber
1463 == wIndex) {
1464 u8 *buf;
1465 u32 n;
1466
1467 /* return the result */
1468 buf = rndis_get_next_response(dev->rndis_config, &n);
1469 if (buf) {
1470 memcpy(req->buf, buf, n);
1471 req->complete = rndis_response_complete;
1472 rndis_free_response(dev->rndis_config, buf);
1473 value = n;
1474 }
1475 /* else stalls ... spec says to avoid that */
1476 }
1477 break;
1478#endif /* RNDIS */
1479
Remy Bohmerdf063442009-07-29 18:18:43 +02001480 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001481 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001482 ctrl->bRequestType, ctrl->bRequest,
1483 wValue, wIndex, wLength);
1484 }
1485
1486 /* respond with data transfer before status phase? */
1487 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001488 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001489 req->length = value;
1490 req->zero = value < wLength
1491 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001492 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001493 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001494 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001495 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001496 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001497 }
1498 }
1499
1500 /* host either stalls (value < 0) or reports success */
1501 return value;
1502}
1503
Remy Bohmerdf063442009-07-29 18:18:43 +02001504/*-------------------------------------------------------------------------*/
1505
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001506static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001507
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001508static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001509 gfp_t gfp_flags)
1510{
1511 int retval = -ENOMEM;
1512 size_t size;
1513
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001514 /*
1515 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001516 * Normally we use the USB "terminate on short read" convention;
1517 * so allow up to (N*maxpacket), since that memory is normally
1518 * already allocated. Some hardware doesn't deal well with short
1519 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1520 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001521 *
1522 * RNDIS uses internal framing, and explicitly allows senders to
1523 * pad to end-of-packet. That's potentially nice for speed,
1524 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001525 */
1526
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001527 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001528 if (!req)
1529 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001530
1531 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1532 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001533 if (rndis_active(dev))
1534 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001535 size -= size % dev->out_ep->maxpacket;
1536
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001537 /*
1538 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001539 * but on at least one, checksumming fails otherwise. Note:
1540 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001541 */
1542
Joe Hershberger9f09a362015-04-08 01:41:06 -05001543 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmerdf063442009-07-29 18:18:43 +02001544 req->length = size;
1545 req->complete = rx_complete;
1546
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001547 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001548
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001549 if (retval)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001550 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001551
Remy Bohmerdf063442009-07-29 18:18:43 +02001552 return retval;
1553}
1554
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001555static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001556{
1557 struct eth_dev *dev = ep->driver_data;
1558
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001559 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001560 switch (req->status) {
1561 /* normal completion */
1562 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001563 if (rndis_active(dev)) {
1564 /* we know MaxPacketsPerTransfer == 1 here */
1565 int length = rndis_rm_hdr(req->buf, req->actual);
1566 if (length < 0)
1567 goto length_err;
1568 req->length -= length;
1569 req->actual -= length;
1570 }
Bin Meng2ccb8b02018-07-31 02:55:22 -07001571 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001572length_err:
1573 dev->stats.rx_errors++;
1574 dev->stats.rx_length_errors++;
1575 debug("rx length %d\n", req->length);
1576 break;
1577 }
1578
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001579 dev->stats.rx_packets++;
1580 dev->stats.rx_bytes += req->length;
1581 break;
1582
1583 /* software-driven interface shutdown */
1584 case -ECONNRESET: /* unlink */
1585 case -ESHUTDOWN: /* disconnect etc */
1586 /* for hardware automagic (such as pxa) */
1587 case -ECONNABORTED: /* endpoint reset */
1588 break;
1589
1590 /* data overrun */
1591 case -EOVERFLOW:
1592 dev->stats.rx_over_errors++;
1593 /* FALLTHROUGH */
1594 default:
1595 dev->stats.rx_errors++;
1596 break;
1597 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001598
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001599 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001600}
1601
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001602static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001603{
1604
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001605 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001606
1607 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001608 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001609
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001610 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001611
1612 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001613 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001614
1615 return 0;
1616
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001617fail2:
1618 usb_ep_free_request(dev->in_ep, dev->tx_req);
1619fail1:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001620 pr_err("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001621 return -1;
1622}
1623
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001624static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001625{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001626 struct eth_dev *dev = ep->driver_data;
1627
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001628 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001629 switch (req->status) {
1630 default:
1631 dev->stats.tx_errors++;
1632 debug("tx err %d\n", req->status);
1633 /* FALLTHROUGH */
1634 case -ECONNRESET: /* unlink */
1635 case -ESHUTDOWN: /* disconnect etc */
1636 break;
1637 case 0:
1638 dev->stats.tx_bytes += req->length;
1639 }
1640 dev->stats.tx_packets++;
1641
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001642 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001643}
1644
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001645static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001646{
1647 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001648 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001649 return 1;
1650 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1651}
1652
1653#if 0
1654static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1655{
1656 struct eth_dev *dev = netdev_priv(net);
1657 int length = skb->len;
1658 int retval;
1659 struct usb_request *req = NULL;
1660 unsigned long flags;
1661
1662 /* apply outgoing CDC or RNDIS filters */
1663 if (!eth_is_promisc (dev)) {
1664 u8 *dest = skb->data;
1665
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001666 if (is_multicast_ethaddr(dest)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001667 u16 type;
1668
1669 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1670 * SET_ETHERNET_MULTICAST_FILTERS requests
1671 */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001672 if (is_broadcast_ethaddr(dest))
Remy Bohmerdf063442009-07-29 18:18:43 +02001673 type = USB_CDC_PACKET_TYPE_BROADCAST;
1674 else
1675 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1676 if (!(dev->cdc_filter & type)) {
1677 dev_kfree_skb_any (skb);
1678 return 0;
1679 }
1680 }
1681 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1682 }
1683
1684 spin_lock_irqsave(&dev->req_lock, flags);
1685 /*
1686 * this freelist can be empty if an interrupt triggered disconnect()
1687 * and reconfigured the gadget (shutting down this queue) after the
1688 * network stack decided to xmit but before we got the spinlock.
1689 */
1690 if (list_empty(&dev->tx_reqs)) {
1691 spin_unlock_irqrestore(&dev->req_lock, flags);
1692 return 1;
1693 }
1694
1695 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1696 list_del (&req->list);
1697
1698 /* temporarily stop TX queue when the freelist empties */
1699 if (list_empty (&dev->tx_reqs))
1700 netif_stop_queue (net);
1701 spin_unlock_irqrestore(&dev->req_lock, flags);
1702
1703 /* no buffer copies needed, unless the network stack did it
1704 * or the hardware can't use skb buffers.
1705 * or there's not enough space for any RNDIS headers we need
1706 */
1707 if (rndis_active(dev)) {
1708 struct sk_buff *skb_rndis;
1709
1710 skb_rndis = skb_realloc_headroom (skb,
1711 sizeof (struct rndis_packet_msg_type));
1712 if (!skb_rndis)
1713 goto drop;
1714
1715 dev_kfree_skb_any (skb);
1716 skb = skb_rndis;
1717 rndis_add_hdr (skb);
1718 length = skb->len;
1719 }
1720 req->buf = skb->data;
1721 req->context = skb;
1722 req->complete = tx_complete;
1723
1724 /* use zlp framing on tx for strict CDC-Ether conformance,
1725 * though any robust network rx path ignores extra padding.
1726 * and some hardware doesn't like to write zlps.
1727 */
1728 req->zero = 1;
1729 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1730 length++;
1731
1732 req->length = length;
1733
1734 /* throttle highspeed IRQ rate back slightly */
1735 if (gadget_is_dualspeed(dev->gadget))
1736 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1737 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1738 : 0;
1739
1740 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1741 switch (retval) {
1742 default:
1743 DEBUG (dev, "tx queue err %d\n", retval);
1744 break;
1745 case 0:
1746 net->trans_start = jiffies;
1747 atomic_inc (&dev->tx_qlen);
1748 }
1749
1750 if (retval) {
1751drop:
1752 dev->stats.tx_dropped++;
1753 dev_kfree_skb_any (skb);
1754 spin_lock_irqsave(&dev->req_lock, flags);
1755 if (list_empty (&dev->tx_reqs))
1756 netif_start_queue (net);
1757 list_add (&req->list, &dev->tx_reqs);
1758 spin_unlock_irqrestore(&dev->req_lock, flags);
1759 }
1760 return 0;
1761}
1762
1763/*-------------------------------------------------------------------------*/
1764#endif
1765
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001766static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001767{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001768 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001769
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001770 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001771 rndis_deregister(dev->rndis_config);
1772 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001773
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001774 /* we've already been disconnected ... no i/o is active */
1775 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001776 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001777 dev->req = NULL;
1778 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001779 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001780 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001781 dev->stat_req = NULL;
1782 }
1783
1784 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001785 usb_ep_free_request(dev->in_ep, dev->tx_req);
1786 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001787 }
1788
1789 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001790 usb_ep_free_request(dev->out_ep, dev->rx_req);
1791 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001792 }
1793
1794/* unregister_netdev (dev->net);*/
1795/* free_netdev(dev->net);*/
1796
Lei Wen4af32732010-12-01 23:43:43 +08001797 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001798 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001799}
1800
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001801static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001802{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001803 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001804 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001805}
1806
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001807static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001808{
1809 /* Not used */
1810}
1811
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001812static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001813{
1814 /* Not used */
1815}
1816
1817/*-------------------------------------------------------------------------*/
1818
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001819#ifdef CONFIG_USB_ETH_RNDIS
1820
1821/*
1822 * The interrupt endpoint is used in RNDIS to notify the host when messages
1823 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1824 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1825 * REMOTE_NDIS_KEEPALIVE_MSG.
1826 *
1827 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1828 * normally just one notification will be queued.
1829 */
1830
1831static void rndis_control_ack_complete(struct usb_ep *ep,
1832 struct usb_request *req)
1833{
1834 struct eth_dev *dev = ep->driver_data;
1835
1836 debug("%s...\n", __func__);
1837 if (req->status || req->actual != req->length)
1838 debug("rndis control ack complete --> %d, %d/%d\n",
1839 req->status, req->actual, req->length);
1840
Mugunthan V N3963de32016-11-18 10:49:13 +05301841 if (!dev->network_started) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001842 if (rndis_get_state(dev->rndis_config)
1843 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301844 dev->network_started = 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001845 printf("USB RNDIS network up!\n");
1846 }
1847 }
1848
1849 req->context = NULL;
1850
1851 if (req != dev->stat_req)
1852 usb_ep_free_request(ep, req);
1853}
1854
1855static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1856
Mugunthan V N095b7612016-11-18 11:09:15 +05301857#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001858static int rndis_control_ack(struct eth_device *net)
Mugunthan V N095b7612016-11-18 11:09:15 +05301859#else
1860static int rndis_control_ack(struct udevice *net)
1861#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001862{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05301863 struct ether_priv *priv = (struct ether_priv *)net->priv;
1864 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001865 int length;
1866 struct usb_request *resp = dev->stat_req;
1867
1868 /* in case RNDIS calls this after disconnect */
1869 if (!dev->status) {
1870 debug("status ENODEV\n");
1871 return -ENODEV;
1872 }
1873
1874 /* in case queue length > 1 */
1875 if (resp->context) {
1876 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1877 if (!resp)
1878 return -ENOMEM;
1879 resp->buf = rndis_resp_buf;
1880 }
1881
1882 /*
1883 * Send RNDIS RESPONSE_AVAILABLE notification;
1884 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1885 */
1886 resp->length = 8;
1887 resp->complete = rndis_control_ack_complete;
1888 resp->context = dev;
1889
1890 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1891 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1892
1893 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1894 if (length < 0) {
1895 resp->status = 0;
1896 rndis_control_ack_complete(dev->status_ep, resp);
1897 }
1898
1899 return 0;
1900}
1901
1902#else
1903
1904#define rndis_control_ack NULL
1905
1906#endif /* RNDIS */
1907
1908static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1909{
1910 if (rndis_active(dev)) {
1911 rndis_set_param_medium(dev->rndis_config,
1912 NDIS_MEDIUM_802_3,
1913 BITRATE(dev->gadget)/100);
1914 rndis_signal_connect(dev->rndis_config);
1915 }
1916}
1917
1918static int eth_stop(struct eth_dev *dev)
1919{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001920#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1921 unsigned long ts;
1922 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1923#endif
1924
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001925 if (rndis_active(dev)) {
1926 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1927 rndis_signal_disconnect(dev->rndis_config);
1928
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001929#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1930 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1931 ts = get_timer(0);
1932 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05301933 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001934#endif
1935
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001936 rndis_uninit(dev->rndis_config);
1937 dev->rndis = 0;
1938 }
1939
1940 return 0;
1941}
1942
1943/*-------------------------------------------------------------------------*/
1944
Remy Bohmerdf063442009-07-29 18:18:43 +02001945static int is_eth_addr_valid(char *str)
1946{
1947 if (strlen(str) == 17) {
1948 int i;
1949 char *p, *q;
1950 uchar ea[6];
1951
1952 /* see if it looks like an ethernet address */
1953
1954 p = str;
1955
1956 for (i = 0; i < 6; i++) {
1957 char term = (i == 5 ? '\0' : ':');
1958
1959 ea[i] = simple_strtol(p, &q, 16);
1960
1961 if ((q - p) != 2 || *q++ != term)
1962 break;
1963
1964 p = q;
1965 }
1966
Tom Riniadd21ca2012-10-31 13:30:41 +00001967 /* Now check the contents. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001968 return is_valid_ethaddr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001969 }
1970 return 0;
1971}
1972
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001973static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001974{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001975 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001976 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001977 c = toupper(c);
1978 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001979 return 10 + c - 'A';
1980 return 0;
1981}
1982
1983static int get_ether_addr(const char *str, u8 *dev_addr)
1984{
1985 if (str) {
1986 unsigned i;
1987
1988 for (i = 0; i < 6; i++) {
1989 unsigned char num;
1990
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001991 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001992 str++;
1993 num = nibble(*str++) << 4;
1994 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001995 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02001996 }
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001997 if (is_valid_ethaddr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02001998 return 0;
1999 }
2000 return 1;
2001}
2002
2003static int eth_bind(struct usb_gadget *gadget)
2004{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302005 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002006 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002007 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002008 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002009 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002010 u8 tmp[7];
Mugunthan V N095b7612016-11-18 11:09:15 +05302011#ifdef CONFIG_DM_ETH
Simon Glassfa20e932020-12-03 16:55:20 -07002012 struct eth_pdata *pdata = dev_get_plat(l_priv->netdev);
Mugunthan V N095b7612016-11-18 11:09:15 +05302013#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002014
2015 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002016#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002017 cdc = 0;
2018#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002019#ifndef CONFIG_USB_ETH_RNDIS
2020 rndis = 0;
2021#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002022 /*
2023 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002024 * standard protocol is _strongly_ preferred for interop purposes.
2025 * (By everyone except Microsoft.)
2026 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002027 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002028 /* pxa doesn't support altsettings */
2029 cdc = 0;
2030 } else if (gadget_is_musbhdrc(gadget)) {
2031 /* reduce tx dma overhead by avoiding special cases */
2032 zlp = 0;
2033 } else if (gadget_is_sh(gadget)) {
2034 /* sh doesn't support multiple interfaces or configs */
2035 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002036 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002037 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002038 /* hardware can't write zlps */
2039 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002040 /*
2041 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002042 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2043 */
2044 cdc = 0;
2045 }
2046
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002047 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002048 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002049 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002050 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002051 /*
2052 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002053 * anything less functional on CDC-capable hardware,
2054 * so we fail in this case.
2055 */
Masahiro Yamada81e10422017-09-16 14:10:41 +09002056 pr_err("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002057 gadget->name);
2058 return -ENODEV;
2059 }
2060
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002061 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002062 * If there's an RNDIS configuration, that's what Windows wants to
2063 * be using ... so use these product IDs here and in the "linux.inf"
2064 * needed to install MSFT drivers. Current Linux kernels will use
2065 * the second configuration if it's CDC Ethernet, and need some help
2066 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002067 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002068 if (rndis) {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002069#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Remy Bohmerdf063442009-07-29 18:18:43 +02002070 device_desc.idVendor =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002071 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Remy Bohmerdf063442009-07-29 18:18:43 +02002072 device_desc.idProduct =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002073 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002074#else
2075 device_desc.idVendor =
2076 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2077 device_desc.idProduct =
2078 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2079#endif
2080 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002081
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002082 /*
2083 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2084 * drivers aren't widely available. (That may be improved by
2085 * supporting one submode of the "SAFE" variant of MDLM.)
2086 */
2087 } else {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002088#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2089 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2090 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002091#else
2092 if (!cdc) {
2093 device_desc.idVendor =
2094 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2095 device_desc.idProduct =
2096 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2097 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002098#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002099 }
2100 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002101 if (bcdDevice)
2102 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2103 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002104 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002105 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002106 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002107 if (iSerialNumber) {
2108 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002109 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002110 }
2111
2112 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002113 usb_ep_autoconfig_reset(gadget);
2114 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002115 if (!in_ep) {
2116autoconf_fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002117 pr_err("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002118 gadget->name);
2119 return -ENODEV;
2120 }
2121 in_ep->driver_data = in_ep; /* claim */
2122
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002123 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002124 if (!out_ep)
2125 goto autoconf_fail;
2126 out_ep->driver_data = out_ep; /* claim */
2127
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002128#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002129 /*
2130 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002131 * Since some hosts expect one, try to allocate one anyway.
2132 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002133 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002134 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002135 if (status_ep) {
2136 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002137 } else if (rndis) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002138 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002139 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002140#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002141 } else if (cdc) {
2142 control_intf.bNumEndpoints = 0;
2143 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002144#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002145 }
2146 }
2147#endif
2148
2149 /* one config: cdc, else minimal subset */
2150 if (!cdc) {
2151 eth_config.bNumInterfaces = 1;
2152 eth_config.iConfiguration = STRING_SUBSET;
2153
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002154 /*
2155 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002156 * with multiple controllers and must override CDC Ethernet.
2157 */
2158 fs_subset_descriptors();
2159 hs_subset_descriptors();
2160 }
2161
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002162 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002163
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002164 /* For now RNDIS is always a second config */
2165 if (rndis)
2166 device_desc.bNumConfigurations = 2;
2167
Remy Bohmerdf063442009-07-29 18:18:43 +02002168 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002169 if (rndis)
2170 dev_qualifier.bNumConfigurations = 2;
2171 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002172 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2173
2174 /* assumes ep0 uses the same value for both speeds ... */
2175 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2176
2177 /* and that all endpoints are dual-speed */
2178 hs_source_desc.bEndpointAddress =
2179 fs_source_desc.bEndpointAddress;
2180 hs_sink_desc.bEndpointAddress =
2181 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002182#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002183 if (status_ep)
2184 hs_status_desc.bEndpointAddress =
2185 fs_status_desc.bEndpointAddress;
2186#endif
2187 }
2188
2189 if (gadget_is_otg(gadget)) {
2190 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2191 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2192 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002193#ifdef CONFIG_USB_ETH_RNDIS
2194 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2195 rndis_config.bMaxPower = 4;
2196#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002197 }
2198
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002199
2200 /* network device setup */
Mugunthan V N095b7612016-11-18 11:09:15 +05302201#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +05302202 dev->net = &l_priv->netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +05302203#else
2204 dev->net = l_priv->netdev;
2205#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002206
2207 dev->cdc = cdc;
2208 dev->zlp = zlp;
2209
2210 dev->in_ep = in_ep;
2211 dev->out_ep = out_ep;
2212 dev->status_ep = status_ep;
2213
Mugunthan V N095b7612016-11-18 11:09:15 +05302214 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002215 /*
2216 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002217 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002218 * ends up in a persistent config database. It's not clear if
2219 * host side code for the SAFE thing cares -- its original BLAN
2220 * thing didn't, Sharp never assigned those addresses on Zaurii.
2221 */
Mugunthan V N095b7612016-11-18 11:09:15 +05302222#ifndef CONFIG_DM_ETH
Remy Bohmerdf063442009-07-29 18:18:43 +02002223 get_ether_addr(dev_addr, dev->net->enetaddr);
Remy Bohmerdf063442009-07-29 18:18:43 +02002224 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
Mugunthan V N095b7612016-11-18 11:09:15 +05302225#else
2226 get_ether_addr(dev_addr, pdata->enetaddr);
2227 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2228#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002229
2230 get_ether_addr(host_addr, dev->host_mac);
2231
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002232 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2233 dev->host_mac[0], dev->host_mac[1],
2234 dev->host_mac[2], dev->host_mac[3],
2235 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002236
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002237 if (rndis) {
2238 status = rndis_init();
2239 if (status < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002240 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002241 goto fail;
2242 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002243 }
2244
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002245 /*
2246 * use PKTSIZE (or aligned... from u-boot) and set
2247 * wMaxSegmentSize accordingly
2248 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002249 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2250
2251 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002252 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002253 if (!dev->req)
2254 goto fail;
2255 dev->req->buf = control_req;
2256 dev->req->complete = eth_setup_complete;
2257
2258 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002259#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002260 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002261 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2262 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002263 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002264 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002265
2266 goto fail;
2267 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002268 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002269 dev->stat_req->context = NULL;
2270 }
2271#endif
2272
2273 /* finish hookup to lower layer ... */
2274 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002275 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002276 gadget->ep0->driver_data = dev;
2277
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002278 /*
2279 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002280 * - iff DATA transfer is active, carrier is "on"
2281 * - tx queueing enabled if open *and* carrier is "on"
2282 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002283
2284 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2285 out_ep->name, in_ep->name,
2286 status_ep ? " STATUS " : "",
2287 status_ep ? status_ep->name : ""
2288 );
Mugunthan V N095b7612016-11-18 11:09:15 +05302289#ifndef CONFIG_DM_ETH
2290 printf("MAC %pM\n", dev->net->enetaddr);
2291#else
2292 printf("MAC %pM\n", pdata->enetaddr);
2293#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002294
2295 if (cdc || rndis)
2296 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2297 dev->host_mac[0], dev->host_mac[1],
2298 dev->host_mac[2], dev->host_mac[3],
2299 dev->host_mac[4], dev->host_mac[5]);
2300
2301 if (rndis) {
2302 u32 vendorID = 0;
2303
2304 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2305
2306 dev->rndis_config = rndis_register(rndis_control_ack);
2307 if (dev->rndis_config < 0) {
2308fail0:
2309 eth_unbind(gadget);
2310 debug("RNDIS setup failed\n");
2311 status = -ENODEV;
2312 goto fail;
2313 }
2314
2315 /* these set up a lot of the OIDs that RNDIS needs */
2316 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2317 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2318 &dev->stats, &dev->cdc_filter))
2319 goto fail0;
2320 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2321 manufacturer))
2322 goto fail0;
2323 if (rndis_set_param_medium(dev->rndis_config,
2324 NDIS_MEDIUM_802_3, 0))
2325 goto fail0;
2326 printf("RNDIS ready\n");
2327 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002328 return 0;
2329
2330fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002331 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002332 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002333 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002334}
2335
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002336/*-------------------------------------------------------------------------*/
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002337static void _usb_eth_halt(struct ether_priv *priv);
2338
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302339static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002340{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302341 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002342 struct usb_gadget *gadget;
2343 unsigned long ts;
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002344 int ret;
Remy Bohmerdf063442009-07-29 18:18:43 +02002345 unsigned long timeout = USB_CONNECT_TIMEOUT;
2346
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002347 ret = usb_gadget_initialize(0);
2348 if (ret)
2349 return ret;
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302350
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002351 /* Configure default mac-addresses for the USB ethernet device */
2352#ifdef CONFIG_USBNET_DEV_ADDR
2353 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2354#endif
2355#ifdef CONFIG_USBNET_HOST_ADDR
2356 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2357#endif
2358 /* Check if the user overruled the MAC addresses */
Simon Glass64b723f2017-08-03 12:22:12 -06002359 if (env_get("usbnet_devaddr"))
2360 strlcpy(dev_addr, env_get("usbnet_devaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002361 sizeof(dev_addr));
2362
Simon Glass64b723f2017-08-03 12:22:12 -06002363 if (env_get("usbnet_hostaddr"))
2364 strlcpy(host_addr, env_get("usbnet_hostaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002365 sizeof(host_addr));
2366
2367 if (!is_eth_addr_valid(dev_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002368 pr_err("Need valid 'usbnet_devaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002369 goto fail;
2370 }
2371 if (!is_eth_addr_valid(host_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002372 pr_err("Need valid 'usbnet_hostaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002373 goto fail;
2374 }
2375
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302376 priv->eth_driver.speed = DEVSPEED;
2377 priv->eth_driver.bind = eth_bind;
2378 priv->eth_driver.unbind = eth_unbind;
2379 priv->eth_driver.setup = eth_setup;
2380 priv->eth_driver.reset = eth_disconnect;
2381 priv->eth_driver.disconnect = eth_disconnect;
2382 priv->eth_driver.suspend = eth_suspend;
2383 priv->eth_driver.resume = eth_resume;
2384 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen4af32732010-12-01 23:43:43 +08002385 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002386
2387 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002388
2389 packet_received = 0;
2390 packet_sent = 0;
2391
2392 gadget = dev->gadget;
2393 usb_gadget_connect(gadget);
2394
Simon Glass64b723f2017-08-03 12:22:12 -06002395 if (env_get("cdc_connect_timeout"))
2396 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
Remy Bohmerdf063442009-07-29 18:18:43 +02002397 NULL, 10) * CONFIG_SYS_HZ;
2398 ts = get_timer(0);
Mugunthan V N3963de32016-11-18 10:49:13 +05302399 while (!dev->network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002400 /* Handle control-c and timeouts */
2401 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002402 pr_err("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002403 goto fail;
2404 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302405 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002406 }
2407
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002408 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002409 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002410 return 0;
2411fail:
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002412 _usb_eth_halt(priv);
Remy Bohmerdf063442009-07-29 18:18:43 +02002413 return -1;
2414}
2415
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302416static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002417{
2418 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002419 void *rndis_pkt = NULL;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302420 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002421 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002422 unsigned long ts;
2423 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002424
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002425 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002426
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002427 /* new buffer is needed to include RNDIS header */
2428 if (rndis_active(dev)) {
2429 rndis_pkt = malloc(length +
2430 sizeof(struct rndis_packet_msg_type));
2431 if (!rndis_pkt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002432 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002433 goto drop;
2434 }
2435 rndis_add_hdr(rndis_pkt, length);
2436 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002437 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002438 packet = rndis_pkt;
2439 length += sizeof(struct rndis_packet_msg_type);
2440 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002441 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002442 req->context = NULL;
2443 req->complete = tx_complete;
2444
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002445 /*
2446 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002447 * though any robust network rx path ignores extra padding.
2448 * and some hardware doesn't like to write zlps.
2449 */
2450 req->zero = 1;
2451 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2452 length++;
2453
2454 req->length = length;
2455#if 0
2456 /* throttle highspeed IRQ rate back slightly */
2457 if (gadget_is_dualspeed(dev->gadget))
2458 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2459 ? ((dev->tx_qlen % qmult) != 0) : 0;
2460#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002461 dev->tx_qlen = 1;
2462 ts = get_timer(0);
2463 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002464
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002465 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002466
2467 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002468 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002469 while (!packet_sent) {
2470 if (get_timer(ts) > timeout) {
2471 printf("timeout sending packets to usb ethernet\n");
2472 return -1;
2473 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302474 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002475 }
Heinrich Schuchardt637ad632020-04-19 12:11:12 +02002476 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002477
2478 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002479drop:
2480 dev->stats.tx_dropped++;
2481 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002482}
2483
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302484static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002485{
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302486 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002487
Remy Bohmerdf063442009-07-29 18:18:43 +02002488 return 0;
2489}
2490
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002491static void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002492{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302493 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002494
Lei Wen4af32732010-12-01 23:43:43 +08002495 /* If the gadget not registered, simple return */
2496 if (!dev->gadget)
2497 return;
2498
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002499 /*
2500 * Some USB controllers may need additional deinitialization here
2501 * before dropping pull-up (also due to hardware issues).
2502 * For example: unhandled interrupt with status stage started may
2503 * bring the controller to fully broken state (until board reset).
2504 * There are some variants to debug and fix such cases:
2505 * 1) In the case of RNDIS connection eth_stop can perform additional
2506 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2507 * 2) 'pullup' callback in your UDC driver can be improved to perform
2508 * this deinitialization.
2509 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002510 eth_stop(dev);
2511
Remy Bohmerdf063442009-07-29 18:18:43 +02002512 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002513
2514 /* Clear pending interrupt */
2515 if (dev->network_started) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302516 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002517 dev->network_started = 0;
2518 }
2519
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302520 usb_gadget_unregister_driver(&priv->eth_driver);
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002521 usb_gadget_release(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002522}
2523
Mugunthan V N095b7612016-11-18 11:09:15 +05302524#ifndef CONFIG_DM_ETH
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09002525static int usb_eth_init(struct eth_device *netdev, struct bd_info *bd)
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302526{
2527 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2528
2529 return _usb_eth_init(priv);
2530}
2531
2532static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2533{
2534 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2535
2536 return _usb_eth_send(priv, packet, length);
2537}
2538
2539static int usb_eth_recv(struct eth_device *netdev)
2540{
2541 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2542 struct eth_dev *dev = &priv->ethdev;
2543 int ret;
2544
2545 ret = _usb_eth_recv(priv);
2546 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002547 pr_err("error packet receive\n");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302548 return ret;
2549 }
2550
2551 if (!packet_received)
2552 return 0;
2553
2554 if (dev->rx_req) {
2555 net_process_received_packet(net_rx_packets[0],
2556 dev->rx_req->length);
2557 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002558 pr_err("dev->rx_req invalid");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302559 }
2560 packet_received = 0;
2561 rx_submit(dev, dev->rx_req, 0);
2562
2563 return 0;
2564}
2565
2566void usb_eth_halt(struct eth_device *netdev)
2567{
2568 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2569
2570 _usb_eth_halt(priv);
2571}
2572
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09002573int usb_eth_initialize(struct bd_info *bi)
Remy Bohmerdf063442009-07-29 18:18:43 +02002574{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302575 struct eth_device *netdev = &l_priv->netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002576
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002577 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002578
2579 netdev->init = usb_eth_init;
2580 netdev->send = usb_eth_send;
2581 netdev->recv = usb_eth_recv;
2582 netdev->halt = usb_eth_halt;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302583 netdev->priv = l_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +02002584
Remy Bohmerdf063442009-07-29 18:18:43 +02002585 eth_register(netdev);
2586 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002587}
Mugunthan V N095b7612016-11-18 11:09:15 +05302588#else
2589static int usb_eth_start(struct udevice *dev)
2590{
2591 struct ether_priv *priv = dev_get_priv(dev);
2592
2593 return _usb_eth_init(priv);
2594}
2595
2596static int usb_eth_send(struct udevice *dev, void *packet, int length)
2597{
2598 struct ether_priv *priv = dev_get_priv(dev);
2599
2600 return _usb_eth_send(priv, packet, length);
2601}
2602
2603static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2604{
2605 struct ether_priv *priv = dev_get_priv(dev);
2606 struct eth_dev *ethdev = &priv->ethdev;
2607 int ret;
2608
2609 ret = _usb_eth_recv(priv);
2610 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002611 pr_err("error packet receive\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302612 return ret;
2613 }
2614
2615 if (packet_received) {
2616 if (ethdev->rx_req) {
2617 *packetp = (uchar *)net_rx_packets[0];
2618 return ethdev->rx_req->length;
2619 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002620 pr_err("dev->rx_req invalid");
Mugunthan V N095b7612016-11-18 11:09:15 +05302621 return -EFAULT;
2622 }
2623 }
2624
2625 return -EAGAIN;
2626}
2627
2628static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2629 int length)
2630{
2631 struct ether_priv *priv = dev_get_priv(dev);
2632 struct eth_dev *ethdev = &priv->ethdev;
2633
2634 packet_received = 0;
2635
2636 return rx_submit(ethdev, ethdev->rx_req, 0);
2637}
2638
2639static void usb_eth_stop(struct udevice *dev)
2640{
2641 struct ether_priv *priv = dev_get_priv(dev);
2642
2643 _usb_eth_halt(priv);
2644}
2645
2646static int usb_eth_probe(struct udevice *dev)
2647{
2648 struct ether_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07002649 struct eth_pdata *pdata = dev_get_plat(dev);
Mugunthan V N095b7612016-11-18 11:09:15 +05302650
2651 priv->netdev = dev;
2652 l_priv = priv;
2653
2654 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
Simon Glass8551d552017-08-03 12:22:11 -06002655 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
Mugunthan V N095b7612016-11-18 11:09:15 +05302656
2657 return 0;
2658}
2659
2660static const struct eth_ops usb_eth_ops = {
2661 .start = usb_eth_start,
2662 .send = usb_eth_send,
2663 .recv = usb_eth_recv,
2664 .free_pkt = usb_eth_free_pkt,
2665 .stop = usb_eth_stop,
2666};
2667
2668int usb_ether_init(void)
2669{
2670 struct udevice *dev;
2671 struct udevice *usb_dev;
2672 int ret;
2673
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +01002674 ret = uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
Mugunthan V N095b7612016-11-18 11:09:15 +05302675 if (!usb_dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002676 pr_err("No USB device found\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302677 return ret;
2678 }
2679
2680 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2681 if (!dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002682 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302683 return ret;
2684 }
2685
2686 return 0;
2687}
2688
2689U_BOOT_DRIVER(eth_usb) = {
2690 .name = "usb_ether",
2691 .id = UCLASS_ETH,
2692 .probe = usb_eth_probe,
2693 .ops = &usb_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07002694 .priv_auto = sizeof(struct ether_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07002695 .plat_auto = sizeof(struct eth_pdata),
Mugunthan V N095b7612016-11-18 11:09:15 +05302696 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2697};
2698#endif /* CONFIG_DM_ETH */