1
/* $Id: tree.c,v 1.3 2003/04/14 03:24:43 bencollins Exp $
2
* tree.c: Basic device tree traversal/scanning for the Linux
5
* Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6
* Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
10
#include <stringops.h>
12
/* Return the child of node 'node' or zero if no this node has no
15
int prom_getchild(int node)
22
if (prom_vers != PROM_P1275)
23
cnode = prom_nodeops->no_child(node);
25
cnode = p1275_cmd ("child", 1, node);
27
if (cnode == 0 || cnode == -1)
33
/* Return the next sibling of node 'node' or zero if no more siblings
34
* at this level of depth in the tree.
36
int prom_getsibling(int node)
43
if (prom_vers != PROM_P1275)
44
sibnode = prom_nodeops->no_nextnode(node);
46
sibnode = p1275_cmd ("peer", 1, node);
48
if (sibnode == 0 || sibnode == -1)
54
/* Return the length in bytes of property 'prop' at node 'node'.
57
int prom_getproplen(int node, char *prop)
61
if((!node) || (!prop))
64
if (prom_vers != PROM_P1275)
65
ret = prom_nodeops->no_proplen(node, prop);
67
ret = p1275_cmd ("getproplen", 2, node, prop);
72
/* Acquire a property 'prop' at node 'node' and place it in
73
* 'buffer' which has a size of 'bufsize'. If the acquisition
74
* was successful the length will be returned, else -1 is returned.
76
int prom_getproperty(int node, char *prop, char *buffer, int bufsize)
80
plen = prom_getproplen(node, prop);
81
if((plen > bufsize) || (plen == 0) || (plen == -1))
84
/* Ok, things seem all right. */
85
if (prom_vers != PROM_P1275)
86
ret = prom_nodeops->no_getprop(node, prop, buffer);
88
ret = p1275_cmd ("getprop", 4, node, prop, buffer, bufsize);
93
/* Acquire an integer property and return its value. Returns -1
96
int prom_getint(int node, char *prop)
100
if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
106
/* Acquire an integer property, upon error return the passed default
109
int prom_getintdefault(int node, char *property, int deflt)
113
retval = prom_getint(node, property);
114
if(retval == -1) return deflt;
119
/* Acquire a property whose value is a string, returns a null
120
* string on error. The char pointer is the user supplied string
123
void prom_getstring(int node, char *prop, char *user_buf, int ubuf_size)
127
len = prom_getproperty(node, prop, user_buf, ubuf_size);
128
if(len != -1) return;
133
/* Search siblings at 'node_start' for a node with name
134
* 'nodename'. Return node if successful, zero if not.
136
int prom_searchsiblings(int node_start, char *nodename)
140
static char promlib_buf[128];
142
for(thisnode = node_start; thisnode;
143
thisnode=prom_getsibling(thisnode)) {
144
error = prom_getproperty(thisnode, "name", promlib_buf,
145
sizeof(promlib_buf));
146
/* Should this ever happen? */
147
if(error == -1) continue;
148
if (strcmp(nodename, promlib_buf) == 0)
155
int prom_finddevice(char *path)
161
node = p1275_cmd ("finddevice", 1, path);
166
if (node == -1) return 0;