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