Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <usb.h> |
| 9 | |
| 10 | #include "usb_ether.h" |
| 11 | |
| 12 | typedef void (*usb_eth_before_probe)(void); |
| 13 | typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum, |
| 14 | struct ueth_data *ss); |
| 15 | typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss, |
| 16 | struct eth_device *dev_desc); |
| 17 | |
| 18 | struct usb_eth_prob_dev { |
| 19 | usb_eth_before_probe before_probe; /* optional */ |
| 20 | usb_eth_probe probe; |
| 21 | usb_eth_get_info get_info; |
| 22 | }; |
| 23 | |
| 24 | /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */ |
| 25 | static const struct usb_eth_prob_dev prob_dev[] = { |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame] | 26 | #ifdef CONFIG_USB_ETHER_ASIX |
| 27 | { |
| 28 | .before_probe = asix_eth_before_probe, |
| 29 | .probe = asix_eth_probe, |
| 30 | .get_info = asix_eth_get_info, |
| 31 | }, |
| 32 | #endif |
Rene Griessl | e9954b8 | 2014-11-07 16:53:48 +0100 | [diff] [blame] | 33 | #ifdef CONFIG_USB_ETHER_ASIX88179 |
| 34 | { |
| 35 | .before_probe = ax88179_eth_before_probe, |
| 36 | .probe = ax88179_eth_probe, |
| 37 | .get_info = ax88179_eth_get_info, |
| 38 | }, |
| 39 | #endif |
Gerhard Sittig | df4fb1c | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_USB_ETHER_MCS7830 |
| 41 | { |
| 42 | .before_probe = mcs7830_eth_before_probe, |
| 43 | .probe = mcs7830_eth_probe, |
| 44 | .get_info = mcs7830_eth_get_info, |
| 45 | }, |
| 46 | #endif |
Simon Glass | 291391b | 2011-06-13 16:13:09 -0700 | [diff] [blame] | 47 | #ifdef CONFIG_USB_ETHER_SMSC95XX |
| 48 | { |
| 49 | .before_probe = smsc95xx_eth_before_probe, |
| 50 | .probe = smsc95xx_eth_probe, |
| 51 | .get_info = smsc95xx_eth_get_info, |
| 52 | }, |
| 53 | #endif |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 54 | { }, /* END */ |
| 55 | }; |
| 56 | |
| 57 | static int usb_max_eth_dev; /* number of highest available usb eth device */ |
| 58 | static struct ueth_data usb_eth[USB_MAX_ETH_DEV]; |
| 59 | |
| 60 | /******************************************************************************* |
| 61 | * tell if current ethernet device is a usb dongle |
| 62 | */ |
| 63 | int is_eth_dev_on_usb_host(void) |
| 64 | { |
| 65 | int i; |
| 66 | struct eth_device *dev = eth_get_dev(); |
| 67 | |
| 68 | if (dev) { |
| 69 | for (i = 0; i < usb_max_eth_dev; i++) |
| 70 | if (&usb_eth[i].eth_dev == dev) |
| 71 | return 1; |
| 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Given a USB device, ask each driver if it can support it, and attach it |
| 78 | * to the first driver that says 'yes' |
| 79 | */ |
| 80 | static void probe_valid_drivers(struct usb_device *dev) |
| 81 | { |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 82 | struct eth_device *eth; |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 83 | int j; |
| 84 | |
| 85 | for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) { |
| 86 | if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev])) |
| 87 | continue; |
| 88 | /* |
| 89 | * ok, it is a supported eth device. Get info and fill it in |
| 90 | */ |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 91 | eth = &usb_eth[usb_max_eth_dev].eth_dev; |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 92 | if (prob_dev[j].get_info(dev, |
| 93 | &usb_eth[usb_max_eth_dev], |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 94 | eth)) { |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 95 | /* found proper driver */ |
| 96 | /* register with networking stack */ |
| 97 | usb_max_eth_dev++; |
| 98 | |
| 99 | /* |
| 100 | * usb_max_eth_dev must be incremented prior to this |
| 101 | * call since eth_current_changed (internally called) |
| 102 | * relies on it |
| 103 | */ |
Simon Glass | 7616e78 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 104 | eth_register(eth); |
| 105 | if (eth_write_hwaddr(eth, "usbeth", |
| 106 | usb_max_eth_dev - 1)) |
| 107 | puts("Warning: failed to set MAC address\n"); |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /******************************************************************************* |
| 114 | * scan the usb and reports device info |
| 115 | * to the user if mode = 1 |
| 116 | * returns current device or -1 if no |
| 117 | */ |
| 118 | int usb_host_eth_scan(int mode) |
| 119 | { |
| 120 | int i, old_async; |
| 121 | struct usb_device *dev; |
| 122 | |
| 123 | |
| 124 | if (mode == 1) |
Lucas Stach | 93c2582 | 2012-09-26 00:14:36 +0200 | [diff] [blame] | 125 | printf(" scanning usb for ethernet devices... "); |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 126 | |
| 127 | old_async = usb_disable_asynch(1); /* asynch transfer not allowed */ |
| 128 | |
Vincent Palatin | e7e982d | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 129 | /* unregister a previously detected device */ |
| 130 | for (i = 0; i < usb_max_eth_dev; i++) |
| 131 | eth_unregister(&usb_eth[i].eth_dev); |
| 132 | |
| 133 | memset(usb_eth, 0, sizeof(usb_eth)); |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 134 | |
| 135 | for (i = 0; prob_dev[i].probe; i++) { |
| 136 | if (prob_dev[i].before_probe) |
| 137 | prob_dev[i].before_probe(); |
| 138 | } |
| 139 | |
| 140 | usb_max_eth_dev = 0; |
| 141 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 142 | dev = usb_get_dev_index(i); /* get device */ |
| 143 | debug("i=%d\n", i); |
| 144 | if (dev == NULL) |
Peter Meerwald | d1f862f | 2012-02-08 05:31:54 +0000 | [diff] [blame] | 145 | break; /* no more devices available */ |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 146 | |
| 147 | /* find valid usb_ether driver for this device, if any */ |
| 148 | probe_valid_drivers(dev); |
| 149 | |
| 150 | /* check limit */ |
| 151 | if (usb_max_eth_dev == USB_MAX_ETH_DEV) { |
| 152 | printf("max USB Ethernet Device reached: %d stopping\n", |
| 153 | usb_max_eth_dev); |
| 154 | break; |
| 155 | } |
| 156 | } /* for */ |
| 157 | |
| 158 | usb_disable_asynch(old_async); /* restore asynch value */ |
| 159 | printf("%d Ethernet Device(s) found\n", usb_max_eth_dev); |
| 160 | if (usb_max_eth_dev > 0) |
| 161 | return 0; |
| 162 | return -1; |
| 163 | } |