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