#include "opt_kbd.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kbio.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/syslog.h>
#include <machine/clock.h>
#include <bus/isa/isareg.h>
#include "atkbdcreg.h"
#include "kbdreg.h"
#include "use_atkbdc.h"
#define MAXKBDC MAX(NATKBDC, 1)
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
#define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
#define availq(q) ((q)->head != (q)->tail)
#if KBDIO_DEBUG >= 2
#define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
#else
#define emptyq(q) ((q)->tail = (q)->head = 0)
#endif
#define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
#define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
#define write_data(k, d) (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
#define write_command(k, d) (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
static atkbdc_softc_t default_kbdc;
static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
static int verbose = KBDIO_DEBUG;
static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
bus_space_handle_t h0, bus_space_handle_t h1);
static int addq(kbdkqueue *q, int c);
static int removeq(kbdkqueue *q);
static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
static int wait_for_data(atkbdc_softc_t *kbdc);
static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
static int wait_for_aux_data(atkbdc_softc_t *kbdc);
static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
struct atkbdc_quirks {
const char *bios_vendor;
const char *maker;
const char *product;
const char *version;
int quirk;
};
#define CHROMEBOOK_WORKAROUND \
(KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT | \
KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT)
static struct atkbdc_quirks quirks[] = {
{ "coreboot", NULL, NULL, "Google_", CHROMEBOOK_WORKAROUND },
{ "coreboot", "GOOGLE", NULL, NULL, CHROMEBOOK_WORKAROUND },
{ "coreboot", "Google", NULL, NULL, CHROMEBOOK_WORKAROUND },
{ NULL, "LENOVO", NULL, NULL, KBDC_QUIRK_DISABLE_MUX_PROBE },
};
#define QUIRK_STR_EQUAL(s1, s2) \
(s1 == NULL || (s2 != NULL && strcmp(s1, s2) == 0))
#define QUIRK_STR_MATCH(s1, s2) \
(s1 == NULL || (s2 != NULL && strncmp(s1, s2, strlen(s1)) == 0))
static int
atkbdc_getquirks(void)
{
int i;
char *bios_vendor = kgetenv("smbios.bios.vendor");
char *maker = kgetenv("smbios.system.maker");
char *product = kgetenv("smbios.system.product");
char *version = kgetenv("smbios.bios.version");
char *reldate = kgetenv("smbios.bios.reldate");
for (i = 0; i < nitems(quirks); i++)
if (QUIRK_STR_EQUAL(quirks[i].bios_vendor, bios_vendor) &&
QUIRK_STR_EQUAL(quirks[i].maker, maker) &&
QUIRK_STR_EQUAL(quirks[i].product, product) &&
QUIRK_STR_MATCH(quirks[i].version, version))
return (quirks[i].quirk);
if (QUIRK_STR_EQUAL("coreboot", bios_vendor) &&
(version != NULL && *version == ' ') &&
(reldate != NULL && strlen(reldate) >= 10 &&
strcmp(reldate + 6, "2018") <= 0))
return (CHROMEBOOK_WORKAROUND);
return (0);
}
atkbdc_softc_t *
atkbdc_get_softc(int unit)
{
atkbdc_softc_t *sc;
if (unit >= nitems(atkbdc_softc))
return NULL;
sc = atkbdc_softc[unit];
if (sc == NULL) {
sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
atkbdc_softc[unit] = sc;
}
return sc;
}
int
atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
{
if (rman_get_start(port0) <= 0)
return ENXIO;
if (rman_get_start(port1) <= 0)
return ENXIO;
return 0;
}
int
atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
struct resource *port1)
{
return atkbdc_setup(sc, rman_get_bustag(port0),
rman_get_bushandle(port0), rman_get_bushandle(port1));
}
extern int acpi_fadt_8042_nolegacy;
int kicked_by_syscons = 0;
static void
atkbdc_fadt_done(void)
{
if (kicked_by_syscons) {
kbd_configure(KB_CONF_PROBE_ONLY);
}
}
SYSINIT(atkbdc_kick, SI_BOOT2_PRESMP, SI_ORDER_THIRD, atkbdc_fadt_done, 0);
int
atkbdc_configure(void)
{
bus_space_tag_t tag;
bus_space_handle_t h0;
bus_space_handle_t h1;
int port0;
int port1;
#if defined(__x86_64__)
int i;
#endif
kicked_by_syscons = 1;
if (acpi_fadt_8042_nolegacy != 0)
return ENXIO;
port0 = IO_KBD;
resource_int_value("atkbdc", 0, "port", &port0);
port1 = IO_KBD + KBD_STATUS_PORT;
#if 0
resource_int_value("atkbdc", 0, "port", &port0);
#endif
#if defined(__x86_64__)
tag = X86_64_BUS_SPACE_IO;
#else
tag = 0;
#endif
#if 0
bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
#else
h0 = (bus_space_handle_t)port0;
h1 = (bus_space_handle_t)port1;
#endif
#if defined(__x86_64__)
for (i = 65536; i != 0; --i) {
if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
break;
DELAY(16);
}
if (i == 0)
return ENXIO;
#endif
return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
}
static int
atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
bus_space_handle_t h1)
{
if (sc->ioh0 == 0) {
sc->command_byte = -1;
sc->command_mask = 0;
sc->lock = FALSE;
sc->kbd.head = sc->kbd.tail = 0;
sc->aux.head = sc->aux.tail = 0;
sc->aux_mux_enabled = FALSE;
#if KBDIO_DEBUG >= 2
sc->kbd.call_count = 0;
sc->kbd.qcount = sc->kbd.max_qcount = 0;
sc->aux.call_count = 0;
sc->aux.qcount = sc->aux.max_qcount = 0;
#endif
}
sc->iot = tag;
sc->ioh0 = h0;
sc->ioh1 = h1;
sc->quirks = atkbdc_getquirks();
return 0;
}
KBDC
atkbdc_open(int unit)
{
if (unit <= 0)
unit = 0;
if (unit >= MAXKBDC)
return NULL;
if ((atkbdc_softc[unit]->port0 != NULL) ||
(atkbdc_softc[unit]->ioh0 != 0))
return (KBDC)atkbdc_softc[unit];
return NULL;
}
int
kbdc_lock(KBDC p, int lock)
{
int prevlock;
prevlock = p->lock;
p->lock = lock;
return (prevlock != lock);
}
int
kbdc_data_ready(KBDC p)
{
return (availq(&p->kbd) || availq(&p->aux) ||
(read_status(p) & KBDS_ANY_BUFFER_FULL));
}
static int
addq(kbdkqueue *q, int c)
{
if (nextq(q->tail) != q->head) {
q->q[q->tail] = c;
q->tail = nextq(q->tail);
#if KBDIO_DEBUG >= 2
++q->call_count;
++q->qcount;
if (q->qcount > q->max_qcount)
q->max_qcount = q->qcount;
#endif
return TRUE;
}
return FALSE;
}
static int
removeq(kbdkqueue *q)
{
int c;
if (q->tail != q->head) {
c = q->q[q->head];
q->head = nextq(q->head);
#if KBDIO_DEBUG >= 2
--q->qcount;
#endif
return c;
}
return -1;
}
static int
wait_while_controller_busy(struct atkbdc_softc *kbdc)
{
TOTALDELAY retry = { .us = 70000, .last_clock = 0 };
int f;
unsigned char c;
while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
c = read_data(kbdc);
addq(&kbdc->kbd, c);
} else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
c = read_data(kbdc);
addq(&kbdc->aux, c);
}
DELAY(KBDC_DELAYTIME);
if (CHECKTIMEOUT(&retry))
return FALSE;
}
return TRUE;
}
static int
wait_for_data(struct atkbdc_softc *kbdc)
{
TOTALDELAY retry = { .us = 200000, .last_clock = 0 };
int f;
while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
DELAY(KBDC_DELAYTIME);
if (CHECKTIMEOUT(&retry))
return 0;
}
DELAY(KBDD_DELAYTIME);
return f;
}
static int
wait_for_kbd_data(struct atkbdc_softc *kbdc)
{
TOTALDELAY retry = { .us = 200000, .last_clock = 0 };
int f;
unsigned char c;
while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) !=
KBDS_KBD_BUFFER_FULL) {
if (f == KBDS_AUX_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
c = read_data(kbdc);
addq(&kbdc->aux, c);
}
DELAY(KBDC_DELAYTIME);
if (CHECKTIMEOUT(&retry))
return 0;
}
DELAY(KBDD_DELAYTIME);
return f;
}
static int
wait_for_kbd_ack(struct atkbdc_softc *kbdc)
{
TOTALDELAY retry = { .us = 200000, .last_clock = 0 };
int f;
int b;
while (CHECKTIMEOUT(&retry) == 0) {
if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(kbdc);
if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
if ((b == KBD_ACK) || (b == KBD_RESEND) ||
(b == KBD_RESET_FAIL))
return b;
addq(&kbdc->kbd, b);
} else if ((f & KBDS_BUFFER_FULL) ==
KBDS_AUX_BUFFER_FULL) {
addq(&kbdc->aux, b);
}
}
DELAY(KBDC_DELAYTIME);
}
return -1;
}
static int
wait_for_aux_data(struct atkbdc_softc *kbdc)
{
TOTALDELAY retry = { .us = 200000, .last_clock = 0 };
int f;
unsigned char b;
while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) !=
KBDS_AUX_BUFFER_FULL) {
if (f == KBDS_KBD_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(kbdc);
addq(&kbdc->kbd, b);
}
DELAY(KBDC_DELAYTIME);
if (CHECKTIMEOUT(&retry))
return 0;
}
DELAY(KBDD_DELAYTIME);
return f;
}
static int
wait_for_aux_ack(struct atkbdc_softc *kbdc)
{
TOTALDELAY retry = { .us = 200000, .last_clock = 0 };
int f;
int b;
while (CHECKTIMEOUT(&retry) == 0) {
if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(kbdc);
if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
if ((b == PSM_ACK) || (b == PSM_RESEND) ||
(b == PSM_RESET_FAIL))
return b;
addq(&kbdc->aux, b);
} else if ((f & KBDS_BUFFER_FULL) ==
KBDS_KBD_BUFFER_FULL) {
addq(&kbdc->kbd, b);
}
}
DELAY(KBDC_DELAYTIME);
}
return -1;
}
int
write_controller_w1r1(KBDC p, int c, int d)
{
if (!write_controller_command(p, c))
return (-1);
if (!write_controller_data(p, d))
return (-1);
return (read_controller_data(p));
}
int
write_controller_command(KBDC p, int c)
{
if (!wait_while_controller_busy(p))
return FALSE;
write_command(p, c);
return TRUE;
}
int
write_controller_data(KBDC p, int c)
{
if (!wait_while_controller_busy(p))
return FALSE;
write_data(p, c);
return TRUE;
}
int
write_kbd_command(KBDC p, int c)
{
if (!wait_while_controller_busy(p))
return FALSE;
write_data(p, c);
return TRUE;
}
int
write_aux_command(KBDC p, int c)
{
int f;
f = aux_mux_is_enabled(p) ? KBDC_WRITE_TO_AUX_MUX + p->aux_mux_port :
KBDC_WRITE_TO_AUX;
if (!write_controller_command(p, f))
return FALSE;
return write_controller_data(p, c);
}
int
send_kbd_command(KBDC p, int c)
{
int retry = KBD_MAXRETRY;
int res = -1;
while (retry-- > 0) {
if (!write_kbd_command(p, c))
continue;
res = wait_for_kbd_ack(p);
if (res == KBD_ACK)
break;
}
return res;
}
int
send_aux_command(KBDC p, int c)
{
int retry = KBD_MAXRETRY;
int res = -1;
while (retry-- > 0) {
if (!write_aux_command(p, c))
continue;
emptyq(&p->aux);
res = wait_for_aux_ack(p);
if (res == PSM_ACK)
break;
}
return res;
}
int
send_kbd_command_and_data(KBDC p, int c, int d)
{
int retry;
int res = -1;
for (retry = KBD_MAXRETRY; retry > 0; --retry) {
if (!write_kbd_command(p, c))
continue;
res = wait_for_kbd_ack(p);
if (res == KBD_ACK)
break;
else if (res != KBD_RESEND)
return res;
}
if (retry <= 0)
return res;
for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
if (!write_kbd_command(p, d))
continue;
res = wait_for_kbd_ack(p);
if (res != KBD_RESEND)
break;
}
return res;
}
int
send_aux_command_and_data(KBDC p, int c, int d)
{
int retry;
int res = -1;
for (retry = KBD_MAXRETRY; retry > 0; --retry) {
if (!write_aux_command(p, c))
continue;
emptyq(&p->aux);
res = wait_for_aux_ack(p);
if (res == PSM_ACK)
break;
else if (res != PSM_RESEND)
return res;
}
if (retry <= 0)
return res;
for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
if (!write_aux_command(p, d))
continue;
res = wait_for_aux_ack(p);
if (res != PSM_RESEND)
break;
}
return res;
}
int
read_controller_data(KBDC p)
{
if (availq(&p->kbd))
return removeq(&p->kbd);
if (availq(&p->aux))
return removeq(&p->aux);
if (!wait_for_data(p))
return -1;
return read_data(p);
}
#if KBDIO_DEBUG >= 2
static int call = 0;
#endif
int
read_kbd_data(KBDC p)
{
unsigned char b;
#if KBDIO_DEBUG >= 2
if (++call > 2000) {
call = 0;
log(LOG_DEBUG,
"kbdc: kbd q: %d calls, max %d chars, "
"aux q: %d calls, max %d chars\n",
p->kbd.call_count, p->kbd.max_qcount, p->aux.call_count,
p->aux.max_qcount);
}
#endif
if (availq(&p->kbd))
return removeq(&p->kbd);
if (!wait_for_kbd_data(p))
return -1;
b = read_data(p);
return b;
}
int
read_kbd_data_no_wait(KBDC p)
{
int f;
unsigned char b;
#if KBDIO_DEBUG >= 2
if (++call > 2000) {
call = 0;
log(LOG_DEBUG,
"kbdc: kbd q: %d calls, max %d chars, "
"aux q: %d calls, max %d chars\n",
p->kbd.call_count, p->kbd.max_qcount, p->aux.call_count,
p->aux.max_qcount);
}
#endif
if (availq(&p->kbd))
return removeq(&p->kbd);
f = read_status(p) & KBDS_BUFFER_FULL;
while (f == KBDS_AUX_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(p);
addq(&p->aux, b);
f = read_status(p) & KBDS_BUFFER_FULL;
}
if (f == KBDS_KBD_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(p);
return (int)b;
}
return -1;
}
int
read_aux_data(KBDC p)
{
unsigned char b;
if (availq(&p->aux))
return removeq(&p->aux);
if (!wait_for_aux_data(p))
return -1;
b = read_data(p);
return b;
}
int
read_aux_data_no_wait(KBDC p)
{
unsigned char b;
int f;
if (availq(&p->aux))
return removeq(&p->aux);
f = read_status(p) & KBDS_BUFFER_FULL;
while (f == KBDS_KBD_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(p);
addq(&p->kbd, b);
f = read_status(p) & KBDS_BUFFER_FULL;
}
if (f == KBDS_AUX_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(p);
return b;
}
return -1;
}
void
empty_kbd_buffer(KBDC p, int wait)
{
int t;
int b;
int f;
#if KBDIO_DEBUG >= 2
int c1 = 0;
int c2 = 0;
#endif
int delta = 2;
for (t = wait; t > 0;) {
if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(p);
if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
addq(&p->aux, b);
#if KBDIO_DEBUG >= 2
++c2;
} else {
++c1;
#endif
}
t = wait;
} else {
t -= delta;
}
DELAY(delta * 1000);
}
#if KBDIO_DEBUG >= 2
if ((c1 > 0) || (c2 > 0))
log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1,
c2);
#endif
emptyq(&p->kbd);
}
void
empty_aux_buffer(KBDC p, int wait)
{
int t;
int b;
int f;
#if KBDIO_DEBUG >= 2
int c1 = 0;
int c2 = 0;
#endif
int delta = 2;
for (t = wait; t > 0;) {
if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
b = read_data(p);
if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
addq(&p->kbd, b);
#if KBDIO_DEBUG >= 2
++c1;
} else {
++c2;
#endif
}
t = wait;
} else {
t -= delta;
}
DELAY(delta * 1000);
}
#if KBDIO_DEBUG >= 2
if ((c1 > 0) || (c2 > 0))
log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1,
c2);
#endif
emptyq(&p->aux);
}
void
empty_both_buffers(KBDC p, int wait)
{
int t;
int f;
int waited = 0;
#if KBDIO_DEBUG >= 2
int c1 = 0;
int c2 = 0;
#endif
int delta = 2;
for (t = wait; t > 0;) {
if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
(void)read_data(p);
#if KBDIO_DEBUG >= 2
if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
++c1;
else
++c2;
#endif
t = wait;
} else {
t -= delta;
}
waited += (delta * 1000);
if (waited == (delta * 1000000))
return;
DELAY(delta * 1000);
}
#if KBDIO_DEBUG >= 2
if ((c1 > 0) || (c2 > 0))
log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n",
c1, c2);
#endif
emptyq(&p->kbd);
emptyq(&p->aux);
}
int
reset_kbd(KBDC p)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = KBD_RESEND;
while (retry-- > 0) {
empty_both_buffers(p, 10);
if (!write_kbd_command(p, KBDC_RESET_KBD))
continue;
emptyq(&p->kbd);
c = read_controller_data(p);
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
if (c == KBD_ACK)
break;
}
if (retry < 0)
return FALSE;
while (again-- > 0) {
DELAY(KBD_RESETDELAY * 1000);
c = read_controller_data(p);
if (c != -1)
break;
}
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
if (c != KBD_RESET_DONE)
return FALSE;
return TRUE;
}
int
reset_aux_dev(KBDC p)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = PSM_RESEND;
while (retry-- > 0) {
empty_both_buffers(p, 10);
if (!write_aux_command(p, PSMC_RESET_DEV))
continue;
emptyq(&p->aux);
for (again = KBD_MAXWAIT; again > 0; --again) {
DELAY(KBD_RESETDELAY * 1000);
c = read_aux_data_no_wait(p);
if (c != -1)
break;
}
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
if (c == PSM_ACK)
break;
}
if (retry < 0)
return FALSE;
for (again = KBD_MAXWAIT; again > 0; --again) {
DELAY(KBD_RESETDELAY * 1000);
c = read_aux_data_no_wait(p);
if (c != -1)
break;
}
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
if (c != PSM_RESET_DONE)
return FALSE;
c = read_aux_data(p);
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
return TRUE;
}
int
test_controller(KBDC p)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = KBD_DIAG_FAIL;
while (retry-- > 0) {
empty_both_buffers(p, 10);
if (write_controller_command(p, KBDC_DIAGNOSE))
break;
}
if (retry < 0)
return FALSE;
emptyq(&p->kbd);
while (again-- > 0) {
DELAY(KBD_RESETDELAY * 1000);
c = read_controller_data(p);
if (c != -1)
break;
}
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
return (c == KBD_DIAG_DONE);
}
int
test_kbd_port(KBDC p)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = -1;
while (retry-- > 0) {
empty_both_buffers(p, 10);
if (write_controller_command(p, KBDC_TEST_KBD_PORT))
break;
}
if (retry < 0)
return FALSE;
emptyq(&p->kbd);
while (again-- > 0) {
c = read_controller_data(p);
if (c != -1)
break;
}
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
return c;
}
int
test_aux_port(KBDC p)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = -1;
while (retry-- > 0) {
empty_both_buffers(p, 10);
if (write_controller_command(p, KBDC_TEST_AUX_PORT))
break;
}
if (retry < 0)
return FALSE;
emptyq(&p->kbd);
while (again-- > 0) {
c = read_controller_data(p);
if (c != -1)
break;
}
if (verbose || bootverbose)
log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
return c;
}
int
kbdc_get_device_mask(KBDC p)
{
return p->command_mask;
}
void
kbdc_set_device_mask(KBDC p, int mask)
{
p->command_mask = mask &
(((p->quirks & KBDC_QUIRK_KEEP_ACTIVATED) ? 0 :
KBD_KBD_CONTROL_BITS) |
KBD_AUX_CONTROL_BITS);
}
int
get_controller_command_byte(KBDC p)
{
if (p->command_byte != -1)
return p->command_byte;
if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
return -1;
emptyq(&p->kbd);
p->command_byte = read_controller_data(p);
return p->command_byte;
}
int
set_controller_command_byte(KBDC p, int mask, int command)
{
if (get_controller_command_byte(p) == -1)
return FALSE;
command = (p->command_byte & ~mask) | (command & mask);
if (command & KBD_DISABLE_KBD_PORT) {
if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
return FALSE;
}
if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
return FALSE;
if (!write_controller_data(p, command))
return FALSE;
p->command_byte = command;
if (verbose)
log(LOG_DEBUG,
"kbdc: new command byte:%04x (set_controller...)\n",
command);
return TRUE;
}
static int
set_aux_mux_state(KBDC p, int enabled)
{
int command, version;
if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
write_controller_data(p, 0xF0) == 0 ||
read_controller_data(p) != 0xF0)
return (-1);
if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
write_controller_data(p, 0x56) == 0 ||
read_controller_data(p) != 0x56)
return (-1);
command = enabled ? 0xa4 : 0xa5;
if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
write_controller_data(p, command) == 0 ||
(version = read_controller_data(p)) == command)
return (-1);
return (version);
}
int
set_active_aux_mux_port(KBDC p, int port)
{
if (!aux_mux_is_enabled(p))
return (FALSE);
if (port < 0 || port >= KBDC_AUX_MUX_NUM_PORTS)
return (FALSE);
p->aux_mux_port = port;
return (TRUE);
}
int
enable_aux_mux(KBDC p)
{
int version;
version = set_aux_mux_state(p, TRUE);
if (version >= 0) {
p->aux_mux_enabled = TRUE;
set_active_aux_mux_port(p, 0);
}
return (version);
}
int
disable_aux_mux(KBDC p)
{
p->aux_mux_enabled = FALSE;
return (set_aux_mux_state(p, FALSE));
}
int
aux_mux_is_enabled(KBDC p)
{
return (p->aux_mux_enabled);
}