Home | History | Annotate | Line # | Download | only in kern
kern_drvctl.c revision 1.5
      1  1.5   thorpej /* $NetBSD: kern_drvctl.c,v 1.5 2006/09/22 04:37:37 thorpej 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.5   thorpej __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.5 2006/09/22 04:37:37 thorpej 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.1  drochner 	nostop, notty, nopoll, nommap, nokqfilter,
     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.1  drochner static int
     57  1.1  drochner detachdevbyname(const char *devname)
     58  1.1  drochner {
     59  1.1  drochner 	struct device *d;
     60  1.1  drochner 
     61  1.1  drochner 	TAILQ_FOREACH(d, &alldevs, dv_list) {
     62  1.1  drochner 		if (!strcmp(devname, d->dv_xname)) {
     63  1.1  drochner #ifndef XXXFULLRISK
     64  1.1  drochner 			/*
     65  1.1  drochner 			 * If the parent cannot be notified, it might keep
     66  1.1  drochner 			 * pointers to the detached device.
     67  1.1  drochner 			 * There might be a private notification mechanism,
     68  1.1  drochner 			 * but better play save here.
     69  1.1  drochner 			 */
     70  1.1  drochner 			if (d->dv_parent &&
     71  1.1  drochner 			    !d->dv_parent->dv_cfattach->ca_childdetached)
     72  1.1  drochner 				return (ENOTSUP);
     73  1.1  drochner #endif
     74  1.1  drochner 			return (config_detach(d, 0));
     75  1.1  drochner 		}
     76  1.1  drochner 	}
     77  1.1  drochner 
     78  1.1  drochner 	return (ENXIO);
     79  1.1  drochner }
     80  1.1  drochner 
     81  1.1  drochner static int
     82  1.1  drochner rescanbus(const char *busname, const char *ifattr,
     83  1.1  drochner 	  int numlocators, const int *locators)
     84  1.1  drochner {
     85  1.1  drochner 	int i;
     86  1.1  drochner 	struct device *d;
     87  1.2  drochner 	const struct cfiattrdata * const *ap;
     88  1.1  drochner 
     89  1.1  drochner 	/* XXX there should be a way to get limits and defaults (per device)
     90  1.1  drochner 	   from config generated data */
     91  1.1  drochner 	int locs[MAXLOCATORS];
     92  1.1  drochner 	for (i = 0; i < MAXLOCATORS; i++)
     93  1.1  drochner 		locs[i] = -1;
     94  1.1  drochner 
     95  1.1  drochner 	for (i = 0; i < numlocators;i++)
     96  1.1  drochner 		locs[i] = locators[i];
     97  1.1  drochner 
     98  1.1  drochner 	TAILQ_FOREACH(d, &alldevs, dv_list) {
     99  1.1  drochner 		if (!strcmp(busname, d->dv_xname)) {
    100  1.1  drochner 			/*
    101  1.1  drochner 			 * must support rescan, and must have something
    102  1.1  drochner 			 * to attach to
    103  1.1  drochner 			 */
    104  1.1  drochner 			if (!d->dv_cfattach->ca_rescan ||
    105  1.1  drochner 			    !d->dv_cfdriver->cd_attrs)
    106  1.1  drochner 				return (ENODEV);
    107  1.1  drochner 
    108  1.1  drochner 			/* allow to omit attribute if there is exactly one */
    109  1.1  drochner 			if (!ifattr) {
    110  1.1  drochner 				if (d->dv_cfdriver->cd_attrs[1])
    111  1.1  drochner 					return (EINVAL);
    112  1.2  drochner 				ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
    113  1.1  drochner 			} else {
    114  1.1  drochner 				/* check for valid attribute passed */
    115  1.1  drochner 				for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
    116  1.2  drochner 					if (!strcmp((*ap)->ci_name, ifattr))
    117  1.1  drochner 						break;
    118  1.1  drochner 				if (!*ap)
    119  1.1  drochner 					return (EINVAL);
    120  1.1  drochner 			}
    121  1.1  drochner 
    122  1.1  drochner 			return (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
    123  1.1  drochner 		}
    124  1.1  drochner 	}
    125  1.1  drochner 
    126  1.1  drochner 	return (ENXIO);
    127  1.1  drochner }
    128  1.1  drochner 
    129  1.1  drochner int
    130  1.4      cube drvctlioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *p)
    131  1.1  drochner {
    132  1.1  drochner 	int res;
    133  1.1  drochner 	char *ifattr;
    134  1.1  drochner 	int *locs;
    135  1.1  drochner 
    136  1.1  drochner 	switch (cmd) {
    137  1.1  drochner 	case DRVDETACHDEV:
    138  1.1  drochner #define d ((struct devdetachargs *)data)
    139  1.1  drochner 		res = detachdevbyname(d->devname);
    140  1.1  drochner #undef d
    141  1.1  drochner 		break;
    142  1.1  drochner 	case DRVRESCANBUS:
    143  1.1  drochner #define d ((struct devrescanargs *)data)
    144  1.1  drochner 		d->busname[sizeof(d->busname) - 1] = '\0';
    145  1.1  drochner 
    146  1.1  drochner 		/* XXX better copyin? */
    147  1.1  drochner 		if (d->ifattr[0]) {
    148  1.1  drochner 			d->ifattr[sizeof(d->ifattr) - 1] = '\0';
    149  1.1  drochner 			ifattr = d->ifattr;
    150  1.1  drochner 		} else
    151  1.1  drochner 			ifattr = 0;
    152  1.1  drochner 
    153  1.1  drochner 		if (d->numlocators) {
    154  1.1  drochner 			if (d->numlocators > MAXLOCATORS)
    155  1.1  drochner 				return (EINVAL);
    156  1.1  drochner 			locs = malloc(d->numlocators * sizeof(int), M_DEVBUF,
    157  1.1  drochner 				      M_WAITOK);
    158  1.1  drochner 			res = copyin(d->locators, locs,
    159  1.1  drochner 				     d->numlocators * sizeof(int));
    160  1.1  drochner 			if (res)
    161  1.1  drochner 				return (res);
    162  1.1  drochner 		} else
    163  1.1  drochner 			locs = 0;
    164  1.1  drochner 		res = rescanbus(d->busname, ifattr, d->numlocators, locs);
    165  1.1  drochner 		if (locs)
    166  1.1  drochner 			free(locs, M_DEVBUF);
    167  1.1  drochner #undef d
    168  1.5   thorpej 		break;
    169  1.5   thorpej 	case DRVCTLCOMMAND:
    170  1.5   thorpej 	    	res = drvctl_command(p, (struct plistref *)data, cmd, flag);
    171  1.5   thorpej 	    	break;
    172  1.5   thorpej 	default:
    173  1.5   thorpej 		return (EPASSTHROUGH);
    174  1.1  drochner 	}
    175  1.1  drochner 	return (res);
    176  1.1  drochner }
    177  1.1  drochner 
    178  1.1  drochner void
    179  1.1  drochner drvctlattach(int arg)
    180  1.1  drochner {
    181  1.1  drochner }
    182  1.5   thorpej 
    183  1.5   thorpej /*****************************************************************************
    184  1.5   thorpej  * Driver control command processing engine
    185  1.5   thorpej  *****************************************************************************/
    186  1.5   thorpej 
    187  1.5   thorpej static int
    188  1.5   thorpej drvctl_command_get_properties(struct lwp *l,
    189  1.5   thorpej 			      prop_dictionary_t command_dict,
    190  1.5   thorpej 			      prop_dictionary_t results_dict)
    191  1.5   thorpej {
    192  1.5   thorpej 	prop_dictionary_t args_dict;
    193  1.5   thorpej 	prop_string_t devname_string;
    194  1.5   thorpej 	device_t dev;
    195  1.5   thorpej 
    196  1.5   thorpej 	args_dict = prop_dictionary_get(command_dict, "drvctl-arguments");
    197  1.5   thorpej 	if (args_dict == NULL)
    198  1.5   thorpej 		return (EINVAL);
    199  1.5   thorpej 
    200  1.5   thorpej 	devname_string = prop_dictionary_get(args_dict, "device-name");
    201  1.5   thorpej 	if (devname_string == NULL)
    202  1.5   thorpej 		return (EINVAL);
    203  1.5   thorpej 
    204  1.5   thorpej 	TAILQ_FOREACH(dev, &alldevs, dv_list) {
    205  1.5   thorpej 		if (prop_string_equals_cstring(devname_string,
    206  1.5   thorpej 					       device_xname(dev)))
    207  1.5   thorpej 			break;
    208  1.5   thorpej 	}
    209  1.5   thorpej 
    210  1.5   thorpej 	if (dev == NULL)
    211  1.5   thorpej 		return (ESRCH);
    212  1.5   thorpej 
    213  1.5   thorpej 	prop_dictionary_set(results_dict, "drvctl-result-data",
    214  1.5   thorpej 			    device_properties(dev));
    215  1.5   thorpej 
    216  1.5   thorpej 	return (0);
    217  1.5   thorpej }
    218  1.5   thorpej 
    219  1.5   thorpej struct drvctl_command_desc {
    220  1.5   thorpej 	const char *dcd_name;		/* command name */
    221  1.5   thorpej 	int (*dcd_func)(struct lwp *,	/* handler function */
    222  1.5   thorpej 			prop_dictionary_t,
    223  1.5   thorpej 			prop_dictionary_t);
    224  1.5   thorpej 	int dcd_rw;			/* read or write required */
    225  1.5   thorpej };
    226  1.5   thorpej 
    227  1.5   thorpej static const struct drvctl_command_desc drvctl_command_table[] = {
    228  1.5   thorpej 	{ .dcd_name = "get-properties",
    229  1.5   thorpej 	  .dcd_func = drvctl_command_get_properties,
    230  1.5   thorpej 	  .dcd_rw   = FREAD,
    231  1.5   thorpej 	},
    232  1.5   thorpej 
    233  1.5   thorpej 	{ .dcd_name = NULL }
    234  1.5   thorpej };
    235  1.5   thorpej 
    236  1.5   thorpej static int
    237  1.5   thorpej drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
    238  1.5   thorpej 	       int fflag)
    239  1.5   thorpej {
    240  1.5   thorpej 	prop_dictionary_t command_dict, results_dict;
    241  1.5   thorpej 	prop_string_t command_string;
    242  1.5   thorpej 	prop_number_t error_number;
    243  1.5   thorpej 	const struct drvctl_command_desc *dcd;
    244  1.5   thorpej 	int error;
    245  1.5   thorpej 
    246  1.5   thorpej 	error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict);
    247  1.5   thorpej 	if (error)
    248  1.5   thorpej 		return (error);
    249  1.5   thorpej 
    250  1.5   thorpej 	results_dict = prop_dictionary_create();
    251  1.5   thorpej 	if (results_dict == NULL) {
    252  1.5   thorpej 		prop_object_release(command_dict);
    253  1.5   thorpej 		return (ENOMEM);
    254  1.5   thorpej 	}
    255  1.5   thorpej 
    256  1.5   thorpej 	command_string = prop_dictionary_get(command_dict, "drvctl-command");
    257  1.5   thorpej 	if (command_string == NULL) {
    258  1.5   thorpej 		error = EINVAL;
    259  1.5   thorpej 		goto out;
    260  1.5   thorpej 	}
    261  1.5   thorpej 
    262  1.5   thorpej 	for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) {
    263  1.5   thorpej 		if (prop_string_equals_cstring(command_string,
    264  1.5   thorpej 					       dcd->dcd_name))
    265  1.5   thorpej 			break;
    266  1.5   thorpej 	}
    267  1.5   thorpej 
    268  1.5   thorpej 	if (dcd->dcd_name == NULL) {
    269  1.5   thorpej 		error = EINVAL;
    270  1.5   thorpej 		goto out;
    271  1.5   thorpej 	}
    272  1.5   thorpej 
    273  1.5   thorpej 	if ((fflag & dcd->dcd_rw) == 0) {
    274  1.5   thorpej 		error = EPERM;
    275  1.5   thorpej 		goto out;
    276  1.5   thorpej 	}
    277  1.5   thorpej 
    278  1.5   thorpej 	error = (*dcd->dcd_func)(l, command_dict, results_dict);
    279  1.5   thorpej 
    280  1.5   thorpej 	error_number = prop_number_create_integer(error);
    281  1.5   thorpej 	prop_dictionary_set(results_dict, "drvctl-error", error_number);
    282  1.5   thorpej 	prop_object_release(error_number);
    283  1.5   thorpej 
    284  1.5   thorpej 	error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict);
    285  1.5   thorpej  out:
    286  1.5   thorpej 	prop_object_release(command_dict);
    287  1.5   thorpej 	prop_object_release(results_dict);
    288  1.5   thorpej 	return (error);
    289  1.5   thorpej }
    290