Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.81
      1 /*	$NetBSD: sysmon_envsys.c,v 1.81 2008/03/23 16:09:41 xtraeme Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007, 2008 Juan Romero Pardines.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*-
     29  * Copyright (c) 2000 Zembu Labs, Inc.
     30  * All rights reserved.
     31  *
     32  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
     33  *
     34  * Redistribution and use in source and binary forms, with or without
     35  * modification, are permitted provided that the following conditions
     36  * are met:
     37  * 1. Redistributions of source code must retain the above copyright
     38  *    notice, this list of conditions and the following disclaimer.
     39  * 2. Redistributions in binary form must reproduce the above copyright
     40  *    notice, this list of conditions and the following disclaimer in the
     41  *    documentation and/or other materials provided with the distribution.
     42  * 3. All advertising materials mentioning features or use of this software
     43  *    must display the following acknowledgement:
     44  *	This product includes software developed by Zembu Labs, Inc.
     45  * 4. Neither the name of Zembu Labs nor the names of its employees may
     46  *    be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     50  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     51  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     52  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59  */
     60 
     61 /*
     62  * Environmental sensor framework for sysmon, exported to userland
     63  * with proplib(3).
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.81 2008/03/23 16:09:41 xtraeme Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/types.h>
     71 #include <sys/conf.h>
     72 #include <sys/errno.h>
     73 #include <sys/fcntl.h>
     74 #include <sys/kernel.h>
     75 #include <sys/systm.h>
     76 #include <sys/proc.h>
     77 #include <sys/mutex.h>
     78 #include <sys/kmem.h>
     79 
     80 /* #define ENVSYS_DEBUG */
     81 #include <dev/sysmon/sysmonvar.h>
     82 #include <dev/sysmon/sysmon_envsysvar.h>
     83 #include <dev/sysmon/sysmon_taskq.h>
     84 
     85 /*
     86  * Notes about locking:
     87  *
     88  * The 'sme_mtx' lock is used to protect access to the sysmon_envsys
     89  * objects (devices, sensors, events) and the global counter
     90  * 'sysmon_envsys_next_sensor_index'. The 'sme_cv' condition variable
     91  * is used to wait for completion paths on these objects.
     92  *
     93  * The 'sme_events_mtx' lock is used to protect initialization and
     94  * finalization of the per device events framework (the callout(9) and
     95  * workqueue(9) that is used to check for conditions and sending events
     96  * to the powerd(8) daemon (if running)).
     97  *
     98  * The callouts are protected by the 'sme_callout_mtx'.
     99  */
    100 
    101 kmutex_t sme_mtx, sme_events_mtx, sme_callout_mtx;
    102 kcondvar_t sme_cv;
    103 
    104 /*
    105  * Types of properties that can be set via userland.
    106  */
    107 enum {
    108 	USERPROP_DESC 		= 0x0001,
    109 	USERPROP_BATTCAP	= 0x0002,
    110 	USERPROP_CRITMAX	= 0x0004,
    111 	USERPROP_CRITMIN	= 0x0008,
    112 	USERPROP_RFACT		= 0x0010
    113 };
    114 
    115 static prop_dictionary_t sme_propd;
    116 static uint32_t sysmon_envsys_next_sensor_index = 0;
    117 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    118 
    119 static void sysmon_envsys_destroy_plist(prop_array_t);
    120 static void sme_remove_userprops(void);
    121 static int sme_add_property_dictionary(struct sysmon_envsys *, prop_array_t,
    122 				       prop_dictionary_t);
    123 static void sme_initial_refresh(void *);
    124 
    125 /*
    126  * sysmon_envsys_init:
    127  *
    128  * 	+ Initialize global mutexes, dictionary and the linked lists.
    129  */
    130 void
    131 sysmon_envsys_init(void)
    132 {
    133 	LIST_INIT(&sysmon_envsys_list);
    134 	mutex_init(&sme_mtx, MUTEX_DEFAULT, IPL_NONE);
    135 	mutex_init(&sme_events_mtx, MUTEX_DEFAULT, IPL_NONE);
    136 	mutex_init(&sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    137 	cv_init(&sme_cv, "smeworker");
    138 	sme_propd = prop_dictionary_create();
    139 }
    140 
    141 /*
    142  * sysmonopen_envsys:
    143  *
    144  *	+ Open the system monitor device.
    145  */
    146 int
    147 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    148 {
    149 	return 0;
    150 }
    151 
    152 /*
    153  * sysmonclose_envsys:
    154  *
    155  *	+ Close the system monitor device.
    156  */
    157 int
    158 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    159 {
    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 	/*
    177 	 * To update the global dictionary with latest data from devices.
    178 	 */
    179 	case ENVSYS_GETDICTIONARY:
    180 	    {
    181 		struct plistref *plist = (struct plistref *)data;
    182 
    183 		/*
    184 		 * Update dictionaries on all sysmon envsys devices
    185 		 * registered.
    186 		 */
    187 		mutex_enter(&sme_mtx);
    188 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    189 			sysmon_envsys_acquire(sme);
    190 			error = sme_update_dictionary(sme);
    191 			if (error) {
    192 				DPRINTF(("%s: sme_update_dictionary, "
    193 				    "error=%d\n", __func__, error));
    194 				sysmon_envsys_release(sme);
    195 				mutex_exit(&sme_mtx);
    196 				return error;
    197 			}
    198 			sysmon_envsys_release(sme);
    199 		}
    200 		mutex_exit(&sme_mtx);
    201 		/*
    202 		 * Copy global dictionary to userland.
    203 		 */
    204 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    205 		break;
    206 	    }
    207 	/*
    208 	 * To set properties on multiple devices.
    209 	 */
    210 	case ENVSYS_SETDICTIONARY:
    211 	    {
    212 		const struct plistref *plist = (const struct plistref *)data;
    213 		prop_dictionary_t udict;
    214 		prop_object_iterator_t iter, iter2;
    215 		prop_object_t obj, obj2;
    216 		prop_array_t array_u, array_k;
    217 		const char *devname = NULL;
    218 
    219 		if ((flag & FWRITE) == 0)
    220 			return EPERM;
    221 
    222 		/*
    223 		 * Get dictionary from userland.
    224 		 */
    225 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    226 		if (error) {
    227 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    228 			    __func__, error));
    229 			break;
    230 		}
    231 
    232 		iter = prop_dictionary_iterator(udict);
    233 		if (!iter) {
    234 			prop_object_release(udict);
    235 			return ENOMEM;
    236 		}
    237 
    238 		/*
    239 		 * Iterate over the userland dictionary and process
    240 		 * the list of devices.
    241 		 */
    242 		while ((obj = prop_object_iterator_next(iter))) {
    243 			array_u = prop_dictionary_get_keysym(udict, obj);
    244 			if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
    245 				prop_object_iterator_release(iter);
    246 				prop_object_release(udict);
    247 				return EINVAL;
    248 			}
    249 
    250 			devname = prop_dictionary_keysym_cstring_nocopy(obj);
    251 			DPRINTF(("%s: processing the '%s' array requests\n",
    252 			    __func__, devname));
    253 
    254 			/*
    255 			 * find the correct sme device.
    256 			 */
    257 			mutex_enter(&sme_mtx);
    258 			sme = sysmon_envsys_find(devname);
    259 			if (!sme) {
    260 				mutex_exit(&sme_mtx);
    261 				DPRINTF(("%s: NULL sme\n", __func__));
    262 				prop_object_iterator_release(iter);
    263 				prop_object_release(udict);
    264 				return EINVAL;
    265 			}
    266 
    267 			/*
    268 			 * Find the correct array object with the string
    269 			 * supplied by the userland dictionary.
    270 			 */
    271 			array_k = prop_dictionary_get(sme_propd, devname);
    272 			if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
    273 				DPRINTF(("%s: array device failed\n",
    274 				    __func__));
    275 				sysmon_envsys_release(sme);
    276 				mutex_exit(&sme_mtx);
    277 				prop_object_iterator_release(iter);
    278 				prop_object_release(udict);
    279 				return EINVAL;
    280 			}
    281 
    282 			iter2 = prop_array_iterator(array_u);
    283 			if (!iter2) {
    284 				sysmon_envsys_release(sme);
    285 				mutex_exit(&sme_mtx);
    286 				prop_object_iterator_release(iter);
    287 				prop_object_release(udict);
    288 				return ENOMEM;
    289 			}
    290 
    291 			/*
    292 			 * Iterate over the array of dictionaries to
    293 			 * process the list of sensors and properties.
    294 			 */
    295 			while ((obj2 = prop_object_iterator_next(iter2))) {
    296 				/*
    297 				 * do the real work now.
    298 				 */
    299 				error = sme_userset_dictionary(sme,
    300 							       obj2,
    301 							       array_k);
    302 				if (error) {
    303 					sysmon_envsys_release(sme);
    304 					mutex_exit(&sme_mtx);
    305 					prop_object_iterator_release(iter2);
    306 					prop_object_iterator_release(iter);
    307 					prop_object_release(udict);
    308 					return error;
    309 				}
    310 			}
    311 
    312 			sysmon_envsys_release(sme);
    313 			mutex_exit(&sme_mtx);
    314 			prop_object_iterator_release(iter2);
    315 		}
    316 
    317 		prop_object_iterator_release(iter);
    318 		prop_object_release(udict);
    319 		break;
    320 	    }
    321 	/*
    322 	 * To remove all properties from all devices registered.
    323 	 */
    324 	case ENVSYS_REMOVEPROPS:
    325 	    {
    326 		const struct plistref *plist = (const struct plistref *)data;
    327 		prop_dictionary_t udict;
    328 		prop_object_t obj;
    329 
    330 		if ((flag & FWRITE) == 0)
    331 			return EPERM;
    332 
    333 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    334 		if (error) {
    335 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    336 			    __func__, error));
    337 			break;
    338 		}
    339 
    340 		obj = prop_dictionary_get(udict, "envsys-remove-props");
    341 		if (!obj || !prop_bool_true(obj)) {
    342 			DPRINTF(("%s: invalid 'envsys-remove-props'\n",
    343 			     __func__));
    344 			return EINVAL;
    345 		}
    346 
    347 		prop_object_release(udict);
    348 		sme_remove_userprops();
    349 
    350 		break;
    351 	    }
    352 	/*
    353 	 * Compatibility ioctls with the old interface, only implemented
    354 	 * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old
    355 	 * applications work.
    356 	 */
    357 	case ENVSYS_GTREDATA:
    358 	    {
    359 		struct envsys_tre_data *tred = (void *)data;
    360 		envsys_data_t *edata = NULL;
    361 		bool found = false;
    362 
    363 		tred->validflags = 0;
    364 
    365 		mutex_enter(&sme_mtx);
    366 		sme = sysmon_envsys_find_40(tred->sensor);
    367 		if (!sme) {
    368 			mutex_exit(&sme_mtx);
    369 			break;
    370 		}
    371 
    372 		oidx = tred->sensor;
    373 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    374 
    375 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    376 		    __func__, tred->sensor, oidx, sme->sme_name,
    377 		    sme->sme_nsensors));
    378 
    379 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    380 			if (edata->sensor == tred->sensor) {
    381 				found = true;
    382 				break;
    383 			}
    384 		}
    385 
    386 		if (!found) {
    387 			sysmon_envsys_release(sme);
    388 			mutex_exit(&sme_mtx);
    389 			error = ENODEV;
    390 			break;
    391 		}
    392 
    393 		if (tred->sensor < sme->sme_nsensors) {
    394 			if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    395 				(*sme->sme_refresh)(sme, edata);
    396 
    397 			/*
    398 			 * copy required values to the old interface.
    399 			 */
    400 			tred->sensor = edata->sensor;
    401 			tred->cur.data_us = edata->value_cur;
    402 			tred->cur.data_s = edata->value_cur;
    403 			tred->max.data_us = edata->value_max;
    404 			tred->max.data_s = edata->value_max;
    405 			tred->min.data_us = edata->value_min;
    406 			tred->min.data_s = edata->value_min;
    407 			tred->avg.data_us = edata->value_avg;
    408 			tred->avg.data_s = edata->value_avg;
    409 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    410 				tred->units = ENVSYS_INDICATOR;
    411 			else
    412 				tred->units = edata->units;
    413 
    414 			tred->validflags |= ENVSYS_FVALID;
    415 			tred->validflags |= ENVSYS_FCURVALID;
    416 
    417 			if (edata->flags & ENVSYS_FPERCENT) {
    418 				tred->validflags |= ENVSYS_FMAXVALID;
    419 				tred->validflags |= ENVSYS_FFRACVALID;
    420 			}
    421 
    422 			if (edata->state == ENVSYS_SINVALID) {
    423 				tred->validflags &= ~ENVSYS_FCURVALID;
    424 				tred->cur.data_us = tred->cur.data_s = 0;
    425 			}
    426 
    427 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    428 			    __func__, edata->desc, tred->cur.data_s));
    429 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    430 			    " tred->sensor=%d\n", __func__, tred->validflags,
    431 			    tred->units, tred->sensor));
    432 		}
    433 		tred->sensor = oidx;
    434 		sysmon_envsys_release(sme);
    435 		mutex_exit(&sme_mtx);
    436 
    437 		break;
    438 	    }
    439 	case ENVSYS_GTREINFO:
    440 	    {
    441 		struct envsys_basic_info *binfo = (void *)data;
    442 		envsys_data_t *edata = NULL;
    443 		bool found = false;
    444 
    445 		binfo->validflags = 0;
    446 
    447 		mutex_enter(&sme_mtx);
    448 		sme = sysmon_envsys_find_40(binfo->sensor);
    449 		if (!sme) {
    450 			mutex_exit(&sme_mtx);
    451 			break;
    452 		}
    453 
    454 		oidx = binfo->sensor;
    455 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    456 
    457 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    458 			if (edata->sensor == binfo->sensor) {
    459 				found = true;
    460 				break;
    461 			}
    462 		}
    463 
    464 		if (!found) {
    465 			sysmon_envsys_release(sme);
    466 			mutex_exit(&sme_mtx);
    467 			error = ENODEV;
    468 			break;
    469 		}
    470 
    471 		binfo->validflags |= ENVSYS_FVALID;
    472 
    473 		if (binfo->sensor < sme->sme_nsensors) {
    474 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    475 				binfo->units = ENVSYS_INDICATOR;
    476 			else
    477 				binfo->units = edata->units;
    478 
    479 			/*
    480 			 * previously, the ACPI sensor names included the
    481 			 * device name. Include that in compatibility code.
    482 			 */
    483 			if (strncmp(sme->sme_name, "acpi", 4) == 0)
    484 				(void)snprintf(binfo->desc, sizeof(binfo->desc),
    485 				    "%s %s", sme->sme_name, edata->desc);
    486 			else
    487 				(void)strlcpy(binfo->desc, edata->desc,
    488 				    sizeof(binfo->desc));
    489 		}
    490 
    491 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    492 		    __func__, binfo->units, binfo->validflags));
    493 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    494 		    __func__, binfo->desc, binfo->sensor));
    495 
    496 		binfo->sensor = oidx;
    497 		sysmon_envsys_release(sme);
    498 		mutex_exit(&sme_mtx);
    499 
    500 		break;
    501 	    }
    502 	default:
    503 		error = ENOTTY;
    504 		break;
    505 	}
    506 
    507 	return error;
    508 }
    509 
    510 /*
    511  * sysmon_envsys_create:
    512  *
    513  * 	+ Allocates a new sysmon_envsys object and initializes the
    514  * 	  stuff for sensors and events.
    515  */
    516 struct sysmon_envsys *
    517 sysmon_envsys_create(void)
    518 {
    519 	struct sysmon_envsys *sme;
    520 
    521 	sme = kmem_zalloc(sizeof(*sme), KM_SLEEP);
    522 	TAILQ_INIT(&sme->sme_sensors_list);
    523 	LIST_INIT(&sme->sme_events_list);
    524 	callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
    525 
    526 	return sme;
    527 }
    528 
    529 /*
    530  * sysmon_envsys_destroy:
    531  *
    532  * 	+ Removes all sensors from the tail queue, destroys the callout
    533  * 	  and frees the sysmon_envsys object.
    534  */
    535 void
    536 sysmon_envsys_destroy(struct sysmon_envsys *sme)
    537 {
    538 	envsys_data_t *edata;
    539 
    540 	KASSERT(sme != NULL);
    541 
    542 	while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    543 		edata = TAILQ_FIRST(&sme->sme_sensors_list);
    544 		TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    545 	}
    546 	callout_destroy(&sme->sme_callout);
    547 	kmem_free(sme, sizeof(*sme));
    548 }
    549 
    550 /*
    551  * sysmon_envsys_sensor_attach:
    552  *
    553  * 	+ Attachs a sensor into a sysmon_envsys device checking that units
    554  * 	  is set to a valid type and description is unique and not empty.
    555  */
    556 int
    557 sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata)
    558 {
    559 	const struct sme_description_table *sdt_units;
    560 	envsys_data_t *oedata;
    561 	int i;
    562 
    563 	KASSERT(sme != NULL || edata != NULL);
    564 
    565 	/*
    566 	 * Find the correct units for this sensor.
    567 	 */
    568 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
    569 	for (i = 0; sdt_units[i].type != -1; i++)
    570 		if (sdt_units[i].type == edata->units)
    571 			break;
    572 
    573 	if (strcmp(sdt_units[i].desc, "unknown") == 0)
    574 		return EINVAL;
    575 
    576 	/*
    577 	 * Check that description is not empty or duplicate.
    578 	 */
    579 	if (strlen(edata->desc) == 0)
    580 		return EINVAL;
    581 
    582 	mutex_enter(&sme_mtx);
    583 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    584 		if (strcmp(oedata->desc, edata->desc) == 0) {
    585 			mutex_exit(&sme_mtx);
    586 			return EEXIST;
    587 		}
    588 	}
    589 	/*
    590 	 * Ok, the sensor has been added into the device queue.
    591 	 */
    592 	TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head);
    593 
    594 	/*
    595 	 * Give the sensor a index position.
    596 	 */
    597 	edata->sensor = sme->sme_nsensors;
    598 	sme->sme_nsensors++;
    599 	mutex_exit(&sme_mtx);
    600 
    601 	return 0;
    602 }
    603 
    604 /*
    605  * sysmon_envsys_sensor_detach:
    606  *
    607  * 	+ Detachs a sensor from a sysmon_envsys device and decrements the
    608  * 	  sensors count on success.
    609  */
    610 int
    611 sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata)
    612 {
    613 	envsys_data_t *oedata;
    614 	bool found = false;
    615 
    616 	KASSERT(sme != NULL || edata != NULL);
    617 
    618 	/*
    619 	 * Check the sensor is already on the list.
    620 	 */
    621 	mutex_enter(&sme_mtx);
    622 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    623 		if (oedata->sensor == edata->sensor) {
    624 			found = true;
    625 			break;
    626 		}
    627 	}
    628 
    629 	if (!found) {
    630 		mutex_exit(&sme_mtx);
    631 		return EINVAL;
    632 	}
    633 
    634 	/*
    635 	 * remove it and decrement the sensors count.
    636 	 */
    637 	TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    638 	sme->sme_nsensors--;
    639 	mutex_exit(&sme_mtx);
    640 
    641 	return 0;
    642 }
    643 
    644 
    645 /*
    646  * sysmon_envsys_register:
    647  *
    648  *	+ Register a sysmon envsys device.
    649  *	+ Create array of dictionaries for a device.
    650  */
    651 int
    652 sysmon_envsys_register(struct sysmon_envsys *sme)
    653 {
    654 	struct sme_evdrv {
    655 		SLIST_ENTRY(sme_evdrv) evdrv_head;
    656 		sme_event_drv_t *evdrv;
    657 	};
    658 	SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
    659 	struct sme_evdrv *sme_evdrv = NULL;
    660 	struct sysmon_envsys *lsme;
    661 	prop_dictionary_t dict, dict2;
    662 	prop_array_t array;
    663 	envsys_data_t *edata = NULL;
    664 	int error = 0;
    665 
    666 	KASSERT(sme != NULL);
    667 	KASSERT(sme->sme_name != NULL);
    668 
    669 	/*
    670 	 * sanity check: if SME_DISABLE_REFRESH is not set,
    671 	 * the sme_refresh function callback must be non NULL.
    672 	 */
    673 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    674 		if (!sme->sme_refresh)
    675 			return EINVAL;
    676 
    677 	/*
    678 	 * If the list of sensors is empty, there's no point to continue...
    679 	 */
    680 	if (TAILQ_EMPTY(&sme->sme_sensors_list)) {
    681 		DPRINTF(("%s: sensors list empty for %s\n", __func__,
    682 		    sme->sme_name));
    683 		return ENOTSUP;
    684 	}
    685 
    686 	/*
    687 	 * create the device array.
    688 	 */
    689 	array = prop_array_create();
    690 	if (!array)
    691 		return ENOMEM;
    692 
    693 	/*
    694 	 * Initialize the singly linked list for driver events.
    695 	 */
    696 	SLIST_INIT(&sme_evdrv_list);
    697 
    698 	/*
    699 	 * Iterate over all sensors and create a dictionary per sensor.
    700 	 * We must respect the order in which the sensors were added.
    701 	 */
    702 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    703 		dict = prop_dictionary_create();
    704 		if (!dict) {
    705 			error = ENOMEM;
    706 			goto out2;
    707 		}
    708 
    709 		/*
    710 		 * Create all objects in sensor's dictionary.
    711 		 */
    712 		sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
    713 		sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
    714 					array, dict, edata);
    715 		if (sme_evdrv->evdrv)
    716 			SLIST_INSERT_HEAD(&sme_evdrv_list,
    717 					  sme_evdrv, evdrv_head);
    718 	}
    719 
    720 	/*
    721 	 * Check if requested sysmon_envsys device is valid
    722 	 * and does not exist already in the list.
    723 	 */
    724 	mutex_enter(&sme_mtx);
    725 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    726 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    727 		       error = EEXIST;
    728 		       goto out;
    729 	       }
    730 	}
    731 
    732 	/*
    733 	 * If the array does not contain any object (sensor), there's
    734 	 * no need to attach the driver.
    735 	 */
    736 	if (prop_array_count(array) == 0) {
    737 		error = EINVAL;
    738 		DPRINTF(("%s: empty array for '%s'\n", __func__,
    739 		    sme->sme_name));
    740 		goto out;
    741 	}
    742 
    743 	/*
    744 	 * Add the dictionary for the global properties of this device.
    745 	 */
    746 	dict2 = prop_dictionary_create();
    747 	if (!dict2) {
    748 		error = ENOMEM;
    749 		goto out;
    750 	}
    751 
    752 	error = sme_add_property_dictionary(sme, array, dict2);
    753 	if (error) {
    754 		prop_object_release(dict2);
    755 		goto out;
    756 	}
    757 
    758 	/*
    759 	 * Add the array into the global dictionary for the driver.
    760 	 *
    761 	 * <dict>
    762 	 * 	<key>foo0</key>
    763 	 * 	<array>
    764 	 * 		...
    765 	 */
    766 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    767 		error = EINVAL;
    768 		DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
    769 		    sme->sme_name));
    770 		goto out;
    771 	}
    772 	/*
    773 	 * Add the device into the list.
    774 	 */
    775 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    776 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    777 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    778 out:
    779 	mutex_exit(&sme_mtx);
    780 
    781 	/*
    782 	 * No errors? register the events that were set in the driver
    783 	 * and make an initial data refresh if was requested.
    784 	 */
    785 	if (error == 0) {
    786 		sysmon_task_queue_init();
    787 		SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
    788 			sysmon_task_queue_sched(0,
    789 			    sme_event_drvadd, sme_evdrv->evdrv);
    790 		}
    791 		DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
    792 		    __func__, sme->sme_name, sme->sme_nsensors));
    793 
    794 		if (sme->sme_flags & SME_INIT_REFRESH)
    795 			sysmon_task_queue_sched(0, sme_initial_refresh, sme);
    796 	}
    797 
    798 out2:
    799 	while (!SLIST_EMPTY(&sme_evdrv_list)) {
    800 		sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
    801 		SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
    802 		kmem_free(sme_evdrv, sizeof(*sme_evdrv));
    803 	}
    804 	if (!error)
    805 		return 0;
    806 
    807 	/*
    808 	 * Ugh... something wasn't right; unregister all events and sensors
    809 	 * previously assigned and destroy the array with all its objects.
    810 	 */
    811 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    812 	    sme->sme_name, error));
    813 	if (error != EEXIST) {
    814 		mutex_enter(&sme_mtx);
    815 		sme_event_unregister_all(sme);
    816 		while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    817 			edata = TAILQ_FIRST(&sme->sme_sensors_list);
    818 			TAILQ_REMOVE(&sme->sme_sensors_list, edata,
    819 			    sensors_head);
    820 		}
    821 		mutex_exit(&sme_mtx);
    822 	}
    823 	sysmon_envsys_destroy_plist(array);
    824 	return error;
    825 }
    826 
    827 /*
    828  * sysmon_envsys_destroy_plist:
    829  *
    830  * 	+ Remove all objects from the array of dictionaries that is
    831  * 	  created in a sysmon envsys device.
    832  */
    833 static void
    834 sysmon_envsys_destroy_plist(prop_array_t array)
    835 {
    836 	prop_object_iterator_t iter, iter2;
    837 	prop_dictionary_t dict;
    838 	prop_object_t obj;
    839 
    840 	KASSERT(array != NULL);
    841 
    842 	DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
    843 	    prop_array_count(array)));
    844 
    845 	iter = prop_array_iterator(array);
    846 	if (!iter)
    847 		return;
    848 
    849 	while ((dict = prop_object_iterator_next(iter))) {
    850 		KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
    851 		iter2 = prop_dictionary_iterator(dict);
    852 		if (!iter2)
    853 			goto out;
    854 		DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
    855 		while ((obj = prop_object_iterator_next(iter2)) != NULL) {
    856 			DPRINTFOBJ(("%s: obj=%s\n", __func__,
    857 			    prop_dictionary_keysym_cstring_nocopy(obj)));
    858 			prop_dictionary_remove(dict,
    859 			    prop_dictionary_keysym_cstring_nocopy(obj));
    860 			prop_object_iterator_reset(iter2);
    861 		}
    862 		prop_object_iterator_release(iter2);
    863 		DPRINTFOBJ(("%s: objects in dictionary:%d\n",
    864 		    __func__, prop_dictionary_count(dict)));
    865 		prop_object_release(dict);
    866 	}
    867 
    868 out:
    869 	prop_object_iterator_release(iter);
    870 	prop_object_release(array);
    871 }
    872 
    873 /*
    874  * sysmon_envsys_unregister:
    875  *
    876  *	+ Unregister a sysmon envsys device.
    877  */
    878 void
    879 sysmon_envsys_unregister(struct sysmon_envsys *sme)
    880 {
    881 	prop_array_t array;
    882 
    883 	KASSERT(sme != NULL);
    884 
    885 	mutex_enter(&sme_mtx);
    886 	/*
    887 	 * Wait for device to be available.
    888 	 */
    889 	while (sme->sme_flags & SME_FLAG_BUSY)
    890 		cv_wait(&sme_cv, &sme_mtx);
    891 	/*
    892 	 * Stop the callout.
    893 	 */
    894 	callout_stop(&sme->sme_callout);
    895 	/*
    896 	 * Decrement global sensors counter (only useful for compatibility).
    897 	 */
    898 	sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
    899 	/*
    900 	 * Unregister all events associated with this device.
    901 	 */
    902 	sme_event_unregister_all(sme);
    903 	LIST_REMOVE(sme, sme_list);
    904 	mutex_exit(&sme_mtx);
    905 	/*
    906 	 * Remove the device (and all its objects) from the global dictionary.
    907 	 */
    908 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    909 	if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
    910 		prop_dictionary_remove(sme_propd, sme->sme_name);
    911 		sysmon_envsys_destroy_plist(array);
    912 	}
    913 	/*
    914 	 * And finally destroy the sysmon_envsys object.
    915 	 */
    916 	sysmon_envsys_destroy(sme);
    917 }
    918 
    919 /*
    920  * sysmon_envsys_find:
    921  *
    922  *	+ Find a sysmon envsys device and mark it as busy if found.
    923  */
    924 struct sysmon_envsys *
    925 sysmon_envsys_find(const char *name)
    926 {
    927 	struct sysmon_envsys *sme;
    928 
    929 	KASSERT(mutex_owned(&sme_mtx));
    930 
    931 again:
    932 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    933 		if (strcmp(sme->sme_name, name) == 0) {
    934 			if (sme->sme_flags & SME_FLAG_BUSY) {
    935 				cv_wait(&sme_cv, &sme_mtx);
    936 				goto again;
    937 			}
    938 			sme->sme_flags |= SME_FLAG_BUSY;
    939 			break;
    940 		}
    941 	}
    942 	return sme;
    943 }
    944 
    945 /*
    946  * sysmon_envsys_acquire:
    947  *
    948  * 	+ Acquire priviledge to a sysmon envsys device (locked).
    949  */
    950 void
    951 sysmon_envsys_acquire(struct sysmon_envsys *sme)
    952 {
    953 	KASSERT(mutex_owned(&sme_mtx));
    954 
    955 	while (sme->sme_flags & SME_FLAG_BUSY)
    956 		cv_wait(&sme_cv, &sme_mtx);
    957 
    958 	sme->sme_flags |= SME_FLAG_BUSY;
    959 }
    960 
    961 /*
    962  * sysmon_envsys_release:
    963  *
    964  * 	+ Release a sysmon envsys device (locked).
    965  */
    966 void
    967 sysmon_envsys_release(struct sysmon_envsys *sme)
    968 {
    969 	KASSERT(mutex_owned(&sme_mtx));
    970 
    971 	sme->sme_flags &= ~SME_FLAG_BUSY;
    972 	cv_broadcast(&sme_cv);
    973 }
    974 
    975 /* compatibility function */
    976 struct sysmon_envsys *
    977 sysmon_envsys_find_40(u_int idx)
    978 {
    979 	struct sysmon_envsys *sme;
    980 
    981 	KASSERT(mutex_owned(&sme_mtx));
    982 
    983 again:
    984 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    985 		if (idx >= sme->sme_fsensor &&
    986 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors)) {
    987 			if (sme->sme_flags & SME_FLAG_BUSY) {
    988 				cv_wait(&sme_cv, &sme_mtx);
    989 				goto again;
    990 			}
    991 			sme->sme_flags |= SME_FLAG_BUSY;
    992 			break;
    993 		}
    994 	}
    995 	return sme;
    996 }
    997 
    998 /*
    999  * sme_initial_refresh:
   1000  *
   1001  * 	+ Do an initial refresh of the sensors in a device just after
   1002  * 	  interrupts are enabled in the autoconf(9) process.
   1003  *
   1004  */
   1005 static void
   1006 sme_initial_refresh(void *arg)
   1007 {
   1008 	struct sysmon_envsys *sme = arg;
   1009 	envsys_data_t *edata;
   1010 
   1011 	mutex_enter(&sme_mtx);
   1012 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head)
   1013 		(*sme->sme_refresh)(sme, edata);
   1014 	mutex_exit(&sme_mtx);
   1015 }
   1016 
   1017 /*
   1018  * sme_sensor_dictionary_get:
   1019  *
   1020  * 	+ Returns a dictionary of a device specified by its index
   1021  * 	  position.
   1022  */
   1023 prop_dictionary_t
   1024 sme_sensor_dictionary_get(prop_array_t array, const char *index)
   1025 {
   1026 	prop_object_iterator_t iter;
   1027 	prop_dictionary_t dict;
   1028 	prop_object_t obj;
   1029 
   1030 	KASSERT(array != NULL || index != NULL);
   1031 
   1032 	iter = prop_array_iterator(array);
   1033 	if (!iter)
   1034 		return NULL;
   1035 
   1036 	while ((dict = prop_object_iterator_next(iter))) {
   1037 		obj = prop_dictionary_get(dict, "index");
   1038 		if (prop_string_equals_cstring(obj, index))
   1039 			break;
   1040 	}
   1041 
   1042 	prop_object_iterator_release(iter);
   1043 	return dict;
   1044 }
   1045 
   1046 /*
   1047  * sme_remove_userprops:
   1048  *
   1049  * 	+ Remove all properties from all devices that were set by
   1050  * 	  the ENVSYS_SETDICTIONARY ioctl.
   1051  */
   1052 static void
   1053 sme_remove_userprops(void)
   1054 {
   1055 	struct sysmon_envsys *sme;
   1056 	prop_array_t array;
   1057 	prop_dictionary_t sdict;
   1058 	envsys_data_t *edata = NULL;
   1059 	char tmp[ENVSYS_DESCLEN];
   1060 	int ptype;
   1061 
   1062 	mutex_enter(&sme_mtx);
   1063 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   1064 		sysmon_envsys_acquire(sme);
   1065 		array = prop_dictionary_get(sme_propd, sme->sme_name);
   1066 
   1067 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1068 			(void)snprintf(tmp, sizeof(tmp), "sensor%d",
   1069 				       edata->sensor);
   1070 			sdict = sme_sensor_dictionary_get(array, tmp);
   1071 			KASSERT(sdict != NULL);
   1072 
   1073 			if (edata->upropset & USERPROP_BATTCAP) {
   1074 				prop_dictionary_remove(sdict,
   1075 				    "critical-capacity");
   1076 				ptype = PENVSYS_EVENT_BATT_USERCAP;
   1077 				sme_event_unregister(sme, edata->desc, ptype);
   1078 			}
   1079 
   1080 			if (edata->upropset & USERPROP_CRITMAX) {
   1081 				prop_dictionary_remove(sdict,
   1082 				    "critical-max");
   1083 				ptype = PENVSYS_EVENT_USER_CRITMAX;
   1084 				sme_event_unregister(sme, edata->desc, ptype);
   1085 			}
   1086 
   1087 			if (edata->upropset & USERPROP_CRITMIN) {
   1088 				prop_dictionary_remove(sdict,
   1089 				    "critical-min");
   1090 				ptype = PENVSYS_EVENT_USER_CRITMIN;
   1091 				sme_event_unregister(sme, edata->desc, ptype);
   1092 			}
   1093 
   1094 			if (edata->upropset & USERPROP_RFACT) {
   1095 				(void)sme_sensor_upint32(sdict, "rfact", 0);
   1096 				edata->rfact = 0;
   1097 			}
   1098 
   1099 			if (edata->upropset & USERPROP_DESC)
   1100 				(void)sme_sensor_upstring(sdict,
   1101 			  	    "description", edata->desc);
   1102 
   1103 			if (edata->upropset)
   1104 				edata->upropset = 0;
   1105 		}
   1106 
   1107 		/*
   1108 		 * Restore default timeout value.
   1109 		 */
   1110 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1111 		sysmon_envsys_release(sme);
   1112 	}
   1113 	mutex_exit(&sme_mtx);
   1114 }
   1115 
   1116 /*
   1117  * sme_add_property_dictionary:
   1118  *
   1119  * 	+ Add global properties into a device.
   1120  */
   1121 static int
   1122 sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1123 			    prop_dictionary_t dict)
   1124 {
   1125 	prop_dictionary_t pdict;
   1126 	int error = 0;
   1127 
   1128 	pdict = prop_dictionary_create();
   1129 	if (!pdict)
   1130 		return EINVAL;
   1131 
   1132 	/*
   1133 	 * Add the 'refresh-timeout' object into the 'device-properties'
   1134 	 * dictionary. We use by default 30 seconds.
   1135 	 *
   1136 	 * 	...
   1137 	 * 	<dict>
   1138 	 * 		<key>device-properties</key>
   1139 	 * 		<dict>
   1140 	 * 			<key>refresh-timeout</key>
   1141 	 * 			<integer>120</integer<
   1142 	 * 		</dict<
   1143 	 * 	</dict>
   1144 	 * 	...
   1145 	 *
   1146 	 */
   1147 	if (!sme->sme_events_timeout)
   1148 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1149 
   1150 	if (!prop_dictionary_set_uint64(pdict, "refresh-timeout",
   1151 					sme->sme_events_timeout)) {
   1152 		error = EINVAL;
   1153 		goto out;
   1154 	}
   1155 
   1156 	if (!prop_dictionary_set(dict, "device-properties", pdict)) {
   1157 		error = EINVAL;
   1158 		goto out;
   1159 	}
   1160 
   1161 	/*
   1162 	 * Add the device dictionary into the sysmon envsys array.
   1163 	 */
   1164 	if (!prop_array_add(array, dict))
   1165 		error = EINVAL;
   1166 
   1167 out:
   1168 	prop_object_release(pdict);
   1169 	return error;
   1170 }
   1171 
   1172 /*
   1173  * sme_add_sensor_dictionary:
   1174  *
   1175  * 	+ Adds the sensor objects into the dictionary and returns a pointer
   1176  * 	  to a sme_event_drv_t object if a monitoring flag was set
   1177  * 	  (or NULL otherwise).
   1178  */
   1179 sme_event_drv_t *
   1180 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1181 		    	  prop_dictionary_t dict, envsys_data_t *edata)
   1182 {
   1183 	const struct sme_description_table *sdt, *sdt_units;
   1184 	sme_event_drv_t *sme_evdrv_t = NULL;
   1185 	int i, j;
   1186 	char indexstr[ENVSYS_DESCLEN];
   1187 
   1188 	/*
   1189 	 * Find the correct units for this sensor.
   1190 	 */
   1191 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
   1192 	for (i = 0; sdt_units[i].type != -1; i++)
   1193 		if (sdt_units[i].type == edata->units)
   1194 			break;
   1195 
   1196 	/*
   1197 	 * Add the index sensor string.
   1198 	 *
   1199 	 * 		...
   1200 	 * 		<key>index</eyr
   1201 	 * 		<string>sensor0</string>
   1202 	 * 		...
   1203 	 */
   1204 	(void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
   1205 	if (sme_sensor_upstring(dict, "index", indexstr))
   1206 		goto bad;
   1207 
   1208 	/*
   1209 	 * 		...
   1210 	 * 		<key>type</key>
   1211 	 * 		<string>foo</string>
   1212 	 * 		<key>description</key>
   1213 	 * 		<string>blah blah</string>
   1214 	 * 		...
   1215 	 */
   1216 	if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
   1217 		goto bad;
   1218 
   1219 	if (sme_sensor_upstring(dict, "description", edata->desc))
   1220 		goto bad;
   1221 
   1222 	/*
   1223 	 * Add sensor's state description.
   1224 	 *
   1225 	 * 		...
   1226 	 * 		<key>state</key>
   1227 	 * 		<string>valid</string>
   1228 	 * 		...
   1229 	 */
   1230 	sdt = sme_get_description_table(SME_DESC_STATES);
   1231 	for (j = 0; sdt[j].type != -1; j++)
   1232 		if (sdt[j].type == edata->state)
   1233 			break;
   1234 
   1235 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
   1236 	    __func__, edata->desc, edata->units, edata->state));
   1237 
   1238 	if (sme_sensor_upstring(dict, "state", sdt[j].desc))
   1239 		goto bad;
   1240 
   1241 	/*
   1242 	 * Add the monitoring boolean object:
   1243 	 *
   1244 	 * 		...
   1245 	 * 		<key>monitoring-supported</key>
   1246 	 * 		<true/>
   1247 	 *		...
   1248 	 *
   1249 	 * always false on Battery {capacity,charge}, Drive and Indicator types.
   1250 	 * They cannot be monitored.
   1251 	 *
   1252 	 */
   1253 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1254 	    (edata->units == ENVSYS_INDICATOR) ||
   1255 	    (edata->units == ENVSYS_DRIVE) ||
   1256 	    (edata->units == ENVSYS_BATTERY_CAPACITY) ||
   1257 	    (edata->units == ENVSYS_BATTERY_CHARGE)) {
   1258 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
   1259 			goto out;
   1260 	} else {
   1261 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
   1262 			goto out;
   1263 	}
   1264 
   1265 	/*
   1266 	 * Add the percentage boolean object, true if ENVSYS_FPERCENT
   1267 	 * is set or false otherwise.
   1268 	 *
   1269 	 * 		...
   1270 	 * 		<key>want-percentage</key>
   1271 	 * 		<true/>
   1272 	 * 		...
   1273 	 */
   1274 	if (edata->flags & ENVSYS_FPERCENT)
   1275 		if (sme_sensor_upbool(dict, "want-percentage", true))
   1276 			goto out;
   1277 
   1278 	/*
   1279 	 * Add the allow-rfact boolean object, true if
   1280 	 * ENVSYS_FCHANGERFACT if set or false otherwise.
   1281 	 *
   1282 	 * 		...
   1283 	 * 		<key>allow-rfact</key>
   1284 	 * 		<true/>
   1285 	 * 		...
   1286 	 */
   1287 	if (edata->units == ENVSYS_SVOLTS_DC ||
   1288 	    edata->units == ENVSYS_SVOLTS_AC) {
   1289 		if (edata->flags & ENVSYS_FCHANGERFACT) {
   1290 			if (sme_sensor_upbool(dict, "allow-rfact", true))
   1291 				goto out;
   1292 		} else {
   1293 			if (sme_sensor_upbool(dict, "allow-rfact", false))
   1294 				goto out;
   1295 		}
   1296 	}
   1297 
   1298 	/*
   1299 	 * Add the object for battery capacity sensors:
   1300 	 *
   1301 	 * 		...
   1302 	 * 		<key>battery-capacity</key>
   1303 	 * 		<string>NORMAL</string>
   1304 	 * 		...
   1305 	 */
   1306 	if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1307 		sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1308 		for (j = 0; sdt[j].type != -1; j++)
   1309 			if (sdt[j].type == edata->value_cur)
   1310 				break;
   1311 
   1312 		if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc))
   1313 			goto out;
   1314 	}
   1315 
   1316 	/*
   1317 	 * Add the drive-state object for drive sensors:
   1318 	 *
   1319 	 * 		...
   1320 	 * 		<key>drive-state</key>
   1321 	 * 		<string>drive is online</string>
   1322 	 * 		...
   1323 	 */
   1324 	if (edata->units == ENVSYS_DRIVE) {
   1325 		sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1326 		for (j = 0; sdt[j].type != -1; j++)
   1327 			if (sdt[j].type == edata->value_cur)
   1328 				break;
   1329 
   1330 		if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
   1331 			goto out;
   1332 	}
   1333 
   1334 	/*
   1335 	 * Add the following objects if sensor is enabled...
   1336 	 */
   1337 	if (edata->state == ENVSYS_SVALID) {
   1338 		/*
   1339 		 * Add the following objects:
   1340 		 *
   1341 		 * 	...
   1342 		 * 	<key>rpms</key>
   1343 		 * 	<integer>2500</integer>
   1344 		 * 	<key>rfact</key>
   1345 		 * 	<integer>10000</integer>
   1346 		 * 	<key>cur-value</key>
   1347 	 	 * 	<integer>1250</integer>
   1348 	 	 * 	<key>min-value</key>
   1349 	 	 * 	<integer>800</integer>
   1350 	 	 * 	<key>max-value</integer>
   1351 	 	 * 	<integer>3000</integer>
   1352 	 	 * 	<key>avg-value</integer>
   1353 	 	 * 	<integer>1400</integer>
   1354 	 	 * 	...
   1355 	 	 */
   1356 		if (edata->units == ENVSYS_SFANRPM)
   1357 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
   1358 				goto out;
   1359 
   1360 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1361 	    	    edata->units == ENVSYS_SVOLTS_DC)
   1362 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
   1363 				goto out;
   1364 
   1365 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
   1366 			goto out;
   1367 
   1368 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1369 			if (sme_sensor_upint32(dict,
   1370 					       "min-value",
   1371 					       edata->value_min))
   1372 			goto out;
   1373 		}
   1374 
   1375 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1376 			if (sme_sensor_upint32(dict,
   1377 					       "max-value",
   1378 					       edata->value_max))
   1379 			goto out;
   1380 		}
   1381 
   1382 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1383 			if (sme_sensor_upint32(dict,
   1384 					       "avg-value",
   1385 					       edata->value_avg))
   1386 			goto out;
   1387 		}
   1388 	}
   1389 
   1390 	/*
   1391 	 * 	...
   1392 	 * </dict>
   1393 	 *
   1394 	 * Add the dictionary into the array.
   1395 	 *
   1396 	 */
   1397 	if (!prop_array_add(array, dict)) {
   1398 		DPRINTF(("%s: prop_array_add\n", __func__));
   1399 		goto bad;
   1400 	}
   1401 
   1402 	/*
   1403 	 * Register a new event if a monitoring flag was set.
   1404 	 */
   1405 	if (edata->monitor) {
   1406 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
   1407 		sme_evdrv_t->sed_sdict = dict;
   1408 		sme_evdrv_t->sed_edata = edata;
   1409 		sme_evdrv_t->sed_sme = sme;
   1410 		sme_evdrv_t->sed_powertype = sdt_units[i].crittype;
   1411 	}
   1412 
   1413 out:
   1414 	return sme_evdrv_t;
   1415 
   1416 bad:
   1417 	prop_object_release(dict);
   1418 	return NULL;
   1419 }
   1420 
   1421 /*
   1422  * sme_update_dictionary:
   1423  *
   1424  * 	+ Update per-sensor dictionaries with new values if there were
   1425  * 	  changes, otherwise the object in dictionary is untouched.
   1426  */
   1427 int
   1428 sme_update_dictionary(struct sysmon_envsys *sme)
   1429 {
   1430 	const struct sme_description_table *sdt;
   1431 	envsys_data_t *edata;
   1432 	prop_object_t array, dict, obj, obj2;
   1433 	int j, error = 0;
   1434 
   1435 	KASSERT(mutex_owned(&sme_mtx));
   1436 
   1437 	/*
   1438 	 * Retrieve the array of dictionaries in device.
   1439 	 */
   1440 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1441 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
   1442 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
   1443 		return EINVAL;
   1444 	}
   1445 
   1446 	/*
   1447 	 * Get the last dictionary on the array, this contains the
   1448 	 * 'device-properties' sub-dictionary.
   1449 	 */
   1450 	obj = prop_array_get(array, prop_array_count(array) - 1);
   1451 	if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) {
   1452 		DPRINTF(("%s: not a device-properties dictionary\n", __func__));
   1453 		return EINVAL;
   1454 	}
   1455 
   1456 	obj2 = prop_dictionary_get(obj, "device-properties");
   1457 	if (!obj2)
   1458 		return EINVAL;
   1459 
   1460 	/*
   1461 	 * Update the 'refresh-timeout' property.
   1462 	 */
   1463 	if (!prop_dictionary_set_uint64(obj2, "refresh-timeout",
   1464 					sme->sme_events_timeout))
   1465 		return EINVAL;
   1466 
   1467 	/*
   1468 	 * - iterate over all sensors.
   1469 	 * - fetch new data.
   1470 	 * - check if data in dictionary is different than new data.
   1471 	 * - update dictionary if there were changes.
   1472 	 */
   1473 	DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
   1474 	    sme->sme_name, sme->sme_nsensors));
   1475 
   1476 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1477 		/*
   1478 		 * refresh sensor data via sme_refresh only if the
   1479 		 * flag is not set.
   1480 		 */
   1481 		if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
   1482 			(*sme->sme_refresh)(sme, edata);
   1483 
   1484 		/*
   1485 		 * retrieve sensor's dictionary.
   1486 		 */
   1487 		dict = prop_array_get(array, edata->sensor);
   1488 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1489 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1490 			    __func__, edata->sensor, sme->sme_name));
   1491 			return EINVAL;
   1492 		}
   1493 
   1494 		/*
   1495 		 * update sensor's state.
   1496 		 */
   1497 		sdt = sme_get_description_table(SME_DESC_STATES);
   1498 		for (j = 0; sdt[j].type != -1; j++)
   1499 			if (sdt[j].type == edata->state)
   1500 				break;
   1501 
   1502 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1503 		    "units=%d sensor=%d\n", __func__, sdt[j].desc,
   1504 		    sdt[j].type, edata->flags, edata->units, edata->sensor));
   1505 
   1506 		error = sme_sensor_upstring(dict, "state", sdt[j].desc);
   1507 		if (error)
   1508 			break;
   1509 
   1510 		/*
   1511 		 * update sensor's type.
   1512 		 */
   1513 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1514 		for (j = 0; sdt[j].type != -1; j++)
   1515 			if (sdt[j].type == edata->units)
   1516 				break;
   1517 
   1518 		error = sme_sensor_upstring(dict, "type", sdt[j].desc);
   1519 		if (error)
   1520 			break;
   1521 
   1522 		/*
   1523 		 * update sensor's current value.
   1524 		 */
   1525 		error = sme_sensor_upint32(dict,
   1526 					   "cur-value",
   1527 					   edata->value_cur);
   1528 		if (error)
   1529 			break;
   1530 
   1531 		/*
   1532 		 * Battery charge, Integer and Indicator types do not
   1533 		 * need the following objects, so skip them.
   1534 		 */
   1535 		if (edata->units == ENVSYS_INTEGER ||
   1536 		    edata->units == ENVSYS_INDICATOR ||
   1537 		    edata->units == ENVSYS_BATTERY_CHARGE)
   1538 			continue;
   1539 
   1540 		/*
   1541 		 * update sensor flags.
   1542 		 */
   1543 		if (edata->flags & ENVSYS_FPERCENT) {
   1544 			error = sme_sensor_upbool(dict,
   1545 						  "want-percentage",
   1546 						  true);
   1547 			if (error)
   1548 				break;
   1549 		}
   1550 
   1551 		/*
   1552 		 * update sensor's {avg,max,min}-value.
   1553 		 */
   1554 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1555 			error = sme_sensor_upint32(dict,
   1556 						   "max-value",
   1557 						   edata->value_max);
   1558 			if (error)
   1559 				break;
   1560 		}
   1561 
   1562 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1563 			error = sme_sensor_upint32(dict,
   1564 						   "min-value",
   1565 						   edata->value_min);
   1566 			if (error)
   1567 				break;
   1568 		}
   1569 
   1570 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1571 			error = sme_sensor_upint32(dict,
   1572 						   "avg-value",
   1573 						   edata->value_avg);
   1574 			if (error)
   1575 				break;
   1576 		}
   1577 
   1578 		/*
   1579 		 * update 'rpms' only for ENVSYS_SFANRPM sensors.
   1580 		 */
   1581 		if (edata->units == ENVSYS_SFANRPM) {
   1582 			error = sme_sensor_upuint32(dict,
   1583 						    "rpms",
   1584 						    edata->rpms);
   1585 			if (error)
   1586 				break;
   1587 		}
   1588 
   1589 		/*
   1590 		 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors.
   1591 		 */
   1592 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1593 		    edata->units == ENVSYS_SVOLTS_DC) {
   1594 			error = sme_sensor_upint32(dict,
   1595 						   "rfact",
   1596 						   edata->rfact);
   1597 			if (error)
   1598 				break;
   1599 		}
   1600 
   1601 		/*
   1602 		 * update 'drive-state' only for ENVSYS_DRIVE sensors.
   1603 		 */
   1604 		if (edata->units == ENVSYS_DRIVE) {
   1605 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1606 			for (j = 0; sdt[j].type != -1; j++)
   1607 				if (sdt[j].type == edata->value_cur)
   1608 					break;
   1609 
   1610 			error = sme_sensor_upstring(dict,
   1611 						    "drive-state",
   1612 						    sdt[j].desc);
   1613 			if (error)
   1614 				break;
   1615 		}
   1616 
   1617 		/*
   1618 		 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY
   1619 		 * sensors.
   1620 		 */
   1621 		if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1622 			sdt =
   1623 			  sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1624 			for (j = 0; sdt[j].type != -1; j++)
   1625 				if (sdt[j].type == edata->value_cur)
   1626 					break;
   1627 
   1628 			error = sme_sensor_upstring(dict,
   1629 						    "battery-capacity",
   1630 						    sdt[j].desc);
   1631 			if (error)
   1632 				break;
   1633 		}
   1634 	}
   1635 
   1636 	return error;
   1637 }
   1638 
   1639 /*
   1640  * sme_userset_dictionary:
   1641  *
   1642  * 	+ Parse the userland dictionary and run the appropiate tasks
   1643  * 	  that were specified.
   1644  */
   1645 int
   1646 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1647 		       prop_array_t array)
   1648 {
   1649 	const struct sme_description_table *sdt;
   1650 	envsys_data_t *edata;
   1651 	prop_dictionary_t dict, tdict = NULL;
   1652 	prop_object_t obj, obj1, obj2, tobj = NULL;
   1653 	uint64_t refresh_timo = 0;
   1654 	int32_t critval;
   1655 	int i, error = 0;
   1656 	const char *blah;
   1657 	bool targetfound = false;
   1658 
   1659 	KASSERT(mutex_owned(&sme_mtx));
   1660 
   1661 	/*
   1662 	 * The user wanted to change the refresh timeout value for this
   1663 	 * device.
   1664 	 *
   1665 	 * Get the 'device-properties' object from the userland dictionary.
   1666 	 */
   1667 	obj = prop_dictionary_get(udict, "device-properties");
   1668 	if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) {
   1669 		/*
   1670 		 * Get the 'refresh-timeout' property for this device.
   1671 		 */
   1672 		obj1 = prop_dictionary_get(obj, "refresh-timeout");
   1673 		if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) {
   1674 			targetfound = true;
   1675 			refresh_timo =
   1676 			    prop_number_unsigned_integer_value(obj1);
   1677 			if (refresh_timo < 1)
   1678 				error = EINVAL;
   1679 			else
   1680 				sme->sme_events_timeout = refresh_timo;
   1681 		}
   1682 		goto out;
   1683 
   1684 	} else if (!obj) {
   1685 		/*
   1686 		 * Get sensor's index from userland dictionary.
   1687 		 */
   1688 		obj = prop_dictionary_get(udict, "index");
   1689 		if (!obj)
   1690 			goto out;
   1691 		if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1692 			DPRINTF(("%s: 'index' not a string\n", __func__));
   1693 			return EINVAL;
   1694 		}
   1695 	} else
   1696 		return EINVAL;
   1697 
   1698 	/*
   1699 	 * iterate over the sensors to find the right one.
   1700 	 */
   1701 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1702 		/*
   1703 		 * Get a dictionary and check if it's our sensor by checking
   1704 		 * at its index position.
   1705 		 */
   1706 		dict = prop_array_get(array, edata->sensor);
   1707 		obj1 = prop_dictionary_get(dict, "index");
   1708 
   1709 		/*
   1710 		 * is it our sensor?
   1711 		 */
   1712 		if (!prop_string_equals(obj1, obj))
   1713 			continue;
   1714 
   1715 		/*
   1716 		 * Check if a new description operation was
   1717 		 * requested by the user and set new description.
   1718 		 */
   1719 		obj2 = prop_dictionary_get(udict, "description");
   1720 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) {
   1721 			targetfound = true;
   1722 			blah = prop_string_cstring_nocopy(obj2);
   1723 
   1724 			/*
   1725 			 * Check for duplicate description.
   1726 			 */
   1727 			for (i = 0; i < sme->sme_nsensors; i++) {
   1728 				if (i == edata->sensor)
   1729 					continue;
   1730 				tdict = prop_array_get(array, i);
   1731 				tobj =
   1732 				    prop_dictionary_get(tdict, "description");
   1733 				if (prop_string_equals(obj2, tobj))
   1734 					return EEXIST;
   1735 			}
   1736 
   1737 			/*
   1738 			 * Update the object in dictionary.
   1739 			 */
   1740 			error = sme_sensor_upstring(dict,
   1741 						    "description",
   1742 						    blah);
   1743 			if (error)
   1744 				return error;
   1745 
   1746 			DPRINTF(("%s: sensor%d changed desc to: %s\n",
   1747 			    __func__, edata->sensor, blah));
   1748 			edata->upropset |= USERPROP_DESC;
   1749 		}
   1750 
   1751 		/*
   1752 		 * did the user want to change the rfact?
   1753 		 */
   1754 		obj2 = prop_dictionary_get(udict, "rfact");
   1755 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1756 			targetfound = true;
   1757 			if (edata->flags & ENVSYS_FCHANGERFACT) {
   1758 				edata->rfact = prop_number_integer_value(obj2);
   1759 				edata->upropset |= USERPROP_RFACT;
   1760 				DPRINTF(("%s: sensor%d changed rfact to %d\n",
   1761 				    __func__, edata->sensor, edata->rfact));
   1762 			} else
   1763 				return ENOTSUP;
   1764 		}
   1765 
   1766 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1767 		for (i = 0; sdt[i].type != -1; i++)
   1768 			if (sdt[i].type == edata->units)
   1769 				break;
   1770 
   1771 		/*
   1772 		 * did the user want to set a critical capacity event?
   1773 		 *
   1774 		 * NOTE: if sme_event_register returns EEXIST that means
   1775 		 * the object is already there, but this is not a real
   1776 		 * error, because the object might be updated.
   1777 		 */
   1778 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1779 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1780 			targetfound = true;
   1781 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1782 			    (edata->flags & ENVSYS_FPERCENT) == 0)
   1783 				return ENOTSUP;
   1784 
   1785 			critval = prop_number_integer_value(obj2);
   1786 			error = sme_event_register(dict,
   1787 					      edata,
   1788 					      sme,
   1789 					      "critical-capacity",
   1790 					      critval,
   1791 					      PENVSYS_EVENT_BATT_USERCAP,
   1792 					      sdt[i].crittype);
   1793 			if (error == EEXIST)
   1794 				error = 0;
   1795 			if (error)
   1796 				goto out;
   1797 			else if (!error)
   1798 				edata->upropset |= USERPROP_BATTCAP;
   1799 		}
   1800 
   1801 		/*
   1802 		 * did the user want to set a critical max event?
   1803 		 */
   1804 		obj2 = prop_dictionary_get(udict, "critical-max");
   1805 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1806 			targetfound = true;
   1807 			if (edata->units == ENVSYS_INDICATOR ||
   1808 			    edata->flags & ENVSYS_FMONNOTSUPP)
   1809 				return ENOTSUP;
   1810 
   1811 			critval = prop_number_integer_value(obj2);
   1812 			error = sme_event_register(dict,
   1813 					      edata,
   1814 					      sme,
   1815 					      "critical-max",
   1816 					      critval,
   1817 					      PENVSYS_EVENT_USER_CRITMAX,
   1818 					      sdt[i].crittype);
   1819 			if (error == EEXIST)
   1820 				error = 0;
   1821 			if (error)
   1822 				goto out;
   1823 			else if (!error)
   1824 				edata->upropset |= USERPROP_CRITMAX;
   1825 		}
   1826 
   1827 		/*
   1828 		 * did the user want to set a critical min event?
   1829 		 */
   1830 		obj2 = prop_dictionary_get(udict, "critical-min");
   1831 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1832 			targetfound = true;
   1833 			if (edata->units == ENVSYS_INDICATOR ||
   1834 			    edata->flags & ENVSYS_FMONNOTSUPP)
   1835 				return ENOTSUP;
   1836 
   1837 			critval = prop_number_integer_value(obj2);
   1838 			error = sme_event_register(dict,
   1839 					      edata,
   1840 					      sme,
   1841 					      "critical-min",
   1842 					      critval,
   1843 					      PENVSYS_EVENT_USER_CRITMIN,
   1844 					      sdt[i].crittype);
   1845 			if (error == EEXIST)
   1846 				error = 0;
   1847 			if (error)
   1848 				goto out;
   1849 			else if (!error)
   1850 				edata->upropset |= USERPROP_CRITMIN;
   1851 		}
   1852 
   1853 		/*
   1854 		 * All objects in dictionary were processed.
   1855 		 */
   1856 		break;
   1857 	}
   1858 
   1859 out:
   1860 	/*
   1861 	 * invalid target? return the error.
   1862 	 */
   1863 	if (!targetfound)
   1864 		error = EINVAL;
   1865 
   1866 	return error;
   1867 }
   1868