xref: /freebsd/share/man/man9/OF_getprop.9 (revision ac32b99c2b90fc98766dd22f66f489a0b39688de)
1*ac32b99cSOleksandr Tymoshenko.\"
2*ac32b99cSOleksandr Tymoshenko.\" Copyright (c) 2018 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3*ac32b99cSOleksandr Tymoshenko.\"
4*ac32b99cSOleksandr Tymoshenko.\" All rights reserved.
5*ac32b99cSOleksandr Tymoshenko.\"
6*ac32b99cSOleksandr Tymoshenko.\" Redistribution and use in source and binary forms, with or without
7*ac32b99cSOleksandr Tymoshenko.\" modification, are permitted provided that the following conditions
8*ac32b99cSOleksandr Tymoshenko.\" are met:
9*ac32b99cSOleksandr Tymoshenko.\" 1. Redistributions of source code must retain the above copyright
10*ac32b99cSOleksandr Tymoshenko.\"    notice, this list of conditions and the following disclaimer.
11*ac32b99cSOleksandr Tymoshenko.\" 2. Redistributions in binary form must reproduce the above copyright
12*ac32b99cSOleksandr Tymoshenko.\"    notice, this list of conditions and the following disclaimer in the
13*ac32b99cSOleksandr Tymoshenko.\"    documentation and/or other materials provided with the distribution.
14*ac32b99cSOleksandr Tymoshenko.\"
15*ac32b99cSOleksandr Tymoshenko.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16*ac32b99cSOleksandr Tymoshenko.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*ac32b99cSOleksandr Tymoshenko.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*ac32b99cSOleksandr Tymoshenko.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19*ac32b99cSOleksandr Tymoshenko.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*ac32b99cSOleksandr Tymoshenko.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*ac32b99cSOleksandr Tymoshenko.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*ac32b99cSOleksandr Tymoshenko.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*ac32b99cSOleksandr Tymoshenko.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*ac32b99cSOleksandr Tymoshenko.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*ac32b99cSOleksandr Tymoshenko.\"
26*ac32b99cSOleksandr Tymoshenko.\" $FreeBSD$
27*ac32b99cSOleksandr Tymoshenko.\"
28*ac32b99cSOleksandr Tymoshenko.Dd April 9, 2018
29*ac32b99cSOleksandr Tymoshenko.Dt OF_CHILD 9
30*ac32b99cSOleksandr Tymoshenko.Os
31*ac32b99cSOleksandr Tymoshenko.Sh NAME
32*ac32b99cSOleksandr Tymoshenko.Nm OF_getprop ,
33*ac32b99cSOleksandr Tymoshenko.Nm OF_getproplen ,
34*ac32b99cSOleksandr Tymoshenko.Nm OF_getencprop ,
35*ac32b99cSOleksandr Tymoshenko.Nm OF_hasprop ,
36*ac32b99cSOleksandr Tymoshenko.Nm OF_searchprop ,
37*ac32b99cSOleksandr Tymoshenko.Nm OF_searchencprop ,
38*ac32b99cSOleksandr Tymoshenko.Nm OF_getprop_alloc ,
39*ac32b99cSOleksandr Tymoshenko.Nm OF_getencprop_alloc ,
40*ac32b99cSOleksandr Tymoshenko.Nm OF_prop_free ,
41*ac32b99cSOleksandr Tymoshenko.Nm OF_nextprop ,
42*ac32b99cSOleksandr Tymoshenko.Nm OF_setprop
43*ac32b99cSOleksandr Tymoshenko.Nd access properties of device tree node
44*ac32b99cSOleksandr Tymoshenko.Sh SYNOPSIS
45*ac32b99cSOleksandr Tymoshenko.In dev/ofw/ofw_bus.h
46*ac32b99cSOleksandr Tymoshenko.In dev/ofw/ofw_bus_subr.h
47*ac32b99cSOleksandr Tymoshenko.Ft ssize_t
48*ac32b99cSOleksandr Tymoshenko.Fn OF_getproplen "phandle_t node" "const char *propname"
49*ac32b99cSOleksandr Tymoshenko.Ft ssize_t
50*ac32b99cSOleksandr Tymoshenko.Fn OF_getprop "phandle_t node" "const char *propname" \
51*ac32b99cSOleksandr Tymoshenko"void *buf" "size_t len"
52*ac32b99cSOleksandr Tymoshenko.Ft ssize_t
53*ac32b99cSOleksandr Tymoshenko.Fn OF_getencprop "phandle_t node" "const char *prop" \
54*ac32b99cSOleksandr Tymoshenko"pcell_t *buf" "size_t len"
55*ac32b99cSOleksandr Tymoshenko.Ft int
56*ac32b99cSOleksandr Tymoshenko.Fn OF_hasprop "phandle_t node" "const char *propname"
57*ac32b99cSOleksandr Tymoshenko.Ft ssize_t
58*ac32b99cSOleksandr Tymoshenko.Fn OF_searchprop "phandle_t node" "const char *propname" \
59*ac32b99cSOleksandr Tymoshenko"void *buf" "size_t len"
60*ac32b99cSOleksandr Tymoshenko.Ft ssize_t
61*ac32b99cSOleksandr Tymoshenko.Fn OF_searchencprop "phandle_t node" "const char *propname" \
62*ac32b99cSOleksandr Tymoshenko"pcell_t *buf" "size_t len"
63*ac32b99cSOleksandr Tymoshenko.Ft ssize_t
64*ac32b99cSOleksandr Tymoshenko.Fn OF_getprop_alloc "phandle_t node" "const char *propname" \
65*ac32b99cSOleksandr Tymoshenko"void **buf"
66*ac32b99cSOleksandr Tymoshenko.Ft ssize_t
67*ac32b99cSOleksandr Tymoshenko.Fn OF_getencprop_alloc "phandle_t node" "const char *propname" \
68*ac32b99cSOleksandr Tymoshenko"pcell_t **buf"
69*ac32b99cSOleksandr Tymoshenko.Ft void
70*ac32b99cSOleksandr Tymoshenko.Fn OF_prop_free "void *buf"
71*ac32b99cSOleksandr Tymoshenko.Ft int
72*ac32b99cSOleksandr Tymoshenko.Fn OF_nextprop "phandle_t node" "const char *propname" \
73*ac32b99cSOleksandr Tymoshenko"char *buf" "size_t len"
74*ac32b99cSOleksandr Tymoshenko.Ft int
75*ac32b99cSOleksandr Tymoshenko.Fn OF_setprop "phandle_t node" "const char *propname" \
76*ac32b99cSOleksandr Tymoshenko"const void *buf" "size_t len"
77*ac32b99cSOleksandr Tymoshenko.Sh DESCRIPTION
78*ac32b99cSOleksandr Tymoshenko.Pp
79*ac32b99cSOleksandr TymoshenkoDevice nodes can have associated properties.
80*ac32b99cSOleksandr TymoshenkoProperties consist of a name and a value.
81*ac32b99cSOleksandr TymoshenkoA name is a human-readable string from 1 to 31 characters long.
82*ac32b99cSOleksandr TymoshenkoA value is an array of zero or more bytes that encode certain
83*ac32b99cSOleksandr Tymoshenkoinformation.
84*ac32b99cSOleksandr TymoshenkoThe meaning of that bytes depends on how drivers interpret them.
85*ac32b99cSOleksandr TymoshenkoProperties can encode byte arrays, text strings, unsigned 32-bit
86*ac32b99cSOleksandr Tymoshenkovalues or any combination of these types.
87*ac32b99cSOleksandr Tymoshenko.Pp
88*ac32b99cSOleksandr TymoshenkoProperty with a zero-length value usually represents boolean
89*ac32b99cSOleksandr Tymoshenkoinformation.
90*ac32b99cSOleksandr TymoshenkoIf the property is present, it signifies true, otherwise false.
91*ac32b99cSOleksandr Tymoshenko.Pp
92*ac32b99cSOleksandr TymoshenkoA byte array is encoded as a sequence of bytes and represents
93*ac32b99cSOleksandr Tymoshenkovalues like MAC addresses.
94*ac32b99cSOleksandr Tymoshenko.Pp
95*ac32b99cSOleksandr TymoshenkoA text string is a sequence of n printable characters.
96*ac32b99cSOleksandr TymoshenkoIt is encoded as a byte array of length n + 1 bytes with
97*ac32b99cSOleksandr Tymoshenkocharacters represented as bytes plus a terminating null character.
98*ac32b99cSOleksandr Tymoshenko.Pp
99*ac32b99cSOleksandr TymoshenkoUnsigned 32-bit values, also sometimes called cells, are
100*ac32b99cSOleksandr Tymoshenkoencoded as a sequence of 4 bytes in big-endian order.
101*ac32b99cSOleksandr Tymoshenko.Pp
102*ac32b99cSOleksandr Tymoshenko.Fn OF_getproplen
103*ac32b99cSOleksandr Tymoshenkoreturns either the length of the value associated with the property
104*ac32b99cSOleksandr Tymoshenko.Fa propname
105*ac32b99cSOleksandr Tymoshenkoin the node identified by
106*ac32b99cSOleksandr Tymoshenko.Fa node ,
107*ac32b99cSOleksandr Tymoshenkoor 0 if the property exists but has no associated value.
108*ac32b99cSOleksandr TymoshenkoIf
109*ac32b99cSOleksandr Tymoshenko.Fa propname
110*ac32b99cSOleksandr Tymoshenkodoes not exist, -1 is returned.
111*ac32b99cSOleksandr Tymoshenko.Pp
112*ac32b99cSOleksandr Tymoshenko.Fn OF_getprop
113*ac32b99cSOleksandr Tymoshenkocopies a maximum of
114*ac32b99cSOleksandr Tymoshenko.Fa len
115*ac32b99cSOleksandr Tymoshenkobytes from the value associated with the property
116*ac32b99cSOleksandr Tymoshenko.Fa propname
117*ac32b99cSOleksandr Tymoshenkoof the device node
118*ac32b99cSOleksandr Tymoshenko.Fa node
119*ac32b99cSOleksandr Tymoshenkointo the memory specified by
120*ac32b99cSOleksandr Tymoshenko.Fa buf .
121*ac32b99cSOleksandr TymoshenkoReturns the actual size of the value or -1 if the
122*ac32b99cSOleksandr Tymoshenkoproperty does not exist.
123*ac32b99cSOleksandr Tymoshenko.Pp
124*ac32b99cSOleksandr Tymoshenko.Fn OF_getencprop
125*ac32b99cSOleksandr Tymoshenkocopies a maximum of
126*ac32b99cSOleksandr Tymoshenko.Fa len
127*ac32b99cSOleksandr Tymoshenkobytes into memory specified by
128*ac32b99cSOleksandr Tymoshenko.Fa buf ,
129*ac32b99cSOleksandr Tymoshenkothen converts cell values from big-endian to host byte order.
130*ac32b99cSOleksandr TymoshenkoReturns the actual size of the value in bytes, or -1
131*ac32b99cSOleksandr Tymoshenkoif the property does not exist.
132*ac32b99cSOleksandr Tymoshenko.Fa len
133*ac32b99cSOleksandr Tymoshenkomust be a multiple of 4.
134*ac32b99cSOleksandr Tymoshenko.Pp
135*ac32b99cSOleksandr Tymoshenko.Fn OF_hasprop
136*ac32b99cSOleksandr Tymoshenkoreturns 1 if the device node
137*ac32b99cSOleksandr Tymoshenko.Fa node
138*ac32b99cSOleksandr Tymoshenkohas a property specified by
139*ac32b99cSOleksandr Tymoshenko.Fa propname ,
140*ac32b99cSOleksandr Tymoshenkoand zero if the property does not exist.
141*ac32b99cSOleksandr Tymoshenko.Pp
142*ac32b99cSOleksandr Tymoshenko.Fn OF_searchprop
143*ac32b99cSOleksandr Tymoshenkorecursively looks for the property specified by
144*ac32b99cSOleksandr Tymoshenko.Fa propname
145*ac32b99cSOleksandr Tymoshenkostarting with the device node
146*ac32b99cSOleksandr Tymoshenko.Fa node
147*ac32b99cSOleksandr Tymoshenkofollowed by the parent node and up to the root node.
148*ac32b99cSOleksandr TymoshenkoIf the property is found, the function copies a maximum of
149*ac32b99cSOleksandr Tymoshenko.Fa len
150*ac32b99cSOleksandr Tymoshenkobytes of the value associated with the property
151*ac32b99cSOleksandr Tymoshenkointo the memory specified by
152*ac32b99cSOleksandr Tymoshenko.Fa buf .
153*ac32b99cSOleksandr TymoshenkoReturns the actual size in bytes of the value,
154*ac32b99cSOleksandr Tymoshenkoor -1 if the property does not exist.
155*ac32b99cSOleksandr Tymoshenko.Pp
156*ac32b99cSOleksandr Tymoshenko.Fn OF_searchencprop
157*ac32b99cSOleksandr Tymoshenkorecursively looks for the property specified by
158*ac32b99cSOleksandr Tymoshenko.Fa propname
159*ac32b99cSOleksandr Tymoshenkostarting with the device node
160*ac32b99cSOleksandr Tymoshenko.Fa node
161*ac32b99cSOleksandr Tymoshenkofollowed by the parent node and up to the root node.
162*ac32b99cSOleksandr TymoshenkoIf the property is found, the function copies a maximum of
163*ac32b99cSOleksandr Tymoshenko.Fa len
164*ac32b99cSOleksandr Tymoshenkobytes of the value associated with the property
165*ac32b99cSOleksandr Tymoshenkointo the memory specified by
166*ac32b99cSOleksandr Tymoshenko.Fa buf ,
167*ac32b99cSOleksandr Tymoshenkothen converts cell values from big-endian to host byte order.
168*ac32b99cSOleksandr TymoshenkoReturns the actual size in bytes of the value,
169*ac32b99cSOleksandr Tymoshenkoor -1 if the property does not exist.
170*ac32b99cSOleksandr Tymoshenko.Pp
171*ac32b99cSOleksandr Tymoshenko.Fn OF_getprop_alloc
172*ac32b99cSOleksandr Tymoshenkoallocates memory large enough to hold the
173*ac32b99cSOleksandr Tymoshenkovalue associated with the property
174*ac32b99cSOleksandr Tymoshenko.Fa propname
175*ac32b99cSOleksandr Tymoshenkoof the device node
176*ac32b99cSOleksandr Tymoshenko.Fa node
177*ac32b99cSOleksandr Tymoshenkoand copies the value into the newly allocated memory region.
178*ac32b99cSOleksandr TymoshenkoReturns the actual size of the value and stores
179*ac32b99cSOleksandr Tymoshenkothe address of the allocated memory in
180*ac32b99cSOleksandr Tymoshenko.Fa *buf .
181*ac32b99cSOleksandr TymoshenkoIf the property has a zero-sized value
182*ac32b99cSOleksandr Tymoshenko.Fa *buf
183*ac32b99cSOleksandr Tymoshenkois set NULL.
184*ac32b99cSOleksandr TymoshenkoReturns -1 if the property does not exist or
185*ac32b99cSOleksandr Tymoshenkomemory allocation failed.
186*ac32b99cSOleksandr TymoshenkoAllocated memory should be released when no longer required
187*ac32b99cSOleksandr Tymoshenkoby calling
188*ac32b99cSOleksandr Tymoshenko.Fn OF_prop_free .
189*ac32b99cSOleksandr TymoshenkoThe function might sleep when allocating memory.
190*ac32b99cSOleksandr Tymoshenko.Pp
191*ac32b99cSOleksandr Tymoshenko.Fn OF_getencprop_alloc
192*ac32b99cSOleksandr Tymoshenkoallocates enough memory to hold the
193*ac32b99cSOleksandr Tymoshenkovalue associated with the property
194*ac32b99cSOleksandr Tymoshenko.Fa propname
195*ac32b99cSOleksandr Tymoshenkoof the device node
196*ac32b99cSOleksandr Tymoshenko.Fa node ,
197*ac32b99cSOleksandr Tymoshenkocopies the value into the newly allocated memory region, and
198*ac32b99cSOleksandr Tymoshenkothen converts cell values from big-endian to host byte
199*ac32b99cSOleksandr Tymoshenkoorder.
200*ac32b99cSOleksandr TymoshenkoThe actual size of the value is returned and the
201*ac32b99cSOleksandr Tymoshenkoaddress of allocated memory is stored in
202*ac32b99cSOleksandr Tymoshenko.Fa *buf .
203*ac32b99cSOleksandr TymoshenkoIf the property has zero-length value,
204*ac32b99cSOleksandr Tymoshenko.Fa *buf
205*ac32b99cSOleksandr Tymoshenkois set to NULL.
206*ac32b99cSOleksandr TymoshenkoReturns -1 if the property does not exist or
207*ac32b99cSOleksandr Tymoshenkomemory allocation failed.
208*ac32b99cSOleksandr TymoshenkoAllocated memory should be released when no longer required
209*ac32b99cSOleksandr Tymoshenkoby calling
210*ac32b99cSOleksandr Tymoshenko.Fn OF_prop_free .
211*ac32b99cSOleksandr TymoshenkoThe function might sleep when allocating memory.
212*ac32b99cSOleksandr Tymoshenko.Pp
213*ac32b99cSOleksandr Tymoshenko.Fn OF_prop_free
214*ac32b99cSOleksandr Tymoshenkoreleases memory at
215*ac32b99cSOleksandr Tymoshenko.Fa buf
216*ac32b99cSOleksandr Tymoshenkothat was allocated by
217*ac32b99cSOleksandr Tymoshenko.Fn OF_getprop_alloc
218*ac32b99cSOleksandr Tymoshenkoor
219*ac32b99cSOleksandr Tymoshenko.Fn OF_getencprop_alloc .
220*ac32b99cSOleksandr Tymoshenko.Pp
221*ac32b99cSOleksandr Tymoshenko.Fn OF_nextprop
222*ac32b99cSOleksandr Tymoshenkocopies a maximum of
223*ac32b99cSOleksandr Tymoshenko.Fa size
224*ac32b99cSOleksandr Tymoshenkobytes of the name of the property following the
225*ac32b99cSOleksandr Tymoshenko.Fa propname
226*ac32b99cSOleksandr Tymoshenkoproperty into
227*ac32b99cSOleksandr Tymoshenko.Fa buf .
228*ac32b99cSOleksandr TymoshenkoIf
229*ac32b99cSOleksandr Tymoshenko.Fa propname
230*ac32b99cSOleksandr Tymoshenkois NULL, the function copies the name of the first property of the
231*ac32b99cSOleksandr Tymoshenkodevice node
232*ac32b99cSOleksandr Tymoshenko.Fa node .
233*ac32b99cSOleksandr Tymoshenko.Fn OF_nextprop
234*ac32b99cSOleksandr Tymoshenkoreturns -1 if
235*ac32b99cSOleksandr Tymoshenko.Fa propname
236*ac32b99cSOleksandr Tymoshenkois invalid or there is an internal error, 0 if there are no more
237*ac32b99cSOleksandr Tymoshenkoproperties after
238*ac32b99cSOleksandr Tymoshenko.Fa propname ,
239*ac32b99cSOleksandr Tymoshenkoor 1 otherwise.
240*ac32b99cSOleksandr Tymoshenko.Pp
241*ac32b99cSOleksandr Tymoshenko.Fn OF_setprop
242*ac32b99cSOleksandr Tymoshenkosets the value of the property
243*ac32b99cSOleksandr Tymoshenko.Fa propname
244*ac32b99cSOleksandr Tymoshenkoin the device node
245*ac32b99cSOleksandr Tymoshenko.Fa node
246*ac32b99cSOleksandr Tymoshenkoto the value beginning at the address specified by
247*ac32b99cSOleksandr Tymoshenko.Fa buf
248*ac32b99cSOleksandr Tymoshenkoand running for
249*ac32b99cSOleksandr Tymoshenko.Fa len
250*ac32b99cSOleksandr Tymoshenkobytes.
251*ac32b99cSOleksandr TymoshenkoIf the property does not exist, the function tries to create
252*ac32b99cSOleksandr Tymoshenkoit.
253*ac32b99cSOleksandr Tymoshenko.Fn OF_setprop
254*ac32b99cSOleksandr Tymoshenkoreturns the actual size of the new value, or -1 if the
255*ac32b99cSOleksandr Tymoshenkoproperty value cannot be changed or the new property
256*ac32b99cSOleksandr Tymoshenkocannot be created.
257*ac32b99cSOleksandr Tymoshenko.Sh EXAMPLES
258*ac32b99cSOleksandr Tymoshenko.Bd -literal
259*ac32b99cSOleksandr Tymoshenko    phandle_t node;
260*ac32b99cSOleksandr Tymoshenko    phandle_t hdmixref, hdminode;
261*ac32b99cSOleksandr Tymoshenko    device_t hdmi;
262*ac32b99cSOleksandr Tymoshenko    uint8_t mac[6];
263*ac32b99cSOleksandr Tymoshenko    char *model;
264*ac32b99cSOleksandr Tymoshenko
265*ac32b99cSOleksandr Tymoshenko    /*
266*ac32b99cSOleksandr Tymoshenko     * Get a byte array property
267*ac32b99cSOleksandr Tymoshenko     */
268*ac32b99cSOleksandr Tymoshenko    if (OF_getprop(node, "eth,hwaddr", mac, sizeof(mac)) != sizeof(mac))
269*ac32b99cSOleksandr Tymoshenko        return;
270*ac32b99cSOleksandr Tymoshenko
271*ac32b99cSOleksandr Tymoshenko    /*
272*ac32b99cSOleksandr Tymoshenko     * Get internal node reference and device associated with it
273*ac32b99cSOleksandr Tymoshenko     */
274*ac32b99cSOleksandr Tymoshenko    if (OF_getencprop(node, "hdmi", &hdmixref) <= 0)
275*ac32b99cSOleksandr Tymoshenko        return;
276*ac32b99cSOleksandr Tymoshenko    hdmi = OF_device_from_xref(hdmixref);
277*ac32b99cSOleksandr Tymoshenko
278*ac32b99cSOleksandr Tymoshenko    /*
279*ac32b99cSOleksandr Tymoshenko     * Get string value of model property of HDMI framer node
280*ac32b99cSOleksandr Tymoshenko     */
281*ac32b99cSOleksandr Tymoshenko    hdminode = OF_node_from_xref(hdmixref);
282*ac32b99cSOleksandr Tymoshenko    if (OF_getprop_alloc(hdminode, "model", (void **)&model) <= 0)
283*ac32b99cSOleksandr Tymoshenko        return;
284*ac32b99cSOleksandr Tymoshenko.Ed
285*ac32b99cSOleksandr Tymoshenko.Sh SEE ALSO
286*ac32b99cSOleksandr Tymoshenko.Xr OF_device_from_xref 9
287*ac32b99cSOleksandr Tymoshenko.Xr OF_node_from_xref 9
288*ac32b99cSOleksandr Tymoshenko.Sh AUTHORS
289*ac32b99cSOleksandr Tymoshenko.An -nosplit
290*ac32b99cSOleksandr TymoshenkoThis manual page was written by
291*ac32b99cSOleksandr Tymoshenko.An Oleksandr Tymoshenko Aq Mt gonzo@FreeBSD.org .
292