Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys_events.c revision 1.58.4.3
      1 /* $NetBSD: sysmon_envsys_events.c,v 1.58.4.3 2008/11/13 00:08:34 snj Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007, 2008 Juan Romero Pardines.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * sysmon_envsys(9) events framework.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.58.4.3 2008/11/13 00:08:34 snj Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/types.h>
     37 #include <sys/conf.h>
     38 #include <sys/errno.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 #include <sys/proc.h>
     42 #include <sys/mutex.h>
     43 #include <sys/kmem.h>
     44 #include <sys/callout.h>
     45 
     46 /* #define ENVSYS_DEBUG */
     47 
     48 #include <dev/sysmon/sysmonvar.h>
     49 #include <dev/sysmon/sysmon_envsysvar.h>
     50 
     51 struct sme_sensor_event {
     52 	int		state;
     53 	int		event;
     54 };
     55 
     56 static const struct sme_sensor_event sme_sensor_event[] = {
     57 	{ ENVSYS_SVALID, 	PENVSYS_EVENT_NORMAL },
     58 	{ ENVSYS_SCRITOVER, 	PENVSYS_EVENT_CRITOVER },
     59 	{ ENVSYS_SCRITUNDER, 	PENVSYS_EVENT_CRITUNDER },
     60 	{ ENVSYS_SWARNOVER, 	PENVSYS_EVENT_WARNOVER },
     61 	{ ENVSYS_SWARNUNDER,	PENVSYS_EVENT_WARNUNDER },
     62 	{ -1, 			-1 }
     63 };
     64 
     65 static bool sysmon_low_power;
     66 
     67 #define SME_EVTIMO	(SME_EVENTS_DEFTIMEOUT * hz)
     68 
     69 static bool sme_event_check_low_power(void);
     70 static bool sme_battery_check(void);
     71 static bool sme_battery_critical(envsys_data_t *);
     72 static bool sme_acadapter_check(void);
     73 
     74 /*
     75  * sme_event_register:
     76  *
     77  * 	+ Registers a new sysmon envsys event or updates any event
     78  * 	  already in the queue.
     79  */
     80 int
     81 sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
     82 		   struct sysmon_envsys *sme, const char *objkey,
     83 		   int32_t critval, int crittype, int powertype)
     84 {
     85 	sme_event_t *see = NULL, *osee = NULL;
     86 	prop_object_t obj;
     87 	bool critvalup = false;
     88 	int error = 0;
     89 	int real_crittype;
     90 	int32_t o_critval;
     91 
     92 	KASSERT(sdict != NULL || edata != NULL || sme != NULL);
     93 	/*
     94 	 * Allocate a new sysmon_envsys event.
     95 	 */
     96 	see = kmem_zalloc(sizeof(*see), KM_SLEEP);
     97 	if (see == NULL)
     98 		return ENOMEM;
     99 
    100 	/*
    101 	 * Map user-types to kernel types
    102 	 */
    103 	switch (crittype) {
    104 	case PENVSYS_EVENT_USER_CRITMAX:
    105 	case PENVSYS_EVENT_USER_CRITMIN:
    106 	case PENVSYS_EVENT_USER_WARNMAX:
    107 	case PENVSYS_EVENT_USER_WARNMIN:
    108 		real_crittype = PENVSYS_EVENT_USER_LIMITS;
    109 		break;
    110 	case PENVSYS_EVENT_BATT_USERCAP:
    111 	case PENVSYS_EVENT_BATT_USERWARN:
    112 		real_crittype = PENVSYS_EVENT_BATT_USER_LIMITS;
    113 		break;
    114 	default:
    115 		real_crittype = crittype;
    116 	}
    117 
    118 	/*
    119 	 * check if the event is already on the list and return
    120 	 * EEXIST if value provided hasn't been changed.
    121 	 */
    122 	mutex_enter(&sme->sme_mtx);
    123 	LIST_FOREACH(osee, &sme->sme_events_list, see_list) {
    124 		if (strcmp(edata->desc, osee->see_pes.pes_sensname) == 0)
    125 			if (real_crittype == osee->see_type) {
    126 				switch (crittype) {
    127 				case PENVSYS_EVENT_USER_CRITMAX:
    128 				case PENVSYS_EVENT_BATT_USERCAP:
    129 					o_critval = osee->see_critmax;
    130 					break;
    131 				case PENVSYS_EVENT_USER_WARNMAX:
    132 				case PENVSYS_EVENT_BATT_USERWARN:
    133 					o_critval = osee->see_warnmax;
    134 					break;
    135 				case PENVSYS_EVENT_USER_WARNMIN:
    136 					o_critval = osee->see_warnmin;
    137 					break;
    138 				case PENVSYS_EVENT_USER_CRITMIN:
    139 				default:
    140 					o_critval = osee->see_critmin;
    141 					break;
    142 				}
    143 				if (o_critval == critval) {
    144 					DPRINTF(("%s: dev=%s sensor=%s type=%d "
    145 				    	    "(already exists)\n", __func__,
    146 				    	    osee->see_pes.pes_dvname,
    147 				    	    osee->see_pes.pes_sensname,
    148 					    osee->see_type));
    149 					error = EEXIST;
    150 					goto out;
    151 				}
    152 				critvalup = true;
    153 				break;
    154 			}
    155 	}
    156 
    157 	/*
    158 	 * Critical condition operation requested by userland.
    159 	 */
    160 	if (objkey && critval && critvalup) {
    161 		obj = prop_dictionary_get(sdict, objkey);
    162 		if (obj && prop_object_type(obj) == PROP_TYPE_NUMBER) {
    163 			/*
    164 			 * object is already in dictionary and value
    165 			 * provided is not the same than we have
    166 			 * currently, update the critical value.
    167 			 */
    168 			switch (crittype) {
    169 			case PENVSYS_EVENT_USER_CRITMAX:
    170 			case PENVSYS_EVENT_BATT_USERCAP:
    171 				osee->see_critmax = critval;
    172 				break;
    173 			case PENVSYS_EVENT_USER_WARNMAX:
    174 			case PENVSYS_EVENT_BATT_USERWARN:
    175 				osee->see_warnmax = critval;
    176 				break;
    177 			case PENVSYS_EVENT_USER_WARNMIN:
    178 				osee->see_warnmin = critval;
    179 				break;
    180 			case PENVSYS_EVENT_USER_CRITMIN:
    181 			default:
    182 				osee->see_critmin = critval;
    183 				break;
    184 			}
    185 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
    186 			    "(critval updated)\n", __func__, sme->sme_name,
    187 			    edata->desc, osee->see_type));
    188 			error = sme_sensor_upint32(sdict, objkey, critval);
    189 			goto out;
    190 		}
    191 	}
    192 
    193 	/*
    194 	 * New limit defined for existing event
    195 	 */
    196 	if (osee != NULL) {
    197 		osee->see_edata = edata;
    198 		switch (crittype) {
    199 		case PENVSYS_EVENT_USER_CRITMAX:
    200 		case PENVSYS_EVENT_BATT_USERCAP:
    201 			osee->see_critmax = critval;
    202 			break;
    203 		case PENVSYS_EVENT_USER_WARNMAX:
    204 		case PENVSYS_EVENT_BATT_USERWARN:
    205 			osee->see_warnmax = critval;
    206 			break;
    207 		case PENVSYS_EVENT_USER_WARNMIN:
    208 			osee->see_warnmin = critval;
    209 			break;
    210 		case PENVSYS_EVENT_USER_CRITMIN:
    211 		default:
    212 			osee->see_critmin = critval;
    213 			break;
    214 		}
    215 		if (objkey && critval) {
    216 			error = sme_sensor_upint32(sdict, objkey, critval);
    217 			if (error)
    218 				goto out;
    219 		}
    220 		DPRINTF(("%s: (%s) new limit added to existing event, type %d "
    221 		    "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%"
    222 		    PRIu32 " critmax=%d\n", __func__, osee->see_sme->sme_name,
    223 		    osee->see_type, osee->see_critmin, osee->see_warnmin,
    224 		    osee->see_warnmax, osee->see_critmax));
    225 		goto out;
    226 	}
    227 	/*
    228 	 * New event requested.
    229 	 */
    230 	see->see_edata = edata;
    231 	switch (crittype) {
    232 	case PENVSYS_EVENT_USER_CRITMAX:
    233 	case PENVSYS_EVENT_BATT_USERCAP:
    234 		see->see_critmax = critval;
    235 		break;
    236 	case PENVSYS_EVENT_USER_WARNMAX:
    237 	case PENVSYS_EVENT_BATT_USERWARN:
    238 		see->see_warnmax = critval;
    239 		break;
    240 	case PENVSYS_EVENT_USER_WARNMIN:
    241 		see->see_warnmin = critval;
    242 		break;
    243 	case PENVSYS_EVENT_USER_CRITMIN:
    244 	default:
    245 		see->see_critmin = critval;
    246 		break;
    247 	}
    248 	see->see_type = real_crittype;
    249 	see->see_sme = sme;
    250 
    251 	/* Initialize sensor type and previously-sent state */
    252 
    253 	see->see_pes.pes_type = powertype;
    254 	switch (realcrittype) {
    255 	case PENVSYS_EVENT_HW_LIMITS:
    256 	case PENVSYS_EVENT_USER_LIMITS:
    257 	case PENVSYS_EVENT_BATT_USER_LIMITS:
    258 		see->see_evsent = ENVSYS_SVALID;
    259 		break;
    260 	case PENVSYS_EVENT_STATE_CHANGED:
    261 		if (edata->units == ENVSYS_BATTERY_CAPACITY)
    262 			see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
    263 		else if (edata->units == ENVSYS_DRIVE)
    264 			see->see_evsent = ENVSYS_DRIVE_EMPTY;
    265 		else
    266 			panic("%s: bad units for "
    267 			      "PENVSYS_EVENT_STATE_CHANGED", __func__);
    268 		break;
    269 	case PENVSYS_EVENT_CRITICAL:
    270 	default:
    271 		see->see_evsent = 0;
    272 		break;
    273 	}
    274 
    275 	(void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
    276 	    sizeof(see->see_pes.pes_dvname));
    277 	(void)strlcpy(see->see_pes.pes_sensname, edata->desc,
    278 	    sizeof(see->see_pes.pes_sensname));
    279 
    280 	LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
    281 	if (objkey && critval) {
    282 		error = sme_sensor_upint32(sdict, objkey, critval);
    283 		if (error)
    284 			goto out;
    285 	}
    286 	DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d "
    287 	    "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32
    288 	    " crixmax=%" PRIu32 ")\n", __func__,
    289 	    see->see_sme->sme_name, see->see_pes.pes_sensname,
    290 	    see->see_edata->sensor, see->see_type, see->see_critmin,
    291 	    see->see_warnmin, see->see_warnmax, see->see_critmax));
    292 	/*
    293 	 * Initialize the events framework if it wasn't initialized before.
    294 	 */
    295 	if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
    296 		error = sme_events_init(sme);
    297 out:
    298 	mutex_exit(&sme->sme_mtx);
    299 	if (error || critvalup)
    300 		kmem_free(see, sizeof(*see));
    301 	return error;
    302 }
    303 
    304 /*
    305  * sme_event_unregister_all:
    306  *
    307  * 	+ Unregisters all events associated with a sysmon envsys device.
    308  */
    309 void
    310 sme_event_unregister_all(struct sysmon_envsys *sme)
    311 {
    312 	sme_event_t *see;
    313 	int evcounter = 0;
    314 
    315 	KASSERT(sme != NULL);
    316 
    317 	mutex_enter(&sme->sme_mtx);
    318 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    319 		while (see->see_flags & SEE_EVENT_WORKING)
    320 			cv_wait(&sme->sme_condvar, &sme->sme_mtx);
    321 
    322 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
    323 			evcounter++;
    324 	}
    325 
    326 	DPRINTF(("%s: total events %d (%s)\n", __func__,
    327 	    evcounter, sme->sme_name));
    328 
    329 	while ((see = LIST_FIRST(&sme->sme_events_list))) {
    330 		if (evcounter == 0)
    331 			break;
    332 
    333 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
    334 			LIST_REMOVE(see, see_list);
    335 			DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
    336 			    see->see_pes.pes_sensname, see->see_type,
    337 			    sme->sme_name));
    338 			kmem_free(see, sizeof(*see));
    339 			evcounter--;
    340 		}
    341 	}
    342 
    343 	if (LIST_EMPTY(&sme->sme_events_list))
    344 		if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
    345 			sme_events_destroy(sme);
    346 	mutex_exit(&sme->sme_mtx);
    347 }
    348 
    349 /*
    350  * sme_event_unregister:
    351  *
    352  * 	+ Unregisters an event from the specified sysmon envsys device.
    353  */
    354 int
    355 sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
    356 {
    357 	sme_event_t *see;
    358 	bool found = false;
    359 
    360 	KASSERT(sensor != NULL);
    361 
    362 	mutex_enter(&sme->sme_mtx);
    363 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    364 		if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
    365 			if (see->see_type == type) {
    366 				found = true;
    367 				break;
    368 			}
    369 		}
    370 	}
    371 
    372 	if (!found) {
    373 		mutex_exit(&sme->sme_mtx);
    374 		return EINVAL;
    375 	}
    376 
    377 	/*
    378 	 * Wait for the event to finish its work, remove from the list
    379 	 * and release resouces.
    380 	 */
    381 	while (see->see_flags & SEE_EVENT_WORKING)
    382 		cv_wait(&sme->sme_condvar, &sme->sme_mtx);
    383 
    384 	DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n",
    385 	    __func__, see->see_pes.pes_dvname, sensor, type));
    386 	LIST_REMOVE(see, see_list);
    387 	/*
    388 	 * So the events list is empty, we'll do the following:
    389 	 *
    390 	 * 	- stop and destroy the callout.
    391 	 * 	- destroy the workqueue.
    392 	 */
    393 	if (LIST_EMPTY(&sme->sme_events_list))
    394 		sme_events_destroy(sme);
    395 	mutex_exit(&sme->sme_mtx);
    396 
    397 	kmem_free(see, sizeof(*see));
    398 	return 0;
    399 }
    400 
    401 /*
    402  * sme_event_drvadd:
    403  *
    404  * 	+ Registers a new event for a device that had enabled any of
    405  * 	  the monitoring flags in the driver.
    406  */
    407 void
    408 sme_event_drvadd(void *arg)
    409 {
    410 	sme_event_drv_t *sed_t = arg;
    411 	int error = 0;
    412 
    413 	KASSERT(sed_t != NULL);
    414 
    415 #define SEE_REGEVENT(a, b, c)						\
    416 do {									\
    417 	if (sed_t->sed_edata->flags & (a)) {				\
    418 		char str[ENVSYS_DESCLEN] = "monitoring-state-";		\
    419 									\
    420 		error = sme_event_register(sed_t->sed_sdict,		\
    421 				      sed_t->sed_edata,			\
    422 				      sed_t->sed_sme,			\
    423 				      NULL,				\
    424 				      0,				\
    425 				      (b),				\
    426 				      sed_t->sed_powertype);		\
    427 		if (error && error != EEXIST)				\
    428 			printf("%s: failed to add event! "		\
    429 			    "error=%d sensor=%s event=%s\n",		\
    430 			    __func__, error,				\
    431 			    sed_t->sed_edata->desc, (c));		\
    432 		else {							\
    433 			(void)strlcat(str, (c), sizeof(str));		\
    434 			prop_dictionary_set_bool(sed_t->sed_sdict,	\
    435 						 str,			\
    436 						 true);			\
    437 		}							\
    438 	}								\
    439 } while (/* CONSTCOND */ 0)
    440 
    441 	SEE_REGEVENT(ENVSYS_FMONCRITICAL,
    442 		     PENVSYS_EVENT_CRITICAL,
    443 		     "critical");
    444 
    445 	SEE_REGEVENT(ENVSYS_FMONCRITUNDER | ENVSYS_FMONCRITOVER |
    446 		     ENVSYS_FMONWARNUNDER | ENVSYS_FMONWARNOVER,
    447 		     PENVSYS_EVENT_HW_LIMITS,
    448 		     "hw-range-limits");
    449 
    450 	SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
    451 		     PENVSYS_EVENT_STATE_CHANGED,
    452 		     "state-changed");
    453 
    454 	/*
    455 	 * we are done, free memory now.
    456 	 */
    457 	kmem_free(sed_t, sizeof(*sed_t));
    458 }
    459 
    460 /*
    461  * sme_events_init:
    462  *
    463  * 	+ Initialize the events framework for this device.
    464  */
    465 int
    466 sme_events_init(struct sysmon_envsys *sme)
    467 {
    468 	int error = 0;
    469 	uint64_t timo;
    470 
    471 	KASSERT(sme != NULL);
    472 	KASSERT(mutex_owned(&sme->sme_mtx));
    473 
    474 	if (sme->sme_events_timeout)
    475 		timo = sme->sme_events_timeout * hz;
    476 	else
    477 		timo = SME_EVTIMO;
    478 
    479 	error = workqueue_create(&sme->sme_wq, sme->sme_name,
    480 	    sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
    481 	if (error)
    482 		return error;
    483 
    484 	mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    485 	callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
    486 	callout_setfunc(&sme->sme_callout, sme_events_check, sme);
    487 	callout_schedule(&sme->sme_callout, timo);
    488 	sme->sme_flags |= SME_CALLOUT_INITIALIZED;
    489 	DPRINTF(("%s: events framework initialized for '%s'\n",
    490 	    __func__, sme->sme_name));
    491 
    492 	return error;
    493 }
    494 
    495 /*
    496  * sme_events_destroy:
    497  *
    498  * 	+ Destroys the event framework for this device: callout
    499  * 	  stopped, workqueue destroyed and callout mutex destroyed.
    500  */
    501 void
    502 sme_events_destroy(struct sysmon_envsys *sme)
    503 {
    504 	KASSERT(mutex_owned(&sme->sme_mtx));
    505 
    506 	callout_stop(&sme->sme_callout);
    507 	workqueue_destroy(sme->sme_wq);
    508 	mutex_destroy(&sme->sme_callout_mtx);
    509 	callout_destroy(&sme->sme_callout);
    510 	sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
    511 	DPRINTF(("%s: events framework destroyed for '%s'\n",
    512 	    __func__, sme->sme_name));
    513 }
    514 
    515 /*
    516  * sme_events_check:
    517  *
    518  * 	+ Passes the events to the workqueue thread and stops
    519  * 	  the callout if the 'low-power' condition is triggered.
    520  */
    521 void
    522 sme_events_check(void *arg)
    523 {
    524 	struct sysmon_envsys *sme = arg;
    525 	sme_event_t *see;
    526 	uint64_t timo;
    527 
    528 	KASSERT(sme != NULL);
    529 
    530 	mutex_enter(&sme->sme_callout_mtx);
    531 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    532 		workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
    533 		see->see_edata->flags |= ENVSYS_FNEED_REFRESH;
    534 	}
    535 	if (sme->sme_events_timeout)
    536 		timo = sme->sme_events_timeout * hz;
    537 	else
    538 		timo = SME_EVTIMO;
    539 	if (!sysmon_low_power)
    540 		callout_schedule(&sme->sme_callout, timo);
    541 	mutex_exit(&sme->sme_callout_mtx);
    542 }
    543 
    544 /*
    545  * sme_events_worker:
    546  *
    547  * 	+ workqueue thread that checks if there's a critical condition
    548  * 	  and sends an event if it was triggered.
    549  */
    550 void
    551 sme_events_worker(struct work *wk, void *arg)
    552 {
    553 	const struct sme_description_table *sdt = NULL;
    554 	const struct sme_sensor_event *sse = sme_sensor_event;
    555 	sme_event_t *see = (void *)wk;
    556 	struct sysmon_envsys *sme = see->see_sme;
    557 	envsys_data_t *edata = see->see_edata;
    558 	int i, state = 0;
    559 
    560 	KASSERT(wk == &see->see_wk);
    561 	KASSERT(sme != NULL || edata != NULL);
    562 
    563 	mutex_enter(&sme->sme_mtx);
    564 	if ((see->see_flags & SEE_EVENT_WORKING) == 0)
    565 		see->see_flags |= SEE_EVENT_WORKING;
    566 	/*
    567 	 * sme_events_check marks the first event for the device to
    568 	 * mak us refresh it here.  Don't refresh if the driver uses
    569 	 * its own method for refreshing.
    570 	 */
    571 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
    572 		if ((see->see_edata->flags & ENVSYS_FNEED_REFRESH) != 0) {
    573 			/* refresh sensor in device */
    574 			(*sme->sme_refresh)(sme, edata);
    575 			see->see_edata->flags &= ~ENVSYS_FNEED_REFRESH;
    576 		}
    577 	}
    578 
    579 	DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
    580 	    "value_cur=%d\n", __func__, sme->sme_name, edata->desc,
    581 	    edata->sensor, see->see_type, edata->state, edata->units,
    582 	    edata->value_cur));
    583 
    584 	/* skip the event if current sensor is in invalid state */
    585 	if (edata->state == ENVSYS_SINVALID)
    586 		goto out;
    587 
    588 	switch (see->see_type) {
    589 	/*
    590 	 * For user range limits, calculate a new state first
    591 	 * State based on user limits will override any hardware
    592 	 * detected state.
    593 	 */
    594 	case PENVSYS_EVENT_USER_LIMITS:
    595 	case PENVSYS_EVENT_BATT_USER_LIMITS:
    596 #define __EXCEEDED_LIMIT(lim, rel) ((lim) && edata->value_cur rel (lim))
    597 		if __EXCEEDED_LIMIT(see->see_critmin, <)
    598 			edata->state = ENVSYS_SCRITUNDER;
    599 		else if __EXCEEDED_LIMIT(see->see_warnmin, <)
    600 			edata->state = ENVSYS_SWARNUNDER;
    601 		else if __EXCEEDED_LIMIT(see->see_warnmax, >)
    602 			edata->state = ENVSYS_SWARNOVER;
    603 		else if __EXCEEDED_LIMIT(see->see_critmax, >)
    604 			edata->state = ENVSYS_SCRITOVER;
    605 		/* FALLTHROUGH */
    606 #undef __EXCEED_LIMIT
    607 	/*
    608 	 * For hardware and user range limits, send event if state has changed
    609 	 */
    610 	case PENVSYS_EVENT_HW_LIMITS:
    611 		if (edata->state == see->see_evsent)
    612 			break;
    613 
    614 		for (i = 0; sse[i].state != -1; i++)
    615 			if (sse[i].state == edata->state)
    616 				break;
    617 
    618 		if (sse[i].state == -1)
    619 			break;
    620 
    621 		if (edata->state == ENVSYS_SVALID)
    622 			sysmon_penvsys_event(&see->see_pes,
    623 					     PENVSYS_EVENT_NORMAL);
    624 		else
    625 			sysmon_penvsys_event(&see->see_pes, sse[i].event);
    626 
    627 		see->see_evsent = edata->state;
    628 
    629 		break;
    630 
    631 	/*
    632 	 * Send PENVSYS_EVENT_CRITICAL event if:
    633 	 *	State has gone from non-CRITICAL to CRITICAL,
    634 	 *	State remains CRITICAL and value has changed, or
    635 	 *	State has returned from CRITICAL to non-CRITICAL
    636 	 */
    637 	case PENVSYS_EVENT_CRITICAL:
    638 		if (edata->state == ENVSYS_SVALID &&
    639 		    see->see_evsent != 0) {
    640 			sysmon_penvsys_event(&see->see_pes,
    641 					     PENVSYS_EVENT_NORMAL);
    642 			see->see_evsent = 0;
    643 		} else if (edata->state == ENVSYS_SCRITICAL &&
    644 		    see->see_evsent != edata->value_cur) {
    645 			sysmon_penvsys_event(&see->see_pes,
    646 					     PENVSYS_EVENT_CRITICAL);
    647 			see->see_evsent = edata->value_cur;
    648 		}
    649 		break;
    650 
    651 	/*
    652 	 * if value_cur is not normal (battery) or online (drive),
    653 	 * send the event...
    654 	 */
    655 	case PENVSYS_EVENT_STATE_CHANGED:
    656 		/*
    657 		 * the state has not been changed, just ignore the event.
    658 		 */
    659 		if (edata->value_cur == see->see_evsent)
    660 			break;
    661 
    662 		switch (edata->units) {
    663 		case ENVSYS_DRIVE:
    664 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
    665 			state = ENVSYS_DRIVE_ONLINE;
    666 			break;
    667 		case ENVSYS_BATTERY_CAPACITY:
    668 			sdt = sme_get_description_table(
    669 			    SME_DESC_BATTERY_CAPACITY);
    670 			state = ENVSYS_BATTERY_CAPACITY_NORMAL;
    671 			break;
    672 		default:
    673 			panic("%s: invalid units for ENVSYS_FMONSTCHANGED",
    674 			    __func__);
    675 		}
    676 
    677 		for (i = 0; sdt[i].type != -1; i++)
    678 			if (sdt[i].type == edata->value_cur)
    679 				break;
    680 
    681 		if (sdt[i].type == -1)
    682 			break;
    683 
    684 		/*
    685 		 * copy current state description.
    686 		 */
    687 		(void)strlcpy(see->see_pes.pes_statedesc, sdt[i].desc,
    688 		    sizeof(see->see_pes.pes_statedesc));
    689 
    690 		/*
    691 		 * state is ok again... send a normal event.
    692 		 */
    693 		if (see->see_evsent && edata->value_cur == state) {
    694 			sysmon_penvsys_event(&see->see_pes,
    695 					     PENVSYS_EVENT_NORMAL);
    696 			see->see_evsent = false;
    697 		}
    698 
    699 		/*
    700 		 * state has been changed... send event.
    701 		 */
    702 		if (see->see_evsent || edata->value_cur != state) {
    703 			/*
    704 			 * save current drive state.
    705 			 */
    706 			see->see_evsent = edata->value_cur;
    707 			sysmon_penvsys_event(&see->see_pes, see->see_type);
    708 		}
    709 
    710 		/*
    711 		 * There's no need to continue if it's a drive sensor.
    712 		 */
    713 		if (edata->units == ENVSYS_DRIVE)
    714 			break;
    715 
    716 		/*
    717 		 * Check if the system is running in low power and send the
    718 		 * event to powerd (if running) or shutdown the system
    719 		 * otherwise.
    720 		 */
    721 		if (!sysmon_low_power && sme_event_check_low_power()) {
    722 			struct penvsys_state pes;
    723 
    724 			/*
    725 			 * Stop the callout and send the 'low-power' event.
    726 			 */
    727 			sysmon_low_power = true;
    728 			callout_stop(&sme->sme_callout);
    729 			pes.pes_type = PENVSYS_TYPE_BATTERY;
    730 			sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
    731 		}
    732 		break;
    733 	default:
    734 		panic("%s: invalid event type %d", __func__, see->see_type);
    735 	}
    736 
    737 out:
    738 	see->see_flags &= ~SEE_EVENT_WORKING;
    739 	cv_broadcast(&sme->sme_condvar);
    740 	mutex_exit(&sme->sme_mtx);
    741 }
    742 
    743 /*
    744  * Returns true if the system is in low power state: an AC adapter
    745  * is OFF and all batteries are in LOW/CRITICAL state.
    746  */
    747 static bool
    748 sme_event_check_low_power(void)
    749 {
    750 	if (!sme_acadapter_check())
    751 		return false;
    752 
    753 	return sme_battery_check();
    754 }
    755 
    756 /*
    757  * Called with the sysmon_envsys device mtx held through the
    758  * workqueue thread.
    759  */
    760 static bool
    761 sme_acadapter_check(void)
    762 {
    763 	struct sysmon_envsys *sme;
    764 	envsys_data_t *edata;
    765 	bool dev = false, sensor = false;
    766 
    767 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    768 		if (sme->sme_class == SME_CLASS_ACADAPTER) {
    769 			dev = true;
    770 			break;
    771 		}
    772 	}
    773 
    774 	/*
    775 	 * No AC Adapter devices were found.
    776 	 */
    777 	if (!dev)
    778 		return false;
    779 
    780 	/*
    781 	 * Check if there's an AC adapter device connected.
    782 	 */
    783 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    784 		if (edata->units == ENVSYS_INDICATOR) {
    785 			sensor = true;
    786 			/* refresh current sensor */
    787 			(*sme->sme_refresh)(sme, edata);
    788 			if (edata->value_cur)
    789 				return false;
    790 		}
    791 	}
    792 
    793 	if (!sensor)
    794 		return false;
    795 
    796 	/*
    797 	 * AC adapter found and not connected.
    798 	 */
    799 	return true;
    800 }
    801 
    802 /*
    803  * Called with the sysmon_envsys device mtx held through the
    804  * workqueue thread.
    805  */
    806 static bool
    807 sme_battery_check(void)
    808 {
    809 	struct sysmon_envsys *sme;
    810 	envsys_data_t *edata;
    811 	int batteriesfound = 0;
    812 	bool present, batterycap, batterycharge;
    813 
    814 	/*
    815 	 * Check for battery devices and its state.
    816 	 */
    817 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    818 		if (sme->sme_class != SME_CLASS_BATTERY)
    819 			continue;
    820 
    821 		present = true;
    822 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    823 			if (edata->units == ENVSYS_INDICATOR &&
    824 			    !edata->value_cur) {
    825 				present = false;
    826 				break;
    827 			}
    828 		}
    829 		if (!present)
    830 			continue;
    831 		/*
    832 		 * We've found a battery device...
    833 		 */
    834 		batteriesfound++;
    835 		batterycap = batterycharge = false;
    836 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    837 			if (edata->units == ENVSYS_BATTERY_CAPACITY) {
    838 				batterycap = true;
    839 				if (!sme_battery_critical(edata))
    840 					return false;
    841 			} else if (edata->units == ENVSYS_BATTERY_CHARGE) {
    842 				batterycharge = true;
    843 				if (edata->value_cur)
    844 					return false;
    845 			}
    846 		}
    847 		if (!batterycap || !batterycharge)
    848 			return false;
    849 	}
    850 
    851 	if (!batteriesfound)
    852 		return false;
    853 
    854 	/*
    855 	 * All batteries in low/critical capacity and discharging.
    856 	 */
    857 	return true;
    858 }
    859 
    860 static bool
    861 sme_battery_critical(envsys_data_t *edata)
    862 {
    863 	if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
    864 	    edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
    865 		return true;
    866 
    867 	return false;
    868 }
    869