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