xref: /netbsd-src/sys/dev/ic/lcdkp_subr.c (revision f46b67cca242df254dcefdbccaf14f5fd403ceb5)
1 /* $NetBSD: lcdkp_subr.c,v 1.7 2011/05/14 02:58:27 rmind Exp $ */
2 
3 /*
4  * Copyright (c) 2002 Dennis I. Chernoivanov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Subroutines for simple one-port keypad.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: lcdkp_subr.c,v 1.7 2011/05/14 02:58:27 rmind Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/conf.h>
41 #include <sys/kernel.h>
42 #include <sys/types.h>
43 
44 #include <machine/autoconf.h>
45 #include <sys/intr.h>
46 #include <sys/bus.h>
47 
48 #include <dev/ic/lcdkp_subr.h>
49 
50 #define HD_POLL_RATE	(hz / 10)
51 
52 static int    lcdkp_poll(struct lcdkp_chip *);
53 static u_char lcdkp_scan(struct lcdkp_chip *, u_int8_t *);
54 
55 /*
56  * Initialization.
57  */
58 void
lcdkp_attach_subr(struct lcdkp_chip * sc)59 lcdkp_attach_subr(struct lcdkp_chip *sc)
60 {
61 
62 	sc->sc_flags = 0x0;
63 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
64 }
65 
66 /*
67  * Scan whether input is pending, don't block if not.
68  */
69 int
lcdkp_scankey(struct lcdkp_chip * sc)70 lcdkp_scankey(struct lcdkp_chip *sc)
71 {
72 	int ret;
73 
74 	if ((sc->sc_knum == 0) || (sc->sc_kpad == NULL))
75 		return 0;
76 
77 	mutex_enter(&sc->sc_lock);
78 	if (!(sc->sc_flags & LCDKP_HAS_BUF)) {
79 		u_int8_t b;
80 		if (lcdkp_scan(sc, &b) != 0) {
81 			sc->sc_buf = b;
82 			sc->sc_flags |= LCDKP_HAS_BUF;
83 		}
84 	}
85 	ret = (sc->sc_flags & LCDKP_HAS_BUF);
86 	mutex_exit(&sc->sc_lock);
87 
88 	return ret;
89 }
90 
91 /*
92  * Read new key code, block if needed.
93  */
94 int
lcdkp_readkey(struct lcdkp_chip * sc,u_int8_t * result)95 lcdkp_readkey(struct lcdkp_chip *sc, u_int8_t *result)
96 {
97 	int error;
98 
99 	if ((sc->sc_knum == 0) || (sc->sc_kpad == NULL))
100 		return EIO;
101 
102 	mutex_enter(&sc->sc_lock);
103 	if ( (error = lcdkp_poll(sc)) == 0) {
104 		*result = sc->sc_buf;
105 		sc->sc_flags &= ~LCDKP_HAS_BUF;
106 	}
107 	mutex_exit(&sc->sc_lock);
108 
109 	return 0;
110 }
111 
112 /*
113  * Scan the keypad and translate the input.
114  */
115 static u_char
lcdkp_scan(struct lcdkp_chip * sc,u_int8_t * b)116 lcdkp_scan(struct lcdkp_chip *sc, u_int8_t *b)
117 {
118 	u_int8_t i;
119 	u_int8_t c;
120 
121 	c = lcdkp_dr_read(sc);
122 	for (i = 0; i < sc->sc_knum; i++) {
123 		if (sc->sc_kpad[i].x_keycode == c) {
124 			*b = sc->sc_kpad[i].x_outcode;
125 			return 1;
126 		}
127 	}
128 	return 0;
129 }
130 
131 /*
132  * Block until input is available.
133  */
134 static int
lcdkp_poll(struct lcdkp_chip * sc)135 lcdkp_poll(struct lcdkp_chip *sc)
136 {
137 	int error;
138 	uint8_t b;
139 
140 	KASSERT(mutex_owned(&sc->sc_lock));
141 
142 	if (sc->sc_flags & LCDKP_HAS_BUF) {
143 		return 0;
144 	}
145 	while (lcdkp_scan(sc, &b) == 0) {
146 		error = mtsleep((void*)sc, PRIBIO | PCATCH, "kppoll",
147 		    HD_POLL_RATE, &sc->sc_lock);
148 		if (error != EWOULDBLOCK) {
149 			if (lcdkp_scan(sc, &b) != 0)
150 				break;
151 			return EINTR;
152 		}
153 	}
154 	sc->sc_buf = b;
155 	sc->sc_flags |= LCDKP_HAS_BUF;
156 	return 0;
157 }
158