Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.30
      1 /*	$NetBSD: sysmon_envsys.c,v 1.30 2007/07/20 10:40:07 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.30 2007/07/20 10:40:07 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 	sme_event_t *see;
    514 
    515 	mutex_enter(&sme_list_mtx);
    516 	while (sme->sme_flags & SME_FLAG_BUSY) {
    517 		sme->sme_flags |= SME_FLAG_WANTED;
    518 		cv_wait(&sme_list_cv, &sme_list_mtx);
    519 	}
    520 	prop_dictionary_remove(sme_propd, sme->sme_name);
    521 	/*
    522 	 * Unregister all events associated with this device.
    523 	 */
    524 	LIST_FOREACH(see, &sme_events_list, see_list) {
    525 		if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
    526 			sme_event_unregister(see->pes.pes_sensname,
    527 			    see->type);
    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 
    620 	/*
    621 	 * <dict>
    622 	 * 	<key>foo0</key>
    623 	 * 	<array>
    624 	 * 		...
    625 	 */
    626 	mutex_enter(&sme_mtx);
    627 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    628 		DPRINTF(("%s: prop_dictionary_set\n", __func__));
    629 		mutex_exit(&sme_mtx);
    630 		error = EINVAL;
    631 		goto out;
    632 	}
    633 	mutex_exit(&sme_mtx);
    634 
    635 	/*
    636 	 * Iterate over all sensors and create a dictionary with all
    637 	 * values specified by the sysmon envsys driver.
    638 	 */
    639 	for (i = 0; i < sme->sme_nsensors; i++) {
    640 		edata = &sme->sme_sensor_data[i];
    641 		/*
    642 		 * refresh sensor data via sme_gtredata only if the
    643 		 * flag is not set.
    644 		 */
    645 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    646 			mutex_enter(&sme_mtx);
    647 			error = (*sme->sme_gtredata)(sme, edata);
    648 			if (error) {
    649 				DPRINTF(("%s: sme->sme_gtredata[%d]\n",
    650 				    __func__, i));
    651 				mutex_exit(&sme_mtx);
    652 				continue;
    653 			}
    654 			mutex_exit(&sme_mtx);
    655 		}
    656 
    657 		error = sme_make_dictionary(sme, array, edata);
    658 		if (error == EINVAL)
    659 			goto out;
    660 		else if (error == EEXIST) {
    661 			error = 0;
    662 			continue;
    663 		}
    664 	}
    665 
    666 out:
    667 	prop_object_release(array);
    668 	return error;
    669 }
    670 
    671 /*
    672  * sme_register_sensorname:
    673  *
    674  * 	+ Registers a sensor name into the list maintained per device.
    675  */
    676 static int
    677 sme_register_sensorname(envsys_data_t *edata)
    678 {
    679 	struct sme_sensor_names *snames, *snames2 = NULL;
    680 
    681 	snames = kmem_zalloc(sizeof(*snames), KM_SLEEP);
    682 
    683 	mutex_enter(&sme_mtx);
    684 	if (SLIST_EMPTY(&sme_names_list))
    685 		goto insert;
    686 	else {
    687 		SLIST_FOREACH(snames2, &sme_names_list, sme_names) {
    688 			if (strcmp(snames2->desc, edata->desc) == 0)
    689 				if (snames2->assigned) {
    690 					edata->flags |= ENVSYS_FDUPDESC;
    691 					mutex_exit(&sme_mtx);
    692 					DPRINTF(("%s: duplicate name "
    693 					    "(%s)\n", __func__, edata->desc));
    694 					kmem_free(snames, sizeof(*snames));
    695 					return EEXIST;
    696 				}
    697 		}
    698 		goto insert;
    699 	}
    700 
    701 insert:
    702 	snames->assigned = true;
    703 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
    704 	DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
    705 	SLIST_INSERT_HEAD(&sme_names_list, snames, sme_names);
    706 	mutex_exit(&sme_mtx);
    707 
    708 	return 0;
    709 }
    710 
    711 /*
    712  * sme_make_dictionary:
    713  *
    714  * 	+ Create sensor's dictionary in device's dictionary array.
    715  */
    716 int
    717 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    718 		    envsys_data_t *edata)
    719 {
    720 	const struct sme_sensor_type *est = sme_sensor_type;
    721 	const struct sme_sensor_state *ess = sme_sensor_state;
    722 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    723 	sme_event_drv_t *sme_evdrv_t = NULL;
    724 	prop_dictionary_t dict;
    725 	prop_object_t obj = NULL;
    726 	int i, j, k;
    727 
    728 	i = j = k = 0;
    729 
    730 	/*
    731 	 * Check if description was already assigned in other sensor.
    732 	 */
    733 	if (sme_register_sensorname(edata))
    734 		return EEXIST;
    735 
    736 	/*
    737 	 * <array>
    738 	 * 	<dict>
    739 	 * 		...
    740 	 */
    741 	dict = prop_dictionary_create();
    742 
    743 	mutex_enter(&sme_mtx);
    744 	if (!prop_array_add(array, dict)) {
    745 		mutex_exit(&sme_mtx);
    746 		prop_object_release(dict);
    747 		DPRINTF(("%s: prop_array_add\n", __func__));
    748 		return EINVAL;
    749 	}
    750 
    751 	/* find the correct unit for this sensor. */
    752 	for (i = 0; est[i].type != -1; i++)
    753 		if (est[i].type == edata->units)
    754 			break;
    755 
    756 	/*
    757 	 * 		...
    758 	 * 		<key>type</key>
    759 	 * 		<string>foo</string>
    760 	 * 		<key>description</key>
    761 	 * 		<string>blah blah</string>
    762 	 * 		...
    763 	 */
    764 	if (sme_sensor_upstring(obj, dict, "type", est[i].desc))
    765 		goto out;
    766 
    767 	if (sme_sensor_upstring(obj, dict, "description", edata->desc))
    768 		goto out;
    769 
    770 
    771 	/*
    772 	 * Add sensor's state description.
    773 	 *
    774 	 * 		...
    775 	 * 		<key>state</key>
    776 	 * 		<string>valid</string>
    777 	 * 		...
    778 	 */
    779 	for (j = 0; ess[j].type != -1; j++)
    780 		if (ess[j].type == edata->state)
    781 			break;
    782 
    783 	if (sme_sensor_upstring(obj, dict, "state", ess[j].desc))
    784 		goto out;
    785 
    786 	/*
    787 	 * add the percentage boolean object:
    788 	 *
    789 	 * 		...
    790 	 * 		<key>want-percentage</key>
    791 	 * 		<true/>
    792 	 * 		...
    793 	 */
    794 	if (edata->flags & ENVSYS_FPERCENT)
    795 		if (sme_sensor_upbool(obj, dict, "want-percentage", true))
    796 			goto out;
    797 
    798 	/*
    799 	 * Add the monitoring boolean object:
    800 	 *
    801 	 * 		...
    802 	 * 		<key>monitoring-supported</key>
    803 	 * 		<true/>
    804 	 *		...
    805 	 *
    806 	 * always false on Drive and Indicator types, they
    807 	 * cannot be monitored.
    808 	 *
    809 	 */
    810 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    811 	    (edata->units == ENVSYS_INDICATOR) ||
    812 	    (edata->units == ENVSYS_DRIVE)) {
    813 		if (sme_sensor_upbool(obj, dict, "monitoring-supported", false))
    814 			goto out;
    815 	} else {
    816 		if (sme_sensor_upbool(obj, dict, "monitoring-supported", true))
    817 			goto out;
    818 	}
    819 
    820 	/*
    821 	 * Add the drive-state object for drive sensors:
    822 	 *
    823 	 * 		...
    824 	 * 		<key>drive-state</key>
    825 	 * 		<string>drive is online</string>
    826 	 * 		...
    827 	 */
    828 	if (edata->units == ENVSYS_DRIVE) {
    829 		for (k = 0; esds[k].type != -1; k++)
    830 			if (esds[k].type == edata->value_cur)
    831 				break;
    832 
    833 		if (sme_sensor_upstring(obj, dict, "drive-state", esds[k].desc))
    834 			goto out;
    835 	}
    836 
    837 	mutex_exit(&sme_mtx);
    838 	/*
    839 	 * Add a new event if a monitoring flag was set.
    840 	 */
    841 	if (edata->monitor) {
    842 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
    843 
    844 		sme_evdrv_t->sdict = dict;
    845 		sme_evdrv_t->edata = edata;
    846 		sme_evdrv_t->sme = sme;
    847 		sme_evdrv_t->powertype = est[i].crittype;
    848 
    849 		sysmon_task_queue_init();
    850 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
    851 	}
    852 
    853 	mutex_enter(&sme_mtx);
    854 	/* if sensor is enabled, add the following properties... */
    855 	if (edata->state == ENVSYS_SVALID) {
    856 		/*
    857 		 * 	...
    858 		 * 	<key>rpms</key>
    859 		 * 	<integer>2500</integer>
    860 		 * 	<key>rfact</key>
    861 		 * 	<integer>10000</integer>
    862 		 * 	<key>cur-value</key>
    863 		 * 	<integer>1250</integer>
    864 		 * 	<key>min-value</key>
    865 		 * 	<integer>800</integer>
    866 		 * 	<key>max-value</integer>
    867 		 * 	<integer>3000</integer>
    868 		 * 	<key>avg-value</integer>
    869 		 * 	<integer>1400</integer>
    870 		 * </dict>
    871 		 */
    872 		if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
    873 			if (sme_sensor_upuint32(obj, dict, "rpms", edata->rpms))
    874 				goto out;
    875 
    876 		if ((edata->units == ENVSYS_SVOLTS_AC ||
    877 		    edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
    878 			if (sme_sensor_upint32(obj, dict, "rfact", edata->rfact))
    879 				goto out;
    880 
    881 		if (edata->value_cur) {
    882 			if (sme_sensor_upint32(obj,
    883 					       dict,
    884 					       "cur-value",
    885 					       edata->value_cur))
    886 				goto out;
    887 		}
    888 
    889 		if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min) {
    890 			if (sme_sensor_upint32(obj,
    891 					       dict,
    892 					       "min-value",
    893 					       edata->value_min))
    894 				goto out;
    895 		}
    896 
    897 		if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max) {
    898 			if (sme_sensor_upint32(obj,
    899 					       dict,
    900 					       "max-value",
    901 					       edata->value_max))
    902 				goto out;
    903 		}
    904 
    905 		if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg) {
    906 			if (sme_sensor_upint32(obj,
    907 					       dict,
    908 					       "avg-value",
    909 					       edata->value_avg))
    910 				goto out;
    911 		}
    912 	}
    913 
    914 	/*
    915 	 * 	...
    916 	 * </array>
    917 	 */
    918 out:
    919 	mutex_exit(&sme_mtx);
    920 	prop_object_release(dict);
    921 	return 0;
    922 }
    923 
    924 /*
    925  * sme_update_dictionary:
    926  *
    927  * 	+ Update per-sensor dictionaries with new values if there were
    928  * 	  changes, otherwise the object in dictionary is untouched.
    929  * 	+ Send a critical event if any sensor is in a critical condition.
    930  */
    931 int
    932 sme_update_dictionary(struct sysmon_envsys *sme)
    933 {
    934 	const struct sme_sensor_state *ess = sme_sensor_state;
    935 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    936 	envsys_data_t *edata = NULL;
    937 	prop_object_t array, obj, dict;
    938 	int i, j, error = 0;
    939 
    940 	array = obj = dict = NULL;
    941 
    942 	/* retrieve the array of dictionaries in device. */
    943 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    944 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    945 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
    946 		return EINVAL;
    947 	}
    948 
    949 	/*
    950 	 * - iterate over all sensors.
    951 	 * - fetch new data.
    952 	 * - check if data in dictionary is different than new data.
    953 	 * - update dictionary if there were changes.
    954 	 */
    955 	for (i = 0; i < sme->sme_nsensors; i++) {
    956 		edata = &sme->sme_sensor_data[i];
    957 		if (edata->flags & ENVSYS_FDUPDESC) {
    958 			--sme->sme_nsensors;
    959 			--i;
    960 			continue;
    961 		}
    962 
    963 		/*
    964 		 * refresh sensor data via sme_gtredata only if the
    965 		 * flag is not set.
    966 		 */
    967 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    968 			error = (*sme->sme_gtredata)(sme, edata);
    969 			if (error) {
    970 				DPRINTF(("%s: gtredata[%d] failed\n",
    971 				    __func__, i));
    972 				return error;
    973 			}
    974 		}
    975 
    976 		/* retrieve sensor's dictionary. */
    977 		dict = prop_array_get(array, i);
    978 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
    979 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
    980 			    __func__, edata->sensor, sme->sme_name));
    981 			return EINVAL;
    982 		}
    983 
    984 		/* update state sensor. */
    985 		for (j = 0; ess[j].type != -1; j++)
    986 			if (ess[j].type == edata->state)
    987 				break;
    988 
    989 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
    990 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
    991 		    ess[j].type, edata->flags, edata->units, edata->sensor));
    992 
    993 		/* update sensor state */
    994 		error = sme_sensor_upstring(obj, dict, "state", ess[j].desc);
    995 		if (error)
    996 			break;
    997 
    998 		/* update sensor current value */
    999 		error = sme_sensor_upint32(obj,
   1000 					   dict,
   1001 					   "cur-value",
   1002 					   edata->value_cur);
   1003 		if (error)
   1004 			break;
   1005 
   1006 		/*
   1007 		 * Integer and Indicator types do not the following
   1008 		 * values, so skip them.
   1009 		 */
   1010 		if (edata->units == ENVSYS_INTEGER ||
   1011 		    edata->units == ENVSYS_INDICATOR)
   1012 			continue;
   1013 
   1014 		/* update sensor flags */
   1015 		if (edata->flags & ENVSYS_FPERCENT) {
   1016 			error = sme_sensor_upbool(obj,
   1017 						  dict,
   1018 						  "want-percentage",
   1019 						  true);
   1020 			if (error)
   1021 				break;
   1022 		}
   1023 
   1024 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1025 			error = sme_sensor_upint32(obj,
   1026 						   dict,
   1027 						   "max-value",
   1028 						   edata->value_max);
   1029 			if (error)
   1030 				break;
   1031 		}
   1032 
   1033 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1034 			error = sme_sensor_upint32(obj,
   1035 						   dict,
   1036 						   "min-value",
   1037 						   edata->value_min);
   1038 			if (error)
   1039 				break;
   1040 		}
   1041 
   1042 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1043 			error = sme_sensor_upint32(obj,
   1044 						   dict,
   1045 						   "avg-value",
   1046 						   edata->value_avg);
   1047 			if (error)
   1048 				break;
   1049 		}
   1050 
   1051 		/* update 'rpms' only in ENVSYS_SFANRPM. */
   1052 		if (edata->units == ENVSYS_SFANRPM) {
   1053 			error = sme_sensor_upuint32(obj,
   1054 						    dict,
   1055 						    "rpms",
   1056 						    edata->rpms);
   1057 			if (error)
   1058 				break;
   1059 		}
   1060 
   1061 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
   1062 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1063 		    edata->units == ENVSYS_SVOLTS_DC) {
   1064 			error = sme_sensor_upint32(obj,
   1065 						   dict,
   1066 						   "rfact",
   1067 						   edata->rfact);
   1068 			if (error)
   1069 				break;
   1070 		}
   1071 
   1072 		/* update 'drive-state' only in ENVSYS_DRIVE. */
   1073 		if (edata->units == ENVSYS_DRIVE) {
   1074 			for (j = 0; esds[j].type != -1; j++)
   1075 				if (esds[j].type == edata->value_cur)
   1076 					break;
   1077 
   1078 			error = sme_sensor_upstring(obj,
   1079 						    dict,
   1080 						    "drive-state",
   1081 						    esds[j].desc);
   1082 		}
   1083 	}
   1084 
   1085 	return error;
   1086 }
   1087 
   1088 /*
   1089  * sme_userset_dictionary:
   1090  *
   1091  * 	+ Parses the userland dictionary and run the appropiate
   1092  * 	  tasks that were requested.
   1093  */
   1094 int
   1095 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1096 		       prop_array_t array)
   1097 {
   1098 	const struct sme_sensor_type *sst = sme_sensor_type;
   1099 	envsys_data_t *edata, *nedata;
   1100 	prop_dictionary_t dict;
   1101 	prop_object_t obj, obj1, obj2;
   1102 	int32_t critval;
   1103 	int i, error = 0;
   1104 	const char *blah, *sname;
   1105 	bool targetfound = false;
   1106 
   1107 	blah = sname = NULL;
   1108 
   1109 	/* get sensor's name from userland dictionary. */
   1110 	obj = prop_dictionary_get(udict, "sensor-name");
   1111 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1112 		DPRINTF(("%s: sensor-name failed\n", __func__));
   1113 		return EINVAL;
   1114 	}
   1115 
   1116 	/* iterate over the sensors to find the right one */
   1117 	for (i = 0; i < sme->sme_nsensors; i++) {
   1118 		edata = &sme->sme_sensor_data[i];
   1119 
   1120 		/* duplicate description? skip it */
   1121 		if (edata->flags & ENVSYS_FDUPDESC)
   1122 			continue;
   1123 
   1124 		dict = prop_array_get(array, i);
   1125 		obj1 = prop_dictionary_get(dict, "description");
   1126 
   1127 		/* is it our sensor? */
   1128 		if (!prop_string_equals(obj1, obj))
   1129 			continue;
   1130 
   1131 		/*
   1132 		 * Check if a new description operation was
   1133 		 * requested by the user and set new description.
   1134 		 */
   1135 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
   1136 			targetfound = true;
   1137 			blah = prop_string_cstring_nocopy(obj2);
   1138 
   1139 			for (i = 0; i < sme->sme_nsensors; i++) {
   1140 				if (i == edata->sensor)
   1141 					continue;
   1142 
   1143 				nedata = &sme->sme_sensor_data[i];
   1144 				if (strcmp(blah, nedata->desc) == 0) {
   1145 					error = EEXIST;
   1146 					break;
   1147 				}
   1148 			}
   1149 
   1150 			error = sme_sensor_upstring(obj,
   1151 						    dict,
   1152 						    "description",
   1153 						    blah);
   1154 			break;
   1155 		}
   1156 
   1157 		/* did the user want to remove a critical capacity limit? */
   1158 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
   1159 		if (obj2 != NULL) {
   1160 			targetfound = true;
   1161 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1162 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1163 				    error = ENOTSUP;
   1164 				    break;
   1165 			}
   1166 
   1167 			sname = prop_string_cstring_nocopy(obj);
   1168 			error = sme_event_unregister(sname,
   1169 			    PENVSYS_EVENT_BATT_USERCAP);
   1170 			if (error)
   1171 				break;
   1172 
   1173 			prop_dictionary_remove(dict, "critical-capacity");
   1174 			break;
   1175 		}
   1176 
   1177 		/* did the user want to remove a critical min limit? */
   1178 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
   1179 		if (obj2 != NULL) {
   1180 			targetfound = true;
   1181 			sname = prop_string_cstring_nocopy(obj);
   1182 			error = sme_event_unregister(sname,
   1183 			    PENVSYS_EVENT_USER_CRITMIN);
   1184 			if (error)
   1185 				break;
   1186 
   1187 			prop_dictionary_remove(dict, "critical-min-limit");
   1188 			break;
   1189 		}
   1190 
   1191 		/* did the user want to remove a critical max limit? */
   1192 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1193 		if (obj2 != NULL) {
   1194 			targetfound = true;
   1195 			sname = prop_string_cstring_nocopy(obj);
   1196 			error = sme_event_unregister(sname,
   1197 			    PENVSYS_EVENT_USER_CRITMAX);
   1198 			if (error)
   1199 				break;
   1200 
   1201 			prop_dictionary_remove(dict, "critical-max-limit");
   1202 			break;
   1203 		}
   1204 
   1205 		/* did the user want to change rfact? */
   1206 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1207 		if (obj2 != NULL) {
   1208 			targetfound = true;
   1209 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1210 				edata->rfact = prop_number_integer_value(obj2);
   1211 			else
   1212 				error = ENOTSUP;
   1213 
   1214 			break;
   1215 		}
   1216 
   1217 		for (i = 0; sst[i].type != -1; i++)
   1218 			if (sst[i].type == edata->units)
   1219 				break;
   1220 
   1221 		/* did the user want to set a critical capacity event? */
   1222 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1223 		if (obj2 != NULL) {
   1224 			targetfound = true;
   1225 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1226 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1227 				error = ENOTSUP;
   1228 				break;
   1229 			}
   1230 
   1231 			critval = prop_number_integer_value(obj2);
   1232 			error = sme_event_add(dict,
   1233 					      edata,
   1234 					      sme->sme_name,
   1235 					      "critical-capacity",
   1236 					      critval,
   1237 					      PENVSYS_EVENT_BATT_USERCAP,
   1238 					      sst[i].crittype);
   1239 			break;
   1240 		}
   1241 
   1242 		/* did the user want to set a critical max event? */
   1243 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1244 		if (obj2 != NULL) {
   1245 			targetfound = true;
   1246 			if (edata->units == ENVSYS_INDICATOR ||
   1247 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1248 				error = ENOTSUP;
   1249 				break;
   1250 			}
   1251 
   1252 			critval = prop_number_integer_value(obj2);
   1253 			error = sme_event_add(dict,
   1254 					      edata,
   1255 					      sme->sme_name,
   1256 					      "critical-max-limit",
   1257 					      critval,
   1258 					      PENVSYS_EVENT_USER_CRITMAX,
   1259 					      sst[i].crittype);
   1260 			break;
   1261 		}
   1262 
   1263 		/* did the user want to set a critical min event? */
   1264 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1265 		if (obj2 != NULL) {
   1266 			targetfound = true;
   1267 			if (edata->units == ENVSYS_INDICATOR ||
   1268 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1269 				error = ENOTSUP;
   1270 				break;
   1271 			}
   1272 
   1273 			critval = prop_number_integer_value(obj2);
   1274 			error = sme_event_add(dict,
   1275 					      edata,
   1276 					      sme->sme_name,
   1277 					      "critical-min-limit",
   1278 					      critval,
   1279 					      PENVSYS_EVENT_USER_CRITMIN,
   1280 					      sst[i].crittype);
   1281 			break;
   1282 		}
   1283 	}
   1284 
   1285 	/* invalid target? return the error */
   1286 	if (!targetfound)
   1287 		error = EINVAL;
   1288 
   1289 	return error;
   1290 }
   1291