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