1 /* $NetBSD: kern_drvctl.c,v 1.54 2026/11/16 20:30:07 riastradh Exp $ */ 2 3 /* 4 * Copyright (c) 2004 5 * Matthias Drochner. 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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.54 2026/11/16 20:30:07 riastradh Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/types.h> 34 35 #include <sys/conf.h> 36 #include <sys/device.h> 37 #include <sys/devmon.h> 38 #include <sys/drvctlio.h> 39 #include <sys/event.h> 40 #include <sys/fcntl.h> 41 #include <sys/file.h> 42 #include <sys/filedesc.h> 43 #include <sys/ioctl.h> 44 #include <sys/kauth.h> 45 #include <sys/kernel.h> 46 #include <sys/kmem.h> 47 #include <sys/lwp.h> 48 #include <sys/module.h> 49 #include <sys/poll.h> 50 #include <sys/sdt.h> 51 #include <sys/select.h> 52 #include <sys/stat.h> 53 #include <sys/systm.h> 54 55 #include "ioconf.h" 56 57 struct drvctl_event { 58 TAILQ_ENTRY(drvctl_event) dce_link; 59 prop_dictionary_t dce_event; 60 }; 61 62 TAILQ_HEAD(drvctl_queue, drvctl_event); 63 64 static struct drvctl_queue drvctl_eventq; /* FIFO */ 65 static kcondvar_t drvctl_cond; 66 static kmutex_t drvctl_lock; 67 static int drvctl_nopen = 0, drvctl_eventcnt = 0; 68 static struct selinfo drvctl_rdsel; 69 70 #define DRVCTL_EVENTQ_DEPTH 64 /* arbitrary queue limit */ 71 72 dev_type_open(drvctlopen); 73 74 const struct cdevsw drvctl_cdevsw = { 75 .d_open = drvctlopen, 76 .d_close = nullclose, 77 .d_read = nullread, 78 .d_write = nullwrite, 79 .d_ioctl = noioctl, 80 .d_stop = nostop, 81 .d_tty = notty, 82 .d_poll = nopoll, 83 .d_mmap = nommap, 84 .d_kqfilter = nokqfilter, 85 .d_discard = nodiscard, 86 .d_flag = D_OTHER 87 }; 88 89 static int drvctl_read(struct file *, off_t *, struct uio *, 90 kauth_cred_t, int); 91 static int drvctl_write(struct file *, off_t *, struct uio *, 92 kauth_cred_t, int); 93 static int drvctl_ioctl(struct file *, u_long, void *); 94 static int drvctl_poll(struct file *, int); 95 static int drvctl_stat(struct file *, struct stat *); 96 static int drvctl_close(struct file *); 97 98 static const struct fileops drvctl_fileops = { 99 .fo_name = "drvctl", 100 .fo_read = drvctl_read, 101 .fo_write = drvctl_write, 102 .fo_ioctl = drvctl_ioctl, 103 .fo_fcntl = fnullop_fcntl, 104 .fo_poll = drvctl_poll, 105 .fo_stat = drvctl_stat, 106 .fo_close = drvctl_close, 107 .fo_kqfilter = fnullop_kqfilter, 108 .fo_restart = fnullop_restart, 109 }; 110 111 #define MAXLOCATORS 100 112 113 static int (*saved_insert_vec)(const char *, prop_dictionary_t) = NULL; 114 115 static int drvctl_command(struct lwp *, struct plistref *, u_long, int); 116 static int drvctl_getevent(struct lwp *, struct plistref *, u_long, int); 117 118 void 119 drvctl_init(void) 120 { 121 TAILQ_INIT(&drvctl_eventq); 122 mutex_init(&drvctl_lock, MUTEX_DEFAULT, IPL_NONE); 123 cv_init(&drvctl_cond, "devmon"); 124 selinit(&drvctl_rdsel); 125 } 126 127 void 128 drvctl_fini(void) 129 { 130 131 seldestroy(&drvctl_rdsel); 132 cv_destroy(&drvctl_cond); 133 mutex_destroy(&drvctl_lock); 134 } 135 136 int 137 devmon_insert(const char *event, prop_dictionary_t ev) 138 { 139 struct drvctl_event *dce, *odce; 140 141 mutex_enter(&drvctl_lock); 142 143 if (drvctl_nopen == 0) { 144 prop_object_release(ev); 145 mutex_exit(&drvctl_lock); 146 return 0; 147 } 148 149 /* Fill in mandatory member */ 150 if (!prop_dictionary_set_string_nocopy(ev, "event", event)) { 151 prop_object_release(ev); 152 mutex_exit(&drvctl_lock); 153 return 0; 154 } 155 156 dce = kmem_alloc(sizeof(*dce), KM_SLEEP); 157 dce->dce_event = ev; 158 159 if (drvctl_eventcnt == DRVCTL_EVENTQ_DEPTH) { 160 odce = TAILQ_FIRST(&drvctl_eventq); 161 TAILQ_REMOVE(&drvctl_eventq, odce, dce_link); 162 prop_object_release(odce->dce_event); 163 kmem_free(odce, sizeof(*odce)); 164 --drvctl_eventcnt; 165 } 166 167 TAILQ_INSERT_TAIL(&drvctl_eventq, dce, dce_link); 168 ++drvctl_eventcnt; 169 cv_broadcast(&drvctl_cond); 170 selnotify(&drvctl_rdsel, 0, 0); 171 172 mutex_exit(&drvctl_lock); 173 return 0; 174 } 175 176 int 177 drvctlopen(dev_t dev, int flags, int mode, struct lwp *l) 178 { 179 struct file *fp; 180 int fd; 181 int ret; 182 183 ret = fd_allocfile(&fp, &fd); 184 if (ret) 185 return ret; 186 187 /* XXX setup context */ 188 mutex_enter(&drvctl_lock); 189 ret = fd_clone(fp, fd, flags, &drvctl_fileops, /* context */NULL); 190 ++drvctl_nopen; 191 mutex_exit(&drvctl_lock); 192 193 return ret; 194 } 195 196 static int 197 pmdevbyname(u_long cmd, struct devpmargs *a) 198 { 199 device_t d; 200 201 KASSERT(KERNEL_LOCKED_P()); 202 203 if ((d = device_find_by_xname(a->devname)) == NULL) 204 return SET_ERROR(ENXIO); 205 206 switch (cmd) { 207 case DRVSUSPENDDEV: 208 return pmf_device_recursive_suspend(d, PMF_Q_DRVCTL) 209 ? 0 : SET_ERROR(EBUSY); 210 case DRVRESUMEDEV: 211 if (a->flags & DEVPM_F_SUBTREE) { 212 return pmf_device_subtree_resume(d, PMF_Q_DRVCTL) 213 ? 0 : SET_ERROR(EBUSY); 214 } else { 215 return pmf_device_recursive_resume(d, PMF_Q_DRVCTL) 216 ? 0 : SET_ERROR(EBUSY); 217 } 218 default: 219 return SET_ERROR(EPASSTHROUGH); 220 } 221 } 222 223 static int 224 listdevbyname(struct devlistargs *l) 225 { 226 device_t d, child; 227 deviter_t di; 228 int cnt = 0, idx, error = 0; 229 230 KASSERT(KERNEL_LOCKED_P()); 231 232 if (*l->l_devname == '\0') 233 d = NULL; 234 else if (memchr(l->l_devname, 0, sizeof(l->l_devname)) == NULL) 235 return SET_ERROR(EINVAL); 236 else if ((d = device_find_by_xname(l->l_devname)) == NULL) 237 return SET_ERROR(ENXIO); 238 239 for (child = deviter_first(&di, 0); child != NULL; 240 child = deviter_next(&di)) { 241 if (device_parent(child) != d) 242 continue; 243 idx = cnt++; 244 if (l->l_childname == NULL || idx >= l->l_children) 245 continue; 246 error = copyoutstr(device_xname(child), l->l_childname[idx], 247 sizeof(l->l_childname[idx]), NULL); 248 if (error != 0) 249 break; 250 } 251 deviter_release(&di); 252 253 l->l_children = cnt; 254 return error; 255 } 256 257 static int 258 detachdevbyname(const char *devname) 259 { 260 device_t d; 261 deviter_t di; 262 int error; 263 264 KASSERT(KERNEL_LOCKED_P()); 265 266 for (d = deviter_first_acquire(&di, DEVITER_F_RW); 267 d != NULL; 268 device_release(d), d = deviter_next_acquire(&di)) { 269 if (strcmp(device_xname(d), devname) == 0) 270 break; 271 } 272 if (d == NULL) { 273 error = SET_ERROR(ENXIO); 274 goto out; 275 } 276 277 #ifndef XXXFULLRISK 278 /* 279 * If the parent cannot be notified, it might keep 280 * pointers to the detached device. 281 * There might be a private notification mechanism, 282 * but better play it safe here. 283 */ 284 if (device_parent(d) && 285 !device_cfattach(device_parent(d))->ca_childdetached) { 286 error = SET_ERROR(ENOTSUP); 287 device_release(d); 288 goto out; 289 } 290 #endif 291 292 error = config_detach_release(d, 0); 293 out: deviter_release(&di); 294 return error; 295 } 296 297 static int 298 rescanbus(const char *busname, const char *ifattr, 299 int numlocators, const int *locators) 300 { 301 int i, rc; 302 device_t d; 303 const struct cfiattrdata * const *ap; 304 305 KASSERT(KERNEL_LOCKED_P()); 306 307 /* XXX there should be a way to get limits and defaults (per device) 308 from config generated data */ 309 int locs[MAXLOCATORS]; 310 for (i = 0; i < MAXLOCATORS; i++) 311 locs[i] = -1; 312 313 for (i = 0; i < numlocators;i++) 314 locs[i] = locators[i]; 315 316 if ((d = device_find_by_xname(busname)) == NULL) 317 return SET_ERROR(ENXIO); 318 319 /* 320 * must support rescan, and must have something 321 * to attach to 322 */ 323 if (!device_cfattach(d)->ca_rescan || 324 !device_cfdriver(d)->cd_attrs) 325 return SET_ERROR(ENODEV); 326 327 /* rescan all ifattrs if none is specified */ 328 if (!ifattr) { 329 rc = 0; 330 for (ap = device_cfdriver(d)->cd_attrs; *ap; ap++) { 331 rc = (*device_cfattach(d)->ca_rescan)(d, 332 (*ap)->ci_name, locs); 333 if (rc) 334 break; 335 } 336 } else { 337 /* check for valid attribute passed */ 338 for (ap = device_cfdriver(d)->cd_attrs; *ap; ap++) 339 if (!strcmp((*ap)->ci_name, ifattr)) 340 break; 341 if (!*ap) 342 return SET_ERROR(EINVAL); 343 rc = (*device_cfattach(d)->ca_rescan)(d, ifattr, locs); 344 } 345 346 config_deferred(NULL); 347 return rc; 348 } 349 350 static int 351 drvctl_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred, 352 int flags) 353 { 354 return SET_ERROR(ENODEV); 355 } 356 357 static int 358 drvctl_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred, 359 int flags) 360 { 361 return SET_ERROR(ENODEV); 362 } 363 364 static int 365 drvctl_ioctl(struct file *fp, u_long cmd, void *data) 366 { 367 int res; 368 char *ifattr; 369 int *locs; 370 size_t locs_sz = 0; /* XXXgcc */ 371 372 KERNEL_LOCK(1, NULL); 373 switch (cmd) { 374 case DRVSUSPENDDEV: 375 case DRVRESUMEDEV: 376 #define d ((struct devpmargs *)data) 377 res = pmdevbyname(cmd, d); 378 #undef d 379 break; 380 case DRVLISTDEV: 381 res = listdevbyname((struct devlistargs *)data); 382 break; 383 case DRVDETACHDEV: 384 #define d ((struct devdetachargs *)data) 385 res = detachdevbyname(d->devname); 386 #undef d 387 break; 388 case DRVRESCANBUS: 389 #define d ((struct devrescanargs *)data) 390 d->busname[sizeof(d->busname) - 1] = '\0'; 391 392 /* XXX better copyin? */ 393 if (d->ifattr[0]) { 394 d->ifattr[sizeof(d->ifattr) - 1] = '\0'; 395 ifattr = d->ifattr; 396 } else 397 ifattr = 0; 398 399 if (d->numlocators) { 400 if (d->numlocators > MAXLOCATORS) { 401 res = SET_ERROR(EINVAL); 402 goto out; 403 } 404 locs_sz = d->numlocators * sizeof(int); 405 locs = kmem_alloc(locs_sz, KM_SLEEP); 406 res = copyin(d->locators, locs, locs_sz); 407 if (res) { 408 kmem_free(locs, locs_sz); 409 goto out; 410 } 411 } else 412 locs = NULL; 413 res = rescanbus(d->busname, ifattr, d->numlocators, locs); 414 if (locs) 415 kmem_free(locs, locs_sz); 416 #undef d 417 break; 418 case DRVCTLCOMMAND: 419 res = drvctl_command(curlwp, (struct plistref *)data, cmd, 420 fp->f_flag); 421 break; 422 case DRVGETEVENT: 423 res = drvctl_getevent(curlwp, (struct plistref *)data, cmd, 424 fp->f_flag); 425 break; 426 default: 427 res = SET_ERROR(EPASSTHROUGH); 428 break; 429 } 430 out: KERNEL_UNLOCK_ONE(NULL); 431 return res; 432 } 433 434 static int 435 drvctl_stat(struct file *fp, struct stat *st) 436 { 437 (void)memset(st, 0, sizeof(*st)); 438 st->st_uid = kauth_cred_geteuid(fp->f_cred); 439 st->st_gid = kauth_cred_getegid(fp->f_cred); 440 return 0; 441 } 442 443 static int 444 drvctl_poll(struct file *fp, int events) 445 { 446 int revents = 0; 447 448 if (!TAILQ_EMPTY(&drvctl_eventq)) 449 revents |= events & (POLLIN | POLLRDNORM); 450 else 451 selrecord(curlwp, &drvctl_rdsel); 452 453 return revents; 454 } 455 456 static int 457 drvctl_close(struct file *fp) 458 { 459 struct drvctl_event *dce; 460 461 /* XXX free context */ 462 mutex_enter(&drvctl_lock); 463 KASSERT(drvctl_nopen > 0); 464 --drvctl_nopen; 465 if (drvctl_nopen == 0) { 466 /* flush queue */ 467 while ((dce = TAILQ_FIRST(&drvctl_eventq)) != NULL) { 468 TAILQ_REMOVE(&drvctl_eventq, dce, dce_link); 469 KASSERT(drvctl_eventcnt > 0); 470 --drvctl_eventcnt; 471 prop_object_release(dce->dce_event); 472 kmem_free(dce, sizeof(*dce)); 473 } 474 } 475 mutex_exit(&drvctl_lock); 476 477 return 0; 478 } 479 480 void 481 drvctlattach(int arg __unused) 482 { 483 } 484 485 /***************************************************************************** 486 * Driver control command processing engine 487 *****************************************************************************/ 488 489 static int 490 drvctl_command_get_properties(struct lwp *l, 491 prop_dictionary_t command_dict, 492 prop_dictionary_t results_dict) 493 { 494 prop_dictionary_t args_dict; 495 prop_string_t devname_string; 496 device_t dev; 497 deviter_t di; 498 499 args_dict = prop_dictionary_get(command_dict, "drvctl-arguments"); 500 if (args_dict == NULL) 501 return SET_ERROR(EINVAL); 502 503 devname_string = prop_dictionary_get(args_dict, "device-name"); 504 if (devname_string == NULL) 505 return SET_ERROR(EINVAL); 506 507 for (dev = deviter_first(&di, 0); dev != NULL; 508 dev = deviter_next(&di)) { 509 if (prop_string_equals_string(devname_string, 510 device_xname(dev))) { 511 prop_dictionary_set(results_dict, "drvctl-result-data", 512 device_properties(dev)); 513 break; 514 } 515 } 516 517 deviter_release(&di); 518 519 if (dev == NULL) 520 return SET_ERROR(ESRCH); 521 522 return 0; 523 } 524 525 struct drvctl_command_desc { 526 const char *dcd_name; /* command name */ 527 int (*dcd_func)(struct lwp *, /* handler function */ 528 prop_dictionary_t, 529 prop_dictionary_t); 530 int dcd_rw; /* read or write required */ 531 }; 532 533 static const struct drvctl_command_desc drvctl_command_table[] = { 534 { .dcd_name = "get-properties", 535 .dcd_func = drvctl_command_get_properties, 536 .dcd_rw = FREAD, 537 }, 538 539 { .dcd_name = NULL } 540 }; 541 542 static int 543 drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd, 544 int fflag) 545 { 546 prop_dictionary_t command_dict, results_dict; 547 prop_string_t command_string; 548 const struct drvctl_command_desc *dcd; 549 int error; 550 551 error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict); 552 if (error) 553 return error; 554 555 results_dict = prop_dictionary_create(); 556 if (results_dict == NULL) { 557 prop_object_release(command_dict); 558 return SET_ERROR(ENOMEM); 559 } 560 561 command_string = prop_dictionary_get(command_dict, "drvctl-command"); 562 if (command_string == NULL) { 563 error = SET_ERROR(EINVAL); 564 goto out; 565 } 566 567 for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) { 568 if (prop_string_equals_string(command_string, 569 dcd->dcd_name)) 570 break; 571 } 572 573 if (dcd->dcd_name == NULL) { 574 error = SET_ERROR(EINVAL); 575 goto out; 576 } 577 578 if ((fflag & dcd->dcd_rw) == 0) { 579 error = SET_ERROR(EPERM); 580 goto out; 581 } 582 583 error = (*dcd->dcd_func)(l, command_dict, results_dict); 584 585 prop_dictionary_set_int32(results_dict, "drvctl-error", error); 586 587 error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict); 588 out: 589 prop_object_release(command_dict); 590 prop_object_release(results_dict); 591 return error; 592 } 593 594 static int 595 drvctl_getevent(struct lwp *l, struct plistref *pref, u_long ioctl_cmd, 596 int fflag) 597 { 598 struct drvctl_event *dce; 599 int ret; 600 601 if ((fflag & (FREAD|FWRITE)) != (FREAD|FWRITE)) 602 return SET_ERROR(EPERM); 603 604 mutex_enter(&drvctl_lock); 605 while ((dce = TAILQ_FIRST(&drvctl_eventq)) == NULL) { 606 if (fflag & O_NONBLOCK) { 607 mutex_exit(&drvctl_lock); 608 return SET_ERROR(EWOULDBLOCK); 609 } 610 611 ret = cv_wait_sig(&drvctl_cond, &drvctl_lock); 612 if (ret) { 613 mutex_exit(&drvctl_lock); 614 return ret; 615 } 616 } 617 TAILQ_REMOVE(&drvctl_eventq, dce, dce_link); 618 KASSERT(drvctl_eventcnt > 0); 619 --drvctl_eventcnt; 620 mutex_exit(&drvctl_lock); 621 622 ret = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, dce->dce_event); 623 624 prop_object_release(dce->dce_event); 625 kmem_free(dce, sizeof(*dce)); 626 627 return ret; 628 } 629 630 /* 631 * Module glue 632 */ 633 634 MODULE(MODULE_CLASS_DRIVER, drvctl, NULL); 635 636 int 637 drvctl_modcmd(modcmd_t cmd, void *arg) 638 { 639 int error; 640 #ifdef _MODULE 641 int bmajor, cmajor; 642 #endif 643 644 error = 0; 645 switch (cmd) { 646 case MODULE_CMD_INIT: 647 drvctl_init(); 648 649 mutex_enter(&drvctl_lock); 650 #ifdef _MODULE 651 bmajor = cmajor = -1; 652 error = devsw_attach("drvctl", NULL, &bmajor, 653 &drvctl_cdevsw, &cmajor); 654 #endif 655 if (error == 0) { 656 KASSERT(saved_insert_vec == NULL); 657 saved_insert_vec = devmon_insert_vec; 658 devmon_insert_vec = devmon_insert; 659 } 660 661 mutex_exit(&drvctl_lock); 662 break; 663 664 case MODULE_CMD_FINI: 665 mutex_enter(&drvctl_lock); 666 if (drvctl_nopen != 0 || drvctl_eventcnt != 0 ) { 667 mutex_exit(&drvctl_lock); 668 return SET_ERROR(EBUSY); 669 } 670 KASSERT(saved_insert_vec != NULL); 671 devmon_insert_vec = saved_insert_vec; 672 saved_insert_vec = NULL; 673 #ifdef _MODULE 674 devsw_detach(NULL, &drvctl_cdevsw); 675 #endif 676 mutex_exit(&drvctl_lock); 677 drvctl_fini(); 678 679 break; 680 default: 681 error = SET_ERROR(ENOTTY); 682 break; 683 } 684 685 return error; 686 } 687