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