2 * Copyright (c) 2011 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
14 #include "usb_ether.h"
18 #define USB_BULK_RECV_TIMEOUT 500
20 int usb_ether_register(struct udevice *dev, struct ueth_data *ueth, int rxsize)
22 struct usb_device *udev = dev_get_parent_priv(dev);
23 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 */
29 iface = &udev->config.if_desc[ifnum];
30 iface_desc = &udev->config.if_desc[ifnum].desc;
32 /* Initialize the ueth_data structure with some useful info */
34 ueth->subclass = iface_desc->bInterfaceSubClass;
35 ueth->protocol = iface_desc->bInterfaceProtocol;
38 * We are expecting a minimum of 3 endpoints - in, out (bulk), and int.
39 * We will ignore any others.
41 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
42 int ep_addr = iface->ep_desc[i].bEndpointAddress;
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;
51 } else if (!(ep_addr & USB_DIR_IN) && !ep_out_found) {
52 ueth->ep_out = ep_addr &
53 USB_ENDPOINT_NUMBER_MASK;
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;
66 debug("Endpoints In %d Out %d Int %d\n", ueth->ep_in, ueth->ep_out,
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);
75 ueth->rxsize = rxsize;
76 ueth->rxbuf = memalign(ARCH_DMA_MINALIGN, rxsize);
80 ret = usb_set_interface(udev, iface_desc->bInterfaceNumber, ifnum);
82 debug("%s: %s: Cannot set interface: %d\n", __func__, dev->name,
86 ueth->pusb_dev = udev;
91 int usb_ether_deregister(struct ueth_data *ueth)
96 int usb_ether_receive(struct ueth_data *ueth, int rxsize)
101 if (rxsize > ueth->rxsize)
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);
109 printf("Rx: failed to receive: %d\n", ret);
112 if (actual_len > rxsize) {
113 debug("Rx: received too many bytes %d\n", actual_len);
116 ueth->rxlen = actual_len;
119 return actual_len ? 0 : -EAGAIN;
122 void usb_ether_advance_rxbuf(struct ueth_data *ueth, int num_bytes)
124 ueth->rxptr += num_bytes;
125 if (num_bytes < 0 || ueth->rxptr >= ueth->rxlen)
129 int usb_ether_get_rx_bytes(struct ueth_data *ueth, uint8_t **ptrp)
134 *ptrp = &ueth->rxbuf[ueth->rxptr];
136 return ueth->rxlen - ueth->rxptr;
141 typedef void (*usb_eth_before_probe)(void);
142 typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
143 struct ueth_data *ss);
144 typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
145 struct eth_device *dev_desc);
147 struct usb_eth_prob_dev {
148 usb_eth_before_probe before_probe; /* optional */
150 usb_eth_get_info get_info;
153 /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
154 static const struct usb_eth_prob_dev prob_dev[] = {
155 #ifdef CONFIG_USB_ETHER_ASIX
157 .before_probe = asix_eth_before_probe,
158 .probe = asix_eth_probe,
159 .get_info = asix_eth_get_info,
162 #ifdef CONFIG_USB_ETHER_ASIX88179
164 .before_probe = ax88179_eth_before_probe,
165 .probe = ax88179_eth_probe,
166 .get_info = ax88179_eth_get_info,
169 #ifdef CONFIG_USB_ETHER_MCS7830
171 .before_probe = mcs7830_eth_before_probe,
172 .probe = mcs7830_eth_probe,
173 .get_info = mcs7830_eth_get_info,
176 #ifdef CONFIG_USB_ETHER_SMSC95XX
178 .before_probe = smsc95xx_eth_before_probe,
179 .probe = smsc95xx_eth_probe,
180 .get_info = smsc95xx_eth_get_info,
183 #ifdef CONFIG_USB_ETHER_RTL8152
185 .before_probe = r8152_eth_before_probe,
186 .probe = r8152_eth_probe,
187 .get_info = r8152_eth_get_info,
193 static int usb_max_eth_dev; /* number of highest available usb eth device */
194 static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
196 /*******************************************************************************
197 * tell if current ethernet device is a usb dongle
199 int is_eth_dev_on_usb_host(void)
202 struct eth_device *dev = eth_get_dev();
205 for (i = 0; i < usb_max_eth_dev; i++)
206 if (&usb_eth[i].eth_dev == dev)
213 * Given a USB device, ask each driver if it can support it, and attach it
214 * to the first driver that says 'yes'
216 static void probe_valid_drivers(struct usb_device *dev)
218 struct eth_device *eth;
221 for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
222 if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
225 * ok, it is a supported eth device. Get info and fill it in
227 eth = &usb_eth[usb_max_eth_dev].eth_dev;
228 if (prob_dev[j].get_info(dev,
229 &usb_eth[usb_max_eth_dev],
231 /* found proper driver */
232 /* register with networking stack */
236 * usb_max_eth_dev must be incremented prior to this
237 * call since eth_current_changed (internally called)
241 if (eth_write_hwaddr(eth, "usbeth",
242 usb_max_eth_dev - 1))
243 puts("Warning: failed to set MAC address\n");
249 /*******************************************************************************
250 * scan the usb and reports device info
251 * to the user if mode = 1
252 * returns current device or -1 if no
254 int usb_host_eth_scan(int mode)
259 printf(" scanning usb for ethernet devices... ");
261 old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
263 /* unregister a previously detected device */
264 for (i = 0; i < usb_max_eth_dev; i++)
265 eth_unregister(&usb_eth[i].eth_dev);
267 memset(usb_eth, 0, sizeof(usb_eth));
269 for (i = 0; prob_dev[i].probe; i++) {
270 if (prob_dev[i].before_probe)
271 prob_dev[i].before_probe();
277 * TODO: We should add U_BOOT_USB_DEVICE() declarations to each USB
278 * Ethernet driver and then most of this file can be removed.
284 ret = uclass_get(UCLASS_USB, &uc);
287 uclass_foreach_dev(bus, uc) {
288 for (i = 0; i < USB_MAX_DEVICE; i++) {
289 struct usb_device *dev;
291 dev = usb_get_dev_index(bus, i); /* get device */
292 debug("i=%d, %s\n", i, dev ? dev->dev->name : "(done)");
294 break; /* no more devices available */
297 * find valid usb_ether driver for this device,
300 probe_valid_drivers(dev);
303 if (usb_max_eth_dev == USB_MAX_ETH_DEV)
308 for (i = 0; i < USB_MAX_DEVICE; i++) {
309 struct usb_device *dev;
311 dev = usb_get_dev_index(i); /* get device */
314 break; /* no more devices available */
316 /* find valid usb_ether driver for this device, if any */
317 probe_valid_drivers(dev);
320 if (usb_max_eth_dev == USB_MAX_ETH_DEV)
324 if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
325 printf("max USB Ethernet Device reached: %d stopping\n",
328 usb_disable_asynch(old_async); /* restore asynch value */
329 printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
330 if (usb_max_eth_dev > 0)