atkbd.c revision 119418
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 *
2642421Syokota */
2742421Syokota
28119418Sobrien#include <sys/cdefs.h>
29119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/atkbdc/atkbd.c 119418 2003-08-24 17:55:58Z obrien $");
30119418Sobrien
3142421Syokota#include "opt_kbd.h"
3244628Syokota#include "opt_atkbd.h"
3342421Syokota
3442421Syokota#include <sys/param.h>
3542421Syokota#include <sys/systm.h>
3642421Syokota#include <sys/kernel.h>
3747336Syokota#include <sys/bus.h>
3842421Syokota#include <sys/proc.h>
39114216Skan#include <sys/limits.h>
4042421Syokota#include <sys/malloc.h>
4142421Syokota
4258271Syokota#include <machine/bus.h>
4358271Syokota#include <machine/resource.h>
4458271Syokota
4555731Syokota#ifdef __i386__
4655731Syokota#include <machine/md_var.h>
4755731Syokota#include <machine/psl.h>
4855731Syokota#include <machine/vm86.h>
4955731Syokota#include <machine/pc/bios.h>
5055731Syokota
5155731Syokota#include <vm/vm.h>
5255731Syokota#include <vm/pmap.h>
5355731Syokota#endif /* __i386__ */
5455731Syokota
5580040Syokota#include <sys/kbio.h>
5642421Syokota#include <dev/kbd/kbdreg.h>
5742421Syokota#include <dev/kbd/atkbdreg.h>
5842421Syokota#include <dev/kbd/atkbdcreg.h>
5942421Syokota
6042831Syokota#include <isa/isareg.h>
6142831Syokota
6242421Syokotastatic timeout_t	atkbd_timeout;
6342421Syokota
6442421Syokotaint
6558271Syokotaatkbd_probe_unit(int unit, int ctlr, int irq, int flags)
6642421Syokota{
6742421Syokota	keyboard_switch_t *sw;
6842421Syokota	int args[2];
6944628Syokota	int error;
7042421Syokota
7142421Syokota	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
7242421Syokota	if (sw == NULL)
7342421Syokota		return ENXIO;
7442421Syokota
7558271Syokota	args[0] = ctlr;
7642421Syokota	args[1] = irq;
7744628Syokota	error = (*sw->probe)(unit, args, flags);
7844628Syokota	if (error)
7944628Syokota		return error;
8044628Syokota	return 0;
8142421Syokota}
8242421Syokota
8342421Syokotaint
8458271Syokotaatkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
8542421Syokota{
8642421Syokota	keyboard_switch_t *sw;
8744628Syokota	int args[2];
8842421Syokota	int error;
8942421Syokota
9042421Syokota	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
9142421Syokota	if (sw == NULL)
9242421Syokota		return ENXIO;
9342421Syokota
9442421Syokota	/* reset, initialize and enable the device */
9558271Syokota	args[0] = ctlr;
9644628Syokota	args[1] = irq;
9750154Syokota	*kbd = NULL;
9844628Syokota	error = (*sw->probe)(unit, args, flags);
9942421Syokota	if (error)
10044628Syokota		return error;
10150154Syokota	error = (*sw->init)(unit, kbd, args, flags);
10244628Syokota	if (error)
10344628Syokota		return error;
10450154Syokota	(*sw->enable)(*kbd);
10542421Syokota
10642421Syokota#ifdef KBD_INSTALL_CDEV
10742421Syokota	/* attach a virtual keyboard cdev */
10850154Syokota	error = kbd_attach(*kbd);
10942421Syokota	if (error)
11042421Syokota		return error;
11142421Syokota#endif
11242421Syokota
11342421Syokota	/*
11442421Syokota	 * This is a kludge to compensate for lost keyboard interrupts.
11542421Syokota	 * A similar code used to be in syscons. See below. XXX
11642421Syokota	 */
11750154Syokota	atkbd_timeout(*kbd);
11842421Syokota
11942421Syokota	if (bootverbose)
12050154Syokota		(*sw->diag)(*kbd, bootverbose);
12142421Syokota	return 0;
12242421Syokota}
12342421Syokota
12442421Syokotastatic void
12542421Syokotaatkbd_timeout(void *arg)
12642421Syokota{
12742421Syokota	keyboard_t *kbd;
12842421Syokota	int s;
12942421Syokota
13056334Syokota	/*
13156334Syokota	 * The original text of the following comments are extracted
13256334Syokota	 * from syscons.c (1.287)
13356334Syokota	 *
13442421Syokota	 * With release 2.1 of the Xaccel server, the keyboard is left
13542421Syokota	 * hanging pretty often. Apparently an interrupt from the
13642421Syokota	 * keyboard is lost, and I don't know why (yet).
13756334Syokota	 * This ugly hack calls the low-level interrupt routine if input
13856334Syokota	 * is ready for the keyboard and conveniently hides the problem. XXX
13956334Syokota	 *
14056334Syokota	 * Try removing anything stuck in the keyboard controller; whether
14156334Syokota	 * it's a keyboard scan code or mouse data. The low-level
14256334Syokota	 * interrupt routine doesn't read the mouse data directly,
14356334Syokota	 * but the keyboard controller driver will, as a side effect.
14442421Syokota	 */
14542421Syokota	/*
14656334Syokota	 * And here is bde's original comment about this:
14756334Syokota	 *
14856334Syokota	 * This is necessary to handle edge triggered interrupts - if we
14956334Syokota	 * returned when our IRQ is high due to unserviced input, then there
15056334Syokota	 * would be no more keyboard IRQs until the keyboard is reset by
15156334Syokota	 * external powers.
15256334Syokota	 *
15356334Syokota	 * The keyboard apparently unwedges the irq in most cases.
15442421Syokota	 */
15542421Syokota	s = spltty();
15642421Syokota	kbd = (keyboard_t *)arg;
15742421Syokota	if ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) {
15842421Syokota		/*
15942421Syokota		 * We have seen the lock flag is not set. Let's reset
16042421Syokota		 * the flag early, otherwise the LED update routine fails
16142421Syokota		 * which may want the lock during the interrupt routine.
16242421Syokota		 */
16342421Syokota		(*kbdsw[kbd->kb_index]->lock)(kbd, FALSE);
16442421Syokota		if ((*kbdsw[kbd->kb_index]->check_char)(kbd))
16544628Syokota			(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
16642421Syokota	}
16742421Syokota	splx(s);
16842421Syokota	timeout(atkbd_timeout, arg, hz/10);
16942421Syokota}
17042421Syokota
17142421Syokota/* LOW-LEVEL */
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
210114293Smarkmstatic keyboard_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.
26894228Sasmodai * NOTE: because of the way the low-level console 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 */
282117167Sjhb	if (resource_disabled("atkbd", ATKBD_DEFAULT)) {
28346764Syokota		i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
28446764Syokota		if (i >= 0) {
28546764Syokota			kbd = kbd_get_keyboard(i);
28646764Syokota			kbd_unregister(kbd);
28746764Syokota			kbd->kb_flags &= ~KB_REGISTERED;
28844628Syokota		}
28948878Syokota		return 0;
29044628Syokota	}
29145720Speter
29246764Syokota	/* XXX: a kludge to obtain the device configuration flags */
29346764Syokota	if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
29446764Syokota		flags |= i;
29546764Syokota
29642421Syokota	/* probe the default keyboard */
29742421Syokota	arg[0] = -1;
29842421Syokota	arg[1] = -1;
29944628Syokota	kbd = NULL;
30044628Syokota	if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
30142421Syokota		return 0;
30244628Syokota	if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
30344628Syokota		return 0;
30442421Syokota
30544628Syokota	/* return the number of found keyboards */
30644628Syokota	return 1;
30744628Syokota}
30844628Syokota
30944628Syokota/* low-level functions */
31044628Syokota
31144628Syokota/* detect a keyboard */
31244628Syokotastatic int
31344628Syokotaatkbd_probe(int unit, void *arg, int flags)
31444628Syokota{
31544628Syokota	KBDC kbdc;
31658271Syokota	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
31744628Syokota
31844628Syokota	/* XXX */
31944628Syokota	if (unit == ATKBD_DEFAULT) {
32044628Syokota		if (KBD_IS_PROBED(&default_kbd))
32142421Syokota			return 0;
32242421Syokota	}
32342421Syokota
32458271Syokota	kbdc = atkbdc_open(data[0]);
32544628Syokota	if (kbdc == NULL)
32644628Syokota		return ENXIO;
32744628Syokota	if (probe_keyboard(kbdc, flags)) {
32844628Syokota		if (flags & KB_CONF_FAIL_IF_NO_KBD)
32944628Syokota			return ENXIO;
33042421Syokota	}
33144628Syokota	return 0;
33242421Syokota}
33342421Syokota
33444628Syokota/* reset and initialize the device */
33542421Syokotastatic int
33644628Syokotaatkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
33742421Syokota{
33842421Syokota	keyboard_t *kbd;
33942421Syokota	atkbd_state_t *state;
34042421Syokota	keymap_t *keymap;
34142421Syokota	accentmap_t *accmap;
34242421Syokota	fkeytab_t *fkeymap;
34342421Syokota	int fkeymap_size;
34455731Syokota	int delay[2];
34558271Syokota	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
34642421Syokota
34742421Syokota	/* XXX */
34842421Syokota	if (unit == ATKBD_DEFAULT) {
34942421Syokota		*kbdp = kbd = &default_kbd;
35044628Syokota		if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
35142421Syokota			return 0;
35242421Syokota		state = &default_kbd_state;
35342421Syokota		keymap = &default_keymap;
35442421Syokota		accmap = &default_accentmap;
35542421Syokota		fkeymap = default_fkeytab;
35642421Syokota		fkeymap_size =
35742421Syokota			sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
35842421Syokota	} else if (*kbdp == NULL) {
35969781Sdwmalone		*kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
36069781Sdwmalone		state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
36142421Syokota		keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
36242421Syokota		accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
36342421Syokota		fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
36442421Syokota		fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
36569781Sdwmalone		if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
36669781Sdwmalone		     || (accmap == NULL) || (fkeymap == NULL)) {
36742421Syokota			if (state != NULL)
36842421Syokota				free(state, M_DEVBUF);
36942421Syokota			if (keymap != NULL)
37042421Syokota				free(keymap, M_DEVBUF);
37142421Syokota			if (accmap != NULL)
37242421Syokota				free(accmap, M_DEVBUF);
37342421Syokota			if (fkeymap != NULL)
37442421Syokota				free(fkeymap, M_DEVBUF);
37571394Sdwmalone			if (kbd != NULL)
37669781Sdwmalone				free(kbd, M_DEVBUF);
37742421Syokota			return ENOMEM;
37842421Syokota		}
37944628Syokota	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
38042421Syokota		return 0;
38142421Syokota	} else {
38242421Syokota		kbd = *kbdp;
38342421Syokota		state = (atkbd_state_t *)kbd->kb_data;
38442421Syokota		bzero(state, sizeof(*state));
38542421Syokota		keymap = kbd->kb_keymap;
38642421Syokota		accmap = kbd->kb_accentmap;
38742421Syokota		fkeymap = kbd->kb_fkeytab;
38842421Syokota		fkeymap_size = kbd->kb_fkeytab_size;
38942421Syokota	}
39042421Syokota
39144628Syokota	if (!KBD_IS_PROBED(kbd)) {
39258271Syokota		state->kbdc = atkbdc_open(data[0]);
39344628Syokota		if (state->kbdc == NULL)
39442421Syokota			return ENXIO;
39544628Syokota		kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
39658271Syokota				0, 0);
39744628Syokota		bcopy(&key_map, keymap, sizeof(key_map));
39844628Syokota		bcopy(&accent_map, accmap, sizeof(accent_map));
39944628Syokota		bcopy(fkey_tab, fkeymap,
40044628Syokota		      imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
40144628Syokota		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
40244628Syokota		kbd->kb_data = (void *)state;
40344628Syokota
40444628Syokota		if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
40544628Syokota			if (flags & KB_CONF_FAIL_IF_NO_KBD)
40644628Syokota				return ENXIO;
40744628Syokota		} else {
40844628Syokota			KBD_FOUND_DEVICE(kbd);
40944628Syokota		}
41044628Syokota		atkbd_clear_state(kbd);
41144628Syokota		state->ks_mode = K_XLATE;
41244628Syokota		/*
41344628Syokota		 * FIXME: set the initial value for lock keys in ks_state
41444628Syokota		 * according to the BIOS data?
41544628Syokota		 */
41644628Syokota		KBD_PROBE_DONE(kbd);
41742421Syokota	}
41844628Syokota	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
41949820Syokota		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
42042421Syokota		if (KBD_HAS_DEVICE(kbd)
42144628Syokota	    	    && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
422117513Ssimokawa	    	    && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
423117513Ssimokawa			kbd_unregister(kbd);
42442421Syokota			return ENXIO;
425117513Ssimokawa		}
42644628Syokota		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
42755731Syokota		get_typematic(kbd);
42855731Syokota		delay[0] = kbd->kb_delay1;
42955731Syokota		delay[1] = kbd->kb_delay2;
43055731Syokota		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
43142421Syokota		KBD_INIT_DONE(kbd);
43242421Syokota	}
43342421Syokota	if (!KBD_IS_CONFIGURED(kbd)) {
43442421Syokota		if (kbd_register(kbd) < 0)
43542421Syokota			return ENXIO;
43642421Syokota		KBD_CONFIG_DONE(kbd);
43742421Syokota	}
43842421Syokota
43942421Syokota	return 0;
44042421Syokota}
44142421Syokota
44242421Syokota/* finish using this keyboard */
44342421Syokotastatic int
44442421Syokotaatkbd_term(keyboard_t *kbd)
44542421Syokota{
44642421Syokota	kbd_unregister(kbd);
44742421Syokota	return 0;
44842421Syokota}
44942421Syokota
45042421Syokota/* keyboard interrupt routine */
45142421Syokotastatic int
45244628Syokotaatkbd_intr(keyboard_t *kbd, void *arg)
45342421Syokota{
45442421Syokota	atkbd_state_t *state;
45555731Syokota	int delay[2];
45642421Syokota	int c;
45742421Syokota
45842421Syokota	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
45942421Syokota		/* let the callback function to process the input */
46042421Syokota		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
46142421Syokota					    kbd->kb_callback.kc_arg);
46242421Syokota	} else {
46342421Syokota		/* read and discard the input; no one is waiting for input */
46442421Syokota		do {
46542421Syokota			c = atkbd_read_char(kbd, FALSE);
46642421Syokota		} while (c != NOKEY);
46742421Syokota
46842421Syokota		if (!KBD_HAS_DEVICE(kbd)) {
46942421Syokota			/*
47042421Syokota			 * The keyboard was not detected before;
47142421Syokota			 * it must have been reconnected!
47242421Syokota			 */
47342421Syokota			state = (atkbd_state_t *)kbd->kb_data;
47442421Syokota			init_keyboard(state->kbdc, &kbd->kb_type,
47542421Syokota				      kbd->kb_config);
47642421Syokota			atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
47755731Syokota			get_typematic(kbd);
47855731Syokota			delay[0] = kbd->kb_delay1;
47955731Syokota			delay[1] = kbd->kb_delay2;
48055731Syokota			atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
48142421Syokota			KBD_FOUND_DEVICE(kbd);
48242421Syokota		}
48342421Syokota	}
48442421Syokota	return 0;
48542421Syokota}
48642421Syokota
48742421Syokota/* test the interface to the device */
48842421Syokotastatic int
48942421Syokotaatkbd_test_if(keyboard_t *kbd)
49042421Syokota{
49142421Syokota	int error;
49242421Syokota	int s;
49342421Syokota
49442421Syokota	error = 0;
49542421Syokota	empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
49642421Syokota	s = spltty();
49742421Syokota	if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
49842421Syokota		error = EIO;
49942421Syokota	else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
50042421Syokota		error = EIO;
50142421Syokota	splx(s);
50242421Syokota
50342421Syokota	return error;
50442421Syokota}
50542421Syokota
50642421Syokota/*
50742421Syokota * Enable the access to the device; until this function is called,
50842421Syokota * the client cannot read from the keyboard.
50942421Syokota */
51042421Syokotastatic int
51142421Syokotaatkbd_enable(keyboard_t *kbd)
51242421Syokota{
51342421Syokota	int s;
51442421Syokota
51542421Syokota	s = spltty();
51642421Syokota	KBD_ACTIVATE(kbd);
51742421Syokota	splx(s);
51842421Syokota	return 0;
51942421Syokota}
52042421Syokota
52142421Syokota/* disallow the access to the device */
52242421Syokotastatic int
52342421Syokotaatkbd_disable(keyboard_t *kbd)
52442421Syokota{
52542421Syokota	int s;
52642421Syokota
52742421Syokota	s = spltty();
52842421Syokota	KBD_DEACTIVATE(kbd);
52942421Syokota	splx(s);
53042421Syokota	return 0;
53142421Syokota}
53242421Syokota
53342421Syokota/* read one byte from the keyboard if it's allowed */
53442421Syokotastatic int
53542421Syokotaatkbd_read(keyboard_t *kbd, int wait)
53642421Syokota{
53742421Syokota	int c;
53842421Syokota
53942421Syokota	if (wait)
54042421Syokota		c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
54142421Syokota	else
54242421Syokota		c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
54354382Syokota	if (c != -1)
54454382Syokota		++kbd->kb_count;
54542421Syokota	return (KBD_IS_ACTIVE(kbd) ? c : -1);
54642421Syokota}
54742421Syokota
54842421Syokota/* check if data is waiting */
54942421Syokotastatic int
55042421Syokotaatkbd_check(keyboard_t *kbd)
55142421Syokota{
55242421Syokota	if (!KBD_IS_ACTIVE(kbd))
55342421Syokota		return FALSE;
55442421Syokota	return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
55542421Syokota}
55642421Syokota
55742421Syokota/* read char from the keyboard */
55842421Syokotastatic u_int
55942421Syokotaatkbd_read_char(keyboard_t *kbd, int wait)
56042421Syokota{
56142421Syokota	atkbd_state_t *state;
56242421Syokota	u_int action;
56342421Syokota	int scancode;
56442421Syokota	int keycode;
56542421Syokota
56642421Syokota	state = (atkbd_state_t *)kbd->kb_data;
56742421Syokotanext_code:
56842421Syokota	/* do we have a composed char to return? */
56942421Syokota	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
57042421Syokota		action = state->ks_composed_char;
57142421Syokota		state->ks_composed_char = 0;
57242421Syokota		if (action > UCHAR_MAX)
57342421Syokota			return ERRKEY;
57442421Syokota		return action;
57542421Syokota	}
57642421Syokota
57742421Syokota	/* see if there is something in the keyboard port */
57842421Syokota	if (wait) {
57942421Syokota		do {
58042421Syokota			scancode = read_kbd_data(state->kbdc);
58142421Syokota		} while (scancode == -1);
58242421Syokota	} else {
58342421Syokota		scancode = read_kbd_data_no_wait(state->kbdc);
58442421Syokota		if (scancode == -1)
58542421Syokota			return NOKEY;
58642421Syokota	}
58754382Syokota	++kbd->kb_count;
58842421Syokota
58954382Syokota#if KBDIO_DEBUG >= 10
59054382Syokota	printf("atkbd_read_char(): scancode:0x%x\n", scancode);
59154382Syokota#endif
59254382Syokota
59342421Syokota	/* return the byte as is for the K_RAW mode */
59442421Syokota	if (state->ks_mode == K_RAW)
59542421Syokota		return scancode;
59642421Syokota
59742421Syokota	/* translate the scan code into a keycode */
59842421Syokota	keycode = scancode & 0x7F;
59942421Syokota	switch (state->ks_prefix) {
60042421Syokota	case 0x00:	/* normal scancode */
60142421Syokota		switch(scancode) {
60242421Syokota		case 0xB8:	/* left alt (compose key) released */
60342421Syokota			if (state->ks_flags & COMPOSE) {
60442421Syokota				state->ks_flags &= ~COMPOSE;
60542421Syokota				if (state->ks_composed_char > UCHAR_MAX)
60642421Syokota					state->ks_composed_char = 0;
60742421Syokota			}
60842421Syokota			break;
60942421Syokota		case 0x38:	/* left alt (compose key) pressed */
61042421Syokota			if (!(state->ks_flags & COMPOSE)) {
61142421Syokota				state->ks_flags |= COMPOSE;
61242421Syokota				state->ks_composed_char = 0;
61342421Syokota			}
61442421Syokota			break;
61542421Syokota		case 0xE0:
61642421Syokota		case 0xE1:
61742421Syokota			state->ks_prefix = scancode;
61842421Syokota			goto next_code;
61942421Syokota		}
62042421Syokota		break;
62142421Syokota	case 0xE0:      /* 0xE0 prefix */
62242421Syokota		state->ks_prefix = 0;
62342421Syokota		switch (keycode) {
62442421Syokota		case 0x1C:	/* right enter key */
62542421Syokota			keycode = 0x59;
62642421Syokota			break;
62742421Syokota		case 0x1D:	/* right ctrl key */
62842421Syokota			keycode = 0x5A;
62942421Syokota			break;
63042421Syokota		case 0x35:	/* keypad divide key */
63142421Syokota	    		keycode = 0x5B;
63242421Syokota	    		break;
63342421Syokota		case 0x37:	/* print scrn key */
63442421Syokota	    		keycode = 0x5C;
63542421Syokota	    		break;
63642421Syokota		case 0x38:	/* right alt key (alt gr) */
63742421Syokota	    		keycode = 0x5D;
63842421Syokota	    		break;
63943337Syokota		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
64043337Syokota			keycode = 0x68;
64143337Syokota	    		break;
64242421Syokota		case 0x47:	/* grey home key */
64342421Syokota	    		keycode = 0x5E;
64442421Syokota	    		break;
64542421Syokota		case 0x48:	/* grey up arrow key */
64642421Syokota	    		keycode = 0x5F;
64742421Syokota	    		break;
64842421Syokota		case 0x49:	/* grey page up key */
64942421Syokota	    		keycode = 0x60;
65042421Syokota	    		break;
65142421Syokota		case 0x4B:	/* grey left arrow key */
65242421Syokota	    		keycode = 0x61;
65342421Syokota	    		break;
65442421Syokota		case 0x4D:	/* grey right arrow key */
65542421Syokota	    		keycode = 0x62;
65642421Syokota	    		break;
65742421Syokota		case 0x4F:	/* grey end key */
65842421Syokota	    		keycode = 0x63;
65942421Syokota	    		break;
66042421Syokota		case 0x50:	/* grey down arrow key */
66142421Syokota	    		keycode = 0x64;
66242421Syokota	    		break;
66342421Syokota		case 0x51:	/* grey page down key */
66442421Syokota	    		keycode = 0x65;
66542421Syokota	    		break;
66642421Syokota		case 0x52:	/* grey insert key */
66742421Syokota	    		keycode = 0x66;
66842421Syokota	    		break;
66942421Syokota		case 0x53:	/* grey delete key */
67042421Syokota	    		keycode = 0x67;
67142421Syokota	    		break;
67242421Syokota		/* the following 3 are only used on the MS "Natural" keyboard */
67342421Syokota		case 0x5b:	/* left Window key */
67442421Syokota	    		keycode = 0x69;
67542421Syokota	    		break;
67642421Syokota		case 0x5c:	/* right Window key */
67742421Syokota	    		keycode = 0x6a;
67842421Syokota	    		break;
67942421Syokota		case 0x5d:	/* menu key */
68042421Syokota	    		keycode = 0x6b;
68142421Syokota	    		break;
68242421Syokota		default:	/* ignore everything else */
68342421Syokota	    		goto next_code;
68442421Syokota		}
68542421Syokota		break;
68642421Syokota    	case 0xE1:	/* 0xE1 prefix */
68743337Syokota		/*
68843337Syokota		 * The pause/break key on the 101 keyboard produces:
68943337Syokota		 * E1-1D-45 E1-9D-C5
69043337Syokota		 * Ctrl-pause/break produces:
69143337Syokota		 * E0-46 E0-C6 (See above.)
69243337Syokota		 */
69342421Syokota		state->ks_prefix = 0;
69442421Syokota		if (keycode == 0x1D)
69542421Syokota	    		state->ks_prefix = 0x1D;
69642421Syokota		goto next_code;
69742421Syokota		/* NOT REACHED */
69842421Syokota    	case 0x1D:	/* pause / break */
69942421Syokota		state->ks_prefix = 0;
70042421Syokota		if (keycode != 0x45)
70143337Syokota			goto next_code;
70242421Syokota		keycode = 0x68;
70342421Syokota		break;
70442421Syokota	}
70542421Syokota
70643337Syokota	if (kbd->kb_type == KB_84) {
70743337Syokota		switch (keycode) {
70843337Syokota		case 0x37:	/* *(numpad)/print screen */
70943337Syokota			if (state->ks_flags & SHIFTS)
71043337Syokota	    			keycode = 0x5c;	/* print screen */
71143337Syokota			break;
71243337Syokota		case 0x45:	/* num lock/pause */
71343337Syokota			if (state->ks_flags & CTLS)
71443337Syokota				keycode = 0x68;	/* pause */
71543337Syokota			break;
71643337Syokota		case 0x46:	/* scroll lock/break */
71743337Syokota			if (state->ks_flags & CTLS)
71843337Syokota				keycode = 0x6c;	/* break */
71943337Syokota			break;
72043337Syokota		}
72143337Syokota	} else if (kbd->kb_type == KB_101) {
72243337Syokota		switch (keycode) {
72343337Syokota		case 0x5c:	/* print screen */
72443337Syokota			if (state->ks_flags & ALTS)
72543337Syokota				keycode = 0x54;	/* sysrq */
72643337Syokota			break;
72743337Syokota		case 0x68:	/* pause/break */
72843337Syokota			if (state->ks_flags & CTLS)
72943337Syokota				keycode = 0x6c;	/* break */
73043337Syokota			break;
73143337Syokota		}
73243337Syokota	}
73343337Syokota
73442421Syokota	/* return the key code in the K_CODE mode */
73542421Syokota	if (state->ks_mode == K_CODE)
73642421Syokota		return (keycode | (scancode & 0x80));
73742421Syokota
73842421Syokota	/* compose a character code */
73942421Syokota	if (state->ks_flags & COMPOSE) {
74047293Syokota		switch (keycode | (scancode & 0x80)) {
74142421Syokota		/* key pressed, process it */
74242421Syokota		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
74342421Syokota			state->ks_composed_char *= 10;
74446765Syokota			state->ks_composed_char += keycode - 0x40;
74542421Syokota			if (state->ks_composed_char > UCHAR_MAX)
74642421Syokota				return ERRKEY;
74742421Syokota			goto next_code;
74842421Syokota		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
74942421Syokota			state->ks_composed_char *= 10;
75046765Syokota			state->ks_composed_char += keycode - 0x47;
75142421Syokota			if (state->ks_composed_char > UCHAR_MAX)
75242421Syokota				return ERRKEY;
75342421Syokota			goto next_code;
75442421Syokota		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
75542421Syokota			state->ks_composed_char *= 10;
75646765Syokota			state->ks_composed_char += keycode - 0x4E;
75742421Syokota			if (state->ks_composed_char > UCHAR_MAX)
75842421Syokota				return ERRKEY;
75942421Syokota			goto next_code;
76042421Syokota		case 0x52:				/* keypad 0 */
76142421Syokota			state->ks_composed_char *= 10;
76242421Syokota			if (state->ks_composed_char > UCHAR_MAX)
76342421Syokota				return ERRKEY;
76442421Syokota			goto next_code;
76542421Syokota
76642421Syokota		/* key released, no interest here */
76742421Syokota		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
76842421Syokota		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
76942421Syokota		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
77042421Syokota		case 0xD2:				/* keypad 0 */
77142421Syokota			goto next_code;
77242421Syokota
77342421Syokota		case 0x38:				/* left alt key */
77442421Syokota			break;
77542421Syokota
77642421Syokota		default:
77742421Syokota			if (state->ks_composed_char > 0) {
77842421Syokota				state->ks_flags &= ~COMPOSE;
77942421Syokota				state->ks_composed_char = 0;
78042421Syokota				return ERRKEY;
78142421Syokota			}
78242421Syokota			break;
78342421Syokota		}
78442421Syokota	}
78542421Syokota
78642421Syokota	/* keycode to key action */
78742421Syokota	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
78842421Syokota				  &state->ks_state, &state->ks_accents);
78942421Syokota	if (action == NOKEY)
79042421Syokota		goto next_code;
79142421Syokota	else
79242421Syokota		return action;
79342421Syokota}
79442421Syokota
79542421Syokota/* check if char is waiting */
79642421Syokotastatic int
79742421Syokotaatkbd_check_char(keyboard_t *kbd)
79842421Syokota{
79942421Syokota	atkbd_state_t *state;
80042421Syokota
80142421Syokota	if (!KBD_IS_ACTIVE(kbd))
80242421Syokota		return FALSE;
80342421Syokota	state = (atkbd_state_t *)kbd->kb_data;
80442421Syokota	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
80542421Syokota		return TRUE;
80642421Syokota	return kbdc_data_ready(state->kbdc);
80742421Syokota}
80842421Syokota
80942421Syokota/* some useful control functions */
81042421Syokotastatic int
81142421Syokotaatkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
81242421Syokota{
81342421Syokota	/* trasnlate LED_XXX bits into the device specific bits */
81442421Syokota	static u_char ledmap[8] = {
81542421Syokota		0, 4, 2, 6, 1, 5, 3, 7,
81642421Syokota	};
81742421Syokota	atkbd_state_t *state = kbd->kb_data;
81842421Syokota	int error;
81942421Syokota	int s;
82042421Syokota	int i;
82142421Syokota
82242421Syokota	s = spltty();
82342421Syokota	switch (cmd) {
82442421Syokota
82542421Syokota	case KDGKBMODE:		/* get keyboard mode */
82642421Syokota		*(int *)arg = state->ks_mode;
82742421Syokota		break;
82842421Syokota	case KDSKBMODE:		/* set keyboard mode */
82942421Syokota		switch (*(int *)arg) {
83042421Syokota		case K_XLATE:
83142421Syokota			if (state->ks_mode != K_XLATE) {
83242421Syokota				/* make lock key state and LED state match */
83342421Syokota				state->ks_state &= ~LOCK_MASK;
83442421Syokota				state->ks_state |= KBD_LED_VAL(kbd);
83542421Syokota			}
836102412Scharnier			/* FALLTHROUGH */
83742421Syokota		case K_RAW:
83842421Syokota		case K_CODE:
83942421Syokota			if (state->ks_mode != *(int *)arg) {
84042421Syokota				atkbd_clear_state(kbd);
84142421Syokota				state->ks_mode = *(int *)arg;
84242421Syokota			}
84342421Syokota			break;
84442421Syokota		default:
84542421Syokota			splx(s);
84642421Syokota			return EINVAL;
84742421Syokota		}
84842421Syokota		break;
84942421Syokota
85042421Syokota	case KDGETLED:		/* get keyboard LED */
85142421Syokota		*(int *)arg = KBD_LED_VAL(kbd);
85242421Syokota		break;
85342421Syokota	case KDSETLED:		/* set keyboard LED */
85442421Syokota		/* NOTE: lock key state in ks_state won't be changed */
85542421Syokota		if (*(int *)arg & ~LOCK_MASK) {
85642421Syokota			splx(s);
85742421Syokota			return EINVAL;
85842421Syokota		}
85942421Syokota		i = *(int *)arg;
86042421Syokota		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
86161004Sache		if (state->ks_mode == K_XLATE &&
86261004Sache		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
86342421Syokota			if (i & ALKED)
86442421Syokota				i |= CLKED;
86542421Syokota			else
86642421Syokota				i &= ~CLKED;
86742421Syokota		}
86842421Syokota		if (KBD_HAS_DEVICE(kbd)) {
86942421Syokota			error = write_kbd(state->kbdc, KBDC_SET_LEDS,
87042421Syokota					  ledmap[i & LED_MASK]);
87142421Syokota			if (error) {
87242421Syokota				splx(s);
87342421Syokota				return error;
87442421Syokota			}
87542421Syokota		}
87642421Syokota		KBD_LED_VAL(kbd) = *(int *)arg;
87742421Syokota		break;
87842421Syokota
87942421Syokota	case KDGKBSTATE:	/* get lock key state */
88042421Syokota		*(int *)arg = state->ks_state & LOCK_MASK;
88142421Syokota		break;
88242421Syokota	case KDSKBSTATE:	/* set lock key state */
88342421Syokota		if (*(int *)arg & ~LOCK_MASK) {
88442421Syokota			splx(s);
88542421Syokota			return EINVAL;
88642421Syokota		}
88742421Syokota		state->ks_state &= ~LOCK_MASK;
88842421Syokota		state->ks_state |= *(int *)arg;
88942421Syokota		splx(s);
89042421Syokota		/* set LEDs and quit */
89142421Syokota		return atkbd_ioctl(kbd, KDSETLED, arg);
89242421Syokota
89344628Syokota	case KDSETREPEAT:	/* set keyboard repeat rate (new interface) */
89442421Syokota		splx(s);
89542421Syokota		if (!KBD_HAS_DEVICE(kbd))
89642421Syokota			return 0;
89744628Syokota		i = typematic(((int *)arg)[0], ((int *)arg)[1]);
89854543Syokota		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
89954543Syokota		if (error == 0) {
90054543Syokota			kbd->kb_delay1 = typematic_delay(i);
90154543Syokota			kbd->kb_delay2 = typematic_rate(i);
90254543Syokota		}
90354543Syokota		return error;
90442421Syokota
90544628Syokota	case KDSETRAD:		/* set keyboard repeat rate (old interface) */
90644628Syokota		splx(s);
90744628Syokota		if (!KBD_HAS_DEVICE(kbd))
90844628Syokota			return 0;
90954543Syokota		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
91054543Syokota		if (error == 0) {
91154543Syokota			kbd->kb_delay1 = typematic_delay(*(int *)arg);
91254543Syokota			kbd->kb_delay2 = typematic_rate(*(int *)arg);
91354543Syokota		}
91454543Syokota		return error;
91544628Syokota
91642421Syokota	case PIO_KEYMAP:	/* set keyboard translation table */
91742421Syokota	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
91842421Syokota	case PIO_DEADKEYMAP:	/* set accent key translation table */
91942421Syokota		state->ks_accents = 0;
920102412Scharnier		/* FALLTHROUGH */
92142421Syokota	default:
92242421Syokota		splx(s);
92342421Syokota		return genkbd_commonioctl(kbd, cmd, arg);
92442421Syokota	}
92542421Syokota
92642421Syokota	splx(s);
92742421Syokota	return 0;
92842421Syokota}
92942421Syokota
93042421Syokota/* lock the access to the keyboard */
93142421Syokotastatic int
93242421Syokotaatkbd_lock(keyboard_t *kbd, int lock)
93342421Syokota{
93442421Syokota	return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
93542421Syokota}
93642421Syokota
93742421Syokota/* clear the internal state of the keyboard */
93842421Syokotastatic void
93942421Syokotaatkbd_clear_state(keyboard_t *kbd)
94042421Syokota{
94142421Syokota	atkbd_state_t *state;
94242421Syokota
94342421Syokota	state = (atkbd_state_t *)kbd->kb_data;
94442421Syokota	state->ks_flags = 0;
94544628Syokota	state->ks_polling = 0;
94642421Syokota	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
94742421Syokota	state->ks_accents = 0;
94842421Syokota	state->ks_composed_char = 0;
94942421Syokota#if 0
95042421Syokota	state->ks_prefix = 0; /* XXX */
95142421Syokota#endif
95242421Syokota}
95342421Syokota
95442421Syokota/* save the internal state */
95542421Syokotastatic int
95642421Syokotaatkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
95742421Syokota{
95842421Syokota	if (len == 0)
95942421Syokota		return sizeof(atkbd_state_t);
96042421Syokota	if (len < sizeof(atkbd_state_t))
96142421Syokota		return -1;
96242421Syokota	bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
96342421Syokota	return 0;
96442421Syokota}
96542421Syokota
96642421Syokota/* set the internal state */
96742421Syokotastatic int
96842421Syokotaatkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
96942421Syokota{
97042421Syokota	if (len < sizeof(atkbd_state_t))
97142421Syokota		return ENOMEM;
97242421Syokota	if (((atkbd_state_t *)kbd->kb_data)->kbdc
97342421Syokota		!= ((atkbd_state_t *)buf)->kbdc)
97442421Syokota		return ENOMEM;
97542421Syokota	bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
97642421Syokota	return 0;
97742421Syokota}
97842421Syokota
97944628Syokotastatic int
98044628Syokotaatkbd_poll(keyboard_t *kbd, int on)
98144628Syokota{
98244628Syokota	atkbd_state_t *state;
98344628Syokota	int s;
98444628Syokota
98544628Syokota	state = (atkbd_state_t *)kbd->kb_data;
98644628Syokota	s = spltty();
98744628Syokota	if (on)
98844628Syokota		++state->ks_polling;
98944628Syokota	else
99044628Syokota		--state->ks_polling;
99144628Syokota	splx(s);
99244628Syokota	return 0;
99344628Syokota}
99444628Syokota
99542421Syokota/* local functions */
99642421Syokota
99742421Syokotastatic int
99855731Syokotaget_typematic(keyboard_t *kbd)
99955731Syokota{
100055731Syokota#ifdef __i386__
100155731Syokota	/*
100255731Syokota	 * Only some systems allow us to retrieve the keyboard repeat
100355731Syokota	 * rate previously set via the BIOS...
100455731Syokota	 */
100555731Syokota	struct vm86frame vmf;
100655731Syokota	u_int32_t p;
100755731Syokota
100855731Syokota	bzero(&vmf, sizeof(vmf));
100955731Syokota	vmf.vmf_ax = 0xc000;
101055731Syokota	vm86_intcall(0x15, &vmf);
101155731Syokota	if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
101255731Syokota		return ENODEV;
101355731Syokota        p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
101455731Syokota	if ((readb(p + 6) & 0x40) == 0)	/* int 16, function 0x09 supported? */
101555731Syokota		return ENODEV;
101655731Syokota	vmf.vmf_ax = 0x0900;
101755731Syokota	vm86_intcall(0x16, &vmf);
101855731Syokota	if ((vmf.vmf_al & 0x08) == 0)	/* int 16, function 0x0306 supported? */
101955731Syokota		return ENODEV;
102055731Syokota	vmf.vmf_ax = 0x0306;
102155731Syokota	vm86_intcall(0x16, &vmf);
102255731Syokota	kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
102355731Syokota	kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
102455731Syokota	return 0;
102555731Syokota#else
102655731Syokota	return ENODEV;
102755731Syokota#endif /* __i386__ */
102855731Syokota}
102955731Syokota
103055731Syokotastatic int
103142421Syokotasetup_kbd_port(KBDC kbdc, int port, int intr)
103242421Syokota{
103342421Syokota	if (!set_controller_command_byte(kbdc,
103442421Syokota		KBD_KBD_CONTROL_BITS,
103542421Syokota		((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
103642421Syokota		    | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
103742421Syokota		return 1;
103842421Syokota	return 0;
103942421Syokota}
104042421Syokota
104142421Syokotastatic int
104242421Syokotaget_kbd_echo(KBDC kbdc)
104342421Syokota{
104442421Syokota	/* enable the keyboard port, but disable the keyboard intr. */
104542421Syokota	if (setup_kbd_port(kbdc, TRUE, FALSE))
104642421Syokota		/* CONTROLLER ERROR: there is very little we can do... */
104742421Syokota		return ENXIO;
104842421Syokota
104942421Syokota	/* see if something is present */
105042421Syokota	write_kbd_command(kbdc, KBDC_ECHO);
105142421Syokota	if (read_kbd_data(kbdc) != KBD_ECHO) {
105242421Syokota		empty_both_buffers(kbdc, 10);
105342421Syokota		test_controller(kbdc);
105442421Syokota		test_kbd_port(kbdc);
105542421Syokota		return ENXIO;
105642421Syokota	}
105742421Syokota
105842421Syokota	/* enable the keyboard port and intr. */
105942421Syokota	if (setup_kbd_port(kbdc, TRUE, TRUE)) {
106042421Syokota		/*
106142421Syokota		 * CONTROLLER ERROR
106242421Syokota		 * This is serious; the keyboard intr is left disabled!
106342421Syokota		 */
106442421Syokota		return ENXIO;
106542421Syokota	}
106642421Syokota
106742421Syokota	return 0;
106842421Syokota}
106942421Syokota
107042421Syokotastatic int
107142421Syokotaprobe_keyboard(KBDC kbdc, int flags)
107242421Syokota{
107342421Syokota	/*
107442421Syokota	 * Don't try to print anything in this function.  The low-level
107542421Syokota	 * console may not have been initialized yet...
107642421Syokota	 */
107742421Syokota	int err;
107842421Syokota	int c;
107942421Syokota	int m;
108042421Syokota
108142421Syokota	if (!kbdc_lock(kbdc, TRUE)) {
108242421Syokota		/* driver error? */
108342421Syokota		return ENXIO;
108442421Syokota	}
108542421Syokota
108657131Syokota	/* temporarily block data transmission from the keyboard */
108757131Syokota	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
108857131Syokota
108942421Syokota	/* flush any noise in the buffer */
109057131Syokota	empty_both_buffers(kbdc, 100);
109142421Syokota
109242421Syokota	/* save the current keyboard controller command byte */
109342421Syokota	m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
109442421Syokota	c = get_controller_command_byte(kbdc);
109542421Syokota	if (c == -1) {
109642421Syokota		/* CONTROLLER ERROR */
109742421Syokota		kbdc_set_device_mask(kbdc, m);
109842421Syokota		kbdc_lock(kbdc, FALSE);
109942421Syokota		return ENXIO;
110042421Syokota	}
110142421Syokota
110242421Syokota	/*
110342421Syokota	 * The keyboard may have been screwed up by the boot block.
110442421Syokota	 * We may just be able to recover from error by testing the controller
110542421Syokota	 * and the keyboard port. The controller command byte needs to be
110642421Syokota	 * saved before this recovery operation, as some controllers seem
110742421Syokota	 * to set the command byte to particular values.
110842421Syokota	 */
110942421Syokota	test_controller(kbdc);
111042421Syokota	test_kbd_port(kbdc);
111142421Syokota
111242421Syokota	err = get_kbd_echo(kbdc);
111357902Syokota
111457902Syokota	/*
111557902Syokota	 * Even if the keyboard doesn't seem to be present (err != 0),
111657902Syokota	 * we shall enable the keyboard port and interrupt so that
111757902Syokota	 * the driver will be operable when the keyboard is attached
111857902Syokota	 * to the system later.  It is NOT recommended to hot-plug
111957902Syokota	 * the AT keyboard, but many people do so...
112057902Syokota	 */
112157902Syokota	kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
112257902Syokota	setup_kbd_port(kbdc, TRUE, TRUE);
112357902Syokota#if 0
112442421Syokota	if (err == 0) {
112542421Syokota		kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
112642421Syokota	} else {
112757131Syokota		/* try to restore the command byte as before */
112857131Syokota		set_controller_command_byte(kbdc, 0xff, c);
112942421Syokota		kbdc_set_device_mask(kbdc, m);
113042421Syokota	}
113157902Syokota#endif
113242421Syokota
113342421Syokota	kbdc_lock(kbdc, FALSE);
113442421Syokota	return err;
113542421Syokota}
113642421Syokota
113742421Syokotastatic int
113842421Syokotainit_keyboard(KBDC kbdc, int *type, int flags)
113942421Syokota{
114042421Syokota	int codeset;
114142421Syokota	int id;
114242421Syokota	int c;
114342421Syokota
114442421Syokota	if (!kbdc_lock(kbdc, TRUE)) {
114542421Syokota		/* driver error? */
114642421Syokota		return EIO;
114742421Syokota	}
114842421Syokota
114957131Syokota	/* temporarily block data transmission from the keyboard */
115057131Syokota	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
115157131Syokota
115242421Syokota	/* save the current controller command byte */
115354382Syokota	empty_both_buffers(kbdc, 200);
115442421Syokota	c = get_controller_command_byte(kbdc);
115542421Syokota	if (c == -1) {
115642421Syokota		/* CONTROLLER ERROR */
115742421Syokota		kbdc_lock(kbdc, FALSE);
115842421Syokota		printf("atkbd: unable to get the current command byte value.\n");
115942421Syokota		return EIO;
116042421Syokota	}
116142421Syokota	if (bootverbose)
116242421Syokota		printf("atkbd: the current kbd controller command byte %04x\n",
116342421Syokota		       c);
116442421Syokota#if 0
116542421Syokota	/* override the keyboard lock switch */
116642421Syokota	c |= KBD_OVERRIDE_KBD_LOCK;
116742421Syokota#endif
116842421Syokota
116942421Syokota	/* enable the keyboard port, but disable the keyboard intr. */
117042421Syokota	if (setup_kbd_port(kbdc, TRUE, FALSE)) {
117142421Syokota		/* CONTROLLER ERROR: there is very little we can do... */
117242421Syokota		printf("atkbd: unable to set the command byte.\n");
117342421Syokota		kbdc_lock(kbdc, FALSE);
117442421Syokota		return EIO;
117542421Syokota	}
117642421Syokota
117742421Syokota	/*
117842421Syokota	 * Check if we have an XT keyboard before we attempt to reset it.
117942421Syokota	 * The procedure assumes that the keyboard and the controller have
118042421Syokota	 * been set up properly by BIOS and have not been messed up
118142421Syokota	 * during the boot process.
118242421Syokota	 */
118342421Syokota	codeset = -1;
118442421Syokota	if (flags & KB_CONF_ALT_SCANCODESET)
118542421Syokota		/* the user says there is a XT keyboard */
118642421Syokota		codeset = 1;
118742421Syokota#ifdef KBD_DETECT_XT_KEYBOARD
118842421Syokota	else if ((c & KBD_TRANSLATION) == 0) {
118942421Syokota		/* SET_SCANCODE_SET is not always supported; ignore error */
119042421Syokota		if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
119142421Syokota			== KBD_ACK)
119242421Syokota			codeset = read_kbd_data(kbdc);
119342421Syokota	}
119442421Syokota	if (bootverbose)
119542421Syokota		printf("atkbd: scancode set %d\n", codeset);
119642421Syokota#endif /* KBD_DETECT_XT_KEYBOARD */
119742421Syokota
119842421Syokota	*type = KB_OTHER;
119942421Syokota	id = get_kbd_id(kbdc);
120042421Syokota	switch(id) {
120155730Syokota	case 0x41ab:	/* 101/102/... Enhanced */
120255730Syokota	case 0x83ab:	/* ditto */
120355730Syokota	case 0x54ab:	/* SpaceSaver */
120455730Syokota	case 0x84ab:	/* ditto */
120555730Syokota#if 0
120655730Syokota	case 0x90ab:	/* 'G' */
120755730Syokota	case 0x91ab:	/* 'P' */
120855730Syokota	case 0x92ab:	/* 'A' */
120955730Syokota#endif
121042421Syokota		*type = KB_101;
121142421Syokota		break;
121242421Syokota	case -1:	/* AT 84 keyboard doesn't return ID */
121342421Syokota		*type = KB_84;
121442421Syokota		break;
121542421Syokota	default:
121642421Syokota		break;
121742421Syokota	}
121842421Syokota	if (bootverbose)
121942421Syokota		printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
122042421Syokota
122142421Syokota	/* reset keyboard hardware */
122242421Syokota	if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
122342421Syokota		/*
122442421Syokota		 * KEYBOARD ERROR
122542421Syokota		 * Keyboard reset may fail either because the keyboard
122642421Syokota		 * doen't exist, or because the keyboard doesn't pass
122742421Syokota		 * the self-test, or the keyboard controller on the
122842421Syokota		 * motherboard and the keyboard somehow fail to shake hands.
122942421Syokota		 * It is just possible, particularly in the last case,
1230110398Scharnier		 * that the keyboard controller may be left in a hung state.
123142421Syokota		 * test_controller() and test_kbd_port() appear to bring
123242421Syokota		 * the keyboard controller back (I don't know why and how,
123342421Syokota		 * though.)
123442421Syokota		 */
123542421Syokota		empty_both_buffers(kbdc, 10);
123642421Syokota		test_controller(kbdc);
123742421Syokota		test_kbd_port(kbdc);
123842421Syokota		/*
123942421Syokota		 * We could disable the keyboard port and interrupt... but,
124042421Syokota		 * the keyboard may still exist (see above).
124142421Syokota		 */
124242421Syokota		set_controller_command_byte(kbdc, 0xff, c);
124342421Syokota		kbdc_lock(kbdc, FALSE);
124442421Syokota		if (bootverbose)
124542421Syokota			printf("atkbd: failed to reset the keyboard.\n");
124642421Syokota		return EIO;
124742421Syokota	}
124842421Syokota
124942421Syokota	/*
125094275Sphk	 * Allow us to set the XT_KEYBD flag so that keyboards
125142421Syokota	 * such as those on the IBM ThinkPad laptop computers can be used
125242421Syokota	 * with the standard console driver.
125342421Syokota	 */
125442421Syokota	if (codeset == 1) {
125542421Syokota		if (send_kbd_command_and_data(kbdc,
125642421Syokota			KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
125742421Syokota			/* XT kbd doesn't need scan code translation */
125842421Syokota			c &= ~KBD_TRANSLATION;
125942421Syokota		} else {
126042421Syokota			/*
126142421Syokota			 * KEYBOARD ERROR
126242421Syokota			 * The XT kbd isn't usable unless the proper scan
126342421Syokota			 * code set is selected.
126442421Syokota			 */
126542421Syokota			set_controller_command_byte(kbdc, 0xff, c);
126642421Syokota			kbdc_lock(kbdc, FALSE);
126742421Syokota			printf("atkbd: unable to set the XT keyboard mode.\n");
126842421Syokota			return EIO;
126942421Syokota		}
127042421Syokota	}
127142421Syokota
127242831Syokota#ifdef __alpha__
127342831Syokota	if (send_kbd_command_and_data(
127442831Syokota		kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
127542831Syokota		printf("atkbd: can't set translation.\n");
127642831Syokota
127742831Syokota	}
127842831Syokota	c |= KBD_TRANSLATION;
127942831Syokota#endif
128042831Syokota
128142421Syokota	/* enable the keyboard port and intr. */
128242421Syokota	if (!set_controller_command_byte(kbdc,
128342421Syokota		KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
128442421Syokota		(c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
128542421Syokota		    | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
128642421Syokota		/*
128742421Syokota		 * CONTROLLER ERROR
128842421Syokota		 * This is serious; we are left with the disabled
128942421Syokota		 * keyboard intr.
129042421Syokota		 */
129142421Syokota		set_controller_command_byte(kbdc, 0xff, c);
129242421Syokota		kbdc_lock(kbdc, FALSE);
129342421Syokota		printf("atkbd: unable to enable the keyboard port and intr.\n");
129442421Syokota		return EIO;
129542421Syokota	}
129642421Syokota
129742421Syokota	kbdc_lock(kbdc, FALSE);
129842421Syokota	return 0;
129942421Syokota}
130042421Syokota
130142421Syokotastatic int
130242421Syokotawrite_kbd(KBDC kbdc, int command, int data)
130342421Syokota{
130442421Syokota    int s;
130542421Syokota
130642421Syokota    /* prevent the timeout routine from polling the keyboard */
130742421Syokota    if (!kbdc_lock(kbdc, TRUE))
130842421Syokota	return EBUSY;
130942421Syokota
131042421Syokota    /* disable the keyboard and mouse interrupt */
131142421Syokota    s = spltty();
131242421Syokota#if 0
131342421Syokota    c = get_controller_command_byte(kbdc);
131442421Syokota    if ((c == -1)
131542421Syokota	|| !set_controller_command_byte(kbdc,
131642421Syokota            kbdc_get_device_mask(kbdc),
131742421Syokota            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
131842421Syokota                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
131942421Syokota	/* CONTROLLER ERROR */
132042421Syokota        kbdc_lock(kbdc, FALSE);
132142421Syokota	splx(s);
132242421Syokota	return EIO;
132342421Syokota    }
132442421Syokota    /*
132542421Syokota     * Now that the keyboard controller is told not to generate
132642421Syokota     * the keyboard and mouse interrupts, call `splx()' to allow
132742421Syokota     * the other tty interrupts. The clock interrupt may also occur,
132842421Syokota     * but the timeout routine (`scrn_timer()') will be blocked
132942421Syokota     * by the lock flag set via `kbdc_lock()'
133042421Syokota     */
133142421Syokota    splx(s);
133242421Syokota#endif
133342421Syokota
133442421Syokota    if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
133542421Syokota        send_kbd_command(kbdc, KBDC_ENABLE_KBD);
133642421Syokota
133742421Syokota#if 0
133842421Syokota    /* restore the interrupts */
133942421Syokota    if (!set_controller_command_byte(kbdc,
134042421Syokota            kbdc_get_device_mask(kbdc),
134142421Syokota	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
134242421Syokota	/* CONTROLLER ERROR */
134342421Syokota    }
134442421Syokota#else
134542421Syokota    splx(s);
134642421Syokota#endif
134742421Syokota    kbdc_lock(kbdc, FALSE);
134842421Syokota
134942421Syokota    return 0;
135042421Syokota}
135142421Syokota
135242421Syokotastatic int
135342421Syokotaget_kbd_id(KBDC kbdc)
135442421Syokota{
135542421Syokota	int id1, id2;
135642421Syokota
135742421Syokota	empty_both_buffers(kbdc, 10);
135842421Syokota	id1 = id2 = -1;
135942421Syokota	if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
136042421Syokota		return -1;
136142421Syokota
136242421Syokota	DELAY(10000); 	/* 10 msec delay */
136342421Syokota	id1 = read_kbd_data(kbdc);
136442421Syokota	if (id1 != -1)
136542421Syokota		id2 = read_kbd_data(kbdc);
136642421Syokota
136742421Syokota	if ((id1 == -1) || (id2 == -1)) {
136842421Syokota		empty_both_buffers(kbdc, 10);
136942421Syokota		test_controller(kbdc);
137042421Syokota		test_kbd_port(kbdc);
137142421Syokota		return -1;
137242421Syokota	}
137342421Syokota	return ((id2 << 8) | id1);
137442421Syokota}
137542421Syokota
137654543Syokotastatic int delays[] = { 250, 500, 750, 1000 };
137754543Syokotastatic int rates[] = {  34,  38,  42,  46,  50,  55,  59,  63,
137854543Syokota			68,  76,  84,  92, 100, 110, 118, 126,
137954543Syokota		       136, 152, 168, 184, 200, 220, 236, 252,
138054543Syokota		       272, 304, 336, 368, 400, 440, 472, 504 };
138154543Syokota
138244628Syokotastatic int
138354543Syokotatypematic_delay(int i)
138454543Syokota{
138554543Syokota	return delays[(i >> 5) & 3];
138654543Syokota}
138754543Syokota
138854543Syokotastatic int
138954543Syokotatypematic_rate(int i)
139054543Syokota{
139154543Syokota	return rates[i & 0x1f];
139254543Syokota}
139354543Syokota
139454543Syokotastatic int
139544628Syokotatypematic(int delay, int rate)
139644628Syokota{
139744628Syokota	int value;
139844628Syokota	int i;
139944628Syokota
140044628Syokota	for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
140144628Syokota		if (delay >= delays[i])
140244628Syokota			break;
140344628Syokota	}
140444628Syokota	value = i << 5;
140544628Syokota	for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
140644628Syokota		if (rate >= rates[i])
140744628Syokota			break;
140844628Syokota	}
140944628Syokota	value |= i;
141044628Syokota	return value;
141144628Syokota}
1412