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