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