xref: /freebsd/lib/libdevinfo/devinfo.h (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Michael Smith
5  * Copyright (c) 2000 BSDi
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *	$FreeBSD$
30  */
31 
32 #ifndef _DEVINFO_H_INCLUDED
33 #define _DEVINFO_H_INCLUDED
34 
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/bus.h>
38 
39 typedef __uintptr_t	devinfo_handle_t;
40 #define DEVINFO_ROOT_DEVICE	((devinfo_handle_t)0)
41 
42 typedef enum device_state devinfo_state_t;
43 
44 struct devinfo_dev {
45 	devinfo_handle_t	dd_handle;	/* device handle */
46 	devinfo_handle_t	dd_parent;	/* parent handle */
47 
48 	char			*dd_name;	/* name of device */
49 	char			*dd_desc;	/* device description */
50 	char			*dd_drivername;	/* name of attached driver*/
51 	char			*dd_pnpinfo;	/* pnp info from parent bus */
52 	char			*dd_location;	/* Where bus thinks dev at */
53 	uint32_t		dd_devflags;	/* API flags */
54 	uint16_t		dd_flags;	/* internal dev flags */
55 	devinfo_state_t		dd_state;	/* attachment state of dev */
56 };
57 
58 struct devinfo_rman {
59 	devinfo_handle_t	dm_handle;	/* resource manager handle */
60 
61 	rman_res_t		dm_start;	/* resource start */
62 	rman_res_t		dm_size;	/* resource size */
63 
64 	char			*dm_desc;	/* resource description */
65 };
66 
67 struct devinfo_res {
68 	devinfo_handle_t	dr_handle;	/* resource handle */
69 	devinfo_handle_t	dr_rman;	/* resource manager handle */
70 	devinfo_handle_t	dr_device;	/* owning device */
71 
72 	rman_res_t		dr_start;	/* region start */
73 	rman_res_t		dr_size;	/* region size */
74 	/* XXX add flags */
75 };
76 
77 __BEGIN_DECLS
78 
79 /*
80  * Acquire a coherent copy of the kernel's device and resource tables.
81  * This must return success (zero) before any other interfaces will
82  * function.  Sets errno on failure.
83  */
84 extern int	devinfo_init(void);
85 
86 /*
87  * Release the storage associated with the internal copy of the device
88  * and resource tables. devinfo_init must be called before any attempt
89  * is made to use any other interfaces.
90  */
91 extern void	devinfo_free(void);
92 
93 /*
94  * Find a device/resource/resource manager by its handle.
95  */
96 extern struct devinfo_dev
97 	*devinfo_handle_to_device(devinfo_handle_t handle);
98 extern struct devinfo_res
99 	*devinfo_handle_to_resource(devinfo_handle_t handle);
100 extern struct devinfo_rman
101 	*devinfo_handle_to_rman(devinfo_handle_t handle);
102 
103 /*
104  * Iterate over the children of a device, calling (fn) on each.  If
105  * (fn) returns nonzero, abort the scan and return.
106  */
107 extern int
108 	devinfo_foreach_device_child(struct devinfo_dev *parent,
109 	    int (* fn)(struct devinfo_dev *child, void *arg),
110 	    void *arg);
111 
112 /*
113  * Iterate over all the resources owned by a device, calling (fn) on each.
114  * If (fn) returns nonzero, abort the scan and return.
115  */
116 extern int
117 	devinfo_foreach_device_resource(struct devinfo_dev *dev,
118 	    int (* fn)(struct devinfo_dev *dev,
119 	    struct devinfo_res *res, void *arg),
120 	    void *arg);
121 
122 /*
123  * Iterate over all the resources owned by a resource manager, calling (fn)
124  * on each.  If (fn) returns nonzero, abort the scan and return.
125  */
126 extern int
127 	devinfo_foreach_rman_resource(struct devinfo_rman *rman,
128 	    int (* fn)(struct devinfo_res *res, void *arg),
129 	    void *arg);
130 
131 /*
132  * Iterate over all the resource managers, calling (fn) on each.  If (fn)
133  * returns nonzero, abort the scan and return.
134  */
135 extern int
136 	devinfo_foreach_rman(int (* fn)(struct devinfo_rman *rman, void *arg),
137 	    void *arg);
138 
139 __END_DECLS
140 
141 #endif /* ! _DEVINFO_H_INCLUDED */
142