xref: /freebsd/usr.sbin/devinfo/devinfo.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
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 
42 static void	print_resource(struct devinfo_res *);
43 static int	print_device_matching_resource(struct devinfo_res *, void *);
44 static int	print_device_rman_resources(struct devinfo_rman *, void *);
45 static int	print_device(struct devinfo_dev *, void *);
46 static int	print_rman_resource(struct devinfo_res *, void *);
47 static int	print_rman(struct devinfo_rman *, void *);
48 
49 struct indent_arg
50 {
51 	int	indent;
52 	void	*arg;
53 };
54 
55 /*
56  * Print a resource.
57  */
58 void
59 print_resource(struct devinfo_res *res)
60 {
61 	struct devinfo_rman	*rman;
62 	int			hexmode;
63 
64 	rman = devinfo_handle_to_rman(res->dr_rman);
65 	hexmode =  (rman->dm_size > 100) || (rman->dm_size == 0);
66 	printf(hexmode ? "0x%lx" : "%lu", res->dr_start);
67 	if (res->dr_size > 1)
68 		printf(hexmode ? "-0x%lx" : "-%lu",
69 		    res->dr_start + res->dr_size - 1);
70 }
71 
72 /*
73  * Print resource information if this resource matches the
74  * given device.
75  *
76  * If the given indent is 0, return an indicator that a matching
77  * resource exists.
78  */
79 int
80 print_device_matching_resource(struct devinfo_res *res, void *arg)
81 {
82 	struct indent_arg	*ia = (struct indent_arg *)arg;
83 	struct devinfo_dev	*dev = (struct devinfo_dev *)ia->arg;
84 	int			i;
85 
86 	if (devinfo_handle_to_device(res->dr_device) == dev) {
87 		/* in 'detect' mode, found a match */
88 		if (ia->indent == 0)
89 			return(1);
90 		for (i = 0; i < ia->indent; i++)
91 			printf(" ");
92 		print_resource(res);
93 		printf("\n");
94 	}
95 	return(0);
96 }
97 
98 /*
99  * Print resource information for this device and resource manager.
100  */
101 int
102 print_device_rman_resources(struct devinfo_rman *rman, void *arg)
103 {
104 	struct indent_arg	*ia = (struct indent_arg *)arg;
105 	struct devinfo_dev	*dev;
106 	int			indent, i;
107 
108 	indent = ia->indent;
109 	dev = (struct devinfo_dev *)ia->arg;
110 
111 	/* check whether there are any resources matching this device */
112 	ia->indent = 0;
113 	if (devinfo_foreach_rman_resource(rman,
114 	    print_device_matching_resource, ia) != 0) {
115 
116 		/* there are, print header */
117 		for (i = 0; i < indent; i++)
118 			printf(" ");
119 		printf("%s:\n", rman->dm_desc);
120 
121 		/* print resources */
122 		ia->indent = indent + 4;
123 		devinfo_foreach_rman_resource(rman,
124 		    print_device_matching_resource, ia);
125 	}
126 	ia->indent = indent;
127 	return(0);
128 }
129 
130 /*
131  * Print information about a device.
132  */
133 int
134 print_device(struct devinfo_dev *dev, void *arg)
135 {
136 	struct indent_arg	ia;
137 	int			i, indent;
138 
139 	if (dev->dd_name[0] != 0) {
140 		indent = (int)(intptr_t)arg;
141 		for (i = 0; i < indent; i++)
142 			printf(" ");
143 		printf("%s\n", dev->dd_name);
144 		if (rflag) {
145 			ia.indent = indent + 4;
146 			ia.arg = dev;
147 			devinfo_foreach_rman(print_device_rman_resources,
148 			    (void *)&ia);
149 		}
150 	}
151 
152 	return(devinfo_foreach_device_child(dev, print_device,
153 	    (void *)((char *)arg + 2)));
154 }
155 
156 /*
157  * Print information about a resource under a resource manager.
158  */
159 int
160 print_rman_resource(struct devinfo_res *res, void *arg __unused)
161 {
162 	struct devinfo_dev	*dev;
163 
164 	printf("    ");
165 	print_resource(res);
166 	dev = devinfo_handle_to_device(res->dr_device);
167 	if ((dev != NULL) && (dev->dd_name[0] != 0)) {
168 		printf(" (%s)", dev->dd_name);
169 	} else {
170 		printf(" ----");
171 	}
172 	printf("\n");
173 	return(0);
174 }
175 
176 /*
177  * Print information about a resource manager.
178  */
179 int
180 print_rman(struct devinfo_rman *rman, void *arg __unused)
181 {
182 	printf("%s:\n", rman->dm_desc);
183 	devinfo_foreach_rman_resource(rman, print_rman_resource, 0);
184 	return(0);
185 }
186 
187 int
188 main(int argc, char *argv[])
189 {
190 	struct devinfo_dev	*root;
191 	int			c, uflag;
192 
193 	uflag = 0;
194 	while ((c = getopt(argc, argv, "ru")) != -1) {
195 		switch(c) {
196 		case 'r':
197 			rflag++;
198 			break;
199 		case 'u':
200 			uflag++;
201 			break;
202 		default:
203 			errx(1, "usage: %s [-ru]", argv[0]);
204 		}
205 	}
206 
207 	if (devinfo_init())
208 		err(1, "devinfo_init");
209 
210 	if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
211 		errx(1, "can't find root device");
212 
213 	/* print resource usage? */
214 	if (uflag) {
215 		devinfo_foreach_rman(print_rman, NULL);
216 	} else {
217 		/* print device hierarchy */
218 		devinfo_foreach_device_child(root, print_device, (void *)0);
219 	}
220 	return(0);
221 }
222