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