Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.15
      1 /*	$NetBSD: sysmon_envsys.c,v 1.15 2007/02/19 00:36:12 xtraeme Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Zembu Labs, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by Zembu Labs, Inc.
     20  * 4. Neither the name of Zembu Labs nor the names of its employees may
     21  *    be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     26  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     27  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Environmental sensor framework for sysmon.  Hardware monitors
     38  * such as the LM78 and VIA 82C686A (or even ACPI, eventually) can
     39  * register themselves to provide backplane fan and temperature
     40  * information, etc.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.15 2007/02/19 00:36:12 xtraeme Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/conf.h>
     48 #include <sys/errno.h>
     49 #include <sys/fcntl.h>
     50 #include <sys/lock.h>
     51 #include <sys/kernel.h>
     52 #include <sys/systm.h>
     53 #include <sys/proc.h>
     54 #include <sys/mutex.h>
     55 #include <sys/condvar.h>
     56 
     57 #include <dev/sysmon/sysmonvar.h>
     58 
     59 /*
     60  * We run at ENVSYS version 1.
     61  */
     62 #define	SYSMON_ENVSYS_VERSION	(1 * 1000)
     63 
     64 kmutex_t sysmon_envsys_mtx;
     65 
     66 LIST_HEAD(, sysmon_envsys) sysmon_envsys_list =
     67     LIST_HEAD_INITIALIZER(&sysmon_envsys_list);
     68 kmutex_t sysmon_envsys_list_mtx;
     69 kcondvar_t sysmon_envsys_cv;
     70 u_int	sysmon_envsys_next_sensor_index;
     71 
     72 int	sysmon_envsys_initialized;
     73 kmutex_t sysmon_envsys_initialized_mtx;
     74 
     75 struct sysmon_envsys *sysmon_envsys_find(u_int);
     76 void	sysmon_envsys_release(struct sysmon_envsys *);
     77 
     78 /*
     79  * sysmonopen_envsys:
     80  *
     81  *	Open the system monitor device.
     82  */
     83 int
     84 sysmonopen_envsys(dev_t dev, int flag, int mode,
     85     struct lwp *l)
     86 {
     87 	mutex_enter(&sysmon_envsys_initialized_mtx);
     88 	if (sysmon_envsys_initialized == 0) {
     89 		mutex_init(&sysmon_envsys_mtx, MUTEX_DRIVER, IPL_NONE);
     90 		sysmon_envsys_initialized = 1;
     91 	}
     92 	mutex_exit(&sysmon_envsys_initialized_mtx);
     93 
     94 	return (0);
     95 }
     96 
     97 /*
     98  * sysmonclose_envsys:
     99  *
    100  *	Close the system monitor device.
    101  */
    102 int
    103 sysmonclose_envsys(dev_t dev, int flag, int mode,
    104     struct lwp *l)
    105 {
    106 
    107 	/* Nothing to do */
    108 	return (0);
    109 }
    110 
    111 /*
    112  * sysmonioctl_envsys:
    113  *
    114  *	Perform an envsys control request.
    115  */
    116 int
    117 sysmonioctl_envsys(dev_t dev, u_long cmd, caddr_t data,
    118     int flag, struct lwp *l)
    119 {
    120 	struct sysmon_envsys *sme;
    121 	int error = 0;
    122 	u_int oidx;
    123 
    124 	switch (cmd) {
    125 	/*
    126 	 * For ENVSYS commands, we translate the absolute sensor index
    127 	 * to a device-relative sensor index.
    128 	 */
    129 	case ENVSYS_VERSION:
    130 		*(int32_t *)data = SYSMON_ENVSYS_VERSION;
    131 		break;
    132 
    133 	case ENVSYS_GRANGE:
    134 	    {
    135 		struct envsys_range *rng = (void *) data;
    136 		int i;
    137 
    138 
    139 		/* Return empty range unless we find something better */
    140 		rng->low = 1;
    141 		rng->high = 0;
    142 
    143 		if (rng->units == -1) {
    144 			rng->low = 0;
    145 			rng->high = sysmon_envsys_next_sensor_index;
    146 			break;
    147 		}
    148 
    149 		sme = sysmon_envsys_find(0);	/* XXX */
    150 		if (sme == NULL) {
    151 			/* Return empty range for `no sensors'. */
    152 			break;
    153 		}
    154 		for (i = 0;
    155 		     sme->sme_ranges[i].low <= sme->sme_ranges[i].high;
    156 		     i++) {
    157 			if (sme->sme_ranges[i].units == rng->units) {
    158 				*rng = sme->sme_ranges[i];
    159 				break;
    160 			}
    161 		}
    162 		sysmon_envsys_release(sme);
    163 		break;
    164 	    }
    165 
    166 	case ENVSYS_GTREDATA:
    167 	    {
    168 		struct envsys_tre_data *tred = (void *) data;
    169 
    170 		tred->validflags = 0;
    171 
    172 		sme = sysmon_envsys_find(tred->sensor);
    173 		if (sme == NULL)
    174 			break;
    175 		oidx = tred->sensor;
    176 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    177 		if (tred->sensor < sme->sme_nsensors
    178 		    && sme->sme_gtredata != NULL) {
    179 			mutex_enter(&sysmon_envsys_mtx);
    180 			error = (*sme->sme_gtredata)(sme, tred);
    181 			mutex_exit(&sysmon_envsys_mtx);
    182 		}
    183 		tred->sensor = oidx;
    184 		sysmon_envsys_release(sme);
    185 		break;
    186 	    }
    187 
    188 	case ENVSYS_STREINFO:
    189 	    {
    190 		struct envsys_basic_info *binfo = (void *) data;
    191 
    192 		sme = sysmon_envsys_find(binfo->sensor);
    193 		if (sme == NULL) {
    194 			binfo->validflags = 0;
    195 			break;
    196 		}
    197 		oidx = binfo->sensor;
    198 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    199 		if (binfo->sensor < sme->sme_nsensors
    200 		    && sme->sme_streinfo != NULL) {
    201 			mutex_enter(&sysmon_envsys_mtx);
    202 			error = (*sme->sme_streinfo)(sme, binfo);
    203 			mutex_exit(&sysmon_envsys_mtx);
    204 		} else
    205 			binfo->validflags = 0;
    206 		binfo->sensor = oidx;
    207 		sysmon_envsys_release(sme);
    208 		break;
    209 	    }
    210 
    211 	case ENVSYS_GTREINFO:
    212 	    {
    213 		struct envsys_basic_info *binfo = (void *) data;
    214 
    215 		binfo->validflags = 0;
    216 
    217 		sme = sysmon_envsys_find(binfo->sensor);
    218 		if (sme == NULL)
    219 			break;
    220 		oidx = binfo->sensor;
    221 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    222 		if (binfo->sensor < sme->sme_nsensors)
    223 			*binfo = sme->sme_sensor_info[binfo->sensor];
    224 		binfo->sensor = oidx;
    225 		sysmon_envsys_release(sme);
    226 		break;
    227 	    }
    228 
    229 	default:
    230 		error = ENOTTY;
    231 	}
    232 
    233 	return (error);
    234 }
    235 
    236 /*
    237  * sysmon_envsys_register:
    238  *
    239  *	Register an ENVSYS device.
    240  */
    241 int
    242 sysmon_envsys_register(struct sysmon_envsys *sme)
    243 {
    244 	int error = 0;
    245 
    246 	mutex_init(&sysmon_envsys_list_mtx, MUTEX_DRIVER, IPL_NONE);
    247 	mutex_init(&sysmon_envsys_initialized_mtx, MUTEX_DRIVER, IPL_NONE);
    248 	cv_init(&sysmon_envsys_cv, "smestate");
    249 
    250 	KASSERT((sme->sme_flags & (SME_FLAG_BUSY | SME_FLAG_WANTED)) == 0);
    251 	mutex_enter(&sysmon_envsys_list_mtx);
    252 
    253 	if (sme->sme_envsys_version != SYSMON_ENVSYS_VERSION) {
    254 		error = EINVAL;
    255 		goto out;
    256 	}
    257 
    258 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    259 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    260 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    261 
    262  out:
    263 	mutex_exit(&sysmon_envsys_list_mtx);
    264 	return (error);
    265 }
    266 
    267 /*
    268  * sysmon_envsys_unregister:
    269  *
    270  *	Unregister an ENVSYS device.
    271  */
    272 void
    273 sysmon_envsys_unregister(struct sysmon_envsys *sme)
    274 {
    275 
    276 	mutex_enter(&sysmon_envsys_list_mtx);
    277 	while (sme->sme_flags & SME_FLAG_BUSY) {
    278 		sme->sme_flags |= SME_FLAG_WANTED;
    279 		cv_wait(&sysmon_envsys_cv, &sysmon_envsys_list_mtx);
    280 	}
    281 	LIST_REMOVE(sme, sme_list);
    282 	mutex_exit(&sysmon_envsys_list_mtx);
    283 }
    284 
    285 /*
    286  * sysmon_envsys_find:
    287  *
    288  *	Find an ENVSYS device.
    289  *	the found device should be sysmon_envsys_release'ed by the caller.
    290  */
    291 struct sysmon_envsys *
    292 sysmon_envsys_find(u_int idx)
    293 {
    294 	struct sysmon_envsys *sme;
    295 
    296 	mutex_enter(&sysmon_envsys_list_mtx);
    297 again:
    298 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    299 	     sme = LIST_NEXT(sme, sme_list)) {
    300 		if (idx >= sme->sme_fsensor &&
    301 		    idx < (sme->sme_fsensor + sme->sme_nsensors)) {
    302 			if (sme->sme_flags & SME_FLAG_BUSY) {
    303 				sme->sme_flags |= SME_FLAG_WANTED;
    304 				cv_wait(&sysmon_envsys_cv,
    305 				    &sysmon_envsys_list_mtx);
    306 				goto again;
    307 			}
    308 			sme->sme_flags |= SME_FLAG_BUSY;
    309 			break;
    310 		}
    311 	}
    312 
    313 	mutex_exit(&sysmon_envsys_list_mtx);
    314 	return sme;
    315 }
    316 
    317 /*
    318  * sysmon_envsys_release:
    319  *
    320  *	Release an ENVSYS device.
    321  */
    322 /* ARGSUSED */
    323 void
    324 sysmon_envsys_release(struct sysmon_envsys *sme)
    325 {
    326 
    327 	KASSERT(sme->sme_flags & SME_FLAG_BUSY);
    328 
    329 	mutex_enter(&sysmon_envsys_list_mtx);
    330 	if (sme->sme_flags & SME_FLAG_WANTED)
    331 		cv_broadcast(&sysmon_envsys_cv);
    332 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    333 	mutex_exit(&sysmon_envsys_list_mtx);
    334 }
    335