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