NetBSD/sys/arch/x68k/dev/kbd.c

440 lines
9.2 KiB
C
Raw Normal View History

/* $NetBSD: kbd.c,v 1.9 1999/12/03 00:14:06 itohy Exp $ */
1996-05-05 16:17:03 +04:00
/*
* Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
* All rights reserved.
*
* 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. 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, Berkeley and its contributors.
* 4. 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.
*/
#include "ite.h"
#include "bell.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
1999-03-24 17:07:38 +03:00
#include <sys/signalvar.h>
1996-05-05 16:17:03 +04:00
#include <machine/cpu.h>
1999-03-16 19:30:16 +03:00
#include <machine/bus.h>
1996-05-05 16:17:03 +04:00
1999-03-16 19:30:16 +03:00
#include <arch/x68k/dev/intiovar.h>
#include <arch/x68k/dev/mfp.h>
#include <arch/x68k/dev/itevar.h>
1996-05-05 16:17:03 +04:00
/* for sun-like event mode, if you go thru /dev/kbd. */
1999-03-16 19:30:16 +03:00
#include <arch/x68k/dev/event_var.h>
1999-03-24 17:07:38 +03:00
1996-05-05 16:17:03 +04:00
#include <machine/kbio.h>
#include <machine/kbd.h>
#include <machine/vuid_event.h>
struct kbd_softc {
1999-03-16 19:30:16 +03:00
struct device sc_dev;
int sc_event_mode; /* if true, collect events, else pass to ite */
struct evvar sc_events; /* event queue state */
};
1996-05-05 16:17:03 +04:00
1999-03-16 19:30:16 +03:00
void kbdenable __P((int));
1997-10-09 17:00:49 +04:00
int kbdopen __P((dev_t, int, int, struct proc *));
int kbdclose __P((dev_t, int, int, struct proc *));
int kbdread __P((dev_t, struct uio *, int));
int kbdwrite __P((dev_t, struct uio *, int));
int kbdioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
int kbdpoll __P((dev_t, int, struct proc *));
1999-03-16 19:30:16 +03:00
int kbdintr __P((void *));
1997-10-09 17:00:49 +04:00
void kbdsoftint __P((void));
void kbd_bell __P((int));
1999-03-16 19:30:16 +03:00
int kbdcngetc __P((void));
1997-10-09 17:00:49 +04:00
void kbd_setLED __P((void));
int kbd_send_command __P((int));
1999-03-16 19:30:16 +03:00
static int kbdmatch __P((struct device *, struct cfdata *, void *));
static void kbdattach __P((struct device *, struct device *, void *));
struct cfattach kbd_ca = {
sizeof(struct kbd_softc), kbdmatch, kbdattach
};
static int
kbdmatch(parent, cf, aux)
1999-03-16 19:30:16 +03:00
struct device *parent;
struct cfdata *cf;
void *aux;
1996-05-05 16:17:03 +04:00
{
1999-03-16 19:30:16 +03:00
if (strcmp(aux, "kbd") != 0)
return (0);
if (cf->cf_unit != 0)
return (0);
return (1);
1996-05-05 16:17:03 +04:00
}
1999-03-16 19:30:16 +03:00
static void
kbdattach(parent, self, aux)
struct device *parent, *self;
void *aux;
{
struct kbd_softc *k = (void*) self;
struct mfp_softc *mfp = (void*) parent;
int s = spltty();
/* MFP interrupt #12 is for USART recieve buffer full */
intio_intr_establish(mfp->sc_intr + 12, "kbd", kbdintr, self);
1999-03-16 19:30:16 +03:00
kbdenable(1);
k->sc_event_mode = 0;
k->sc_events.ev_io = 0;
splx(s);
printf("\n");
1999-03-16 19:30:16 +03:00
}
1996-05-05 16:17:03 +04:00
/* definitions for x68k keyboard encoding. */
#define KEY_CODE(c) ((c) & 0x7f)
#define KEY_UP(c) ((c) & 0x80)
void
1999-03-16 19:30:16 +03:00
kbdenable(mode)
int mode; /* 1: interrupt, 0: poll */
1996-05-05 16:17:03 +04:00
{
1999-03-16 19:30:16 +03:00
intio_set_sysport_keyctrl(8);
mfp_bit_clear_iera(MFP_INTR_RCV_FULL | MFP_INTR_TIMER_B);
1999-03-16 19:30:16 +03:00
mfp_set_tbcr(MFP_TIMERB_RESET | MFP_TIMERB_STOP);
mfp_set_tbdr(13); /* Timer B interrupt interval */
mfp_set_tbcr(1); /* 1/4 delay mode */
mfp_set_ucr(MFP_UCR_CLKX16 | MFP_UCR_RW_8 | MFP_UCR_ONESB);
mfp_set_rsr(MFP_RSR_RE); /* USART receive enable */
mfp_set_tsr(MFP_TSR_TE); /* USART transmit enable */
1996-05-05 16:17:03 +04:00
if (mode) {
1999-03-16 19:30:16 +03:00
mfp_bit_set_iera(MFP_INTR_RCV_FULL);
/*
* Perform null read in case that an input byte is in the
* receiver buffer, which prevents further interrupts.
* We could save the input, but probably not so valuable.
*/
(void) mfp_get_udr();
}
1999-03-16 19:30:16 +03:00
kbdled = 0; /* all keyboard LED turn off. */
1996-05-05 16:17:03 +04:00
kbd_setLED();
1999-03-16 19:30:16 +03:00
if (!(intio_get_sysport_keyctrl() & 8))
printf(" (no connected keyboard)");
1996-05-05 16:17:03 +04:00
}
1999-03-16 19:30:16 +03:00
extern struct cfdriver kbd_cd;
1996-05-05 16:17:03 +04:00
int
kbdopen(dev, flags, mode, p)
dev_t dev;
int flags, mode;
struct proc *p;
{
1999-03-16 19:30:16 +03:00
struct kbd_softc *k;
int unit = minor(dev);
if (unit >= kbd_cd.cd_ndevs)
return (ENXIO);
k = kbd_cd.cd_devs[minor(dev)];
if (k == NULL)
return (ENXIO);
if (k->sc_events.ev_io)
1996-05-05 16:17:03 +04:00
return (EBUSY);
1999-03-16 19:30:16 +03:00
k->sc_events.ev_io = p;
ev_init(&k->sc_events);
1996-05-05 16:17:03 +04:00
return (0);
}
int
kbdclose(dev, flags, mode, p)
dev_t dev;
int flags, mode;
struct proc *p;
{
1999-03-16 19:30:16 +03:00
struct kbd_softc *k = kbd_cd.cd_devs[minor(dev)];
1996-05-05 16:17:03 +04:00
/* Turn off event mode, dump the queue */
1999-03-16 19:30:16 +03:00
k->sc_event_mode = 0;
ev_fini(&k->sc_events);
k->sc_events.ev_io = NULL;
1996-05-05 16:17:03 +04:00
return (0);
}
1999-03-16 19:30:16 +03:00
1996-05-05 16:17:03 +04:00
int
kbdread(dev, uio, flags)
dev_t dev;
struct uio *uio;
int flags;
{
1999-03-16 19:30:16 +03:00
struct kbd_softc *k = kbd_cd.cd_devs[minor(dev)];
return ev_read(&k->sc_events, uio, flags);
1996-05-05 16:17:03 +04:00
}
/* this routine should not exist, but is convenient to write here for now */
int
kbdwrite(dev, uio, flags)
dev_t dev;
struct uio *uio;
int flags;
{
return EOPNOTSUPP;
}
1999-03-24 17:07:38 +03:00
#if NBELL > 0
struct bell_info;
int opm_bell_setup __P((struct bell_info *));
void opm_bell_on __P((void));
void opm_bell_off __P((void));
#endif
1996-05-05 16:17:03 +04:00
int
kbdioctl(dev, cmd, data, flag, p)
dev_t dev;
u_long cmd;
caddr_t data;
int flag;
struct proc *p;
{
1999-03-16 19:30:16 +03:00
register struct kbd_softc *k = kbd_cd.cd_devs[minor(dev)];
1996-05-05 16:17:03 +04:00
int cmd_data;
switch (cmd) {
case KIOCTRANS:
if (*(int *)data == TR_UNTRANS_EVENT)
return (0);
break;
case KIOCGTRANS:
/*
* Get translation mode
*/
*(int *)data = TR_UNTRANS_EVENT;
return (0);
case KIOCSDIRECT:
1999-03-16 19:30:16 +03:00
k->sc_event_mode = *(int *)data;
1996-05-05 16:17:03 +04:00
return (0);
case KIOCCMD:
cmd_data = *(int *)data;
return kbd_send_command(cmd_data);
case KIOCSLED:
kbdled = *(char *)data;
kbd_setLED();
return (0);
case KIOCGLED:
*(char *)data = kbdled;
return (0);
case KIOCSBELL:
#if NBELL > 0
1999-03-24 17:07:38 +03:00
return opm_bell_setup((struct bell_info *)data);
1996-05-05 16:17:03 +04:00
#else
return (0); /* allways success */
#endif
case FIONBIO: /* we will remove this someday (soon???) */
return (0);
case FIOASYNC:
1999-03-16 19:30:16 +03:00
k->sc_events.ev_async = *(int *)data != 0;
1996-05-05 16:17:03 +04:00
return (0);
case TIOCSPGRP:
1999-03-16 19:30:16 +03:00
if (*(int *)data != k->sc_events.ev_io->p_pgid)
1996-05-05 16:17:03 +04:00
return (EPERM);
return (0);
default:
return (ENOTTY);
}
/*
* We identified the ioctl, but we do not handle it.
*/
return (EOPNOTSUPP); /* misuse, but what the heck */
}
1999-03-16 19:30:16 +03:00
1996-05-05 16:17:03 +04:00
int
1996-11-23 12:44:53 +03:00
kbdpoll(dev, events, p)
1996-05-05 16:17:03 +04:00
dev_t dev;
1996-11-23 12:44:53 +03:00
int events;
1996-05-05 16:17:03 +04:00
struct proc *p;
{
1999-03-16 19:30:16 +03:00
struct kbd_softc *k;
k = kbd_cd.cd_devs[minor(dev)];
return (ev_poll(&k->sc_events, events, p));
1996-05-05 16:17:03 +04:00
}
1999-03-16 19:30:16 +03:00
1996-05-05 16:17:03 +04:00
#define KBDBUFMASK 63
#define KBDBUFSIZ 64
static u_char kbdbuf[KBDBUFSIZ];
static int kbdputoff = 0;
static int kbdgetoff = 0;
1999-03-16 19:30:16 +03:00
int
kbdintr(arg)
void *arg;
1996-05-05 16:17:03 +04:00
{
u_char c, in;
1999-03-16 19:30:16 +03:00
struct kbd_softc *k = arg; /* XXX */
1996-05-05 16:17:03 +04:00
struct firm_event *fe;
int put;
1999-03-16 19:30:16 +03:00
c = in = mfp_get_udr();
1996-05-05 16:17:03 +04:00
/* if not in event mode, deliver straight to ite to process key stroke */
1999-03-16 19:30:16 +03:00
if (! k->sc_event_mode) {
1996-05-05 16:17:03 +04:00
kbdbuf[kbdputoff++ & KBDBUFMASK] = c;
setsoftkbd();
1999-03-24 17:07:38 +03:00
return 0;
1996-05-05 16:17:03 +04:00
}
/* Keyboard is generating events. Turn this keystroke into an
event and put it in the queue. If the queue is full, the
keystroke is lost (sorry!). */
1999-03-16 19:30:16 +03:00
put = k->sc_events.ev_put;
fe = &k->sc_events.ev_q[put];
1996-05-05 16:17:03 +04:00
put = (put + 1) % EV_QSIZE;
1999-03-16 19:30:16 +03:00
if (put == k->sc_events.ev_get) {
1996-05-05 16:17:03 +04:00
log(LOG_WARNING, "keyboard event queue overflow\n"); /* ??? */
1999-03-24 17:07:38 +03:00
return 0;
1996-05-05 16:17:03 +04:00
}
fe->id = KEY_CODE(c);
fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
fe->time = time;
1999-03-16 19:30:16 +03:00
k->sc_events.ev_put = put;
EV_WAKEUP(&k->sc_events);
1999-03-24 17:07:38 +03:00
return 0;
1996-05-05 16:17:03 +04:00
}
void
1999-03-16 19:30:16 +03:00
kbdsoftint() /* what if ite is not configured? */
1996-05-05 16:17:03 +04:00
{
1999-03-16 19:30:16 +03:00
int s = spltty();
1996-05-05 16:17:03 +04:00
while(kbdgetoff < kbdputoff)
1999-03-24 17:07:38 +03:00
ite_filter(kbdbuf[kbdgetoff++ & KBDBUFMASK]);
1996-05-05 16:17:03 +04:00
kbdgetoff = kbdputoff = 0;
splx(s);
}
void
1996-05-05 16:17:03 +04:00
kbd_bell(mode)
int mode;
{
#if NBELL > 0
if (mode)
opm_bell_on();
else
opm_bell_off();
#endif
}
1999-03-16 19:30:16 +03:00
unsigned char kbdled;
void
1996-05-05 16:17:03 +04:00
kbd_setLED()
{
1999-03-16 19:30:16 +03:00
mfp_send_usart(~kbdled | 0x80);
1996-05-05 16:17:03 +04:00
}
int
kbd_send_command(cmd)
int cmd;
{
switch (cmd) {
case KBD_CMD_RESET:
/* XXX */
return 0;
case KBD_CMD_BELL:
kbd_bell(1);
return 0;
case KBD_CMD_NOBELL:
kbd_bell(0);
return 0;
default:
return ENOTTY;
}
}
1999-03-16 19:30:16 +03:00
/*
* for console
*/
#include "ite.h"
#if NITE > 0
int
kbdcngetc()
{
int s;
u_char ints, c;
1999-03-16 19:30:16 +03:00
s = splhigh();
1999-03-16 19:30:16 +03:00
ints = mfp_get_iera();
mfp_bit_clear_iera(MFP_INTR_RCV_FULL);
mfp_set_rsr(mfp_get_rsr() | MFP_RSR_RE);
c = mfp_recieve_usart();
1999-03-16 19:30:16 +03:00
mfp_set_iera(ints);
splx(s);
1999-03-16 19:30:16 +03:00
return c;
}
1996-05-05 16:17:03 +04:00
#endif