atkbd.c revision 282269
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer as
10 *    the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/dev/atkbdc/atkbd.c 282269 2015-04-30 07:00:25Z royger $");
30
31#include "opt_compat.h"
32#include "opt_kbd.h"
33#include "opt_atkbd.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/bus.h>
39#include <sys/eventhandler.h>
40#include <sys/proc.h>
41#include <sys/limits.h>
42#include <sys/malloc.h>
43
44#include <machine/bus.h>
45#include <machine/resource.h>
46
47#if defined(__i386__) || defined(__amd64__)
48#include <machine/md_var.h>
49#include <machine/psl.h>
50
51#include <vm/vm.h>
52#include <vm/pmap.h>
53#include <vm/vm_param.h>
54
55#include <isa/isareg.h>
56#endif /* __i386__ || __amd64__ */
57
58#include <sys/kbio.h>
59#include <dev/kbd/kbdreg.h>
60#include <dev/atkbdc/atkbdreg.h>
61#include <dev/atkbdc/atkbdcreg.h>
62
63typedef struct atkbd_state {
64	KBDC		kbdc;		/* keyboard controller */
65	int		ks_mode;	/* input mode (K_XLATE,K_RAW,K_CODE) */
66	int		ks_flags;	/* flags */
67#define COMPOSE		(1 << 0)
68	int		ks_polling;
69	int		ks_state;	/* shift/lock key state */
70	int		ks_accents;	/* accent key index (> 0) */
71	u_int		ks_composed_char; /* composed char code (> 0) */
72	u_char		ks_prefix;	/* AT scan code prefix */
73	struct callout	ks_timer;
74} atkbd_state_t;
75
76static void		atkbd_timeout(void *arg);
77static void		atkbd_shutdown_final(void *v);
78static int		atkbd_reset(KBDC kbdc, int flags, int c);
79
80#define HAS_QUIRK(p, q)		(((atkbdc_softc_t *)(p))->quirks & q)
81#define ALLOW_DISABLE_KBD(kbdc)	!HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED)
82
83#define DEFAULT_DELAY		0x1  /* 500ms */
84#define DEFAULT_RATE		0x10 /* 14Hz */
85
86int
87atkbd_probe_unit(device_t dev, int irq, int flags)
88{
89	keyboard_switch_t *sw;
90	int args[2];
91	int error;
92
93	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
94	if (sw == NULL)
95		return ENXIO;
96
97	args[0] = device_get_unit(device_get_parent(dev));
98	args[1] = irq;
99	error = (*sw->probe)(device_get_unit(dev), args, flags);
100	if (error)
101		return error;
102	return 0;
103}
104
105int
106atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags)
107{
108	keyboard_switch_t *sw;
109	atkbd_state_t *state;
110	int args[2];
111	int error;
112	int unit;
113
114	sw = kbd_get_switch(ATKBD_DRIVER_NAME);
115	if (sw == NULL)
116		return ENXIO;
117
118	/* reset, initialize and enable the device */
119	unit = device_get_unit(dev);
120	args[0] = device_get_unit(device_get_parent(dev));
121	args[1] = irq;
122	*kbd = NULL;
123	error = (*sw->probe)(unit, args, flags);
124	if (error)
125		return error;
126	error = (*sw->init)(unit, kbd, args, flags);
127	if (error)
128		return error;
129	(*sw->enable)(*kbd);
130
131#ifdef KBD_INSTALL_CDEV
132	/* attach a virtual keyboard cdev */
133	error = kbd_attach(*kbd);
134	if (error)
135		return error;
136#endif
137
138	/*
139	 * This is a kludge to compensate for lost keyboard interrupts.
140	 * A similar code used to be in syscons. See below. XXX
141	 */
142	state = (atkbd_state_t *)(*kbd)->kb_data;
143	callout_init(&state->ks_timer, 0);
144	atkbd_timeout(*kbd);
145
146	if (bootverbose)
147		(*sw->diag)(*kbd, bootverbose);
148
149	EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd,
150	    SHUTDOWN_PRI_DEFAULT);
151
152	return 0;
153}
154
155static void
156atkbd_timeout(void *arg)
157{
158	atkbd_state_t *state;
159	keyboard_t *kbd;
160	int s;
161
162	/*
163	 * The original text of the following comments are extracted
164	 * from syscons.c (1.287)
165	 *
166	 * With release 2.1 of the Xaccel server, the keyboard is left
167	 * hanging pretty often. Apparently an interrupt from the
168	 * keyboard is lost, and I don't know why (yet).
169	 * This ugly hack calls the low-level interrupt routine if input
170	 * is ready for the keyboard and conveniently hides the problem. XXX
171	 *
172	 * Try removing anything stuck in the keyboard controller; whether
173	 * it's a keyboard scan code or mouse data. The low-level
174	 * interrupt routine doesn't read the mouse data directly,
175	 * but the keyboard controller driver will, as a side effect.
176	 */
177	/*
178	 * And here is bde's original comment about this:
179	 *
180	 * This is necessary to handle edge triggered interrupts - if we
181	 * returned when our IRQ is high due to unserviced input, then there
182	 * would be no more keyboard IRQs until the keyboard is reset by
183	 * external powers.
184	 *
185	 * The keyboard apparently unwedges the irq in most cases.
186	 */
187	s = spltty();
188	kbd = (keyboard_t *)arg;
189	if (kbdd_lock(kbd, TRUE)) {
190		/*
191		 * We have seen the lock flag is not set. Let's reset
192		 * the flag early, otherwise the LED update routine fails
193		 * which may want the lock during the interrupt routine.
194		 */
195		kbdd_lock(kbd, FALSE);
196		if (kbdd_check_char(kbd))
197			kbdd_intr(kbd, NULL);
198	}
199	splx(s);
200	state = (atkbd_state_t *)kbd->kb_data;
201	callout_reset(&state->ks_timer, hz / 10, atkbd_timeout, arg);
202}
203
204/* LOW-LEVEL */
205
206#define ATKBD_DEFAULT	0
207
208/* keyboard driver declaration */
209static int		atkbd_configure(int flags);
210static kbd_probe_t	atkbd_probe;
211static kbd_init_t	atkbd_init;
212static kbd_term_t	atkbd_term;
213static kbd_intr_t	atkbd_intr;
214static kbd_test_if_t	atkbd_test_if;
215static kbd_enable_t	atkbd_enable;
216static kbd_disable_t	atkbd_disable;
217static kbd_read_t	atkbd_read;
218static kbd_check_t	atkbd_check;
219static kbd_read_char_t	atkbd_read_char;
220static kbd_check_char_t	atkbd_check_char;
221static kbd_ioctl_t	atkbd_ioctl;
222static kbd_lock_t	atkbd_lock;
223static kbd_clear_state_t atkbd_clear_state;
224static kbd_get_state_t	atkbd_get_state;
225static kbd_set_state_t	atkbd_set_state;
226static kbd_poll_mode_t	atkbd_poll;
227
228static keyboard_switch_t atkbdsw = {
229	atkbd_probe,
230	atkbd_init,
231	atkbd_term,
232	atkbd_intr,
233	atkbd_test_if,
234	atkbd_enable,
235	atkbd_disable,
236	atkbd_read,
237	atkbd_check,
238	atkbd_read_char,
239	atkbd_check_char,
240	atkbd_ioctl,
241	atkbd_lock,
242	atkbd_clear_state,
243	atkbd_get_state,
244	atkbd_set_state,
245	genkbd_get_fkeystr,
246	atkbd_poll,
247	genkbd_diag,
248};
249
250KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
251
252/* local functions */
253static int		set_typematic(keyboard_t *kbd);
254static int		setup_kbd_port(KBDC kbdc, int port, int intr);
255static int		get_kbd_echo(KBDC kbdc);
256static int		probe_keyboard(KBDC kbdc, int flags);
257static int		init_keyboard(KBDC kbdc, int *type, int flags);
258static int		write_kbd(KBDC kbdc, int command, int data);
259static int		get_kbd_id(KBDC kbdc);
260static int		typematic(int delay, int rate);
261static int		typematic_delay(int delay);
262static int		typematic_rate(int rate);
263
264/* local variables */
265
266/* the initial key map, accent map and fkey strings */
267#ifdef ATKBD_DFLT_KEYMAP
268#define KBD_DFLT_KEYMAP
269#include "atkbdmap.h"
270#endif
271#include <dev/kbd/kbdtables.h>
272
273/* structures for the default keyboard */
274static keyboard_t	default_kbd;
275static atkbd_state_t	default_kbd_state;
276static keymap_t		default_keymap;
277static accentmap_t	default_accentmap;
278static fkeytab_t	default_fkeytab[NUM_FKEYS];
279
280/*
281 * The back door to the keyboard driver!
282 * This function is called by the console driver, via the kbdio module,
283 * to tickle keyboard drivers when the low-level console is being initialized.
284 * Almost nothing in the kernel has been initialied yet.  Try to probe
285 * keyboards if possible.
286 * NOTE: because of the way the low-level console is initialized, this routine
287 * may be called more than once!!
288 */
289static int
290atkbd_configure(int flags)
291{
292	keyboard_t *kbd;
293	int arg[2];
294	int i;
295
296	/*
297	 * Probe the keyboard controller, if not present or if the driver
298	 * is disabled, unregister the keyboard if any.
299	 */
300	if (atkbdc_configure() != 0 ||
301	    resource_disabled("atkbd", ATKBD_DEFAULT)) {
302		i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
303		if (i >= 0) {
304			kbd = kbd_get_keyboard(i);
305			kbd_unregister(kbd);
306			kbd->kb_flags &= ~KB_REGISTERED;
307		}
308		return 0;
309	}
310
311	/* XXX: a kludge to obtain the device configuration flags */
312	if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
313		flags |= i;
314
315	/* probe the default keyboard */
316	arg[0] = -1;
317	arg[1] = -1;
318	kbd = NULL;
319	if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
320		return 0;
321	if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
322		return 0;
323
324	/* return the number of found keyboards */
325	return 1;
326}
327
328/* low-level functions */
329
330/* detect a keyboard */
331static int
332atkbd_probe(int unit, void *arg, int flags)
333{
334	KBDC kbdc;
335	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
336
337	/* XXX */
338	if (unit == ATKBD_DEFAULT) {
339		if (KBD_IS_PROBED(&default_kbd))
340			return 0;
341	}
342
343	kbdc = atkbdc_open(data[0]);
344	if (kbdc == NULL)
345		return ENXIO;
346	if (probe_keyboard(kbdc, flags)) {
347		if (flags & KB_CONF_FAIL_IF_NO_KBD)
348			return ENXIO;
349	}
350	return 0;
351}
352
353/* reset and initialize the device */
354static int
355atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
356{
357	keyboard_t *kbd;
358	atkbd_state_t *state;
359	keymap_t *keymap;
360	accentmap_t *accmap;
361	fkeytab_t *fkeymap;
362	int fkeymap_size;
363	int delay[2];
364	int *data = (int *)arg;	/* data[0]: controller, data[1]: irq */
365	int error, needfree;
366
367	/* XXX */
368	if (unit == ATKBD_DEFAULT) {
369		*kbdp = kbd = &default_kbd;
370		if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
371			return 0;
372		state = &default_kbd_state;
373		keymap = &default_keymap;
374		accmap = &default_accentmap;
375		fkeymap = default_fkeytab;
376		fkeymap_size =
377			sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
378		needfree = 0;
379	} else if (*kbdp == NULL) {
380		*kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
381		state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
382		/* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
383		keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
384		accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
385		fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
386		fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
387		needfree = 1;
388		if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
389		     || (accmap == NULL) || (fkeymap == NULL)) {
390			error = ENOMEM;
391			goto bad;
392		}
393	} else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
394		return 0;
395	} else {
396		kbd = *kbdp;
397		state = (atkbd_state_t *)kbd->kb_data;
398		bzero(state, sizeof(*state));
399		keymap = kbd->kb_keymap;
400		accmap = kbd->kb_accentmap;
401		fkeymap = kbd->kb_fkeytab;
402		fkeymap_size = kbd->kb_fkeytab_size;
403		needfree = 0;
404	}
405
406	if (!KBD_IS_PROBED(kbd)) {
407		state->kbdc = atkbdc_open(data[0]);
408		if (state->kbdc == NULL) {
409			error = ENXIO;
410			goto bad;
411		}
412		kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
413				0, 0);
414		bcopy(&key_map, keymap, sizeof(key_map));
415		bcopy(&accent_map, accmap, sizeof(accent_map));
416		bcopy(fkey_tab, fkeymap,
417		    imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab)));
418		kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
419		kbd->kb_data = (void *)state;
420
421		if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
422			if (flags & KB_CONF_FAIL_IF_NO_KBD) {
423				error = ENXIO;
424				goto bad;
425			}
426		} else {
427			KBD_FOUND_DEVICE(kbd);
428		}
429		atkbd_clear_state(kbd);
430		state->ks_mode = K_XLATE;
431		/*
432		 * FIXME: set the initial value for lock keys in ks_state
433		 * according to the BIOS data?
434		 */
435		KBD_PROBE_DONE(kbd);
436	}
437	if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
438		kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
439		if (KBD_HAS_DEVICE(kbd)
440		    && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
441		    && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
442			kbd_unregister(kbd);
443			error = ENXIO;
444			goto bad;
445		}
446		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
447		set_typematic(kbd);
448		delay[0] = kbd->kb_delay1;
449		delay[1] = kbd->kb_delay2;
450		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
451		KBD_INIT_DONE(kbd);
452	}
453	if (!KBD_IS_CONFIGURED(kbd)) {
454		if (kbd_register(kbd) < 0) {
455			error = ENXIO;
456			goto bad;
457		}
458		KBD_CONFIG_DONE(kbd);
459	}
460
461	return 0;
462bad:
463	if (needfree) {
464		if (state != NULL)
465			free(state, M_DEVBUF);
466		if (keymap != NULL)
467			free(keymap, M_DEVBUF);
468		if (accmap != NULL)
469			free(accmap, M_DEVBUF);
470		if (fkeymap != NULL)
471			free(fkeymap, M_DEVBUF);
472		if (kbd != NULL) {
473			free(kbd, M_DEVBUF);
474			*kbdp = NULL;	/* insure ref doesn't leak to caller */
475		}
476	}
477	return error;
478}
479
480/* finish using this keyboard */
481static int
482atkbd_term(keyboard_t *kbd)
483{
484	atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
485
486	kbd_unregister(kbd);
487	callout_drain(&state->ks_timer);
488	return 0;
489}
490
491/* keyboard interrupt routine */
492static int
493atkbd_intr(keyboard_t *kbd, void *arg)
494{
495	atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
496	int delay[2];
497	int c;
498
499	if (!KBD_HAS_DEVICE(kbd)) {
500		/*
501		 * The keyboard was not detected before;
502		 * it must have been reconnected!
503		 */
504		init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config);
505		KBD_FOUND_DEVICE(kbd);
506		atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
507		set_typematic(kbd);
508		delay[0] = kbd->kb_delay1;
509		delay[1] = kbd->kb_delay2;
510		atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
511	}
512
513	if (state->ks_polling)
514		return 0;
515
516	if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
517		/* let the callback function to process the input */
518		(*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
519					    kbd->kb_callback.kc_arg);
520	} else {
521		/* read and discard the input; no one is waiting for input */
522		do {
523			c = atkbd_read_char(kbd, FALSE);
524		} while (c != NOKEY);
525	}
526	return 0;
527}
528
529/* test the interface to the device */
530static int
531atkbd_test_if(keyboard_t *kbd)
532{
533	int error;
534	int s;
535
536	error = 0;
537	empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
538	s = spltty();
539	if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
540		error = EIO;
541	else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
542		error = EIO;
543	splx(s);
544
545	return error;
546}
547
548/*
549 * Enable the access to the device; until this function is called,
550 * the client cannot read from the keyboard.
551 */
552static int
553atkbd_enable(keyboard_t *kbd)
554{
555	int s;
556
557	s = spltty();
558	KBD_ACTIVATE(kbd);
559	splx(s);
560	return 0;
561}
562
563/* disallow the access to the device */
564static int
565atkbd_disable(keyboard_t *kbd)
566{
567	int s;
568
569	s = spltty();
570	KBD_DEACTIVATE(kbd);
571	splx(s);
572	return 0;
573}
574
575/* read one byte from the keyboard if it's allowed */
576static int
577atkbd_read(keyboard_t *kbd, int wait)
578{
579	int c;
580
581	if (wait)
582		c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
583	else
584		c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
585	if (c != -1)
586		++kbd->kb_count;
587	return (KBD_IS_ACTIVE(kbd) ? c : -1);
588}
589
590/* check if data is waiting */
591static int
592atkbd_check(keyboard_t *kbd)
593{
594	if (!KBD_IS_ACTIVE(kbd))
595		return FALSE;
596	return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
597}
598
599/* read char from the keyboard */
600static u_int
601atkbd_read_char(keyboard_t *kbd, int wait)
602{
603	atkbd_state_t *state;
604	u_int action;
605	int scancode;
606	int keycode;
607
608	state = (atkbd_state_t *)kbd->kb_data;
609next_code:
610	/* do we have a composed char to return? */
611	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
612		action = state->ks_composed_char;
613		state->ks_composed_char = 0;
614		if (action > UCHAR_MAX)
615			return ERRKEY;
616		return action;
617	}
618
619	/* see if there is something in the keyboard port */
620	if (wait) {
621		do {
622			scancode = read_kbd_data(state->kbdc);
623		} while (scancode == -1);
624	} else {
625		scancode = read_kbd_data_no_wait(state->kbdc);
626		if (scancode == -1)
627			return NOKEY;
628	}
629	++kbd->kb_count;
630
631#if KBDIO_DEBUG >= 10
632	printf("atkbd_read_char(): scancode:0x%x\n", scancode);
633#endif
634
635	/* return the byte as is for the K_RAW mode */
636	if (state->ks_mode == K_RAW)
637		return scancode;
638
639	/* translate the scan code into a keycode */
640	keycode = scancode & 0x7F;
641	switch (state->ks_prefix) {
642	case 0x00:	/* normal scancode */
643		switch(scancode) {
644		case 0xB8:	/* left alt (compose key) released */
645			if (state->ks_flags & COMPOSE) {
646				state->ks_flags &= ~COMPOSE;
647				if (state->ks_composed_char > UCHAR_MAX)
648					state->ks_composed_char = 0;
649			}
650			break;
651		case 0x38:	/* left alt (compose key) pressed */
652			if (!(state->ks_flags & COMPOSE)) {
653				state->ks_flags |= COMPOSE;
654				state->ks_composed_char = 0;
655			}
656			break;
657		case 0xE0:
658		case 0xE1:
659			state->ks_prefix = scancode;
660			goto next_code;
661		}
662		break;
663	case 0xE0:		/* 0xE0 prefix */
664		state->ks_prefix = 0;
665		switch (keycode) {
666		case 0x1C:	/* right enter key */
667			keycode = 0x59;
668			break;
669		case 0x1D:	/* right ctrl key */
670			keycode = 0x5A;
671			break;
672		case 0x35:	/* keypad divide key */
673			keycode = 0x5B;
674			break;
675		case 0x37:	/* print scrn key */
676			keycode = 0x5C;
677			break;
678		case 0x38:	/* right alt key (alt gr) */
679			keycode = 0x5D;
680			break;
681		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
682			keycode = 0x68;
683			break;
684		case 0x47:	/* grey home key */
685			keycode = 0x5E;
686			break;
687		case 0x48:	/* grey up arrow key */
688			keycode = 0x5F;
689			break;
690		case 0x49:	/* grey page up key */
691			keycode = 0x60;
692			break;
693		case 0x4B:	/* grey left arrow key */
694			keycode = 0x61;
695			break;
696		case 0x4D:	/* grey right arrow key */
697			keycode = 0x62;
698			break;
699		case 0x4F:	/* grey end key */
700			keycode = 0x63;
701			break;
702		case 0x50:	/* grey down arrow key */
703			keycode = 0x64;
704			break;
705		case 0x51:	/* grey page down key */
706			keycode = 0x65;
707			break;
708		case 0x52:	/* grey insert key */
709			keycode = 0x66;
710			break;
711		case 0x53:	/* grey delete key */
712			keycode = 0x67;
713			break;
714			/* the following 3 are only used on the MS "Natural" keyboard */
715		case 0x5b:	/* left Window key */
716			keycode = 0x69;
717			break;
718		case 0x5c:	/* right Window key */
719			keycode = 0x6a;
720			break;
721		case 0x5d:	/* menu key */
722			keycode = 0x6b;
723			break;
724		case 0x5e:	/* power key */
725			keycode = 0x6d;
726			break;
727		case 0x5f:	/* sleep key */
728			keycode = 0x6e;
729			break;
730		case 0x63:	/* wake key */
731			keycode = 0x6f;
732			break;
733		default:	/* ignore everything else */
734			goto next_code;
735		}
736		break;
737   	case 0xE1:	/* 0xE1 prefix */
738		/*
739		 * The pause/break key on the 101 keyboard produces:
740		 * E1-1D-45 E1-9D-C5
741		 * Ctrl-pause/break produces:
742		 * E0-46 E0-C6 (See above.)
743		 */
744		state->ks_prefix = 0;
745		if (keycode == 0x1D)
746			state->ks_prefix = 0x1D;
747		goto next_code;
748		/* NOT REACHED */
749   	case 0x1D:	/* pause / break */
750		state->ks_prefix = 0;
751		if (keycode != 0x45)
752			goto next_code;
753		keycode = 0x68;
754		break;
755	}
756
757	if (kbd->kb_type == KB_84) {
758		switch (keycode) {
759		case 0x37:	/* *(numpad)/print screen */
760			if (state->ks_flags & SHIFTS)
761				keycode = 0x5c;	/* print screen */
762			break;
763		case 0x45:	/* num lock/pause */
764			if (state->ks_flags & CTLS)
765				keycode = 0x68;	/* pause */
766			break;
767		case 0x46:	/* scroll lock/break */
768			if (state->ks_flags & CTLS)
769				keycode = 0x6c;	/* break */
770			break;
771		}
772	} else if (kbd->kb_type == KB_101) {
773		switch (keycode) {
774		case 0x5c:	/* print screen */
775			if (state->ks_flags & ALTS)
776				keycode = 0x54;	/* sysrq */
777			break;
778		case 0x68:	/* pause/break */
779			if (state->ks_flags & CTLS)
780				keycode = 0x6c;	/* break */
781			break;
782		}
783	}
784
785	/* return the key code in the K_CODE mode */
786	if (state->ks_mode == K_CODE)
787		return (keycode | (scancode & 0x80));
788
789	/* compose a character code */
790	if (state->ks_flags & COMPOSE) {
791		switch (keycode | (scancode & 0x80)) {
792		/* key pressed, process it */
793		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
794			state->ks_composed_char *= 10;
795			state->ks_composed_char += keycode - 0x40;
796			if (state->ks_composed_char > UCHAR_MAX)
797				return ERRKEY;
798			goto next_code;
799		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
800			state->ks_composed_char *= 10;
801			state->ks_composed_char += keycode - 0x47;
802			if (state->ks_composed_char > UCHAR_MAX)
803				return ERRKEY;
804			goto next_code;
805		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
806			state->ks_composed_char *= 10;
807			state->ks_composed_char += keycode - 0x4E;
808			if (state->ks_composed_char > UCHAR_MAX)
809				return ERRKEY;
810			goto next_code;
811		case 0x52:				/* keypad 0 */
812			state->ks_composed_char *= 10;
813			if (state->ks_composed_char > UCHAR_MAX)
814				return ERRKEY;
815			goto next_code;
816
817		/* key released, no interest here */
818		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
819		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
820		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
821		case 0xD2:				/* keypad 0 */
822			goto next_code;
823
824		case 0x38:				/* left alt key */
825			break;
826
827		default:
828			if (state->ks_composed_char > 0) {
829				state->ks_flags &= ~COMPOSE;
830				state->ks_composed_char = 0;
831				return ERRKEY;
832			}
833			break;
834		}
835	}
836
837	/* keycode to key action */
838	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
839				  &state->ks_state, &state->ks_accents);
840	if (action == NOKEY)
841		goto next_code;
842	else
843		return action;
844}
845
846/* check if char is waiting */
847static int
848atkbd_check_char(keyboard_t *kbd)
849{
850	atkbd_state_t *state;
851
852	if (!KBD_IS_ACTIVE(kbd))
853		return FALSE;
854	state = (atkbd_state_t *)kbd->kb_data;
855	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
856		return TRUE;
857	return kbdc_data_ready(state->kbdc);
858}
859
860/* some useful control functions */
861static int
862atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
863{
864	/* translate LED_XXX bits into the device specific bits */
865	static u_char ledmap[8] = {
866		0, 4, 2, 6, 1, 5, 3, 7,
867	};
868	atkbd_state_t *state = kbd->kb_data;
869	int error;
870	int s;
871	int i;
872#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
873    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
874	int ival;
875#endif
876
877	s = spltty();
878	switch (cmd) {
879
880	case KDGKBMODE:		/* get keyboard mode */
881		*(int *)arg = state->ks_mode;
882		break;
883#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
884    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
885	case _IO('K', 7):
886		ival = IOCPARM_IVAL(arg);
887		arg = (caddr_t)&ival;
888		/* FALLTHROUGH */
889#endif
890	case KDSKBMODE:		/* set keyboard mode */
891		switch (*(int *)arg) {
892		case K_XLATE:
893			if (state->ks_mode != K_XLATE) {
894				/* make lock key state and LED state match */
895				state->ks_state &= ~LOCK_MASK;
896				state->ks_state |= KBD_LED_VAL(kbd);
897			}
898			/* FALLTHROUGH */
899		case K_RAW:
900		case K_CODE:
901			if (state->ks_mode != *(int *)arg) {
902				atkbd_clear_state(kbd);
903				state->ks_mode = *(int *)arg;
904			}
905			break;
906		default:
907			splx(s);
908			return EINVAL;
909		}
910		break;
911
912	case KDGETLED:		/* get keyboard LED */
913		*(int *)arg = KBD_LED_VAL(kbd);
914		break;
915#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
916    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
917	case _IO('K', 66):
918		ival = IOCPARM_IVAL(arg);
919		arg = (caddr_t)&ival;
920		/* FALLTHROUGH */
921#endif
922	case KDSETLED:		/* set keyboard LED */
923		/* NOTE: lock key state in ks_state won't be changed */
924		if (*(int *)arg & ~LOCK_MASK) {
925			splx(s);
926			return EINVAL;
927		}
928		i = *(int *)arg;
929		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
930		if (state->ks_mode == K_XLATE &&
931		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
932			if (i & ALKED)
933				i |= CLKED;
934			else
935				i &= ~CLKED;
936		}
937		if (KBD_HAS_DEVICE(kbd)) {
938			error = write_kbd(state->kbdc, KBDC_SET_LEDS,
939					  ledmap[i & LED_MASK]);
940			if (error) {
941				splx(s);
942				return error;
943			}
944		}
945		KBD_LED_VAL(kbd) = *(int *)arg;
946		break;
947
948	case KDGKBSTATE:	/* get lock key state */
949		*(int *)arg = state->ks_state & LOCK_MASK;
950		break;
951#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
952    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
953	case _IO('K', 20):
954		ival = IOCPARM_IVAL(arg);
955		arg = (caddr_t)&ival;
956		/* FALLTHROUGH */
957#endif
958	case KDSKBSTATE:	/* set lock key state */
959		if (*(int *)arg & ~LOCK_MASK) {
960			splx(s);
961			return EINVAL;
962		}
963		state->ks_state &= ~LOCK_MASK;
964		state->ks_state |= *(int *)arg;
965		splx(s);
966		/* set LEDs and quit */
967		return atkbd_ioctl(kbd, KDSETLED, arg);
968
969	case KDSETREPEAT:	/* set keyboard repeat rate (new interface) */
970		splx(s);
971		if (!KBD_HAS_DEVICE(kbd))
972			return 0;
973		i = typematic(((int *)arg)[0], ((int *)arg)[1]);
974		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
975		if (error == 0) {
976			kbd->kb_delay1 = typematic_delay(i);
977			kbd->kb_delay2 = typematic_rate(i);
978		}
979		return error;
980
981#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
982    defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
983	case _IO('K', 67):
984		ival = IOCPARM_IVAL(arg);
985		arg = (caddr_t)&ival;
986		/* FALLTHROUGH */
987#endif
988	case KDSETRAD:		/* set keyboard repeat rate (old interface) */
989		splx(s);
990		if (!KBD_HAS_DEVICE(kbd))
991			return 0;
992		error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
993		if (error == 0) {
994			kbd->kb_delay1 = typematic_delay(*(int *)arg);
995			kbd->kb_delay2 = typematic_rate(*(int *)arg);
996		}
997		return error;
998
999	case PIO_KEYMAP:	/* set keyboard translation table */
1000	case OPIO_KEYMAP:	/* set keyboard translation table (compat) */
1001	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
1002	case PIO_DEADKEYMAP:	/* set accent key translation table */
1003		state->ks_accents = 0;
1004		/* FALLTHROUGH */
1005	default:
1006		splx(s);
1007		return genkbd_commonioctl(kbd, cmd, arg);
1008	}
1009
1010	splx(s);
1011	return 0;
1012}
1013
1014/* lock the access to the keyboard */
1015static int
1016atkbd_lock(keyboard_t *kbd, int lock)
1017{
1018	return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
1019}
1020
1021/* clear the internal state of the keyboard */
1022static void
1023atkbd_clear_state(keyboard_t *kbd)
1024{
1025	atkbd_state_t *state;
1026
1027	state = (atkbd_state_t *)kbd->kb_data;
1028	state->ks_flags = 0;
1029	state->ks_polling = 0;
1030	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
1031	state->ks_accents = 0;
1032	state->ks_composed_char = 0;
1033#if 0
1034	state->ks_prefix = 0; /* XXX */
1035#endif
1036}
1037
1038/* save the internal state */
1039static int
1040atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1041{
1042	if (len == 0)
1043		return sizeof(atkbd_state_t);
1044	if (len < sizeof(atkbd_state_t))
1045		return -1;
1046	bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
1047	return 0;
1048}
1049
1050/* set the internal state */
1051static int
1052atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1053{
1054	if (len < sizeof(atkbd_state_t))
1055		return ENOMEM;
1056	if (((atkbd_state_t *)kbd->kb_data)->kbdc
1057		!= ((atkbd_state_t *)buf)->kbdc)
1058		return ENOMEM;
1059	bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1060	return 0;
1061}
1062
1063static int
1064atkbd_poll(keyboard_t *kbd, int on)
1065{
1066	atkbd_state_t *state;
1067	int s;
1068
1069	state = (atkbd_state_t *)kbd->kb_data;
1070	s = spltty();
1071	if (on)
1072		++state->ks_polling;
1073	else
1074		--state->ks_polling;
1075	splx(s);
1076	return 0;
1077}
1078
1079static void
1080atkbd_shutdown_final(void *v)
1081{
1082#ifdef __sparc64__
1083	keyboard_t *kbd = v;
1084	KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc;
1085
1086	/*
1087	 * Turn off the translation in preparation for handing the keyboard
1088	 * over to the OFW as the OBP driver doesn't use translation and
1089	 * also doesn't disable it itself resulting in a broken keymap at
1090	 * the boot prompt. Also disable the aux port and the interrupts as
1091	 * the OBP driver doesn't use them, i.e. polls the keyboard. Not
1092	 * disabling the interrupts doesn't cause real problems but the
1093	 * responsiveness is a bit better when they are turned off.
1094	 */
1095	send_kbd_command(kbdc, KBDC_DISABLE_KBD);
1096	set_controller_command_byte(kbdc,
1097	    KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION,
1098	    KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT);
1099	send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1100#endif
1101}
1102
1103static int
1104atkbd_reset(KBDC kbdc, int flags, int c)
1105{
1106	/* reset keyboard hardware */
1107	if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1108		/*
1109		 * KEYBOARD ERROR
1110		 * Keyboard reset may fail either because the keyboard
1111		 * doen't exist, or because the keyboard doesn't pass
1112		 * the self-test, or the keyboard controller on the
1113		 * motherboard and the keyboard somehow fail to shake hands.
1114		 * It is just possible, particularly in the last case,
1115		 * that the keyboard controller may be left in a hung state.
1116		 * test_controller() and test_kbd_port() appear to bring
1117		 * the keyboard controller back (I don't know why and how,
1118		 * though.)
1119		 */
1120		empty_both_buffers(kbdc, 10);
1121		test_controller(kbdc);
1122		test_kbd_port(kbdc);
1123		/*
1124		 * We could disable the keyboard port and interrupt... but,
1125		 * the keyboard may still exist (see above).
1126		 */
1127		set_controller_command_byte(kbdc,
1128		    ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1129		if (bootverbose)
1130			printf("atkbd: failed to reset the keyboard.\n");
1131		return (EIO);
1132	}
1133	return (0);
1134}
1135
1136/* local functions */
1137
1138static int
1139set_typematic(keyboard_t *kbd)
1140{
1141	int val, error;
1142	atkbd_state_t *state = kbd->kb_data;
1143
1144	val = typematic(DEFAULT_DELAY, DEFAULT_RATE);
1145	error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val);
1146	if (error == 0) {
1147		kbd->kb_delay1 = typematic_delay(val);
1148		kbd->kb_delay2 = typematic_rate(val);
1149	}
1150
1151	return (error);
1152}
1153
1154static int
1155setup_kbd_port(KBDC kbdc, int port, int intr)
1156{
1157	if (!set_controller_command_byte(kbdc,
1158		KBD_KBD_CONTROL_BITS,
1159		((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1160		    | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1161		return 1;
1162	return 0;
1163}
1164
1165static int
1166get_kbd_echo(KBDC kbdc)
1167{
1168	/* enable the keyboard port, but disable the keyboard intr. */
1169	if (setup_kbd_port(kbdc, TRUE, FALSE))
1170		/* CONTROLLER ERROR: there is very little we can do... */
1171		return ENXIO;
1172
1173	/* see if something is present */
1174	write_kbd_command(kbdc, KBDC_ECHO);
1175	if (read_kbd_data(kbdc) != KBD_ECHO) {
1176		empty_both_buffers(kbdc, 10);
1177		test_controller(kbdc);
1178		test_kbd_port(kbdc);
1179		return ENXIO;
1180	}
1181
1182	/* enable the keyboard port and intr. */
1183	if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1184		/*
1185		 * CONTROLLER ERROR
1186		 * This is serious; the keyboard intr is left disabled!
1187		 */
1188		return ENXIO;
1189	}
1190
1191	return 0;
1192}
1193
1194static int
1195probe_keyboard(KBDC kbdc, int flags)
1196{
1197	/*
1198	 * Don't try to print anything in this function.  The low-level
1199	 * console may not have been initialized yet...
1200	 */
1201	int err;
1202	int c;
1203	int m;
1204
1205	if (!kbdc_lock(kbdc, TRUE)) {
1206		/* driver error? */
1207		return ENXIO;
1208	}
1209
1210	/* temporarily block data transmission from the keyboard */
1211	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1212
1213	/* flush any noise in the buffer */
1214	empty_both_buffers(kbdc, 100);
1215
1216	/* save the current keyboard controller command byte */
1217	m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1218	c = get_controller_command_byte(kbdc);
1219	if (c == -1) {
1220		/* CONTROLLER ERROR */
1221		kbdc_set_device_mask(kbdc, m);
1222		kbdc_lock(kbdc, FALSE);
1223		return ENXIO;
1224	}
1225
1226	/*
1227	 * The keyboard may have been screwed up by the boot block.
1228	 * We may just be able to recover from error by testing the controller
1229	 * and the keyboard port. The controller command byte needs to be
1230	 * saved before this recovery operation, as some controllers seem
1231	 * to set the command byte to particular values.
1232	 */
1233	test_controller(kbdc);
1234	if (!(flags & KB_CONF_NO_PROBE_TEST))
1235		test_kbd_port(kbdc);
1236
1237	err = get_kbd_echo(kbdc);
1238
1239	/*
1240	 * Even if the keyboard doesn't seem to be present (err != 0),
1241	 * we shall enable the keyboard port and interrupt so that
1242	 * the driver will be operable when the keyboard is attached
1243	 * to the system later.  It is NOT recommended to hot-plug
1244	 * the AT keyboard, but many people do so...
1245	 */
1246	kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1247	setup_kbd_port(kbdc, TRUE, TRUE);
1248#if 0
1249	if (err == 0) {
1250		kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1251	} else {
1252		/* try to restore the command byte as before */
1253		set_controller_command_byte(kbdc,
1254		    ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1255		kbdc_set_device_mask(kbdc, m);
1256	}
1257#endif
1258
1259	kbdc_lock(kbdc, FALSE);
1260	return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err);
1261}
1262
1263static int
1264init_keyboard(KBDC kbdc, int *type, int flags)
1265{
1266	int codeset;
1267	int id;
1268	int c;
1269
1270	if (!kbdc_lock(kbdc, TRUE)) {
1271		/* driver error? */
1272		return EIO;
1273	}
1274
1275	/* temporarily block data transmission from the keyboard */
1276	write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1277
1278	/* save the current controller command byte */
1279	empty_both_buffers(kbdc, 200);
1280	c = get_controller_command_byte(kbdc);
1281	if (c == -1) {
1282		/* CONTROLLER ERROR */
1283		kbdc_lock(kbdc, FALSE);
1284		printf("atkbd: unable to get the current command byte value.\n");
1285		return EIO;
1286	}
1287	if (bootverbose)
1288		printf("atkbd: the current kbd controller command byte %04x\n",
1289		   c);
1290#if 0
1291	/* override the keyboard lock switch */
1292	c |= KBD_OVERRIDE_KBD_LOCK;
1293#endif
1294
1295	/* enable the keyboard port, but disable the keyboard intr. */
1296	if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1297		/* CONTROLLER ERROR: there is very little we can do... */
1298		printf("atkbd: unable to set the command byte.\n");
1299		kbdc_lock(kbdc, FALSE);
1300		return EIO;
1301	}
1302
1303	if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1304	    atkbd_reset(kbdc, flags, c)) {
1305		kbdc_lock(kbdc, FALSE);
1306		return EIO;
1307	}
1308
1309	/*
1310	 * Check if we have an XT keyboard before we attempt to reset it.
1311	 * The procedure assumes that the keyboard and the controller have
1312	 * been set up properly by BIOS and have not been messed up
1313	 * during the boot process.
1314	 */
1315	codeset = -1;
1316	if (flags & KB_CONF_ALT_SCANCODESET)
1317		/* the user says there is a XT keyboard */
1318		codeset = 1;
1319#ifdef KBD_DETECT_XT_KEYBOARD
1320	else if ((c & KBD_TRANSLATION) == 0) {
1321		/* SET_SCANCODE_SET is not always supported; ignore error */
1322		if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1323			== KBD_ACK)
1324			codeset = read_kbd_data(kbdc);
1325	}
1326	if (bootverbose)
1327		printf("atkbd: scancode set %d\n", codeset);
1328#endif /* KBD_DETECT_XT_KEYBOARD */
1329
1330	*type = KB_OTHER;
1331	id = get_kbd_id(kbdc);
1332	switch(id) {
1333	case 0x41ab:	/* 101/102/... Enhanced */
1334	case 0x83ab:	/* ditto */
1335	case 0x54ab:	/* SpaceSaver */
1336	case 0x84ab:	/* ditto */
1337#if 0
1338	case 0x90ab:	/* 'G' */
1339	case 0x91ab:	/* 'P' */
1340	case 0x92ab:	/* 'A' */
1341#endif
1342		*type = KB_101;
1343		break;
1344	case -1:	/* AT 84 keyboard doesn't return ID */
1345		*type = KB_84;
1346		break;
1347	default:
1348		break;
1349	}
1350	if (bootverbose)
1351		printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1352
1353	if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1354	    atkbd_reset(kbdc, flags, c)) {
1355		kbdc_lock(kbdc, FALSE);
1356		return EIO;
1357	}
1358
1359	/*
1360	 * Allow us to set the XT_KEYBD flag so that keyboards
1361	 * such as those on the IBM ThinkPad laptop computers can be used
1362	 * with the standard console driver.
1363	 */
1364	if (codeset == 1) {
1365		if (send_kbd_command_and_data(kbdc,
1366			KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1367			/* XT kbd doesn't need scan code translation */
1368			c &= ~KBD_TRANSLATION;
1369		} else {
1370			/*
1371			 * KEYBOARD ERROR
1372			 * The XT kbd isn't usable unless the proper scan
1373			 * code set is selected.
1374			 */
1375			set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1376			    ? 0xff : KBD_KBD_CONTROL_BITS, c);
1377			kbdc_lock(kbdc, FALSE);
1378			printf("atkbd: unable to set the XT keyboard mode.\n");
1379			return EIO;
1380		}
1381	}
1382
1383#if defined(__sparc64__)
1384	if (send_kbd_command_and_data(
1385		kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1386		printf("atkbd: can't set translation.\n");
1387	}
1388	c |= KBD_TRANSLATION;
1389#endif
1390
1391	/*
1392	 * Some keyboards require a SETLEDS command to be sent after
1393	 * the reset command before they will send keystrokes to us
1394	 */
1395	if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) &&
1396	    send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) {
1397		printf("atkbd: setleds failed\n");
1398	}
1399	if (!ALLOW_DISABLE_KBD(kbdc))
1400	    send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1401
1402	/* enable the keyboard port and intr. */
1403	if (!set_controller_command_byte(kbdc,
1404		KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1405		(c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1406		    | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1407		/*
1408		 * CONTROLLER ERROR
1409		 * This is serious; we are left with the disabled
1410		 * keyboard intr.
1411		 */
1412		set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1413		    ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1414			KBD_OVERRIDE_KBD_LOCK), c);
1415		kbdc_lock(kbdc, FALSE);
1416		printf("atkbd: unable to enable the keyboard port and intr.\n");
1417		return EIO;
1418	}
1419
1420	kbdc_lock(kbdc, FALSE);
1421	return 0;
1422}
1423
1424static int
1425write_kbd(KBDC kbdc, int command, int data)
1426{
1427	int s;
1428
1429	/* prevent the timeout routine from polling the keyboard */
1430	if (!kbdc_lock(kbdc, TRUE))
1431		return EBUSY;
1432
1433	/* disable the keyboard and mouse interrupt */
1434	s = spltty();
1435#if 0
1436	c = get_controller_command_byte(kbdc);
1437	if ((c == -1)
1438	    || !set_controller_command_byte(kbdc,
1439		kbdc_get_device_mask(kbdc),
1440		KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1441		| KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1442		/* CONTROLLER ERROR */
1443		kbdc_lock(kbdc, FALSE);
1444		splx(s);
1445		return EIO;
1446	}
1447	/*
1448	 * Now that the keyboard controller is told not to generate
1449	 * the keyboard and mouse interrupts, call `splx()' to allow
1450	 * the other tty interrupts. The clock interrupt may also occur,
1451	 * but the timeout routine (`scrn_timer()') will be blocked
1452	 * by the lock flag set via `kbdc_lock()'
1453	 */
1454	splx(s);
1455#endif
1456	if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1457		send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1458#if 0
1459	/* restore the interrupts */
1460	if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc),
1461	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1462		/* CONTROLLER ERROR */
1463	}
1464#else
1465	splx(s);
1466#endif
1467	kbdc_lock(kbdc, FALSE);
1468
1469	return 0;
1470}
1471
1472static int
1473get_kbd_id(KBDC kbdc)
1474{
1475	int id1, id2;
1476
1477	empty_both_buffers(kbdc, 10);
1478	id1 = id2 = -1;
1479	if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1480		return -1;
1481
1482	DELAY(10000); 	/* 10 msec delay */
1483	id1 = read_kbd_data(kbdc);
1484	if (id1 != -1)
1485		id2 = read_kbd_data(kbdc);
1486
1487	if ((id1 == -1) || (id2 == -1)) {
1488		empty_both_buffers(kbdc, 10);
1489		test_controller(kbdc);
1490		test_kbd_port(kbdc);
1491		return -1;
1492	}
1493	return ((id2 << 8) | id1);
1494}
1495
1496static int delays[] = { 250, 500, 750, 1000 };
1497static int rates[] = {  34,  38,  42,  46,  50,  55,  59,  63,
1498			68,  76,  84,  92, 100, 110, 118, 126,
1499		       136, 152, 168, 184, 200, 220, 236, 252,
1500		       272, 304, 336, 368, 400, 440, 472, 504 };
1501
1502static int
1503typematic_delay(int i)
1504{
1505	return delays[(i >> 5) & 3];
1506}
1507
1508static int
1509typematic_rate(int i)
1510{
1511	return rates[i & 0x1f];
1512}
1513
1514static int
1515typematic(int delay, int rate)
1516{
1517	int value;
1518	int i;
1519
1520	for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1521		if (delay >= delays[i])
1522			break;
1523	}
1524	value = i << 5;
1525	for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1526		if (rate >= rates[i])
1527			break;
1528	}
1529	value |= i;
1530	return value;
1531}
1532