15de18cdeSSam Ravnborg /* 25de18cdeSSam Ravnborg * tree.c: Basic device tree traversal/scanning for the Linux 35de18cdeSSam Ravnborg * prom library. 45de18cdeSSam Ravnborg * 55de18cdeSSam Ravnborg * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) 65de18cdeSSam Ravnborg * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz) 75de18cdeSSam Ravnborg */ 85de18cdeSSam Ravnborg 95de18cdeSSam Ravnborg #include <linux/string.h> 105de18cdeSSam Ravnborg #include <linux/types.h> 115de18cdeSSam Ravnborg #include <linux/kernel.h> 125de18cdeSSam Ravnborg #include <linux/sched.h> 13917c3660SSam Ravnborg #include <linux/module.h> 145de18cdeSSam Ravnborg 155de18cdeSSam Ravnborg #include <asm/openprom.h> 165de18cdeSSam Ravnborg #include <asm/oplib.h> 175de18cdeSSam Ravnborg #include <asm/ldc.h> 185de18cdeSSam Ravnborg 198d125562SAndres Salomon static phandle prom_node_to_node(const char *type, phandle node) 2025edd694SDavid S. Miller { 2125edd694SDavid S. Miller unsigned long args[5]; 2225edd694SDavid S. Miller 2325edd694SDavid S. Miller args[0] = (unsigned long) type; 2425edd694SDavid S. Miller args[1] = 1; 2525edd694SDavid S. Miller args[2] = 1; 2625edd694SDavid S. Miller args[3] = (unsigned int) node; 2725edd694SDavid S. Miller args[4] = (unsigned long) -1; 2825edd694SDavid S. Miller 2925edd694SDavid S. Miller p1275_cmd_direct(args); 3025edd694SDavid S. Miller 318d125562SAndres Salomon return (phandle) args[4]; 3225edd694SDavid S. Miller } 3325edd694SDavid S. Miller 345de18cdeSSam Ravnborg /* Return the child of node 'node' or zero if no this node has no 355de18cdeSSam Ravnborg * direct descendent. 365de18cdeSSam Ravnborg */ 378d125562SAndres Salomon inline phandle __prom_getchild(phandle node) 385de18cdeSSam Ravnborg { 3925edd694SDavid S. Miller return prom_node_to_node("child", node); 405de18cdeSSam Ravnborg } 415de18cdeSSam Ravnborg 428d125562SAndres Salomon inline phandle prom_getchild(phandle node) 435de18cdeSSam Ravnborg { 448d125562SAndres Salomon phandle cnode; 455de18cdeSSam Ravnborg 46*4a3a2552SAndres Salomon if ((s32)node == -1) 4725edd694SDavid S. Miller return 0; 485de18cdeSSam Ravnborg cnode = __prom_getchild(node); 49*4a3a2552SAndres Salomon if ((s32)cnode == -1) 5025edd694SDavid S. Miller return 0; 5125edd694SDavid S. Miller return cnode; 525de18cdeSSam Ravnborg } 53917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getchild); 545de18cdeSSam Ravnborg 558d125562SAndres Salomon inline phandle prom_getparent(phandle node) 565de18cdeSSam Ravnborg { 578d125562SAndres Salomon phandle cnode; 585de18cdeSSam Ravnborg 59*4a3a2552SAndres Salomon if ((s32)node == -1) 6025edd694SDavid S. Miller return 0; 6125edd694SDavid S. Miller cnode = prom_node_to_node("parent", node); 62*4a3a2552SAndres Salomon if ((s32)cnode == -1) 6325edd694SDavid S. Miller return 0; 6425edd694SDavid S. Miller return cnode; 655de18cdeSSam Ravnborg } 665de18cdeSSam Ravnborg 675de18cdeSSam Ravnborg /* Return the next sibling of node 'node' or zero if no more siblings 685de18cdeSSam Ravnborg * at this level of depth in the tree. 695de18cdeSSam Ravnborg */ 708d125562SAndres Salomon inline phandle __prom_getsibling(phandle node) 715de18cdeSSam Ravnborg { 7225edd694SDavid S. Miller return prom_node_to_node(prom_peer_name, node); 735de18cdeSSam Ravnborg } 745de18cdeSSam Ravnborg 758d125562SAndres Salomon inline phandle prom_getsibling(phandle node) 765de18cdeSSam Ravnborg { 778d125562SAndres Salomon phandle sibnode; 785de18cdeSSam Ravnborg 79*4a3a2552SAndres Salomon if ((s32)node == -1) 805de18cdeSSam Ravnborg return 0; 815de18cdeSSam Ravnborg sibnode = __prom_getsibling(node); 82*4a3a2552SAndres Salomon if ((s32)sibnode == -1) 835de18cdeSSam Ravnborg return 0; 845de18cdeSSam Ravnborg 855de18cdeSSam Ravnborg return sibnode; 865de18cdeSSam Ravnborg } 87917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getsibling); 885de18cdeSSam Ravnborg 895de18cdeSSam Ravnborg /* Return the length in bytes of property 'prop' at node 'node'. 905de18cdeSSam Ravnborg * Return -1 on error. 915de18cdeSSam Ravnborg */ 928d125562SAndres Salomon inline int prom_getproplen(phandle node, const char *prop) 935de18cdeSSam Ravnborg { 9425edd694SDavid S. Miller unsigned long args[6]; 9525edd694SDavid S. Miller 9625edd694SDavid S. Miller if (!node || !prop) 9725edd694SDavid S. Miller return -1; 9825edd694SDavid S. Miller 9925edd694SDavid S. Miller args[0] = (unsigned long) "getproplen"; 10025edd694SDavid S. Miller args[1] = 2; 10125edd694SDavid S. Miller args[2] = 1; 10225edd694SDavid S. Miller args[3] = (unsigned int) node; 10325edd694SDavid S. Miller args[4] = (unsigned long) prop; 10425edd694SDavid S. Miller args[5] = (unsigned long) -1; 10525edd694SDavid S. Miller 10625edd694SDavid S. Miller p1275_cmd_direct(args); 10725edd694SDavid S. Miller 10825edd694SDavid S. Miller return (int) args[5]; 1095de18cdeSSam Ravnborg } 110917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getproplen); 1115de18cdeSSam Ravnborg 1125de18cdeSSam Ravnborg /* Acquire a property 'prop' at node 'node' and place it in 1135de18cdeSSam Ravnborg * 'buffer' which has a size of 'bufsize'. If the acquisition 1145de18cdeSSam Ravnborg * was successful the length will be returned, else -1 is returned. 1155de18cdeSSam Ravnborg */ 1168d125562SAndres Salomon inline int prom_getproperty(phandle node, const char *prop, 1175de18cdeSSam Ravnborg char *buffer, int bufsize) 1185de18cdeSSam Ravnborg { 11925edd694SDavid S. Miller unsigned long args[8]; 1205de18cdeSSam Ravnborg int plen; 1215de18cdeSSam Ravnborg 1225de18cdeSSam Ravnborg plen = prom_getproplen(node, prop); 12325edd694SDavid S. Miller if ((plen > bufsize) || (plen == 0) || (plen == -1)) 1245de18cdeSSam Ravnborg return -1; 12525edd694SDavid S. Miller 12625edd694SDavid S. Miller args[0] = (unsigned long) prom_getprop_name; 12725edd694SDavid S. Miller args[1] = 4; 12825edd694SDavid S. Miller args[2] = 1; 12925edd694SDavid S. Miller args[3] = (unsigned int) node; 13025edd694SDavid S. Miller args[4] = (unsigned long) prop; 13125edd694SDavid S. Miller args[5] = (unsigned long) buffer; 13225edd694SDavid S. Miller args[6] = bufsize; 13325edd694SDavid S. Miller args[7] = (unsigned long) -1; 13425edd694SDavid S. Miller 13525edd694SDavid S. Miller p1275_cmd_direct(args); 13625edd694SDavid S. Miller 13725edd694SDavid S. Miller return (int) args[7]; 1385de18cdeSSam Ravnborg } 139917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getproperty); 1405de18cdeSSam Ravnborg 1415de18cdeSSam Ravnborg /* Acquire an integer property and return its value. Returns -1 1425de18cdeSSam Ravnborg * on failure. 1435de18cdeSSam Ravnborg */ 1448d125562SAndres Salomon inline int prom_getint(phandle node, const char *prop) 1455de18cdeSSam Ravnborg { 1465de18cdeSSam Ravnborg int intprop; 1475de18cdeSSam Ravnborg 1485de18cdeSSam Ravnborg if (prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1) 1495de18cdeSSam Ravnborg return intprop; 1505de18cdeSSam Ravnborg 1515de18cdeSSam Ravnborg return -1; 1525de18cdeSSam Ravnborg } 153917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getint); 1545de18cdeSSam Ravnborg 1555de18cdeSSam Ravnborg /* Acquire an integer property, upon error return the passed default 1565de18cdeSSam Ravnborg * integer. 1575de18cdeSSam Ravnborg */ 1585de18cdeSSam Ravnborg 1598d125562SAndres Salomon int prom_getintdefault(phandle node, const char *property, int deflt) 1605de18cdeSSam Ravnborg { 1615de18cdeSSam Ravnborg int retval; 1625de18cdeSSam Ravnborg 1635de18cdeSSam Ravnborg retval = prom_getint(node, property); 16425edd694SDavid S. Miller if (retval == -1) 16525edd694SDavid S. Miller return deflt; 1665de18cdeSSam Ravnborg 1675de18cdeSSam Ravnborg return retval; 1685de18cdeSSam Ravnborg } 169917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getintdefault); 1705de18cdeSSam Ravnborg 1715de18cdeSSam Ravnborg /* Acquire a boolean property, 1=TRUE 0=FALSE. */ 1728d125562SAndres Salomon int prom_getbool(phandle node, const char *prop) 1735de18cdeSSam Ravnborg { 1745de18cdeSSam Ravnborg int retval; 1755de18cdeSSam Ravnborg 1765de18cdeSSam Ravnborg retval = prom_getproplen(node, prop); 17725edd694SDavid S. Miller if (retval == -1) 17825edd694SDavid S. Miller return 0; 1795de18cdeSSam Ravnborg return 1; 1805de18cdeSSam Ravnborg } 181917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getbool); 1825de18cdeSSam Ravnborg 1835de18cdeSSam Ravnborg /* Acquire a property whose value is a string, returns a null 1845de18cdeSSam Ravnborg * string on error. The char pointer is the user supplied string 1855de18cdeSSam Ravnborg * buffer. 1865de18cdeSSam Ravnborg */ 1878d125562SAndres Salomon void prom_getstring(phandle node, const char *prop, char *user_buf, 1888d125562SAndres Salomon int ubuf_size) 1895de18cdeSSam Ravnborg { 1905de18cdeSSam Ravnborg int len; 1915de18cdeSSam Ravnborg 1925de18cdeSSam Ravnborg len = prom_getproperty(node, prop, user_buf, ubuf_size); 19325edd694SDavid S. Miller if (len != -1) 19425edd694SDavid S. Miller return; 1955de18cdeSSam Ravnborg user_buf[0] = 0; 1965de18cdeSSam Ravnborg } 197917c3660SSam Ravnborg EXPORT_SYMBOL(prom_getstring); 1985de18cdeSSam Ravnborg 1995de18cdeSSam Ravnborg /* Does the device at node 'node' have name 'name'? 2005de18cdeSSam Ravnborg * YES = 1 NO = 0 2015de18cdeSSam Ravnborg */ 2028d125562SAndres Salomon int prom_nodematch(phandle node, const char *name) 2035de18cdeSSam Ravnborg { 2045de18cdeSSam Ravnborg char namebuf[128]; 2055de18cdeSSam Ravnborg prom_getproperty(node, "name", namebuf, sizeof(namebuf)); 20625edd694SDavid S. Miller if (strcmp(namebuf, name) == 0) 20725edd694SDavid S. Miller return 1; 2085de18cdeSSam Ravnborg return 0; 2095de18cdeSSam Ravnborg } 2105de18cdeSSam Ravnborg 2115de18cdeSSam Ravnborg /* Search siblings at 'node_start' for a node with name 2125de18cdeSSam Ravnborg * 'nodename'. Return node if successful, zero if not. 2135de18cdeSSam Ravnborg */ 2148d125562SAndres Salomon phandle prom_searchsiblings(phandle node_start, const char *nodename) 2155de18cdeSSam Ravnborg { 2168d125562SAndres Salomon phandle thisnode; 2178d125562SAndres Salomon int error; 2185de18cdeSSam Ravnborg char promlib_buf[128]; 2195de18cdeSSam Ravnborg 2205de18cdeSSam Ravnborg for(thisnode = node_start; thisnode; 2215de18cdeSSam Ravnborg thisnode=prom_getsibling(thisnode)) { 2225de18cdeSSam Ravnborg error = prom_getproperty(thisnode, "name", promlib_buf, 2235de18cdeSSam Ravnborg sizeof(promlib_buf)); 2245de18cdeSSam Ravnborg /* Should this ever happen? */ 2255de18cdeSSam Ravnborg if(error == -1) continue; 2265de18cdeSSam Ravnborg if(strcmp(nodename, promlib_buf)==0) return thisnode; 2275de18cdeSSam Ravnborg } 2285de18cdeSSam Ravnborg 2295de18cdeSSam Ravnborg return 0; 2305de18cdeSSam Ravnborg } 231917c3660SSam Ravnborg EXPORT_SYMBOL(prom_searchsiblings); 2325de18cdeSSam Ravnborg 23325edd694SDavid S. Miller static const char *prom_nextprop_name = "nextprop"; 23425edd694SDavid S. Miller 2355de18cdeSSam Ravnborg /* Return the first property type for node 'node'. 2365de18cdeSSam Ravnborg * buffer should be at least 32B in length 2375de18cdeSSam Ravnborg */ 2388d125562SAndres Salomon inline char *prom_firstprop(phandle node, char *buffer) 2395de18cdeSSam Ravnborg { 24025edd694SDavid S. Miller unsigned long args[7]; 24125edd694SDavid S. Miller 2425de18cdeSSam Ravnborg *buffer = 0; 243*4a3a2552SAndres Salomon if ((s32)node == -1) 24425edd694SDavid S. Miller return buffer; 24525edd694SDavid S. Miller 24625edd694SDavid S. Miller args[0] = (unsigned long) prom_nextprop_name; 24725edd694SDavid S. Miller args[1] = 3; 24825edd694SDavid S. Miller args[2] = 1; 24925edd694SDavid S. Miller args[3] = (unsigned int) node; 25025edd694SDavid S. Miller args[4] = 0; 25125edd694SDavid S. Miller args[5] = (unsigned long) buffer; 25225edd694SDavid S. Miller args[6] = (unsigned long) -1; 25325edd694SDavid S. Miller 25425edd694SDavid S. Miller p1275_cmd_direct(args); 25525edd694SDavid S. Miller 2565de18cdeSSam Ravnborg return buffer; 2575de18cdeSSam Ravnborg } 258917c3660SSam Ravnborg EXPORT_SYMBOL(prom_firstprop); 2595de18cdeSSam Ravnborg 2605de18cdeSSam Ravnborg /* Return the property type string after property type 'oprop' 2615de18cdeSSam Ravnborg * at node 'node' . Returns NULL string if no more 2625de18cdeSSam Ravnborg * property types for this node. 2635de18cdeSSam Ravnborg */ 2648d125562SAndres Salomon inline char *prom_nextprop(phandle node, const char *oprop, char *buffer) 2655de18cdeSSam Ravnborg { 26625edd694SDavid S. Miller unsigned long args[7]; 2675de18cdeSSam Ravnborg char buf[32]; 2685de18cdeSSam Ravnborg 269*4a3a2552SAndres Salomon if ((s32)node == -1) { 2705de18cdeSSam Ravnborg *buffer = 0; 2715de18cdeSSam Ravnborg return buffer; 2725de18cdeSSam Ravnborg } 2735de18cdeSSam Ravnborg if (oprop == buffer) { 2745de18cdeSSam Ravnborg strcpy (buf, oprop); 2755de18cdeSSam Ravnborg oprop = buf; 2765de18cdeSSam Ravnborg } 27725edd694SDavid S. Miller 27825edd694SDavid S. Miller args[0] = (unsigned long) prom_nextprop_name; 27925edd694SDavid S. Miller args[1] = 3; 28025edd694SDavid S. Miller args[2] = 1; 28125edd694SDavid S. Miller args[3] = (unsigned int) node; 28225edd694SDavid S. Miller args[4] = (unsigned long) oprop; 28325edd694SDavid S. Miller args[5] = (unsigned long) buffer; 28425edd694SDavid S. Miller args[6] = (unsigned long) -1; 28525edd694SDavid S. Miller 28625edd694SDavid S. Miller p1275_cmd_direct(args); 28725edd694SDavid S. Miller 2885de18cdeSSam Ravnborg return buffer; 2895de18cdeSSam Ravnborg } 290917c3660SSam Ravnborg EXPORT_SYMBOL(prom_nextprop); 2915de18cdeSSam Ravnborg 2928d125562SAndres Salomon phandle prom_finddevice(const char *name) 2935de18cdeSSam Ravnborg { 29425edd694SDavid S. Miller unsigned long args[5]; 29525edd694SDavid S. Miller 2965de18cdeSSam Ravnborg if (!name) 2975de18cdeSSam Ravnborg return 0; 29825edd694SDavid S. Miller args[0] = (unsigned long) "finddevice"; 29925edd694SDavid S. Miller args[1] = 1; 30025edd694SDavid S. Miller args[2] = 1; 30125edd694SDavid S. Miller args[3] = (unsigned long) name; 30225edd694SDavid S. Miller args[4] = (unsigned long) -1; 30325edd694SDavid S. Miller 30425edd694SDavid S. Miller p1275_cmd_direct(args); 30525edd694SDavid S. Miller 30625edd694SDavid S. Miller return (int) args[4]; 3075de18cdeSSam Ravnborg } 308917c3660SSam Ravnborg EXPORT_SYMBOL(prom_finddevice); 3095de18cdeSSam Ravnborg 3108d125562SAndres Salomon int prom_node_has_property(phandle node, const char *prop) 3115de18cdeSSam Ravnborg { 3125de18cdeSSam Ravnborg char buf [32]; 3135de18cdeSSam Ravnborg 3145de18cdeSSam Ravnborg *buf = 0; 3155de18cdeSSam Ravnborg do { 3165de18cdeSSam Ravnborg prom_nextprop(node, buf, buf); 3175de18cdeSSam Ravnborg if (!strcmp(buf, prop)) 3185de18cdeSSam Ravnborg return 1; 3195de18cdeSSam Ravnborg } while (*buf); 3205de18cdeSSam Ravnborg return 0; 3215de18cdeSSam Ravnborg } 322917c3660SSam Ravnborg EXPORT_SYMBOL(prom_node_has_property); 3235de18cdeSSam Ravnborg 3245de18cdeSSam Ravnborg /* Set property 'pname' at node 'node' to value 'value' which has a length 3255de18cdeSSam Ravnborg * of 'size' bytes. Return the number of bytes the prom accepted. 3265de18cdeSSam Ravnborg */ 3275de18cdeSSam Ravnborg int 3288d125562SAndres Salomon prom_setprop(phandle node, const char *pname, char *value, int size) 3295de18cdeSSam Ravnborg { 33025edd694SDavid S. Miller unsigned long args[8]; 33125edd694SDavid S. Miller 3325de18cdeSSam Ravnborg if (size == 0) 3335de18cdeSSam Ravnborg return 0; 3345de18cdeSSam Ravnborg if ((pname == 0) || (value == 0)) 3355de18cdeSSam Ravnborg return 0; 3365de18cdeSSam Ravnborg 3375de18cdeSSam Ravnborg #ifdef CONFIG_SUN_LDOMS 3385de18cdeSSam Ravnborg if (ldom_domaining_enabled) { 3395de18cdeSSam Ravnborg ldom_set_var(pname, value); 3405de18cdeSSam Ravnborg return 0; 3415de18cdeSSam Ravnborg } 3425de18cdeSSam Ravnborg #endif 34325edd694SDavid S. Miller args[0] = (unsigned long) "setprop"; 34425edd694SDavid S. Miller args[1] = 4; 34525edd694SDavid S. Miller args[2] = 1; 34625edd694SDavid S. Miller args[3] = (unsigned int) node; 34725edd694SDavid S. Miller args[4] = (unsigned long) pname; 34825edd694SDavid S. Miller args[5] = (unsigned long) value; 34925edd694SDavid S. Miller args[6] = size; 35025edd694SDavid S. Miller args[7] = (unsigned long) -1; 35125edd694SDavid S. Miller 35225edd694SDavid S. Miller p1275_cmd_direct(args); 35325edd694SDavid S. Miller 35425edd694SDavid S. Miller return (int) args[7]; 3555de18cdeSSam Ravnborg } 356917c3660SSam Ravnborg EXPORT_SYMBOL(prom_setprop); 3575de18cdeSSam Ravnborg 3588d125562SAndres Salomon inline phandle prom_inst2pkg(int inst) 3595de18cdeSSam Ravnborg { 36025edd694SDavid S. Miller unsigned long args[5]; 3618d125562SAndres Salomon phandle node; 3625de18cdeSSam Ravnborg 36325edd694SDavid S. Miller args[0] = (unsigned long) "instance-to-package"; 36425edd694SDavid S. Miller args[1] = 1; 36525edd694SDavid S. Miller args[2] = 1; 36625edd694SDavid S. Miller args[3] = (unsigned int) inst; 36725edd694SDavid S. Miller args[4] = (unsigned long) -1; 36825edd694SDavid S. Miller 36925edd694SDavid S. Miller p1275_cmd_direct(args); 37025edd694SDavid S. Miller 37125edd694SDavid S. Miller node = (int) args[4]; 372*4a3a2552SAndres Salomon if ((s32)node == -1) 37325edd694SDavid S. Miller return 0; 3745de18cdeSSam Ravnborg return node; 3755de18cdeSSam Ravnborg } 3765de18cdeSSam Ravnborg 3775de18cdeSSam Ravnborg int prom_ihandle2path(int handle, char *buffer, int bufsize) 3785de18cdeSSam Ravnborg { 37925edd694SDavid S. Miller unsigned long args[7]; 38025edd694SDavid S. Miller 38125edd694SDavid S. Miller args[0] = (unsigned long) "instance-to-path"; 38225edd694SDavid S. Miller args[1] = 3; 38325edd694SDavid S. Miller args[2] = 1; 38425edd694SDavid S. Miller args[3] = (unsigned int) handle; 38525edd694SDavid S. Miller args[4] = (unsigned long) buffer; 38625edd694SDavid S. Miller args[5] = bufsize; 38725edd694SDavid S. Miller args[6] = (unsigned long) -1; 38825edd694SDavid S. Miller 38925edd694SDavid S. Miller p1275_cmd_direct(args); 39025edd694SDavid S. Miller 39125edd694SDavid S. Miller return (int) args[6]; 3925de18cdeSSam Ravnborg } 393