/*
* Copyright (c) 2011 Tetsuya Isaki. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
struct powsw_softc {
device_t sc_dev;
struct sysmon_pswitch sc_smpsw;
callout_t sc_callout;
int sc_mask;
int sc_prev;
int sc_last_sw;
int sc_tick;
int sc_count;
#if defined(POWSW_DEBUG)
char sc_log[100];
int sc_loglen;
#endif
};
/* Set AER and enable interrupt */
sw = (mfp_get_gpip() & sc->sc_mask);
powsw_set_aer(sc, sw ? 0 : 1);
mfp_bit_set_ierb(sc->sc_mask);
aprint_normal(": %s\n", desc->name);
}
static int
powsw_intr(void *arg)
{
struct powsw_softc *sc = arg;
if (sc->sc_tick == 0) {
mfp_bit_clear_ierb(sc->sc_mask);
sc->sc_tick++;
DEBUG_LOG_ADD('i');
/*
* The button state seems unstable for few ticks,
* so wait a bit to settle.
*/
callout_schedule(&sc->sc_callout, 1);
} else {
DEBUG_LOG_ADD('x');
}
return 0;
}
void
powsw_softintr(void *arg)
{
struct powsw_softc *sc = arg;
int sw;
int s;
s = spl6();
if (sc->sc_tick++ >= POWSW_MAX_TICK) {
/* tick is over, broken switch? */
printf("%s: unstable power switch?, ignored\n",
device_xname(sc->sc_dev));
powsw_reset_counter(sc);
if (sc->sc_last_sw == sc->sc_prev) {
/* switch state is not changed, it was a noise */
DPRINTF(" ignore(sw=%d,prev=%d)\n",
sc->sc_last_sw, sc->sc_prev);
} else {
/* switch state has been changed */
sc->sc_prev = sc->sc_last_sw;
powsw_set_aer(sc, 1 - sc->sc_prev);
sysmon_task_queue_sched(0, powsw_pswitch_event, sc);
}
powsw_reset_counter(sc);
/* enable interrupt */
mfp_bit_set_ierb(sc->sc_mask);
}