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