2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
33 #include "opt_atkbd.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/eventhandler.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
47 #if defined(__i386__) || defined(__amd64__)
48 #include <machine/md_var.h>
49 #include <machine/psl.h>
50 #include <compat/x86bios/x86bios.h>
51 #include <machine/pc/bios.h>
55 #include <vm/vm_param.h>
57 #include <isa/isareg.h>
58 #endif /* __i386__ || __amd64__ */
61 #include <dev/kbd/kbdreg.h>
62 #include <dev/atkbdc/atkbdreg.h>
63 #include <dev/atkbdc/atkbdcreg.h>
65 typedef struct atkbd_state {
66 KBDC kbdc; /* keyboard controller */
67 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
68 int ks_flags; /* flags */
69 #define COMPOSE (1 << 0)
71 int ks_state; /* shift/lock key state */
72 int ks_accents; /* accent key index (> 0) */
73 u_int ks_composed_char; /* composed char code (> 0) */
74 u_char ks_prefix; /* AT scan code prefix */
75 struct callout ks_timer;
78 static void atkbd_timeout(void *arg);
79 static void atkbd_shutdown_final(void *v);
80 static int atkbd_reset(KBDC kbdc, int flags, int c);
82 #define HAS_QUIRK(p, q) (((atkbdc_softc_t *)(p))->quirks & q)
83 #define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED)
86 atkbd_probe_unit(device_t dev, int irq, int flags)
88 keyboard_switch_t *sw;
92 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
96 args[0] = device_get_unit(device_get_parent(dev));
98 error = (*sw->probe)(device_get_unit(dev), args, flags);
105 atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags)
107 keyboard_switch_t *sw;
108 atkbd_state_t *state;
113 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
117 /* reset, initialize and enable the device */
118 unit = device_get_unit(dev);
119 args[0] = device_get_unit(device_get_parent(dev));
122 error = (*sw->probe)(unit, args, flags);
125 error = (*sw->init)(unit, kbd, args, flags);
130 #ifdef KBD_INSTALL_CDEV
131 /* attach a virtual keyboard cdev */
132 error = kbd_attach(*kbd);
138 * This is a kludge to compensate for lost keyboard interrupts.
139 * A similar code used to be in syscons. See below. XXX
141 state = (atkbd_state_t *)(*kbd)->kb_data;
142 callout_init(&state->ks_timer, 0);
146 (*sw->diag)(*kbd, bootverbose);
148 EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd,
149 SHUTDOWN_PRI_DEFAULT);
155 atkbd_timeout(void *arg)
157 atkbd_state_t *state;
162 * The original text of the following comments are extracted
163 * from syscons.c (1.287)
165 * With release 2.1 of the Xaccel server, the keyboard is left
166 * hanging pretty often. Apparently an interrupt from the
167 * keyboard is lost, and I don't know why (yet).
168 * This ugly hack calls the low-level interrupt routine if input
169 * is ready for the keyboard and conveniently hides the problem. XXX
171 * Try removing anything stuck in the keyboard controller; whether
172 * it's a keyboard scan code or mouse data. The low-level
173 * interrupt routine doesn't read the mouse data directly,
174 * but the keyboard controller driver will, as a side effect.
177 * And here is bde's original comment about this:
179 * This is necessary to handle edge triggered interrupts - if we
180 * returned when our IRQ is high due to unserviced input, then there
181 * would be no more keyboard IRQs until the keyboard is reset by
184 * The keyboard apparently unwedges the irq in most cases.
187 kbd = (keyboard_t *)arg;
188 if (kbdd_lock(kbd, TRUE)) {
190 * We have seen the lock flag is not set. Let's reset
191 * the flag early, otherwise the LED update routine fails
192 * which may want the lock during the interrupt routine.
194 kbdd_lock(kbd, FALSE);
195 if (kbdd_check_char(kbd))
196 kbdd_intr(kbd, NULL);
199 state = (atkbd_state_t *)kbd->kb_data;
200 callout_reset(&state->ks_timer, hz / 10, atkbd_timeout, arg);
205 #define ATKBD_DEFAULT 0
207 /* keyboard driver declaration */
208 static int atkbd_configure(int flags);
209 static kbd_probe_t atkbd_probe;
210 static kbd_init_t atkbd_init;
211 static kbd_term_t atkbd_term;
212 static kbd_intr_t atkbd_intr;
213 static kbd_test_if_t atkbd_test_if;
214 static kbd_enable_t atkbd_enable;
215 static kbd_disable_t atkbd_disable;
216 static kbd_read_t atkbd_read;
217 static kbd_check_t atkbd_check;
218 static kbd_read_char_t atkbd_read_char;
219 static kbd_check_char_t atkbd_check_char;
220 static kbd_ioctl_t atkbd_ioctl;
221 static kbd_lock_t atkbd_lock;
222 static kbd_clear_state_t atkbd_clear_state;
223 static kbd_get_state_t atkbd_get_state;
224 static kbd_set_state_t atkbd_set_state;
225 static kbd_poll_mode_t atkbd_poll;
227 static keyboard_switch_t atkbdsw = {
249 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
251 /* local functions */
252 static int get_typematic(keyboard_t *kbd);
253 static int setup_kbd_port(KBDC kbdc, int port, int intr);
254 static int get_kbd_echo(KBDC kbdc);
255 static int probe_keyboard(KBDC kbdc, int flags);
256 static int init_keyboard(KBDC kbdc, int *type, int flags);
257 static int write_kbd(KBDC kbdc, int command, int data);
258 static int get_kbd_id(KBDC kbdc);
259 static int typematic(int delay, int rate);
260 static int typematic_delay(int delay);
261 static int typematic_rate(int rate);
263 /* local variables */
265 /* the initial key map, accent map and fkey strings */
266 #ifdef ATKBD_DFLT_KEYMAP
267 #define KBD_DFLT_KEYMAP
268 #include "atkbdmap.h"
270 #include <dev/kbd/kbdtables.h>
272 /* structures for the default keyboard */
273 static keyboard_t default_kbd;
274 static atkbd_state_t default_kbd_state;
275 static keymap_t default_keymap;
276 static accentmap_t default_accentmap;
277 static fkeytab_t default_fkeytab[NUM_FKEYS];
280 * The back door to the keyboard driver!
281 * This function is called by the console driver, via the kbdio module,
282 * to tickle keyboard drivers when the low-level console is being initialized.
283 * Almost nothing in the kernel has been initialied yet. Try to probe
284 * keyboards if possible.
285 * NOTE: because of the way the low-level console is initialized, this routine
286 * may be called more than once!!
289 atkbd_configure(int flags)
296 * Probe the keyboard controller, if not present or if the driver
297 * is disabled, unregister the keyboard if any.
299 if (atkbdc_configure() != 0 ||
300 resource_disabled("atkbd", ATKBD_DEFAULT)) {
301 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
303 kbd = kbd_get_keyboard(i);
305 kbd->kb_flags &= ~KB_REGISTERED;
310 /* XXX: a kludge to obtain the device configuration flags */
311 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
314 /* probe the default keyboard */
318 if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
320 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
323 /* return the number of found keyboards */
327 /* low-level functions */
329 /* detect a keyboard */
331 atkbd_probe(int unit, void *arg, int flags)
334 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
337 if (unit == ATKBD_DEFAULT) {
338 if (KBD_IS_PROBED(&default_kbd))
342 kbdc = atkbdc_open(data[0]);
345 if (probe_keyboard(kbdc, flags)) {
346 if (flags & KB_CONF_FAIL_IF_NO_KBD)
352 /* reset and initialize the device */
354 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
357 atkbd_state_t *state;
363 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
367 if (unit == ATKBD_DEFAULT) {
368 *kbdp = kbd = &default_kbd;
369 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
371 state = &default_kbd_state;
372 keymap = &default_keymap;
373 accmap = &default_accentmap;
374 fkeymap = default_fkeytab;
376 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
378 } else if (*kbdp == NULL) {
379 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
380 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
381 /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
382 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
383 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
384 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
385 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
387 if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
388 || (accmap == NULL) || (fkeymap == NULL)) {
392 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
396 state = (atkbd_state_t *)kbd->kb_data;
397 bzero(state, sizeof(*state));
398 keymap = kbd->kb_keymap;
399 accmap = kbd->kb_accentmap;
400 fkeymap = kbd->kb_fkeytab;
401 fkeymap_size = kbd->kb_fkeytab_size;
405 if (!KBD_IS_PROBED(kbd)) {
406 state->kbdc = atkbdc_open(data[0]);
407 if (state->kbdc == NULL) {
411 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
413 bcopy(&key_map, keymap, sizeof(key_map));
414 bcopy(&accent_map, accmap, sizeof(accent_map));
415 bcopy(fkey_tab, fkeymap,
416 imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab)));
417 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
418 kbd->kb_data = (void *)state;
420 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
421 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
426 KBD_FOUND_DEVICE(kbd);
428 atkbd_clear_state(kbd);
429 state->ks_mode = K_XLATE;
431 * FIXME: set the initial value for lock keys in ks_state
432 * according to the BIOS data?
436 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
437 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
438 if (KBD_HAS_DEVICE(kbd)
439 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
440 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
445 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
447 delay[0] = kbd->kb_delay1;
448 delay[1] = kbd->kb_delay2;
449 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
452 if (!KBD_IS_CONFIGURED(kbd)) {
453 if (kbd_register(kbd) < 0) {
457 KBD_CONFIG_DONE(kbd);
464 free(state, M_DEVBUF);
466 free(keymap, M_DEVBUF);
468 free(accmap, M_DEVBUF);
470 free(fkeymap, M_DEVBUF);
473 *kbdp = NULL; /* insure ref doesn't leak to caller */
479 /* finish using this keyboard */
481 atkbd_term(keyboard_t *kbd)
483 atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
486 callout_drain(&state->ks_timer);
490 /* keyboard interrupt routine */
492 atkbd_intr(keyboard_t *kbd, void *arg)
494 atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
498 if (!KBD_HAS_DEVICE(kbd)) {
500 * The keyboard was not detected before;
501 * it must have been reconnected!
503 init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config);
504 KBD_FOUND_DEVICE(kbd);
505 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
507 delay[0] = kbd->kb_delay1;
508 delay[1] = kbd->kb_delay2;
509 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
512 if (state->ks_polling)
515 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
516 /* let the callback function to process the input */
517 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
518 kbd->kb_callback.kc_arg);
520 /* read and discard the input; no one is waiting for input */
522 c = atkbd_read_char(kbd, FALSE);
523 } while (c != NOKEY);
528 /* test the interface to the device */
530 atkbd_test_if(keyboard_t *kbd)
536 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
538 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
540 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
548 * Enable the access to the device; until this function is called,
549 * the client cannot read from the keyboard.
552 atkbd_enable(keyboard_t *kbd)
562 /* disallow the access to the device */
564 atkbd_disable(keyboard_t *kbd)
574 /* read one byte from the keyboard if it's allowed */
576 atkbd_read(keyboard_t *kbd, int wait)
581 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
583 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
586 return (KBD_IS_ACTIVE(kbd) ? c : -1);
589 /* check if data is waiting */
591 atkbd_check(keyboard_t *kbd)
593 if (!KBD_IS_ACTIVE(kbd))
595 return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
598 /* read char from the keyboard */
600 atkbd_read_char(keyboard_t *kbd, int wait)
602 atkbd_state_t *state;
607 state = (atkbd_state_t *)kbd->kb_data;
609 /* do we have a composed char to return? */
610 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
611 action = state->ks_composed_char;
612 state->ks_composed_char = 0;
613 if (action > UCHAR_MAX)
618 /* see if there is something in the keyboard port */
621 scancode = read_kbd_data(state->kbdc);
622 } while (scancode == -1);
624 scancode = read_kbd_data_no_wait(state->kbdc);
630 #if KBDIO_DEBUG >= 10
631 printf("atkbd_read_char(): scancode:0x%x\n", scancode);
634 /* return the byte as is for the K_RAW mode */
635 if (state->ks_mode == K_RAW)
638 /* translate the scan code into a keycode */
639 keycode = scancode & 0x7F;
640 switch (state->ks_prefix) {
641 case 0x00: /* normal scancode */
643 case 0xB8: /* left alt (compose key) released */
644 if (state->ks_flags & COMPOSE) {
645 state->ks_flags &= ~COMPOSE;
646 if (state->ks_composed_char > UCHAR_MAX)
647 state->ks_composed_char = 0;
650 case 0x38: /* left alt (compose key) pressed */
651 if (!(state->ks_flags & COMPOSE)) {
652 state->ks_flags |= COMPOSE;
653 state->ks_composed_char = 0;
658 state->ks_prefix = scancode;
662 case 0xE0: /* 0xE0 prefix */
663 state->ks_prefix = 0;
665 case 0x1C: /* right enter key */
668 case 0x1D: /* right ctrl key */
671 case 0x35: /* keypad divide key */
674 case 0x37: /* print scrn key */
677 case 0x38: /* right alt key (alt gr) */
680 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
683 case 0x47: /* grey home key */
686 case 0x48: /* grey up arrow key */
689 case 0x49: /* grey page up key */
692 case 0x4B: /* grey left arrow key */
695 case 0x4D: /* grey right arrow key */
698 case 0x4F: /* grey end key */
701 case 0x50: /* grey down arrow key */
704 case 0x51: /* grey page down key */
707 case 0x52: /* grey insert key */
710 case 0x53: /* grey delete key */
713 /* the following 3 are only used on the MS "Natural" keyboard */
714 case 0x5b: /* left Window key */
717 case 0x5c: /* right Window key */
720 case 0x5d: /* menu key */
723 case 0x5e: /* power key */
726 case 0x5f: /* sleep key */
729 case 0x63: /* wake key */
732 default: /* ignore everything else */
736 case 0xE1: /* 0xE1 prefix */
738 * The pause/break key on the 101 keyboard produces:
740 * Ctrl-pause/break produces:
741 * E0-46 E0-C6 (See above.)
743 state->ks_prefix = 0;
745 state->ks_prefix = 0x1D;
748 case 0x1D: /* pause / break */
749 state->ks_prefix = 0;
756 if (kbd->kb_type == KB_84) {
758 case 0x37: /* *(numpad)/print screen */
759 if (state->ks_flags & SHIFTS)
760 keycode = 0x5c; /* print screen */
762 case 0x45: /* num lock/pause */
763 if (state->ks_flags & CTLS)
764 keycode = 0x68; /* pause */
766 case 0x46: /* scroll lock/break */
767 if (state->ks_flags & CTLS)
768 keycode = 0x6c; /* break */
771 } else if (kbd->kb_type == KB_101) {
773 case 0x5c: /* print screen */
774 if (state->ks_flags & ALTS)
775 keycode = 0x54; /* sysrq */
777 case 0x68: /* pause/break */
778 if (state->ks_flags & CTLS)
779 keycode = 0x6c; /* break */
784 /* return the key code in the K_CODE mode */
785 if (state->ks_mode == K_CODE)
786 return (keycode | (scancode & 0x80));
788 /* compose a character code */
789 if (state->ks_flags & COMPOSE) {
790 switch (keycode | (scancode & 0x80)) {
791 /* key pressed, process it */
792 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
793 state->ks_composed_char *= 10;
794 state->ks_composed_char += keycode - 0x40;
795 if (state->ks_composed_char > UCHAR_MAX)
798 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
799 state->ks_composed_char *= 10;
800 state->ks_composed_char += keycode - 0x47;
801 if (state->ks_composed_char > UCHAR_MAX)
804 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
805 state->ks_composed_char *= 10;
806 state->ks_composed_char += keycode - 0x4E;
807 if (state->ks_composed_char > UCHAR_MAX)
810 case 0x52: /* keypad 0 */
811 state->ks_composed_char *= 10;
812 if (state->ks_composed_char > UCHAR_MAX)
816 /* key released, no interest here */
817 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
818 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
819 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
820 case 0xD2: /* keypad 0 */
823 case 0x38: /* left alt key */
827 if (state->ks_composed_char > 0) {
828 state->ks_flags &= ~COMPOSE;
829 state->ks_composed_char = 0;
836 /* keycode to key action */
837 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
838 &state->ks_state, &state->ks_accents);
845 /* check if char is waiting */
847 atkbd_check_char(keyboard_t *kbd)
849 atkbd_state_t *state;
851 if (!KBD_IS_ACTIVE(kbd))
853 state = (atkbd_state_t *)kbd->kb_data;
854 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
856 return kbdc_data_ready(state->kbdc);
859 /* some useful control functions */
861 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
863 /* translate LED_XXX bits into the device specific bits */
864 static u_char ledmap[8] = {
865 0, 4, 2, 6, 1, 5, 3, 7,
867 atkbd_state_t *state = kbd->kb_data;
871 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
872 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
879 case KDGKBMODE: /* get keyboard mode */
880 *(int *)arg = state->ks_mode;
882 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
883 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
885 ival = IOCPARM_IVAL(arg);
886 arg = (caddr_t)&ival;
889 case KDSKBMODE: /* set keyboard mode */
890 switch (*(int *)arg) {
892 if (state->ks_mode != K_XLATE) {
893 /* make lock key state and LED state match */
894 state->ks_state &= ~LOCK_MASK;
895 state->ks_state |= KBD_LED_VAL(kbd);
900 if (state->ks_mode != *(int *)arg) {
901 atkbd_clear_state(kbd);
902 state->ks_mode = *(int *)arg;
911 case KDGETLED: /* get keyboard LED */
912 *(int *)arg = KBD_LED_VAL(kbd);
914 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
915 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
917 ival = IOCPARM_IVAL(arg);
918 arg = (caddr_t)&ival;
921 case KDSETLED: /* set keyboard LED */
922 /* NOTE: lock key state in ks_state won't be changed */
923 if (*(int *)arg & ~LOCK_MASK) {
928 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
929 if (state->ks_mode == K_XLATE &&
930 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
936 if (KBD_HAS_DEVICE(kbd)) {
937 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
938 ledmap[i & LED_MASK]);
944 KBD_LED_VAL(kbd) = *(int *)arg;
947 case KDGKBSTATE: /* get lock key state */
948 *(int *)arg = state->ks_state & LOCK_MASK;
950 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
951 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
953 ival = IOCPARM_IVAL(arg);
954 arg = (caddr_t)&ival;
957 case KDSKBSTATE: /* set lock key state */
958 if (*(int *)arg & ~LOCK_MASK) {
962 state->ks_state &= ~LOCK_MASK;
963 state->ks_state |= *(int *)arg;
965 /* set LEDs and quit */
966 return atkbd_ioctl(kbd, KDSETLED, arg);
968 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
970 if (!KBD_HAS_DEVICE(kbd))
972 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
973 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
975 kbd->kb_delay1 = typematic_delay(i);
976 kbd->kb_delay2 = typematic_rate(i);
980 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
981 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
983 ival = IOCPARM_IVAL(arg);
984 arg = (caddr_t)&ival;
987 case KDSETRAD: /* set keyboard repeat rate (old interface) */
989 if (!KBD_HAS_DEVICE(kbd))
991 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
993 kbd->kb_delay1 = typematic_delay(*(int *)arg);
994 kbd->kb_delay2 = typematic_rate(*(int *)arg);
998 case PIO_KEYMAP: /* set keyboard translation table */
999 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
1000 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1001 case PIO_DEADKEYMAP: /* set accent key translation table */
1002 state->ks_accents = 0;
1006 return genkbd_commonioctl(kbd, cmd, arg);
1013 /* lock the access to the keyboard */
1015 atkbd_lock(keyboard_t *kbd, int lock)
1017 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
1020 /* clear the internal state of the keyboard */
1022 atkbd_clear_state(keyboard_t *kbd)
1024 atkbd_state_t *state;
1026 state = (atkbd_state_t *)kbd->kb_data;
1027 state->ks_flags = 0;
1028 state->ks_polling = 0;
1029 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1030 state->ks_accents = 0;
1031 state->ks_composed_char = 0;
1033 state->ks_prefix = 0; /* XXX */
1037 /* save the internal state */
1039 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1042 return sizeof(atkbd_state_t);
1043 if (len < sizeof(atkbd_state_t))
1045 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
1049 /* set the internal state */
1051 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1053 if (len < sizeof(atkbd_state_t))
1055 if (((atkbd_state_t *)kbd->kb_data)->kbdc
1056 != ((atkbd_state_t *)buf)->kbdc)
1058 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1063 atkbd_poll(keyboard_t *kbd, int on)
1065 atkbd_state_t *state;
1068 state = (atkbd_state_t *)kbd->kb_data;
1071 ++state->ks_polling;
1073 --state->ks_polling;
1079 atkbd_shutdown_final(void *v)
1082 keyboard_t *kbd = v;
1083 KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc;
1086 * Turn off the translation in preparation for handing the keyboard
1087 * over to the OFW as the OBP driver doesn't use translation and
1088 * also doesn't disable it itself resulting in a broken keymap at
1089 * the boot prompt. Also disable the aux port and the interrupts as
1090 * the OBP driver doesn't use them, i.e. polls the keyboard. Not
1091 * disabling the interrupts doesn't cause real problems but the
1092 * responsiveness is a bit better when they are turned off.
1094 send_kbd_command(kbdc, KBDC_DISABLE_KBD);
1095 set_controller_command_byte(kbdc,
1096 KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION,
1097 KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT);
1098 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1103 atkbd_reset(KBDC kbdc, int flags, int c)
1105 /* reset keyboard hardware */
1106 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1109 * Keyboard reset may fail either because the keyboard
1110 * doen't exist, or because the keyboard doesn't pass
1111 * the self-test, or the keyboard controller on the
1112 * motherboard and the keyboard somehow fail to shake hands.
1113 * It is just possible, particularly in the last case,
1114 * that the keyboard controller may be left in a hung state.
1115 * test_controller() and test_kbd_port() appear to bring
1116 * the keyboard controller back (I don't know why and how,
1119 empty_both_buffers(kbdc, 10);
1120 test_controller(kbdc);
1121 test_kbd_port(kbdc);
1123 * We could disable the keyboard port and interrupt... but,
1124 * the keyboard may still exist (see above).
1126 set_controller_command_byte(kbdc,
1127 ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1129 printf("atkbd: failed to reset the keyboard.\n");
1135 /* local functions */
1138 get_typematic(keyboard_t *kbd)
1140 #if defined(__i386__) || defined(__amd64__)
1142 * Only some systems allow us to retrieve the keyboard repeat
1143 * rate previously set via the BIOS...
1149 * Traditional entry points of int 0x15 and 0x16 are fixed
1150 * and later BIOSes follow them. (U)EFI CSM specification
1151 * also mandates these fixed entry points.
1153 * Validate the entry points here before we proceed further.
1154 * It's known that some recent laptops does not have the
1155 * same entry point and hang on boot if we call it.
1157 if (x86bios_get_intr(0x15) != 0xf000f859 ||
1158 x86bios_get_intr(0x16) != 0xf000e82e)
1161 /* Is BIOS system configuration table supported? */
1162 x86bios_init_regs(®s);
1164 x86bios_intr(®s, 0x15);
1165 if ((regs.R_FLG & PSL_C) != 0 || regs.R_AH != 0)
1168 /* Is int 0x16, function 0x09 supported? */
1169 p = x86bios_offset((regs.R_ES << 4) + regs.R_BX);
1170 if (readw(p) < 5 || (readb(p + 6) & 0x40) == 0)
1173 /* Is int 0x16, function 0x0306 supported? */
1174 x86bios_init_regs(®s);
1176 x86bios_intr(®s, 0x16);
1177 if ((regs.R_AL & 0x08) == 0)
1180 x86bios_init_regs(®s);
1182 x86bios_intr(®s, 0x16);
1183 kbd->kb_delay1 = typematic_delay(regs.R_BH << 5);
1184 kbd->kb_delay2 = typematic_rate(regs.R_BL);
1188 #endif /* __i386__ || __amd64__ */
1192 setup_kbd_port(KBDC kbdc, int port, int intr)
1194 if (!set_controller_command_byte(kbdc,
1195 KBD_KBD_CONTROL_BITS,
1196 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1197 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1203 get_kbd_echo(KBDC kbdc)
1205 /* enable the keyboard port, but disable the keyboard intr. */
1206 if (setup_kbd_port(kbdc, TRUE, FALSE))
1207 /* CONTROLLER ERROR: there is very little we can do... */
1210 /* see if something is present */
1211 write_kbd_command(kbdc, KBDC_ECHO);
1212 if (read_kbd_data(kbdc) != KBD_ECHO) {
1213 empty_both_buffers(kbdc, 10);
1214 test_controller(kbdc);
1215 test_kbd_port(kbdc);
1219 /* enable the keyboard port and intr. */
1220 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1223 * This is serious; the keyboard intr is left disabled!
1232 probe_keyboard(KBDC kbdc, int flags)
1235 * Don't try to print anything in this function. The low-level
1236 * console may not have been initialized yet...
1242 if (!kbdc_lock(kbdc, TRUE)) {
1247 /* temporarily block data transmission from the keyboard */
1248 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1250 /* flush any noise in the buffer */
1251 empty_both_buffers(kbdc, 100);
1253 /* save the current keyboard controller command byte */
1254 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1255 c = get_controller_command_byte(kbdc);
1257 /* CONTROLLER ERROR */
1258 kbdc_set_device_mask(kbdc, m);
1259 kbdc_lock(kbdc, FALSE);
1264 * The keyboard may have been screwed up by the boot block.
1265 * We may just be able to recover from error by testing the controller
1266 * and the keyboard port. The controller command byte needs to be
1267 * saved before this recovery operation, as some controllers seem
1268 * to set the command byte to particular values.
1270 test_controller(kbdc);
1271 if (!(flags & KB_CONF_NO_PROBE_TEST))
1272 test_kbd_port(kbdc);
1274 err = get_kbd_echo(kbdc);
1277 * Even if the keyboard doesn't seem to be present (err != 0),
1278 * we shall enable the keyboard port and interrupt so that
1279 * the driver will be operable when the keyboard is attached
1280 * to the system later. It is NOT recommended to hot-plug
1281 * the AT keyboard, but many people do so...
1283 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1284 setup_kbd_port(kbdc, TRUE, TRUE);
1287 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1289 /* try to restore the command byte as before */
1290 set_controller_command_byte(kbdc,
1291 ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1292 kbdc_set_device_mask(kbdc, m);
1296 kbdc_lock(kbdc, FALSE);
1297 return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err);
1301 init_keyboard(KBDC kbdc, int *type, int flags)
1307 if (!kbdc_lock(kbdc, TRUE)) {
1312 /* temporarily block data transmission from the keyboard */
1313 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1315 /* save the current controller command byte */
1316 empty_both_buffers(kbdc, 200);
1317 c = get_controller_command_byte(kbdc);
1319 /* CONTROLLER ERROR */
1320 kbdc_lock(kbdc, FALSE);
1321 printf("atkbd: unable to get the current command byte value.\n");
1325 printf("atkbd: the current kbd controller command byte %04x\n",
1328 /* override the keyboard lock switch */
1329 c |= KBD_OVERRIDE_KBD_LOCK;
1332 /* enable the keyboard port, but disable the keyboard intr. */
1333 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1334 /* CONTROLLER ERROR: there is very little we can do... */
1335 printf("atkbd: unable to set the command byte.\n");
1336 kbdc_lock(kbdc, FALSE);
1340 if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1341 atkbd_reset(kbdc, flags, c)) {
1342 kbdc_lock(kbdc, FALSE);
1347 * Check if we have an XT keyboard before we attempt to reset it.
1348 * The procedure assumes that the keyboard and the controller have
1349 * been set up properly by BIOS and have not been messed up
1350 * during the boot process.
1353 if (flags & KB_CONF_ALT_SCANCODESET)
1354 /* the user says there is a XT keyboard */
1356 #ifdef KBD_DETECT_XT_KEYBOARD
1357 else if ((c & KBD_TRANSLATION) == 0) {
1358 /* SET_SCANCODE_SET is not always supported; ignore error */
1359 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1361 codeset = read_kbd_data(kbdc);
1364 printf("atkbd: scancode set %d\n", codeset);
1365 #endif /* KBD_DETECT_XT_KEYBOARD */
1368 id = get_kbd_id(kbdc);
1370 case 0x41ab: /* 101/102/... Enhanced */
1371 case 0x83ab: /* ditto */
1372 case 0x54ab: /* SpaceSaver */
1373 case 0x84ab: /* ditto */
1375 case 0x90ab: /* 'G' */
1376 case 0x91ab: /* 'P' */
1377 case 0x92ab: /* 'A' */
1381 case -1: /* AT 84 keyboard doesn't return ID */
1388 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1390 if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
1391 atkbd_reset(kbdc, flags, c)) {
1392 kbdc_lock(kbdc, FALSE);
1397 * Allow us to set the XT_KEYBD flag so that keyboards
1398 * such as those on the IBM ThinkPad laptop computers can be used
1399 * with the standard console driver.
1402 if (send_kbd_command_and_data(kbdc,
1403 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1404 /* XT kbd doesn't need scan code translation */
1405 c &= ~KBD_TRANSLATION;
1409 * The XT kbd isn't usable unless the proper scan
1410 * code set is selected.
1412 set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1413 ? 0xff : KBD_KBD_CONTROL_BITS, c);
1414 kbdc_lock(kbdc, FALSE);
1415 printf("atkbd: unable to set the XT keyboard mode.\n");
1420 #if defined(__sparc64__)
1421 if (send_kbd_command_and_data(
1422 kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1423 printf("atkbd: can't set translation.\n");
1425 c |= KBD_TRANSLATION;
1429 * Some keyboards require a SETLEDS command to be sent after
1430 * the reset command before they will send keystrokes to us
1432 if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) &&
1433 send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) {
1434 printf("atkbd: setleds failed\n");
1436 if (!ALLOW_DISABLE_KBD(kbdc))
1437 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1439 /* enable the keyboard port and intr. */
1440 if (!set_controller_command_byte(kbdc,
1441 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1442 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1443 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1446 * This is serious; we are left with the disabled
1449 set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
1450 ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
1451 KBD_OVERRIDE_KBD_LOCK), c);
1452 kbdc_lock(kbdc, FALSE);
1453 printf("atkbd: unable to enable the keyboard port and intr.\n");
1457 kbdc_lock(kbdc, FALSE);
1462 write_kbd(KBDC kbdc, int command, int data)
1466 /* prevent the timeout routine from polling the keyboard */
1467 if (!kbdc_lock(kbdc, TRUE))
1470 /* disable the keyboard and mouse interrupt */
1473 c = get_controller_command_byte(kbdc);
1475 || !set_controller_command_byte(kbdc,
1476 kbdc_get_device_mask(kbdc),
1477 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1478 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1479 /* CONTROLLER ERROR */
1480 kbdc_lock(kbdc, FALSE);
1485 * Now that the keyboard controller is told not to generate
1486 * the keyboard and mouse interrupts, call `splx()' to allow
1487 * the other tty interrupts. The clock interrupt may also occur,
1488 * but the timeout routine (`scrn_timer()') will be blocked
1489 * by the lock flag set via `kbdc_lock()'
1493 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1494 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1496 /* restore the interrupts */
1497 if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc),
1498 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1499 /* CONTROLLER ERROR */
1504 kbdc_lock(kbdc, FALSE);
1510 get_kbd_id(KBDC kbdc)
1514 empty_both_buffers(kbdc, 10);
1516 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1519 DELAY(10000); /* 10 msec delay */
1520 id1 = read_kbd_data(kbdc);
1522 id2 = read_kbd_data(kbdc);
1524 if ((id1 == -1) || (id2 == -1)) {
1525 empty_both_buffers(kbdc, 10);
1526 test_controller(kbdc);
1527 test_kbd_port(kbdc);
1530 return ((id2 << 8) | id1);
1533 static int delays[] = { 250, 500, 750, 1000 };
1534 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1535 68, 76, 84, 92, 100, 110, 118, 126,
1536 136, 152, 168, 184, 200, 220, 236, 252,
1537 272, 304, 336, 368, 400, 440, 472, 504 };
1540 typematic_delay(int i)
1542 return delays[(i >> 5) & 3];
1546 typematic_rate(int i)
1548 return rates[i & 0x1f];
1552 typematic(int delay, int rate)
1557 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1558 if (delay >= delays[i])
1562 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1563 if (rate >= rates[i])