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