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