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