root/arch/sparc/prom/tree.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. prom_getchild
  2. prom_getsibling
  3. prom_getproplen
  4. prom_getproperty
  5. prom_getint
  6. prom_getintdefault
  7. prom_getbool
  8. prom_getstring
  9. prom_nodematch
  10. prom_searchsiblings
  11. prom_firstprop
  12. prom_nextprop
  13. prom_node_has_property
  14. prom_setprop

   1 /* $Id: tree.c,v 1.7 1996/01/01 02:46:24 davem Exp $
   2  * tree.c: Basic device tree traversal/scanning for the Linux
   3  *         prom library.
   4  *
   5  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
   6  */
   7 
   8 #include <linux/string.h>
   9 
  10 #include <asm/openprom.h>
  11 #include <asm/oplib.h>
  12 
  13 static char promlib_buf[128];
  14 
  15 /* Return the child of node 'node' or zero if no this node has no
  16  * direct descendent.
  17  */
  18 int
  19 prom_getchild(int node)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21         int cnode;
  22 
  23         if(node == -1) return 0;
  24         cnode = prom_nodeops->no_child(node);
  25         if((cnode == 0) || (cnode == -1)) return 0;
  26         return cnode;
  27 }
  28 
  29 /* Return the next sibling of node 'node' or zero if no more siblings
  30  * at this level of depth in the tree.
  31  */
  32 int
  33 prom_getsibling(int node)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35         int sibnode;
  36 
  37         if(node == -1) return 0;
  38         sibnode = prom_nodeops->no_nextnode(node);
  39         if((sibnode == 0) || (sibnode == -1)) return 0;
  40         return sibnode;
  41 }
  42 
  43 /* Return the length in bytes of property 'prop' at node 'node'.
  44  * Return -1 on error.
  45  */
  46 int
  47 prom_getproplen(int node, char *prop)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         if((!node) || (!prop)) return -1;
  50         return prom_nodeops->no_proplen(node, prop);
  51 }
  52 
  53 /* Acquire a property 'prop' at node 'node' and place it in
  54  * 'buffer' which has a size of 'bufsize'.  If the acquisition
  55  * was successful the length will be returned, else -1 is returned.
  56  */
  57 int
  58 prom_getproperty(int node, char *prop, char *buffer, int bufsize)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60         int plen;
  61 
  62         plen = prom_getproplen(node, prop);
  63         if((plen > bufsize) || (plen == 0) || (plen == -1)) return -1;
  64 
  65         /* Ok, things seem all right. */
  66         return prom_nodeops->no_getprop(node, prop, buffer);
  67 }
  68 
  69 /* Acquire an integer property and return it's value.  Returns -1
  70  * on failure.
  71  */
  72 int
  73 prom_getint(int node, char *prop)
     /* [previous][next][first][last][top][bottom][index][help] */
  74 {
  75         static int intprop;
  76 
  77         if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
  78                 return intprop;
  79 
  80         return -1;
  81 }
  82 
  83 /* Acquire an integer property, upon error return the passed default
  84  * integer.
  85  */
  86 
  87 int
  88 prom_getintdefault(int node, char *property, int deflt)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90         int retval;
  91 
  92         retval = prom_getint(node, property);
  93         if(retval == -1) return deflt;
  94 
  95         return retval;
  96 }
  97 
  98 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
  99 int
 100 prom_getbool(int node, char *prop)
     /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102         int retval;
 103 
 104         retval = prom_getproplen(node, prop);
 105         if(retval == -1) return 0;
 106         return 1;
 107 }
 108 
 109 /* Acquire a property whose value is a string, returns a null
 110  * string on error.  The char pointer is the user supplied string
 111  * buffer.
 112  */
 113 void
 114 prom_getstring(int node, char *prop, char *user_buf, int ubuf_size)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116         int len;
 117 
 118         len = prom_getproperty(node, prop, user_buf, ubuf_size);
 119         if(len != -1) return;
 120         user_buf[0] = 0;
 121         return;
 122 }
 123 
 124 
 125 /* Does the device at node 'node' have name 'name'?
 126  * YES = 1   NO = 0
 127  */
 128 int
 129 prom_nodematch(int node, char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131         static char namebuf[128];
 132         prom_getproperty(node, "name", namebuf, sizeof(namebuf));
 133         if(strcmp(namebuf, name) == 0) return 1;
 134         return 0;
 135 }
 136 
 137 /* Search siblings at 'node_start' for a node with name
 138  * 'nodename'.  Return node if successful, zero if not.
 139  */
 140 int
 141 prom_searchsiblings(int node_start, char *nodename)
     /* [previous][next][first][last][top][bottom][index][help] */
 142 {
 143         int thisnode, error;
 144 
 145         for(thisnode = node_start; thisnode;
 146             thisnode=prom_getsibling(thisnode)) {
 147                 error = prom_getproperty(thisnode, "name", promlib_buf,
 148                                          sizeof(promlib_buf));
 149                 /* Should this ever happen? */
 150                 if(error == -1) continue;
 151                 if(strcmp(nodename, promlib_buf)==0) return thisnode;
 152         }
 153 
 154         return 0;
 155 }
 156 
 157 /* Return the first property type for node 'node'.
 158  */
 159 char *
 160 prom_firstprop(int node)
     /* [previous][next][first][last][top][bottom][index][help] */
 161 {
 162         if(node == -1) return "";
 163         return prom_nodeops->no_nextprop(node, (char *) 0x0);
 164 }
 165 
 166 /* Return the property type string after property type 'oprop'
 167  * at node 'node' .  Returns NULL string if no more
 168  * property types for this node.
 169  */
 170 char *
 171 prom_nextprop(int node, char *oprop)
     /* [previous][next][first][last][top][bottom][index][help] */
 172 {
 173         if(node == -1) return "";
 174         return prom_nodeops->no_nextprop(node, oprop);
 175 }
 176 
 177 int
 178 prom_node_has_property(int node, char *prop)
     /* [previous][next][first][last][top][bottom][index][help] */
 179 {
 180         char *current_property = "";
 181 
 182         do {
 183                 current_property = prom_nextprop(node, current_property);
 184                 if(!strcmp(current_property, prop))
 185                    return 1;
 186         } while (*current_property);
 187         return 0;
 188 }
 189 
 190 /* Set property 'pname' at node 'node' to value 'value' which has a length
 191  * of 'size' bytes.  Return the number of bytes the prom accepted.
 192  */
 193 int
 194 prom_setprop(int node, char *pname, char *value, int size)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         if(size == 0) return 0;
 197         if((pname == 0) || (value == 0)) return 0;
 198         return prom_nodeops->no_setprop(node, pname, value, size);
 199 }

/* [previous][next][first][last][top][bottom][index][help] */