/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz and Don Ahn.
*
* 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.
*
* @(#)pccons.c 5.11 (Berkeley) 5/21/91
*/
/*-
* Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz and Don Ahn.
*
* Copyright (c) 1994 Charles M. Hannum.
* Copyright (c) 1992, 1993 Erik Forsberg.
*
* 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.
*
* @(#)pccons.c 5.11 (Berkeley) 5/21/91
*/
/*
* code to work keyboard & display for PC-style console
*/
static u_short *Crtat; /* pointer to backing store */
static u_short *crtat; /* pointer to current char */
static u_char async, kernel, polling; /* Really, you don't want to know. */
static u_char lock_state = 0x00, /* all off */
old_lock_state = 0xff,
typematic_rate = 0xff, /* don't update until set by user */
old_typematic_rate = 0xff;
static u_short cursor_shape = 0xffff, /* don't update until set by user */
old_cursor_shape = 0xffff;
static pccons_keymap_t scan_codes[KB_NUM_KEYS];/* keyboard translation table */
int pc_xmode = 0;
/*
* Keyboard output queue.
*/
int kb_oq_put = 0;
int kb_oq_get = 0;
u_char kb_oq[8];
#define PCUNIT(x) (minor(x))
static struct video_state {
int cx, cy; /* escape parameters */
int row, col; /* current cursor position */
int nrow, ncol, nchr; /* current screen geometry */
int offset; /* Saved cursor pos */
u_char state; /* parser state */
#define VSS_ESCAPE 1
#define VSS_EBRACE 2
#define VSS_EPARAM 3
char so; /* in standout mode? */
char color; /* color or mono display */
char at; /* normal attributes */
char so_at; /* standout attributes */
} vs;
static callout_t async_update_ch;
void pc_xmode_on(void);
void pc_xmode_off(void);
static u_char kbc_get8042cmd(void);
int kbd_cmd(u_char, u_char);
static inline int kbd_wait_output(void);
static inline int kbd_wait_input(void);
void kbd_flush_input(void);
void set_cursor_shape(void);
void get_cursor_shape(void);
void async_update(void);
void do_async_update(u_char);
void pccnputc(dev_t, int c);
int pccngetc(dev_t);
void pccnpollc(dev_t, int);
/*
* pc->pc_6845_ioh and pc->pc_crt_memh will be initialized later,
* when `Crtat' is initialized.
*/
pc->pc_config = config;
(*config->pc_init)();
}
/*
* bcopy variant that only moves word-aligned 16-bit entities,
* for stupid VGA cards. cnt is required to be an even value.
*/
static inline void
wcopy(void *src, void *tgt, u_int cnt)
{
uint16_t *from = src;
uint16_t *to = tgt;
cnt >>= 1;
if (to < from || to >= from + cnt)
while (cnt--)
*to++ = *from++;
else {
to += cnt;
from += cnt;
while (cnt--)
*--to = *--from;
}
}
static inline int
kbd_wait_output(void)
{
u_int i;
for (i = 100000; i; i--)
if ((kbd_cmd_read_1() & KBS_IBF) == 0) {
KBD_DELAY;
return 1;
}
return 0;
}
static inline int
kbd_wait_input(void)
{
u_int i;
for (i = 100000; i; i--)
if ((kbd_cmd_read_1() & KBS_DIB) != 0) {
KBD_DELAY;
return 1;
}
return 0;
}
void
kbd_flush_input(void)
{
uint8_t c;
while ((c = kbd_cmd_read_1()) & 0x03)
if ((c & KBS_DIB) == KBS_DIB) {
/* XXX - delay is needed to prevent some keyboards from
wedging when the system boots */
delay(6);
(void)kbd_data_read_1();
}
}
#if 1
/*
* Get the current command byte.
*/
static u_char
kbc_get8042cmd(void)
{
if (!kbd_wait_output())
return -1;
kbd_cmd_write_1(K_RDCMDBYTE);
if (!kbd_wait_input())
return -1;
return kbd_data_read_1();
}
#endif
/*
* Pass command byte to keyboard controller (8042).
*/
int
kbc_put8042cmd(uint8_t val)
{
if (!kbd_wait_output())
return 0;
kbd_cmd_write_1(K_LDCMDBYTE);
if (!kbd_wait_output())
return 0;
kbd_data_write_1(val);
return 1;
}
/*
* Pass command to keyboard itself
*/
int
kbd_cmd(uint8_t val, uint8_t polled)
{
u_int retries = 3;
u_int i;
if (!polled) {
i = spltty();
if (kb_oq_get == kb_oq_put) {
kbd_data_write_1(val);
}
kb_oq[kb_oq_put] = val;
kb_oq_put = (kb_oq_put + 1) & 7;
splx(i);
return 1;
}
do {
if (!kbd_wait_output())
return 0;
kbd_data_write_1(val);
for (i = 100000; i; i--) {
if (kbd_cmd_read_1() & KBS_DIB) {
uint8_t c;
KBD_DELAY;
c = kbd_data_read_1();
if (c == KBR_ACK || c == KBR_ECHO) {
return 1;
}
if (c == KBR_RESEND) {
break;
}
#ifdef DIAGNOSTIC
printf("kbd_cmd: input char %x lost\n", c);
#endif
}
}
} while (--retries);
return 0;
}
/*
* real 6845's, as found on, MDA, Hercules or CGA cards, do
* not support reading the cursor shape registers. the 6845
* tri-states its data bus. This is _normally_ read by the
* CPU as either 0x00 or 0xff.. in which case we just use
* a line cursor.
*/
if (cursor_shape == 0x0000 || cursor_shape == 0xffff)
cursor_shape = 0x0b10;
else
cursor_shape &= 0x1f1f;
}
void
do_async_update(uint8_t poll)
{
int pos;
static int old_pos = -1;
if (kernel || polling) {
if (async)
callout_stop(&async_update_ch);
do_async_update(1);
} else {
if (async)
return;
async = 1;
callout_reset(&async_update_ch, 1,
(void(*)(void *))do_async_update, NULL);
}
}
/*
* these are both bad jokes
*/
int
pccons_common_match(bus_space_tag_t crt_iot, bus_space_tag_t crt_memt,
bus_space_tag_t kbd_iot, struct pccons_config *config)
{
int i;
/* Enable interrupts and keyboard, etc. */
if (!kbc_put8042cmd(CMDBYTE)) {
printf("pcprobe: command error\n");
return 0;
}
#if 1
/* Flush any garbage. */
kbd_flush_input();
/* Reset the keyboard. */
if (!kbd_cmd(KBC_RESET, 1)) {
printf("pcprobe: reset error %d\n", 1);
goto lose;
}
for (i = 600000; i; i--)
if ((kbd_cmd_read_1() & KBS_DIB) != 0) {
KBD_DELAY;
break;
}
if (i == 0 || kbd_data_read_1() != KBR_RSTDONE) {
printf("pcprobe: reset error %d\n", 2);
goto lose;
}
/*
* Some keyboards seem to leave a second ack byte after the reset.
* This is kind of stupid, but we account for them anyway by just
* flushing the buffer.
*/
kbd_flush_input();
/* Just to be sure. */
if (!kbd_cmd(KBC_ENABLE, 1)) {
printf("pcprobe: reset error %d\n", 3);
goto lose;
}
/*
* Some keyboard/8042 combinations do not seem to work if the keyboard
* is set to table 1; in fact, it would appear that some keyboards just
* ignore the command altogether. So by default, we use the AT scan
* codes and have the 8042 translate them. Unfortunately, this is
* known to not work on some PS/2 machines. We try desperately to deal
* with this by checking the (lack of a) translate bit in the 8042 and
* attempting to set the keyboard to XT mode. If this all fails, well,
* tough luck.
*
* XXX It would perhaps be a better choice to just use AT scan codes
* and not bother with this.
*/
if (kbc_get8042cmd() & KC8_TRANS) {
/* The 8042 is translating for us; use AT codes. */
if (!kbd_cmd(KBC_SETTABLE, 1) || !kbd_cmd(2, 1)) {
printf("pcprobe: reset error %d\n", 4);
goto lose;
}
} else {
/* Stupid 8042; set keyboard to XT codes. */
if (!kbd_cmd(KBC_SETTABLE, 1) || !kbd_cmd(1, 1)) {
printf("pcprobe: reset error %d\n", 5);
goto lose;
}
}
lose:
/*
* Technically, we should probably fail the probe. But we'll be nice
* and allow keyboard-less machines to boot with the console.
*/
#endif
/*
* Got a console receive interrupt -
* the console processor wants to give us a character.
* Catch the character, and see who it goes to.
*/
int
pcintr(void *arg)
{
struct pc_softc *sc = arg;
struct tty *tp = sc->sc_tty;
uint8_t *cp;
if ((kbd_cmd_read_1() & KBS_DIB) == 0)
return 0;
if (polling)
return 1;
do {
cp = sget();
if (!tp || (tp->t_state & TS_ISOPEN) == 0)
return 1;
if (cp)
do
(*tp->t_linesw->l_rint)(*cp++, tp);
while (*cp);
} while (kbd_cmd_read_1() & KBS_DIB);
return 1;
}
int
pcioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct pc_softc *sc = device_lookup_private(&pc_cd, PCUNIT(dev));
struct tty *tp = sc->sc_tty;
int error;
switch (cmd) {
case CONSOLE_X_MODE_ON:
pc_xmode_on();
return 0;
case CONSOLE_X_MODE_OFF:
pc_xmode_off();
return 0;
case CONSOLE_X_BELL:
/*
* If set, data is a pointer to a length 2 array of
* integers. data[0] is the pitch in Hz and data[1]
* is the duration in msec.
*/
if (data)
sysbeep(((int*)data)[0],
(((int*)data)[1] * hz) / 1000);
else
sysbeep(BEEP_FREQ, BEEP_TIME);
return 0;
case CONSOLE_SET_TYPEMATIC_RATE: {
u_char rate;
if (!data)
return EINVAL;
rate = *((u_char *)data);
/*
* Check that it isn't too big (which would cause it to be
* confused with a command).
*/
if (rate & 0x80)
return EINVAL;
typematic_rate = rate;
async_update();
return 0;
}
case CONSOLE_SET_KEYMAP: {
pccons_keymap_t *map = (pccons_keymap_t *) data;
int i;
if (!data)
return EINVAL;
for (i = 0; i < KB_NUM_KEYS; i++)
if (map[i].unshift[KB_CODE_SIZE-1] ||
map[i].shift[KB_CODE_SIZE-1] ||
map[i].ctl[KB_CODE_SIZE-1] ||
map[i].altgr[KB_CODE_SIZE-1] ||
map[i].shift_altgr[KB_CODE_SIZE-1])
return EINVAL;
memcpy(scan_codes, data, sizeof(pccons_keymap_t[KB_NUM_KEYS]));
return 0;
}
case CONSOLE_GET_KEYMAP:
if (!data)
return EINVAL;
memcpy(scan_codes, data, sizeof(pccons_keymap_t[KB_NUM_KEYS]));
return 0;
s = spltty();
if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
goto out;
tp->t_state |= TS_BUSY;
splx(s);
/*
* We need to do this outside spl since it could be fairly
* expensive and we don't want our serial ports to overflow.
*/
cl = &tp->t_outq;
len = q_to_b(cl, buf, PCBURST);
sput(buf, len);
s = spltty();
tp->t_state &= ~TS_BUSY;
if (ttypull(tp)) {
tp->t_state |= TS_TIMEOUT;
callout_schedule(&tp->t_rstrt_ch, 1);
}
out:
splx(s);
}
/* ARGSUSED */
int
pccngetc(dev_t dev)
{
char *cp;
if (pc_xmode > 0)
return 0;
do {
/* wait for byte */
while ((kbd_cmd_read_1() & KBS_DIB) == 0);
/* see if it's worthwhile */
cp = sget();
} while (!cp);
if (*cp == '\r')
return '\n';
return *cp;
}
void
pccnpollc(dev_t dev, int on)
{
polling = on;
if (!on) {
int unit;
struct pc_softc *sc;
int s;
/*
* If disabling polling on a device that's been configured,
* make sure there are no bytes left in the FIFO, holding up
* the interrupt line. Otherwise we won't get any further
* interrupts.
*/
unit = PCUNIT(dev);
if (pc_cd.cd_ndevs > unit) {
sc = device_lookup_private(&pc_cd, unit);
if (sc != NULL) {
s = spltty();
pcintr(sc);
splx(s);
}
}
}
}
/*
* Set line parameters.
*/
int
pcparam(struct tty *tp, struct termios *t)
{
case '\b':
if (crtat <= Crtat)
break;
--crtat;
if (--vs.col < 0)
vs.col += vs.ncol; /* non-destructive backspace */
break;
case '\r':
crtat -= vs.col;
vs.col = 0;
break;
case '\n':
crtat += vs.ncol;
scroll = 1;
break;
default:
switch (vs.state) {
case 0:
if (c == '\a')
sysbeep(BEEP_FREQ, BEEP_TIME);
else {
/*
* If we're outputting multiple printed
* characters, just blast them to the
* screen until we reach the end of the
* buffer or a control character. This
* saves time by short-circuiting the
* switch.
* If we reach the end of the line, we
* break to do a scroll check.
*/
for (;;) {
if (c & 0x80)
c = iso2ibm437[c&0x7f];