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