blob: f1b070982190362afbccfb9b55f6274e5abdf2b9 [file] [log] [blame]
Remy Bohmer23cd1382009-07-29 18:18:43 +02001/*
2 * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
6 * Copyright (C) 2008 Nokia Corporation
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Remy Bohmer23cd1382009-07-29 18:18:43 +02009 */
10
11#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +030014#include <linux/netdevice.h>
Remy Bohmer23cd1382009-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 I8bfc2882015-08-19 13:49:46 +053019#include <usb.h>
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030020#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060021#include <memalign.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020022#include <linux/ctype.h>
23
24#include "gadget_chips.h"
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030025#include "rndis.h"
Remy Bohmer23cd1382009-07-29 18:18:43 +020026
Mugunthan V Nd4345ae2016-11-18 10:49:12 +053027#include <dm.h>
Mugunthan V Nd4a37552016-11-18 11:09:15 +053028#include <dm/lists.h>
Mugunthan V Nd4345ae2016-11-18 10:49:12 +053029#include <dm/uclass-internal.h>
30#include <dm/device-internal.h>
31
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +030032#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040033
Remy Bohmer23cd1382009-07-29 18:18:43 +020034#define atomic_read
35extern struct platform_data brd;
Remy Bohmer23cd1382009-07-29 18:18:43 +020036
37
38unsigned packet_received, packet_sent;
39
Remy Bohmer23cd1382009-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 Bohmer23cd1382009-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 Kuzmichev6142e0a2010-09-13 18:37:11 +040084static const char driver_desc[] = DRIVER_DESC;
Remy Bohmer23cd1382009-07-29 18:18:43 +020085
86#define RX_EXTRA 20 /* guard against rx overflows */
87
Vitaly Kuzmichev7612a432011-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 Bohmer23cd1382009-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 Kuzmichev8b6b66b2011-02-11 18:18:33 +0300103
104struct eth_dev {
105 struct usb_gadget *gadget;
106 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300107 struct usb_request *stat_req; /* for cdc & rndis status */
Mugunthan V Nd4345ae2016-11-18 10:49:12 +0530108#ifdef CONFIG_DM_USB
109 struct udevice *usb_udev;
110#endif
Vitaly Kuzmichev8b6b66b2011-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 Nd4a37552016-11-18 11:09:15 +0530119#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300120 struct eth_device *net;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530121#else
122 struct udevice *net;
123#endif
Vitaly Kuzmichev8b6b66b2011-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 Kuzmichev7612a432011-02-11 18:18:34 +0300129 unsigned rndis:1;
Vitaly Kuzmichev8b6b66b2011-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 Kuzmichev7612a432011-02-11 18:18:34 +0300136 int rndis_config;
Vitaly Kuzmichev8b6b66b2011-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 N5cb3b9d2016-11-18 11:06:13 +0530148struct ether_priv {
149 struct eth_dev ethdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530150#ifndef CONFIG_DM_ETH
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +0530151 struct eth_device netdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +0530152#else
153 struct udevice *netdev;
154#endif
Mugunthan V N5cb3b9d2016-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 Bohmer23cd1382009-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 Dalek2bb37882012-10-02 17:04:31 +0200166#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200167 return 1; /* only cdc possible */
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200168#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmer23cd1382009-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 Kuzmichev7612a432011-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 Bohmer23cd1382009-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 Kuzmichev2721dbf2010-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 Bohmer23cd1382009-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 Bohmer23cd1382009-07-29 18:18:43 +0200226/*-------------------------------------------------------------------------*/
227
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400228/*
229 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmer23cd1382009-07-29 18:18:43 +0200230 * Instead: allocate your own, using normal USB-IF procedures.
231 */
232
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400233/*
234 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmer23cd1382009-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 Kuzmichev6142e0a2010-09-13 18:37:11 +0400240/*
241 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmer23cd1382009-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 Kuzmichev7612a432011-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 */
254
255/*
256 * 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 */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200263
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400264/*
265 * Some systems will want different product identifers published in the
Remy Bohmer23cd1382009-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 Kuzmichev7612a432011-02-11 18:18:34 +0300270/*
271 * Emulating them in eth_bind:
272 * static ushort idVendor;
273 * static ushort idProduct;
274 */
275
Remy Bohmer23cd1382009-07-29 18:18:43 +0200276#if defined(CONFIG_USBNET_MANUFACTURER)
277static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
278#else
Bin Menga1875592016-02-05 19:30:11 -0800279static char *iManufacturer = "U-Boot";
Remy Bohmer23cd1382009-07-29 18:18:43 +0200280#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300281
282/* These probably need to be configurable. */
283static ushort bcdDevice;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200284static char *iProduct;
285static char *iSerialNumber;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300286
Remy Bohmer23cd1382009-07-29 18:18:43 +0200287static char dev_addr[18];
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300288
Remy Bohmer23cd1382009-07-29 18:18:43 +0200289static char host_addr[18];
290
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300291
Remy Bohmer23cd1382009-07-29 18:18:43 +0200292/*-------------------------------------------------------------------------*/
293
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400294/*
295 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmer23cd1382009-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 Kuzmichev7612a432011-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 Bohmer23cd1382009-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 Kuzmichev7612a432011-02-11 18:18:34 +0300316#define STRING_RNDIS_CONTROL 6
Remy Bohmer23cd1382009-07-29 18:18:43 +0200317#define STRING_CDC 7
318#define STRING_SUBSET 8
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300319#define STRING_RNDIS 9
Remy Bohmer23cd1382009-07-29 18:18:43 +0200320#define STRING_SERIALNUMBER 10
321
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300322/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200323#define USB_BUFSIZ 256
324
325/*
Vitaly Kuzmichev7612a432011-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 Bohmer23cd1382009-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 Kuzmichev7612a432011-02-11 18:18:34 +0300337#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmer23cd1382009-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 Kuzmichev6142e0a2010-09-13 18:37:11 +0400344 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200345
346 .bDeviceClass = USB_CLASS_COMM,
347 .bDeviceSubClass = 0,
348 .bDeviceProtocol = 0,
349
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400350 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
351 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmer23cd1382009-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};
377
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300378#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
392
Remy Bohmer23cd1382009-07-29 18:18:43 +0200393/*
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 Kuzmichev7612a432011-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 Bohmer23cd1382009-07-29 18:18:43 +0200410 */
411
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200412#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-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 Kuzmichev7612a432011-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 Bohmer23cd1382009-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 Kuzmichev6142e0a2010-09-13 18:37:11 +0400448 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200449};
450
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200451#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-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 Kuzmichev7612a432011-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 Bohmer23cd1382009-07-29 18:18:43 +0200484
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200485#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200486
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400487/*
488 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmer23cd1382009-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 Kuzmichev6142e0a2010-09-13 18:37:11 +0400505/*
506 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmer23cd1382009-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 */
510static const u8 mdlm_detail_desc[] = {
511 6,
512 USB_DT_CS_INTERFACE,
513 USB_CDC_MDLM_DETAIL_TYPE,
514
515 0, /* "SAFE" */
516 0, /* network control capabilities (none) */
517 0, /* network data capabilities ("raw" encapsulation) */
518};
519
520#endif
521
Remy Bohmer23cd1382009-07-29 18:18:43 +0200522static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400523 .bLength = sizeof(ether_desc),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200524 .bDescriptorType = USB_DT_CS_INTERFACE,
525 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
526
527 /* this descriptor actually adds value, surprise! */
528 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400529 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
530 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
531 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200532 .bNumberPowerFilters = 0,
533};
534
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200535#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200536
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400537/*
538 * include the status endpoint if we can, even where it's optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200539 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
540 * packet, to simplify cancellation; and a big transfer interval, to
541 * waste less bandwidth.
542 *
543 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
544 * if they ignore the connect/disconnect notifications that real aether
545 * can provide. more advanced cdc configurations might want to support
546 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300547 *
548 * RNDIS requires the status endpoint, since it uses that encapsulation
549 * mechanism for its funky RPC scheme.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200550 */
551
552#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
553#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
554
555static struct usb_endpoint_descriptor
556fs_status_desc = {
557 .bLength = USB_DT_ENDPOINT_SIZE,
558 .bDescriptorType = USB_DT_ENDPOINT,
559
560 .bEndpointAddress = USB_DIR_IN,
561 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400562 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200563 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
564};
565#endif
566
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200567#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200568
569/* the default data interface has no endpoints ... */
570
571static const struct usb_interface_descriptor
572data_nop_intf = {
573 .bLength = sizeof data_nop_intf,
574 .bDescriptorType = USB_DT_INTERFACE,
575
576 .bInterfaceNumber = 1,
577 .bAlternateSetting = 0,
578 .bNumEndpoints = 0,
579 .bInterfaceClass = USB_CLASS_CDC_DATA,
580 .bInterfaceSubClass = 0,
581 .bInterfaceProtocol = 0,
582};
583
584/* ... but the "real" data interface has two bulk endpoints */
585
586static const struct usb_interface_descriptor
587data_intf = {
588 .bLength = sizeof data_intf,
589 .bDescriptorType = USB_DT_INTERFACE,
590
591 .bInterfaceNumber = 1,
592 .bAlternateSetting = 1,
593 .bNumEndpoints = 2,
594 .bInterfaceClass = USB_CLASS_CDC_DATA,
595 .bInterfaceSubClass = 0,
596 .bInterfaceProtocol = 0,
597 .iInterface = STRING_DATA,
598};
599
600#endif
601
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300602#ifdef CONFIG_USB_ETH_RNDIS
603
604/* RNDIS doesn't activate by changing to the "real" altsetting */
605
606static const struct usb_interface_descriptor
607rndis_data_intf = {
608 .bLength = sizeof rndis_data_intf,
609 .bDescriptorType = USB_DT_INTERFACE,
610
611 .bInterfaceNumber = 1,
612 .bAlternateSetting = 0,
613 .bNumEndpoints = 2,
614 .bInterfaceClass = USB_CLASS_CDC_DATA,
615 .bInterfaceSubClass = 0,
616 .bInterfaceProtocol = 0,
617 .iInterface = STRING_DATA,
618};
619
620#endif
621
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200622#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200623
624/*
625 * "Simple" CDC-subset option is a simple vendor-neutral model that most
626 * full speed controllers can handle: one interface, two bulk endpoints.
627 *
628 * To assist host side drivers, we fancy it up a bit, and add descriptors
629 * so some host side drivers will understand it as a "SAFE" variant.
630 */
631
632static const struct usb_interface_descriptor
633subset_data_intf = {
634 .bLength = sizeof subset_data_intf,
635 .bDescriptorType = USB_DT_INTERFACE,
636
637 .bInterfaceNumber = 0,
638 .bAlternateSetting = 0,
639 .bNumEndpoints = 2,
640 .bInterfaceClass = USB_CLASS_COMM,
641 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
642 .bInterfaceProtocol = 0,
643 .iInterface = STRING_DATA,
644};
645
646#endif /* SUBSET */
647
Remy Bohmer23cd1382009-07-29 18:18:43 +0200648static struct usb_endpoint_descriptor
649fs_source_desc = {
650 .bLength = USB_DT_ENDPOINT_SIZE,
651 .bDescriptorType = USB_DT_ENDPOINT,
652
653 .bEndpointAddress = USB_DIR_IN,
654 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700655 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200656};
657
658static struct usb_endpoint_descriptor
659fs_sink_desc = {
660 .bLength = USB_DT_ENDPOINT_SIZE,
661 .bDescriptorType = USB_DT_ENDPOINT,
662
663 .bEndpointAddress = USB_DIR_OUT,
664 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700665 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200666};
667
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400668static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200669 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200670#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200671 /* "cdc" mode descriptors */
672 (struct usb_descriptor_header *) &control_intf,
673 (struct usb_descriptor_header *) &header_desc,
674 (struct usb_descriptor_header *) &union_desc,
675 (struct usb_descriptor_header *) &ether_desc,
676 /* NOTE: status endpoint may need to be removed */
677 (struct usb_descriptor_header *) &fs_status_desc,
678 /* data interface, with altsetting */
679 (struct usb_descriptor_header *) &data_nop_intf,
680 (struct usb_descriptor_header *) &data_intf,
681 (struct usb_descriptor_header *) &fs_source_desc,
682 (struct usb_descriptor_header *) &fs_sink_desc,
683 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200684#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200685};
686
687static inline void fs_subset_descriptors(void)
688{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200689#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200690 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
691 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
692 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
693 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
694 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
695 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
696 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
697 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
698 fs_eth_function[8] = NULL;
699#else
700 fs_eth_function[1] = NULL;
701#endif
702}
703
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300704#ifdef CONFIG_USB_ETH_RNDIS
705static const struct usb_descriptor_header *fs_rndis_function[] = {
706 (struct usb_descriptor_header *) &otg_descriptor,
707 /* control interface matches ACM, not Ethernet */
708 (struct usb_descriptor_header *) &rndis_control_intf,
709 (struct usb_descriptor_header *) &header_desc,
710 (struct usb_descriptor_header *) &call_mgmt_descriptor,
711 (struct usb_descriptor_header *) &acm_descriptor,
712 (struct usb_descriptor_header *) &union_desc,
713 (struct usb_descriptor_header *) &fs_status_desc,
714 /* data interface has no altsetting */
715 (struct usb_descriptor_header *) &rndis_data_intf,
716 (struct usb_descriptor_header *) &fs_source_desc,
717 (struct usb_descriptor_header *) &fs_sink_desc,
718 NULL,
719};
720#endif
721
Remy Bohmer23cd1382009-07-29 18:18:43 +0200722/*
723 * usb 2.0 devices need to expose both high speed and full speed
724 * descriptors, unless they only run at full speed.
725 */
726
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200727#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200728static struct usb_endpoint_descriptor
729hs_status_desc = {
730 .bLength = USB_DT_ENDPOINT_SIZE,
731 .bDescriptorType = USB_DT_ENDPOINT,
732
733 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400734 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200735 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
736};
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200737#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200738
739static struct usb_endpoint_descriptor
740hs_source_desc = {
741 .bLength = USB_DT_ENDPOINT_SIZE,
742 .bDescriptorType = USB_DT_ENDPOINT,
743
744 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400745 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200746};
747
748static struct usb_endpoint_descriptor
749hs_sink_desc = {
750 .bLength = USB_DT_ENDPOINT_SIZE,
751 .bDescriptorType = USB_DT_ENDPOINT,
752
753 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400754 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200755};
756
757static struct usb_qualifier_descriptor
758dev_qualifier = {
759 .bLength = sizeof dev_qualifier,
760 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
761
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400762 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200763 .bDeviceClass = USB_CLASS_COMM,
764
765 .bNumConfigurations = 1,
766};
767
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400768static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200769 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200770#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200771 /* "cdc" mode descriptors */
772 (struct usb_descriptor_header *) &control_intf,
773 (struct usb_descriptor_header *) &header_desc,
774 (struct usb_descriptor_header *) &union_desc,
775 (struct usb_descriptor_header *) &ether_desc,
776 /* NOTE: status endpoint may need to be removed */
777 (struct usb_descriptor_header *) &hs_status_desc,
778 /* data interface, with altsetting */
779 (struct usb_descriptor_header *) &data_nop_intf,
780 (struct usb_descriptor_header *) &data_intf,
781 (struct usb_descriptor_header *) &hs_source_desc,
782 (struct usb_descriptor_header *) &hs_sink_desc,
783 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200784#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200785};
786
787static inline void hs_subset_descriptors(void)
788{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200789#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200790 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
791 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
792 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
793 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
794 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
795 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
796 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
797 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
798 hs_eth_function[8] = NULL;
799#else
800 hs_eth_function[1] = NULL;
801#endif
802}
803
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300804#ifdef CONFIG_USB_ETH_RNDIS
805static const struct usb_descriptor_header *hs_rndis_function[] = {
806 (struct usb_descriptor_header *) &otg_descriptor,
807 /* control interface matches ACM, not Ethernet */
808 (struct usb_descriptor_header *) &rndis_control_intf,
809 (struct usb_descriptor_header *) &header_desc,
810 (struct usb_descriptor_header *) &call_mgmt_descriptor,
811 (struct usb_descriptor_header *) &acm_descriptor,
812 (struct usb_descriptor_header *) &union_desc,
813 (struct usb_descriptor_header *) &hs_status_desc,
814 /* data interface has no altsetting */
815 (struct usb_descriptor_header *) &rndis_data_intf,
816 (struct usb_descriptor_header *) &hs_source_desc,
817 (struct usb_descriptor_header *) &hs_sink_desc,
818 NULL,
819};
820#endif
821
822
Remy Bohmer23cd1382009-07-29 18:18:43 +0200823/* maxpacket and other transfer characteristics vary by speed. */
824static inline struct usb_endpoint_descriptor *
825ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
826 struct usb_endpoint_descriptor *fs)
827{
828 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
829 return hs;
830 return fs;
831}
832
Remy Bohmer23cd1382009-07-29 18:18:43 +0200833/*-------------------------------------------------------------------------*/
834
835/* descriptors that are built on-demand */
836
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400837static char manufacturer[50];
838static char product_desc[40] = DRIVER_DESC;
839static char serial_number[20];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200840
841/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400842static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200843
844/* static strings, in UTF-8 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400845static struct usb_string strings[] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200846 { STRING_MANUFACTURER, manufacturer, },
847 { STRING_PRODUCT, product_desc, },
848 { STRING_SERIALNUMBER, serial_number, },
849 { STRING_DATA, "Ethernet Data", },
850 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200851#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200852 { STRING_CDC, "CDC Ethernet", },
853 { STRING_CONTROL, "CDC Communications Control", },
854#endif
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200855#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200856 { STRING_SUBSET, "CDC Ethernet Subset", },
857#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300858#ifdef CONFIG_USB_ETH_RNDIS
859 { STRING_RNDIS, "RNDIS", },
860 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
861#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200862 { } /* end of list */
863};
864
865static struct usb_gadget_strings stringtab = {
866 .language = 0x0409, /* en-us */
867 .strings = strings,
868};
869
Remy Bohmer23cd1382009-07-29 18:18:43 +0200870/*============================================================================*/
Joel Fernandes52907592013-09-04 18:55:14 -0500871DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
872
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200873#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes52907592013-09-04 18:55:14 -0500874DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek563aed22012-10-02 17:04:29 +0200875#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200876
Remy Bohmer23cd1382009-07-29 18:18:43 +0200877/*============================================================================*/
878
879/*
880 * one config, two interfaces: control, data.
881 * complications: class descriptors, and an altsetting.
882 */
883static int
884config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
885{
886 int len;
887 const struct usb_config_descriptor *config;
888 const struct usb_descriptor_header **function;
889 int hs = 0;
890
891 if (gadget_is_dualspeed(g)) {
892 hs = (g->speed == USB_SPEED_HIGH);
893 if (type == USB_DT_OTHER_SPEED_CONFIG)
894 hs = !hs;
895 }
896#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
897
898 if (index >= device_desc.bNumConfigurations)
899 return -EINVAL;
900
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300901#ifdef CONFIG_USB_ETH_RNDIS
902 /*
903 * list the RNDIS config first, to make Microsoft's drivers
904 * happy. DOCSIS 1.0 needs this too.
905 */
906 if (device_desc.bNumConfigurations == 2 && index == 0) {
907 config = &rndis_config;
908 function = which_fn(rndis);
909 } else
910#endif
911 {
912 config = &eth_config;
913 function = which_fn(eth);
914 }
Remy Bohmer23cd1382009-07-29 18:18:43 +0200915
916 /* for now, don't advertise srp-only devices */
917 if (!is_otg)
918 function++;
919
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400920 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200921 if (len < 0)
922 return len;
923 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
924 return len;
925}
926
927/*-------------------------------------------------------------------------*/
928
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300929static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400930static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200931
932static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400933set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200934{
935 int result = 0;
936 struct usb_gadget *gadget = dev->gadget;
937
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200938#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300939 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200940 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400941 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmer23cd1382009-07-29 18:18:43 +0200942 &fs_status_desc);
943 dev->status_ep->driver_data = dev;
944
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400945 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200946 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400947 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200948 dev->status_ep->name, result);
949 goto done;
950 }
951 }
952#endif
953
954 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
955 dev->in_ep->driver_data = dev;
956
957 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
958 dev->out_ep->driver_data = dev;
959
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400960 /*
961 * With CDC, the host isn't allowed to use these two data
Remy Bohmer23cd1382009-07-29 18:18:43 +0200962 * endpoints in the default altsetting for the interface.
963 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300964 *
965 * Strictly speaking RNDIS should work the same: activation is
966 * a side effect of setting a packet filter. Deactivation is
967 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200968 */
969 if (!cdc_active(dev)) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400970 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200971 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400972 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200973 dev->in_ep->name, result);
974 goto done;
975 }
976
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400977 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200978 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400979 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200980 dev->out_ep->name, result);
981 goto done;
982 }
983 }
984
985done:
986 if (result == 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400987 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200988
989 /* on error, disable any endpoints */
990 if (result < 0) {
Vitaly Kuzmichevd5292c12010-08-13 17:02:29 +0400991 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400992 (void) usb_ep_disable(dev->status_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200993 dev->status = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400994 (void) usb_ep_disable(dev->in_ep);
995 (void) usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200996 dev->in = NULL;
997 dev->out = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300998 } else if (!cdc_active(dev)) {
999 /*
1000 * activate non-CDC configs right away
1001 * this isn't strictly according to the RNDIS spec
1002 */
1003 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001004 }
1005
1006 /* caller is responsible for cleanup on error */
1007 return result;
1008}
1009
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001010static void eth_reset_config(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001011{
1012 if (dev->config == 0)
1013 return;
1014
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001015 debug("%s\n", __func__);
1016
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001017 rndis_uninit(dev->rndis_config);
1018
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001019 /*
1020 * disable endpoints, forcing (synchronous) completion of
Remy Bohmer23cd1382009-07-29 18:18:43 +02001021 * pending i/o. then free the requests.
1022 */
1023
1024 if (dev->in) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001025 usb_ep_disable(dev->in_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001026 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001027 usb_ep_free_request(dev->in_ep, dev->tx_req);
1028 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001029 }
1030 }
1031 if (dev->out) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001032 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001033 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001034 usb_ep_free_request(dev->out_ep, dev->rx_req);
1035 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001036 }
1037 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001038 if (dev->status)
1039 usb_ep_disable(dev->status_ep);
1040
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001041 dev->rndis = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001042 dev->cdc_filter = 0;
1043 dev->config = 0;
1044}
1045
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001046/*
1047 * change our operational config. must agree with the code
Remy Bohmer23cd1382009-07-29 18:18:43 +02001048 * that returns config descriptors, and altsetting code.
1049 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001050static int eth_set_config(struct eth_dev *dev, unsigned number,
1051 gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001052{
1053 int result = 0;
1054 struct usb_gadget *gadget = dev->gadget;
1055
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001056 if (gadget_is_sa1100(gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001057 && dev->config
1058 && dev->tx_qlen != 0) {
1059 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001060 error("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001061 return -ESPIPE;
1062 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001063 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001064
1065 switch (number) {
1066 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001067 result = set_ether_config(dev, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001068 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001069#ifdef CONFIG_USB_ETH_RNDIS
1070 case DEV_RNDIS_CONFIG_VALUE:
1071 dev->rndis = 1;
1072 result = set_ether_config(dev, gfp_flags);
1073 break;
1074#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02001075 default:
1076 result = -EINVAL;
1077 /* FALL THROUGH */
1078 case 0:
1079 break;
1080 }
1081
1082 if (result) {
1083 if (number)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001084 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001085 usb_gadget_vbus_draw(dev->gadget,
1086 gadget_is_otg(dev->gadget) ? 8 : 100);
1087 } else {
1088 char *speed;
1089 unsigned power;
1090
1091 power = 2 * eth_config.bMaxPower;
1092 usb_gadget_vbus_draw(dev->gadget, power);
1093
1094 switch (gadget->speed) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001095 case USB_SPEED_FULL:
1096 speed = "full"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001097#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001098 case USB_SPEED_HIGH:
1099 speed = "high"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001100#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001101 default:
1102 speed = "?"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001103 }
1104
1105 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001106 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001107 speed, number, power, driver_desc,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001108 rndis_active(dev)
1109 ? "RNDIS"
1110 : (cdc_active(dev)
1111 ? "CDC Ethernet"
Remy Bohmer23cd1382009-07-29 18:18:43 +02001112 : "CDC Ethernet Subset"));
1113 }
1114 return result;
1115}
1116
1117/*-------------------------------------------------------------------------*/
1118
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001119#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001120
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001121/*
1122 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001123 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001124 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmer23cd1382009-07-29 18:18:43 +02001125 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1126 * command mechanism; and only one status request is ever queued.
1127 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001128static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001129{
1130 struct usb_cdc_notification *event = req->buf;
1131 int value = req->status;
1132 struct eth_dev *dev = ep->driver_data;
1133
1134 /* issue the second notification if host reads the first */
1135 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1136 && value == 0) {
1137 __le32 *data = req->buf + sizeof *event;
1138
1139 event->bmRequestType = 0xA1;
1140 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001141 event->wValue = __constant_cpu_to_le16(0);
1142 event->wIndex = __constant_cpu_to_le16(1);
1143 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001144
1145 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001146 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmer23cd1382009-07-29 18:18:43 +02001147
1148 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001149 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001150 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001151 if (value == 0)
1152 return;
1153 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001154 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001155 event->bNotificationType, value);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001156 if (event->bNotificationType ==
1157 USB_CDC_NOTIFY_SPEED_CHANGE) {
Mugunthan V N17b4f302016-11-18 10:49:13 +05301158 dev->network_started = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001159 printf("USB network up!\n");
1160 }
1161 }
1162 req->context = NULL;
1163}
1164
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001165static void issue_start_status(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001166{
1167 struct usb_request *req = dev->stat_req;
1168 struct usb_cdc_notification *event;
1169 int value;
1170
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001171 /*
1172 * flush old status
Remy Bohmer23cd1382009-07-29 18:18:43 +02001173 *
1174 * FIXME ugly idiom, maybe we'd be better with just
1175 * a "cancel the whole queue" primitive since any
1176 * unlink-one primitive has way too many error modes.
1177 * here, we "know" toggle is already clear...
1178 *
1179 * FIXME iff req->context != null just dequeue it
1180 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001181 usb_ep_disable(dev->status_ep);
1182 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001183
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001184 /*
1185 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmer23cd1382009-07-29 18:18:43 +02001186 * a SPEED_CHANGE. could be useful in some configs.
1187 */
1188 event = req->buf;
1189 event->bmRequestType = 0xA1;
1190 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001191 event->wValue = __constant_cpu_to_le16(1); /* connected */
1192 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001193 event->wLength = 0;
1194
1195 req->length = sizeof *event;
1196 req->complete = eth_status_complete;
1197 req->context = dev;
1198
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001199 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001200 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001201 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001202}
1203
1204#endif
1205
1206/*-------------------------------------------------------------------------*/
1207
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001208static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001209{
1210 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001211 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001212 req->status, req->actual, req->length);
1213}
1214
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001215#ifdef CONFIG_USB_ETH_RNDIS
1216
1217static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1218{
1219 if (req->status || req->actual != req->length)
1220 debug("rndis response complete --> %d, %d/%d\n",
1221 req->status, req->actual, req->length);
1222
1223 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1224}
1225
1226static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1227{
1228 struct eth_dev *dev = ep->driver_data;
1229 int status;
1230
1231 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1232 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1233 if (status < 0)
1234 error("%s: rndis parse error %d", __func__, status);
1235}
1236
1237#endif /* RNDIS */
1238
Remy Bohmer23cd1382009-07-29 18:18:43 +02001239/*
1240 * The setup() callback implements all the ep0 functionality that's not
1241 * handled lower down. CDC has a number of less-common features:
1242 *
1243 * - two interfaces: control, and ethernet data
1244 * - Ethernet data interface has two altsettings: default, and active
1245 * - class-specific descriptors for the control interface
1246 * - class-specific control requests
1247 */
1248static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001249eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001250{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001251 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001252 struct usb_request *req = dev->req;
1253 int value = -EOPNOTSUPP;
1254 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1255 u16 wValue = le16_to_cpu(ctrl->wValue);
1256 u16 wLength = le16_to_cpu(ctrl->wLength);
1257
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001258 /*
1259 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001260 * while config change events may enable network traffic.
1261 */
1262
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001263 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001264
1265 req->complete = eth_setup_complete;
1266 switch (ctrl->bRequest) {
1267
1268 case USB_REQ_GET_DESCRIPTOR:
1269 if (ctrl->bRequestType != USB_DIR_IN)
1270 break;
1271 switch (wValue >> 8) {
1272
1273 case USB_DT_DEVICE:
Kishon Vijay Abraham I04afd5b2015-02-23 18:40:17 +05301274 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001275 value = min(wLength, (u16) sizeof device_desc);
1276 memcpy(req->buf, &device_desc, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001277 break;
1278 case USB_DT_DEVICE_QUALIFIER:
1279 if (!gadget_is_dualspeed(gadget))
1280 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001281 value = min(wLength, (u16) sizeof dev_qualifier);
1282 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001283 break;
1284
1285 case USB_DT_OTHER_SPEED_CONFIG:
1286 if (!gadget_is_dualspeed(gadget))
1287 break;
1288 /* FALLTHROUGH */
1289 case USB_DT_CONFIG:
1290 value = config_buf(gadget, req->buf,
1291 wValue >> 8,
1292 wValue & 0xff,
1293 gadget_is_otg(gadget));
1294 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001295 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001296 break;
1297
1298 case USB_DT_STRING:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001299 value = usb_gadget_get_string(&stringtab,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001300 wValue & 0xff, req->buf);
1301
1302 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001303 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001304
1305 break;
1306 }
1307 break;
1308
1309 case USB_REQ_SET_CONFIGURATION:
1310 if (ctrl->bRequestType != 0)
1311 break;
1312 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001313 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001314 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001315 debug("HNP needs a different root port\n");
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001316 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001317 break;
1318 case USB_REQ_GET_CONFIGURATION:
1319 if (ctrl->bRequestType != USB_DIR_IN)
1320 break;
1321 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001322 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001323 break;
1324
1325 case USB_REQ_SET_INTERFACE:
1326 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1327 || !dev->config
1328 || wIndex > 1)
1329 break;
1330 if (!cdc_active(dev) && wIndex != 0)
1331 break;
1332
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001333 /*
1334 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001335 * we need to kluge around that interference.
1336 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001337 if (gadget_is_pxa(gadget)) {
1338 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001339 GFP_ATOMIC);
Lukasz Dalek563aed22012-10-02 17:04:29 +02001340 /*
1341 * PXA25x driver use non-CDC ethernet gadget.
1342 * But only _CDC and _RNDIS code can signalize
1343 * that network is working. So we signalize it
1344 * here.
1345 */
Mugunthan V N17b4f302016-11-18 10:49:13 +05301346 dev->network_started = 1;
Lukasz Dalek563aed22012-10-02 17:04:29 +02001347 debug("USB network up!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001348 goto done_set_intf;
1349 }
1350
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001351#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001352 switch (wIndex) {
1353 case 0: /* control/master intf */
1354 if (wValue != 0)
1355 break;
1356 if (dev->status) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001357 usb_ep_disable(dev->status_ep);
1358 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001359 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001360
Remy Bohmer23cd1382009-07-29 18:18:43 +02001361 value = 0;
1362 break;
1363 case 1: /* data intf */
1364 if (wValue > 1)
1365 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001366 usb_ep_disable(dev->in_ep);
1367 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001368
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001369 /*
1370 * CDC requires the data transfers not be done from
Remy Bohmer23cd1382009-07-29 18:18:43 +02001371 * the default interface setting ... also, setting
1372 * the non-default interface resets filters etc.
1373 */
1374 if (wValue == 1) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001375 if (!cdc_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001376 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001377 usb_ep_enable(dev->in_ep, dev->in);
1378 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001379 dev->cdc_filter = DEFAULT_FILTER;
1380 if (dev->status)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001381 issue_start_status(dev);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001382 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001383 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001384 value = 0;
1385 break;
1386 }
1387#else
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001388 /*
1389 * FIXME this is wrong, as is the assumption that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001390 * all non-PXA hardware talks real CDC ...
1391 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001392 debug("set_interface ignored!\n");
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001393#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001394
1395done_set_intf:
1396 break;
1397 case USB_REQ_GET_INTERFACE:
1398 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1399 || !dev->config
1400 || wIndex > 1)
1401 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001402 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001403 break;
1404
1405 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001406 if (rndis_active(dev) || wIndex != 1)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001407 *(u8 *)req->buf = 0;
1408 else {
1409 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1410 /* carrier always ok ...*/
1411 *(u8 *)req->buf = 1 ;
1412 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001413 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001414 break;
1415
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001416#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001417 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001418 /*
1419 * see 6.2.30: no data, wIndex = interface,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001420 * wValue = packet filter bitmap
1421 */
1422 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1423 || !cdc_active(dev)
1424 || wLength != 0
1425 || wIndex > 1)
1426 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001427 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001428 dev->cdc_filter = wValue;
1429 value = 0;
1430 break;
1431
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001432 /*
1433 * and potentially:
Remy Bohmer23cd1382009-07-29 18:18:43 +02001434 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1435 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1436 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1437 * case USB_CDC_GET_ETHERNET_STATISTIC:
1438 */
1439
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001440#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001441
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001442#ifdef CONFIG_USB_ETH_RNDIS
1443 /*
1444 * RNDIS uses the CDC command encapsulation mechanism to implement
1445 * an RPC scheme, with much getting/setting of attributes by OID.
1446 */
1447 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1448 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1449 || !rndis_active(dev)
1450 || wLength > USB_BUFSIZ
1451 || wValue
1452 || rndis_control_intf.bInterfaceNumber
1453 != wIndex)
1454 break;
1455 /* read the request, then process it */
1456 value = wLength;
1457 req->complete = rndis_command_complete;
1458 /* later, rndis_control_ack () sends a notification */
1459 break;
1460
1461 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1462 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1463 == ctrl->bRequestType
1464 && rndis_active(dev)
1465 /* && wLength >= 0x0400 */
1466 && !wValue
1467 && rndis_control_intf.bInterfaceNumber
1468 == wIndex) {
1469 u8 *buf;
1470 u32 n;
1471
1472 /* return the result */
1473 buf = rndis_get_next_response(dev->rndis_config, &n);
1474 if (buf) {
1475 memcpy(req->buf, buf, n);
1476 req->complete = rndis_response_complete;
1477 rndis_free_response(dev->rndis_config, buf);
1478 value = n;
1479 }
1480 /* else stalls ... spec says to avoid that */
1481 }
1482 break;
1483#endif /* RNDIS */
1484
Remy Bohmer23cd1382009-07-29 18:18:43 +02001485 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001486 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001487 ctrl->bRequestType, ctrl->bRequest,
1488 wValue, wIndex, wLength);
1489 }
1490
1491 /* respond with data transfer before status phase? */
1492 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001493 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001494 req->length = value;
1495 req->zero = value < wLength
1496 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001497 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001498 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001499 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001500 req->status = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001501 eth_setup_complete(gadget->ep0, req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001502 }
1503 }
1504
1505 /* host either stalls (value < 0) or reports success */
1506 return value;
1507}
1508
Remy Bohmer23cd1382009-07-29 18:18:43 +02001509/*-------------------------------------------------------------------------*/
1510
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001511static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001512
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001513static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001514 gfp_t gfp_flags)
1515{
1516 int retval = -ENOMEM;
1517 size_t size;
1518
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001519 /*
1520 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001521 * Normally we use the USB "terminate on short read" convention;
1522 * so allow up to (N*maxpacket), since that memory is normally
1523 * already allocated. Some hardware doesn't deal well with short
1524 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1525 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001526 *
1527 * RNDIS uses internal framing, and explicitly allows senders to
1528 * pad to end-of-packet. That's potentially nice for speed,
1529 * but means receivers can't recover synch on their own.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001530 */
1531
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001532 debug("%s\n", __func__);
Troy Kisky71fc5f92013-09-25 18:41:05 -07001533 if (!req)
1534 return -EINVAL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001535
1536 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1537 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001538 if (rndis_active(dev))
1539 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001540 size -= size % dev->out_ep->maxpacket;
1541
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001542 /*
1543 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001544 * but on at least one, checksumming fails otherwise. Note:
1545 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001546 */
1547
Joe Hershberger1fd92db2015-04-08 01:41:06 -05001548 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmer23cd1382009-07-29 18:18:43 +02001549 req->length = size;
1550 req->complete = rx_complete;
1551
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001552 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001553
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001554 if (retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001555 error("rx submit --> %d", retval);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001556
Remy Bohmer23cd1382009-07-29 18:18:43 +02001557 return retval;
1558}
1559
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001560static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001561{
1562 struct eth_dev *dev = ep->driver_data;
1563
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001564 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001565 switch (req->status) {
1566 /* normal completion */
1567 case 0:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001568 if (rndis_active(dev)) {
1569 /* we know MaxPacketsPerTransfer == 1 here */
1570 int length = rndis_rm_hdr(req->buf, req->actual);
1571 if (length < 0)
1572 goto length_err;
1573 req->length -= length;
1574 req->actual -= length;
1575 }
1576 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1577length_err:
1578 dev->stats.rx_errors++;
1579 dev->stats.rx_length_errors++;
1580 debug("rx length %d\n", req->length);
1581 break;
1582 }
1583
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001584 dev->stats.rx_packets++;
1585 dev->stats.rx_bytes += req->length;
1586 break;
1587
1588 /* software-driven interface shutdown */
1589 case -ECONNRESET: /* unlink */
1590 case -ESHUTDOWN: /* disconnect etc */
1591 /* for hardware automagic (such as pxa) */
1592 case -ECONNABORTED: /* endpoint reset */
1593 break;
1594
1595 /* data overrun */
1596 case -EOVERFLOW:
1597 dev->stats.rx_over_errors++;
1598 /* FALLTHROUGH */
1599 default:
1600 dev->stats.rx_errors++;
1601 break;
1602 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001603
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001604 packet_received = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001605}
1606
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001607static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001608{
1609
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001610 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001611
1612 if (!dev->tx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001613 goto fail1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001614
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001615 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001616
1617 if (!dev->rx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001618 goto fail2;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001619
1620 return 0;
1621
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001622fail2:
1623 usb_ep_free_request(dev->in_ep, dev->tx_req);
1624fail1:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001625 error("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001626 return -1;
1627}
1628
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001629static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001630{
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001631 struct eth_dev *dev = ep->driver_data;
1632
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001633 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001634 switch (req->status) {
1635 default:
1636 dev->stats.tx_errors++;
1637 debug("tx err %d\n", req->status);
1638 /* FALLTHROUGH */
1639 case -ECONNRESET: /* unlink */
1640 case -ESHUTDOWN: /* disconnect etc */
1641 break;
1642 case 0:
1643 dev->stats.tx_bytes += req->length;
1644 }
1645 dev->stats.tx_packets++;
1646
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001647 packet_sent = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001648}
1649
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001650static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001651{
1652 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001653 if (subset_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001654 return 1;
1655 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1656}
1657
1658#if 0
1659static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1660{
1661 struct eth_dev *dev = netdev_priv(net);
1662 int length = skb->len;
1663 int retval;
1664 struct usb_request *req = NULL;
1665 unsigned long flags;
1666
1667 /* apply outgoing CDC or RNDIS filters */
1668 if (!eth_is_promisc (dev)) {
1669 u8 *dest = skb->data;
1670
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001671 if (is_multicast_ethaddr(dest)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02001672 u16 type;
1673
1674 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1675 * SET_ETHERNET_MULTICAST_FILTERS requests
1676 */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001677 if (is_broadcast_ethaddr(dest))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001678 type = USB_CDC_PACKET_TYPE_BROADCAST;
1679 else
1680 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1681 if (!(dev->cdc_filter & type)) {
1682 dev_kfree_skb_any (skb);
1683 return 0;
1684 }
1685 }
1686 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1687 }
1688
1689 spin_lock_irqsave(&dev->req_lock, flags);
1690 /*
1691 * this freelist can be empty if an interrupt triggered disconnect()
1692 * and reconfigured the gadget (shutting down this queue) after the
1693 * network stack decided to xmit but before we got the spinlock.
1694 */
1695 if (list_empty(&dev->tx_reqs)) {
1696 spin_unlock_irqrestore(&dev->req_lock, flags);
1697 return 1;
1698 }
1699
1700 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1701 list_del (&req->list);
1702
1703 /* temporarily stop TX queue when the freelist empties */
1704 if (list_empty (&dev->tx_reqs))
1705 netif_stop_queue (net);
1706 spin_unlock_irqrestore(&dev->req_lock, flags);
1707
1708 /* no buffer copies needed, unless the network stack did it
1709 * or the hardware can't use skb buffers.
1710 * or there's not enough space for any RNDIS headers we need
1711 */
1712 if (rndis_active(dev)) {
1713 struct sk_buff *skb_rndis;
1714
1715 skb_rndis = skb_realloc_headroom (skb,
1716 sizeof (struct rndis_packet_msg_type));
1717 if (!skb_rndis)
1718 goto drop;
1719
1720 dev_kfree_skb_any (skb);
1721 skb = skb_rndis;
1722 rndis_add_hdr (skb);
1723 length = skb->len;
1724 }
1725 req->buf = skb->data;
1726 req->context = skb;
1727 req->complete = tx_complete;
1728
1729 /* use zlp framing on tx for strict CDC-Ether conformance,
1730 * though any robust network rx path ignores extra padding.
1731 * and some hardware doesn't like to write zlps.
1732 */
1733 req->zero = 1;
1734 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1735 length++;
1736
1737 req->length = length;
1738
1739 /* throttle highspeed IRQ rate back slightly */
1740 if (gadget_is_dualspeed(dev->gadget))
1741 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1742 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1743 : 0;
1744
1745 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1746 switch (retval) {
1747 default:
1748 DEBUG (dev, "tx queue err %d\n", retval);
1749 break;
1750 case 0:
1751 net->trans_start = jiffies;
1752 atomic_inc (&dev->tx_qlen);
1753 }
1754
1755 if (retval) {
1756drop:
1757 dev->stats.tx_dropped++;
1758 dev_kfree_skb_any (skb);
1759 spin_lock_irqsave(&dev->req_lock, flags);
1760 if (list_empty (&dev->tx_reqs))
1761 netif_start_queue (net);
1762 list_add (&req->list, &dev->tx_reqs);
1763 spin_unlock_irqrestore(&dev->req_lock, flags);
1764 }
1765 return 0;
1766}
1767
1768/*-------------------------------------------------------------------------*/
1769#endif
1770
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001771static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001772{
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001773 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001774
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001775 debug("%s...\n", __func__);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001776 rndis_deregister(dev->rndis_config);
1777 rndis_exit();
Remy Bohmer23cd1382009-07-29 18:18:43 +02001778
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001779 /* we've already been disconnected ... no i/o is active */
1780 if (dev->req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001781 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001782 dev->req = NULL;
1783 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001784 if (dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001785 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001786 dev->stat_req = NULL;
1787 }
1788
1789 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001790 usb_ep_free_request(dev->in_ep, dev->tx_req);
1791 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001792 }
1793
1794 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001795 usb_ep_free_request(dev->out_ep, dev->rx_req);
1796 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001797 }
1798
1799/* unregister_netdev (dev->net);*/
1800/* free_netdev(dev->net);*/
1801
Lei Wen988ee3e2010-12-01 23:43:43 +08001802 dev->gadget = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001803 set_gadget_data(gadget, NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001804}
1805
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001806static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001807{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001808 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001809 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001810}
1811
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001812static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001813{
1814 /* Not used */
1815}
1816
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001817static void eth_resume(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001818{
1819 /* Not used */
1820}
1821
1822/*-------------------------------------------------------------------------*/
1823
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001824#ifdef CONFIG_USB_ETH_RNDIS
1825
1826/*
1827 * The interrupt endpoint is used in RNDIS to notify the host when messages
1828 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1829 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1830 * REMOTE_NDIS_KEEPALIVE_MSG.
1831 *
1832 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1833 * normally just one notification will be queued.
1834 */
1835
1836static void rndis_control_ack_complete(struct usb_ep *ep,
1837 struct usb_request *req)
1838{
1839 struct eth_dev *dev = ep->driver_data;
1840
1841 debug("%s...\n", __func__);
1842 if (req->status || req->actual != req->length)
1843 debug("rndis control ack complete --> %d, %d/%d\n",
1844 req->status, req->actual, req->length);
1845
Mugunthan V N17b4f302016-11-18 10:49:13 +05301846 if (!dev->network_started) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001847 if (rndis_get_state(dev->rndis_config)
1848 == RNDIS_DATA_INITIALIZED) {
Mugunthan V N17b4f302016-11-18 10:49:13 +05301849 dev->network_started = 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001850 printf("USB RNDIS network up!\n");
1851 }
1852 }
1853
1854 req->context = NULL;
1855
1856 if (req != dev->stat_req)
1857 usb_ep_free_request(ep, req);
1858}
1859
1860static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1861
Mugunthan V Nd4a37552016-11-18 11:09:15 +05301862#ifndef CONFIG_DM_ETH
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001863static int rndis_control_ack(struct eth_device *net)
Mugunthan V Nd4a37552016-11-18 11:09:15 +05301864#else
1865static int rndis_control_ack(struct udevice *net)
1866#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001867{
Mugunthan V Nae701002016-11-18 11:07:18 +05301868 struct ether_priv *priv = (struct ether_priv *)net->priv;
1869 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001870 int length;
1871 struct usb_request *resp = dev->stat_req;
1872
1873 /* in case RNDIS calls this after disconnect */
1874 if (!dev->status) {
1875 debug("status ENODEV\n");
1876 return -ENODEV;
1877 }
1878
1879 /* in case queue length > 1 */
1880 if (resp->context) {
1881 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1882 if (!resp)
1883 return -ENOMEM;
1884 resp->buf = rndis_resp_buf;
1885 }
1886
1887 /*
1888 * Send RNDIS RESPONSE_AVAILABLE notification;
1889 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1890 */
1891 resp->length = 8;
1892 resp->complete = rndis_control_ack_complete;
1893 resp->context = dev;
1894
1895 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1896 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1897
1898 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1899 if (length < 0) {
1900 resp->status = 0;
1901 rndis_control_ack_complete(dev->status_ep, resp);
1902 }
1903
1904 return 0;
1905}
1906
1907#else
1908
1909#define rndis_control_ack NULL
1910
1911#endif /* RNDIS */
1912
1913static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1914{
1915 if (rndis_active(dev)) {
1916 rndis_set_param_medium(dev->rndis_config,
1917 NDIS_MEDIUM_802_3,
1918 BITRATE(dev->gadget)/100);
1919 rndis_signal_connect(dev->rndis_config);
1920 }
1921}
1922
1923static int eth_stop(struct eth_dev *dev)
1924{
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001925#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1926 unsigned long ts;
1927 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1928#endif
1929
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001930 if (rndis_active(dev)) {
1931 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1932 rndis_signal_disconnect(dev->rndis_config);
1933
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001934#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1935 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1936 ts = get_timer(0);
1937 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05301938 usb_gadget_handle_interrupts(0);
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001939#endif
1940
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001941 rndis_uninit(dev->rndis_config);
1942 dev->rndis = 0;
1943 }
1944
1945 return 0;
1946}
1947
1948/*-------------------------------------------------------------------------*/
1949
Remy Bohmer23cd1382009-07-29 18:18:43 +02001950static int is_eth_addr_valid(char *str)
1951{
1952 if (strlen(str) == 17) {
1953 int i;
1954 char *p, *q;
1955 uchar ea[6];
1956
1957 /* see if it looks like an ethernet address */
1958
1959 p = str;
1960
1961 for (i = 0; i < 6; i++) {
1962 char term = (i == 5 ? '\0' : ':');
1963
1964 ea[i] = simple_strtol(p, &q, 16);
1965
1966 if ((q - p) != 2 || *q++ != term)
1967 break;
1968
1969 p = q;
1970 }
1971
Tom Rini57a87a22012-10-31 13:30:41 +00001972 /* Now check the contents. */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001973 return is_valid_ethaddr(ea);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001974 }
1975 return 0;
1976}
1977
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001978static u8 nibble(unsigned char c)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001979{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001980 if (likely(isdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001981 return c - '0';
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001982 c = toupper(c);
1983 if (likely(isxdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001984 return 10 + c - 'A';
1985 return 0;
1986}
1987
1988static int get_ether_addr(const char *str, u8 *dev_addr)
1989{
1990 if (str) {
1991 unsigned i;
1992
1993 for (i = 0; i < 6; i++) {
1994 unsigned char num;
1995
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001996 if ((*str == '.') || (*str == ':'))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001997 str++;
1998 num = nibble(*str++) << 4;
1999 num |= (nibble(*str++));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002000 dev_addr[i] = num;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002001 }
Joe Hershberger0adb5b72015-04-08 01:41:04 -05002002 if (is_valid_ethaddr(dev_addr))
Remy Bohmer23cd1382009-07-29 18:18:43 +02002003 return 0;
2004 }
2005 return 1;
2006}
2007
2008static int eth_bind(struct usb_gadget *gadget)
2009{
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302010 struct eth_dev *dev = &l_priv->ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002011 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002012 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002013 int status = -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002014 int gcnum;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002015 u8 tmp[7];
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302016#ifdef CONFIG_DM_ETH
2017 struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
2018#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002019
2020 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002021#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002022 cdc = 0;
2023#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002024#ifndef CONFIG_USB_ETH_RNDIS
2025 rndis = 0;
2026#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002027 /*
2028 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmer23cd1382009-07-29 18:18:43 +02002029 * standard protocol is _strongly_ preferred for interop purposes.
2030 * (By everyone except Microsoft.)
2031 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002032 if (gadget_is_pxa(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002033 /* pxa doesn't support altsettings */
2034 cdc = 0;
2035 } else if (gadget_is_musbhdrc(gadget)) {
2036 /* reduce tx dma overhead by avoiding special cases */
2037 zlp = 0;
2038 } else if (gadget_is_sh(gadget)) {
2039 /* sh doesn't support multiple interfaces or configs */
2040 cdc = 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002041 rndis = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002042 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002043 /* hardware can't write zlps */
2044 zlp = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002045 /*
2046 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmer23cd1382009-07-29 18:18:43 +02002047 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2048 */
2049 cdc = 0;
2050 }
2051
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002052 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002053 if (gcnum >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002054 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002055 else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002056 /*
2057 * can't assume CDC works. don't want to default to
Remy Bohmer23cd1382009-07-29 18:18:43 +02002058 * anything less functional on CDC-capable hardware,
2059 * so we fail in this case.
2060 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002061 error("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002062 gadget->name);
2063 return -ENODEV;
2064 }
2065
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002066 /*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002067 * If there's an RNDIS configuration, that's what Windows wants to
2068 * be using ... so use these product IDs here and in the "linux.inf"
2069 * needed to install MSFT drivers. Current Linux kernels will use
2070 * the second configuration if it's CDC Ethernet, and need some help
2071 * to choose the right configuration otherwise.
2072 */
2073 if (rndis) {
2074#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2075 device_desc.idVendor =
2076 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2077 device_desc.idProduct =
2078 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2079#else
2080 device_desc.idVendor =
2081 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2082 device_desc.idProduct =
2083 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2084#endif
2085 sprintf(product_desc, "RNDIS/%s", driver_desc);
2086
2087 /*
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002088 * CDC subset ... recognized by Linux since 2.4.10, but Windows
Remy Bohmer23cd1382009-07-29 18:18:43 +02002089 * drivers aren't widely available. (That may be improved by
2090 * supporting one submode of the "SAFE" variant of MDLM.)
2091 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002092 } else {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002093#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002094 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2095 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2096#else
2097 if (!cdc) {
2098 device_desc.idVendor =
2099 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2100 device_desc.idProduct =
2101 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2102 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002103#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002104 }
2105 /* support optional vendor/distro customization */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002106 if (bcdDevice)
2107 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2108 if (iManufacturer)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002109 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002110 if (iProduct)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002111 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002112 if (iSerialNumber) {
2113 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev2e12abe2010-08-13 17:00:45 +04002114 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002115 }
2116
2117 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002118 usb_ep_autoconfig_reset(gadget);
2119 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002120 if (!in_ep) {
2121autoconf_fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002122 error("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002123 gadget->name);
2124 return -ENODEV;
2125 }
2126 in_ep->driver_data = in_ep; /* claim */
2127
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002128 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002129 if (!out_ep)
2130 goto autoconf_fail;
2131 out_ep->driver_data = out_ep; /* claim */
2132
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002133#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002134 /*
2135 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmer23cd1382009-07-29 18:18:43 +02002136 * Since some hosts expect one, try to allocate one anyway.
2137 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002138 if (cdc || rndis) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002139 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002140 if (status_ep) {
2141 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002142 } else if (rndis) {
2143 error("can't run RNDIS on %s", gadget->name);
2144 return -ENODEV;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002145#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002146 } else if (cdc) {
2147 control_intf.bNumEndpoints = 0;
2148 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002149#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002150 }
2151 }
2152#endif
2153
2154 /* one config: cdc, else minimal subset */
2155 if (!cdc) {
2156 eth_config.bNumInterfaces = 1;
2157 eth_config.iConfiguration = STRING_SUBSET;
2158
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002159 /*
2160 * use functions to set these up, in case we're built to work
Remy Bohmer23cd1382009-07-29 18:18:43 +02002161 * with multiple controllers and must override CDC Ethernet.
2162 */
2163 fs_subset_descriptors();
2164 hs_subset_descriptors();
2165 }
2166
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002167 usb_gadget_set_selfpowered(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002168
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002169 /* For now RNDIS is always a second config */
2170 if (rndis)
2171 device_desc.bNumConfigurations = 2;
2172
Remy Bohmer23cd1382009-07-29 18:18:43 +02002173 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002174 if (rndis)
2175 dev_qualifier.bNumConfigurations = 2;
2176 else if (!cdc)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002177 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2178
2179 /* assumes ep0 uses the same value for both speeds ... */
2180 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2181
2182 /* and that all endpoints are dual-speed */
2183 hs_source_desc.bEndpointAddress =
2184 fs_source_desc.bEndpointAddress;
2185 hs_sink_desc.bEndpointAddress =
2186 fs_sink_desc.bEndpointAddress;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002187#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002188 if (status_ep)
2189 hs_status_desc.bEndpointAddress =
2190 fs_status_desc.bEndpointAddress;
2191#endif
2192 }
2193
2194 if (gadget_is_otg(gadget)) {
2195 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2196 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2197 eth_config.bMaxPower = 4;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002198#ifdef CONFIG_USB_ETH_RNDIS
2199 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2200 rndis_config.bMaxPower = 4;
2201#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002202 }
2203
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002204
2205 /* network device setup */
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302206#ifndef CONFIG_DM_ETH
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302207 dev->net = &l_priv->netdev;
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302208#else
2209 dev->net = l_priv->netdev;
2210#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002211
2212 dev->cdc = cdc;
2213 dev->zlp = zlp;
2214
2215 dev->in_ep = in_ep;
2216 dev->out_ep = out_ep;
2217 dev->status_ep = status_ep;
2218
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302219 memset(tmp, 0, sizeof(tmp));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002220 /*
2221 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002222 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmer23cd1382009-07-29 18:18:43 +02002223 * ends up in a persistent config database. It's not clear if
2224 * host side code for the SAFE thing cares -- its original BLAN
2225 * thing didn't, Sharp never assigned those addresses on Zaurii.
2226 */
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302227#ifndef CONFIG_DM_ETH
Remy Bohmer23cd1382009-07-29 18:18:43 +02002228 get_ether_addr(dev_addr, dev->net->enetaddr);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002229 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302230#else
2231 get_ether_addr(dev_addr, pdata->enetaddr);
2232 memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
2233#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002234
2235 get_ether_addr(host_addr, dev->host_mac);
2236
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002237 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2238 dev->host_mac[0], dev->host_mac[1],
2239 dev->host_mac[2], dev->host_mac[3],
2240 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002241
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002242 if (rndis) {
2243 status = rndis_init();
2244 if (status < 0) {
2245 error("can't init RNDIS, %d", status);
2246 goto fail;
2247 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002248 }
2249
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002250 /*
2251 * use PKTSIZE (or aligned... from u-boot) and set
2252 * wMaxSegmentSize accordingly
2253 */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002254 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2255
2256 /* preallocate control message data and buffer */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002257 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002258 if (!dev->req)
2259 goto fail;
2260 dev->req->buf = control_req;
2261 dev->req->complete = eth_setup_complete;
2262
2263 /* ... and maybe likewise for status transfer */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002264#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002265 if (dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002266 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2267 GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002268 if (!dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002269 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002270
2271 goto fail;
2272 }
Vitaly Kuzmichevdf559c12010-08-13 17:01:06 +04002273 dev->stat_req->buf = status_req;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002274 dev->stat_req->context = NULL;
2275 }
2276#endif
2277
2278 /* finish hookup to lower layer ... */
2279 dev->gadget = gadget;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002280 set_gadget_data(gadget, dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002281 gadget->ep0->driver_data = dev;
2282
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002283 /*
2284 * two kinds of host-initiated state changes:
Remy Bohmer23cd1382009-07-29 18:18:43 +02002285 * - iff DATA transfer is active, carrier is "on"
2286 * - tx queueing enabled if open *and* carrier is "on"
2287 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002288
2289 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2290 out_ep->name, in_ep->name,
2291 status_ep ? " STATUS " : "",
2292 status_ep ? status_ep->name : ""
2293 );
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302294#ifndef CONFIG_DM_ETH
2295 printf("MAC %pM\n", dev->net->enetaddr);
2296#else
2297 printf("MAC %pM\n", pdata->enetaddr);
2298#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002299
2300 if (cdc || rndis)
2301 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2302 dev->host_mac[0], dev->host_mac[1],
2303 dev->host_mac[2], dev->host_mac[3],
2304 dev->host_mac[4], dev->host_mac[5]);
2305
2306 if (rndis) {
2307 u32 vendorID = 0;
2308
2309 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2310
2311 dev->rndis_config = rndis_register(rndis_control_ack);
2312 if (dev->rndis_config < 0) {
2313fail0:
2314 eth_unbind(gadget);
2315 debug("RNDIS setup failed\n");
2316 status = -ENODEV;
2317 goto fail;
2318 }
2319
2320 /* these set up a lot of the OIDs that RNDIS needs */
2321 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2322 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2323 &dev->stats, &dev->cdc_filter))
2324 goto fail0;
2325 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2326 manufacturer))
2327 goto fail0;
2328 if (rndis_set_param_medium(dev->rndis_config,
2329 NDIS_MEDIUM_802_3, 0))
2330 goto fail0;
2331 printf("RNDIS ready\n");
2332 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002333 return 0;
2334
2335fail:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002336 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002337 eth_unbind(gadget);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002338 return status;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002339}
2340
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002341/*-------------------------------------------------------------------------*/
2342
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302343#ifdef CONFIG_DM_USB
2344int dm_usb_init(struct eth_dev *e_dev)
2345{
2346 struct udevice *dev = NULL;
2347 int ret;
2348
2349 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
2350 if (!dev || ret) {
2351 error("No USB device found\n");
2352 return -ENODEV;
2353 }
2354
2355 e_dev->usb_udev = dev;
2356
2357 return ret;
2358}
2359#endif
2360
Mugunthan V N8269ee42016-11-18 11:08:27 +05302361static int _usb_eth_init(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002362{
Mugunthan V Nae701002016-11-18 11:07:18 +05302363 struct eth_dev *dev = &priv->ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002364 struct usb_gadget *gadget;
2365 unsigned long ts;
2366 unsigned long timeout = USB_CONNECT_TIMEOUT;
2367
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302368#ifdef CONFIG_DM_USB
2369 if (dm_usb_init(dev)) {
2370 error("USB ether not found\n");
2371 return -ENODEV;
2372 }
2373#else
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302374 board_usb_init(0, USB_INIT_DEVICE);
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302375#endif
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302376
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002377 /* Configure default mac-addresses for the USB ethernet device */
2378#ifdef CONFIG_USBNET_DEV_ADDR
2379 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2380#endif
2381#ifdef CONFIG_USBNET_HOST_ADDR
2382 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2383#endif
2384 /* Check if the user overruled the MAC addresses */
2385 if (getenv("usbnet_devaddr"))
2386 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2387 sizeof(dev_addr));
2388
2389 if (getenv("usbnet_hostaddr"))
2390 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2391 sizeof(host_addr));
2392
2393 if (!is_eth_addr_valid(dev_addr)) {
2394 error("Need valid 'usbnet_devaddr' to be set");
2395 goto fail;
2396 }
2397 if (!is_eth_addr_valid(host_addr)) {
2398 error("Need valid 'usbnet_hostaddr' to be set");
2399 goto fail;
2400 }
2401
Mugunthan V Nae701002016-11-18 11:07:18 +05302402 priv->eth_driver.speed = DEVSPEED;
2403 priv->eth_driver.bind = eth_bind;
2404 priv->eth_driver.unbind = eth_unbind;
2405 priv->eth_driver.setup = eth_setup;
2406 priv->eth_driver.reset = eth_disconnect;
2407 priv->eth_driver.disconnect = eth_disconnect;
2408 priv->eth_driver.suspend = eth_suspend;
2409 priv->eth_driver.resume = eth_resume;
2410 if (usb_gadget_register_driver(&priv->eth_driver) < 0)
Lei Wen988ee3e2010-12-01 23:43:43 +08002411 goto fail;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002412
2413 dev->network_started = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002414
2415 packet_received = 0;
2416 packet_sent = 0;
2417
2418 gadget = dev->gadget;
2419 usb_gadget_connect(gadget);
2420
2421 if (getenv("cdc_connect_timeout"))
2422 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2423 NULL, 10) * CONFIG_SYS_HZ;
2424 ts = get_timer(0);
Mugunthan V N17b4f302016-11-18 10:49:13 +05302425 while (!dev->network_started) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002426 /* Handle control-c and timeouts */
2427 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002428 error("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002429 goto fail;
2430 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302431 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002432 }
2433
Vitaly Kuzmichev98fae972010-09-22 13:13:56 +04002434 packet_received = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002435 rx_submit(dev, dev->rx_req, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002436 return 0;
2437fail:
2438 return -1;
2439}
2440
Mugunthan V N8269ee42016-11-18 11:08:27 +05302441static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002442{
2443 int retval;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002444 void *rndis_pkt = NULL;
Mugunthan V Nae701002016-11-18 11:07:18 +05302445 struct eth_dev *dev = &priv->ethdev;
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002446 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002447 unsigned long ts;
2448 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002449
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002450 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002451
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002452 /* new buffer is needed to include RNDIS header */
2453 if (rndis_active(dev)) {
2454 rndis_pkt = malloc(length +
2455 sizeof(struct rndis_packet_msg_type));
2456 if (!rndis_pkt) {
2457 error("No memory to alloc RNDIS packet");
2458 goto drop;
2459 }
2460 rndis_add_hdr(rndis_pkt, length);
2461 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002462 packet, length);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002463 packet = rndis_pkt;
2464 length += sizeof(struct rndis_packet_msg_type);
2465 }
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002466 req->buf = packet;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002467 req->context = NULL;
2468 req->complete = tx_complete;
2469
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002470 /*
2471 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002472 * though any robust network rx path ignores extra padding.
2473 * and some hardware doesn't like to write zlps.
2474 */
2475 req->zero = 1;
2476 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2477 length++;
2478
2479 req->length = length;
2480#if 0
2481 /* throttle highspeed IRQ rate back slightly */
2482 if (gadget_is_dualspeed(dev->gadget))
2483 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2484 ? ((dev->tx_qlen % qmult) != 0) : 0;
2485#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002486 dev->tx_qlen = 1;
2487 ts = get_timer(0);
2488 packet_sent = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002489
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002490 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002491
2492 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002493 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002494 while (!packet_sent) {
2495 if (get_timer(ts) > timeout) {
2496 printf("timeout sending packets to usb ethernet\n");
2497 return -1;
2498 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302499 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002500 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002501 if (rndis_pkt)
2502 free(rndis_pkt);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002503
2504 return 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002505drop:
2506 dev->stats.tx_dropped++;
2507 return -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002508}
2509
Mugunthan V N8269ee42016-11-18 11:08:27 +05302510static int _usb_eth_recv(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002511{
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302512 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002513
Remy Bohmer23cd1382009-07-29 18:18:43 +02002514 return 0;
2515}
2516
Mugunthan V N8269ee42016-11-18 11:08:27 +05302517void _usb_eth_halt(struct ether_priv *priv)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002518{
Mugunthan V Nae701002016-11-18 11:07:18 +05302519 struct eth_dev *dev = &priv->ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002520
Lei Wen988ee3e2010-12-01 23:43:43 +08002521 /* If the gadget not registered, simple return */
2522 if (!dev->gadget)
2523 return;
2524
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03002525 /*
2526 * Some USB controllers may need additional deinitialization here
2527 * before dropping pull-up (also due to hardware issues).
2528 * For example: unhandled interrupt with status stage started may
2529 * bring the controller to fully broken state (until board reset).
2530 * There are some variants to debug and fix such cases:
2531 * 1) In the case of RNDIS connection eth_stop can perform additional
2532 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2533 * 2) 'pullup' callback in your UDC driver can be improved to perform
2534 * this deinitialization.
2535 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002536 eth_stop(dev);
2537
Remy Bohmer23cd1382009-07-29 18:18:43 +02002538 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002539
2540 /* Clear pending interrupt */
2541 if (dev->network_started) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302542 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002543 dev->network_started = 0;
2544 }
2545
Mugunthan V Nae701002016-11-18 11:07:18 +05302546 usb_gadget_unregister_driver(&priv->eth_driver);
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302547#ifndef CONFIG_DM_USB
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302548 board_usb_cleanup(0, USB_INIT_DEVICE);
Mugunthan V Nd4345ae2016-11-18 10:49:12 +05302549#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002550}
2551
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302552#ifndef CONFIG_DM_ETH
Mugunthan V N8269ee42016-11-18 11:08:27 +05302553static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
2554{
2555 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2556
2557 return _usb_eth_init(priv);
2558}
2559
2560static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
2561{
2562 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2563
2564 return _usb_eth_send(priv, packet, length);
2565}
2566
2567static int usb_eth_recv(struct eth_device *netdev)
2568{
2569 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2570 struct eth_dev *dev = &priv->ethdev;
2571 int ret;
2572
2573 ret = _usb_eth_recv(priv);
2574 if (ret) {
2575 error("error packet receive\n");
2576 return ret;
2577 }
2578
2579 if (!packet_received)
2580 return 0;
2581
2582 if (dev->rx_req) {
2583 net_process_received_packet(net_rx_packets[0],
2584 dev->rx_req->length);
2585 } else {
2586 error("dev->rx_req invalid");
2587 }
2588 packet_received = 0;
2589 rx_submit(dev, dev->rx_req, 0);
2590
2591 return 0;
2592}
2593
2594void usb_eth_halt(struct eth_device *netdev)
2595{
2596 struct ether_priv *priv = (struct ether_priv *)netdev->priv;
2597
2598 _usb_eth_halt(priv);
2599}
2600
Remy Bohmer23cd1382009-07-29 18:18:43 +02002601int usb_eth_initialize(bd_t *bi)
2602{
Mugunthan V N5cb3b9d2016-11-18 11:06:13 +05302603 struct eth_device *netdev = &l_priv->netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002604
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +03002605 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmer23cd1382009-07-29 18:18:43 +02002606
2607 netdev->init = usb_eth_init;
2608 netdev->send = usb_eth_send;
2609 netdev->recv = usb_eth_recv;
2610 netdev->halt = usb_eth_halt;
Mugunthan V Nae701002016-11-18 11:07:18 +05302611 netdev->priv = l_priv;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002612
2613#ifdef CONFIG_MCAST_TFTP
2614 #error not supported
2615#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002616 eth_register(netdev);
2617 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002618}
Mugunthan V Nd4a37552016-11-18 11:09:15 +05302619#else
2620static int usb_eth_start(struct udevice *dev)
2621{
2622 struct ether_priv *priv = dev_get_priv(dev);
2623
2624 return _usb_eth_init(priv);
2625}
2626
2627static int usb_eth_send(struct udevice *dev, void *packet, int length)
2628{
2629 struct ether_priv *priv = dev_get_priv(dev);
2630
2631 return _usb_eth_send(priv, packet, length);
2632}
2633
2634static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
2635{
2636 struct ether_priv *priv = dev_get_priv(dev);
2637 struct eth_dev *ethdev = &priv->ethdev;
2638 int ret;
2639
2640 ret = _usb_eth_recv(priv);
2641 if (ret) {
2642 error("error packet receive\n");
2643 return ret;
2644 }
2645
2646 if (packet_received) {
2647 if (ethdev->rx_req) {
2648 *packetp = (uchar *)net_rx_packets[0];
2649 return ethdev->rx_req->length;
2650 } else {
2651 error("dev->rx_req invalid");
2652 return -EFAULT;
2653 }
2654 }
2655
2656 return -EAGAIN;
2657}
2658
2659static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
2660 int length)
2661{
2662 struct ether_priv *priv = dev_get_priv(dev);
2663 struct eth_dev *ethdev = &priv->ethdev;
2664
2665 packet_received = 0;
2666
2667 return rx_submit(ethdev, ethdev->rx_req, 0);
2668}
2669
2670static void usb_eth_stop(struct udevice *dev)
2671{
2672 struct ether_priv *priv = dev_get_priv(dev);
2673
2674 _usb_eth_halt(priv);
2675}
2676
2677static int usb_eth_probe(struct udevice *dev)
2678{
2679 struct ether_priv *priv = dev_get_priv(dev);
2680 struct eth_pdata *pdata = dev_get_platdata(dev);
2681
2682 priv->netdev = dev;
2683 l_priv = priv;
2684
2685 get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
2686 eth_setenv_enetaddr("usbnet_devaddr", pdata->enetaddr);
2687
2688 return 0;
2689}
2690
2691static const struct eth_ops usb_eth_ops = {
2692 .start = usb_eth_start,
2693 .send = usb_eth_send,
2694 .recv = usb_eth_recv,
2695 .free_pkt = usb_eth_free_pkt,
2696 .stop = usb_eth_stop,
2697};
2698
2699int usb_ether_init(void)
2700{
2701 struct udevice *dev;
2702 struct udevice *usb_dev;
2703 int ret;
2704
2705 ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
2706 if (!usb_dev || ret) {
2707 error("No USB device found\n");
2708 return ret;
2709 }
2710
2711 ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
2712 if (!dev || ret) {
2713 error("usb - not able to bind usb_ether device\n");
2714 return ret;
2715 }
2716
2717 return 0;
2718}
2719
2720U_BOOT_DRIVER(eth_usb) = {
2721 .name = "usb_ether",
2722 .id = UCLASS_ETH,
2723 .probe = usb_eth_probe,
2724 .ops = &usb_eth_ops,
2725 .priv_auto_alloc_size = sizeof(struct ether_priv),
2726 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
2727 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2728};
2729#endif /* CONFIG_DM_ETH */