/*-
* Copyright (c) 1999 SASAKI Takesi All rights reserved.
* Copyright (c) 1999, 2000, 2002 TAKEMRUA, Shin All rights reserved.
* Copyright (c) 1999 PocketBSD Project. 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 PocketBSD project
* and its contributors.
* 4. Neither the name of the project 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.
*
*/
int
vrkiu_intr(void *arg)
{
struct vrkiu_softc *sc = arg;
/* When key scan finished, this entry is called. */
#if 0
DPRINTF(("vrkiu_intr: intr=%x scan=%x\n",
vrkiu_read(sc->sc_chip, KIUINT) & 7,
vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK));
#endif
/*
* First, we must clear the interrupt register because
* vrkiu_scan() may takes long time if a bitmap screen
* scrolls up and it makes us to miss some key release
* event.
*/
vrkiu_write(sc->sc_chip, KIUINT, 0x7); /* Clear all interrupt */
#if 1
/* just return if kiu is scanning keyboard. */
if ((vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
KIUSCANS_SSTAT_SCANNING)
return (0);
#endif
vrkiu_scan(sc->sc_chip);
return (0);
}
static int
countbits(int d)
{
int i, n;
for (i = 0, n = 0; i < NBBY; i++)
if (d & (1 << i))
n++;
return (n);
}
static void
eliminate_phantom_keys(struct vrkiu_chip *chip, unsigned short *scandata)
{
unsigned char inkey[KIU_NSCANLINE], *prevkey, *reskey;
int i, j;
#ifdef VRKIUDEBUG
int modified = 0;
static int prevmod = 0;
#endif
reskey = (unsigned char *)scandata;
for (i = 0; i < KIU_NSCANLINE; i++)
inkey[i] = reskey[i];
prevkey = (unsigned char *)chip->kc_scandata;
for (i = 0; i < KIU_NSCANLINE; i++) {
if (countbits(inkey[i]) > 1) {
for (j = 0; j < KIU_NSCANLINE; j++) {
if (i != j && (inkey[i] & inkey[j])) {
#ifdef VRKIUDEBUG
modified = 1;
if (!prevmod) {
DPRINTF(("vrkiu_scan: %x:%02x->%02x",
i, inkey[i], prevkey[i]));
DPRINTF((" %x:%02x->%02x\n",
j, inkey[j], prevkey[j]));
}
#endif
reskey[i] = prevkey[i];
reskey[j] = prevkey[j];
}
}
}
}
#ifdef VRKIUDEBUG
prevmod = modified;
#endif
}
static void
vrkiu_scan(struct vrkiu_chip* chip)
{
int i, j, modified, mask;
unsigned short scandata[KIU_NSCANLINE/2];
if (!chip->kc_enabled)
return;
for (i = 0; i < KIU_NSCANLINE / 2; i++) {
scandata[i] = vrkiu_read(chip, KIUDATP + i * 2);
}
eliminate_phantom_keys(chip, scandata);
for (i = 0; i < KIU_NSCANLINE / 2; i++) {
modified = scandata[i] ^ chip->kc_scandata[i];
chip->kc_scandata[i] = scandata[i];
mask = 1;
for (j = 0; j < 16; j++, mask <<= 1) {
/*
* Simultaneous keypresses are resolved by registering
* the one with the lowest bit index first.
*/
if (modified & mask) {
int key = i * 16 + j;
DPRINTF(("vrkiu_scan: %s(%d,%d)\n",
(scandata[i] & mask) ? "down" : "up",
i, j));
hpckbd_input(chip->kc_hpckbd,
(scandata[i] & mask), key);
}
}
}
}
/* called from bicons.c */
int
vrkiu_getc(void)
{
static int flag = 1;
/*
* XXX, currently
*/
if (flag) {
flag = 0;
printf("%s(%d): vrkiu_getc() is not implemented\n",
__FILE__, __LINE__);
}
return (0);
}
/* save hpckbd interface */
kc->kc_hpckbd = kbdif;
kc->kc_enabled = 1;
return (0);
}
int
vrkiu_poll(void *ic)
{
struct vrkiu_chip *kc = ic;
#if 1
/* wait until kiu completes keyboard scan. */
while ((vrkiu_read(kc, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
KIUSCANS_SSTAT_SCANNING)
/* wait until kiu completes keyboard scan */;
#endif
vrkiu_scan(kc);
return (0);
}
/*
* console support routine
*/
int
vrkiu_cnattach(bus_space_tag_t iot, int iobase)
{
static struct vrkiu_chip vrkiu_consdata_body;
bus_space_handle_t ioh;
if (vrkiu_consdata) {
panic("vrkiu is already attached as the console");
}
if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
printf("%s(%d): can't map bus space\n", __FILE__, __LINE__);
return (-1);
}