Home | History | Annotate | Line # | Download | only in kern
kern_drvctl.c revision 1.13
      1  1.13  drochner /* $NetBSD: kern_drvctl.c,v 1.13 2008/02/06 20:24:17 drochner 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.13  drochner __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.13 2008/02/06 20:24:17 drochner 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.1  drochner #include <sys/ioctl.h>
     40   1.5   thorpej #include <sys/fcntl.h>
     41   1.1  drochner #include <sys/drvctlio.h>
     42   1.1  drochner 
     43   1.1  drochner dev_type_ioctl(drvctlioctl);
     44   1.1  drochner 
     45   1.1  drochner const struct cdevsw drvctl_cdevsw = {
     46   1.1  drochner 	nullopen, nullclose, nullread, nullwrite, drvctlioctl,
     47   1.6       gdt 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
     48   1.1  drochner };
     49   1.1  drochner 
     50   1.1  drochner void drvctlattach(int);
     51  1.12    dyoung static struct device *devbyname(const char *);
     52   1.1  drochner 
     53   1.1  drochner #define MAXLOCATORS 100
     54   1.1  drochner 
     55   1.5   thorpej static int drvctl_command(struct lwp *, struct plistref *, u_long, int flag);
     56   1.5   thorpej 
     57  1.12    dyoung static struct device *
     58  1.12    dyoung devbyname(const char *devname)
     59  1.12    dyoung {
     60  1.12    dyoung 	struct device *d;
     61  1.12    dyoung 
     62  1.12    dyoung 	TAILQ_FOREACH(d, &alldevs, dv_list) {
     63  1.12    dyoung 		if (strcmp(devname, device_xname(d)) == 0)
     64  1.12    dyoung 			break;
     65  1.12    dyoung 	}
     66  1.12    dyoung 	return d;
     67  1.12    dyoung }
     68  1.12    dyoung 
     69  1.12    dyoung static int
     70  1.12    dyoung pmdevbyname(int cmd, struct devpmargs *a)
     71  1.12    dyoung {
     72  1.12    dyoung 	struct device *d;
     73  1.12    dyoung 
     74  1.12    dyoung 	if ((d = devbyname(a->devname)) == NULL)
     75  1.12    dyoung 		return ENXIO;
     76  1.12    dyoung 
     77  1.12    dyoung 	switch (cmd) {
     78  1.12    dyoung 	case DRVSUSPENDDEV:
     79  1.12    dyoung 		return pmf_device_recursive_suspend(d) ? 0 : EBUSY;
     80  1.12    dyoung 	case DRVRESUMEDEV:
     81  1.12    dyoung 		if (a->flags & DEVPM_F_SUBTREE)
     82  1.12    dyoung 			return pmf_device_resume_subtree(d) ? 0 : EBUSY;
     83  1.12    dyoung 		else
     84  1.12    dyoung 			return pmf_device_recursive_resume(d) ? 0 : EBUSY;
     85  1.12    dyoung 	default:
     86  1.12    dyoung 		return EPASSTHROUGH;
     87  1.12    dyoung 	}
     88  1.12    dyoung }
     89  1.12    dyoung 
     90  1.12    dyoung static int
     91  1.12    dyoung listdevbyname(struct devlistargs *l)
     92  1.12    dyoung {
     93  1.13  drochner 	device_t d, child;
     94  1.13  drochner 	int cnt = 0, idx, error;
     95  1.12    dyoung 
     96  1.12    dyoung 	if ((d = devbyname(l->l_devname)) == NULL)
     97  1.12    dyoung 		return ENXIO;
     98  1.12    dyoung 
     99  1.13  drochner 	TAILQ_FOREACH(child, &alldevs, dv_list) {
    100  1.13  drochner 		if (device_parent(child) != d)
    101  1.13  drochner 			continue;
    102  1.13  drochner 		idx = cnt++;
    103  1.13  drochner 		if (l->l_childname == NULL || idx >= l->l_children)
    104  1.13  drochner 			continue;
    105  1.13  drochner 		error = copyoutstr(device_xname(child), l->l_childname[idx],
    106  1.13  drochner 				sizeof(l->l_childname[idx]), NULL);
    107  1.13  drochner 		if (error)
    108  1.13  drochner 			return error;
    109  1.13  drochner 	}
    110  1.12    dyoung 
    111  1.13  drochner 	l->l_children = cnt;
    112  1.12    dyoung 	return 0;
    113  1.12    dyoung }
    114  1.12    dyoung 
    115   1.1  drochner static int
    116   1.1  drochner detachdevbyname(const char *devname)
    117   1.1  drochner {
    118   1.1  drochner 	struct device *d;
    119   1.1  drochner 
    120  1.12    dyoung 	if ((d = devbyname(devname)) == NULL)
    121  1.12    dyoung 		return ENXIO;
    122  1.12    dyoung 
    123   1.1  drochner #ifndef XXXFULLRISK
    124  1.12    dyoung 	/*
    125  1.12    dyoung 	 * If the parent cannot be notified, it might keep
    126  1.12    dyoung 	 * pointers to the detached device.
    127  1.12    dyoung 	 * There might be a private notification mechanism,
    128  1.12    dyoung 	 * but better play save here.
    129  1.12    dyoung 	 */
    130  1.12    dyoung 	if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached)
    131  1.12    dyoung 		return (ENOTSUP);
    132   1.1  drochner #endif
    133  1.12    dyoung 	return (config_detach(d, 0));
    134   1.1  drochner }
    135   1.1  drochner 
    136   1.1  drochner static int
    137   1.1  drochner rescanbus(const char *busname, const char *ifattr,
    138   1.1  drochner 	  int numlocators, const int *locators)
    139   1.1  drochner {
    140  1.12    dyoung 	int i, rc;
    141   1.1  drochner 	struct device *d;
    142   1.2  drochner 	const struct cfiattrdata * const *ap;
    143   1.1  drochner 
    144   1.1  drochner 	/* XXX there should be a way to get limits and defaults (per device)
    145   1.1  drochner 	   from config generated data */
    146   1.1  drochner 	int locs[MAXLOCATORS];
    147   1.1  drochner 	for (i = 0; i < MAXLOCATORS; i++)
    148   1.1  drochner 		locs[i] = -1;
    149   1.1  drochner 
    150   1.1  drochner 	for (i = 0; i < numlocators;i++)
    151   1.1  drochner 		locs[i] = locators[i];
    152   1.1  drochner 
    153  1.12    dyoung 	if ((d = devbyname(busname)) == NULL)
    154  1.12    dyoung 		return ENXIO;
    155   1.1  drochner 
    156  1.12    dyoung 	/*
    157  1.12    dyoung 	 * must support rescan, and must have something
    158  1.12    dyoung 	 * to attach to
    159  1.12    dyoung 	 */
    160  1.12    dyoung 	if (!d->dv_cfattach->ca_rescan ||
    161  1.12    dyoung 	    !d->dv_cfdriver->cd_attrs)
    162  1.12    dyoung 		return (ENODEV);
    163  1.12    dyoung 
    164  1.12    dyoung 	/* allow to omit attribute if there is exactly one */
    165  1.12    dyoung 	if (!ifattr) {
    166  1.12    dyoung 		if (d->dv_cfdriver->cd_attrs[1])
    167  1.12    dyoung 			return (EINVAL);
    168  1.12    dyoung 		ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
    169  1.12    dyoung 	} else {
    170  1.12    dyoung 		/* check for valid attribute passed */
    171  1.12    dyoung 		for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
    172  1.12    dyoung 			if (!strcmp((*ap)->ci_name, ifattr))
    173  1.12    dyoung 				break;
    174  1.12    dyoung 		if (!*ap)
    175  1.12    dyoung 			return (EINVAL);
    176   1.1  drochner 	}
    177   1.1  drochner 
    178  1.12    dyoung 	rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
    179  1.12    dyoung 	config_deferred(NULL);
    180  1.12    dyoung 	return rc;
    181   1.1  drochner }
    182   1.1  drochner 
    183   1.1  drochner int
    184  1.10  christos drvctlioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *p)
    185   1.1  drochner {
    186   1.1  drochner 	int res;
    187   1.1  drochner 	char *ifattr;
    188   1.1  drochner 	int *locs;
    189   1.1  drochner 
    190   1.1  drochner 	switch (cmd) {
    191  1.12    dyoung 	case DRVSUSPENDDEV:
    192  1.12    dyoung 	case DRVRESUMEDEV:
    193  1.12    dyoung #define d ((struct devpmargs *)data)
    194  1.12    dyoung 		res = pmdevbyname(cmd, d);
    195  1.12    dyoung #undef d
    196  1.12    dyoung 		break;
    197  1.12    dyoung 	case DRVLISTDEV:
    198  1.12    dyoung 		res = listdevbyname((struct devlistargs *)data);
    199  1.12    dyoung 		break;
    200   1.1  drochner 	case DRVDETACHDEV:
    201   1.1  drochner #define d ((struct devdetachargs *)data)
    202   1.1  drochner 		res = detachdevbyname(d->devname);
    203   1.1  drochner #undef d
    204   1.1  drochner 		break;
    205   1.1  drochner 	case DRVRESCANBUS:
    206   1.1  drochner #define d ((struct devrescanargs *)data)
    207   1.1  drochner 		d->busname[sizeof(d->busname) - 1] = '\0';
    208   1.1  drochner 
    209   1.1  drochner 		/* XXX better copyin? */
    210   1.1  drochner 		if (d->ifattr[0]) {
    211   1.1  drochner 			d->ifattr[sizeof(d->ifattr) - 1] = '\0';
    212   1.1  drochner 			ifattr = d->ifattr;
    213   1.1  drochner 		} else
    214   1.1  drochner 			ifattr = 0;
    215   1.1  drochner 
    216   1.1  drochner 		if (d->numlocators) {
    217   1.1  drochner 			if (d->numlocators > MAXLOCATORS)
    218   1.1  drochner 				return (EINVAL);
    219   1.1  drochner 			locs = malloc(d->numlocators * sizeof(int), M_DEVBUF,
    220   1.1  drochner 				      M_WAITOK);
    221   1.1  drochner 			res = copyin(d->locators, locs,
    222   1.1  drochner 				     d->numlocators * sizeof(int));
    223  1.11     rmind 			if (res) {
    224  1.11     rmind 				free(locs, M_DEVBUF);
    225   1.1  drochner 				return (res);
    226  1.11     rmind 			}
    227   1.1  drochner 		} else
    228   1.1  drochner 			locs = 0;
    229   1.1  drochner 		res = rescanbus(d->busname, ifattr, d->numlocators, locs);
    230   1.1  drochner 		if (locs)
    231   1.1  drochner 			free(locs, M_DEVBUF);
    232   1.1  drochner #undef d
    233   1.5   thorpej 		break;
    234   1.5   thorpej 	case DRVCTLCOMMAND:
    235   1.5   thorpej 	    	res = drvctl_command(p, (struct plistref *)data, cmd, flag);
    236   1.5   thorpej 	    	break;
    237   1.5   thorpej 	default:
    238   1.5   thorpej 		return (EPASSTHROUGH);
    239   1.1  drochner 	}
    240   1.1  drochner 	return (res);
    241   1.1  drochner }
    242   1.1  drochner 
    243   1.1  drochner void
    244   1.9      yamt drvctlattach(int arg)
    245   1.1  drochner {
    246   1.1  drochner }
    247   1.5   thorpej 
    248   1.5   thorpej /*****************************************************************************
    249   1.5   thorpej  * Driver control command processing engine
    250   1.5   thorpej  *****************************************************************************/
    251   1.5   thorpej 
    252   1.5   thorpej static int
    253   1.9      yamt drvctl_command_get_properties(struct lwp *l,
    254   1.5   thorpej 			      prop_dictionary_t command_dict,
    255   1.5   thorpej 			      prop_dictionary_t results_dict)
    256   1.5   thorpej {
    257   1.5   thorpej 	prop_dictionary_t args_dict;
    258   1.5   thorpej 	prop_string_t devname_string;
    259   1.5   thorpej 	device_t dev;
    260   1.5   thorpej 
    261   1.5   thorpej 	args_dict = prop_dictionary_get(command_dict, "drvctl-arguments");
    262   1.5   thorpej 	if (args_dict == NULL)
    263   1.5   thorpej 		return (EINVAL);
    264   1.5   thorpej 
    265   1.5   thorpej 	devname_string = prop_dictionary_get(args_dict, "device-name");
    266   1.5   thorpej 	if (devname_string == NULL)
    267   1.5   thorpej 		return (EINVAL);
    268   1.5   thorpej 
    269   1.5   thorpej 	TAILQ_FOREACH(dev, &alldevs, dv_list) {
    270   1.5   thorpej 		if (prop_string_equals_cstring(devname_string,
    271   1.5   thorpej 					       device_xname(dev)))
    272   1.5   thorpej 			break;
    273   1.5   thorpej 	}
    274   1.5   thorpej 
    275   1.5   thorpej 	if (dev == NULL)
    276   1.5   thorpej 		return (ESRCH);
    277   1.5   thorpej 
    278   1.5   thorpej 	prop_dictionary_set(results_dict, "drvctl-result-data",
    279   1.5   thorpej 			    device_properties(dev));
    280   1.5   thorpej 
    281   1.5   thorpej 	return (0);
    282   1.5   thorpej }
    283   1.5   thorpej 
    284   1.5   thorpej struct drvctl_command_desc {
    285   1.5   thorpej 	const char *dcd_name;		/* command name */
    286   1.5   thorpej 	int (*dcd_func)(struct lwp *,	/* handler function */
    287   1.5   thorpej 			prop_dictionary_t,
    288   1.5   thorpej 			prop_dictionary_t);
    289   1.5   thorpej 	int dcd_rw;			/* read or write required */
    290   1.5   thorpej };
    291   1.5   thorpej 
    292   1.5   thorpej static const struct drvctl_command_desc drvctl_command_table[] = {
    293   1.5   thorpej 	{ .dcd_name = "get-properties",
    294   1.5   thorpej 	  .dcd_func = drvctl_command_get_properties,
    295   1.5   thorpej 	  .dcd_rw   = FREAD,
    296   1.5   thorpej 	},
    297   1.5   thorpej 
    298   1.5   thorpej 	{ .dcd_name = NULL }
    299   1.5   thorpej };
    300   1.5   thorpej 
    301   1.5   thorpej static int
    302   1.5   thorpej drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
    303   1.5   thorpej 	       int fflag)
    304   1.5   thorpej {
    305   1.5   thorpej 	prop_dictionary_t command_dict, results_dict;
    306   1.5   thorpej 	prop_string_t command_string;
    307   1.5   thorpej 	const struct drvctl_command_desc *dcd;
    308   1.5   thorpej 	int error;
    309   1.5   thorpej 
    310   1.5   thorpej 	error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict);
    311   1.5   thorpej 	if (error)
    312   1.5   thorpej 		return (error);
    313   1.5   thorpej 
    314   1.5   thorpej 	results_dict = prop_dictionary_create();
    315   1.5   thorpej 	if (results_dict == NULL) {
    316   1.5   thorpej 		prop_object_release(command_dict);
    317   1.5   thorpej 		return (ENOMEM);
    318   1.5   thorpej 	}
    319   1.5   thorpej 
    320   1.5   thorpej 	command_string = prop_dictionary_get(command_dict, "drvctl-command");
    321   1.5   thorpej 	if (command_string == NULL) {
    322   1.5   thorpej 		error = EINVAL;
    323   1.5   thorpej 		goto out;
    324   1.5   thorpej 	}
    325   1.5   thorpej 
    326   1.5   thorpej 	for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) {
    327   1.5   thorpej 		if (prop_string_equals_cstring(command_string,
    328   1.5   thorpej 					       dcd->dcd_name))
    329   1.5   thorpej 			break;
    330   1.5   thorpej 	}
    331   1.5   thorpej 
    332   1.5   thorpej 	if (dcd->dcd_name == NULL) {
    333   1.5   thorpej 		error = EINVAL;
    334   1.5   thorpej 		goto out;
    335   1.5   thorpej 	}
    336   1.5   thorpej 
    337   1.5   thorpej 	if ((fflag & dcd->dcd_rw) == 0) {
    338   1.5   thorpej 		error = EPERM;
    339   1.5   thorpej 		goto out;
    340   1.5   thorpej 	}
    341   1.5   thorpej 
    342   1.5   thorpej 	error = (*dcd->dcd_func)(l, command_dict, results_dict);
    343   1.5   thorpej 
    344   1.8   thorpej 	prop_dictionary_set_int32(results_dict, "drvctl-error", error);
    345   1.5   thorpej 
    346   1.5   thorpej 	error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict);
    347   1.5   thorpej  out:
    348   1.5   thorpej 	prop_object_release(command_dict);
    349   1.5   thorpej 	prop_object_release(results_dict);
    350   1.5   thorpej 	return (error);
    351   1.5   thorpej }
    352