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