Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys_events.c revision 1.44
      1 /* $NetBSD: sysmon_envsys_events.c,v 1.44 2007/11/03 23:05:21 xtraeme Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 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.44 2007/11/03 23:05:21 xtraeme 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/sysctl.h>
     41 #include <sys/systm.h>
     42 #include <sys/proc.h>
     43 #include <sys/mutex.h>
     44 #include <sys/kmem.h>
     45 #include <sys/callout.h>
     46 
     47 /* #define ENVSYS_DEBUG */
     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_SCRITICAL, 	PENVSYS_EVENT_CRITICAL },
     59 	{ ENVSYS_SCRITOVER, 	PENVSYS_EVENT_CRITOVER },
     60 	{ ENVSYS_SCRITUNDER, 	PENVSYS_EVENT_CRITUNDER },
     61 	{ ENVSYS_SWARNOVER, 	PENVSYS_EVENT_WARNOVER },
     62 	{ ENVSYS_SWARNUNDER,	PENVSYS_EVENT_WARNUNDER },
     63 	{ -1, 			-1 }
     64 };
     65 
     66 static struct workqueue *seewq;
     67 static struct callout seeco;
     68 static bool sme_events_initialized = false;
     69 static bool sysmon_low_power = false;
     70 kmutex_t sme_mtx, sme_event_init_mtx;
     71 kcondvar_t sme_cv;
     72 
     73 /* 10 seconds of timeout for the callout */
     74 static int sme_events_timeout = 10;
     75 static int sme_events_timeout_sysctl(SYSCTLFN_PROTO);
     76 #define SME_EVTIMO 	(sme_events_timeout * hz)
     77 
     78 static bool sme_event_check_low_power(void);
     79 static bool sme_battery_critical(envsys_data_t *);
     80 
     81 /*
     82  * sysctl(9) stuff to handle the refresh value in the callout
     83  * function.
     84  */
     85 static int
     86 sme_events_timeout_sysctl(SYSCTLFN_ARGS)
     87 {
     88 	struct sysctlnode node;
     89 	int timo, error;
     90 
     91 	node = *rnode;
     92 	timo = sme_events_timeout;
     93 	node.sysctl_data = &timo;
     94 
     95 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
     96 	if (error || !newp)
     97 		return error;
     98 
     99 	/* min 1s */
    100 	if (timo < 1)
    101 		return EINVAL;
    102 
    103 	sme_events_timeout = timo;
    104 	return 0;
    105 }
    106 
    107 SYSCTL_SETUP(sysctl_kern_envsys_timeout_setup, "sysctl kern.envsys subtree")
    108 {
    109 	const struct sysctlnode *node, *envsys_node;
    110 
    111 	sysctl_createv(clog, 0, NULL, &node,
    112 			CTLFLAG_PERMANENT,
    113 			CTLTYPE_NODE, "kern", NULL,
    114 			NULL, 0, NULL, 0,
    115 			CTL_KERN, CTL_EOL);
    116 
    117 	sysctl_createv(clog, 0, &node, &envsys_node,
    118 			0,
    119 			CTLTYPE_NODE, "envsys", NULL,
    120 			NULL, 0, NULL, 0,
    121 			CTL_CREATE, CTL_EOL);
    122 
    123 	sysctl_createv(clog, 0, &envsys_node, &node,
    124 			CTLFLAG_READWRITE,
    125 			CTLTYPE_INT, "refresh_value",
    126 			SYSCTL_DESCR("wait time in seconds to refresh "
    127 			    "sensors being monitored"),
    128 			sme_events_timeout_sysctl, 0, &sme_events_timeout, 0,
    129 			CTL_CREATE, CTL_EOL);
    130 }
    131 
    132 /*
    133  * sme_event_register:
    134  *
    135  * 	+ Registers a new sysmon envsys event or updates any event
    136  * 	  already in the queue.
    137  */
    138 int
    139 sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
    140 		   const char *drvn, const char *objkey,
    141 		   int32_t critval, int crittype, int powertype)
    142 {
    143 	sme_event_t *see = NULL;
    144 	prop_object_t obj;
    145 	bool critvalup = false;
    146 	int error = 0;
    147 
    148 	KASSERT(sdict != NULL || edata != NULL);
    149 
    150 	mutex_enter(&sme_mtx);
    151 	/*
    152 	 * check if the event is already on the list and return
    153 	 * EEXIST if value provided hasn't been changed.
    154 	 */
    155 	LIST_FOREACH(see, &sme_events_list, see_list) {
    156 		if (strcmp(edata->desc, see->pes.pes_sensname) == 0)
    157 			if (crittype == see->type) {
    158 				if (see->critval == critval) {
    159 					DPRINTF(("%s: dev=%s sensor=%s type=%d "
    160 				    	    "(already exists)\n", __func__,
    161 				    	    see->pes.pes_dvname,
    162 				    	    see->pes.pes_sensname, see->type));
    163 					mutex_exit(&sme_mtx);
    164 					return EEXIST;
    165 				}
    166 				critvalup = true;
    167 				break;
    168 			}
    169 	}
    170 
    171 	/*
    172 	 * Critical condition operation requested by userland.
    173 	 */
    174 	if (objkey && critval && critvalup) {
    175 		obj = prop_dictionary_get(sdict, objkey);
    176 		if (obj) {
    177 			/*
    178 			 * object is already in dictionary and value
    179 			 * provided is not the same than we have
    180 			 * currently,  update the critical value.
    181 			 */
    182 			see->critval = critval;
    183 			DPRINTF(("%s: sensor=%s type=%d (critval updated)\n",
    184 				 __func__, edata->desc, see->type));
    185 			error = sme_sensor_upint32(sdict, objkey, critval);
    186 			mutex_exit(&sme_mtx);
    187 			return error;
    188 		}
    189 	}
    190 
    191 	/*
    192 	 * The event is not in on the list or in a dictionary, create a new
    193 	 * sme event, assign required members and update the object in
    194 	 * the dictionary.
    195 	 */
    196 	see = NULL;
    197 	see = kmem_zalloc(sizeof(*see), KM_NOSLEEP);
    198 	if (see == NULL) {
    199 		mutex_exit(&sme_mtx);
    200 		return ENOMEM;
    201 	}
    202 
    203 	see->critval = critval;
    204 	see->type = crittype;
    205 	(void)strlcpy(see->pes.pes_dvname, drvn,
    206 	    sizeof(see->pes.pes_dvname));
    207 	see->pes.pes_type = powertype;
    208 	(void)strlcpy(see->pes.pes_sensname, edata->desc,
    209 	    sizeof(see->pes.pes_sensname));
    210 	see->snum = edata->sensor;
    211 
    212 	LIST_INSERT_HEAD(&sme_events_list, see, see_list);
    213 	if (objkey && critval) {
    214 		error = sme_sensor_upint32(sdict, objkey, critval);
    215 		if (error) {
    216 			mutex_exit(&sme_mtx);
    217 			goto out;
    218 		}
    219 	}
    220 	DPRINTF(("%s: registering dev=%s sensor=%s snum=%d type=%d "
    221 	    "critval=%" PRIu32 "\n", __func__,
    222 	    see->pes.pes_dvname, see->pes.pes_sensname,
    223 	    see->snum, see->type, see->critval));
    224 	/*
    225 	 * Initialize the events framework if it wasn't initialized
    226 	 * before.
    227 	 */
    228 	mutex_enter(&sme_event_init_mtx);
    229 	mutex_exit(&sme_mtx);
    230 	if (sme_events_initialized == false)
    231 		error = sme_events_init();
    232 	mutex_exit(&sme_event_init_mtx);
    233 out:
    234 	if (error)
    235 		kmem_free(see, sizeof(*see));
    236 	return error;
    237 }
    238 
    239 /*
    240  * sme_event_unregister_all:
    241  *
    242  * 	+ Unregisters all sysmon envsys events associated with a
    243  * 	  sysmon envsys device.
    244  */
    245 void
    246 sme_event_unregister_all(const char *sme_name)
    247 {
    248 	sme_event_t *see;
    249 	int evcounter = 0;
    250 
    251 	KASSERT(mutex_owned(&sme_mtx));
    252 	KASSERT(sme_name != NULL);
    253 
    254 	LIST_FOREACH(see, &sme_events_list, see_list) {
    255 		if (strcmp(see->pes.pes_dvname, sme_name) == 0)
    256 			evcounter++;
    257 	}
    258 
    259 	DPRINTF(("%s: total events %d (%s)\n", __func__,
    260 	    evcounter, sme_name));
    261 
    262 	while ((see = LIST_FIRST(&sme_events_list))) {
    263 		if (evcounter == 0)
    264 			break;
    265 
    266 		if (strcmp(see->pes.pes_dvname, sme_name) == 0) {
    267 			DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
    268 			    see->pes.pes_sensname, see->type, sme_name));
    269 
    270 			while (see->see_flags & SME_EVENT_WORKING)
    271 				cv_wait(&sme_cv, &sme_mtx);
    272 
    273 			LIST_REMOVE(see, see_list);
    274 			kmem_free(see, sizeof(*see));
    275 			evcounter--;
    276 		}
    277 	}
    278 
    279 	if (LIST_EMPTY(&sme_events_list)) {
    280 		mutex_enter(&sme_event_init_mtx);
    281 		if (sme_events_initialized)
    282 			sme_events_destroy();
    283 		mutex_exit(&sme_event_init_mtx);
    284 	}
    285 }
    286 
    287 /*
    288  * sme_event_unregister:
    289  *
    290  * 	+ Unregisters a sysmon envsys event.
    291  */
    292 int
    293 sme_event_unregister(const char *sensor, int type)
    294 {
    295 	sme_event_t *see;
    296 	bool found = false;
    297 
    298 	KASSERT(mutex_owned(&sme_mtx));
    299 	KASSERT(sensor != NULL);
    300 
    301 	LIST_FOREACH(see, &sme_events_list, see_list) {
    302 		if (strcmp(see->pes.pes_sensname, sensor) == 0) {
    303 			if (see->type == type) {
    304 				found = true;
    305 				break;
    306 			}
    307 		}
    308 	}
    309 
    310 	if (!found)
    311 		return EINVAL;
    312 
    313 	while (see->see_flags & SME_EVENT_WORKING)
    314 		cv_wait(&sme_cv, &sme_mtx);
    315 
    316 	DPRINTF(("%s: removing dev=%s sensor=%s type=%d\n",
    317 	    __func__, see->pes.pes_dvname, sensor, type));
    318 	LIST_REMOVE(see, see_list);
    319 	/*
    320 	 * So the events list is empty, we'll do the following:
    321 	 *
    322 	 * 	- stop and destroy the callout.
    323 	 * 	- destroy the workqueue.
    324 	 */
    325 	if (LIST_EMPTY(&sme_events_list)) {
    326 		mutex_enter(&sme_event_init_mtx);
    327 		sme_events_destroy();
    328 		mutex_exit(&sme_event_init_mtx);
    329 	}
    330 
    331 	kmem_free(see, sizeof(*see));
    332 	return 0;
    333 }
    334 
    335 /*
    336  * sme_event_drvadd:
    337  *
    338  * 	+ Adds a new sysmon envsys event for a driver if a sensor
    339  * 	  has set any accepted monitoring flag.
    340  */
    341 void
    342 sme_event_drvadd(void *arg)
    343 {
    344 	sme_event_drv_t *sed_t = arg;
    345 	int error = 0;
    346 
    347 	KASSERT(sed_t != NULL);
    348 
    349 #define SEE_REGEVENT(a, b, c)						\
    350 do {									\
    351 	if (sed_t->edata->flags & (a)) {				\
    352 		char str[ENVSYS_DESCLEN] = "monitoring-state-";		\
    353 									\
    354 		error = sme_event_register(sed_t->sdict,		\
    355 				      sed_t->edata,			\
    356 				      sed_t->sme->sme_name,		\
    357 				      NULL,				\
    358 				      0,				\
    359 				      (b),				\
    360 				      sed_t->powertype);		\
    361 		if (error && error != EEXIST)				\
    362 			printf("%s: failed to add event! "		\
    363 			    "error=%d sensor=%s event=%s\n",		\
    364 			    __func__, error, sed_t->edata->desc, (c));	\
    365 		else {							\
    366 			(void)strlcat(str, (c), sizeof(str));		\
    367 			prop_dictionary_set_bool(sed_t->sdict,		\
    368 						 str,			\
    369 						 true);			\
    370 		}							\
    371 	}								\
    372 } while (/* CONSTCOND */ 0)
    373 
    374 	SEE_REGEVENT(ENVSYS_FMONCRITICAL,
    375 		     PENVSYS_EVENT_CRITICAL,
    376 		     "critical");
    377 
    378 	SEE_REGEVENT(ENVSYS_FMONCRITUNDER,
    379 		     PENVSYS_EVENT_CRITUNDER,
    380 		     "critunder");
    381 
    382 	SEE_REGEVENT(ENVSYS_FMONCRITOVER,
    383 		     PENVSYS_EVENT_CRITOVER,
    384 		     "critover");
    385 
    386 	SEE_REGEVENT(ENVSYS_FMONWARNUNDER,
    387 		     PENVSYS_EVENT_WARNUNDER,
    388 		     "warnunder");
    389 
    390 	SEE_REGEVENT(ENVSYS_FMONWARNOVER,
    391 		     PENVSYS_EVENT_WARNOVER,
    392 		     "warnover");
    393 
    394 	SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
    395 		     PENVSYS_EVENT_STATE_CHANGED,
    396 		     "state-changed");
    397 
    398 	/* we are done, free memory now */
    399 	kmem_free(sed_t, sizeof(*sed_t));
    400 }
    401 
    402 /*
    403  * sme_events_init:
    404  *
    405  * 	+ Initializes the events framework.
    406  */
    407 int
    408 sme_events_init(void)
    409 {
    410 	int error;
    411 
    412 	KASSERT(mutex_owned(&sme_event_init_mtx));
    413 
    414 	error = workqueue_create(&seewq, "envsysev",
    415 	    sme_events_worker, NULL, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
    416 	if (error)
    417 		goto out;
    418 
    419 	callout_init(&seeco, 0);
    420 	callout_setfunc(&seeco, sme_events_check, NULL);
    421 	callout_schedule(&seeco, SME_EVTIMO);
    422 	sme_events_initialized = true;
    423 	DPRINTF(("%s: events framework initialized\n", __func__));
    424 
    425 out:
    426 	return error;
    427 }
    428 
    429 /*
    430  * sme_events_destroy:
    431  *
    432  * 	+ Destroys the events framework: the workqueue and the
    433  * 	  callout are stopped and destroyed because there are not
    434  * 	  events in the queue.
    435  */
    436 void
    437 sme_events_destroy(void)
    438 {
    439 	KASSERT(mutex_owned(&sme_event_init_mtx));
    440 
    441 	callout_stop(&seeco);
    442 	sme_events_initialized = false;
    443 	DPRINTF(("%s: events framework destroyed\n", __func__));
    444 	callout_destroy(&seeco);
    445 	workqueue_destroy(seewq);
    446 }
    447 
    448 /*
    449  * sme_events_check:
    450  *
    451  * 	+ Runs the work on each sysmon envsys event in our
    452  * 	  workqueue periodically with callout.
    453  */
    454 void
    455 sme_events_check(void *arg)
    456 {
    457 	sme_event_t *see;
    458 
    459 	LIST_FOREACH(see, &sme_events_list, see_list) {
    460 		DPRINTFOBJ(("%s: dev=%s sensor=%s type=%d\n",
    461 		    __func__,
    462 		    see->pes.pes_dvname,
    463 		    see->pes.pes_sensname,
    464 		    see->type));
    465 		workqueue_enqueue(seewq, &see->see_wk, NULL);
    466 	}
    467 	/*
    468 	 * Now that the events list was checked, reset the refresh value.
    469 	 */
    470 	LIST_FOREACH(see, &sme_events_list, see_list)
    471 		see->see_flags &= ~SME_EVENT_REFRESHED;
    472 
    473 	if (!sysmon_low_power)
    474 		callout_schedule(&seeco, SME_EVTIMO);
    475 }
    476 
    477 /*
    478  * sme_events_worker:
    479  *
    480  * 	+ workqueue thread that checks if there's a critical condition
    481  * 	  and sends an event if the condition was triggered.
    482  */
    483 void
    484 sme_events_worker(struct work *wk, void *arg)
    485 {
    486 	const struct sme_description_table *sdt = NULL;
    487 	const struct sme_sensor_event *sse = sme_sensor_event;
    488 	sme_event_t *see = (void *)wk;
    489 	struct sysmon_envsys *sme;
    490 	envsys_data_t *edata;
    491 	int i, state, error;
    492 
    493 	KASSERT(wk == &see->see_wk);
    494 
    495 	state = error = 0;
    496 
    497 	mutex_enter(&sme_mtx);
    498 	see->see_flags |= SME_EVENT_WORKING;
    499 
    500 	/*
    501 	 * We have to find the sme device by looking
    502 	 * at the power envsys device name.
    503 	 */
    504 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
    505 		if (strcmp(sme->sme_name, see->pes.pes_dvname) == 0)
    506 			break;
    507 	if (!sme)
    508 		goto out;
    509 
    510 	/* get the sensor with the index specified in see->snum */
    511 	edata = &sme->sme_sensor_data[see->snum];
    512 
    513 	/*
    514 	 * refresh the sensor that was marked with a critical event
    515 	 * only if it wasn't refreshed before or if the driver doesn't
    516 	 * use its own method for refreshing.
    517 	 */
    518 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    519 		if ((see->see_flags & SME_EVENT_REFRESHED) == 0) {
    520 			error = (*sme->sme_gtredata)(sme, edata);
    521 			if (error)
    522 				goto out;
    523 			see->see_flags |= SME_EVENT_REFRESHED;
    524 		}
    525 	}
    526 
    527 	DPRINTFOBJ(("%s: desc=%s sensor=%d units=%d value_cur=%d\n",
    528 	    __func__, edata->desc, edata->sensor,
    529 	    edata->units, edata->value_cur));
    530 
    531 #define SME_SEND_NORMALEVENT()						\
    532 do {									\
    533 	see->evsent = false;						\
    534 	sysmon_penvsys_event(&see->pes, PENVSYS_EVENT_NORMAL);		\
    535 } while (/* CONSTCOND */ 0)
    536 
    537 #define SME_SEND_EVENT(type)						\
    538 do {									\
    539 	see->evsent = true;						\
    540 	sysmon_penvsys_event(&see->pes, (type));			\
    541 } while (/* CONSTCOND */ 0)
    542 
    543 
    544 	switch (see->type) {
    545 	/*
    546 	 * if state is the same than the one that matches sse[i].state,
    547 	 * send the event...
    548 	 */
    549 	case PENVSYS_EVENT_CRITICAL:
    550 	case PENVSYS_EVENT_CRITUNDER:
    551 	case PENVSYS_EVENT_CRITOVER:
    552 	case PENVSYS_EVENT_WARNUNDER:
    553 	case PENVSYS_EVENT_WARNOVER:
    554 		for (i = 0; sse[i].state != -1; i++)
    555 			if (sse[i].event == see->type)
    556 				break;
    557 
    558 		if (see->evsent && edata->state == ENVSYS_SVALID)
    559 			SME_SEND_NORMALEVENT();
    560 
    561 		if (!see->evsent && edata->state == sse[i].state)
    562 			SME_SEND_EVENT(see->type);
    563 
    564 		break;
    565 	/*
    566 	 * if value_cur is lower than the limit, send the event...
    567 	 */
    568 	case PENVSYS_EVENT_BATT_USERCAP:
    569 	case PENVSYS_EVENT_USER_CRITMIN:
    570 		if (see->evsent && edata->value_cur > see->critval)
    571 			SME_SEND_NORMALEVENT();
    572 
    573 		if (!see->evsent && edata->value_cur < see->critval)
    574 			SME_SEND_EVENT(see->type);
    575 
    576 		break;
    577 	/*
    578 	 * if value_cur is higher than the limit, send the event...
    579 	 */
    580 	case PENVSYS_EVENT_USER_CRITMAX:
    581 		if (see->evsent && edata->value_cur < see->critval)
    582 			SME_SEND_NORMALEVENT();
    583 
    584 		if (!see->evsent && edata->value_cur > see->critval)
    585 			SME_SEND_EVENT(see->type);
    586 
    587 		break;
    588 	/*
    589 	 * if value_cur is not normal (battery) or online (drive),
    590 	 * send the event...
    591 	 */
    592 	case PENVSYS_EVENT_STATE_CHANGED:
    593 		/* the state has not been changed, just ignore the event */
    594 		if (edata->value_cur == see->evsent)
    595 			break;
    596 
    597 		switch (edata->units) {
    598 		case ENVSYS_DRIVE:
    599 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
    600 			state = ENVSYS_DRIVE_ONLINE;
    601 			break;
    602 		case ENVSYS_BATTERY_CAPACITY:
    603 			sdt =
    604 			    sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
    605 			state = ENVSYS_BATTERY_CAPACITY_NORMAL;
    606 			break;
    607 		default:
    608 			panic("%s: invalid units for ENVSYS_FMONSTCHANGED",
    609 			    __func__);
    610 		}
    611 
    612 		for (i = 0; sdt[i].type != -1; i++)
    613 			if (sdt[i].type == edata->value_cur)
    614 				break;
    615 
    616 		/* copy current state description  */
    617 		(void)strlcpy(see->pes.pes_statedesc, sdt[i].desc,
    618 		    sizeof(see->pes.pes_statedesc));
    619 
    620 		/* state is ok again... send a normal event */
    621 		if (see->evsent && edata->value_cur == state)
    622 			SME_SEND_NORMALEVENT();
    623 
    624 		/* state has been changed... send event */
    625 		if (see->evsent || edata->value_cur != state) {
    626 			/* save current drive state */
    627 			see->evsent = edata->value_cur;
    628 			sysmon_penvsys_event(&see->pes, see->type);
    629 		}
    630 
    631 		/*
    632 		 * Check if the system is running in low power and send the
    633 		 * event to powerd (if running) or shutdown the system
    634 		 * otherwise.
    635 		 */
    636 		if (!sysmon_low_power && sme_event_check_low_power()) {
    637 			struct penvsys_state pes;
    638 
    639 			pes.pes_type = PENVSYS_TYPE_BATTERY;
    640 			sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
    641 			sysmon_low_power = true;
    642 			callout_stop(&seeco);
    643 		}
    644 
    645 		break;
    646 	}
    647 out:
    648 	see->see_flags &= ~SME_EVENT_WORKING;
    649 	cv_broadcast(&sme_cv);
    650 	mutex_exit(&sme_mtx);
    651 }
    652 
    653 static bool
    654 sme_event_check_low_power(void)
    655 {
    656 	struct sysmon_envsys *sme;
    657 	envsys_data_t *edata;
    658 	int i;
    659 	bool battery, batterycap, batterycharge;
    660 
    661 	KASSERT(mutex_owned(&sme_mtx));
    662 
    663 	battery = batterycap = batterycharge = false;
    664 
    665 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
    666 		if (sme->sme_class == SME_CLASS_ACADAPTER)
    667 			break;
    668 
    669 	/*
    670 	 * No AC Adapter devices were found, do nothing.
    671 	 */
    672 	if (!sme)
    673 		return false;
    674 
    675 	/*
    676 	 * If there's an AC Adapter connected, there's no need
    677 	 * to continue...
    678 	 */
    679 	for (i = 0; i < sme->sme_nsensors; i++) {
    680 		edata = &sme->sme_sensor_data[i];
    681 		if (edata->units == ENVSYS_INDICATOR) {
    682 			if (edata->value_cur)
    683 				return false;
    684 		}
    685 	}
    686 
    687 	/*
    688 	 * Check for battery devices and its state.
    689 	 */
    690 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    691 		if (sme->sme_class != SME_CLASS_BATTERY)
    692 			continue;
    693 
    694 		/*
    695 		 * We've found a battery device...
    696 		 */
    697 		battery = true;
    698 		for (i = 0; i < sme->sme_nsensors; i++) {
    699 			edata = &sme->sme_sensor_data[i];
    700 			if (edata->units == ENVSYS_BATTERY_CAPACITY) {
    701 				batterycap = true;
    702 				if (!sme_battery_critical(edata))
    703 					return false;
    704 			} else if (edata->units == ENVSYS_BATTERY_CHARGE) {
    705 				batterycharge = true;
    706 				if (edata->value_cur)
    707 					return false;
    708 			}
    709 		}
    710 	}
    711 	if (!battery || !batterycap || !batterycharge)
    712 		return false;
    713 
    714 	/*
    715 	 * All batteries in low/critical capacity and discharging.
    716 	 */
    717 	return true;
    718 }
    719 
    720 static bool
    721 sme_battery_critical(envsys_data_t *edata)
    722 {
    723 	if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
    724 	    edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
    725 		return true;
    726 
    727 	return false;
    728 }
    729