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