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