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