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