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