/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kbd.c 8.2 (Berkeley) 10/30/93
*/
/*
* LK200/LK400 keyboard attached with channel A of the 2nd SCC
*/
/*
* How many input characters we can buffer.
* The port-specific var.h may override this.
* Note: must be a power of two!
*/
#define ZSKBD_RX_RING_SIZE 256
#define ZSKBD_RX_RING_MASK (ZSKBD_RX_RING_SIZE-1)
/*
* Output buffer. Only need a few chars.
*/
#define ZSKBD_TX_RING_SIZE 16
#define ZSKBD_TX_RING_MASK (ZSKBD_TX_RING_SIZE-1)
/* Flags to communicate with zskbd_softintr() */
volatile int zskbd_intr_flags;
#define INTR_RX_OVERRUN 1
#define INTR_TX_EMPTY 2
#define INTR_ST_CHECK 4
/*
* The receive ring buffer.
*/
u_int zskbd_rbget; /* ring buffer `get' index */
volatile u_int zskbd_rbput; /* ring buffer `put' index */
u_short zskbd_rbuf[ZSKBD_RX_RING_SIZE]; /* rr1, data pairs */
/* Initialize the speed, etc. */
s = splzs();
/* May need reset... */
zs_write_reg(cs, 9, ZSWR9_A_RESET);
/* These are OK as set by zscc: WR3, WR4, WR5 */
/* We don't care about status or tx interrupts. */
cs->cs_preg[1] = ZSWR1_RIE;
(void) zs_set_speed(cs, ZSKBD_BPS);
zs_loadchannelregs(cs);
splx(s);
if (!isconsole)
lk201_init(&zsi->zsi_ks);
/* XXX should identify keyboard ID here XXX */
/* XXX layout and the number of LED is varying XXX */
static int
zskbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
struct zskbd_softc *sc = v;
switch (cmd) {
case WSKBDIO_GTYPE:
*(int *)data = sc->kbd_type;
return 0;
case WSKBDIO_SETLEDS:
lk201_set_leds(&sc->sc_itl->zsi_ks, *(int *)data);
return 0;
case WSKBDIO_GETLEDS:
/* XXX don't dig in kbd internals */
*(int *)data = sc->sc_itl->zsi_ks.leds_state;
return 0;
case WSKBDIO_COMPLEXBELL:
lk201_bell(&sc->sc_itl->zsi_ks,
(struct wskbd_bell_data *)data);
return 0;
case WSKBDIO_SETKEYCLICK:
lk201_set_keyclick(&sc->sc_itl->zsi_ks, *(int *)data);
return 0;
case WSKBDIO_GETKEYCLICK:
/* XXX don't dig in kbd internals */
*(int *)data = sc->sc_itl->zsi_ks.kcvol;
return 0;
}
return EPASSTHROUGH;
}
static void
zskbd_input(struct zskbd_softc *sc, int data)
{
u_int type;
int val;
int decode;
do {
decode = lk201_decode(&sc->sc_itl->zsi_ks, 1,
data, &type, &val);
if (decode != LKD_NODATA)
wskbd_input(sc->sc_wskbddev, type, val);
} while (decode == LKD_MORE);
}
/****************************************************************
* Interface to the lower layer (zscc)
****************************************************************/
/*
* First read the status, because reading the received char
* destroys the status of this char.
*/
rr1 = zs_read_reg(cs, 1);
c = zs_read_data(cs);
if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
/* Clear the receive error. */
zs_write_csr(cs, ZSWR0_RESET_ERRORS);
}
/*
* We have to accumulate status line changes here.
* Otherwise, if we get multiple status interrupts
* before the softint runs, we could fail to notice
* some status line changes in the softint routine.
* Fix from Bill Studenmund, October 1996.
*/
cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
cs->cs_rr0 = rr0;
zskbd->zskbd_intr_flags |= INTR_ST_CHECK;
/* Ask for softint() call. */
cs->cs_softreq = 1;
}
static void
zskbd_softint(struct zs_chanstate *cs)
{
struct zskbd_softc *zskbd;
int get, c, s;
int intr_flags;
u_short ring_data;
zskbd = cs->cs_private;
/* Atomically get and clear flags. */
s = splzs();
intr_flags = zskbd->zskbd_intr_flags;
zskbd->zskbd_intr_flags = 0;
/* Now lower to spltty for the rest. */
(void) spltty();
/*
* Copy data from the receive ring to the event layer.
*/
get = zskbd->zskbd_rbget;
while (get != zskbd->zskbd_rbput) {
ring_data = zskbd->zskbd_rbuf[get];
get = (get + 1) & ZSKBD_RX_RING_MASK;
/* low byte of ring_data is rr1 */
c = (ring_data >> 8) & 0xff;
if (ring_data & ZSRR1_DO)
intr_flags |= INTR_RX_OVERRUN;
if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
#if 0 /* XXX */
log(LOG_ERR, "%s: input error (0x%x)\n",
device_xname(zskbd->zskbd_dev), ring_data);
c = -1; /* signal input error */
#endif
}
/* Pass this up to the "middle" layer. */
zskbd_input(zskbd, c);
}
if (intr_flags & INTR_RX_OVERRUN) {
#if 0 /* XXX */
log(LOG_ERR, "%s: input overrun\n",
device_xname(zskbd->zskbd_dev));
#endif
}
zskbd->zskbd_rbget = get;