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