2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000, 2001 Michael Smith
5 * Copyright (c) 2000 BSDi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Print information about system device configuration.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
49 static void print_resource(struct devinfo_res *);
50 static int print_device_matching_resource(struct devinfo_res *, void *);
51 static int print_device_rman_resources(struct devinfo_rman *, void *);
52 static int print_device(struct devinfo_dev *, void *);
53 static int print_rman_resource(struct devinfo_res *, void *);
54 static int print_rman(struct devinfo_rman *, void *);
66 print_resource(struct devinfo_res *res)
68 struct devinfo_rman *rman;
71 rman = devinfo_handle_to_rman(res->dr_rman);
72 hexmode = (rman->dm_size > 1000) || (rman->dm_size == 0);
73 printf(hexmode ? "0x%jx" : "%ju", res->dr_start);
75 printf(hexmode ? "-0x%jx" : "-%ju",
76 res->dr_start + res->dr_size - 1);
80 * Print resource information if this resource matches the
83 * If the given indent is 0, return an indicator that a matching
87 print_device_matching_resource(struct devinfo_res *res, void *arg)
89 struct indent_arg *ia = (struct indent_arg *)arg;
90 struct devinfo_dev *dev = (struct devinfo_dev *)ia->arg;
93 if (devinfo_handle_to_device(res->dr_device) == dev) {
94 /* in 'detect' mode, found a match */
97 for (i = 0; i < ia->indent; i++)
106 * Print resource information for this device and resource manager.
109 print_device_rman_resources(struct devinfo_rman *rman, void *arg)
111 struct indent_arg *ia = (struct indent_arg *)arg;
116 /* check whether there are any resources matching this device */
118 if (devinfo_foreach_rman_resource(rman,
119 print_device_matching_resource, ia) != 0) {
121 /* there are, print header */
122 for (i = 0; i < indent; i++)
124 printf("%s:\n", rman->dm_desc);
126 /* print resources */
127 ia->indent = indent + 4;
128 devinfo_foreach_rman_resource(rman,
129 print_device_matching_resource, ia);
136 print_dev(struct devinfo_dev *dev)
139 printf("%s", dev->dd_name[0] ? dev->dd_name : "unknown");
140 if (vflag && *dev->dd_pnpinfo)
141 printf(" pnpinfo %s", dev->dd_pnpinfo);
142 if (vflag && *dev->dd_location)
143 printf(" at %s", dev->dd_location);
144 if (!(dev->dd_flags & DF_ENABLED))
145 printf(" (disabled)");
146 else if (dev->dd_flags & DF_SUSPENDED)
147 printf(" (suspended)");
152 * Print information about a device.
155 print_device(struct devinfo_dev *dev, void *arg)
157 struct indent_arg ia;
160 if (vflag || (dev->dd_name[0] != 0 && dev->dd_state >= DS_ATTACHED)) {
161 indent = (int)(intptr_t)arg;
162 for (i = 0; i < indent; i++)
167 ia.indent = indent + 4;
169 devinfo_foreach_rman(print_device_rman_resources,
174 return(devinfo_foreach_device_child(dev, print_device,
175 (void *)((char *)arg + 2)));
179 * Print information about a resource under a resource manager.
182 print_rman_resource(struct devinfo_res *res, void *arg __unused)
184 struct devinfo_dev *dev;
188 dev = devinfo_handle_to_device(res->dr_device);
189 if ((dev != NULL) && (dev->dd_name[0] != 0)) {
190 printf(" (%s)", dev->dd_name);
199 * Print information about a resource manager.
202 print_rman(struct devinfo_rman *rman, void *arg __unused)
204 printf("%s:\n", rman->dm_desc);
205 devinfo_foreach_rman_resource(rman, print_rman_resource, 0);
210 print_path(struct devinfo_dev *dev, void *xname)
212 const char *name = xname;
215 if (strcmp(dev->dd_name, name) == 0) {
222 rv = devinfo_foreach_device_child(dev, print_path, xname);
235 fprintf(stderr, "%s\n%s\n%s\n",
236 "usage: devinfo [-rv]",
238 " devifno -p dev [-v]");
243 main(int argc, char *argv[])
245 struct devinfo_dev *root;
250 while ((c = getopt(argc, argv, "p:ruv")) != -1) {
269 if (path && (rflag || uflag))
272 if ((rv = devinfo_init()) != 0) {
274 err(1, "devinfo_init");
277 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
278 errx(1, "can't find root device");
281 if (devinfo_foreach_device_child(root, print_path, (void *)path) == 0)
282 errx(1, "%s: Not found", path);
286 /* print resource usage? */
287 devinfo_foreach_rman(print_rman, NULL);
289 /* print device hierarchy */
290 devinfo_foreach_device_child(root, print_device, (void *)0);