Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 3 | * See file CREDITS for list of people who contributed to this |
| 4 | * project. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | * MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <usb.h> |
| 24 | |
| 25 | #include "usb_ether.h" |
| 26 | |
| 27 | typedef void (*usb_eth_before_probe)(void); |
| 28 | typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum, |
| 29 | struct ueth_data *ss); |
| 30 | typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss, |
| 31 | struct eth_device *dev_desc); |
| 32 | |
| 33 | struct usb_eth_prob_dev { |
| 34 | usb_eth_before_probe before_probe; /* optional */ |
| 35 | usb_eth_probe probe; |
| 36 | usb_eth_get_info get_info; |
| 37 | }; |
| 38 | |
| 39 | /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */ |
| 40 | static const struct usb_eth_prob_dev prob_dev[] = { |
Simon Glass | 9b70e00 | 2011-02-16 11:14:34 -0800 | [diff] [blame^] | 41 | #ifdef CONFIG_USB_ETHER_ASIX |
| 42 | { |
| 43 | .before_probe = asix_eth_before_probe, |
| 44 | .probe = asix_eth_probe, |
| 45 | .get_info = asix_eth_get_info, |
| 46 | }, |
| 47 | #endif |
Simon Glass | 89d4836 | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 48 | { }, /* END */ |
| 49 | }; |
| 50 | |
| 51 | static int usb_max_eth_dev; /* number of highest available usb eth device */ |
| 52 | static struct ueth_data usb_eth[USB_MAX_ETH_DEV]; |
| 53 | |
| 54 | /******************************************************************************* |
| 55 | * tell if current ethernet device is a usb dongle |
| 56 | */ |
| 57 | int is_eth_dev_on_usb_host(void) |
| 58 | { |
| 59 | int i; |
| 60 | struct eth_device *dev = eth_get_dev(); |
| 61 | |
| 62 | if (dev) { |
| 63 | for (i = 0; i < usb_max_eth_dev; i++) |
| 64 | if (&usb_eth[i].eth_dev == dev) |
| 65 | return 1; |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Given a USB device, ask each driver if it can support it, and attach it |
| 72 | * to the first driver that says 'yes' |
| 73 | */ |
| 74 | static void probe_valid_drivers(struct usb_device *dev) |
| 75 | { |
| 76 | int j; |
| 77 | |
| 78 | for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) { |
| 79 | if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev])) |
| 80 | continue; |
| 81 | /* |
| 82 | * ok, it is a supported eth device. Get info and fill it in |
| 83 | */ |
| 84 | if (prob_dev[j].get_info(dev, |
| 85 | &usb_eth[usb_max_eth_dev], |
| 86 | &usb_eth[usb_max_eth_dev].eth_dev)) { |
| 87 | /* found proper driver */ |
| 88 | /* register with networking stack */ |
| 89 | usb_max_eth_dev++; |
| 90 | |
| 91 | /* |
| 92 | * usb_max_eth_dev must be incremented prior to this |
| 93 | * call since eth_current_changed (internally called) |
| 94 | * relies on it |
| 95 | */ |
| 96 | eth_register(&usb_eth[usb_max_eth_dev - 1].eth_dev); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /******************************************************************************* |
| 103 | * scan the usb and reports device info |
| 104 | * to the user if mode = 1 |
| 105 | * returns current device or -1 if no |
| 106 | */ |
| 107 | int usb_host_eth_scan(int mode) |
| 108 | { |
| 109 | int i, old_async; |
| 110 | struct usb_device *dev; |
| 111 | |
| 112 | |
| 113 | if (mode == 1) |
| 114 | printf(" scanning bus for ethernet devices... "); |
| 115 | |
| 116 | old_async = usb_disable_asynch(1); /* asynch transfer not allowed */ |
| 117 | |
| 118 | for (i = 0; i < USB_MAX_ETH_DEV; i++) |
| 119 | memset(&usb_eth[i], 0, sizeof(usb_eth[i])); |
| 120 | |
| 121 | for (i = 0; prob_dev[i].probe; i++) { |
| 122 | if (prob_dev[i].before_probe) |
| 123 | prob_dev[i].before_probe(); |
| 124 | } |
| 125 | |
| 126 | usb_max_eth_dev = 0; |
| 127 | for (i = 0; i < USB_MAX_DEVICE; i++) { |
| 128 | dev = usb_get_dev_index(i); /* get device */ |
| 129 | debug("i=%d\n", i); |
| 130 | if (dev == NULL) |
| 131 | break; /* no more devices avaiable */ |
| 132 | |
| 133 | /* find valid usb_ether driver for this device, if any */ |
| 134 | probe_valid_drivers(dev); |
| 135 | |
| 136 | /* check limit */ |
| 137 | if (usb_max_eth_dev == USB_MAX_ETH_DEV) { |
| 138 | printf("max USB Ethernet Device reached: %d stopping\n", |
| 139 | usb_max_eth_dev); |
| 140 | break; |
| 141 | } |
| 142 | } /* for */ |
| 143 | |
| 144 | usb_disable_asynch(old_async); /* restore asynch value */ |
| 145 | printf("%d Ethernet Device(s) found\n", usb_max_eth_dev); |
| 146 | if (usb_max_eth_dev > 0) |
| 147 | return 0; |
| 148 | return -1; |
| 149 | } |
| 150 | |