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