Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.18.16.1
      1 /*	$NetBSD: kbd.c,v 1.18.16.1 2001/09/09 19:12:35 thorpej 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 cdev_decl(kbd);
     75 
     76 /* Interrupt handler */
     77 void	kbdintr __P((int));
     78 
     79 static void kbdsoft __P((void *, void *));
     80 static void kbdattach __P((struct device *, struct device *, void *));
     81 static int  kbdmatch __P((struct device *, struct cfdata *, void *));
     82 static int  kbd_do_modifier __P((u_char));
     83 static int  kbd_write_poll __P((u_char *, int));
     84 static void kbd_pkg_start __P((struct kbd_softc *, u_char));
     85 
     86 struct cfattach kbd_ca = {
     87 	sizeof(struct device), kbdmatch, kbdattach
     88 };
     89 
     90 /*ARGSUSED*/
     91 static	int
     92 kbdmatch(pdp, cfp, auxp)
     93 struct	device	*pdp;
     94 struct	cfdata	*cfp;
     95 void		*auxp;
     96 {
     97 	if (!strcmp((char *)auxp, "kbd"))
     98 		return (1);
     99 	return (0);
    100 }
    101 
    102 /*ARGSUSED*/
    103 static void
    104 kbdattach(pdp, dp, auxp)
    105 struct	device *pdp, *dp;
    106 void	*auxp;
    107 {
    108 	int	timeout;
    109 	u_char	kbd_rst[]  = { 0x80, 0x01 };
    110 	u_char	kbd_icmd[] = { 0x12, 0x15 };
    111 
    112 	/*
    113 	 * Disable keyboard interrupts from MFP
    114 	 */
    115 	MFP->mf_ierb &= ~IB_AINT;
    116 
    117 	/*
    118 	 * Reset ACIA and intialize to:
    119 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
    120 	 */
    121 	KBD->ac_cs = A_RESET;
    122 	delay(100);	/* XXX: enough? */
    123 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
    124 
    125 	/*
    126 	 * Clear error conditions
    127 	 */
    128 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
    129 		timeout = KBD->ac_da;
    130 
    131 	/*
    132 	 * Now send the reset string, and read+ignore it's response
    133 	 */
    134 	if (!kbd_write_poll(kbd_rst, 2))
    135 		printf("kbd: error cannot reset keyboard\n");
    136 	for (timeout = 1000; timeout > 0; timeout--) {
    137 		if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
    138 			timeout = KBD->ac_da;
    139 			timeout = 100;
    140 		}
    141 		delay(100);
    142 	}
    143 	/*
    144 	 * Send init command: disable mice & joysticks
    145 	 */
    146 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
    147 
    148 	printf("\n");
    149 }
    150 
    151 void
    152 kbdenable()
    153 {
    154 	int	s, code;
    155 
    156 	s = spltty();
    157 
    158 	/*
    159 	 * Clear error conditions...
    160 	 */
    161 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
    162 		code = KBD->ac_da;
    163 	/*
    164 	 * Enable interrupts from MFP
    165 	 */
    166 	MFP->mf_iprb  = (u_int8_t)~IB_AINT;
    167 	MFP->mf_ierb |= IB_AINT;
    168 	MFP->mf_imrb |= IB_AINT;
    169 
    170 	kbd_softc.k_event_mode   = 0;
    171 	kbd_softc.k_events.ev_io = 0;
    172 	kbd_softc.k_pkg_size     = 0;
    173 	splx(s);
    174 }
    175 
    176 int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
    177 {
    178 	if (kbd_softc.k_events.ev_io)
    179 		return EBUSY;
    180 
    181 	kbd_softc.k_events.ev_io = p;
    182 	ev_init(&kbd_softc.k_events);
    183 	return (0);
    184 }
    185 
    186 int
    187 kbdclose(dev_t dev, int flags, int mode, struct proc *p)
    188 {
    189 	/* Turn off event mode, dump the queue */
    190 	kbd_softc.k_event_mode = 0;
    191 	ev_fini(&kbd_softc.k_events);
    192 	kbd_softc.k_events.ev_io = NULL;
    193 	return (0);
    194 }
    195 
    196 int
    197 kbdread(dev_t dev, struct uio *uio, int flags)
    198 {
    199 	return ev_read(&kbd_softc.k_events, uio, flags);
    200 }
    201 
    202 int
    203 kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
    204 {
    205 	register struct kbd_softc *k = &kbd_softc;
    206 	struct kbdbell	*kb;
    207 
    208 	switch (cmd) {
    209 		case KIOCTRANS:
    210 			if (*(int *)data == TR_UNTRANS_EVENT)
    211 				return 0;
    212 			break;
    213 
    214 		case KIOCGTRANS:
    215 			/*
    216 			 * Get translation mode
    217 			 */
    218 			*(int *)data = TR_UNTRANS_EVENT;
    219 			return 0;
    220 
    221 		case KIOCSDIRECT:
    222 			k->k_event_mode = *(int *)data;
    223 			return 0;
    224 
    225 		case KIOCRINGBELL:
    226 			kb = (struct kbdbell *)data;
    227 			if (kb)
    228 				kbd_bell_sparms(kb->volume, kb->pitch,
    229 							kb->duration);
    230 			kbdbell();
    231 			return 0;
    232 
    233 		case FIONBIO:	/* we will remove this someday (soon???) */
    234 			return 0;
    235 
    236 		case FIOASYNC:
    237 			k->k_events.ev_async = *(int *)data != 0;
    238 				return 0;
    239 
    240 		case TIOCSPGRP:
    241 			if (*(int *)data != k->k_events.ev_io->p_pgid)
    242 				return EPERM;
    243 			return 0;
    244 
    245 		default:
    246 			return ENOTTY;
    247 	}
    248 
    249 	/*
    250 	 * We identified the ioctl, but we do not handle it.
    251 	 */
    252 	return EOPNOTSUPP;		/* misuse, but what the heck */
    253 }
    254 
    255 int
    256 kbdpoll (dev_t dev, int events, struct proc *p)
    257 {
    258   return ev_poll (&kbd_softc.k_events, events, p);
    259 }
    260 
    261 int
    262 kbdkqfilter(dev_t dev, struct knote *kn)
    263 {
    264 
    265 	return (ev_kqfilter(&kbd_softc.k_events, kn));
    266 }
    267 
    268 /*
    269  * Keyboard interrupt handler called straight from MFP at spl6.
    270  */
    271 void
    272 kbdintr(sr)
    273 int sr;	/* sr at time of interrupt	*/
    274 {
    275 	int	code;
    276 	int	got_char = 0;
    277 
    278 	/*
    279 	 * There may be multiple keys available. Read them all.
    280 	 */
    281 	while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
    282 		got_char = 1;
    283 		if (KBD->ac_cs & (A_OE|A_PE)) {
    284 			code = KBD->ac_da;	/* Silently ignore errors */
    285 			continue;
    286 		}
    287 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
    288 	}
    289 
    290 	/*
    291 	 * If characters are waiting for transmit, send them.
    292 	 */
    293 	if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
    294 		if (kbd_softc.k_sendp != NULL)
    295 			KBD->ac_da = *kbd_softc.k_sendp++;
    296 		if (--kbd_softc.k_send_cnt <= 0) {
    297 			/*
    298 			 * The total package has been transmitted,
    299 			 * wakeup anyone waiting for it.
    300 			 */
    301 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
    302 			kbd_softc.k_sendp    = NULL;
    303 			kbd_softc.k_send_cnt = 0;
    304 			wakeup((caddr_t)&kbd_softc.k_send_cnt);
    305 		}
    306 	}
    307 
    308 	/*
    309 	 * Activate software-level to handle possible input.
    310 	 */
    311 	if (got_char) {
    312 		if (!BASEPRI(sr)) {
    313 			if (!kbd_soft++)
    314 				add_sicallback(kbdsoft, 0, 0);
    315 		} else {
    316 			spl1();
    317 			kbdsoft(NULL, NULL);
    318 		}
    319 	}
    320 }
    321 
    322 /*
    323  * Keyboard soft interrupt handler
    324  */
    325 void
    326 kbdsoft(junk1, junk2)
    327 void	*junk1, *junk2;
    328 {
    329 	int			s;
    330 	u_char			code;
    331 	struct kbd_softc	*k = &kbd_softc;
    332 	struct firm_event	*fe;
    333 	int			put;
    334 	int			n, get;
    335 
    336 	kbd_soft = 0;
    337 	get      = kbd_rbget;
    338 
    339 	for (;;) {
    340 		n = kbd_rbput;
    341 		if (get == n) /* We're done	*/
    342 			break;
    343 		n -= get;
    344 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
    345 			get += n - KBD_RING_SIZE;
    346 			n    = KBD_RING_SIZE;
    347 		}
    348 		while (--n >= 0) {
    349 			code = kbd_ring[get++ & KBD_RING_MASK];
    350 
    351 			/*
    352 			 * If collecting a package, stuff it in and
    353 			 * continue.
    354 			 */
    355 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
    356 			    k->k_package[k->k_pkg_idx++] = code;
    357 			    if (k->k_pkg_idx == k->k_pkg_size) {
    358 				/*
    359 				 * Package is complete.
    360 				 */
    361 				switch(k->k_pkg_type) {
    362 #if NMOUSE > 0
    363 				    case KBD_AMS_PKG:
    364 				    case KBD_RMS_PKG:
    365 				    case KBD_JOY1_PKG:
    366 			 		mouse_soft((REL_MOUSE *)k->k_package,
    367 						k->k_pkg_size, k->k_pkg_type);
    368 #endif /* NMOUSE */
    369 				}
    370 				k->k_pkg_size = 0;
    371 			    }
    372 			    continue;
    373 			}
    374 			/*
    375 			 * If this is a package header, init pkg. handling.
    376 			 */
    377 			if (!KBD_IS_KEY(code)) {
    378 				kbd_pkg_start(k, code);
    379 				continue;
    380 			}
    381 			if (kbd_do_modifier(code) && !k->k_event_mode)
    382 				continue;
    383 
    384 			/*
    385 			 * if not in event mode, deliver straight to ite to
    386 			 * process key stroke
    387 			 */
    388 			if (!k->k_event_mode) {
    389 				/* Gets to spltty() by itself	*/
    390 				ite_filter(code, ITEFILT_TTY);
    391 				continue;
    392 			}
    393 
    394 			/*
    395 			 * Keyboard is generating events.  Turn this keystroke
    396 			 * into an event and put it in the queue.  If the queue
    397 			 * is full, the keystroke is lost (sorry!).
    398 			 */
    399 			s = spltty();
    400 			put = k->k_events.ev_put;
    401 			fe  = &k->k_events.ev_q[put];
    402 			put = (put + 1) % EV_QSIZE;
    403 			if (put == k->k_events.ev_get) {
    404 				log(LOG_WARNING,
    405 					"keyboard event queue overflow\n");
    406 				splx(s);
    407 				continue;
    408 			}
    409 			fe->id    = KBD_SCANCODE(code);
    410 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
    411 			fe->time  = time;
    412 			k->k_events.ev_put = put;
    413 			EV_WAKEUP(&k->k_events);
    414 			splx(s);
    415 		}
    416 		kbd_rbget = get;
    417 	}
    418 }
    419 
    420 static	u_char sound[] = {
    421 	0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
    422 	0xF8,0x10,0x10,0x10,0x00,0x20,0x03
    423 };
    424 
    425 void
    426 kbdbell()
    427 {
    428 	register int	i, sps;
    429 
    430 	sps = splhigh();
    431 	for (i = 0; i < sizeof(sound); i++) {
    432 		YM2149->sd_selr = i;
    433 		YM2149->sd_wdat = sound[i];
    434 	}
    435 	splx(sps);
    436 }
    437 
    438 
    439 /*
    440  * Set the parameters of the 'default' beep.
    441  */
    442 
    443 #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
    444 #define KBDBELLDURATION	128	/* 256 / 2MHz */
    445 
    446 void
    447 kbd_bell_gparms(volume, pitch, duration)
    448 	u_int	*volume, *pitch, *duration;
    449 {
    450 	u_int	tmp;
    451 
    452 	tmp = sound[11] | (sound[12] << 8);
    453 	*duration = (tmp * KBDBELLDURATION) / 1000;
    454 
    455 	tmp = sound[0] | (sound[1] << 8);
    456 	*pitch = KBDBELLCLOCK / tmp;
    457 
    458 	*volume = 0;
    459 }
    460 
    461 void
    462 kbd_bell_sparms(volume, pitch, duration)
    463 	u_int	volume, pitch, duration;
    464 {
    465 	u_int	f, t;
    466 
    467 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
    468 	if (f > 20000)
    469 		f = 20000;		/* maximum pitch */
    470 
    471 	f = KBDBELLCLOCK / f;
    472 
    473 	t = (duration * 1000) / KBDBELLDURATION;
    474 
    475 	sound[ 0] = f & 0xff;
    476 	sound[ 1] = (f >> 8) & 0xf;
    477 	f -= 1;
    478 	sound[ 2] = f & 0xff;
    479 	sound[ 3] = (f >> 8) & 0xf;
    480 	f += 2;
    481 	sound[ 4] = f & 0xff;
    482 	sound[ 5] = (f >> 8) & 0xf;
    483 
    484 	sound[11] = t & 0xff;
    485 	sound[12] = (t >> 8) & 0xff;
    486 
    487 	sound[13] = 0x03;
    488 }
    489 
    490 int
    491 kbdgetcn()
    492 {
    493 	u_char	code;
    494 	int	s = spltty();
    495 	int	ints_active;
    496 
    497 	ints_active = 0;
    498 	if (MFP->mf_imrb & IB_AINT) {
    499 		ints_active   = 1;
    500 		MFP->mf_imrb &= ~IB_AINT;
    501 	}
    502 	for (;;) {
    503 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
    504 			;	/* Wait for key	*/
    505 		if (KBD->ac_cs & (A_OE|A_PE)) {
    506 			code = KBD->ac_da;	/* Silently ignore errors */
    507 			continue;
    508 		}
    509 		code = KBD->ac_da;
    510 		if (!kbd_do_modifier(code))
    511 			break;
    512 	}
    513 
    514 	if (ints_active) {
    515 		MFP->mf_iprb  = (u_int8_t)~IB_AINT;
    516 		MFP->mf_imrb |=  IB_AINT;
    517 	}
    518 
    519 	splx (s);
    520 	return code;
    521 }
    522 
    523 /*
    524  * Write a command to the keyboard in 'polled' mode.
    525  */
    526 static int
    527 kbd_write_poll(cmd, len)
    528 u_char	*cmd;
    529 int	len;
    530 {
    531 	int	timeout;
    532 
    533 	while (len-- > 0) {
    534 		KBD->ac_da = *cmd++;
    535 		for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
    536 			delay(10);
    537 		if (!(KBD->ac_cs & A_TXRDY))
    538 			return (0);
    539 	}
    540 	return (1);
    541 }
    542 
    543 /*
    544  * Write a command to the keyboard. Return when command is send.
    545  */
    546 void
    547 kbd_write(cmd, len)
    548 u_char	*cmd;
    549 int	len;
    550 {
    551 	struct kbd_softc	*k = &kbd_softc;
    552 	int			sps;
    553 
    554 	/*
    555 	 * Get to splhigh, 'real' interrupts arrive at spl6!
    556 	 */
    557 	sps = splhigh();
    558 
    559 	/*
    560 	 * Make sure any privious write has ended...
    561 	 */
    562 	while (k->k_sendp != NULL)
    563 		tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
    564 
    565 	/*
    566 	 * If the KBD-acia is not currently busy, send the first
    567 	 * character now.
    568 	 */
    569 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
    570 	if (KBD->ac_cs & A_TXRDY) {
    571 		KBD->ac_da = *cmd++;
    572 		len--;
    573 	}
    574 
    575 	/*
    576 	 * If we're not yet done, wait until all characters are send.
    577 	 */
    578 	if (len > 0) {
    579 		k->k_sendp    = cmd;
    580 		k->k_send_cnt = len;
    581 		tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
    582 	}
    583 	splx(sps);
    584 
    585 	/*
    586 	 * Wakeup all procs waiting for us.
    587 	 */
    588 	wakeup((caddr_t)&k->k_sendp);
    589 }
    590 
    591 /*
    592  * Setup softc-fields to assemble a keyboard package.
    593  */
    594 static void
    595 kbd_pkg_start(kp, msg_start)
    596 struct kbd_softc *kp;
    597 u_char		 msg_start;
    598 {
    599 	kp->k_pkg_idx    = 1;
    600 	kp->k_package[0] = msg_start;
    601 	switch (msg_start) {
    602 		case 0xf6:
    603 			kp->k_pkg_type = KBD_MEM_PKG;
    604 			kp->k_pkg_size = 8;
    605 			break;
    606 		case 0xf7:
    607 			kp->k_pkg_type = KBD_AMS_PKG;
    608 			kp->k_pkg_size = 6;
    609 			break;
    610 		case 0xf8:
    611 		case 0xf9:
    612 		case 0xfa:
    613 		case 0xfb:
    614 			kp->k_pkg_type = KBD_RMS_PKG;
    615 			kp->k_pkg_size = 3;
    616 			break;
    617 		case 0xfc:
    618 			kp->k_pkg_type = KBD_CLK_PKG;
    619 			kp->k_pkg_size = 7;
    620 			break;
    621 		case 0xfe:
    622 			kp->k_pkg_type = KBD_JOY0_PKG;
    623 			kp->k_pkg_size = 2;
    624 			break;
    625 		case 0xff:
    626 			kp->k_pkg_type = KBD_JOY1_PKG;
    627 			kp->k_pkg_size = 2;
    628 			break;
    629 		default:
    630 			printf("kbd: Unknown packet 0x%x\n", msg_start);
    631 			break;
    632 	}
    633 }
    634 
    635 /*
    636  * Modifier processing
    637  */
    638 static int
    639 kbd_do_modifier(code)
    640 u_char	code;
    641 {
    642 	u_char	up, mask;
    643 
    644 	up   = KBD_RELEASED(code);
    645 	mask = 0;
    646 
    647 	switch(KBD_SCANCODE(code)) {
    648 		case KBD_LEFT_SHIFT:
    649 			mask = KBD_MOD_LSHIFT;
    650 			break;
    651 		case KBD_RIGHT_SHIFT:
    652 			mask = KBD_MOD_RSHIFT;
    653 			break;
    654 		case KBD_CTRL:
    655 			mask = KBD_MOD_CTRL;
    656 			break;
    657 		case KBD_ALT:
    658 			mask = KBD_MOD_ALT;
    659 			break;
    660 		case KBD_CAPS_LOCK:
    661 			/* CAPSLOCK is a toggle */
    662 			if(!up)
    663 				kbd_modifier ^= KBD_MOD_CAPS;
    664 			return 1;
    665 	}
    666 	if(mask) {
    667 		if(up)
    668 			kbd_modifier &= ~mask;
    669 		else
    670 			kbd_modifier |= mask;
    671 		return 1;
    672 	}
    673 	return 0;
    674 }
    675