1 /*- 2 * Copyright (c) 2000, 2001 Michael Smith 3 * Copyright (c) 2000 BSDi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 /* 31 * Print information about system device configuration. 32 */ 33 34 #include <sys/types.h> 35 #include <err.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 #include "devinfo.h" 39 40 int rflag; 41 int vflag; 42 43 static void print_resource(struct devinfo_res *); 44 static int print_device_matching_resource(struct devinfo_res *, void *); 45 static int print_device_rman_resources(struct devinfo_rman *, void *); 46 static int print_device(struct devinfo_dev *, void *); 47 static int print_rman_resource(struct devinfo_res *, void *); 48 static int print_rman(struct devinfo_rman *, void *); 49 50 struct indent_arg 51 { 52 int indent; 53 void *arg; 54 }; 55 56 /* 57 * Print a resource. 58 */ 59 void 60 print_resource(struct devinfo_res *res) 61 { 62 struct devinfo_rman *rman; 63 int hexmode; 64 65 rman = devinfo_handle_to_rman(res->dr_rman); 66 hexmode = (rman->dm_size > 100) || (rman->dm_size == 0); 67 printf(hexmode ? "0x%lx" : "%lu", res->dr_start); 68 if (res->dr_size > 1) 69 printf(hexmode ? "-0x%lx" : "-%lu", 70 res->dr_start + res->dr_size - 1); 71 } 72 73 /* 74 * Print resource information if this resource matches the 75 * given device. 76 * 77 * If the given indent is 0, return an indicator that a matching 78 * resource exists. 79 */ 80 int 81 print_device_matching_resource(struct devinfo_res *res, void *arg) 82 { 83 struct indent_arg *ia = (struct indent_arg *)arg; 84 struct devinfo_dev *dev = (struct devinfo_dev *)ia->arg; 85 int i; 86 87 if (devinfo_handle_to_device(res->dr_device) == dev) { 88 /* in 'detect' mode, found a match */ 89 if (ia->indent == 0) 90 return(1); 91 for (i = 0; i < ia->indent; i++) 92 printf(" "); 93 print_resource(res); 94 printf("\n"); 95 } 96 return(0); 97 } 98 99 /* 100 * Print resource information for this device and resource manager. 101 */ 102 int 103 print_device_rman_resources(struct devinfo_rman *rman, void *arg) 104 { 105 struct indent_arg *ia = (struct indent_arg *)arg; 106 struct devinfo_dev *dev; 107 int indent, i; 108 109 indent = ia->indent; 110 dev = (struct devinfo_dev *)ia->arg; 111 112 /* check whether there are any resources matching this device */ 113 ia->indent = 0; 114 if (devinfo_foreach_rman_resource(rman, 115 print_device_matching_resource, ia) != 0) { 116 117 /* there are, print header */ 118 for (i = 0; i < indent; i++) 119 printf(" "); 120 printf("%s:\n", rman->dm_desc); 121 122 /* print resources */ 123 ia->indent = indent + 4; 124 devinfo_foreach_rman_resource(rman, 125 print_device_matching_resource, ia); 126 } 127 ia->indent = indent; 128 return(0); 129 } 130 131 /* 132 * Print information about a device. 133 */ 134 int 135 print_device(struct devinfo_dev *dev, void *arg) 136 { 137 struct indent_arg ia; 138 int i, indent; 139 140 if (vflag || (dev->dd_name[0] != 0 && dev->dd_state >= DIS_ATTACHED)) { 141 indent = (int)(intptr_t)arg; 142 for (i = 0; i < indent; i++) 143 printf(" "); 144 printf("%s", dev->dd_name[0] ? dev->dd_name : "unknown"); 145 if (vflag && *dev->dd_pnpinfo) 146 printf(" pnpinfo %s", dev->dd_pnpinfo); 147 if (vflag && *dev->dd_location) 148 printf(" at %s", dev->dd_location); 149 printf("\n"); 150 if (rflag) { 151 ia.indent = indent + 4; 152 ia.arg = dev; 153 devinfo_foreach_rman(print_device_rman_resources, 154 (void *)&ia); 155 } 156 } 157 158 return(devinfo_foreach_device_child(dev, print_device, 159 (void *)((char *)arg + 2))); 160 } 161 162 /* 163 * Print information about a resource under a resource manager. 164 */ 165 int 166 print_rman_resource(struct devinfo_res *res, void *arg __unused) 167 { 168 struct devinfo_dev *dev; 169 170 printf(" "); 171 print_resource(res); 172 dev = devinfo_handle_to_device(res->dr_device); 173 if ((dev != NULL) && (dev->dd_name[0] != 0)) { 174 printf(" (%s)", dev->dd_name); 175 } else { 176 printf(" ----"); 177 } 178 printf("\n"); 179 return(0); 180 } 181 182 /* 183 * Print information about a resource manager. 184 */ 185 int 186 print_rman(struct devinfo_rman *rman, void *arg __unused) 187 { 188 printf("%s:\n", rman->dm_desc); 189 devinfo_foreach_rman_resource(rman, print_rman_resource, 0); 190 return(0); 191 } 192 193 int 194 main(int argc, char *argv[]) 195 { 196 struct devinfo_dev *root; 197 int c, uflag; 198 199 uflag = 0; 200 while ((c = getopt(argc, argv, "ruv")) != -1) { 201 switch(c) { 202 case 'r': 203 rflag++; 204 break; 205 case 'u': 206 uflag++; 207 break; 208 case 'v': 209 vflag++; 210 break; 211 default: 212 errx(1, "usage: %s [-ruv]", argv[0]); 213 } 214 } 215 216 if (devinfo_init()) 217 err(1, "devinfo_init"); 218 219 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL) 220 errx(1, "can't find root device"); 221 222 /* print resource usage? */ 223 if (uflag) { 224 devinfo_foreach_rman(print_rman, NULL); 225 } else { 226 /* print device hierarchy */ 227 devinfo_foreach_device_child(root, print_device, (void *)0); 228 } 229 return(0); 230 } 231