kernel - Get rid of old KDSETRAD keyboard ioctl, and some __i386__ kbd code.
[dragonfly.git] / sys / dev / misc / kbd / atkbd.c
blob96266962dbff2593ad53d215288191c49b2015ee
1 /*-
2 * (MPSAFE)
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * $FreeBSD: src/sys/dev/kbd/atkbd.c,v 1.25.2.4 2002/04/08 19:21:38 asmodai Exp $
31 * NOTE: All locks are handled by the kbd wrappers.
34 #include "opt_kbd.h"
35 #include "opt_atkbd.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/proc.h>
42 #include <sys/malloc.h>
43 #include <sys/thread2.h>
45 #include <sys/kbio.h>
46 #include "kbdreg.h"
47 #include "atkbdreg.h"
48 #include "atkbdcreg.h"
50 #include <bus/isa/isareg.h>
52 static timeout_t atkbd_timeout;
54 #if 0
55 static int atkbd_setmuxmode(KBDC kbdc, int value, int *mux_version);
56 #endif
58 int
59 atkbd_probe_unit(int unit, int ctlr, int irq, int flags)
61 keyboard_switch_t *sw;
62 int args[2];
63 int error;
65 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
66 if (sw == NULL) {
67 return ENXIO;
70 args[0] = ctlr;
71 args[1] = irq;
72 error = (*sw->probe)(unit, args, flags);
74 if (error)
75 return error;
76 return 0;
79 int
80 atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
82 keyboard_switch_t *sw;
83 int args[2];
84 int error;
86 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
87 if (sw == NULL) {
88 return ENXIO;
91 /* reset, initialize and enable the device */
92 args[0] = ctlr;
93 args[1] = irq;
94 *kbd = NULL;
95 error = (*sw->probe)(unit, args, flags);
96 if (error) {
97 return error;
99 error = (*sw->init)(unit, kbd, args, flags);
100 if (error) {
101 return error;
103 (*sw->enable)(*kbd);
105 #ifdef KBD_INSTALL_CDEV
106 /* attach a virtual keyboard cdev */
107 error = kbd_attach(*kbd);
108 if (error) {
109 return error;
111 #endif
114 * This is a kludge to compensate for lost keyboard interrupts.
115 * A similar code used to be in syscons. See below. XXX
117 atkbd_timeout(*kbd);
119 if (bootverbose)
120 (*sw->diag)(*kbd, bootverbose);
122 return 0;
125 static void
126 atkbd_timeout(void *arg)
128 keyboard_t *kbd;
131 * The original text of the following comments are extracted
132 * from syscons.c (1.287)
134 * With release 2.1 of the Xaccel server, the keyboard is left
135 * hanging pretty often. Apparently an interrupt from the
136 * keyboard is lost, and I don't know why (yet).
137 * This ugly hack calls the low-level interrupt routine if input
138 * is ready for the keyboard and conveniently hides the problem. XXX
140 * Try removing anything stuck in the keyboard controller; whether
141 * it's a keyboard scan code or mouse data. The low-level
142 * interrupt routine doesn't read the mouse data directly,
143 * but the keyboard controller driver will, as a side effect.
146 * And here is bde's original comment about this:
148 * This is necessary to handle edge triggered interrupts - if we
149 * returned when our IRQ is high due to unserviced input, then there
150 * would be no more keyboard IRQs until the keyboard is reset by
151 * external powers.
153 * The keyboard apparently unwedges the irq in most cases.
155 crit_enter();
156 kbd = (keyboard_t *)arg;
157 if (kbd_lock(kbd, TRUE)) {
159 * We have seen the lock flag is not set. Let's reset
160 * the flag early, otherwise the LED update routine fails
161 * which may want the lock during the interrupt routine.
163 kbd_lock(kbd, FALSE);
164 if (kbd_check_char(kbd))
165 kbd_intr(kbd, NULL);
167 callout_reset(&kbd->kb_atkbd_timeout_ch, hz / 10, atkbd_timeout, arg);
168 crit_exit();
171 /* LOW-LEVEL */
173 #include <machine/limits.h>
174 #include <machine/clock.h>
176 #define ATKBD_DEFAULT 0
178 typedef struct atkbd_state {
179 KBDC kbdc; /* keyboard controller */
180 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
181 int ks_flags; /* flags */
182 #define COMPOSE (1 << 0)
183 int ks_polling;
184 int ks_state; /* shift/lock key state */
185 int ks_accents; /* accent key index (> 0) */
186 u_int ks_composed_char; /* composed char code (> 0) */
187 u_char ks_prefix; /* AT scan code prefix */
188 } atkbd_state_t;
190 /* keyboard driver declaration */
191 static int atkbd_configure(int flags);
192 static kbd_probe_t atkbd_probe;
193 static kbd_init_t atkbd_init;
194 static kbd_term_t atkbd_term;
195 static kbd_intr_t atkbd_intr;
196 static kbd_test_if_t atkbd_test_if;
197 static kbd_enable_t atkbd_enable;
198 static kbd_disable_t atkbd_disable;
199 static kbd_read_t atkbd_read;
200 static kbd_check_t atkbd_check;
201 static kbd_read_char_t atkbd_read_char;
202 static kbd_check_char_t atkbd_check_char;
203 static kbd_ioctl_t atkbd_ioctl;
204 static kbd_lock_t atkbd_lock;
205 static kbd_clear_state_t atkbd_clear_state;
206 static kbd_get_state_t atkbd_get_state;
207 static kbd_set_state_t atkbd_set_state;
208 static kbd_poll_mode_t atkbd_poll;
210 keyboard_switch_t atkbdsw = {
211 atkbd_probe,
212 atkbd_init,
213 atkbd_term,
214 atkbd_intr,
215 atkbd_test_if,
216 atkbd_enable,
217 atkbd_disable,
218 atkbd_read,
219 atkbd_check,
220 atkbd_read_char,
221 atkbd_check_char,
222 atkbd_ioctl,
223 atkbd_lock,
224 atkbd_clear_state,
225 atkbd_get_state,
226 atkbd_set_state,
227 genkbd_get_fkeystr,
228 atkbd_poll,
229 genkbd_diag,
232 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
234 /* local functions */
235 static int get_typematic(keyboard_t *kbd);
236 static int setup_kbd_port(KBDC kbdc, int port, int intr);
237 static int get_kbd_echo(KBDC kbdc);
238 static int probe_keyboard(KBDC kbdc, int flags);
239 static int init_keyboard(KBDC kbdc, int *type, int flags);
240 static int write_kbd(KBDC kbdc, int command, int data);
241 static int get_kbd_id(KBDC kbdc, int cmd);
242 static int typematic(int delay, int rate);
243 static int typematic_delay(int delay);
244 static int typematic_rate(int rate);
246 /* local variables */
248 /* the initial key map, accent map and fkey strings */
249 #ifdef ATKBD_DFLT_KEYMAP
250 #define KBD_DFLT_KEYMAP
251 #include "atkbdmap.h"
252 #endif
253 #include "kbdtables.h"
255 /* structures for the default keyboard */
256 static keyboard_t default_kbd;
257 static atkbd_state_t default_kbd_state;
258 static keymap_t default_keymap;
259 static accentmap_t default_accentmap;
260 static fkeytab_t default_fkeytab[NUM_FKEYS];
263 * The back door to the keyboard driver!
264 * This function is called by the console driver, via the kbdio module,
265 * to tickle keyboard drivers when the low-level console is being initialized.
266 * Almost nothing in the kernel has been initialied yet. Try to probe
267 * keyboards if possible.
268 * NOTE: because of the way the low-level console is initialized, this routine
269 * may be called more than once!!
271 static int
272 atkbd_configure(int flags)
274 keyboard_t *kbd;
275 int arg[2];
276 int i;
279 * Probe the keyboard controller, if not present or if the driver
280 * is disabled, unregister the keyboard if any.
282 if (atkbdc_configure() != 0 ||
283 resource_disabled("atkbd", ATKBD_DEFAULT)) {
284 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
285 if (i >= 0) {
286 kbd = kbd_get_keyboard(i);
287 KBD_ALWAYS_LOCK(kbd);
288 kbd_unregister(kbd);
289 kbd = NULL; /* huh? */
291 return 0;
294 /* XXX: a kludge to obtain the device configuration flags */
295 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
296 flags |= i;
298 /* probe the default keyboard */
299 arg[0] = -1;
300 arg[1] = -1;
301 kbd = NULL;
302 if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) {
303 return 0;
305 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) {
306 return 0;
309 /* return the number of found keyboards */
310 return 1;
313 /* low-level functions */
315 /* detect a keyboard */
316 static int
317 atkbd_probe(int unit, void *arg, int flags)
319 KBDC kbdc;
320 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
323 if (unit == ATKBD_DEFAULT) {
324 if (KBD_IS_PROBED(&default_kbd)) {
325 return 0;
329 kbdc = atkbdc_open(data[0]);
330 if (kbdc == NULL) {
331 return ENXIO;
333 if (probe_keyboard(kbdc, flags)) {
334 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
335 return ENXIO;
339 return 0;
342 /* reset and initialize the device */
343 static int
344 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
346 keyboard_t *kbd;
347 atkbd_state_t *state;
348 keymap_t *keymap;
349 accentmap_t *accmap;
350 fkeytab_t *fkeymap;
351 int fkeymap_size;
352 int delay[2];
353 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
355 /* XXX */
356 if (unit == ATKBD_DEFAULT) {
357 *kbdp = kbd = &default_kbd;
358 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) {
359 return 0;
361 state = &default_kbd_state;
362 keymap = &default_keymap;
363 accmap = &default_accentmap;
364 fkeymap = default_fkeytab;
365 fkeymap_size = NELEM(default_fkeytab);
366 } else if (*kbdp == NULL) {
367 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_WAITOK|M_ZERO);
368 state = kmalloc(sizeof(*state), M_DEVBUF, M_WAITOK|M_ZERO);
369 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_WAITOK);
370 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_WAITOK);
371 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_WAITOK);
372 fkeymap_size = NELEM(fkey_tab);
373 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
374 return 0;
375 } else {
376 kbd = *kbdp;
377 state = (atkbd_state_t *)kbd->kb_data;
378 bzero(state, sizeof(*state));
379 keymap = kbd->kb_keymap;
380 accmap = kbd->kb_accentmap;
381 fkeymap = kbd->kb_fkeytab;
382 fkeymap_size = kbd->kb_fkeytab_size;
385 if (!KBD_IS_PROBED(kbd)) {
386 state->kbdc = atkbdc_open(data[0]);
387 if (state->kbdc == NULL) {
388 return ENXIO;
390 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
391 KB_PRI_ATKBD, 0, 0);
392 bcopy(&key_map, keymap, sizeof(key_map));
393 bcopy(&accent_map, accmap, sizeof(accent_map));
394 bcopy(fkey_tab, fkeymap,
395 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
396 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
397 kbd->kb_data = (void *)state;
399 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
400 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
401 return ENXIO;
403 } else {
404 KBD_FOUND_DEVICE(kbd);
406 atkbd_clear_state(kbd);
407 state->ks_mode = K_XLATE;
409 * FIXME: set the initial value for lock keys in ks_state
410 * according to the BIOS data?
412 KBD_PROBE_DONE(kbd);
414 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
415 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
416 if (!KBD_HAS_DEVICE(kbd)
417 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
418 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
419 return ENXIO;
421 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
422 get_typematic(kbd);
423 delay[0] = kbd->kb_delay1;
424 delay[1] = kbd->kb_delay2;
425 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
426 KBD_FOUND_DEVICE(kbd);
427 KBD_INIT_DONE(kbd);
429 if (!KBD_IS_CONFIGURED(kbd)) {
430 if (kbd_register(kbd) < 0) {
431 return ENXIO;
433 KBD_CONFIG_DONE(kbd);
436 return 0;
439 /* finish using this keyboard */
440 static int
441 atkbd_term(keyboard_t *kbd)
443 kbd_unregister(kbd);
444 return 0;
447 /* keyboard interrupt routine */
448 static int
449 atkbd_intr(keyboard_t *kbd, void *arg)
451 atkbd_state_t *state;
452 int delay[2];
453 int c;
455 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
456 /* let the callback function to process the input */
457 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
458 kbd->kb_callback.kc_arg);
459 } else {
460 /* read and discard the input; no one is waiting for input */
461 do {
462 c = atkbd_read_char(kbd, FALSE);
463 } while (c != NOKEY);
465 if (!KBD_HAS_DEVICE(kbd)) {
467 * The keyboard was not detected before;
468 * it must have been reconnected!
470 state = (atkbd_state_t *)kbd->kb_data;
471 init_keyboard(state->kbdc, &kbd->kb_type,
472 kbd->kb_config);
473 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
474 get_typematic(kbd);
475 delay[0] = kbd->kb_delay1;
476 delay[1] = kbd->kb_delay2;
477 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
478 KBD_FOUND_DEVICE(kbd);
482 return 0;
485 /* test the interface to the device */
486 static int
487 atkbd_test_if(keyboard_t *kbd)
489 int error;
491 error = 0;
492 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
493 crit_enter();
494 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
495 error = EIO;
496 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
497 error = EIO;
498 crit_exit();
500 return error;
504 * Enable the access to the device; until this function is called,
505 * the client cannot read from the keyboard.
507 static int
508 atkbd_enable(keyboard_t *kbd)
510 crit_enter();
511 KBD_ACTIVATE(kbd);
512 crit_exit();
513 return 0;
516 /* disallow the access to the device */
517 static int
518 atkbd_disable(keyboard_t *kbd)
520 crit_enter();
521 KBD_DEACTIVATE(kbd);
522 crit_exit();
523 return 0;
526 /* read one byte from the keyboard if it's allowed */
527 static int
528 atkbd_read(keyboard_t *kbd, int wait)
530 int c, ret;
532 if (wait)
533 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
534 else
535 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
536 if (c != -1)
537 ++kbd->kb_count;
539 ret = (KBD_IS_ACTIVE(kbd) ? c : -1);
541 return ret;
544 /* check if data is waiting */
545 static int
546 atkbd_check(keyboard_t *kbd)
548 int ret;
550 if (!KBD_IS_ACTIVE(kbd)) {
551 return FALSE;
553 ret = kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
555 return ret;
558 /* read char from the keyboard */
559 static u_int
560 atkbd_read_char(keyboard_t *kbd, int wait)
562 atkbd_state_t *state;
563 u_int action;
564 int scancode;
565 int keycode;
567 state = (atkbd_state_t *)kbd->kb_data;
568 next_code:
569 /* do we have a composed char to return? */
570 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
571 action = state->ks_composed_char;
572 state->ks_composed_char = 0;
573 if (action > UCHAR_MAX) {
574 return ERRKEY;
576 return action;
579 /* see if there is something in the keyboard port */
580 if (wait) {
581 do {
582 scancode = read_kbd_data(state->kbdc);
583 } while (scancode == -1);
584 } else {
585 scancode = read_kbd_data_no_wait(state->kbdc);
586 if (scancode == -1) {
587 return NOKEY;
590 ++kbd->kb_count;
592 #if KBDIO_DEBUG >= 10
593 kprintf("atkbd_read_char(): scancode:0x%x\n", scancode);
594 #endif
596 /* return the byte as is for the K_RAW mode */
597 if (state->ks_mode == K_RAW) {
598 return scancode;
601 /* translate the scan code into a keycode */
602 keycode = scancode & 0x7F;
603 switch (state->ks_prefix) {
604 case 0x00: /* normal scancode */
605 switch(scancode) {
606 case 0xB8: /* left alt (compose key) released */
607 if (state->ks_flags & COMPOSE) {
608 state->ks_flags &= ~COMPOSE;
609 if (state->ks_composed_char > UCHAR_MAX)
610 state->ks_composed_char = 0;
612 break;
613 case 0x38: /* left alt (compose key) pressed */
614 if (!(state->ks_flags & COMPOSE)) {
615 state->ks_flags |= COMPOSE;
616 state->ks_composed_char = 0;
618 break;
619 case 0xE0:
620 case 0xE1:
621 state->ks_prefix = scancode;
622 goto next_code;
624 break;
625 case 0xE0: /* 0xE0 prefix */
626 state->ks_prefix = 0;
627 switch (keycode) {
628 case 0x1C: /* right enter key */
629 keycode = 0x59;
630 break;
631 case 0x1D: /* right ctrl key */
632 keycode = 0x5A;
633 break;
634 case 0x35: /* keypad divide key */
635 keycode = 0x5B;
636 break;
637 case 0x37: /* print scrn key */
638 keycode = 0x5C;
639 break;
640 case 0x38: /* right alt key (alt gr) */
641 keycode = 0x5D;
642 break;
643 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
644 keycode = 0x68;
645 break;
646 case 0x47: /* grey home key */
647 keycode = 0x5E;
648 break;
649 case 0x48: /* grey up arrow key */
650 keycode = 0x5F;
651 break;
652 case 0x49: /* grey page up key */
653 keycode = 0x60;
654 break;
655 case 0x4B: /* grey left arrow key */
656 keycode = 0x61;
657 break;
658 case 0x4D: /* grey right arrow key */
659 keycode = 0x62;
660 break;
661 case 0x4F: /* grey end key */
662 keycode = 0x63;
663 break;
664 case 0x50: /* grey down arrow key */
665 keycode = 0x64;
666 break;
667 case 0x51: /* grey page down key */
668 keycode = 0x65;
669 break;
670 case 0x52: /* grey insert key */
671 keycode = 0x66;
672 break;
673 case 0x53: /* grey delete key */
674 keycode = 0x67;
675 break;
676 /* the following 3 are only used on the MS "Natural" keyboard */
677 case 0x5b: /* left Window key */
678 keycode = 0x69;
679 break;
680 case 0x5c: /* right Window key */
681 keycode = 0x6a;
682 break;
683 case 0x5d: /* menu key */
684 keycode = 0x6b;
685 break;
686 default: /* ignore everything else */
687 goto next_code;
689 break;
690 case 0xE1: /* 0xE1 prefix */
692 * The pause/break key on the 101 keyboard produces:
693 * E1-1D-45 E1-9D-C5
694 * Ctrl-pause/break produces:
695 * E0-46 E0-C6 (See above.)
697 state->ks_prefix = 0;
698 if (keycode == 0x1D)
699 state->ks_prefix = 0x1D;
700 goto next_code;
701 /* NOT REACHED */
702 case 0x1D: /* pause / break */
703 state->ks_prefix = 0;
704 if (keycode != 0x45)
705 goto next_code;
706 keycode = 0x68;
707 break;
710 if (kbd->kb_type == KB_84) {
711 switch (keycode) {
712 case 0x37: /* *(numpad)/print screen */
713 if (state->ks_flags & SHIFTS)
714 keycode = 0x5c; /* print screen */
715 break;
716 case 0x45: /* num lock/pause */
717 if (state->ks_flags & CTLS)
718 keycode = 0x68; /* pause */
719 break;
720 case 0x46: /* scroll lock/break */
721 if (state->ks_flags & CTLS)
722 keycode = 0x6c; /* break */
723 break;
725 } else if (kbd->kb_type == KB_101) {
726 switch (keycode) {
727 case 0x5c: /* print screen */
728 if (state->ks_flags & ALTS)
729 keycode = 0x54; /* sysrq */
730 break;
731 case 0x68: /* pause/break */
732 if (state->ks_flags & CTLS)
733 keycode = 0x6c; /* break */
734 break;
738 /* return the key code in the K_CODE mode */
739 if (state->ks_mode == K_CODE) {
740 return (keycode | (scancode & 0x80));
743 /* compose a character code */
744 if (state->ks_flags & COMPOSE) {
745 switch (keycode | (scancode & 0x80)) {
746 /* key pressed, process it */
747 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
748 state->ks_composed_char *= 10;
749 state->ks_composed_char += keycode - 0x40;
750 if (state->ks_composed_char > UCHAR_MAX)
751 return ERRKEY;
752 goto next_code;
753 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
754 state->ks_composed_char *= 10;
755 state->ks_composed_char += keycode - 0x47;
756 if (state->ks_composed_char > UCHAR_MAX)
757 return ERRKEY;
758 goto next_code;
759 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
760 state->ks_composed_char *= 10;
761 state->ks_composed_char += keycode - 0x4E;
762 if (state->ks_composed_char > UCHAR_MAX)
763 return ERRKEY;
764 goto next_code;
765 case 0x52: /* keypad 0 */
766 state->ks_composed_char *= 10;
767 if (state->ks_composed_char > UCHAR_MAX)
768 return ERRKEY;
769 goto next_code;
771 /* key released, no interest here */
772 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
773 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
774 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
775 case 0xD2: /* keypad 0 */
776 goto next_code;
778 case 0x38: /* left alt key */
779 break;
781 default:
782 if (state->ks_composed_char > 0) {
783 state->ks_flags &= ~COMPOSE;
784 state->ks_composed_char = 0;
785 return ERRKEY;
787 break;
791 /* keycode to key action */
792 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
793 &state->ks_state, &state->ks_accents);
794 if (action == NOKEY) {
795 goto next_code;
796 } else {
797 return action;
801 /* check if char is waiting */
802 static int
803 atkbd_check_char(keyboard_t *kbd)
805 atkbd_state_t *state;
806 int ret;
808 if (!KBD_IS_ACTIVE(kbd)) {
809 return FALSE;
811 state = (atkbd_state_t *)kbd->kb_data;
812 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
813 return TRUE;
815 ret = kbdc_data_ready(state->kbdc);
816 return ret;
819 /* some useful control functions */
820 static int
821 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
823 /* trasnlate LED_XXX bits into the device specific bits */
824 static u_char ledmap[8] = {
825 0, 4, 2, 6, 1, 5, 3, 7,
827 atkbd_state_t *state = kbd->kb_data;
828 int error;
829 int i;
831 crit_enter();
833 switch (cmd) {
835 case KDGKBMODE: /* get keyboard mode */
836 *(int *)arg = state->ks_mode;
837 break;
838 case KDSKBMODE: /* set keyboard mode */
839 switch (*(int *)arg) {
840 case K_XLATE:
841 if (state->ks_mode != K_XLATE) {
842 /* make lock key state and LED state match */
843 state->ks_state &= ~LOCK_MASK;
844 state->ks_state |= KBD_LED_VAL(kbd);
846 /* FALL THROUGH */
847 case K_RAW:
848 case K_CODE:
849 if (state->ks_mode != *(int *)arg) {
850 atkbd_clear_state(kbd);
851 state->ks_mode = *(int *)arg;
853 break;
854 default:
855 crit_exit();
856 return EINVAL;
858 break;
860 case KDGETLED: /* get keyboard LED */
861 *(int *)arg = KBD_LED_VAL(kbd);
862 break;
863 case KDSETLED: /* set keyboard LED */
864 /* NOTE: lock key state in ks_state won't be changed */
865 if (*(int *)arg & ~LOCK_MASK) {
866 crit_exit();
867 return EINVAL;
869 i = *(int *)arg;
870 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
871 if (state->ks_mode == K_XLATE &&
872 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
873 if (i & ALKED)
874 i |= CLKED;
875 else
876 i &= ~CLKED;
878 if (KBD_HAS_DEVICE(kbd)) {
879 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
880 ledmap[i & LED_MASK]);
881 if (error) {
882 crit_exit();
883 return error;
886 KBD_LED_VAL(kbd) = *(int *)arg;
887 break;
889 case KDGKBSTATE: /* get lock key state */
890 *(int *)arg = state->ks_state & LOCK_MASK;
891 break;
892 case KDSKBSTATE: /* set lock key state */
893 if (*(int *)arg & ~LOCK_MASK) {
894 crit_exit();
895 return EINVAL;
897 state->ks_state &= ~LOCK_MASK;
898 state->ks_state |= *(int *)arg;
899 crit_exit();
900 /* set LEDs and quit */
901 return atkbd_ioctl(kbd, KDSETLED, arg);
903 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
904 crit_exit();
905 if (!KBD_HAS_DEVICE(kbd)) {
906 return 0;
908 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
909 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
910 if (error == 0) {
911 kbd->kb_delay1 = typematic_delay(i);
912 kbd->kb_delay2 = typematic_rate(i);
914 return error;
916 case PIO_KEYMAP: /* set keyboard translation table */
917 case PIO_KEYMAPENT: /* set keyboard translation table entry */
918 case PIO_DEADKEYMAP: /* set accent key translation table */
919 state->ks_accents = 0;
920 /* FALL THROUGH */
921 default:
922 crit_exit();
923 return genkbd_commonioctl(kbd, cmd, arg);
926 crit_exit();
927 return 0;
930 /* lock the access to the keyboard */
931 static int
932 atkbd_lock(keyboard_t *kbd, int lock)
934 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
937 /* clear the internal state of the keyboard */
938 static void
939 atkbd_clear_state(keyboard_t *kbd)
941 atkbd_state_t *state;
943 state = (atkbd_state_t *)kbd->kb_data;
944 state->ks_flags = 0;
945 state->ks_polling = 0;
946 state->ks_state &= LOCK_MASK; /* preserve locking key state */
947 state->ks_accents = 0;
948 state->ks_composed_char = 0;
949 #if 0
950 state->ks_prefix = 0; /* XXX */
951 #endif
954 /* save the internal state */
955 static int
956 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
958 if (len == 0)
959 return sizeof(atkbd_state_t);
960 if (len < sizeof(atkbd_state_t))
961 return -1;
963 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
964 return 0;
967 /* set the internal state */
968 static int
969 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
971 if (len < sizeof(atkbd_state_t))
972 return ENOMEM;
973 if (((atkbd_state_t *)kbd->kb_data)->kbdc
974 != ((atkbd_state_t *)buf)->kbdc)
975 return ENOMEM;
976 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
977 return 0;
980 static int
981 atkbd_poll(keyboard_t *kbd, int on)
983 atkbd_state_t *state;
985 state = (atkbd_state_t *)kbd->kb_data;
986 crit_enter();
987 if (on)
988 state->ks_polling = 1;
989 else
990 state->ks_polling = 0;
991 crit_exit();
992 return 0;
995 /* local functions */
997 static int
998 get_typematic(keyboard_t *kbd)
1000 return ENODEV;
1003 static int
1004 setup_kbd_port(KBDC kbdc, int port, int intr)
1006 if (!set_controller_command_byte(kbdc,
1007 KBD_KBD_CONTROL_BITS,
1008 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT) |
1009 ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1010 return 1;
1011 return 0;
1014 static int
1015 get_kbd_echo(KBDC kbdc)
1017 /* enable the keyboard port, but disable the keyboard intr. */
1018 if (setup_kbd_port(kbdc, TRUE, FALSE))
1019 /* CONTROLLER ERROR: there is very little we can do... */
1020 return ENXIO;
1022 /* see if something is present */
1023 write_kbd_command(kbdc, KBDC_ECHO);
1024 if (read_kbd_data(kbdc) != KBD_ECHO) {
1025 empty_both_buffers(kbdc, 10);
1026 test_controller(kbdc);
1027 test_kbd_port(kbdc);
1028 return ENXIO;
1031 /* enable the keyboard port and intr. */
1032 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1034 * CONTROLLER ERROR
1035 * This is serious; the keyboard intr is left disabled!
1037 return ENXIO;
1040 return 0;
1043 static int
1044 probe_keyboard(KBDC kbdc, int flags)
1047 * Don't try to print anything in this function. The low-level
1048 * console may not have been initialized yet...
1050 int err;
1051 int c;
1053 if (!kbdc_lock(kbdc, TRUE)) {
1054 /* driver error? */
1055 return ENXIO;
1059 * XXX block data transmission from the keyboard. This can cause
1060 * the keyboard to stop sending keystrokes even when re-enabled
1061 * under certain circumstances if not followed by a full reset.
1063 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1065 /* flush any noise in the buffer */
1066 empty_both_buffers(kbdc, 100);
1068 /* save the current keyboard controller command byte */
1069 c = get_controller_command_byte(kbdc);
1070 if (c == -1) {
1071 /* CONTROLLER ERROR */
1072 kbdc_lock(kbdc, FALSE);
1073 return ENXIO;
1077 * The keyboard may have been screwed up by the boot block.
1078 * We may just be able to recover from error by testing the controller
1079 * and the keyboard port. The controller command byte needs to be
1080 * saved before this recovery operation, as some controllers seem
1081 * to set the command byte to particular values.
1083 test_controller(kbdc);
1084 test_kbd_port(kbdc);
1086 err = get_kbd_echo(kbdc);
1089 * Even if the keyboard doesn't seem to be present (err != 0),
1090 * we shall enable the keyboard port and interrupt so that
1091 * the driver will be operable when the keyboard is attached
1092 * to the system later. It is NOT recommended to hot-plug
1093 * the AT keyboard, but many people do so...
1095 setup_kbd_port(kbdc, TRUE, TRUE);
1096 #if 0
1097 if (err) {
1098 /* try to restore the command byte as before */
1099 set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c);
1101 #endif
1103 kbdc_lock(kbdc, FALSE);
1104 return err;
1107 static int
1108 init_keyboard(KBDC kbdc, int *type, int flags)
1110 int codeset;
1111 int id;
1112 int c;
1113 int mux_version;
1114 int mux_mask;
1115 int mux_val;
1117 if (!kbdc_lock(kbdc, TRUE)) {
1118 /* driver error? */
1119 return EIO;
1123 * XXX block data transmission from the keyboard. This can cause
1124 * the keyboard to stop sending keystrokes even when re-enabled
1125 * under certain circumstances if not followed by a full reset.
1127 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1129 #if 0
1130 if (atkbd_setmuxmode(kbdc, 1, &mux_version)) {
1131 kprintf("atkbd: no mux\n");
1132 mux_version = -1;
1133 } else {
1134 kprintf("atkbd: mux present version %d\n", mux_version);
1136 #else
1137 mux_version = -1;
1138 #endif
1140 /* save the current controller command byte */
1141 empty_both_buffers(kbdc, 200);
1142 c = get_controller_command_byte(kbdc);
1143 if (c == -1) {
1144 /* CONTROLLER ERROR */
1145 kbdc_lock(kbdc, FALSE);
1146 kprintf("atkbd: unable to get the current command byte value.\n");
1147 return EIO;
1149 if (bootverbose)
1150 kprintf("atkbd: the current kbd controller command byte %04x\n",
1152 #if 0
1153 /* override the keyboard lock switch */
1154 c |= KBD_OVERRIDE_KBD_LOCK;
1155 #endif
1157 /* enable the keyboard port, but disable the keyboard intr. */
1158 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1159 /* CONTROLLER ERROR: there is very little we can do... */
1160 kprintf("atkbd: unable to set the command byte.\n");
1161 kbdc_lock(kbdc, FALSE);
1162 return EIO;
1165 /* default codeset */
1166 codeset = -1;
1168 /* reset keyboard hardware */
1169 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1171 * KEYBOARD ERROR
1172 * Keyboard reset may fail either because the keyboard
1173 * doen't exist, or because the keyboard doesn't pass
1174 * the self-test, or the keyboard controller on the
1175 * motherboard and the keyboard somehow fail to shake hands.
1176 * It is just possible, particularly in the last case,
1177 * that the keyoard controller may be left in a hung state.
1178 * test_controller() and test_kbd_port() appear to bring
1179 * the keyboard controller back (I don't know why and how,
1180 * though.)
1182 empty_both_buffers(kbdc, 10);
1183 test_controller(kbdc);
1184 test_kbd_port(kbdc);
1186 * We could disable the keyboard port and interrupt... but,
1187 * the keyboard may still exist (see above).
1189 set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c);
1190 kbdc_lock(kbdc, FALSE);
1191 if (bootverbose)
1192 kprintf("atkbd: failed to reset the keyboard.\n");
1193 return EIO;
1197 * Check if we have an XT keyboard before we attempt to reset it.
1198 * The procedure assumes that the keyboard and the controller have
1199 * been set up properly by BIOS and have not been messed up
1200 * during the boot process.
1202 codeset = -1;
1203 if (flags & KB_CONF_ALT_SCANCODESET)
1204 /* the user says there is a XT keyboard */
1205 codeset = 1;
1206 #ifdef KBD_DETECT_XT_KEYBOARD
1207 else if ((c & KBD_TRANSLATION) == 0) {
1208 /* SET_SCANCODE_SET is not always supported; ignore error */
1209 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1210 == KBD_ACK)
1211 codeset = read_kbd_data(kbdc);
1213 #endif /* KBD_DETECT_XT_KEYBOARD */
1214 if (bootverbose)
1215 kprintf("atkbd: scancode set %d\n", codeset);
1218 * Get the keyboard id.
1220 *type = KB_OTHER;
1221 id = get_kbd_id(kbdc, ATKBD_CMD_GETID);
1222 switch(id) {
1223 case 0x41ab: /* 101/102/... Enhanced */
1224 case 0x83ab: /* ditto */
1225 case 0x54ab: /* SpaceSaver */
1226 case 0x84ab: /* ditto */
1227 #if 0
1228 case 0x90ab: /* 'G' */
1229 case 0x91ab: /* 'P' */
1230 case 0x92ab: /* 'A' */
1231 #endif
1232 *type = KB_101;
1233 break;
1234 case -1: /* AT 84 keyboard doesn't return ID */
1235 *type = KB_84;
1236 break;
1237 default:
1238 break;
1240 if (bootverbose)
1241 kprintf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1244 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
1245 * such as those on the IBM ThinkPad laptop computers can be used
1246 * with the standard console driver.
1248 if (codeset == 1) {
1249 if (send_kbd_command_and_data(kbdc,
1250 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1251 /* XT kbd doesn't need scan code translation */
1252 c &= ~KBD_TRANSLATION;
1253 } else {
1255 * KEYBOARD ERROR
1256 * The XT kbd isn't usable unless the proper scan
1257 * code set is selected.
1259 set_controller_command_byte(kbdc,
1260 KBD_KBD_CONTROL_BITS, c);
1261 kbdc_lock(kbdc, FALSE);
1262 kprintf("atkbd: unable to set the XT keyboard mode.\n");
1263 return EIO;
1267 #if 0
1268 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_EX_ENABLE, 0x71) != KBD_ACK)
1269 kprintf("atkbd: can't CMD_EX_ENABLE\n");
1271 if (send_kbd_command(kbdc, ATKBD_CMD_SETALL_MB) != KBD_ACK)
1272 kprintf("atkbd: can't SETALL_MB\n");
1273 if (send_kbd_command(kbdc, ATKBD_CMD_SETALL_MBR) != KBD_ACK)
1274 kprintf("atkbd: can't SETALL_MBR\n");
1275 #endif
1276 #if 0
1277 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SSCANSET, 2) != KBD_ACK)
1278 kprintf("atkbd: can't SSCANSET\n");
1279 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_GSCANSET, 0) != KBD_ACK)
1280 kprintf("atkbd: can't SSCANSET\n");
1281 else
1282 kprintf("atkbd: scanset %d\n", read_kbd_data(kbdc));
1283 #endif
1284 #if 0
1285 kprintf("atkbd: id %04x\n", get_kbd_id(kbdc, ATKBD_CMD_OK_GETID));
1286 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETLEDS, 0) != KBD_ACK)
1287 kprintf("atkbd: setleds failed\n");
1288 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETREP, 255) != KBD_ACK)
1289 kprintf("atkbd: setrep failed\n");
1290 if (send_kbd_command(kbdc, ATKBD_CMD_RESEND) != KBD_ACK)
1291 kprintf("atkbd: resend failed\n");
1292 #endif
1294 * Some keyboards require a SETLEDS command to be sent after
1295 * the reset command before they will send keystrokes to us
1296 * (Acer C720).
1298 if (send_kbd_command_and_data(kbdc, ATKBD_CMD_SETLEDS, 0) != KBD_ACK)
1299 kprintf("atkbd: setleds failed\n");
1300 send_kbd_command(kbdc, ATKBD_CMD_ENABLE);
1302 #if 0
1303 /* DEBUGGING */
1305 int retry;
1306 int c;
1307 kprintf("atkbd: waiting for keypress");
1308 for (retry = 0; retry < 10; ++retry) {
1309 c = read_kbd_data_no_wait(kbdc);
1310 kprintf(" %d", c);
1311 tsleep(&c, 0, "wait", hz);
1313 kprintf("\n");
1315 #endif
1317 if (mux_version == -1) {
1318 mux_mask = 0;
1319 mux_val = 0;
1320 } else {
1321 mux_mask = KBD_AUX_CONTROL_BITS;
1322 mux_val = 0;
1323 kprintf("atkbd: setaux for multiplexer\n");
1326 /* enable the keyboard port and intr. */
1327 if (!set_controller_command_byte(kbdc,
1328 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1329 KBD_OVERRIDE_KBD_LOCK | mux_mask,
1330 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1331 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT | mux_val)) {
1333 * CONTROLLER ERROR
1334 * This is serious; we are left with the disabled
1335 * keyboard intr.
1337 set_controller_command_byte(kbdc,
1338 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1339 KBD_OVERRIDE_KBD_LOCK | mux_mask,
1341 kbdc_lock(kbdc, FALSE);
1342 kprintf("atkbd: unable to enable the keyboard port and intr.\n");
1343 return EIO;
1346 kbdc_lock(kbdc, FALSE);
1347 return 0;
1350 #if 0
1352 static
1354 atkbd_setmuxmode(KBDC kbdc, int enable, int *mux_version)
1356 int param;
1357 int val;
1358 int i;
1360 kbdc->mux_active = 0;
1361 empty_both_buffers(kbdc, 100);
1362 val = 0xf0;
1363 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) {
1364 kprintf("setmuxmode: fail1\n");
1365 return(-1);
1367 val = enable ? 0x56 : 0xf6;
1368 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) {
1369 kprintf("setmuxmode: fail2\n");
1370 return(-1);
1372 val = enable ? 0xa4 : 0xa5;
1373 if ((param = write_controller_w1r1(kbdc, KBDC_AUX_LOOP, val)) != val) {
1374 kprintf("setmuxmode: fail3\n");
1375 return(-1);
1377 kprintf("mux version %02x\n", param);
1378 if (param == 0xac) {
1379 kprintf("setmuxmode: fail4\n");
1380 return(-1);
1383 if (enable) {
1384 for (i = 0; i < KBD_NUM_MUX_PORTS; ++i) {
1385 write_controller_command(kbdc, KBDC_MUX_PFX + i);
1386 write_controller_command(kbdc, KBDC_ENABLE_AUX_PORT);
1390 kbdc->mux_active = 1;
1391 if (mux_version)
1392 *mux_version = param;
1393 return 0;
1396 #endif
1398 static int
1399 write_kbd(KBDC kbdc, int command, int data)
1401 /* prevent the timeout routine from polling the keyboard */
1402 if (!kbdc_lock(kbdc, TRUE))
1403 return EBUSY;
1405 /* disable the keyboard and mouse interrupt */
1406 crit_enter();
1408 #if 0
1410 * XXX NOTE: We can't just disable the KBD port any more, even
1411 * temporarily, without blowing up some BIOS emulations
1412 * if not followed by a full reset.
1414 c = get_controller_command_byte(kbdc);
1415 if ((c == -1) ||
1416 !set_controller_command_byte(kbdc,
1417 KBD_KBD_CONTROL_BITS,
1418 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT)) {
1419 /* CONTROLLER ERROR */
1420 kbdc_lock(kbdc, FALSE);
1421 crit_exit();
1422 return EIO;
1425 * Now that the keyboard controller is told not to generate
1426 * the keyboard and mouse interrupts, call `splx()' to allow
1427 * the other tty interrupts. The clock interrupt may also occur,
1428 * but the timeout routine (`scrn_timer()') will be blocked
1429 * by the lock flag set via `kbdc_lock()'
1431 crit_exit();
1432 #endif
1434 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1435 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1437 #if 0
1438 /* restore the interrupts */
1439 if (!set_controller_command_byte(kbdc, KBD_KBD_CONTROL_BITS, c)) {
1440 /* CONTROLLER ERROR */
1442 #else
1443 crit_exit();
1444 #endif
1445 kbdc_lock(kbdc, FALSE);
1447 return 0;
1450 static int
1451 get_kbd_id(KBDC kbdc, int cmd)
1453 int id1, id2;
1455 empty_both_buffers(kbdc, 10);
1456 id1 = id2 = -1;
1457 if (send_kbd_command(kbdc, cmd) != KBD_ACK)
1458 return -1;
1460 DELAY(10000); /* 10 msec delay */
1461 id1 = read_kbd_data(kbdc);
1462 if (id1 != -1)
1463 id2 = read_kbd_data(kbdc);
1465 if ((id1 == -1) || (id2 == -1)) {
1466 empty_both_buffers(kbdc, 10);
1467 test_controller(kbdc);
1468 test_kbd_port(kbdc);
1469 return -1;
1471 return ((id2 << 8) | id1);
1474 static int delays[] = { 250, 500, 750, 1000 };
1475 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1476 68, 76, 84, 92, 100, 110, 118, 126,
1477 136, 152, 168, 184, 200, 220, 236, 252,
1478 272, 304, 336, 368, 400, 440, 472, 504 };
1480 static int
1481 typematic_delay(int i)
1483 return delays[(i >> 5) & 3];
1486 static int
1487 typematic_rate(int i)
1489 return rates[i & 0x1f];
1492 static int
1493 typematic(int delay, int rate)
1495 int value;
1496 int i;
1498 for (i = NELEM(delays) - 1; i > 0; --i) {
1499 if (delay >= delays[i])
1500 break;
1502 value = i << 5;
1503 for (i = NELEM(rates) - 1; i > 0; --i) {
1504 if (rate >= rates[i])
1505 break;
1507 value |= i;
1508 return value;