Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.140
      1 /*	$NetBSD: sysmon_envsys.c,v 1.140 2017/09/06 11:08:53 msaitoh 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.140 2017/09/06 11:08:53 msaitoh 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 #include <sys/rndsource.h>
     80 #include <sys/module.h>
     81 #include <sys/once.h>
     82 
     83 #include <dev/sysmon/sysmonvar.h>
     84 #include <dev/sysmon/sysmon_envsysvar.h>
     85 #include <dev/sysmon/sysmon_taskq.h>
     86 
     87 kmutex_t sme_global_mtx;
     88 
     89 prop_dictionary_t sme_propd;
     90 
     91 struct sysmon_envsys_lh sysmon_envsys_list;
     92 
     93 static uint32_t sysmon_envsys_next_sensor_index;
     94 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
     95 
     96 static void sysmon_envsys_destroy_plist(prop_array_t);
     97 static void sme_remove_userprops(void);
     98 static int sme_add_property_dictionary(struct sysmon_envsys *, prop_array_t,
     99 				       prop_dictionary_t);
    100 static sme_event_drv_t * sme_add_sensor_dictionary(struct sysmon_envsys *,
    101 	prop_array_t, prop_dictionary_t, envsys_data_t *);
    102 static void sme_initial_refresh(void *);
    103 static uint32_t sme_get_max_value(struct sysmon_envsys *,
    104      bool (*)(const envsys_data_t*), bool);
    105 
    106 MODULE(MODULE_CLASS_DRIVER, sysmon_envsys, "sysmon,sysmon_taskq,sysmon_power");
    107 
    108 static struct sysmon_opvec sysmon_envsys_opvec = {
    109         sysmonopen_envsys, sysmonclose_envsys, sysmonioctl_envsys,
    110         NULL, NULL, NULL
    111 };
    112 
    113 ONCE_DECL(once_envsys);
    114 
    115 static int
    116 sme_preinit(void)
    117 {
    118 
    119 	LIST_INIT(&sysmon_envsys_list);
    120 	mutex_init(&sme_global_mtx, MUTEX_DEFAULT, IPL_NONE);
    121 	sme_propd = prop_dictionary_create();
    122 
    123 	return 0;
    124 }
    125 
    126 /*
    127  * sysmon_envsys_init:
    128  *
    129  * 	+ Initialize global mutex, dictionary and the linked list.
    130  */
    131 int
    132 sysmon_envsys_init(void)
    133 {
    134 	int error;
    135 
    136 	(void)RUN_ONCE(&once_envsys, sme_preinit);
    137 
    138 	error = sysmon_attach_minor(SYSMON_MINOR_ENVSYS, &sysmon_envsys_opvec);
    139 
    140 	return error;
    141 }
    142 
    143 int
    144 sysmon_envsys_fini(void)
    145 {
    146 	int error;
    147 
    148 	if ( ! LIST_EMPTY(&sysmon_envsys_list))
    149 		error = EBUSY;
    150 	else
    151 		error = sysmon_attach_minor(SYSMON_MINOR_ENVSYS, NULL);
    152 
    153 	if (error == 0)
    154 		mutex_destroy(&sme_global_mtx);
    155 
    156 	// XXX: prop_dictionary ???
    157 
    158 	return error;
    159 }
    160 
    161 /*
    162  * sysmonopen_envsys:
    163  *
    164  *	+ Open the system monitor device.
    165  */
    166 int
    167 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    168 {
    169 	return 0;
    170 }
    171 
    172 /*
    173  * sysmonclose_envsys:
    174  *
    175  *	+ Close the system monitor device.
    176  */
    177 int
    178 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    179 {
    180 	return 0;
    181 }
    182 
    183 /*
    184  * sysmonioctl_envsys:
    185  *
    186  *	+ Perform a sysmon envsys control request.
    187  */
    188 int
    189 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    190 {
    191 	struct sysmon_envsys *sme = NULL;
    192 	int error = 0;
    193 	u_int oidx;
    194 
    195 	switch (cmd) {
    196 	/*
    197 	 * To update the global dictionary with latest data from devices.
    198 	 */
    199 	case ENVSYS_GETDICTIONARY:
    200 	    {
    201 		struct plistref *plist = (struct plistref *)data;
    202 
    203 		/*
    204 		 * Update dictionaries on all sysmon envsys devices
    205 		 * registered.
    206 		 */
    207 		mutex_enter(&sme_global_mtx);
    208 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    209 			sysmon_envsys_acquire(sme, false);
    210 			error = sme_update_dictionary(sme);
    211 			if (error) {
    212 				DPRINTF(("%s: sme_update_dictionary, "
    213 				    "error=%d\n", __func__, error));
    214 				sysmon_envsys_release(sme, false);
    215 				mutex_exit(&sme_global_mtx);
    216 				return error;
    217 			}
    218 			sysmon_envsys_release(sme, false);
    219 		}
    220 		mutex_exit(&sme_global_mtx);
    221 		/*
    222 		 * Copy global dictionary to userland.
    223 		 */
    224 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    225 		break;
    226 	    }
    227 	/*
    228 	 * To set properties on multiple devices.
    229 	 */
    230 	case ENVSYS_SETDICTIONARY:
    231 	    {
    232 		const struct plistref *plist = (const struct plistref *)data;
    233 		prop_dictionary_t udict;
    234 		prop_object_iterator_t iter, iter2;
    235 		prop_object_t obj, obj2;
    236 		prop_array_t array_u, array_k;
    237 		const char *devname = NULL;
    238 
    239 		if ((flag & FWRITE) == 0)
    240 			return EPERM;
    241 
    242 		/*
    243 		 * Get dictionary from userland.
    244 		 */
    245 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    246 		if (error) {
    247 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    248 			    __func__, error));
    249 			break;
    250 		}
    251 
    252 		iter = prop_dictionary_iterator(udict);
    253 		if (!iter) {
    254 			prop_object_release(udict);
    255 			return ENOMEM;
    256 		}
    257 
    258 		/*
    259 		 * Iterate over the userland dictionary and process
    260 		 * the list of devices.
    261 		 */
    262 		while ((obj = prop_object_iterator_next(iter))) {
    263 			array_u = prop_dictionary_get_keysym(udict, obj);
    264 			if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
    265 				prop_object_iterator_release(iter);
    266 				prop_object_release(udict);
    267 				return EINVAL;
    268 			}
    269 
    270 			devname = prop_dictionary_keysym_cstring_nocopy(obj);
    271 			DPRINTF(("%s: processing the '%s' array requests\n",
    272 			    __func__, devname));
    273 
    274 			/*
    275 			 * find the correct sme device.
    276 			 */
    277 			sme = sysmon_envsys_find(devname);
    278 			if (!sme) {
    279 				DPRINTF(("%s: NULL sme\n", __func__));
    280 				prop_object_iterator_release(iter);
    281 				prop_object_release(udict);
    282 				return EINVAL;
    283 			}
    284 
    285 			/*
    286 			 * Find the correct array object with the string
    287 			 * supplied by the userland dictionary.
    288 			 */
    289 			array_k = prop_dictionary_get(sme_propd, devname);
    290 			if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
    291 				DPRINTF(("%s: array device failed\n",
    292 				    __func__));
    293 				sysmon_envsys_release(sme, false);
    294 				prop_object_iterator_release(iter);
    295 				prop_object_release(udict);
    296 				return EINVAL;
    297 			}
    298 
    299 			iter2 = prop_array_iterator(array_u);
    300 			if (!iter2) {
    301 				sysmon_envsys_release(sme, false);
    302 				prop_object_iterator_release(iter);
    303 				prop_object_release(udict);
    304 				return ENOMEM;
    305 			}
    306 
    307 			/*
    308 			 * Iterate over the array of dictionaries to
    309 			 * process the list of sensors and properties.
    310 			 */
    311 			while ((obj2 = prop_object_iterator_next(iter2))) {
    312 				/*
    313 				 * do the real work now.
    314 				 */
    315 				error = sme_userset_dictionary(sme,
    316 							       obj2,
    317 							       array_k);
    318 				if (error) {
    319 					sysmon_envsys_release(sme, false);
    320 					prop_object_iterator_release(iter2);
    321 					prop_object_iterator_release(iter);
    322 					prop_object_release(udict);
    323 					return error;
    324 				}
    325 			}
    326 
    327 			sysmon_envsys_release(sme, false);
    328 			prop_object_iterator_release(iter2);
    329 		}
    330 
    331 		prop_object_iterator_release(iter);
    332 		prop_object_release(udict);
    333 		break;
    334 	    }
    335 	/*
    336 	 * To remove all properties from all devices registered.
    337 	 */
    338 	case ENVSYS_REMOVEPROPS:
    339 	    {
    340 		const struct plistref *plist = (const struct plistref *)data;
    341 		prop_dictionary_t udict;
    342 		prop_object_t obj;
    343 
    344 		if ((flag & FWRITE) == 0)
    345 			return EPERM;
    346 
    347 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    348 		if (error) {
    349 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    350 			    __func__, error));
    351 			break;
    352 		}
    353 
    354 		obj = prop_dictionary_get(udict, "envsys-remove-props");
    355 		if (!obj || !prop_bool_true(obj)) {
    356 			DPRINTF(("%s: invalid 'envsys-remove-props'\n",
    357 			     __func__));
    358 			return EINVAL;
    359 		}
    360 
    361 		prop_object_release(udict);
    362 		sme_remove_userprops();
    363 
    364 		break;
    365 	    }
    366 	/*
    367 	 * Compatibility ioctls with the old interface, only implemented
    368 	 * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old
    369 	 * applications work.
    370 	 */
    371 	case ENVSYS_GTREDATA:
    372 	    {
    373 		struct envsys_tre_data *tred = (void *)data;
    374 		envsys_data_t *edata = NULL;
    375 		bool found = false;
    376 
    377 		tred->validflags = 0;
    378 
    379 		sme = sysmon_envsys_find_40(tred->sensor);
    380 		if (!sme)
    381 			break;
    382 
    383 		oidx = tred->sensor;
    384 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    385 
    386 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    387 		    __func__, tred->sensor, oidx, sme->sme_name,
    388 		    sme->sme_nsensors));
    389 
    390 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    391 			if (edata->sensor == tred->sensor) {
    392 				found = true;
    393 				break;
    394 			}
    395 		}
    396 
    397 		if (!found) {
    398 			sysmon_envsys_release(sme, false);
    399 			error = ENODEV;
    400 			break;
    401 		}
    402 
    403 		if (tred->sensor < sme->sme_nsensors) {
    404 			if ((sme->sme_flags & SME_POLL_ONLY) == 0) {
    405 				mutex_enter(&sme->sme_mtx);
    406 				sysmon_envsys_refresh_sensor(sme, edata);
    407 				mutex_exit(&sme->sme_mtx);
    408 			}
    409 
    410 			/*
    411 			 * copy required values to the old interface.
    412 			 */
    413 			tred->sensor = edata->sensor;
    414 			tred->cur.data_us = edata->value_cur;
    415 			tred->cur.data_s = edata->value_cur;
    416 			tred->max.data_us = edata->value_max;
    417 			tred->max.data_s = edata->value_max;
    418 			tred->min.data_us = edata->value_min;
    419 			tred->min.data_s = edata->value_min;
    420 			tred->avg.data_us = 0;
    421 			tred->avg.data_s = 0;
    422 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    423 				tred->units = ENVSYS_INDICATOR;
    424 			else
    425 				tred->units = edata->units;
    426 
    427 			tred->validflags |= ENVSYS_FVALID;
    428 			tred->validflags |= ENVSYS_FCURVALID;
    429 
    430 			if (edata->flags & ENVSYS_FPERCENT) {
    431 				tred->validflags |= ENVSYS_FMAXVALID;
    432 				tred->validflags |= ENVSYS_FFRACVALID;
    433 			}
    434 
    435 			if (edata->state == ENVSYS_SINVALID) {
    436 				tred->validflags &= ~ENVSYS_FCURVALID;
    437 				tred->cur.data_us = tred->cur.data_s = 0;
    438 			}
    439 
    440 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    441 			    __func__, edata->desc, tred->cur.data_s));
    442 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    443 			    " tred->sensor=%d\n", __func__, tred->validflags,
    444 			    tred->units, tred->sensor));
    445 		}
    446 		tred->sensor = oidx;
    447 		sysmon_envsys_release(sme, false);
    448 
    449 		break;
    450 	    }
    451 	case ENVSYS_GTREINFO:
    452 	    {
    453 		struct envsys_basic_info *binfo = (void *)data;
    454 		envsys_data_t *edata = NULL;
    455 		bool found = false;
    456 
    457 		binfo->validflags = 0;
    458 
    459 		sme = sysmon_envsys_find_40(binfo->sensor);
    460 		if (!sme)
    461 			break;
    462 
    463 		oidx = binfo->sensor;
    464 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    465 
    466 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    467 			if (edata->sensor == binfo->sensor) {
    468 				found = true;
    469 				break;
    470 			}
    471 		}
    472 
    473 		if (!found) {
    474 			sysmon_envsys_release(sme, false);
    475 			error = ENODEV;
    476 			break;
    477 		}
    478 
    479 		binfo->validflags |= ENVSYS_FVALID;
    480 
    481 		if (binfo->sensor < sme->sme_nsensors) {
    482 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    483 				binfo->units = ENVSYS_INDICATOR;
    484 			else
    485 				binfo->units = edata->units;
    486 
    487 			/*
    488 			 * previously, the ACPI sensor names included the
    489 			 * device name. Include that in compatibility code.
    490 			 */
    491 			if (strncmp(sme->sme_name, "acpi", 4) == 0)
    492 				(void)snprintf(binfo->desc, sizeof(binfo->desc),
    493 				    "%s %s", sme->sme_name, edata->desc);
    494 			else
    495 				(void)strlcpy(binfo->desc, edata->desc,
    496 				    sizeof(binfo->desc));
    497 		}
    498 
    499 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    500 		    __func__, binfo->units, binfo->validflags));
    501 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    502 		    __func__, binfo->desc, binfo->sensor));
    503 
    504 		binfo->sensor = oidx;
    505 		sysmon_envsys_release(sme, false);
    506 
    507 		break;
    508 	    }
    509 	default:
    510 		error = ENOTTY;
    511 		break;
    512 	}
    513 
    514 	return error;
    515 }
    516 
    517 /*
    518  * sysmon_envsys_create:
    519  *
    520  * 	+ Allocates a new sysmon_envsys object and initializes the
    521  * 	  stuff for sensors and events.
    522  */
    523 struct sysmon_envsys *
    524 sysmon_envsys_create(void)
    525 {
    526 	struct sysmon_envsys *sme;
    527 
    528 	sme = kmem_zalloc(sizeof(*sme), KM_SLEEP);
    529 	TAILQ_INIT(&sme->sme_sensors_list);
    530 	LIST_INIT(&sme->sme_events_list);
    531 	mutex_init(&sme->sme_mtx, MUTEX_DEFAULT, IPL_NONE);
    532 	mutex_init(&sme->sme_work_mtx, MUTEX_DEFAULT, IPL_NONE);
    533 	cv_init(&sme->sme_condvar, "sme_wait");
    534 
    535 	return sme;
    536 }
    537 
    538 /*
    539  * sysmon_envsys_destroy:
    540  *
    541  * 	+ Removes all sensors from the tail queue, destroys the callout
    542  * 	  and frees the sysmon_envsys object.
    543  */
    544 void
    545 sysmon_envsys_destroy(struct sysmon_envsys *sme)
    546 {
    547 	envsys_data_t *edata;
    548 
    549 	KASSERT(sme != NULL);
    550 
    551 	while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    552 		edata = TAILQ_FIRST(&sme->sme_sensors_list);
    553 		TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    554 	}
    555 	mutex_destroy(&sme->sme_mtx);
    556 	mutex_destroy(&sme->sme_work_mtx);
    557 	cv_destroy(&sme->sme_condvar);
    558 	kmem_free(sme, sizeof(*sme));
    559 }
    560 
    561 /*
    562  * sysmon_envsys_sensor_attach:
    563  *
    564  * 	+ Attaches a sensor into a sysmon_envsys device checking that units
    565  * 	  is set to a valid type and description is unique and not empty.
    566  */
    567 int
    568 sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata)
    569 {
    570 	const struct sme_descr_entry *sdt_units;
    571 	envsys_data_t *oedata;
    572 
    573 	KASSERT(sme != NULL || edata != NULL);
    574 
    575 	/*
    576 	 * Find the correct units for this sensor.
    577 	 */
    578 	sdt_units = sme_find_table_entry(SME_DESC_UNITS, edata->units);
    579 	if (sdt_units->type == -1)
    580 		return EINVAL;
    581 
    582 	/*
    583 	 * Check that description is not empty or duplicate.
    584 	 */
    585 	if (strlen(edata->desc) == 0)
    586 		return EINVAL;
    587 
    588 	mutex_enter(&sme->sme_mtx);
    589 	sysmon_envsys_acquire(sme, true);
    590 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    591 		if (strcmp(oedata->desc, edata->desc) == 0) {
    592 			sysmon_envsys_release(sme, true);
    593 			mutex_exit(&sme->sme_mtx);
    594 			return EEXIST;
    595 		}
    596 	}
    597 	/*
    598 	 * Ok, the sensor has been added into the device queue.
    599 	 */
    600 	TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head);
    601 
    602 	/*
    603 	 * Give the sensor an index position.
    604 	 */
    605 	edata->sensor = sme->sme_nsensors;
    606 	sme->sme_nsensors++;
    607 	sysmon_envsys_release(sme, true);
    608 	mutex_exit(&sme->sme_mtx);
    609 
    610 	DPRINTF(("%s: attached #%d (%s), units=%d (%s)\n",
    611 	    __func__, edata->sensor, edata->desc,
    612 	    sdt_units->type, sdt_units->desc));
    613 
    614 	return 0;
    615 }
    616 
    617 /*
    618  * sysmon_envsys_sensor_detach:
    619  *
    620  * 	+ Detachs a sensor from a sysmon_envsys device and decrements the
    621  * 	  sensors count on success.
    622  */
    623 int
    624 sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata)
    625 {
    626 	envsys_data_t *oedata;
    627 	bool found = false;
    628 	bool destroy = false;
    629 
    630 	KASSERT(sme != NULL || edata != NULL);
    631 
    632 	/*
    633 	 * Check the sensor is already on the list.
    634 	 */
    635 	mutex_enter(&sme->sme_mtx);
    636 	sysmon_envsys_acquire(sme, true);
    637 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    638 		if (oedata->sensor == edata->sensor) {
    639 			found = true;
    640 			break;
    641 		}
    642 	}
    643 
    644 	if (!found) {
    645 		sysmon_envsys_release(sme, true);
    646 		mutex_exit(&sme->sme_mtx);
    647 		return EINVAL;
    648 	}
    649 
    650 	/*
    651 	 * remove it, unhook from rnd(4), and decrement the sensors count.
    652 	 */
    653 	if (oedata->flags & ENVSYS_FHAS_ENTROPY)
    654 		rnd_detach_source(&oedata->rnd_src);
    655 	sme_event_unregister_sensor(sme, edata);
    656 	if (LIST_EMPTY(&sme->sme_events_list)) {
    657 		sme_events_halt_callout(sme);
    658 		destroy = true;
    659 	}
    660 	TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    661 	sme->sme_nsensors--;
    662 	sysmon_envsys_release(sme, true);
    663 	mutex_exit(&sme->sme_mtx);
    664 
    665 	if (destroy)
    666 		sme_events_destroy(sme);
    667 
    668 	return 0;
    669 }
    670 
    671 
    672 /*
    673  * sysmon_envsys_register:
    674  *
    675  *	+ Register a sysmon envsys device.
    676  *	+ Create array of dictionaries for a device.
    677  */
    678 int
    679 sysmon_envsys_register(struct sysmon_envsys *sme)
    680 {
    681 	struct sme_evdrv {
    682 		SLIST_ENTRY(sme_evdrv) evdrv_head;
    683 		sme_event_drv_t *evdrv;
    684 	};
    685 	SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
    686 	struct sme_evdrv *evdv = NULL;
    687 	struct sysmon_envsys *lsme;
    688 	prop_array_t array = NULL;
    689 	prop_dictionary_t dict, dict2;
    690 	envsys_data_t *edata = NULL;
    691 	sme_event_drv_t *this_evdrv;
    692 	int nevent;
    693 	int error = 0;
    694 	char rnd_name[sizeof(edata->rnd_src.name)];
    695 
    696 	KASSERT(sme != NULL);
    697 	KASSERT(sme->sme_name != NULL);
    698 
    699 	(void)RUN_ONCE(&once_envsys, sme_preinit);
    700 
    701 	/*
    702 	 * Check if requested sysmon_envsys device is valid
    703 	 * and does not exist already in the list.
    704 	 */
    705 	mutex_enter(&sme_global_mtx);
    706 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    707 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    708 			mutex_exit(&sme_global_mtx);
    709 			return EEXIST;
    710 	       }
    711 	}
    712 	mutex_exit(&sme_global_mtx);
    713 
    714 	/*
    715 	 * sanity check: if SME_DISABLE_REFRESH is not set,
    716 	 * the sme_refresh function callback must be non NULL.
    717 	 */
    718 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    719 		if (!sme->sme_refresh)
    720 			return EINVAL;
    721 
    722 	/*
    723 	 * If the list of sensors is empty, there's no point to continue...
    724 	 */
    725 	if (TAILQ_EMPTY(&sme->sme_sensors_list)) {
    726 		DPRINTF(("%s: sensors list empty for %s\n", __func__,
    727 		    sme->sme_name));
    728 		return ENOTSUP;
    729 	}
    730 
    731 	/*
    732 	 * Initialize the singly linked list for driver events.
    733 	 */
    734 	SLIST_INIT(&sme_evdrv_list);
    735 
    736 	array = prop_array_create();
    737 	if (!array)
    738 		return ENOMEM;
    739 
    740 	/*
    741 	 * Iterate over all sensors and create a dictionary per sensor.
    742 	 * We must respect the order in which the sensors were added.
    743 	 */
    744 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    745 		dict = prop_dictionary_create();
    746 		if (!dict) {
    747 			error = ENOMEM;
    748 			goto out2;
    749 		}
    750 
    751 		/*
    752 		 * Create all objects in sensor's dictionary.
    753 		 */
    754 		this_evdrv = sme_add_sensor_dictionary(sme, array,
    755 						       dict, edata);
    756 		if (this_evdrv) {
    757 			evdv = kmem_zalloc(sizeof(*evdv), KM_SLEEP);
    758 			evdv->evdrv = this_evdrv;
    759 			SLIST_INSERT_HEAD(&sme_evdrv_list, evdv, evdrv_head);
    760 		}
    761 	}
    762 
    763 	/*
    764 	 * If the array does not contain any object (sensor), there's
    765 	 * no need to attach the driver.
    766 	 */
    767 	if (prop_array_count(array) == 0) {
    768 		error = EINVAL;
    769 		DPRINTF(("%s: empty array for '%s'\n", __func__,
    770 		    sme->sme_name));
    771 		goto out;
    772 	}
    773 
    774 	/*
    775 	 * Add the dictionary for the global properties of this device.
    776 	 */
    777 	dict2 = prop_dictionary_create();
    778 	if (!dict2) {
    779 		error = ENOMEM;
    780 		goto out;
    781 	}
    782 
    783 	error = sme_add_property_dictionary(sme, array, dict2);
    784 	if (error) {
    785 		prop_object_release(dict2);
    786 		goto out;
    787 	}
    788 
    789 	/*
    790 	 * Add the array into the global dictionary for the driver.
    791 	 *
    792 	 * <dict>
    793 	 * 	<key>foo0</key>
    794 	 * 	<array>
    795 	 * 		...
    796 	 */
    797 	mutex_enter(&sme_global_mtx);
    798 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    799 		error = EINVAL;
    800 		mutex_exit(&sme_global_mtx);
    801 		DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
    802 		    sme->sme_name));
    803 		goto out;
    804 	}
    805 
    806 	/*
    807 	 * Add the device into the list.
    808 	 */
    809 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    810 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    811 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    812 	mutex_exit(&sme_global_mtx);
    813 
    814 out:
    815 	/*
    816 	 * No errors?  Make an initial data refresh if was requested,
    817 	 * then register the events that were set in the driver.  Do
    818 	 * the refresh first in case it is needed to establish the
    819 	 * limits or max_value needed by some events.
    820 	 */
    821 	if (error == 0) {
    822 		nevent = 0;
    823 
    824 		if (sme->sme_flags & SME_INIT_REFRESH) {
    825 			sysmon_task_queue_sched(0, sme_initial_refresh, sme);
    826 			DPRINTF(("%s: scheduled initial refresh for '%s'\n",
    827 				__func__, sme->sme_name));
    828 		}
    829 		SLIST_FOREACH(evdv, &sme_evdrv_list, evdrv_head) {
    830 			sysmon_task_queue_sched(0,
    831 			    sme_event_drvadd, evdv->evdrv);
    832 			nevent++;
    833 		}
    834 		/*
    835 		 * Hook the sensor into rnd(4) entropy pool if requested
    836 		 */
    837 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    838 			if (edata->flags & ENVSYS_FHAS_ENTROPY) {
    839 				uint32_t rnd_type, rnd_flag = 0;
    840 				size_t n;
    841 				int tail = 1;
    842 
    843 				snprintf(rnd_name, sizeof(rnd_name), "%s-%s",
    844 				    sme->sme_name, edata->desc);
    845 				n = strlen(rnd_name);
    846 				/*
    847 				 * 1) Remove trailing white space(s).
    848 				 * 2) If space exist, replace it with '-'
    849 				 */
    850 				while (--n) {
    851 					if (rnd_name[n] == ' ') {
    852 						if (tail != 0)
    853 							rnd_name[n] = '\0';
    854 						else
    855 							rnd_name[n] = '-';
    856 					} else
    857 						tail = 0;
    858 				}
    859 				rnd_flag |= RND_FLAG_COLLECT_TIME;
    860 				rnd_flag |= RND_FLAG_ESTIMATE_TIME;
    861 
    862 				switch (edata->units) {
    863 				    case ENVSYS_STEMP:
    864 				    case ENVSYS_SFANRPM:
    865 				    case ENVSYS_INTEGER:
    866 					rnd_type = RND_TYPE_ENV;
    867 					rnd_flag |= RND_FLAG_COLLECT_VALUE;
    868 					rnd_flag |= RND_FLAG_ESTIMATE_VALUE;
    869 					break;
    870 				    case ENVSYS_SVOLTS_AC:
    871 				    case ENVSYS_SVOLTS_DC:
    872 				    case ENVSYS_SOHMS:
    873 				    case ENVSYS_SWATTS:
    874 				    case ENVSYS_SAMPS:
    875 				    case ENVSYS_SWATTHOUR:
    876 				    case ENVSYS_SAMPHOUR:
    877 					rnd_type = RND_TYPE_POWER;
    878 					rnd_flag |= RND_FLAG_COLLECT_VALUE;
    879 					rnd_flag |= RND_FLAG_ESTIMATE_VALUE;
    880 					break;
    881 				    default:
    882 					rnd_type = RND_TYPE_UNKNOWN;
    883 					break;
    884 				}
    885 				rnd_attach_source(&edata->rnd_src, rnd_name,
    886 				    rnd_type, rnd_flag);
    887 			}
    888 		}
    889 		DPRINTF(("%s: driver '%s' registered (nsens=%d nevent=%d)\n",
    890 		    __func__, sme->sme_name, sme->sme_nsensors, nevent));
    891 	}
    892 
    893 out2:
    894 	while (!SLIST_EMPTY(&sme_evdrv_list)) {
    895 		evdv = SLIST_FIRST(&sme_evdrv_list);
    896 		SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
    897 		kmem_free(evdv, sizeof(*evdv));
    898 	}
    899 	if (!error)
    900 		return 0;
    901 
    902 	/*
    903 	 * Ugh... something wasn't right; unregister all events and sensors
    904 	 * previously assigned and destroy the array with all its objects.
    905 	 */
    906 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    907 	    sme->sme_name, error));
    908 
    909 	sme_event_unregister_all(sme);
    910 	while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    911 		edata = TAILQ_FIRST(&sme->sme_sensors_list);
    912 		TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    913 	}
    914 	sysmon_envsys_destroy_plist(array);
    915 	return error;
    916 }
    917 
    918 /*
    919  * sysmon_envsys_destroy_plist:
    920  *
    921  * 	+ Remove all objects from the array of dictionaries that is
    922  * 	  created in a sysmon envsys device.
    923  */
    924 static void
    925 sysmon_envsys_destroy_plist(prop_array_t array)
    926 {
    927 	prop_object_iterator_t iter, iter2;
    928 	prop_dictionary_t dict;
    929 	prop_object_t obj;
    930 
    931 	KASSERT(array != NULL);
    932 	KASSERT(prop_object_type(array) == PROP_TYPE_ARRAY);
    933 
    934 	DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
    935 	    prop_array_count(array)));
    936 
    937 	iter = prop_array_iterator(array);
    938 	if (!iter)
    939 		return;
    940 
    941 	while ((dict = prop_object_iterator_next(iter))) {
    942 		KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
    943 		iter2 = prop_dictionary_iterator(dict);
    944 		if (!iter2)
    945 			goto out;
    946 		DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
    947 		while ((obj = prop_object_iterator_next(iter2)) != NULL) {
    948 			DPRINTFOBJ(("%s: obj=%s\n", __func__,
    949 			    prop_dictionary_keysym_cstring_nocopy(obj)));
    950 			prop_dictionary_remove(dict,
    951 			    prop_dictionary_keysym_cstring_nocopy(obj));
    952 			prop_object_iterator_reset(iter2);
    953 		}
    954 		prop_object_iterator_release(iter2);
    955 		DPRINTFOBJ(("%s: objects in dictionary:%d\n",
    956 		    __func__, prop_dictionary_count(dict)));
    957 		prop_object_release(dict);
    958 	}
    959 
    960 out:
    961 	prop_object_iterator_release(iter);
    962 	prop_object_release(array);
    963 }
    964 
    965 /*
    966  * sysmon_envsys_unregister:
    967  *
    968  *	+ Unregister a sysmon envsys device.
    969  */
    970 void
    971 sysmon_envsys_unregister(struct sysmon_envsys *sme)
    972 {
    973 	prop_array_t array;
    974 	struct sysmon_envsys *osme;
    975 	envsys_data_t *edata;
    976 
    977 	KASSERT(sme != NULL);
    978 
    979 	/*
    980 	 * Decrement global sensors counter and the first_sensor index
    981 	 * for remaining devices in the list (only used for compatibility
    982 	 * with previous API), and remove the device from the list.
    983 	 */
    984 	mutex_enter(&sme_global_mtx);
    985 	sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
    986 	LIST_FOREACH(osme, &sysmon_envsys_list, sme_list) {
    987 		if (osme->sme_fsensor >= sme->sme_fsensor)
    988 			osme->sme_fsensor -= sme->sme_nsensors;
    989 	}
    990 	LIST_REMOVE(sme, sme_list);
    991 	mutex_exit(&sme_global_mtx);
    992 
    993 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    994 		sysmon_envsys_sensor_detach(sme, edata);
    995 	}
    996 
    997 	/*
    998 	 * Unregister all events associated with device.
    999 	 */
   1000 	sme_event_unregister_all(sme);
   1001 
   1002 	/*
   1003 	 * Remove the device (and all its objects) from the global dictionary.
   1004 	 */
   1005 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1006 	if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
   1007 		mutex_enter(&sme_global_mtx);
   1008 		prop_dictionary_remove(sme_propd, sme->sme_name);
   1009 		mutex_exit(&sme_global_mtx);
   1010 		sysmon_envsys_destroy_plist(array);
   1011 	}
   1012 	/*
   1013 	 * And finally destroy the sysmon_envsys object.
   1014 	 */
   1015 	sysmon_envsys_destroy(sme);
   1016 }
   1017 
   1018 /*
   1019  * sysmon_envsys_find:
   1020  *
   1021  *	+ Find a sysmon envsys device and mark it as busy
   1022  *	  once it's available.
   1023  */
   1024 struct sysmon_envsys *
   1025 sysmon_envsys_find(const char *name)
   1026 {
   1027 	struct sysmon_envsys *sme;
   1028 
   1029 	mutex_enter(&sme_global_mtx);
   1030 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   1031 		if (strcmp(sme->sme_name, name) == 0) {
   1032 			sysmon_envsys_acquire(sme, false);
   1033 			break;
   1034 		}
   1035 	}
   1036 	mutex_exit(&sme_global_mtx);
   1037 
   1038 	return sme;
   1039 }
   1040 
   1041 /*
   1042  * Compatibility function with the old API.
   1043  */
   1044 struct sysmon_envsys *
   1045 sysmon_envsys_find_40(u_int idx)
   1046 {
   1047 	struct sysmon_envsys *sme;
   1048 
   1049 	mutex_enter(&sme_global_mtx);
   1050 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   1051 		if (idx >= sme->sme_fsensor &&
   1052 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors)) {
   1053 			sysmon_envsys_acquire(sme, false);
   1054 			break;
   1055 		}
   1056 	}
   1057 	mutex_exit(&sme_global_mtx);
   1058 
   1059 	return sme;
   1060 }
   1061 
   1062 /*
   1063  * sysmon_envsys_acquire:
   1064  *
   1065  * 	+ Wait until a sysmon envsys device is available and mark
   1066  * 	  it as busy.
   1067  */
   1068 void
   1069 sysmon_envsys_acquire(struct sysmon_envsys *sme, bool locked)
   1070 {
   1071 	KASSERT(sme != NULL);
   1072 
   1073 	if (locked) {
   1074 		while (sme->sme_flags & SME_FLAG_BUSY)
   1075 			cv_wait(&sme->sme_condvar, &sme->sme_mtx);
   1076 		sme->sme_flags |= SME_FLAG_BUSY;
   1077 	} else {
   1078 		mutex_enter(&sme->sme_mtx);
   1079 		while (sme->sme_flags & SME_FLAG_BUSY)
   1080 			cv_wait(&sme->sme_condvar, &sme->sme_mtx);
   1081 		sme->sme_flags |= SME_FLAG_BUSY;
   1082 		mutex_exit(&sme->sme_mtx);
   1083 	}
   1084 }
   1085 
   1086 /*
   1087  * sysmon_envsys_release:
   1088  *
   1089  * 	+ Unmark a sysmon envsys device as busy, and notify
   1090  * 	  waiters.
   1091  */
   1092 void
   1093 sysmon_envsys_release(struct sysmon_envsys *sme, bool locked)
   1094 {
   1095 	KASSERT(sme != NULL);
   1096 
   1097 	if (locked) {
   1098 		sme->sme_flags &= ~SME_FLAG_BUSY;
   1099 		cv_broadcast(&sme->sme_condvar);
   1100 	} else {
   1101 		mutex_enter(&sme->sme_mtx);
   1102 		sme->sme_flags &= ~SME_FLAG_BUSY;
   1103 		cv_broadcast(&sme->sme_condvar);
   1104 		mutex_exit(&sme->sme_mtx);
   1105 	}
   1106 }
   1107 
   1108 /*
   1109  * sme_initial_refresh:
   1110  *
   1111  * 	+ Do an initial refresh of the sensors in a device just after
   1112  * 	  interrupts are enabled in the autoconf(9) process.
   1113  *
   1114  */
   1115 static void
   1116 sme_initial_refresh(void *arg)
   1117 {
   1118 	struct sysmon_envsys *sme = arg;
   1119 	envsys_data_t *edata;
   1120 
   1121 	mutex_enter(&sme->sme_mtx);
   1122 	sysmon_envsys_acquire(sme, true);
   1123 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head)
   1124 		sysmon_envsys_refresh_sensor(sme, edata);
   1125 	sysmon_envsys_release(sme, true);
   1126 	mutex_exit(&sme->sme_mtx);
   1127 }
   1128 
   1129 /*
   1130  * sme_sensor_dictionary_get:
   1131  *
   1132  * 	+ Returns a dictionary of a device specified by its index
   1133  * 	  position.
   1134  */
   1135 prop_dictionary_t
   1136 sme_sensor_dictionary_get(prop_array_t array, const char *index)
   1137 {
   1138 	prop_object_iterator_t iter;
   1139 	prop_dictionary_t dict;
   1140 	prop_object_t obj;
   1141 
   1142 	KASSERT(array != NULL || index != NULL);
   1143 
   1144 	iter = prop_array_iterator(array);
   1145 	if (!iter)
   1146 		return NULL;
   1147 
   1148 	while ((dict = prop_object_iterator_next(iter))) {
   1149 		obj = prop_dictionary_get(dict, "index");
   1150 		if (prop_string_equals_cstring(obj, index))
   1151 			break;
   1152 	}
   1153 
   1154 	prop_object_iterator_release(iter);
   1155 	return dict;
   1156 }
   1157 
   1158 /*
   1159  * sme_remove_userprops:
   1160  *
   1161  * 	+ Remove all properties from all devices that were set by
   1162  * 	  the ENVSYS_SETDICTIONARY ioctl.
   1163  */
   1164 static void
   1165 sme_remove_userprops(void)
   1166 {
   1167 	struct sysmon_envsys *sme;
   1168 	prop_array_t array;
   1169 	prop_dictionary_t sdict;
   1170 	envsys_data_t *edata = NULL;
   1171 	char tmp[ENVSYS_DESCLEN];
   1172 	char rnd_name[sizeof(edata->rnd_src.name)];
   1173 	sysmon_envsys_lim_t lims;
   1174 	const struct sme_descr_entry *sdt_units;
   1175 	uint32_t props;
   1176 	int ptype;
   1177 
   1178 	mutex_enter(&sme_global_mtx);
   1179 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   1180 		sysmon_envsys_acquire(sme, false);
   1181 		array = prop_dictionary_get(sme_propd, sme->sme_name);
   1182 
   1183 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1184 			(void)snprintf(tmp, sizeof(tmp), "sensor%d",
   1185 				       edata->sensor);
   1186 			sdict = sme_sensor_dictionary_get(array, tmp);
   1187 			KASSERT(sdict != NULL);
   1188 
   1189 			ptype = 0;
   1190 			if (edata->upropset & PROP_BATTCAP) {
   1191 				prop_dictionary_remove(sdict,
   1192 				    "critical-capacity");
   1193 				ptype = PENVSYS_EVENT_CAPACITY;
   1194 			}
   1195 
   1196 			if (edata->upropset & PROP_BATTWARN) {
   1197 				prop_dictionary_remove(sdict,
   1198 				    "warning-capacity");
   1199 				ptype = PENVSYS_EVENT_CAPACITY;
   1200 			}
   1201 
   1202 			if (edata->upropset & PROP_BATTHIGH) {
   1203 				prop_dictionary_remove(sdict,
   1204 				    "high-capacity");
   1205 				ptype = PENVSYS_EVENT_CAPACITY;
   1206 			}
   1207 
   1208 			if (edata->upropset & PROP_BATTMAX) {
   1209 				prop_dictionary_remove(sdict,
   1210 				    "maximum-capacity");
   1211 				ptype = PENVSYS_EVENT_CAPACITY;
   1212 			}
   1213 			if (edata->upropset & PROP_WARNMAX) {
   1214 				prop_dictionary_remove(sdict, "warning-max");
   1215 				ptype = PENVSYS_EVENT_LIMITS;
   1216 			}
   1217 
   1218 			if (edata->upropset & PROP_WARNMIN) {
   1219 				prop_dictionary_remove(sdict, "warning-min");
   1220 				ptype = PENVSYS_EVENT_LIMITS;
   1221 			}
   1222 
   1223 			if (edata->upropset & PROP_CRITMAX) {
   1224 				prop_dictionary_remove(sdict, "critical-max");
   1225 				ptype = PENVSYS_EVENT_LIMITS;
   1226 			}
   1227 
   1228 			if (edata->upropset & PROP_CRITMIN) {
   1229 				prop_dictionary_remove(sdict, "critical-min");
   1230 				ptype = PENVSYS_EVENT_LIMITS;
   1231 			}
   1232 			if (edata->upropset & PROP_RFACT) {
   1233 				(void)sme_sensor_upint32(sdict, "rfact", 0);
   1234 				edata->rfact = 0;
   1235 			}
   1236 
   1237 			if (edata->upropset & PROP_DESC)
   1238 				(void)sme_sensor_upstring(sdict,
   1239 			  	    "description", edata->desc);
   1240 
   1241 			if (ptype == 0)
   1242 				continue;
   1243 
   1244 			/*
   1245 			 * If there were any limit values removed, we
   1246 			 * need to revert to initial limits.
   1247 			 *
   1248 			 * First, tell the driver that we need it to
   1249 			 * restore any h/w limits which may have been
   1250 			 * changed to stored, boot-time values.
   1251 			 */
   1252 			if (sme->sme_set_limits) {
   1253 				DPRINTF(("%s: reset limits for %s %s\n",
   1254 					__func__, sme->sme_name, edata->desc));
   1255 				(*sme->sme_set_limits)(sme, edata, NULL, NULL);
   1256 			}
   1257 
   1258 			/*
   1259 			 * Next, we need to retrieve those initial limits.
   1260 			 */
   1261 			props = 0;
   1262 			edata->upropset &= ~PROP_LIMITS;
   1263 			if (sme->sme_get_limits) {
   1264 				DPRINTF(("%s: retrieve limits for %s %s\n",
   1265 					__func__, sme->sme_name, edata->desc));
   1266 				lims = edata->limits;
   1267 				(*sme->sme_get_limits)(sme, edata, &lims,
   1268 						       &props);
   1269 			}
   1270 
   1271 			/*
   1272 			 * Finally, remove any old limits event, then
   1273 			 * install a new event (which will update the
   1274 			 * dictionary)
   1275 			 */
   1276 			sme_event_unregister(sme, edata->desc,
   1277 			    PENVSYS_EVENT_LIMITS);
   1278 
   1279 			/*
   1280 			 * Find the correct units for this sensor.
   1281 			 */
   1282 			sdt_units = sme_find_table_entry(SME_DESC_UNITS,
   1283 			    edata->units);
   1284 
   1285 			if (props & PROP_LIMITS) {
   1286 				DPRINTF(("%s: install limits for %s %s\n",
   1287 					__func__, sme->sme_name, edata->desc));
   1288 
   1289 				sme_event_register(sdict, edata, sme,
   1290 				    &lims, props, PENVSYS_EVENT_LIMITS,
   1291 				    sdt_units->crittype);
   1292 			}
   1293 			if (edata->flags & ENVSYS_FHAS_ENTROPY) {
   1294 				sme_event_register(sdict, edata, sme,
   1295 				    &lims, props, PENVSYS_EVENT_NULL,
   1296 				    sdt_units->crittype);
   1297 				snprintf(rnd_name, sizeof(rnd_name), "%s-%s",
   1298 				    sme->sme_name, edata->desc);
   1299 				rnd_attach_source(&edata->rnd_src, rnd_name,
   1300 				    RND_TYPE_ENV, RND_FLAG_COLLECT_VALUE|
   1301 						  RND_FLAG_COLLECT_TIME|
   1302 						  RND_FLAG_ESTIMATE_VALUE|
   1303 						  RND_FLAG_ESTIMATE_TIME);
   1304 			}
   1305 		}
   1306 
   1307 		/*
   1308 		 * Restore default timeout value.
   1309 		 */
   1310 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1311 		sme_schedule_callout(sme);
   1312 		sysmon_envsys_release(sme, false);
   1313 	}
   1314 	mutex_exit(&sme_global_mtx);
   1315 }
   1316 
   1317 /*
   1318  * sme_add_property_dictionary:
   1319  *
   1320  * 	+ Add global properties into a device.
   1321  */
   1322 static int
   1323 sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1324 			    prop_dictionary_t dict)
   1325 {
   1326 	prop_dictionary_t pdict;
   1327 	const char *class;
   1328 	int error = 0;
   1329 
   1330 	pdict = prop_dictionary_create();
   1331 	if (!pdict)
   1332 		return EINVAL;
   1333 
   1334 	/*
   1335 	 * Add the 'refresh-timeout' and 'dev-class' objects into the
   1336 	 * 'device-properties' dictionary.
   1337 	 *
   1338 	 * 	...
   1339 	 * 	<dict>
   1340 	 * 		<key>device-properties</key>
   1341 	 * 		<dict>
   1342 	 * 			<key>refresh-timeout</key>
   1343 	 * 			<integer>120</integer<
   1344 	 *			<key>device-class</key>
   1345 	 *			<string>class_name</string>
   1346 	 * 		</dict>
   1347 	 * 	</dict>
   1348 	 * 	...
   1349 	 *
   1350 	 */
   1351 	if (sme->sme_events_timeout == 0) {
   1352 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1353 		sme_schedule_callout(sme);
   1354 	}
   1355 
   1356 	if (!prop_dictionary_set_uint64(pdict, "refresh-timeout",
   1357 					sme->sme_events_timeout)) {
   1358 		error = EINVAL;
   1359 		goto out;
   1360 	}
   1361 	if (sme->sme_class == SME_CLASS_BATTERY)
   1362 		class = "battery";
   1363 	else if (sme->sme_class == SME_CLASS_ACADAPTER)
   1364 		class = "ac-adapter";
   1365 	else
   1366 		class = "other";
   1367 	if (!prop_dictionary_set_cstring_nocopy(pdict, "device-class", class)) {
   1368 		error = EINVAL;
   1369 		goto out;
   1370 	}
   1371 
   1372 	if (!prop_dictionary_set(dict, "device-properties", pdict)) {
   1373 		error = EINVAL;
   1374 		goto out;
   1375 	}
   1376 
   1377 	/*
   1378 	 * Add the device dictionary into the sysmon envsys array.
   1379 	 */
   1380 	if (!prop_array_add(array, dict))
   1381 		error = EINVAL;
   1382 
   1383 out:
   1384 	prop_object_release(pdict);
   1385 	return error;
   1386 }
   1387 
   1388 /*
   1389  * sme_add_sensor_dictionary:
   1390  *
   1391  * 	+ Adds the sensor objects into the dictionary and returns a pointer
   1392  * 	  to a sme_event_drv_t object if a monitoring flag was set
   1393  * 	  (or NULL otherwise).
   1394  */
   1395 static sme_event_drv_t *
   1396 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1397 		    	  prop_dictionary_t dict, envsys_data_t *edata)
   1398 {
   1399 	const struct sme_descr_entry *sdt;
   1400 	int error;
   1401 	sme_event_drv_t *sme_evdrv_t = NULL;
   1402 	char indexstr[ENVSYS_DESCLEN];
   1403 	bool mon_supported, allow_rfact;
   1404 
   1405 	/*
   1406 	 * Add the index sensor string.
   1407 	 *
   1408 	 * 		...
   1409 	 * 		<key>index</eyr
   1410 	 * 		<string>sensor0</string>
   1411 	 * 		...
   1412 	 */
   1413 	(void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
   1414 	if (sme_sensor_upstring(dict, "index", indexstr))
   1415 		goto bad;
   1416 
   1417 	/*
   1418 	 * 		...
   1419 	 * 		<key>description</key>
   1420 	 * 		<string>blah blah</string>
   1421 	 * 		...
   1422 	 */
   1423 	if (sme_sensor_upstring(dict, "description", edata->desc))
   1424 		goto bad;
   1425 
   1426 	/*
   1427 	 * Add the monitoring boolean object:
   1428 	 *
   1429 	 * 		...
   1430 	 * 		<key>monitoring-supported</key>
   1431 	 * 		<true/>
   1432 	 *		...
   1433 	 *
   1434 	 * always false on Battery {capacity,charge}, Drive and Indicator types.
   1435 	 * They cannot be monitored.
   1436 	 *
   1437 	 */
   1438 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1439 	    (edata->units == ENVSYS_INDICATOR) ||
   1440 	    (edata->units == ENVSYS_DRIVE) ||
   1441 	    (edata->units == ENVSYS_BATTERY_CAPACITY) ||
   1442 	    (edata->units == ENVSYS_BATTERY_CHARGE))
   1443 		mon_supported = false;
   1444 	else
   1445 		mon_supported = true;
   1446 	if (sme_sensor_upbool(dict, "monitoring-supported", mon_supported))
   1447 		goto out;
   1448 
   1449 	/*
   1450 	 * Add the allow-rfact boolean object, true if
   1451 	 * ENVSYS_FCHANGERFACT is set, false otherwise.
   1452 	 *
   1453 	 * 		...
   1454 	 * 		<key>allow-rfact</key>
   1455 	 * 		<true/>
   1456 	 * 		...
   1457 	 */
   1458 	if (edata->units == ENVSYS_SVOLTS_DC ||
   1459 	    edata->units == ENVSYS_SVOLTS_AC) {
   1460 		if (edata->flags & ENVSYS_FCHANGERFACT)
   1461 			allow_rfact = true;
   1462 		else
   1463 			allow_rfact = false;
   1464 		if (sme_sensor_upbool(dict, "allow-rfact", allow_rfact))
   1465 			goto out;
   1466 	}
   1467 
   1468 	error = sme_update_sensor_dictionary(dict, edata,
   1469 			(edata->state == ENVSYS_SVALID));
   1470 	if (error < 0)
   1471 		goto bad;
   1472 	else if (error)
   1473 		goto out;
   1474 
   1475 	/*
   1476 	 * 	...
   1477 	 * </dict>
   1478 	 *
   1479 	 * Add the dictionary into the array.
   1480 	 *
   1481 	 */
   1482 	if (!prop_array_add(array, dict)) {
   1483 		DPRINTF(("%s: prop_array_add\n", __func__));
   1484 		goto bad;
   1485 	}
   1486 
   1487 	/*
   1488 	 * Register new event(s) if any monitoring flag was set or if
   1489 	 * the sensor provides entropy for rnd(4).
   1490 	 */
   1491 	if (edata->flags & (ENVSYS_FMONANY | ENVSYS_FHAS_ENTROPY)) {
   1492 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
   1493 		sme_evdrv_t->sed_sdict = dict;
   1494 		sme_evdrv_t->sed_edata = edata;
   1495 		sme_evdrv_t->sed_sme = sme;
   1496 		sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
   1497 		sme_evdrv_t->sed_powertype = sdt->crittype;
   1498 	}
   1499 
   1500 out:
   1501 	return sme_evdrv_t;
   1502 
   1503 bad:
   1504 	prop_object_release(dict);
   1505 	return NULL;
   1506 }
   1507 
   1508 /*
   1509  * Find the maximum of all currently reported values.
   1510  * The provided callback decides whether a sensor is part of the
   1511  * maximum calculation (by returning true) or ignored (callback
   1512  * returns false). Example usage: callback selects temperature
   1513  * sensors in a given thermal zone, the function calculates the
   1514  * maximum currently reported temperature in this zone.
   1515  * If the parameter "refresh" is true, new values will be aquired
   1516  * from the hardware, if not, the last reported value will be used.
   1517  */
   1518 uint32_t
   1519 sysmon_envsys_get_max_value(bool (*predicate)(const envsys_data_t*),
   1520 	bool refresh)
   1521 {
   1522 	struct sysmon_envsys *sme;
   1523 	uint32_t maxv, v;
   1524 
   1525 	maxv = 0;
   1526 	mutex_enter(&sme_global_mtx);
   1527 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   1528 		sysmon_envsys_acquire(sme, false);
   1529 		v = sme_get_max_value(sme, predicate, refresh);
   1530 		sysmon_envsys_release(sme, false);
   1531 		if (v > maxv)
   1532 			maxv = v;
   1533 	}
   1534 	mutex_exit(&sme_global_mtx);
   1535 	return maxv;
   1536 }
   1537 
   1538 static uint32_t
   1539 sme_get_max_value(struct sysmon_envsys *sme,
   1540     bool (*predicate)(const envsys_data_t*),
   1541     bool refresh)
   1542 {
   1543 	envsys_data_t *edata;
   1544 	uint32_t maxv, v;
   1545 
   1546 	/*
   1547 	 * Iterate over all sensors that match the predicate
   1548 	 */
   1549 	maxv = 0;
   1550 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1551 		if (!(*predicate)(edata))
   1552 			continue;
   1553 
   1554 		/*
   1555 		 * refresh sensor data
   1556 		 */
   1557 		mutex_enter(&sme->sme_mtx);
   1558 		sysmon_envsys_refresh_sensor(sme, edata);
   1559 		mutex_exit(&sme->sme_mtx);
   1560 
   1561 		v = edata->value_cur;
   1562 		if (v > maxv)
   1563 			maxv = v;
   1564 
   1565 	}
   1566 
   1567 	return maxv;
   1568 }
   1569 
   1570 /*
   1571  * sme_update_dictionary:
   1572  *
   1573  * 	+ Update per-sensor dictionaries with new values if there were
   1574  * 	  changes, otherwise the object in dictionary is untouched.
   1575  */
   1576 int
   1577 sme_update_dictionary(struct sysmon_envsys *sme)
   1578 {
   1579 	envsys_data_t *edata;
   1580 	prop_object_t array, dict, obj, obj2;
   1581 	int error = 0;
   1582 
   1583 	/*
   1584 	 * Retrieve the array of dictionaries in device.
   1585 	 */
   1586 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1587 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
   1588 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
   1589 		return EINVAL;
   1590 	}
   1591 
   1592 	/*
   1593 	 * Get the last dictionary on the array, this contains the
   1594 	 * 'device-properties' sub-dictionary.
   1595 	 */
   1596 	obj = prop_array_get(array, prop_array_count(array) - 1);
   1597 	if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) {
   1598 		DPRINTF(("%s: not a device-properties dictionary\n", __func__));
   1599 		return EINVAL;
   1600 	}
   1601 
   1602 	obj2 = prop_dictionary_get(obj, "device-properties");
   1603 	if (!obj2)
   1604 		return EINVAL;
   1605 
   1606 	/*
   1607 	 * Update the 'refresh-timeout' property.
   1608 	 */
   1609 	if (!prop_dictionary_set_uint64(obj2, "refresh-timeout",
   1610 					sme->sme_events_timeout))
   1611 		return EINVAL;
   1612 
   1613 	/*
   1614 	 * - iterate over all sensors.
   1615 	 * - fetch new data.
   1616 	 * - check if data in dictionary is different than new data.
   1617 	 * - update dictionary if there were changes.
   1618 	 */
   1619 	DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
   1620 	    sme->sme_name, sme->sme_nsensors));
   1621 
   1622 	/*
   1623 	 * Don't bother with locking when traversing the queue,
   1624 	 * the device is already marked as busy; if a sensor
   1625 	 * is going to be removed or added it will have to wait.
   1626 	 */
   1627 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1628 		/*
   1629 		 * refresh sensor data via sme_envsys_refresh_sensor
   1630 		 */
   1631 		mutex_enter(&sme->sme_mtx);
   1632 		sysmon_envsys_refresh_sensor(sme, edata);
   1633 		mutex_exit(&sme->sme_mtx);
   1634 
   1635 		/*
   1636 		 * retrieve sensor's dictionary.
   1637 		 */
   1638 		dict = prop_array_get(array, edata->sensor);
   1639 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1640 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1641 			    __func__, edata->sensor, sme->sme_name));
   1642 			return EINVAL;
   1643 		}
   1644 
   1645 		/*
   1646 		 * update sensor's state.
   1647 		 */
   1648 		error = sme_update_sensor_dictionary(dict, edata, true);
   1649 
   1650 		if (error)
   1651 			break;
   1652 	}
   1653 
   1654 	return error;
   1655 }
   1656 
   1657 int
   1658 sme_update_sensor_dictionary(prop_object_t dict, envsys_data_t *edata,
   1659 	bool value_update)
   1660 {
   1661 	const struct sme_descr_entry *sdt;
   1662 	int error = 0;
   1663 
   1664 	sdt = sme_find_table_entry(SME_DESC_STATES, edata->state);
   1665 	if (sdt == NULL) {
   1666 		printf("sme_update_sensor_dictionary: cannot update sensor %d "
   1667 		    "state %d unknown\n", edata->sensor, edata->state);
   1668 		return EINVAL;
   1669 	}
   1670 
   1671 	DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", __func__,
   1672 	    edata->sensor, sdt->type, sdt->desc, edata->flags));
   1673 
   1674 	error = sme_sensor_upstring(dict, "state", sdt->desc);
   1675 	if (error)
   1676 		return (-error);
   1677 
   1678 	/*
   1679 	 * update sensor's type.
   1680 	 */
   1681 	sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
   1682 
   1683 	DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", __func__, edata->sensor,
   1684 	    sdt->type, sdt->desc));
   1685 
   1686 	error = sme_sensor_upstring(dict, "type", sdt->desc);
   1687 	if (error)
   1688 		return (-error);
   1689 
   1690 	if (value_update) {
   1691 		/*
   1692 		 * update sensor's current value.
   1693 		 */
   1694 		error = sme_sensor_upint32(dict, "cur-value", edata->value_cur);
   1695 		if (error)
   1696 			return error;
   1697 	}
   1698 
   1699 	/*
   1700 	 * Battery charge and Indicator types do not
   1701 	 * need the remaining objects, so skip them.
   1702 	 */
   1703 	if (edata->units == ENVSYS_INDICATOR ||
   1704 	    edata->units == ENVSYS_BATTERY_CHARGE)
   1705 		return error;
   1706 
   1707 	/*
   1708 	 * update sensor flags.
   1709 	 */
   1710 	if (edata->flags & ENVSYS_FPERCENT) {
   1711 		error = sme_sensor_upbool(dict, "want-percentage", true);
   1712 		if (error)
   1713 			return error;
   1714 	}
   1715 
   1716 	if (value_update) {
   1717 		/*
   1718 		 * update sensor's {max,min}-value.
   1719 		 */
   1720 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1721 			error = sme_sensor_upint32(dict, "max-value",
   1722 						   edata->value_max);
   1723 			if (error)
   1724 				return error;
   1725 		}
   1726 
   1727 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1728 			error = sme_sensor_upint32(dict, "min-value",
   1729 						   edata->value_min);
   1730 			if (error)
   1731 				return error;
   1732 		}
   1733 
   1734 		/*
   1735 		 * update 'rpms' only for ENVSYS_SFANRPM sensors.
   1736 		 */
   1737 		if (edata->units == ENVSYS_SFANRPM) {
   1738 			error = sme_sensor_upuint32(dict, "rpms", edata->rpms);
   1739 			if (error)
   1740 				return error;
   1741 		}
   1742 
   1743 		/*
   1744 		 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors.
   1745 		 */
   1746 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1747 		    edata->units == ENVSYS_SVOLTS_DC) {
   1748 			error = sme_sensor_upint32(dict, "rfact", edata->rfact);
   1749 			if (error)
   1750 				return error;
   1751 		}
   1752 	}
   1753 
   1754 	/*
   1755 	 * update 'drive-state' only for ENVSYS_DRIVE sensors.
   1756 	 */
   1757 	if (edata->units == ENVSYS_DRIVE) {
   1758 		sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
   1759 					   edata->value_cur);
   1760 		error = sme_sensor_upstring(dict, "drive-state", sdt->desc);
   1761 		if (error)
   1762 			return error;
   1763 	}
   1764 
   1765 	/*
   1766 	 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY
   1767 	 * sensors.
   1768 	 */
   1769 	if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1770 		sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
   1771 		    edata->value_cur);
   1772 		error = sme_sensor_upstring(dict, "battery-capacity",
   1773 					    sdt->desc);
   1774 		if (error)
   1775 			return error;
   1776 	}
   1777 
   1778 	return error;
   1779 }
   1780 
   1781 /*
   1782  * sme_userset_dictionary:
   1783  *
   1784  * 	+ Parse the userland dictionary and run the appropiate tasks
   1785  * 	  that were specified.
   1786  */
   1787 int
   1788 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1789 		       prop_array_t array)
   1790 {
   1791 	const struct sme_descr_entry *sdt;
   1792 	envsys_data_t *edata;
   1793 	prop_dictionary_t dict, tdict = NULL;
   1794 	prop_object_t obj, obj1, obj2, tobj = NULL;
   1795 	uint32_t props;
   1796 	uint64_t refresh_timo = 0;
   1797 	sysmon_envsys_lim_t lims;
   1798 	int i, error = 0;
   1799 	const char *blah;
   1800 	bool targetfound = false;
   1801 
   1802 	/*
   1803 	 * The user wanted to change the refresh timeout value for this
   1804 	 * device.
   1805 	 *
   1806 	 * Get the 'device-properties' object from the userland dictionary.
   1807 	 */
   1808 	obj = prop_dictionary_get(udict, "device-properties");
   1809 	if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) {
   1810 		/*
   1811 		 * Get the 'refresh-timeout' property for this device.
   1812 		 */
   1813 		obj1 = prop_dictionary_get(obj, "refresh-timeout");
   1814 		if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) {
   1815 			targetfound = true;
   1816 			refresh_timo =
   1817 			    prop_number_unsigned_integer_value(obj1);
   1818 			if (refresh_timo < 1)
   1819 				error = EINVAL;
   1820 			else {
   1821 				mutex_enter(&sme->sme_mtx);
   1822 				if (sme->sme_events_timeout != refresh_timo) {
   1823 					sme->sme_events_timeout = refresh_timo;
   1824 					sme_schedule_callout(sme);
   1825 				}
   1826 				mutex_exit(&sme->sme_mtx);
   1827 		}
   1828 		}
   1829 		return error;
   1830 
   1831 	} else if (!obj) {
   1832 		/*
   1833 		 * Get sensor's index from userland dictionary.
   1834 		 */
   1835 		obj = prop_dictionary_get(udict, "index");
   1836 		if (!obj)
   1837 			return EINVAL;
   1838 		if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1839 			DPRINTF(("%s: 'index' not a string\n", __func__));
   1840 			return EINVAL;
   1841 		}
   1842 	} else
   1843 		return EINVAL;
   1844 
   1845 	/*
   1846 	 * Don't bother with locking when traversing the queue,
   1847 	 * the device is already marked as busy; if a sensor
   1848 	 * is going to be removed or added it will have to wait.
   1849 	 */
   1850 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1851 		/*
   1852 		 * Get a dictionary and check if it's our sensor by checking
   1853 		 * at its index position.
   1854 		 */
   1855 		dict = prop_array_get(array, edata->sensor);
   1856 		obj1 = prop_dictionary_get(dict, "index");
   1857 
   1858 		/*
   1859 		 * is it our sensor?
   1860 		 */
   1861 		if (!prop_string_equals(obj1, obj))
   1862 			continue;
   1863 
   1864 		props = 0;
   1865 
   1866 		/*
   1867 		 * Check if a new description operation was
   1868 		 * requested by the user and set new description.
   1869 		 */
   1870 		obj2 = prop_dictionary_get(udict, "description");
   1871 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) {
   1872 			targetfound = true;
   1873 			blah = prop_string_cstring_nocopy(obj2);
   1874 
   1875 			/*
   1876 			 * Check for duplicate description.
   1877 			 */
   1878 			for (i = 0; i < sme->sme_nsensors; i++) {
   1879 				if (i == edata->sensor)
   1880 					continue;
   1881 				tdict = prop_array_get(array, i);
   1882 				tobj =
   1883 				    prop_dictionary_get(tdict, "description");
   1884 				if (prop_string_equals(obj2, tobj)) {
   1885 					error = EEXIST;
   1886 					goto out;
   1887 				}
   1888 			}
   1889 
   1890 			/*
   1891 			 * Update the object in dictionary.
   1892 			 */
   1893 			mutex_enter(&sme->sme_mtx);
   1894 			error = sme_sensor_upstring(dict,
   1895 						    "description",
   1896 						    blah);
   1897 			if (error) {
   1898 				mutex_exit(&sme->sme_mtx);
   1899 				goto out;
   1900 			}
   1901 
   1902 			DPRINTF(("%s: sensor%d changed desc to: %s\n",
   1903 			    __func__, edata->sensor, blah));
   1904 			edata->upropset |= PROP_DESC;
   1905 			mutex_exit(&sme->sme_mtx);
   1906 		}
   1907 
   1908 		/*
   1909 		 * did the user want to change the rfact?
   1910 		 */
   1911 		obj2 = prop_dictionary_get(udict, "rfact");
   1912 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1913 			targetfound = true;
   1914 			if (edata->flags & ENVSYS_FCHANGERFACT) {
   1915 				mutex_enter(&sme->sme_mtx);
   1916 				edata->rfact = prop_number_integer_value(obj2);
   1917 				edata->upropset |= PROP_RFACT;
   1918 				mutex_exit(&sme->sme_mtx);
   1919 				DPRINTF(("%s: sensor%d changed rfact to %d\n",
   1920 				    __func__, edata->sensor, edata->rfact));
   1921 			} else {
   1922 				error = ENOTSUP;
   1923 				goto out;
   1924 			}
   1925 		}
   1926 
   1927 		sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
   1928 
   1929 		/*
   1930 		 * did the user want to set a critical capacity event?
   1931 		 */
   1932 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1933 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1934 			targetfound = true;
   1935 			lims.sel_critmin = prop_number_integer_value(obj2);
   1936 			props |= PROP_BATTCAP;
   1937 		}
   1938 
   1939 		/*
   1940 		 * did the user want to set a warning capacity event?
   1941 		 */
   1942 		obj2 = prop_dictionary_get(udict, "warning-capacity");
   1943 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1944 			targetfound = true;
   1945 			lims.sel_warnmin = prop_number_integer_value(obj2);
   1946 			props |= PROP_BATTWARN;
   1947 		}
   1948 
   1949 		/*
   1950 		 * did the user want to set a high capacity event?
   1951 		 */
   1952 		obj2 = prop_dictionary_get(udict, "high-capacity");
   1953 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1954 			targetfound = true;
   1955 			lims.sel_warnmin = prop_number_integer_value(obj2);
   1956 			props |= PROP_BATTHIGH;
   1957 		}
   1958 
   1959 		/*
   1960 		 * did the user want to set a maximum capacity event?
   1961 		 */
   1962 		obj2 = prop_dictionary_get(udict, "maximum-capacity");
   1963 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1964 			targetfound = true;
   1965 			lims.sel_warnmin = prop_number_integer_value(obj2);
   1966 			props |= PROP_BATTMAX;
   1967 		}
   1968 
   1969 		/*
   1970 		 * did the user want to set a critical max event?
   1971 		 */
   1972 		obj2 = prop_dictionary_get(udict, "critical-max");
   1973 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1974 			targetfound = true;
   1975 			lims.sel_critmax = prop_number_integer_value(obj2);
   1976 			props |= PROP_CRITMAX;
   1977 		}
   1978 
   1979 		/*
   1980 		 * did the user want to set a warning max event?
   1981 		 */
   1982 		obj2 = prop_dictionary_get(udict, "warning-max");
   1983 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1984 			targetfound = true;
   1985 			lims.sel_warnmax = prop_number_integer_value(obj2);
   1986 			props |= PROP_WARNMAX;
   1987 		}
   1988 
   1989 		/*
   1990 		 * did the user want to set a critical min event?
   1991 		 */
   1992 		obj2 = prop_dictionary_get(udict, "critical-min");
   1993 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1994 			targetfound = true;
   1995 			lims.sel_critmin = prop_number_integer_value(obj2);
   1996 			props |= PROP_CRITMIN;
   1997 		}
   1998 
   1999 		/*
   2000 		 * did the user want to set a warning min event?
   2001 		 */
   2002 		obj2 = prop_dictionary_get(udict, "warning-min");
   2003 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   2004 			targetfound = true;
   2005 			lims.sel_warnmin = prop_number_integer_value(obj2);
   2006 			props |= PROP_WARNMIN;
   2007 		}
   2008 
   2009 		if (props && (edata->flags & ENVSYS_FMONNOTSUPP) != 0) {
   2010 			error = ENOTSUP;
   2011 			goto out;
   2012 		}
   2013 		if (props || (edata->flags & ENVSYS_FHAS_ENTROPY) != 0) {
   2014 			error = sme_event_register(dict, edata, sme, &lims,
   2015 					props,
   2016 					(edata->flags & ENVSYS_FPERCENT)?
   2017 						PENVSYS_EVENT_CAPACITY:
   2018 						PENVSYS_EVENT_LIMITS,
   2019 					sdt->crittype);
   2020 			if (error == EEXIST)
   2021 				error = 0;
   2022 			if (error)
   2023 				goto out;
   2024 		}
   2025 
   2026 		/*
   2027 		 * All objects in dictionary were processed.
   2028 		 */
   2029 		break;
   2030 	}
   2031 
   2032 out:
   2033 	/*
   2034 	 * invalid target? return the error.
   2035 	 */
   2036 	if (!targetfound)
   2037 		error = EINVAL;
   2038 
   2039 	return error;
   2040 }
   2041 
   2042 /*
   2043  * + sysmon_envsys_foreach_sensor
   2044  *
   2045  *	Walk through the devices' sensor lists and execute the callback.
   2046  *	If the callback returns false, the remainder of the current
   2047  *	device's sensors are skipped.
   2048  */
   2049 void
   2050 sysmon_envsys_foreach_sensor(sysmon_envsys_callback_t func, void *arg,
   2051 			     bool refresh)
   2052 {
   2053 	struct sysmon_envsys *sme;
   2054 	envsys_data_t *sensor;
   2055 
   2056 	mutex_enter(&sme_global_mtx);
   2057 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   2058 
   2059 		sysmon_envsys_acquire(sme, false);
   2060 		TAILQ_FOREACH(sensor, &sme->sme_sensors_list, sensors_head) {
   2061 			if (refresh) {
   2062 				mutex_enter(&sme->sme_mtx);
   2063 				sysmon_envsys_refresh_sensor(sme, sensor);
   2064 				mutex_exit(&sme->sme_mtx);
   2065 			}
   2066 			if (!(*func)(sme, sensor, arg))
   2067 				break;
   2068 		}
   2069 		sysmon_envsys_release(sme, false);
   2070 	}
   2071 	mutex_exit(&sme_global_mtx);
   2072 }
   2073 
   2074 /*
   2075  * Call the sensor's refresh function, and collect/stir entropy
   2076  */
   2077 void
   2078 sysmon_envsys_refresh_sensor(struct sysmon_envsys *sme, envsys_data_t *edata)
   2079 {
   2080 
   2081 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
   2082 		(*sme->sme_refresh)(sme, edata);
   2083 
   2084 	if (edata->flags & ENVSYS_FHAS_ENTROPY &&
   2085 	    edata->state != ENVSYS_SINVALID &&
   2086 	    edata->value_prev != edata->value_cur)
   2087 		rnd_add_uint32(&edata->rnd_src, edata->value_cur);
   2088 	edata->value_prev = edata->value_cur;
   2089 }
   2090 
   2091 static
   2092 int
   2093 sysmon_envsys_modcmd(modcmd_t cmd, void *arg)
   2094 {
   2095         int ret;
   2096 
   2097         switch (cmd) {
   2098         case MODULE_CMD_INIT:
   2099                 ret = sysmon_envsys_init();
   2100                 break;
   2101 
   2102         case MODULE_CMD_FINI:
   2103                 ret = sysmon_envsys_fini();
   2104                 break;
   2105 
   2106         case MODULE_CMD_STAT:
   2107         default:
   2108                 ret = ENOTTY;
   2109         }
   2110 
   2111         return ret;
   2112 }
   2113