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