Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.42
      1 /*	$NetBSD: sysmon_envsys.c,v 1.42 2007/07/23 08:45:51 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.42 2007/07/23 08:45:51 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,			"unknown" }
    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 static int sme_uniqsensors = 0;
    160 
    161 kmutex_t sme_mtx, sme_event_mtx;
    162 kcondvar_t sme_event_cv;
    163 
    164 #ifdef COMPAT_40
    165 static u_int sysmon_envsys_next_sensor_index = 0;
    166 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    167 #endif
    168 
    169 static void sysmon_envsys_release(struct sysmon_envsys *);
    170 static int sme_register_sensorname(envsys_data_t *);
    171 
    172 /*
    173  * sysmon_envsys_init:
    174  *
    175  * 	+ Initialize global mutexes, dictionary and the linked lists.
    176  */
    177 void
    178 sysmon_envsys_init(void)
    179 {
    180 	LIST_INIT(&sysmon_envsys_list);
    181 	LIST_INIT(&sme_events_list);
    182 	mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
    183 	mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
    184 	mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
    185 	mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
    186 	cv_init(&sme_list_cv, "smefind");
    187 	cv_init(&sme_event_cv, "smeevent");
    188 	sme_propd = prop_dictionary_create();
    189 }
    190 
    191 /*
    192  * sysmonopen_envsys:
    193  *
    194  *	+ Open the system monitor device.
    195  */
    196 int
    197 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    198 {
    199 	return 0;
    200 }
    201 
    202 /*
    203  * sysmonclose_envsys:
    204  *
    205  *	+ Close the system monitor device.
    206  */
    207 int
    208 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    209 {
    210 	/* Nothing to do */
    211 	return 0;
    212 }
    213 
    214 /*
    215  * sysmonioctl_envsys:
    216  *
    217  *	+ Perform an envsys control request.
    218  */
    219 int
    220 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    221 {
    222 	struct sysmon_envsys *sme = NULL;
    223 	int error = 0;
    224 #ifdef COMPAT_40
    225 	u_int oidx;
    226 #endif
    227 
    228 	switch (cmd) {
    229 	case ENVSYS_GETDICTIONARY:
    230 	    {
    231 		struct plistref *plist = (struct plistref *)data;
    232 
    233 		/*
    234 		 * Update all sysmon envsys devices dictionaries with
    235 		 * new data if it's different than we have currently
    236 		 * in the dictionary.
    237 		 */
    238 		mutex_enter(&sme_list_mtx);
    239 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    240 			if (sme == NULL)
    241 				continue;
    242 
    243 			mutex_enter(&sme_mtx);
    244 			error = sme_update_dictionary(sme);
    245 			if (error) {
    246 				DPRINTF(("%s: sme_update_dictionary, "
    247 				    "error=%d\n", __func__, error));
    248 				mutex_exit(&sme_list_mtx);
    249 				mutex_exit(&sme_mtx);
    250 				return error;
    251 			}
    252 			mutex_exit(&sme_mtx);
    253 		}
    254 		mutex_exit(&sme_list_mtx);
    255 		/*
    256 		 * Copy global dictionary to userland.
    257 		 */
    258 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    259 		break;
    260 	    }
    261 	case ENVSYS_SETDICTIONARY:
    262 	    {
    263 		const struct plistref *plist = (const struct plistref *)data;
    264 		prop_dictionary_t udict;
    265 		prop_object_t obj;
    266 		const char *devname = NULL;
    267 
    268 		if ((flag & FWRITE) == 0)
    269 			return EPERM;
    270 
    271 		/*
    272 		 * Get dictionary from userland.
    273 		 */
    274 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    275 		if (error) {
    276 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    277 			    __func__, error));
    278 			break;
    279 		}
    280 
    281 		/*
    282 		 * Parse "driver-name" key to obtain the driver we
    283 		 * are searching for.
    284 		 */
    285 		obj = prop_dictionary_get(udict, "driver-name");
    286 		if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
    287 			DPRINTF(("%s: driver-name failed\n", __func__));
    288 			prop_object_release(udict);
    289 			error = EINVAL;
    290 			break;
    291 		}
    292 
    293 		/* driver name */
    294 		devname = prop_string_cstring_nocopy(obj);
    295 
    296 		/* find the correct sme device */
    297 		sme = sysmon_envsys_find(devname);
    298 		if (sme == NULL) {
    299 			DPRINTF(("%s: NULL sme\n", __func__));
    300 			prop_object_release(udict);
    301 			error = EINVAL;
    302 			break;
    303 		}
    304 
    305 		/*
    306 		 * Find the correct array object with the string supplied
    307 		 * by the userland dictionary.
    308 		 */
    309 		obj = prop_dictionary_get(sme_propd, devname);
    310 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    311 			DPRINTF(("%s: array device failed\n", __func__));
    312 			prop_object_release(udict);
    313 			error = EINVAL;
    314 			break;
    315 		}
    316 
    317 		/* do the real work now */
    318 		error = sme_userset_dictionary(sme, udict, obj);
    319 		sysmon_envsys_release(sme);
    320 		break;
    321 	    }
    322 	    /*
    323 	     * Compatibility functions with the old interface, only
    324 	     * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
    325 	     * to make old applications work.
    326 	     */
    327 #ifdef COMPAT_40
    328 	case ENVSYS_GTREDATA:
    329 	    {
    330 		struct envsys_tre_data *tred = (void *)data;
    331 		envsys_data_t *edata = NULL;
    332 
    333 		tred->validflags = 0;
    334 
    335 		sme = sysmon_envsys_find_40(tred->sensor);
    336 		if (sme == NULL)
    337 			break;
    338 
    339 		mutex_enter(&sme_mtx);
    340 		oidx = tred->sensor;
    341 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    342 
    343 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    344 		    __func__, tred->sensor, oidx, sme->sme_name,
    345 		    sme->sme_nsensors));
    346 
    347 		edata = &sme->sme_sensor_data[tred->sensor];
    348 
    349 		if (tred->sensor < sme->sme_nsensors) {
    350 			if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    351 				error = (*sme->sme_gtredata)(sme, edata);
    352 				if (error) {
    353 					DPRINTF(("%s: sme_gtredata failed\n",
    354 				    	    __func__));
    355 					mutex_exit(&sme_mtx);
    356 					return error;
    357 				}
    358 			}
    359 
    360 			/* copy required values to the old interface */
    361 			tred->sensor = edata->sensor;
    362 			tred->cur.data_us = edata->value_cur;
    363 			tred->cur.data_s = edata->value_cur;
    364 			tred->max.data_us = edata->value_max;
    365 			tred->max.data_s = edata->value_max;
    366 			tred->min.data_us = edata->value_min;
    367 			tred->min.data_s = edata->value_min;
    368 			tred->avg.data_us = edata->value_avg;
    369 			tred->avg.data_s = edata->value_avg;
    370 			tred->units = edata->units;
    371 
    372 			tred->validflags |= ENVSYS_FVALID;
    373 			tred->validflags |= ENVSYS_FCURVALID;
    374 
    375 			if (edata->flags & ENVSYS_FPERCENT) {
    376 				tred->validflags |= ENVSYS_FMAXVALID;
    377 				tred->validflags |= ENVSYS_FFRACVALID;
    378 			}
    379 
    380 			if (edata->state == ENVSYS_SINVALID ||
    381 			    edata->flags & ENVSYS_FNOTVALID) {
    382 				tred->validflags &= ~ENVSYS_FCURVALID;
    383 				tred->cur.data_us = tred->cur.data_s = 0;
    384 			}
    385 
    386 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    387 			    __func__, edata->desc, tred->cur.data_s));
    388 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    389 			    " tred->sensor=%d\n", __func__, tred->validflags,
    390 			    tred->units, tred->sensor));
    391 		}
    392 		tred->sensor = oidx;
    393 		mutex_exit(&sme_mtx);
    394 
    395 		break;
    396 	    }
    397 	case ENVSYS_GTREINFO:
    398 	    {
    399 		struct envsys_basic_info *binfo = (void *)data;
    400 		envsys_data_t *edata = NULL;
    401 
    402 		binfo->validflags = 0;
    403 
    404 		sme = sysmon_envsys_find_40(binfo->sensor);
    405 		if (sme == NULL)
    406 			break;
    407 
    408 		mutex_enter(&sme_mtx);
    409 		oidx = binfo->sensor;
    410 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    411 
    412 		edata = &sme->sme_sensor_data[binfo->sensor];
    413 
    414 		binfo->validflags |= ENVSYS_FVALID;
    415 
    416 		if (binfo->sensor < sme->sme_nsensors) {
    417 			binfo->units = edata->units;
    418 			(void)strlcpy(binfo->desc, edata->desc,
    419 			    sizeof(binfo->desc));
    420 		}
    421 
    422 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    423 		    __func__, binfo->units, binfo->validflags));
    424 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    425 		    __func__, binfo->desc, binfo->sensor));
    426 
    427 		binfo->sensor = oidx;
    428 		mutex_exit(&sme_mtx);
    429 
    430 		break;
    431 	    }
    432 #endif /* COMPAT_40 */
    433 	default:
    434 		error = ENOTTY;
    435 		break;
    436 	}
    437 
    438 	return error;
    439 }
    440 
    441 /*
    442  * sysmon_envsys_register:
    443  *
    444  *	+ Register an envsys device.
    445  *	+ Create device dictionary.
    446  */
    447 int
    448 sysmon_envsys_register(struct sysmon_envsys *sme)
    449 {
    450 	struct sysmon_envsys *lsme;
    451 	int error = 0;
    452 
    453 	/*
    454 	 * sanity check 1, make sure the driver has initialized
    455 	 * the sensors count or the value is not too high.
    456 	 */
    457 	if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
    458 		return EINVAL;
    459 
    460 	/*
    461 	 * sanity check 2, make sure that sme->sme_name and
    462 	 * sme->sme_data are not NULL.
    463 	 */
    464 	if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
    465 		return EINVAL;
    466 
    467 	/*
    468 	 * sanity check 3, if SME_DISABLE_GTREDATA is not set
    469 	 * the sme_gtredata function callback must be non NULL.
    470 	 */
    471 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    472 		if (sme->sme_gtredata == NULL)
    473 			return EINVAL;
    474 	}
    475 
    476 	/*
    477 	 * - check if requested sysmon_envsys device is valid
    478 	 *   and does not exist already in the list.
    479 	 * - add device into the list.
    480 	 * - create the plist structure.
    481 	 */
    482 	mutex_enter(&sme_list_mtx);
    483 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    484 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    485 		       mutex_exit(&sme_list_mtx);
    486 		       error = EEXIST;
    487 		       goto out;
    488 	       }
    489 	}
    490 
    491 	/* initialize the singly linked list for sensor descriptions */
    492 	SLIST_INIT(&sme_names_list);
    493 
    494 #ifdef COMPAT_40
    495 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    496 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    497 #endif
    498 	mutex_exit(&sme_list_mtx);
    499 	error = sysmon_envsys_createplist(sme);
    500 	if (!error) {
    501 		mutex_enter(&sme_list_mtx);
    502 		LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    503 		sme_uniqsensors = 0;
    504 		mutex_exit(&sme_list_mtx);
    505 	}
    506 
    507 out:
    508 	return error;
    509 }
    510 
    511 /*
    512  * sysmon_envsys_unregister:
    513  *
    514  *	+ Unregister an envsys device.
    515  *	+ Unregister all events associated with this device.
    516  *	+ Remove device dictionary.
    517  */
    518 void
    519 sysmon_envsys_unregister(struct sysmon_envsys *sme)
    520 {
    521 	struct sme_sensor_names *snames;
    522 
    523 	KASSERT(sme != NULL);
    524 
    525 	mutex_enter(&sme_list_mtx);
    526 	while (sme->sme_flags & SME_FLAG_BUSY) {
    527 		sme->sme_flags |= SME_FLAG_WANTED;
    528 		cv_wait(&sme_list_cv, &sme_list_mtx);
    529 	}
    530 	prop_dictionary_remove(sme_propd, sme->sme_name);
    531 	/*
    532 	 * Unregister all events associated with this device.
    533 	 */
    534 	mutex_exit(&sme_list_mtx);
    535 	sme_event_unregister_all(sme);
    536 	mutex_enter(&sme_list_mtx);
    537 
    538 	/*
    539 	 * Remove all elements from the sensor names singly linked list.
    540 	 */
    541 	while (!SLIST_EMPTY(&sme_names_list)) {
    542 		snames = SLIST_FIRST(&sme_names_list);
    543 		SLIST_REMOVE_HEAD(&sme_names_list, sme_names);
    544 		mutex_exit(&sme_list_mtx);
    545 		kmem_free(snames, sizeof(*snames));
    546 		mutex_enter(&sme_list_mtx);
    547 	}
    548 	LIST_REMOVE(sme, sme_list);
    549 	mutex_exit(&sme_list_mtx);
    550 }
    551 
    552 /*
    553  * sysmon_envsys_find:
    554  *
    555  *	+ Find an envsys device.
    556  */
    557 struct sysmon_envsys *
    558 sysmon_envsys_find(const char *name)
    559 {
    560 	struct sysmon_envsys *sme;
    561 
    562 	mutex_enter(&sme_list_mtx);
    563 again:
    564 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    565 	     sme = LIST_NEXT(sme, sme_list)) {
    566 			if (strcmp(sme->sme_name, name) == 0) {
    567 				if (sme->sme_flags & SME_FLAG_BUSY) {
    568 					sme->sme_flags |= SME_FLAG_WANTED;
    569 					cv_wait(&sme_list_cv, &sme_list_mtx);
    570 					goto again;
    571 				}
    572 				sme->sme_flags |= SME_FLAG_BUSY;
    573 				break;
    574 			}
    575 	}
    576 	mutex_exit(&sme_list_mtx);
    577 	return sme;
    578 }
    579 
    580 /*
    581  * sysmon_envsys_release:
    582  *
    583  * 	+ Release an envsys device.
    584  */
    585 void
    586 sysmon_envsys_release(struct sysmon_envsys *sme)
    587 {
    588 	mutex_enter(&sme_list_mtx);
    589 	if (sme->sme_flags & SME_FLAG_WANTED)
    590 		cv_broadcast(&sme_list_cv);
    591 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    592 	mutex_exit(&sme_list_mtx);
    593 }
    594 
    595 /* compatibility function */
    596 #ifdef COMPAT_40
    597 struct sysmon_envsys *
    598 sysmon_envsys_find_40(u_int idx)
    599 {
    600 	struct sysmon_envsys *sme;
    601 
    602 	mutex_enter(&sme_list_mtx);
    603 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    604 	     sme = LIST_NEXT(sme, sme_list)) {
    605 		if (idx >= sme->sme_fsensor &&
    606 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    607 			break;
    608 	}
    609 	mutex_exit(&sme_list_mtx);
    610 	return sme;
    611 }
    612 #endif
    613 
    614 /*
    615  * sysmon_envsys_createplist:
    616  *
    617  * 	+ Create the property list structure for a device.
    618  */
    619 int
    620 sysmon_envsys_createplist(struct sysmon_envsys *sme)
    621 {
    622 	envsys_data_t *edata;
    623 	prop_array_t array;
    624 	int i, nsens, error;
    625 
    626 	nsens = error = 0;
    627 
    628 	/* create the sysmon envsys device array. */
    629 	array = prop_array_create();
    630 	if (array == NULL)
    631 		return EINVAL;
    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 		mutex_enter(&sme_mtx);
    639 		/*
    640 		 * XXX:
    641 		 *
    642 		 * workaround for LKMs. First sensor used in a LKM,
    643 		 * gets the index in sensor from all edata structures
    644 		 * allocated in kernel. It is unknown to me why this
    645 		 * value is not 0 when using a LKM.
    646 		 *
    647 		 * For now, overwrite its index to 0.
    648 		 */
    649 			if (i == 0)
    650 				sme->sme_sensor_data[0].sensor = 0;
    651 
    652 		edata = &sme->sme_sensor_data[i];
    653 		/*
    654 		 * refresh sensor data via sme_gtredata only if the
    655 		 * flag is not set.
    656 		 */
    657 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    658 			if ((*sme->sme_gtredata)(sme, edata)) {
    659 				DPRINTF(("%s: sme->sme_gtredata[%d]\n",
    660 				    __func__, i));
    661 				mutex_exit(&sme_mtx);
    662 				continue;
    663 			}
    664 		}
    665 		mutex_exit(&sme_mtx);
    666 		sme_make_dictionary(sme, array, edata);
    667 	}
    668 
    669 	if (prop_array_count(array) == 0) {
    670 		error = EINVAL;
    671 		prop_object_release(array);
    672 		return error;
    673 	}
    674 
    675 	/*
    676 	 * <dict>
    677 	 * 	<key>foo0</key>
    678 	 * 	<array>
    679 	 * 		...
    680 	 */
    681 	mutex_enter(&sme_mtx);
    682 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    683 		DPRINTF(("%s: prop_dictionary_set\n", __func__));
    684 		error = EINVAL;
    685 	}
    686 	mutex_exit(&sme_mtx);
    687 
    688 
    689 	prop_object_release(array);
    690 	return error;
    691 }
    692 
    693 /*
    694  * sme_register_sensorname:
    695  *
    696  * 	+ Registers a sensor name into the list maintained per device.
    697  */
    698 static int
    699 sme_register_sensorname(envsys_data_t *edata)
    700 {
    701 	struct sme_sensor_names *snames, *snames2 = NULL;
    702 
    703 	KASSERT(edata != NULL);
    704 
    705 	snames = kmem_zalloc(sizeof(*snames), KM_SLEEP);
    706 
    707 	mutex_enter(&sme_list_mtx);
    708 	SLIST_FOREACH(snames2, &sme_names_list, sme_names) {
    709 		/*
    710 		 * Match sensors with empty and duplicate description.
    711 		 */
    712 		if (strlen(edata->desc) == 0 ||
    713 		    strcmp(snames2->desc, edata->desc) == 0)
    714 			if (snames2->assigned) {
    715 				edata->flags |= ENVSYS_FNOTVALID;
    716 				mutex_exit(&sme_list_mtx);
    717 				DPRINTF(("%s: duplicate name "
    718 				    "(%s)\n", __func__, edata->desc));
    719 				kmem_free(snames, sizeof(*snames));
    720 				return EEXIST;
    721 			}
    722 	}
    723 
    724 	snames->assigned = true;
    725 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
    726 	DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
    727 	SLIST_INSERT_HEAD(&sme_names_list, snames, sme_names);
    728 	sme_uniqsensors++;
    729 	mutex_exit(&sme_list_mtx);
    730 
    731 	return 0;
    732 }
    733 
    734 /*
    735  * sme_make_dictionary:
    736  *
    737  * 	+ Create sensor's dictionary in device's array.
    738  */
    739 void
    740 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    741 		    envsys_data_t *edata)
    742 {
    743 	const struct sme_sensor_type *est = sme_sensor_type;
    744 	const struct sme_sensor_state *ess = sme_sensor_state;
    745 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    746 	sme_event_drv_t *sme_evdrv_t = NULL;
    747 	prop_dictionary_t dict;
    748 	int i, j;
    749 
    750 	i = j = 0;
    751 
    752 
    753 	/*
    754 	 * <array>
    755 	 * 	<dict>
    756 	 * 		...
    757 	 */
    758 	dict = prop_dictionary_create();
    759 	if (dict == NULL) {
    760 		DPRINTF(("%s: couldn't allocate a dictionary\n", __func__));
    761 		goto invalidate_sensor;
    762 	}
    763 
    764 	/* find the correct unit for this sensor. */
    765 	for (i = 0; est[i].type != -1; i++)
    766 		if (est[i].type == edata->units)
    767 			break;
    768 
    769 	if (strcmp(est[i].desc, "unknown") == 0) {
    770 		DPRINTF(("%s: invalid units type for sensor=%d\n",
    771 		    __func__, edata->sensor));
    772 		goto invalidate_sensor;
    773 	}
    774 
    775 	/*
    776 	 * 		...
    777 	 * 		<key>type</key>
    778 	 * 		<string>foo</string>
    779 	 * 		<key>description</key>
    780 	 * 		<string>blah blah</string>
    781 	 * 		...
    782 	 */
    783 	if (sme_sensor_upstring(dict, "type", est[i].desc))
    784 		goto invalidate_sensor;
    785 
    786 	if (strlen(edata->desc) == 0) {
    787 		DPRINTF(("%s: invalid description for sensor=%d\n",
    788 		    __func__, edata->sensor));
    789 		goto invalidate_sensor;
    790 	}
    791 
    792 	/*
    793 	 * Check if description was already assigned in other sensor.
    794 	 */
    795 	if (sme_register_sensorname(edata))
    796 		goto out;
    797 
    798 	if (sme_sensor_upstring(dict, "description", edata->desc))
    799 		goto invalidate_sensor;
    800 
    801 	/*
    802 	 * Add sensor's state description.
    803 	 *
    804 	 * 		...
    805 	 * 		<key>state</key>
    806 	 * 		<string>valid</string>
    807 	 * 		...
    808 	 */
    809 	for (j = 0; ess[j].type != -1; j++)
    810 		if (ess[j].type == edata->state)
    811 			break;
    812 
    813 	if (strcmp(ess[j].desc, "unknown") == 0) {
    814 		DPRINTF(("%s: invalid state for sensor=%d\n",
    815 		    __func__, edata->sensor));
    816 		goto invalidate_sensor;
    817 	}
    818 
    819 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
    820 	    __func__, edata->desc, edata->units, edata->state));
    821 
    822 	if (sme_sensor_upstring(dict, "state", ess[j].desc))
    823 		goto invalidate_sensor;
    824 
    825 	/*
    826 	 * add the percentage boolean object:
    827 	 *
    828 	 * 		...
    829 	 * 		<key>want-percentage</key>
    830 	 * 		<true/>
    831 	 * 		...
    832 	 */
    833 	if (edata->flags & ENVSYS_FPERCENT)
    834 		if (sme_sensor_upbool(dict, "want-percentage", true))
    835 			goto out;
    836 
    837 	/*
    838 	 * Add the monitoring boolean object:
    839 	 *
    840 	 * 		...
    841 	 * 		<key>monitoring-supported</key>
    842 	 * 		<true/>
    843 	 *		...
    844 	 *
    845 	 * always false on Drive and Indicator types, they
    846 	 * cannot be monitored.
    847 	 *
    848 	 */
    849 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    850 	    (edata->units == ENVSYS_INDICATOR) ||
    851 	    (edata->units == ENVSYS_DRIVE)) {
    852 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
    853 			goto out;
    854 	} else {
    855 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
    856 			goto out;
    857 	}
    858 
    859 	/*
    860 	 * Add the drive-state object for drive sensors:
    861 	 *
    862 	 * 		...
    863 	 * 		<key>drive-state</key>
    864 	 * 		<string>drive is online</string>
    865 	 * 		...
    866 	 */
    867 	if (edata->units == ENVSYS_DRIVE) {
    868 		for (j = 0; esds[j].type != -1; j++)
    869 			if (esds[j].type == edata->value_cur)
    870 				break;
    871 
    872 		if (sme_sensor_upstring(dict, "drive-state", esds[j].desc))
    873 			goto out;
    874 	}
    875 
    876 	/* if sensor is enabled, add the following properties... */
    877 	if (edata->state == ENVSYS_SVALID) {
    878 		/*
    879 		 * 	...
    880 		 * 	<key>rpms</key>
    881 		 * 	<integer>2500</integer>
    882 		 * 	<key>rfact</key>
    883 		 * 	<integer>10000</integer>
    884 		 * 	<key>cur-value</key>
    885 		 * 	<integer>1250</integer>
    886 		 * 	<key>min-value</key>
    887 		 * 	<integer>800</integer>
    888 		 * 	<key>max-value</integer>
    889 		 * 	<integer>3000</integer>
    890 		 * 	<key>avg-value</integer>
    891 		 * 	<integer>1400</integer>
    892 		 * </dict>
    893 		 */
    894 		if (edata->units == ENVSYS_SFANRPM)
    895 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
    896 				goto out;
    897 
    898 		if (edata->units == ENVSYS_SVOLTS_AC ||
    899 		    edata->units == ENVSYS_SVOLTS_DC)
    900 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
    901 				goto out;
    902 
    903 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
    904 			goto out;
    905 
    906 		if (edata->flags & ENVSYS_FVALID_MIN) {
    907 			if (sme_sensor_upint32(dict,
    908 					       "min-value",
    909 					       edata->value_min))
    910 				goto out;
    911 		}
    912 
    913 		if (edata->flags & ENVSYS_FVALID_MAX) {
    914 			if (sme_sensor_upint32(dict,
    915 					       "max-value",
    916 					       edata->value_max))
    917 				goto out;
    918 		}
    919 
    920 		if (edata->flags & ENVSYS_FVALID_AVG) {
    921 			if (sme_sensor_upint32(dict,
    922 					       "avg-value",
    923 					        edata->value_avg))
    924 				goto out;
    925 		}
    926 	}
    927 
    928 	/*
    929 	 * 	...
    930 	 * </array>
    931 	 */
    932 
    933 	mutex_enter(&sme_mtx);
    934 	if (!prop_array_set(array, sme_uniqsensors - 1, dict)) {
    935 		mutex_exit(&sme_mtx);
    936 		DPRINTF(("%s: prop_array_add\n", __func__));
    937 		goto invalidate_sensor;
    938 	}
    939 	mutex_exit(&sme_mtx);
    940 
    941 	/*
    942 	 * Add a new event if a monitoring flag was set.
    943 	 */
    944 	if (edata->monitor) {
    945 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
    946 
    947 		sme_evdrv_t->sdict = dict;
    948 		sme_evdrv_t->edata = edata;
    949 		sme_evdrv_t->sme = sme;
    950 		sme_evdrv_t->powertype = est[i].crittype;
    951 
    952 		sysmon_task_queue_init();
    953 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
    954 	}
    955 
    956 out:
    957 	prop_object_release(dict);
    958 	return;
    959 
    960 invalidate_sensor:
    961 	mutex_enter(&sme_mtx);
    962 	edata->flags |= ENVSYS_FNOTVALID;
    963 	mutex_exit(&sme_mtx);
    964 	prop_object_release(dict);
    965 }
    966 
    967 /*
    968  * sme_update_dictionary:
    969  *
    970  * 	+ Update per-sensor dictionaries with new values if there were
    971  * 	  changes, otherwise the object in dictionary is untouched.
    972  * 	+ Send a critical event if any sensor is in a critical condition.
    973  */
    974 int
    975 sme_update_dictionary(struct sysmon_envsys *sme)
    976 {
    977 	const struct sme_sensor_state *ess = sme_sensor_state;
    978 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    979 	envsys_data_t *edata = NULL;
    980 	prop_object_t array, dict;
    981 	int i, j, error, invalid;
    982 
    983 	error = invalid = 0;
    984 	array = dict = NULL;
    985 
    986 	/* retrieve the array of dictionaries in device. */
    987 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    988 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    989 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
    990 		return EINVAL;
    991 	}
    992 
    993 	/*
    994 	 * - iterate over all sensors.
    995 	 * - fetch new data.
    996 	 * - check if data in dictionary is different than new data.
    997 	 * - update dictionary if there were changes.
    998 	 */
    999 	for (i = 0; i < sme->sme_nsensors; i++) {
   1000 		edata = &sme->sme_sensor_data[i];
   1001 		/* skip invalid sensors */
   1002 		if (edata->flags & ENVSYS_FNOTVALID) {
   1003 			invalid++;
   1004 			continue;
   1005 		}
   1006 
   1007 		/*
   1008 		 * refresh sensor data via sme_gtredata only if the
   1009 		 * flag is not set.
   1010 		 */
   1011 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
   1012 			error = (*sme->sme_gtredata)(sme, edata);
   1013 			if (error) {
   1014 				DPRINTF(("%s: gtredata[%d] failed\n",
   1015 				    __func__, i));
   1016 				return error;
   1017 			}
   1018 		}
   1019 
   1020 		/* retrieve sensor's dictionary. */
   1021 		dict = prop_array_get(array, i - invalid);
   1022 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1023 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1024 			    __func__, edata->sensor, sme->sme_name));
   1025 			return EINVAL;
   1026 		}
   1027 
   1028 		/* update state sensor. */
   1029 		for (j = 0; ess[j].type != -1; j++)
   1030 			if (ess[j].type == edata->state)
   1031 				break;
   1032 
   1033 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1034 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
   1035 		    ess[j].type, edata->flags, edata->units, edata->sensor));
   1036 
   1037 		/* update sensor state */
   1038 		error = sme_sensor_upstring(dict, "state", ess[j].desc);
   1039 		if (error)
   1040 			break;
   1041 
   1042 		/* update sensor current value */
   1043 		error = sme_sensor_upint32(dict,
   1044 					   "cur-value",
   1045 					   edata->value_cur);
   1046 		if (error)
   1047 			break;
   1048 
   1049 		/*
   1050 		 * Integer and Indicator types do not the following
   1051 		 * values, so skip them.
   1052 		 */
   1053 		if (edata->units == ENVSYS_INTEGER ||
   1054 		    edata->units == ENVSYS_INDICATOR)
   1055 			continue;
   1056 
   1057 		/* update sensor flags */
   1058 		if (edata->flags & ENVSYS_FPERCENT) {
   1059 			error = sme_sensor_upbool(dict,
   1060 						  "want-percentage",
   1061 						  true);
   1062 			if (error)
   1063 				break;
   1064 		}
   1065 
   1066 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1067 			error = sme_sensor_upint32(dict,
   1068 						   "max-value",
   1069 						   edata->value_max);
   1070 			if (error)
   1071 				break;
   1072 		}
   1073 
   1074 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1075 			error = sme_sensor_upint32(dict,
   1076 						   "min-value",
   1077 						   edata->value_min);
   1078 			if (error)
   1079 				break;
   1080 		}
   1081 
   1082 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1083 			error = sme_sensor_upint32(dict,
   1084 						   "avg-value",
   1085 						   edata->value_avg);
   1086 			if (error)
   1087 				break;
   1088 		}
   1089 
   1090 		/* update 'rpms' only in ENVSYS_SFANRPM. */
   1091 		if (edata->units == ENVSYS_SFANRPM) {
   1092 			error = sme_sensor_upuint32(dict,
   1093 						    "rpms",
   1094 						    edata->rpms);
   1095 			if (error)
   1096 				break;
   1097 		}
   1098 
   1099 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
   1100 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1101 		    edata->units == ENVSYS_SVOLTS_DC) {
   1102 			error = sme_sensor_upint32(dict,
   1103 						   "rfact",
   1104 						   edata->rfact);
   1105 			if (error)
   1106 				break;
   1107 		}
   1108 
   1109 		/* update 'drive-state' only in ENVSYS_DRIVE. */
   1110 		if (edata->units == ENVSYS_DRIVE) {
   1111 			for (j = 0; esds[j].type != -1; j++)
   1112 				if (esds[j].type == edata->value_cur)
   1113 					break;
   1114 
   1115 			error = sme_sensor_upstring(dict,
   1116 						    "drive-state",
   1117 						    esds[j].desc);
   1118 			if (error)
   1119 				break;
   1120 		}
   1121 	}
   1122 
   1123 	return error;
   1124 }
   1125 
   1126 /*
   1127  * sme_userset_dictionary:
   1128  *
   1129  * 	+ Parses the userland dictionary and run the appropiate
   1130  * 	  tasks that were requested.
   1131  */
   1132 int
   1133 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1134 		       prop_array_t array)
   1135 {
   1136 	const struct sme_sensor_type *sst = sme_sensor_type;
   1137 	envsys_data_t *edata, *nedata;
   1138 	prop_dictionary_t dict;
   1139 	prop_object_t obj, obj1, obj2;
   1140 	int32_t critval;
   1141 	int i, invalid, error;
   1142 	const char *blah, *sname;
   1143 	bool targetfound = false;
   1144 
   1145 	error = invalid = 0;
   1146 	blah = sname = NULL;
   1147 
   1148 	/* get sensor's name from userland dictionary. */
   1149 	obj = prop_dictionary_get(udict, "sensor-name");
   1150 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1151 		DPRINTF(("%s: sensor-name failed\n", __func__));
   1152 		return EINVAL;
   1153 	}
   1154 
   1155 	/* iterate over the sensors to find the right one */
   1156 	for (i = 0; i < sme->sme_nsensors; i++) {
   1157 		edata = &sme->sme_sensor_data[i];
   1158 		/* skip sensors with duplicate description */
   1159 		if (edata->flags & ENVSYS_FNOTVALID) {
   1160 			invalid++;
   1161 			continue;
   1162 		}
   1163 
   1164 		dict = prop_array_get(array, i - invalid);
   1165 		obj1 = prop_dictionary_get(dict, "description");
   1166 
   1167 		/* is it our sensor? */
   1168 		if (!prop_string_equals(obj1, obj))
   1169 			continue;
   1170 
   1171 		/*
   1172 		 * Check if a new description operation was
   1173 		 * requested by the user and set new description.
   1174 		 */
   1175 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
   1176 			targetfound = true;
   1177 			blah = prop_string_cstring_nocopy(obj2);
   1178 
   1179 			for (i = 0; i < sme->sme_nsensors; i++) {
   1180 				if (i == edata->sensor)
   1181 					continue;
   1182 
   1183 				nedata = &sme->sme_sensor_data[i];
   1184 				if (strcmp(blah, nedata->desc) == 0) {
   1185 					error = EEXIST;
   1186 					break;
   1187 				}
   1188 			}
   1189 
   1190 			if (error)
   1191 				break;
   1192 
   1193 			error = sme_sensor_upstring(dict,
   1194 						    "description",
   1195 						    blah);
   1196 			if (!error)
   1197 				(void)strlcpy(edata->desc,
   1198 					      blah,
   1199 					      sizeof(edata->desc));
   1200 
   1201 			break;
   1202 		}
   1203 
   1204 		/* did the user want to remove a critical capacity limit? */
   1205 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
   1206 		if (obj2 != NULL) {
   1207 			targetfound = true;
   1208 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1209 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1210 				    error = ENOTSUP;
   1211 				    break;
   1212 			}
   1213 
   1214 			sname = prop_string_cstring_nocopy(obj);
   1215 			error = sme_event_unregister(sname,
   1216 			    PENVSYS_EVENT_BATT_USERCAP);
   1217 			if (error)
   1218 				break;
   1219 
   1220 			prop_dictionary_remove(dict, "critical-capacity");
   1221 			break;
   1222 		}
   1223 
   1224 		/* did the user want to remove a critical min limit? */
   1225 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
   1226 		if (obj2 != NULL) {
   1227 			targetfound = true;
   1228 			sname = prop_string_cstring_nocopy(obj);
   1229 			error = sme_event_unregister(sname,
   1230 			    PENVSYS_EVENT_USER_CRITMIN);
   1231 			if (error)
   1232 				break;
   1233 
   1234 			prop_dictionary_remove(dict, "critical-min-limit");
   1235 			break;
   1236 		}
   1237 
   1238 		/* did the user want to remove a critical max limit? */
   1239 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1240 		if (obj2 != NULL) {
   1241 			targetfound = true;
   1242 			sname = prop_string_cstring_nocopy(obj);
   1243 			error = sme_event_unregister(sname,
   1244 			    PENVSYS_EVENT_USER_CRITMAX);
   1245 			if (error)
   1246 				break;
   1247 
   1248 			prop_dictionary_remove(dict, "critical-max-limit");
   1249 			break;
   1250 		}
   1251 
   1252 		/* did the user want to change rfact? */
   1253 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1254 		if (obj2 != NULL) {
   1255 			targetfound = true;
   1256 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1257 				edata->rfact = prop_number_integer_value(obj2);
   1258 			else
   1259 				error = ENOTSUP;
   1260 
   1261 			break;
   1262 		}
   1263 
   1264 		for (i = 0; sst[i].type != -1; i++)
   1265 			if (sst[i].type == edata->units)
   1266 				break;
   1267 
   1268 		/* did the user want to set a critical capacity event? */
   1269 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1270 		if (obj2 != NULL) {
   1271 			targetfound = true;
   1272 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1273 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1274 				error = ENOTSUP;
   1275 				break;
   1276 			}
   1277 
   1278 			critval = prop_number_integer_value(obj2);
   1279 			error = sme_event_add(dict,
   1280 					      edata,
   1281 					      sme->sme_name,
   1282 					      "critical-capacity",
   1283 					      critval,
   1284 					      PENVSYS_EVENT_BATT_USERCAP,
   1285 					      sst[i].crittype);
   1286 			break;
   1287 		}
   1288 
   1289 		/* did the user want to set a critical max event? */
   1290 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1291 		if (obj2 != NULL) {
   1292 			targetfound = true;
   1293 			if (edata->units == ENVSYS_INDICATOR ||
   1294 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1295 				error = ENOTSUP;
   1296 				break;
   1297 			}
   1298 
   1299 			critval = prop_number_integer_value(obj2);
   1300 			error = sme_event_add(dict,
   1301 					      edata,
   1302 					      sme->sme_name,
   1303 					      "critical-max-limit",
   1304 					      critval,
   1305 					      PENVSYS_EVENT_USER_CRITMAX,
   1306 					      sst[i].crittype);
   1307 			break;
   1308 		}
   1309 
   1310 		/* did the user want to set a critical min event? */
   1311 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1312 		if (obj2 != NULL) {
   1313 			targetfound = true;
   1314 			if (edata->units == ENVSYS_INDICATOR ||
   1315 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1316 				error = ENOTSUP;
   1317 				break;
   1318 			}
   1319 
   1320 			critval = prop_number_integer_value(obj2);
   1321 			error = sme_event_add(dict,
   1322 					      edata,
   1323 					      sme->sme_name,
   1324 					      "critical-min-limit",
   1325 					      critval,
   1326 					      PENVSYS_EVENT_USER_CRITMIN,
   1327 					      sst[i].crittype);
   1328 			break;
   1329 		}
   1330 	}
   1331 
   1332 	/* invalid target? return the error */
   1333 	if (!targetfound)
   1334 		error = EINVAL;
   1335 
   1336 	return error;
   1337 }
   1338