Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * tree.c: Basic device tree traversal/scanning for the Linux
0004  *         prom library.
0005  *
0006  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
0007  * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
0008  */
0009 
0010 #include <linux/string.h>
0011 #include <linux/types.h>
0012 #include <linux/kernel.h>
0013 #include <linux/sched.h>
0014 #include <linux/module.h>
0015 
0016 #include <asm/openprom.h>
0017 #include <asm/oplib.h>
0018 #include <asm/ldc.h>
0019 
0020 static phandle prom_node_to_node(const char *type, phandle node)
0021 {
0022     unsigned long args[5];
0023 
0024     args[0] = (unsigned long) type;
0025     args[1] = 1;
0026     args[2] = 1;
0027     args[3] = (unsigned int) node;
0028     args[4] = (unsigned long) -1;
0029 
0030     p1275_cmd_direct(args);
0031 
0032     return (phandle) args[4];
0033 }
0034 
0035 /* Return the child of node 'node' or zero if no this node has no
0036  * direct descendent.
0037  */
0038 inline phandle __prom_getchild(phandle node)
0039 {
0040     return prom_node_to_node("child", node);
0041 }
0042 
0043 phandle prom_getchild(phandle node)
0044 {
0045     phandle cnode;
0046 
0047     if ((s32)node == -1)
0048         return 0;
0049     cnode = __prom_getchild(node);
0050     if ((s32)cnode == -1)
0051         return 0;
0052     return cnode;
0053 }
0054 EXPORT_SYMBOL(prom_getchild);
0055 
0056 inline phandle prom_getparent(phandle node)
0057 {
0058     phandle cnode;
0059 
0060     if ((s32)node == -1)
0061         return 0;
0062     cnode = prom_node_to_node("parent", node);
0063     if ((s32)cnode == -1)
0064         return 0;
0065     return cnode;
0066 }
0067 
0068 /* Return the next sibling of node 'node' or zero if no more siblings
0069  * at this level of depth in the tree.
0070  */
0071 inline phandle __prom_getsibling(phandle node)
0072 {
0073     return prom_node_to_node(prom_peer_name, node);
0074 }
0075 
0076 phandle prom_getsibling(phandle node)
0077 {
0078     phandle sibnode;
0079 
0080     if ((s32)node == -1)
0081         return 0;
0082     sibnode = __prom_getsibling(node);
0083     if ((s32)sibnode == -1)
0084         return 0;
0085 
0086     return sibnode;
0087 }
0088 EXPORT_SYMBOL(prom_getsibling);
0089 
0090 /* Return the length in bytes of property 'prop' at node 'node'.
0091  * Return -1 on error.
0092  */
0093 int prom_getproplen(phandle node, const char *prop)
0094 {
0095     unsigned long args[6];
0096 
0097     if (!node || !prop)
0098         return -1;
0099 
0100     args[0] = (unsigned long) "getproplen";
0101     args[1] = 2;
0102     args[2] = 1;
0103     args[3] = (unsigned int) node;
0104     args[4] = (unsigned long) prop;
0105     args[5] = (unsigned long) -1;
0106 
0107     p1275_cmd_direct(args);
0108 
0109     return (int) args[5];
0110 }
0111 EXPORT_SYMBOL(prom_getproplen);
0112 
0113 /* Acquire a property 'prop' at node 'node' and place it in
0114  * 'buffer' which has a size of 'bufsize'.  If the acquisition
0115  * was successful the length will be returned, else -1 is returned.
0116  */
0117 int prom_getproperty(phandle node, const char *prop,
0118              char *buffer, int bufsize)
0119 {
0120     unsigned long args[8];
0121     int plen;
0122 
0123     plen = prom_getproplen(node, prop);
0124     if ((plen > bufsize) || (plen == 0) || (plen == -1))
0125         return -1;
0126 
0127     args[0] = (unsigned long) prom_getprop_name;
0128     args[1] = 4;
0129     args[2] = 1;
0130     args[3] = (unsigned int) node;
0131     args[4] = (unsigned long) prop;
0132     args[5] = (unsigned long) buffer;
0133     args[6] = bufsize;
0134     args[7] = (unsigned long) -1;
0135 
0136     p1275_cmd_direct(args);
0137 
0138     return (int) args[7];
0139 }
0140 EXPORT_SYMBOL(prom_getproperty);
0141 
0142 /* Acquire an integer property and return its value.  Returns -1
0143  * on failure.
0144  */
0145 int prom_getint(phandle node, const char *prop)
0146 {
0147     int intprop;
0148 
0149     if (prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
0150         return intprop;
0151 
0152     return -1;
0153 }
0154 EXPORT_SYMBOL(prom_getint);
0155 
0156 /* Acquire an integer property, upon error return the passed default
0157  * integer.
0158  */
0159 
0160 int prom_getintdefault(phandle node, const char *property, int deflt)
0161 {
0162     int retval;
0163 
0164     retval = prom_getint(node, property);
0165     if (retval == -1)
0166         return deflt;
0167 
0168     return retval;
0169 }
0170 EXPORT_SYMBOL(prom_getintdefault);
0171 
0172 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
0173 int prom_getbool(phandle node, const char *prop)
0174 {
0175     int retval;
0176 
0177     retval = prom_getproplen(node, prop);
0178     if (retval == -1)
0179         return 0;
0180     return 1;
0181 }
0182 EXPORT_SYMBOL(prom_getbool);
0183 
0184 /* Acquire a property whose value is a string, returns a null
0185  * string on error.  The char pointer is the user supplied string
0186  * buffer.
0187  */
0188 void prom_getstring(phandle node, const char *prop, char *user_buf,
0189         int ubuf_size)
0190 {
0191     int len;
0192 
0193     len = prom_getproperty(node, prop, user_buf, ubuf_size);
0194     if (len != -1)
0195         return;
0196     user_buf[0] = 0;
0197 }
0198 EXPORT_SYMBOL(prom_getstring);
0199 
0200 /* Does the device at node 'node' have name 'name'?
0201  * YES = 1   NO = 0
0202  */
0203 int prom_nodematch(phandle node, const char *name)
0204 {
0205     char namebuf[128];
0206     prom_getproperty(node, "name", namebuf, sizeof(namebuf));
0207     if (strcmp(namebuf, name) == 0)
0208         return 1;
0209     return 0;
0210 }
0211 
0212 /* Search siblings at 'node_start' for a node with name
0213  * 'nodename'.  Return node if successful, zero if not.
0214  */
0215 phandle prom_searchsiblings(phandle node_start, const char *nodename)
0216 {
0217     phandle thisnode;
0218     int error;
0219     char promlib_buf[128];
0220 
0221     for(thisnode = node_start; thisnode;
0222         thisnode=prom_getsibling(thisnode)) {
0223         error = prom_getproperty(thisnode, "name", promlib_buf,
0224                      sizeof(promlib_buf));
0225         /* Should this ever happen? */
0226         if(error == -1) continue;
0227         if(strcmp(nodename, promlib_buf)==0) return thisnode;
0228     }
0229 
0230     return 0;
0231 }
0232 EXPORT_SYMBOL(prom_searchsiblings);
0233 
0234 static const char *prom_nextprop_name = "nextprop";
0235 
0236 /* Return the first property type for node 'node'.
0237  * buffer should be at least 32B in length
0238  */
0239 char *prom_firstprop(phandle node, char *buffer)
0240 {
0241     unsigned long args[7];
0242 
0243     *buffer = 0;
0244     if ((s32)node == -1)
0245         return buffer;
0246 
0247     args[0] = (unsigned long) prom_nextprop_name;
0248     args[1] = 3;
0249     args[2] = 1;
0250     args[3] = (unsigned int) node;
0251     args[4] = 0;
0252     args[5] = (unsigned long) buffer;
0253     args[6] = (unsigned long) -1;
0254 
0255     p1275_cmd_direct(args);
0256 
0257     return buffer;
0258 }
0259 EXPORT_SYMBOL(prom_firstprop);
0260 
0261 /* Return the property type string after property type 'oprop'
0262  * at node 'node' .  Returns NULL string if no more
0263  * property types for this node.
0264  */
0265 char *prom_nextprop(phandle node, const char *oprop, char *buffer)
0266 {
0267     unsigned long args[7];
0268     char buf[32];
0269 
0270     if ((s32)node == -1) {
0271         *buffer = 0;
0272         return buffer;
0273     }
0274     if (oprop == buffer) {
0275         strcpy (buf, oprop);
0276         oprop = buf;
0277     }
0278 
0279     args[0] = (unsigned long) prom_nextprop_name;
0280     args[1] = 3;
0281     args[2] = 1;
0282     args[3] = (unsigned int) node;
0283     args[4] = (unsigned long) oprop;
0284     args[5] = (unsigned long) buffer;
0285     args[6] = (unsigned long) -1;
0286 
0287     p1275_cmd_direct(args);
0288 
0289     return buffer;
0290 }
0291 EXPORT_SYMBOL(prom_nextprop);
0292 
0293 phandle prom_finddevice(const char *name)
0294 {
0295     unsigned long args[5];
0296 
0297     if (!name)
0298         return 0;
0299     args[0] = (unsigned long) "finddevice";
0300     args[1] = 1;
0301     args[2] = 1;
0302     args[3] = (unsigned long) name;
0303     args[4] = (unsigned long) -1;
0304 
0305     p1275_cmd_direct(args);
0306 
0307     return (int) args[4];
0308 }
0309 EXPORT_SYMBOL(prom_finddevice);
0310 
0311 int prom_node_has_property(phandle node, const char *prop)
0312 {
0313     char buf [32];
0314         
0315     *buf = 0;
0316     do {
0317         prom_nextprop(node, buf, buf);
0318         if (!strcmp(buf, prop))
0319             return 1;
0320     } while (*buf);
0321     return 0;
0322 }
0323 EXPORT_SYMBOL(prom_node_has_property);
0324 
0325 /* Set property 'pname' at node 'node' to value 'value' which has a length
0326  * of 'size' bytes.  Return the number of bytes the prom accepted.
0327  */
0328 int
0329 prom_setprop(phandle node, const char *pname, char *value, int size)
0330 {
0331     unsigned long args[8];
0332 
0333     if (size == 0)
0334         return 0;
0335     if ((pname == 0) || (value == 0))
0336         return 0;
0337     
0338 #ifdef CONFIG_SUN_LDOMS
0339     if (ldom_domaining_enabled) {
0340         ldom_set_var(pname, value);
0341         return 0;
0342     }
0343 #endif
0344     args[0] = (unsigned long) "setprop";
0345     args[1] = 4;
0346     args[2] = 1;
0347     args[3] = (unsigned int) node;
0348     args[4] = (unsigned long) pname;
0349     args[5] = (unsigned long) value;
0350     args[6] = size;
0351     args[7] = (unsigned long) -1;
0352 
0353     p1275_cmd_direct(args);
0354 
0355     return (int) args[7];
0356 }
0357 EXPORT_SYMBOL(prom_setprop);
0358 
0359 inline phandle prom_inst2pkg(int inst)
0360 {
0361     unsigned long args[5];
0362     phandle node;
0363     
0364     args[0] = (unsigned long) "instance-to-package";
0365     args[1] = 1;
0366     args[2] = 1;
0367     args[3] = (unsigned int) inst;
0368     args[4] = (unsigned long) -1;
0369 
0370     p1275_cmd_direct(args);
0371 
0372     node = (int) args[4];
0373     if ((s32)node == -1)
0374         return 0;
0375     return node;
0376 }
0377 
0378 int prom_ihandle2path(int handle, char *buffer, int bufsize)
0379 {
0380     unsigned long args[7];
0381 
0382     args[0] = (unsigned long) "instance-to-path";
0383     args[1] = 3;
0384     args[2] = 1;
0385     args[3] = (unsigned int) handle;
0386     args[4] = (unsigned long) buffer;
0387     args[5] = bufsize;
0388     args[6] = (unsigned long) -1;
0389 
0390     p1275_cmd_direct(args);
0391 
0392     return (int) args[6];
0393 }