Home | History | Annotate | Line # | Download | only in kern
kern_drvctl.c revision 1.1
      1  1.1  drochner /* $NetBSD: kern_drvctl.c,v 1.1 2004/08/18 12:19:29 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.1  drochner __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.1 2004/08/18 12:19:29 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.1  drochner #include <sys/drvctlio.h>
     41  1.1  drochner 
     42  1.1  drochner dev_type_ioctl(drvctlioctl);
     43  1.1  drochner 
     44  1.1  drochner const struct cdevsw drvctl_cdevsw = {
     45  1.1  drochner 	nullopen, nullclose, nullread, nullwrite, drvctlioctl,
     46  1.1  drochner 	nostop, notty, nopoll, nommap, nokqfilter,
     47  1.1  drochner };
     48  1.1  drochner 
     49  1.1  drochner void drvctlattach(int);
     50  1.1  drochner 
     51  1.1  drochner #define MAXLOCATORS 100
     52  1.1  drochner 
     53  1.1  drochner static int
     54  1.1  drochner detachdevbyname(const char *devname)
     55  1.1  drochner {
     56  1.1  drochner 	struct device *d;
     57  1.1  drochner 
     58  1.1  drochner 	TAILQ_FOREACH(d, &alldevs, dv_list) {
     59  1.1  drochner 		if (!strcmp(devname, d->dv_xname)) {
     60  1.1  drochner #ifndef XXXFULLRISK
     61  1.1  drochner 			/*
     62  1.1  drochner 			 * If the parent cannot be notified, it might keep
     63  1.1  drochner 			 * pointers to the detached device.
     64  1.1  drochner 			 * There might be a private notification mechanism,
     65  1.1  drochner 			 * but better play save here.
     66  1.1  drochner 			 */
     67  1.1  drochner 			if (d->dv_parent &&
     68  1.1  drochner 			    !d->dv_parent->dv_cfattach->ca_childdetached)
     69  1.1  drochner 				return (ENOTSUP);
     70  1.1  drochner #endif
     71  1.1  drochner 			return (config_detach(d, 0));
     72  1.1  drochner 		}
     73  1.1  drochner 	}
     74  1.1  drochner 
     75  1.1  drochner 	return (ENXIO);
     76  1.1  drochner }
     77  1.1  drochner 
     78  1.1  drochner static int
     79  1.1  drochner rescanbus(const char *busname, const char *ifattr,
     80  1.1  drochner 	  int numlocators, const int *locators)
     81  1.1  drochner {
     82  1.1  drochner 	int i;
     83  1.1  drochner 	struct device *d;
     84  1.1  drochner 	const char * const *ap;
     85  1.1  drochner 
     86  1.1  drochner 	/* XXX there should be a way to get limits and defaults (per device)
     87  1.1  drochner 	   from config generated data */
     88  1.1  drochner 	int locs[MAXLOCATORS];
     89  1.1  drochner 	for (i = 0; i < MAXLOCATORS; i++)
     90  1.1  drochner 		locs[i] = -1;
     91  1.1  drochner 
     92  1.1  drochner 	for (i = 0; i < numlocators;i++)
     93  1.1  drochner 		locs[i] = locators[i];
     94  1.1  drochner 
     95  1.1  drochner 	TAILQ_FOREACH(d, &alldevs, dv_list) {
     96  1.1  drochner 		if (!strcmp(busname, d->dv_xname)) {
     97  1.1  drochner 			/*
     98  1.1  drochner 			 * must support rescan, and must have something
     99  1.1  drochner 			 * to attach to
    100  1.1  drochner 			 */
    101  1.1  drochner 			if (!d->dv_cfattach->ca_rescan ||
    102  1.1  drochner 			    !d->dv_cfdriver->cd_attrs)
    103  1.1  drochner 				return (ENODEV);
    104  1.1  drochner 
    105  1.1  drochner 			/* allow to omit attribute if there is exactly one */
    106  1.1  drochner 			if (!ifattr) {
    107  1.1  drochner 				if (d->dv_cfdriver->cd_attrs[1])
    108  1.1  drochner 					return (EINVAL);
    109  1.1  drochner 				ifattr = d->dv_cfdriver->cd_attrs[0];
    110  1.1  drochner 			} else {
    111  1.1  drochner 				/* check for valid attribute passed */
    112  1.1  drochner 				for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
    113  1.1  drochner 					if (!strcmp(*ap, ifattr))
    114  1.1  drochner 						break;
    115  1.1  drochner 				if (!*ap)
    116  1.1  drochner 					return (EINVAL);
    117  1.1  drochner 			}
    118  1.1  drochner 
    119  1.1  drochner 			return (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
    120  1.1  drochner 		}
    121  1.1  drochner 	}
    122  1.1  drochner 
    123  1.1  drochner 	return (ENXIO);
    124  1.1  drochner }
    125  1.1  drochner 
    126  1.1  drochner int
    127  1.1  drochner drvctlioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    128  1.1  drochner {
    129  1.1  drochner 	int res;
    130  1.1  drochner 	char *ifattr;
    131  1.1  drochner 	int *locs;
    132  1.1  drochner 
    133  1.1  drochner 	switch (cmd) {
    134  1.1  drochner 	case DRVDETACHDEV:
    135  1.1  drochner #define d ((struct devdetachargs *)data)
    136  1.1  drochner 		res = detachdevbyname(d->devname);
    137  1.1  drochner #undef d
    138  1.1  drochner 		break;
    139  1.1  drochner 	case DRVRESCANBUS:
    140  1.1  drochner #define d ((struct devrescanargs *)data)
    141  1.1  drochner 		d->busname[sizeof(d->busname) - 1] = '\0';
    142  1.1  drochner 
    143  1.1  drochner 		/* XXX better copyin? */
    144  1.1  drochner 		if (d->ifattr[0]) {
    145  1.1  drochner 			d->ifattr[sizeof(d->ifattr) - 1] = '\0';
    146  1.1  drochner 			ifattr = d->ifattr;
    147  1.1  drochner 		} else
    148  1.1  drochner 			ifattr = 0;
    149  1.1  drochner 
    150  1.1  drochner 		if (d->numlocators) {
    151  1.1  drochner 			if (d->numlocators > MAXLOCATORS)
    152  1.1  drochner 				return (EINVAL);
    153  1.1  drochner 			locs = malloc(d->numlocators * sizeof(int), M_DEVBUF,
    154  1.1  drochner 				      M_WAITOK);
    155  1.1  drochner 			res = copyin(d->locators, locs,
    156  1.1  drochner 				     d->numlocators * sizeof(int));
    157  1.1  drochner 			if (res)
    158  1.1  drochner 				return (res);
    159  1.1  drochner 		} else
    160  1.1  drochner 			locs = 0;
    161  1.1  drochner 		res = rescanbus(d->busname, ifattr, d->numlocators, locs);
    162  1.1  drochner 		if (locs)
    163  1.1  drochner 			free(locs, M_DEVBUF);
    164  1.1  drochner #undef d
    165  1.1  drochner 			break;
    166  1.1  drochner 		default:
    167  1.1  drochner 			return (EPASSTHROUGH);
    168  1.1  drochner 	}
    169  1.1  drochner 	return (res);
    170  1.1  drochner }
    171  1.1  drochner 
    172  1.1  drochner void
    173  1.1  drochner drvctlattach(int arg)
    174  1.1  drochner {
    175  1.1  drochner }
    176