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