2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 #include "usb_ether.h"
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);
33 struct usb_eth_prob_dev {
34 usb_eth_before_probe before_probe; /* optional */
36 usb_eth_get_info get_info;
39 /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
40 static const struct usb_eth_prob_dev prob_dev[] = {
41 #ifdef CONFIG_USB_ETHER_ASIX
43 .before_probe = asix_eth_before_probe,
44 .probe = asix_eth_probe,
45 .get_info = asix_eth_get_info,
48 #ifdef CONFIG_USB_ETHER_SMSC95XX
50 .before_probe = smsc95xx_eth_before_probe,
51 .probe = smsc95xx_eth_probe,
52 .get_info = smsc95xx_eth_get_info,
58 static int usb_max_eth_dev; /* number of highest available usb eth device */
59 static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
61 /*******************************************************************************
62 * tell if current ethernet device is a usb dongle
64 int is_eth_dev_on_usb_host(void)
67 struct eth_device *dev = eth_get_dev();
70 for (i = 0; i < usb_max_eth_dev; i++)
71 if (&usb_eth[i].eth_dev == dev)
78 * Given a USB device, ask each driver if it can support it, and attach it
79 * to the first driver that says 'yes'
81 static void probe_valid_drivers(struct usb_device *dev)
83 struct eth_device *eth;
86 for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
87 if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
90 * ok, it is a supported eth device. Get info and fill it in
92 eth = &usb_eth[usb_max_eth_dev].eth_dev;
93 if (prob_dev[j].get_info(dev,
94 &usb_eth[usb_max_eth_dev],
96 /* found proper driver */
97 /* register with networking stack */
101 * usb_max_eth_dev must be incremented prior to this
102 * call since eth_current_changed (internally called)
106 if (eth_write_hwaddr(eth, "usbeth",
107 usb_max_eth_dev - 1))
108 puts("Warning: failed to set MAC address\n");
114 /*******************************************************************************
115 * scan the usb and reports device info
116 * to the user if mode = 1
117 * returns current device or -1 if no
119 int usb_host_eth_scan(int mode)
122 struct usb_device *dev;
126 printf(" scanning bus for ethernet devices... ");
128 old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
130 for (i = 0; i < USB_MAX_ETH_DEV; i++)
131 memset(&usb_eth[i], 0, sizeof(usb_eth[i]));
133 for (i = 0; prob_dev[i].probe; i++) {
134 if (prob_dev[i].before_probe)
135 prob_dev[i].before_probe();
139 for (i = 0; i < USB_MAX_DEVICE; i++) {
140 dev = usb_get_dev_index(i); /* get device */
143 break; /* no more devices avaiable */
145 /* find valid usb_ether driver for this device, if any */
146 probe_valid_drivers(dev);
149 if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
150 printf("max USB Ethernet Device reached: %d stopping\n",
156 usb_disable_asynch(old_async); /* restore asynch value */
157 printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
158 if (usb_max_eth_dev > 0)