1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2018 National Instruments
4  * Copyright (c) 2018 Joe Hershberger <joe.hershberger@ni.com>
5  */
6 
7 #include <common.h>
8 #include <asm/eth-raw-os.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <dm/device-internal.h>
12 #include <dm/lists.h>
13 
14 static int eth_raw_bus_post_bind(struct udevice *dev)
15 {
16 	struct sandbox_eth_raw_if_nameindex *ni, *i;
17 	struct udevice *child;
18 	struct eth_sandbox_raw_priv *priv;
19 	char *ub_ifname;
20 	static const char ub_ifname_pfx[] = "host_";
21 	u32 skip_localhost = 0;
22 
23 	ni = sandbox_eth_raw_if_nameindex();
24 	if (!ni)
25 		return -EINVAL;
26 
27 	dev_read_u32(dev, "skip-localhost", &skip_localhost);
28 	for (i = ni; !(i->if_index == 0 && !i->if_name); i++) {
29 		int local = sandbox_eth_raw_os_is_local(i->if_name);
30 
31 		if (local < 0)
32 			continue;
33 		if (skip_localhost && local)
34 			continue;
35 
36 		ub_ifname = calloc(IFNAMSIZ + sizeof(ub_ifname_pfx), 1);
37 		strcpy(ub_ifname, ub_ifname_pfx);
38 		strncat(ub_ifname, i->if_name, IFNAMSIZ);
39 		device_bind_driver(dev, "eth_sandbox_raw", ub_ifname, &child);
40 
41 		device_set_name_alloced(child);
42 		device_probe(child);
43 		priv = dev_get_priv(child);
44 		if (priv) {
45 			strcpy(priv->host_ifname, i->if_name);
46 			priv->host_ifindex = i->if_index;
47 			priv->local = local;
48 		}
49 	}
50 
51 	sandbox_eth_raw_if_freenameindex(ni);
52 
53 	return 0;
54 }
55 
56 static const struct udevice_id sandbox_eth_raw_bus_ids[] = {
57 	{ .compatible = "sandbox,eth-raw-bus" },
58 	{ }
59 };
60 
61 U_BOOT_DRIVER(sandbox_eth_raw_bus) = {
62 	.name       = "sb_eth_raw_bus",
63 	.id         = UCLASS_SIMPLE_BUS,
64 	.of_match   = sandbox_eth_raw_bus_ids,
65 	.bind       = eth_raw_bus_post_bind,
66 };
67