subr_device.c revision 1.8.2.1 1 /* $NetBSD: subr_device.c,v 1.8.2.1 2021/09/11 17:22:36 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.8.2.1 2021/09/11 17:22:36 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35
36 /* Root device. */
37 device_t root_device;
38
39 /*
40 * devhandle_t accessors / mutators.
41 */
42
43 static bool
44 devhandle_is_valid_internal(const devhandle_t * const handlep)
45 {
46 if (handlep->impl == NULL) {
47 return false;
48 }
49 return handlep->impl->type != DEVHANDLE_TYPE_INVALID;
50 }
51
52 bool
53 devhandle_is_valid(devhandle_t handle)
54 {
55 return devhandle_is_valid_internal(&handle);
56 }
57
58 void
59 devhandle_invalidate(devhandle_t * const handlep)
60 {
61 handlep->impl = NULL;
62 handlep->uintptr = 0;
63 }
64
65 devhandle_type_t
66 devhandle_type(devhandle_t handle)
67 {
68 if (!devhandle_is_valid_internal(&handle)) {
69 return DEVHANDLE_TYPE_INVALID;
70 }
71
72 return handle.impl->type;
73 }
74
75 device_call_t
76 devhandle_lookup_device_call(devhandle_t handle, const char *name,
77 devhandle_t *call_handlep)
78 {
79 const struct devhandle_impl *impl;
80 device_call_t call;
81
82 /*
83 * The back-end can override the handle to use for the call,
84 * if needed.
85 */
86 *call_handlep = handle;
87
88 for (impl = handle.impl; impl != NULL; impl = impl->super) {
89 if (impl->lookup_device_call != NULL) {
90 call = impl->lookup_device_call(handle, name,
91 call_handlep);
92 if (call != NULL) {
93 return call;
94 }
95 }
96 }
97 return NULL;
98 }
99
100 void
101 devhandle_impl_inherit(struct devhandle_impl *impl,
102 const struct devhandle_impl *super)
103 {
104 memcpy(impl, super, sizeof(*impl));
105 impl->super = super;
106 }
107
108 /*
109 * Helper function that provides a short-hand method of the common
110 * "subclass a device handle" flow.
111 */
112 devhandle_t
113 devhandle_subclass(devhandle_t handle,
114 struct devhandle_impl *new_impl,
115 device_call_t (*new_lookup)(devhandle_t, const char *, devhandle_t *))
116 {
117 devhandle_impl_inherit(new_impl, handle.impl);
118 new_impl->lookup_device_call = new_lookup;
119 handle.impl = new_impl;
120
121 return handle;
122 }
123
124 /*
125 * Accessor functions for the device_t type.
126 */
127
128 devclass_t
129 device_class(device_t dev)
130 {
131
132 return dev->dv_class;
133 }
134
135 cfdata_t
136 device_cfdata(device_t dev)
137 {
138
139 return dev->dv_cfdata;
140 }
141
142 cfdriver_t
143 device_cfdriver(device_t dev)
144 {
145
146 return dev->dv_cfdriver;
147 }
148
149 cfattach_t
150 device_cfattach(device_t dev)
151 {
152
153 return dev->dv_cfattach;
154 }
155
156 int
157 device_unit(device_t dev)
158 {
159
160 return dev->dv_unit;
161 }
162
163 const char *
164 device_xname(device_t dev)
165 {
166
167 return dev->dv_xname;
168 }
169
170 device_t
171 device_parent(device_t dev)
172 {
173
174 return dev->dv_parent;
175 }
176
177 bool
178 device_activation(device_t dev, devact_level_t level)
179 {
180 int active_flags;
181
182 active_flags = DVF_ACTIVE;
183 switch (level) {
184 case DEVACT_LEVEL_FULL:
185 active_flags |= DVF_CLASS_SUSPENDED;
186 /*FALLTHROUGH*/
187 case DEVACT_LEVEL_DRIVER:
188 active_flags |= DVF_DRIVER_SUSPENDED;
189 /*FALLTHROUGH*/
190 case DEVACT_LEVEL_BUS:
191 active_flags |= DVF_BUS_SUSPENDED;
192 break;
193 }
194
195 return (dev->dv_flags & active_flags) == DVF_ACTIVE;
196 }
197
198 bool
199 device_is_active(device_t dev)
200 {
201 int active_flags;
202
203 active_flags = DVF_ACTIVE;
204 active_flags |= DVF_CLASS_SUSPENDED;
205 active_flags |= DVF_DRIVER_SUSPENDED;
206 active_flags |= DVF_BUS_SUSPENDED;
207
208 return (dev->dv_flags & active_flags) == DVF_ACTIVE;
209 }
210
211 bool
212 device_is_enabled(device_t dev)
213 {
214 return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
215 }
216
217 bool
218 device_has_power(device_t dev)
219 {
220 int active_flags;
221
222 active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
223
224 return (dev->dv_flags & active_flags) == DVF_ACTIVE;
225 }
226
227 int
228 device_locator(device_t dev, u_int locnum)
229 {
230
231 KASSERT(dev->dv_locators != NULL);
232 return dev->dv_locators[locnum];
233 }
234
235 void *
236 device_private(device_t dev)
237 {
238
239 /*
240 * The reason why device_private(NULL) is allowed is to simplify the
241 * work of a lot of userspace request handlers (i.e., c/bdev
242 * handlers) which grab cfdriver_t->cd_units[n].
243 * It avoids having them test for it to be NULL and only then calling
244 * device_private.
245 */
246 return dev == NULL ? NULL : dev->dv_private;
247 }
248
249 prop_dictionary_t
250 device_properties(device_t dev)
251 {
252
253 return dev->dv_properties;
254 }
255
256 /*
257 * device_is_a:
258 *
259 * Returns true if the device is an instance of the specified
260 * driver.
261 */
262 bool
263 device_is_a(device_t dev, const char *dname)
264 {
265 if (dev == NULL || dev->dv_cfdriver == NULL) {
266 return false;
267 }
268
269 return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
270 }
271
272 /*
273 * device_attached_to_iattr:
274 *
275 * Returns true if the device attached to the specified interface
276 * attribute.
277 */
278 bool
279 device_attached_to_iattr(device_t dev, const char *iattr)
280 {
281 cfdata_t cfdata = device_cfdata(dev);
282 const struct cfparent *pspec;
283
284 if (cfdata == NULL || (pspec = cfdata->cf_pspec) == NULL) {
285 return false;
286 }
287
288 return strcmp(pspec->cfp_iattr, iattr) == 0;
289 }
290
291 void
292 device_set_handle(device_t dev, devhandle_t handle)
293 {
294 dev->dv_handle = handle;
295 }
296
297 devhandle_t
298 device_handle(device_t dev)
299 {
300 return dev->dv_handle;
301 }
302
303 int
304 device_call(device_t dev, const char *name, void *arg)
305 {
306 devhandle_t handle = device_handle(dev);
307 device_call_t call;
308 devhandle_t call_handle;
309
310 call = devhandle_lookup_device_call(handle, name, &call_handle);
311 if (call == NULL) {
312 return ENOTSUP;
313 }
314 return call(dev, call_handle, arg);
315 }
316
317 int
318 device_enumerate_children(device_t dev,
319 bool (*callback)(device_t, devhandle_t, void *),
320 void *callback_arg)
321 {
322 struct device_enumerate_children_args args = {
323 .callback = callback,
324 .callback_arg = callback_arg,
325 };
326
327 return device_call(dev, "device-enumerate-children", &args);
328 }
329