Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 7 | #include <dm.h> |
Simon Glass | e96ba56 | 2015-07-07 20:53:35 -0600 | [diff] [blame] | 8 | #include <errno.h> |
Simon Glass | 5b026f9 | 2015-07-06 16:47:50 -0600 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame^] | 10 | #include <net.h> |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 11 | #include <usb.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame^] | 12 | #include <asm/cache.h> |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 14 | |
| 15 | #include "usb_ether.h" |
| 16 | |
Simon Glass | 5b026f9 | 2015-07-06 16:47:50 -0600 | [diff] [blame] | 17 | #ifdef CONFIG_DM_ETH |
| 18 | |
| 19 | #define USB_BULK_RECV_TIMEOUT 500 |
| 20 | |
| 21 | int usb_ether_register(struct udevice *dev, struct ueth_data *ueth, int rxsize) |
| 22 | { |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 23 | struct usb_device *udev = dev_get_parent_priv(dev); |
Simon Glass | 5b026f9 | 2015-07-06 16:47:50 -0600 | [diff] [blame] | 24 | struct usb_interface_descriptor *iface_desc; |
| 25 | bool ep_in_found = false, ep_out_found = false; |
| 26 | struct usb_interface *iface; |
| 27 | const int ifnum = 0; /* Always use interface 0 */ |
| 28 | int ret, i; |
| 29 | |
| 30 | iface = &udev->config.if_desc[ifnum]; |
| 31 | iface_desc = &udev->config.if_desc[ifnum].desc; |
| 32 | |
| 33 | /* Initialize the ueth_data structure with some useful info */ |
| 34 | ueth->ifnum = ifnum; |
| 35 | ueth->subclass = iface_desc->bInterfaceSubClass; |
| 36 | ueth->protocol = iface_desc->bInterfaceProtocol; |
| 37 | |
| 38 | /* |
| 39 | * We are expecting a minimum of 3 endpoints - in, out (bulk), and int. |
| 40 | * We will ignore any others. |
| 41 | */ |
| 42 | for (i = 0; i < iface_desc->bNumEndpoints; i++) { |
| 43 | int ep_addr = iface->ep_desc[i].bEndpointAddress; |
| 44 | |
| 45 | /* is it an BULK endpoint? */ |
| 46 | if ((iface->ep_desc[i].bmAttributes & |
| 47 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) { |
| 48 | if (ep_addr & USB_DIR_IN && !ep_in_found) { |
| 49 | ueth->ep_in = ep_addr & |
| 50 | USB_ENDPOINT_NUMBER_MASK; |
| 51 | ep_in_found = true; |
| 52 | } else if (!(ep_addr & USB_DIR_IN) && !ep_out_found) { |
| 53 | ueth->ep_out = ep_addr & |
| 54 | USB_ENDPOINT_NUMBER_MASK; |
| 55 | ep_out_found = true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* is it an interrupt endpoint? */ |
| 60 | if ((iface->ep_desc[i].bmAttributes & |
| 61 | USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) { |
| 62 | ueth->ep_int = iface->ep_desc[i].bEndpointAddress & |
| 63 | USB_ENDPOINT_NUMBER_MASK; |
| 64 | ueth->irqinterval = iface->ep_desc[i].bInterval; |
| 65 | } |
| 66 | } |
| 67 | debug("Endpoints In %d Out %d Int %d\n", ueth->ep_in, ueth->ep_out, |
| 68 | ueth->ep_int); |
| 69 | |
| 70 | /* Do some basic sanity checks, and bail if we find a problem */ |
| 71 | if (!ueth->ep_in || !ueth->ep_out || !ueth->ep_int) { |
| 72 | debug("%s: %s: Cannot find endpoints\n", __func__, dev->name); |
| 73 | return -ENXIO; |
| 74 | } |
| 75 | |
| 76 | ueth->rxsize = rxsize; |
Stephen Warren | dfdfe6f | 2016-02-12 13:56:01 -0700 | [diff] [blame] | 77 | ueth->rxbuf = memalign(ARCH_DMA_MINALIGN, rxsize); |
Simon Glass | 5b026f9 | 2015-07-06 16:47:50 -0600 | [diff] [blame] | 78 | if (!ueth->rxbuf) |
| 79 | return -ENOMEM; |
| 80 | |
| 81 | ret = usb_set_interface(udev, iface_desc->bInterfaceNumber, ifnum); |
| 82 | if (ret) { |
| 83 | debug("%s: %s: Cannot set interface: %d\n", __func__, dev->name, |
| 84 | ret); |
| 85 | return ret; |
| 86 | } |
| 87 | ueth->pusb_dev = udev; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int usb_ether_deregister(struct ueth_data *ueth) |
| 93 | { |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int usb_ether_receive(struct ueth_data *ueth, int rxsize) |
| 98 | { |
| 99 | int actual_len; |
| 100 | int ret; |
| 101 | |
| 102 | if (rxsize > ueth->rxsize) |
| 103 | return -EINVAL; |
| 104 | ret = usb_bulk_msg(ueth->pusb_dev, |
| 105 | usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in), |
| 106 | ueth->rxbuf, rxsize, &actual_len, |
| 107 | USB_BULK_RECV_TIMEOUT); |
| 108 | debug("Rx: len = %u, actual = %u, err = %d\n", rxsize, actual_len, ret); |
| 109 | if (ret) { |
| 110 | printf("Rx: failed to receive: %d\n", ret); |
| 111 | return ret; |
| 112 | } |
| 113 | if (actual_len > rxsize) { |
| 114 | debug("Rx: received too many bytes %d\n", actual_len); |
| 115 | return -ENOSPC; |
| 116 | } |
| 117 | ueth->rxlen = actual_len; |
| 118 | ueth->rxptr = 0; |
| 119 | |
| 120 | return actual_len ? 0 : -EAGAIN; |
| 121 | } |
| 122 | |
| 123 | void usb_ether_advance_rxbuf(struct ueth_data *ueth, int num_bytes) |
| 124 | { |
| 125 | ueth->rxptr += num_bytes; |
| 126 | if (num_bytes < 0 || ueth->rxptr >= ueth->rxlen) |
| 127 | ueth->rxlen = 0; |
| 128 | } |
| 129 | |
| 130 | int usb_ether_get_rx_bytes(struct ueth_data *ueth, uint8_t **ptrp) |
| 131 | { |
| 132 | if (!ueth->rxlen) |
| 133 | return 0; |
| 134 | |
| 135 | *ptrp = &ueth->rxbuf[ueth->rxptr]; |
| 136 | |
| 137 | return ueth->rxlen - ueth->rxptr; |
| 138 | } |
| 139 | |
| 140 | #else |
| 141 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 142 | typedef void (*usb_eth_before_probe)(void); |
| 143 | typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum, |
| 144 | struct ueth_data *ss); |
| 145 | typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss, |
| 146 | struct eth_device *dev_desc); |
| 147 | |
| 148 | struct usb_eth_prob_dev { |
| 149 | usb_eth_before_probe before_probe; /* optional */ |
| 150 | usb_eth_probe probe; |
| 151 | usb_eth_get_info get_info; |
| 152 | }; |
| 153 | |
| 154 | /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */ |
| 155 | static const struct usb_eth_prob_dev prob_dev[] = { |
Simon Glass | 246d693 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 156 | #ifdef CONFIG_USB_ETHER_ASIX |
| 157 | { |
| 158 | .before_probe = asix_eth_before_probe, |
| 159 | .probe = asix_eth_probe, |
| 160 | .get_info = asix_eth_get_info, |
| 161 | }, |
| 162 | #endif |
Rene Griessl | 979beee | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 163 | #ifdef CONFIG_USB_ETHER_ASIX88179 |
| 164 | { |
| 165 | .before_probe = ax88179_eth_before_probe, |
| 166 | .probe = ax88179_eth_probe, |
| 167 | .get_info = ax88179_eth_get_info, |
| 168 | }, |
| 169 | #endif |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 170 | #ifdef CONFIG_USB_ETHER_MCS7830 |
| 171 | { |
| 172 | .before_probe = mcs7830_eth_before_probe, |
| 173 | .probe = mcs7830_eth_probe, |
| 174 | .get_info = mcs7830_eth_get_info, |
| 175 | }, |
| 176 | #endif |
Simon Glass | 246c119 | 2011-06-13 16:13:09 -0700 | [diff] [blame] | 177 | #ifdef CONFIG_USB_ETHER_SMSC95XX |
| 178 | { |
| 179 | .before_probe = smsc95xx_eth_before_probe, |
| 180 | .probe = smsc95xx_eth_probe, |
| 181 | .get_info = smsc95xx_eth_get_info, |
| 182 | }, |
| 183 | #endif |
Ted Chen | 9b6dbd4 | 2016-01-20 14:24:52 +0800 | [diff] [blame] | 184 | #ifdef CONFIG_USB_ETHER_RTL8152 |
| 185 | { |
| 186 | .before_probe = r8152_eth_before_probe, |
| 187 | .probe = r8152_eth_probe, |
| 188 | .get_info = r8152_eth_get_info, |
| 189 | }, |
| 190 | #endif |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 191 | { }, /* END */ |
| 192 | }; |
| 193 | |
| 194 | static int usb_max_eth_dev; /* number of highest available usb eth device */ |
| 195 | static struct ueth_data usb_eth[USB_MAX_ETH_DEV]; |
| 196 | |
| 197 | /******************************************************************************* |
| 198 | * tell if current ethernet device is a usb dongle |
| 199 | */ |
| 200 | int is_eth_dev_on_usb_host(void) |
| 201 | { |
| 202 | int i; |
| 203 | struct eth_device *dev = eth_get_dev(); |
| 204 | |
| 205 | if (dev) { |
| 206 | for (i = 0; i < usb_max_eth_dev; i++) |
| 207 | if (&usb_eth[i].eth_dev == dev) |
| 208 | return 1; |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Given a USB device, ask each driver if it can support it, and attach it |
| 215 | * to the first driver that says 'yes' |
| 216 | */ |
| 217 | static void probe_valid_drivers(struct usb_device *dev) |
| 218 | { |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 219 | struct eth_device *eth; |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 220 | int j; |
| 221 | |
| 222 | for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) { |
| 223 | if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev])) |
| 224 | continue; |
| 225 | /* |
| 226 | * ok, it is a supported eth device. Get info and fill it in |
| 227 | */ |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 228 | eth = &usb_eth[usb_max_eth_dev].eth_dev; |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 229 | if (prob_dev[j].get_info(dev, |
| 230 | &usb_eth[usb_max_eth_dev], |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 231 | eth)) { |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 232 | /* found proper driver */ |
| 233 | /* register with networking stack */ |
| 234 | usb_max_eth_dev++; |
| 235 | |
| 236 | /* |
| 237 | * usb_max_eth_dev must be incremented prior to this |
| 238 | * call since eth_current_changed (internally called) |
| 239 | * relies on it |
| 240 | */ |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 241 | eth_register(eth); |
| 242 | if (eth_write_hwaddr(eth, "usbeth", |
| 243 | usb_max_eth_dev - 1)) |
| 244 | puts("Warning: failed to set MAC address\n"); |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /******************************************************************************* |
| 251 | * scan the usb and reports device info |
| 252 | * to the user if mode = 1 |
| 253 | * returns current device or -1 if no |
| 254 | */ |
| 255 | int usb_host_eth_scan(int mode) |
| 256 | { |
| 257 | int i, old_async; |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 258 | |
| 259 | if (mode == 1) |
Lucas Stach | e6d3345 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 260 | printf(" scanning usb for ethernet devices... "); |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 261 | |
| 262 | old_async = usb_disable_asynch(1); /* asynch transfer not allowed */ |
| 263 | |
Vincent Palatin | 451be50 | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 264 | /* unregister a previously detected device */ |
| 265 | for (i = 0; i < usb_max_eth_dev; i++) |
| 266 | eth_unregister(&usb_eth[i].eth_dev); |
| 267 | |
| 268 | memset(usb_eth, 0, sizeof(usb_eth)); |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 269 | |
| 270 | for (i = 0; prob_dev[i].probe; i++) { |
| 271 | if (prob_dev[i].before_probe) |
| 272 | prob_dev[i].before_probe(); |
| 273 | } |
| 274 | |
| 275 | usb_max_eth_dev = 0; |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 276 | #if CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 277 | /* |
Simon Glass | 46952fb | 2015-07-06 16:47:51 -0600 | [diff] [blame] | 278 | * TODO: We should add U_BOOT_USB_DEVICE() declarations to each USB |
| 279 | * Ethernet driver and then most of this file can be removed. |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 280 | */ |
| 281 | struct udevice *bus; |
| 282 | struct uclass *uc; |
| 283 | int ret; |
| 284 | |
| 285 | ret = uclass_get(UCLASS_USB, &uc); |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | uclass_foreach_dev(bus, uc) { |
| 289 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 290 | struct usb_device *dev; |
| 291 | |
| 292 | dev = usb_get_dev_index(bus, i); /* get device */ |
| 293 | debug("i=%d, %s\n", i, dev ? dev->dev->name : "(done)"); |
| 294 | if (!dev) |
| 295 | break; /* no more devices available */ |
| 296 | |
| 297 | /* |
| 298 | * find valid usb_ether driver for this device, |
| 299 | * if any |
| 300 | */ |
| 301 | probe_valid_drivers(dev); |
| 302 | |
| 303 | /* check limit */ |
| 304 | if (usb_max_eth_dev == USB_MAX_ETH_DEV) |
| 305 | break; |
| 306 | } /* for */ |
| 307 | } |
| 308 | #else |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 309 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 310 | struct usb_device *dev; |
| 311 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 312 | dev = usb_get_dev_index(i); /* get device */ |
| 313 | debug("i=%d\n", i); |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 314 | if (!dev) |
Peter Meerwald | e124c7b | 2012-02-08 05:31:54 +0000 | [diff] [blame] | 315 | break; /* no more devices available */ |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 316 | |
| 317 | /* find valid usb_ether driver for this device, if any */ |
| 318 | probe_valid_drivers(dev); |
| 319 | |
| 320 | /* check limit */ |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 321 | if (usb_max_eth_dev == USB_MAX_ETH_DEV) |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 322 | break; |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 323 | } /* for */ |
Simon Glass | 33b66e8 | 2015-03-25 12:22:43 -0600 | [diff] [blame] | 324 | #endif |
| 325 | if (usb_max_eth_dev == USB_MAX_ETH_DEV) { |
| 326 | printf("max USB Ethernet Device reached: %d stopping\n", |
| 327 | usb_max_eth_dev); |
| 328 | } |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 329 | usb_disable_asynch(old_async); /* restore asynch value */ |
| 330 | printf("%d Ethernet Device(s) found\n", usb_max_eth_dev); |
| 331 | if (usb_max_eth_dev > 0) |
| 332 | return 0; |
| 333 | return -1; |
| 334 | } |
Simon Glass | 5b026f9 | 2015-07-06 16:47:50 -0600 | [diff] [blame] | 335 | #endif |