2009-07-29 16:18:43 +00:00
|
|
|
/*
|
|
|
|
* ether.c -- Ethernet gadget driver, with CDC and non-CDC options
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2005,2008 David Brownell
|
|
|
|
* Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
|
|
|
|
* Copyright (C) 2008 Nokia Corporation
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2015-11-09 06:47:45 +00:00
|
|
|
#include <console.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2011-02-11 15:18:32 +00:00
|
|
|
#include <linux/netdevice.h>
|
2009-07-29 16:18:43 +00:00
|
|
|
#include <linux/usb/ch9.h>
|
|
|
|
#include <linux/usb/cdc.h>
|
|
|
|
#include <linux/usb/gadget.h>
|
|
|
|
#include <net.h>
|
2015-08-19 08:19:46 +00:00
|
|
|
#include <usb.h>
|
2011-02-11 15:18:34 +00:00
|
|
|
#include <malloc.h>
|
2015-09-02 23:24:58 +00:00
|
|
|
#include <memalign.h>
|
2009-07-29 16:18:43 +00:00
|
|
|
#include <linux/ctype.h>
|
|
|
|
|
|
|
|
#include "gadget_chips.h"
|
2011-02-11 15:18:34 +00:00
|
|
|
#include "rndis.h"
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2016-11-18 05:19:12 +00:00
|
|
|
#include <dm.h>
|
2016-11-18 05:39:15 +00:00
|
|
|
#include <dm/lists.h>
|
2016-11-18 05:19:12 +00:00
|
|
|
#include <dm/uclass-internal.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
|
2010-12-28 13:59:31 +00:00
|
|
|
#define USB_NET_NAME "usb_ether"
|
2010-08-13 12:57:51 +00:00
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
#define atomic_read
|
|
|
|
extern struct platform_data brd;
|
|
|
|
|
|
|
|
|
|
|
|
unsigned packet_received, packet_sent;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ethernet gadget driver -- with CDC and non-CDC options
|
|
|
|
* Builds on hardware support for a full duplex link.
|
|
|
|
*
|
|
|
|
* CDC Ethernet is the standard USB solution for sending Ethernet frames
|
|
|
|
* using USB. Real hardware tends to use the same framing protocol but look
|
|
|
|
* different for control features. This driver strongly prefers to use
|
|
|
|
* this USB-IF standard as its open-systems interoperability solution;
|
|
|
|
* most host side USB stacks (except from Microsoft) support it.
|
|
|
|
*
|
|
|
|
* This is sometimes called "CDC ECM" (Ethernet Control Model) to support
|
|
|
|
* TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
|
|
|
|
* "CDC EEM" (Ethernet Emulation Model) is starting to spread.
|
|
|
|
*
|
|
|
|
* There's some hardware that can't talk CDC ECM. We make that hardware
|
|
|
|
* implement a "minimalist" vendor-agnostic CDC core: same framing, but
|
|
|
|
* link-level setup only requires activating the configuration. Only the
|
|
|
|
* endpoint descriptors, and product/vendor IDs, are relevant; no control
|
|
|
|
* operations are available. Linux supports it, but other host operating
|
|
|
|
* systems may not. (This is a subset of CDC Ethernet.)
|
|
|
|
*
|
|
|
|
* It turns out that if you add a few descriptors to that "CDC Subset",
|
|
|
|
* (Windows) host side drivers from MCCI can treat it as one submode of
|
|
|
|
* a proprietary scheme called "SAFE" ... without needing to know about
|
|
|
|
* specific product/vendor IDs. So we do that, making it easier to use
|
|
|
|
* those MS-Windows drivers. Those added descriptors make it resemble a
|
|
|
|
* CDC MDLM device, but they don't change device behavior at all. (See
|
|
|
|
* MCCI Engineering report 950198 "SAFE Networking Functions".)
|
|
|
|
*
|
|
|
|
* A third option is also in use. Rather than CDC Ethernet, or something
|
|
|
|
* simpler, Microsoft pushes their own approach: RNDIS. The published
|
|
|
|
* RNDIS specs are ambiguous and appear to be incomplete, and are also
|
|
|
|
* needlessly complex. They borrow more from CDC ACM than CDC ECM.
|
|
|
|
*/
|
|
|
|
#define ETH_ALEN 6 /* Octets in one ethernet addr */
|
|
|
|
#define ETH_HLEN 14 /* Total octets in header. */
|
|
|
|
#define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
|
|
|
|
#define ETH_DATA_LEN 1500 /* Max. octets in payload */
|
|
|
|
#define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
|
|
|
|
|
|
|
|
#define DRIVER_DESC "Ethernet Gadget"
|
|
|
|
/* Based on linux 2.6.27 version */
|
|
|
|
#define DRIVER_VERSION "May Day 2005"
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static const char driver_desc[] = DRIVER_DESC;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
#define RX_EXTRA 20 /* guard against rx overflows */
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifndef CONFIG_USB_ETH_RNDIS
|
|
|
|
#define rndis_uninit(x) do {} while (0)
|
|
|
|
#define rndis_deregister(c) do {} while (0)
|
|
|
|
#define rndis_exit() do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
|
2009-07-29 16:18:43 +00:00
|
|
|
#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
|
|
|
|
|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
|
|
|
|
|USB_CDC_PACKET_TYPE_PROMISCUOUS \
|
|
|
|
|USB_CDC_PACKET_TYPE_DIRECTED)
|
|
|
|
|
|
|
|
#define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
|
|
|
|
|
2011-02-11 15:18:33 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
struct eth_dev {
|
|
|
|
struct usb_gadget *gadget;
|
|
|
|
struct usb_request *req; /* for control responses */
|
2011-02-11 15:18:34 +00:00
|
|
|
struct usb_request *stat_req; /* for cdc & rndis status */
|
2016-11-18 05:19:12 +00:00
|
|
|
#ifdef CONFIG_DM_USB
|
|
|
|
struct udevice *usb_udev;
|
|
|
|
#endif
|
2011-02-11 15:18:33 +00:00
|
|
|
|
|
|
|
u8 config;
|
|
|
|
struct usb_ep *in_ep, *out_ep, *status_ep;
|
|
|
|
const struct usb_endpoint_descriptor
|
|
|
|
*in, *out, *status;
|
|
|
|
|
|
|
|
struct usb_request *tx_req, *rx_req;
|
|
|
|
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
2011-02-11 15:18:33 +00:00
|
|
|
struct eth_device *net;
|
2016-11-18 05:39:15 +00:00
|
|
|
#else
|
|
|
|
struct udevice *net;
|
|
|
|
#endif
|
2011-02-11 15:18:33 +00:00
|
|
|
struct net_device_stats stats;
|
|
|
|
unsigned int tx_qlen;
|
|
|
|
|
|
|
|
unsigned zlp:1;
|
|
|
|
unsigned cdc:1;
|
2011-02-11 15:18:34 +00:00
|
|
|
unsigned rndis:1;
|
2011-02-11 15:18:33 +00:00
|
|
|
unsigned suspended:1;
|
|
|
|
unsigned network_started:1;
|
|
|
|
u16 cdc_filter;
|
|
|
|
unsigned long todo;
|
|
|
|
int mtu;
|
|
|
|
#define WORK_RX_MEMORY 0
|
2011-02-11 15:18:34 +00:00
|
|
|
int rndis_config;
|
2011-02-11 15:18:33 +00:00
|
|
|
u8 host_mac[ETH_ALEN];
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This version autoconfigures as much as possible at run-time.
|
|
|
|
*
|
|
|
|
* It also ASSUMES a self-powered device, without remote wakeup,
|
|
|
|
* although remote wakeup support would make sense.
|
|
|
|
*/
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
2016-11-18 05:36:13 +00:00
|
|
|
struct ether_priv {
|
|
|
|
struct eth_dev ethdev;
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
2016-11-18 05:36:13 +00:00
|
|
|
struct eth_device netdev;
|
2016-11-18 05:39:15 +00:00
|
|
|
#else
|
|
|
|
struct udevice *netdev;
|
|
|
|
#endif
|
2016-11-18 05:36:13 +00:00
|
|
|
struct usb_gadget_driver eth_driver;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ether_priv eth_priv;
|
|
|
|
struct ether_priv *l_priv = ð_priv;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* "main" config is either CDC, or its simple subset */
|
|
|
|
static inline int is_cdc(struct eth_dev *dev)
|
|
|
|
{
|
2012-10-02 15:04:31 +00:00
|
|
|
#if !defined(CONFIG_USB_ETH_SUBSET)
|
2009-07-29 16:18:43 +00:00
|
|
|
return 1; /* only cdc possible */
|
2012-10-02 15:04:31 +00:00
|
|
|
#elif !defined(CONFIG_USB_ETH_CDC)
|
2009-07-29 16:18:43 +00:00
|
|
|
return 0; /* only subset possible */
|
|
|
|
#else
|
|
|
|
return dev->cdc; /* depends on what hardware we found */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
/* "secondary" RNDIS config may sometimes be activated */
|
|
|
|
static inline int rndis_active(struct eth_dev *dev)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
return dev->rndis;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
|
|
|
|
#define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev))
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
#define DEFAULT_QLEN 2 /* double buffering by default */
|
|
|
|
|
|
|
|
/* peak bulk transfer bits-per-second */
|
|
|
|
#define HS_BPS (13 * 512 * 8 * 1000 * 8)
|
|
|
|
#define FS_BPS (19 * 64 * 1 * 1000 * 8)
|
|
|
|
|
|
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
|
|
|
#define DEVSPEED USB_SPEED_HIGH
|
|
|
|
|
2010-08-12 12:44:40 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_QMULT
|
|
|
|
#define qmult CONFIG_USB_ETH_QMULT
|
|
|
|
#else
|
|
|
|
#define qmult 5
|
|
|
|
#endif
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
/* for dual-speed hardware, use deeper queues at highspeed */
|
|
|
|
#define qlen(gadget) \
|
|
|
|
(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
|
|
|
|
|
|
|
|
static inline int BITRATE(struct usb_gadget *g)
|
|
|
|
{
|
|
|
|
return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* full speed (low speed doesn't do bulk) */
|
|
|
|
|
|
|
|
#define qmult 1
|
|
|
|
|
|
|
|
#define DEVSPEED USB_SPEED_FULL
|
|
|
|
|
|
|
|
#define qlen(gadget) DEFAULT_QLEN
|
|
|
|
|
|
|
|
static inline int BITRATE(struct usb_gadget *g)
|
|
|
|
{
|
|
|
|
return FS_BPS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
|
2009-07-29 16:18:43 +00:00
|
|
|
* Instead: allocate your own, using normal USB-IF procedures.
|
|
|
|
*/
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* Thanks to NetChip Technologies for donating this product ID.
|
2009-07-29 16:18:43 +00:00
|
|
|
* It's for devices with only CDC Ethernet configurations.
|
|
|
|
*/
|
|
|
|
#define CDC_VENDOR_NUM 0x0525 /* NetChip */
|
|
|
|
#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* For hardware that can't talk CDC, we use the same vendor ID that
|
2009-07-29 16:18:43 +00:00
|
|
|
* ARM Linux has used for ethernet-over-usb, both with sa1100 and
|
|
|
|
* with pxa250. We're protocol-compatible, if the host-side drivers
|
|
|
|
* use the endpoint descriptors. bcdDevice (version) is nonzero, so
|
|
|
|
* drivers that need to hard-wire endpoint numbers have a hook.
|
|
|
|
*
|
|
|
|
* The protocol is a minimal subset of CDC Ether, which works on any bulk
|
|
|
|
* hardware that's not deeply broken ... even on hardware that can't talk
|
|
|
|
* RNDIS (like SA-1100, with no interrupt endpoint, or anything that
|
|
|
|
* doesn't handle control-OUT).
|
|
|
|
*/
|
2011-02-11 15:18:34 +00:00
|
|
|
#define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */
|
|
|
|
#define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For hardware that can talk RNDIS and either of the above protocols,
|
|
|
|
* use this ID ... the windows INF files will know it. Unless it's
|
|
|
|
* used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
|
|
|
|
* the non-RNDIS configuration.
|
|
|
|
*/
|
|
|
|
#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
|
|
|
|
#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* Some systems will want different product identifers published in the
|
2009-07-29 16:18:43 +00:00
|
|
|
* device descriptor, either numbers or strings or both. These string
|
|
|
|
* parameters are in UTF-8 (superset of ASCII's 7 bit characters).
|
|
|
|
*/
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
/*
|
|
|
|
* Emulating them in eth_bind:
|
|
|
|
* static ushort idVendor;
|
|
|
|
* static ushort idProduct;
|
|
|
|
*/
|
|
|
|
|
2017-09-07 07:15:08 +00:00
|
|
|
#if defined(CONFIG_USB_GADGET_MANUFACTURER)
|
|
|
|
static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER;
|
2009-07-29 16:18:43 +00:00
|
|
|
#else
|
2016-02-06 03:30:11 +00:00
|
|
|
static char *iManufacturer = "U-Boot";
|
2009-07-29 16:18:43 +00:00
|
|
|
#endif
|
2011-02-11 15:18:34 +00:00
|
|
|
|
|
|
|
/* These probably need to be configurable. */
|
|
|
|
static ushort bcdDevice;
|
2009-07-29 16:18:43 +00:00
|
|
|
static char *iProduct;
|
|
|
|
static char *iSerialNumber;
|
2011-02-11 15:18:34 +00:00
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
static char dev_addr[18];
|
2011-02-11 15:18:34 +00:00
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
static char host_addr[18];
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
|
2009-07-29 16:18:43 +00:00
|
|
|
* ep0 implementation: descriptors, config management, setup().
|
|
|
|
* also optional class-specific notification interrupt transfer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DESCRIPTORS ... most are static, but strings and (full) configuration
|
|
|
|
* descriptors are built on demand. For now we do either full CDC, or
|
2011-02-11 15:18:34 +00:00
|
|
|
* our simple subset, with RNDIS as an optional second configuration.
|
|
|
|
*
|
|
|
|
* RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
|
|
|
|
* the class descriptors match a modem (they're ignored; it's really just
|
|
|
|
* Ethernet functionality), they don't need the NOP altsetting, and the
|
|
|
|
* status transfer endpoint isn't optional.
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define STRING_MANUFACTURER 1
|
|
|
|
#define STRING_PRODUCT 2
|
|
|
|
#define STRING_ETHADDR 3
|
|
|
|
#define STRING_DATA 4
|
|
|
|
#define STRING_CONTROL 5
|
2011-02-11 15:18:34 +00:00
|
|
|
#define STRING_RNDIS_CONTROL 6
|
2009-07-29 16:18:43 +00:00
|
|
|
#define STRING_CDC 7
|
|
|
|
#define STRING_SUBSET 8
|
2011-02-11 15:18:34 +00:00
|
|
|
#define STRING_RNDIS 9
|
2009-07-29 16:18:43 +00:00
|
|
|
#define STRING_SERIALNUMBER 10
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
/* holds our biggest descriptor (or RNDIS response) */
|
2009-07-29 16:18:43 +00:00
|
|
|
#define USB_BUFSIZ 256
|
|
|
|
|
|
|
|
/*
|
2011-02-11 15:18:34 +00:00
|
|
|
* This device advertises one configuration, eth_config, unless RNDIS
|
|
|
|
* is enabled (rndis_config) on hardware supporting at least two configs.
|
|
|
|
*
|
|
|
|
* NOTE: Controllers like superh_udc should probably be able to use
|
|
|
|
* an RNDIS-only configuration.
|
2009-07-29 16:18:43 +00:00
|
|
|
*
|
|
|
|
* FIXME define some higher-powered configurations to make it easier
|
|
|
|
* to recharge batteries ...
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEV_CONFIG_VALUE 1 /* cdc or subset */
|
2011-02-11 15:18:34 +00:00
|
|
|
#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
static struct usb_device_descriptor
|
|
|
|
device_desc = {
|
|
|
|
.bLength = sizeof device_desc,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE,
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
.bcdUSB = __constant_cpu_to_le16(0x0200),
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
.bDeviceClass = USB_CLASS_COMM,
|
|
|
|
.bDeviceSubClass = 0,
|
|
|
|
.bDeviceProtocol = 0,
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
.idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
|
|
|
|
.idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
|
2009-07-29 16:18:43 +00:00
|
|
|
.iManufacturer = STRING_MANUFACTURER,
|
|
|
|
.iProduct = STRING_PRODUCT,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_otg_descriptor
|
|
|
|
otg_descriptor = {
|
|
|
|
.bLength = sizeof otg_descriptor,
|
|
|
|
.bDescriptorType = USB_DT_OTG,
|
|
|
|
|
|
|
|
.bmAttributes = USB_OTG_SRP,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_config_descriptor
|
|
|
|
eth_config = {
|
|
|
|
.bLength = sizeof eth_config,
|
|
|
|
.bDescriptorType = USB_DT_CONFIG,
|
|
|
|
|
|
|
|
/* compute wTotalLength on the fly */
|
|
|
|
.bNumInterfaces = 2,
|
|
|
|
.bConfigurationValue = DEV_CONFIG_VALUE,
|
|
|
|
.iConfiguration = STRING_CDC,
|
|
|
|
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
|
|
|
|
.bMaxPower = 1,
|
|
|
|
};
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
static struct usb_config_descriptor
|
|
|
|
rndis_config = {
|
|
|
|
.bLength = sizeof rndis_config,
|
|
|
|
.bDescriptorType = USB_DT_CONFIG,
|
|
|
|
|
|
|
|
/* compute wTotalLength on the fly */
|
|
|
|
.bNumInterfaces = 2,
|
|
|
|
.bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
|
|
|
|
.iConfiguration = STRING_RNDIS,
|
|
|
|
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
|
|
|
|
.bMaxPower = 1,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
/*
|
|
|
|
* Compared to the simple CDC subset, the full CDC Ethernet model adds
|
|
|
|
* three class descriptors, two interface descriptors, optional status
|
|
|
|
* endpoint. Both have a "data" interface and two bulk endpoints.
|
|
|
|
* There are also differences in how control requests are handled.
|
2011-02-11 15:18:34 +00:00
|
|
|
*
|
|
|
|
* RNDIS shares a lot with CDC-Ethernet, since it's a variant of the
|
|
|
|
* CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it
|
|
|
|
* may hang or oops. Since bugfixes (or accurate specs, letting Linux
|
|
|
|
* work around those bugs) are unlikely to ever come from MSFT, you may
|
|
|
|
* wish to avoid using RNDIS.
|
|
|
|
*
|
|
|
|
* MCCI offers an alternative to RNDIS if you need to connect to Windows
|
|
|
|
* but have hardware that can't support CDC Ethernet. We add descriptors
|
|
|
|
* to present the CDC Subset as a (nonconformant) CDC MDLM variant called
|
|
|
|
* "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can
|
|
|
|
* get those drivers from MCCI, or bundled with various products.
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
static struct usb_interface_descriptor
|
|
|
|
control_intf = {
|
|
|
|
.bLength = sizeof control_intf,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
/* status endpoint is optional; this may be patched later */
|
|
|
|
.bNumEndpoints = 1,
|
|
|
|
.bInterfaceClass = USB_CLASS_COMM,
|
|
|
|
.bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
|
|
|
|
.bInterfaceProtocol = USB_CDC_PROTO_NONE,
|
|
|
|
.iInterface = STRING_CONTROL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
static const struct usb_interface_descriptor
|
|
|
|
rndis_control_intf = {
|
|
|
|
.bLength = sizeof rndis_control_intf,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bNumEndpoints = 1,
|
|
|
|
.bInterfaceClass = USB_CLASS_COMM,
|
|
|
|
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
|
|
|
|
.bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
|
|
|
|
.iInterface = STRING_RNDIS_CONTROL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
static const struct usb_cdc_header_desc header_desc = {
|
|
|
|
.bLength = sizeof header_desc,
|
|
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
|
|
.bDescriptorSubType = USB_CDC_HEADER_TYPE,
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
.bcdCDC = __constant_cpu_to_le16(0x0110),
|
2009-07-29 16:18:43 +00:00
|
|
|
};
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
static const struct usb_cdc_union_desc union_desc = {
|
|
|
|
.bLength = sizeof union_desc,
|
|
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
|
|
.bDescriptorSubType = USB_CDC_UNION_TYPE,
|
|
|
|
|
|
|
|
.bMasterInterface0 = 0, /* index of control interface */
|
|
|
|
.bSlaveInterface0 = 1, /* index of DATA interface */
|
|
|
|
};
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#endif /* CDC || RNDIS */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
|
|
|
|
static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
|
|
|
|
.bLength = sizeof call_mgmt_descriptor,
|
|
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
|
|
.bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
|
|
|
|
|
|
|
|
.bmCapabilities = 0x00,
|
|
|
|
.bDataInterface = 0x01,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct usb_cdc_acm_descriptor acm_descriptor = {
|
|
|
|
.bLength = sizeof acm_descriptor,
|
|
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
|
|
.bDescriptorSubType = USB_CDC_ACM_TYPE,
|
|
|
|
|
|
|
|
.bmCapabilities = 0x00,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifndef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
|
2009-07-29 16:18:43 +00:00
|
|
|
* ways: data endpoints live in the control interface, there's no data
|
|
|
|
* interface, and it's not used to talk to a cell phone radio.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct usb_cdc_mdlm_desc mdlm_desc = {
|
|
|
|
.bLength = sizeof mdlm_desc,
|
|
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
|
|
.bDescriptorSubType = USB_CDC_MDLM_TYPE,
|
|
|
|
|
|
|
|
.bcdVersion = __constant_cpu_to_le16(0x0100),
|
|
|
|
.bGUID = {
|
|
|
|
0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
|
|
|
|
0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
|
2009-07-29 16:18:43 +00:00
|
|
|
* can't really use its struct. All we do here is say that we're using
|
|
|
|
* the submode of "SAFE" which directly matches the CDC Subset.
|
|
|
|
*/
|
2017-01-17 03:21:22 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_SUBSET
|
2009-07-29 16:18:43 +00:00
|
|
|
static const u8 mdlm_detail_desc[] = {
|
|
|
|
6,
|
|
|
|
USB_DT_CS_INTERFACE,
|
|
|
|
USB_CDC_MDLM_DETAIL_TYPE,
|
|
|
|
|
|
|
|
0, /* "SAFE" */
|
|
|
|
0, /* network control capabilities (none) */
|
|
|
|
0, /* network data capabilities ("raw" encapsulation) */
|
|
|
|
};
|
2017-01-17 03:21:22 +00:00
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct usb_cdc_ether_desc ether_desc = {
|
2010-09-13 14:37:11 +00:00
|
|
|
.bLength = sizeof(ether_desc),
|
2009-07-29 16:18:43 +00:00
|
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
|
|
.bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
|
|
|
|
|
|
|
|
/* this descriptor actually adds value, surprise! */
|
|
|
|
.iMACAddress = STRING_ETHADDR,
|
2010-09-13 14:37:11 +00:00
|
|
|
.bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
|
|
|
|
.wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
|
|
|
|
.wNumberMCFilters = __constant_cpu_to_le16(0),
|
2009-07-29 16:18:43 +00:00
|
|
|
.bNumberPowerFilters = 0,
|
|
|
|
};
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* include the status endpoint if we can, even where it's optional.
|
2009-07-29 16:18:43 +00:00
|
|
|
* use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
|
|
|
|
* packet, to simplify cancellation; and a big transfer interval, to
|
|
|
|
* waste less bandwidth.
|
|
|
|
*
|
|
|
|
* some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
|
|
|
|
* if they ignore the connect/disconnect notifications that real aether
|
|
|
|
* can provide. more advanced cdc configurations might want to support
|
|
|
|
* encapsulated commands (vendor-specific, using control-OUT).
|
2011-02-11 15:18:34 +00:00
|
|
|
*
|
|
|
|
* RNDIS requires the status endpoint, since it uses that encapsulation
|
|
|
|
* mechanism for its funky RPC scheme.
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
|
|
|
|
#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor
|
|
|
|
fs_status_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
2010-09-13 14:37:11 +00:00
|
|
|
.wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
|
2009-07-29 16:18:43 +00:00
|
|
|
.bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/* the default data interface has no endpoints ... */
|
|
|
|
|
|
|
|
static const struct usb_interface_descriptor
|
|
|
|
data_nop_intf = {
|
|
|
|
.bLength = sizeof data_nop_intf,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
|
|
|
|
.bInterfaceNumber = 1,
|
|
|
|
.bAlternateSetting = 0,
|
|
|
|
.bNumEndpoints = 0,
|
|
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
|
|
|
.bInterfaceSubClass = 0,
|
|
|
|
.bInterfaceProtocol = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ... but the "real" data interface has two bulk endpoints */
|
|
|
|
|
|
|
|
static const struct usb_interface_descriptor
|
|
|
|
data_intf = {
|
|
|
|
.bLength = sizeof data_intf,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
|
|
|
|
.bInterfaceNumber = 1,
|
|
|
|
.bAlternateSetting = 1,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
|
|
|
.bInterfaceSubClass = 0,
|
|
|
|
.bInterfaceProtocol = 0,
|
|
|
|
.iInterface = STRING_DATA,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
|
|
|
|
/* RNDIS doesn't activate by changing to the "real" altsetting */
|
|
|
|
|
|
|
|
static const struct usb_interface_descriptor
|
|
|
|
rndis_data_intf = {
|
|
|
|
.bLength = sizeof rndis_data_intf,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
|
|
|
|
.bInterfaceNumber = 1,
|
|
|
|
.bAlternateSetting = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
|
|
|
.bInterfaceSubClass = 0,
|
|
|
|
.bInterfaceProtocol = 0,
|
|
|
|
.iInterface = STRING_DATA,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_SUBSET
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "Simple" CDC-subset option is a simple vendor-neutral model that most
|
|
|
|
* full speed controllers can handle: one interface, two bulk endpoints.
|
|
|
|
*
|
|
|
|
* To assist host side drivers, we fancy it up a bit, and add descriptors
|
|
|
|
* so some host side drivers will understand it as a "SAFE" variant.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct usb_interface_descriptor
|
|
|
|
subset_data_intf = {
|
|
|
|
.bLength = sizeof subset_data_intf,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bAlternateSetting = 0,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_COMM,
|
|
|
|
.bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
|
|
|
|
.bInterfaceProtocol = 0,
|
|
|
|
.iInterface = STRING_DATA,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SUBSET */
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor
|
|
|
|
fs_source_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2013-09-26 01:41:04 +00:00
|
|
|
.wMaxPacketSize = __constant_cpu_to_le16(64),
|
2009-07-29 16:18:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor
|
|
|
|
fs_sink_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2013-09-26 01:41:04 +00:00
|
|
|
.wMaxPacketSize = __constant_cpu_to_le16(64),
|
2009-07-29 16:18:43 +00:00
|
|
|
};
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static const struct usb_descriptor_header *fs_eth_function[11] = {
|
2009-07-29 16:18:43 +00:00
|
|
|
(struct usb_descriptor_header *) &otg_descriptor,
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
/* "cdc" mode descriptors */
|
|
|
|
(struct usb_descriptor_header *) &control_intf,
|
|
|
|
(struct usb_descriptor_header *) &header_desc,
|
|
|
|
(struct usb_descriptor_header *) &union_desc,
|
|
|
|
(struct usb_descriptor_header *) ðer_desc,
|
|
|
|
/* NOTE: status endpoint may need to be removed */
|
|
|
|
(struct usb_descriptor_header *) &fs_status_desc,
|
|
|
|
/* data interface, with altsetting */
|
|
|
|
(struct usb_descriptor_header *) &data_nop_intf,
|
|
|
|
(struct usb_descriptor_header *) &data_intf,
|
|
|
|
(struct usb_descriptor_header *) &fs_source_desc,
|
|
|
|
(struct usb_descriptor_header *) &fs_sink_desc,
|
|
|
|
NULL,
|
2012-10-02 15:04:31 +00:00
|
|
|
#endif /* CONFIG_USB_ETH_CDC */
|
2009-07-29 16:18:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline void fs_subset_descriptors(void)
|
|
|
|
{
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_SUBSET
|
2009-07-29 16:18:43 +00:00
|
|
|
/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
|
|
|
|
fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
|
|
|
|
fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
|
|
|
|
fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
|
|
|
|
fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
|
|
|
|
fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
|
|
|
|
fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
|
|
|
|
fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
|
|
|
|
fs_eth_function[8] = NULL;
|
|
|
|
#else
|
|
|
|
fs_eth_function[1] = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
static const struct usb_descriptor_header *fs_rndis_function[] = {
|
|
|
|
(struct usb_descriptor_header *) &otg_descriptor,
|
|
|
|
/* control interface matches ACM, not Ethernet */
|
|
|
|
(struct usb_descriptor_header *) &rndis_control_intf,
|
|
|
|
(struct usb_descriptor_header *) &header_desc,
|
|
|
|
(struct usb_descriptor_header *) &call_mgmt_descriptor,
|
|
|
|
(struct usb_descriptor_header *) &acm_descriptor,
|
|
|
|
(struct usb_descriptor_header *) &union_desc,
|
|
|
|
(struct usb_descriptor_header *) &fs_status_desc,
|
|
|
|
/* data interface has no altsetting */
|
|
|
|
(struct usb_descriptor_header *) &rndis_data_intf,
|
|
|
|
(struct usb_descriptor_header *) &fs_source_desc,
|
|
|
|
(struct usb_descriptor_header *) &fs_sink_desc,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
/*
|
|
|
|
* usb 2.0 devices need to expose both high speed and full speed
|
|
|
|
* descriptors, unless they only run at full speed.
|
|
|
|
*/
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2009-07-29 16:18:43 +00:00
|
|
|
static struct usb_endpoint_descriptor
|
|
|
|
hs_status_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
2010-09-13 14:37:11 +00:00
|
|
|
.wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
|
2009-07-29 16:18:43 +00:00
|
|
|
.bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
|
|
|
|
};
|
2012-10-02 15:04:31 +00:00
|
|
|
#endif /* CONFIG_USB_ETH_CDC */
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor
|
|
|
|
hs_source_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2010-09-13 14:37:11 +00:00
|
|
|
.wMaxPacketSize = __constant_cpu_to_le16(512),
|
2009-07-29 16:18:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor
|
|
|
|
hs_sink_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2010-09-13 14:37:11 +00:00
|
|
|
.wMaxPacketSize = __constant_cpu_to_le16(512),
|
2009-07-29 16:18:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_qualifier_descriptor
|
|
|
|
dev_qualifier = {
|
|
|
|
.bLength = sizeof dev_qualifier,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
.bcdUSB = __constant_cpu_to_le16(0x0200),
|
2009-07-29 16:18:43 +00:00
|
|
|
.bDeviceClass = USB_CLASS_COMM,
|
|
|
|
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
};
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static const struct usb_descriptor_header *hs_eth_function[11] = {
|
2009-07-29 16:18:43 +00:00
|
|
|
(struct usb_descriptor_header *) &otg_descriptor,
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
/* "cdc" mode descriptors */
|
|
|
|
(struct usb_descriptor_header *) &control_intf,
|
|
|
|
(struct usb_descriptor_header *) &header_desc,
|
|
|
|
(struct usb_descriptor_header *) &union_desc,
|
|
|
|
(struct usb_descriptor_header *) ðer_desc,
|
|
|
|
/* NOTE: status endpoint may need to be removed */
|
|
|
|
(struct usb_descriptor_header *) &hs_status_desc,
|
|
|
|
/* data interface, with altsetting */
|
|
|
|
(struct usb_descriptor_header *) &data_nop_intf,
|
|
|
|
(struct usb_descriptor_header *) &data_intf,
|
|
|
|
(struct usb_descriptor_header *) &hs_source_desc,
|
|
|
|
(struct usb_descriptor_header *) &hs_sink_desc,
|
|
|
|
NULL,
|
2012-10-02 15:04:31 +00:00
|
|
|
#endif /* CONFIG_USB_ETH_CDC */
|
2009-07-29 16:18:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline void hs_subset_descriptors(void)
|
|
|
|
{
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_SUBSET
|
2009-07-29 16:18:43 +00:00
|
|
|
/* behavior is "CDC Subset"; extra descriptors say "SAFE" */
|
|
|
|
hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
|
|
|
|
hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
|
|
|
|
hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
|
|
|
|
hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
|
|
|
|
hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc;
|
|
|
|
hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
|
|
|
|
hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
|
|
|
|
hs_eth_function[8] = NULL;
|
|
|
|
#else
|
|
|
|
hs_eth_function[1] = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
static const struct usb_descriptor_header *hs_rndis_function[] = {
|
|
|
|
(struct usb_descriptor_header *) &otg_descriptor,
|
|
|
|
/* control interface matches ACM, not Ethernet */
|
|
|
|
(struct usb_descriptor_header *) &rndis_control_intf,
|
|
|
|
(struct usb_descriptor_header *) &header_desc,
|
|
|
|
(struct usb_descriptor_header *) &call_mgmt_descriptor,
|
|
|
|
(struct usb_descriptor_header *) &acm_descriptor,
|
|
|
|
(struct usb_descriptor_header *) &union_desc,
|
|
|
|
(struct usb_descriptor_header *) &hs_status_desc,
|
|
|
|
/* data interface has no altsetting */
|
|
|
|
(struct usb_descriptor_header *) &rndis_data_intf,
|
|
|
|
(struct usb_descriptor_header *) &hs_source_desc,
|
|
|
|
(struct usb_descriptor_header *) &hs_sink_desc,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
/* maxpacket and other transfer characteristics vary by speed. */
|
|
|
|
static inline struct usb_endpoint_descriptor *
|
|
|
|
ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
|
|
|
|
struct usb_endpoint_descriptor *fs)
|
|
|
|
{
|
|
|
|
if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
|
|
|
|
return hs;
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* descriptors that are built on-demand */
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static char manufacturer[50];
|
|
|
|
static char product_desc[40] = DRIVER_DESC;
|
|
|
|
static char serial_number[20];
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/* address that the host will use ... usually assigned at random */
|
2010-09-13 14:37:11 +00:00
|
|
|
static char ethaddr[2 * ETH_ALEN + 1];
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/* static strings, in UTF-8 */
|
2010-09-13 14:37:11 +00:00
|
|
|
static struct usb_string strings[] = {
|
2009-07-29 16:18:43 +00:00
|
|
|
{ STRING_MANUFACTURER, manufacturer, },
|
|
|
|
{ STRING_PRODUCT, product_desc, },
|
|
|
|
{ STRING_SERIALNUMBER, serial_number, },
|
|
|
|
{ STRING_DATA, "Ethernet Data", },
|
|
|
|
{ STRING_ETHADDR, ethaddr, },
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
{ STRING_CDC, "CDC Ethernet", },
|
|
|
|
{ STRING_CONTROL, "CDC Communications Control", },
|
|
|
|
#endif
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_SUBSET
|
2009-07-29 16:18:43 +00:00
|
|
|
{ STRING_SUBSET, "CDC Ethernet Subset", },
|
2011-02-11 15:18:34 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
{ STRING_RNDIS, "RNDIS", },
|
|
|
|
{ STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
|
2009-07-29 16:18:43 +00:00
|
|
|
#endif
|
|
|
|
{ } /* end of list */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings stringtab = {
|
|
|
|
.language = 0x0409, /* en-us */
|
|
|
|
.strings = strings,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*============================================================================*/
|
2013-09-04 23:55:14 +00:00
|
|
|
DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ);
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2013-09-04 23:55:14 +00:00
|
|
|
DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT);
|
2012-10-02 15:04:29 +00:00
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* one config, two interfaces: control, data.
|
|
|
|
* complications: class descriptors, and an altsetting.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
const struct usb_config_descriptor *config;
|
|
|
|
const struct usb_descriptor_header **function;
|
|
|
|
int hs = 0;
|
|
|
|
|
|
|
|
if (gadget_is_dualspeed(g)) {
|
|
|
|
hs = (g->speed == USB_SPEED_HIGH);
|
|
|
|
if (type == USB_DT_OTHER_SPEED_CONFIG)
|
|
|
|
hs = !hs;
|
|
|
|
}
|
|
|
|
#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
|
|
|
|
|
|
|
|
if (index >= device_desc.bNumConfigurations)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
/*
|
|
|
|
* list the RNDIS config first, to make Microsoft's drivers
|
|
|
|
* happy. DOCSIS 1.0 needs this too.
|
|
|
|
*/
|
|
|
|
if (device_desc.bNumConfigurations == 2 && index == 0) {
|
|
|
|
config = &rndis_config;
|
|
|
|
function = which_fn(rndis);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
config = ð_config;
|
|
|
|
function = which_fn(eth);
|
|
|
|
}
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/* for now, don't advertise srp-only devices */
|
|
|
|
if (!is_otg)
|
|
|
|
function++;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (len < 0)
|
|
|
|
return len;
|
|
|
|
((struct usb_config_descriptor *) buf)->bDescriptorType = type;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
|
2010-09-13 14:37:11 +00:00
|
|
|
static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
static int
|
2010-09-13 14:37:11 +00:00
|
|
|
set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
struct usb_gadget *gadget = dev->gadget;
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2011-02-11 15:18:34 +00:00
|
|
|
/* status endpoint used for RNDIS and (optionally) CDC */
|
2009-07-29 16:18:43 +00:00
|
|
|
if (!subset_active(dev) && dev->status_ep) {
|
2010-09-13 14:37:11 +00:00
|
|
|
dev->status = ep_desc(gadget, &hs_status_desc,
|
2009-07-29 16:18:43 +00:00
|
|
|
&fs_status_desc);
|
|
|
|
dev->status_ep->driver_data = dev;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
result = usb_ep_enable(dev->status_ep, dev->status);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (result != 0) {
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("enable %s --> %d\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->status_ep->name, result);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
|
|
|
|
dev->in_ep->driver_data = dev;
|
|
|
|
|
|
|
|
dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
|
|
|
|
dev->out_ep->driver_data = dev;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* With CDC, the host isn't allowed to use these two data
|
2009-07-29 16:18:43 +00:00
|
|
|
* endpoints in the default altsetting for the interface.
|
|
|
|
* so we don't activate them yet. Reset from SET_INTERFACE.
|
2011-02-11 15:18:34 +00:00
|
|
|
*
|
|
|
|
* Strictly speaking RNDIS should work the same: activation is
|
|
|
|
* a side effect of setting a packet filter. Deactivation is
|
|
|
|
* from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
|
|
|
if (!cdc_active(dev)) {
|
2010-09-13 14:37:11 +00:00
|
|
|
result = usb_ep_enable(dev->in_ep, dev->in);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (result != 0) {
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("enable %s --> %d\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->in_ep->name, result);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
result = usb_ep_enable(dev->out_ep, dev->out);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (result != 0) {
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("enable %s --> %d\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->out_ep->name, result);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (result == 0)
|
2010-09-13 14:37:11 +00:00
|
|
|
result = alloc_requests(dev, qlen(gadget), gfp_flags);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/* on error, disable any endpoints */
|
|
|
|
if (result < 0) {
|
2010-08-13 13:02:29 +00:00
|
|
|
if (!subset_active(dev) && dev->status_ep)
|
2010-09-13 14:37:11 +00:00
|
|
|
(void) usb_ep_disable(dev->status_ep);
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->status = NULL;
|
2010-09-13 14:37:11 +00:00
|
|
|
(void) usb_ep_disable(dev->in_ep);
|
|
|
|
(void) usb_ep_disable(dev->out_ep);
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->in = NULL;
|
|
|
|
dev->out = NULL;
|
2011-02-11 15:18:34 +00:00
|
|
|
} else if (!cdc_active(dev)) {
|
|
|
|
/*
|
|
|
|
* activate non-CDC configs right away
|
|
|
|
* this isn't strictly according to the RNDIS spec
|
|
|
|
*/
|
|
|
|
eth_start(dev, GFP_ATOMIC);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* caller is responsible for cleanup on error */
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void eth_reset_config(struct eth_dev *dev)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
if (dev->config == 0)
|
|
|
|
return;
|
|
|
|
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("%s\n", __func__);
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
rndis_uninit(dev->rndis_config);
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* disable endpoints, forcing (synchronous) completion of
|
2009-07-29 16:18:43 +00:00
|
|
|
* pending i/o. then free the requests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->in) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_disable(dev->in_ep);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (dev->tx_req) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_free_request(dev->in_ep, dev->tx_req);
|
|
|
|
dev->tx_req = NULL;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dev->out) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_disable(dev->out_ep);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (dev->rx_req) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_free_request(dev->out_ep, dev->rx_req);
|
|
|
|
dev->rx_req = NULL;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-13 14:37:11 +00:00
|
|
|
if (dev->status)
|
|
|
|
usb_ep_disable(dev->status_ep);
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
dev->rndis = 0;
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->cdc_filter = 0;
|
|
|
|
dev->config = 0;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* change our operational config. must agree with the code
|
2009-07-29 16:18:43 +00:00
|
|
|
* that returns config descriptors, and altsetting code.
|
|
|
|
*/
|
2010-09-13 14:37:11 +00:00
|
|
|
static int eth_set_config(struct eth_dev *dev, unsigned number,
|
|
|
|
gfp_t gfp_flags)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
struct usb_gadget *gadget = dev->gadget;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
if (gadget_is_sa1100(gadget)
|
2009-07-29 16:18:43 +00:00
|
|
|
&& dev->config
|
|
|
|
&& dev->tx_qlen != 0) {
|
|
|
|
/* tx fifo is full, but we can't clear it...*/
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("can't change configurations");
|
2009-07-29 16:18:43 +00:00
|
|
|
return -ESPIPE;
|
|
|
|
}
|
2010-09-13 14:37:11 +00:00
|
|
|
eth_reset_config(dev);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
switch (number) {
|
|
|
|
case DEV_CONFIG_VALUE:
|
2010-09-13 14:37:11 +00:00
|
|
|
result = set_ether_config(dev, gfp_flags);
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
case DEV_RNDIS_CONFIG_VALUE:
|
|
|
|
dev->rndis = 1;
|
|
|
|
result = set_ether_config(dev, gfp_flags);
|
|
|
|
break;
|
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
default:
|
|
|
|
result = -EINVAL;
|
|
|
|
/* FALL THROUGH */
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
if (number)
|
2010-09-13 14:37:11 +00:00
|
|
|
eth_reset_config(dev);
|
2009-07-29 16:18:43 +00:00
|
|
|
usb_gadget_vbus_draw(dev->gadget,
|
|
|
|
gadget_is_otg(dev->gadget) ? 8 : 100);
|
|
|
|
} else {
|
|
|
|
char *speed;
|
|
|
|
unsigned power;
|
|
|
|
|
|
|
|
power = 2 * eth_config.bMaxPower;
|
|
|
|
usb_gadget_vbus_draw(dev->gadget, power);
|
|
|
|
|
|
|
|
switch (gadget->speed) {
|
2010-09-13 14:37:11 +00:00
|
|
|
case USB_SPEED_FULL:
|
|
|
|
speed = "full"; break;
|
2009-07-29 16:18:43 +00:00
|
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
2010-09-13 14:37:11 +00:00
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
speed = "high"; break;
|
2009-07-29 16:18:43 +00:00
|
|
|
#endif
|
2010-09-13 14:37:11 +00:00
|
|
|
default:
|
|
|
|
speed = "?"; break;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev->config = number;
|
2010-08-13 12:57:51 +00:00
|
|
|
printf("%s speed config #%d: %d mA, %s, using %s\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
speed, number, power, driver_desc,
|
2011-02-11 15:18:34 +00:00
|
|
|
rndis_active(dev)
|
|
|
|
? "RNDIS"
|
|
|
|
: (cdc_active(dev)
|
|
|
|
? "CDC Ethernet"
|
2009-07-29 16:18:43 +00:00
|
|
|
: "CDC Ethernet Subset"));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
|
2009-07-29 16:18:43 +00:00
|
|
|
* only to notify the host about link status changes (which we support) or
|
2011-02-11 15:18:34 +00:00
|
|
|
* report completion of some encapsulated command (as used in RNDIS). Since
|
2009-07-29 16:18:43 +00:00
|
|
|
* we want this CDC Ethernet code to be vendor-neutral, we don't use that
|
|
|
|
* command mechanism; and only one status request is ever queued.
|
|
|
|
*/
|
2010-09-13 14:37:11 +00:00
|
|
|
static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
struct usb_cdc_notification *event = req->buf;
|
|
|
|
int value = req->status;
|
|
|
|
struct eth_dev *dev = ep->driver_data;
|
|
|
|
|
|
|
|
/* issue the second notification if host reads the first */
|
|
|
|
if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
|
|
|
|
&& value == 0) {
|
|
|
|
__le32 *data = req->buf + sizeof *event;
|
|
|
|
|
|
|
|
event->bmRequestType = 0xA1;
|
|
|
|
event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
|
2010-09-13 14:37:11 +00:00
|
|
|
event->wValue = __constant_cpu_to_le16(0);
|
|
|
|
event->wIndex = __constant_cpu_to_le16(1);
|
|
|
|
event->wLength = __constant_cpu_to_le16(8);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/* SPEED_CHANGE data is up/down speeds in bits/sec */
|
2010-09-13 14:37:11 +00:00
|
|
|
data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
req->length = STATUS_BYTECOUNT;
|
2010-09-13 14:37:11 +00:00
|
|
|
value = usb_ep_queue(ep, req, GFP_ATOMIC);
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("send SPEED_CHANGE --> %d\n", value);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (value == 0)
|
|
|
|
return;
|
|
|
|
} else if (value != -ECONNRESET) {
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("event %02x --> %d\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
event->bNotificationType, value);
|
2010-09-13 14:37:11 +00:00
|
|
|
if (event->bNotificationType ==
|
|
|
|
USB_CDC_NOTIFY_SPEED_CHANGE) {
|
2016-11-18 05:19:13 +00:00
|
|
|
dev->network_started = 1;
|
2009-07-29 16:18:43 +00:00
|
|
|
printf("USB network up!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req->context = NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void issue_start_status(struct eth_dev *dev)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
struct usb_request *req = dev->stat_req;
|
|
|
|
struct usb_cdc_notification *event;
|
|
|
|
int value;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* flush old status
|
2009-07-29 16:18:43 +00:00
|
|
|
*
|
|
|
|
* FIXME ugly idiom, maybe we'd be better with just
|
|
|
|
* a "cancel the whole queue" primitive since any
|
|
|
|
* unlink-one primitive has way too many error modes.
|
|
|
|
* here, we "know" toggle is already clear...
|
|
|
|
*
|
|
|
|
* FIXME iff req->context != null just dequeue it
|
|
|
|
*/
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_disable(dev->status_ep);
|
|
|
|
usb_ep_enable(dev->status_ep, dev->status);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* 3.8.1 says to issue first NETWORK_CONNECTION, then
|
2009-07-29 16:18:43 +00:00
|
|
|
* a SPEED_CHANGE. could be useful in some configs.
|
|
|
|
*/
|
|
|
|
event = req->buf;
|
|
|
|
event->bmRequestType = 0xA1;
|
|
|
|
event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
|
2010-09-13 14:37:11 +00:00
|
|
|
event->wValue = __constant_cpu_to_le16(1); /* connected */
|
|
|
|
event->wIndex = __constant_cpu_to_le16(1);
|
2009-07-29 16:18:43 +00:00
|
|
|
event->wLength = 0;
|
|
|
|
|
|
|
|
req->length = sizeof *event;
|
|
|
|
req->complete = eth_status_complete;
|
|
|
|
req->context = dev;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (value < 0)
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("status buf queue --> %d\n", value);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
if (req->status || req->actual != req->length)
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("setup complete --> %d, %d/%d\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
req->status, req->actual, req->length);
|
|
|
|
}
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
|
|
|
|
static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
|
|
|
|
{
|
|
|
|
if (req->status || req->actual != req->length)
|
|
|
|
debug("rndis response complete --> %d, %d/%d\n",
|
|
|
|
req->status, req->actual, req->length);
|
|
|
|
|
|
|
|
/* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
|
|
|
|
{
|
|
|
|
struct eth_dev *dev = ep->driver_data;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
|
|
|
|
status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
|
|
|
|
if (status < 0)
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("%s: rndis parse error %d", __func__, status);
|
2011-02-11 15:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* RNDIS */
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
/*
|
|
|
|
* The setup() callback implements all the ep0 functionality that's not
|
|
|
|
* handled lower down. CDC has a number of less-common features:
|
|
|
|
*
|
|
|
|
* - two interfaces: control, and ethernet data
|
|
|
|
* - Ethernet data interface has two altsettings: default, and active
|
|
|
|
* - class-specific descriptors for the control interface
|
|
|
|
* - class-specific control requests
|
|
|
|
*/
|
|
|
|
static int
|
2010-09-13 14:37:11 +00:00
|
|
|
eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2010-09-13 14:37:11 +00:00
|
|
|
struct eth_dev *dev = get_gadget_data(gadget);
|
2009-07-29 16:18:43 +00:00
|
|
|
struct usb_request *req = dev->req;
|
|
|
|
int value = -EOPNOTSUPP;
|
|
|
|
u16 wIndex = le16_to_cpu(ctrl->wIndex);
|
|
|
|
u16 wValue = le16_to_cpu(ctrl->wValue);
|
|
|
|
u16 wLength = le16_to_cpu(ctrl->wLength);
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* descriptors just go into the pre-allocated ep0 buffer,
|
2009-07-29 16:18:43 +00:00
|
|
|
* while config change events may enable network traffic.
|
|
|
|
*/
|
|
|
|
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("%s\n", __func__);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
req->complete = eth_setup_complete;
|
|
|
|
switch (ctrl->bRequest) {
|
|
|
|
|
|
|
|
case USB_REQ_GET_DESCRIPTOR:
|
|
|
|
if (ctrl->bRequestType != USB_DIR_IN)
|
|
|
|
break;
|
|
|
|
switch (wValue >> 8) {
|
|
|
|
|
|
|
|
case USB_DT_DEVICE:
|
2015-02-23 13:10:17 +00:00
|
|
|
device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
|
2010-09-13 14:37:11 +00:00
|
|
|
value = min(wLength, (u16) sizeof device_desc);
|
|
|
|
memcpy(req->buf, &device_desc, value);
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
|
|
|
case USB_DT_DEVICE_QUALIFIER:
|
|
|
|
if (!gadget_is_dualspeed(gadget))
|
|
|
|
break;
|
2010-09-13 14:37:11 +00:00
|
|
|
value = min(wLength, (u16) sizeof dev_qualifier);
|
|
|
|
memcpy(req->buf, &dev_qualifier, value);
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_DT_OTHER_SPEED_CONFIG:
|
|
|
|
if (!gadget_is_dualspeed(gadget))
|
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case USB_DT_CONFIG:
|
|
|
|
value = config_buf(gadget, req->buf,
|
|
|
|
wValue >> 8,
|
|
|
|
wValue & 0xff,
|
|
|
|
gadget_is_otg(gadget));
|
|
|
|
if (value >= 0)
|
2010-09-13 14:37:11 +00:00
|
|
|
value = min(wLength, (u16) value);
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_DT_STRING:
|
2010-09-13 14:37:11 +00:00
|
|
|
value = usb_gadget_get_string(&stringtab,
|
2009-07-29 16:18:43 +00:00
|
|
|
wValue & 0xff, req->buf);
|
|
|
|
|
|
|
|
if (value >= 0)
|
2010-09-13 14:37:11 +00:00
|
|
|
value = min(wLength, (u16) value);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SET_CONFIGURATION:
|
|
|
|
if (ctrl->bRequestType != 0)
|
|
|
|
break;
|
|
|
|
if (gadget->a_hnp_support)
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("HNP available\n");
|
2009-07-29 16:18:43 +00:00
|
|
|
else if (gadget->a_alt_hnp_support)
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("HNP needs a different root port\n");
|
2010-09-13 14:37:11 +00:00
|
|
|
value = eth_set_config(dev, wValue, GFP_ATOMIC);
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
|
|
|
case USB_REQ_GET_CONFIGURATION:
|
|
|
|
if (ctrl->bRequestType != USB_DIR_IN)
|
|
|
|
break;
|
|
|
|
*(u8 *)req->buf = dev->config;
|
2010-09-13 14:37:11 +00:00
|
|
|
value = min(wLength, (u16) 1);
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SET_INTERFACE:
|
|
|
|
if (ctrl->bRequestType != USB_RECIP_INTERFACE
|
|
|
|
|| !dev->config
|
|
|
|
|| wIndex > 1)
|
|
|
|
break;
|
|
|
|
if (!cdc_active(dev) && wIndex != 0)
|
|
|
|
break;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* PXA hardware partially handles SET_INTERFACE;
|
2009-07-29 16:18:43 +00:00
|
|
|
* we need to kluge around that interference.
|
|
|
|
*/
|
2010-09-13 14:37:11 +00:00
|
|
|
if (gadget_is_pxa(gadget)) {
|
|
|
|
value = eth_set_config(dev, DEV_CONFIG_VALUE,
|
2009-07-29 16:18:43 +00:00
|
|
|
GFP_ATOMIC);
|
2012-10-02 15:04:29 +00:00
|
|
|
/*
|
|
|
|
* PXA25x driver use non-CDC ethernet gadget.
|
|
|
|
* But only _CDC and _RNDIS code can signalize
|
|
|
|
* that network is working. So we signalize it
|
|
|
|
* here.
|
|
|
|
*/
|
2016-11-18 05:19:13 +00:00
|
|
|
dev->network_started = 1;
|
2012-10-02 15:04:29 +00:00
|
|
|
debug("USB network up!\n");
|
2009-07-29 16:18:43 +00:00
|
|
|
goto done_set_intf;
|
|
|
|
}
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
switch (wIndex) {
|
|
|
|
case 0: /* control/master intf */
|
|
|
|
if (wValue != 0)
|
|
|
|
break;
|
|
|
|
if (dev->status) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_disable(dev->status_ep);
|
|
|
|
usb_ep_enable(dev->status_ep, dev->status);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
2011-02-11 15:18:34 +00:00
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
value = 0;
|
|
|
|
break;
|
|
|
|
case 1: /* data intf */
|
|
|
|
if (wValue > 1)
|
|
|
|
break;
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_disable(dev->in_ep);
|
|
|
|
usb_ep_disable(dev->out_ep);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* CDC requires the data transfers not be done from
|
2009-07-29 16:18:43 +00:00
|
|
|
* the default interface setting ... also, setting
|
|
|
|
* the non-default interface resets filters etc.
|
|
|
|
*/
|
|
|
|
if (wValue == 1) {
|
2010-09-13 14:37:11 +00:00
|
|
|
if (!cdc_active(dev))
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_enable(dev->in_ep, dev->in);
|
|
|
|
usb_ep_enable(dev->out_ep, dev->out);
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->cdc_filter = DEFAULT_FILTER;
|
|
|
|
if (dev->status)
|
2010-09-13 14:37:11 +00:00
|
|
|
issue_start_status(dev);
|
2011-02-11 15:18:34 +00:00
|
|
|
eth_start(dev, GFP_ATOMIC);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
value = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* FIXME this is wrong, as is the assumption that
|
2009-07-29 16:18:43 +00:00
|
|
|
* all non-PXA hardware talks real CDC ...
|
|
|
|
*/
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("set_interface ignored!\n");
|
2012-10-02 15:04:31 +00:00
|
|
|
#endif /* CONFIG_USB_ETH_CDC */
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
done_set_intf:
|
|
|
|
break;
|
|
|
|
case USB_REQ_GET_INTERFACE:
|
|
|
|
if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
|
|
|
|
|| !dev->config
|
|
|
|
|| wIndex > 1)
|
|
|
|
break;
|
2011-02-11 15:18:34 +00:00
|
|
|
if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* for CDC, iff carrier is on, data interface is active. */
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis_active(dev) || wIndex != 1)
|
2009-07-29 16:18:43 +00:00
|
|
|
*(u8 *)req->buf = 0;
|
|
|
|
else {
|
|
|
|
/* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
|
|
|
|
/* carrier always ok ...*/
|
|
|
|
*(u8 *)req->buf = 1 ;
|
|
|
|
}
|
2010-09-13 14:37:11 +00:00
|
|
|
value = min(wLength, (u16) 1);
|
2009-07-29 16:18:43 +00:00
|
|
|
break;
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
case USB_CDC_SET_ETHERNET_PACKET_FILTER:
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* see 6.2.30: no data, wIndex = interface,
|
2009-07-29 16:18:43 +00:00
|
|
|
* wValue = packet filter bitmap
|
|
|
|
*/
|
|
|
|
if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
|
|
|
|
|| !cdc_active(dev)
|
|
|
|
|| wLength != 0
|
|
|
|
|| wIndex > 1)
|
|
|
|
break;
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("packet filter %02x\n", wValue);
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->cdc_filter = wValue;
|
|
|
|
value = 0;
|
|
|
|
break;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* and potentially:
|
2009-07-29 16:18:43 +00:00
|
|
|
* case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
|
|
|
|
* case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
|
|
|
|
* case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
|
|
|
|
* case USB_CDC_GET_ETHERNET_STATISTIC:
|
|
|
|
*/
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#endif /* CONFIG_USB_ETH_CDC */
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
/*
|
|
|
|
* RNDIS uses the CDC command encapsulation mechanism to implement
|
|
|
|
* an RPC scheme, with much getting/setting of attributes by OID.
|
|
|
|
*/
|
|
|
|
case USB_CDC_SEND_ENCAPSULATED_COMMAND:
|
|
|
|
if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
|
|
|
|
|| !rndis_active(dev)
|
|
|
|
|| wLength > USB_BUFSIZ
|
|
|
|
|| wValue
|
|
|
|
|| rndis_control_intf.bInterfaceNumber
|
|
|
|
!= wIndex)
|
|
|
|
break;
|
|
|
|
/* read the request, then process it */
|
|
|
|
value = wLength;
|
|
|
|
req->complete = rndis_command_complete;
|
|
|
|
/* later, rndis_control_ack () sends a notification */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_CDC_GET_ENCAPSULATED_RESPONSE:
|
|
|
|
if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
|
|
|
|
== ctrl->bRequestType
|
|
|
|
&& rndis_active(dev)
|
|
|
|
/* && wLength >= 0x0400 */
|
|
|
|
&& !wValue
|
|
|
|
&& rndis_control_intf.bInterfaceNumber
|
|
|
|
== wIndex) {
|
|
|
|
u8 *buf;
|
|
|
|
u32 n;
|
|
|
|
|
|
|
|
/* return the result */
|
|
|
|
buf = rndis_get_next_response(dev->rndis_config, &n);
|
|
|
|
if (buf) {
|
|
|
|
memcpy(req->buf, buf, n);
|
|
|
|
req->complete = rndis_response_complete;
|
|
|
|
rndis_free_response(dev->rndis_config, buf);
|
|
|
|
value = n;
|
|
|
|
}
|
|
|
|
/* else stalls ... spec says to avoid that */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* RNDIS */
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
default:
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
ctrl->bRequestType, ctrl->bRequest,
|
|
|
|
wValue, wIndex, wLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* respond with data transfer before status phase? */
|
|
|
|
if (value >= 0) {
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("respond with data transfer before status phase\n");
|
2009-07-29 16:18:43 +00:00
|
|
|
req->length = value;
|
|
|
|
req->zero = value < wLength
|
|
|
|
&& (value % gadget->ep0->maxpacket) == 0;
|
2010-09-13 14:37:11 +00:00
|
|
|
value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (value < 0) {
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("ep_queue --> %d\n", value);
|
2009-07-29 16:18:43 +00:00
|
|
|
req->status = 0;
|
2010-09-13 14:37:11 +00:00
|
|
|
eth_setup_complete(gadget->ep0, req);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* host either stalls (value < 0) or reports success */
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void rx_complete(struct usb_ep *ep, struct usb_request *req);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static int rx_submit(struct eth_dev *dev, struct usb_request *req,
|
2009-07-29 16:18:43 +00:00
|
|
|
gfp_t gfp_flags)
|
|
|
|
{
|
|
|
|
int retval = -ENOMEM;
|
|
|
|
size_t size;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* Padding up to RX_EXTRA handles minor disagreements with host.
|
2009-07-29 16:18:43 +00:00
|
|
|
* Normally we use the USB "terminate on short read" convention;
|
|
|
|
* so allow up to (N*maxpacket), since that memory is normally
|
|
|
|
* already allocated. Some hardware doesn't deal well with short
|
|
|
|
* reads (e.g. DMA must be N*maxpacket), so for now don't trim a
|
|
|
|
* byte off the end (to force hardware errors on overflow).
|
2011-02-11 15:18:34 +00:00
|
|
|
*
|
|
|
|
* RNDIS uses internal framing, and explicitly allows senders to
|
|
|
|
* pad to end-of-packet. That's potentially nice for speed,
|
|
|
|
* but means receivers can't recover synch on their own.
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
|
|
|
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("%s\n", __func__);
|
2013-09-26 01:41:05 +00:00
|
|
|
if (!req)
|
|
|
|
return -EINVAL;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
|
|
|
|
size += dev->out_ep->maxpacket - 1;
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis_active(dev))
|
|
|
|
size += sizeof(struct rndis_packet_msg_type);
|
2009-07-29 16:18:43 +00:00
|
|
|
size -= size % dev->out_ep->maxpacket;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* Some platforms perform better when IP packets are aligned,
|
2011-02-11 15:18:34 +00:00
|
|
|
* but on at least one, checksumming fails otherwise. Note:
|
|
|
|
* RNDIS headers involve variable numbers of LE32 values.
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-08 06:41:06 +00:00
|
|
|
req->buf = (u8 *)net_rx_packets[0];
|
2009-07-29 16:18:43 +00:00
|
|
|
req->length = size;
|
|
|
|
req->complete = rx_complete;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
if (retval)
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("rx submit --> %d", retval);
|
2010-09-13 14:37:11 +00:00
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void rx_complete(struct usb_ep *ep, struct usb_request *req)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
struct eth_dev *dev = ep->driver_data;
|
|
|
|
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("%s: status %d\n", __func__, req->status);
|
2011-02-11 15:18:32 +00:00
|
|
|
switch (req->status) {
|
|
|
|
/* normal completion */
|
|
|
|
case 0:
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis_active(dev)) {
|
|
|
|
/* we know MaxPacketsPerTransfer == 1 here */
|
|
|
|
int length = rndis_rm_hdr(req->buf, req->actual);
|
|
|
|
if (length < 0)
|
|
|
|
goto length_err;
|
|
|
|
req->length -= length;
|
|
|
|
req->actual -= length;
|
|
|
|
}
|
|
|
|
if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
|
|
|
|
length_err:
|
|
|
|
dev->stats.rx_errors++;
|
|
|
|
dev->stats.rx_length_errors++;
|
|
|
|
debug("rx length %d\n", req->length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-02-11 15:18:32 +00:00
|
|
|
dev->stats.rx_packets++;
|
|
|
|
dev->stats.rx_bytes += req->length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* software-driven interface shutdown */
|
|
|
|
case -ECONNRESET: /* unlink */
|
|
|
|
case -ESHUTDOWN: /* disconnect etc */
|
|
|
|
/* for hardware automagic (such as pxa) */
|
|
|
|
case -ECONNABORTED: /* endpoint reset */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* data overrun */
|
|
|
|
case -EOVERFLOW:
|
|
|
|
dev->stats.rx_over_errors++;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
dev->stats.rx_errors++;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
packet_received = 1;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
if (!dev->tx_req)
|
2010-09-22 09:13:55 +00:00
|
|
|
goto fail1;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
if (!dev->rx_req)
|
2010-09-22 09:13:55 +00:00
|
|
|
goto fail2;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2010-09-22 09:13:55 +00:00
|
|
|
fail2:
|
|
|
|
usb_ep_free_request(dev->in_ep, dev->tx_req);
|
|
|
|
fail1:
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("can't alloc requests");
|
2009-07-29 16:18:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void tx_complete(struct usb_ep *ep, struct usb_request *req)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2011-02-11 15:18:32 +00:00
|
|
|
struct eth_dev *dev = ep->driver_data;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
|
2011-02-11 15:18:32 +00:00
|
|
|
switch (req->status) {
|
|
|
|
default:
|
|
|
|
dev->stats.tx_errors++;
|
|
|
|
debug("tx err %d\n", req->status);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case -ECONNRESET: /* unlink */
|
|
|
|
case -ESHUTDOWN: /* disconnect etc */
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
dev->stats.tx_bytes += req->length;
|
|
|
|
}
|
|
|
|
dev->stats.tx_packets++;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
packet_sent = 1;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static inline int eth_is_promisc(struct eth_dev *dev)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
/* no filters for the CDC subset; always promisc */
|
2010-09-13 14:37:11 +00:00
|
|
|
if (subset_active(dev))
|
2009-07-29 16:18:43 +00:00
|
|
|
return 1;
|
|
|
|
return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
|
|
|
|
{
|
|
|
|
struct eth_dev *dev = netdev_priv(net);
|
|
|
|
int length = skb->len;
|
|
|
|
int retval;
|
|
|
|
struct usb_request *req = NULL;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
/* apply outgoing CDC or RNDIS filters */
|
|
|
|
if (!eth_is_promisc (dev)) {
|
|
|
|
u8 *dest = skb->data;
|
|
|
|
|
2015-04-08 06:41:04 +00:00
|
|
|
if (is_multicast_ethaddr(dest)) {
|
2009-07-29 16:18:43 +00:00
|
|
|
u16 type;
|
|
|
|
|
|
|
|
/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
|
|
|
|
* SET_ETHERNET_MULTICAST_FILTERS requests
|
|
|
|
*/
|
2015-04-08 06:41:04 +00:00
|
|
|
if (is_broadcast_ethaddr(dest))
|
2009-07-29 16:18:43 +00:00
|
|
|
type = USB_CDC_PACKET_TYPE_BROADCAST;
|
|
|
|
else
|
|
|
|
type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
|
|
|
|
if (!(dev->cdc_filter & type)) {
|
|
|
|
dev_kfree_skb_any (skb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->req_lock, flags);
|
|
|
|
/*
|
|
|
|
* this freelist can be empty if an interrupt triggered disconnect()
|
|
|
|
* and reconfigured the gadget (shutting down this queue) after the
|
|
|
|
* network stack decided to xmit but before we got the spinlock.
|
|
|
|
*/
|
|
|
|
if (list_empty(&dev->tx_reqs)) {
|
|
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = container_of (dev->tx_reqs.next, struct usb_request, list);
|
|
|
|
list_del (&req->list);
|
|
|
|
|
|
|
|
/* temporarily stop TX queue when the freelist empties */
|
|
|
|
if (list_empty (&dev->tx_reqs))
|
|
|
|
netif_stop_queue (net);
|
|
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
|
|
|
|
|
|
/* no buffer copies needed, unless the network stack did it
|
|
|
|
* or the hardware can't use skb buffers.
|
|
|
|
* or there's not enough space for any RNDIS headers we need
|
|
|
|
*/
|
|
|
|
if (rndis_active(dev)) {
|
|
|
|
struct sk_buff *skb_rndis;
|
|
|
|
|
|
|
|
skb_rndis = skb_realloc_headroom (skb,
|
|
|
|
sizeof (struct rndis_packet_msg_type));
|
|
|
|
if (!skb_rndis)
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
dev_kfree_skb_any (skb);
|
|
|
|
skb = skb_rndis;
|
|
|
|
rndis_add_hdr (skb);
|
|
|
|
length = skb->len;
|
|
|
|
}
|
|
|
|
req->buf = skb->data;
|
|
|
|
req->context = skb;
|
|
|
|
req->complete = tx_complete;
|
|
|
|
|
|
|
|
/* use zlp framing on tx for strict CDC-Ether conformance,
|
|
|
|
* though any robust network rx path ignores extra padding.
|
|
|
|
* and some hardware doesn't like to write zlps.
|
|
|
|
*/
|
|
|
|
req->zero = 1;
|
|
|
|
if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
|
|
|
|
length++;
|
|
|
|
|
|
|
|
req->length = length;
|
|
|
|
|
|
|
|
/* throttle highspeed IRQ rate back slightly */
|
|
|
|
if (gadget_is_dualspeed(dev->gadget))
|
|
|
|
req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
|
|
|
|
? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
|
|
|
|
switch (retval) {
|
|
|
|
default:
|
|
|
|
DEBUG (dev, "tx queue err %d\n", retval);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
net->trans_start = jiffies;
|
|
|
|
atomic_inc (&dev->tx_qlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
drop:
|
|
|
|
dev->stats.tx_dropped++;
|
|
|
|
dev_kfree_skb_any (skb);
|
|
|
|
spin_lock_irqsave(&dev->req_lock, flags);
|
|
|
|
if (list_empty (&dev->tx_reqs))
|
|
|
|
netif_start_queue (net);
|
|
|
|
list_add (&req->list, &dev->tx_reqs);
|
|
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
#endif
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void eth_unbind(struct usb_gadget *gadget)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2011-02-11 15:18:34 +00:00
|
|
|
struct eth_dev *dev = get_gadget_data(gadget);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("%s...\n", __func__);
|
2011-02-11 15:18:34 +00:00
|
|
|
rndis_deregister(dev->rndis_config);
|
|
|
|
rndis_exit();
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-08-13 13:00:16 +00:00
|
|
|
/* we've already been disconnected ... no i/o is active */
|
|
|
|
if (dev->req) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_free_request(gadget->ep0, dev->req);
|
2010-08-13 13:00:16 +00:00
|
|
|
dev->req = NULL;
|
|
|
|
}
|
2009-07-29 16:18:43 +00:00
|
|
|
if (dev->stat_req) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_free_request(dev->status_ep, dev->stat_req);
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->stat_req = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->tx_req) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_free_request(dev->in_ep, dev->tx_req);
|
|
|
|
dev->tx_req = NULL;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->rx_req) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_free_request(dev->out_ep, dev->rx_req);
|
|
|
|
dev->rx_req = NULL;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unregister_netdev (dev->net);*/
|
|
|
|
/* free_netdev(dev->net);*/
|
|
|
|
|
2010-12-01 15:43:43 +00:00
|
|
|
dev->gadget = NULL;
|
2010-09-13 14:37:11 +00:00
|
|
|
set_gadget_data(gadget, NULL);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void eth_disconnect(struct usb_gadget *gadget)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2010-09-13 14:37:11 +00:00
|
|
|
eth_reset_config(get_gadget_data(gadget));
|
2011-02-11 15:18:34 +00:00
|
|
|
/* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void eth_suspend(struct usb_gadget *gadget)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
/* Not used */
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static void eth_resume(struct usb_gadget *gadget)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
/* Not used */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The interrupt endpoint is used in RNDIS to notify the host when messages
|
|
|
|
* other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
|
|
|
|
* messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
|
|
|
|
* REMOTE_NDIS_KEEPALIVE_MSG.
|
|
|
|
*
|
|
|
|
* The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
|
|
|
|
* normally just one notification will be queued.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void rndis_control_ack_complete(struct usb_ep *ep,
|
|
|
|
struct usb_request *req)
|
|
|
|
{
|
|
|
|
struct eth_dev *dev = ep->driver_data;
|
|
|
|
|
|
|
|
debug("%s...\n", __func__);
|
|
|
|
if (req->status || req->actual != req->length)
|
|
|
|
debug("rndis control ack complete --> %d, %d/%d\n",
|
|
|
|
req->status, req->actual, req->length);
|
|
|
|
|
2016-11-18 05:19:13 +00:00
|
|
|
if (!dev->network_started) {
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis_get_state(dev->rndis_config)
|
|
|
|
== RNDIS_DATA_INITIALIZED) {
|
2016-11-18 05:19:13 +00:00
|
|
|
dev->network_started = 1;
|
2011-02-11 15:18:34 +00:00
|
|
|
printf("USB RNDIS network up!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req->context = NULL;
|
|
|
|
|
|
|
|
if (req != dev->stat_req)
|
|
|
|
usb_ep_free_request(ep, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
|
|
|
|
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
2011-02-11 15:18:34 +00:00
|
|
|
static int rndis_control_ack(struct eth_device *net)
|
2016-11-18 05:39:15 +00:00
|
|
|
#else
|
|
|
|
static int rndis_control_ack(struct udevice *net)
|
|
|
|
#endif
|
2011-02-11 15:18:34 +00:00
|
|
|
{
|
2016-11-18 05:37:18 +00:00
|
|
|
struct ether_priv *priv = (struct ether_priv *)net->priv;
|
|
|
|
struct eth_dev *dev = &priv->ethdev;
|
2011-02-11 15:18:34 +00:00
|
|
|
int length;
|
|
|
|
struct usb_request *resp = dev->stat_req;
|
|
|
|
|
|
|
|
/* in case RNDIS calls this after disconnect */
|
|
|
|
if (!dev->status) {
|
|
|
|
debug("status ENODEV\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in case queue length > 1 */
|
|
|
|
if (resp->context) {
|
|
|
|
resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
|
|
|
|
if (!resp)
|
|
|
|
return -ENOMEM;
|
|
|
|
resp->buf = rndis_resp_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send RNDIS RESPONSE_AVAILABLE notification;
|
|
|
|
* USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
|
|
|
|
*/
|
|
|
|
resp->length = 8;
|
|
|
|
resp->complete = rndis_control_ack_complete;
|
|
|
|
resp->context = dev;
|
|
|
|
|
|
|
|
*((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
|
|
|
|
*((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
|
|
|
|
|
|
|
|
length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
|
|
|
|
if (length < 0) {
|
|
|
|
resp->status = 0;
|
|
|
|
rndis_control_ack_complete(dev->status_ep, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define rndis_control_ack NULL
|
|
|
|
|
|
|
|
#endif /* RNDIS */
|
|
|
|
|
|
|
|
static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
|
|
|
|
{
|
|
|
|
if (rndis_active(dev)) {
|
|
|
|
rndis_set_param_medium(dev->rndis_config,
|
|
|
|
NDIS_MEDIUM_802_3,
|
|
|
|
BITRATE(dev->gadget)/100);
|
|
|
|
rndis_signal_connect(dev->rndis_config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eth_stop(struct eth_dev *dev)
|
|
|
|
{
|
2011-02-11 15:18:35 +00:00
|
|
|
#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
|
|
|
|
unsigned long ts;
|
|
|
|
unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
|
|
|
|
#endif
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis_active(dev)) {
|
|
|
|
rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
|
|
|
|
rndis_signal_disconnect(dev->rndis_config);
|
|
|
|
|
2011-02-11 15:18:35 +00:00
|
|
|
#ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
|
|
|
|
/* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
|
|
|
|
ts = get_timer(0);
|
|
|
|
while (get_timer(ts) < timeout)
|
2015-02-23 13:10:23 +00:00
|
|
|
usb_gadget_handle_interrupts(0);
|
2011-02-11 15:18:35 +00:00
|
|
|
#endif
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
rndis_uninit(dev->rndis_config);
|
|
|
|
dev->rndis = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
static int is_eth_addr_valid(char *str)
|
|
|
|
{
|
|
|
|
if (strlen(str) == 17) {
|
|
|
|
int i;
|
|
|
|
char *p, *q;
|
|
|
|
uchar ea[6];
|
|
|
|
|
|
|
|
/* see if it looks like an ethernet address */
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
char term = (i == 5 ? '\0' : ':');
|
|
|
|
|
|
|
|
ea[i] = simple_strtol(p, &q, 16);
|
|
|
|
|
|
|
|
if ((q - p) != 2 || *q++ != term)
|
|
|
|
break;
|
|
|
|
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
|
2012-10-31 13:30:41 +00:00
|
|
|
/* Now check the contents. */
|
2015-04-08 06:41:04 +00:00
|
|
|
return is_valid_ethaddr(ea);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
static u8 nibble(unsigned char c)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2010-09-13 14:37:11 +00:00
|
|
|
if (likely(isdigit(c)))
|
2009-07-29 16:18:43 +00:00
|
|
|
return c - '0';
|
2010-09-13 14:37:11 +00:00
|
|
|
c = toupper(c);
|
|
|
|
if (likely(isxdigit(c)))
|
2009-07-29 16:18:43 +00:00
|
|
|
return 10 + c - 'A';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_ether_addr(const char *str, u8 *dev_addr)
|
|
|
|
{
|
|
|
|
if (str) {
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
unsigned char num;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
if ((*str == '.') || (*str == ':'))
|
2009-07-29 16:18:43 +00:00
|
|
|
str++;
|
|
|
|
num = nibble(*str++) << 4;
|
|
|
|
num |= (nibble(*str++));
|
2010-09-13 14:37:11 +00:00
|
|
|
dev_addr[i] = num;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
2015-04-08 06:41:04 +00:00
|
|
|
if (is_valid_ethaddr(dev_addr))
|
2009-07-29 16:18:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eth_bind(struct usb_gadget *gadget)
|
|
|
|
{
|
2016-11-18 05:36:13 +00:00
|
|
|
struct eth_dev *dev = &l_priv->ethdev;
|
2011-02-11 15:18:34 +00:00
|
|
|
u8 cdc = 1, zlp = 1, rndis = 1;
|
2009-07-29 16:18:43 +00:00
|
|
|
struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
|
2011-02-11 15:18:34 +00:00
|
|
|
int status = -ENOMEM;
|
2009-07-29 16:18:43 +00:00
|
|
|
int gcnum;
|
2010-09-13 14:37:11 +00:00
|
|
|
u8 tmp[7];
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifdef CONFIG_DM_ETH
|
|
|
|
struct eth_pdata *pdata = dev_get_platdata(l_priv->netdev);
|
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
/* these flags are only ever cleared; compiler take note */
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifndef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
cdc = 0;
|
2011-02-11 15:18:34 +00:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_USB_ETH_RNDIS
|
|
|
|
rndis = 0;
|
2009-07-29 16:18:43 +00:00
|
|
|
#endif
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* Because most host side USB stacks handle CDC Ethernet, that
|
2009-07-29 16:18:43 +00:00
|
|
|
* standard protocol is _strongly_ preferred for interop purposes.
|
|
|
|
* (By everyone except Microsoft.)
|
|
|
|
*/
|
2010-09-13 14:37:11 +00:00
|
|
|
if (gadget_is_pxa(gadget)) {
|
2009-07-29 16:18:43 +00:00
|
|
|
/* pxa doesn't support altsettings */
|
|
|
|
cdc = 0;
|
|
|
|
} else if (gadget_is_musbhdrc(gadget)) {
|
|
|
|
/* reduce tx dma overhead by avoiding special cases */
|
|
|
|
zlp = 0;
|
|
|
|
} else if (gadget_is_sh(gadget)) {
|
|
|
|
/* sh doesn't support multiple interfaces or configs */
|
|
|
|
cdc = 0;
|
2011-02-11 15:18:34 +00:00
|
|
|
rndis = 0;
|
2010-09-13 14:37:11 +00:00
|
|
|
} else if (gadget_is_sa1100(gadget)) {
|
2009-07-29 16:18:43 +00:00
|
|
|
/* hardware can't write zlps */
|
|
|
|
zlp = 0;
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* sa1100 CAN do CDC, without status endpoint ... we use
|
2009-07-29 16:18:43 +00:00
|
|
|
* non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
|
|
|
|
*/
|
|
|
|
cdc = 0;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
gcnum = usb_gadget_controller_number(gadget);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (gcnum >= 0)
|
2010-09-13 14:37:11 +00:00
|
|
|
device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
|
2009-07-29 16:18:43 +00:00
|
|
|
else {
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* can't assume CDC works. don't want to default to
|
2009-07-29 16:18:43 +00:00
|
|
|
* anything less functional on CDC-capable hardware,
|
|
|
|
* so we fail in this case.
|
|
|
|
*/
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("controller '%s' not recognized",
|
2009-07-29 16:18:43 +00:00
|
|
|
gadget->name);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
2011-02-11 15:18:34 +00:00
|
|
|
* If there's an RNDIS configuration, that's what Windows wants to
|
|
|
|
* be using ... so use these product IDs here and in the "linux.inf"
|
|
|
|
* needed to install MSFT drivers. Current Linux kernels will use
|
|
|
|
* the second configuration if it's CDC Ethernet, and need some help
|
|
|
|
* to choose the right configuration otherwise.
|
2009-07-29 16:18:43 +00:00
|
|
|
*/
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis) {
|
2017-09-07 07:15:08 +00:00
|
|
|
#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
|
2009-07-29 16:18:43 +00:00
|
|
|
device_desc.idVendor =
|
2017-09-07 07:15:08 +00:00
|
|
|
__constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
|
2009-07-29 16:18:43 +00:00
|
|
|
device_desc.idProduct =
|
2017-09-07 07:15:08 +00:00
|
|
|
__constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
|
2011-02-11 15:18:34 +00:00
|
|
|
#else
|
|
|
|
device_desc.idVendor =
|
|
|
|
__constant_cpu_to_le16(RNDIS_VENDOR_NUM);
|
|
|
|
device_desc.idProduct =
|
|
|
|
__constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
|
|
|
|
#endif
|
|
|
|
sprintf(product_desc, "RNDIS/%s", driver_desc);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
/*
|
|
|
|
* CDC subset ... recognized by Linux since 2.4.10, but Windows
|
|
|
|
* drivers aren't widely available. (That may be improved by
|
|
|
|
* supporting one submode of the "SAFE" variant of MDLM.)
|
|
|
|
*/
|
|
|
|
} else {
|
2017-09-07 07:15:08 +00:00
|
|
|
#if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM)
|
|
|
|
device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM);
|
|
|
|
device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM);
|
2011-02-11 15:18:34 +00:00
|
|
|
#else
|
|
|
|
if (!cdc) {
|
|
|
|
device_desc.idVendor =
|
|
|
|
__constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
|
|
|
|
device_desc.idProduct =
|
|
|
|
__constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
|
|
|
|
}
|
2009-07-29 16:18:43 +00:00
|
|
|
#endif
|
2011-02-11 15:18:34 +00:00
|
|
|
}
|
|
|
|
/* support optional vendor/distro customization */
|
2009-07-29 16:18:43 +00:00
|
|
|
if (bcdDevice)
|
|
|
|
device_desc.bcdDevice = cpu_to_le16(bcdDevice);
|
|
|
|
if (iManufacturer)
|
2010-09-13 14:37:11 +00:00
|
|
|
strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (iProduct)
|
2010-09-13 14:37:11 +00:00
|
|
|
strlcpy(product_desc, iProduct, sizeof product_desc);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (iSerialNumber) {
|
|
|
|
device_desc.iSerialNumber = STRING_SERIALNUMBER,
|
2010-08-13 13:00:45 +00:00
|
|
|
strlcpy(serial_number, iSerialNumber, sizeof serial_number);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* all we really need is bulk IN/OUT */
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_autoconfig_reset(gadget);
|
|
|
|
in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (!in_ep) {
|
|
|
|
autoconf_fail:
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("can't autoconfigure on %s\n",
|
2009-07-29 16:18:43 +00:00
|
|
|
gadget->name);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
in_ep->driver_data = in_ep; /* claim */
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (!out_ep)
|
|
|
|
goto autoconf_fail;
|
|
|
|
out_ep->driver_data = out_ep; /* claim */
|
|
|
|
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* CDC Ethernet control interface doesn't require a status endpoint.
|
2009-07-29 16:18:43 +00:00
|
|
|
* Since some hosts expect one, try to allocate one anyway.
|
|
|
|
*/
|
2011-02-11 15:18:34 +00:00
|
|
|
if (cdc || rndis) {
|
2010-09-13 14:37:11 +00:00
|
|
|
status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (status_ep) {
|
|
|
|
status_ep->driver_data = status_ep; /* claim */
|
2011-02-11 15:18:34 +00:00
|
|
|
} else if (rndis) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("can't run RNDIS on %s", gadget->name);
|
2011-02-11 15:18:34 +00:00
|
|
|
return -ENODEV;
|
2012-10-02 15:04:31 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_CDC
|
2009-07-29 16:18:43 +00:00
|
|
|
} else if (cdc) {
|
|
|
|
control_intf.bNumEndpoints = 0;
|
|
|
|
/* FIXME remove endpoint from descriptor list */
|
2011-02-11 15:18:34 +00:00
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* one config: cdc, else minimal subset */
|
|
|
|
if (!cdc) {
|
|
|
|
eth_config.bNumInterfaces = 1;
|
|
|
|
eth_config.iConfiguration = STRING_SUBSET;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* use functions to set these up, in case we're built to work
|
2009-07-29 16:18:43 +00:00
|
|
|
* with multiple controllers and must override CDC Ethernet.
|
|
|
|
*/
|
|
|
|
fs_subset_descriptors();
|
|
|
|
hs_subset_descriptors();
|
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_gadget_set_selfpowered(gadget);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
/* For now RNDIS is always a second config */
|
|
|
|
if (rndis)
|
|
|
|
device_desc.bNumConfigurations = 2;
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
if (gadget_is_dualspeed(gadget)) {
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis)
|
|
|
|
dev_qualifier.bNumConfigurations = 2;
|
|
|
|
else if (!cdc)
|
2009-07-29 16:18:43 +00:00
|
|
|
dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
|
|
|
|
|
|
|
|
/* assumes ep0 uses the same value for both speeds ... */
|
|
|
|
dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
|
|
|
|
|
|
|
|
/* and that all endpoints are dual-speed */
|
|
|
|
hs_source_desc.bEndpointAddress =
|
|
|
|
fs_source_desc.bEndpointAddress;
|
|
|
|
hs_sink_desc.bEndpointAddress =
|
|
|
|
fs_sink_desc.bEndpointAddress;
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2009-07-29 16:18:43 +00:00
|
|
|
if (status_ep)
|
|
|
|
hs_status_desc.bEndpointAddress =
|
|
|
|
fs_status_desc.bEndpointAddress;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gadget_is_otg(gadget)) {
|
|
|
|
otg_descriptor.bmAttributes |= USB_OTG_HNP,
|
|
|
|
eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
|
|
|
|
eth_config.bMaxPower = 4;
|
2011-02-11 15:18:34 +00:00
|
|
|
#ifdef CONFIG_USB_ETH_RNDIS
|
|
|
|
rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
|
|
|
|
rndis_config.bMaxPower = 4;
|
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
|
|
|
|
/* network device setup */
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
2016-11-18 05:36:13 +00:00
|
|
|
dev->net = &l_priv->netdev;
|
2016-11-18 05:39:15 +00:00
|
|
|
#else
|
|
|
|
dev->net = l_priv->netdev;
|
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
dev->cdc = cdc;
|
|
|
|
dev->zlp = zlp;
|
|
|
|
|
|
|
|
dev->in_ep = in_ep;
|
|
|
|
dev->out_ep = out_ep;
|
|
|
|
dev->status_ep = status_ep;
|
|
|
|
|
2016-11-18 05:39:15 +00:00
|
|
|
memset(tmp, 0, sizeof(tmp));
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* Module params for these addresses should come from ID proms.
|
2011-02-11 15:18:34 +00:00
|
|
|
* The host side address is used with CDC and RNDIS, and commonly
|
2009-07-29 16:18:43 +00:00
|
|
|
* ends up in a persistent config database. It's not clear if
|
|
|
|
* host side code for the SAFE thing cares -- its original BLAN
|
|
|
|
* thing didn't, Sharp never assigned those addresses on Zaurii.
|
|
|
|
*/
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
2009-07-29 16:18:43 +00:00
|
|
|
get_ether_addr(dev_addr, dev->net->enetaddr);
|
|
|
|
memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
|
2016-11-18 05:39:15 +00:00
|
|
|
#else
|
|
|
|
get_ether_addr(dev_addr, pdata->enetaddr);
|
|
|
|
memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
|
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
get_ether_addr(host_addr, dev->host_mac);
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
|
|
|
|
dev->host_mac[0], dev->host_mac[1],
|
|
|
|
dev->host_mac[2], dev->host_mac[3],
|
|
|
|
dev->host_mac[4], dev->host_mac[5]);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis) {
|
|
|
|
status = rndis_init();
|
|
|
|
if (status < 0) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("can't init RNDIS, %d", status);
|
2011-02-11 15:18:34 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* use PKTSIZE (or aligned... from u-boot) and set
|
|
|
|
* wMaxSegmentSize accordingly
|
|
|
|
*/
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
|
|
|
|
|
|
|
|
/* preallocate control message data and buffer */
|
2010-09-13 14:37:11 +00:00
|
|
|
dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (!dev->req)
|
|
|
|
goto fail;
|
|
|
|
dev->req->buf = control_req;
|
|
|
|
dev->req->complete = eth_setup_complete;
|
|
|
|
|
|
|
|
/* ... and maybe likewise for status transfer */
|
2012-10-02 15:04:31 +00:00
|
|
|
#if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS)
|
2009-07-29 16:18:43 +00:00
|
|
|
if (dev->status_ep) {
|
2010-09-13 14:37:11 +00:00
|
|
|
dev->stat_req = usb_ep_alloc_request(dev->status_ep,
|
|
|
|
GFP_KERNEL);
|
2009-07-29 16:18:43 +00:00
|
|
|
if (!dev->stat_req) {
|
2010-09-13 14:37:11 +00:00
|
|
|
usb_ep_free_request(dev->status_ep, dev->req);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-08-13 13:01:06 +00:00
|
|
|
dev->stat_req->buf = status_req;
|
2009-07-29 16:18:43 +00:00
|
|
|
dev->stat_req->context = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* finish hookup to lower layer ... */
|
|
|
|
dev->gadget = gadget;
|
2010-09-13 14:37:11 +00:00
|
|
|
set_gadget_data(gadget, dev);
|
2009-07-29 16:18:43 +00:00
|
|
|
gadget->ep0->driver_data = dev;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* two kinds of host-initiated state changes:
|
2009-07-29 16:18:43 +00:00
|
|
|
* - iff DATA transfer is active, carrier is "on"
|
|
|
|
* - tx queueing enabled if open *and* carrier is "on"
|
|
|
|
*/
|
2011-02-11 15:18:34 +00:00
|
|
|
|
|
|
|
printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
|
|
|
|
out_ep->name, in_ep->name,
|
|
|
|
status_ep ? " STATUS " : "",
|
|
|
|
status_ep ? status_ep->name : ""
|
|
|
|
);
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
|
|
|
printf("MAC %pM\n", dev->net->enetaddr);
|
|
|
|
#else
|
|
|
|
printf("MAC %pM\n", pdata->enetaddr);
|
|
|
|
#endif
|
2011-02-11 15:18:34 +00:00
|
|
|
|
|
|
|
if (cdc || rndis)
|
|
|
|
printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
|
|
|
|
dev->host_mac[0], dev->host_mac[1],
|
|
|
|
dev->host_mac[2], dev->host_mac[3],
|
|
|
|
dev->host_mac[4], dev->host_mac[5]);
|
|
|
|
|
|
|
|
if (rndis) {
|
|
|
|
u32 vendorID = 0;
|
|
|
|
|
|
|
|
/* FIXME RNDIS vendor id == "vendor NIC code" == ? */
|
|
|
|
|
|
|
|
dev->rndis_config = rndis_register(rndis_control_ack);
|
|
|
|
if (dev->rndis_config < 0) {
|
|
|
|
fail0:
|
|
|
|
eth_unbind(gadget);
|
|
|
|
debug("RNDIS setup failed\n");
|
|
|
|
status = -ENODEV;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* these set up a lot of the OIDs that RNDIS needs */
|
|
|
|
rndis_set_host_mac(dev->rndis_config, dev->host_mac);
|
|
|
|
if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
|
|
|
|
&dev->stats, &dev->cdc_filter))
|
|
|
|
goto fail0;
|
|
|
|
if (rndis_set_param_vendor(dev->rndis_config, vendorID,
|
|
|
|
manufacturer))
|
|
|
|
goto fail0;
|
|
|
|
if (rndis_set_param_medium(dev->rndis_config,
|
|
|
|
NDIS_MEDIUM_802_3, 0))
|
|
|
|
goto fail0;
|
|
|
|
printf("RNDIS ready\n");
|
|
|
|
}
|
2009-07-29 16:18:43 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("%s failed, status = %d", __func__, status);
|
2010-09-13 14:37:11 +00:00
|
|
|
eth_unbind(gadget);
|
2011-02-11 15:18:34 +00:00
|
|
|
return status;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2016-11-18 05:19:12 +00:00
|
|
|
#ifdef CONFIG_DM_USB
|
|
|
|
int dm_usb_init(struct eth_dev *e_dev)
|
|
|
|
{
|
|
|
|
struct udevice *dev = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
|
|
|
|
if (!dev || ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("No USB device found\n");
|
2016-11-18 05:19:12 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
e_dev->usb_udev = dev;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-18 05:38:27 +00:00
|
|
|
static int _usb_eth_init(struct ether_priv *priv)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2016-11-18 05:37:18 +00:00
|
|
|
struct eth_dev *dev = &priv->ethdev;
|
2009-07-29 16:18:43 +00:00
|
|
|
struct usb_gadget *gadget;
|
|
|
|
unsigned long ts;
|
|
|
|
unsigned long timeout = USB_CONNECT_TIMEOUT;
|
|
|
|
|
2016-11-18 05:19:12 +00:00
|
|
|
#ifdef CONFIG_DM_USB
|
|
|
|
if (dm_usb_init(dev)) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("USB ether not found\n");
|
2016-11-18 05:19:12 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#else
|
2015-08-19 08:19:46 +00:00
|
|
|
board_usb_init(0, USB_INIT_DEVICE);
|
2016-11-18 05:19:12 +00:00
|
|
|
#endif
|
2015-08-19 08:19:46 +00:00
|
|
|
|
2010-12-28 13:59:32 +00:00
|
|
|
/* Configure default mac-addresses for the USB ethernet device */
|
|
|
|
#ifdef CONFIG_USBNET_DEV_ADDR
|
|
|
|
strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_USBNET_HOST_ADDR
|
|
|
|
strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
|
|
|
|
#endif
|
|
|
|
/* Check if the user overruled the MAC addresses */
|
2017-08-03 18:22:12 +00:00
|
|
|
if (env_get("usbnet_devaddr"))
|
|
|
|
strlcpy(dev_addr, env_get("usbnet_devaddr"),
|
2010-12-28 13:59:32 +00:00
|
|
|
sizeof(dev_addr));
|
|
|
|
|
2017-08-03 18:22:12 +00:00
|
|
|
if (env_get("usbnet_hostaddr"))
|
|
|
|
strlcpy(host_addr, env_get("usbnet_hostaddr"),
|
2010-12-28 13:59:32 +00:00
|
|
|
sizeof(host_addr));
|
|
|
|
|
|
|
|
if (!is_eth_addr_valid(dev_addr)) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("Need valid 'usbnet_devaddr' to be set");
|
2010-12-28 13:59:32 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!is_eth_addr_valid(host_addr)) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("Need valid 'usbnet_hostaddr' to be set");
|
2010-12-28 13:59:32 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:37:18 +00:00
|
|
|
priv->eth_driver.speed = DEVSPEED;
|
|
|
|
priv->eth_driver.bind = eth_bind;
|
|
|
|
priv->eth_driver.unbind = eth_unbind;
|
|
|
|
priv->eth_driver.setup = eth_setup;
|
|
|
|
priv->eth_driver.reset = eth_disconnect;
|
|
|
|
priv->eth_driver.disconnect = eth_disconnect;
|
|
|
|
priv->eth_driver.suspend = eth_suspend;
|
|
|
|
priv->eth_driver.resume = eth_resume;
|
|
|
|
if (usb_gadget_register_driver(&priv->eth_driver) < 0)
|
2010-12-01 15:43:43 +00:00
|
|
|
goto fail;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
dev->network_started = 0;
|
|
|
|
|
|
|
|
packet_received = 0;
|
|
|
|
packet_sent = 0;
|
|
|
|
|
|
|
|
gadget = dev->gadget;
|
|
|
|
usb_gadget_connect(gadget);
|
|
|
|
|
2017-08-03 18:22:12 +00:00
|
|
|
if (env_get("cdc_connect_timeout"))
|
|
|
|
timeout = simple_strtoul(env_get("cdc_connect_timeout"),
|
2009-07-29 16:18:43 +00:00
|
|
|
NULL, 10) * CONFIG_SYS_HZ;
|
|
|
|
ts = get_timer(0);
|
2016-11-18 05:19:13 +00:00
|
|
|
while (!dev->network_started) {
|
2009-07-29 16:18:43 +00:00
|
|
|
/* Handle control-c and timeouts */
|
|
|
|
if (ctrlc() || (get_timer(ts) > timeout)) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("The remote end did not respond in time.");
|
2009-07-29 16:18:43 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2015-02-23 13:10:23 +00:00
|
|
|
usb_gadget_handle_interrupts(0);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-22 09:13:56 +00:00
|
|
|
packet_received = 0;
|
2010-09-13 14:37:11 +00:00
|
|
|
rx_submit(dev, dev->rx_req, 0);
|
2009-07-29 16:18:43 +00:00
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:38:27 +00:00
|
|
|
static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
|
|
|
int retval;
|
2011-02-11 15:18:34 +00:00
|
|
|
void *rndis_pkt = NULL;
|
2016-11-18 05:37:18 +00:00
|
|
|
struct eth_dev *dev = &priv->ethdev;
|
2010-09-22 09:13:55 +00:00
|
|
|
struct usb_request *req = dev->tx_req;
|
2010-09-13 14:37:11 +00:00
|
|
|
unsigned long ts;
|
|
|
|
unsigned long timeout = USB_CONNECT_TIMEOUT;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("%s:...\n", __func__);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2011-02-11 15:18:34 +00:00
|
|
|
/* new buffer is needed to include RNDIS header */
|
|
|
|
if (rndis_active(dev)) {
|
|
|
|
rndis_pkt = malloc(length +
|
|
|
|
sizeof(struct rndis_packet_msg_type));
|
|
|
|
if (!rndis_pkt) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("No memory to alloc RNDIS packet");
|
2011-02-11 15:18:34 +00:00
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
rndis_add_hdr(rndis_pkt, length);
|
|
|
|
memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
|
2012-05-22 18:36:19 +00:00
|
|
|
packet, length);
|
2011-02-11 15:18:34 +00:00
|
|
|
packet = rndis_pkt;
|
|
|
|
length += sizeof(struct rndis_packet_msg_type);
|
|
|
|
}
|
2012-05-22 18:36:19 +00:00
|
|
|
req->buf = packet;
|
2009-07-29 16:18:43 +00:00
|
|
|
req->context = NULL;
|
|
|
|
req->complete = tx_complete;
|
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
/*
|
|
|
|
* use zlp framing on tx for strict CDC-Ether conformance,
|
2009-07-29 16:18:43 +00:00
|
|
|
* though any robust network rx path ignores extra padding.
|
|
|
|
* and some hardware doesn't like to write zlps.
|
|
|
|
*/
|
|
|
|
req->zero = 1;
|
|
|
|
if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
|
|
|
|
length++;
|
|
|
|
|
|
|
|
req->length = length;
|
|
|
|
#if 0
|
|
|
|
/* throttle highspeed IRQ rate back slightly */
|
|
|
|
if (gadget_is_dualspeed(dev->gadget))
|
|
|
|
req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
|
|
|
|
? ((dev->tx_qlen % qmult) != 0) : 0;
|
|
|
|
#endif
|
2010-09-13 14:37:11 +00:00
|
|
|
dev->tx_qlen = 1;
|
|
|
|
ts = get_timer(0);
|
|
|
|
packet_sent = 0;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-09-13 14:37:11 +00:00
|
|
|
retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
if (!retval)
|
2010-08-13 12:57:51 +00:00
|
|
|
debug("%s: packet queued\n", __func__);
|
2010-09-13 14:37:11 +00:00
|
|
|
while (!packet_sent) {
|
|
|
|
if (get_timer(ts) > timeout) {
|
|
|
|
printf("timeout sending packets to usb ethernet\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-23 13:10:23 +00:00
|
|
|
usb_gadget_handle_interrupts(0);
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
2011-02-11 15:18:34 +00:00
|
|
|
if (rndis_pkt)
|
|
|
|
free(rndis_pkt);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-02-11 15:18:34 +00:00
|
|
|
drop:
|
|
|
|
dev->stats.tx_dropped++;
|
|
|
|
return -ENOMEM;
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 05:38:27 +00:00
|
|
|
static int _usb_eth_recv(struct ether_priv *priv)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2015-02-23 13:10:23 +00:00
|
|
|
usb_gadget_handle_interrupts(0);
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:38:27 +00:00
|
|
|
void _usb_eth_halt(struct ether_priv *priv)
|
2009-07-29 16:18:43 +00:00
|
|
|
{
|
2016-11-18 05:37:18 +00:00
|
|
|
struct eth_dev *dev = &priv->ethdev;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-12-01 15:43:43 +00:00
|
|
|
/* If the gadget not registered, simple return */
|
|
|
|
if (!dev->gadget)
|
|
|
|
return;
|
|
|
|
|
2011-02-11 15:18:35 +00:00
|
|
|
/*
|
|
|
|
* Some USB controllers may need additional deinitialization here
|
|
|
|
* before dropping pull-up (also due to hardware issues).
|
|
|
|
* For example: unhandled interrupt with status stage started may
|
|
|
|
* bring the controller to fully broken state (until board reset).
|
|
|
|
* There are some variants to debug and fix such cases:
|
|
|
|
* 1) In the case of RNDIS connection eth_stop can perform additional
|
|
|
|
* interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
|
|
|
|
* 2) 'pullup' callback in your UDC driver can be improved to perform
|
|
|
|
* this deinitialization.
|
|
|
|
*/
|
2011-02-11 15:18:34 +00:00
|
|
|
eth_stop(dev);
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
usb_gadget_disconnect(dev->gadget);
|
2011-02-11 15:18:31 +00:00
|
|
|
|
|
|
|
/* Clear pending interrupt */
|
|
|
|
if (dev->network_started) {
|
2015-02-23 13:10:23 +00:00
|
|
|
usb_gadget_handle_interrupts(0);
|
2011-02-11 15:18:31 +00:00
|
|
|
dev->network_started = 0;
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:37:18 +00:00
|
|
|
usb_gadget_unregister_driver(&priv->eth_driver);
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_USB
|
2015-08-19 08:19:46 +00:00
|
|
|
board_usb_cleanup(0, USB_INIT_DEVICE);
|
2016-11-18 05:19:12 +00:00
|
|
|
#endif
|
2009-07-29 16:18:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 05:39:15 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
2016-11-18 05:38:27 +00:00
|
|
|
static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = (struct ether_priv *)netdev->priv;
|
|
|
|
|
|
|
|
return _usb_eth_init(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = (struct ether_priv *)netdev->priv;
|
|
|
|
|
|
|
|
return _usb_eth_send(priv, packet, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_eth_recv(struct eth_device *netdev)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = (struct ether_priv *)netdev->priv;
|
|
|
|
struct eth_dev *dev = &priv->ethdev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = _usb_eth_recv(priv);
|
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("error packet receive\n");
|
2016-11-18 05:38:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!packet_received)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (dev->rx_req) {
|
|
|
|
net_process_received_packet(net_rx_packets[0],
|
|
|
|
dev->rx_req->length);
|
|
|
|
} else {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("dev->rx_req invalid");
|
2016-11-18 05:38:27 +00:00
|
|
|
}
|
|
|
|
packet_received = 0;
|
|
|
|
rx_submit(dev, dev->rx_req, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_eth_halt(struct eth_device *netdev)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = (struct ether_priv *)netdev->priv;
|
|
|
|
|
|
|
|
_usb_eth_halt(priv);
|
|
|
|
}
|
|
|
|
|
2009-07-29 16:18:43 +00:00
|
|
|
int usb_eth_initialize(bd_t *bi)
|
|
|
|
{
|
2016-11-18 05:36:13 +00:00
|
|
|
struct eth_device *netdev = &l_priv->netdev;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
2010-12-28 13:59:31 +00:00
|
|
|
strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
netdev->init = usb_eth_init;
|
|
|
|
netdev->send = usb_eth_send;
|
|
|
|
netdev->recv = usb_eth_recv;
|
|
|
|
netdev->halt = usb_eth_halt;
|
2016-11-18 05:37:18 +00:00
|
|
|
netdev->priv = l_priv;
|
2009-07-29 16:18:43 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
#error not supported
|
|
|
|
#endif
|
|
|
|
eth_register(netdev);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-18 05:39:15 +00:00
|
|
|
#else
|
|
|
|
static int usb_eth_start(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return _usb_eth_init(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_eth_send(struct udevice *dev, void *packet, int length)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return _usb_eth_send(priv, packet, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = dev_get_priv(dev);
|
|
|
|
struct eth_dev *ethdev = &priv->ethdev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = _usb_eth_recv(priv);
|
|
|
|
if (ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("error packet receive\n");
|
2016-11-18 05:39:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet_received) {
|
|
|
|
if (ethdev->rx_req) {
|
|
|
|
*packetp = (uchar *)net_rx_packets[0];
|
|
|
|
return ethdev->rx_req->length;
|
|
|
|
} else {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("dev->rx_req invalid");
|
2016-11-18 05:39:15 +00:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
|
|
|
|
int length)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = dev_get_priv(dev);
|
|
|
|
struct eth_dev *ethdev = &priv->ethdev;
|
|
|
|
|
|
|
|
packet_received = 0;
|
|
|
|
|
|
|
|
return rx_submit(ethdev, ethdev->rx_req, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_eth_stop(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
_usb_eth_halt(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_eth_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct ether_priv *priv = dev_get_priv(dev);
|
|
|
|
struct eth_pdata *pdata = dev_get_platdata(dev);
|
|
|
|
|
|
|
|
priv->netdev = dev;
|
|
|
|
l_priv = priv;
|
|
|
|
|
|
|
|
get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr);
|
2017-08-03 18:22:11 +00:00
|
|
|
eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr);
|
2016-11-18 05:39:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct eth_ops usb_eth_ops = {
|
|
|
|
.start = usb_eth_start,
|
|
|
|
.send = usb_eth_send,
|
|
|
|
.recv = usb_eth_recv,
|
|
|
|
.free_pkt = usb_eth_free_pkt,
|
|
|
|
.stop = usb_eth_stop,
|
|
|
|
};
|
|
|
|
|
|
|
|
int usb_ether_init(void)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
struct udevice *usb_dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
|
|
|
|
if (!usb_dev || ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("No USB device found\n");
|
2016-11-18 05:39:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
|
|
|
|
if (!dev || ret) {
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("usb - not able to bind usb_ether device\n");
|
2016-11-18 05:39:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(eth_usb) = {
|
|
|
|
.name = "usb_ether",
|
|
|
|
.id = UCLASS_ETH,
|
|
|
|
.probe = usb_eth_probe,
|
|
|
|
.ops = &usb_eth_ops,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct ether_priv),
|
|
|
|
.platdata_auto_alloc_size = sizeof(struct eth_pdata),
|
|
|
|
.flags = DM_FLAG_ALLOC_PRIV_DMA,
|
|
|
|
};
|
|
|
|
#endif /* CONFIG_DM_ETH */
|