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