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