Home | History | Annotate | Line # | Download | only in sun
kbd.c revision 1.33
      1  1.33      uwe /*	$NetBSD: kbd.c,v 1.33 2002/10/21 15:36:35 uwe Exp $	*/
      2   1.1      gwr 
      3   1.1      gwr /*
      4   1.1      gwr  * Copyright (c) 1992, 1993
      5   1.1      gwr  *	The Regents of the University of California.  All rights reserved.
      6   1.1      gwr  *
      7   1.1      gwr  * This software was developed by the Computer Systems Engineering group
      8   1.1      gwr  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9   1.1      gwr  * contributed to Berkeley.
     10   1.1      gwr  *
     11   1.1      gwr  * All advertising materials mentioning features or use of this software
     12   1.1      gwr  * must display the following acknowledgement:
     13   1.1      gwr  *	This product includes software developed by the University of
     14   1.1      gwr  *	California, Lawrence Berkeley Laboratory.
     15   1.1      gwr  *
     16   1.1      gwr  * Redistribution and use in source and binary forms, with or without
     17   1.1      gwr  * modification, are permitted provided that the following conditions
     18   1.1      gwr  * are met:
     19   1.1      gwr  * 1. Redistributions of source code must retain the above copyright
     20   1.1      gwr  *    notice, this list of conditions and the following disclaimer.
     21   1.1      gwr  * 2. Redistributions in binary form must reproduce the above copyright
     22   1.1      gwr  *    notice, this list of conditions and the following disclaimer in the
     23   1.1      gwr  *    documentation and/or other materials provided with the distribution.
     24   1.1      gwr  * 3. All advertising materials mentioning features or use of this software
     25   1.1      gwr  *    must display the following acknowledgement:
     26   1.1      gwr  *	This product includes software developed by the University of
     27   1.1      gwr  *	California, Berkeley and its contributors.
     28   1.1      gwr  * 4. Neither the name of the University nor the names of its contributors
     29   1.1      gwr  *    may be used to endorse or promote products derived from this software
     30   1.1      gwr  *    without specific prior written permission.
     31   1.1      gwr  *
     32   1.1      gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33   1.1      gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34   1.1      gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35   1.1      gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36   1.1      gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37   1.1      gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38   1.1      gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39   1.1      gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40   1.1      gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41   1.1      gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42   1.1      gwr  * SUCH DAMAGE.
     43   1.1      gwr  *
     44   1.1      gwr  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     45   1.1      gwr  */
     46   1.1      gwr 
     47   1.1      gwr /*
     48   1.1      gwr  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
     49   1.1      gwr  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
     50   1.1      gwr  * passes them up to the appropriate reader.
     51   1.1      gwr  */
     52  1.29    lukem 
     53  1.29    lukem #include <sys/cdefs.h>
     54  1.33      uwe __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.33 2002/10/21 15:36:35 uwe Exp $");
     55   1.1      gwr 
     56   1.1      gwr #include <sys/param.h>
     57   1.1      gwr #include <sys/systm.h>
     58  1.13      gwr #include <sys/conf.h>
     59   1.1      gwr #include <sys/device.h>
     60   1.1      gwr #include <sys/ioctl.h>
     61  1.13      gwr #include <sys/kernel.h>
     62  1.13      gwr #include <sys/proc.h>
     63  1.33      uwe #include <sys/malloc.h>
     64  1.13      gwr #include <sys/signal.h>
     65  1.13      gwr #include <sys/signalvar.h>
     66   1.1      gwr #include <sys/time.h>
     67   1.1      gwr #include <sys/syslog.h>
     68   1.9      mrg #include <sys/select.h>
     69   1.9      mrg #include <sys/poll.h>
     70  1.27      eeh #include <sys/file.h>
     71   1.1      gwr 
     72  1.33      uwe #include <dev/sun/kbd_reg.h>
     73  1.33      uwe #include <dev/sun/kbio.h>
     74  1.33      uwe #include <dev/sun/vuid_event.h>
     75  1.22      mrg #include <dev/sun/event_var.h>
     76  1.22      mrg #include <dev/sun/kbd_xlate.h>
     77  1.22      mrg #include <dev/sun/kbdvar.h>
     78   1.1      gwr 
     79  1.14      jtk #include "locators.h"
     80   1.1      gwr 
     81  1.31  gehenna extern struct cfdriver kbd_cd;
     82   1.1      gwr 
     83  1.31  gehenna dev_type_open(kbdopen);
     84  1.31  gehenna dev_type_close(kbdclose);
     85  1.31  gehenna dev_type_read(kbdread);
     86  1.31  gehenna dev_type_ioctl(kbdioctl);
     87  1.31  gehenna dev_type_poll(kbdpoll);
     88  1.31  gehenna 
     89  1.31  gehenna const struct cdevsw kbd_cdevsw = {
     90  1.31  gehenna 	kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
     91  1.31  gehenna 	nostop, notty, kbdpoll, nommap,
     92  1.31  gehenna };
     93   1.1      gwr 
     94  1.32      uwe 
     95  1.32      uwe /* ioctl helpers */
     96  1.32      uwe static int kbd_iockeymap(struct kbd_state *ks,
     97  1.32      uwe 			 u_long cmd, struct kiockeymap *kio);
     98  1.32      uwe #ifdef KIOCGETKEY
     99  1.32      uwe static int kbd_oldkeymap(struct kbd_state *ks,
    100  1.32      uwe 			 u_long cmd, struct okiockey *okio);
    101  1.32      uwe #endif
    102  1.32      uwe 
    103  1.33      uwe 
    104  1.33      uwe /* callbacks for console driver */
    105  1.33      uwe static int kbd_cc_open(struct cons_channel *);
    106  1.33      uwe static int kbd_cc_close(struct cons_channel *);
    107  1.33      uwe 
    108  1.33      uwe /* console input */
    109  1.33      uwe static void	kbd_input_console(struct kbd_softc *, int);
    110  1.33      uwe static void	kbd_repeat(void *);
    111  1.33      uwe static int	kbd_input_keysym(struct kbd_softc *, int);
    112  1.33      uwe static void	kbd_input_string(struct kbd_softc *, char *);
    113  1.33      uwe static void	kbd_input_funckey(struct kbd_softc *, int);
    114  1.33      uwe static void	kbd_update_leds(struct kbd_softc *);
    115  1.33      uwe 
    116  1.33      uwe /* firm events input */
    117  1.33      uwe static void	kbd_input_event(struct kbd_softc *, int);
    118  1.33      uwe 
    119  1.32      uwe 
    120  1.32      uwe 
    121   1.1      gwr /****************************************************************
    122   1.1      gwr  *  Entry points for /dev/kbd
    123   1.1      gwr  *  (open,close,read,write,...)
    124   1.1      gwr  ****************************************************************/
    125   1.1      gwr 
    126   1.1      gwr /*
    127   1.1      gwr  * Open:
    128   1.1      gwr  * Check exclusion, open actual device (_iopen),
    129   1.1      gwr  * setup event channel, clear ASCII repeat stuff.
    130   1.1      gwr  */
    131   1.1      gwr int
    132   1.1      gwr kbdopen(dev, flags, mode, p)
    133   1.1      gwr 	dev_t dev;
    134   1.1      gwr 	int flags, mode;
    135   1.1      gwr 	struct proc *p;
    136   1.1      gwr {
    137   1.1      gwr 	struct kbd_softc *k;
    138  1.13      gwr 	int error, unit;
    139   1.1      gwr 
    140  1.32      uwe 	/* locate device */
    141   1.1      gwr 	unit = minor(dev);
    142   1.5  thorpej 	if (unit >= kbd_cd.cd_ndevs)
    143   1.1      gwr 		return (ENXIO);
    144   1.5  thorpej 	k = kbd_cd.cd_devs[unit];
    145   1.1      gwr 	if (k == NULL)
    146   1.1      gwr 		return (ENXIO);
    147   1.1      gwr 
    148  1.33      uwe 	/*
    149  1.33      uwe 	 * NB: wscons support: while we can track if wskbd has called
    150  1.33      uwe 	 * enable(), we can't tell if that's for console input or for
    151  1.33      uwe 	 * events input, so we should probably just let the open to
    152  1.33      uwe 	 * always succeed regardless (e.g. Xsun opening /dev/kbd).
    153  1.33      uwe 	 */
    154  1.33      uwe 
    155  1.32      uwe 	/* exclusive open required for /dev/kbd */
    156   1.1      gwr 	if (k->k_events.ev_io)
    157   1.1      gwr 		return (EBUSY);
    158   1.1      gwr 	k->k_events.ev_io = p;
    159   1.1      gwr 
    160  1.33      uwe 	/* stop pending autorepeat of console input */
    161  1.33      uwe 	if (k->k_repeating) {
    162  1.33      uwe 		k->k_repeating = 0;
    163  1.33      uwe 		callout_stop(&k->k_repeat_ch);
    164  1.33      uwe 	}
    165  1.33      uwe 
    166  1.32      uwe 	/* open actual underlying device */
    167  1.32      uwe 	if (k->k_ops != NULL && k->k_ops->open != NULL)
    168  1.32      uwe 		if ((error = (*k->k_ops->open)(k)) != 0) {
    169  1.32      uwe 			k->k_events.ev_io = NULL;
    170  1.32      uwe 			return (error);
    171  1.32      uwe 		}
    172  1.32      uwe 
    173   1.1      gwr 	ev_init(&k->k_events);
    174  1.27      eeh 	k->k_evmode = 0;	/* XXX: OK? */
    175   1.1      gwr 
    176   1.1      gwr 	return (0);
    177   1.1      gwr }
    178   1.1      gwr 
    179  1.32      uwe 
    180   1.1      gwr /*
    181   1.1      gwr  * Close:
    182   1.1      gwr  * Turn off event mode, dump the queue, and close the keyboard
    183   1.1      gwr  * unless it is supplying console input.
    184   1.1      gwr  */
    185   1.1      gwr int
    186   1.1      gwr kbdclose(dev, flags, mode, p)
    187   1.1      gwr 	dev_t dev;
    188   1.1      gwr 	int flags, mode;
    189   1.1      gwr 	struct proc *p;
    190   1.1      gwr {
    191   1.1      gwr 	struct kbd_softc *k;
    192   1.1      gwr 
    193   1.5  thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    194   1.1      gwr 	k->k_evmode = 0;
    195   1.1      gwr 	ev_fini(&k->k_events);
    196   1.1      gwr 	k->k_events.ev_io = NULL;
    197  1.32      uwe 
    198  1.32      uwe 	if (k->k_ops != NULL && k->k_ops->close != NULL) {
    199  1.32      uwe 		int error;
    200  1.32      uwe 		if ((error = (*k->k_ops->close)(k)) != 0)
    201  1.32      uwe 			return (error);
    202  1.32      uwe 	}
    203   1.1      gwr 	return (0);
    204   1.1      gwr }
    205   1.1      gwr 
    206  1.32      uwe 
    207   1.1      gwr int
    208   1.1      gwr kbdread(dev, uio, flags)
    209   1.1      gwr 	dev_t dev;
    210   1.1      gwr 	struct uio *uio;
    211   1.1      gwr 	int flags;
    212   1.1      gwr {
    213   1.1      gwr 	struct kbd_softc *k;
    214   1.1      gwr 
    215   1.5  thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    216   1.1      gwr 	return (ev_read(&k->k_events, uio, flags));
    217   1.1      gwr }
    218   1.1      gwr 
    219  1.32      uwe 
    220   1.1      gwr int
    221   1.9      mrg kbdpoll(dev, events, p)
    222   1.1      gwr 	dev_t dev;
    223   1.9      mrg 	int events;
    224   1.1      gwr 	struct proc *p;
    225   1.1      gwr {
    226   1.1      gwr 	struct kbd_softc *k;
    227   1.1      gwr 
    228   1.5  thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    229   1.9      mrg 	return (ev_poll(&k->k_events, events, p));
    230   1.1      gwr }
    231   1.1      gwr 
    232   1.1      gwr 
    233   1.1      gwr int
    234   1.1      gwr kbdioctl(dev, cmd, data, flag, p)
    235   1.1      gwr 	dev_t dev;
    236   1.1      gwr 	u_long cmd;
    237  1.23       pk 	caddr_t data;
    238   1.1      gwr 	int flag;
    239   1.1      gwr 	struct proc *p;
    240   1.1      gwr {
    241   1.1      gwr 	struct kbd_softc *k;
    242   1.1      gwr 	struct kbd_state *ks;
    243   1.1      gwr 	int error = 0;
    244   1.1      gwr 
    245   1.5  thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    246   1.1      gwr 	ks = &k->k_state;
    247   1.1      gwr 
    248   1.1      gwr 	switch (cmd) {
    249   1.1      gwr 
    250   1.1      gwr 	case KIOCTRANS: 	/* Set translation mode */
    251   1.1      gwr 		/* We only support "raw" mode on /dev/kbd */
    252  1.16      gwr 		if (*(int *)data != TR_UNTRANS_EVENT)
    253   1.1      gwr 			error = EINVAL;
    254   1.1      gwr 		break;
    255   1.1      gwr 
    256   1.1      gwr 	case KIOCGTRANS:	/* Get translation mode */
    257   1.1      gwr 		/* We only support "raw" mode on /dev/kbd */
    258  1.16      gwr 		*(int *)data = TR_UNTRANS_EVENT;
    259   1.1      gwr 		break;
    260   1.1      gwr 
    261  1.32      uwe #ifdef KIOCGETKEY
    262   1.1      gwr 	case KIOCGETKEY:	/* Get keymap entry (old format) */
    263   1.1      gwr 		error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
    264   1.1      gwr 		break;
    265  1.32      uwe #endif /* KIOCGETKEY */
    266   1.1      gwr 
    267   1.1      gwr 	case KIOCSKEY:  	/* Set keymap entry */
    268  1.32      uwe 		/* FALLTHROUGH */
    269   1.1      gwr 	case KIOCGKEY:  	/* Get keymap entry */
    270   1.1      gwr 		error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
    271   1.1      gwr 		break;
    272   1.1      gwr 
    273  1.32      uwe 	case KIOCCMD:		/* Send a command to the keyboard */
    274  1.32      uwe 		/* pass it to the middle layer */
    275  1.32      uwe 		if (k->k_ops != NULL && k->k_ops->docmd != NULL)
    276  1.32      uwe 			error = (*k->k_ops->docmd)(k, *(int *)data, 1);
    277   1.1      gwr 		break;
    278   1.1      gwr 
    279  1.32      uwe 	case KIOCTYPE:		/* Get keyboard type */
    280  1.16      gwr 		*(int *)data = ks->kbd_id;
    281   1.1      gwr 		break;
    282   1.1      gwr 
    283  1.32      uwe 	case KIOCSDIRECT:	/* Where to send input */
    284  1.16      gwr 		k->k_evmode = *(int *)data;
    285   1.1      gwr 		break;
    286   1.1      gwr 
    287   1.1      gwr 	case KIOCLAYOUT:	/* Get keyboard layout */
    288  1.16      gwr 		*(int *)data = ks->kbd_layout;
    289   1.1      gwr 		break;
    290   1.1      gwr 
    291  1.32      uwe 	case KIOCSLED:		/* Set keyboard LEDs */
    292  1.32      uwe 		/* pass the request to the middle layer */
    293  1.32      uwe 		if (k->k_ops != NULL && k->k_ops->setleds != NULL)
    294  1.32      uwe 			error = (*k->k_ops->setleds)(k, *(char *)data, 1);
    295   1.1      gwr 		break;
    296   1.1      gwr 
    297  1.32      uwe 	case KIOCGLED:		/* Get keyboard LEDs */
    298   1.1      gwr 		*(char *)data = ks->kbd_leds;
    299   1.1      gwr 		break;
    300   1.1      gwr 
    301   1.1      gwr 	case FIONBIO:		/* we will remove this someday (soon???) */
    302   1.1      gwr 		break;
    303   1.1      gwr 
    304   1.1      gwr 	case FIOASYNC:
    305  1.32      uwe 		k->k_events.ev_async = (*(int *)data != 0);
    306   1.1      gwr 		break;
    307   1.1      gwr 
    308   1.1      gwr 	case TIOCSPGRP:
    309  1.16      gwr 		if (*(int *)data != k->k_events.ev_io->p_pgid)
    310   1.1      gwr 			error = EPERM;
    311   1.1      gwr 		break;
    312   1.1      gwr 
    313  1.16      gwr 	default:
    314  1.16      gwr 		error = ENOTTY;
    315  1.16      gwr 		break;
    316   1.1      gwr 	}
    317   1.1      gwr 
    318   1.1      gwr 	return (error);
    319   1.1      gwr }
    320   1.1      gwr 
    321  1.32      uwe 
    322   1.1      gwr /****************************************************************
    323   1.1      gwr  * ioctl helpers
    324   1.1      gwr  ****************************************************************/
    325   1.1      gwr 
    326   1.1      gwr /*
    327   1.1      gwr  * Get/Set keymap entry
    328   1.1      gwr  */
    329   1.7      gwr static int
    330   1.1      gwr kbd_iockeymap(ks, cmd, kio)
    331   1.1      gwr 	struct kbd_state *ks;
    332   1.1      gwr 	u_long cmd;
    333   1.1      gwr 	struct kiockeymap *kio;
    334   1.1      gwr {
    335  1.13      gwr 	u_short *km;
    336   1.1      gwr 	u_int station;
    337   1.1      gwr 
    338   1.1      gwr 	switch (kio->kio_tablemask) {
    339   1.1      gwr 	case KIOC_NOMASK:
    340   1.1      gwr 		km = ks->kbd_k.k_normal;
    341   1.1      gwr 		break;
    342   1.1      gwr 	case KIOC_SHIFTMASK:
    343   1.1      gwr 		km = ks->kbd_k.k_shifted;
    344   1.1      gwr 		break;
    345   1.1      gwr 	case KIOC_CTRLMASK:
    346   1.1      gwr 		km = ks->kbd_k.k_control;
    347   1.1      gwr 		break;
    348   1.1      gwr 	case KIOC_UPMASK:
    349   1.1      gwr 		km = ks->kbd_k.k_release;
    350   1.1      gwr 		break;
    351   1.1      gwr 	default:
    352   1.1      gwr 		/* Silently ignore unsupported masks */
    353   1.1      gwr 		return (0);
    354   1.1      gwr 	}
    355   1.1      gwr 
    356   1.1      gwr 	/* Range-check the table position. */
    357   1.1      gwr 	station = kio->kio_station;
    358   1.1      gwr 	if (station >= KEYMAP_SIZE)
    359   1.1      gwr 		return (EINVAL);
    360   1.1      gwr 
    361   1.1      gwr 	switch (cmd) {
    362   1.1      gwr 
    363   1.1      gwr 	case KIOCGKEY:	/* Get keymap entry */
    364  1.13      gwr 		kio->kio_entry = km[station];
    365   1.1      gwr 		break;
    366   1.1      gwr 
    367   1.1      gwr 	case KIOCSKEY:	/* Set keymap entry */
    368  1.13      gwr 		km[station] = kio->kio_entry;
    369   1.1      gwr 		break;
    370   1.1      gwr 
    371   1.1      gwr 	default:
    372   1.1      gwr 		return(ENOTTY);
    373   1.1      gwr 	}
    374   1.1      gwr 	return (0);
    375   1.1      gwr }
    376   1.1      gwr 
    377  1.32      uwe 
    378  1.32      uwe #ifdef KIOCGETKEY
    379   1.1      gwr /*
    380   1.1      gwr  * Get/Set keymap entry,
    381   1.1      gwr  * old format (compatibility)
    382   1.1      gwr  */
    383   1.1      gwr int
    384   1.1      gwr kbd_oldkeymap(ks, cmd, kio)
    385   1.1      gwr 	struct kbd_state *ks;
    386   1.1      gwr 	u_long cmd;
    387   1.1      gwr 	struct okiockey *kio;
    388   1.1      gwr {
    389   1.1      gwr 	int error = 0;
    390   1.1      gwr 
    391   1.1      gwr 	switch (cmd) {
    392   1.1      gwr 
    393   1.1      gwr 	case KIOCGETKEY:
    394   1.1      gwr 		if (kio->kio_station == 118) {
    395   1.1      gwr 			/*
    396   1.1      gwr 			 * This is X11 asking if a type 3 keyboard is
    397   1.1      gwr 			 * really a type 3 keyboard.  Say yes, it is,
    398   1.1      gwr 			 * by reporting key station 118 as a "hole".
    399   1.1      gwr 			 * Note old (SunOS 3.5) definition of HOLE!
    400   1.1      gwr 			 */
    401   1.1      gwr 			kio->kio_entry = 0xA2;
    402   1.1      gwr 			break;
    403   1.1      gwr 		}
    404   1.1      gwr 		/* fall through */
    405   1.1      gwr 
    406   1.1      gwr 	default:
    407   1.1      gwr 		error = ENOTTY;
    408   1.1      gwr 		break;
    409   1.1      gwr 	}
    410   1.1      gwr 
    411   1.1      gwr 	return (error);
    412   1.1      gwr }
    413  1.32      uwe #endif /* KIOCGETKEY */
    414   1.1      gwr 
    415   1.7      gwr 
    416  1.33      uwe 
    417  1.32      uwe /****************************************************************
    418  1.33      uwe  *  Keyboard input - called by middle layer at spltty().
    419  1.32      uwe  ****************************************************************/
    420  1.32      uwe 
    421  1.33      uwe void
    422  1.33      uwe kbd_input(k, code)
    423  1.33      uwe 	struct kbd_softc *k;
    424  1.33      uwe 	int code;
    425  1.33      uwe {
    426  1.33      uwe 	/*
    427  1.33      uwe 	 * TODO: wscons support: check if attached wskbd has called
    428  1.33      uwe 	 * enable() and pass input to wskbd_input() if it has.  But
    429  1.33      uwe 	 * check for k_evmode first(!) - see the comment in kbdopen().
    430  1.33      uwe 	 */
    431  1.33      uwe 
    432  1.33      uwe 	/*
    433  1.33      uwe 	 * If /dev/kbd is not connected in event mode,
    434  1.33      uwe 	 * translate and send upstream (to console).
    435  1.33      uwe 	 */
    436  1.33      uwe 	if (!k->k_evmode) {
    437  1.33      uwe 		kbd_input_console(k, code);
    438  1.33      uwe 		return;
    439  1.33      uwe 	}
    440  1.33      uwe 
    441  1.33      uwe 	/*
    442  1.33      uwe 	 * XXX: is this still true?
    443  1.33      uwe 	 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
    444  1.33      uwe 	 * This is bad as it means the server will not automatically resync
    445  1.33      uwe 	 * on all-up IDLEs, but I did not drop them before, and the server
    446  1.33      uwe 	 * goes crazy when it comes time to blank the screen....
    447  1.33      uwe 	 */
    448  1.33      uwe 	if (code == KBD_IDLE)
    449  1.33      uwe 		return;
    450  1.33      uwe 
    451  1.33      uwe 	/*
    452  1.33      uwe 	 * Keyboard is generating firm events.  Turn this keystroke
    453  1.33      uwe 	 * into an event and put it in the queue.
    454  1.33      uwe 	 */
    455  1.33      uwe 	kbd_input_event(k, code);
    456  1.33      uwe }
    457  1.33      uwe 
    458  1.33      uwe 
    459  1.33      uwe 
    460  1.33      uwe /****************************************************************
    461  1.33      uwe  *  Open/close routines called upon opening /dev/console
    462  1.33      uwe  *  if we serve console input.
    463  1.33      uwe  ****************************************************************/
    464  1.33      uwe 
    465  1.33      uwe struct cons_channel *
    466  1.33      uwe kbd_cc_alloc(k)
    467  1.33      uwe 	struct kbd_softc *k;
    468  1.33      uwe {
    469  1.33      uwe 	struct cons_channel *cc;
    470  1.33      uwe 
    471  1.33      uwe 	if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
    472  1.33      uwe 		return (NULL);
    473  1.33      uwe 
    474  1.33      uwe 	/* our callbacks for the console driver */
    475  1.33      uwe 	cc->cc_dev = k;
    476  1.33      uwe 	cc->cc_iopen = kbd_cc_open;
    477  1.33      uwe 	cc->cc_iclose = kbd_cc_close;
    478  1.33      uwe 
    479  1.33      uwe 	/* will be provided by the console driver so that we can feed input */
    480  1.33      uwe 	cc->cc_upstream = NULL;
    481  1.33      uwe 
    482  1.33      uwe 	/*
    483  1.33      uwe 	 * TODO: clean up cons_attach_input() vs kd_attach_input() in
    484  1.33      uwe 	 * lower layers and move that code here.
    485  1.33      uwe 	 */
    486  1.33      uwe 
    487  1.33      uwe 	k->k_cc = cc;
    488  1.33      uwe 	return (cc);
    489  1.33      uwe }
    490  1.33      uwe 
    491  1.33      uwe 
    492  1.33      uwe static int
    493  1.32      uwe kbd_cc_open(cc)
    494  1.32      uwe 	struct cons_channel *cc;
    495  1.15      gwr {
    496   1.7      gwr 	struct kbd_softc *k;
    497  1.33      uwe 	int ret;
    498  1.15      gwr 
    499  1.32      uwe 	if (cc == NULL)
    500  1.33      uwe 		return (0);
    501   1.7      gwr 
    502  1.32      uwe 	k = (struct kbd_softc *)cc->cc_dev;
    503  1.33      uwe 	if (k == NULL)
    504  1.33      uwe 		return (0);
    505  1.33      uwe 
    506  1.33      uwe 	if (k->k_ops != NULL && k->k_ops->open != NULL)
    507  1.33      uwe 		ret = (*k->k_ops->open)(k);
    508  1.32      uwe 	else
    509  1.33      uwe 		ret = 0;
    510  1.33      uwe 
    511  1.33      uwe 	/* XXX: verify that callout is not active? */
    512  1.33      uwe 	k->k_repeat_start = hz/2;
    513  1.33      uwe 	k->k_repeat_step = hz/20;
    514  1.33      uwe 	callout_init(&k->k_repeat_ch);
    515  1.33      uwe 
    516  1.33      uwe 	return (ret);
    517  1.32      uwe }
    518   1.7      gwr 
    519   1.7      gwr 
    520  1.33      uwe static int
    521  1.32      uwe kbd_cc_close(cc)
    522  1.32      uwe 	struct cons_channel *cc;
    523  1.32      uwe {
    524   1.7      gwr 	struct kbd_softc *k;
    525  1.33      uwe 	int ret;
    526   1.7      gwr 
    527  1.32      uwe 	if (cc == NULL)
    528  1.33      uwe 		return (0);
    529   1.7      gwr 
    530  1.32      uwe 	k = (struct kbd_softc *)cc->cc_dev;
    531  1.33      uwe 	if (k == NULL)
    532  1.33      uwe 		return (0);
    533  1.33      uwe 
    534  1.33      uwe 	if (k->k_ops != NULL && k->k_ops->close != NULL)
    535  1.33      uwe 		ret = (*k->k_ops->close)(k);
    536  1.32      uwe 	else
    537  1.33      uwe 		ret = 0;
    538  1.33      uwe 
    539  1.33      uwe 	/* stop any pending auto-repeat */
    540  1.33      uwe 	if (k->k_repeating) {
    541  1.33      uwe 		k->k_repeating = 0;
    542  1.33      uwe 		callout_stop(&k->k_repeat_ch);
    543  1.33      uwe 	}
    544  1.33      uwe 
    545  1.33      uwe 	return (ret);
    546   1.7      gwr }
    547   1.7      gwr 
    548   1.7      gwr 
    549  1.33      uwe 
    550   1.1      gwr /****************************************************************
    551  1.32      uwe  *  Console input - called by middle layer at spltty().
    552   1.1      gwr  ****************************************************************/
    553   1.1      gwr 
    554  1.33      uwe static void
    555  1.33      uwe kbd_input_console(k, code)
    556  1.33      uwe 	struct kbd_softc *k;
    557  1.33      uwe 	int code;
    558  1.33      uwe {
    559  1.33      uwe 	struct kbd_state *ks= &k->k_state;
    560  1.33      uwe 	int keysym;
    561  1.33      uwe 
    562  1.33      uwe 	/* any input stops auto-repeat (i.e. key release) */
    563  1.33      uwe 	if (k->k_repeating) {
    564  1.33      uwe 		k->k_repeating = 0;
    565  1.33      uwe 		callout_stop(&k->k_repeat_ch);
    566  1.33      uwe 	}
    567  1.33      uwe 
    568  1.33      uwe 	keysym = kbd_code_to_keysym(ks, code);
    569  1.33      uwe 
    570  1.33      uwe 	/* pass to console */
    571  1.33      uwe 	if (kbd_input_keysym(k, keysym)) {
    572  1.33      uwe 		log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
    573  1.33      uwe 		    " produced unexpected keysym 0x%x\n",
    574  1.33      uwe 		    k->k_dev.dv_xname,
    575  1.33      uwe 		    code, ks->kbd_modbits, keysym);
    576  1.33      uwe 		return;		/* no point in auto-repeat here */
    577  1.33      uwe 	}
    578  1.33      uwe 
    579  1.33      uwe 	if (KEYSYM_NOREPEAT(keysym))
    580  1.33      uwe 		return;
    581  1.33      uwe 
    582  1.33      uwe 	/* setup for auto-repeat after initial delay */
    583  1.33      uwe 	k->k_repeating = 1;
    584  1.33      uwe 	k->k_repeatsym = keysym;
    585  1.33      uwe 	callout_reset(&k->k_repeat_ch, k->k_repeat_start,
    586  1.33      uwe 		      kbd_repeat, k);
    587  1.33      uwe }
    588  1.33      uwe 
    589  1.33      uwe 
    590  1.33      uwe /*
    591  1.33      uwe  * This is the autorepeat callout function scheduled by kbd_input() above.
    592  1.33      uwe  * Called at splsoftclock().
    593  1.33      uwe  */
    594  1.33      uwe static void
    595  1.33      uwe kbd_repeat(arg)
    596  1.33      uwe 	void *arg;
    597  1.33      uwe {
    598  1.33      uwe 	struct kbd_softc *k = (struct kbd_softc *)arg;
    599  1.33      uwe 	int s;
    600  1.33      uwe 
    601  1.33      uwe 	s = spltty();
    602  1.33      uwe 	if (k->k_repeating && k->k_repeatsym >= 0) {
    603  1.33      uwe 		/* feed typematic keysym to the console */
    604  1.33      uwe 		(void)kbd_input_keysym(k, k->k_repeatsym);
    605  1.33      uwe 
    606  1.33      uwe 		/* reschedule next repeat */
    607  1.33      uwe 		callout_reset(&k->k_repeat_ch, k->k_repeat_step,
    608  1.33      uwe 			      kbd_repeat, k);
    609  1.33      uwe 	}
    610  1.33      uwe 	splx(s);
    611  1.33      uwe }
    612  1.33      uwe 
    613  1.33      uwe 
    614  1.33      uwe 
    615   1.1      gwr /*
    616  1.33      uwe  * Supply keysym as console input.  Convert keysym to character(s) and
    617  1.33      uwe  * pass them up to cons_channel's upstream hook.
    618  1.17      gwr  *
    619  1.32      uwe  * Return zero on success, else the keysym that we could not handle
    620  1.32      uwe  * (so that the caller may complain).
    621   1.1      gwr  */
    622  1.33      uwe static int
    623   1.1      gwr kbd_input_keysym(k, keysym)
    624   1.1      gwr 	struct kbd_softc *k;
    625  1.23       pk 	int keysym;
    626   1.1      gwr {
    627   1.1      gwr 	struct kbd_state *ks = &k->k_state;
    628  1.23       pk 	int data;
    629  1.30       pk 
    630  1.30       pk 	/* Check if a recipient has been configured */
    631  1.32      uwe 	if (k->k_cc == NULL || k->k_cc->cc_upstream == NULL)
    632  1.30       pk 		return (0);
    633   1.1      gwr 
    634   1.4      gwr 	switch (KEYSYM_CLASS(keysym)) {
    635   1.1      gwr 
    636   1.1      gwr 	case KEYSYM_ASCII:
    637   1.1      gwr 		data = KEYSYM_DATA(keysym);
    638   1.1      gwr 		if (ks->kbd_modbits & KBMOD_META_MASK)
    639   1.1      gwr 			data |= 0x80;
    640  1.23       pk 		(*k->k_cc->cc_upstream)(data);
    641   1.1      gwr 		break;
    642   1.1      gwr 
    643   1.1      gwr 	case KEYSYM_STRING:
    644   1.1      gwr 		data = keysym & 0xF;
    645   1.1      gwr 		kbd_input_string(k, kbd_stringtab[data]);
    646   1.1      gwr 		break;
    647   1.1      gwr 
    648   1.1      gwr 	case KEYSYM_FUNC:
    649   1.1      gwr 		kbd_input_funckey(k, keysym);
    650   1.1      gwr 		break;
    651   1.1      gwr 
    652   1.1      gwr 	case KEYSYM_CLRMOD:
    653   1.1      gwr 		data = 1 << (keysym & 0x1F);
    654   1.1      gwr 		ks->kbd_modbits &= ~data;
    655   1.1      gwr 		break;
    656   1.1      gwr 
    657   1.1      gwr 	case KEYSYM_SETMOD:
    658   1.1      gwr 		data = 1 << (keysym & 0x1F);
    659   1.1      gwr 		ks->kbd_modbits |= data;
    660   1.1      gwr 		break;
    661   1.1      gwr 
    662   1.1      gwr 	case KEYSYM_INVMOD:
    663   1.1      gwr 		data = 1 << (keysym & 0x1F);
    664   1.1      gwr 		ks->kbd_modbits ^= data;
    665   1.4      gwr 		kbd_update_leds(k);
    666   1.1      gwr 		break;
    667   1.1      gwr 
    668   1.1      gwr 	case KEYSYM_ALL_UP:
    669   1.1      gwr 		ks->kbd_modbits &= ~0xFFFF;
    670   1.1      gwr 		break;
    671   1.1      gwr 
    672   1.1      gwr 	case KEYSYM_SPECIAL:
    673   1.1      gwr 		if (keysym == KEYSYM_NOP)
    674   1.1      gwr 			break;
    675  1.32      uwe 		/* FALLTHROUGH */
    676   1.1      gwr 	default:
    677  1.17      gwr 		/* We could not handle it. */
    678  1.17      gwr 		return (keysym);
    679   1.1      gwr 	}
    680  1.32      uwe 
    681  1.17      gwr 	return (0);
    682   1.1      gwr }
    683   1.1      gwr 
    684  1.32      uwe 
    685   1.1      gwr /*
    686  1.32      uwe  * Send string upstream.
    687   1.1      gwr  */
    688  1.13      gwr static void
    689  1.32      uwe kbd_input_string(k, str)
    690  1.32      uwe 	struct kbd_softc *k;
    691  1.32      uwe 	char *str;
    692   1.1      gwr {
    693   1.1      gwr 
    694  1.32      uwe 	while (*str) {
    695  1.32      uwe 		(*k->k_cc->cc_upstream)(*str);
    696  1.32      uwe 		++str;
    697   1.1      gwr 	}
    698   1.1      gwr }
    699   1.1      gwr 
    700  1.32      uwe 
    701   1.1      gwr /*
    702  1.32      uwe  * Format the F-key sequence and send as a string.
    703  1.32      uwe  * XXX: Ugly compatibility mappings.
    704   1.1      gwr  */
    705  1.32      uwe static void
    706  1.32      uwe kbd_input_funckey(k, keysym)
    707   1.1      gwr 	struct kbd_softc *k;
    708  1.32      uwe 	int keysym;
    709   1.1      gwr {
    710  1.32      uwe 	int n;
    711  1.32      uwe 	char str[12];
    712  1.32      uwe 
    713  1.32      uwe 	n = 0xC0 + (keysym & 0x3F);
    714  1.32      uwe 	sprintf(str, "\033[%dz", n);
    715  1.32      uwe 	kbd_input_string(k, str);
    716  1.32      uwe }
    717   1.1      gwr 
    718   1.1      gwr 
    719  1.32      uwe /*
    720  1.32      uwe  * Update LEDs to reflect console input state.
    721  1.32      uwe  */
    722  1.32      uwe static void
    723  1.32      uwe kbd_update_leds(k)
    724  1.32      uwe 	struct kbd_softc *k;
    725  1.32      uwe {
    726  1.32      uwe 	struct kbd_state *ks = &k->k_state;
    727  1.32      uwe 	char leds;
    728   1.1      gwr 
    729  1.32      uwe 	leds = ks->kbd_leds;
    730  1.32      uwe 	leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
    731   1.1      gwr 
    732  1.32      uwe 	if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
    733  1.32      uwe 		leds |= LED_CAPS_LOCK;
    734  1.32      uwe 	if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
    735  1.32      uwe 		leds |= LED_NUM_LOCK;
    736   1.1      gwr 
    737  1.32      uwe 	if (k->k_ops != NULL && k->k_ops->setleds != NULL)
    738  1.32      uwe 		(void)(*k->k_ops->setleds)(k, leds, 0);
    739  1.32      uwe }
    740   1.1      gwr 
    741   1.1      gwr 
    742   1.1      gwr 
    743  1.32      uwe /****************************************************************
    744  1.32      uwe  *  Events input - called by middle layer at spltty().
    745  1.32      uwe  ****************************************************************/
    746   1.1      gwr 
    747  1.32      uwe /*
    748  1.33      uwe  * Supply raw keystrokes when keyboard is open in firm event mode.
    749  1.32      uwe  *
    750  1.32      uwe  * Turn the keystroke into an event and put it in the queue.
    751  1.32      uwe  * If the queue is full, the keystroke is lost (sorry!).
    752  1.32      uwe  */
    753  1.33      uwe static void
    754  1.33      uwe kbd_input_event(k, code)
    755  1.32      uwe 	struct kbd_softc *k;
    756  1.33      uwe 	int code;
    757  1.32      uwe {
    758  1.32      uwe 	struct firm_event *fe;
    759  1.32      uwe 	int put;
    760   1.1      gwr 
    761  1.32      uwe #ifdef DIAGNOSTIC
    762  1.32      uwe 	if (!k->k_evmode) {
    763  1.32      uwe 		printf("%s: kbd_input_event called when not in event mode\n",
    764  1.32      uwe 		       k->k_dev.dv_xname);
    765   1.1      gwr 		return;
    766   1.1      gwr 	}
    767  1.32      uwe #endif
    768   1.1      gwr 	put = k->k_events.ev_put;
    769   1.1      gwr 	fe = &k->k_events.ev_q[put];
    770   1.1      gwr 	put = (put + 1) % EV_QSIZE;
    771   1.1      gwr 	if (put == k->k_events.ev_get) {
    772   1.1      gwr 		log(LOG_WARNING, "%s: event queue overflow\n",
    773  1.32      uwe 		    k->k_dev.dv_xname);
    774   1.1      gwr 		return;
    775   1.1      gwr 	}
    776  1.33      uwe 
    777  1.33      uwe 	fe->id = KEY_CODE(code);
    778  1.33      uwe 	fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
    779   1.1      gwr 	fe->time = time;
    780   1.1      gwr 	k->k_events.ev_put = put;
    781   1.1      gwr 	EV_WAKEUP(&k->k_events);
    782   1.1      gwr }
    783   1.1      gwr 
    784  1.32      uwe 
    785  1.33      uwe 
    786  1.32      uwe /****************************************************************
    787  1.32      uwe  *  Translation stuff declared in kbd_xlate.h
    788  1.32      uwe  ****************************************************************/
    789  1.24       pk 
    790  1.24       pk /*
    791  1.32      uwe  * Initialization - called by either lower layer attach or by kdcninit.
    792  1.24       pk  */
    793  1.32      uwe void
    794  1.32      uwe kbd_xlate_init(ks)
    795  1.32      uwe 	struct kbd_state *ks;
    796  1.24       pk {
    797  1.32      uwe 	struct keyboard *ktbls;
    798  1.32      uwe 	int id;
    799  1.32      uwe 
    800  1.32      uwe 	id = ks->kbd_id;
    801  1.32      uwe 	if (id < KBD_MIN_TYPE)
    802  1.32      uwe 		id = KBD_MIN_TYPE;
    803  1.32      uwe 	if (id > kbd_max_type)
    804  1.32      uwe 		id = kbd_max_type;
    805  1.32      uwe 	ktbls = keyboards[id];
    806  1.24       pk 
    807  1.32      uwe 	ks->kbd_k = *ktbls; 	/* struct assignment */
    808  1.32      uwe 	ks->kbd_modbits = 0;
    809  1.24       pk }
    810   1.1      gwr 
    811   1.1      gwr /*
    812  1.32      uwe  * Turn keyboard up/down codes into a KEYSYM.
    813  1.32      uwe  * Note that the "kd" driver (on sun3 and sparc64) uses this too!
    814   1.1      gwr  */
    815   1.1      gwr int
    816  1.32      uwe kbd_code_to_keysym(ks, c)
    817  1.32      uwe 	struct kbd_state *ks;
    818  1.32      uwe 	int c;
    819   1.1      gwr {
    820  1.32      uwe 	u_short *km;
    821  1.32      uwe 	int keysym;
    822   1.1      gwr 
    823  1.32      uwe 	/*
    824  1.32      uwe 	 * Get keymap pointer.  One of these:
    825  1.32      uwe 	 * release, control, shifted, normal, ...
    826  1.32      uwe 	 */
    827  1.32      uwe 	if (KEY_UP(c))
    828  1.32      uwe 		km = ks->kbd_k.k_release;
    829  1.32      uwe 	else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
    830  1.32      uwe 		km = ks->kbd_k.k_control;
    831  1.32      uwe 	else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
    832  1.32      uwe 		km = ks->kbd_k.k_shifted;
    833  1.32      uwe 	else
    834  1.32      uwe 		km = ks->kbd_k.k_normal;
    835  1.23       pk 
    836  1.32      uwe 	if (km == NULL) {
    837   1.1      gwr 		/*
    838  1.32      uwe 		 * Do not know how to translate yet.
    839  1.32      uwe 		 * We will find out when a RESET comes along.
    840   1.1      gwr 		 */
    841  1.32      uwe 		return (KEYSYM_NOP);
    842   1.1      gwr 	}
    843  1.32      uwe 	keysym = km[KEY_CODE(c)];
    844   1.1      gwr 
    845   1.1      gwr 	/*
    846  1.32      uwe 	 * Post-processing for Caps-lock
    847   1.1      gwr 	 */
    848  1.32      uwe 	if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
    849  1.32      uwe 		(KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
    850  1.32      uwe 	{
    851  1.32      uwe 		if (('a' <= keysym) && (keysym <= 'z'))
    852  1.32      uwe 			keysym -= ('a' - 'A');
    853   1.1      gwr 	}
    854   1.1      gwr 
    855   1.1      gwr 	/*
    856  1.32      uwe 	 * Post-processing for Num-lock.  All "function"
    857  1.32      uwe 	 * keysyms get indirected through another table.
    858  1.32      uwe 	 * (XXX: Only if numlock on.  Want off also!)
    859   1.1      gwr 	 */
    860  1.32      uwe 	if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
    861  1.32      uwe 		(KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
    862  1.32      uwe 	{
    863  1.32      uwe 		keysym = kbd_numlock_map[keysym & 0x3F];
    864   1.1      gwr 	}
    865   1.7      gwr 
    866  1.32      uwe 	return (keysym);
    867   1.1      gwr }
    868   1.1      gwr 
    869   1.1      gwr 
    870   1.7      gwr /*
    871  1.32      uwe  * Back door for rcons (fb.c)
    872   1.7      gwr  */
    873  1.22      mrg void
    874  1.32      uwe kbd_bell(on)
    875  1.32      uwe 	int on;
    876   1.1      gwr {
    877  1.33      uwe 	struct kbd_softc *k = kbd_cd.cd_devs[0]; /* XXX: hardcoded minor */
    878  1.33      uwe 
    879  1.33      uwe 	if (k == NULL || k->k_ops == NULL || k->k_ops->docmd == NULL)
    880  1.33      uwe 		return;
    881  1.33      uwe 
    882  1.33      uwe 	(void)(*k->k_ops->docmd)(k, on ? KBD_CMD_BELL : KBD_CMD_NOBELL, 0);
    883   1.1      gwr }
    884