2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/usr.sbin/devinfo/devinfo.c,v 1.7 2007/10/27 13:06:15 jhb Exp $
28 * $DragonFly: src/usr.sbin/devinfo/devinfo.c,v 1.1 2008/09/30 12:20:29 hasso Exp $
32 * Print information about system device configuration.
35 #include <sys/types.h>
45 static void print_resource(struct devinfo_res *);
46 static int print_device_matching_resource(struct devinfo_res *, void *);
47 static int print_device_rman_resources(struct devinfo_rman *, void *);
48 static int print_device(struct devinfo_dev *, void *);
49 static int print_rman_resource(struct devinfo_res *, void *);
50 static int print_rman(struct devinfo_rman *, void *);
62 print_resource(struct devinfo_res *res)
64 struct devinfo_rman *rman;
67 rman = devinfo_handle_to_rman(res->dr_rman);
68 hexmode = (rman->dm_size > 1000) || (rman->dm_size == 0);
69 printf(hexmode ? "0x%lx" : "%lu", res->dr_start);
71 printf(hexmode ? "-0x%lx" : "-%lu",
72 res->dr_start + res->dr_size - 1);
76 * Print resource information if this resource matches the
79 * If the given indent is 0, return an indicator that a matching
83 print_device_matching_resource(struct devinfo_res *res, void *arg)
85 struct indent_arg *ia = (struct indent_arg *)arg;
86 struct devinfo_dev *dev = (struct devinfo_dev *)ia->arg;
89 if (devinfo_handle_to_device(res->dr_device) == dev) {
90 /* in 'detect' mode, found a match */
93 for (i = 0; i < ia->indent; i++)
102 * Print resource information for this device and resource manager.
105 print_device_rman_resources(struct devinfo_rman *rman, void *arg)
107 struct indent_arg *ia = (struct indent_arg *)arg;
112 /* check whether there are any resources matching this device */
114 if (devinfo_foreach_rman_resource(rman,
115 print_device_matching_resource, ia) != 0) {
117 /* there are, print header */
118 for (i = 0; i < indent; i++)
120 printf("%s:\n", rman->dm_desc);
122 /* print resources */
123 ia->indent = indent + 4;
124 devinfo_foreach_rman_resource(rman,
125 print_device_matching_resource, ia);
132 * Print information about a device.
135 print_device(struct devinfo_dev *dev, void *arg)
137 struct indent_arg ia;
141 (dev->dd_name[0] != 0 && dev->dd_state >= DIS_INPROGRESS)) {
142 indent = (int)(intptr_t)arg;
143 for (i = 0; i < indent; i++)
145 printf("%s", dev->dd_name[0] ? dev->dd_name : "unknown");
146 if (vflag && *dev->dd_pnpinfo)
147 printf(" pnpinfo %s", dev->dd_pnpinfo);
148 if (vflag && *dev->dd_location)
149 printf(" at %s", dev->dd_location);
152 ia.indent = indent + 4;
154 devinfo_foreach_rman(print_device_rman_resources,
159 return(devinfo_foreach_device_child(dev, print_device,
160 (void *)((char *)arg + 2)));
164 * Print information about a resource under a resource manager.
167 print_rman_resource(struct devinfo_res *res, void *arg __unused)
169 struct devinfo_dev *dev;
173 dev = devinfo_handle_to_device(res->dr_device);
174 if ((dev != NULL) && (dev->dd_name[0] != 0)) {
175 printf(" (%s)", dev->dd_name);
184 * Print information about a resource manager.
187 print_rman(struct devinfo_rman *rman, void *arg __unused)
189 printf("%s:\n", rman->dm_desc);
190 devinfo_foreach_rman_resource(rman, print_rman_resource, 0);
195 main(int argc, char *argv[])
197 struct devinfo_dev *root;
201 while ((c = getopt(argc, argv, "ruv")) != -1) {
213 fprintf(stderr, "%s\n%s\n",
214 "usage: devinfo [-rv]",
221 err(1, "devinfo_init");
223 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
224 errx(1, "can't find root device");
226 /* print resource usage? */
228 devinfo_foreach_rman(print_rman, NULL);
230 /* print device hierarchy */
231 devinfo_foreach_device_child(root, print_device, NULL);