epockbd.c revision 1.5 1 1.5 thorpej /* $NetBSD: epockbd.c,v 1.5 2021/08/07 16:18:48 thorpej Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2013 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara #include <sys/cdefs.h>
28 1.5 thorpej __KERNEL_RCSID(0, "$NetBSD: epockbd.c,v 1.5 2021/08/07 16:18:48 thorpej Exp $");
29 1.1 kiyohara
30 1.1 kiyohara #include <sys/param.h>
31 1.1 kiyohara #include <sys/bus.h>
32 1.1 kiyohara #include <sys/callout.h>
33 1.1 kiyohara #include <sys/device.h>
34 1.1 kiyohara #include <sys/errno.h>
35 1.1 kiyohara #include <sys/kernel.h>
36 1.1 kiyohara
37 1.1 kiyohara #include <dev/wscons/wsconsio.h>
38 1.1 kiyohara #include <dev/wscons/wskbdvar.h>
39 1.1 kiyohara #include <dev/wscons/wsksymdef.h>
40 1.1 kiyohara #include <dev/wscons/wsksymvar.h>
41 1.1 kiyohara
42 1.1 kiyohara #include <epoc32/dev/epockbdvar.h>
43 1.1 kiyohara #include <epoc32/dev/epockbdmap.h>
44 1.1 kiyohara
45 1.1 kiyohara #define EPOCKBD_COLUMN0 8
46 1.1 kiyohara
47 1.1 kiyohara #define EPOCKBD_SCAN_READ(sc) \
48 1.1 kiyohara bus_space_read_1((sc)->sc_scant, (sc)->sc_scanh, 0)
49 1.1 kiyohara #define EPOCKBD_SCAN_WRITE(sc, cmd) \
50 1.1 kiyohara bus_space_write_1((sc)->sc_scant, (sc)->sc_scanh, 0, (cmd))
51 1.1 kiyohara #define EPOCKBD_DATA_READ(sc) \
52 1.1 kiyohara bus_space_read_1((sc)->sc_datat, (sc)->sc_datah, 0)
53 1.1 kiyohara
54 1.1 kiyohara #define EPOC2WS_KBD_DATA(r, c) (((c) << 3) + (31 - __builtin_clz(r)) + 1)
55 1.1 kiyohara
56 1.1 kiyohara static int epockbd_enable(void *, int);
57 1.1 kiyohara static void epockbd_set_leds(void *, int);
58 1.1 kiyohara static int epockbd_ioctl(void *, u_long, void *, int, struct lwp *);
59 1.1 kiyohara
60 1.1 kiyohara static void epockbd_poll(void *);
61 1.1 kiyohara
62 1.1 kiyohara static void epockbd_cngetc(void *, u_int *, int *);
63 1.1 kiyohara static void epockbd_cnpollc(void *, int);
64 1.1 kiyohara
65 1.1 kiyohara static struct wskbd_consops epockbd_consops = {
66 1.1 kiyohara epockbd_cngetc,
67 1.1 kiyohara epockbd_cnpollc,
68 1.1 kiyohara NULL
69 1.1 kiyohara };
70 1.1 kiyohara
71 1.1 kiyohara static struct wskbd_accessops epockbd_accessops = {
72 1.1 kiyohara epockbd_enable,
73 1.1 kiyohara epockbd_set_leds,
74 1.1 kiyohara epockbd_ioctl,
75 1.1 kiyohara };
76 1.1 kiyohara
77 1.1 kiyohara static struct wskbd_mapdata epockbd_mapdata = {
78 1.1 kiyohara epockbd_keydesctab,
79 1.1 kiyohara KB_UK,
80 1.1 kiyohara };
81 1.1 kiyohara
82 1.1 kiyohara static int is_console;
83 1.1 kiyohara
84 1.1 kiyohara void
85 1.1 kiyohara epockbd_attach(struct epockbd_softc *sc)
86 1.1 kiyohara {
87 1.1 kiyohara struct wskbddev_attach_args aa;
88 1.1 kiyohara
89 1.1 kiyohara aprint_normal("\n");
90 1.1 kiyohara aprint_naive("\n");
91 1.1 kiyohara
92 1.1 kiyohara if (is_console)
93 1.1 kiyohara wskbd_cnattach(&epockbd_consops, sc, &epockbd_mapdata);
94 1.1 kiyohara
95 1.1 kiyohara callout_init(&sc->sc_c, 0);
96 1.1 kiyohara callout_reset(&sc->sc_c, hz, epockbd_poll, sc);
97 1.1 kiyohara sc->sc_flags |= EPOCKBD_FLAG_ENABLE;
98 1.1 kiyohara
99 1.1 kiyohara aa.console = is_console;
100 1.1 kiyohara aa.keymap = &epockbd_mapdata;
101 1.1 kiyohara aa.accessops = &epockbd_accessops;
102 1.1 kiyohara aa.accesscookie = sc;
103 1.4 thorpej sc->sc_wskbddev = config_found(sc->sc_dev, &aa, wskbddevprint,
104 1.5 thorpej CFARGS_NONE);
105 1.1 kiyohara }
106 1.1 kiyohara
107 1.1 kiyohara /*
108 1.1 kiyohara * wskbd(9) functions
109 1.1 kiyohara */
110 1.1 kiyohara
111 1.1 kiyohara static int
112 1.1 kiyohara epockbd_enable(void *v, int on)
113 1.1 kiyohara {
114 1.1 kiyohara struct epockbd_softc *sc = v;
115 1.1 kiyohara
116 1.1 kiyohara if (on) {
117 1.1 kiyohara sc->sc_flags |= EPOCKBD_FLAG_ENABLE;
118 1.1 kiyohara callout_schedule(&sc->sc_c, hz / 20);
119 1.1 kiyohara } else {
120 1.1 kiyohara sc->sc_flags &= ~EPOCKBD_FLAG_ENABLE;
121 1.1 kiyohara callout_stop(&sc->sc_c);
122 1.1 kiyohara }
123 1.1 kiyohara return 0;
124 1.1 kiyohara }
125 1.1 kiyohara
126 1.1 kiyohara static void
127 1.1 kiyohara epockbd_set_leds(void *v, int on)
128 1.1 kiyohara {
129 1.1 kiyohara /* Nothing */
130 1.1 kiyohara }
131 1.1 kiyohara
132 1.1 kiyohara static int
133 1.1 kiyohara epockbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
134 1.1 kiyohara {
135 1.3 skrll #ifdef WSDISPLAY_COMPAT_RAWKBD
136 1.3 skrll struct epockbd_softc *sc = v;
137 1.3 skrll #endif
138 1.1 kiyohara
139 1.1 kiyohara switch (cmd) {
140 1.1 kiyohara case WSKBDIO_GTYPE:
141 1.1 kiyohara *(int *)data = WSKBD_TYPE_EPOC;
142 1.1 kiyohara return 0;
143 1.1 kiyohara
144 1.1 kiyohara case WSKBDIO_SETLEDS:
145 1.1 kiyohara return 0;
146 1.1 kiyohara
147 1.1 kiyohara case WSKBDIO_GETLEDS:
148 1.1 kiyohara *(int *)data = 0;
149 1.1 kiyohara return 0;
150 1.1 kiyohara
151 1.1 kiyohara #ifdef WSDISPLAY_COMPAT_RAWKBD
152 1.1 kiyohara case WSKBDIO_SETMODE:
153 1.3 skrll *(int *)data = WSKBD_RAW;
154 1.1 kiyohara sc->sc_flags |= EPOCKBD_FLAG_RAW;
155 1.1 kiyohara
156 1.1 kiyohara /* Not yet...: return 0; */
157 1.1 kiyohara #endif
158 1.1 kiyohara }
159 1.1 kiyohara return EPASSTHROUGH;
160 1.1 kiyohara }
161 1.1 kiyohara
162 1.1 kiyohara static void
163 1.1 kiyohara epockbd_poll(void *v)
164 1.1 kiyohara {
165 1.1 kiyohara struct epockbd_softc *sc = v;
166 1.1 kiyohara u_int type;
167 1.1 kiyohara int data;
168 1.1 kiyohara
169 1.1 kiyohara epockbd_cngetc(v, &type, &data);
170 1.1 kiyohara if (data != 0)
171 1.1 kiyohara wskbd_input(sc->sc_wskbddev, type, data);
172 1.1 kiyohara
173 1.1 kiyohara callout_schedule(&sc->sc_c, hz / 20);
174 1.1 kiyohara }
175 1.1 kiyohara
176 1.1 kiyohara void
177 1.1 kiyohara epockbd_cnattach(void)
178 1.1 kiyohara {
179 1.1 kiyohara
180 1.1 kiyohara is_console = 1;
181 1.1 kiyohara }
182 1.1 kiyohara
183 1.1 kiyohara static void
184 1.1 kiyohara epockbd_cngetc(void *conscookie, u_int *type, int *data)
185 1.1 kiyohara {
186 1.1 kiyohara struct epockbd_softc *sc = conscookie;
187 1.1 kiyohara uint8_t cmd, key, mask;
188 1.1 kiyohara int column, row;
189 1.2 kiyohara #if 1
190 1.2 kiyohara /*
191 1.2 kiyohara * For some machines which return a strange response, it calculates
192 1.2 kiyohara * using this variable.
193 1.2 kiyohara */
194 1.2 kiyohara int pc = 0, pr = 0;
195 1.2 kiyohara #endif
196 1.1 kiyohara
197 1.1 kiyohara *type = 0;
198 1.1 kiyohara *data = 0;
199 1.1 kiyohara mask = (1 << sc->sc_kbd_nrow) - 1;
200 1.1 kiyohara for (column = 0; column < sc->sc_kbd_ncolumn; column++) {
201 1.1 kiyohara delay(16);
202 1.1 kiyohara cmd = EPOCKBD_SCAN_READ(sc);
203 1.1 kiyohara cmd &= ~0xf;
204 1.1 kiyohara cmd |= (EPOCKBD_COLUMN0 + column);
205 1.1 kiyohara EPOCKBD_SCAN_WRITE(sc, cmd);
206 1.1 kiyohara delay(16);
207 1.1 kiyohara key = EPOCKBD_DATA_READ(sc) & mask;
208 1.1 kiyohara if (sc->sc_state[column] != key) {
209 1.1 kiyohara row = sc->sc_state[column] ^ key;
210 1.1 kiyohara sc->sc_state[column] = key;
211 1.2 kiyohara #if 1
212 1.2 kiyohara if (*data == 0 ||
213 1.2 kiyohara (row == pr && pc == 0 &&
214 1.2 kiyohara column == sc->sc_kbd_ncolumn - 1))
215 1.2 kiyohara #else
216 1.2 kiyohara if (*data == 0)
217 1.2 kiyohara #endif
218 1.2 kiyohara {
219 1.1 kiyohara if (key & row)
220 1.1 kiyohara *type = WSCONS_EVENT_KEY_DOWN;
221 1.1 kiyohara else
222 1.1 kiyohara *type = WSCONS_EVENT_KEY_UP;
223 1.1 kiyohara *data = EPOC2WS_KBD_DATA(row, column);
224 1.2 kiyohara #if 1
225 1.2 kiyohara pc = column;
226 1.2 kiyohara pr = row;
227 1.2 kiyohara #endif
228 1.1 kiyohara }
229 1.1 kiyohara }
230 1.1 kiyohara }
231 1.1 kiyohara }
232 1.1 kiyohara
233 1.1 kiyohara /* ARGSUSED */
234 1.1 kiyohara static void
235 1.1 kiyohara epockbd_cnpollc(void *conckookie, int on)
236 1.1 kiyohara {
237 1.1 kiyohara /* Nothing */
238 1.1 kiyohara }
239