Thu Jul 16 21:32:44 2020 UTC ()
Add of_find_bycompat helper to search a tree for a node by compat string.


(jmcneill)
diff -r1.39 -r1.40 src/sys/dev/ofw/ofw_subr.c
diff -r1.38 -r1.39 src/sys/dev/ofw/openfirm.h

cvs diff -r1.39 -r1.40 src/sys/dev/ofw/ofw_subr.c (expand / switch to unified diff)

--- src/sys/dev/ofw/ofw_subr.c 2020/06/26 10:14:32 1.39
+++ src/sys/dev/ofw/ofw_subr.c 2020/07/16 21:32:44 1.40
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ofw_subr.c,v 1.39 2020/06/26 10:14:32 martin Exp $ */ 1/* $NetBSD: ofw_subr.c,v 1.40 2020/07/16 21:32:44 jmcneill Exp $ */
2 2
3/* 3/*
4 * Copyright 1998 4 * Copyright 1998
5 * Digital Equipment Corporation. All rights reserved. 5 * Digital Equipment Corporation. All rights reserved.
6 * 6 *
7 * This software is furnished under license and may be used and 7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions. 8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install, 9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary 10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby. 11 * form. No title or ownership is transferred hereby.
12 * 12 *
13 * 1) Any source code used, modified or distributed must reproduce 13 * 1) Any source code used, modified or distributed must reproduce
14 * and retain this copyright notice and list of conditions as 14 * and retain this copyright notice and list of conditions as
@@ -24,27 +24,27 @@ @@ -24,27 +24,27 @@
24 * 3) This software is provided "AS-IS" and any express or implied 24 * 3) This software is provided "AS-IS" and any express or implied
25 * warranties, including but not limited to, any implied warranties 25 * warranties, including but not limited to, any implied warranties
26 * of merchantability, fitness for a particular purpose, or 26 * of merchantability, fitness for a particular purpose, or
27 * non-infringement are disclaimed. In no event shall DIGITAL be 27 * non-infringement are disclaimed. In no event shall DIGITAL be
28 * liable for any damages whatsoever, and in particular, DIGITAL 28 * liable for any damages whatsoever, and in particular, DIGITAL
29 * shall not be liable for special, indirect, consequential, or 29 * shall not be liable for special, indirect, consequential, or
30 * incidental damages or damages for lost profits, loss of 30 * incidental damages or damages for lost profits, loss of
31 * revenue or loss of use, whether such damages arise in contract, 31 * revenue or loss of use, whether such damages arise in contract,
32 * negligence, tort, under statute, in equity, at law or otherwise, 32 * negligence, tort, under statute, in equity, at law or otherwise,
33 * even if advised of the possibility of such damage. 33 * even if advised of the possibility of such damage.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.39 2020/06/26 10:14:32 martin Exp $"); 37__KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.40 2020/07/16 21:32:44 jmcneill Exp $");
38 38
39#include <sys/param.h> 39#include <sys/param.h>
40#include <sys/systm.h> 40#include <sys/systm.h>
41#include <sys/malloc.h> 41#include <sys/malloc.h>
42#include <dev/ofw/openfirm.h> 42#include <dev/ofw/openfirm.h>
43 43
44#define OFW_MAX_STACK_BUF_SIZE 256 44#define OFW_MAX_STACK_BUF_SIZE 256
45#define OFW_PATH_BUF_SIZE 512 45#define OFW_PATH_BUF_SIZE 512
46 46
47/* 47/*
48 * int of_decode_int(p) 48 * int of_decode_int(p)
49 * 49 *
50 * This routine converts OFW encoded-int datums 50 * This routine converts OFW encoded-int datums
@@ -327,26 +327,46 @@ of_find_firstchild_byname(int node, cons @@ -327,26 +327,46 @@ of_find_firstchild_byname(int node, cons
327 int nn; 327 int nn;
328  328
329 for (nn = OF_child(node); nn; nn = OF_peer(nn)) { 329 for (nn = OF_child(node); nn; nn = OF_peer(nn)) {
330 memset(namex, 0, sizeof(namex)); 330 memset(namex, 0, sizeof(namex));
331 if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1) 331 if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1)
332 continue; 332 continue;
333 if (strcmp(name, namex) == 0) 333 if (strcmp(name, namex) == 0)
334 return nn; 334 return nn;
335 } 335 }
336 return -1; 336 return -1;
337} 337}
338 338
339/* 339/*
 340 * Find a child node that is compatible with str. Recurses, starting at node.
 341 */
 342int
 343of_find_bycompat(int node, const char *str)
 344{
 345 const char * compatible[] = { str, NULL };
 346 int child, ret;
 347
 348 for (child = OF_child(node); child; child = OF_peer(child)) {
 349 if (of_match_compatible(child, compatible) != 0)
 350 return child;
 351 ret = of_find_bycompat(child, str);
 352 if (ret != -1)
 353 return ret;
 354 }
 355
 356 return -1;
 357}
 358
 359/*
340 * Find a give node by name. Recurses, and seems to walk upwards too. 360 * Find a give node by name. Recurses, and seems to walk upwards too.
341 */ 361 */
342 362
343int 363int
344of_getnode_byname(int start, const char *target) 364of_getnode_byname(int start, const char *target)
345{ 365{
346 int node, next; 366 int node, next;
347 char name[64]; 367 char name[64];
348 368
349 if (start == 0) 369 if (start == 0)
350 start = OF_peer(0); 370 start = OF_peer(0);
351 371
352 for (node = start; node; node = next) { 372 for (node = start; node; node = next) {

cvs diff -r1.38 -r1.39 src/sys/dev/ofw/openfirm.h (expand / switch to unified diff)

--- src/sys/dev/ofw/openfirm.h 2019/08/06 18:17:52 1.38
+++ src/sys/dev/ofw/openfirm.h 2020/07/16 21:32:44 1.39
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: openfirm.h,v 1.38 2019/08/06 18:17:52 tnn Exp $ */ 1/* $NetBSD: openfirm.h,v 1.39 2020/07/16 21:32:44 jmcneill Exp $ */
2 2
3/* 3/*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH. 5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -106,26 +106,27 @@ int OF_write(int, const void *, int); @@ -106,26 +106,27 @@ int OF_write(int, const void *, int);
106int openfirmware(void *); 106int openfirmware(void *);
107 107
108/* 108/*
109 * Functions and variables provided by machine-independent code. 109 * Functions and variables provided by machine-independent code.
110 */ 110 */
111int of_compatible(int, const char * const *); 111int of_compatible(int, const char * const *);
112int of_match_compatible(int, const char * const *); 112int of_match_compatible(int, const char * const *);
113int of_match_compat_data(int, const struct of_compat_data *); 113int of_match_compat_data(int, const struct of_compat_data *);
114const struct of_compat_data * 114const struct of_compat_data *
115 of_search_compatible(int, const struct of_compat_data *); 115 of_search_compatible(int, const struct of_compat_data *);
116int of_decode_int(const unsigned char *); 116int of_decode_int(const unsigned char *);
117int of_packagename(int, char *, int); 117int of_packagename(int, char *, int);
118int of_find_firstchild_byname(int, const char *); 118int of_find_firstchild_byname(int, const char *);
 119int of_find_bycompat(int, const char *);
119int of_getnode_byname(int, const char *); 120int of_getnode_byname(int, const char *);
120boolean_t of_to_uint32_prop(prop_dictionary_t, int, const char *, 121boolean_t of_to_uint32_prop(prop_dictionary_t, int, const char *,
121 const char *); 122 const char *);
122boolean_t of_to_dataprop(prop_dictionary_t, int, const char *, 123boolean_t of_to_dataprop(prop_dictionary_t, int, const char *,
123 const char *); 124 const char *);
124 125
125int *of_network_decode_media(int, int *, int *); 126int *of_network_decode_media(int, int *, int *);
126char *of_get_mode_string(char *, int); 127char *of_get_mode_string(char *, int);
127 128
128void of_enter_i2c_devs(prop_dictionary_t, int, size_t, int); 129void of_enter_i2c_devs(prop_dictionary_t, int, size_t, int);
129void of_enter_spi_devs(prop_dictionary_t, int, size_t); 130void of_enter_spi_devs(prop_dictionary_t, int, size_t);
130 131
131bool of_hasprop(int, const char *); 132bool of_hasprop(int, const char *);