atkbd.c revision 55731
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 55731 2000-01-10 08:52:32Z yokota $
2742421Syokota */
2842421Syokota
2942421Syokota#include "atkbd.h"
3042421Syokota#include "opt_kbd.h"
3144628Syokota#include "opt_atkbd.h"
3242421Syokota
3342421Syokota#if NATKBD > 0
3442421Syokota
3542421Syokota#include <sys/param.h>
3642421Syokota#include <sys/systm.h>
3742421Syokota#include <sys/kernel.h>
3847336Syokota#include <sys/bus.h>
3942421Syokota#include <sys/proc.h>
4042421Syokota#include <sys/malloc.h>
4142421Syokota
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
6144628Syokotaatkbd_probe_unit(int unit, int port, 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
7142421Syokota	args[0] = port;
7242421Syokota	args[1] = irq;
7344628Syokota	error = (*sw->probe)(unit, args, flags);
7444628Syokota	if (error)
7544628Syokota		return error;
7644628Syokota	return 0;
7742421Syokota}
7842421Syokota
7942421Syokotaint
8050154Syokotaatkbd_attach_unit(int unit, keyboard_t **kbd, int port, 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 */
9144628Syokota	args[0] = port;
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
12642421Syokota	/* The following comments are extracted from syscons.c (1.287) */
12742421Syokota	/*
12842421Syokota	 * With release 2.1 of the Xaccel server, the keyboard is left
12942421Syokota	 * hanging pretty often. Apparently an interrupt from the
13042421Syokota	 * keyboard is lost, and I don't know why (yet).
13142421Syokota	 * This ugly hack calls scintr if input is ready for the keyboard
13242421Syokota	 * and conveniently hides the problem.			XXX
13342421Syokota	 */
13442421Syokota	/*
13542421Syokota	 * Try removing anything stuck in the keyboard controller; whether
13642421Syokota	 * it's a keyboard scan code or mouse data. `scintr()' doesn't
13742421Syokota	 * read the mouse data directly, but `kbdio' routines will, as a
13842421Syokota	 * side effect.
13942421Syokota	 */
14042421Syokota	s = spltty();
14142421Syokota	kbd = (keyboard_t *)arg;
14242421Syokota	if ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) {
14342421Syokota		/*
14442421Syokota		 * We have seen the lock flag is not set. Let's reset
14542421Syokota		 * the flag early, otherwise the LED update routine fails
14642421Syokota		 * which may want the lock during the interrupt routine.
14742421Syokota		 */
14842421Syokota		(*kbdsw[kbd->kb_index]->lock)(kbd, FALSE);
14942421Syokota		if ((*kbdsw[kbd->kb_index]->check_char)(kbd))
15044628Syokota			(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
15142421Syokota	}
15242421Syokota	splx(s);
15342421Syokota	timeout(atkbd_timeout, arg, hz/10);
15442421Syokota}
15542421Syokota
15642421Syokota/* cdev driver functions */
15742421Syokota
15842421Syokota
15942421Syokota/* LOW-LEVEL */
16042421Syokota
16142421Syokota#include <machine/limits.h>
16242421Syokota#include <machine/console.h>
16342421Syokota#include <machine/clock.h>
16442421Syokota
16542421Syokota#define ATKBD_DEFAULT	0
16642421Syokota
16742421Syokotatypedef struct atkbd_state {
16842421Syokota	KBDC		kbdc;		/* keyboard controller */
16942421Syokota					/* XXX: don't move this field; pcvt
17042421Syokota					 * expects `kbdc' to be the first
17142421Syokota					 * field in this structure. */
17242421Syokota	int		ks_mode;	/* input mode (K_XLATE,K_RAW,K_CODE) */
17342421Syokota	int		ks_flags;	/* flags */
17442421Syokota#define COMPOSE		(1 << 0)
17544628Syokota	int		ks_polling;
17642421Syokota	int		ks_state;	/* shift/lock key state */
17742421Syokota	int		ks_accents;	/* accent key index (> 0) */
17842421Syokota	u_int		ks_composed_char; /* composed char code (> 0) */
17942421Syokota	u_char		ks_prefix;	/* AT scan code prefix */
18042421Syokota} atkbd_state_t;
18142421Syokota
18242421Syokota/* keyboard driver declaration */
18342421Syokotastatic int		atkbd_configure(int flags);
18442421Syokotastatic kbd_probe_t	atkbd_probe;
18542421Syokotastatic kbd_init_t	atkbd_init;
18642421Syokotastatic kbd_term_t	atkbd_term;
18742421Syokotastatic kbd_intr_t	atkbd_intr;
18842421Syokotastatic kbd_test_if_t	atkbd_test_if;
18942421Syokotastatic kbd_enable_t	atkbd_enable;
19042421Syokotastatic kbd_disable_t	atkbd_disable;
19142421Syokotastatic kbd_read_t	atkbd_read;
19242421Syokotastatic kbd_check_t	atkbd_check;
19342421Syokotastatic kbd_read_char_t	atkbd_read_char;
19442421Syokotastatic kbd_check_char_t	atkbd_check_char;
19542421Syokotastatic kbd_ioctl_t	atkbd_ioctl;
19642421Syokotastatic kbd_lock_t	atkbd_lock;
19742421Syokotastatic kbd_clear_state_t atkbd_clear_state;
19842421Syokotastatic kbd_get_state_t	atkbd_get_state;
19942421Syokotastatic kbd_set_state_t	atkbd_set_state;
20044628Syokotastatic kbd_poll_mode_t	atkbd_poll;
20142421Syokota
20242421Syokotakeyboard_switch_t atkbdsw = {
20342421Syokota	atkbd_probe,
20442421Syokota	atkbd_init,
20542421Syokota	atkbd_term,
20642421Syokota	atkbd_intr,
20742421Syokota	atkbd_test_if,
20842421Syokota	atkbd_enable,
20942421Syokota	atkbd_disable,
21042421Syokota	atkbd_read,
21142421Syokota	atkbd_check,
21242421Syokota	atkbd_read_char,
21342421Syokota	atkbd_check_char,
21442421Syokota	atkbd_ioctl,
21542421Syokota	atkbd_lock,
21642421Syokota	atkbd_clear_state,
21742421Syokota	atkbd_get_state,
21842421Syokota	atkbd_set_state,
21942421Syokota	genkbd_get_fkeystr,
22044628Syokota	atkbd_poll,
22142421Syokota	genkbd_diag,
22242421Syokota};
22342421Syokota
22442421SyokotaKEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
22542421Syokota
22642421Syokota/* local functions */
22755731Syokotastatic int		get_typematic(keyboard_t *kbd);
22842421Syokotastatic int		setup_kbd_port(KBDC kbdc, int port, int intr);
22942421Syokotastatic int		get_kbd_echo(KBDC kbdc);
23042421Syokotastatic int		probe_keyboard(KBDC kbdc, int flags);
23142421Syokotastatic int		init_keyboard(KBDC kbdc, int *type, int flags);
23242421Syokotastatic int		write_kbd(KBDC kbdc, int command, int data);
23342421Syokotastatic int		get_kbd_id(KBDC kbdc);
23444628Syokotastatic int		typematic(int delay, int rate);
23554543Syokotastatic int		typematic_delay(int delay);
23654543Syokotastatic int		typematic_rate(int rate);
23742421Syokota
23842421Syokota/* local variables */
23942421Syokota
24042421Syokota/* the initial key map, accent map and fkey strings */
24144628Syokota#ifdef ATKBD_DFLT_KEYMAP
24244628Syokota#define KBD_DFLT_KEYMAP
24344628Syokota#include "atkbdmap.h"
24444628Syokota#endif
24542831Syokota#include <dev/kbd/kbdtables.h>
24642421Syokota
24742421Syokota/* structures for the default keyboard */
24842421Syokotastatic keyboard_t	default_kbd;
24942421Syokotastatic atkbd_state_t	default_kbd_state;
25042421Syokotastatic keymap_t		default_keymap;
25142421Syokotastatic accentmap_t	default_accentmap;
25242421Syokotastatic fkeytab_t	default_fkeytab[NUM_FKEYS];
25342421Syokota
25442421Syokota/*
25542421Syokota * The back door to the keyboard driver!
25642421Syokota * This function is called by the console driver, via the kbdio module,
25742421Syokota * to tickle keyboard drivers when the low-level console is being initialized.
25842421Syokota * Almost nothing in the kernel has been initialied yet.  Try to probe
25942421Syokota * keyboards if possible.
26042421Syokota * NOTE: because of the way the low-level conole is initialized, this routine
26142421Syokota * may be called more than once!!
26242421Syokota */
26342421Syokotastatic int
26442421Syokotaatkbd_configure(int flags)
26542421Syokota{
26642421Syokota	keyboard_t *kbd;
26742421Syokota	int arg[2];
26844628Syokota	int i;
26942421Syokota
27048878Syokota	/* probe the keyboard controller */
27148878Syokota	atkbdc_configure();
27248878Syokota
27346764Syokota	/* if the driver is disabled, unregister the keyboard if any */
27446764Syokota	if ((resource_int_value("atkbd", ATKBD_DEFAULT, "disabled", &i) == 0)
27546764Syokota	    && i != 0) {
27646764Syokota		i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
27746764Syokota		if (i >= 0) {
27846764Syokota			kbd = kbd_get_keyboard(i);
27946764Syokota			kbd_unregister(kbd);
28046764Syokota			kbd->kb_flags &= ~KB_REGISTERED;
28144628Syokota		}
28248878Syokota		return 0;
28344628Syokota	}
28445720Speter
28546764Syokota	/* XXX: a kludge to obtain the device configuration flags */
28646764Syokota	if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
28746764Syokota		flags |= i;
28846764Syokota
28942421Syokota	/* probe the default keyboard */
29042421Syokota	arg[0] = -1;
29142421Syokota	arg[1] = -1;
29244628Syokota	kbd = NULL;
29344628Syokota	if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
29442421Syokota		return 0;
29544628Syokota	if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
29644628Syokota		return 0;
29742421Syokota
29844628Syokota	/* return the number of found keyboards */
29944628Syokota	return 1;
30044628Syokota}
30144628Syokota
30244628Syokota/* low-level functions */
30344628Syokota
30444628Syokota/* detect a keyboard */
30544628Syokotastatic int
30644628Syokotaatkbd_probe(int unit, void *arg, int flags)
30744628Syokota{
30844628Syokota	KBDC kbdc;
30944628Syokota	int *data = (int *)arg;
31044628Syokota
31144628Syokota	/* XXX */
31244628Syokota	if (unit == ATKBD_DEFAULT) {
31344628Syokota		if (KBD_IS_PROBED(&default_kbd))
31442421Syokota			return 0;
31542421Syokota	}
31642421Syokota
31744628Syokota	kbdc = kbdc_open(data[0]);
31844628Syokota	if (kbdc == NULL)
31944628Syokota		return ENXIO;
32044628Syokota	if (probe_keyboard(kbdc, flags)) {
32144628Syokota		if (flags & KB_CONF_FAIL_IF_NO_KBD)
32244628Syokota			return ENXIO;
32342421Syokota	}
32444628Syokota	return 0;
32542421Syokota}
32642421Syokota
32744628Syokota/* reset and initialize the device */
32842421Syokotastatic int
32944628Syokotaatkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
33042421Syokota{
33142421Syokota	keyboard_t *kbd;
33242421Syokota	atkbd_state_t *state;
33342421Syokota	keymap_t *keymap;
33442421Syokota	accentmap_t *accmap;
33542421Syokota	fkeytab_t *fkeymap;
33642421Syokota	int fkeymap_size;
33755731Syokota	int delay[2];
33842421Syokota	int *data = (int *)arg;
33942421Syokota
34042421Syokota	/* XXX */
34142421Syokota	if (unit == ATKBD_DEFAULT) {
34242421Syokota		*kbdp = kbd = &default_kbd;
34344628Syokota		if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
34442421Syokota			return 0;
34542421Syokota		state = &default_kbd_state;
34642421Syokota		keymap = &default_keymap;
34742421Syokota		accmap = &default_accentmap;
34842421Syokota		fkeymap = default_fkeytab;
34942421Syokota		fkeymap_size =
35042421Syokota			sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
35142421Syokota	} else if (*kbdp == NULL) {
35242421Syokota		*kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT);
35342421Syokota		if (kbd == NULL)
35442421Syokota			return ENOMEM;
35542421Syokota		bzero(kbd, sizeof(*kbd));
35642421Syokota		state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT);
35742421Syokota		keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
35842421Syokota		accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
35942421Syokota		fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
36042421Syokota		fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
36142421Syokota		if ((state == NULL) || (keymap == NULL) || (accmap == NULL)
36242421Syokota		     || (fkeymap == NULL)) {
36342421Syokota			if (state != NULL)
36442421Syokota				free(state, M_DEVBUF);
36542421Syokota			if (keymap != NULL)
36642421Syokota				free(keymap, M_DEVBUF);
36742421Syokota			if (accmap != NULL)
36842421Syokota				free(accmap, M_DEVBUF);
36942421Syokota			if (fkeymap != NULL)
37042421Syokota				free(fkeymap, M_DEVBUF);
37142421Syokota			free(kbd, M_DEVBUF);
37242421Syokota			return ENOMEM;
37342421Syokota		}
37442421Syokota		bzero(state, sizeof(*state));
37544628Syokota	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
37642421Syokota		return 0;
37742421Syokota	} else {
37842421Syokota		kbd = *kbdp;
37942421Syokota		state = (atkbd_state_t *)kbd->kb_data;
38042421Syokota		bzero(state, sizeof(*state));
38142421Syokota		keymap = kbd->kb_keymap;
38242421Syokota		accmap = kbd->kb_accentmap;
38342421Syokota		fkeymap = kbd->kb_fkeytab;
38442421Syokota		fkeymap_size = kbd->kb_fkeytab_size;
38542421Syokota	}
38642421Syokota
38744628Syokota	if (!KBD_IS_PROBED(kbd)) {
38844628Syokota		state->kbdc = kbdc_open(data[0]);
38944628Syokota		if (state->kbdc == NULL)
39042421Syokota			return ENXIO;
39144628Syokota		kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
39244628Syokota				data[0], IO_KBDSIZE);
39344628Syokota		bcopy(&key_map, keymap, sizeof(key_map));
39444628Syokota		bcopy(&accent_map, accmap, sizeof(accent_map));
39544628Syokota		bcopy(fkey_tab, fkeymap,
39644628Syokota		      imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
39744628Syokota		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
39844628Syokota		kbd->kb_data = (void *)state;
39944628Syokota
40044628Syokota		if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
40144628Syokota			if (flags & KB_CONF_FAIL_IF_NO_KBD)
40244628Syokota				return ENXIO;
40344628Syokota		} else {
40444628Syokota			KBD_FOUND_DEVICE(kbd);
40544628Syokota		}
40644628Syokota		atkbd_clear_state(kbd);
40744628Syokota		state->ks_mode = K_XLATE;
40844628Syokota		/*
40944628Syokota		 * FIXME: set the initial value for lock keys in ks_state
41044628Syokota		 * according to the BIOS data?
41144628Syokota		 */
41244628Syokota		KBD_PROBE_DONE(kbd);
41342421Syokota	}
41444628Syokota	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
41549820Syokota		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
41642421Syokota		if (KBD_HAS_DEVICE(kbd)
41744628Syokota	    	    && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
41844628Syokota	    	    && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD))
41942421Syokota			return ENXIO;
42044628Syokota		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
42155731Syokota		get_typematic(kbd);
42255731Syokota		delay[0] = kbd->kb_delay1;
42355731Syokota		delay[1] = kbd->kb_delay2;
42455731Syokota		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
42542421Syokota		KBD_INIT_DONE(kbd);
42642421Syokota	}
42742421Syokota	if (!KBD_IS_CONFIGURED(kbd)) {
42842421Syokota		if (kbd_register(kbd) < 0)
42942421Syokota			return ENXIO;
43042421Syokota		KBD_CONFIG_DONE(kbd);
43142421Syokota	}
43242421Syokota
43342421Syokota	return 0;
43442421Syokota}
43542421Syokota
43642421Syokota/* finish using this keyboard */
43742421Syokotastatic int
43842421Syokotaatkbd_term(keyboard_t *kbd)
43942421Syokota{
44042421Syokota	kbd_unregister(kbd);
44142421Syokota	return 0;
44242421Syokota}
44342421Syokota
44442421Syokota/* keyboard interrupt routine */
44542421Syokotastatic int
44644628Syokotaatkbd_intr(keyboard_t *kbd, void *arg)
44742421Syokota{
44842421Syokota	atkbd_state_t *state;
44955731Syokota	int delay[2];
45042421Syokota	int c;
45142421Syokota
45242421Syokota	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
45342421Syokota		/* let the callback function to process the input */
45442421Syokota		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
45542421Syokota					    kbd->kb_callback.kc_arg);
45642421Syokota	} else {
45742421Syokota		/* read and discard the input; no one is waiting for input */
45842421Syokota		do {
45942421Syokota			c = atkbd_read_char(kbd, FALSE);
46042421Syokota		} while (c != NOKEY);
46142421Syokota
46242421Syokota		if (!KBD_HAS_DEVICE(kbd)) {
46342421Syokota			/*
46442421Syokota			 * The keyboard was not detected before;
46542421Syokota			 * it must have been reconnected!
46642421Syokota			 */
46742421Syokota			state = (atkbd_state_t *)kbd->kb_data;
46842421Syokota			init_keyboard(state->kbdc, &kbd->kb_type,
46942421Syokota				      kbd->kb_config);
47042421Syokota			atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
47155731Syokota			get_typematic(kbd);
47255731Syokota			delay[0] = kbd->kb_delay1;
47355731Syokota			delay[1] = kbd->kb_delay2;
47455731Syokota			atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
47542421Syokota			KBD_FOUND_DEVICE(kbd);
47642421Syokota		}
47742421Syokota	}
47842421Syokota	return 0;
47942421Syokota}
48042421Syokota
48142421Syokota/* test the interface to the device */
48242421Syokotastatic int
48342421Syokotaatkbd_test_if(keyboard_t *kbd)
48442421Syokota{
48542421Syokota	int error;
48642421Syokota	int s;
48742421Syokota
48842421Syokota	error = 0;
48942421Syokota	empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
49042421Syokota	s = spltty();
49142421Syokota	if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
49242421Syokota		error = EIO;
49342421Syokota	else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
49442421Syokota		error = EIO;
49542421Syokota	splx(s);
49642421Syokota
49742421Syokota	return error;
49842421Syokota}
49942421Syokota
50042421Syokota/*
50142421Syokota * Enable the access to the device; until this function is called,
50242421Syokota * the client cannot read from the keyboard.
50342421Syokota */
50442421Syokotastatic int
50542421Syokotaatkbd_enable(keyboard_t *kbd)
50642421Syokota{
50742421Syokota	int s;
50842421Syokota
50942421Syokota	s = spltty();
51042421Syokota	KBD_ACTIVATE(kbd);
51142421Syokota	splx(s);
51242421Syokota	return 0;
51342421Syokota}
51442421Syokota
51542421Syokota/* disallow the access to the device */
51642421Syokotastatic int
51742421Syokotaatkbd_disable(keyboard_t *kbd)
51842421Syokota{
51942421Syokota	int s;
52042421Syokota
52142421Syokota	s = spltty();
52242421Syokota	KBD_DEACTIVATE(kbd);
52342421Syokota	splx(s);
52442421Syokota	return 0;
52542421Syokota}
52642421Syokota
52742421Syokota/* read one byte from the keyboard if it's allowed */
52842421Syokotastatic int
52942421Syokotaatkbd_read(keyboard_t *kbd, int wait)
53042421Syokota{
53142421Syokota	int c;
53242421Syokota
53342421Syokota	if (wait)
53442421Syokota		c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
53542421Syokota	else
53642421Syokota		c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
53754382Syokota	if (c != -1)
53854382Syokota		++kbd->kb_count;
53942421Syokota	return (KBD_IS_ACTIVE(kbd) ? c : -1);
54042421Syokota}
54142421Syokota
54242421Syokota/* check if data is waiting */
54342421Syokotastatic int
54442421Syokotaatkbd_check(keyboard_t *kbd)
54542421Syokota{
54642421Syokota	if (!KBD_IS_ACTIVE(kbd))
54742421Syokota		return FALSE;
54842421Syokota	return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
54942421Syokota}
55042421Syokota
55142421Syokota/* read char from the keyboard */
55242421Syokotastatic u_int
55342421Syokotaatkbd_read_char(keyboard_t *kbd, int wait)
55442421Syokota{
55542421Syokota	atkbd_state_t *state;
55642421Syokota	u_int action;
55742421Syokota	int scancode;
55842421Syokota	int keycode;
55942421Syokota
56042421Syokota	state = (atkbd_state_t *)kbd->kb_data;
56142421Syokotanext_code:
56242421Syokota	/* do we have a composed char to return? */
56342421Syokota	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
56442421Syokota		action = state->ks_composed_char;
56542421Syokota		state->ks_composed_char = 0;
56642421Syokota		if (action > UCHAR_MAX)
56742421Syokota			return ERRKEY;
56842421Syokota		return action;
56942421Syokota	}
57042421Syokota
57142421Syokota	/* see if there is something in the keyboard port */
57242421Syokota	if (wait) {
57342421Syokota		do {
57442421Syokota			scancode = read_kbd_data(state->kbdc);
57542421Syokota		} while (scancode == -1);
57642421Syokota	} else {
57742421Syokota		scancode = read_kbd_data_no_wait(state->kbdc);
57842421Syokota		if (scancode == -1)
57942421Syokota			return NOKEY;
58042421Syokota	}
58154382Syokota	++kbd->kb_count;
58242421Syokota
58354382Syokota#if KBDIO_DEBUG >= 10
58454382Syokota	printf("atkbd_read_char(): scancode:0x%x\n", scancode);
58554382Syokota#endif
58654382Syokota
58742421Syokota	/* return the byte as is for the K_RAW mode */
58842421Syokota	if (state->ks_mode == K_RAW)
58942421Syokota		return scancode;
59042421Syokota
59142421Syokota	/* translate the scan code into a keycode */
59242421Syokota	keycode = scancode & 0x7F;
59342421Syokota	switch (state->ks_prefix) {
59442421Syokota	case 0x00:	/* normal scancode */
59542421Syokota		switch(scancode) {
59642421Syokota		case 0xB8:	/* left alt (compose key) released */
59742421Syokota			if (state->ks_flags & COMPOSE) {
59842421Syokota				state->ks_flags &= ~COMPOSE;
59942421Syokota				if (state->ks_composed_char > UCHAR_MAX)
60042421Syokota					state->ks_composed_char = 0;
60142421Syokota			}
60242421Syokota			break;
60342421Syokota		case 0x38:	/* left alt (compose key) pressed */
60442421Syokota			if (!(state->ks_flags & COMPOSE)) {
60542421Syokota				state->ks_flags |= COMPOSE;
60642421Syokota				state->ks_composed_char = 0;
60742421Syokota			}
60842421Syokota			break;
60942421Syokota		case 0xE0:
61042421Syokota		case 0xE1:
61142421Syokota			state->ks_prefix = scancode;
61242421Syokota			goto next_code;
61342421Syokota		}
61442421Syokota		break;
61542421Syokota	case 0xE0:      /* 0xE0 prefix */
61642421Syokota		state->ks_prefix = 0;
61742421Syokota		switch (keycode) {
61842421Syokota		case 0x1C:	/* right enter key */
61942421Syokota			keycode = 0x59;
62042421Syokota			break;
62142421Syokota		case 0x1D:	/* right ctrl key */
62242421Syokota			keycode = 0x5A;
62342421Syokota			break;
62442421Syokota		case 0x35:	/* keypad divide key */
62542421Syokota	    		keycode = 0x5B;
62642421Syokota	    		break;
62742421Syokota		case 0x37:	/* print scrn key */
62842421Syokota	    		keycode = 0x5C;
62942421Syokota	    		break;
63042421Syokota		case 0x38:	/* right alt key (alt gr) */
63142421Syokota	    		keycode = 0x5D;
63242421Syokota	    		break;
63343337Syokota		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
63443337Syokota			keycode = 0x68;
63543337Syokota	    		break;
63642421Syokota		case 0x47:	/* grey home key */
63742421Syokota	    		keycode = 0x5E;
63842421Syokota	    		break;
63942421Syokota		case 0x48:	/* grey up arrow key */
64042421Syokota	    		keycode = 0x5F;
64142421Syokota	    		break;
64242421Syokota		case 0x49:	/* grey page up key */
64342421Syokota	    		keycode = 0x60;
64442421Syokota	    		break;
64542421Syokota		case 0x4B:	/* grey left arrow key */
64642421Syokota	    		keycode = 0x61;
64742421Syokota	    		break;
64842421Syokota		case 0x4D:	/* grey right arrow key */
64942421Syokota	    		keycode = 0x62;
65042421Syokota	    		break;
65142421Syokota		case 0x4F:	/* grey end key */
65242421Syokota	    		keycode = 0x63;
65342421Syokota	    		break;
65442421Syokota		case 0x50:	/* grey down arrow key */
65542421Syokota	    		keycode = 0x64;
65642421Syokota	    		break;
65742421Syokota		case 0x51:	/* grey page down key */
65842421Syokota	    		keycode = 0x65;
65942421Syokota	    		break;
66042421Syokota		case 0x52:	/* grey insert key */
66142421Syokota	    		keycode = 0x66;
66242421Syokota	    		break;
66342421Syokota		case 0x53:	/* grey delete key */
66442421Syokota	    		keycode = 0x67;
66542421Syokota	    		break;
66642421Syokota		/* the following 3 are only used on the MS "Natural" keyboard */
66742421Syokota		case 0x5b:	/* left Window key */
66842421Syokota	    		keycode = 0x69;
66942421Syokota	    		break;
67042421Syokota		case 0x5c:	/* right Window key */
67142421Syokota	    		keycode = 0x6a;
67242421Syokota	    		break;
67342421Syokota		case 0x5d:	/* menu key */
67442421Syokota	    		keycode = 0x6b;
67542421Syokota	    		break;
67642421Syokota		default:	/* ignore everything else */
67742421Syokota	    		goto next_code;
67842421Syokota		}
67942421Syokota		break;
68042421Syokota    	case 0xE1:	/* 0xE1 prefix */
68143337Syokota		/*
68243337Syokota		 * The pause/break key on the 101 keyboard produces:
68343337Syokota		 * E1-1D-45 E1-9D-C5
68443337Syokota		 * Ctrl-pause/break produces:
68543337Syokota		 * E0-46 E0-C6 (See above.)
68643337Syokota		 */
68742421Syokota		state->ks_prefix = 0;
68842421Syokota		if (keycode == 0x1D)
68942421Syokota	    		state->ks_prefix = 0x1D;
69042421Syokota		goto next_code;
69142421Syokota		/* NOT REACHED */
69242421Syokota    	case 0x1D:	/* pause / break */
69342421Syokota		state->ks_prefix = 0;
69442421Syokota		if (keycode != 0x45)
69543337Syokota			goto next_code;
69642421Syokota		keycode = 0x68;
69742421Syokota		break;
69842421Syokota	}
69942421Syokota
70043337Syokota	if (kbd->kb_type == KB_84) {
70143337Syokota		switch (keycode) {
70243337Syokota		case 0x37:	/* *(numpad)/print screen */
70343337Syokota			if (state->ks_flags & SHIFTS)
70443337Syokota	    			keycode = 0x5c;	/* print screen */
70543337Syokota			break;
70643337Syokota		case 0x45:	/* num lock/pause */
70743337Syokota			if (state->ks_flags & CTLS)
70843337Syokota				keycode = 0x68;	/* pause */
70943337Syokota			break;
71043337Syokota		case 0x46:	/* scroll lock/break */
71143337Syokota			if (state->ks_flags & CTLS)
71243337Syokota				keycode = 0x6c;	/* break */
71343337Syokota			break;
71443337Syokota		}
71543337Syokota	} else if (kbd->kb_type == KB_101) {
71643337Syokota		switch (keycode) {
71743337Syokota		case 0x5c:	/* print screen */
71843337Syokota			if (state->ks_flags & ALTS)
71943337Syokota				keycode = 0x54;	/* sysrq */
72043337Syokota			break;
72143337Syokota		case 0x68:	/* pause/break */
72243337Syokota			if (state->ks_flags & CTLS)
72343337Syokota				keycode = 0x6c;	/* break */
72443337Syokota			break;
72543337Syokota		}
72643337Syokota	}
72743337Syokota
72842421Syokota	/* return the key code in the K_CODE mode */
72942421Syokota	if (state->ks_mode == K_CODE)
73042421Syokota		return (keycode | (scancode & 0x80));
73142421Syokota
73242421Syokota	/* compose a character code */
73342421Syokota	if (state->ks_flags & COMPOSE) {
73447293Syokota		switch (keycode | (scancode & 0x80)) {
73542421Syokota		/* key pressed, process it */
73642421Syokota		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
73742421Syokota			state->ks_composed_char *= 10;
73846765Syokota			state->ks_composed_char += keycode - 0x40;
73954382Syokota			kbd->kb_prev_key = keycode | (scancode & 0x80);
74042421Syokota			if (state->ks_composed_char > UCHAR_MAX)
74142421Syokota				return ERRKEY;
74242421Syokota			goto next_code;
74342421Syokota		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
74442421Syokota			state->ks_composed_char *= 10;
74546765Syokota			state->ks_composed_char += keycode - 0x47;
74654382Syokota			kbd->kb_prev_key = keycode | (scancode & 0x80);
74742421Syokota			if (state->ks_composed_char > UCHAR_MAX)
74842421Syokota				return ERRKEY;
74942421Syokota			goto next_code;
75042421Syokota		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
75142421Syokota			state->ks_composed_char *= 10;
75246765Syokota			state->ks_composed_char += keycode - 0x4E;
75354382Syokota			kbd->kb_prev_key = keycode | (scancode & 0x80);
75442421Syokota			if (state->ks_composed_char > UCHAR_MAX)
75542421Syokota				return ERRKEY;
75642421Syokota			goto next_code;
75742421Syokota		case 0x52:				/* keypad 0 */
75842421Syokota			state->ks_composed_char *= 10;
75954382Syokota			kbd->kb_prev_key = keycode | (scancode & 0x80);
76042421Syokota			if (state->ks_composed_char > UCHAR_MAX)
76142421Syokota				return ERRKEY;
76242421Syokota			goto next_code;
76342421Syokota
76442421Syokota		/* key released, no interest here */
76542421Syokota		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
76642421Syokota		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
76742421Syokota		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
76842421Syokota		case 0xD2:				/* keypad 0 */
76954382Syokota			kbd->kb_prev_key = keycode | (scancode & 0x80);
77042421Syokota			goto next_code;
77142421Syokota
77242421Syokota		case 0x38:				/* left alt key */
77342421Syokota			break;
77442421Syokota
77542421Syokota		default:
77642421Syokota			if (state->ks_composed_char > 0) {
77742421Syokota				state->ks_flags &= ~COMPOSE;
77842421Syokota				state->ks_composed_char = 0;
77954382Syokota				kbd->kb_prev_key = keycode | (scancode & 0x80);
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);
78954382Syokota	kbd->kb_prev_key = keycode | (scancode & 0x80);
79042421Syokota	if (action == NOKEY)
79142421Syokota		goto next_code;
79242421Syokota	else
79342421Syokota		return action;
79442421Syokota}
79542421Syokota
79642421Syokota/* check if char is waiting */
79742421Syokotastatic int
79842421Syokotaatkbd_check_char(keyboard_t *kbd)
79942421Syokota{
80042421Syokota	atkbd_state_t *state;
80142421Syokota
80242421Syokota	if (!KBD_IS_ACTIVE(kbd))
80342421Syokota		return FALSE;
80442421Syokota	state = (atkbd_state_t *)kbd->kb_data;
80542421Syokota	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
80642421Syokota		return TRUE;
80742421Syokota	return kbdc_data_ready(state->kbdc);
80842421Syokota}
80942421Syokota
81042421Syokota/* some useful control functions */
81142421Syokotastatic int
81242421Syokotaatkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
81342421Syokota{
81442421Syokota	/* trasnlate LED_XXX bits into the device specific bits */
81542421Syokota	static u_char ledmap[8] = {
81642421Syokota		0, 4, 2, 6, 1, 5, 3, 7,
81742421Syokota	};
81842421Syokota	atkbd_state_t *state = kbd->kb_data;
81942421Syokota	int error;
82042421Syokota	int s;
82142421Syokota	int i;
82242421Syokota
82342421Syokota	s = spltty();
82442421Syokota	switch (cmd) {
82542421Syokota
82642421Syokota	case KDGKBMODE:		/* get keyboard mode */
82742421Syokota		*(int *)arg = state->ks_mode;
82842421Syokota		break;
82942421Syokota	case KDSKBMODE:		/* set keyboard mode */
83042421Syokota		switch (*(int *)arg) {
83142421Syokota		case K_XLATE:
83242421Syokota			if (state->ks_mode != K_XLATE) {
83342421Syokota				/* make lock key state and LED state match */
83442421Syokota				state->ks_state &= ~LOCK_MASK;
83542421Syokota				state->ks_state |= KBD_LED_VAL(kbd);
83642421Syokota			}
83742421Syokota			/* FALL THROUGH */
83842421Syokota		case K_RAW:
83942421Syokota		case K_CODE:
84042421Syokota			if (state->ks_mode != *(int *)arg) {
84142421Syokota				atkbd_clear_state(kbd);
84242421Syokota				state->ks_mode = *(int *)arg;
84342421Syokota			}
84442421Syokota			break;
84542421Syokota		default:
84642421Syokota			splx(s);
84742421Syokota			return EINVAL;
84842421Syokota		}
84942421Syokota		break;
85042421Syokota
85142421Syokota	case KDGETLED:		/* get keyboard LED */
85242421Syokota		*(int *)arg = KBD_LED_VAL(kbd);
85342421Syokota		break;
85442421Syokota	case KDSETLED:		/* set keyboard LED */
85542421Syokota		/* NOTE: lock key state in ks_state won't be changed */
85642421Syokota		if (*(int *)arg & ~LOCK_MASK) {
85742421Syokota			splx(s);
85842421Syokota			return EINVAL;
85942421Syokota		}
86042421Syokota		i = *(int *)arg;
86142421Syokota		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
86242421Syokota		if (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;
92042421Syokota		/* FALL THROUGH */
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
108642421Syokota	/* flush any noise in the buffer */
108742421Syokota	empty_both_buffers(kbdc, 10);
108842421Syokota
108942421Syokota	/* save the current keyboard controller command byte */
109042421Syokota	m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
109142421Syokota	c = get_controller_command_byte(kbdc);
109242421Syokota	if (c == -1) {
109342421Syokota		/* CONTROLLER ERROR */
109442421Syokota		kbdc_set_device_mask(kbdc, m);
109542421Syokota		kbdc_lock(kbdc, FALSE);
109642421Syokota		return ENXIO;
109742421Syokota	}
109842421Syokota
109942421Syokota	/*
110042421Syokota	 * The keyboard may have been screwed up by the boot block.
110142421Syokota	 * We may just be able to recover from error by testing the controller
110242421Syokota	 * and the keyboard port. The controller command byte needs to be
110342421Syokota	 * saved before this recovery operation, as some controllers seem
110442421Syokota	 * to set the command byte to particular values.
110542421Syokota	 */
110642421Syokota	test_controller(kbdc);
110742421Syokota	test_kbd_port(kbdc);
110842421Syokota
110942421Syokota	err = get_kbd_echo(kbdc);
111042421Syokota	if (err == 0) {
111142421Syokota		kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
111242421Syokota	} else {
111342421Syokota		if (c != -1)
111442421Syokota			/* try to restore the command byte as before */
111542421Syokota			set_controller_command_byte(kbdc, 0xff, c);
111642421Syokota		kbdc_set_device_mask(kbdc, m);
111742421Syokota	}
111842421Syokota
111942421Syokota	kbdc_lock(kbdc, FALSE);
112042421Syokota	return err;
112142421Syokota}
112242421Syokota
112342421Syokotastatic int
112442421Syokotainit_keyboard(KBDC kbdc, int *type, int flags)
112542421Syokota{
112642421Syokota	int codeset;
112742421Syokota	int id;
112842421Syokota	int c;
112942421Syokota
113042421Syokota	if (!kbdc_lock(kbdc, TRUE)) {
113142421Syokota		/* driver error? */
113242421Syokota		return EIO;
113342421Syokota	}
113442421Syokota
113542421Syokota	/* save the current controller command byte */
113654382Syokota	empty_both_buffers(kbdc, 200);
113742421Syokota	c = get_controller_command_byte(kbdc);
113842421Syokota	if (c == -1) {
113942421Syokota		/* CONTROLLER ERROR */
114042421Syokota		kbdc_lock(kbdc, FALSE);
114142421Syokota		printf("atkbd: unable to get the current command byte value.\n");
114242421Syokota		return EIO;
114342421Syokota	}
114442421Syokota	if (bootverbose)
114542421Syokota		printf("atkbd: the current kbd controller command byte %04x\n",
114642421Syokota		       c);
114742421Syokota#if 0
114842421Syokota	/* override the keyboard lock switch */
114942421Syokota	c |= KBD_OVERRIDE_KBD_LOCK;
115042421Syokota#endif
115142421Syokota
115242421Syokota	/* enable the keyboard port, but disable the keyboard intr. */
115342421Syokota	if (setup_kbd_port(kbdc, TRUE, FALSE)) {
115442421Syokota		/* CONTROLLER ERROR: there is very little we can do... */
115542421Syokota		printf("atkbd: unable to set the command byte.\n");
115642421Syokota		kbdc_lock(kbdc, FALSE);
115742421Syokota		return EIO;
115842421Syokota	}
115942421Syokota
116042421Syokota	/*
116142421Syokota	 * Check if we have an XT keyboard before we attempt to reset it.
116242421Syokota	 * The procedure assumes that the keyboard and the controller have
116342421Syokota	 * been set up properly by BIOS and have not been messed up
116442421Syokota	 * during the boot process.
116542421Syokota	 */
116642421Syokota	codeset = -1;
116742421Syokota	if (flags & KB_CONF_ALT_SCANCODESET)
116842421Syokota		/* the user says there is a XT keyboard */
116942421Syokota		codeset = 1;
117042421Syokota#ifdef KBD_DETECT_XT_KEYBOARD
117142421Syokota	else if ((c & KBD_TRANSLATION) == 0) {
117242421Syokota		/* SET_SCANCODE_SET is not always supported; ignore error */
117342421Syokota		if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
117442421Syokota			== KBD_ACK)
117542421Syokota			codeset = read_kbd_data(kbdc);
117642421Syokota	}
117742421Syokota	if (bootverbose)
117842421Syokota		printf("atkbd: scancode set %d\n", codeset);
117942421Syokota#endif /* KBD_DETECT_XT_KEYBOARD */
118042421Syokota
118142421Syokota	*type = KB_OTHER;
118242421Syokota	id = get_kbd_id(kbdc);
118342421Syokota	switch(id) {
118455730Syokota	case 0x41ab:	/* 101/102/... Enhanced */
118555730Syokota	case 0x83ab:	/* ditto */
118655730Syokota	case 0x54ab:	/* SpaceSaver */
118755730Syokota	case 0x84ab:	/* ditto */
118855730Syokota#if 0
118955730Syokota	case 0x90ab:	/* 'G' */
119055730Syokota	case 0x91ab:	/* 'P' */
119155730Syokota	case 0x92ab:	/* 'A' */
119255730Syokota#endif
119342421Syokota		*type = KB_101;
119442421Syokota		break;
119542421Syokota	case -1:	/* AT 84 keyboard doesn't return ID */
119642421Syokota		*type = KB_84;
119742421Syokota		break;
119842421Syokota	default:
119942421Syokota		break;
120042421Syokota	}
120142421Syokota	if (bootverbose)
120242421Syokota		printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
120342421Syokota
120442421Syokota	/* reset keyboard hardware */
120542421Syokota	if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
120642421Syokota		/*
120742421Syokota		 * KEYBOARD ERROR
120842421Syokota		 * Keyboard reset may fail either because the keyboard
120942421Syokota		 * doen't exist, or because the keyboard doesn't pass
121042421Syokota		 * the self-test, or the keyboard controller on the
121142421Syokota		 * motherboard and the keyboard somehow fail to shake hands.
121242421Syokota		 * It is just possible, particularly in the last case,
121342421Syokota		 * that the keyoard controller may be left in a hung state.
121442421Syokota		 * test_controller() and test_kbd_port() appear to bring
121542421Syokota		 * the keyboard controller back (I don't know why and how,
121642421Syokota		 * though.)
121742421Syokota		 */
121842421Syokota		empty_both_buffers(kbdc, 10);
121942421Syokota		test_controller(kbdc);
122042421Syokota		test_kbd_port(kbdc);
122142421Syokota		/*
122242421Syokota		 * We could disable the keyboard port and interrupt... but,
122342421Syokota		 * the keyboard may still exist (see above).
122442421Syokota		 */
122542421Syokota		set_controller_command_byte(kbdc, 0xff, c);
122642421Syokota		kbdc_lock(kbdc, FALSE);
122742421Syokota		if (bootverbose)
122842421Syokota			printf("atkbd: failed to reset the keyboard.\n");
122942421Syokota		return EIO;
123042421Syokota	}
123142421Syokota
123242421Syokota	/*
123342421Syokota	 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
123442421Syokota	 * such as those on the IBM ThinkPad laptop computers can be used
123542421Syokota	 * with the standard console driver.
123642421Syokota	 */
123742421Syokota	if (codeset == 1) {
123842421Syokota		if (send_kbd_command_and_data(kbdc,
123942421Syokota			KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
124042421Syokota			/* XT kbd doesn't need scan code translation */
124142421Syokota			c &= ~KBD_TRANSLATION;
124242421Syokota		} else {
124342421Syokota			/*
124442421Syokota			 * KEYBOARD ERROR
124542421Syokota			 * The XT kbd isn't usable unless the proper scan
124642421Syokota			 * code set is selected.
124742421Syokota			 */
124842421Syokota			set_controller_command_byte(kbdc, 0xff, c);
124942421Syokota			kbdc_lock(kbdc, FALSE);
125042421Syokota			printf("atkbd: unable to set the XT keyboard mode.\n");
125142421Syokota			return EIO;
125242421Syokota		}
125342421Syokota	}
125442421Syokota
125542831Syokota#ifdef __alpha__
125642831Syokota	if (send_kbd_command_and_data(
125742831Syokota		kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
125842831Syokota		printf("atkbd: can't set translation.\n");
125942831Syokota
126042831Syokota	}
126142831Syokota	c |= KBD_TRANSLATION;
126242831Syokota#endif
126342831Syokota
126442421Syokota	/* enable the keyboard port and intr. */
126542421Syokota	if (!set_controller_command_byte(kbdc,
126642421Syokota		KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
126742421Syokota		(c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
126842421Syokota		    | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
126942421Syokota		/*
127042421Syokota		 * CONTROLLER ERROR
127142421Syokota		 * This is serious; we are left with the disabled
127242421Syokota		 * keyboard intr.
127342421Syokota		 */
127442421Syokota		set_controller_command_byte(kbdc, 0xff, c);
127542421Syokota		kbdc_lock(kbdc, FALSE);
127642421Syokota		printf("atkbd: unable to enable the keyboard port and intr.\n");
127742421Syokota		return EIO;
127842421Syokota	}
127942421Syokota
128042421Syokota	kbdc_lock(kbdc, FALSE);
128142421Syokota	return 0;
128242421Syokota}
128342421Syokota
128442421Syokotastatic int
128542421Syokotawrite_kbd(KBDC kbdc, int command, int data)
128642421Syokota{
128742421Syokota    int s;
128842421Syokota
128942421Syokota    /* prevent the timeout routine from polling the keyboard */
129042421Syokota    if (!kbdc_lock(kbdc, TRUE))
129142421Syokota	return EBUSY;
129242421Syokota
129342421Syokota    /* disable the keyboard and mouse interrupt */
129442421Syokota    s = spltty();
129542421Syokota#if 0
129642421Syokota    c = get_controller_command_byte(kbdc);
129742421Syokota    if ((c == -1)
129842421Syokota	|| !set_controller_command_byte(kbdc,
129942421Syokota            kbdc_get_device_mask(kbdc),
130042421Syokota            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
130142421Syokota                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
130242421Syokota	/* CONTROLLER ERROR */
130342421Syokota        kbdc_lock(kbdc, FALSE);
130442421Syokota	splx(s);
130542421Syokota	return EIO;
130642421Syokota    }
130742421Syokota    /*
130842421Syokota     * Now that the keyboard controller is told not to generate
130942421Syokota     * the keyboard and mouse interrupts, call `splx()' to allow
131042421Syokota     * the other tty interrupts. The clock interrupt may also occur,
131142421Syokota     * but the timeout routine (`scrn_timer()') will be blocked
131242421Syokota     * by the lock flag set via `kbdc_lock()'
131342421Syokota     */
131442421Syokota    splx(s);
131542421Syokota#endif
131642421Syokota
131742421Syokota    if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
131842421Syokota        send_kbd_command(kbdc, KBDC_ENABLE_KBD);
131942421Syokota
132042421Syokota#if 0
132142421Syokota    /* restore the interrupts */
132242421Syokota    if (!set_controller_command_byte(kbdc,
132342421Syokota            kbdc_get_device_mask(kbdc),
132442421Syokota	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
132542421Syokota	/* CONTROLLER ERROR */
132642421Syokota    }
132742421Syokota#else
132842421Syokota    splx(s);
132942421Syokota#endif
133042421Syokota    kbdc_lock(kbdc, FALSE);
133142421Syokota
133242421Syokota    return 0;
133342421Syokota}
133442421Syokota
133542421Syokotastatic int
133642421Syokotaget_kbd_id(KBDC kbdc)
133742421Syokota{
133842421Syokota	int id1, id2;
133942421Syokota
134042421Syokota	empty_both_buffers(kbdc, 10);
134142421Syokota	id1 = id2 = -1;
134242421Syokota	if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
134342421Syokota		return -1;
134442421Syokota
134542421Syokota	DELAY(10000); 	/* 10 msec delay */
134642421Syokota	id1 = read_kbd_data(kbdc);
134742421Syokota	if (id1 != -1)
134842421Syokota		id2 = read_kbd_data(kbdc);
134942421Syokota
135042421Syokota	if ((id1 == -1) || (id2 == -1)) {
135142421Syokota		empty_both_buffers(kbdc, 10);
135242421Syokota		test_controller(kbdc);
135342421Syokota		test_kbd_port(kbdc);
135442421Syokota		return -1;
135542421Syokota	}
135642421Syokota	return ((id2 << 8) | id1);
135742421Syokota}
135842421Syokota
135954543Syokotastatic int delays[] = { 250, 500, 750, 1000 };
136054543Syokotastatic int rates[] = {  34,  38,  42,  46,  50,  55,  59,  63,
136154543Syokota			68,  76,  84,  92, 100, 110, 118, 126,
136254543Syokota		       136, 152, 168, 184, 200, 220, 236, 252,
136354543Syokota		       272, 304, 336, 368, 400, 440, 472, 504 };
136454543Syokota
136544628Syokotastatic int
136654543Syokotatypematic_delay(int i)
136754543Syokota{
136854543Syokota	return delays[(i >> 5) & 3];
136954543Syokota}
137054543Syokota
137154543Syokotastatic int
137254543Syokotatypematic_rate(int i)
137354543Syokota{
137454543Syokota	return rates[i & 0x1f];
137554543Syokota}
137654543Syokota
137754543Syokotastatic int
137844628Syokotatypematic(int delay, int rate)
137944628Syokota{
138044628Syokota	int value;
138144628Syokota	int i;
138244628Syokota
138344628Syokota	for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
138444628Syokota		if (delay >= delays[i])
138544628Syokota			break;
138644628Syokota	}
138744628Syokota	value = i << 5;
138844628Syokota	for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
138944628Syokota		if (rate >= rates[i])
139044628Syokota			break;
139144628Syokota	}
139244628Syokota	value |= i;
139344628Syokota	return value;
139444628Syokota}
139544628Syokota
139642421Syokota#endif /* NATKBD > 0 */
1397