usb_ether.c 4.75 KB
Newer Older
1 2 3
/*
 * Copyright (c) 2011 The Chromium OS Authors.
 *
4
 * SPDX-License-Identifier:	GPL-2.0+
5 6 7
 */

#include <common.h>
8
#include <dm.h>
9
#include <usb.h>
10
#include <dm/device-internal.h>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

#include "usb_ether.h"

typedef void (*usb_eth_before_probe)(void);
typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
			struct ueth_data *ss);
typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
			struct eth_device *dev_desc);

struct usb_eth_prob_dev {
	usb_eth_before_probe	before_probe; /* optional */
	usb_eth_probe			probe;
	usb_eth_get_info		get_info;
};

/* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
static const struct usb_eth_prob_dev prob_dev[] = {
28 29 30 31 32
#ifdef CONFIG_USB_ETHER_ASIX
	{
		.before_probe = asix_eth_before_probe,
		.probe = asix_eth_probe,
		.get_info = asix_eth_get_info,
33 34 35 36 37 38 39
	},
#endif
#ifdef CONFIG_USB_ETHER_ASIX88179
	{
		.before_probe = ax88179_eth_before_probe,
		.probe = ax88179_eth_probe,
		.get_info = ax88179_eth_get_info,
40
	},
41
#endif
42 43 44 45 46 47 48
#ifdef CONFIG_USB_ETHER_MCS7830
	{
		.before_probe = mcs7830_eth_before_probe,
		.probe = mcs7830_eth_probe,
		.get_info = mcs7830_eth_get_info,
	},
#endif
49 50 51 52 53 54
#ifdef CONFIG_USB_ETHER_SMSC95XX
	{
		.before_probe = smsc95xx_eth_before_probe,
		.probe = smsc95xx_eth_probe,
		.get_info = smsc95xx_eth_get_info,
	},
55
#endif
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	{ },		/* END */
};

static int usb_max_eth_dev; /* number of highest available usb eth device */
static struct ueth_data usb_eth[USB_MAX_ETH_DEV];

/*******************************************************************************
 * tell if current ethernet device is a usb dongle
 */
int is_eth_dev_on_usb_host(void)
{
	int i;
	struct eth_device *dev = eth_get_dev();

	if (dev) {
		for (i = 0; i < usb_max_eth_dev; i++)
			if (&usb_eth[i].eth_dev == dev)
				return 1;
	}
	return 0;
}

/*
 * Given a USB device, ask each driver if it can support it, and attach it
 * to the first driver that says 'yes'
 */
static void probe_valid_drivers(struct usb_device *dev)
{
84
	struct eth_device *eth;
85 86 87 88 89 90 91 92
	int j;

	for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
		if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
			continue;
		/*
		 * ok, it is a supported eth device. Get info and fill it in
		 */
93
		eth = &usb_eth[usb_max_eth_dev].eth_dev;
94 95
		if (prob_dev[j].get_info(dev,
			&usb_eth[usb_max_eth_dev],
96
			eth)) {
97 98 99 100 101 102 103 104 105
			/* found proper driver */
			/* register with networking stack */
			usb_max_eth_dev++;

			/*
			 * usb_max_eth_dev must be incremented prior to this
			 * call since eth_current_changed (internally called)
			 * relies on it
			 */
106 107 108 109
			eth_register(eth);
			if (eth_write_hwaddr(eth, "usbeth",
					usb_max_eth_dev - 1))
				puts("Warning: failed to set MAC address\n");
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
			break;
			}
		}
	}

/*******************************************************************************
 * scan the usb and reports device info
 * to the user if mode = 1
 * returns current device or -1 if no
 */
int usb_host_eth_scan(int mode)
{
	int i, old_async;

	if (mode == 1)
125
		printf("       scanning usb for ethernet devices... ");
126 127 128

	old_async = usb_disable_asynch(1); /* asynch transfer not allowed */

129 130 131 132 133
	/* unregister a previously detected device */
	for (i = 0; i < usb_max_eth_dev; i++)
		eth_unregister(&usb_eth[i].eth_dev);

	memset(usb_eth, 0, sizeof(usb_eth));
134 135 136 137 138 139 140

	for (i = 0; prob_dev[i].probe; i++) {
		if (prob_dev[i].before_probe)
			prob_dev[i].before_probe();
	}

	usb_max_eth_dev = 0;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#ifdef CONFIG_DM_USB
	/*
	 * TODO: We should add USB_DEVICE() declarations to each USB ethernet
	 * driver and then most of this file can be removed.
	 */
	struct udevice *bus;
	struct uclass *uc;
	int ret;

	ret = uclass_get(UCLASS_USB, &uc);
	if (ret)
		return ret;
	uclass_foreach_dev(bus, uc) {
		for (i = 0; i < USB_MAX_DEVICE; i++) {
			struct usb_device *dev;

			dev = usb_get_dev_index(bus, i); /* get device */
			debug("i=%d, %s\n", i, dev ? dev->dev->name : "(done)");
			if (!dev)
				break; /* no more devices available */

			/*
			 * find valid usb_ether driver for this device,
			 * if any
			 */
			probe_valid_drivers(dev);

			/* check limit */
			if (usb_max_eth_dev == USB_MAX_ETH_DEV)
				break;
		} /* for */
	}
#else
174
	for (i = 0; i < USB_MAX_DEVICE; i++) {
175 176
		struct usb_device *dev;

177 178
		dev = usb_get_dev_index(i); /* get device */
		debug("i=%d\n", i);
179
		if (!dev)
Peter Meerwald's avatar
Peter Meerwald committed
180
			break; /* no more devices available */
181 182 183 184 185

		/* find valid usb_ether driver for this device, if any */
		probe_valid_drivers(dev);

		/* check limit */
186
		if (usb_max_eth_dev == USB_MAX_ETH_DEV)
187 188
			break;
	} /* for */
189 190 191 192 193
#endif
	if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
		printf("max USB Ethernet Device reached: %d stopping\n",
		       usb_max_eth_dev);
	}
194 195 196 197 198 199
	usb_disable_asynch(old_async); /* restore asynch value */
	printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
	if (usb_max_eth_dev > 0)
		return 0;
	return -1;
}