Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys_events.c revision 1.85.2.3
      1  1.85.2.2     rmind /* $NetBSD: sysmon_envsys_events.c,v 1.85.2.3 2011/06/12 00:24:26 rmind Exp $ */
      2       1.1   xtraeme 
      3       1.1   xtraeme /*-
      4      1.51   xtraeme  * Copyright (c) 2007, 2008 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.36   xtraeme  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.36   xtraeme  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.36   xtraeme  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.36   xtraeme  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.36   xtraeme  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21      1.36   xtraeme  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22      1.36   xtraeme  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23      1.36   xtraeme  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24      1.36   xtraeme  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25      1.36   xtraeme  * 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.85.2.2     rmind __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.85.2.3 2011/06/12 00:24:26 rmind 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.32   xtraeme /* #define ENVSYS_DEBUG */
     47      1.82  pgoyette /* #define ENVSYS_OBJECTS_DEBUG */
     48      1.56  pgoyette 
     49       1.1   xtraeme #include <dev/sysmon/sysmonvar.h>
     50       1.1   xtraeme #include <dev/sysmon/sysmon_envsysvar.h>
     51       1.1   xtraeme 
     52      1.10   xtraeme struct sme_sensor_event {
     53      1.10   xtraeme 	int		state;
     54      1.10   xtraeme 	int		event;
     55      1.10   xtraeme };
     56      1.10   xtraeme 
     57      1.10   xtraeme static const struct sme_sensor_event sme_sensor_event[] = {
     58      1.65  pgoyette 	{ ENVSYS_SVALID,			PENVSYS_EVENT_NORMAL },
     59      1.65  pgoyette 	{ ENVSYS_SCRITOVER, 			PENVSYS_EVENT_CRITOVER },
     60      1.65  pgoyette 	{ ENVSYS_SCRITUNDER, 			PENVSYS_EVENT_CRITUNDER },
     61      1.65  pgoyette 	{ ENVSYS_SWARNOVER, 			PENVSYS_EVENT_WARNOVER },
     62      1.65  pgoyette 	{ ENVSYS_SWARNUNDER,			PENVSYS_EVENT_WARNUNDER },
     63      1.65  pgoyette 	{ ENVSYS_BATTERY_CAPACITY_NORMAL,	PENVSYS_EVENT_NORMAL },
     64      1.65  pgoyette 	{ ENVSYS_BATTERY_CAPACITY_WARNING,	PENVSYS_EVENT_BATT_WARN },
     65      1.65  pgoyette 	{ ENVSYS_BATTERY_CAPACITY_CRITICAL,	PENVSYS_EVENT_BATT_CRIT },
     66      1.84  pgoyette 	{ ENVSYS_BATTERY_CAPACITY_HIGH,		PENVSYS_EVENT_BATT_HIGH },
     67      1.84  pgoyette 	{ ENVSYS_BATTERY_CAPACITY_MAX,		PENVSYS_EVENT_BATT_MAX },
     68      1.65  pgoyette 	{ -1, 					-1 }
     69      1.10   xtraeme };
     70      1.10   xtraeme 
     71      1.51   xtraeme static bool sysmon_low_power;
     72       1.1   xtraeme 
     73      1.45   xtraeme #define SME_EVTIMO	(SME_EVENTS_DEFTIMEOUT * hz)
     74       1.3   xtraeme 
     75      1.44   xtraeme static bool sme_event_check_low_power(void);
     76      1.47   xtraeme static bool sme_battery_check(void);
     77      1.44   xtraeme static bool sme_battery_critical(envsys_data_t *);
     78      1.47   xtraeme static bool sme_acadapter_check(void);
     79      1.38   xtraeme 
     80       1.3   xtraeme /*
     81       1.1   xtraeme  * sme_event_register:
     82       1.1   xtraeme  *
     83      1.24   xtraeme  * 	+ Registers a new sysmon envsys event or updates any event
     84      1.24   xtraeme  * 	  already in the queue.
     85       1.1   xtraeme  */
     86       1.1   xtraeme int
     87      1.24   xtraeme sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
     88      1.67  pgoyette 		   struct sysmon_envsys *sme, sysmon_envsys_lim_t *lims,
     89      1.82  pgoyette 		   uint32_t props, int crittype, int powertype)
     90       1.1   xtraeme {
     91      1.51   xtraeme 	sme_event_t *see = NULL, *osee = NULL;
     92      1.24   xtraeme 	prop_object_t obj;
     93       1.1   xtraeme 	int error = 0;
     94      1.65  pgoyette 	const char *objkey;
     95       1.1   xtraeme 
     96      1.76  pgoyette 	KASSERT(sdict != NULL);
     97      1.76  pgoyette 	KASSERT(edata != NULL);
     98      1.76  pgoyette 	KASSERT(sme != NULL);
     99      1.77  pgoyette 	KASSERT(lims != NULL);
    100      1.77  pgoyette 
    101      1.77  pgoyette 	/*
    102      1.77  pgoyette 	 * Some validation first for limit-checking events
    103      1.77  pgoyette 	 *
    104  1.85.2.3     rmind 	 * 1. Limits are not permitted if the units is ENVSYS_INDICATOR
    105  1.85.2.3     rmind 	 *    or ENVSYS_BATTERY_CHARGE.
    106      1.85  pgoyette 	 *
    107      1.85  pgoyette 	 * 2. Capacity limits are permitted only if the sensor has the
    108      1.85  pgoyette 	 *    ENVSYS_FPERCENT flag set and value_max is set.
    109      1.85  pgoyette 	 *
    110      1.85  pgoyette 	 * 3. It is not permissible for both capacity and value limits
    111      1.85  pgoyette 	 *    to coexist.
    112      1.85  pgoyette 	 *
    113      1.85  pgoyette 	 * Note that it permissible for a sensor to have value limits
    114      1.85  pgoyette 	 * even if its ENVSYS_FPERCENT flag and value_max are set.
    115      1.77  pgoyette 	 */
    116      1.77  pgoyette 
    117  1.85.2.1     rmind 	DPRINTF(("%s: units %d props 0x%04x upropset 0x%04x max_val %d"
    118  1.85.2.1     rmind 		" edata-flags 0x%04x\n", __func__, edata->units, props,
    119  1.85.2.1     rmind 		edata->upropset, edata->value_max, edata->flags));
    120      1.77  pgoyette 
    121  1.85.2.3     rmind 	if (props)
    122  1.85.2.3     rmind 		if (edata->units == ENVSYS_INDICATOR ||
    123  1.85.2.3     rmind 		    edata->units == ENVSYS_BATTERY_CHARGE)
    124  1.85.2.3     rmind 			return ENOTSUP;
    125      1.85  pgoyette 
    126      1.82  pgoyette 	if ((props & PROP_CAP_LIMITS) &&
    127      1.85  pgoyette 	    ((edata->value_max == 0) ||
    128      1.85  pgoyette 	     !(edata->flags & ENVSYS_FPERCENT) ||
    129      1.85  pgoyette 	     (props & PROP_VAL_LIMITS) ||
    130      1.85  pgoyette 	     (edata->upropset & PROP_VAL_LIMITS)))
    131  1.85.2.1     rmind 		props = 0;
    132      1.85  pgoyette 
    133      1.85  pgoyette 	if ((props & PROP_VAL_LIMITS) && (edata->upropset & PROP_CAP_LIMITS))
    134  1.85.2.1     rmind 		props = 0;
    135      1.56  pgoyette 
    136       1.1   xtraeme 	/*
    137      1.24   xtraeme 	 * check if the event is already on the list and return
    138      1.24   xtraeme 	 * EEXIST if value provided hasn't been changed.
    139       1.1   xtraeme 	 */
    140      1.51   xtraeme 	mutex_enter(&sme->sme_mtx);
    141      1.51   xtraeme 	LIST_FOREACH(osee, &sme->sme_events_list, see_list) {
    142      1.65  pgoyette 		if (strcmp(edata->desc, osee->see_pes.pes_sensname) != 0)
    143      1.65  pgoyette 			continue;
    144      1.65  pgoyette 		if (crittype != osee->see_type)
    145      1.65  pgoyette 			continue;
    146      1.65  pgoyette 
    147      1.76  pgoyette 		/*
    148      1.76  pgoyette 		 * We found an existing event for this sensor.  Make
    149      1.76  pgoyette 		 * sure it references the correct edata
    150      1.76  pgoyette 		 */
    151      1.76  pgoyette 		KASSERT(edata == osee->see_edata);
    152      1.76  pgoyette 
    153      1.77  pgoyette 		DPRINTF(("%s: dev %s sensor %s: event type %d exists\n",
    154      1.77  pgoyette 		    __func__, sme->sme_name, edata->desc, crittype));
    155      1.65  pgoyette 
    156      1.65  pgoyette 		see = osee;
    157  1.85.2.2     rmind 		if (props & edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) {
    158      1.76  pgoyette 			if (lims->sel_critmax == edata->limits.sel_critmax) {
    159  1.85.2.2     rmind 				DPRINTF(("%s: critmax exists\n", __func__));
    160      1.65  pgoyette 				error = EEXIST;
    161      1.84  pgoyette 				props &= ~(PROP_CRITMAX | PROP_BATTMAX);
    162      1.65  pgoyette 			}
    163      1.65  pgoyette 		}
    164  1.85.2.2     rmind 		if (props & edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) {
    165      1.76  pgoyette 			if (lims->sel_warnmax == edata->limits.sel_warnmax) {
    166      1.77  pgoyette 				DPRINTF(("%s: warnmax exists\n", __func__));
    167      1.65  pgoyette 				error = EEXIST;
    168      1.84  pgoyette 				props &= ~(PROP_WARNMAX | PROP_BATTHIGH);
    169      1.65  pgoyette 			}
    170      1.65  pgoyette 		}
    171  1.85.2.2     rmind 		if (props & edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) {
    172      1.76  pgoyette 			if (lims->sel_warnmin == edata->limits.sel_warnmin) {
    173      1.77  pgoyette 				DPRINTF(("%s: warnmin exists\n", __func__));
    174      1.65  pgoyette 				error = EEXIST;
    175      1.82  pgoyette 				props &= ~(PROP_WARNMIN | PROP_BATTWARN);
    176       1.1   xtraeme 			}
    177      1.65  pgoyette 		}
    178  1.85.2.2     rmind 		if (props & edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) {
    179      1.76  pgoyette 			if (lims->sel_critmin == edata->limits.sel_critmin) {
    180      1.77  pgoyette 				DPRINTF(("%s: critmin exists\n", __func__));
    181      1.65  pgoyette 				error = EEXIST;
    182      1.82  pgoyette 				props &= ~(PROP_CRITMIN | PROP_BATTCAP);
    183      1.56  pgoyette 			}
    184       1.1   xtraeme 		}
    185      1.65  pgoyette 		break;
    186       1.1   xtraeme 	}
    187      1.65  pgoyette 	if (see == NULL) {
    188      1.65  pgoyette 		/*
    189      1.65  pgoyette 		 * New event requested - allocate a sysmon_envsys event.
    190      1.65  pgoyette 		 */
    191      1.65  pgoyette 		see = kmem_zalloc(sizeof(*see), KM_SLEEP);
    192      1.65  pgoyette 		if (see == NULL)
    193      1.65  pgoyette 			return ENOMEM;
    194      1.65  pgoyette 
    195      1.77  pgoyette 		DPRINTF(("%s: dev %s sensor %s: new event\n",
    196      1.79    jruoho 		    __func__, sme->sme_name, edata->desc));
    197      1.65  pgoyette 
    198      1.65  pgoyette 		see->see_type = crittype;
    199      1.65  pgoyette 		see->see_sme = sme;
    200      1.65  pgoyette 		see->see_edata = edata;
    201      1.65  pgoyette 
    202      1.65  pgoyette 		/* Initialize sensor type and previously-sent state */
    203      1.65  pgoyette 
    204      1.65  pgoyette 		see->see_pes.pes_type = powertype;
    205       1.1   xtraeme 
    206      1.56  pgoyette 		switch (crittype) {
    207      1.65  pgoyette 		case PENVSYS_EVENT_LIMITS:
    208      1.65  pgoyette 			see->see_evsent = ENVSYS_SVALID;
    209      1.65  pgoyette 			break;
    210      1.65  pgoyette 		case PENVSYS_EVENT_CAPACITY:
    211      1.65  pgoyette 			see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
    212      1.56  pgoyette 			break;
    213      1.65  pgoyette 		case PENVSYS_EVENT_STATE_CHANGED:
    214      1.65  pgoyette 			if (edata->units == ENVSYS_BATTERY_CAPACITY)
    215      1.65  pgoyette 				see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
    216      1.65  pgoyette 			else if (edata->units == ENVSYS_DRIVE)
    217      1.65  pgoyette 				see->see_evsent = ENVSYS_DRIVE_EMPTY;
    218      1.65  pgoyette 			else
    219      1.65  pgoyette 				panic("%s: bad units for "
    220      1.65  pgoyette 				      "PENVSYS_EVENT_STATE_CHANGED", __func__);
    221      1.56  pgoyette 			break;
    222      1.65  pgoyette 		case PENVSYS_EVENT_CRITICAL:
    223      1.56  pgoyette 		default:
    224      1.65  pgoyette 			see->see_evsent = 0;
    225      1.56  pgoyette 			break;
    226      1.56  pgoyette 		}
    227      1.65  pgoyette 
    228      1.65  pgoyette 		(void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
    229      1.65  pgoyette 		    sizeof(see->see_pes.pes_dvname));
    230      1.65  pgoyette 		(void)strlcpy(see->see_pes.pes_sensname, edata->desc,
    231      1.65  pgoyette 		    sizeof(see->see_pes.pes_sensname));
    232      1.56  pgoyette 	}
    233      1.65  pgoyette 
    234      1.56  pgoyette 	/*
    235      1.65  pgoyette 	 * Limit operation requested.
    236      1.24   xtraeme 	 */
    237      1.83  pgoyette #define	LIMIT_OP(k, l, p)						\
    238      1.83  pgoyette 	if (props & p) {						\
    239      1.83  pgoyette 		objkey = k;						\
    240      1.83  pgoyette 		obj = prop_dictionary_get(sdict, objkey);		\
    241      1.83  pgoyette 		if (obj != NULL &&					\
    242      1.83  pgoyette 		    prop_object_type(obj) != PROP_TYPE_NUMBER) {	\
    243      1.83  pgoyette 			DPRINTF(("%s: (%s) %s object no TYPE_NUMBER\n",	\
    244      1.83  pgoyette 			    __func__, sme->sme_name, objkey));		\
    245      1.83  pgoyette 			error = ENOTSUP;				\
    246      1.83  pgoyette 		} else {						\
    247      1.83  pgoyette 			edata->limits.l = lims->l;			\
    248      1.83  pgoyette 			error = sme_sensor_upint32(sdict, objkey,lims->l); \
    249      1.83  pgoyette 			DPRINTF(("%s: (%s) event [sensor=%s type=%d] "	\
    250      1.83  pgoyette 			    "(%s updated)\n", __func__, sme->sme_name,	\
    251      1.83  pgoyette 			    edata->desc, crittype, objkey));		\
    252      1.83  pgoyette 		}							\
    253      1.83  pgoyette 		if (error && error != EEXIST)				\
    254      1.83  pgoyette 			goto out;					\
    255      1.83  pgoyette 		edata->upropset |= p;					\
    256      1.27   xtraeme 	}
    257      1.65  pgoyette 
    258      1.83  pgoyette 	/* Value-based limits */
    259      1.83  pgoyette 	LIMIT_OP("critical-max", sel_critmax, PROP_CRITMAX);
    260      1.83  pgoyette 	LIMIT_OP("warning-max",  sel_warnmax, PROP_WARNMAX);
    261      1.83  pgoyette 	LIMIT_OP("warning-min",  sel_warnmin, PROP_WARNMIN);
    262      1.83  pgoyette 	LIMIT_OP("critical-min", sel_critmin, PROP_CRITMIN);
    263      1.83  pgoyette 
    264      1.83  pgoyette 	/* %Capacity-based limits */
    265      1.84  pgoyette 	LIMIT_OP("maximum-capacity",  sel_critmax,  PROP_BATTMAX);
    266      1.84  pgoyette 	LIMIT_OP("high-capacity",     sel_warnmax,  PROP_BATTHIGH);
    267      1.83  pgoyette 	LIMIT_OP("warning-capacity",  sel_warnmin,  PROP_BATTWARN);
    268      1.83  pgoyette 	LIMIT_OP("critical-capacity", sel_critmin,  PROP_BATTCAP);
    269      1.83  pgoyette 
    270      1.83  pgoyette #undef LIMIT_OP
    271      1.83  pgoyette 
    272      1.82  pgoyette 	if (props & PROP_DRIVER_LIMITS)
    273      1.76  pgoyette 		edata->upropset |= PROP_DRIVER_LIMITS;
    274      1.76  pgoyette 	else
    275      1.76  pgoyette 		edata->upropset &= ~PROP_DRIVER_LIMITS;
    276      1.74  pgoyette 
    277      1.51   xtraeme 	DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d "
    278      1.56  pgoyette 	    "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32
    279      1.65  pgoyette 	    " critmax=%" PRIu32 " props 0x%04x)\n", __func__,
    280      1.45   xtraeme 	    see->see_sme->sme_name, see->see_pes.pes_sensname,
    281      1.76  pgoyette 	    edata->sensor, see->see_type, edata->limits.sel_critmin,
    282      1.76  pgoyette 	    edata->limits.sel_warnmin, edata->limits.sel_warnmax,
    283      1.76  pgoyette 	    edata->limits.sel_critmax, edata->upropset));
    284       1.1   xtraeme 	/*
    285      1.45   xtraeme 	 * Initialize the events framework if it wasn't initialized before.
    286       1.1   xtraeme 	 */
    287      1.45   xtraeme 	if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
    288      1.45   xtraeme 		error = sme_events_init(sme);
    289      1.67  pgoyette 
    290      1.67  pgoyette 	/*
    291      1.67  pgoyette 	 * If driver requested notification, advise it of new
    292      1.67  pgoyette 	 * limit values
    293      1.67  pgoyette 	 */
    294      1.82  pgoyette 	if (sme->sme_set_limits)
    295      1.82  pgoyette 		(*sme->sme_set_limits)(sme, edata, &(edata->limits),
    296      1.82  pgoyette 					&(edata->upropset));
    297      1.67  pgoyette 
    298      1.27   xtraeme out:
    299      1.65  pgoyette 	if ((error == 0 || error == EEXIST) && osee == NULL)
    300      1.65  pgoyette 		LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
    301      1.65  pgoyette 
    302      1.51   xtraeme 	mutex_exit(&sme->sme_mtx);
    303      1.65  pgoyette 
    304       1.1   xtraeme 	return error;
    305       1.1   xtraeme }
    306       1.1   xtraeme 
    307       1.1   xtraeme /*
    308      1.18   xtraeme  * sme_event_unregister_all:
    309      1.18   xtraeme  *
    310      1.51   xtraeme  * 	+ Unregisters all events associated with a sysmon envsys device.
    311      1.18   xtraeme  */
    312      1.18   xtraeme void
    313      1.45   xtraeme sme_event_unregister_all(struct sysmon_envsys *sme)
    314      1.18   xtraeme {
    315      1.18   xtraeme 	sme_event_t *see;
    316      1.18   xtraeme 	int evcounter = 0;
    317      1.18   xtraeme 
    318      1.45   xtraeme 	KASSERT(sme != NULL);
    319      1.18   xtraeme 
    320      1.51   xtraeme 	mutex_enter(&sme->sme_mtx);
    321      1.45   xtraeme 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    322      1.56  pgoyette 		while (see->see_flags & SEE_EVENT_WORKING)
    323      1.51   xtraeme 			cv_wait(&sme->sme_condvar, &sme->sme_mtx);
    324      1.51   xtraeme 
    325      1.45   xtraeme 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
    326      1.18   xtraeme 			evcounter++;
    327      1.18   xtraeme 	}
    328      1.18   xtraeme 
    329      1.18   xtraeme 	DPRINTF(("%s: total events %d (%s)\n", __func__,
    330      1.45   xtraeme 	    evcounter, sme->sme_name));
    331      1.18   xtraeme 
    332      1.45   xtraeme 	while ((see = LIST_FIRST(&sme->sme_events_list))) {
    333      1.18   xtraeme 		if (evcounter == 0)
    334      1.18   xtraeme 			break;
    335      1.18   xtraeme 
    336      1.45   xtraeme 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
    337      1.51   xtraeme 			LIST_REMOVE(see, see_list);
    338      1.18   xtraeme 			DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
    339      1.45   xtraeme 			    see->see_pes.pes_sensname, see->see_type,
    340      1.45   xtraeme 			    sme->sme_name));
    341      1.18   xtraeme 			kmem_free(see, sizeof(*see));
    342      1.18   xtraeme 			evcounter--;
    343      1.18   xtraeme 		}
    344      1.18   xtraeme 	}
    345      1.18   xtraeme 
    346      1.51   xtraeme 	if (LIST_EMPTY(&sme->sme_events_list))
    347      1.45   xtraeme 		if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
    348      1.45   xtraeme 			sme_events_destroy(sme);
    349      1.51   xtraeme 	mutex_exit(&sme->sme_mtx);
    350      1.18   xtraeme }
    351      1.18   xtraeme 
    352      1.18   xtraeme /*
    353       1.1   xtraeme  * sme_event_unregister:
    354       1.1   xtraeme  *
    355      1.45   xtraeme  * 	+ Unregisters an event from the specified sysmon envsys device.
    356       1.1   xtraeme  */
    357       1.1   xtraeme int
    358      1.45   xtraeme sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
    359       1.1   xtraeme {
    360       1.1   xtraeme 	sme_event_t *see;
    361       1.1   xtraeme 	bool found = false;
    362       1.1   xtraeme 
    363       1.1   xtraeme 	KASSERT(sensor != NULL);
    364       1.1   xtraeme 
    365      1.51   xtraeme 	mutex_enter(&sme->sme_mtx);
    366      1.45   xtraeme 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    367      1.45   xtraeme 		if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
    368      1.45   xtraeme 			if (see->see_type == type) {
    369       1.1   xtraeme 				found = true;
    370       1.1   xtraeme 				break;
    371       1.1   xtraeme 			}
    372       1.1   xtraeme 		}
    373       1.1   xtraeme 	}
    374       1.1   xtraeme 
    375      1.51   xtraeme 	if (!found) {
    376      1.51   xtraeme 		mutex_exit(&sme->sme_mtx);
    377       1.1   xtraeme 		return EINVAL;
    378      1.51   xtraeme 	}
    379       1.1   xtraeme 
    380      1.51   xtraeme 	/*
    381      1.51   xtraeme 	 * Wait for the event to finish its work, remove from the list
    382      1.51   xtraeme 	 * and release resouces.
    383      1.51   xtraeme 	 */
    384      1.56  pgoyette 	while (see->see_flags & SEE_EVENT_WORKING)
    385      1.51   xtraeme 		cv_wait(&sme->sme_condvar, &sme->sme_mtx);
    386      1.15   xtraeme 
    387      1.51   xtraeme 	DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n",
    388      1.45   xtraeme 	    __func__, see->see_pes.pes_dvname, sensor, type));
    389       1.1   xtraeme 	LIST_REMOVE(see, see_list);
    390       1.1   xtraeme 	/*
    391       1.1   xtraeme 	 * So the events list is empty, we'll do the following:
    392       1.1   xtraeme 	 *
    393       1.9   xtraeme 	 * 	- stop and destroy the callout.
    394       1.1   xtraeme 	 * 	- destroy the workqueue.
    395       1.1   xtraeme 	 */
    396      1.51   xtraeme 	if (LIST_EMPTY(&sme->sme_events_list))
    397      1.45   xtraeme 		sme_events_destroy(sme);
    398      1.51   xtraeme 	mutex_exit(&sme->sme_mtx);
    399       1.1   xtraeme 
    400      1.20   xtraeme 	kmem_free(see, sizeof(*see));
    401       1.1   xtraeme 	return 0;
    402       1.1   xtraeme }
    403       1.1   xtraeme 
    404       1.1   xtraeme /*
    405       1.1   xtraeme  * sme_event_drvadd:
    406       1.1   xtraeme  *
    407      1.45   xtraeme  * 	+ Registers a new event for a device that had enabled any of
    408      1.45   xtraeme  * 	  the monitoring flags in the driver.
    409       1.1   xtraeme  */
    410       1.1   xtraeme void
    411       1.1   xtraeme sme_event_drvadd(void *arg)
    412       1.1   xtraeme {
    413       1.1   xtraeme 	sme_event_drv_t *sed_t = arg;
    414      1.67  pgoyette 	sysmon_envsys_lim_t lims;
    415      1.82  pgoyette 	uint32_t props;
    416       1.2   xtraeme 	int error = 0;
    417       1.1   xtraeme 
    418       1.1   xtraeme 	KASSERT(sed_t != NULL);
    419       1.1   xtraeme 
    420       1.1   xtraeme #define SEE_REGEVENT(a, b, c)						\
    421       1.1   xtraeme do {									\
    422      1.45   xtraeme 	if (sed_t->sed_edata->flags & (a)) {				\
    423      1.36   xtraeme 		char str[ENVSYS_DESCLEN] = "monitoring-state-";		\
    424       1.1   xtraeme 									\
    425      1.45   xtraeme 		error = sme_event_register(sed_t->sed_sdict,		\
    426      1.45   xtraeme 				      sed_t->sed_edata,			\
    427      1.45   xtraeme 				      sed_t->sed_sme,			\
    428      1.82  pgoyette 				      &lims, props,			\
    429       1.2   xtraeme 				      (b),				\
    430      1.45   xtraeme 				      sed_t->sed_powertype);		\
    431       1.2   xtraeme 		if (error && error != EEXIST)				\
    432       1.2   xtraeme 			printf("%s: failed to add event! "		\
    433       1.2   xtraeme 			    "error=%d sensor=%s event=%s\n",		\
    434      1.45   xtraeme 			    __func__, error,				\
    435      1.45   xtraeme 			    sed_t->sed_edata->desc, (c));		\
    436       1.2   xtraeme 		else {							\
    437       1.2   xtraeme 			(void)strlcat(str, (c), sizeof(str));		\
    438      1.45   xtraeme 			prop_dictionary_set_bool(sed_t->sed_sdict,	\
    439       1.2   xtraeme 						 str,			\
    440       1.2   xtraeme 						 true);			\
    441       1.2   xtraeme 		}							\
    442       1.1   xtraeme 	}								\
    443       1.1   xtraeme } while (/* CONSTCOND */ 0)
    444       1.1   xtraeme 
    445      1.71  pgoyette 	/*
    446      1.73  pgoyette 	 * If driver provides a method to retrieve its internal limit
    447      1.74  pgoyette 	 * values, call it and use those returned values as initial
    448      1.73  pgoyette 	 * limits for event monitoring.
    449      1.71  pgoyette 	 */
    450      1.82  pgoyette 	props = 0;
    451      1.71  pgoyette 	if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS)
    452      1.67  pgoyette 		if (sed_t->sed_sme->sme_get_limits)
    453      1.67  pgoyette 			(*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme,
    454      1.68  pgoyette 							  sed_t->sed_edata,
    455      1.82  pgoyette 							  &lims, &props);
    456      1.73  pgoyette 	/*
    457      1.73  pgoyette 	 * If driver doesn't provide a way to "absorb" user-specified
    458      1.73  pgoyette 	 * limit values, we must monitor all limits ourselves
    459      1.73  pgoyette 	 */
    460  1.85.2.1     rmind 	if (sed_t->sed_sme->sme_set_limits == NULL)
    461      1.82  pgoyette 		props &= ~PROP_DRIVER_LIMITS;
    462      1.71  pgoyette 
    463      1.71  pgoyette 	/* Register the events that were specified */
    464      1.67  pgoyette 
    465       1.1   xtraeme 	SEE_REGEVENT(ENVSYS_FMONCRITICAL,
    466       1.1   xtraeme 		     PENVSYS_EVENT_CRITICAL,
    467       1.1   xtraeme 		     "critical");
    468       1.1   xtraeme 
    469      1.67  pgoyette 	SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
    470      1.67  pgoyette 		     PENVSYS_EVENT_STATE_CHANGED,
    471      1.67  pgoyette 		     "state-changed");
    472      1.67  pgoyette 
    473      1.64  pgoyette 	SEE_REGEVENT(ENVSYS_FMONLIMITS,
    474      1.65  pgoyette 		     PENVSYS_EVENT_LIMITS,
    475      1.56  pgoyette 		     "hw-range-limits");
    476       1.1   xtraeme 
    477      1.45   xtraeme 	/*
    478      1.45   xtraeme 	 * we are done, free memory now.
    479      1.45   xtraeme 	 */
    480       1.1   xtraeme 	kmem_free(sed_t, sizeof(*sed_t));
    481       1.1   xtraeme }
    482       1.1   xtraeme 
    483       1.1   xtraeme /*
    484       1.1   xtraeme  * sme_events_init:
    485       1.1   xtraeme  *
    486      1.45   xtraeme  * 	+ Initialize the events framework for this device.
    487       1.1   xtraeme  */
    488       1.1   xtraeme int
    489      1.45   xtraeme sme_events_init(struct sysmon_envsys *sme)
    490       1.1   xtraeme {
    491      1.51   xtraeme 	int error = 0;
    492       1.1   xtraeme 
    493      1.45   xtraeme 	KASSERT(sme != NULL);
    494      1.51   xtraeme 	KASSERT(mutex_owned(&sme->sme_mtx));
    495      1.22   xtraeme 
    496      1.45   xtraeme 	error = workqueue_create(&sme->sme_wq, sme->sme_name,
    497      1.45   xtraeme 	    sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
    498       1.1   xtraeme 	if (error)
    499      1.51   xtraeme 		return error;
    500       1.1   xtraeme 
    501      1.51   xtraeme 	mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    502      1.54   xtraeme 	callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
    503      1.45   xtraeme 	callout_setfunc(&sme->sme_callout, sme_events_check, sme);
    504      1.45   xtraeme 	sme->sme_flags |= SME_CALLOUT_INITIALIZED;
    505  1.85.2.2     rmind 	sme_schedule_callout(sme);
    506      1.45   xtraeme 	DPRINTF(("%s: events framework initialized for '%s'\n",
    507      1.45   xtraeme 	    __func__, sme->sme_name));
    508       1.1   xtraeme 
    509       1.1   xtraeme 	return error;
    510       1.1   xtraeme }
    511       1.1   xtraeme 
    512       1.1   xtraeme /*
    513  1.85.2.2     rmind  * sme_schedule_callout
    514  1.85.2.2     rmind  *
    515  1.85.2.2     rmind  *	(Re)-schedule the device's callout timer
    516  1.85.2.2     rmind  */
    517  1.85.2.2     rmind void
    518  1.85.2.2     rmind sme_schedule_callout(struct sysmon_envsys *sme)
    519  1.85.2.2     rmind {
    520  1.85.2.2     rmind 	uint64_t timo;
    521  1.85.2.2     rmind 
    522  1.85.2.2     rmind 	KASSERT(sme != NULL);
    523  1.85.2.2     rmind 
    524  1.85.2.2     rmind 	if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
    525  1.85.2.2     rmind 		return;
    526  1.85.2.2     rmind 
    527  1.85.2.2     rmind 	if (sme->sme_events_timeout)
    528  1.85.2.2     rmind 		timo = sme->sme_events_timeout * hz;
    529  1.85.2.2     rmind 	else
    530  1.85.2.2     rmind 		timo = SME_EVTIMO;
    531  1.85.2.2     rmind 
    532  1.85.2.2     rmind 	callout_stop(&sme->sme_callout);
    533  1.85.2.2     rmind 	callout_schedule(&sme->sme_callout, timo);
    534  1.85.2.2     rmind }
    535  1.85.2.2     rmind 
    536  1.85.2.2     rmind /*
    537      1.18   xtraeme  * sme_events_destroy:
    538      1.18   xtraeme  *
    539      1.51   xtraeme  * 	+ Destroys the event framework for this device: callout
    540      1.51   xtraeme  * 	  stopped, workqueue destroyed and callout mutex destroyed.
    541      1.18   xtraeme  */
    542      1.18   xtraeme void
    543      1.45   xtraeme sme_events_destroy(struct sysmon_envsys *sme)
    544      1.18   xtraeme {
    545      1.51   xtraeme 	KASSERT(mutex_owned(&sme->sme_mtx));
    546      1.22   xtraeme 
    547      1.45   xtraeme 	callout_stop(&sme->sme_callout);
    548      1.54   xtraeme 	workqueue_destroy(sme->sme_wq);
    549      1.54   xtraeme 	mutex_destroy(&sme->sme_callout_mtx);
    550      1.54   xtraeme 	callout_destroy(&sme->sme_callout);
    551      1.45   xtraeme 	sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
    552      1.45   xtraeme 	DPRINTF(("%s: events framework destroyed for '%s'\n",
    553      1.45   xtraeme 	    __func__, sme->sme_name));
    554      1.18   xtraeme }
    555      1.18   xtraeme 
    556      1.18   xtraeme /*
    557  1.85.2.1     rmind  * sysmon_envsys_update_limits
    558  1.85.2.1     rmind  *
    559  1.85.2.1     rmind  *	+ If a driver needs to update the limits that it is providing,
    560  1.85.2.1     rmind  *	  we need to update the dictionary data as well as the limits.
    561  1.85.2.1     rmind  *	  This only makes sense if the driver is capable of providing
    562  1.85.2.1     rmind  *	  its limits, and if there is a limits event-monitor.
    563  1.85.2.1     rmind  */
    564  1.85.2.1     rmind int
    565  1.85.2.1     rmind sysmon_envsys_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
    566  1.85.2.1     rmind {
    567  1.85.2.1     rmind 	int err;
    568  1.85.2.1     rmind 
    569  1.85.2.2     rmind 	sysmon_envsys_acquire(sme, false);
    570  1.85.2.1     rmind 	if (sme->sme_get_limits == NULL ||
    571  1.85.2.1     rmind 	    (edata->flags & ENVSYS_FMONLIMITS) == 0)
    572  1.85.2.2     rmind 		err = EINVAL;
    573  1.85.2.2     rmind 	else
    574  1.85.2.2     rmind 		err = sme_update_limits(sme, edata);
    575  1.85.2.1     rmind 	sysmon_envsys_release(sme, false);
    576  1.85.2.1     rmind 
    577  1.85.2.1     rmind 	return err;
    578  1.85.2.1     rmind }
    579  1.85.2.1     rmind 
    580  1.85.2.1     rmind /*
    581  1.85.2.1     rmind  * sme_update_limits
    582  1.85.2.1     rmind  *
    583  1.85.2.1     rmind  *	+ Internal version of sysmon_envsys_update_limits() to be used
    584  1.85.2.1     rmind  *	  when the device has already been sysmon_envsys_acquire()d.
    585  1.85.2.1     rmind  */
    586  1.85.2.1     rmind 
    587  1.85.2.1     rmind int
    588  1.85.2.1     rmind sme_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
    589  1.85.2.1     rmind {
    590  1.85.2.1     rmind 	prop_dictionary_t sdict = NULL;
    591  1.85.2.1     rmind 	prop_array_t array = NULL;
    592  1.85.2.1     rmind 	sysmon_envsys_lim_t lims;
    593  1.85.2.1     rmind 	sme_event_t *see;
    594  1.85.2.1     rmind 	uint32_t props = 0;
    595  1.85.2.1     rmind 
    596  1.85.2.1     rmind 	/* Find the dictionary for this sensor */
    597  1.85.2.1     rmind 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    598  1.85.2.1     rmind 	if (array == NULL ||
    599  1.85.2.1     rmind 	    prop_object_type(array) != PROP_TYPE_ARRAY) {
    600  1.85.2.1     rmind 		DPRINTF(("%s: array device failed\n", __func__));
    601  1.85.2.1     rmind 		return EINVAL;
    602  1.85.2.1     rmind 	}
    603  1.85.2.1     rmind 
    604  1.85.2.1     rmind 	sdict = prop_array_get(array, edata->sensor);
    605  1.85.2.1     rmind 	if (sdict == NULL) {
    606  1.85.2.1     rmind 		return EINVAL;
    607  1.85.2.1     rmind 	}
    608  1.85.2.1     rmind 
    609  1.85.2.1     rmind 	/* Find the event definition to get its powertype */
    610  1.85.2.1     rmind 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    611  1.85.2.1     rmind 		if (edata == see->see_edata &&
    612  1.85.2.1     rmind 		    see->see_type == PENVSYS_EVENT_LIMITS)
    613  1.85.2.1     rmind 			break;
    614  1.85.2.1     rmind 	}
    615  1.85.2.1     rmind 	if (see == NULL)
    616  1.85.2.1     rmind 		return EINVAL;
    617  1.85.2.1     rmind 
    618  1.85.2.2     rmind 	/* Update limit values from driver if possible */
    619  1.85.2.2     rmind 	if (sme->sme_get_limits != NULL)
    620  1.85.2.2     rmind 		(*sme->sme_get_limits)(sme, edata, &lims, &props);
    621  1.85.2.1     rmind 
    622  1.85.2.1     rmind 	/* Update event and dictionary */
    623  1.85.2.1     rmind 	sme_event_register(sdict, edata, sme, &lims, props,
    624  1.85.2.1     rmind 			   PENVSYS_EVENT_LIMITS, see->see_pes.pes_type);
    625  1.85.2.1     rmind 
    626  1.85.2.1     rmind 	return 0;
    627  1.85.2.1     rmind }
    628  1.85.2.1     rmind 
    629  1.85.2.1     rmind /*
    630       1.1   xtraeme  * sme_events_check:
    631       1.1   xtraeme  *
    632      1.51   xtraeme  * 	+ Passes the events to the workqueue thread and stops
    633      1.51   xtraeme  * 	  the callout if the 'low-power' condition is triggered.
    634       1.1   xtraeme  */
    635       1.1   xtraeme void
    636       1.1   xtraeme sme_events_check(void *arg)
    637       1.1   xtraeme {
    638      1.45   xtraeme 	struct sysmon_envsys *sme = arg;
    639       1.1   xtraeme 	sme_event_t *see;
    640      1.45   xtraeme 	uint64_t timo;
    641      1.45   xtraeme 
    642      1.45   xtraeme 	KASSERT(sme != NULL);
    643      1.45   xtraeme 
    644      1.51   xtraeme 	mutex_enter(&sme->sme_callout_mtx);
    645      1.51   xtraeme 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    646      1.45   xtraeme 		workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
    647      1.56  pgoyette 		see->see_edata->flags |= ENVSYS_FNEED_REFRESH;
    648      1.51   xtraeme 	}
    649      1.45   xtraeme 	if (sme->sme_events_timeout)
    650      1.45   xtraeme 		timo = sme->sme_events_timeout * hz;
    651      1.45   xtraeme 	else
    652      1.45   xtraeme 		timo = SME_EVTIMO;
    653      1.38   xtraeme 	if (!sysmon_low_power)
    654  1.85.2.2     rmind 		sme_schedule_callout(sme);
    655      1.51   xtraeme 	mutex_exit(&sme->sme_callout_mtx);
    656       1.1   xtraeme }
    657       1.1   xtraeme 
    658       1.1   xtraeme /*
    659       1.1   xtraeme  * sme_events_worker:
    660       1.1   xtraeme  *
    661       1.1   xtraeme  * 	+ workqueue thread that checks if there's a critical condition
    662      1.51   xtraeme  * 	  and sends an event if it was triggered.
    663       1.1   xtraeme  */
    664       1.1   xtraeme void
    665       1.1   xtraeme sme_events_worker(struct work *wk, void *arg)
    666       1.1   xtraeme {
    667       1.1   xtraeme 	sme_event_t *see = (void *)wk;
    668      1.51   xtraeme 	struct sysmon_envsys *sme = see->see_sme;
    669      1.51   xtraeme 	envsys_data_t *edata = see->see_edata;
    670       1.1   xtraeme 
    671       1.1   xtraeme 	KASSERT(wk == &see->see_wk);
    672      1.51   xtraeme 	KASSERT(sme != NULL || edata != NULL);
    673       1.1   xtraeme 
    674      1.51   xtraeme 	mutex_enter(&sme->sme_mtx);
    675  1.85.2.1     rmind 	see->see_flags |= SEE_EVENT_WORKING;
    676       1.1   xtraeme 	/*
    677      1.65  pgoyette 	 * sme_events_check marks the sensors to make us refresh them here.
    678      1.65  pgoyette 	 * Don't refresh if the driver uses its own method for refreshing.
    679       1.1   xtraeme 	 */
    680      1.45   xtraeme 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
    681      1.65  pgoyette 		if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) {
    682      1.51   xtraeme 			/* refresh sensor in device */
    683      1.51   xtraeme 			(*sme->sme_refresh)(sme, edata);
    684      1.65  pgoyette 			edata->flags &= ~ENVSYS_FNEED_REFRESH;
    685      1.42   xtraeme 		}
    686       1.1   xtraeme 	}
    687       1.1   xtraeme 
    688      1.56  pgoyette 	DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
    689      1.82  pgoyette 	    "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc,
    690      1.56  pgoyette 	    edata->sensor, see->see_type, edata->state, edata->units,
    691      1.82  pgoyette 	    edata->value_cur, edata->upropset));
    692       1.1   xtraeme 
    693      1.50   xtraeme 	/* skip the event if current sensor is in invalid state */
    694      1.51   xtraeme 	if (edata->state == ENVSYS_SINVALID)
    695      1.50   xtraeme 		goto out;
    696      1.50   xtraeme 
    697      1.10   xtraeme 	/*
    698      1.67  pgoyette 	 * For range limits, if the driver claims responsibility for
    699      1.67  pgoyette 	 * limit/range checking, just user driver-supplied status.
    700      1.67  pgoyette 	 * Else calculate our own status.  Note that driver must
    701      1.67  pgoyette 	 * relinquish responsibility for ALL limits if there is even
    702      1.67  pgoyette 	 * one limit that it cannot handle!
    703  1.85.2.1     rmind 	 *
    704  1.85.2.1     rmind 	 * If this is a CAPACITY monitor, but the sensor's max_value
    705  1.85.2.1     rmind 	 * is not set, treat it as though the monitor does not exist.
    706      1.56  pgoyette 	 */
    707  1.85.2.1     rmind 	if ((see->see_type == PENVSYS_EVENT_LIMITS ||
    708  1.85.2.1     rmind 	     see->see_type == PENVSYS_EVENT_CAPACITY) &&
    709  1.85.2.1     rmind 	    (edata->upropset & PROP_DRIVER_LIMITS) == 0) {
    710  1.85.2.1     rmind 		if ((see->see_type == PENVSYS_EVENT_CAPACITY) &&
    711  1.85.2.1     rmind 		    (edata->value_max == 0))
    712  1.85.2.1     rmind 			edata->state = ENVSYS_SVALID;
    713  1.85.2.1     rmind 		else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) &&
    714  1.85.2.1     rmind 		    (edata->value_cur < edata->limits.sel_critmin))
    715  1.85.2.1     rmind 			edata->state = ENVSYS_SCRITUNDER;
    716  1.85.2.1     rmind 		else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) &&
    717  1.85.2.1     rmind 			 (edata->value_cur < edata->limits.sel_warnmin))
    718  1.85.2.1     rmind 			edata->state = ENVSYS_SWARNUNDER;
    719  1.85.2.1     rmind 		else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) &&
    720  1.85.2.1     rmind 			 (edata->value_cur > edata->limits.sel_critmax))
    721  1.85.2.1     rmind 			edata->state = ENVSYS_SCRITOVER;
    722  1.85.2.1     rmind 		else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) &&
    723  1.85.2.1     rmind 			 (edata->value_cur > edata->limits.sel_warnmax))
    724  1.85.2.1     rmind 			edata->state = ENVSYS_SWARNOVER;
    725  1.85.2.1     rmind 		else
    726  1.85.2.1     rmind 			edata->state = ENVSYS_SVALID;
    727  1.85.2.1     rmind 	}
    728  1.85.2.1     rmind 	sme_deliver_event(see);
    729  1.85.2.1     rmind 
    730  1.85.2.1     rmind out:
    731  1.85.2.1     rmind 	see->see_flags &= ~SEE_EVENT_WORKING;
    732  1.85.2.1     rmind 	cv_broadcast(&sme->sme_condvar);
    733  1.85.2.1     rmind 	mutex_exit(&sme->sme_mtx);
    734  1.85.2.1     rmind }
    735  1.85.2.1     rmind 
    736  1.85.2.1     rmind /*
    737  1.85.2.1     rmind  * sysmon_envsys_sensor_event
    738  1.85.2.1     rmind  *
    739  1.85.2.1     rmind  *	+ Find the monitor event of a particular type for a given sensor
    740  1.85.2.1     rmind  *	  on a device and deliver the event if one is required.  If
    741  1.85.2.1     rmind  *	  no event type is specified, deliver all events for the sensor.
    742  1.85.2.1     rmind  */
    743  1.85.2.1     rmind void
    744  1.85.2.1     rmind sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata,
    745  1.85.2.1     rmind 			   int ev_type)
    746  1.85.2.1     rmind {
    747  1.85.2.1     rmind 	sme_event_t *see;
    748  1.85.2.1     rmind 
    749  1.85.2.1     rmind 	mutex_enter(&sme->sme_mtx);
    750  1.85.2.1     rmind 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    751  1.85.2.1     rmind 		if (edata != see->see_edata)
    752  1.85.2.1     rmind 			continue;
    753  1.85.2.1     rmind 		if (ev_type == 0 ||
    754  1.85.2.1     rmind 		    ev_type == see->see_type) {
    755  1.85.2.1     rmind 			sme_deliver_event(see);
    756  1.85.2.1     rmind 			if (ev_type != 0)
    757  1.85.2.1     rmind 				break;
    758      1.67  pgoyette 		}
    759  1.85.2.1     rmind 	}
    760  1.85.2.1     rmind 	mutex_exit(&sme->sme_mtx);
    761  1.85.2.1     rmind }
    762  1.85.2.1     rmind 
    763  1.85.2.1     rmind /*
    764  1.85.2.1     rmind  * sme_deliver_event:
    765  1.85.2.1     rmind  *
    766  1.85.2.1     rmind  * 	+ If new sensor state requires it, send an event to powerd
    767  1.85.2.1     rmind  *
    768  1.85.2.1     rmind  *	  Must be called with the device's sysmon mutex held
    769  1.85.2.1     rmind  *		see->see_sme->sme_mtx
    770  1.85.2.1     rmind  */
    771  1.85.2.1     rmind void
    772  1.85.2.1     rmind sme_deliver_event(sme_event_t *see)
    773  1.85.2.1     rmind {
    774  1.85.2.1     rmind 	envsys_data_t *edata = see->see_edata;
    775  1.85.2.2     rmind 	const struct sme_descr_entry *sdt = NULL;
    776  1.85.2.1     rmind 	const struct sme_sensor_event *sse = sme_sensor_event;
    777  1.85.2.1     rmind 	int i, state = 0;
    778      1.65  pgoyette 
    779  1.85.2.1     rmind 	switch (see->see_type) {
    780  1.85.2.1     rmind 	case PENVSYS_EVENT_LIMITS:
    781  1.85.2.1     rmind 	case PENVSYS_EVENT_CAPACITY:
    782      1.67  pgoyette 		/*
    783      1.67  pgoyette 		 * Send event if state has changed
    784      1.67  pgoyette 		 */
    785      1.58  pgoyette 		if (edata->state == see->see_evsent)
    786      1.58  pgoyette 			break;
    787      1.56  pgoyette 
    788      1.58  pgoyette 		for (i = 0; sse[i].state != -1; i++)
    789      1.58  pgoyette 			if (sse[i].state == edata->state)
    790      1.10   xtraeme 				break;
    791       1.1   xtraeme 
    792      1.58  pgoyette 		if (sse[i].state == -1)
    793      1.58  pgoyette 			break;
    794      1.58  pgoyette 
    795      1.58  pgoyette 		if (edata->state == ENVSYS_SVALID)
    796      1.58  pgoyette 			sysmon_penvsys_event(&see->see_pes,
    797      1.58  pgoyette 					     PENVSYS_EVENT_NORMAL);
    798      1.58  pgoyette 		else
    799      1.58  pgoyette 			sysmon_penvsys_event(&see->see_pes, sse[i].event);
    800      1.58  pgoyette 
    801      1.58  pgoyette 		see->see_evsent = edata->state;
    802      1.83  pgoyette 		DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n",
    803  1.85.2.2     rmind 		    __func__, see->see_sme->sme_name, edata->desc,
    804  1.85.2.2     rmind 		    edata->sensor, edata->state,
    805      1.83  pgoyette 		    (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL :
    806      1.83  pgoyette 			sse[i].event));
    807      1.58  pgoyette 
    808      1.58  pgoyette 		break;
    809       1.1   xtraeme 
    810      1.58  pgoyette 	/*
    811      1.59  pgoyette 	 * Send PENVSYS_EVENT_CRITICAL event if:
    812      1.59  pgoyette 	 *	State has gone from non-CRITICAL to CRITICAL,
    813      1.59  pgoyette 	 *	State remains CRITICAL and value has changed, or
    814      1.59  pgoyette 	 *	State has returned from CRITICAL to non-CRITICAL
    815      1.58  pgoyette 	 */
    816      1.58  pgoyette 	case PENVSYS_EVENT_CRITICAL:
    817      1.59  pgoyette 		if (edata->state == ENVSYS_SVALID &&
    818      1.59  pgoyette 		    see->see_evsent != 0) {
    819      1.58  pgoyette 			sysmon_penvsys_event(&see->see_pes,
    820      1.58  pgoyette 					     PENVSYS_EVENT_NORMAL);
    821      1.59  pgoyette 			see->see_evsent = 0;
    822      1.59  pgoyette 		} else if (edata->state == ENVSYS_SCRITICAL &&
    823      1.59  pgoyette 		    see->see_evsent != edata->value_cur) {
    824      1.58  pgoyette 			sysmon_penvsys_event(&see->see_pes,
    825      1.58  pgoyette 					     PENVSYS_EVENT_CRITICAL);
    826      1.60  pgoyette 			see->see_evsent = edata->value_cur;
    827      1.56  pgoyette 		}
    828       1.1   xtraeme 		break;
    829       1.1   xtraeme 
    830      1.10   xtraeme 	/*
    831      1.28   xtraeme 	 * if value_cur is not normal (battery) or online (drive),
    832      1.28   xtraeme 	 * send the event...
    833      1.10   xtraeme 	 */
    834      1.28   xtraeme 	case PENVSYS_EVENT_STATE_CHANGED:
    835      1.45   xtraeme 		/*
    836      1.45   xtraeme 		 * the state has not been changed, just ignore the event.
    837      1.45   xtraeme 		 */
    838      1.51   xtraeme 		if (edata->value_cur == see->see_evsent)
    839       1.1   xtraeme 			break;
    840       1.1   xtraeme 
    841      1.51   xtraeme 		switch (edata->units) {
    842      1.28   xtraeme 		case ENVSYS_DRIVE:
    843  1.85.2.2     rmind 			sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
    844  1.85.2.2     rmind 			    edata->value_cur);
    845      1.28   xtraeme 			state = ENVSYS_DRIVE_ONLINE;
    846      1.28   xtraeme 			break;
    847      1.44   xtraeme 		case ENVSYS_BATTERY_CAPACITY:
    848  1.85.2.2     rmind 			sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
    849  1.85.2.2     rmind 			    edata->value_cur);
    850      1.44   xtraeme 			state = ENVSYS_BATTERY_CAPACITY_NORMAL;
    851      1.28   xtraeme 			break;
    852      1.34   xtraeme 		default:
    853      1.65  pgoyette 			panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED",
    854      1.35   xtraeme 			    __func__);
    855      1.28   xtraeme 		}
    856      1.28   xtraeme 
    857  1.85.2.2     rmind 		if (sdt->type == -1)
    858      1.51   xtraeme 			break;
    859      1.51   xtraeme 
    860      1.45   xtraeme 		/*
    861      1.45   xtraeme 		 * copy current state description.
    862      1.45   xtraeme 		 */
    863  1.85.2.2     rmind 		(void)strlcpy(see->see_pes.pes_statedesc, sdt->desc,
    864      1.45   xtraeme 		    sizeof(see->see_pes.pes_statedesc));
    865       1.1   xtraeme 
    866      1.65  pgoyette 		if (edata->value_cur == state)
    867      1.65  pgoyette 			/*
    868      1.65  pgoyette 			 * state returned to normal condition
    869      1.65  pgoyette 			 */
    870      1.56  pgoyette 			sysmon_penvsys_event(&see->see_pes,
    871      1.56  pgoyette 					     PENVSYS_EVENT_NORMAL);
    872      1.65  pgoyette 		else
    873      1.65  pgoyette 			/*
    874      1.65  pgoyette 			 * state changed to abnormal condition
    875      1.45   xtraeme 			 */
    876      1.45   xtraeme 			sysmon_penvsys_event(&see->see_pes, see->see_type);
    877      1.65  pgoyette 
    878      1.65  pgoyette 		see->see_evsent = edata->value_cur;
    879       1.1   xtraeme 
    880      1.49   xtraeme 		/*
    881      1.49   xtraeme 		 * There's no need to continue if it's a drive sensor.
    882      1.49   xtraeme 		 */
    883      1.51   xtraeme 		if (edata->units == ENVSYS_DRIVE)
    884      1.49   xtraeme 			break;
    885      1.49   xtraeme 
    886      1.39   xtraeme 		/*
    887      1.39   xtraeme 		 * Check if the system is running in low power and send the
    888      1.39   xtraeme 		 * event to powerd (if running) or shutdown the system
    889      1.39   xtraeme 		 * otherwise.
    890      1.39   xtraeme 		 */
    891      1.39   xtraeme 		if (!sysmon_low_power && sme_event_check_low_power()) {
    892      1.39   xtraeme 			struct penvsys_state pes;
    893      1.39   xtraeme 
    894      1.45   xtraeme 			/*
    895      1.45   xtraeme 			 * Stop the callout and send the 'low-power' event.
    896      1.45   xtraeme 			 */
    897      1.45   xtraeme 			sysmon_low_power = true;
    898  1.85.2.1     rmind 			callout_stop(&see->see_sme->sme_callout);
    899      1.39   xtraeme 			pes.pes_type = PENVSYS_TYPE_BATTERY;
    900      1.39   xtraeme 			sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
    901      1.39   xtraeme 		}
    902       1.1   xtraeme 		break;
    903      1.56  pgoyette 	default:
    904      1.56  pgoyette 		panic("%s: invalid event type %d", __func__, see->see_type);
    905       1.1   xtraeme 	}
    906       1.1   xtraeme }
    907      1.38   xtraeme 
    908      1.47   xtraeme /*
    909      1.51   xtraeme  * Returns true if the system is in low power state: an AC adapter
    910      1.51   xtraeme  * is OFF and all batteries are in LOW/CRITICAL state.
    911      1.47   xtraeme  */
    912      1.44   xtraeme static bool
    913      1.38   xtraeme sme_event_check_low_power(void)
    914      1.38   xtraeme {
    915      1.47   xtraeme 	if (!sme_acadapter_check())
    916      1.47   xtraeme 		return false;
    917      1.47   xtraeme 
    918      1.47   xtraeme 	return sme_battery_check();
    919      1.47   xtraeme }
    920      1.47   xtraeme 
    921      1.51   xtraeme /*
    922      1.51   xtraeme  * Called with the sysmon_envsys device mtx held through the
    923      1.51   xtraeme  * workqueue thread.
    924      1.51   xtraeme  */
    925      1.47   xtraeme static bool
    926      1.47   xtraeme sme_acadapter_check(void)
    927      1.47   xtraeme {
    928      1.38   xtraeme 	struct sysmon_envsys *sme;
    929      1.38   xtraeme 	envsys_data_t *edata;
    930      1.50   xtraeme 	bool dev = false, sensor = false;
    931      1.38   xtraeme 
    932      1.50   xtraeme 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    933      1.50   xtraeme 		if (sme->sme_class == SME_CLASS_ACADAPTER) {
    934      1.50   xtraeme 			dev = true;
    935      1.38   xtraeme 			break;
    936      1.50   xtraeme 		}
    937      1.50   xtraeme 	}
    938      1.51   xtraeme 
    939      1.38   xtraeme 	/*
    940      1.47   xtraeme 	 * No AC Adapter devices were found.
    941      1.38   xtraeme 	 */
    942      1.50   xtraeme 	if (!dev)
    943      1.44   xtraeme 		return false;
    944      1.51   xtraeme 
    945      1.44   xtraeme 	/*
    946      1.47   xtraeme 	 * Check if there's an AC adapter device connected.
    947      1.44   xtraeme 	 */
    948      1.45   xtraeme 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    949      1.38   xtraeme 		if (edata->units == ENVSYS_INDICATOR) {
    950      1.47   xtraeme 			sensor = true;
    951      1.50   xtraeme 			/* refresh current sensor */
    952      1.78  pgoyette 			if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    953      1.78  pgoyette 				(*sme->sme_refresh)(sme, edata);
    954      1.44   xtraeme 			if (edata->value_cur)
    955      1.44   xtraeme 				return false;
    956      1.38   xtraeme 		}
    957      1.38   xtraeme 	}
    958      1.51   xtraeme 
    959      1.47   xtraeme 	if (!sensor)
    960      1.47   xtraeme 		return false;
    961      1.47   xtraeme 
    962      1.51   xtraeme 	/*
    963      1.51   xtraeme 	 * AC adapter found and not connected.
    964      1.51   xtraeme 	 */
    965      1.47   xtraeme 	return true;
    966      1.47   xtraeme }
    967      1.47   xtraeme 
    968      1.51   xtraeme /*
    969      1.51   xtraeme  * Called with the sysmon_envsys device mtx held through the
    970      1.51   xtraeme  * workqueue thread.
    971      1.51   xtraeme  */
    972      1.47   xtraeme static bool
    973      1.47   xtraeme sme_battery_check(void)
    974      1.47   xtraeme {
    975      1.47   xtraeme 	struct sysmon_envsys *sme;
    976      1.47   xtraeme 	envsys_data_t *edata;
    977      1.55  drochner 	int batteriesfound = 0;
    978      1.55  drochner 	bool present, batterycap, batterycharge;
    979      1.44   xtraeme 
    980      1.38   xtraeme 	/*
    981      1.44   xtraeme 	 * Check for battery devices and its state.
    982      1.38   xtraeme 	 */
    983      1.44   xtraeme 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    984      1.44   xtraeme 		if (sme->sme_class != SME_CLASS_BATTERY)
    985      1.44   xtraeme 			continue;
    986      1.38   xtraeme 
    987      1.55  drochner 		present = true;
    988      1.55  drochner 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    989      1.55  drochner 			if (edata->units == ENVSYS_INDICATOR &&
    990      1.55  drochner 			    !edata->value_cur) {
    991      1.55  drochner 				present = false;
    992      1.55  drochner 				break;
    993      1.55  drochner 			}
    994      1.55  drochner 		}
    995      1.55  drochner 		if (!present)
    996      1.55  drochner 			continue;
    997      1.44   xtraeme 		/*
    998      1.44   xtraeme 		 * We've found a battery device...
    999      1.44   xtraeme 		 */
   1000      1.55  drochner 		batteriesfound++;
   1001      1.55  drochner 		batterycap = batterycharge = false;
   1002      1.45   xtraeme 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1003      1.44   xtraeme 			if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1004      1.44   xtraeme 				batterycap = true;
   1005      1.44   xtraeme 				if (!sme_battery_critical(edata))
   1006      1.44   xtraeme 					return false;
   1007      1.44   xtraeme 			} else if (edata->units == ENVSYS_BATTERY_CHARGE) {
   1008      1.44   xtraeme 				batterycharge = true;
   1009      1.44   xtraeme 				if (edata->value_cur)
   1010      1.44   xtraeme 					return false;
   1011      1.38   xtraeme 			}
   1012      1.38   xtraeme 		}
   1013      1.55  drochner 		if (!batterycap || !batterycharge)
   1014      1.55  drochner 			return false;
   1015      1.38   xtraeme 	}
   1016      1.51   xtraeme 
   1017      1.55  drochner 	if (!batteriesfound)
   1018      1.44   xtraeme 		return false;
   1019      1.38   xtraeme 
   1020      1.38   xtraeme 	/*
   1021      1.44   xtraeme 	 * All batteries in low/critical capacity and discharging.
   1022      1.38   xtraeme 	 */
   1023      1.44   xtraeme 	return true;
   1024      1.44   xtraeme }
   1025      1.38   xtraeme 
   1026      1.44   xtraeme static bool
   1027      1.44   xtraeme sme_battery_critical(envsys_data_t *edata)
   1028      1.44   xtraeme {
   1029      1.44   xtraeme 	if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
   1030      1.44   xtraeme 	    edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
   1031      1.44   xtraeme 		return true;
   1032      1.44   xtraeme 
   1033      1.44   xtraeme 	return false;
   1034      1.38   xtraeme }
   1035