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