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