OF_FINDDEVICE(9)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

πŸ’… OF_FINDDEVICE(9) β€” like, the device-tree finder bestie

okay so this is a page from the Kernel Developer's Manual (section 9, kernel girlies only ✨) and it's all about OF_finddevice β€” the little function that goes hunting for a node in the device tree πŸŒ³πŸ’–


✨ NAME ✨

OF_finddevice β€” find node in device tree πŸ¦‹


🌸 SYNOPSIS 🌸

#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

phandle_t
OF_finddevice(const char *path);

like, yes, you need both headers. iconic of them tbh πŸŽ€


πŸ’œ DESCRIPTION πŸ’œ

OF_finddevice() returns the phandle for the node specified by the path. That's it, that's the whole vibe 🫢

Returns -1 if the path cannot be found in the tree. no node, no phandle, no slay πŸ’€


🧚 EXAMPLES 🧚

phandle_t root, i2c;

root = OF_finddevice("/");
i2c = OF_finddevice("/soc/axi/i2c@a0e0000");
if (i2c != -1) {
    ...
}

see how it's done?? find the root, find the i2c node, then check it isn't -1 before you touch it. main character energy fr fr πŸ‘‘


πŸ”₯ SEE ALSO πŸ”₯

the whole device-tree friend group, we love a supporting cast πŸ’–


πŸ‘‘ AUTHORS πŸ‘‘

This manual page was written by Oleksandr Tymoshenko <gonzo@FreeBSD.org>. ty for the lore bestie 😭🫢


πŸ’€ CAVEATS πŸ’€

okay this is the important bit, pay attention, no cap:

The return value should only be checked with equality operators (equal to, not equal to) and not relational comparison (less than, greater than). 🚨

There is a discrepancy between IEEE 1275 standard and FreeBSD's internal representation of a phandle: IEEE 1275 requires the return value of this function to be -1 if the path is not found. But phandle_t is an unsigned type, so it cannot be relationally compared with -1 or 0 β€” this comparison is always true or always false. πŸ™ˆ

so like... == and != are your besties here, < and > are lowkey lying to you. it's giving unsigned type behaviour, and it is NOT the moment for relational comparisons, periodt πŸ’…

anyway that's the whole page, hope you found your node πŸ¦‹πŸŒΈβ­ β€” written by the Debian crew, dated April 9, 2018, and that's the whole footer moment, mwah πŸ’‹βœ¨πŸŽ€

πŸ“¦ where this page comes from