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