Home | History | Annotate | Line # | Download | only in ic
matrixkp_subr.c revision 1.2
      1  1.1   joff /*
      2  1.1   joff  * Copyright (c) 2005 Jesse Off.  All rights reserved.
      3  1.1   joff  *
      4  1.1   joff  * Redistribution and use in source and binary forms, with or without
      5  1.1   joff  * modification, are permitted provided that the following conditions
      6  1.1   joff  * are met:
      7  1.1   joff  * 1. Redistributions of source code must retain the above copyright
      8  1.1   joff  *    notice, this list of conditions and the following disclaimer.
      9  1.1   joff  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1   joff  *    notice, this list of conditions and the following disclaimer in the
     11  1.1   joff  *    documentation and/or other materials provided with the distribution.
     12  1.1   joff  *
     13  1.1   joff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     14  1.1   joff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  1.1   joff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     16  1.1   joff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     17  1.1   joff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18  1.1   joff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     19  1.1   joff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     20  1.1   joff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21  1.1   joff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22  1.1   joff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23  1.1   joff  * SUCH DAMAGE.
     24  1.1   joff  *
     25  1.1   joff  *
     26  1.1   joff  * The matrix keypad is a primitive type of keying device
     27  1.1   joff  * commonly used in systems as a small, cheap, easy-to-build and rugged
     28  1.1   joff  * way to get user input in a variety of embedded environments.  This
     29  1.1   joff  * driver can work for any size of keypad.  A one key keypad (aka
     30  1.1   joff  * button) can also be used.  The theory of operation is described
     31  1.1   joff  * thusly:
     32  1.2  perry  *
     33  1.1   joff  * 	1) The keypad is connected to the NetBSD embedded system
     34  1.1   joff  * 	with digital I/O (DIO) pins connected to each column of
     35  1.1   joff  * 	the keypad and also to each row of the keypad.
     36  1.2  perry  *
     37  1.1   joff  * 	2) When a button is pressed, a short is made between a
     38  1.1   joff  * 	column line and the intersecting row line.
     39  1.2  perry  *
     40  1.1   joff  * 	3) Software is responsible to poll each row/column individually
     41  1.1   joff  * 	and also to debounce any key presses.
     42  1.2  perry  *
     43  1.1   joff  * To correctly wire up such a thing requires the input DIO
     44  1.1   joff  * lines to have pull-up resistors, otherwise an input may be read as a random
     45  1.1   joff  * value if not currently being shorted by a button press.
     46  1.1   joff  */
     47  1.1   joff 
     48  1.1   joff #include <sys/cdefs.h>
     49  1.2  perry __KERNEL_RCSID(0, "$NetBSD: matrixkp_subr.c,v 1.2 2005/02/27 00:27:02 perry Exp $");
     50  1.1   joff 
     51  1.1   joff #include <sys/param.h>
     52  1.1   joff #include <sys/systm.h>
     53  1.1   joff #include <sys/proc.h>
     54  1.1   joff #include <sys/callout.h>
     55  1.1   joff #include <sys/conf.h>
     56  1.1   joff #include <sys/kernel.h>
     57  1.1   joff #include <sys/types.h>
     58  1.1   joff 
     59  1.1   joff #include <machine/autoconf.h>
     60  1.1   joff #include <machine/intr.h>
     61  1.1   joff #include <machine/bus.h>
     62  1.1   joff 
     63  1.1   joff #include <dev/wscons/wsconsio.h>
     64  1.1   joff #include <dev/wscons/wskbdvar.h>
     65  1.1   joff #include <dev/wscons/wsksymdef.h>
     66  1.1   joff #include <dev/wscons/wsksymvar.h>
     67  1.1   joff 
     68  1.1   joff #include <dev/ic/matrixkpvar.h>
     69  1.1   joff 
     70  1.1   joff #define TV_ELAPSED_US(x, y)	(((x).tv_sec - (y).tv_sec) * 1000000 + \
     71  1.1   joff 	((x).tv_usec - (y).tv_usec))
     72  1.1   joff 
     73  1.1   joff const struct wskbd_accessops mxkp_accessops = {
     74  1.1   joff 	mxkp_enable,
     75  1.1   joff 	mxkp_set_leds,
     76  1.1   joff 	mxkp_ioctl,
     77  1.1   joff };
     78  1.1   joff 
     79  1.1   joff void
     80  1.1   joff mxkp_attach(struct matrixkp_softc *sc)
     81  1.1   joff {
     82  1.1   joff 	u_int32_t i;
     83  1.1   joff 
     84  1.1   joff 	callout_init(&sc->sc_callout);
     85  1.1   joff 	callout_setfunc(&sc->sc_callout, mxkp_poll, sc);
     86  1.1   joff 	if (sc->poll_freq > hz || sc->poll_freq == 0)
     87  1.1   joff 		sc->poll_freq = hz;
     88  1.1   joff 	sc->sc_enabled = 0;
     89  1.1   joff 	if (sc->debounce_stable_ms == 0)
     90  1.1   joff 		sc->sc_flags |= MXKP_NODEBOUNCE;
     91  1.1   joff 	if (sc->mxkp_event == NULL)
     92  1.1   joff 		sc->mxkp_event = mxkp_wskbd_event;
     93  1.1   joff 	FOR_KEYS(i, sc->mxkp_pressed[i] = 0);
     94  1.1   joff }
     95  1.1   joff 
     96  1.1   joff void
     97  1.1   joff mxkp_poll(void *arg)
     98  1.1   joff {
     99  1.1   joff 	struct matrixkp_softc *sc = (struct matrixkp_softc *)arg;
    100  1.1   joff 	u_int32_t i, anychanged;
    101  1.1   joff 	u_int32_t scanned[(MAXNKEYS + 31) / 32];
    102  1.1   joff 	u_int32_t changed[(MAXNKEYS + 31) / 32];
    103  1.1   joff 	u_int32_t set[(MAXNKEYS + 31) / 32];
    104  1.1   joff 	u_int32_t cleared[(MAXNKEYS + 31) / 32];
    105  1.1   joff 
    106  1.1   joff rescan:
    107  1.1   joff 	anychanged = 0;
    108  1.1   joff 	FOR_KEYS(i, scanned[i] = 0);
    109  1.1   joff 	sc->mxkp_scankeys(sc, scanned);
    110  1.1   joff 	FOR_KEYS(i, changed[i] = sc->mxkp_pressed[i] ^ scanned[i]);
    111  1.1   joff 	FOR_KEYS(i, anychanged |= changed[i]);
    112  1.2  perry 
    113  1.1   joff 	if (!(sc->sc_flags & MXKP_NODEBOUNCE) && anychanged) {
    114  1.1   joff 		mxkp_debounce(sc, changed, scanned);
    115  1.1   joff 		anychanged = 0;
    116  1.1   joff 		FOR_KEYS(i, changed[i] &= sc->mxkp_pressed[i] ^ scanned[i]);
    117  1.1   joff 		FOR_KEYS(i, anychanged |= changed[i]);
    118  1.1   joff 	}
    119  1.1   joff 	if (anychanged) {
    120  1.1   joff 		FOR_KEYS(i, set[i] = changed[i] & scanned[i]);
    121  1.1   joff 		FOR_KEYS(i, cleared[i] = changed[i] & sc->mxkp_pressed[i]);
    122  1.1   joff 		sc->mxkp_event(sc, set, cleared);
    123  1.1   joff 		FOR_KEYS(i, sc->mxkp_pressed[i] &= ~cleared[i]);
    124  1.1   joff 		FOR_KEYS(i, sc->mxkp_pressed[i] |= set[i]);
    125  1.1   joff 		goto rescan;
    126  1.1   joff 	}
    127  1.1   joff 	if (sc->sc_enabled)
    128  1.1   joff 		callout_schedule(&sc->sc_callout, hz / sc->poll_freq);
    129  1.1   joff }
    130  1.1   joff 
    131  1.1   joff /*
    132  1.2  perry  * debounce will return when masked keys have been stable
    133  1.2  perry  * for sc->debounce_stable_ms
    134  1.1   joff  */
    135  1.2  perry void
    136  1.1   joff mxkp_debounce(struct matrixkp_softc *sc, u_int32_t *mask, u_int32_t *scan) {
    137  1.1   joff 	struct timeval verystart, start, now;
    138  1.1   joff 	u_int32_t last_val[(MAXNKEYS + 31) / 32];
    139  1.1   joff 	u_int32_t anyset, i;
    140  1.2  perry 
    141  1.1   joff 	FOR_KEYS(i, last_val[i] = scan[i]);
    142  1.1   joff 	microtime(&verystart);
    143  1.1   joff 	start = verystart;
    144  1.1   joff 	do {
    145  1.1   joff 		FOR_KEYS(i, scan[i] = 0);
    146  1.1   joff 		sc->mxkp_scankeys(sc, scan);
    147  1.1   joff 		microtime(&now);
    148  1.1   joff 		anyset = 0;
    149  1.1   joff 		FOR_KEYS(i, anyset |= (scan[i] ^ last_val[i]) & mask[i]);
    150  1.1   joff 		if (anyset) /* bounce detected */
    151  1.1   joff 			start = now;
    152  1.1   joff 		FOR_KEYS(i, last_val[i] = scan[i]);
    153  1.1   joff 	} while (TV_ELAPSED_US(now, start) <= (sc->debounce_stable_ms * 1000));
    154  1.1   joff }
    155  1.1   joff 
    156  1.1   joff void
    157  1.1   joff mxkp_wskbd_event(struct matrixkp_softc *sc, u_int32_t *on, u_int32_t *off)
    158  1.1   joff {
    159  1.1   joff 	unsigned int i;
    160  1.1   joff 
    161  1.1   joff 	for(i = 0; i < sc->mxkp_nkeys; i++) {
    162  1.1   joff 		if (off[i / 32] & (1 << (i % 32))) {
    163  1.1   joff 			wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, i);
    164  1.1   joff 		}
    165  1.1   joff 	}
    166  1.1   joff 	for(i = 0; i < sc->mxkp_nkeys; i++) {
    167  1.1   joff 		if (on[i / 32] & (1 << (i % 32))) {
    168  1.1   joff 			wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, i);
    169  1.1   joff 		}
    170  1.1   joff 	}
    171  1.1   joff }
    172  1.1   joff 
    173  1.1   joff int
    174  1.1   joff mxkp_enable(void *v, int on)
    175  1.1   joff {
    176  1.1   joff 	struct matrixkp_softc *sc = v;
    177  1.1   joff 
    178  1.1   joff 	if (on) {
    179  1.1   joff 		if (sc->sc_enabled)
    180  1.1   joff 			return EBUSY;
    181  1.1   joff 
    182  1.1   joff 		sc->sc_enabled = 1;
    183  1.1   joff 		callout_schedule(&sc->sc_callout, hz / sc->poll_freq);
    184  1.1   joff 	} else {
    185  1.1   joff 		sc->sc_enabled = 0;
    186  1.1   joff 	}
    187  1.1   joff 
    188  1.1   joff 	return 0;
    189  1.1   joff }
    190  1.1   joff 
    191  1.1   joff void
    192  1.1   joff mxkp_set_leds(void *v, int leds)
    193  1.1   joff {
    194  1.1   joff }
    195  1.1   joff 
    196  1.1   joff int
    197  1.1   joff mxkp_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    198  1.1   joff {
    199  1.1   joff 	switch (cmd) {
    200  1.1   joff 	case WSKBDIO_GTYPE:
    201  1.1   joff 		*(int *)data = WSKBD_TYPE_MATRIXKP;
    202  1.1   joff 		return 0;
    203  1.1   joff 	case WSKBDIO_SETLEDS:
    204  1.1   joff 		return 0;
    205  1.1   joff 	case WSKBDIO_GETLEDS:
    206  1.1   joff 		*(int *)data = 0;
    207  1.1   joff 		return 0;
    208  1.1   joff 	case WSKBDIO_COMPLEXBELL:
    209  1.1   joff 		return 0;
    210  1.1   joff 	}
    211  1.1   joff 	return EPASSTHROUGH;
    212  1.1   joff }
    213