xref: /netbsd-src/sys/arch/dreamcast/dev/maple/mkbd.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: mkbd.c,v 1.30 2021/04/24 23:36:31 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 Marcus Comstedt
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Marcus Comstedt.
18  * 4. Neither the name of The NetBSD Foundation nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: mkbd.c,v 1.30 2021/04/24 23:36:31 thorpej Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/device.h>
40 #include <sys/fcntl.h>
41 #include <sys/poll.h>
42 #include <sys/select.h>
43 #include <sys/proc.h>
44 #include <sys/signalvar.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 
48 #include "wskbd.h"
49 
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wskbdvar.h>
52 #include <dev/wscons/wsksymdef.h>
53 #include <dev/wscons/wsksymvar.h>
54 
55 #include <machine/cpu.h>
56 
57 #include <dreamcast/dev/maple/maple.h>
58 #include <dreamcast/dev/maple/mapleconf.h>
59 #include <dreamcast/dev/maple/mkbdvar.h>
60 #include <dreamcast/dev/maple/mkbdmap.h>
61 
62 /*
63  * Function declarations.
64  */
65 static	int mkbdmatch(device_t, cfdata_t, void *);
66 static	void mkbdattach(device_t, device_t, void *);
67 static	int mkbddetach(device_t, int);
68 
69 int	mkbd_enable(void *, int);
70 void	mkbd_set_leds(void *, int);
71 int	mkbd_ioctl(void *, u_long, void *, int, struct lwp *);
72 
73 struct wskbd_accessops mkbd_accessops = {
74 	mkbd_enable,
75 	mkbd_set_leds,
76 	mkbd_ioctl,
77 };
78 
79 static void mkbd_intr(void *, struct maple_response *, int, int);
80 
81 void	mkbd_cngetc(void *, u_int *, int *);
82 void	mkbd_cnpollc(void *, int);
83 int	mkbd_cnattach(void);
84 
85 struct wskbd_consops mkbd_consops = {
86 	mkbd_cngetc,
87 	mkbd_cnpollc,
88 };
89 
90 struct wskbd_mapdata mkbd_keymapdata = {
91 	mkbd_keydesctab,
92 	KB_JP,
93 };
94 
95 static struct mkbd_softc *mkbd_console_softc;
96 
97 static int mkbd_is_console;
98 static int mkbd_console_initted;
99 
100 CFATTACH_DECL_NEW(mkbd, sizeof(struct mkbd_softc),
101     mkbdmatch, mkbdattach, mkbddetach, NULL);
102 
103 static int
104 mkbdmatch(device_t parent, cfdata_t cf, void *aux)
105 {
106 	struct maple_attach_args *ma = aux;
107 
108 	return ma->ma_function == MAPLE_FN_KEYBOARD ? MAPLE_MATCH_FUNC : 0;
109 }
110 
111 static void
112 mkbdattach(device_t parent, device_t self, void *aux)
113 {
114 	struct mkbd_softc *sc = device_private(self);
115 	struct maple_attach_args *ma = aux;
116 #if NWSKBD > 0
117 	struct wskbddev_attach_args a;
118 #endif
119 	uint32_t kbdtype;
120 
121 	sc->sc_dev = self;
122 	sc->sc_parent = parent;
123 	sc->sc_unit = ma->ma_unit;
124 
125 	kbdtype = maple_get_function_data(ma->ma_devinfo,
126 	    MAPLE_FN_KEYBOARD) >> 24;
127 	switch (kbdtype) {
128 	case 1:
129 		printf(": Japanese keyboard");
130 		mkbd_keymapdata.layout = KB_JP;
131 		break;
132 	case 2:
133 		printf(": US keyboard");
134 		mkbd_keymapdata.layout = KB_US;
135 		break;
136 	case 3:
137 		printf(": European keyboard");
138 		mkbd_keymapdata.layout = KB_UK;
139 		break;
140 	default:
141 		printf(": Unknown keyboard %d", kbdtype);
142 	}
143 	printf("\n");
144 #ifdef MKBD_LAYOUT
145 	/* allow user to override the default keymap */
146 	mkbd_keymapdata.layout = MKBD_LAYOUT;
147 #endif
148 #ifdef MKBD_SWAPCTRLCAPS
149 	/* allow user to specify swapctrlcaps with the default keymap */
150 	mkbd_keymapdata.layout |= KB_SWAPCTRLCAPS;
151 #endif
152 
153 #if NWSKBD > 0
154 	if ((a.console = mkbd_is_console) != 0) {
155 		mkbd_is_console = 0;
156 		if (!mkbd_console_initted)
157 			wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata);
158 		mkbd_console_softc = sc;
159 	}
160 	a.keymap = &mkbd_keymapdata;
161 	a.accessops = &mkbd_accessops;
162 	a.accesscookie = sc;
163 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARG_EOL);
164 #endif
165 
166 	maple_set_callback(parent, sc->sc_unit, MAPLE_FN_KEYBOARD,
167 	    mkbd_intr, sc);
168 	maple_enable_periodic(parent, sc->sc_unit, MAPLE_FN_KEYBOARD, 1);
169 }
170 
171 static int
172 mkbddetach(device_t self, int flags)
173 {
174 	struct mkbd_softc *sc = device_private(self);
175 	int rv = 0;
176 
177 	if (sc == mkbd_console_softc) {
178 		/*
179 		 * Hack to allow another Maple keyboard to be new console.
180 		 * XXX Should some other type device can be console.
181 		 */
182 		printf("%s: was console keyboard\n", device_xname(sc->sc_dev));
183 		wskbd_cndetach();
184 		mkbd_console_softc = NULL;
185 		mkbd_console_initted = 0;
186 		mkbd_is_console = 1;
187 	}
188 	if (sc->sc_wskbddev)
189 		rv = config_detach(sc->sc_wskbddev, flags);
190 
191 	return rv;
192 }
193 
194 int
195 mkbd_enable(void *v, int on)
196 {
197 
198 	return 0;
199 }
200 
201 void
202 mkbd_set_leds(void *v, int on)
203 {
204 }
205 
206 int
207 mkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
208 {
209 
210 	switch (cmd) {
211 	case WSKBDIO_GTYPE:
212 		*(int *) data = WSKBD_TYPE_MAPLE;
213 		return 0;
214 	case WSKBDIO_SETLEDS:
215 		return 0;
216 	case WSKBDIO_GETLEDS:
217 		*(int *) data = 0;
218 		return 0;
219 	case WSKBDIO_BELL:
220 	case WSKBDIO_COMPLEXBELL:
221 		return 0;
222 	}
223 
224 	return EPASSTHROUGH;
225 }
226 
227 int
228 mkbd_cnattach(void)
229 {
230 
231 	wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata);
232 	mkbd_console_initted = 1;
233 	mkbd_is_console = 1;
234 
235 	return 0;
236 }
237 
238 static int polledkey;
239 extern int maple_polling;
240 
241 #define SHIFT_KEYCODE_BASE 0xe0
242 #define UP_KEYCODE_FLAG 0x1000
243 
244 #define KEY_UP(n) do {							\
245 	if (maple_polling)						\
246 		polledkey = (n)|UP_KEYCODE_FLAG;			\
247 	else								\
248 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, (n));	\
249 	} while (/*CONSTCOND*/0)
250 
251 #define KEY_DOWN(n) do {						\
252 	if (maple_polling)						\
253 		polledkey = (n);					\
254 	else								\
255 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, (n)); \
256 	} while (/*CONSTCOND*/0)
257 
258 #define SHIFT_UP(n)	KEY_UP((n) | SHIFT_KEYCODE_BASE)
259 #define SHIFT_DOWN(n)	KEY_DOWN((n) | SHIFT_KEYCODE_BASE)
260 
261 static void
262 mkbd_intr(void *arg, struct maple_response *response, int sz, int flags)
263 {
264 	struct mkbd_softc *sc = arg;
265 	struct mkbd_condition *kbddata = (void *) response->data;
266 
267 	if ((flags & MAPLE_FLAG_PERIODIC) &&
268 	    sz >= sizeof(struct mkbd_condition)) {
269 		int i, j, v;
270 
271 		v = sc->sc_condition.shift & ~kbddata->shift;
272 		if (v)
273 			for (i = 0; i < 8; i++)
274 				if (v & (1 << i))
275 					SHIFT_UP(i);
276 
277 		v = kbddata->shift & ~sc->sc_condition.shift;
278 		if (v)
279 			for (i = 0; i < 8; i++)
280 				if (v & (1 << i))
281 					SHIFT_DOWN(i);
282 
283 		for (i = 0, j = 0; i < 6; i++)
284 			if (sc->sc_condition.key[i] < 4)
285 				break;
286 			else if (sc->sc_condition.key[i] == kbddata->key[j])
287 				j++;
288 			else
289 				KEY_UP(sc->sc_condition.key[i]);
290 
291 		for (; j < 6; j++)
292 			if (kbddata->key[j] < 4)
293 				break;
294 			else
295 				KEY_DOWN(kbddata->key[j]);
296 
297 		memcpy(&sc->sc_condition, kbddata,
298 		    sizeof(struct mkbd_condition));
299 	}
300 }
301 
302 void
303 mkbd_cngetc(void *v, u_int *type, int *data)
304 {
305 	int key;
306 
307 	polledkey = -1;
308 	maple_polling = 1;
309 	while (polledkey == -1) {
310 		if (mkbd_console_softc != NULL &&
311 		    mkbd_console_softc->sc_parent != NULL) {
312 			DELAY(20000);
313 			maple_run_polling(mkbd_console_softc->sc_parent);
314 		}
315 	}
316 	maple_polling = 0;
317 	key = polledkey;
318 
319 	*data = key & ~UP_KEYCODE_FLAG;
320 	*type = (key & UP_KEYCODE_FLAG) ?
321 	    WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
322 }
323 
324 void
325 mkbd_cnpollc(void *v, int on)
326 {
327 }
328