Home | History | Annotate | Line # | Download | only in kern
kern_drvctl.c revision 1.17
      1  1.17  jmcneill /* $NetBSD: kern_drvctl.c,v 1.17 2008/05/25 12:30:40 jmcneill Exp $ */
      2   1.1  drochner 
      3   1.1  drochner /*
      4   1.1  drochner  * Copyright (c) 2004
      5   1.1  drochner  * 	Matthias Drochner.  All rights reserved.
      6   1.1  drochner  *
      7   1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8   1.1  drochner  * modification, are permitted provided that the following conditions
      9   1.1  drochner  * are met:
     10   1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11   1.1  drochner  *    notice, this list of conditions, and the following disclaimer.
     12   1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15   1.1  drochner  *
     16   1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17   1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20   1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  drochner  * SUCH DAMAGE.
     27   1.1  drochner  */
     28   1.1  drochner 
     29   1.1  drochner #include <sys/cdefs.h>
     30  1.17  jmcneill __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.17 2008/05/25 12:30:40 jmcneill Exp $");
     31   1.1  drochner 
     32   1.1  drochner #include <sys/param.h>
     33   1.1  drochner #include <sys/systm.h>
     34   1.1  drochner #include <sys/kernel.h>
     35   1.1  drochner #include <sys/conf.h>
     36   1.1  drochner #include <sys/device.h>
     37   1.1  drochner #include <sys/event.h>
     38   1.1  drochner #include <sys/malloc.h>
     39  1.17  jmcneill #include <sys/kmem.h>
     40   1.1  drochner #include <sys/ioctl.h>
     41   1.5   thorpej #include <sys/fcntl.h>
     42  1.17  jmcneill #include <sys/file.h>
     43  1.17  jmcneill #include <sys/filedesc.h>
     44   1.1  drochner #include <sys/drvctlio.h>
     45  1.17  jmcneill #include <sys/devmon.h>
     46   1.1  drochner 
     47  1.17  jmcneill struct drvctl_event {
     48  1.17  jmcneill 	TAILQ_ENTRY(drvctl_event) dce_link;
     49  1.17  jmcneill 	prop_dictionary_t	dce_event;
     50  1.17  jmcneill };
     51  1.17  jmcneill 
     52  1.17  jmcneill TAILQ_HEAD(drvctl_queue, drvctl_event);
     53  1.17  jmcneill 
     54  1.17  jmcneill static struct drvctl_queue	drvctl_eventq;		/* FIFO */
     55  1.17  jmcneill static kcondvar_t		drvctl_cond;
     56  1.17  jmcneill static kmutex_t			drvctl_lock;
     57  1.17  jmcneill static int			drvctl_nopen = 0, drvctl_eventcnt = 0;
     58  1.17  jmcneill 
     59  1.17  jmcneill #define DRVCTL_EVENTQ_DEPTH	64	/* arbitrary queue limit */
     60  1.17  jmcneill 
     61  1.17  jmcneill dev_type_open(drvctlopen);
     62   1.1  drochner 
     63   1.1  drochner const struct cdevsw drvctl_cdevsw = {
     64  1.17  jmcneill 	drvctlopen, nullclose, nullread, nullwrite, noioctl,
     65   1.6       gdt 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
     66   1.1  drochner };
     67   1.1  drochner 
     68   1.1  drochner void drvctlattach(int);
     69   1.1  drochner 
     70  1.17  jmcneill static int	drvctl_read(struct file *, off_t *, struct uio *,
     71  1.17  jmcneill 			    kauth_cred_t, int);
     72  1.17  jmcneill static int	drvctl_write(struct file *, off_t *, struct uio *,
     73  1.17  jmcneill 			     kauth_cred_t, int);
     74  1.17  jmcneill static int	drvctl_ioctl(struct file *, u_long, void *);
     75  1.17  jmcneill static int	drvctl_close(struct file *);
     76  1.17  jmcneill 
     77  1.17  jmcneill static const struct fileops drvctl_fileops = {
     78  1.17  jmcneill 	drvctl_read,
     79  1.17  jmcneill 	drvctl_write,
     80  1.17  jmcneill 	drvctl_ioctl,
     81  1.17  jmcneill 	fnullop_fcntl,
     82  1.17  jmcneill 	fnullop_poll,
     83  1.17  jmcneill 	fbadop_stat,
     84  1.17  jmcneill 	drvctl_close,
     85  1.17  jmcneill 	fnullop_kqfilter
     86  1.17  jmcneill };
     87  1.17  jmcneill 
     88   1.1  drochner #define MAXLOCATORS 100
     89   1.1  drochner 
     90  1.17  jmcneill static int drvctl_command(struct lwp *, struct plistref *, u_long, int);
     91  1.17  jmcneill static int drvctl_getevent(struct lwp *, struct plistref *, u_long, int);
     92  1.17  jmcneill 
     93  1.17  jmcneill void
     94  1.17  jmcneill drvctl_init(void)
     95  1.17  jmcneill {
     96  1.17  jmcneill 	TAILQ_INIT(&drvctl_eventq);
     97  1.17  jmcneill 	mutex_init(&drvctl_lock, MUTEX_DEFAULT, IPL_NONE);
     98  1.17  jmcneill 	cv_init(&drvctl_cond, "devmon");
     99  1.17  jmcneill }
    100  1.17  jmcneill 
    101  1.17  jmcneill void
    102  1.17  jmcneill devmon_insert(const char *event, prop_dictionary_t ev)
    103  1.17  jmcneill {
    104  1.17  jmcneill 	struct drvctl_event *dce, *odce;;
    105  1.17  jmcneill 
    106  1.17  jmcneill 	mutex_enter(&drvctl_lock);
    107  1.17  jmcneill 
    108  1.17  jmcneill 	if (drvctl_nopen == 0) {
    109  1.17  jmcneill 		mutex_exit(&drvctl_lock);
    110  1.17  jmcneill 		return;
    111  1.17  jmcneill 	}
    112  1.17  jmcneill 
    113  1.17  jmcneill 	/* Fill in mandatory member */
    114  1.17  jmcneill 	if (!prop_dictionary_set_cstring_nocopy(ev, "event", event)) {
    115  1.17  jmcneill 		prop_object_release(ev);
    116  1.17  jmcneill 		mutex_exit(&drvctl_lock);
    117  1.17  jmcneill 		return;
    118  1.17  jmcneill 	}
    119  1.17  jmcneill 
    120  1.17  jmcneill 	dce = kmem_alloc(sizeof(*dce), KM_SLEEP);
    121  1.17  jmcneill 	if (dce == NULL) {
    122  1.17  jmcneill 		mutex_exit(&drvctl_lock);
    123  1.17  jmcneill 		return;
    124  1.17  jmcneill 	}
    125  1.17  jmcneill 
    126  1.17  jmcneill 	dce->dce_event = ev;
    127  1.17  jmcneill 
    128  1.17  jmcneill 	if (drvctl_eventcnt == DRVCTL_EVENTQ_DEPTH) {
    129  1.17  jmcneill 		odce = TAILQ_FIRST(&drvctl_eventq);
    130  1.17  jmcneill 		TAILQ_REMOVE(&drvctl_eventq, odce, dce_link);
    131  1.17  jmcneill 		--drvctl_eventcnt;
    132  1.17  jmcneill 	}
    133  1.17  jmcneill 
    134  1.17  jmcneill 	TAILQ_INSERT_TAIL(&drvctl_eventq, dce, dce_link);
    135  1.17  jmcneill 	++drvctl_eventcnt;
    136  1.17  jmcneill 	cv_broadcast(&drvctl_cond);
    137  1.17  jmcneill 
    138  1.17  jmcneill 	mutex_exit(&drvctl_lock);
    139  1.17  jmcneill }
    140  1.17  jmcneill 
    141  1.17  jmcneill int
    142  1.17  jmcneill drvctlopen(dev_t dev, int flags, int mode, struct lwp *l)
    143  1.17  jmcneill {
    144  1.17  jmcneill 	struct file *fp;
    145  1.17  jmcneill 	int fd;
    146  1.17  jmcneill 	int ret;
    147  1.17  jmcneill 
    148  1.17  jmcneill 	ret = fd_allocfile(&fp, &fd);
    149  1.17  jmcneill 	if (ret)
    150  1.17  jmcneill 		return (ret);
    151  1.17  jmcneill 
    152  1.17  jmcneill 	/* XXX setup context */
    153  1.17  jmcneill 	mutex_enter(&drvctl_lock);
    154  1.17  jmcneill 	ret = fd_clone(fp, fd, flags, &drvctl_fileops, /* context */NULL);
    155  1.17  jmcneill 	++drvctl_nopen;
    156  1.17  jmcneill 	mutex_exit(&drvctl_lock);
    157  1.17  jmcneill 
    158  1.17  jmcneill 	return ret;
    159  1.17  jmcneill }
    160   1.5   thorpej 
    161  1.12    dyoung static int
    162  1.12    dyoung pmdevbyname(int cmd, struct devpmargs *a)
    163  1.12    dyoung {
    164  1.12    dyoung 	struct device *d;
    165  1.12    dyoung 
    166  1.14     joerg 	if ((d = device_find_by_xname(a->devname)) == NULL)
    167  1.12    dyoung 		return ENXIO;
    168  1.12    dyoung 
    169  1.12    dyoung 	switch (cmd) {
    170  1.12    dyoung 	case DRVSUSPENDDEV:
    171  1.16    dyoung 		return pmf_device_recursive_suspend(d, PMF_F_NONE) ? 0 : EBUSY;
    172  1.12    dyoung 	case DRVRESUMEDEV:
    173  1.16    dyoung 		if (a->flags & DEVPM_F_SUBTREE) {
    174  1.16    dyoung 			return pmf_device_resume_subtree(d, PMF_F_NONE)
    175  1.16    dyoung 			    ? 0 : EBUSY;
    176  1.16    dyoung 		} else {
    177  1.16    dyoung 			return pmf_device_recursive_resume(d, PMF_F_NONE)
    178  1.16    dyoung 			    ? 0 : EBUSY;
    179  1.16    dyoung 		}
    180  1.12    dyoung 	default:
    181  1.12    dyoung 		return EPASSTHROUGH;
    182  1.12    dyoung 	}
    183  1.12    dyoung }
    184  1.12    dyoung 
    185  1.12    dyoung static int
    186  1.12    dyoung listdevbyname(struct devlistargs *l)
    187  1.12    dyoung {
    188  1.13  drochner 	device_t d, child;
    189  1.15    dyoung 	deviter_t di;
    190  1.15    dyoung 	int cnt = 0, idx, error = 0;
    191  1.12    dyoung 
    192  1.14     joerg 	if ((d = device_find_by_xname(l->l_devname)) == NULL)
    193  1.12    dyoung 		return ENXIO;
    194  1.12    dyoung 
    195  1.15    dyoung 	for (child = deviter_first(&di, 0); child != NULL;
    196  1.15    dyoung 	     child = deviter_next(&di)) {
    197  1.13  drochner 		if (device_parent(child) != d)
    198  1.13  drochner 			continue;
    199  1.13  drochner 		idx = cnt++;
    200  1.13  drochner 		if (l->l_childname == NULL || idx >= l->l_children)
    201  1.13  drochner 			continue;
    202  1.13  drochner 		error = copyoutstr(device_xname(child), l->l_childname[idx],
    203  1.13  drochner 				sizeof(l->l_childname[idx]), NULL);
    204  1.15    dyoung 		if (error != 0)
    205  1.15    dyoung 			break;
    206  1.13  drochner 	}
    207  1.15    dyoung 	deviter_release(&di);
    208  1.12    dyoung 
    209  1.13  drochner 	l->l_children = cnt;
    210  1.15    dyoung 	return error;
    211  1.12    dyoung }
    212  1.12    dyoung 
    213   1.1  drochner static int
    214   1.1  drochner detachdevbyname(const char *devname)
    215   1.1  drochner {
    216   1.1  drochner 	struct device *d;
    217   1.1  drochner 
    218  1.14     joerg 	if ((d = device_find_by_xname(devname)) == NULL)
    219  1.12    dyoung 		return ENXIO;
    220  1.12    dyoung 
    221   1.1  drochner #ifndef XXXFULLRISK
    222  1.12    dyoung 	/*
    223  1.12    dyoung 	 * If the parent cannot be notified, it might keep
    224  1.12    dyoung 	 * pointers to the detached device.
    225  1.12    dyoung 	 * There might be a private notification mechanism,
    226  1.12    dyoung 	 * but better play save here.
    227  1.12    dyoung 	 */
    228  1.12    dyoung 	if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached)
    229  1.12    dyoung 		return (ENOTSUP);
    230   1.1  drochner #endif
    231  1.12    dyoung 	return (config_detach(d, 0));
    232   1.1  drochner }
    233   1.1  drochner 
    234   1.1  drochner static int
    235   1.1  drochner rescanbus(const char *busname, const char *ifattr,
    236   1.1  drochner 	  int numlocators, const int *locators)
    237   1.1  drochner {
    238  1.12    dyoung 	int i, rc;
    239   1.1  drochner 	struct device *d;
    240   1.2  drochner 	const struct cfiattrdata * const *ap;
    241   1.1  drochner 
    242   1.1  drochner 	/* XXX there should be a way to get limits and defaults (per device)
    243   1.1  drochner 	   from config generated data */
    244   1.1  drochner 	int locs[MAXLOCATORS];
    245   1.1  drochner 	for (i = 0; i < MAXLOCATORS; i++)
    246   1.1  drochner 		locs[i] = -1;
    247   1.1  drochner 
    248   1.1  drochner 	for (i = 0; i < numlocators;i++)
    249   1.1  drochner 		locs[i] = locators[i];
    250   1.1  drochner 
    251  1.14     joerg 	if ((d = device_find_by_xname(busname)) == NULL)
    252  1.12    dyoung 		return ENXIO;
    253   1.1  drochner 
    254  1.12    dyoung 	/*
    255  1.12    dyoung 	 * must support rescan, and must have something
    256  1.12    dyoung 	 * to attach to
    257  1.12    dyoung 	 */
    258  1.12    dyoung 	if (!d->dv_cfattach->ca_rescan ||
    259  1.12    dyoung 	    !d->dv_cfdriver->cd_attrs)
    260  1.12    dyoung 		return (ENODEV);
    261  1.12    dyoung 
    262  1.12    dyoung 	/* allow to omit attribute if there is exactly one */
    263  1.12    dyoung 	if (!ifattr) {
    264  1.12    dyoung 		if (d->dv_cfdriver->cd_attrs[1])
    265  1.12    dyoung 			return (EINVAL);
    266  1.12    dyoung 		ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
    267  1.12    dyoung 	} else {
    268  1.12    dyoung 		/* check for valid attribute passed */
    269  1.12    dyoung 		for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
    270  1.12    dyoung 			if (!strcmp((*ap)->ci_name, ifattr))
    271  1.12    dyoung 				break;
    272  1.12    dyoung 		if (!*ap)
    273  1.12    dyoung 			return (EINVAL);
    274   1.1  drochner 	}
    275   1.1  drochner 
    276  1.12    dyoung 	rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
    277  1.12    dyoung 	config_deferred(NULL);
    278  1.12    dyoung 	return rc;
    279   1.1  drochner }
    280   1.1  drochner 
    281  1.17  jmcneill static int
    282  1.17  jmcneill drvctl_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    283  1.17  jmcneill     int flags)
    284  1.17  jmcneill {
    285  1.17  jmcneill 	return (ENODEV);
    286  1.17  jmcneill }
    287  1.17  jmcneill 
    288  1.17  jmcneill static int
    289  1.17  jmcneill drvctl_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    290  1.17  jmcneill     int flags)
    291  1.17  jmcneill {
    292  1.17  jmcneill 	return (ENODEV);
    293  1.17  jmcneill }
    294  1.17  jmcneill 
    295  1.17  jmcneill static int
    296  1.17  jmcneill drvctl_ioctl(struct file *fp, u_long cmd, void *data)
    297   1.1  drochner {
    298   1.1  drochner 	int res;
    299   1.1  drochner 	char *ifattr;
    300   1.1  drochner 	int *locs;
    301   1.1  drochner 
    302   1.1  drochner 	switch (cmd) {
    303  1.12    dyoung 	case DRVSUSPENDDEV:
    304  1.12    dyoung 	case DRVRESUMEDEV:
    305  1.12    dyoung #define d ((struct devpmargs *)data)
    306  1.12    dyoung 		res = pmdevbyname(cmd, d);
    307  1.12    dyoung #undef d
    308  1.12    dyoung 		break;
    309  1.12    dyoung 	case DRVLISTDEV:
    310  1.12    dyoung 		res = listdevbyname((struct devlistargs *)data);
    311  1.12    dyoung 		break;
    312   1.1  drochner 	case DRVDETACHDEV:
    313   1.1  drochner #define d ((struct devdetachargs *)data)
    314   1.1  drochner 		res = detachdevbyname(d->devname);
    315   1.1  drochner #undef d
    316   1.1  drochner 		break;
    317   1.1  drochner 	case DRVRESCANBUS:
    318   1.1  drochner #define d ((struct devrescanargs *)data)
    319   1.1  drochner 		d->busname[sizeof(d->busname) - 1] = '\0';
    320   1.1  drochner 
    321   1.1  drochner 		/* XXX better copyin? */
    322   1.1  drochner 		if (d->ifattr[0]) {
    323   1.1  drochner 			d->ifattr[sizeof(d->ifattr) - 1] = '\0';
    324   1.1  drochner 			ifattr = d->ifattr;
    325   1.1  drochner 		} else
    326   1.1  drochner 			ifattr = 0;
    327   1.1  drochner 
    328   1.1  drochner 		if (d->numlocators) {
    329   1.1  drochner 			if (d->numlocators > MAXLOCATORS)
    330   1.1  drochner 				return (EINVAL);
    331   1.1  drochner 			locs = malloc(d->numlocators * sizeof(int), M_DEVBUF,
    332   1.1  drochner 				      M_WAITOK);
    333   1.1  drochner 			res = copyin(d->locators, locs,
    334   1.1  drochner 				     d->numlocators * sizeof(int));
    335  1.11     rmind 			if (res) {
    336  1.11     rmind 				free(locs, M_DEVBUF);
    337   1.1  drochner 				return (res);
    338  1.11     rmind 			}
    339   1.1  drochner 		} else
    340   1.1  drochner 			locs = 0;
    341   1.1  drochner 		res = rescanbus(d->busname, ifattr, d->numlocators, locs);
    342   1.1  drochner 		if (locs)
    343   1.1  drochner 			free(locs, M_DEVBUF);
    344   1.1  drochner #undef d
    345   1.5   thorpej 		break;
    346   1.5   thorpej 	case DRVCTLCOMMAND:
    347  1.17  jmcneill 	    	res = drvctl_command(curlwp, (struct plistref *)data, cmd,
    348  1.17  jmcneill 		    fp->f_flag);
    349   1.5   thorpej 	    	break;
    350  1.17  jmcneill 	case DRVGETEVENT:
    351  1.17  jmcneill 		res = drvctl_getevent(curlwp, (struct plistref *)data, cmd,
    352  1.17  jmcneill 		    fp->f_flag);
    353  1.17  jmcneill 		break;
    354   1.5   thorpej 	default:
    355   1.5   thorpej 		return (EPASSTHROUGH);
    356   1.1  drochner 	}
    357   1.1  drochner 	return (res);
    358   1.1  drochner }
    359   1.1  drochner 
    360  1.17  jmcneill static int
    361  1.17  jmcneill drvctl_close(struct file *fp)
    362  1.17  jmcneill {
    363  1.17  jmcneill 	struct drvctl_event *dce;
    364  1.17  jmcneill 
    365  1.17  jmcneill 	/* XXX free context */
    366  1.17  jmcneill 	mutex_enter(&drvctl_lock);
    367  1.17  jmcneill 	KASSERT(drvctl_nopen > 0);
    368  1.17  jmcneill 	--drvctl_nopen;
    369  1.17  jmcneill 	if (drvctl_nopen == 0) {
    370  1.17  jmcneill 		/* flush queue */
    371  1.17  jmcneill 		while ((dce = TAILQ_FIRST(&drvctl_eventq)) != NULL) {
    372  1.17  jmcneill 			TAILQ_REMOVE(&drvctl_eventq, dce, dce_link);
    373  1.17  jmcneill 			KASSERT(drvctl_eventcnt > 0);
    374  1.17  jmcneill 			--drvctl_eventcnt;
    375  1.17  jmcneill 			kmem_free(dce, sizeof(*dce));
    376  1.17  jmcneill 		}
    377  1.17  jmcneill 	}
    378  1.17  jmcneill 	mutex_exit(&drvctl_lock);
    379  1.17  jmcneill 
    380  1.17  jmcneill 	return (0);
    381  1.17  jmcneill }
    382  1.17  jmcneill 
    383   1.1  drochner void
    384   1.9      yamt drvctlattach(int arg)
    385   1.1  drochner {
    386   1.1  drochner }
    387   1.5   thorpej 
    388   1.5   thorpej /*****************************************************************************
    389   1.5   thorpej  * Driver control command processing engine
    390   1.5   thorpej  *****************************************************************************/
    391   1.5   thorpej 
    392   1.5   thorpej static int
    393   1.9      yamt drvctl_command_get_properties(struct lwp *l,
    394   1.5   thorpej 			      prop_dictionary_t command_dict,
    395   1.5   thorpej 			      prop_dictionary_t results_dict)
    396   1.5   thorpej {
    397   1.5   thorpej 	prop_dictionary_t args_dict;
    398   1.5   thorpej 	prop_string_t devname_string;
    399   1.5   thorpej 	device_t dev;
    400  1.15    dyoung 	deviter_t di;
    401   1.5   thorpej 
    402   1.5   thorpej 	args_dict = prop_dictionary_get(command_dict, "drvctl-arguments");
    403   1.5   thorpej 	if (args_dict == NULL)
    404   1.5   thorpej 		return (EINVAL);
    405   1.5   thorpej 
    406   1.5   thorpej 	devname_string = prop_dictionary_get(args_dict, "device-name");
    407   1.5   thorpej 	if (devname_string == NULL)
    408   1.5   thorpej 		return (EINVAL);
    409   1.5   thorpej 
    410  1.15    dyoung 	for (dev = deviter_first(&di, 0); dev != NULL;
    411  1.15    dyoung 	     dev = deviter_next(&di)) {
    412   1.5   thorpej 		if (prop_string_equals_cstring(devname_string,
    413  1.15    dyoung 					       device_xname(dev))) {
    414  1.15    dyoung 			prop_dictionary_set(results_dict, "drvctl-result-data",
    415  1.15    dyoung 			    device_properties(dev));
    416   1.5   thorpej 			break;
    417  1.15    dyoung 		}
    418   1.5   thorpej 	}
    419   1.5   thorpej 
    420  1.15    dyoung 	deviter_release(&di);
    421  1.15    dyoung 
    422   1.5   thorpej 	if (dev == NULL)
    423   1.5   thorpej 		return (ESRCH);
    424  1.15    dyoung 
    425   1.5   thorpej 	return (0);
    426   1.5   thorpej }
    427   1.5   thorpej 
    428   1.5   thorpej struct drvctl_command_desc {
    429   1.5   thorpej 	const char *dcd_name;		/* command name */
    430   1.5   thorpej 	int (*dcd_func)(struct lwp *,	/* handler function */
    431   1.5   thorpej 			prop_dictionary_t,
    432   1.5   thorpej 			prop_dictionary_t);
    433   1.5   thorpej 	int dcd_rw;			/* read or write required */
    434   1.5   thorpej };
    435   1.5   thorpej 
    436   1.5   thorpej static const struct drvctl_command_desc drvctl_command_table[] = {
    437   1.5   thorpej 	{ .dcd_name = "get-properties",
    438   1.5   thorpej 	  .dcd_func = drvctl_command_get_properties,
    439   1.5   thorpej 	  .dcd_rw   = FREAD,
    440   1.5   thorpej 	},
    441   1.5   thorpej 
    442   1.5   thorpej 	{ .dcd_name = NULL }
    443   1.5   thorpej };
    444   1.5   thorpej 
    445   1.5   thorpej static int
    446   1.5   thorpej drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
    447   1.5   thorpej 	       int fflag)
    448   1.5   thorpej {
    449   1.5   thorpej 	prop_dictionary_t command_dict, results_dict;
    450   1.5   thorpej 	prop_string_t command_string;
    451   1.5   thorpej 	const struct drvctl_command_desc *dcd;
    452   1.5   thorpej 	int error;
    453   1.5   thorpej 
    454   1.5   thorpej 	error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict);
    455   1.5   thorpej 	if (error)
    456   1.5   thorpej 		return (error);
    457   1.5   thorpej 
    458   1.5   thorpej 	results_dict = prop_dictionary_create();
    459   1.5   thorpej 	if (results_dict == NULL) {
    460   1.5   thorpej 		prop_object_release(command_dict);
    461   1.5   thorpej 		return (ENOMEM);
    462   1.5   thorpej 	}
    463   1.5   thorpej 
    464   1.5   thorpej 	command_string = prop_dictionary_get(command_dict, "drvctl-command");
    465   1.5   thorpej 	if (command_string == NULL) {
    466   1.5   thorpej 		error = EINVAL;
    467   1.5   thorpej 		goto out;
    468   1.5   thorpej 	}
    469   1.5   thorpej 
    470   1.5   thorpej 	for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) {
    471   1.5   thorpej 		if (prop_string_equals_cstring(command_string,
    472   1.5   thorpej 					       dcd->dcd_name))
    473   1.5   thorpej 			break;
    474   1.5   thorpej 	}
    475   1.5   thorpej 
    476   1.5   thorpej 	if (dcd->dcd_name == NULL) {
    477   1.5   thorpej 		error = EINVAL;
    478   1.5   thorpej 		goto out;
    479   1.5   thorpej 	}
    480   1.5   thorpej 
    481   1.5   thorpej 	if ((fflag & dcd->dcd_rw) == 0) {
    482   1.5   thorpej 		error = EPERM;
    483   1.5   thorpej 		goto out;
    484   1.5   thorpej 	}
    485   1.5   thorpej 
    486   1.5   thorpej 	error = (*dcd->dcd_func)(l, command_dict, results_dict);
    487   1.5   thorpej 
    488   1.8   thorpej 	prop_dictionary_set_int32(results_dict, "drvctl-error", error);
    489   1.5   thorpej 
    490   1.5   thorpej 	error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict);
    491   1.5   thorpej  out:
    492   1.5   thorpej 	prop_object_release(command_dict);
    493   1.5   thorpej 	prop_object_release(results_dict);
    494   1.5   thorpej 	return (error);
    495   1.5   thorpej }
    496  1.17  jmcneill 
    497  1.17  jmcneill static int
    498  1.17  jmcneill drvctl_getevent(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
    499  1.17  jmcneill 	        int fflag)
    500  1.17  jmcneill {
    501  1.17  jmcneill 	struct drvctl_event *dce;
    502  1.17  jmcneill 	int ret;
    503  1.17  jmcneill 
    504  1.17  jmcneill 	if ((fflag & (FREAD|FWRITE)) != (FREAD|FWRITE))
    505  1.17  jmcneill 		return (EPERM);
    506  1.17  jmcneill 
    507  1.17  jmcneill 	mutex_enter(&drvctl_lock);
    508  1.17  jmcneill 	while ((dce = TAILQ_FIRST(&drvctl_eventq)) == NULL) {
    509  1.17  jmcneill 		if (fflag & O_NONBLOCK) {
    510  1.17  jmcneill 			mutex_exit(&drvctl_lock);
    511  1.17  jmcneill 			return (EWOULDBLOCK);
    512  1.17  jmcneill 		}
    513  1.17  jmcneill 
    514  1.17  jmcneill 		ret = cv_wait_sig(&drvctl_cond, &drvctl_lock);
    515  1.17  jmcneill 		if (ret) {
    516  1.17  jmcneill 			mutex_exit(&drvctl_lock);
    517  1.17  jmcneill 			return (ret);
    518  1.17  jmcneill 		}
    519  1.17  jmcneill 	}
    520  1.17  jmcneill 	TAILQ_REMOVE(&drvctl_eventq, dce, dce_link);
    521  1.17  jmcneill 	KASSERT(drvctl_eventcnt > 0);
    522  1.17  jmcneill 	--drvctl_eventcnt;
    523  1.17  jmcneill 	mutex_exit(&drvctl_lock);
    524  1.17  jmcneill 
    525  1.17  jmcneill 	ret = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, dce->dce_event);
    526  1.17  jmcneill 
    527  1.17  jmcneill 	prop_object_release(dce->dce_event);
    528  1.17  jmcneill 	kmem_free(dce, sizeof(*dce));
    529  1.17  jmcneill 
    530  1.17  jmcneill 	return (ret);
    531  1.17  jmcneill }
    532