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