OF_DEVICE_FROM_XREF(9)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1, nixpkgs unstable

πŸ’… OF_DEVICE_FROM_XREF πŸ’…

okay bestie, welcome to OF_DEVICE_FROM_XREF(9) β€” the section 9 kernel developer's manual page that's literally all about keeping track of who's who in the device tree πŸŽ€

✨ NAME ✨

OF_device_from_xref, OF_xref_from_device, OF_device_register_xref, OF_device_unregister_xref β€” manage mappings between xrefs and devices. yes girl, it's giving "matchmaker for nodes" πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

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

int
OF_device_register_xref(phandle_t xref, device_t dev);

void
OF_device_unregister_xref(phandle_t xref, device_t dev);

device_t
OF_device_from_xref(phandle_t xref);

phandle_t
OF_xref_from_device(device_t dev);

like, four functions, two headers, zero drama 🫢


🌸 DESCRIPTION 🌸

so picture this: a device tree node is out here referencing another node (main character energy tbh), and the driver lowkey needs to get the device_t instance that's associated with the referenced node. classic example? an Ethernet driver trying to vibe with a PHY device. to make that whole situation possible, the kernel keeps a whole table mapping effective handles to device_t instances. it's giving rolodex πŸ“‡

  • OF_device_register_xref() β€” adds a map entry from the effective phandle xref to dev. and if a mapping entry for xref already exists? it gets replaced with the new one, no cap. this function always returns 0. always. it's an overachiever in the consistency department πŸ‘‘
  • OF_device_unregister_xref() β€” removes the map entry from the effective phandle xref to dev. if there's no mapping entry for xref? it just silently returns. no error, no fuss, no text back, it's giving read receipt off πŸ«₯
  • OF_device_from_xref() β€” returns the device_t instance associated with the effective phandle xref. if that mapping doesn't exist, it returns NULL. bestie said "not found" and moved on 😭
  • OF_xref_from_device() β€” returns the effective phandle associated with the device dev. and if no such mapping exists, it returns 0. periodt πŸ’…

🧚 EXAMPLES 🧚

ok so here's the whole thing in action:

static int
acmephy_attach(device_t dev)
{
    phandle_t node;

    /* PHY node is referenced from eth device, register it */
    node = ofw_bus_get_node(dev);
    OF_device_register_xref(OF_xref_from_node(node), dev);

    return (0);
}

see how the comment says it all? the PHY node is referenced from the eth device, and we register it. iconic behavior honestly ⭐


πŸ”₯ SEE ALSO πŸ”₯

  • OF_node_to_xref(9) β€” go read that one next, iykyk

πŸ‘‘ AUTHORS πŸ‘‘

this manual page was written by Oleksandr Tymoshenko <gonzo@FreeBSD.org>. we stan a documentation legend 🌈


and that's a wrap, bestie! this page came to you courtesy of Debian, dated April 3, 2025, and yes β€” OF_DEVICE_FROM_XREF(9) is the name and it's staying in the group chat πŸ’œπŸ¦‹βœ¨

πŸ“¦ where this page comes from