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