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