Home | History | Annotate | Line # | Download | only in sun
kbd.c revision 1.28.4.1
      1  1.28.4.1      fvdl /*	$NetBSD: kbd.c,v 1.28.4.1 2001/10/10 11:57:02 fvdl Exp $	*/
      2       1.1       gwr 
      3       1.1       gwr /*
      4       1.1       gwr  * Copyright (c) 1992, 1993
      5       1.1       gwr  *	The Regents of the University of California.  All rights reserved.
      6       1.1       gwr  *
      7       1.1       gwr  * This software was developed by the Computer Systems Engineering group
      8       1.1       gwr  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9       1.1       gwr  * contributed to Berkeley.
     10       1.1       gwr  *
     11       1.1       gwr  * All advertising materials mentioning features or use of this software
     12       1.1       gwr  * must display the following acknowledgement:
     13       1.1       gwr  *	This product includes software developed by the University of
     14       1.1       gwr  *	California, Lawrence Berkeley Laboratory.
     15       1.1       gwr  *
     16       1.1       gwr  * Redistribution and use in source and binary forms, with or without
     17       1.1       gwr  * modification, are permitted provided that the following conditions
     18       1.1       gwr  * are met:
     19       1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     20       1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     21       1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     22       1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     23       1.1       gwr  *    documentation and/or other materials provided with the distribution.
     24       1.1       gwr  * 3. All advertising materials mentioning features or use of this software
     25       1.1       gwr  *    must display the following acknowledgement:
     26       1.1       gwr  *	This product includes software developed by the University of
     27       1.1       gwr  *	California, Berkeley and its contributors.
     28       1.1       gwr  * 4. Neither the name of the University nor the names of its contributors
     29       1.1       gwr  *    may be used to endorse or promote products derived from this software
     30       1.1       gwr  *    without specific prior written permission.
     31       1.1       gwr  *
     32       1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33       1.1       gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34       1.1       gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35       1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36       1.1       gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37       1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38       1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39       1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40       1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41       1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42       1.1       gwr  * SUCH DAMAGE.
     43       1.1       gwr  *
     44       1.1       gwr  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     45       1.1       gwr  */
     46       1.1       gwr 
     47       1.1       gwr /*
     48       1.1       gwr  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
     49       1.1       gwr  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
     50       1.1       gwr  * passes them up to the appropriate reader.
     51       1.1       gwr  */
     52       1.1       gwr 
     53      1.27       eeh #include "opt_ddb.h"
     54      1.27       eeh 
     55       1.1       gwr /*
     56       1.1       gwr  * This is the "slave" driver that will be attached to
     57       1.1       gwr  * the "zsc" driver for a Sun keyboard.
     58       1.1       gwr  */
     59       1.1       gwr 
     60       1.1       gwr #include <sys/param.h>
     61       1.1       gwr #include <sys/systm.h>
     62      1.13       gwr #include <sys/conf.h>
     63       1.1       gwr #include <sys/device.h>
     64       1.1       gwr #include <sys/ioctl.h>
     65      1.13       gwr #include <sys/kernel.h>
     66      1.13       gwr #include <sys/proc.h>
     67      1.13       gwr #include <sys/signal.h>
     68      1.13       gwr #include <sys/signalvar.h>
     69       1.1       gwr #include <sys/time.h>
     70       1.1       gwr #include <sys/syslog.h>
     71       1.9       mrg #include <sys/select.h>
     72       1.9       mrg #include <sys/poll.h>
     73      1.27       eeh #include <sys/file.h>
     74  1.28.4.1      fvdl #include <sys/vnode.h>
     75       1.1       gwr 
     76       1.1       gwr #include <dev/ic/z8530reg.h>
     77       1.1       gwr #include <machine/z8530var.h>
     78       1.1       gwr #include <machine/vuid_event.h>
     79       1.1       gwr #include <machine/kbd.h>
     80       1.1       gwr #include <machine/kbio.h>
     81      1.22       mrg #include <dev/sun/event_var.h>
     82      1.22       mrg #include <dev/sun/kbd_xlate.h>
     83      1.22       mrg #include <dev/sun/kbdvar.h>
     84       1.1       gwr 
     85      1.14       jtk #include "locators.h"
     86       1.1       gwr 
     87       1.1       gwr /*
     88       1.1       gwr  * Ideas:
     89       1.1       gwr  * /dev/kbd is not a tty (plain device)
     90       1.1       gwr  */
     91       1.1       gwr 
     92       1.1       gwr /* Prototypes */
     93      1.24        pk static void	kbd_new_layout __P((struct kbd_softc *));
     94      1.24        pk static void	kbd_repeat __P((void *));
     95      1.24        pk static void	kbd_set_leds __P((struct kbd_softc *, int));
     96      1.24        pk static void	kbd_update_leds __P((struct kbd_softc *));
     97      1.24        pk static void	kbd_was_reset __P((struct kbd_softc *));
     98      1.24        pk static int 	kbd_drain_tx __P((struct kbd_softc *));
     99      1.24        pk static int	kbd_iopen __P((struct kbd_softc *));
    100      1.24        pk static int	kbd_iclose __P((struct kbd_softc *));
    101       1.1       gwr 
    102       1.1       gwr cdev_decl(kbd);	/* open, close, read, write, ioctl, stop, ... */
    103       1.1       gwr 
    104      1.20   thorpej extern struct cfdriver kbd_cd;
    105       1.1       gwr 
    106       1.1       gwr /****************************************************************
    107       1.1       gwr  *  Entry points for /dev/kbd
    108       1.1       gwr  *  (open,close,read,write,...)
    109       1.1       gwr  ****************************************************************/
    110       1.1       gwr 
    111       1.1       gwr /*
    112       1.1       gwr  * Open:
    113       1.1       gwr  * Check exclusion, open actual device (_iopen),
    114       1.1       gwr  * setup event channel, clear ASCII repeat stuff.
    115       1.1       gwr  */
    116       1.1       gwr int
    117  1.28.4.1      fvdl kbdopen(devvp, flags, mode, p)
    118  1.28.4.1      fvdl 	struct vnode *devvp;
    119       1.1       gwr 	int flags, mode;
    120       1.1       gwr 	struct proc *p;
    121       1.1       gwr {
    122  1.28.4.1      fvdl 	dev_t dev = vdev_rdev(devvp);
    123       1.1       gwr 	struct kbd_softc *k;
    124      1.13       gwr 	int error, unit;
    125       1.1       gwr 
    126       1.1       gwr 	unit = minor(dev);
    127       1.5   thorpej 	if (unit >= kbd_cd.cd_ndevs)
    128       1.1       gwr 		return (ENXIO);
    129       1.5   thorpej 	k = kbd_cd.cd_devs[unit];
    130       1.1       gwr 	if (k == NULL)
    131       1.1       gwr 		return (ENXIO);
    132       1.1       gwr 
    133       1.1       gwr 	/* Exclusive open required for /dev/kbd */
    134       1.1       gwr 	if (k->k_events.ev_io)
    135       1.1       gwr 		return (EBUSY);
    136       1.1       gwr 	k->k_events.ev_io = p;
    137       1.1       gwr 
    138  1.28.4.1      fvdl 	vdev_setprivdata(devvp, k);
    139  1.28.4.1      fvdl 
    140      1.24        pk 	if ((error = kbd_iopen(k)) != 0) {
    141       1.1       gwr 		k->k_events.ev_io = NULL;
    142       1.1       gwr 		return (error);
    143       1.1       gwr 	}
    144       1.1       gwr 	ev_init(&k->k_events);
    145      1.27       eeh 	k->k_evmode = 0;	/* XXX: OK? */
    146       1.1       gwr 
    147       1.1       gwr 	if (k->k_repeating) {
    148       1.1       gwr 		k->k_repeating = 0;
    149      1.25   thorpej 		callout_stop(&k->k_repeat_ch);
    150       1.1       gwr 	}
    151       1.1       gwr 
    152       1.1       gwr 	return (0);
    153       1.1       gwr }
    154       1.1       gwr 
    155       1.1       gwr /*
    156       1.1       gwr  * Close:
    157       1.1       gwr  * Turn off event mode, dump the queue, and close the keyboard
    158       1.1       gwr  * unless it is supplying console input.
    159       1.1       gwr  */
    160       1.1       gwr int
    161  1.28.4.1      fvdl kbdclose(devvp, flags, mode, p)
    162  1.28.4.1      fvdl 	struct vnode *devvp;
    163       1.1       gwr 	int flags, mode;
    164       1.1       gwr 	struct proc *p;
    165       1.1       gwr {
    166       1.1       gwr 	struct kbd_softc *k;
    167       1.1       gwr 
    168  1.28.4.1      fvdl 	k = vdev_privdata(devvp);
    169       1.1       gwr 	k->k_evmode = 0;
    170       1.1       gwr 	ev_fini(&k->k_events);
    171       1.1       gwr 	k->k_events.ev_io = NULL;
    172       1.1       gwr 	return (0);
    173       1.1       gwr }
    174       1.1       gwr 
    175       1.1       gwr int
    176  1.28.4.1      fvdl kbdread(devvp, uio, flags)
    177  1.28.4.1      fvdl 	struct vnode *devvp;
    178       1.1       gwr 	struct uio *uio;
    179       1.1       gwr 	int flags;
    180       1.1       gwr {
    181       1.1       gwr 	struct kbd_softc *k;
    182       1.1       gwr 
    183  1.28.4.1      fvdl 	k = vdev_privdata(devvp);
    184       1.1       gwr 	return (ev_read(&k->k_events, uio, flags));
    185       1.1       gwr }
    186       1.1       gwr 
    187       1.1       gwr /* this routine should not exist, but is convenient to write here for now */
    188       1.1       gwr int
    189  1.28.4.1      fvdl kbdwrite(devvp, uio, flags)
    190  1.28.4.1      fvdl 	struct vnode *devvp;
    191       1.1       gwr 	struct uio *uio;
    192       1.1       gwr 	int flags;
    193       1.1       gwr {
    194       1.1       gwr 
    195       1.1       gwr 	return (EOPNOTSUPP);
    196       1.1       gwr }
    197       1.1       gwr 
    198       1.1       gwr int
    199  1.28.4.1      fvdl kbdpoll(devvp, events, p)
    200  1.28.4.1      fvdl 	struct vnode *devvp;
    201       1.9       mrg 	int events;
    202       1.1       gwr 	struct proc *p;
    203       1.1       gwr {
    204       1.1       gwr 	struct kbd_softc *k;
    205       1.1       gwr 
    206  1.28.4.1      fvdl 	k = vdev_privdata(devvp);
    207       1.9       mrg 	return (ev_poll(&k->k_events, events, p));
    208       1.1       gwr }
    209       1.1       gwr 
    210       1.1       gwr 
    211       1.1       gwr static int kbd_iockeymap __P((struct kbd_state *ks,
    212       1.1       gwr 	u_long cmd, struct kiockeymap *kio));
    213       1.1       gwr 
    214      1.18       gwr static int kbd_iocsled(struct kbd_softc *k, char *data);
    215       1.7       gwr 
    216       1.7       gwr #ifdef	KIOCGETKEY
    217       1.7       gwr static int kbd_oldkeymap __P((struct kbd_state *ks,
    218       1.7       gwr 	u_long cmd, struct okiockey *okio));
    219       1.7       gwr #endif
    220       1.7       gwr 
    221       1.1       gwr int
    222  1.28.4.1      fvdl kbdioctl(devvp, cmd, data, flag, p)
    223  1.28.4.1      fvdl 	struct vnode *devvp;
    224       1.1       gwr 	u_long cmd;
    225      1.23        pk 	caddr_t data;
    226       1.1       gwr 	int flag;
    227       1.1       gwr 	struct proc *p;
    228       1.1       gwr {
    229       1.1       gwr 	struct kbd_softc *k;
    230       1.1       gwr 	struct kbd_state *ks;
    231       1.1       gwr 	int error = 0;
    232       1.1       gwr 
    233  1.28.4.1      fvdl 	k = vdev_privdata(devvp);
    234       1.1       gwr 	ks = &k->k_state;
    235       1.1       gwr 
    236       1.1       gwr 	switch (cmd) {
    237       1.1       gwr 
    238       1.1       gwr 	case KIOCTRANS: 	/* Set translation mode */
    239       1.1       gwr 		/* We only support "raw" mode on /dev/kbd */
    240      1.16       gwr 		if (*(int *)data != TR_UNTRANS_EVENT)
    241       1.1       gwr 			error = EINVAL;
    242       1.1       gwr 		break;
    243       1.1       gwr 
    244       1.1       gwr 	case KIOCGTRANS:	/* Get translation mode */
    245       1.1       gwr 		/* We only support "raw" mode on /dev/kbd */
    246      1.16       gwr 		*(int *)data = TR_UNTRANS_EVENT;
    247       1.1       gwr 		break;
    248       1.1       gwr 
    249       1.1       gwr #ifdef	KIOCGETKEY
    250       1.1       gwr 	case KIOCGETKEY:	/* Get keymap entry (old format) */
    251       1.1       gwr 		error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
    252       1.1       gwr 		break;
    253      1.28       mrg #endif	/* KIOCGETKEY */
    254       1.1       gwr 
    255       1.1       gwr 	case KIOCSKEY:  	/* Set keymap entry */
    256       1.1       gwr 		/* fallthrough */
    257       1.1       gwr 	case KIOCGKEY:  	/* Get keymap entry */
    258       1.1       gwr 		error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
    259       1.1       gwr 		break;
    260       1.1       gwr 
    261       1.1       gwr 	case KIOCCMD:	/* Send a command to the keyboard */
    262      1.16       gwr 		error = kbd_docmd(*(int *)data, 1);
    263       1.1       gwr 		break;
    264       1.1       gwr 
    265       1.1       gwr 	case KIOCTYPE:	/* Get keyboard type */
    266      1.16       gwr 		*(int *)data = ks->kbd_id;
    267       1.1       gwr 		break;
    268       1.1       gwr 
    269       1.1       gwr 	case KIOCSDIRECT:	/* where to send input */
    270      1.16       gwr 		k->k_evmode = *(int *)data;
    271       1.1       gwr 		break;
    272       1.1       gwr 
    273       1.1       gwr 	case KIOCLAYOUT:	/* Get keyboard layout */
    274      1.16       gwr 		*(int *)data = ks->kbd_layout;
    275       1.1       gwr 		break;
    276       1.1       gwr 
    277       1.1       gwr 	case KIOCSLED:
    278      1.18       gwr 		error = kbd_iocsled(k, (char *)data);
    279       1.1       gwr 		break;
    280       1.1       gwr 
    281       1.1       gwr 	case KIOCGLED:
    282       1.1       gwr 		*(char *)data = ks->kbd_leds;
    283       1.1       gwr 		break;
    284       1.1       gwr 
    285       1.1       gwr 	case FIONBIO:		/* we will remove this someday (soon???) */
    286       1.1       gwr 		break;
    287       1.1       gwr 
    288       1.1       gwr 	case FIOASYNC:
    289       1.1       gwr 		k->k_events.ev_async = *(int *)data != 0;
    290       1.1       gwr 		break;
    291       1.1       gwr 
    292       1.1       gwr 	case TIOCSPGRP:
    293      1.16       gwr 		if (*(int *)data != k->k_events.ev_io->p_pgid)
    294       1.1       gwr 			error = EPERM;
    295       1.1       gwr 		break;
    296       1.1       gwr 
    297      1.16       gwr 	default:
    298      1.16       gwr 		error = ENOTTY;
    299      1.16       gwr 		break;
    300       1.1       gwr 	}
    301       1.1       gwr 
    302       1.1       gwr 	return (error);
    303       1.1       gwr }
    304       1.1       gwr 
    305       1.1       gwr /****************************************************************
    306       1.1       gwr  * ioctl helpers
    307       1.1       gwr  ****************************************************************/
    308       1.1       gwr 
    309       1.1       gwr /*
    310       1.1       gwr  * Get/Set keymap entry
    311       1.1       gwr  */
    312       1.7       gwr static int
    313       1.1       gwr kbd_iockeymap(ks, cmd, kio)
    314       1.1       gwr 	struct kbd_state *ks;
    315       1.1       gwr 	u_long cmd;
    316       1.1       gwr 	struct kiockeymap *kio;
    317       1.1       gwr {
    318      1.13       gwr 	u_short *km;
    319       1.1       gwr 	u_int station;
    320       1.1       gwr 
    321       1.1       gwr 	switch (kio->kio_tablemask) {
    322       1.1       gwr 	case KIOC_NOMASK:
    323       1.1       gwr 		km = ks->kbd_k.k_normal;
    324       1.1       gwr 		break;
    325       1.1       gwr 	case KIOC_SHIFTMASK:
    326       1.1       gwr 		km = ks->kbd_k.k_shifted;
    327       1.1       gwr 		break;
    328       1.1       gwr 	case KIOC_CTRLMASK:
    329       1.1       gwr 		km = ks->kbd_k.k_control;
    330       1.1       gwr 		break;
    331       1.1       gwr 	case KIOC_UPMASK:
    332       1.1       gwr 		km = ks->kbd_k.k_release;
    333       1.1       gwr 		break;
    334       1.1       gwr 	default:
    335       1.1       gwr 		/* Silently ignore unsupported masks */
    336       1.1       gwr 		return (0);
    337       1.1       gwr 	}
    338       1.1       gwr 
    339       1.1       gwr 	/* Range-check the table position. */
    340       1.1       gwr 	station = kio->kio_station;
    341       1.1       gwr 	if (station >= KEYMAP_SIZE)
    342       1.1       gwr 		return (EINVAL);
    343       1.1       gwr 
    344       1.1       gwr 	switch (cmd) {
    345       1.1       gwr 
    346       1.1       gwr 	case KIOCGKEY:	/* Get keymap entry */
    347      1.13       gwr 		kio->kio_entry = km[station];
    348       1.1       gwr 		break;
    349       1.1       gwr 
    350       1.1       gwr 	case KIOCSKEY:	/* Set keymap entry */
    351      1.13       gwr 		km[station] = kio->kio_entry;
    352       1.1       gwr 		break;
    353       1.1       gwr 
    354       1.1       gwr 	default:
    355       1.1       gwr 		return(ENOTTY);
    356       1.1       gwr 	}
    357       1.1       gwr 	return (0);
    358       1.1       gwr }
    359       1.1       gwr 
    360       1.1       gwr #ifdef	KIOCGETKEY
    361       1.1       gwr /*
    362       1.1       gwr  * Get/Set keymap entry,
    363       1.1       gwr  * old format (compatibility)
    364       1.1       gwr  */
    365       1.1       gwr int
    366       1.1       gwr kbd_oldkeymap(ks, cmd, kio)
    367       1.1       gwr 	struct kbd_state *ks;
    368       1.1       gwr 	u_long cmd;
    369       1.1       gwr 	struct okiockey *kio;
    370       1.1       gwr {
    371       1.1       gwr 	int error = 0;
    372       1.1       gwr 
    373       1.1       gwr 	switch (cmd) {
    374       1.1       gwr 
    375       1.1       gwr 	case KIOCGETKEY:
    376       1.1       gwr 		if (kio->kio_station == 118) {
    377       1.1       gwr 			/*
    378       1.1       gwr 			 * This is X11 asking if a type 3 keyboard is
    379       1.1       gwr 			 * really a type 3 keyboard.  Say yes, it is,
    380       1.1       gwr 			 * by reporting key station 118 as a "hole".
    381       1.1       gwr 			 * Note old (SunOS 3.5) definition of HOLE!
    382       1.1       gwr 			 */
    383       1.1       gwr 			kio->kio_entry = 0xA2;
    384       1.1       gwr 			break;
    385       1.1       gwr 		}
    386       1.1       gwr 		/* fall through */
    387       1.1       gwr 
    388       1.1       gwr 	default:
    389       1.1       gwr 		error = ENOTTY;
    390       1.1       gwr 		break;
    391       1.1       gwr 	}
    392       1.1       gwr 
    393       1.1       gwr 	return (error);
    394       1.1       gwr }
    395       1.1       gwr #endif	/* KIOCGETKEY */
    396       1.1       gwr 
    397       1.7       gwr 
    398       1.7       gwr /*
    399       1.7       gwr  * keyboard command ioctl
    400       1.7       gwr  * ``unimplemented commands are ignored'' (blech)
    401      1.15       gwr  * This is also export to the fb driver.
    402       1.7       gwr  */
    403      1.15       gwr int
    404      1.15       gwr kbd_docmd(cmd, isuser)
    405      1.15       gwr 	int cmd;
    406      1.15       gwr 	int isuser;
    407      1.15       gwr {
    408       1.7       gwr 	struct kbd_softc *k;
    409      1.15       gwr 	struct kbd_state *ks;
    410      1.15       gwr 	int error, s;
    411      1.15       gwr 
    412      1.15       gwr 	error = 0;
    413      1.15       gwr 	k = kbd_cd.cd_devs[0];
    414      1.15       gwr 	ks = &k->k_state;
    415       1.7       gwr 
    416       1.7       gwr 	switch (cmd) {
    417       1.7       gwr 
    418       1.7       gwr 	case KBD_CMD_BELL:
    419       1.7       gwr 	case KBD_CMD_NOBELL:
    420       1.7       gwr 		/* Supported by type 2, 3, and 4 keyboards */
    421       1.7       gwr 		break;
    422       1.7       gwr 
    423       1.7       gwr 	case KBD_CMD_CLICK:
    424       1.7       gwr 	case KBD_CMD_NOCLICK:
    425       1.7       gwr 		/* Unsupported by type 2 keyboards */
    426       1.7       gwr 		if (ks->kbd_id <= KB_SUN2)
    427       1.7       gwr 			return (0);
    428       1.7       gwr 		ks->kbd_click = (cmd == KBD_CMD_CLICK);
    429       1.7       gwr 		break;
    430       1.7       gwr 
    431       1.7       gwr 	default:
    432       1.7       gwr 		return (0);
    433       1.7       gwr 	}
    434       1.7       gwr 
    435       1.7       gwr 	s = spltty();
    436       1.7       gwr 
    437      1.15       gwr 	if (isuser)
    438      1.15       gwr 		error = kbd_drain_tx(k);
    439      1.15       gwr 
    440       1.7       gwr 	if (error == 0) {
    441       1.7       gwr 		kbd_output(k, cmd);
    442       1.7       gwr 		kbd_start_tx(k);
    443       1.7       gwr 	}
    444       1.7       gwr 
    445       1.7       gwr 	splx(s);
    446       1.7       gwr 
    447       1.7       gwr 	return (error);
    448       1.7       gwr }
    449       1.7       gwr 
    450       1.7       gwr /*
    451       1.7       gwr  * Set LEDs ioctl.
    452       1.7       gwr  */
    453       1.7       gwr static int
    454       1.7       gwr kbd_iocsled(k, data)
    455       1.7       gwr 	struct kbd_softc *k;
    456      1.18       gwr 	char *data;
    457       1.7       gwr {
    458       1.7       gwr 	int leds, error, s;
    459       1.7       gwr 
    460       1.7       gwr 	leds = *data;
    461       1.7       gwr 
    462       1.7       gwr 	s = spltty();
    463       1.7       gwr 	error = kbd_drain_tx(k);
    464       1.7       gwr 	if (error == 0) {
    465       1.7       gwr 		kbd_set_leds(k, leds);
    466       1.7       gwr 	}
    467       1.7       gwr 	splx(s);
    468       1.7       gwr 
    469       1.7       gwr 	return (error);
    470       1.7       gwr }
    471       1.7       gwr 
    472       1.7       gwr 
    473       1.1       gwr /****************************************************************
    474       1.1       gwr  * middle layers:
    475       1.1       gwr  *  - keysym to ASCII sequence
    476       1.1       gwr  *  - raw key codes to keysym
    477       1.1       gwr  ****************************************************************/
    478       1.1       gwr 
    479      1.13       gwr static void kbd_input_string __P((struct kbd_softc *, char *));
    480      1.13       gwr static void kbd_input_funckey __P((struct kbd_softc *, int));
    481      1.17       gwr static int  kbd_input_keysym __P((struct kbd_softc *, int));
    482       1.1       gwr 
    483       1.1       gwr /*
    484       1.1       gwr  * Initialization done by either kdcninit or kbd_iopen
    485       1.1       gwr  */
    486       1.1       gwr void
    487       1.1       gwr kbd_xlate_init(ks)
    488       1.1       gwr 	struct kbd_state *ks;
    489       1.1       gwr {
    490       1.1       gwr 	struct keyboard *ktbls;
    491       1.1       gwr 	int id;
    492       1.1       gwr 
    493       1.1       gwr 	id = ks->kbd_id;
    494       1.1       gwr 	if (id < KBD_MIN_TYPE)
    495       1.1       gwr 		id = KBD_MIN_TYPE;
    496       1.1       gwr 	if (id > kbd_max_type)
    497       1.1       gwr 		id = kbd_max_type;
    498       1.1       gwr 	ktbls = keyboards[id];
    499       1.1       gwr 
    500       1.1       gwr 	ks->kbd_k = *ktbls; 	/* struct assignment */
    501       1.1       gwr 	ks->kbd_modbits = 0;
    502       1.1       gwr }
    503       1.1       gwr 
    504       1.1       gwr /*
    505       1.1       gwr  * Turn keyboard up/down codes into a KEYSYM.
    506       1.1       gwr  * Note that the "kd" driver uses this too!
    507       1.1       gwr  */
    508       1.1       gwr int
    509       1.1       gwr kbd_code_to_keysym(ks, c)
    510      1.23        pk 	struct kbd_state *ks;
    511      1.23        pk 	int c;
    512       1.1       gwr {
    513      1.13       gwr 	u_short *km;
    514       1.1       gwr 	int keysym;
    515       1.1       gwr 
    516       1.1       gwr 	/*
    517       1.1       gwr 	 * Get keymap pointer.  One of these:
    518       1.1       gwr 	 * release, control, shifted, normal, ...
    519       1.1       gwr 	 */
    520       1.1       gwr 	if (KEY_UP(c))
    521       1.1       gwr 		km = ks->kbd_k.k_release;
    522       1.4       gwr 	else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
    523       1.4       gwr 		km = ks->kbd_k.k_control;
    524       1.4       gwr 	else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
    525       1.4       gwr 		km = ks->kbd_k.k_shifted;
    526       1.4       gwr 	else
    527       1.4       gwr 		km = ks->kbd_k.k_normal;
    528       1.4       gwr 
    529       1.1       gwr 	if (km == NULL) {
    530       1.1       gwr 		/*
    531       1.1       gwr 		 * Do not know how to translate yet.
    532       1.1       gwr 		 * We will find out when a RESET comes along.
    533       1.1       gwr 		 */
    534       1.4       gwr 		return (KEYSYM_NOP);
    535       1.4       gwr 	}
    536      1.13       gwr 	keysym = km[KEY_CODE(c)];
    537       1.4       gwr 
    538       1.4       gwr 	/*
    539       1.4       gwr 	 * Post-processing for Caps-lock
    540       1.4       gwr 	 */
    541       1.4       gwr 	if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
    542       1.4       gwr 		(KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
    543       1.4       gwr 	{
    544       1.4       gwr 		if (('a' <= keysym) && (keysym <= 'z'))
    545       1.4       gwr 			keysym -= ('a' - 'A');
    546       1.4       gwr 	}
    547       1.4       gwr 
    548       1.4       gwr 	/*
    549      1.17       gwr 	 * Post-processing for Num-lock.  All "function"
    550      1.17       gwr 	 * keysyms get indirected through another table.
    551      1.17       gwr 	 * (XXX: Only if numlock on.  Want off also!)
    552       1.4       gwr 	 */
    553       1.4       gwr 	if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
    554       1.4       gwr 		(KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
    555       1.4       gwr 	{
    556       1.4       gwr 		keysym = kbd_numlock_map[keysym & 0x3F];
    557       1.4       gwr 	}
    558       1.1       gwr 
    559       1.1       gwr 	return (keysym);
    560       1.1       gwr }
    561       1.1       gwr 
    562       1.1       gwr void
    563       1.1       gwr kbd_input_string(k, str)
    564       1.1       gwr 	struct kbd_softc *k;
    565       1.1       gwr 	char *str;
    566       1.1       gwr {
    567      1.22       mrg 
    568       1.1       gwr 	while (*str) {
    569      1.23        pk 		(*k->k_cc->cc_upstream)(*str);
    570       1.1       gwr 		str++;
    571       1.1       gwr 	}
    572       1.1       gwr }
    573       1.1       gwr 
    574       1.1       gwr void
    575       1.1       gwr kbd_input_funckey(k, keysym)
    576       1.1       gwr 	struct kbd_softc *k;
    577      1.23        pk 	int keysym;
    578       1.1       gwr {
    579      1.23        pk 	int n;
    580       1.1       gwr 	char str[12];
    581       1.1       gwr 
    582       1.1       gwr 	/*
    583       1.1       gwr 	 * Format the F-key sequence and send as a string.
    584       1.1       gwr 	 * XXX: Ugly compatibility mappings.
    585       1.1       gwr 	 */
    586       1.1       gwr 	n = 0xC0 + (keysym & 0x3F);
    587      1.11  christos 	sprintf(str, "\033[%dz", n);
    588       1.1       gwr 	kbd_input_string(k, str);
    589       1.1       gwr }
    590       1.1       gwr 
    591       1.1       gwr /*
    592       1.1       gwr  * This is called by kbd_input_raw() or by kb_repeat()
    593       1.7       gwr  * to deliver ASCII input.  Called at spltty().
    594      1.17       gwr  *
    595      1.17       gwr  * Return zero on success, else the keysym that we
    596      1.17       gwr  * could not handle (so the caller may complain).
    597       1.1       gwr  */
    598      1.17       gwr int
    599       1.1       gwr kbd_input_keysym(k, keysym)
    600       1.1       gwr 	struct kbd_softc *k;
    601      1.23        pk 	int keysym;
    602       1.1       gwr {
    603       1.1       gwr 	struct kbd_state *ks = &k->k_state;
    604      1.23        pk 	int data;
    605       1.1       gwr 
    606       1.4       gwr 	switch (KEYSYM_CLASS(keysym)) {
    607       1.1       gwr 
    608       1.1       gwr 	case KEYSYM_ASCII:
    609       1.1       gwr 		data = KEYSYM_DATA(keysym);
    610       1.1       gwr 		if (ks->kbd_modbits & KBMOD_META_MASK)
    611       1.1       gwr 			data |= 0x80;
    612      1.23        pk 		(*k->k_cc->cc_upstream)(data);
    613       1.1       gwr 		break;
    614       1.1       gwr 
    615       1.1       gwr 	case KEYSYM_STRING:
    616       1.1       gwr 		data = keysym & 0xF;
    617       1.1       gwr 		kbd_input_string(k, kbd_stringtab[data]);
    618       1.1       gwr 		break;
    619       1.1       gwr 
    620       1.1       gwr 	case KEYSYM_FUNC:
    621       1.1       gwr 		kbd_input_funckey(k, keysym);
    622       1.1       gwr 		break;
    623       1.1       gwr 
    624       1.1       gwr 	case KEYSYM_CLRMOD:
    625       1.1       gwr 		data = 1 << (keysym & 0x1F);
    626       1.1       gwr 		ks->kbd_modbits &= ~data;
    627       1.1       gwr 		break;
    628       1.1       gwr 
    629       1.1       gwr 	case KEYSYM_SETMOD:
    630       1.1       gwr 		data = 1 << (keysym & 0x1F);
    631       1.1       gwr 		ks->kbd_modbits |= data;
    632       1.1       gwr 		break;
    633       1.1       gwr 
    634       1.1       gwr 	case KEYSYM_INVMOD:
    635       1.1       gwr 		data = 1 << (keysym & 0x1F);
    636       1.1       gwr 		ks->kbd_modbits ^= data;
    637       1.4       gwr 		kbd_update_leds(k);
    638       1.1       gwr 		break;
    639       1.1       gwr 
    640       1.1       gwr 	case KEYSYM_ALL_UP:
    641       1.1       gwr 		ks->kbd_modbits &= ~0xFFFF;
    642       1.1       gwr 		break;
    643       1.1       gwr 
    644       1.1       gwr 	case KEYSYM_SPECIAL:
    645       1.1       gwr 		if (keysym == KEYSYM_NOP)
    646       1.1       gwr 			break;
    647       1.1       gwr 		/* fall through */
    648       1.1       gwr 	default:
    649      1.17       gwr 		/* We could not handle it. */
    650      1.17       gwr 		return (keysym);
    651       1.1       gwr 	}
    652      1.17       gwr 	return (0);
    653       1.1       gwr }
    654       1.1       gwr 
    655       1.1       gwr /*
    656       1.1       gwr  * This is the autorepeat timeout function.
    657       1.7       gwr  * Called at splsoftclock().
    658       1.1       gwr  */
    659      1.13       gwr static void
    660      1.22       mrg kbd_repeat(arg)
    661      1.22       mrg 	void *arg;
    662       1.1       gwr {
    663       1.1       gwr 	struct kbd_softc *k = (struct kbd_softc *)arg;
    664       1.7       gwr 	int s = spltty();
    665       1.1       gwr 
    666       1.1       gwr 	if (k->k_repeating && k->k_repeatsym >= 0) {
    667      1.17       gwr 		(void)kbd_input_keysym(k, k->k_repeatsym);
    668      1.25   thorpej 		callout_reset(&k->k_repeat_ch, k->k_repeat_step,
    669      1.25   thorpej 		    kbd_repeat, k);
    670       1.1       gwr 	}
    671       1.7       gwr 	splx(s);
    672       1.1       gwr }
    673       1.1       gwr 
    674       1.1       gwr /*
    675       1.1       gwr  * Called by our kbd_softint() routine on input,
    676       1.1       gwr  * which passes the raw hardware scan codes.
    677       1.7       gwr  * Called at spltty()
    678       1.1       gwr  */
    679       1.1       gwr void
    680       1.1       gwr kbd_input_raw(k, c)
    681       1.1       gwr 	struct kbd_softc *k;
    682      1.23        pk 	int c;
    683       1.1       gwr {
    684       1.1       gwr 	struct kbd_state *ks = &k->k_state;
    685       1.1       gwr 	struct firm_event *fe;
    686       1.1       gwr 	int put, keysym;
    687       1.1       gwr 
    688       1.1       gwr 	/* XXX - Input errors already handled. */
    689       1.1       gwr 
    690       1.1       gwr 	/* Are we expecting special input? */
    691       1.1       gwr 	if (ks->kbd_expect) {
    692       1.1       gwr 		if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
    693       1.1       gwr 			/* We read a KBD_RESET last time. */
    694       1.1       gwr 			ks->kbd_id = c;
    695       1.1       gwr 			kbd_was_reset(k);
    696       1.1       gwr 		}
    697       1.1       gwr 		if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
    698       1.1       gwr 			/* We read a KBD_LAYOUT last time. */
    699       1.1       gwr 			ks->kbd_layout = c;
    700       1.1       gwr 			kbd_new_layout(k);
    701       1.1       gwr 		}
    702       1.1       gwr 		ks->kbd_expect = 0;
    703       1.1       gwr 		return;
    704       1.1       gwr 	}
    705       1.1       gwr 
    706       1.1       gwr 	/* Is this one of the "special" input codes? */
    707       1.1       gwr 	if (KBD_SPECIAL(c)) {
    708       1.1       gwr 		switch (c) {
    709       1.1       gwr 		case KBD_RESET:
    710       1.1       gwr 			ks->kbd_expect |= KBD_EXPECT_IDCODE;
    711       1.1       gwr 			/* Fake an "all-up" to resync. translation. */
    712       1.1       gwr 			c = KBD_IDLE;
    713       1.1       gwr 			break;
    714       1.1       gwr 
    715       1.1       gwr 		case KBD_LAYOUT:
    716       1.1       gwr 			ks->kbd_expect |= KBD_EXPECT_LAYOUT;
    717       1.1       gwr 			return;
    718       1.1       gwr 
    719       1.1       gwr 		case KBD_ERROR:
    720       1.1       gwr 			log(LOG_WARNING, "%s: received error indicator\n",
    721       1.1       gwr 				k->k_dev.dv_xname);
    722       1.1       gwr 			return;
    723       1.1       gwr 
    724       1.1       gwr 		case KBD_IDLE:
    725       1.1       gwr 			/* Let this go to the translator. */
    726       1.1       gwr 			break;
    727       1.1       gwr 		}
    728       1.1       gwr 	}
    729       1.1       gwr 
    730       1.1       gwr 	/*
    731       1.1       gwr 	 * If /dev/kbd is not connected in event mode,
    732       1.1       gwr 	 * translate and send upstream (to console).
    733       1.1       gwr 	 */
    734      1.27       eeh 	if (!k->k_evmode) {
    735       1.1       gwr 
    736       1.1       gwr 		/* Any input stops auto-repeat (i.e. key release). */
    737       1.1       gwr 		if (k->k_repeating) {
    738       1.1       gwr 			k->k_repeating = 0;
    739      1.25   thorpej 			callout_stop(&k->k_repeat_ch);
    740       1.1       gwr 		}
    741       1.1       gwr 
    742       1.1       gwr 		/* Translate this code to a keysym */
    743       1.1       gwr 		keysym = kbd_code_to_keysym(ks, c);
    744       1.1       gwr 
    745       1.1       gwr 		/* Pass up to the next layer. */
    746      1.17       gwr 		if (kbd_input_keysym(k, keysym)) {
    747      1.17       gwr 			log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
    748      1.17       gwr 				" produced unexpected keysym 0x%x\n",
    749      1.17       gwr 				k->k_dev.dv_xname, c,
    750      1.17       gwr 				ks->kbd_modbits, keysym);
    751      1.17       gwr 			/* No point in auto-repeat here. */
    752      1.17       gwr 			return;
    753      1.17       gwr 		}
    754       1.1       gwr 
    755       1.1       gwr 		/* Does this symbol get auto-repeat? */
    756       1.1       gwr 		if (KEYSYM_NOREPEAT(keysym))
    757       1.1       gwr 			return;
    758       1.1       gwr 
    759       1.1       gwr 		/* Setup for auto-repeat after initial delay. */
    760       1.1       gwr 		k->k_repeating = 1;
    761       1.1       gwr 		k->k_repeatsym = keysym;
    762      1.26   hannken 		callout_reset(&k->k_repeat_ch, k->k_repeat_start,
    763      1.25   thorpej 		    kbd_repeat, k);
    764       1.1       gwr 		return;
    765       1.1       gwr 	}
    766       1.1       gwr 
    767       1.1       gwr 	/*
    768       1.1       gwr 	 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
    769       1.1       gwr 	 * This is bad as it means the server will not automatically resync
    770       1.1       gwr 	 * on all-up IDLEs, but I did not drop them before, and the server
    771       1.1       gwr 	 * goes crazy when it comes time to blank the screen....
    772       1.1       gwr 	 */
    773       1.1       gwr 	if (c == KBD_IDLE)
    774       1.1       gwr 		return;
    775       1.1       gwr 
    776       1.1       gwr 	/*
    777       1.1       gwr 	 * Keyboard is generating events.  Turn this keystroke into an
    778       1.1       gwr 	 * event and put it in the queue.  If the queue is full, the
    779       1.1       gwr 	 * keystroke is lost (sorry!).
    780       1.1       gwr 	 */
    781       1.1       gwr 	put = k->k_events.ev_put;
    782       1.1       gwr 	fe = &k->k_events.ev_q[put];
    783       1.1       gwr 	put = (put + 1) % EV_QSIZE;
    784       1.1       gwr 	if (put == k->k_events.ev_get) {
    785       1.1       gwr 		log(LOG_WARNING, "%s: event queue overflow\n",
    786       1.1       gwr 			k->k_dev.dv_xname); /* ??? */
    787       1.1       gwr 		return;
    788       1.1       gwr 	}
    789       1.1       gwr 	fe->id = KEY_CODE(c);
    790       1.1       gwr 	fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    791       1.1       gwr 	fe->time = time;
    792       1.1       gwr 	k->k_events.ev_put = put;
    793       1.1       gwr 	EV_WAKEUP(&k->k_events);
    794       1.1       gwr }
    795       1.1       gwr 
    796      1.24        pk /****************************************************************/
    797      1.24        pk 
    798      1.24        pk /*
    799      1.24        pk  * Open/close routines called upon opening /dev/console
    800      1.24        pk  * if we serve console input.
    801      1.24        pk  */
    802      1.24        pk int
    803      1.24        pk kbd_cc_open(cc)
    804      1.24        pk 	struct cons_channel *cc;
    805      1.24        pk {
    806      1.24        pk 	struct kbd_softc *k = (struct kbd_softc *)cc->cc_dev;
    807      1.24        pk 	return (kbd_iopen(k));
    808      1.24        pk }
    809      1.24        pk 
    810      1.24        pk int
    811      1.24        pk kbd_cc_close(cc)
    812      1.24        pk 	struct cons_channel *cc;
    813      1.24        pk {
    814      1.24        pk 	struct kbd_softc *k = (struct kbd_softc *)cc->cc_dev;
    815      1.24        pk 	return (kbd_iclose(k));
    816      1.24        pk }
    817       1.1       gwr 
    818       1.1       gwr /*
    819       1.1       gwr  * Initialization to be done at first open.
    820      1.24        pk  * This is called from kbdopen() or kd_cc_open()
    821       1.7       gwr  * Called with user context.
    822       1.1       gwr  */
    823       1.1       gwr int
    824      1.24        pk kbd_iopen(k)
    825      1.24        pk 	struct kbd_softc *k;
    826       1.1       gwr {
    827       1.1       gwr 	struct kbd_state *ks;
    828       1.1       gwr 	int error, s;
    829       1.1       gwr 
    830       1.1       gwr 	if (k == NULL)
    831       1.1       gwr 		return (ENXIO);
    832      1.23        pk 
    833       1.1       gwr 	ks = &k->k_state;
    834       1.1       gwr 
    835       1.1       gwr 	/* Tolerate extra calls. */
    836       1.1       gwr 	if (k->k_isopen)
    837      1.23        pk 		return (0);
    838       1.1       gwr 
    839      1.27       eeh 	/* Open internal device */
    840      1.27       eeh 	if (k->k_deviopen)
    841      1.27       eeh 		(*k->k_deviopen)((struct device *)k, FREAD|FWRITE);
    842      1.27       eeh 
    843       1.1       gwr 	s = spltty();
    844       1.1       gwr 
    845       1.1       gwr 	/* Reset the keyboard and find out its type. */
    846       1.1       gwr 	kbd_output(k, KBD_CMD_RESET);
    847       1.1       gwr 	kbd_start_tx(k);
    848       1.1       gwr 	kbd_drain_tx(k);
    849       1.1       gwr 	/* The wakeup for this is in kbd_was_reset(). */
    850       1.1       gwr 	error = tsleep((caddr_t)&ks->kbd_id,
    851       1.1       gwr 				   PZERO | PCATCH, devopn, hz);
    852       1.1       gwr 	if (error == EWOULDBLOCK) { 	/* no response */
    853       1.1       gwr 		error = 0;
    854       1.1       gwr 		log(LOG_ERR, "%s: reset failed\n",
    855       1.1       gwr 			k->k_dev.dv_xname);
    856       1.1       gwr 		/*
    857       1.1       gwr 		 * Allow the open anyway (to keep getty happy)
    858       1.1       gwr 		 * but assume the "least common denominator".
    859       1.1       gwr 		 */
    860       1.1       gwr 		ks->kbd_id = KB_SUN2;
    861       1.1       gwr 	}
    862       1.1       gwr 
    863      1.17       gwr 	/* Initialize the table pointers for this type. */
    864      1.17       gwr 	kbd_xlate_init(ks);
    865      1.17       gwr 
    866       1.1       gwr 	/* Earlier than type 4 does not know "layout". */
    867       1.1       gwr 	if (ks->kbd_id < KB_SUN4)
    868       1.1       gwr 		goto out;
    869       1.1       gwr 
    870       1.1       gwr 	/* Ask for the layout. */
    871       1.1       gwr 	kbd_output(k, KBD_CMD_GETLAYOUT);
    872       1.1       gwr 	kbd_start_tx(k);
    873       1.1       gwr 	kbd_drain_tx(k);
    874       1.1       gwr 	/* The wakeup for this is in kbd_new_layout(). */
    875       1.1       gwr 	error = tsleep((caddr_t)&ks->kbd_layout,
    876       1.1       gwr 				   PZERO | PCATCH, devopn, hz);
    877       1.1       gwr 	if (error == EWOULDBLOCK) { 	/* no response */
    878       1.1       gwr 		error = 0;
    879       1.1       gwr 		log(LOG_ERR, "%s: no response to get_layout\n",
    880       1.1       gwr 			k->k_dev.dv_xname);
    881       1.1       gwr 		ks->kbd_layout = 0;
    882       1.1       gwr 	}
    883       1.1       gwr 
    884       1.1       gwr out:
    885       1.1       gwr 	splx(s);
    886       1.1       gwr 
    887       1.1       gwr 	if (error == 0)
    888       1.1       gwr 		k->k_isopen = 1;
    889       1.1       gwr 
    890      1.23        pk 	return (error);
    891      1.23        pk }
    892      1.23        pk 
    893      1.23        pk int
    894      1.24        pk kbd_iclose(k)
    895      1.24        pk 	struct kbd_softc *k;
    896      1.23        pk {
    897      1.23        pk 	/* For now: */ return (0);
    898       1.1       gwr }
    899       1.1       gwr 
    900       1.7       gwr /*
    901       1.7       gwr  * Called by kbd_input_raw, at spltty()
    902       1.7       gwr  */
    903      1.13       gwr static void
    904       1.1       gwr kbd_was_reset(k)
    905       1.1       gwr 	struct kbd_softc *k;
    906       1.1       gwr {
    907       1.1       gwr 	struct kbd_state *ks = &k->k_state;
    908       1.1       gwr 
    909       1.1       gwr 	/*
    910       1.1       gwr 	 * On first identification, wake up anyone waiting for type
    911       1.1       gwr 	 * and set up the table pointers.
    912       1.1       gwr 	 */
    913       1.1       gwr 	wakeup((caddr_t)&ks->kbd_id);
    914       1.1       gwr 
    915       1.1       gwr 	/* Restore keyclick, if necessary */
    916       1.1       gwr 	switch (ks->kbd_id) {
    917       1.1       gwr 
    918       1.1       gwr 	case KB_SUN2:
    919       1.1       gwr 		/* Type 2 keyboards don't support keyclick */
    920       1.1       gwr 		break;
    921       1.1       gwr 
    922       1.1       gwr 	case KB_SUN3:
    923       1.1       gwr 		/* Type 3 keyboards come up with keyclick on */
    924       1.7       gwr 		if (!ks->kbd_click) {
    925       1.7       gwr 			/* turn off the click */
    926       1.7       gwr 			kbd_output(k, KBD_CMD_NOCLICK);
    927       1.7       gwr 			kbd_start_tx(k);
    928       1.7       gwr 		}
    929       1.1       gwr 		break;
    930       1.1       gwr 
    931       1.1       gwr 	case KB_SUN4:
    932       1.1       gwr 		/* Type 4 keyboards come up with keyclick off */
    933       1.7       gwr 		if (ks->kbd_click) {
    934       1.7       gwr 			/* turn on the click */
    935       1.7       gwr 			kbd_output(k, KBD_CMD_CLICK);
    936       1.7       gwr 			kbd_start_tx(k);
    937       1.7       gwr 		}
    938       1.1       gwr 		break;
    939       1.1       gwr 	}
    940       1.1       gwr 
    941       1.1       gwr 	/* LEDs are off after reset. */
    942       1.1       gwr 	ks->kbd_leds = 0;
    943       1.1       gwr }
    944       1.1       gwr 
    945       1.7       gwr /*
    946       1.7       gwr  * Called by kbd_input_raw, at spltty()
    947       1.7       gwr  */
    948      1.13       gwr static void
    949       1.1       gwr kbd_new_layout(k)
    950       1.1       gwr 	struct kbd_softc *k;
    951       1.1       gwr {
    952       1.1       gwr 	struct kbd_state *ks = &k->k_state;
    953       1.1       gwr 
    954       1.1       gwr 	/*
    955       1.1       gwr 	 * On first identification, wake up anyone waiting for type
    956       1.1       gwr 	 * and set up the table pointers.
    957       1.1       gwr 	 */
    958       1.1       gwr 	wakeup((caddr_t)&ks->kbd_layout);
    959       1.1       gwr 
    960       1.1       gwr 	/* XXX: switch decoding tables? */
    961       1.1       gwr }
    962       1.1       gwr 
    963       1.1       gwr 
    964       1.1       gwr /*
    965       1.1       gwr  * Wait for output to finish.
    966       1.7       gwr  * Called at spltty().  Has user context.
    967       1.1       gwr  */
    968      1.13       gwr static int
    969       1.1       gwr kbd_drain_tx(k)
    970       1.1       gwr 	struct kbd_softc *k;
    971       1.1       gwr {
    972       1.7       gwr 	int error;
    973       1.1       gwr 
    974       1.1       gwr 	error = 0;
    975       1.7       gwr 
    976      1.27       eeh 	while (k->k_txflags & K_TXBUSY && !error) {
    977       1.1       gwr 		k->k_txflags |= K_TXWANT;
    978       1.1       gwr 		error = tsleep((caddr_t)&k->k_txflags,
    979       1.1       gwr 					   PZERO | PCATCH, "kbdout", 0);
    980       1.1       gwr 	}
    981       1.7       gwr 
    982       1.1       gwr 	return (error);
    983       1.1       gwr }
    984       1.1       gwr 
    985       1.1       gwr /*
    986       1.7       gwr  * Enqueue some output for the keyboard
    987       1.7       gwr  * Called at spltty().
    988       1.1       gwr  */
    989      1.22       mrg void
    990       1.1       gwr kbd_output(k, c)
    991       1.1       gwr 	struct kbd_softc *k;
    992       1.1       gwr 	int c;	/* the data */
    993       1.1       gwr {
    994       1.7       gwr 	int put;
    995       1.1       gwr 
    996       1.1       gwr 	put = k->k_tbput;
    997       1.1       gwr 	k->k_tbuf[put] = (u_char)c;
    998       1.1       gwr 	put = (put + 1) & KBD_TX_RING_MASK;
    999       1.1       gwr 
   1000       1.1       gwr 	/* Would overrun if increment makes (put==get). */
   1001       1.1       gwr 	if (put == k->k_tbget) {
   1002       1.1       gwr 		log(LOG_WARNING, "%s: output overrun\n",
   1003       1.1       gwr             k->k_dev.dv_xname);
   1004       1.1       gwr 	} else {
   1005       1.1       gwr 		/* OK, really increment. */
   1006       1.1       gwr 		k->k_tbput = put;
   1007       1.1       gwr 	}
   1008       1.1       gwr }
   1009       1.1       gwr 
   1010       1.7       gwr /*
   1011       1.7       gwr  * Start the sending data from the output queue
   1012       1.7       gwr  * Called at spltty().
   1013       1.7       gwr  */
   1014      1.22       mrg void
   1015       1.1       gwr kbd_start_tx(k)
   1016      1.22       mrg 	struct kbd_softc *k;
   1017       1.1       gwr {
   1018      1.22       mrg 	int get;
   1019       1.1       gwr 	u_char c;
   1020       1.1       gwr 
   1021       1.1       gwr 	if (k->k_txflags & K_TXBUSY)
   1022       1.7       gwr 		return;
   1023       1.1       gwr 
   1024       1.1       gwr 	/* Is there anything to send? */
   1025       1.1       gwr 	get = k->k_tbget;
   1026       1.1       gwr 	if (get == k->k_tbput) {
   1027       1.1       gwr 		/* Nothing to send.  Wake drain waiters. */
   1028       1.1       gwr 		if (k->k_txflags & K_TXWANT) {
   1029       1.1       gwr 			k->k_txflags &= ~K_TXWANT;
   1030       1.1       gwr 			wakeup((caddr_t)&k->k_txflags);
   1031       1.1       gwr 		}
   1032       1.7       gwr 		return;
   1033       1.1       gwr 	}
   1034       1.1       gwr 
   1035       1.1       gwr 	/* Have something to send. */
   1036       1.1       gwr 	c = k->k_tbuf[get];
   1037       1.1       gwr 	get = (get + 1) & KBD_TX_RING_MASK;
   1038       1.1       gwr 	k->k_tbget = get;
   1039       1.1       gwr 	k->k_txflags |= K_TXBUSY;
   1040       1.1       gwr 
   1041      1.22       mrg 	k->k_write_data(k, c);
   1042       1.1       gwr }
   1043       1.1       gwr 
   1044       1.7       gwr /*
   1045       1.7       gwr  * Called at spltty by:
   1046       1.7       gwr  * kbd_update_leds, kbd_iocsled
   1047       1.7       gwr  */
   1048      1.13       gwr static void
   1049       1.4       gwr kbd_set_leds(k, new_leds)
   1050       1.1       gwr 	struct kbd_softc *k;
   1051       1.4       gwr 	int new_leds;
   1052       1.1       gwr {
   1053       1.1       gwr 	struct kbd_state *ks = &k->k_state;
   1054       1.1       gwr 
   1055       1.1       gwr 	/* Don't send unless state changes. */
   1056       1.1       gwr 	if (ks->kbd_leds == new_leds)
   1057       1.7       gwr 		return;
   1058       1.7       gwr 
   1059       1.1       gwr 	ks->kbd_leds = new_leds;
   1060       1.1       gwr 
   1061       1.1       gwr 	/* Only type 4 and later has LEDs anyway. */
   1062      1.19   mycroft 	if (ks->kbd_id < KB_SUN4)
   1063       1.7       gwr 		return;
   1064       1.1       gwr 
   1065       1.1       gwr 	kbd_output(k, KBD_CMD_SETLED);
   1066       1.1       gwr 	kbd_output(k, new_leds);
   1067       1.1       gwr 	kbd_start_tx(k);
   1068       1.4       gwr }
   1069       1.4       gwr 
   1070       1.7       gwr /*
   1071       1.7       gwr  * Called at spltty by:
   1072       1.7       gwr  * kbd_input_keysym
   1073       1.7       gwr  */
   1074      1.13       gwr static void
   1075       1.4       gwr kbd_update_leds(k)
   1076       1.4       gwr     struct kbd_softc *k;
   1077       1.4       gwr {
   1078      1.17       gwr 	struct kbd_state *ks = &k->k_state;
   1079      1.23        pk 	char leds;
   1080       1.4       gwr 
   1081       1.4       gwr 	leds = ks->kbd_leds;
   1082       1.4       gwr 	leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
   1083       1.4       gwr 
   1084       1.4       gwr 	if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
   1085       1.4       gwr 		leds |= LED_CAPS_LOCK;
   1086       1.4       gwr 	if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
   1087       1.4       gwr 		leds |= LED_NUM_LOCK;
   1088       1.4       gwr 
   1089       1.4       gwr 	kbd_set_leds(k, leds);
   1090       1.1       gwr }
   1091