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