ofw_subr.c revision 1.57 1 1.57 thorpej /* $NetBSD: ofw_subr.c,v 1.57 2021/02/05 17:17:59 thorpej Exp $ */
2 1.57 thorpej
3 1.57 thorpej /*
4 1.57 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.57 thorpej * All rights reserved.
6 1.57 thorpej *
7 1.57 thorpej * Redistribution and use in source and binary forms, with or without
8 1.57 thorpej * modification, are permitted provided that the following conditions
9 1.57 thorpej * are met:
10 1.57 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.57 thorpej * notice, this list of conditions and the following disclaimer.
12 1.57 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.57 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.57 thorpej * documentation and/or other materials provided with the distribution.
15 1.57 thorpej *
16 1.57 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.57 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.57 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.57 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.57 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.57 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.57 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.57 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.57 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.57 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.57 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.57 thorpej */
28 1.1 cgd
29 1.1 cgd /*
30 1.1 cgd * Copyright 1998
31 1.1 cgd * Digital Equipment Corporation. All rights reserved.
32 1.1 cgd *
33 1.1 cgd * This software is furnished under license and may be used and
34 1.1 cgd * copied only in accordance with the following terms and conditions.
35 1.1 cgd * Subject to these conditions, you may download, copy, install,
36 1.1 cgd * use, modify and distribute this software in source and/or binary
37 1.1 cgd * form. No title or ownership is transferred hereby.
38 1.1 cgd *
39 1.1 cgd * 1) Any source code used, modified or distributed must reproduce
40 1.1 cgd * and retain this copyright notice and list of conditions as
41 1.1 cgd * they appear in the source file.
42 1.1 cgd *
43 1.1 cgd * 2) No right is granted to use any trade name, trademark, or logo of
44 1.1 cgd * Digital Equipment Corporation. Neither the "Digital Equipment
45 1.1 cgd * Corporation" name nor any trademark or logo of Digital Equipment
46 1.1 cgd * Corporation may be used to endorse or promote products derived
47 1.1 cgd * from this software without the prior written permission of
48 1.1 cgd * Digital Equipment Corporation.
49 1.1 cgd *
50 1.1 cgd * 3) This software is provided "AS-IS" and any express or implied
51 1.1 cgd * warranties, including but not limited to, any implied warranties
52 1.1 cgd * of merchantability, fitness for a particular purpose, or
53 1.1 cgd * non-infringement are disclaimed. In no event shall DIGITAL be
54 1.1 cgd * liable for any damages whatsoever, and in particular, DIGITAL
55 1.1 cgd * shall not be liable for special, indirect, consequential, or
56 1.1 cgd * incidental damages or damages for lost profits, loss of
57 1.1 cgd * revenue or loss of use, whether such damages arise in contract,
58 1.1 cgd * negligence, tort, under statute, in equity, at law or otherwise,
59 1.1 cgd * even if advised of the possibility of such damage.
60 1.1 cgd */
61 1.7 lukem
62 1.7 lukem #include <sys/cdefs.h>
63 1.57 thorpej __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.57 2021/02/05 17:17:59 thorpej Exp $");
64 1.1 cgd
65 1.2 cgd #include <sys/param.h>
66 1.42 thorpej #include <sys/device.h>
67 1.46 thorpej #include <sys/kmem.h>
68 1.2 cgd #include <sys/systm.h>
69 1.2 cgd #include <dev/ofw/openfirm.h>
70 1.2 cgd
71 1.3 cgd #define OFW_MAX_STACK_BUF_SIZE 256
72 1.3 cgd #define OFW_PATH_BUF_SIZE 512
73 1.3 cgd
74 1.1 cgd /*
75 1.57 thorpej * OpenFirmware device handle support.
76 1.57 thorpej */
77 1.57 thorpej
78 1.57 thorpej static device_call_t
79 1.57 thorpej of_devhandle_lookup_device_call(devhandle_t handle, const char *name,
80 1.57 thorpej devhandle_t *call_handlep)
81 1.57 thorpej {
82 1.57 thorpej __link_set_decl(of_device_calls, struct device_call_descriptor);
83 1.57 thorpej struct device_call_descriptor * const *desc;
84 1.57 thorpej
85 1.57 thorpej __link_set_foreach(desc, of_device_calls) {
86 1.57 thorpej if (strcmp((*desc)->name, name) == 0) {
87 1.57 thorpej return (*desc)->call;
88 1.57 thorpej }
89 1.57 thorpej }
90 1.57 thorpej return NULL;
91 1.57 thorpej }
92 1.57 thorpej
93 1.57 thorpej static const struct devhandle_impl of_devhandle_impl = {
94 1.57 thorpej .type = DEVHANDLE_TYPE_OF,
95 1.57 thorpej .lookup_device_call = of_devhandle_lookup_device_call,
96 1.57 thorpej };
97 1.57 thorpej
98 1.57 thorpej devhandle_t
99 1.57 thorpej devhandle_from_of(int phandle)
100 1.57 thorpej {
101 1.57 thorpej devhandle_t handle = {
102 1.57 thorpej .impl = &of_devhandle_impl,
103 1.57 thorpej .integer = phandle,
104 1.57 thorpej };
105 1.57 thorpej
106 1.57 thorpej return handle;
107 1.57 thorpej }
108 1.57 thorpej
109 1.57 thorpej int
110 1.57 thorpej devhandle_to_of(devhandle_t const handle)
111 1.57 thorpej {
112 1.57 thorpej KASSERT(devhandle_type(handle) == DEVHANDLE_TYPE_OF);
113 1.57 thorpej
114 1.57 thorpej return handle.integer;
115 1.57 thorpej }
116 1.57 thorpej
117 1.57 thorpej static int
118 1.57 thorpej of_device_enumerate_children(device_t dev, devhandle_t call_handle, void *v)
119 1.57 thorpej {
120 1.57 thorpej struct device_enumerate_children_args *args = v;
121 1.57 thorpej int phandle = devhandle_to_of(call_handle);
122 1.57 thorpej int child;
123 1.57 thorpej
124 1.57 thorpej for (child = OF_child(phandle); child != 0; child = OF_peer(child)) {
125 1.57 thorpej if (!args->callback(dev, devhandle_from_of(child),
126 1.57 thorpej args->callback_arg)) {
127 1.57 thorpej break;
128 1.57 thorpej }
129 1.57 thorpej }
130 1.57 thorpej
131 1.57 thorpej return 0;
132 1.57 thorpej }
133 1.57 thorpej OF_DEVICE_CALL_REGISTER("device-enumerate-children",
134 1.57 thorpej of_device_enumerate_children)
135 1.57 thorpej
136 1.57 thorpej /*
137 1.3 cgd * int of_decode_int(p)
138 1.3 cgd *
139 1.3 cgd * This routine converts OFW encoded-int datums
140 1.3 cgd * into the integer format of the host machine.
141 1.1 cgd *
142 1.3 cgd * It is primarily used to convert integer properties
143 1.3 cgd * returned by the OF_getprop routine.
144 1.2 cgd *
145 1.2 cgd * Arguments:
146 1.2 cgd * p pointer to unsigned char array which is an
147 1.2 cgd * OFW-encoded integer.
148 1.2 cgd *
149 1.2 cgd * Return Value:
150 1.2 cgd * Decoded integer value of argument p.
151 1.3 cgd *
152 1.3 cgd * Side Effects:
153 1.3 cgd * None.
154 1.1 cgd */
155 1.1 cgd int
156 1.14 dsl of_decode_int(const unsigned char *p)
157 1.1 cgd {
158 1.1 cgd unsigned int i = *p++ << 8;
159 1.1 cgd i = (i + *p++) << 8;
160 1.1 cgd i = (i + *p++) << 8;
161 1.1 cgd return (i + *p);
162 1.2 cgd }
163 1.2 cgd
164 1.2 cgd /*
165 1.3 cgd * int of_compatible(phandle, strings)
166 1.3 cgd *
167 1.2 cgd * This routine checks an OFW node's "compatible" entry to see if
168 1.2 cgd * it matches any of the provided strings.
169 1.2 cgd *
170 1.54 thorpej * of_compatible_match() is the preferred way to perform driver
171 1.52 thorpej * compatibility match. However, this routine that deals with
172 1.52 thorpej * only strings is useful in some situations and is provided for
173 1.52 thorpej * convenience.
174 1.2 cgd *
175 1.2 cgd * Arguments:
176 1.2 cgd * phandle OFW phandle of device to be checked for
177 1.2 cgd * compatibility.
178 1.2 cgd * strings Array of containing expected "compatibility"
179 1.2 cgd * property values, presence of any of which
180 1.2 cgd * indicates compatibility.
181 1.2 cgd *
182 1.2 cgd * Return Value:
183 1.52 thorpej * 0 if none of the strings are found in phandle's "compatibility"
184 1.24 jmcneill * property, or the reverse index of the matching string in the
185 1.52 thorpej * phandle's "compatibility" property plus 1.
186 1.3 cgd *
187 1.3 cgd * Side Effects:
188 1.3 cgd * None.
189 1.2 cgd */
190 1.2 cgd int
191 1.12 garbled of_compatible(int phandle, const char * const *strings)
192 1.2 cgd {
193 1.47 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
194 1.47 thorpej const char *cp;
195 1.52 thorpej int proplen, match = 0;
196 1.24 jmcneill
197 1.47 thorpej proplen = OF_getproplen(phandle, "compatible");
198 1.47 thorpej if (proplen <= 0) {
199 1.52 thorpej return 0;
200 1.38 rin }
201 1.2 cgd
202 1.47 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
203 1.47 thorpej
204 1.47 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
205 1.2 cgd goto out;
206 1.2 cgd }
207 1.2 cgd
208 1.47 thorpej for (; (cp = *strings) != NULL; strings++) {
209 1.47 thorpej if ((match = strlist_match(prop, proplen, cp)) != 0) {
210 1.47 thorpej break;
211 1.47 thorpej }
212 1.47 thorpej }
213 1.47 thorpej
214 1.47 thorpej out:
215 1.47 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
216 1.52 thorpej return match;
217 1.24 jmcneill }
218 1.10 perry
219 1.24 jmcneill /*
220 1.54 thorpej * int of_compatible_match(phandle, compat_data)
221 1.30 jmcneill *
222 1.51 thorpej * This routine searches an array of device_compatible_entry structures
223 1.51 thorpej * for a matching "compatible" entry matching the supplied OFW node,
224 1.51 thorpej * and returns a weighted match value corresponding to which string
225 1.51 thorpej * from the "compatible" property was matched, which more weight given
226 1.51 thorpej * to the first string than the last.
227 1.30 jmcneill *
228 1.30 jmcneill * It should be used when determining whether a driver can drive
229 1.30 jmcneill * a particular device.
230 1.30 jmcneill *
231 1.30 jmcneill * Arguments:
232 1.30 jmcneill * phandle OFW phandle of device to be checked for
233 1.30 jmcneill * compatibility.
234 1.30 jmcneill * compat_data Array of possible compat entry strings and
235 1.30 jmcneill * associated metadata. The last entry in the
236 1.30 jmcneill * list should have a "compat" of NULL to terminate
237 1.30 jmcneill * the list.
238 1.30 jmcneill *
239 1.30 jmcneill * Return Value:
240 1.30 jmcneill * 0 if none of the strings are found in phandle's "compatibility"
241 1.30 jmcneill * property, or a positive number based on the reverse index of the
242 1.30 jmcneill * matching string in the phandle's "compatibility" property, plus 1.
243 1.30 jmcneill *
244 1.30 jmcneill * Side Effects:
245 1.30 jmcneill * None.
246 1.30 jmcneill */
247 1.30 jmcneill int
248 1.54 thorpej of_compatible_match(int phandle,
249 1.42 thorpej const struct device_compatible_entry *compat_data)
250 1.30 jmcneill {
251 1.46 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
252 1.46 thorpej int proplen, match = 0;
253 1.46 thorpej
254 1.46 thorpej proplen = OF_getproplen(phandle, "compatible");
255 1.46 thorpej if (proplen <= 0) {
256 1.46 thorpej return 0;
257 1.46 thorpej }
258 1.46 thorpej
259 1.46 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
260 1.46 thorpej
261 1.46 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
262 1.46 thorpej goto out;
263 1.30 jmcneill }
264 1.46 thorpej
265 1.46 thorpej match = device_compatible_match_strlist(prop, proplen, compat_data);
266 1.46 thorpej
267 1.46 thorpej out:
268 1.46 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
269 1.46 thorpej return match;
270 1.30 jmcneill }
271 1.30 jmcneill
272 1.30 jmcneill /*
273 1.54 thorpej * const struct device_compatible_entry *of_compatible_lookup(phandle,
274 1.43 jmcneill * compat_data)
275 1.29 jmcneill *
276 1.51 thorpej * This routine searches an array of device_compatible_entry structures
277 1.51 thorpej * for a "compatible" entry matching the supplied OFW node.
278 1.29 jmcneill *
279 1.29 jmcneill * Arguments:
280 1.29 jmcneill * phandle OFW phandle of device to be checked for
281 1.29 jmcneill * compatibility.
282 1.29 jmcneill * compat_data Array of possible compat entry strings and
283 1.29 jmcneill * associated metadata. The last entry in the
284 1.29 jmcneill * list should have a "compat" of NULL to terminate
285 1.29 jmcneill * the list.
286 1.29 jmcneill *
287 1.29 jmcneill * Return Value:
288 1.29 jmcneill * The first matching compat_data entry in the array. If no matches
289 1.46 thorpej * are found, NULL is returned.
290 1.29 jmcneill *
291 1.29 jmcneill * Side Effects:
292 1.29 jmcneill * None.
293 1.29 jmcneill */
294 1.42 thorpej const struct device_compatible_entry *
295 1.54 thorpej of_compatible_lookup(int phandle,
296 1.42 thorpej const struct device_compatible_entry *compat_data)
297 1.29 jmcneill {
298 1.46 thorpej char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
299 1.46 thorpej const struct device_compatible_entry *match = NULL;
300 1.46 thorpej int proplen;
301 1.46 thorpej
302 1.46 thorpej proplen = OF_getproplen(phandle, "compatible");
303 1.46 thorpej if (proplen <= 0) {
304 1.46 thorpej return 0;
305 1.29 jmcneill }
306 1.46 thorpej
307 1.46 thorpej prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
308 1.46 thorpej
309 1.46 thorpej if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
310 1.46 thorpej goto out;
311 1.46 thorpej }
312 1.46 thorpej
313 1.46 thorpej match = device_compatible_lookup_strlist(prop, proplen, compat_data);
314 1.46 thorpej
315 1.46 thorpej out:
316 1.46 thorpej kmem_tmpbuf_free(prop, proplen, propbuf);
317 1.46 thorpej return match;
318 1.29 jmcneill }
319 1.29 jmcneill
320 1.29 jmcneill /*
321 1.4 cgd * int of_packagename(phandle, buf, bufsize)
322 1.3 cgd *
323 1.3 cgd * This routine places the last component of an OFW node's name
324 1.3 cgd * into a user-provided buffer.
325 1.3 cgd *
326 1.3 cgd * It can be used during autoconfiguration to make printing of
327 1.3 cgd * device names more informative.
328 1.3 cgd *
329 1.3 cgd * Arguments:
330 1.3 cgd * phandle OFW phandle of device whose name name is
331 1.3 cgd * desired.
332 1.3 cgd * buf Buffer to contain device name, provided by
333 1.3 cgd * caller. (For now, must be at least 4
334 1.3 cgd * bytes long.)
335 1.3 cgd * bufsize Length of buffer referenced by 'buf', in
336 1.3 cgd * bytes.
337 1.3 cgd *
338 1.3 cgd * Return Value:
339 1.3 cgd * -1 if the device path name could not be obtained or would
340 1.3 cgd * not fit in the allocated temporary buffer, or zero otherwise
341 1.6 soren * (meaning that the leaf node name was successfully extracted).
342 1.3 cgd *
343 1.3 cgd * Side Effects:
344 1.3 cgd * If the leaf node name was successfully extracted, 'buf' is
345 1.3 cgd * filled in with at most 'bufsize' bytes of the leaf node
346 1.3 cgd * name. If the leaf node was not successfully extracted, a
347 1.3 cgd * somewhat meaningful string is placed in the buffer. In
348 1.3 cgd * either case, the contents of 'buf' will be NUL-terminated.
349 1.3 cgd */
350 1.3 cgd int
351 1.12 garbled of_packagename(int phandle, char *buf, int bufsize)
352 1.3 cgd {
353 1.3 cgd char *pbuf;
354 1.3 cgd const char *lastslash;
355 1.3 cgd int l, rv;
356 1.3 cgd
357 1.48 thorpej pbuf = kmem_alloc(OFW_PATH_BUF_SIZE, KM_SLEEP);
358 1.3 cgd l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
359 1.3 cgd
360 1.3 cgd /* check that we could get the name, and that it's not too long. */
361 1.3 cgd if (l < 0 ||
362 1.3 cgd (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
363 1.3 cgd if (bufsize >= 25)
364 1.9 itojun snprintf(buf, bufsize, "??? (phandle 0x%x)", phandle);
365 1.3 cgd else if (bufsize >= 4)
366 1.9 itojun strlcpy(buf, "???", bufsize);
367 1.3 cgd else
368 1.4 cgd panic("of_packagename: bufsize = %d is silly",
369 1.4 cgd bufsize);
370 1.3 cgd rv = -1;
371 1.3 cgd } else {
372 1.5 mycroft pbuf[l] = '\0';
373 1.3 cgd lastslash = strrchr(pbuf, '/');
374 1.9 itojun strlcpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
375 1.3 cgd bufsize);
376 1.3 cgd rv = 0;
377 1.3 cgd }
378 1.3 cgd
379 1.48 thorpej kmem_free(pbuf, OFW_PATH_BUF_SIZE);
380 1.3 cgd return (rv);
381 1.1 cgd }
382 1.12 garbled
383 1.12 garbled /*
384 1.13 garbled * Find the first child of a given node that matches name. Does not recurse.
385 1.12 garbled */
386 1.12 garbled int
387 1.12 garbled of_find_firstchild_byname(int node, const char *name)
388 1.12 garbled {
389 1.12 garbled char namex[32];
390 1.12 garbled int nn;
391 1.12 garbled
392 1.12 garbled for (nn = OF_child(node); nn; nn = OF_peer(nn)) {
393 1.12 garbled memset(namex, 0, sizeof(namex));
394 1.12 garbled if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1)
395 1.12 garbled continue;
396 1.12 garbled if (strcmp(name, namex) == 0)
397 1.12 garbled return nn;
398 1.12 garbled }
399 1.12 garbled return -1;
400 1.12 garbled }
401 1.13 garbled
402 1.13 garbled /*
403 1.40 jmcneill * Find a child node that is compatible with str. Recurses, starting at node.
404 1.40 jmcneill */
405 1.40 jmcneill int
406 1.40 jmcneill of_find_bycompat(int node, const char *str)
407 1.40 jmcneill {
408 1.40 jmcneill const char * compatible[] = { str, NULL };
409 1.40 jmcneill int child, ret;
410 1.40 jmcneill
411 1.40 jmcneill for (child = OF_child(node); child; child = OF_peer(child)) {
412 1.53 thorpej if (of_compatible(child, compatible))
413 1.40 jmcneill return child;
414 1.40 jmcneill ret = of_find_bycompat(child, str);
415 1.40 jmcneill if (ret != -1)
416 1.40 jmcneill return ret;
417 1.40 jmcneill }
418 1.40 jmcneill
419 1.40 jmcneill return -1;
420 1.40 jmcneill }
421 1.40 jmcneill
422 1.40 jmcneill /*
423 1.13 garbled * Find a give node by name. Recurses, and seems to walk upwards too.
424 1.13 garbled */
425 1.13 garbled
426 1.13 garbled int
427 1.13 garbled of_getnode_byname(int start, const char *target)
428 1.13 garbled {
429 1.13 garbled int node, next;
430 1.13 garbled char name[64];
431 1.13 garbled
432 1.13 garbled if (start == 0)
433 1.13 garbled start = OF_peer(0);
434 1.13 garbled
435 1.13 garbled for (node = start; node; node = next) {
436 1.13 garbled memset(name, 0, sizeof name);
437 1.13 garbled OF_getprop(node, "name", name, sizeof name - 1);
438 1.13 garbled if (strcmp(name, target) == 0)
439 1.13 garbled break;
440 1.13 garbled
441 1.13 garbled if ((next = OF_child(node)) != 0)
442 1.13 garbled continue;
443 1.13 garbled
444 1.13 garbled while (node) {
445 1.13 garbled if ((next = OF_peer(node)) != 0)
446 1.13 garbled break;
447 1.13 garbled node = OF_parent(node);
448 1.13 garbled }
449 1.13 garbled }
450 1.13 garbled
451 1.13 garbled /* XXX is this correct? */
452 1.13 garbled return node;
453 1.13 garbled }
454 1.13 garbled
455 1.13 garbled /*
456 1.13 garbled * Create a uint32_t integer property from an OFW node property.
457 1.13 garbled */
458 1.13 garbled
459 1.50 mrg bool
460 1.13 garbled of_to_uint32_prop(prop_dictionary_t dict, int node, const char *ofname,
461 1.13 garbled const char *propname)
462 1.13 garbled {
463 1.13 garbled uint32_t prop;
464 1.13 garbled
465 1.13 garbled if (OF_getprop(node, ofname, &prop, sizeof(prop)) != sizeof(prop))
466 1.13 garbled return FALSE;
467 1.13 garbled
468 1.13 garbled return(prop_dictionary_set_uint32(dict, propname, prop));
469 1.13 garbled }
470 1.13 garbled
471 1.13 garbled /*
472 1.13 garbled * Create a data property from an OFW node property. Max size of 256bytes.
473 1.13 garbled */
474 1.13 garbled
475 1.50 mrg bool
476 1.13 garbled of_to_dataprop(prop_dictionary_t dict, int node, const char *ofname,
477 1.13 garbled const char *propname)
478 1.13 garbled {
479 1.13 garbled int len;
480 1.13 garbled uint8_t prop[256];
481 1.13 garbled
482 1.13 garbled len = OF_getprop(node, ofname, prop, 256);
483 1.13 garbled if (len < 1)
484 1.13 garbled return FALSE;
485 1.13 garbled
486 1.36 thorpej return prop_dictionary_set_data(dict, propname, prop, len);
487 1.13 garbled }
488 1.15 macallan
489 1.15 macallan /*
490 1.15 macallan * look at output-device, see if there's a Sun-typical video mode specifier as
491 1.15 macallan * in screen:r1024x768x60 attached. If found copy it into *buffer, otherwise
492 1.15 macallan * return NULL
493 1.15 macallan */
494 1.15 macallan
495 1.15 macallan char *
496 1.15 macallan of_get_mode_string(char *buffer, int len)
497 1.15 macallan {
498 1.15 macallan int options;
499 1.15 macallan char *pos, output_device[256];
500 1.15 macallan
501 1.15 macallan /*
502 1.15 macallan * finally, let's see if there's a video mode specified in
503 1.15 macallan * output-device and pass it on so there's at least some way
504 1.15 macallan * to program video modes
505 1.15 macallan */
506 1.15 macallan options = OF_finddevice("/options");
507 1.15 macallan if ((options == 0) || (options == -1))
508 1.15 macallan return NULL;
509 1.15 macallan if (OF_getprop(options, "output-device", output_device, 256) == 0)
510 1.15 macallan return NULL;
511 1.15 macallan
512 1.15 macallan /* find the mode string if there is one */
513 1.15 macallan pos = strstr(output_device, ":r");
514 1.15 macallan if (pos == NULL)
515 1.15 macallan return NULL;
516 1.15 macallan strncpy(buffer, pos + 2, len);
517 1.15 macallan return buffer;
518 1.15 macallan }
519 1.17 martin
520 1.57 thorpej void
521 1.57 thorpej of_device_register(device_t dev, int phandle)
522 1.57 thorpej {
523 1.57 thorpej
524 1.57 thorpej /* All we do here is set the devhandle in the device_t. */
525 1.57 thorpej device_set_handle(dev, devhandle_from_of(phandle));
526 1.57 thorpej }
527 1.57 thorpej
528 1.57 thorpej /*
529 1.57 thorpej * of_device_from_phandle --
530 1.57 thorpej *
531 1.57 thorpej * Return a device_t associated with the specified phandle.
532 1.57 thorpej *
533 1.57 thorpej * This is expected to be used rarely, so we don't care if
534 1.57 thorpej * it's fast. Also, it can only find devices that have
535 1.57 thorpej * gone through of_device_register() (obviously).
536 1.57 thorpej */
537 1.57 thorpej device_t
538 1.57 thorpej of_device_from_phandle(int phandle)
539 1.57 thorpej {
540 1.57 thorpej devhandle_t devhandle;
541 1.57 thorpej deviter_t di;
542 1.57 thorpej device_t dev;
543 1.57 thorpej
544 1.57 thorpej for (dev = deviter_first(&di, DEVITER_F_ROOT_FIRST);
545 1.57 thorpej dev != NULL;
546 1.57 thorpej dev = deviter_next(&di)) {
547 1.57 thorpej devhandle = device_handle(dev);
548 1.57 thorpej if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
549 1.57 thorpej if (devhandle_to_of(devhandle) == phandle) {
550 1.57 thorpej /* Found it! */
551 1.57 thorpej break;
552 1.57 thorpej }
553 1.57 thorpej }
554 1.57 thorpej }
555 1.57 thorpej deviter_release(&di);
556 1.57 thorpej return dev;
557 1.57 thorpej }
558 1.57 thorpej
559 1.17 martin /*
560 1.28 jmcneill * Returns true if the specified property is present.
561 1.27 jmcneill */
562 1.27 jmcneill bool
563 1.28 jmcneill of_hasprop(int node, const char *prop)
564 1.27 jmcneill {
565 1.27 jmcneill return OF_getproplen(node, prop) >= 0;
566 1.27 jmcneill }
567 1.27 jmcneill
568 1.27 jmcneill /*
569 1.27 jmcneill * Get the value of a uint32 property, compensating for host byte order.
570 1.27 jmcneill * Returns 0 on success, non-zero on failure.
571 1.27 jmcneill */
572 1.27 jmcneill int
573 1.27 jmcneill of_getprop_uint32(int node, const char *prop, uint32_t *val)
574 1.27 jmcneill {
575 1.27 jmcneill uint32_t v;
576 1.27 jmcneill int len;
577 1.27 jmcneill
578 1.27 jmcneill len = OF_getprop(node, prop, &v, sizeof(v));
579 1.27 jmcneill if (len != sizeof(v))
580 1.27 jmcneill return -1;
581 1.27 jmcneill
582 1.27 jmcneill *val = be32toh(v);
583 1.27 jmcneill return 0;
584 1.27 jmcneill }
585 1.32 jmcneill
586 1.41 ryo int
587 1.41 ryo of_getprop_uint32_array(int node, const char *prop, uint32_t *array, int n)
588 1.41 ryo {
589 1.41 ryo uint32_t *v = array;
590 1.41 ryo int len;
591 1.41 ryo
592 1.41 ryo len = OF_getprop(node, prop, array, n * sizeof(*v));
593 1.41 ryo if (len < (int)(n * sizeof(*v)))
594 1.41 ryo return -1;
595 1.41 ryo
596 1.41 ryo for (; n > 0; n--) {
597 1.41 ryo BE32TOH(*v);
598 1.41 ryo v++;
599 1.41 ryo }
600 1.41 ryo
601 1.41 ryo return 0;
602 1.41 ryo }
603 1.32 jmcneill /*
604 1.32 jmcneill * Get the value of a uint64 property, compensating for host byte order.
605 1.32 jmcneill * Returns 0 on success, non-zero on failure.
606 1.32 jmcneill */
607 1.32 jmcneill int
608 1.32 jmcneill of_getprop_uint64(int node, const char *prop, uint64_t *val)
609 1.32 jmcneill {
610 1.32 jmcneill uint64_t v;
611 1.32 jmcneill int len;
612 1.32 jmcneill
613 1.32 jmcneill len = OF_getprop(node, prop, &v, sizeof(v));
614 1.32 jmcneill if (len != sizeof(v))
615 1.32 jmcneill return -1;
616 1.32 jmcneill
617 1.32 jmcneill *val = be64toh(v);
618 1.32 jmcneill return 0;
619 1.32 jmcneill }
620