OF_FINDDEVICE(9)

πŸ’– OF_FINDDEVICE(9) πŸ’–

okay bestie, this is a Kernel Developer's Manual page, section 9, and we're talking about OF_finddevice πŸ’…


🌸 NAME 🌸

OF_finddevice β€” find a node in the device tree πŸŽ€


✨ SYNOPSIS ✨

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

phandle_t
OF_finddevice(const char *path);

πŸ¦‹ DESCRIPTION πŸ¦‹

So basically, OF_finddevice() hands you back the phandle for whatever node the path points at. That's it, that's the whole vibe. πŸ’–

But if that path literally cannot be found anywhere in the tree? It returns -1. Not found, bestie, it's giving ghosted. πŸ‘»


πŸ’€ CAVEATS πŸ’€

okay this part is important, no cap, please actually read it πŸ™ˆ

The return value should only be checked with equality operators β€” equal to, not equal to β€” and never with relational comparison (less than, greater than). Like, do not do that. Periodt.

Here's the tea β˜•: there's a whole discrepancy between the IEEE 1275 standard and FreeBSD's internal representation of a phandle. IEEE 1275 requires that the return value of this function be -1 when the path isn't found. But here's the plot twist β€” phandle_t is an unsigned type, so it literally cannot be relationally compared with -1 or 0. That comparison is always true or always false. 😭 Every single time. It's a trap and it will not warn you. 🚩

⚠️ fr fr: use == and != only. Relational operators on this return value are undefined nonsense dressed up as logic. πŸ’€

🌈 EXAMPLES 🌈

    phandle_t root, i2c;

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

see how the check is !=? that's the equality-operator energy we were just talking about. main character move. πŸ‘‘


🫢 SEE ALSO 🫢


πŸ‘‘ AUTHORS πŸ‘‘

This manual page was written by Oleksandr Tymoshenko <gonzo@FreeBSD.org> πŸ’–


that's the whole page, bestie β€” go forth and find your devices. the Debian version of this lil cutie landed on April 9, 2018, and it's filed under OF_FINDDEVICE(9). mwah πŸ’…βœ¨

πŸ“¦ where this page comes from