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