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