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