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