Home | History | Annotate | Line # | Download | only in sun
kbd.c revision 1.3
      1 /*	$NetBSD: kbd.c,v 1.3 1996/02/19 04:36:13 gwr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     45  */
     46 
     47 /*
     48  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
     49  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
     50  * passes them up to the appropriate reader.
     51  */
     52 
     53 /*
     54  * Zilog Z8530 Dual UART driver (keyboard interface)
     55  *
     56  * This is the "slave" driver that will be attached to
     57  * the "zsc" driver for a Sun keyboard.
     58  */
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/proc.h>
     63 #include <sys/device.h>
     64 #include <sys/conf.h>
     65 #include <sys/file.h>
     66 #include <sys/ioctl.h>
     67 /* #include <sys/tty.h> */
     68 #include <sys/time.h>
     69 #include <sys/kernel.h>
     70 #include <sys/syslog.h>
     71 
     72 #include <dev/ic/z8530reg.h>
     73 #include <machine/z8530var.h>
     74 #include <machine/vuid_event.h>
     75 #include <machine/kbd.h>
     76 #include <machine/kbio.h>
     77 
     78 #include "event_var.h"
     79 #include "kbd_xlate.h"
     80 
     81 /*
     82  * Ideas:
     83  * /dev/kbd is not a tty (plain device)
     84  */
     85 
     86 /*
     87  * How many input characters we can buffer.
     88  * The port-specific var.h may override this.
     89  * Note: must be a power of two!
     90  */
     91 #define	KBD_RX_RING_SIZE	256
     92 #define KBD_RX_RING_MASK (KBD_RX_RING_SIZE-1)
     93 /*
     94  * Output buffer.  Only need a few chars.
     95  */
     96 #define	KBD_TX_RING_SIZE	16
     97 #define KBD_TX_RING_MASK (KBD_TX_RING_SIZE-1)
     98 /*
     99  * Keyboard serial line speed is fixed at 1200 bps.
    100  */
    101 #define KBD_BPS 1200
    102 #define KBD_RESET_TIMO 1000 /* mS. */
    103 
    104 /*
    105  * XXX - Historical comment - no longer quite right...
    106  * Keyboard driver state.  The ascii and kbd links go up and down and
    107  * we just sit in the middle doing translation.  Note that it is possible
    108  * to get just one of the two links, in which case /dev/kbd is unavailable.
    109  * The downlink supplies us with `internal' open and close routines which
    110  * will enable dataflow across the downlink.  We promise to call open when
    111  * we are willing to take keystrokes, and to call close when we are not.
    112  * If /dev/kbd is not the console tty input source, we do this whenever
    113  * /dev/kbd is in use; otherwise we just leave it open forever.
    114  */
    115 struct kbd_softc {
    116 	struct	device k_dev;		/* required first: base device */
    117 	struct	zs_chanstate *k_cs;
    118 
    119 	/* Flags to communicate with kbd_softint() */
    120 	volatile int k_intr_flags;
    121 #define	INTR_RX_OVERRUN 1
    122 #define INTR_TX_EMPTY   2
    123 #define INTR_ST_CHECK   4
    124 
    125 	/* Transmit state */
    126 	volatile int k_txflags;
    127 #define	K_TXBUSY 1
    128 #define K_TXWANT 2
    129 
    130 	/*
    131 	 * State of upper interface.
    132 	 */
    133 	int	k_isopen;		/* set if open has been done */
    134 	int	k_evmode;		/* set if we should produce events */
    135 	struct	evvar k_events;		/* event queue state */
    136 
    137 	/*
    138 	 * ACSI translation state
    139 	 */
    140 	int k_repeat_start; 	/* initial delay */
    141 	int k_repeat_step;  	/* inter-char delay */
    142 	int	k_repeatsym;		/* repeating symbol */
    143 	int	k_repeating;		/* we've called timeout() */
    144 	struct	kbd_state k_state;	/* ASCII translation state */
    145 
    146 	/*
    147 	 * Magic sequence stuff (L1-A)
    148 	 */
    149 	char k_isconsole;
    150 	char k_magic1_down;
    151 	u_char k_magic1;	/* L1 */
    152 	u_char k_magic2;	/* A */
    153 
    154 	/*
    155 	 * The transmit ring buffer.
    156 	 */
    157 	volatile u_int	k_tbget;	/* transmit buffer `get' index */
    158 	volatile u_int	k_tbput;	/* transmit buffer `put' index */
    159 	u_char	k_tbuf[KBD_TX_RING_SIZE]; /* data */
    160 
    161 	/*
    162 	 * The receive ring buffer.
    163 	 */
    164 	u_int	k_rbget;	/* ring buffer `get' index */
    165 	volatile u_int	k_rbput;	/* ring buffer `put' index */
    166 	u_short	k_rbuf[KBD_RX_RING_SIZE]; /* rr1, data pairs */
    167 
    168 };
    169 
    170 /* Prototypes */
    171 void	kbd_ascii(struct tty *);
    172 void	kbd_serial(struct tty *, void (*)(), void (*)());
    173 int 	kbd_iopen(int unit);
    174 void	kbd_was_reset(struct kbd_softc *);
    175 void	kbd_new_layout(struct kbd_softc *);
    176 void	kbd_rint(int);
    177 int 	kbd_docmd(struct kbd_softc *k, int cmd);
    178 void	kbd_output(struct kbd_softc *k, int c);
    179 void	kbd_start_tx(struct kbd_softc *k);
    180 void	kbd_repeat(void *arg);
    181 
    182 extern void kd_input(int ascii);
    183 
    184 cdev_decl(kbd);	/* open, close, read, write, ioctl, stop, ... */
    185 
    186 struct zsops zsops_kbd;
    187 
    188 /****************************************************************
    189  * Definition of the driver for autoconfig.
    190  ****************************************************************/
    191 
    192 static int	kbd_match(struct device *, void *, void *);
    193 static void	kbd_attach(struct device *, struct device *, void *);
    194 
    195 struct cfdriver kbdcd = {
    196 	NULL, "kbd", kbd_match, kbd_attach,
    197 	DV_DULL, sizeof(struct kbd_softc), NULL,
    198 };
    199 
    200 
    201 /*
    202  * kbd_match: how is this zs channel configured?
    203  */
    204 int
    205 kbd_match(parent, match, aux)
    206 	struct device *parent;
    207 	void   *match, *aux;
    208 {
    209 	struct cfdata *cf = match;
    210 	struct zsc_attach_args *args = aux;
    211 
    212 	/* Exact match required for keyboard. */
    213 	if (cf->cf_loc[0] == args->channel)
    214 		return 2;
    215 
    216 	return 0;
    217 }
    218 
    219 void
    220 kbd_attach(parent, self, aux)
    221 	struct device *parent, *self;
    222 	void   *aux;
    223 
    224 {
    225 	struct zsc_softc *zsc = (void *) parent;
    226 	struct kbd_softc *k = (void *) self;
    227 	struct zsc_attach_args *args = aux;
    228 	struct zs_chanstate *cs;
    229 	struct cfdata *cf;
    230 	int channel, kbd_unit;
    231 	int reset, s, tconst;
    232 
    233 	cf = k->k_dev.dv_cfdata;
    234 	kbd_unit = k->k_dev.dv_unit;
    235 	channel = args->channel;
    236 	cs = &zsc->zsc_cs[channel];
    237 	cs->cs_private = k;
    238 	cs->cs_ops = &zsops_kbd;
    239 	k->k_cs = cs;
    240 
    241 	if (args->hwflags & ZS_HWFLAG_CONSOLE) {
    242 		k->k_isconsole = 1;
    243 		printf(" (console)");
    244 	}
    245 	printf("\n");
    246 
    247 	/* Initialize the speed, etc. */
    248 	tconst = BPS_TO_TCONST(cs->cs_pclk_div16, KBD_BPS);
    249 	s = splzs();
    250 	if (k->k_isconsole == 0) {
    251 		/* Not the console; may need reset. */
    252 		reset = (channel == 0) ?
    253 			ZSWR9_A_RESET : ZSWR9_B_RESET;
    254 		zs_write_reg(cs, 9, reset);
    255 	}
    256 	/* These are OK as set by zscc: WR3, WR4, WR5 */
    257 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    258 	cs->cs_preg[12] = tconst;
    259 	cs->cs_preg[13] = tconst >> 8;
    260 	zs_loadchannelregs(cs);
    261 	splx(s);
    262 
    263 	/* Do this before any calls to kbd_rint(). */
    264 	kbd_xlate_init(&k->k_state);
    265 
    266 	/* XXX - Do this in open? */
    267 	k->k_repeat_start = hz/2;
    268 	k->k_repeat_step = hz/20;
    269 
    270 	/* Magic sequence. */
    271 	k->k_magic1 = KBD_L1;
    272 	k->k_magic2 = KBD_A;
    273 
    274 	/* Now attach the (kd) pseudo-driver. */
    275 	kd_init(kbd_unit);
    276 }
    277 
    278 
    279 /****************************************************************
    280  *  Entry points for /dev/kbd
    281  *  (open,close,read,write,...)
    282  ****************************************************************/
    283 
    284 /*
    285  * Open:
    286  * Check exclusion, open actual device (_iopen),
    287  * setup event channel, clear ASCII repeat stuff.
    288  */
    289 int
    290 kbdopen(dev, flags, mode, p)
    291 	dev_t dev;
    292 	int flags, mode;
    293 	struct proc *p;
    294 {
    295 	struct kbd_softc *k;
    296 	int error, s, unit;
    297 
    298 	unit = minor(dev);
    299 	if (unit >= kbdcd.cd_ndevs)
    300 		return (ENXIO);
    301 	k = kbdcd.cd_devs[unit];
    302 	if (k == NULL)
    303 		return (ENXIO);
    304 
    305 	/* Exclusive open required for /dev/kbd */
    306 	if (k->k_events.ev_io)
    307 		return (EBUSY);
    308 	k->k_events.ev_io = p;
    309 
    310 	if ((error = kbd_iopen(unit)) != 0) {
    311 		k->k_events.ev_io = NULL;
    312 		return (error);
    313 	}
    314 	ev_init(&k->k_events);
    315 	k->k_evmode = 1;	/* XXX: OK? */
    316 
    317 	if (k->k_repeating) {
    318 		k->k_repeating = 0;
    319 		untimeout(kbd_repeat, k);
    320 	}
    321 
    322 	return (0);
    323 }
    324 
    325 /*
    326  * Close:
    327  * Turn off event mode, dump the queue, and close the keyboard
    328  * unless it is supplying console input.
    329  */
    330 int
    331 kbdclose(dev, flags, mode, p)
    332 	dev_t dev;
    333 	int flags, mode;
    334 	struct proc *p;
    335 {
    336 	struct kbd_softc *k;
    337 
    338 	k = kbdcd.cd_devs[minor(dev)];
    339 	k->k_evmode = 0;
    340 	ev_fini(&k->k_events);
    341 	k->k_events.ev_io = NULL;
    342 	return (0);
    343 }
    344 
    345 int
    346 kbdread(dev, uio, flags)
    347 	dev_t dev;
    348 	struct uio *uio;
    349 	int flags;
    350 {
    351 	struct kbd_softc *k;
    352 
    353 	k = kbdcd.cd_devs[minor(dev)];
    354 	return (ev_read(&k->k_events, uio, flags));
    355 }
    356 
    357 /* this routine should not exist, but is convenient to write here for now */
    358 int
    359 kbdwrite(dev, uio, flags)
    360 	dev_t dev;
    361 	struct uio *uio;
    362 	int flags;
    363 {
    364 
    365 	return (EOPNOTSUPP);
    366 }
    367 
    368 int
    369 kbdselect(dev, rw, p)
    370 	dev_t dev;
    371 	int rw;
    372 	struct proc *p;
    373 {
    374 	struct kbd_softc *k;
    375 
    376 	k = kbdcd.cd_devs[minor(dev)];
    377 	return (ev_select(&k->k_events, rw, p));
    378 }
    379 
    380 static int kbd_oldkeymap __P((struct kbd_state *ks,
    381 	u_long cmd, struct okiockey *okio));
    382 
    383 static int kbd_iockeymap __P((struct kbd_state *ks,
    384 	u_long cmd, struct kiockeymap *kio));
    385 
    386 int
    387 kbdioctl(dev, cmd, data, flag, p)
    388 	dev_t dev;
    389 	u_long cmd;
    390 	register caddr_t data;
    391 	int flag;
    392 	struct proc *p;
    393 {
    394 	struct kbd_softc *k;
    395 	struct kbd_state *ks;
    396 	int *ip;
    397 	int error = 0;
    398 
    399 	k = kbdcd.cd_devs[minor(dev)];
    400 	ks = &k->k_state;
    401 
    402 	switch (cmd) {
    403 
    404 	case KIOCTRANS: 	/* Set translation mode */
    405 		ip = (int *)data;
    406 		/* We only support "raw" mode on /dev/kbd */
    407 		if (*ip != TR_UNTRANS_EVENT)
    408 			error = EINVAL;
    409 		break;
    410 
    411 	case KIOCGTRANS:	/* Get translation mode */
    412 		ip = (int *)data;
    413 		/* We only support "raw" mode on /dev/kbd */
    414 		*ip = TR_UNTRANS_EVENT;
    415 		break;
    416 
    417 #ifdef	KIOCGETKEY
    418 	case KIOCGETKEY:	/* Get keymap entry (old format) */
    419 		error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
    420 		break;
    421 #endif	KIOCGETKEY */
    422 
    423 	case KIOCSKEY:  	/* Set keymap entry */
    424 		/* Don't let just anyone hose the keyboard. */
    425 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    426 			return (error);
    427 		/* fallthrough */
    428 	case KIOCGKEY:  	/* Get keymap entry */
    429 		error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
    430 		break;
    431 
    432 	case KIOCCMD:	/* Send a command to the keyboard */
    433 		/*
    434 		 * ``unimplemented commands are ignored'' (blech)
    435 		 * so cannot check return value from kbd_docmd
    436 		 */
    437 		error = kbd_drain_tx(k);
    438 		if (error == 0) {
    439 			(void) kbd_docmd(k, *(int *)data);
    440 		}
    441 		break;
    442 
    443 	case KIOCTYPE:	/* Get keyboard type */
    444 		ip = (int *)data;
    445 		*ip = ks->kbd_id;
    446 		break;
    447 
    448 	case KIOCSDIRECT:	/* where to send input */
    449 		ip = (int *)data;
    450 		k->k_evmode = *ip;
    451 		break;
    452 
    453 	case KIOCLAYOUT:	/* Get keyboard layout */
    454 		*data = ks->kbd_layout;
    455 		break;
    456 
    457 	case KIOCSLED:
    458 		error = kbd_set_leds(k, data);
    459 		break;
    460 
    461 	case KIOCGLED:
    462 		*(char *)data = ks->kbd_leds;
    463 		break;
    464 
    465 	case FIONBIO:		/* we will remove this someday (soon???) */
    466 		break;
    467 
    468 	case FIOASYNC:
    469 		k->k_events.ev_async = *(int *)data != 0;
    470 		break;
    471 
    472 	case TIOCSPGRP:
    473 		ip = (int *)data;
    474 		if (*ip != k->k_events.ev_io->p_pgid)
    475 			error = EPERM;
    476 		break;
    477 
    478 	}
    479 
    480 	return (error);
    481 }
    482 
    483 /****************************************************************
    484  * ioctl helpers
    485  ****************************************************************/
    486 
    487 /*
    488  * Get/Set keymap entry
    489  */
    490 int
    491 kbd_iockeymap(ks, cmd, kio)
    492 	struct kbd_state *ks;
    493 	u_long cmd;
    494 	struct kiockeymap *kio;
    495 {
    496 	struct keymap *km;
    497 	u_int station;
    498 
    499 	switch (kio->kio_tablemask) {
    500 	case KIOC_NOMASK:
    501 		km = ks->kbd_k.k_normal;
    502 		break;
    503 	case KIOC_SHIFTMASK:
    504 		km = ks->kbd_k.k_shifted;
    505 		break;
    506 	case KIOC_CTRLMASK:
    507 		km = ks->kbd_k.k_control;
    508 		break;
    509 	case KIOC_UPMASK:
    510 		km = ks->kbd_k.k_release;
    511 		break;
    512 	default:
    513 		/* Silently ignore unsupported masks */
    514 		return (0);
    515 	}
    516 
    517 	/* Range-check the table position. */
    518 	station = kio->kio_station;
    519 	if (station >= KEYMAP_SIZE)
    520 		return (EINVAL);
    521 
    522 	switch (cmd) {
    523 
    524 	case KIOCGKEY:	/* Get keymap entry */
    525 		kio->kio_entry = km->keymap[station];
    526 		break;
    527 
    528 	case KIOCSKEY:	/* Set keymap entry */
    529 		km->keymap[station] = kio->kio_entry;
    530 		break;
    531 
    532 	default:
    533 		return(ENOTTY);
    534 	}
    535 	return (0);
    536 }
    537 
    538 #ifdef	KIOCGETKEY
    539 /*
    540  * Get/Set keymap entry,
    541  * old format (compatibility)
    542  */
    543 int
    544 kbd_oldkeymap(ks, cmd, kio)
    545 	struct kbd_state *ks;
    546 	u_long cmd;
    547 	struct okiockey *kio;
    548 {
    549 	int error = 0;
    550 
    551 	switch (cmd) {
    552 
    553 	case KIOCGETKEY:
    554 		if (kio->kio_station == 118) {
    555 			/*
    556 			 * This is X11 asking if a type 3 keyboard is
    557 			 * really a type 3 keyboard.  Say yes, it is,
    558 			 * by reporting key station 118 as a "hole".
    559 			 * Note old (SunOS 3.5) definition of HOLE!
    560 			 */
    561 			kio->kio_entry = 0xA2;
    562 			break;
    563 		}
    564 		/* fall through */
    565 
    566 	default:
    567 		error = ENOTTY;
    568 		break;
    569 	}
    570 
    571 	return (error);
    572 }
    573 #endif	/* KIOCGETKEY */
    574 
    575 /****************************************************************
    576  * middle layers:
    577  *  - keysym to ASCII sequence
    578  *  - raw key codes to keysym
    579  ****************************************************************/
    580 
    581 
    582 /*
    583  * Initialization done by either kdcninit or kbd_iopen
    584  */
    585 void
    586 kbd_xlate_init(ks)
    587 	struct kbd_state *ks;
    588 {
    589 	struct keyboard *ktbls;
    590 	int id;
    591 
    592 	id = ks->kbd_id;
    593 	if (id < KBD_MIN_TYPE)
    594 		id = KBD_MIN_TYPE;
    595 	if (id > kbd_max_type)
    596 		id = kbd_max_type;
    597 	ktbls = keyboards[id];
    598 
    599 	ks->kbd_k = *ktbls; 	/* struct assignment */
    600 	ks->kbd_modbits = 0;
    601 }
    602 
    603 /*
    604  * Turn keyboard up/down codes into a KEYSYM.
    605  * Note that the "kd" driver uses this too!
    606  */
    607 int
    608 kbd_code_to_keysym(ks, c)
    609 	register struct kbd_state *ks;
    610 	register int c;
    611 {
    612 	struct keymap *km;
    613 	int keysym;
    614 
    615 	/*
    616 	 * Get keymap pointer.  One of these:
    617 	 * release, control, shifted, normal, ...
    618 	 */
    619 	if (KEY_UP(c))
    620 		km = ks->kbd_k.k_release;
    621 	else {
    622 		if (ks->kbd_modbits & KBMOD_CTRL_MASK)
    623 			km = ks->kbd_k.k_control;
    624 		else {
    625 			if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
    626 				km = ks->kbd_k.k_shifted;
    627 			else
    628 				km = ks->kbd_k.k_normal;
    629 		}
    630 	}
    631 	if (km == NULL) {
    632 		/*
    633 		 * Do not know how to translate yet.
    634 		 * We will find out when a RESET comes along.
    635 		 */
    636 		keysym = KEYSYM_NOP;
    637 	} else
    638 		keysym = km->keymap[KEY_CODE(c)];
    639 
    640 	return (keysym);
    641 }
    642 
    643 void
    644 kbd_input_string(k, str)
    645 	struct kbd_softc *k;
    646 	char *str;
    647 {
    648 	while (*str) {
    649 		kd_input(*str);
    650 		str++;
    651 	}
    652 }
    653 
    654 void
    655 kbd_input_funckey(k, keysym)
    656 	struct kbd_softc *k;
    657 	register int keysym;
    658 {
    659 	register int n;
    660 	char str[12];
    661 
    662 	/*
    663 	 * Format the F-key sequence and send as a string.
    664 	 * XXX: Ugly compatibility mappings.
    665 	 */
    666 	n = 0xC0 + (keysym & 0x3F);
    667 	sprintf(str, "\033[%dz", n);
    668 	kbd_input_string(k, str);
    669 }
    670 
    671 /*
    672  * This is called by kbd_input_raw() or by kb_repeat()
    673  * to deliver ASCII input.  Called at splsoftclock()
    674  * XXX: Raise to spltty before calling kd_input() ?
    675  */
    676 void
    677 kbd_input_keysym(k, keysym)
    678 	struct kbd_softc *k;
    679 	register int keysym;
    680 {
    681 	struct kbd_state *ks = &k->k_state;
    682 	register int class, data;
    683 
    684 	class = KEYSYM_CLASS(keysym);
    685 	switch (class) {
    686 
    687 	case KEYSYM_ASCII:
    688 		data = KEYSYM_DATA(keysym);
    689 		if (ks->kbd_modbits & KBMOD_META_MASK)
    690 			data |= 0x80;
    691 		kd_input(data);
    692 		break;
    693 
    694 	case KEYSYM_STRING:
    695 		data = keysym & 0xF;
    696 		kbd_input_string(k, kbd_stringtab[data]);
    697 		break;
    698 
    699 	case KEYSYM_FUNC:
    700 		kbd_input_funckey(k, keysym);
    701 		break;
    702 
    703 	case KEYSYM_CLRMOD:
    704 		data = 1 << (keysym & 0x1F);
    705 		ks->kbd_modbits &= ~data;
    706 		break;
    707 
    708 	case KEYSYM_SETMOD:
    709 		data = 1 << (keysym & 0x1F);
    710 		ks->kbd_modbits |= data;
    711 		break;
    712 
    713 	case KEYSYM_INVMOD:
    714 		data = 1 << (keysym & 0x1F);
    715 		ks->kbd_modbits ^= data;
    716 		break;
    717 
    718 	case KEYSYM_ALL_UP:
    719 		ks->kbd_modbits &= ~0xFFFF;
    720 		break;
    721 
    722 	case KEYSYM_SPECIAL:
    723 		if (keysym == KEYSYM_NOP)
    724 			break;
    725 		/* fall through */
    726 	default:
    727 		log(LOG_WARNING, "%s: unexpected keysym 0x%x\n",
    728 			k->k_dev.dv_xname, keysym);
    729 		break;
    730 	}
    731 }
    732 
    733 /*
    734  * This is the autorepeat timeout function.
    735  * (called at splsoftclock)
    736  */
    737 void
    738 kbd_repeat(void *arg)
    739 {
    740 	struct kbd_softc *k = (struct kbd_softc *)arg;
    741 
    742 	if (k->k_repeating && k->k_repeatsym >= 0) {
    743 		kbd_input_keysym(k, k->k_repeatsym);
    744 		timeout(kbd_repeat, k, k->k_repeat_step);
    745 	}
    746 }
    747 
    748 /*
    749  * Called by our kbd_softint() routine on input,
    750  * which passes the raw hardware scan codes.
    751  * Note: this is called at splsoftclock()
    752  */
    753 void
    754 kbd_input_raw(k, c)
    755 	struct kbd_softc *k;
    756 	register int c;
    757 {
    758 	struct kbd_state *ks = &k->k_state;
    759 	struct firm_event *fe;
    760 	int put, keysym;
    761 
    762 	/* XXX - Input errors already handled. */
    763 
    764 	/* Are we expecting special input? */
    765 	if (ks->kbd_expect) {
    766 		if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
    767 			/* We read a KBD_RESET last time. */
    768 			ks->kbd_id = c;
    769 			kbd_was_reset(k);
    770 		}
    771 		if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
    772 			/* We read a KBD_LAYOUT last time. */
    773 			ks->kbd_layout = c;
    774 			kbd_new_layout(k);
    775 		}
    776 		ks->kbd_expect = 0;
    777 		return;
    778 	}
    779 
    780 	/* Is this one of the "special" input codes? */
    781 	if (KBD_SPECIAL(c)) {
    782 		switch (c) {
    783 		case KBD_RESET:
    784 			ks->kbd_expect |= KBD_EXPECT_IDCODE;
    785 			/* Fake an "all-up" to resync. translation. */
    786 			c = KBD_IDLE;
    787 			break;
    788 
    789 		case KBD_LAYOUT:
    790 			ks->kbd_expect |= KBD_EXPECT_LAYOUT;
    791 			return;
    792 
    793 		case KBD_ERROR:
    794 			log(LOG_WARNING, "%s: received error indicator\n",
    795 				k->k_dev.dv_xname);
    796 			return;
    797 
    798 		case KBD_IDLE:
    799 			/* Let this go to the translator. */
    800 			break;
    801 		}
    802 	}
    803 
    804 	/*
    805 	 * If /dev/kbd is not connected in event mode,
    806 	 * translate and send upstream (to console).
    807 	 */
    808 	if (!k->k_evmode) {
    809 
    810 		/* Any input stops auto-repeat (i.e. key release). */
    811 		if (k->k_repeating) {
    812 			k->k_repeating = 0;
    813 			untimeout(kbd_repeat, k);
    814 		}
    815 
    816 		/* Translate this code to a keysym */
    817 		keysym = kbd_code_to_keysym(ks, c);
    818 
    819 		/* Pass up to the next layer. */
    820 		kbd_input_keysym(k, keysym);
    821 
    822 		/* Does this symbol get auto-repeat? */
    823 		if (KEYSYM_NOREPEAT(keysym))
    824 			return;
    825 
    826 		/* Setup for auto-repeat after initial delay. */
    827 		k->k_repeating = 1;
    828 		k->k_repeatsym = keysym;
    829 		timeout(kbd_repeat, k, k->k_repeat_start);
    830 		return;
    831 	}
    832 
    833 	/*
    834 	 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
    835 	 * This is bad as it means the server will not automatically resync
    836 	 * on all-up IDLEs, but I did not drop them before, and the server
    837 	 * goes crazy when it comes time to blank the screen....
    838 	 */
    839 	if (c == KBD_IDLE)
    840 		return;
    841 
    842 	/*
    843 	 * Keyboard is generating events.  Turn this keystroke into an
    844 	 * event and put it in the queue.  If the queue is full, the
    845 	 * keystroke is lost (sorry!).
    846 	 */
    847 	put = k->k_events.ev_put;
    848 	fe = &k->k_events.ev_q[put];
    849 	put = (put + 1) % EV_QSIZE;
    850 	if (put == k->k_events.ev_get) {
    851 		log(LOG_WARNING, "%s: event queue overflow\n",
    852 			k->k_dev.dv_xname); /* ??? */
    853 		return;
    854 	}
    855 	fe->id = KEY_CODE(c);
    856 	fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    857 	fe->time = time;
    858 	k->k_events.ev_put = put;
    859 	EV_WAKEUP(&k->k_events);
    860 }
    861 
    862 /****************************************************************
    863  * Interface to the lower layer (zscc)
    864  ****************************************************************/
    865 
    866 static int
    867 kbd_rxint(cs)
    868 	register struct zs_chanstate *cs;
    869 {
    870 	register struct kbd_softc *k;
    871 	register int put, put_next;
    872 	register u_char c, rr1;
    873 
    874 	k = cs->cs_private;
    875 	put = k->k_rbput;
    876 
    877 	/* Read the input data ASAP. */
    878 	c = zs_read_data(cs);
    879 
    880 	/* Save the status register too. */
    881 	rr1 = zs_read_reg(cs, 1);
    882 
    883 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    884 		/* Clear the receive error. */
    885 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    886 	}
    887 
    888 	/*
    889 	 * Check NOW for a console abort sequence, so that we can
    890 	 * abort even when interrupts are locking up the machine.
    891 	 */
    892 	if (k->k_magic1_down) {
    893 		/* The last keycode was "MAGIC1" down. */
    894 		k->k_magic1_down = 0;
    895 		if ((c == k->k_magic2) && k->k_isconsole) {
    896 			/* Magic "L1-A" sequence; enter debugger. */
    897 			zs_abort();
    898 			/* Debugger done.  Fake L1-up to finish it. */
    899 			c = k->k_magic1 | KBD_UP;
    900 		}
    901 	}
    902 	if (c == k->k_magic1) {
    903 		k->k_magic1_down = 1;
    904 	}
    905 
    906 	k->k_rbuf[put] = (c << 8) | rr1;
    907 	put_next = (put + 1) & KBD_RX_RING_MASK;
    908 
    909 	/* Would overrun if increment makes (put==get). */
    910 	if (put_next == k->k_rbget) {
    911 		k->k_intr_flags |= INTR_RX_OVERRUN;
    912 	} else {
    913 		/* OK, really increment. */
    914 		put = put_next;
    915 	}
    916 
    917 	/* Done reading. */
    918 	k->k_rbput = put;
    919 
    920 	/* Ask for softint() call. */
    921 	cs->cs_softreq = 1;
    922 	return(1);
    923 }
    924 
    925 
    926 static int
    927 kbd_txint(cs)
    928 	register struct zs_chanstate *cs;
    929 {
    930 	register struct kbd_softc *k;
    931 	register int count, rval;
    932 
    933 	k = cs->cs_private;
    934 
    935 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    936 
    937 	k->k_intr_flags |= INTR_TX_EMPTY;
    938 	/* Ask for softint() call. */
    939 	cs->cs_softreq = 1;
    940 	return (1);
    941 }
    942 
    943 
    944 static int
    945 kbd_stint(cs)
    946 	register struct zs_chanstate *cs;
    947 {
    948 	register struct kbd_softc *k;
    949 	register int rr0;
    950 
    951 	k = cs->cs_private;
    952 
    953 	rr0 = zs_read_csr(cs);
    954 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    955 
    956 #if 0
    957 	if (rr0 & ZSRR0_BREAK) {
    958 		/* Keyboard unplugged? */
    959 		zs_abort();
    960 		return (0);
    961 	}
    962 #endif
    963 
    964 	k->k_intr_flags |= INTR_ST_CHECK;
    965 	/* Ask for softint() call. */
    966 	cs->cs_softreq = 1;
    967 	return (1);
    968 }
    969 
    970 /*
    971  * Get input from the recieve ring and pass it on.
    972  * Note: this is called at splsoftclock()
    973  */
    974 static int
    975 kbd_softint(cs)
    976 	struct zs_chanstate *cs;
    977 {
    978 	register struct kbd_softc *k;
    979 	register int get, c, s;
    980 	int intr_flags;
    981 	register u_short ring_data;
    982 	register u_char rr0, rr1;
    983 
    984 	k = cs->cs_private;
    985 
    986 	/* Atomically get and clear flags. */
    987 	s = splzs();
    988 	intr_flags = k->k_intr_flags;
    989 	k->k_intr_flags = 0;
    990 	splx(s);
    991 
    992 	/*
    993 	 * Copy data from the receive ring to the event layer.
    994 	 */
    995 	get = k->k_rbget;
    996 	while (get != k->k_rbput) {
    997 		ring_data = k->k_rbuf[get];
    998 		get = (get + 1) & KBD_RX_RING_MASK;
    999 
   1000 		/* low byte of ring_data is rr1 */
   1001 		c = (ring_data >> 8) & 0xff;
   1002 
   1003 		if (ring_data & ZSRR1_DO)
   1004 			intr_flags |= INTR_RX_OVERRUN;
   1005 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
   1006 			/*
   1007 			 * After garbage, flush pending input, and
   1008 			 * send a reset to resync key translation.
   1009 			 */
   1010 			log(LOG_ERR, "%s: input error (0x%x)\n",
   1011 				k->k_dev.dv_xname, ring_data);
   1012 			get = k->k_rbput; /* flush */
   1013 			goto send_reset;
   1014 		}
   1015 
   1016 		/* Pass this up to the "middle" layer. */
   1017 		kbd_input_raw(k, c);
   1018 	}
   1019 	if (intr_flags & INTR_RX_OVERRUN) {
   1020 		log(LOG_ERR, "%s: input overrun\n",
   1021 		    k->k_dev.dv_xname);
   1022 	send_reset:
   1023 		/* Send a reset to resync translation. */
   1024 		kbd_output(k, KBD_CMD_RESET);
   1025 		kbd_start_tx(k);
   1026 	}
   1027 	k->k_rbget = get;
   1028 
   1029 	if (intr_flags & INTR_TX_EMPTY) {
   1030 		/*
   1031 		 * Transmit done.  Try to send more, or
   1032 		 * clear busy and wakeup drain waiters.
   1033 		 */
   1034 		k->k_txflags &= ~K_TXBUSY;
   1035 		kbd_start_tx(k);
   1036 	}
   1037 
   1038 	if (intr_flags & INTR_ST_CHECK) {
   1039 		/*
   1040 		 * Status line change.  (Not expected.)
   1041 		 */
   1042 		log(LOG_ERR, "%s: status interrupt?\n",
   1043 		    k->k_dev.dv_xname);
   1044 	}
   1045 
   1046 	return (1);
   1047 }
   1048 
   1049 struct zsops zsops_kbd = {
   1050 	kbd_rxint,	/* receive char available */
   1051 	kbd_stint,	/* external/status */
   1052 	kbd_txint,	/* xmit buffer empty */
   1053 	kbd_softint,	/* process software interrupt */
   1054 };
   1055 
   1056 /****************************************************************
   1057  * misc...
   1058  ****************************************************************/
   1059 
   1060 /*
   1061  * Initialization to be done at first open.
   1062  * This is called from kbdopen or kdopen (in kd.c)
   1063  */
   1064 int
   1065 kbd_iopen(unit)
   1066 	int unit;
   1067 {
   1068 	struct kbd_softc *k;
   1069 	struct kbd_state *ks;
   1070 	int error, s;
   1071 
   1072 	if (unit >= kbdcd.cd_ndevs)
   1073 		return (ENXIO);
   1074 	k = kbdcd.cd_devs[unit];
   1075 	if (k == NULL)
   1076 		return (ENXIO);
   1077 	ks = &k->k_state;
   1078 	error = 0;
   1079 
   1080 	/* Tolerate extra calls. */
   1081 	if (k->k_isopen)
   1082 		return (error);
   1083 
   1084 	s = spltty();
   1085 
   1086 	/* Reset the keyboard and find out its type. */
   1087 	kbd_output(k, KBD_CMD_RESET);
   1088 	kbd_start_tx(k);
   1089 	kbd_drain_tx(k);
   1090 	/* The wakeup for this is in kbd_was_reset(). */
   1091 	error = tsleep((caddr_t)&ks->kbd_id,
   1092 				   PZERO | PCATCH, devopn, hz);
   1093 	if (error == EWOULDBLOCK) { 	/* no response */
   1094 		error = 0;
   1095 		log(LOG_ERR, "%s: reset failed\n",
   1096 			k->k_dev.dv_xname);
   1097 		/*
   1098 		 * Allow the open anyway (to keep getty happy)
   1099 		 * but assume the "least common denominator".
   1100 		 */
   1101 		ks->kbd_id = KB_SUN2;
   1102 	}
   1103 
   1104 	/* Earlier than type 4 does not know "layout". */
   1105 	if (ks->kbd_id < KB_SUN4)
   1106 		goto out;
   1107 
   1108 	/* Ask for the layout. */
   1109 	kbd_output(k, KBD_CMD_GETLAYOUT);
   1110 	kbd_start_tx(k);
   1111 	kbd_drain_tx(k);
   1112 	/* The wakeup for this is in kbd_new_layout(). */
   1113 	error = tsleep((caddr_t)&ks->kbd_layout,
   1114 				   PZERO | PCATCH, devopn, hz);
   1115 	if (error == EWOULDBLOCK) { 	/* no response */
   1116 		error = 0;
   1117 		log(LOG_ERR, "%s: no response to get_layout\n",
   1118 			k->k_dev.dv_xname);
   1119 		ks->kbd_layout = 0;
   1120 	}
   1121 
   1122 out:
   1123 	splx(s);
   1124 
   1125 	if (error == 0)
   1126 		k->k_isopen = 1;
   1127 
   1128 	return error;
   1129 }
   1130 
   1131 void
   1132 kbd_was_reset(k)
   1133 	struct kbd_softc *k;
   1134 {
   1135 	struct kbd_state *ks = &k->k_state;
   1136 
   1137 	/*
   1138 	 * On first identification, wake up anyone waiting for type
   1139 	 * and set up the table pointers.
   1140 	 */
   1141 	wakeup((caddr_t)&ks->kbd_id);
   1142 
   1143 	/* Restore keyclick, if necessary */
   1144 	switch (ks->kbd_id) {
   1145 
   1146 	case KB_SUN2:
   1147 		/* Type 2 keyboards don't support keyclick */
   1148 		break;
   1149 
   1150 	case KB_SUN3:
   1151 		/* Type 3 keyboards come up with keyclick on */
   1152 		if (!ks->kbd_click)
   1153 			(void) kbd_docmd(k, KBD_CMD_NOCLICK);
   1154 		break;
   1155 
   1156 	case KB_SUN4:
   1157 		/* Type 4 keyboards come up with keyclick off */
   1158 		if (ks->kbd_click)
   1159 			(void) kbd_docmd(k, KBD_CMD_CLICK);
   1160 		break;
   1161 	}
   1162 
   1163 	/* LEDs are off after reset. */
   1164 	ks->kbd_leds = 0;
   1165 }
   1166 
   1167 void
   1168 kbd_new_layout(k)
   1169 	struct kbd_softc *k;
   1170 {
   1171 	struct kbd_state *ks = &k->k_state;
   1172 
   1173 	/*
   1174 	 * On first identification, wake up anyone waiting for type
   1175 	 * and set up the table pointers.
   1176 	 */
   1177 	wakeup((caddr_t)&ks->kbd_layout);
   1178 
   1179 	/* XXX: switch decoding tables? */
   1180 }
   1181 
   1182 
   1183 /*
   1184  * Wait for output to finish.
   1185  * Called with user context.
   1186  */
   1187 int
   1188 kbd_drain_tx(k)
   1189 	struct kbd_softc *k;
   1190 {
   1191 	int error, s;
   1192 
   1193 	error = 0;
   1194 	s = spltty();
   1195 	while (k->k_txflags & K_TXBUSY) {
   1196 		k->k_txflags |= K_TXWANT;
   1197 		error = tsleep((caddr_t)&k->k_txflags,
   1198 					   PZERO | PCATCH, "kbdout", 0);
   1199 	}
   1200 	splx(s);
   1201 	return (error);
   1202 }
   1203 
   1204 /*
   1205  * Send out a byte to the keyboard (i.e. reset)
   1206  * Called with user context.
   1207  */
   1208 void
   1209 kbd_output(k, c)
   1210 	struct kbd_softc *k;
   1211 	int c;	/* the data */
   1212 {
   1213 	struct zs_chanstate *cs = k->k_cs;
   1214 	int put, s;
   1215 
   1216 	s = spltty();
   1217 	put = k->k_tbput;
   1218 	k->k_tbuf[put] = (u_char)c;
   1219 	put = (put + 1) & KBD_TX_RING_MASK;
   1220 
   1221 	/* Would overrun if increment makes (put==get). */
   1222 	if (put == k->k_tbget) {
   1223 		log(LOG_WARNING, "%s: output overrun\n",
   1224             k->k_dev.dv_xname);
   1225 	} else {
   1226 		/* OK, really increment. */
   1227 		k->k_tbput = put;
   1228 	}
   1229 
   1230 	splx(s);
   1231 }
   1232 
   1233 void
   1234 kbd_start_tx(k)
   1235     struct kbd_softc *k;
   1236 {
   1237 	struct zs_chanstate *cs = k->k_cs;
   1238 	int get, s;
   1239 	u_char c;
   1240 
   1241 	s = spltty();
   1242 	if (k->k_txflags & K_TXBUSY)
   1243 		goto out;
   1244 
   1245 	/* Is there anything to send? */
   1246 	get = k->k_tbget;
   1247 	if (get == k->k_tbput) {
   1248 		/* Nothing to send.  Wake drain waiters. */
   1249 		if (k->k_txflags & K_TXWANT) {
   1250 			k->k_txflags &= ~K_TXWANT;
   1251 			wakeup((caddr_t)&k->k_txflags);
   1252 		}
   1253 		goto out;
   1254 	}
   1255 
   1256 	/* Have something to send. */
   1257 	c = k->k_tbuf[get];
   1258 	get = (get + 1) & KBD_TX_RING_MASK;
   1259 	k->k_tbget = get;
   1260 	k->k_txflags |= K_TXBUSY;
   1261 
   1262 	/* Need splzs to avoid interruption of the delay. */
   1263 	(void) splzs();
   1264 	zs_write_data(cs, c);
   1265 
   1266 out:
   1267 	splx(s);
   1268 }
   1269 
   1270 
   1271 int
   1272 kbd_set_leds(k, data)
   1273 	struct kbd_softc *k;
   1274 	caddr_t data;
   1275 {
   1276 	struct kbd_state *ks = &k->k_state;
   1277 	int error, s;
   1278 	char new_leds;
   1279 
   1280 	error = 0;
   1281 	new_leds = *(char*)data;
   1282 	s = spltty();
   1283 
   1284 	/* Don't send unless state changes. */
   1285 	if (ks->kbd_leds == new_leds)
   1286 		goto out;
   1287 	ks->kbd_leds = new_leds;
   1288 
   1289 	/* Only type 4 and later has LEDs anyway. */
   1290 	if (ks->kbd_id < 4)
   1291 		goto out;
   1292 
   1293 	error = kbd_drain_tx(k);
   1294 	kbd_output(k, KBD_CMD_SETLED);
   1295 	kbd_output(k, new_leds);
   1296 	kbd_start_tx(k);
   1297 
   1298 out:
   1299 	splx(s);
   1300 	return(error);
   1301 }
   1302 
   1303 
   1304 /*
   1305  * Execute a keyboard command; return 0 on success.
   1306  */
   1307 int
   1308 kbd_docmd(k, cmd)
   1309 	struct kbd_softc *k;
   1310 	int cmd;
   1311 {
   1312 	struct kbd_state *ks = &k->k_state;
   1313 	int error, s;
   1314 
   1315 	switch (cmd) {
   1316 
   1317 	case KBD_CMD_BELL:
   1318 	case KBD_CMD_NOBELL:
   1319 		/* Supported by type 2, 3, and 4 keyboards */
   1320 		break;
   1321 
   1322 	case KBD_CMD_CLICK:
   1323 		/* Unsupported by type 2 keyboards */
   1324 		if (ks->kbd_id != KB_SUN2) {
   1325 			ks->kbd_click = 1;
   1326 			break;
   1327 		}
   1328 		return (EINVAL);
   1329 
   1330 	case KBD_CMD_NOCLICK:
   1331 		/* Unsupported by type 2 keyboards */
   1332 		if (ks->kbd_id != KB_SUN2) {
   1333 			ks->kbd_click = 0;
   1334 			break;
   1335 		}
   1336 		return (EINVAL);
   1337 
   1338 	default:
   1339 		return (EINVAL);	/* ENOTTY? EOPNOTSUPP? */
   1340 	}
   1341 
   1342 	kbd_output(k, cmd);
   1343 	kbd_start_tx(k);
   1344 	return (0);
   1345 }
   1346 
   1347