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