Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.61
      1 /*	$NetBSD: sysmon_envsys.c,v 1.61 2007/09/08 15:25:18 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.61 2007/09/08 15:25:18 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 		return 0;
    527 	}
    528 out2:
    529 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    530 	    sme->sme_name, error));
    531 	while (!SLIST_EMPTY(&sme_evdrv_list)) {
    532 		sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
    533 		SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
    534 		kmem_free(sme_evdrv, sizeof(*sme_evdrv));
    535 	}
    536 	if (error != EEXIST) {
    537 		mutex_enter(&sme_mtx);
    538 		sme_event_unregister_all(sme->sme_name);
    539 		mutex_exit(&sme_mtx);
    540 	}
    541 	sysmon_envsys_destroy_plist(array);
    542 	return error;
    543 }
    544 
    545 /*
    546  * sysmon_envsys_destroy_plist:
    547  *
    548  * 	+ Remove all objects from the array of dictionaries that is
    549  * 	  created in a sysmon envsys device.
    550  */
    551 static void
    552 sysmon_envsys_destroy_plist(prop_array_t array)
    553 {
    554 	prop_dictionary_t dict;
    555 	prop_object_iterator_t iter, iter2;
    556 	prop_object_t obj;
    557 
    558 	KASSERT(array != NULL);
    559 
    560 	iter = prop_array_iterator(array);
    561 	if (iter == NULL)
    562 		return;
    563 
    564 	while ((dict = prop_object_iterator_next(iter)) != NULL) {
    565 		iter2 = prop_dictionary_iterator(dict);
    566 		if (iter2) {
    567 			while ((obj = prop_object_iterator_next(iter2)) != NULL)
    568 				prop_object_release(obj);
    569 
    570 			prop_object_iterator_release(iter2);
    571 		}
    572 		prop_object_release(dict);
    573 	}
    574 
    575 	prop_object_iterator_release(iter);
    576 }
    577 
    578 /*
    579  * sysmon_envsys_unregister:
    580  *
    581  *	+ Unregister a sysmon envsys device.
    582  */
    583 void
    584 sysmon_envsys_unregister(struct sysmon_envsys *sme)
    585 {
    586 	struct sme_sensor_names *snames;
    587 	prop_array_t array;
    588 
    589 	KASSERT(sme != NULL);
    590 
    591 	mutex_enter(&sme_mtx);
    592 	while (sme->sme_flags & SME_FLAG_BUSY) {
    593 		sme->sme_flags |= SME_FLAG_WANTED;
    594 		cv_wait(&sme_cv, &sme_mtx);
    595 	}
    596 	sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
    597 	/*
    598 	 * Remove all sensor descriptions from the singly linked list.
    599 	 */
    600 	while (!SLIST_EMPTY(&sme->sme_names_list)) {
    601 		snames = SLIST_FIRST(&sme->sme_names_list);
    602 		SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
    603 		kmem_free(snames, sizeof(*snames));
    604 	}
    605 	/*
    606 	 * Unregister all events associated with this device.
    607 	 */
    608 	sme_event_unregister_all(sme->sme_name);
    609 	LIST_REMOVE(sme, sme_list);
    610 	mutex_exit(&sme_mtx);
    611 	/*
    612 	 * Remove the device (and all its objects) from the global dictionary.
    613 	 */
    614 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    615 	if (array) {
    616 		sysmon_envsys_destroy_plist(array);
    617 		prop_dictionary_remove(sme_propd, sme->sme_name);
    618 	}
    619 }
    620 
    621 /*
    622  * sysmon_envsys_find:
    623  *
    624  *	+ Find a sysmon envsys device.
    625  */
    626 struct sysmon_envsys *
    627 sysmon_envsys_find(const char *name)
    628 {
    629 	struct sysmon_envsys *sme;
    630 
    631 	mutex_enter(&sme_mtx);
    632 again:
    633 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    634 	     sme = LIST_NEXT(sme, sme_list)) {
    635 			if (strcmp(sme->sme_name, name) == 0) {
    636 				if (sme->sme_flags & SME_FLAG_BUSY) {
    637 					sme->sme_flags |= SME_FLAG_WANTED;
    638 					cv_wait(&sme_cv, &sme_mtx);
    639 					goto again;
    640 				}
    641 				sme->sme_flags |= SME_FLAG_BUSY;
    642 				break;
    643 			}
    644 	}
    645 	mutex_exit(&sme_mtx);
    646 	return sme;
    647 }
    648 
    649 /*
    650  * sysmon_envsys_release:
    651  *
    652  * 	+ Release a sysmon envsys device.
    653  */
    654 void
    655 sysmon_envsys_release(struct sysmon_envsys *sme)
    656 {
    657 	mutex_enter(&sme_mtx);
    658 	if (sme->sme_flags & SME_FLAG_WANTED)
    659 		cv_broadcast(&sme_cv);
    660 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    661 	mutex_exit(&sme_mtx);
    662 }
    663 
    664 /* compatibility function */
    665 struct sysmon_envsys *
    666 sysmon_envsys_find_40(u_int idx)
    667 {
    668 	struct sysmon_envsys *sme;
    669 
    670 	mutex_enter(&sme_mtx);
    671 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    672 	     sme = LIST_NEXT(sme, sme_list)) {
    673 		if (idx >= sme->sme_fsensor &&
    674 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    675 			break;
    676 	}
    677 	mutex_exit(&sme_mtx);
    678 	return sme;
    679 }
    680 
    681 /*
    682  * sme_register_sensorname:
    683  *
    684  * 	+ Register a sensor description into the list maintained per device.
    685  */
    686 static int
    687 sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
    688 {
    689 	struct sme_sensor_names *snames, *snames2 = NULL;
    690 
    691 	KASSERT(edata != NULL);
    692 
    693 	SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
    694 		/*
    695 		 * Match sensors with empty and duplicate description.
    696 		 */
    697 		if (strlen(edata->desc) == 0 ||
    698 		    strcmp(snames2->desc, edata->desc) == 0)
    699 			if (snames2->assigned) {
    700 				edata->flags |= ENVSYS_FNOTVALID;
    701 				DPRINTF(("%s: wrong sensor name='%s'\n",
    702 				    sme->sme_name, edata->desc));
    703 				return EEXIST;
    704 			}
    705 	}
    706 
    707 	snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
    708 	if (snames == NULL)
    709 		return ENOMEM;
    710 
    711 	snames->assigned = true;
    712 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
    713 	DPRINTF(("%s: registering sensor name='%s'\n",
    714 	    sme->sme_name, edata->desc));
    715 	SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
    716 	sme->sme_uniqsensors++;
    717 
    718 	return 0;
    719 }
    720 
    721 /*
    722  * sme_add_sensor_dictionary:
    723  *
    724  * 	+ Add the objects into the dictionary.
    725  */
    726 sme_event_drv_t *
    727 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    728 		    	  prop_dictionary_t dict, envsys_data_t *edata)
    729 {
    730 	const struct sme_description_table *sdt, *sdt_units;
    731 	sme_event_drv_t *sme_evdrv_t = NULL;
    732 	int i, j;
    733 
    734 	i = j = 0;
    735 
    736 	/* find the correct unit for this sensor. */
    737 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
    738 	for (i = 0; sdt_units[i].type != -1; i++)
    739 		if (sdt_units[i].type == edata->units)
    740 			break;
    741 
    742 	if (strcmp(sdt_units[i].desc, "unknown") == 0) {
    743 		DPRINTF(("%s: invalid units type for sensor=%d\n",
    744 		    __func__, edata->sensor));
    745 		goto invalidate_sensor;
    746 	}
    747 
    748 	/*
    749 	 * 		...
    750 	 * 		<key>type</key>
    751 	 * 		<string>foo</string>
    752 	 * 		<key>description</key>
    753 	 * 		<string>blah blah</string>
    754 	 * 		...
    755 	 */
    756 	if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
    757 		goto invalidate_sensor;
    758 
    759 	if (strlen(edata->desc) == 0) {
    760 		DPRINTF(("%s: invalid description for sensor=%d\n",
    761 		    __func__, edata->sensor));
    762 		goto invalidate_sensor;
    763 	}
    764 
    765 	if (sme_sensor_upstring(dict, "description", edata->desc))
    766 		goto invalidate_sensor;
    767 
    768 	/*
    769 	 * Add sensor's state description.
    770 	 *
    771 	 * 		...
    772 	 * 		<key>state</key>
    773 	 * 		<string>valid</string>
    774 	 * 		...
    775 	 */
    776 	sdt = sme_get_description_table(SME_DESC_STATES);
    777 	for (j = 0; sdt[j].type != -1; j++)
    778 		if (sdt[j].type == edata->state)
    779 			break;
    780 
    781 	if (strcmp(sdt[j].desc, "unknown") == 0) {
    782 		DPRINTF(("%s: invalid state for sensor=%d\n",
    783 		    __func__, edata->sensor));
    784 		goto invalidate_sensor;
    785 	}
    786 
    787 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
    788 	    __func__, edata->desc, edata->units, edata->state));
    789 
    790 	if (sme_sensor_upstring(dict, "state", sdt[j].desc))
    791 		goto invalidate_sensor;
    792 
    793 	/*
    794 	 * Add the monitoring boolean object:
    795 	 *
    796 	 * 		...
    797 	 * 		<key>monitoring-supported</key>
    798 	 * 		<true/>
    799 	 *		...
    800 	 *
    801 	 * always false on Battery state, Drive and Indicator types.
    802 	 * They cannot be monitored.
    803 	 *
    804 	 */
    805 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    806 	    (edata->units == ENVSYS_INDICATOR) ||
    807 	    (edata->units == ENVSYS_DRIVE) ||
    808 	    (edata->units == ENVSYS_BATTERY_STATE)) {
    809 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
    810 			goto out;
    811 	} else {
    812 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
    813 			goto out;
    814 	}
    815 
    816 	/*
    817 	 * add the percentage boolean object:
    818 	 *
    819 	 * 		...
    820 	 * 		<key>want-percentage</key>
    821 	 * 		<true/>
    822 	 * 		...
    823 	 */
    824 	if (edata->flags & ENVSYS_FPERCENT)
    825 		if (sme_sensor_upbool(dict, "want-percentage", true))
    826 			goto out;
    827 
    828 	/*
    829 	 * Add the battery-state object for battery state sensors:
    830 	 *
    831 	 * 		...
    832 	 * 		<key>battery-state</key>
    833 	 * 		<string>NORMAL</string>
    834 	 * 		...
    835 	 */
    836 	if (edata->units == ENVSYS_BATTERY_STATE) {
    837 		sdt = sme_get_description_table(SME_DESC_BATTERY_STATES);
    838 		for (j = 0; sdt[j].type != -1; j++)
    839 			if (sdt[j].type == edata->value_cur)
    840 				break;
    841 
    842 		if (sme_sensor_upstring(dict, "battery-state", sdt[j].desc))
    843 			goto out;
    844 	}
    845 
    846 	/*
    847 	 * Add the drive-state object for drive sensors:
    848 	 *
    849 	 * 		...
    850 	 * 		<key>drive-state</key>
    851 	 * 		<string>drive is online</string>
    852 	 * 		...
    853 	 */
    854 	if (edata->units == ENVSYS_DRIVE) {
    855 		sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
    856 		for (j = 0; sdt[j].type != -1; j++)
    857 			if (sdt[j].type == edata->value_cur)
    858 				break;
    859 
    860 		if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
    861 			goto out;
    862 	}
    863 
    864 	/*
    865 	 * if sensor is enabled, add the following properties...
    866 	 */
    867 	if (edata->state == ENVSYS_SVALID) {
    868 		/*
    869 		 * 	...
    870 		 * 	<key>rpms</key>
    871 		 * 	<integer>2500</integer>
    872 		 * 	<key>rfact</key>
    873 		 * 	<integer>10000</integer>
    874 		 * 	<key>cur-value</key>
    875 		 * 	<integer>1250</integer>
    876 		 * 	<key>min-value</key>
    877 		 * 	<integer>800</integer>
    878 		 * 	<key>max-value</integer>
    879 		 * 	<integer>3000</integer>
    880 		 * 	<key>avg-value</integer>
    881 		 * 	<integer>1400</integer>
    882 		 * </dict>
    883 		 */
    884 		if (edata->units == ENVSYS_SFANRPM)
    885 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
    886 				goto out;
    887 
    888 		if (edata->units == ENVSYS_SVOLTS_AC ||
    889 		    edata->units == ENVSYS_SVOLTS_DC)
    890 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
    891 				goto out;
    892 
    893 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
    894 			goto out;
    895 
    896 		if (edata->flags & ENVSYS_FVALID_MIN) {
    897 			if (sme_sensor_upint32(dict,
    898 					       "min-value",
    899 					       edata->value_min))
    900 				goto out;
    901 		}
    902 
    903 		if (edata->flags & ENVSYS_FVALID_MAX) {
    904 			if (sme_sensor_upint32(dict,
    905 					       "max-value",
    906 					       edata->value_max))
    907 				goto out;
    908 		}
    909 
    910 		if (edata->flags & ENVSYS_FVALID_AVG) {
    911 			if (sme_sensor_upint32(dict,
    912 					       "avg-value",
    913 					        edata->value_avg))
    914 				goto out;
    915 		}
    916 	}
    917 
    918 	/*
    919 	 * 	...
    920 	 * </array>
    921 	 *
    922 	 * Add the dictionary into the array.
    923 	 *
    924 	 */
    925 
    926 	if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
    927 		DPRINTF(("%s: prop_array_add\n", __func__));
    928 		goto invalidate_sensor;
    929 	}
    930 
    931 	/*
    932 	 * Add a new event if a monitoring flag was set.
    933 	 */
    934 	if (edata->monitor) {
    935 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
    936 		sme_evdrv_t->sdict = dict;
    937 		sme_evdrv_t->edata = edata;
    938 		sme_evdrv_t->sme = sme;
    939 		sme_evdrv_t->powertype = sdt_units[i].crittype;
    940 	}
    941 
    942 out:
    943 	return sme_evdrv_t;
    944 
    945 invalidate_sensor:
    946 	edata->flags |= ENVSYS_FNOTVALID;
    947 	return sme_evdrv_t;
    948 }
    949 
    950 /*
    951  * sme_update_dictionary:
    952  *
    953  * 	+ Update per-sensor dictionaries with new values if there were
    954  * 	  changes, otherwise the object in dictionary is untouched.
    955  */
    956 int
    957 sme_update_dictionary(struct sysmon_envsys *sme)
    958 {
    959 	const struct sme_description_table *sdt;
    960 	envsys_data_t *edata;
    961 	prop_object_t array, dict;
    962 	int i, j, error, invalid;
    963 
    964 	KASSERT(mutex_owned(&sme_mtx));
    965 
    966 	error = invalid = 0;
    967 	array = dict = NULL;
    968 
    969 	/* retrieve the array of dictionaries in device. */
    970 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    971 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
    972 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
    973 		return EINVAL;
    974 	}
    975 
    976 	/*
    977 	 * - iterate over all sensors.
    978 	 * - fetch new data.
    979 	 * - check if data in dictionary is different than new data.
    980 	 * - update dictionary if there were changes.
    981 	 */
    982 	DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
    983 	    sme->sme_name, sme->sme_nsensors));
    984 
    985 	for (i = 0; i < sme->sme_nsensors; i++) {
    986 		edata = &sme->sme_sensor_data[i];
    987 		/* skip invalid sensors */
    988 		if (edata->flags & ENVSYS_FNOTVALID) {
    989 			DPRINTF(("%s: invalid sensor=%s idx=%d\n",
    990 			    __func__, edata->desc, edata->sensor));
    991 			invalid++;
    992 			continue;
    993 		}
    994 
    995 		/*
    996 		 * refresh sensor data via sme_gtredata only if the
    997 		 * flag is not set.
    998 		 */
    999 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
   1000 			error = (*sme->sme_gtredata)(sme, edata);
   1001 			if (error) {
   1002 				DPRINTF(("%s: gtredata[%d] failed\n",
   1003 				    __func__, i));
   1004 				return error;
   1005 			}
   1006 		}
   1007 
   1008 		/* retrieve sensor's dictionary. */
   1009 		dict = prop_array_get(array, i - invalid);
   1010 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1011 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1012 			    __func__, edata->sensor, sme->sme_name));
   1013 			return EINVAL;
   1014 		}
   1015 
   1016 		/* update sensor's state */
   1017 		sdt = sme_get_description_table(SME_DESC_STATES);
   1018 		for (j = 0; sdt[j].type != -1; j++)
   1019 			if (sdt[j].type == edata->state)
   1020 				break;
   1021 
   1022 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1023 		    "units=%d sensor=%d\n", __func__, sdt[j].desc,
   1024 		    sdt[j].type, edata->flags, edata->units, edata->sensor));
   1025 
   1026 		/* update sensor state */
   1027 		error = sme_sensor_upstring(dict, "state", sdt[j].desc);
   1028 		if (error)
   1029 			break;
   1030 
   1031 		/* update sensor type */
   1032 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1033 		for (j = 0; sdt[j].type != -1; j++)
   1034 			if (sdt[j].type == edata->units)
   1035 				break;
   1036 
   1037 		error = sme_sensor_upstring(dict, "type", sdt[j].desc);
   1038 		if (error)
   1039 			break;
   1040 
   1041 		/* update sensor current value */
   1042 		error = sme_sensor_upint32(dict,
   1043 					   "cur-value",
   1044 					   edata->value_cur);
   1045 		if (error)
   1046 			break;
   1047 
   1048 		/*
   1049 		 * Integer and Indicator types do not the following
   1050 		 * values, so skip them.
   1051 		 */
   1052 		if (edata->units == ENVSYS_INTEGER ||
   1053 		    edata->units == ENVSYS_INDICATOR)
   1054 			continue;
   1055 
   1056 		/* update sensor flags */
   1057 		if (edata->flags & ENVSYS_FPERCENT) {
   1058 			error = sme_sensor_upbool(dict,
   1059 						  "want-percentage",
   1060 						  true);
   1061 			if (error)
   1062 				break;
   1063 		}
   1064 
   1065 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1066 			error = sme_sensor_upint32(dict,
   1067 						   "max-value",
   1068 						   edata->value_max);
   1069 			if (error)
   1070 				break;
   1071 		}
   1072 
   1073 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1074 			error = sme_sensor_upint32(dict,
   1075 						   "min-value",
   1076 						   edata->value_min);
   1077 			if (error)
   1078 				break;
   1079 		}
   1080 
   1081 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1082 			error = sme_sensor_upint32(dict,
   1083 						   "avg-value",
   1084 						   edata->value_avg);
   1085 			if (error)
   1086 				break;
   1087 		}
   1088 
   1089 		/* update 'rpms' only in ENVSYS_SFANRPM. */
   1090 		if (edata->units == ENVSYS_SFANRPM) {
   1091 			error = sme_sensor_upuint32(dict,
   1092 						    "rpms",
   1093 						    edata->rpms);
   1094 			if (error)
   1095 				break;
   1096 		}
   1097 
   1098 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
   1099 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1100 		    edata->units == ENVSYS_SVOLTS_DC) {
   1101 			error = sme_sensor_upint32(dict,
   1102 						   "rfact",
   1103 						   edata->rfact);
   1104 			if (error)
   1105 				break;
   1106 		}
   1107 
   1108 		/* update 'drive-state' only in ENVSYS_DRIVE. */
   1109 		if (edata->units == ENVSYS_DRIVE) {
   1110 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1111 			for (j = 0; sdt[j].type != -1; j++)
   1112 				if (sdt[j].type == edata->value_cur)
   1113 					break;
   1114 
   1115 			error = sme_sensor_upstring(dict,
   1116 						    "drive-state",
   1117 						    sdt[j].desc);
   1118 			if (error)
   1119 				break;
   1120 		}
   1121 
   1122 		/* update 'battery-state' only in ENVSYS_BATTERY_STATE. */
   1123 		if (edata->units == ENVSYS_BATTERY_STATE) {
   1124 			sdt =
   1125 			    sme_get_description_table(SME_DESC_BATTERY_STATES);
   1126 			for (j = 0; sdt[j].type != -1; j++)
   1127 				if (sdt[j].type == edata->value_cur)
   1128 					break;
   1129 
   1130 			error = sme_sensor_upstring(dict,
   1131 						    "battery-state",
   1132 						    sdt[j].desc);
   1133 			if (error)
   1134 				break;
   1135 		}
   1136 	}
   1137 
   1138 	return error;
   1139 }
   1140 
   1141 /*
   1142  * sme_userset_dictionary:
   1143  *
   1144  * 	+ Parse the userland dictionary and run the appropiate
   1145  * 	  task that was requested.
   1146  */
   1147 int
   1148 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1149 		       prop_array_t array)
   1150 {
   1151 	const struct sme_description_table *sdt;
   1152 	envsys_data_t *edata, *nedata;
   1153 	prop_dictionary_t dict;
   1154 	prop_object_t obj, obj1, obj2;
   1155 	int32_t critval;
   1156 	int i, invalid, error;
   1157 	const char *blah, *sname;
   1158 	bool targetfound = false;
   1159 
   1160 	error = invalid = 0;
   1161 	blah = sname = NULL;
   1162 
   1163 	/* get sensor's name from userland dictionary. */
   1164 	obj = prop_dictionary_get(udict, "sensor-name");
   1165 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1166 		DPRINTF(("%s: sensor-name failed\n", __func__));
   1167 		return EINVAL;
   1168 	}
   1169 
   1170 	/* iterate over the sensors to find the right one */
   1171 	for (i = 0; i < sme->sme_nsensors; i++) {
   1172 		edata = &sme->sme_sensor_data[i];
   1173 		/* skip sensors with duplicate description */
   1174 		if (edata->flags & ENVSYS_FNOTVALID) {
   1175 			invalid++;
   1176 			continue;
   1177 		}
   1178 
   1179 		dict = prop_array_get(array, i - invalid);
   1180 		obj1 = prop_dictionary_get(dict, "description");
   1181 
   1182 		/* is it our sensor? */
   1183 		if (!prop_string_equals(obj1, obj))
   1184 			continue;
   1185 
   1186 		/*
   1187 		 * Check if a new description operation was
   1188 		 * requested by the user and set new description.
   1189 		 */
   1190 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
   1191 			targetfound = true;
   1192 			blah = prop_string_cstring_nocopy(obj2);
   1193 
   1194 			for (i = 0; i < sme->sme_nsensors; i++) {
   1195 				if (i == edata->sensor)
   1196 					continue;
   1197 
   1198 				nedata = &sme->sme_sensor_data[i];
   1199 				if (strcmp(blah, nedata->desc) == 0) {
   1200 					error = EEXIST;
   1201 					break;
   1202 				}
   1203 			}
   1204 
   1205 			if (error)
   1206 				break;
   1207 
   1208 			error = sme_sensor_upstring(dict,
   1209 						    "description",
   1210 						    blah);
   1211 			if (!error)
   1212 				(void)strlcpy(edata->desc,
   1213 					      blah,
   1214 					      sizeof(edata->desc));
   1215 
   1216 			break;
   1217 		}
   1218 
   1219 		/* did the user want to remove a critical capacity limit? */
   1220 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
   1221 		if (obj2 != NULL) {
   1222 			targetfound = true;
   1223 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1224 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1225 				    error = ENOTSUP;
   1226 				    break;
   1227 			}
   1228 
   1229 			sname = prop_string_cstring_nocopy(obj);
   1230 			error = sme_event_unregister(sname,
   1231 			    PENVSYS_EVENT_BATT_USERCAP);
   1232 			if (error)
   1233 				break;
   1234 
   1235 			prop_dictionary_remove(dict, "critical-capacity");
   1236 			break;
   1237 		}
   1238 
   1239 		/* did the user want to remove a critical min limit? */
   1240 		obj2 = prop_dictionary_get(udict, "remove-cmin-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_CRITMIN);
   1246 			if (error)
   1247 				break;
   1248 
   1249 			prop_dictionary_remove(dict, "critical-min-limit");
   1250 			break;
   1251 		}
   1252 
   1253 		/* did the user want to remove a critical max limit? */
   1254 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1255 		if (obj2 != NULL) {
   1256 			targetfound = true;
   1257 			sname = prop_string_cstring_nocopy(obj);
   1258 			error = sme_event_unregister(sname,
   1259 			    PENVSYS_EVENT_USER_CRITMAX);
   1260 			if (error)
   1261 				break;
   1262 
   1263 			prop_dictionary_remove(dict, "critical-max-limit");
   1264 			break;
   1265 		}
   1266 
   1267 		/* did the user want to change rfact? */
   1268 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1269 		if (obj2 != NULL) {
   1270 			targetfound = true;
   1271 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1272 				edata->rfact = prop_number_integer_value(obj2);
   1273 			else
   1274 				error = ENOTSUP;
   1275 
   1276 			break;
   1277 		}
   1278 
   1279 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1280 		for (i = 0; sdt[i].type != -1; i++)
   1281 			if (sdt[i].type == edata->units)
   1282 				break;
   1283 
   1284 		/* did the user want to set a critical capacity event? */
   1285 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1286 		if (obj2 != NULL) {
   1287 			targetfound = true;
   1288 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1289 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1290 				error = ENOTSUP;
   1291 				break;
   1292 			}
   1293 
   1294 			critval = prop_number_integer_value(obj2);
   1295 			error = sme_event_register(dict,
   1296 					      edata,
   1297 					      sme->sme_name,
   1298 					      "critical-capacity",
   1299 					      critval,
   1300 					      PENVSYS_EVENT_BATT_USERCAP,
   1301 					      sdt[i].crittype);
   1302 			break;
   1303 		}
   1304 
   1305 		/* did the user want to set a critical max event? */
   1306 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1307 		if (obj2 != NULL) {
   1308 			targetfound = true;
   1309 			if (edata->units == ENVSYS_INDICATOR ||
   1310 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1311 				error = ENOTSUP;
   1312 				break;
   1313 			}
   1314 
   1315 			critval = prop_number_integer_value(obj2);
   1316 			error = sme_event_register(dict,
   1317 					      edata,
   1318 					      sme->sme_name,
   1319 					      "critical-max-limit",
   1320 					      critval,
   1321 					      PENVSYS_EVENT_USER_CRITMAX,
   1322 					      sdt[i].crittype);
   1323 			break;
   1324 		}
   1325 
   1326 		/* did the user want to set a critical min event? */
   1327 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1328 		if (obj2 != NULL) {
   1329 			targetfound = true;
   1330 			if (edata->units == ENVSYS_INDICATOR ||
   1331 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1332 				error = ENOTSUP;
   1333 				break;
   1334 			}
   1335 
   1336 			critval = prop_number_integer_value(obj2);
   1337 			error = sme_event_register(dict,
   1338 					      edata,
   1339 					      sme->sme_name,
   1340 					      "critical-min-limit",
   1341 					      critval,
   1342 					      PENVSYS_EVENT_USER_CRITMIN,
   1343 					      sdt[i].crittype);
   1344 			break;
   1345 		}
   1346 	}
   1347 
   1348 	/* invalid target? return the error */
   1349 	if (!targetfound)
   1350 		error = EINVAL;
   1351 
   1352 	return error;
   1353 }
   1354