blob: 1dda54c2f116cffc8d6dc59c6bf8ac12128a3715 [file] [log] [blame]
Simon Glass89d48362011-02-16 11:14:33 -08001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass89d48362011-02-16 11:14:33 -08003 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass89d48362011-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 Glass9b70e002011-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
Gerhard Sittigdf4fb1c2014-03-08 19:46:14 +010033#ifdef CONFIG_USB_ETHER_MCS7830
34 {
35 .before_probe = mcs7830_eth_before_probe,
36 .probe = mcs7830_eth_probe,
37 .get_info = mcs7830_eth_get_info,
38 },
39#endif
Simon Glass291391b2011-06-13 16:13:09 -070040#ifdef CONFIG_USB_ETHER_SMSC95XX
41 {
42 .before_probe = smsc95xx_eth_before_probe,
43 .probe = smsc95xx_eth_probe,
44 .get_info = smsc95xx_eth_get_info,
45 },
46#endif
Simon Glass89d48362011-02-16 11:14:33 -080047 { }, /* END */
48};
49
50static int usb_max_eth_dev; /* number of highest available usb eth device */
51static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
52
53/*******************************************************************************
54 * tell if current ethernet device is a usb dongle
55 */
56int is_eth_dev_on_usb_host(void)
57{
58 int i;
59 struct eth_device *dev = eth_get_dev();
60
61 if (dev) {
62 for (i = 0; i < usb_max_eth_dev; i++)
63 if (&usb_eth[i].eth_dev == dev)
64 return 1;
65 }
66 return 0;
67}
68
69/*
70 * Given a USB device, ask each driver if it can support it, and attach it
71 * to the first driver that says 'yes'
72 */
73static void probe_valid_drivers(struct usb_device *dev)
74{
Simon Glass7616e782011-06-13 16:13:10 -070075 struct eth_device *eth;
Simon Glass89d48362011-02-16 11:14:33 -080076 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 */
Simon Glass7616e782011-06-13 16:13:10 -070084 eth = &usb_eth[usb_max_eth_dev].eth_dev;
Simon Glass89d48362011-02-16 11:14:33 -080085 if (prob_dev[j].get_info(dev,
86 &usb_eth[usb_max_eth_dev],
Simon Glass7616e782011-06-13 16:13:10 -070087 eth)) {
Simon Glass89d48362011-02-16 11:14:33 -080088 /* found proper driver */
89 /* register with networking stack */
90 usb_max_eth_dev++;
91
92 /*
93 * usb_max_eth_dev must be incremented prior to this
94 * call since eth_current_changed (internally called)
95 * relies on it
96 */
Simon Glass7616e782011-06-13 16:13:10 -070097 eth_register(eth);
98 if (eth_write_hwaddr(eth, "usbeth",
99 usb_max_eth_dev - 1))
100 puts("Warning: failed to set MAC address\n");
Simon Glass89d48362011-02-16 11:14:33 -0800101 break;
102 }
103 }
104 }
105
106/*******************************************************************************
107 * scan the usb and reports device info
108 * to the user if mode = 1
109 * returns current device or -1 if no
110 */
111int usb_host_eth_scan(int mode)
112{
113 int i, old_async;
114 struct usb_device *dev;
115
116
117 if (mode == 1)
Lucas Stach93c25822012-09-26 00:14:36 +0200118 printf(" scanning usb for ethernet devices... ");
Simon Glass89d48362011-02-16 11:14:33 -0800119
120 old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
121
Vincent Palatine7e982d2012-01-09 08:32:36 +0000122 /* unregister a previously detected device */
123 for (i = 0; i < usb_max_eth_dev; i++)
124 eth_unregister(&usb_eth[i].eth_dev);
125
126 memset(usb_eth, 0, sizeof(usb_eth));
Simon Glass89d48362011-02-16 11:14:33 -0800127
128 for (i = 0; prob_dev[i].probe; i++) {
129 if (prob_dev[i].before_probe)
130 prob_dev[i].before_probe();
131 }
132
133 usb_max_eth_dev = 0;
134 for (i = 0; i < USB_MAX_DEVICE; i++) {
135 dev = usb_get_dev_index(i); /* get device */
136 debug("i=%d\n", i);
137 if (dev == NULL)
Peter Meerwaldd1f862f2012-02-08 05:31:54 +0000138 break; /* no more devices available */
Simon Glass89d48362011-02-16 11:14:33 -0800139
140 /* find valid usb_ether driver for this device, if any */
141 probe_valid_drivers(dev);
142
143 /* check limit */
144 if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
145 printf("max USB Ethernet Device reached: %d stopping\n",
146 usb_max_eth_dev);
147 break;
148 }
149 } /* for */
150
151 usb_disable_asynch(old_async); /* restore asynch value */
152 printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
153 if (usb_max_eth_dev > 0)
154 return 0;
155 return -1;
156}