/*-
* Copyright (c) 1998, 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*-
* 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.
*
* 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. 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 for PC-style console
*/
/*
* Hackish support for a bell on the PC Keyboard; when a suitable beeper
* is found, it attaches itself into the pckbd driver here.
*/
void (*pckbd_bell_fn)(void *, u_int, u_int, u_int, int);
void *pckbd_bell_fn_arg;
void pckbd_bell(u_int, u_int, u_int, int);
int pckbd_scancode_translate(struct pckbd_internal *, int);
int pckbd_set_xtscancode(pckbport_tag_t, pckbport_slot_t,
struct pckbd_internal *);
int pckbd_init(struct pckbd_internal *, pckbport_tag_t, pckbport_slot_t,
int);
void pckbd_input(void *, int);
struct pckbd_internal pckbd_consdata;
int
pckbd_set_xtscancode(pckbport_tag_t kbctag, pckbport_slot_t kbcslot,
struct pckbd_internal *id)
{
int xt, res = 0;
u_char cmd[2];
/*
* 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. If the PCKBC_CANT_TRANSLATE pckbc flag was set, we
* enable software translation.
*
* XXX It would perhaps be a better choice to just use AT scan codes
* and not bother with this.
*/
xt = pckbport_xt_translation(kbctag, kbcslot, 1);
if (xt == 1) {
/* The 8042 is translating for us; use AT codes. */
cmd[0] = KBC_SETTABLE;
cmd[1] = 2;
res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
if (res) {
u_char cmdb[1];
aprint_debug("%s: error setting scanset 2\n", __func__);
/*
* XXX at least one keyboard is reported to lock up
* if a "set table" is attempted, thus the "reset".
* XXX ignore errors, scanset 2 should be
* default anyway.
*/
cmdb[0] = KBC_RESET;
(void)pckbport_poll_cmd(kbctag, kbcslot, cmdb, 1, 1,
0, 1);
pckbport_flush(kbctag, kbcslot);
res = 0;
}
if (id != NULL)
id->t_translating = 1;
} else if (xt == -1) {
/* Software translation required */
if (id != NULL)
id->t_translating = 0;
} else {
/* Stupid 8042; set keyboard to XT codes. */
cmd[0] = KBC_SETTABLE;
cmd[1] = 1;
res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 2, 0, 0, 0);
if (res)
aprint_debug("%s: error setting scanset 1\n", __func__);
if (id != NULL)
id->t_translating = 1;
}
return res;
}
static int
pckbd_is_console(pckbport_tag_t tag, pckbport_slot_t slot)
{
/* XXX duped from pckbd_enable, but we want to disable
* it even if it's the console kbd
*/
cmd[0] = KBC_DISABLE;
res = pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmd, 1, 0, 1, 0);
if (res)
return false;
/*
* these are three bad jokes
*/
static bool
check_keyboard_by_id(struct pckbport_attach_args *pa)
{
u_char cmd[1], resp[2];
int res;
cmd[0] = KBC_GETID;
res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 0);
if (res) {
aprint_debug("%s: getid failed with %d\n", __func__, res);
return false;
}
switch (resp[0]) {
case 0xab: case 0xac: /* Regular and NCD Sun keyboards */
case 0x2b: case 0x5d: /* Trust keyboard, raw and translated */
case 0x60: case 0x47: /* NMB SGI keyboard, raw and translated */
return true;
default:
aprint_debug("%s: getid returned %#x\n", __func__, resp[0]);
return false;
}
}
int
pckbdprobe(device_t parent, cfdata_t cf, void *aux)
{
struct pckbport_attach_args *pa = aux;
int res;
u_char cmd[1], resp[1];
/*
* XXX There are rumours that a keyboard can be connected
* to the aux port as well. For me, this didn't work.
* For further experiments, allow it if explicitly
* wired in the config file.
*/
if ((pa->pa_slot != PCKBPORT_KBD_SLOT) &&
(cf->cf_loc[PCKBPORTCF_SLOT] == PCKBPORTCF_SLOT_DEFAULT))
return 0;
/* Flush any garbage. */
pckbport_flush(pa->pa_tag, pa->pa_slot);
/* Reset the keyboard. */
cmd[0] = KBC_RESET;
res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 1, resp, 1);
if (res) {
aprint_debug("%s: reset error %d\n", __func__, res);
/*
* On Chromebooks reset fails but otherwise the controller
* works fine.
* Check keyboard IDs similar to Linux and Haiku.
* FreeBSD's approach here is to skip probing if
* coreboot is detected which is suboptimal as coreboot
* also supports some mac models which have no PC controller
*/
if (check_keyboard_by_id(pa)) {
if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot, NULL))
return 0;
return 2;
}
/*
* There is probably no keyboard connected.
* Let the probe succeed if the keyboard is used
* as console input - it can be connected later.
*/
return pckbd_is_console(pa->pa_tag, pa->pa_slot) ? 1 : 0;
}
if (resp[0] != KBR_RSTDONE) {
printf("%s: reset response %#x\n", __func__, resp[0]);
if (check_keyboard_by_id(pa)) {
if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot, NULL))
return 0;
return 2;
}
return 0;
}
/*
* 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.
*/
pckbport_flush(pa->pa_tag, pa->pa_slot);
if (pckbd_set_xtscancode(pa->pa_tag, pa->pa_slot, NULL))
return 0;
/*
* Translate scan codes from set 2 to set 1
*/
int
pckbd_scancode_translate(struct pckbd_internal *id, int datain)
{
if (id->t_translating != 0)
return datain;
if (datain == KBR_BREAK) {
id->t_releasing = 0x80; /* next keycode is a release */
return 0; /* consume scancode */
}
/*
* If we are mapping in the range 128-254, then make this
* an extended keycode, as table 1 codes are limited to
* the range 0-127 (the top bit is used for key up/break).
*/
if (datain > 0x7f) {
datain &= 0x7f;
id->t_extended0 = 0x80;
}
if (datain == 0) {
/*
* We don't know how to translate this scan code, but
* we can't silently eat it either (because there might
* have been an extended byte transmitted already).
* Hopefully this value will be harmless to the upper
* layers.
*/
return 0xff;
}
return datain | id->t_releasing;
}
static bool
pckbd_decode(struct pckbd_internal *id, int datain, u_int *type, int *dataout)
{
int key;
int releasing;
/*
* Got a console receive interrupt -
* the console processor wants to give us a character.
*/
void
pckbd_input(void *vsc, int data)
{
struct pckbd_softc *sc = vsc;
int key;
u_int type;
data = pckbd_scancode_translate(sc->id, data);
if (data == 0)
return;
#ifdef WSDISPLAY_COMPAT_RAWKBD
if (sc->rawkbd) {
u_char d = data;
wskbd_rawinput(sc->sc_wskbddev, &d, 1);
return;
}
#endif
if (pckbd_decode(sc->id, data, &type, &key))
wskbd_input(sc->sc_wskbddev, type, key);
}
int
pckbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
struct pckbd_softc *sc = v;
u_char cmdb[2];
switch (cmd) {
case WSKBDIO_GTYPE:
*(int *)data = WSKBD_TYPE_PC_XT;
return 0;
case WSKBDIO_SETLEDS:
cmdb[0] = KBC_MODEIND;
cmdb[1] = pckbd_led_encode(*(int *)data);
sc->sc_ledstate = cmdb[1];
return pckbport_enqueue_cmd(sc->id->t_kbctag, sc->id->t_kbcslot,
cmdb, 2, 0, 1, 0);
case WSKBDIO_GETLEDS:
*(int *)data = pckbd_led_decode(sc->sc_ledstate);
return 0;
#if 0
case WSKBDIO_COMPLEXBELL:
#define d ((struct wskbd_bell_data *)data)
/*
* Keyboard can't beep directly; we have an
* externally-provided global hook to do this.
*/
pckbd_bell(d->pitch, d->period, d->volume, 0);
#undef d
return 0;
#endif
#ifdef WSDISPLAY_COMPAT_RAWKBD
case WSKBDIO_SETMODE:
sc->rawkbd = (*(int *)data == WSKBD_RAW);
return 0;
#endif
}
return EPASSTHROUGH;
}
void
pckbd_bell(u_int pitch, u_int period, u_int volume, int poll)
{
if (pckbd_bell_fn != NULL)
(*pckbd_bell_fn)(pckbd_bell_fn_arg, pitch, period,
volume, poll);
}
int
pckbd_cnattach(pckbport_tag_t kbctag, int kbcslot)
{
int res;
u_char cmd[1];
res = pckbd_init(&pckbd_consdata, kbctag, kbcslot, 1);
/* We may allow the console to be attached if no keyboard is present */
#if defined(PCKBD_CNATTACH_MAY_FAIL)
if (res)
return res;
#endif
/* Just to be sure. */
cmd[0] = KBC_ENABLE;
res = pckbport_poll_cmd(kbctag, kbcslot, cmd, 1, 0, 0, 0);
#if defined(PCKBD_CNATTACH_MAY_FAIL)
if (res)
return res;
#endif