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