Merge branch 'vendor/DHCPCD'
[dragonfly.git] / usr.sbin / devinfo / devinfo.c
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: 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 $
29  */
30
31 /*
32  * Print information about system device configuration.
33  */
34
35 #include <sys/types.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include "devinfo.h"
41
42 int     rflag;
43 int     vflag;
44
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 *);
51
52 struct indent_arg
53 {
54         int     indent;
55         void    *arg;
56 };
57
58 /*
59  * Print a resource.
60  */
61 void
62 print_resource(struct devinfo_res *res)
63 {
64         struct devinfo_rman     *rman;
65         int                     hexmode;
66
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);
70         if (res->dr_size > 1)
71                 printf(hexmode ? "-0x%lx" : "-%lu",
72                     res->dr_start + res->dr_size - 1);
73 }
74
75 /*
76  * Print resource information if this resource matches the
77  * given device.
78  *
79  * If the given indent is 0, return an indicator that a matching
80  * resource exists.
81  */
82 int
83 print_device_matching_resource(struct devinfo_res *res, void *arg)
84 {
85         struct indent_arg       *ia = (struct indent_arg *)arg;
86         struct devinfo_dev      *dev = (struct devinfo_dev *)ia->arg;
87         int                     i;
88
89         if (devinfo_handle_to_device(res->dr_device) == dev) {
90                 /* in 'detect' mode, found a match */
91                 if (ia->indent == 0)
92                         return(1);
93                 for (i = 0; i < ia->indent; i++)
94                         printf(" ");
95                 print_resource(res);
96                 printf("\n");
97         }
98         return(0);
99 }
100
101 /*
102  * Print resource information for this device and resource manager.
103  */
104 int
105 print_device_rman_resources(struct devinfo_rman *rman, void *arg)
106 {
107         struct indent_arg       *ia = (struct indent_arg *)arg;
108         int                     indent, i;
109
110         indent = ia->indent;
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 > 1 ||
141             (vflag && (dev->dd_flags & DIF_ENABLED)) ||
142             (dev->dd_name[0] != 0 && dev->dd_state >= DIS_INPROGRESS)) {
143                 indent = (int)(intptr_t)arg;
144                 for (i = 0; i < indent; i++)
145                         printf(" ");
146                 if (vflag && (dev->dd_flags & DIF_ENABLED) == 0) {
147                         if (dev->dd_name[0])
148                                 printf("%s", dev->dd_name);
149                         printf("(disabled)");
150                 } else {
151                         printf("%s", dev->dd_name[0] ? dev->dd_name : "unknown");
152                 }
153                 if (vflag && *dev->dd_pnpinfo)
154                         printf(" pnpinfo %s", dev->dd_pnpinfo);
155                 if (vflag && *dev->dd_location)
156                         printf(" at %s", dev->dd_location);
157                 printf("\n");
158                 if (rflag) {
159                         ia.indent = indent + 4;
160                         ia.arg = dev;
161                         devinfo_foreach_rman(print_device_rman_resources,
162                             (void *)&ia);
163                 }
164         }
165
166         return(devinfo_foreach_device_child(dev, print_device,
167             (void *)((char *)arg + 2)));
168 }
169
170 /*
171  * Print information about a resource under a resource manager.
172  */
173 int
174 print_rman_resource(struct devinfo_res *res, void *arg __unused)
175 {
176         struct devinfo_dev      *dev;
177         
178         printf("    ");
179         print_resource(res);
180         dev = devinfo_handle_to_device(res->dr_device);
181         if ((dev != NULL) && (dev->dd_name[0] != 0)) {
182                 printf(" (%s)", dev->dd_name);
183         } else {
184                 printf(" ----");
185         }
186         printf("\n");
187         return(0);
188 }
189
190 /*
191  * Print information about a resource manager.
192  */
193 int
194 print_rman(struct devinfo_rman *rman, void *arg __unused)
195 {
196         printf("%s:\n", rman->dm_desc);
197         devinfo_foreach_rman_resource(rman, print_rman_resource, 0);
198         return(0);
199 }
200
201 int
202 main(int argc, char *argv[]) 
203 {
204         struct devinfo_dev      *root;
205         int                     c, uflag;
206
207         uflag = 0;
208         while ((c = getopt(argc, argv, "ruv")) != -1) {
209                 switch(c) {
210                 case 'r':
211                         rflag++;
212                         break;
213                 case 'u':
214                         uflag++;
215                         break;
216                 case 'v':
217                         vflag++;
218                         break;
219                 default:
220                         fprintf(stderr, "%s\n%s\n",
221                             "usage: devinfo [-rv]",
222                             "       devinfo -u");
223                         exit(1);
224                 }
225         }
226
227         if (devinfo_init())
228                 err(1, "devinfo_init");
229
230         if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
231                 errx(1, "can't find root device");
232
233         /* print resource usage? */
234         if (uflag) {
235                 devinfo_foreach_rman(print_rman, NULL);
236         } else {
237                 /* print device hierarchy */
238                 devinfo_foreach_device_child(root, print_device, NULL);
239         }
240         return(0);
241 }