Home | History | Annotate | Line # | Download | only in sun
kbd.c revision 1.24
      1 /*	$NetBSD: kbd.c,v 1.24 2000/03/22 16:08:51 pk 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  * This is the "slave" driver that will be attached to
     55  * the "zsc" driver for a Sun keyboard.
     56  */
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/conf.h>
     61 #include <sys/device.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/kernel.h>
     64 #include <sys/proc.h>
     65 #include <sys/signal.h>
     66 #include <sys/signalvar.h>
     67 #include <sys/time.h>
     68 #include <sys/syslog.h>
     69 #include <sys/select.h>
     70 #include <sys/poll.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 #include <dev/sun/event_var.h>
     78 #include <dev/sun/kbd_xlate.h>
     79 #include <dev/sun/kbdvar.h>
     80 
     81 #include "locators.h"
     82 
     83 /*
     84  * Ideas:
     85  * /dev/kbd is not a tty (plain device)
     86  */
     87 
     88 /* Prototypes */
     89 static void	kbd_new_layout __P((struct kbd_softc *));
     90 static void	kbd_repeat __P((void *));
     91 static void	kbd_set_leds __P((struct kbd_softc *, int));
     92 static void	kbd_update_leds __P((struct kbd_softc *));
     93 static void	kbd_was_reset __P((struct kbd_softc *));
     94 static int 	kbd_drain_tx __P((struct kbd_softc *));
     95 static int	kbd_iopen __P((struct kbd_softc *));
     96 static int	kbd_iclose __P((struct kbd_softc *));
     97 
     98 cdev_decl(kbd);	/* open, close, read, write, ioctl, stop, ... */
     99 
    100 extern struct cfdriver kbd_cd;
    101 
    102 /****************************************************************
    103  *  Entry points for /dev/kbd
    104  *  (open,close,read,write,...)
    105  ****************************************************************/
    106 
    107 /*
    108  * Open:
    109  * Check exclusion, open actual device (_iopen),
    110  * setup event channel, clear ASCII repeat stuff.
    111  */
    112 int
    113 kbdopen(dev, flags, mode, p)
    114 	dev_t dev;
    115 	int flags, mode;
    116 	struct proc *p;
    117 {
    118 	struct kbd_softc *k;
    119 	int error, unit;
    120 
    121 	unit = minor(dev);
    122 	if (unit >= kbd_cd.cd_ndevs)
    123 		return (ENXIO);
    124 	k = kbd_cd.cd_devs[unit];
    125 	if (k == NULL)
    126 		return (ENXIO);
    127 
    128 	/* Exclusive open required for /dev/kbd */
    129 	if (k->k_events.ev_io)
    130 		return (EBUSY);
    131 	k->k_events.ev_io = p;
    132 
    133 	if ((error = kbd_iopen(k)) != 0) {
    134 		k->k_events.ev_io = NULL;
    135 		return (error);
    136 	}
    137 	ev_init(&k->k_events);
    138 	k->k_evmode = 1;	/* XXX: OK? */
    139 
    140 	if (k->k_repeating) {
    141 		k->k_repeating = 0;
    142 		untimeout(kbd_repeat, k);
    143 	}
    144 
    145 	return (0);
    146 }
    147 
    148 /*
    149  * Close:
    150  * Turn off event mode, dump the queue, and close the keyboard
    151  * unless it is supplying console input.
    152  */
    153 int
    154 kbdclose(dev, flags, mode, p)
    155 	dev_t dev;
    156 	int flags, mode;
    157 	struct proc *p;
    158 {
    159 	struct kbd_softc *k;
    160 
    161 	k = kbd_cd.cd_devs[minor(dev)];
    162 	k->k_evmode = 0;
    163 	ev_fini(&k->k_events);
    164 	k->k_events.ev_io = NULL;
    165 	return (0);
    166 }
    167 
    168 int
    169 kbdread(dev, uio, flags)
    170 	dev_t dev;
    171 	struct uio *uio;
    172 	int flags;
    173 {
    174 	struct kbd_softc *k;
    175 
    176 	k = kbd_cd.cd_devs[minor(dev)];
    177 	return (ev_read(&k->k_events, uio, flags));
    178 }
    179 
    180 /* this routine should not exist, but is convenient to write here for now */
    181 int
    182 kbdwrite(dev, uio, flags)
    183 	dev_t dev;
    184 	struct uio *uio;
    185 	int flags;
    186 {
    187 
    188 	return (EOPNOTSUPP);
    189 }
    190 
    191 int
    192 kbdpoll(dev, events, p)
    193 	dev_t dev;
    194 	int events;
    195 	struct proc *p;
    196 {
    197 	struct kbd_softc *k;
    198 
    199 	k = kbd_cd.cd_devs[minor(dev)];
    200 	return (ev_poll(&k->k_events, events, p));
    201 }
    202 
    203 
    204 static int kbd_iockeymap __P((struct kbd_state *ks,
    205 	u_long cmd, struct kiockeymap *kio));
    206 
    207 static int kbd_iocsled(struct kbd_softc *k, char *data);
    208 
    209 #ifdef	KIOCGETKEY
    210 static int kbd_oldkeymap __P((struct kbd_state *ks,
    211 	u_long cmd, struct okiockey *okio));
    212 #endif
    213 
    214 int
    215 kbdioctl(dev, cmd, data, flag, p)
    216 	dev_t dev;
    217 	u_long cmd;
    218 	caddr_t data;
    219 	int flag;
    220 	struct proc *p;
    221 {
    222 	struct kbd_softc *k;
    223 	struct kbd_state *ks;
    224 	int error = 0;
    225 
    226 	k = kbd_cd.cd_devs[minor(dev)];
    227 	ks = &k->k_state;
    228 
    229 	switch (cmd) {
    230 
    231 	case KIOCTRANS: 	/* Set translation mode */
    232 		/* We only support "raw" mode on /dev/kbd */
    233 		if (*(int *)data != TR_UNTRANS_EVENT)
    234 			error = EINVAL;
    235 		break;
    236 
    237 	case KIOCGTRANS:	/* Get translation mode */
    238 		/* We only support "raw" mode on /dev/kbd */
    239 		*(int *)data = TR_UNTRANS_EVENT;
    240 		break;
    241 
    242 #ifdef	KIOCGETKEY
    243 	case KIOCGETKEY:	/* Get keymap entry (old format) */
    244 		error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
    245 		break;
    246 #endif	KIOCGETKEY */
    247 
    248 	case KIOCSKEY:  	/* Set keymap entry */
    249 		/* fallthrough */
    250 	case KIOCGKEY:  	/* Get keymap entry */
    251 		error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
    252 		break;
    253 
    254 	case KIOCCMD:	/* Send a command to the keyboard */
    255 		error = kbd_docmd(*(int *)data, 1);
    256 		break;
    257 
    258 	case KIOCTYPE:	/* Get keyboard type */
    259 		*(int *)data = ks->kbd_id;
    260 		break;
    261 
    262 	case KIOCSDIRECT:	/* where to send input */
    263 		k->k_evmode = *(int *)data;
    264 		break;
    265 
    266 	case KIOCLAYOUT:	/* Get keyboard layout */
    267 		*(int *)data = ks->kbd_layout;
    268 		break;
    269 
    270 	case KIOCSLED:
    271 		error = kbd_iocsled(k, (char *)data);
    272 		break;
    273 
    274 	case KIOCGLED:
    275 		*(char *)data = ks->kbd_leds;
    276 		break;
    277 
    278 	case FIONBIO:		/* we will remove this someday (soon???) */
    279 		break;
    280 
    281 	case FIOASYNC:
    282 		k->k_events.ev_async = *(int *)data != 0;
    283 		break;
    284 
    285 	case TIOCSPGRP:
    286 		if (*(int *)data != k->k_events.ev_io->p_pgid)
    287 			error = EPERM;
    288 		break;
    289 
    290 	default:
    291 		error = ENOTTY;
    292 		break;
    293 	}
    294 
    295 	return (error);
    296 }
    297 
    298 /****************************************************************
    299  * ioctl helpers
    300  ****************************************************************/
    301 
    302 /*
    303  * Get/Set keymap entry
    304  */
    305 static int
    306 kbd_iockeymap(ks, cmd, kio)
    307 	struct kbd_state *ks;
    308 	u_long cmd;
    309 	struct kiockeymap *kio;
    310 {
    311 	u_short *km;
    312 	u_int station;
    313 
    314 	switch (kio->kio_tablemask) {
    315 	case KIOC_NOMASK:
    316 		km = ks->kbd_k.k_normal;
    317 		break;
    318 	case KIOC_SHIFTMASK:
    319 		km = ks->kbd_k.k_shifted;
    320 		break;
    321 	case KIOC_CTRLMASK:
    322 		km = ks->kbd_k.k_control;
    323 		break;
    324 	case KIOC_UPMASK:
    325 		km = ks->kbd_k.k_release;
    326 		break;
    327 	default:
    328 		/* Silently ignore unsupported masks */
    329 		return (0);
    330 	}
    331 
    332 	/* Range-check the table position. */
    333 	station = kio->kio_station;
    334 	if (station >= KEYMAP_SIZE)
    335 		return (EINVAL);
    336 
    337 	switch (cmd) {
    338 
    339 	case KIOCGKEY:	/* Get keymap entry */
    340 		kio->kio_entry = km[station];
    341 		break;
    342 
    343 	case KIOCSKEY:	/* Set keymap entry */
    344 		km[station] = kio->kio_entry;
    345 		break;
    346 
    347 	default:
    348 		return(ENOTTY);
    349 	}
    350 	return (0);
    351 }
    352 
    353 #ifdef	KIOCGETKEY
    354 /*
    355  * Get/Set keymap entry,
    356  * old format (compatibility)
    357  */
    358 int
    359 kbd_oldkeymap(ks, cmd, kio)
    360 	struct kbd_state *ks;
    361 	u_long cmd;
    362 	struct okiockey *kio;
    363 {
    364 	int error = 0;
    365 
    366 	switch (cmd) {
    367 
    368 	case KIOCGETKEY:
    369 		if (kio->kio_station == 118) {
    370 			/*
    371 			 * This is X11 asking if a type 3 keyboard is
    372 			 * really a type 3 keyboard.  Say yes, it is,
    373 			 * by reporting key station 118 as a "hole".
    374 			 * Note old (SunOS 3.5) definition of HOLE!
    375 			 */
    376 			kio->kio_entry = 0xA2;
    377 			break;
    378 		}
    379 		/* fall through */
    380 
    381 	default:
    382 		error = ENOTTY;
    383 		break;
    384 	}
    385 
    386 	return (error);
    387 }
    388 #endif	/* KIOCGETKEY */
    389 
    390 
    391 /*
    392  * keyboard command ioctl
    393  * ``unimplemented commands are ignored'' (blech)
    394  * This is also export to the fb driver.
    395  */
    396 int
    397 kbd_docmd(cmd, isuser)
    398 	int cmd;
    399 	int isuser;
    400 {
    401 	struct kbd_softc *k;
    402 	struct kbd_state *ks;
    403 	int error, s;
    404 
    405 	error = 0;
    406 	k = kbd_cd.cd_devs[0];
    407 	ks = &k->k_state;
    408 
    409 	switch (cmd) {
    410 
    411 	case KBD_CMD_BELL:
    412 	case KBD_CMD_NOBELL:
    413 		/* Supported by type 2, 3, and 4 keyboards */
    414 		break;
    415 
    416 	case KBD_CMD_CLICK:
    417 	case KBD_CMD_NOCLICK:
    418 		/* Unsupported by type 2 keyboards */
    419 		if (ks->kbd_id <= KB_SUN2)
    420 			return (0);
    421 		ks->kbd_click = (cmd == KBD_CMD_CLICK);
    422 		break;
    423 
    424 	default:
    425 		return (0);
    426 	}
    427 
    428 	s = spltty();
    429 
    430 	if (isuser)
    431 		error = kbd_drain_tx(k);
    432 
    433 	if (error == 0) {
    434 		kbd_output(k, cmd);
    435 		kbd_start_tx(k);
    436 	}
    437 
    438 	splx(s);
    439 
    440 	return (error);
    441 }
    442 
    443 /*
    444  * Set LEDs ioctl.
    445  */
    446 static int
    447 kbd_iocsled(k, data)
    448 	struct kbd_softc *k;
    449 	char *data;
    450 {
    451 	int leds, error, s;
    452 
    453 	leds = *data;
    454 
    455 	s = spltty();
    456 	error = kbd_drain_tx(k);
    457 	if (error == 0) {
    458 		kbd_set_leds(k, leds);
    459 	}
    460 	splx(s);
    461 
    462 	return (error);
    463 }
    464 
    465 
    466 /****************************************************************
    467  * middle layers:
    468  *  - keysym to ASCII sequence
    469  *  - raw key codes to keysym
    470  ****************************************************************/
    471 
    472 static void kbd_input_string __P((struct kbd_softc *, char *));
    473 static void kbd_input_funckey __P((struct kbd_softc *, int));
    474 static int  kbd_input_keysym __P((struct kbd_softc *, int));
    475 
    476 /*
    477  * Initialization done by either kdcninit or kbd_iopen
    478  */
    479 void
    480 kbd_xlate_init(ks)
    481 	struct kbd_state *ks;
    482 {
    483 	struct keyboard *ktbls;
    484 	int id;
    485 
    486 	id = ks->kbd_id;
    487 	if (id < KBD_MIN_TYPE)
    488 		id = KBD_MIN_TYPE;
    489 	if (id > kbd_max_type)
    490 		id = kbd_max_type;
    491 	ktbls = keyboards[id];
    492 
    493 	ks->kbd_k = *ktbls; 	/* struct assignment */
    494 	ks->kbd_modbits = 0;
    495 }
    496 
    497 /*
    498  * Turn keyboard up/down codes into a KEYSYM.
    499  * Note that the "kd" driver uses this too!
    500  */
    501 int
    502 kbd_code_to_keysym(ks, c)
    503 	struct kbd_state *ks;
    504 	int c;
    505 {
    506 	u_short *km;
    507 	int keysym;
    508 
    509 	/*
    510 	 * Get keymap pointer.  One of these:
    511 	 * release, control, shifted, normal, ...
    512 	 */
    513 	if (KEY_UP(c))
    514 		km = ks->kbd_k.k_release;
    515 	else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
    516 		km = ks->kbd_k.k_control;
    517 	else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
    518 		km = ks->kbd_k.k_shifted;
    519 	else
    520 		km = ks->kbd_k.k_normal;
    521 
    522 	if (km == NULL) {
    523 		/*
    524 		 * Do not know how to translate yet.
    525 		 * We will find out when a RESET comes along.
    526 		 */
    527 		return (KEYSYM_NOP);
    528 	}
    529 	keysym = km[KEY_CODE(c)];
    530 
    531 	/*
    532 	 * Post-processing for Caps-lock
    533 	 */
    534 	if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
    535 		(KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
    536 	{
    537 		if (('a' <= keysym) && (keysym <= 'z'))
    538 			keysym -= ('a' - 'A');
    539 	}
    540 
    541 	/*
    542 	 * Post-processing for Num-lock.  All "function"
    543 	 * keysyms get indirected through another table.
    544 	 * (XXX: Only if numlock on.  Want off also!)
    545 	 */
    546 	if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
    547 		(KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
    548 	{
    549 		keysym = kbd_numlock_map[keysym & 0x3F];
    550 	}
    551 
    552 	return (keysym);
    553 }
    554 
    555 void
    556 kbd_input_string(k, str)
    557 	struct kbd_softc *k;
    558 	char *str;
    559 {
    560 
    561 	while (*str) {
    562 		(*k->k_cc->cc_upstream)(*str);
    563 		str++;
    564 	}
    565 }
    566 
    567 void
    568 kbd_input_funckey(k, keysym)
    569 	struct kbd_softc *k;
    570 	int keysym;
    571 {
    572 	int n;
    573 	char str[12];
    574 
    575 	/*
    576 	 * Format the F-key sequence and send as a string.
    577 	 * XXX: Ugly compatibility mappings.
    578 	 */
    579 	n = 0xC0 + (keysym & 0x3F);
    580 	sprintf(str, "\033[%dz", n);
    581 	kbd_input_string(k, str);
    582 }
    583 
    584 /*
    585  * This is called by kbd_input_raw() or by kb_repeat()
    586  * to deliver ASCII input.  Called at spltty().
    587  *
    588  * Return zero on success, else the keysym that we
    589  * could not handle (so the caller may complain).
    590  */
    591 int
    592 kbd_input_keysym(k, keysym)
    593 	struct kbd_softc *k;
    594 	int keysym;
    595 {
    596 	struct kbd_state *ks = &k->k_state;
    597 	int data;
    598 
    599 	switch (KEYSYM_CLASS(keysym)) {
    600 
    601 	case KEYSYM_ASCII:
    602 		data = KEYSYM_DATA(keysym);
    603 		if (ks->kbd_modbits & KBMOD_META_MASK)
    604 			data |= 0x80;
    605 		(*k->k_cc->cc_upstream)(data);
    606 		break;
    607 
    608 	case KEYSYM_STRING:
    609 		data = keysym & 0xF;
    610 		kbd_input_string(k, kbd_stringtab[data]);
    611 		break;
    612 
    613 	case KEYSYM_FUNC:
    614 		kbd_input_funckey(k, keysym);
    615 		break;
    616 
    617 	case KEYSYM_CLRMOD:
    618 		data = 1 << (keysym & 0x1F);
    619 		ks->kbd_modbits &= ~data;
    620 		break;
    621 
    622 	case KEYSYM_SETMOD:
    623 		data = 1 << (keysym & 0x1F);
    624 		ks->kbd_modbits |= data;
    625 		break;
    626 
    627 	case KEYSYM_INVMOD:
    628 		data = 1 << (keysym & 0x1F);
    629 		ks->kbd_modbits ^= data;
    630 		kbd_update_leds(k);
    631 		break;
    632 
    633 	case KEYSYM_ALL_UP:
    634 		ks->kbd_modbits &= ~0xFFFF;
    635 		break;
    636 
    637 	case KEYSYM_SPECIAL:
    638 		if (keysym == KEYSYM_NOP)
    639 			break;
    640 		/* fall through */
    641 	default:
    642 		/* We could not handle it. */
    643 		return (keysym);
    644 	}
    645 	return (0);
    646 }
    647 
    648 /*
    649  * This is the autorepeat timeout function.
    650  * Called at splsoftclock().
    651  */
    652 static void
    653 kbd_repeat(arg)
    654 	void *arg;
    655 {
    656 	struct kbd_softc *k = (struct kbd_softc *)arg;
    657 	int s = spltty();
    658 
    659 	if (k->k_repeating && k->k_repeatsym >= 0) {
    660 		(void)kbd_input_keysym(k, k->k_repeatsym);
    661 		timeout(kbd_repeat, k, k->k_repeat_step);
    662 	}
    663 	splx(s);
    664 }
    665 
    666 /*
    667  * Called by our kbd_softint() routine on input,
    668  * which passes the raw hardware scan codes.
    669  * Called at spltty()
    670  */
    671 void
    672 kbd_input_raw(k, c)
    673 	struct kbd_softc *k;
    674 	int c;
    675 {
    676 	struct kbd_state *ks = &k->k_state;
    677 	struct firm_event *fe;
    678 	int put, keysym;
    679 
    680 	/* XXX - Input errors already handled. */
    681 
    682 	/* Are we expecting special input? */
    683 	if (ks->kbd_expect) {
    684 		if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
    685 			/* We read a KBD_RESET last time. */
    686 			ks->kbd_id = c;
    687 			kbd_was_reset(k);
    688 		}
    689 		if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
    690 			/* We read a KBD_LAYOUT last time. */
    691 			ks->kbd_layout = c;
    692 			kbd_new_layout(k);
    693 		}
    694 		ks->kbd_expect = 0;
    695 		return;
    696 	}
    697 
    698 	/* Is this one of the "special" input codes? */
    699 	if (KBD_SPECIAL(c)) {
    700 		switch (c) {
    701 		case KBD_RESET:
    702 			ks->kbd_expect |= KBD_EXPECT_IDCODE;
    703 			/* Fake an "all-up" to resync. translation. */
    704 			c = KBD_IDLE;
    705 			break;
    706 
    707 		case KBD_LAYOUT:
    708 			ks->kbd_expect |= KBD_EXPECT_LAYOUT;
    709 			return;
    710 
    711 		case KBD_ERROR:
    712 			log(LOG_WARNING, "%s: received error indicator\n",
    713 				k->k_dev.dv_xname);
    714 			return;
    715 
    716 		case KBD_IDLE:
    717 			/* Let this go to the translator. */
    718 			break;
    719 		}
    720 	}
    721 
    722 	/*
    723 	 * If /dev/kbd is not connected in event mode,
    724 	 * translate and send upstream (to console).
    725 	 */
    726 	if (!k->k_evmode && k->k_isconsole) {
    727 
    728 		/* Any input stops auto-repeat (i.e. key release). */
    729 		if (k->k_repeating) {
    730 			k->k_repeating = 0;
    731 			untimeout(kbd_repeat, k);
    732 		}
    733 
    734 		/* Translate this code to a keysym */
    735 		keysym = kbd_code_to_keysym(ks, c);
    736 
    737 		/* Pass up to the next layer. */
    738 		if (kbd_input_keysym(k, keysym)) {
    739 			log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
    740 				" produced unexpected keysym 0x%x\n",
    741 				k->k_dev.dv_xname, c,
    742 				ks->kbd_modbits, keysym);
    743 			/* No point in auto-repeat here. */
    744 			return;
    745 		}
    746 
    747 		/* Does this symbol get auto-repeat? */
    748 		if (KEYSYM_NOREPEAT(keysym))
    749 			return;
    750 
    751 		/* Setup for auto-repeat after initial delay. */
    752 		k->k_repeating = 1;
    753 		k->k_repeatsym = keysym;
    754 		timeout(kbd_repeat, k, k->k_repeat_start);
    755 		return;
    756 	}
    757 
    758 	/*
    759 	 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
    760 	 * This is bad as it means the server will not automatically resync
    761 	 * on all-up IDLEs, but I did not drop them before, and the server
    762 	 * goes crazy when it comes time to blank the screen....
    763 	 */
    764 	if (c == KBD_IDLE)
    765 		return;
    766 
    767 	/*
    768 	 * Keyboard is generating events.  Turn this keystroke into an
    769 	 * event and put it in the queue.  If the queue is full, the
    770 	 * keystroke is lost (sorry!).
    771 	 */
    772 	put = k->k_events.ev_put;
    773 	fe = &k->k_events.ev_q[put];
    774 	put = (put + 1) % EV_QSIZE;
    775 	if (put == k->k_events.ev_get) {
    776 		log(LOG_WARNING, "%s: event queue overflow\n",
    777 			k->k_dev.dv_xname); /* ??? */
    778 		return;
    779 	}
    780 	fe->id = KEY_CODE(c);
    781 	fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    782 	fe->time = time;
    783 	k->k_events.ev_put = put;
    784 	EV_WAKEUP(&k->k_events);
    785 }
    786 
    787 /****************************************************************/
    788 
    789 /*
    790  * Open/close routines called upon opening /dev/console
    791  * if we serve console input.
    792  */
    793 int
    794 kbd_cc_open(cc)
    795 	struct cons_channel *cc;
    796 {
    797 	struct kbd_softc *k = (struct kbd_softc *)cc->cc_dev;
    798 	return (kbd_iopen(k));
    799 }
    800 
    801 int
    802 kbd_cc_close(cc)
    803 	struct cons_channel *cc;
    804 {
    805 	struct kbd_softc *k = (struct kbd_softc *)cc->cc_dev;
    806 	return (kbd_iclose(k));
    807 }
    808 
    809 /*
    810  * Initialization to be done at first open.
    811  * This is called from kbdopen() or kd_cc_open()
    812  * Called with user context.
    813  */
    814 int
    815 kbd_iopen(k)
    816 	struct kbd_softc *k;
    817 {
    818 	struct kbd_state *ks;
    819 	int error, s;
    820 
    821 	if (k == NULL)
    822 		return (ENXIO);
    823 
    824 	ks = &k->k_state;
    825 
    826 	/* Tolerate extra calls. */
    827 	if (k->k_isopen)
    828 		return (0);
    829 
    830 	s = spltty();
    831 
    832 	/* Reset the keyboard and find out its type. */
    833 	kbd_output(k, KBD_CMD_RESET);
    834 	kbd_start_tx(k);
    835 	kbd_drain_tx(k);
    836 	/* The wakeup for this is in kbd_was_reset(). */
    837 	error = tsleep((caddr_t)&ks->kbd_id,
    838 				   PZERO | PCATCH, devopn, hz);
    839 	if (error == EWOULDBLOCK) { 	/* no response */
    840 		error = 0;
    841 		log(LOG_ERR, "%s: reset failed\n",
    842 			k->k_dev.dv_xname);
    843 		/*
    844 		 * Allow the open anyway (to keep getty happy)
    845 		 * but assume the "least common denominator".
    846 		 */
    847 		ks->kbd_id = KB_SUN2;
    848 	}
    849 
    850 	/* Initialize the table pointers for this type. */
    851 	kbd_xlate_init(ks);
    852 
    853 	/* Earlier than type 4 does not know "layout". */
    854 	if (ks->kbd_id < KB_SUN4)
    855 		goto out;
    856 
    857 	/* Ask for the layout. */
    858 	kbd_output(k, KBD_CMD_GETLAYOUT);
    859 	kbd_start_tx(k);
    860 	kbd_drain_tx(k);
    861 	/* The wakeup for this is in kbd_new_layout(). */
    862 	error = tsleep((caddr_t)&ks->kbd_layout,
    863 				   PZERO | PCATCH, devopn, hz);
    864 	if (error == EWOULDBLOCK) { 	/* no response */
    865 		error = 0;
    866 		log(LOG_ERR, "%s: no response to get_layout\n",
    867 			k->k_dev.dv_xname);
    868 		ks->kbd_layout = 0;
    869 	}
    870 
    871 out:
    872 	splx(s);
    873 
    874 	if (error == 0)
    875 		k->k_isopen = 1;
    876 
    877 	return (error);
    878 }
    879 
    880 int
    881 kbd_iclose(k)
    882 	struct kbd_softc *k;
    883 {
    884 	/* For now: */ return (0);
    885 }
    886 
    887 /*
    888  * Called by kbd_input_raw, at spltty()
    889  */
    890 static void
    891 kbd_was_reset(k)
    892 	struct kbd_softc *k;
    893 {
    894 	struct kbd_state *ks = &k->k_state;
    895 
    896 	/*
    897 	 * On first identification, wake up anyone waiting for type
    898 	 * and set up the table pointers.
    899 	 */
    900 	wakeup((caddr_t)&ks->kbd_id);
    901 
    902 	/* Restore keyclick, if necessary */
    903 	switch (ks->kbd_id) {
    904 
    905 	case KB_SUN2:
    906 		/* Type 2 keyboards don't support keyclick */
    907 		break;
    908 
    909 	case KB_SUN3:
    910 		/* Type 3 keyboards come up with keyclick on */
    911 		if (!ks->kbd_click) {
    912 			/* turn off the click */
    913 			kbd_output(k, KBD_CMD_NOCLICK);
    914 			kbd_start_tx(k);
    915 		}
    916 		break;
    917 
    918 	case KB_SUN4:
    919 		/* Type 4 keyboards come up with keyclick off */
    920 		if (ks->kbd_click) {
    921 			/* turn on the click */
    922 			kbd_output(k, KBD_CMD_CLICK);
    923 			kbd_start_tx(k);
    924 		}
    925 		break;
    926 	}
    927 
    928 	/* LEDs are off after reset. */
    929 	ks->kbd_leds = 0;
    930 }
    931 
    932 /*
    933  * Called by kbd_input_raw, at spltty()
    934  */
    935 static void
    936 kbd_new_layout(k)
    937 	struct kbd_softc *k;
    938 {
    939 	struct kbd_state *ks = &k->k_state;
    940 
    941 	/*
    942 	 * On first identification, wake up anyone waiting for type
    943 	 * and set up the table pointers.
    944 	 */
    945 	wakeup((caddr_t)&ks->kbd_layout);
    946 
    947 	/* XXX: switch decoding tables? */
    948 }
    949 
    950 
    951 /*
    952  * Wait for output to finish.
    953  * Called at spltty().  Has user context.
    954  */
    955 static int
    956 kbd_drain_tx(k)
    957 	struct kbd_softc *k;
    958 {
    959 	int error;
    960 
    961 	error = 0;
    962 
    963 	while (k->k_txflags & K_TXBUSY) {
    964 		k->k_txflags |= K_TXWANT;
    965 		error = tsleep((caddr_t)&k->k_txflags,
    966 					   PZERO | PCATCH, "kbdout", 0);
    967 	}
    968 
    969 	return (error);
    970 }
    971 
    972 /*
    973  * Enqueue some output for the keyboard
    974  * Called at spltty().
    975  */
    976 void
    977 kbd_output(k, c)
    978 	struct kbd_softc *k;
    979 	int c;	/* the data */
    980 {
    981 	int put;
    982 
    983 	put = k->k_tbput;
    984 	k->k_tbuf[put] = (u_char)c;
    985 	put = (put + 1) & KBD_TX_RING_MASK;
    986 
    987 	/* Would overrun if increment makes (put==get). */
    988 	if (put == k->k_tbget) {
    989 		log(LOG_WARNING, "%s: output overrun\n",
    990             k->k_dev.dv_xname);
    991 	} else {
    992 		/* OK, really increment. */
    993 		k->k_tbput = put;
    994 	}
    995 }
    996 
    997 /*
    998  * Start the sending data from the output queue
    999  * Called at spltty().
   1000  */
   1001 void
   1002 kbd_start_tx(k)
   1003 	struct kbd_softc *k;
   1004 {
   1005 	int get;
   1006 	u_char c;
   1007 
   1008 	if (k->k_txflags & K_TXBUSY)
   1009 		return;
   1010 
   1011 	/* Is there anything to send? */
   1012 	get = k->k_tbget;
   1013 	if (get == k->k_tbput) {
   1014 		/* Nothing to send.  Wake drain waiters. */
   1015 		if (k->k_txflags & K_TXWANT) {
   1016 			k->k_txflags &= ~K_TXWANT;
   1017 			wakeup((caddr_t)&k->k_txflags);
   1018 		}
   1019 		return;
   1020 	}
   1021 
   1022 	/* Have something to send. */
   1023 	c = k->k_tbuf[get];
   1024 	get = (get + 1) & KBD_TX_RING_MASK;
   1025 	k->k_tbget = get;
   1026 	k->k_txflags |= K_TXBUSY;
   1027 
   1028 	k->k_write_data(k, c);
   1029 }
   1030 
   1031 /*
   1032  * Called at spltty by:
   1033  * kbd_update_leds, kbd_iocsled
   1034  */
   1035 static void
   1036 kbd_set_leds(k, new_leds)
   1037 	struct kbd_softc *k;
   1038 	int new_leds;
   1039 {
   1040 	struct kbd_state *ks = &k->k_state;
   1041 
   1042 	/* Don't send unless state changes. */
   1043 	if (ks->kbd_leds == new_leds)
   1044 		return;
   1045 
   1046 	ks->kbd_leds = new_leds;
   1047 
   1048 	/* Only type 4 and later has LEDs anyway. */
   1049 	if (ks->kbd_id < KB_SUN4)
   1050 		return;
   1051 
   1052 	kbd_output(k, KBD_CMD_SETLED);
   1053 	kbd_output(k, new_leds);
   1054 	kbd_start_tx(k);
   1055 }
   1056 
   1057 /*
   1058  * Called at spltty by:
   1059  * kbd_input_keysym
   1060  */
   1061 static void
   1062 kbd_update_leds(k)
   1063     struct kbd_softc *k;
   1064 {
   1065 	struct kbd_state *ks = &k->k_state;
   1066 	char leds;
   1067 
   1068 	leds = ks->kbd_leds;
   1069 	leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
   1070 
   1071 	if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
   1072 		leds |= LED_CAPS_LOCK;
   1073 	if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
   1074 		leds |= LED_NUM_LOCK;
   1075 
   1076 	kbd_set_leds(k, leds);
   1077 }
   1078