blob: c5e35ee35077a37cca9a623c77c835dc75542bb2 [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>
12#include <asm/errno.h>
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +030013#include <linux/netdevice.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020014#include <linux/usb/ch9.h>
15#include <linux/usb/cdc.h>
16#include <linux/usb/gadget.h>
17#include <net.h>
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +053018#include <usb.h>
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030019#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020021#include <linux/ctype.h>
22
23#include "gadget_chips.h"
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030024#include "rndis.h"
Remy Bohmer23cd1382009-07-29 18:18:43 +020025
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +030026#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040027
Remy Bohmer23cd1382009-07-29 18:18:43 +020028#define atomic_read
29extern struct platform_data brd;
Remy Bohmer23cd1382009-07-29 18:18:43 +020030
31
32unsigned packet_received, packet_sent;
33
Remy Bohmer23cd1382009-07-29 18:18:43 +020034/*
35 * Ethernet gadget driver -- with CDC and non-CDC options
36 * Builds on hardware support for a full duplex link.
37 *
38 * CDC Ethernet is the standard USB solution for sending Ethernet frames
39 * using USB. Real hardware tends to use the same framing protocol but look
40 * different for control features. This driver strongly prefers to use
41 * this USB-IF standard as its open-systems interoperability solution;
42 * most host side USB stacks (except from Microsoft) support it.
43 *
44 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
45 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
46 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
47 *
48 * There's some hardware that can't talk CDC ECM. We make that hardware
49 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
50 * link-level setup only requires activating the configuration. Only the
51 * endpoint descriptors, and product/vendor IDs, are relevant; no control
52 * operations are available. Linux supports it, but other host operating
53 * systems may not. (This is a subset of CDC Ethernet.)
54 *
55 * It turns out that if you add a few descriptors to that "CDC Subset",
56 * (Windows) host side drivers from MCCI can treat it as one submode of
57 * a proprietary scheme called "SAFE" ... without needing to know about
58 * specific product/vendor IDs. So we do that, making it easier to use
59 * those MS-Windows drivers. Those added descriptors make it resemble a
60 * CDC MDLM device, but they don't change device behavior at all. (See
61 * MCCI Engineering report 950198 "SAFE Networking Functions".)
62 *
63 * A third option is also in use. Rather than CDC Ethernet, or something
64 * simpler, Microsoft pushes their own approach: RNDIS. The published
65 * RNDIS specs are ambiguous and appear to be incomplete, and are also
66 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
67 */
68#define ETH_ALEN 6 /* Octets in one ethernet addr */
69#define ETH_HLEN 14 /* Total octets in header. */
70#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
71#define ETH_DATA_LEN 1500 /* Max. octets in payload */
72#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
Remy Bohmer23cd1382009-07-29 18:18:43 +020073
74#define DRIVER_DESC "Ethernet Gadget"
75/* Based on linux 2.6.27 version */
76#define DRIVER_VERSION "May Day 2005"
77
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040078static const char shortname[] = "ether";
79static const char driver_desc[] = DRIVER_DESC;
Remy Bohmer23cd1382009-07-29 18:18:43 +020080
81#define RX_EXTRA 20 /* guard against rx overflows */
82
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030083#ifndef CONFIG_USB_ETH_RNDIS
84#define rndis_uninit(x) do {} while (0)
85#define rndis_deregister(c) do {} while (0)
86#define rndis_exit() do {} while (0)
87#endif
88
89/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmer23cd1382009-07-29 18:18:43 +020090#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
91 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
92 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
93 |USB_CDC_PACKET_TYPE_DIRECTED)
94
95#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
96
97/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +030098
99struct eth_dev {
100 struct usb_gadget *gadget;
101 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300102 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300103
104 u8 config;
105 struct usb_ep *in_ep, *out_ep, *status_ep;
106 const struct usb_endpoint_descriptor
107 *in, *out, *status;
108
109 struct usb_request *tx_req, *rx_req;
110
111 struct eth_device *net;
112 struct net_device_stats stats;
113 unsigned int tx_qlen;
114
115 unsigned zlp:1;
116 unsigned cdc:1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300117 unsigned rndis:1;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300118 unsigned suspended:1;
119 unsigned network_started:1;
120 u16 cdc_filter;
121 unsigned long todo;
122 int mtu;
123#define WORK_RX_MEMORY 0
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300124 int rndis_config;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300125 u8 host_mac[ETH_ALEN];
126};
127
128/*
129 * This version autoconfigures as much as possible at run-time.
130 *
131 * It also ASSUMES a self-powered device, without remote wakeup,
132 * although remote wakeup support would make sense.
133 */
134
135/*-------------------------------------------------------------------------*/
Remy Bohmer23cd1382009-07-29 18:18:43 +0200136static struct eth_dev l_ethdev;
137static struct eth_device l_netdev;
138static struct usb_gadget_driver eth_driver;
139
140/*-------------------------------------------------------------------------*/
141
142/* "main" config is either CDC, or its simple subset */
143static inline int is_cdc(struct eth_dev *dev)
144{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200145#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200146 return 1; /* only cdc possible */
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200147#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200148 return 0; /* only subset possible */
149#else
150 return dev->cdc; /* depends on what hardware we found */
151#endif
152}
153
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300154/* "secondary" RNDIS config may sometimes be activated */
155static inline int rndis_active(struct eth_dev *dev)
156{
157#ifdef CONFIG_USB_ETH_RNDIS
158 return dev->rndis;
159#else
160 return 0;
161#endif
162}
163
164#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
165#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200166
167#define DEFAULT_QLEN 2 /* double buffering by default */
168
169/* peak bulk transfer bits-per-second */
170#define HS_BPS (13 * 512 * 8 * 1000 * 8)
171#define FS_BPS (19 * 64 * 1 * 1000 * 8)
172
173#ifdef CONFIG_USB_GADGET_DUALSPEED
174#define DEVSPEED USB_SPEED_HIGH
175
Vitaly Kuzmichev2721dbf2010-08-12 16:44:40 +0400176#ifdef CONFIG_USB_ETH_QMULT
177#define qmult CONFIG_USB_ETH_QMULT
178#else
179#define qmult 5
180#endif
181
Remy Bohmer23cd1382009-07-29 18:18:43 +0200182/* for dual-speed hardware, use deeper queues at highspeed */
183#define qlen(gadget) \
184 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
185
186static inline int BITRATE(struct usb_gadget *g)
187{
188 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
189}
190
191#else /* full speed (low speed doesn't do bulk) */
192
193#define qmult 1
194
195#define DEVSPEED USB_SPEED_FULL
196
197#define qlen(gadget) DEFAULT_QLEN
198
199static inline int BITRATE(struct usb_gadget *g)
200{
201 return FS_BPS;
202}
203#endif
204
Remy Bohmer23cd1382009-07-29 18:18:43 +0200205/*-------------------------------------------------------------------------*/
206
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400207/*
208 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmer23cd1382009-07-29 18:18:43 +0200209 * Instead: allocate your own, using normal USB-IF procedures.
210 */
211
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400212/*
213 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200214 * It's for devices with only CDC Ethernet configurations.
215 */
216#define CDC_VENDOR_NUM 0x0525 /* NetChip */
217#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
218
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400219/*
220 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmer23cd1382009-07-29 18:18:43 +0200221 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
222 * with pxa250. We're protocol-compatible, if the host-side drivers
223 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
224 * drivers that need to hard-wire endpoint numbers have a hook.
225 *
226 * The protocol is a minimal subset of CDC Ether, which works on any bulk
227 * hardware that's not deeply broken ... even on hardware that can't talk
228 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
229 * doesn't handle control-OUT).
230 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300231#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
232#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
233
234/*
235 * For hardware that can talk RNDIS and either of the above protocols,
236 * use this ID ... the windows INF files will know it. Unless it's
237 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
238 * the non-RNDIS configuration.
239 */
240#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
241#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200242
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400243/*
244 * Some systems will want different product identifers published in the
Remy Bohmer23cd1382009-07-29 18:18:43 +0200245 * device descriptor, either numbers or strings or both. These string
246 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
247 */
248
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300249/*
250 * Emulating them in eth_bind:
251 * static ushort idVendor;
252 * static ushort idProduct;
253 */
254
Remy Bohmer23cd1382009-07-29 18:18:43 +0200255#if defined(CONFIG_USBNET_MANUFACTURER)
256static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
257#else
258static char *iManufacturer = "U-boot";
259#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300260
261/* These probably need to be configurable. */
262static ushort bcdDevice;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200263static char *iProduct;
264static char *iSerialNumber;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300265
Remy Bohmer23cd1382009-07-29 18:18:43 +0200266static char dev_addr[18];
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300267
Remy Bohmer23cd1382009-07-29 18:18:43 +0200268static char host_addr[18];
269
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300270
Remy Bohmer23cd1382009-07-29 18:18:43 +0200271/*-------------------------------------------------------------------------*/
272
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400273/*
274 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmer23cd1382009-07-29 18:18:43 +0200275 * ep0 implementation: descriptors, config management, setup().
276 * also optional class-specific notification interrupt transfer.
277 */
278
279/*
280 * DESCRIPTORS ... most are static, but strings and (full) configuration
281 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300282 * our simple subset, with RNDIS as an optional second configuration.
283 *
284 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
285 * the class descriptors match a modem (they're ignored; it's really just
286 * Ethernet functionality), they don't need the NOP altsetting, and the
287 * status transfer endpoint isn't optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200288 */
289
290#define STRING_MANUFACTURER 1
291#define STRING_PRODUCT 2
292#define STRING_ETHADDR 3
293#define STRING_DATA 4
294#define STRING_CONTROL 5
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300295#define STRING_RNDIS_CONTROL 6
Remy Bohmer23cd1382009-07-29 18:18:43 +0200296#define STRING_CDC 7
297#define STRING_SUBSET 8
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300298#define STRING_RNDIS 9
Remy Bohmer23cd1382009-07-29 18:18:43 +0200299#define STRING_SERIALNUMBER 10
300
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300301/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200302#define USB_BUFSIZ 256
303
304/*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300305 * This device advertises one configuration, eth_config, unless RNDIS
306 * is enabled (rndis_config) on hardware supporting at least two configs.
307 *
308 * NOTE: Controllers like superh_udc should probably be able to use
309 * an RNDIS-only configuration.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200310 *
311 * FIXME define some higher-powered configurations to make it easier
312 * to recharge batteries ...
313 */
314
315#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300316#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200317
318static struct usb_device_descriptor
319device_desc = {
320 .bLength = sizeof device_desc,
321 .bDescriptorType = USB_DT_DEVICE,
322
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400323 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200324
325 .bDeviceClass = USB_CLASS_COMM,
326 .bDeviceSubClass = 0,
327 .bDeviceProtocol = 0,
328
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400329 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
330 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200331 .iManufacturer = STRING_MANUFACTURER,
332 .iProduct = STRING_PRODUCT,
333 .bNumConfigurations = 1,
334};
335
336static struct usb_otg_descriptor
337otg_descriptor = {
338 .bLength = sizeof otg_descriptor,
339 .bDescriptorType = USB_DT_OTG,
340
341 .bmAttributes = USB_OTG_SRP,
342};
343
344static struct usb_config_descriptor
345eth_config = {
346 .bLength = sizeof eth_config,
347 .bDescriptorType = USB_DT_CONFIG,
348
349 /* compute wTotalLength on the fly */
350 .bNumInterfaces = 2,
351 .bConfigurationValue = DEV_CONFIG_VALUE,
352 .iConfiguration = STRING_CDC,
353 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
354 .bMaxPower = 1,
355};
356
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300357#ifdef CONFIG_USB_ETH_RNDIS
358static struct usb_config_descriptor
359rndis_config = {
360 .bLength = sizeof rndis_config,
361 .bDescriptorType = USB_DT_CONFIG,
362
363 /* compute wTotalLength on the fly */
364 .bNumInterfaces = 2,
365 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
366 .iConfiguration = STRING_RNDIS,
367 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
368 .bMaxPower = 1,
369};
370#endif
371
Remy Bohmer23cd1382009-07-29 18:18:43 +0200372/*
373 * Compared to the simple CDC subset, the full CDC Ethernet model adds
374 * three class descriptors, two interface descriptors, optional status
375 * endpoint. Both have a "data" interface and two bulk endpoints.
376 * There are also differences in how control requests are handled.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300377 *
378 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
379 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
380 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
381 * work around those bugs) are unlikely to ever come from MSFT, you may
382 * wish to avoid using RNDIS.
383 *
384 * MCCI offers an alternative to RNDIS if you need to connect to Windows
385 * but have hardware that can't support CDC Ethernet. We add descriptors
386 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
387 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
388 * get those drivers from MCCI, or bundled with various products.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200389 */
390
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200391#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200392static struct usb_interface_descriptor
393control_intf = {
394 .bLength = sizeof control_intf,
395 .bDescriptorType = USB_DT_INTERFACE,
396
397 .bInterfaceNumber = 0,
398 /* status endpoint is optional; this may be patched later */
399 .bNumEndpoints = 1,
400 .bInterfaceClass = USB_CLASS_COMM,
401 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
402 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
403 .iInterface = STRING_CONTROL,
404};
405#endif
406
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300407#ifdef CONFIG_USB_ETH_RNDIS
408static const struct usb_interface_descriptor
409rndis_control_intf = {
410 .bLength = sizeof rndis_control_intf,
411 .bDescriptorType = USB_DT_INTERFACE,
412
413 .bInterfaceNumber = 0,
414 .bNumEndpoints = 1,
415 .bInterfaceClass = USB_CLASS_COMM,
416 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
417 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
418 .iInterface = STRING_RNDIS_CONTROL,
419};
420#endif
421
Remy Bohmer23cd1382009-07-29 18:18:43 +0200422static const struct usb_cdc_header_desc header_desc = {
423 .bLength = sizeof header_desc,
424 .bDescriptorType = USB_DT_CS_INTERFACE,
425 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
426
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400427 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200428};
429
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200430#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200431
432static const struct usb_cdc_union_desc union_desc = {
433 .bLength = sizeof union_desc,
434 .bDescriptorType = USB_DT_CS_INTERFACE,
435 .bDescriptorSubType = USB_CDC_UNION_TYPE,
436
437 .bMasterInterface0 = 0, /* index of control interface */
438 .bSlaveInterface0 = 1, /* index of DATA interface */
439};
440
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300441#endif /* CDC || RNDIS */
442
443#ifdef CONFIG_USB_ETH_RNDIS
444
445static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
446 .bLength = sizeof call_mgmt_descriptor,
447 .bDescriptorType = USB_DT_CS_INTERFACE,
448 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
449
450 .bmCapabilities = 0x00,
451 .bDataInterface = 0x01,
452};
453
454static const struct usb_cdc_acm_descriptor acm_descriptor = {
455 .bLength = sizeof acm_descriptor,
456 .bDescriptorType = USB_DT_CS_INTERFACE,
457 .bDescriptorSubType = USB_CDC_ACM_TYPE,
458
459 .bmCapabilities = 0x00,
460};
461
462#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200463
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200464#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200465
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400466/*
467 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmer23cd1382009-07-29 18:18:43 +0200468 * ways: data endpoints live in the control interface, there's no data
469 * interface, and it's not used to talk to a cell phone radio.
470 */
471
472static const struct usb_cdc_mdlm_desc mdlm_desc = {
473 .bLength = sizeof mdlm_desc,
474 .bDescriptorType = USB_DT_CS_INTERFACE,
475 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
476
477 .bcdVersion = __constant_cpu_to_le16(0x0100),
478 .bGUID = {
479 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
480 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
481 },
482};
483
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400484/*
485 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmer23cd1382009-07-29 18:18:43 +0200486 * can't really use its struct. All we do here is say that we're using
487 * the submode of "SAFE" which directly matches the CDC Subset.
488 */
489static const u8 mdlm_detail_desc[] = {
490 6,
491 USB_DT_CS_INTERFACE,
492 USB_CDC_MDLM_DETAIL_TYPE,
493
494 0, /* "SAFE" */
495 0, /* network control capabilities (none) */
496 0, /* network data capabilities ("raw" encapsulation) */
497};
498
499#endif
500
Remy Bohmer23cd1382009-07-29 18:18:43 +0200501static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400502 .bLength = sizeof(ether_desc),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200503 .bDescriptorType = USB_DT_CS_INTERFACE,
504 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
505
506 /* this descriptor actually adds value, surprise! */
507 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400508 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
509 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
510 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200511 .bNumberPowerFilters = 0,
512};
513
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200514#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200515
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400516/*
517 * include the status endpoint if we can, even where it's optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200518 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
519 * packet, to simplify cancellation; and a big transfer interval, to
520 * waste less bandwidth.
521 *
522 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
523 * if they ignore the connect/disconnect notifications that real aether
524 * can provide. more advanced cdc configurations might want to support
525 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300526 *
527 * RNDIS requires the status endpoint, since it uses that encapsulation
528 * mechanism for its funky RPC scheme.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200529 */
530
531#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
532#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
533
534static struct usb_endpoint_descriptor
535fs_status_desc = {
536 .bLength = USB_DT_ENDPOINT_SIZE,
537 .bDescriptorType = USB_DT_ENDPOINT,
538
539 .bEndpointAddress = USB_DIR_IN,
540 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400541 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200542 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
543};
544#endif
545
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200546#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200547
548/* the default data interface has no endpoints ... */
549
550static const struct usb_interface_descriptor
551data_nop_intf = {
552 .bLength = sizeof data_nop_intf,
553 .bDescriptorType = USB_DT_INTERFACE,
554
555 .bInterfaceNumber = 1,
556 .bAlternateSetting = 0,
557 .bNumEndpoints = 0,
558 .bInterfaceClass = USB_CLASS_CDC_DATA,
559 .bInterfaceSubClass = 0,
560 .bInterfaceProtocol = 0,
561};
562
563/* ... but the "real" data interface has two bulk endpoints */
564
565static const struct usb_interface_descriptor
566data_intf = {
567 .bLength = sizeof data_intf,
568 .bDescriptorType = USB_DT_INTERFACE,
569
570 .bInterfaceNumber = 1,
571 .bAlternateSetting = 1,
572 .bNumEndpoints = 2,
573 .bInterfaceClass = USB_CLASS_CDC_DATA,
574 .bInterfaceSubClass = 0,
575 .bInterfaceProtocol = 0,
576 .iInterface = STRING_DATA,
577};
578
579#endif
580
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300581#ifdef CONFIG_USB_ETH_RNDIS
582
583/* RNDIS doesn't activate by changing to the "real" altsetting */
584
585static const struct usb_interface_descriptor
586rndis_data_intf = {
587 .bLength = sizeof rndis_data_intf,
588 .bDescriptorType = USB_DT_INTERFACE,
589
590 .bInterfaceNumber = 1,
591 .bAlternateSetting = 0,
592 .bNumEndpoints = 2,
593 .bInterfaceClass = USB_CLASS_CDC_DATA,
594 .bInterfaceSubClass = 0,
595 .bInterfaceProtocol = 0,
596 .iInterface = STRING_DATA,
597};
598
599#endif
600
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200601#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200602
603/*
604 * "Simple" CDC-subset option is a simple vendor-neutral model that most
605 * full speed controllers can handle: one interface, two bulk endpoints.
606 *
607 * To assist host side drivers, we fancy it up a bit, and add descriptors
608 * so some host side drivers will understand it as a "SAFE" variant.
609 */
610
611static const struct usb_interface_descriptor
612subset_data_intf = {
613 .bLength = sizeof subset_data_intf,
614 .bDescriptorType = USB_DT_INTERFACE,
615
616 .bInterfaceNumber = 0,
617 .bAlternateSetting = 0,
618 .bNumEndpoints = 2,
619 .bInterfaceClass = USB_CLASS_COMM,
620 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
621 .bInterfaceProtocol = 0,
622 .iInterface = STRING_DATA,
623};
624
625#endif /* SUBSET */
626
Remy Bohmer23cd1382009-07-29 18:18:43 +0200627static struct usb_endpoint_descriptor
628fs_source_desc = {
629 .bLength = USB_DT_ENDPOINT_SIZE,
630 .bDescriptorType = USB_DT_ENDPOINT,
631
632 .bEndpointAddress = USB_DIR_IN,
633 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700634 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200635};
636
637static struct usb_endpoint_descriptor
638fs_sink_desc = {
639 .bLength = USB_DT_ENDPOINT_SIZE,
640 .bDescriptorType = USB_DT_ENDPOINT,
641
642 .bEndpointAddress = USB_DIR_OUT,
643 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700644 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200645};
646
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400647static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200648 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200649#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200650 /* "cdc" mode descriptors */
651 (struct usb_descriptor_header *) &control_intf,
652 (struct usb_descriptor_header *) &header_desc,
653 (struct usb_descriptor_header *) &union_desc,
654 (struct usb_descriptor_header *) &ether_desc,
655 /* NOTE: status endpoint may need to be removed */
656 (struct usb_descriptor_header *) &fs_status_desc,
657 /* data interface, with altsetting */
658 (struct usb_descriptor_header *) &data_nop_intf,
659 (struct usb_descriptor_header *) &data_intf,
660 (struct usb_descriptor_header *) &fs_source_desc,
661 (struct usb_descriptor_header *) &fs_sink_desc,
662 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200663#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200664};
665
666static inline void fs_subset_descriptors(void)
667{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200668#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200669 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
670 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
671 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
672 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
673 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
674 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
675 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
676 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
677 fs_eth_function[8] = NULL;
678#else
679 fs_eth_function[1] = NULL;
680#endif
681}
682
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300683#ifdef CONFIG_USB_ETH_RNDIS
684static const struct usb_descriptor_header *fs_rndis_function[] = {
685 (struct usb_descriptor_header *) &otg_descriptor,
686 /* control interface matches ACM, not Ethernet */
687 (struct usb_descriptor_header *) &rndis_control_intf,
688 (struct usb_descriptor_header *) &header_desc,
689 (struct usb_descriptor_header *) &call_mgmt_descriptor,
690 (struct usb_descriptor_header *) &acm_descriptor,
691 (struct usb_descriptor_header *) &union_desc,
692 (struct usb_descriptor_header *) &fs_status_desc,
693 /* data interface has no altsetting */
694 (struct usb_descriptor_header *) &rndis_data_intf,
695 (struct usb_descriptor_header *) &fs_source_desc,
696 (struct usb_descriptor_header *) &fs_sink_desc,
697 NULL,
698};
699#endif
700
Remy Bohmer23cd1382009-07-29 18:18:43 +0200701/*
702 * usb 2.0 devices need to expose both high speed and full speed
703 * descriptors, unless they only run at full speed.
704 */
705
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200706#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200707static struct usb_endpoint_descriptor
708hs_status_desc = {
709 .bLength = USB_DT_ENDPOINT_SIZE,
710 .bDescriptorType = USB_DT_ENDPOINT,
711
712 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400713 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200714 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
715};
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200716#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200717
718static struct usb_endpoint_descriptor
719hs_source_desc = {
720 .bLength = USB_DT_ENDPOINT_SIZE,
721 .bDescriptorType = USB_DT_ENDPOINT,
722
723 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400724 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200725};
726
727static struct usb_endpoint_descriptor
728hs_sink_desc = {
729 .bLength = USB_DT_ENDPOINT_SIZE,
730 .bDescriptorType = USB_DT_ENDPOINT,
731
732 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400733 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200734};
735
736static struct usb_qualifier_descriptor
737dev_qualifier = {
738 .bLength = sizeof dev_qualifier,
739 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
740
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400741 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200742 .bDeviceClass = USB_CLASS_COMM,
743
744 .bNumConfigurations = 1,
745};
746
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400747static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200748 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200749#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200750 /* "cdc" mode descriptors */
751 (struct usb_descriptor_header *) &control_intf,
752 (struct usb_descriptor_header *) &header_desc,
753 (struct usb_descriptor_header *) &union_desc,
754 (struct usb_descriptor_header *) &ether_desc,
755 /* NOTE: status endpoint may need to be removed */
756 (struct usb_descriptor_header *) &hs_status_desc,
757 /* data interface, with altsetting */
758 (struct usb_descriptor_header *) &data_nop_intf,
759 (struct usb_descriptor_header *) &data_intf,
760 (struct usb_descriptor_header *) &hs_source_desc,
761 (struct usb_descriptor_header *) &hs_sink_desc,
762 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200763#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200764};
765
766static inline void hs_subset_descriptors(void)
767{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200768#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200769 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
770 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
771 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
772 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
773 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
774 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
775 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
776 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
777 hs_eth_function[8] = NULL;
778#else
779 hs_eth_function[1] = NULL;
780#endif
781}
782
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300783#ifdef CONFIG_USB_ETH_RNDIS
784static const struct usb_descriptor_header *hs_rndis_function[] = {
785 (struct usb_descriptor_header *) &otg_descriptor,
786 /* control interface matches ACM, not Ethernet */
787 (struct usb_descriptor_header *) &rndis_control_intf,
788 (struct usb_descriptor_header *) &header_desc,
789 (struct usb_descriptor_header *) &call_mgmt_descriptor,
790 (struct usb_descriptor_header *) &acm_descriptor,
791 (struct usb_descriptor_header *) &union_desc,
792 (struct usb_descriptor_header *) &hs_status_desc,
793 /* data interface has no altsetting */
794 (struct usb_descriptor_header *) &rndis_data_intf,
795 (struct usb_descriptor_header *) &hs_source_desc,
796 (struct usb_descriptor_header *) &hs_sink_desc,
797 NULL,
798};
799#endif
800
801
Remy Bohmer23cd1382009-07-29 18:18:43 +0200802/* maxpacket and other transfer characteristics vary by speed. */
803static inline struct usb_endpoint_descriptor *
804ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
805 struct usb_endpoint_descriptor *fs)
806{
807 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
808 return hs;
809 return fs;
810}
811
Remy Bohmer23cd1382009-07-29 18:18:43 +0200812/*-------------------------------------------------------------------------*/
813
814/* descriptors that are built on-demand */
815
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400816static char manufacturer[50];
817static char product_desc[40] = DRIVER_DESC;
818static char serial_number[20];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200819
820/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400821static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200822
823/* static strings, in UTF-8 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400824static struct usb_string strings[] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200825 { STRING_MANUFACTURER, manufacturer, },
826 { STRING_PRODUCT, product_desc, },
827 { STRING_SERIALNUMBER, serial_number, },
828 { STRING_DATA, "Ethernet Data", },
829 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200830#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200831 { STRING_CDC, "CDC Ethernet", },
832 { STRING_CONTROL, "CDC Communications Control", },
833#endif
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200834#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200835 { STRING_SUBSET, "CDC Ethernet Subset", },
836#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300837#ifdef CONFIG_USB_ETH_RNDIS
838 { STRING_RNDIS, "RNDIS", },
839 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
840#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200841 { } /* end of list */
842};
843
844static struct usb_gadget_strings stringtab = {
845 .language = 0x0409, /* en-us */
846 .strings = strings,
847};
848
Remy Bohmer23cd1382009-07-29 18:18:43 +0200849/*============================================================================*/
Joel Fernandes52907592013-09-04 18:55:14 -0500850DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
851
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200852#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes52907592013-09-04 18:55:14 -0500853DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek563aed22012-10-02 17:04:29 +0200854#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200855
Remy Bohmer23cd1382009-07-29 18:18:43 +0200856/*============================================================================*/
857
858/*
859 * one config, two interfaces: control, data.
860 * complications: class descriptors, and an altsetting.
861 */
862static int
863config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
864{
865 int len;
866 const struct usb_config_descriptor *config;
867 const struct usb_descriptor_header **function;
868 int hs = 0;
869
870 if (gadget_is_dualspeed(g)) {
871 hs = (g->speed == USB_SPEED_HIGH);
872 if (type == USB_DT_OTHER_SPEED_CONFIG)
873 hs = !hs;
874 }
875#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
876
877 if (index >= device_desc.bNumConfigurations)
878 return -EINVAL;
879
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300880#ifdef CONFIG_USB_ETH_RNDIS
881 /*
882 * list the RNDIS config first, to make Microsoft's drivers
883 * happy. DOCSIS 1.0 needs this too.
884 */
885 if (device_desc.bNumConfigurations == 2 && index == 0) {
886 config = &rndis_config;
887 function = which_fn(rndis);
888 } else
889#endif
890 {
891 config = &eth_config;
892 function = which_fn(eth);
893 }
Remy Bohmer23cd1382009-07-29 18:18:43 +0200894
895 /* for now, don't advertise srp-only devices */
896 if (!is_otg)
897 function++;
898
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400899 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200900 if (len < 0)
901 return len;
902 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
903 return len;
904}
905
906/*-------------------------------------------------------------------------*/
907
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300908static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400909static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200910
911static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400912set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200913{
914 int result = 0;
915 struct usb_gadget *gadget = dev->gadget;
916
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200917#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300918 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200919 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400920 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmer23cd1382009-07-29 18:18:43 +0200921 &fs_status_desc);
922 dev->status_ep->driver_data = dev;
923
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400924 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200925 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400926 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200927 dev->status_ep->name, result);
928 goto done;
929 }
930 }
931#endif
932
933 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
934 dev->in_ep->driver_data = dev;
935
936 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
937 dev->out_ep->driver_data = dev;
938
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400939 /*
940 * With CDC, the host isn't allowed to use these two data
Remy Bohmer23cd1382009-07-29 18:18:43 +0200941 * endpoints in the default altsetting for the interface.
942 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300943 *
944 * Strictly speaking RNDIS should work the same: activation is
945 * a side effect of setting a packet filter. Deactivation is
946 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200947 */
948 if (!cdc_active(dev)) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400949 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200950 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400951 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200952 dev->in_ep->name, result);
953 goto done;
954 }
955
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400956 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200957 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400958 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200959 dev->out_ep->name, result);
960 goto done;
961 }
962 }
963
964done:
965 if (result == 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400966 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200967
968 /* on error, disable any endpoints */
969 if (result < 0) {
Vitaly Kuzmichevd5292c12010-08-13 17:02:29 +0400970 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400971 (void) usb_ep_disable(dev->status_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200972 dev->status = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400973 (void) usb_ep_disable(dev->in_ep);
974 (void) usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200975 dev->in = NULL;
976 dev->out = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300977 } else if (!cdc_active(dev)) {
978 /*
979 * activate non-CDC configs right away
980 * this isn't strictly according to the RNDIS spec
981 */
982 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200983 }
984
985 /* caller is responsible for cleanup on error */
986 return result;
987}
988
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400989static void eth_reset_config(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200990{
991 if (dev->config == 0)
992 return;
993
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400994 debug("%s\n", __func__);
995
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300996 rndis_uninit(dev->rndis_config);
997
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400998 /*
999 * disable endpoints, forcing (synchronous) completion of
Remy Bohmer23cd1382009-07-29 18:18:43 +02001000 * pending i/o. then free the requests.
1001 */
1002
1003 if (dev->in) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001004 usb_ep_disable(dev->in_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001005 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001006 usb_ep_free_request(dev->in_ep, dev->tx_req);
1007 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001008 }
1009 }
1010 if (dev->out) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001011 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001012 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001013 usb_ep_free_request(dev->out_ep, dev->rx_req);
1014 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001015 }
1016 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001017 if (dev->status)
1018 usb_ep_disable(dev->status_ep);
1019
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001020 dev->rndis = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001021 dev->cdc_filter = 0;
1022 dev->config = 0;
1023}
1024
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001025/*
1026 * change our operational config. must agree with the code
Remy Bohmer23cd1382009-07-29 18:18:43 +02001027 * that returns config descriptors, and altsetting code.
1028 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001029static int eth_set_config(struct eth_dev *dev, unsigned number,
1030 gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001031{
1032 int result = 0;
1033 struct usb_gadget *gadget = dev->gadget;
1034
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001035 if (gadget_is_sa1100(gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001036 && dev->config
1037 && dev->tx_qlen != 0) {
1038 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001039 error("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001040 return -ESPIPE;
1041 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001042 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001043
1044 switch (number) {
1045 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001046 result = set_ether_config(dev, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001047 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001048#ifdef CONFIG_USB_ETH_RNDIS
1049 case DEV_RNDIS_CONFIG_VALUE:
1050 dev->rndis = 1;
1051 result = set_ether_config(dev, gfp_flags);
1052 break;
1053#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02001054 default:
1055 result = -EINVAL;
1056 /* FALL THROUGH */
1057 case 0:
1058 break;
1059 }
1060
1061 if (result) {
1062 if (number)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001063 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001064 usb_gadget_vbus_draw(dev->gadget,
1065 gadget_is_otg(dev->gadget) ? 8 : 100);
1066 } else {
1067 char *speed;
1068 unsigned power;
1069
1070 power = 2 * eth_config.bMaxPower;
1071 usb_gadget_vbus_draw(dev->gadget, power);
1072
1073 switch (gadget->speed) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001074 case USB_SPEED_FULL:
1075 speed = "full"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001076#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001077 case USB_SPEED_HIGH:
1078 speed = "high"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001079#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001080 default:
1081 speed = "?"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001082 }
1083
1084 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001085 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001086 speed, number, power, driver_desc,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001087 rndis_active(dev)
1088 ? "RNDIS"
1089 : (cdc_active(dev)
1090 ? "CDC Ethernet"
Remy Bohmer23cd1382009-07-29 18:18:43 +02001091 : "CDC Ethernet Subset"));
1092 }
1093 return result;
1094}
1095
1096/*-------------------------------------------------------------------------*/
1097
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001098#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001099
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001100/*
1101 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001102 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001103 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmer23cd1382009-07-29 18:18:43 +02001104 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1105 * command mechanism; and only one status request is ever queued.
1106 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001107static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001108{
1109 struct usb_cdc_notification *event = req->buf;
1110 int value = req->status;
1111 struct eth_dev *dev = ep->driver_data;
1112
1113 /* issue the second notification if host reads the first */
1114 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1115 && value == 0) {
1116 __le32 *data = req->buf + sizeof *event;
1117
1118 event->bmRequestType = 0xA1;
1119 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001120 event->wValue = __constant_cpu_to_le16(0);
1121 event->wIndex = __constant_cpu_to_le16(1);
1122 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001123
1124 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001125 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmer23cd1382009-07-29 18:18:43 +02001126
1127 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001128 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001129 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001130 if (value == 0)
1131 return;
1132 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001133 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001134 event->bNotificationType, value);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001135 if (event->bNotificationType ==
1136 USB_CDC_NOTIFY_SPEED_CHANGE) {
1137 l_ethdev.network_started = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001138 printf("USB network up!\n");
1139 }
1140 }
1141 req->context = NULL;
1142}
1143
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001144static void issue_start_status(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001145{
1146 struct usb_request *req = dev->stat_req;
1147 struct usb_cdc_notification *event;
1148 int value;
1149
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001150 /*
1151 * flush old status
Remy Bohmer23cd1382009-07-29 18:18:43 +02001152 *
1153 * FIXME ugly idiom, maybe we'd be better with just
1154 * a "cancel the whole queue" primitive since any
1155 * unlink-one primitive has way too many error modes.
1156 * here, we "know" toggle is already clear...
1157 *
1158 * FIXME iff req->context != null just dequeue it
1159 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001160 usb_ep_disable(dev->status_ep);
1161 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001162
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001163 /*
1164 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmer23cd1382009-07-29 18:18:43 +02001165 * a SPEED_CHANGE. could be useful in some configs.
1166 */
1167 event = req->buf;
1168 event->bmRequestType = 0xA1;
1169 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001170 event->wValue = __constant_cpu_to_le16(1); /* connected */
1171 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001172 event->wLength = 0;
1173
1174 req->length = sizeof *event;
1175 req->complete = eth_status_complete;
1176 req->context = dev;
1177
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001178 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001179 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001180 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001181}
1182
1183#endif
1184
1185/*-------------------------------------------------------------------------*/
1186
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001187static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001188{
1189 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001190 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001191 req->status, req->actual, req->length);
1192}
1193
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001194#ifdef CONFIG_USB_ETH_RNDIS
1195
1196static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1197{
1198 if (req->status || req->actual != req->length)
1199 debug("rndis response complete --> %d, %d/%d\n",
1200 req->status, req->actual, req->length);
1201
1202 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1203}
1204
1205static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1206{
1207 struct eth_dev *dev = ep->driver_data;
1208 int status;
1209
1210 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1211 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1212 if (status < 0)
1213 error("%s: rndis parse error %d", __func__, status);
1214}
1215
1216#endif /* RNDIS */
1217
Remy Bohmer23cd1382009-07-29 18:18:43 +02001218/*
1219 * The setup() callback implements all the ep0 functionality that's not
1220 * handled lower down. CDC has a number of less-common features:
1221 *
1222 * - two interfaces: control, and ethernet data
1223 * - Ethernet data interface has two altsettings: default, and active
1224 * - class-specific descriptors for the control interface
1225 * - class-specific control requests
1226 */
1227static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001228eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001229{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001230 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001231 struct usb_request *req = dev->req;
1232 int value = -EOPNOTSUPP;
1233 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1234 u16 wValue = le16_to_cpu(ctrl->wValue);
1235 u16 wLength = le16_to_cpu(ctrl->wLength);
1236
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001237 /*
1238 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001239 * while config change events may enable network traffic.
1240 */
1241
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001242 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001243
1244 req->complete = eth_setup_complete;
1245 switch (ctrl->bRequest) {
1246
1247 case USB_REQ_GET_DESCRIPTOR:
1248 if (ctrl->bRequestType != USB_DIR_IN)
1249 break;
1250 switch (wValue >> 8) {
1251
1252 case USB_DT_DEVICE:
Kishon Vijay Abraham I04afd5b2015-02-23 18:40:17 +05301253 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001254 value = min(wLength, (u16) sizeof device_desc);
1255 memcpy(req->buf, &device_desc, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001256 break;
1257 case USB_DT_DEVICE_QUALIFIER:
1258 if (!gadget_is_dualspeed(gadget))
1259 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001260 value = min(wLength, (u16) sizeof dev_qualifier);
1261 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001262 break;
1263
1264 case USB_DT_OTHER_SPEED_CONFIG:
1265 if (!gadget_is_dualspeed(gadget))
1266 break;
1267 /* FALLTHROUGH */
1268 case USB_DT_CONFIG:
1269 value = config_buf(gadget, req->buf,
1270 wValue >> 8,
1271 wValue & 0xff,
1272 gadget_is_otg(gadget));
1273 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001274 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001275 break;
1276
1277 case USB_DT_STRING:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001278 value = usb_gadget_get_string(&stringtab,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001279 wValue & 0xff, req->buf);
1280
1281 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001282 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001283
1284 break;
1285 }
1286 break;
1287
1288 case USB_REQ_SET_CONFIGURATION:
1289 if (ctrl->bRequestType != 0)
1290 break;
1291 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001292 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001293 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001294 debug("HNP needs a different root port\n");
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001295 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001296 break;
1297 case USB_REQ_GET_CONFIGURATION:
1298 if (ctrl->bRequestType != USB_DIR_IN)
1299 break;
1300 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001301 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001302 break;
1303
1304 case USB_REQ_SET_INTERFACE:
1305 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1306 || !dev->config
1307 || wIndex > 1)
1308 break;
1309 if (!cdc_active(dev) && wIndex != 0)
1310 break;
1311
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001312 /*
1313 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001314 * we need to kluge around that interference.
1315 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001316 if (gadget_is_pxa(gadget)) {
1317 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001318 GFP_ATOMIC);
Lukasz Dalek563aed22012-10-02 17:04:29 +02001319 /*
1320 * PXA25x driver use non-CDC ethernet gadget.
1321 * But only _CDC and _RNDIS code can signalize
1322 * that network is working. So we signalize it
1323 * here.
1324 */
1325 l_ethdev.network_started = 1;
1326 debug("USB network up!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001327 goto done_set_intf;
1328 }
1329
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001330#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001331 switch (wIndex) {
1332 case 0: /* control/master intf */
1333 if (wValue != 0)
1334 break;
1335 if (dev->status) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001336 usb_ep_disable(dev->status_ep);
1337 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001338 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001339
Remy Bohmer23cd1382009-07-29 18:18:43 +02001340 value = 0;
1341 break;
1342 case 1: /* data intf */
1343 if (wValue > 1)
1344 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001345 usb_ep_disable(dev->in_ep);
1346 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001347
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001348 /*
1349 * CDC requires the data transfers not be done from
Remy Bohmer23cd1382009-07-29 18:18:43 +02001350 * the default interface setting ... also, setting
1351 * the non-default interface resets filters etc.
1352 */
1353 if (wValue == 1) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001354 if (!cdc_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001355 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001356 usb_ep_enable(dev->in_ep, dev->in);
1357 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001358 dev->cdc_filter = DEFAULT_FILTER;
1359 if (dev->status)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001360 issue_start_status(dev);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001361 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001362 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001363 value = 0;
1364 break;
1365 }
1366#else
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001367 /*
1368 * FIXME this is wrong, as is the assumption that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001369 * all non-PXA hardware talks real CDC ...
1370 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001371 debug("set_interface ignored!\n");
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001372#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001373
1374done_set_intf:
1375 break;
1376 case USB_REQ_GET_INTERFACE:
1377 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1378 || !dev->config
1379 || wIndex > 1)
1380 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001381 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001382 break;
1383
1384 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001385 if (rndis_active(dev) || wIndex != 1)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001386 *(u8 *)req->buf = 0;
1387 else {
1388 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1389 /* carrier always ok ...*/
1390 *(u8 *)req->buf = 1 ;
1391 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001392 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001393 break;
1394
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001395#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001396 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001397 /*
1398 * see 6.2.30: no data, wIndex = interface,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001399 * wValue = packet filter bitmap
1400 */
1401 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1402 || !cdc_active(dev)
1403 || wLength != 0
1404 || wIndex > 1)
1405 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001406 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001407 dev->cdc_filter = wValue;
1408 value = 0;
1409 break;
1410
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001411 /*
1412 * and potentially:
Remy Bohmer23cd1382009-07-29 18:18:43 +02001413 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1414 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1415 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1416 * case USB_CDC_GET_ETHERNET_STATISTIC:
1417 */
1418
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001419#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001420
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001421#ifdef CONFIG_USB_ETH_RNDIS
1422 /*
1423 * RNDIS uses the CDC command encapsulation mechanism to implement
1424 * an RPC scheme, with much getting/setting of attributes by OID.
1425 */
1426 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1427 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1428 || !rndis_active(dev)
1429 || wLength > USB_BUFSIZ
1430 || wValue
1431 || rndis_control_intf.bInterfaceNumber
1432 != wIndex)
1433 break;
1434 /* read the request, then process it */
1435 value = wLength;
1436 req->complete = rndis_command_complete;
1437 /* later, rndis_control_ack () sends a notification */
1438 break;
1439
1440 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1441 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1442 == ctrl->bRequestType
1443 && rndis_active(dev)
1444 /* && wLength >= 0x0400 */
1445 && !wValue
1446 && rndis_control_intf.bInterfaceNumber
1447 == wIndex) {
1448 u8 *buf;
1449 u32 n;
1450
1451 /* return the result */
1452 buf = rndis_get_next_response(dev->rndis_config, &n);
1453 if (buf) {
1454 memcpy(req->buf, buf, n);
1455 req->complete = rndis_response_complete;
1456 rndis_free_response(dev->rndis_config, buf);
1457 value = n;
1458 }
1459 /* else stalls ... spec says to avoid that */
1460 }
1461 break;
1462#endif /* RNDIS */
1463
Remy Bohmer23cd1382009-07-29 18:18:43 +02001464 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001465 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001466 ctrl->bRequestType, ctrl->bRequest,
1467 wValue, wIndex, wLength);
1468 }
1469
1470 /* respond with data transfer before status phase? */
1471 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001472 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001473 req->length = value;
1474 req->zero = value < wLength
1475 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001476 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001477 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001478 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001479 req->status = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001480 eth_setup_complete(gadget->ep0, req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001481 }
1482 }
1483
1484 /* host either stalls (value < 0) or reports success */
1485 return value;
1486}
1487
Remy Bohmer23cd1382009-07-29 18:18:43 +02001488/*-------------------------------------------------------------------------*/
1489
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001490static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001491
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001492static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001493 gfp_t gfp_flags)
1494{
1495 int retval = -ENOMEM;
1496 size_t size;
1497
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001498 /*
1499 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001500 * Normally we use the USB "terminate on short read" convention;
1501 * so allow up to (N*maxpacket), since that memory is normally
1502 * already allocated. Some hardware doesn't deal well with short
1503 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1504 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001505 *
1506 * RNDIS uses internal framing, and explicitly allows senders to
1507 * pad to end-of-packet. That's potentially nice for speed,
1508 * but means receivers can't recover synch on their own.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001509 */
1510
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001511 debug("%s\n", __func__);
Troy Kisky71fc5f92013-09-25 18:41:05 -07001512 if (!req)
1513 return -EINVAL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001514
1515 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1516 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001517 if (rndis_active(dev))
1518 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001519 size -= size % dev->out_ep->maxpacket;
1520
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001521 /*
1522 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001523 * but on at least one, checksumming fails otherwise. Note:
1524 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001525 */
1526
Joe Hershberger1fd92db2015-04-08 01:41:06 -05001527 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmer23cd1382009-07-29 18:18:43 +02001528 req->length = size;
1529 req->complete = rx_complete;
1530
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001531 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001532
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001533 if (retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001534 error("rx submit --> %d", retval);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001535
Remy Bohmer23cd1382009-07-29 18:18:43 +02001536 return retval;
1537}
1538
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001539static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001540{
1541 struct eth_dev *dev = ep->driver_data;
1542
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001543 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001544 switch (req->status) {
1545 /* normal completion */
1546 case 0:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001547 if (rndis_active(dev)) {
1548 /* we know MaxPacketsPerTransfer == 1 here */
1549 int length = rndis_rm_hdr(req->buf, req->actual);
1550 if (length < 0)
1551 goto length_err;
1552 req->length -= length;
1553 req->actual -= length;
1554 }
1555 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1556length_err:
1557 dev->stats.rx_errors++;
1558 dev->stats.rx_length_errors++;
1559 debug("rx length %d\n", req->length);
1560 break;
1561 }
1562
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001563 dev->stats.rx_packets++;
1564 dev->stats.rx_bytes += req->length;
1565 break;
1566
1567 /* software-driven interface shutdown */
1568 case -ECONNRESET: /* unlink */
1569 case -ESHUTDOWN: /* disconnect etc */
1570 /* for hardware automagic (such as pxa) */
1571 case -ECONNABORTED: /* endpoint reset */
1572 break;
1573
1574 /* data overrun */
1575 case -EOVERFLOW:
1576 dev->stats.rx_over_errors++;
1577 /* FALLTHROUGH */
1578 default:
1579 dev->stats.rx_errors++;
1580 break;
1581 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001582
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001583 packet_received = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001584}
1585
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001586static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001587{
1588
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001589 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001590
1591 if (!dev->tx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001592 goto fail1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001593
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001594 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001595
1596 if (!dev->rx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001597 goto fail2;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001598
1599 return 0;
1600
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001601fail2:
1602 usb_ep_free_request(dev->in_ep, dev->tx_req);
1603fail1:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001604 error("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001605 return -1;
1606}
1607
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001608static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001609{
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001610 struct eth_dev *dev = ep->driver_data;
1611
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001612 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001613 switch (req->status) {
1614 default:
1615 dev->stats.tx_errors++;
1616 debug("tx err %d\n", req->status);
1617 /* FALLTHROUGH */
1618 case -ECONNRESET: /* unlink */
1619 case -ESHUTDOWN: /* disconnect etc */
1620 break;
1621 case 0:
1622 dev->stats.tx_bytes += req->length;
1623 }
1624 dev->stats.tx_packets++;
1625
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001626 packet_sent = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001627}
1628
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001629static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001630{
1631 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001632 if (subset_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001633 return 1;
1634 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1635}
1636
1637#if 0
1638static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1639{
1640 struct eth_dev *dev = netdev_priv(net);
1641 int length = skb->len;
1642 int retval;
1643 struct usb_request *req = NULL;
1644 unsigned long flags;
1645
1646 /* apply outgoing CDC or RNDIS filters */
1647 if (!eth_is_promisc (dev)) {
1648 u8 *dest = skb->data;
1649
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001650 if (is_multicast_ethaddr(dest)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02001651 u16 type;
1652
1653 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1654 * SET_ETHERNET_MULTICAST_FILTERS requests
1655 */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001656 if (is_broadcast_ethaddr(dest))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001657 type = USB_CDC_PACKET_TYPE_BROADCAST;
1658 else
1659 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1660 if (!(dev->cdc_filter & type)) {
1661 dev_kfree_skb_any (skb);
1662 return 0;
1663 }
1664 }
1665 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1666 }
1667
1668 spin_lock_irqsave(&dev->req_lock, flags);
1669 /*
1670 * this freelist can be empty if an interrupt triggered disconnect()
1671 * and reconfigured the gadget (shutting down this queue) after the
1672 * network stack decided to xmit but before we got the spinlock.
1673 */
1674 if (list_empty(&dev->tx_reqs)) {
1675 spin_unlock_irqrestore(&dev->req_lock, flags);
1676 return 1;
1677 }
1678
1679 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1680 list_del (&req->list);
1681
1682 /* temporarily stop TX queue when the freelist empties */
1683 if (list_empty (&dev->tx_reqs))
1684 netif_stop_queue (net);
1685 spin_unlock_irqrestore(&dev->req_lock, flags);
1686
1687 /* no buffer copies needed, unless the network stack did it
1688 * or the hardware can't use skb buffers.
1689 * or there's not enough space for any RNDIS headers we need
1690 */
1691 if (rndis_active(dev)) {
1692 struct sk_buff *skb_rndis;
1693
1694 skb_rndis = skb_realloc_headroom (skb,
1695 sizeof (struct rndis_packet_msg_type));
1696 if (!skb_rndis)
1697 goto drop;
1698
1699 dev_kfree_skb_any (skb);
1700 skb = skb_rndis;
1701 rndis_add_hdr (skb);
1702 length = skb->len;
1703 }
1704 req->buf = skb->data;
1705 req->context = skb;
1706 req->complete = tx_complete;
1707
1708 /* use zlp framing on tx for strict CDC-Ether conformance,
1709 * though any robust network rx path ignores extra padding.
1710 * and some hardware doesn't like to write zlps.
1711 */
1712 req->zero = 1;
1713 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1714 length++;
1715
1716 req->length = length;
1717
1718 /* throttle highspeed IRQ rate back slightly */
1719 if (gadget_is_dualspeed(dev->gadget))
1720 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1721 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1722 : 0;
1723
1724 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1725 switch (retval) {
1726 default:
1727 DEBUG (dev, "tx queue err %d\n", retval);
1728 break;
1729 case 0:
1730 net->trans_start = jiffies;
1731 atomic_inc (&dev->tx_qlen);
1732 }
1733
1734 if (retval) {
1735drop:
1736 dev->stats.tx_dropped++;
1737 dev_kfree_skb_any (skb);
1738 spin_lock_irqsave(&dev->req_lock, flags);
1739 if (list_empty (&dev->tx_reqs))
1740 netif_start_queue (net);
1741 list_add (&req->list, &dev->tx_reqs);
1742 spin_unlock_irqrestore(&dev->req_lock, flags);
1743 }
1744 return 0;
1745}
1746
1747/*-------------------------------------------------------------------------*/
1748#endif
1749
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001750static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001751{
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001752 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001753
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001754 debug("%s...\n", __func__);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001755 rndis_deregister(dev->rndis_config);
1756 rndis_exit();
Remy Bohmer23cd1382009-07-29 18:18:43 +02001757
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001758 /* we've already been disconnected ... no i/o is active */
1759 if (dev->req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001760 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001761 dev->req = NULL;
1762 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001763 if (dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001764 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001765 dev->stat_req = NULL;
1766 }
1767
1768 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001769 usb_ep_free_request(dev->in_ep, dev->tx_req);
1770 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001771 }
1772
1773 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001774 usb_ep_free_request(dev->out_ep, dev->rx_req);
1775 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001776 }
1777
1778/* unregister_netdev (dev->net);*/
1779/* free_netdev(dev->net);*/
1780
Lei Wen988ee3e2010-12-01 23:43:43 +08001781 dev->gadget = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001782 set_gadget_data(gadget, NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001783}
1784
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001785static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001786{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001787 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001788 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001789}
1790
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001791static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001792{
1793 /* Not used */
1794}
1795
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001796static void eth_resume(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001797{
1798 /* Not used */
1799}
1800
1801/*-------------------------------------------------------------------------*/
1802
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001803#ifdef CONFIG_USB_ETH_RNDIS
1804
1805/*
1806 * The interrupt endpoint is used in RNDIS to notify the host when messages
1807 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1808 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1809 * REMOTE_NDIS_KEEPALIVE_MSG.
1810 *
1811 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1812 * normally just one notification will be queued.
1813 */
1814
1815static void rndis_control_ack_complete(struct usb_ep *ep,
1816 struct usb_request *req)
1817{
1818 struct eth_dev *dev = ep->driver_data;
1819
1820 debug("%s...\n", __func__);
1821 if (req->status || req->actual != req->length)
1822 debug("rndis control ack complete --> %d, %d/%d\n",
1823 req->status, req->actual, req->length);
1824
1825 if (!l_ethdev.network_started) {
1826 if (rndis_get_state(dev->rndis_config)
1827 == RNDIS_DATA_INITIALIZED) {
1828 l_ethdev.network_started = 1;
1829 printf("USB RNDIS network up!\n");
1830 }
1831 }
1832
1833 req->context = NULL;
1834
1835 if (req != dev->stat_req)
1836 usb_ep_free_request(ep, req);
1837}
1838
1839static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1840
1841static int rndis_control_ack(struct eth_device *net)
1842{
1843 struct eth_dev *dev = &l_ethdev;
1844 int length;
1845 struct usb_request *resp = dev->stat_req;
1846
1847 /* in case RNDIS calls this after disconnect */
1848 if (!dev->status) {
1849 debug("status ENODEV\n");
1850 return -ENODEV;
1851 }
1852
1853 /* in case queue length > 1 */
1854 if (resp->context) {
1855 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1856 if (!resp)
1857 return -ENOMEM;
1858 resp->buf = rndis_resp_buf;
1859 }
1860
1861 /*
1862 * Send RNDIS RESPONSE_AVAILABLE notification;
1863 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1864 */
1865 resp->length = 8;
1866 resp->complete = rndis_control_ack_complete;
1867 resp->context = dev;
1868
1869 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1870 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1871
1872 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1873 if (length < 0) {
1874 resp->status = 0;
1875 rndis_control_ack_complete(dev->status_ep, resp);
1876 }
1877
1878 return 0;
1879}
1880
1881#else
1882
1883#define rndis_control_ack NULL
1884
1885#endif /* RNDIS */
1886
1887static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1888{
1889 if (rndis_active(dev)) {
1890 rndis_set_param_medium(dev->rndis_config,
1891 NDIS_MEDIUM_802_3,
1892 BITRATE(dev->gadget)/100);
1893 rndis_signal_connect(dev->rndis_config);
1894 }
1895}
1896
1897static int eth_stop(struct eth_dev *dev)
1898{
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001899#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1900 unsigned long ts;
1901 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1902#endif
1903
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001904 if (rndis_active(dev)) {
1905 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1906 rndis_signal_disconnect(dev->rndis_config);
1907
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001908#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1909 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1910 ts = get_timer(0);
1911 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05301912 usb_gadget_handle_interrupts(0);
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001913#endif
1914
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001915 rndis_uninit(dev->rndis_config);
1916 dev->rndis = 0;
1917 }
1918
1919 return 0;
1920}
1921
1922/*-------------------------------------------------------------------------*/
1923
Remy Bohmer23cd1382009-07-29 18:18:43 +02001924static int is_eth_addr_valid(char *str)
1925{
1926 if (strlen(str) == 17) {
1927 int i;
1928 char *p, *q;
1929 uchar ea[6];
1930
1931 /* see if it looks like an ethernet address */
1932
1933 p = str;
1934
1935 for (i = 0; i < 6; i++) {
1936 char term = (i == 5 ? '\0' : ':');
1937
1938 ea[i] = simple_strtol(p, &q, 16);
1939
1940 if ((q - p) != 2 || *q++ != term)
1941 break;
1942
1943 p = q;
1944 }
1945
Tom Rini57a87a22012-10-31 13:30:41 +00001946 /* Now check the contents. */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001947 return is_valid_ethaddr(ea);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001948 }
1949 return 0;
1950}
1951
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001952static u8 nibble(unsigned char c)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001953{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001954 if (likely(isdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001955 return c - '0';
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001956 c = toupper(c);
1957 if (likely(isxdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001958 return 10 + c - 'A';
1959 return 0;
1960}
1961
1962static int get_ether_addr(const char *str, u8 *dev_addr)
1963{
1964 if (str) {
1965 unsigned i;
1966
1967 for (i = 0; i < 6; i++) {
1968 unsigned char num;
1969
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001970 if ((*str == '.') || (*str == ':'))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001971 str++;
1972 num = nibble(*str++) << 4;
1973 num |= (nibble(*str++));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001974 dev_addr[i] = num;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001975 }
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001976 if (is_valid_ethaddr(dev_addr))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001977 return 0;
1978 }
1979 return 1;
1980}
1981
1982static int eth_bind(struct usb_gadget *gadget)
1983{
1984 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001985 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001986 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001987 int status = -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001988 int gcnum;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001989 u8 tmp[7];
Remy Bohmer23cd1382009-07-29 18:18:43 +02001990
1991 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001992#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001993 cdc = 0;
1994#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001995#ifndef CONFIG_USB_ETH_RNDIS
1996 rndis = 0;
1997#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001998 /*
1999 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmer23cd1382009-07-29 18:18:43 +02002000 * standard protocol is _strongly_ preferred for interop purposes.
2001 * (By everyone except Microsoft.)
2002 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002003 if (gadget_is_pxa(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002004 /* pxa doesn't support altsettings */
2005 cdc = 0;
2006 } else if (gadget_is_musbhdrc(gadget)) {
2007 /* reduce tx dma overhead by avoiding special cases */
2008 zlp = 0;
2009 } else if (gadget_is_sh(gadget)) {
2010 /* sh doesn't support multiple interfaces or configs */
2011 cdc = 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002012 rndis = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002013 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002014 /* hardware can't write zlps */
2015 zlp = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002016 /*
2017 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmer23cd1382009-07-29 18:18:43 +02002018 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2019 */
2020 cdc = 0;
2021 }
2022
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002023 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002024 if (gcnum >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002025 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002026 else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002027 /*
2028 * can't assume CDC works. don't want to default to
Remy Bohmer23cd1382009-07-29 18:18:43 +02002029 * anything less functional on CDC-capable hardware,
2030 * so we fail in this case.
2031 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002032 error("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002033 gadget->name);
2034 return -ENODEV;
2035 }
2036
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002037 /*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002038 * If there's an RNDIS configuration, that's what Windows wants to
2039 * be using ... so use these product IDs here and in the "linux.inf"
2040 * needed to install MSFT drivers. Current Linux kernels will use
2041 * the second configuration if it's CDC Ethernet, and need some help
2042 * to choose the right configuration otherwise.
2043 */
2044 if (rndis) {
2045#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2046 device_desc.idVendor =
2047 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2048 device_desc.idProduct =
2049 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2050#else
2051 device_desc.idVendor =
2052 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2053 device_desc.idProduct =
2054 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2055#endif
2056 sprintf(product_desc, "RNDIS/%s", driver_desc);
2057
2058 /*
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002059 * CDC subset ... recognized by Linux since 2.4.10, but Windows
Remy Bohmer23cd1382009-07-29 18:18:43 +02002060 * drivers aren't widely available. (That may be improved by
2061 * supporting one submode of the "SAFE" variant of MDLM.)
2062 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002063 } else {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002064#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002065 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2066 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2067#else
2068 if (!cdc) {
2069 device_desc.idVendor =
2070 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2071 device_desc.idProduct =
2072 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2073 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002074#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002075 }
2076 /* support optional vendor/distro customization */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002077 if (bcdDevice)
2078 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2079 if (iManufacturer)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002080 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002081 if (iProduct)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002082 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002083 if (iSerialNumber) {
2084 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev2e12abe2010-08-13 17:00:45 +04002085 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002086 }
2087
2088 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002089 usb_ep_autoconfig_reset(gadget);
2090 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002091 if (!in_ep) {
2092autoconf_fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002093 error("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002094 gadget->name);
2095 return -ENODEV;
2096 }
2097 in_ep->driver_data = in_ep; /* claim */
2098
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002099 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002100 if (!out_ep)
2101 goto autoconf_fail;
2102 out_ep->driver_data = out_ep; /* claim */
2103
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002104#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002105 /*
2106 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmer23cd1382009-07-29 18:18:43 +02002107 * Since some hosts expect one, try to allocate one anyway.
2108 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002109 if (cdc || rndis) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002110 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002111 if (status_ep) {
2112 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002113 } else if (rndis) {
2114 error("can't run RNDIS on %s", gadget->name);
2115 return -ENODEV;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002116#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002117 } else if (cdc) {
2118 control_intf.bNumEndpoints = 0;
2119 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002120#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002121 }
2122 }
2123#endif
2124
2125 /* one config: cdc, else minimal subset */
2126 if (!cdc) {
2127 eth_config.bNumInterfaces = 1;
2128 eth_config.iConfiguration = STRING_SUBSET;
2129
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002130 /*
2131 * use functions to set these up, in case we're built to work
Remy Bohmer23cd1382009-07-29 18:18:43 +02002132 * with multiple controllers and must override CDC Ethernet.
2133 */
2134 fs_subset_descriptors();
2135 hs_subset_descriptors();
2136 }
2137
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002138 usb_gadget_set_selfpowered(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002139
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002140 /* For now RNDIS is always a second config */
2141 if (rndis)
2142 device_desc.bNumConfigurations = 2;
2143
Remy Bohmer23cd1382009-07-29 18:18:43 +02002144 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002145 if (rndis)
2146 dev_qualifier.bNumConfigurations = 2;
2147 else if (!cdc)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002148 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2149
2150 /* assumes ep0 uses the same value for both speeds ... */
2151 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2152
2153 /* and that all endpoints are dual-speed */
2154 hs_source_desc.bEndpointAddress =
2155 fs_source_desc.bEndpointAddress;
2156 hs_sink_desc.bEndpointAddress =
2157 fs_sink_desc.bEndpointAddress;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002158#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002159 if (status_ep)
2160 hs_status_desc.bEndpointAddress =
2161 fs_status_desc.bEndpointAddress;
2162#endif
2163 }
2164
2165 if (gadget_is_otg(gadget)) {
2166 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2167 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2168 eth_config.bMaxPower = 4;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002169#ifdef CONFIG_USB_ETH_RNDIS
2170 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2171 rndis_config.bMaxPower = 4;
2172#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002173 }
2174
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002175
2176 /* network device setup */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002177 dev->net = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002178
2179 dev->cdc = cdc;
2180 dev->zlp = zlp;
2181
2182 dev->in_ep = in_ep;
2183 dev->out_ep = out_ep;
2184 dev->status_ep = status_ep;
2185
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002186 /*
2187 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002188 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmer23cd1382009-07-29 18:18:43 +02002189 * ends up in a persistent config database. It's not clear if
2190 * host side code for the SAFE thing cares -- its original BLAN
2191 * thing didn't, Sharp never assigned those addresses on Zaurii.
2192 */
2193 get_ether_addr(dev_addr, dev->net->enetaddr);
2194
2195 memset(tmp, 0, sizeof(tmp));
2196 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2197
2198 get_ether_addr(host_addr, dev->host_mac);
2199
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002200 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2201 dev->host_mac[0], dev->host_mac[1],
2202 dev->host_mac[2], dev->host_mac[3],
2203 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002204
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002205 if (rndis) {
2206 status = rndis_init();
2207 if (status < 0) {
2208 error("can't init RNDIS, %d", status);
2209 goto fail;
2210 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002211 }
2212
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002213 /*
2214 * use PKTSIZE (or aligned... from u-boot) and set
2215 * wMaxSegmentSize accordingly
2216 */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002217 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2218
2219 /* preallocate control message data and buffer */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002220 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002221 if (!dev->req)
2222 goto fail;
2223 dev->req->buf = control_req;
2224 dev->req->complete = eth_setup_complete;
2225
2226 /* ... and maybe likewise for status transfer */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002227#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002228 if (dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002229 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2230 GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002231 if (!dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002232 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002233
2234 goto fail;
2235 }
Vitaly Kuzmichevdf559c12010-08-13 17:01:06 +04002236 dev->stat_req->buf = status_req;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002237 dev->stat_req->context = NULL;
2238 }
2239#endif
2240
2241 /* finish hookup to lower layer ... */
2242 dev->gadget = gadget;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002243 set_gadget_data(gadget, dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002244 gadget->ep0->driver_data = dev;
2245
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002246 /*
2247 * two kinds of host-initiated state changes:
Remy Bohmer23cd1382009-07-29 18:18:43 +02002248 * - iff DATA transfer is active, carrier is "on"
2249 * - tx queueing enabled if open *and* carrier is "on"
2250 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002251
2252 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2253 out_ep->name, in_ep->name,
2254 status_ep ? " STATUS " : "",
2255 status_ep ? status_ep->name : ""
2256 );
2257 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2258 dev->net->enetaddr[0], dev->net->enetaddr[1],
2259 dev->net->enetaddr[2], dev->net->enetaddr[3],
2260 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2261
2262 if (cdc || rndis)
2263 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2264 dev->host_mac[0], dev->host_mac[1],
2265 dev->host_mac[2], dev->host_mac[3],
2266 dev->host_mac[4], dev->host_mac[5]);
2267
2268 if (rndis) {
2269 u32 vendorID = 0;
2270
2271 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2272
2273 dev->rndis_config = rndis_register(rndis_control_ack);
2274 if (dev->rndis_config < 0) {
2275fail0:
2276 eth_unbind(gadget);
2277 debug("RNDIS setup failed\n");
2278 status = -ENODEV;
2279 goto fail;
2280 }
2281
2282 /* these set up a lot of the OIDs that RNDIS needs */
2283 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2284 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2285 &dev->stats, &dev->cdc_filter))
2286 goto fail0;
2287 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2288 manufacturer))
2289 goto fail0;
2290 if (rndis_set_param_medium(dev->rndis_config,
2291 NDIS_MEDIUM_802_3, 0))
2292 goto fail0;
2293 printf("RNDIS ready\n");
2294 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002295 return 0;
2296
2297fail:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002298 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002299 eth_unbind(gadget);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002300 return status;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002301}
2302
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002303/*-------------------------------------------------------------------------*/
2304
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002305static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002306{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002307 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002308 struct usb_gadget *gadget;
2309 unsigned long ts;
2310 unsigned long timeout = USB_CONNECT_TIMEOUT;
2311
2312 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002313 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002314 goto fail;
2315 }
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002316
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302317 board_usb_init(0, USB_INIT_DEVICE);
2318
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002319 /* Configure default mac-addresses for the USB ethernet device */
2320#ifdef CONFIG_USBNET_DEV_ADDR
2321 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2322#endif
2323#ifdef CONFIG_USBNET_HOST_ADDR
2324 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2325#endif
2326 /* Check if the user overruled the MAC addresses */
2327 if (getenv("usbnet_devaddr"))
2328 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2329 sizeof(dev_addr));
2330
2331 if (getenv("usbnet_hostaddr"))
2332 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2333 sizeof(host_addr));
2334
2335 if (!is_eth_addr_valid(dev_addr)) {
2336 error("Need valid 'usbnet_devaddr' to be set");
2337 goto fail;
2338 }
2339 if (!is_eth_addr_valid(host_addr)) {
2340 error("Need valid 'usbnet_hostaddr' to be set");
2341 goto fail;
2342 }
2343
Lei Wen988ee3e2010-12-01 23:43:43 +08002344 if (usb_gadget_register_driver(&eth_driver) < 0)
2345 goto fail;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002346
2347 dev->network_started = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002348
2349 packet_received = 0;
2350 packet_sent = 0;
2351
2352 gadget = dev->gadget;
2353 usb_gadget_connect(gadget);
2354
2355 if (getenv("cdc_connect_timeout"))
2356 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2357 NULL, 10) * CONFIG_SYS_HZ;
2358 ts = get_timer(0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002359 while (!l_ethdev.network_started) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002360 /* Handle control-c and timeouts */
2361 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002362 error("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002363 goto fail;
2364 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302365 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002366 }
2367
Vitaly Kuzmichev98fae972010-09-22 13:13:56 +04002368 packet_received = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002369 rx_submit(dev, dev->rx_req, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002370 return 0;
2371fail:
2372 return -1;
2373}
2374
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002375static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002376{
2377 int retval;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002378 void *rndis_pkt = NULL;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002379 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002380 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002381 unsigned long ts;
2382 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002383
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002384 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002385
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002386 /* new buffer is needed to include RNDIS header */
2387 if (rndis_active(dev)) {
2388 rndis_pkt = malloc(length +
2389 sizeof(struct rndis_packet_msg_type));
2390 if (!rndis_pkt) {
2391 error("No memory to alloc RNDIS packet");
2392 goto drop;
2393 }
2394 rndis_add_hdr(rndis_pkt, length);
2395 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002396 packet, length);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002397 packet = rndis_pkt;
2398 length += sizeof(struct rndis_packet_msg_type);
2399 }
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002400 req->buf = packet;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002401 req->context = NULL;
2402 req->complete = tx_complete;
2403
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002404 /*
2405 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002406 * though any robust network rx path ignores extra padding.
2407 * and some hardware doesn't like to write zlps.
2408 */
2409 req->zero = 1;
2410 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2411 length++;
2412
2413 req->length = length;
2414#if 0
2415 /* throttle highspeed IRQ rate back slightly */
2416 if (gadget_is_dualspeed(dev->gadget))
2417 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2418 ? ((dev->tx_qlen % qmult) != 0) : 0;
2419#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002420 dev->tx_qlen = 1;
2421 ts = get_timer(0);
2422 packet_sent = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002423
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002424 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002425
2426 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002427 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002428 while (!packet_sent) {
2429 if (get_timer(ts) > timeout) {
2430 printf("timeout sending packets to usb ethernet\n");
2431 return -1;
2432 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302433 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002434 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002435 if (rndis_pkt)
2436 free(rndis_pkt);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002437
2438 return 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002439drop:
2440 dev->stats.tx_dropped++;
2441 return -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002442}
2443
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002444static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002445{
2446 struct eth_dev *dev = &l_ethdev;
2447
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302448 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002449
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002450 if (packet_received) {
2451 debug("%s: packet received\n", __func__);
2452 if (dev->rx_req) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -05002453 net_process_received_packet(net_rx_packets[0],
2454 dev->rx_req->length);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002455 packet_received = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002456
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002457 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002458 } else
2459 error("dev->rx_req invalid");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002460 }
2461 return 0;
2462}
2463
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002464void usb_eth_halt(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002465{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002466 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002467
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002468 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002469 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002470 return;
2471 }
2472
Lei Wen988ee3e2010-12-01 23:43:43 +08002473 /* If the gadget not registered, simple return */
2474 if (!dev->gadget)
2475 return;
2476
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03002477 /*
2478 * Some USB controllers may need additional deinitialization here
2479 * before dropping pull-up (also due to hardware issues).
2480 * For example: unhandled interrupt with status stage started may
2481 * bring the controller to fully broken state (until board reset).
2482 * There are some variants to debug and fix such cases:
2483 * 1) In the case of RNDIS connection eth_stop can perform additional
2484 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2485 * 2) 'pullup' callback in your UDC driver can be improved to perform
2486 * this deinitialization.
2487 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002488 eth_stop(dev);
2489
Remy Bohmer23cd1382009-07-29 18:18:43 +02002490 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002491
2492 /* Clear pending interrupt */
2493 if (dev->network_started) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302494 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002495 dev->network_started = 0;
2496 }
2497
Lei Wen988ee3e2010-12-01 23:43:43 +08002498 usb_gadget_unregister_driver(&eth_driver);
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302499 board_usb_cleanup(0, USB_INIT_DEVICE);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002500}
2501
2502static struct usb_gadget_driver eth_driver = {
2503 .speed = DEVSPEED,
2504
2505 .bind = eth_bind,
2506 .unbind = eth_unbind,
2507
2508 .setup = eth_setup,
Kishon Vijay Abraham I5df13152015-08-19 13:49:48 +05302509 .reset = eth_disconnect,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002510 .disconnect = eth_disconnect,
2511
2512 .suspend = eth_suspend,
2513 .resume = eth_resume,
2514};
2515
2516int usb_eth_initialize(bd_t *bi)
2517{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002518 struct eth_device *netdev = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002519
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +03002520 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmer23cd1382009-07-29 18:18:43 +02002521
2522 netdev->init = usb_eth_init;
2523 netdev->send = usb_eth_send;
2524 netdev->recv = usb_eth_recv;
2525 netdev->halt = usb_eth_halt;
2526
2527#ifdef CONFIG_MCAST_TFTP
2528 #error not supported
2529#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002530 eth_register(netdev);
2531 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002532}