blob: c72b7e47c488bf31cd837a8b6964f5375d6f59f8 [file] [log] [blame]
Simon Glass1e9961d2011-02-16 11:14:33 -08001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass1e9961d2011-02-16 11:14:33 -08003 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass1e9961d2011-02-16 11:14:33 -08005 */
6
7#include <common.h>
Simon Glass33b66e82015-03-25 12:22:43 -06008#include <dm.h>
Simon Glass1e9961d2011-02-16 11:14:33 -08009#include <usb.h>
Simon Glass33b66e82015-03-25 12:22:43 -060010#include <dm/device-internal.h>
Simon Glass1e9961d2011-02-16 11:14:33 -080011
12#include "usb_ether.h"
13
14typedef void (*usb_eth_before_probe)(void);
15typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
16 struct ueth_data *ss);
17typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
18 struct eth_device *dev_desc);
19
20struct usb_eth_prob_dev {
21 usb_eth_before_probe before_probe; /* optional */
22 usb_eth_probe probe;
23 usb_eth_get_info get_info;
24};
25
26/* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
27static const struct usb_eth_prob_dev prob_dev[] = {
Simon Glass246d6932011-02-16 11:14:34 -080028#ifdef CONFIG_USB_ETHER_ASIX
29 {
30 .before_probe = asix_eth_before_probe,
31 .probe = asix_eth_probe,
32 .get_info = asix_eth_get_info,
33 },
34#endif
Rene Griessl979beee2014-11-07 16:53:48 +010035#ifdef CONFIG_USB_ETHER_ASIX88179
36 {
37 .before_probe = ax88179_eth_before_probe,
38 .probe = ax88179_eth_probe,
39 .get_info = ax88179_eth_get_info,
40 },
41#endif
Gerhard Sittig2385df62014-03-08 19:46:14 +010042#ifdef CONFIG_USB_ETHER_MCS7830
43 {
44 .before_probe = mcs7830_eth_before_probe,
45 .probe = mcs7830_eth_probe,
46 .get_info = mcs7830_eth_get_info,
47 },
48#endif
Simon Glass246c1192011-06-13 16:13:09 -070049#ifdef CONFIG_USB_ETHER_SMSC95XX
50 {
51 .before_probe = smsc95xx_eth_before_probe,
52 .probe = smsc95xx_eth_probe,
53 .get_info = smsc95xx_eth_get_info,
54 },
55#endif
Simon Glass1e9961d2011-02-16 11:14:33 -080056 { }, /* END */
57};
58
59static int usb_max_eth_dev; /* number of highest available usb eth device */
60static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
61
62/*******************************************************************************
63 * tell if current ethernet device is a usb dongle
64 */
65int is_eth_dev_on_usb_host(void)
66{
67 int i;
68 struct eth_device *dev = eth_get_dev();
69
70 if (dev) {
71 for (i = 0; i < usb_max_eth_dev; i++)
72 if (&usb_eth[i].eth_dev == dev)
73 return 1;
74 }
75 return 0;
76}
77
78/*
79 * Given a USB device, ask each driver if it can support it, and attach it
80 * to the first driver that says 'yes'
81 */
82static void probe_valid_drivers(struct usb_device *dev)
83{
Simon Glass62b36c92011-06-13 16:13:10 -070084 struct eth_device *eth;
Simon Glass1e9961d2011-02-16 11:14:33 -080085 int j;
86
87 for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
88 if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
89 continue;
90 /*
91 * ok, it is a supported eth device. Get info and fill it in
92 */
Simon Glass62b36c92011-06-13 16:13:10 -070093 eth = &usb_eth[usb_max_eth_dev].eth_dev;
Simon Glass1e9961d2011-02-16 11:14:33 -080094 if (prob_dev[j].get_info(dev,
95 &usb_eth[usb_max_eth_dev],
Simon Glass62b36c92011-06-13 16:13:10 -070096 eth)) {
Simon Glass1e9961d2011-02-16 11:14:33 -080097 /* found proper driver */
98 /* register with networking stack */
99 usb_max_eth_dev++;
100
101 /*
102 * usb_max_eth_dev must be incremented prior to this
103 * call since eth_current_changed (internally called)
104 * relies on it
105 */
Simon Glass62b36c92011-06-13 16:13:10 -0700106 eth_register(eth);
107 if (eth_write_hwaddr(eth, "usbeth",
108 usb_max_eth_dev - 1))
109 puts("Warning: failed to set MAC address\n");
Simon Glass1e9961d2011-02-16 11:14:33 -0800110 break;
111 }
112 }
113 }
114
115/*******************************************************************************
116 * scan the usb and reports device info
117 * to the user if mode = 1
118 * returns current device or -1 if no
119 */
120int usb_host_eth_scan(int mode)
121{
122 int i, old_async;
Simon Glass1e9961d2011-02-16 11:14:33 -0800123
124 if (mode == 1)
Lucas Stache6d33452012-09-26 00:14:36 +0200125 printf(" scanning usb for ethernet devices... ");
Simon Glass1e9961d2011-02-16 11:14:33 -0800126
127 old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
128
Vincent Palatin451be502012-01-09 08:32:36 +0000129 /* 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 Glass1e9961d2011-02-16 11:14:33 -0800134
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;
Simon Glass33b66e82015-03-25 12:22:43 -0600141#ifdef CONFIG_DM_USB
142 /*
143 * TODO: We should add USB_DEVICE() declarations to each USB ethernet
144 * driver and then most of this file can be removed.
145 */
146 struct udevice *bus;
147 struct uclass *uc;
148 int ret;
149
150 ret = uclass_get(UCLASS_USB, &uc);
151 if (ret)
152 return ret;
153 uclass_foreach_dev(bus, uc) {
154 for (i = 0; i < USB_MAX_DEVICE; i++) {
155 struct usb_device *dev;
156
157 dev = usb_get_dev_index(bus, i); /* get device */
158 debug("i=%d, %s\n", i, dev ? dev->dev->name : "(done)");
159 if (!dev)
160 break; /* no more devices available */
161
162 /*
163 * find valid usb_ether driver for this device,
164 * if any
165 */
166 probe_valid_drivers(dev);
167
168 /* check limit */
169 if (usb_max_eth_dev == USB_MAX_ETH_DEV)
170 break;
171 } /* for */
172 }
173#else
Simon Glass1e9961d2011-02-16 11:14:33 -0800174 for (i = 0; i < USB_MAX_DEVICE; i++) {
Simon Glass33b66e82015-03-25 12:22:43 -0600175 struct usb_device *dev;
176
Simon Glass1e9961d2011-02-16 11:14:33 -0800177 dev = usb_get_dev_index(i); /* get device */
178 debug("i=%d\n", i);
Simon Glass33b66e82015-03-25 12:22:43 -0600179 if (!dev)
Peter Meerwalde124c7b2012-02-08 05:31:54 +0000180 break; /* no more devices available */
Simon Glass1e9961d2011-02-16 11:14:33 -0800181
182 /* find valid usb_ether driver for this device, if any */
183 probe_valid_drivers(dev);
184
185 /* check limit */
Simon Glass33b66e82015-03-25 12:22:43 -0600186 if (usb_max_eth_dev == USB_MAX_ETH_DEV)
Simon Glass1e9961d2011-02-16 11:14:33 -0800187 break;
Simon Glass1e9961d2011-02-16 11:14:33 -0800188 } /* for */
Simon Glass33b66e82015-03-25 12:22:43 -0600189#endif
190 if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
191 printf("max USB Ethernet Device reached: %d stopping\n",
192 usb_max_eth_dev);
193 }
Simon Glass1e9961d2011-02-16 11:14:33 -0800194 usb_disable_asynch(old_async); /* restore asynch value */
195 printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
196 if (usb_max_eth_dev > 0)
197 return 0;
198 return -1;
199}