Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.20
      1 /*	$NetBSD: sysmon_envsys.c,v 1.20 2007/07/04 16:30:18 xtraeme Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juan Romero Pardines.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by Juan Romero Pardines
     21  *      for the NetBSD Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 2000 Zembu Labs, Inc.
     41  * All rights reserved.
     42  *
     43  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *	This product includes software developed by Zembu Labs, Inc.
     56  * 4. Neither the name of Zembu Labs nor the names of its employees may
     57  *    be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     61  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     62  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     63  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     64  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     65  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     66  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     67  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     68  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     69  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 /*
     73  * Environmental sensor framework for sysmon, exported to userland
     74  * with proplib(3).
     75  */
     76 
     77 #include <sys/cdefs.h>
     78 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.20 2007/07/04 16:30:18 xtraeme Exp $");
     79 
     80 #include <sys/param.h>
     81 #include <sys/types.h>
     82 #include <sys/conf.h>
     83 #include <sys/errno.h>
     84 #include <sys/kernel.h>
     85 #include <sys/systm.h>
     86 #include <sys/proc.h>
     87 #include <sys/mutex.h>
     88 #include <sys/kmem.h>
     89 
     90 #include <dev/sysmon/sysmonvar.h>
     91 #include <dev/sysmon/sysmon_envsysvar.h>
     92 #include <dev/sysmon/sysmon_taskq.h>
     93 
     94 #include "opt_compat_netbsd.h"
     95 
     96 struct sme_sensor_type {
     97 	int		type;
     98 	int		crittype;
     99 	const char	*desc;
    100 };
    101 
    102 static const struct sme_sensor_type sme_sensor_type[] = {
    103 	{ ENVSYS_STEMP,		PENVSYS_TYPE_TEMP,	"Temperature" },
    104 	{ ENVSYS_SFANRPM,	PENVSYS_TYPE_FAN,	"Fan" },
    105 	{ ENVSYS_SVOLTS_AC,	PENVSYS_TYPE_VOLTAGE,	"Voltage AC" },
    106 	{ ENVSYS_SVOLTS_DC,	PENVSYS_TYPE_VOLTAGE,	"Voltage DC" },
    107 	{ ENVSYS_SOHMS,		PENVSYS_TYPE_RESISTANCE,"Ohms" },
    108 	{ ENVSYS_SWATTS,	PENVSYS_TYPE_POWER,	"Watts" },
    109 	{ ENVSYS_SAMPS,		PENVSYS_TYPE_POWER,	"Ampere" },
    110 	{ ENVSYS_SWATTHOUR,	PENVSYS_TYPE_BATTERY,	"Watt hour" },
    111 	{ ENVSYS_SAMPHOUR,	PENVSYS_TYPE_BATTERY,	"Ampere hour" },
    112 	{ ENVSYS_INDICATOR,	PENVSYS_TYPE_INDICATOR,	"Indicator" },
    113 	{ ENVSYS_INTEGER,	-1, 			"Integer" },
    114 	{ ENVSYS_DRIVE,		PENVSYS_TYPE_DRIVE,	"Drive" },
    115 	{ -1,			-1,			NULL }
    116 };
    117 
    118 struct sme_sensor_state {
    119 	int		type;
    120 	const char 	*desc;
    121 };
    122 
    123 static const struct sme_sensor_state sme_sensor_state[] = {
    124 	{ ENVSYS_SVALID,	"valid" },
    125 	{ ENVSYS_SINVALID,	"invalid" },
    126 	{ ENVSYS_SCRITICAL,	"critical" },
    127 	{ ENVSYS_SCRITUNDER,	"critical-under" },
    128 	{ ENVSYS_SCRITOVER,	"critical-over" },
    129 	{ ENVSYS_SWARNUNDER,	"warning-under" },
    130 	{ ENVSYS_SWARNOVER,	"warning-over" },
    131 	{ -1,			"unknown" }
    132 };
    133 
    134 static const struct sme_sensor_state sme_sensor_drive_state[] = {
    135 	{ ENVSYS_DRIVE_EMPTY,		"drive state is unknown" },
    136 	{ ENVSYS_DRIVE_READY,		"drive is ready" },
    137 	{ ENVSYS_DRIVE_POWERUP,		"drive is powering up" },
    138 	{ ENVSYS_DRIVE_ONLINE,		"drive is online" },
    139 	{ ENVSYS_DRIVE_IDLE,		"drive is idle" },
    140 	{ ENVSYS_DRIVE_ACTIVE,		"drive is active" },
    141 	{ ENVSYS_DRIVE_REBUILD,		"drive is rebuilding" },
    142 	{ ENVSYS_DRIVE_POWERDOWN,	"drive is powering down" },
    143 	{ ENVSYS_DRIVE_FAIL,		"drive failed" },
    144 	{ ENVSYS_DRIVE_PFAIL,		"drive degraded" },
    145 	{ -1,				"unknown" }
    146 };
    147 
    148 static prop_dictionary_t sme_propd;
    149 static kmutex_t sme_list_mtx;
    150 static kcondvar_t sme_list_cv;
    151 
    152 #ifdef COMPAT_40
    153 static u_int sysmon_envsys_next_sensor_index = 0;
    154 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    155 #endif
    156 
    157 static void sysmon_envsys_release(struct sysmon_envsys *);
    158 
    159 kmutex_t sme_mtx, sme_event_mtx;
    160 
    161 /*
    162  * sysmon_envsys_init:
    163  *
    164  * 	+ Initialize global mutexes, dictionary and the linked lists.
    165  */
    166 void
    167 sysmon_envsys_init(void)
    168 {
    169 	LIST_INIT(&sysmon_envsys_list);
    170 	LIST_INIT(&sme_events_list);
    171 	mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
    172 	mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
    173 	mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
    174 	mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
    175 	cv_init(&sme_list_cv, "smefind");
    176 	sme_propd = prop_dictionary_create();
    177 }
    178 
    179 /*
    180  * sysmonopen_envsys:
    181  *
    182  *	+ Open the system monitor device.
    183  */
    184 int
    185 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    186 {
    187 	return 0;
    188 }
    189 
    190 /*
    191  * sysmonclose_envsys:
    192  *
    193  *	+ Close the system monitor device.
    194  */
    195 int
    196 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    197 {
    198 	/* Nothing to do */
    199 	return 0;
    200 }
    201 
    202 /*
    203  * sysmonioctl_envsys:
    204  *
    205  *	+ Perform an envsys control request.
    206  */
    207 int
    208 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    209 {
    210 	struct sysmon_envsys *sme = NULL;
    211 	int error = 0;
    212 #ifdef COMPAT_40
    213 	u_int oidx;
    214 #endif
    215 
    216 	switch (cmd) {
    217 	case ENVSYS_GETDICTIONARY:
    218 	    {
    219 		struct plistref *plist = (struct plistref *)data;
    220 
    221 		/*
    222 		 * Update all sysmon envsys devices dictionaries with
    223 		 * new data if it's different than we have currently
    224 		 * in the dictionary.
    225 		 */
    226 		mutex_enter(&sme_mtx);
    227 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    228 			if (sme == NULL)
    229 				continue;
    230 
    231 			error = sme_update_dictionary(sme);
    232 			if (error) {
    233 				DPRINTF(("%s: sme_update_dictionary, "
    234 				    "error=%d\n", __func__, error));
    235 				mutex_exit(&sme_mtx);
    236 				return error;
    237 			}
    238 		}
    239 		mutex_exit(&sme_mtx);
    240 		/*
    241 		 * Copy global dictionary to userland.
    242 		 */
    243 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    244 		break;
    245 	    }
    246 	case ENVSYS_SETDICTIONARY:
    247 	    {
    248 		const struct plistref *plist = (const struct plistref *)data;
    249 		prop_dictionary_t udict;
    250 		prop_object_t obj;
    251 		const char *devname = NULL;
    252 
    253 		/*
    254 		 * Get dictionary from userland.
    255 		 */
    256 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    257 		DPRINTF(("%s: copyin_ioctl error=%d\n", __func__, error));
    258 		if (error)
    259 			break;
    260 
    261 		/*
    262 		 * Parse "driver-name" key to obtain the driver we
    263 		 * are searching for.
    264 		 */
    265 		obj = prop_dictionary_get(udict, "driver-name");
    266 		if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
    267 			DPRINTF(("%s: driver-name failed\n", __func__));
    268 			prop_object_release(udict);
    269 			error = EINVAL;
    270 			break;
    271 		}
    272 
    273 		/* driver name */
    274 		devname = prop_string_cstring_nocopy(obj);
    275 
    276 		/* find the correct sme device */
    277 		sme = sysmon_envsys_find(devname);
    278 		if (sme == NULL) {
    279 			DPRINTF(("%s: NULL sme\n", __func__));
    280 			prop_object_release(udict);
    281 			error = EINVAL;
    282 			break;
    283 		}
    284 
    285 		/*
    286 		 * Find the correct array object with the string supplied
    287 		 * by the userland dictionary.
    288 		 */
    289 		obj = prop_dictionary_get(sme_propd, devname);
    290 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
    291 			DPRINTF(("%s: array device failed\n", __func__));
    292 			prop_object_release(udict);
    293 			error = EINVAL;
    294 			break;
    295 		}
    296 
    297 		/* do the real work now */
    298 		error = sme_userset_dictionary(sme, udict, obj);
    299 		sysmon_envsys_release(sme);
    300 		break;
    301 	    }
    302 	    /*
    303 	     * Compatibility functions with the old interface, only
    304 	     * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
    305 	     * to make old applications work.
    306 	     */
    307 #ifdef COMPAT_40
    308 	case ENVSYS_GTREDATA:
    309 	    {
    310 		struct envsys_tre_data *tred = (void *)data;
    311 		envsys_data_t *edata = NULL;
    312 
    313 		tred->validflags = 0;
    314 
    315 		sme = sysmon_envsys_find_40(tred->sensor);
    316 		if (sme == NULL)
    317 			break;
    318 
    319 		mutex_enter(&sme_mtx);
    320 		oidx = tred->sensor;
    321 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    322 
    323 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    324 		    __func__, tred->sensor, oidx, sme->sme_name,
    325 		    sme->sme_nsensors));
    326 
    327 		edata = &sme->sme_sensor_data[tred->sensor];
    328 
    329 		if (tred->sensor < sme->sme_nsensors) {
    330 			if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    331 				error = (*sme->sme_gtredata)(sme, edata);
    332 				if (error) {
    333 					DPRINTF(("%s: sme_gtredata failed\n",
    334 				    	    __func__));
    335 					mutex_exit(&sme_mtx);
    336 					return error;
    337 				}
    338 			}
    339 
    340 			/* copy required values to the old interface */
    341 			tred->sensor = edata->sensor;
    342 			tred->cur.data_us = edata->value_cur;
    343 			tred->cur.data_s = edata->value_cur;
    344 			tred->max.data_us = edata->value_max;
    345 			tred->max.data_s = edata->value_max;
    346 			tred->min.data_us = edata->value_min;
    347 			tred->min.data_s = edata->value_min;
    348 			tred->avg.data_us = edata->value_avg;
    349 			tred->avg.data_s = edata->value_avg;
    350 			tred->units = edata->units;
    351 
    352 			tred->validflags |= ENVSYS_FVALID;
    353 			tred->validflags |= ENVSYS_FCURVALID;
    354 
    355 			if (edata->flags & ENVSYS_FPERCENT) {
    356 				tred->validflags |= ENVSYS_FMAXVALID;
    357 				tred->validflags |= ENVSYS_FFRACVALID;
    358 			}
    359 
    360 			if (edata->state == ENVSYS_SINVALID) {
    361 				tred->validflags &= ~ENVSYS_FCURVALID;
    362 				tred->cur.data_us = tred->cur.data_s = 0;
    363 			}
    364 
    365 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    366 			    __func__, edata->desc, tred->cur.data_s));
    367 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    368 			    " tred->sensor=%d\n", __func__, tred->validflags,
    369 			    tred->units, tred->sensor));
    370 		}
    371 		tred->sensor = oidx;
    372 		mutex_exit(&sme_mtx);
    373 
    374 		break;
    375 	    }
    376 	case ENVSYS_GTREINFO:
    377 	    {
    378 		struct envsys_basic_info *binfo = (void *)data;
    379 		envsys_data_t *edata = NULL;
    380 
    381 		binfo->validflags = 0;
    382 
    383 		sme = sysmon_envsys_find_40(binfo->sensor);
    384 		if (sme == NULL)
    385 			break;
    386 
    387 		mutex_enter(&sme_mtx);
    388 		oidx = binfo->sensor;
    389 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    390 
    391 		edata = &sme->sme_sensor_data[binfo->sensor];
    392 
    393 		binfo->validflags |= ENVSYS_FVALID;
    394 
    395 		if (binfo->sensor < sme->sme_nsensors) {
    396 			binfo->units = edata->units;
    397 			(void)strlcpy(binfo->desc, edata->desc,
    398 			    sizeof(binfo->desc));
    399 		}
    400 
    401 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    402 		    __func__, binfo->units, binfo->validflags));
    403 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    404 		    __func__, binfo->desc, binfo->sensor));
    405 
    406 		binfo->sensor = oidx;
    407 		mutex_exit(&sme_mtx);
    408 
    409 		break;
    410 	    }
    411 #endif /* COMPAT_40 */
    412 	default:
    413 		error = ENOTTY;
    414 		break;
    415 	}
    416 
    417 	return error;
    418 }
    419 
    420 /*
    421  * sysmon_envsys_register:
    422  *
    423  *	+ Register an envsys device.
    424  *	+ Create device dictionary.
    425  */
    426 int
    427 sysmon_envsys_register(struct sysmon_envsys *sme)
    428 {
    429 	struct sysmon_envsys *lsme;
    430 	int error = 0;
    431 
    432 	/*
    433 	 * sanity check 1, make sure the driver has initialized
    434 	 * the sensors count or the value is not too high.
    435 	 */
    436 	if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
    437 		return EINVAL;
    438 
    439 	/*
    440 	 * sanity check 2, make sure that sme->sme_name and
    441 	 * sme->sme_data are not NULL.
    442 	 */
    443 	if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
    444 		return EINVAL;
    445 
    446 	/*
    447 	 * sanity check 3, if SME_DISABLE_GTREDATA is not set
    448 	 * the sme_gtredata function callback must be non NULL.
    449 	 */
    450 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    451 		if (sme->sme_gtredata == NULL)
    452 			return EINVAL;
    453 	}
    454 
    455 	/*
    456 	 * - check if requested sysmon_envsys device is valid
    457 	 *   and does not exist already in the list.
    458 	 * - add device into the list.
    459 	 * - create the plist structure.
    460 	 */
    461 	mutex_enter(&sme_list_mtx);
    462 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    463 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    464 		       mutex_exit(&sme_list_mtx);
    465 		       error = EEXIST;
    466 		       goto out;
    467 	       }
    468 	}
    469 #ifdef COMPAT_40
    470 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    471 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    472 #endif
    473 	mutex_exit(&sme_list_mtx);
    474 	error = sysmon_envsys_createplist(sme);
    475 	if (!error) {
    476 		mutex_enter(&sme_list_mtx);
    477 		LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    478 		mutex_exit(&sme_list_mtx);
    479 	}
    480 
    481 out:
    482 	return error;
    483 }
    484 
    485 /*
    486  * sysmon_envsys_unregister:
    487  *
    488  *	+ Unregister an envsys device.
    489  *	+ Unregister all events associated with this device.
    490  *	+ Remove device dictionary.
    491  */
    492 void
    493 sysmon_envsys_unregister(struct sysmon_envsys *sme)
    494 {
    495 	sme_event_t *see;
    496 
    497 	mutex_enter(&sme_list_mtx);
    498 	while (sme->sme_flags & SME_FLAG_BUSY) {
    499 		sme->sme_flags |= SME_FLAG_WANTED;
    500 		cv_wait(&sme_list_cv, &sme_list_mtx);
    501 	}
    502 	prop_dictionary_remove(sme_propd, sme->sme_name);
    503 	LIST_FOREACH(see, &sme_events_list, see_list) {
    504 		if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
    505 			sme_event_unregister(see->pes.pes_sensname,
    506 			    see->type);
    507 	}
    508 	LIST_REMOVE(sme, sme_list);
    509 	mutex_exit(&sme_list_mtx);
    510 }
    511 
    512 /*
    513  * sysmon_envsys_find:
    514  *
    515  *	+ Find an envsys device.
    516  */
    517 struct sysmon_envsys *
    518 sysmon_envsys_find(const char *name)
    519 {
    520 	struct sysmon_envsys *sme;
    521 
    522 	mutex_enter(&sme_list_mtx);
    523 again:
    524 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    525 	     sme = LIST_NEXT(sme, sme_list)) {
    526 			if (strcmp(sme->sme_name, name) == 0) {
    527 				if (sme->sme_flags & SME_FLAG_BUSY) {
    528 					sme->sme_flags |= SME_FLAG_WANTED;
    529 					cv_wait(&sme_list_cv, &sme_list_mtx);
    530 					goto again;
    531 				}
    532 				sme->sme_flags |= SME_FLAG_BUSY;
    533 				break;
    534 			}
    535 	}
    536 	mutex_exit(&sme_list_mtx);
    537 	return sme;
    538 }
    539 
    540 /*
    541  * sysmon_envsys_release:
    542  *
    543  * 	+ Release an envsys device.
    544  */
    545 void
    546 sysmon_envsys_release(struct sysmon_envsys *sme)
    547 {
    548 	mutex_enter(&sme_list_mtx);
    549 	if (sme->sme_flags & SME_FLAG_WANTED)
    550 		cv_broadcast(&sme_list_cv);
    551 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    552 	mutex_exit(&sme_list_mtx);
    553 }
    554 
    555 /* compatibility function */
    556 #ifdef COMPAT_40
    557 struct sysmon_envsys *
    558 sysmon_envsys_find_40(u_int idx)
    559 {
    560 	struct sysmon_envsys *sme;
    561 
    562 	mutex_enter(&sme_list_mtx);
    563 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    564 	     sme = LIST_NEXT(sme, sme_list)) {
    565 		if (idx >= sme->sme_fsensor &&
    566 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    567 			break;
    568 	}
    569 	mutex_exit(&sme_list_mtx);
    570 	return sme;
    571 }
    572 #endif
    573 
    574 /*
    575  * sysmon_envsys_createplist:
    576  *
    577  * 	+ Create the property list structure for a device.
    578  */
    579 int
    580 sysmon_envsys_createplist(struct sysmon_envsys *sme)
    581 {
    582 	envsys_data_t *edata;
    583 	prop_array_t array;
    584 	int i, error = 0;
    585 
    586 	/* create the sysmon envsys device array. */
    587 	array = prop_array_create();
    588 
    589 	/*
    590 	 * <dict>
    591 	 * 	<key>foo0</key>
    592 	 * 	<array>
    593 	 * 		...
    594 	 */
    595 	mutex_enter(&sme_mtx);
    596 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    597 		DPRINTF(("%s: prop_dictionary_set\n", __func__));
    598 		mutex_exit(&sme_mtx);
    599 		error = EINVAL;
    600 		goto out;
    601 	}
    602 	mutex_exit(&sme_mtx);
    603 
    604 	/*
    605 	 * Iterate over all sensors and create a dictionary with all
    606 	 * values specified by the sysmon envsys driver.
    607 	 */
    608 	for (i = 0; i < sme->sme_nsensors; i++) {
    609 		edata = &sme->sme_sensor_data[i];
    610 		/*
    611 		 * refresh sensor data via sme_gtredata only if the
    612 		 * flag is not set.
    613 		 */
    614 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    615 			mutex_enter(&sme_mtx);
    616 			error = (*sme->sme_gtredata)(sme, edata);
    617 			if (error) {
    618 				DPRINTF(("%s: sme->sme_gtredata[%d]\n",
    619 				    __func__, i));
    620 				mutex_exit(&sme_mtx);
    621 				continue;
    622 			}
    623 			mutex_exit(&sme_mtx);
    624 		}
    625 
    626 		error = sme_make_dictionary(sme, array, edata);
    627 		if (error) {
    628 			DPRINTF(("%s: sme_make_dictionary[%d]\n", __func__, i));
    629 			goto out;
    630 		}
    631 	}
    632 
    633 out:
    634 	prop_object_release(array);
    635 	return error;
    636 }
    637 
    638 /*
    639  * sme_make_dictionary:
    640  *
    641  * 	+ Create sensor's dictionary in device's dictionary array.
    642  */
    643 int
    644 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    645 		    envsys_data_t *edata)
    646 {
    647 	const struct sme_sensor_type *est = sme_sensor_type;
    648 	const struct sme_sensor_state *ess = sme_sensor_state;
    649 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    650 	sme_event_drv_t *sme_evdrv_t = NULL;
    651 	prop_dictionary_t dict;
    652 	int i, j, k;
    653 
    654 	i = j = k = 0;
    655 
    656 	/*
    657 	 * <array>
    658 	 * 	<dict>
    659 	 * 		...
    660 	 */
    661 	dict = prop_dictionary_create();
    662 
    663 	mutex_enter(&sme_mtx);
    664 	if (!prop_array_add(array, dict)) {
    665 		mutex_exit(&sme_mtx);
    666 		DPRINTF(("%s: prop_array_add\n", __func__));
    667 		return EINVAL;
    668 	}
    669 
    670 	/* find the correct unit for this sensor. */
    671 	for (i = 0; est[i].type != -1; i++)
    672 		if (est[i].type == edata->units)
    673 			break;
    674 
    675 	/*
    676 	 * 		...
    677 	 * 		<key>type</key>
    678 	 * 		<string>foo</string>
    679 	 * 		<key>description</key>
    680 	 * 		<string>blah blah</string>
    681 	 * 		...
    682 	 */
    683 	SENSOR_SSTRING(dict, "type", est[i].desc);
    684 	SENSOR_SSTRING(dict, "description", edata->desc);
    685 
    686 	/*
    687 	 * Add sensor's state description.
    688 	 *
    689 	 * 		...
    690 	 * 		<key>state</key>
    691 	 * 		<string>valid</string>
    692 	 * 		...
    693 	 */
    694 	for (j = 0; ess[j].type != -1; j++)
    695 		if (ess[j].type == edata->state)
    696 			break;
    697 
    698 	SENSOR_SSTRING(dict, "state", ess[j].desc);
    699 
    700 	/*
    701 	 * add the percentage boolean object:
    702 	 *
    703 	 * 		...
    704 	 * 		<key>want-percentage</key>
    705 	 * 		<true/>
    706 	 * 		...
    707 	 */
    708 	if (edata->flags & ENVSYS_FPERCENT)
    709 		SENSOR_SBOOL(dict, "want-percentage", true);
    710 
    711 	/*
    712 	 * Add the monitoring boolean object:
    713 	 *
    714 	 * 		...
    715 	 * 		<key>monitoring-supported</key>
    716 	 * 		<true/>
    717 	 *		...
    718 	 *
    719 	 * always false on Drive, Integer and Indicator types, they
    720 	 * cannot be monitored.
    721 	 *
    722 	 */
    723 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    724 	    (edata->units == ENVSYS_INDICATOR) ||
    725 	    (edata->units == ENVSYS_INTEGER) ||
    726 	    (edata->units == ENVSYS_DRIVE)) {
    727 		SENSOR_SBOOL(dict, "monitoring-supported", false);
    728 	} else {
    729 		SENSOR_SBOOL(dict, "monitoring-supported", true);
    730 	}
    731 
    732 	/*
    733 	 * Add the drive-state object for drive sensors:
    734 	 *
    735 	 * 		...
    736 	 * 		<key>drive-state</key>
    737 	 * 		<string>drive is online</string>
    738 	 * 		...
    739 	 */
    740 	if (edata->units == ENVSYS_DRIVE) {
    741 		for (k = 0; esds[k].type != -1; k++)
    742 			if (esds[k].type == edata->value_cur)
    743 				break;
    744 		SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
    745 	}
    746 
    747 	mutex_exit(&sme_mtx);
    748 	/*
    749 	 * Add a new event if a monitoring flag was set.
    750 	 */
    751 	if (edata->monitor && edata->units != ENVSYS_INTEGER) {
    752 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
    753 
    754 		sme_evdrv_t->sdict = dict;
    755 		sme_evdrv_t->edata = edata;
    756 		sme_evdrv_t->sme = sme;
    757 		sme_evdrv_t->powertype = est[i].crittype;
    758 
    759 		sysmon_task_queue_init();
    760 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
    761 	}
    762 
    763 	mutex_enter(&sme_mtx);
    764 	/* if sensor is enabled, add the following properties... */
    765 	if (edata->state == ENVSYS_SVALID) {
    766 		/*
    767 		 * 	...
    768 		 * 	<key>rpms</key>
    769 		 * 	<integer>2500</integer>
    770 		 * 	<key>rfact</key>
    771 		 * 	<integer>10000</integer>
    772 		 * 	<key>cur-value</key>
    773 		 * 	<integer>1250</integer>
    774 		 * 	<key>min-value</key>
    775 		 * 	<integer>800</integer>
    776 		 * 	<key>max-value</integer>
    777 		 * 	<integer>3000</integer>
    778 		 * 	<key>avg-value</integer>
    779 		 * 	<integer>1400</integer>
    780 		 * </dict>
    781 		 */
    782 		if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
    783 			SENSOR_SUINT32(dict, "rpms", edata->rpms);
    784 
    785 		if ((edata->units == ENVSYS_SVOLTS_AC ||
    786 		    edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
    787 			SENSOR_SINT32(dict, "rfact", edata->rfact);
    788 
    789 		if (edata->value_cur)
    790 			SENSOR_SINT32(dict, "cur-value", edata->value_cur);
    791 
    792 		if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
    793 			SENSOR_SINT32(dict, "min-value", edata->value_min);
    794 
    795 		if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
    796 			SENSOR_SINT32(dict, "max-value", edata->value_max);
    797 
    798 		if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
    799 			SENSOR_SINT32(dict, "avg-value", edata->value_avg);
    800 	}
    801 
    802 	/*
    803 	 * 	...
    804 	 * </array>
    805 	 */
    806 out:
    807 	mutex_exit(&sme_mtx);
    808 	prop_object_release(dict);
    809 	return 0;
    810 }
    811 
    812 /*
    813  * sme_update_dictionary:
    814  *
    815  * 	+ Update per-sensor dictionaries with new values if there were
    816  * 	  changes, otherwise the object in dictionary is untouched.
    817  * 	+ Send a critical event if any sensor is in a critical condition.
    818  */
    819 int
    820 sme_update_dictionary(struct sysmon_envsys *sme)
    821 {
    822 	const struct sme_sensor_state *ess = sme_sensor_state;
    823 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    824 	envsys_data_t *edata = NULL;
    825 	prop_object_t array, obj, dict = NULL;
    826 	int i, j, error = 0;
    827 
    828 	/* retrieve the array of dictionaries in device. */
    829 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    830 	if (prop_object_type(array) != PROP_TYPE_ARRAY)
    831 		return EINVAL;
    832 
    833 	/*
    834 	 * - iterate over all sensors.
    835 	 * - fetch new data.
    836 	 * - check if data in dictionary is different than new data.
    837 	 * - update dictionary if there were changes.
    838 	 */
    839 	for (i = 0; i < sme->sme_nsensors; i++) {
    840 		edata = &sme->sme_sensor_data[i];
    841 
    842 		/*
    843 		 * refresh sensor data via sme_gtredata only if the
    844 		 * flag is not set.
    845 		 */
    846 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    847 			error = (*sme->sme_gtredata)(sme, edata);
    848 			if (error) {
    849 				DPRINTF(("%s: gtredata[%d] failed\n",
    850 				    __func__, i));
    851 				return error;
    852 			}
    853 		}
    854 
    855 		/* retrieve sensor's dictionary. */
    856 		dict = prop_array_get(array, i);
    857 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY)
    858 			return EINVAL;
    859 
    860 		/* update state sensor. */
    861 		for (j = 0; ess[j].type != -1; j++)
    862 			if (ess[j].type == edata->state)
    863 				break;
    864 
    865 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
    866 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
    867 		    ess[j].type, edata->flags, edata->units, edata->sensor));
    868 
    869 		/* update sensor state */
    870 		SENSOR_UPSTRING(dict, "state", ess[j].desc);
    871 
    872 		/* update sensor current value */
    873 		SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
    874 
    875 		/*
    876 		 * Integer and Indicator types do not the following
    877 		 * values, so skip them.
    878 		 */
    879 		if (edata->units == ENVSYS_INTEGER ||
    880 		    edata->units == ENVSYS_INDICATOR)
    881 			continue;
    882 
    883 		/* update sensor flags */
    884 		if (edata->flags & ENVSYS_FPERCENT)
    885 			SENSOR_SBOOL(dict, "want-percentage", true);
    886 		else {
    887 			obj = prop_dictionary_get(dict, "want-percentage");
    888 			if (obj)
    889 				SENSOR_SBOOL(dict, "want-percentage", false);
    890 		}
    891 
    892 		if (edata->flags & ENVSYS_FVALID_MAX)
    893 			SENSOR_UPINT32(dict, "max-value", edata->value_max);
    894 
    895 		if (edata->flags & ENVSYS_FVALID_MIN)
    896 			SENSOR_UPINT32(dict, "min-value", edata->value_min);
    897 
    898 		if (edata->flags & ENVSYS_FVALID_AVG)
    899 			SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
    900 
    901 		/* update 'rpms' only in ENVSYS_SFANRPM. */
    902 		if (edata->units == ENVSYS_SFANRPM)
    903 			SENSOR_UPUINT32(dict, "rpms", edata->rpms);
    904 
    905 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
    906 		if (edata->units == ENVSYS_SVOLTS_AC ||
    907 		    edata->units == ENVSYS_SVOLTS_DC) {
    908 			SENSOR_UPINT32(dict, "rfact", edata->rfact);
    909 		}
    910 
    911 		/* update 'drive-state' only in ENVSYS_DRIVE. */
    912 		if (edata->units == ENVSYS_DRIVE) {
    913 			for (j = 0; esds[j].type != -1; j++)
    914 				if (esds[j].type == edata->value_cur)
    915 					break;
    916 
    917 			SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
    918 		}
    919 	}
    920 
    921 out:
    922 	return error;
    923 }
    924 
    925 /*
    926  * sme_userset_dictionary:
    927  *
    928  * 	+ Parses the userland dictionary and run the appropiate
    929  * 	  tasks that were requested.
    930  */
    931 int
    932 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
    933 		       prop_array_t array)
    934 {
    935 	const struct sme_sensor_type *sst = sme_sensor_type;
    936 	envsys_data_t *edata;
    937 	prop_dictionary_t dict;
    938 	prop_object_t obj, obj1, obj2;
    939 	int32_t critval;
    940 	int i, error = 0;
    941 	const char *blah, *sname;
    942 	bool targetfound = false;
    943 
    944 	blah = sname = NULL;
    945 
    946 	/* get sensor's name from userland dictionary. */
    947 	obj = prop_dictionary_get(udict, "sensor-name");
    948 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
    949 		DPRINTF(("%s: sensor-name failed\n", __func__));
    950 		return EINVAL;
    951 	}
    952 
    953 	/* iterate over the sensors to find the right one */
    954 	for (i = 0; i < sme->sme_nsensors; i++) {
    955 		edata = &sme->sme_sensor_data[i];
    956 		dict = prop_array_get(array, i);
    957 		obj1 = prop_dictionary_get(dict, "description");
    958 
    959 		/* is it our sensor? */
    960 		if (!prop_string_equals(obj1, obj))
    961 			continue;
    962 
    963 		/*
    964 		 * Check if a new description operation was
    965 		 * requested by the user and set new description.
    966 		 */
    967 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
    968 			targetfound = true;
    969 			blah = prop_string_cstring_nocopy(obj2);
    970 			SENSOR_UPSTRING(dict, "description", blah);
    971 			break;
    972 		}
    973 
    974 		/* did the user want to remove a critical capacity limit? */
    975 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
    976 		if (obj2 != NULL) {
    977 			targetfound = true;
    978 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    979 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
    980 				    error = ENOTSUP;
    981 				    break;
    982 			}
    983 
    984 			sname = prop_string_cstring_nocopy(obj);
    985 			error = sme_event_unregister(sname,
    986 			    PENVSYS_EVENT_BATT_USERCAP);
    987 			if (error)
    988 				break;
    989 
    990 			prop_dictionary_remove(dict, "critical-capacity");
    991 			break;
    992 		}
    993 
    994 		/* did the user want to remove a critical min limit? */
    995 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
    996 		if (obj2 != NULL) {
    997 			targetfound = true;
    998 			sname = prop_string_cstring_nocopy(obj);
    999 			error = sme_event_unregister(sname,
   1000 			    PENVSYS_EVENT_USER_CRITMIN);
   1001 			if (error)
   1002 				break;
   1003 
   1004 			prop_dictionary_remove(dict, "critical-min-limit");
   1005 			break;
   1006 		}
   1007 
   1008 		/* did the user want to remove a critical max limit? */
   1009 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1010 		if (obj2 != NULL) {
   1011 			targetfound = true;
   1012 			sname = prop_string_cstring_nocopy(obj);
   1013 			error = sme_event_unregister(sname,
   1014 			    PENVSYS_EVENT_USER_CRITMAX);
   1015 			if (error)
   1016 				break;
   1017 
   1018 			prop_dictionary_remove(dict, "critical-max-limit");
   1019 			break;
   1020 		}
   1021 
   1022 		/* did the user want to change rfact? */
   1023 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1024 		if (obj2 != NULL) {
   1025 			targetfound = true;
   1026 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1027 				edata->rfact = prop_number_integer_value(obj2);
   1028 			else
   1029 				error = ENOTSUP;
   1030 
   1031 			break;
   1032 		}
   1033 
   1034 		for (i = 0; sst[i].type != -1; i++)
   1035 			if (sst[i].type == edata->units)
   1036 				break;
   1037 
   1038 		/* did the user want to set a critical capacity event? */
   1039 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1040 		if (obj2 != NULL) {
   1041 			targetfound = true;
   1042 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1043 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1044 				error = ENOTSUP;
   1045 				break;
   1046 			}
   1047 
   1048 			critval = prop_number_integer_value(obj2);
   1049 			error = sme_event_add(dict,
   1050 					      edata,
   1051 					      sme->sme_name,
   1052 					      "critical-capacity",
   1053 					      critval,
   1054 					      PENVSYS_EVENT_BATT_USERCAP,
   1055 					      sst[i].crittype);
   1056 			break;
   1057 		}
   1058 
   1059 		/* did the user want to set a critical max event? */
   1060 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1061 		if (obj2 != NULL) {
   1062 			targetfound = true;
   1063 			if (edata->units == ENVSYS_INDICATOR ||
   1064 			    edata->units == ENVSYS_INTEGER ||
   1065 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1066 				error = ENOTSUP;
   1067 				break;
   1068 			}
   1069 
   1070 			critval = prop_number_integer_value(obj2);
   1071 			error = sme_event_add(dict,
   1072 					      edata,
   1073 					      sme->sme_name,
   1074 					      "critical-max-limit",
   1075 					      critval,
   1076 					      PENVSYS_EVENT_USER_CRITMAX,
   1077 					      sst[i].crittype);
   1078 			break;
   1079 		}
   1080 
   1081 		/* did the user want to set a critical min event? */
   1082 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1083 		if (obj2 != NULL) {
   1084 			targetfound = true;
   1085 			if (edata->units == ENVSYS_INDICATOR ||
   1086 			    edata->units == ENVSYS_INTEGER ||
   1087 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1088 				error = ENOTSUP;
   1089 				break;
   1090 			}
   1091 
   1092 			critval = prop_number_integer_value(obj2);
   1093 			error = sme_event_add(dict,
   1094 					      edata,
   1095 					      sme->sme_name,
   1096 					      "critical-min-limit",
   1097 					      critval,
   1098 					      PENVSYS_EVENT_USER_CRITMIN,
   1099 					      sst[i].crittype);
   1100 			break;
   1101 		}
   1102 	}
   1103 
   1104 	/* invalid target? return the error */
   1105 	if (!targetfound)
   1106 		error = EINVAL;
   1107 
   1108 out:
   1109 	return error;
   1110 }
   1111