xref: /netbsd-src/sys/arch/atari/dev/kbd.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: kbd.c,v 1.18 1999/08/06 08:27:31 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman
5  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/ioctl.h>
41 #include <sys/tty.h>
42 #include <sys/proc.h>
43 #include <sys/conf.h>
44 #include <sys/file.h>
45 #include <sys/kernel.h>
46 #include <sys/signalvar.h>
47 #include <sys/syslog.h>
48 #include <dev/cons.h>
49 #include <machine/cpu.h>
50 #include <machine/iomap.h>
51 #include <machine/mfp.h>
52 #include <machine/acia.h>
53 #include <atari/dev/itevar.h>
54 #include <atari/dev/event_var.h>
55 #include <atari/dev/vuid_event.h>
56 #include <atari/dev/ym2149reg.h>
57 #include <atari/dev/kbdreg.h>
58 #include <atari/dev/kbdvar.h>
59 #include <atari/dev/kbdmap.h>
60 #include <atari/dev/msvar.h>
61 
62 #include "mouse.h"
63 
64 u_char			kbd_modifier;	/* Modifier mask		*/
65 
66 static u_char		kbd_ring[KBD_RING_SIZE];
67 static volatile u_int	kbd_rbput = 0;	/* 'put' index			*/
68 static u_int		kbd_rbget = 0;	/* 'get' index			*/
69 static u_char		kbd_soft  = 0;	/* 1: Softint has been scheduled*/
70 
71 static struct kbd_softc kbd_softc;
72 
73 /* {b,c}devsw[] function prototypes */
74 dev_type_open(kbdopen);
75 dev_type_close(kbdclose);
76 dev_type_read(kbdread);
77 dev_type_ioctl(kbdioctl);
78 dev_type_poll(kbdpoll);
79 
80 /* Interrupt handler */
81 void	kbdintr __P((int));
82 
83 static void kbdsoft __P((void *, void *));
84 static void kbdattach __P((struct device *, struct device *, void *));
85 static int  kbdmatch __P((struct device *, struct cfdata *, void *));
86 static int  kbd_do_modifier __P((u_char));
87 static int  kbd_write_poll __P((u_char *, int));
88 static void kbd_pkg_start __P((struct kbd_softc *, u_char));
89 
90 struct cfattach kbd_ca = {
91 	sizeof(struct device), kbdmatch, kbdattach
92 };
93 
94 /*ARGSUSED*/
95 static	int
96 kbdmatch(pdp, cfp, auxp)
97 struct	device	*pdp;
98 struct	cfdata	*cfp;
99 void		*auxp;
100 {
101 	if (!strcmp((char *)auxp, "kbd"))
102 		return (1);
103 	return (0);
104 }
105 
106 /*ARGSUSED*/
107 static void
108 kbdattach(pdp, dp, auxp)
109 struct	device *pdp, *dp;
110 void	*auxp;
111 {
112 	int	timeout;
113 	u_char	kbd_rst[]  = { 0x80, 0x01 };
114 	u_char	kbd_icmd[] = { 0x12, 0x15 };
115 
116 	/*
117 	 * Disable keyboard interrupts from MFP
118 	 */
119 	MFP->mf_ierb &= ~IB_AINT;
120 
121 	/*
122 	 * Reset ACIA and intialize to:
123 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
124 	 */
125 	KBD->ac_cs = A_RESET;
126 	delay(100);	/* XXX: enough? */
127 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
128 
129 	/*
130 	 * Clear error conditions
131 	 */
132 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
133 		timeout = KBD->ac_da;
134 
135 	/*
136 	 * Now send the reset string, and read+ignore it's response
137 	 */
138 	if (!kbd_write_poll(kbd_rst, 2))
139 		printf("kbd: error cannot reset keyboard\n");
140 	for (timeout = 1000; timeout > 0; timeout--) {
141 		if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
142 			timeout = KBD->ac_da;
143 			timeout = 100;
144 		}
145 		delay(100);
146 	}
147 	/*
148 	 * Send init command: disable mice & joysticks
149 	 */
150 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
151 
152 	printf("\n");
153 }
154 
155 void
156 kbdenable()
157 {
158 	int	s, code;
159 
160 	s = spltty();
161 
162 	/*
163 	 * Clear error conditions...
164 	 */
165 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
166 		code = KBD->ac_da;
167 	/*
168 	 * Enable interrupts from MFP
169 	 */
170 	MFP->mf_iprb  = (u_int8_t)~IB_AINT;
171 	MFP->mf_ierb |= IB_AINT;
172 	MFP->mf_imrb |= IB_AINT;
173 
174 	kbd_softc.k_event_mode   = 0;
175 	kbd_softc.k_events.ev_io = 0;
176 	kbd_softc.k_pkg_size     = 0;
177 	splx(s);
178 }
179 
180 int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
181 {
182 	if (kbd_softc.k_events.ev_io)
183 		return EBUSY;
184 
185 	kbd_softc.k_events.ev_io = p;
186 	ev_init(&kbd_softc.k_events);
187 	return (0);
188 }
189 
190 int
191 kbdclose(dev_t dev, int flags, int mode, struct proc *p)
192 {
193 	/* Turn off event mode, dump the queue */
194 	kbd_softc.k_event_mode = 0;
195 	ev_fini(&kbd_softc.k_events);
196 	kbd_softc.k_events.ev_io = NULL;
197 	return (0);
198 }
199 
200 int
201 kbdread(dev_t dev, struct uio *uio, int flags)
202 {
203 	return ev_read(&kbd_softc.k_events, uio, flags);
204 }
205 
206 int
207 kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
208 {
209 	register struct kbd_softc *k = &kbd_softc;
210 	struct kbdbell	*kb;
211 
212 	switch (cmd) {
213 		case KIOCTRANS:
214 			if (*(int *)data == TR_UNTRANS_EVENT)
215 				return 0;
216 			break;
217 
218 		case KIOCGTRANS:
219 			/*
220 			 * Get translation mode
221 			 */
222 			*(int *)data = TR_UNTRANS_EVENT;
223 			return 0;
224 
225 		case KIOCSDIRECT:
226 			k->k_event_mode = *(int *)data;
227 			return 0;
228 
229 		case KIOCRINGBELL:
230 			kb = (struct kbdbell *)data;
231 			if (kb)
232 				kbd_bell_sparms(kb->volume, kb->pitch,
233 							kb->duration);
234 			kbdbell();
235 			return 0;
236 
237 		case FIONBIO:	/* we will remove this someday (soon???) */
238 			return 0;
239 
240 		case FIOASYNC:
241 			k->k_events.ev_async = *(int *)data != 0;
242 				return 0;
243 
244 		case TIOCSPGRP:
245 			if (*(int *)data != k->k_events.ev_io->p_pgid)
246 				return EPERM;
247 			return 0;
248 
249 		default:
250 			return ENOTTY;
251 	}
252 
253 	/*
254 	 * We identified the ioctl, but we do not handle it.
255 	 */
256 	return EOPNOTSUPP;		/* misuse, but what the heck */
257 }
258 
259 int
260 kbdpoll (dev_t dev, int events, struct proc *p)
261 {
262   return ev_poll (&kbd_softc.k_events, events, p);
263 }
264 
265 /*
266  * Keyboard interrupt handler called straight from MFP at spl6.
267  */
268 void
269 kbdintr(sr)
270 int sr;	/* sr at time of interrupt	*/
271 {
272 	int	code;
273 	int	got_char = 0;
274 
275 	/*
276 	 * There may be multiple keys available. Read them all.
277 	 */
278 	while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
279 		got_char = 1;
280 		if (KBD->ac_cs & (A_OE|A_PE)) {
281 			code = KBD->ac_da;	/* Silently ignore errors */
282 			continue;
283 		}
284 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
285 	}
286 
287 	/*
288 	 * If characters are waiting for transmit, send them.
289 	 */
290 	if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
291 		if (kbd_softc.k_sendp != NULL)
292 			KBD->ac_da = *kbd_softc.k_sendp++;
293 		if (--kbd_softc.k_send_cnt <= 0) {
294 			/*
295 			 * The total package has been transmitted,
296 			 * wakeup anyone waiting for it.
297 			 */
298 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
299 			kbd_softc.k_sendp    = NULL;
300 			kbd_softc.k_send_cnt = 0;
301 			wakeup((caddr_t)&kbd_softc.k_send_cnt);
302 		}
303 	}
304 
305 	/*
306 	 * Activate software-level to handle possible input.
307 	 */
308 	if (got_char) {
309 		if (!BASEPRI(sr)) {
310 			if (!kbd_soft++)
311 				add_sicallback(kbdsoft, 0, 0);
312 		} else {
313 			spl1();
314 			kbdsoft(NULL, NULL);
315 		}
316 	}
317 }
318 
319 /*
320  * Keyboard soft interrupt handler
321  */
322 void
323 kbdsoft(junk1, junk2)
324 void	*junk1, *junk2;
325 {
326 	int			s;
327 	u_char			code;
328 	struct kbd_softc	*k = &kbd_softc;
329 	struct firm_event	*fe;
330 	int			put;
331 	int			n, get;
332 
333 	kbd_soft = 0;
334 	get      = kbd_rbget;
335 
336 	for (;;) {
337 		n = kbd_rbput;
338 		if (get == n) /* We're done	*/
339 			break;
340 		n -= get;
341 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
342 			get += n - KBD_RING_SIZE;
343 			n    = KBD_RING_SIZE;
344 		}
345 		while (--n >= 0) {
346 			code = kbd_ring[get++ & KBD_RING_MASK];
347 
348 			/*
349 			 * If collecting a package, stuff it in and
350 			 * continue.
351 			 */
352 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
353 			    k->k_package[k->k_pkg_idx++] = code;
354 			    if (k->k_pkg_idx == k->k_pkg_size) {
355 				/*
356 				 * Package is complete.
357 				 */
358 				switch(k->k_pkg_type) {
359 #if NMOUSE > 0
360 				    case KBD_AMS_PKG:
361 				    case KBD_RMS_PKG:
362 				    case KBD_JOY1_PKG:
363 			 		mouse_soft((REL_MOUSE *)k->k_package,
364 						k->k_pkg_size, k->k_pkg_type);
365 #endif /* NMOUSE */
366 				}
367 				k->k_pkg_size = 0;
368 			    }
369 			    continue;
370 			}
371 			/*
372 			 * If this is a package header, init pkg. handling.
373 			 */
374 			if (!KBD_IS_KEY(code)) {
375 				kbd_pkg_start(k, code);
376 				continue;
377 			}
378 			if (kbd_do_modifier(code) && !k->k_event_mode)
379 				continue;
380 
381 			/*
382 			 * if not in event mode, deliver straight to ite to
383 			 * process key stroke
384 			 */
385 			if (!k->k_event_mode) {
386 				/* Gets to spltty() by itself	*/
387 				ite_filter(code, ITEFILT_TTY);
388 				continue;
389 			}
390 
391 			/*
392 			 * Keyboard is generating events.  Turn this keystroke
393 			 * into an event and put it in the queue.  If the queue
394 			 * is full, the keystroke is lost (sorry!).
395 			 */
396 			s = spltty();
397 			put = k->k_events.ev_put;
398 			fe  = &k->k_events.ev_q[put];
399 			put = (put + 1) % EV_QSIZE;
400 			if (put == k->k_events.ev_get) {
401 				log(LOG_WARNING,
402 					"keyboard event queue overflow\n");
403 				splx(s);
404 				continue;
405 			}
406 			fe->id    = KBD_SCANCODE(code);
407 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
408 			fe->time  = time;
409 			k->k_events.ev_put = put;
410 			EV_WAKEUP(&k->k_events);
411 			splx(s);
412 		}
413 		kbd_rbget = get;
414 	}
415 }
416 
417 static	u_char sound[] = {
418 	0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
419 	0xF8,0x10,0x10,0x10,0x00,0x20,0x03
420 };
421 
422 void
423 kbdbell()
424 {
425 	register int	i, sps;
426 
427 	sps = splhigh();
428 	for (i = 0; i < sizeof(sound); i++) {
429 		YM2149->sd_selr = i;
430 		YM2149->sd_wdat = sound[i];
431 	}
432 	splx(sps);
433 }
434 
435 
436 /*
437  * Set the parameters of the 'default' beep.
438  */
439 
440 #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
441 #define KBDBELLDURATION	128	/* 256 / 2MHz */
442 
443 void
444 kbd_bell_gparms(volume, pitch, duration)
445 	u_int	*volume, *pitch, *duration;
446 {
447 	u_int	tmp;
448 
449 	tmp = sound[11] | (sound[12] << 8);
450 	*duration = (tmp * KBDBELLDURATION) / 1000;
451 
452 	tmp = sound[0] | (sound[1] << 8);
453 	*pitch = KBDBELLCLOCK / tmp;
454 
455 	*volume = 0;
456 }
457 
458 void
459 kbd_bell_sparms(volume, pitch, duration)
460 	u_int	volume, pitch, duration;
461 {
462 	u_int	f, t;
463 
464 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
465 	if (f > 20000)
466 		f = 20000;		/* maximum pitch */
467 
468 	f = KBDBELLCLOCK / f;
469 
470 	t = (duration * 1000) / KBDBELLDURATION;
471 
472 	sound[ 0] = f & 0xff;
473 	sound[ 1] = (f >> 8) & 0xf;
474 	f -= 1;
475 	sound[ 2] = f & 0xff;
476 	sound[ 3] = (f >> 8) & 0xf;
477 	f += 2;
478 	sound[ 4] = f & 0xff;
479 	sound[ 5] = (f >> 8) & 0xf;
480 
481 	sound[11] = t & 0xff;
482 	sound[12] = (t >> 8) & 0xff;
483 
484 	sound[13] = 0x03;
485 }
486 
487 int
488 kbdgetcn()
489 {
490 	u_char	code;
491 	int	s = spltty();
492 	int	ints_active;
493 
494 	ints_active = 0;
495 	if (MFP->mf_imrb & IB_AINT) {
496 		ints_active   = 1;
497 		MFP->mf_imrb &= ~IB_AINT;
498 	}
499 	for (;;) {
500 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
501 			;	/* Wait for key	*/
502 		if (KBD->ac_cs & (A_OE|A_PE)) {
503 			code = KBD->ac_da;	/* Silently ignore errors */
504 			continue;
505 		}
506 		code = KBD->ac_da;
507 		if (!kbd_do_modifier(code))
508 			break;
509 	}
510 
511 	if (ints_active) {
512 		MFP->mf_iprb  = (u_int8_t)~IB_AINT;
513 		MFP->mf_imrb |=  IB_AINT;
514 	}
515 
516 	splx (s);
517 	return code;
518 }
519 
520 /*
521  * Write a command to the keyboard in 'polled' mode.
522  */
523 static int
524 kbd_write_poll(cmd, len)
525 u_char	*cmd;
526 int	len;
527 {
528 	int	timeout;
529 
530 	while (len-- > 0) {
531 		KBD->ac_da = *cmd++;
532 		for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
533 			delay(10);
534 		if (!(KBD->ac_cs & A_TXRDY))
535 			return (0);
536 	}
537 	return (1);
538 }
539 
540 /*
541  * Write a command to the keyboard. Return when command is send.
542  */
543 void
544 kbd_write(cmd, len)
545 u_char	*cmd;
546 int	len;
547 {
548 	struct kbd_softc	*k = &kbd_softc;
549 	int			sps;
550 
551 	/*
552 	 * Get to splhigh, 'real' interrupts arrive at spl6!
553 	 */
554 	sps = splhigh();
555 
556 	/*
557 	 * Make sure any privious write has ended...
558 	 */
559 	while (k->k_sendp != NULL)
560 		tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
561 
562 	/*
563 	 * If the KBD-acia is not currently busy, send the first
564 	 * character now.
565 	 */
566 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
567 	if (KBD->ac_cs & A_TXRDY) {
568 		KBD->ac_da = *cmd++;
569 		len--;
570 	}
571 
572 	/*
573 	 * If we're not yet done, wait until all characters are send.
574 	 */
575 	if (len > 0) {
576 		k->k_sendp    = cmd;
577 		k->k_send_cnt = len;
578 		tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
579 	}
580 	splx(sps);
581 
582 	/*
583 	 * Wakeup all procs waiting for us.
584 	 */
585 	wakeup((caddr_t)&k->k_sendp);
586 }
587 
588 /*
589  * Setup softc-fields to assemble a keyboard package.
590  */
591 static void
592 kbd_pkg_start(kp, msg_start)
593 struct kbd_softc *kp;
594 u_char		 msg_start;
595 {
596 	kp->k_pkg_idx    = 1;
597 	kp->k_package[0] = msg_start;
598 	switch (msg_start) {
599 		case 0xf6:
600 			kp->k_pkg_type = KBD_MEM_PKG;
601 			kp->k_pkg_size = 8;
602 			break;
603 		case 0xf7:
604 			kp->k_pkg_type = KBD_AMS_PKG;
605 			kp->k_pkg_size = 6;
606 			break;
607 		case 0xf8:
608 		case 0xf9:
609 		case 0xfa:
610 		case 0xfb:
611 			kp->k_pkg_type = KBD_RMS_PKG;
612 			kp->k_pkg_size = 3;
613 			break;
614 		case 0xfc:
615 			kp->k_pkg_type = KBD_CLK_PKG;
616 			kp->k_pkg_size = 7;
617 			break;
618 		case 0xfe:
619 			kp->k_pkg_type = KBD_JOY0_PKG;
620 			kp->k_pkg_size = 2;
621 			break;
622 		case 0xff:
623 			kp->k_pkg_type = KBD_JOY1_PKG;
624 			kp->k_pkg_size = 2;
625 			break;
626 		default:
627 			printf("kbd: Unknown packet 0x%x\n", msg_start);
628 			break;
629 	}
630 }
631 
632 /*
633  * Modifier processing
634  */
635 static int
636 kbd_do_modifier(code)
637 u_char	code;
638 {
639 	u_char	up, mask;
640 
641 	up   = KBD_RELEASED(code);
642 	mask = 0;
643 
644 	switch(KBD_SCANCODE(code)) {
645 		case KBD_LEFT_SHIFT:
646 			mask = KBD_MOD_LSHIFT;
647 			break;
648 		case KBD_RIGHT_SHIFT:
649 			mask = KBD_MOD_RSHIFT;
650 			break;
651 		case KBD_CTRL:
652 			mask = KBD_MOD_CTRL;
653 			break;
654 		case KBD_ALT:
655 			mask = KBD_MOD_ALT;
656 			break;
657 		case KBD_CAPS_LOCK:
658 			/* CAPSLOCK is a toggle */
659 			if(!up)
660 				kbd_modifier ^= KBD_MOD_CAPS;
661 			return 1;
662 	}
663 	if(mask) {
664 		if(up)
665 			kbd_modifier &= ~mask;
666 		else
667 			kbd_modifier |= mask;
668 		return 1;
669 	}
670 	return 0;
671 }
672