Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.24.2.2
      1  1.24.2.2     skrll /*	$NetBSD: sysmon_envsys.c,v 1.24.2.2 2007/09/03 10:22:00 skrll 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.24.2.2     skrll __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.24.2.2 2007/09/03 10:22:00 skrll 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.22   xtraeme #include <sys/fcntl.h>
     85       1.1   thorpej #include <sys/kernel.h>
     86       1.1   thorpej #include <sys/systm.h>
     87       1.1   thorpej #include <sys/proc.h>
     88      1.18   xtraeme #include <sys/mutex.h>
     89      1.18   xtraeme #include <sys/kmem.h>
     90       1.1   thorpej 
     91       1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     92      1.18   xtraeme #include <dev/sysmon/sysmon_envsysvar.h>
     93      1.18   xtraeme #include <dev/sysmon/sysmon_taskq.h>
     94       1.1   thorpej 
     95      1.18   xtraeme #include "opt_compat_netbsd.h"
     96       1.1   thorpej 
     97      1.18   xtraeme struct sme_sensor_type {
     98      1.18   xtraeme 	int		type;
     99      1.18   xtraeme 	int		crittype;
    100      1.18   xtraeme 	const char	*desc;
    101      1.18   xtraeme };
    102      1.18   xtraeme 
    103      1.18   xtraeme static const struct sme_sensor_type sme_sensor_type[] = {
    104      1.18   xtraeme 	{ ENVSYS_STEMP,		PENVSYS_TYPE_TEMP,	"Temperature" },
    105      1.18   xtraeme 	{ ENVSYS_SFANRPM,	PENVSYS_TYPE_FAN,	"Fan" },
    106      1.18   xtraeme 	{ ENVSYS_SVOLTS_AC,	PENVSYS_TYPE_VOLTAGE,	"Voltage AC" },
    107      1.18   xtraeme 	{ ENVSYS_SVOLTS_DC,	PENVSYS_TYPE_VOLTAGE,	"Voltage DC" },
    108      1.18   xtraeme 	{ ENVSYS_SOHMS,		PENVSYS_TYPE_RESISTANCE,"Ohms" },
    109      1.18   xtraeme 	{ ENVSYS_SWATTS,	PENVSYS_TYPE_POWER,	"Watts" },
    110      1.18   xtraeme 	{ ENVSYS_SAMPS,		PENVSYS_TYPE_POWER,	"Ampere" },
    111      1.18   xtraeme 	{ ENVSYS_SWATTHOUR,	PENVSYS_TYPE_BATTERY,	"Watt hour" },
    112      1.18   xtraeme 	{ ENVSYS_SAMPHOUR,	PENVSYS_TYPE_BATTERY,	"Ampere hour" },
    113      1.19   xtraeme 	{ ENVSYS_INDICATOR,	PENVSYS_TYPE_INDICATOR,	"Indicator" },
    114      1.21   xtraeme 	{ ENVSYS_INTEGER,	PENVSYS_TYPE_INDICATOR,	"Integer" },
    115      1.18   xtraeme 	{ ENVSYS_DRIVE,		PENVSYS_TYPE_DRIVE,	"Drive" },
    116  1.24.2.2     skrll 	{ ENVSYS_GSTRING,	-1, 			"Generic string" },
    117  1.24.2.1     skrll 	{ -1,			-1,			"unknown" }
    118      1.18   xtraeme };
    119      1.18   xtraeme 
    120  1.24.2.2     skrll static const struct sme_sensor_type sme_sensor_state[] = {
    121  1.24.2.2     skrll 	{ ENVSYS_SVALID,	-1, 	"valid" },
    122  1.24.2.2     skrll 	{ ENVSYS_SINVALID,	-1, 	"invalid" },
    123  1.24.2.2     skrll 	{ ENVSYS_SCRITICAL,	-1, 	"critical" },
    124  1.24.2.2     skrll 	{ ENVSYS_SCRITUNDER,	-1, 	"critical-under" },
    125  1.24.2.2     skrll 	{ ENVSYS_SCRITOVER,	-1, 	"critical-over" },
    126  1.24.2.2     skrll 	{ ENVSYS_SWARNUNDER,	-1, 	"warning-under" },
    127  1.24.2.2     skrll 	{ ENVSYS_SWARNOVER,	-1, 	"warning-over" },
    128  1.24.2.2     skrll 	{ -1,			-1, 	"unknown" }
    129      1.18   xtraeme };
    130      1.18   xtraeme 
    131  1.24.2.2     skrll static const struct sme_sensor_type sme_sensor_drive_state[] = {
    132  1.24.2.2     skrll 	{ ENVSYS_DRIVE_EMPTY,		-1, 	"drive state is unknown" },
    133  1.24.2.2     skrll 	{ ENVSYS_DRIVE_READY,		-1, 	"drive is ready" },
    134  1.24.2.2     skrll 	{ ENVSYS_DRIVE_POWERUP,		-1, 	"drive is powering up" },
    135  1.24.2.2     skrll 	{ ENVSYS_DRIVE_ONLINE,		-1, 	"drive is online" },
    136  1.24.2.2     skrll 	{ ENVSYS_DRIVE_IDLE,		-1, 	"drive is idle" },
    137  1.24.2.2     skrll 	{ ENVSYS_DRIVE_ACTIVE,		-1, 	"drive is active" },
    138  1.24.2.2     skrll 	{ ENVSYS_DRIVE_REBUILD,		-1, 	"drive is rebuilding" },
    139  1.24.2.2     skrll 	{ ENVSYS_DRIVE_POWERDOWN,	-1, 	"drive is powering down" },
    140  1.24.2.2     skrll 	{ ENVSYS_DRIVE_FAIL,		-1, 	"drive failed" },
    141  1.24.2.2     skrll 	{ ENVSYS_DRIVE_PFAIL,		-1, 	"drive degraded" },
    142  1.24.2.2     skrll 	{ -1,				-1, 	"unknown" }
    143      1.18   xtraeme };
    144      1.18   xtraeme 
    145      1.18   xtraeme static prop_dictionary_t sme_propd;
    146      1.18   xtraeme static kcondvar_t sme_list_cv;
    147  1.24.2.1     skrll 
    148  1.24.2.2     skrll kmutex_t sme_list_mtx, sme_event_mtx, sme_event_init_mtx;
    149  1.24.2.1     skrll kcondvar_t sme_event_cv;
    150      1.18   xtraeme 
    151      1.18   xtraeme #ifdef COMPAT_40
    152      1.18   xtraeme static u_int sysmon_envsys_next_sensor_index = 0;
    153      1.18   xtraeme static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    154      1.18   xtraeme #endif
    155       1.1   thorpej 
    156      1.18   xtraeme static void sysmon_envsys_release(struct sysmon_envsys *);
    157  1.24.2.2     skrll static void sysmon_envsys_destroy_plist(prop_array_t);
    158  1.24.2.2     skrll static int sme_register_sensorname(struct sysmon_envsys *, envsys_data_t *);
    159      1.16        ad 
    160      1.18   xtraeme /*
    161      1.18   xtraeme  * sysmon_envsys_init:
    162      1.18   xtraeme  *
    163      1.18   xtraeme  * 	+ Initialize global mutexes, dictionary and the linked lists.
    164      1.18   xtraeme  */
    165      1.18   xtraeme void
    166      1.18   xtraeme sysmon_envsys_init(void)
    167      1.18   xtraeme {
    168      1.18   xtraeme 	LIST_INIT(&sysmon_envsys_list);
    169      1.18   xtraeme 	LIST_INIT(&sme_events_list);
    170      1.18   xtraeme 	mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
    171      1.18   xtraeme 	mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
    172      1.18   xtraeme 	mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
    173      1.18   xtraeme 	cv_init(&sme_list_cv, "smefind");
    174  1.24.2.1     skrll 	cv_init(&sme_event_cv, "smeevent");
    175      1.18   xtraeme 	sme_propd = prop_dictionary_create();
    176      1.18   xtraeme }
    177       1.1   thorpej 
    178       1.1   thorpej /*
    179       1.1   thorpej  * sysmonopen_envsys:
    180       1.1   thorpej  *
    181      1.18   xtraeme  *	+ Open the system monitor device.
    182       1.1   thorpej  */
    183       1.1   thorpej int
    184      1.18   xtraeme sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    185       1.1   thorpej {
    186      1.18   xtraeme 	return 0;
    187       1.1   thorpej }
    188       1.1   thorpej 
    189       1.1   thorpej /*
    190       1.1   thorpej  * sysmonclose_envsys:
    191       1.1   thorpej  *
    192      1.18   xtraeme  *	+ Close the system monitor device.
    193       1.1   thorpej  */
    194       1.1   thorpej int
    195      1.18   xtraeme sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    196       1.1   thorpej {
    197       1.3  jdolecek 	/* Nothing to do */
    198      1.18   xtraeme 	return 0;
    199       1.1   thorpej }
    200       1.1   thorpej 
    201       1.1   thorpej /*
    202       1.1   thorpej  * sysmonioctl_envsys:
    203       1.1   thorpej  *
    204  1.24.2.2     skrll  *	+ Perform a sysmon envsys control request.
    205       1.1   thorpej  */
    206       1.1   thorpej int
    207      1.18   xtraeme sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    208       1.1   thorpej {
    209      1.18   xtraeme 	struct sysmon_envsys *sme = NULL;
    210       1.1   thorpej 	int error = 0;
    211      1.18   xtraeme #ifdef COMPAT_40
    212       1.1   thorpej 	u_int oidx;
    213      1.18   xtraeme #endif
    214       1.1   thorpej 
    215       1.1   thorpej 	switch (cmd) {
    216      1.18   xtraeme 	case ENVSYS_GETDICTIONARY:
    217      1.18   xtraeme 	    {
    218      1.18   xtraeme 		struct plistref *plist = (struct plistref *)data;
    219      1.18   xtraeme 
    220      1.18   xtraeme 		/*
    221      1.18   xtraeme 		 * Update all sysmon envsys devices dictionaries with
    222      1.18   xtraeme 		 * new data if it's different than we have currently
    223      1.18   xtraeme 		 * in the dictionary.
    224      1.18   xtraeme 		 */
    225  1.24.2.1     skrll 		mutex_enter(&sme_list_mtx);
    226      1.18   xtraeme 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    227  1.24.2.2     skrll 			sme->sme_flags |= SME_FLAG_BUSY;
    228      1.18   xtraeme 			error = sme_update_dictionary(sme);
    229      1.18   xtraeme 			if (error) {
    230      1.18   xtraeme 				DPRINTF(("%s: sme_update_dictionary, "
    231      1.18   xtraeme 				    "error=%d\n", __func__, error));
    232  1.24.2.2     skrll 				sme->sme_flags &= ~SME_FLAG_BUSY;
    233  1.24.2.1     skrll 				mutex_exit(&sme_list_mtx);
    234      1.18   xtraeme 				return error;
    235      1.18   xtraeme 			}
    236  1.24.2.2     skrll 			sme->sme_flags &= ~SME_FLAG_BUSY;
    237      1.18   xtraeme 		}
    238  1.24.2.1     skrll 		mutex_exit(&sme_list_mtx);
    239      1.18   xtraeme 		/*
    240      1.18   xtraeme 		 * Copy global dictionary to userland.
    241      1.18   xtraeme 		 */
    242      1.18   xtraeme 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    243       1.1   thorpej 		break;
    244      1.18   xtraeme 	    }
    245      1.18   xtraeme 	case ENVSYS_SETDICTIONARY:
    246       1.1   thorpej 	    {
    247      1.18   xtraeme 		const struct plistref *plist = (const struct plistref *)data;
    248      1.18   xtraeme 		prop_dictionary_t udict;
    249      1.18   xtraeme 		prop_object_t obj;
    250      1.18   xtraeme 		const char *devname = NULL;
    251      1.18   xtraeme 
    252      1.22   xtraeme 		if ((flag & FWRITE) == 0)
    253      1.22   xtraeme 			return EPERM;
    254      1.22   xtraeme 
    255      1.18   xtraeme 		/*
    256      1.18   xtraeme 		 * Get dictionary from userland.
    257      1.18   xtraeme 		 */
    258      1.18   xtraeme 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    259  1.24.2.1     skrll 		if (error) {
    260  1.24.2.1     skrll 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    261  1.24.2.1     skrll 			    __func__, error));
    262      1.18   xtraeme 			break;
    263  1.24.2.1     skrll 		}
    264       1.4  explorer 
    265      1.18   xtraeme 		/*
    266      1.18   xtraeme 		 * Parse "driver-name" key to obtain the driver we
    267      1.18   xtraeme 		 * are searching for.
    268      1.18   xtraeme 		 */
    269      1.18   xtraeme 		obj = prop_dictionary_get(udict, "driver-name");
    270      1.18   xtraeme 		if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
    271      1.18   xtraeme 			DPRINTF(("%s: driver-name failed\n", __func__));
    272      1.18   xtraeme 			prop_object_release(udict);
    273      1.18   xtraeme 			error = EINVAL;
    274      1.18   xtraeme 			break;
    275      1.18   xtraeme 		}
    276       1.4  explorer 
    277      1.18   xtraeme 		/* driver name */
    278      1.18   xtraeme 		devname = prop_string_cstring_nocopy(obj);
    279       1.4  explorer 
    280      1.18   xtraeme 		/* find the correct sme device */
    281      1.18   xtraeme 		sme = sysmon_envsys_find(devname);
    282      1.18   xtraeme 		if (sme == NULL) {
    283      1.18   xtraeme 			DPRINTF(("%s: NULL sme\n", __func__));
    284      1.18   xtraeme 			prop_object_release(udict);
    285      1.18   xtraeme 			error = EINVAL;
    286       1.4  explorer 			break;
    287       1.4  explorer 		}
    288       1.1   thorpej 
    289      1.18   xtraeme 		/*
    290      1.18   xtraeme 		 * Find the correct array object with the string supplied
    291      1.18   xtraeme 		 * by the userland dictionary.
    292      1.18   xtraeme 		 */
    293      1.18   xtraeme 		obj = prop_dictionary_get(sme_propd, devname);
    294      1.18   xtraeme 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    295      1.18   xtraeme 			DPRINTF(("%s: array device failed\n", __func__));
    296  1.24.2.1     skrll 			sysmon_envsys_release(sme);
    297      1.18   xtraeme 			prop_object_release(udict);
    298      1.18   xtraeme 			error = EINVAL;
    299       1.1   thorpej 			break;
    300       1.1   thorpej 		}
    301      1.18   xtraeme 
    302      1.18   xtraeme 		/* do the real work now */
    303      1.18   xtraeme 		error = sme_userset_dictionary(sme, udict, obj);
    304       1.1   thorpej 		sysmon_envsys_release(sme);
    305  1.24.2.2     skrll 		prop_object_release(udict);
    306       1.1   thorpej 		break;
    307       1.1   thorpej 	    }
    308      1.18   xtraeme 	    /*
    309      1.18   xtraeme 	     * Compatibility functions with the old interface, only
    310      1.18   xtraeme 	     * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
    311      1.18   xtraeme 	     * to make old applications work.
    312      1.18   xtraeme 	     */
    313      1.18   xtraeme #ifdef COMPAT_40
    314       1.1   thorpej 	case ENVSYS_GTREDATA:
    315       1.1   thorpej 	    {
    316      1.18   xtraeme 		struct envsys_tre_data *tred = (void *)data;
    317      1.18   xtraeme 		envsys_data_t *edata = NULL;
    318       1.1   thorpej 
    319       1.1   thorpej 		tred->validflags = 0;
    320       1.1   thorpej 
    321      1.18   xtraeme 		sme = sysmon_envsys_find_40(tred->sensor);
    322       1.1   thorpej 		if (sme == NULL)
    323       1.1   thorpej 			break;
    324      1.18   xtraeme 
    325  1.24.2.2     skrll 		mutex_enter(&sme_list_mtx);
    326       1.1   thorpej 		oidx = tred->sensor;
    327       1.1   thorpej 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    328      1.18   xtraeme 
    329      1.18   xtraeme 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    330      1.18   xtraeme 		    __func__, tred->sensor, oidx, sme->sme_name,
    331      1.18   xtraeme 		    sme->sme_nsensors));
    332      1.18   xtraeme 
    333      1.18   xtraeme 		edata = &sme->sme_sensor_data[tred->sensor];
    334      1.18   xtraeme 
    335      1.18   xtraeme 		if (tred->sensor < sme->sme_nsensors) {
    336      1.18   xtraeme 			if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    337      1.18   xtraeme 				error = (*sme->sme_gtredata)(sme, edata);
    338      1.18   xtraeme 				if (error) {
    339      1.18   xtraeme 					DPRINTF(("%s: sme_gtredata failed\n",
    340      1.18   xtraeme 				    	    __func__));
    341  1.24.2.2     skrll 					mutex_exit(&sme_list_mtx);
    342      1.18   xtraeme 					return error;
    343      1.18   xtraeme 				}
    344      1.18   xtraeme 			}
    345      1.18   xtraeme 
    346      1.18   xtraeme 			/* copy required values to the old interface */
    347      1.18   xtraeme 			tred->sensor = edata->sensor;
    348      1.18   xtraeme 			tred->cur.data_us = edata->value_cur;
    349      1.18   xtraeme 			tred->cur.data_s = edata->value_cur;
    350      1.18   xtraeme 			tred->max.data_us = edata->value_max;
    351      1.18   xtraeme 			tred->max.data_s = edata->value_max;
    352      1.18   xtraeme 			tred->min.data_us = edata->value_min;
    353      1.18   xtraeme 			tred->min.data_s = edata->value_min;
    354      1.18   xtraeme 			tred->avg.data_us = edata->value_avg;
    355      1.18   xtraeme 			tred->avg.data_s = edata->value_avg;
    356      1.18   xtraeme 			tred->units = edata->units;
    357      1.18   xtraeme 
    358      1.20   xtraeme 			tred->validflags |= ENVSYS_FVALID;
    359      1.20   xtraeme 			tred->validflags |= ENVSYS_FCURVALID;
    360      1.20   xtraeme 
    361      1.20   xtraeme 			if (edata->flags & ENVSYS_FPERCENT) {
    362      1.20   xtraeme 				tred->validflags |= ENVSYS_FMAXVALID;
    363      1.20   xtraeme 				tred->validflags |= ENVSYS_FFRACVALID;
    364      1.20   xtraeme 			}
    365      1.20   xtraeme 
    366  1.24.2.1     skrll 			if (edata->state == ENVSYS_SINVALID ||
    367  1.24.2.1     skrll 			    edata->flags & ENVSYS_FNOTVALID) {
    368      1.20   xtraeme 				tred->validflags &= ~ENVSYS_FCURVALID;
    369      1.18   xtraeme 				tred->cur.data_us = tred->cur.data_s = 0;
    370      1.18   xtraeme 			}
    371      1.18   xtraeme 
    372      1.18   xtraeme 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    373      1.18   xtraeme 			    __func__, edata->desc, tred->cur.data_s));
    374      1.18   xtraeme 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    375      1.18   xtraeme 			    " tred->sensor=%d\n", __func__, tred->validflags,
    376      1.18   xtraeme 			    tred->units, tred->sensor));
    377       1.3  jdolecek 		}
    378       1.1   thorpej 		tred->sensor = oidx;
    379  1.24.2.2     skrll 		mutex_exit(&sme_list_mtx);
    380       1.1   thorpej 
    381       1.1   thorpej 		break;
    382       1.1   thorpej 	    }
    383       1.1   thorpej 	case ENVSYS_GTREINFO:
    384       1.1   thorpej 	    {
    385      1.18   xtraeme 		struct envsys_basic_info *binfo = (void *)data;
    386      1.18   xtraeme 		envsys_data_t *edata = NULL;
    387       1.1   thorpej 
    388       1.1   thorpej 		binfo->validflags = 0;
    389       1.1   thorpej 
    390      1.18   xtraeme 		sme = sysmon_envsys_find_40(binfo->sensor);
    391       1.1   thorpej 		if (sme == NULL)
    392       1.1   thorpej 			break;
    393      1.18   xtraeme 
    394  1.24.2.2     skrll 		mutex_enter(&sme_list_mtx);
    395       1.1   thorpej 		oidx = binfo->sensor;
    396       1.1   thorpej 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    397      1.18   xtraeme 
    398      1.18   xtraeme 		edata = &sme->sme_sensor_data[binfo->sensor];
    399      1.18   xtraeme 
    400      1.20   xtraeme 		binfo->validflags |= ENVSYS_FVALID;
    401      1.18   xtraeme 
    402      1.18   xtraeme 		if (binfo->sensor < sme->sme_nsensors) {
    403      1.18   xtraeme 			binfo->units = edata->units;
    404      1.18   xtraeme 			(void)strlcpy(binfo->desc, edata->desc,
    405      1.18   xtraeme 			    sizeof(binfo->desc));
    406      1.18   xtraeme 		}
    407      1.18   xtraeme 
    408      1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    409      1.18   xtraeme 		    __func__, binfo->units, binfo->validflags));
    410      1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    411      1.18   xtraeme 		    __func__, binfo->desc, binfo->sensor));
    412      1.18   xtraeme 
    413       1.1   thorpej 		binfo->sensor = oidx;
    414  1.24.2.2     skrll 		mutex_exit(&sme_list_mtx);
    415      1.18   xtraeme 
    416       1.1   thorpej 		break;
    417       1.1   thorpej 	    }
    418      1.18   xtraeme #endif /* COMPAT_40 */
    419       1.1   thorpej 	default:
    420       1.1   thorpej 		error = ENOTTY;
    421      1.18   xtraeme 		break;
    422       1.1   thorpej 	}
    423       1.1   thorpej 
    424      1.18   xtraeme 	return error;
    425       1.1   thorpej }
    426       1.1   thorpej 
    427       1.1   thorpej /*
    428       1.1   thorpej  * sysmon_envsys_register:
    429       1.1   thorpej  *
    430  1.24.2.2     skrll  *	+ Register a sysmon envsys device.
    431  1.24.2.2     skrll  *	+ Create array of dictionaries for a device.
    432       1.1   thorpej  */
    433       1.1   thorpej int
    434       1.1   thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
    435       1.1   thorpej {
    436      1.18   xtraeme 	struct sysmon_envsys *lsme;
    437  1.24.2.2     skrll 	prop_dictionary_t dict;
    438  1.24.2.2     skrll 	prop_array_t array;
    439  1.24.2.2     skrll 	envsys_data_t *edata = NULL;
    440  1.24.2.2     skrll 	int i, error = 0;
    441       1.1   thorpej 
    442  1.24.2.2     skrll 	KASSERT(sme != NULL);
    443  1.24.2.2     skrll 	KASSERT(sme->sme_name != NULL);
    444  1.24.2.2     skrll 	KASSERT(sme->sme_sensor_data != NULL);
    445      1.18   xtraeme 
    446  1.24.2.2     skrll 	/*
    447  1.24.2.2     skrll 	 * sme_nsensors is mandatory...
    448      1.18   xtraeme 	 */
    449  1.24.2.2     skrll 	if (!sme->sme_nsensors)
    450      1.18   xtraeme 		return EINVAL;
    451  1.24.2.2     skrll 
    452      1.18   xtraeme 	/*
    453  1.24.2.2     skrll 	 * sanity check: if SME_DISABLE_GTREDATA is not set,
    454      1.18   xtraeme 	 * the sme_gtredata function callback must be non NULL.
    455      1.18   xtraeme 	 */
    456      1.18   xtraeme 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    457      1.18   xtraeme 		if (sme->sme_gtredata == NULL)
    458      1.18   xtraeme 			return EINVAL;
    459       1.1   thorpej 	}
    460       1.1   thorpej 
    461  1.24.2.2     skrll 	/* create the sysmon envsys device array. */
    462  1.24.2.2     skrll 	array = prop_array_create();
    463  1.24.2.2     skrll 	if (array == NULL)
    464  1.24.2.2     skrll 		return ENOMEM;
    465  1.24.2.2     skrll 
    466  1.24.2.2     skrll 	/*
    467  1.24.2.2     skrll 	 * Initialize the singly linked list for sensor descriptions.
    468  1.24.2.2     skrll 	 */
    469  1.24.2.2     skrll 	SLIST_INIT(&sme->sme_names_list);
    470  1.24.2.2     skrll 	/*
    471  1.24.2.2     skrll 	 * Iterate over all sensors and create a dictionary per sensor,
    472  1.24.2.2     skrll 	 * checking firstly if sensor description is unique.
    473  1.24.2.2     skrll 	 */
    474  1.24.2.2     skrll 	for (i = 0; i < sme->sme_nsensors; i++) {
    475  1.24.2.2     skrll 		/*
    476  1.24.2.2     skrll 		 * XXX:
    477  1.24.2.2     skrll 		 *
    478  1.24.2.2     skrll 		 * workaround for LKMs. First sensor used in a LKM
    479  1.24.2.2     skrll 		 * gets the index sensor from all edata structs
    480  1.24.2.2     skrll 		 * allocated in kernel. It is unknown to me why this
    481  1.24.2.2     skrll 		 * value is not 0 when using a LKM.
    482  1.24.2.2     skrll 		 *
    483  1.24.2.2     skrll 		 * For now overwrite its index to 0.
    484  1.24.2.2     skrll 		 */
    485  1.24.2.2     skrll 		if (i == 0)
    486  1.24.2.2     skrll 			sme->sme_sensor_data[0].sensor = 0;
    487  1.24.2.2     skrll 
    488  1.24.2.2     skrll 		edata = &sme->sme_sensor_data[i];
    489  1.24.2.2     skrll 		/*
    490  1.24.2.2     skrll 		 * Check if sensor description is unique.
    491  1.24.2.2     skrll 		 */
    492  1.24.2.2     skrll 		if (sme_register_sensorname(sme, edata))
    493  1.24.2.2     skrll 			continue;
    494  1.24.2.2     skrll 
    495  1.24.2.2     skrll 		dict = prop_dictionary_create();
    496  1.24.2.2     skrll 		if (dict == NULL) {
    497  1.24.2.2     skrll 			error = ENOMEM;
    498  1.24.2.2     skrll 			goto out2;
    499  1.24.2.2     skrll 		}
    500  1.24.2.2     skrll 
    501  1.24.2.2     skrll 		/*
    502  1.24.2.2     skrll 		 * Create all objects in sensor's dictionary.
    503  1.24.2.2     skrll 		 */
    504  1.24.2.2     skrll 		sme_add_sensor_dictionary(sme, array, dict, edata);
    505  1.24.2.2     skrll 	}
    506  1.24.2.2     skrll 
    507      1.18   xtraeme 	/*
    508  1.24.2.2     skrll 	 * Check if requested sysmon_envsys device is valid
    509  1.24.2.2     skrll 	 * and does not exist already in the list.
    510      1.18   xtraeme 	 */
    511      1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    512  1.24.2.2     skrll 	sme->sme_flags |= SME_FLAG_BUSY;
    513      1.18   xtraeme 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    514      1.18   xtraeme 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    515      1.18   xtraeme 		       error = EEXIST;
    516      1.18   xtraeme 		       goto out;
    517      1.18   xtraeme 	       }
    518      1.18   xtraeme 	}
    519  1.24.2.2     skrll 	/*
    520  1.24.2.2     skrll 	 * If the array does not contain any object (sensor), there's
    521  1.24.2.2     skrll 	 * no need to attach the driver.
    522  1.24.2.2     skrll 	 */
    523  1.24.2.2     skrll 	if (prop_array_count(array) == 0) {
    524  1.24.2.2     skrll 		error = EINVAL;
    525  1.24.2.2     skrll 		DPRINTF(("%s: empty array for '%s'\n", __func__,
    526  1.24.2.2     skrll 		    sme->sme_name));
    527  1.24.2.2     skrll 		goto out;
    528  1.24.2.2     skrll 	}
    529  1.24.2.2     skrll 	/*
    530  1.24.2.2     skrll 	 * Add the array into the global dictionary for the driver.
    531  1.24.2.2     skrll 	 *
    532  1.24.2.2     skrll 	 * <dict>
    533  1.24.2.2     skrll 	 * 	<key>foo0</key>
    534  1.24.2.2     skrll 	 * 	<array>
    535  1.24.2.2     skrll 	 * 		...
    536  1.24.2.2     skrll 	 */
    537  1.24.2.2     skrll 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    538  1.24.2.2     skrll 		error = EINVAL;
    539  1.24.2.2     skrll 		DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
    540  1.24.2.2     skrll 		    sme->sme_name));
    541  1.24.2.2     skrll 		goto out;
    542  1.24.2.2     skrll 	}
    543  1.24.2.2     skrll 	/*
    544  1.24.2.2     skrll 	 * Add the device into the list.
    545  1.24.2.2     skrll 	 */
    546      1.18   xtraeme #ifdef COMPAT_40
    547       1.1   thorpej 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    548       1.1   thorpej 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    549      1.18   xtraeme #endif
    550  1.24.2.2     skrll 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    551       1.1   thorpej 
    552      1.18   xtraeme out:
    553  1.24.2.2     skrll 	sme->sme_flags &= ~SME_FLAG_BUSY;
    554  1.24.2.2     skrll 	sme->sme_uniqsensors = 0;
    555  1.24.2.1     skrll 	mutex_exit(&sme_list_mtx);
    556  1.24.2.2     skrll 	if (error == 0)
    557  1.24.2.2     skrll 		return 0;
    558  1.24.2.2     skrll out2:
    559  1.24.2.2     skrll 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    560  1.24.2.2     skrll 	    sme->sme_name, error));
    561  1.24.2.2     skrll 	sysmon_envsys_destroy_plist(array);
    562  1.24.2.2     skrll 	prop_object_release(array);
    563      1.18   xtraeme 	return error;
    564       1.1   thorpej }
    565       1.1   thorpej 
    566       1.1   thorpej /*
    567  1.24.2.2     skrll  * sysmon_envsys_destroy_plist:
    568  1.24.2.2     skrll  *
    569  1.24.2.2     skrll  * 	+ Remove all objects from the array of dictionaries that is
    570  1.24.2.2     skrll  * 	  created in a sysmon envsys device.
    571  1.24.2.2     skrll  */
    572  1.24.2.2     skrll static void
    573  1.24.2.2     skrll sysmon_envsys_destroy_plist(prop_array_t array)
    574  1.24.2.2     skrll {
    575  1.24.2.2     skrll 	prop_dictionary_t dict;
    576  1.24.2.2     skrll 	prop_object_iterator_t iter, iter2;
    577  1.24.2.2     skrll 	prop_object_t obj;
    578  1.24.2.2     skrll 
    579  1.24.2.2     skrll 	KASSERT(array != NULL);
    580  1.24.2.2     skrll 
    581  1.24.2.2     skrll 	iter = prop_array_iterator(array);
    582  1.24.2.2     skrll 	if (iter == NULL)
    583  1.24.2.2     skrll 		return;
    584  1.24.2.2     skrll 
    585  1.24.2.2     skrll 	while ((dict = prop_object_iterator_next(iter)) != NULL) {
    586  1.24.2.2     skrll 		iter2 = prop_dictionary_iterator(dict);
    587  1.24.2.2     skrll 		if (iter2) {
    588  1.24.2.2     skrll 			while ((obj = prop_object_iterator_next(iter2)) != NULL)
    589  1.24.2.2     skrll 				prop_object_release(obj);
    590  1.24.2.2     skrll 
    591  1.24.2.2     skrll 			prop_object_iterator_release(iter2);
    592  1.24.2.2     skrll 		}
    593  1.24.2.2     skrll 		prop_object_release(dict);
    594  1.24.2.2     skrll 	}
    595  1.24.2.2     skrll 
    596  1.24.2.2     skrll 	prop_object_iterator_release(iter);
    597  1.24.2.2     skrll }
    598  1.24.2.2     skrll 
    599  1.24.2.2     skrll /*
    600       1.1   thorpej  * sysmon_envsys_unregister:
    601       1.1   thorpej  *
    602  1.24.2.2     skrll  *	+ Unregister a sysmon envsys device.
    603       1.1   thorpej  */
    604       1.1   thorpej void
    605       1.1   thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
    606       1.1   thorpej {
    607  1.24.2.1     skrll 	struct sme_sensor_names *snames;
    608  1.24.2.2     skrll 	prop_array_t array;
    609  1.24.2.1     skrll 
    610  1.24.2.1     skrll 	KASSERT(sme != NULL);
    611       1.1   thorpej 
    612      1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    613       1.7      yamt 	while (sme->sme_flags & SME_FLAG_BUSY) {
    614       1.8      yamt 		sme->sme_flags |= SME_FLAG_WANTED;
    615      1.18   xtraeme 		cv_wait(&sme_list_cv, &sme_list_mtx);
    616      1.18   xtraeme 	}
    617  1.24.2.2     skrll 	LIST_REMOVE(sme, sme_list);
    618  1.24.2.2     skrll 	mutex_exit(&sme_list_mtx);
    619  1.24.2.1     skrll 	/*
    620  1.24.2.2     skrll 	 * Remove all sensor descriptions from the singly linked list.
    621  1.24.2.2     skrll 	 */
    622  1.24.2.2     skrll 	while (!SLIST_EMPTY(&sme->sme_names_list)) {
    623  1.24.2.2     skrll 		snames = SLIST_FIRST(&sme->sme_names_list);
    624  1.24.2.2     skrll 		SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
    625  1.24.2.2     skrll 		kmem_free(snames, sizeof(*snames));
    626  1.24.2.2     skrll 	}
    627  1.24.2.2     skrll 	/*
    628  1.24.2.1     skrll 	 * Unregister all events associated with this device.
    629  1.24.2.1     skrll 	 */
    630  1.24.2.2     skrll 	sme_event_unregister_all(sme->sme_name);
    631  1.24.2.1     skrll 	/*
    632  1.24.2.2     skrll 	 * Remove the device (and all its objects) from the global dictionary.
    633  1.24.2.1     skrll 	 */
    634  1.24.2.2     skrll 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    635  1.24.2.2     skrll 	if (array) {
    636  1.24.2.2     skrll 		sysmon_envsys_destroy_plist(array);
    637  1.24.2.2     skrll 		prop_dictionary_remove(sme_propd, sme->sme_name);
    638       1.7      yamt 	}
    639       1.1   thorpej }
    640       1.1   thorpej 
    641       1.1   thorpej /*
    642       1.1   thorpej  * sysmon_envsys_find:
    643       1.1   thorpej  *
    644  1.24.2.2     skrll  *	+ Find a sysmon envsys device.
    645       1.1   thorpej  */
    646       1.1   thorpej struct sysmon_envsys *
    647      1.18   xtraeme sysmon_envsys_find(const char *name)
    648       1.1   thorpej {
    649       1.1   thorpej 	struct sysmon_envsys *sme;
    650       1.1   thorpej 
    651      1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    652       1.7      yamt again:
    653       1.1   thorpej 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    654       1.1   thorpej 	     sme = LIST_NEXT(sme, sme_list)) {
    655      1.18   xtraeme 			if (strcmp(sme->sme_name, name) == 0) {
    656      1.18   xtraeme 				if (sme->sme_flags & SME_FLAG_BUSY) {
    657      1.18   xtraeme 					sme->sme_flags |= SME_FLAG_WANTED;
    658      1.18   xtraeme 					cv_wait(&sme_list_cv, &sme_list_mtx);
    659      1.18   xtraeme 					goto again;
    660      1.18   xtraeme 				}
    661      1.18   xtraeme 				sme->sme_flags |= SME_FLAG_BUSY;
    662      1.18   xtraeme 				break;
    663      1.18   xtraeme 			}
    664      1.18   xtraeme 	}
    665      1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    666      1.18   xtraeme 	return sme;
    667      1.18   xtraeme }
    668      1.18   xtraeme 
    669      1.18   xtraeme /*
    670      1.18   xtraeme  * sysmon_envsys_release:
    671      1.18   xtraeme  *
    672  1.24.2.2     skrll  * 	+ Release a sysmon envsys device.
    673      1.18   xtraeme  */
    674      1.18   xtraeme void
    675      1.18   xtraeme sysmon_envsys_release(struct sysmon_envsys *sme)
    676      1.18   xtraeme {
    677      1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    678      1.18   xtraeme 	if (sme->sme_flags & SME_FLAG_WANTED)
    679      1.18   xtraeme 		cv_broadcast(&sme_list_cv);
    680      1.18   xtraeme 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    681      1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    682      1.18   xtraeme }
    683      1.18   xtraeme 
    684      1.18   xtraeme /* compatibility function */
    685      1.18   xtraeme #ifdef COMPAT_40
    686      1.18   xtraeme struct sysmon_envsys *
    687      1.18   xtraeme sysmon_envsys_find_40(u_int idx)
    688      1.18   xtraeme {
    689      1.18   xtraeme 	struct sysmon_envsys *sme;
    690      1.18   xtraeme 
    691      1.18   xtraeme 	mutex_enter(&sme_list_mtx);
    692      1.18   xtraeme 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    693      1.18   xtraeme 	     sme = LIST_NEXT(sme, sme_list)) {
    694       1.1   thorpej 		if (idx >= sme->sme_fsensor &&
    695      1.18   xtraeme 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    696      1.18   xtraeme 			break;
    697      1.18   xtraeme 	}
    698      1.18   xtraeme 	mutex_exit(&sme_list_mtx);
    699      1.18   xtraeme 	return sme;
    700      1.18   xtraeme }
    701      1.18   xtraeme #endif
    702      1.18   xtraeme 
    703      1.18   xtraeme /*
    704  1.24.2.1     skrll  * sme_register_sensorname:
    705  1.24.2.1     skrll  *
    706  1.24.2.2     skrll  * 	+ Register a sensor description into the list maintained per device.
    707  1.24.2.1     skrll  */
    708  1.24.2.1     skrll static int
    709  1.24.2.2     skrll sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
    710  1.24.2.1     skrll {
    711  1.24.2.1     skrll 	struct sme_sensor_names *snames, *snames2 = NULL;
    712  1.24.2.1     skrll 
    713  1.24.2.1     skrll 	KASSERT(edata != NULL);
    714  1.24.2.1     skrll 
    715  1.24.2.2     skrll 	SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
    716      1.18   xtraeme 		/*
    717  1.24.2.1     skrll 		 * Match sensors with empty and duplicate description.
    718      1.18   xtraeme 		 */
    719  1.24.2.1     skrll 		if (strlen(edata->desc) == 0 ||
    720  1.24.2.1     skrll 		    strcmp(snames2->desc, edata->desc) == 0)
    721  1.24.2.1     skrll 			if (snames2->assigned) {
    722  1.24.2.1     skrll 				edata->flags |= ENVSYS_FNOTVALID;
    723  1.24.2.1     skrll 				DPRINTF(("%s: duplicate name "
    724  1.24.2.1     skrll 				    "(%s)\n", __func__, edata->desc));
    725  1.24.2.1     skrll 				return EEXIST;
    726       1.7      yamt 			}
    727      1.18   xtraeme 	}
    728      1.18   xtraeme 
    729  1.24.2.2     skrll 	snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
    730  1.24.2.2     skrll 	if (snames == NULL)
    731  1.24.2.2     skrll 		return ENOMEM;
    732  1.24.2.2     skrll 
    733  1.24.2.1     skrll 	snames->assigned = true;
    734  1.24.2.1     skrll 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
    735  1.24.2.1     skrll 	DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
    736  1.24.2.2     skrll 	SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
    737  1.24.2.2     skrll 	sme->sme_uniqsensors++;
    738  1.24.2.1     skrll 
    739  1.24.2.1     skrll 	return 0;
    740      1.18   xtraeme }
    741      1.18   xtraeme 
    742      1.18   xtraeme /*
    743  1.24.2.2     skrll  * sme_add_sensor_dictionary:
    744      1.18   xtraeme  *
    745  1.24.2.2     skrll  * 	+ Add the objects into the dictionary.
    746      1.18   xtraeme  */
    747  1.24.2.1     skrll void
    748  1.24.2.2     skrll sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    749  1.24.2.2     skrll 		    	  prop_dictionary_t dict, envsys_data_t *edata)
    750      1.18   xtraeme {
    751      1.18   xtraeme 	const struct sme_sensor_type *est = sme_sensor_type;
    752  1.24.2.2     skrll 	const struct sme_sensor_type *ess = sme_sensor_state;
    753  1.24.2.2     skrll 	const struct sme_sensor_type *esds = sme_sensor_drive_state;
    754      1.18   xtraeme 	sme_event_drv_t *sme_evdrv_t = NULL;
    755  1.24.2.1     skrll 	int i, j;
    756  1.24.2.1     skrll 
    757  1.24.2.1     skrll 	i = j = 0;
    758      1.18   xtraeme 
    759      1.18   xtraeme 	/* find the correct unit for this sensor. */
    760      1.18   xtraeme 	for (i = 0; est[i].type != -1; i++)
    761      1.18   xtraeme 		if (est[i].type == edata->units)
    762      1.18   xtraeme 			break;
    763      1.18   xtraeme 
    764  1.24.2.1     skrll 	if (strcmp(est[i].desc, "unknown") == 0) {
    765  1.24.2.1     skrll 		DPRINTF(("%s: invalid units type for sensor=%d\n",
    766  1.24.2.1     skrll 		    __func__, edata->sensor));
    767  1.24.2.1     skrll 		goto invalidate_sensor;
    768  1.24.2.1     skrll 	}
    769  1.24.2.1     skrll 
    770      1.18   xtraeme 	/*
    771      1.18   xtraeme 	 * 		...
    772      1.18   xtraeme 	 * 		<key>type</key>
    773      1.18   xtraeme 	 * 		<string>foo</string>
    774      1.18   xtraeme 	 * 		<key>description</key>
    775      1.18   xtraeme 	 * 		<string>blah blah</string>
    776      1.18   xtraeme 	 * 		...
    777      1.18   xtraeme 	 */
    778  1.24.2.1     skrll 	if (sme_sensor_upstring(dict, "type", est[i].desc))
    779  1.24.2.1     skrll 		goto invalidate_sensor;
    780  1.24.2.1     skrll 
    781  1.24.2.1     skrll 	if (strlen(edata->desc) == 0) {
    782  1.24.2.1     skrll 		DPRINTF(("%s: invalid description for sensor=%d\n",
    783  1.24.2.1     skrll 		    __func__, edata->sensor));
    784  1.24.2.1     skrll 		goto invalidate_sensor;
    785  1.24.2.1     skrll 	}
    786      1.23   xtraeme 
    787  1.24.2.1     skrll 	if (sme_sensor_upstring(dict, "description", edata->desc))
    788  1.24.2.1     skrll 		goto invalidate_sensor;
    789      1.18   xtraeme 
    790      1.18   xtraeme 	/*
    791      1.18   xtraeme 	 * Add sensor's state description.
    792      1.18   xtraeme 	 *
    793      1.18   xtraeme 	 * 		...
    794      1.18   xtraeme 	 * 		<key>state</key>
    795      1.18   xtraeme 	 * 		<string>valid</string>
    796      1.18   xtraeme 	 * 		...
    797      1.18   xtraeme 	 */
    798      1.18   xtraeme 	for (j = 0; ess[j].type != -1; j++)
    799      1.18   xtraeme 		if (ess[j].type == edata->state)
    800       1.7      yamt 			break;
    801      1.18   xtraeme 
    802  1.24.2.1     skrll 	if (strcmp(ess[j].desc, "unknown") == 0) {
    803  1.24.2.1     skrll 		DPRINTF(("%s: invalid state for sensor=%d\n",
    804  1.24.2.1     skrll 		    __func__, edata->sensor));
    805  1.24.2.1     skrll 		goto invalidate_sensor;
    806  1.24.2.1     skrll 	}
    807  1.24.2.1     skrll 
    808  1.24.2.1     skrll 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
    809  1.24.2.1     skrll 	    __func__, edata->desc, edata->units, edata->state));
    810  1.24.2.1     skrll 
    811  1.24.2.1     skrll 	if (sme_sensor_upstring(dict, "state", ess[j].desc))
    812  1.24.2.1     skrll 		goto invalidate_sensor;
    813      1.18   xtraeme 
    814      1.18   xtraeme 	/*
    815      1.18   xtraeme 	 * Add the monitoring boolean object:
    816      1.18   xtraeme 	 *
    817      1.18   xtraeme 	 * 		...
    818      1.18   xtraeme 	 * 		<key>monitoring-supported</key>
    819      1.18   xtraeme 	 * 		<true/>
    820      1.18   xtraeme 	 *		...
    821      1.18   xtraeme 	 *
    822  1.24.2.2     skrll 	 * always false on Drive, Generic strings and Indicator types.
    823  1.24.2.2     skrll 	 * They cannot be monitored.
    824      1.18   xtraeme 	 *
    825      1.18   xtraeme 	 */
    826      1.18   xtraeme 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    827      1.18   xtraeme 	    (edata->units == ENVSYS_INDICATOR) ||
    828  1.24.2.2     skrll 	    (edata->units == ENVSYS_DRIVE) ||
    829  1.24.2.2     skrll 	    (edata->units == ENVSYS_GSTRING)) {
    830  1.24.2.1     skrll 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
    831  1.24.2.2     skrll 			return;
    832      1.18   xtraeme 	} else {
    833  1.24.2.1     skrll 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
    834  1.24.2.2     skrll 			return;
    835  1.24.2.2     skrll 	}
    836  1.24.2.2     skrll 
    837  1.24.2.2     skrll 	/*
    838  1.24.2.2     skrll 	 * Add the generic-state-string object for gstring sensors:
    839  1.24.2.2     skrll 	 *
    840  1.24.2.2     skrll 	 * 		...
    841  1.24.2.2     skrll 	 * 		<key>generic-state-string</key>
    842  1.24.2.2     skrll 	 * 		<string>NORMAL</string>
    843  1.24.2.2     skrll 	 * 		...
    844  1.24.2.2     skrll 	 */
    845  1.24.2.2     skrll 	if (edata->units == ENVSYS_GSTRING) {
    846  1.24.2.2     skrll 		if (strlen(edata->genstr) == 0) {
    847  1.24.2.2     skrll 			DPRINTF(("%s: invalid generic state string for "
    848  1.24.2.2     skrll 			    "sensor=%s\n", __func__, edata->desc));
    849  1.24.2.2     skrll 			goto invalidate_sensor;
    850  1.24.2.2     skrll 		}
    851  1.24.2.2     skrll 		if (sme_sensor_upstring(dict,
    852  1.24.2.2     skrll 					"generic-state-string",
    853  1.24.2.2     skrll 					edata->genstr))
    854  1.24.2.2     skrll 			goto invalidate_sensor;
    855  1.24.2.2     skrll 
    856  1.24.2.2     skrll 		goto add_sensor;
    857      1.18   xtraeme 	}
    858      1.18   xtraeme 
    859      1.18   xtraeme 	/*
    860  1.24.2.2     skrll 	 * add the percentage boolean object:
    861  1.24.2.2     skrll 	 *
    862  1.24.2.2     skrll 	 * 		...
    863  1.24.2.2     skrll 	 * 		<key>want-percentage</key>
    864  1.24.2.2     skrll 	 * 		<true/>
    865  1.24.2.2     skrll 	 * 		...
    866  1.24.2.2     skrll 	 */
    867  1.24.2.2     skrll 	if (edata->flags & ENVSYS_FPERCENT)
    868  1.24.2.2     skrll 		if (sme_sensor_upbool(dict, "want-percentage", true))
    869  1.24.2.2     skrll 			return;
    870  1.24.2.2     skrll 
    871  1.24.2.2     skrll 	/*
    872      1.18   xtraeme 	 * Add the drive-state object for drive sensors:
    873      1.18   xtraeme 	 *
    874      1.18   xtraeme 	 * 		...
    875      1.18   xtraeme 	 * 		<key>drive-state</key>
    876      1.18   xtraeme 	 * 		<string>drive is online</string>
    877      1.18   xtraeme 	 * 		...
    878      1.18   xtraeme 	 */
    879      1.18   xtraeme 	if (edata->units == ENVSYS_DRIVE) {
    880  1.24.2.1     skrll 		for (j = 0; esds[j].type != -1; j++)
    881  1.24.2.1     skrll 			if (esds[j].type == edata->value_cur)
    882      1.18   xtraeme 				break;
    883      1.18   xtraeme 
    884  1.24.2.1     skrll 		if (sme_sensor_upstring(dict, "drive-state", esds[j].desc))
    885  1.24.2.2     skrll 			return;
    886      1.18   xtraeme 	}
    887      1.18   xtraeme 
    888  1.24.2.2     skrll 	/*
    889  1.24.2.2     skrll 	 * if sensor is enabled, add the following properties...
    890  1.24.2.2     skrll 	 */
    891      1.18   xtraeme 	if (edata->state == ENVSYS_SVALID) {
    892      1.18   xtraeme 		/*
    893      1.18   xtraeme 		 * 	...
    894      1.18   xtraeme 		 * 	<key>rpms</key>
    895      1.18   xtraeme 		 * 	<integer>2500</integer>
    896      1.18   xtraeme 		 * 	<key>rfact</key>
    897      1.18   xtraeme 		 * 	<integer>10000</integer>
    898      1.18   xtraeme 		 * 	<key>cur-value</key>
    899      1.18   xtraeme 		 * 	<integer>1250</integer>
    900      1.18   xtraeme 		 * 	<key>min-value</key>
    901      1.18   xtraeme 		 * 	<integer>800</integer>
    902      1.18   xtraeme 		 * 	<key>max-value</integer>
    903      1.18   xtraeme 		 * 	<integer>3000</integer>
    904      1.18   xtraeme 		 * 	<key>avg-value</integer>
    905      1.18   xtraeme 		 * 	<integer>1400</integer>
    906      1.18   xtraeme 		 * </dict>
    907      1.18   xtraeme 		 */
    908  1.24.2.1     skrll 		if (edata->units == ENVSYS_SFANRPM)
    909  1.24.2.1     skrll 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
    910  1.24.2.2     skrll 				return;
    911      1.18   xtraeme 
    912  1.24.2.1     skrll 		if (edata->units == ENVSYS_SVOLTS_AC ||
    913  1.24.2.1     skrll 		    edata->units == ENVSYS_SVOLTS_DC)
    914  1.24.2.1     skrll 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
    915  1.24.2.2     skrll 				return;
    916      1.18   xtraeme 
    917  1.24.2.1     skrll 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
    918  1.24.2.2     skrll 			return;
    919      1.18   xtraeme 
    920  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FVALID_MIN) {
    921  1.24.2.1     skrll 			if (sme_sensor_upint32(dict,
    922  1.24.2.1     skrll 					       "min-value",
    923  1.24.2.1     skrll 					       edata->value_min))
    924  1.24.2.2     skrll 				return;
    925  1.24.2.1     skrll 		}
    926  1.24.2.1     skrll 
    927  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FVALID_MAX) {
    928  1.24.2.1     skrll 			if (sme_sensor_upint32(dict,
    929  1.24.2.1     skrll 					       "max-value",
    930  1.24.2.1     skrll 					       edata->value_max))
    931  1.24.2.2     skrll 				return;
    932  1.24.2.1     skrll 		}
    933  1.24.2.1     skrll 
    934  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FVALID_AVG) {
    935  1.24.2.1     skrll 			if (sme_sensor_upint32(dict,
    936  1.24.2.1     skrll 					       "avg-value",
    937  1.24.2.1     skrll 					        edata->value_avg))
    938  1.24.2.2     skrll 				return;
    939  1.24.2.1     skrll 		}
    940      1.18   xtraeme 	}
    941      1.18   xtraeme 
    942      1.18   xtraeme 	/*
    943      1.18   xtraeme 	 * 	...
    944      1.18   xtraeme 	 * </array>
    945  1.24.2.2     skrll 	 *
    946  1.24.2.2     skrll 	 * Add the dictionary into the array.
    947  1.24.2.2     skrll 	 *
    948      1.18   xtraeme 	 */
    949  1.24.2.1     skrll 
    950  1.24.2.2     skrll add_sensor:
    951  1.24.2.2     skrll 	if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
    952  1.24.2.1     skrll 		DPRINTF(("%s: prop_array_add\n", __func__));
    953  1.24.2.1     skrll 		goto invalidate_sensor;
    954  1.24.2.1     skrll 	}
    955  1.24.2.1     skrll 
    956  1.24.2.1     skrll 	/*
    957  1.24.2.1     skrll 	 * Add a new event if a monitoring flag was set.
    958  1.24.2.1     skrll 	 */
    959  1.24.2.1     skrll 	if (edata->monitor) {
    960  1.24.2.2     skrll 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_NOSLEEP);
    961  1.24.2.2     skrll 		if (sme_evdrv_t == NULL) {
    962  1.24.2.2     skrll 			DPRINTF(("%s: edata->monitor failed\n", __func__));
    963  1.24.2.2     skrll 			return;
    964  1.24.2.2     skrll 		}
    965  1.24.2.1     skrll 
    966  1.24.2.1     skrll 		sme_evdrv_t->sdict = dict;
    967  1.24.2.1     skrll 		sme_evdrv_t->edata = edata;
    968  1.24.2.1     skrll 		sme_evdrv_t->sme = sme;
    969  1.24.2.1     skrll 		sme_evdrv_t->powertype = est[i].crittype;
    970  1.24.2.1     skrll 
    971  1.24.2.1     skrll 		sysmon_task_queue_init();
    972  1.24.2.1     skrll 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
    973  1.24.2.1     skrll 	}
    974  1.24.2.1     skrll 
    975  1.24.2.1     skrll 	return;
    976  1.24.2.1     skrll 
    977  1.24.2.1     skrll invalidate_sensor:
    978  1.24.2.1     skrll 	edata->flags |= ENVSYS_FNOTVALID;
    979      1.18   xtraeme }
    980      1.18   xtraeme 
    981      1.18   xtraeme /*
    982      1.18   xtraeme  * sme_update_dictionary:
    983      1.18   xtraeme  *
    984      1.18   xtraeme  * 	+ Update per-sensor dictionaries with new values if there were
    985      1.18   xtraeme  * 	  changes, otherwise the object in dictionary is untouched.
    986      1.18   xtraeme  */
    987      1.18   xtraeme int
    988      1.18   xtraeme sme_update_dictionary(struct sysmon_envsys *sme)
    989      1.18   xtraeme {
    990  1.24.2.1     skrll 	const struct sme_sensor_type *est = sme_sensor_type;
    991  1.24.2.2     skrll 	const struct sme_sensor_type *ess = sme_sensor_state;
    992  1.24.2.2     skrll 	const struct sme_sensor_type *esds = sme_sensor_drive_state;
    993      1.18   xtraeme 	envsys_data_t *edata = NULL;
    994  1.24.2.1     skrll 	prop_object_t array, dict;
    995  1.24.2.1     skrll 	int i, j, error, invalid;
    996  1.24.2.1     skrll 
    997  1.24.2.2     skrll 	KASSERT(mutex_owned(&sme_list_mtx));
    998  1.24.2.2     skrll 
    999  1.24.2.1     skrll 	error = invalid = 0;
   1000  1.24.2.1     skrll 	array = dict = NULL;
   1001      1.18   xtraeme 
   1002      1.18   xtraeme 	/* retrieve the array of dictionaries in device. */
   1003      1.18   xtraeme 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1004      1.24   xtraeme 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
   1005      1.24   xtraeme 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
   1006      1.18   xtraeme 		return EINVAL;
   1007      1.24   xtraeme 	}
   1008      1.18   xtraeme 
   1009      1.18   xtraeme 	/*
   1010      1.18   xtraeme 	 * - iterate over all sensors.
   1011      1.18   xtraeme 	 * - fetch new data.
   1012      1.18   xtraeme 	 * - check if data in dictionary is different than new data.
   1013      1.18   xtraeme 	 * - update dictionary if there were changes.
   1014      1.18   xtraeme 	 */
   1015      1.18   xtraeme 	for (i = 0; i < sme->sme_nsensors; i++) {
   1016      1.18   xtraeme 		edata = &sme->sme_sensor_data[i];
   1017  1.24.2.1     skrll 		/* skip invalid sensors */
   1018  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FNOTVALID) {
   1019  1.24.2.1     skrll 			invalid++;
   1020      1.23   xtraeme 			continue;
   1021      1.24   xtraeme 		}
   1022      1.23   xtraeme 
   1023      1.18   xtraeme 		/*
   1024      1.18   xtraeme 		 * refresh sensor data via sme_gtredata only if the
   1025      1.18   xtraeme 		 * flag is not set.
   1026      1.18   xtraeme 		 */
   1027      1.18   xtraeme 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
   1028      1.18   xtraeme 			error = (*sme->sme_gtredata)(sme, edata);
   1029      1.18   xtraeme 			if (error) {
   1030      1.18   xtraeme 				DPRINTF(("%s: gtredata[%d] failed\n",
   1031      1.18   xtraeme 				    __func__, i));
   1032      1.18   xtraeme 				return error;
   1033      1.18   xtraeme 			}
   1034      1.18   xtraeme 		}
   1035      1.18   xtraeme 
   1036      1.18   xtraeme 		/* retrieve sensor's dictionary. */
   1037  1.24.2.1     skrll 		dict = prop_array_get(array, i - invalid);
   1038      1.24   xtraeme 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1039      1.24   xtraeme 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1040      1.24   xtraeme 			    __func__, edata->sensor, sme->sme_name));
   1041      1.18   xtraeme 			return EINVAL;
   1042      1.24   xtraeme 		}
   1043      1.18   xtraeme 
   1044      1.18   xtraeme 		for (j = 0; ess[j].type != -1; j++)
   1045      1.18   xtraeme 			if (ess[j].type == edata->state)
   1046      1.18   xtraeme 				break;
   1047      1.18   xtraeme 
   1048      1.18   xtraeme 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1049      1.18   xtraeme 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
   1050      1.18   xtraeme 		    ess[j].type, edata->flags, edata->units, edata->sensor));
   1051      1.18   xtraeme 
   1052      1.18   xtraeme 		/* update sensor state */
   1053  1.24.2.1     skrll 		error = sme_sensor_upstring(dict, "state", ess[j].desc);
   1054  1.24.2.1     skrll 		if (error)
   1055  1.24.2.1     skrll 			break;
   1056  1.24.2.1     skrll 
   1057  1.24.2.1     skrll 		for (j = 0; est[j].type != -1; j++)
   1058  1.24.2.1     skrll 			if (est[j].type == edata->units)
   1059  1.24.2.1     skrll 				break;
   1060  1.24.2.1     skrll 
   1061  1.24.2.1     skrll 		/* update sensor type */
   1062  1.24.2.1     skrll 		error = sme_sensor_upstring(dict, "type", est[j].desc);
   1063  1.24.2.1     skrll 		if (error)
   1064  1.24.2.1     skrll 			break;
   1065      1.18   xtraeme 
   1066  1.24.2.2     skrll 		/*
   1067  1.24.2.2     skrll 		 * Update the generic-state-string object for gstring
   1068  1.24.2.2     skrll 		 * sensors, they only need the following objects:
   1069  1.24.2.2     skrll 		 *
   1070  1.24.2.2     skrll 		 * 	description, generic-state-string, state and type.
   1071  1.24.2.2     skrll 		 *
   1072  1.24.2.2     skrll 		 */
   1073  1.24.2.2     skrll 		if (edata->units == ENVSYS_GSTRING) {
   1074  1.24.2.2     skrll 			error = sme_sensor_upstring(dict,
   1075  1.24.2.2     skrll 						    "generic-state-string",
   1076  1.24.2.2     skrll 						    edata->genstr);
   1077  1.24.2.2     skrll 			if (error)
   1078  1.24.2.2     skrll 				break;
   1079  1.24.2.2     skrll 			continue;
   1080  1.24.2.2     skrll 		}
   1081  1.24.2.2     skrll 
   1082      1.18   xtraeme 		/* update sensor current value */
   1083  1.24.2.1     skrll 		error = sme_sensor_upint32(dict,
   1084  1.24.2.1     skrll 					   "cur-value",
   1085  1.24.2.1     skrll 					   edata->value_cur);
   1086  1.24.2.1     skrll 		if (error)
   1087  1.24.2.1     skrll 			break;
   1088      1.18   xtraeme 
   1089      1.18   xtraeme 		/*
   1090      1.18   xtraeme 		 * Integer and Indicator types do not the following
   1091      1.18   xtraeme 		 * values, so skip them.
   1092      1.18   xtraeme 		 */
   1093      1.18   xtraeme 		if (edata->units == ENVSYS_INTEGER ||
   1094      1.18   xtraeme 		    edata->units == ENVSYS_INDICATOR)
   1095      1.18   xtraeme 			continue;
   1096      1.18   xtraeme 
   1097      1.18   xtraeme 		/* update sensor flags */
   1098  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FPERCENT) {
   1099  1.24.2.1     skrll 			error = sme_sensor_upbool(dict,
   1100  1.24.2.1     skrll 						  "want-percentage",
   1101  1.24.2.1     skrll 						  true);
   1102  1.24.2.1     skrll 			if (error)
   1103  1.24.2.1     skrll 				break;
   1104      1.18   xtraeme 		}
   1105      1.18   xtraeme 
   1106  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1107  1.24.2.1     skrll 			error = sme_sensor_upint32(dict,
   1108  1.24.2.1     skrll 						   "max-value",
   1109  1.24.2.1     skrll 						   edata->value_max);
   1110  1.24.2.1     skrll 			if (error)
   1111  1.24.2.1     skrll 				break;
   1112  1.24.2.1     skrll 		}
   1113  1.24.2.1     skrll 
   1114  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1115  1.24.2.1     skrll 			error = sme_sensor_upint32(dict,
   1116  1.24.2.1     skrll 						   "min-value",
   1117  1.24.2.1     skrll 						   edata->value_min);
   1118  1.24.2.1     skrll 			if (error)
   1119  1.24.2.1     skrll 				break;
   1120  1.24.2.1     skrll 		}
   1121      1.18   xtraeme 
   1122  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1123  1.24.2.1     skrll 			error = sme_sensor_upint32(dict,
   1124  1.24.2.1     skrll 						   "avg-value",
   1125  1.24.2.1     skrll 						   edata->value_avg);
   1126  1.24.2.1     skrll 			if (error)
   1127  1.24.2.1     skrll 				break;
   1128  1.24.2.1     skrll 		}
   1129      1.18   xtraeme 
   1130      1.18   xtraeme 		/* update 'rpms' only in ENVSYS_SFANRPM. */
   1131  1.24.2.1     skrll 		if (edata->units == ENVSYS_SFANRPM) {
   1132  1.24.2.1     skrll 			error = sme_sensor_upuint32(dict,
   1133  1.24.2.1     skrll 						    "rpms",
   1134  1.24.2.1     skrll 						    edata->rpms);
   1135  1.24.2.1     skrll 			if (error)
   1136  1.24.2.1     skrll 				break;
   1137  1.24.2.1     skrll 		}
   1138      1.18   xtraeme 
   1139      1.18   xtraeme 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
   1140      1.18   xtraeme 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1141      1.18   xtraeme 		    edata->units == ENVSYS_SVOLTS_DC) {
   1142  1.24.2.1     skrll 			error = sme_sensor_upint32(dict,
   1143  1.24.2.1     skrll 						   "rfact",
   1144  1.24.2.1     skrll 						   edata->rfact);
   1145  1.24.2.1     skrll 			if (error)
   1146  1.24.2.1     skrll 				break;
   1147      1.18   xtraeme 		}
   1148      1.18   xtraeme 
   1149      1.18   xtraeme 		/* update 'drive-state' only in ENVSYS_DRIVE. */
   1150      1.18   xtraeme 		if (edata->units == ENVSYS_DRIVE) {
   1151      1.18   xtraeme 			for (j = 0; esds[j].type != -1; j++)
   1152      1.18   xtraeme 				if (esds[j].type == edata->value_cur)
   1153      1.18   xtraeme 					break;
   1154      1.18   xtraeme 
   1155  1.24.2.1     skrll 			error = sme_sensor_upstring(dict,
   1156  1.24.2.1     skrll 						    "drive-state",
   1157  1.24.2.1     skrll 						    esds[j].desc);
   1158  1.24.2.1     skrll 			if (error)
   1159  1.24.2.1     skrll 				break;
   1160       1.7      yamt 		}
   1161       1.1   thorpej 	}
   1162       1.1   thorpej 
   1163      1.18   xtraeme 	return error;
   1164       1.1   thorpej }
   1165       1.1   thorpej 
   1166       1.1   thorpej /*
   1167      1.18   xtraeme  * sme_userset_dictionary:
   1168       1.1   thorpej  *
   1169  1.24.2.2     skrll  * 	+ Parse the userland dictionary and run the appropiate
   1170  1.24.2.2     skrll  * 	  task that was requested.
   1171       1.1   thorpej  */
   1172      1.18   xtraeme int
   1173      1.18   xtraeme sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1174      1.18   xtraeme 		       prop_array_t array)
   1175       1.1   thorpej {
   1176      1.18   xtraeme 	const struct sme_sensor_type *sst = sme_sensor_type;
   1177      1.23   xtraeme 	envsys_data_t *edata, *nedata;
   1178      1.18   xtraeme 	prop_dictionary_t dict;
   1179      1.18   xtraeme 	prop_object_t obj, obj1, obj2;
   1180      1.18   xtraeme 	int32_t critval;
   1181  1.24.2.1     skrll 	int i, invalid, error;
   1182      1.18   xtraeme 	const char *blah, *sname;
   1183      1.18   xtraeme 	bool targetfound = false;
   1184      1.18   xtraeme 
   1185  1.24.2.1     skrll 	error = invalid = 0;
   1186      1.18   xtraeme 	blah = sname = NULL;
   1187      1.18   xtraeme 
   1188      1.18   xtraeme 	/* get sensor's name from userland dictionary. */
   1189      1.18   xtraeme 	obj = prop_dictionary_get(udict, "sensor-name");
   1190      1.18   xtraeme 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1191      1.18   xtraeme 		DPRINTF(("%s: sensor-name failed\n", __func__));
   1192      1.18   xtraeme 		return EINVAL;
   1193      1.18   xtraeme 	}
   1194       1.1   thorpej 
   1195      1.18   xtraeme 	/* iterate over the sensors to find the right one */
   1196      1.18   xtraeme 	for (i = 0; i < sme->sme_nsensors; i++) {
   1197      1.18   xtraeme 		edata = &sme->sme_sensor_data[i];
   1198  1.24.2.1     skrll 		/* skip sensors with duplicate description */
   1199  1.24.2.1     skrll 		if (edata->flags & ENVSYS_FNOTVALID) {
   1200  1.24.2.1     skrll 			invalid++;
   1201  1.24.2.1     skrll 			continue;
   1202  1.24.2.1     skrll 		}
   1203  1.24.2.1     skrll 
   1204  1.24.2.1     skrll 		dict = prop_array_get(array, i - invalid);
   1205      1.18   xtraeme 		obj1 = prop_dictionary_get(dict, "description");
   1206      1.18   xtraeme 
   1207      1.18   xtraeme 		/* is it our sensor? */
   1208      1.18   xtraeme 		if (!prop_string_equals(obj1, obj))
   1209      1.18   xtraeme 			continue;
   1210      1.18   xtraeme 
   1211      1.18   xtraeme 		/*
   1212      1.18   xtraeme 		 * Check if a new description operation was
   1213      1.18   xtraeme 		 * requested by the user and set new description.
   1214      1.18   xtraeme 		 */
   1215      1.18   xtraeme 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
   1216      1.18   xtraeme 			targetfound = true;
   1217      1.18   xtraeme 			blah = prop_string_cstring_nocopy(obj2);
   1218      1.23   xtraeme 
   1219      1.23   xtraeme 			for (i = 0; i < sme->sme_nsensors; i++) {
   1220      1.23   xtraeme 				if (i == edata->sensor)
   1221      1.23   xtraeme 					continue;
   1222      1.23   xtraeme 
   1223      1.23   xtraeme 				nedata = &sme->sme_sensor_data[i];
   1224      1.23   xtraeme 				if (strcmp(blah, nedata->desc) == 0) {
   1225      1.23   xtraeme 					error = EEXIST;
   1226  1.24.2.1     skrll 					break;
   1227      1.23   xtraeme 				}
   1228      1.23   xtraeme 			}
   1229      1.23   xtraeme 
   1230  1.24.2.1     skrll 			if (error)
   1231  1.24.2.1     skrll 				break;
   1232  1.24.2.1     skrll 
   1233  1.24.2.1     skrll 			error = sme_sensor_upstring(dict,
   1234  1.24.2.1     skrll 						    "description",
   1235  1.24.2.1     skrll 						    blah);
   1236  1.24.2.1     skrll 			if (!error)
   1237  1.24.2.1     skrll 				(void)strlcpy(edata->desc,
   1238  1.24.2.1     skrll 					      blah,
   1239  1.24.2.1     skrll 					      sizeof(edata->desc));
   1240      1.23   xtraeme 
   1241      1.18   xtraeme 			break;
   1242      1.18   xtraeme 		}
   1243      1.18   xtraeme 
   1244      1.18   xtraeme 		/* did the user want to remove a critical capacity limit? */
   1245      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
   1246      1.18   xtraeme 		if (obj2 != NULL) {
   1247      1.18   xtraeme 			targetfound = true;
   1248      1.18   xtraeme 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1249      1.18   xtraeme 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1250      1.18   xtraeme 				    error = ENOTSUP;
   1251      1.18   xtraeme 				    break;
   1252      1.18   xtraeme 			}
   1253      1.18   xtraeme 
   1254      1.18   xtraeme 			sname = prop_string_cstring_nocopy(obj);
   1255      1.18   xtraeme 			error = sme_event_unregister(sname,
   1256      1.18   xtraeme 			    PENVSYS_EVENT_BATT_USERCAP);
   1257      1.18   xtraeme 			if (error)
   1258      1.18   xtraeme 				break;
   1259      1.18   xtraeme 
   1260      1.18   xtraeme 			prop_dictionary_remove(dict, "critical-capacity");
   1261      1.18   xtraeme 			break;
   1262      1.18   xtraeme 		}
   1263      1.18   xtraeme 
   1264      1.18   xtraeme 		/* did the user want to remove a critical min limit? */
   1265      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
   1266      1.18   xtraeme 		if (obj2 != NULL) {
   1267      1.18   xtraeme 			targetfound = true;
   1268      1.18   xtraeme 			sname = prop_string_cstring_nocopy(obj);
   1269      1.18   xtraeme 			error = sme_event_unregister(sname,
   1270      1.18   xtraeme 			    PENVSYS_EVENT_USER_CRITMIN);
   1271      1.18   xtraeme 			if (error)
   1272      1.18   xtraeme 				break;
   1273      1.18   xtraeme 
   1274      1.18   xtraeme 			prop_dictionary_remove(dict, "critical-min-limit");
   1275      1.18   xtraeme 			break;
   1276      1.18   xtraeme 		}
   1277      1.18   xtraeme 
   1278      1.18   xtraeme 		/* did the user want to remove a critical max limit? */
   1279      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1280      1.18   xtraeme 		if (obj2 != NULL) {
   1281      1.18   xtraeme 			targetfound = true;
   1282      1.18   xtraeme 			sname = prop_string_cstring_nocopy(obj);
   1283      1.18   xtraeme 			error = sme_event_unregister(sname,
   1284      1.18   xtraeme 			    PENVSYS_EVENT_USER_CRITMAX);
   1285      1.18   xtraeme 			if (error)
   1286      1.18   xtraeme 				break;
   1287       1.7      yamt 
   1288      1.18   xtraeme 			prop_dictionary_remove(dict, "critical-max-limit");
   1289      1.18   xtraeme 			break;
   1290      1.18   xtraeme 		}
   1291      1.18   xtraeme 
   1292      1.18   xtraeme 		/* did the user want to change rfact? */
   1293      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1294      1.18   xtraeme 		if (obj2 != NULL) {
   1295      1.18   xtraeme 			targetfound = true;
   1296      1.18   xtraeme 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1297      1.18   xtraeme 				edata->rfact = prop_number_integer_value(obj2);
   1298      1.18   xtraeme 			else
   1299      1.18   xtraeme 				error = ENOTSUP;
   1300      1.18   xtraeme 
   1301      1.18   xtraeme 			break;
   1302      1.18   xtraeme 		}
   1303      1.18   xtraeme 
   1304      1.18   xtraeme 		for (i = 0; sst[i].type != -1; i++)
   1305      1.18   xtraeme 			if (sst[i].type == edata->units)
   1306      1.18   xtraeme 				break;
   1307      1.18   xtraeme 
   1308      1.18   xtraeme 		/* did the user want to set a critical capacity event? */
   1309      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1310      1.18   xtraeme 		if (obj2 != NULL) {
   1311      1.18   xtraeme 			targetfound = true;
   1312      1.18   xtraeme 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1313      1.18   xtraeme 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1314      1.18   xtraeme 				error = ENOTSUP;
   1315      1.18   xtraeme 				break;
   1316      1.18   xtraeme 			}
   1317      1.18   xtraeme 
   1318      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1319  1.24.2.2     skrll 			error = sme_event_register(dict,
   1320      1.18   xtraeme 					      edata,
   1321      1.18   xtraeme 					      sme->sme_name,
   1322      1.18   xtraeme 					      "critical-capacity",
   1323      1.18   xtraeme 					      critval,
   1324      1.18   xtraeme 					      PENVSYS_EVENT_BATT_USERCAP,
   1325      1.18   xtraeme 					      sst[i].crittype);
   1326      1.18   xtraeme 			break;
   1327      1.18   xtraeme 		}
   1328      1.18   xtraeme 
   1329      1.18   xtraeme 		/* did the user want to set a critical max event? */
   1330      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1331      1.18   xtraeme 		if (obj2 != NULL) {
   1332      1.18   xtraeme 			targetfound = true;
   1333      1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1334      1.18   xtraeme 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1335      1.18   xtraeme 				error = ENOTSUP;
   1336      1.18   xtraeme 				break;
   1337      1.18   xtraeme 			}
   1338      1.18   xtraeme 
   1339      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1340  1.24.2.2     skrll 			error = sme_event_register(dict,
   1341      1.18   xtraeme 					      edata,
   1342      1.18   xtraeme 					      sme->sme_name,
   1343      1.18   xtraeme 					      "critical-max-limit",
   1344      1.18   xtraeme 					      critval,
   1345      1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMAX,
   1346      1.18   xtraeme 					      sst[i].crittype);
   1347      1.18   xtraeme 			break;
   1348      1.18   xtraeme 		}
   1349      1.18   xtraeme 
   1350      1.18   xtraeme 		/* did the user want to set a critical min event? */
   1351      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1352      1.18   xtraeme 		if (obj2 != NULL) {
   1353      1.18   xtraeme 			targetfound = true;
   1354      1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1355      1.18   xtraeme 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1356      1.18   xtraeme 				error = ENOTSUP;
   1357      1.18   xtraeme 				break;
   1358      1.18   xtraeme 			}
   1359      1.18   xtraeme 
   1360      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1361  1.24.2.2     skrll 			error = sme_event_register(dict,
   1362      1.18   xtraeme 					      edata,
   1363      1.18   xtraeme 					      sme->sme_name,
   1364      1.18   xtraeme 					      "critical-min-limit",
   1365      1.18   xtraeme 					      critval,
   1366      1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMIN,
   1367      1.18   xtraeme 					      sst[i].crittype);
   1368      1.18   xtraeme 			break;
   1369      1.18   xtraeme 		}
   1370      1.18   xtraeme 	}
   1371      1.18   xtraeme 
   1372      1.18   xtraeme 	/* invalid target? return the error */
   1373      1.18   xtraeme 	if (!targetfound)
   1374      1.18   xtraeme 		error = EINVAL;
   1375      1.18   xtraeme 
   1376      1.18   xtraeme 	return error;
   1377       1.1   thorpej }
   1378