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