blob: 36618f0bdf36ab0909642e1335bc312741a532ed [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>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <linux/printk.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020018#include <linux/usb/ch9.h>
19#include <linux/usb/cdc.h>
20#include <linux/usb/gadget.h>
21#include <net.h>
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +053022#include <usb.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030023#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060024#include <memalign.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020025#include <linux/ctype.h>
26
27#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030028#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020029
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053030#include <dm.h>
Mugunthan V N095b7612016-11-18 11:09:15 +053031#include <dm/lists.h>
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053032#include <dm/uclass-internal.h>
33#include <dm/device-internal.h>
34
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030035#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040036
Remy Bohmerdf063442009-07-29 18:18:43 +020037extern struct platform_data brd;
Remy Bohmerdf063442009-07-29 18:18:43 +020038
39
40unsigned packet_received, packet_sent;
41
Remy Bohmerdf063442009-07-29 18:18:43 +020042/*
43 * Ethernet gadget driver -- with CDC and non-CDC options
44 * Builds on hardware support for a full duplex link.
45 *
46 * CDC Ethernet is the standard USB solution for sending Ethernet frames
47 * using USB. Real hardware tends to use the same framing protocol but look
48 * different for control features. This driver strongly prefers to use
49 * this USB-IF standard as its open-systems interoperability solution;
50 * most host side USB stacks (except from Microsoft) support it.
51 *
52 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
53 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
54 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
55 *
56 * There's some hardware that can't talk CDC ECM. We make that hardware
57 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
58 * link-level setup only requires activating the configuration. Only the
59 * endpoint descriptors, and product/vendor IDs, are relevant; no control
60 * operations are available. Linux supports it, but other host operating
61 * systems may not. (This is a subset of CDC Ethernet.)
62 *
63 * It turns out that if you add a few descriptors to that "CDC Subset",
64 * (Windows) host side drivers from MCCI can treat it as one submode of
65 * a proprietary scheme called "SAFE" ... without needing to know about
66 * specific product/vendor IDs. So we do that, making it easier to use
67 * those MS-Windows drivers. Those added descriptors make it resemble a
68 * CDC MDLM device, but they don't change device behavior at all. (See
69 * MCCI Engineering report 950198 "SAFE Networking Functions".)
70 *
71 * A third option is also in use. Rather than CDC Ethernet, or something
72 * simpler, Microsoft pushes their own approach: RNDIS. The published
73 * RNDIS specs are ambiguous and appear to be incomplete, and are also
74 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
75 */
Remy Bohmerdf063442009-07-29 18:18:43 +020076
77#define DRIVER_DESC "Ethernet Gadget"
78/* Based on linux 2.6.27 version */
79#define DRIVER_VERSION "May Day 2005"
80
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040081static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020082
83#define RX_EXTRA 20 /* guard against rx overflows */
84
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030085#ifndef CONFIG_USB_ETH_RNDIS
86#define rndis_uninit(x) do {} while (0)
87#define rndis_deregister(c) do {} while (0)
88#define rndis_exit() do {} while (0)
89#endif
90
91/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +020092#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
93 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
94 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
95 |USB_CDC_PACKET_TYPE_DIRECTED)
96
97#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
98
99/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300100
101struct eth_dev {
102 struct usb_gadget *gadget;
103 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300104 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300105
106 u8 config;
107 struct usb_ep *in_ep, *out_ep, *status_ep;
108 const struct usb_endpoint_descriptor
109 *in, *out, *status;
110
111 struct usb_request *tx_req, *rx_req;
112
Mugunthan V N095b7612016-11-18 11:09:15 +0530113 struct udevice *net;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300114 struct net_device_stats stats;
115 unsigned int tx_qlen;
116
117 unsigned zlp:1;
118 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300119 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300120 unsigned suspended:1;
121 unsigned network_started:1;
122 u16 cdc_filter;
123 unsigned long todo;
124 int mtu;
125#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300126 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300127 u8 host_mac[ETH_ALEN];
128};
129
130/*
131 * This version autoconfigures as much as possible at run-time.
132 *
133 * It also ASSUMES a self-powered device, without remote wakeup,
134 * although remote wakeup support would make sense.
135 */
136
137/*-------------------------------------------------------------------------*/
Mugunthan V N88e48be2016-11-18 11:06:13 +0530138struct ether_priv {
139 struct eth_dev ethdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530140 struct udevice *netdev;
Mugunthan V N88e48be2016-11-18 11:06:13 +0530141 struct usb_gadget_driver eth_driver;
142};
143
144struct ether_priv eth_priv;
145struct ether_priv *l_priv = &eth_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +0200146
147/*-------------------------------------------------------------------------*/
148
149/* "main" config is either CDC, or its simple subset */
150static inline int is_cdc(struct eth_dev *dev)
151{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200152#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200153 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200154#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200155 return 0; /* only subset possible */
156#else
157 return dev->cdc; /* depends on what hardware we found */
158#endif
159}
160
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300161/* "secondary" RNDIS config may sometimes be activated */
162static inline int rndis_active(struct eth_dev *dev)
163{
164#ifdef CONFIG_USB_ETH_RNDIS
165 return dev->rndis;
166#else
167 return 0;
168#endif
169}
170
171#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
172#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200173
174#define DEFAULT_QLEN 2 /* double buffering by default */
175
176/* peak bulk transfer bits-per-second */
177#define HS_BPS (13 * 512 * 8 * 1000 * 8)
178#define FS_BPS (19 * 64 * 1 * 1000 * 8)
179
180#ifdef CONFIG_USB_GADGET_DUALSPEED
181#define DEVSPEED USB_SPEED_HIGH
182
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400183#ifdef CONFIG_USB_ETH_QMULT
184#define qmult CONFIG_USB_ETH_QMULT
185#else
186#define qmult 5
187#endif
188
Remy Bohmerdf063442009-07-29 18:18:43 +0200189/* for dual-speed hardware, use deeper queues at highspeed */
190#define qlen(gadget) \
191 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
192
193static inline int BITRATE(struct usb_gadget *g)
194{
195 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
196}
197
198#else /* full speed (low speed doesn't do bulk) */
199
200#define qmult 1
201
202#define DEVSPEED USB_SPEED_FULL
203
204#define qlen(gadget) DEFAULT_QLEN
205
206static inline int BITRATE(struct usb_gadget *g)
207{
208 return FS_BPS;
209}
210#endif
211
Remy Bohmerdf063442009-07-29 18:18:43 +0200212/*-------------------------------------------------------------------------*/
213
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400214/*
215 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200216 * Instead: allocate your own, using normal USB-IF procedures.
217 */
218
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400219/*
220 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200221 * It's for devices with only CDC Ethernet configurations.
222 */
223#define CDC_VENDOR_NUM 0x0525 /* NetChip */
224#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
225
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400226/*
227 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200228 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
229 * with pxa250. We're protocol-compatible, if the host-side drivers
230 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
231 * drivers that need to hard-wire endpoint numbers have a hook.
232 *
233 * The protocol is a minimal subset of CDC Ether, which works on any bulk
234 * hardware that's not deeply broken ... even on hardware that can't talk
235 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
236 * doesn't handle control-OUT).
237 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300238#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
239#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200240
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400241/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300242 * For hardware that can talk RNDIS and either of the above protocols,
243 * use this ID ... the windows INF files will know it. Unless it's
244 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
245 * the non-RNDIS configuration.
246 */
247#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
248#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
249
250/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400251 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200252 * device descriptor, either numbers or strings or both. These string
253 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
254 */
255
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300256/*
257 * Emulating them in eth_bind:
258 * static ushort idVendor;
259 * static ushort idProduct;
260 */
261
Maxime Ripard48e78f82017-09-07 09:15:08 +0200262#if defined(CONFIG_USB_GADGET_MANUFACTURER)
263static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmerdf063442009-07-29 18:18:43 +0200264#else
Bin Meng75574052016-02-05 19:30:11 -0800265static char *iManufacturer = "U-Boot";
Remy Bohmerdf063442009-07-29 18:18:43 +0200266#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300267
268/* These probably need to be configurable. */
269static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200270static char *iProduct;
271static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300272
Remy Bohmerdf063442009-07-29 18:18:43 +0200273static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300274
Remy Bohmerdf063442009-07-29 18:18:43 +0200275static char host_addr[18];
276
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300277
Remy Bohmerdf063442009-07-29 18:18:43 +0200278/*-------------------------------------------------------------------------*/
279
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400280/*
281 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200282 * ep0 implementation: descriptors, config management, setup().
283 * also optional class-specific notification interrupt transfer.
284 */
285
286/*
287 * DESCRIPTORS ... most are static, but strings and (full) configuration
288 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300289 * our simple subset, with RNDIS as an optional second configuration.
290 *
291 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
292 * the class descriptors match a modem (they're ignored; it's really just
293 * Ethernet functionality), they don't need the NOP altsetting, and the
294 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200295 */
296
297#define STRING_MANUFACTURER 1
298#define STRING_PRODUCT 2
299#define STRING_ETHADDR 3
300#define STRING_DATA 4
301#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300302#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200303#define STRING_CDC 7
304#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300305#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200306#define STRING_SERIALNUMBER 10
307
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300308/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200309#define USB_BUFSIZ 256
310
311/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300312 * This device advertises one configuration, eth_config, unless RNDIS
313 * is enabled (rndis_config) on hardware supporting at least two configs.
314 *
315 * NOTE: Controllers like superh_udc should probably be able to use
316 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200317 *
318 * FIXME define some higher-powered configurations to make it easier
319 * to recharge batteries ...
320 */
321
322#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300323#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200324
325static struct usb_device_descriptor
326device_desc = {
327 .bLength = sizeof device_desc,
328 .bDescriptorType = USB_DT_DEVICE,
329
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400330 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200331
332 .bDeviceClass = USB_CLASS_COMM,
333 .bDeviceSubClass = 0,
334 .bDeviceProtocol = 0,
335
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400336 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
337 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200338 .iManufacturer = STRING_MANUFACTURER,
339 .iProduct = STRING_PRODUCT,
340 .bNumConfigurations = 1,
341};
342
343static struct usb_otg_descriptor
344otg_descriptor = {
345 .bLength = sizeof otg_descriptor,
346 .bDescriptorType = USB_DT_OTG,
347
348 .bmAttributes = USB_OTG_SRP,
349};
350
351static struct usb_config_descriptor
352eth_config = {
353 .bLength = sizeof eth_config,
354 .bDescriptorType = USB_DT_CONFIG,
355
356 /* compute wTotalLength on the fly */
357 .bNumInterfaces = 2,
358 .bConfigurationValue = DEV_CONFIG_VALUE,
359 .iConfiguration = STRING_CDC,
360 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
361 .bMaxPower = 1,
362};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300363
364#ifdef CONFIG_USB_ETH_RNDIS
365static struct usb_config_descriptor
366rndis_config = {
367 .bLength = sizeof rndis_config,
368 .bDescriptorType = USB_DT_CONFIG,
369
370 /* compute wTotalLength on the fly */
371 .bNumInterfaces = 2,
372 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
373 .iConfiguration = STRING_RNDIS,
374 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
375 .bMaxPower = 1,
376};
377#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200378
379/*
380 * Compared to the simple CDC subset, the full CDC Ethernet model adds
381 * three class descriptors, two interface descriptors, optional status
382 * endpoint. Both have a "data" interface and two bulk endpoints.
383 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300384 *
385 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
386 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
387 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
388 * work around those bugs) are unlikely to ever come from MSFT, you may
389 * wish to avoid using RNDIS.
390 *
391 * MCCI offers an alternative to RNDIS if you need to connect to Windows
392 * but have hardware that can't support CDC Ethernet. We add descriptors
393 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
394 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
395 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200396 */
397
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200398#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200399static struct usb_interface_descriptor
400control_intf = {
401 .bLength = sizeof control_intf,
402 .bDescriptorType = USB_DT_INTERFACE,
403
404 .bInterfaceNumber = 0,
405 /* status endpoint is optional; this may be patched later */
406 .bNumEndpoints = 1,
407 .bInterfaceClass = USB_CLASS_COMM,
408 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
409 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
410 .iInterface = STRING_CONTROL,
411};
412#endif
413
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300414#ifdef CONFIG_USB_ETH_RNDIS
415static const struct usb_interface_descriptor
416rndis_control_intf = {
417 .bLength = sizeof rndis_control_intf,
418 .bDescriptorType = USB_DT_INTERFACE,
419
420 .bInterfaceNumber = 0,
421 .bNumEndpoints = 1,
422 .bInterfaceClass = USB_CLASS_COMM,
423 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
424 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
425 .iInterface = STRING_RNDIS_CONTROL,
426};
427#endif
428
Remy Bohmerdf063442009-07-29 18:18:43 +0200429static const struct usb_cdc_header_desc header_desc = {
430 .bLength = sizeof header_desc,
431 .bDescriptorType = USB_DT_CS_INTERFACE,
432 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
433
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400434 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200435};
436
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200437#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200438
439static const struct usb_cdc_union_desc union_desc = {
440 .bLength = sizeof union_desc,
441 .bDescriptorType = USB_DT_CS_INTERFACE,
442 .bDescriptorSubType = USB_CDC_UNION_TYPE,
443
444 .bMasterInterface0 = 0, /* index of control interface */
445 .bSlaveInterface0 = 1, /* index of DATA interface */
446};
447
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300448#endif /* CDC || RNDIS */
449
450#ifdef CONFIG_USB_ETH_RNDIS
451
452static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
453 .bLength = sizeof call_mgmt_descriptor,
454 .bDescriptorType = USB_DT_CS_INTERFACE,
455 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
456
457 .bmCapabilities = 0x00,
458 .bDataInterface = 0x01,
459};
460
461static const struct usb_cdc_acm_descriptor acm_descriptor = {
462 .bLength = sizeof acm_descriptor,
463 .bDescriptorType = USB_DT_CS_INTERFACE,
464 .bDescriptorSubType = USB_CDC_ACM_TYPE,
465
466 .bmCapabilities = 0x00,
467};
468
469#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200470
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200471#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200472
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400473/*
474 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200475 * ways: data endpoints live in the control interface, there's no data
476 * interface, and it's not used to talk to a cell phone radio.
477 */
478
479static const struct usb_cdc_mdlm_desc mdlm_desc = {
480 .bLength = sizeof mdlm_desc,
481 .bDescriptorType = USB_DT_CS_INTERFACE,
482 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
483
484 .bcdVersion = __constant_cpu_to_le16(0x0100),
485 .bGUID = {
486 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
487 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
488 },
489};
490
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400491/*
492 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200493 * can't really use its struct. All we do here is say that we're using
494 * the submode of "SAFE" which directly matches the CDC Subset.
495 */
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530496#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200497static const u8 mdlm_detail_desc[] = {
498 6,
499 USB_DT_CS_INTERFACE,
500 USB_CDC_MDLM_DETAIL_TYPE,
501
502 0, /* "SAFE" */
503 0, /* network control capabilities (none) */
504 0, /* network data capabilities ("raw" encapsulation) */
505};
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530506#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200507
508#endif
509
Remy Bohmerdf063442009-07-29 18:18:43 +0200510static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400511 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200512 .bDescriptorType = USB_DT_CS_INTERFACE,
513 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
514
515 /* this descriptor actually adds value, surprise! */
516 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400517 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
Bin Meng2ccb8b02018-07-31 02:55:22 -0700518 .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN),
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400519 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200520 .bNumberPowerFilters = 0,
521};
522
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200523#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200524
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400525/*
526 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200527 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
528 * packet, to simplify cancellation; and a big transfer interval, to
529 * waste less bandwidth.
530 *
531 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
532 * if they ignore the connect/disconnect notifications that real aether
533 * can provide. more advanced cdc configurations might want to support
534 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300535 *
536 * RNDIS requires the status endpoint, since it uses that encapsulation
537 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200538 */
539
540#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
541#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
542
543static struct usb_endpoint_descriptor
544fs_status_desc = {
545 .bLength = USB_DT_ENDPOINT_SIZE,
546 .bDescriptorType = USB_DT_ENDPOINT,
547
548 .bEndpointAddress = USB_DIR_IN,
549 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400550 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200551 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
552};
553#endif
554
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200555#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200556
557/* the default data interface has no endpoints ... */
558
559static const struct usb_interface_descriptor
560data_nop_intf = {
561 .bLength = sizeof data_nop_intf,
562 .bDescriptorType = USB_DT_INTERFACE,
563
564 .bInterfaceNumber = 1,
565 .bAlternateSetting = 0,
566 .bNumEndpoints = 0,
567 .bInterfaceClass = USB_CLASS_CDC_DATA,
568 .bInterfaceSubClass = 0,
569 .bInterfaceProtocol = 0,
570};
571
572/* ... but the "real" data interface has two bulk endpoints */
573
574static const struct usb_interface_descriptor
575data_intf = {
576 .bLength = sizeof data_intf,
577 .bDescriptorType = USB_DT_INTERFACE,
578
579 .bInterfaceNumber = 1,
580 .bAlternateSetting = 1,
581 .bNumEndpoints = 2,
582 .bInterfaceClass = USB_CLASS_CDC_DATA,
583 .bInterfaceSubClass = 0,
584 .bInterfaceProtocol = 0,
585 .iInterface = STRING_DATA,
586};
587
588#endif
589
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300590#ifdef CONFIG_USB_ETH_RNDIS
591
592/* RNDIS doesn't activate by changing to the "real" altsetting */
593
594static const struct usb_interface_descriptor
595rndis_data_intf = {
596 .bLength = sizeof rndis_data_intf,
597 .bDescriptorType = USB_DT_INTERFACE,
598
599 .bInterfaceNumber = 1,
600 .bAlternateSetting = 0,
601 .bNumEndpoints = 2,
602 .bInterfaceClass = USB_CLASS_CDC_DATA,
603 .bInterfaceSubClass = 0,
604 .bInterfaceProtocol = 0,
605 .iInterface = STRING_DATA,
606};
607
608#endif
609
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200610#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200611
612/*
613 * "Simple" CDC-subset option is a simple vendor-neutral model that most
614 * full speed controllers can handle: one interface, two bulk endpoints.
615 *
616 * To assist host side drivers, we fancy it up a bit, and add descriptors
617 * so some host side drivers will understand it as a "SAFE" variant.
618 */
619
620static const struct usb_interface_descriptor
621subset_data_intf = {
622 .bLength = sizeof subset_data_intf,
623 .bDescriptorType = USB_DT_INTERFACE,
624
625 .bInterfaceNumber = 0,
626 .bAlternateSetting = 0,
627 .bNumEndpoints = 2,
628 .bInterfaceClass = USB_CLASS_COMM,
629 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
630 .bInterfaceProtocol = 0,
631 .iInterface = STRING_DATA,
632};
633
634#endif /* SUBSET */
635
Remy Bohmerdf063442009-07-29 18:18:43 +0200636static struct usb_endpoint_descriptor
637fs_source_desc = {
638 .bLength = USB_DT_ENDPOINT_SIZE,
639 .bDescriptorType = USB_DT_ENDPOINT,
640
641 .bEndpointAddress = USB_DIR_IN,
642 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700643 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200644};
645
646static struct usb_endpoint_descriptor
647fs_sink_desc = {
648 .bLength = USB_DT_ENDPOINT_SIZE,
649 .bDescriptorType = USB_DT_ENDPOINT,
650
651 .bEndpointAddress = USB_DIR_OUT,
652 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700653 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200654};
655
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400656static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200657 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200658#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200659 /* "cdc" mode descriptors */
660 (struct usb_descriptor_header *) &control_intf,
661 (struct usb_descriptor_header *) &header_desc,
662 (struct usb_descriptor_header *) &union_desc,
663 (struct usb_descriptor_header *) &ether_desc,
664 /* NOTE: status endpoint may need to be removed */
665 (struct usb_descriptor_header *) &fs_status_desc,
666 /* data interface, with altsetting */
667 (struct usb_descriptor_header *) &data_nop_intf,
668 (struct usb_descriptor_header *) &data_intf,
669 (struct usb_descriptor_header *) &fs_source_desc,
670 (struct usb_descriptor_header *) &fs_sink_desc,
671 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200672#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200673};
674
675static inline void fs_subset_descriptors(void)
676{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200677#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200678 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
679 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
680 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
681 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
682 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
683 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
684 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
685 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
686 fs_eth_function[8] = NULL;
687#else
688 fs_eth_function[1] = NULL;
689#endif
690}
691
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300692#ifdef CONFIG_USB_ETH_RNDIS
693static const struct usb_descriptor_header *fs_rndis_function[] = {
694 (struct usb_descriptor_header *) &otg_descriptor,
695 /* control interface matches ACM, not Ethernet */
696 (struct usb_descriptor_header *) &rndis_control_intf,
697 (struct usb_descriptor_header *) &header_desc,
698 (struct usb_descriptor_header *) &call_mgmt_descriptor,
699 (struct usb_descriptor_header *) &acm_descriptor,
700 (struct usb_descriptor_header *) &union_desc,
701 (struct usb_descriptor_header *) &fs_status_desc,
702 /* data interface has no altsetting */
703 (struct usb_descriptor_header *) &rndis_data_intf,
704 (struct usb_descriptor_header *) &fs_source_desc,
705 (struct usb_descriptor_header *) &fs_sink_desc,
706 NULL,
707};
708#endif
709
Remy Bohmerdf063442009-07-29 18:18:43 +0200710/*
711 * usb 2.0 devices need to expose both high speed and full speed
712 * descriptors, unless they only run at full speed.
713 */
714
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200715#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200716static struct usb_endpoint_descriptor
717hs_status_desc = {
718 .bLength = USB_DT_ENDPOINT_SIZE,
719 .bDescriptorType = USB_DT_ENDPOINT,
720
721 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400722 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200723 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
724};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200725#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200726
727static struct usb_endpoint_descriptor
728hs_source_desc = {
729 .bLength = USB_DT_ENDPOINT_SIZE,
730 .bDescriptorType = USB_DT_ENDPOINT,
731
732 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400733 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200734};
735
736static struct usb_endpoint_descriptor
737hs_sink_desc = {
738 .bLength = USB_DT_ENDPOINT_SIZE,
739 .bDescriptorType = USB_DT_ENDPOINT,
740
741 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400742 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200743};
744
745static struct usb_qualifier_descriptor
746dev_qualifier = {
747 .bLength = sizeof dev_qualifier,
748 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
749
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400750 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200751 .bDeviceClass = USB_CLASS_COMM,
752
753 .bNumConfigurations = 1,
754};
755
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400756static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200757 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200758#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200759 /* "cdc" mode descriptors */
760 (struct usb_descriptor_header *) &control_intf,
761 (struct usb_descriptor_header *) &header_desc,
762 (struct usb_descriptor_header *) &union_desc,
763 (struct usb_descriptor_header *) &ether_desc,
764 /* NOTE: status endpoint may need to be removed */
765 (struct usb_descriptor_header *) &hs_status_desc,
766 /* data interface, with altsetting */
767 (struct usb_descriptor_header *) &data_nop_intf,
768 (struct usb_descriptor_header *) &data_intf,
769 (struct usb_descriptor_header *) &hs_source_desc,
770 (struct usb_descriptor_header *) &hs_sink_desc,
771 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200772#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200773};
774
775static inline void hs_subset_descriptors(void)
776{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200777#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200778 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
779 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
780 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
781 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
782 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
783 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
784 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
785 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
786 hs_eth_function[8] = NULL;
787#else
788 hs_eth_function[1] = NULL;
789#endif
790}
791
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300792#ifdef CONFIG_USB_ETH_RNDIS
793static const struct usb_descriptor_header *hs_rndis_function[] = {
794 (struct usb_descriptor_header *) &otg_descriptor,
795 /* control interface matches ACM, not Ethernet */
796 (struct usb_descriptor_header *) &rndis_control_intf,
797 (struct usb_descriptor_header *) &header_desc,
798 (struct usb_descriptor_header *) &call_mgmt_descriptor,
799 (struct usb_descriptor_header *) &acm_descriptor,
800 (struct usb_descriptor_header *) &union_desc,
801 (struct usb_descriptor_header *) &hs_status_desc,
802 /* data interface has no altsetting */
803 (struct usb_descriptor_header *) &rndis_data_intf,
804 (struct usb_descriptor_header *) &hs_source_desc,
805 (struct usb_descriptor_header *) &hs_sink_desc,
806 NULL,
807};
808#endif
809
810
Remy Bohmerdf063442009-07-29 18:18:43 +0200811/* maxpacket and other transfer characteristics vary by speed. */
812static inline struct usb_endpoint_descriptor *
813ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
814 struct usb_endpoint_descriptor *fs)
815{
816 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
817 return hs;
818 return fs;
819}
820
Remy Bohmerdf063442009-07-29 18:18:43 +0200821/*-------------------------------------------------------------------------*/
822
823/* descriptors that are built on-demand */
824
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400825static char manufacturer[50];
826static char product_desc[40] = DRIVER_DESC;
827static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200828
829/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400830static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200831
832/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400833static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200834 { STRING_MANUFACTURER, manufacturer, },
835 { STRING_PRODUCT, product_desc, },
836 { STRING_SERIALNUMBER, serial_number, },
837 { STRING_DATA, "Ethernet Data", },
838 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200839#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200840 { STRING_CDC, "CDC Ethernet", },
841 { STRING_CONTROL, "CDC Communications Control", },
842#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200843#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200844 { STRING_SUBSET, "CDC Ethernet Subset", },
845#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300846#ifdef CONFIG_USB_ETH_RNDIS
847 { STRING_RNDIS, "RNDIS", },
848 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
849#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200850 { } /* end of list */
851};
852
853static struct usb_gadget_strings stringtab = {
854 .language = 0x0409, /* en-us */
855 .strings = strings,
856};
857
Remy Bohmerdf063442009-07-29 18:18:43 +0200858/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500859DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
860
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200861#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500862DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200863#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200864
Remy Bohmerdf063442009-07-29 18:18:43 +0200865/*============================================================================*/
866
867/*
868 * one config, two interfaces: control, data.
869 * complications: class descriptors, and an altsetting.
870 */
871static int
872config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
873{
874 int len;
875 const struct usb_config_descriptor *config;
876 const struct usb_descriptor_header **function;
877 int hs = 0;
878
879 if (gadget_is_dualspeed(g)) {
880 hs = (g->speed == USB_SPEED_HIGH);
881 if (type == USB_DT_OTHER_SPEED_CONFIG)
882 hs = !hs;
883 }
884#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
885
886 if (index >= device_desc.bNumConfigurations)
887 return -EINVAL;
888
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300889#ifdef CONFIG_USB_ETH_RNDIS
890 /*
891 * list the RNDIS config first, to make Microsoft's drivers
892 * happy. DOCSIS 1.0 needs this too.
893 */
894 if (device_desc.bNumConfigurations == 2 && index == 0) {
895 config = &rndis_config;
896 function = which_fn(rndis);
897 } else
898#endif
899 {
900 config = &eth_config;
901 function = which_fn(eth);
902 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200903
904 /* for now, don't advertise srp-only devices */
905 if (!is_otg)
906 function++;
907
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400908 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200909 if (len < 0)
910 return len;
911 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
912 return len;
913}
914
915/*-------------------------------------------------------------------------*/
916
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300917static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400918static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200919
920static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400921set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200922{
923 int result = 0;
924 struct usb_gadget *gadget = dev->gadget;
925
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200926#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300927 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200928 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400929 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200930 &fs_status_desc);
931 dev->status_ep->driver_data = dev;
932
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400933 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200934 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400935 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200936 dev->status_ep->name, result);
937 goto done;
938 }
939 }
940#endif
941
942 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
943 dev->in_ep->driver_data = dev;
944
945 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
946 dev->out_ep->driver_data = dev;
947
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400948 /*
949 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200950 * endpoints in the default altsetting for the interface.
951 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300952 *
953 * Strictly speaking RNDIS should work the same: activation is
954 * a side effect of setting a packet filter. Deactivation is
955 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200956 */
957 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400958 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200959 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400960 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200961 dev->in_ep->name, result);
962 goto done;
963 }
964
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400965 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200966 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400967 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200968 dev->out_ep->name, result);
969 goto done;
970 }
971 }
972
973done:
974 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400975 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200976
977 /* on error, disable any endpoints */
978 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400979 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400980 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200981 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400982 (void) usb_ep_disable(dev->in_ep);
983 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200984 dev->in = NULL;
985 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300986 } else if (!cdc_active(dev)) {
987 /*
988 * activate non-CDC configs right away
989 * this isn't strictly according to the RNDIS spec
990 */
991 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +0200992 }
993
994 /* caller is responsible for cleanup on error */
995 return result;
996}
997
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400998static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +0200999{
1000 if (dev->config == 0)
1001 return;
1002
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001003 debug("%s\n", __func__);
1004
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001005 rndis_uninit(dev->rndis_config);
1006
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001007 /*
1008 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001009 * pending i/o. then free the requests.
1010 */
1011
1012 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001013 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001014 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001015 usb_ep_free_request(dev->in_ep, dev->tx_req);
1016 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001017 }
1018 }
1019 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001020 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001021 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001022 usb_ep_free_request(dev->out_ep, dev->rx_req);
1023 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001024 }
1025 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001026 if (dev->status)
1027 usb_ep_disable(dev->status_ep);
1028
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001029 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001030 dev->cdc_filter = 0;
1031 dev->config = 0;
1032}
1033
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001034/*
1035 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001036 * that returns config descriptors, and altsetting code.
1037 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001038static int eth_set_config(struct eth_dev *dev, unsigned number,
1039 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001040{
1041 int result = 0;
1042 struct usb_gadget *gadget = dev->gadget;
1043
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001044 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001045
1046 switch (number) {
1047 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001048 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001049 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001050#ifdef CONFIG_USB_ETH_RNDIS
1051 case DEV_RNDIS_CONFIG_VALUE:
1052 dev->rndis = 1;
1053 result = set_ether_config(dev, gfp_flags);
1054 break;
1055#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001056 default:
1057 result = -EINVAL;
1058 /* FALL THROUGH */
1059 case 0:
1060 break;
1061 }
1062
1063 if (result) {
1064 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001065 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001066 usb_gadget_vbus_draw(dev->gadget,
1067 gadget_is_otg(dev->gadget) ? 8 : 100);
1068 } else {
1069 char *speed;
1070 unsigned power;
1071
1072 power = 2 * eth_config.bMaxPower;
1073 usb_gadget_vbus_draw(dev->gadget, power);
1074
1075 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001076 case USB_SPEED_FULL:
1077 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001078#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001079 case USB_SPEED_HIGH:
1080 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001081#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001082 default:
1083 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001084 }
1085
1086 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001087 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001088 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001089 rndis_active(dev)
1090 ? "RNDIS"
1091 : (cdc_active(dev)
1092 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001093 : "CDC Ethernet Subset"));
1094 }
1095 return result;
1096}
1097
1098/*-------------------------------------------------------------------------*/
1099
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001100#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001101
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001102/*
1103 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001104 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001105 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001106 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1107 * command mechanism; and only one status request is ever queued.
1108 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001109static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001110{
1111 struct usb_cdc_notification *event = req->buf;
1112 int value = req->status;
1113 struct eth_dev *dev = ep->driver_data;
1114
1115 /* issue the second notification if host reads the first */
1116 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1117 && value == 0) {
1118 __le32 *data = req->buf + sizeof *event;
1119
1120 event->bmRequestType = 0xA1;
1121 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001122 event->wValue = __constant_cpu_to_le16(0);
1123 event->wIndex = __constant_cpu_to_le16(1);
1124 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001125
1126 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001127 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001128
1129 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001130 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001131 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001132 if (value == 0)
1133 return;
1134 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001135 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001136 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001137 if (event->bNotificationType ==
1138 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301139 dev->network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001140 printf("USB network up!\n");
1141 }
1142 }
1143 req->context = NULL;
1144}
1145
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001146static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001147{
1148 struct usb_request *req = dev->stat_req;
1149 struct usb_cdc_notification *event;
1150 int value;
1151
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001152 /*
1153 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001154 *
1155 * FIXME ugly idiom, maybe we'd be better with just
1156 * a "cancel the whole queue" primitive since any
1157 * unlink-one primitive has way too many error modes.
1158 * here, we "know" toggle is already clear...
1159 *
1160 * FIXME iff req->context != null just dequeue it
1161 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001162 usb_ep_disable(dev->status_ep);
1163 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001164
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001165 /*
1166 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001167 * a SPEED_CHANGE. could be useful in some configs.
1168 */
1169 event = req->buf;
1170 event->bmRequestType = 0xA1;
1171 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001172 event->wValue = __constant_cpu_to_le16(1); /* connected */
1173 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001174 event->wLength = 0;
1175
1176 req->length = sizeof *event;
1177 req->complete = eth_status_complete;
1178 req->context = dev;
1179
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001180 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001181 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001182 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001183}
1184
1185#endif
1186
1187/*-------------------------------------------------------------------------*/
1188
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001189static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001190{
1191 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001192 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001193 req->status, req->actual, req->length);
1194}
1195
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001196#ifdef CONFIG_USB_ETH_RNDIS
1197
1198static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1199{
1200 if (req->status || req->actual != req->length)
1201 debug("rndis response complete --> %d, %d/%d\n",
1202 req->status, req->actual, req->length);
1203
1204 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1205}
1206
1207static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1208{
1209 struct eth_dev *dev = ep->driver_data;
1210 int status;
1211
1212 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1213 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1214 if (status < 0)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001215 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001216}
1217
1218#endif /* RNDIS */
1219
Remy Bohmerdf063442009-07-29 18:18:43 +02001220/*
1221 * The setup() callback implements all the ep0 functionality that's not
1222 * handled lower down. CDC has a number of less-common features:
1223 *
1224 * - two interfaces: control, and ethernet data
1225 * - Ethernet data interface has two altsettings: default, and active
1226 * - class-specific descriptors for the control interface
1227 * - class-specific control requests
1228 */
1229static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001230eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001231{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001232 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001233 struct usb_request *req = dev->req;
1234 int value = -EOPNOTSUPP;
1235 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1236 u16 wValue = le16_to_cpu(ctrl->wValue);
1237 u16 wLength = le16_to_cpu(ctrl->wLength);
1238
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001239 /*
1240 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001241 * while config change events may enable network traffic.
1242 */
1243
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001244 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001245
1246 req->complete = eth_setup_complete;
1247 switch (ctrl->bRequest) {
1248
1249 case USB_REQ_GET_DESCRIPTOR:
1250 if (ctrl->bRequestType != USB_DIR_IN)
1251 break;
1252 switch (wValue >> 8) {
1253
1254 case USB_DT_DEVICE:
Kishon Vijay Abraham I58d701e2015-02-23 18:40:17 +05301255 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001256 value = min(wLength, (u16) sizeof device_desc);
1257 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001258 break;
1259 case USB_DT_DEVICE_QUALIFIER:
1260 if (!gadget_is_dualspeed(gadget))
1261 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001262 value = min(wLength, (u16) sizeof dev_qualifier);
1263 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001264 break;
1265
1266 case USB_DT_OTHER_SPEED_CONFIG:
1267 if (!gadget_is_dualspeed(gadget))
1268 break;
1269 /* FALLTHROUGH */
1270 case USB_DT_CONFIG:
1271 value = config_buf(gadget, req->buf,
1272 wValue >> 8,
1273 wValue & 0xff,
1274 gadget_is_otg(gadget));
1275 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001276 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001277 break;
1278
1279 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001280 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001281 wValue & 0xff, req->buf);
1282
1283 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001284 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001285
1286 break;
1287 }
1288 break;
1289
1290 case USB_REQ_SET_CONFIGURATION:
1291 if (ctrl->bRequestType != 0)
1292 break;
1293 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001294 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001295 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001296 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001297 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001298 break;
1299 case USB_REQ_GET_CONFIGURATION:
1300 if (ctrl->bRequestType != USB_DIR_IN)
1301 break;
1302 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001303 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001304 break;
1305
1306 case USB_REQ_SET_INTERFACE:
1307 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1308 || !dev->config
1309 || wIndex > 1)
1310 break;
1311 if (!cdc_active(dev) && wIndex != 0)
1312 break;
1313
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001314#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001315 switch (wIndex) {
1316 case 0: /* control/master intf */
1317 if (wValue != 0)
1318 break;
1319 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001320 usb_ep_disable(dev->status_ep);
1321 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001322 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001323
Remy Bohmerdf063442009-07-29 18:18:43 +02001324 value = 0;
1325 break;
1326 case 1: /* data intf */
1327 if (wValue > 1)
1328 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001329 usb_ep_disable(dev->in_ep);
1330 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001331
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001332 /*
1333 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001334 * the default interface setting ... also, setting
1335 * the non-default interface resets filters etc.
1336 */
1337 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001338 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001339 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001340 usb_ep_enable(dev->in_ep, dev->in);
1341 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001342 dev->cdc_filter = DEFAULT_FILTER;
1343 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001344 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001345 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001346 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001347 value = 0;
1348 break;
1349 }
1350#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001351 /*
1352 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001353 * all non-PXA hardware talks real CDC ...
1354 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001355 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001356#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001357 break;
1358 case USB_REQ_GET_INTERFACE:
1359 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1360 || !dev->config
1361 || wIndex > 1)
1362 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001363 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001364 break;
1365
1366 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001367 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001368 *(u8 *)req->buf = 0;
1369 else {
1370 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1371 /* carrier always ok ...*/
1372 *(u8 *)req->buf = 1 ;
1373 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001374 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001375 break;
1376
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001377#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001378 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001379 /*
1380 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001381 * wValue = packet filter bitmap
1382 */
1383 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1384 || !cdc_active(dev)
1385 || wLength != 0
1386 || wIndex > 1)
1387 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001388 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001389 dev->cdc_filter = wValue;
1390 value = 0;
1391 break;
1392
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001393 /*
1394 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001395 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1396 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1397 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1398 * case USB_CDC_GET_ETHERNET_STATISTIC:
1399 */
1400
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001401#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001402
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001403#ifdef CONFIG_USB_ETH_RNDIS
1404 /*
1405 * RNDIS uses the CDC command encapsulation mechanism to implement
1406 * an RPC scheme, with much getting/setting of attributes by OID.
1407 */
1408 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1409 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1410 || !rndis_active(dev)
1411 || wLength > USB_BUFSIZ
1412 || wValue
1413 || rndis_control_intf.bInterfaceNumber
1414 != wIndex)
1415 break;
1416 /* read the request, then process it */
1417 value = wLength;
1418 req->complete = rndis_command_complete;
1419 /* later, rndis_control_ack () sends a notification */
1420 break;
1421
1422 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1423 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1424 == ctrl->bRequestType
1425 && rndis_active(dev)
1426 /* && wLength >= 0x0400 */
1427 && !wValue
1428 && rndis_control_intf.bInterfaceNumber
1429 == wIndex) {
1430 u8 *buf;
1431 u32 n;
1432
1433 /* return the result */
1434 buf = rndis_get_next_response(dev->rndis_config, &n);
1435 if (buf) {
1436 memcpy(req->buf, buf, n);
1437 req->complete = rndis_response_complete;
1438 rndis_free_response(dev->rndis_config, buf);
1439 value = n;
1440 }
1441 /* else stalls ... spec says to avoid that */
1442 }
1443 break;
1444#endif /* RNDIS */
1445
Remy Bohmerdf063442009-07-29 18:18:43 +02001446 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001447 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001448 ctrl->bRequestType, ctrl->bRequest,
1449 wValue, wIndex, wLength);
1450 }
1451
1452 /* respond with data transfer before status phase? */
1453 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001454 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001455 req->length = value;
1456 req->zero = value < wLength
1457 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001458 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001459 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001460 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001461 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001462 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001463 }
1464 }
1465
1466 /* host either stalls (value < 0) or reports success */
1467 return value;
1468}
1469
Remy Bohmerdf063442009-07-29 18:18:43 +02001470/*-------------------------------------------------------------------------*/
1471
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001472static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001473
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001474static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001475 gfp_t gfp_flags)
1476{
1477 int retval = -ENOMEM;
1478 size_t size;
1479
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001480 /*
1481 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001482 * Normally we use the USB "terminate on short read" convention;
1483 * so allow up to (N*maxpacket), since that memory is normally
1484 * already allocated. Some hardware doesn't deal well with short
1485 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1486 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001487 *
1488 * RNDIS uses internal framing, and explicitly allows senders to
1489 * pad to end-of-packet. That's potentially nice for speed,
1490 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001491 */
1492
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001493 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001494 if (!req)
1495 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001496
1497 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1498 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001499 if (rndis_active(dev))
1500 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001501 size -= size % dev->out_ep->maxpacket;
1502
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001503 /*
1504 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001505 * but on at least one, checksumming fails otherwise. Note:
1506 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001507 */
1508
Joe Hershberger9f09a362015-04-08 01:41:06 -05001509 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmerdf063442009-07-29 18:18:43 +02001510 req->length = size;
1511 req->complete = rx_complete;
1512
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001513 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001514
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001515 if (retval)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001516 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001517
Remy Bohmerdf063442009-07-29 18:18:43 +02001518 return retval;
1519}
1520
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001521static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001522{
1523 struct eth_dev *dev = ep->driver_data;
1524
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001525 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001526 switch (req->status) {
1527 /* normal completion */
1528 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001529 if (rndis_active(dev)) {
1530 /* we know MaxPacketsPerTransfer == 1 here */
1531 int length = rndis_rm_hdr(req->buf, req->actual);
1532 if (length < 0)
1533 goto length_err;
1534 req->length -= length;
1535 req->actual -= length;
1536 }
Bin Meng2ccb8b02018-07-31 02:55:22 -07001537 if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001538length_err:
1539 dev->stats.rx_errors++;
1540 dev->stats.rx_length_errors++;
1541 debug("rx length %d\n", req->length);
1542 break;
1543 }
1544
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001545 dev->stats.rx_packets++;
1546 dev->stats.rx_bytes += req->length;
1547 break;
1548
1549 /* software-driven interface shutdown */
1550 case -ECONNRESET: /* unlink */
1551 case -ESHUTDOWN: /* disconnect etc */
1552 /* for hardware automagic (such as pxa) */
1553 case -ECONNABORTED: /* endpoint reset */
1554 break;
1555
1556 /* data overrun */
1557 case -EOVERFLOW:
1558 dev->stats.rx_over_errors++;
1559 /* FALLTHROUGH */
1560 default:
1561 dev->stats.rx_errors++;
1562 break;
1563 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001564
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001565 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001566}
1567
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001568static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001569{
1570
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001571 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001572
1573 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001574 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001575
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001576 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001577
1578 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001579 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001580
1581 return 0;
1582
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001583fail2:
1584 usb_ep_free_request(dev->in_ep, dev->tx_req);
1585fail1:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001586 pr_err("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001587 return -1;
1588}
1589
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001590static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001591{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001592 struct eth_dev *dev = ep->driver_data;
1593
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001594 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001595 switch (req->status) {
1596 default:
1597 dev->stats.tx_errors++;
1598 debug("tx err %d\n", req->status);
1599 /* FALLTHROUGH */
1600 case -ECONNRESET: /* unlink */
1601 case -ESHUTDOWN: /* disconnect etc */
1602 break;
1603 case 0:
1604 dev->stats.tx_bytes += req->length;
1605 }
1606 dev->stats.tx_packets++;
1607
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001608 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001609}
1610
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001611static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001612{
1613 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001614 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001615 return 1;
1616 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1617}
1618
1619#if 0
1620static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1621{
1622 struct eth_dev *dev = netdev_priv(net);
1623 int length = skb->len;
1624 int retval;
1625 struct usb_request *req = NULL;
1626 unsigned long flags;
1627
1628 /* apply outgoing CDC or RNDIS filters */
1629 if (!eth_is_promisc (dev)) {
1630 u8 *dest = skb->data;
1631
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001632 if (is_multicast_ethaddr(dest)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001633 u16 type;
1634
1635 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1636 * SET_ETHERNET_MULTICAST_FILTERS requests
1637 */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001638 if (is_broadcast_ethaddr(dest))
Remy Bohmerdf063442009-07-29 18:18:43 +02001639 type = USB_CDC_PACKET_TYPE_BROADCAST;
1640 else
1641 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1642 if (!(dev->cdc_filter & type)) {
1643 dev_kfree_skb_any (skb);
1644 return 0;
1645 }
1646 }
1647 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1648 }
1649
1650 spin_lock_irqsave(&dev->req_lock, flags);
1651 /*
1652 * this freelist can be empty if an interrupt triggered disconnect()
1653 * and reconfigured the gadget (shutting down this queue) after the
1654 * network stack decided to xmit but before we got the spinlock.
1655 */
1656 if (list_empty(&dev->tx_reqs)) {
1657 spin_unlock_irqrestore(&dev->req_lock, flags);
1658 return 1;
1659 }
1660
1661 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1662 list_del (&req->list);
1663
1664 /* temporarily stop TX queue when the freelist empties */
1665 if (list_empty (&dev->tx_reqs))
1666 netif_stop_queue (net);
1667 spin_unlock_irqrestore(&dev->req_lock, flags);
1668
1669 /* no buffer copies needed, unless the network stack did it
1670 * or the hardware can't use skb buffers.
1671 * or there's not enough space for any RNDIS headers we need
1672 */
1673 if (rndis_active(dev)) {
1674 struct sk_buff *skb_rndis;
1675
1676 skb_rndis = skb_realloc_headroom (skb,
1677 sizeof (struct rndis_packet_msg_type));
1678 if (!skb_rndis)
1679 goto drop;
1680
1681 dev_kfree_skb_any (skb);
1682 skb = skb_rndis;
1683 rndis_add_hdr (skb);
1684 length = skb->len;
1685 }
1686 req->buf = skb->data;
1687 req->context = skb;
1688 req->complete = tx_complete;
1689
1690 /* use zlp framing on tx for strict CDC-Ether conformance,
1691 * though any robust network rx path ignores extra padding.
1692 * and some hardware doesn't like to write zlps.
1693 */
1694 req->zero = 1;
1695 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1696 length++;
1697
1698 req->length = length;
1699
1700 /* throttle highspeed IRQ rate back slightly */
1701 if (gadget_is_dualspeed(dev->gadget))
1702 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1703 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1704 : 0;
1705
1706 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1707 switch (retval) {
1708 default:
1709 DEBUG (dev, "tx queue err %d\n", retval);
1710 break;
1711 case 0:
1712 net->trans_start = jiffies;
1713 atomic_inc (&dev->tx_qlen);
1714 }
1715
1716 if (retval) {
1717drop:
1718 dev->stats.tx_dropped++;
1719 dev_kfree_skb_any (skb);
1720 spin_lock_irqsave(&dev->req_lock, flags);
1721 if (list_empty (&dev->tx_reqs))
1722 netif_start_queue (net);
1723 list_add (&req->list, &dev->tx_reqs);
1724 spin_unlock_irqrestore(&dev->req_lock, flags);
1725 }
1726 return 0;
1727}
1728
1729/*-------------------------------------------------------------------------*/
1730#endif
1731
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001732static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001733{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001734 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001735
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001736 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001737 rndis_deregister(dev->rndis_config);
1738 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001739
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001740 /* we've already been disconnected ... no i/o is active */
1741 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001742 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001743 dev->req = NULL;
1744 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001745 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001746 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001747 dev->stat_req = NULL;
1748 }
1749
1750 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001751 usb_ep_free_request(dev->in_ep, dev->tx_req);
1752 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001753 }
1754
1755 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001756 usb_ep_free_request(dev->out_ep, dev->rx_req);
1757 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001758 }
1759
1760/* unregister_netdev (dev->net);*/
1761/* free_netdev(dev->net);*/
1762
Lei Wen4af32732010-12-01 23:43:43 +08001763 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001764 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001765}
1766
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001767static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001768{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001769 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001770 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001771}
1772
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001773static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001774{
1775 /* Not used */
1776}
1777
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001778static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001779{
1780 /* Not used */
1781}
1782
1783/*-------------------------------------------------------------------------*/
1784
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001785#ifdef CONFIG_USB_ETH_RNDIS
1786
1787/*
1788 * The interrupt endpoint is used in RNDIS to notify the host when messages
1789 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1790 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1791 * REMOTE_NDIS_KEEPALIVE_MSG.
1792 *
1793 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1794 * normally just one notification will be queued.
1795 */
1796
1797static void rndis_control_ack_complete(struct usb_ep *ep,
1798 struct usb_request *req)
1799{
1800 struct eth_dev *dev = ep->driver_data;
1801
1802 debug("%s...\n", __func__);
1803 if (req->status || req->actual != req->length)
1804 debug("rndis control ack complete --> %d, %d/%d\n",
1805 req->status, req->actual, req->length);
1806
Mugunthan V N3963de32016-11-18 10:49:13 +05301807 if (!dev->network_started) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001808 if (rndis_get_state(dev->rndis_config)
1809 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301810 dev->network_started = 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001811 printf("USB RNDIS network up!\n");
1812 }
1813 }
1814
1815 req->context = NULL;
1816
1817 if (req != dev->stat_req)
1818 usb_ep_free_request(ep, req);
1819}
1820
1821static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1822
Mugunthan V N095b7612016-11-18 11:09:15 +05301823static int rndis_control_ack(struct udevice *net)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001824{
Simon Glass95588622020-12-22 19:30:28 -07001825 struct ether_priv *priv;
1826 struct eth_dev *dev;
1827 int length;
1828 struct usb_request *resp;
1829
Simon Glass95588622020-12-22 19:30:28 -07001830 priv = dev_get_priv(net);
Simon Glass95588622020-12-22 19:30:28 -07001831 dev = &priv->ethdev;
1832 resp = dev->stat_req;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001833
1834 /* in case RNDIS calls this after disconnect */
1835 if (!dev->status) {
1836 debug("status ENODEV\n");
1837 return -ENODEV;
1838 }
1839
1840 /* in case queue length > 1 */
1841 if (resp->context) {
1842 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1843 if (!resp)
1844 return -ENOMEM;
1845 resp->buf = rndis_resp_buf;
1846 }
1847
1848 /*
1849 * Send RNDIS RESPONSE_AVAILABLE notification;
1850 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1851 */
1852 resp->length = 8;
1853 resp->complete = rndis_control_ack_complete;
1854 resp->context = dev;
1855
1856 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1857 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1858
1859 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1860 if (length < 0) {
1861 resp->status = 0;
1862 rndis_control_ack_complete(dev->status_ep, resp);
1863 }
1864
1865 return 0;
1866}
1867
1868#else
1869
1870#define rndis_control_ack NULL
1871
1872#endif /* RNDIS */
1873
1874static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1875{
1876 if (rndis_active(dev)) {
1877 rndis_set_param_medium(dev->rndis_config,
1878 NDIS_MEDIUM_802_3,
1879 BITRATE(dev->gadget)/100);
1880 rndis_signal_connect(dev->rndis_config);
1881 }
1882}
1883
Marek Vasut8552db02023-09-01 11:50:01 +02001884static int eth_stop(struct udevice *udev)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001885{
Marek Vasut8552db02023-09-01 11:50:01 +02001886 struct ether_priv *priv = dev_get_priv(udev);
1887 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001888#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1889 unsigned long ts;
1890 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1891#endif
1892
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001893 if (rndis_active(dev)) {
1894 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1895 rndis_signal_disconnect(dev->rndis_config);
1896
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001897#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1898 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1899 ts = get_timer(0);
1900 while (get_timer(ts) < timeout)
Marek Vasut8552db02023-09-01 11:50:01 +02001901 dm_usb_gadget_handle_interrupts(udev->parent);
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001902#endif
1903
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001904 rndis_uninit(dev->rndis_config);
1905 dev->rndis = 0;
1906 }
1907
1908 return 0;
1909}
1910
1911/*-------------------------------------------------------------------------*/
1912
Remy Bohmerdf063442009-07-29 18:18:43 +02001913static int is_eth_addr_valid(char *str)
1914{
1915 if (strlen(str) == 17) {
1916 int i;
1917 char *p, *q;
1918 uchar ea[6];
1919
1920 /* see if it looks like an ethernet address */
1921
1922 p = str;
1923
1924 for (i = 0; i < 6; i++) {
1925 char term = (i == 5 ? '\0' : ':');
1926
1927 ea[i] = simple_strtol(p, &q, 16);
1928
1929 if ((q - p) != 2 || *q++ != term)
1930 break;
1931
1932 p = q;
1933 }
1934
Tom Riniadd21ca2012-10-31 13:30:41 +00001935 /* Now check the contents. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001936 return is_valid_ethaddr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001937 }
1938 return 0;
1939}
1940
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001941static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001942{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001943 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001944 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001945 c = toupper(c);
1946 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001947 return 10 + c - 'A';
1948 return 0;
1949}
1950
1951static int get_ether_addr(const char *str, u8 *dev_addr)
1952{
1953 if (str) {
1954 unsigned i;
1955
1956 for (i = 0; i < 6; i++) {
1957 unsigned char num;
1958
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001959 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001960 str++;
1961 num = nibble(*str++) << 4;
1962 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001963 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02001964 }
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001965 if (is_valid_ethaddr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02001966 return 0;
1967 }
1968 return 1;
1969}
1970
1971static int eth_bind(struct usb_gadget *gadget)
1972{
Mugunthan V N88e48be2016-11-18 11:06:13 +05301973 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001974 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001975 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001976 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02001977 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001978 u8 tmp[7];
Simon Glassfa20e932020-12-03 16:55:20 -07001979 struct eth_pdata *pdata = dev_get_plat(l_priv->netdev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001980
1981 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001982#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001983 cdc = 0;
1984#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001985#ifndef CONFIG_USB_ETH_RNDIS
1986 rndis = 0;
1987#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001988 /*
1989 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02001990 * standard protocol is _strongly_ preferred for interop purposes.
1991 * (By everyone except Microsoft.)
1992 */
Tom Rini1e56de02022-06-25 11:02:42 -04001993 if (gadget_is_musbhdrc(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001994 /* reduce tx dma overhead by avoiding special cases */
1995 zlp = 0;
1996 } else if (gadget_is_sh(gadget)) {
1997 /* sh doesn't support multiple interfaces or configs */
1998 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001999 rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002000 }
2001
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002002 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002003 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002004 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002005 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002006 /*
2007 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002008 * anything less functional on CDC-capable hardware,
2009 * so we fail in this case.
2010 */
Masahiro Yamada81e10422017-09-16 14:10:41 +09002011 pr_err("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002012 gadget->name);
2013 return -ENODEV;
2014 }
2015
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002016 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002017 * If there's an RNDIS configuration, that's what Windows wants to
2018 * be using ... so use these product IDs here and in the "linux.inf"
2019 * needed to install MSFT drivers. Current Linux kernels will use
2020 * the second configuration if it's CDC Ethernet, and need some help
2021 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002022 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002023 if (rndis) {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002024#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Remy Bohmerdf063442009-07-29 18:18:43 +02002025 device_desc.idVendor =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002026 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Remy Bohmerdf063442009-07-29 18:18:43 +02002027 device_desc.idProduct =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002028 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002029#else
2030 device_desc.idVendor =
2031 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2032 device_desc.idProduct =
2033 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2034#endif
2035 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002036
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002037 /*
2038 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2039 * drivers aren't widely available. (That may be improved by
2040 * supporting one submode of the "SAFE" variant of MDLM.)
2041 */
2042 } else {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002043#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2044 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2045 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002046#else
2047 if (!cdc) {
2048 device_desc.idVendor =
2049 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2050 device_desc.idProduct =
2051 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2052 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002053#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002054 }
2055 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002056 if (bcdDevice)
2057 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2058 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002059 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002060 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002061 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002062 if (iSerialNumber) {
2063 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002064 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002065 }
2066
2067 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002068 usb_ep_autoconfig_reset(gadget);
2069 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002070 if (!in_ep) {
2071autoconf_fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002072 pr_err("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002073 gadget->name);
2074 return -ENODEV;
2075 }
2076 in_ep->driver_data = in_ep; /* claim */
2077
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002078 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002079 if (!out_ep)
2080 goto autoconf_fail;
2081 out_ep->driver_data = out_ep; /* claim */
2082
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002083#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002084 /*
2085 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002086 * Since some hosts expect one, try to allocate one anyway.
2087 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002088 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002089 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002090 if (status_ep) {
2091 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002092 } else if (rndis) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002093 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002094 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002095#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002096 } else if (cdc) {
2097 control_intf.bNumEndpoints = 0;
2098 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002099#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002100 }
2101 }
2102#endif
2103
2104 /* one config: cdc, else minimal subset */
2105 if (!cdc) {
2106 eth_config.bNumInterfaces = 1;
2107 eth_config.iConfiguration = STRING_SUBSET;
2108
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002109 /*
2110 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002111 * with multiple controllers and must override CDC Ethernet.
2112 */
2113 fs_subset_descriptors();
2114 hs_subset_descriptors();
2115 }
2116
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002117 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002118
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002119 /* For now RNDIS is always a second config */
2120 if (rndis)
2121 device_desc.bNumConfigurations = 2;
2122
Remy Bohmerdf063442009-07-29 18:18:43 +02002123 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002124 if (rndis)
2125 dev_qualifier.bNumConfigurations = 2;
2126 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002127 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2128
2129 /* assumes ep0 uses the same value for both speeds ... */
2130 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2131
2132 /* and that all endpoints are dual-speed */
2133 hs_source_desc.bEndpointAddress =
2134 fs_source_desc.bEndpointAddress;
2135 hs_sink_desc.bEndpointAddress =
2136 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002137#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002138 if (status_ep)
2139 hs_status_desc.bEndpointAddress =
2140 fs_status_desc.bEndpointAddress;
2141#endif
2142 }
2143
2144 if (gadget_is_otg(gadget)) {
2145 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2146 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2147 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002148#ifdef CONFIG_USB_ETH_RNDIS
2149 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2150 rndis_config.bMaxPower = 4;
2151#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002152 }
2153
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002154
2155 /* network device setup */
Mugunthan V N095b7612016-11-18 11:09:15 +05302156 dev->net = l_priv->netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002157
2158 dev->cdc = cdc;
2159 dev->zlp = zlp;
2160
2161 dev->in_ep = in_ep;
2162 dev->out_ep = out_ep;
2163 dev->status_ep = status_ep;
2164
Mugunthan V N095b7612016-11-18 11:09:15 +05302165 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002166 /*
2167 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002168 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002169 * ends up in a persistent config database. It's not clear if
2170 * host side code for the SAFE thing cares -- its original BLAN
2171 * thing didn't, Sharp never assigned those addresses on Zaurii.
2172 */
Mugunthan V N095b7612016-11-18 11:09:15 +05302173 get_ether_addr(dev_addr, pdata->enetaddr);
2174 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
Remy Bohmerdf063442009-07-29 18:18:43 +02002175
2176 get_ether_addr(host_addr, dev->host_mac);
2177
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002178 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2179 dev->host_mac[0], dev->host_mac[1],
2180 dev->host_mac[2], dev->host_mac[3],
2181 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002182
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002183 if (rndis) {
2184 status = rndis_init();
2185 if (status < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002186 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002187 goto fail;
2188 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002189 }
2190
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002191 /*
2192 * use PKTSIZE (or aligned... from u-boot) and set
2193 * wMaxSegmentSize accordingly
2194 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002195 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2196
2197 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002198 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002199 if (!dev->req)
2200 goto fail;
2201 dev->req->buf = control_req;
2202 dev->req->complete = eth_setup_complete;
2203
2204 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002205#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002206 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002207 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2208 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002209 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002210 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002211
2212 goto fail;
2213 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002214 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002215 dev->stat_req->context = NULL;
2216 }
2217#endif
2218
2219 /* finish hookup to lower layer ... */
2220 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002221 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002222 gadget->ep0->driver_data = dev;
2223
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002224 /*
2225 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002226 * - iff DATA transfer is active, carrier is "on"
2227 * - tx queueing enabled if open *and* carrier is "on"
2228 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002229
2230 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2231 out_ep->name, in_ep->name,
2232 status_ep ? " STATUS " : "",
2233 status_ep ? status_ep->name : ""
2234 );
Mugunthan V N095b7612016-11-18 11:09:15 +05302235 printf("MAC %pM\n", pdata->enetaddr);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002236
2237 if (cdc || rndis)
2238 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2239 dev->host_mac[0], dev->host_mac[1],
2240 dev->host_mac[2], dev->host_mac[3],
2241 dev->host_mac[4], dev->host_mac[5]);
2242
2243 if (rndis) {
2244 u32 vendorID = 0;
2245
2246 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2247
2248 dev->rndis_config = rndis_register(rndis_control_ack);
2249 if (dev->rndis_config < 0) {
2250fail0:
2251 eth_unbind(gadget);
2252 debug("RNDIS setup failed\n");
2253 status = -ENODEV;
2254 goto fail;
2255 }
2256
2257 /* these set up a lot of the OIDs that RNDIS needs */
2258 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2259 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2260 &dev->stats, &dev->cdc_filter))
2261 goto fail0;
2262 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2263 manufacturer))
2264 goto fail0;
2265 if (rndis_set_param_medium(dev->rndis_config,
2266 NDIS_MEDIUM_802_3, 0))
2267 goto fail0;
2268 printf("RNDIS ready\n");
2269 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002270 return 0;
2271
2272fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002273 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002274 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002275 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002276}
2277
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002278/*-------------------------------------------------------------------------*/
Marek Vasut34ee0332023-08-04 17:41:09 +02002279static void usb_eth_stop(struct udevice *dev);
Jean-Jacques Hiblot128151f2019-01-22 16:48:16 +01002280
Marek Vasut34ee0332023-08-04 17:41:09 +02002281static int usb_eth_start(struct udevice *udev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002282{
Marek Vasut34ee0332023-08-04 17:41:09 +02002283 struct ether_priv *priv = dev_get_priv(udev);
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302284 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002285 struct usb_gadget *gadget;
2286 unsigned long ts;
2287 unsigned long timeout = USB_CONNECT_TIMEOUT;
2288
Remy Bohmerdf063442009-07-29 18:18:43 +02002289 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002290
2291 packet_received = 0;
2292 packet_sent = 0;
2293
2294 gadget = dev->gadget;
2295 usb_gadget_connect(gadget);
2296
Simon Glass64b723f2017-08-03 12:22:12 -06002297 if (env_get("cdc_connect_timeout"))
Simon Glassff9b9032021-07-24 09:03:30 -06002298 timeout = dectoul(env_get("cdc_connect_timeout"), NULL) * CONFIG_SYS_HZ;
Remy Bohmerdf063442009-07-29 18:18:43 +02002299 ts = get_timer(0);
Mugunthan V N3963de32016-11-18 10:49:13 +05302300 while (!dev->network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002301 /* Handle control-c and timeouts */
2302 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002303 pr_err("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002304 goto fail;
2305 }
Marek Vasut8552db02023-09-01 11:50:01 +02002306 dm_usb_gadget_handle_interrupts(udev->parent);
Remy Bohmerdf063442009-07-29 18:18:43 +02002307 }
2308
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002309 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002310 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002311 return 0;
2312fail:
Marek Vasut34ee0332023-08-04 17:41:09 +02002313 usb_eth_stop(udev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002314 return -1;
2315}
2316
Marek Vasut34ee0332023-08-04 17:41:09 +02002317static int usb_eth_send(struct udevice *udev, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002318{
Marek Vasut34ee0332023-08-04 17:41:09 +02002319 struct ether_priv *priv = dev_get_priv(udev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002320 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002321 void *rndis_pkt = NULL;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302322 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002323 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002324 unsigned long ts;
2325 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002326
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002327 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002328
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002329 /* new buffer is needed to include RNDIS header */
2330 if (rndis_active(dev)) {
2331 rndis_pkt = malloc(length +
2332 sizeof(struct rndis_packet_msg_type));
2333 if (!rndis_pkt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002334 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002335 goto drop;
2336 }
2337 rndis_add_hdr(rndis_pkt, length);
2338 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002339 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002340 packet = rndis_pkt;
2341 length += sizeof(struct rndis_packet_msg_type);
2342 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002343 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002344 req->context = NULL;
2345 req->complete = tx_complete;
2346
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002347 /*
2348 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002349 * though any robust network rx path ignores extra padding.
2350 * and some hardware doesn't like to write zlps.
2351 */
2352 req->zero = 1;
2353 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2354 length++;
2355
2356 req->length = length;
2357#if 0
2358 /* throttle highspeed IRQ rate back slightly */
2359 if (gadget_is_dualspeed(dev->gadget))
2360 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2361 ? ((dev->tx_qlen % qmult) != 0) : 0;
2362#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002363 dev->tx_qlen = 1;
2364 ts = get_timer(0);
2365 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002366
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002367 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002368
2369 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002370 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002371 while (!packet_sent) {
2372 if (get_timer(ts) > timeout) {
2373 printf("timeout sending packets to usb ethernet\n");
2374 return -1;
2375 }
Marek Vasut8552db02023-09-01 11:50:01 +02002376 dm_usb_gadget_handle_interrupts(udev->parent);
Remy Bohmerdf063442009-07-29 18:18:43 +02002377 }
Heinrich Schuchardt637ad632020-04-19 12:11:12 +02002378 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002379
2380 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002381drop:
2382 dev->stats.tx_dropped++;
2383 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002384}
2385
Marek Vasut34ee0332023-08-04 17:41:09 +02002386static void usb_eth_stop(struct udevice *udev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002387{
Marek Vasut34ee0332023-08-04 17:41:09 +02002388 struct ether_priv *priv = dev_get_priv(udev);
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302389 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002390
Lei Wen4af32732010-12-01 23:43:43 +08002391 /* If the gadget not registered, simple return */
2392 if (!dev->gadget)
2393 return;
2394
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002395 /*
2396 * Some USB controllers may need additional deinitialization here
2397 * before dropping pull-up (also due to hardware issues).
2398 * For example: unhandled interrupt with status stage started may
2399 * bring the controller to fully broken state (until board reset).
2400 * There are some variants to debug and fix such cases:
2401 * 1) In the case of RNDIS connection eth_stop can perform additional
2402 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2403 * 2) 'pullup' callback in your UDC driver can be improved to perform
2404 * this deinitialization.
2405 */
Marek Vasut8552db02023-09-01 11:50:01 +02002406 eth_stop(udev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002407
Remy Bohmerdf063442009-07-29 18:18:43 +02002408 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002409
2410 /* Clear pending interrupt */
2411 if (dev->network_started) {
Marek Vasut8552db02023-09-01 11:50:01 +02002412 dm_usb_gadget_handle_interrupts(udev->parent);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002413 dev->network_started = 0;
2414 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002415}
2416
Mugunthan V N095b7612016-11-18 11:09:15 +05302417static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2418{
2419 struct ether_priv *priv = dev_get_priv(dev);
2420 struct eth_dev *ethdev = &priv->ethdev;
Mugunthan V N095b7612016-11-18 11:09:15 +05302421
Marek Vasut8552db02023-09-01 11:50:01 +02002422 dm_usb_gadget_handle_interrupts(dev->parent);
Mugunthan V N095b7612016-11-18 11:09:15 +05302423
2424 if (packet_received) {
2425 if (ethdev->rx_req) {
2426 *packetp = (uchar *)net_rx_packets[0];
2427 return ethdev->rx_req->length;
2428 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002429 pr_err("dev->rx_req invalid");
Mugunthan V N095b7612016-11-18 11:09:15 +05302430 return -EFAULT;
2431 }
2432 }
2433
2434 return -EAGAIN;
2435}
2436
2437static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2438 int length)
2439{
2440 struct ether_priv *priv = dev_get_priv(dev);
2441 struct eth_dev *ethdev = &priv->ethdev;
2442
2443 packet_received = 0;
2444
2445 return rx_submit(ethdev, ethdev->rx_req, 0);
2446}
2447
Mugunthan V N095b7612016-11-18 11:09:15 +05302448static const struct eth_ops usb_eth_ops = {
2449 .start = usb_eth_start,
2450 .send = usb_eth_send,
2451 .recv = usb_eth_recv,
2452 .free_pkt = usb_eth_free_pkt,
2453 .stop = usb_eth_stop,
2454};
2455
2456int usb_ether_init(void)
2457{
Mugunthan V N095b7612016-11-18 11:09:15 +05302458 struct udevice *usb_dev;
2459 int ret;
2460
Michal Suchaneka18b6a52022-10-12 21:57:54 +02002461 uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev);
2462 if (!usb_dev) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002463 pr_err("No USB device found\n");
Michal Suchaneka18b6a52022-10-12 21:57:54 +02002464 return -ENODEV;
Mugunthan V N095b7612016-11-18 11:09:15 +05302465 }
2466
Michal Suchaneka18b6a52022-10-12 21:57:54 +02002467 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", NULL);
2468 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002469 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302470 return ret;
2471 }
2472
Marek Vasut8552db02023-09-01 11:50:01 +02002473 return 0;
Mugunthan V N095b7612016-11-18 11:09:15 +05302474}
2475
Marek Vasut038b36d2023-08-04 17:41:10 +02002476static int usb_eth_probe(struct udevice *dev)
2477{
2478 struct ether_priv *priv = dev_get_priv(dev);
2479 struct eth_pdata *pdata = dev_get_plat(dev);
2480
2481 priv->netdev = dev;
2482 l_priv = priv;
2483
2484 get_ether_addr(CONFIG_USBNET_DEV_ADDR, pdata->enetaddr);
2485 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
2486
Marek Vasuta9358192023-08-04 17:41:11 +02002487 /* Configure default mac-addresses for the USB ethernet device */
2488#ifdef CONFIG_USBNET_DEV_ADDR
2489 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2490#endif
2491#ifdef CONFIG_USBNET_HOST_ADDR
2492 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2493#endif
2494 /* Check if the user overruled the MAC addresses */
2495 if (env_get("usbnet_devaddr"))
2496 strlcpy(dev_addr, env_get("usbnet_devaddr"),
2497 sizeof(dev_addr));
2498
2499 if (env_get("usbnet_hostaddr"))
2500 strlcpy(host_addr, env_get("usbnet_hostaddr"),
2501 sizeof(host_addr));
2502
2503 if (!is_eth_addr_valid(dev_addr)) {
2504 pr_err("Need valid 'usbnet_devaddr' to be set");
2505 return -EINVAL;
2506 }
2507 if (!is_eth_addr_valid(host_addr)) {
2508 pr_err("Need valid 'usbnet_hostaddr' to be set");
2509 return -EINVAL;
2510 }
2511
2512 priv->eth_driver.speed = DEVSPEED;
2513 priv->eth_driver.bind = eth_bind;
2514 priv->eth_driver.unbind = eth_unbind;
2515 priv->eth_driver.setup = eth_setup;
2516 priv->eth_driver.reset = eth_disconnect;
2517 priv->eth_driver.disconnect = eth_disconnect;
2518 priv->eth_driver.suspend = eth_suspend;
2519 priv->eth_driver.resume = eth_resume;
2520 return usb_gadget_register_driver(&priv->eth_driver);
2521}
2522
2523static int usb_eth_remove(struct udevice *dev)
2524{
2525 struct ether_priv *priv = dev_get_priv(dev);
2526
2527 usb_gadget_unregister_driver(&priv->eth_driver);
2528
2529 return 0;
2530}
2531
2532static int usb_eth_unbind(struct udevice *dev)
2533{
Marek Vasut8552db02023-09-01 11:50:01 +02002534 udc_device_put(dev->parent);
Marek Vasuta9358192023-08-04 17:41:11 +02002535
Marek Vasut038b36d2023-08-04 17:41:10 +02002536 return 0;
2537}
2538
Mugunthan V N095b7612016-11-18 11:09:15 +05302539U_BOOT_DRIVER(eth_usb) = {
2540 .name = "usb_ether",
2541 .id = UCLASS_ETH,
2542 .probe = usb_eth_probe,
Marek Vasuta9358192023-08-04 17:41:11 +02002543 .remove = usb_eth_remove,
2544 .unbind = usb_eth_unbind,
Mugunthan V N095b7612016-11-18 11:09:15 +05302545 .ops = &usb_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07002546 .priv_auto = sizeof(struct ether_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07002547 .plat_auto = sizeof(struct eth_pdata),
Mugunthan V N095b7612016-11-18 11:09:15 +05302548 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2549};