Cover V11, I10
oct2002.tar

Listing 2 Dumping lots of device tree information

  1 /* Compile with gcc -g -o devinfo devinfo.c -ldevinfo */
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <sys/stat.h>
  5 #include <libdevinfo.h>
  6
  7 void showstate(unsigned int state) {
  8    static char outstate[100];
  9
 10    outstate[0] = '\0';
 11    if (state & DI_DRIVER_DETACHED)
 12       strcat(outstate, "drvdet,");
 13    if (state & DI_DEVICE_OFFLINE)
 14       strcat(outstate, "offline,");
 15    if (state & DI_DEVICE_DOWN)
 16       strcat(outstate, "down,");
 17    if (state & DI_BUS_QUIESCED)
 18       strcat(outstate, "busquiesced,");
 19    if (state & DI_BUS_DOWN)
 20       strcat(outstate, "busdown,");
 21    if (outstate[0])
 22       outstate[strlen(outstate) - 1] = '\0';
 23
 24    printf("state(%s) ", outstate);
 25 }
 26
 27 void showprop(di_node_t tNode, di_prop_t tProp) {
 28    if (DINO(tNode)->hw_prop &&
 29        (DIPROP(tProp)->self >= DINO(tNode)->hw_prop))
 30       printf("hw");
 31    else if (DINO(tNode)->sys_prop &&
 32             (DIPROP(tProp)->self >= DINO(tNode)->sys_prop))
 33       printf("sys");
 34    else
 35       printf("drv");
 36
 37    printf("prop(%s) ", di_prop_name(tProp));
 38 }
 39
 40 void showminor(di_minor_t tMinor) {
 41    if (di_minor_spectype(tMinor) == S_IFCHR)
 42       putc('c', stdout);
 43    else
 44       putc('b', stdout);
 45
 46    printf("minor(%s %s) ", di_minor_name(tMinor),
 47           (di_minor_nodetype(tMinor) ? : "None"));
 48 }
 49
 50 void examinenode (di_node_t tNode, int indent) {
 51    di_minor_t tMinor;
 52    di_prop_t tProp;
 53    int i = indent++;
 54
 55    while (i--)
 56       printf("   ");
 57
 58    /* Print basic information */
 59    printf("%s bind(%s) bus(%s) drv(%s) inst(%d) ",
 60           di_node_name(tNode),
 61           di_binding_name(tNode),
 62           (di_bus_addr(tNode) ? : ""),
 63           (di_driver_name(tNode) ? : ""),
 64           di_instance(tNode));
 65
 66    showstate(di_state(tNode));
 67
 68    /* Examine any properties */
 69    tProp = DI_PROP_NIL;
 70    while ((tProp = di_prop_next(tNode, tProp)) != DI_PROP_NIL)
 71       showprop(tNode, tProp);
 72
 73    /* Examine any minor nodes */
 74    tMinor = DI_MINOR_NIL;
 75    while ((tMinor = di_minor_next(tNode, tMinor)) != DI_MINOR_NIL)
 76       showminor(tMinor);
 77
 78    printf("\n");
 79
 80    /* Now examine all the child nodes */
 81    tNode = di_child_node(tNode);
 82    while (tNode != DI_NODE_NIL) {
 83       examinenode(tNode, indent);
 84       tNode = di_sibling_node(tNode);
 85    }
 86 }
 87
 88 int main() {
 89    di_node_t tRootNode;
 90
 91    /* Get a snapshot of the device tree */
 92    if (!(tRootNode = di_init("/", DINFOCPYALL))) {
 93       perror("Failed to get root node");
 94       return(1);
 95    }
 96
 97    /* Recursively examine all nodes */
 98    examinenode(tRootNode, 0);
 99
100    /* Free the snapshot */
101    di_fini(tRootNode);
102
103    return(0);
104 }