Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.27
      1 /*	$NetBSD: sysmon_envsys.c,v 1.27 2007/07/19 00:12:47 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.27 2007/07/19 00:12:47 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 		kmem_free(snames, sizeof(*snames));
    536 	}
    537 	LIST_REMOVE(sme, sme_list);
    538 	mutex_exit(&sme_list_mtx);
    539 }
    540 
    541 /*
    542  * sysmon_envsys_find:
    543  *
    544  *	+ Find an envsys device.
    545  */
    546 struct sysmon_envsys *
    547 sysmon_envsys_find(const char *name)
    548 {
    549 	struct sysmon_envsys *sme;
    550 
    551 	mutex_enter(&sme_list_mtx);
    552 again:
    553 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    554 	     sme = LIST_NEXT(sme, sme_list)) {
    555 			if (strcmp(sme->sme_name, name) == 0) {
    556 				if (sme->sme_flags & SME_FLAG_BUSY) {
    557 					sme->sme_flags |= SME_FLAG_WANTED;
    558 					cv_wait(&sme_list_cv, &sme_list_mtx);
    559 					goto again;
    560 				}
    561 				sme->sme_flags |= SME_FLAG_BUSY;
    562 				break;
    563 			}
    564 	}
    565 	mutex_exit(&sme_list_mtx);
    566 	return sme;
    567 }
    568 
    569 /*
    570  * sysmon_envsys_release:
    571  *
    572  * 	+ Release an envsys device.
    573  */
    574 void
    575 sysmon_envsys_release(struct sysmon_envsys *sme)
    576 {
    577 	mutex_enter(&sme_list_mtx);
    578 	if (sme->sme_flags & SME_FLAG_WANTED)
    579 		cv_broadcast(&sme_list_cv);
    580 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    581 	mutex_exit(&sme_list_mtx);
    582 }
    583 
    584 /* compatibility function */
    585 #ifdef COMPAT_40
    586 struct sysmon_envsys *
    587 sysmon_envsys_find_40(u_int idx)
    588 {
    589 	struct sysmon_envsys *sme;
    590 
    591 	mutex_enter(&sme_list_mtx);
    592 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    593 	     sme = LIST_NEXT(sme, sme_list)) {
    594 		if (idx >= sme->sme_fsensor &&
    595 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    596 			break;
    597 	}
    598 	mutex_exit(&sme_list_mtx);
    599 	return sme;
    600 }
    601 #endif
    602 
    603 /*
    604  * sysmon_envsys_createplist:
    605  *
    606  * 	+ Create the property list structure for a device.
    607  */
    608 int
    609 sysmon_envsys_createplist(struct sysmon_envsys *sme)
    610 {
    611 	envsys_data_t *edata;
    612 	prop_array_t array;
    613 	int i, error = 0;
    614 
    615 	/* create the sysmon envsys device array. */
    616 	array = prop_array_create();
    617 
    618 	/*
    619 	 * <dict>
    620 	 * 	<key>foo0</key>
    621 	 * 	<array>
    622 	 * 		...
    623 	 */
    624 	mutex_enter(&sme_mtx);
    625 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    626 		DPRINTF(("%s: prop_dictionary_set\n", __func__));
    627 		mutex_exit(&sme_mtx);
    628 		error = EINVAL;
    629 		goto out;
    630 	}
    631 	mutex_exit(&sme_mtx);
    632 
    633 	/*
    634 	 * Iterate over all sensors and create a dictionary with all
    635 	 * values specified by the sysmon envsys driver.
    636 	 */
    637 	for (i = 0; i < sme->sme_nsensors; i++) {
    638 		edata = &sme->sme_sensor_data[i];
    639 		/*
    640 		 * refresh sensor data via sme_gtredata only if the
    641 		 * flag is not set.
    642 		 */
    643 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    644 			mutex_enter(&sme_mtx);
    645 			error = (*sme->sme_gtredata)(sme, edata);
    646 			if (error) {
    647 				DPRINTF(("%s: sme->sme_gtredata[%d]\n",
    648 				    __func__, i));
    649 				mutex_exit(&sme_mtx);
    650 				continue;
    651 			}
    652 			mutex_exit(&sme_mtx);
    653 		}
    654 
    655 		error = sme_make_dictionary(sme, array, edata);
    656 		if (error == EINVAL)
    657 			goto out;
    658 		else if (error == EEXIST) {
    659 			error = 0;
    660 			continue;
    661 		}
    662 	}
    663 
    664 out:
    665 	prop_object_release(array);
    666 	return error;
    667 }
    668 
    669 /*
    670  * sme_register_sensorname:
    671  *
    672  * 	+ Registers a sensor name into the list maintained per device.
    673  */
    674 static int
    675 sme_register_sensorname(envsys_data_t *edata)
    676 {
    677 	struct sme_sensor_names *snames, *snames2 = NULL;
    678 
    679 	snames = kmem_zalloc(sizeof(*snames), KM_SLEEP);
    680 
    681 	mutex_enter(&sme_mtx);
    682 	if (SLIST_EMPTY(&sme_names_list))
    683 		goto insert;
    684 	else {
    685 		SLIST_FOREACH(snames2, &sme_names_list, sme_names) {
    686 			if (strcmp(snames2->desc, edata->desc) == 0)
    687 				if (snames2->assigned) {
    688 					edata->flags |= ENVSYS_FDUPDESC;
    689 					mutex_exit(&sme_mtx);
    690 					DPRINTF(("%s: duplicate name "
    691 					    "(%s)\n", __func__, edata->desc));
    692 					return EEXIST;
    693 				}
    694 		}
    695 		goto insert;
    696 	}
    697 
    698 insert:
    699 	snames->assigned = true;
    700 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
    701 	DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
    702 	SLIST_INSERT_HEAD(&sme_names_list, snames, sme_names);
    703 	mutex_exit(&sme_mtx);
    704 
    705 	return 0;
    706 }
    707 
    708 /*
    709  * sme_make_dictionary:
    710  *
    711  * 	+ Create sensor's dictionary in device's dictionary array.
    712  */
    713 int
    714 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    715 		    envsys_data_t *edata)
    716 {
    717 	const struct sme_sensor_type *est = sme_sensor_type;
    718 	const struct sme_sensor_state *ess = sme_sensor_state;
    719 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    720 	sme_event_drv_t *sme_evdrv_t = NULL;
    721 	prop_dictionary_t dict;
    722 	int i, j, k;
    723 
    724 	i = j = k = 0;
    725 
    726 	/*
    727 	 * Check if description was already assigned in other sensor.
    728 	 */
    729 	if (sme_register_sensorname(edata))
    730 		return EEXIST;
    731 
    732 	/*
    733 	 * <array>
    734 	 * 	<dict>
    735 	 * 		...
    736 	 */
    737 	dict = prop_dictionary_create();
    738 
    739 	mutex_enter(&sme_mtx);
    740 	if (!prop_array_add(array, dict)) {
    741 		mutex_exit(&sme_mtx);
    742 		prop_object_release(dict);
    743 		DPRINTF(("%s: prop_array_add\n", __func__));
    744 		return EINVAL;
    745 	}
    746 
    747 	/* find the correct unit for this sensor. */
    748 	for (i = 0; est[i].type != -1; i++)
    749 		if (est[i].type == edata->units)
    750 			break;
    751 
    752 	/*
    753 	 * 		...
    754 	 * 		<key>type</key>
    755 	 * 		<string>foo</string>
    756 	 * 		<key>description</key>
    757 	 * 		<string>blah blah</string>
    758 	 * 		...
    759 	 */
    760 	SENSOR_SSTRING(dict, "type", est[i].desc);
    761 	SENSOR_SSTRING(dict, "description", edata->desc);
    762 
    763 	/*
    764 	 * Add sensor's state description.
    765 	 *
    766 	 * 		...
    767 	 * 		<key>state</key>
    768 	 * 		<string>valid</string>
    769 	 * 		...
    770 	 */
    771 	for (j = 0; ess[j].type != -1; j++)
    772 		if (ess[j].type == edata->state)
    773 			break;
    774 
    775 	SENSOR_SSTRING(dict, "state", ess[j].desc);
    776 
    777 	/*
    778 	 * add the percentage boolean object:
    779 	 *
    780 	 * 		...
    781 	 * 		<key>want-percentage</key>
    782 	 * 		<true/>
    783 	 * 		...
    784 	 */
    785 	if (edata->flags & ENVSYS_FPERCENT)
    786 		SENSOR_SBOOL(dict, "want-percentage", true);
    787 
    788 	/*
    789 	 * Add the monitoring boolean object:
    790 	 *
    791 	 * 		...
    792 	 * 		<key>monitoring-supported</key>
    793 	 * 		<true/>
    794 	 *		...
    795 	 *
    796 	 * always false on Drive and Indicator types, they
    797 	 * cannot be monitored.
    798 	 *
    799 	 */
    800 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    801 	    (edata->units == ENVSYS_INDICATOR) ||
    802 	    (edata->units == ENVSYS_DRIVE)) {
    803 		SENSOR_SBOOL(dict, "monitoring-supported", false);
    804 	} else {
    805 		SENSOR_SBOOL(dict, "monitoring-supported", true);
    806 	}
    807 
    808 	/*
    809 	 * Add the drive-state object for drive sensors:
    810 	 *
    811 	 * 		...
    812 	 * 		<key>drive-state</key>
    813 	 * 		<string>drive is online</string>
    814 	 * 		...
    815 	 */
    816 	if (edata->units == ENVSYS_DRIVE) {
    817 		for (k = 0; esds[k].type != -1; k++)
    818 			if (esds[k].type == edata->value_cur)
    819 				break;
    820 		SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
    821 	}
    822 
    823 	mutex_exit(&sme_mtx);
    824 	/*
    825 	 * Add a new event if a monitoring flag was set.
    826 	 */
    827 	if (edata->monitor) {
    828 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
    829 
    830 		sme_evdrv_t->sdict = dict;
    831 		sme_evdrv_t->edata = edata;
    832 		sme_evdrv_t->sme = sme;
    833 		sme_evdrv_t->powertype = est[i].crittype;
    834 
    835 		sysmon_task_queue_init();
    836 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
    837 	}
    838 
    839 	mutex_enter(&sme_mtx);
    840 	/* if sensor is enabled, add the following properties... */
    841 	if (edata->state == ENVSYS_SVALID) {
    842 		/*
    843 		 * 	...
    844 		 * 	<key>rpms</key>
    845 		 * 	<integer>2500</integer>
    846 		 * 	<key>rfact</key>
    847 		 * 	<integer>10000</integer>
    848 		 * 	<key>cur-value</key>
    849 		 * 	<integer>1250</integer>
    850 		 * 	<key>min-value</key>
    851 		 * 	<integer>800</integer>
    852 		 * 	<key>max-value</integer>
    853 		 * 	<integer>3000</integer>
    854 		 * 	<key>avg-value</integer>
    855 		 * 	<integer>1400</integer>
    856 		 * </dict>
    857 		 */
    858 		if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
    859 			SENSOR_SUINT32(dict, "rpms", edata->rpms);
    860 
    861 		if ((edata->units == ENVSYS_SVOLTS_AC ||
    862 		    edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
    863 			SENSOR_SINT32(dict, "rfact", edata->rfact);
    864 
    865 		if (edata->value_cur)
    866 			SENSOR_SINT32(dict, "cur-value", edata->value_cur);
    867 
    868 		if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
    869 			SENSOR_SINT32(dict, "min-value", edata->value_min);
    870 
    871 		if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
    872 			SENSOR_SINT32(dict, "max-value", edata->value_max);
    873 
    874 		if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
    875 			SENSOR_SINT32(dict, "avg-value", edata->value_avg);
    876 	}
    877 
    878 	/*
    879 	 * 	...
    880 	 * </array>
    881 	 */
    882 out:
    883 	mutex_exit(&sme_mtx);
    884 	prop_object_release(dict);
    885 	return 0;
    886 }
    887 
    888 /*
    889  * sme_update_dictionary:
    890  *
    891  * 	+ Update per-sensor dictionaries with new values if there were
    892  * 	  changes, otherwise the object in dictionary is untouched.
    893  * 	+ Send a critical event if any sensor is in a critical condition.
    894  */
    895 int
    896 sme_update_dictionary(struct sysmon_envsys *sme)
    897 {
    898 	const struct sme_sensor_state *ess = sme_sensor_state;
    899 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    900 	envsys_data_t *edata = NULL;
    901 	prop_object_t array, obj, dict = NULL;
    902 	int i, j, error = 0;
    903 
    904 	/* retrieve the array of dictionaries in device. */
    905 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    906 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    907 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
    908 		return EINVAL;
    909 	}
    910 
    911 	/*
    912 	 * - iterate over all sensors.
    913 	 * - fetch new data.
    914 	 * - check if data in dictionary is different than new data.
    915 	 * - update dictionary if there were changes.
    916 	 */
    917 	for (i = 0; i < sme->sme_nsensors; i++) {
    918 		edata = &sme->sme_sensor_data[i];
    919 		if (edata->flags & ENVSYS_FDUPDESC) {
    920 			--sme->sme_nsensors;
    921 			--i;
    922 			continue;
    923 		}
    924 
    925 		/*
    926 		 * refresh sensor data via sme_gtredata only if the
    927 		 * flag is not set.
    928 		 */
    929 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    930 			error = (*sme->sme_gtredata)(sme, edata);
    931 			if (error) {
    932 				DPRINTF(("%s: gtredata[%d] failed\n",
    933 				    __func__, i));
    934 				return error;
    935 			}
    936 		}
    937 
    938 		/* retrieve sensor's dictionary. */
    939 		dict = prop_array_get(array, i);
    940 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
    941 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
    942 			    __func__, edata->sensor, sme->sme_name));
    943 			return EINVAL;
    944 		}
    945 
    946 		/* update state sensor. */
    947 		for (j = 0; ess[j].type != -1; j++)
    948 			if (ess[j].type == edata->state)
    949 				break;
    950 
    951 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
    952 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
    953 		    ess[j].type, edata->flags, edata->units, edata->sensor));
    954 
    955 		/* update sensor state */
    956 		SENSOR_UPSTRING(dict, "state", ess[j].desc);
    957 
    958 		/* update sensor current value */
    959 		SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
    960 
    961 		/*
    962 		 * Integer and Indicator types do not the following
    963 		 * values, so skip them.
    964 		 */
    965 		if (edata->units == ENVSYS_INTEGER ||
    966 		    edata->units == ENVSYS_INDICATOR)
    967 			continue;
    968 
    969 		/* update sensor flags */
    970 		if (edata->flags & ENVSYS_FPERCENT)
    971 			SENSOR_SBOOL(dict, "want-percentage", true);
    972 		else {
    973 			obj = prop_dictionary_get(dict, "want-percentage");
    974 			if (obj)
    975 				SENSOR_SBOOL(dict, "want-percentage", false);
    976 		}
    977 
    978 		if (edata->flags & ENVSYS_FVALID_MAX)
    979 			SENSOR_UPINT32(dict, "max-value", edata->value_max);
    980 
    981 		if (edata->flags & ENVSYS_FVALID_MIN)
    982 			SENSOR_UPINT32(dict, "min-value", edata->value_min);
    983 
    984 		if (edata->flags & ENVSYS_FVALID_AVG)
    985 			SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
    986 
    987 		/* update 'rpms' only in ENVSYS_SFANRPM. */
    988 		if (edata->units == ENVSYS_SFANRPM)
    989 			SENSOR_UPUINT32(dict, "rpms", edata->rpms);
    990 
    991 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
    992 		if (edata->units == ENVSYS_SVOLTS_AC ||
    993 		    edata->units == ENVSYS_SVOLTS_DC) {
    994 			SENSOR_UPINT32(dict, "rfact", edata->rfact);
    995 		}
    996 
    997 		/* update 'drive-state' only in ENVSYS_DRIVE. */
    998 		if (edata->units == ENVSYS_DRIVE) {
    999 			for (j = 0; esds[j].type != -1; j++)
   1000 				if (esds[j].type == edata->value_cur)
   1001 					break;
   1002 
   1003 			SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
   1004 		}
   1005 	}
   1006 
   1007 out:
   1008 	return error;
   1009 }
   1010 
   1011 /*
   1012  * sme_userset_dictionary:
   1013  *
   1014  * 	+ Parses the userland dictionary and run the appropiate
   1015  * 	  tasks that were requested.
   1016  */
   1017 int
   1018 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1019 		       prop_array_t array)
   1020 {
   1021 	const struct sme_sensor_type *sst = sme_sensor_type;
   1022 	envsys_data_t *edata, *nedata;
   1023 	prop_dictionary_t dict;
   1024 	prop_object_t obj, obj1, obj2;
   1025 	int32_t critval;
   1026 	int i, error = 0;
   1027 	const char *blah, *sname;
   1028 	bool targetfound = false;
   1029 
   1030 	blah = sname = NULL;
   1031 
   1032 	/* get sensor's name from userland dictionary. */
   1033 	obj = prop_dictionary_get(udict, "sensor-name");
   1034 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1035 		DPRINTF(("%s: sensor-name failed\n", __func__));
   1036 		return EINVAL;
   1037 	}
   1038 
   1039 	/* iterate over the sensors to find the right one */
   1040 	for (i = 0; i < sme->sme_nsensors; i++) {
   1041 		edata = &sme->sme_sensor_data[i];
   1042 
   1043 		/* duplicate description? skip it */
   1044 		if (edata->flags & ENVSYS_FDUPDESC)
   1045 			continue;
   1046 
   1047 		dict = prop_array_get(array, i);
   1048 		obj1 = prop_dictionary_get(dict, "description");
   1049 
   1050 		/* is it our sensor? */
   1051 		if (!prop_string_equals(obj1, obj))
   1052 			continue;
   1053 
   1054 		/*
   1055 		 * Check if a new description operation was
   1056 		 * requested by the user and set new description.
   1057 		 */
   1058 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
   1059 			targetfound = true;
   1060 			blah = prop_string_cstring_nocopy(obj2);
   1061 
   1062 			for (i = 0; i < sme->sme_nsensors; i++) {
   1063 				if (i == edata->sensor)
   1064 					continue;
   1065 
   1066 				nedata = &sme->sme_sensor_data[i];
   1067 				if (strcmp(blah, nedata->desc) == 0) {
   1068 					error = EEXIST;
   1069 					goto out;
   1070 				}
   1071 			}
   1072 
   1073 			SENSOR_UPSTRING(dict, "description", blah);
   1074 			(void)strlcpy(edata->desc, blah, sizeof(edata->desc));
   1075 
   1076 			break;
   1077 		}
   1078 
   1079 		/* did the user want to remove a critical capacity limit? */
   1080 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
   1081 		if (obj2 != NULL) {
   1082 			targetfound = true;
   1083 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1084 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1085 				    error = ENOTSUP;
   1086 				    break;
   1087 			}
   1088 
   1089 			sname = prop_string_cstring_nocopy(obj);
   1090 			error = sme_event_unregister(sname,
   1091 			    PENVSYS_EVENT_BATT_USERCAP);
   1092 			if (error)
   1093 				break;
   1094 
   1095 			prop_dictionary_remove(dict, "critical-capacity");
   1096 			break;
   1097 		}
   1098 
   1099 		/* did the user want to remove a critical min limit? */
   1100 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
   1101 		if (obj2 != NULL) {
   1102 			targetfound = true;
   1103 			sname = prop_string_cstring_nocopy(obj);
   1104 			error = sme_event_unregister(sname,
   1105 			    PENVSYS_EVENT_USER_CRITMIN);
   1106 			if (error)
   1107 				break;
   1108 
   1109 			prop_dictionary_remove(dict, "critical-min-limit");
   1110 			break;
   1111 		}
   1112 
   1113 		/* did the user want to remove a critical max limit? */
   1114 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1115 		if (obj2 != NULL) {
   1116 			targetfound = true;
   1117 			sname = prop_string_cstring_nocopy(obj);
   1118 			error = sme_event_unregister(sname,
   1119 			    PENVSYS_EVENT_USER_CRITMAX);
   1120 			if (error)
   1121 				break;
   1122 
   1123 			prop_dictionary_remove(dict, "critical-max-limit");
   1124 			break;
   1125 		}
   1126 
   1127 		/* did the user want to change rfact? */
   1128 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1129 		if (obj2 != NULL) {
   1130 			targetfound = true;
   1131 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1132 				edata->rfact = prop_number_integer_value(obj2);
   1133 			else
   1134 				error = ENOTSUP;
   1135 
   1136 			break;
   1137 		}
   1138 
   1139 		for (i = 0; sst[i].type != -1; i++)
   1140 			if (sst[i].type == edata->units)
   1141 				break;
   1142 
   1143 		/* did the user want to set a critical capacity event? */
   1144 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1145 		if (obj2 != NULL) {
   1146 			targetfound = true;
   1147 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1148 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1149 				error = ENOTSUP;
   1150 				break;
   1151 			}
   1152 
   1153 			critval = prop_number_integer_value(obj2);
   1154 			error = sme_event_add(dict,
   1155 					      edata,
   1156 					      sme->sme_name,
   1157 					      "critical-capacity",
   1158 					      critval,
   1159 					      PENVSYS_EVENT_BATT_USERCAP,
   1160 					      sst[i].crittype);
   1161 			break;
   1162 		}
   1163 
   1164 		/* did the user want to set a critical max event? */
   1165 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1166 		if (obj2 != NULL) {
   1167 			targetfound = true;
   1168 			if (edata->units == ENVSYS_INDICATOR ||
   1169 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1170 				error = ENOTSUP;
   1171 				break;
   1172 			}
   1173 
   1174 			critval = prop_number_integer_value(obj2);
   1175 			error = sme_event_add(dict,
   1176 					      edata,
   1177 					      sme->sme_name,
   1178 					      "critical-max-limit",
   1179 					      critval,
   1180 					      PENVSYS_EVENT_USER_CRITMAX,
   1181 					      sst[i].crittype);
   1182 			break;
   1183 		}
   1184 
   1185 		/* did the user want to set a critical min event? */
   1186 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1187 		if (obj2 != NULL) {
   1188 			targetfound = true;
   1189 			if (edata->units == ENVSYS_INDICATOR ||
   1190 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1191 				error = ENOTSUP;
   1192 				break;
   1193 			}
   1194 
   1195 			critval = prop_number_integer_value(obj2);
   1196 			error = sme_event_add(dict,
   1197 					      edata,
   1198 					      sme->sme_name,
   1199 					      "critical-min-limit",
   1200 					      critval,
   1201 					      PENVSYS_EVENT_USER_CRITMIN,
   1202 					      sst[i].crittype);
   1203 			break;
   1204 		}
   1205 	}
   1206 
   1207 	/* invalid target? return the error */
   1208 	if (!targetfound)
   1209 		error = EINVAL;
   1210 
   1211 out:
   1212 	return error;
   1213 }
   1214