Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.8
      1 /*	$NetBSD: kbd.c,v 1.8 1996/03/27 12:15:28 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/syslog.h>
     47 #include <dev/cons.h>
     48 #include <machine/cpu.h>
     49 #include <machine/iomap.h>
     50 #include <machine/mfp.h>
     51 #include <machine/acia.h>
     52 #include <atari/dev/ym2149reg.h>
     53 #include <atari/dev/itevar.h>
     54 #include <atari/dev/kbdreg.h>
     55 #include <atari/dev/event_var.h>
     56 #include <atari/dev/vuid_event.h>
     57 
     58 #include "mouse.h"
     59 
     60 /*
     61  * The ringbuffer is the interface between the hard and soft interrupt handler.
     62  * The hard interrupt runs straight from the MFP interrupt.
     63  */
     64 #define KBD_RING_SIZE	256   /* Sz of input ring buffer, must be power of 2 */
     65 #define KBD_RING_MASK	255   /* Modulo mask for above			     */
     66 
     67 static u_char		kbd_ring[KBD_RING_SIZE];
     68 static volatile u_int	kbd_rbput = 0;	/* 'put' index			*/
     69 static u_int		kbd_rbget = 0;	/* 'get' index			*/
     70 static u_char		kbd_soft  = 0;	/* 1: Softint has been scheduled*/
     71 
     72 struct kbd_softc {
     73 	int		k_event_mode;	/* if 1, collect events,	*/
     74 					/*   else pass to ite		*/
     75 	struct evvar	k_events;	/* event queue state		*/
     76 	u_char		k_soft_cs;	/* control-reg. copy		*/
     77 	u_char		k_package[20];	/* XXX package being build	*/
     78 	u_char		k_pkg_size;	/* Size of the package		*/
     79 	u_char		k_pkg_idx;
     80 	u_char		*k_sendp;	/* Output pointer		*/
     81 	int		k_send_cnt;	/* Chars left for output	*/
     82 };
     83 
     84 static struct kbd_softc kbd_softc;
     85 
     86 /* {b,c}devsw[] function prototypes */
     87 dev_type_open(kbdopen);
     88 dev_type_close(kbdclose);
     89 dev_type_read(kbdread);
     90 dev_type_ioctl(kbdioctl);
     91 dev_type_select(kbdselect);
     92 
     93 /* Interrupt handler */
     94 void	kbdintr __P((int));
     95 
     96 void	kbd_write __P((u_char *, int));
     97 
     98 static void kbdsoft __P((void));
     99 static void kbdattach __P((struct device *, struct device *, void *));
    100 static int  kbdmatch __P((struct device *, void *, void *));
    101 static int  kbd_write_poll __P((u_char *, int));
    102 static void kbd_pkg_start __P((struct kbd_softc *, u_char));
    103 
    104 struct cfattach kbd_ca = {
    105 	sizeof(struct device), kbdmatch, kbdattach
    106 };
    107 
    108 struct cfdriver kbd_cd = {
    109 	NULL, "kbd", DV_DULL, NULL, 0
    110 };
    111 
    112 /*ARGSUSED*/
    113 static	int
    114 kbdmatch(pdp, match, auxp)
    115 struct	device *pdp;
    116 void	*match, *auxp;
    117 {
    118 	if (!strcmp((char *)auxp, "kbd"))
    119 		return (1);
    120 	return (0);
    121 }
    122 
    123 /*ARGSUSED*/
    124 static void
    125 kbdattach(pdp, dp, auxp)
    126 struct	device *pdp, *dp;
    127 void	*auxp;
    128 {
    129 	int	timeout;
    130 	u_char	kbd_rst[]  = { 0x80, 0x01 };
    131 	u_char	kbd_icmd[] = { 0x12, 0x15 };
    132 
    133 	/*
    134 	 * Disable keyboard interrupts from MFP
    135 	 */
    136 	MFP->mf_ierb &= ~IB_AINT;
    137 
    138 	/*
    139 	 * Reset ACIA and intialize to:
    140 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
    141 	 */
    142 	KBD->ac_cs = A_RESET;
    143 	delay(100);	/* XXX: enough? */
    144 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
    145 
    146 	/*
    147 	 * Clear error conditions
    148 	 */
    149 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
    150 		timeout = KBD->ac_da;
    151 
    152 	/*
    153 	 * Now send the reset string, and read+ignore it's response
    154 	 */
    155 	if (!kbd_write_poll(kbd_rst, 2))
    156 		printf("kbd: error cannot reset keyboard\n");
    157 	for (timeout = 1000; timeout > 0; timeout--) {
    158 		if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
    159 			timeout = KBD->ac_da;
    160 			timeout = 100;
    161 		}
    162 		delay(100);
    163 	}
    164 	/*
    165 	 * Send init command: disable mice & joysticks
    166 	 */
    167 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
    168 
    169 	printf("\n");
    170 }
    171 
    172 /* definitions for atari keyboard encoding. */
    173 #define KEY_CODE(c)	((u_char)(c) & 0x7f)
    174 #define KEY_UP(c)	((u_char)(c) & 0x80)
    175 #define	IS_KEY(c)	((u_char)(c) < 0xf6)
    176 
    177 void
    178 kbdenable()
    179 {
    180 	int	s, code;
    181 
    182 	s = spltty();
    183 
    184 	/*
    185 	 * Clear error conditions...
    186 	 */
    187 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
    188 		code = KBD->ac_da;
    189 	/*
    190 	 * Enable interrupts from MFP
    191 	 */
    192 	MFP->mf_iprb &= ~IB_AINT;
    193 	MFP->mf_ierb |= IB_AINT;
    194 	MFP->mf_imrb |= IB_AINT;
    195 
    196 	kbd_softc.k_event_mode   = 0;
    197 	kbd_softc.k_events.ev_io = 0;
    198 	kbd_softc.k_pkg_size     = 0;
    199 	splx(s);
    200 }
    201 
    202 int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
    203 {
    204 	if (kbd_softc.k_events.ev_io)
    205 		return EBUSY;
    206 
    207 	kbd_softc.k_events.ev_io = p;
    208 	ev_init(&kbd_softc.k_events);
    209 	return (0);
    210 }
    211 
    212 int
    213 kbdclose(dev_t dev, int flags, int mode, struct proc *p)
    214 {
    215 	/* Turn off event mode, dump the queue */
    216 	kbd_softc.k_event_mode = 0;
    217 	ev_fini(&kbd_softc.k_events);
    218 	kbd_softc.k_events.ev_io = NULL;
    219 	return (0);
    220 }
    221 
    222 int
    223 kbdread(dev_t dev, struct uio *uio, int flags)
    224 {
    225 	return ev_read(&kbd_softc.k_events, uio, flags);
    226 }
    227 
    228 int
    229 kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
    230 {
    231 	register struct kbd_softc *k = &kbd_softc;
    232 
    233 	switch (cmd) {
    234 		case KIOCTRANS:
    235 			if (*(int *)data == TR_UNTRANS_EVENT)
    236 				return 0;
    237 			break;
    238 
    239 		case KIOCGTRANS:
    240 			/*
    241 			 * Get translation mode
    242 			 */
    243 			*(int *)data = TR_UNTRANS_EVENT;
    244 			return 0;
    245 
    246 		case KIOCSDIRECT:
    247 			k->k_event_mode = *(int *)data;
    248 			return 0;
    249 
    250 		case FIONBIO:	/* we will remove this someday (soon???) */
    251 			return 0;
    252 
    253 		case FIOASYNC:
    254 			k->k_events.ev_async = *(int *)data != 0;
    255 				return 0;
    256 
    257 		case TIOCSPGRP:
    258 			if (*(int *)data != k->k_events.ev_io->p_pgid)
    259 				return EPERM;
    260 			return 0;
    261 
    262 		default:
    263 			return ENOTTY;
    264 	}
    265 
    266 	/*
    267 	 * We identified the ioctl, but we do not handle it.
    268 	 */
    269 	return EOPNOTSUPP;		/* misuse, but what the heck */
    270 }
    271 
    272 int
    273 kbdselect (dev_t dev, int rw, struct proc *p)
    274 {
    275   return ev_select (&kbd_softc.k_events, rw, p);
    276 }
    277 
    278 /*
    279  * Keyboard interrupt handler called straight from MFP at spl6.
    280  */
    281 void
    282 kbdintr(sr)
    283 int sr;	/* sr at time of interrupt	*/
    284 {
    285 	int	code;
    286 	int	got_char = 0;
    287 
    288 	/*
    289 	 * There may be multiple keys available. Read them all.
    290 	 */
    291 	while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
    292 		got_char = 1;
    293 		if (KBD->ac_cs & (A_OE|A_PE)) {
    294 			code = KBD->ac_da;	/* Silently ignore errors */
    295 			continue;
    296 		}
    297 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
    298 	}
    299 
    300 	/*
    301 	 * If characters are waiting for transmit, send them.
    302 	 */
    303 	if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
    304 		if (kbd_softc.k_sendp != NULL)
    305 			KBD->ac_da = *kbd_softc.k_sendp++;
    306 		if (--kbd_softc.k_send_cnt <= 0) {
    307 			/*
    308 			 * The total package has been transmitted,
    309 			 * wakeup anyone waiting for it.
    310 			 */
    311 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
    312 			kbd_softc.k_sendp    = NULL;
    313 			kbd_softc.k_send_cnt = 0;
    314 			wakeup((caddr_t)&kbd_softc.k_send_cnt);
    315 		}
    316 	}
    317 
    318 	/*
    319 	 * Activate software-level to handle possible input.
    320 	 */
    321 	if (got_char) {
    322 		if (!BASEPRI(sr)) {
    323 			if (!kbd_soft++)
    324 				add_sicallback(kbdsoft, 0, 0);
    325 		} else {
    326 			spl1();
    327 			kbdsoft();
    328 		}
    329 	}
    330 }
    331 
    332 /*
    333  * Keyboard soft interrupt handler
    334  */
    335 void
    336 kbdsoft()
    337 {
    338 	int			s;
    339 	u_char			code;
    340 	struct kbd_softc	*k = &kbd_softc;
    341 	struct firm_event	*fe;
    342 	int			put;
    343 	int			n, get;
    344 
    345 	kbd_soft = 0;
    346 	get      = kbd_rbget;
    347 
    348 	for (;;) {
    349 		n = kbd_rbput;
    350 		if (get == n) /* We're done	*/
    351 			break;
    352 		n -= get;
    353 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
    354 			get += n - KBD_RING_SIZE;
    355 			n    = KBD_RING_SIZE;
    356 		}
    357 		while (--n >= 0) {
    358 			code = kbd_ring[get++ & KBD_RING_MASK];
    359 
    360 			/*
    361 			 * If collecting a package, stuff it in and
    362 			 * continue.
    363 			 */
    364 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
    365 				k->k_package[k->k_pkg_idx++] = code;
    366 				if (k->k_pkg_idx == k->k_pkg_size) {
    367 				    k->k_pkg_size = 0;
    368 #if NMOUSE > 0
    369 				    /*
    370 				     * Package is complete, we can now
    371 				     * send it to the mouse driver...
    372 				     */
    373 				    mouse_soft(k->k_package, k->k_pkg_size);
    374 #endif /* NMOUSE */
    375 				}
    376 				continue;
    377 			}
    378 			/*
    379 			 * If this is a package header, init pkg. handling.
    380 			 */
    381 			if (!IS_KEY(code)) {
    382 				kbd_pkg_start(k, code);
    383 				continue;
    384 			}
    385 			/*
    386 			 * if not in event mode, deliver straight to ite to
    387 			 * process key stroke
    388 			 */
    389 			if (!k->k_event_mode) {
    390 				/* Gets to spltty() by itself	*/
    391 				ite_filter(code, ITEFILT_TTY);
    392 				continue;
    393 			}
    394 
    395 			/*
    396 			 * Keyboard is generating events.  Turn this keystroke
    397 			 * into an event and put it in the queue.  If the queue
    398 			 * is full, the keystroke is lost (sorry!).
    399 			 */
    400 			s = spltty();
    401 			put = k->k_events.ev_put;
    402 			fe  = &k->k_events.ev_q[put];
    403 			put = (put + 1) % EV_QSIZE;
    404 			if (put == k->k_events.ev_get) {
    405 				log(LOG_WARNING,
    406 					"keyboard event queue overflow\n");
    407 				splx(s);
    408 				continue;
    409 			}
    410 			fe->id             = KEY_CODE(code);
    411 			fe->value          = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
    412 			fe->time           = time;
    413 			k->k_events.ev_put = put;
    414 			EV_WAKEUP(&k->k_events);
    415 			splx(s);
    416 		}
    417 		kbd_rbget = get;
    418 	}
    419 }
    420 
    421 static	char sound[] = {
    422 	0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
    423 	0xF8,0x10,0x10,0x10,0x00,0x20,0x03
    424 };
    425 
    426 void
    427 kbdbell()
    428 {
    429 	register int	i, sps;
    430 
    431 	sps = spltty();
    432 	for (i = 0; i < sizeof(sound); i++) {
    433 		YM2149->sd_selr = i;
    434 		YM2149->sd_wdat = sound[i];
    435 	}
    436 	splx(sps);
    437 }
    438 
    439 int
    440 kbdgetcn()
    441 {
    442 	u_char	code;
    443 	int	s = spltty();
    444 	int	ints_active;
    445 
    446 	ints_active = 0;
    447 	if (MFP->mf_imrb & IB_AINT) {
    448 		ints_active   = 1;
    449 		MFP->mf_imrb &= ~IB_AINT;
    450 	}
    451 	for (;;) {
    452 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
    453 			;	/* Wait for key	*/
    454 		if (KBD->ac_cs & (A_OE|A_PE)) {
    455 			code = KBD->ac_da;	/* Silently ignore errors */
    456 			continue;
    457 		}
    458 		break;
    459 	}
    460 
    461 	code = KBD->ac_da;
    462 	if (ints_active) {
    463 		MFP->mf_iprb &= ~IB_AINT;
    464 		MFP->mf_imrb |=  IB_AINT;
    465 	}
    466 
    467 	splx (s);
    468 	return code;
    469 }
    470 
    471 /*
    472  * Write a command to the keyboard in 'polled' mode.
    473  */
    474 static int
    475 kbd_write_poll(cmd, len)
    476 u_char	*cmd;
    477 int	len;
    478 {
    479 	int	timeout;
    480 
    481 	while (len-- > 0) {
    482 		KBD->ac_da = *cmd++;
    483 		for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
    484 			delay(10);
    485 		if (!(KBD->ac_cs & A_TXRDY))
    486 			return (0);
    487 	}
    488 	return (1);
    489 }
    490 
    491 /*
    492  * Write a command to the keyboard. Return when command is send.
    493  */
    494 void
    495 kbd_write(cmd, len)
    496 u_char	*cmd;
    497 int	len;
    498 {
    499 	struct kbd_softc	*k = &kbd_softc;
    500 	int			sps;
    501 
    502 	/*
    503 	 * Get to splhigh, 'real' interrupts arrive at spl6!
    504 	 */
    505 	sps = splhigh();
    506 
    507 	/*
    508 	 * Make sure any privious write has ended...
    509 	 */
    510 	while (k->k_sendp != NULL)
    511 		tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
    512 
    513 	/*
    514 	 * If the KBD-acia is not currently busy, send the first
    515 	 * character now.
    516 	 */
    517 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
    518 	if (KBD->ac_cs & A_TXRDY) {
    519 		KBD->ac_da = *cmd++;
    520 		len--;
    521 	}
    522 
    523 	/*
    524 	 * If we're not yet done, wait until all characters are send.
    525 	 */
    526 	if (len > 0) {
    527 		k->k_sendp    = cmd;
    528 		k->k_send_cnt = len;
    529 		tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
    530 	}
    531 	splx(sps);
    532 
    533 	/*
    534 	 * Wakeup all procs waiting for us.
    535 	 */
    536 	wakeup((caddr_t)&k->k_sendp);
    537 }
    538 
    539 /*
    540  * Setup softc-fields to assemble a keyboard package.
    541  */
    542 static void
    543 kbd_pkg_start(kp, msg_start)
    544 struct kbd_softc *kp;
    545 u_char		 msg_start;
    546 {
    547 	kp->k_pkg_idx    = 1;
    548 	kp->k_package[0] = msg_start;
    549 	switch (msg_start) {
    550 		case 0xf6:
    551 			kp->k_pkg_size = 8;
    552 			break;
    553 		case 0xf7:
    554 			kp->k_pkg_size = 6;
    555 			break;
    556 		case 0xf8:
    557 		case 0xf9:
    558 		case 0xfa:
    559 		case 0xfb:
    560 			kp->k_pkg_size = 3;
    561 			break;
    562 		case 0xfc:
    563 			kp->k_pkg_size = 7;
    564 			break;
    565 		case 0xfe:
    566 		case 0xff:
    567 			kp->k_pkg_size = 2;
    568 			break;
    569 		default:
    570 			printf("kbd: Unknown packet 0x%x\n", msg_start);
    571 			break;
    572 	}
    573 }
    574