Home | History | Annotate | Line # | Download | only in sun
kbd.c revision 1.20
      1  1.20   thorpej /*	$NetBSD: kbd.c,v 1.20 1998/01/12 09:39:27 thorpej 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.1       gwr /*
     54   1.1       gwr  * Zilog Z8530 Dual UART driver (keyboard interface)
     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.1       gwr 
     74   1.1       gwr #include <dev/ic/z8530reg.h>
     75   1.1       gwr #include <machine/z8530var.h>
     76   1.1       gwr #include <machine/vuid_event.h>
     77   1.1       gwr #include <machine/kbd.h>
     78   1.1       gwr #include <machine/kbio.h>
     79   1.1       gwr 
     80   1.1       gwr #include "event_var.h"
     81   1.1       gwr #include "kbd_xlate.h"
     82  1.14       jtk #include "locators.h"
     83   1.1       gwr 
     84   1.1       gwr /*
     85   1.1       gwr  * Ideas:
     86   1.1       gwr  * /dev/kbd is not a tty (plain device)
     87   1.1       gwr  */
     88   1.1       gwr 
     89   1.1       gwr /*
     90   1.1       gwr  * How many input characters we can buffer.
     91   1.1       gwr  * The port-specific var.h may override this.
     92   1.1       gwr  * Note: must be a power of two!
     93   1.1       gwr  */
     94   1.1       gwr #define	KBD_RX_RING_SIZE	256
     95   1.1       gwr #define KBD_RX_RING_MASK (KBD_RX_RING_SIZE-1)
     96   1.1       gwr /*
     97   1.1       gwr  * Output buffer.  Only need a few chars.
     98   1.1       gwr  */
     99   1.1       gwr #define	KBD_TX_RING_SIZE	16
    100   1.1       gwr #define KBD_TX_RING_MASK (KBD_TX_RING_SIZE-1)
    101   1.1       gwr /*
    102   1.1       gwr  * Keyboard serial line speed is fixed at 1200 bps.
    103   1.1       gwr  */
    104   1.1       gwr #define KBD_BPS 1200
    105   1.1       gwr #define KBD_RESET_TIMO 1000 /* mS. */
    106   1.1       gwr 
    107   1.1       gwr /*
    108   1.1       gwr  * XXX - Historical comment - no longer quite right...
    109   1.1       gwr  * Keyboard driver state.  The ascii and kbd links go up and down and
    110   1.1       gwr  * we just sit in the middle doing translation.  Note that it is possible
    111   1.1       gwr  * to get just one of the two links, in which case /dev/kbd is unavailable.
    112   1.1       gwr  * The downlink supplies us with `internal' open and close routines which
    113   1.1       gwr  * will enable dataflow across the downlink.  We promise to call open when
    114   1.1       gwr  * we are willing to take keystrokes, and to call close when we are not.
    115   1.1       gwr  * If /dev/kbd is not the console tty input source, we do this whenever
    116   1.1       gwr  * /dev/kbd is in use; otherwise we just leave it open forever.
    117   1.1       gwr  */
    118   1.1       gwr struct kbd_softc {
    119   1.1       gwr 	struct	device k_dev;		/* required first: base device */
    120   1.1       gwr 	struct	zs_chanstate *k_cs;
    121   1.1       gwr 
    122   1.1       gwr 	/* Flags to communicate with kbd_softint() */
    123   1.1       gwr 	volatile int k_intr_flags;
    124   1.1       gwr #define	INTR_RX_OVERRUN 1
    125   1.1       gwr #define INTR_TX_EMPTY   2
    126   1.1       gwr #define INTR_ST_CHECK   4
    127   1.1       gwr 
    128   1.1       gwr 	/* Transmit state */
    129   1.1       gwr 	volatile int k_txflags;
    130   1.1       gwr #define	K_TXBUSY 1
    131   1.1       gwr #define K_TXWANT 2
    132   1.1       gwr 
    133   1.1       gwr 	/*
    134   1.1       gwr 	 * State of upper interface.
    135   1.1       gwr 	 */
    136   1.1       gwr 	int	k_isopen;		/* set if open has been done */
    137   1.1       gwr 	int	k_evmode;		/* set if we should produce events */
    138   1.1       gwr 	struct	evvar k_events;		/* event queue state */
    139   1.1       gwr 
    140   1.1       gwr 	/*
    141   1.1       gwr 	 * ACSI translation state
    142   1.1       gwr 	 */
    143   1.1       gwr 	int k_repeat_start; 	/* initial delay */
    144   1.1       gwr 	int k_repeat_step;  	/* inter-char delay */
    145   1.1       gwr 	int	k_repeatsym;		/* repeating symbol */
    146   1.1       gwr 	int	k_repeating;		/* we've called timeout() */
    147   1.1       gwr 	struct	kbd_state k_state;	/* ASCII translation state */
    148   1.1       gwr 
    149   1.1       gwr 	/*
    150   1.1       gwr 	 * Magic sequence stuff (L1-A)
    151   1.1       gwr 	 */
    152   1.1       gwr 	char k_isconsole;
    153   1.1       gwr 	char k_magic1_down;
    154   1.1       gwr 	u_char k_magic1;	/* L1 */
    155   1.1       gwr 	u_char k_magic2;	/* A */
    156   1.1       gwr 
    157   1.1       gwr 	/*
    158   1.1       gwr 	 * The transmit ring buffer.
    159   1.1       gwr 	 */
    160   1.1       gwr 	volatile u_int	k_tbget;	/* transmit buffer `get' index */
    161   1.1       gwr 	volatile u_int	k_tbput;	/* transmit buffer `put' index */
    162   1.1       gwr 	u_char	k_tbuf[KBD_TX_RING_SIZE]; /* data */
    163   1.1       gwr 
    164   1.1       gwr 	/*
    165   1.1       gwr 	 * The receive ring buffer.
    166   1.1       gwr 	 */
    167   1.1       gwr 	u_int	k_rbget;	/* ring buffer `get' index */
    168   1.1       gwr 	volatile u_int	k_rbput;	/* ring buffer `put' index */
    169   1.1       gwr 	u_short	k_rbuf[KBD_RX_RING_SIZE]; /* rr1, data pairs */
    170   1.1       gwr 
    171   1.1       gwr };
    172   1.1       gwr 
    173   1.1       gwr /* Prototypes */
    174  1.13       gwr static void	kbd_new_layout(struct kbd_softc *k);
    175  1.13       gwr static void	kbd_output(struct kbd_softc *k, int c);
    176  1.13       gwr static void	kbd_repeat(void *arg);
    177  1.13       gwr static void	kbd_set_leds(struct kbd_softc *k, int leds);
    178  1.13       gwr static void	kbd_start_tx(struct kbd_softc *k);
    179  1.13       gwr static void	kbd_update_leds(struct kbd_softc *k);
    180  1.13       gwr static void	kbd_was_reset(struct kbd_softc *k);
    181  1.13       gwr static int 	kbd_drain_tx(struct kbd_softc *k);
    182   1.1       gwr 
    183   1.1       gwr cdev_decl(kbd);	/* open, close, read, write, ioctl, stop, ... */
    184   1.1       gwr 
    185   1.1       gwr struct zsops zsops_kbd;
    186   1.1       gwr 
    187   1.1       gwr /****************************************************************
    188   1.1       gwr  * Definition of the driver for autoconfig.
    189   1.1       gwr  ****************************************************************/
    190   1.1       gwr 
    191  1.13       gwr static int	kbd_match(struct device *, struct cfdata *, void *);
    192   1.1       gwr static void	kbd_attach(struct device *, struct device *, void *);
    193   1.1       gwr 
    194   1.6       gwr struct cfattach kbd_ca = {
    195   1.5   thorpej 	sizeof(struct kbd_softc), kbd_match, kbd_attach
    196   1.5   thorpej };
    197   1.5   thorpej 
    198  1.20   thorpej extern struct cfdriver kbd_cd;
    199   1.1       gwr 
    200   1.1       gwr /*
    201   1.1       gwr  * kbd_match: how is this zs channel configured?
    202   1.1       gwr  */
    203   1.1       gwr int
    204  1.13       gwr kbd_match(parent, cf, aux)
    205   1.1       gwr 	struct device *parent;
    206  1.13       gwr 	struct cfdata *cf;
    207  1.13       gwr 	void   *aux;
    208   1.1       gwr {
    209   1.1       gwr 	struct zsc_attach_args *args = aux;
    210   1.1       gwr 
    211   1.1       gwr 	/* Exact match required for keyboard. */
    212  1.14       jtk 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    213   1.1       gwr 		return 2;
    214   1.1       gwr 
    215   1.1       gwr 	return 0;
    216   1.1       gwr }
    217   1.1       gwr 
    218   1.1       gwr void
    219   1.1       gwr kbd_attach(parent, self, aux)
    220   1.1       gwr 	struct device *parent, *self;
    221   1.1       gwr 	void   *aux;
    222   1.1       gwr 
    223   1.1       gwr {
    224   1.1       gwr 	struct zsc_softc *zsc = (void *) parent;
    225   1.1       gwr 	struct kbd_softc *k = (void *) self;
    226   1.1       gwr 	struct zsc_attach_args *args = aux;
    227   1.1       gwr 	struct zs_chanstate *cs;
    228   1.1       gwr 	struct cfdata *cf;
    229   1.1       gwr 	int channel, kbd_unit;
    230  1.13       gwr 	int reset, s;
    231   1.1       gwr 
    232   1.1       gwr 	cf = k->k_dev.dv_cfdata;
    233   1.3       gwr 	kbd_unit = k->k_dev.dv_unit;
    234   1.1       gwr 	channel = args->channel;
    235  1.13       gwr 	cs = zsc->zsc_cs[channel];
    236   1.1       gwr 	cs->cs_private = k;
    237   1.1       gwr 	cs->cs_ops = &zsops_kbd;
    238   1.1       gwr 	k->k_cs = cs;
    239   1.1       gwr 
    240   1.1       gwr 	if (args->hwflags & ZS_HWFLAG_CONSOLE) {
    241   1.1       gwr 		k->k_isconsole = 1;
    242  1.11  christos 		printf(" (console)");
    243   1.1       gwr 	}
    244  1.11  christos 	printf("\n");
    245   1.1       gwr 
    246   1.1       gwr 	/* Initialize the speed, etc. */
    247   1.1       gwr 	s = splzs();
    248   1.1       gwr 	if (k->k_isconsole == 0) {
    249   1.1       gwr 		/* Not the console; may need reset. */
    250   1.1       gwr 		reset = (channel == 0) ?
    251   1.1       gwr 			ZSWR9_A_RESET : ZSWR9_B_RESET;
    252   1.2       gwr 		zs_write_reg(cs, 9, reset);
    253   1.1       gwr 	}
    254   1.1       gwr 	/* These are OK as set by zscc: WR3, WR4, WR5 */
    255  1.13       gwr 	/* We don't care about status interrupts. */
    256  1.13       gwr 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
    257  1.13       gwr 	(void) zs_set_speed(cs, KBD_BPS);
    258   1.1       gwr 	zs_loadchannelregs(cs);
    259   1.1       gwr 	splx(s);
    260   1.1       gwr 
    261   1.1       gwr 	/* Do this before any calls to kbd_rint(). */
    262   1.1       gwr 	kbd_xlate_init(&k->k_state);
    263   1.1       gwr 
    264   1.1       gwr 	/* XXX - Do this in open? */
    265   1.1       gwr 	k->k_repeat_start = hz/2;
    266   1.1       gwr 	k->k_repeat_step = hz/20;
    267   1.1       gwr 
    268   1.1       gwr 	/* Magic sequence. */
    269   1.1       gwr 	k->k_magic1 = KBD_L1;
    270   1.1       gwr 	k->k_magic2 = KBD_A;
    271   1.1       gwr 
    272   1.1       gwr 	/* Now attach the (kd) pseudo-driver. */
    273   1.1       gwr 	kd_init(kbd_unit);
    274   1.1       gwr }
    275   1.1       gwr 
    276   1.1       gwr 
    277   1.1       gwr /****************************************************************
    278   1.1       gwr  *  Entry points for /dev/kbd
    279   1.1       gwr  *  (open,close,read,write,...)
    280   1.1       gwr  ****************************************************************/
    281   1.1       gwr 
    282   1.1       gwr /*
    283   1.1       gwr  * Open:
    284   1.1       gwr  * Check exclusion, open actual device (_iopen),
    285   1.1       gwr  * setup event channel, clear ASCII repeat stuff.
    286   1.1       gwr  */
    287   1.1       gwr int
    288   1.1       gwr kbdopen(dev, flags, mode, p)
    289   1.1       gwr 	dev_t dev;
    290   1.1       gwr 	int flags, mode;
    291   1.1       gwr 	struct proc *p;
    292   1.1       gwr {
    293   1.1       gwr 	struct kbd_softc *k;
    294  1.13       gwr 	int error, unit;
    295   1.1       gwr 
    296   1.1       gwr 	unit = minor(dev);
    297   1.5   thorpej 	if (unit >= kbd_cd.cd_ndevs)
    298   1.1       gwr 		return (ENXIO);
    299   1.5   thorpej 	k = kbd_cd.cd_devs[unit];
    300   1.1       gwr 	if (k == NULL)
    301   1.1       gwr 		return (ENXIO);
    302   1.1       gwr 
    303   1.1       gwr 	/* Exclusive open required for /dev/kbd */
    304   1.1       gwr 	if (k->k_events.ev_io)
    305   1.1       gwr 		return (EBUSY);
    306   1.1       gwr 	k->k_events.ev_io = p;
    307   1.1       gwr 
    308   1.1       gwr 	if ((error = kbd_iopen(unit)) != 0) {
    309   1.1       gwr 		k->k_events.ev_io = NULL;
    310   1.1       gwr 		return (error);
    311   1.1       gwr 	}
    312   1.1       gwr 	ev_init(&k->k_events);
    313   1.1       gwr 	k->k_evmode = 1;	/* XXX: OK? */
    314   1.1       gwr 
    315   1.1       gwr 	if (k->k_repeating) {
    316   1.1       gwr 		k->k_repeating = 0;
    317   1.1       gwr 		untimeout(kbd_repeat, k);
    318   1.1       gwr 	}
    319   1.1       gwr 
    320   1.1       gwr 	return (0);
    321   1.1       gwr }
    322   1.1       gwr 
    323   1.1       gwr /*
    324   1.1       gwr  * Close:
    325   1.1       gwr  * Turn off event mode, dump the queue, and close the keyboard
    326   1.1       gwr  * unless it is supplying console input.
    327   1.1       gwr  */
    328   1.1       gwr int
    329   1.1       gwr kbdclose(dev, flags, mode, p)
    330   1.1       gwr 	dev_t dev;
    331   1.1       gwr 	int flags, mode;
    332   1.1       gwr 	struct proc *p;
    333   1.1       gwr {
    334   1.1       gwr 	struct kbd_softc *k;
    335   1.1       gwr 
    336   1.5   thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    337   1.1       gwr 	k->k_evmode = 0;
    338   1.1       gwr 	ev_fini(&k->k_events);
    339   1.1       gwr 	k->k_events.ev_io = NULL;
    340   1.1       gwr 	return (0);
    341   1.1       gwr }
    342   1.1       gwr 
    343   1.1       gwr int
    344   1.1       gwr kbdread(dev, uio, flags)
    345   1.1       gwr 	dev_t dev;
    346   1.1       gwr 	struct uio *uio;
    347   1.1       gwr 	int flags;
    348   1.1       gwr {
    349   1.1       gwr 	struct kbd_softc *k;
    350   1.1       gwr 
    351   1.5   thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    352   1.1       gwr 	return (ev_read(&k->k_events, uio, flags));
    353   1.1       gwr }
    354   1.1       gwr 
    355   1.1       gwr /* this routine should not exist, but is convenient to write here for now */
    356   1.1       gwr int
    357   1.1       gwr kbdwrite(dev, uio, flags)
    358   1.1       gwr 	dev_t dev;
    359   1.1       gwr 	struct uio *uio;
    360   1.1       gwr 	int flags;
    361   1.1       gwr {
    362   1.1       gwr 
    363   1.1       gwr 	return (EOPNOTSUPP);
    364   1.1       gwr }
    365   1.1       gwr 
    366   1.1       gwr int
    367   1.9       mrg kbdpoll(dev, events, p)
    368   1.1       gwr 	dev_t dev;
    369   1.9       mrg 	int events;
    370   1.1       gwr 	struct proc *p;
    371   1.1       gwr {
    372   1.1       gwr 	struct kbd_softc *k;
    373   1.1       gwr 
    374   1.5   thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    375   1.9       mrg 	return (ev_poll(&k->k_events, events, p));
    376   1.1       gwr }
    377   1.1       gwr 
    378   1.1       gwr 
    379   1.1       gwr static int kbd_iockeymap __P((struct kbd_state *ks,
    380   1.1       gwr 	u_long cmd, struct kiockeymap *kio));
    381   1.1       gwr 
    382  1.18       gwr static int kbd_iocsled(struct kbd_softc *k, char *data);
    383   1.7       gwr 
    384   1.7       gwr #ifdef	KIOCGETKEY
    385   1.7       gwr static int kbd_oldkeymap __P((struct kbd_state *ks,
    386   1.7       gwr 	u_long cmd, struct okiockey *okio));
    387   1.7       gwr #endif
    388   1.7       gwr 
    389   1.1       gwr int
    390   1.1       gwr kbdioctl(dev, cmd, data, flag, p)
    391   1.1       gwr 	dev_t dev;
    392   1.1       gwr 	u_long cmd;
    393   1.1       gwr 	register caddr_t data;
    394   1.1       gwr 	int flag;
    395   1.1       gwr 	struct proc *p;
    396   1.1       gwr {
    397   1.1       gwr 	struct kbd_softc *k;
    398   1.1       gwr 	struct kbd_state *ks;
    399   1.1       gwr 	int error = 0;
    400   1.1       gwr 
    401   1.5   thorpej 	k = kbd_cd.cd_devs[minor(dev)];
    402   1.1       gwr 	ks = &k->k_state;
    403   1.1       gwr 
    404   1.1       gwr 	switch (cmd) {
    405   1.1       gwr 
    406   1.1       gwr 	case KIOCTRANS: 	/* Set translation mode */
    407   1.1       gwr 		/* We only support "raw" mode on /dev/kbd */
    408  1.16       gwr 		if (*(int *)data != TR_UNTRANS_EVENT)
    409   1.1       gwr 			error = EINVAL;
    410   1.1       gwr 		break;
    411   1.1       gwr 
    412   1.1       gwr 	case KIOCGTRANS:	/* Get translation mode */
    413   1.1       gwr 		/* We only support "raw" mode on /dev/kbd */
    414  1.16       gwr 		*(int *)data = TR_UNTRANS_EVENT;
    415   1.1       gwr 		break;
    416   1.1       gwr 
    417   1.1       gwr #ifdef	KIOCGETKEY
    418   1.1       gwr 	case KIOCGETKEY:	/* Get keymap entry (old format) */
    419   1.1       gwr 		error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
    420   1.1       gwr 		break;
    421   1.1       gwr #endif	KIOCGETKEY */
    422   1.1       gwr 
    423   1.1       gwr 	case KIOCSKEY:  	/* Set keymap entry */
    424   1.1       gwr 		/* fallthrough */
    425   1.1       gwr 	case KIOCGKEY:  	/* Get keymap entry */
    426   1.1       gwr 		error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
    427   1.1       gwr 		break;
    428   1.1       gwr 
    429   1.1       gwr 	case KIOCCMD:	/* Send a command to the keyboard */
    430  1.16       gwr 		error = kbd_docmd(*(int *)data, 1);
    431   1.1       gwr 		break;
    432   1.1       gwr 
    433   1.1       gwr 	case KIOCTYPE:	/* Get keyboard type */
    434  1.16       gwr 		*(int *)data = ks->kbd_id;
    435   1.1       gwr 		break;
    436   1.1       gwr 
    437   1.1       gwr 	case KIOCSDIRECT:	/* where to send input */
    438  1.16       gwr 		k->k_evmode = *(int *)data;
    439   1.1       gwr 		break;
    440   1.1       gwr 
    441   1.1       gwr 	case KIOCLAYOUT:	/* Get keyboard layout */
    442  1.16       gwr 		*(int *)data = ks->kbd_layout;
    443   1.1       gwr 		break;
    444   1.1       gwr 
    445   1.1       gwr 	case KIOCSLED:
    446  1.18       gwr 		error = kbd_iocsled(k, (char *)data);
    447   1.1       gwr 		break;
    448   1.1       gwr 
    449   1.1       gwr 	case KIOCGLED:
    450   1.1       gwr 		*(char *)data = ks->kbd_leds;
    451   1.1       gwr 		break;
    452   1.1       gwr 
    453   1.1       gwr 	case FIONBIO:		/* we will remove this someday (soon???) */
    454   1.1       gwr 		break;
    455   1.1       gwr 
    456   1.1       gwr 	case FIOASYNC:
    457   1.1       gwr 		k->k_events.ev_async = *(int *)data != 0;
    458   1.1       gwr 		break;
    459   1.1       gwr 
    460   1.1       gwr 	case TIOCSPGRP:
    461  1.16       gwr 		if (*(int *)data != k->k_events.ev_io->p_pgid)
    462   1.1       gwr 			error = EPERM;
    463   1.1       gwr 		break;
    464   1.1       gwr 
    465  1.16       gwr 	default:
    466  1.16       gwr 		error = ENOTTY;
    467  1.16       gwr 		break;
    468   1.1       gwr 	}
    469   1.1       gwr 
    470   1.1       gwr 	return (error);
    471   1.1       gwr }
    472   1.1       gwr 
    473   1.1       gwr /****************************************************************
    474   1.1       gwr  * ioctl helpers
    475   1.1       gwr  ****************************************************************/
    476   1.1       gwr 
    477   1.1       gwr /*
    478   1.1       gwr  * Get/Set keymap entry
    479   1.1       gwr  */
    480   1.7       gwr static int
    481   1.1       gwr kbd_iockeymap(ks, cmd, kio)
    482   1.1       gwr 	struct kbd_state *ks;
    483   1.1       gwr 	u_long cmd;
    484   1.1       gwr 	struct kiockeymap *kio;
    485   1.1       gwr {
    486  1.13       gwr 	u_short *km;
    487   1.1       gwr 	u_int station;
    488   1.1       gwr 
    489   1.1       gwr 	switch (kio->kio_tablemask) {
    490   1.1       gwr 	case KIOC_NOMASK:
    491   1.1       gwr 		km = ks->kbd_k.k_normal;
    492   1.1       gwr 		break;
    493   1.1       gwr 	case KIOC_SHIFTMASK:
    494   1.1       gwr 		km = ks->kbd_k.k_shifted;
    495   1.1       gwr 		break;
    496   1.1       gwr 	case KIOC_CTRLMASK:
    497   1.1       gwr 		km = ks->kbd_k.k_control;
    498   1.1       gwr 		break;
    499   1.1       gwr 	case KIOC_UPMASK:
    500   1.1       gwr 		km = ks->kbd_k.k_release;
    501   1.1       gwr 		break;
    502   1.1       gwr 	default:
    503   1.1       gwr 		/* Silently ignore unsupported masks */
    504   1.1       gwr 		return (0);
    505   1.1       gwr 	}
    506   1.1       gwr 
    507   1.1       gwr 	/* Range-check the table position. */
    508   1.1       gwr 	station = kio->kio_station;
    509   1.1       gwr 	if (station >= KEYMAP_SIZE)
    510   1.1       gwr 		return (EINVAL);
    511   1.1       gwr 
    512   1.1       gwr 	switch (cmd) {
    513   1.1       gwr 
    514   1.1       gwr 	case KIOCGKEY:	/* Get keymap entry */
    515  1.13       gwr 		kio->kio_entry = km[station];
    516   1.1       gwr 		break;
    517   1.1       gwr 
    518   1.1       gwr 	case KIOCSKEY:	/* Set keymap entry */
    519  1.13       gwr 		km[station] = kio->kio_entry;
    520   1.1       gwr 		break;
    521   1.1       gwr 
    522   1.1       gwr 	default:
    523   1.1       gwr 		return(ENOTTY);
    524   1.1       gwr 	}
    525   1.1       gwr 	return (0);
    526   1.1       gwr }
    527   1.1       gwr 
    528   1.1       gwr #ifdef	KIOCGETKEY
    529   1.1       gwr /*
    530   1.1       gwr  * Get/Set keymap entry,
    531   1.1       gwr  * old format (compatibility)
    532   1.1       gwr  */
    533   1.1       gwr int
    534   1.1       gwr kbd_oldkeymap(ks, cmd, kio)
    535   1.1       gwr 	struct kbd_state *ks;
    536   1.1       gwr 	u_long cmd;
    537   1.1       gwr 	struct okiockey *kio;
    538   1.1       gwr {
    539   1.1       gwr 	int error = 0;
    540   1.1       gwr 
    541   1.1       gwr 	switch (cmd) {
    542   1.1       gwr 
    543   1.1       gwr 	case KIOCGETKEY:
    544   1.1       gwr 		if (kio->kio_station == 118) {
    545   1.1       gwr 			/*
    546   1.1       gwr 			 * This is X11 asking if a type 3 keyboard is
    547   1.1       gwr 			 * really a type 3 keyboard.  Say yes, it is,
    548   1.1       gwr 			 * by reporting key station 118 as a "hole".
    549   1.1       gwr 			 * Note old (SunOS 3.5) definition of HOLE!
    550   1.1       gwr 			 */
    551   1.1       gwr 			kio->kio_entry = 0xA2;
    552   1.1       gwr 			break;
    553   1.1       gwr 		}
    554   1.1       gwr 		/* fall through */
    555   1.1       gwr 
    556   1.1       gwr 	default:
    557   1.1       gwr 		error = ENOTTY;
    558   1.1       gwr 		break;
    559   1.1       gwr 	}
    560   1.1       gwr 
    561   1.1       gwr 	return (error);
    562   1.1       gwr }
    563   1.1       gwr #endif	/* KIOCGETKEY */
    564   1.1       gwr 
    565   1.7       gwr 
    566   1.7       gwr /*
    567   1.7       gwr  * keyboard command ioctl
    568   1.7       gwr  * ``unimplemented commands are ignored'' (blech)
    569  1.15       gwr  * This is also export to the fb driver.
    570   1.7       gwr  */
    571  1.15       gwr int
    572  1.15       gwr kbd_docmd(cmd, isuser)
    573  1.15       gwr 	int cmd;
    574  1.15       gwr 	int isuser;
    575  1.15       gwr {
    576   1.7       gwr 	struct kbd_softc *k;
    577  1.15       gwr 	struct kbd_state *ks;
    578  1.15       gwr 	int error, s;
    579  1.15       gwr 
    580  1.15       gwr 	error = 0;
    581  1.15       gwr 	k = kbd_cd.cd_devs[0];
    582  1.15       gwr 	ks = &k->k_state;
    583   1.7       gwr 
    584   1.7       gwr 	switch (cmd) {
    585   1.7       gwr 
    586   1.7       gwr 	case KBD_CMD_BELL:
    587   1.7       gwr 	case KBD_CMD_NOBELL:
    588   1.7       gwr 		/* Supported by type 2, 3, and 4 keyboards */
    589   1.7       gwr 		break;
    590   1.7       gwr 
    591   1.7       gwr 	case KBD_CMD_CLICK:
    592   1.7       gwr 	case KBD_CMD_NOCLICK:
    593   1.7       gwr 		/* Unsupported by type 2 keyboards */
    594   1.7       gwr 		if (ks->kbd_id <= KB_SUN2)
    595   1.7       gwr 			return (0);
    596   1.7       gwr 		ks->kbd_click = (cmd == KBD_CMD_CLICK);
    597   1.7       gwr 		break;
    598   1.7       gwr 
    599   1.7       gwr 	default:
    600   1.7       gwr 		return (0);
    601   1.7       gwr 	}
    602   1.7       gwr 
    603   1.7       gwr 	s = spltty();
    604   1.7       gwr 
    605  1.15       gwr 	if (isuser)
    606  1.15       gwr 		error = kbd_drain_tx(k);
    607  1.15       gwr 
    608   1.7       gwr 	if (error == 0) {
    609   1.7       gwr 		kbd_output(k, cmd);
    610   1.7       gwr 		kbd_start_tx(k);
    611   1.7       gwr 	}
    612   1.7       gwr 
    613   1.7       gwr 	splx(s);
    614   1.7       gwr 
    615   1.7       gwr 	return (error);
    616   1.7       gwr }
    617   1.7       gwr 
    618   1.7       gwr /*
    619   1.7       gwr  * Set LEDs ioctl.
    620   1.7       gwr  */
    621   1.7       gwr static int
    622   1.7       gwr kbd_iocsled(k, data)
    623   1.7       gwr 	struct kbd_softc *k;
    624  1.18       gwr 	char *data;
    625   1.7       gwr {
    626   1.7       gwr 	int leds, error, s;
    627   1.7       gwr 
    628   1.7       gwr 	leds = *data;
    629   1.7       gwr 
    630   1.7       gwr 	s = spltty();
    631   1.7       gwr 	error = kbd_drain_tx(k);
    632   1.7       gwr 	if (error == 0) {
    633   1.7       gwr 		kbd_set_leds(k, leds);
    634   1.7       gwr 	}
    635   1.7       gwr 	splx(s);
    636   1.7       gwr 
    637   1.7       gwr 	return (error);
    638   1.7       gwr }
    639   1.7       gwr 
    640   1.7       gwr 
    641   1.1       gwr /****************************************************************
    642   1.1       gwr  * middle layers:
    643   1.1       gwr  *  - keysym to ASCII sequence
    644   1.1       gwr  *  - raw key codes to keysym
    645   1.1       gwr  ****************************************************************/
    646   1.1       gwr 
    647  1.13       gwr static void kbd_input_string __P((struct kbd_softc *, char *));
    648  1.13       gwr static void kbd_input_funckey __P((struct kbd_softc *, int));
    649  1.17       gwr static int  kbd_input_keysym __P((struct kbd_softc *, int));
    650  1.13       gwr static void kbd_input_raw __P((struct kbd_softc *k, int));
    651   1.1       gwr 
    652   1.1       gwr /*
    653   1.1       gwr  * Initialization done by either kdcninit or kbd_iopen
    654   1.1       gwr  */
    655   1.1       gwr void
    656   1.1       gwr kbd_xlate_init(ks)
    657   1.1       gwr 	struct kbd_state *ks;
    658   1.1       gwr {
    659   1.1       gwr 	struct keyboard *ktbls;
    660   1.1       gwr 	int id;
    661   1.1       gwr 
    662   1.1       gwr 	id = ks->kbd_id;
    663   1.1       gwr 	if (id < KBD_MIN_TYPE)
    664   1.1       gwr 		id = KBD_MIN_TYPE;
    665   1.1       gwr 	if (id > kbd_max_type)
    666   1.1       gwr 		id = kbd_max_type;
    667   1.1       gwr 	ktbls = keyboards[id];
    668   1.1       gwr 
    669   1.1       gwr 	ks->kbd_k = *ktbls; 	/* struct assignment */
    670   1.1       gwr 	ks->kbd_modbits = 0;
    671   1.1       gwr }
    672   1.1       gwr 
    673   1.1       gwr /*
    674   1.1       gwr  * Turn keyboard up/down codes into a KEYSYM.
    675   1.1       gwr  * Note that the "kd" driver uses this too!
    676   1.1       gwr  */
    677   1.1       gwr int
    678   1.1       gwr kbd_code_to_keysym(ks, c)
    679   1.1       gwr 	register struct kbd_state *ks;
    680   1.1       gwr 	register int c;
    681   1.1       gwr {
    682  1.13       gwr 	u_short *km;
    683   1.1       gwr 	int keysym;
    684   1.1       gwr 
    685   1.1       gwr 	/*
    686   1.1       gwr 	 * Get keymap pointer.  One of these:
    687   1.1       gwr 	 * release, control, shifted, normal, ...
    688   1.1       gwr 	 */
    689   1.1       gwr 	if (KEY_UP(c))
    690   1.1       gwr 		km = ks->kbd_k.k_release;
    691   1.4       gwr 	else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
    692   1.4       gwr 		km = ks->kbd_k.k_control;
    693   1.4       gwr 	else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
    694   1.4       gwr 		km = ks->kbd_k.k_shifted;
    695   1.4       gwr 	else
    696   1.4       gwr 		km = ks->kbd_k.k_normal;
    697   1.4       gwr 
    698   1.1       gwr 	if (km == NULL) {
    699   1.1       gwr 		/*
    700   1.1       gwr 		 * Do not know how to translate yet.
    701   1.1       gwr 		 * We will find out when a RESET comes along.
    702   1.1       gwr 		 */
    703   1.4       gwr 		return (KEYSYM_NOP);
    704   1.4       gwr 	}
    705  1.13       gwr 	keysym = km[KEY_CODE(c)];
    706   1.4       gwr 
    707   1.4       gwr 	/*
    708   1.4       gwr 	 * Post-processing for Caps-lock
    709   1.4       gwr 	 */
    710   1.4       gwr 	if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
    711   1.4       gwr 		(KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
    712   1.4       gwr 	{
    713   1.4       gwr 		if (('a' <= keysym) && (keysym <= 'z'))
    714   1.4       gwr 			keysym -= ('a' - 'A');
    715   1.4       gwr 	}
    716   1.4       gwr 
    717   1.4       gwr 	/*
    718  1.17       gwr 	 * Post-processing for Num-lock.  All "function"
    719  1.17       gwr 	 * keysyms get indirected through another table.
    720  1.17       gwr 	 * (XXX: Only if numlock on.  Want off also!)
    721   1.4       gwr 	 */
    722   1.4       gwr 	if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
    723   1.4       gwr 		(KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
    724   1.4       gwr 	{
    725   1.4       gwr 		keysym = kbd_numlock_map[keysym & 0x3F];
    726   1.4       gwr 	}
    727   1.1       gwr 
    728   1.1       gwr 	return (keysym);
    729   1.1       gwr }
    730   1.1       gwr 
    731   1.1       gwr void
    732   1.1       gwr kbd_input_string(k, str)
    733   1.1       gwr 	struct kbd_softc *k;
    734   1.1       gwr 	char *str;
    735   1.1       gwr {
    736   1.1       gwr 	while (*str) {
    737   1.1       gwr 		kd_input(*str);
    738   1.1       gwr 		str++;
    739   1.1       gwr 	}
    740   1.1       gwr }
    741   1.1       gwr 
    742   1.1       gwr void
    743   1.1       gwr kbd_input_funckey(k, keysym)
    744   1.1       gwr 	struct kbd_softc *k;
    745   1.1       gwr 	register int keysym;
    746   1.1       gwr {
    747   1.1       gwr 	register int n;
    748   1.1       gwr 	char str[12];
    749   1.1       gwr 
    750   1.1       gwr 	/*
    751   1.1       gwr 	 * Format the F-key sequence and send as a string.
    752   1.1       gwr 	 * XXX: Ugly compatibility mappings.
    753   1.1       gwr 	 */
    754   1.1       gwr 	n = 0xC0 + (keysym & 0x3F);
    755  1.11  christos 	sprintf(str, "\033[%dz", n);
    756   1.1       gwr 	kbd_input_string(k, str);
    757   1.1       gwr }
    758   1.1       gwr 
    759   1.1       gwr /*
    760   1.1       gwr  * This is called by kbd_input_raw() or by kb_repeat()
    761   1.7       gwr  * to deliver ASCII input.  Called at spltty().
    762  1.17       gwr  *
    763  1.17       gwr  * Return zero on success, else the keysym that we
    764  1.17       gwr  * could not handle (so the caller may complain).
    765   1.1       gwr  */
    766  1.17       gwr int
    767   1.1       gwr kbd_input_keysym(k, keysym)
    768   1.1       gwr 	struct kbd_softc *k;
    769   1.1       gwr 	register int keysym;
    770   1.1       gwr {
    771   1.1       gwr 	struct kbd_state *ks = &k->k_state;
    772   1.4       gwr 	register int data;
    773   1.1       gwr 
    774   1.4       gwr 	switch (KEYSYM_CLASS(keysym)) {
    775   1.1       gwr 
    776   1.1       gwr 	case KEYSYM_ASCII:
    777   1.1       gwr 		data = KEYSYM_DATA(keysym);
    778   1.1       gwr 		if (ks->kbd_modbits & KBMOD_META_MASK)
    779   1.1       gwr 			data |= 0x80;
    780   1.1       gwr 		kd_input(data);
    781   1.1       gwr 		break;
    782   1.1       gwr 
    783   1.1       gwr 	case KEYSYM_STRING:
    784   1.1       gwr 		data = keysym & 0xF;
    785   1.1       gwr 		kbd_input_string(k, kbd_stringtab[data]);
    786   1.1       gwr 		break;
    787   1.1       gwr 
    788   1.1       gwr 	case KEYSYM_FUNC:
    789   1.1       gwr 		kbd_input_funckey(k, keysym);
    790   1.1       gwr 		break;
    791   1.1       gwr 
    792   1.1       gwr 	case KEYSYM_CLRMOD:
    793   1.1       gwr 		data = 1 << (keysym & 0x1F);
    794   1.1       gwr 		ks->kbd_modbits &= ~data;
    795   1.1       gwr 		break;
    796   1.1       gwr 
    797   1.1       gwr 	case KEYSYM_SETMOD:
    798   1.1       gwr 		data = 1 << (keysym & 0x1F);
    799   1.1       gwr 		ks->kbd_modbits |= data;
    800   1.1       gwr 		break;
    801   1.1       gwr 
    802   1.1       gwr 	case KEYSYM_INVMOD:
    803   1.1       gwr 		data = 1 << (keysym & 0x1F);
    804   1.1       gwr 		ks->kbd_modbits ^= data;
    805   1.4       gwr 		kbd_update_leds(k);
    806   1.1       gwr 		break;
    807   1.1       gwr 
    808   1.1       gwr 	case KEYSYM_ALL_UP:
    809   1.1       gwr 		ks->kbd_modbits &= ~0xFFFF;
    810   1.1       gwr 		break;
    811   1.1       gwr 
    812   1.1       gwr 	case KEYSYM_SPECIAL:
    813   1.1       gwr 		if (keysym == KEYSYM_NOP)
    814   1.1       gwr 			break;
    815   1.1       gwr 		/* fall through */
    816   1.1       gwr 	default:
    817  1.17       gwr 		/* We could not handle it. */
    818  1.17       gwr 		return (keysym);
    819   1.1       gwr 	}
    820  1.17       gwr 	return (0);
    821   1.1       gwr }
    822   1.1       gwr 
    823   1.1       gwr /*
    824   1.1       gwr  * This is the autorepeat timeout function.
    825   1.7       gwr  * Called at splsoftclock().
    826   1.1       gwr  */
    827  1.13       gwr static void
    828   1.1       gwr kbd_repeat(void *arg)
    829   1.1       gwr {
    830   1.1       gwr 	struct kbd_softc *k = (struct kbd_softc *)arg;
    831   1.7       gwr 	int s = spltty();
    832   1.1       gwr 
    833   1.1       gwr 	if (k->k_repeating && k->k_repeatsym >= 0) {
    834  1.17       gwr 		(void)kbd_input_keysym(k, k->k_repeatsym);
    835   1.1       gwr 		timeout(kbd_repeat, k, k->k_repeat_step);
    836   1.1       gwr 	}
    837   1.7       gwr 	splx(s);
    838   1.1       gwr }
    839   1.1       gwr 
    840   1.1       gwr /*
    841   1.1       gwr  * Called by our kbd_softint() routine on input,
    842   1.1       gwr  * which passes the raw hardware scan codes.
    843   1.7       gwr  * Called at spltty()
    844   1.1       gwr  */
    845   1.1       gwr void
    846   1.1       gwr kbd_input_raw(k, c)
    847   1.1       gwr 	struct kbd_softc *k;
    848   1.1       gwr 	register int c;
    849   1.1       gwr {
    850   1.1       gwr 	struct kbd_state *ks = &k->k_state;
    851   1.1       gwr 	struct firm_event *fe;
    852   1.1       gwr 	int put, keysym;
    853   1.1       gwr 
    854   1.1       gwr 	/* XXX - Input errors already handled. */
    855   1.1       gwr 
    856   1.1       gwr 	/* Are we expecting special input? */
    857   1.1       gwr 	if (ks->kbd_expect) {
    858   1.1       gwr 		if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
    859   1.1       gwr 			/* We read a KBD_RESET last time. */
    860   1.1       gwr 			ks->kbd_id = c;
    861   1.1       gwr 			kbd_was_reset(k);
    862   1.1       gwr 		}
    863   1.1       gwr 		if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
    864   1.1       gwr 			/* We read a KBD_LAYOUT last time. */
    865   1.1       gwr 			ks->kbd_layout = c;
    866   1.1       gwr 			kbd_new_layout(k);
    867   1.1       gwr 		}
    868   1.1       gwr 		ks->kbd_expect = 0;
    869   1.1       gwr 		return;
    870   1.1       gwr 	}
    871   1.1       gwr 
    872   1.1       gwr 	/* Is this one of the "special" input codes? */
    873   1.1       gwr 	if (KBD_SPECIAL(c)) {
    874   1.1       gwr 		switch (c) {
    875   1.1       gwr 		case KBD_RESET:
    876   1.1       gwr 			ks->kbd_expect |= KBD_EXPECT_IDCODE;
    877   1.1       gwr 			/* Fake an "all-up" to resync. translation. */
    878   1.1       gwr 			c = KBD_IDLE;
    879   1.1       gwr 			break;
    880   1.1       gwr 
    881   1.1       gwr 		case KBD_LAYOUT:
    882   1.1       gwr 			ks->kbd_expect |= KBD_EXPECT_LAYOUT;
    883   1.1       gwr 			return;
    884   1.1       gwr 
    885   1.1       gwr 		case KBD_ERROR:
    886   1.1       gwr 			log(LOG_WARNING, "%s: received error indicator\n",
    887   1.1       gwr 				k->k_dev.dv_xname);
    888   1.1       gwr 			return;
    889   1.1       gwr 
    890   1.1       gwr 		case KBD_IDLE:
    891   1.1       gwr 			/* Let this go to the translator. */
    892   1.1       gwr 			break;
    893   1.1       gwr 		}
    894   1.1       gwr 	}
    895   1.1       gwr 
    896   1.1       gwr 	/*
    897   1.1       gwr 	 * If /dev/kbd is not connected in event mode,
    898   1.1       gwr 	 * translate and send upstream (to console).
    899   1.1       gwr 	 */
    900   1.1       gwr 	if (!k->k_evmode) {
    901   1.1       gwr 
    902   1.1       gwr 		/* Any input stops auto-repeat (i.e. key release). */
    903   1.1       gwr 		if (k->k_repeating) {
    904   1.1       gwr 			k->k_repeating = 0;
    905   1.1       gwr 			untimeout(kbd_repeat, k);
    906   1.1       gwr 		}
    907   1.1       gwr 
    908   1.1       gwr 		/* Translate this code to a keysym */
    909   1.1       gwr 		keysym = kbd_code_to_keysym(ks, c);
    910   1.1       gwr 
    911   1.1       gwr 		/* Pass up to the next layer. */
    912  1.17       gwr 		if (kbd_input_keysym(k, keysym)) {
    913  1.17       gwr 			log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
    914  1.17       gwr 				" produced unexpected keysym 0x%x\n",
    915  1.17       gwr 				k->k_dev.dv_xname, c,
    916  1.17       gwr 				ks->kbd_modbits, keysym);
    917  1.17       gwr 			/* No point in auto-repeat here. */
    918  1.17       gwr 			return;
    919  1.17       gwr 		}
    920   1.1       gwr 
    921   1.1       gwr 		/* Does this symbol get auto-repeat? */
    922   1.1       gwr 		if (KEYSYM_NOREPEAT(keysym))
    923   1.1       gwr 			return;
    924   1.1       gwr 
    925   1.1       gwr 		/* Setup for auto-repeat after initial delay. */
    926   1.1       gwr 		k->k_repeating = 1;
    927   1.1       gwr 		k->k_repeatsym = keysym;
    928   1.1       gwr 		timeout(kbd_repeat, k, k->k_repeat_start);
    929   1.1       gwr 		return;
    930   1.1       gwr 	}
    931   1.1       gwr 
    932   1.1       gwr 	/*
    933   1.1       gwr 	 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
    934   1.1       gwr 	 * This is bad as it means the server will not automatically resync
    935   1.1       gwr 	 * on all-up IDLEs, but I did not drop them before, and the server
    936   1.1       gwr 	 * goes crazy when it comes time to blank the screen....
    937   1.1       gwr 	 */
    938   1.1       gwr 	if (c == KBD_IDLE)
    939   1.1       gwr 		return;
    940   1.1       gwr 
    941   1.1       gwr 	/*
    942   1.1       gwr 	 * Keyboard is generating events.  Turn this keystroke into an
    943   1.1       gwr 	 * event and put it in the queue.  If the queue is full, the
    944   1.1       gwr 	 * keystroke is lost (sorry!).
    945   1.1       gwr 	 */
    946   1.1       gwr 	put = k->k_events.ev_put;
    947   1.1       gwr 	fe = &k->k_events.ev_q[put];
    948   1.1       gwr 	put = (put + 1) % EV_QSIZE;
    949   1.1       gwr 	if (put == k->k_events.ev_get) {
    950   1.1       gwr 		log(LOG_WARNING, "%s: event queue overflow\n",
    951   1.1       gwr 			k->k_dev.dv_xname); /* ??? */
    952   1.1       gwr 		return;
    953   1.1       gwr 	}
    954   1.1       gwr 	fe->id = KEY_CODE(c);
    955   1.1       gwr 	fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    956   1.1       gwr 	fe->time = time;
    957   1.1       gwr 	k->k_events.ev_put = put;
    958   1.1       gwr 	EV_WAKEUP(&k->k_events);
    959   1.1       gwr }
    960   1.1       gwr 
    961   1.1       gwr /****************************************************************
    962   1.1       gwr  * Interface to the lower layer (zscc)
    963   1.1       gwr  ****************************************************************/
    964   1.1       gwr 
    965  1.13       gwr static void kbd_rxint __P((struct zs_chanstate *));
    966  1.13       gwr static void kbd_txint __P((struct zs_chanstate *));
    967  1.13       gwr static void kbd_stint __P((struct zs_chanstate *));
    968  1.13       gwr static void kbd_softint __P((struct zs_chanstate *));
    969  1.13       gwr 
    970   1.7       gwr static void
    971   1.1       gwr kbd_rxint(cs)
    972   1.1       gwr 	register struct zs_chanstate *cs;
    973   1.1       gwr {
    974   1.1       gwr 	register struct kbd_softc *k;
    975   1.1       gwr 	register int put, put_next;
    976   1.1       gwr 	register u_char c, rr1;
    977   1.1       gwr 
    978   1.1       gwr 	k = cs->cs_private;
    979   1.1       gwr 	put = k->k_rbput;
    980   1.1       gwr 
    981   1.7       gwr 	/*
    982   1.7       gwr 	 * First read the status, because reading the received char
    983   1.7       gwr 	 * destroys the status of this char.
    984   1.7       gwr 	 */
    985   1.7       gwr 	rr1 = zs_read_reg(cs, 1);
    986   1.2       gwr 	c = zs_read_data(cs);
    987   1.1       gwr 
    988   1.1       gwr 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    989   1.1       gwr 		/* Clear the receive error. */
    990   1.2       gwr 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    991   1.1       gwr 	}
    992   1.1       gwr 
    993   1.1       gwr 	/*
    994   1.1       gwr 	 * Check NOW for a console abort sequence, so that we can
    995   1.1       gwr 	 * abort even when interrupts are locking up the machine.
    996   1.1       gwr 	 */
    997   1.1       gwr 	if (k->k_magic1_down) {
    998   1.1       gwr 		/* The last keycode was "MAGIC1" down. */
    999   1.1       gwr 		k->k_magic1_down = 0;
   1000  1.17       gwr 		if (c == k->k_magic2) {
   1001   1.1       gwr 			/* Magic "L1-A" sequence; enter debugger. */
   1002  1.17       gwr 			if (k->k_isconsole) {
   1003  1.17       gwr 				zs_abort(cs);
   1004  1.17       gwr 				/* Debugger done.  Fake L1-up to finish it. */
   1005  1.17       gwr 				c = k->k_magic1 | KBD_UP;
   1006  1.17       gwr 			} else {
   1007  1.17       gwr 				printf("kbd: magic sequence, but not console\n");
   1008  1.17       gwr 			}
   1009   1.1       gwr 		}
   1010   1.1       gwr 	}
   1011   1.1       gwr 	if (c == k->k_magic1) {
   1012   1.1       gwr 		k->k_magic1_down = 1;
   1013   1.1       gwr 	}
   1014   1.1       gwr 
   1015   1.1       gwr 	k->k_rbuf[put] = (c << 8) | rr1;
   1016   1.1       gwr 	put_next = (put + 1) & KBD_RX_RING_MASK;
   1017   1.1       gwr 
   1018   1.1       gwr 	/* Would overrun if increment makes (put==get). */
   1019   1.1       gwr 	if (put_next == k->k_rbget) {
   1020   1.1       gwr 		k->k_intr_flags |= INTR_RX_OVERRUN;
   1021   1.1       gwr 	} else {
   1022   1.1       gwr 		/* OK, really increment. */
   1023   1.1       gwr 		put = put_next;
   1024   1.1       gwr 	}
   1025   1.1       gwr 
   1026   1.1       gwr 	/* Done reading. */
   1027   1.1       gwr 	k->k_rbput = put;
   1028   1.1       gwr 
   1029   1.1       gwr 	/* Ask for softint() call. */
   1030   1.1       gwr 	cs->cs_softreq = 1;
   1031   1.1       gwr }
   1032   1.1       gwr 
   1033   1.1       gwr 
   1034   1.7       gwr static void
   1035   1.1       gwr kbd_txint(cs)
   1036   1.1       gwr 	register struct zs_chanstate *cs;
   1037   1.1       gwr {
   1038   1.1       gwr 	register struct kbd_softc *k;
   1039   1.1       gwr 
   1040   1.1       gwr 	k = cs->cs_private;
   1041   1.2       gwr 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
   1042   1.1       gwr 	k->k_intr_flags |= INTR_TX_EMPTY;
   1043   1.1       gwr 	/* Ask for softint() call. */
   1044   1.1       gwr 	cs->cs_softreq = 1;
   1045   1.1       gwr }
   1046   1.1       gwr 
   1047   1.1       gwr 
   1048   1.7       gwr static void
   1049   1.1       gwr kbd_stint(cs)
   1050   1.1       gwr 	register struct zs_chanstate *cs;
   1051   1.1       gwr {
   1052   1.1       gwr 	register struct kbd_softc *k;
   1053   1.1       gwr 	register int rr0;
   1054   1.1       gwr 
   1055   1.1       gwr 	k = cs->cs_private;
   1056   1.1       gwr 
   1057  1.12       gwr 	rr0 = zs_read_csr(cs);
   1058   1.2       gwr 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
   1059   1.1       gwr 
   1060   1.1       gwr #if 0
   1061   1.1       gwr 	if (rr0 & ZSRR0_BREAK) {
   1062   1.1       gwr 		/* Keyboard unplugged? */
   1063  1.13       gwr 		zs_abort(cs);
   1064   1.1       gwr 		return (0);
   1065   1.1       gwr 	}
   1066   1.1       gwr #endif
   1067   1.1       gwr 
   1068  1.12       gwr 	/*
   1069  1.12       gwr 	 * We have to accumulate status line changes here.
   1070  1.12       gwr 	 * Otherwise, if we get multiple status interrupts
   1071  1.12       gwr 	 * before the softint runs, we could fail to notice
   1072  1.12       gwr 	 * some status line changes in the softint routine.
   1073  1.12       gwr 	 * Fix from Bill Studenmund, October 1996.
   1074  1.12       gwr 	 */
   1075  1.12       gwr 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
   1076  1.12       gwr 	cs->cs_rr0 = rr0;
   1077   1.1       gwr 	k->k_intr_flags |= INTR_ST_CHECK;
   1078  1.12       gwr 
   1079   1.1       gwr 	/* Ask for softint() call. */
   1080   1.1       gwr 	cs->cs_softreq = 1;
   1081   1.1       gwr }
   1082   1.1       gwr 
   1083   1.1       gwr /*
   1084   1.1       gwr  * Get input from the recieve ring and pass it on.
   1085   1.1       gwr  * Note: this is called at splsoftclock()
   1086   1.1       gwr  */
   1087   1.7       gwr static void
   1088   1.1       gwr kbd_softint(cs)
   1089   1.1       gwr 	struct zs_chanstate *cs;
   1090   1.1       gwr {
   1091   1.1       gwr 	register struct kbd_softc *k;
   1092   1.1       gwr 	register int get, c, s;
   1093   1.1       gwr 	int intr_flags;
   1094   1.1       gwr 	register u_short ring_data;
   1095   1.1       gwr 
   1096   1.1       gwr 	k = cs->cs_private;
   1097   1.1       gwr 
   1098   1.1       gwr 	/* Atomically get and clear flags. */
   1099   1.1       gwr 	s = splzs();
   1100   1.1       gwr 	intr_flags = k->k_intr_flags;
   1101   1.1       gwr 	k->k_intr_flags = 0;
   1102   1.7       gwr 
   1103   1.7       gwr 	/* Now lower to spltty for the rest. */
   1104   1.7       gwr 	(void) spltty();
   1105   1.1       gwr 
   1106   1.1       gwr 	/*
   1107   1.1       gwr 	 * Copy data from the receive ring to the event layer.
   1108   1.1       gwr 	 */
   1109   1.1       gwr 	get = k->k_rbget;
   1110   1.1       gwr 	while (get != k->k_rbput) {
   1111   1.1       gwr 		ring_data = k->k_rbuf[get];
   1112   1.1       gwr 		get = (get + 1) & KBD_RX_RING_MASK;
   1113   1.1       gwr 
   1114   1.1       gwr 		/* low byte of ring_data is rr1 */
   1115   1.1       gwr 		c = (ring_data >> 8) & 0xff;
   1116   1.1       gwr 
   1117   1.1       gwr 		if (ring_data & ZSRR1_DO)
   1118   1.1       gwr 			intr_flags |= INTR_RX_OVERRUN;
   1119   1.1       gwr 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
   1120   1.1       gwr 			/*
   1121   1.1       gwr 			 * After garbage, flush pending input, and
   1122   1.1       gwr 			 * send a reset to resync key translation.
   1123   1.1       gwr 			 */
   1124   1.1       gwr 			log(LOG_ERR, "%s: input error (0x%x)\n",
   1125   1.1       gwr 				k->k_dev.dv_xname, ring_data);
   1126   1.1       gwr 			get = k->k_rbput; /* flush */
   1127   1.1       gwr 			goto send_reset;
   1128   1.1       gwr 		}
   1129   1.1       gwr 
   1130   1.1       gwr 		/* Pass this up to the "middle" layer. */
   1131   1.1       gwr 		kbd_input_raw(k, c);
   1132   1.1       gwr 	}
   1133   1.1       gwr 	if (intr_flags & INTR_RX_OVERRUN) {
   1134   1.1       gwr 		log(LOG_ERR, "%s: input overrun\n",
   1135   1.1       gwr 		    k->k_dev.dv_xname);
   1136   1.1       gwr 	send_reset:
   1137   1.1       gwr 		/* Send a reset to resync translation. */
   1138   1.1       gwr 		kbd_output(k, KBD_CMD_RESET);
   1139   1.1       gwr 		kbd_start_tx(k);
   1140   1.1       gwr 	}
   1141   1.1       gwr 	k->k_rbget = get;
   1142   1.1       gwr 
   1143   1.1       gwr 	if (intr_flags & INTR_TX_EMPTY) {
   1144   1.1       gwr 		/*
   1145   1.1       gwr 		 * Transmit done.  Try to send more, or
   1146   1.1       gwr 		 * clear busy and wakeup drain waiters.
   1147   1.1       gwr 		 */
   1148   1.1       gwr 		k->k_txflags &= ~K_TXBUSY;
   1149   1.1       gwr 		kbd_start_tx(k);
   1150   1.1       gwr 	}
   1151   1.1       gwr 
   1152   1.1       gwr 	if (intr_flags & INTR_ST_CHECK) {
   1153   1.1       gwr 		/*
   1154   1.1       gwr 		 * Status line change.  (Not expected.)
   1155   1.1       gwr 		 */
   1156   1.1       gwr 		log(LOG_ERR, "%s: status interrupt?\n",
   1157   1.1       gwr 		    k->k_dev.dv_xname);
   1158  1.12       gwr 		cs->cs_rr0_delta = 0;
   1159   1.1       gwr 	}
   1160   1.1       gwr 
   1161   1.7       gwr 	splx(s);
   1162   1.1       gwr }
   1163   1.1       gwr 
   1164   1.1       gwr struct zsops zsops_kbd = {
   1165   1.1       gwr 	kbd_rxint,	/* receive char available */
   1166   1.1       gwr 	kbd_stint,	/* external/status */
   1167   1.1       gwr 	kbd_txint,	/* xmit buffer empty */
   1168   1.1       gwr 	kbd_softint,	/* process software interrupt */
   1169   1.1       gwr };
   1170   1.1       gwr 
   1171   1.1       gwr /****************************************************************
   1172   1.1       gwr  * misc...
   1173   1.1       gwr  ****************************************************************/
   1174   1.1       gwr 
   1175   1.1       gwr /*
   1176   1.1       gwr  * Initialization to be done at first open.
   1177   1.1       gwr  * This is called from kbdopen or kdopen (in kd.c)
   1178   1.7       gwr  * Called with user context.
   1179   1.1       gwr  */
   1180   1.1       gwr int
   1181   1.1       gwr kbd_iopen(unit)
   1182   1.1       gwr 	int unit;
   1183   1.1       gwr {
   1184   1.1       gwr 	struct kbd_softc *k;
   1185   1.1       gwr 	struct kbd_state *ks;
   1186   1.1       gwr 	int error, s;
   1187   1.1       gwr 
   1188   1.5   thorpej 	if (unit >= kbd_cd.cd_ndevs)
   1189   1.1       gwr 		return (ENXIO);
   1190   1.5   thorpej 	k = kbd_cd.cd_devs[unit];
   1191   1.1       gwr 	if (k == NULL)
   1192   1.1       gwr 		return (ENXIO);
   1193   1.1       gwr 	ks = &k->k_state;
   1194   1.1       gwr 	error = 0;
   1195   1.1       gwr 
   1196   1.1       gwr 	/* Tolerate extra calls. */
   1197   1.1       gwr 	if (k->k_isopen)
   1198   1.1       gwr 		return (error);
   1199   1.1       gwr 
   1200   1.1       gwr 	s = spltty();
   1201   1.1       gwr 
   1202   1.1       gwr 	/* Reset the keyboard and find out its type. */
   1203   1.1       gwr 	kbd_output(k, KBD_CMD_RESET);
   1204   1.1       gwr 	kbd_start_tx(k);
   1205   1.1       gwr 	kbd_drain_tx(k);
   1206   1.1       gwr 	/* The wakeup for this is in kbd_was_reset(). */
   1207   1.1       gwr 	error = tsleep((caddr_t)&ks->kbd_id,
   1208   1.1       gwr 				   PZERO | PCATCH, devopn, hz);
   1209   1.1       gwr 	if (error == EWOULDBLOCK) { 	/* no response */
   1210   1.1       gwr 		error = 0;
   1211   1.1       gwr 		log(LOG_ERR, "%s: reset failed\n",
   1212   1.1       gwr 			k->k_dev.dv_xname);
   1213   1.1       gwr 		/*
   1214   1.1       gwr 		 * Allow the open anyway (to keep getty happy)
   1215   1.1       gwr 		 * but assume the "least common denominator".
   1216   1.1       gwr 		 */
   1217   1.1       gwr 		ks->kbd_id = KB_SUN2;
   1218   1.1       gwr 	}
   1219   1.1       gwr 
   1220  1.17       gwr 	/* Initialize the table pointers for this type. */
   1221  1.17       gwr 	kbd_xlate_init(ks);
   1222  1.17       gwr 
   1223   1.1       gwr 	/* Earlier than type 4 does not know "layout". */
   1224   1.1       gwr 	if (ks->kbd_id < KB_SUN4)
   1225   1.1       gwr 		goto out;
   1226   1.1       gwr 
   1227   1.1       gwr 	/* Ask for the layout. */
   1228   1.1       gwr 	kbd_output(k, KBD_CMD_GETLAYOUT);
   1229   1.1       gwr 	kbd_start_tx(k);
   1230   1.1       gwr 	kbd_drain_tx(k);
   1231   1.1       gwr 	/* The wakeup for this is in kbd_new_layout(). */
   1232   1.1       gwr 	error = tsleep((caddr_t)&ks->kbd_layout,
   1233   1.1       gwr 				   PZERO | PCATCH, devopn, hz);
   1234   1.1       gwr 	if (error == EWOULDBLOCK) { 	/* no response */
   1235   1.1       gwr 		error = 0;
   1236   1.1       gwr 		log(LOG_ERR, "%s: no response to get_layout\n",
   1237   1.1       gwr 			k->k_dev.dv_xname);
   1238   1.1       gwr 		ks->kbd_layout = 0;
   1239   1.1       gwr 	}
   1240   1.1       gwr 
   1241   1.1       gwr out:
   1242   1.1       gwr 	splx(s);
   1243   1.1       gwr 
   1244   1.1       gwr 	if (error == 0)
   1245   1.1       gwr 		k->k_isopen = 1;
   1246   1.1       gwr 
   1247   1.1       gwr 	return error;
   1248   1.1       gwr }
   1249   1.1       gwr 
   1250   1.7       gwr /*
   1251   1.7       gwr  * Called by kbd_input_raw, at spltty()
   1252   1.7       gwr  */
   1253  1.13       gwr static void
   1254   1.1       gwr kbd_was_reset(k)
   1255   1.1       gwr 	struct kbd_softc *k;
   1256   1.1       gwr {
   1257   1.1       gwr 	struct kbd_state *ks = &k->k_state;
   1258   1.1       gwr 
   1259   1.1       gwr 	/*
   1260   1.1       gwr 	 * On first identification, wake up anyone waiting for type
   1261   1.1       gwr 	 * and set up the table pointers.
   1262   1.1       gwr 	 */
   1263   1.1       gwr 	wakeup((caddr_t)&ks->kbd_id);
   1264   1.1       gwr 
   1265   1.1       gwr 	/* Restore keyclick, if necessary */
   1266   1.1       gwr 	switch (ks->kbd_id) {
   1267   1.1       gwr 
   1268   1.1       gwr 	case KB_SUN2:
   1269   1.1       gwr 		/* Type 2 keyboards don't support keyclick */
   1270   1.1       gwr 		break;
   1271   1.1       gwr 
   1272   1.1       gwr 	case KB_SUN3:
   1273   1.1       gwr 		/* Type 3 keyboards come up with keyclick on */
   1274   1.7       gwr 		if (!ks->kbd_click) {
   1275   1.7       gwr 			/* turn off the click */
   1276   1.7       gwr 			kbd_output(k, KBD_CMD_NOCLICK);
   1277   1.7       gwr 			kbd_start_tx(k);
   1278   1.7       gwr 		}
   1279   1.1       gwr 		break;
   1280   1.1       gwr 
   1281   1.1       gwr 	case KB_SUN4:
   1282   1.1       gwr 		/* Type 4 keyboards come up with keyclick off */
   1283   1.7       gwr 		if (ks->kbd_click) {
   1284   1.7       gwr 			/* turn on the click */
   1285   1.7       gwr 			kbd_output(k, KBD_CMD_CLICK);
   1286   1.7       gwr 			kbd_start_tx(k);
   1287   1.7       gwr 		}
   1288   1.1       gwr 		break;
   1289   1.1       gwr 	}
   1290   1.1       gwr 
   1291   1.1       gwr 	/* LEDs are off after reset. */
   1292   1.1       gwr 	ks->kbd_leds = 0;
   1293   1.1       gwr }
   1294   1.1       gwr 
   1295   1.7       gwr /*
   1296   1.7       gwr  * Called by kbd_input_raw, at spltty()
   1297   1.7       gwr  */
   1298  1.13       gwr static void
   1299   1.1       gwr kbd_new_layout(k)
   1300   1.1       gwr 	struct kbd_softc *k;
   1301   1.1       gwr {
   1302   1.1       gwr 	struct kbd_state *ks = &k->k_state;
   1303   1.1       gwr 
   1304   1.1       gwr 	/*
   1305   1.1       gwr 	 * On first identification, wake up anyone waiting for type
   1306   1.1       gwr 	 * and set up the table pointers.
   1307   1.1       gwr 	 */
   1308   1.1       gwr 	wakeup((caddr_t)&ks->kbd_layout);
   1309   1.1       gwr 
   1310   1.1       gwr 	/* XXX: switch decoding tables? */
   1311   1.1       gwr }
   1312   1.1       gwr 
   1313   1.1       gwr 
   1314   1.1       gwr /*
   1315   1.1       gwr  * Wait for output to finish.
   1316   1.7       gwr  * Called at spltty().  Has user context.
   1317   1.1       gwr  */
   1318  1.13       gwr static int
   1319   1.1       gwr kbd_drain_tx(k)
   1320   1.1       gwr 	struct kbd_softc *k;
   1321   1.1       gwr {
   1322   1.7       gwr 	int error;
   1323   1.1       gwr 
   1324   1.1       gwr 	error = 0;
   1325   1.7       gwr 
   1326   1.1       gwr 	while (k->k_txflags & K_TXBUSY) {
   1327   1.1       gwr 		k->k_txflags |= K_TXWANT;
   1328   1.1       gwr 		error = tsleep((caddr_t)&k->k_txflags,
   1329   1.1       gwr 					   PZERO | PCATCH, "kbdout", 0);
   1330   1.1       gwr 	}
   1331   1.7       gwr 
   1332   1.1       gwr 	return (error);
   1333   1.1       gwr }
   1334   1.1       gwr 
   1335   1.1       gwr /*
   1336   1.7       gwr  * Enqueue some output for the keyboard
   1337   1.7       gwr  * Called at spltty().
   1338   1.1       gwr  */
   1339  1.13       gwr static void
   1340   1.1       gwr kbd_output(k, c)
   1341   1.1       gwr 	struct kbd_softc *k;
   1342   1.1       gwr 	int c;	/* the data */
   1343   1.1       gwr {
   1344   1.7       gwr 	int put;
   1345   1.1       gwr 
   1346   1.1       gwr 	put = k->k_tbput;
   1347   1.1       gwr 	k->k_tbuf[put] = (u_char)c;
   1348   1.1       gwr 	put = (put + 1) & KBD_TX_RING_MASK;
   1349   1.1       gwr 
   1350   1.1       gwr 	/* Would overrun if increment makes (put==get). */
   1351   1.1       gwr 	if (put == k->k_tbget) {
   1352   1.1       gwr 		log(LOG_WARNING, "%s: output overrun\n",
   1353   1.1       gwr             k->k_dev.dv_xname);
   1354   1.1       gwr 	} else {
   1355   1.1       gwr 		/* OK, really increment. */
   1356   1.1       gwr 		k->k_tbput = put;
   1357   1.1       gwr 	}
   1358   1.1       gwr }
   1359   1.1       gwr 
   1360   1.7       gwr /*
   1361   1.7       gwr  * Start the sending data from the output queue
   1362   1.7       gwr  * Called at spltty().
   1363   1.7       gwr  */
   1364  1.13       gwr static void
   1365   1.1       gwr kbd_start_tx(k)
   1366   1.1       gwr     struct kbd_softc *k;
   1367   1.1       gwr {
   1368   1.1       gwr 	struct zs_chanstate *cs = k->k_cs;
   1369   1.1       gwr 	int get, s;
   1370   1.1       gwr 	u_char c;
   1371   1.1       gwr 
   1372   1.1       gwr 	if (k->k_txflags & K_TXBUSY)
   1373   1.7       gwr 		return;
   1374   1.1       gwr 
   1375   1.1       gwr 	/* Is there anything to send? */
   1376   1.1       gwr 	get = k->k_tbget;
   1377   1.1       gwr 	if (get == k->k_tbput) {
   1378   1.1       gwr 		/* Nothing to send.  Wake drain waiters. */
   1379   1.1       gwr 		if (k->k_txflags & K_TXWANT) {
   1380   1.1       gwr 			k->k_txflags &= ~K_TXWANT;
   1381   1.1       gwr 			wakeup((caddr_t)&k->k_txflags);
   1382   1.1       gwr 		}
   1383   1.7       gwr 		return;
   1384   1.1       gwr 	}
   1385   1.1       gwr 
   1386   1.1       gwr 	/* Have something to send. */
   1387   1.1       gwr 	c = k->k_tbuf[get];
   1388   1.1       gwr 	get = (get + 1) & KBD_TX_RING_MASK;
   1389   1.1       gwr 	k->k_tbget = get;
   1390   1.1       gwr 	k->k_txflags |= K_TXBUSY;
   1391   1.1       gwr 
   1392   1.1       gwr 	/* Need splzs to avoid interruption of the delay. */
   1393   1.7       gwr 	s = splzs();
   1394   1.2       gwr 	zs_write_data(cs, c);
   1395   1.1       gwr 	splx(s);
   1396   1.1       gwr }
   1397   1.1       gwr 
   1398   1.7       gwr /*
   1399   1.7       gwr  * Called at spltty by:
   1400   1.7       gwr  * kbd_update_leds, kbd_iocsled
   1401   1.7       gwr  */
   1402  1.13       gwr static void
   1403   1.4       gwr kbd_set_leds(k, new_leds)
   1404   1.1       gwr 	struct kbd_softc *k;
   1405   1.4       gwr 	int new_leds;
   1406   1.1       gwr {
   1407   1.1       gwr 	struct kbd_state *ks = &k->k_state;
   1408   1.1       gwr 
   1409   1.1       gwr 	/* Don't send unless state changes. */
   1410   1.1       gwr 	if (ks->kbd_leds == new_leds)
   1411   1.7       gwr 		return;
   1412   1.7       gwr 
   1413   1.1       gwr 	ks->kbd_leds = new_leds;
   1414   1.1       gwr 
   1415   1.1       gwr 	/* Only type 4 and later has LEDs anyway. */
   1416  1.19   mycroft 	if (ks->kbd_id < KB_SUN4)
   1417   1.7       gwr 		return;
   1418   1.1       gwr 
   1419   1.1       gwr 	kbd_output(k, KBD_CMD_SETLED);
   1420   1.1       gwr 	kbd_output(k, new_leds);
   1421   1.1       gwr 	kbd_start_tx(k);
   1422   1.4       gwr }
   1423   1.4       gwr 
   1424   1.7       gwr /*
   1425   1.7       gwr  * Called at spltty by:
   1426   1.7       gwr  * kbd_input_keysym
   1427   1.7       gwr  */
   1428  1.13       gwr static void
   1429   1.4       gwr kbd_update_leds(k)
   1430   1.4       gwr     struct kbd_softc *k;
   1431   1.4       gwr {
   1432  1.17       gwr 	struct kbd_state *ks = &k->k_state;
   1433  1.17       gwr 	register char leds;
   1434   1.4       gwr 
   1435   1.4       gwr 	leds = ks->kbd_leds;
   1436   1.4       gwr 	leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
   1437   1.4       gwr 
   1438   1.4       gwr 	if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
   1439   1.4       gwr 		leds |= LED_CAPS_LOCK;
   1440   1.4       gwr 	if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
   1441   1.4       gwr 		leds |= LED_NUM_LOCK;
   1442   1.4       gwr 
   1443   1.4       gwr 	kbd_set_leds(k, leds);
   1444   1.1       gwr }
   1445   1.1       gwr 
   1446