Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.18
      1  1.18   xtraeme /*	$NetBSD: sysmon_envsys.c,v 1.18 2007/07/01 07:36:45 xtraeme Exp $	*/
      2  1.18   xtraeme 
      3  1.18   xtraeme /*-
      4  1.18   xtraeme  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.18   xtraeme  * All rights reserved.
      6  1.18   xtraeme  *
      7  1.18   xtraeme  * This code is derived from software contributed to The NetBSD Foundation
      8  1.18   xtraeme  * by Juan Romero Pardines.
      9  1.18   xtraeme  *
     10  1.18   xtraeme  * Redistribution and use in source and binary forms, with or without
     11  1.18   xtraeme  * modification, are permitted provided that the following conditions
     12  1.18   xtraeme  * are met:
     13  1.18   xtraeme  * 1. Redistributions of source code must retain the above copyright
     14  1.18   xtraeme  *    notice, this list of conditions and the following disclaimer.
     15  1.18   xtraeme  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.18   xtraeme  *    notice, this list of conditions and the following disclaimer in the
     17  1.18   xtraeme  *    documentation and/or other materials provided with the distribution.
     18  1.18   xtraeme  * 3. All advertising materials mentioning features or use of this software
     19  1.18   xtraeme  *    must display the following acknowledgement:
     20  1.18   xtraeme  *      This product includes software developed by Juan Romero Pardines
     21  1.18   xtraeme  *      for the NetBSD Foundation, Inc. and its contributors.
     22  1.18   xtraeme  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.18   xtraeme  *    contributors may be used to endorse or promote products derived
     24  1.18   xtraeme  *    from this software without specific prior written permission.
     25  1.18   xtraeme  *
     26  1.18   xtraeme  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.18   xtraeme  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.18   xtraeme  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.18   xtraeme  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.18   xtraeme  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.18   xtraeme  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.18   xtraeme  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.18   xtraeme  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.18   xtraeme  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.18   xtraeme  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.18   xtraeme  * POSSIBILITY OF SUCH DAMAGE.
     37  1.18   xtraeme  */
     38   1.1   thorpej 
     39   1.1   thorpej /*-
     40   1.1   thorpej  * Copyright (c) 2000 Zembu Labs, Inc.
     41   1.1   thorpej  * All rights reserved.
     42   1.1   thorpej  *
     43   1.1   thorpej  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
     44   1.1   thorpej  *
     45   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     46   1.1   thorpej  * modification, are permitted provided that the following conditions
     47   1.1   thorpej  * are met:
     48   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     49   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     50   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     51   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     52   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     53   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     54   1.1   thorpej  *    must display the following acknowledgement:
     55   1.1   thorpej  *	This product includes software developed by Zembu Labs, Inc.
     56   1.1   thorpej  * 4. Neither the name of Zembu Labs nor the names of its employees may
     57   1.1   thorpej  *    be used to endorse or promote products derived from this software
     58   1.1   thorpej  *    without specific prior written permission.
     59   1.1   thorpej  *
     60   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     61   1.1   thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     62   1.1   thorpej  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     63   1.1   thorpej  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     64   1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     65   1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     66   1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     67   1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     68   1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     69   1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     70   1.1   thorpej  */
     71   1.1   thorpej 
     72   1.1   thorpej /*
     73  1.18   xtraeme  * Environmental sensor framework for sysmon, exported to userland
     74  1.18   xtraeme  * with proplib(3).
     75   1.1   thorpej  */
     76   1.2     lukem 
     77   1.2     lukem #include <sys/cdefs.h>
     78  1.18   xtraeme __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.18 2007/07/01 07:36:45 xtraeme Exp $");
     79   1.1   thorpej 
     80   1.1   thorpej #include <sys/param.h>
     81  1.18   xtraeme #include <sys/types.h>
     82   1.1   thorpej #include <sys/conf.h>
     83   1.1   thorpej #include <sys/errno.h>
     84   1.1   thorpej #include <sys/kernel.h>
     85   1.1   thorpej #include <sys/systm.h>
     86   1.1   thorpej #include <sys/proc.h>
     87  1.18   xtraeme #include <sys/mutex.h>
     88  1.18   xtraeme #include <sys/kmem.h>
     89   1.1   thorpej 
     90   1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     91  1.18   xtraeme #include <dev/sysmon/sysmon_envsysvar.h>
     92  1.18   xtraeme #include <dev/sysmon/sysmon_taskq.h>
     93   1.1   thorpej 
     94  1.18   xtraeme #include "opt_compat_netbsd.h"
     95   1.1   thorpej 
     96  1.18   xtraeme struct sme_sensor_type {
     97  1.18   xtraeme 	int		type;
     98  1.18   xtraeme 	int		crittype;
     99  1.18   xtraeme 	const char	*desc;
    100  1.18   xtraeme };
    101  1.18   xtraeme 
    102  1.18   xtraeme static const struct sme_sensor_type sme_sensor_type[] = {
    103  1.18   xtraeme 	{ ENVSYS_STEMP,		PENVSYS_TYPE_TEMP,	"Temperature" },
    104  1.18   xtraeme 	{ ENVSYS_SFANRPM,	PENVSYS_TYPE_FAN,	"Fan" },
    105  1.18   xtraeme 	{ ENVSYS_SVOLTS_AC,	PENVSYS_TYPE_VOLTAGE,	"Voltage AC" },
    106  1.18   xtraeme 	{ ENVSYS_SVOLTS_DC,	PENVSYS_TYPE_VOLTAGE,	"Voltage DC" },
    107  1.18   xtraeme 	{ ENVSYS_SOHMS,		PENVSYS_TYPE_RESISTANCE,"Ohms" },
    108  1.18   xtraeme 	{ ENVSYS_SWATTS,	PENVSYS_TYPE_POWER,	"Watts" },
    109  1.18   xtraeme 	{ ENVSYS_SAMPS,		PENVSYS_TYPE_POWER,	"Ampere" },
    110  1.18   xtraeme 	{ ENVSYS_SWATTHOUR,	PENVSYS_TYPE_BATTERY,	"Watt hour" },
    111  1.18   xtraeme 	{ ENVSYS_SAMPHOUR,	PENVSYS_TYPE_BATTERY,	"Ampere hour" },
    112  1.18   xtraeme 	{ ENVSYS_INDICATOR,	-1,			"Indicator" },
    113  1.18   xtraeme 	{ ENVSYS_INTEGER,	-1,			"Integer" },
    114  1.18   xtraeme 	{ ENVSYS_DRIVE,		PENVSYS_TYPE_DRIVE,	"Drive" },
    115  1.18   xtraeme 	{ -1,			-1,			NULL }
    116  1.18   xtraeme };
    117  1.18   xtraeme 
    118  1.18   xtraeme struct sme_sensor_state {
    119  1.18   xtraeme 	int		type;
    120  1.18   xtraeme 	const char 	*desc;
    121  1.18   xtraeme };
    122  1.18   xtraeme 
    123  1.18   xtraeme static const struct sme_sensor_state sme_sensor_state[] = {
    124  1.18   xtraeme 	{ ENVSYS_SVALID,	"valid" },
    125  1.18   xtraeme 	{ ENVSYS_SINVALID,	"invalid" },
    126  1.18   xtraeme 	{ ENVSYS_SCRITICAL,	"critical" },
    127  1.18   xtraeme 	{ ENVSYS_SCRITUNDER,	"critical-under" },
    128  1.18   xtraeme 	{ ENVSYS_SCRITOVER,	"critical-over" },
    129  1.18   xtraeme 	{ ENVSYS_SWARNUNDER,	"warning-under" },
    130  1.18   xtraeme 	{ ENVSYS_SWARNOVER,	"warning-over" },
    131  1.18   xtraeme 	{ -1,			"unknown" }
    132  1.18   xtraeme };
    133  1.18   xtraeme 
    134  1.18   xtraeme static const struct sme_sensor_state sme_sensor_drive_state[] = {
    135  1.18   xtraeme 	{ ENVSYS_DRIVE_EMPTY,		"drive state is unknown" },
    136  1.18   xtraeme 	{ ENVSYS_DRIVE_READY,		"drive is ready" },
    137  1.18   xtraeme 	{ ENVSYS_DRIVE_POWERUP,		"drive is powering up" },
    138  1.18   xtraeme 	{ ENVSYS_DRIVE_ONLINE,		"drive is online" },
    139  1.18   xtraeme 	{ ENVSYS_DRIVE_IDLE,		"drive is idle" },
    140  1.18   xtraeme 	{ ENVSYS_DRIVE_ACTIVE,		"drive is active" },
    141  1.18   xtraeme 	{ ENVSYS_DRIVE_REBUILD,		"drive is rebuilding" },
    142  1.18   xtraeme 	{ ENVSYS_DRIVE_POWERDOWN,	"drive is powering down" },
    143  1.18   xtraeme 	{ ENVSYS_DRIVE_FAIL,		"drive failed" },
    144  1.18   xtraeme 	{ ENVSYS_DRIVE_PFAIL,		"drive degraded" },
    145  1.18   xtraeme 	{ -1,				"unknown" }
    146  1.18   xtraeme };
    147  1.18   xtraeme 
    148  1.18   xtraeme static prop_dictionary_t sme_propd;
    149  1.18   xtraeme static kmutex_t sme_list_mtx;
    150  1.18   xtraeme static kcondvar_t sme_list_cv;
    151  1.18   xtraeme 
    152  1.18   xtraeme #ifdef COMPAT_40
    153  1.18   xtraeme static u_int sysmon_envsys_next_sensor_index = 0;
    154  1.18   xtraeme static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    155  1.18   xtraeme #endif
    156   1.1   thorpej 
    157  1.18   xtraeme static void sysmon_envsys_release(struct sysmon_envsys *);
    158   1.1   thorpej 
    159  1.18   xtraeme kmutex_t sme_mtx, sme_event_mtx;
    160  1.16        ad 
    161  1.18   xtraeme /*
    162  1.18   xtraeme  * sysmon_envsys_init:
    163  1.18   xtraeme  *
    164  1.18   xtraeme  * 	+ Initialize global mutexes, dictionary and the linked lists.
    165  1.18   xtraeme  */
    166  1.18   xtraeme void
    167  1.18   xtraeme sysmon_envsys_init(void)
    168  1.18   xtraeme {
    169  1.18   xtraeme 	LIST_INIT(&sysmon_envsys_list);
    170  1.18   xtraeme 	LIST_INIT(&sme_events_list);
    171  1.18   xtraeme 	mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
    172  1.18   xtraeme 	mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
    173  1.18   xtraeme 	mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
    174  1.18   xtraeme 	mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
    175  1.18   xtraeme 	cv_init(&sme_list_cv, "smefind");
    176  1.18   xtraeme 	sme_propd = prop_dictionary_create();
    177  1.18   xtraeme }
    178   1.1   thorpej 
    179   1.1   thorpej /*
    180   1.1   thorpej  * sysmonopen_envsys:
    181   1.1   thorpej  *
    182  1.18   xtraeme  *	+ Open the system monitor device.
    183   1.1   thorpej  */
    184   1.1   thorpej int
    185  1.18   xtraeme sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    186   1.1   thorpej {
    187  1.18   xtraeme 	return 0;
    188   1.1   thorpej }
    189   1.1   thorpej 
    190   1.1   thorpej /*
    191   1.1   thorpej  * sysmonclose_envsys:
    192   1.1   thorpej  *
    193  1.18   xtraeme  *	+ Close the system monitor device.
    194   1.1   thorpej  */
    195   1.1   thorpej int
    196  1.18   xtraeme sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    197   1.1   thorpej {
    198   1.3  jdolecek 	/* Nothing to do */
    199  1.18   xtraeme 	return 0;
    200   1.1   thorpej }
    201   1.1   thorpej 
    202   1.1   thorpej /*
    203   1.1   thorpej  * sysmonioctl_envsys:
    204   1.1   thorpej  *
    205  1.18   xtraeme  *	+ Perform an envsys control request.
    206   1.1   thorpej  */
    207   1.1   thorpej int
    208  1.18   xtraeme sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    209   1.1   thorpej {
    210  1.18   xtraeme 	struct sysmon_envsys *sme = NULL;
    211   1.1   thorpej 	int error = 0;
    212  1.18   xtraeme #ifdef COMPAT_40
    213   1.1   thorpej 	u_int oidx;
    214  1.18   xtraeme #endif
    215   1.1   thorpej 
    216   1.1   thorpej 	switch (cmd) {
    217  1.18   xtraeme 	case ENVSYS_GETDICTIONARY:
    218  1.18   xtraeme 	    {
    219  1.18   xtraeme 		struct plistref *plist = (struct plistref *)data;
    220  1.18   xtraeme 
    221  1.18   xtraeme 		/*
    222  1.18   xtraeme 		 * Update all sysmon envsys devices dictionaries with
    223  1.18   xtraeme 		 * new data if it's different than we have currently
    224  1.18   xtraeme 		 * in the dictionary.
    225  1.18   xtraeme 		 */
    226  1.18   xtraeme 		mutex_enter(&sme_mtx);
    227  1.18   xtraeme 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    228  1.18   xtraeme 			if (sme == NULL)
    229  1.18   xtraeme 				continue;
    230  1.18   xtraeme 
    231  1.18   xtraeme 			error = sme_update_dictionary(sme);
    232  1.18   xtraeme 			if (error) {
    233  1.18   xtraeme 				DPRINTF(("%s: sme_update_dictionary, "
    234  1.18   xtraeme 				    "error=%d\n", __func__, error));
    235  1.18   xtraeme 				mutex_exit(&sme_mtx);
    236  1.18   xtraeme 				return error;
    237  1.18   xtraeme 			}
    238  1.18   xtraeme 		}
    239  1.18   xtraeme 		mutex_exit(&sme_mtx);
    240  1.18   xtraeme 		/*
    241  1.18   xtraeme 		 * Copy global dictionary to userland.
    242  1.18   xtraeme 		 */
    243  1.18   xtraeme 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    244   1.1   thorpej 		break;
    245  1.18   xtraeme 	    }
    246  1.18   xtraeme 	case ENVSYS_SETDICTIONARY:
    247   1.1   thorpej 	    {
    248  1.18   xtraeme 		const struct plistref *plist = (const struct plistref *)data;
    249  1.18   xtraeme 		prop_dictionary_t udict;
    250  1.18   xtraeme 		prop_object_t obj;
    251  1.18   xtraeme 		const char *devname = NULL;
    252  1.18   xtraeme 
    253  1.18   xtraeme 		/*
    254  1.18   xtraeme 		 * Get dictionary from userland.
    255  1.18   xtraeme 		 */
    256  1.18   xtraeme 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    257  1.18   xtraeme 		DPRINTF(("%s: copyin_ioctl error=%d\n", __func__, error));
    258  1.18   xtraeme 		if (error)
    259  1.18   xtraeme 			break;
    260   1.4  explorer 
    261  1.18   xtraeme 		/*
    262  1.18   xtraeme 		 * Parse "driver-name" key to obtain the driver we
    263  1.18   xtraeme 		 * are searching for.
    264  1.18   xtraeme 		 */
    265  1.18   xtraeme 		obj = prop_dictionary_get(udict, "driver-name");
    266  1.18   xtraeme 		if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
    267  1.18   xtraeme 			DPRINTF(("%s: driver-name failed\n", __func__));
    268  1.18   xtraeme 			prop_object_release(udict);
    269  1.18   xtraeme 			error = EINVAL;
    270  1.18   xtraeme 			break;
    271  1.18   xtraeme 		}
    272   1.4  explorer 
    273  1.18   xtraeme 		/* driver name */
    274  1.18   xtraeme 		devname = prop_string_cstring_nocopy(obj);
    275   1.4  explorer 
    276  1.18   xtraeme 		/* find the correct sme device */
    277  1.18   xtraeme 		sme = sysmon_envsys_find(devname);
    278  1.18   xtraeme 		if (sme == NULL) {
    279  1.18   xtraeme 			DPRINTF(("%s: NULL sme\n", __func__));
    280  1.18   xtraeme 			prop_object_release(udict);
    281  1.18   xtraeme 			error = EINVAL;
    282   1.4  explorer 			break;
    283   1.4  explorer 		}
    284   1.1   thorpej 
    285  1.18   xtraeme 		/*
    286  1.18   xtraeme 		 * Find the correct array object with the string supplied
    287  1.18   xtraeme 		 * by the userland dictionary.
    288  1.18   xtraeme 		 */
    289  1.18   xtraeme 		obj = prop_dictionary_get(sme_propd, devname);
    290  1.18   xtraeme 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    291  1.18   xtraeme 			DPRINTF(("%s: array device failed\n", __func__));
    292  1.18   xtraeme 			prop_object_release(udict);
    293  1.18   xtraeme 			error = EINVAL;
    294   1.1   thorpej 			break;
    295   1.1   thorpej 		}
    296  1.18   xtraeme 
    297  1.18   xtraeme 		/* do the real work now */
    298  1.18   xtraeme 		error = sme_userset_dictionary(sme, udict, obj);
    299   1.1   thorpej 		sysmon_envsys_release(sme);
    300   1.1   thorpej 		break;
    301   1.1   thorpej 	    }
    302  1.18   xtraeme 	    /*
    303  1.18   xtraeme 	     * Compatibility functions with the old interface, only
    304  1.18   xtraeme 	     * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
    305  1.18   xtraeme 	     * to make old applications work.
    306  1.18   xtraeme 	     */
    307  1.18   xtraeme #ifdef COMPAT_40
    308   1.1   thorpej 	case ENVSYS_GTREDATA:
    309   1.1   thorpej 	    {
    310  1.18   xtraeme 		struct envsys_tre_data *tred = (void *)data;
    311  1.18   xtraeme 		envsys_data_t *edata = NULL;
    312   1.1   thorpej 
    313   1.1   thorpej 		tred->validflags = 0;
    314   1.1   thorpej 
    315  1.18   xtraeme 		sme = sysmon_envsys_find_40(tred->sensor);
    316   1.1   thorpej 		if (sme == NULL)
    317   1.1   thorpej 			break;
    318  1.18   xtraeme 
    319  1.18   xtraeme 		mutex_enter(&sme_mtx);
    320   1.1   thorpej 		oidx = tred->sensor;
    321   1.1   thorpej 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    322  1.18   xtraeme 
    323  1.18   xtraeme 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    324  1.18   xtraeme 		    __func__, tred->sensor, oidx, sme->sme_name,
    325  1.18   xtraeme 		    sme->sme_nsensors));
    326  1.18   xtraeme 
    327  1.18   xtraeme 		edata = &sme->sme_sensor_data[tred->sensor];
    328  1.18   xtraeme 
    329  1.18   xtraeme 		if (tred->sensor < sme->sme_nsensors) {
    330  1.18   xtraeme 			if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    331  1.18   xtraeme 				error = (*sme->sme_gtredata)(sme, edata);
    332  1.18   xtraeme 				if (error) {
    333  1.18   xtraeme 					DPRINTF(("%s: sme_gtredata failed\n",
    334  1.18   xtraeme 				    	    __func__));
    335  1.18   xtraeme 					mutex_exit(&sme_mtx);
    336  1.18   xtraeme 					return error;
    337  1.18   xtraeme 				}
    338  1.18   xtraeme 			}
    339  1.18   xtraeme 
    340  1.18   xtraeme 			/* copy required values to the old interface */
    341  1.18   xtraeme 			tred->sensor = edata->sensor;
    342  1.18   xtraeme 			tred->cur.data_us = edata->value_cur;
    343  1.18   xtraeme 			tred->cur.data_s = edata->value_cur;
    344  1.18   xtraeme 			tred->max.data_us = edata->value_max;
    345  1.18   xtraeme 			tred->max.data_s = edata->value_max;
    346  1.18   xtraeme 			tred->min.data_us = edata->value_min;
    347  1.18   xtraeme 			tred->min.data_s = edata->value_min;
    348  1.18   xtraeme 			tred->avg.data_us = edata->value_avg;
    349  1.18   xtraeme 			tred->avg.data_s = edata->value_avg;
    350  1.18   xtraeme 			tred->units = edata->units;
    351  1.18   xtraeme 
    352  1.18   xtraeme 			/* mark invalid sensors */
    353  1.18   xtraeme 			if (edata->state == ENVSYS_SINVALID) {
    354  1.18   xtraeme 				tred->validflags &=
    355  1.18   xtraeme 				    ~(ENVSYS_FVALID|ENVSYS_FCURVALID);
    356  1.18   xtraeme 				tred->validflags &=
    357  1.18   xtraeme 				    ~(ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    358  1.18   xtraeme 				tred->cur.data_us = tred->cur.data_s = 0;
    359  1.18   xtraeme 			}
    360  1.18   xtraeme 
    361  1.18   xtraeme 			tred->validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    362  1.18   xtraeme 			if (edata->flags & ENVSYS_FPERCENT)
    363  1.18   xtraeme 				tred->validflags |=
    364  1.18   xtraeme 				    (ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    365  1.18   xtraeme 
    366  1.18   xtraeme 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    367  1.18   xtraeme 			    __func__, edata->desc, tred->cur.data_s));
    368  1.18   xtraeme 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    369  1.18   xtraeme 			    " tred->sensor=%d\n", __func__, tred->validflags,
    370  1.18   xtraeme 			    tred->units, tred->sensor));
    371   1.3  jdolecek 		}
    372   1.1   thorpej 		tred->sensor = oidx;
    373  1.18   xtraeme 		mutex_exit(&sme_mtx);
    374   1.1   thorpej 
    375   1.1   thorpej 		break;
    376   1.1   thorpej 	    }
    377   1.1   thorpej 	case ENVSYS_GTREINFO:
    378   1.1   thorpej 	    {
    379  1.18   xtraeme 		struct envsys_basic_info *binfo = (void *)data;
    380  1.18   xtraeme 		envsys_data_t *edata = NULL;
    381   1.1   thorpej 
    382   1.1   thorpej 		binfo->validflags = 0;
    383   1.1   thorpej 
    384  1.18   xtraeme 		sme = sysmon_envsys_find_40(binfo->sensor);
    385   1.1   thorpej 		if (sme == NULL)
    386   1.1   thorpej 			break;
    387  1.18   xtraeme 
    388  1.18   xtraeme 		mutex_enter(&sme_mtx);
    389   1.1   thorpej 		oidx = binfo->sensor;
    390   1.1   thorpej 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    391  1.18   xtraeme 
    392  1.18   xtraeme 		edata = &sme->sme_sensor_data[binfo->sensor];
    393  1.18   xtraeme 
    394  1.18   xtraeme 		/* skip invalid sensors */
    395  1.18   xtraeme 		if (edata->state == ENVSYS_SINVALID) {
    396  1.18   xtraeme 			binfo->validflags &= ~(ENVSYS_FVALID|ENVSYS_FCURVALID);
    397  1.18   xtraeme 			binfo->validflags &=
    398  1.18   xtraeme 			    ~(ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    399  1.18   xtraeme 		}
    400  1.18   xtraeme 
    401  1.18   xtraeme 		if (binfo->sensor < sme->sme_nsensors) {
    402  1.18   xtraeme 			binfo->validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    403  1.18   xtraeme 			if (edata->flags & ENVSYS_FPERCENT)
    404  1.18   xtraeme 				binfo->validflags |=
    405  1.18   xtraeme 				    (ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    406  1.18   xtraeme 			binfo->units = edata->units;
    407  1.18   xtraeme 			(void)strlcpy(binfo->desc, edata->desc,
    408  1.18   xtraeme 			    sizeof(binfo->desc));
    409  1.18   xtraeme 		}
    410  1.18   xtraeme 
    411  1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    412  1.18   xtraeme 		    __func__, binfo->units, binfo->validflags));
    413  1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    414  1.18   xtraeme 		    __func__, binfo->desc, binfo->sensor));
    415  1.18   xtraeme 
    416   1.1   thorpej 		binfo->sensor = oidx;
    417  1.18   xtraeme 		mutex_exit(&sme_mtx);
    418  1.18   xtraeme 
    419   1.1   thorpej 		break;
    420   1.1   thorpej 	    }
    421  1.18   xtraeme #endif /* COMPAT_40 */
    422   1.1   thorpej 	default:
    423   1.1   thorpej 		error = ENOTTY;
    424  1.18   xtraeme 		break;
    425   1.1   thorpej 	}
    426   1.1   thorpej 
    427  1.18   xtraeme 	return error;
    428   1.1   thorpej }
    429   1.1   thorpej 
    430   1.1   thorpej /*
    431   1.1   thorpej  * sysmon_envsys_register:
    432   1.1   thorpej  *
    433  1.18   xtraeme  *	+ Register an envsys device.
    434  1.18   xtraeme  *	+ Create device dictionary.
    435   1.1   thorpej  */
    436   1.1   thorpej int
    437   1.1   thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
    438   1.1   thorpej {
    439  1.18   xtraeme 	struct sysmon_envsys *lsme;
    440   1.1   thorpej 	int error = 0;
    441   1.1   thorpej 
    442  1.18   xtraeme 	/*
    443  1.18   xtraeme 	 * sanity check 1, make sure the driver has initialized
    444  1.18   xtraeme 	 * the sensors count or the value is not too high.
    445  1.18   xtraeme 	 */
    446  1.18   xtraeme 	if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
    447  1.18   xtraeme 		return EINVAL;
    448  1.18   xtraeme 
    449  1.18   xtraeme 	/*
    450  1.18   xtraeme 	 * sanity check 2, make sure that sme->sme_name and
    451  1.18   xtraeme 	 * sme->sme_data are not NULL.
    452  1.18   xtraeme 	 */
    453  1.18   xtraeme 	if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
    454  1.18   xtraeme 		return EINVAL;
    455   1.1   thorpej 
    456  1.18   xtraeme 	/*
    457  1.18   xtraeme 	 * sanity check 3, if SME_DISABLE_GTREDATA is not set
    458  1.18   xtraeme 	 * the sme_gtredata function callback must be non NULL.
    459  1.18   xtraeme 	 */
    460  1.18   xtraeme 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    461  1.18   xtraeme 		if (sme->sme_gtredata == NULL)
    462  1.18   xtraeme 			return EINVAL;
    463   1.1   thorpej 	}
    464   1.1   thorpej 
    465  1.18   xtraeme 	/*
    466  1.18   xtraeme 	 * - check if requested sysmon_envsys device is valid
    467  1.18   xtraeme 	 *   and does not exist already in the list.
    468  1.18   xtraeme 	 * - add device into the list.
    469  1.18   xtraeme 	 * - create the plist structure.
    470  1.18   xtraeme 	 */
    471  1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    472  1.18   xtraeme 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    473  1.18   xtraeme 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    474  1.18   xtraeme 		       mutex_exit(&sme_list_mtx);
    475  1.18   xtraeme 		       error = EEXIST;
    476  1.18   xtraeme 		       goto out;
    477  1.18   xtraeme 	       }
    478  1.18   xtraeme 	}
    479  1.18   xtraeme #ifdef COMPAT_40
    480   1.1   thorpej 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    481   1.1   thorpej 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    482  1.18   xtraeme #endif
    483  1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    484  1.18   xtraeme 	error = sysmon_envsys_createplist(sme);
    485  1.18   xtraeme 	if (!error) {
    486  1.18   xtraeme 		mutex_enter(&sme_list_mtx);
    487  1.18   xtraeme 		LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    488  1.18   xtraeme 		mutex_exit(&sme_list_mtx);
    489  1.18   xtraeme 	}
    490   1.1   thorpej 
    491  1.18   xtraeme out:
    492  1.18   xtraeme 	return error;
    493   1.1   thorpej }
    494   1.1   thorpej 
    495   1.1   thorpej /*
    496   1.1   thorpej  * sysmon_envsys_unregister:
    497   1.1   thorpej  *
    498  1.18   xtraeme  *	+ Unregister an envsys device.
    499  1.18   xtraeme  *	+ Unregister all events associated with this device.
    500  1.18   xtraeme  *	+ Remove device dictionary.
    501   1.1   thorpej  */
    502   1.1   thorpej void
    503   1.1   thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
    504   1.1   thorpej {
    505  1.18   xtraeme 	sme_event_t *see;
    506   1.1   thorpej 
    507  1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    508   1.7      yamt 	while (sme->sme_flags & SME_FLAG_BUSY) {
    509   1.8      yamt 		sme->sme_flags |= SME_FLAG_WANTED;
    510  1.18   xtraeme 		cv_wait(&sme_list_cv, &sme_list_mtx);
    511  1.18   xtraeme 	}
    512  1.18   xtraeme 	prop_dictionary_remove(sme_propd, sme->sme_name);
    513  1.18   xtraeme 	LIST_FOREACH(see, &sme_events_list, see_list) {
    514  1.18   xtraeme 		if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
    515  1.18   xtraeme 			sme_event_unregister(see->pes.pes_sensname,
    516  1.18   xtraeme 			    see->type);
    517   1.7      yamt 	}
    518   1.1   thorpej 	LIST_REMOVE(sme, sme_list);
    519  1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    520   1.1   thorpej }
    521   1.1   thorpej 
    522   1.1   thorpej /*
    523   1.1   thorpej  * sysmon_envsys_find:
    524   1.1   thorpej  *
    525  1.18   xtraeme  *	+ Find an envsys device.
    526   1.1   thorpej  */
    527   1.1   thorpej struct sysmon_envsys *
    528  1.18   xtraeme sysmon_envsys_find(const char *name)
    529   1.1   thorpej {
    530   1.1   thorpej 	struct sysmon_envsys *sme;
    531   1.1   thorpej 
    532  1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    533   1.7      yamt again:
    534   1.1   thorpej 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    535   1.1   thorpej 	     sme = LIST_NEXT(sme, sme_list)) {
    536  1.18   xtraeme 			if (strcmp(sme->sme_name, name) == 0) {
    537  1.18   xtraeme 				if (sme->sme_flags & SME_FLAG_BUSY) {
    538  1.18   xtraeme 					sme->sme_flags |= SME_FLAG_WANTED;
    539  1.18   xtraeme 					cv_wait(&sme_list_cv, &sme_list_mtx);
    540  1.18   xtraeme 					goto again;
    541  1.18   xtraeme 				}
    542  1.18   xtraeme 				sme->sme_flags |= SME_FLAG_BUSY;
    543  1.18   xtraeme 				break;
    544  1.18   xtraeme 			}
    545  1.18   xtraeme 	}
    546  1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    547  1.18   xtraeme 	return sme;
    548  1.18   xtraeme }
    549  1.18   xtraeme 
    550  1.18   xtraeme /*
    551  1.18   xtraeme  * sysmon_envsys_release:
    552  1.18   xtraeme  *
    553  1.18   xtraeme  * 	+ Release an envsys device.
    554  1.18   xtraeme  */
    555  1.18   xtraeme void
    556  1.18   xtraeme sysmon_envsys_release(struct sysmon_envsys *sme)
    557  1.18   xtraeme {
    558  1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    559  1.18   xtraeme 	if (sme->sme_flags & SME_FLAG_WANTED)
    560  1.18   xtraeme 		cv_broadcast(&sme_list_cv);
    561  1.18   xtraeme 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    562  1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    563  1.18   xtraeme }
    564  1.18   xtraeme 
    565  1.18   xtraeme /* compatibility function */
    566  1.18   xtraeme #ifdef COMPAT_40
    567  1.18   xtraeme struct sysmon_envsys *
    568  1.18   xtraeme sysmon_envsys_find_40(u_int idx)
    569  1.18   xtraeme {
    570  1.18   xtraeme 	struct sysmon_envsys *sme;
    571  1.18   xtraeme 
    572  1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    573  1.18   xtraeme 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    574  1.18   xtraeme 	     sme = LIST_NEXT(sme, sme_list)) {
    575   1.1   thorpej 		if (idx >= sme->sme_fsensor &&
    576  1.18   xtraeme 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    577  1.18   xtraeme 			break;
    578  1.18   xtraeme 	}
    579  1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    580  1.18   xtraeme 	return sme;
    581  1.18   xtraeme }
    582  1.18   xtraeme #endif
    583  1.18   xtraeme 
    584  1.18   xtraeme /*
    585  1.18   xtraeme  * sysmon_envsys_createplist:
    586  1.18   xtraeme  *
    587  1.18   xtraeme  * 	+ Create the property list structure for a device.
    588  1.18   xtraeme  */
    589  1.18   xtraeme int
    590  1.18   xtraeme sysmon_envsys_createplist(struct sysmon_envsys *sme)
    591  1.18   xtraeme {
    592  1.18   xtraeme 	envsys_data_t *edata;
    593  1.18   xtraeme 	prop_array_t array;
    594  1.18   xtraeme 	int i, error = 0;
    595  1.18   xtraeme 
    596  1.18   xtraeme 	/* create the sysmon envsys device array. */
    597  1.18   xtraeme 	array = prop_array_create();
    598  1.18   xtraeme 
    599  1.18   xtraeme 	/*
    600  1.18   xtraeme 	 * <dict>
    601  1.18   xtraeme 	 * 	<key>foo0</key>
    602  1.18   xtraeme 	 * 	<array>
    603  1.18   xtraeme 	 * 		...
    604  1.18   xtraeme 	 */
    605  1.18   xtraeme 	mutex_enter(&sme_mtx);
    606  1.18   xtraeme 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    607  1.18   xtraeme 		DPRINTF(("%s: prop_dictionary_set\n", __func__));
    608  1.18   xtraeme 		mutex_exit(&sme_mtx);
    609  1.18   xtraeme 		error = EINVAL;
    610  1.18   xtraeme 		goto out;
    611  1.18   xtraeme 	}
    612  1.18   xtraeme 	mutex_exit(&sme_mtx);
    613  1.18   xtraeme 
    614  1.18   xtraeme 	/*
    615  1.18   xtraeme 	 * Iterate over all sensors and create a dictionary with all
    616  1.18   xtraeme 	 * values specified by the sysmon envsys driver.
    617  1.18   xtraeme 	 */
    618  1.18   xtraeme 	for (i = 0; i < sme->sme_nsensors; i++) {
    619  1.18   xtraeme 		edata = &sme->sme_sensor_data[i];
    620  1.18   xtraeme 		/*
    621  1.18   xtraeme 		 * refresh sensor data via sme_gtredata only if the
    622  1.18   xtraeme 		 * flag is not set.
    623  1.18   xtraeme 		 */
    624  1.18   xtraeme 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    625  1.18   xtraeme 			mutex_enter(&sme_mtx);
    626  1.18   xtraeme 			error = (*sme->sme_gtredata)(sme, edata);
    627  1.18   xtraeme 			if (error) {
    628  1.18   xtraeme 				DPRINTF(("%s: sme->sme_gtredata[%d]\n",
    629  1.18   xtraeme 				    __func__, i));
    630  1.18   xtraeme 				mutex_exit(&sme_mtx);
    631  1.18   xtraeme 				continue;
    632   1.7      yamt 			}
    633  1.18   xtraeme 			mutex_exit(&sme_mtx);
    634  1.18   xtraeme 		}
    635  1.18   xtraeme 
    636  1.18   xtraeme 		error = sme_make_dictionary(sme, array, edata);
    637  1.18   xtraeme 		if (error) {
    638  1.18   xtraeme 			DPRINTF(("%s: sme_make_dictionary[%d]\n", __func__, i));
    639  1.18   xtraeme 			goto out;
    640  1.18   xtraeme 		}
    641  1.18   xtraeme 	}
    642  1.18   xtraeme 
    643  1.18   xtraeme out:
    644  1.18   xtraeme 	prop_object_release(array);
    645  1.18   xtraeme 	return error;
    646  1.18   xtraeme }
    647  1.18   xtraeme 
    648  1.18   xtraeme /*
    649  1.18   xtraeme  * sme_make_dictionary:
    650  1.18   xtraeme  *
    651  1.18   xtraeme  * 	+ Create sensor's dictionary in device's dictionary array.
    652  1.18   xtraeme  */
    653  1.18   xtraeme int
    654  1.18   xtraeme sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    655  1.18   xtraeme 		    envsys_data_t *edata)
    656  1.18   xtraeme {
    657  1.18   xtraeme 	const struct sme_sensor_type *est = sme_sensor_type;
    658  1.18   xtraeme 	const struct sme_sensor_state *ess = sme_sensor_state;
    659  1.18   xtraeme 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    660  1.18   xtraeme 	sme_event_drv_t *sme_evdrv_t = NULL;
    661  1.18   xtraeme 	prop_dictionary_t dict;
    662  1.18   xtraeme 	int i, j, k;
    663  1.18   xtraeme 
    664  1.18   xtraeme 	i = j = k = 0;
    665  1.18   xtraeme 
    666  1.18   xtraeme 	/*
    667  1.18   xtraeme 	 * <array>
    668  1.18   xtraeme 	 * 	<dict>
    669  1.18   xtraeme 	 * 		...
    670  1.18   xtraeme 	 */
    671  1.18   xtraeme 	dict = prop_dictionary_create();
    672  1.18   xtraeme 
    673  1.18   xtraeme 	mutex_enter(&sme_mtx);
    674  1.18   xtraeme 	if (!prop_array_add(array, dict)) {
    675  1.18   xtraeme 		mutex_exit(&sme_mtx);
    676  1.18   xtraeme 		DPRINTF(("%s: prop_array_add\n", __func__));
    677  1.18   xtraeme 		return EINVAL;
    678  1.18   xtraeme 	}
    679  1.18   xtraeme 
    680  1.18   xtraeme 	/* find the correct unit for this sensor. */
    681  1.18   xtraeme 	for (i = 0; est[i].type != -1; i++)
    682  1.18   xtraeme 		if (est[i].type == edata->units)
    683  1.18   xtraeme 			break;
    684  1.18   xtraeme 
    685  1.18   xtraeme 	/*
    686  1.18   xtraeme 	 * 		...
    687  1.18   xtraeme 	 * 		<key>type</key>
    688  1.18   xtraeme 	 * 		<string>foo</string>
    689  1.18   xtraeme 	 * 		<key>description</key>
    690  1.18   xtraeme 	 * 		<string>blah blah</string>
    691  1.18   xtraeme 	 * 		...
    692  1.18   xtraeme 	 */
    693  1.18   xtraeme 	SENSOR_SSTRING(dict, "type", est[i].desc);
    694  1.18   xtraeme 	SENSOR_SSTRING(dict, "description", edata->desc);
    695  1.18   xtraeme 
    696  1.18   xtraeme 	/*
    697  1.18   xtraeme 	 * Add sensor's state description.
    698  1.18   xtraeme 	 *
    699  1.18   xtraeme 	 * 		...
    700  1.18   xtraeme 	 * 		<key>state</key>
    701  1.18   xtraeme 	 * 		<string>valid</string>
    702  1.18   xtraeme 	 * 		...
    703  1.18   xtraeme 	 */
    704  1.18   xtraeme 	for (j = 0; ess[j].type != -1; j++)
    705  1.18   xtraeme 		if (ess[j].type == edata->state)
    706   1.7      yamt 			break;
    707  1.18   xtraeme 
    708  1.18   xtraeme 	SENSOR_SSTRING(dict, "state", ess[j].desc);
    709  1.18   xtraeme 
    710  1.18   xtraeme 	/*
    711  1.18   xtraeme 	 * add the percentage boolean object:
    712  1.18   xtraeme 	 *
    713  1.18   xtraeme 	 * 		...
    714  1.18   xtraeme 	 * 		<key>want-percentage</key>
    715  1.18   xtraeme 	 * 		<true/>
    716  1.18   xtraeme 	 * 		...
    717  1.18   xtraeme 	 */
    718  1.18   xtraeme 	if (edata->flags & ENVSYS_FPERCENT)
    719  1.18   xtraeme 		SENSOR_SBOOL(dict, "want-percentage", true);
    720  1.18   xtraeme 
    721  1.18   xtraeme 	/*
    722  1.18   xtraeme 	 * Add the monitoring boolean object:
    723  1.18   xtraeme 	 *
    724  1.18   xtraeme 	 * 		...
    725  1.18   xtraeme 	 * 		<key>monitoring-supported</key>
    726  1.18   xtraeme 	 * 		<true/>
    727  1.18   xtraeme 	 *		...
    728  1.18   xtraeme 	 *
    729  1.18   xtraeme 	 * always false on Drive, Integer and Indicator types, they
    730  1.18   xtraeme 	 * cannot be monitored.
    731  1.18   xtraeme 	 *
    732  1.18   xtraeme 	 */
    733  1.18   xtraeme 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    734  1.18   xtraeme 	    (edata->units == ENVSYS_INDICATOR) ||
    735  1.18   xtraeme 	    (edata->units == ENVSYS_INTEGER) ||
    736  1.18   xtraeme 	    (edata->units == ENVSYS_DRIVE)) {
    737  1.18   xtraeme 		SENSOR_SBOOL(dict, "monitoring-supported", false);
    738  1.18   xtraeme 	} else {
    739  1.18   xtraeme 		SENSOR_SBOOL(dict, "monitoring-supported", true);
    740  1.18   xtraeme 	}
    741  1.18   xtraeme 
    742  1.18   xtraeme 	/*
    743  1.18   xtraeme 	 * Add the drive-state object for drive sensors:
    744  1.18   xtraeme 	 *
    745  1.18   xtraeme 	 * 		...
    746  1.18   xtraeme 	 * 		<key>drive-state</key>
    747  1.18   xtraeme 	 * 		<string>drive is online</string>
    748  1.18   xtraeme 	 * 		...
    749  1.18   xtraeme 	 */
    750  1.18   xtraeme 	if (edata->units == ENVSYS_DRIVE) {
    751  1.18   xtraeme 		for (k = 0; esds[k].type != -1; k++)
    752  1.18   xtraeme 			if (esds[k].type == edata->value_cur)
    753  1.18   xtraeme 				break;
    754  1.18   xtraeme 		SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
    755  1.18   xtraeme 	}
    756  1.18   xtraeme 
    757  1.18   xtraeme 	mutex_exit(&sme_mtx);
    758  1.18   xtraeme 	/*
    759  1.18   xtraeme 	 * Add a new event if a monitoring flag was set.
    760  1.18   xtraeme 	 */
    761  1.18   xtraeme 	if (edata->monitor) {
    762  1.18   xtraeme 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
    763  1.18   xtraeme 
    764  1.18   xtraeme 		sme_evdrv_t->sdict = dict;
    765  1.18   xtraeme 		sme_evdrv_t->edata = edata;
    766  1.18   xtraeme 		sme_evdrv_t->sme = sme;
    767  1.18   xtraeme 		sme_evdrv_t->powertype = est[i].crittype;
    768  1.18   xtraeme 
    769  1.18   xtraeme 		sysmon_task_queue_init();
    770  1.18   xtraeme 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
    771  1.18   xtraeme 	}
    772  1.18   xtraeme 
    773  1.18   xtraeme 	mutex_enter(&sme_mtx);
    774  1.18   xtraeme 	/* if sensor is enabled, add the following properties... */
    775  1.18   xtraeme 	if (edata->state == ENVSYS_SVALID) {
    776  1.18   xtraeme 		/*
    777  1.18   xtraeme 		 * 	...
    778  1.18   xtraeme 		 * 	<key>rpms</key>
    779  1.18   xtraeme 		 * 	<integer>2500</integer>
    780  1.18   xtraeme 		 * 	<key>rfact</key>
    781  1.18   xtraeme 		 * 	<integer>10000</integer>
    782  1.18   xtraeme 		 * 	<key>cur-value</key>
    783  1.18   xtraeme 		 * 	<integer>1250</integer>
    784  1.18   xtraeme 		 * 	<key>min-value</key>
    785  1.18   xtraeme 		 * 	<integer>800</integer>
    786  1.18   xtraeme 		 * 	<key>max-value</integer>
    787  1.18   xtraeme 		 * 	<integer>3000</integer>
    788  1.18   xtraeme 		 * 	<key>avg-value</integer>
    789  1.18   xtraeme 		 * 	<integer>1400</integer>
    790  1.18   xtraeme 		 * </dict>
    791  1.18   xtraeme 		 */
    792  1.18   xtraeme 		if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
    793  1.18   xtraeme 			SENSOR_SUINT32(dict, "rpms", edata->rpms);
    794  1.18   xtraeme 
    795  1.18   xtraeme 		if ((edata->units == ENVSYS_SVOLTS_AC ||
    796  1.18   xtraeme 		    edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
    797  1.18   xtraeme 			SENSOR_SINT32(dict, "rfact", edata->rfact);
    798  1.18   xtraeme 
    799  1.18   xtraeme 		if (edata->value_cur)
    800  1.18   xtraeme 			SENSOR_SINT32(dict, "cur-value", edata->value_cur);
    801  1.18   xtraeme 
    802  1.18   xtraeme 		if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
    803  1.18   xtraeme 			SENSOR_SINT32(dict, "min-value", edata->value_min);
    804  1.18   xtraeme 
    805  1.18   xtraeme 		if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
    806  1.18   xtraeme 			SENSOR_SINT32(dict, "max-value", edata->value_max);
    807  1.18   xtraeme 
    808  1.18   xtraeme 		if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
    809  1.18   xtraeme 			SENSOR_SINT32(dict, "avg-value", edata->value_avg);
    810  1.18   xtraeme 	}
    811  1.18   xtraeme 
    812  1.18   xtraeme 	/*
    813  1.18   xtraeme 	 * 	...
    814  1.18   xtraeme 	 * </array>
    815  1.18   xtraeme 	 */
    816  1.18   xtraeme out:
    817  1.18   xtraeme 	mutex_exit(&sme_mtx);
    818  1.18   xtraeme 	prop_object_release(dict);
    819  1.18   xtraeme 	return 0;
    820  1.18   xtraeme }
    821  1.18   xtraeme 
    822  1.18   xtraeme /*
    823  1.18   xtraeme  * sme_update_dictionary:
    824  1.18   xtraeme  *
    825  1.18   xtraeme  * 	+ Update per-sensor dictionaries with new values if there were
    826  1.18   xtraeme  * 	  changes, otherwise the object in dictionary is untouched.
    827  1.18   xtraeme  * 	+ Send a critical event if any sensor is in a critical condition.
    828  1.18   xtraeme  */
    829  1.18   xtraeme int
    830  1.18   xtraeme sme_update_dictionary(struct sysmon_envsys *sme)
    831  1.18   xtraeme {
    832  1.18   xtraeme 	const struct sme_sensor_state *ess = sme_sensor_state;
    833  1.18   xtraeme 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    834  1.18   xtraeme 	envsys_data_t *edata = NULL;
    835  1.18   xtraeme 	prop_object_t array, obj, dict = NULL;
    836  1.18   xtraeme 	int i, j, error = 0;
    837  1.18   xtraeme 
    838  1.18   xtraeme 	/* retrieve the array of dictionaries in device. */
    839  1.18   xtraeme 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    840  1.18   xtraeme 	if (prop_object_type(array) != PROP_TYPE_ARRAY)
    841  1.18   xtraeme 		return EINVAL;
    842  1.18   xtraeme 
    843  1.18   xtraeme 	/*
    844  1.18   xtraeme 	 * - iterate over all sensors.
    845  1.18   xtraeme 	 * - fetch new data.
    846  1.18   xtraeme 	 * - check if data in dictionary is different than new data.
    847  1.18   xtraeme 	 * - update dictionary if there were changes.
    848  1.18   xtraeme 	 */
    849  1.18   xtraeme 	for (i = 0; i < sme->sme_nsensors; i++) {
    850  1.18   xtraeme 		edata = &sme->sme_sensor_data[i];
    851  1.18   xtraeme 
    852  1.18   xtraeme 		/*
    853  1.18   xtraeme 		 * refresh sensor data via sme_gtredata only if the
    854  1.18   xtraeme 		 * flag is not set.
    855  1.18   xtraeme 		 */
    856  1.18   xtraeme 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    857  1.18   xtraeme 			error = (*sme->sme_gtredata)(sme, edata);
    858  1.18   xtraeme 			if (error) {
    859  1.18   xtraeme 				DPRINTF(("%s: gtredata[%d] failed\n",
    860  1.18   xtraeme 				    __func__, i));
    861  1.18   xtraeme 				return error;
    862  1.18   xtraeme 			}
    863  1.18   xtraeme 		}
    864  1.18   xtraeme 
    865  1.18   xtraeme 		/* retrieve sensor's dictionary. */
    866  1.18   xtraeme 		dict = prop_array_get(array, i);
    867  1.18   xtraeme 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY)
    868  1.18   xtraeme 			return EINVAL;
    869  1.18   xtraeme 
    870  1.18   xtraeme 		/* update state sensor. */
    871  1.18   xtraeme 		for (j = 0; ess[j].type != -1; j++)
    872  1.18   xtraeme 			if (ess[j].type == edata->state)
    873  1.18   xtraeme 				break;
    874  1.18   xtraeme 
    875  1.18   xtraeme 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
    876  1.18   xtraeme 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
    877  1.18   xtraeme 		    ess[j].type, edata->flags, edata->units, edata->sensor));
    878  1.18   xtraeme 
    879  1.18   xtraeme 		/* update sensor state */
    880  1.18   xtraeme 		SENSOR_UPSTRING(dict, "state", ess[j].desc);
    881  1.18   xtraeme 
    882  1.18   xtraeme 		/* update sensor current value */
    883  1.18   xtraeme 		SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
    884  1.18   xtraeme 
    885  1.18   xtraeme 		/*
    886  1.18   xtraeme 		 * Integer and Indicator types do not the following
    887  1.18   xtraeme 		 * values, so skip them.
    888  1.18   xtraeme 		 */
    889  1.18   xtraeme 		if (edata->units == ENVSYS_INTEGER ||
    890  1.18   xtraeme 		    edata->units == ENVSYS_INDICATOR)
    891  1.18   xtraeme 			continue;
    892  1.18   xtraeme 
    893  1.18   xtraeme 		/* update sensor flags */
    894  1.18   xtraeme 		if (edata->flags & ENVSYS_FPERCENT)
    895  1.18   xtraeme 			SENSOR_SBOOL(dict, "want-percentage", true);
    896  1.18   xtraeme 		else {
    897  1.18   xtraeme 			obj = prop_dictionary_get(dict, "want-percentage");
    898  1.18   xtraeme 			if (obj)
    899  1.18   xtraeme 				SENSOR_SBOOL(dict, "want-percentage", false);
    900  1.18   xtraeme 		}
    901  1.18   xtraeme 
    902  1.18   xtraeme 		if (edata->flags & ENVSYS_FVALID_MAX)
    903  1.18   xtraeme 			SENSOR_UPINT32(dict, "max-value", edata->value_max);
    904  1.18   xtraeme 
    905  1.18   xtraeme 		if (edata->flags & ENVSYS_FVALID_MIN)
    906  1.18   xtraeme 			SENSOR_UPINT32(dict, "min-value", edata->value_min);
    907  1.18   xtraeme 
    908  1.18   xtraeme 		if (edata->flags & ENVSYS_FVALID_AVG)
    909  1.18   xtraeme 			SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
    910  1.18   xtraeme 
    911  1.18   xtraeme 		/* update 'rpms' only in ENVSYS_SFANRPM. */
    912  1.18   xtraeme 		if (edata->units == ENVSYS_SFANRPM)
    913  1.18   xtraeme 			SENSOR_UPUINT32(dict, "rpms", edata->rpms);
    914  1.18   xtraeme 
    915  1.18   xtraeme 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
    916  1.18   xtraeme 		if (edata->units == ENVSYS_SVOLTS_AC ||
    917  1.18   xtraeme 		    edata->units == ENVSYS_SVOLTS_DC) {
    918  1.18   xtraeme 			SENSOR_UPINT32(dict, "rfact", edata->rfact);
    919  1.18   xtraeme 		}
    920  1.18   xtraeme 
    921  1.18   xtraeme 		/* update 'drive-state' only in ENVSYS_DRIVE. */
    922  1.18   xtraeme 		if (edata->units == ENVSYS_DRIVE) {
    923  1.18   xtraeme 			for (j = 0; esds[j].type != -1; j++)
    924  1.18   xtraeme 				if (esds[j].type == edata->value_cur)
    925  1.18   xtraeme 					break;
    926  1.18   xtraeme 
    927  1.18   xtraeme 			SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
    928   1.7      yamt 		}
    929   1.1   thorpej 	}
    930   1.1   thorpej 
    931  1.18   xtraeme out:
    932  1.18   xtraeme 	return error;
    933   1.1   thorpej }
    934   1.1   thorpej 
    935   1.1   thorpej /*
    936  1.18   xtraeme  * sme_userset_dictionary:
    937   1.1   thorpej  *
    938  1.18   xtraeme  * 	+ Parses the userland dictionary and run the appropiate
    939  1.18   xtraeme  * 	  tasks that were requested.
    940   1.1   thorpej  */
    941  1.18   xtraeme int
    942  1.18   xtraeme sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
    943  1.18   xtraeme 		       prop_array_t array)
    944   1.1   thorpej {
    945  1.18   xtraeme 	const struct sme_sensor_type *sst = sme_sensor_type;
    946  1.18   xtraeme 	envsys_data_t *edata;
    947  1.18   xtraeme 	prop_dictionary_t dict;
    948  1.18   xtraeme 	prop_object_t obj, obj1, obj2;
    949  1.18   xtraeme 	int32_t critval;
    950  1.18   xtraeme 	int i, error = 0;
    951  1.18   xtraeme 	const char *blah, *sname;
    952  1.18   xtraeme 	bool targetfound = false;
    953  1.18   xtraeme 
    954  1.18   xtraeme 	blah = sname = NULL;
    955  1.18   xtraeme 
    956  1.18   xtraeme 	/* get sensor's name from userland dictionary. */
    957  1.18   xtraeme 	obj = prop_dictionary_get(udict, "sensor-name");
    958  1.18   xtraeme 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
    959  1.18   xtraeme 		DPRINTF(("%s: sensor-name failed\n", __func__));
    960  1.18   xtraeme 		return EINVAL;
    961  1.18   xtraeme 	}
    962   1.1   thorpej 
    963  1.18   xtraeme 	/* iterate over the sensors to find the right one */
    964  1.18   xtraeme 	for (i = 0; i < sme->sme_nsensors; i++) {
    965  1.18   xtraeme 		edata = &sme->sme_sensor_data[i];
    966  1.18   xtraeme 		dict = prop_array_get(array, i);
    967  1.18   xtraeme 		obj1 = prop_dictionary_get(dict, "description");
    968  1.18   xtraeme 
    969  1.18   xtraeme 		/* is it our sensor? */
    970  1.18   xtraeme 		if (!prop_string_equals(obj1, obj))
    971  1.18   xtraeme 			continue;
    972  1.18   xtraeme 
    973  1.18   xtraeme 		/*
    974  1.18   xtraeme 		 * Check if a new description operation was
    975  1.18   xtraeme 		 * requested by the user and set new description.
    976  1.18   xtraeme 		 */
    977  1.18   xtraeme 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
    978  1.18   xtraeme 			targetfound = true;
    979  1.18   xtraeme 			blah = prop_string_cstring_nocopy(obj2);
    980  1.18   xtraeme 			SENSOR_UPSTRING(dict, "description", blah);
    981  1.18   xtraeme 			break;
    982  1.18   xtraeme 		}
    983  1.18   xtraeme 
    984  1.18   xtraeme 		/* did the user want to remove a critical capacity limit? */
    985  1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
    986  1.18   xtraeme 		if (obj2 != NULL) {
    987  1.18   xtraeme 			targetfound = true;
    988  1.18   xtraeme 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    989  1.18   xtraeme 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
    990  1.18   xtraeme 				    error = ENOTSUP;
    991  1.18   xtraeme 				    break;
    992  1.18   xtraeme 			}
    993  1.18   xtraeme 
    994  1.18   xtraeme 			sname = prop_string_cstring_nocopy(obj);
    995  1.18   xtraeme 			error = sme_event_unregister(sname,
    996  1.18   xtraeme 			    PENVSYS_EVENT_BATT_USERCAP);
    997  1.18   xtraeme 			if (error)
    998  1.18   xtraeme 				break;
    999  1.18   xtraeme 
   1000  1.18   xtraeme 			prop_dictionary_remove(dict, "critical-capacity");
   1001  1.18   xtraeme 			break;
   1002  1.18   xtraeme 		}
   1003  1.18   xtraeme 
   1004  1.18   xtraeme 		/* did the user want to remove a critical min limit? */
   1005  1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
   1006  1.18   xtraeme 		if (obj2 != NULL) {
   1007  1.18   xtraeme 			targetfound = true;
   1008  1.18   xtraeme 			sname = prop_string_cstring_nocopy(obj);
   1009  1.18   xtraeme 			error = sme_event_unregister(sname,
   1010  1.18   xtraeme 			    PENVSYS_EVENT_USER_CRITMIN);
   1011  1.18   xtraeme 			if (error)
   1012  1.18   xtraeme 				break;
   1013  1.18   xtraeme 
   1014  1.18   xtraeme 			prop_dictionary_remove(dict, "critical-min-limit");
   1015  1.18   xtraeme 			break;
   1016  1.18   xtraeme 		}
   1017  1.18   xtraeme 
   1018  1.18   xtraeme 		/* did the user want to remove a critical max limit? */
   1019  1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1020  1.18   xtraeme 		if (obj2 != NULL) {
   1021  1.18   xtraeme 			targetfound = true;
   1022  1.18   xtraeme 			sname = prop_string_cstring_nocopy(obj);
   1023  1.18   xtraeme 			error = sme_event_unregister(sname,
   1024  1.18   xtraeme 			    PENVSYS_EVENT_USER_CRITMAX);
   1025  1.18   xtraeme 			if (error)
   1026  1.18   xtraeme 				break;
   1027   1.7      yamt 
   1028  1.18   xtraeme 			prop_dictionary_remove(dict, "critical-max-limit");
   1029  1.18   xtraeme 			break;
   1030  1.18   xtraeme 		}
   1031  1.18   xtraeme 
   1032  1.18   xtraeme 		/* did the user want to change rfact? */
   1033  1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1034  1.18   xtraeme 		if (obj2 != NULL) {
   1035  1.18   xtraeme 			targetfound = true;
   1036  1.18   xtraeme 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1037  1.18   xtraeme 				edata->rfact = prop_number_integer_value(obj2);
   1038  1.18   xtraeme 			else
   1039  1.18   xtraeme 				error = ENOTSUP;
   1040  1.18   xtraeme 
   1041  1.18   xtraeme 			break;
   1042  1.18   xtraeme 		}
   1043  1.18   xtraeme 
   1044  1.18   xtraeme 		for (i = 0; sst[i].type != -1; i++)
   1045  1.18   xtraeme 			if (sst[i].type == edata->units)
   1046  1.18   xtraeme 				break;
   1047  1.18   xtraeme 
   1048  1.18   xtraeme 		/* did the user want to set a critical capacity event? */
   1049  1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1050  1.18   xtraeme 		if (obj2 != NULL) {
   1051  1.18   xtraeme 			targetfound = true;
   1052  1.18   xtraeme 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1053  1.18   xtraeme 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1054  1.18   xtraeme 				error = ENOTSUP;
   1055  1.18   xtraeme 				break;
   1056  1.18   xtraeme 			}
   1057  1.18   xtraeme 
   1058  1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1059  1.18   xtraeme 			error = sme_event_add(dict,
   1060  1.18   xtraeme 					      edata,
   1061  1.18   xtraeme 					      sme->sme_name,
   1062  1.18   xtraeme 					      "critical-capacity",
   1063  1.18   xtraeme 					      critval,
   1064  1.18   xtraeme 					      PENVSYS_EVENT_BATT_USERCAP,
   1065  1.18   xtraeme 					      sst[i].crittype);
   1066  1.18   xtraeme 			break;
   1067  1.18   xtraeme 		}
   1068  1.18   xtraeme 
   1069  1.18   xtraeme 		/* did the user want to set a critical max event? */
   1070  1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1071  1.18   xtraeme 		if (obj2 != NULL) {
   1072  1.18   xtraeme 			targetfound = true;
   1073  1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1074  1.18   xtraeme 			    edata->units == ENVSYS_INTEGER ||
   1075  1.18   xtraeme 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1076  1.18   xtraeme 				error = ENOTSUP;
   1077  1.18   xtraeme 				break;
   1078  1.18   xtraeme 			}
   1079  1.18   xtraeme 
   1080  1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1081  1.18   xtraeme 			error = sme_event_add(dict,
   1082  1.18   xtraeme 					      edata,
   1083  1.18   xtraeme 					      sme->sme_name,
   1084  1.18   xtraeme 					      "critical-max-limit",
   1085  1.18   xtraeme 					      critval,
   1086  1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMAX,
   1087  1.18   xtraeme 					      sst[i].crittype);
   1088  1.18   xtraeme 			break;
   1089  1.18   xtraeme 		}
   1090  1.18   xtraeme 
   1091  1.18   xtraeme 		/* did the user want to set a critical min event? */
   1092  1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1093  1.18   xtraeme 		if (obj2 != NULL) {
   1094  1.18   xtraeme 			targetfound = true;
   1095  1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1096  1.18   xtraeme 			    edata->units == ENVSYS_INTEGER ||
   1097  1.18   xtraeme 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1098  1.18   xtraeme 				error = ENOTSUP;
   1099  1.18   xtraeme 				break;
   1100  1.18   xtraeme 			}
   1101  1.18   xtraeme 
   1102  1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1103  1.18   xtraeme 			error = sme_event_add(dict,
   1104  1.18   xtraeme 					      edata,
   1105  1.18   xtraeme 					      sme->sme_name,
   1106  1.18   xtraeme 					      "critical-min-limit",
   1107  1.18   xtraeme 					      critval,
   1108  1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMIN,
   1109  1.18   xtraeme 					      sst[i].crittype);
   1110  1.18   xtraeme 			break;
   1111  1.18   xtraeme 		}
   1112  1.18   xtraeme 	}
   1113  1.18   xtraeme 
   1114  1.18   xtraeme 	/* invalid target? return the error */
   1115  1.18   xtraeme 	if (!targetfound)
   1116  1.18   xtraeme 		error = EINVAL;
   1117  1.18   xtraeme 
   1118  1.18   xtraeme out:
   1119  1.18   xtraeme 	return error;
   1120   1.1   thorpej }
   1121