4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 $
29 * $DragonFly: src/sys/dev/misc/kbd/atkbd.c,v 1.14 2007/04/22 10:43:00 y0netan1 Exp $
32 * NOTE: All locks are handled by the kbd wrappers.
36 #include "opt_atkbd.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/thread2.h>
47 #include <machine/md_var.h>
48 #include <machine/psl.h>
49 #include <machine/vm86.h>
50 #include <machine/pc/bios.h>
59 #include "atkbdcreg.h"
61 #include <bus/isa/isareg.h>
63 static timeout_t atkbd_timeout;
66 atkbd_probe_unit(int unit, int ctlr, int irq, int flags)
68 keyboard_switch_t *sw;
72 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
79 error = (*sw->probe)(unit, args, flags);
87 atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
89 keyboard_switch_t *sw;
93 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
98 /* reset, initialize and enable the device */
102 error = (*sw->probe)(unit, args, flags);
106 error = (*sw->init)(unit, kbd, args, flags);
112 #ifdef KBD_INSTALL_CDEV
113 /* attach a virtual keyboard cdev */
114 error = kbd_attach(*kbd);
121 * This is a kludge to compensate for lost keyboard interrupts.
122 * A similar code used to be in syscons. See below. XXX
127 (*sw->diag)(*kbd, bootverbose);
133 atkbd_timeout(void *arg)
138 * The original text of the following comments are extracted
139 * from syscons.c (1.287)
141 * With release 2.1 of the Xaccel server, the keyboard is left
142 * hanging pretty often. Apparently an interrupt from the
143 * keyboard is lost, and I don't know why (yet).
144 * This ugly hack calls the low-level interrupt routine if input
145 * is ready for the keyboard and conveniently hides the problem. XXX
147 * Try removing anything stuck in the keyboard controller; whether
148 * it's a keyboard scan code or mouse data. The low-level
149 * interrupt routine doesn't read the mouse data directly,
150 * but the keyboard controller driver will, as a side effect.
153 * And here is bde's original comment about this:
155 * This is necessary to handle edge triggered interrupts - if we
156 * returned when our IRQ is high due to unserviced input, then there
157 * would be no more keyboard IRQs until the keyboard is reset by
160 * The keyboard apparently unwedges the irq in most cases.
163 kbd = (keyboard_t *)arg;
164 if (kbd_lock(kbd, TRUE)) {
166 * We have seen the lock flag is not set. Let's reset
167 * the flag early, otherwise the LED update routine fails
168 * which may want the lock during the interrupt routine.
170 kbd_lock(kbd, FALSE);
171 if (kbd_check_char(kbd))
174 callout_reset(&kbd->kb_atkbd_timeout_ch, hz / 10, atkbd_timeout, arg);
180 #include <machine/limits.h>
181 #include <machine/clock.h>
183 #define ATKBD_DEFAULT 0
185 typedef struct atkbd_state {
186 KBDC kbdc; /* keyboard controller */
187 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
188 int ks_flags; /* flags */
189 #define COMPOSE (1 << 0)
191 int ks_state; /* shift/lock key state */
192 int ks_accents; /* accent key index (> 0) */
193 u_int ks_composed_char; /* composed char code (> 0) */
194 u_char ks_prefix; /* AT scan code prefix */
197 /* keyboard driver declaration */
198 static int atkbd_configure(int flags);
199 static kbd_probe_t atkbd_probe;
200 static kbd_init_t atkbd_init;
201 static kbd_term_t atkbd_term;
202 static kbd_intr_t atkbd_intr;
203 static kbd_test_if_t atkbd_test_if;
204 static kbd_enable_t atkbd_enable;
205 static kbd_disable_t atkbd_disable;
206 static kbd_read_t atkbd_read;
207 static kbd_check_t atkbd_check;
208 static kbd_read_char_t atkbd_read_char;
209 static kbd_check_char_t atkbd_check_char;
210 static kbd_ioctl_t atkbd_ioctl;
211 static kbd_lock_t atkbd_lock;
212 static kbd_clear_state_t atkbd_clear_state;
213 static kbd_get_state_t atkbd_get_state;
214 static kbd_set_state_t atkbd_set_state;
215 static kbd_poll_mode_t atkbd_poll;
217 keyboard_switch_t atkbdsw = {
239 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
241 /* local functions */
242 static int get_typematic(keyboard_t *kbd);
243 static int setup_kbd_port(KBDC kbdc, int port, int intr);
244 static int get_kbd_echo(KBDC kbdc);
245 static int probe_keyboard(KBDC kbdc, int flags);
246 static int init_keyboard(KBDC kbdc, int *type, int flags);
247 static int write_kbd(KBDC kbdc, int command, int data);
248 static int get_kbd_id(KBDC kbdc);
249 static int typematic(int delay, int rate);
250 static int typematic_delay(int delay);
251 static int typematic_rate(int rate);
253 /* local variables */
255 /* the initial key map, accent map and fkey strings */
256 #ifdef ATKBD_DFLT_KEYMAP
257 #define KBD_DFLT_KEYMAP
258 #include "atkbdmap.h"
260 #include "kbdtables.h"
262 /* structures for the default keyboard */
263 static keyboard_t default_kbd;
264 static atkbd_state_t default_kbd_state;
265 static keymap_t default_keymap;
266 static accentmap_t default_accentmap;
267 static fkeytab_t default_fkeytab[NUM_FKEYS];
270 * The back door to the keyboard driver!
271 * This function is called by the console driver, via the kbdio module,
272 * to tickle keyboard drivers when the low-level console is being initialized.
273 * Almost nothing in the kernel has been initialied yet. Try to probe
274 * keyboards if possible.
275 * NOTE: because of the way the low-level console is initialized, this routine
276 * may be called more than once!!
279 atkbd_configure(int flags)
286 * Probe the keyboard controller, if not present or if the driver
287 * is disabled, unregister the keyboard if any.
289 if (atkbdc_configure() != 0 ||
290 resource_disabled("atkbd", ATKBD_DEFAULT)) {
291 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
293 kbd = kbd_get_keyboard(i);
295 /* kbd is stale after this */
301 /* XXX: a kludge to obtain the device configuration flags */
302 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
305 /* probe the default keyboard */
309 if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) {
312 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) {
316 /* return the number of found keyboards */
320 /* low-level functions */
322 /* detect a keyboard */
324 atkbd_probe(int unit, void *arg, int flags)
327 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
330 if (unit == ATKBD_DEFAULT) {
331 if (KBD_IS_PROBED(&default_kbd)) {
336 kbdc = atkbdc_open(data[0]);
340 if (probe_keyboard(kbdc, flags)) {
341 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
349 /* reset and initialize the device */
351 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
354 atkbd_state_t *state;
360 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
363 if (unit == ATKBD_DEFAULT) {
364 *kbdp = kbd = &default_kbd;
365 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) {
368 state = &default_kbd_state;
369 keymap = &default_keymap;
370 accmap = &default_accentmap;
371 fkeymap = default_fkeytab;
373 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
374 } else if (*kbdp == NULL) {
375 *kbdp = kbd = kmalloc(sizeof(*kbd), M_DEVBUF, M_WAITOK|M_ZERO);
376 state = kmalloc(sizeof(*state), M_DEVBUF, M_WAITOK|M_ZERO);
377 keymap = kmalloc(sizeof(key_map), M_DEVBUF, M_WAITOK);
378 accmap = kmalloc(sizeof(accent_map), M_DEVBUF, M_WAITOK);
379 fkeymap = kmalloc(sizeof(fkey_tab), M_DEVBUF, M_WAITOK);
380 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
381 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
385 state = (atkbd_state_t *)kbd->kb_data;
386 bzero(state, sizeof(*state));
387 keymap = kbd->kb_keymap;
388 accmap = kbd->kb_accentmap;
389 fkeymap = kbd->kb_fkeytab;
390 fkeymap_size = kbd->kb_fkeytab_size;
393 if (!KBD_IS_PROBED(kbd)) {
394 state->kbdc = atkbdc_open(data[0]);
395 if (state->kbdc == NULL) {
398 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
400 bcopy(&key_map, keymap, sizeof(key_map));
401 bcopy(&accent_map, accmap, sizeof(accent_map));
402 bcopy(fkey_tab, fkeymap,
403 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
404 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
405 kbd->kb_data = (void *)state;
407 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
408 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
412 KBD_FOUND_DEVICE(kbd);
414 atkbd_clear_state(kbd);
415 state->ks_mode = K_XLATE;
417 * FIXME: set the initial value for lock keys in ks_state
418 * according to the BIOS data?
422 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
423 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
424 if (KBD_HAS_DEVICE(kbd)
425 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
426 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
429 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
431 delay[0] = kbd->kb_delay1;
432 delay[1] = kbd->kb_delay2;
433 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
436 if (!KBD_IS_CONFIGURED(kbd)) {
437 if (kbd_register(kbd) < 0) {
440 KBD_CONFIG_DONE(kbd);
446 /* finish using this keyboard */
448 atkbd_term(keyboard_t *kbd)
454 /* keyboard interrupt routine */
456 atkbd_intr(keyboard_t *kbd, void *arg)
458 atkbd_state_t *state;
462 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
463 /* let the callback function to process the input */
464 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
465 kbd->kb_callback.kc_arg);
467 /* read and discard the input; no one is waiting for input */
469 c = atkbd_read_char(kbd, FALSE);
470 } while (c != NOKEY);
472 if (!KBD_HAS_DEVICE(kbd)) {
474 * The keyboard was not detected before;
475 * it must have been reconnected!
477 state = (atkbd_state_t *)kbd->kb_data;
478 init_keyboard(state->kbdc, &kbd->kb_type,
480 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
482 delay[0] = kbd->kb_delay1;
483 delay[1] = kbd->kb_delay2;
484 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
485 KBD_FOUND_DEVICE(kbd);
492 /* test the interface to the device */
494 atkbd_test_if(keyboard_t *kbd)
499 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
501 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
503 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
511 * Enable the access to the device; until this function is called,
512 * the client cannot read from the keyboard.
515 atkbd_enable(keyboard_t *kbd)
523 /* disallow the access to the device */
525 atkbd_disable(keyboard_t *kbd)
533 /* read one byte from the keyboard if it's allowed */
535 atkbd_read(keyboard_t *kbd, int wait)
540 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
542 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
546 ret = (KBD_IS_ACTIVE(kbd) ? c : -1);
551 /* check if data is waiting */
553 atkbd_check(keyboard_t *kbd)
558 if (!KBD_IS_ACTIVE(kbd)) {
561 ret = kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
566 /* read char from the keyboard */
568 atkbd_read_char(keyboard_t *kbd, int wait)
570 atkbd_state_t *state;
575 state = (atkbd_state_t *)kbd->kb_data;
577 /* do we have a composed char to return? */
578 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
579 action = state->ks_composed_char;
580 state->ks_composed_char = 0;
581 if (action > UCHAR_MAX) {
587 /* see if there is something in the keyboard port */
590 scancode = read_kbd_data(state->kbdc);
591 } while (scancode == -1);
593 scancode = read_kbd_data_no_wait(state->kbdc);
594 if (scancode == -1) {
600 #if KBDIO_DEBUG >= 10
601 kprintf("atkbd_read_char(): scancode:0x%x\n", scancode);
604 /* return the byte as is for the K_RAW mode */
605 if (state->ks_mode == K_RAW) {
609 /* translate the scan code into a keycode */
610 keycode = scancode & 0x7F;
611 switch (state->ks_prefix) {
612 case 0x00: /* normal scancode */
614 case 0xB8: /* left alt (compose key) released */
615 if (state->ks_flags & COMPOSE) {
616 state->ks_flags &= ~COMPOSE;
617 if (state->ks_composed_char > UCHAR_MAX)
618 state->ks_composed_char = 0;
621 case 0x38: /* left alt (compose key) pressed */
622 if (!(state->ks_flags & COMPOSE)) {
623 state->ks_flags |= COMPOSE;
624 state->ks_composed_char = 0;
629 state->ks_prefix = scancode;
633 case 0xE0: /* 0xE0 prefix */
634 state->ks_prefix = 0;
636 case 0x1C: /* right enter key */
639 case 0x1D: /* right ctrl key */
642 case 0x35: /* keypad divide key */
645 case 0x37: /* print scrn key */
648 case 0x38: /* right alt key (alt gr) */
651 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
654 case 0x47: /* grey home key */
657 case 0x48: /* grey up arrow key */
660 case 0x49: /* grey page up key */
663 case 0x4B: /* grey left arrow key */
666 case 0x4D: /* grey right arrow key */
669 case 0x4F: /* grey end key */
672 case 0x50: /* grey down arrow key */
675 case 0x51: /* grey page down key */
678 case 0x52: /* grey insert key */
681 case 0x53: /* grey delete key */
684 /* the following 3 are only used on the MS "Natural" keyboard */
685 case 0x5b: /* left Window key */
688 case 0x5c: /* right Window key */
691 case 0x5d: /* menu key */
694 default: /* ignore everything else */
698 case 0xE1: /* 0xE1 prefix */
700 * The pause/break key on the 101 keyboard produces:
702 * Ctrl-pause/break produces:
703 * E0-46 E0-C6 (See above.)
705 state->ks_prefix = 0;
707 state->ks_prefix = 0x1D;
710 case 0x1D: /* pause / break */
711 state->ks_prefix = 0;
718 if (kbd->kb_type == KB_84) {
720 case 0x37: /* *(numpad)/print screen */
721 if (state->ks_flags & SHIFTS)
722 keycode = 0x5c; /* print screen */
724 case 0x45: /* num lock/pause */
725 if (state->ks_flags & CTLS)
726 keycode = 0x68; /* pause */
728 case 0x46: /* scroll lock/break */
729 if (state->ks_flags & CTLS)
730 keycode = 0x6c; /* break */
733 } else if (kbd->kb_type == KB_101) {
735 case 0x5c: /* print screen */
736 if (state->ks_flags & ALTS)
737 keycode = 0x54; /* sysrq */
739 case 0x68: /* pause/break */
740 if (state->ks_flags & CTLS)
741 keycode = 0x6c; /* break */
746 /* return the key code in the K_CODE mode */
747 if (state->ks_mode == K_CODE) {
748 return (keycode | (scancode & 0x80));
751 /* compose a character code */
752 if (state->ks_flags & COMPOSE) {
753 switch (keycode | (scancode & 0x80)) {
754 /* key pressed, process it */
755 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
756 state->ks_composed_char *= 10;
757 state->ks_composed_char += keycode - 0x40;
758 if (state->ks_composed_char > UCHAR_MAX)
761 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
762 state->ks_composed_char *= 10;
763 state->ks_composed_char += keycode - 0x47;
764 if (state->ks_composed_char > UCHAR_MAX)
767 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
768 state->ks_composed_char *= 10;
769 state->ks_composed_char += keycode - 0x4E;
770 if (state->ks_composed_char > UCHAR_MAX)
773 case 0x52: /* keypad 0 */
774 state->ks_composed_char *= 10;
775 if (state->ks_composed_char > UCHAR_MAX)
779 /* key released, no interest here */
780 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
781 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
782 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
783 case 0xD2: /* keypad 0 */
786 case 0x38: /* left alt key */
790 if (state->ks_composed_char > 0) {
791 state->ks_flags &= ~COMPOSE;
792 state->ks_composed_char = 0;
799 /* keycode to key action */
800 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
801 &state->ks_state, &state->ks_accents);
802 if (action == NOKEY) {
809 /* check if char is waiting */
811 atkbd_check_char(keyboard_t *kbd)
813 atkbd_state_t *state;
816 if (!KBD_IS_ACTIVE(kbd)) {
819 state = (atkbd_state_t *)kbd->kb_data;
820 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
823 ret = kbdc_data_ready(state->kbdc);
827 /* some useful control functions */
829 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
831 /* trasnlate LED_XXX bits into the device specific bits */
832 static u_char ledmap[8] = {
833 0, 4, 2, 6, 1, 5, 3, 7,
835 atkbd_state_t *state = kbd->kb_data;
843 case KDGKBMODE: /* get keyboard mode */
844 *(int *)arg = state->ks_mode;
846 case KDSKBMODE: /* set keyboard mode */
847 switch (*(int *)arg) {
849 if (state->ks_mode != K_XLATE) {
850 /* make lock key state and LED state match */
851 state->ks_state &= ~LOCK_MASK;
852 state->ks_state |= KBD_LED_VAL(kbd);
857 if (state->ks_mode != *(int *)arg) {
858 atkbd_clear_state(kbd);
859 state->ks_mode = *(int *)arg;
868 case KDGETLED: /* get keyboard LED */
869 *(int *)arg = KBD_LED_VAL(kbd);
871 case KDSETLED: /* set keyboard LED */
872 /* NOTE: lock key state in ks_state won't be changed */
873 if (*(int *)arg & ~LOCK_MASK) {
878 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
879 if (state->ks_mode == K_XLATE &&
880 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
886 if (KBD_HAS_DEVICE(kbd)) {
887 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
888 ledmap[i & LED_MASK]);
894 KBD_LED_VAL(kbd) = *(int *)arg;
897 case KDGKBSTATE: /* get lock key state */
898 *(int *)arg = state->ks_state & LOCK_MASK;
900 case KDSKBSTATE: /* set lock key state */
901 if (*(int *)arg & ~LOCK_MASK) {
905 state->ks_state &= ~LOCK_MASK;
906 state->ks_state |= *(int *)arg;
908 /* set LEDs and quit */
909 return atkbd_ioctl(kbd, KDSETLED, arg);
911 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
913 if (!KBD_HAS_DEVICE(kbd)) {
916 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
917 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
919 kbd->kb_delay1 = typematic_delay(i);
920 kbd->kb_delay2 = typematic_rate(i);
924 case KDSETRAD: /* set keyboard repeat rate (old interface) */
926 if (!KBD_HAS_DEVICE(kbd)) {
929 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
931 kbd->kb_delay1 = typematic_delay(*(int *)arg);
932 kbd->kb_delay2 = typematic_rate(*(int *)arg);
936 case PIO_KEYMAP: /* set keyboard translation table */
937 case PIO_KEYMAPENT: /* set keyboard translation table entry */
938 case PIO_DEADKEYMAP: /* set accent key translation table */
939 state->ks_accents = 0;
943 return genkbd_commonioctl(kbd, cmd, arg);
950 /* lock the access to the keyboard */
952 atkbd_lock(keyboard_t *kbd, int lock)
954 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
957 /* clear the internal state of the keyboard */
959 atkbd_clear_state(keyboard_t *kbd)
961 atkbd_state_t *state;
963 state = (atkbd_state_t *)kbd->kb_data;
965 state->ks_polling = 0;
966 state->ks_state &= LOCK_MASK; /* preserve locking key state */
967 state->ks_accents = 0;
968 state->ks_composed_char = 0;
970 state->ks_prefix = 0; /* XXX */
974 /* save the internal state */
976 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
979 return sizeof(atkbd_state_t);
980 if (len < sizeof(atkbd_state_t))
983 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
987 /* set the internal state */
989 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
991 if (len < sizeof(atkbd_state_t))
993 if (((atkbd_state_t *)kbd->kb_data)->kbdc
994 != ((atkbd_state_t *)buf)->kbdc)
996 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1001 atkbd_poll(keyboard_t *kbd, int on)
1003 atkbd_state_t *state;
1005 state = (atkbd_state_t *)kbd->kb_data;
1008 state->ks_polling = 1;
1010 state->ks_polling = 0;
1015 /* local functions */
1018 get_typematic(keyboard_t *kbd)
1022 * Only some systems allow us to retrieve the keyboard repeat
1023 * rate previously set via the BIOS...
1025 struct vm86frame vmf;
1028 bzero(&vmf, sizeof(vmf));
1029 vmf.vmf_ax = 0xc000;
1030 vm86_intcall(0x15, &vmf);
1031 if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
1033 p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
1034 if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */
1036 vmf.vmf_ax = 0x0900;
1037 vm86_intcall(0x16, &vmf);
1038 if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */
1040 vmf.vmf_ax = 0x0306;
1041 vm86_intcall(0x16, &vmf);
1042 kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
1043 kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
1047 #endif /* __i386__ */
1051 setup_kbd_port(KBDC kbdc, int port, int intr)
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)))
1062 get_kbd_echo(KBDC kbdc)
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... */
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);
1078 /* enable the keyboard port and intr. */
1079 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1082 * This is serious; the keyboard intr is left disabled!
1091 probe_keyboard(KBDC kbdc, int flags)
1094 * Don't try to print anything in this function. The low-level
1095 * console may not have been initialized yet...
1101 if (!kbdc_lock(kbdc, TRUE)) {
1106 /* temporarily block data transmission from the keyboard */
1107 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1109 /* flush any noise in the buffer */
1110 empty_both_buffers(kbdc, 100);
1112 /* save the current keyboard controller command byte */
1113 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1114 c = get_controller_command_byte(kbdc);
1116 /* CONTROLLER ERROR */
1117 kbdc_set_device_mask(kbdc, m);
1118 kbdc_lock(kbdc, FALSE);
1123 * The keyboard may have been screwed up by the boot block.
1124 * We may just be able to recover from error by testing the controller
1125 * and the keyboard port. The controller command byte needs to be
1126 * saved before this recovery operation, as some controllers seem
1127 * to set the command byte to particular values.
1129 test_controller(kbdc);
1130 test_kbd_port(kbdc);
1132 err = get_kbd_echo(kbdc);
1135 * Even if the keyboard doesn't seem to be present (err != 0),
1136 * we shall enable the keyboard port and interrupt so that
1137 * the driver will be operable when the keyboard is attached
1138 * to the system later. It is NOT recommended to hot-plug
1139 * the AT keyboard, but many people do so...
1141 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1142 setup_kbd_port(kbdc, TRUE, TRUE);
1145 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1147 /* try to restore the command byte as before */
1148 set_controller_command_byte(kbdc, 0xff, c);
1149 kbdc_set_device_mask(kbdc, m);
1153 kbdc_lock(kbdc, FALSE);
1158 init_keyboard(KBDC kbdc, int *type, int flags)
1164 if (!kbdc_lock(kbdc, TRUE)) {
1169 /* temporarily block data transmission from the keyboard */
1170 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1172 /* save the current controller command byte */
1173 empty_both_buffers(kbdc, 200);
1174 c = get_controller_command_byte(kbdc);
1176 /* CONTROLLER ERROR */
1177 kbdc_lock(kbdc, FALSE);
1178 kprintf("atkbd: unable to get the current command byte value.\n");
1182 kprintf("atkbd: the current kbd controller command byte %04x\n",
1185 /* override the keyboard lock switch */
1186 c |= KBD_OVERRIDE_KBD_LOCK;
1189 /* enable the keyboard port, but disable the keyboard intr. */
1190 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1191 /* CONTROLLER ERROR: there is very little we can do... */
1192 kprintf("atkbd: unable to set the command byte.\n");
1193 kbdc_lock(kbdc, FALSE);
1198 * Check if we have an XT keyboard before we attempt to reset it.
1199 * The procedure assumes that the keyboard and the controller have
1200 * been set up properly by BIOS and have not been messed up
1201 * during the boot process.
1204 if (flags & KB_CONF_ALT_SCANCODESET)
1205 /* the user says there is a XT keyboard */
1207 #ifdef KBD_DETECT_XT_KEYBOARD
1208 else if ((c & KBD_TRANSLATION) == 0) {
1209 /* SET_SCANCODE_SET is not always supported; ignore error */
1210 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1212 codeset = read_kbd_data(kbdc);
1215 kprintf("atkbd: scancode set %d\n", codeset);
1216 #endif /* KBD_DETECT_XT_KEYBOARD */
1219 id = get_kbd_id(kbdc);
1221 case 0x41ab: /* 101/102/... Enhanced */
1222 case 0x83ab: /* ditto */
1223 case 0x54ab: /* SpaceSaver */
1224 case 0x84ab: /* ditto */
1226 case 0x90ab: /* 'G' */
1227 case 0x91ab: /* 'P' */
1228 case 0x92ab: /* 'A' */
1232 case -1: /* AT 84 keyboard doesn't return ID */
1239 kprintf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1241 /* reset keyboard hardware */
1242 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1245 * Keyboard reset may fail either because the keyboard
1246 * doen't exist, or because the keyboard doesn't pass
1247 * the self-test, or the keyboard controller on the
1248 * motherboard and the keyboard somehow fail to shake hands.
1249 * It is just possible, particularly in the last case,
1250 * that the keyoard controller may be left in a hung state.
1251 * test_controller() and test_kbd_port() appear to bring
1252 * the keyboard controller back (I don't know why and how,
1255 empty_both_buffers(kbdc, 10);
1256 test_controller(kbdc);
1257 test_kbd_port(kbdc);
1259 * We could disable the keyboard port and interrupt... but,
1260 * the keyboard may still exist (see above).
1262 set_controller_command_byte(kbdc, 0xff, c);
1263 kbdc_lock(kbdc, FALSE);
1265 kprintf("atkbd: failed to reset the keyboard.\n");
1270 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
1271 * such as those on the IBM ThinkPad laptop computers can be used
1272 * with the standard console driver.
1275 if (send_kbd_command_and_data(kbdc,
1276 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1277 /* XT kbd doesn't need scan code translation */
1278 c &= ~KBD_TRANSLATION;
1282 * The XT kbd isn't usable unless the proper scan
1283 * code set is selected.
1285 set_controller_command_byte(kbdc, 0xff, c);
1286 kbdc_lock(kbdc, FALSE);
1287 kprintf("atkbd: unable to set the XT keyboard mode.\n");
1292 /* enable the keyboard port and intr. */
1293 if (!set_controller_command_byte(kbdc,
1294 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1295 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1296 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1299 * This is serious; we are left with the disabled
1302 set_controller_command_byte(kbdc, 0xff, c);
1303 kbdc_lock(kbdc, FALSE);
1304 kprintf("atkbd: unable to enable the keyboard port and intr.\n");
1308 kbdc_lock(kbdc, FALSE);
1313 write_kbd(KBDC kbdc, int command, int data)
1315 /* prevent the timeout routine from polling the keyboard */
1316 if (!kbdc_lock(kbdc, TRUE))
1319 /* disable the keyboard and mouse interrupt */
1322 c = get_controller_command_byte(kbdc);
1324 || !set_controller_command_byte(kbdc,
1325 kbdc_get_device_mask(kbdc),
1326 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1327 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1328 /* CONTROLLER ERROR */
1329 kbdc_lock(kbdc, FALSE);
1334 * Now that the keyboard controller is told not to generate
1335 * the keyboard and mouse interrupts, call `splx()' to allow
1336 * the other tty interrupts. The clock interrupt may also occur,
1337 * but the timeout routine (`scrn_timer()') will be blocked
1338 * by the lock flag set via `kbdc_lock()'
1343 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1344 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1347 /* restore the interrupts */
1348 if (!set_controller_command_byte(kbdc,
1349 kbdc_get_device_mask(kbdc),
1350 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1351 /* CONTROLLER ERROR */
1356 kbdc_lock(kbdc, FALSE);
1362 get_kbd_id(KBDC kbdc)
1366 empty_both_buffers(kbdc, 10);
1368 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1371 DELAY(10000); /* 10 msec delay */
1372 id1 = read_kbd_data(kbdc);
1374 id2 = read_kbd_data(kbdc);
1376 if ((id1 == -1) || (id2 == -1)) {
1377 empty_both_buffers(kbdc, 10);
1378 test_controller(kbdc);
1379 test_kbd_port(kbdc);
1382 return ((id2 << 8) | id1);
1385 static int delays[] = { 250, 500, 750, 1000 };
1386 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1387 68, 76, 84, 92, 100, 110, 118, 126,
1388 136, 152, 168, 184, 200, 220, 236, 252,
1389 272, 304, 336, 368, 400, 440, 472, 504 };
1392 typematic_delay(int i)
1394 return delays[(i >> 5) & 3];
1398 typematic_rate(int i)
1400 return rates[i & 0x1f];
1404 typematic(int delay, int rate)
1409 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1410 if (delay >= delays[i])
1414 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1415 if (rate >= rates[i])