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