blob: 53f4672291f3d83a32cae43f4bd99caedba6c502 [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>
Remy Bohmer23cd1382009-07-29 18:18:43 +020020#include <linux/ctype.h>
21
22#include "gadget_chips.h"
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030023#include "rndis.h"
Remy Bohmer23cd1382009-07-29 18:18:43 +020024
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +030025#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +040026
Remy Bohmer23cd1382009-07-29 18:18:43 +020027#define atomic_read
28extern struct platform_data brd;
Remy Bohmer23cd1382009-07-29 18:18:43 +020029
30
31unsigned packet_received, packet_sent;
32
Remy Bohmer23cd1382009-07-29 18:18:43 +020033/*
34 * Ethernet gadget driver -- with CDC and non-CDC options
35 * Builds on hardware support for a full duplex link.
36 *
37 * CDC Ethernet is the standard USB solution for sending Ethernet frames
38 * using USB. Real hardware tends to use the same framing protocol but look
39 * different for control features. This driver strongly prefers to use
40 * this USB-IF standard as its open-systems interoperability solution;
41 * most host side USB stacks (except from Microsoft) support it.
42 *
43 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
44 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
45 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
46 *
47 * There's some hardware that can't talk CDC ECM. We make that hardware
48 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
49 * link-level setup only requires activating the configuration. Only the
50 * endpoint descriptors, and product/vendor IDs, are relevant; no control
51 * operations are available. Linux supports it, but other host operating
52 * systems may not. (This is a subset of CDC Ethernet.)
53 *
54 * It turns out that if you add a few descriptors to that "CDC Subset",
55 * (Windows) host side drivers from MCCI can treat it as one submode of
56 * a proprietary scheme called "SAFE" ... without needing to know about
57 * specific product/vendor IDs. So we do that, making it easier to use
58 * those MS-Windows drivers. Those added descriptors make it resemble a
59 * CDC MDLM device, but they don't change device behavior at all. (See
60 * MCCI Engineering report 950198 "SAFE Networking Functions".)
61 *
62 * A third option is also in use. Rather than CDC Ethernet, or something
63 * simpler, Microsoft pushes their own approach: RNDIS. The published
64 * RNDIS specs are ambiguous and appear to be incomplete, and are also
65 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
66 */
67#define ETH_ALEN 6 /* Octets in one ethernet addr */
68#define ETH_HLEN 14 /* Total octets in header. */
69#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
70#define ETH_DATA_LEN 1500 /* Max. octets in payload */
71#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
Remy Bohmer23cd1382009-07-29 18:18:43 +020072
73#define DRIVER_DESC "Ethernet Gadget"
74/* Based on linux 2.6.27 version */
75#define DRIVER_VERSION "May Day 2005"
76
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040077static const char shortname[] = "ether";
78static const char driver_desc[] = DRIVER_DESC;
Remy Bohmer23cd1382009-07-29 18:18:43 +020079
80#define RX_EXTRA 20 /* guard against rx overflows */
81
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +030082#ifndef CONFIG_USB_ETH_RNDIS
83#define rndis_uninit(x) do {} while (0)
84#define rndis_deregister(c) do {} while (0)
85#define rndis_exit() do {} while (0)
86#endif
87
88/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmer23cd1382009-07-29 18:18:43 +020089#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
90 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
91 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
92 |USB_CDC_PACKET_TYPE_DIRECTED)
93
94#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
95
96/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +030097
98struct eth_dev {
99 struct usb_gadget *gadget;
100 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300101 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300102
103 u8 config;
104 struct usb_ep *in_ep, *out_ep, *status_ep;
105 const struct usb_endpoint_descriptor
106 *in, *out, *status;
107
108 struct usb_request *tx_req, *rx_req;
109
110 struct eth_device *net;
111 struct net_device_stats stats;
112 unsigned int tx_qlen;
113
114 unsigned zlp:1;
115 unsigned cdc:1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300116 unsigned rndis:1;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300117 unsigned suspended:1;
118 unsigned network_started:1;
119 u16 cdc_filter;
120 unsigned long todo;
121 int mtu;
122#define WORK_RX_MEMORY 0
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300123 int rndis_config;
Vitaly Kuzmichev8b6b66b2011-02-11 18:18:33 +0300124 u8 host_mac[ETH_ALEN];
125};
126
127/*
128 * This version autoconfigures as much as possible at run-time.
129 *
130 * It also ASSUMES a self-powered device, without remote wakeup,
131 * although remote wakeup support would make sense.
132 */
133
134/*-------------------------------------------------------------------------*/
Remy Bohmer23cd1382009-07-29 18:18:43 +0200135static struct eth_dev l_ethdev;
136static struct eth_device l_netdev;
137static struct usb_gadget_driver eth_driver;
138
139/*-------------------------------------------------------------------------*/
140
141/* "main" config is either CDC, or its simple subset */
142static inline int is_cdc(struct eth_dev *dev)
143{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200144#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200145 return 1; /* only cdc possible */
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200146#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200147 return 0; /* only subset possible */
148#else
149 return dev->cdc; /* depends on what hardware we found */
150#endif
151}
152
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300153/* "secondary" RNDIS config may sometimes be activated */
154static inline int rndis_active(struct eth_dev *dev)
155{
156#ifdef CONFIG_USB_ETH_RNDIS
157 return dev->rndis;
158#else
159 return 0;
160#endif
161}
162
163#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
164#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200165
166#define DEFAULT_QLEN 2 /* double buffering by default */
167
168/* peak bulk transfer bits-per-second */
169#define HS_BPS (13 * 512 * 8 * 1000 * 8)
170#define FS_BPS (19 * 64 * 1 * 1000 * 8)
171
172#ifdef CONFIG_USB_GADGET_DUALSPEED
173#define DEVSPEED USB_SPEED_HIGH
174
Vitaly Kuzmichev2721dbf2010-08-12 16:44:40 +0400175#ifdef CONFIG_USB_ETH_QMULT
176#define qmult CONFIG_USB_ETH_QMULT
177#else
178#define qmult 5
179#endif
180
Remy Bohmer23cd1382009-07-29 18:18:43 +0200181/* for dual-speed hardware, use deeper queues at highspeed */
182#define qlen(gadget) \
183 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
184
185static inline int BITRATE(struct usb_gadget *g)
186{
187 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
188}
189
190#else /* full speed (low speed doesn't do bulk) */
191
192#define qmult 1
193
194#define DEVSPEED USB_SPEED_FULL
195
196#define qlen(gadget) DEFAULT_QLEN
197
198static inline int BITRATE(struct usb_gadget *g)
199{
200 return FS_BPS;
201}
202#endif
203
Remy Bohmer23cd1382009-07-29 18:18:43 +0200204/*-------------------------------------------------------------------------*/
205
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400206/*
207 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmer23cd1382009-07-29 18:18:43 +0200208 * Instead: allocate your own, using normal USB-IF procedures.
209 */
210
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400211/*
212 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200213 * It's for devices with only CDC Ethernet configurations.
214 */
215#define CDC_VENDOR_NUM 0x0525 /* NetChip */
216#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
217
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400218/*
219 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmer23cd1382009-07-29 18:18:43 +0200220 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
221 * with pxa250. We're protocol-compatible, if the host-side drivers
222 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
223 * drivers that need to hard-wire endpoint numbers have a hook.
224 *
225 * The protocol is a minimal subset of CDC Ether, which works on any bulk
226 * hardware that's not deeply broken ... even on hardware that can't talk
227 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
228 * doesn't handle control-OUT).
229 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300230#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
231#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
232
233/*
234 * For hardware that can talk RNDIS and either of the above protocols,
235 * use this ID ... the windows INF files will know it. Unless it's
236 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
237 * the non-RNDIS configuration.
238 */
239#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
240#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200241
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400242/*
243 * Some systems will want different product identifers published in the
Remy Bohmer23cd1382009-07-29 18:18:43 +0200244 * device descriptor, either numbers or strings or both. These string
245 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
246 */
247
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300248/*
249 * Emulating them in eth_bind:
250 * static ushort idVendor;
251 * static ushort idProduct;
252 */
253
Remy Bohmer23cd1382009-07-29 18:18:43 +0200254#if defined(CONFIG_USBNET_MANUFACTURER)
255static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
256#else
257static char *iManufacturer = "U-boot";
258#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300259
260/* These probably need to be configurable. */
261static ushort bcdDevice;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200262static char *iProduct;
263static char *iSerialNumber;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300264
Remy Bohmer23cd1382009-07-29 18:18:43 +0200265static char dev_addr[18];
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300266
Remy Bohmer23cd1382009-07-29 18:18:43 +0200267static char host_addr[18];
268
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300269
Remy Bohmer23cd1382009-07-29 18:18:43 +0200270/*-------------------------------------------------------------------------*/
271
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400272/*
273 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmer23cd1382009-07-29 18:18:43 +0200274 * ep0 implementation: descriptors, config management, setup().
275 * also optional class-specific notification interrupt transfer.
276 */
277
278/*
279 * DESCRIPTORS ... most are static, but strings and (full) configuration
280 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300281 * our simple subset, with RNDIS as an optional second configuration.
282 *
283 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
284 * the class descriptors match a modem (they're ignored; it's really just
285 * Ethernet functionality), they don't need the NOP altsetting, and the
286 * status transfer endpoint isn't optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200287 */
288
289#define STRING_MANUFACTURER 1
290#define STRING_PRODUCT 2
291#define STRING_ETHADDR 3
292#define STRING_DATA 4
293#define STRING_CONTROL 5
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300294#define STRING_RNDIS_CONTROL 6
Remy Bohmer23cd1382009-07-29 18:18:43 +0200295#define STRING_CDC 7
296#define STRING_SUBSET 8
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300297#define STRING_RNDIS 9
Remy Bohmer23cd1382009-07-29 18:18:43 +0200298#define STRING_SERIALNUMBER 10
299
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300300/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200301#define USB_BUFSIZ 256
302
303/*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300304 * This device advertises one configuration, eth_config, unless RNDIS
305 * is enabled (rndis_config) on hardware supporting at least two configs.
306 *
307 * NOTE: Controllers like superh_udc should probably be able to use
308 * an RNDIS-only configuration.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200309 *
310 * FIXME define some higher-powered configurations to make it easier
311 * to recharge batteries ...
312 */
313
314#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300315#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200316
317static struct usb_device_descriptor
318device_desc = {
319 .bLength = sizeof device_desc,
320 .bDescriptorType = USB_DT_DEVICE,
321
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400322 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200323
324 .bDeviceClass = USB_CLASS_COMM,
325 .bDeviceSubClass = 0,
326 .bDeviceProtocol = 0,
327
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400328 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
329 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200330 .iManufacturer = STRING_MANUFACTURER,
331 .iProduct = STRING_PRODUCT,
332 .bNumConfigurations = 1,
333};
334
335static struct usb_otg_descriptor
336otg_descriptor = {
337 .bLength = sizeof otg_descriptor,
338 .bDescriptorType = USB_DT_OTG,
339
340 .bmAttributes = USB_OTG_SRP,
341};
342
343static struct usb_config_descriptor
344eth_config = {
345 .bLength = sizeof eth_config,
346 .bDescriptorType = USB_DT_CONFIG,
347
348 /* compute wTotalLength on the fly */
349 .bNumInterfaces = 2,
350 .bConfigurationValue = DEV_CONFIG_VALUE,
351 .iConfiguration = STRING_CDC,
352 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
353 .bMaxPower = 1,
354};
355
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300356#ifdef CONFIG_USB_ETH_RNDIS
357static struct usb_config_descriptor
358rndis_config = {
359 .bLength = sizeof rndis_config,
360 .bDescriptorType = USB_DT_CONFIG,
361
362 /* compute wTotalLength on the fly */
363 .bNumInterfaces = 2,
364 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
365 .iConfiguration = STRING_RNDIS,
366 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
367 .bMaxPower = 1,
368};
369#endif
370
Remy Bohmer23cd1382009-07-29 18:18:43 +0200371/*
372 * Compared to the simple CDC subset, the full CDC Ethernet model adds
373 * three class descriptors, two interface descriptors, optional status
374 * endpoint. Both have a "data" interface and two bulk endpoints.
375 * There are also differences in how control requests are handled.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300376 *
377 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
378 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
379 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
380 * work around those bugs) are unlikely to ever come from MSFT, you may
381 * wish to avoid using RNDIS.
382 *
383 * MCCI offers an alternative to RNDIS if you need to connect to Windows
384 * but have hardware that can't support CDC Ethernet. We add descriptors
385 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
386 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
387 * get those drivers from MCCI, or bundled with various products.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200388 */
389
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200390#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200391static struct usb_interface_descriptor
392control_intf = {
393 .bLength = sizeof control_intf,
394 .bDescriptorType = USB_DT_INTERFACE,
395
396 .bInterfaceNumber = 0,
397 /* status endpoint is optional; this may be patched later */
398 .bNumEndpoints = 1,
399 .bInterfaceClass = USB_CLASS_COMM,
400 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
401 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
402 .iInterface = STRING_CONTROL,
403};
404#endif
405
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300406#ifdef CONFIG_USB_ETH_RNDIS
407static const struct usb_interface_descriptor
408rndis_control_intf = {
409 .bLength = sizeof rndis_control_intf,
410 .bDescriptorType = USB_DT_INTERFACE,
411
412 .bInterfaceNumber = 0,
413 .bNumEndpoints = 1,
414 .bInterfaceClass = USB_CLASS_COMM,
415 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
416 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
417 .iInterface = STRING_RNDIS_CONTROL,
418};
419#endif
420
Remy Bohmer23cd1382009-07-29 18:18:43 +0200421static const struct usb_cdc_header_desc header_desc = {
422 .bLength = sizeof header_desc,
423 .bDescriptorType = USB_DT_CS_INTERFACE,
424 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
425
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400426 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200427};
428
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200429#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200430
431static const struct usb_cdc_union_desc union_desc = {
432 .bLength = sizeof union_desc,
433 .bDescriptorType = USB_DT_CS_INTERFACE,
434 .bDescriptorSubType = USB_CDC_UNION_TYPE,
435
436 .bMasterInterface0 = 0, /* index of control interface */
437 .bSlaveInterface0 = 1, /* index of DATA interface */
438};
439
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300440#endif /* CDC || RNDIS */
441
442#ifdef CONFIG_USB_ETH_RNDIS
443
444static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
445 .bLength = sizeof call_mgmt_descriptor,
446 .bDescriptorType = USB_DT_CS_INTERFACE,
447 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
448
449 .bmCapabilities = 0x00,
450 .bDataInterface = 0x01,
451};
452
453static const struct usb_cdc_acm_descriptor acm_descriptor = {
454 .bLength = sizeof acm_descriptor,
455 .bDescriptorType = USB_DT_CS_INTERFACE,
456 .bDescriptorSubType = USB_CDC_ACM_TYPE,
457
458 .bmCapabilities = 0x00,
459};
460
461#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200462
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200463#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200464
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400465/*
466 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmer23cd1382009-07-29 18:18:43 +0200467 * ways: data endpoints live in the control interface, there's no data
468 * interface, and it's not used to talk to a cell phone radio.
469 */
470
471static const struct usb_cdc_mdlm_desc mdlm_desc = {
472 .bLength = sizeof mdlm_desc,
473 .bDescriptorType = USB_DT_CS_INTERFACE,
474 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
475
476 .bcdVersion = __constant_cpu_to_le16(0x0100),
477 .bGUID = {
478 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
479 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
480 },
481};
482
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400483/*
484 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmer23cd1382009-07-29 18:18:43 +0200485 * can't really use its struct. All we do here is say that we're using
486 * the submode of "SAFE" which directly matches the CDC Subset.
487 */
488static const u8 mdlm_detail_desc[] = {
489 6,
490 USB_DT_CS_INTERFACE,
491 USB_CDC_MDLM_DETAIL_TYPE,
492
493 0, /* "SAFE" */
494 0, /* network control capabilities (none) */
495 0, /* network data capabilities ("raw" encapsulation) */
496};
497
498#endif
499
Remy Bohmer23cd1382009-07-29 18:18:43 +0200500static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400501 .bLength = sizeof(ether_desc),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200502 .bDescriptorType = USB_DT_CS_INTERFACE,
503 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
504
505 /* this descriptor actually adds value, surprise! */
506 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400507 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
508 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
509 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200510 .bNumberPowerFilters = 0,
511};
512
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200513#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200514
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400515/*
516 * include the status endpoint if we can, even where it's optional.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200517 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
518 * packet, to simplify cancellation; and a big transfer interval, to
519 * waste less bandwidth.
520 *
521 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
522 * if they ignore the connect/disconnect notifications that real aether
523 * can provide. more advanced cdc configurations might want to support
524 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300525 *
526 * RNDIS requires the status endpoint, since it uses that encapsulation
527 * mechanism for its funky RPC scheme.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200528 */
529
530#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
531#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
532
533static struct usb_endpoint_descriptor
534fs_status_desc = {
535 .bLength = USB_DT_ENDPOINT_SIZE,
536 .bDescriptorType = USB_DT_ENDPOINT,
537
538 .bEndpointAddress = USB_DIR_IN,
539 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400540 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200541 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
542};
543#endif
544
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200545#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200546
547/* the default data interface has no endpoints ... */
548
549static const struct usb_interface_descriptor
550data_nop_intf = {
551 .bLength = sizeof data_nop_intf,
552 .bDescriptorType = USB_DT_INTERFACE,
553
554 .bInterfaceNumber = 1,
555 .bAlternateSetting = 0,
556 .bNumEndpoints = 0,
557 .bInterfaceClass = USB_CLASS_CDC_DATA,
558 .bInterfaceSubClass = 0,
559 .bInterfaceProtocol = 0,
560};
561
562/* ... but the "real" data interface has two bulk endpoints */
563
564static const struct usb_interface_descriptor
565data_intf = {
566 .bLength = sizeof data_intf,
567 .bDescriptorType = USB_DT_INTERFACE,
568
569 .bInterfaceNumber = 1,
570 .bAlternateSetting = 1,
571 .bNumEndpoints = 2,
572 .bInterfaceClass = USB_CLASS_CDC_DATA,
573 .bInterfaceSubClass = 0,
574 .bInterfaceProtocol = 0,
575 .iInterface = STRING_DATA,
576};
577
578#endif
579
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300580#ifdef CONFIG_USB_ETH_RNDIS
581
582/* RNDIS doesn't activate by changing to the "real" altsetting */
583
584static const struct usb_interface_descriptor
585rndis_data_intf = {
586 .bLength = sizeof rndis_data_intf,
587 .bDescriptorType = USB_DT_INTERFACE,
588
589 .bInterfaceNumber = 1,
590 .bAlternateSetting = 0,
591 .bNumEndpoints = 2,
592 .bInterfaceClass = USB_CLASS_CDC_DATA,
593 .bInterfaceSubClass = 0,
594 .bInterfaceProtocol = 0,
595 .iInterface = STRING_DATA,
596};
597
598#endif
599
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200600#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200601
602/*
603 * "Simple" CDC-subset option is a simple vendor-neutral model that most
604 * full speed controllers can handle: one interface, two bulk endpoints.
605 *
606 * To assist host side drivers, we fancy it up a bit, and add descriptors
607 * so some host side drivers will understand it as a "SAFE" variant.
608 */
609
610static const struct usb_interface_descriptor
611subset_data_intf = {
612 .bLength = sizeof subset_data_intf,
613 .bDescriptorType = USB_DT_INTERFACE,
614
615 .bInterfaceNumber = 0,
616 .bAlternateSetting = 0,
617 .bNumEndpoints = 2,
618 .bInterfaceClass = USB_CLASS_COMM,
619 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
620 .bInterfaceProtocol = 0,
621 .iInterface = STRING_DATA,
622};
623
624#endif /* SUBSET */
625
Remy Bohmer23cd1382009-07-29 18:18:43 +0200626static struct usb_endpoint_descriptor
627fs_source_desc = {
628 .bLength = USB_DT_ENDPOINT_SIZE,
629 .bDescriptorType = USB_DT_ENDPOINT,
630
631 .bEndpointAddress = USB_DIR_IN,
632 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700633 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200634};
635
636static struct usb_endpoint_descriptor
637fs_sink_desc = {
638 .bLength = USB_DT_ENDPOINT_SIZE,
639 .bDescriptorType = USB_DT_ENDPOINT,
640
641 .bEndpointAddress = USB_DIR_OUT,
642 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kisky43880ce2013-09-25 18:41:04 -0700643 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200644};
645
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400646static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200647 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200648#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200649 /* "cdc" mode descriptors */
650 (struct usb_descriptor_header *) &control_intf,
651 (struct usb_descriptor_header *) &header_desc,
652 (struct usb_descriptor_header *) &union_desc,
653 (struct usb_descriptor_header *) &ether_desc,
654 /* NOTE: status endpoint may need to be removed */
655 (struct usb_descriptor_header *) &fs_status_desc,
656 /* data interface, with altsetting */
657 (struct usb_descriptor_header *) &data_nop_intf,
658 (struct usb_descriptor_header *) &data_intf,
659 (struct usb_descriptor_header *) &fs_source_desc,
660 (struct usb_descriptor_header *) &fs_sink_desc,
661 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200662#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200663};
664
665static inline void fs_subset_descriptors(void)
666{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200667#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200668 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
669 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
670 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
671 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
672 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
673 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
674 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
675 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
676 fs_eth_function[8] = NULL;
677#else
678 fs_eth_function[1] = NULL;
679#endif
680}
681
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300682#ifdef CONFIG_USB_ETH_RNDIS
683static const struct usb_descriptor_header *fs_rndis_function[] = {
684 (struct usb_descriptor_header *) &otg_descriptor,
685 /* control interface matches ACM, not Ethernet */
686 (struct usb_descriptor_header *) &rndis_control_intf,
687 (struct usb_descriptor_header *) &header_desc,
688 (struct usb_descriptor_header *) &call_mgmt_descriptor,
689 (struct usb_descriptor_header *) &acm_descriptor,
690 (struct usb_descriptor_header *) &union_desc,
691 (struct usb_descriptor_header *) &fs_status_desc,
692 /* data interface has no altsetting */
693 (struct usb_descriptor_header *) &rndis_data_intf,
694 (struct usb_descriptor_header *) &fs_source_desc,
695 (struct usb_descriptor_header *) &fs_sink_desc,
696 NULL,
697};
698#endif
699
Remy Bohmer23cd1382009-07-29 18:18:43 +0200700/*
701 * usb 2.0 devices need to expose both high speed and full speed
702 * descriptors, unless they only run at full speed.
703 */
704
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200705#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200706static struct usb_endpoint_descriptor
707hs_status_desc = {
708 .bLength = USB_DT_ENDPOINT_SIZE,
709 .bDescriptorType = USB_DT_ENDPOINT,
710
711 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400712 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200713 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
714};
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200715#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200716
717static struct usb_endpoint_descriptor
718hs_source_desc = {
719 .bLength = USB_DT_ENDPOINT_SIZE,
720 .bDescriptorType = USB_DT_ENDPOINT,
721
722 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400723 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200724};
725
726static struct usb_endpoint_descriptor
727hs_sink_desc = {
728 .bLength = USB_DT_ENDPOINT_SIZE,
729 .bDescriptorType = USB_DT_ENDPOINT,
730
731 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400732 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200733};
734
735static struct usb_qualifier_descriptor
736dev_qualifier = {
737 .bLength = sizeof dev_qualifier,
738 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
739
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400740 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmer23cd1382009-07-29 18:18:43 +0200741 .bDeviceClass = USB_CLASS_COMM,
742
743 .bNumConfigurations = 1,
744};
745
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400746static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200747 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200748#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200749 /* "cdc" mode descriptors */
750 (struct usb_descriptor_header *) &control_intf,
751 (struct usb_descriptor_header *) &header_desc,
752 (struct usb_descriptor_header *) &union_desc,
753 (struct usb_descriptor_header *) &ether_desc,
754 /* NOTE: status endpoint may need to be removed */
755 (struct usb_descriptor_header *) &hs_status_desc,
756 /* data interface, with altsetting */
757 (struct usb_descriptor_header *) &data_nop_intf,
758 (struct usb_descriptor_header *) &data_intf,
759 (struct usb_descriptor_header *) &hs_source_desc,
760 (struct usb_descriptor_header *) &hs_sink_desc,
761 NULL,
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200762#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200763};
764
765static inline void hs_subset_descriptors(void)
766{
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200767#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200768 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
769 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
770 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
771 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
772 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
773 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
774 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
775 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
776 hs_eth_function[8] = NULL;
777#else
778 hs_eth_function[1] = NULL;
779#endif
780}
781
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300782#ifdef CONFIG_USB_ETH_RNDIS
783static const struct usb_descriptor_header *hs_rndis_function[] = {
784 (struct usb_descriptor_header *) &otg_descriptor,
785 /* control interface matches ACM, not Ethernet */
786 (struct usb_descriptor_header *) &rndis_control_intf,
787 (struct usb_descriptor_header *) &header_desc,
788 (struct usb_descriptor_header *) &call_mgmt_descriptor,
789 (struct usb_descriptor_header *) &acm_descriptor,
790 (struct usb_descriptor_header *) &union_desc,
791 (struct usb_descriptor_header *) &hs_status_desc,
792 /* data interface has no altsetting */
793 (struct usb_descriptor_header *) &rndis_data_intf,
794 (struct usb_descriptor_header *) &hs_source_desc,
795 (struct usb_descriptor_header *) &hs_sink_desc,
796 NULL,
797};
798#endif
799
800
Remy Bohmer23cd1382009-07-29 18:18:43 +0200801/* maxpacket and other transfer characteristics vary by speed. */
802static inline struct usb_endpoint_descriptor *
803ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
804 struct usb_endpoint_descriptor *fs)
805{
806 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
807 return hs;
808 return fs;
809}
810
Remy Bohmer23cd1382009-07-29 18:18:43 +0200811/*-------------------------------------------------------------------------*/
812
813/* descriptors that are built on-demand */
814
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400815static char manufacturer[50];
816static char product_desc[40] = DRIVER_DESC;
817static char serial_number[20];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200818
819/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400820static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmer23cd1382009-07-29 18:18:43 +0200821
822/* static strings, in UTF-8 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400823static struct usb_string strings[] = {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200824 { STRING_MANUFACTURER, manufacturer, },
825 { STRING_PRODUCT, product_desc, },
826 { STRING_SERIALNUMBER, serial_number, },
827 { STRING_DATA, "Ethernet Data", },
828 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200829#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +0200830 { STRING_CDC, "CDC Ethernet", },
831 { STRING_CONTROL, "CDC Communications Control", },
832#endif
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200833#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmer23cd1382009-07-29 18:18:43 +0200834 { STRING_SUBSET, "CDC Ethernet Subset", },
835#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300836#ifdef CONFIG_USB_ETH_RNDIS
837 { STRING_RNDIS, "RNDIS", },
838 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
839#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200840 { } /* end of list */
841};
842
843static struct usb_gadget_strings stringtab = {
844 .language = 0x0409, /* en-us */
845 .strings = strings,
846};
847
Remy Bohmer23cd1382009-07-29 18:18:43 +0200848/*============================================================================*/
Joel Fernandes52907592013-09-04 18:55:14 -0500849DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
850
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200851#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes52907592013-09-04 18:55:14 -0500852DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek563aed22012-10-02 17:04:29 +0200853#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200854
Remy Bohmer23cd1382009-07-29 18:18:43 +0200855/*============================================================================*/
856
857/*
858 * one config, two interfaces: control, data.
859 * complications: class descriptors, and an altsetting.
860 */
861static int
862config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
863{
864 int len;
865 const struct usb_config_descriptor *config;
866 const struct usb_descriptor_header **function;
867 int hs = 0;
868
869 if (gadget_is_dualspeed(g)) {
870 hs = (g->speed == USB_SPEED_HIGH);
871 if (type == USB_DT_OTHER_SPEED_CONFIG)
872 hs = !hs;
873 }
874#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
875
876 if (index >= device_desc.bNumConfigurations)
877 return -EINVAL;
878
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300879#ifdef CONFIG_USB_ETH_RNDIS
880 /*
881 * list the RNDIS config first, to make Microsoft's drivers
882 * happy. DOCSIS 1.0 needs this too.
883 */
884 if (device_desc.bNumConfigurations == 2 && index == 0) {
885 config = &rndis_config;
886 function = which_fn(rndis);
887 } else
888#endif
889 {
890 config = &eth_config;
891 function = which_fn(eth);
892 }
Remy Bohmer23cd1382009-07-29 18:18:43 +0200893
894 /* for now, don't advertise srp-only devices */
895 if (!is_otg)
896 function++;
897
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400898 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200899 if (len < 0)
900 return len;
901 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
902 return len;
903}
904
905/*-------------------------------------------------------------------------*/
906
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300907static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400908static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200909
910static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400911set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200912{
913 int result = 0;
914 struct usb_gadget *gadget = dev->gadget;
915
Lukasz Dalek2bb37882012-10-02 17:04:31 +0200916#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300917 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200918 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400919 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmer23cd1382009-07-29 18:18:43 +0200920 &fs_status_desc);
921 dev->status_ep->driver_data = dev;
922
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400923 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200924 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400925 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200926 dev->status_ep->name, result);
927 goto done;
928 }
929 }
930#endif
931
932 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
933 dev->in_ep->driver_data = dev;
934
935 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
936 dev->out_ep->driver_data = dev;
937
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400938 /*
939 * With CDC, the host isn't allowed to use these two data
Remy Bohmer23cd1382009-07-29 18:18:43 +0200940 * endpoints in the default altsetting for the interface.
941 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300942 *
943 * Strictly speaking RNDIS should work the same: activation is
944 * a side effect of setting a packet filter. Deactivation is
945 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmer23cd1382009-07-29 18:18:43 +0200946 */
947 if (!cdc_active(dev)) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400948 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200949 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400950 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200951 dev->in_ep->name, result);
952 goto done;
953 }
954
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400955 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200956 if (result != 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400957 debug("enable %s --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +0200958 dev->out_ep->name, result);
959 goto done;
960 }
961 }
962
963done:
964 if (result == 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400965 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200966
967 /* on error, disable any endpoints */
968 if (result < 0) {
Vitaly Kuzmichevd5292c12010-08-13 17:02:29 +0400969 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400970 (void) usb_ep_disable(dev->status_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200971 dev->status = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400972 (void) usb_ep_disable(dev->in_ep);
973 (void) usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200974 dev->in = NULL;
975 dev->out = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300976 } else if (!cdc_active(dev)) {
977 /*
978 * activate non-CDC configs right away
979 * this isn't strictly according to the RNDIS spec
980 */
981 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200982 }
983
984 /* caller is responsible for cleanup on error */
985 return result;
986}
987
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400988static void eth_reset_config(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200989{
990 if (dev->config == 0)
991 return;
992
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +0400993 debug("%s\n", __func__);
994
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +0300995 rndis_uninit(dev->rndis_config);
996
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400997 /*
998 * disable endpoints, forcing (synchronous) completion of
Remy Bohmer23cd1382009-07-29 18:18:43 +0200999 * pending i/o. then free the requests.
1000 */
1001
1002 if (dev->in) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001003 usb_ep_disable(dev->in_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001004 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001005 usb_ep_free_request(dev->in_ep, dev->tx_req);
1006 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001007 }
1008 }
1009 if (dev->out) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001010 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001011 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001012 usb_ep_free_request(dev->out_ep, dev->rx_req);
1013 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001014 }
1015 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001016 if (dev->status)
1017 usb_ep_disable(dev->status_ep);
1018
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001019 dev->rndis = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001020 dev->cdc_filter = 0;
1021 dev->config = 0;
1022}
1023
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001024/*
1025 * change our operational config. must agree with the code
Remy Bohmer23cd1382009-07-29 18:18:43 +02001026 * that returns config descriptors, and altsetting code.
1027 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001028static int eth_set_config(struct eth_dev *dev, unsigned number,
1029 gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001030{
1031 int result = 0;
1032 struct usb_gadget *gadget = dev->gadget;
1033
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001034 if (gadget_is_sa1100(gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001035 && dev->config
1036 && dev->tx_qlen != 0) {
1037 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001038 error("can't change configurations");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001039 return -ESPIPE;
1040 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001041 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001042
1043 switch (number) {
1044 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001045 result = set_ether_config(dev, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001046 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001047#ifdef CONFIG_USB_ETH_RNDIS
1048 case DEV_RNDIS_CONFIG_VALUE:
1049 dev->rndis = 1;
1050 result = set_ether_config(dev, gfp_flags);
1051 break;
1052#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02001053 default:
1054 result = -EINVAL;
1055 /* FALL THROUGH */
1056 case 0:
1057 break;
1058 }
1059
1060 if (result) {
1061 if (number)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001062 eth_reset_config(dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001063 usb_gadget_vbus_draw(dev->gadget,
1064 gadget_is_otg(dev->gadget) ? 8 : 100);
1065 } else {
1066 char *speed;
1067 unsigned power;
1068
1069 power = 2 * eth_config.bMaxPower;
1070 usb_gadget_vbus_draw(dev->gadget, power);
1071
1072 switch (gadget->speed) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001073 case USB_SPEED_FULL:
1074 speed = "full"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001075#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001076 case USB_SPEED_HIGH:
1077 speed = "high"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001078#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001079 default:
1080 speed = "?"; break;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001081 }
1082
1083 dev->config = number;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001084 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001085 speed, number, power, driver_desc,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001086 rndis_active(dev)
1087 ? "RNDIS"
1088 : (cdc_active(dev)
1089 ? "CDC Ethernet"
Remy Bohmer23cd1382009-07-29 18:18:43 +02001090 : "CDC Ethernet Subset"));
1091 }
1092 return result;
1093}
1094
1095/*-------------------------------------------------------------------------*/
1096
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001097#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001098
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001099/*
1100 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001101 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001102 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmer23cd1382009-07-29 18:18:43 +02001103 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1104 * command mechanism; and only one status request is ever queued.
1105 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001106static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001107{
1108 struct usb_cdc_notification *event = req->buf;
1109 int value = req->status;
1110 struct eth_dev *dev = ep->driver_data;
1111
1112 /* issue the second notification if host reads the first */
1113 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1114 && value == 0) {
1115 __le32 *data = req->buf + sizeof *event;
1116
1117 event->bmRequestType = 0xA1;
1118 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001119 event->wValue = __constant_cpu_to_le16(0);
1120 event->wIndex = __constant_cpu_to_le16(1);
1121 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001122
1123 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001124 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmer23cd1382009-07-29 18:18:43 +02001125
1126 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001127 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001128 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001129 if (value == 0)
1130 return;
1131 } else if (value != -ECONNRESET) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001132 debug("event %02x --> %d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001133 event->bNotificationType, value);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001134 if (event->bNotificationType ==
1135 USB_CDC_NOTIFY_SPEED_CHANGE) {
1136 l_ethdev.network_started = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001137 printf("USB network up!\n");
1138 }
1139 }
1140 req->context = NULL;
1141}
1142
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001143static void issue_start_status(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001144{
1145 struct usb_request *req = dev->stat_req;
1146 struct usb_cdc_notification *event;
1147 int value;
1148
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001149 /*
1150 * flush old status
Remy Bohmer23cd1382009-07-29 18:18:43 +02001151 *
1152 * FIXME ugly idiom, maybe we'd be better with just
1153 * a "cancel the whole queue" primitive since any
1154 * unlink-one primitive has way too many error modes.
1155 * here, we "know" toggle is already clear...
1156 *
1157 * FIXME iff req->context != null just dequeue it
1158 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001159 usb_ep_disable(dev->status_ep);
1160 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001161
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001162 /*
1163 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmer23cd1382009-07-29 18:18:43 +02001164 * a SPEED_CHANGE. could be useful in some configs.
1165 */
1166 event = req->buf;
1167 event->bmRequestType = 0xA1;
1168 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001169 event->wValue = __constant_cpu_to_le16(1); /* connected */
1170 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001171 event->wLength = 0;
1172
1173 req->length = sizeof *event;
1174 req->complete = eth_status_complete;
1175 req->context = dev;
1176
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001177 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001178 if (value < 0)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001179 debug("status buf queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001180}
1181
1182#endif
1183
1184/*-------------------------------------------------------------------------*/
1185
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001186static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001187{
1188 if (req->status || req->actual != req->length)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001189 debug("setup complete --> %d, %d/%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001190 req->status, req->actual, req->length);
1191}
1192
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001193#ifdef CONFIG_USB_ETH_RNDIS
1194
1195static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1196{
1197 if (req->status || req->actual != req->length)
1198 debug("rndis response complete --> %d, %d/%d\n",
1199 req->status, req->actual, req->length);
1200
1201 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1202}
1203
1204static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1205{
1206 struct eth_dev *dev = ep->driver_data;
1207 int status;
1208
1209 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1210 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1211 if (status < 0)
1212 error("%s: rndis parse error %d", __func__, status);
1213}
1214
1215#endif /* RNDIS */
1216
Remy Bohmer23cd1382009-07-29 18:18:43 +02001217/*
1218 * The setup() callback implements all the ep0 functionality that's not
1219 * handled lower down. CDC has a number of less-common features:
1220 *
1221 * - two interfaces: control, and ethernet data
1222 * - Ethernet data interface has two altsettings: default, and active
1223 * - class-specific descriptors for the control interface
1224 * - class-specific control requests
1225 */
1226static int
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001227eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001228{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001229 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001230 struct usb_request *req = dev->req;
1231 int value = -EOPNOTSUPP;
1232 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1233 u16 wValue = le16_to_cpu(ctrl->wValue);
1234 u16 wLength = le16_to_cpu(ctrl->wLength);
1235
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001236 /*
1237 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001238 * while config change events may enable network traffic.
1239 */
1240
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001241 debug("%s\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001242
1243 req->complete = eth_setup_complete;
1244 switch (ctrl->bRequest) {
1245
1246 case USB_REQ_GET_DESCRIPTOR:
1247 if (ctrl->bRequestType != USB_DIR_IN)
1248 break;
1249 switch (wValue >> 8) {
1250
1251 case USB_DT_DEVICE:
Kishon Vijay Abraham I04afd5b2015-02-23 18:40:17 +05301252 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001253 value = min(wLength, (u16) sizeof device_desc);
1254 memcpy(req->buf, &device_desc, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001255 break;
1256 case USB_DT_DEVICE_QUALIFIER:
1257 if (!gadget_is_dualspeed(gadget))
1258 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001259 value = min(wLength, (u16) sizeof dev_qualifier);
1260 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001261 break;
1262
1263 case USB_DT_OTHER_SPEED_CONFIG:
1264 if (!gadget_is_dualspeed(gadget))
1265 break;
1266 /* FALLTHROUGH */
1267 case USB_DT_CONFIG:
1268 value = config_buf(gadget, req->buf,
1269 wValue >> 8,
1270 wValue & 0xff,
1271 gadget_is_otg(gadget));
1272 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001273 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001274 break;
1275
1276 case USB_DT_STRING:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001277 value = usb_gadget_get_string(&stringtab,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001278 wValue & 0xff, req->buf);
1279
1280 if (value >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001281 value = min(wLength, (u16) value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001282
1283 break;
1284 }
1285 break;
1286
1287 case USB_REQ_SET_CONFIGURATION:
1288 if (ctrl->bRequestType != 0)
1289 break;
1290 if (gadget->a_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001291 debug("HNP available\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001292 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001293 debug("HNP needs a different root port\n");
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001294 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001295 break;
1296 case USB_REQ_GET_CONFIGURATION:
1297 if (ctrl->bRequestType != USB_DIR_IN)
1298 break;
1299 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001300 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001301 break;
1302
1303 case USB_REQ_SET_INTERFACE:
1304 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1305 || !dev->config
1306 || wIndex > 1)
1307 break;
1308 if (!cdc_active(dev) && wIndex != 0)
1309 break;
1310
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001311 /*
1312 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001313 * we need to kluge around that interference.
1314 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001315 if (gadget_is_pxa(gadget)) {
1316 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001317 GFP_ATOMIC);
Lukasz Dalek563aed22012-10-02 17:04:29 +02001318 /*
1319 * PXA25x driver use non-CDC ethernet gadget.
1320 * But only _CDC and _RNDIS code can signalize
1321 * that network is working. So we signalize it
1322 * here.
1323 */
1324 l_ethdev.network_started = 1;
1325 debug("USB network up!\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001326 goto done_set_intf;
1327 }
1328
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001329#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001330 switch (wIndex) {
1331 case 0: /* control/master intf */
1332 if (wValue != 0)
1333 break;
1334 if (dev->status) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001335 usb_ep_disable(dev->status_ep);
1336 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001337 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001338
Remy Bohmer23cd1382009-07-29 18:18:43 +02001339 value = 0;
1340 break;
1341 case 1: /* data intf */
1342 if (wValue > 1)
1343 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001344 usb_ep_disable(dev->in_ep);
1345 usb_ep_disable(dev->out_ep);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001346
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001347 /*
1348 * CDC requires the data transfers not be done from
Remy Bohmer23cd1382009-07-29 18:18:43 +02001349 * the default interface setting ... also, setting
1350 * the non-default interface resets filters etc.
1351 */
1352 if (wValue == 1) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001353 if (!cdc_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001354 break;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001355 usb_ep_enable(dev->in_ep, dev->in);
1356 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001357 dev->cdc_filter = DEFAULT_FILTER;
1358 if (dev->status)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001359 issue_start_status(dev);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001360 eth_start(dev, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001361 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001362 value = 0;
1363 break;
1364 }
1365#else
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001366 /*
1367 * FIXME this is wrong, as is the assumption that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001368 * all non-PXA hardware talks real CDC ...
1369 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001370 debug("set_interface ignored!\n");
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001371#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001372
1373done_set_intf:
1374 break;
1375 case USB_REQ_GET_INTERFACE:
1376 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1377 || !dev->config
1378 || wIndex > 1)
1379 break;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001380 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001381 break;
1382
1383 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001384 if (rndis_active(dev) || wIndex != 1)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001385 *(u8 *)req->buf = 0;
1386 else {
1387 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1388 /* carrier always ok ...*/
1389 *(u8 *)req->buf = 1 ;
1390 }
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001391 value = min(wLength, (u16) 1);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001392 break;
1393
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001394#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001395 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001396 /*
1397 * see 6.2.30: no data, wIndex = interface,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001398 * wValue = packet filter bitmap
1399 */
1400 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1401 || !cdc_active(dev)
1402 || wLength != 0
1403 || wIndex > 1)
1404 break;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001405 debug("packet filter %02x\n", wValue);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001406 dev->cdc_filter = wValue;
1407 value = 0;
1408 break;
1409
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001410 /*
1411 * and potentially:
Remy Bohmer23cd1382009-07-29 18:18:43 +02001412 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1413 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1414 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1415 * case USB_CDC_GET_ETHERNET_STATISTIC:
1416 */
1417
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001418#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001419
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001420#ifdef CONFIG_USB_ETH_RNDIS
1421 /*
1422 * RNDIS uses the CDC command encapsulation mechanism to implement
1423 * an RPC scheme, with much getting/setting of attributes by OID.
1424 */
1425 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1426 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1427 || !rndis_active(dev)
1428 || wLength > USB_BUFSIZ
1429 || wValue
1430 || rndis_control_intf.bInterfaceNumber
1431 != wIndex)
1432 break;
1433 /* read the request, then process it */
1434 value = wLength;
1435 req->complete = rndis_command_complete;
1436 /* later, rndis_control_ack () sends a notification */
1437 break;
1438
1439 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1440 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1441 == ctrl->bRequestType
1442 && rndis_active(dev)
1443 /* && wLength >= 0x0400 */
1444 && !wValue
1445 && rndis_control_intf.bInterfaceNumber
1446 == wIndex) {
1447 u8 *buf;
1448 u32 n;
1449
1450 /* return the result */
1451 buf = rndis_get_next_response(dev->rndis_config, &n);
1452 if (buf) {
1453 memcpy(req->buf, buf, n);
1454 req->complete = rndis_response_complete;
1455 rndis_free_response(dev->rndis_config, buf);
1456 value = n;
1457 }
1458 /* else stalls ... spec says to avoid that */
1459 }
1460 break;
1461#endif /* RNDIS */
1462
Remy Bohmer23cd1382009-07-29 18:18:43 +02001463 default:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001464 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02001465 ctrl->bRequestType, ctrl->bRequest,
1466 wValue, wIndex, wLength);
1467 }
1468
1469 /* respond with data transfer before status phase? */
1470 if (value >= 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001471 debug("respond with data transfer before status phase\n");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001472 req->length = value;
1473 req->zero = value < wLength
1474 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001475 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001476 if (value < 0) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001477 debug("ep_queue --> %d\n", value);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001478 req->status = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001479 eth_setup_complete(gadget->ep0, req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001480 }
1481 }
1482
1483 /* host either stalls (value < 0) or reports success */
1484 return value;
1485}
1486
Remy Bohmer23cd1382009-07-29 18:18:43 +02001487/*-------------------------------------------------------------------------*/
1488
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001489static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001490
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001491static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmer23cd1382009-07-29 18:18:43 +02001492 gfp_t gfp_flags)
1493{
1494 int retval = -ENOMEM;
1495 size_t size;
1496
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001497 /*
1498 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001499 * Normally we use the USB "terminate on short read" convention;
1500 * so allow up to (N*maxpacket), since that memory is normally
1501 * already allocated. Some hardware doesn't deal well with short
1502 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1503 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001504 *
1505 * RNDIS uses internal framing, and explicitly allows senders to
1506 * pad to end-of-packet. That's potentially nice for speed,
1507 * but means receivers can't recover synch on their own.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001508 */
1509
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001510 debug("%s\n", __func__);
Troy Kisky71fc5f92013-09-25 18:41:05 -07001511 if (!req)
1512 return -EINVAL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001513
1514 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1515 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001516 if (rndis_active(dev))
1517 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001518 size -= size % dev->out_ep->maxpacket;
1519
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001520 /*
1521 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001522 * but on at least one, checksumming fails otherwise. Note:
1523 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmer23cd1382009-07-29 18:18:43 +02001524 */
1525
Joe Hershberger1fd92db2015-04-08 01:41:06 -05001526 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmer23cd1382009-07-29 18:18:43 +02001527 req->length = size;
1528 req->complete = rx_complete;
1529
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001530 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001531
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001532 if (retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001533 error("rx submit --> %d", retval);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001534
Remy Bohmer23cd1382009-07-29 18:18:43 +02001535 return retval;
1536}
1537
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001538static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001539{
1540 struct eth_dev *dev = ep->driver_data;
1541
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001542 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001543 switch (req->status) {
1544 /* normal completion */
1545 case 0:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001546 if (rndis_active(dev)) {
1547 /* we know MaxPacketsPerTransfer == 1 here */
1548 int length = rndis_rm_hdr(req->buf, req->actual);
1549 if (length < 0)
1550 goto length_err;
1551 req->length -= length;
1552 req->actual -= length;
1553 }
1554 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1555length_err:
1556 dev->stats.rx_errors++;
1557 dev->stats.rx_length_errors++;
1558 debug("rx length %d\n", req->length);
1559 break;
1560 }
1561
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001562 dev->stats.rx_packets++;
1563 dev->stats.rx_bytes += req->length;
1564 break;
1565
1566 /* software-driven interface shutdown */
1567 case -ECONNRESET: /* unlink */
1568 case -ESHUTDOWN: /* disconnect etc */
1569 /* for hardware automagic (such as pxa) */
1570 case -ECONNABORTED: /* endpoint reset */
1571 break;
1572
1573 /* data overrun */
1574 case -EOVERFLOW:
1575 dev->stats.rx_over_errors++;
1576 /* FALLTHROUGH */
1577 default:
1578 dev->stats.rx_errors++;
1579 break;
1580 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001581
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001582 packet_received = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001583}
1584
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001585static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001586{
1587
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001588 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001589
1590 if (!dev->tx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001591 goto fail1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001592
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001593 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001594
1595 if (!dev->rx_req)
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001596 goto fail2;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001597
1598 return 0;
1599
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04001600fail2:
1601 usb_ep_free_request(dev->in_ep, dev->tx_req);
1602fail1:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001603 error("can't alloc requests");
Remy Bohmer23cd1382009-07-29 18:18:43 +02001604 return -1;
1605}
1606
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001607static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001608{
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001609 struct eth_dev *dev = ep->driver_data;
1610
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001611 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichevc85d70e2011-02-11 18:18:32 +03001612 switch (req->status) {
1613 default:
1614 dev->stats.tx_errors++;
1615 debug("tx err %d\n", req->status);
1616 /* FALLTHROUGH */
1617 case -ECONNRESET: /* unlink */
1618 case -ESHUTDOWN: /* disconnect etc */
1619 break;
1620 case 0:
1621 dev->stats.tx_bytes += req->length;
1622 }
1623 dev->stats.tx_packets++;
1624
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001625 packet_sent = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001626}
1627
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001628static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001629{
1630 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001631 if (subset_active(dev))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001632 return 1;
1633 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1634}
1635
1636#if 0
1637static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1638{
1639 struct eth_dev *dev = netdev_priv(net);
1640 int length = skb->len;
1641 int retval;
1642 struct usb_request *req = NULL;
1643 unsigned long flags;
1644
1645 /* apply outgoing CDC or RNDIS filters */
1646 if (!eth_is_promisc (dev)) {
1647 u8 *dest = skb->data;
1648
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001649 if (is_multicast_ethaddr(dest)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02001650 u16 type;
1651
1652 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1653 * SET_ETHERNET_MULTICAST_FILTERS requests
1654 */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001655 if (is_broadcast_ethaddr(dest))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001656 type = USB_CDC_PACKET_TYPE_BROADCAST;
1657 else
1658 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1659 if (!(dev->cdc_filter & type)) {
1660 dev_kfree_skb_any (skb);
1661 return 0;
1662 }
1663 }
1664 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1665 }
1666
1667 spin_lock_irqsave(&dev->req_lock, flags);
1668 /*
1669 * this freelist can be empty if an interrupt triggered disconnect()
1670 * and reconfigured the gadget (shutting down this queue) after the
1671 * network stack decided to xmit but before we got the spinlock.
1672 */
1673 if (list_empty(&dev->tx_reqs)) {
1674 spin_unlock_irqrestore(&dev->req_lock, flags);
1675 return 1;
1676 }
1677
1678 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1679 list_del (&req->list);
1680
1681 /* temporarily stop TX queue when the freelist empties */
1682 if (list_empty (&dev->tx_reqs))
1683 netif_stop_queue (net);
1684 spin_unlock_irqrestore(&dev->req_lock, flags);
1685
1686 /* no buffer copies needed, unless the network stack did it
1687 * or the hardware can't use skb buffers.
1688 * or there's not enough space for any RNDIS headers we need
1689 */
1690 if (rndis_active(dev)) {
1691 struct sk_buff *skb_rndis;
1692
1693 skb_rndis = skb_realloc_headroom (skb,
1694 sizeof (struct rndis_packet_msg_type));
1695 if (!skb_rndis)
1696 goto drop;
1697
1698 dev_kfree_skb_any (skb);
1699 skb = skb_rndis;
1700 rndis_add_hdr (skb);
1701 length = skb->len;
1702 }
1703 req->buf = skb->data;
1704 req->context = skb;
1705 req->complete = tx_complete;
1706
1707 /* use zlp framing on tx for strict CDC-Ether conformance,
1708 * though any robust network rx path ignores extra padding.
1709 * and some hardware doesn't like to write zlps.
1710 */
1711 req->zero = 1;
1712 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1713 length++;
1714
1715 req->length = length;
1716
1717 /* throttle highspeed IRQ rate back slightly */
1718 if (gadget_is_dualspeed(dev->gadget))
1719 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1720 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1721 : 0;
1722
1723 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1724 switch (retval) {
1725 default:
1726 DEBUG (dev, "tx queue err %d\n", retval);
1727 break;
1728 case 0:
1729 net->trans_start = jiffies;
1730 atomic_inc (&dev->tx_qlen);
1731 }
1732
1733 if (retval) {
1734drop:
1735 dev->stats.tx_dropped++;
1736 dev_kfree_skb_any (skb);
1737 spin_lock_irqsave(&dev->req_lock, flags);
1738 if (list_empty (&dev->tx_reqs))
1739 netif_start_queue (net);
1740 list_add (&req->list, &dev->tx_reqs);
1741 spin_unlock_irqrestore(&dev->req_lock, flags);
1742 }
1743 return 0;
1744}
1745
1746/*-------------------------------------------------------------------------*/
1747#endif
1748
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001749static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001750{
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001751 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001752
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04001753 debug("%s...\n", __func__);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001754 rndis_deregister(dev->rndis_config);
1755 rndis_exit();
Remy Bohmer23cd1382009-07-29 18:18:43 +02001756
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001757 /* we've already been disconnected ... no i/o is active */
1758 if (dev->req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001759 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev0129e322010-08-13 17:00:16 +04001760 dev->req = NULL;
1761 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02001762 if (dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001763 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001764 dev->stat_req = NULL;
1765 }
1766
1767 if (dev->tx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001768 usb_ep_free_request(dev->in_ep, dev->tx_req);
1769 dev->tx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001770 }
1771
1772 if (dev->rx_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001773 usb_ep_free_request(dev->out_ep, dev->rx_req);
1774 dev->rx_req = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001775 }
1776
1777/* unregister_netdev (dev->net);*/
1778/* free_netdev(dev->net);*/
1779
Lei Wen988ee3e2010-12-01 23:43:43 +08001780 dev->gadget = NULL;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001781 set_gadget_data(gadget, NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001782}
1783
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001784static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001785{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001786 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001787 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmer23cd1382009-07-29 18:18:43 +02001788}
1789
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001790static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001791{
1792 /* Not used */
1793}
1794
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001795static void eth_resume(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001796{
1797 /* Not used */
1798}
1799
1800/*-------------------------------------------------------------------------*/
1801
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001802#ifdef CONFIG_USB_ETH_RNDIS
1803
1804/*
1805 * The interrupt endpoint is used in RNDIS to notify the host when messages
1806 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1807 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1808 * REMOTE_NDIS_KEEPALIVE_MSG.
1809 *
1810 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1811 * normally just one notification will be queued.
1812 */
1813
1814static void rndis_control_ack_complete(struct usb_ep *ep,
1815 struct usb_request *req)
1816{
1817 struct eth_dev *dev = ep->driver_data;
1818
1819 debug("%s...\n", __func__);
1820 if (req->status || req->actual != req->length)
1821 debug("rndis control ack complete --> %d, %d/%d\n",
1822 req->status, req->actual, req->length);
1823
1824 if (!l_ethdev.network_started) {
1825 if (rndis_get_state(dev->rndis_config)
1826 == RNDIS_DATA_INITIALIZED) {
1827 l_ethdev.network_started = 1;
1828 printf("USB RNDIS network up!\n");
1829 }
1830 }
1831
1832 req->context = NULL;
1833
1834 if (req != dev->stat_req)
1835 usb_ep_free_request(ep, req);
1836}
1837
1838static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1839
1840static int rndis_control_ack(struct eth_device *net)
1841{
1842 struct eth_dev *dev = &l_ethdev;
1843 int length;
1844 struct usb_request *resp = dev->stat_req;
1845
1846 /* in case RNDIS calls this after disconnect */
1847 if (!dev->status) {
1848 debug("status ENODEV\n");
1849 return -ENODEV;
1850 }
1851
1852 /* in case queue length > 1 */
1853 if (resp->context) {
1854 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1855 if (!resp)
1856 return -ENOMEM;
1857 resp->buf = rndis_resp_buf;
1858 }
1859
1860 /*
1861 * Send RNDIS RESPONSE_AVAILABLE notification;
1862 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1863 */
1864 resp->length = 8;
1865 resp->complete = rndis_control_ack_complete;
1866 resp->context = dev;
1867
1868 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1869 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1870
1871 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1872 if (length < 0) {
1873 resp->status = 0;
1874 rndis_control_ack_complete(dev->status_ep, resp);
1875 }
1876
1877 return 0;
1878}
1879
1880#else
1881
1882#define rndis_control_ack NULL
1883
1884#endif /* RNDIS */
1885
1886static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1887{
1888 if (rndis_active(dev)) {
1889 rndis_set_param_medium(dev->rndis_config,
1890 NDIS_MEDIUM_802_3,
1891 BITRATE(dev->gadget)/100);
1892 rndis_signal_connect(dev->rndis_config);
1893 }
1894}
1895
1896static int eth_stop(struct eth_dev *dev)
1897{
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001898#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1899 unsigned long ts;
1900 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1901#endif
1902
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001903 if (rndis_active(dev)) {
1904 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1905 rndis_signal_disconnect(dev->rndis_config);
1906
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001907#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1908 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1909 ts = get_timer(0);
1910 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05301911 usb_gadget_handle_interrupts(0);
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03001912#endif
1913
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001914 rndis_uninit(dev->rndis_config);
1915 dev->rndis = 0;
1916 }
1917
1918 return 0;
1919}
1920
1921/*-------------------------------------------------------------------------*/
1922
Remy Bohmer23cd1382009-07-29 18:18:43 +02001923static int is_eth_addr_valid(char *str)
1924{
1925 if (strlen(str) == 17) {
1926 int i;
1927 char *p, *q;
1928 uchar ea[6];
1929
1930 /* see if it looks like an ethernet address */
1931
1932 p = str;
1933
1934 for (i = 0; i < 6; i++) {
1935 char term = (i == 5 ? '\0' : ':');
1936
1937 ea[i] = simple_strtol(p, &q, 16);
1938
1939 if ((q - p) != 2 || *q++ != term)
1940 break;
1941
1942 p = q;
1943 }
1944
Tom Rini57a87a22012-10-31 13:30:41 +00001945 /* Now check the contents. */
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001946 return is_valid_ethaddr(ea);
Remy Bohmer23cd1382009-07-29 18:18:43 +02001947 }
1948 return 0;
1949}
1950
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001951static u8 nibble(unsigned char c)
Remy Bohmer23cd1382009-07-29 18:18:43 +02001952{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001953 if (likely(isdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001954 return c - '0';
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001955 c = toupper(c);
1956 if (likely(isxdigit(c)))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001957 return 10 + c - 'A';
1958 return 0;
1959}
1960
1961static int get_ether_addr(const char *str, u8 *dev_addr)
1962{
1963 if (str) {
1964 unsigned i;
1965
1966 for (i = 0; i < 6; i++) {
1967 unsigned char num;
1968
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001969 if ((*str == '.') || (*str == ':'))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001970 str++;
1971 num = nibble(*str++) << 4;
1972 num |= (nibble(*str++));
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001973 dev_addr[i] = num;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001974 }
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001975 if (is_valid_ethaddr(dev_addr))
Remy Bohmer23cd1382009-07-29 18:18:43 +02001976 return 0;
1977 }
1978 return 1;
1979}
1980
1981static int eth_bind(struct usb_gadget *gadget)
1982{
1983 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001984 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001985 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001986 int status = -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02001987 int gcnum;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001988 u8 tmp[7];
Remy Bohmer23cd1382009-07-29 18:18:43 +02001989
1990 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02001991#ifndef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02001992 cdc = 0;
1993#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03001994#ifndef CONFIG_USB_ETH_RNDIS
1995 rndis = 0;
1996#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04001997 /*
1998 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmer23cd1382009-07-29 18:18:43 +02001999 * standard protocol is _strongly_ preferred for interop purposes.
2000 * (By everyone except Microsoft.)
2001 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002002 if (gadget_is_pxa(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002003 /* pxa doesn't support altsettings */
2004 cdc = 0;
2005 } else if (gadget_is_musbhdrc(gadget)) {
2006 /* reduce tx dma overhead by avoiding special cases */
2007 zlp = 0;
2008 } else if (gadget_is_sh(gadget)) {
2009 /* sh doesn't support multiple interfaces or configs */
2010 cdc = 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002011 rndis = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002012 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002013 /* hardware can't write zlps */
2014 zlp = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002015 /*
2016 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmer23cd1382009-07-29 18:18:43 +02002017 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2018 */
2019 cdc = 0;
2020 }
2021
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002022 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002023 if (gcnum >= 0)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002024 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002025 else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002026 /*
2027 * can't assume CDC works. don't want to default to
Remy Bohmer23cd1382009-07-29 18:18:43 +02002028 * anything less functional on CDC-capable hardware,
2029 * so we fail in this case.
2030 */
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002031 error("controller '%s' not recognized",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002032 gadget->name);
2033 return -ENODEV;
2034 }
2035
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002036 /*
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002037 * If there's an RNDIS configuration, that's what Windows wants to
2038 * be using ... so use these product IDs here and in the "linux.inf"
2039 * needed to install MSFT drivers. Current Linux kernels will use
2040 * the second configuration if it's CDC Ethernet, and need some help
2041 * to choose the right configuration otherwise.
2042 */
2043 if (rndis) {
2044#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
2045 device_desc.idVendor =
2046 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
2047 device_desc.idProduct =
2048 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2049#else
2050 device_desc.idVendor =
2051 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2052 device_desc.idProduct =
2053 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2054#endif
2055 sprintf(product_desc, "RNDIS/%s", driver_desc);
2056
2057 /*
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002058 * CDC subset ... recognized by Linux since 2.4.10, but Windows
Remy Bohmer23cd1382009-07-29 18:18:43 +02002059 * drivers aren't widely available. (That may be improved by
2060 * supporting one submode of the "SAFE" variant of MDLM.)
2061 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002062 } else {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002063#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002064 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2065 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2066#else
2067 if (!cdc) {
2068 device_desc.idVendor =
2069 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2070 device_desc.idProduct =
2071 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2072 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002073#endif
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002074 }
2075 /* support optional vendor/distro customization */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002076 if (bcdDevice)
2077 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2078 if (iManufacturer)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002079 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002080 if (iProduct)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002081 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002082 if (iSerialNumber) {
2083 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev2e12abe2010-08-13 17:00:45 +04002084 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002085 }
2086
2087 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002088 usb_ep_autoconfig_reset(gadget);
2089 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002090 if (!in_ep) {
2091autoconf_fail:
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002092 error("can't autoconfigure on %s\n",
Remy Bohmer23cd1382009-07-29 18:18:43 +02002093 gadget->name);
2094 return -ENODEV;
2095 }
2096 in_ep->driver_data = in_ep; /* claim */
2097
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002098 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002099 if (!out_ep)
2100 goto autoconf_fail;
2101 out_ep->driver_data = out_ep; /* claim */
2102
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002103#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002104 /*
2105 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmer23cd1382009-07-29 18:18:43 +02002106 * Since some hosts expect one, try to allocate one anyway.
2107 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002108 if (cdc || rndis) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002109 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002110 if (status_ep) {
2111 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002112 } else if (rndis) {
2113 error("can't run RNDIS on %s", gadget->name);
2114 return -ENODEV;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002115#ifdef CONFIG_USB_ETH_CDC
Remy Bohmer23cd1382009-07-29 18:18:43 +02002116 } else if (cdc) {
2117 control_intf.bNumEndpoints = 0;
2118 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002119#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002120 }
2121 }
2122#endif
2123
2124 /* one config: cdc, else minimal subset */
2125 if (!cdc) {
2126 eth_config.bNumInterfaces = 1;
2127 eth_config.iConfiguration = STRING_SUBSET;
2128
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002129 /*
2130 * use functions to set these up, in case we're built to work
Remy Bohmer23cd1382009-07-29 18:18:43 +02002131 * with multiple controllers and must override CDC Ethernet.
2132 */
2133 fs_subset_descriptors();
2134 hs_subset_descriptors();
2135 }
2136
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002137 usb_gadget_set_selfpowered(gadget);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002138
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002139 /* For now RNDIS is always a second config */
2140 if (rndis)
2141 device_desc.bNumConfigurations = 2;
2142
Remy Bohmer23cd1382009-07-29 18:18:43 +02002143 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002144 if (rndis)
2145 dev_qualifier.bNumConfigurations = 2;
2146 else if (!cdc)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002147 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2148
2149 /* assumes ep0 uses the same value for both speeds ... */
2150 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2151
2152 /* and that all endpoints are dual-speed */
2153 hs_source_desc.bEndpointAddress =
2154 fs_source_desc.bEndpointAddress;
2155 hs_sink_desc.bEndpointAddress =
2156 fs_sink_desc.bEndpointAddress;
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002157#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002158 if (status_ep)
2159 hs_status_desc.bEndpointAddress =
2160 fs_status_desc.bEndpointAddress;
2161#endif
2162 }
2163
2164 if (gadget_is_otg(gadget)) {
2165 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2166 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2167 eth_config.bMaxPower = 4;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002168#ifdef CONFIG_USB_ETH_RNDIS
2169 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2170 rndis_config.bMaxPower = 4;
2171#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002172 }
2173
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002174
2175 /* network device setup */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002176 dev->net = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002177
2178 dev->cdc = cdc;
2179 dev->zlp = zlp;
2180
2181 dev->in_ep = in_ep;
2182 dev->out_ep = out_ep;
2183 dev->status_ep = status_ep;
2184
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002185 /*
2186 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002187 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmer23cd1382009-07-29 18:18:43 +02002188 * ends up in a persistent config database. It's not clear if
2189 * host side code for the SAFE thing cares -- its original BLAN
2190 * thing didn't, Sharp never assigned those addresses on Zaurii.
2191 */
2192 get_ether_addr(dev_addr, dev->net->enetaddr);
2193
2194 memset(tmp, 0, sizeof(tmp));
2195 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2196
2197 get_ether_addr(host_addr, dev->host_mac);
2198
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002199 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2200 dev->host_mac[0], dev->host_mac[1],
2201 dev->host_mac[2], dev->host_mac[3],
2202 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002203
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002204 if (rndis) {
2205 status = rndis_init();
2206 if (status < 0) {
2207 error("can't init RNDIS, %d", status);
2208 goto fail;
2209 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002210 }
2211
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002212 /*
2213 * use PKTSIZE (or aligned... from u-boot) and set
2214 * wMaxSegmentSize accordingly
2215 */
Remy Bohmer23cd1382009-07-29 18:18:43 +02002216 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2217
2218 /* preallocate control message data and buffer */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002219 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002220 if (!dev->req)
2221 goto fail;
2222 dev->req->buf = control_req;
2223 dev->req->complete = eth_setup_complete;
2224
2225 /* ... and maybe likewise for status transfer */
Lukasz Dalek2bb37882012-10-02 17:04:31 +02002226#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002227 if (dev->status_ep) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002228 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2229 GFP_KERNEL);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002230 if (!dev->stat_req) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002231 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002232
2233 goto fail;
2234 }
Vitaly Kuzmichevdf559c12010-08-13 17:01:06 +04002235 dev->stat_req->buf = status_req;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002236 dev->stat_req->context = NULL;
2237 }
2238#endif
2239
2240 /* finish hookup to lower layer ... */
2241 dev->gadget = gadget;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002242 set_gadget_data(gadget, dev);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002243 gadget->ep0->driver_data = dev;
2244
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002245 /*
2246 * two kinds of host-initiated state changes:
Remy Bohmer23cd1382009-07-29 18:18:43 +02002247 * - iff DATA transfer is active, carrier is "on"
2248 * - tx queueing enabled if open *and* carrier is "on"
2249 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002250
2251 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2252 out_ep->name, in_ep->name,
2253 status_ep ? " STATUS " : "",
2254 status_ep ? status_ep->name : ""
2255 );
2256 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2257 dev->net->enetaddr[0], dev->net->enetaddr[1],
2258 dev->net->enetaddr[2], dev->net->enetaddr[3],
2259 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2260
2261 if (cdc || rndis)
2262 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2263 dev->host_mac[0], dev->host_mac[1],
2264 dev->host_mac[2], dev->host_mac[3],
2265 dev->host_mac[4], dev->host_mac[5]);
2266
2267 if (rndis) {
2268 u32 vendorID = 0;
2269
2270 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2271
2272 dev->rndis_config = rndis_register(rndis_control_ack);
2273 if (dev->rndis_config < 0) {
2274fail0:
2275 eth_unbind(gadget);
2276 debug("RNDIS setup failed\n");
2277 status = -ENODEV;
2278 goto fail;
2279 }
2280
2281 /* these set up a lot of the OIDs that RNDIS needs */
2282 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2283 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2284 &dev->stats, &dev->cdc_filter))
2285 goto fail0;
2286 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2287 manufacturer))
2288 goto fail0;
2289 if (rndis_set_param_medium(dev->rndis_config,
2290 NDIS_MEDIUM_802_3, 0))
2291 goto fail0;
2292 printf("RNDIS ready\n");
2293 }
Remy Bohmer23cd1382009-07-29 18:18:43 +02002294 return 0;
2295
2296fail:
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002297 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002298 eth_unbind(gadget);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002299 return status;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002300}
2301
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002302/*-------------------------------------------------------------------------*/
2303
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002304static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002305{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002306 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002307 struct usb_gadget *gadget;
2308 unsigned long ts;
2309 unsigned long timeout = USB_CONNECT_TIMEOUT;
2310
2311 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002312 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002313 goto fail;
2314 }
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002315
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302316 board_usb_init(0, USB_INIT_DEVICE);
2317
Vitaly Kuzmichev58939fc2010-12-28 16:59:32 +03002318 /* Configure default mac-addresses for the USB ethernet device */
2319#ifdef CONFIG_USBNET_DEV_ADDR
2320 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2321#endif
2322#ifdef CONFIG_USBNET_HOST_ADDR
2323 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2324#endif
2325 /* Check if the user overruled the MAC addresses */
2326 if (getenv("usbnet_devaddr"))
2327 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2328 sizeof(dev_addr));
2329
2330 if (getenv("usbnet_hostaddr"))
2331 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2332 sizeof(host_addr));
2333
2334 if (!is_eth_addr_valid(dev_addr)) {
2335 error("Need valid 'usbnet_devaddr' to be set");
2336 goto fail;
2337 }
2338 if (!is_eth_addr_valid(host_addr)) {
2339 error("Need valid 'usbnet_hostaddr' to be set");
2340 goto fail;
2341 }
2342
Lei Wen988ee3e2010-12-01 23:43:43 +08002343 if (usb_gadget_register_driver(&eth_driver) < 0)
2344 goto fail;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002345
2346 dev->network_started = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002347
2348 packet_received = 0;
2349 packet_sent = 0;
2350
2351 gadget = dev->gadget;
2352 usb_gadget_connect(gadget);
2353
2354 if (getenv("cdc_connect_timeout"))
2355 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2356 NULL, 10) * CONFIG_SYS_HZ;
2357 ts = get_timer(0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002358 while (!l_ethdev.network_started) {
Remy Bohmer23cd1382009-07-29 18:18:43 +02002359 /* Handle control-c and timeouts */
2360 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002361 error("The remote end did not respond in time.");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002362 goto fail;
2363 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302364 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002365 }
2366
Vitaly Kuzmichev98fae972010-09-22 13:13:56 +04002367 packet_received = 0;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002368 rx_submit(dev, dev->rx_req, 0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002369 return 0;
2370fail:
2371 return -1;
2372}
2373
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002374static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002375{
2376 int retval;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002377 void *rndis_pkt = NULL;
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002378 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002379 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002380 unsigned long ts;
2381 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002382
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002383 debug("%s:...\n", __func__);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002384
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002385 /* new buffer is needed to include RNDIS header */
2386 if (rndis_active(dev)) {
2387 rndis_pkt = malloc(length +
2388 sizeof(struct rndis_packet_msg_type));
2389 if (!rndis_pkt) {
2390 error("No memory to alloc RNDIS packet");
2391 goto drop;
2392 }
2393 rndis_add_hdr(rndis_pkt, length);
2394 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002395 packet, length);
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002396 packet = rndis_pkt;
2397 length += sizeof(struct rndis_packet_msg_type);
2398 }
Joe Hershberger10cbe3b2012-05-22 18:36:19 +00002399 req->buf = packet;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002400 req->context = NULL;
2401 req->complete = tx_complete;
2402
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002403 /*
2404 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002405 * though any robust network rx path ignores extra padding.
2406 * and some hardware doesn't like to write zlps.
2407 */
2408 req->zero = 1;
2409 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2410 length++;
2411
2412 req->length = length;
2413#if 0
2414 /* throttle highspeed IRQ rate back slightly */
2415 if (gadget_is_dualspeed(dev->gadget))
2416 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2417 ? ((dev->tx_qlen % qmult) != 0) : 0;
2418#endif
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002419 dev->tx_qlen = 1;
2420 ts = get_timer(0);
2421 packet_sent = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002422
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002423 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002424
2425 if (!retval)
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002426 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002427 while (!packet_sent) {
2428 if (get_timer(ts) > timeout) {
2429 printf("timeout sending packets to usb ethernet\n");
2430 return -1;
2431 }
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302432 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002433 }
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002434 if (rndis_pkt)
2435 free(rndis_pkt);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002436
2437 return 0;
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002438drop:
2439 dev->stats.tx_dropped++;
2440 return -ENOMEM;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002441}
2442
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002443static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002444{
2445 struct eth_dev *dev = &l_ethdev;
2446
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302447 usb_gadget_handle_interrupts(0);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002448
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002449 if (packet_received) {
2450 debug("%s: packet received\n", __func__);
2451 if (dev->rx_req) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -05002452 net_process_received_packet(net_rx_packets[0],
2453 dev->rx_req->length);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002454 packet_received = 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002455
Vitaly Kuzmichevac5d32d2010-09-22 13:13:55 +04002456 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002457 } else
2458 error("dev->rx_req invalid");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002459 }
2460 return 0;
2461}
2462
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002463void usb_eth_halt(struct eth_device *netdev)
Remy Bohmer23cd1382009-07-29 18:18:43 +02002464{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002465 struct eth_dev *dev = &l_ethdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002466
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002467 if (!netdev) {
Vitaly Kuzmichev7de73182010-08-13 16:57:51 +04002468 error("received NULL ptr");
Remy Bohmer23cd1382009-07-29 18:18:43 +02002469 return;
2470 }
2471
Lei Wen988ee3e2010-12-01 23:43:43 +08002472 /* If the gadget not registered, simple return */
2473 if (!dev->gadget)
2474 return;
2475
Vitaly Kuzmicheve4ae6662011-02-11 18:18:35 +03002476 /*
2477 * Some USB controllers may need additional deinitialization here
2478 * before dropping pull-up (also due to hardware issues).
2479 * For example: unhandled interrupt with status stage started may
2480 * bring the controller to fully broken state (until board reset).
2481 * There are some variants to debug and fix such cases:
2482 * 1) In the case of RNDIS connection eth_stop can perform additional
2483 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2484 * 2) 'pullup' callback in your UDC driver can be improved to perform
2485 * this deinitialization.
2486 */
Vitaly Kuzmichev7612a432011-02-11 18:18:34 +03002487 eth_stop(dev);
2488
Remy Bohmer23cd1382009-07-29 18:18:43 +02002489 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002490
2491 /* Clear pending interrupt */
2492 if (dev->network_started) {
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +05302493 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichevb3649f32011-02-11 18:18:31 +03002494 dev->network_started = 0;
2495 }
2496
Lei Wen988ee3e2010-12-01 23:43:43 +08002497 usb_gadget_unregister_driver(&eth_driver);
Kishon Vijay Abraham I8bfc2882015-08-19 13:49:46 +05302498 board_usb_cleanup(0, USB_INIT_DEVICE);
Remy Bohmer23cd1382009-07-29 18:18:43 +02002499}
2500
2501static struct usb_gadget_driver eth_driver = {
2502 .speed = DEVSPEED,
2503
2504 .bind = eth_bind,
2505 .unbind = eth_unbind,
2506
2507 .setup = eth_setup,
Kishon Vijay Abraham I5df13152015-08-19 13:49:48 +05302508 .reset = eth_disconnect,
Remy Bohmer23cd1382009-07-29 18:18:43 +02002509 .disconnect = eth_disconnect,
2510
2511 .suspend = eth_suspend,
2512 .resume = eth_resume,
2513};
2514
2515int usb_eth_initialize(bd_t *bi)
2516{
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +04002517 struct eth_device *netdev = &l_netdev;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002518
Vitaly Kuzmichev8f7aa832010-12-28 16:59:31 +03002519 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmer23cd1382009-07-29 18:18:43 +02002520
2521 netdev->init = usb_eth_init;
2522 netdev->send = usb_eth_send;
2523 netdev->recv = usb_eth_recv;
2524 netdev->halt = usb_eth_halt;
2525
2526#ifdef CONFIG_MCAST_TFTP
2527 #error not supported
2528#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +02002529 eth_register(netdev);
2530 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +02002531}