atkbd.c revision 54382
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: head/sys/dev/atkbdc/atkbd.c 54382 1999-12-10 04:31:33Z yokota $
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
51static timeout_t	atkbd_timeout;
52
53int
54atkbd_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
72int
73atkbd_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
113static void
114atkbd_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
160typedef 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 */
176static int		atkbd_configure(int flags);
177static kbd_probe_t	atkbd_probe;
178static kbd_init_t	atkbd_init;
179static kbd_term_t	atkbd_term;
180static kbd_intr_t	atkbd_intr;
181static kbd_test_if_t	atkbd_test_if;
182static kbd_enable_t	atkbd_enable;
183static kbd_disable_t	atkbd_disable;
184static kbd_read_t	atkbd_read;
185static kbd_check_t	atkbd_check;
186static kbd_read_char_t	atkbd_read_char;
187static kbd_check_char_t	atkbd_check_char;
188static kbd_ioctl_t	atkbd_ioctl;
189static kbd_lock_t	atkbd_lock;
190static kbd_clear_state_t atkbd_clear_state;
191static kbd_get_state_t	atkbd_get_state;
192static kbd_set_state_t	atkbd_set_state;
193static kbd_poll_mode_t	atkbd_poll;
194
195keyboard_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
217KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
218
219/* local functions */
220static int		setup_kbd_port(KBDC kbdc, int port, int intr);
221static int		get_kbd_echo(KBDC kbdc);
222static int		probe_keyboard(KBDC kbdc, int flags);
223static int		init_keyboard(KBDC kbdc, int *type, int flags);
224static int		write_kbd(KBDC kbdc, int command, int data);
225static int		get_kbd_id(KBDC kbdc);
226static 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 */
238static keyboard_t	default_kbd;
239static atkbd_state_t	default_kbd_state;
240static keymap_t		default_keymap;
241static accentmap_t	default_accentmap;
242static 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 */
253static int
254atkbd_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 */
295static int
296atkbd_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 */
318static int
319atkbd_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 */
422static int
423atkbd_term(keyboard_t *kbd)
424{
425	kbd_unregister(kbd);
426	return 0;
427}
428
429/* keyboard interrupt routine */
430static int
431atkbd_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 */
462static int
463atkbd_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 */
484static int
485atkbd_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 */
496static int
497atkbd_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 */
508static int
509atkbd_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	if (c != -1)
518		++kbd->kb_count;
519	return (KBD_IS_ACTIVE(kbd) ? c : -1);
520}
521
522/* check if data is waiting */
523static int
524atkbd_check(keyboard_t *kbd)
525{
526	if (!KBD_IS_ACTIVE(kbd))
527		return FALSE;
528	return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
529}
530
531/* read char from the keyboard */
532static u_int
533atkbd_read_char(keyboard_t *kbd, int wait)
534{
535	atkbd_state_t *state;
536	u_int action;
537	int scancode;
538	int keycode;
539
540	state = (atkbd_state_t *)kbd->kb_data;
541next_code:
542	/* do we have a composed char to return? */
543	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
544		action = state->ks_composed_char;
545		state->ks_composed_char = 0;
546		if (action > UCHAR_MAX)
547			return ERRKEY;
548		return action;
549	}
550
551	/* see if there is something in the keyboard port */
552	if (wait) {
553		do {
554			scancode = read_kbd_data(state->kbdc);
555		} while (scancode == -1);
556	} else {
557		scancode = read_kbd_data_no_wait(state->kbdc);
558		if (scancode == -1)
559			return NOKEY;
560	}
561	++kbd->kb_count;
562
563#if KBDIO_DEBUG >= 10
564	printf("atkbd_read_char(): scancode:0x%x\n", scancode);
565#endif
566
567	/* return the byte as is for the K_RAW mode */
568	if (state->ks_mode == K_RAW)
569		return scancode;
570
571	/* translate the scan code into a keycode */
572	keycode = scancode & 0x7F;
573	switch (state->ks_prefix) {
574	case 0x00:	/* normal scancode */
575		switch(scancode) {
576		case 0xB8:	/* left alt (compose key) released */
577			if (state->ks_flags & COMPOSE) {
578				state->ks_flags &= ~COMPOSE;
579				if (state->ks_composed_char > UCHAR_MAX)
580					state->ks_composed_char = 0;
581			}
582			break;
583		case 0x38:	/* left alt (compose key) pressed */
584			if (!(state->ks_flags & COMPOSE)) {
585				state->ks_flags |= COMPOSE;
586				state->ks_composed_char = 0;
587			}
588			break;
589		case 0xE0:
590		case 0xE1:
591			state->ks_prefix = scancode;
592			goto next_code;
593		}
594		break;
595	case 0xE0:      /* 0xE0 prefix */
596		state->ks_prefix = 0;
597		switch (keycode) {
598		case 0x1C:	/* right enter key */
599			keycode = 0x59;
600			break;
601		case 0x1D:	/* right ctrl key */
602			keycode = 0x5A;
603			break;
604		case 0x35:	/* keypad divide key */
605	    		keycode = 0x5B;
606	    		break;
607		case 0x37:	/* print scrn key */
608	    		keycode = 0x5C;
609	    		break;
610		case 0x38:	/* right alt key (alt gr) */
611	    		keycode = 0x5D;
612	    		break;
613		case 0x46:	/* ctrl-pause/break on AT 101 (see below) */
614			keycode = 0x68;
615	    		break;
616		case 0x47:	/* grey home key */
617	    		keycode = 0x5E;
618	    		break;
619		case 0x48:	/* grey up arrow key */
620	    		keycode = 0x5F;
621	    		break;
622		case 0x49:	/* grey page up key */
623	    		keycode = 0x60;
624	    		break;
625		case 0x4B:	/* grey left arrow key */
626	    		keycode = 0x61;
627	    		break;
628		case 0x4D:	/* grey right arrow key */
629	    		keycode = 0x62;
630	    		break;
631		case 0x4F:	/* grey end key */
632	    		keycode = 0x63;
633	    		break;
634		case 0x50:	/* grey down arrow key */
635	    		keycode = 0x64;
636	    		break;
637		case 0x51:	/* grey page down key */
638	    		keycode = 0x65;
639	    		break;
640		case 0x52:	/* grey insert key */
641	    		keycode = 0x66;
642	    		break;
643		case 0x53:	/* grey delete key */
644	    		keycode = 0x67;
645	    		break;
646		/* the following 3 are only used on the MS "Natural" keyboard */
647		case 0x5b:	/* left Window key */
648	    		keycode = 0x69;
649	    		break;
650		case 0x5c:	/* right Window key */
651	    		keycode = 0x6a;
652	    		break;
653		case 0x5d:	/* menu key */
654	    		keycode = 0x6b;
655	    		break;
656		default:	/* ignore everything else */
657	    		goto next_code;
658		}
659		break;
660    	case 0xE1:	/* 0xE1 prefix */
661		/*
662		 * The pause/break key on the 101 keyboard produces:
663		 * E1-1D-45 E1-9D-C5
664		 * Ctrl-pause/break produces:
665		 * E0-46 E0-C6 (See above.)
666		 */
667		state->ks_prefix = 0;
668		if (keycode == 0x1D)
669	    		state->ks_prefix = 0x1D;
670		goto next_code;
671		/* NOT REACHED */
672    	case 0x1D:	/* pause / break */
673		state->ks_prefix = 0;
674		if (keycode != 0x45)
675			goto next_code;
676		keycode = 0x68;
677		break;
678	}
679
680	if (kbd->kb_type == KB_84) {
681		switch (keycode) {
682		case 0x37:	/* *(numpad)/print screen */
683			if (state->ks_flags & SHIFTS)
684	    			keycode = 0x5c;	/* print screen */
685			break;
686		case 0x45:	/* num lock/pause */
687			if (state->ks_flags & CTLS)
688				keycode = 0x68;	/* pause */
689			break;
690		case 0x46:	/* scroll lock/break */
691			if (state->ks_flags & CTLS)
692				keycode = 0x6c;	/* break */
693			break;
694		}
695	} else if (kbd->kb_type == KB_101) {
696		switch (keycode) {
697		case 0x5c:	/* print screen */
698			if (state->ks_flags & ALTS)
699				keycode = 0x54;	/* sysrq */
700			break;
701		case 0x68:	/* pause/break */
702			if (state->ks_flags & CTLS)
703				keycode = 0x6c;	/* break */
704			break;
705		}
706	}
707
708	/* return the key code in the K_CODE mode */
709	if (state->ks_mode == K_CODE)
710		return (keycode | (scancode & 0x80));
711
712	/* compose a character code */
713	if (state->ks_flags & COMPOSE) {
714		switch (keycode | (scancode & 0x80)) {
715		/* key pressed, process it */
716		case 0x47: case 0x48: case 0x49:	/* keypad 7,8,9 */
717			state->ks_composed_char *= 10;
718			state->ks_composed_char += keycode - 0x40;
719			kbd->kb_prev_key = keycode | (scancode & 0x80);
720			if (state->ks_composed_char > UCHAR_MAX)
721				return ERRKEY;
722			goto next_code;
723		case 0x4B: case 0x4C: case 0x4D:	/* keypad 4,5,6 */
724			state->ks_composed_char *= 10;
725			state->ks_composed_char += keycode - 0x47;
726			kbd->kb_prev_key = keycode | (scancode & 0x80);
727			if (state->ks_composed_char > UCHAR_MAX)
728				return ERRKEY;
729			goto next_code;
730		case 0x4F: case 0x50: case 0x51:	/* keypad 1,2,3 */
731			state->ks_composed_char *= 10;
732			state->ks_composed_char += keycode - 0x4E;
733			kbd->kb_prev_key = keycode | (scancode & 0x80);
734			if (state->ks_composed_char > UCHAR_MAX)
735				return ERRKEY;
736			goto next_code;
737		case 0x52:				/* keypad 0 */
738			state->ks_composed_char *= 10;
739			kbd->kb_prev_key = keycode | (scancode & 0x80);
740			if (state->ks_composed_char > UCHAR_MAX)
741				return ERRKEY;
742			goto next_code;
743
744		/* key released, no interest here */
745		case 0xC7: case 0xC8: case 0xC9:	/* keypad 7,8,9 */
746		case 0xCB: case 0xCC: case 0xCD:	/* keypad 4,5,6 */
747		case 0xCF: case 0xD0: case 0xD1:	/* keypad 1,2,3 */
748		case 0xD2:				/* keypad 0 */
749			kbd->kb_prev_key = keycode | (scancode & 0x80);
750			goto next_code;
751
752		case 0x38:				/* left alt key */
753			break;
754
755		default:
756			if (state->ks_composed_char > 0) {
757				state->ks_flags &= ~COMPOSE;
758				state->ks_composed_char = 0;
759				kbd->kb_prev_key = keycode | (scancode & 0x80);
760				return ERRKEY;
761			}
762			break;
763		}
764	}
765
766	/* keycode to key action */
767	action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
768				  &state->ks_state, &state->ks_accents);
769	kbd->kb_prev_key = keycode | (scancode & 0x80);
770	if (action == NOKEY)
771		goto next_code;
772	else
773		return action;
774}
775
776/* check if char is waiting */
777static int
778atkbd_check_char(keyboard_t *kbd)
779{
780	atkbd_state_t *state;
781
782	if (!KBD_IS_ACTIVE(kbd))
783		return FALSE;
784	state = (atkbd_state_t *)kbd->kb_data;
785	if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
786		return TRUE;
787	return kbdc_data_ready(state->kbdc);
788}
789
790/* some useful control functions */
791static int
792atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
793{
794	/* trasnlate LED_XXX bits into the device specific bits */
795	static u_char ledmap[8] = {
796		0, 4, 2, 6, 1, 5, 3, 7,
797	};
798	atkbd_state_t *state = kbd->kb_data;
799	int error;
800	int s;
801	int i;
802
803	s = spltty();
804	switch (cmd) {
805
806	case KDGKBMODE:		/* get keyboard mode */
807		*(int *)arg = state->ks_mode;
808		break;
809	case KDSKBMODE:		/* set keyboard mode */
810		switch (*(int *)arg) {
811		case K_XLATE:
812			if (state->ks_mode != K_XLATE) {
813				/* make lock key state and LED state match */
814				state->ks_state &= ~LOCK_MASK;
815				state->ks_state |= KBD_LED_VAL(kbd);
816			}
817			/* FALL THROUGH */
818		case K_RAW:
819		case K_CODE:
820			if (state->ks_mode != *(int *)arg) {
821				atkbd_clear_state(kbd);
822				state->ks_mode = *(int *)arg;
823			}
824			break;
825		default:
826			splx(s);
827			return EINVAL;
828		}
829		break;
830
831	case KDGETLED:		/* get keyboard LED */
832		*(int *)arg = KBD_LED_VAL(kbd);
833		break;
834	case KDSETLED:		/* set keyboard LED */
835		/* NOTE: lock key state in ks_state won't be changed */
836		if (*(int *)arg & ~LOCK_MASK) {
837			splx(s);
838			return EINVAL;
839		}
840		i = *(int *)arg;
841		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
842		if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
843			if (i & ALKED)
844				i |= CLKED;
845			else
846				i &= ~CLKED;
847		}
848		if (KBD_HAS_DEVICE(kbd)) {
849			error = write_kbd(state->kbdc, KBDC_SET_LEDS,
850					  ledmap[i & LED_MASK]);
851			if (error) {
852				splx(s);
853				return error;
854			}
855		}
856		KBD_LED_VAL(kbd) = *(int *)arg;
857		break;
858
859	case KDGKBSTATE:	/* get lock key state */
860		*(int *)arg = state->ks_state & LOCK_MASK;
861		break;
862	case KDSKBSTATE:	/* set lock key state */
863		if (*(int *)arg & ~LOCK_MASK) {
864			splx(s);
865			return EINVAL;
866		}
867		state->ks_state &= ~LOCK_MASK;
868		state->ks_state |= *(int *)arg;
869		splx(s);
870		/* set LEDs and quit */
871		return atkbd_ioctl(kbd, KDSETLED, arg);
872
873	case KDSETREPEAT:	/* set keyboard repeat rate (new interface) */
874		splx(s);
875		if (!KBD_HAS_DEVICE(kbd))
876			return 0;
877		i = typematic(((int *)arg)[0], ((int *)arg)[1]);
878		return write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
879
880	case KDSETRAD:		/* set keyboard repeat rate (old interface) */
881		splx(s);
882		if (!KBD_HAS_DEVICE(kbd))
883			return 0;
884		return write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
885
886	case PIO_KEYMAP:	/* set keyboard translation table */
887	case PIO_KEYMAPENT:	/* set keyboard translation table entry */
888	case PIO_DEADKEYMAP:	/* set accent key translation table */
889		state->ks_accents = 0;
890		/* FALL THROUGH */
891	default:
892		splx(s);
893		return genkbd_commonioctl(kbd, cmd, arg);
894	}
895
896	splx(s);
897	return 0;
898}
899
900/* lock the access to the keyboard */
901static int
902atkbd_lock(keyboard_t *kbd, int lock)
903{
904	return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
905}
906
907/* clear the internal state of the keyboard */
908static void
909atkbd_clear_state(keyboard_t *kbd)
910{
911	atkbd_state_t *state;
912
913	state = (atkbd_state_t *)kbd->kb_data;
914	state->ks_flags = 0;
915	state->ks_polling = 0;
916	state->ks_state &= LOCK_MASK;	/* preserve locking key state */
917	state->ks_accents = 0;
918	state->ks_composed_char = 0;
919#if 0
920	state->ks_prefix = 0; /* XXX */
921#endif
922}
923
924/* save the internal state */
925static int
926atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
927{
928	if (len == 0)
929		return sizeof(atkbd_state_t);
930	if (len < sizeof(atkbd_state_t))
931		return -1;
932	bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
933	return 0;
934}
935
936/* set the internal state */
937static int
938atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
939{
940	if (len < sizeof(atkbd_state_t))
941		return ENOMEM;
942	if (((atkbd_state_t *)kbd->kb_data)->kbdc
943		!= ((atkbd_state_t *)buf)->kbdc)
944		return ENOMEM;
945	bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
946	return 0;
947}
948
949static int
950atkbd_poll(keyboard_t *kbd, int on)
951{
952	atkbd_state_t *state;
953	int s;
954
955	state = (atkbd_state_t *)kbd->kb_data;
956	s = spltty();
957	if (on)
958		++state->ks_polling;
959	else
960		--state->ks_polling;
961	splx(s);
962	return 0;
963}
964
965/* local functions */
966
967static int
968setup_kbd_port(KBDC kbdc, int port, int intr)
969{
970	if (!set_controller_command_byte(kbdc,
971		KBD_KBD_CONTROL_BITS,
972		((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
973		    | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
974		return 1;
975	return 0;
976}
977
978static int
979get_kbd_echo(KBDC kbdc)
980{
981	/* enable the keyboard port, but disable the keyboard intr. */
982	if (setup_kbd_port(kbdc, TRUE, FALSE))
983		/* CONTROLLER ERROR: there is very little we can do... */
984		return ENXIO;
985
986	/* see if something is present */
987	write_kbd_command(kbdc, KBDC_ECHO);
988	if (read_kbd_data(kbdc) != KBD_ECHO) {
989		empty_both_buffers(kbdc, 10);
990		test_controller(kbdc);
991		test_kbd_port(kbdc);
992		return ENXIO;
993	}
994
995	/* enable the keyboard port and intr. */
996	if (setup_kbd_port(kbdc, TRUE, TRUE)) {
997		/*
998		 * CONTROLLER ERROR
999		 * This is serious; the keyboard intr is left disabled!
1000		 */
1001		return ENXIO;
1002	}
1003
1004	return 0;
1005}
1006
1007static int
1008probe_keyboard(KBDC kbdc, int flags)
1009{
1010	/*
1011	 * Don't try to print anything in this function.  The low-level
1012	 * console may not have been initialized yet...
1013	 */
1014	int err;
1015	int c;
1016	int m;
1017
1018	if (!kbdc_lock(kbdc, TRUE)) {
1019		/* driver error? */
1020		return ENXIO;
1021	}
1022
1023	/* flush any noise in the buffer */
1024	empty_both_buffers(kbdc, 10);
1025
1026	/* save the current keyboard controller command byte */
1027	m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1028	c = get_controller_command_byte(kbdc);
1029	if (c == -1) {
1030		/* CONTROLLER ERROR */
1031		kbdc_set_device_mask(kbdc, m);
1032		kbdc_lock(kbdc, FALSE);
1033		return ENXIO;
1034	}
1035
1036	/*
1037	 * The keyboard may have been screwed up by the boot block.
1038	 * We may just be able to recover from error by testing the controller
1039	 * and the keyboard port. The controller command byte needs to be
1040	 * saved before this recovery operation, as some controllers seem
1041	 * to set the command byte to particular values.
1042	 */
1043	test_controller(kbdc);
1044	test_kbd_port(kbdc);
1045
1046	err = get_kbd_echo(kbdc);
1047	if (err == 0) {
1048		kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1049	} else {
1050		if (c != -1)
1051			/* try to restore the command byte as before */
1052			set_controller_command_byte(kbdc, 0xff, c);
1053		kbdc_set_device_mask(kbdc, m);
1054	}
1055
1056	kbdc_lock(kbdc, FALSE);
1057	return err;
1058}
1059
1060static int
1061init_keyboard(KBDC kbdc, int *type, int flags)
1062{
1063	int codeset;
1064	int id;
1065	int c;
1066
1067	if (!kbdc_lock(kbdc, TRUE)) {
1068		/* driver error? */
1069		return EIO;
1070	}
1071
1072	/* save the current controller command byte */
1073	empty_both_buffers(kbdc, 200);
1074	c = get_controller_command_byte(kbdc);
1075	if (c == -1) {
1076		/* CONTROLLER ERROR */
1077		kbdc_lock(kbdc, FALSE);
1078		printf("atkbd: unable to get the current command byte value.\n");
1079		return EIO;
1080	}
1081	if (bootverbose)
1082		printf("atkbd: the current kbd controller command byte %04x\n",
1083		       c);
1084#if 0
1085	/* override the keyboard lock switch */
1086	c |= KBD_OVERRIDE_KBD_LOCK;
1087#endif
1088
1089	/* enable the keyboard port, but disable the keyboard intr. */
1090	if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1091		/* CONTROLLER ERROR: there is very little we can do... */
1092		printf("atkbd: unable to set the command byte.\n");
1093		kbdc_lock(kbdc, FALSE);
1094		return EIO;
1095	}
1096
1097	/*
1098	 * Check if we have an XT keyboard before we attempt to reset it.
1099	 * The procedure assumes that the keyboard and the controller have
1100	 * been set up properly by BIOS and have not been messed up
1101	 * during the boot process.
1102	 */
1103	codeset = -1;
1104	if (flags & KB_CONF_ALT_SCANCODESET)
1105		/* the user says there is a XT keyboard */
1106		codeset = 1;
1107#ifdef KBD_DETECT_XT_KEYBOARD
1108	else if ((c & KBD_TRANSLATION) == 0) {
1109		/* SET_SCANCODE_SET is not always supported; ignore error */
1110		if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1111			== KBD_ACK)
1112			codeset = read_kbd_data(kbdc);
1113	}
1114	if (bootverbose)
1115		printf("atkbd: scancode set %d\n", codeset);
1116#endif /* KBD_DETECT_XT_KEYBOARD */
1117
1118	*type = KB_OTHER;
1119	id = get_kbd_id(kbdc);
1120	switch(id) {
1121	case 0x41ab:
1122	case 0x83ab:
1123		*type = KB_101;
1124		break;
1125	case -1:	/* AT 84 keyboard doesn't return ID */
1126		*type = KB_84;
1127		break;
1128	default:
1129		break;
1130	}
1131	if (bootverbose)
1132		printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1133
1134	/* reset keyboard hardware */
1135	if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1136		/*
1137		 * KEYBOARD ERROR
1138		 * Keyboard reset may fail either because the keyboard
1139		 * doen't exist, or because the keyboard doesn't pass
1140		 * the self-test, or the keyboard controller on the
1141		 * motherboard and the keyboard somehow fail to shake hands.
1142		 * It is just possible, particularly in the last case,
1143		 * that the keyoard controller may be left in a hung state.
1144		 * test_controller() and test_kbd_port() appear to bring
1145		 * the keyboard controller back (I don't know why and how,
1146		 * though.)
1147		 */
1148		empty_both_buffers(kbdc, 10);
1149		test_controller(kbdc);
1150		test_kbd_port(kbdc);
1151		/*
1152		 * We could disable the keyboard port and interrupt... but,
1153		 * the keyboard may still exist (see above).
1154		 */
1155		set_controller_command_byte(kbdc, 0xff, c);
1156		kbdc_lock(kbdc, FALSE);
1157		if (bootverbose)
1158			printf("atkbd: failed to reset the keyboard.\n");
1159		return EIO;
1160	}
1161
1162	/*
1163	 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
1164	 * such as those on the IBM ThinkPad laptop computers can be used
1165	 * with the standard console driver.
1166	 */
1167	if (codeset == 1) {
1168		if (send_kbd_command_and_data(kbdc,
1169			KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1170			/* XT kbd doesn't need scan code translation */
1171			c &= ~KBD_TRANSLATION;
1172		} else {
1173			/*
1174			 * KEYBOARD ERROR
1175			 * The XT kbd isn't usable unless the proper scan
1176			 * code set is selected.
1177			 */
1178			set_controller_command_byte(kbdc, 0xff, c);
1179			kbdc_lock(kbdc, FALSE);
1180			printf("atkbd: unable to set the XT keyboard mode.\n");
1181			return EIO;
1182		}
1183	}
1184
1185#ifdef __alpha__
1186	if (send_kbd_command_and_data(
1187		kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1188		printf("atkbd: can't set translation.\n");
1189
1190	}
1191	c |= KBD_TRANSLATION;
1192#endif
1193
1194	/* enable the keyboard port and intr. */
1195	if (!set_controller_command_byte(kbdc,
1196		KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1197		(c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1198		    | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1199		/*
1200		 * CONTROLLER ERROR
1201		 * This is serious; we are left with the disabled
1202		 * keyboard intr.
1203		 */
1204		set_controller_command_byte(kbdc, 0xff, c);
1205		kbdc_lock(kbdc, FALSE);
1206		printf("atkbd: unable to enable the keyboard port and intr.\n");
1207		return EIO;
1208	}
1209
1210	kbdc_lock(kbdc, FALSE);
1211	return 0;
1212}
1213
1214static int
1215write_kbd(KBDC kbdc, int command, int data)
1216{
1217    int s;
1218
1219    /* prevent the timeout routine from polling the keyboard */
1220    if (!kbdc_lock(kbdc, TRUE))
1221	return EBUSY;
1222
1223    /* disable the keyboard and mouse interrupt */
1224    s = spltty();
1225#if 0
1226    c = get_controller_command_byte(kbdc);
1227    if ((c == -1)
1228	|| !set_controller_command_byte(kbdc,
1229            kbdc_get_device_mask(kbdc),
1230            KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1231                | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1232	/* CONTROLLER ERROR */
1233        kbdc_lock(kbdc, FALSE);
1234	splx(s);
1235	return EIO;
1236    }
1237    /*
1238     * Now that the keyboard controller is told not to generate
1239     * the keyboard and mouse interrupts, call `splx()' to allow
1240     * the other tty interrupts. The clock interrupt may also occur,
1241     * but the timeout routine (`scrn_timer()') will be blocked
1242     * by the lock flag set via `kbdc_lock()'
1243     */
1244    splx(s);
1245#endif
1246
1247    if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1248        send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1249
1250#if 0
1251    /* restore the interrupts */
1252    if (!set_controller_command_byte(kbdc,
1253            kbdc_get_device_mask(kbdc),
1254	    c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1255	/* CONTROLLER ERROR */
1256    }
1257#else
1258    splx(s);
1259#endif
1260    kbdc_lock(kbdc, FALSE);
1261
1262    return 0;
1263}
1264
1265static int
1266get_kbd_id(KBDC kbdc)
1267{
1268	int id1, id2;
1269
1270	empty_both_buffers(kbdc, 10);
1271	id1 = id2 = -1;
1272	if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1273		return -1;
1274
1275	DELAY(10000); 	/* 10 msec delay */
1276	id1 = read_kbd_data(kbdc);
1277	if (id1 != -1)
1278		id2 = read_kbd_data(kbdc);
1279
1280	if ((id1 == -1) || (id2 == -1)) {
1281		empty_both_buffers(kbdc, 10);
1282		test_controller(kbdc);
1283		test_kbd_port(kbdc);
1284		return -1;
1285	}
1286	return ((id2 << 8) | id1);
1287}
1288
1289static int
1290typematic(int delay, int rate)
1291{
1292	static int delays[] = { 250, 500, 750, 1000 };
1293	static int rates[] = {  34,  38,  42,  46,  50,  55,  59,  63,
1294				68,  76,  84,  92, 100, 110, 118, 126,
1295			       136, 152, 168, 184, 200, 220, 236, 252,
1296			       272, 304, 336, 368, 400, 440, 472, 504 };
1297	int value;
1298	int i;
1299
1300	for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1301		if (delay >= delays[i])
1302			break;
1303	}
1304	value = i << 5;
1305	for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1306		if (rate >= rates[i])
1307			break;
1308	}
1309	value |= i;
1310	return value;
1311}
1312
1313#endif /* NATKBD > 0 */
1314