xref: /netbsd-src/sys/kern/subr_device.c (revision f0fde9902fd4d72ded2807793acc7bfaa1ebf243)
1 /*	$NetBSD: subr_device.c,v 1.11 2022/01/22 11:58:15 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.11 2022/01/22 11:58:15 thorpej Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35 
36 #include <sys/device_calls.h>
37 
38 /* Root device. */
39 device_t			root_device;
40 
41 /*
42  * devhandle_t accessors / mutators.
43  */
44 
45 static bool
46 devhandle_is_valid_internal(const devhandle_t * const handlep)
47 {
48 	if (handlep->impl == NULL) {
49 		return false;
50 	}
51 	return handlep->impl->type != DEVHANDLE_TYPE_INVALID;
52 }
53 
54 bool
55 devhandle_is_valid(devhandle_t handle)
56 {
57 	return devhandle_is_valid_internal(&handle);
58 }
59 
60 devhandle_t
61 devhandle_invalid(void)
62 {
63 	static const devhandle_t invalid_devhandle = {
64 		.impl = NULL,
65 		.uintptr = 0,
66 	};
67 	return invalid_devhandle;
68 }
69 
70 devhandle_type_t
71 devhandle_type(devhandle_t handle)
72 {
73 	if (!devhandle_is_valid_internal(&handle)) {
74 		return DEVHANDLE_TYPE_INVALID;
75 	}
76 
77 	return handle.impl->type;
78 }
79 
80 int
81 devhandle_compare(devhandle_t handle1, devhandle_t handle2)
82 {
83 	devhandle_type_t type1 = devhandle_type(handle1);
84 	devhandle_type_t type2 = devhandle_type(handle2);
85 
86 	if (type1 == DEVHANDLE_TYPE_INVALID) {
87 		return -1;
88 	}
89 	if (type2 == DEVHANDLE_TYPE_INVALID) {
90 		return 1;
91 	}
92 
93 	if (type1 < type2) {
94 		return -1;
95 	}
96 	if (type1 > type2) {
97 		return 1;
98 	}
99 
100 	/* For private handles, we also compare the impl pointers. */
101 	if (type1 == DEVHANDLE_TYPE_PRIVATE) {
102 		intptr_t impl1 = (intptr_t)handle1.impl;
103 		intptr_t impl2 = (intptr_t)handle2.impl;
104 
105 		if (impl1 < impl2) {
106 			return -1;
107 		}
108 		if (impl1 > impl2) {
109 			return 1;
110 		}
111 	}
112 
113 	if (handle1.integer < handle2.integer) {
114 		return -1;
115 	}
116 	if (handle1.integer > handle2.integer) {
117 		return 1;
118 	}
119 
120 	return 0;
121 }
122 
123 device_call_t
124 devhandle_lookup_device_call(devhandle_t handle, const char *name,
125     devhandle_t *call_handlep)
126 {
127 	const struct devhandle_impl *impl;
128 	device_call_t call;
129 
130 	/*
131 	 * The back-end can override the handle to use for the call,
132 	 * if needed.
133 	 */
134 	*call_handlep = handle;
135 
136 	for (impl = handle.impl; impl != NULL; impl = impl->super) {
137 		if (impl->lookup_device_call != NULL) {
138 			call = impl->lookup_device_call(handle, name,
139 			    call_handlep);
140 			if (call != NULL) {
141 				return call;
142 			}
143 		}
144 	}
145 	return NULL;
146 }
147 
148 void
149 devhandle_impl_inherit(struct devhandle_impl *impl,
150     const struct devhandle_impl *super)
151 {
152 	memcpy(impl, super, sizeof(*impl));
153 	impl->super = super;
154 }
155 
156 /*
157  * Accessor functions for the device_t type.
158  */
159 
160 devclass_t
161 device_class(device_t dev)
162 {
163 
164 	return dev->dv_class;
165 }
166 
167 cfdata_t
168 device_cfdata(device_t dev)
169 {
170 
171 	return dev->dv_cfdata;
172 }
173 
174 cfdriver_t
175 device_cfdriver(device_t dev)
176 {
177 
178 	return dev->dv_cfdriver;
179 }
180 
181 cfattach_t
182 device_cfattach(device_t dev)
183 {
184 
185 	return dev->dv_cfattach;
186 }
187 
188 int
189 device_unit(device_t dev)
190 {
191 
192 	return dev->dv_unit;
193 }
194 
195 const char *
196 device_xname(device_t dev)
197 {
198 
199 	return dev->dv_xname;
200 }
201 
202 device_t
203 device_parent(device_t dev)
204 {
205 
206 	return dev->dv_parent;
207 }
208 
209 bool
210 device_activation(device_t dev, devact_level_t level)
211 {
212 	int active_flags;
213 
214 	active_flags = DVF_ACTIVE;
215 	switch (level) {
216 	case DEVACT_LEVEL_FULL:
217 		active_flags |= DVF_CLASS_SUSPENDED;
218 		/*FALLTHROUGH*/
219 	case DEVACT_LEVEL_DRIVER:
220 		active_flags |= DVF_DRIVER_SUSPENDED;
221 		/*FALLTHROUGH*/
222 	case DEVACT_LEVEL_BUS:
223 		active_flags |= DVF_BUS_SUSPENDED;
224 		break;
225 	}
226 
227 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
228 }
229 
230 bool
231 device_is_active(device_t dev)
232 {
233 	int active_flags;
234 
235 	active_flags = DVF_ACTIVE;
236 	active_flags |= DVF_CLASS_SUSPENDED;
237 	active_flags |= DVF_DRIVER_SUSPENDED;
238 	active_flags |= DVF_BUS_SUSPENDED;
239 
240 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
241 }
242 
243 bool
244 device_is_enabled(device_t dev)
245 {
246 	return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
247 }
248 
249 bool
250 device_has_power(device_t dev)
251 {
252 	int active_flags;
253 
254 	active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
255 
256 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
257 }
258 
259 int
260 device_locator(device_t dev, u_int locnum)
261 {
262 
263 	KASSERT(dev->dv_locators != NULL);
264 	return dev->dv_locators[locnum];
265 }
266 
267 void *
268 device_private(device_t dev)
269 {
270 
271 	/*
272 	 * The reason why device_private(NULL) is allowed is to simplify the
273 	 * work of a lot of userspace request handlers (i.e., c/bdev
274 	 * handlers) which grab cfdriver_t->cd_units[n].
275 	 * It avoids having them test for it to be NULL and only then calling
276 	 * device_private.
277 	 */
278 	return dev == NULL ? NULL : dev->dv_private;
279 }
280 
281 prop_dictionary_t
282 device_properties(device_t dev)
283 {
284 
285 	return dev->dv_properties;
286 }
287 
288 /*
289  * device_is_a:
290  *
291  *	Returns true if the device is an instance of the specified
292  *	driver.
293  */
294 bool
295 device_is_a(device_t dev, const char *dname)
296 {
297 	if (dev == NULL || dev->dv_cfdriver == NULL) {
298 		return false;
299 	}
300 
301 	return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
302 }
303 
304 /*
305  * device_attached_to_iattr:
306  *
307  *	Returns true if the device attached to the specified interface
308  *	attribute.
309  */
310 bool
311 device_attached_to_iattr(device_t dev, const char *iattr)
312 {
313 	cfdata_t cfdata = device_cfdata(dev);
314 	const struct cfparent *pspec;
315 
316 	if (cfdata == NULL || (pspec = cfdata->cf_pspec) == NULL) {
317 		return false;
318 	}
319 
320 	return strcmp(pspec->cfp_iattr, iattr) == 0;
321 }
322 
323 void
324 device_set_handle(device_t dev, devhandle_t handle)
325 {
326 	dev->dv_handle = handle;
327 }
328 
329 devhandle_t
330 device_handle(device_t dev)
331 {
332 	return dev->dv_handle;
333 }
334 
335 int
336 device_call_generic(device_t dev, const struct device_call_generic *gen)
337 {
338 	devhandle_t handle = device_handle(dev);
339 	device_call_t call;
340 	devhandle_t call_handle;
341 
342 	call = devhandle_lookup_device_call(handle, gen->name, &call_handle);
343 	if (call == NULL) {
344 		return ENOTSUP;
345 	}
346 	return call(dev, call_handle, gen->args);
347 }
348 
349 int
350 device_enumerate_children(device_t dev,
351     bool (*callback)(device_t, devhandle_t, void *),
352     void *callback_arg)
353 {
354 	struct device_enumerate_children_args args = {
355 		.callback = callback,
356 		.callback_arg = callback_arg,
357 	};
358 
359 	return device_call(dev, DEVICE_ENUMERATE_CHILDREN(&args));
360 }
361