blob: 497b981129b5d9bcfba34a3407baf6068ff4219d [file] [log] [blame]
Remy Bohmerdf063442009-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 Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-07-29 18:18:43 +02009 */
10
11#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070012#include <console.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +030014#include <linux/netdevice.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020015#include <linux/usb/ch9.h>
16#include <linux/usb/cdc.h>
17#include <linux/usb/gadget.h>
18#include <net.h>
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +053019#include <usb.h>
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030020#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060021#include <memalign.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020022#include <linux/ctype.h>
23
24#include "gadget_chips.h"
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030025#include "rndis.h"
Remy Bohmerdf063442009-07-29 18:18:43 +020026
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +030027#define USB_NET_NAME "usb_ether"
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +040028
Remy Bohmerdf063442009-07-29 18:18:43 +020029#define atomic_read
30extern struct platform_data brd;
Remy Bohmerdf063442009-07-29 18:18:43 +020031
32
33unsigned packet_received, packet_sent;
34
Remy Bohmerdf063442009-07-29 18:18:43 +020035/*
36 * Ethernet gadget driver -- with CDC and non-CDC options
37 * Builds on hardware support for a full duplex link.
38 *
39 * CDC Ethernet is the standard USB solution for sending Ethernet frames
40 * using USB. Real hardware tends to use the same framing protocol but look
41 * different for control features. This driver strongly prefers to use
42 * this USB-IF standard as its open-systems interoperability solution;
43 * most host side USB stacks (except from Microsoft) support it.
44 *
45 * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
46 * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
47 * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
48 *
49 * There's some hardware that can't talk CDC ECM. We make that hardware
50 * implement a "minimalist" vendor-agnostic CDC core: same framing, but
51 * link-level setup only requires activating the configuration. Only the
52 * endpoint descriptors, and product/vendor IDs, are relevant; no control
53 * operations are available. Linux supports it, but other host operating
54 * systems may not. (This is a subset of CDC Ethernet.)
55 *
56 * It turns out that if you add a few descriptors to that "CDC Subset",
57 * (Windows) host side drivers from MCCI can treat it as one submode of
58 * a proprietary scheme called "SAFE" ... without needing to know about
59 * specific product/vendor IDs. So we do that, making it easier to use
60 * those MS-Windows drivers. Those added descriptors make it resemble a
61 * CDC MDLM device, but they don't change device behavior at all. (See
62 * MCCI Engineering report 950198 "SAFE Networking Functions".)
63 *
64 * A third option is also in use. Rather than CDC Ethernet, or something
65 * simpler, Microsoft pushes their own approach: RNDIS. The published
66 * RNDIS specs are ambiguous and appear to be incomplete, and are also
67 * needlessly complex. They borrow more from CDC ACM than CDC ECM.
68 */
69#define ETH_ALEN 6 /* Octets in one ethernet addr */
70#define ETH_HLEN 14 /* Total octets in header. */
71#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
72#define ETH_DATA_LEN 1500 /* Max. octets in payload */
73#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
Remy Bohmerdf063442009-07-29 18:18:43 +020074
75#define DRIVER_DESC "Ethernet Gadget"
76/* Based on linux 2.6.27 version */
77#define DRIVER_VERSION "May Day 2005"
78
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040079static const char shortname[] = "ether";
80static const char driver_desc[] = DRIVER_DESC;
Remy Bohmerdf063442009-07-29 18:18:43 +020081
82#define RX_EXTRA 20 /* guard against rx overflows */
83
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +030084#ifndef CONFIG_USB_ETH_RNDIS
85#define rndis_uninit(x) do {} while (0)
86#define rndis_deregister(c) do {} while (0)
87#define rndis_exit() do {} while (0)
88#endif
89
90/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
Remy Bohmerdf063442009-07-29 18:18:43 +020091#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
92 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
93 |USB_CDC_PACKET_TYPE_PROMISCUOUS \
94 |USB_CDC_PACKET_TYPE_DIRECTED)
95
96#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
97
98/*-------------------------------------------------------------------------*/
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +030099
100struct eth_dev {
101 struct usb_gadget *gadget;
102 struct usb_request *req; /* for control responses */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300103 struct usb_request *stat_req; /* for cdc & rndis status */
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300104
105 u8 config;
106 struct usb_ep *in_ep, *out_ep, *status_ep;
107 const struct usb_endpoint_descriptor
108 *in, *out, *status;
109
110 struct usb_request *tx_req, *rx_req;
111
112 struct eth_device *net;
113 struct net_device_stats stats;
114 unsigned int tx_qlen;
115
116 unsigned zlp:1;
117 unsigned cdc:1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300118 unsigned rndis:1;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300119 unsigned suspended:1;
120 unsigned network_started:1;
121 u16 cdc_filter;
122 unsigned long todo;
123 int mtu;
124#define WORK_RX_MEMORY 0
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300125 int rndis_config;
Vitaly Kuzmichev737f6b22011-02-11 18:18:33 +0300126 u8 host_mac[ETH_ALEN];
127};
128
129/*
130 * This version autoconfigures as much as possible at run-time.
131 *
132 * It also ASSUMES a self-powered device, without remote wakeup,
133 * although remote wakeup support would make sense.
134 */
135
136/*-------------------------------------------------------------------------*/
Remy Bohmerdf063442009-07-29 18:18:43 +0200137static struct eth_dev l_ethdev;
138static struct eth_device l_netdev;
139static struct usb_gadget_driver eth_driver;
140
141/*-------------------------------------------------------------------------*/
142
143/* "main" config is either CDC, or its simple subset */
144static inline int is_cdc(struct eth_dev *dev)
145{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200146#if !defined(CONFIG_USB_ETH_SUBSET)
Remy Bohmerdf063442009-07-29 18:18:43 +0200147 return 1; /* only cdc possible */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200148#elif !defined(CONFIG_USB_ETH_CDC)
Remy Bohmerdf063442009-07-29 18:18:43 +0200149 return 0; /* only subset possible */
150#else
151 return dev->cdc; /* depends on what hardware we found */
152#endif
153}
154
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300155/* "secondary" RNDIS config may sometimes be activated */
156static inline int rndis_active(struct eth_dev *dev)
157{
158#ifdef CONFIG_USB_ETH_RNDIS
159 return dev->rndis;
160#else
161 return 0;
162#endif
163}
164
165#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
166#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +0200167
168#define DEFAULT_QLEN 2 /* double buffering by default */
169
170/* peak bulk transfer bits-per-second */
171#define HS_BPS (13 * 512 * 8 * 1000 * 8)
172#define FS_BPS (19 * 64 * 1 * 1000 * 8)
173
174#ifdef CONFIG_USB_GADGET_DUALSPEED
175#define DEVSPEED USB_SPEED_HIGH
176
Vitaly Kuzmichevaec83b82010-08-12 16:44:40 +0400177#ifdef CONFIG_USB_ETH_QMULT
178#define qmult CONFIG_USB_ETH_QMULT
179#else
180#define qmult 5
181#endif
182
Remy Bohmerdf063442009-07-29 18:18:43 +0200183/* for dual-speed hardware, use deeper queues at highspeed */
184#define qlen(gadget) \
185 (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
186
187static inline int BITRATE(struct usb_gadget *g)
188{
189 return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
190}
191
192#else /* full speed (low speed doesn't do bulk) */
193
194#define qmult 1
195
196#define DEVSPEED USB_SPEED_FULL
197
198#define qlen(gadget) DEFAULT_QLEN
199
200static inline int BITRATE(struct usb_gadget *g)
201{
202 return FS_BPS;
203}
204#endif
205
Remy Bohmerdf063442009-07-29 18:18:43 +0200206/*-------------------------------------------------------------------------*/
207
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400208/*
209 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
Remy Bohmerdf063442009-07-29 18:18:43 +0200210 * Instead: allocate your own, using normal USB-IF procedures.
211 */
212
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400213/*
214 * Thanks to NetChip Technologies for donating this product ID.
Remy Bohmerdf063442009-07-29 18:18:43 +0200215 * It's for devices with only CDC Ethernet configurations.
216 */
217#define CDC_VENDOR_NUM 0x0525 /* NetChip */
218#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
219
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400220/*
221 * For hardware that can't talk CDC, we use the same vendor ID that
Remy Bohmerdf063442009-07-29 18:18:43 +0200222 * ARM Linux has used for ethernet-over-usb, both with sa1100 and
223 * with pxa250. We're protocol-compatible, if the host-side drivers
224 * use the endpoint descriptors. bcdDevice (version) is nonzero, so
225 * drivers that need to hard-wire endpoint numbers have a hook.
226 *
227 * The protocol is a minimal subset of CDC Ether, which works on any bulk
228 * hardware that's not deeply broken ... even on hardware that can't talk
229 * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
230 * doesn't handle control-OUT).
231 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300232#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
233#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
Remy Bohmerdf063442009-07-29 18:18:43 +0200234
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400235/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300236 * For hardware that can talk RNDIS and either of the above protocols,
237 * use this ID ... the windows INF files will know it. Unless it's
238 * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
239 * the non-RNDIS configuration.
240 */
241#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
242#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
243
244/*
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400245 * Some systems will want different product identifers published in the
Remy Bohmerdf063442009-07-29 18:18:43 +0200246 * device descriptor, either numbers or strings or both. These string
247 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
248 */
249
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300250/*
251 * Emulating them in eth_bind:
252 * static ushort idVendor;
253 * static ushort idProduct;
254 */
255
Remy Bohmerdf063442009-07-29 18:18:43 +0200256#if defined(CONFIG_USBNET_MANUFACTURER)
257static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
258#else
Bin Meng75574052016-02-05 19:30:11 -0800259static char *iManufacturer = "U-Boot";
Remy Bohmerdf063442009-07-29 18:18:43 +0200260#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300261
262/* These probably need to be configurable. */
263static ushort bcdDevice;
Remy Bohmerdf063442009-07-29 18:18:43 +0200264static char *iProduct;
265static char *iSerialNumber;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300266
Remy Bohmerdf063442009-07-29 18:18:43 +0200267static char dev_addr[18];
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300268
Remy Bohmerdf063442009-07-29 18:18:43 +0200269static char host_addr[18];
270
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300271
Remy Bohmerdf063442009-07-29 18:18:43 +0200272/*-------------------------------------------------------------------------*/
273
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400274/*
275 * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
Remy Bohmerdf063442009-07-29 18:18:43 +0200276 * ep0 implementation: descriptors, config management, setup().
277 * also optional class-specific notification interrupt transfer.
278 */
279
280/*
281 * DESCRIPTORS ... most are static, but strings and (full) configuration
282 * descriptors are built on demand. For now we do either full CDC, or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300283 * our simple subset, with RNDIS as an optional second configuration.
284 *
285 * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
286 * the class descriptors match a modem (they're ignored; it's really just
287 * Ethernet functionality), they don't need the NOP altsetting, and the
288 * status transfer endpoint isn't optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200289 */
290
291#define STRING_MANUFACTURER 1
292#define STRING_PRODUCT 2
293#define STRING_ETHADDR 3
294#define STRING_DATA 4
295#define STRING_CONTROL 5
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300296#define STRING_RNDIS_CONTROL 6
Remy Bohmerdf063442009-07-29 18:18:43 +0200297#define STRING_CDC 7
298#define STRING_SUBSET 8
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300299#define STRING_RNDIS 9
Remy Bohmerdf063442009-07-29 18:18:43 +0200300#define STRING_SERIALNUMBER 10
301
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300302/* holds our biggest descriptor (or RNDIS response) */
Remy Bohmerdf063442009-07-29 18:18:43 +0200303#define USB_BUFSIZ 256
304
305/*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300306 * This device advertises one configuration, eth_config, unless RNDIS
307 * is enabled (rndis_config) on hardware supporting at least two configs.
308 *
309 * NOTE: Controllers like superh_udc should probably be able to use
310 * an RNDIS-only configuration.
Remy Bohmerdf063442009-07-29 18:18:43 +0200311 *
312 * FIXME define some higher-powered configurations to make it easier
313 * to recharge batteries ...
314 */
315
316#define DEV_CONFIG_VALUE 1 /* cdc or subset */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300317#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
Remy Bohmerdf063442009-07-29 18:18:43 +0200318
319static struct usb_device_descriptor
320device_desc = {
321 .bLength = sizeof device_desc,
322 .bDescriptorType = USB_DT_DEVICE,
323
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400324 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200325
326 .bDeviceClass = USB_CLASS_COMM,
327 .bDeviceSubClass = 0,
328 .bDeviceProtocol = 0,
329
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400330 .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
331 .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
Remy Bohmerdf063442009-07-29 18:18:43 +0200332 .iManufacturer = STRING_MANUFACTURER,
333 .iProduct = STRING_PRODUCT,
334 .bNumConfigurations = 1,
335};
336
337static struct usb_otg_descriptor
338otg_descriptor = {
339 .bLength = sizeof otg_descriptor,
340 .bDescriptorType = USB_DT_OTG,
341
342 .bmAttributes = USB_OTG_SRP,
343};
344
345static struct usb_config_descriptor
346eth_config = {
347 .bLength = sizeof eth_config,
348 .bDescriptorType = USB_DT_CONFIG,
349
350 /* compute wTotalLength on the fly */
351 .bNumInterfaces = 2,
352 .bConfigurationValue = DEV_CONFIG_VALUE,
353 .iConfiguration = STRING_CDC,
354 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
355 .bMaxPower = 1,
356};
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300357
358#ifdef CONFIG_USB_ETH_RNDIS
359static struct usb_config_descriptor
360rndis_config = {
361 .bLength = sizeof rndis_config,
362 .bDescriptorType = USB_DT_CONFIG,
363
364 /* compute wTotalLength on the fly */
365 .bNumInterfaces = 2,
366 .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
367 .iConfiguration = STRING_RNDIS,
368 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
369 .bMaxPower = 1,
370};
371#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200372
373/*
374 * Compared to the simple CDC subset, the full CDC Ethernet model adds
375 * three class descriptors, two interface descriptors, optional status
376 * endpoint. Both have a "data" interface and two bulk endpoints.
377 * There are also differences in how control requests are handled.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300378 *
379 * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
380 * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
381 * may hang or oops. Since bugfixes (or accurate specs, letting Linux
382 * work around those bugs) are unlikely to ever come from MSFT, you may
383 * wish to avoid using RNDIS.
384 *
385 * MCCI offers an alternative to RNDIS if you need to connect to Windows
386 * but have hardware that can't support CDC Ethernet. We add descriptors
387 * to present the CDC Subset as a (nonconformant) CDC MDLM variant called
388 * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
389 * get those drivers from MCCI, or bundled with various products.
Remy Bohmerdf063442009-07-29 18:18:43 +0200390 */
391
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200392#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200393static struct usb_interface_descriptor
394control_intf = {
395 .bLength = sizeof control_intf,
396 .bDescriptorType = USB_DT_INTERFACE,
397
398 .bInterfaceNumber = 0,
399 /* status endpoint is optional; this may be patched later */
400 .bNumEndpoints = 1,
401 .bInterfaceClass = USB_CLASS_COMM,
402 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
403 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
404 .iInterface = STRING_CONTROL,
405};
406#endif
407
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300408#ifdef CONFIG_USB_ETH_RNDIS
409static const struct usb_interface_descriptor
410rndis_control_intf = {
411 .bLength = sizeof rndis_control_intf,
412 .bDescriptorType = USB_DT_INTERFACE,
413
414 .bInterfaceNumber = 0,
415 .bNumEndpoints = 1,
416 .bInterfaceClass = USB_CLASS_COMM,
417 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
418 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
419 .iInterface = STRING_RNDIS_CONTROL,
420};
421#endif
422
Remy Bohmerdf063442009-07-29 18:18:43 +0200423static const struct usb_cdc_header_desc header_desc = {
424 .bLength = sizeof header_desc,
425 .bDescriptorType = USB_DT_CS_INTERFACE,
426 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
427
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400428 .bcdCDC = __constant_cpu_to_le16(0x0110),
Remy Bohmerdf063442009-07-29 18:18:43 +0200429};
430
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200431#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200432
433static const struct usb_cdc_union_desc union_desc = {
434 .bLength = sizeof union_desc,
435 .bDescriptorType = USB_DT_CS_INTERFACE,
436 .bDescriptorSubType = USB_CDC_UNION_TYPE,
437
438 .bMasterInterface0 = 0, /* index of control interface */
439 .bSlaveInterface0 = 1, /* index of DATA interface */
440};
441
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300442#endif /* CDC || RNDIS */
443
444#ifdef CONFIG_USB_ETH_RNDIS
445
446static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
447 .bLength = sizeof call_mgmt_descriptor,
448 .bDescriptorType = USB_DT_CS_INTERFACE,
449 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
450
451 .bmCapabilities = 0x00,
452 .bDataInterface = 0x01,
453};
454
455static const struct usb_cdc_acm_descriptor acm_descriptor = {
456 .bLength = sizeof acm_descriptor,
457 .bDescriptorType = USB_DT_CS_INTERFACE,
458 .bDescriptorSubType = USB_CDC_ACM_TYPE,
459
460 .bmCapabilities = 0x00,
461};
462
463#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200464
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200465#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200466
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400467/*
468 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
Remy Bohmerdf063442009-07-29 18:18:43 +0200469 * ways: data endpoints live in the control interface, there's no data
470 * interface, and it's not used to talk to a cell phone radio.
471 */
472
473static const struct usb_cdc_mdlm_desc mdlm_desc = {
474 .bLength = sizeof mdlm_desc,
475 .bDescriptorType = USB_DT_CS_INTERFACE,
476 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
477
478 .bcdVersion = __constant_cpu_to_le16(0x0100),
479 .bGUID = {
480 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
481 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
482 },
483};
484
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400485/*
486 * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
Remy Bohmerdf063442009-07-29 18:18:43 +0200487 * can't really use its struct. All we do here is say that we're using
488 * the submode of "SAFE" which directly matches the CDC Subset.
489 */
490static const u8 mdlm_detail_desc[] = {
491 6,
492 USB_DT_CS_INTERFACE,
493 USB_CDC_MDLM_DETAIL_TYPE,
494
495 0, /* "SAFE" */
496 0, /* network control capabilities (none) */
497 0, /* network data capabilities ("raw" encapsulation) */
498};
499
500#endif
501
Remy Bohmerdf063442009-07-29 18:18:43 +0200502static const struct usb_cdc_ether_desc ether_desc = {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400503 .bLength = sizeof(ether_desc),
Remy Bohmerdf063442009-07-29 18:18:43 +0200504 .bDescriptorType = USB_DT_CS_INTERFACE,
505 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
506
507 /* this descriptor actually adds value, surprise! */
508 .iMACAddress = STRING_ETHADDR,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400509 .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
510 .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
511 .wNumberMCFilters = __constant_cpu_to_le16(0),
Remy Bohmerdf063442009-07-29 18:18:43 +0200512 .bNumberPowerFilters = 0,
513};
514
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200515#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200516
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400517/*
518 * include the status endpoint if we can, even where it's optional.
Remy Bohmerdf063442009-07-29 18:18:43 +0200519 * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
520 * packet, to simplify cancellation; and a big transfer interval, to
521 * waste less bandwidth.
522 *
523 * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
524 * if they ignore the connect/disconnect notifications that real aether
525 * can provide. more advanced cdc configurations might want to support
526 * encapsulated commands (vendor-specific, using control-OUT).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300527 *
528 * RNDIS requires the status endpoint, since it uses that encapsulation
529 * mechanism for its funky RPC scheme.
Remy Bohmerdf063442009-07-29 18:18:43 +0200530 */
531
532#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
533#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
534
535static struct usb_endpoint_descriptor
536fs_status_desc = {
537 .bLength = USB_DT_ENDPOINT_SIZE,
538 .bDescriptorType = USB_DT_ENDPOINT,
539
540 .bEndpointAddress = USB_DIR_IN,
541 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400542 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200543 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
544};
545#endif
546
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200547#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200548
549/* the default data interface has no endpoints ... */
550
551static const struct usb_interface_descriptor
552data_nop_intf = {
553 .bLength = sizeof data_nop_intf,
554 .bDescriptorType = USB_DT_INTERFACE,
555
556 .bInterfaceNumber = 1,
557 .bAlternateSetting = 0,
558 .bNumEndpoints = 0,
559 .bInterfaceClass = USB_CLASS_CDC_DATA,
560 .bInterfaceSubClass = 0,
561 .bInterfaceProtocol = 0,
562};
563
564/* ... but the "real" data interface has two bulk endpoints */
565
566static const struct usb_interface_descriptor
567data_intf = {
568 .bLength = sizeof data_intf,
569 .bDescriptorType = USB_DT_INTERFACE,
570
571 .bInterfaceNumber = 1,
572 .bAlternateSetting = 1,
573 .bNumEndpoints = 2,
574 .bInterfaceClass = USB_CLASS_CDC_DATA,
575 .bInterfaceSubClass = 0,
576 .bInterfaceProtocol = 0,
577 .iInterface = STRING_DATA,
578};
579
580#endif
581
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300582#ifdef CONFIG_USB_ETH_RNDIS
583
584/* RNDIS doesn't activate by changing to the "real" altsetting */
585
586static const struct usb_interface_descriptor
587rndis_data_intf = {
588 .bLength = sizeof rndis_data_intf,
589 .bDescriptorType = USB_DT_INTERFACE,
590
591 .bInterfaceNumber = 1,
592 .bAlternateSetting = 0,
593 .bNumEndpoints = 2,
594 .bInterfaceClass = USB_CLASS_CDC_DATA,
595 .bInterfaceSubClass = 0,
596 .bInterfaceProtocol = 0,
597 .iInterface = STRING_DATA,
598};
599
600#endif
601
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200602#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200603
604/*
605 * "Simple" CDC-subset option is a simple vendor-neutral model that most
606 * full speed controllers can handle: one interface, two bulk endpoints.
607 *
608 * To assist host side drivers, we fancy it up a bit, and add descriptors
609 * so some host side drivers will understand it as a "SAFE" variant.
610 */
611
612static const struct usb_interface_descriptor
613subset_data_intf = {
614 .bLength = sizeof subset_data_intf,
615 .bDescriptorType = USB_DT_INTERFACE,
616
617 .bInterfaceNumber = 0,
618 .bAlternateSetting = 0,
619 .bNumEndpoints = 2,
620 .bInterfaceClass = USB_CLASS_COMM,
621 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
622 .bInterfaceProtocol = 0,
623 .iInterface = STRING_DATA,
624};
625
626#endif /* SUBSET */
627
Remy Bohmerdf063442009-07-29 18:18:43 +0200628static struct usb_endpoint_descriptor
629fs_source_desc = {
630 .bLength = USB_DT_ENDPOINT_SIZE,
631 .bDescriptorType = USB_DT_ENDPOINT,
632
633 .bEndpointAddress = USB_DIR_IN,
634 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700635 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200636};
637
638static struct usb_endpoint_descriptor
639fs_sink_desc = {
640 .bLength = USB_DT_ENDPOINT_SIZE,
641 .bDescriptorType = USB_DT_ENDPOINT,
642
643 .bEndpointAddress = USB_DIR_OUT,
644 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Troy Kiskydbadc542013-09-25 18:41:04 -0700645 .wMaxPacketSize = __constant_cpu_to_le16(64),
Remy Bohmerdf063442009-07-29 18:18:43 +0200646};
647
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400648static const struct usb_descriptor_header *fs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200649 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200650#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200651 /* "cdc" mode descriptors */
652 (struct usb_descriptor_header *) &control_intf,
653 (struct usb_descriptor_header *) &header_desc,
654 (struct usb_descriptor_header *) &union_desc,
655 (struct usb_descriptor_header *) &ether_desc,
656 /* NOTE: status endpoint may need to be removed */
657 (struct usb_descriptor_header *) &fs_status_desc,
658 /* data interface, with altsetting */
659 (struct usb_descriptor_header *) &data_nop_intf,
660 (struct usb_descriptor_header *) &data_intf,
661 (struct usb_descriptor_header *) &fs_source_desc,
662 (struct usb_descriptor_header *) &fs_sink_desc,
663 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200664#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200665};
666
667static inline void fs_subset_descriptors(void)
668{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200669#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200670 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
671 fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
672 fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
673 fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
674 fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
675 fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
676 fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
677 fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
678 fs_eth_function[8] = NULL;
679#else
680 fs_eth_function[1] = NULL;
681#endif
682}
683
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300684#ifdef CONFIG_USB_ETH_RNDIS
685static const struct usb_descriptor_header *fs_rndis_function[] = {
686 (struct usb_descriptor_header *) &otg_descriptor,
687 /* control interface matches ACM, not Ethernet */
688 (struct usb_descriptor_header *) &rndis_control_intf,
689 (struct usb_descriptor_header *) &header_desc,
690 (struct usb_descriptor_header *) &call_mgmt_descriptor,
691 (struct usb_descriptor_header *) &acm_descriptor,
692 (struct usb_descriptor_header *) &union_desc,
693 (struct usb_descriptor_header *) &fs_status_desc,
694 /* data interface has no altsetting */
695 (struct usb_descriptor_header *) &rndis_data_intf,
696 (struct usb_descriptor_header *) &fs_source_desc,
697 (struct usb_descriptor_header *) &fs_sink_desc,
698 NULL,
699};
700#endif
701
Remy Bohmerdf063442009-07-29 18:18:43 +0200702/*
703 * usb 2.0 devices need to expose both high speed and full speed
704 * descriptors, unless they only run at full speed.
705 */
706
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200707#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +0200708static struct usb_endpoint_descriptor
709hs_status_desc = {
710 .bLength = USB_DT_ENDPOINT_SIZE,
711 .bDescriptorType = USB_DT_ENDPOINT,
712
713 .bmAttributes = USB_ENDPOINT_XFER_INT,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400714 .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
Remy Bohmerdf063442009-07-29 18:18:43 +0200715 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
716};
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200717#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200718
719static struct usb_endpoint_descriptor
720hs_source_desc = {
721 .bLength = USB_DT_ENDPOINT_SIZE,
722 .bDescriptorType = USB_DT_ENDPOINT,
723
724 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400725 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200726};
727
728static struct usb_endpoint_descriptor
729hs_sink_desc = {
730 .bLength = USB_DT_ENDPOINT_SIZE,
731 .bDescriptorType = USB_DT_ENDPOINT,
732
733 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400734 .wMaxPacketSize = __constant_cpu_to_le16(512),
Remy Bohmerdf063442009-07-29 18:18:43 +0200735};
736
737static struct usb_qualifier_descriptor
738dev_qualifier = {
739 .bLength = sizeof dev_qualifier,
740 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
741
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400742 .bcdUSB = __constant_cpu_to_le16(0x0200),
Remy Bohmerdf063442009-07-29 18:18:43 +0200743 .bDeviceClass = USB_CLASS_COMM,
744
745 .bNumConfigurations = 1,
746};
747
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400748static const struct usb_descriptor_header *hs_eth_function[11] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200749 (struct usb_descriptor_header *) &otg_descriptor,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200750#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200751 /* "cdc" mode descriptors */
752 (struct usb_descriptor_header *) &control_intf,
753 (struct usb_descriptor_header *) &header_desc,
754 (struct usb_descriptor_header *) &union_desc,
755 (struct usb_descriptor_header *) &ether_desc,
756 /* NOTE: status endpoint may need to be removed */
757 (struct usb_descriptor_header *) &hs_status_desc,
758 /* data interface, with altsetting */
759 (struct usb_descriptor_header *) &data_nop_intf,
760 (struct usb_descriptor_header *) &data_intf,
761 (struct usb_descriptor_header *) &hs_source_desc,
762 (struct usb_descriptor_header *) &hs_sink_desc,
763 NULL,
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200764#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200765};
766
767static inline void hs_subset_descriptors(void)
768{
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200769#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200770 /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
771 hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
772 hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
773 hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
774 hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
775 hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
776 hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
777 hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
778 hs_eth_function[8] = NULL;
779#else
780 hs_eth_function[1] = NULL;
781#endif
782}
783
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300784#ifdef CONFIG_USB_ETH_RNDIS
785static const struct usb_descriptor_header *hs_rndis_function[] = {
786 (struct usb_descriptor_header *) &otg_descriptor,
787 /* control interface matches ACM, not Ethernet */
788 (struct usb_descriptor_header *) &rndis_control_intf,
789 (struct usb_descriptor_header *) &header_desc,
790 (struct usb_descriptor_header *) &call_mgmt_descriptor,
791 (struct usb_descriptor_header *) &acm_descriptor,
792 (struct usb_descriptor_header *) &union_desc,
793 (struct usb_descriptor_header *) &hs_status_desc,
794 /* data interface has no altsetting */
795 (struct usb_descriptor_header *) &rndis_data_intf,
796 (struct usb_descriptor_header *) &hs_source_desc,
797 (struct usb_descriptor_header *) &hs_sink_desc,
798 NULL,
799};
800#endif
801
802
Remy Bohmerdf063442009-07-29 18:18:43 +0200803/* maxpacket and other transfer characteristics vary by speed. */
804static inline struct usb_endpoint_descriptor *
805ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
806 struct usb_endpoint_descriptor *fs)
807{
808 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
809 return hs;
810 return fs;
811}
812
Remy Bohmerdf063442009-07-29 18:18:43 +0200813/*-------------------------------------------------------------------------*/
814
815/* descriptors that are built on-demand */
816
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400817static char manufacturer[50];
818static char product_desc[40] = DRIVER_DESC;
819static char serial_number[20];
Remy Bohmerdf063442009-07-29 18:18:43 +0200820
821/* address that the host will use ... usually assigned at random */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400822static char ethaddr[2 * ETH_ALEN + 1];
Remy Bohmerdf063442009-07-29 18:18:43 +0200823
824/* static strings, in UTF-8 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400825static struct usb_string strings[] = {
Remy Bohmerdf063442009-07-29 18:18:43 +0200826 { STRING_MANUFACTURER, manufacturer, },
827 { STRING_PRODUCT, product_desc, },
828 { STRING_SERIALNUMBER, serial_number, },
829 { STRING_DATA, "Ethernet Data", },
830 { STRING_ETHADDR, ethaddr, },
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200831#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +0200832 { STRING_CDC, "CDC Ethernet", },
833 { STRING_CONTROL, "CDC Communications Control", },
834#endif
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200835#ifdef CONFIG_USB_ETH_SUBSET
Remy Bohmerdf063442009-07-29 18:18:43 +0200836 { STRING_SUBSET, "CDC Ethernet Subset", },
837#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300838#ifdef CONFIG_USB_ETH_RNDIS
839 { STRING_RNDIS, "RNDIS", },
840 { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
841#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200842 { } /* end of list */
843};
844
845static struct usb_gadget_strings stringtab = {
846 .language = 0x0409, /* en-us */
847 .strings = strings,
848};
849
Remy Bohmerdf063442009-07-29 18:18:43 +0200850/*============================================================================*/
Joel Fernandes00b78392013-09-04 18:55:14 -0500851DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
852
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200853#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Joel Fernandes00b78392013-09-04 18:55:14 -0500854DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +0200855#endif
Remy Bohmerdf063442009-07-29 18:18:43 +0200856
Remy Bohmerdf063442009-07-29 18:18:43 +0200857/*============================================================================*/
858
859/*
860 * one config, two interfaces: control, data.
861 * complications: class descriptors, and an altsetting.
862 */
863static int
864config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
865{
866 int len;
867 const struct usb_config_descriptor *config;
868 const struct usb_descriptor_header **function;
869 int hs = 0;
870
871 if (gadget_is_dualspeed(g)) {
872 hs = (g->speed == USB_SPEED_HIGH);
873 if (type == USB_DT_OTHER_SPEED_CONFIG)
874 hs = !hs;
875 }
876#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
877
878 if (index >= device_desc.bNumConfigurations)
879 return -EINVAL;
880
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300881#ifdef CONFIG_USB_ETH_RNDIS
882 /*
883 * list the RNDIS config first, to make Microsoft's drivers
884 * happy. DOCSIS 1.0 needs this too.
885 */
886 if (device_desc.bNumConfigurations == 2 && index == 0) {
887 config = &rndis_config;
888 function = which_fn(rndis);
889 } else
890#endif
891 {
892 config = &eth_config;
893 function = which_fn(eth);
894 }
Remy Bohmerdf063442009-07-29 18:18:43 +0200895
896 /* for now, don't advertise srp-only devices */
897 if (!is_otg)
898 function++;
899
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400900 len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
Remy Bohmerdf063442009-07-29 18:18:43 +0200901 if (len < 0)
902 return len;
903 ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
904 return len;
905}
906
907/*-------------------------------------------------------------------------*/
908
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300909static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400910static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200911
912static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400913set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +0200914{
915 int result = 0;
916 struct usb_gadget *gadget = dev->gadget;
917
Lukasz Dalek284c4cf2012-10-02 17:04:31 +0200918#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300919 /* status endpoint used for RNDIS and (optionally) CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +0200920 if (!subset_active(dev) && dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400921 dev->status = ep_desc(gadget, &hs_status_desc,
Remy Bohmerdf063442009-07-29 18:18:43 +0200922 &fs_status_desc);
923 dev->status_ep->driver_data = dev;
924
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400925 result = usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +0200926 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400927 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200928 dev->status_ep->name, result);
929 goto done;
930 }
931 }
932#endif
933
934 dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
935 dev->in_ep->driver_data = dev;
936
937 dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
938 dev->out_ep->driver_data = dev;
939
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400940 /*
941 * With CDC, the host isn't allowed to use these two data
Remy Bohmerdf063442009-07-29 18:18:43 +0200942 * endpoints in the default altsetting for the interface.
943 * so we don't activate them yet. Reset from SET_INTERFACE.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300944 *
945 * Strictly speaking RNDIS should work the same: activation is
946 * a side effect of setting a packet filter. Deactivation is
947 * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
Remy Bohmerdf063442009-07-29 18:18:43 +0200948 */
949 if (!cdc_active(dev)) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400950 result = usb_ep_enable(dev->in_ep, dev->in);
Remy Bohmerdf063442009-07-29 18:18:43 +0200951 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400952 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200953 dev->in_ep->name, result);
954 goto done;
955 }
956
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400957 result = usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +0200958 if (result != 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400959 debug("enable %s --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +0200960 dev->out_ep->name, result);
961 goto done;
962 }
963 }
964
965done:
966 if (result == 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400967 result = alloc_requests(dev, qlen(gadget), gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +0200968
969 /* on error, disable any endpoints */
970 if (result < 0) {
Vitaly Kuzmichev750be4c2010-08-13 17:02:29 +0400971 if (!subset_active(dev) && dev->status_ep)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400972 (void) usb_ep_disable(dev->status_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200973 dev->status = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400974 (void) usb_ep_disable(dev->in_ep);
975 (void) usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +0200976 dev->in = NULL;
977 dev->out = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300978 } else if (!cdc_active(dev)) {
979 /*
980 * activate non-CDC configs right away
981 * this isn't strictly according to the RNDIS spec
982 */
983 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +0200984 }
985
986 /* caller is responsible for cleanup on error */
987 return result;
988}
989
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400990static void eth_reset_config(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +0200991{
992 if (dev->config == 0)
993 return;
994
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +0400995 debug("%s\n", __func__);
996
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +0300997 rndis_uninit(dev->rndis_config);
998
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400999 /*
1000 * disable endpoints, forcing (synchronous) completion of
Remy Bohmerdf063442009-07-29 18:18:43 +02001001 * pending i/o. then free the requests.
1002 */
1003
1004 if (dev->in) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001005 usb_ep_disable(dev->in_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001006 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001007 usb_ep_free_request(dev->in_ep, dev->tx_req);
1008 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001009 }
1010 }
1011 if (dev->out) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001012 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001013 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001014 usb_ep_free_request(dev->out_ep, dev->rx_req);
1015 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001016 }
1017 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001018 if (dev->status)
1019 usb_ep_disable(dev->status_ep);
1020
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001021 dev->rndis = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02001022 dev->cdc_filter = 0;
1023 dev->config = 0;
1024}
1025
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001026/*
1027 * change our operational config. must agree with the code
Remy Bohmerdf063442009-07-29 18:18:43 +02001028 * that returns config descriptors, and altsetting code.
1029 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001030static int eth_set_config(struct eth_dev *dev, unsigned number,
1031 gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001032{
1033 int result = 0;
1034 struct usb_gadget *gadget = dev->gadget;
1035
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001036 if (gadget_is_sa1100(gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001037 && dev->config
1038 && dev->tx_qlen != 0) {
1039 /* tx fifo is full, but we can't clear it...*/
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001040 error("can't change configurations");
Remy Bohmerdf063442009-07-29 18:18:43 +02001041 return -ESPIPE;
1042 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001043 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001044
1045 switch (number) {
1046 case DEV_CONFIG_VALUE:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001047 result = set_ether_config(dev, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001048 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001049#ifdef CONFIG_USB_ETH_RNDIS
1050 case DEV_RNDIS_CONFIG_VALUE:
1051 dev->rndis = 1;
1052 result = set_ether_config(dev, gfp_flags);
1053 break;
1054#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02001055 default:
1056 result = -EINVAL;
1057 /* FALL THROUGH */
1058 case 0:
1059 break;
1060 }
1061
1062 if (result) {
1063 if (number)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001064 eth_reset_config(dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02001065 usb_gadget_vbus_draw(dev->gadget,
1066 gadget_is_otg(dev->gadget) ? 8 : 100);
1067 } else {
1068 char *speed;
1069 unsigned power;
1070
1071 power = 2 * eth_config.bMaxPower;
1072 usb_gadget_vbus_draw(dev->gadget, power);
1073
1074 switch (gadget->speed) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001075 case USB_SPEED_FULL:
1076 speed = "full"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001077#ifdef CONFIG_USB_GADGET_DUALSPEED
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001078 case USB_SPEED_HIGH:
1079 speed = "high"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001080#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001081 default:
1082 speed = "?"; break;
Remy Bohmerdf063442009-07-29 18:18:43 +02001083 }
1084
1085 dev->config = number;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001086 printf("%s speed config #%d: %d mA, %s, using %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001087 speed, number, power, driver_desc,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001088 rndis_active(dev)
1089 ? "RNDIS"
1090 : (cdc_active(dev)
1091 ? "CDC Ethernet"
Remy Bohmerdf063442009-07-29 18:18:43 +02001092 : "CDC Ethernet Subset"));
1093 }
1094 return result;
1095}
1096
1097/*-------------------------------------------------------------------------*/
1098
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001099#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001100
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001101/*
1102 * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
Remy Bohmerdf063442009-07-29 18:18:43 +02001103 * only to notify the host about link status changes (which we support) or
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001104 * report completion of some encapsulated command (as used in RNDIS). Since
Remy Bohmerdf063442009-07-29 18:18:43 +02001105 * we want this CDC Ethernet code to be vendor-neutral, we don't use that
1106 * command mechanism; and only one status request is ever queued.
1107 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001108static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001109{
1110 struct usb_cdc_notification *event = req->buf;
1111 int value = req->status;
1112 struct eth_dev *dev = ep->driver_data;
1113
1114 /* issue the second notification if host reads the first */
1115 if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
1116 && value == 0) {
1117 __le32 *data = req->buf + sizeof *event;
1118
1119 event->bmRequestType = 0xA1;
1120 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001121 event->wValue = __constant_cpu_to_le16(0);
1122 event->wIndex = __constant_cpu_to_le16(1);
1123 event->wLength = __constant_cpu_to_le16(8);
Remy Bohmerdf063442009-07-29 18:18:43 +02001124
1125 /* SPEED_CHANGE data is up/down speeds in bits/sec */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001126 data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
Remy Bohmerdf063442009-07-29 18:18:43 +02001127
1128 req->length = STATUS_BYTECOUNT;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001129 value = usb_ep_queue(ep, req, GFP_ATOMIC);
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001130 debug("send SPEED_CHANGE --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001131 if (value == 0)
1132 return;
1133 } else if (value != -ECONNRESET) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001134 debug("event %02x --> %d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001135 event->bNotificationType, value);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001136 if (event->bNotificationType ==
1137 USB_CDC_NOTIFY_SPEED_CHANGE) {
1138 l_ethdev.network_started = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001139 printf("USB network up!\n");
1140 }
1141 }
1142 req->context = NULL;
1143}
1144
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001145static void issue_start_status(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001146{
1147 struct usb_request *req = dev->stat_req;
1148 struct usb_cdc_notification *event;
1149 int value;
1150
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001151 /*
1152 * flush old status
Remy Bohmerdf063442009-07-29 18:18:43 +02001153 *
1154 * FIXME ugly idiom, maybe we'd be better with just
1155 * a "cancel the whole queue" primitive since any
1156 * unlink-one primitive has way too many error modes.
1157 * here, we "know" toggle is already clear...
1158 *
1159 * FIXME iff req->context != null just dequeue it
1160 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001161 usb_ep_disable(dev->status_ep);
1162 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001163
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001164 /*
1165 * 3.8.1 says to issue first NETWORK_CONNECTION, then
Remy Bohmerdf063442009-07-29 18:18:43 +02001166 * a SPEED_CHANGE. could be useful in some configs.
1167 */
1168 event = req->buf;
1169 event->bmRequestType = 0xA1;
1170 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001171 event->wValue = __constant_cpu_to_le16(1); /* connected */
1172 event->wIndex = __constant_cpu_to_le16(1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001173 event->wLength = 0;
1174
1175 req->length = sizeof *event;
1176 req->complete = eth_status_complete;
1177 req->context = dev;
1178
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001179 value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001180 if (value < 0)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001181 debug("status buf queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001182}
1183
1184#endif
1185
1186/*-------------------------------------------------------------------------*/
1187
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001188static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001189{
1190 if (req->status || req->actual != req->length)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001191 debug("setup complete --> %d, %d/%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001192 req->status, req->actual, req->length);
1193}
1194
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001195#ifdef CONFIG_USB_ETH_RNDIS
1196
1197static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
1198{
1199 if (req->status || req->actual != req->length)
1200 debug("rndis response complete --> %d, %d/%d\n",
1201 req->status, req->actual, req->length);
1202
1203 /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
1204}
1205
1206static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
1207{
1208 struct eth_dev *dev = ep->driver_data;
1209 int status;
1210
1211 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
1212 status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
1213 if (status < 0)
1214 error("%s: rndis parse error %d", __func__, status);
1215}
1216
1217#endif /* RNDIS */
1218
Remy Bohmerdf063442009-07-29 18:18:43 +02001219/*
1220 * The setup() callback implements all the ep0 functionality that's not
1221 * handled lower down. CDC has a number of less-common features:
1222 *
1223 * - two interfaces: control, and ethernet data
1224 * - Ethernet data interface has two altsettings: default, and active
1225 * - class-specific descriptors for the control interface
1226 * - class-specific control requests
1227 */
1228static int
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001229eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Remy Bohmerdf063442009-07-29 18:18:43 +02001230{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001231 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001232 struct usb_request *req = dev->req;
1233 int value = -EOPNOTSUPP;
1234 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1235 u16 wValue = le16_to_cpu(ctrl->wValue);
1236 u16 wLength = le16_to_cpu(ctrl->wLength);
1237
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001238 /*
1239 * descriptors just go into the pre-allocated ep0 buffer,
Remy Bohmerdf063442009-07-29 18:18:43 +02001240 * while config change events may enable network traffic.
1241 */
1242
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001243 debug("%s\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02001244
1245 req->complete = eth_setup_complete;
1246 switch (ctrl->bRequest) {
1247
1248 case USB_REQ_GET_DESCRIPTOR:
1249 if (ctrl->bRequestType != USB_DIR_IN)
1250 break;
1251 switch (wValue >> 8) {
1252
1253 case USB_DT_DEVICE:
Kishon Vijay Abraham I58d701e2015-02-23 18:40:17 +05301254 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001255 value = min(wLength, (u16) sizeof device_desc);
1256 memcpy(req->buf, &device_desc, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001257 break;
1258 case USB_DT_DEVICE_QUALIFIER:
1259 if (!gadget_is_dualspeed(gadget))
1260 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001261 value = min(wLength, (u16) sizeof dev_qualifier);
1262 memcpy(req->buf, &dev_qualifier, value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001263 break;
1264
1265 case USB_DT_OTHER_SPEED_CONFIG:
1266 if (!gadget_is_dualspeed(gadget))
1267 break;
1268 /* FALLTHROUGH */
1269 case USB_DT_CONFIG:
1270 value = config_buf(gadget, req->buf,
1271 wValue >> 8,
1272 wValue & 0xff,
1273 gadget_is_otg(gadget));
1274 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001275 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001276 break;
1277
1278 case USB_DT_STRING:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001279 value = usb_gadget_get_string(&stringtab,
Remy Bohmerdf063442009-07-29 18:18:43 +02001280 wValue & 0xff, req->buf);
1281
1282 if (value >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001283 value = min(wLength, (u16) value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001284
1285 break;
1286 }
1287 break;
1288
1289 case USB_REQ_SET_CONFIGURATION:
1290 if (ctrl->bRequestType != 0)
1291 break;
1292 if (gadget->a_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001293 debug("HNP available\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001294 else if (gadget->a_alt_hnp_support)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001295 debug("HNP needs a different root port\n");
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001296 value = eth_set_config(dev, wValue, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001297 break;
1298 case USB_REQ_GET_CONFIGURATION:
1299 if (ctrl->bRequestType != USB_DIR_IN)
1300 break;
1301 *(u8 *)req->buf = dev->config;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001302 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001303 break;
1304
1305 case USB_REQ_SET_INTERFACE:
1306 if (ctrl->bRequestType != USB_RECIP_INTERFACE
1307 || !dev->config
1308 || wIndex > 1)
1309 break;
1310 if (!cdc_active(dev) && wIndex != 0)
1311 break;
1312
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001313 /*
1314 * PXA hardware partially handles SET_INTERFACE;
Remy Bohmerdf063442009-07-29 18:18:43 +02001315 * we need to kluge around that interference.
1316 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001317 if (gadget_is_pxa(gadget)) {
1318 value = eth_set_config(dev, DEV_CONFIG_VALUE,
Remy Bohmerdf063442009-07-29 18:18:43 +02001319 GFP_ATOMIC);
Lukasz Dalek6ca42dd2012-10-02 17:04:29 +02001320 /*
1321 * PXA25x driver use non-CDC ethernet gadget.
1322 * But only _CDC and _RNDIS code can signalize
1323 * that network is working. So we signalize it
1324 * here.
1325 */
1326 l_ethdev.network_started = 1;
1327 debug("USB network up!\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001328 goto done_set_intf;
1329 }
1330
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001331#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001332 switch (wIndex) {
1333 case 0: /* control/master intf */
1334 if (wValue != 0)
1335 break;
1336 if (dev->status) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001337 usb_ep_disable(dev->status_ep);
1338 usb_ep_enable(dev->status_ep, dev->status);
Remy Bohmerdf063442009-07-29 18:18:43 +02001339 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001340
Remy Bohmerdf063442009-07-29 18:18:43 +02001341 value = 0;
1342 break;
1343 case 1: /* data intf */
1344 if (wValue > 1)
1345 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001346 usb_ep_disable(dev->in_ep);
1347 usb_ep_disable(dev->out_ep);
Remy Bohmerdf063442009-07-29 18:18:43 +02001348
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001349 /*
1350 * CDC requires the data transfers not be done from
Remy Bohmerdf063442009-07-29 18:18:43 +02001351 * the default interface setting ... also, setting
1352 * the non-default interface resets filters etc.
1353 */
1354 if (wValue == 1) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001355 if (!cdc_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001356 break;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001357 usb_ep_enable(dev->in_ep, dev->in);
1358 usb_ep_enable(dev->out_ep, dev->out);
Remy Bohmerdf063442009-07-29 18:18:43 +02001359 dev->cdc_filter = DEFAULT_FILTER;
1360 if (dev->status)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001361 issue_start_status(dev);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001362 eth_start(dev, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001363 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001364 value = 0;
1365 break;
1366 }
1367#else
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001368 /*
1369 * FIXME this is wrong, as is the assumption that
Remy Bohmerdf063442009-07-29 18:18:43 +02001370 * all non-PXA hardware talks real CDC ...
1371 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001372 debug("set_interface ignored!\n");
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001373#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001374
1375done_set_intf:
1376 break;
1377 case USB_REQ_GET_INTERFACE:
1378 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
1379 || !dev->config
1380 || wIndex > 1)
1381 break;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001382 if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
Remy Bohmerdf063442009-07-29 18:18:43 +02001383 break;
1384
1385 /* for CDC, iff carrier is on, data interface is active. */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001386 if (rndis_active(dev) || wIndex != 1)
Remy Bohmerdf063442009-07-29 18:18:43 +02001387 *(u8 *)req->buf = 0;
1388 else {
1389 /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
1390 /* carrier always ok ...*/
1391 *(u8 *)req->buf = 1 ;
1392 }
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001393 value = min(wLength, (u16) 1);
Remy Bohmerdf063442009-07-29 18:18:43 +02001394 break;
1395
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001396#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001397 case USB_CDC_SET_ETHERNET_PACKET_FILTER:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001398 /*
1399 * see 6.2.30: no data, wIndex = interface,
Remy Bohmerdf063442009-07-29 18:18:43 +02001400 * wValue = packet filter bitmap
1401 */
1402 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1403 || !cdc_active(dev)
1404 || wLength != 0
1405 || wIndex > 1)
1406 break;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001407 debug("packet filter %02x\n", wValue);
Remy Bohmerdf063442009-07-29 18:18:43 +02001408 dev->cdc_filter = wValue;
1409 value = 0;
1410 break;
1411
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001412 /*
1413 * and potentially:
Remy Bohmerdf063442009-07-29 18:18:43 +02001414 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
1415 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
1416 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
1417 * case USB_CDC_GET_ETHERNET_STATISTIC:
1418 */
1419
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001420#endif /* CONFIG_USB_ETH_CDC */
Remy Bohmerdf063442009-07-29 18:18:43 +02001421
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001422#ifdef CONFIG_USB_ETH_RNDIS
1423 /*
1424 * RNDIS uses the CDC command encapsulation mechanism to implement
1425 * an RPC scheme, with much getting/setting of attributes by OID.
1426 */
1427 case USB_CDC_SEND_ENCAPSULATED_COMMAND:
1428 if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1429 || !rndis_active(dev)
1430 || wLength > USB_BUFSIZ
1431 || wValue
1432 || rndis_control_intf.bInterfaceNumber
1433 != wIndex)
1434 break;
1435 /* read the request, then process it */
1436 value = wLength;
1437 req->complete = rndis_command_complete;
1438 /* later, rndis_control_ack () sends a notification */
1439 break;
1440
1441 case USB_CDC_GET_ENCAPSULATED_RESPONSE:
1442 if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
1443 == ctrl->bRequestType
1444 && rndis_active(dev)
1445 /* && wLength >= 0x0400 */
1446 && !wValue
1447 && rndis_control_intf.bInterfaceNumber
1448 == wIndex) {
1449 u8 *buf;
1450 u32 n;
1451
1452 /* return the result */
1453 buf = rndis_get_next_response(dev->rndis_config, &n);
1454 if (buf) {
1455 memcpy(req->buf, buf, n);
1456 req->complete = rndis_response_complete;
1457 rndis_free_response(dev->rndis_config, buf);
1458 value = n;
1459 }
1460 /* else stalls ... spec says to avoid that */
1461 }
1462 break;
1463#endif /* RNDIS */
1464
Remy Bohmerdf063442009-07-29 18:18:43 +02001465 default:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001466 debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02001467 ctrl->bRequestType, ctrl->bRequest,
1468 wValue, wIndex, wLength);
1469 }
1470
1471 /* respond with data transfer before status phase? */
1472 if (value >= 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001473 debug("respond with data transfer before status phase\n");
Remy Bohmerdf063442009-07-29 18:18:43 +02001474 req->length = value;
1475 req->zero = value < wLength
1476 && (value % gadget->ep0->maxpacket) == 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001477 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02001478 if (value < 0) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001479 debug("ep_queue --> %d\n", value);
Remy Bohmerdf063442009-07-29 18:18:43 +02001480 req->status = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001481 eth_setup_complete(gadget->ep0, req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001482 }
1483 }
1484
1485 /* host either stalls (value < 0) or reports success */
1486 return value;
1487}
1488
Remy Bohmerdf063442009-07-29 18:18:43 +02001489/*-------------------------------------------------------------------------*/
1490
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001491static void rx_complete(struct usb_ep *ep, struct usb_request *req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001492
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001493static int rx_submit(struct eth_dev *dev, struct usb_request *req,
Remy Bohmerdf063442009-07-29 18:18:43 +02001494 gfp_t gfp_flags)
1495{
1496 int retval = -ENOMEM;
1497 size_t size;
1498
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001499 /*
1500 * Padding up to RX_EXTRA handles minor disagreements with host.
Remy Bohmerdf063442009-07-29 18:18:43 +02001501 * Normally we use the USB "terminate on short read" convention;
1502 * so allow up to (N*maxpacket), since that memory is normally
1503 * already allocated. Some hardware doesn't deal well with short
1504 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
1505 * byte off the end (to force hardware errors on overflow).
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001506 *
1507 * RNDIS uses internal framing, and explicitly allows senders to
1508 * pad to end-of-packet. That's potentially nice for speed,
1509 * but means receivers can't recover synch on their own.
Remy Bohmerdf063442009-07-29 18:18:43 +02001510 */
1511
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001512 debug("%s\n", __func__);
Troy Kisky1d01de12013-09-25 18:41:05 -07001513 if (!req)
1514 return -EINVAL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001515
1516 size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
1517 size += dev->out_ep->maxpacket - 1;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001518 if (rndis_active(dev))
1519 size += sizeof(struct rndis_packet_msg_type);
Remy Bohmerdf063442009-07-29 18:18:43 +02001520 size -= size % dev->out_ep->maxpacket;
1521
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001522 /*
1523 * Some platforms perform better when IP packets are aligned,
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001524 * but on at least one, checksumming fails otherwise. Note:
1525 * RNDIS headers involve variable numbers of LE32 values.
Remy Bohmerdf063442009-07-29 18:18:43 +02001526 */
1527
Joe Hershberger9f09a362015-04-08 01:41:06 -05001528 req->buf = (u8 *)net_rx_packets[0];
Remy Bohmerdf063442009-07-29 18:18:43 +02001529 req->length = size;
1530 req->complete = rx_complete;
1531
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001532 retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
Remy Bohmerdf063442009-07-29 18:18:43 +02001533
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001534 if (retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001535 error("rx submit --> %d", retval);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001536
Remy Bohmerdf063442009-07-29 18:18:43 +02001537 return retval;
1538}
1539
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001540static void rx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001541{
1542 struct eth_dev *dev = ep->driver_data;
1543
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001544 debug("%s: status %d\n", __func__, req->status);
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001545 switch (req->status) {
1546 /* normal completion */
1547 case 0:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001548 if (rndis_active(dev)) {
1549 /* we know MaxPacketsPerTransfer == 1 here */
1550 int length = rndis_rm_hdr(req->buf, req->actual);
1551 if (length < 0)
1552 goto length_err;
1553 req->length -= length;
1554 req->actual -= length;
1555 }
1556 if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
1557length_err:
1558 dev->stats.rx_errors++;
1559 dev->stats.rx_length_errors++;
1560 debug("rx length %d\n", req->length);
1561 break;
1562 }
1563
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001564 dev->stats.rx_packets++;
1565 dev->stats.rx_bytes += req->length;
1566 break;
1567
1568 /* software-driven interface shutdown */
1569 case -ECONNRESET: /* unlink */
1570 case -ESHUTDOWN: /* disconnect etc */
1571 /* for hardware automagic (such as pxa) */
1572 case -ECONNABORTED: /* endpoint reset */
1573 break;
1574
1575 /* data overrun */
1576 case -EOVERFLOW:
1577 dev->stats.rx_over_errors++;
1578 /* FALLTHROUGH */
1579 default:
1580 dev->stats.rx_errors++;
1581 break;
1582 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001583
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001584 packet_received = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001585}
1586
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001587static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
Remy Bohmerdf063442009-07-29 18:18:43 +02001588{
1589
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001590 dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001591
1592 if (!dev->tx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001593 goto fail1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001594
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001595 dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02001596
1597 if (!dev->rx_req)
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001598 goto fail2;
Remy Bohmerdf063442009-07-29 18:18:43 +02001599
1600 return 0;
1601
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04001602fail2:
1603 usb_ep_free_request(dev->in_ep, dev->tx_req);
1604fail1:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001605 error("can't alloc requests");
Remy Bohmerdf063442009-07-29 18:18:43 +02001606 return -1;
1607}
1608
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001609static void tx_complete(struct usb_ep *ep, struct usb_request *req)
Remy Bohmerdf063442009-07-29 18:18:43 +02001610{
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001611 struct eth_dev *dev = ep->driver_data;
1612
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001613 debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
Vitaly Kuzmichev24fcb472011-02-11 18:18:32 +03001614 switch (req->status) {
1615 default:
1616 dev->stats.tx_errors++;
1617 debug("tx err %d\n", req->status);
1618 /* FALLTHROUGH */
1619 case -ECONNRESET: /* unlink */
1620 case -ESHUTDOWN: /* disconnect etc */
1621 break;
1622 case 0:
1623 dev->stats.tx_bytes += req->length;
1624 }
1625 dev->stats.tx_packets++;
1626
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001627 packet_sent = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001628}
1629
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001630static inline int eth_is_promisc(struct eth_dev *dev)
Remy Bohmerdf063442009-07-29 18:18:43 +02001631{
1632 /* no filters for the CDC subset; always promisc */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001633 if (subset_active(dev))
Remy Bohmerdf063442009-07-29 18:18:43 +02001634 return 1;
1635 return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
1636}
1637
1638#if 0
1639static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
1640{
1641 struct eth_dev *dev = netdev_priv(net);
1642 int length = skb->len;
1643 int retval;
1644 struct usb_request *req = NULL;
1645 unsigned long flags;
1646
1647 /* apply outgoing CDC or RNDIS filters */
1648 if (!eth_is_promisc (dev)) {
1649 u8 *dest = skb->data;
1650
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001651 if (is_multicast_ethaddr(dest)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02001652 u16 type;
1653
1654 /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
1655 * SET_ETHERNET_MULTICAST_FILTERS requests
1656 */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001657 if (is_broadcast_ethaddr(dest))
Remy Bohmerdf063442009-07-29 18:18:43 +02001658 type = USB_CDC_PACKET_TYPE_BROADCAST;
1659 else
1660 type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
1661 if (!(dev->cdc_filter & type)) {
1662 dev_kfree_skb_any (skb);
1663 return 0;
1664 }
1665 }
1666 /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
1667 }
1668
1669 spin_lock_irqsave(&dev->req_lock, flags);
1670 /*
1671 * this freelist can be empty if an interrupt triggered disconnect()
1672 * and reconfigured the gadget (shutting down this queue) after the
1673 * network stack decided to xmit but before we got the spinlock.
1674 */
1675 if (list_empty(&dev->tx_reqs)) {
1676 spin_unlock_irqrestore(&dev->req_lock, flags);
1677 return 1;
1678 }
1679
1680 req = container_of (dev->tx_reqs.next, struct usb_request, list);
1681 list_del (&req->list);
1682
1683 /* temporarily stop TX queue when the freelist empties */
1684 if (list_empty (&dev->tx_reqs))
1685 netif_stop_queue (net);
1686 spin_unlock_irqrestore(&dev->req_lock, flags);
1687
1688 /* no buffer copies needed, unless the network stack did it
1689 * or the hardware can't use skb buffers.
1690 * or there's not enough space for any RNDIS headers we need
1691 */
1692 if (rndis_active(dev)) {
1693 struct sk_buff *skb_rndis;
1694
1695 skb_rndis = skb_realloc_headroom (skb,
1696 sizeof (struct rndis_packet_msg_type));
1697 if (!skb_rndis)
1698 goto drop;
1699
1700 dev_kfree_skb_any (skb);
1701 skb = skb_rndis;
1702 rndis_add_hdr (skb);
1703 length = skb->len;
1704 }
1705 req->buf = skb->data;
1706 req->context = skb;
1707 req->complete = tx_complete;
1708
1709 /* use zlp framing on tx for strict CDC-Ether conformance,
1710 * though any robust network rx path ignores extra padding.
1711 * and some hardware doesn't like to write zlps.
1712 */
1713 req->zero = 1;
1714 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
1715 length++;
1716
1717 req->length = length;
1718
1719 /* throttle highspeed IRQ rate back slightly */
1720 if (gadget_is_dualspeed(dev->gadget))
1721 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
1722 ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
1723 : 0;
1724
1725 retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
1726 switch (retval) {
1727 default:
1728 DEBUG (dev, "tx queue err %d\n", retval);
1729 break;
1730 case 0:
1731 net->trans_start = jiffies;
1732 atomic_inc (&dev->tx_qlen);
1733 }
1734
1735 if (retval) {
1736drop:
1737 dev->stats.tx_dropped++;
1738 dev_kfree_skb_any (skb);
1739 spin_lock_irqsave(&dev->req_lock, flags);
1740 if (list_empty (&dev->tx_reqs))
1741 netif_start_queue (net);
1742 list_add (&req->list, &dev->tx_reqs);
1743 spin_unlock_irqrestore(&dev->req_lock, flags);
1744 }
1745 return 0;
1746}
1747
1748/*-------------------------------------------------------------------------*/
1749#endif
1750
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001751static void eth_unbind(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001752{
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001753 struct eth_dev *dev = get_gadget_data(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02001754
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04001755 debug("%s...\n", __func__);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001756 rndis_deregister(dev->rndis_config);
1757 rndis_exit();
Remy Bohmerdf063442009-07-29 18:18:43 +02001758
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001759 /* we've already been disconnected ... no i/o is active */
1760 if (dev->req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001761 usb_ep_free_request(gadget->ep0, dev->req);
Vitaly Kuzmichev90670702010-08-13 17:00:16 +04001762 dev->req = NULL;
1763 }
Remy Bohmerdf063442009-07-29 18:18:43 +02001764 if (dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001765 usb_ep_free_request(dev->status_ep, dev->stat_req);
Remy Bohmerdf063442009-07-29 18:18:43 +02001766 dev->stat_req = NULL;
1767 }
1768
1769 if (dev->tx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001770 usb_ep_free_request(dev->in_ep, dev->tx_req);
1771 dev->tx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001772 }
1773
1774 if (dev->rx_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001775 usb_ep_free_request(dev->out_ep, dev->rx_req);
1776 dev->rx_req = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +02001777 }
1778
1779/* unregister_netdev (dev->net);*/
1780/* free_netdev(dev->net);*/
1781
Lei Wen4af32732010-12-01 23:43:43 +08001782 dev->gadget = NULL;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001783 set_gadget_data(gadget, NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +02001784}
1785
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001786static void eth_disconnect(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001787{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001788 eth_reset_config(get_gadget_data(gadget));
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001789 /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
Remy Bohmerdf063442009-07-29 18:18:43 +02001790}
1791
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001792static void eth_suspend(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001793{
1794 /* Not used */
1795}
1796
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001797static void eth_resume(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +02001798{
1799 /* Not used */
1800}
1801
1802/*-------------------------------------------------------------------------*/
1803
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001804#ifdef CONFIG_USB_ETH_RNDIS
1805
1806/*
1807 * The interrupt endpoint is used in RNDIS to notify the host when messages
1808 * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
1809 * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
1810 * REMOTE_NDIS_KEEPALIVE_MSG.
1811 *
1812 * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
1813 * normally just one notification will be queued.
1814 */
1815
1816static void rndis_control_ack_complete(struct usb_ep *ep,
1817 struct usb_request *req)
1818{
1819 struct eth_dev *dev = ep->driver_data;
1820
1821 debug("%s...\n", __func__);
1822 if (req->status || req->actual != req->length)
1823 debug("rndis control ack complete --> %d, %d/%d\n",
1824 req->status, req->actual, req->length);
1825
1826 if (!l_ethdev.network_started) {
1827 if (rndis_get_state(dev->rndis_config)
1828 == RNDIS_DATA_INITIALIZED) {
1829 l_ethdev.network_started = 1;
1830 printf("USB RNDIS network up!\n");
1831 }
1832 }
1833
1834 req->context = NULL;
1835
1836 if (req != dev->stat_req)
1837 usb_ep_free_request(ep, req);
1838}
1839
1840static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
1841
1842static int rndis_control_ack(struct eth_device *net)
1843{
1844 struct eth_dev *dev = &l_ethdev;
1845 int length;
1846 struct usb_request *resp = dev->stat_req;
1847
1848 /* in case RNDIS calls this after disconnect */
1849 if (!dev->status) {
1850 debug("status ENODEV\n");
1851 return -ENODEV;
1852 }
1853
1854 /* in case queue length > 1 */
1855 if (resp->context) {
1856 resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
1857 if (!resp)
1858 return -ENOMEM;
1859 resp->buf = rndis_resp_buf;
1860 }
1861
1862 /*
1863 * Send RNDIS RESPONSE_AVAILABLE notification;
1864 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
1865 */
1866 resp->length = 8;
1867 resp->complete = rndis_control_ack_complete;
1868 resp->context = dev;
1869
1870 *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
1871 *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
1872
1873 length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
1874 if (length < 0) {
1875 resp->status = 0;
1876 rndis_control_ack_complete(dev->status_ep, resp);
1877 }
1878
1879 return 0;
1880}
1881
1882#else
1883
1884#define rndis_control_ack NULL
1885
1886#endif /* RNDIS */
1887
1888static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
1889{
1890 if (rndis_active(dev)) {
1891 rndis_set_param_medium(dev->rndis_config,
1892 NDIS_MEDIUM_802_3,
1893 BITRATE(dev->gadget)/100);
1894 rndis_signal_connect(dev->rndis_config);
1895 }
1896}
1897
1898static int eth_stop(struct eth_dev *dev)
1899{
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001900#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1901 unsigned long ts;
1902 unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
1903#endif
1904
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001905 if (rndis_active(dev)) {
1906 rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
1907 rndis_signal_disconnect(dev->rndis_config);
1908
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001909#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
1910 /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
1911 ts = get_timer(0);
1912 while (get_timer(ts) < timeout)
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05301913 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03001914#endif
1915
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001916 rndis_uninit(dev->rndis_config);
1917 dev->rndis = 0;
1918 }
1919
1920 return 0;
1921}
1922
1923/*-------------------------------------------------------------------------*/
1924
Remy Bohmerdf063442009-07-29 18:18:43 +02001925static int is_eth_addr_valid(char *str)
1926{
1927 if (strlen(str) == 17) {
1928 int i;
1929 char *p, *q;
1930 uchar ea[6];
1931
1932 /* see if it looks like an ethernet address */
1933
1934 p = str;
1935
1936 for (i = 0; i < 6; i++) {
1937 char term = (i == 5 ? '\0' : ':');
1938
1939 ea[i] = simple_strtol(p, &q, 16);
1940
1941 if ((q - p) != 2 || *q++ != term)
1942 break;
1943
1944 p = q;
1945 }
1946
Tom Riniadd21ca2012-10-31 13:30:41 +00001947 /* Now check the contents. */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001948 return is_valid_ethaddr(ea);
Remy Bohmerdf063442009-07-29 18:18:43 +02001949 }
1950 return 0;
1951}
1952
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001953static u8 nibble(unsigned char c)
Remy Bohmerdf063442009-07-29 18:18:43 +02001954{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001955 if (likely(isdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001956 return c - '0';
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001957 c = toupper(c);
1958 if (likely(isxdigit(c)))
Remy Bohmerdf063442009-07-29 18:18:43 +02001959 return 10 + c - 'A';
1960 return 0;
1961}
1962
1963static int get_ether_addr(const char *str, u8 *dev_addr)
1964{
1965 if (str) {
1966 unsigned i;
1967
1968 for (i = 0; i < 6; i++) {
1969 unsigned char num;
1970
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001971 if ((*str == '.') || (*str == ':'))
Remy Bohmerdf063442009-07-29 18:18:43 +02001972 str++;
1973 num = nibble(*str++) << 4;
1974 num |= (nibble(*str++));
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001975 dev_addr[i] = num;
Remy Bohmerdf063442009-07-29 18:18:43 +02001976 }
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001977 if (is_valid_ethaddr(dev_addr))
Remy Bohmerdf063442009-07-29 18:18:43 +02001978 return 0;
1979 }
1980 return 1;
1981}
1982
1983static int eth_bind(struct usb_gadget *gadget)
1984{
1985 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001986 u8 cdc = 1, zlp = 1, rndis = 1;
Remy Bohmerdf063442009-07-29 18:18:43 +02001987 struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001988 int status = -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02001989 int gcnum;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001990 u8 tmp[7];
Remy Bohmerdf063442009-07-29 18:18:43 +02001991
1992 /* these flags are only ever cleared; compiler take note */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02001993#ifndef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02001994 cdc = 0;
1995#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03001996#ifndef CONFIG_USB_ETH_RNDIS
1997 rndis = 0;
1998#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04001999 /*
2000 * Because most host side USB stacks handle CDC Ethernet, that
Remy Bohmerdf063442009-07-29 18:18:43 +02002001 * standard protocol is _strongly_ preferred for interop purposes.
2002 * (By everyone except Microsoft.)
2003 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002004 if (gadget_is_pxa(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002005 /* pxa doesn't support altsettings */
2006 cdc = 0;
2007 } else if (gadget_is_musbhdrc(gadget)) {
2008 /* reduce tx dma overhead by avoiding special cases */
2009 zlp = 0;
2010 } else if (gadget_is_sh(gadget)) {
2011 /* sh doesn't support multiple interfaces or configs */
2012 cdc = 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002013 rndis = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002014 } else if (gadget_is_sa1100(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002015 /* hardware can't write zlps */
2016 zlp = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002017 /*
2018 * sa1100 CAN do CDC, without status endpoint ... we use
Remy Bohmerdf063442009-07-29 18:18:43 +02002019 * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
2020 */
2021 cdc = 0;
2022 }
2023
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002024 gcnum = usb_gadget_controller_number(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002025 if (gcnum >= 0)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002026 device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
Remy Bohmerdf063442009-07-29 18:18:43 +02002027 else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002028 /*
2029 * can't assume CDC works. don't want to default to
Remy Bohmerdf063442009-07-29 18:18:43 +02002030 * anything less functional on CDC-capable hardware,
2031 * so we fail in this case.
2032 */
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002033 error("controller '%s' not recognized",
Remy Bohmerdf063442009-07-29 18:18:43 +02002034 gadget->name);
2035 return -ENODEV;
2036 }
2037
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002038 /*
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002039 * If there's an RNDIS configuration, that's what Windows wants to
2040 * be using ... so use these product IDs here and in the "linux.inf"
2041 * needed to install MSFT drivers. Current Linux kernels will use
2042 * the second configuration if it's CDC Ethernet, and need some help
2043 * to choose the right configuration otherwise.
Remy Bohmerdf063442009-07-29 18:18:43 +02002044 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002045 if (rndis) {
2046#if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
Remy Bohmerdf063442009-07-29 18:18:43 +02002047 device_desc.idVendor =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002048 __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
Remy Bohmerdf063442009-07-29 18:18:43 +02002049 device_desc.idProduct =
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002050 __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
2051#else
2052 device_desc.idVendor =
2053 __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
2054 device_desc.idProduct =
2055 __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
2056#endif
2057 sprintf(product_desc, "RNDIS/%s", driver_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002058
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002059 /*
2060 * CDC subset ... recognized by Linux since 2.4.10, but Windows
2061 * drivers aren't widely available. (That may be improved by
2062 * supporting one submode of the "SAFE" variant of MDLM.)
2063 */
2064 } else {
Remy Bohmerdf063442009-07-29 18:18:43 +02002065#if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002066 device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
2067 device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
2068#else
2069 if (!cdc) {
2070 device_desc.idVendor =
2071 __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
2072 device_desc.idProduct =
2073 __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
2074 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002075#endif
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002076 }
2077 /* support optional vendor/distro customization */
Remy Bohmerdf063442009-07-29 18:18:43 +02002078 if (bcdDevice)
2079 device_desc.bcdDevice = cpu_to_le16(bcdDevice);
2080 if (iManufacturer)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002081 strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
Remy Bohmerdf063442009-07-29 18:18:43 +02002082 if (iProduct)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002083 strlcpy(product_desc, iProduct, sizeof product_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002084 if (iSerialNumber) {
2085 device_desc.iSerialNumber = STRING_SERIALNUMBER,
Vitaly Kuzmichev214d7b02010-08-13 17:00:45 +04002086 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
Remy Bohmerdf063442009-07-29 18:18:43 +02002087 }
2088
2089 /* all we really need is bulk IN/OUT */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002090 usb_ep_autoconfig_reset(gadget);
2091 in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002092 if (!in_ep) {
2093autoconf_fail:
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002094 error("can't autoconfigure on %s\n",
Remy Bohmerdf063442009-07-29 18:18:43 +02002095 gadget->name);
2096 return -ENODEV;
2097 }
2098 in_ep->driver_data = in_ep; /* claim */
2099
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002100 out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002101 if (!out_ep)
2102 goto autoconf_fail;
2103 out_ep->driver_data = out_ep; /* claim */
2104
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002105#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002106 /*
2107 * CDC Ethernet control interface doesn't require a status endpoint.
Remy Bohmerdf063442009-07-29 18:18:43 +02002108 * Since some hosts expect one, try to allocate one anyway.
2109 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002110 if (cdc || rndis) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002111 status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
Remy Bohmerdf063442009-07-29 18:18:43 +02002112 if (status_ep) {
2113 status_ep->driver_data = status_ep; /* claim */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002114 } else if (rndis) {
2115 error("can't run RNDIS on %s", gadget->name);
2116 return -ENODEV;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002117#ifdef CONFIG_USB_ETH_CDC
Remy Bohmerdf063442009-07-29 18:18:43 +02002118 } else if (cdc) {
2119 control_intf.bNumEndpoints = 0;
2120 /* FIXME remove endpoint from descriptor list */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002121#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002122 }
2123 }
2124#endif
2125
2126 /* one config: cdc, else minimal subset */
2127 if (!cdc) {
2128 eth_config.bNumInterfaces = 1;
2129 eth_config.iConfiguration = STRING_SUBSET;
2130
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002131 /*
2132 * use functions to set these up, in case we're built to work
Remy Bohmerdf063442009-07-29 18:18:43 +02002133 * with multiple controllers and must override CDC Ethernet.
2134 */
2135 fs_subset_descriptors();
2136 hs_subset_descriptors();
2137 }
2138
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002139 usb_gadget_set_selfpowered(gadget);
Remy Bohmerdf063442009-07-29 18:18:43 +02002140
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002141 /* For now RNDIS is always a second config */
2142 if (rndis)
2143 device_desc.bNumConfigurations = 2;
2144
Remy Bohmerdf063442009-07-29 18:18:43 +02002145 if (gadget_is_dualspeed(gadget)) {
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002146 if (rndis)
2147 dev_qualifier.bNumConfigurations = 2;
2148 else if (!cdc)
Remy Bohmerdf063442009-07-29 18:18:43 +02002149 dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
2150
2151 /* assumes ep0 uses the same value for both speeds ... */
2152 dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
2153
2154 /* and that all endpoints are dual-speed */
2155 hs_source_desc.bEndpointAddress =
2156 fs_source_desc.bEndpointAddress;
2157 hs_sink_desc.bEndpointAddress =
2158 fs_sink_desc.bEndpointAddress;
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002159#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002160 if (status_ep)
2161 hs_status_desc.bEndpointAddress =
2162 fs_status_desc.bEndpointAddress;
2163#endif
2164 }
2165
2166 if (gadget_is_otg(gadget)) {
2167 otg_descriptor.bmAttributes |= USB_OTG_HNP,
2168 eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2169 eth_config.bMaxPower = 4;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002170#ifdef CONFIG_USB_ETH_RNDIS
2171 rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
2172 rndis_config.bMaxPower = 4;
2173#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002174 }
2175
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002176
2177 /* network device setup */
Remy Bohmerdf063442009-07-29 18:18:43 +02002178 dev->net = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002179
2180 dev->cdc = cdc;
2181 dev->zlp = zlp;
2182
2183 dev->in_ep = in_ep;
2184 dev->out_ep = out_ep;
2185 dev->status_ep = status_ep;
2186
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002187 /*
2188 * Module params for these addresses should come from ID proms.
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002189 * The host side address is used with CDC and RNDIS, and commonly
Remy Bohmerdf063442009-07-29 18:18:43 +02002190 * ends up in a persistent config database. It's not clear if
2191 * host side code for the SAFE thing cares -- its original BLAN
2192 * thing didn't, Sharp never assigned those addresses on Zaurii.
2193 */
2194 get_ether_addr(dev_addr, dev->net->enetaddr);
2195
2196 memset(tmp, 0, sizeof(tmp));
2197 memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
2198
2199 get_ether_addr(host_addr, dev->host_mac);
2200
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002201 sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
2202 dev->host_mac[0], dev->host_mac[1],
2203 dev->host_mac[2], dev->host_mac[3],
2204 dev->host_mac[4], dev->host_mac[5]);
Remy Bohmerdf063442009-07-29 18:18:43 +02002205
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002206 if (rndis) {
2207 status = rndis_init();
2208 if (status < 0) {
2209 error("can't init RNDIS, %d", status);
2210 goto fail;
2211 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002212 }
2213
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002214 /*
2215 * use PKTSIZE (or aligned... from u-boot) and set
2216 * wMaxSegmentSize accordingly
2217 */
Remy Bohmerdf063442009-07-29 18:18:43 +02002218 dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
2219
2220 /* preallocate control message data and buffer */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002221 dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002222 if (!dev->req)
2223 goto fail;
2224 dev->req->buf = control_req;
2225 dev->req->complete = eth_setup_complete;
2226
2227 /* ... and maybe likewise for status transfer */
Lukasz Dalek284c4cf2012-10-02 17:04:31 +02002228#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
Remy Bohmerdf063442009-07-29 18:18:43 +02002229 if (dev->status_ep) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002230 dev->stat_req = usb_ep_alloc_request(dev->status_ep,
2231 GFP_KERNEL);
Remy Bohmerdf063442009-07-29 18:18:43 +02002232 if (!dev->stat_req) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002233 usb_ep_free_request(dev->status_ep, dev->req);
Remy Bohmerdf063442009-07-29 18:18:43 +02002234
2235 goto fail;
2236 }
Vitaly Kuzmichevef8d8d72010-08-13 17:01:06 +04002237 dev->stat_req->buf = status_req;
Remy Bohmerdf063442009-07-29 18:18:43 +02002238 dev->stat_req->context = NULL;
2239 }
2240#endif
2241
2242 /* finish hookup to lower layer ... */
2243 dev->gadget = gadget;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002244 set_gadget_data(gadget, dev);
Remy Bohmerdf063442009-07-29 18:18:43 +02002245 gadget->ep0->driver_data = dev;
2246
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002247 /*
2248 * two kinds of host-initiated state changes:
Remy Bohmerdf063442009-07-29 18:18:43 +02002249 * - iff DATA transfer is active, carrier is "on"
2250 * - tx queueing enabled if open *and* carrier is "on"
2251 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002252
2253 printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
2254 out_ep->name, in_ep->name,
2255 status_ep ? " STATUS " : "",
2256 status_ep ? status_ep->name : ""
2257 );
2258 printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2259 dev->net->enetaddr[0], dev->net->enetaddr[1],
2260 dev->net->enetaddr[2], dev->net->enetaddr[3],
2261 dev->net->enetaddr[4], dev->net->enetaddr[5]);
2262
2263 if (cdc || rndis)
2264 printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2265 dev->host_mac[0], dev->host_mac[1],
2266 dev->host_mac[2], dev->host_mac[3],
2267 dev->host_mac[4], dev->host_mac[5]);
2268
2269 if (rndis) {
2270 u32 vendorID = 0;
2271
2272 /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
2273
2274 dev->rndis_config = rndis_register(rndis_control_ack);
2275 if (dev->rndis_config < 0) {
2276fail0:
2277 eth_unbind(gadget);
2278 debug("RNDIS setup failed\n");
2279 status = -ENODEV;
2280 goto fail;
2281 }
2282
2283 /* these set up a lot of the OIDs that RNDIS needs */
2284 rndis_set_host_mac(dev->rndis_config, dev->host_mac);
2285 if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
2286 &dev->stats, &dev->cdc_filter))
2287 goto fail0;
2288 if (rndis_set_param_vendor(dev->rndis_config, vendorID,
2289 manufacturer))
2290 goto fail0;
2291 if (rndis_set_param_medium(dev->rndis_config,
2292 NDIS_MEDIUM_802_3, 0))
2293 goto fail0;
2294 printf("RNDIS ready\n");
2295 }
Remy Bohmerdf063442009-07-29 18:18:43 +02002296 return 0;
2297
2298fail:
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002299 error("%s failed, status = %d", __func__, status);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002300 eth_unbind(gadget);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002301 return status;
Remy Bohmerdf063442009-07-29 18:18:43 +02002302}
2303
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002304/*-------------------------------------------------------------------------*/
2305
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002306static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
Remy Bohmerdf063442009-07-29 18:18:43 +02002307{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002308 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002309 struct usb_gadget *gadget;
2310 unsigned long ts;
2311 unsigned long timeout = USB_CONNECT_TIMEOUT;
2312
2313 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002314 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002315 goto fail;
2316 }
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002317
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302318 board_usb_init(0, USB_INIT_DEVICE);
2319
Vitaly Kuzmichevbe0afbf2010-12-28 16:59:32 +03002320 /* Configure default mac-addresses for the USB ethernet device */
2321#ifdef CONFIG_USBNET_DEV_ADDR
2322 strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
2323#endif
2324#ifdef CONFIG_USBNET_HOST_ADDR
2325 strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
2326#endif
2327 /* Check if the user overruled the MAC addresses */
2328 if (getenv("usbnet_devaddr"))
2329 strlcpy(dev_addr, getenv("usbnet_devaddr"),
2330 sizeof(dev_addr));
2331
2332 if (getenv("usbnet_hostaddr"))
2333 strlcpy(host_addr, getenv("usbnet_hostaddr"),
2334 sizeof(host_addr));
2335
2336 if (!is_eth_addr_valid(dev_addr)) {
2337 error("Need valid 'usbnet_devaddr' to be set");
2338 goto fail;
2339 }
2340 if (!is_eth_addr_valid(host_addr)) {
2341 error("Need valid 'usbnet_hostaddr' to be set");
2342 goto fail;
2343 }
2344
Lei Wen4af32732010-12-01 23:43:43 +08002345 if (usb_gadget_register_driver(&eth_driver) < 0)
2346 goto fail;
Remy Bohmerdf063442009-07-29 18:18:43 +02002347
2348 dev->network_started = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002349
2350 packet_received = 0;
2351 packet_sent = 0;
2352
2353 gadget = dev->gadget;
2354 usb_gadget_connect(gadget);
2355
2356 if (getenv("cdc_connect_timeout"))
2357 timeout = simple_strtoul(getenv("cdc_connect_timeout"),
2358 NULL, 10) * CONFIG_SYS_HZ;
2359 ts = get_timer(0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002360 while (!l_ethdev.network_started) {
Remy Bohmerdf063442009-07-29 18:18:43 +02002361 /* Handle control-c and timeouts */
2362 if (ctrlc() || (get_timer(ts) > timeout)) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002363 error("The remote end did not respond in time.");
Remy Bohmerdf063442009-07-29 18:18:43 +02002364 goto fail;
2365 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302366 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002367 }
2368
Vitaly Kuzmicheve4bd3862010-09-22 13:13:56 +04002369 packet_received = 0;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002370 rx_submit(dev, dev->rx_req, 0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002371 return 0;
2372fail:
2373 return -1;
2374}
2375
Joe Hershbergere4e04882012-05-22 18:36:19 +00002376static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
Remy Bohmerdf063442009-07-29 18:18:43 +02002377{
2378 int retval;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002379 void *rndis_pkt = NULL;
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002380 struct eth_dev *dev = &l_ethdev;
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002381 struct usb_request *req = dev->tx_req;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002382 unsigned long ts;
2383 unsigned long timeout = USB_CONNECT_TIMEOUT;
Remy Bohmerdf063442009-07-29 18:18:43 +02002384
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002385 debug("%s:...\n", __func__);
Remy Bohmerdf063442009-07-29 18:18:43 +02002386
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002387 /* new buffer is needed to include RNDIS header */
2388 if (rndis_active(dev)) {
2389 rndis_pkt = malloc(length +
2390 sizeof(struct rndis_packet_msg_type));
2391 if (!rndis_pkt) {
2392 error("No memory to alloc RNDIS packet");
2393 goto drop;
2394 }
2395 rndis_add_hdr(rndis_pkt, length);
2396 memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
Joe Hershbergere4e04882012-05-22 18:36:19 +00002397 packet, length);
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002398 packet = rndis_pkt;
2399 length += sizeof(struct rndis_packet_msg_type);
2400 }
Joe Hershbergere4e04882012-05-22 18:36:19 +00002401 req->buf = packet;
Remy Bohmerdf063442009-07-29 18:18:43 +02002402 req->context = NULL;
2403 req->complete = tx_complete;
2404
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002405 /*
2406 * use zlp framing on tx for strict CDC-Ether conformance,
Remy Bohmerdf063442009-07-29 18:18:43 +02002407 * though any robust network rx path ignores extra padding.
2408 * and some hardware doesn't like to write zlps.
2409 */
2410 req->zero = 1;
2411 if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2412 length++;
2413
2414 req->length = length;
2415#if 0
2416 /* throttle highspeed IRQ rate back slightly */
2417 if (gadget_is_dualspeed(dev->gadget))
2418 req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2419 ? ((dev->tx_qlen % qmult) != 0) : 0;
2420#endif
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002421 dev->tx_qlen = 1;
2422 ts = get_timer(0);
2423 packet_sent = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002424
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002425 retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Remy Bohmerdf063442009-07-29 18:18:43 +02002426
2427 if (!retval)
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002428 debug("%s: packet queued\n", __func__);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002429 while (!packet_sent) {
2430 if (get_timer(ts) > timeout) {
2431 printf("timeout sending packets to usb ethernet\n");
2432 return -1;
2433 }
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302434 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002435 }
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002436 if (rndis_pkt)
2437 free(rndis_pkt);
Remy Bohmerdf063442009-07-29 18:18:43 +02002438
2439 return 0;
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002440drop:
2441 dev->stats.tx_dropped++;
2442 return -ENOMEM;
Remy Bohmerdf063442009-07-29 18:18:43 +02002443}
2444
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002445static int usb_eth_recv(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002446{
2447 struct eth_dev *dev = &l_ethdev;
2448
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302449 usb_gadget_handle_interrupts(0);
Remy Bohmerdf063442009-07-29 18:18:43 +02002450
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002451 if (packet_received) {
2452 debug("%s: packet received\n", __func__);
2453 if (dev->rx_req) {
Joe Hershberger9f09a362015-04-08 01:41:06 -05002454 net_process_received_packet(net_rx_packets[0],
2455 dev->rx_req->length);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002456 packet_received = 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002457
Vitaly Kuzmichev2907b2c2010-09-22 13:13:55 +04002458 rx_submit(dev, dev->rx_req, 0);
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002459 } else
2460 error("dev->rx_req invalid");
Remy Bohmerdf063442009-07-29 18:18:43 +02002461 }
2462 return 0;
2463}
2464
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002465void usb_eth_halt(struct eth_device *netdev)
Remy Bohmerdf063442009-07-29 18:18:43 +02002466{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002467 struct eth_dev *dev = &l_ethdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002468
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002469 if (!netdev) {
Vitaly Kuzmichevae5f9322010-08-13 16:57:51 +04002470 error("received NULL ptr");
Remy Bohmerdf063442009-07-29 18:18:43 +02002471 return;
2472 }
2473
Lei Wen4af32732010-12-01 23:43:43 +08002474 /* If the gadget not registered, simple return */
2475 if (!dev->gadget)
2476 return;
2477
Vitaly Kuzmichev3433c332011-02-11 18:18:35 +03002478 /*
2479 * Some USB controllers may need additional deinitialization here
2480 * before dropping pull-up (also due to hardware issues).
2481 * For example: unhandled interrupt with status stage started may
2482 * bring the controller to fully broken state (until board reset).
2483 * There are some variants to debug and fix such cases:
2484 * 1) In the case of RNDIS connection eth_stop can perform additional
2485 * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
2486 * 2) 'pullup' callback in your UDC driver can be improved to perform
2487 * this deinitialization.
2488 */
Vitaly Kuzmichev216c4472011-02-11 18:18:34 +03002489 eth_stop(dev);
2490
Remy Bohmerdf063442009-07-29 18:18:43 +02002491 usb_gadget_disconnect(dev->gadget);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002492
2493 /* Clear pending interrupt */
2494 if (dev->network_started) {
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +05302495 usb_gadget_handle_interrupts(0);
Vitaly Kuzmichev08d4dd72011-02-11 18:18:31 +03002496 dev->network_started = 0;
2497 }
2498
Lei Wen4af32732010-12-01 23:43:43 +08002499 usb_gadget_unregister_driver(&eth_driver);
Kishon Vijay Abraham I8db25202015-08-19 13:49:46 +05302500 board_usb_cleanup(0, USB_INIT_DEVICE);
Remy Bohmerdf063442009-07-29 18:18:43 +02002501}
2502
2503static struct usb_gadget_driver eth_driver = {
2504 .speed = DEVSPEED,
2505
2506 .bind = eth_bind,
2507 .unbind = eth_unbind,
2508
2509 .setup = eth_setup,
Kishon Vijay Abraham I2b9d1632015-08-19 13:49:48 +05302510 .reset = eth_disconnect,
Remy Bohmerdf063442009-07-29 18:18:43 +02002511 .disconnect = eth_disconnect,
2512
2513 .suspend = eth_suspend,
2514 .resume = eth_resume,
2515};
2516
2517int usb_eth_initialize(bd_t *bi)
2518{
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +04002519 struct eth_device *netdev = &l_netdev;
Remy Bohmerdf063442009-07-29 18:18:43 +02002520
Vitaly Kuzmicheva36cff12010-12-28 16:59:31 +03002521 strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
Remy Bohmerdf063442009-07-29 18:18:43 +02002522
2523 netdev->init = usb_eth_init;
2524 netdev->send = usb_eth_send;
2525 netdev->recv = usb_eth_recv;
2526 netdev->halt = usb_eth_halt;
2527
2528#ifdef CONFIG_MCAST_TFTP
2529 #error not supported
2530#endif
Remy Bohmerdf063442009-07-29 18:18:43 +02002531 eth_register(netdev);
2532 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +02002533}