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