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