xref: /netbsd-src/sys/kern/subr_device.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: subr_device.c,v 1.7 2021/02/06 05:33:20 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.7 2021/02/06 05:33:20 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 static 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  * Accessor functions for the device_t type.
110  */
111 
112 devclass_t
113 device_class(device_t dev)
114 {
115 
116 	return dev->dv_class;
117 }
118 
119 cfdata_t
120 device_cfdata(device_t dev)
121 {
122 
123 	return dev->dv_cfdata;
124 }
125 
126 cfdriver_t
127 device_cfdriver(device_t dev)
128 {
129 
130 	return dev->dv_cfdriver;
131 }
132 
133 cfattach_t
134 device_cfattach(device_t dev)
135 {
136 
137 	return dev->dv_cfattach;
138 }
139 
140 int
141 device_unit(device_t dev)
142 {
143 
144 	return dev->dv_unit;
145 }
146 
147 const char *
148 device_xname(device_t dev)
149 {
150 
151 	return dev->dv_xname;
152 }
153 
154 device_t
155 device_parent(device_t dev)
156 {
157 
158 	return dev->dv_parent;
159 }
160 
161 bool
162 device_activation(device_t dev, devact_level_t level)
163 {
164 	int active_flags;
165 
166 	active_flags = DVF_ACTIVE;
167 	switch (level) {
168 	case DEVACT_LEVEL_FULL:
169 		active_flags |= DVF_CLASS_SUSPENDED;
170 		/*FALLTHROUGH*/
171 	case DEVACT_LEVEL_DRIVER:
172 		active_flags |= DVF_DRIVER_SUSPENDED;
173 		/*FALLTHROUGH*/
174 	case DEVACT_LEVEL_BUS:
175 		active_flags |= DVF_BUS_SUSPENDED;
176 		break;
177 	}
178 
179 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
180 }
181 
182 bool
183 device_is_active(device_t dev)
184 {
185 	int active_flags;
186 
187 	active_flags = DVF_ACTIVE;
188 	active_flags |= DVF_CLASS_SUSPENDED;
189 	active_flags |= DVF_DRIVER_SUSPENDED;
190 	active_flags |= DVF_BUS_SUSPENDED;
191 
192 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
193 }
194 
195 bool
196 device_is_enabled(device_t dev)
197 {
198 	return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
199 }
200 
201 bool
202 device_has_power(device_t dev)
203 {
204 	int active_flags;
205 
206 	active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
207 
208 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
209 }
210 
211 int
212 device_locator(device_t dev, u_int locnum)
213 {
214 
215 	KASSERT(dev->dv_locators != NULL);
216 	return dev->dv_locators[locnum];
217 }
218 
219 void *
220 device_private(device_t dev)
221 {
222 
223 	/*
224 	 * The reason why device_private(NULL) is allowed is to simplify the
225 	 * work of a lot of userspace request handlers (i.e., c/bdev
226 	 * handlers) which grab cfdriver_t->cd_units[n].
227 	 * It avoids having them test for it to be NULL and only then calling
228 	 * device_private.
229 	 */
230 	return dev == NULL ? NULL : dev->dv_private;
231 }
232 
233 prop_dictionary_t
234 device_properties(device_t dev)
235 {
236 
237 	return dev->dv_properties;
238 }
239 
240 /*
241  * device_is_a:
242  *
243  *	Returns true if the device is an instance of the specified
244  *	driver.
245  */
246 bool
247 device_is_a(device_t dev, const char *dname)
248 {
249 	if (dev == NULL || dev->dv_cfdriver == NULL) {
250 		return false;
251 	}
252 
253 	return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
254 }
255 
256 /*
257  * device_attached_to_iattr:
258  *
259  *	Returns true if the device attached to the specified interface
260  *	attribute.
261  */
262 bool
263 device_attached_to_iattr(device_t dev, const char *iattr)
264 {
265 	cfdata_t cfdata = device_cfdata(dev);
266 	const struct cfparent *pspec;
267 
268 	if (cfdata == NULL || (pspec = cfdata->cf_pspec) == NULL) {
269 		return false;
270 	}
271 
272 	return strcmp(pspec->cfp_iattr, iattr) == 0;
273 }
274 
275 void
276 device_set_handle(device_t dev, devhandle_t handle)
277 {
278 	dev->dv_handle = handle;
279 }
280 
281 devhandle_t
282 device_handle(device_t dev)
283 {
284 	return dev->dv_handle;
285 }
286 
287 int
288 device_call(device_t dev, const char *name, void *arg)
289 {
290 	devhandle_t handle = device_handle(dev);
291 	device_call_t call;
292 	devhandle_t call_handle;
293 
294 	call = devhandle_lookup_device_call(handle, name, &call_handle);
295 	if (call == NULL) {
296 		return ENOTSUP;
297 	}
298 	return call(dev, call_handle, arg);
299 }
300 
301 int
302 device_enumerate_children(device_t dev,
303     bool (*callback)(device_t, devhandle_t, void *),
304     void *callback_arg)
305 {
306 	struct device_enumerate_children_args args = {
307 		.callback = callback,
308 		.callback_arg = callback_arg,
309 	};
310 
311 	return device_call(dev, "device-enumerate-children", &args);
312 }
313