Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys_events.c revision 1.73
      1 /* $NetBSD: sysmon_envsys_events.c,v 1.73 2010/01/01 15:41:25 pgoyette 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.73 2010/01/01 15:41:25 pgoyette 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 	{ ENVSYS_BATTERY_CAPACITY_NORMAL,	PENVSYS_EVENT_NORMAL },
     63 	{ ENVSYS_BATTERY_CAPACITY_WARNING,	PENVSYS_EVENT_BATT_WARN },
     64 	{ ENVSYS_BATTERY_CAPACITY_CRITICAL,	PENVSYS_EVENT_BATT_CRIT },
     65 	{ -1, 					-1 }
     66 };
     67 
     68 static bool sysmon_low_power;
     69 
     70 #define SME_EVTIMO	(SME_EVENTS_DEFTIMEOUT * hz)
     71 
     72 static bool sme_event_check_low_power(void);
     73 static bool sme_battery_check(void);
     74 static bool sme_battery_critical(envsys_data_t *);
     75 static bool sme_acadapter_check(void);
     76 
     77 /*
     78  * sme_event_register:
     79  *
     80  * 	+ Registers a new sysmon envsys event or updates any event
     81  * 	  already in the queue.
     82  */
     83 int
     84 sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
     85 		   struct sysmon_envsys *sme, sysmon_envsys_lim_t *lims,
     86 		   int crittype, int powertype)
     87 {
     88 	sme_event_t *see = NULL, *osee = NULL;
     89 	prop_object_t obj;
     90 	int error = 0;
     91 	const char *objkey;
     92 
     93 	KASSERT(sdict != NULL || edata != NULL || sme != NULL);
     94 
     95 	/*
     96 	 * check if the event is already on the list and return
     97 	 * EEXIST if value provided hasn't been changed.
     98 	 */
     99 	mutex_enter(&sme->sme_mtx);
    100 	LIST_FOREACH(osee, &sme->sme_events_list, see_list) {
    101 		if (strcmp(edata->desc, osee->see_pes.pes_sensname) != 0)
    102 			continue;
    103 		if (crittype != osee->see_type)
    104 			continue;
    105 
    106 		DPRINTF(("%s: dev %s sensor %s lim_flags 0x%04x event exists\n",
    107 		    __func__, sme->sme_name, edata->desc, lims->sel_flags));
    108 
    109 		see = osee;
    110 		if (lims->sel_flags & PROP_CRITMAX) {
    111 			if (lims->sel_critmax == see->see_lims.sel_critmax) {
    112 				DPRINTF(("%s: type=%d (critmax exists)\n",
    113 				    __func__, crittype));
    114 				error = EEXIST;
    115 				lims->sel_flags &= ~PROP_CRITMAX;
    116 			}
    117 		}
    118 		if (lims->sel_flags & PROP_WARNMAX) {
    119 			if (lims->sel_warnmax == see->see_lims.sel_warnmax) {
    120 				DPRINTF(("%s: type=%d (warnmax exists)\n",
    121 				    __func__, crittype));
    122 				error = EEXIST;
    123 				lims->sel_flags &= ~PROP_WARNMAX;
    124 			}
    125 		}
    126 		if (lims->sel_flags & (PROP_WARNMIN | PROP_BATTWARN)) {
    127 			if (lims->sel_warnmin == see->see_lims.sel_warnmin) {
    128 				DPRINTF(("%s: type=%d (warnmin exists)\n",
    129 				    __func__, crittype));
    130 				error = EEXIST;
    131 				lims->sel_flags &= ~(PROP_WARNMIN | PROP_BATTWARN);
    132 			}
    133 		}
    134 		if (lims->sel_flags & (PROP_CRITMIN | PROP_BATTCAP)) {
    135 			if (lims->sel_critmin == see->see_lims.sel_critmin) {
    136 				DPRINTF(("%s: type=%d (critmin exists)\n",
    137 				    __func__, crittype));
    138 				error = EEXIST;
    139 				lims->sel_flags &= ~(PROP_CRITMIN | PROP_BATTCAP);
    140 			}
    141 		}
    142 		break;
    143 	}
    144 	if (see == NULL) {
    145 		/*
    146 		 * New event requested - allocate a sysmon_envsys event.
    147 		 */
    148 		see = kmem_zalloc(sizeof(*see), KM_SLEEP);
    149 		if (see == NULL)
    150 			return ENOMEM;
    151 
    152 		DPRINTF(("%s: dev %s sensor %s lim_flags 0x%04x new event\n",
    153 		    __func__, sme->sme_name, edata->desc, lims->sel_flags));
    154 
    155 		see->see_type = crittype;
    156 		see->see_sme = sme;
    157 		see->see_edata = edata;
    158 
    159 		/* Initialize sensor type and previously-sent state */
    160 
    161 		see->see_pes.pes_type = powertype;
    162 
    163 		switch (crittype) {
    164 		case PENVSYS_EVENT_LIMITS:
    165 			see->see_evsent = ENVSYS_SVALID;
    166 			break;
    167 		case PENVSYS_EVENT_CAPACITY:
    168 			see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
    169 			break;
    170 		case PENVSYS_EVENT_STATE_CHANGED:
    171 			if (edata->units == ENVSYS_BATTERY_CAPACITY)
    172 				see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
    173 			else if (edata->units == ENVSYS_DRIVE)
    174 				see->see_evsent = ENVSYS_DRIVE_EMPTY;
    175 			else
    176 				panic("%s: bad units for "
    177 				      "PENVSYS_EVENT_STATE_CHANGED", __func__);
    178 			break;
    179 		case PENVSYS_EVENT_CRITICAL:
    180 		default:
    181 			see->see_evsent = 0;
    182 			break;
    183 		}
    184 
    185 		(void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
    186 		    sizeof(see->see_pes.pes_dvname));
    187 		(void)strlcpy(see->see_pes.pes_sensname, edata->desc,
    188 		    sizeof(see->see_pes.pes_sensname));
    189 	}
    190 
    191 	/*
    192 	 * Limit operation requested.
    193 	 */
    194 	if (lims->sel_flags & PROP_CRITMAX) {
    195 		objkey = "critical-max";
    196 		obj = prop_dictionary_get(sdict, objkey);
    197 		if (obj && prop_object_type(obj) != PROP_TYPE_NUMBER) {
    198 			DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
    199 			    __func__, sme->sme_name, objkey));
    200 			error = ENOTSUP;
    201 		} else {
    202 			see->see_lims.sel_critmax = lims->sel_critmax;
    203 			error = sme_sensor_upint32(sdict, objkey,
    204 						   lims->sel_critmax);
    205 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
    206 			    "(%s updated)\n", __func__, sme->sme_name,
    207 			    edata->desc, crittype, objkey));
    208 		}
    209 		if (error && error != EEXIST)
    210 			goto out;
    211 		see->see_edata->upropset |= PROP_CRITMAX;
    212 	}
    213 
    214 	if (lims->sel_flags & PROP_WARNMAX) {
    215 		objkey = "warning-max";
    216 		obj = prop_dictionary_get(sdict, objkey);
    217 		if (obj && prop_object_type(obj) != PROP_TYPE_NUMBER) {
    218 			DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
    219 			    __func__, sme->sme_name, objkey));
    220 			error = ENOTSUP;
    221 		} else {
    222 			see->see_lims.sel_warnmax = lims->sel_warnmax;
    223 			error = sme_sensor_upint32(sdict, objkey,
    224 						   lims->sel_warnmax);
    225 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
    226 			    "(%s updated)\n", __func__, sme->sme_name,
    227 			    edata->desc, crittype, objkey));
    228 		}
    229 		if (error && error != EEXIST)
    230 			goto out;
    231 		see->see_edata->upropset |= PROP_WARNMAX;
    232 	}
    233 
    234 	if (lims->sel_flags & PROP_WARNMIN) {
    235 		objkey = "warning-min";
    236 		obj = prop_dictionary_get(sdict, objkey);
    237 		if (obj && prop_object_type(obj) != PROP_TYPE_NUMBER) {
    238 			DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
    239 			    __func__, sme->sme_name, objkey));
    240 			error = ENOTSUP;
    241 		} else {
    242 			see->see_lims.sel_warnmin = lims->sel_warnmin;
    243 			error = sme_sensor_upint32(sdict, objkey,
    244 						   lims->sel_warnmin);
    245 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
    246 			    "(%s updated)\n", __func__, sme->sme_name,
    247 			    edata->desc, crittype, objkey));
    248 		}
    249 		if (error && error != EEXIST)
    250 			goto out;
    251 		see->see_edata->upropset |= PROP_WARNMIN;
    252 	}
    253 
    254 	if (lims->sel_flags & PROP_CRITMIN) {
    255 		objkey = "critical-min";
    256 		obj = prop_dictionary_get(sdict, objkey);
    257 		if (obj && prop_object_type(obj) != PROP_TYPE_NUMBER) {
    258 			DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
    259 			    __func__, sme->sme_name, objkey));
    260 			error = ENOTSUP;
    261 		} else {
    262 			see->see_lims.sel_critmin = lims->sel_critmin;
    263 			error = sme_sensor_upint32(sdict, objkey,
    264 						   lims->sel_critmin);
    265 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
    266 			    "(%s updated)\n", __func__, sme->sme_name,
    267 			    edata->desc, crittype, objkey));
    268 		}
    269 		if (error && error != EEXIST)
    270 			goto out;
    271 		see->see_edata->upropset |= PROP_CRITMIN;
    272 	}
    273 
    274 	if (lims->sel_flags & PROP_BATTWARN) {
    275 		objkey = "warning-capacity";
    276 		obj = prop_dictionary_get(sdict, objkey);
    277 		if (obj && prop_object_type(obj) != PROP_TYPE_NUMBER) {
    278 			DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
    279 			    __func__, sme->sme_name, objkey));
    280 			error = ENOTSUP;
    281 		} else {
    282 			see->see_lims.sel_warnmin = lims->sel_warnmin;
    283 			error = sme_sensor_upint32(sdict, objkey,
    284 						   lims->sel_warnmin);
    285 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
    286 			    "(%s updated)\n", __func__, sme->sme_name,
    287 			    edata->desc, crittype, objkey));
    288 		}
    289 		if (error && error != EEXIST)
    290 			goto out;
    291 		see->see_edata->upropset |= PROP_BATTWARN;
    292 	}
    293 
    294 	if (lims->sel_flags & PROP_BATTCAP) {
    295 		objkey = "critical-capacity";
    296 		obj = prop_dictionary_get(sdict, objkey);
    297 		if (obj && prop_object_type(obj) != PROP_TYPE_NUMBER) {
    298 			DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
    299 			    __func__, sme->sme_name, objkey));
    300 			error = ENOTSUP;
    301 		} else {
    302 			see->see_lims.sel_critmin = lims->sel_critmin;
    303 			error = sme_sensor_upint32(sdict, objkey,
    304 						   lims->sel_critmin);
    305 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
    306 			    "(%s updated)\n", __func__, sme->sme_name,
    307 			    edata->desc, crittype, objkey));
    308 		}
    309 		if (error && error != EEXIST)
    310 			goto out;
    311 		see->see_edata->upropset |= PROP_BATTCAP;
    312 	}
    313 
    314 	DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d "
    315 	    "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32
    316 	    " critmax=%" PRIu32 " props 0x%04x)\n", __func__,
    317 	    see->see_sme->sme_name, see->see_pes.pes_sensname,
    318 	    see->see_edata->sensor, see->see_type, see->see_lims.sel_critmin,
    319 	    see->see_lims.sel_warnmin, see->see_lims.sel_warnmax,
    320 	    see->see_lims.sel_critmax, see->see_edata->upropset));
    321 	/*
    322 	 * Initialize the events framework if it wasn't initialized before.
    323 	 */
    324 	if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
    325 		error = sme_events_init(sme);
    326 
    327 	/*
    328 	 * If driver requested notification, advise it of new
    329 	 * limit values
    330 	 */
    331 	if (sme->sme_set_limits) {
    332 		see->see_lims.sel_flags = see->see_edata->upropset &
    333 					  PROP_LIMITS;
    334 		(*sme->sme_set_limits)(sme, edata, &(see->see_lims));
    335 	}
    336 
    337 out:
    338 	if ((error == 0 || error == EEXIST) && osee == NULL)
    339 		LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
    340 
    341 	mutex_exit(&sme->sme_mtx);
    342 
    343 	return error;
    344 }
    345 
    346 /*
    347  * sme_event_unregister_all:
    348  *
    349  * 	+ Unregisters all events associated with a sysmon envsys device.
    350  */
    351 void
    352 sme_event_unregister_all(struct sysmon_envsys *sme)
    353 {
    354 	sme_event_t *see;
    355 	int evcounter = 0;
    356 
    357 	KASSERT(sme != NULL);
    358 
    359 	mutex_enter(&sme->sme_mtx);
    360 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    361 		while (see->see_flags & SEE_EVENT_WORKING)
    362 			cv_wait(&sme->sme_condvar, &sme->sme_mtx);
    363 
    364 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
    365 			evcounter++;
    366 	}
    367 
    368 	DPRINTF(("%s: total events %d (%s)\n", __func__,
    369 	    evcounter, sme->sme_name));
    370 
    371 	while ((see = LIST_FIRST(&sme->sme_events_list))) {
    372 		if (evcounter == 0)
    373 			break;
    374 
    375 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
    376 			LIST_REMOVE(see, see_list);
    377 			DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
    378 			    see->see_pes.pes_sensname, see->see_type,
    379 			    sme->sme_name));
    380 			kmem_free(see, sizeof(*see));
    381 			evcounter--;
    382 		}
    383 	}
    384 
    385 	if (LIST_EMPTY(&sme->sme_events_list))
    386 		if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
    387 			sme_events_destroy(sme);
    388 	mutex_exit(&sme->sme_mtx);
    389 }
    390 
    391 /*
    392  * sme_event_unregister:
    393  *
    394  * 	+ Unregisters an event from the specified sysmon envsys device.
    395  */
    396 int
    397 sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
    398 {
    399 	sme_event_t *see;
    400 	bool found = false;
    401 
    402 	KASSERT(sensor != NULL);
    403 
    404 	mutex_enter(&sme->sme_mtx);
    405 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    406 		if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
    407 			if (see->see_type == type) {
    408 				found = true;
    409 				break;
    410 			}
    411 		}
    412 	}
    413 
    414 	if (!found) {
    415 		mutex_exit(&sme->sme_mtx);
    416 		return EINVAL;
    417 	}
    418 
    419 	/*
    420 	 * Wait for the event to finish its work, remove from the list
    421 	 * and release resouces.
    422 	 */
    423 	while (see->see_flags & SEE_EVENT_WORKING)
    424 		cv_wait(&sme->sme_condvar, &sme->sme_mtx);
    425 
    426 	DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n",
    427 	    __func__, see->see_pes.pes_dvname, sensor, type));
    428 	LIST_REMOVE(see, see_list);
    429 	/*
    430 	 * So the events list is empty, we'll do the following:
    431 	 *
    432 	 * 	- stop and destroy the callout.
    433 	 * 	- destroy the workqueue.
    434 	 */
    435 	if (LIST_EMPTY(&sme->sme_events_list))
    436 		sme_events_destroy(sme);
    437 	mutex_exit(&sme->sme_mtx);
    438 
    439 	kmem_free(see, sizeof(*see));
    440 	return 0;
    441 }
    442 
    443 /*
    444  * sme_event_drvadd:
    445  *
    446  * 	+ Registers a new event for a device that had enabled any of
    447  * 	  the monitoring flags in the driver.
    448  */
    449 void
    450 sme_event_drvadd(void *arg)
    451 {
    452 	sme_event_drv_t *sed_t = arg;
    453 	sysmon_envsys_lim_t lims;
    454 	int error = 0;
    455 
    456 	KASSERT(sed_t != NULL);
    457 
    458 #define SEE_REGEVENT(a, b, c)						\
    459 do {									\
    460 	if (sed_t->sed_edata->flags & (a)) {				\
    461 		char str[ENVSYS_DESCLEN] = "monitoring-state-";		\
    462 									\
    463 		error = sme_event_register(sed_t->sed_sdict,		\
    464 				      sed_t->sed_edata,			\
    465 				      sed_t->sed_sme,			\
    466 				      &lims,				\
    467 				      (b),				\
    468 				      sed_t->sed_powertype);		\
    469 		if (error && error != EEXIST)				\
    470 			printf("%s: failed to add event! "		\
    471 			    "error=%d sensor=%s event=%s\n",		\
    472 			    __func__, error,				\
    473 			    sed_t->sed_edata->desc, (c));		\
    474 		else {							\
    475 			(void)strlcat(str, (c), sizeof(str));		\
    476 			prop_dictionary_set_bool(sed_t->sed_sdict,	\
    477 						 str,			\
    478 						 true);			\
    479 		}							\
    480 	}								\
    481 } while (/* CONSTCOND */ 0)
    482 
    483 	/*
    484 	 * If driver provides a method to retrieve its internal limit
    485 	 * values, call it and use thoe returned values as initial
    486 	 * limits for event monitoring.
    487 	 */
    488 	lims.sel_flags = 0;
    489 	if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS)
    490 		if (sed_t->sed_sme->sme_get_limits)
    491 			(*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme,
    492 							  sed_t->sed_edata,
    493 							  &lims);
    494 	/*
    495 	 * If no values returned, don't create the event monitor at
    496 	 * this time.  We'll get another chance later when the user
    497 	 * provides us with limits.
    498 	 */
    499 	if (lims.sel_flags == 0)
    500 		sed_t->sed_edata->flags &= ~ENVSYS_FMONLIMITS;
    501 
    502 	/*
    503 	 * If driver doesn't provide a way to "absorb" user-specified
    504 	 * limit values, we must monitor all limits ourselves
    505 	 */
    506 	else if (sed_t->sed_sme->sme_get_limits == NULL)
    507 		lims.sel_flags |= PROP_DRIVER_LIMITS;
    508 
    509 	/* Register the events that were specified */
    510 
    511 	SEE_REGEVENT(ENVSYS_FMONCRITICAL,
    512 		     PENVSYS_EVENT_CRITICAL,
    513 		     "critical");
    514 
    515 	SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
    516 		     PENVSYS_EVENT_STATE_CHANGED,
    517 		     "state-changed");
    518 
    519 	SEE_REGEVENT(ENVSYS_FMONLIMITS,
    520 		     PENVSYS_EVENT_LIMITS,
    521 		     "hw-range-limits");
    522 
    523 	/*
    524 	 * we are done, free memory now.
    525 	 */
    526 	kmem_free(sed_t, sizeof(*sed_t));
    527 }
    528 
    529 /*
    530  * sme_events_init:
    531  *
    532  * 	+ Initialize the events framework for this device.
    533  */
    534 int
    535 sme_events_init(struct sysmon_envsys *sme)
    536 {
    537 	int error = 0;
    538 	uint64_t timo;
    539 
    540 	KASSERT(sme != NULL);
    541 	KASSERT(mutex_owned(&sme->sme_mtx));
    542 
    543 	if (sme->sme_events_timeout)
    544 		timo = sme->sme_events_timeout * hz;
    545 	else
    546 		timo = SME_EVTIMO;
    547 
    548 	error = workqueue_create(&sme->sme_wq, sme->sme_name,
    549 	    sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
    550 	if (error)
    551 		return error;
    552 
    553 	mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    554 	callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
    555 	callout_setfunc(&sme->sme_callout, sme_events_check, sme);
    556 	callout_schedule(&sme->sme_callout, timo);
    557 	sme->sme_flags |= SME_CALLOUT_INITIALIZED;
    558 	DPRINTF(("%s: events framework initialized for '%s'\n",
    559 	    __func__, sme->sme_name));
    560 
    561 	return error;
    562 }
    563 
    564 /*
    565  * sme_events_destroy:
    566  *
    567  * 	+ Destroys the event framework for this device: callout
    568  * 	  stopped, workqueue destroyed and callout mutex destroyed.
    569  */
    570 void
    571 sme_events_destroy(struct sysmon_envsys *sme)
    572 {
    573 	KASSERT(mutex_owned(&sme->sme_mtx));
    574 
    575 	callout_stop(&sme->sme_callout);
    576 	workqueue_destroy(sme->sme_wq);
    577 	mutex_destroy(&sme->sme_callout_mtx);
    578 	callout_destroy(&sme->sme_callout);
    579 	sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
    580 	DPRINTF(("%s: events framework destroyed for '%s'\n",
    581 	    __func__, sme->sme_name));
    582 }
    583 
    584 /*
    585  * sme_events_check:
    586  *
    587  * 	+ Passes the events to the workqueue thread and stops
    588  * 	  the callout if the 'low-power' condition is triggered.
    589  */
    590 void
    591 sme_events_check(void *arg)
    592 {
    593 	struct sysmon_envsys *sme = arg;
    594 	sme_event_t *see;
    595 	uint64_t timo;
    596 
    597 	KASSERT(sme != NULL);
    598 
    599 	mutex_enter(&sme->sme_callout_mtx);
    600 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    601 		workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
    602 		see->see_edata->flags |= ENVSYS_FNEED_REFRESH;
    603 	}
    604 	if (sme->sme_events_timeout)
    605 		timo = sme->sme_events_timeout * hz;
    606 	else
    607 		timo = SME_EVTIMO;
    608 	if (!sysmon_low_power)
    609 		callout_schedule(&sme->sme_callout, timo);
    610 	mutex_exit(&sme->sme_callout_mtx);
    611 }
    612 
    613 /*
    614  * sme_events_worker:
    615  *
    616  * 	+ workqueue thread that checks if there's a critical condition
    617  * 	  and sends an event if it was triggered.
    618  */
    619 void
    620 sme_events_worker(struct work *wk, void *arg)
    621 {
    622 	const struct sme_description_table *sdt = NULL;
    623 	const struct sme_sensor_event *sse = sme_sensor_event;
    624 	sme_event_t *see = (void *)wk;
    625 	struct sysmon_envsys *sme = see->see_sme;
    626 	envsys_data_t *edata = see->see_edata;
    627 	int i, state = 0;
    628 
    629 	KASSERT(wk == &see->see_wk);
    630 	KASSERT(sme != NULL || edata != NULL);
    631 
    632 	mutex_enter(&sme->sme_mtx);
    633 	if ((see->see_flags & SEE_EVENT_WORKING) == 0)
    634 		see->see_flags |= SEE_EVENT_WORKING;
    635 	/*
    636 	 * sme_events_check marks the sensors to make us refresh them here.
    637 	 * Don't refresh if the driver uses its own method for refreshing.
    638 	 */
    639 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
    640 		if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) {
    641 			/* refresh sensor in device */
    642 			(*sme->sme_refresh)(sme, edata);
    643 			edata->flags &= ~ENVSYS_FNEED_REFRESH;
    644 		}
    645 	}
    646 
    647 	DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
    648 	    "value_cur=%d\n", __func__, sme->sme_name, edata->desc,
    649 	    edata->sensor, see->see_type, edata->state, edata->units,
    650 	    edata->value_cur));
    651 
    652 	/* skip the event if current sensor is in invalid state */
    653 	if (edata->state == ENVSYS_SINVALID)
    654 		goto out;
    655 
    656 	switch (see->see_type) {
    657 	/*
    658 	 * For range limits, if the driver claims responsibility for
    659 	 * limit/range checking, just user driver-supplied status.
    660 	 * Else calculate our own status.  Note that driver must
    661 	 * relinquish responsibility for ALL limits if there is even
    662 	 * one limit that it cannot handle!
    663 	 */
    664 	case PENVSYS_EVENT_LIMITS:
    665 	case PENVSYS_EVENT_CAPACITY:
    666 #define	__EXCEED_LIM(valid, lim, rel) \
    667 		((see->see_lims.sel_flags & (valid)) && \
    668 		 (edata->value_cur rel (see->see_lims.lim)))
    669 
    670 		if ((see->see_lims.sel_flags & PROP_DRIVER_LIMITS) == 0) {
    671 			if __EXCEED_LIM(PROP_CRITMIN | PROP_BATTCAP,
    672 					sel_critmin, <)
    673 				edata->state = ENVSYS_SCRITUNDER;
    674 			else if __EXCEED_LIM(PROP_WARNMIN | PROP_BATTWARN,
    675 					sel_warnmin, <)
    676 				edata->state = ENVSYS_SWARNUNDER;
    677 			else if __EXCEED_LIM(PROP_CRITMAX, sel_critmax, >)
    678 				edata->state = ENVSYS_SCRITOVER;
    679 			else if __EXCEED_LIM(PROP_WARNMAX, sel_warnmax, >)
    680 				edata->state = ENVSYS_SWARNOVER;
    681 		}
    682 #undef	__EXCEED_LIM
    683 
    684 		/*
    685 		 * Send event if state has changed
    686 		 */
    687 		if (edata->state == see->see_evsent)
    688 			break;
    689 
    690 		for (i = 0; sse[i].state != -1; i++)
    691 			if (sse[i].state == edata->state)
    692 				break;
    693 
    694 		if (sse[i].state == -1)
    695 			break;
    696 
    697 		if (edata->state == ENVSYS_SVALID)
    698 			sysmon_penvsys_event(&see->see_pes,
    699 					     PENVSYS_EVENT_NORMAL);
    700 		else
    701 			sysmon_penvsys_event(&see->see_pes, sse[i].event);
    702 
    703 		see->see_evsent = edata->state;
    704 
    705 		break;
    706 
    707 	/*
    708 	 * Send PENVSYS_EVENT_CRITICAL event if:
    709 	 *	State has gone from non-CRITICAL to CRITICAL,
    710 	 *	State remains CRITICAL and value has changed, or
    711 	 *	State has returned from CRITICAL to non-CRITICAL
    712 	 */
    713 	case PENVSYS_EVENT_CRITICAL:
    714 		if (edata->state == ENVSYS_SVALID &&
    715 		    see->see_evsent != 0) {
    716 			sysmon_penvsys_event(&see->see_pes,
    717 					     PENVSYS_EVENT_NORMAL);
    718 			see->see_evsent = 0;
    719 		} else if (edata->state == ENVSYS_SCRITICAL &&
    720 		    see->see_evsent != edata->value_cur) {
    721 			sysmon_penvsys_event(&see->see_pes,
    722 					     PENVSYS_EVENT_CRITICAL);
    723 			see->see_evsent = edata->value_cur;
    724 		}
    725 		break;
    726 
    727 	/*
    728 	 * if value_cur is not normal (battery) or online (drive),
    729 	 * send the event...
    730 	 */
    731 	case PENVSYS_EVENT_STATE_CHANGED:
    732 		/*
    733 		 * the state has not been changed, just ignore the event.
    734 		 */
    735 		if (edata->value_cur == see->see_evsent)
    736 			break;
    737 
    738 		switch (edata->units) {
    739 		case ENVSYS_DRIVE:
    740 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
    741 			state = ENVSYS_DRIVE_ONLINE;
    742 			break;
    743 		case ENVSYS_BATTERY_CAPACITY:
    744 			sdt = sme_get_description_table(
    745 			    SME_DESC_BATTERY_CAPACITY);
    746 			state = ENVSYS_BATTERY_CAPACITY_NORMAL;
    747 			break;
    748 		default:
    749 			panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED",
    750 			    __func__);
    751 		}
    752 
    753 		for (i = 0; sdt[i].type != -1; i++)
    754 			if (sdt[i].type == edata->value_cur)
    755 				break;
    756 
    757 		if (sdt[i].type == -1)
    758 			break;
    759 
    760 		/*
    761 		 * copy current state description.
    762 		 */
    763 		(void)strlcpy(see->see_pes.pes_statedesc, sdt[i].desc,
    764 		    sizeof(see->see_pes.pes_statedesc));
    765 
    766 		if (edata->value_cur == state)
    767 			/*
    768 			 * state returned to normal condition
    769 			 */
    770 			sysmon_penvsys_event(&see->see_pes,
    771 					     PENVSYS_EVENT_NORMAL);
    772 		else
    773 			/*
    774 			 * state changed to abnormal condition
    775 			 */
    776 			sysmon_penvsys_event(&see->see_pes, see->see_type);
    777 
    778 		see->see_evsent = edata->value_cur;
    779 
    780 		/*
    781 		 * There's no need to continue if it's a drive sensor.
    782 		 */
    783 		if (edata->units == ENVSYS_DRIVE)
    784 			break;
    785 
    786 		/*
    787 		 * Check if the system is running in low power and send the
    788 		 * event to powerd (if running) or shutdown the system
    789 		 * otherwise.
    790 		 */
    791 		if (!sysmon_low_power && sme_event_check_low_power()) {
    792 			struct penvsys_state pes;
    793 
    794 			/*
    795 			 * Stop the callout and send the 'low-power' event.
    796 			 */
    797 			sysmon_low_power = true;
    798 			callout_stop(&sme->sme_callout);
    799 			pes.pes_type = PENVSYS_TYPE_BATTERY;
    800 			sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
    801 		}
    802 		break;
    803 	default:
    804 		panic("%s: invalid event type %d", __func__, see->see_type);
    805 	}
    806 
    807 out:
    808 	see->see_flags &= ~SEE_EVENT_WORKING;
    809 	cv_broadcast(&sme->sme_condvar);
    810 	mutex_exit(&sme->sme_mtx);
    811 }
    812 
    813 /*
    814  * Returns true if the system is in low power state: an AC adapter
    815  * is OFF and all batteries are in LOW/CRITICAL state.
    816  */
    817 static bool
    818 sme_event_check_low_power(void)
    819 {
    820 	if (!sme_acadapter_check())
    821 		return false;
    822 
    823 	return sme_battery_check();
    824 }
    825 
    826 /*
    827  * Called with the sysmon_envsys device mtx held through the
    828  * workqueue thread.
    829  */
    830 static bool
    831 sme_acadapter_check(void)
    832 {
    833 	struct sysmon_envsys *sme;
    834 	envsys_data_t *edata;
    835 	bool dev = false, sensor = false;
    836 
    837 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    838 		if (sme->sme_class == SME_CLASS_ACADAPTER) {
    839 			dev = true;
    840 			break;
    841 		}
    842 	}
    843 
    844 	/*
    845 	 * No AC Adapter devices were found.
    846 	 */
    847 	if (!dev)
    848 		return false;
    849 
    850 	/*
    851 	 * Check if there's an AC adapter device connected.
    852 	 */
    853 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    854 		if (edata->units == ENVSYS_INDICATOR) {
    855 			sensor = true;
    856 			/* refresh current sensor */
    857 			(*sme->sme_refresh)(sme, edata);
    858 			if (edata->value_cur)
    859 				return false;
    860 		}
    861 	}
    862 
    863 	if (!sensor)
    864 		return false;
    865 
    866 	/*
    867 	 * AC adapter found and not connected.
    868 	 */
    869 	return true;
    870 }
    871 
    872 /*
    873  * Called with the sysmon_envsys device mtx held through the
    874  * workqueue thread.
    875  */
    876 static bool
    877 sme_battery_check(void)
    878 {
    879 	struct sysmon_envsys *sme;
    880 	envsys_data_t *edata;
    881 	int batteriesfound = 0;
    882 	bool present, batterycap, batterycharge;
    883 
    884 	/*
    885 	 * Check for battery devices and its state.
    886 	 */
    887 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    888 		if (sme->sme_class != SME_CLASS_BATTERY)
    889 			continue;
    890 
    891 		present = true;
    892 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    893 			if (edata->units == ENVSYS_INDICATOR &&
    894 			    !edata->value_cur) {
    895 				present = false;
    896 				break;
    897 			}
    898 		}
    899 		if (!present)
    900 			continue;
    901 		/*
    902 		 * We've found a battery device...
    903 		 */
    904 		batteriesfound++;
    905 		batterycap = batterycharge = false;
    906 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    907 			if (edata->units == ENVSYS_BATTERY_CAPACITY) {
    908 				batterycap = true;
    909 				if (!sme_battery_critical(edata))
    910 					return false;
    911 			} else if (edata->units == ENVSYS_BATTERY_CHARGE) {
    912 				batterycharge = true;
    913 				if (edata->value_cur)
    914 					return false;
    915 			}
    916 		}
    917 		if (!batterycap || !batterycharge)
    918 			return false;
    919 	}
    920 
    921 	if (!batteriesfound)
    922 		return false;
    923 
    924 	/*
    925 	 * All batteries in low/critical capacity and discharging.
    926 	 */
    927 	return true;
    928 }
    929 
    930 static bool
    931 sme_battery_critical(envsys_data_t *edata)
    932 {
    933 	if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
    934 	    edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
    935 		return true;
    936 
    937 	return false;
    938 }
    939