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