of_helpers.c 976 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <linux/string.h>
  2. #include <linux/err.h>
  3. #include <linux/slab.h>
  4. #include <linux/of.h>
  5. #include "of_helpers.h"
  6. /**
  7. * pseries_of_derive_parent - basically like dirname(1)
  8. * @path: the full_name of a node to be added to the tree
  9. *
  10. * Returns the node which should be the parent of the node
  11. * described by path. E.g., for path = "/foo/bar", returns
  12. * the node with full_name = "/foo".
  13. */
  14. struct device_node *pseries_of_derive_parent(const char *path)
  15. {
  16. struct device_node *parent;
  17. char *parent_path = "/";
  18. const char *tail;
  19. /* We do not want the trailing '/' character */
  20. tail = kbasename(path) - 1;
  21. /* reject if path is "/" */
  22. if (!strcmp(path, "/"))
  23. return ERR_PTR(-EINVAL);
  24. if (tail > path) {
  25. parent_path = kstrndup(path, tail - path, GFP_KERNEL);
  26. if (!parent_path)
  27. return ERR_PTR(-ENOMEM);
  28. }
  29. parent = of_find_node_by_path(parent_path);
  30. if (strcmp(parent_path, "/"))
  31. kfree(parent_path);
  32. return parent ? parent : ERR_PTR(-EINVAL);
  33. }