/*
* Copyright (c) 2004 Ben Harris.
* Copyright (c) 1998
* Matthias Drochner. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/* data per slave device */
struct pckbc_slotdata {
int polling; /* don't process data in interrupt handler */
int poll_data; /* data read from inr handler if polling */
int poll_stat; /* status read from inr handler if polling */
krndsource_t rnd_source;
};
static inline int
pckbc_wait_output(bus_space_tag_t iot, bus_space_handle_t ioh_c)
{
u_int i;
for (i = 100000; i; i--)
if (!(bus_space_read_1(iot, ioh_c, 0) & KBS_IBF)) {
KBD_DELAY;
return (1);
}
return (0);
}
int
pckbc_send_cmd(bus_space_tag_t iot, bus_space_handle_t ioh_c, u_char val)
{
if (!pckbc_wait_output(iot, ioh_c))
return (0);
bus_space_write_1(iot, ioh_c, 0, val);
return (1);
}
/*
* Note: the spl games here are to deal with some strange PC kbd controllers
* in some system configurations.
* This is not canonical way to handle polling input.
*/
int
pckbc_poll_data1(void *pt, pckbc_slot_t slot)
{
struct pckbc_internal *t = pt;
struct pckbc_slotdata *q = t->t_slotdata[slot];
int s;
u_char stat, c;
int i = 100; /* polls for ~100ms */
int checkaux = t->t_haveaux;
s = splhigh();
if (q && q->polling && q->poll_data != -1 && q->poll_stat != -1) {
stat = q->poll_stat;
c = q->poll_data;
q->poll_data = -1;
q->poll_stat = -1;
goto process;
}
for (; i; i--, delay(1000)) {
stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
if (stat & KBS_DIB) {
c = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
process:
if (checkaux && (stat & 0x20)) { /* aux data */
if (slot != PCKBC_AUX_SLOT) {
#ifdef PCKBCDEBUG
printf("pckbc: lost aux 0x%x\n", c);
#endif
continue;
}
} else {
if (slot == PCKBC_AUX_SLOT) {
#ifdef PCKBCDEBUG
printf("pckbc: lost kbd 0x%x\n", c);
#endif
continue;
}
}
splx(s);
return (c);
}
}
splx(s);
return (-1);
}
/*
* Get the current command byte.
*/
static int
pckbc_get8042cmd(struct pckbc_internal *t)
{
bus_space_tag_t iot = t->t_iot;
bus_space_handle_t ioh_c = t->t_ioh_c;
int data;
if (!pckbc_send_cmd(iot, ioh_c, K_RDCMDBYTE))
return (0);
data = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
if (data == -1)
return (0);
t->t_cmdbyte = data;
return (1);
}
/* set initial cmd byte */
if (!pckbc_put8042cmd(t)) {
aprint_error("pckbc: cmd word write error\n");
return;
}
/*
* XXX Don't check the keyboard port. There are broken keyboard controllers
* which don't pass the test but work normally otherwise.
*/
#if 0
/*
* check kbd port ok
*/
if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST))
return;
res = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
/*
* Normally, we should get a "0" here.
* But there are keyboard controllers behaving differently.
*/
if (!(res == 0 || res == 0xfa || res == 0x01 || res == 0xab)) {
printf("pckbc: kbd port test: %x\n", res);
return;
}
#ifdef PCKBCDEBUG
if (res != 0)
printf("pckbc: returned %x on kbd slot test\n", res);
#endif
#endif /* 0 */
if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
cmdbits |= KC8_KENABLE;
/*
* Check aux port ok.
* Avoid KBC_AUXTEST because it hangs some older controllers
* (eg UMC880?).
*/
if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXECHO)) {
aprint_error("pckbc: aux echo error 1\n");
goto nomouse;
}
if (!pckbc_wait_output(iot, ioh_c)) {
aprint_error("pckbc: aux echo error 2\n");
goto nomouse;
}
t->t_haveaux = 1;
bus_space_write_1(iot, ioh_d, 0, 0x5a); /* a random value */
res = pckbc_poll_data1(t, PCKBC_AUX_SLOT);
/*
* The following is needed to find the aux port on the Tadpole
* SPARCle.
*/
if (res == -1 && ISSET(t->t_flags, PCKBC_NEED_AUXWRITE)) {
/* Read of aux echo timed out, try again */
if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE))
goto nomouse;
if (!pckbc_wait_output(iot, ioh_c))
goto nomouse;
bus_space_write_1(iot, ioh_d, 0, 0x5a);
res = pckbc_poll_data1(t, PCKBC_AUX_SLOT);
}
if (res != -1) {
/*
* In most cases, the 0x5a gets echoed.
* Some older controllers (Gateway 2000 circa 1993)
* return 0xfe here.
* We are satisfied if there is anything in the
* aux output buffer.
*/
if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT))
cmdbits |= KC8_MENABLE;
} else {
#ifdef PCKBCDEBUG
printf("pckbc: aux echo test failed\n");
#endif
t->t_haveaux = 0;
}
nomouse:
/* enable needed interrupts */
t->t_cmdbyte |= cmdbits;
if (!pckbc_put8042cmd(t))
aprint_error("pckbc: cmd word write error\n");
}
/*
* switch scancode translation on / off
* return nonzero on success
*/
static int
pckbc_xt_translation(void *self, pckbc_slot_t slot, int on)
{
struct pckbc_internal *t = self;
int ison;
if (ISSET(t->t_flags, PCKBC_CANT_TRANSLATE))
return (-1);
if (slot != PCKBC_KBD_SLOT) {
/* translation only for kbd slot */
if (on)
return (0);
else
return (1);
}
ison = t->t_cmdbyte & KC8_TRANS;
if ((on && ison) || (!on && !ison))
return (1);
t->t_cmdbyte ^= KC8_TRANS;
if (!pckbc_put8042cmd(t))
return (0);
/* read back to be sure */
if (!pckbc_get8042cmd(t))
return (0);
ison = t->t_cmdbyte & KC8_TRANS;
if ((on && ison) || (!on && !ison))
return (1);
return (0);
}
if (on) {
t->t_slotdata[slot]->poll_data = -1;
t->t_slotdata[slot]->poll_stat = -1;
} else {
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.
*/
if (t->t_sc) {
s = spltty();
pckbcintr(t->t_sc);
splx(s);
}
}
}
if (!q) {
/* XXX do something for live insertion? */
printf("pckbc: no dev for slot %d\n", slot);
(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
continue;
}
data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
if (q->polling) {
q->poll_data = data;
q->poll_stat = stat;
break; /* pckbc_poll_data() will get it */
}
served = 1;
data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
if (q != NULL)
rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
pckbportintr(t->t_pt, slot, data);
}
return (served);
}
int
pckbc_cnattach(bus_space_tag_t iot, bus_addr_t addr,
bus_size_t cmd_offset, pckbc_slot_t slot, int flags)
{
bus_space_handle_t ioh_d, ioh_c;
#ifdef PCKBC_CNATTACH_SELFTEST
int reply;
#endif
int res = 0;
#ifdef PCKBC_CNATTACH_SELFTEST
/*
* In some machines (e.g. netwinder) pckbc refuses to talk at
* all until we request a self-test.
*/
if (!pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST)) {
printf("pckbc: unable to request selftest\n");
res = EIO;
goto out;
}
reply = pckbc_poll_data1(&pckbc_consdata, PCKBC_KBD_SLOT);
if (reply != 0x55) {
printf("pckbc: selftest returned 0x%02x\n", reply);
res = EIO;
goto out;
}
#endif /* PCKBC_CNATTACH_SELFTEST */
/* init cmd byte, enable ports */
pckbc_consdata.t_cmdbyte = KC8_CPU;
if (!pckbc_put8042cmd(&pckbc_consdata)) {
printf("pckbc: cmd word write error\n");
res = EIO;
goto out;
}
res = pckbport_cnattach(&pckbc_consdata, &pckbc_ops, slot);