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