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