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