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