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