Home | History | Annotate | Line # | Download | only in sun
kbd.c revision 1.69.4.1
      1 /*	$NetBSD: kbd.c,v 1.69.4.1 2020/04/08 14:08:12 martin 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. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     41  */
     42 
     43 /*
     44  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
     45  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
     46  * passes them up to the appropriate reader.
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.69.4.1 2020/04/08 14:08:12 martin Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/conf.h>
     55 #include <sys/device.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/kernel.h>
     58 #include <sys/proc.h>
     59 #include <sys/malloc.h>
     60 #include <sys/signal.h>
     61 #include <sys/signalvar.h>
     62 #include <sys/time.h>
     63 #include <sys/syslog.h>
     64 #include <sys/select.h>
     65 #include <sys/poll.h>
     66 #include <sys/file.h>
     67 
     68 #include <dev/sysmon/sysmon_taskq.h>
     69 
     70 #include <dev/wscons/wsksymdef.h>
     71 
     72 #include <dev/sun/kbd_reg.h>
     73 #include <dev/sun/kbio.h>
     74 #include <dev/sun/vuid_event.h>
     75 #include <dev/sun/event_var.h>
     76 #include <dev/sun/kbd_xlate.h>
     77 #include <dev/sun/kbdvar.h>
     78 
     79 #include "ioconf.h"
     80 #include "locators.h"
     81 #include "opt_sunkbd.h"
     82 #include "sysmon_envsys.h"
     83 
     84 dev_type_open(kbdopen);
     85 dev_type_close(kbdclose);
     86 dev_type_read(kbdread);
     87 dev_type_ioctl(kbdioctl);
     88 dev_type_poll(kbdpoll);
     89 dev_type_kqfilter(kbdkqfilter);
     90 
     91 const struct cdevsw kbd_cdevsw = {
     92 	.d_open = kbdopen,
     93 	.d_close = kbdclose,
     94 	.d_read = kbdread,
     95 	.d_write = nowrite,
     96 	.d_ioctl = kbdioctl,
     97 	.d_stop = nostop,
     98 	.d_tty = notty,
     99 	.d_poll = kbdpoll,
    100 	.d_mmap = nommap,
    101 	.d_kqfilter = kbdkqfilter,
    102 	.d_discard = nodiscard,
    103 	.d_flag = D_OTHER
    104 };
    105 
    106 #if NWSKBD > 0
    107 static int	wssunkbd_enable(void *, int);
    108 static void	wssunkbd_set_leds(void *, int);
    109 static int	wssunkbd_ioctl(void *, u_long, void *, int, struct lwp *);
    110 static void	sunkbd_wskbd_cngetc(void *, u_int *, int *);
    111 static void	sunkbd_wskbd_cnpollc(void *, int);
    112 static void	sunkbd_wskbd_cnbell(void *, u_int, u_int, u_int);
    113 static void	sunkbd_bell_off(void *v);
    114 static void	kbd_enable(device_t); /* deferred keyboard init */
    115 
    116 const struct wskbd_accessops sunkbd_wskbd_accessops = {
    117 	wssunkbd_enable,
    118 	wssunkbd_set_leds,
    119 	wssunkbd_ioctl,
    120 };
    121 
    122 extern const struct wscons_keydesc wssun_keydesctab[];
    123 const struct wskbd_mapdata sunkbd_wskbd_keymapdata = {
    124 	wssun_keydesctab,
    125 #ifdef SUNKBD_LAYOUT
    126 	SUNKBD_LAYOUT,
    127 #else
    128 	KB_US,
    129 #endif
    130 };
    131 
    132 const struct wskbd_consops sunkbd_wskbd_consops = {
    133         sunkbd_wskbd_cngetc,
    134         sunkbd_wskbd_cnpollc,
    135         sunkbd_wskbd_cnbell,
    136 };
    137 
    138 void kbd_wskbd_attach(struct kbd_softc *, int);
    139 #endif
    140 
    141 /* ioctl helpers */
    142 static int kbd_iockeymap(struct kbd_state *, u_long, struct kiockeymap *);
    143 #ifdef KIOCGETKEY
    144 static int kbd_oldkeymap(struct kbd_state *, u_long, struct okiockey *);
    145 #endif
    146 
    147 
    148 /* callbacks for console driver */
    149 static int kbd_cc_open(struct cons_channel *);
    150 static int kbd_cc_close(struct cons_channel *);
    151 
    152 /* console input */
    153 static void	kbd_input_console(struct kbd_softc *, int);
    154 static void	kbd_repeat(void *);
    155 static int	kbd_input_keysym(struct kbd_softc *, int);
    156 static void	kbd_input_string(struct kbd_softc *, char *);
    157 static void	kbd_input_funckey(struct kbd_softc *, int);
    158 static void	kbd_update_leds(struct kbd_softc *);
    159 
    160 #if NWSKBD > 0
    161 static void	kbd_input_wskbd(struct kbd_softc *, int);
    162 #endif
    163 
    164 /* firm events input */
    165 static void	kbd_input_event(struct kbd_softc *, int);
    166 
    167 /****************************************************************
    168  *  Entry points for /dev/kbd
    169  *  (open,close,read,write,...)
    170  ****************************************************************/
    171 
    172 /*
    173  * Open:
    174  * Check exclusion, open actual device (_iopen),
    175  * setup event channel, clear ASCII repeat stuff.
    176  */
    177 int
    178 kbdopen(dev_t dev, int flags, int mode, struct lwp *l)
    179 {
    180 	struct kbd_softc *k;
    181 	int error;
    182 
    183 	/* locate device */
    184 	k = device_lookup_private(&kbd_cd, minor(dev));
    185 	if (k == NULL)
    186 		return ENXIO;
    187 
    188 #if NWSKBD > 0
    189 	/*
    190 	 * NB: wscons support: while we can track if wskbd has called
    191 	 * enable(), we can't tell if that's for console input or for
    192 	 * events input, so we should probably just let the open to
    193 	 * always succeed regardless (e.g. Xsun opening /dev/kbd).
    194 	 */
    195 	if (!k->k_wsenabled)
    196 		wssunkbd_enable(k, 1);
    197 #endif
    198 
    199 	/* exclusive open required for /dev/kbd */
    200 	if (k->k_events.ev_io)
    201 		return EBUSY;
    202 	k->k_events.ev_io = l->l_proc;
    203 
    204 	/* stop pending autorepeat of console input */
    205 	if (k->k_cc != NULL && k->k_repeating) {
    206 		k->k_repeating = 0;
    207 		callout_stop(&k->k_repeat_ch);
    208 	}
    209 
    210 	/* open actual underlying device */
    211 	if (k->k_ops != NULL && k->k_ops->open != NULL)
    212 		if ((error = (*k->k_ops->open)(k)) != 0) {
    213 			k->k_events.ev_io = NULL;
    214 			return error;
    215 		}
    216 
    217 	ev_init(&k->k_events);
    218 	k->k_evmode = 0;	/* XXX: OK? */
    219 
    220 	return 0;
    221 }
    222 
    223 
    224 /*
    225  * Close:
    226  * Turn off event mode, dump the queue, and close the keyboard
    227  * unless it is supplying console input.
    228  */
    229 int
    230 kbdclose(dev_t dev, int flags, int mode, struct lwp *l)
    231 {
    232 	struct kbd_softc *k;
    233 
    234 	k = device_lookup_private(&kbd_cd, minor(dev));
    235 	k->k_evmode = 0;
    236 	ev_fini(&k->k_events);
    237 	k->k_events.ev_io = NULL;
    238 
    239 	if (k->k_ops != NULL && k->k_ops->close != NULL) {
    240 		int error;
    241 		if ((error = (*k->k_ops->close)(k)) != 0)
    242 			return error;
    243 	}
    244 	return 0;
    245 }
    246 
    247 
    248 int
    249 kbdread(dev_t dev, struct uio *uio, int flags)
    250 {
    251 	struct kbd_softc *k;
    252 
    253 	k = device_lookup_private(&kbd_cd, minor(dev));
    254 	return ev_read(&k->k_events, uio, flags);
    255 }
    256 
    257 
    258 int
    259 kbdpoll(dev_t dev, int events, struct lwp *l)
    260 {
    261 	struct kbd_softc *k;
    262 
    263 	k = device_lookup_private(&kbd_cd, minor(dev));
    264 	return ev_poll(&k->k_events, events, l);
    265 }
    266 
    267 int
    268 kbdkqfilter(dev_t dev, struct knote *kn)
    269 {
    270 	struct kbd_softc *k;
    271 
    272 	k = device_lookup_private(&kbd_cd, minor(dev));
    273 	return ev_kqfilter(&k->k_events, kn);
    274 }
    275 
    276 int
    277 kbdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    278 {
    279 	struct kbd_softc *k;
    280 	struct kbd_state *ks;
    281 	int error = 0;
    282 
    283 	k = device_lookup_private(&kbd_cd, minor(dev));
    284 	ks = &k->k_state;
    285 
    286 	switch (cmd) {
    287 
    288 	case KIOCTRANS: 	/* Set translation mode */
    289 		/* We only support "raw" mode on /dev/kbd */
    290 		if (*(int *)data != TR_UNTRANS_EVENT)
    291 			error = EINVAL;
    292 		break;
    293 
    294 	case KIOCGTRANS:	/* Get translation mode */
    295 		/* We only support "raw" mode on /dev/kbd */
    296 		*(int *)data = TR_UNTRANS_EVENT;
    297 		break;
    298 
    299 #ifdef KIOCGETKEY
    300 	case KIOCGETKEY:	/* Get keymap entry (old format) */
    301 		error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
    302 		break;
    303 #endif /* KIOCGETKEY */
    304 
    305 	case KIOCSKEY:  	/* Set keymap entry */
    306 		/* FALLTHROUGH */
    307 	case KIOCGKEY:  	/* Get keymap entry */
    308 		error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
    309 		break;
    310 
    311 	case KIOCCMD:		/* Send a command to the keyboard */
    312 		/* pass it to the middle layer */
    313 		if (k->k_ops != NULL && k->k_ops->docmd != NULL)
    314 			error = (*k->k_ops->docmd)(k, *(int *)data, 1);
    315 		break;
    316 
    317 	case KIOCTYPE:		/* Get keyboard type */
    318 		*(int *)data = ks->kbd_id;
    319 		break;
    320 
    321 	case KIOCSDIRECT:	/* Where to send input */
    322 		k->k_evmode = *(int *)data;
    323 		break;
    324 
    325 	case KIOCLAYOUT:	/* Get keyboard layout */
    326 		*(int *)data = ks->kbd_layout;
    327 		break;
    328 
    329 	case KIOCSLED:		/* Set keyboard LEDs */
    330 		/* pass the request to the middle layer */
    331 		if (k->k_ops != NULL && k->k_ops->setleds != NULL)
    332 			error = (*k->k_ops->setleds)(k, *(char *)data, 1);
    333 		break;
    334 
    335 	case KIOCGLED:		/* Get keyboard LEDs */
    336 		*(char *)data = ks->kbd_leds;
    337 		break;
    338 
    339 	case FIONBIO:		/* we will remove this someday (soon???) */
    340 		break;
    341 
    342 	case FIOASYNC:
    343 		k->k_events.ev_async = (*(int *)data != 0);
    344 		break;
    345 
    346 	case FIOSETOWN:
    347 		if (-*(int *)data != k->k_events.ev_io->p_pgid
    348 		    && *(int *)data != k->k_events.ev_io->p_pid)
    349 			error = EPERM;
    350 		break;
    351 
    352 	case TIOCSPGRP:
    353 		if (*(int *)data != k->k_events.ev_io->p_pgid)
    354 			error = EPERM;
    355 		break;
    356 
    357 	default:
    358 		error = ENOTTY;
    359 		break;
    360 	}
    361 
    362 	return error;
    363 }
    364 
    365 
    366 /****************************************************************
    367  * ioctl helpers
    368  ****************************************************************/
    369 
    370 /*
    371  * Get/Set keymap entry
    372  */
    373 static int
    374 kbd_iockeymap(struct kbd_state *ks, u_long cmd, struct kiockeymap *kio)
    375 {
    376 	u_short *km;
    377 	u_int station;
    378 
    379 	switch (kio->kio_tablemask) {
    380 	case KIOC_NOMASK:
    381 		km = ks->kbd_k.k_normal;
    382 		break;
    383 	case KIOC_SHIFTMASK:
    384 		km = ks->kbd_k.k_shifted;
    385 		break;
    386 	case KIOC_CTRLMASK:
    387 		km = ks->kbd_k.k_control;
    388 		break;
    389 	case KIOC_UPMASK:
    390 		km = ks->kbd_k.k_release;
    391 		break;
    392 	default:
    393 		/* Silently ignore unsupported masks */
    394 		return 0;
    395 	}
    396 
    397 	/* Range-check the table position. */
    398 	station = kio->kio_station;
    399 	if (station >= KEYMAP_SIZE)
    400 		return EINVAL;
    401 
    402 	switch (cmd) {
    403 
    404 	case KIOCGKEY:	/* Get keymap entry */
    405 		kio->kio_entry = km[station];
    406 		break;
    407 
    408 	case KIOCSKEY:	/* Set keymap entry */
    409 		km[station] = kio->kio_entry;
    410 		break;
    411 
    412 	default:
    413 		return ENOTTY;
    414 	}
    415 	return 0;
    416 }
    417 
    418 
    419 #ifdef KIOCGETKEY
    420 /*
    421  * Get/Set keymap entry,
    422  * old format (compatibility)
    423  */
    424 int
    425 kbd_oldkeymap(struct kbd_state *ks, u_long cmd, struct okiockey *kio)
    426 {
    427 	int error = 0;
    428 
    429 	switch (cmd) {
    430 
    431 	case KIOCGETKEY:
    432 		if (kio->kio_station == 118) {
    433 			/*
    434 			 * This is X11 asking if a type 3 keyboard is
    435 			 * really a type 3 keyboard.  Say yes, it is,
    436 			 * by reporting key station 118 as a "hole".
    437 			 * Note old (SunOS 3.5) definition of HOLE!
    438 			 */
    439 			kio->kio_entry = 0xA2;
    440 			break;
    441 		}
    442 		/* fall through */
    443 
    444 	default:
    445 		error = ENOTTY;
    446 		break;
    447 	}
    448 
    449 	return error;
    450 }
    451 #endif /* KIOCGETKEY */
    452 
    453 
    454 
    455 /****************************************************************
    456  *  Keyboard input - called by middle layer at spltty().
    457  ****************************************************************/
    458 
    459 void
    460 kbd_input(struct kbd_softc *k, int code)
    461 {
    462 	if (k->k_evmode) {
    463 		/*
    464 		 * XXX: is this still true?
    465 		 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
    466 		 * This is bad as it means the server will not automatically resync
    467 		 * on all-up IDLEs, but I did not drop them before, and the server
    468 		 * goes crazy when it comes time to blank the screen....
    469 		 */
    470 		if (code == KBD_IDLE)
    471 			return;
    472 
    473 		/*
    474 		 * Keyboard is generating firm events.  Turn this keystroke
    475 		 * into an event and put it in the queue.
    476 		 */
    477 		kbd_input_event(k, code);
    478 		return;
    479 	}
    480 
    481 #if NWSKBD > 0
    482 	if (k->k_wskbd != NULL && k->k_wsenabled) {
    483 		/*
    484 		 * We are using wskbd input mode, pass the event up.
    485 		 */
    486 		if (code == KBD_IDLE)
    487 			return;	/* this key is not in the mapped */
    488 		kbd_input_wskbd(k, code);
    489 		return;
    490 	}
    491 #endif
    492 
    493 	/*
    494 	 * If /dev/kbd is not connected in event mode, or wskbd mode,
    495 	 * and is attached as console, translate and send upstream
    496 	 * (to console).
    497 	 */
    498 	if (k->k_cc != NULL)
    499 		kbd_input_console(k, code);
    500 }
    501 
    502 
    503 
    504 /****************************************************************
    505  *  Open/close routines called upon opening /dev/console
    506  *  if we serve console input.
    507  ****************************************************************/
    508 
    509 struct cons_channel *
    510 kbd_cc_alloc(struct kbd_softc *k)
    511 {
    512 	struct cons_channel *cc;
    513 
    514 	if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
    515 		return NULL;
    516 
    517 	/* our callbacks for the console driver */
    518 	cc->cc_private = k;
    519 	cc->cc_iopen = kbd_cc_open;
    520 	cc->cc_iclose = kbd_cc_close;
    521 
    522 	/* will be provided by the console driver so that we can feed input */
    523 	cc->cc_upstream = NULL;
    524 
    525 	/*
    526 	 * TODO: clean up cons_attach_input() vs kd_attach_input() in
    527 	 * lower layers and move that code here.
    528 	 */
    529 
    530 	k->k_cc = cc;
    531 	return cc;
    532 }
    533 
    534 
    535 static int
    536 kbd_cc_open(struct cons_channel *cc)
    537 {
    538 	struct kbd_softc *k;
    539 	int ret;
    540 
    541 	if (cc == NULL)
    542 		return 0;
    543 
    544 	k = cc->cc_private;
    545 	if (k == NULL)
    546 		return 0;
    547 
    548 	if (k->k_ops != NULL && k->k_ops->open != NULL)
    549 		ret = (*k->k_ops->open)(k);
    550 	else
    551 		ret = 0;
    552 
    553 	/* XXX: verify that callout is not active? */
    554 	k->k_repeat_start = hz/2;
    555 	k->k_repeat_step = hz/20;
    556 	callout_init(&k->k_repeat_ch, 0);
    557 
    558 	return ret;
    559 }
    560 
    561 
    562 static int
    563 kbd_cc_close(struct cons_channel *cc)
    564 {
    565 	struct kbd_softc *k;
    566 	int ret;
    567 
    568 	if (cc == NULL)
    569 		return 0;
    570 
    571 	k = cc->cc_private;
    572 	if (k == NULL)
    573 		return 0;
    574 
    575 	if (k->k_ops != NULL && k->k_ops->close != NULL)
    576 		ret = (*k->k_ops->close)(k);
    577 	else
    578 		ret = 0;
    579 
    580 	/* stop any pending auto-repeat */
    581 	if (k->k_repeating) {
    582 		k->k_repeating = 0;
    583 		callout_stop(&k->k_repeat_ch);
    584 	}
    585 
    586 	return ret;
    587 }
    588 
    589 
    590 
    591 /****************************************************************
    592  *  Console input - called by middle layer at spltty().
    593  ****************************************************************/
    594 
    595 static void
    596 kbd_input_console(struct kbd_softc *k, int code)
    597 {
    598 	struct kbd_state *ks= &k->k_state;
    599 	int keysym;
    600 
    601 	/* any input stops auto-repeat (i.e. key release) */
    602 	if (k->k_repeating) {
    603 		k->k_repeating = 0;
    604 		callout_stop(&k->k_repeat_ch);
    605 	}
    606 
    607 	keysym = kbd_code_to_keysym(ks, code);
    608 
    609 	/* pass to console */
    610 	if (kbd_input_keysym(k, keysym)) {
    611 		log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
    612 		    " produced unexpected keysym 0x%x\n",
    613 		    device_xname(k->k_dev),
    614 		    code, ks->kbd_modbits, keysym);
    615 		return;		/* no point in auto-repeat here */
    616 	}
    617 
    618 	if (KEYSYM_NOREPEAT(keysym))
    619 		return;
    620 
    621 	/* setup for auto-repeat after initial delay */
    622 	k->k_repeating = 1;
    623 	k->k_repeatsym = keysym;
    624 	callout_reset(&k->k_repeat_ch, k->k_repeat_start,
    625 		      kbd_repeat, k);
    626 }
    627 
    628 
    629 /*
    630  * This is the autorepeat callout function scheduled by kbd_input() above.
    631  * Called at splsoftclock().
    632  */
    633 static void
    634 kbd_repeat(void *arg)
    635 {
    636 	struct kbd_softc *k = arg;
    637 	int s;
    638 
    639 	s = spltty();
    640 	if (k->k_repeating && k->k_repeatsym >= 0) {
    641 		/* feed typematic keysym to the console */
    642 		(void)kbd_input_keysym(k, k->k_repeatsym);
    643 
    644 		/* reschedule next repeat */
    645 		callout_reset(&k->k_repeat_ch, k->k_repeat_step,
    646 			      kbd_repeat, k);
    647 	}
    648 	splx(s);
    649 }
    650 
    651 
    652 
    653 /*
    654  * Supply keysym as console input.  Convert keysym to character(s) and
    655  * pass them up to cons_channel's upstream hook.
    656  *
    657  * Return zero on success, else the keysym that we could not handle
    658  * (so that the caller may complain).
    659  */
    660 static int
    661 kbd_input_keysym(struct kbd_softc *k, int keysym)
    662 {
    663 	struct kbd_state *ks = &k->k_state;
    664 	int data;
    665 	/* Check if a recipient has been configured */
    666 	if (k->k_cc == NULL || k->k_cc->cc_upstream == NULL)
    667 		return 0;
    668 
    669 	switch (KEYSYM_CLASS(keysym)) {
    670 
    671 	case KEYSYM_ASCII:
    672 		data = KEYSYM_DATA(keysym);
    673 		if (ks->kbd_modbits & KBMOD_META_MASK)
    674 			data |= 0x80;
    675 		(*k->k_cc->cc_upstream)(data);
    676 		break;
    677 
    678 	case KEYSYM_STRING:
    679 		data = keysym & 0xF;
    680 		kbd_input_string(k, kbd_stringtab[data]);
    681 		break;
    682 
    683 	case KEYSYM_FUNC:
    684 		kbd_input_funckey(k, keysym);
    685 		break;
    686 
    687 	case KEYSYM_CLRMOD:
    688 		data = 1 << (keysym & 0x1F);
    689 		ks->kbd_modbits &= ~data;
    690 		break;
    691 
    692 	case KEYSYM_SETMOD:
    693 		data = 1 << (keysym & 0x1F);
    694 		ks->kbd_modbits |= data;
    695 		break;
    696 
    697 	case KEYSYM_INVMOD:
    698 		data = 1 << (keysym & 0x1F);
    699 		ks->kbd_modbits ^= data;
    700 		kbd_update_leds(k);
    701 		break;
    702 
    703 	case KEYSYM_ALL_UP:
    704 		ks->kbd_modbits &= ~0xFFFF;
    705 		break;
    706 
    707 	case KEYSYM_SPECIAL:
    708 		if (keysym == KEYSYM_NOP)
    709 			break;
    710 		/* FALLTHROUGH */
    711 	default:
    712 		/* We could not handle it. */
    713 		return keysym;
    714 	}
    715 
    716 	return 0;
    717 }
    718 
    719 
    720 /*
    721  * Send string upstream.
    722  */
    723 static void
    724 kbd_input_string(struct kbd_softc *k, char *str)
    725 {
    726 
    727 	while (*str) {
    728 		(*k->k_cc->cc_upstream)(*str);
    729 		++str;
    730 	}
    731 }
    732 
    733 
    734 /*
    735  * Format the F-key sequence and send as a string.
    736  * XXX: Ugly compatibility mappings.
    737  */
    738 static void
    739 kbd_input_funckey(struct kbd_softc *k, int keysym)
    740 {
    741 	int n;
    742 	char str[12];
    743 
    744 	n = 0xC0 + (keysym & 0x3F);
    745 	snprintf(str, sizeof(str), "\033[%dz", n);
    746 	kbd_input_string(k, str);
    747 }
    748 
    749 
    750 /*
    751  * Update LEDs to reflect console input state.
    752  */
    753 static void
    754 kbd_update_leds(struct kbd_softc *k)
    755 {
    756 	struct kbd_state *ks = &k->k_state;
    757 	char leds;
    758 
    759 	leds = ks->kbd_leds;
    760 	leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
    761 
    762 	if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
    763 		leds |= LED_CAPS_LOCK;
    764 	if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
    765 		leds |= LED_NUM_LOCK;
    766 
    767 	if (k->k_ops != NULL && k->k_ops->setleds != NULL)
    768 		(void)(*k->k_ops->setleds)(k, leds, 0);
    769 }
    770 
    771 
    772 
    773 /****************************************************************
    774  *  Events input - called by middle layer at spltty().
    775  ****************************************************************/
    776 
    777 /*
    778  * Supply raw keystrokes when keyboard is open in firm event mode.
    779  *
    780  * Turn the keystroke into an event and put it in the queue.
    781  * If the queue is full, the keystroke is lost (sorry!).
    782  */
    783 static void
    784 kbd_input_event(struct kbd_softc *k, int code)
    785 {
    786 	struct firm_event *fe;
    787 	int put;
    788 
    789 #ifdef DIAGNOSTIC
    790 	if (!k->k_evmode) {
    791 		printf("%s: kbd_input_event called when not in event mode\n",
    792 		    device_xname(k->k_dev));
    793 		return;
    794 	}
    795 #endif
    796 	put = k->k_events.ev_put;
    797 	fe = &k->k_events.ev_q[put];
    798 	put = (put + 1) % EV_QSIZE;
    799 	if (put == k->k_events.ev_get) {
    800 		log(LOG_WARNING, "%s: event queue overflow\n",
    801 		    device_xname(k->k_dev));
    802 		return;
    803 	}
    804 
    805 	fe->id = KEY_CODE(code);
    806 	fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
    807 	firm_gettime(fe);
    808 	k->k_events.ev_put = put;
    809 	EV_WAKEUP(&k->k_events);
    810 }
    811 
    812 
    813 
    814 /****************************************************************
    815  *  Translation stuff declared in kbd_xlate.h
    816  ****************************************************************/
    817 
    818 /*
    819  * Initialization - called by either lower layer attach or by kdcninit.
    820  */
    821 void
    822 kbd_xlate_init(struct kbd_state *ks)
    823 {
    824 	struct keyboard *ktbls;
    825 	int id;
    826 
    827 	id = ks->kbd_id;
    828 	if (id < KBD_MIN_TYPE)
    829 		id = KBD_MIN_TYPE;
    830 	if (id > kbd_max_type)
    831 		id = kbd_max_type;
    832 	ktbls = keyboards[id];
    833 
    834 	ks->kbd_k = *ktbls; 	/* struct assignment */
    835 	ks->kbd_modbits = 0;
    836 }
    837 
    838 /*
    839  * Turn keyboard up/down codes into a KEYSYM.
    840  * Note that the "kd" driver (on sun3 and sparc64) uses this too!
    841  */
    842 int
    843 kbd_code_to_keysym(struct kbd_state *ks, int c)
    844 {
    845 	u_short *km;
    846 	int keysym;
    847 
    848 	/*
    849 	 * Get keymap pointer.  One of these:
    850 	 * release, control, shifted, normal, ...
    851 	 */
    852 	if (KEY_UP(c))
    853 		km = ks->kbd_k.k_release;
    854 	else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
    855 		km = ks->kbd_k.k_control;
    856 	else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
    857 		km = ks->kbd_k.k_shifted;
    858 	else
    859 		km = ks->kbd_k.k_normal;
    860 
    861 	if (km == NULL) {
    862 		/*
    863 		 * Do not know how to translate yet.
    864 		 * We will find out when a RESET comes along.
    865 		 */
    866 		return KEYSYM_NOP;
    867 	}
    868 	keysym = km[KEY_CODE(c)];
    869 
    870 	/*
    871 	 * Post-processing for Caps-lock
    872 	 */
    873 	if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
    874 		(KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
    875 	{
    876 		if (('a' <= keysym) && (keysym <= 'z'))
    877 			keysym -= ('a' - 'A');
    878 	}
    879 
    880 	/*
    881 	 * Post-processing for Num-lock.  All "function"
    882 	 * keysyms get indirected through another table.
    883 	 * (XXX: Only if numlock on.  Want off also!)
    884 	 */
    885 	if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
    886 		(KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
    887 	{
    888 		keysym = kbd_numlock_map[keysym & 0x3F];
    889 	}
    890 
    891 	return keysym;
    892 }
    893 
    894 
    895 /*
    896  * Back door for rcons (fb.c)
    897  */
    898 void
    899 kbd_bell(int on)
    900 {
    901 	struct kbd_softc *k;
    902 
    903 	k = device_lookup_private(&kbd_cd, 0); /* XXX: hardcoded minor */
    904 
    905 	if (k == NULL || k->k_ops == NULL || k->k_ops->docmd == NULL)
    906 		return;
    907 
    908 	(void)(*k->k_ops->docmd)(k, on ? KBD_CMD_BELL : KBD_CMD_NOBELL, 0);
    909 }
    910 
    911 #if NWSKBD > 0
    912 
    913 #if NSYSMON_ENVSYS
    914 static void
    915 kbd_powerbutton(void *cookie)
    916 {
    917 	struct kbd_softc *k = cookie;
    918 
    919 	sysmon_pswitch_event(&k->k_sm_pbutton, k->k_ev);
    920 }
    921 #endif
    922 
    923 static void
    924 kbd_input_wskbd(struct kbd_softc *k, int code)
    925 {
    926 	int type, key;
    927 
    928 #ifdef WSDISPLAY_COMPAT_RAWKBD
    929 	if (k->k_wsraw) {
    930 		u_char buf;
    931 
    932 		buf = code;
    933 		wskbd_rawinput(k->k_wskbd, &buf, 1);
    934 		return;
    935 	}
    936 #endif
    937 
    938 	type = KEY_UP(code) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    939 	key = KEY_CODE(code);
    940 
    941 	if (type == WSCONS_EVENT_KEY_DOWN) {
    942 		switch (key) {
    943 #ifdef KBD_HIJACK_VOLUME_BUTTONS
    944 			case 0x02:
    945 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    946 				return;
    947 			case 0x04:
    948 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    949 				return;
    950 #endif
    951 			case 0x30:
    952 #if NSYSMON_ENVSYS
    953 				if (k->k_isconsole) {
    954 					k->k_ev = KEY_UP(code) ?
    955 					    PSWITCH_EVENT_RELEASED :
    956 					    PSWITCH_EVENT_PRESSED;
    957 					sysmon_task_queue_sched(0,
    958 					    kbd_powerbutton, k);
    959 				}
    960 #endif
    961 				return;
    962 		}
    963 	}
    964 
    965 	wskbd_input(k->k_wskbd, type, key);
    966 }
    967 
    968 int
    969 wssunkbd_enable(void *v, int on)
    970 {
    971 	struct kbd_softc *k = v;
    972 
    973 	if (k->k_wsenabled != on) {
    974 		k->k_wsenabled = on;
    975 		if (on) {
    976 			/* open actual underlying device */
    977 			if (k->k_ops != NULL && k->k_ops->open != NULL)
    978 				(*k->k_ops->open)(k);
    979 			ev_init(&k->k_events);
    980 			k->k_evmode = 0;	/* XXX: OK? */
    981 		} else {
    982 			/* close underlying device */
    983 			if (k->k_ops != NULL && k->k_ops->close != NULL)
    984 				(*k->k_ops->close)(k);
    985 		}
    986 	}
    987 	return 0;
    988 }
    989 
    990 void
    991 wssunkbd_set_leds(void *v, int leds)
    992 {
    993 	struct kbd_softc *k = v;
    994 	int l = 0;
    995 
    996 	if (leds & WSKBD_LED_CAPS)
    997 		l |= LED_CAPS_LOCK;
    998 	if (leds & WSKBD_LED_NUM)
    999 		l |= LED_NUM_LOCK;
   1000 	if (leds & WSKBD_LED_SCROLL)
   1001 		l |= LED_SCROLL_LOCK;
   1002 	if (leds & WSKBD_LED_COMPOSE)
   1003 		l |= LED_COMPOSE;
   1004 	if (k->k_ops != NULL && k->k_ops->setleds != NULL)
   1005 		(*k->k_ops->setleds)(k, l, 0);
   1006 	k->k_leds=l;
   1007 }
   1008 
   1009 static int
   1010 wssunkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
   1011 {
   1012 	struct kbd_softc *k = v;
   1013 
   1014 	switch (cmd) {
   1015 		case WSKBDIO_GTYPE:
   1016 			/* we can't tell  4 from  5 or 6 */
   1017 			*(int *)data = k->k_state.kbd_id < KB_SUN4 ?
   1018 			    WSKBD_TYPE_SUN : WSKBD_TYPE_SUN5;
   1019 			return 0;
   1020 		case WSKBDIO_SETLEDS:
   1021 			wssunkbd_set_leds(v, *(int *)data);
   1022 			return 0;
   1023 		case WSKBDIO_GETLEDS:
   1024 			*(int *)data = k->k_leds;
   1025 			return 0;
   1026 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1027 		case WSKBDIO_SETMODE:
   1028 			k->k_wsraw = *(int *)data == WSKBD_RAW;
   1029 			return 0;
   1030 #endif
   1031 	}
   1032 	return EPASSTHROUGH;
   1033 }
   1034 
   1035 extern int	prom_cngetc(dev_t);
   1036 
   1037 static void
   1038 sunkbd_wskbd_cngetc(void *v, u_int *type, int *data)
   1039 {
   1040 	/* struct kbd_sun_softc *k = v; */
   1041 
   1042 	*data = prom_cngetc(0);
   1043 	*type = WSCONS_EVENT_ASCII;
   1044 }
   1045 
   1046 void
   1047 sunkbd_wskbd_cnpollc(void *v, int on)
   1048 {
   1049 }
   1050 
   1051 static void
   1052 sunkbd_bell_off(void *v)
   1053 {
   1054 	struct kbd_softc *k = v;
   1055 
   1056 	k->k_ops->docmd(k, KBD_CMD_NOBELL, 0);
   1057 }
   1058 
   1059 void
   1060 sunkbd_wskbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
   1061 {
   1062 	struct kbd_softc *k = v;
   1063 
   1064 	callout_reset(&k->k_wsbell, period * 1000 / hz, sunkbd_bell_off, v);
   1065 	k->k_ops->docmd(k, KBD_CMD_BELL, 0);
   1066 }
   1067 
   1068 void
   1069 kbd_enable(device_t dev)
   1070 {
   1071 	struct kbd_softc *k = device_private(dev);
   1072 	struct wskbddev_attach_args a;
   1073 
   1074 	if (k->k_isconsole)
   1075 		wskbd_cnattach(&sunkbd_wskbd_consops, k,
   1076 		    &sunkbd_wskbd_keymapdata);
   1077 
   1078 	a.console = k->k_isconsole;
   1079 	a.keymap = &sunkbd_wskbd_keymapdata;
   1080 	a.accessops = &sunkbd_wskbd_accessops;
   1081 	a.accesscookie = k;
   1082 
   1083 	/* XXX why? */
   1084 	k->k_wsenabled = 0;
   1085 
   1086 	/* Attach the wskbd */
   1087 	k->k_wskbd = config_found(k->k_dev, &a, wskbddevprint);
   1088 
   1089 	callout_init(&k->k_wsbell, 0);
   1090 
   1091 	wssunkbd_enable(k,1);
   1092 
   1093 	wssunkbd_set_leds(k, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
   1094 	delay(100000);
   1095 	wssunkbd_set_leds(k, 0);
   1096 }
   1097 
   1098 void
   1099 kbd_wskbd_attach(struct kbd_softc *k, int isconsole)
   1100 {
   1101 	k->k_isconsole = isconsole;
   1102 	if (isconsole) {
   1103 #if NSYSMON_ENVSYS
   1104 		sysmon_task_queue_init();
   1105 		memset(&k->k_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
   1106 		k->k_sm_pbutton.smpsw_name = device_xname(k->k_dev);
   1107 		k->k_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
   1108 		if (sysmon_pswitch_register(&k->k_sm_pbutton) != 0)
   1109 			aprint_error_dev(k->k_dev,
   1110 			    "unable to register power button with sysmon\n");
   1111 #endif
   1112 	}
   1113 	config_interrupts(k->k_dev, kbd_enable);
   1114 }
   1115 #endif
   1116