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