Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.18
      1 /*	$NetBSD: sysmon_envsys.c,v 1.18 2007/07/01 07:36:45 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.18 2007/07/01 07:36:45 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,	-1,			"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 			/* mark invalid sensors */
    353 			if (edata->state == ENVSYS_SINVALID) {
    354 				tred->validflags &=
    355 				    ~(ENVSYS_FVALID|ENVSYS_FCURVALID);
    356 				tred->validflags &=
    357 				    ~(ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    358 				tred->cur.data_us = tred->cur.data_s = 0;
    359 			}
    360 
    361 			tred->validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    362 			if (edata->flags & ENVSYS_FPERCENT)
    363 				tred->validflags |=
    364 				    (ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    365 
    366 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    367 			    __func__, edata->desc, tred->cur.data_s));
    368 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    369 			    " tred->sensor=%d\n", __func__, tred->validflags,
    370 			    tred->units, tred->sensor));
    371 		}
    372 		tred->sensor = oidx;
    373 		mutex_exit(&sme_mtx);
    374 
    375 		break;
    376 	    }
    377 	case ENVSYS_GTREINFO:
    378 	    {
    379 		struct envsys_basic_info *binfo = (void *)data;
    380 		envsys_data_t *edata = NULL;
    381 
    382 		binfo->validflags = 0;
    383 
    384 		sme = sysmon_envsys_find_40(binfo->sensor);
    385 		if (sme == NULL)
    386 			break;
    387 
    388 		mutex_enter(&sme_mtx);
    389 		oidx = binfo->sensor;
    390 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    391 
    392 		edata = &sme->sme_sensor_data[binfo->sensor];
    393 
    394 		/* skip invalid sensors */
    395 		if (edata->state == ENVSYS_SINVALID) {
    396 			binfo->validflags &= ~(ENVSYS_FVALID|ENVSYS_FCURVALID);
    397 			binfo->validflags &=
    398 			    ~(ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    399 		}
    400 
    401 		if (binfo->sensor < sme->sme_nsensors) {
    402 			binfo->validflags |= (ENVSYS_FVALID|ENVSYS_FCURVALID);
    403 			if (edata->flags & ENVSYS_FPERCENT)
    404 				binfo->validflags |=
    405 				    (ENVSYS_FMAXVALID|ENVSYS_FFRACVALID);
    406 			binfo->units = edata->units;
    407 			(void)strlcpy(binfo->desc, edata->desc,
    408 			    sizeof(binfo->desc));
    409 		}
    410 
    411 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    412 		    __func__, binfo->units, binfo->validflags));
    413 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    414 		    __func__, binfo->desc, binfo->sensor));
    415 
    416 		binfo->sensor = oidx;
    417 		mutex_exit(&sme_mtx);
    418 
    419 		break;
    420 	    }
    421 #endif /* COMPAT_40 */
    422 	default:
    423 		error = ENOTTY;
    424 		break;
    425 	}
    426 
    427 	return error;
    428 }
    429 
    430 /*
    431  * sysmon_envsys_register:
    432  *
    433  *	+ Register an envsys device.
    434  *	+ Create device dictionary.
    435  */
    436 int
    437 sysmon_envsys_register(struct sysmon_envsys *sme)
    438 {
    439 	struct sysmon_envsys *lsme;
    440 	int error = 0;
    441 
    442 	/*
    443 	 * sanity check 1, make sure the driver has initialized
    444 	 * the sensors count or the value is not too high.
    445 	 */
    446 	if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
    447 		return EINVAL;
    448 
    449 	/*
    450 	 * sanity check 2, make sure that sme->sme_name and
    451 	 * sme->sme_data are not NULL.
    452 	 */
    453 	if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
    454 		return EINVAL;
    455 
    456 	/*
    457 	 * sanity check 3, if SME_DISABLE_GTREDATA is not set
    458 	 * the sme_gtredata function callback must be non NULL.
    459 	 */
    460 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    461 		if (sme->sme_gtredata == NULL)
    462 			return EINVAL;
    463 	}
    464 
    465 	/*
    466 	 * - check if requested sysmon_envsys device is valid
    467 	 *   and does not exist already in the list.
    468 	 * - add device into the list.
    469 	 * - create the plist structure.
    470 	 */
    471 	mutex_enter(&sme_list_mtx);
    472 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    473 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    474 		       mutex_exit(&sme_list_mtx);
    475 		       error = EEXIST;
    476 		       goto out;
    477 	       }
    478 	}
    479 #ifdef COMPAT_40
    480 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    481 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    482 #endif
    483 	mutex_exit(&sme_list_mtx);
    484 	error = sysmon_envsys_createplist(sme);
    485 	if (!error) {
    486 		mutex_enter(&sme_list_mtx);
    487 		LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    488 		mutex_exit(&sme_list_mtx);
    489 	}
    490 
    491 out:
    492 	return error;
    493 }
    494 
    495 /*
    496  * sysmon_envsys_unregister:
    497  *
    498  *	+ Unregister an envsys device.
    499  *	+ Unregister all events associated with this device.
    500  *	+ Remove device dictionary.
    501  */
    502 void
    503 sysmon_envsys_unregister(struct sysmon_envsys *sme)
    504 {
    505 	sme_event_t *see;
    506 
    507 	mutex_enter(&sme_list_mtx);
    508 	while (sme->sme_flags & SME_FLAG_BUSY) {
    509 		sme->sme_flags |= SME_FLAG_WANTED;
    510 		cv_wait(&sme_list_cv, &sme_list_mtx);
    511 	}
    512 	prop_dictionary_remove(sme_propd, sme->sme_name);
    513 	LIST_FOREACH(see, &sme_events_list, see_list) {
    514 		if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
    515 			sme_event_unregister(see->pes.pes_sensname,
    516 			    see->type);
    517 	}
    518 	LIST_REMOVE(sme, sme_list);
    519 	mutex_exit(&sme_list_mtx);
    520 }
    521 
    522 /*
    523  * sysmon_envsys_find:
    524  *
    525  *	+ Find an envsys device.
    526  */
    527 struct sysmon_envsys *
    528 sysmon_envsys_find(const char *name)
    529 {
    530 	struct sysmon_envsys *sme;
    531 
    532 	mutex_enter(&sme_list_mtx);
    533 again:
    534 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    535 	     sme = LIST_NEXT(sme, sme_list)) {
    536 			if (strcmp(sme->sme_name, name) == 0) {
    537 				if (sme->sme_flags & SME_FLAG_BUSY) {
    538 					sme->sme_flags |= SME_FLAG_WANTED;
    539 					cv_wait(&sme_list_cv, &sme_list_mtx);
    540 					goto again;
    541 				}
    542 				sme->sme_flags |= SME_FLAG_BUSY;
    543 				break;
    544 			}
    545 	}
    546 	mutex_exit(&sme_list_mtx);
    547 	return sme;
    548 }
    549 
    550 /*
    551  * sysmon_envsys_release:
    552  *
    553  * 	+ Release an envsys device.
    554  */
    555 void
    556 sysmon_envsys_release(struct sysmon_envsys *sme)
    557 {
    558 	mutex_enter(&sme_list_mtx);
    559 	if (sme->sme_flags & SME_FLAG_WANTED)
    560 		cv_broadcast(&sme_list_cv);
    561 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    562 	mutex_exit(&sme_list_mtx);
    563 }
    564 
    565 /* compatibility function */
    566 #ifdef COMPAT_40
    567 struct sysmon_envsys *
    568 sysmon_envsys_find_40(u_int idx)
    569 {
    570 	struct sysmon_envsys *sme;
    571 
    572 	mutex_enter(&sme_list_mtx);
    573 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
    574 	     sme = LIST_NEXT(sme, sme_list)) {
    575 		if (idx >= sme->sme_fsensor &&
    576 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    577 			break;
    578 	}
    579 	mutex_exit(&sme_list_mtx);
    580 	return sme;
    581 }
    582 #endif
    583 
    584 /*
    585  * sysmon_envsys_createplist:
    586  *
    587  * 	+ Create the property list structure for a device.
    588  */
    589 int
    590 sysmon_envsys_createplist(struct sysmon_envsys *sme)
    591 {
    592 	envsys_data_t *edata;
    593 	prop_array_t array;
    594 	int i, error = 0;
    595 
    596 	/* create the sysmon envsys device array. */
    597 	array = prop_array_create();
    598 
    599 	/*
    600 	 * <dict>
    601 	 * 	<key>foo0</key>
    602 	 * 	<array>
    603 	 * 		...
    604 	 */
    605 	mutex_enter(&sme_mtx);
    606 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    607 		DPRINTF(("%s: prop_dictionary_set\n", __func__));
    608 		mutex_exit(&sme_mtx);
    609 		error = EINVAL;
    610 		goto out;
    611 	}
    612 	mutex_exit(&sme_mtx);
    613 
    614 	/*
    615 	 * Iterate over all sensors and create a dictionary with all
    616 	 * values specified by the sysmon envsys driver.
    617 	 */
    618 	for (i = 0; i < sme->sme_nsensors; i++) {
    619 		edata = &sme->sme_sensor_data[i];
    620 		/*
    621 		 * refresh sensor data via sme_gtredata only if the
    622 		 * flag is not set.
    623 		 */
    624 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    625 			mutex_enter(&sme_mtx);
    626 			error = (*sme->sme_gtredata)(sme, edata);
    627 			if (error) {
    628 				DPRINTF(("%s: sme->sme_gtredata[%d]\n",
    629 				    __func__, i));
    630 				mutex_exit(&sme_mtx);
    631 				continue;
    632 			}
    633 			mutex_exit(&sme_mtx);
    634 		}
    635 
    636 		error = sme_make_dictionary(sme, array, edata);
    637 		if (error) {
    638 			DPRINTF(("%s: sme_make_dictionary[%d]\n", __func__, i));
    639 			goto out;
    640 		}
    641 	}
    642 
    643 out:
    644 	prop_object_release(array);
    645 	return error;
    646 }
    647 
    648 /*
    649  * sme_make_dictionary:
    650  *
    651  * 	+ Create sensor's dictionary in device's dictionary array.
    652  */
    653 int
    654 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    655 		    envsys_data_t *edata)
    656 {
    657 	const struct sme_sensor_type *est = sme_sensor_type;
    658 	const struct sme_sensor_state *ess = sme_sensor_state;
    659 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    660 	sme_event_drv_t *sme_evdrv_t = NULL;
    661 	prop_dictionary_t dict;
    662 	int i, j, k;
    663 
    664 	i = j = k = 0;
    665 
    666 	/*
    667 	 * <array>
    668 	 * 	<dict>
    669 	 * 		...
    670 	 */
    671 	dict = prop_dictionary_create();
    672 
    673 	mutex_enter(&sme_mtx);
    674 	if (!prop_array_add(array, dict)) {
    675 		mutex_exit(&sme_mtx);
    676 		DPRINTF(("%s: prop_array_add\n", __func__));
    677 		return EINVAL;
    678 	}
    679 
    680 	/* find the correct unit for this sensor. */
    681 	for (i = 0; est[i].type != -1; i++)
    682 		if (est[i].type == edata->units)
    683 			break;
    684 
    685 	/*
    686 	 * 		...
    687 	 * 		<key>type</key>
    688 	 * 		<string>foo</string>
    689 	 * 		<key>description</key>
    690 	 * 		<string>blah blah</string>
    691 	 * 		...
    692 	 */
    693 	SENSOR_SSTRING(dict, "type", est[i].desc);
    694 	SENSOR_SSTRING(dict, "description", edata->desc);
    695 
    696 	/*
    697 	 * Add sensor's state description.
    698 	 *
    699 	 * 		...
    700 	 * 		<key>state</key>
    701 	 * 		<string>valid</string>
    702 	 * 		...
    703 	 */
    704 	for (j = 0; ess[j].type != -1; j++)
    705 		if (ess[j].type == edata->state)
    706 			break;
    707 
    708 	SENSOR_SSTRING(dict, "state", ess[j].desc);
    709 
    710 	/*
    711 	 * add the percentage boolean object:
    712 	 *
    713 	 * 		...
    714 	 * 		<key>want-percentage</key>
    715 	 * 		<true/>
    716 	 * 		...
    717 	 */
    718 	if (edata->flags & ENVSYS_FPERCENT)
    719 		SENSOR_SBOOL(dict, "want-percentage", true);
    720 
    721 	/*
    722 	 * Add the monitoring boolean object:
    723 	 *
    724 	 * 		...
    725 	 * 		<key>monitoring-supported</key>
    726 	 * 		<true/>
    727 	 *		...
    728 	 *
    729 	 * always false on Drive, Integer and Indicator types, they
    730 	 * cannot be monitored.
    731 	 *
    732 	 */
    733 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    734 	    (edata->units == ENVSYS_INDICATOR) ||
    735 	    (edata->units == ENVSYS_INTEGER) ||
    736 	    (edata->units == ENVSYS_DRIVE)) {
    737 		SENSOR_SBOOL(dict, "monitoring-supported", false);
    738 	} else {
    739 		SENSOR_SBOOL(dict, "monitoring-supported", true);
    740 	}
    741 
    742 	/*
    743 	 * Add the drive-state object for drive sensors:
    744 	 *
    745 	 * 		...
    746 	 * 		<key>drive-state</key>
    747 	 * 		<string>drive is online</string>
    748 	 * 		...
    749 	 */
    750 	if (edata->units == ENVSYS_DRIVE) {
    751 		for (k = 0; esds[k].type != -1; k++)
    752 			if (esds[k].type == edata->value_cur)
    753 				break;
    754 		SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
    755 	}
    756 
    757 	mutex_exit(&sme_mtx);
    758 	/*
    759 	 * Add a new event if a monitoring flag was set.
    760 	 */
    761 	if (edata->monitor) {
    762 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
    763 
    764 		sme_evdrv_t->sdict = dict;
    765 		sme_evdrv_t->edata = edata;
    766 		sme_evdrv_t->sme = sme;
    767 		sme_evdrv_t->powertype = est[i].crittype;
    768 
    769 		sysmon_task_queue_init();
    770 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
    771 	}
    772 
    773 	mutex_enter(&sme_mtx);
    774 	/* if sensor is enabled, add the following properties... */
    775 	if (edata->state == ENVSYS_SVALID) {
    776 		/*
    777 		 * 	...
    778 		 * 	<key>rpms</key>
    779 		 * 	<integer>2500</integer>
    780 		 * 	<key>rfact</key>
    781 		 * 	<integer>10000</integer>
    782 		 * 	<key>cur-value</key>
    783 		 * 	<integer>1250</integer>
    784 		 * 	<key>min-value</key>
    785 		 * 	<integer>800</integer>
    786 		 * 	<key>max-value</integer>
    787 		 * 	<integer>3000</integer>
    788 		 * 	<key>avg-value</integer>
    789 		 * 	<integer>1400</integer>
    790 		 * </dict>
    791 		 */
    792 		if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
    793 			SENSOR_SUINT32(dict, "rpms", edata->rpms);
    794 
    795 		if ((edata->units == ENVSYS_SVOLTS_AC ||
    796 		    edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
    797 			SENSOR_SINT32(dict, "rfact", edata->rfact);
    798 
    799 		if (edata->value_cur)
    800 			SENSOR_SINT32(dict, "cur-value", edata->value_cur);
    801 
    802 		if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
    803 			SENSOR_SINT32(dict, "min-value", edata->value_min);
    804 
    805 		if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
    806 			SENSOR_SINT32(dict, "max-value", edata->value_max);
    807 
    808 		if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
    809 			SENSOR_SINT32(dict, "avg-value", edata->value_avg);
    810 	}
    811 
    812 	/*
    813 	 * 	...
    814 	 * </array>
    815 	 */
    816 out:
    817 	mutex_exit(&sme_mtx);
    818 	prop_object_release(dict);
    819 	return 0;
    820 }
    821 
    822 /*
    823  * sme_update_dictionary:
    824  *
    825  * 	+ Update per-sensor dictionaries with new values if there were
    826  * 	  changes, otherwise the object in dictionary is untouched.
    827  * 	+ Send a critical event if any sensor is in a critical condition.
    828  */
    829 int
    830 sme_update_dictionary(struct sysmon_envsys *sme)
    831 {
    832 	const struct sme_sensor_state *ess = sme_sensor_state;
    833 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
    834 	envsys_data_t *edata = NULL;
    835 	prop_object_t array, obj, dict = NULL;
    836 	int i, j, error = 0;
    837 
    838 	/* retrieve the array of dictionaries in device. */
    839 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    840 	if (prop_object_type(array) != PROP_TYPE_ARRAY)
    841 		return EINVAL;
    842 
    843 	/*
    844 	 * - iterate over all sensors.
    845 	 * - fetch new data.
    846 	 * - check if data in dictionary is different than new data.
    847 	 * - update dictionary if there were changes.
    848 	 */
    849 	for (i = 0; i < sme->sme_nsensors; i++) {
    850 		edata = &sme->sme_sensor_data[i];
    851 
    852 		/*
    853 		 * refresh sensor data via sme_gtredata only if the
    854 		 * flag is not set.
    855 		 */
    856 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    857 			error = (*sme->sme_gtredata)(sme, edata);
    858 			if (error) {
    859 				DPRINTF(("%s: gtredata[%d] failed\n",
    860 				    __func__, i));
    861 				return error;
    862 			}
    863 		}
    864 
    865 		/* retrieve sensor's dictionary. */
    866 		dict = prop_array_get(array, i);
    867 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY)
    868 			return EINVAL;
    869 
    870 		/* update state sensor. */
    871 		for (j = 0; ess[j].type != -1; j++)
    872 			if (ess[j].type == edata->state)
    873 				break;
    874 
    875 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
    876 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
    877 		    ess[j].type, edata->flags, edata->units, edata->sensor));
    878 
    879 		/* update sensor state */
    880 		SENSOR_UPSTRING(dict, "state", ess[j].desc);
    881 
    882 		/* update sensor current value */
    883 		SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
    884 
    885 		/*
    886 		 * Integer and Indicator types do not the following
    887 		 * values, so skip them.
    888 		 */
    889 		if (edata->units == ENVSYS_INTEGER ||
    890 		    edata->units == ENVSYS_INDICATOR)
    891 			continue;
    892 
    893 		/* update sensor flags */
    894 		if (edata->flags & ENVSYS_FPERCENT)
    895 			SENSOR_SBOOL(dict, "want-percentage", true);
    896 		else {
    897 			obj = prop_dictionary_get(dict, "want-percentage");
    898 			if (obj)
    899 				SENSOR_SBOOL(dict, "want-percentage", false);
    900 		}
    901 
    902 		if (edata->flags & ENVSYS_FVALID_MAX)
    903 			SENSOR_UPINT32(dict, "max-value", edata->value_max);
    904 
    905 		if (edata->flags & ENVSYS_FVALID_MIN)
    906 			SENSOR_UPINT32(dict, "min-value", edata->value_min);
    907 
    908 		if (edata->flags & ENVSYS_FVALID_AVG)
    909 			SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
    910 
    911 		/* update 'rpms' only in ENVSYS_SFANRPM. */
    912 		if (edata->units == ENVSYS_SFANRPM)
    913 			SENSOR_UPUINT32(dict, "rpms", edata->rpms);
    914 
    915 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
    916 		if (edata->units == ENVSYS_SVOLTS_AC ||
    917 		    edata->units == ENVSYS_SVOLTS_DC) {
    918 			SENSOR_UPINT32(dict, "rfact", edata->rfact);
    919 		}
    920 
    921 		/* update 'drive-state' only in ENVSYS_DRIVE. */
    922 		if (edata->units == ENVSYS_DRIVE) {
    923 			for (j = 0; esds[j].type != -1; j++)
    924 				if (esds[j].type == edata->value_cur)
    925 					break;
    926 
    927 			SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
    928 		}
    929 	}
    930 
    931 out:
    932 	return error;
    933 }
    934 
    935 /*
    936  * sme_userset_dictionary:
    937  *
    938  * 	+ Parses the userland dictionary and run the appropiate
    939  * 	  tasks that were requested.
    940  */
    941 int
    942 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
    943 		       prop_array_t array)
    944 {
    945 	const struct sme_sensor_type *sst = sme_sensor_type;
    946 	envsys_data_t *edata;
    947 	prop_dictionary_t dict;
    948 	prop_object_t obj, obj1, obj2;
    949 	int32_t critval;
    950 	int i, error = 0;
    951 	const char *blah, *sname;
    952 	bool targetfound = false;
    953 
    954 	blah = sname = NULL;
    955 
    956 	/* get sensor's name from userland dictionary. */
    957 	obj = prop_dictionary_get(udict, "sensor-name");
    958 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
    959 		DPRINTF(("%s: sensor-name failed\n", __func__));
    960 		return EINVAL;
    961 	}
    962 
    963 	/* iterate over the sensors to find the right one */
    964 	for (i = 0; i < sme->sme_nsensors; i++) {
    965 		edata = &sme->sme_sensor_data[i];
    966 		dict = prop_array_get(array, i);
    967 		obj1 = prop_dictionary_get(dict, "description");
    968 
    969 		/* is it our sensor? */
    970 		if (!prop_string_equals(obj1, obj))
    971 			continue;
    972 
    973 		/*
    974 		 * Check if a new description operation was
    975 		 * requested by the user and set new description.
    976 		 */
    977 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
    978 			targetfound = true;
    979 			blah = prop_string_cstring_nocopy(obj2);
    980 			SENSOR_UPSTRING(dict, "description", blah);
    981 			break;
    982 		}
    983 
    984 		/* did the user want to remove a critical capacity limit? */
    985 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
    986 		if (obj2 != NULL) {
    987 			targetfound = true;
    988 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    989 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
    990 				    error = ENOTSUP;
    991 				    break;
    992 			}
    993 
    994 			sname = prop_string_cstring_nocopy(obj);
    995 			error = sme_event_unregister(sname,
    996 			    PENVSYS_EVENT_BATT_USERCAP);
    997 			if (error)
    998 				break;
    999 
   1000 			prop_dictionary_remove(dict, "critical-capacity");
   1001 			break;
   1002 		}
   1003 
   1004 		/* did the user want to remove a critical min limit? */
   1005 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
   1006 		if (obj2 != NULL) {
   1007 			targetfound = true;
   1008 			sname = prop_string_cstring_nocopy(obj);
   1009 			error = sme_event_unregister(sname,
   1010 			    PENVSYS_EVENT_USER_CRITMIN);
   1011 			if (error)
   1012 				break;
   1013 
   1014 			prop_dictionary_remove(dict, "critical-min-limit");
   1015 			break;
   1016 		}
   1017 
   1018 		/* did the user want to remove a critical max limit? */
   1019 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
   1020 		if (obj2 != NULL) {
   1021 			targetfound = true;
   1022 			sname = prop_string_cstring_nocopy(obj);
   1023 			error = sme_event_unregister(sname,
   1024 			    PENVSYS_EVENT_USER_CRITMAX);
   1025 			if (error)
   1026 				break;
   1027 
   1028 			prop_dictionary_remove(dict, "critical-max-limit");
   1029 			break;
   1030 		}
   1031 
   1032 		/* did the user want to change rfact? */
   1033 		obj2 = prop_dictionary_get(udict, "new-rfact");
   1034 		if (obj2 != NULL) {
   1035 			targetfound = true;
   1036 			if (edata->flags & ENVSYS_FCHANGERFACT)
   1037 				edata->rfact = prop_number_integer_value(obj2);
   1038 			else
   1039 				error = ENOTSUP;
   1040 
   1041 			break;
   1042 		}
   1043 
   1044 		for (i = 0; sst[i].type != -1; i++)
   1045 			if (sst[i].type == edata->units)
   1046 				break;
   1047 
   1048 		/* did the user want to set a critical capacity event? */
   1049 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1050 		if (obj2 != NULL) {
   1051 			targetfound = true;
   1052 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1053 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1054 				error = ENOTSUP;
   1055 				break;
   1056 			}
   1057 
   1058 			critval = prop_number_integer_value(obj2);
   1059 			error = sme_event_add(dict,
   1060 					      edata,
   1061 					      sme->sme_name,
   1062 					      "critical-capacity",
   1063 					      critval,
   1064 					      PENVSYS_EVENT_BATT_USERCAP,
   1065 					      sst[i].crittype);
   1066 			break;
   1067 		}
   1068 
   1069 		/* did the user want to set a critical max event? */
   1070 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
   1071 		if (obj2 != NULL) {
   1072 			targetfound = true;
   1073 			if (edata->units == ENVSYS_INDICATOR ||
   1074 			    edata->units == ENVSYS_INTEGER ||
   1075 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1076 				error = ENOTSUP;
   1077 				break;
   1078 			}
   1079 
   1080 			critval = prop_number_integer_value(obj2);
   1081 			error = sme_event_add(dict,
   1082 					      edata,
   1083 					      sme->sme_name,
   1084 					      "critical-max-limit",
   1085 					      critval,
   1086 					      PENVSYS_EVENT_USER_CRITMAX,
   1087 					      sst[i].crittype);
   1088 			break;
   1089 		}
   1090 
   1091 		/* did the user want to set a critical min event? */
   1092 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
   1093 		if (obj2 != NULL) {
   1094 			targetfound = true;
   1095 			if (edata->units == ENVSYS_INDICATOR ||
   1096 			    edata->units == ENVSYS_INTEGER ||
   1097 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1098 				error = ENOTSUP;
   1099 				break;
   1100 			}
   1101 
   1102 			critval = prop_number_integer_value(obj2);
   1103 			error = sme_event_add(dict,
   1104 					      edata,
   1105 					      sme->sme_name,
   1106 					      "critical-min-limit",
   1107 					      critval,
   1108 					      PENVSYS_EVENT_USER_CRITMIN,
   1109 					      sst[i].crittype);
   1110 			break;
   1111 		}
   1112 	}
   1113 
   1114 	/* invalid target? return the error */
   1115 	if (!targetfound)
   1116 		error = EINVAL;
   1117 
   1118 out:
   1119 	return error;
   1120 }
   1121