blob: 2c4126be3601dcf55419e1934e3a56a71a609b4a [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>
8#include <usb.h>
9
10#include "usb_ether.h"
11
12typedef void (*usb_eth_before_probe)(void);
13typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
14 struct ueth_data *ss);
15typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
16 struct eth_device *dev_desc);
17
18struct 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 */
25static const struct usb_eth_prob_dev prob_dev[] = {
Simon Glass246d6932011-02-16 11:14:34 -080026#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
Simon Glass246c1192011-06-13 16:13:09 -070033#ifdef CONFIG_USB_ETHER_SMSC95XX
34 {
35 .before_probe = smsc95xx_eth_before_probe,
36 .probe = smsc95xx_eth_probe,
37 .get_info = smsc95xx_eth_get_info,
38 },
39#endif
Simon Glass1e9961d2011-02-16 11:14:33 -080040 { }, /* END */
41};
42
43static int usb_max_eth_dev; /* number of highest available usb eth device */
44static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
45
46/*******************************************************************************
47 * tell if current ethernet device is a usb dongle
48 */
49int is_eth_dev_on_usb_host(void)
50{
51 int i;
52 struct eth_device *dev = eth_get_dev();
53
54 if (dev) {
55 for (i = 0; i < usb_max_eth_dev; i++)
56 if (&usb_eth[i].eth_dev == dev)
57 return 1;
58 }
59 return 0;
60}
61
62/*
63 * Given a USB device, ask each driver if it can support it, and attach it
64 * to the first driver that says 'yes'
65 */
66static void probe_valid_drivers(struct usb_device *dev)
67{
Simon Glass62b36c92011-06-13 16:13:10 -070068 struct eth_device *eth;
Simon Glass1e9961d2011-02-16 11:14:33 -080069 int j;
70
71 for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
72 if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
73 continue;
74 /*
75 * ok, it is a supported eth device. Get info and fill it in
76 */
Simon Glass62b36c92011-06-13 16:13:10 -070077 eth = &usb_eth[usb_max_eth_dev].eth_dev;
Simon Glass1e9961d2011-02-16 11:14:33 -080078 if (prob_dev[j].get_info(dev,
79 &usb_eth[usb_max_eth_dev],
Simon Glass62b36c92011-06-13 16:13:10 -070080 eth)) {
Simon Glass1e9961d2011-02-16 11:14:33 -080081 /* found proper driver */
82 /* register with networking stack */
83 usb_max_eth_dev++;
84
85 /*
86 * usb_max_eth_dev must be incremented prior to this
87 * call since eth_current_changed (internally called)
88 * relies on it
89 */
Simon Glass62b36c92011-06-13 16:13:10 -070090 eth_register(eth);
91 if (eth_write_hwaddr(eth, "usbeth",
92 usb_max_eth_dev - 1))
93 puts("Warning: failed to set MAC address\n");
Simon Glass1e9961d2011-02-16 11:14:33 -080094 break;
95 }
96 }
97 }
98
99/*******************************************************************************
100 * scan the usb and reports device info
101 * to the user if mode = 1
102 * returns current device or -1 if no
103 */
104int usb_host_eth_scan(int mode)
105{
106 int i, old_async;
107 struct usb_device *dev;
108
109
110 if (mode == 1)
Lucas Stache6d33452012-09-26 00:14:36 +0200111 printf(" scanning usb for ethernet devices... ");
Simon Glass1e9961d2011-02-16 11:14:33 -0800112
113 old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
114
Vincent Palatin451be502012-01-09 08:32:36 +0000115 /* unregister a previously detected device */
116 for (i = 0; i < usb_max_eth_dev; i++)
117 eth_unregister(&usb_eth[i].eth_dev);
118
119 memset(usb_eth, 0, sizeof(usb_eth));
Simon Glass1e9961d2011-02-16 11:14:33 -0800120
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)
Peter Meerwalde124c7b2012-02-08 05:31:54 +0000131 break; /* no more devices available */
Simon Glass1e9961d2011-02-16 11:14:33 -0800132
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}