/*
* Copyright (c) 2005, Miodrag Vallat
* Copyright (c) 1997 Michael Smith. 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 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 AUTHOR 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.
*/
/*
* Driver for the Apollo Domain keyboard and mouse.
*
* Random notes on the Apollo keyboard :
*
* - Powers up in 'cooked' mode, where the alpha keys generate ascii rather
* than make/break codes. Other keys seem to behave OK though.
*
* - Alt L/R keys generate two-byte sequence :
* make break
* L 0xfe,2 0xfe,3
* R 0xfe,0 0xfe,1
*
* - Mouse activity shows up inline in 4-byte packets introduced with 0xdf.
* Byte 1 is 1MRL0000 where M, R, L are the mouse buttons, and 0 is
* down, 1 is up.
* Byte 2 is 2's complement X movement, +ve to the right.
* Byte 3 is 2's complement Y movement, +ve is up.
*
* - Keyboard recognises commands sent to it, all preceded by 0xff. Commands
* are echoed once sent completely.
*
* 0x00 go to cooked mode.
* 0x01 go to 'raw' (scancode) mode.
* 0x12,0x21 status report as <id1>\r<id2>\r<model>\r followed by 0xff
* and then the cooked/raw status.
* 0x21,0x81 beep on
* 0x21,0x82 beep off
*
* Some version examples :
*
* <3-@> <1-0> <SD-03687-MS> Apollo p/n 007121 REV 00 ('old-style' US layout)
* <3-@> <2-0> <SD-03683-MS> Apollo p/n 007121 REV 01 ('old-style' US layout)
* <3-@> <2-0> <SD-03980-MS> Apollo 3500? keyboard.
* <3-@> <X-X> <RX-60857-HW> HP p/n A1630-82001 R2
* ('new-style' off 425t, US layout),
* also Apollo p/n 014555-002
* ('new-style' off DN5500, US layout).
*/
/*
* If both hilkbd and dnkbd are configured, prefer the Domain
* keyboard as console (if we are here, we know the keyboard is
* plugged), unless the console keyboard has been claimed already
* (i.e. late hotplug with hil keyboard plugged first).
*/
#if NWSDISPLAY > 0
if (cn_tab->cn_putc == wsdisplay_cnputc) {
#if NHILKBD > 0
if (hil_is_console == -1) {
ka.console = 1;
hil_is_console = 0;
} else
ka.console = 0;
#else
ka.console = 1;
#endif
} else
#endif
{
ka.console = 0;
}
for (i = 0; ; i++) {
dat = dnkbd_pollin(sc, 10000);
if (dat == -1)
break;
if (i < sizeof(rspbuf))
rspbuf[i] = dat;
}
if (i > sizeof(rspbuf) || i == 0) {
aprint_error_dev(sc->sc_dev,
"unexpected identify string length %d\n", i);
rc = ENXIO;
goto out;
}
/*
* Make sure the identification string is NULL terminated
* (overwriting the keyboard mode byte if necessary).
*/
i--;
if (rspbuf[i] != 0)
rspbuf[i] = 0;
/*
* Now display the identification strings, if they changed.
*/
if (i != sc->sc_identlen || memcmp(rspbuf, sc->sc_ident, i) != 0) {
sc->sc_layout = KB_US;
sc->sc_identlen = i;
memcpy(sc->sc_ident, rspbuf, i);
if (cold == 0)
aprint_normal_dev(sc->sc_dev, "");
aprint_normal("model ");
word = rspbuf;
for (i = 0; i < 3; i++) {
end = strchr(word, '\r');
if (end == NULL)
break;
*end++ = '\0';
aprint_normal("<%s> ", word);
/*
* Parse the layout code if applicable
*/
if (i == 1 && *word++ == '3') {
if (*word == '-')
word++;
switch (*word) {
#if 0
default:
case ' ':
sc->sc_layout = KB_US;
break;
#endif
case 'a':
sc->sc_layout = KB_DE;
break;
case 'b':
sc->sc_layout = KB_FR;
break;
case 'c':
sc->sc_layout = KB_DK;
break;
case 'd':
sc->sc_layout = KB_SV;
break;
case 'e':
sc->sc_layout = KB_UK;
break;
case 'f':
sc->sc_layout = KB_JP;
break;
case 'g':
sc->sc_layout = KB_SG;
break;
}
}
word = end;
}
aprint_normal("\n");
}
/*
* Ready to work, the default channel is the keyboard.
*/
sc->sc_state = STATE_KEYBOARD;
out:
s = spltty();
sc->sc_flags = flags;
splx(s);
return rc;
}
/*
* State machine.
*
* In raw mode, the keyboard may feed us the following sequences:
* - on the keyboard channel:
* + a raw key code, in the range 0x01-0x7e, or'ed with 0x80 if key release.
* + the key repeat sequence 0x7f.
* - on the mouse channel:
* + a 3 byte mouse sequence (buttons state, dx move, dy move).
* - at any time:
* + a 2 byte channel sequence (0xff followed by the channel number) telling
* us which device the following input will come from.
* + if we get 0xff but an invalid channel number, this is a command echo.
* Currently we only handle this for bell commands, which size are known.
* Other commands are issued through dnkbd_send() which ``eats'' the echo.
*
* Older keyboards reset the channel to the keyboard (by sending ff 01) after
* every mouse packet.
*/
case STATE_CHANNEL:
switch (dat) {
case DNKEY_CHANNEL:
/*
* During hotplug, we might get spurious 0xff bytes.
* Ignore them.
*/
break;
case DNCHANNEL_RESET:
/*
* Identify the keyboard again. This will switch it to
* raw mode again. If this fails, we'll consider the
* keyboard as unplugged (to ignore further events until
* a successful reset).
*/
if (dnkbd_probe(sc) == 0) {
/*
* We need to attach wskbd and wsmouse children
* if this is a live first plug.
*/
if (!ISSET(sc->sc_flags, SF_ATTACHED))
dnkbd_attach_subdevices(sc);
SET(sc->sc_flags, SF_PLUGGED);
} else {
CLR(sc->sc_flags, SF_PLUGGED);
}
sc->sc_state = STATE_KEYBOARD;
break;
case DNCHANNEL_KBD:
sc->sc_state = STATE_KEYBOARD;
break;
case DNCHANNEL_MOUSE:
sc->sc_state = STATE_MOUSE;
sc->sc_mousepos = 0; /* just in case */
break;
case DNCMD_BELL:
/*
* We are getting a bell command echoed to us.
* Ignore it.
*/
sc->sc_state = STATE_ECHO;
sc->sc_echolen = 1; /* one byte to follow */
break;
default:
printf("%s: unexpected channel byte %02x\n",
device_xname(sc->sc_dev), dat);
break;
}
break;
case STATE_ECHO:
if (--sc->sc_echolen == 0) {
/* get back to the state we were in before the echo */
sc->sc_state = sc->sc_prevstate;
}
break;
}
void
dnevent_kbd(struct dnkbd_softc *sc, int dat)
{
if (!ISSET(sc->sc_flags, SF_PLUGGED))
return;
if (sc->sc_wskbddev == NULL)
return;
if (!ISSET(sc->sc_flags, SF_ENABLED))
return;
/*
* Even in raw mode, the caps lock key is treated specially:
* first key press causes event 0x7e, release causes no event;
* then a new key press causes nothing, and release causes
* event 0xfe. Moreover, while kept down, it does not produce
* repeat events.
*
* So the best we can do is fake the missed events, but this
* will not allow the capslock key to be remapped as a control
* key since it will not be possible to chord it with anything.
*/
dnevent_kbd_internal(sc, dat);
if ((dat & ~DNKEY_RELEASE) == DNKEY_CAPSLOCK)
dnevent_kbd_internal(sc, dat ^ DNKEY_RELEASE);
}
void
dnevent_kbd_internal(struct dnkbd_softc *sc, int dat)
{
u_int type;
int key;
int s;
dnkbd_decode(dat, &type, &key);
#ifdef WSDISPLAY_COMPAT_RAWKBD
if (sc->sc_rawkbd) {
u_char cbuf[2];
int c, j;
j = 0;
c = dnkbd_raw[key];
if (c != 0) {
/* fake extended scancode if necessary */
if (c & 0x80)
cbuf[j++] = 0xe0;
cbuf[j] = c & 0x7f;
if (type == WSCONS_EVENT_KEY_UP)
cbuf[j] |= 0x80;
j++;
}
if (j != 0) {
s = spltty();
wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
splx(s);
}
} else
#endif
{
s = spltty();
wskbd_input(sc->sc_wskbddev, type, key);
splx(s);
}
}
/*
* First byte is button status. It has the 0x80 bit always set, and
* the next 3 bits are *cleared* when the mouse buttons are pressed.
*/
#ifdef DEBUG
if (!ISSET(*dat, 0x80)) {
printf("%s: incorrect mouse packet %02x %02x %02x\n",
device_xname(sc->sc_dev), dat[0], dat[1], dat[2]);
return;
}
#endif
for (;;) {
iir = bus_space_read_1(bst, bsh, com_iir);
switch (iir & IIR_IMASK) {
case IIR_RLS:
/*
* Line status change. This should never happen,
* so silently ack the interrupt.
*/
c = bus_space_read_1(bst, bsh, com_lsr);
break;
case IIR_RXRDY:
case IIR_RXTOUT:
/*
* Data available. We process it byte by byte,
* unless we are doing polling work...
*/
if (ISSET(sc->sc_flags, SF_POLLING)) {
return 1;
}
for (;;) {
c = bus_space_read_1(bst, bsh, com_data);
switch (dnkbd_input(sc, c)) {
case EVENT_KEYBOARD:
dnevent_kbd(sc, c);
break;
#if NWSMOUSE > 0
case EVENT_MOUSE:
dnevent_mouse(sc, sc->sc_mousepkt);
break;
#endif
default: /* appease gcc */
break;
}
lsr = bus_space_read_1(bst, bsh, com_lsr) &
LSR_RCV_MASK;
if (lsr == 0)
break;
else if (lsr != LSR_RXRDY) {
/* ignore error */
break;
}
}
break;
case IIR_TXRDY:
/*
* Transmit available. Since we do all our commands
* in polling mode, we do not need to do anything here.
*/
break;
default:
if (iir & IIR_NOPEND)
return claimed;
/* FALLTHROUGH */
case IIR_MLSC:
/*
* Modem status change. This should never happen,
* so silently ack the interrupt.
*/
c = bus_space_read_1(bst, bsh, com_msr);
break;
}
claimed = 1;
}
}
/*
* Wskbd callbacks
*/
int
dnkbd_enable(void *v, int on)
{
struct dnkbd_softc *sc = v;
if (on) {
if (ISSET(sc->sc_flags, SF_ENABLED))
return EBUSY;
SET(sc->sc_flags, SF_ENABLED);
} else {
if (ISSET(sc->sc_flags, SF_CONSOLE))
return EBUSY;
CLR(sc->sc_flags, SF_ENABLED);
}
return 0;
}
void
dnkbd_set_leds(void *v, int leds)
{
/*
* Not supported. There is only one LED on this keyboard, and
* is hardware tied to the caps lock key.
*/
}