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