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