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