blob: 8ab9b9fe56aa675d1f8971b8ce1c6edd10494928 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-07-29 18:18:43 +02002/*
3 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
4 *
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
7 * Copyright (C) 2008 Nokia Corporation
Remy Bohmerdf063442009-07-29 18:18:43 +02008 */
9
10#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
Alex Kiernan9c215492018-04-01 09:22:38 +000012#include <environment.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +030014#include <linux/netdevice.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020015#include <linux/usb/ch9.h>
16#include <linux/usb/cdc.h>
17#include <linux/usb/gadget.h>
18#include <net.h>
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +053019#include <usb.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030020#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060021#include <memalign.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020022#include <linux/ctype.h>
23
24#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030025#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020026
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053027#include <dm.h>
Mugunthan V N095b7612016-11-18 11:09:15 +053028#include <dm/lists.h>
Mugunthan V Nb3d902b2016-11-18 10:49:12 +053029#include <dm/uclass-internal.h>
30#include <dm/device-internal.h>
31
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030032#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040033
Remy Bohmerdf063442009-07-29 18:18:43 +020034#define atomic_read
35extern struct platform_data brd;
Remy Bohmerdf063442009-07-29 18:18:43 +020036
37
38unsigned packet_received, packet_sent;
39
Remy Bohmerdf063442009-07-29 18:18:43 +020040/*
41 * Ethernet gadget driver -- with CDC and non-CDC options
42 * Builds on hardware support for a full duplex link.
43 *
44 * CDC Ethernet is the standard USB solution for sending Ethernet frames
45 * using USB. Real hardware tends to use the same framing protocol but look
46 * different for control features. This driver strongly prefers to use
47 * this USB-IF standard as its open-systems interoperability solution;
48 * most host side USB stacks (except from Microsoft) support it.
49 *
50 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
51 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
52 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
53 *
54 * There's some hardware that can't talk CDC ECM. We make that hardware
55 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
56 * link-level setup only requires activating the configuration. Only the
57 * endpoint descriptors, and product/vendor IDs, are relevant; no control
58 * operations are available. Linux supports it, but other host operating
59 * systems may not. (This is a subset of CDC Ethernet.)
60 *
61 * It turns out that if you add a few descriptors to that "CDC Subset",
62 * (Windows) host side drivers from MCCI can treat it as one submode of
63 * a proprietary scheme called "SAFE" ... without needing to know about
64 * specific product/vendor IDs. So we do that, making it easier to use
65 * those MS-Windows drivers. Those added descriptors make it resemble a
66 * CDC MDLM device, but they don't change device behavior at all. (See
67 * MCCI Engineering report 950198 "SAFE Networking Functions".)
68 *
69 * A third option is also in use. Rather than CDC Ethernet, or something
70 * simpler, Microsoft pushes their own approach: RNDIS. The published
71 * RNDIS specs are ambiguous and appear to be incomplete, and are also
72 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
73 */
74#define ETH_ALEN 6 /* Octets in one ethernet addr */
75#define ETH_HLEN 14 /* Total octets in header. */
76#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
77#define ETH_DATA_LEN 1500 /* Max. octets in payload */
78#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
Remy Bohmerdf063442009-07-29 18:18:43 +020079
80#define DRIVER_DESC "Ethernet Gadget"
81/* Based on linux 2.6.27 version */
82#define DRIVER_VERSION "May Day 2005"
83
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040084static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020085
86#define RX_EXTRA 20 /* guard against rx overflows */
87
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030088#ifndef CONFIG_USB_ETH_RNDIS
89#define rndis_uninit(x) do {} while (0)
90#define rndis_deregister(c) do {} while (0)
91#define rndis_exit() do {} while (0)
92#endif
93
94/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +020095#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
96 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
97 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
98 |USB_CDC_PACKET_TYPE_DIRECTED)
99
100#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
101
102/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300103
104struct eth_dev {
105 struct usb_gadget *gadget;
106 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300107 struct usb_request *stat_req; /* for cdc & rndis status */
Mugunthan V Nb3d902b2016-11-18 10:49:12 +0530108#ifdef CONFIG_DM_USB
109 struct udevice *usb_udev;
110#endif
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300111
112 u8 config;
113 struct usb_ep *in_ep, *out_ep, *status_ep;
114 const struct usb_endpoint_descriptor
115 *in, *out, *status;
116
117 struct usb_request *tx_req, *rx_req;
118
Mugunthan V N095b7612016-11-18 11:09:15 +0530119#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300120 struct eth_device *net;
Mugunthan V N095b7612016-11-18 11:09:15 +0530121#else
122 struct udevice *net;
123#endif
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300124 struct net_device_stats stats;
125 unsigned int tx_qlen;
126
127 unsigned zlp:1;
128 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300129 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300130 unsigned suspended:1;
131 unsigned network_started:1;
132 u16 cdc_filter;
133 unsigned long todo;
134 int mtu;
135#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300136 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300137 u8 host_mac[ETH_ALEN];
138};
139
140/*
141 * This version autoconfigures as much as possible at run-time.
142 *
143 * It also ASSUMES a self-powered device, without remote wakeup,
144 * although remote wakeup support would make sense.
145 */
146
147/*-------------------------------------------------------------------------*/
Mugunthan V N88e48be2016-11-18 11:06:13 +0530148struct ether_priv {
149 struct eth_dev ethdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530150#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +0530151 struct eth_device netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +0530152#else
153 struct udevice *netdev;
154#endif
Mugunthan V N88e48be2016-11-18 11:06:13 +0530155 struct usb_gadget_driver eth_driver;
156};
157
158struct ether_priv eth_priv;
159struct ether_priv *l_priv = &eth_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +0200160
161/*-------------------------------------------------------------------------*/
162
163/* "main" config is either CDC, or its simple subset */
164static inline int is_cdc(struct eth_dev *dev)
165{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200166#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200167 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200168#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200169 return 0; /* only subset possible */
170#else
171 return dev->cdc; /* depends on what hardware we found */
172#endif
173}
174
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300175/* "secondary" RNDIS config may sometimes be activated */
176static inline int rndis_active(struct eth_dev *dev)
177{
178#ifdef CONFIG_USB_ETH_RNDIS
179 return dev->rndis;
180#else
181 return 0;
182#endif
183}
184
185#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
186#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200187
188#define DEFAULT_QLEN 2 /* double buffering by default */
189
190/* peak bulk transfer bits-per-second */
191#define HS_BPS (13 * 512 * 8 * 1000 * 8)
192#define FS_BPS (19 * 64 * 1 * 1000 * 8)
193
194#ifdef CONFIG_USB_GADGET_DUALSPEED
195#define DEVSPEED USB_SPEED_HIGH
196
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400197#ifdef CONFIG_USB_ETH_QMULT
198#define qmult CONFIG_USB_ETH_QMULT
199#else
200#define qmult 5
201#endif
202
Remy Bohmerdf063442009-07-29 18:18:43 +0200203/* for dual-speed hardware, use deeper queues at highspeed */
204#define qlen(gadget) \
205 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
206
207static inline int BITRATE(struct usb_gadget *g)
208{
209 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
210}
211
212#else /* full speed (low speed doesn't do bulk) */
213
214#define qmult 1
215
216#define DEVSPEED USB_SPEED_FULL
217
218#define qlen(gadget) DEFAULT_QLEN
219
220static inline int BITRATE(struct usb_gadget *g)
221{
222 return FS_BPS;
223}
224#endif
225
Remy Bohmerdf063442009-07-29 18:18:43 +0200226/*-------------------------------------------------------------------------*/
227
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400228/*
229 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200230 * Instead: allocate your own, using normal USB-IF procedures.
231 */
232
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400233/*
234 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200235 * It's for devices with only CDC Ethernet configurations.
236 */
237#define CDC_VENDOR_NUM 0x0525 /* NetChip */
238#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
239
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400240/*
241 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200242 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
243 * with pxa250. We're protocol-compatible, if the host-side drivers
244 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
245 * drivers that need to hard-wire endpoint numbers have a hook.
246 *
247 * The protocol is a minimal subset of CDC Ether, which works on any bulk
248 * hardware that's not deeply broken ... even on hardware that can't talk
249 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
250 * doesn't handle control-OUT).
251 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300252#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
253#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200254
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400255/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300256 * For hardware that can talk RNDIS and either of the above protocols,
257 * use this ID ... the windows INF files will know it. Unless it's
258 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
259 * the non-RNDIS configuration.
260 */
261#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
262#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
263
264/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400265 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200266 * device descriptor, either numbers or strings or both. These string
267 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
268 */
269
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300270/*
271 * Emulating them in eth_bind:
272 * static ushort idVendor;
273 * static ushort idProduct;
274 */
275
Maxime Ripard48e78f82017-09-07 09:15:08 +0200276#if defined(CONFIG_USB_GADGET_MANUFACTURER)
277static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
Remy Bohmerdf063442009-07-29 18:18:43 +0200278#else
Bin Meng75574052016-02-05 19:30:11 -0800279static char *iManufacturer = "U-Boot";
Remy Bohmerdf063442009-07-29 18:18:43 +0200280#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300281
282/* These probably need to be configurable. */
283static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200284static char *iProduct;
285static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300286
Remy Bohmerdf063442009-07-29 18:18:43 +0200287static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300288
Remy Bohmerdf063442009-07-29 18:18:43 +0200289static char host_addr[18];
290
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300291
Remy Bohmerdf063442009-07-29 18:18:43 +0200292/*-------------------------------------------------------------------------*/
293
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400294/*
295 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200296 * ep0 implementation: descriptors, config management, setup().
297 * also optional class-specific notification interrupt transfer.
298 */
299
300/*
301 * DESCRIPTORS ... most are static, but strings and (full) configuration
302 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300303 * our simple subset, with RNDIS as an optional second configuration.
304 *
305 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
306 * the class descriptors match a modem (they're ignored; it's really just
307 * Ethernet functionality), they don't need the NOP altsetting, and the
308 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200309 */
310
311#define STRING_MANUFACTURER 1
312#define STRING_PRODUCT 2
313#define STRING_ETHADDR 3
314#define STRING_DATA 4
315#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300316#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200317#define STRING_CDC 7
318#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300319#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200320#define STRING_SERIALNUMBER 10
321
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300322/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200323#define USB_BUFSIZ 256
324
325/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300326 * This device advertises one configuration, eth_config, unless RNDIS
327 * is enabled (rndis_config) on hardware supporting at least two configs.
328 *
329 * NOTE: Controllers like superh_udc should probably be able to use
330 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200331 *
332 * FIXME define some higher-powered configurations to make it easier
333 * to recharge batteries ...
334 */
335
336#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300337#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200338
339static struct usb_device_descriptor
340device_desc = {
341 .bLength = sizeof device_desc,
342 .bDescriptorType = USB_DT_DEVICE,
343
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400344 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200345
346 .bDeviceClass = USB_CLASS_COMM,
347 .bDeviceSubClass = 0,
348 .bDeviceProtocol = 0,
349
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400350 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
351 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200352 .iManufacturer = STRING_MANUFACTURER,
353 .iProduct = STRING_PRODUCT,
354 .bNumConfigurations = 1,
355};
356
357static struct usb_otg_descriptor
358otg_descriptor = {
359 .bLength = sizeof otg_descriptor,
360 .bDescriptorType = USB_DT_OTG,
361
362 .bmAttributes = USB_OTG_SRP,
363};
364
365static struct usb_config_descriptor
366eth_config = {
367 .bLength = sizeof eth_config,
368 .bDescriptorType = USB_DT_CONFIG,
369
370 /* compute wTotalLength on the fly */
371 .bNumInterfaces = 2,
372 .bConfigurationValue = DEV_CONFIG_VALUE,
373 .iConfiguration = STRING_CDC,
374 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
375 .bMaxPower = 1,
376};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300377
378#ifdef CONFIG_USB_ETH_RNDIS
379static struct usb_config_descriptor
380rndis_config = {
381 .bLength = sizeof rndis_config,
382 .bDescriptorType = USB_DT_CONFIG,
383
384 /* compute wTotalLength on the fly */
385 .bNumInterfaces = 2,
386 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
387 .iConfiguration = STRING_RNDIS,
388 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
389 .bMaxPower = 1,
390};
391#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200392
393/*
394 * Compared to the simple CDC subset, the full CDC Ethernet model adds
395 * three class descriptors, two interface descriptors, optional status
396 * endpoint. Both have a "data" interface and two bulk endpoints.
397 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300398 *
399 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
400 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
401 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
402 * work around those bugs) are unlikely to ever come from MSFT, you may
403 * wish to avoid using RNDIS.
404 *
405 * MCCI offers an alternative to RNDIS if you need to connect to Windows
406 * but have hardware that can't support CDC Ethernet. We add descriptors
407 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
408 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
409 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200410 */
411
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200412#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200413static struct usb_interface_descriptor
414control_intf = {
415 .bLength = sizeof control_intf,
416 .bDescriptorType = USB_DT_INTERFACE,
417
418 .bInterfaceNumber = 0,
419 /* status endpoint is optional; this may be patched later */
420 .bNumEndpoints = 1,
421 .bInterfaceClass = USB_CLASS_COMM,
422 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
423 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
424 .iInterface = STRING_CONTROL,
425};
426#endif
427
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300428#ifdef CONFIG_USB_ETH_RNDIS
429static const struct usb_interface_descriptor
430rndis_control_intf = {
431 .bLength = sizeof rndis_control_intf,
432 .bDescriptorType = USB_DT_INTERFACE,
433
434 .bInterfaceNumber = 0,
435 .bNumEndpoints = 1,
436 .bInterfaceClass = USB_CLASS_COMM,
437 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
438 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
439 .iInterface = STRING_RNDIS_CONTROL,
440};
441#endif
442
Remy Bohmerdf063442009-07-29 18:18:43 +0200443static const struct usb_cdc_header_desc header_desc = {
444 .bLength = sizeof header_desc,
445 .bDescriptorType = USB_DT_CS_INTERFACE,
446 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
447
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400448 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200449};
450
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200451#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200452
453static const struct usb_cdc_union_desc union_desc = {
454 .bLength = sizeof union_desc,
455 .bDescriptorType = USB_DT_CS_INTERFACE,
456 .bDescriptorSubType = USB_CDC_UNION_TYPE,
457
458 .bMasterInterface0 = 0, /* index of control interface */
459 .bSlaveInterface0 = 1, /* index of DATA interface */
460};
461
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300462#endif /* CDC || RNDIS */
463
464#ifdef CONFIG_USB_ETH_RNDIS
465
466static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
467 .bLength = sizeof call_mgmt_descriptor,
468 .bDescriptorType = USB_DT_CS_INTERFACE,
469 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
470
471 .bmCapabilities = 0x00,
472 .bDataInterface = 0x01,
473};
474
475static const struct usb_cdc_acm_descriptor acm_descriptor = {
476 .bLength = sizeof acm_descriptor,
477 .bDescriptorType = USB_DT_CS_INTERFACE,
478 .bDescriptorSubType = USB_CDC_ACM_TYPE,
479
480 .bmCapabilities = 0x00,
481};
482
483#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200484
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200485#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200486
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400487/*
488 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200489 * ways: data endpoints live in the control interface, there's no data
490 * interface, and it's not used to talk to a cell phone radio.
491 */
492
493static const struct usb_cdc_mdlm_desc mdlm_desc = {
494 .bLength = sizeof mdlm_desc,
495 .bDescriptorType = USB_DT_CS_INTERFACE,
496 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
497
498 .bcdVersion = __constant_cpu_to_le16(0x0100),
499 .bGUID = {
500 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
501 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
502 },
503};
504
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400505/*
506 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200507 * can't really use its struct. All we do here is say that we're using
508 * the submode of "SAFE" which directly matches the CDC Subset.
509 */
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530510#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200511static const u8 mdlm_detail_desc[] = {
512 6,
513 USB_DT_CS_INTERFACE,
514 USB_CDC_MDLM_DETAIL_TYPE,
515
516 0, /* "SAFE" */
517 0, /* network control capabilities (none) */
518 0, /* network data capabilities ("raw" encapsulation) */
519};
Lokesh Vutlaae6b2df2017-01-17 08:51:22 +0530520#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200521
522#endif
523
Remy Bohmerdf063442009-07-29 18:18:43 +0200524static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400525 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200526 .bDescriptorType = USB_DT_CS_INTERFACE,
527 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
528
529 /* this descriptor actually adds value, surprise! */
530 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400531 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
532 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
533 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200534 .bNumberPowerFilters = 0,
535};
536
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200537#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200538
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400539/*
540 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200541 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
542 * packet, to simplify cancellation; and a big transfer interval, to
543 * waste less bandwidth.
544 *
545 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
546 * if they ignore the connect/disconnect notifications that real aether
547 * can provide. more advanced cdc configurations might want to support
548 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300549 *
550 * RNDIS requires the status endpoint, since it uses that encapsulation
551 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200552 */
553
554#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
555#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
556
557static struct usb_endpoint_descriptor
558fs_status_desc = {
559 .bLength = USB_DT_ENDPOINT_SIZE,
560 .bDescriptorType = USB_DT_ENDPOINT,
561
562 .bEndpointAddress = USB_DIR_IN,
563 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400564 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200565 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
566};
567#endif
568
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200569#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200570
571/* the default data interface has no endpoints ... */
572
573static const struct usb_interface_descriptor
574data_nop_intf = {
575 .bLength = sizeof data_nop_intf,
576 .bDescriptorType = USB_DT_INTERFACE,
577
578 .bInterfaceNumber = 1,
579 .bAlternateSetting = 0,
580 .bNumEndpoints = 0,
581 .bInterfaceClass = USB_CLASS_CDC_DATA,
582 .bInterfaceSubClass = 0,
583 .bInterfaceProtocol = 0,
584};
585
586/* ... but the "real" data interface has two bulk endpoints */
587
588static const struct usb_interface_descriptor
589data_intf = {
590 .bLength = sizeof data_intf,
591 .bDescriptorType = USB_DT_INTERFACE,
592
593 .bInterfaceNumber = 1,
594 .bAlternateSetting = 1,
595 .bNumEndpoints = 2,
596 .bInterfaceClass = USB_CLASS_CDC_DATA,
597 .bInterfaceSubClass = 0,
598 .bInterfaceProtocol = 0,
599 .iInterface = STRING_DATA,
600};
601
602#endif
603
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300604#ifdef CONFIG_USB_ETH_RNDIS
605
606/* RNDIS doesn't activate by changing to the "real" altsetting */
607
608static const struct usb_interface_descriptor
609rndis_data_intf = {
610 .bLength = sizeof rndis_data_intf,
611 .bDescriptorType = USB_DT_INTERFACE,
612
613 .bInterfaceNumber = 1,
614 .bAlternateSetting = 0,
615 .bNumEndpoints = 2,
616 .bInterfaceClass = USB_CLASS_CDC_DATA,
617 .bInterfaceSubClass = 0,
618 .bInterfaceProtocol = 0,
619 .iInterface = STRING_DATA,
620};
621
622#endif
623
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200624#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200625
626/*
627 * "Simple" CDC-subset option is a simple vendor-neutral model that most
628 * full speed controllers can handle: one interface, two bulk endpoints.
629 *
630 * To assist host side drivers, we fancy it up a bit, and add descriptors
631 * so some host side drivers will understand it as a "SAFE" variant.
632 */
633
634static const struct usb_interface_descriptor
635subset_data_intf = {
636 .bLength = sizeof subset_data_intf,
637 .bDescriptorType = USB_DT_INTERFACE,
638
639 .bInterfaceNumber = 0,
640 .bAlternateSetting = 0,
641 .bNumEndpoints = 2,
642 .bInterfaceClass = USB_CLASS_COMM,
643 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
644 .bInterfaceProtocol = 0,
645 .iInterface = STRING_DATA,
646};
647
648#endif /* SUBSET */
649
Remy Bohmerdf063442009-07-29 18:18:43 +0200650static struct usb_endpoint_descriptor
651fs_source_desc = {
652 .bLength = USB_DT_ENDPOINT_SIZE,
653 .bDescriptorType = USB_DT_ENDPOINT,
654
655 .bEndpointAddress = USB_DIR_IN,
656 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700657 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200658};
659
660static struct usb_endpoint_descriptor
661fs_sink_desc = {
662 .bLength = USB_DT_ENDPOINT_SIZE,
663 .bDescriptorType = USB_DT_ENDPOINT,
664
665 .bEndpointAddress = USB_DIR_OUT,
666 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700667 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200668};
669
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400670static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200671 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200672#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200673 /* "cdc" mode descriptors */
674 (struct usb_descriptor_header *) &control_intf,
675 (struct usb_descriptor_header *) &header_desc,
676 (struct usb_descriptor_header *) &union_desc,
677 (struct usb_descriptor_header *) &ether_desc,
678 /* NOTE: status endpoint may need to be removed */
679 (struct usb_descriptor_header *) &fs_status_desc,
680 /* data interface, with altsetting */
681 (struct usb_descriptor_header *) &data_nop_intf,
682 (struct usb_descriptor_header *) &data_intf,
683 (struct usb_descriptor_header *) &fs_source_desc,
684 (struct usb_descriptor_header *) &fs_sink_desc,
685 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200686#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200687};
688
689static inline void fs_subset_descriptors(void)
690{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200691#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200692 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
693 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
694 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
695 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
696 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
697 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
698 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
699 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
700 fs_eth_function[8] = NULL;
701#else
702 fs_eth_function[1] = NULL;
703#endif
704}
705
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300706#ifdef CONFIG_USB_ETH_RNDIS
707static const struct usb_descriptor_header *fs_rndis_function[] = {
708 (struct usb_descriptor_header *) &otg_descriptor,
709 /* control interface matches ACM, not Ethernet */
710 (struct usb_descriptor_header *) &rndis_control_intf,
711 (struct usb_descriptor_header *) &header_desc,
712 (struct usb_descriptor_header *) &call_mgmt_descriptor,
713 (struct usb_descriptor_header *) &acm_descriptor,
714 (struct usb_descriptor_header *) &union_desc,
715 (struct usb_descriptor_header *) &fs_status_desc,
716 /* data interface has no altsetting */
717 (struct usb_descriptor_header *) &rndis_data_intf,
718 (struct usb_descriptor_header *) &fs_source_desc,
719 (struct usb_descriptor_header *) &fs_sink_desc,
720 NULL,
721};
722#endif
723
Remy Bohmerdf063442009-07-29 18:18:43 +0200724/*
725 * usb 2.0 devices need to expose both high speed and full speed
726 * descriptors, unless they only run at full speed.
727 */
728
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200729#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200730static struct usb_endpoint_descriptor
731hs_status_desc = {
732 .bLength = USB_DT_ENDPOINT_SIZE,
733 .bDescriptorType = USB_DT_ENDPOINT,
734
735 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400736 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200737 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
738};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200739#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200740
741static struct usb_endpoint_descriptor
742hs_source_desc = {
743 .bLength = USB_DT_ENDPOINT_SIZE,
744 .bDescriptorType = USB_DT_ENDPOINT,
745
746 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400747 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200748};
749
750static struct usb_endpoint_descriptor
751hs_sink_desc = {
752 .bLength = USB_DT_ENDPOINT_SIZE,
753 .bDescriptorType = USB_DT_ENDPOINT,
754
755 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400756 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200757};
758
759static struct usb_qualifier_descriptor
760dev_qualifier = {
761 .bLength = sizeof dev_qualifier,
762 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
763
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400764 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200765 .bDeviceClass = USB_CLASS_COMM,
766
767 .bNumConfigurations = 1,
768};
769
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400770static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200771 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200772#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200773 /* "cdc" mode descriptors */
774 (struct usb_descriptor_header *) &control_intf,
775 (struct usb_descriptor_header *) &header_desc,
776 (struct usb_descriptor_header *) &union_desc,
777 (struct usb_descriptor_header *) &ether_desc,
778 /* NOTE: status endpoint may need to be removed */
779 (struct usb_descriptor_header *) &hs_status_desc,
780 /* data interface, with altsetting */
781 (struct usb_descriptor_header *) &data_nop_intf,
782 (struct usb_descriptor_header *) &data_intf,
783 (struct usb_descriptor_header *) &hs_source_desc,
784 (struct usb_descriptor_header *) &hs_sink_desc,
785 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200786#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200787};
788
789static inline void hs_subset_descriptors(void)
790{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200791#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200792 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
793 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
794 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
795 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
796 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
797 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
798 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
799 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
800 hs_eth_function[8] = NULL;
801#else
802 hs_eth_function[1] = NULL;
803#endif
804}
805
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300806#ifdef CONFIG_USB_ETH_RNDIS
807static const struct usb_descriptor_header *hs_rndis_function[] = {
808 (struct usb_descriptor_header *) &otg_descriptor,
809 /* control interface matches ACM, not Ethernet */
810 (struct usb_descriptor_header *) &rndis_control_intf,
811 (struct usb_descriptor_header *) &header_desc,
812 (struct usb_descriptor_header *) &call_mgmt_descriptor,
813 (struct usb_descriptor_header *) &acm_descriptor,
814 (struct usb_descriptor_header *) &union_desc,
815 (struct usb_descriptor_header *) &hs_status_desc,
816 /* data interface has no altsetting */
817 (struct usb_descriptor_header *) &rndis_data_intf,
818 (struct usb_descriptor_header *) &hs_source_desc,
819 (struct usb_descriptor_header *) &hs_sink_desc,
820 NULL,
821};
822#endif
823
824
Remy Bohmerdf063442009-07-29 18:18:43 +0200825/* maxpacket and other transfer characteristics vary by speed. */
826static inline struct usb_endpoint_descriptor *
827ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
828 struct usb_endpoint_descriptor *fs)
829{
830 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
831 return hs;
832 return fs;
833}
834
Remy Bohmerdf063442009-07-29 18:18:43 +0200835/*-------------------------------------------------------------------------*/
836
837/* descriptors that are built on-demand */
838
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400839static char manufacturer[50];
840static char product_desc[40] = DRIVER_DESC;
841static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200842
843/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400844static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200845
846/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400847static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200848 { STRING_MANUFACTURER, manufacturer, },
849 { STRING_PRODUCT, product_desc, },
850 { STRING_SERIALNUMBER, serial_number, },
851 { STRING_DATA, "Ethernet Data", },
852 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200853#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200854 { STRING_CDC, "CDC Ethernet", },
855 { STRING_CONTROL, "CDC Communications Control", },
856#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200857#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200858 { STRING_SUBSET, "CDC Ethernet Subset", },
859#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300860#ifdef CONFIG_USB_ETH_RNDIS
861 { STRING_RNDIS, "RNDIS", },
862 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
863#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200864 { } /* end of list */
865};
866
867static struct usb_gadget_strings stringtab = {
868 .language = 0x0409, /* en-us */
869 .strings = strings,
870};
871
Remy Bohmerdf063442009-07-29 18:18:43 +0200872/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500873DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
874
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200875#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500876DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200877#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200878
Remy Bohmerdf063442009-07-29 18:18:43 +0200879/*============================================================================*/
880
881/*
882 * one config, two interfaces: control, data.
883 * complications: class descriptors, and an altsetting.
884 */
885static int
886config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
887{
888 int len;
889 const struct usb_config_descriptor *config;
890 const struct usb_descriptor_header **function;
891 int hs = 0;
892
893 if (gadget_is_dualspeed(g)) {
894 hs = (g->speed == USB_SPEED_HIGH);
895 if (type == USB_DT_OTHER_SPEED_CONFIG)
896 hs = !hs;
897 }
898#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
899
900 if (index >= device_desc.bNumConfigurations)
901 return -EINVAL;
902
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300903#ifdef CONFIG_USB_ETH_RNDIS
904 /*
905 * list the RNDIS config first, to make Microsoft's drivers
906 * happy. DOCSIS 1.0 needs this too.
907 */
908 if (device_desc.bNumConfigurations == 2 && index == 0) {
909 config = &rndis_config;
910 function = which_fn(rndis);
911 } else
912#endif
913 {
914 config = &eth_config;
915 function = which_fn(eth);
916 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200917
918 /* for now, don't advertise srp-only devices */
919 if (!is_otg)
920 function++;
921
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400922 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200923 if (len < 0)
924 return len;
925 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
926 return len;
927}
928
929/*-------------------------------------------------------------------------*/
930
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300931static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400932static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200933
934static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400935set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200936{
937 int result = 0;
938 struct usb_gadget *gadget = dev->gadget;
939
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200940#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300941 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200942 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400943 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200944 &fs_status_desc);
945 dev->status_ep->driver_data = dev;
946
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400947 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200948 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400949 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200950 dev->status_ep->name, result);
951 goto done;
952 }
953 }
954#endif
955
956 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
957 dev->in_ep->driver_data = dev;
958
959 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
960 dev->out_ep->driver_data = dev;
961
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400962 /*
963 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200964 * endpoints in the default altsetting for the interface.
965 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300966 *
967 * Strictly speaking RNDIS should work the same: activation is
968 * a side effect of setting a packet filter. Deactivation is
969 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200970 */
971 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400972 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200973 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400974 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200975 dev->in_ep->name, result);
976 goto done;
977 }
978
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400979 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200980 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400981 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200982 dev->out_ep->name, result);
983 goto done;
984 }
985 }
986
987done:
988 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400989 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200990
991 /* on error, disable any endpoints */
992 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400993 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400994 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200995 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400996 (void) usb_ep_disable(dev->in_ep);
997 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200998 dev->in = NULL;
999 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001000 } else if (!cdc_active(dev)) {
1001 /*
1002 * activate non-CDC configs right away
1003 * this isn't strictly according to the RNDIS spec
1004 */
1005 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001006 }
1007
1008 /* caller is responsible for cleanup on error */
1009 return result;
1010}
1011
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001012static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001013{
1014 if (dev->config == 0)
1015 return;
1016
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001017 debug("%s\n", __func__);
1018
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001019 rndis_uninit(dev->rndis_config);
1020
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001021 /*
1022 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001023 * pending i/o. then free the requests.
1024 */
1025
1026 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001027 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001028 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001029 usb_ep_free_request(dev->in_ep, dev->tx_req);
1030 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001031 }
1032 }
1033 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001034 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001035 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001036 usb_ep_free_request(dev->out_ep, dev->rx_req);
1037 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001038 }
1039 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001040 if (dev->status)
1041 usb_ep_disable(dev->status_ep);
1042
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001043 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001044 dev->cdc_filter = 0;
1045 dev->config = 0;
1046}
1047
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001048/*
1049 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001050 * that returns config descriptors, and altsetting code.
1051 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001052static int eth_set_config(struct eth_dev *dev, unsigned number,
1053 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001054{
1055 int result = 0;
1056 struct usb_gadget *gadget = dev->gadget;
1057
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001058 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001059 && dev->config
1060 && dev->tx_qlen != 0) {
1061 /* tx fifo is full, but we can't clear it...*/
Masahiro Yamada81e10422017-09-16 14:10:41 +09001062 pr_err("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001063 return -ESPIPE;
1064 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001065 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001066
1067 switch (number) {
1068 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001069 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001070 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001071#ifdef CONFIG_USB_ETH_RNDIS
1072 case DEV_RNDIS_CONFIG_VALUE:
1073 dev->rndis = 1;
1074 result = set_ether_config(dev, gfp_flags);
1075 break;
1076#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001077 default:
1078 result = -EINVAL;
1079 /* FALL THROUGH */
1080 case 0:
1081 break;
1082 }
1083
1084 if (result) {
1085 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001086 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001087 usb_gadget_vbus_draw(dev->gadget,
1088 gadget_is_otg(dev->gadget) ? 8 : 100);
1089 } else {
1090 char *speed;
1091 unsigned power;
1092
1093 power = 2 * eth_config.bMaxPower;
1094 usb_gadget_vbus_draw(dev->gadget, power);
1095
1096 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001097 case USB_SPEED_FULL:
1098 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001099#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001100 case USB_SPEED_HIGH:
1101 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001102#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001103 default:
1104 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001105 }
1106
1107 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001108 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001109 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001110 rndis_active(dev)
1111 ? "RNDIS"
1112 : (cdc_active(dev)
1113 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001114 : "CDC Ethernet Subset"));
1115 }
1116 return result;
1117}
1118
1119/*-------------------------------------------------------------------------*/
1120
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001121#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001122
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001123/*
1124 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001125 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001126 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001127 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1128 * command mechanism; and only one status request is ever queued.
1129 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001130static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001131{
1132 struct usb_cdc_notification *event = req->buf;
1133 int value = req->status;
1134 struct eth_dev *dev = ep->driver_data;
1135
1136 /* issue the second notification if host reads the first */
1137 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1138 && value == 0) {
1139 __le32 *data = req->buf + sizeof *event;
1140
1141 event->bmRequestType = 0xA1;
1142 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001143 event->wValue = __constant_cpu_to_le16(0);
1144 event->wIndex = __constant_cpu_to_le16(1);
1145 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001146
1147 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001148 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001149
1150 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001151 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001152 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001153 if (value == 0)
1154 return;
1155 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001156 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001157 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001158 if (event->bNotificationType ==
1159 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301160 dev->network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001161 printf("USB network up!\n");
1162 }
1163 }
1164 req->context = NULL;
1165}
1166
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001167static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001168{
1169 struct usb_request *req = dev->stat_req;
1170 struct usb_cdc_notification *event;
1171 int value;
1172
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001173 /*
1174 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001175 *
1176 * FIXME ugly idiom, maybe we'd be better with just
1177 * a "cancel the whole queue" primitive since any
1178 * unlink-one primitive has way too many error modes.
1179 * here, we "know" toggle is already clear...
1180 *
1181 * FIXME iff req->context != null just dequeue it
1182 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001183 usb_ep_disable(dev->status_ep);
1184 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001185
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001186 /*
1187 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001188 * a SPEED_CHANGE. could be useful in some configs.
1189 */
1190 event = req->buf;
1191 event->bmRequestType = 0xA1;
1192 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001193 event->wValue = __constant_cpu_to_le16(1); /* connected */
1194 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001195 event->wLength = 0;
1196
1197 req->length = sizeof *event;
1198 req->complete = eth_status_complete;
1199 req->context = dev;
1200
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001201 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001202 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001203 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001204}
1205
1206#endif
1207
1208/*-------------------------------------------------------------------------*/
1209
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001210static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001211{
1212 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001213 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001214 req->status, req->actual, req->length);
1215}
1216
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001217#ifdef CONFIG_USB_ETH_RNDIS
1218
1219static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1220{
1221 if (req->status || req->actual != req->length)
1222 debug("rndis response complete --> %d, %d/%d\n",
1223 req->status, req->actual, req->length);
1224
1225 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1226}
1227
1228static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1229{
1230 struct eth_dev *dev = ep->driver_data;
1231 int status;
1232
1233 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1234 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1235 if (status < 0)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001236 pr_err("%s: rndis parse error %d", __func__, status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001237}
1238
1239#endif /* RNDIS */
1240
Remy Bohmerdf063442009-07-29 18:18:43 +02001241/*
1242 * The setup() callback implements all the ep0 functionality that's not
1243 * handled lower down. CDC has a number of less-common features:
1244 *
1245 * - two interfaces: control, and ethernet data
1246 * - Ethernet data interface has two altsettings: default, and active
1247 * - class-specific descriptors for the control interface
1248 * - class-specific control requests
1249 */
1250static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001251eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001252{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001253 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001254 struct usb_request *req = dev->req;
1255 int value = -EOPNOTSUPP;
1256 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1257 u16 wValue = le16_to_cpu(ctrl->wValue);
1258 u16 wLength = le16_to_cpu(ctrl->wLength);
1259
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001260 /*
1261 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001262 * while config change events may enable network traffic.
1263 */
1264
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001265 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001266
1267 req->complete = eth_setup_complete;
1268 switch (ctrl->bRequest) {
1269
1270 case USB_REQ_GET_DESCRIPTOR:
1271 if (ctrl->bRequestType != USB_DIR_IN)
1272 break;
1273 switch (wValue >> 8) {
1274
1275 case USB_DT_DEVICE:
Kishon Vijay Abraham I58d701e2015-02-23 18:40:17 +05301276 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001277 value = min(wLength, (u16) sizeof device_desc);
1278 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001279 break;
1280 case USB_DT_DEVICE_QUALIFIER:
1281 if (!gadget_is_dualspeed(gadget))
1282 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001283 value = min(wLength, (u16) sizeof dev_qualifier);
1284 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001285 break;
1286
1287 case USB_DT_OTHER_SPEED_CONFIG:
1288 if (!gadget_is_dualspeed(gadget))
1289 break;
1290 /* FALLTHROUGH */
1291 case USB_DT_CONFIG:
1292 value = config_buf(gadget, req->buf,
1293 wValue >> 8,
1294 wValue & 0xff,
1295 gadget_is_otg(gadget));
1296 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001297 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001298 break;
1299
1300 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001301 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001302 wValue & 0xff, req->buf);
1303
1304 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001305 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001306
1307 break;
1308 }
1309 break;
1310
1311 case USB_REQ_SET_CONFIGURATION:
1312 if (ctrl->bRequestType != 0)
1313 break;
1314 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001315 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001316 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001317 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001318 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001319 break;
1320 case USB_REQ_GET_CONFIGURATION:
1321 if (ctrl->bRequestType != USB_DIR_IN)
1322 break;
1323 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001324 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001325 break;
1326
1327 case USB_REQ_SET_INTERFACE:
1328 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1329 || !dev->config
1330 || wIndex > 1)
1331 break;
1332 if (!cdc_active(dev) && wIndex != 0)
1333 break;
1334
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001335 /*
1336 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001337 * we need to kluge around that interference.
1338 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001339 if (gadget_is_pxa(gadget)) {
1340 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001341 GFP_ATOMIC);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001342 /*
1343 * PXA25x driver use non-CDC ethernet gadget.
1344 * But only _CDC and _RNDIS code can signalize
1345 * that network is working. So we signalize it
1346 * here.
1347 */
Mugunthan V N3963de32016-11-18 10:49:13 +05301348 dev->network_started = 1;
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001349 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001350 goto done_set_intf;
1351 }
1352
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001353#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001354 switch (wIndex) {
1355 case 0: /* control/master intf */
1356 if (wValue != 0)
1357 break;
1358 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001359 usb_ep_disable(dev->status_ep);
1360 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001361 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001362
Remy Bohmerdf063442009-07-29 18:18:43 +02001363 value = 0;
1364 break;
1365 case 1: /* data intf */
1366 if (wValue > 1)
1367 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001368 usb_ep_disable(dev->in_ep);
1369 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001370
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001371 /*
1372 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001373 * the default interface setting ... also, setting
1374 * the non-default interface resets filters etc.
1375 */
1376 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001377 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001378 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001379 usb_ep_enable(dev->in_ep, dev->in);
1380 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001381 dev->cdc_filter = DEFAULT_FILTER;
1382 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001383 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001384 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001385 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001386 value = 0;
1387 break;
1388 }
1389#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001390 /*
1391 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001392 * all non-PXA hardware talks real CDC ...
1393 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001394 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001395#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001396
1397done_set_intf:
1398 break;
1399 case USB_REQ_GET_INTERFACE:
1400 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1401 || !dev->config
1402 || wIndex > 1)
1403 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001404 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001405 break;
1406
1407 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001408 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001409 *(u8 *)req->buf = 0;
1410 else {
1411 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1412 /* carrier always ok ...*/
1413 *(u8 *)req->buf = 1 ;
1414 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001415 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001416 break;
1417
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001418#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001419 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001420 /*
1421 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001422 * wValue = packet filter bitmap
1423 */
1424 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1425 || !cdc_active(dev)
1426 || wLength != 0
1427 || wIndex > 1)
1428 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001429 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001430 dev->cdc_filter = wValue;
1431 value = 0;
1432 break;
1433
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001434 /*
1435 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001436 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1437 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1438 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1439 * case USB_CDC_GET_ETHERNET_STATISTIC:
1440 */
1441
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001442#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001443
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001444#ifdef CONFIG_USB_ETH_RNDIS
1445 /*
1446 * RNDIS uses the CDC command encapsulation mechanism to implement
1447 * an RPC scheme, with much getting/setting of attributes by OID.
1448 */
1449 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1450 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1451 || !rndis_active(dev)
1452 || wLength > USB_BUFSIZ
1453 || wValue
1454 || rndis_control_intf.bInterfaceNumber
1455 != wIndex)
1456 break;
1457 /* read the request, then process it */
1458 value = wLength;
1459 req->complete = rndis_command_complete;
1460 /* later, rndis_control_ack () sends a notification */
1461 break;
1462
1463 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1464 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1465 == ctrl->bRequestType
1466 && rndis_active(dev)
1467 /* && wLength >= 0x0400 */
1468 && !wValue
1469 && rndis_control_intf.bInterfaceNumber
1470 == wIndex) {
1471 u8 *buf;
1472 u32 n;
1473
1474 /* return the result */
1475 buf = rndis_get_next_response(dev->rndis_config, &n);
1476 if (buf) {
1477 memcpy(req->buf, buf, n);
1478 req->complete = rndis_response_complete;
1479 rndis_free_response(dev->rndis_config, buf);
1480 value = n;
1481 }
1482 /* else stalls ... spec says to avoid that */
1483 }
1484 break;
1485#endif /* RNDIS */
1486
Remy Bohmerdf063442009-07-29 18:18:43 +02001487 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001488 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001489 ctrl->bRequestType, ctrl->bRequest,
1490 wValue, wIndex, wLength);
1491 }
1492
1493 /* respond with data transfer before status phase? */
1494 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001495 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001496 req->length = value;
1497 req->zero = value < wLength
1498 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001499 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001500 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001501 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001502 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001503 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001504 }
1505 }
1506
1507 /* host either stalls (value < 0) or reports success */
1508 return value;
1509}
1510
Remy Bohmerdf063442009-07-29 18:18:43 +02001511/*-------------------------------------------------------------------------*/
1512
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001513static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001514
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001515static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001516 gfp_t gfp_flags)
1517{
1518 int retval = -ENOMEM;
1519 size_t size;
1520
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001521 /*
1522 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001523 * Normally we use the USB "terminate on short read" convention;
1524 * so allow up to (N*maxpacket), since that memory is normally
1525 * already allocated. Some hardware doesn't deal well with short
1526 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1527 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001528 *
1529 * RNDIS uses internal framing, and explicitly allows senders to
1530 * pad to end-of-packet. That's potentially nice for speed,
1531 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001532 */
1533
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001534 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001535 if (!req)
1536 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001537
1538 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1539 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001540 if (rndis_active(dev))
1541 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001542 size -= size % dev->out_ep->maxpacket;
1543
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001544 /*
1545 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001546 * but on at least one, checksumming fails otherwise. Note:
1547 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001548 */
1549
Joe Hershberger9f09a362015-04-08 01:41:06 -05001550 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmerdf063442009-07-29 18:18:43 +02001551 req->length = size;
1552 req->complete = rx_complete;
1553
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001554 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001555
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001556 if (retval)
Masahiro Yamada81e10422017-09-16 14:10:41 +09001557 pr_err("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001558
Remy Bohmerdf063442009-07-29 18:18:43 +02001559 return retval;
1560}
1561
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001562static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001563{
1564 struct eth_dev *dev = ep->driver_data;
1565
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001566 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001567 switch (req->status) {
1568 /* normal completion */
1569 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001570 if (rndis_active(dev)) {
1571 /* we know MaxPacketsPerTransfer == 1 here */
1572 int length = rndis_rm_hdr(req->buf, req->actual);
1573 if (length < 0)
1574 goto length_err;
1575 req->length -= length;
1576 req->actual -= length;
1577 }
1578 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1579length_err:
1580 dev->stats.rx_errors++;
1581 dev->stats.rx_length_errors++;
1582 debug("rx length %d\n", req->length);
1583 break;
1584 }
1585
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001586 dev->stats.rx_packets++;
1587 dev->stats.rx_bytes += req->length;
1588 break;
1589
1590 /* software-driven interface shutdown */
1591 case -ECONNRESET: /* unlink */
1592 case -ESHUTDOWN: /* disconnect etc */
1593 /* for hardware automagic (such as pxa) */
1594 case -ECONNABORTED: /* endpoint reset */
1595 break;
1596
1597 /* data overrun */
1598 case -EOVERFLOW:
1599 dev->stats.rx_over_errors++;
1600 /* FALLTHROUGH */
1601 default:
1602 dev->stats.rx_errors++;
1603 break;
1604 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001605
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001606 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001607}
1608
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001609static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001610{
1611
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001612 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001613
1614 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001615 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001616
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001617 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001618
1619 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001620 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001621
1622 return 0;
1623
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001624fail2:
1625 usb_ep_free_request(dev->in_ep, dev->tx_req);
1626fail1:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001627 pr_err("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001628 return -1;
1629}
1630
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001631static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001632{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001633 struct eth_dev *dev = ep->driver_data;
1634
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001635 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001636 switch (req->status) {
1637 default:
1638 dev->stats.tx_errors++;
1639 debug("tx err %d\n", req->status);
1640 /* FALLTHROUGH */
1641 case -ECONNRESET: /* unlink */
1642 case -ESHUTDOWN: /* disconnect etc */
1643 break;
1644 case 0:
1645 dev->stats.tx_bytes += req->length;
1646 }
1647 dev->stats.tx_packets++;
1648
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001649 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001650}
1651
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001652static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001653{
1654 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001655 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001656 return 1;
1657 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1658}
1659
1660#if 0
1661static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1662{
1663 struct eth_dev *dev = netdev_priv(net);
1664 int length = skb->len;
1665 int retval;
1666 struct usb_request *req = NULL;
1667 unsigned long flags;
1668
1669 /* apply outgoing CDC or RNDIS filters */
1670 if (!eth_is_promisc (dev)) {
1671 u8 *dest = skb->data;
1672
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001673 if (is_multicast_ethaddr(dest)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001674 u16 type;
1675
1676 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1677 * SET_ETHERNET_MULTICAST_FILTERS requests
1678 */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001679 if (is_broadcast_ethaddr(dest))
Remy Bohmerdf063442009-07-29 18:18:43 +02001680 type = USB_CDC_PACKET_TYPE_BROADCAST;
1681 else
1682 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1683 if (!(dev->cdc_filter & type)) {
1684 dev_kfree_skb_any (skb);
1685 return 0;
1686 }
1687 }
1688 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1689 }
1690
1691 spin_lock_irqsave(&dev->req_lock, flags);
1692 /*
1693 * this freelist can be empty if an interrupt triggered disconnect()
1694 * and reconfigured the gadget (shutting down this queue) after the
1695 * network stack decided to xmit but before we got the spinlock.
1696 */
1697 if (list_empty(&dev->tx_reqs)) {
1698 spin_unlock_irqrestore(&dev->req_lock, flags);
1699 return 1;
1700 }
1701
1702 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1703 list_del (&req->list);
1704
1705 /* temporarily stop TX queue when the freelist empties */
1706 if (list_empty (&dev->tx_reqs))
1707 netif_stop_queue (net);
1708 spin_unlock_irqrestore(&dev->req_lock, flags);
1709
1710 /* no buffer copies needed, unless the network stack did it
1711 * or the hardware can't use skb buffers.
1712 * or there's not enough space for any RNDIS headers we need
1713 */
1714 if (rndis_active(dev)) {
1715 struct sk_buff *skb_rndis;
1716
1717 skb_rndis = skb_realloc_headroom (skb,
1718 sizeof (struct rndis_packet_msg_type));
1719 if (!skb_rndis)
1720 goto drop;
1721
1722 dev_kfree_skb_any (skb);
1723 skb = skb_rndis;
1724 rndis_add_hdr (skb);
1725 length = skb->len;
1726 }
1727 req->buf = skb->data;
1728 req->context = skb;
1729 req->complete = tx_complete;
1730
1731 /* use zlp framing on tx for strict CDC-Ether conformance,
1732 * though any robust network rx path ignores extra padding.
1733 * and some hardware doesn't like to write zlps.
1734 */
1735 req->zero = 1;
1736 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1737 length++;
1738
1739 req->length = length;
1740
1741 /* throttle highspeed IRQ rate back slightly */
1742 if (gadget_is_dualspeed(dev->gadget))
1743 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1744 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1745 : 0;
1746
1747 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1748 switch (retval) {
1749 default:
1750 DEBUG (dev, "tx queue err %d\n", retval);
1751 break;
1752 case 0:
1753 net->trans_start = jiffies;
1754 atomic_inc (&dev->tx_qlen);
1755 }
1756
1757 if (retval) {
1758drop:
1759 dev->stats.tx_dropped++;
1760 dev_kfree_skb_any (skb);
1761 spin_lock_irqsave(&dev->req_lock, flags);
1762 if (list_empty (&dev->tx_reqs))
1763 netif_start_queue (net);
1764 list_add (&req->list, &dev->tx_reqs);
1765 spin_unlock_irqrestore(&dev->req_lock, flags);
1766 }
1767 return 0;
1768}
1769
1770/*-------------------------------------------------------------------------*/
1771#endif
1772
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001773static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001774{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001775 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001776
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001777 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001778 rndis_deregister(dev->rndis_config);
1779 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001780
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001781 /* we've already been disconnected ... no i/o is active */
1782 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001783 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001784 dev->req = NULL;
1785 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001786 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001787 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001788 dev->stat_req = NULL;
1789 }
1790
1791 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001792 usb_ep_free_request(dev->in_ep, dev->tx_req);
1793 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001794 }
1795
1796 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001797 usb_ep_free_request(dev->out_ep, dev->rx_req);
1798 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001799 }
1800
1801/* unregister_netdev (dev->net);*/
1802/* free_netdev(dev->net);*/
1803
Lei Wen4af32732010-12-01 23:43:43 +08001804 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001805 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001806}
1807
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001808static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001809{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001810 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001811 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001812}
1813
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001814static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001815{
1816 /* Not used */
1817}
1818
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001819static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001820{
1821 /* Not used */
1822}
1823
1824/*-------------------------------------------------------------------------*/
1825
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001826#ifdef CONFIG_USB_ETH_RNDIS
1827
1828/*
1829 * The interrupt endpoint is used in RNDIS to notify the host when messages
1830 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1831 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1832 * REMOTE_NDIS_KEEPALIVE_MSG.
1833 *
1834 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1835 * normally just one notification will be queued.
1836 */
1837
1838static void rndis_control_ack_complete(struct usb_ep *ep,
1839 struct usb_request *req)
1840{
1841 struct eth_dev *dev = ep->driver_data;
1842
1843 debug("%s...\n", __func__);
1844 if (req->status || req->actual != req->length)
1845 debug("rndis control ack complete --> %d, %d/%d\n",
1846 req->status, req->actual, req->length);
1847
Mugunthan V N3963de32016-11-18 10:49:13 +05301848 if (!dev->network_started) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001849 if (rndis_get_state(dev->rndis_config)
1850 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N3963de32016-11-18 10:49:13 +05301851 dev->network_started = 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001852 printf("USB RNDIS network up!\n");
1853 }
1854 }
1855
1856 req->context = NULL;
1857
1858 if (req != dev->stat_req)
1859 usb_ep_free_request(ep, req);
1860}
1861
1862static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1863
Mugunthan V N095b7612016-11-18 11:09:15 +05301864#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001865static int rndis_control_ack(struct eth_device *net)
Mugunthan V N095b7612016-11-18 11:09:15 +05301866#else
1867static int rndis_control_ack(struct udevice *net)
1868#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001869{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05301870 struct ether_priv *priv = (struct ether_priv *)net->priv;
1871 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001872 int length;
1873 struct usb_request *resp = dev->stat_req;
1874
1875 /* in case RNDIS calls this after disconnect */
1876 if (!dev->status) {
1877 debug("status ENODEV\n");
1878 return -ENODEV;
1879 }
1880
1881 /* in case queue length > 1 */
1882 if (resp->context) {
1883 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1884 if (!resp)
1885 return -ENOMEM;
1886 resp->buf = rndis_resp_buf;
1887 }
1888
1889 /*
1890 * Send RNDIS RESPONSE_AVAILABLE notification;
1891 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1892 */
1893 resp->length = 8;
1894 resp->complete = rndis_control_ack_complete;
1895 resp->context = dev;
1896
1897 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1898 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1899
1900 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1901 if (length < 0) {
1902 resp->status = 0;
1903 rndis_control_ack_complete(dev->status_ep, resp);
1904 }
1905
1906 return 0;
1907}
1908
1909#else
1910
1911#define rndis_control_ack NULL
1912
1913#endif /* RNDIS */
1914
1915static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1916{
1917 if (rndis_active(dev)) {
1918 rndis_set_param_medium(dev->rndis_config,
1919 NDIS_MEDIUM_802_3,
1920 BITRATE(dev->gadget)/100);
1921 rndis_signal_connect(dev->rndis_config);
1922 }
1923}
1924
1925static int eth_stop(struct eth_dev *dev)
1926{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001927#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1928 unsigned long ts;
1929 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1930#endif
1931
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001932 if (rndis_active(dev)) {
1933 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1934 rndis_signal_disconnect(dev->rndis_config);
1935
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001936#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1937 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1938 ts = get_timer(0);
1939 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05301940 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001941#endif
1942
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001943 rndis_uninit(dev->rndis_config);
1944 dev->rndis = 0;
1945 }
1946
1947 return 0;
1948}
1949
1950/*-------------------------------------------------------------------------*/
1951
Remy Bohmerdf063442009-07-29 18:18:43 +02001952static int is_eth_addr_valid(char *str)
1953{
1954 if (strlen(str) == 17) {
1955 int i;
1956 char *p, *q;
1957 uchar ea[6];
1958
1959 /* see if it looks like an ethernet address */
1960
1961 p = str;
1962
1963 for (i = 0; i < 6; i++) {
1964 char term = (i == 5 ? '\0' : ':');
1965
1966 ea[i] = simple_strtol(p, &q, 16);
1967
1968 if ((q - p) != 2 || *q++ != term)
1969 break;
1970
1971 p = q;
1972 }
1973
Tom Riniadd21ca2012-10-31 13:30:41 +00001974 /* Now check the contents. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001975 return is_valid_ethaddr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001976 }
1977 return 0;
1978}
1979
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001980static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001981{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001982 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001983 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001984 c = toupper(c);
1985 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001986 return 10 + c - 'A';
1987 return 0;
1988}
1989
1990static int get_ether_addr(const char *str, u8 *dev_addr)
1991{
1992 if (str) {
1993 unsigned i;
1994
1995 for (i = 0; i < 6; i++) {
1996 unsigned char num;
1997
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001998 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001999 str++;
2000 num = nibble(*str++) << 4;
2001 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002002 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02002003 }
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05002004 if (is_valid_ethaddr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02002005 return 0;
2006 }
2007 return 1;
2008}
2009
2010static int eth_bind(struct usb_gadget *gadget)
2011{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302012 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002013 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02002014 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002015 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002016 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002017 u8 tmp[7];
Mugunthan V N095b7612016-11-18 11:09:15 +05302018#ifdef CONFIG_DM_ETH
2019 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2020#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002021
2022 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002023#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002024 cdc = 0;
2025#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002026#ifndef CONFIG_USB_ETH_RNDIS
2027 rndis = 0;
2028#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002029 /*
2030 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002031 * standard protocol is _strongly_ preferred for interop purposes.
2032 * (By everyone except Microsoft.)
2033 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002034 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002035 /* pxa doesn't support altsettings */
2036 cdc = 0;
2037 } else if (gadget_is_musbhdrc(gadget)) {
2038 /* reduce tx dma overhead by avoiding special cases */
2039 zlp = 0;
2040 } else if (gadget_is_sh(gadget)) {
2041 /* sh doesn't support multiple interfaces or configs */
2042 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002043 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002044 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002045 /* hardware can't write zlps */
2046 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002047 /*
2048 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002049 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2050 */
2051 cdc = 0;
2052 }
2053
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002054 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002055 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002056 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002057 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002058 /*
2059 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002060 * anything less functional on CDC-capable hardware,
2061 * so we fail in this case.
2062 */
Masahiro Yamada81e10422017-09-16 14:10:41 +09002063 pr_err("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002064 gadget->name);
2065 return -ENODEV;
2066 }
2067
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002068 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002069 * If there's an RNDIS configuration, that's what Windows wants to
2070 * be using ... so use these product IDs here and in the "linux.inf"
2071 * needed to install MSFT drivers. Current Linux kernels will use
2072 * the second configuration if it's CDC Ethernet, and need some help
2073 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002074 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002075 if (rndis) {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002076#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
Remy Bohmerdf063442009-07-29 18:18:43 +02002077 device_desc.idVendor =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002078 __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
Remy Bohmerdf063442009-07-29 18:18:43 +02002079 device_desc.idProduct =
Maxime Ripard48e78f82017-09-07 09:15:08 +02002080 __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002081#else
2082 device_desc.idVendor =
2083 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2084 device_desc.idProduct =
2085 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2086#endif
2087 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002088
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002089 /*
2090 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2091 * drivers aren't widely available. (That may be improved by
2092 * supporting one submode of the "SAFE" variant of MDLM.)
2093 */
2094 } else {
Maxime Ripard48e78f82017-09-07 09:15:08 +02002095#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
2096 device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
2097 device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002098#else
2099 if (!cdc) {
2100 device_desc.idVendor =
2101 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2102 device_desc.idProduct =
2103 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2104 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002105#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002106 }
2107 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002108 if (bcdDevice)
2109 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2110 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002111 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002112 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002113 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002114 if (iSerialNumber) {
2115 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002116 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002117 }
2118
2119 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002120 usb_ep_autoconfig_reset(gadget);
2121 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002122 if (!in_ep) {
2123autoconf_fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002124 pr_err("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002125 gadget->name);
2126 return -ENODEV;
2127 }
2128 in_ep->driver_data = in_ep; /* claim */
2129
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002130 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002131 if (!out_ep)
2132 goto autoconf_fail;
2133 out_ep->driver_data = out_ep; /* claim */
2134
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002135#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002136 /*
2137 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002138 * Since some hosts expect one, try to allocate one anyway.
2139 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002140 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002141 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002142 if (status_ep) {
2143 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002144 } else if (rndis) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002145 pr_err("can't run RNDIS on %s", gadget->name);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002146 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002147#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002148 } else if (cdc) {
2149 control_intf.bNumEndpoints = 0;
2150 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002151#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002152 }
2153 }
2154#endif
2155
2156 /* one config: cdc, else minimal subset */
2157 if (!cdc) {
2158 eth_config.bNumInterfaces = 1;
2159 eth_config.iConfiguration = STRING_SUBSET;
2160
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002161 /*
2162 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002163 * with multiple controllers and must override CDC Ethernet.
2164 */
2165 fs_subset_descriptors();
2166 hs_subset_descriptors();
2167 }
2168
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002169 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002170
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002171 /* For now RNDIS is always a second config */
2172 if (rndis)
2173 device_desc.bNumConfigurations = 2;
2174
Remy Bohmerdf063442009-07-29 18:18:43 +02002175 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002176 if (rndis)
2177 dev_qualifier.bNumConfigurations = 2;
2178 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002179 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2180
2181 /* assumes ep0 uses the same value for both speeds ... */
2182 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2183
2184 /* and that all endpoints are dual-speed */
2185 hs_source_desc.bEndpointAddress =
2186 fs_source_desc.bEndpointAddress;
2187 hs_sink_desc.bEndpointAddress =
2188 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002189#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002190 if (status_ep)
2191 hs_status_desc.bEndpointAddress =
2192 fs_status_desc.bEndpointAddress;
2193#endif
2194 }
2195
2196 if (gadget_is_otg(gadget)) {
2197 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2198 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2199 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002200#ifdef CONFIG_USB_ETH_RNDIS
2201 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2202 rndis_config.bMaxPower = 4;
2203#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002204 }
2205
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002206
2207 /* network device setup */
Mugunthan V N095b7612016-11-18 11:09:15 +05302208#ifndef CONFIG_DM_ETH
Mugunthan V N88e48be2016-11-18 11:06:13 +05302209 dev->net = &l_priv->netdev;
Mugunthan V N095b7612016-11-18 11:09:15 +05302210#else
2211 dev->net = l_priv->netdev;
2212#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002213
2214 dev->cdc = cdc;
2215 dev->zlp = zlp;
2216
2217 dev->in_ep = in_ep;
2218 dev->out_ep = out_ep;
2219 dev->status_ep = status_ep;
2220
Mugunthan V N095b7612016-11-18 11:09:15 +05302221 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002222 /*
2223 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002224 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002225 * ends up in a persistent config database. It's not clear if
2226 * host side code for the SAFE thing cares -- its original BLAN
2227 * thing didn't, Sharp never assigned those addresses on Zaurii.
2228 */
Mugunthan V N095b7612016-11-18 11:09:15 +05302229#ifndef CONFIG_DM_ETH
Remy Bohmerdf063442009-07-29 18:18:43 +02002230 get_ether_addr(dev_addr, dev->net->enetaddr);
Remy Bohmerdf063442009-07-29 18:18:43 +02002231 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
Mugunthan V N095b7612016-11-18 11:09:15 +05302232#else
2233 get_ether_addr(dev_addr, pdata->enetaddr);
2234 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2235#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002236
2237 get_ether_addr(host_addr, dev->host_mac);
2238
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002239 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2240 dev->host_mac[0], dev->host_mac[1],
2241 dev->host_mac[2], dev->host_mac[3],
2242 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002243
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002244 if (rndis) {
2245 status = rndis_init();
2246 if (status < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002247 pr_err("can't init RNDIS, %d", status);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002248 goto fail;
2249 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002250 }
2251
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002252 /*
2253 * use PKTSIZE (or aligned... from u-boot) and set
2254 * wMaxSegmentSize accordingly
2255 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002256 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2257
2258 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002259 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002260 if (!dev->req)
2261 goto fail;
2262 dev->req->buf = control_req;
2263 dev->req->complete = eth_setup_complete;
2264
2265 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002266#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002267 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002268 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2269 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002270 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002271 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002272
2273 goto fail;
2274 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002275 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002276 dev->stat_req->context = NULL;
2277 }
2278#endif
2279
2280 /* finish hookup to lower layer ... */
2281 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002282 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002283 gadget->ep0->driver_data = dev;
2284
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002285 /*
2286 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002287 * - iff DATA transfer is active, carrier is "on"
2288 * - tx queueing enabled if open *and* carrier is "on"
2289 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002290
2291 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2292 out_ep->name, in_ep->name,
2293 status_ep ? " STATUS " : "",
2294 status_ep ? status_ep->name : ""
2295 );
Mugunthan V N095b7612016-11-18 11:09:15 +05302296#ifndef CONFIG_DM_ETH
2297 printf("MAC %pM\n", dev->net->enetaddr);
2298#else
2299 printf("MAC %pM\n", pdata->enetaddr);
2300#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002301
2302 if (cdc || rndis)
2303 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2304 dev->host_mac[0], dev->host_mac[1],
2305 dev->host_mac[2], dev->host_mac[3],
2306 dev->host_mac[4], dev->host_mac[5]);
2307
2308 if (rndis) {
2309 u32 vendorID = 0;
2310
2311 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2312
2313 dev->rndis_config = rndis_register(rndis_control_ack);
2314 if (dev->rndis_config < 0) {
2315fail0:
2316 eth_unbind(gadget);
2317 debug("RNDIS setup failed\n");
2318 status = -ENODEV;
2319 goto fail;
2320 }
2321
2322 /* these set up a lot of the OIDs that RNDIS needs */
2323 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2324 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2325 &dev->stats, &dev->cdc_filter))
2326 goto fail0;
2327 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2328 manufacturer))
2329 goto fail0;
2330 if (rndis_set_param_medium(dev->rndis_config,
2331 NDIS_MEDIUM_802_3, 0))
2332 goto fail0;
2333 printf("RNDIS ready\n");
2334 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002335 return 0;
2336
2337fail:
Masahiro Yamada81e10422017-09-16 14:10:41 +09002338 pr_err("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002339 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002340 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002341}
2342
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002343/*-------------------------------------------------------------------------*/
2344
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302345#ifdef CONFIG_DM_USB
2346int dm_usb_init(struct eth_dev *e_dev)
2347{
2348 struct udevice *dev = NULL;
2349 int ret;
2350
2351 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2352 if (!dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002353 pr_err("No USB device found\n");
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302354 return -ENODEV;
2355 }
2356
2357 e_dev->usb_udev = dev;
2358
2359 return ret;
2360}
2361#endif
2362
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302363static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002364{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302365 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002366 struct usb_gadget *gadget;
2367 unsigned long ts;
2368 unsigned long timeout = USB_CONNECT_TIMEOUT;
2369
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302370#ifdef CONFIG_DM_USB
2371 if (dm_usb_init(dev)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002372 pr_err("USB ether not found\n");
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302373 return -ENODEV;
2374 }
2375#else
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302376 board_usb_init(0, USB_INIT_DEVICE);
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302377#endif
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302378
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002379 /* Configure default mac-addresses for the USB ethernet device */
2380#ifdef CONFIG_USBNET_DEV_ADDR
2381 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2382#endif
2383#ifdef CONFIG_USBNET_HOST_ADDR
2384 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2385#endif
2386 /* Check if the user overruled the MAC addresses */
Simon Glass64b723f2017-08-03 12:22:12 -06002387 if (env_get("usbnet_devaddr"))
2388 strlcpy(dev_addr, env_get("usbnet_devaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002389 sizeof(dev_addr));
2390
Simon Glass64b723f2017-08-03 12:22:12 -06002391 if (env_get("usbnet_hostaddr"))
2392 strlcpy(host_addr, env_get("usbnet_hostaddr"),
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002393 sizeof(host_addr));
2394
2395 if (!is_eth_addr_valid(dev_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002396 pr_err("Need valid 'usbnet_devaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002397 goto fail;
2398 }
2399 if (!is_eth_addr_valid(host_addr)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002400 pr_err("Need valid 'usbnet_hostaddr' to be set");
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002401 goto fail;
2402 }
2403
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302404 priv->eth_driver.speed = DEVSPEED;
2405 priv->eth_driver.bind = eth_bind;
2406 priv->eth_driver.unbind = eth_unbind;
2407 priv->eth_driver.setup = eth_setup;
2408 priv->eth_driver.reset = eth_disconnect;
2409 priv->eth_driver.disconnect = eth_disconnect;
2410 priv->eth_driver.suspend = eth_suspend;
2411 priv->eth_driver.resume = eth_resume;
2412 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen4af32732010-12-01 23:43:43 +08002413 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002414
2415 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002416
2417 packet_received = 0;
2418 packet_sent = 0;
2419
2420 gadget = dev->gadget;
2421 usb_gadget_connect(gadget);
2422
Simon Glass64b723f2017-08-03 12:22:12 -06002423 if (env_get("cdc_connect_timeout"))
2424 timeout = simple_strtoul(env_get("cdc_connect_timeout"),
Remy Bohmerdf063442009-07-29 18:18:43 +02002425 NULL, 10) * CONFIG_SYS_HZ;
2426 ts = get_timer(0);
Mugunthan V N3963de32016-11-18 10:49:13 +05302427 while (!dev->network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002428 /* Handle control-c and timeouts */
2429 if (ctrlc() || (get_timer(ts) > timeout)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002430 pr_err("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002431 goto fail;
2432 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302433 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002434 }
2435
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002436 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002437 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002438 return 0;
2439fail:
2440 return -1;
2441}
2442
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302443static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002444{
2445 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002446 void *rndis_pkt = NULL;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302447 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002448 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002449 unsigned long ts;
2450 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002451
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002452 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002453
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002454 /* new buffer is needed to include RNDIS header */
2455 if (rndis_active(dev)) {
2456 rndis_pkt = malloc(length +
2457 sizeof(struct rndis_packet_msg_type));
2458 if (!rndis_pkt) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002459 pr_err("No memory to alloc RNDIS packet");
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002460 goto drop;
2461 }
2462 rndis_add_hdr(rndis_pkt, length);
2463 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002464 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002465 packet = rndis_pkt;
2466 length += sizeof(struct rndis_packet_msg_type);
2467 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002468 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002469 req->context = NULL;
2470 req->complete = tx_complete;
2471
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002472 /*
2473 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002474 * though any robust network rx path ignores extra padding.
2475 * and some hardware doesn't like to write zlps.
2476 */
2477 req->zero = 1;
2478 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2479 length++;
2480
2481 req->length = length;
2482#if 0
2483 /* throttle highspeed IRQ rate back slightly */
2484 if (gadget_is_dualspeed(dev->gadget))
2485 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2486 ? ((dev->tx_qlen % qmult) != 0) : 0;
2487#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002488 dev->tx_qlen = 1;
2489 ts = get_timer(0);
2490 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002491
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002492 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002493
2494 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002495 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002496 while (!packet_sent) {
2497 if (get_timer(ts) > timeout) {
2498 printf("timeout sending packets to usb ethernet\n");
2499 return -1;
2500 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302501 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002502 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002503 if (rndis_pkt)
2504 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002505
2506 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002507drop:
2508 dev->stats.tx_dropped++;
2509 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002510}
2511
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302512static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002513{
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302514 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002515
Remy Bohmerdf063442009-07-29 18:18:43 +02002516 return 0;
2517}
2518
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302519void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmerdf063442009-07-29 18:18:43 +02002520{
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302521 struct eth_dev *dev = &priv->ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002522
Lei Wen4af32732010-12-01 23:43:43 +08002523 /* If the gadget not registered, simple return */
2524 if (!dev->gadget)
2525 return;
2526
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002527 /*
2528 * Some USB controllers may need additional deinitialization here
2529 * before dropping pull-up (also due to hardware issues).
2530 * For example: unhandled interrupt with status stage started may
2531 * bring the controller to fully broken state (until board reset).
2532 * There are some variants to debug and fix such cases:
2533 * 1) In the case of RNDIS connection eth_stop can perform additional
2534 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2535 * 2) 'pullup' callback in your UDC driver can be improved to perform
2536 * this deinitialization.
2537 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002538 eth_stop(dev);
2539
Remy Bohmerdf063442009-07-29 18:18:43 +02002540 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002541
2542 /* Clear pending interrupt */
2543 if (dev->network_started) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302544 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002545 dev->network_started = 0;
2546 }
2547
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302548 usb_gadget_unregister_driver(&priv->eth_driver);
Mugunthan V N095b7612016-11-18 11:09:15 +05302549#ifndef CONFIG_DM_USB
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302550 board_usb_cleanup(0, USB_INIT_DEVICE);
Mugunthan V Nb3d902b2016-11-18 10:49:12 +05302551#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002552}
2553
Mugunthan V N095b7612016-11-18 11:09:15 +05302554#ifndef CONFIG_DM_ETH
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302555static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2556{
2557 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2558
2559 return _usb_eth_init(priv);
2560}
2561
2562static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2563{
2564 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2565
2566 return _usb_eth_send(priv, packet, length);
2567}
2568
2569static int usb_eth_recv(struct eth_device *netdev)
2570{
2571 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2572 struct eth_dev *dev = &priv->ethdev;
2573 int ret;
2574
2575 ret = _usb_eth_recv(priv);
2576 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002577 pr_err("error packet receive\n");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302578 return ret;
2579 }
2580
2581 if (!packet_received)
2582 return 0;
2583
2584 if (dev->rx_req) {
2585 net_process_received_packet(net_rx_packets[0],
2586 dev->rx_req->length);
2587 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002588 pr_err("dev->rx_req invalid");
Mugunthan V Ndb5eb2a2016-11-18 11:08:27 +05302589 }
2590 packet_received = 0;
2591 rx_submit(dev, dev->rx_req, 0);
2592
2593 return 0;
2594}
2595
2596void usb_eth_halt(struct eth_device *netdev)
2597{
2598 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2599
2600 _usb_eth_halt(priv);
2601}
2602
Remy Bohmerdf063442009-07-29 18:18:43 +02002603int usb_eth_initialize(bd_t *bi)
2604{
Mugunthan V N88e48be2016-11-18 11:06:13 +05302605 struct eth_device *netdev = &l_priv->netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002606
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002607 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002608
2609 netdev->init = usb_eth_init;
2610 netdev->send = usb_eth_send;
2611 netdev->recv = usb_eth_recv;
2612 netdev->halt = usb_eth_halt;
Mugunthan V N8ec9c742016-11-18 11:07:18 +05302613 netdev->priv = l_priv;
Remy Bohmerdf063442009-07-29 18:18:43 +02002614
2615#ifdef CONFIG_MCAST_TFTP
2616 #error not supported
2617#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002618 eth_register(netdev);
2619 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002620}
Mugunthan V N095b7612016-11-18 11:09:15 +05302621#else
2622static int usb_eth_start(struct udevice *dev)
2623{
2624 struct ether_priv *priv = dev_get_priv(dev);
2625
2626 return _usb_eth_init(priv);
2627}
2628
2629static int usb_eth_send(struct udevice *dev, void *packet, int length)
2630{
2631 struct ether_priv *priv = dev_get_priv(dev);
2632
2633 return _usb_eth_send(priv, packet, length);
2634}
2635
2636static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2637{
2638 struct ether_priv *priv = dev_get_priv(dev);
2639 struct eth_dev *ethdev = &priv->ethdev;
2640 int ret;
2641
2642 ret = _usb_eth_recv(priv);
2643 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002644 pr_err("error packet receive\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302645 return ret;
2646 }
2647
2648 if (packet_received) {
2649 if (ethdev->rx_req) {
2650 *packetp = (uchar *)net_rx_packets[0];
2651 return ethdev->rx_req->length;
2652 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002653 pr_err("dev->rx_req invalid");
Mugunthan V N095b7612016-11-18 11:09:15 +05302654 return -EFAULT;
2655 }
2656 }
2657
2658 return -EAGAIN;
2659}
2660
2661static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2662 int length)
2663{
2664 struct ether_priv *priv = dev_get_priv(dev);
2665 struct eth_dev *ethdev = &priv->ethdev;
2666
2667 packet_received = 0;
2668
2669 return rx_submit(ethdev, ethdev->rx_req, 0);
2670}
2671
2672static void usb_eth_stop(struct udevice *dev)
2673{
2674 struct ether_priv *priv = dev_get_priv(dev);
2675
2676 _usb_eth_halt(priv);
2677}
2678
2679static int usb_eth_probe(struct udevice *dev)
2680{
2681 struct ether_priv *priv = dev_get_priv(dev);
2682 struct eth_pdata *pdata = dev_get_platdata(dev);
2683
2684 priv->netdev = dev;
2685 l_priv = priv;
2686
2687 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
Simon Glass8551d552017-08-03 12:22:11 -06002688 eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
Mugunthan V N095b7612016-11-18 11:09:15 +05302689
2690 return 0;
2691}
2692
2693static const struct eth_ops usb_eth_ops = {
2694 .start = usb_eth_start,
2695 .send = usb_eth_send,
2696 .recv = usb_eth_recv,
2697 .free_pkt = usb_eth_free_pkt,
2698 .stop = usb_eth_stop,
2699};
2700
2701int usb_ether_init(void)
2702{
2703 struct udevice *dev;
2704 struct udevice *usb_dev;
2705 int ret;
2706
2707 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
2708 if (!usb_dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002709 pr_err("No USB device found\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302710 return ret;
2711 }
2712
2713 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2714 if (!dev || ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09002715 pr_err("usb - not able to bind usb_ether device\n");
Mugunthan V N095b7612016-11-18 11:09:15 +05302716 return ret;
2717 }
2718
2719 return 0;
2720}
2721
2722U_BOOT_DRIVER(eth_usb) = {
2723 .name = "usb_ether",
2724 .id = UCLASS_ETH,
2725 .probe = usb_eth_probe,
2726 .ops = &usb_eth_ops,
2727 .priv_auto_alloc_size = sizeof(struct ether_priv),
2728 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2729 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2730};
2731#endif /* CONFIG_DM_ETH */