sandbox-raw-bus.c 1.6 KB

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