Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.8.16.9
      1  1.8.16.9      yamt /*	$NetBSD: sysmon_envsys.c,v 1.8.16.9 2008/03/24 09:39:00 yamt Exp $	*/
      2  1.8.16.3      yamt 
      3  1.8.16.3      yamt /*-
      4  1.8.16.9      yamt  * Copyright (c) 2007, 2008 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.9      yamt __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.8.16.9 2008/03/24 09:39:00 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.6      yamt  * The 'sme_mtx' lock is used to protect access to the sysmon_envsys
     89  1.8.16.6      yamt  * objects (devices, sensors, events) and the global counter
     90  1.8.16.6      yamt  * 'sysmon_envsys_next_sensor_index'. The 'sme_cv' condition variable
     91  1.8.16.6      yamt  * is used to wait for completion paths on these objects.
     92  1.8.16.6      yamt  *
     93  1.8.16.6      yamt  * The 'sme_events_mtx' lock is used to protect initialization and
     94  1.8.16.6      yamt  * finalization of the per device events framework (the callout(9) and
     95  1.8.16.6      yamt  * workqueue(9) that is used to check for conditions and sending events
     96  1.8.16.6      yamt  * to the powerd(8) daemon (if running)).
     97  1.8.16.4      yamt  *
     98  1.8.16.6      yamt  * The callouts are protected by the 'sme_callout_mtx'.
     99  1.8.16.4      yamt  */
    100  1.8.16.3      yamt 
    101  1.8.16.6      yamt kmutex_t sme_mtx, sme_events_mtx, sme_callout_mtx;
    102  1.8.16.4      yamt kcondvar_t sme_cv;
    103  1.8.16.3      yamt 
    104  1.8.16.4      yamt /*
    105  1.8.16.4      yamt  * Types of properties that can be set via userland.
    106  1.8.16.4      yamt  */
    107  1.8.16.4      yamt enum {
    108  1.8.16.4      yamt 	USERPROP_DESC 		= 0x0001,
    109  1.8.16.4      yamt 	USERPROP_BATTCAP	= 0x0002,
    110  1.8.16.4      yamt 	USERPROP_CRITMAX	= 0x0004,
    111  1.8.16.4      yamt 	USERPROP_CRITMIN	= 0x0008,
    112  1.8.16.4      yamt 	USERPROP_RFACT		= 0x0010
    113  1.8.16.3      yamt };
    114  1.8.16.3      yamt 
    115  1.8.16.3      yamt static prop_dictionary_t sme_propd;
    116  1.8.16.4      yamt static uint32_t sysmon_envsys_next_sensor_index = 0;
    117  1.8.16.3      yamt static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    118  1.8.16.3      yamt 
    119  1.8.16.3      yamt static void sysmon_envsys_destroy_plist(prop_array_t);
    120  1.8.16.4      yamt static void sme_remove_userprops(void);
    121  1.8.16.6      yamt static int sme_add_property_dictionary(struct sysmon_envsys *, prop_array_t,
    122  1.8.16.6      yamt 				       prop_dictionary_t);
    123  1.8.16.9      yamt static void sme_initial_refresh(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.6      yamt 	mutex_init(&sme_mtx, MUTEX_DEFAULT, IPL_NONE);
    135  1.8.16.6      yamt 	mutex_init(&sme_events_mtx, MUTEX_DEFAULT, IPL_NONE);
    136  1.8.16.6      yamt 	mutex_init(&sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    137  1.8.16.6      yamt 	cv_init(&sme_cv, "smeworker");
    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.8.16.3      yamt 	return 0;
    161       1.1   thorpej }
    162       1.1   thorpej 
    163       1.1   thorpej /*
    164       1.1   thorpej  * sysmonioctl_envsys:
    165       1.1   thorpej  *
    166  1.8.16.3      yamt  *	+ Perform a sysmon envsys control request.
    167       1.1   thorpej  */
    168       1.1   thorpej int
    169  1.8.16.3      yamt sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    170       1.1   thorpej {
    171  1.8.16.3      yamt 	struct sysmon_envsys *sme = NULL;
    172       1.1   thorpej 	int error = 0;
    173       1.1   thorpej 	u_int oidx;
    174       1.1   thorpej 
    175       1.1   thorpej 	switch (cmd) {
    176  1.8.16.6      yamt 	/*
    177  1.8.16.6      yamt 	 * To update the global dictionary with latest data from devices.
    178  1.8.16.6      yamt 	 */
    179  1.8.16.3      yamt 	case ENVSYS_GETDICTIONARY:
    180  1.8.16.3      yamt 	    {
    181  1.8.16.3      yamt 		struct plistref *plist = (struct plistref *)data;
    182  1.8.16.4      yamt 
    183  1.8.16.3      yamt 		/*
    184  1.8.16.6      yamt 		 * Update dictionaries on all sysmon envsys devices
    185  1.8.16.6      yamt 		 * registered.
    186  1.8.16.3      yamt 		 */
    187  1.8.16.4      yamt 		mutex_enter(&sme_mtx);
    188  1.8.16.3      yamt 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    189  1.8.16.6      yamt 			sysmon_envsys_acquire(sme);
    190  1.8.16.3      yamt 			error = sme_update_dictionary(sme);
    191  1.8.16.3      yamt 			if (error) {
    192  1.8.16.3      yamt 				DPRINTF(("%s: sme_update_dictionary, "
    193  1.8.16.3      yamt 				    "error=%d\n", __func__, error));
    194  1.8.16.6      yamt 				sysmon_envsys_release(sme);
    195  1.8.16.4      yamt 				mutex_exit(&sme_mtx);
    196  1.8.16.3      yamt 				return error;
    197  1.8.16.3      yamt 			}
    198  1.8.16.6      yamt 			sysmon_envsys_release(sme);
    199  1.8.16.3      yamt 		}
    200  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    201  1.8.16.3      yamt 		/*
    202  1.8.16.3      yamt 		 * Copy global dictionary to userland.
    203  1.8.16.3      yamt 		 */
    204  1.8.16.3      yamt 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    205       1.1   thorpej 		break;
    206  1.8.16.3      yamt 	    }
    207  1.8.16.6      yamt 	/*
    208  1.8.16.6      yamt 	 * To set properties on multiple devices.
    209  1.8.16.6      yamt 	 */
    210  1.8.16.3      yamt 	case ENVSYS_SETDICTIONARY:
    211       1.1   thorpej 	    {
    212  1.8.16.3      yamt 		const struct plistref *plist = (const struct plistref *)data;
    213  1.8.16.3      yamt 		prop_dictionary_t udict;
    214  1.8.16.4      yamt 		prop_object_iterator_t iter, iter2;
    215  1.8.16.4      yamt 		prop_object_t obj, obj2;
    216  1.8.16.4      yamt 		prop_array_t array_u, array_k;
    217  1.8.16.3      yamt 		const char *devname = NULL;
    218  1.8.16.3      yamt 
    219  1.8.16.3      yamt 		if ((flag & FWRITE) == 0)
    220  1.8.16.3      yamt 			return EPERM;
    221  1.8.16.3      yamt 
    222  1.8.16.3      yamt 		/*
    223  1.8.16.3      yamt 		 * Get dictionary from userland.
    224  1.8.16.3      yamt 		 */
    225  1.8.16.3      yamt 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    226  1.8.16.3      yamt 		if (error) {
    227  1.8.16.3      yamt 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    228  1.8.16.3      yamt 			    __func__, error));
    229  1.8.16.3      yamt 			break;
    230  1.8.16.3      yamt 		}
    231       1.4  explorer 
    232  1.8.16.4      yamt 		iter = prop_dictionary_iterator(udict);
    233  1.8.16.4      yamt 		if (!iter) {
    234  1.8.16.4      yamt 			prop_object_release(udict);
    235  1.8.16.4      yamt 			return ENOMEM;
    236  1.8.16.4      yamt 		}
    237  1.8.16.4      yamt 
    238  1.8.16.3      yamt 		/*
    239  1.8.16.4      yamt 		 * Iterate over the userland dictionary and process
    240  1.8.16.4      yamt 		 * the list of devices.
    241  1.8.16.3      yamt 		 */
    242  1.8.16.4      yamt 		while ((obj = prop_object_iterator_next(iter))) {
    243  1.8.16.4      yamt 			array_u = prop_dictionary_get_keysym(udict, obj);
    244  1.8.16.4      yamt 			if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
    245  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    246  1.8.16.4      yamt 				prop_object_release(udict);
    247  1.8.16.4      yamt 				return EINVAL;
    248  1.8.16.4      yamt 			}
    249  1.8.16.4      yamt 
    250  1.8.16.4      yamt 			devname = prop_dictionary_keysym_cstring_nocopy(obj);
    251  1.8.16.4      yamt 			DPRINTF(("%s: processing the '%s' array requests\n",
    252  1.8.16.4      yamt 			    __func__, devname));
    253  1.8.16.4      yamt 
    254  1.8.16.4      yamt 			/*
    255  1.8.16.4      yamt 			 * find the correct sme device.
    256  1.8.16.4      yamt 			 */
    257  1.8.16.6      yamt 			mutex_enter(&sme_mtx);
    258  1.8.16.4      yamt 			sme = sysmon_envsys_find(devname);
    259  1.8.16.4      yamt 			if (!sme) {
    260  1.8.16.6      yamt 				mutex_exit(&sme_mtx);
    261  1.8.16.4      yamt 				DPRINTF(("%s: NULL sme\n", __func__));
    262  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    263  1.8.16.4      yamt 				prop_object_release(udict);
    264  1.8.16.4      yamt 				return EINVAL;
    265  1.8.16.4      yamt 			}
    266  1.8.16.4      yamt 
    267  1.8.16.4      yamt 			/*
    268  1.8.16.4      yamt 			 * Find the correct array object with the string
    269  1.8.16.4      yamt 			 * supplied by the userland dictionary.
    270  1.8.16.4      yamt 			 */
    271  1.8.16.4      yamt 			array_k = prop_dictionary_get(sme_propd, devname);
    272  1.8.16.4      yamt 			if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
    273  1.8.16.4      yamt 				DPRINTF(("%s: array device failed\n",
    274  1.8.16.4      yamt 				    __func__));
    275  1.8.16.4      yamt 				sysmon_envsys_release(sme);
    276  1.8.16.6      yamt 				mutex_exit(&sme_mtx);
    277  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    278  1.8.16.4      yamt 				prop_object_release(udict);
    279  1.8.16.4      yamt 				return EINVAL;
    280  1.8.16.4      yamt 			}
    281  1.8.16.4      yamt 
    282  1.8.16.4      yamt 			iter2 = prop_array_iterator(array_u);
    283  1.8.16.4      yamt 			if (!iter2) {
    284  1.8.16.4      yamt 				sysmon_envsys_release(sme);
    285  1.8.16.6      yamt 				mutex_exit(&sme_mtx);
    286  1.8.16.4      yamt 				prop_object_iterator_release(iter);
    287  1.8.16.4      yamt 				prop_object_release(udict);
    288  1.8.16.4      yamt 				return ENOMEM;
    289  1.8.16.4      yamt 			}
    290  1.8.16.4      yamt 
    291  1.8.16.4      yamt 			/*
    292  1.8.16.4      yamt 			 * Iterate over the array of dictionaries to
    293  1.8.16.6      yamt 			 * process the list of sensors and properties.
    294  1.8.16.4      yamt 			 */
    295  1.8.16.4      yamt 			while ((obj2 = prop_object_iterator_next(iter2))) {
    296  1.8.16.6      yamt 				/*
    297  1.8.16.6      yamt 				 * do the real work now.
    298  1.8.16.6      yamt 				 */
    299  1.8.16.4      yamt 				error = sme_userset_dictionary(sme,
    300  1.8.16.4      yamt 							       obj2,
    301  1.8.16.4      yamt 							       array_k);
    302  1.8.16.4      yamt 				if (error) {
    303  1.8.16.4      yamt 					sysmon_envsys_release(sme);
    304  1.8.16.6      yamt 					mutex_exit(&sme_mtx);
    305  1.8.16.4      yamt 					prop_object_iterator_release(iter2);
    306  1.8.16.4      yamt 					prop_object_iterator_release(iter);
    307  1.8.16.4      yamt 					prop_object_release(udict);
    308  1.8.16.6      yamt 					return error;
    309  1.8.16.4      yamt 				}
    310  1.8.16.4      yamt 			}
    311  1.8.16.4      yamt 
    312  1.8.16.4      yamt 			sysmon_envsys_release(sme);
    313  1.8.16.6      yamt 			mutex_exit(&sme_mtx);
    314  1.8.16.4      yamt 			prop_object_iterator_release(iter2);
    315       1.4  explorer 		}
    316       1.1   thorpej 
    317  1.8.16.4      yamt 		prop_object_iterator_release(iter);
    318  1.8.16.4      yamt 		prop_object_release(udict);
    319  1.8.16.4      yamt 		break;
    320  1.8.16.4      yamt 	    }
    321  1.8.16.6      yamt 	/*
    322  1.8.16.6      yamt 	 * To remove all properties from all devices registered.
    323  1.8.16.6      yamt 	 */
    324  1.8.16.4      yamt 	case ENVSYS_REMOVEPROPS:
    325  1.8.16.4      yamt 	    {
    326  1.8.16.4      yamt 		const struct plistref *plist = (const struct plistref *)data;
    327  1.8.16.4      yamt 		prop_dictionary_t udict;
    328  1.8.16.4      yamt 		prop_object_t obj;
    329  1.8.16.3      yamt 
    330  1.8.16.4      yamt 		if ((flag & FWRITE) == 0)
    331  1.8.16.4      yamt 			return EPERM;
    332  1.8.16.4      yamt 
    333  1.8.16.4      yamt 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    334  1.8.16.4      yamt 		if (error) {
    335  1.8.16.4      yamt 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    336  1.8.16.4      yamt 			    __func__, error));
    337       1.1   thorpej 			break;
    338       1.1   thorpej 		}
    339  1.8.16.3      yamt 
    340  1.8.16.4      yamt 		obj = prop_dictionary_get(udict, "envsys-remove-props");
    341  1.8.16.4      yamt 		if (!obj || !prop_bool_true(obj)) {
    342  1.8.16.4      yamt 			DPRINTF(("%s: invalid 'envsys-remove-props'\n",
    343  1.8.16.4      yamt 			     __func__));
    344  1.8.16.4      yamt 			return EINVAL;
    345       1.1   thorpej 		}
    346  1.8.16.3      yamt 
    347  1.8.16.6      yamt 		prop_object_release(udict);
    348  1.8.16.4      yamt 		sme_remove_userprops();
    349  1.8.16.4      yamt 
    350       1.1   thorpej 		break;
    351       1.1   thorpej 	    }
    352  1.8.16.6      yamt 	/*
    353  1.8.16.6      yamt 	 * Compatibility ioctls with the old interface, only implemented
    354  1.8.16.6      yamt 	 * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old
    355  1.8.16.6      yamt 	 * applications work.
    356  1.8.16.6      yamt 	 */
    357       1.1   thorpej 	case ENVSYS_GTREDATA:
    358       1.1   thorpej 	    {
    359  1.8.16.3      yamt 		struct envsys_tre_data *tred = (void *)data;
    360  1.8.16.3      yamt 		envsys_data_t *edata = NULL;
    361  1.8.16.6      yamt 		bool found = false;
    362       1.1   thorpej 
    363       1.1   thorpej 		tred->validflags = 0;
    364       1.1   thorpej 
    365  1.8.16.6      yamt 		mutex_enter(&sme_mtx);
    366  1.8.16.3      yamt 		sme = sysmon_envsys_find_40(tred->sensor);
    367  1.8.16.6      yamt 		if (!sme) {
    368  1.8.16.6      yamt 			mutex_exit(&sme_mtx);
    369       1.1   thorpej 			break;
    370  1.8.16.6      yamt 		}
    371  1.8.16.3      yamt 
    372       1.1   thorpej 		oidx = tred->sensor;
    373       1.1   thorpej 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    374       1.1   thorpej 
    375  1.8.16.3      yamt 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    376  1.8.16.3      yamt 		    __func__, tred->sensor, oidx, sme->sme_name,
    377  1.8.16.3      yamt 		    sme->sme_nsensors));
    378  1.8.16.3      yamt 
    379  1.8.16.6      yamt 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    380  1.8.16.6      yamt 			if (edata->sensor == tred->sensor) {
    381  1.8.16.6      yamt 				found = true;
    382  1.8.16.6      yamt 				break;
    383  1.8.16.6      yamt 			}
    384  1.8.16.6      yamt 		}
    385  1.8.16.6      yamt 
    386  1.8.16.6      yamt 		if (!found) {
    387  1.8.16.6      yamt 			sysmon_envsys_release(sme);
    388  1.8.16.6      yamt 			mutex_exit(&sme_mtx);
    389  1.8.16.6      yamt 			error = ENODEV;
    390  1.8.16.6      yamt 			break;
    391  1.8.16.6      yamt 		}
    392  1.8.16.3      yamt 
    393  1.8.16.3      yamt 		if (tred->sensor < sme->sme_nsensors) {
    394  1.8.16.6      yamt 			if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    395  1.8.16.6      yamt 				(*sme->sme_refresh)(sme, edata);
    396       1.1   thorpej 
    397  1.8.16.6      yamt 			/*
    398  1.8.16.6      yamt 			 * copy required values to the old interface.
    399  1.8.16.6      yamt 			 */
    400  1.8.16.3      yamt 			tred->sensor = edata->sensor;
    401  1.8.16.3      yamt 			tred->cur.data_us = edata->value_cur;
    402  1.8.16.3      yamt 			tred->cur.data_s = edata->value_cur;
    403  1.8.16.3      yamt 			tred->max.data_us = edata->value_max;
    404  1.8.16.3      yamt 			tred->max.data_s = edata->value_max;
    405  1.8.16.3      yamt 			tred->min.data_us = edata->value_min;
    406  1.8.16.3      yamt 			tred->min.data_s = edata->value_min;
    407  1.8.16.3      yamt 			tred->avg.data_us = edata->value_avg;
    408  1.8.16.3      yamt 			tred->avg.data_s = edata->value_avg;
    409  1.8.16.5      yamt 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    410  1.8.16.5      yamt 				tred->units = ENVSYS_INDICATOR;
    411  1.8.16.5      yamt 			else
    412  1.8.16.6      yamt 				tred->units = edata->units;
    413  1.8.16.3      yamt 
    414  1.8.16.3      yamt 			tred->validflags |= ENVSYS_FVALID;
    415  1.8.16.3      yamt 			tred->validflags |= ENVSYS_FCURVALID;
    416  1.8.16.3      yamt 
    417  1.8.16.3      yamt 			if (edata->flags & ENVSYS_FPERCENT) {
    418  1.8.16.3      yamt 				tred->validflags |= ENVSYS_FMAXVALID;
    419  1.8.16.3      yamt 				tred->validflags |= ENVSYS_FFRACVALID;
    420  1.8.16.3      yamt 			}
    421  1.8.16.3      yamt 
    422  1.8.16.6      yamt 			if (edata->state == ENVSYS_SINVALID) {
    423  1.8.16.3      yamt 				tred->validflags &= ~ENVSYS_FCURVALID;
    424  1.8.16.3      yamt 				tred->cur.data_us = tred->cur.data_s = 0;
    425  1.8.16.3      yamt 			}
    426  1.8.16.3      yamt 
    427  1.8.16.3      yamt 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    428  1.8.16.3      yamt 			    __func__, edata->desc, tred->cur.data_s));
    429  1.8.16.3      yamt 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    430  1.8.16.3      yamt 			    " tred->sensor=%d\n", __func__, tred->validflags,
    431  1.8.16.3      yamt 			    tred->units, tred->sensor));
    432       1.1   thorpej 		}
    433  1.8.16.3      yamt 		tred->sensor = oidx;
    434  1.8.16.6      yamt 		sysmon_envsys_release(sme);
    435  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    436  1.8.16.3      yamt 
    437       1.1   thorpej 		break;
    438       1.1   thorpej 	    }
    439       1.1   thorpej 	case ENVSYS_GTREINFO:
    440       1.1   thorpej 	    {
    441  1.8.16.3      yamt 		struct envsys_basic_info *binfo = (void *)data;
    442  1.8.16.3      yamt 		envsys_data_t *edata = NULL;
    443  1.8.16.6      yamt 		bool found = false;
    444       1.1   thorpej 
    445       1.1   thorpej 		binfo->validflags = 0;
    446       1.1   thorpej 
    447  1.8.16.6      yamt 		mutex_enter(&sme_mtx);
    448  1.8.16.3      yamt 		sme = sysmon_envsys_find_40(binfo->sensor);
    449  1.8.16.6      yamt 		if (!sme) {
    450  1.8.16.6      yamt 			mutex_exit(&sme_mtx);
    451       1.1   thorpej 			break;
    452  1.8.16.6      yamt 		}
    453  1.8.16.3      yamt 
    454       1.1   thorpej 		oidx = binfo->sensor;
    455       1.1   thorpej 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    456  1.8.16.3      yamt 
    457  1.8.16.6      yamt 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    458  1.8.16.6      yamt 			if (edata->sensor == binfo->sensor) {
    459  1.8.16.6      yamt 				found = true;
    460  1.8.16.6      yamt 				break;
    461  1.8.16.6      yamt 			}
    462  1.8.16.6      yamt 		}
    463  1.8.16.6      yamt 
    464  1.8.16.6      yamt 		if (!found) {
    465  1.8.16.6      yamt 			sysmon_envsys_release(sme);
    466  1.8.16.6      yamt 			mutex_exit(&sme_mtx);
    467  1.8.16.6      yamt 			error = ENODEV;
    468  1.8.16.6      yamt 			break;
    469  1.8.16.6      yamt 		}
    470  1.8.16.3      yamt 
    471  1.8.16.3      yamt 		binfo->validflags |= ENVSYS_FVALID;
    472  1.8.16.3      yamt 
    473  1.8.16.3      yamt 		if (binfo->sensor < sme->sme_nsensors) {
    474  1.8.16.5      yamt 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    475  1.8.16.5      yamt 				binfo->units = ENVSYS_INDICATOR;
    476  1.8.16.5      yamt 			else
    477  1.8.16.6      yamt 				binfo->units = edata->units;
    478  1.8.16.5      yamt 
    479  1.8.16.5      yamt 			/*
    480  1.8.16.5      yamt 			 * previously, the ACPI sensor names included the
    481  1.8.16.5      yamt 			 * device name. Include that in compatibility code.
    482  1.8.16.5      yamt 			 */
    483  1.8.16.5      yamt 			if (strncmp(sme->sme_name, "acpi", 4) == 0)
    484  1.8.16.5      yamt 				(void)snprintf(binfo->desc, sizeof(binfo->desc),
    485  1.8.16.5      yamt 				    "%s %s", sme->sme_name, edata->desc);
    486  1.8.16.5      yamt 			else
    487  1.8.16.5      yamt 				(void)strlcpy(binfo->desc, edata->desc,
    488  1.8.16.5      yamt 				    sizeof(binfo->desc));
    489  1.8.16.3      yamt 		}
    490  1.8.16.3      yamt 
    491  1.8.16.3      yamt 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    492  1.8.16.3      yamt 		    __func__, binfo->units, binfo->validflags));
    493  1.8.16.3      yamt 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    494  1.8.16.3      yamt 		    __func__, binfo->desc, binfo->sensor));
    495  1.8.16.3      yamt 
    496       1.1   thorpej 		binfo->sensor = oidx;
    497  1.8.16.6      yamt 		sysmon_envsys_release(sme);
    498  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    499  1.8.16.3      yamt 
    500       1.1   thorpej 		break;
    501       1.1   thorpej 	    }
    502       1.1   thorpej 	default:
    503       1.1   thorpej 		error = ENOTTY;
    504  1.8.16.3      yamt 		break;
    505       1.1   thorpej 	}
    506       1.1   thorpej 
    507  1.8.16.3      yamt 	return error;
    508       1.1   thorpej }
    509       1.1   thorpej 
    510       1.1   thorpej /*
    511  1.8.16.6      yamt  * sysmon_envsys_create:
    512  1.8.16.6      yamt  *
    513  1.8.16.6      yamt  * 	+ Allocates a new sysmon_envsys object and initializes the
    514  1.8.16.6      yamt  * 	  stuff for sensors and events.
    515  1.8.16.6      yamt  */
    516  1.8.16.6      yamt struct sysmon_envsys *
    517  1.8.16.6      yamt sysmon_envsys_create(void)
    518  1.8.16.6      yamt {
    519  1.8.16.6      yamt 	struct sysmon_envsys *sme;
    520  1.8.16.6      yamt 
    521  1.8.16.6      yamt 	sme = kmem_zalloc(sizeof(*sme), KM_SLEEP);
    522  1.8.16.6      yamt 	TAILQ_INIT(&sme->sme_sensors_list);
    523  1.8.16.6      yamt 	LIST_INIT(&sme->sme_events_list);
    524  1.8.16.7      yamt 	callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
    525  1.8.16.6      yamt 
    526  1.8.16.6      yamt 	return sme;
    527  1.8.16.6      yamt }
    528  1.8.16.6      yamt 
    529  1.8.16.6      yamt /*
    530  1.8.16.6      yamt  * sysmon_envsys_destroy:
    531  1.8.16.6      yamt  *
    532  1.8.16.8      yamt  * 	+ Removes all sensors from the tail queue, destroys the callout
    533  1.8.16.8      yamt  * 	  and frees the sysmon_envsys object.
    534  1.8.16.6      yamt  */
    535  1.8.16.6      yamt void
    536  1.8.16.6      yamt sysmon_envsys_destroy(struct sysmon_envsys *sme)
    537  1.8.16.6      yamt {
    538  1.8.16.6      yamt 	envsys_data_t *edata;
    539  1.8.16.6      yamt 
    540  1.8.16.6      yamt 	KASSERT(sme != NULL);
    541  1.8.16.6      yamt 
    542  1.8.16.6      yamt 	while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    543  1.8.16.6      yamt 		edata = TAILQ_FIRST(&sme->sme_sensors_list);
    544  1.8.16.6      yamt 		TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    545  1.8.16.6      yamt 	}
    546  1.8.16.8      yamt 	callout_destroy(&sme->sme_callout);
    547  1.8.16.6      yamt 	kmem_free(sme, sizeof(*sme));
    548  1.8.16.6      yamt }
    549  1.8.16.6      yamt 
    550  1.8.16.6      yamt /*
    551  1.8.16.6      yamt  * sysmon_envsys_sensor_attach:
    552  1.8.16.6      yamt  *
    553  1.8.16.6      yamt  * 	+ Attachs a sensor into a sysmon_envsys device checking that units
    554  1.8.16.6      yamt  * 	  is set to a valid type and description is unique and not empty.
    555  1.8.16.6      yamt  */
    556  1.8.16.6      yamt int
    557  1.8.16.6      yamt sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata)
    558  1.8.16.6      yamt {
    559  1.8.16.6      yamt 	const struct sme_description_table *sdt_units;
    560  1.8.16.6      yamt 	envsys_data_t *oedata;
    561  1.8.16.6      yamt 	int i;
    562  1.8.16.6      yamt 
    563  1.8.16.6      yamt 	KASSERT(sme != NULL || edata != NULL);
    564  1.8.16.6      yamt 
    565  1.8.16.6      yamt 	/*
    566  1.8.16.6      yamt 	 * Find the correct units for this sensor.
    567  1.8.16.6      yamt 	 */
    568  1.8.16.6      yamt 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
    569  1.8.16.6      yamt 	for (i = 0; sdt_units[i].type != -1; i++)
    570  1.8.16.6      yamt 		if (sdt_units[i].type == edata->units)
    571  1.8.16.6      yamt 			break;
    572  1.8.16.6      yamt 
    573  1.8.16.6      yamt 	if (strcmp(sdt_units[i].desc, "unknown") == 0)
    574  1.8.16.6      yamt 		return EINVAL;
    575  1.8.16.6      yamt 
    576  1.8.16.6      yamt 	/*
    577  1.8.16.6      yamt 	 * Check that description is not empty or duplicate.
    578  1.8.16.6      yamt 	 */
    579  1.8.16.6      yamt 	if (strlen(edata->desc) == 0)
    580  1.8.16.6      yamt 		return EINVAL;
    581  1.8.16.6      yamt 
    582  1.8.16.6      yamt 	mutex_enter(&sme_mtx);
    583  1.8.16.6      yamt 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    584  1.8.16.6      yamt 		if (strcmp(oedata->desc, edata->desc) == 0) {
    585  1.8.16.6      yamt 			mutex_exit(&sme_mtx);
    586  1.8.16.6      yamt 			return EEXIST;
    587  1.8.16.6      yamt 		}
    588  1.8.16.6      yamt 	}
    589  1.8.16.6      yamt 	/*
    590  1.8.16.6      yamt 	 * Ok, the sensor has been added into the device queue.
    591  1.8.16.6      yamt 	 */
    592  1.8.16.6      yamt 	TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head);
    593  1.8.16.6      yamt 
    594  1.8.16.6      yamt 	/*
    595  1.8.16.6      yamt 	 * Give the sensor a index position.
    596  1.8.16.6      yamt 	 */
    597  1.8.16.6      yamt 	edata->sensor = sme->sme_nsensors;
    598  1.8.16.6      yamt 	sme->sme_nsensors++;
    599  1.8.16.6      yamt 	mutex_exit(&sme_mtx);
    600  1.8.16.6      yamt 
    601  1.8.16.6      yamt 	return 0;
    602  1.8.16.6      yamt }
    603  1.8.16.6      yamt 
    604  1.8.16.6      yamt /*
    605  1.8.16.6      yamt  * sysmon_envsys_sensor_detach:
    606  1.8.16.6      yamt  *
    607  1.8.16.6      yamt  * 	+ Detachs a sensor from a sysmon_envsys device and decrements the
    608  1.8.16.6      yamt  * 	  sensors count on success.
    609  1.8.16.6      yamt  */
    610  1.8.16.6      yamt int
    611  1.8.16.6      yamt sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata)
    612  1.8.16.6      yamt {
    613  1.8.16.6      yamt 	envsys_data_t *oedata;
    614  1.8.16.6      yamt 	bool found = false;
    615  1.8.16.6      yamt 
    616  1.8.16.6      yamt 	KASSERT(sme != NULL || edata != NULL);
    617  1.8.16.6      yamt 
    618  1.8.16.6      yamt 	/*
    619  1.8.16.6      yamt 	 * Check the sensor is already on the list.
    620  1.8.16.6      yamt 	 */
    621  1.8.16.6      yamt 	mutex_enter(&sme_mtx);
    622  1.8.16.6      yamt 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    623  1.8.16.6      yamt 		if (oedata->sensor == edata->sensor) {
    624  1.8.16.6      yamt 			found = true;
    625  1.8.16.6      yamt 			break;
    626  1.8.16.6      yamt 		}
    627  1.8.16.6      yamt 	}
    628  1.8.16.6      yamt 
    629  1.8.16.6      yamt 	if (!found) {
    630  1.8.16.6      yamt 		mutex_exit(&sme_mtx);
    631  1.8.16.6      yamt 		return EINVAL;
    632  1.8.16.6      yamt 	}
    633  1.8.16.6      yamt 
    634  1.8.16.6      yamt 	/*
    635  1.8.16.6      yamt 	 * remove it and decrement the sensors count.
    636  1.8.16.6      yamt 	 */
    637  1.8.16.6      yamt 	TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    638  1.8.16.6      yamt 	sme->sme_nsensors--;
    639  1.8.16.6      yamt 	mutex_exit(&sme_mtx);
    640  1.8.16.6      yamt 
    641  1.8.16.6      yamt 	return 0;
    642  1.8.16.6      yamt }
    643  1.8.16.6      yamt 
    644  1.8.16.6      yamt 
    645  1.8.16.6      yamt /*
    646       1.1   thorpej  * sysmon_envsys_register:
    647       1.1   thorpej  *
    648  1.8.16.3      yamt  *	+ Register a sysmon envsys device.
    649  1.8.16.3      yamt  *	+ Create array of dictionaries for a device.
    650       1.1   thorpej  */
    651       1.1   thorpej int
    652       1.1   thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
    653       1.1   thorpej {
    654  1.8.16.4      yamt 	struct sme_evdrv {
    655  1.8.16.4      yamt 		SLIST_ENTRY(sme_evdrv) evdrv_head;
    656  1.8.16.4      yamt 		sme_event_drv_t *evdrv;
    657  1.8.16.4      yamt 	};
    658  1.8.16.4      yamt 	SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
    659  1.8.16.4      yamt 	struct sme_evdrv *sme_evdrv = NULL;
    660  1.8.16.3      yamt 	struct sysmon_envsys *lsme;
    661  1.8.16.6      yamt 	prop_dictionary_t dict, dict2;
    662  1.8.16.3      yamt 	prop_array_t array;
    663  1.8.16.3      yamt 	envsys_data_t *edata = NULL;
    664  1.8.16.9      yamt 	int error = 0;
    665  1.8.16.3      yamt 
    666  1.8.16.3      yamt 	KASSERT(sme != NULL);
    667  1.8.16.3      yamt 	KASSERT(sme->sme_name != NULL);
    668       1.1   thorpej 
    669  1.8.16.3      yamt 	/*
    670  1.8.16.6      yamt 	 * sanity check: if SME_DISABLE_REFRESH is not set,
    671  1.8.16.6      yamt 	 * the sme_refresh function callback must be non NULL.
    672  1.8.16.3      yamt 	 */
    673  1.8.16.6      yamt 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    674  1.8.16.6      yamt 		if (!sme->sme_refresh)
    675  1.8.16.6      yamt 			return EINVAL;
    676  1.8.16.6      yamt 
    677  1.8.16.3      yamt 	/*
    678  1.8.16.6      yamt 	 * If the list of sensors is empty, there's no point to continue...
    679  1.8.16.3      yamt 	 */
    680  1.8.16.6      yamt 	if (TAILQ_EMPTY(&sme->sme_sensors_list)) {
    681  1.8.16.6      yamt 		DPRINTF(("%s: sensors list empty for %s\n", __func__,
    682  1.8.16.6      yamt 		    sme->sme_name));
    683  1.8.16.6      yamt 		return ENOTSUP;
    684  1.8.16.3      yamt 	}
    685  1.8.16.3      yamt 
    686  1.8.16.6      yamt 	/*
    687  1.8.16.6      yamt 	 * create the device array.
    688  1.8.16.6      yamt 	 */
    689  1.8.16.3      yamt 	array = prop_array_create();
    690  1.8.16.4      yamt 	if (!array)
    691  1.8.16.3      yamt 		return ENOMEM;
    692  1.8.16.3      yamt 
    693  1.8.16.4      yamt 	/*
    694  1.8.16.4      yamt 	 * Initialize the singly linked list for driver events.
    695  1.8.16.4      yamt 	 */
    696  1.8.16.4      yamt 	SLIST_INIT(&sme_evdrv_list);
    697  1.8.16.6      yamt 
    698  1.8.16.3      yamt 	/*
    699  1.8.16.6      yamt 	 * Iterate over all sensors and create a dictionary per sensor.
    700  1.8.16.6      yamt 	 * We must respect the order in which the sensors were added.
    701  1.8.16.3      yamt 	 */
    702  1.8.16.7      yamt 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    703  1.8.16.3      yamt 		dict = prop_dictionary_create();
    704  1.8.16.4      yamt 		if (!dict) {
    705  1.8.16.3      yamt 			error = ENOMEM;
    706  1.8.16.3      yamt 			goto out2;
    707  1.8.16.3      yamt 		}
    708       1.1   thorpej 
    709  1.8.16.3      yamt 		/*
    710  1.8.16.3      yamt 		 * Create all objects in sensor's dictionary.
    711  1.8.16.3      yamt 		 */
    712  1.8.16.4      yamt 		sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
    713  1.8.16.4      yamt 		sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
    714  1.8.16.4      yamt 					array, dict, edata);
    715  1.8.16.4      yamt 		if (sme_evdrv->evdrv)
    716  1.8.16.4      yamt 			SLIST_INSERT_HEAD(&sme_evdrv_list,
    717  1.8.16.4      yamt 					  sme_evdrv, evdrv_head);
    718  1.8.16.3      yamt 	}
    719  1.8.16.3      yamt 
    720  1.8.16.3      yamt 	/*
    721  1.8.16.3      yamt 	 * Check if requested sysmon_envsys device is valid
    722  1.8.16.3      yamt 	 * and does not exist already in the list.
    723  1.8.16.3      yamt 	 */
    724  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    725  1.8.16.3      yamt 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    726  1.8.16.3      yamt 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    727  1.8.16.3      yamt 		       error = EEXIST;
    728  1.8.16.3      yamt 		       goto out;
    729  1.8.16.3      yamt 	       }
    730  1.8.16.3      yamt 	}
    731  1.8.16.4      yamt 
    732  1.8.16.3      yamt 	/*
    733  1.8.16.3      yamt 	 * If the array does not contain any object (sensor), there's
    734  1.8.16.3      yamt 	 * no need to attach the driver.
    735  1.8.16.3      yamt 	 */
    736  1.8.16.3      yamt 	if (prop_array_count(array) == 0) {
    737       1.1   thorpej 		error = EINVAL;
    738  1.8.16.3      yamt 		DPRINTF(("%s: empty array for '%s'\n", __func__,
    739  1.8.16.3      yamt 		    sme->sme_name));
    740       1.1   thorpej 		goto out;
    741       1.1   thorpej 	}
    742  1.8.16.6      yamt 
    743  1.8.16.6      yamt 	/*
    744  1.8.16.6      yamt 	 * Add the dictionary for the global properties of this device.
    745  1.8.16.6      yamt 	 */
    746  1.8.16.6      yamt 	dict2 = prop_dictionary_create();
    747  1.8.16.6      yamt 	if (!dict2) {
    748  1.8.16.6      yamt 		error = ENOMEM;
    749  1.8.16.6      yamt 		goto out;
    750  1.8.16.6      yamt 	}
    751  1.8.16.6      yamt 
    752  1.8.16.6      yamt 	error = sme_add_property_dictionary(sme, array, dict2);
    753  1.8.16.6      yamt 	if (error) {
    754  1.8.16.6      yamt 		prop_object_release(dict2);
    755  1.8.16.6      yamt 		goto out;
    756  1.8.16.6      yamt 	}
    757  1.8.16.6      yamt 
    758  1.8.16.3      yamt 	/*
    759  1.8.16.3      yamt 	 * Add the array into the global dictionary for the driver.
    760  1.8.16.3      yamt 	 *
    761  1.8.16.3      yamt 	 * <dict>
    762  1.8.16.3      yamt 	 * 	<key>foo0</key>
    763  1.8.16.3      yamt 	 * 	<array>
    764  1.8.16.3      yamt 	 * 		...
    765  1.8.16.3      yamt 	 */
    766  1.8.16.3      yamt 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    767  1.8.16.3      yamt 		error = EINVAL;
    768  1.8.16.3      yamt 		DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
    769  1.8.16.3      yamt 		    sme->sme_name));
    770  1.8.16.3      yamt 		goto out;
    771  1.8.16.3      yamt 	}
    772  1.8.16.3      yamt 	/*
    773  1.8.16.3      yamt 	 * Add the device into the list.
    774  1.8.16.3      yamt 	 */
    775  1.8.16.4      yamt 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    776       1.1   thorpej 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    777       1.1   thorpej 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    778  1.8.16.3      yamt out:
    779  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    780  1.8.16.4      yamt 
    781  1.8.16.6      yamt 	/*
    782  1.8.16.9      yamt 	 * No errors? register the events that were set in the driver
    783  1.8.16.9      yamt 	 * and make an initial data refresh if was requested.
    784  1.8.16.6      yamt 	 */
    785  1.8.16.4      yamt 	if (error == 0) {
    786  1.8.16.9      yamt 		sysmon_task_queue_init();
    787  1.8.16.4      yamt 		SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
    788  1.8.16.4      yamt 			sysmon_task_queue_sched(0,
    789  1.8.16.4      yamt 			    sme_event_drvadd, sme_evdrv->evdrv);
    790  1.8.16.4      yamt 		}
    791  1.8.16.4      yamt 		DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
    792  1.8.16.4      yamt 		    __func__, sme->sme_name, sme->sme_nsensors));
    793  1.8.16.9      yamt 
    794  1.8.16.9      yamt 		if (sme->sme_flags & SME_INIT_REFRESH)
    795  1.8.16.9      yamt 			sysmon_task_queue_sched(0, sme_initial_refresh, sme);
    796  1.8.16.4      yamt 	}
    797  1.8.16.4      yamt 
    798  1.8.16.3      yamt out2:
    799  1.8.16.4      yamt 	while (!SLIST_EMPTY(&sme_evdrv_list)) {
    800  1.8.16.4      yamt 		sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
    801  1.8.16.4      yamt 		SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
    802  1.8.16.4      yamt 		kmem_free(sme_evdrv, sizeof(*sme_evdrv));
    803  1.8.16.4      yamt 	}
    804  1.8.16.4      yamt 	if (!error)
    805  1.8.16.4      yamt 		return 0;
    806  1.8.16.4      yamt 
    807  1.8.16.6      yamt 	/*
    808  1.8.16.6      yamt 	 * Ugh... something wasn't right; unregister all events and sensors
    809  1.8.16.6      yamt 	 * previously assigned and destroy the array with all its objects.
    810  1.8.16.6      yamt 	 */
    811  1.8.16.3      yamt 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    812  1.8.16.3      yamt 	    sme->sme_name, error));
    813  1.8.16.4      yamt 	if (error != EEXIST) {
    814  1.8.16.4      yamt 		mutex_enter(&sme_mtx);
    815  1.8.16.6      yamt 		sme_event_unregister_all(sme);
    816  1.8.16.6      yamt 		while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    817  1.8.16.6      yamt 			edata = TAILQ_FIRST(&sme->sme_sensors_list);
    818  1.8.16.6      yamt 			TAILQ_REMOVE(&sme->sme_sensors_list, edata,
    819  1.8.16.6      yamt 			    sensors_head);
    820  1.8.16.6      yamt 		}
    821  1.8.16.4      yamt 		mutex_exit(&sme_mtx);
    822  1.8.16.4      yamt 	}
    823  1.8.16.3      yamt 	sysmon_envsys_destroy_plist(array);
    824  1.8.16.3      yamt 	return error;
    825  1.8.16.3      yamt }
    826  1.8.16.3      yamt 
    827  1.8.16.3      yamt /*
    828  1.8.16.3      yamt  * sysmon_envsys_destroy_plist:
    829  1.8.16.3      yamt  *
    830  1.8.16.3      yamt  * 	+ Remove all objects from the array of dictionaries that is
    831  1.8.16.3      yamt  * 	  created in a sysmon envsys device.
    832  1.8.16.3      yamt  */
    833  1.8.16.3      yamt static void
    834  1.8.16.3      yamt sysmon_envsys_destroy_plist(prop_array_t array)
    835  1.8.16.3      yamt {
    836  1.8.16.3      yamt 	prop_object_iterator_t iter, iter2;
    837  1.8.16.4      yamt 	prop_dictionary_t dict;
    838  1.8.16.3      yamt 	prop_object_t obj;
    839  1.8.16.3      yamt 
    840  1.8.16.3      yamt 	KASSERT(array != NULL);
    841  1.8.16.3      yamt 
    842  1.8.16.4      yamt 	DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
    843  1.8.16.4      yamt 	    prop_array_count(array)));
    844  1.8.16.4      yamt 
    845  1.8.16.3      yamt 	iter = prop_array_iterator(array);
    846  1.8.16.4      yamt 	if (!iter)
    847  1.8.16.3      yamt 		return;
    848  1.8.16.3      yamt 
    849  1.8.16.4      yamt 	while ((dict = prop_object_iterator_next(iter))) {
    850  1.8.16.4      yamt 		KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
    851  1.8.16.3      yamt 		iter2 = prop_dictionary_iterator(dict);
    852  1.8.16.4      yamt 		if (!iter2)
    853  1.8.16.4      yamt 			goto out;
    854  1.8.16.4      yamt 		DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
    855  1.8.16.4      yamt 		while ((obj = prop_object_iterator_next(iter2)) != NULL) {
    856  1.8.16.4      yamt 			DPRINTFOBJ(("%s: obj=%s\n", __func__,
    857  1.8.16.4      yamt 			    prop_dictionary_keysym_cstring_nocopy(obj)));
    858  1.8.16.4      yamt 			prop_dictionary_remove(dict,
    859  1.8.16.4      yamt 			    prop_dictionary_keysym_cstring_nocopy(obj));
    860  1.8.16.4      yamt 			prop_object_iterator_reset(iter2);
    861  1.8.16.4      yamt 		}
    862  1.8.16.4      yamt 		prop_object_iterator_release(iter2);
    863  1.8.16.4      yamt 		DPRINTFOBJ(("%s: objects in dictionary:%d\n",
    864  1.8.16.4      yamt 		    __func__, prop_dictionary_count(dict)));
    865  1.8.16.3      yamt 		prop_object_release(dict);
    866  1.8.16.3      yamt 	}
    867  1.8.16.3      yamt 
    868  1.8.16.4      yamt out:
    869  1.8.16.3      yamt 	prop_object_iterator_release(iter);
    870  1.8.16.4      yamt 	prop_object_release(array);
    871       1.1   thorpej }
    872       1.1   thorpej 
    873       1.1   thorpej /*
    874       1.1   thorpej  * sysmon_envsys_unregister:
    875       1.1   thorpej  *
    876  1.8.16.3      yamt  *	+ Unregister a sysmon envsys device.
    877       1.1   thorpej  */
    878       1.1   thorpej void
    879       1.1   thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
    880       1.1   thorpej {
    881  1.8.16.3      yamt 	prop_array_t array;
    882  1.8.16.3      yamt 
    883  1.8.16.3      yamt 	KASSERT(sme != NULL);
    884       1.1   thorpej 
    885  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
    886  1.8.16.6      yamt 	/*
    887  1.8.16.6      yamt 	 * Wait for device to be available.
    888  1.8.16.6      yamt 	 */
    889  1.8.16.6      yamt 	while (sme->sme_flags & SME_FLAG_BUSY)
    890  1.8.16.4      yamt 		cv_wait(&sme_cv, &sme_mtx);
    891  1.8.16.3      yamt 	/*
    892  1.8.16.6      yamt 	 * Stop the callout.
    893  1.8.16.3      yamt 	 */
    894  1.8.16.6      yamt 	callout_stop(&sme->sme_callout);
    895  1.8.16.6      yamt 	/*
    896  1.8.16.6      yamt 	 * Decrement global sensors counter (only useful for compatibility).
    897  1.8.16.6      yamt 	 */
    898  1.8.16.6      yamt 	sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
    899  1.8.16.3      yamt 	/*
    900  1.8.16.3      yamt 	 * Unregister all events associated with this device.
    901  1.8.16.3      yamt 	 */
    902  1.8.16.6      yamt 	sme_event_unregister_all(sme);
    903  1.8.16.4      yamt 	LIST_REMOVE(sme, sme_list);
    904  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
    905  1.8.16.3      yamt 	/*
    906  1.8.16.3      yamt 	 * Remove the device (and all its objects) from the global dictionary.
    907  1.8.16.3      yamt 	 */
    908  1.8.16.3      yamt 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    909  1.8.16.4      yamt 	if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
    910  1.8.16.3      yamt 		prop_dictionary_remove(sme_propd, sme->sme_name);
    911  1.8.16.4      yamt 		sysmon_envsys_destroy_plist(array);
    912  1.8.16.3      yamt 	}
    913  1.8.16.6      yamt 	/*
    914  1.8.16.6      yamt 	 * And finally destroy the sysmon_envsys object.
    915  1.8.16.6      yamt 	 */
    916  1.8.16.6      yamt 	sysmon_envsys_destroy(sme);
    917       1.1   thorpej }
    918       1.1   thorpej 
    919       1.1   thorpej /*
    920       1.1   thorpej  * sysmon_envsys_find:
    921       1.1   thorpej  *
    922  1.8.16.6      yamt  *	+ Find a sysmon envsys device and mark it as busy if found.
    923       1.1   thorpej  */
    924       1.1   thorpej struct sysmon_envsys *
    925  1.8.16.3      yamt sysmon_envsys_find(const char *name)
    926       1.1   thorpej {
    927       1.1   thorpej 	struct sysmon_envsys *sme;
    928       1.1   thorpej 
    929  1.8.16.6      yamt 	KASSERT(mutex_owned(&sme_mtx));
    930  1.8.16.6      yamt 
    931       1.7      yamt again:
    932  1.8.16.7      yamt 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    933  1.8.16.7      yamt 		if (strcmp(sme->sme_name, name) == 0) {
    934  1.8.16.7      yamt 			if (sme->sme_flags & SME_FLAG_BUSY) {
    935  1.8.16.7      yamt 				cv_wait(&sme_cv, &sme_mtx);
    936  1.8.16.7      yamt 				goto again;
    937       1.7      yamt 			}
    938  1.8.16.7      yamt 			sme->sme_flags |= SME_FLAG_BUSY;
    939  1.8.16.7      yamt 			break;
    940  1.8.16.7      yamt 		}
    941       1.1   thorpej 	}
    942       1.7      yamt 	return sme;
    943       1.1   thorpej }
    944       1.1   thorpej 
    945       1.1   thorpej /*
    946  1.8.16.6      yamt  * sysmon_envsys_acquire:
    947  1.8.16.6      yamt  *
    948  1.8.16.6      yamt  * 	+ Acquire priviledge to a sysmon envsys device (locked).
    949  1.8.16.6      yamt  */
    950  1.8.16.6      yamt void
    951  1.8.16.6      yamt sysmon_envsys_acquire(struct sysmon_envsys *sme)
    952  1.8.16.6      yamt {
    953  1.8.16.6      yamt 	KASSERT(mutex_owned(&sme_mtx));
    954  1.8.16.6      yamt 
    955  1.8.16.6      yamt 	while (sme->sme_flags & SME_FLAG_BUSY)
    956  1.8.16.6      yamt 		cv_wait(&sme_cv, &sme_mtx);
    957  1.8.16.6      yamt 
    958  1.8.16.6      yamt 	sme->sme_flags |= SME_FLAG_BUSY;
    959  1.8.16.6      yamt }
    960  1.8.16.6      yamt 
    961  1.8.16.6      yamt /*
    962       1.1   thorpej  * sysmon_envsys_release:
    963       1.1   thorpej  *
    964  1.8.16.6      yamt  * 	+ Release a sysmon envsys device (locked).
    965       1.1   thorpej  */
    966       1.1   thorpej void
    967       1.1   thorpej sysmon_envsys_release(struct sysmon_envsys *sme)
    968       1.1   thorpej {
    969  1.8.16.6      yamt 	KASSERT(mutex_owned(&sme_mtx));
    970  1.8.16.6      yamt 
    971  1.8.16.6      yamt 	sme->sme_flags &= ~SME_FLAG_BUSY;
    972  1.8.16.6      yamt 	cv_broadcast(&sme_cv);
    973  1.8.16.3      yamt }
    974  1.8.16.3      yamt 
    975  1.8.16.3      yamt /* compatibility function */
    976  1.8.16.3      yamt struct sysmon_envsys *
    977  1.8.16.3      yamt sysmon_envsys_find_40(u_int idx)
    978  1.8.16.3      yamt {
    979  1.8.16.3      yamt 	struct sysmon_envsys *sme;
    980  1.8.16.3      yamt 
    981  1.8.16.6      yamt 	KASSERT(mutex_owned(&sme_mtx));
    982  1.8.16.6      yamt 
    983  1.8.16.7      yamt again:
    984  1.8.16.7      yamt 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    985  1.8.16.3      yamt 		if (idx >= sme->sme_fsensor &&
    986  1.8.16.6      yamt 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors)) {
    987  1.8.16.7      yamt 			if (sme->sme_flags & SME_FLAG_BUSY) {
    988  1.8.16.7      yamt 				cv_wait(&sme_cv, &sme_mtx);
    989  1.8.16.7      yamt 				goto again;
    990  1.8.16.7      yamt 			}
    991  1.8.16.6      yamt 			sme->sme_flags |= SME_FLAG_BUSY;
    992  1.8.16.3      yamt 			break;
    993  1.8.16.6      yamt 		}
    994  1.8.16.3      yamt 	}
    995  1.8.16.3      yamt 	return sme;
    996  1.8.16.3      yamt }
    997  1.8.16.3      yamt 
    998  1.8.16.3      yamt /*
    999  1.8.16.9      yamt  * sme_initial_refresh:
   1000  1.8.16.9      yamt  *
   1001  1.8.16.9      yamt  * 	+ Do an initial refresh of the sensors in a device just after
   1002  1.8.16.9      yamt  * 	  interrupts are enabled in the autoconf(9) process.
   1003  1.8.16.9      yamt  *
   1004  1.8.16.9      yamt  */
   1005  1.8.16.9      yamt static void
   1006  1.8.16.9      yamt sme_initial_refresh(void *arg)
   1007  1.8.16.9      yamt {
   1008  1.8.16.9      yamt 	struct sysmon_envsys *sme = arg;
   1009  1.8.16.9      yamt 	envsys_data_t *edata;
   1010  1.8.16.9      yamt 
   1011  1.8.16.9      yamt 	mutex_enter(&sme_mtx);
   1012  1.8.16.9      yamt 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head)
   1013  1.8.16.9      yamt 		(*sme->sme_refresh)(sme, edata);
   1014  1.8.16.9      yamt 	mutex_exit(&sme_mtx);
   1015  1.8.16.9      yamt }
   1016  1.8.16.9      yamt 
   1017  1.8.16.9      yamt /*
   1018  1.8.16.4      yamt  * sme_sensor_dictionary_get:
   1019  1.8.16.4      yamt  *
   1020  1.8.16.6      yamt  * 	+ Returns a dictionary of a device specified by its index
   1021  1.8.16.6      yamt  * 	  position.
   1022  1.8.16.4      yamt  */
   1023  1.8.16.4      yamt prop_dictionary_t
   1024  1.8.16.4      yamt sme_sensor_dictionary_get(prop_array_t array, const char *index)
   1025  1.8.16.4      yamt {
   1026  1.8.16.4      yamt 	prop_object_iterator_t iter;
   1027  1.8.16.4      yamt 	prop_dictionary_t dict;
   1028  1.8.16.4      yamt 	prop_object_t obj;
   1029  1.8.16.4      yamt 
   1030  1.8.16.4      yamt 	KASSERT(array != NULL || index != NULL);
   1031  1.8.16.4      yamt 
   1032  1.8.16.4      yamt 	iter = prop_array_iterator(array);
   1033  1.8.16.4      yamt 	if (!iter)
   1034  1.8.16.4      yamt 		return NULL;
   1035  1.8.16.4      yamt 
   1036  1.8.16.4      yamt 	while ((dict = prop_object_iterator_next(iter))) {
   1037  1.8.16.4      yamt 		obj = prop_dictionary_get(dict, "index");
   1038  1.8.16.4      yamt 		if (prop_string_equals_cstring(obj, index))
   1039  1.8.16.4      yamt 			break;
   1040  1.8.16.4      yamt 	}
   1041  1.8.16.4      yamt 
   1042  1.8.16.4      yamt 	prop_object_iterator_release(iter);
   1043  1.8.16.4      yamt 	return dict;
   1044  1.8.16.4      yamt }
   1045  1.8.16.4      yamt 
   1046  1.8.16.4      yamt /*
   1047  1.8.16.4      yamt  * sme_remove_userprops:
   1048  1.8.16.4      yamt  *
   1049  1.8.16.4      yamt  * 	+ Remove all properties from all devices that were set by
   1050  1.8.16.6      yamt  * 	  the ENVSYS_SETDICTIONARY ioctl.
   1051  1.8.16.4      yamt  */
   1052  1.8.16.4      yamt static void
   1053  1.8.16.4      yamt sme_remove_userprops(void)
   1054  1.8.16.4      yamt {
   1055  1.8.16.4      yamt 	struct sysmon_envsys *sme;
   1056  1.8.16.4      yamt 	prop_array_t array;
   1057  1.8.16.4      yamt 	prop_dictionary_t sdict;
   1058  1.8.16.4      yamt 	envsys_data_t *edata = NULL;
   1059  1.8.16.4      yamt 	char tmp[ENVSYS_DESCLEN];
   1060  1.8.16.6      yamt 	int ptype;
   1061  1.8.16.4      yamt 
   1062  1.8.16.4      yamt 	mutex_enter(&sme_mtx);
   1063  1.8.16.4      yamt 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   1064  1.8.16.6      yamt 		sysmon_envsys_acquire(sme);
   1065  1.8.16.4      yamt 		array = prop_dictionary_get(sme_propd, sme->sme_name);
   1066  1.8.16.4      yamt 
   1067  1.8.16.6      yamt 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1068  1.8.16.4      yamt 			(void)snprintf(tmp, sizeof(tmp), "sensor%d",
   1069  1.8.16.4      yamt 				       edata->sensor);
   1070  1.8.16.4      yamt 			sdict = sme_sensor_dictionary_get(array, tmp);
   1071  1.8.16.6      yamt 			KASSERT(sdict != NULL);
   1072  1.8.16.4      yamt 
   1073  1.8.16.4      yamt 			if (edata->upropset & USERPROP_BATTCAP) {
   1074  1.8.16.4      yamt 				prop_dictionary_remove(sdict,
   1075  1.8.16.4      yamt 				    "critical-capacity");
   1076  1.8.16.4      yamt 				ptype = PENVSYS_EVENT_BATT_USERCAP;
   1077  1.8.16.6      yamt 				sme_event_unregister(sme, edata->desc, ptype);
   1078  1.8.16.4      yamt 			}
   1079  1.8.16.4      yamt 
   1080  1.8.16.4      yamt 			if (edata->upropset & USERPROP_CRITMAX) {
   1081  1.8.16.4      yamt 				prop_dictionary_remove(sdict,
   1082  1.8.16.4      yamt 				    "critical-max");
   1083  1.8.16.4      yamt 				ptype = PENVSYS_EVENT_USER_CRITMAX;
   1084  1.8.16.6      yamt 				sme_event_unregister(sme, edata->desc, ptype);
   1085  1.8.16.4      yamt 			}
   1086  1.8.16.4      yamt 
   1087  1.8.16.4      yamt 			if (edata->upropset & USERPROP_CRITMIN) {
   1088  1.8.16.4      yamt 				prop_dictionary_remove(sdict,
   1089  1.8.16.4      yamt 				    "critical-min");
   1090  1.8.16.4      yamt 				ptype = PENVSYS_EVENT_USER_CRITMIN;
   1091  1.8.16.6      yamt 				sme_event_unregister(sme, edata->desc, ptype);
   1092  1.8.16.4      yamt 			}
   1093  1.8.16.4      yamt 
   1094  1.8.16.4      yamt 			if (edata->upropset & USERPROP_RFACT) {
   1095  1.8.16.4      yamt 				(void)sme_sensor_upint32(sdict, "rfact", 0);
   1096  1.8.16.4      yamt 				edata->rfact = 0;
   1097  1.8.16.4      yamt 			}
   1098  1.8.16.4      yamt 
   1099  1.8.16.4      yamt 			if (edata->upropset & USERPROP_DESC)
   1100  1.8.16.4      yamt 				(void)sme_sensor_upstring(sdict,
   1101  1.8.16.4      yamt 			  	    "description", edata->desc);
   1102  1.8.16.6      yamt 
   1103  1.8.16.6      yamt 			if (edata->upropset)
   1104  1.8.16.6      yamt 				edata->upropset = 0;
   1105  1.8.16.4      yamt 		}
   1106  1.8.16.4      yamt 
   1107  1.8.16.6      yamt 		/*
   1108  1.8.16.6      yamt 		 * Restore default timeout value.
   1109  1.8.16.6      yamt 		 */
   1110  1.8.16.6      yamt 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1111  1.8.16.6      yamt 		sysmon_envsys_release(sme);
   1112  1.8.16.4      yamt 	}
   1113  1.8.16.4      yamt 	mutex_exit(&sme_mtx);
   1114  1.8.16.4      yamt }
   1115  1.8.16.6      yamt 
   1116  1.8.16.4      yamt /*
   1117  1.8.16.6      yamt  * sme_add_property_dictionary:
   1118  1.8.16.6      yamt  *
   1119  1.8.16.6      yamt  * 	+ Add global properties into a device.
   1120  1.8.16.3      yamt  */
   1121  1.8.16.3      yamt static int
   1122  1.8.16.6      yamt sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1123  1.8.16.6      yamt 			    prop_dictionary_t dict)
   1124  1.8.16.3      yamt {
   1125  1.8.16.6      yamt 	prop_dictionary_t pdict;
   1126  1.8.16.6      yamt 	int error = 0;
   1127  1.8.16.3      yamt 
   1128  1.8.16.6      yamt 	pdict = prop_dictionary_create();
   1129  1.8.16.6      yamt 	if (!pdict)
   1130  1.8.16.6      yamt 		return EINVAL;
   1131  1.8.16.3      yamt 
   1132  1.8.16.6      yamt 	/*
   1133  1.8.16.6      yamt 	 * Add the 'refresh-timeout' object into the 'device-properties'
   1134  1.8.16.6      yamt 	 * dictionary. We use by default 30 seconds.
   1135  1.8.16.6      yamt 	 *
   1136  1.8.16.6      yamt 	 * 	...
   1137  1.8.16.6      yamt 	 * 	<dict>
   1138  1.8.16.6      yamt 	 * 		<key>device-properties</key>
   1139  1.8.16.6      yamt 	 * 		<dict>
   1140  1.8.16.6      yamt 	 * 			<key>refresh-timeout</key>
   1141  1.8.16.6      yamt 	 * 			<integer>120</integer<
   1142  1.8.16.6      yamt 	 * 		</dict<
   1143  1.8.16.6      yamt 	 * 	</dict>
   1144  1.8.16.6      yamt 	 * 	...
   1145  1.8.16.6      yamt 	 *
   1146  1.8.16.6      yamt 	 */
   1147  1.8.16.6      yamt 	if (!sme->sme_events_timeout)
   1148  1.8.16.6      yamt 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1149  1.8.16.6      yamt 
   1150  1.8.16.6      yamt 	if (!prop_dictionary_set_uint64(pdict, "refresh-timeout",
   1151  1.8.16.6      yamt 					sme->sme_events_timeout)) {
   1152  1.8.16.6      yamt 		error = EINVAL;
   1153  1.8.16.6      yamt 		goto out;
   1154  1.8.16.3      yamt 	}
   1155  1.8.16.3      yamt 
   1156  1.8.16.6      yamt 	if (!prop_dictionary_set(dict, "device-properties", pdict)) {
   1157  1.8.16.6      yamt 		error = EINVAL;
   1158  1.8.16.6      yamt 		goto out;
   1159  1.8.16.6      yamt 	}
   1160  1.8.16.3      yamt 
   1161  1.8.16.6      yamt 	/*
   1162  1.8.16.6      yamt 	 * Add the device dictionary into the sysmon envsys array.
   1163  1.8.16.6      yamt 	 */
   1164  1.8.16.6      yamt 	if (!prop_array_add(array, dict))
   1165  1.8.16.6      yamt 		error = EINVAL;
   1166  1.8.16.3      yamt 
   1167  1.8.16.6      yamt out:
   1168  1.8.16.6      yamt 	prop_object_release(pdict);
   1169  1.8.16.6      yamt 	return error;
   1170  1.8.16.3      yamt }
   1171  1.8.16.3      yamt 
   1172  1.8.16.3      yamt /*
   1173  1.8.16.3      yamt  * sme_add_sensor_dictionary:
   1174  1.8.16.3      yamt  *
   1175  1.8.16.6      yamt  * 	+ Adds the sensor objects into the dictionary and returns a pointer
   1176  1.8.16.6      yamt  * 	  to a sme_event_drv_t object if a monitoring flag was set
   1177  1.8.16.6      yamt  * 	  (or NULL otherwise).
   1178  1.8.16.3      yamt  */
   1179  1.8.16.4      yamt sme_event_drv_t *
   1180  1.8.16.3      yamt sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1181  1.8.16.3      yamt 		    	  prop_dictionary_t dict, envsys_data_t *edata)
   1182  1.8.16.3      yamt {
   1183  1.8.16.4      yamt 	const struct sme_description_table *sdt, *sdt_units;
   1184  1.8.16.3      yamt 	sme_event_drv_t *sme_evdrv_t = NULL;
   1185  1.8.16.3      yamt 	int i, j;
   1186  1.8.16.4      yamt 	char indexstr[ENVSYS_DESCLEN];
   1187  1.8.16.3      yamt 
   1188  1.8.16.6      yamt 	/*
   1189  1.8.16.6      yamt 	 * Find the correct units for this sensor.
   1190  1.8.16.6      yamt 	 */
   1191  1.8.16.4      yamt 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
   1192  1.8.16.4      yamt 	for (i = 0; sdt_units[i].type != -1; i++)
   1193  1.8.16.4      yamt 		if (sdt_units[i].type == edata->units)
   1194  1.8.16.3      yamt 			break;
   1195  1.8.16.3      yamt 
   1196  1.8.16.3      yamt 	/*
   1197  1.8.16.4      yamt 	 * Add the index sensor string.
   1198  1.8.16.4      yamt 	 *
   1199  1.8.16.4      yamt 	 * 		...
   1200  1.8.16.6      yamt 	 * 		<key>index</eyr
   1201  1.8.16.4      yamt 	 * 		<string>sensor0</string>
   1202  1.8.16.4      yamt 	 * 		...
   1203  1.8.16.4      yamt 	 */
   1204  1.8.16.4      yamt 	(void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
   1205  1.8.16.4      yamt 	if (sme_sensor_upstring(dict, "index", indexstr))
   1206  1.8.16.6      yamt 		goto bad;
   1207  1.8.16.4      yamt 
   1208  1.8.16.4      yamt 	/*
   1209  1.8.16.3      yamt 	 * 		...
   1210  1.8.16.3      yamt 	 * 		<key>type</key>
   1211  1.8.16.3      yamt 	 * 		<string>foo</string>
   1212  1.8.16.3      yamt 	 * 		<key>description</key>
   1213  1.8.16.3      yamt 	 * 		<string>blah blah</string>
   1214  1.8.16.3      yamt 	 * 		...
   1215  1.8.16.3      yamt 	 */
   1216  1.8.16.4      yamt 	if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
   1217  1.8.16.6      yamt 		goto bad;
   1218  1.8.16.3      yamt 
   1219  1.8.16.3      yamt 	if (sme_sensor_upstring(dict, "description", edata->desc))
   1220  1.8.16.6      yamt 		goto bad;
   1221  1.8.16.3      yamt 
   1222  1.8.16.3      yamt 	/*
   1223  1.8.16.3      yamt 	 * Add sensor's state description.
   1224  1.8.16.3      yamt 	 *
   1225  1.8.16.3      yamt 	 * 		...
   1226  1.8.16.3      yamt 	 * 		<key>state</key>
   1227  1.8.16.3      yamt 	 * 		<string>valid</string>
   1228  1.8.16.3      yamt 	 * 		...
   1229  1.8.16.3      yamt 	 */
   1230  1.8.16.4      yamt 	sdt = sme_get_description_table(SME_DESC_STATES);
   1231  1.8.16.4      yamt 	for (j = 0; sdt[j].type != -1; j++)
   1232  1.8.16.4      yamt 		if (sdt[j].type == edata->state)
   1233  1.8.16.3      yamt 			break;
   1234  1.8.16.3      yamt 
   1235  1.8.16.3      yamt 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
   1236  1.8.16.3      yamt 	    __func__, edata->desc, edata->units, edata->state));
   1237  1.8.16.3      yamt 
   1238  1.8.16.4      yamt 	if (sme_sensor_upstring(dict, "state", sdt[j].desc))
   1239  1.8.16.6      yamt 		goto bad;
   1240  1.8.16.3      yamt 
   1241  1.8.16.3      yamt 	/*
   1242  1.8.16.3      yamt 	 * Add the monitoring boolean object:
   1243  1.8.16.3      yamt 	 *
   1244  1.8.16.3      yamt 	 * 		...
   1245  1.8.16.3      yamt 	 * 		<key>monitoring-supported</key>
   1246  1.8.16.3      yamt 	 * 		<true/>
   1247  1.8.16.3      yamt 	 *		...
   1248  1.8.16.3      yamt 	 *
   1249  1.8.16.5      yamt 	 * always false on Battery {capacity,charge}, Drive and Indicator types.
   1250  1.8.16.3      yamt 	 * They cannot be monitored.
   1251  1.8.16.3      yamt 	 *
   1252  1.8.16.3      yamt 	 */
   1253  1.8.16.3      yamt 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1254  1.8.16.3      yamt 	    (edata->units == ENVSYS_INDICATOR) ||
   1255  1.8.16.3      yamt 	    (edata->units == ENVSYS_DRIVE) ||
   1256  1.8.16.5      yamt 	    (edata->units == ENVSYS_BATTERY_CAPACITY) ||
   1257  1.8.16.5      yamt 	    (edata->units == ENVSYS_BATTERY_CHARGE)) {
   1258  1.8.16.3      yamt 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
   1259  1.8.16.4      yamt 			goto out;
   1260  1.8.16.3      yamt 	} else {
   1261  1.8.16.3      yamt 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
   1262  1.8.16.4      yamt 			goto out;
   1263  1.8.16.3      yamt 	}
   1264  1.8.16.3      yamt 
   1265  1.8.16.3      yamt 	/*
   1266  1.8.16.4      yamt 	 * Add the percentage boolean object, true if ENVSYS_FPERCENT
   1267  1.8.16.4      yamt 	 * is set or false otherwise.
   1268  1.8.16.3      yamt 	 *
   1269  1.8.16.3      yamt 	 * 		...
   1270  1.8.16.4      yamt 	 * 		<key>want-percentage</key>
   1271  1.8.16.4      yamt 	 * 		<true/>
   1272  1.8.16.3      yamt 	 * 		...
   1273  1.8.16.3      yamt 	 */
   1274  1.8.16.4      yamt 	if (edata->flags & ENVSYS_FPERCENT)
   1275  1.8.16.4      yamt 		if (sme_sensor_upbool(dict, "want-percentage", true))
   1276  1.8.16.4      yamt 			goto out;
   1277  1.8.16.3      yamt 
   1278  1.8.16.4      yamt 	/*
   1279  1.8.16.4      yamt 	 * Add the allow-rfact boolean object, true if
   1280  1.8.16.4      yamt 	 * ENVSYS_FCHANGERFACT if set or false otherwise.
   1281  1.8.16.4      yamt 	 *
   1282  1.8.16.4      yamt 	 * 		...
   1283  1.8.16.4      yamt 	 * 		<key>allow-rfact</key>
   1284  1.8.16.4      yamt 	 * 		<true/>
   1285  1.8.16.4      yamt 	 * 		...
   1286  1.8.16.4      yamt 	 */
   1287  1.8.16.4      yamt 	if (edata->units == ENVSYS_SVOLTS_DC ||
   1288  1.8.16.4      yamt 	    edata->units == ENVSYS_SVOLTS_AC) {
   1289  1.8.16.4      yamt 		if (edata->flags & ENVSYS_FCHANGERFACT) {
   1290  1.8.16.4      yamt 			if (sme_sensor_upbool(dict, "allow-rfact", true))
   1291  1.8.16.4      yamt 				goto out;
   1292  1.8.16.4      yamt 		} else {
   1293  1.8.16.4      yamt 			if (sme_sensor_upbool(dict, "allow-rfact", false))
   1294  1.8.16.4      yamt 				goto out;
   1295  1.8.16.4      yamt 		}
   1296  1.8.16.3      yamt 	}
   1297  1.8.16.3      yamt 
   1298  1.8.16.3      yamt 	/*
   1299  1.8.16.5      yamt 	 * Add the object for battery capacity sensors:
   1300  1.8.16.3      yamt 	 *
   1301  1.8.16.3      yamt 	 * 		...
   1302  1.8.16.5      yamt 	 * 		<key>battery-capacity</key>
   1303  1.8.16.4      yamt 	 * 		<string>NORMAL</string>
   1304  1.8.16.3      yamt 	 * 		...
   1305  1.8.16.3      yamt 	 */
   1306  1.8.16.5      yamt 	if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1307  1.8.16.5      yamt 		sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1308  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1309  1.8.16.4      yamt 			if (sdt[j].type == edata->value_cur)
   1310  1.8.16.4      yamt 				break;
   1311  1.8.16.4      yamt 
   1312  1.8.16.5      yamt 		if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc))
   1313  1.8.16.4      yamt 			goto out;
   1314  1.8.16.4      yamt 	}
   1315  1.8.16.3      yamt 
   1316  1.8.16.3      yamt 	/*
   1317  1.8.16.3      yamt 	 * Add the drive-state object for drive sensors:
   1318  1.8.16.3      yamt 	 *
   1319  1.8.16.3      yamt 	 * 		...
   1320  1.8.16.3      yamt 	 * 		<key>drive-state</key>
   1321  1.8.16.3      yamt 	 * 		<string>drive is online</string>
   1322  1.8.16.3      yamt 	 * 		...
   1323  1.8.16.3      yamt 	 */
   1324  1.8.16.3      yamt 	if (edata->units == ENVSYS_DRIVE) {
   1325  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1326  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1327  1.8.16.4      yamt 			if (sdt[j].type == edata->value_cur)
   1328  1.8.16.3      yamt 				break;
   1329  1.8.16.3      yamt 
   1330  1.8.16.4      yamt 		if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
   1331  1.8.16.4      yamt 			goto out;
   1332  1.8.16.3      yamt 	}
   1333  1.8.16.3      yamt 
   1334  1.8.16.6      yamt 	/*
   1335  1.8.16.6      yamt 	 * Add the following objects if sensor is enabled...
   1336  1.8.16.3      yamt 	 */
   1337  1.8.16.3      yamt 	if (edata->state == ENVSYS_SVALID) {
   1338  1.8.16.3      yamt 		/*
   1339  1.8.16.6      yamt 		 * Add the following objects:
   1340  1.8.16.6      yamt 		 *
   1341  1.8.16.3      yamt 		 * 	...
   1342  1.8.16.3      yamt 		 * 	<key>rpms</key>
   1343  1.8.16.3      yamt 		 * 	<integer>2500</integer>
   1344  1.8.16.3      yamt 		 * 	<key>rfact</key>
   1345  1.8.16.3      yamt 		 * 	<integer>10000</integer>
   1346  1.8.16.3      yamt 		 * 	<key>cur-value</key>
   1347  1.8.16.6      yamt 	 	 * 	<integer>1250</integer>
   1348  1.8.16.6      yamt 	 	 * 	<key>min-value</key>
   1349  1.8.16.6      yamt 	 	 * 	<integer>800</integer>
   1350  1.8.16.6      yamt 	 	 * 	<key>max-value</integer>
   1351  1.8.16.6      yamt 	 	 * 	<integer>3000</integer>
   1352  1.8.16.6      yamt 	 	 * 	<key>avg-value</integer>
   1353  1.8.16.6      yamt 	 	 * 	<integer>1400</integer>
   1354  1.8.16.6      yamt 	 	 * 	...
   1355  1.8.16.6      yamt 	 	 */
   1356  1.8.16.3      yamt 		if (edata->units == ENVSYS_SFANRPM)
   1357  1.8.16.3      yamt 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
   1358  1.8.16.4      yamt 				goto out;
   1359  1.8.16.3      yamt 
   1360  1.8.16.3      yamt 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1361  1.8.16.6      yamt 	    	    edata->units == ENVSYS_SVOLTS_DC)
   1362  1.8.16.3      yamt 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
   1363  1.8.16.4      yamt 				goto out;
   1364  1.8.16.3      yamt 
   1365  1.8.16.3      yamt 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
   1366  1.8.16.4      yamt 			goto out;
   1367  1.8.16.3      yamt 
   1368  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1369  1.8.16.3      yamt 			if (sme_sensor_upint32(dict,
   1370  1.8.16.3      yamt 					       "min-value",
   1371  1.8.16.3      yamt 					       edata->value_min))
   1372  1.8.16.6      yamt 			goto out;
   1373  1.8.16.3      yamt 		}
   1374  1.8.16.3      yamt 
   1375  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1376  1.8.16.3      yamt 			if (sme_sensor_upint32(dict,
   1377  1.8.16.3      yamt 					       "max-value",
   1378  1.8.16.3      yamt 					       edata->value_max))
   1379  1.8.16.6      yamt 			goto out;
   1380  1.8.16.3      yamt 		}
   1381  1.8.16.3      yamt 
   1382  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1383  1.8.16.3      yamt 			if (sme_sensor_upint32(dict,
   1384  1.8.16.3      yamt 					       "avg-value",
   1385  1.8.16.6      yamt 					       edata->value_avg))
   1386  1.8.16.6      yamt 			goto out;
   1387  1.8.16.3      yamt 		}
   1388  1.8.16.3      yamt 	}
   1389  1.8.16.3      yamt 
   1390  1.8.16.3      yamt 	/*
   1391  1.8.16.3      yamt 	 * 	...
   1392  1.8.16.6      yamt 	 * </dict>
   1393  1.8.16.3      yamt 	 *
   1394  1.8.16.3      yamt 	 * Add the dictionary into the array.
   1395  1.8.16.3      yamt 	 *
   1396  1.8.16.3      yamt 	 */
   1397  1.8.16.6      yamt 	if (!prop_array_add(array, dict)) {
   1398  1.8.16.3      yamt 		DPRINTF(("%s: prop_array_add\n", __func__));
   1399  1.8.16.6      yamt 		goto bad;
   1400  1.8.16.3      yamt 	}
   1401  1.8.16.3      yamt 
   1402  1.8.16.3      yamt 	/*
   1403  1.8.16.6      yamt 	 * Register a new event if a monitoring flag was set.
   1404  1.8.16.3      yamt 	 */
   1405  1.8.16.3      yamt 	if (edata->monitor) {
   1406  1.8.16.4      yamt 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
   1407  1.8.16.6      yamt 		sme_evdrv_t->sed_sdict = dict;
   1408  1.8.16.6      yamt 		sme_evdrv_t->sed_edata = edata;
   1409  1.8.16.6      yamt 		sme_evdrv_t->sed_sme = sme;
   1410  1.8.16.6      yamt 		sme_evdrv_t->sed_powertype = sdt_units[i].crittype;
   1411  1.8.16.3      yamt 	}
   1412  1.8.16.3      yamt 
   1413  1.8.16.4      yamt out:
   1414  1.8.16.4      yamt 	return sme_evdrv_t;
   1415  1.8.16.3      yamt 
   1416  1.8.16.6      yamt bad:
   1417  1.8.16.4      yamt 	prop_object_release(dict);
   1418  1.8.16.6      yamt 	return NULL;
   1419  1.8.16.3      yamt }
   1420  1.8.16.3      yamt 
   1421  1.8.16.3      yamt /*
   1422  1.8.16.3      yamt  * sme_update_dictionary:
   1423  1.8.16.3      yamt  *
   1424  1.8.16.3      yamt  * 	+ Update per-sensor dictionaries with new values if there were
   1425  1.8.16.3      yamt  * 	  changes, otherwise the object in dictionary is untouched.
   1426  1.8.16.3      yamt  */
   1427  1.8.16.3      yamt int
   1428  1.8.16.3      yamt sme_update_dictionary(struct sysmon_envsys *sme)
   1429  1.8.16.3      yamt {
   1430  1.8.16.4      yamt 	const struct sme_description_table *sdt;
   1431  1.8.16.4      yamt 	envsys_data_t *edata;
   1432  1.8.16.6      yamt 	prop_object_t array, dict, obj, obj2;
   1433  1.8.16.6      yamt 	int j, error = 0;
   1434  1.8.16.3      yamt 
   1435  1.8.16.4      yamt 	KASSERT(mutex_owned(&sme_mtx));
   1436  1.8.16.3      yamt 
   1437  1.8.16.6      yamt 	/*
   1438  1.8.16.6      yamt 	 * Retrieve the array of dictionaries in device.
   1439  1.8.16.6      yamt 	 */
   1440  1.8.16.3      yamt 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1441  1.8.16.3      yamt 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
   1442  1.8.16.3      yamt 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
   1443  1.8.16.3      yamt 		return EINVAL;
   1444  1.8.16.3      yamt 	}
   1445  1.8.16.3      yamt 
   1446  1.8.16.6      yamt 	/*
   1447  1.8.16.6      yamt 	 * Get the last dictionary on the array, this contains the
   1448  1.8.16.6      yamt 	 * 'device-properties' sub-dictionary.
   1449  1.8.16.6      yamt 	 */
   1450  1.8.16.6      yamt 	obj = prop_array_get(array, prop_array_count(array) - 1);
   1451  1.8.16.6      yamt 	if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) {
   1452  1.8.16.6      yamt 		DPRINTF(("%s: not a device-properties dictionary\n", __func__));
   1453  1.8.16.6      yamt 		return EINVAL;
   1454  1.8.16.6      yamt 	}
   1455  1.8.16.6      yamt 
   1456  1.8.16.6      yamt 	obj2 = prop_dictionary_get(obj, "device-properties");
   1457  1.8.16.6      yamt 	if (!obj2)
   1458  1.8.16.6      yamt 		return EINVAL;
   1459  1.8.16.6      yamt 
   1460  1.8.16.6      yamt 	/*
   1461  1.8.16.6      yamt 	 * Update the 'refresh-timeout' property.
   1462  1.8.16.6      yamt 	 */
   1463  1.8.16.6      yamt 	if (!prop_dictionary_set_uint64(obj2, "refresh-timeout",
   1464  1.8.16.6      yamt 					sme->sme_events_timeout))
   1465  1.8.16.6      yamt 		return EINVAL;
   1466  1.8.16.6      yamt 
   1467  1.8.16.3      yamt 	/*
   1468  1.8.16.3      yamt 	 * - iterate over all sensors.
   1469  1.8.16.3      yamt 	 * - fetch new data.
   1470  1.8.16.3      yamt 	 * - check if data in dictionary is different than new data.
   1471  1.8.16.3      yamt 	 * - update dictionary if there were changes.
   1472  1.8.16.3      yamt 	 */
   1473  1.8.16.4      yamt 	DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
   1474  1.8.16.4      yamt 	    sme->sme_name, sme->sme_nsensors));
   1475  1.8.16.4      yamt 
   1476  1.8.16.6      yamt 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1477  1.8.16.3      yamt 		/*
   1478  1.8.16.6      yamt 		 * refresh sensor data via sme_refresh only if the
   1479  1.8.16.3      yamt 		 * flag is not set.
   1480  1.8.16.3      yamt 		 */
   1481  1.8.16.6      yamt 		if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
   1482  1.8.16.6      yamt 			(*sme->sme_refresh)(sme, edata);
   1483  1.8.16.3      yamt 
   1484  1.8.16.6      yamt 		/*
   1485  1.8.16.6      yamt 		 * retrieve sensor's dictionary.
   1486  1.8.16.6      yamt 		 */
   1487  1.8.16.6      yamt 		dict = prop_array_get(array, edata->sensor);
   1488  1.8.16.3      yamt 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1489  1.8.16.3      yamt 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1490  1.8.16.3      yamt 			    __func__, edata->sensor, sme->sme_name));
   1491  1.8.16.3      yamt 			return EINVAL;
   1492  1.8.16.3      yamt 		}
   1493  1.8.16.3      yamt 
   1494  1.8.16.6      yamt 		/*
   1495  1.8.16.6      yamt 		 * update sensor's state.
   1496  1.8.16.6      yamt 		 */
   1497  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_STATES);
   1498  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1499  1.8.16.4      yamt 			if (sdt[j].type == edata->state)
   1500  1.8.16.3      yamt 				break;
   1501  1.8.16.3      yamt 
   1502  1.8.16.3      yamt 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1503  1.8.16.4      yamt 		    "units=%d sensor=%d\n", __func__, sdt[j].desc,
   1504  1.8.16.4      yamt 		    sdt[j].type, edata->flags, edata->units, edata->sensor));
   1505  1.8.16.3      yamt 
   1506  1.8.16.4      yamt 		error = sme_sensor_upstring(dict, "state", sdt[j].desc);
   1507  1.8.16.3      yamt 		if (error)
   1508  1.8.16.3      yamt 			break;
   1509  1.8.16.3      yamt 
   1510  1.8.16.6      yamt 		/*
   1511  1.8.16.6      yamt 		 * update sensor's type.
   1512  1.8.16.6      yamt 		 */
   1513  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1514  1.8.16.4      yamt 		for (j = 0; sdt[j].type != -1; j++)
   1515  1.8.16.4      yamt 			if (sdt[j].type == edata->units)
   1516  1.8.16.3      yamt 				break;
   1517  1.8.16.3      yamt 
   1518  1.8.16.4      yamt 		error = sme_sensor_upstring(dict, "type", sdt[j].desc);
   1519  1.8.16.3      yamt 		if (error)
   1520  1.8.16.3      yamt 			break;
   1521  1.8.16.3      yamt 
   1522  1.8.16.6      yamt 		/*
   1523  1.8.16.6      yamt 		 * update sensor's current value.
   1524  1.8.16.6      yamt 		 */
   1525  1.8.16.3      yamt 		error = sme_sensor_upint32(dict,
   1526  1.8.16.3      yamt 					   "cur-value",
   1527  1.8.16.3      yamt 					   edata->value_cur);
   1528  1.8.16.3      yamt 		if (error)
   1529  1.8.16.3      yamt 			break;
   1530  1.8.16.3      yamt 
   1531  1.8.16.3      yamt 		/*
   1532  1.8.16.5      yamt 		 * Battery charge, Integer and Indicator types do not
   1533  1.8.16.5      yamt 		 * need the following objects, so skip them.
   1534  1.8.16.3      yamt 		 */
   1535  1.8.16.3      yamt 		if (edata->units == ENVSYS_INTEGER ||
   1536  1.8.16.5      yamt 		    edata->units == ENVSYS_INDICATOR ||
   1537  1.8.16.5      yamt 		    edata->units == ENVSYS_BATTERY_CHARGE)
   1538  1.8.16.3      yamt 			continue;
   1539  1.8.16.3      yamt 
   1540  1.8.16.6      yamt 		/*
   1541  1.8.16.6      yamt 		 * update sensor flags.
   1542  1.8.16.6      yamt 		 */
   1543  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FPERCENT) {
   1544  1.8.16.3      yamt 			error = sme_sensor_upbool(dict,
   1545  1.8.16.3      yamt 						  "want-percentage",
   1546  1.8.16.3      yamt 						  true);
   1547  1.8.16.3      yamt 			if (error)
   1548  1.8.16.3      yamt 				break;
   1549  1.8.16.3      yamt 		}
   1550  1.8.16.3      yamt 
   1551  1.8.16.6      yamt 		/*
   1552  1.8.16.6      yamt 		 * update sensor's {avg,max,min}-value.
   1553  1.8.16.6      yamt 		 */
   1554  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1555  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1556  1.8.16.3      yamt 						   "max-value",
   1557  1.8.16.3      yamt 						   edata->value_max);
   1558  1.8.16.3      yamt 			if (error)
   1559  1.8.16.3      yamt 				break;
   1560  1.8.16.3      yamt 		}
   1561  1.8.16.3      yamt 
   1562  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1563  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1564  1.8.16.3      yamt 						   "min-value",
   1565  1.8.16.3      yamt 						   edata->value_min);
   1566  1.8.16.3      yamt 			if (error)
   1567  1.8.16.3      yamt 				break;
   1568  1.8.16.3      yamt 		}
   1569  1.8.16.3      yamt 
   1570  1.8.16.3      yamt 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1571  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1572  1.8.16.3      yamt 						   "avg-value",
   1573  1.8.16.3      yamt 						   edata->value_avg);
   1574  1.8.16.3      yamt 			if (error)
   1575  1.8.16.3      yamt 				break;
   1576  1.8.16.3      yamt 		}
   1577  1.8.16.3      yamt 
   1578  1.8.16.6      yamt 		/*
   1579  1.8.16.6      yamt 		 * update 'rpms' only for ENVSYS_SFANRPM sensors.
   1580  1.8.16.6      yamt 		 */
   1581  1.8.16.3      yamt 		if (edata->units == ENVSYS_SFANRPM) {
   1582  1.8.16.3      yamt 			error = sme_sensor_upuint32(dict,
   1583  1.8.16.3      yamt 						    "rpms",
   1584  1.8.16.3      yamt 						    edata->rpms);
   1585  1.8.16.3      yamt 			if (error)
   1586  1.8.16.3      yamt 				break;
   1587  1.8.16.3      yamt 		}
   1588  1.8.16.3      yamt 
   1589  1.8.16.6      yamt 		/*
   1590  1.8.16.6      yamt 		 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors.
   1591  1.8.16.6      yamt 		 */
   1592  1.8.16.3      yamt 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1593  1.8.16.3      yamt 		    edata->units == ENVSYS_SVOLTS_DC) {
   1594  1.8.16.3      yamt 			error = sme_sensor_upint32(dict,
   1595  1.8.16.3      yamt 						   "rfact",
   1596  1.8.16.3      yamt 						   edata->rfact);
   1597  1.8.16.3      yamt 			if (error)
   1598  1.8.16.3      yamt 				break;
   1599  1.8.16.3      yamt 		}
   1600  1.8.16.3      yamt 
   1601  1.8.16.6      yamt 		/*
   1602  1.8.16.6      yamt 		 * update 'drive-state' only for ENVSYS_DRIVE sensors.
   1603  1.8.16.6      yamt 		 */
   1604  1.8.16.3      yamt 		if (edata->units == ENVSYS_DRIVE) {
   1605  1.8.16.4      yamt 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1606  1.8.16.4      yamt 			for (j = 0; sdt[j].type != -1; j++)
   1607  1.8.16.4      yamt 				if (sdt[j].type == edata->value_cur)
   1608  1.8.16.3      yamt 					break;
   1609  1.8.16.3      yamt 
   1610  1.8.16.3      yamt 			error = sme_sensor_upstring(dict,
   1611  1.8.16.3      yamt 						    "drive-state",
   1612  1.8.16.4      yamt 						    sdt[j].desc);
   1613  1.8.16.4      yamt 			if (error)
   1614  1.8.16.4      yamt 				break;
   1615  1.8.16.4      yamt 		}
   1616  1.8.16.4      yamt 
   1617  1.8.16.6      yamt 		/*
   1618  1.8.16.6      yamt 		 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY
   1619  1.8.16.6      yamt 		 * sensors.
   1620  1.8.16.6      yamt 		 */
   1621  1.8.16.5      yamt 		if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1622  1.8.16.4      yamt 			sdt =
   1623  1.8.16.6      yamt 			  sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1624  1.8.16.4      yamt 			for (j = 0; sdt[j].type != -1; j++)
   1625  1.8.16.4      yamt 				if (sdt[j].type == edata->value_cur)
   1626  1.8.16.4      yamt 					break;
   1627  1.8.16.4      yamt 
   1628  1.8.16.4      yamt 			error = sme_sensor_upstring(dict,
   1629  1.8.16.5      yamt 						    "battery-capacity",
   1630  1.8.16.4      yamt 						    sdt[j].desc);
   1631  1.8.16.3      yamt 			if (error)
   1632  1.8.16.3      yamt 				break;
   1633  1.8.16.3      yamt 		}
   1634  1.8.16.3      yamt 	}
   1635  1.8.16.3      yamt 
   1636  1.8.16.3      yamt 	return error;
   1637  1.8.16.3      yamt }
   1638  1.8.16.3      yamt 
   1639  1.8.16.3      yamt /*
   1640  1.8.16.3      yamt  * sme_userset_dictionary:
   1641  1.8.16.3      yamt  *
   1642  1.8.16.6      yamt  * 	+ Parse the userland dictionary and run the appropiate tasks
   1643  1.8.16.6      yamt  * 	  that were specified.
   1644  1.8.16.3      yamt  */
   1645  1.8.16.3      yamt int
   1646  1.8.16.3      yamt sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1647  1.8.16.3      yamt 		       prop_array_t array)
   1648  1.8.16.3      yamt {
   1649  1.8.16.4      yamt 	const struct sme_description_table *sdt;
   1650  1.8.16.6      yamt 	envsys_data_t *edata;
   1651  1.8.16.6      yamt 	prop_dictionary_t dict, tdict = NULL;
   1652  1.8.16.6      yamt 	prop_object_t obj, obj1, obj2, tobj = NULL;
   1653  1.8.16.6      yamt 	uint64_t refresh_timo = 0;
   1654  1.8.16.3      yamt 	int32_t critval;
   1655  1.8.16.6      yamt 	int i, error = 0;
   1656  1.8.16.6      yamt 	const char *blah;
   1657  1.8.16.3      yamt 	bool targetfound = false;
   1658  1.8.16.3      yamt 
   1659  1.8.16.6      yamt 	KASSERT(mutex_owned(&sme_mtx));
   1660  1.8.16.3      yamt 
   1661  1.8.16.6      yamt 	/*
   1662  1.8.16.6      yamt 	 * The user wanted to change the refresh timeout value for this
   1663  1.8.16.6      yamt 	 * device.
   1664  1.8.16.6      yamt 	 *
   1665  1.8.16.6      yamt 	 * Get the 'device-properties' object from the userland dictionary.
   1666  1.8.16.6      yamt 	 */
   1667  1.8.16.6      yamt 	obj = prop_dictionary_get(udict, "device-properties");
   1668  1.8.16.6      yamt 	if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) {
   1669  1.8.16.6      yamt 		/*
   1670  1.8.16.6      yamt 		 * Get the 'refresh-timeout' property for this device.
   1671  1.8.16.6      yamt 		 */
   1672  1.8.16.6      yamt 		obj1 = prop_dictionary_get(obj, "refresh-timeout");
   1673  1.8.16.6      yamt 		if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) {
   1674  1.8.16.6      yamt 			targetfound = true;
   1675  1.8.16.6      yamt 			refresh_timo =
   1676  1.8.16.6      yamt 			    prop_number_unsigned_integer_value(obj1);
   1677  1.8.16.6      yamt 			if (refresh_timo < 1)
   1678  1.8.16.6      yamt 				error = EINVAL;
   1679  1.8.16.6      yamt 			else
   1680  1.8.16.6      yamt 				sme->sme_events_timeout = refresh_timo;
   1681  1.8.16.6      yamt 		}
   1682  1.8.16.6      yamt 		goto out;
   1683  1.8.16.3      yamt 
   1684  1.8.16.6      yamt 	} else if (!obj) {
   1685  1.8.16.4      yamt 		/*
   1686  1.8.16.6      yamt 		 * Get sensor's index from userland dictionary.
   1687  1.8.16.4      yamt 		 */
   1688  1.8.16.6      yamt 		obj = prop_dictionary_get(udict, "index");
   1689  1.8.16.6      yamt 		if (!obj)
   1690  1.8.16.6      yamt 			goto out;
   1691  1.8.16.6      yamt 		if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1692  1.8.16.6      yamt 			DPRINTF(("%s: 'index' not a string\n", __func__));
   1693  1.8.16.6      yamt 			return EINVAL;
   1694  1.8.16.3      yamt 		}
   1695  1.8.16.6      yamt 	} else
   1696  1.8.16.6      yamt 		return EINVAL;
   1697  1.8.16.3      yamt 
   1698  1.8.16.6      yamt 	/*
   1699  1.8.16.6      yamt 	 * iterate over the sensors to find the right one.
   1700  1.8.16.6      yamt 	 */
   1701  1.8.16.6      yamt 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1702  1.8.16.6      yamt 		/*
   1703  1.8.16.6      yamt 		 * Get a dictionary and check if it's our sensor by checking
   1704  1.8.16.6      yamt 		 * at its index position.
   1705  1.8.16.6      yamt 		 */
   1706  1.8.16.6      yamt 		dict = prop_array_get(array, edata->sensor);
   1707  1.8.16.4      yamt 		obj1 = prop_dictionary_get(dict, "index");
   1708  1.8.16.3      yamt 
   1709  1.8.16.6      yamt 		/*
   1710  1.8.16.6      yamt 		 * is it our sensor?
   1711  1.8.16.6      yamt 		 */
   1712  1.8.16.3      yamt 		if (!prop_string_equals(obj1, obj))
   1713  1.8.16.3      yamt 			continue;
   1714  1.8.16.3      yamt 
   1715  1.8.16.3      yamt 		/*
   1716  1.8.16.3      yamt 		 * Check if a new description operation was
   1717  1.8.16.3      yamt 		 * requested by the user and set new description.
   1718  1.8.16.3      yamt 		 */
   1719  1.8.16.6      yamt 		obj2 = prop_dictionary_get(udict, "description");
   1720  1.8.16.6      yamt 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) {
   1721  1.8.16.3      yamt 			targetfound = true;
   1722  1.8.16.3      yamt 			blah = prop_string_cstring_nocopy(obj2);
   1723  1.8.16.6      yamt 
   1724  1.8.16.6      yamt 			/*
   1725  1.8.16.6      yamt 			 * Check for duplicate description.
   1726  1.8.16.6      yamt 			 */
   1727  1.8.16.3      yamt 			for (i = 0; i < sme->sme_nsensors; i++) {
   1728  1.8.16.3      yamt 				if (i == edata->sensor)
   1729  1.8.16.3      yamt 					continue;
   1730  1.8.16.6      yamt 				tdict = prop_array_get(array, i);
   1731  1.8.16.6      yamt 				tobj =
   1732  1.8.16.6      yamt 				    prop_dictionary_get(tdict, "description");
   1733  1.8.16.6      yamt 				if (prop_string_equals(obj2, tobj))
   1734  1.8.16.6      yamt 					return EEXIST;
   1735  1.8.16.3      yamt 			}
   1736  1.8.16.3      yamt 
   1737  1.8.16.6      yamt 			/*
   1738  1.8.16.6      yamt 			 * Update the object in dictionary.
   1739  1.8.16.6      yamt 			 */
   1740  1.8.16.3      yamt 			error = sme_sensor_upstring(dict,
   1741  1.8.16.3      yamt 						    "description",
   1742  1.8.16.3      yamt 						    blah);
   1743  1.8.16.3      yamt 			if (error)
   1744  1.8.16.6      yamt 				return error;
   1745  1.8.16.3      yamt 
   1746  1.8.16.6      yamt 			DPRINTF(("%s: sensor%d changed desc to: %s\n",
   1747  1.8.16.6      yamt 			    __func__, edata->sensor, blah));
   1748  1.8.16.4      yamt 			edata->upropset |= USERPROP_DESC;
   1749  1.8.16.3      yamt 		}
   1750  1.8.16.3      yamt 
   1751  1.8.16.4      yamt 		/*
   1752  1.8.16.4      yamt 		 * did the user want to change the rfact?
   1753  1.8.16.4      yamt 		 */
   1754  1.8.16.4      yamt 		obj2 = prop_dictionary_get(udict, "rfact");
   1755  1.8.16.6      yamt 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1756  1.8.16.3      yamt 			targetfound = true;
   1757  1.8.16.4      yamt 			if (edata->flags & ENVSYS_FCHANGERFACT) {
   1758  1.8.16.3      yamt 				edata->rfact = prop_number_integer_value(obj2);
   1759  1.8.16.4      yamt 				edata->upropset |= USERPROP_RFACT;
   1760  1.8.16.6      yamt 				DPRINTF(("%s: sensor%d changed rfact to %d\n",
   1761  1.8.16.6      yamt 				    __func__, edata->sensor, edata->rfact));
   1762  1.8.16.6      yamt 			} else
   1763  1.8.16.6      yamt 				return ENOTSUP;
   1764  1.8.16.3      yamt 		}
   1765  1.8.16.3      yamt 
   1766  1.8.16.4      yamt 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1767  1.8.16.4      yamt 		for (i = 0; sdt[i].type != -1; i++)
   1768  1.8.16.4      yamt 			if (sdt[i].type == edata->units)
   1769  1.8.16.3      yamt 				break;
   1770  1.8.16.3      yamt 
   1771  1.8.16.4      yamt 		/*
   1772  1.8.16.4      yamt 		 * did the user want to set a critical capacity event?
   1773  1.8.16.4      yamt 		 *
   1774  1.8.16.4      yamt 		 * NOTE: if sme_event_register returns EEXIST that means
   1775  1.8.16.4      yamt 		 * the object is already there, but this is not a real
   1776  1.8.16.4      yamt 		 * error, because the object might be updated.
   1777  1.8.16.4      yamt 		 */
   1778  1.8.16.3      yamt 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1779  1.8.16.6      yamt 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1780  1.8.16.3      yamt 			targetfound = true;
   1781  1.8.16.3      yamt 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1782  1.8.16.6      yamt 			    (edata->flags & ENVSYS_FPERCENT) == 0)
   1783  1.8.16.6      yamt 				return ENOTSUP;
   1784  1.8.16.3      yamt 
   1785  1.8.16.3      yamt 			critval = prop_number_integer_value(obj2);
   1786  1.8.16.3      yamt 			error = sme_event_register(dict,
   1787  1.8.16.3      yamt 					      edata,
   1788  1.8.16.6      yamt 					      sme,
   1789  1.8.16.3      yamt 					      "critical-capacity",
   1790  1.8.16.3      yamt 					      critval,
   1791  1.8.16.3      yamt 					      PENVSYS_EVENT_BATT_USERCAP,
   1792  1.8.16.4      yamt 					      sdt[i].crittype);
   1793  1.8.16.4      yamt 			if (error == EEXIST)
   1794  1.8.16.4      yamt 				error = 0;
   1795  1.8.16.6      yamt 			if (error)
   1796  1.8.16.6      yamt 				goto out;
   1797  1.8.16.6      yamt 			else if (!error)
   1798  1.8.16.4      yamt 				edata->upropset |= USERPROP_BATTCAP;
   1799  1.8.16.3      yamt 		}
   1800  1.8.16.3      yamt 
   1801  1.8.16.4      yamt 		/*
   1802  1.8.16.4      yamt 		 * did the user want to set a critical max event?
   1803  1.8.16.4      yamt 		 */
   1804  1.8.16.4      yamt 		obj2 = prop_dictionary_get(udict, "critical-max");
   1805  1.8.16.6      yamt 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1806  1.8.16.3      yamt 			targetfound = true;
   1807  1.8.16.3      yamt 			if (edata->units == ENVSYS_INDICATOR ||
   1808  1.8.16.6      yamt 			    edata->flags & ENVSYS_FMONNOTSUPP)
   1809  1.8.16.6      yamt 				return ENOTSUP;
   1810  1.8.16.3      yamt 
   1811  1.8.16.3      yamt 			critval = prop_number_integer_value(obj2);
   1812  1.8.16.3      yamt 			error = sme_event_register(dict,
   1813  1.8.16.3      yamt 					      edata,
   1814  1.8.16.6      yamt 					      sme,
   1815  1.8.16.4      yamt 					      "critical-max",
   1816  1.8.16.3      yamt 					      critval,
   1817  1.8.16.3      yamt 					      PENVSYS_EVENT_USER_CRITMAX,
   1818  1.8.16.4      yamt 					      sdt[i].crittype);
   1819  1.8.16.4      yamt 			if (error == EEXIST)
   1820  1.8.16.4      yamt 				error = 0;
   1821  1.8.16.6      yamt 			if (error)
   1822  1.8.16.6      yamt 				goto out;
   1823  1.8.16.6      yamt 			else if (!error)
   1824  1.8.16.4      yamt 				edata->upropset |= USERPROP_CRITMAX;
   1825  1.8.16.3      yamt 		}
   1826  1.8.16.3      yamt 
   1827  1.8.16.4      yamt 		/*
   1828  1.8.16.4      yamt 		 * did the user want to set a critical min event?
   1829  1.8.16.4      yamt 		 */
   1830  1.8.16.4      yamt 		obj2 = prop_dictionary_get(udict, "critical-min");
   1831  1.8.16.6      yamt 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1832  1.8.16.3      yamt 			targetfound = true;
   1833  1.8.16.3      yamt 			if (edata->units == ENVSYS_INDICATOR ||
   1834  1.8.16.6      yamt 			    edata->flags & ENVSYS_FMONNOTSUPP)
   1835  1.8.16.6      yamt 				return ENOTSUP;
   1836  1.8.16.3      yamt 
   1837  1.8.16.3      yamt 			critval = prop_number_integer_value(obj2);
   1838  1.8.16.3      yamt 			error = sme_event_register(dict,
   1839  1.8.16.3      yamt 					      edata,
   1840  1.8.16.6      yamt 					      sme,
   1841  1.8.16.4      yamt 					      "critical-min",
   1842  1.8.16.3      yamt 					      critval,
   1843  1.8.16.3      yamt 					      PENVSYS_EVENT_USER_CRITMIN,
   1844  1.8.16.4      yamt 					      sdt[i].crittype);
   1845  1.8.16.4      yamt 			if (error == EEXIST)
   1846  1.8.16.4      yamt 				error = 0;
   1847  1.8.16.6      yamt 			if (error)
   1848  1.8.16.6      yamt 				goto out;
   1849  1.8.16.6      yamt 			else if (!error)
   1850  1.8.16.4      yamt 				edata->upropset |= USERPROP_CRITMIN;
   1851  1.8.16.3      yamt 		}
   1852  1.8.16.4      yamt 
   1853  1.8.16.4      yamt 		/*
   1854  1.8.16.4      yamt 		 * All objects in dictionary were processed.
   1855  1.8.16.4      yamt 		 */
   1856  1.8.16.4      yamt 		break;
   1857  1.8.16.3      yamt 	}
   1858  1.8.16.3      yamt 
   1859  1.8.16.6      yamt out:
   1860  1.8.16.6      yamt 	/*
   1861  1.8.16.6      yamt 	 * invalid target? return the error.
   1862  1.8.16.6      yamt 	 */
   1863  1.8.16.3      yamt 	if (!targetfound)
   1864  1.8.16.3      yamt 		error = EINVAL;
   1865  1.8.16.3      yamt 
   1866  1.8.16.3      yamt 	return error;
   1867       1.1   thorpej }
   1868