Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.8.16.5
      1  1.8.16.5      yamt /*	$NetBSD: sysmon_envsys.c,v 1.8.16.5 2007/11/15 11:44:31 yamt Exp $	*/
      2  1.8.16.3      yamt 
      3  1.8.16.3      yamt /*-
      4  1.8.16.4      yamt  * Copyright (c) 2007 Juan Romero Pardines.
      5  1.8.16.3      yamt  * All rights reserved.
      6  1.8.16.3      yamt  *
      7  1.8.16.3      yamt  * Redistribution and use in source and binary forms, with or without
      8  1.8.16.3      yamt  * modification, are permitted provided that the following conditions
      9  1.8.16.3      yamt  * are met:
     10  1.8.16.3      yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.8.16.3      yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.8.16.3      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.8.16.3      yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.8.16.3      yamt  *    documentation and/or other materials provided with the distribution.
     15  1.8.16.4      yamt  *
     16  1.8.16.4      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.8.16.4      yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.8.16.4      yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.8.16.4      yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.8.16.4      yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.8.16.4      yamt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.8.16.4      yamt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.8.16.4      yamt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.8.16.4      yamt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.8.16.4      yamt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.8.16.3      yamt  */
     27       1.1   thorpej 
     28       1.1   thorpej /*-
     29       1.1   thorpej  * Copyright (c) 2000 Zembu Labs, Inc.
     30       1.1   thorpej  * All rights reserved.
     31       1.1   thorpej  *
     32       1.1   thorpej  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
     33       1.1   thorpej  *
     34       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     35       1.1   thorpej  * modification, are permitted provided that the following conditions
     36       1.1   thorpej  * are met:
     37       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     38       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     39       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     40       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     41       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     42       1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     43       1.1   thorpej  *    must display the following acknowledgement:
     44       1.1   thorpej  *	This product includes software developed by Zembu Labs, Inc.
     45       1.1   thorpej  * 4. Neither the name of Zembu Labs nor the names of its employees may
     46       1.1   thorpej  *    be used to endorse or promote products derived from this software
     47       1.1   thorpej  *    without specific prior written permission.
     48       1.1   thorpej  *
     49       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     50       1.1   thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     51       1.1   thorpej  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     52       1.1   thorpej  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     53       1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54       1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55       1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56       1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57       1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58       1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59       1.1   thorpej  */
     60       1.1   thorpej 
     61       1.1   thorpej /*
     62  1.8.16.3      yamt  * Environmental sensor framework for sysmon, exported to userland
     63  1.8.16.3      yamt  * with proplib(3).
     64       1.1   thorpej  */
     65       1.2     lukem 
     66       1.2     lukem #include <sys/cdefs.h>
     67  1.8.16.5      yamt __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.8.16.5 2007/11/15 11:44:31 yamt Exp $");
     68       1.1   thorpej 
     69       1.1   thorpej #include <sys/param.h>
     70  1.8.16.3      yamt #include <sys/types.h>
     71       1.1   thorpej #include <sys/conf.h>
     72       1.1   thorpej #include <sys/errno.h>
     73       1.1   thorpej #include <sys/fcntl.h>
     74       1.1   thorpej #include <sys/kernel.h>
     75       1.1   thorpej #include <sys/systm.h>
     76       1.1   thorpej #include <sys/proc.h>
     77  1.8.16.3      yamt #include <sys/mutex.h>
     78  1.8.16.3      yamt #include <sys/kmem.h>
     79       1.1   thorpej 
     80  1.8.16.4      yamt /* #define ENVSYS_DEBUG */
     81       1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     82  1.8.16.3      yamt #include <dev/sysmon/sysmon_envsysvar.h>
     83  1.8.16.3      yamt #include <dev/sysmon/sysmon_taskq.h>
     84       1.1   thorpej 
     85  1.8.16.4      yamt /*
     86  1.8.16.4      yamt  * Notes about locking:
     87  1.8.16.4      yamt  *
     88  1.8.16.4      yamt  * There's a global lock 'sme_mtx' to protect access to 'sysmon_envsys_list'
     89  1.8.16.4      yamt  * (devices linked list), 'struct sysmon_envsys' (device), 'sme_events_list'
     90  1.8.16.4      yamt  * (events linked list), 'sme_event_t' (event) and the global counter
     91  1.8.16.4      yamt  * 'sysmon_envsys_next_sensor_index'.
     92  1.8.16.4      yamt  *
     93  1.8.16.4      yamt  * Another lock 'sme_init_mtx' is used to protect initialization and
     94  1.8.16.4      yamt  * finalization of the events framework (the callout(9) and workqueue(9)
     95  1.8.16.4      yamt  * that is used to check for conditions and sending events to powerd(8)).
     96  1.8.16.4      yamt  *
     97  1.8.16.4      yamt  * The global 'sme_cv' condition variable is used to wait for state changes
     98  1.8.16.4      yamt  * on the 'sysmon_envsys_list' and 'sme_events_list' linked lists.
     99  1.8.16.4      yamt  *
    100  1.8.16.4      yamt  */
    101  1.8.16.3      yamt 
    102  1.8.16.4      yamt kmutex_t sme_mtx, sme_event_init_mtx;
    103  1.8.16.4      yamt kcondvar_t sme_cv;
    104  1.8.16.3      yamt 
    105  1.8.16.4      yamt /*
    106  1.8.16.4      yamt  * Types of properties that can be set via userland.
    107  1.8.16.4      yamt  */
    108  1.8.16.4      yamt enum {
    109  1.8.16.4      yamt 	USERPROP_DESC 		= 0x0001,
    110  1.8.16.4      yamt 	USERPROP_BATTCAP	= 0x0002,
    111  1.8.16.4      yamt 	USERPROP_CRITMAX	= 0x0004,
    112  1.8.16.4      yamt 	USERPROP_CRITMIN	= 0x0008,
    113  1.8.16.4      yamt 	USERPROP_RFACT		= 0x0010
    114  1.8.16.3      yamt };
    115  1.8.16.3      yamt 
    116  1.8.16.3      yamt static prop_dictionary_t sme_propd;
    117  1.8.16.4      yamt static uint32_t sysmon_envsys_next_sensor_index = 0;
    118  1.8.16.3      yamt static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    119  1.8.16.3      yamt 
    120  1.8.16.3      yamt static void sysmon_envsys_release(struct sysmon_envsys *);
    121  1.8.16.3      yamt static void sysmon_envsys_destroy_plist(prop_array_t);
    122  1.8.16.3      yamt static int sme_register_sensorname(struct sysmon_envsys *, envsys_data_t *);
    123  1.8.16.4      yamt static void sme_remove_userprops(void);
    124       1.3  jdolecek 
    125  1.8.16.3      yamt /*
    126  1.8.16.3      yamt  * sysmon_envsys_init:
    127  1.8.16.3      yamt  *
    128  1.8.16.3      yamt  * 	+ Initialize global mutexes, dictionary and the linked lists.
    129  1.8.16.3      yamt  */
    130  1.8.16.3      yamt void
    131  1.8.16.3      yamt sysmon_envsys_init(void)
    132  1.8.16.3      yamt {
    133  1.8.16.3      yamt 	LIST_INIT(&sysmon_envsys_list);
    134  1.8.16.3      yamt 	LIST_INIT(&sme_events_list);
    135  1.8.16.4      yamt 	mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
    136  1.8.16.3      yamt 	mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
    137  1.8.16.4      yamt 	cv_init(&sme_cv, "smework");
    138  1.8.16.3      yamt 	sme_propd = prop_dictionary_create();
    139  1.8.16.3      yamt }
    140       1.1   thorpej 
    141       1.1   thorpej /*
    142       1.1   thorpej  * sysmonopen_envsys:
    143       1.1   thorpej  *
    144  1.8.16.3      yamt  *	+ Open the system monitor device.
    145       1.1   thorpej  */
    146       1.1   thorpej int
    147  1.8.16.3      yamt sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    148       1.1   thorpej {
    149  1.8.16.3      yamt 	return 0;
    150       1.1   thorpej }
    151       1.1   thorpej 
    152       1.1   thorpej /*
    153       1.1   thorpej  * sysmonclose_envsys:
    154       1.1   thorpej  *
    155  1.8.16.3      yamt  *	+ Close the system monitor device.
    156       1.1   thorpej  */
    157       1.1   thorpej int
    158  1.8.16.3      yamt sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    159       1.1   thorpej {
    160       1.3  jdolecek 	/* Nothing to do */
    161  1.8.16.3      yamt 	return 0;
    162       1.1   thorpej }
    163       1.1   thorpej 
    164       1.1   thorpej /*
    165       1.1   thorpej  * sysmonioctl_envsys:
    166       1.1   thorpej  *
    167  1.8.16.3      yamt  *	+ Perform a sysmon envsys control request.
    168       1.1   thorpej  */
    169       1.1   thorpej int
    170  1.8.16.3      yamt sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    171       1.1   thorpej {
    172  1.8.16.3      yamt 	struct sysmon_envsys *sme = NULL;
    173       1.1   thorpej 	int error = 0;
    174       1.1   thorpej 	u_int oidx;
    175       1.1   thorpej 
    176       1.1   thorpej 	switch (cmd) {
    177  1.8.16.3      yamt 	case ENVSYS_GETDICTIONARY:
    178  1.8.16.3      yamt 	    {
    179  1.8.16.3      yamt 		struct plistref *plist = (struct plistref *)data;
    180  1.8.16.4      yamt 
    181  1.8.16.3      yamt 		/*
    182  1.8.16.3      yamt 		 * Update all sysmon envsys devices dictionaries with
    183  1.8.16.3      yamt 		 * new data if it's different than we have currently
    184  1.8.16.3      yamt 		 * in the dictionary.
    185  1.8.16.3      yamt 		 */
    186  1.8.16.4      yamt 		mutex_enter(&sme_mtx);
    187  1.8.16.3      yamt 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    188  1.8.16.3      yamt 			sme->sme_flags |= SME_FLAG_BUSY;
    189  1.8.16.3      yamt 			error = sme_update_dictionary(sme);
    190  1.8.16.3      yamt 			if (error) {
    191  1.8.16.3      yamt 				DPRINTF(("%s: sme_update_dictionary, "
    192  1.8.16.3      yamt 				    "error=%d\n", __func__, error));
    193  1.8.16.3      yamt 				sme->sme_flags &= ~SME_FLAG_BUSY;
    194  1.8.16.4      yamt 				mutex_exit(&sme_mtx);
    195  1.8.16.3      yamt 				return error;
    196  1.8.16.3      yamt 			}
    197  1.8.16.3      yamt 			sme->sme_flags &= ~SME_FLAG_BUSY;
    198  1.8.16.3      yamt 		}
    199  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    200  1.8.16.3      yamt 		/*
    201  1.8.16.3      yamt 		 * Copy global dictionary to userland.
    202  1.8.16.3      yamt 		 */
    203  1.8.16.3      yamt 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    204       1.1   thorpej 		break;
    205  1.8.16.3      yamt 	    }
    206  1.8.16.3      yamt 	case ENVSYS_SETDICTIONARY:
    207       1.1   thorpej 	    {
    208  1.8.16.3      yamt 		const struct plistref *plist = (const struct plistref *)data;
    209  1.8.16.3      yamt 		prop_dictionary_t udict;
    210  1.8.16.4      yamt 		prop_object_iterator_t iter, iter2;
    211  1.8.16.4      yamt 		prop_object_t obj, obj2;
    212  1.8.16.4      yamt 		prop_array_t array_u, array_k;
    213  1.8.16.3      yamt 		const char *devname = NULL;
    214  1.8.16.3      yamt 
    215  1.8.16.3      yamt 		if ((flag & FWRITE) == 0)
    216  1.8.16.3      yamt 			return EPERM;
    217  1.8.16.3      yamt 
    218  1.8.16.3      yamt 		/*
    219  1.8.16.3      yamt 		 * Get dictionary from userland.
    220  1.8.16.3      yamt 		 */
    221  1.8.16.3      yamt 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    222  1.8.16.3      yamt 		if (error) {
    223  1.8.16.3      yamt 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    224  1.8.16.3      yamt 			    __func__, error));
    225  1.8.16.3      yamt 			break;
    226  1.8.16.3      yamt 		}
    227       1.4  explorer 
    228  1.8.16.4      yamt 		iter = prop_dictionary_iterator(udict);
    229  1.8.16.4      yamt 		if (!iter) {
    230  1.8.16.4      yamt 			prop_object_release(udict);
    231  1.8.16.4      yamt 			return ENOMEM;
    232  1.8.16.4      yamt 		}
    233  1.8.16.4      yamt 
    234  1.8.16.3      yamt 		/*
    235  1.8.16.4      yamt 		 * Iterate over the userland dictionary and process
    236  1.8.16.4      yamt 		 * the list of devices.
    237  1.8.16.3      yamt 		 */
    238  1.8.16.4      yamt 		while ((obj = prop_object_iterator_next(iter))) {
    239  1.8.16.4      yamt 			array_u = prop_dictionary_get_keysym(udict, obj);
    240  1.8.16.4      yamt 			if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
    241  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    242  1.8.16.4      yamt 				prop_object_release(udict);
    243  1.8.16.4      yamt 				return EINVAL;
    244  1.8.16.4      yamt 			}
    245  1.8.16.4      yamt 
    246  1.8.16.4      yamt 			devname = prop_dictionary_keysym_cstring_nocopy(obj);
    247  1.8.16.4      yamt 			DPRINTF(("%s: processing the '%s' array requests\n",
    248  1.8.16.4      yamt 			    __func__, devname));
    249  1.8.16.4      yamt 
    250  1.8.16.4      yamt 			/*
    251  1.8.16.4      yamt 			 * find the correct sme device.
    252  1.8.16.4      yamt 			 */
    253  1.8.16.4      yamt 			sme = sysmon_envsys_find(devname);
    254  1.8.16.4      yamt 			if (!sme) {
    255  1.8.16.4      yamt 				DPRINTF(("%s: NULL sme\n", __func__));
    256  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    257  1.8.16.4      yamt 				prop_object_release(udict);
    258  1.8.16.4      yamt 				return EINVAL;
    259  1.8.16.4      yamt 			}
    260  1.8.16.4      yamt 
    261  1.8.16.4      yamt 			/*
    262  1.8.16.4      yamt 			 * Find the correct array object with the string
    263  1.8.16.4      yamt 			 * supplied by the userland dictionary.
    264  1.8.16.4      yamt 			 */
    265  1.8.16.4      yamt 			array_k = prop_dictionary_get(sme_propd, devname);
    266  1.8.16.4      yamt 			if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
    267  1.8.16.4      yamt 				DPRINTF(("%s: array device failed\n",
    268  1.8.16.4      yamt 				    __func__));
    269  1.8.16.4      yamt 				sysmon_envsys_release(sme);
    270  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    271  1.8.16.4      yamt 				prop_object_release(udict);
    272  1.8.16.4      yamt 				return EINVAL;
    273  1.8.16.4      yamt 			}
    274  1.8.16.4      yamt 
    275  1.8.16.4      yamt 			iter2 = prop_array_iterator(array_u);
    276  1.8.16.4      yamt 			if (!iter2) {
    277  1.8.16.4      yamt 				sysmon_envsys_release(sme);
    278  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    279  1.8.16.4      yamt 				prop_object_release(udict);
    280  1.8.16.4      yamt 				return ENOMEM;
    281  1.8.16.4      yamt 			}
    282  1.8.16.4      yamt 
    283  1.8.16.4      yamt 			/*
    284  1.8.16.4      yamt 			 * Iterate over the array of dictionaries to
    285  1.8.16.4      yamt 			 * process the list of sensors.
    286  1.8.16.4      yamt 			 */
    287  1.8.16.4      yamt 			while ((obj2 = prop_object_iterator_next(iter2))) {
    288  1.8.16.4      yamt 				/* do the real work now */
    289  1.8.16.4      yamt 				error = sme_userset_dictionary(sme,
    290  1.8.16.4      yamt 							       obj2,
    291  1.8.16.4      yamt 							       array_k);
    292  1.8.16.4      yamt 				if (error) {
    293  1.8.16.4      yamt 					sysmon_envsys_release(sme);
    294  1.8.16.4      yamt 					prop_object_iterator_release(iter2);
    295  1.8.16.4      yamt 					prop_object_iterator_release(iter);
    296  1.8.16.4      yamt 					prop_object_release(udict);
    297  1.8.16.4      yamt 					return EINVAL;
    298  1.8.16.4      yamt 				}
    299  1.8.16.4      yamt 			}
    300  1.8.16.4      yamt 
    301  1.8.16.4      yamt 			sysmon_envsys_release(sme);
    302  1.8.16.4      yamt 			prop_object_iterator_release(iter2);
    303       1.4  explorer 		}
    304       1.1   thorpej 
    305  1.8.16.4      yamt 		prop_object_iterator_release(iter);
    306  1.8.16.4      yamt 		prop_object_release(udict);
    307  1.8.16.4      yamt 		break;
    308  1.8.16.4      yamt 	    }
    309  1.8.16.4      yamt 	case ENVSYS_REMOVEPROPS:
    310  1.8.16.4      yamt 	    {
    311  1.8.16.4      yamt 		const struct plistref *plist = (const struct plistref *)data;
    312  1.8.16.4      yamt 		prop_dictionary_t udict;
    313  1.8.16.4      yamt 		prop_object_t obj;
    314  1.8.16.3      yamt 
    315  1.8.16.4      yamt 		if ((flag & FWRITE) == 0)
    316  1.8.16.4      yamt 			return EPERM;
    317  1.8.16.4      yamt 
    318  1.8.16.4      yamt 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    319  1.8.16.4      yamt 		if (error) {
    320  1.8.16.4      yamt 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    321  1.8.16.4      yamt 			    __func__, error));
    322       1.1   thorpej 			break;
    323       1.1   thorpej 		}
    324  1.8.16.3      yamt 
    325  1.8.16.4      yamt 		obj = prop_dictionary_get(udict, "envsys-remove-props");
    326  1.8.16.4      yamt 		if (!obj || !prop_bool_true(obj)) {
    327  1.8.16.4      yamt 			DPRINTF(("%s: invalid 'envsys-remove-props'\n",
    328  1.8.16.4      yamt 			     __func__));
    329  1.8.16.4      yamt 			return EINVAL;
    330       1.1   thorpej 		}
    331  1.8.16.3      yamt 
    332  1.8.16.4      yamt 		sme_remove_userprops();
    333  1.8.16.4      yamt 
    334  1.8.16.3      yamt 		prop_object_release(udict);
    335       1.1   thorpej 		break;
    336       1.1   thorpej 	    }
    337  1.8.16.4      yamt 
    338  1.8.16.3      yamt 	    /*
    339  1.8.16.3      yamt 	     * Compatibility functions with the old interface, only
    340  1.8.16.3      yamt 	     * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
    341  1.8.16.3      yamt 	     * to make old applications work.
    342  1.8.16.3      yamt 	     */
    343       1.1   thorpej 	case ENVSYS_GTREDATA:
    344       1.1   thorpej 	    {
    345  1.8.16.3      yamt 		struct envsys_tre_data *tred = (void *)data;
    346  1.8.16.3      yamt 		envsys_data_t *edata = NULL;
    347       1.1   thorpej 
    348       1.1   thorpej 		tred->validflags = 0;
    349       1.1   thorpej 
    350  1.8.16.3      yamt 		sme = sysmon_envsys_find_40(tred->sensor);
    351  1.8.16.4      yamt 		if (!sme)
    352       1.1   thorpej 			break;
    353  1.8.16.3      yamt 
    354  1.8.16.4      yamt 		mutex_enter(&sme_mtx);
    355       1.1   thorpej 		oidx = tred->sensor;
    356       1.1   thorpej 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    357       1.1   thorpej 
    358  1.8.16.3      yamt 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    359  1.8.16.3      yamt 		    __func__, tred->sensor, oidx, sme->sme_name,
    360  1.8.16.3      yamt 		    sme->sme_nsensors));
    361  1.8.16.3      yamt 
    362  1.8.16.3      yamt 		edata = &sme->sme_sensor_data[tred->sensor];
    363  1.8.16.3      yamt 
    364  1.8.16.3      yamt 		if (tred->sensor < sme->sme_nsensors) {
    365  1.8.16.3      yamt 			if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    366  1.8.16.3      yamt 				error = (*sme->sme_gtredata)(sme, edata);
    367  1.8.16.3      yamt 				if (error) {
    368  1.8.16.3      yamt 					DPRINTF(("%s: sme_gtredata failed\n",
    369  1.8.16.3      yamt 				    	    __func__));
    370  1.8.16.4      yamt 					mutex_exit(&sme_mtx);
    371  1.8.16.3      yamt 					return error;
    372  1.8.16.3      yamt 				}
    373  1.8.16.3      yamt 			}
    374       1.1   thorpej 
    375  1.8.16.3      yamt 			/* copy required values to the old interface */
    376  1.8.16.3      yamt 			tred->sensor = edata->sensor;
    377  1.8.16.3      yamt 			tred->cur.data_us = edata->value_cur;
    378  1.8.16.3      yamt 			tred->cur.data_s = edata->value_cur;
    379  1.8.16.3      yamt 			tred->max.data_us = edata->value_max;
    380  1.8.16.3      yamt 			tred->max.data_s = edata->value_max;
    381  1.8.16.3      yamt 			tred->min.data_us = edata->value_min;
    382  1.8.16.3      yamt 			tred->min.data_s = edata->value_min;
    383  1.8.16.3      yamt 			tred->avg.data_us = edata->value_avg;
    384  1.8.16.3      yamt 			tred->avg.data_s = edata->value_avg;
    385  1.8.16.5      yamt 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    386  1.8.16.5      yamt 				tred->units = ENVSYS_INDICATOR;
    387  1.8.16.5      yamt 			else
    388  1.8.16.3      yamt 			tred->units = edata->units;
    389  1.8.16.3      yamt 
    390  1.8.16.3      yamt 			tred->validflags |= ENVSYS_FVALID;
    391  1.8.16.3      yamt 			tred->validflags |= ENVSYS_FCURVALID;
    392  1.8.16.3      yamt 
    393  1.8.16.3      yamt 			if (edata->flags & ENVSYS_FPERCENT) {
    394  1.8.16.3      yamt 				tred->validflags |= ENVSYS_FMAXVALID;
    395  1.8.16.3      yamt 				tred->validflags |= ENVSYS_FFRACVALID;
    396  1.8.16.3      yamt 			}
    397  1.8.16.3      yamt 
    398  1.8.16.3      yamt 			if (edata->state == ENVSYS_SINVALID ||
    399  1.8.16.3      yamt 			    edata->flags & ENVSYS_FNOTVALID) {
    400  1.8.16.3      yamt 				tred->validflags &= ~ENVSYS_FCURVALID;
    401  1.8.16.3      yamt 				tred->cur.data_us = tred->cur.data_s = 0;
    402  1.8.16.3      yamt 			}
    403  1.8.16.3      yamt 
    404  1.8.16.3      yamt 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    405  1.8.16.3      yamt 			    __func__, edata->desc, tred->cur.data_s));
    406  1.8.16.3      yamt 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    407  1.8.16.3      yamt 			    " tred->sensor=%d\n", __func__, tred->validflags,
    408  1.8.16.3      yamt 			    tred->units, tred->sensor));
    409       1.1   thorpej 		}
    410  1.8.16.3      yamt 		tred->sensor = oidx;
    411  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    412  1.8.16.3      yamt 
    413       1.1   thorpej 		break;
    414       1.1   thorpej 	    }
    415       1.1   thorpej 	case ENVSYS_GTREINFO:
    416       1.1   thorpej 	    {
    417  1.8.16.3      yamt 		struct envsys_basic_info *binfo = (void *)data;
    418  1.8.16.3      yamt 		envsys_data_t *edata = NULL;
    419       1.1   thorpej 
    420       1.1   thorpej 		binfo->validflags = 0;
    421       1.1   thorpej 
    422  1.8.16.3      yamt 		sme = sysmon_envsys_find_40(binfo->sensor);
    423  1.8.16.4      yamt 		if (!sme)
    424       1.1   thorpej 			break;
    425  1.8.16.3      yamt 
    426  1.8.16.4      yamt 		mutex_enter(&sme_mtx);
    427       1.1   thorpej 		oidx = binfo->sensor;
    428       1.1   thorpej 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    429  1.8.16.3      yamt 
    430  1.8.16.3      yamt 		edata = &sme->sme_sensor_data[binfo->sensor];
    431  1.8.16.3      yamt 
    432  1.8.16.3      yamt 		binfo->validflags |= ENVSYS_FVALID;
    433  1.8.16.3      yamt 
    434  1.8.16.3      yamt 		if (binfo->sensor < sme->sme_nsensors) {
    435  1.8.16.5      yamt 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    436  1.8.16.5      yamt 				binfo->units = ENVSYS_INDICATOR;
    437  1.8.16.5      yamt 			else
    438  1.8.16.3      yamt 			binfo->units = edata->units;
    439  1.8.16.5      yamt 
    440  1.8.16.5      yamt 			/*
    441  1.8.16.5      yamt 			 * previously, the ACPI sensor names included the
    442  1.8.16.5      yamt 			 * device name. Include that in compatibility code.
    443  1.8.16.5      yamt 			 */
    444  1.8.16.5      yamt 			if (strncmp(sme->sme_name, "acpi", 4) == 0)
    445  1.8.16.5      yamt 				(void)snprintf(binfo->desc, sizeof(binfo->desc),
    446  1.8.16.5      yamt 				    "%s %s", sme->sme_name, edata->desc);
    447  1.8.16.5      yamt 			else
    448  1.8.16.5      yamt 				(void)strlcpy(binfo->desc, edata->desc,
    449  1.8.16.5      yamt 				    sizeof(binfo->desc));
    450  1.8.16.3      yamt 		}
    451  1.8.16.3      yamt 
    452  1.8.16.3      yamt 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    453  1.8.16.3      yamt 		    __func__, binfo->units, binfo->validflags));
    454  1.8.16.3      yamt 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    455  1.8.16.3      yamt 		    __func__, binfo->desc, binfo->sensor));
    456  1.8.16.3      yamt 
    457       1.1   thorpej 		binfo->sensor = oidx;
    458  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    459  1.8.16.3      yamt 
    460       1.1   thorpej 		break;
    461       1.1   thorpej 	    }
    462       1.1   thorpej 	default:
    463       1.1   thorpej 		error = ENOTTY;
    464  1.8.16.3      yamt 		break;
    465       1.1   thorpej 	}
    466       1.1   thorpej 
    467  1.8.16.3      yamt 	return error;
    468       1.1   thorpej }
    469       1.1   thorpej 
    470       1.1   thorpej /*
    471       1.1   thorpej  * sysmon_envsys_register:
    472       1.1   thorpej  *
    473  1.8.16.3      yamt  *	+ Register a sysmon envsys device.
    474  1.8.16.3      yamt  *	+ Create array of dictionaries for a device.
    475       1.1   thorpej  */
    476       1.1   thorpej int
    477       1.1   thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
    478       1.1   thorpej {
    479  1.8.16.4      yamt 	struct sme_evdrv {
    480  1.8.16.4      yamt 		SLIST_ENTRY(sme_evdrv) evdrv_head;
    481  1.8.16.4      yamt 		sme_event_drv_t *evdrv;
    482  1.8.16.4      yamt 	};
    483  1.8.16.4      yamt 	SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
    484  1.8.16.4      yamt 	struct sme_evdrv *sme_evdrv = NULL;
    485  1.8.16.3      yamt 	struct sysmon_envsys *lsme;
    486  1.8.16.3      yamt 	prop_dictionary_t dict;
    487  1.8.16.3      yamt 	prop_array_t array;
    488  1.8.16.3      yamt 	envsys_data_t *edata = NULL;
    489  1.8.16.3      yamt 	int i, error = 0;
    490  1.8.16.3      yamt 
    491  1.8.16.3      yamt 	KASSERT(sme != NULL);
    492  1.8.16.3      yamt 	KASSERT(sme->sme_name != NULL);
    493  1.8.16.3      yamt 	KASSERT(sme->sme_sensor_data != NULL);
    494       1.1   thorpej 
    495  1.8.16.3      yamt 	/*
    496  1.8.16.3      yamt 	 * sme_nsensors is mandatory...
    497  1.8.16.3      yamt 	 */
    498  1.8.16.3      yamt 	if (!sme->sme_nsensors)
    499  1.8.16.3      yamt 		return EINVAL;
    500  1.8.16.3      yamt 
    501  1.8.16.3      yamt 	/*
    502  1.8.16.3      yamt 	 * sanity check: if SME_DISABLE_GTREDATA is not set,
    503  1.8.16.3      yamt 	 * the sme_gtredata function callback must be non NULL.
    504  1.8.16.3      yamt 	 */
    505  1.8.16.3      yamt 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    506  1.8.16.4      yamt 		if (!sme->sme_gtredata)
    507  1.8.16.3      yamt 			return EINVAL;
    508  1.8.16.3      yamt 	}
    509  1.8.16.3      yamt 
    510  1.8.16.4      yamt 
    511  1.8.16.3      yamt 	/* create the sysmon envsys device array. */
    512  1.8.16.3      yamt 	array = prop_array_create();
    513  1.8.16.4      yamt 	if (!array)
    514  1.8.16.3      yamt 		return ENOMEM;
    515  1.8.16.3      yamt 
    516  1.8.16.3      yamt 	/*
    517  1.8.16.3      yamt 	 * Initialize the singly linked list for sensor descriptions.
    518  1.8.16.3      yamt 	 */
    519  1.8.16.3      yamt 	SLIST_INIT(&sme->sme_names_list);
    520  1.8.16.4      yamt 
    521  1.8.16.4      yamt 	/*
    522  1.8.16.4      yamt 	 * Initialize the singly linked list for driver events.
    523  1.8.16.4      yamt 	 */
    524  1.8.16.4      yamt 	SLIST_INIT(&sme_evdrv_list);
    525  1.8.16.3      yamt 	/*
    526  1.8.16.3      yamt 	 * Iterate over all sensors and create a dictionary per sensor,
    527  1.8.16.3      yamt 	 * checking firstly if sensor description is unique.
    528  1.8.16.3      yamt 	 */
    529  1.8.16.3      yamt 	for (i = 0; i < sme->sme_nsensors; i++) {
    530  1.8.16.3      yamt 		edata = &sme->sme_sensor_data[i];
    531  1.8.16.3      yamt 		/*
    532  1.8.16.3      yamt 		 * Check if sensor description is unique.
    533  1.8.16.3      yamt 		 */
    534  1.8.16.3      yamt 		if (sme_register_sensorname(sme, edata))
    535  1.8.16.3      yamt 			continue;
    536  1.8.16.3      yamt 
    537  1.8.16.3      yamt 		dict = prop_dictionary_create();
    538  1.8.16.4      yamt 		if (!dict) {
    539  1.8.16.3      yamt 			error = ENOMEM;
    540  1.8.16.3      yamt 			goto out2;
    541  1.8.16.3      yamt 		}
    542       1.1   thorpej 
    543  1.8.16.3      yamt 		/*
    544  1.8.16.3      yamt 		 * Create all objects in sensor's dictionary.
    545  1.8.16.3      yamt 		 */
    546  1.8.16.4      yamt 		sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
    547  1.8.16.4      yamt 		sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
    548  1.8.16.4      yamt 					array, dict, edata);
    549  1.8.16.4      yamt 		if (sme_evdrv->evdrv)
    550  1.8.16.4      yamt 			SLIST_INSERT_HEAD(&sme_evdrv_list,
    551  1.8.16.4      yamt 					  sme_evdrv, evdrv_head);
    552  1.8.16.3      yamt 	}
    553  1.8.16.3      yamt 
    554  1.8.16.3      yamt 	/*
    555  1.8.16.3      yamt 	 * Check if requested sysmon_envsys device is valid
    556  1.8.16.3      yamt 	 * and does not exist already in the list.
    557  1.8.16.3      yamt 	 */
    558  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    559  1.8.16.3      yamt 	sme->sme_flags |= SME_FLAG_BUSY;
    560  1.8.16.3      yamt 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    561  1.8.16.3      yamt 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    562  1.8.16.3      yamt 		       error = EEXIST;
    563  1.8.16.3      yamt 		       goto out;
    564  1.8.16.3      yamt 	       }
    565  1.8.16.3      yamt 	}
    566  1.8.16.4      yamt 
    567  1.8.16.3      yamt 	/*
    568  1.8.16.3      yamt 	 * If the array does not contain any object (sensor), there's
    569  1.8.16.3      yamt 	 * no need to attach the driver.
    570  1.8.16.3      yamt 	 */
    571  1.8.16.3      yamt 	if (prop_array_count(array) == 0) {
    572       1.1   thorpej 		error = EINVAL;
    573  1.8.16.3      yamt 		DPRINTF(("%s: empty array for '%s'\n", __func__,
    574  1.8.16.3      yamt 		    sme->sme_name));
    575       1.1   thorpej 		goto out;
    576       1.1   thorpej 	}
    577  1.8.16.3      yamt 	/*
    578  1.8.16.3      yamt 	 * Add the array into the global dictionary for the driver.
    579  1.8.16.3      yamt 	 *
    580  1.8.16.3      yamt 	 * <dict>
    581  1.8.16.3      yamt 	 * 	<key>foo0</key>
    582  1.8.16.3      yamt 	 * 	<array>
    583  1.8.16.3      yamt 	 * 		...
    584  1.8.16.3      yamt 	 */
    585  1.8.16.3      yamt 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    586  1.8.16.3      yamt 		error = EINVAL;
    587  1.8.16.3      yamt 		DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
    588  1.8.16.3      yamt 		    sme->sme_name));
    589  1.8.16.3      yamt 		goto out;
    590  1.8.16.3      yamt 	}
    591  1.8.16.3      yamt 	/*
    592  1.8.16.3      yamt 	 * Add the device into the list.
    593  1.8.16.3      yamt 	 */
    594  1.8.16.4      yamt 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    595       1.1   thorpej 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    596       1.1   thorpej 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    597  1.8.16.3      yamt out:
    598  1.8.16.3      yamt 	sme->sme_uniqsensors = 0;
    599  1.8.16.4      yamt 	sme->sme_flags &= ~SME_FLAG_BUSY;
    600  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    601  1.8.16.4      yamt 
    602  1.8.16.4      yamt 	if (error == 0) {
    603  1.8.16.4      yamt 		i = 0;
    604  1.8.16.4      yamt 		SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
    605  1.8.16.4      yamt 			if (i == 0)
    606  1.8.16.4      yamt 				sysmon_task_queue_init();
    607  1.8.16.4      yamt 			sysmon_task_queue_sched(0,
    608  1.8.16.4      yamt 			    sme_event_drvadd, sme_evdrv->evdrv);
    609  1.8.16.4      yamt 		}
    610  1.8.16.4      yamt 		DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
    611  1.8.16.4      yamt 		    __func__, sme->sme_name, sme->sme_nsensors));
    612  1.8.16.4      yamt 	}
    613  1.8.16.4      yamt 
    614  1.8.16.3      yamt out2:
    615  1.8.16.4      yamt 	while (!SLIST_EMPTY(&sme_evdrv_list)) {
    616  1.8.16.4      yamt 		sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
    617  1.8.16.4      yamt 		SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
    618  1.8.16.4      yamt 		kmem_free(sme_evdrv, sizeof(*sme_evdrv));
    619  1.8.16.4      yamt 	}
    620  1.8.16.4      yamt 	if (!error)
    621  1.8.16.4      yamt 		return 0;
    622  1.8.16.4      yamt 
    623  1.8.16.3      yamt 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    624  1.8.16.3      yamt 	    sme->sme_name, error));
    625  1.8.16.4      yamt 	if (error != EEXIST) {
    626  1.8.16.4      yamt 		mutex_enter(&sme_mtx);
    627  1.8.16.4      yamt 		sme_event_unregister_all(sme->sme_name);
    628  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    629  1.8.16.4      yamt 	}
    630  1.8.16.3      yamt 	sysmon_envsys_destroy_plist(array);
    631  1.8.16.3      yamt 	return error;
    632  1.8.16.3      yamt }
    633  1.8.16.3      yamt 
    634  1.8.16.3      yamt /*
    635  1.8.16.3      yamt  * sysmon_envsys_destroy_plist:
    636  1.8.16.3      yamt  *
    637  1.8.16.3      yamt  * 	+ Remove all objects from the array of dictionaries that is
    638  1.8.16.3      yamt  * 	  created in a sysmon envsys device.
    639  1.8.16.3      yamt  */
    640  1.8.16.3      yamt static void
    641  1.8.16.3      yamt sysmon_envsys_destroy_plist(prop_array_t array)
    642  1.8.16.3      yamt {
    643  1.8.16.3      yamt 	prop_object_iterator_t iter, iter2;
    644  1.8.16.4      yamt 	prop_dictionary_t dict;
    645  1.8.16.3      yamt 	prop_object_t obj;
    646  1.8.16.3      yamt 
    647  1.8.16.3      yamt 	KASSERT(array != NULL);
    648  1.8.16.3      yamt 
    649  1.8.16.4      yamt 	DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
    650  1.8.16.4      yamt 	    prop_array_count(array)));
    651  1.8.16.4      yamt 
    652  1.8.16.3      yamt 	iter = prop_array_iterator(array);
    653  1.8.16.4      yamt 	if (!iter)
    654  1.8.16.3      yamt 		return;
    655  1.8.16.3      yamt 
    656  1.8.16.4      yamt 	while ((dict = prop_object_iterator_next(iter))) {
    657  1.8.16.4      yamt 		KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
    658  1.8.16.3      yamt 		iter2 = prop_dictionary_iterator(dict);
    659  1.8.16.4      yamt 		if (!iter2)
    660  1.8.16.4      yamt 			goto out;
    661  1.8.16.4      yamt 		DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
    662  1.8.16.4      yamt 		while ((obj = prop_object_iterator_next(iter2)) != NULL) {
    663  1.8.16.4      yamt 			DPRINTFOBJ(("%s: obj=%s\n", __func__,
    664  1.8.16.4      yamt 			    prop_dictionary_keysym_cstring_nocopy(obj)));
    665  1.8.16.4      yamt 			prop_dictionary_remove(dict,
    666  1.8.16.4      yamt 			    prop_dictionary_keysym_cstring_nocopy(obj));
    667  1.8.16.4      yamt 			prop_object_iterator_reset(iter2);
    668  1.8.16.4      yamt 		}
    669  1.8.16.4      yamt 		prop_object_iterator_release(iter2);
    670  1.8.16.4      yamt 		DPRINTFOBJ(("%s: objects in dictionary:%d\n",
    671  1.8.16.4      yamt 		    __func__, prop_dictionary_count(dict)));
    672  1.8.16.3      yamt 		prop_object_release(dict);
    673  1.8.16.3      yamt 	}
    674  1.8.16.3      yamt 
    675  1.8.16.4      yamt out:
    676  1.8.16.3      yamt 	prop_object_iterator_release(iter);
    677  1.8.16.4      yamt 	prop_object_release(array);
    678       1.1   thorpej }
    679       1.1   thorpej 
    680       1.1   thorpej /*
    681       1.1   thorpej  * sysmon_envsys_unregister:
    682       1.1   thorpej  *
    683  1.8.16.3      yamt  *	+ Unregister a sysmon envsys device.
    684       1.1   thorpej  */
    685       1.1   thorpej void
    686       1.1   thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
    687       1.1   thorpej {
    688  1.8.16.3      yamt 	struct sme_sensor_names *snames;
    689  1.8.16.3      yamt 	prop_array_t array;
    690  1.8.16.3      yamt 
    691  1.8.16.3      yamt 	KASSERT(sme != NULL);
    692       1.1   thorpej 
    693  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    694       1.7      yamt 	while (sme->sme_flags & SME_FLAG_BUSY) {
    695       1.8      yamt 		sme->sme_flags |= SME_FLAG_WANTED;
    696  1.8.16.4      yamt 		cv_wait(&sme_cv, &sme_mtx);
    697       1.7      yamt 	}
    698  1.8.16.4      yamt 	sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
    699  1.8.16.3      yamt 	/*
    700  1.8.16.3      yamt 	 * Remove all sensor descriptions from the singly linked list.
    701  1.8.16.3      yamt 	 */
    702  1.8.16.3      yamt 	while (!SLIST_EMPTY(&sme->sme_names_list)) {
    703  1.8.16.3      yamt 		snames = SLIST_FIRST(&sme->sme_names_list);
    704  1.8.16.3      yamt 		SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
    705  1.8.16.3      yamt 		kmem_free(snames, sizeof(*snames));
    706  1.8.16.3      yamt 	}
    707  1.8.16.3      yamt 	/*
    708  1.8.16.3      yamt 	 * Unregister all events associated with this device.
    709  1.8.16.3      yamt 	 */
    710  1.8.16.3      yamt 	sme_event_unregister_all(sme->sme_name);
    711  1.8.16.4      yamt 	LIST_REMOVE(sme, sme_list);
    712  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    713  1.8.16.3      yamt 	/*
    714  1.8.16.3      yamt 	 * Remove the device (and all its objects) from the global dictionary.
    715  1.8.16.3      yamt 	 */
    716  1.8.16.3      yamt 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    717  1.8.16.4      yamt 	if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
    718  1.8.16.3      yamt 		prop_dictionary_remove(sme_propd, sme->sme_name);
    719  1.8.16.4      yamt 		sysmon_envsys_destroy_plist(array);
    720  1.8.16.3      yamt 	}
    721       1.1   thorpej }
    722       1.1   thorpej 
    723       1.1   thorpej /*
    724       1.1   thorpej  * sysmon_envsys_find:
    725       1.1   thorpej  *
    726  1.8.16.3      yamt  *	+ Find a sysmon envsys device.
    727       1.1   thorpej  */
    728       1.1   thorpej struct sysmon_envsys *
    729  1.8.16.3      yamt sysmon_envsys_find(const char *name)
    730       1.1   thorpej {
    731       1.1   thorpej 	struct sysmon_envsys *sme;
    732       1.1   thorpej 
    733  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    734       1.7      yamt again:
    735  1.8.16.4      yamt 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
    736       1.1   thorpej 	     sme = LIST_NEXT(sme, sme_list)) {
    737  1.8.16.3      yamt 			if (strcmp(sme->sme_name, name) == 0) {
    738  1.8.16.3      yamt 				if (sme->sme_flags & SME_FLAG_BUSY) {
    739  1.8.16.3      yamt 					sme->sme_flags |= SME_FLAG_WANTED;
    740  1.8.16.4      yamt 					cv_wait(&sme_cv, &sme_mtx);
    741  1.8.16.3      yamt 					goto again;
    742  1.8.16.3      yamt 				}
    743  1.8.16.3      yamt 				sme->sme_flags |= SME_FLAG_BUSY;
    744  1.8.16.3      yamt 				break;
    745       1.7      yamt 			}
    746       1.1   thorpej 	}
    747  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    748       1.7      yamt 	return sme;
    749       1.1   thorpej }
    750       1.1   thorpej 
    751       1.1   thorpej /*
    752       1.1   thorpej  * sysmon_envsys_release:
    753       1.1   thorpej  *
    754  1.8.16.3      yamt  * 	+ Release a sysmon envsys device.
    755       1.1   thorpej  */
    756       1.1   thorpej void
    757       1.1   thorpej sysmon_envsys_release(struct sysmon_envsys *sme)
    758       1.1   thorpej {
    759  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    760       1.8      yamt 	if (sme->sme_flags & SME_FLAG_WANTED)
    761  1.8.16.4      yamt 		cv_broadcast(&sme_cv);
    762       1.8      yamt 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    763  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    764  1.8.16.3      yamt }
    765  1.8.16.3      yamt 
    766  1.8.16.3      yamt /* compatibility function */
    767  1.8.16.3      yamt struct sysmon_envsys *
    768  1.8.16.3      yamt sysmon_envsys_find_40(u_int idx)
    769  1.8.16.3      yamt {
    770  1.8.16.3      yamt 	struct sysmon_envsys *sme;
    771  1.8.16.3      yamt 
    772  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    773  1.8.16.4      yamt 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
    774  1.8.16.3      yamt 	     sme = LIST_NEXT(sme, sme_list)) {
    775  1.8.16.3      yamt 		if (idx >= sme->sme_fsensor &&
    776  1.8.16.3      yamt 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    777  1.8.16.3      yamt 			break;
    778  1.8.16.3      yamt 	}
    779  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    780  1.8.16.3      yamt 	return sme;
    781  1.8.16.3      yamt }
    782  1.8.16.3      yamt 
    783  1.8.16.3      yamt /*
    784  1.8.16.4      yamt  * sme_sensor_dictionary_get:
    785  1.8.16.4      yamt  *
    786  1.8.16.4      yamt  * 	+ Returns a dictionary of a device specified by 'index'.
    787  1.8.16.4      yamt  */
    788  1.8.16.4      yamt prop_dictionary_t
    789  1.8.16.4      yamt sme_sensor_dictionary_get(prop_array_t array, const char *index)
    790  1.8.16.4      yamt {
    791  1.8.16.4      yamt 	prop_object_iterator_t iter;
    792  1.8.16.4      yamt 	prop_dictionary_t dict;
    793  1.8.16.4      yamt 	prop_object_t obj;
    794  1.8.16.4      yamt 
    795  1.8.16.4      yamt 	KASSERT(array != NULL || index != NULL);
    796  1.8.16.4      yamt 
    797  1.8.16.4      yamt 	iter = prop_array_iterator(array);
    798  1.8.16.4      yamt 	if (!iter)
    799  1.8.16.4      yamt 		return NULL;
    800  1.8.16.4      yamt 
    801  1.8.16.4      yamt 	while ((dict = prop_object_iterator_next(iter))) {
    802  1.8.16.4      yamt 		obj = prop_dictionary_get(dict, "index");
    803  1.8.16.4      yamt 		if (prop_string_equals_cstring(obj, index))
    804  1.8.16.4      yamt 			break;
    805  1.8.16.4      yamt 	}
    806  1.8.16.4      yamt 
    807  1.8.16.4      yamt 	prop_object_iterator_release(iter);
    808  1.8.16.4      yamt 	return dict;
    809  1.8.16.4      yamt }
    810  1.8.16.4      yamt 
    811  1.8.16.4      yamt /*
    812  1.8.16.4      yamt  * sme_remove_userprops:
    813  1.8.16.4      yamt  *
    814  1.8.16.4      yamt  * 	+ Remove all properties from all devices that were set by
    815  1.8.16.4      yamt  * 	  the userland.
    816  1.8.16.4      yamt  */
    817  1.8.16.4      yamt static void
    818  1.8.16.4      yamt sme_remove_userprops(void)
    819  1.8.16.4      yamt {
    820  1.8.16.4      yamt 	struct sysmon_envsys *sme;
    821  1.8.16.4      yamt 	prop_array_t array;
    822  1.8.16.4      yamt 	prop_dictionary_t sdict;
    823  1.8.16.4      yamt 	envsys_data_t *edata = NULL;
    824  1.8.16.4      yamt 	char tmp[ENVSYS_DESCLEN];
    825  1.8.16.4      yamt 	int i, ptype;
    826  1.8.16.4      yamt 
    827  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    828  1.8.16.4      yamt 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    829  1.8.16.4      yamt 		sme->sme_flags |= SME_FLAG_BUSY;
    830  1.8.16.4      yamt 		array = prop_dictionary_get(sme_propd, sme->sme_name);
    831  1.8.16.4      yamt 
    832  1.8.16.4      yamt 		for (i = 0; i < sme->sme_nsensors; i++) {
    833  1.8.16.4      yamt 			edata = &sme->sme_sensor_data[i];
    834  1.8.16.4      yamt 			(void)snprintf(tmp, sizeof(tmp), "sensor%d",
    835  1.8.16.4      yamt 				       edata->sensor);
    836  1.8.16.4      yamt 			sdict = sme_sensor_dictionary_get(array, tmp);
    837  1.8.16.4      yamt 
    838  1.8.16.4      yamt 			if (edata->upropset & USERPROP_BATTCAP) {
    839  1.8.16.4      yamt 				prop_dictionary_remove(sdict,
    840  1.8.16.4      yamt 				    "critical-capacity");
    841  1.8.16.4      yamt 				ptype = PENVSYS_EVENT_BATT_USERCAP;
    842  1.8.16.4      yamt 				sme_event_unregister(edata->desc, ptype);
    843  1.8.16.4      yamt 			}
    844  1.8.16.4      yamt 
    845  1.8.16.4      yamt 			if (edata->upropset & USERPROP_CRITMAX) {
    846  1.8.16.4      yamt 				prop_dictionary_remove(sdict,
    847  1.8.16.4      yamt 				    "critical-max");
    848  1.8.16.4      yamt 				ptype = PENVSYS_EVENT_USER_CRITMAX;
    849  1.8.16.4      yamt 				sme_event_unregister(edata->desc, ptype);
    850  1.8.16.4      yamt 			}
    851  1.8.16.4      yamt 
    852  1.8.16.4      yamt 			if (edata->upropset & USERPROP_CRITMIN) {
    853  1.8.16.4      yamt 				prop_dictionary_remove(sdict,
    854  1.8.16.4      yamt 				    "critical-min");
    855  1.8.16.4      yamt 				ptype = PENVSYS_EVENT_USER_CRITMIN;
    856  1.8.16.4      yamt 				sme_event_unregister(edata->desc, ptype);
    857  1.8.16.4      yamt 			}
    858  1.8.16.4      yamt 
    859  1.8.16.4      yamt 			if (edata->upropset & USERPROP_RFACT) {
    860  1.8.16.4      yamt 				(void)sme_sensor_upint32(sdict, "rfact", 0);
    861  1.8.16.4      yamt 				edata->rfact = 0;
    862  1.8.16.4      yamt 			}
    863  1.8.16.4      yamt 
    864  1.8.16.4      yamt 			if (edata->upropset & USERPROP_DESC)
    865  1.8.16.4      yamt 				(void)sme_sensor_upstring(sdict,
    866  1.8.16.4      yamt 			  	    "description", edata->desc);
    867  1.8.16.4      yamt 		}
    868  1.8.16.4      yamt 
    869  1.8.16.4      yamt 		if (edata->upropset)
    870  1.8.16.4      yamt 			edata->upropset = 0;
    871  1.8.16.4      yamt 
    872  1.8.16.4      yamt 		sme->sme_flags &= ~SME_FLAG_BUSY;
    873  1.8.16.4      yamt 	}
    874  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    875  1.8.16.4      yamt }
    876  1.8.16.4      yamt /*
    877  1.8.16.3      yamt  * sme_register_sensorname:
    878  1.8.16.3      yamt  *
    879  1.8.16.3      yamt  * 	+ Register a sensor description into the list maintained per device.
    880  1.8.16.3      yamt  */
    881  1.8.16.3      yamt static int
    882  1.8.16.3      yamt sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
    883  1.8.16.3      yamt {
    884  1.8.16.3      yamt 	struct sme_sensor_names *snames, *snames2 = NULL;
    885  1.8.16.3      yamt 
    886  1.8.16.3      yamt 	KASSERT(edata != NULL);
    887  1.8.16.3      yamt 
    888  1.8.16.3      yamt 	SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
    889  1.8.16.3      yamt 		/*
    890  1.8.16.3      yamt 		 * Match sensors with empty and duplicate description.
    891  1.8.16.3      yamt 		 */
    892  1.8.16.3      yamt 		if (strlen(edata->desc) == 0 ||
    893  1.8.16.3      yamt 		    strcmp(snames2->desc, edata->desc) == 0)
    894  1.8.16.3      yamt 			if (snames2->assigned) {
    895  1.8.16.3      yamt 				edata->flags |= ENVSYS_FNOTVALID;
    896  1.8.16.4      yamt 				DPRINTF(("%s: wrong sensor name='%s'\n",
    897  1.8.16.4      yamt 				    sme->sme_name, edata->desc));
    898  1.8.16.3      yamt 				return EEXIST;
    899  1.8.16.3      yamt 			}
    900  1.8.16.3      yamt 	}
    901  1.8.16.3      yamt 
    902  1.8.16.3      yamt 	snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
    903  1.8.16.4      yamt 	if (!snames)
    904  1.8.16.3      yamt 		return ENOMEM;
    905  1.8.16.3      yamt 
    906  1.8.16.3      yamt 	snames->assigned = true;
    907  1.8.16.3      yamt 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
    908  1.8.16.4      yamt 	DPRINTF(("%s: registering sensor name='%s'\n",
    909  1.8.16.4      yamt 	    sme->sme_name, edata->desc));
    910  1.8.16.3      yamt 	SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
    911  1.8.16.3      yamt 	sme->sme_uniqsensors++;
    912  1.8.16.3      yamt 
    913  1.8.16.3      yamt 	return 0;
    914  1.8.16.3      yamt }
    915  1.8.16.3      yamt 
    916  1.8.16.3      yamt /*
    917  1.8.16.3      yamt  * sme_add_sensor_dictionary:
    918  1.8.16.3      yamt  *
    919  1.8.16.3      yamt  * 	+ Add the objects into the dictionary.
    920  1.8.16.3      yamt  */
    921  1.8.16.4      yamt sme_event_drv_t *
    922  1.8.16.3      yamt sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    923  1.8.16.3      yamt 		    	  prop_dictionary_t dict, envsys_data_t *edata)
    924  1.8.16.3      yamt {
    925  1.8.16.4      yamt 	const struct sme_description_table *sdt, *sdt_units;
    926  1.8.16.3      yamt 	sme_event_drv_t *sme_evdrv_t = NULL;
    927  1.8.16.3      yamt 	int i, j;
    928  1.8.16.4      yamt 	char indexstr[ENVSYS_DESCLEN];
    929  1.8.16.3      yamt 
    930  1.8.16.3      yamt 	i = j = 0;
    931  1.8.16.3      yamt 
    932  1.8.16.3      yamt 	/* find the correct unit for this sensor. */
    933  1.8.16.4      yamt 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
    934  1.8.16.4      yamt 	for (i = 0; sdt_units[i].type != -1; i++)
    935  1.8.16.4      yamt 		if (sdt_units[i].type == edata->units)
    936  1.8.16.3      yamt 			break;
    937  1.8.16.3      yamt 
    938  1.8.16.4      yamt 	if (strcmp(sdt_units[i].desc, "unknown") == 0) {
    939  1.8.16.3      yamt 		DPRINTF(("%s: invalid units type for sensor=%d\n",
    940  1.8.16.3      yamt 		    __func__, edata->sensor));
    941  1.8.16.3      yamt 		goto invalidate_sensor;
    942  1.8.16.3      yamt 	}
    943  1.8.16.3      yamt 
    944  1.8.16.3      yamt 	/*
    945  1.8.16.4      yamt 	 * Add the index sensor string.
    946  1.8.16.4      yamt 	 *
    947  1.8.16.4      yamt 	 * 		...
    948  1.8.16.4      yamt 	 * 		<key>index</key>
    949  1.8.16.4      yamt 	 * 		<string>sensor0</string>
    950  1.8.16.4      yamt 	 * 		...
    951  1.8.16.4      yamt 	 */
    952  1.8.16.4      yamt 	(void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
    953  1.8.16.4      yamt 	if (sme_sensor_upstring(dict, "index", indexstr))
    954  1.8.16.4      yamt 		goto invalidate_sensor;
    955  1.8.16.4      yamt 
    956  1.8.16.4      yamt 	/*
    957  1.8.16.3      yamt 	 * 		...
    958  1.8.16.3      yamt 	 * 		<key>type</key>
    959  1.8.16.3      yamt 	 * 		<string>foo</string>
    960  1.8.16.3      yamt 	 * 		<key>description</key>
    961  1.8.16.3      yamt 	 * 		<string>blah blah</string>
    962  1.8.16.3      yamt 	 * 		...
    963  1.8.16.3      yamt 	 */
    964  1.8.16.4      yamt 	if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
    965  1.8.16.3      yamt 		goto invalidate_sensor;
    966  1.8.16.3      yamt 
    967  1.8.16.3      yamt 	if (strlen(edata->desc) == 0) {
    968  1.8.16.3      yamt 		DPRINTF(("%s: invalid description for sensor=%d\n",
    969  1.8.16.3      yamt 		    __func__, edata->sensor));
    970  1.8.16.3      yamt 		goto invalidate_sensor;
    971  1.8.16.3      yamt 	}
    972  1.8.16.3      yamt 
    973  1.8.16.3      yamt 	if (sme_sensor_upstring(dict, "description", edata->desc))
    974  1.8.16.3      yamt 		goto invalidate_sensor;
    975  1.8.16.3      yamt 
    976  1.8.16.3      yamt 	/*
    977  1.8.16.3      yamt 	 * Add sensor's state description.
    978  1.8.16.3      yamt 	 *
    979  1.8.16.3      yamt 	 * 		...
    980  1.8.16.3      yamt 	 * 		<key>state</key>
    981  1.8.16.3      yamt 	 * 		<string>valid</string>
    982  1.8.16.3      yamt 	 * 		...
    983  1.8.16.3      yamt 	 */
    984  1.8.16.4      yamt 	sdt = sme_get_description_table(SME_DESC_STATES);
    985  1.8.16.4      yamt 	for (j = 0; sdt[j].type != -1; j++)
    986  1.8.16.4      yamt 		if (sdt[j].type == edata->state)
    987  1.8.16.3      yamt 			break;
    988  1.8.16.3      yamt 
    989  1.8.16.4      yamt 	if (strcmp(sdt[j].desc, "unknown") == 0) {
    990  1.8.16.3      yamt 		DPRINTF(("%s: invalid state for sensor=%d\n",
    991  1.8.16.3      yamt 		    __func__, edata->sensor));
    992  1.8.16.3      yamt 		goto invalidate_sensor;
    993  1.8.16.3      yamt 	}
    994  1.8.16.3      yamt 
    995  1.8.16.3      yamt 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
    996  1.8.16.3      yamt 	    __func__, edata->desc, edata->units, edata->state));
    997  1.8.16.3      yamt 
    998  1.8.16.4      yamt 	if (sme_sensor_upstring(dict, "state", sdt[j].desc))
    999  1.8.16.3      yamt 		goto invalidate_sensor;
   1000  1.8.16.3      yamt 
   1001  1.8.16.3      yamt 	/*
   1002  1.8.16.3      yamt 	 * Add the monitoring boolean object:
   1003  1.8.16.3      yamt 	 *
   1004  1.8.16.3      yamt 	 * 		...
   1005  1.8.16.3      yamt 	 * 		<key>monitoring-supported</key>
   1006  1.8.16.3      yamt 	 * 		<true/>
   1007  1.8.16.3      yamt 	 *		...
   1008  1.8.16.3      yamt 	 *
   1009  1.8.16.5      yamt 	 * always false on Battery {capacity,charge}, Drive and Indicator types.
   1010  1.8.16.3      yamt 	 * They cannot be monitored.
   1011  1.8.16.3      yamt 	 *
   1012  1.8.16.3      yamt 	 */
   1013  1.8.16.3      yamt 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1014  1.8.16.3      yamt 	    (edata->units == ENVSYS_INDICATOR) ||
   1015  1.8.16.3      yamt 	    (edata->units == ENVSYS_DRIVE) ||
   1016  1.8.16.5      yamt 	    (edata->units == ENVSYS_BATTERY_CAPACITY) ||
   1017  1.8.16.5      yamt 	    (edata->units == ENVSYS_BATTERY_CHARGE)) {
   1018  1.8.16.3      yamt 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
   1019  1.8.16.4      yamt 			goto out;
   1020  1.8.16.3      yamt 	} else {
   1021  1.8.16.3      yamt 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
   1022  1.8.16.4      yamt 			goto out;
   1023  1.8.16.3      yamt 	}
   1024  1.8.16.3      yamt 
   1025  1.8.16.3      yamt 	/*
   1026  1.8.16.4      yamt 	 * Add the percentage boolean object, true if ENVSYS_FPERCENT
   1027  1.8.16.4      yamt 	 * is set or false otherwise.
   1028  1.8.16.3      yamt 	 *
   1029  1.8.16.3      yamt 	 * 		...
   1030  1.8.16.4      yamt 	 * 		<key>want-percentage</key>
   1031  1.8.16.4      yamt 	 * 		<true/>
   1032  1.8.16.3      yamt 	 * 		...
   1033  1.8.16.3      yamt 	 */
   1034  1.8.16.4      yamt 	if (edata->flags & ENVSYS_FPERCENT)
   1035  1.8.16.4      yamt 		if (sme_sensor_upbool(dict, "want-percentage", true))
   1036  1.8.16.4      yamt 			goto out;
   1037  1.8.16.3      yamt 
   1038  1.8.16.4      yamt 	/*
   1039  1.8.16.4      yamt 	 * Add the allow-rfact boolean object, true if
   1040  1.8.16.4      yamt 	 * ENVSYS_FCHANGERFACT if set or false otherwise.
   1041  1.8.16.4      yamt 	 *
   1042  1.8.16.4      yamt 	 * 		...
   1043  1.8.16.4      yamt 	 * 		<key>allow-rfact</key>
   1044  1.8.16.4      yamt 	 * 		<true/>
   1045  1.8.16.4      yamt 	 * 		...
   1046  1.8.16.4      yamt 	 */
   1047  1.8.16.4      yamt 	if (edata->units == ENVSYS_SVOLTS_DC ||
   1048  1.8.16.4      yamt 	    edata->units == ENVSYS_SVOLTS_AC) {
   1049  1.8.16.4      yamt 		if (edata->flags & ENVSYS_FCHANGERFACT) {
   1050  1.8.16.4      yamt 			if (sme_sensor_upbool(dict, "allow-rfact", true))
   1051  1.8.16.4      yamt 				goto out;
   1052  1.8.16.4      yamt 		} else {
   1053  1.8.16.4      yamt 			if (sme_sensor_upbool(dict, "allow-rfact", false))
   1054  1.8.16.4      yamt 				goto out;
   1055  1.8.16.4      yamt 		}
   1056  1.8.16.3      yamt 	}
   1057  1.8.16.3      yamt 
   1058  1.8.16.4      yamt 
   1059  1.8.16.3      yamt 	/*
   1060  1.8.16.5      yamt 	 * Add the object for battery capacity sensors:
   1061  1.8.16.3      yamt 	 *
   1062  1.8.16.3      yamt 	 * 		...
   1063  1.8.16.5      yamt 	 * 		<key>battery-capacity</key>
   1064  1.8.16.4      yamt 	 * 		<string>NORMAL</string>
   1065  1.8.16.3      yamt 	 * 		...
   1066  1.8.16.3      yamt 	 */
   1067  1.8.16.5      yamt 	if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1068  1.8.16.5      yamt 		sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1069  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1070  1.8.16.4      yamt 			if (sdt[j].type == edata->value_cur)
   1071  1.8.16.4      yamt 				break;
   1072  1.8.16.4      yamt 
   1073  1.8.16.5      yamt 		if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc))
   1074  1.8.16.4      yamt 			goto out;
   1075  1.8.16.4      yamt 	}
   1076  1.8.16.3      yamt 
   1077  1.8.16.3      yamt 	/*
   1078  1.8.16.3      yamt 	 * Add the drive-state object for drive sensors:
   1079  1.8.16.3      yamt 	 *
   1080  1.8.16.3      yamt 	 * 		...
   1081  1.8.16.3      yamt 	 * 		<key>drive-state</key>
   1082  1.8.16.3      yamt 	 * 		<string>drive is online</string>
   1083  1.8.16.3      yamt 	 * 		...
   1084  1.8.16.3      yamt 	 */
   1085  1.8.16.3      yamt 	if (edata->units == ENVSYS_DRIVE) {
   1086  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1087  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1088  1.8.16.4      yamt 			if (sdt[j].type == edata->value_cur)
   1089  1.8.16.3      yamt 				break;
   1090  1.8.16.3      yamt 
   1091  1.8.16.4      yamt 		if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
   1092  1.8.16.4      yamt 			goto out;
   1093  1.8.16.3      yamt 	}
   1094  1.8.16.3      yamt 
   1095  1.8.16.3      yamt 	/*
   1096  1.8.16.3      yamt 	 * if sensor is enabled, add the following properties...
   1097  1.8.16.3      yamt 	 */
   1098  1.8.16.3      yamt 	if (edata->state == ENVSYS_SVALID) {
   1099  1.8.16.3      yamt 		/*
   1100  1.8.16.3      yamt 		 * 	...
   1101  1.8.16.3      yamt 		 * 	<key>rpms</key>
   1102  1.8.16.3      yamt 		 * 	<integer>2500</integer>
   1103  1.8.16.3      yamt 		 * 	<key>rfact</key>
   1104  1.8.16.3      yamt 		 * 	<integer>10000</integer>
   1105  1.8.16.3      yamt 		 * 	<key>cur-value</key>
   1106  1.8.16.3      yamt 		 * 	<integer>1250</integer>
   1107  1.8.16.3      yamt 		 * 	<key>min-value</key>
   1108  1.8.16.3      yamt 		 * 	<integer>800</integer>
   1109  1.8.16.3      yamt 		 * 	<key>max-value</integer>
   1110  1.8.16.3      yamt 		 * 	<integer>3000</integer>
   1111  1.8.16.3      yamt 		 * 	<key>avg-value</integer>
   1112  1.8.16.3      yamt 		 * 	<integer>1400</integer>
   1113  1.8.16.3      yamt 		 * </dict>
   1114  1.8.16.3      yamt 		 */
   1115  1.8.16.3      yamt 		if (edata->units == ENVSYS_SFANRPM)
   1116  1.8.16.3      yamt 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
   1117  1.8.16.4      yamt 				goto out;
   1118  1.8.16.3      yamt 
   1119  1.8.16.3      yamt 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1120  1.8.16.3      yamt 		    edata->units == ENVSYS_SVOLTS_DC)
   1121  1.8.16.3      yamt 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
   1122  1.8.16.4      yamt 				goto out;
   1123  1.8.16.3      yamt 
   1124  1.8.16.3      yamt 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
   1125  1.8.16.4      yamt 			goto out;
   1126  1.8.16.3      yamt 
   1127  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1128  1.8.16.3      yamt 			if (sme_sensor_upint32(dict,
   1129  1.8.16.3      yamt 					       "min-value",
   1130  1.8.16.3      yamt 					       edata->value_min))
   1131  1.8.16.4      yamt 				goto out;
   1132  1.8.16.3      yamt 		}
   1133  1.8.16.3      yamt 
   1134  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1135  1.8.16.3      yamt 			if (sme_sensor_upint32(dict,
   1136  1.8.16.3      yamt 					       "max-value",
   1137  1.8.16.3      yamt 					       edata->value_max))
   1138  1.8.16.4      yamt 				goto out;
   1139  1.8.16.3      yamt 		}
   1140  1.8.16.3      yamt 
   1141  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1142  1.8.16.3      yamt 			if (sme_sensor_upint32(dict,
   1143  1.8.16.3      yamt 					       "avg-value",
   1144  1.8.16.3      yamt 					        edata->value_avg))
   1145  1.8.16.4      yamt 				goto out;
   1146  1.8.16.3      yamt 		}
   1147  1.8.16.3      yamt 	}
   1148  1.8.16.3      yamt 
   1149  1.8.16.3      yamt 	/*
   1150  1.8.16.3      yamt 	 * 	...
   1151  1.8.16.3      yamt 	 * </array>
   1152  1.8.16.3      yamt 	 *
   1153  1.8.16.3      yamt 	 * Add the dictionary into the array.
   1154  1.8.16.3      yamt 	 *
   1155  1.8.16.3      yamt 	 */
   1156  1.8.16.3      yamt 
   1157  1.8.16.3      yamt 	if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
   1158  1.8.16.3      yamt 		DPRINTF(("%s: prop_array_add\n", __func__));
   1159  1.8.16.3      yamt 		goto invalidate_sensor;
   1160  1.8.16.3      yamt 	}
   1161  1.8.16.3      yamt 
   1162  1.8.16.3      yamt 	/*
   1163  1.8.16.3      yamt 	 * Add a new event if a monitoring flag was set.
   1164  1.8.16.3      yamt 	 */
   1165  1.8.16.3      yamt 	if (edata->monitor) {
   1166  1.8.16.4      yamt 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
   1167  1.8.16.3      yamt 		sme_evdrv_t->sdict = dict;
   1168  1.8.16.3      yamt 		sme_evdrv_t->edata = edata;
   1169  1.8.16.3      yamt 		sme_evdrv_t->sme = sme;
   1170  1.8.16.4      yamt 		sme_evdrv_t->powertype = sdt_units[i].crittype;
   1171  1.8.16.3      yamt 	}
   1172  1.8.16.3      yamt 
   1173  1.8.16.4      yamt out:
   1174  1.8.16.4      yamt 	return sme_evdrv_t;
   1175  1.8.16.3      yamt 
   1176  1.8.16.3      yamt invalidate_sensor:
   1177  1.8.16.3      yamt 	edata->flags |= ENVSYS_FNOTVALID;
   1178  1.8.16.4      yamt 	prop_object_release(dict);
   1179  1.8.16.4      yamt 	return sme_evdrv_t;
   1180  1.8.16.3      yamt }
   1181  1.8.16.3      yamt 
   1182  1.8.16.3      yamt /*
   1183  1.8.16.3      yamt  * sme_update_dictionary:
   1184  1.8.16.3      yamt  *
   1185  1.8.16.3      yamt  * 	+ Update per-sensor dictionaries with new values if there were
   1186  1.8.16.3      yamt  * 	  changes, otherwise the object in dictionary is untouched.
   1187  1.8.16.3      yamt  */
   1188  1.8.16.3      yamt int
   1189  1.8.16.3      yamt sme_update_dictionary(struct sysmon_envsys *sme)
   1190  1.8.16.3      yamt {
   1191  1.8.16.4      yamt 	const struct sme_description_table *sdt;
   1192  1.8.16.4      yamt 	envsys_data_t *edata;
   1193  1.8.16.3      yamt 	prop_object_t array, dict;
   1194  1.8.16.3      yamt 	int i, j, error, invalid;
   1195  1.8.16.3      yamt 
   1196  1.8.16.4      yamt 	KASSERT(mutex_owned(&sme_mtx));
   1197  1.8.16.3      yamt 
   1198  1.8.16.3      yamt 	error = invalid = 0;
   1199  1.8.16.3      yamt 	array = dict = NULL;
   1200  1.8.16.3      yamt 
   1201  1.8.16.3      yamt 	/* retrieve the array of dictionaries in device. */
   1202  1.8.16.3      yamt 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1203  1.8.16.3      yamt 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
   1204  1.8.16.3      yamt 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
   1205  1.8.16.3      yamt 		return EINVAL;
   1206  1.8.16.3      yamt 	}
   1207  1.8.16.3      yamt 
   1208  1.8.16.3      yamt 	/*
   1209  1.8.16.3      yamt 	 * - iterate over all sensors.
   1210  1.8.16.3      yamt 	 * - fetch new data.
   1211  1.8.16.3      yamt 	 * - check if data in dictionary is different than new data.
   1212  1.8.16.3      yamt 	 * - update dictionary if there were changes.
   1213  1.8.16.3      yamt 	 */
   1214  1.8.16.4      yamt 	DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
   1215  1.8.16.4      yamt 	    sme->sme_name, sme->sme_nsensors));
   1216  1.8.16.4      yamt 
   1217  1.8.16.3      yamt 	for (i = 0; i < sme->sme_nsensors; i++) {
   1218  1.8.16.3      yamt 		edata = &sme->sme_sensor_data[i];
   1219  1.8.16.3      yamt 		/* skip invalid sensors */
   1220  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FNOTVALID) {
   1221  1.8.16.4      yamt 			DPRINTF(("%s: invalid sensor=%s idx=%d\n",
   1222  1.8.16.4      yamt 			    __func__, edata->desc, edata->sensor));
   1223  1.8.16.3      yamt 			invalid++;
   1224  1.8.16.3      yamt 			continue;
   1225  1.8.16.3      yamt 		}
   1226  1.8.16.3      yamt 
   1227  1.8.16.3      yamt 		/*
   1228  1.8.16.3      yamt 		 * refresh sensor data via sme_gtredata only if the
   1229  1.8.16.3      yamt 		 * flag is not set.
   1230  1.8.16.3      yamt 		 */
   1231  1.8.16.3      yamt 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
   1232  1.8.16.3      yamt 			error = (*sme->sme_gtredata)(sme, edata);
   1233  1.8.16.3      yamt 			if (error) {
   1234  1.8.16.3      yamt 				DPRINTF(("%s: gtredata[%d] failed\n",
   1235  1.8.16.3      yamt 				    __func__, i));
   1236  1.8.16.3      yamt 				return error;
   1237  1.8.16.3      yamt 			}
   1238  1.8.16.3      yamt 		}
   1239  1.8.16.3      yamt 
   1240  1.8.16.3      yamt 		/* retrieve sensor's dictionary. */
   1241  1.8.16.3      yamt 		dict = prop_array_get(array, i - invalid);
   1242  1.8.16.3      yamt 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1243  1.8.16.3      yamt 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1244  1.8.16.3      yamt 			    __func__, edata->sensor, sme->sme_name));
   1245  1.8.16.3      yamt 			return EINVAL;
   1246  1.8.16.3      yamt 		}
   1247  1.8.16.3      yamt 
   1248  1.8.16.4      yamt 		/* update sensor's state */
   1249  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_STATES);
   1250  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1251  1.8.16.4      yamt 			if (sdt[j].type == edata->state)
   1252  1.8.16.3      yamt 				break;
   1253  1.8.16.3      yamt 
   1254  1.8.16.3      yamt 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1255  1.8.16.4      yamt 		    "units=%d sensor=%d\n", __func__, sdt[j].desc,
   1256  1.8.16.4      yamt 		    sdt[j].type, edata->flags, edata->units, edata->sensor));
   1257  1.8.16.3      yamt 
   1258  1.8.16.3      yamt 		/* update sensor state */
   1259  1.8.16.4      yamt 		error = sme_sensor_upstring(dict, "state", sdt[j].desc);
   1260  1.8.16.3      yamt 		if (error)
   1261  1.8.16.3      yamt 			break;
   1262  1.8.16.3      yamt 
   1263  1.8.16.4      yamt 		/* update sensor type */
   1264  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1265  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1266  1.8.16.4      yamt 			if (sdt[j].type == edata->units)
   1267  1.8.16.3      yamt 				break;
   1268  1.8.16.3      yamt 
   1269  1.8.16.4      yamt 		error = sme_sensor_upstring(dict, "type", sdt[j].desc);
   1270  1.8.16.3      yamt 		if (error)
   1271  1.8.16.3      yamt 			break;
   1272  1.8.16.3      yamt 
   1273  1.8.16.3      yamt 		/* update sensor current value */
   1274  1.8.16.3      yamt 		error = sme_sensor_upint32(dict,
   1275  1.8.16.3      yamt 					   "cur-value",
   1276  1.8.16.3      yamt 					   edata->value_cur);
   1277  1.8.16.3      yamt 		if (error)
   1278  1.8.16.3      yamt 			break;
   1279  1.8.16.3      yamt 
   1280  1.8.16.3      yamt 		/*
   1281  1.8.16.5      yamt 		 * Battery charge, Integer and Indicator types do not
   1282  1.8.16.5      yamt 		 * need the following objects, so skip them.
   1283  1.8.16.3      yamt 		 */
   1284  1.8.16.3      yamt 		if (edata->units == ENVSYS_INTEGER ||
   1285  1.8.16.5      yamt 		    edata->units == ENVSYS_INDICATOR ||
   1286  1.8.16.5      yamt 		    edata->units == ENVSYS_BATTERY_CHARGE)
   1287  1.8.16.3      yamt 			continue;
   1288  1.8.16.3      yamt 
   1289  1.8.16.3      yamt 		/* update sensor flags */
   1290  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FPERCENT) {
   1291  1.8.16.3      yamt 			error = sme_sensor_upbool(dict,
   1292  1.8.16.3      yamt 						  "want-percentage",
   1293  1.8.16.3      yamt 						  true);
   1294  1.8.16.3      yamt 			if (error)
   1295  1.8.16.3      yamt 				break;
   1296  1.8.16.3      yamt 		}
   1297  1.8.16.3      yamt 
   1298  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1299  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1300  1.8.16.3      yamt 						   "max-value",
   1301  1.8.16.3      yamt 						   edata->value_max);
   1302  1.8.16.3      yamt 			if (error)
   1303  1.8.16.3      yamt 				break;
   1304  1.8.16.3      yamt 		}
   1305  1.8.16.3      yamt 
   1306  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1307  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1308  1.8.16.3      yamt 						   "min-value",
   1309  1.8.16.3      yamt 						   edata->value_min);
   1310  1.8.16.3      yamt 			if (error)
   1311  1.8.16.3      yamt 				break;
   1312  1.8.16.3      yamt 		}
   1313  1.8.16.3      yamt 
   1314  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1315  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1316  1.8.16.3      yamt 						   "avg-value",
   1317  1.8.16.3      yamt 						   edata->value_avg);
   1318  1.8.16.3      yamt 			if (error)
   1319  1.8.16.3      yamt 				break;
   1320  1.8.16.3      yamt 		}
   1321  1.8.16.3      yamt 
   1322  1.8.16.3      yamt 		/* update 'rpms' only in ENVSYS_SFANRPM. */
   1323  1.8.16.3      yamt 		if (edata->units == ENVSYS_SFANRPM) {
   1324  1.8.16.3      yamt 			error = sme_sensor_upuint32(dict,
   1325  1.8.16.3      yamt 						    "rpms",
   1326  1.8.16.3      yamt 						    edata->rpms);
   1327  1.8.16.3      yamt 			if (error)
   1328  1.8.16.3      yamt 				break;
   1329  1.8.16.3      yamt 		}
   1330  1.8.16.3      yamt 
   1331  1.8.16.3      yamt 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
   1332  1.8.16.3      yamt 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1333  1.8.16.3      yamt 		    edata->units == ENVSYS_SVOLTS_DC) {
   1334  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1335  1.8.16.3      yamt 						   "rfact",
   1336  1.8.16.3      yamt 						   edata->rfact);
   1337  1.8.16.3      yamt 			if (error)
   1338  1.8.16.3      yamt 				break;
   1339  1.8.16.3      yamt 		}
   1340  1.8.16.3      yamt 
   1341  1.8.16.3      yamt 		/* update 'drive-state' only in ENVSYS_DRIVE. */
   1342  1.8.16.3      yamt 		if (edata->units == ENVSYS_DRIVE) {
   1343  1.8.16.4      yamt 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1344  1.8.16.4      yamt 			for (j = 0; sdt[j].type != -1; j++)
   1345  1.8.16.4      yamt 				if (sdt[j].type == edata->value_cur)
   1346  1.8.16.3      yamt 					break;
   1347  1.8.16.3      yamt 
   1348  1.8.16.3      yamt 			error = sme_sensor_upstring(dict,
   1349  1.8.16.3      yamt 						    "drive-state",
   1350  1.8.16.4      yamt 						    sdt[j].desc);
   1351  1.8.16.4      yamt 			if (error)
   1352  1.8.16.4      yamt 				break;
   1353  1.8.16.4      yamt 		}
   1354  1.8.16.4      yamt 
   1355  1.8.16.5      yamt 		/* update 'battery-capacity' only in ENVSYS_BATTERY_CAPACITY. */
   1356  1.8.16.5      yamt 		if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1357  1.8.16.4      yamt 			sdt =
   1358  1.8.16.5      yamt 			    sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1359  1.8.16.4      yamt 			for (j = 0; sdt[j].type != -1; j++)
   1360  1.8.16.4      yamt 				if (sdt[j].type == edata->value_cur)
   1361  1.8.16.4      yamt 					break;
   1362  1.8.16.4      yamt 
   1363  1.8.16.4      yamt 			error = sme_sensor_upstring(dict,
   1364  1.8.16.5      yamt 						    "battery-capacity",
   1365  1.8.16.4      yamt 						    sdt[j].desc);
   1366  1.8.16.3      yamt 			if (error)
   1367  1.8.16.3      yamt 				break;
   1368  1.8.16.3      yamt 		}
   1369  1.8.16.3      yamt 	}
   1370  1.8.16.3      yamt 
   1371  1.8.16.3      yamt 	return error;
   1372  1.8.16.3      yamt }
   1373  1.8.16.3      yamt 
   1374  1.8.16.3      yamt /*
   1375  1.8.16.3      yamt  * sme_userset_dictionary:
   1376  1.8.16.3      yamt  *
   1377  1.8.16.4      yamt  * 	+ Parse the userland sensor's dictionary and run the appropiate
   1378  1.8.16.4      yamt  * 	  tasks that was requested.
   1379  1.8.16.3      yamt  */
   1380  1.8.16.3      yamt int
   1381  1.8.16.3      yamt sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1382  1.8.16.3      yamt 		       prop_array_t array)
   1383  1.8.16.3      yamt {
   1384  1.8.16.4      yamt 	const struct sme_description_table *sdt;
   1385  1.8.16.3      yamt 	envsys_data_t *edata, *nedata;
   1386  1.8.16.3      yamt 	prop_dictionary_t dict;
   1387  1.8.16.3      yamt 	prop_object_t obj, obj1, obj2;
   1388  1.8.16.3      yamt 	int32_t critval;
   1389  1.8.16.3      yamt 	int i, invalid, error;
   1390  1.8.16.3      yamt 	const char *blah, *sname;
   1391  1.8.16.3      yamt 	bool targetfound = false;
   1392  1.8.16.3      yamt 
   1393  1.8.16.3      yamt 	error = invalid = 0;
   1394  1.8.16.3      yamt 	blah = sname = NULL;
   1395  1.8.16.3      yamt 
   1396  1.8.16.4      yamt 	/* get sensor's index from userland dictionary. */
   1397  1.8.16.4      yamt 	obj = prop_dictionary_get(udict, "index");
   1398  1.8.16.3      yamt 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1399  1.8.16.3      yamt 		DPRINTF(("%s: sensor-name failed\n", __func__));
   1400  1.8.16.3      yamt 		return EINVAL;
   1401  1.8.16.3      yamt 	}
   1402  1.8.16.3      yamt 
   1403  1.8.16.3      yamt 	/* iterate over the sensors to find the right one */
   1404  1.8.16.3      yamt 	for (i = 0; i < sme->sme_nsensors; i++) {
   1405  1.8.16.3      yamt 		edata = &sme->sme_sensor_data[i];
   1406  1.8.16.4      yamt 		/*
   1407  1.8.16.4      yamt 		 * skip invalid sensors.
   1408  1.8.16.4      yamt 		 */
   1409  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FNOTVALID) {
   1410  1.8.16.3      yamt 			invalid++;
   1411  1.8.16.3      yamt 			continue;
   1412  1.8.16.3      yamt 		}
   1413  1.8.16.3      yamt 
   1414  1.8.16.3      yamt 		dict = prop_array_get(array, i - invalid);
   1415  1.8.16.4      yamt 		obj1 = prop_dictionary_get(dict, "index");
   1416  1.8.16.3      yamt 
   1417  1.8.16.3      yamt 		/* is it our sensor? */
   1418  1.8.16.3      yamt 		if (!prop_string_equals(obj1, obj))
   1419  1.8.16.3      yamt 			continue;
   1420  1.8.16.3      yamt 
   1421  1.8.16.3      yamt 		/*
   1422  1.8.16.3      yamt 		 * Check if a new description operation was
   1423  1.8.16.3      yamt 		 * requested by the user and set new description.
   1424  1.8.16.3      yamt 		 */
   1425  1.8.16.4      yamt 		if ((obj2 = prop_dictionary_get(udict, "description"))) {
   1426  1.8.16.3      yamt 			targetfound = true;
   1427  1.8.16.3      yamt 			blah = prop_string_cstring_nocopy(obj2);
   1428  1.8.16.3      yamt 
   1429  1.8.16.3      yamt 			for (i = 0; i < sme->sme_nsensors; i++) {
   1430  1.8.16.3      yamt 				if (i == edata->sensor)
   1431  1.8.16.3      yamt 					continue;
   1432  1.8.16.3      yamt 
   1433  1.8.16.3      yamt 				nedata = &sme->sme_sensor_data[i];
   1434  1.8.16.3      yamt 				if (strcmp(blah, nedata->desc) == 0) {
   1435  1.8.16.3      yamt 					error = EEXIST;
   1436  1.8.16.3      yamt 					break;
   1437  1.8.16.3      yamt 				}
   1438  1.8.16.3      yamt 			}
   1439  1.8.16.3      yamt 
   1440  1.8.16.3      yamt 			if (error)
   1441  1.8.16.3      yamt 				break;
   1442  1.8.16.3      yamt 
   1443  1.8.16.3      yamt 			error = sme_sensor_upstring(dict,
   1444  1.8.16.3      yamt 						    "description",
   1445  1.8.16.3      yamt 						    blah);
   1446  1.8.16.3      yamt 			if (error)
   1447  1.8.16.3      yamt 				break;
   1448  1.8.16.3      yamt 
   1449  1.8.16.4      yamt 			edata->upropset |= USERPROP_DESC;
   1450  1.8.16.3      yamt 		}
   1451  1.8.16.3      yamt 
   1452  1.8.16.4      yamt 		/*
   1453  1.8.16.4      yamt 		 * did the user want to change the rfact?
   1454  1.8.16.4      yamt 		 */
   1455  1.8.16.4      yamt 		obj2 = prop_dictionary_get(udict, "rfact");
   1456  1.8.16.4      yamt 		if (obj2) {
   1457  1.8.16.3      yamt 			targetfound = true;
   1458  1.8.16.4      yamt 			if (edata->flags & ENVSYS_FCHANGERFACT) {
   1459  1.8.16.3      yamt 				edata->rfact = prop_number_integer_value(obj2);
   1460  1.8.16.4      yamt 				edata->upropset |= USERPROP_RFACT;
   1461  1.8.16.4      yamt 			} else {
   1462  1.8.16.3      yamt 				error = ENOTSUP;
   1463  1.8.16.4      yamt 				break;
   1464  1.8.16.4      yamt 			}
   1465  1.8.16.3      yamt 		}
   1466  1.8.16.3      yamt 
   1467  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1468  1.8.16.4      yamt 		for (i = 0; sdt[i].type != -1; i++)
   1469  1.8.16.4      yamt 			if (sdt[i].type == edata->units)
   1470  1.8.16.3      yamt 				break;
   1471  1.8.16.3      yamt 
   1472  1.8.16.4      yamt 
   1473  1.8.16.4      yamt 		/*
   1474  1.8.16.4      yamt 		 * did the user want to set a critical capacity event?
   1475  1.8.16.4      yamt 		 *
   1476  1.8.16.4      yamt 		 * NOTE: if sme_event_register returns EEXIST that means
   1477  1.8.16.4      yamt 		 * the object is already there, but this is not a real
   1478  1.8.16.4      yamt 		 * error, because the object might be updated.
   1479  1.8.16.4      yamt 		 */
   1480  1.8.16.3      yamt 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1481  1.8.16.4      yamt 		if (obj2) {
   1482  1.8.16.3      yamt 			targetfound = true;
   1483  1.8.16.3      yamt 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1484  1.8.16.3      yamt 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1485  1.8.16.3      yamt 				error = ENOTSUP;
   1486  1.8.16.3      yamt 				break;
   1487  1.8.16.3      yamt 			}
   1488  1.8.16.3      yamt 
   1489  1.8.16.3      yamt 			critval = prop_number_integer_value(obj2);
   1490  1.8.16.3      yamt 			error = sme_event_register(dict,
   1491  1.8.16.3      yamt 					      edata,
   1492  1.8.16.3      yamt 					      sme->sme_name,
   1493  1.8.16.3      yamt 					      "critical-capacity",
   1494  1.8.16.3      yamt 					      critval,
   1495  1.8.16.3      yamt 					      PENVSYS_EVENT_BATT_USERCAP,
   1496  1.8.16.4      yamt 					      sdt[i].crittype);
   1497  1.8.16.4      yamt 			if (error == EEXIST)
   1498  1.8.16.4      yamt 				error = 0;
   1499  1.8.16.4      yamt 			if (!error)
   1500  1.8.16.4      yamt 				edata->upropset |= USERPROP_BATTCAP;
   1501  1.8.16.3      yamt 		}
   1502  1.8.16.3      yamt 
   1503  1.8.16.4      yamt 		/*
   1504  1.8.16.4      yamt 		 * did the user want to set a critical max event?
   1505  1.8.16.4      yamt 		 */
   1506  1.8.16.4      yamt 		obj2 = prop_dictionary_get(udict, "critical-max");
   1507  1.8.16.4      yamt 		if (obj2) {
   1508  1.8.16.3      yamt 			targetfound = true;
   1509  1.8.16.3      yamt 			if (edata->units == ENVSYS_INDICATOR ||
   1510  1.8.16.3      yamt 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1511  1.8.16.3      yamt 				error = ENOTSUP;
   1512  1.8.16.3      yamt 				break;
   1513  1.8.16.3      yamt 			}
   1514  1.8.16.3      yamt 
   1515  1.8.16.3      yamt 			critval = prop_number_integer_value(obj2);
   1516  1.8.16.3      yamt 			error = sme_event_register(dict,
   1517  1.8.16.3      yamt 					      edata,
   1518  1.8.16.3      yamt 					      sme->sme_name,
   1519  1.8.16.4      yamt 					      "critical-max",
   1520  1.8.16.3      yamt 					      critval,
   1521  1.8.16.3      yamt 					      PENVSYS_EVENT_USER_CRITMAX,
   1522  1.8.16.4      yamt 					      sdt[i].crittype);
   1523  1.8.16.4      yamt 			if (error == EEXIST)
   1524  1.8.16.4      yamt 				error = 0;
   1525  1.8.16.4      yamt 			if (!error)
   1526  1.8.16.4      yamt 				edata->upropset |= USERPROP_CRITMAX;
   1527  1.8.16.3      yamt 		}
   1528  1.8.16.3      yamt 
   1529  1.8.16.4      yamt 		/*
   1530  1.8.16.4      yamt 		 * did the user want to set a critical min event?
   1531  1.8.16.4      yamt 		 */
   1532  1.8.16.4      yamt 		obj2 = prop_dictionary_get(udict, "critical-min");
   1533  1.8.16.4      yamt 		if (obj2) {
   1534  1.8.16.3      yamt 			targetfound = true;
   1535  1.8.16.3      yamt 			if (edata->units == ENVSYS_INDICATOR ||
   1536  1.8.16.3      yamt 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1537  1.8.16.3      yamt 				error = ENOTSUP;
   1538  1.8.16.3      yamt 				break;
   1539  1.8.16.3      yamt 			}
   1540  1.8.16.3      yamt 
   1541  1.8.16.3      yamt 			critval = prop_number_integer_value(obj2);
   1542  1.8.16.3      yamt 			error = sme_event_register(dict,
   1543  1.8.16.3      yamt 					      edata,
   1544  1.8.16.3      yamt 					      sme->sme_name,
   1545  1.8.16.4      yamt 					      "critical-min",
   1546  1.8.16.3      yamt 					      critval,
   1547  1.8.16.3      yamt 					      PENVSYS_EVENT_USER_CRITMIN,
   1548  1.8.16.4      yamt 					      sdt[i].crittype);
   1549  1.8.16.4      yamt 			if (error == EEXIST)
   1550  1.8.16.4      yamt 				error = 0;
   1551  1.8.16.4      yamt 			if (!error)
   1552  1.8.16.4      yamt 				edata->upropset |= USERPROP_CRITMIN;
   1553  1.8.16.3      yamt 		}
   1554  1.8.16.4      yamt 
   1555  1.8.16.4      yamt 		/*
   1556  1.8.16.4      yamt 		 * All objects in dictionary were processed.
   1557  1.8.16.4      yamt 		 */
   1558  1.8.16.4      yamt 		break;
   1559  1.8.16.3      yamt 	}
   1560  1.8.16.3      yamt 
   1561  1.8.16.3      yamt 	/* invalid target? return the error */
   1562  1.8.16.3      yamt 	if (!targetfound)
   1563  1.8.16.3      yamt 		error = EINVAL;
   1564  1.8.16.3      yamt 
   1565  1.8.16.3      yamt 	return error;
   1566       1.1   thorpej }
   1567