atkbd.c revision 58271
142421Syokota/*-
242421Syokota * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
342421Syokota * All rights reserved.
442421Syokota *
542421Syokota * Redistribution and use in source and binary forms, with or without
642421Syokota * modification, are permitted provided that the following conditions
742421Syokota * are met:
842421Syokota * 1. Redistributions of source code must retain the above copyright
942421Syokota *    notice, this list of conditions and the following disclaimer as
1042421Syokota *    the first lines of this file unmodified.
1142421Syokota * 2. Redistributions in binary form must reproduce the above copyright
1242421Syokota *    notice, this list of conditions and the following disclaimer in the
1342421Syokota *    documentation and/or other materials provided with the distribution.
1442421Syokota *
1542421Syokota * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
1642421Syokota * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1742421Syokota * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1842421Syokota * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1942421Syokota * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2042421Syokota * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2142421Syokota * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2242421Syokota * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2342421Syokota * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2442421Syokota * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2542421Syokota *
2650477Speter * $FreeBSD: head/sys/dev/atkbdc/atkbd.c 58271 2000-03-19 03:25:13Z yokota $
2742421Syokota */
2842421Syokota
2942421Syokota#include "opt_kbd.h"
3044628Syokota#include "opt_atkbd.h"
3142421Syokota
3242421Syokota#include <sys/param.h>
3342421Syokota#include <sys/systm.h>
3442421Syokota#include <sys/kernel.h>
3547336Syokota#include <sys/bus.h>
3642421Syokota#include <sys/proc.h>
3742421Syokota#include <sys/malloc.h>
3842421Syokota
3958271Syokota#include <machine/bus.h>
4058271Syokota#include <machine/resource.h>
4158271Syokota
4255731Syokota#ifdef __i386__
4355731Syokota#include <machine/md_var.h>
4455731Syokota#include <machine/psl.h>
4555731Syokota#include <machine/vm86.h>
4655731Syokota#include <machine/pc/bios.h>
4755731Syokota
4855731Syokota#include <vm/vm.h>
4955731Syokota#include <vm/pmap.h>
5055731Syokota#endif /* __i386__ */
5155731Syokota
5242421Syokota#include <dev/kbd/kbdreg.h>
5342421Syokota#include <dev/kbd/atkbdreg.h>
5442421Syokota#include <dev/kbd/atkbdcreg.h>
5542421Syokota
5642831Syokota#include <isa/isareg.h>
5742831Syokota
5842421Syokotastatic timeout_t	atkbd_timeout;
5942421Syokota
6042421Syokotaint
6158271Syokotaatkbd_probe_unit(int unit, int ctlr, int irq, int flags)
6242421Syokota{
6342421Syokota	keyboard_switch_t *sw;
6442421Syokota	int args[2];
6544628Syokota	int error;
6642421Syokota
6742421Syokota	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
6842421Syokota	if (sw == NULL)
6942421Syokota		return ENXIO;
7042421Syokota
7158271Syokota	args[0] = ctlr;
7242421Syokota	args[1] = irq;
7344628Syokota	error = (*sw->probe)(unit, args, flags);
7444628Syokota	if (error)
7544628Syokota		return error;
7644628Syokota	return 0;
7742421Syokota}
7842421Syokota
7942421Syokotaint
8058271Syokotaatkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
8142421Syokota{
8242421Syokota	keyboard_switch_t *sw;
8344628Syokota	int args[2];
8442421Syokota	int error;
8542421Syokota
8642421Syokota	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
8742421Syokota	if (sw == NULL)
8842421Syokota		return ENXIO;
8942421Syokota
9042421Syokota	/* reset, initialize and enable the device */
9158271Syokota	args[0] = ctlr;
9244628Syokota	args[1] = irq;
9350154Syokota	*kbd = NULL;
9444628Syokota	error = (*sw->probe)(unit, args, flags);
9542421Syokota	if (error)
9644628Syokota		return error;
9750154Syokota	error = (*sw->init)(unit, kbd, args, flags);
9844628Syokota	if (error)
9944628Syokota		return error;
10050154Syokota	(*sw->enable)(*kbd);
10142421Syokota
10242421Syokota#ifdef KBD_INSTALL_CDEV
10342421Syokota	/* attach a virtual keyboard cdev */
10450154Syokota	error = kbd_attach(*kbd);
10542421Syokota	if (error)
10642421Syokota		return error;
10742421Syokota#endif
10842421Syokota
10942421Syokota	/*
11042421Syokota	 * This is a kludge to compensate for lost keyboard interrupts.
11142421Syokota	 * A similar code used to be in syscons. See below. XXX
11242421Syokota	 */
11350154Syokota	atkbd_timeout(*kbd);
11442421Syokota
11542421Syokota	if (bootverbose)
11650154Syokota		(*sw->diag)(*kbd, bootverbose);
11742421Syokota	return 0;
11842421Syokota}
11942421Syokota
12042421Syokotastatic void
12142421Syokotaatkbd_timeout(void *arg)
12242421Syokota{
12342421Syokota	keyboard_t *kbd;
12442421Syokota	int s;
12542421Syokota
12656334Syokota	/*
12756334Syokota	 * The original text of the following comments are extracted
12856334Syokota	 * from syscons.c (1.287)
12956334Syokota	 *
13042421Syokota	 * With release 2.1 of the Xaccel server, the keyboard is left
13142421Syokota	 * hanging pretty often. Apparently an interrupt from the
13242421Syokota	 * keyboard is lost, and I don't know why (yet).
13356334Syokota	 * This ugly hack calls the low-level interrupt routine if input
13456334Syokota	 * is ready for the keyboard and conveniently hides the problem. XXX
13556334Syokota	 *
13656334Syokota	 * Try removing anything stuck in the keyboard controller; whether
13756334Syokota	 * it's a keyboard scan code or mouse data. The low-level
13856334Syokota	 * interrupt routine doesn't read the mouse data directly,
13956334Syokota	 * but the keyboard controller driver will, as a side effect.
14042421Syokota	 */
14142421Syokota	/*
14256334Syokota	 * And here is bde's original comment about this:
14356334Syokota	 *
14456334Syokota	 * This is necessary to handle edge triggered interrupts - if we
14556334Syokota	 * returned when our IRQ is high due to unserviced input, then there
14656334Syokota	 * would be no more keyboard IRQs until the keyboard is reset by
14756334Syokota	 * external powers.
14856334Syokota	 *
14956334Syokota	 * The keyboard apparently unwedges the irq in most cases.
15042421Syokota	 */
15142421Syokota	s = spltty();
15242421Syokota	kbd = (keyboard_t *)arg;
15342421Syokota	if ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) {
15442421Syokota		/*
15542421Syokota		 * We have seen the lock flag is not set. Let's reset
15642421Syokota		 * the flag early, otherwise the LED update routine fails
15742421Syokota		 * which may want the lock during the interrupt routine.
15842421Syokota		 */
15942421Syokota		(*kbdsw[kbd->kb_index]->lock)(kbd, FALSE);
16042421Syokota		if ((*kbdsw[kbd->kb_index]->check_char)(kbd))
16144628Syokota			(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
16242421Syokota	}
16342421Syokota	splx(s);
16442421Syokota	timeout(atkbd_timeout, arg, hz/10);
16542421Syokota}
16642421Syokota
16742421Syokota/* LOW-LEVEL */
16842421Syokota
16942421Syokota#include <machine/limits.h>
17042421Syokota#include <machine/console.h>
17142421Syokota#include <machine/clock.h>
17242421Syokota
17342421Syokota#define ATKBD_DEFAULT	0
17442421Syokota
17542421Syokotatypedef struct atkbd_state {
17642421Syokota	KBDC		kbdc;		/* keyboard controller */
17742421Syokota					/* XXX: don't move this field; pcvt
17842421Syokota					 * expects `kbdc' to be the first
17942421Syokota					 * field in this structure. */
18042421Syokota	int		ks_mode;	/* input mode (K_XLATE,K_RAW,K_CODE) */
18142421Syokota	int		ks_flags;	/* flags */
18242421Syokota#define COMPOSE		(1 << 0)
18344628Syokota	int		ks_polling;
18442421Syokota	int		ks_state;	/* shift/lock key state */
18542421Syokota	int		ks_accents;	/* accent key index (> 0) */
18642421Syokota	u_int		ks_composed_char; /* composed char code (> 0) */
18742421Syokota	u_char		ks_prefix;	/* AT scan code prefix */
18842421Syokota} atkbd_state_t;
18942421Syokota
19042421Syokota/* keyboard driver declaration */
19142421Syokotastatic int		atkbd_configure(int flags);
19242421Syokotastatic kbd_probe_t	atkbd_probe;
19342421Syokotastatic kbd_init_t	atkbd_init;
19442421Syokotastatic kbd_term_t	atkbd_term;
19542421Syokotastatic kbd_intr_t	atkbd_intr;
19642421Syokotastatic kbd_test_if_t	atkbd_test_if;
19742421Syokotastatic kbd_enable_t	atkbd_enable;
19842421Syokotastatic kbd_disable_t	atkbd_disable;
19942421Syokotastatic kbd_read_t	atkbd_read;
20042421Syokotastatic kbd_check_t	atkbd_check;
20142421Syokotastatic kbd_read_char_t	atkbd_read_char;
20242421Syokotastatic kbd_check_char_t	atkbd_check_char;
20342421Syokotastatic kbd_ioctl_t	atkbd_ioctl;
20442421Syokotastatic kbd_lock_t	atkbd_lock;
20542421Syokotastatic kbd_clear_state_t atkbd_clear_state;
20642421Syokotastatic kbd_get_state_t	atkbd_get_state;
20742421Syokotastatic kbd_set_state_t	atkbd_set_state;
20844628Syokotastatic kbd_poll_mode_t	atkbd_poll;
20942421Syokota
21042421Syokotakeyboard_switch_t atkbdsw = {
21142421Syokota	atkbd_probe,
21242421Syokota	atkbd_init,
21342421Syokota	atkbd_term,
21442421Syokota	atkbd_intr,
21542421Syokota	atkbd_test_if,
21642421Syokota	atkbd_enable,
21742421Syokota	atkbd_disable,
21842421Syokota	atkbd_read,
21942421Syokota	atkbd_check,
22042421Syokota	atkbd_read_char,
22142421Syokota	atkbd_check_char,
22242421Syokota	atkbd_ioctl,
22342421Syokota	atkbd_lock,
22442421Syokota	atkbd_clear_state,
22542421Syokota	atkbd_get_state,
22642421Syokota	atkbd_set_state,
22742421Syokota	genkbd_get_fkeystr,
22844628Syokota	atkbd_poll,
22942421Syokota	genkbd_diag,
23042421Syokota};
23142421Syokota
23242421SyokotaKEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
23342421Syokota
23442421Syokota/* local functions */
23555731Syokotastatic int		get_typematic(keyboard_t *kbd);
23642421Syokotastatic int		setup_kbd_port(KBDC kbdc, int port, int intr);
23742421Syokotastatic int		get_kbd_echo(KBDC kbdc);
23842421Syokotastatic int		probe_keyboard(KBDC kbdc, int flags);
23942421Syokotastatic int		init_keyboard(KBDC kbdc, int *type, int flags);
24042421Syokotastatic int		write_kbd(KBDC kbdc, int command, int data);
24142421Syokotastatic int		get_kbd_id(KBDC kbdc);
24244628Syokotastatic int		typematic(int delay, int rate);
24354543Syokotastatic int		typematic_delay(int delay);
24454543Syokotastatic int		typematic_rate(int rate);
24542421Syokota
24642421Syokota/* local variables */
24742421Syokota
24842421Syokota/* the initial key map, accent map and fkey strings */
24944628Syokota#ifdef ATKBD_DFLT_KEYMAP
25044628Syokota#define KBD_DFLT_KEYMAP
25144628Syokota#include "atkbdmap.h"
25244628Syokota#endif
25342831Syokota#include <dev/kbd/kbdtables.h>
25442421Syokota
25542421Syokota/* structures for the default keyboard */
25642421Syokotastatic keyboard_t	default_kbd;
25742421Syokotastatic atkbd_state_t	default_kbd_state;
25842421Syokotastatic keymap_t		default_keymap;
25942421Syokotastatic accentmap_t	default_accentmap;
26042421Syokotastatic fkeytab_t	default_fkeytab[NUM_FKEYS];
26142421Syokota
26242421Syokota/*
26342421Syokota * The back door to the keyboard driver!
26442421Syokota * This function is called by the console driver, via the kbdio module,
26542421Syokota * to tickle keyboard drivers when the low-level console is being initialized.
26642421Syokota * Almost nothing in the kernel has been initialied yet.  Try to probe
26742421Syokota * keyboards if possible.
26842421Syokota * NOTE: because of the way the low-level conole is initialized, this routine
26942421Syokota * may be called more than once!!
27042421Syokota */
27142421Syokotastatic int
27242421Syokotaatkbd_configure(int flags)
27342421Syokota{
27442421Syokota	keyboard_t *kbd;
27542421Syokota	int arg[2];
27644628Syokota	int i;
27742421Syokota
27848878Syokota	/* probe the keyboard controller */
27948878Syokota	atkbdc_configure();
28048878Syokota
28146764Syokota	/* if the driver is disabled, unregister the keyboard if any */
28246764Syokota	if ((resource_int_value("atkbd", ATKBD_DEFAULT, "disabled", &i) == 0)
28346764Syokota	    && i != 0) {
28446764Syokota		i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
28546764Syokota		if (i >= 0) {
28646764Syokota			kbd = kbd_get_keyboard(i);
28746764Syokota			kbd_unregister(kbd);
28846764Syokota			kbd->kb_flags &= ~KB_REGISTERED;
28944628Syokota		}
29048878Syokota		return 0;
29144628Syokota	}
29245720Speter
29346764Syokota	/* XXX: a kludge to obtain the device configuration flags */
29446764Syokota	if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
29546764Syokota		flags |= i;
29646764Syokota
29742421Syokota	/* probe the default keyboard */
29842421Syokota	arg[0] = -1;
29942421Syokota	arg[1] = -1;
30044628Syokota	kbd = NULL;
30144628Syokota	if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
30242421Syokota		return 0;
30344628Syokota	if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
30444628Syokota		return 0;
30542421Syokota
30644628Syokota	/* return the number of found keyboards */
30744628Syokota	return 1;
30844628Syokota}
30944628Syokota
31044628Syokota/* low-level functions */
31144628Syokota
31244628Syokota/* detect a keyboard */
31344628Syokotastatic int
31444628Syokotaatkbd_probe(int unit, void *arg, int flags)
31544628Syokota{
31644628Syokota	KBDC kbdc;
31758271Syokota	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
31844628Syokota
31944628Syokota	/* XXX */
32044628Syokota	if (unit == ATKBD_DEFAULT) {
32144628Syokota		if (KBD_IS_PROBED(&default_kbd))
32242421Syokota			return 0;
32342421Syokota	}
32442421Syokota
32558271Syokota	kbdc = atkbdc_open(data[0]);
32644628Syokota	if (kbdc == NULL)
32744628Syokota		return ENXIO;
32844628Syokota	if (probe_keyboard(kbdc, flags)) {
32944628Syokota		if (flags & KB_CONF_FAIL_IF_NO_KBD)
33044628Syokota			return ENXIO;
33142421Syokota	}
33244628Syokota	return 0;
33342421Syokota}
33442421Syokota
33544628Syokota/* reset and initialize the device */
33642421Syokotastatic int
33744628Syokotaatkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
33842421Syokota{
33942421Syokota	keyboard_t *kbd;
34042421Syokota	atkbd_state_t *state;
34142421Syokota	keymap_t *keymap;
34242421Syokota	accentmap_t *accmap;
34342421Syokota	fkeytab_t *fkeymap;
34442421Syokota	int fkeymap_size;
34555731Syokota	int delay[2];
34658271Syokota	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
34742421Syokota
34842421Syokota	/* XXX */
34942421Syokota	if (unit == ATKBD_DEFAULT) {
35042421Syokota		*kbdp = kbd = &default_kbd;
35144628Syokota		if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
35242421Syokota			return 0;
35342421Syokota		state = &default_kbd_state;
35442421Syokota		keymap = &default_keymap;
35542421Syokota		accmap = &default_accentmap;
35642421Syokota		fkeymap = default_fkeytab;
35742421Syokota		fkeymap_size =
35842421Syokota			sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
35942421Syokota	} else if (*kbdp == NULL) {
36042421Syokota		*kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT);
36142421Syokota		if (kbd == NULL)
36242421Syokota			return ENOMEM;
36342421Syokota		bzero(kbd, sizeof(*kbd));
36442421Syokota		state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT);
36542421Syokota		keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
36642421Syokota		accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
36742421Syokota		fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
36842421Syokota		fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
36942421Syokota		if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
37042421Syokota		     || (fkeymap == NULL)) {
37142421Syokota			if (state != NULL)
37242421Syokota				free(state, M_DEVBUF);
37342421Syokota			if (keymap != NULL)
37442421Syokota				free(keymap, M_DEVBUF);
37542421Syokota			if (accmap != NULL)
37642421Syokota				free(accmap, M_DEVBUF);
37742421Syokota			if (fkeymap != NULL)
37842421Syokota				free(fkeymap, M_DEVBUF);
37942421Syokota			free(kbd, M_DEVBUF);
38042421Syokota			return ENOMEM;
38142421Syokota		}
38242421Syokota		bzero(state, sizeof(*state));
38344628Syokota	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
38442421Syokota		return 0;
38542421Syokota	} else {
38642421Syokota		kbd = *kbdp;
38742421Syokota		state = (atkbd_state_t *)kbd->kb_data;
38842421Syokota		bzero(state, sizeof(*state));
38942421Syokota		keymap = kbd->kb_keymap;
39042421Syokota		accmap = kbd->kb_accentmap;
39142421Syokota		fkeymap = kbd->kb_fkeytab;
39242421Syokota		fkeymap_size = kbd->kb_fkeytab_size;
39342421Syokota	}
39442421Syokota
39544628Syokota	if (!KBD_IS_PROBED(kbd)) {
39658271Syokota		state->kbdc = atkbdc_open(data[0]);
39744628Syokota		if (state->kbdc == NULL)
39842421Syokota			return ENXIO;
39944628Syokota		kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
40058271Syokota				0, 0);
40144628Syokota		bcopy(&key_map, keymap, sizeof(key_map));
40244628Syokota		bcopy(&accent_map, accmap, sizeof(accent_map));
40344628Syokota		bcopy(fkey_tab, fkeymap,
40444628Syokota		      imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
40544628Syokota		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
40644628Syokota		kbd->kb_data = (void *)state;
40744628Syokota
40844628Syokota		if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
40944628Syokota			if (flags & KB_CONF_FAIL_IF_NO_KBD)
41044628Syokota				return ENXIO;
41144628Syokota		} else {
41244628Syokota			KBD_FOUND_DEVICE(kbd);
41344628Syokota		}
41444628Syokota		atkbd_clear_state(kbd);
41544628Syokota		state->ks_mode = K_XLATE;
41644628Syokota		/*
41744628Syokota		 * FIXME: set the initial value for lock keys in ks_state
41844628Syokota		 * according to the BIOS data?
41944628Syokota		 */
42044628Syokota		KBD_PROBE_DONE(kbd);
42142421Syokota	}
42244628Syokota	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
42349820Syokota		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
42442421Syokota		if (KBD_HAS_DEVICE(kbd)
42544628Syokota	    	    && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
42644628Syokota	    	    && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD))
42742421Syokota			return ENXIO;
42844628Syokota		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
42955731Syokota		get_typematic(kbd);
43055731Syokota		delay[0] = kbd->kb_delay1;
43155731Syokota		delay[1] = kbd->kb_delay2;
43255731Syokota		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
43342421Syokota		KBD_INIT_DONE(kbd);
43442421Syokota	}
43542421Syokota	if (!KBD_IS_CONFIGURED(kbd)) {
43642421Syokota		if (kbd_register(kbd) < 0)
43742421Syokota			return ENXIO;
43842421Syokota		KBD_CONFIG_DONE(kbd);
43942421Syokota	}
44042421Syokota
44142421Syokota	return 0;
44242421Syokota}
44342421Syokota
44442421Syokota/* finish using this keyboard */
44542421Syokotastatic int
44642421Syokotaatkbd_term(keyboard_t *kbd)
44742421Syokota{
44842421Syokota	kbd_unregister(kbd);
44942421Syokota	return 0;
45042421Syokota}
45142421Syokota
45242421Syokota/* keyboard interrupt routine */
45342421Syokotastatic int
45444628Syokotaatkbd_intr(keyboard_t *kbd, void *arg)
45542421Syokota{
45642421Syokota	atkbd_state_t *state;
45755731Syokota	int delay[2];
45842421Syokota	int c;
45942421Syokota
46042421Syokota	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
46142421Syokota		/* let the callback function to process the input */
46242421Syokota		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
46342421Syokota					    kbd->kb_callback.kc_arg);
46442421Syokota	} else {
46542421Syokota		/* read and discard the input; no one is waiting for input */
46642421Syokota		do {
46742421Syokota			c = atkbd_read_char(kbd, FALSE);
46842421Syokota		} while (c != NOKEY);
46942421Syokota
47042421Syokota		if (!KBD_HAS_DEVICE(kbd)) {
47142421Syokota			/*
47242421Syokota			 * The keyboard was not detected before;
47342421Syokota			 * it must have been reconnected!
47442421Syokota			 */
47542421Syokota			state = (atkbd_state_t *)kbd->kb_data;
47642421Syokota			init_keyboard(state->kbdc, &kbd->kb_type,
47742421Syokota				      kbd->kb_config);
47842421Syokota			atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
47955731Syokota			get_typematic(kbd);
48055731Syokota			delay[0] = kbd->kb_delay1;
48155731Syokota			delay[1] = kbd->kb_delay2;
48255731Syokota			atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
48342421Syokota			KBD_FOUND_DEVICE(kbd);
48442421Syokota		}
48542421Syokota	}
48642421Syokota	return 0;
48742421Syokota}
48842421Syokota
48942421Syokota/* test the interface to the device */
49042421Syokotastatic int
49142421Syokotaatkbd_test_if(keyboard_t *kbd)
49242421Syokota{
49342421Syokota	int error;
49442421Syokota	int s;
49542421Syokota
49642421Syokota	error = 0;
49742421Syokota	empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
49842421Syokota	s = spltty();
49942421Syokota	if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
50042421Syokota		error = EIO;
50142421Syokota	else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
50242421Syokota		error = EIO;
50342421Syokota	splx(s);
50442421Syokota
50542421Syokota	return error;
50642421Syokota}
50742421Syokota
50842421Syokota/*
50942421Syokota * Enable the access to the device; until this function is called,
51042421Syokota * the client cannot read from the keyboard.
51142421Syokota */
51242421Syokotastatic int
51342421Syokotaatkbd_enable(keyboard_t *kbd)
51442421Syokota{
51542421Syokota	int s;
51642421Syokota
51742421Syokota	s = spltty();
51842421Syokota	KBD_ACTIVATE(kbd);
51942421Syokota	splx(s);
52042421Syokota	return 0;
52142421Syokota}
52242421Syokota
52342421Syokota/* disallow the access to the device */
52442421Syokotastatic int
52542421Syokotaatkbd_disable(keyboard_t *kbd)
52642421Syokota{
52742421Syokota	int s;
52842421Syokota
52942421Syokota	s = spltty();
53042421Syokota	KBD_DEACTIVATE(kbd);
53142421Syokota	splx(s);
53242421Syokota	return 0;
53342421Syokota}
53442421Syokota
53542421Syokota/* read one byte from the keyboard if it's allowed */
53642421Syokotastatic int
53742421Syokotaatkbd_read(keyboard_t *kbd, int wait)
53842421Syokota{
53942421Syokota	int c;
54042421Syokota
54142421Syokota	if (wait)
54242421Syokota		c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
54342421Syokota	else
54442421Syokota		c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
54554382Syokota	if (c != -1)
54654382Syokota		++kbd->kb_count;
54742421Syokota	return (KBD_IS_ACTIVE(kbd) ? c : -1);
54842421Syokota}
54942421Syokota
55042421Syokota/* check if data is waiting */
55142421Syokotastatic int
55242421Syokotaatkbd_check(keyboard_t *kbd)
55342421Syokota{
55442421Syokota	if (!KBD_IS_ACTIVE(kbd))
55542421Syokota		return FALSE;
55642421Syokota	return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
55742421Syokota}
55842421Syokota
55942421Syokota/* read char from the keyboard */
56042421Syokotastatic u_int
56142421Syokotaatkbd_read_char(keyboard_t *kbd, int wait)
56242421Syokota{
56342421Syokota	atkbd_state_t *state;
56442421Syokota	u_int action;
56542421Syokota	int scancode;
56642421Syokota	int keycode;
56742421Syokota
56842421Syokota	state = (atkbd_state_t *)kbd->kb_data;
56942421Syokotanext_code:
57042421Syokota	/* do we have a composed char to return? */
57142421Syokota	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
57242421Syokota		action = state->ks_composed_char;
57342421Syokota		state->ks_composed_char = 0;
57442421Syokota		if (action > UCHAR_MAX)
57542421Syokota			return ERRKEY;
57642421Syokota		return action;
57742421Syokota	}
57842421Syokota
57942421Syokota	/* see if there is something in the keyboard port */
58042421Syokota	if (wait) {
58142421Syokota		do {
58242421Syokota			scancode = read_kbd_data(state->kbdc);
58342421Syokota		} while (scancode == -1);
58442421Syokota	} else {
58542421Syokota		scancode = read_kbd_data_no_wait(state->kbdc);
58642421Syokota		if (scancode == -1)
58742421Syokota			return NOKEY;
58842421Syokota	}
58954382Syokota	++kbd->kb_count;
59042421Syokota
59154382Syokota#if KBDIO_DEBUG >= 10
59254382Syokota	printf("atkbd_read_char(): scancode:0x%x\n", scancode);
59354382Syokota#endif
59454382Syokota
59542421Syokota	/* return the byte as is for the K_RAW mode */
59642421Syokota	if (state->ks_mode == K_RAW)
59742421Syokota		return scancode;
59842421Syokota
59942421Syokota	/* translate the scan code into a keycode */
60042421Syokota	keycode = scancode & 0x7F;
60142421Syokota	switch (state->ks_prefix) {
60242421Syokota	case 0x00:	/* normal scancode */
60342421Syokota		switch(scancode) {
60442421Syokota		case 0xB8:	/* left alt (compose key) released */
60542421Syokota			if (state->ks_flags & COMPOSE) {
60642421Syokota				state->ks_flags &= ~COMPOSE;
60742421Syokota				if (state->ks_composed_char > UCHAR_MAX)
60842421Syokota					state->ks_composed_char = 0;
60942421Syokota			}
61042421Syokota			break;
61142421Syokota		case 0x38:	/* left alt (compose key) pressed */
61242421Syokota			if (!(state->ks_flags & COMPOSE)) {
61342421Syokota				state->ks_flags |= COMPOSE;
61442421Syokota				state->ks_composed_char = 0;
61542421Syokota			}
61642421Syokota			break;
61742421Syokota		case 0xE0:
61842421Syokota		case 0xE1:
61942421Syokota			state->ks_prefix = scancode;
62042421Syokota			goto next_code;
62142421Syokota		}
62242421Syokota		break;
62342421Syokota	case 0xE0:      /* 0xE0 prefix */
62442421Syokota		state->ks_prefix = 0;
62542421Syokota		switch (keycode) {
62642421Syokota		case 0x1C:	/* right enter key */
62742421Syokota			keycode = 0x59;
62842421Syokota			break;
62942421Syokota		case 0x1D:	/* right ctrl key */
63042421Syokota			keycode = 0x5A;
63142421Syokota			break;
63242421Syokota		case 0x35:	/* keypad divide key */
63342421Syokota	    		keycode = 0x5B;
63442421Syokota	    		break;
63542421Syokota		case 0x37:	/* print scrn key */
63642421Syokota	    		keycode = 0x5C;
63742421Syokota	    		break;
63842421Syokota		case 0x38:	/* right alt key (alt gr) */
63942421Syokota	    		keycode = 0x5D;
64042421Syokota	    		break;
64143337Syokota		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
64243337Syokota			keycode = 0x68;
64343337Syokota	    		break;
64442421Syokota		case 0x47:	/* grey home key */
64542421Syokota	    		keycode = 0x5E;
64642421Syokota	    		break;
64742421Syokota		case 0x48:	/* grey up arrow key */
64842421Syokota	    		keycode = 0x5F;
64942421Syokota	    		break;
65042421Syokota		case 0x49:	/* grey page up key */
65142421Syokota	    		keycode = 0x60;
65242421Syokota	    		break;
65342421Syokota		case 0x4B:	/* grey left arrow key */
65442421Syokota	    		keycode = 0x61;
65542421Syokota	    		break;
65642421Syokota		case 0x4D:	/* grey right arrow key */
65742421Syokota	    		keycode = 0x62;
65842421Syokota	    		break;
65942421Syokota		case 0x4F:	/* grey end key */
66042421Syokota	    		keycode = 0x63;
66142421Syokota	    		break;
66242421Syokota		case 0x50:	/* grey down arrow key */
66342421Syokota	    		keycode = 0x64;
66442421Syokota	    		break;
66542421Syokota		case 0x51:	/* grey page down key */
66642421Syokota	    		keycode = 0x65;
66742421Syokota	    		break;
66842421Syokota		case 0x52:	/* grey insert key */
66942421Syokota	    		keycode = 0x66;
67042421Syokota	    		break;
67142421Syokota		case 0x53:	/* grey delete key */
67242421Syokota	    		keycode = 0x67;
67342421Syokota	    		break;
67442421Syokota		/* the following 3 are only used on the MS "Natural" keyboard */
67542421Syokota		case 0x5b:	/* left Window key */
67642421Syokota	    		keycode = 0x69;
67742421Syokota	    		break;
67842421Syokota		case 0x5c:	/* right Window key */
67942421Syokota	    		keycode = 0x6a;
68042421Syokota	    		break;
68142421Syokota		case 0x5d:	/* menu key */
68242421Syokota	    		keycode = 0x6b;
68342421Syokota	    		break;
68442421Syokota		default:	/* ignore everything else */
68542421Syokota	    		goto next_code;
68642421Syokota		}
68742421Syokota		break;
68842421Syokota    	case 0xE1:	/* 0xE1 prefix */
68943337Syokota		/*
69043337Syokota		 * The pause/break key on the 101 keyboard produces:
69143337Syokota		 * E1-1D-45 E1-9D-C5
69243337Syokota		 * Ctrl-pause/break produces:
69343337Syokota		 * E0-46 E0-C6 (See above.)
69443337Syokota		 */
69542421Syokota		state->ks_prefix = 0;
69642421Syokota		if (keycode == 0x1D)
69742421Syokota	    		state->ks_prefix = 0x1D;
69842421Syokota		goto next_code;
69942421Syokota		/* NOT REACHED */
70042421Syokota    	case 0x1D:	/* pause / break */
70142421Syokota		state->ks_prefix = 0;
70242421Syokota		if (keycode != 0x45)
70343337Syokota			goto next_code;
70442421Syokota		keycode = 0x68;
70542421Syokota		break;
70642421Syokota	}
70742421Syokota
70843337Syokota	if (kbd->kb_type == KB_84) {
70943337Syokota		switch (keycode) {
71043337Syokota		case 0x37:	/* *(numpad)/print screen */
71143337Syokota			if (state->ks_flags & SHIFTS)
71243337Syokota	    			keycode = 0x5c;	/* print screen */
71343337Syokota			break;
71443337Syokota		case 0x45:	/* num lock/pause */
71543337Syokota			if (state->ks_flags & CTLS)
71643337Syokota				keycode = 0x68;	/* pause */
71743337Syokota			break;
71843337Syokota		case 0x46:	/* scroll lock/break */
71943337Syokota			if (state->ks_flags & CTLS)
72043337Syokota				keycode = 0x6c;	/* break */
72143337Syokota			break;
72243337Syokota		}
72343337Syokota	} else if (kbd->kb_type == KB_101) {
72443337Syokota		switch (keycode) {
72543337Syokota		case 0x5c:	/* print screen */
72643337Syokota			if (state->ks_flags & ALTS)
72743337Syokota				keycode = 0x54;	/* sysrq */
72843337Syokota			break;
72943337Syokota		case 0x68:	/* pause/break */
73043337Syokota			if (state->ks_flags & CTLS)
73143337Syokota				keycode = 0x6c;	/* break */
73243337Syokota			break;
73343337Syokota		}
73443337Syokota	}
73543337Syokota
73642421Syokota	/* return the key code in the K_CODE mode */
73742421Syokota	if (state->ks_mode == K_CODE)
73842421Syokota		return (keycode | (scancode & 0x80));
73942421Syokota
74042421Syokota	/* compose a character code */
74142421Syokota	if (state->ks_flags & COMPOSE) {
74247293Syokota		switch (keycode | (scancode & 0x80)) {
74342421Syokota		/* key pressed, process it */
74442421Syokota		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
74542421Syokota			state->ks_composed_char *= 10;
74646765Syokota			state->ks_composed_char += keycode - 0x40;
74742421Syokota			if (state->ks_composed_char > UCHAR_MAX)
74842421Syokota				return ERRKEY;
74942421Syokota			goto next_code;
75042421Syokota		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
75142421Syokota			state->ks_composed_char *= 10;
75246765Syokota			state->ks_composed_char += keycode - 0x47;
75342421Syokota			if (state->ks_composed_char > UCHAR_MAX)
75442421Syokota				return ERRKEY;
75542421Syokota			goto next_code;
75642421Syokota		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
75742421Syokota			state->ks_composed_char *= 10;
75846765Syokota			state->ks_composed_char += keycode - 0x4E;
75942421Syokota			if (state->ks_composed_char > UCHAR_MAX)
76042421Syokota				return ERRKEY;
76142421Syokota			goto next_code;
76242421Syokota		case 0x52:				/* keypad 0 */
76342421Syokota			state->ks_composed_char *= 10;
76442421Syokota			if (state->ks_composed_char > UCHAR_MAX)
76542421Syokota				return ERRKEY;
76642421Syokota			goto next_code;
76742421Syokota
76842421Syokota		/* key released, no interest here */
76942421Syokota		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
77042421Syokota		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
77142421Syokota		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
77242421Syokota		case 0xD2:				/* keypad 0 */
77342421Syokota			goto next_code;
77442421Syokota
77542421Syokota		case 0x38:				/* left alt key */
77642421Syokota			break;
77742421Syokota
77842421Syokota		default:
77942421Syokota			if (state->ks_composed_char > 0) {
78042421Syokota				state->ks_flags &= ~COMPOSE;
78142421Syokota				state->ks_composed_char = 0;
78242421Syokota				return ERRKEY;
78342421Syokota			}
78442421Syokota			break;
78542421Syokota		}
78642421Syokota	}
78742421Syokota
78842421Syokota	/* keycode to key action */
78942421Syokota	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
79042421Syokota				  &state->ks_state, &state->ks_accents);
79142421Syokota	if (action == NOKEY)
79242421Syokota		goto next_code;
79342421Syokota	else
79442421Syokota		return action;
79542421Syokota}
79642421Syokota
79742421Syokota/* check if char is waiting */
79842421Syokotastatic int
79942421Syokotaatkbd_check_char(keyboard_t *kbd)
80042421Syokota{
80142421Syokota	atkbd_state_t *state;
80242421Syokota
80342421Syokota	if (!KBD_IS_ACTIVE(kbd))
80442421Syokota		return FALSE;
80542421Syokota	state = (atkbd_state_t *)kbd->kb_data;
80642421Syokota	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
80742421Syokota		return TRUE;
80842421Syokota	return kbdc_data_ready(state->kbdc);
80942421Syokota}
81042421Syokota
81142421Syokota/* some useful control functions */
81242421Syokotastatic int
81342421Syokotaatkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
81442421Syokota{
81542421Syokota	/* trasnlate LED_XXX bits into the device specific bits */
81642421Syokota	static u_char ledmap[8] = {
81742421Syokota		0, 4, 2, 6, 1, 5, 3, 7,
81842421Syokota	};
81942421Syokota	atkbd_state_t *state = kbd->kb_data;
82042421Syokota	int error;
82142421Syokota	int s;
82242421Syokota	int i;
82342421Syokota
82442421Syokota	s = spltty();
82542421Syokota	switch (cmd) {
82642421Syokota
82742421Syokota	case KDGKBMODE:		/* get keyboard mode */
82842421Syokota		*(int *)arg = state->ks_mode;
82942421Syokota		break;
83042421Syokota	case KDSKBMODE:		/* set keyboard mode */
83142421Syokota		switch (*(int *)arg) {
83242421Syokota		case K_XLATE:
83342421Syokota			if (state->ks_mode != K_XLATE) {
83442421Syokota				/* make lock key state and LED state match */
83542421Syokota				state->ks_state &= ~LOCK_MASK;
83642421Syokota				state->ks_state |= KBD_LED_VAL(kbd);
83742421Syokota			}
83842421Syokota			/* FALL THROUGH */
83942421Syokota		case K_RAW:
84042421Syokota		case K_CODE:
84142421Syokota			if (state->ks_mode != *(int *)arg) {
84242421Syokota				atkbd_clear_state(kbd);
84342421Syokota				state->ks_mode = *(int *)arg;
84442421Syokota			}
84542421Syokota			break;
84642421Syokota		default:
84742421Syokota			splx(s);
84842421Syokota			return EINVAL;
84942421Syokota		}
85042421Syokota		break;
85142421Syokota
85242421Syokota	case KDGETLED:		/* get keyboard LED */
85342421Syokota		*(int *)arg = KBD_LED_VAL(kbd);
85442421Syokota		break;
85542421Syokota	case KDSETLED:		/* set keyboard LED */
85642421Syokota		/* NOTE: lock key state in ks_state won't be changed */
85742421Syokota		if (*(int *)arg & ~LOCK_MASK) {
85842421Syokota			splx(s);
85942421Syokota			return EINVAL;
86042421Syokota		}
86142421Syokota		i = *(int *)arg;
86242421Syokota		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
86342421Syokota		if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
86442421Syokota			if (i & ALKED)
86542421Syokota				i |= CLKED;
86642421Syokota			else
86742421Syokota				i &= ~CLKED;
86842421Syokota		}
86942421Syokota		if (KBD_HAS_DEVICE(kbd)) {
87042421Syokota			error = write_kbd(state->kbdc, KBDC_SET_LEDS,
87142421Syokota					  ledmap[i & LED_MASK]);
87242421Syokota			if (error) {
87342421Syokota				splx(s);
87442421Syokota				return error;
87542421Syokota			}
87642421Syokota		}
87742421Syokota		KBD_LED_VAL(kbd) = *(int *)arg;
87842421Syokota		break;
87942421Syokota
88042421Syokota	case KDGKBSTATE:	/* get lock key state */
88142421Syokota		*(int *)arg = state->ks_state & LOCK_MASK;
88242421Syokota		break;
88342421Syokota	case KDSKBSTATE:	/* set lock key state */
88442421Syokota		if (*(int *)arg & ~LOCK_MASK) {
88542421Syokota			splx(s);
88642421Syokota			return EINVAL;
88742421Syokota		}
88842421Syokota		state->ks_state &= ~LOCK_MASK;
88942421Syokota		state->ks_state |= *(int *)arg;
89042421Syokota		splx(s);
89142421Syokota		/* set LEDs and quit */
89242421Syokota		return atkbd_ioctl(kbd, KDSETLED, arg);
89342421Syokota
89444628Syokota	case KDSETREPEAT:	/* set keyboard repeat rate (new interface) */
89542421Syokota		splx(s);
89642421Syokota		if (!KBD_HAS_DEVICE(kbd))
89742421Syokota			return 0;
89844628Syokota		i = typematic(((int *)arg)[0], ((int *)arg)[1]);
89954543Syokota		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
90054543Syokota		if (error == 0) {
90154543Syokota			kbd->kb_delay1 = typematic_delay(i);
90254543Syokota			kbd->kb_delay2 = typematic_rate(i);
90354543Syokota		}
90454543Syokota		return error;
90542421Syokota
90644628Syokota	case KDSETRAD:		/* set keyboard repeat rate (old interface) */
90744628Syokota		splx(s);
90844628Syokota		if (!KBD_HAS_DEVICE(kbd))
90944628Syokota			return 0;
91054543Syokota		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
91154543Syokota		if (error == 0) {
91254543Syokota			kbd->kb_delay1 = typematic_delay(*(int *)arg);
91354543Syokota			kbd->kb_delay2 = typematic_rate(*(int *)arg);
91454543Syokota		}
91554543Syokota		return error;
91644628Syokota
91742421Syokota	case PIO_KEYMAP:	/* set keyboard translation table */
91842421Syokota	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
91942421Syokota	case PIO_DEADKEYMAP:	/* set accent key translation table */
92042421Syokota		state->ks_accents = 0;
92142421Syokota		/* FALL THROUGH */
92242421Syokota	default:
92342421Syokota		splx(s);
92442421Syokota		return genkbd_commonioctl(kbd, cmd, arg);
92542421Syokota	}
92642421Syokota
92742421Syokota	splx(s);
92842421Syokota	return 0;
92942421Syokota}
93042421Syokota
93142421Syokota/* lock the access to the keyboard */
93242421Syokotastatic int
93342421Syokotaatkbd_lock(keyboard_t *kbd, int lock)
93442421Syokota{
93542421Syokota	return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
93642421Syokota}
93742421Syokota
93842421Syokota/* clear the internal state of the keyboard */
93942421Syokotastatic void
94042421Syokotaatkbd_clear_state(keyboard_t *kbd)
94142421Syokota{
94242421Syokota	atkbd_state_t *state;
94342421Syokota
94442421Syokota	state = (atkbd_state_t *)kbd->kb_data;
94542421Syokota	state->ks_flags = 0;
94644628Syokota	state->ks_polling = 0;
94742421Syokota	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
94842421Syokota	state->ks_accents = 0;
94942421Syokota	state->ks_composed_char = 0;
95042421Syokota#if 0
95142421Syokota	state->ks_prefix = 0; /* XXX */
95242421Syokota#endif
95342421Syokota}
95442421Syokota
95542421Syokota/* save the internal state */
95642421Syokotastatic int
95742421Syokotaatkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
95842421Syokota{
95942421Syokota	if (len == 0)
96042421Syokota		return sizeof(atkbd_state_t);
96142421Syokota	if (len < sizeof(atkbd_state_t))
96242421Syokota		return -1;
96342421Syokota	bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
96442421Syokota	return 0;
96542421Syokota}
96642421Syokota
96742421Syokota/* set the internal state */
96842421Syokotastatic int
96942421Syokotaatkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
97042421Syokota{
97142421Syokota	if (len < sizeof(atkbd_state_t))
97242421Syokota		return ENOMEM;
97342421Syokota	if (((atkbd_state_t *)kbd->kb_data)->kbdc
97442421Syokota		!= ((atkbd_state_t *)buf)->kbdc)
97542421Syokota		return ENOMEM;
97642421Syokota	bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
97742421Syokota	return 0;
97842421Syokota}
97942421Syokota
98044628Syokotastatic int
98144628Syokotaatkbd_poll(keyboard_t *kbd, int on)
98244628Syokota{
98344628Syokota	atkbd_state_t *state;
98444628Syokota	int s;
98544628Syokota
98644628Syokota	state = (atkbd_state_t *)kbd->kb_data;
98744628Syokota	s = spltty();
98844628Syokota	if (on)
98944628Syokota		++state->ks_polling;
99044628Syokota	else
99144628Syokota		--state->ks_polling;
99244628Syokota	splx(s);
99344628Syokota	return 0;
99444628Syokota}
99544628Syokota
99642421Syokota/* local functions */
99742421Syokota
99842421Syokotastatic int
99955731Syokotaget_typematic(keyboard_t *kbd)
100055731Syokota{
100155731Syokota#ifdef __i386__
100255731Syokota	/*
100355731Syokota	 * Only some systems allow us to retrieve the keyboard repeat
100455731Syokota	 * rate previously set via the BIOS...
100555731Syokota	 */
100655731Syokota	struct vm86frame vmf;
100755731Syokota	u_int32_t p;
100855731Syokota
100955731Syokota	bzero(&vmf, sizeof(vmf));
101055731Syokota	vmf.vmf_ax = 0xc000;
101155731Syokota	vm86_intcall(0x15, &vmf);
101255731Syokota	if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
101355731Syokota		return ENODEV;
101455731Syokota        p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
101555731Syokota	if ((readb(p + 6) & 0x40) == 0)	/* int 16, function 0x09 supported? */
101655731Syokota		return ENODEV;
101755731Syokota	vmf.vmf_ax = 0x0900;
101855731Syokota	vm86_intcall(0x16, &vmf);
101955731Syokota	if ((vmf.vmf_al & 0x08) == 0)	/* int 16, function 0x0306 supported? */
102055731Syokota		return ENODEV;
102155731Syokota	vmf.vmf_ax = 0x0306;
102255731Syokota	vm86_intcall(0x16, &vmf);
102355731Syokota	kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
102455731Syokota	kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
102555731Syokota	return 0;
102655731Syokota#else
102755731Syokota	return ENODEV;
102855731Syokota#endif /* __i386__ */
102955731Syokota}
103055731Syokota
103155731Syokotastatic int
103242421Syokotasetup_kbd_port(KBDC kbdc, int port, int intr)
103342421Syokota{
103442421Syokota	if (!set_controller_command_byte(kbdc,
103542421Syokota		KBD_KBD_CONTROL_BITS,
103642421Syokota		((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
103742421Syokota		    | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
103842421Syokota		return 1;
103942421Syokota	return 0;
104042421Syokota}
104142421Syokota
104242421Syokotastatic int
104342421Syokotaget_kbd_echo(KBDC kbdc)
104442421Syokota{
104542421Syokota	/* enable the keyboard port, but disable the keyboard intr. */
104642421Syokota	if (setup_kbd_port(kbdc, TRUE, FALSE))
104742421Syokota		/* CONTROLLER ERROR: there is very little we can do... */
104842421Syokota		return ENXIO;
104942421Syokota
105042421Syokota	/* see if something is present */
105142421Syokota	write_kbd_command(kbdc, KBDC_ECHO);
105242421Syokota	if (read_kbd_data(kbdc) != KBD_ECHO) {
105342421Syokota		empty_both_buffers(kbdc, 10);
105442421Syokota		test_controller(kbdc);
105542421Syokota		test_kbd_port(kbdc);
105642421Syokota		return ENXIO;
105742421Syokota	}
105842421Syokota
105942421Syokota	/* enable the keyboard port and intr. */
106042421Syokota	if (setup_kbd_port(kbdc, TRUE, TRUE)) {
106142421Syokota		/*
106242421Syokota		 * CONTROLLER ERROR
106342421Syokota		 * This is serious; the keyboard intr is left disabled!
106442421Syokota		 */
106542421Syokota		return ENXIO;
106642421Syokota	}
106742421Syokota
106842421Syokota	return 0;
106942421Syokota}
107042421Syokota
107142421Syokotastatic int
107242421Syokotaprobe_keyboard(KBDC kbdc, int flags)
107342421Syokota{
107442421Syokota	/*
107542421Syokota	 * Don't try to print anything in this function.  The low-level
107642421Syokota	 * console may not have been initialized yet...
107742421Syokota	 */
107842421Syokota	int err;
107942421Syokota	int c;
108042421Syokota	int m;
108142421Syokota
108242421Syokota	if (!kbdc_lock(kbdc, TRUE)) {
108342421Syokota		/* driver error? */
108442421Syokota		return ENXIO;
108542421Syokota	}
108642421Syokota
108757131Syokota	/* temporarily block data transmission from the keyboard */
108857131Syokota	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
108957131Syokota
109042421Syokota	/* flush any noise in the buffer */
109157131Syokota	empty_both_buffers(kbdc, 100);
109242421Syokota
109342421Syokota	/* save the current keyboard controller command byte */
109442421Syokota	m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
109542421Syokota	c = get_controller_command_byte(kbdc);
109642421Syokota	if (c == -1) {
109742421Syokota		/* CONTROLLER ERROR */
109842421Syokota		kbdc_set_device_mask(kbdc, m);
109942421Syokota		kbdc_lock(kbdc, FALSE);
110042421Syokota		return ENXIO;
110142421Syokota	}
110242421Syokota
110342421Syokota	/*
110442421Syokota	 * The keyboard may have been screwed up by the boot block.
110542421Syokota	 * We may just be able to recover from error by testing the controller
110642421Syokota	 * and the keyboard port. The controller command byte needs to be
110742421Syokota	 * saved before this recovery operation, as some controllers seem
110842421Syokota	 * to set the command byte to particular values.
110942421Syokota	 */
111042421Syokota	test_controller(kbdc);
111142421Syokota	test_kbd_port(kbdc);
111242421Syokota
111342421Syokota	err = get_kbd_echo(kbdc);
111457902Syokota
111557902Syokota	/*
111657902Syokota	 * Even if the keyboard doesn't seem to be present (err != 0),
111757902Syokota	 * we shall enable the keyboard port and interrupt so that
111857902Syokota	 * the driver will be operable when the keyboard is attached
111957902Syokota	 * to the system later.  It is NOT recommended to hot-plug
112057902Syokota	 * the AT keyboard, but many people do so...
112157902Syokota	 */
112257902Syokota	kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
112357902Syokota	setup_kbd_port(kbdc, TRUE, TRUE);
112457902Syokota#if 0
112542421Syokota	if (err == 0) {
112642421Syokota		kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
112742421Syokota	} else {
112857131Syokota		/* try to restore the command byte as before */
112957131Syokota		set_controller_command_byte(kbdc, 0xff, c);
113042421Syokota		kbdc_set_device_mask(kbdc, m);
113142421Syokota	}
113257902Syokota#endif
113342421Syokota
113442421Syokota	kbdc_lock(kbdc, FALSE);
113542421Syokota	return err;
113642421Syokota}
113742421Syokota
113842421Syokotastatic int
113942421Syokotainit_keyboard(KBDC kbdc, int *type, int flags)
114042421Syokota{
114142421Syokota	int codeset;
114242421Syokota	int id;
114342421Syokota	int c;
114442421Syokota
114542421Syokota	if (!kbdc_lock(kbdc, TRUE)) {
114642421Syokota		/* driver error? */
114742421Syokota		return EIO;
114842421Syokota	}
114942421Syokota
115057131Syokota	/* temporarily block data transmission from the keyboard */
115157131Syokota	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
115257131Syokota
115342421Syokota	/* save the current controller command byte */
115454382Syokota	empty_both_buffers(kbdc, 200);
115542421Syokota	c = get_controller_command_byte(kbdc);
115642421Syokota	if (c == -1) {
115742421Syokota		/* CONTROLLER ERROR */
115842421Syokota		kbdc_lock(kbdc, FALSE);
115942421Syokota		printf("atkbd: unable to get the current command byte value.\n");
116042421Syokota		return EIO;
116142421Syokota	}
116242421Syokota	if (bootverbose)
116342421Syokota		printf("atkbd: the current kbd controller command byte %04x\n",
116442421Syokota		       c);
116542421Syokota#if 0
116642421Syokota	/* override the keyboard lock switch */
116742421Syokota	c |= KBD_OVERRIDE_KBD_LOCK;
116842421Syokota#endif
116942421Syokota
117042421Syokota	/* enable the keyboard port, but disable the keyboard intr. */
117142421Syokota	if (setup_kbd_port(kbdc, TRUE, FALSE)) {
117242421Syokota		/* CONTROLLER ERROR: there is very little we can do... */
117342421Syokota		printf("atkbd: unable to set the command byte.\n");
117442421Syokota		kbdc_lock(kbdc, FALSE);
117542421Syokota		return EIO;
117642421Syokota	}
117742421Syokota
117842421Syokota	/*
117942421Syokota	 * Check if we have an XT keyboard before we attempt to reset it.
118042421Syokota	 * The procedure assumes that the keyboard and the controller have
118142421Syokota	 * been set up properly by BIOS and have not been messed up
118242421Syokota	 * during the boot process.
118342421Syokota	 */
118442421Syokota	codeset = -1;
118542421Syokota	if (flags & KB_CONF_ALT_SCANCODESET)
118642421Syokota		/* the user says there is a XT keyboard */
118742421Syokota		codeset = 1;
118842421Syokota#ifdef KBD_DETECT_XT_KEYBOARD
118942421Syokota	else if ((c & KBD_TRANSLATION) == 0) {
119042421Syokota		/* SET_SCANCODE_SET is not always supported; ignore error */
119142421Syokota		if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
119242421Syokota			== KBD_ACK)
119342421Syokota			codeset = read_kbd_data(kbdc);
119442421Syokota	}
119542421Syokota	if (bootverbose)
119642421Syokota		printf("atkbd: scancode set %d\n", codeset);
119742421Syokota#endif /* KBD_DETECT_XT_KEYBOARD */
119842421Syokota
119942421Syokota	*type = KB_OTHER;
120042421Syokota	id = get_kbd_id(kbdc);
120142421Syokota	switch(id) {
120255730Syokota	case 0x41ab:	/* 101/102/... Enhanced */
120355730Syokota	case 0x83ab:	/* ditto */
120455730Syokota	case 0x54ab:	/* SpaceSaver */
120555730Syokota	case 0x84ab:	/* ditto */
120655730Syokota#if 0
120755730Syokota	case 0x90ab:	/* 'G' */
120855730Syokota	case 0x91ab:	/* 'P' */
120955730Syokota	case 0x92ab:	/* 'A' */
121055730Syokota#endif
121142421Syokota		*type = KB_101;
121242421Syokota		break;
121342421Syokota	case -1:	/* AT 84 keyboard doesn't return ID */
121442421Syokota		*type = KB_84;
121542421Syokota		break;
121642421Syokota	default:
121742421Syokota		break;
121842421Syokota	}
121942421Syokota	if (bootverbose)
122042421Syokota		printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
122142421Syokota
122242421Syokota	/* reset keyboard hardware */
122342421Syokota	if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
122442421Syokota		/*
122542421Syokota		 * KEYBOARD ERROR
122642421Syokota		 * Keyboard reset may fail either because the keyboard
122742421Syokota		 * doen't exist, or because the keyboard doesn't pass
122842421Syokota		 * the self-test, or the keyboard controller on the
122942421Syokota		 * motherboard and the keyboard somehow fail to shake hands.
123042421Syokota		 * It is just possible, particularly in the last case,
123142421Syokota		 * that the keyoard controller may be left in a hung state.
123242421Syokota		 * test_controller() and test_kbd_port() appear to bring
123342421Syokota		 * the keyboard controller back (I don't know why and how,
123442421Syokota		 * though.)
123542421Syokota		 */
123642421Syokota		empty_both_buffers(kbdc, 10);
123742421Syokota		test_controller(kbdc);
123842421Syokota		test_kbd_port(kbdc);
123942421Syokota		/*
124042421Syokota		 * We could disable the keyboard port and interrupt... but,
124142421Syokota		 * the keyboard may still exist (see above).
124242421Syokota		 */
124342421Syokota		set_controller_command_byte(kbdc, 0xff, c);
124442421Syokota		kbdc_lock(kbdc, FALSE);
124542421Syokota		if (bootverbose)
124642421Syokota			printf("atkbd: failed to reset the keyboard.\n");
124742421Syokota		return EIO;
124842421Syokota	}
124942421Syokota
125042421Syokota	/*
125142421Syokota	 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
125242421Syokota	 * such as those on the IBM ThinkPad laptop computers can be used
125342421Syokota	 * with the standard console driver.
125442421Syokota	 */
125542421Syokota	if (codeset == 1) {
125642421Syokota		if (send_kbd_command_and_data(kbdc,
125742421Syokota			KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
125842421Syokota			/* XT kbd doesn't need scan code translation */
125942421Syokota			c &= ~KBD_TRANSLATION;
126042421Syokota		} else {
126142421Syokota			/*
126242421Syokota			 * KEYBOARD ERROR
126342421Syokota			 * The XT kbd isn't usable unless the proper scan
126442421Syokota			 * code set is selected.
126542421Syokota			 */
126642421Syokota			set_controller_command_byte(kbdc, 0xff, c);
126742421Syokota			kbdc_lock(kbdc, FALSE);
126842421Syokota			printf("atkbd: unable to set the XT keyboard mode.\n");
126942421Syokota			return EIO;
127042421Syokota		}
127142421Syokota	}
127242421Syokota
127342831Syokota#ifdef __alpha__
127442831Syokota	if (send_kbd_command_and_data(
127542831Syokota		kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
127642831Syokota		printf("atkbd: can't set translation.\n");
127742831Syokota
127842831Syokota	}
127942831Syokota	c |= KBD_TRANSLATION;
128042831Syokota#endif
128142831Syokota
128242421Syokota	/* enable the keyboard port and intr. */
128342421Syokota	if (!set_controller_command_byte(kbdc,
128442421Syokota		KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
128542421Syokota		(c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
128642421Syokota		    | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
128742421Syokota		/*
128842421Syokota		 * CONTROLLER ERROR
128942421Syokota		 * This is serious; we are left with the disabled
129042421Syokota		 * keyboard intr.
129142421Syokota		 */
129242421Syokota		set_controller_command_byte(kbdc, 0xff, c);
129342421Syokota		kbdc_lock(kbdc, FALSE);
129442421Syokota		printf("atkbd: unable to enable the keyboard port and intr.\n");
129542421Syokota		return EIO;
129642421Syokota	}
129742421Syokota
129842421Syokota	kbdc_lock(kbdc, FALSE);
129942421Syokota	return 0;
130042421Syokota}
130142421Syokota
130242421Syokotastatic int
130342421Syokotawrite_kbd(KBDC kbdc, int command, int data)
130442421Syokota{
130542421Syokota    int s;
130642421Syokota
130742421Syokota    /* prevent the timeout routine from polling the keyboard */
130842421Syokota    if (!kbdc_lock(kbdc, TRUE))
130942421Syokota	return EBUSY;
131042421Syokota
131142421Syokota    /* disable the keyboard and mouse interrupt */
131242421Syokota    s = spltty();
131342421Syokota#if 0
131442421Syokota    c = get_controller_command_byte(kbdc);
131542421Syokota    if ((c == -1)
131642421Syokota	|| !set_controller_command_byte(kbdc,
131742421Syokota            kbdc_get_device_mask(kbdc),
131842421Syokota            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
131942421Syokota                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
132042421Syokota	/* CONTROLLER ERROR */
132142421Syokota        kbdc_lock(kbdc, FALSE);
132242421Syokota	splx(s);
132342421Syokota	return EIO;
132442421Syokota    }
132542421Syokota    /*
132642421Syokota     * Now that the keyboard controller is told not to generate
132742421Syokota     * the keyboard and mouse interrupts, call `splx()' to allow
132842421Syokota     * the other tty interrupts. The clock interrupt may also occur,
132942421Syokota     * but the timeout routine (`scrn_timer()') will be blocked
133042421Syokota     * by the lock flag set via `kbdc_lock()'
133142421Syokota     */
133242421Syokota    splx(s);
133342421Syokota#endif
133442421Syokota
133542421Syokota    if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
133642421Syokota        send_kbd_command(kbdc, KBDC_ENABLE_KBD);
133742421Syokota
133842421Syokota#if 0
133942421Syokota    /* restore the interrupts */
134042421Syokota    if (!set_controller_command_byte(kbdc,
134142421Syokota            kbdc_get_device_mask(kbdc),
134242421Syokota	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
134342421Syokota	/* CONTROLLER ERROR */
134442421Syokota    }
134542421Syokota#else
134642421Syokota    splx(s);
134742421Syokota#endif
134842421Syokota    kbdc_lock(kbdc, FALSE);
134942421Syokota
135042421Syokota    return 0;
135142421Syokota}
135242421Syokota
135342421Syokotastatic int
135442421Syokotaget_kbd_id(KBDC kbdc)
135542421Syokota{
135642421Syokota	int id1, id2;
135742421Syokota
135842421Syokota	empty_both_buffers(kbdc, 10);
135942421Syokota	id1 = id2 = -1;
136042421Syokota	if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
136142421Syokota		return -1;
136242421Syokota
136342421Syokota	DELAY(10000); 	/* 10 msec delay */
136442421Syokota	id1 = read_kbd_data(kbdc);
136542421Syokota	if (id1 != -1)
136642421Syokota		id2 = read_kbd_data(kbdc);
136742421Syokota
136842421Syokota	if ((id1 == -1) || (id2 == -1)) {
136942421Syokota		empty_both_buffers(kbdc, 10);
137042421Syokota		test_controller(kbdc);
137142421Syokota		test_kbd_port(kbdc);
137242421Syokota		return -1;
137342421Syokota	}
137442421Syokota	return ((id2 << 8) | id1);
137542421Syokota}
137642421Syokota
137754543Syokotastatic int delays[] = { 250, 500, 750, 1000 };
137854543Syokotastatic int rates[] = {  34,  38,  42,  46,  50,  55,  59,  63,
137954543Syokota			68,  76,  84,  92, 100, 110, 118, 126,
138054543Syokota		       136, 152, 168, 184, 200, 220, 236, 252,
138154543Syokota		       272, 304, 336, 368, 400, 440, 472, 504 };
138254543Syokota
138344628Syokotastatic int
138454543Syokotatypematic_delay(int i)
138554543Syokota{
138654543Syokota	return delays[(i >> 5) & 3];
138754543Syokota}
138854543Syokota
138954543Syokotastatic int
139054543Syokotatypematic_rate(int i)
139154543Syokota{
139254543Syokota	return rates[i & 0x1f];
139354543Syokota}
139454543Syokota
139554543Syokotastatic int
139644628Syokotatypematic(int delay, int rate)
139744628Syokota{
139844628Syokota	int value;
139944628Syokota	int i;
140044628Syokota
140144628Syokota	for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
140244628Syokota		if (delay >= delays[i])
140344628Syokota			break;
140444628Syokota	}
140544628Syokota	value = i << 5;
140644628Syokota	for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
140744628Syokota		if (rate >= rates[i])
140844628Syokota			break;
140944628Syokota	}
141044628Syokota	value |= i;
141144628Syokota	return value;
141244628Syokota}
1413