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