Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2 | /* |
| 3 | * ether.c -- Ethernet gadget driver, with CDC and non-CDC options |
| 4 | * |
| 5 | * Copyright (C) 2003-2005,2008 David Brownell |
| 6 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 7 | * Copyright (C) 2008 Nokia Corporation |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 14 | #include <part.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 15 | #include <linux/errno.h> |
Vitaly Kuzmichev | 24fcb47 | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 16 | #include <linux/netdevice.h> |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 17 | #include <linux/usb/ch9.h> |
| 18 | #include <linux/usb/cdc.h> |
| 19 | #include <linux/usb/gadget.h> |
| 20 | #include <net.h> |
Kishon Vijay Abraham I | 8db2520 | 2015-08-19 13:49:46 +0530 | [diff] [blame] | 21 | #include <usb.h> |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 22 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 23 | #include <memalign.h> |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 24 | #include <linux/ctype.h> |
| 25 | |
| 26 | #include "gadget_chips.h" |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 27 | #include "rndis.h" |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 28 | |
Mugunthan V N | b3d902b | 2016-11-18 10:49:12 +0530 | [diff] [blame] | 29 | #include <dm.h> |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 30 | #include <dm/lists.h> |
Mugunthan V N | b3d902b | 2016-11-18 10:49:12 +0530 | [diff] [blame] | 31 | #include <dm/uclass-internal.h> |
| 32 | #include <dm/device-internal.h> |
| 33 | |
Vitaly Kuzmichev | a36cff1 | 2010-12-28 16:59:31 +0300 | [diff] [blame] | 34 | #define USB_NET_NAME "usb_ether" |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 35 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 36 | extern struct platform_data brd; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 37 | |
| 38 | |
| 39 | unsigned packet_received, packet_sent; |
| 40 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 41 | /* |
| 42 | * Ethernet gadget driver -- with CDC and non-CDC options |
| 43 | * Builds on hardware support for a full duplex link. |
| 44 | * |
| 45 | * CDC Ethernet is the standard USB solution for sending Ethernet frames |
| 46 | * using USB. Real hardware tends to use the same framing protocol but look |
| 47 | * different for control features. This driver strongly prefers to use |
| 48 | * this USB-IF standard as its open-systems interoperability solution; |
| 49 | * most host side USB stacks (except from Microsoft) support it. |
| 50 | * |
| 51 | * This is sometimes called "CDC ECM" (Ethernet Control Model) to support |
| 52 | * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new |
| 53 | * "CDC EEM" (Ethernet Emulation Model) is starting to spread. |
| 54 | * |
| 55 | * There's some hardware that can't talk CDC ECM. We make that hardware |
| 56 | * implement a "minimalist" vendor-agnostic CDC core: same framing, but |
| 57 | * link-level setup only requires activating the configuration. Only the |
| 58 | * endpoint descriptors, and product/vendor IDs, are relevant; no control |
| 59 | * operations are available. Linux supports it, but other host operating |
| 60 | * systems may not. (This is a subset of CDC Ethernet.) |
| 61 | * |
| 62 | * It turns out that if you add a few descriptors to that "CDC Subset", |
| 63 | * (Windows) host side drivers from MCCI can treat it as one submode of |
| 64 | * a proprietary scheme called "SAFE" ... without needing to know about |
| 65 | * specific product/vendor IDs. So we do that, making it easier to use |
| 66 | * those MS-Windows drivers. Those added descriptors make it resemble a |
| 67 | * CDC MDLM device, but they don't change device behavior at all. (See |
| 68 | * MCCI Engineering report 950198 "SAFE Networking Functions".) |
| 69 | * |
| 70 | * A third option is also in use. Rather than CDC Ethernet, or something |
| 71 | * simpler, Microsoft pushes their own approach: RNDIS. The published |
| 72 | * RNDIS specs are ambiguous and appear to be incomplete, and are also |
| 73 | * needlessly complex. They borrow more from CDC ACM than CDC ECM. |
| 74 | */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 75 | |
| 76 | #define DRIVER_DESC "Ethernet Gadget" |
| 77 | /* Based on linux 2.6.27 version */ |
| 78 | #define DRIVER_VERSION "May Day 2005" |
| 79 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 80 | static const char driver_desc[] = DRIVER_DESC; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 81 | |
| 82 | #define RX_EXTRA 20 /* guard against rx overflows */ |
| 83 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 84 | #ifndef CONFIG_USB_ETH_RNDIS |
| 85 | #define rndis_uninit(x) do {} while (0) |
| 86 | #define rndis_deregister(c) do {} while (0) |
| 87 | #define rndis_exit() do {} while (0) |
| 88 | #endif |
| 89 | |
| 90 | /* CDC and RNDIS support the same host-chosen outgoing packet filters. */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 91 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ |
| 92 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ |
| 93 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ |
| 94 | |USB_CDC_PACKET_TYPE_DIRECTED) |
| 95 | |
| 96 | #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ) |
| 97 | |
| 98 | /*-------------------------------------------------------------------------*/ |
Vitaly Kuzmichev | 737f6b2 | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 99 | |
| 100 | struct eth_dev { |
| 101 | struct usb_gadget *gadget; |
| 102 | struct usb_request *req; /* for control responses */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 103 | struct usb_request *stat_req; /* for cdc & rndis status */ |
Vitaly Kuzmichev | 737f6b2 | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 104 | |
| 105 | u8 config; |
| 106 | struct usb_ep *in_ep, *out_ep, *status_ep; |
| 107 | const struct usb_endpoint_descriptor |
| 108 | *in, *out, *status; |
| 109 | |
| 110 | struct usb_request *tx_req, *rx_req; |
| 111 | |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 112 | #ifndef CONFIG_DM_ETH |
Vitaly Kuzmichev | 737f6b2 | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 113 | struct eth_device *net; |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 114 | #else |
| 115 | struct udevice *net; |
| 116 | #endif |
Vitaly Kuzmichev | 737f6b2 | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 117 | struct net_device_stats stats; |
| 118 | unsigned int tx_qlen; |
| 119 | |
| 120 | unsigned zlp:1; |
| 121 | unsigned cdc:1; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 122 | unsigned rndis:1; |
Vitaly Kuzmichev | 737f6b2 | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 123 | unsigned suspended:1; |
| 124 | unsigned network_started:1; |
| 125 | u16 cdc_filter; |
| 126 | unsigned long todo; |
| 127 | int mtu; |
| 128 | #define WORK_RX_MEMORY 0 |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 129 | int rndis_config; |
Vitaly Kuzmichev | 737f6b2 | 2011-02-11 18:18:33 +0300 | [diff] [blame] | 130 | u8 host_mac[ETH_ALEN]; |
| 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * This version autoconfigures as much as possible at run-time. |
| 135 | * |
| 136 | * It also ASSUMES a self-powered device, without remote wakeup, |
| 137 | * although remote wakeup support would make sense. |
| 138 | */ |
| 139 | |
| 140 | /*-------------------------------------------------------------------------*/ |
Mugunthan V N | 88e48be | 2016-11-18 11:06:13 +0530 | [diff] [blame] | 141 | struct ether_priv { |
| 142 | struct eth_dev ethdev; |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 143 | #ifndef CONFIG_DM_ETH |
Mugunthan V N | 88e48be | 2016-11-18 11:06:13 +0530 | [diff] [blame] | 144 | struct eth_device netdev; |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 145 | #else |
| 146 | struct udevice *netdev; |
| 147 | #endif |
Mugunthan V N | 88e48be | 2016-11-18 11:06:13 +0530 | [diff] [blame] | 148 | struct usb_gadget_driver eth_driver; |
| 149 | }; |
| 150 | |
| 151 | struct ether_priv eth_priv; |
| 152 | struct ether_priv *l_priv = ð_priv; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 153 | |
| 154 | /*-------------------------------------------------------------------------*/ |
| 155 | |
| 156 | /* "main" config is either CDC, or its simple subset */ |
| 157 | static inline int is_cdc(struct eth_dev *dev) |
| 158 | { |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 159 | #if !defined(CONFIG_USB_ETH_SUBSET) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 160 | return 1; /* only cdc possible */ |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 161 | #elif !defined(CONFIG_USB_ETH_CDC) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 162 | return 0; /* only subset possible */ |
| 163 | #else |
| 164 | return dev->cdc; /* depends on what hardware we found */ |
| 165 | #endif |
| 166 | } |
| 167 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 168 | /* "secondary" RNDIS config may sometimes be activated */ |
| 169 | static inline int rndis_active(struct eth_dev *dev) |
| 170 | { |
| 171 | #ifdef CONFIG_USB_ETH_RNDIS |
| 172 | return dev->rndis; |
| 173 | #else |
| 174 | return 0; |
| 175 | #endif |
| 176 | } |
| 177 | |
| 178 | #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev)) |
| 179 | #define cdc_active(dev) (is_cdc(dev) && !rndis_active(dev)) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 180 | |
| 181 | #define DEFAULT_QLEN 2 /* double buffering by default */ |
| 182 | |
| 183 | /* peak bulk transfer bits-per-second */ |
| 184 | #define HS_BPS (13 * 512 * 8 * 1000 * 8) |
| 185 | #define FS_BPS (19 * 64 * 1 * 1000 * 8) |
| 186 | |
| 187 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
| 188 | #define DEVSPEED USB_SPEED_HIGH |
| 189 | |
Vitaly Kuzmichev | aec83b8 | 2010-08-12 16:44:40 +0400 | [diff] [blame] | 190 | #ifdef CONFIG_USB_ETH_QMULT |
| 191 | #define qmult CONFIG_USB_ETH_QMULT |
| 192 | #else |
| 193 | #define qmult 5 |
| 194 | #endif |
| 195 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 196 | /* for dual-speed hardware, use deeper queues at highspeed */ |
| 197 | #define qlen(gadget) \ |
| 198 | (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1)) |
| 199 | |
| 200 | static inline int BITRATE(struct usb_gadget *g) |
| 201 | { |
| 202 | return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS; |
| 203 | } |
| 204 | |
| 205 | #else /* full speed (low speed doesn't do bulk) */ |
| 206 | |
| 207 | #define qmult 1 |
| 208 | |
| 209 | #define DEVSPEED USB_SPEED_FULL |
| 210 | |
| 211 | #define qlen(gadget) DEFAULT_QLEN |
| 212 | |
| 213 | static inline int BITRATE(struct usb_gadget *g) |
| 214 | { |
| 215 | return FS_BPS; |
| 216 | } |
| 217 | #endif |
| 218 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 219 | /*-------------------------------------------------------------------------*/ |
| 220 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 221 | /* |
| 222 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 223 | * Instead: allocate your own, using normal USB-IF procedures. |
| 224 | */ |
| 225 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 226 | /* |
| 227 | * Thanks to NetChip Technologies for donating this product ID. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 228 | * It's for devices with only CDC Ethernet configurations. |
| 229 | */ |
| 230 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ |
| 231 | #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */ |
| 232 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 233 | /* |
| 234 | * For hardware that can't talk CDC, we use the same vendor ID that |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 235 | * ARM Linux has used for ethernet-over-usb, both with sa1100 and |
| 236 | * with pxa250. We're protocol-compatible, if the host-side drivers |
| 237 | * use the endpoint descriptors. bcdDevice (version) is nonzero, so |
| 238 | * drivers that need to hard-wire endpoint numbers have a hook. |
| 239 | * |
| 240 | * The protocol is a minimal subset of CDC Ether, which works on any bulk |
| 241 | * hardware that's not deeply broken ... even on hardware that can't talk |
| 242 | * RNDIS (like SA-1100, with no interrupt endpoint, or anything that |
| 243 | * doesn't handle control-OUT). |
| 244 | */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 245 | #define SIMPLE_VENDOR_NUM 0x049f /* Compaq Computer Corp. */ |
| 246 | #define SIMPLE_PRODUCT_NUM 0x505a /* Linux-USB "CDC Subset" Device */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 247 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 248 | /* |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 249 | * For hardware that can talk RNDIS and either of the above protocols, |
| 250 | * use this ID ... the windows INF files will know it. Unless it's |
| 251 | * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose |
| 252 | * the non-RNDIS configuration. |
| 253 | */ |
| 254 | #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */ |
| 255 | #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */ |
| 256 | |
| 257 | /* |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 258 | * Some systems will want different product identifers published in the |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 259 | * device descriptor, either numbers or strings or both. These string |
| 260 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). |
| 261 | */ |
| 262 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 263 | /* |
| 264 | * Emulating them in eth_bind: |
| 265 | * static ushort idVendor; |
| 266 | * static ushort idProduct; |
| 267 | */ |
| 268 | |
Maxime Ripard | 48e78f8 | 2017-09-07 09:15:08 +0200 | [diff] [blame] | 269 | #if defined(CONFIG_USB_GADGET_MANUFACTURER) |
| 270 | static char *iManufacturer = CONFIG_USB_GADGET_MANUFACTURER; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 271 | #else |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 272 | static char *iManufacturer = "U-Boot"; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 273 | #endif |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 274 | |
| 275 | /* These probably need to be configurable. */ |
| 276 | static ushort bcdDevice; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 277 | static char *iProduct; |
| 278 | static char *iSerialNumber; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 279 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 280 | static char dev_addr[18]; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 281 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 282 | static char host_addr[18]; |
| 283 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 284 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 285 | /*-------------------------------------------------------------------------*/ |
| 286 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 287 | /* |
| 288 | * USB DRIVER HOOKUP (to the hardware driver, below us), mostly |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 289 | * ep0 implementation: descriptors, config management, setup(). |
| 290 | * also optional class-specific notification interrupt transfer. |
| 291 | */ |
| 292 | |
| 293 | /* |
| 294 | * DESCRIPTORS ... most are static, but strings and (full) configuration |
| 295 | * descriptors are built on demand. For now we do either full CDC, or |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 296 | * our simple subset, with RNDIS as an optional second configuration. |
| 297 | * |
| 298 | * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But |
| 299 | * the class descriptors match a modem (they're ignored; it's really just |
| 300 | * Ethernet functionality), they don't need the NOP altsetting, and the |
| 301 | * status transfer endpoint isn't optional. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 302 | */ |
| 303 | |
| 304 | #define STRING_MANUFACTURER 1 |
| 305 | #define STRING_PRODUCT 2 |
| 306 | #define STRING_ETHADDR 3 |
| 307 | #define STRING_DATA 4 |
| 308 | #define STRING_CONTROL 5 |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 309 | #define STRING_RNDIS_CONTROL 6 |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 310 | #define STRING_CDC 7 |
| 311 | #define STRING_SUBSET 8 |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 312 | #define STRING_RNDIS 9 |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 313 | #define STRING_SERIALNUMBER 10 |
| 314 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 315 | /* holds our biggest descriptor (or RNDIS response) */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 316 | #define USB_BUFSIZ 256 |
| 317 | |
| 318 | /* |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 319 | * This device advertises one configuration, eth_config, unless RNDIS |
| 320 | * is enabled (rndis_config) on hardware supporting at least two configs. |
| 321 | * |
| 322 | * NOTE: Controllers like superh_udc should probably be able to use |
| 323 | * an RNDIS-only configuration. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 324 | * |
| 325 | * FIXME define some higher-powered configurations to make it easier |
| 326 | * to recharge batteries ... |
| 327 | */ |
| 328 | |
| 329 | #define DEV_CONFIG_VALUE 1 /* cdc or subset */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 330 | #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 331 | |
| 332 | static struct usb_device_descriptor |
| 333 | device_desc = { |
| 334 | .bLength = sizeof device_desc, |
| 335 | .bDescriptorType = USB_DT_DEVICE, |
| 336 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 337 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 338 | |
| 339 | .bDeviceClass = USB_CLASS_COMM, |
| 340 | .bDeviceSubClass = 0, |
| 341 | .bDeviceProtocol = 0, |
| 342 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 343 | .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), |
| 344 | .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 345 | .iManufacturer = STRING_MANUFACTURER, |
| 346 | .iProduct = STRING_PRODUCT, |
| 347 | .bNumConfigurations = 1, |
| 348 | }; |
| 349 | |
| 350 | static struct usb_otg_descriptor |
| 351 | otg_descriptor = { |
| 352 | .bLength = sizeof otg_descriptor, |
| 353 | .bDescriptorType = USB_DT_OTG, |
| 354 | |
| 355 | .bmAttributes = USB_OTG_SRP, |
| 356 | }; |
| 357 | |
| 358 | static struct usb_config_descriptor |
| 359 | eth_config = { |
| 360 | .bLength = sizeof eth_config, |
| 361 | .bDescriptorType = USB_DT_CONFIG, |
| 362 | |
| 363 | /* compute wTotalLength on the fly */ |
| 364 | .bNumInterfaces = 2, |
| 365 | .bConfigurationValue = DEV_CONFIG_VALUE, |
| 366 | .iConfiguration = STRING_CDC, |
| 367 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 368 | .bMaxPower = 1, |
| 369 | }; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 370 | |
| 371 | #ifdef CONFIG_USB_ETH_RNDIS |
| 372 | static struct usb_config_descriptor |
| 373 | rndis_config = { |
| 374 | .bLength = sizeof rndis_config, |
| 375 | .bDescriptorType = USB_DT_CONFIG, |
| 376 | |
| 377 | /* compute wTotalLength on the fly */ |
| 378 | .bNumInterfaces = 2, |
| 379 | .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE, |
| 380 | .iConfiguration = STRING_RNDIS, |
| 381 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 382 | .bMaxPower = 1, |
| 383 | }; |
| 384 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 385 | |
| 386 | /* |
| 387 | * Compared to the simple CDC subset, the full CDC Ethernet model adds |
| 388 | * three class descriptors, two interface descriptors, optional status |
| 389 | * endpoint. Both have a "data" interface and two bulk endpoints. |
| 390 | * There are also differences in how control requests are handled. |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 391 | * |
| 392 | * RNDIS shares a lot with CDC-Ethernet, since it's a variant of the |
| 393 | * CDC-ACM (modem) spec. Unfortunately MSFT's RNDIS driver is buggy; it |
| 394 | * may hang or oops. Since bugfixes (or accurate specs, letting Linux |
| 395 | * work around those bugs) are unlikely to ever come from MSFT, you may |
| 396 | * wish to avoid using RNDIS. |
| 397 | * |
| 398 | * MCCI offers an alternative to RNDIS if you need to connect to Windows |
| 399 | * but have hardware that can't support CDC Ethernet. We add descriptors |
| 400 | * to present the CDC Subset as a (nonconformant) CDC MDLM variant called |
| 401 | * "SAFE". That borrows from both CDC Ethernet and CDC MDLM. You can |
| 402 | * get those drivers from MCCI, or bundled with various products. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 403 | */ |
| 404 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 405 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 406 | static struct usb_interface_descriptor |
| 407 | control_intf = { |
| 408 | .bLength = sizeof control_intf, |
| 409 | .bDescriptorType = USB_DT_INTERFACE, |
| 410 | |
| 411 | .bInterfaceNumber = 0, |
| 412 | /* status endpoint is optional; this may be patched later */ |
| 413 | .bNumEndpoints = 1, |
| 414 | .bInterfaceClass = USB_CLASS_COMM, |
| 415 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, |
| 416 | .bInterfaceProtocol = USB_CDC_PROTO_NONE, |
| 417 | .iInterface = STRING_CONTROL, |
| 418 | }; |
| 419 | #endif |
| 420 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 421 | #ifdef CONFIG_USB_ETH_RNDIS |
| 422 | static const struct usb_interface_descriptor |
| 423 | rndis_control_intf = { |
| 424 | .bLength = sizeof rndis_control_intf, |
| 425 | .bDescriptorType = USB_DT_INTERFACE, |
| 426 | |
| 427 | .bInterfaceNumber = 0, |
| 428 | .bNumEndpoints = 1, |
| 429 | .bInterfaceClass = USB_CLASS_COMM, |
| 430 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 431 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, |
| 432 | .iInterface = STRING_RNDIS_CONTROL, |
| 433 | }; |
| 434 | #endif |
| 435 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 436 | static const struct usb_cdc_header_desc header_desc = { |
| 437 | .bLength = sizeof header_desc, |
| 438 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 439 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 440 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 441 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 442 | }; |
| 443 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 444 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 445 | |
| 446 | static const struct usb_cdc_union_desc union_desc = { |
| 447 | .bLength = sizeof union_desc, |
| 448 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 449 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 450 | |
| 451 | .bMasterInterface0 = 0, /* index of control interface */ |
| 452 | .bSlaveInterface0 = 1, /* index of DATA interface */ |
| 453 | }; |
| 454 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 455 | #endif /* CDC || RNDIS */ |
| 456 | |
| 457 | #ifdef CONFIG_USB_ETH_RNDIS |
| 458 | |
| 459 | static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { |
| 460 | .bLength = sizeof call_mgmt_descriptor, |
| 461 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 462 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 463 | |
| 464 | .bmCapabilities = 0x00, |
| 465 | .bDataInterface = 0x01, |
| 466 | }; |
| 467 | |
| 468 | static const struct usb_cdc_acm_descriptor acm_descriptor = { |
| 469 | .bLength = sizeof acm_descriptor, |
| 470 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 471 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
| 472 | |
| 473 | .bmCapabilities = 0x00, |
| 474 | }; |
| 475 | |
| 476 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 477 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 478 | #ifndef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 479 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 480 | /* |
| 481 | * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 482 | * ways: data endpoints live in the control interface, there's no data |
| 483 | * interface, and it's not used to talk to a cell phone radio. |
| 484 | */ |
| 485 | |
| 486 | static const struct usb_cdc_mdlm_desc mdlm_desc = { |
| 487 | .bLength = sizeof mdlm_desc, |
| 488 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 489 | .bDescriptorSubType = USB_CDC_MDLM_TYPE, |
| 490 | |
| 491 | .bcdVersion = __constant_cpu_to_le16(0x0100), |
| 492 | .bGUID = { |
| 493 | 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6, |
| 494 | 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f, |
| 495 | }, |
| 496 | }; |
| 497 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 498 | /* |
| 499 | * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 500 | * can't really use its struct. All we do here is say that we're using |
| 501 | * the submode of "SAFE" which directly matches the CDC Subset. |
| 502 | */ |
Lokesh Vutla | ae6b2df | 2017-01-17 08:51:22 +0530 | [diff] [blame] | 503 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 504 | static const u8 mdlm_detail_desc[] = { |
| 505 | 6, |
| 506 | USB_DT_CS_INTERFACE, |
| 507 | USB_CDC_MDLM_DETAIL_TYPE, |
| 508 | |
| 509 | 0, /* "SAFE" */ |
| 510 | 0, /* network control capabilities (none) */ |
| 511 | 0, /* network data capabilities ("raw" encapsulation) */ |
| 512 | }; |
Lokesh Vutla | ae6b2df | 2017-01-17 08:51:22 +0530 | [diff] [blame] | 513 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 514 | |
| 515 | #endif |
| 516 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 517 | static const struct usb_cdc_ether_desc ether_desc = { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 518 | .bLength = sizeof(ether_desc), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 519 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 520 | .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, |
| 521 | |
| 522 | /* this descriptor actually adds value, surprise! */ |
| 523 | .iMACAddress = STRING_ETHADDR, |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 524 | .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */ |
Bin Meng | 2ccb8b0 | 2018-07-31 02:55:22 -0700 | [diff] [blame] | 525 | .wMaxSegmentSize = __constant_cpu_to_le16(PKTSIZE_ALIGN), |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 526 | .wNumberMCFilters = __constant_cpu_to_le16(0), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 527 | .bNumberPowerFilters = 0, |
| 528 | }; |
| 529 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 530 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 531 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 532 | /* |
| 533 | * include the status endpoint if we can, even where it's optional. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 534 | * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one |
| 535 | * packet, to simplify cancellation; and a big transfer interval, to |
| 536 | * waste less bandwidth. |
| 537 | * |
| 538 | * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even |
| 539 | * if they ignore the connect/disconnect notifications that real aether |
| 540 | * can provide. more advanced cdc configurations might want to support |
| 541 | * encapsulated commands (vendor-specific, using control-OUT). |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 542 | * |
| 543 | * RNDIS requires the status endpoint, since it uses that encapsulation |
| 544 | * mechanism for its funky RPC scheme. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 545 | */ |
| 546 | |
| 547 | #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */ |
| 548 | #define STATUS_BYTECOUNT 16 /* 8 byte header + data */ |
| 549 | |
| 550 | static struct usb_endpoint_descriptor |
| 551 | fs_status_desc = { |
| 552 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 553 | .bDescriptorType = USB_DT_ENDPOINT, |
| 554 | |
| 555 | .bEndpointAddress = USB_DIR_IN, |
| 556 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 557 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 558 | .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, |
| 559 | }; |
| 560 | #endif |
| 561 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 562 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 563 | |
| 564 | /* the default data interface has no endpoints ... */ |
| 565 | |
| 566 | static const struct usb_interface_descriptor |
| 567 | data_nop_intf = { |
| 568 | .bLength = sizeof data_nop_intf, |
| 569 | .bDescriptorType = USB_DT_INTERFACE, |
| 570 | |
| 571 | .bInterfaceNumber = 1, |
| 572 | .bAlternateSetting = 0, |
| 573 | .bNumEndpoints = 0, |
| 574 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 575 | .bInterfaceSubClass = 0, |
| 576 | .bInterfaceProtocol = 0, |
| 577 | }; |
| 578 | |
| 579 | /* ... but the "real" data interface has two bulk endpoints */ |
| 580 | |
| 581 | static const struct usb_interface_descriptor |
| 582 | data_intf = { |
| 583 | .bLength = sizeof data_intf, |
| 584 | .bDescriptorType = USB_DT_INTERFACE, |
| 585 | |
| 586 | .bInterfaceNumber = 1, |
| 587 | .bAlternateSetting = 1, |
| 588 | .bNumEndpoints = 2, |
| 589 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 590 | .bInterfaceSubClass = 0, |
| 591 | .bInterfaceProtocol = 0, |
| 592 | .iInterface = STRING_DATA, |
| 593 | }; |
| 594 | |
| 595 | #endif |
| 596 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 597 | #ifdef CONFIG_USB_ETH_RNDIS |
| 598 | |
| 599 | /* RNDIS doesn't activate by changing to the "real" altsetting */ |
| 600 | |
| 601 | static const struct usb_interface_descriptor |
| 602 | rndis_data_intf = { |
| 603 | .bLength = sizeof rndis_data_intf, |
| 604 | .bDescriptorType = USB_DT_INTERFACE, |
| 605 | |
| 606 | .bInterfaceNumber = 1, |
| 607 | .bAlternateSetting = 0, |
| 608 | .bNumEndpoints = 2, |
| 609 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 610 | .bInterfaceSubClass = 0, |
| 611 | .bInterfaceProtocol = 0, |
| 612 | .iInterface = STRING_DATA, |
| 613 | }; |
| 614 | |
| 615 | #endif |
| 616 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 617 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 618 | |
| 619 | /* |
| 620 | * "Simple" CDC-subset option is a simple vendor-neutral model that most |
| 621 | * full speed controllers can handle: one interface, two bulk endpoints. |
| 622 | * |
| 623 | * To assist host side drivers, we fancy it up a bit, and add descriptors |
| 624 | * so some host side drivers will understand it as a "SAFE" variant. |
| 625 | */ |
| 626 | |
| 627 | static const struct usb_interface_descriptor |
| 628 | subset_data_intf = { |
| 629 | .bLength = sizeof subset_data_intf, |
| 630 | .bDescriptorType = USB_DT_INTERFACE, |
| 631 | |
| 632 | .bInterfaceNumber = 0, |
| 633 | .bAlternateSetting = 0, |
| 634 | .bNumEndpoints = 2, |
| 635 | .bInterfaceClass = USB_CLASS_COMM, |
| 636 | .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM, |
| 637 | .bInterfaceProtocol = 0, |
| 638 | .iInterface = STRING_DATA, |
| 639 | }; |
| 640 | |
| 641 | #endif /* SUBSET */ |
| 642 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 643 | static struct usb_endpoint_descriptor |
| 644 | fs_source_desc = { |
| 645 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 646 | .bDescriptorType = USB_DT_ENDPOINT, |
| 647 | |
| 648 | .bEndpointAddress = USB_DIR_IN, |
| 649 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Troy Kisky | dbadc54 | 2013-09-25 18:41:04 -0700 | [diff] [blame] | 650 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 651 | }; |
| 652 | |
| 653 | static struct usb_endpoint_descriptor |
| 654 | fs_sink_desc = { |
| 655 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 656 | .bDescriptorType = USB_DT_ENDPOINT, |
| 657 | |
| 658 | .bEndpointAddress = USB_DIR_OUT, |
| 659 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Troy Kisky | dbadc54 | 2013-09-25 18:41:04 -0700 | [diff] [blame] | 660 | .wMaxPacketSize = __constant_cpu_to_le16(64), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 661 | }; |
| 662 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 663 | static const struct usb_descriptor_header *fs_eth_function[11] = { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 664 | (struct usb_descriptor_header *) &otg_descriptor, |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 665 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 666 | /* "cdc" mode descriptors */ |
| 667 | (struct usb_descriptor_header *) &control_intf, |
| 668 | (struct usb_descriptor_header *) &header_desc, |
| 669 | (struct usb_descriptor_header *) &union_desc, |
| 670 | (struct usb_descriptor_header *) ðer_desc, |
| 671 | /* NOTE: status endpoint may need to be removed */ |
| 672 | (struct usb_descriptor_header *) &fs_status_desc, |
| 673 | /* data interface, with altsetting */ |
| 674 | (struct usb_descriptor_header *) &data_nop_intf, |
| 675 | (struct usb_descriptor_header *) &data_intf, |
| 676 | (struct usb_descriptor_header *) &fs_source_desc, |
| 677 | (struct usb_descriptor_header *) &fs_sink_desc, |
| 678 | NULL, |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 679 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 680 | }; |
| 681 | |
| 682 | static inline void fs_subset_descriptors(void) |
| 683 | { |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 684 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 685 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
| 686 | fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; |
| 687 | fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; |
| 688 | fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; |
| 689 | fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; |
| 690 | fs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; |
| 691 | fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc; |
| 692 | fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc; |
| 693 | fs_eth_function[8] = NULL; |
| 694 | #else |
| 695 | fs_eth_function[1] = NULL; |
| 696 | #endif |
| 697 | } |
| 698 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 699 | #ifdef CONFIG_USB_ETH_RNDIS |
| 700 | static const struct usb_descriptor_header *fs_rndis_function[] = { |
| 701 | (struct usb_descriptor_header *) &otg_descriptor, |
| 702 | /* control interface matches ACM, not Ethernet */ |
| 703 | (struct usb_descriptor_header *) &rndis_control_intf, |
| 704 | (struct usb_descriptor_header *) &header_desc, |
| 705 | (struct usb_descriptor_header *) &call_mgmt_descriptor, |
| 706 | (struct usb_descriptor_header *) &acm_descriptor, |
| 707 | (struct usb_descriptor_header *) &union_desc, |
| 708 | (struct usb_descriptor_header *) &fs_status_desc, |
| 709 | /* data interface has no altsetting */ |
| 710 | (struct usb_descriptor_header *) &rndis_data_intf, |
| 711 | (struct usb_descriptor_header *) &fs_source_desc, |
| 712 | (struct usb_descriptor_header *) &fs_sink_desc, |
| 713 | NULL, |
| 714 | }; |
| 715 | #endif |
| 716 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 717 | /* |
| 718 | * usb 2.0 devices need to expose both high speed and full speed |
| 719 | * descriptors, unless they only run at full speed. |
| 720 | */ |
| 721 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 722 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 723 | static struct usb_endpoint_descriptor |
| 724 | hs_status_desc = { |
| 725 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 726 | .bDescriptorType = USB_DT_ENDPOINT, |
| 727 | |
| 728 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 729 | .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 730 | .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, |
| 731 | }; |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 732 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 733 | |
| 734 | static struct usb_endpoint_descriptor |
| 735 | hs_source_desc = { |
| 736 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 737 | .bDescriptorType = USB_DT_ENDPOINT, |
| 738 | |
| 739 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 740 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 741 | }; |
| 742 | |
| 743 | static struct usb_endpoint_descriptor |
| 744 | hs_sink_desc = { |
| 745 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 746 | .bDescriptorType = USB_DT_ENDPOINT, |
| 747 | |
| 748 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 749 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | static struct usb_qualifier_descriptor |
| 753 | dev_qualifier = { |
| 754 | .bLength = sizeof dev_qualifier, |
| 755 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, |
| 756 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 757 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 758 | .bDeviceClass = USB_CLASS_COMM, |
| 759 | |
| 760 | .bNumConfigurations = 1, |
| 761 | }; |
| 762 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 763 | static const struct usb_descriptor_header *hs_eth_function[11] = { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 764 | (struct usb_descriptor_header *) &otg_descriptor, |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 765 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 766 | /* "cdc" mode descriptors */ |
| 767 | (struct usb_descriptor_header *) &control_intf, |
| 768 | (struct usb_descriptor_header *) &header_desc, |
| 769 | (struct usb_descriptor_header *) &union_desc, |
| 770 | (struct usb_descriptor_header *) ðer_desc, |
| 771 | /* NOTE: status endpoint may need to be removed */ |
| 772 | (struct usb_descriptor_header *) &hs_status_desc, |
| 773 | /* data interface, with altsetting */ |
| 774 | (struct usb_descriptor_header *) &data_nop_intf, |
| 775 | (struct usb_descriptor_header *) &data_intf, |
| 776 | (struct usb_descriptor_header *) &hs_source_desc, |
| 777 | (struct usb_descriptor_header *) &hs_sink_desc, |
| 778 | NULL, |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 779 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 780 | }; |
| 781 | |
| 782 | static inline void hs_subset_descriptors(void) |
| 783 | { |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 784 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 785 | /* behavior is "CDC Subset"; extra descriptors say "SAFE" */ |
| 786 | hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf; |
| 787 | hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc; |
| 788 | hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc; |
| 789 | hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc; |
| 790 | hs_eth_function[5] = (struct usb_descriptor_header *) ðer_desc; |
| 791 | hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc; |
| 792 | hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc; |
| 793 | hs_eth_function[8] = NULL; |
| 794 | #else |
| 795 | hs_eth_function[1] = NULL; |
| 796 | #endif |
| 797 | } |
| 798 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 799 | #ifdef CONFIG_USB_ETH_RNDIS |
| 800 | static const struct usb_descriptor_header *hs_rndis_function[] = { |
| 801 | (struct usb_descriptor_header *) &otg_descriptor, |
| 802 | /* control interface matches ACM, not Ethernet */ |
| 803 | (struct usb_descriptor_header *) &rndis_control_intf, |
| 804 | (struct usb_descriptor_header *) &header_desc, |
| 805 | (struct usb_descriptor_header *) &call_mgmt_descriptor, |
| 806 | (struct usb_descriptor_header *) &acm_descriptor, |
| 807 | (struct usb_descriptor_header *) &union_desc, |
| 808 | (struct usb_descriptor_header *) &hs_status_desc, |
| 809 | /* data interface has no altsetting */ |
| 810 | (struct usb_descriptor_header *) &rndis_data_intf, |
| 811 | (struct usb_descriptor_header *) &hs_source_desc, |
| 812 | (struct usb_descriptor_header *) &hs_sink_desc, |
| 813 | NULL, |
| 814 | }; |
| 815 | #endif |
| 816 | |
| 817 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 818 | /* maxpacket and other transfer characteristics vary by speed. */ |
| 819 | static inline struct usb_endpoint_descriptor * |
| 820 | ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, |
| 821 | struct usb_endpoint_descriptor *fs) |
| 822 | { |
| 823 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 824 | return hs; |
| 825 | return fs; |
| 826 | } |
| 827 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 828 | /*-------------------------------------------------------------------------*/ |
| 829 | |
| 830 | /* descriptors that are built on-demand */ |
| 831 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 832 | static char manufacturer[50]; |
| 833 | static char product_desc[40] = DRIVER_DESC; |
| 834 | static char serial_number[20]; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 835 | |
| 836 | /* address that the host will use ... usually assigned at random */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 837 | static char ethaddr[2 * ETH_ALEN + 1]; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 838 | |
| 839 | /* static strings, in UTF-8 */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 840 | static struct usb_string strings[] = { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 841 | { STRING_MANUFACTURER, manufacturer, }, |
| 842 | { STRING_PRODUCT, product_desc, }, |
| 843 | { STRING_SERIALNUMBER, serial_number, }, |
| 844 | { STRING_DATA, "Ethernet Data", }, |
| 845 | { STRING_ETHADDR, ethaddr, }, |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 846 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 847 | { STRING_CDC, "CDC Ethernet", }, |
| 848 | { STRING_CONTROL, "CDC Communications Control", }, |
| 849 | #endif |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 850 | #ifdef CONFIG_USB_ETH_SUBSET |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 851 | { STRING_SUBSET, "CDC Ethernet Subset", }, |
| 852 | #endif |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 853 | #ifdef CONFIG_USB_ETH_RNDIS |
| 854 | { STRING_RNDIS, "RNDIS", }, |
| 855 | { STRING_RNDIS_CONTROL, "RNDIS Communications Control", }, |
| 856 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 857 | { } /* end of list */ |
| 858 | }; |
| 859 | |
| 860 | static struct usb_gadget_strings stringtab = { |
| 861 | .language = 0x0409, /* en-us */ |
| 862 | .strings = strings, |
| 863 | }; |
| 864 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 865 | /*============================================================================*/ |
Joel Fernandes | 00b7839 | 2013-09-04 18:55:14 -0500 | [diff] [blame] | 866 | DEFINE_CACHE_ALIGN_BUFFER(u8, control_req, USB_BUFSIZ); |
| 867 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 868 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Joel Fernandes | 00b7839 | 2013-09-04 18:55:14 -0500 | [diff] [blame] | 869 | DEFINE_CACHE_ALIGN_BUFFER(u8, status_req, STATUS_BYTECOUNT); |
Lukasz Dalek | 6ca42dd | 2012-10-02 17:04:29 +0200 | [diff] [blame] | 870 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 871 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 872 | /*============================================================================*/ |
| 873 | |
| 874 | /* |
| 875 | * one config, two interfaces: control, data. |
| 876 | * complications: class descriptors, and an altsetting. |
| 877 | */ |
| 878 | static int |
| 879 | config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg) |
| 880 | { |
| 881 | int len; |
| 882 | const struct usb_config_descriptor *config; |
| 883 | const struct usb_descriptor_header **function; |
| 884 | int hs = 0; |
| 885 | |
| 886 | if (gadget_is_dualspeed(g)) { |
| 887 | hs = (g->speed == USB_SPEED_HIGH); |
| 888 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 889 | hs = !hs; |
| 890 | } |
| 891 | #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function) |
| 892 | |
| 893 | if (index >= device_desc.bNumConfigurations) |
| 894 | return -EINVAL; |
| 895 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 896 | #ifdef CONFIG_USB_ETH_RNDIS |
| 897 | /* |
| 898 | * list the RNDIS config first, to make Microsoft's drivers |
| 899 | * happy. DOCSIS 1.0 needs this too. |
| 900 | */ |
| 901 | if (device_desc.bNumConfigurations == 2 && index == 0) { |
| 902 | config = &rndis_config; |
| 903 | function = which_fn(rndis); |
| 904 | } else |
| 905 | #endif |
| 906 | { |
| 907 | config = ð_config; |
| 908 | function = which_fn(eth); |
| 909 | } |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 910 | |
| 911 | /* for now, don't advertise srp-only devices */ |
| 912 | if (!is_otg) |
| 913 | function++; |
| 914 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 915 | len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 916 | if (len < 0) |
| 917 | return len; |
| 918 | ((struct usb_config_descriptor *) buf)->bDescriptorType = type; |
| 919 | return len; |
| 920 | } |
| 921 | |
| 922 | /*-------------------------------------------------------------------------*/ |
| 923 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 924 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 925 | static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 926 | |
| 927 | static int |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 928 | set_ether_config(struct eth_dev *dev, gfp_t gfp_flags) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 929 | { |
| 930 | int result = 0; |
| 931 | struct usb_gadget *gadget = dev->gadget; |
| 932 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 933 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 934 | /* status endpoint used for RNDIS and (optionally) CDC */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 935 | if (!subset_active(dev) && dev->status_ep) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 936 | dev->status = ep_desc(gadget, &hs_status_desc, |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 937 | &fs_status_desc); |
| 938 | dev->status_ep->driver_data = dev; |
| 939 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 940 | result = usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 941 | if (result != 0) { |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 942 | debug("enable %s --> %d\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 943 | dev->status_ep->name, result); |
| 944 | goto done; |
| 945 | } |
| 946 | } |
| 947 | #endif |
| 948 | |
| 949 | dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc); |
| 950 | dev->in_ep->driver_data = dev; |
| 951 | |
| 952 | dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc); |
| 953 | dev->out_ep->driver_data = dev; |
| 954 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 955 | /* |
| 956 | * With CDC, the host isn't allowed to use these two data |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 957 | * endpoints in the default altsetting for the interface. |
| 958 | * so we don't activate them yet. Reset from SET_INTERFACE. |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 959 | * |
| 960 | * Strictly speaking RNDIS should work the same: activation is |
| 961 | * a side effect of setting a packet filter. Deactivation is |
| 962 | * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 963 | */ |
| 964 | if (!cdc_active(dev)) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 965 | result = usb_ep_enable(dev->in_ep, dev->in); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 966 | if (result != 0) { |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 967 | debug("enable %s --> %d\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 968 | dev->in_ep->name, result); |
| 969 | goto done; |
| 970 | } |
| 971 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 972 | result = usb_ep_enable(dev->out_ep, dev->out); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 973 | if (result != 0) { |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 974 | debug("enable %s --> %d\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 975 | dev->out_ep->name, result); |
| 976 | goto done; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | done: |
| 981 | if (result == 0) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 982 | result = alloc_requests(dev, qlen(gadget), gfp_flags); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 983 | |
| 984 | /* on error, disable any endpoints */ |
| 985 | if (result < 0) { |
Vitaly Kuzmichev | 750be4c | 2010-08-13 17:02:29 +0400 | [diff] [blame] | 986 | if (!subset_active(dev) && dev->status_ep) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 987 | (void) usb_ep_disable(dev->status_ep); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 988 | dev->status = NULL; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 989 | (void) usb_ep_disable(dev->in_ep); |
| 990 | (void) usb_ep_disable(dev->out_ep); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 991 | dev->in = NULL; |
| 992 | dev->out = NULL; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 993 | } else if (!cdc_active(dev)) { |
| 994 | /* |
| 995 | * activate non-CDC configs right away |
| 996 | * this isn't strictly according to the RNDIS spec |
| 997 | */ |
| 998 | eth_start(dev, GFP_ATOMIC); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* caller is responsible for cleanup on error */ |
| 1002 | return result; |
| 1003 | } |
| 1004 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1005 | static void eth_reset_config(struct eth_dev *dev) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1006 | { |
| 1007 | if (dev->config == 0) |
| 1008 | return; |
| 1009 | |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1010 | debug("%s\n", __func__); |
| 1011 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1012 | rndis_uninit(dev->rndis_config); |
| 1013 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1014 | /* |
| 1015 | * disable endpoints, forcing (synchronous) completion of |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1016 | * pending i/o. then free the requests. |
| 1017 | */ |
| 1018 | |
| 1019 | if (dev->in) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1020 | usb_ep_disable(dev->in_ep); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1021 | if (dev->tx_req) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1022 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1023 | dev->tx_req = NULL; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | if (dev->out) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1027 | usb_ep_disable(dev->out_ep); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1028 | if (dev->rx_req) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1029 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
| 1030 | dev->rx_req = NULL; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1031 | } |
| 1032 | } |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1033 | if (dev->status) |
| 1034 | usb_ep_disable(dev->status_ep); |
| 1035 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1036 | dev->rndis = 0; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1037 | dev->cdc_filter = 0; |
| 1038 | dev->config = 0; |
| 1039 | } |
| 1040 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1041 | /* |
| 1042 | * change our operational config. must agree with the code |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1043 | * that returns config descriptors, and altsetting code. |
| 1044 | */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1045 | static int eth_set_config(struct eth_dev *dev, unsigned number, |
| 1046 | gfp_t gfp_flags) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1047 | { |
| 1048 | int result = 0; |
| 1049 | struct usb_gadget *gadget = dev->gadget; |
| 1050 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1051 | if (gadget_is_sa1100(gadget) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1052 | && dev->config |
| 1053 | && dev->tx_qlen != 0) { |
| 1054 | /* tx fifo is full, but we can't clear it...*/ |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1055 | pr_err("can't change configurations"); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1056 | return -ESPIPE; |
| 1057 | } |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1058 | eth_reset_config(dev); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1059 | |
| 1060 | switch (number) { |
| 1061 | case DEV_CONFIG_VALUE: |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1062 | result = set_ether_config(dev, gfp_flags); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1063 | break; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1064 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1065 | case DEV_RNDIS_CONFIG_VALUE: |
| 1066 | dev->rndis = 1; |
| 1067 | result = set_ether_config(dev, gfp_flags); |
| 1068 | break; |
| 1069 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1070 | default: |
| 1071 | result = -EINVAL; |
| 1072 | /* FALL THROUGH */ |
| 1073 | case 0: |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | if (result) { |
| 1078 | if (number) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1079 | eth_reset_config(dev); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1080 | usb_gadget_vbus_draw(dev->gadget, |
| 1081 | gadget_is_otg(dev->gadget) ? 8 : 100); |
| 1082 | } else { |
| 1083 | char *speed; |
| 1084 | unsigned power; |
| 1085 | |
| 1086 | power = 2 * eth_config.bMaxPower; |
| 1087 | usb_gadget_vbus_draw(dev->gadget, power); |
| 1088 | |
| 1089 | switch (gadget->speed) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1090 | case USB_SPEED_FULL: |
| 1091 | speed = "full"; break; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1092 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1093 | case USB_SPEED_HIGH: |
| 1094 | speed = "high"; break; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1095 | #endif |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1096 | default: |
| 1097 | speed = "?"; break; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | dev->config = number; |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1101 | printf("%s speed config #%d: %d mA, %s, using %s\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1102 | speed, number, power, driver_desc, |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1103 | rndis_active(dev) |
| 1104 | ? "RNDIS" |
| 1105 | : (cdc_active(dev) |
| 1106 | ? "CDC Ethernet" |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1107 | : "CDC Ethernet Subset")); |
| 1108 | } |
| 1109 | return result; |
| 1110 | } |
| 1111 | |
| 1112 | /*-------------------------------------------------------------------------*/ |
| 1113 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1114 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1115 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1116 | /* |
| 1117 | * The interrupt endpoint is used in CDC networking models (Ethernet, ATM) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1118 | * only to notify the host about link status changes (which we support) or |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1119 | * report completion of some encapsulated command (as used in RNDIS). Since |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1120 | * we want this CDC Ethernet code to be vendor-neutral, we don't use that |
| 1121 | * command mechanism; and only one status request is ever queued. |
| 1122 | */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1123 | static void eth_status_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1124 | { |
| 1125 | struct usb_cdc_notification *event = req->buf; |
| 1126 | int value = req->status; |
| 1127 | struct eth_dev *dev = ep->driver_data; |
| 1128 | |
| 1129 | /* issue the second notification if host reads the first */ |
| 1130 | if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION |
| 1131 | && value == 0) { |
| 1132 | __le32 *data = req->buf + sizeof *event; |
| 1133 | |
| 1134 | event->bmRequestType = 0xA1; |
| 1135 | event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1136 | event->wValue = __constant_cpu_to_le16(0); |
| 1137 | event->wIndex = __constant_cpu_to_le16(1); |
| 1138 | event->wLength = __constant_cpu_to_le16(8); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1139 | |
| 1140 | /* SPEED_CHANGE data is up/down speeds in bits/sec */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1141 | data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget)); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1142 | |
| 1143 | req->length = STATUS_BYTECOUNT; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1144 | value = usb_ep_queue(ep, req, GFP_ATOMIC); |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1145 | debug("send SPEED_CHANGE --> %d\n", value); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1146 | if (value == 0) |
| 1147 | return; |
| 1148 | } else if (value != -ECONNRESET) { |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1149 | debug("event %02x --> %d\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1150 | event->bNotificationType, value); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1151 | if (event->bNotificationType == |
| 1152 | USB_CDC_NOTIFY_SPEED_CHANGE) { |
Mugunthan V N | 3963de3 | 2016-11-18 10:49:13 +0530 | [diff] [blame] | 1153 | dev->network_started = 1; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1154 | printf("USB network up!\n"); |
| 1155 | } |
| 1156 | } |
| 1157 | req->context = NULL; |
| 1158 | } |
| 1159 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1160 | static void issue_start_status(struct eth_dev *dev) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1161 | { |
| 1162 | struct usb_request *req = dev->stat_req; |
| 1163 | struct usb_cdc_notification *event; |
| 1164 | int value; |
| 1165 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1166 | /* |
| 1167 | * flush old status |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1168 | * |
| 1169 | * FIXME ugly idiom, maybe we'd be better with just |
| 1170 | * a "cancel the whole queue" primitive since any |
| 1171 | * unlink-one primitive has way too many error modes. |
| 1172 | * here, we "know" toggle is already clear... |
| 1173 | * |
| 1174 | * FIXME iff req->context != null just dequeue it |
| 1175 | */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1176 | usb_ep_disable(dev->status_ep); |
| 1177 | usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1178 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1179 | /* |
| 1180 | * 3.8.1 says to issue first NETWORK_CONNECTION, then |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1181 | * a SPEED_CHANGE. could be useful in some configs. |
| 1182 | */ |
| 1183 | event = req->buf; |
| 1184 | event->bmRequestType = 0xA1; |
| 1185 | event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1186 | event->wValue = __constant_cpu_to_le16(1); /* connected */ |
| 1187 | event->wIndex = __constant_cpu_to_le16(1); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1188 | event->wLength = 0; |
| 1189 | |
| 1190 | req->length = sizeof *event; |
| 1191 | req->complete = eth_status_complete; |
| 1192 | req->context = dev; |
| 1193 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1194 | value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1195 | if (value < 0) |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1196 | debug("status buf queue --> %d\n", value); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | #endif |
| 1200 | |
| 1201 | /*-------------------------------------------------------------------------*/ |
| 1202 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1203 | static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1204 | { |
| 1205 | if (req->status || req->actual != req->length) |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1206 | debug("setup complete --> %d, %d/%d\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1207 | req->status, req->actual, req->length); |
| 1208 | } |
| 1209 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1210 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1211 | |
| 1212 | static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) |
| 1213 | { |
| 1214 | if (req->status || req->actual != req->length) |
| 1215 | debug("rndis response complete --> %d, %d/%d\n", |
| 1216 | req->status, req->actual, req->length); |
| 1217 | |
| 1218 | /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */ |
| 1219 | } |
| 1220 | |
| 1221 | static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) |
| 1222 | { |
| 1223 | struct eth_dev *dev = ep->driver_data; |
| 1224 | int status; |
| 1225 | |
| 1226 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ |
| 1227 | status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf); |
| 1228 | if (status < 0) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1229 | pr_err("%s: rndis parse error %d", __func__, status); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | #endif /* RNDIS */ |
| 1233 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1234 | /* |
| 1235 | * The setup() callback implements all the ep0 functionality that's not |
| 1236 | * handled lower down. CDC has a number of less-common features: |
| 1237 | * |
| 1238 | * - two interfaces: control, and ethernet data |
| 1239 | * - Ethernet data interface has two altsettings: default, and active |
| 1240 | * - class-specific descriptors for the control interface |
| 1241 | * - class-specific control requests |
| 1242 | */ |
| 1243 | static int |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1244 | eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1245 | { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1246 | struct eth_dev *dev = get_gadget_data(gadget); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1247 | struct usb_request *req = dev->req; |
| 1248 | int value = -EOPNOTSUPP; |
| 1249 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 1250 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 1251 | u16 wLength = le16_to_cpu(ctrl->wLength); |
| 1252 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1253 | /* |
| 1254 | * descriptors just go into the pre-allocated ep0 buffer, |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1255 | * while config change events may enable network traffic. |
| 1256 | */ |
| 1257 | |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1258 | debug("%s\n", __func__); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1259 | |
| 1260 | req->complete = eth_setup_complete; |
| 1261 | switch (ctrl->bRequest) { |
| 1262 | |
| 1263 | case USB_REQ_GET_DESCRIPTOR: |
| 1264 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1265 | break; |
| 1266 | switch (wValue >> 8) { |
| 1267 | |
| 1268 | case USB_DT_DEVICE: |
Kishon Vijay Abraham I | 58d701e | 2015-02-23 18:40:17 +0530 | [diff] [blame] | 1269 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1270 | value = min(wLength, (u16) sizeof device_desc); |
| 1271 | memcpy(req->buf, &device_desc, value); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1272 | break; |
| 1273 | case USB_DT_DEVICE_QUALIFIER: |
| 1274 | if (!gadget_is_dualspeed(gadget)) |
| 1275 | break; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1276 | value = min(wLength, (u16) sizeof dev_qualifier); |
| 1277 | memcpy(req->buf, &dev_qualifier, value); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1278 | break; |
| 1279 | |
| 1280 | case USB_DT_OTHER_SPEED_CONFIG: |
| 1281 | if (!gadget_is_dualspeed(gadget)) |
| 1282 | break; |
| 1283 | /* FALLTHROUGH */ |
| 1284 | case USB_DT_CONFIG: |
| 1285 | value = config_buf(gadget, req->buf, |
| 1286 | wValue >> 8, |
| 1287 | wValue & 0xff, |
| 1288 | gadget_is_otg(gadget)); |
| 1289 | if (value >= 0) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1290 | value = min(wLength, (u16) value); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1291 | break; |
| 1292 | |
| 1293 | case USB_DT_STRING: |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1294 | value = usb_gadget_get_string(&stringtab, |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1295 | wValue & 0xff, req->buf); |
| 1296 | |
| 1297 | if (value >= 0) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1298 | value = min(wLength, (u16) value); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1299 | |
| 1300 | break; |
| 1301 | } |
| 1302 | break; |
| 1303 | |
| 1304 | case USB_REQ_SET_CONFIGURATION: |
| 1305 | if (ctrl->bRequestType != 0) |
| 1306 | break; |
| 1307 | if (gadget->a_hnp_support) |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1308 | debug("HNP available\n"); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1309 | else if (gadget->a_alt_hnp_support) |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1310 | debug("HNP needs a different root port\n"); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1311 | value = eth_set_config(dev, wValue, GFP_ATOMIC); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1312 | break; |
| 1313 | case USB_REQ_GET_CONFIGURATION: |
| 1314 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1315 | break; |
| 1316 | *(u8 *)req->buf = dev->config; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1317 | value = min(wLength, (u16) 1); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1318 | break; |
| 1319 | |
| 1320 | case USB_REQ_SET_INTERFACE: |
| 1321 | if (ctrl->bRequestType != USB_RECIP_INTERFACE |
| 1322 | || !dev->config |
| 1323 | || wIndex > 1) |
| 1324 | break; |
| 1325 | if (!cdc_active(dev) && wIndex != 0) |
| 1326 | break; |
| 1327 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1328 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1329 | switch (wIndex) { |
| 1330 | case 0: /* control/master intf */ |
| 1331 | if (wValue != 0) |
| 1332 | break; |
| 1333 | if (dev->status) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1334 | usb_ep_disable(dev->status_ep); |
| 1335 | usb_ep_enable(dev->status_ep, dev->status); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1336 | } |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1337 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1338 | value = 0; |
| 1339 | break; |
| 1340 | case 1: /* data intf */ |
| 1341 | if (wValue > 1) |
| 1342 | break; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1343 | usb_ep_disable(dev->in_ep); |
| 1344 | usb_ep_disable(dev->out_ep); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1345 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1346 | /* |
| 1347 | * CDC requires the data transfers not be done from |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1348 | * the default interface setting ... also, setting |
| 1349 | * the non-default interface resets filters etc. |
| 1350 | */ |
| 1351 | if (wValue == 1) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1352 | if (!cdc_active(dev)) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1353 | break; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1354 | usb_ep_enable(dev->in_ep, dev->in); |
| 1355 | usb_ep_enable(dev->out_ep, dev->out); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1356 | dev->cdc_filter = DEFAULT_FILTER; |
| 1357 | if (dev->status) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1358 | issue_start_status(dev); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1359 | eth_start(dev, GFP_ATOMIC); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1360 | } |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1361 | value = 0; |
| 1362 | break; |
| 1363 | } |
| 1364 | #else |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1365 | /* |
| 1366 | * FIXME this is wrong, as is the assumption that |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1367 | * all non-PXA hardware talks real CDC ... |
| 1368 | */ |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1369 | debug("set_interface ignored!\n"); |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1370 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1371 | break; |
| 1372 | case USB_REQ_GET_INTERFACE: |
| 1373 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) |
| 1374 | || !dev->config |
| 1375 | || wIndex > 1) |
| 1376 | break; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1377 | if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1378 | break; |
| 1379 | |
| 1380 | /* for CDC, iff carrier is on, data interface is active. */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1381 | if (rndis_active(dev) || wIndex != 1) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1382 | *(u8 *)req->buf = 0; |
| 1383 | else { |
| 1384 | /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */ |
| 1385 | /* carrier always ok ...*/ |
| 1386 | *(u8 *)req->buf = 1 ; |
| 1387 | } |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1388 | value = min(wLength, (u16) 1); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1389 | break; |
| 1390 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1391 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1392 | case USB_CDC_SET_ETHERNET_PACKET_FILTER: |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1393 | /* |
| 1394 | * see 6.2.30: no data, wIndex = interface, |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1395 | * wValue = packet filter bitmap |
| 1396 | */ |
| 1397 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) |
| 1398 | || !cdc_active(dev) |
| 1399 | || wLength != 0 |
| 1400 | || wIndex > 1) |
| 1401 | break; |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1402 | debug("packet filter %02x\n", wValue); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1403 | dev->cdc_filter = wValue; |
| 1404 | value = 0; |
| 1405 | break; |
| 1406 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1407 | /* |
| 1408 | * and potentially: |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1409 | * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: |
| 1410 | * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: |
| 1411 | * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: |
| 1412 | * case USB_CDC_GET_ETHERNET_STATISTIC: |
| 1413 | */ |
| 1414 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 1415 | #endif /* CONFIG_USB_ETH_CDC */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1416 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1417 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1418 | /* |
| 1419 | * RNDIS uses the CDC command encapsulation mechanism to implement |
| 1420 | * an RPC scheme, with much getting/setting of attributes by OID. |
| 1421 | */ |
| 1422 | case USB_CDC_SEND_ENCAPSULATED_COMMAND: |
| 1423 | if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE) |
| 1424 | || !rndis_active(dev) |
| 1425 | || wLength > USB_BUFSIZ |
| 1426 | || wValue |
| 1427 | || rndis_control_intf.bInterfaceNumber |
| 1428 | != wIndex) |
| 1429 | break; |
| 1430 | /* read the request, then process it */ |
| 1431 | value = wLength; |
| 1432 | req->complete = rndis_command_complete; |
| 1433 | /* later, rndis_control_ack () sends a notification */ |
| 1434 | break; |
| 1435 | |
| 1436 | case USB_CDC_GET_ENCAPSULATED_RESPONSE: |
| 1437 | if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE) |
| 1438 | == ctrl->bRequestType |
| 1439 | && rndis_active(dev) |
| 1440 | /* && wLength >= 0x0400 */ |
| 1441 | && !wValue |
| 1442 | && rndis_control_intf.bInterfaceNumber |
| 1443 | == wIndex) { |
| 1444 | u8 *buf; |
| 1445 | u32 n; |
| 1446 | |
| 1447 | /* return the result */ |
| 1448 | buf = rndis_get_next_response(dev->rndis_config, &n); |
| 1449 | if (buf) { |
| 1450 | memcpy(req->buf, buf, n); |
| 1451 | req->complete = rndis_response_complete; |
| 1452 | rndis_free_response(dev->rndis_config, buf); |
| 1453 | value = n; |
| 1454 | } |
| 1455 | /* else stalls ... spec says to avoid that */ |
| 1456 | } |
| 1457 | break; |
| 1458 | #endif /* RNDIS */ |
| 1459 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1460 | default: |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1461 | debug("unknown control req%02x.%02x v%04x i%04x l%d\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1462 | ctrl->bRequestType, ctrl->bRequest, |
| 1463 | wValue, wIndex, wLength); |
| 1464 | } |
| 1465 | |
| 1466 | /* respond with data transfer before status phase? */ |
| 1467 | if (value >= 0) { |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1468 | debug("respond with data transfer before status phase\n"); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1469 | req->length = value; |
| 1470 | req->zero = value < wLength |
| 1471 | && (value % gadget->ep0->maxpacket) == 0; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1472 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1473 | if (value < 0) { |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1474 | debug("ep_queue --> %d\n", value); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1475 | req->status = 0; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1476 | eth_setup_complete(gadget->ep0, req); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | /* host either stalls (value < 0) or reports success */ |
| 1481 | return value; |
| 1482 | } |
| 1483 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1484 | /*-------------------------------------------------------------------------*/ |
| 1485 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1486 | static void rx_complete(struct usb_ep *ep, struct usb_request *req); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1487 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1488 | static int rx_submit(struct eth_dev *dev, struct usb_request *req, |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1489 | gfp_t gfp_flags) |
| 1490 | { |
| 1491 | int retval = -ENOMEM; |
| 1492 | size_t size; |
| 1493 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1494 | /* |
| 1495 | * Padding up to RX_EXTRA handles minor disagreements with host. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1496 | * Normally we use the USB "terminate on short read" convention; |
| 1497 | * so allow up to (N*maxpacket), since that memory is normally |
| 1498 | * already allocated. Some hardware doesn't deal well with short |
| 1499 | * reads (e.g. DMA must be N*maxpacket), so for now don't trim a |
| 1500 | * byte off the end (to force hardware errors on overflow). |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1501 | * |
| 1502 | * RNDIS uses internal framing, and explicitly allows senders to |
| 1503 | * pad to end-of-packet. That's potentially nice for speed, |
| 1504 | * but means receivers can't recover synch on their own. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1505 | */ |
| 1506 | |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1507 | debug("%s\n", __func__); |
Troy Kisky | 1d01de1 | 2013-09-25 18:41:05 -0700 | [diff] [blame] | 1508 | if (!req) |
| 1509 | return -EINVAL; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1510 | |
| 1511 | size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA); |
| 1512 | size += dev->out_ep->maxpacket - 1; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1513 | if (rndis_active(dev)) |
| 1514 | size += sizeof(struct rndis_packet_msg_type); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1515 | size -= size % dev->out_ep->maxpacket; |
| 1516 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1517 | /* |
| 1518 | * Some platforms perform better when IP packets are aligned, |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1519 | * but on at least one, checksumming fails otherwise. Note: |
| 1520 | * RNDIS headers involve variable numbers of LE32 values. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1521 | */ |
| 1522 | |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 1523 | req->buf = (u8 *)net_rx_packets[0]; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1524 | req->length = size; |
| 1525 | req->complete = rx_complete; |
| 1526 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1527 | retval = usb_ep_queue(dev->out_ep, req, gfp_flags); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1528 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1529 | if (retval) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1530 | pr_err("rx submit --> %d", retval); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1531 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1532 | return retval; |
| 1533 | } |
| 1534 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1535 | static void rx_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1536 | { |
| 1537 | struct eth_dev *dev = ep->driver_data; |
| 1538 | |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1539 | debug("%s: status %d\n", __func__, req->status); |
Vitaly Kuzmichev | 24fcb47 | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1540 | switch (req->status) { |
| 1541 | /* normal completion */ |
| 1542 | case 0: |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1543 | if (rndis_active(dev)) { |
| 1544 | /* we know MaxPacketsPerTransfer == 1 here */ |
| 1545 | int length = rndis_rm_hdr(req->buf, req->actual); |
| 1546 | if (length < 0) |
| 1547 | goto length_err; |
| 1548 | req->length -= length; |
| 1549 | req->actual -= length; |
| 1550 | } |
Bin Meng | 2ccb8b0 | 2018-07-31 02:55:22 -0700 | [diff] [blame] | 1551 | if (req->actual < ETH_HLEN || PKTSIZE_ALIGN < req->actual) { |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1552 | length_err: |
| 1553 | dev->stats.rx_errors++; |
| 1554 | dev->stats.rx_length_errors++; |
| 1555 | debug("rx length %d\n", req->length); |
| 1556 | break; |
| 1557 | } |
| 1558 | |
Vitaly Kuzmichev | 24fcb47 | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1559 | dev->stats.rx_packets++; |
| 1560 | dev->stats.rx_bytes += req->length; |
| 1561 | break; |
| 1562 | |
| 1563 | /* software-driven interface shutdown */ |
| 1564 | case -ECONNRESET: /* unlink */ |
| 1565 | case -ESHUTDOWN: /* disconnect etc */ |
| 1566 | /* for hardware automagic (such as pxa) */ |
| 1567 | case -ECONNABORTED: /* endpoint reset */ |
| 1568 | break; |
| 1569 | |
| 1570 | /* data overrun */ |
| 1571 | case -EOVERFLOW: |
| 1572 | dev->stats.rx_over_errors++; |
| 1573 | /* FALLTHROUGH */ |
| 1574 | default: |
| 1575 | dev->stats.rx_errors++; |
| 1576 | break; |
| 1577 | } |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1578 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1579 | packet_received = 1; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1580 | } |
| 1581 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1582 | static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1583 | { |
| 1584 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1585 | dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1586 | |
| 1587 | if (!dev->tx_req) |
Vitaly Kuzmichev | 2907b2c | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1588 | goto fail1; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1589 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1590 | dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1591 | |
| 1592 | if (!dev->rx_req) |
Vitaly Kuzmichev | 2907b2c | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1593 | goto fail2; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1594 | |
| 1595 | return 0; |
| 1596 | |
Vitaly Kuzmichev | 2907b2c | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 1597 | fail2: |
| 1598 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1599 | fail1: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1600 | pr_err("can't alloc requests"); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1601 | return -1; |
| 1602 | } |
| 1603 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1604 | static void tx_complete(struct usb_ep *ep, struct usb_request *req) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1605 | { |
Vitaly Kuzmichev | 24fcb47 | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1606 | struct eth_dev *dev = ep->driver_data; |
| 1607 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1608 | debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok"); |
Vitaly Kuzmichev | 24fcb47 | 2011-02-11 18:18:32 +0300 | [diff] [blame] | 1609 | switch (req->status) { |
| 1610 | default: |
| 1611 | dev->stats.tx_errors++; |
| 1612 | debug("tx err %d\n", req->status); |
| 1613 | /* FALLTHROUGH */ |
| 1614 | case -ECONNRESET: /* unlink */ |
| 1615 | case -ESHUTDOWN: /* disconnect etc */ |
| 1616 | break; |
| 1617 | case 0: |
| 1618 | dev->stats.tx_bytes += req->length; |
| 1619 | } |
| 1620 | dev->stats.tx_packets++; |
| 1621 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1622 | packet_sent = 1; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1623 | } |
| 1624 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1625 | static inline int eth_is_promisc(struct eth_dev *dev) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1626 | { |
| 1627 | /* no filters for the CDC subset; always promisc */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1628 | if (subset_active(dev)) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1629 | return 1; |
| 1630 | return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS; |
| 1631 | } |
| 1632 | |
| 1633 | #if 0 |
| 1634 | static int eth_start_xmit (struct sk_buff *skb, struct net_device *net) |
| 1635 | { |
| 1636 | struct eth_dev *dev = netdev_priv(net); |
| 1637 | int length = skb->len; |
| 1638 | int retval; |
| 1639 | struct usb_request *req = NULL; |
| 1640 | unsigned long flags; |
| 1641 | |
| 1642 | /* apply outgoing CDC or RNDIS filters */ |
| 1643 | if (!eth_is_promisc (dev)) { |
| 1644 | u8 *dest = skb->data; |
| 1645 | |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1646 | if (is_multicast_ethaddr(dest)) { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1647 | u16 type; |
| 1648 | |
| 1649 | /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host |
| 1650 | * SET_ETHERNET_MULTICAST_FILTERS requests |
| 1651 | */ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1652 | if (is_broadcast_ethaddr(dest)) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1653 | type = USB_CDC_PACKET_TYPE_BROADCAST; |
| 1654 | else |
| 1655 | type = USB_CDC_PACKET_TYPE_ALL_MULTICAST; |
| 1656 | if (!(dev->cdc_filter & type)) { |
| 1657 | dev_kfree_skb_any (skb); |
| 1658 | return 0; |
| 1659 | } |
| 1660 | } |
| 1661 | /* ignores USB_CDC_PACKET_TYPE_DIRECTED */ |
| 1662 | } |
| 1663 | |
| 1664 | spin_lock_irqsave(&dev->req_lock, flags); |
| 1665 | /* |
| 1666 | * this freelist can be empty if an interrupt triggered disconnect() |
| 1667 | * and reconfigured the gadget (shutting down this queue) after the |
| 1668 | * network stack decided to xmit but before we got the spinlock. |
| 1669 | */ |
| 1670 | if (list_empty(&dev->tx_reqs)) { |
| 1671 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1672 | return 1; |
| 1673 | } |
| 1674 | |
| 1675 | req = container_of (dev->tx_reqs.next, struct usb_request, list); |
| 1676 | list_del (&req->list); |
| 1677 | |
| 1678 | /* temporarily stop TX queue when the freelist empties */ |
| 1679 | if (list_empty (&dev->tx_reqs)) |
| 1680 | netif_stop_queue (net); |
| 1681 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1682 | |
| 1683 | /* no buffer copies needed, unless the network stack did it |
| 1684 | * or the hardware can't use skb buffers. |
| 1685 | * or there's not enough space for any RNDIS headers we need |
| 1686 | */ |
| 1687 | if (rndis_active(dev)) { |
| 1688 | struct sk_buff *skb_rndis; |
| 1689 | |
| 1690 | skb_rndis = skb_realloc_headroom (skb, |
| 1691 | sizeof (struct rndis_packet_msg_type)); |
| 1692 | if (!skb_rndis) |
| 1693 | goto drop; |
| 1694 | |
| 1695 | dev_kfree_skb_any (skb); |
| 1696 | skb = skb_rndis; |
| 1697 | rndis_add_hdr (skb); |
| 1698 | length = skb->len; |
| 1699 | } |
| 1700 | req->buf = skb->data; |
| 1701 | req->context = skb; |
| 1702 | req->complete = tx_complete; |
| 1703 | |
| 1704 | /* use zlp framing on tx for strict CDC-Ether conformance, |
| 1705 | * though any robust network rx path ignores extra padding. |
| 1706 | * and some hardware doesn't like to write zlps. |
| 1707 | */ |
| 1708 | req->zero = 1; |
| 1709 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) |
| 1710 | length++; |
| 1711 | |
| 1712 | req->length = length; |
| 1713 | |
| 1714 | /* throttle highspeed IRQ rate back slightly */ |
| 1715 | if (gadget_is_dualspeed(dev->gadget)) |
| 1716 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) |
| 1717 | ? ((atomic_read(&dev->tx_qlen) % qmult) != 0) |
| 1718 | : 0; |
| 1719 | |
| 1720 | retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC); |
| 1721 | switch (retval) { |
| 1722 | default: |
| 1723 | DEBUG (dev, "tx queue err %d\n", retval); |
| 1724 | break; |
| 1725 | case 0: |
| 1726 | net->trans_start = jiffies; |
| 1727 | atomic_inc (&dev->tx_qlen); |
| 1728 | } |
| 1729 | |
| 1730 | if (retval) { |
| 1731 | drop: |
| 1732 | dev->stats.tx_dropped++; |
| 1733 | dev_kfree_skb_any (skb); |
| 1734 | spin_lock_irqsave(&dev->req_lock, flags); |
| 1735 | if (list_empty (&dev->tx_reqs)) |
| 1736 | netif_start_queue (net); |
| 1737 | list_add (&req->list, &dev->tx_reqs); |
| 1738 | spin_unlock_irqrestore(&dev->req_lock, flags); |
| 1739 | } |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | /*-------------------------------------------------------------------------*/ |
| 1744 | #endif |
| 1745 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1746 | static void eth_unbind(struct usb_gadget *gadget) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1747 | { |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1748 | struct eth_dev *dev = get_gadget_data(gadget); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1749 | |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 1750 | debug("%s...\n", __func__); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1751 | rndis_deregister(dev->rndis_config); |
| 1752 | rndis_exit(); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1753 | |
Vitaly Kuzmichev | 9067070 | 2010-08-13 17:00:16 +0400 | [diff] [blame] | 1754 | /* we've already been disconnected ... no i/o is active */ |
| 1755 | if (dev->req) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1756 | usb_ep_free_request(gadget->ep0, dev->req); |
Vitaly Kuzmichev | 9067070 | 2010-08-13 17:00:16 +0400 | [diff] [blame] | 1757 | dev->req = NULL; |
| 1758 | } |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1759 | if (dev->stat_req) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1760 | usb_ep_free_request(dev->status_ep, dev->stat_req); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1761 | dev->stat_req = NULL; |
| 1762 | } |
| 1763 | |
| 1764 | if (dev->tx_req) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1765 | usb_ep_free_request(dev->in_ep, dev->tx_req); |
| 1766 | dev->tx_req = NULL; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | if (dev->rx_req) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1770 | usb_ep_free_request(dev->out_ep, dev->rx_req); |
| 1771 | dev->rx_req = NULL; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | /* unregister_netdev (dev->net);*/ |
| 1775 | /* free_netdev(dev->net);*/ |
| 1776 | |
Lei Wen | 4af3273 | 2010-12-01 23:43:43 +0800 | [diff] [blame] | 1777 | dev->gadget = NULL; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1778 | set_gadget_data(gadget, NULL); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1781 | static void eth_disconnect(struct usb_gadget *gadget) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1782 | { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1783 | eth_reset_config(get_gadget_data(gadget)); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1784 | /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1785 | } |
| 1786 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1787 | static void eth_suspend(struct usb_gadget *gadget) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1788 | { |
| 1789 | /* Not used */ |
| 1790 | } |
| 1791 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1792 | static void eth_resume(struct usb_gadget *gadget) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1793 | { |
| 1794 | /* Not used */ |
| 1795 | } |
| 1796 | |
| 1797 | /*-------------------------------------------------------------------------*/ |
| 1798 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1799 | #ifdef CONFIG_USB_ETH_RNDIS |
| 1800 | |
| 1801 | /* |
| 1802 | * The interrupt endpoint is used in RNDIS to notify the host when messages |
| 1803 | * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT |
| 1804 | * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even |
| 1805 | * REMOTE_NDIS_KEEPALIVE_MSG. |
| 1806 | * |
| 1807 | * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and |
| 1808 | * normally just one notification will be queued. |
| 1809 | */ |
| 1810 | |
| 1811 | static void rndis_control_ack_complete(struct usb_ep *ep, |
| 1812 | struct usb_request *req) |
| 1813 | { |
| 1814 | struct eth_dev *dev = ep->driver_data; |
| 1815 | |
| 1816 | debug("%s...\n", __func__); |
| 1817 | if (req->status || req->actual != req->length) |
| 1818 | debug("rndis control ack complete --> %d, %d/%d\n", |
| 1819 | req->status, req->actual, req->length); |
| 1820 | |
Mugunthan V N | 3963de3 | 2016-11-18 10:49:13 +0530 | [diff] [blame] | 1821 | if (!dev->network_started) { |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1822 | if (rndis_get_state(dev->rndis_config) |
| 1823 | == RNDIS_DATA_INITIALIZED) { |
Mugunthan V N | 3963de3 | 2016-11-18 10:49:13 +0530 | [diff] [blame] | 1824 | dev->network_started = 1; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1825 | printf("USB RNDIS network up!\n"); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | req->context = NULL; |
| 1830 | |
| 1831 | if (req != dev->stat_req) |
| 1832 | usb_ep_free_request(ep, req); |
| 1833 | } |
| 1834 | |
| 1835 | static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32)))); |
| 1836 | |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 1837 | #ifndef CONFIG_DM_ETH |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1838 | static int rndis_control_ack(struct eth_device *net) |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 1839 | #else |
| 1840 | static int rndis_control_ack(struct udevice *net) |
| 1841 | #endif |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1842 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 1843 | struct ether_priv *priv; |
| 1844 | struct eth_dev *dev; |
| 1845 | int length; |
| 1846 | struct usb_request *resp; |
| 1847 | |
| 1848 | #ifndef CONFIG_DM_ETH |
| 1849 | priv = (struct ether_priv *)net->priv; |
| 1850 | #else |
| 1851 | priv = dev_get_priv(net); |
| 1852 | #endif |
| 1853 | dev = &priv->ethdev; |
| 1854 | resp = dev->stat_req; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1855 | |
| 1856 | /* in case RNDIS calls this after disconnect */ |
| 1857 | if (!dev->status) { |
| 1858 | debug("status ENODEV\n"); |
| 1859 | return -ENODEV; |
| 1860 | } |
| 1861 | |
| 1862 | /* in case queue length > 1 */ |
| 1863 | if (resp->context) { |
| 1864 | resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC); |
| 1865 | if (!resp) |
| 1866 | return -ENOMEM; |
| 1867 | resp->buf = rndis_resp_buf; |
| 1868 | } |
| 1869 | |
| 1870 | /* |
| 1871 | * Send RNDIS RESPONSE_AVAILABLE notification; |
| 1872 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too |
| 1873 | */ |
| 1874 | resp->length = 8; |
| 1875 | resp->complete = rndis_control_ack_complete; |
| 1876 | resp->context = dev; |
| 1877 | |
| 1878 | *((__le32 *) resp->buf) = __constant_cpu_to_le32(1); |
| 1879 | *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0); |
| 1880 | |
| 1881 | length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC); |
| 1882 | if (length < 0) { |
| 1883 | resp->status = 0; |
| 1884 | rndis_control_ack_complete(dev->status_ep, resp); |
| 1885 | } |
| 1886 | |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
| 1890 | #else |
| 1891 | |
| 1892 | #define rndis_control_ack NULL |
| 1893 | |
| 1894 | #endif /* RNDIS */ |
| 1895 | |
| 1896 | static void eth_start(struct eth_dev *dev, gfp_t gfp_flags) |
| 1897 | { |
| 1898 | if (rndis_active(dev)) { |
| 1899 | rndis_set_param_medium(dev->rndis_config, |
| 1900 | NDIS_MEDIUM_802_3, |
| 1901 | BITRATE(dev->gadget)/100); |
| 1902 | rndis_signal_connect(dev->rndis_config); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | static int eth_stop(struct eth_dev *dev) |
| 1907 | { |
Vitaly Kuzmichev | 3433c33 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 1908 | #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT |
| 1909 | unsigned long ts; |
| 1910 | unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */ |
| 1911 | #endif |
| 1912 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1913 | if (rndis_active(dev)) { |
| 1914 | rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0); |
| 1915 | rndis_signal_disconnect(dev->rndis_config); |
| 1916 | |
Vitaly Kuzmichev | 3433c33 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 1917 | #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT |
| 1918 | /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */ |
| 1919 | ts = get_timer(0); |
| 1920 | while (get_timer(ts) < timeout) |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 1921 | usb_gadget_handle_interrupts(0); |
Vitaly Kuzmichev | 3433c33 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 1922 | #endif |
| 1923 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1924 | rndis_uninit(dev->rndis_config); |
| 1925 | dev->rndis = 0; |
| 1926 | } |
| 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | /*-------------------------------------------------------------------------*/ |
| 1932 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1933 | static int is_eth_addr_valid(char *str) |
| 1934 | { |
| 1935 | if (strlen(str) == 17) { |
| 1936 | int i; |
| 1937 | char *p, *q; |
| 1938 | uchar ea[6]; |
| 1939 | |
| 1940 | /* see if it looks like an ethernet address */ |
| 1941 | |
| 1942 | p = str; |
| 1943 | |
| 1944 | for (i = 0; i < 6; i++) { |
| 1945 | char term = (i == 5 ? '\0' : ':'); |
| 1946 | |
| 1947 | ea[i] = simple_strtol(p, &q, 16); |
| 1948 | |
| 1949 | if ((q - p) != 2 || *q++ != term) |
| 1950 | break; |
| 1951 | |
| 1952 | p = q; |
| 1953 | } |
| 1954 | |
Tom Rini | add21ca | 2012-10-31 13:30:41 +0000 | [diff] [blame] | 1955 | /* Now check the contents. */ |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1956 | return is_valid_ethaddr(ea); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1957 | } |
| 1958 | return 0; |
| 1959 | } |
| 1960 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1961 | static u8 nibble(unsigned char c) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1962 | { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1963 | if (likely(isdigit(c))) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1964 | return c - '0'; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1965 | c = toupper(c); |
| 1966 | if (likely(isxdigit(c))) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1967 | return 10 + c - 'A'; |
| 1968 | return 0; |
| 1969 | } |
| 1970 | |
| 1971 | static int get_ether_addr(const char *str, u8 *dev_addr) |
| 1972 | { |
| 1973 | if (str) { |
| 1974 | unsigned i; |
| 1975 | |
| 1976 | for (i = 0; i < 6; i++) { |
| 1977 | unsigned char num; |
| 1978 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1979 | if ((*str == '.') || (*str == ':')) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1980 | str++; |
| 1981 | num = nibble(*str++) << 4; |
| 1982 | num |= (nibble(*str++)); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1983 | dev_addr[i] = num; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1984 | } |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 1985 | if (is_valid_ethaddr(dev_addr)) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1986 | return 0; |
| 1987 | } |
| 1988 | return 1; |
| 1989 | } |
| 1990 | |
| 1991 | static int eth_bind(struct usb_gadget *gadget) |
| 1992 | { |
Mugunthan V N | 88e48be | 2016-11-18 11:06:13 +0530 | [diff] [blame] | 1993 | struct eth_dev *dev = &l_priv->ethdev; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1994 | u8 cdc = 1, zlp = 1, rndis = 1; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1995 | struct usb_ep *in_ep, *out_ep, *status_ep = NULL; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 1996 | int status = -ENOMEM; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 1997 | int gcnum; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 1998 | u8 tmp[7]; |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 1999 | #ifdef CONFIG_DM_ETH |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 2000 | struct eth_pdata *pdata = dev_get_plat(l_priv->netdev); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2001 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2002 | |
| 2003 | /* these flags are only ever cleared; compiler take note */ |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2004 | #ifndef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2005 | cdc = 0; |
| 2006 | #endif |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2007 | #ifndef CONFIG_USB_ETH_RNDIS |
| 2008 | rndis = 0; |
| 2009 | #endif |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2010 | /* |
| 2011 | * Because most host side USB stacks handle CDC Ethernet, that |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2012 | * standard protocol is _strongly_ preferred for interop purposes. |
| 2013 | * (By everyone except Microsoft.) |
| 2014 | */ |
Tom Rini | 1e56de0 | 2022-06-25 11:02:42 -0400 | [diff] [blame^] | 2015 | if (gadget_is_musbhdrc(gadget)) { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2016 | /* reduce tx dma overhead by avoiding special cases */ |
| 2017 | zlp = 0; |
| 2018 | } else if (gadget_is_sh(gadget)) { |
| 2019 | /* sh doesn't support multiple interfaces or configs */ |
| 2020 | cdc = 0; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2021 | rndis = 0; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2022 | } else if (gadget_is_sa1100(gadget)) { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2023 | /* hardware can't write zlps */ |
| 2024 | zlp = 0; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2025 | /* |
| 2026 | * sa1100 CAN do CDC, without status endpoint ... we use |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2027 | * non-CDC to be compatible with ARM Linux-2.4 "usb-eth". |
| 2028 | */ |
| 2029 | cdc = 0; |
| 2030 | } |
| 2031 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2032 | gcnum = usb_gadget_controller_number(gadget); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2033 | if (gcnum >= 0) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2034 | device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2035 | else { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2036 | /* |
| 2037 | * can't assume CDC works. don't want to default to |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2038 | * anything less functional on CDC-capable hardware, |
| 2039 | * so we fail in this case. |
| 2040 | */ |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2041 | pr_err("controller '%s' not recognized", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2042 | gadget->name); |
| 2043 | return -ENODEV; |
| 2044 | } |
| 2045 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2046 | /* |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2047 | * If there's an RNDIS configuration, that's what Windows wants to |
| 2048 | * be using ... so use these product IDs here and in the "linux.inf" |
| 2049 | * needed to install MSFT drivers. Current Linux kernels will use |
| 2050 | * the second configuration if it's CDC Ethernet, and need some help |
| 2051 | * to choose the right configuration otherwise. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2052 | */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2053 | if (rndis) { |
Maxime Ripard | 48e78f8 | 2017-09-07 09:15:08 +0200 | [diff] [blame] | 2054 | #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2055 | device_desc.idVendor = |
Maxime Ripard | 48e78f8 | 2017-09-07 09:15:08 +0200 | [diff] [blame] | 2056 | __constant_cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2057 | device_desc.idProduct = |
Maxime Ripard | 48e78f8 | 2017-09-07 09:15:08 +0200 | [diff] [blame] | 2058 | __constant_cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2059 | #else |
| 2060 | device_desc.idVendor = |
| 2061 | __constant_cpu_to_le16(RNDIS_VENDOR_NUM); |
| 2062 | device_desc.idProduct = |
| 2063 | __constant_cpu_to_le16(RNDIS_PRODUCT_NUM); |
| 2064 | #endif |
| 2065 | sprintf(product_desc, "RNDIS/%s", driver_desc); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2066 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2067 | /* |
| 2068 | * CDC subset ... recognized by Linux since 2.4.10, but Windows |
| 2069 | * drivers aren't widely available. (That may be improved by |
| 2070 | * supporting one submode of the "SAFE" variant of MDLM.) |
| 2071 | */ |
| 2072 | } else { |
Maxime Ripard | 48e78f8 | 2017-09-07 09:15:08 +0200 | [diff] [blame] | 2073 | #if defined(CONFIG_USB_GADGET_VENDOR_NUM) && defined(CONFIG_USB_GADGET_PRODUCT_NUM) |
| 2074 | device_desc.idVendor = cpu_to_le16(CONFIG_USB_GADGET_VENDOR_NUM); |
| 2075 | device_desc.idProduct = cpu_to_le16(CONFIG_USB_GADGET_PRODUCT_NUM); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2076 | #else |
| 2077 | if (!cdc) { |
| 2078 | device_desc.idVendor = |
| 2079 | __constant_cpu_to_le16(SIMPLE_VENDOR_NUM); |
| 2080 | device_desc.idProduct = |
| 2081 | __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM); |
| 2082 | } |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2083 | #endif |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2084 | } |
| 2085 | /* support optional vendor/distro customization */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2086 | if (bcdDevice) |
| 2087 | device_desc.bcdDevice = cpu_to_le16(bcdDevice); |
| 2088 | if (iManufacturer) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2089 | strlcpy(manufacturer, iManufacturer, sizeof manufacturer); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2090 | if (iProduct) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2091 | strlcpy(product_desc, iProduct, sizeof product_desc); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2092 | if (iSerialNumber) { |
| 2093 | device_desc.iSerialNumber = STRING_SERIALNUMBER, |
Vitaly Kuzmichev | 214d7b0 | 2010-08-13 17:00:45 +0400 | [diff] [blame] | 2094 | strlcpy(serial_number, iSerialNumber, sizeof serial_number); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /* all we really need is bulk IN/OUT */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2098 | usb_ep_autoconfig_reset(gadget); |
| 2099 | in_ep = usb_ep_autoconfig(gadget, &fs_source_desc); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2100 | if (!in_ep) { |
| 2101 | autoconf_fail: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2102 | pr_err("can't autoconfigure on %s\n", |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2103 | gadget->name); |
| 2104 | return -ENODEV; |
| 2105 | } |
| 2106 | in_ep->driver_data = in_ep; /* claim */ |
| 2107 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2108 | out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2109 | if (!out_ep) |
| 2110 | goto autoconf_fail; |
| 2111 | out_ep->driver_data = out_ep; /* claim */ |
| 2112 | |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2113 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2114 | /* |
| 2115 | * CDC Ethernet control interface doesn't require a status endpoint. |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2116 | * Since some hosts expect one, try to allocate one anyway. |
| 2117 | */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2118 | if (cdc || rndis) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2119 | status_ep = usb_ep_autoconfig(gadget, &fs_status_desc); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2120 | if (status_ep) { |
| 2121 | status_ep->driver_data = status_ep; /* claim */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2122 | } else if (rndis) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2123 | pr_err("can't run RNDIS on %s", gadget->name); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2124 | return -ENODEV; |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2125 | #ifdef CONFIG_USB_ETH_CDC |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2126 | } else if (cdc) { |
| 2127 | control_intf.bNumEndpoints = 0; |
| 2128 | /* FIXME remove endpoint from descriptor list */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2129 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | #endif |
| 2133 | |
| 2134 | /* one config: cdc, else minimal subset */ |
| 2135 | if (!cdc) { |
| 2136 | eth_config.bNumInterfaces = 1; |
| 2137 | eth_config.iConfiguration = STRING_SUBSET; |
| 2138 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2139 | /* |
| 2140 | * use functions to set these up, in case we're built to work |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2141 | * with multiple controllers and must override CDC Ethernet. |
| 2142 | */ |
| 2143 | fs_subset_descriptors(); |
| 2144 | hs_subset_descriptors(); |
| 2145 | } |
| 2146 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2147 | usb_gadget_set_selfpowered(gadget); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2148 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2149 | /* For now RNDIS is always a second config */ |
| 2150 | if (rndis) |
| 2151 | device_desc.bNumConfigurations = 2; |
| 2152 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2153 | if (gadget_is_dualspeed(gadget)) { |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2154 | if (rndis) |
| 2155 | dev_qualifier.bNumConfigurations = 2; |
| 2156 | else if (!cdc) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2157 | dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
| 2158 | |
| 2159 | /* assumes ep0 uses the same value for both speeds ... */ |
| 2160 | dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0; |
| 2161 | |
| 2162 | /* and that all endpoints are dual-speed */ |
| 2163 | hs_source_desc.bEndpointAddress = |
| 2164 | fs_source_desc.bEndpointAddress; |
| 2165 | hs_sink_desc.bEndpointAddress = |
| 2166 | fs_sink_desc.bEndpointAddress; |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2167 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2168 | if (status_ep) |
| 2169 | hs_status_desc.bEndpointAddress = |
| 2170 | fs_status_desc.bEndpointAddress; |
| 2171 | #endif |
| 2172 | } |
| 2173 | |
| 2174 | if (gadget_is_otg(gadget)) { |
| 2175 | otg_descriptor.bmAttributes |= USB_OTG_HNP, |
| 2176 | eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 2177 | eth_config.bMaxPower = 4; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2178 | #ifdef CONFIG_USB_ETH_RNDIS |
| 2179 | rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 2180 | rndis_config.bMaxPower = 4; |
| 2181 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2182 | } |
| 2183 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2184 | |
| 2185 | /* network device setup */ |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2186 | #ifndef CONFIG_DM_ETH |
Mugunthan V N | 88e48be | 2016-11-18 11:06:13 +0530 | [diff] [blame] | 2187 | dev->net = &l_priv->netdev; |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2188 | #else |
| 2189 | dev->net = l_priv->netdev; |
| 2190 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2191 | |
| 2192 | dev->cdc = cdc; |
| 2193 | dev->zlp = zlp; |
| 2194 | |
| 2195 | dev->in_ep = in_ep; |
| 2196 | dev->out_ep = out_ep; |
| 2197 | dev->status_ep = status_ep; |
| 2198 | |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2199 | memset(tmp, 0, sizeof(tmp)); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2200 | /* |
| 2201 | * Module params for these addresses should come from ID proms. |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2202 | * The host side address is used with CDC and RNDIS, and commonly |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2203 | * ends up in a persistent config database. It's not clear if |
| 2204 | * host side code for the SAFE thing cares -- its original BLAN |
| 2205 | * thing didn't, Sharp never assigned those addresses on Zaurii. |
| 2206 | */ |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2207 | #ifndef CONFIG_DM_ETH |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2208 | get_ether_addr(dev_addr, dev->net->enetaddr); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2209 | memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr)); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2210 | #else |
| 2211 | get_ether_addr(dev_addr, pdata->enetaddr); |
| 2212 | memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr)); |
| 2213 | #endif |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2214 | |
| 2215 | get_ether_addr(host_addr, dev->host_mac); |
| 2216 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2217 | sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X", |
| 2218 | dev->host_mac[0], dev->host_mac[1], |
| 2219 | dev->host_mac[2], dev->host_mac[3], |
| 2220 | dev->host_mac[4], dev->host_mac[5]); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2221 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2222 | if (rndis) { |
| 2223 | status = rndis_init(); |
| 2224 | if (status < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2225 | pr_err("can't init RNDIS, %d", status); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2226 | goto fail; |
| 2227 | } |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2230 | /* |
| 2231 | * use PKTSIZE (or aligned... from u-boot) and set |
| 2232 | * wMaxSegmentSize accordingly |
| 2233 | */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2234 | dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/ |
| 2235 | |
| 2236 | /* preallocate control message data and buffer */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2237 | dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2238 | if (!dev->req) |
| 2239 | goto fail; |
| 2240 | dev->req->buf = control_req; |
| 2241 | dev->req->complete = eth_setup_complete; |
| 2242 | |
| 2243 | /* ... and maybe likewise for status transfer */ |
Lukasz Dalek | 284c4cf | 2012-10-02 17:04:31 +0200 | [diff] [blame] | 2244 | #if defined(CONFIG_USB_ETH_CDC) || defined(CONFIG_USB_ETH_RNDIS) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2245 | if (dev->status_ep) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2246 | dev->stat_req = usb_ep_alloc_request(dev->status_ep, |
| 2247 | GFP_KERNEL); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2248 | if (!dev->stat_req) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2249 | usb_ep_free_request(dev->status_ep, dev->req); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2250 | |
| 2251 | goto fail; |
| 2252 | } |
Vitaly Kuzmichev | ef8d8d7 | 2010-08-13 17:01:06 +0400 | [diff] [blame] | 2253 | dev->stat_req->buf = status_req; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2254 | dev->stat_req->context = NULL; |
| 2255 | } |
| 2256 | #endif |
| 2257 | |
| 2258 | /* finish hookup to lower layer ... */ |
| 2259 | dev->gadget = gadget; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2260 | set_gadget_data(gadget, dev); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2261 | gadget->ep0->driver_data = dev; |
| 2262 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2263 | /* |
| 2264 | * two kinds of host-initiated state changes: |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2265 | * - iff DATA transfer is active, carrier is "on" |
| 2266 | * - tx queueing enabled if open *and* carrier is "on" |
| 2267 | */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2268 | |
| 2269 | printf("using %s, OUT %s IN %s%s%s\n", gadget->name, |
| 2270 | out_ep->name, in_ep->name, |
| 2271 | status_ep ? " STATUS " : "", |
| 2272 | status_ep ? status_ep->name : "" |
| 2273 | ); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2274 | #ifndef CONFIG_DM_ETH |
| 2275 | printf("MAC %pM\n", dev->net->enetaddr); |
| 2276 | #else |
| 2277 | printf("MAC %pM\n", pdata->enetaddr); |
| 2278 | #endif |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2279 | |
| 2280 | if (cdc || rndis) |
| 2281 | printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 2282 | dev->host_mac[0], dev->host_mac[1], |
| 2283 | dev->host_mac[2], dev->host_mac[3], |
| 2284 | dev->host_mac[4], dev->host_mac[5]); |
| 2285 | |
| 2286 | if (rndis) { |
| 2287 | u32 vendorID = 0; |
| 2288 | |
| 2289 | /* FIXME RNDIS vendor id == "vendor NIC code" == ? */ |
| 2290 | |
| 2291 | dev->rndis_config = rndis_register(rndis_control_ack); |
| 2292 | if (dev->rndis_config < 0) { |
| 2293 | fail0: |
| 2294 | eth_unbind(gadget); |
| 2295 | debug("RNDIS setup failed\n"); |
| 2296 | status = -ENODEV; |
| 2297 | goto fail; |
| 2298 | } |
| 2299 | |
| 2300 | /* these set up a lot of the OIDs that RNDIS needs */ |
| 2301 | rndis_set_host_mac(dev->rndis_config, dev->host_mac); |
| 2302 | if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu, |
| 2303 | &dev->stats, &dev->cdc_filter)) |
| 2304 | goto fail0; |
| 2305 | if (rndis_set_param_vendor(dev->rndis_config, vendorID, |
| 2306 | manufacturer)) |
| 2307 | goto fail0; |
| 2308 | if (rndis_set_param_medium(dev->rndis_config, |
| 2309 | NDIS_MEDIUM_802_3, 0)) |
| 2310 | goto fail0; |
| 2311 | printf("RNDIS ready\n"); |
| 2312 | } |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2313 | return 0; |
| 2314 | |
| 2315 | fail: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2316 | pr_err("%s failed, status = %d", __func__, status); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2317 | eth_unbind(gadget); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2318 | return status; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2319 | } |
| 2320 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2321 | /*-------------------------------------------------------------------------*/ |
Jean-Jacques Hiblot | 128151f | 2019-01-22 16:48:16 +0100 | [diff] [blame] | 2322 | static void _usb_eth_halt(struct ether_priv *priv); |
| 2323 | |
Mugunthan V N | db5eb2a | 2016-11-18 11:08:27 +0530 | [diff] [blame] | 2324 | static int _usb_eth_init(struct ether_priv *priv) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2325 | { |
Mugunthan V N | 8ec9c74 | 2016-11-18 11:07:18 +0530 | [diff] [blame] | 2326 | struct eth_dev *dev = &priv->ethdev; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2327 | struct usb_gadget *gadget; |
| 2328 | unsigned long ts; |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 2329 | int ret; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2330 | unsigned long timeout = USB_CONNECT_TIMEOUT; |
| 2331 | |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 2332 | ret = usb_gadget_initialize(0); |
| 2333 | if (ret) |
| 2334 | return ret; |
Kishon Vijay Abraham I | 8db2520 | 2015-08-19 13:49:46 +0530 | [diff] [blame] | 2335 | |
Vitaly Kuzmichev | be0afbf | 2010-12-28 16:59:32 +0300 | [diff] [blame] | 2336 | /* Configure default mac-addresses for the USB ethernet device */ |
| 2337 | #ifdef CONFIG_USBNET_DEV_ADDR |
| 2338 | strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr)); |
| 2339 | #endif |
| 2340 | #ifdef CONFIG_USBNET_HOST_ADDR |
| 2341 | strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr)); |
| 2342 | #endif |
| 2343 | /* Check if the user overruled the MAC addresses */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 2344 | if (env_get("usbnet_devaddr")) |
| 2345 | strlcpy(dev_addr, env_get("usbnet_devaddr"), |
Vitaly Kuzmichev | be0afbf | 2010-12-28 16:59:32 +0300 | [diff] [blame] | 2346 | sizeof(dev_addr)); |
| 2347 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 2348 | if (env_get("usbnet_hostaddr")) |
| 2349 | strlcpy(host_addr, env_get("usbnet_hostaddr"), |
Vitaly Kuzmichev | be0afbf | 2010-12-28 16:59:32 +0300 | [diff] [blame] | 2350 | sizeof(host_addr)); |
| 2351 | |
| 2352 | if (!is_eth_addr_valid(dev_addr)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2353 | pr_err("Need valid 'usbnet_devaddr' to be set"); |
Vitaly Kuzmichev | be0afbf | 2010-12-28 16:59:32 +0300 | [diff] [blame] | 2354 | goto fail; |
| 2355 | } |
| 2356 | if (!is_eth_addr_valid(host_addr)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2357 | pr_err("Need valid 'usbnet_hostaddr' to be set"); |
Vitaly Kuzmichev | be0afbf | 2010-12-28 16:59:32 +0300 | [diff] [blame] | 2358 | goto fail; |
| 2359 | } |
| 2360 | |
Mugunthan V N | 8ec9c74 | 2016-11-18 11:07:18 +0530 | [diff] [blame] | 2361 | priv->eth_driver.speed = DEVSPEED; |
| 2362 | priv->eth_driver.bind = eth_bind; |
| 2363 | priv->eth_driver.unbind = eth_unbind; |
| 2364 | priv->eth_driver.setup = eth_setup; |
| 2365 | priv->eth_driver.reset = eth_disconnect; |
| 2366 | priv->eth_driver.disconnect = eth_disconnect; |
| 2367 | priv->eth_driver.suspend = eth_suspend; |
| 2368 | priv->eth_driver.resume = eth_resume; |
| 2369 | if (usb_gadget_register_driver(&priv->eth_driver) < 0) |
Lei Wen | 4af3273 | 2010-12-01 23:43:43 +0800 | [diff] [blame] | 2370 | goto fail; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2371 | |
| 2372 | dev->network_started = 0; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2373 | |
| 2374 | packet_received = 0; |
| 2375 | packet_sent = 0; |
| 2376 | |
| 2377 | gadget = dev->gadget; |
| 2378 | usb_gadget_connect(gadget); |
| 2379 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 2380 | if (env_get("cdc_connect_timeout")) |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 2381 | timeout = dectoul(env_get("cdc_connect_timeout"), NULL) * CONFIG_SYS_HZ; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2382 | ts = get_timer(0); |
Mugunthan V N | 3963de3 | 2016-11-18 10:49:13 +0530 | [diff] [blame] | 2383 | while (!dev->network_started) { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2384 | /* Handle control-c and timeouts */ |
| 2385 | if (ctrlc() || (get_timer(ts) > timeout)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2386 | pr_err("The remote end did not respond in time."); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2387 | goto fail; |
| 2388 | } |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2389 | usb_gadget_handle_interrupts(0); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2390 | } |
| 2391 | |
Vitaly Kuzmichev | e4bd386 | 2010-09-22 13:13:56 +0400 | [diff] [blame] | 2392 | packet_received = 0; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2393 | rx_submit(dev, dev->rx_req, 0); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2394 | return 0; |
| 2395 | fail: |
Jean-Jacques Hiblot | 128151f | 2019-01-22 16:48:16 +0100 | [diff] [blame] | 2396 | _usb_eth_halt(priv); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2397 | return -1; |
| 2398 | } |
| 2399 | |
Mugunthan V N | db5eb2a | 2016-11-18 11:08:27 +0530 | [diff] [blame] | 2400 | static int _usb_eth_send(struct ether_priv *priv, void *packet, int length) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2401 | { |
| 2402 | int retval; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2403 | void *rndis_pkt = NULL; |
Mugunthan V N | 8ec9c74 | 2016-11-18 11:07:18 +0530 | [diff] [blame] | 2404 | struct eth_dev *dev = &priv->ethdev; |
Vitaly Kuzmichev | 2907b2c | 2010-09-22 13:13:55 +0400 | [diff] [blame] | 2405 | struct usb_request *req = dev->tx_req; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2406 | unsigned long ts; |
| 2407 | unsigned long timeout = USB_CONNECT_TIMEOUT; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2408 | |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2409 | debug("%s:...\n", __func__); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2410 | |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2411 | /* new buffer is needed to include RNDIS header */ |
| 2412 | if (rndis_active(dev)) { |
| 2413 | rndis_pkt = malloc(length + |
| 2414 | sizeof(struct rndis_packet_msg_type)); |
| 2415 | if (!rndis_pkt) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2416 | pr_err("No memory to alloc RNDIS packet"); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2417 | goto drop; |
| 2418 | } |
| 2419 | rndis_add_hdr(rndis_pkt, length); |
| 2420 | memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type), |
Joe Hershberger | e4e0488 | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 2421 | packet, length); |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2422 | packet = rndis_pkt; |
| 2423 | length += sizeof(struct rndis_packet_msg_type); |
| 2424 | } |
Joe Hershberger | e4e0488 | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 2425 | req->buf = packet; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2426 | req->context = NULL; |
| 2427 | req->complete = tx_complete; |
| 2428 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2429 | /* |
| 2430 | * use zlp framing on tx for strict CDC-Ether conformance, |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2431 | * though any robust network rx path ignores extra padding. |
| 2432 | * and some hardware doesn't like to write zlps. |
| 2433 | */ |
| 2434 | req->zero = 1; |
| 2435 | if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0) |
| 2436 | length++; |
| 2437 | |
| 2438 | req->length = length; |
| 2439 | #if 0 |
| 2440 | /* throttle highspeed IRQ rate back slightly */ |
| 2441 | if (gadget_is_dualspeed(dev->gadget)) |
| 2442 | req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH) |
| 2443 | ? ((dev->tx_qlen % qmult) != 0) : 0; |
| 2444 | #endif |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2445 | dev->tx_qlen = 1; |
| 2446 | ts = get_timer(0); |
| 2447 | packet_sent = 0; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2448 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2449 | retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2450 | |
| 2451 | if (!retval) |
Vitaly Kuzmichev | ae5f932 | 2010-08-13 16:57:51 +0400 | [diff] [blame] | 2452 | debug("%s: packet queued\n", __func__); |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 2453 | while (!packet_sent) { |
| 2454 | if (get_timer(ts) > timeout) { |
| 2455 | printf("timeout sending packets to usb ethernet\n"); |
| 2456 | return -1; |
| 2457 | } |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2458 | usb_gadget_handle_interrupts(0); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2459 | } |
Heinrich Schuchardt | 637ad63 | 2020-04-19 12:11:12 +0200 | [diff] [blame] | 2460 | free(rndis_pkt); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2461 | |
| 2462 | return 0; |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2463 | drop: |
| 2464 | dev->stats.tx_dropped++; |
| 2465 | return -ENOMEM; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2466 | } |
| 2467 | |
Mugunthan V N | db5eb2a | 2016-11-18 11:08:27 +0530 | [diff] [blame] | 2468 | static int _usb_eth_recv(struct ether_priv *priv) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2469 | { |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2470 | usb_gadget_handle_interrupts(0); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2471 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2472 | return 0; |
| 2473 | } |
| 2474 | |
Jean-Jacques Hiblot | 128151f | 2019-01-22 16:48:16 +0100 | [diff] [blame] | 2475 | static void _usb_eth_halt(struct ether_priv *priv) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2476 | { |
Mugunthan V N | 8ec9c74 | 2016-11-18 11:07:18 +0530 | [diff] [blame] | 2477 | struct eth_dev *dev = &priv->ethdev; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2478 | |
Lei Wen | 4af3273 | 2010-12-01 23:43:43 +0800 | [diff] [blame] | 2479 | /* If the gadget not registered, simple return */ |
| 2480 | if (!dev->gadget) |
| 2481 | return; |
| 2482 | |
Vitaly Kuzmichev | 3433c33 | 2011-02-11 18:18:35 +0300 | [diff] [blame] | 2483 | /* |
| 2484 | * Some USB controllers may need additional deinitialization here |
| 2485 | * before dropping pull-up (also due to hardware issues). |
| 2486 | * For example: unhandled interrupt with status stage started may |
| 2487 | * bring the controller to fully broken state (until board reset). |
| 2488 | * There are some variants to debug and fix such cases: |
| 2489 | * 1) In the case of RNDIS connection eth_stop can perform additional |
| 2490 | * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition. |
| 2491 | * 2) 'pullup' callback in your UDC driver can be improved to perform |
| 2492 | * this deinitialization. |
| 2493 | */ |
Vitaly Kuzmichev | 216c447 | 2011-02-11 18:18:34 +0300 | [diff] [blame] | 2494 | eth_stop(dev); |
| 2495 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2496 | usb_gadget_disconnect(dev->gadget); |
Vitaly Kuzmichev | 08d4dd7 | 2011-02-11 18:18:31 +0300 | [diff] [blame] | 2497 | |
| 2498 | /* Clear pending interrupt */ |
| 2499 | if (dev->network_started) { |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 2500 | usb_gadget_handle_interrupts(0); |
Vitaly Kuzmichev | 08d4dd7 | 2011-02-11 18:18:31 +0300 | [diff] [blame] | 2501 | dev->network_started = 0; |
| 2502 | } |
| 2503 | |
Mugunthan V N | 8ec9c74 | 2016-11-18 11:07:18 +0530 | [diff] [blame] | 2504 | usb_gadget_unregister_driver(&priv->eth_driver); |
Jean-Jacques Hiblot | 585e199 | 2018-11-29 10:52:41 +0100 | [diff] [blame] | 2505 | usb_gadget_release(0); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2506 | } |
| 2507 | |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2508 | #ifndef CONFIG_DM_ETH |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 2509 | static int usb_eth_init(struct eth_device *netdev, struct bd_info *bd) |
Mugunthan V N | db5eb2a | 2016-11-18 11:08:27 +0530 | [diff] [blame] | 2510 | { |
| 2511 | struct ether_priv *priv = (struct ether_priv *)netdev->priv; |
| 2512 | |
| 2513 | return _usb_eth_init(priv); |
| 2514 | } |
| 2515 | |
| 2516 | static int usb_eth_send(struct eth_device *netdev, void *packet, int length) |
| 2517 | { |
| 2518 | struct ether_priv *priv = (struct ether_priv *)netdev->priv; |
| 2519 | |
| 2520 | return _usb_eth_send(priv, packet, length); |
| 2521 | } |
| 2522 | |
| 2523 | static int usb_eth_recv(struct eth_device *netdev) |
| 2524 | { |
| 2525 | struct ether_priv *priv = (struct ether_priv *)netdev->priv; |
| 2526 | struct eth_dev *dev = &priv->ethdev; |
| 2527 | int ret; |
| 2528 | |
| 2529 | ret = _usb_eth_recv(priv); |
| 2530 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2531 | pr_err("error packet receive\n"); |
Mugunthan V N | db5eb2a | 2016-11-18 11:08:27 +0530 | [diff] [blame] | 2532 | return ret; |
| 2533 | } |
| 2534 | |
| 2535 | if (!packet_received) |
| 2536 | return 0; |
| 2537 | |
| 2538 | if (dev->rx_req) { |
| 2539 | net_process_received_packet(net_rx_packets[0], |
| 2540 | dev->rx_req->length); |
| 2541 | } else { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2542 | pr_err("dev->rx_req invalid"); |
Mugunthan V N | db5eb2a | 2016-11-18 11:08:27 +0530 | [diff] [blame] | 2543 | } |
| 2544 | packet_received = 0; |
| 2545 | rx_submit(dev, dev->rx_req, 0); |
| 2546 | |
| 2547 | return 0; |
| 2548 | } |
| 2549 | |
| 2550 | void usb_eth_halt(struct eth_device *netdev) |
| 2551 | { |
| 2552 | struct ether_priv *priv = (struct ether_priv *)netdev->priv; |
| 2553 | |
| 2554 | _usb_eth_halt(priv); |
| 2555 | } |
| 2556 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 2557 | int usb_eth_initialize(struct bd_info *bi) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2558 | { |
Mugunthan V N | 88e48be | 2016-11-18 11:06:13 +0530 | [diff] [blame] | 2559 | struct eth_device *netdev = &l_priv->netdev; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2560 | |
Vitaly Kuzmichev | a36cff1 | 2010-12-28 16:59:31 +0300 | [diff] [blame] | 2561 | strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name)); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2562 | |
| 2563 | netdev->init = usb_eth_init; |
| 2564 | netdev->send = usb_eth_send; |
| 2565 | netdev->recv = usb_eth_recv; |
| 2566 | netdev->halt = usb_eth_halt; |
Mugunthan V N | 8ec9c74 | 2016-11-18 11:07:18 +0530 | [diff] [blame] | 2567 | netdev->priv = l_priv; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2568 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2569 | eth_register(netdev); |
| 2570 | return 0; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2571 | } |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2572 | #else |
| 2573 | static int usb_eth_start(struct udevice *dev) |
| 2574 | { |
| 2575 | struct ether_priv *priv = dev_get_priv(dev); |
| 2576 | |
| 2577 | return _usb_eth_init(priv); |
| 2578 | } |
| 2579 | |
| 2580 | static int usb_eth_send(struct udevice *dev, void *packet, int length) |
| 2581 | { |
| 2582 | struct ether_priv *priv = dev_get_priv(dev); |
| 2583 | |
| 2584 | return _usb_eth_send(priv, packet, length); |
| 2585 | } |
| 2586 | |
| 2587 | static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 2588 | { |
| 2589 | struct ether_priv *priv = dev_get_priv(dev); |
| 2590 | struct eth_dev *ethdev = &priv->ethdev; |
| 2591 | int ret; |
| 2592 | |
| 2593 | ret = _usb_eth_recv(priv); |
| 2594 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2595 | pr_err("error packet receive\n"); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2596 | return ret; |
| 2597 | } |
| 2598 | |
| 2599 | if (packet_received) { |
| 2600 | if (ethdev->rx_req) { |
| 2601 | *packetp = (uchar *)net_rx_packets[0]; |
| 2602 | return ethdev->rx_req->length; |
| 2603 | } else { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2604 | pr_err("dev->rx_req invalid"); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2605 | return -EFAULT; |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | return -EAGAIN; |
| 2610 | } |
| 2611 | |
| 2612 | static int usb_eth_free_pkt(struct udevice *dev, uchar *packet, |
| 2613 | int length) |
| 2614 | { |
| 2615 | struct ether_priv *priv = dev_get_priv(dev); |
| 2616 | struct eth_dev *ethdev = &priv->ethdev; |
| 2617 | |
| 2618 | packet_received = 0; |
| 2619 | |
| 2620 | return rx_submit(ethdev, ethdev->rx_req, 0); |
| 2621 | } |
| 2622 | |
| 2623 | static void usb_eth_stop(struct udevice *dev) |
| 2624 | { |
| 2625 | struct ether_priv *priv = dev_get_priv(dev); |
| 2626 | |
| 2627 | _usb_eth_halt(priv); |
| 2628 | } |
| 2629 | |
| 2630 | static int usb_eth_probe(struct udevice *dev) |
| 2631 | { |
| 2632 | struct ether_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 2633 | struct eth_pdata *pdata = dev_get_plat(dev); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2634 | |
| 2635 | priv->netdev = dev; |
| 2636 | l_priv = priv; |
| 2637 | |
| 2638 | get_ether_addr(CONFIG_USBNET_DEVADDR, pdata->enetaddr); |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 2639 | eth_env_set_enetaddr("usbnet_devaddr", pdata->enetaddr); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2640 | |
| 2641 | return 0; |
| 2642 | } |
| 2643 | |
| 2644 | static const struct eth_ops usb_eth_ops = { |
| 2645 | .start = usb_eth_start, |
| 2646 | .send = usb_eth_send, |
| 2647 | .recv = usb_eth_recv, |
| 2648 | .free_pkt = usb_eth_free_pkt, |
| 2649 | .stop = usb_eth_stop, |
| 2650 | }; |
| 2651 | |
| 2652 | int usb_ether_init(void) |
| 2653 | { |
| 2654 | struct udevice *dev; |
| 2655 | struct udevice *usb_dev; |
| 2656 | int ret; |
| 2657 | |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 2658 | ret = uclass_first_device(UCLASS_USB_GADGET_GENERIC, &usb_dev); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2659 | if (!usb_dev || ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2660 | pr_err("No USB device found\n"); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2661 | return ret; |
| 2662 | } |
| 2663 | |
| 2664 | ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev); |
| 2665 | if (!dev || ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 2666 | pr_err("usb - not able to bind usb_ether device\n"); |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2667 | return ret; |
| 2668 | } |
| 2669 | |
| 2670 | return 0; |
| 2671 | } |
| 2672 | |
| 2673 | U_BOOT_DRIVER(eth_usb) = { |
| 2674 | .name = "usb_ether", |
| 2675 | .id = UCLASS_ETH, |
| 2676 | .probe = usb_eth_probe, |
| 2677 | .ops = &usb_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 2678 | .priv_auto = sizeof(struct ether_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 2679 | .plat_auto = sizeof(struct eth_pdata), |
Mugunthan V N | 095b761 | 2016-11-18 11:09:15 +0530 | [diff] [blame] | 2680 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 2681 | }; |
| 2682 | #endif /* CONFIG_DM_ETH */ |