1.\"
2.\" Copyright (c) 2018 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3.\"
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.Dd April 9, 2018
27.Dt OF_DEVICE_FROM_XREF 9
28.Os
29.Sh NAME
30.Nm OF_device_from_xref ,
31.Nm OF_xref_from_device ,
32.Nm OF_device_register_xref
33.Nd "manage mappings between xrefs and devices"
34.Sh SYNOPSIS
35.In dev/ofw/ofw_bus.h
36.In dev/ofw/ofw_bus_subr.h
37.Ft int
38.Fn OF_device_register_xref "phandle_t xref" "device_t dev"
39.Ft device_t
40.Fn OF_device_from_xref "phandle_t xref"
41.Ft phandle_t
42.Fn OF_xref_from_device "device_t dev"
43.Sh DESCRIPTION
44When a device tree node references another node, the driver may
45need to get a device_t instance associated with the referenced node.
46For instance, an Ethernet driver accessing a PHY device.
47To make this possible, the kernel maintains a table that
48maps effective handles to device_t instances.
49.Pp
50.Fn OF_device_register_xref
51adds a map entry from the effective phandle
52.Fa xref
53to device
54.Fa dev .
55If a mapping entry for
56.Fa xref
57already exists, it is replaced with the new one.
58The function always returns 0.
59.Pp
60.Fn OF_device_from_xref
61returns a device_t instance associated with the effective phandle
62.Fa xref .
63If no such mapping exists, the function returns NULL.
64.Pp
65.Fn OF_xref_from_device
66returns the effective phandle associated with the device
67.Fa dev .
68If no such mapping exists, the function returns 0.
69.Sh EXAMPLES
70.Bd -literal
71    static int
72    acmephy_attach(device_t dev)
73    {
74        phandle_t node;
75
76	/* PHY node is referenced from eth device, register it */
77        node = ofw_bus_get_node(dev);
78        OF_device_register_xref(OF_xref_from_node(node), dev);
79
80        return (0);
81    }
82.Ed
83.Sh SEE ALSO
84.Xr OF_node_to_xref 9
85.Sh AUTHORS
86.An -nosplit
87This manual page was written by
88.An Oleksandr Tymoshenko Aq Mt gonzo@FreeBSD.org .
89