blob: 85c971e4c43e7015304f2563005a38e895401f3c [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 struct udevice *net;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300113 struct net_device_stats stats;
114 unsigned int tx_qlen;
115
116 unsigned zlp:1;
117 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300118 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300119 unsigned suspended:1;
120 unsigned network_started:1;
121 u16 cdc_filter;
122 unsigned long todo;
123 int mtu;
124#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300125 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300126 u8 host_mac[ETH_ALEN];
127};
128
129/*
130 * This version autoconfigures as much as possible at run-time.
131 *
132 * It also ASSUMES a self-powered device, without remote wakeup,
133 * although remote wakeup support would make sense.
134 */
135
136/*-------------------------------------------------------------------------*/
Mugunthan V N88e48be2016-11-18 11:06:13 +0530137struct ether_priv {
138 struct eth_dev ethdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530139 struct udevice *netdev;
Mugunthan V N88e48be2016-11-18 11:06:13 +0530140 struct usb_gadget_driver eth_driver;
141};
142
143struct ether_priv eth_priv;
144struct ether_priv *l_priv = &eth_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +0200145
146/*-------------------------------------------------------------------------*/
147
148/* "main" config is either CDC, or its simple subset */
149static inline int is_cdc(struct eth_dev *dev)
150{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200151#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200152 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200153#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200154 return 0; /* only subset possible */
155#else
156 return dev->cdc; /* depends on what hardware we found */
157#endif
158}
159
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300160/* "secondary" RNDIS config may sometimes be activated */
161static inline int rndis_active(struct eth_dev *dev)
162{
163#ifdef CONFIG_USB_ETH_RNDIS
164 return dev->rndis;
165#else
166 return 0;
167#endif
168}
169
170#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
171#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200172
173#define DEFAULT_QLEN 2 /* double buffering by default */
174
175/* peak bulk transfer bits-per-second */
176#define HS_BPS (13 * 512 * 8 * 1000 * 8)
177#define FS_BPS (19 * 64 * 1 * 1000 * 8)
178
179#ifdef CONFIG_USB_GADGET_DUALSPEED
180#define DEVSPEED USB_SPEED_HIGH
181
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400182#ifdef CONFIG_USB_ETH_QMULT
183#define qmult CONFIG_USB_ETH_QMULT
184#else
185#define qmult 5
186#endif
187
Remy Bohmerdf063442009-07-29 18:18:43 +0200188/* for dual-speed hardware, use deeper queues at highspeed */
189#define qlen(gadget) \
190 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
191
192static inline int BITRATE(struct usb_gadget *g)
193{
194 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
195}
196
197#else /* full speed (low speed doesn't do bulk) */
198
199#define qmult 1
200
201#define DEVSPEED USB_SPEED_FULL
202
203#define qlen(gadget) DEFAULT_QLEN
204
205static inline int BITRATE(struct usb_gadget *g)
206{
207 return FS_BPS;
208}
209#endif
210
Remy Bohmerdf063442009-07-29 18:18:43 +0200211/*-------------------------------------------------------------------------*/
212
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400213/*
214 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200215 * Instead: allocate your own, using normal USB-IF procedures.
216 */
217
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400218/*
219 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200220 * It's for devices with only CDC Ethernet configurations.
221 */
222#define CDC_VENDOR_NUM 0x0525 /* NetChip */
223#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
224
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400225/*
226 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200227 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
228 * with pxa250. We're protocol-compatible, if the host-side drivers
229 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
230 * drivers that need to hard-wire endpoint numbers have a hook.
231 *
232 * The protocol is a minimal subset of CDC Ether, which works on any bulk
233 * hardware that's not deeply broken ... even on hardware that can't talk
234 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
235 * doesn't handle control-OUT).
236 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300237#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
238#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200239
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400240/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300241 * For hardware that can talk RNDIS and either of the above protocols,
242 * use this ID ... the windows INF files will know it. Unless it's
243 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
244 * the non-RNDIS configuration.
245 */
246#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
247#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
248
249/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400250 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200251 * device descriptor, either numbers or strings or both. These string
252 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
253 */
254
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300255/*
256 * Emulating them in eth_bind:
257 * static ushort idVendor;
258 * static ushort idProduct;
259 */
260
Maxime Ripard48e78f82017-09-07 09:15:08 +0200261#if defined(CONFIG_USB_GADGET_MANUFACTURER)
262static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmerdf063442009-07-29 18:18:43 +0200263#else
Bin Meng75574052016-02-05 19:30:11 -0800264static char *iManufacturer = "U-Boot";
Remy Bohmerdf063442009-07-29 18:18:43 +0200265#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300266
267/* These probably need to be configurable. */
268static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200269static char *iProduct;
270static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300271
Remy Bohmerdf063442009-07-29 18:18:43 +0200272static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300273
Remy Bohmerdf063442009-07-29 18:18:43 +0200274static char host_addr[18];
275
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300276
Remy Bohmerdf063442009-07-29 18:18:43 +0200277/*-------------------------------------------------------------------------*/
278
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400279/*
280 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200281 * ep0 implementation: descriptors, config management, setup().
282 * also optional class-specific notification interrupt transfer.
283 */
284
285/*
286 * DESCRIPTORS ... most are static, but strings and (full) configuration
287 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300288 * our simple subset, with RNDIS as an optional second configuration.
289 *
290 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
291 * the class descriptors match a modem (they're ignored; it's really just
292 * Ethernet functionality), they don't need the NOP altsetting, and the
293 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200294 */
295
296#define STRING_MANUFACTURER 1
297#define STRING_PRODUCT 2
298#define STRING_ETHADDR 3
299#define STRING_DATA 4
300#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300301#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200302#define STRING_CDC 7
303#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300304#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200305#define STRING_SERIALNUMBER 10
306
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300307/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200308#define USB_BUFSIZ 256
309
310/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300311 * This device advertises one configuration, eth_config, unless RNDIS
312 * is enabled (rndis_config) on hardware supporting at least two configs.
313 *
314 * NOTE: Controllers like superh_udc should probably be able to use
315 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200316 *
317 * FIXME define some higher-powered configurations to make it easier
318 * to recharge batteries ...
319 */
320
321#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300322#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200323
324static struct usb_device_descriptor
325device_desc = {
326 .bLength = sizeof device_desc,
327 .bDescriptorType = USB_DT_DEVICE,
328
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400329 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200330
331 .bDeviceClass = USB_CLASS_COMM,
332 .bDeviceSubClass = 0,
333 .bDeviceProtocol = 0,
334
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400335 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
336 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200337 .iManufacturer = STRING_MANUFACTURER,
338 .iProduct = STRING_PRODUCT,
339 .bNumConfigurations = 1,
340};
341
342static struct usb_otg_descriptor
343otg_descriptor = {
344 .bLength = sizeof otg_descriptor,
345 .bDescriptorType = USB_DT_OTG,
346
347 .bmAttributes = USB_OTG_SRP,
348};
349
350static struct usb_config_descriptor
351eth_config = {
352 .bLength = sizeof eth_config,
353 .bDescriptorType = USB_DT_CONFIG,
354
355 /* compute wTotalLength on the fly */
356 .bNumInterfaces = 2,
357 .bConfigurationValue = DEV_CONFIG_VALUE,
358 .iConfiguration = STRING_CDC,
359 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
360 .bMaxPower = 1,
361};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300362
363#ifdef CONFIG_USB_ETH_RNDIS
364static struct usb_config_descriptor
365rndis_config = {
366 .bLength = sizeof rndis_config,
367 .bDescriptorType = USB_DT_CONFIG,
368
369 /* compute wTotalLength on the fly */
370 .bNumInterfaces = 2,
371 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
372 .iConfiguration = STRING_RNDIS,
373 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
374 .bMaxPower = 1,
375};
376#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200377
378/*
379 * Compared to the simple CDC subset, the full CDC Ethernet model adds
380 * three class descriptors, two interface descriptors, optional status
381 * endpoint. Both have a "data" interface and two bulk endpoints.
382 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300383 *
384 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
385 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
386 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
387 * work around those bugs) are unlikely to ever come from MSFT, you may
388 * wish to avoid using RNDIS.
389 *
390 * MCCI offers an alternative to RNDIS if you need to connect to Windows
391 * but have hardware that can't support CDC Ethernet. We add descriptors
392 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
393 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
394 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200395 */
396
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200397#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200398static struct usb_interface_descriptor
399control_intf = {
400 .bLength = sizeof control_intf,
401 .bDescriptorType = USB_DT_INTERFACE,
402
403 .bInterfaceNumber = 0,
404 /* status endpoint is optional; this may be patched later */
405 .bNumEndpoints = 1,
406 .bInterfaceClass = USB_CLASS_COMM,
407 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
408 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
409 .iInterface = STRING_CONTROL,
410};
411#endif
412
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300413#ifdef CONFIG_USB_ETH_RNDIS
414static const struct usb_interface_descriptor
415rndis_control_intf = {
416 .bLength = sizeof rndis_control_intf,
417 .bDescriptorType = USB_DT_INTERFACE,
418
419 .bInterfaceNumber = 0,
420 .bNumEndpoints = 1,
421 .bInterfaceClass = USB_CLASS_COMM,
422 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
423 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
424 .iInterface = STRING_RNDIS_CONTROL,
425};
426#endif
427
Remy Bohmerdf063442009-07-29 18:18:43 +0200428static const struct usb_cdc_header_desc header_desc = {
429 .bLength = sizeof header_desc,
430 .bDescriptorType = USB_DT_CS_INTERFACE,
431 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
432
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400433 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200434};
435
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200436#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200437
438static const struct usb_cdc_union_desc union_desc = {
439 .bLength = sizeof union_desc,
440 .bDescriptorType = USB_DT_CS_INTERFACE,
441 .bDescriptorSubType = USB_CDC_UNION_TYPE,
442
443 .bMasterInterface0 = 0, /* index of control interface */
444 .bSlaveInterface0 = 1, /* index of DATA interface */
445};
446
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300447#endif /* CDC || RNDIS */
448
449#ifdef CONFIG_USB_ETH_RNDIS
450
451static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
452 .bLength = sizeof call_mgmt_descriptor,
453 .bDescriptorType = USB_DT_CS_INTERFACE,
454 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
455
456 .bmCapabilities = 0x00,
457 .bDataInterface = 0x01,
458};
459
460static const struct usb_cdc_acm_descriptor acm_descriptor = {
461 .bLength = sizeof acm_descriptor,
462 .bDescriptorType = USB_DT_CS_INTERFACE,
463 .bDescriptorSubType = USB_CDC_ACM_TYPE,
464
465 .bmCapabilities = 0x00,
466};
467
468#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200469
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200470#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200471
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400472/*
473 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200474 * ways: data endpoints live in the control interface, there's no data
475 * interface, and it's not used to talk to a cell phone radio.
476 */
477
478static const struct usb_cdc_mdlm_desc mdlm_desc = {
479 .bLength = sizeof mdlm_desc,
480 .bDescriptorType = USB_DT_CS_INTERFACE,
481 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
482
483 .bcdVersion = __constant_cpu_to_le16(0x0100),
484 .bGUID = {
485 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
486 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
487 },
488};
489
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400490/*
491 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200492 * can't really use its struct. All we do here is say that we're using
493 * the submode of "SAFE" which directly matches the CDC Subset.
494 */
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530495#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200496static const u8 mdlm_detail_desc[] = {
497 6,
498 USB_DT_CS_INTERFACE,
499 USB_CDC_MDLM_DETAIL_TYPE,
500
501 0, /* "SAFE" */
502 0, /* network control capabilities (none) */
503 0, /* network data capabilities ("raw" encapsulation) */
504};
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530505#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200506
507#endif
508
Remy Bohmerdf063442009-07-29 18:18:43 +0200509static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400510 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200511 .bDescriptorType = USB_DT_CS_INTERFACE,
512 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
513
514 /* this descriptor actually adds value, surprise! */
515 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400516 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
Bin Meng2ccb8b02018-07-31 02:55:22 -0700517 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400518 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200519 .bNumberPowerFilters = 0,
520};
521
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200522#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200523
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400524/*
525 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200526 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
527 * packet, to simplify cancellation; and a big transfer interval, to
528 * waste less bandwidth.
529 *
530 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
531 * if they ignore the connect/disconnect notifications that real aether
532 * can provide. more advanced cdc configurations might want to support
533 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300534 *
535 * RNDIS requires the status endpoint, since it uses that encapsulation
536 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200537 */
538
539#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
540#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
541
542static struct usb_endpoint_descriptor
543fs_status_desc = {
544 .bLength = USB_DT_ENDPOINT_SIZE,
545 .bDescriptorType = USB_DT_ENDPOINT,
546
547 .bEndpointAddress = USB_DIR_IN,
548 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400549 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200550 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
551};
552#endif
553
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200554#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200555
556/* the default data interface has no endpoints ... */
557
558static const struct usb_interface_descriptor
559data_nop_intf = {
560 .bLength = sizeof data_nop_intf,
561 .bDescriptorType = USB_DT_INTERFACE,
562
563 .bInterfaceNumber = 1,
564 .bAlternateSetting = 0,
565 .bNumEndpoints = 0,
566 .bInterfaceClass = USB_CLASS_CDC_DATA,
567 .bInterfaceSubClass = 0,
568 .bInterfaceProtocol = 0,
569};
570
571/* ... but the "real" data interface has two bulk endpoints */
572
573static const struct usb_interface_descriptor
574data_intf = {
575 .bLength = sizeof data_intf,
576 .bDescriptorType = USB_DT_INTERFACE,
577
578 .bInterfaceNumber = 1,
579 .bAlternateSetting = 1,
580 .bNumEndpoints = 2,
581 .bInterfaceClass = USB_CLASS_CDC_DATA,
582 .bInterfaceSubClass = 0,
583 .bInterfaceProtocol = 0,
584 .iInterface = STRING_DATA,
585};
586
587#endif
588
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300589#ifdef CONFIG_USB_ETH_RNDIS
590
591/* RNDIS doesn't activate by changing to the "real" altsetting */
592
593static const struct usb_interface_descriptor
594rndis_data_intf = {
595 .bLength = sizeof rndis_data_intf,
596 .bDescriptorType = USB_DT_INTERFACE,
597
598 .bInterfaceNumber = 1,
599 .bAlternateSetting = 0,
600 .bNumEndpoints = 2,
601 .bInterfaceClass = USB_CLASS_CDC_DATA,
602 .bInterfaceSubClass = 0,
603 .bInterfaceProtocol = 0,
604 .iInterface = STRING_DATA,
605};
606
607#endif
608
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200609#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200610
611/*
612 * "Simple" CDC-subset option is a simple vendor-neutral model that most
613 * full speed controllers can handle: one interface, two bulk endpoints.
614 *
615 * To assist host side drivers, we fancy it up a bit, and add descriptors
616 * so some host side drivers will understand it as a "SAFE" variant.
617 */
618
619static const struct usb_interface_descriptor
620subset_data_intf = {
621 .bLength = sizeof subset_data_intf,
622 .bDescriptorType = USB_DT_INTERFACE,
623
624 .bInterfaceNumber = 0,
625 .bAlternateSetting = 0,
626 .bNumEndpoints = 2,
627 .bInterfaceClass = USB_CLASS_COMM,
628 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
629 .bInterfaceProtocol = 0,
630 .iInterface = STRING_DATA,
631};
632
633#endif /* SUBSET */
634
Remy Bohmerdf063442009-07-29 18:18:43 +0200635static struct usb_endpoint_descriptor
636fs_source_desc = {
637 .bLength = USB_DT_ENDPOINT_SIZE,
638 .bDescriptorType = USB_DT_ENDPOINT,
639
640 .bEndpointAddress = USB_DIR_IN,
641 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700642 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200643};
644
645static struct usb_endpoint_descriptor
646fs_sink_desc = {
647 .bLength = USB_DT_ENDPOINT_SIZE,
648 .bDescriptorType = USB_DT_ENDPOINT,
649
650 .bEndpointAddress = USB_DIR_OUT,
651 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700652 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200653};
654
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400655static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200656 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200657#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200658 /* "cdc" mode descriptors */
659 (struct usb_descriptor_header *) &control_intf,
660 (struct usb_descriptor_header *) &header_desc,
661 (struct usb_descriptor_header *) &union_desc,
662 (struct usb_descriptor_header *) &ether_desc,
663 /* NOTE: status endpoint may need to be removed */
664 (struct usb_descriptor_header *) &fs_status_desc,
665 /* data interface, with altsetting */
666 (struct usb_descriptor_header *) &data_nop_intf,
667 (struct usb_descriptor_header *) &data_intf,
668 (struct usb_descriptor_header *) &fs_source_desc,
669 (struct usb_descriptor_header *) &fs_sink_desc,
670 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200671#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200672};
673
674static inline void fs_subset_descriptors(void)
675{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200676#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200677 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
678 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
679 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
680 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
681 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
682 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
683 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
684 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
685 fs_eth_function[8] = NULL;
686#else
687 fs_eth_function[1] = NULL;
688#endif
689}
690
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300691#ifdef CONFIG_USB_ETH_RNDIS
692static const struct usb_descriptor_header *fs_rndis_function[] = {
693 (struct usb_descriptor_header *) &otg_descriptor,
694 /* control interface matches ACM, not Ethernet */
695 (struct usb_descriptor_header *) &rndis_control_intf,
696 (struct usb_descriptor_header *) &header_desc,
697 (struct usb_descriptor_header *) &call_mgmt_descriptor,
698 (struct usb_descriptor_header *) &acm_descriptor,
699 (struct usb_descriptor_header *) &union_desc,
700 (struct usb_descriptor_header *) &fs_status_desc,
701 /* data interface has no altsetting */
702 (struct usb_descriptor_header *) &rndis_data_intf,
703 (struct usb_descriptor_header *) &fs_source_desc,
704 (struct usb_descriptor_header *) &fs_sink_desc,
705 NULL,
706};
707#endif
708
Remy Bohmerdf063442009-07-29 18:18:43 +0200709/*
710 * usb 2.0 devices need to expose both high speed and full speed
711 * descriptors, unless they only run at full speed.
712 */
713
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200714#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200715static struct usb_endpoint_descriptor
716hs_status_desc = {
717 .bLength = USB_DT_ENDPOINT_SIZE,
718 .bDescriptorType = USB_DT_ENDPOINT,
719
720 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400721 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200722 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
723};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200724#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200725
726static struct usb_endpoint_descriptor
727hs_source_desc = {
728 .bLength = USB_DT_ENDPOINT_SIZE,
729 .bDescriptorType = USB_DT_ENDPOINT,
730
731 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400732 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200733};
734
735static struct usb_endpoint_descriptor
736hs_sink_desc = {
737 .bLength = USB_DT_ENDPOINT_SIZE,
738 .bDescriptorType = USB_DT_ENDPOINT,
739
740 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400741 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200742};
743
744static struct usb_qualifier_descriptor
745dev_qualifier = {
746 .bLength = sizeof dev_qualifier,
747 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
748
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400749 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200750 .bDeviceClass = USB_CLASS_COMM,
751
752 .bNumConfigurations = 1,
753};
754
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400755static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200756 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200757#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200758 /* "cdc" mode descriptors */
759 (struct usb_descriptor_header *) &control_intf,
760 (struct usb_descriptor_header *) &header_desc,
761 (struct usb_descriptor_header *) &union_desc,
762 (struct usb_descriptor_header *) &ether_desc,
763 /* NOTE: status endpoint may need to be removed */
764 (struct usb_descriptor_header *) &hs_status_desc,
765 /* data interface, with altsetting */
766 (struct usb_descriptor_header *) &data_nop_intf,
767 (struct usb_descriptor_header *) &data_intf,
768 (struct usb_descriptor_header *) &hs_source_desc,
769 (struct usb_descriptor_header *) &hs_sink_desc,
770 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200771#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200772};
773
774static inline void hs_subset_descriptors(void)
775{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200776#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200777 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
778 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
779 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
780 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
781 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
782 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
783 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
784 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
785 hs_eth_function[8] = NULL;
786#else
787 hs_eth_function[1] = NULL;
788#endif
789}
790
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300791#ifdef CONFIG_USB_ETH_RNDIS
792static const struct usb_descriptor_header *hs_rndis_function[] = {
793 (struct usb_descriptor_header *) &otg_descriptor,
794 /* control interface matches ACM, not Ethernet */
795 (struct usb_descriptor_header *) &rndis_control_intf,
796 (struct usb_descriptor_header *) &header_desc,
797 (struct usb_descriptor_header *) &call_mgmt_descriptor,
798 (struct usb_descriptor_header *) &acm_descriptor,
799 (struct usb_descriptor_header *) &union_desc,
800 (struct usb_descriptor_header *) &hs_status_desc,
801 /* data interface has no altsetting */
802 (struct usb_descriptor_header *) &rndis_data_intf,
803 (struct usb_descriptor_header *) &hs_source_desc,
804 (struct usb_descriptor_header *) &hs_sink_desc,
805 NULL,
806};
807#endif
808
809
Remy Bohmerdf063442009-07-29 18:18:43 +0200810/* maxpacket and other transfer characteristics vary by speed. */
811static inline struct usb_endpoint_descriptor *
812ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
813 struct usb_endpoint_descriptor *fs)
814{
815 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
816 return hs;
817 return fs;
818}
819
Remy Bohmerdf063442009-07-29 18:18:43 +0200820/*-------------------------------------------------------------------------*/
821
822/* descriptors that are built on-demand */
823
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400824static char manufacturer[50];
825static char product_desc[40] = DRIVER_DESC;
826static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200827
828/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400829static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200830
831/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400832static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200833 { STRING_MANUFACTURER, manufacturer, },
834 { STRING_PRODUCT, product_desc, },
835 { STRING_SERIALNUMBER, serial_number, },
836 { STRING_DATA, "Ethernet Data", },
837 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200838#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200839 { STRING_CDC, "CDC Ethernet", },
840 { STRING_CONTROL, "CDC Communications Control", },
841#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200842#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200843 { STRING_SUBSET, "CDC Ethernet Subset", },
844#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300845#ifdef CONFIG_USB_ETH_RNDIS
846 { STRING_RNDIS, "RNDIS", },
847 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
848#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200849 { } /* end of list */
850};
851
852static struct usb_gadget_strings stringtab = {
853 .language = 0x0409, /* en-us */
854 .strings = strings,
855};
856
Remy Bohmerdf063442009-07-29 18:18:43 +0200857/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500858DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
859
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200860#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500861DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200862#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200863
Remy Bohmerdf063442009-07-29 18:18:43 +0200864/*============================================================================*/
865
866/*
867 * one config, two interfaces: control, data.
868 * complications: class descriptors, and an altsetting.
869 */
870static int
871config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
872{
873 int len;
874 const struct usb_config_descriptor *config;
875 const struct usb_descriptor_header **function;
876 int hs = 0;
877
878 if (gadget_is_dualspeed(g)) {
879 hs = (g->speed == USB_SPEED_HIGH);
880 if (type == USB_DT_OTHER_SPEED_CONFIG)
881 hs = !hs;
882 }
883#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
884
885 if (index >= device_desc.bNumConfigurations)
886 return -EINVAL;
887
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300888#ifdef CONFIG_USB_ETH_RNDIS
889 /*
890 * list the RNDIS config first, to make Microsoft's drivers
891 * happy. DOCSIS 1.0 needs this too.
892 */
893 if (device_desc.bNumConfigurations == 2 && index == 0) {
894 config = &rndis_config;
895 function = which_fn(rndis);
896 } else
897#endif
898 {
899 config = &eth_config;
900 function = which_fn(eth);
901 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200902
903 /* for now, don't advertise srp-only devices */
904 if (!is_otg)
905 function++;
906
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400907 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200908 if (len < 0)
909 return len;
910 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
911 return len;
912}
913
914/*-------------------------------------------------------------------------*/
915
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300916static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400917static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200918
919static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400920set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200921{
922 int result = 0;
923 struct usb_gadget *gadget = dev->gadget;
924
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200925#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300926 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200927 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400928 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200929 &fs_status_desc);
930 dev->status_ep->driver_data = dev;
931
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400932 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200933 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400934 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200935 dev->status_ep->name, result);
936 goto done;
937 }
938 }
939#endif
940
941 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
942 dev->in_ep->driver_data = dev;
943
944 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
945 dev->out_ep->driver_data = dev;
946
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400947 /*
948 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200949 * endpoints in the default altsetting for the interface.
950 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300951 *
952 * Strictly speaking RNDIS should work the same: activation is
953 * a side effect of setting a packet filter. Deactivation is
954 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200955 */
956 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400957 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200958 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400959 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200960 dev->in_ep->name, result);
961 goto done;
962 }
963
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400964 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200965 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400966 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200967 dev->out_ep->name, result);
968 goto done;
969 }
970 }
971
972done:
973 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400974 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200975
976 /* on error, disable any endpoints */
977 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400978 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400979 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200980 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400981 (void) usb_ep_disable(dev->in_ep);
982 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200983 dev->in = NULL;
984 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300985 } else if (!cdc_active(dev)) {
986 /*
987 * activate non-CDC configs right away
988 * this isn't strictly according to the RNDIS spec
989 */
990 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +0200991 }
992
993 /* caller is responsible for cleanup on error */
994 return result;
995}
996
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400997static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +0200998{
999 if (dev->config == 0)
1000 return;
1001
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001002 debug("%s\n", __func__);
1003
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001004 rndis_uninit(dev->rndis_config);
1005
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001006 /*
1007 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001008 * pending i/o. then free the requests.
1009 */
1010
1011 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001012 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001013 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001014 usb_ep_free_request(dev->in_ep, dev->tx_req);
1015 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001016 }
1017 }
1018 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001019 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001020 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001021 usb_ep_free_request(dev->out_ep, dev->rx_req);
1022 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001023 }
1024 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001025 if (dev->status)
1026 usb_ep_disable(dev->status_ep);
1027
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001028 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001029 dev->cdc_filter = 0;
1030 dev->config = 0;
1031}
1032
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001033/*
1034 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001035 * that returns config descriptors, and altsetting code.
1036 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001037static int eth_set_config(struct eth_dev *dev, unsigned number,
1038 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001039{
1040 int result = 0;
1041 struct usb_gadget *gadget = dev->gadget;
1042
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001043 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001044
1045 switch (number) {
1046 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001047 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001048 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001049#ifdef CONFIG_USB_ETH_RNDIS
1050 case DEV_RNDIS_CONFIG_VALUE:
1051 dev->rndis = 1;
1052 result = set_ether_config(dev, gfp_flags);
1053 break;
1054#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001055 default:
1056 result = -EINVAL;
1057 /* FALL THROUGH */
1058 case 0:
1059 break;
1060 }
1061
1062 if (result) {
1063 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001064 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001065 usb_gadget_vbus_draw(dev->gadget,
1066 gadget_is_otg(dev->gadget) ? 8 : 100);
1067 } else {
1068 char *speed;
1069 unsigned power;
1070
1071 power = 2 * eth_config.bMaxPower;
1072 usb_gadget_vbus_draw(dev->gadget, power);
1073
1074 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001075 case USB_SPEED_FULL:
1076 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001077#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001078 case USB_SPEED_HIGH:
1079 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001080#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001081 default:
1082 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001083 }
1084
1085 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001086 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001087 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001088 rndis_active(dev)
1089 ? "RNDIS"
1090 : (cdc_active(dev)
1091 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001092 : "CDC Ethernet Subset"));
1093 }
1094 return result;
1095}
1096
1097/*-------------------------------------------------------------------------*/
1098
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001099#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001100
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001101/*
1102 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001103 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001104 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001105 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1106 * command mechanism; and only one status request is ever queued.
1107 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001108static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001109{
1110 struct usb_cdc_notification *event = req->buf;
1111 int value = req->status;
1112 struct eth_dev *dev = ep->driver_data;
1113
1114 /* issue the second notification if host reads the first */
1115 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1116 && value == 0) {
1117 __le32 *data = req->buf + sizeof *event;
1118
1119 event->bmRequestType = 0xA1;
1120 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001121 event->wValue = __constant_cpu_to_le16(0);
1122 event->wIndex = __constant_cpu_to_le16(1);
1123 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001124
1125 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001126 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001127
1128 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001129 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001130 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001131 if (value == 0)
1132 return;
1133 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001134 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001135 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001136 if (event->bNotificationType ==
1137 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301138 dev->network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001139 printf("USB network up!\n");
1140 }
1141 }
1142 req->context = NULL;
1143}
1144
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001145static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001146{
1147 struct usb_request *req = dev->stat_req;
1148 struct usb_cdc_notification *event;
1149 int value;
1150
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001151 /*
1152 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001153 *
1154 * FIXME ugly idiom, maybe we'd be better with just
1155 * a "cancel the whole queue" primitive since any
1156 * unlink-one primitive has way too many error modes.
1157 * here, we "know" toggle is already clear...
1158 *
1159 * FIXME iff req->context != null just dequeue it
1160 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001161 usb_ep_disable(dev->status_ep);
1162 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001163
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001164 /*
1165 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001166 * a SPEED_CHANGE. could be useful in some configs.
1167 */
1168 event = req->buf;
1169 event->bmRequestType = 0xA1;
1170 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001171 event->wValue = __constant_cpu_to_le16(1); /* connected */
1172 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001173 event->wLength = 0;
1174
1175 req->length = sizeof *event;
1176 req->complete = eth_status_complete;
1177 req->context = dev;
1178
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001179 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001180 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001181 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001182}
1183
1184#endif
1185
1186/*-------------------------------------------------------------------------*/
1187
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001188static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001189{
1190 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001191 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001192 req->status, req->actual, req->length);
1193}
1194
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001195#ifdef CONFIG_USB_ETH_RNDIS
1196
1197static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1198{
1199 if (req->status || req->actual != req->length)
1200 debug("rndis response complete --> %d, %d/%d\n",
1201 req->status, req->actual, req->length);
1202
1203 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1204}
1205
1206static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1207{
1208 struct eth_dev *dev = ep->driver_data;
1209 int status;
1210
1211 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1212 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1213 if (status < 0)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001214 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001215}
1216
1217#endif /* RNDIS */
1218
Remy Bohmerdf063442009-07-29 18:18:43 +02001219/*
1220 * The setup() callback implements all the ep0 functionality that's not
1221 * handled lower down. CDC has a number of less-common features:
1222 *
1223 * - two interfaces: control, and ethernet data
1224 * - Ethernet data interface has two altsettings: default, and active
1225 * - class-specific descriptors for the control interface
1226 * - class-specific control requests
1227 */
1228static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001229eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001230{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001231 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001232 struct usb_request *req = dev->req;
1233 int value = -EOPNOTSUPP;
1234 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1235 u16 wValue = le16_to_cpu(ctrl->wValue);
1236 u16 wLength = le16_to_cpu(ctrl->wLength);
1237
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001238 /*
1239 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001240 * while config change events may enable network traffic.
1241 */
1242
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001243 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001244
1245 req->complete = eth_setup_complete;
1246 switch (ctrl->bRequest) {
1247
1248 case USB_REQ_GET_DESCRIPTOR:
1249 if (ctrl->bRequestType != USB_DIR_IN)
1250 break;
1251 switch (wValue >> 8) {
1252
1253 case USB_DT_DEVICE:
Kishon Vijay Abraham I58d701e2015-02-23 18:40:17 +05301254 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001255 value = min(wLength, (u16) sizeof device_desc);
1256 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001257 break;
1258 case USB_DT_DEVICE_QUALIFIER:
1259 if (!gadget_is_dualspeed(gadget))
1260 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001261 value = min(wLength, (u16) sizeof dev_qualifier);
1262 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001263 break;
1264
1265 case USB_DT_OTHER_SPEED_CONFIG:
1266 if (!gadget_is_dualspeed(gadget))
1267 break;
1268 /* FALLTHROUGH */
1269 case USB_DT_CONFIG:
1270 value = config_buf(gadget, req->buf,
1271 wValue >> 8,
1272 wValue & 0xff,
1273 gadget_is_otg(gadget));
1274 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001275 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001276 break;
1277
1278 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001279 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001280 wValue & 0xff, req->buf);
1281
1282 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001283 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001284
1285 break;
1286 }
1287 break;
1288
1289 case USB_REQ_SET_CONFIGURATION:
1290 if (ctrl->bRequestType != 0)
1291 break;
1292 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001293 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001294 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001295 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001296 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001297 break;
1298 case USB_REQ_GET_CONFIGURATION:
1299 if (ctrl->bRequestType != USB_DIR_IN)
1300 break;
1301 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001302 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001303 break;
1304
1305 case USB_REQ_SET_INTERFACE:
1306 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1307 || !dev->config
1308 || wIndex > 1)
1309 break;
1310 if (!cdc_active(dev) && wIndex != 0)
1311 break;
1312
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001313#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001314 switch (wIndex) {
1315 case 0: /* control/master intf */
1316 if (wValue != 0)
1317 break;
1318 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001319 usb_ep_disable(dev->status_ep);
1320 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001321 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001322
Remy Bohmerdf063442009-07-29 18:18:43 +02001323 value = 0;
1324 break;
1325 case 1: /* data intf */
1326 if (wValue > 1)
1327 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001328 usb_ep_disable(dev->in_ep);
1329 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001330
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001331 /*
1332 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001333 * the default interface setting ... also, setting
1334 * the non-default interface resets filters etc.
1335 */
1336 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001337 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001338 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001339 usb_ep_enable(dev->in_ep, dev->in);
1340 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001341 dev->cdc_filter = DEFAULT_FILTER;
1342 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001343 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001344 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001345 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001346 value = 0;
1347 break;
1348 }
1349#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001350 /*
1351 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001352 * all non-PXA hardware talks real CDC ...
1353 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001354 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001355#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001356 break;
1357 case USB_REQ_GET_INTERFACE:
1358 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1359 || !dev->config
1360 || wIndex > 1)
1361 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001362 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001363 break;
1364
1365 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001366 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001367 *(u8 *)req->buf = 0;
1368 else {
1369 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1370 /* carrier always ok ...*/
1371 *(u8 *)req->buf = 1 ;
1372 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001373 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001374 break;
1375
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001376#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001377 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001378 /*
1379 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001380 * wValue = packet filter bitmap
1381 */
1382 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1383 || !cdc_active(dev)
1384 || wLength != 0
1385 || wIndex > 1)
1386 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001387 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001388 dev->cdc_filter = wValue;
1389 value = 0;
1390 break;
1391
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001392 /*
1393 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001394 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1395 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1396 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1397 * case USB_CDC_GET_ETHERNET_STATISTIC:
1398 */
1399
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001400#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001401
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001402#ifdef CONFIG_USB_ETH_RNDIS
1403 /*
1404 * RNDIS uses the CDC command encapsulation mechanism to implement
1405 * an RPC scheme, with much getting/setting of attributes by OID.
1406 */
1407 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1408 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1409 || !rndis_active(dev)
1410 || wLength > USB_BUFSIZ
1411 || wValue
1412 || rndis_control_intf.bInterfaceNumber
1413 != wIndex)
1414 break;
1415 /* read the request, then process it */
1416 value = wLength;
1417 req->complete = rndis_command_complete;
1418 /* later, rndis_control_ack () sends a notification */
1419 break;
1420
1421 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1422 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1423 == ctrl->bRequestType
1424 && rndis_active(dev)
1425 /* && wLength >= 0x0400 */
1426 && !wValue
1427 && rndis_control_intf.bInterfaceNumber
1428 == wIndex) {
1429 u8 *buf;
1430 u32 n;
1431
1432 /* return the result */
1433 buf = rndis_get_next_response(dev->rndis_config, &n);
1434 if (buf) {
1435 memcpy(req->buf, buf, n);
1436 req->complete = rndis_response_complete;
1437 rndis_free_response(dev->rndis_config, buf);
1438 value = n;
1439 }
1440 /* else stalls ... spec says to avoid that */
1441 }
1442 break;
1443#endif /* RNDIS */
1444
Remy Bohmerdf063442009-07-29 18:18:43 +02001445 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001446 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001447 ctrl->bRequestType, ctrl->bRequest,
1448 wValue, wIndex, wLength);
1449 }
1450
1451 /* respond with data transfer before status phase? */
1452 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001453 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001454 req->length = value;
1455 req->zero = value < wLength
1456 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001457 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001458 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001459 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001460 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001461 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001462 }
1463 }
1464
1465 /* host either stalls (value < 0) or reports success */
1466 return value;
1467}
1468
Remy Bohmerdf063442009-07-29 18:18:43 +02001469/*-------------------------------------------------------------------------*/
1470
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001471static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001472
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001473static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001474 gfp_t gfp_flags)
1475{
1476 int retval = -ENOMEM;
1477 size_t size;
1478
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001479 /*
1480 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001481 * Normally we use the USB "terminate on short read" convention;
1482 * so allow up to (N*maxpacket), since that memory is normally
1483 * already allocated. Some hardware doesn't deal well with short
1484 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1485 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001486 *
1487 * RNDIS uses internal framing, and explicitly allows senders to
1488 * pad to end-of-packet. That's potentially nice for speed,
1489 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001490 */
1491
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001492 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001493 if (!req)
1494 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001495
1496 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1497 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001498 if (rndis_active(dev))
1499 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001500 size -= size % dev->out_ep->maxpacket;
1501
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001502 /*
1503 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001504 * but on at least one, checksumming fails otherwise. Note:
1505 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001506 */
1507
Joe Hershberger9f09a362015-04-08 01:41:06 -05001508 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmerdf063442009-07-29 18:18:43 +02001509 req->length = size;
1510 req->complete = rx_complete;
1511
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001512 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001513
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001514 if (retval)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001515 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001516
Remy Bohmerdf063442009-07-29 18:18:43 +02001517 return retval;
1518}
1519
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001520static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001521{
1522 struct eth_dev *dev = ep->driver_data;
1523
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001524 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001525 switch (req->status) {
1526 /* normal completion */
1527 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001528 if (rndis_active(dev)) {
1529 /* we know MaxPacketsPerTransfer == 1 here */
1530 int length = rndis_rm_hdr(req->buf, req->actual);
1531 if (length < 0)
1532 goto length_err;
1533 req->length -= length;
1534 req->actual -= length;
1535 }
Bin Meng2ccb8b02018-07-31 02:55:22 -07001536 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001537length_err:
1538 dev->stats.rx_errors++;
1539 dev->stats.rx_length_errors++;
1540 debug("rx length %d\n", req->length);
1541 break;
1542 }
1543
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001544 dev->stats.rx_packets++;
1545 dev->stats.rx_bytes += req->length;
1546 break;
1547
1548 /* software-driven interface shutdown */
1549 case -ECONNRESET: /* unlink */
1550 case -ESHUTDOWN: /* disconnect etc */
1551 /* for hardware automagic (such as pxa) */
1552 case -ECONNABORTED: /* endpoint reset */
1553 break;
1554
1555 /* data overrun */
1556 case -EOVERFLOW:
1557 dev->stats.rx_over_errors++;
1558 /* FALLTHROUGH */
1559 default:
1560 dev->stats.rx_errors++;
1561 break;
1562 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001563
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001564 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001565}
1566
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001567static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001568{
1569
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001570 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001571
1572 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001573 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001574
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001575 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001576
1577 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001578 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001579
1580 return 0;
1581
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001582fail2:
1583 usb_ep_free_request(dev->in_ep, dev->tx_req);
1584fail1:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001585 pr_err("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001586 return -1;
1587}
1588
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001589static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001590{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001591 struct eth_dev *dev = ep->driver_data;
1592
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001593 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001594 switch (req->status) {
1595 default:
1596 dev->stats.tx_errors++;
1597 debug("tx err %d\n", req->status);
1598 /* FALLTHROUGH */
1599 case -ECONNRESET: /* unlink */
1600 case -ESHUTDOWN: /* disconnect etc */
1601 break;
1602 case 0:
1603 dev->stats.tx_bytes += req->length;
1604 }
1605 dev->stats.tx_packets++;
1606
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001607 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001608}
1609
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001610static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001611{
1612 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001613 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001614 return 1;
1615 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1616}
1617
1618#if 0
1619static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1620{
1621 struct eth_dev *dev = netdev_priv(net);
1622 int length = skb->len;
1623 int retval;
1624 struct usb_request *req = NULL;
1625 unsigned long flags;
1626
1627 /* apply outgoing CDC or RNDIS filters */
1628 if (!eth_is_promisc (dev)) {
1629 u8 *dest = skb->data;
1630
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001631 if (is_multicast_ethaddr(dest)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001632 u16 type;
1633
1634 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1635 * SET_ETHERNET_MULTICAST_FILTERS requests
1636 */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001637 if (is_broadcast_ethaddr(dest))
Remy Bohmerdf063442009-07-29 18:18:43 +02001638 type = USB_CDC_PACKET_TYPE_BROADCAST;
1639 else
1640 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1641 if (!(dev->cdc_filter & type)) {
1642 dev_kfree_skb_any (skb);
1643 return 0;
1644 }
1645 }
1646 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1647 }
1648
1649 spin_lock_irqsave(&dev->req_lock, flags);
1650 /*
1651 * this freelist can be empty if an interrupt triggered disconnect()
1652 * and reconfigured the gadget (shutting down this queue) after the
1653 * network stack decided to xmit but before we got the spinlock.
1654 */
1655 if (list_empty(&dev->tx_reqs)) {
1656 spin_unlock_irqrestore(&dev->req_lock, flags);
1657 return 1;
1658 }
1659
1660 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1661 list_del (&req->list);
1662
1663 /* temporarily stop TX queue when the freelist empties */
1664 if (list_empty (&dev->tx_reqs))
1665 netif_stop_queue (net);
1666 spin_unlock_irqrestore(&dev->req_lock, flags);
1667
1668 /* no buffer copies needed, unless the network stack did it
1669 * or the hardware can't use skb buffers.
1670 * or there's not enough space for any RNDIS headers we need
1671 */
1672 if (rndis_active(dev)) {
1673 struct sk_buff *skb_rndis;
1674
1675 skb_rndis = skb_realloc_headroom (skb,
1676 sizeof (struct rndis_packet_msg_type));
1677 if (!skb_rndis)
1678 goto drop;
1679
1680 dev_kfree_skb_any (skb);
1681 skb = skb_rndis;
1682 rndis_add_hdr (skb);
1683 length = skb->len;
1684 }
1685 req->buf = skb->data;
1686 req->context = skb;
1687 req->complete = tx_complete;
1688
1689 /* use zlp framing on tx for strict CDC-Ether conformance,
1690 * though any robust network rx path ignores extra padding.
1691 * and some hardware doesn't like to write zlps.
1692 */
1693 req->zero = 1;
1694 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1695 length++;
1696
1697 req->length = length;
1698
1699 /* throttle highspeed IRQ rate back slightly */
1700 if (gadget_is_dualspeed(dev->gadget))
1701 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1702 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1703 : 0;
1704
1705 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1706 switch (retval) {
1707 default:
1708 DEBUG (dev, "tx queue err %d\n", retval);
1709 break;
1710 case 0:
1711 net->trans_start = jiffies;
1712 atomic_inc (&dev->tx_qlen);
1713 }
1714
1715 if (retval) {
1716drop:
1717 dev->stats.tx_dropped++;
1718 dev_kfree_skb_any (skb);
1719 spin_lock_irqsave(&dev->req_lock, flags);
1720 if (list_empty (&dev->tx_reqs))
1721 netif_start_queue (net);
1722 list_add (&req->list, &dev->tx_reqs);
1723 spin_unlock_irqrestore(&dev->req_lock, flags);
1724 }
1725 return 0;
1726}
1727
1728/*-------------------------------------------------------------------------*/
1729#endif
1730
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001731static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001732{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001733 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001734
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001735 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001736 rndis_deregister(dev->rndis_config);
1737 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001738
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001739 /* we've already been disconnected ... no i/o is active */
1740 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001741 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001742 dev->req = NULL;
1743 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001744 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001745 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001746 dev->stat_req = NULL;
1747 }
1748
1749 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001750 usb_ep_free_request(dev->in_ep, dev->tx_req);
1751 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001752 }
1753
1754 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001755 usb_ep_free_request(dev->out_ep, dev->rx_req);
1756 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001757 }
1758
1759/* unregister_netdev (dev->net);*/
1760/* free_netdev(dev->net);*/
1761
Lei Wen4af32732010-12-01 23:43:43 +08001762 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001763 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001764}
1765
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001766static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001767{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001768 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001769 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001770}
1771
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001772static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001773{
1774 /* Not used */
1775}
1776
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001777static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001778{
1779 /* Not used */
1780}
1781
1782/*-------------------------------------------------------------------------*/
1783
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001784#ifdef CONFIG_USB_ETH_RNDIS
1785
1786/*
1787 * The interrupt endpoint is used in RNDIS to notify the host when messages
1788 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1789 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1790 * REMOTE_NDIS_KEEPALIVE_MSG.
1791 *
1792 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1793 * normally just one notification will be queued.
1794 */
1795
1796static void rndis_control_ack_complete(struct usb_ep *ep,
1797 struct usb_request *req)
1798{
1799 struct eth_dev *dev = ep->driver_data;
1800
1801 debug("%s...\n", __func__);
1802 if (req->status || req->actual != req->length)
1803 debug("rndis control ack complete --> %d, %d/%d\n",
1804 req->status, req->actual, req->length);
1805
Mugunthan V N3963de32016-11-18 10:49:13 +05301806 if (!dev->network_started) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001807 if (rndis_get_state(dev->rndis_config)
1808 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301809 dev->network_started = 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001810 printf("USB RNDIS network up!\n");
1811 }
1812 }
1813
1814 req->context = NULL;
1815
1816 if (req != dev->stat_req)
1817 usb_ep_free_request(ep, req);
1818}
1819
1820static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1821
Mugunthan V N095b7612016-11-18 11:09:15 +05301822static int rndis_control_ack(struct udevice *net)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001823{
Simon Glass95588622020-12-22 19:30:28 -07001824 struct ether_priv *priv;
1825 struct eth_dev *dev;
1826 int length;
1827 struct usb_request *resp;
1828
Simon Glass95588622020-12-22 19:30:28 -07001829 priv = dev_get_priv(net);
Simon Glass95588622020-12-22 19:30:28 -07001830 dev = &priv->ethdev;
1831 resp = dev->stat_req;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001832
1833 /* in case RNDIS calls this after disconnect */
1834 if (!dev->status) {
1835 debug("status ENODEV\n");
1836 return -ENODEV;
1837 }
1838
1839 /* in case queue length > 1 */
1840 if (resp->context) {
1841 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1842 if (!resp)
1843 return -ENOMEM;
1844 resp->buf = rndis_resp_buf;
1845 }
1846
1847 /*
1848 * Send RNDIS RESPONSE_AVAILABLE notification;
1849 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1850 */
1851 resp->length = 8;
1852 resp->complete = rndis_control_ack_complete;
1853 resp->context = dev;
1854
1855 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1856 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1857
1858 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1859 if (length < 0) {
1860 resp->status = 0;
1861 rndis_control_ack_complete(dev->status_ep, resp);
1862 }
1863
1864 return 0;
1865}
1866
1867#else
1868
1869#define rndis_control_ack NULL
1870
1871#endif /* RNDIS */
1872
1873static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1874{
1875 if (rndis_active(dev)) {
1876 rndis_set_param_medium(dev->rndis_config,
1877 NDIS_MEDIUM_802_3,
1878 BITRATE(dev->gadget)/100);
1879 rndis_signal_connect(dev->rndis_config);
1880 }
1881}
1882
1883static int eth_stop(struct eth_dev *dev)
1884{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001885#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1886 unsigned long ts;
1887 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1888#endif
1889
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001890 if (rndis_active(dev)) {
1891 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1892 rndis_signal_disconnect(dev->rndis_config);
1893
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001894#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1895 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1896 ts = get_timer(0);
1897 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05301898 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001899#endif
1900
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001901 rndis_uninit(dev->rndis_config);
1902 dev->rndis = 0;
1903 }
1904
1905 return 0;
1906}
1907
1908/*-------------------------------------------------------------------------*/
1909
Remy Bohmerdf063442009-07-29 18:18:43 +02001910static int is_eth_addr_valid(char *str)
1911{
1912 if (strlen(str) == 17) {
1913 int i;
1914 char *p, *q;
1915 uchar ea[6];
1916
1917 /* see if it looks like an ethernet address */
1918
1919 p = str;
1920
1921 for (i = 0; i < 6; i++) {
1922 char term = (i == 5 ? '\0' : ':');
1923
1924 ea[i] = simple_strtol(p, &q, 16);
1925
1926 if ((q - p) != 2 || *q++ != term)
1927 break;
1928
1929 p = q;
1930 }
1931
Tom Riniadd21ca2012-10-31 13:30:41 +00001932 /* Now check the contents. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001933 return is_valid_ethaddr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001934 }
1935 return 0;
1936}
1937
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001938static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001939{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001940 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001941 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001942 c = toupper(c);
1943 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001944 return 10 + c - 'A';
1945 return 0;
1946}
1947
1948static int get_ether_addr(const char *str, u8 *dev_addr)
1949{
1950 if (str) {
1951 unsigned i;
1952
1953 for (i = 0; i < 6; i++) {
1954 unsigned char num;
1955
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001956 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001957 str++;
1958 num = nibble(*str++) << 4;
1959 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001960 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02001961 }
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001962 if (is_valid_ethaddr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02001963 return 0;
1964 }
1965 return 1;
1966}
1967
1968static int eth_bind(struct usb_gadget *gadget)
1969{
Mugunthan V N88e48be2016-11-18 11:06:13 +05301970 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001971 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001972 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001973 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02001974 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001975 u8 tmp[7];
Simon Glassfa20e932020-12-03 16:55:20 -07001976 struct eth_pdata *pdata = dev_get_plat(l_priv->netdev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001977
1978 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001979#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001980 cdc = 0;
1981#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001982#ifndef CONFIG_USB_ETH_RNDIS
1983 rndis = 0;
1984#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001985 /*
1986 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02001987 * standard protocol is _strongly_ preferred for interop purposes.
1988 * (By everyone except Microsoft.)
1989 */
Tom Rini1e56de02022-06-25 11:02:42 -04001990 if (gadget_is_musbhdrc(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001991 /* reduce tx dma overhead by avoiding special cases */
1992 zlp = 0;
1993 } else if (gadget_is_sh(gadget)) {
1994 /* sh doesn't support multiple interfaces or configs */
1995 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001996 rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001997 }
1998
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001999 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002000 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002001 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002002 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002003 /*
2004 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002005 * anything less functional on CDC-capable hardware,
2006 * so we fail in this case.
2007 */
Masahiro Yamada81e10422017-09-16 14:10:41 +09002008 pr_err("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002009 gadget->name);
2010 return -ENODEV;
2011 }
2012
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002013 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002014 * If there's an RNDIS configuration, that's what Windows wants to
2015 * be using ... so use these product IDs here and in the "linux.inf"
2016 * needed to install MSFT drivers. Current Linux kernels will use
2017 * the second configuration if it's CDC Ethernet, and need some help
2018 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002019 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002020 if (rndis) {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002021#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Remy Bohmerdf063442009-07-29 18:18:43 +02002022 device_desc.idVendor =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002023 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Remy Bohmerdf063442009-07-29 18:18:43 +02002024 device_desc.idProduct =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002025 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002026#else
2027 device_desc.idVendor =
2028 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2029 device_desc.idProduct =
2030 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2031#endif
2032 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002033
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002034 /*
2035 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2036 * drivers aren't widely available. (That may be improved by
2037 * supporting one submode of the "SAFE" variant of MDLM.)
2038 */
2039 } else {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002040#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2041 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2042 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002043#else
2044 if (!cdc) {
2045 device_desc.idVendor =
2046 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2047 device_desc.idProduct =
2048 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2049 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002050#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002051 }
2052 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002053 if (bcdDevice)
2054 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2055 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002056 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002057 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002058 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002059 if (iSerialNumber) {
2060 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002061 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002062 }
2063
2064 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002065 usb_ep_autoconfig_reset(gadget);
2066 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002067 if (!in_ep) {
2068autoconf_fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002069 pr_err("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002070 gadget->name);
2071 return -ENODEV;
2072 }
2073 in_ep->driver_data = in_ep; /* claim */
2074
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002075 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002076 if (!out_ep)
2077 goto autoconf_fail;
2078 out_ep->driver_data = out_ep; /* claim */
2079
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002080#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002081 /*
2082 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002083 * Since some hosts expect one, try to allocate one anyway.
2084 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002085 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002086 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002087 if (status_ep) {
2088 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002089 } else if (rndis) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002090 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002091 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002092#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002093 } else if (cdc) {
2094 control_intf.bNumEndpoints = 0;
2095 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002096#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002097 }
2098 }
2099#endif
2100
2101 /* one config: cdc, else minimal subset */
2102 if (!cdc) {
2103 eth_config.bNumInterfaces = 1;
2104 eth_config.iConfiguration = STRING_SUBSET;
2105
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002106 /*
2107 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002108 * with multiple controllers and must override CDC Ethernet.
2109 */
2110 fs_subset_descriptors();
2111 hs_subset_descriptors();
2112 }
2113
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002114 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002115
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002116 /* For now RNDIS is always a second config */
2117 if (rndis)
2118 device_desc.bNumConfigurations = 2;
2119
Remy Bohmerdf063442009-07-29 18:18:43 +02002120 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002121 if (rndis)
2122 dev_qualifier.bNumConfigurations = 2;
2123 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002124 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2125
2126 /* assumes ep0 uses the same value for both speeds ... */
2127 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2128
2129 /* and that all endpoints are dual-speed */
2130 hs_source_desc.bEndpointAddress =
2131 fs_source_desc.bEndpointAddress;
2132 hs_sink_desc.bEndpointAddress =
2133 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002134#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002135 if (status_ep)
2136 hs_status_desc.bEndpointAddress =
2137 fs_status_desc.bEndpointAddress;
2138#endif
2139 }
2140
2141 if (gadget_is_otg(gadget)) {
2142 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2143 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2144 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002145#ifdef CONFIG_USB_ETH_RNDIS
2146 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2147 rndis_config.bMaxPower = 4;
2148#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002149 }
2150
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002151
2152 /* network device setup */
Mugunthan V N095b7612016-11-18 11:09:15 +05302153 dev->net = l_priv->netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002154
2155 dev->cdc = cdc;
2156 dev->zlp = zlp;
2157
2158 dev->in_ep = in_ep;
2159 dev->out_ep = out_ep;
2160 dev->status_ep = status_ep;
2161
Mugunthan V N095b7612016-11-18 11:09:15 +05302162 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002163 /*
2164 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002165 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002166 * ends up in a persistent config database. It's not clear if
2167 * host side code for the SAFE thing cares -- its original BLAN
2168 * thing didn't, Sharp never assigned those addresses on Zaurii.
2169 */
Mugunthan V N095b7612016-11-18 11:09:15 +05302170 get_ether_addr(dev_addr, pdata->enetaddr);
2171 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
Remy Bohmerdf063442009-07-29 18:18:43 +02002172
2173 get_ether_addr(host_addr, dev->host_mac);
2174
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002175 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2176 dev->host_mac[0], dev->host_mac[1],
2177 dev->host_mac[2], dev->host_mac[3],
2178 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002179
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002180 if (rndis) {
2181 status = rndis_init();
2182 if (status < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002183 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002184 goto fail;
2185 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002186 }
2187
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002188 /*
2189 * use PKTSIZE (or aligned... from u-boot) and set
2190 * wMaxSegmentSize accordingly
2191 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002192 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2193
2194 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002195 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002196 if (!dev->req)
2197 goto fail;
2198 dev->req->buf = control_req;
2199 dev->req->complete = eth_setup_complete;
2200
2201 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002202#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002203 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002204 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2205 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002206 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002207 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002208
2209 goto fail;
2210 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002211 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002212 dev->stat_req->context = NULL;
2213 }
2214#endif
2215
2216 /* finish hookup to lower layer ... */
2217 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002218 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002219 gadget->ep0->driver_data = dev;
2220
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002221 /*
2222 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002223 * - iff DATA transfer is active, carrier is "on"
2224 * - tx queueing enabled if open *and* carrier is "on"
2225 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002226
2227 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2228 out_ep->name, in_ep->name,
2229 status_ep ? " STATUS " : "",
2230 status_ep ? status_ep->name : ""
2231 );
Mugunthan V N095b7612016-11-18 11:09:15 +05302232 printf("MAC %pM\n", pdata->enetaddr);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002233
2234 if (cdc || rndis)
2235 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2236 dev->host_mac[0], dev->host_mac[1],
2237 dev->host_mac[2], dev->host_mac[3],
2238 dev->host_mac[4], dev->host_mac[5]);
2239
2240 if (rndis) {
2241 u32 vendorID = 0;
2242
2243 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2244
2245 dev->rndis_config = rndis_register(rndis_control_ack);
2246 if (dev->rndis_config < 0) {
2247fail0:
2248 eth_unbind(gadget);
2249 debug("RNDIS setup failed\n");
2250 status = -ENODEV;
2251 goto fail;
2252 }
2253
2254 /* these set up a lot of the OIDs that RNDIS needs */
2255 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2256 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2257 &dev->stats, &dev->cdc_filter))
2258 goto fail0;
2259 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2260 manufacturer))
2261 goto fail0;
2262 if (rndis_set_param_medium(dev->rndis_config,
2263 NDIS_MEDIUM_802_3, 0))
2264 goto fail0;
2265 printf("RNDIS ready\n");
2266 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002267 return 0;
2268
2269fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002270 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002271 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002272 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002273}
2274
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002275/*-------------------------------------------------------------------------*/
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002276static void _usb_eth_halt(struct ether_priv *priv);
2277
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302278static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002279{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302280 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002281 struct usb_gadget *gadget;
2282 unsigned long ts;
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002283 int ret;
Remy Bohmerdf063442009-07-29 18:18:43 +02002284 unsigned long timeout = USB_CONNECT_TIMEOUT;
2285
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002286 ret = usb_gadget_initialize(0);
2287 if (ret)
2288 return ret;
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302289
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002290 /* Configure default mac-addresses for the USB ethernet device */
2291#ifdef CONFIG_USBNET_DEV_ADDR
2292 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2293#endif
2294#ifdef CONFIG_USBNET_HOST_ADDR
2295 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2296#endif
2297 /* Check if the user overruled the MAC addresses */
Simon Glass64b723f2017-08-03 12:22:12 -06002298 if (env_get("usbnet_devaddr"))
2299 strlcpy(dev_addr, env_get("usbnet_devaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002300 sizeof(dev_addr));
2301
Simon Glass64b723f2017-08-03 12:22:12 -06002302 if (env_get("usbnet_hostaddr"))
2303 strlcpy(host_addr, env_get("usbnet_hostaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002304 sizeof(host_addr));
2305
2306 if (!is_eth_addr_valid(dev_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002307 pr_err("Need valid 'usbnet_devaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002308 goto fail;
2309 }
2310 if (!is_eth_addr_valid(host_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002311 pr_err("Need valid 'usbnet_hostaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002312 goto fail;
2313 }
2314
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302315 priv->eth_driver.speed = DEVSPEED;
2316 priv->eth_driver.bind = eth_bind;
2317 priv->eth_driver.unbind = eth_unbind;
2318 priv->eth_driver.setup = eth_setup;
2319 priv->eth_driver.reset = eth_disconnect;
2320 priv->eth_driver.disconnect = eth_disconnect;
2321 priv->eth_driver.suspend = eth_suspend;
2322 priv->eth_driver.resume = eth_resume;
2323 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen4af32732010-12-01 23:43:43 +08002324 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002325
2326 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002327
2328 packet_received = 0;
2329 packet_sent = 0;
2330
2331 gadget = dev->gadget;
2332 usb_gadget_connect(gadget);
2333
Simon Glass64b723f2017-08-03 12:22:12 -06002334 if (env_get("cdc_connect_timeout"))
Simon Glassff9b9032021-07-24 09:03:30 -06002335 timeout = dectoul(env_get("cdc_connect_timeout"), NULL) * CONFIG_SYS_HZ;
Remy Bohmerdf063442009-07-29 18:18:43 +02002336 ts = get_timer(0);
Mugunthan V N3963de32016-11-18 10:49:13 +05302337 while (!dev->network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002338 /* Handle control-c and timeouts */
2339 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002340 pr_err("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002341 goto fail;
2342 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302343 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002344 }
2345
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002346 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002347 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002348 return 0;
2349fail:
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002350 _usb_eth_halt(priv);
Remy Bohmerdf063442009-07-29 18:18:43 +02002351 return -1;
2352}
2353
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302354static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002355{
2356 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002357 void *rndis_pkt = NULL;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302358 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002359 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002360 unsigned long ts;
2361 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002362
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002363 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002364
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002365 /* new buffer is needed to include RNDIS header */
2366 if (rndis_active(dev)) {
2367 rndis_pkt = malloc(length +
2368 sizeof(struct rndis_packet_msg_type));
2369 if (!rndis_pkt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002370 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002371 goto drop;
2372 }
2373 rndis_add_hdr(rndis_pkt, length);
2374 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002375 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002376 packet = rndis_pkt;
2377 length += sizeof(struct rndis_packet_msg_type);
2378 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002379 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002380 req->context = NULL;
2381 req->complete = tx_complete;
2382
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002383 /*
2384 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002385 * though any robust network rx path ignores extra padding.
2386 * and some hardware doesn't like to write zlps.
2387 */
2388 req->zero = 1;
2389 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2390 length++;
2391
2392 req->length = length;
2393#if 0
2394 /* throttle highspeed IRQ rate back slightly */
2395 if (gadget_is_dualspeed(dev->gadget))
2396 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2397 ? ((dev->tx_qlen % qmult) != 0) : 0;
2398#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002399 dev->tx_qlen = 1;
2400 ts = get_timer(0);
2401 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002402
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002403 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002404
2405 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002406 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002407 while (!packet_sent) {
2408 if (get_timer(ts) > timeout) {
2409 printf("timeout sending packets to usb ethernet\n");
2410 return -1;
2411 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302412 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002413 }
Heinrich Schuchardt637ad632020-04-19 12:11:12 +02002414 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002415
2416 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002417drop:
2418 dev->stats.tx_dropped++;
2419 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002420}
2421
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302422static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002423{
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302424 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002425
Remy Bohmerdf063442009-07-29 18:18:43 +02002426 return 0;
2427}
2428
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002429static void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002430{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302431 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002432
Lei Wen4af32732010-12-01 23:43:43 +08002433 /* If the gadget not registered, simple return */
2434 if (!dev->gadget)
2435 return;
2436
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002437 /*
2438 * Some USB controllers may need additional deinitialization here
2439 * before dropping pull-up (also due to hardware issues).
2440 * For example: unhandled interrupt with status stage started may
2441 * bring the controller to fully broken state (until board reset).
2442 * There are some variants to debug and fix such cases:
2443 * 1) In the case of RNDIS connection eth_stop can perform additional
2444 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2445 * 2) 'pullup' callback in your UDC driver can be improved to perform
2446 * this deinitialization.
2447 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002448 eth_stop(dev);
2449
Remy Bohmerdf063442009-07-29 18:18:43 +02002450 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002451
2452 /* Clear pending interrupt */
2453 if (dev->network_started) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302454 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002455 dev->network_started = 0;
2456 }
2457
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302458 usb_gadget_unregister_driver(&priv->eth_driver);
Jean-Jacques Hiblot585e1992018-11-29 10:52:41 +01002459 usb_gadget_release(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002460}
2461
Mugunthan V N095b7612016-11-18 11:09:15 +05302462static int usb_eth_start(struct udevice *dev)
2463{
2464 struct ether_priv *priv = dev_get_priv(dev);
2465
2466 return _usb_eth_init(priv);
2467}
2468
2469static int usb_eth_send(struct udevice *dev, void *packet, int length)
2470{
2471 struct ether_priv *priv = dev_get_priv(dev);
2472
2473 return _usb_eth_send(priv, packet, length);
2474}
2475
2476static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2477{
2478 struct ether_priv *priv = dev_get_priv(dev);
2479 struct eth_dev *ethdev = &priv->ethdev;
2480 int ret;
2481
2482 ret = _usb_eth_recv(priv);
2483 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002484 pr_err("error packet receive\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302485 return ret;
2486 }
2487
2488 if (packet_received) {
2489 if (ethdev->rx_req) {
2490 *packetp = (uchar *)net_rx_packets[0];
2491 return ethdev->rx_req->length;
2492 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002493 pr_err("dev->rx_req invalid");
Mugunthan V N095b7612016-11-18 11:09:15 +05302494 return -EFAULT;
2495 }
2496 }
2497
2498 return -EAGAIN;
2499}
2500
2501static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2502 int length)
2503{
2504 struct ether_priv *priv = dev_get_priv(dev);
2505 struct eth_dev *ethdev = &priv->ethdev;
2506
2507 packet_received = 0;
2508
2509 return rx_submit(ethdev, ethdev->rx_req, 0);
2510}
2511
2512static void usb_eth_stop(struct udevice *dev)
2513{
2514 struct ether_priv *priv = dev_get_priv(dev);
2515
2516 _usb_eth_halt(priv);
2517}
2518
2519static int usb_eth_probe(struct udevice *dev)
2520{
2521 struct ether_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07002522 struct eth_pdata *pdata = dev_get_plat(dev);
Mugunthan V N095b7612016-11-18 11:09:15 +05302523
2524 priv->netdev = dev;
2525 l_priv = priv;
2526
Ignacio Zamora3cf84bb2022-10-08 09:50:56 -05002527 get_ether_addr(CONFIG_USBNET_DEV_ADDR, pdata->enetaddr);
Simon Glass8551d552017-08-03 12:22:11 -06002528 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
Mugunthan V N095b7612016-11-18 11:09:15 +05302529
2530 return 0;
2531}
2532
2533static const struct eth_ops usb_eth_ops = {
2534 .start = usb_eth_start,
2535 .send = usb_eth_send,
2536 .recv = usb_eth_recv,
2537 .free_pkt = usb_eth_free_pkt,
2538 .stop = usb_eth_stop,
2539};
2540
2541int usb_ether_init(void)
2542{
Mugunthan V N095b7612016-11-18 11:09:15 +05302543 struct udevice *usb_dev;
2544 int ret;
2545
Michal Suchaneka18b6a52022-10-12 21:57:54 +02002546 uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2547 if (!usb_dev) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002548 pr_err("No USB device found\n");
Michal Suchaneka18b6a52022-10-12 21:57:54 +02002549 return -ENODEV;
Mugunthan V N095b7612016-11-18 11:09:15 +05302550 }
2551
Michal Suchaneka18b6a52022-10-12 21:57:54 +02002552 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", NULL);
2553 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002554 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302555 return ret;
2556 }
2557
2558 return 0;
2559}
2560
2561U_BOOT_DRIVER(eth_usb) = {
2562 .name = "usb_ether",
2563 .id = UCLASS_ETH,
2564 .probe = usb_eth_probe,
2565 .ops = &usb_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07002566 .priv_auto = sizeof(struct ether_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07002567 .plat_auto = sizeof(struct eth_pdata),
Mugunthan V N095b7612016-11-18 11:09:15 +05302568 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2569};