blob: 62d20f0db989664f69643765701642ea9edcbce5 [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 Glasse96ba562015-07-07 20:53:35 -06009#include <errno.h>
Simon Glass5b026f92015-07-06 16:47:50 -060010#include <malloc.h>
Simon Glass1e9961d2011-02-16 11:14:33 -080011#include <usb.h>
Simon Glass33b66e82015-03-25 12:22:43 -060012#include <dm/device-internal.h>
Simon Glass1e9961d2011-02-16 11:14:33 -080013
14#include "usb_ether.h"
15
Simon Glass5b026f92015-07-06 16:47:50 -060016#ifdef CONFIG_DM_ETH
17
18#define USB_BULK_RECV_TIMEOUT 500
19
20int usb_ether_register(struct udevice *dev, struct ueth_data *ueth, int rxsize)
21{
Simon Glassde44acf2015-09-28 23:32:01 -060022 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass5b026f92015-07-06 16:47:50 -060023 struct usb_interface_descriptor *iface_desc;
24 bool ep_in_found = false, ep_out_found = false;
25 struct usb_interface *iface;
26 const int ifnum = 0; /* Always use interface 0 */
27 int ret, i;
28
29 iface = &udev->config.if_desc[ifnum];
30 iface_desc = &udev->config.if_desc[ifnum].desc;
31
32 /* Initialize the ueth_data structure with some useful info */
33 ueth->ifnum = ifnum;
34 ueth->subclass = iface_desc->bInterfaceSubClass;
35 ueth->protocol = iface_desc->bInterfaceProtocol;
36
37 /*
38 * We are expecting a minimum of 3 endpoints - in, out (bulk), and int.
39 * We will ignore any others.
40 */
41 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
42 int ep_addr = iface->ep_desc[i].bEndpointAddress;
43
44 /* is it an BULK endpoint? */
45 if ((iface->ep_desc[i].bmAttributes &
46 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
47 if (ep_addr & USB_DIR_IN && !ep_in_found) {
48 ueth->ep_in = ep_addr &
49 USB_ENDPOINT_NUMBER_MASK;
50 ep_in_found = true;
51 } else if (!(ep_addr & USB_DIR_IN) && !ep_out_found) {
52 ueth->ep_out = ep_addr &
53 USB_ENDPOINT_NUMBER_MASK;
54 ep_out_found = true;
55 }
56 }
57
58 /* is it an interrupt endpoint? */
59 if ((iface->ep_desc[i].bmAttributes &
60 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
61 ueth->ep_int = iface->ep_desc[i].bEndpointAddress &
62 USB_ENDPOINT_NUMBER_MASK;
63 ueth->irqinterval = iface->ep_desc[i].bInterval;
64 }
65 }
66 debug("Endpoints In %d Out %d Int %d\n", ueth->ep_in, ueth->ep_out,
67 ueth->ep_int);
68
69 /* Do some basic sanity checks, and bail if we find a problem */
70 if (!ueth->ep_in || !ueth->ep_out || !ueth->ep_int) {
71 debug("%s: %s: Cannot find endpoints\n", __func__, dev->name);
72 return -ENXIO;
73 }
74
75 ueth->rxsize = rxsize;
76 ueth->rxbuf = memalign(rxsize, ARCH_DMA_MINALIGN);
77 if (!ueth->rxbuf)
78 return -ENOMEM;
79
80 ret = usb_set_interface(udev, iface_desc->bInterfaceNumber, ifnum);
81 if (ret) {
82 debug("%s: %s: Cannot set interface: %d\n", __func__, dev->name,
83 ret);
84 return ret;
85 }
86 ueth->pusb_dev = udev;
87
88 return 0;
89}
90
91int usb_ether_deregister(struct ueth_data *ueth)
92{
93 return 0;
94}
95
96int usb_ether_receive(struct ueth_data *ueth, int rxsize)
97{
98 int actual_len;
99 int ret;
100
101 if (rxsize > ueth->rxsize)
102 return -EINVAL;
103 ret = usb_bulk_msg(ueth->pusb_dev,
104 usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
105 ueth->rxbuf, rxsize, &actual_len,
106 USB_BULK_RECV_TIMEOUT);
107 debug("Rx: len = %u, actual = %u, err = %d\n", rxsize, actual_len, ret);
108 if (ret) {
109 printf("Rx: failed to receive: %d\n", ret);
110 return ret;
111 }
112 if (actual_len > rxsize) {
113 debug("Rx: received too many bytes %d\n", actual_len);
114 return -ENOSPC;
115 }
116 ueth->rxlen = actual_len;
117 ueth->rxptr = 0;
118
119 return actual_len ? 0 : -EAGAIN;
120}
121
122void usb_ether_advance_rxbuf(struct ueth_data *ueth, int num_bytes)
123{
124 ueth->rxptr += num_bytes;
125 if (num_bytes < 0 || ueth->rxptr >= ueth->rxlen)
126 ueth->rxlen = 0;
127}
128
129int usb_ether_get_rx_bytes(struct ueth_data *ueth, uint8_t **ptrp)
130{
131 if (!ueth->rxlen)
132 return 0;
133
134 *ptrp = &ueth->rxbuf[ueth->rxptr];
135
136 return ueth->rxlen - ueth->rxptr;
137}
138
139#else
140
Simon Glass1e9961d2011-02-16 11:14:33 -0800141typedef void (*usb_eth_before_probe)(void);
142typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
143 struct ueth_data *ss);
144typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
145 struct eth_device *dev_desc);
146
147struct usb_eth_prob_dev {
148 usb_eth_before_probe before_probe; /* optional */
149 usb_eth_probe probe;
150 usb_eth_get_info get_info;
151};
152
153/* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
154static const struct usb_eth_prob_dev prob_dev[] = {
Simon Glass246d6932011-02-16 11:14:34 -0800155#ifdef CONFIG_USB_ETHER_ASIX
156 {
157 .before_probe = asix_eth_before_probe,
158 .probe = asix_eth_probe,
159 .get_info = asix_eth_get_info,
160 },
161#endif
Rene Griessl979beee2014-11-07 16:53:48 +0100162#ifdef CONFIG_USB_ETHER_ASIX88179
163 {
164 .before_probe = ax88179_eth_before_probe,
165 .probe = ax88179_eth_probe,
166 .get_info = ax88179_eth_get_info,
167 },
168#endif
Gerhard Sittig2385df62014-03-08 19:46:14 +0100169#ifdef CONFIG_USB_ETHER_MCS7830
170 {
171 .before_probe = mcs7830_eth_before_probe,
172 .probe = mcs7830_eth_probe,
173 .get_info = mcs7830_eth_get_info,
174 },
175#endif
Simon Glass246c1192011-06-13 16:13:09 -0700176#ifdef CONFIG_USB_ETHER_SMSC95XX
177 {
178 .before_probe = smsc95xx_eth_before_probe,
179 .probe = smsc95xx_eth_probe,
180 .get_info = smsc95xx_eth_get_info,
181 },
182#endif
Simon Glass1e9961d2011-02-16 11:14:33 -0800183 { }, /* END */
184};
185
186static int usb_max_eth_dev; /* number of highest available usb eth device */
187static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
188
189/*******************************************************************************
190 * tell if current ethernet device is a usb dongle
191 */
192int is_eth_dev_on_usb_host(void)
193{
194 int i;
195 struct eth_device *dev = eth_get_dev();
196
197 if (dev) {
198 for (i = 0; i < usb_max_eth_dev; i++)
199 if (&usb_eth[i].eth_dev == dev)
200 return 1;
201 }
202 return 0;
203}
204
205/*
206 * Given a USB device, ask each driver if it can support it, and attach it
207 * to the first driver that says 'yes'
208 */
209static void probe_valid_drivers(struct usb_device *dev)
210{
Simon Glass62b36c92011-06-13 16:13:10 -0700211 struct eth_device *eth;
Simon Glass1e9961d2011-02-16 11:14:33 -0800212 int j;
213
214 for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
215 if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
216 continue;
217 /*
218 * ok, it is a supported eth device. Get info and fill it in
219 */
Simon Glass62b36c92011-06-13 16:13:10 -0700220 eth = &usb_eth[usb_max_eth_dev].eth_dev;
Simon Glass1e9961d2011-02-16 11:14:33 -0800221 if (prob_dev[j].get_info(dev,
222 &usb_eth[usb_max_eth_dev],
Simon Glass62b36c92011-06-13 16:13:10 -0700223 eth)) {
Simon Glass1e9961d2011-02-16 11:14:33 -0800224 /* found proper driver */
225 /* register with networking stack */
226 usb_max_eth_dev++;
227
228 /*
229 * usb_max_eth_dev must be incremented prior to this
230 * call since eth_current_changed (internally called)
231 * relies on it
232 */
Simon Glass62b36c92011-06-13 16:13:10 -0700233 eth_register(eth);
234 if (eth_write_hwaddr(eth, "usbeth",
235 usb_max_eth_dev - 1))
236 puts("Warning: failed to set MAC address\n");
Simon Glass1e9961d2011-02-16 11:14:33 -0800237 break;
238 }
239 }
240 }
241
242/*******************************************************************************
243 * scan the usb and reports device info
244 * to the user if mode = 1
245 * returns current device or -1 if no
246 */
247int usb_host_eth_scan(int mode)
248{
249 int i, old_async;
Simon Glass1e9961d2011-02-16 11:14:33 -0800250
251 if (mode == 1)
Lucas Stache6d33452012-09-26 00:14:36 +0200252 printf(" scanning usb for ethernet devices... ");
Simon Glass1e9961d2011-02-16 11:14:33 -0800253
254 old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
255
Vincent Palatin451be502012-01-09 08:32:36 +0000256 /* unregister a previously detected device */
257 for (i = 0; i < usb_max_eth_dev; i++)
258 eth_unregister(&usb_eth[i].eth_dev);
259
260 memset(usb_eth, 0, sizeof(usb_eth));
Simon Glass1e9961d2011-02-16 11:14:33 -0800261
262 for (i = 0; prob_dev[i].probe; i++) {
263 if (prob_dev[i].before_probe)
264 prob_dev[i].before_probe();
265 }
266
267 usb_max_eth_dev = 0;
Simon Glass33b66e82015-03-25 12:22:43 -0600268#ifdef CONFIG_DM_USB
269 /*
Simon Glass46952fb2015-07-06 16:47:51 -0600270 * TODO: We should add U_BOOT_USB_DEVICE() declarations to each USB
271 * Ethernet driver and then most of this file can be removed.
Simon Glass33b66e82015-03-25 12:22:43 -0600272 */
273 struct udevice *bus;
274 struct uclass *uc;
275 int ret;
276
277 ret = uclass_get(UCLASS_USB, &uc);
278 if (ret)
279 return ret;
280 uclass_foreach_dev(bus, uc) {
281 for (i = 0; i < USB_MAX_DEVICE; i++) {
282 struct usb_device *dev;
283
284 dev = usb_get_dev_index(bus, i); /* get device */
285 debug("i=%d, %s\n", i, dev ? dev->dev->name : "(done)");
286 if (!dev)
287 break; /* no more devices available */
288
289 /*
290 * find valid usb_ether driver for this device,
291 * if any
292 */
293 probe_valid_drivers(dev);
294
295 /* check limit */
296 if (usb_max_eth_dev == USB_MAX_ETH_DEV)
297 break;
298 } /* for */
299 }
300#else
Simon Glass1e9961d2011-02-16 11:14:33 -0800301 for (i = 0; i < USB_MAX_DEVICE; i++) {
Simon Glass33b66e82015-03-25 12:22:43 -0600302 struct usb_device *dev;
303
Simon Glass1e9961d2011-02-16 11:14:33 -0800304 dev = usb_get_dev_index(i); /* get device */
305 debug("i=%d\n", i);
Simon Glass33b66e82015-03-25 12:22:43 -0600306 if (!dev)
Peter Meerwalde124c7b2012-02-08 05:31:54 +0000307 break; /* no more devices available */
Simon Glass1e9961d2011-02-16 11:14:33 -0800308
309 /* find valid usb_ether driver for this device, if any */
310 probe_valid_drivers(dev);
311
312 /* check limit */
Simon Glass33b66e82015-03-25 12:22:43 -0600313 if (usb_max_eth_dev == USB_MAX_ETH_DEV)
Simon Glass1e9961d2011-02-16 11:14:33 -0800314 break;
Simon Glass1e9961d2011-02-16 11:14:33 -0800315 } /* for */
Simon Glass33b66e82015-03-25 12:22:43 -0600316#endif
317 if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
318 printf("max USB Ethernet Device reached: %d stopping\n",
319 usb_max_eth_dev);
320 }
Simon Glass1e9961d2011-02-16 11:14:33 -0800321 usb_disable_asynch(old_async); /* restore asynch value */
322 printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
323 if (usb_max_eth_dev > 0)
324 return 0;
325 return -1;
326}
Simon Glass5b026f92015-07-06 16:47:50 -0600327#endif