xref: /netbsd-src/sys/arch/next68k/dev/nextkbd.c (revision 1d39c7845c7d63630dec9db1282715af056f440e)
1 /* $NetBSD: nextkbd.c,v 1.19 2023/02/03 23:13:00 tsutsui Exp $ */
2 /*
3  * Copyright (c) 1998 Matt DeBergalis
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  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Matt DeBergalis
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: nextkbd.c,v 1.19 2023/02/03 23:13:00 tsutsui Exp $");
34 
35 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/device.h>
42 #include <sys/kmem.h>
43 #include <sys/errno.h>
44 #include <sys/queue.h>
45 #include <sys/bus.h>
46 #include <sys/cpu.h>
47 #include <sys/intr.h>
48 
49 #include <machine/autoconf.h>
50 
51 #include <dev/wscons/wsconsio.h>
52 #include <dev/wscons/wskbdvar.h>
53 #include <dev/wscons/wsksymdef.h>
54 #include <dev/wscons/wsksymvar.h>
55 
56 #include <next68k/dev/nextkbdvar.h>
57 #include <next68k/dev/wskbdmap_next.h>
58 
59 #include <next68k/next68k/isr.h>
60 
61 #include <next68k/dev/intiovar.h>
62 
63 struct nextkbd_internal {
64 	int num_ints; /* interrupt total */
65 	int polling;
66 	int isconsole;
67 
68 	bus_space_tag_t iot;
69 	bus_space_handle_t ioh;
70 	struct nextkbd_softc *t_sc; /* back pointer */
71 	uint32_t mods;
72 };
73 
74 struct mon_regs {
75 	uint32_t mon_csr;
76 	uint32_t mon_1;
77 	uint32_t mon_data;
78 };
79 
80 static int attached = 0;
81 
82 int nextkbd_match(device_t, cfdata_t, void *);
83 void nextkbd_attach(device_t, device_t, void *);
84 
85 int nextkbc_cnattach(bus_space_tag_t);
86 
87 CFATTACH_DECL_NEW(nextkbd, sizeof(struct nextkbd_softc),
88     nextkbd_match, nextkbd_attach, NULL, NULL);
89 
90 int	nextkbd_enable(void *, int);
91 void	nextkbd_set_leds(void *, int);
92 int	nextkbd_ioctl(void *, u_long, void *, int, struct lwp *);
93 
94 const struct wskbd_accessops nextkbd_accessops = {
95 	nextkbd_enable,
96 	nextkbd_set_leds,
97 	nextkbd_ioctl,
98 };
99 
100 void	nextkbd_cngetc(void *, u_int *, int *);
101 void	nextkbd_cnpollc(void *, int);
102 
103 const struct wskbd_consops nextkbd_consops = {
104 	nextkbd_cngetc,
105 	nextkbd_cnpollc,
106 };
107 
108 const struct wskbd_mapdata nextkbd_keymapdata = {
109 	nextkbd_keydesctab,
110 	KB_US,
111 };
112 
113 static int nextkbd_read_data(struct nextkbd_internal *);
114 static int nextkbd_decode(struct nextkbd_internal *, int, u_int *, int *);
115 
116 static struct nextkbd_internal nextkbd_consdata;
117 static int nextkbd_is_console(bus_space_tag_t);
118 
119 int nextkbdhard(void *);
120 
121 static int
nextkbd_is_console(bus_space_tag_t bst)122 nextkbd_is_console(bus_space_tag_t bst)
123 {
124 
125 	return (nextkbd_consdata.isconsole && (bst == nextkbd_consdata.iot));
126 }
127 
128 int
nextkbd_match(device_t parent,cfdata_t match,void * aux)129 nextkbd_match(device_t parent, cfdata_t match, void *aux)
130 {
131 	struct intio_attach_args *ia = (struct intio_attach_args *)aux;
132 
133 	if (attached)
134 		return 0;
135 
136 	ia->ia_addr = (void *)NEXT_P_MON;
137 
138 	return 1;
139 }
140 
141 void
nextkbd_attach(device_t parent,device_t self,void * aux)142 nextkbd_attach(device_t parent, device_t self, void *aux)
143 {
144 	struct nextkbd_softc *sc = device_private(self);
145 	struct intio_attach_args *ia = aux;
146 	int isconsole;
147 	struct wskbddev_attach_args a;
148 
149 	printf("\n");
150 
151 	isconsole = nextkbd_is_console(ia->ia_bst); /* XXX */
152 
153 	if (isconsole) {
154 		sc->id = &nextkbd_consdata;
155 	} else {
156 		sc->id = kmem_zalloc(sizeof(struct nextkbd_internal), KM_SLEEP);
157 
158 		sc->id->iot = ia->ia_bst;
159 		if (bus_space_map(sc->id->iot, NEXT_P_MON,
160 		    sizeof(struct mon_regs), 0, &sc->id->ioh)) {
161 			printf("%s: can't map mon status control register\n",
162 			    device_xname(self));
163 			return;
164 		}
165 	}
166 
167 	sc->id->t_sc = sc; /* set back pointer */
168 
169 	isrlink_autovec(nextkbdhard, sc, NEXT_I_IPL(NEXT_I_KYBD_MOUSE),
170 	    0, NULL);
171 
172 	INTR_ENABLE(NEXT_I_KYBD_MOUSE);
173 
174 	a.console = isconsole;
175 	a.keymap = &nextkbd_keymapdata;
176 	a.accessops = &nextkbd_accessops;
177 	a.accesscookie = sc;
178 
179 	/*
180 	 * Attach the wskbd, saving a handle to it.
181 	 * XXX XXX XXX
182 	 */
183 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
184 
185 	attached = 1;
186 }
187 
188 int
nextkbd_enable(void * v,int on)189 nextkbd_enable(void *v, int on)
190 {
191 
192 	/* XXX not sure if this should do anything */
193 	/* printf("nextkbd_enable %d\n", on); */
194 	return 0;
195 }
196 
197 void
nextkbd_set_leds(void * v,int leds)198 nextkbd_set_leds(void *v, int leds)
199 {
200 	struct nextkbd_softc *sc = v;
201 	uint32_t hw_leds = 0;
202 	int s;
203 
204 	sc->sc_leds &= ~ NEXT_WSKBD_LEDS;
205 	sc->sc_leds |= (leds & NEXT_WSKBD_LEDS);
206 
207 	if (sc->sc_leds & WSKBD_LED_CAPS) {
208 		hw_leds |= 0x30000;
209 	}
210 
211 	s = spltty();
212 	bus_space_write_1(sc->id->iot, sc->id->ioh, 3, 0xc5);
213 	/*
214 	 * @@@ need to add:
215 	 * if bit 7 of @ioh+0 set:
216 	 *   repeat 2
217 	 *     wait until bit 6 of @ioh+2 clears
218 	 */
219 	bus_space_write_4(sc->id->iot, sc->id->ioh, 4, hw_leds);
220 	/*
221 	 * @@@ need to add:
222 	 *   wait until bit 4 of @ioh+0 (@ioh+2 if bit 7 was set above)
223 	 *    clears
224 	 */
225 	splx(s);
226 }
227 
228 int
nextkbd_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)229 nextkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
230 {
231 	struct nextkbd_softc *sc = v;
232 
233 	switch (cmd) {
234 	case WSKBDIO_GTYPE:
235 		/* XXX */
236 		*(int *)data = WSKBD_TYPE_NEXT;
237 		return 0;
238 	case WSKBDIO_SETLEDS:
239 		nextkbd_set_leds (sc, *(int *)data);
240 		return 0;
241 	case WSKBDIO_GETLEDS:
242 		*(int *)data = sc->sc_leds & NEXT_WSKBD_LEDS;
243 		return 0;
244 	case WSKBDIO_COMPLEXBELL:
245 		return 0;
246 	}
247 	return EPASSTHROUGH;
248 }
249 
250 int
nextkbdhard(void * arg)251 nextkbdhard(void *arg)
252 {
253 	struct nextkbd_softc *sc = arg;
254 	int type, key, val;
255 
256 	if (!INTR_OCCURRED(NEXT_I_KYBD_MOUSE))
257 		return 0;
258 
259 #define CSR_INT		0x00800000
260 #define CSR_DATA	0x00400000
261 
262 #define KD_KEYMASK	0x007f
263 #define KD_DIRECTION	0x0080 /* pressed or released */
264 #define KD_CNTL		0x0100
265 #define KD_LSHIFT	0x0200
266 #define KD_RSHIFT	0x0400
267 #define KD_LCOMM	0x0800
268 #define KD_RCOMM	0x1000
269 #define KD_LALT		0x2000
270 #define KD_RALT		0x4000
271 #define KD_VALID	0x8000 /* only set for scancode keys ? */
272 #define KD_MODS		0x4f00
273 
274 	val = nextkbd_read_data(sc->id);
275 	if ((val != -1) && nextkbd_decode(sc->id, val, &type, &key)) {
276 		wskbd_input(sc->sc_wskbddev, type, key);
277 	}
278 	return 1;
279 }
280 
281 int
nextkbd_cnattach(bus_space_tag_t bst)282 nextkbd_cnattach(bus_space_tag_t bst)
283 {
284 	bus_space_handle_t bsh;
285 
286 	if (bus_space_map(bst, NEXT_P_MON, sizeof(struct mon_regs), 0, &bsh))
287 		return ENXIO;
288 
289 	memset(&nextkbd_consdata, 0, sizeof(nextkbd_consdata));
290 
291 	nextkbd_consdata.iot = bst;
292 	nextkbd_consdata.ioh = bsh;
293 	nextkbd_consdata.isconsole = 1;
294 
295 	wskbd_cnattach(&nextkbd_consops, &nextkbd_consdata,
296 	    &nextkbd_keymapdata);
297 
298 	return 0;
299 }
300 
301 void
nextkbd_cngetc(void * v,u_int * type,int * data)302 nextkbd_cngetc(void *v, u_int *type, int *data)
303 {
304 	struct nextkbd_internal *t = v;
305 	int val;
306 
307 	for (;;) {
308 		if (INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) {
309 			val = nextkbd_read_data(t);
310 			if (val != -1 && nextkbd_decode(t, val, type, data))
311 				return;
312 		}
313 	}
314 }
315 
316 void
nextkbd_cnpollc(void * v,int on)317 nextkbd_cnpollc(void *v, int on)
318 {
319 	struct nextkbd_internal *t = v;
320 
321 	t->polling = on;
322 	if (on) {
323 		INTR_DISABLE(NEXT_I_KYBD_MOUSE);
324 	} else {
325 		INTR_ENABLE(NEXT_I_KYBD_MOUSE);
326 	}
327 
328 }
329 
330 static int
nextkbd_read_data(struct nextkbd_internal * id)331 nextkbd_read_data(struct nextkbd_internal *id)
332 {
333 	unsigned char device;
334 	struct mon_regs stat = { 0 };
335 
336 	bus_space_read_region_4(id->iot, id->ioh, 0, &stat, 3);
337 	if ((stat.mon_csr & CSR_INT) != 0 &&
338 	    (stat.mon_csr & CSR_DATA) != 0) {
339 		stat.mon_csr &= ~CSR_INT;
340 		id->num_ints++;
341 		bus_space_write_4(id->iot, id->ioh, 0, stat.mon_csr);
342 		device = stat.mon_data >> 28;
343 		if (device != 1)
344 			return -1; /* XXX: mouse */
345 		return stat.mon_data & 0xffff;
346 	}
347 	return -1;
348 }
349 
350 static int
nextkbd_decode(struct nextkbd_internal * id,int datain,u_int * type,int * dataout)351 nextkbd_decode(struct nextkbd_internal *id, int datain, u_int *type,
352     int *dataout)
353 {
354 
355 #if 0
356 	printf("datain %08x mods %08x\n", datain, id->mods);
357 #endif
358 
359 	if (((datain ^ id->mods) & KD_LSHIFT) != 0) {
360 		id->mods ^= KD_LSHIFT;
361 		*dataout = 90;
362 		if (datain & KD_LSHIFT)
363 			*type = WSCONS_EVENT_KEY_DOWN;
364 		else
365 			*type = WSCONS_EVENT_KEY_UP;
366 	} else if (((datain ^ id->mods) & KD_RSHIFT) != 0) {
367 		id->mods ^= KD_RSHIFT;
368 		*dataout = 91;
369 		if ((datain & KD_RSHIFT) != 0)
370 			*type = WSCONS_EVENT_KEY_DOWN;
371 		else
372 			*type = WSCONS_EVENT_KEY_UP;
373 	} else if (((datain ^ id->mods) & KD_LALT) != 0) {
374 		id->mods ^= KD_LALT;
375 		*dataout = 92;
376 		if ((datain & KD_LALT) != 0)
377 			*type = WSCONS_EVENT_KEY_DOWN;
378 		else
379 			*type = WSCONS_EVENT_KEY_UP;
380 	} else if (((datain ^ id->mods) & KD_RALT) != 0) {
381 		id->mods ^= KD_RALT;
382 		*dataout = 93;
383 		if ((datain & KD_RALT) != 0)
384 			*type = WSCONS_EVENT_KEY_DOWN;
385 		else
386 			*type = WSCONS_EVENT_KEY_UP;
387 	} else if (((datain ^ id->mods) & KD_CNTL) != 0) {
388 		id->mods ^= KD_CNTL;
389 		*dataout = 94;
390 		if ((datain & KD_CNTL) != 0)
391 			*type = WSCONS_EVENT_KEY_DOWN;
392 		else
393 			*type = WSCONS_EVENT_KEY_UP;
394 	} else if (((datain ^ id->mods) & KD_LCOMM) != 0) {
395 		id->mods ^= KD_LCOMM;
396 		*dataout = 95;
397 		if ((datain & KD_LCOMM) != 0)
398 			*type = WSCONS_EVENT_KEY_DOWN;
399 		else
400 			*type = WSCONS_EVENT_KEY_UP;
401 	} else if (((datain ^ id->mods) & KD_RCOMM) != 0) {
402 		id->mods ^= KD_RCOMM;
403 		*dataout = 96;
404 		if ((datain & KD_RCOMM) != 0)
405 			*type = WSCONS_EVENT_KEY_DOWN;
406 		else
407 			*type = WSCONS_EVENT_KEY_UP;
408 	} else if ((datain & KD_KEYMASK) != 0) {
409 		if ((datain & KD_DIRECTION) != 0)
410 			*type = WSCONS_EVENT_KEY_UP;
411 		else
412 			*type = WSCONS_EVENT_KEY_DOWN;
413 
414 		*dataout = (datain & KD_KEYMASK);
415 	} else {
416 		*dataout = 0;
417 	}
418 
419 	return 1;
420 }
421