Home | History | Annotate | Line # | Download | only in sun
kbd.c revision 1.68
      1 /*	$NetBSD: kbd.c,v 1.68 2014/07/25 08:10:39 dholland 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.68 2014/07/25 08:10:39 dholland 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_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 	 * translate and send upstream (to console).
    496 	 */
    497 	kbd_input_console(k, code);
    498 }
    499 
    500 
    501 
    502 /****************************************************************
    503  *  Open/close routines called upon opening /dev/console
    504  *  if we serve console input.
    505  ****************************************************************/
    506 
    507 struct cons_channel *
    508 kbd_cc_alloc(struct kbd_softc *k)
    509 {
    510 	struct cons_channel *cc;
    511 
    512 	if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
    513 		return NULL;
    514 
    515 	/* our callbacks for the console driver */
    516 	cc->cc_private = k;
    517 	cc->cc_iopen = kbd_cc_open;
    518 	cc->cc_iclose = kbd_cc_close;
    519 
    520 	/* will be provided by the console driver so that we can feed input */
    521 	cc->cc_upstream = NULL;
    522 
    523 	/*
    524 	 * TODO: clean up cons_attach_input() vs kd_attach_input() in
    525 	 * lower layers and move that code here.
    526 	 */
    527 
    528 	k->k_cc = cc;
    529 	return cc;
    530 }
    531 
    532 
    533 static int
    534 kbd_cc_open(struct cons_channel *cc)
    535 {
    536 	struct kbd_softc *k;
    537 	int ret;
    538 
    539 	if (cc == NULL)
    540 		return 0;
    541 
    542 	k = cc->cc_private;
    543 	if (k == NULL)
    544 		return 0;
    545 
    546 	if (k->k_ops != NULL && k->k_ops->open != NULL)
    547 		ret = (*k->k_ops->open)(k);
    548 	else
    549 		ret = 0;
    550 
    551 	/* XXX: verify that callout is not active? */
    552 	k->k_repeat_start = hz/2;
    553 	k->k_repeat_step = hz/20;
    554 	callout_init(&k->k_repeat_ch, 0);
    555 
    556 	return ret;
    557 }
    558 
    559 
    560 static int
    561 kbd_cc_close(struct cons_channel *cc)
    562 {
    563 	struct kbd_softc *k;
    564 	int ret;
    565 
    566 	if (cc == NULL)
    567 		return 0;
    568 
    569 	k = cc->cc_private;
    570 	if (k == NULL)
    571 		return 0;
    572 
    573 	if (k->k_ops != NULL && k->k_ops->close != NULL)
    574 		ret = (*k->k_ops->close)(k);
    575 	else
    576 		ret = 0;
    577 
    578 	/* stop any pending auto-repeat */
    579 	if (k->k_repeating) {
    580 		k->k_repeating = 0;
    581 		callout_stop(&k->k_repeat_ch);
    582 	}
    583 
    584 	return ret;
    585 }
    586 
    587 
    588 
    589 /****************************************************************
    590  *  Console input - called by middle layer at spltty().
    591  ****************************************************************/
    592 
    593 static void
    594 kbd_input_console(struct kbd_softc *k, int code)
    595 {
    596 	struct kbd_state *ks= &k->k_state;
    597 	int keysym;
    598 
    599 	/* any input stops auto-repeat (i.e. key release) */
    600 	if (k->k_repeating) {
    601 		k->k_repeating = 0;
    602 		callout_stop(&k->k_repeat_ch);
    603 	}
    604 
    605 	keysym = kbd_code_to_keysym(ks, code);
    606 
    607 	/* pass to console */
    608 	if (kbd_input_keysym(k, keysym)) {
    609 		log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
    610 		    " produced unexpected keysym 0x%x\n",
    611 		    device_xname(k->k_dev),
    612 		    code, ks->kbd_modbits, keysym);
    613 		return;		/* no point in auto-repeat here */
    614 	}
    615 
    616 	if (KEYSYM_NOREPEAT(keysym))
    617 		return;
    618 
    619 	/* setup for auto-repeat after initial delay */
    620 	k->k_repeating = 1;
    621 	k->k_repeatsym = keysym;
    622 	callout_reset(&k->k_repeat_ch, k->k_repeat_start,
    623 		      kbd_repeat, k);
    624 }
    625 
    626 
    627 /*
    628  * This is the autorepeat callout function scheduled by kbd_input() above.
    629  * Called at splsoftclock().
    630  */
    631 static void
    632 kbd_repeat(void *arg)
    633 {
    634 	struct kbd_softc *k = arg;
    635 	int s;
    636 
    637 	s = spltty();
    638 	if (k->k_repeating && k->k_repeatsym >= 0) {
    639 		/* feed typematic keysym to the console */
    640 		(void)kbd_input_keysym(k, k->k_repeatsym);
    641 
    642 		/* reschedule next repeat */
    643 		callout_reset(&k->k_repeat_ch, k->k_repeat_step,
    644 			      kbd_repeat, k);
    645 	}
    646 	splx(s);
    647 }
    648 
    649 
    650 
    651 /*
    652  * Supply keysym as console input.  Convert keysym to character(s) and
    653  * pass them up to cons_channel's upstream hook.
    654  *
    655  * Return zero on success, else the keysym that we could not handle
    656  * (so that the caller may complain).
    657  */
    658 static int
    659 kbd_input_keysym(struct kbd_softc *k, int keysym)
    660 {
    661 	struct kbd_state *ks = &k->k_state;
    662 	int data;
    663 	/* Check if a recipient has been configured */
    664 	if (k->k_cc == NULL || k->k_cc->cc_upstream == NULL)
    665 		return 0;
    666 
    667 	switch (KEYSYM_CLASS(keysym)) {
    668 
    669 	case KEYSYM_ASCII:
    670 		data = KEYSYM_DATA(keysym);
    671 		if (ks->kbd_modbits & KBMOD_META_MASK)
    672 			data |= 0x80;
    673 		(*k->k_cc->cc_upstream)(data);
    674 		break;
    675 
    676 	case KEYSYM_STRING:
    677 		data = keysym & 0xF;
    678 		kbd_input_string(k, kbd_stringtab[data]);
    679 		break;
    680 
    681 	case KEYSYM_FUNC:
    682 		kbd_input_funckey(k, keysym);
    683 		break;
    684 
    685 	case KEYSYM_CLRMOD:
    686 		data = 1 << (keysym & 0x1F);
    687 		ks->kbd_modbits &= ~data;
    688 		break;
    689 
    690 	case KEYSYM_SETMOD:
    691 		data = 1 << (keysym & 0x1F);
    692 		ks->kbd_modbits |= data;
    693 		break;
    694 
    695 	case KEYSYM_INVMOD:
    696 		data = 1 << (keysym & 0x1F);
    697 		ks->kbd_modbits ^= data;
    698 		kbd_update_leds(k);
    699 		break;
    700 
    701 	case KEYSYM_ALL_UP:
    702 		ks->kbd_modbits &= ~0xFFFF;
    703 		break;
    704 
    705 	case KEYSYM_SPECIAL:
    706 		if (keysym == KEYSYM_NOP)
    707 			break;
    708 		/* FALLTHROUGH */
    709 	default:
    710 		/* We could not handle it. */
    711 		return keysym;
    712 	}
    713 
    714 	return 0;
    715 }
    716 
    717 
    718 /*
    719  * Send string upstream.
    720  */
    721 static void
    722 kbd_input_string(struct kbd_softc *k, char *str)
    723 {
    724 
    725 	while (*str) {
    726 		(*k->k_cc->cc_upstream)(*str);
    727 		++str;
    728 	}
    729 }
    730 
    731 
    732 /*
    733  * Format the F-key sequence and send as a string.
    734  * XXX: Ugly compatibility mappings.
    735  */
    736 static void
    737 kbd_input_funckey(struct kbd_softc *k, int keysym)
    738 {
    739 	int n;
    740 	char str[12];
    741 
    742 	n = 0xC0 + (keysym & 0x3F);
    743 	snprintf(str, sizeof(str), "\033[%dz", n);
    744 	kbd_input_string(k, str);
    745 }
    746 
    747 
    748 /*
    749  * Update LEDs to reflect console input state.
    750  */
    751 static void
    752 kbd_update_leds(struct kbd_softc *k)
    753 {
    754 	struct kbd_state *ks = &k->k_state;
    755 	char leds;
    756 
    757 	leds = ks->kbd_leds;
    758 	leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
    759 
    760 	if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
    761 		leds |= LED_CAPS_LOCK;
    762 	if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
    763 		leds |= LED_NUM_LOCK;
    764 
    765 	if (k->k_ops != NULL && k->k_ops->setleds != NULL)
    766 		(void)(*k->k_ops->setleds)(k, leds, 0);
    767 }
    768 
    769 
    770 
    771 /****************************************************************
    772  *  Events input - called by middle layer at spltty().
    773  ****************************************************************/
    774 
    775 /*
    776  * Supply raw keystrokes when keyboard is open in firm event mode.
    777  *
    778  * Turn the keystroke into an event and put it in the queue.
    779  * If the queue is full, the keystroke is lost (sorry!).
    780  */
    781 static void
    782 kbd_input_event(struct kbd_softc *k, int code)
    783 {
    784 	struct firm_event *fe;
    785 	int put;
    786 
    787 #ifdef DIAGNOSTIC
    788 	if (!k->k_evmode) {
    789 		printf("%s: kbd_input_event called when not in event mode\n",
    790 		    device_xname(k->k_dev));
    791 		return;
    792 	}
    793 #endif
    794 	put = k->k_events.ev_put;
    795 	fe = &k->k_events.ev_q[put];
    796 	put = (put + 1) % EV_QSIZE;
    797 	if (put == k->k_events.ev_get) {
    798 		log(LOG_WARNING, "%s: event queue overflow\n",
    799 		    device_xname(k->k_dev));
    800 		return;
    801 	}
    802 
    803 	fe->id = KEY_CODE(code);
    804 	fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
    805 	firm_gettime(fe);
    806 	k->k_events.ev_put = put;
    807 	EV_WAKEUP(&k->k_events);
    808 }
    809 
    810 
    811 
    812 /****************************************************************
    813  *  Translation stuff declared in kbd_xlate.h
    814  ****************************************************************/
    815 
    816 /*
    817  * Initialization - called by either lower layer attach or by kdcninit.
    818  */
    819 void
    820 kbd_xlate_init(struct kbd_state *ks)
    821 {
    822 	struct keyboard *ktbls;
    823 	int id;
    824 
    825 	id = ks->kbd_id;
    826 	if (id < KBD_MIN_TYPE)
    827 		id = KBD_MIN_TYPE;
    828 	if (id > kbd_max_type)
    829 		id = kbd_max_type;
    830 	ktbls = keyboards[id];
    831 
    832 	ks->kbd_k = *ktbls; 	/* struct assignment */
    833 	ks->kbd_modbits = 0;
    834 }
    835 
    836 /*
    837  * Turn keyboard up/down codes into a KEYSYM.
    838  * Note that the "kd" driver (on sun3 and sparc64) uses this too!
    839  */
    840 int
    841 kbd_code_to_keysym(struct kbd_state *ks, int c)
    842 {
    843 	u_short *km;
    844 	int keysym;
    845 
    846 	/*
    847 	 * Get keymap pointer.  One of these:
    848 	 * release, control, shifted, normal, ...
    849 	 */
    850 	if (KEY_UP(c))
    851 		km = ks->kbd_k.k_release;
    852 	else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
    853 		km = ks->kbd_k.k_control;
    854 	else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
    855 		km = ks->kbd_k.k_shifted;
    856 	else
    857 		km = ks->kbd_k.k_normal;
    858 
    859 	if (km == NULL) {
    860 		/*
    861 		 * Do not know how to translate yet.
    862 		 * We will find out when a RESET comes along.
    863 		 */
    864 		return KEYSYM_NOP;
    865 	}
    866 	keysym = km[KEY_CODE(c)];
    867 
    868 	/*
    869 	 * Post-processing for Caps-lock
    870 	 */
    871 	if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
    872 		(KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
    873 	{
    874 		if (('a' <= keysym) && (keysym <= 'z'))
    875 			keysym -= ('a' - 'A');
    876 	}
    877 
    878 	/*
    879 	 * Post-processing for Num-lock.  All "function"
    880 	 * keysyms get indirected through another table.
    881 	 * (XXX: Only if numlock on.  Want off also!)
    882 	 */
    883 	if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
    884 		(KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
    885 	{
    886 		keysym = kbd_numlock_map[keysym & 0x3F];
    887 	}
    888 
    889 	return keysym;
    890 }
    891 
    892 
    893 /*
    894  * Back door for rcons (fb.c)
    895  */
    896 void
    897 kbd_bell(int on)
    898 {
    899 	struct kbd_softc *k;
    900 
    901 	k = device_lookup_private(&kbd_cd, 0); /* XXX: hardcoded minor */
    902 
    903 	if (k == NULL || k->k_ops == NULL || k->k_ops->docmd == NULL)
    904 		return;
    905 
    906 	(void)(*k->k_ops->docmd)(k, on ? KBD_CMD_BELL : KBD_CMD_NOBELL, 0);
    907 }
    908 
    909 #if NWSKBD > 0
    910 
    911 #if NSYSMON_ENVSYS
    912 static void
    913 kbd_powerbutton(void *cookie)
    914 {
    915 	struct kbd_softc *k = cookie;
    916 
    917 	sysmon_pswitch_event(&k->k_sm_pbutton, k->k_ev);
    918 }
    919 #endif
    920 
    921 static void
    922 kbd_input_wskbd(struct kbd_softc *k, int code)
    923 {
    924 	int type, key;
    925 
    926 #ifdef WSDISPLAY_COMPAT_RAWKBD
    927 	if (k->k_wsraw) {
    928 		u_char buf;
    929 
    930 		buf = code;
    931 		wskbd_rawinput(k->k_wskbd, &buf, 1);
    932 		return;
    933 	}
    934 #endif
    935 
    936 	type = KEY_UP(code) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    937 	key = KEY_CODE(code);
    938 
    939 	if (type == WSCONS_EVENT_KEY_DOWN) {
    940 		switch (key) {
    941 #ifdef KBD_HIJACK_VOLUME_BUTTONS
    942 			case 0x02:
    943 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    944 				return;
    945 			case 0x04:
    946 				pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    947 				return;
    948 #endif
    949 			case 0x30:
    950 #if NSYSMON_ENVSYS
    951 				if (k->k_isconsole)
    952 					k->k_ev = KEY_UP(code) ?
    953 					    PSWITCH_EVENT_RELEASED :
    954 					    PSWITCH_EVENT_PRESSED;
    955 					sysmon_task_queue_sched(0,
    956 					    kbd_powerbutton, k);
    957 #endif
    958 				return;
    959 		}
    960 	}
    961 
    962 	wskbd_input(k->k_wskbd, type, key);
    963 }
    964 
    965 int
    966 wssunkbd_enable(void *v, int on)
    967 {
    968 	struct kbd_softc *k = v;
    969 
    970 	if (k->k_wsenabled != on) {
    971 		k->k_wsenabled = on;
    972 		if (on) {
    973 			/* open actual underlying device */
    974 			if (k->k_ops != NULL && k->k_ops->open != NULL)
    975 				(*k->k_ops->open)(k);
    976 			ev_init(&k->k_events);
    977 			k->k_evmode = 0;	/* XXX: OK? */
    978 		} else {
    979 			/* close underlying device */
    980 			if (k->k_ops != NULL && k->k_ops->close != NULL)
    981 				(*k->k_ops->close)(k);
    982 		}
    983 	}
    984 	return 0;
    985 }
    986 
    987 void
    988 wssunkbd_set_leds(void *v, int leds)
    989 {
    990 	struct kbd_softc *k = v;
    991 	int l = 0;
    992 
    993 	if (leds & WSKBD_LED_CAPS)
    994 		l |= LED_CAPS_LOCK;
    995 	if (leds & WSKBD_LED_NUM)
    996 		l |= LED_NUM_LOCK;
    997 	if (leds & WSKBD_LED_SCROLL)
    998 		l |= LED_SCROLL_LOCK;
    999 	if (leds & WSKBD_LED_COMPOSE)
   1000 		l |= LED_COMPOSE;
   1001 	if (k->k_ops != NULL && k->k_ops->setleds != NULL)
   1002 		(*k->k_ops->setleds)(k, l, 0);
   1003 	k->k_leds=l;
   1004 }
   1005 
   1006 static int
   1007 wssunkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
   1008 {
   1009 	struct kbd_softc *k = v;
   1010 
   1011 	switch (cmd) {
   1012 		case WSKBDIO_GTYPE:
   1013 			/* we can't tell  4 from  5 or 6 */
   1014 			*(int *)data = k->k_state.kbd_id < KB_SUN4 ?
   1015 			    WSKBD_TYPE_SUN : WSKBD_TYPE_SUN5;
   1016 			return 0;
   1017 		case WSKBDIO_SETLEDS:
   1018 			wssunkbd_set_leds(v, *(int *)data);
   1019 			return 0;
   1020 		case WSKBDIO_GETLEDS:
   1021 			*(int *)data = k->k_leds;
   1022 			return 0;
   1023 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1024 		case WSKBDIO_SETMODE:
   1025 			k->k_wsraw = *(int *)data == WSKBD_RAW;
   1026 			return 0;
   1027 #endif
   1028 	}
   1029 	return EPASSTHROUGH;
   1030 }
   1031 
   1032 extern int	prom_cngetc(dev_t);
   1033 
   1034 static void
   1035 sunkbd_wskbd_cngetc(void *v, u_int *type, int *data)
   1036 {
   1037 	/* struct kbd_sun_softc *k = v; */
   1038 
   1039 	*data = prom_cngetc(0);
   1040 	*type = WSCONS_EVENT_ASCII;
   1041 }
   1042 
   1043 void
   1044 sunkbd_wskbd_cnpollc(void *v, int on)
   1045 {
   1046 }
   1047 
   1048 static void
   1049 sunkbd_bell_off(void *v)
   1050 {
   1051 	struct kbd_softc *k = v;
   1052 
   1053 	k->k_ops->docmd(k, KBD_CMD_NOBELL, 0);
   1054 }
   1055 
   1056 void
   1057 sunkbd_wskbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
   1058 {
   1059 	struct kbd_softc *k = v;
   1060 
   1061 	callout_reset(&k->k_wsbell, period * 1000 / hz, sunkbd_bell_off, v);
   1062 	k->k_ops->docmd(k, KBD_CMD_BELL, 0);
   1063 }
   1064 
   1065 void
   1066 kbd_enable(device_t dev)
   1067 {
   1068 	struct kbd_softc *k = device_private(dev);
   1069 	struct wskbddev_attach_args a;
   1070 
   1071 	if (k->k_isconsole)
   1072 		wskbd_cnattach(&sunkbd_wskbd_consops, k,
   1073 		    &sunkbd_wskbd_keymapdata);
   1074 
   1075 	a.console = k->k_isconsole;
   1076 	a.keymap = &sunkbd_wskbd_keymapdata;
   1077 	a.accessops = &sunkbd_wskbd_accessops;
   1078 	a.accesscookie = k;
   1079 
   1080 	/* XXX why? */
   1081 	k->k_wsenabled = 0;
   1082 
   1083 	/* Attach the wskbd */
   1084 	k->k_wskbd = config_found(k->k_dev, &a, wskbddevprint);
   1085 
   1086 	callout_init(&k->k_wsbell, 0);
   1087 
   1088 	wssunkbd_enable(k,1);
   1089 
   1090 	wssunkbd_set_leds(k, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
   1091 	delay(100000);
   1092 	wssunkbd_set_leds(k, 0);
   1093 }
   1094 
   1095 void
   1096 kbd_wskbd_attach(struct kbd_softc *k, int isconsole)
   1097 {
   1098 	k->k_isconsole = isconsole;
   1099 	if (isconsole) {
   1100 #if NSYSMON_ENVSYS
   1101 		sysmon_task_queue_init();
   1102 		memset(&k->k_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
   1103 		k->k_sm_pbutton.smpsw_name = device_xname(k->k_dev);
   1104 		k->k_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
   1105 		if (sysmon_pswitch_register(&k->k_sm_pbutton) != 0)
   1106 			aprint_error_dev(k->k_dev,
   1107 			    "unable to register power button with sysmon\n");
   1108 #endif
   1109 	}
   1110 	config_interrupts(k->k_dev, kbd_enable);
   1111 }
   1112 #endif
   1113