summaryrefslogtreecommitdiff
path: root/sys/dev/ic/matrixkp_subr.c
blob: 2c33ae78a5652660a43bcd1690c8084208bad63c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (c) 2005 Jesse Off.  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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 *
 *
 * The matrix keypad is a primitive type of keying device
 * commonly used in systems as a small, cheap, easy-to-build and rugged
 * way to get user input in a variety of embedded environments.  This
 * driver can work for any size of keypad.  A one key keypad (aka
 * button) can also be used.  The theory of operation is described
 * thusly:
 *
 * 	1) The keypad is connected to the NetBSD embedded system
 * 	with digital I/O (DIO) pins connected to each column of
 * 	the keypad and also to each row of the keypad.
 *
 * 	2) When a button is pressed, a short is made between a
 * 	column line and the intersecting row line.
 *
 * 	3) Software is responsible to poll each row/column individually
 * 	and also to debounce any key presses.
 *
 * To correctly wire up such a thing requires the input DIO
 * lines to have pull-up resistors, otherwise an input may be read as a random
 * value if not currently being shorted by a button press.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: matrixkp_subr.c,v 1.7 2007/10/19 11:59:55 ad Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/callout.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/types.h>

#include <machine/autoconf.h>
#include <sys/intr.h>
#include <sys/bus.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsksymvar.h>

#include <dev/ic/matrixkpvar.h>

#define TV_ELAPSED_US(x, y)	(((x).tv_sec - (y).tv_sec) * 1000000 + \
	((x).tv_usec - (y).tv_usec))

const struct wskbd_accessops mxkp_accessops = {
	mxkp_enable,
	mxkp_set_leds,
	mxkp_ioctl,
};

void
mxkp_attach(struct matrixkp_softc *sc)
{
	u_int32_t i;

	callout_init(&sc->sc_callout, 0);
	callout_setfunc(&sc->sc_callout, mxkp_poll, sc);
	if (sc->poll_freq > hz || sc->poll_freq == 0)
		sc->poll_freq = hz;
	sc->sc_enabled = 0;
	if (sc->debounce_stable_ms == 0)
		sc->sc_flags |= MXKP_NODEBOUNCE;
	if (sc->mxkp_event == NULL)
		sc->mxkp_event = mxkp_wskbd_event;
	FOR_KEYS(i, sc->mxkp_pressed[i] = 0);
}

void
mxkp_poll(void *arg)
{
	struct matrixkp_softc *sc = (struct matrixkp_softc *)arg;
	u_int32_t i, anychanged;
	u_int32_t scanned[(MAXNKEYS + 31) / 32];
	u_int32_t changed[(MAXNKEYS + 31) / 32];
	u_int32_t set[(MAXNKEYS + 31) / 32];
	u_int32_t cleared[(MAXNKEYS + 31) / 32];

rescan:
	anychanged = 0;
	FOR_KEYS(i, scanned[i] = 0);
	sc->mxkp_scankeys(sc, scanned);
	FOR_KEYS(i, changed[i] = sc->mxkp_pressed[i] ^ scanned[i]);
	FOR_KEYS(i, anychanged |= changed[i]);

	if (!(sc->sc_flags & MXKP_NODEBOUNCE) && anychanged) {
		mxkp_debounce(sc, changed, scanned);
		anychanged = 0;
		FOR_KEYS(i, changed[i] &= sc->mxkp_pressed[i] ^ scanned[i]);
		FOR_KEYS(i, anychanged |= changed[i]);
	}
	if (anychanged) {
		FOR_KEYS(i, set[i] = changed[i] & scanned[i]);
		FOR_KEYS(i, cleared[i] = changed[i] & sc->mxkp_pressed[i]);
		sc->mxkp_event(sc, set, cleared);
		FOR_KEYS(i, sc->mxkp_pressed[i] &= ~cleared[i]);
		FOR_KEYS(i, sc->mxkp_pressed[i] |= set[i]);
		goto rescan;
	}
	if (sc->sc_enabled)
		callout_schedule(&sc->sc_callout, hz / sc->poll_freq);
}

/*
 * debounce will return when masked keys have been stable
 * for sc->debounce_stable_ms
 */
void
mxkp_debounce(struct matrixkp_softc *sc, u_int32_t *mask, u_int32_t *scan) {
	struct timeval verystart, start, now;
	u_int32_t last_val[(MAXNKEYS + 31) / 32];
	u_int32_t anyset, i;

	FOR_KEYS(i, last_val[i] = scan[i]);
	microtime(&verystart);
	start = verystart;
	do {
		FOR_KEYS(i, scan[i] = 0);
		sc->mxkp_scankeys(sc, scan);
		microtime(&now);
		anyset = 0;
		FOR_KEYS(i, anyset |= (scan[i] ^ last_val[i]) & mask[i]);
		if (anyset) /* bounce detected */
			start = now;
		FOR_KEYS(i, last_val[i] = scan[i]);
	} while (TV_ELAPSED_US(now, start) <= (sc->debounce_stable_ms * 1000));
}

void
mxkp_wskbd_event(struct matrixkp_softc *sc, u_int32_t *on, u_int32_t *off)
{
	unsigned int i;

	for(i = 0; i < sc->mxkp_nkeys; i++) {
		if (off[i / 32] & (1 << (i % 32))) {
			wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, i);
		}
	}
	for(i = 0; i < sc->mxkp_nkeys; i++) {
		if (on[i / 32] & (1 << (i % 32))) {
			wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, i);
		}
	}
}

int
mxkp_enable(void *v, int on)
{
	struct matrixkp_softc *sc = v;

	if (on) {
		if (sc->sc_enabled)
			return EBUSY;

		sc->sc_enabled = 1;
		callout_schedule(&sc->sc_callout, hz / sc->poll_freq);
	} else {
		sc->sc_enabled = 0;
	}

	return 0;
}

void
mxkp_set_leds(void *v, int leds)
{
}

int
mxkp_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
	switch (cmd) {
	case WSKBDIO_GTYPE:
		*(int *)data = WSKBD_TYPE_MATRIXKP;
		return 0;
	case WSKBDIO_SETLEDS:
		return 0;
	case WSKBDIO_GETLEDS:
		*(int *)data = 0;
		return 0;
	case WSKBDIO_COMPLEXBELL:
		return 0;
	}
	return EPASSTHROUGH;
}