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