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_setprop

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

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