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>
48 #include <dev/kbd/kbdreg.h>
49 #include <dev/atkbdc/atkbdreg.h>
50 #include <dev/atkbdc/atkbdcreg.h>
52 static timeout_t atkbd_timeout;
53 static void atkbd_shutdown_final(void *v);
55 #define DEFAULT_DELAY 0x1 /* 500ms */
56 #define DEFAULT_RATE 0x10 /* 14Hz */
59 atkbd_probe_unit(device_t dev, int irq, int flags)
61 keyboard_switch_t *sw;
65 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
69 args[0] = device_get_unit(device_get_parent(dev));
71 error = (*sw->probe)(device_get_unit(dev), args, flags);
78 atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags)
80 keyboard_switch_t *sw;
85 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
89 /* reset, initialize and enable the device */
90 unit = device_get_unit(dev);
91 args[0] = device_get_unit(device_get_parent(dev));
94 error = (*sw->probe)(unit, args, flags);
97 error = (*sw->init)(unit, kbd, args, flags);
102 #ifdef KBD_INSTALL_CDEV
103 /* attach a virtual keyboard cdev */
104 error = kbd_attach(*kbd);
110 * This is a kludge to compensate for lost keyboard interrupts.
111 * A similar code used to be in syscons. See below. XXX
116 (*sw->diag)(*kbd, bootverbose);
118 EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd,
119 SHUTDOWN_PRI_DEFAULT);
125 atkbd_timeout(void *arg)
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
153 * The keyboard apparently unwedges the irq in most cases.
156 kbd = (keyboard_t *)arg;
157 if (kbdd_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 kbdd_lock(kbd, FALSE);
164 if (kbdd_check_char(kbd))
165 kbdd_intr(kbd, NULL);
168 timeout(atkbd_timeout, arg, hz/10);
173 #define ATKBD_DEFAULT 0
175 typedef struct atkbd_state {
176 KBDC kbdc; /* keyboard controller */
177 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
178 int ks_flags; /* flags */
179 #define COMPOSE (1 << 0)
181 int ks_state; /* shift/lock key state */
182 int ks_accents; /* accent key index (> 0) */
183 u_int ks_composed_char; /* composed char code (> 0) */
184 u_char ks_prefix; /* AT scan code prefix */
187 /* keyboard driver declaration */
188 static int atkbd_configure(int flags);
189 static kbd_probe_t atkbd_probe;
190 static kbd_init_t atkbd_init;
191 static kbd_term_t atkbd_term;
192 static kbd_intr_t atkbd_intr;
193 static kbd_test_if_t atkbd_test_if;
194 static kbd_enable_t atkbd_enable;
195 static kbd_disable_t atkbd_disable;
196 static kbd_read_t atkbd_read;
197 static kbd_check_t atkbd_check;
198 static kbd_read_char_t atkbd_read_char;
199 static kbd_check_char_t atkbd_check_char;
200 static kbd_ioctl_t atkbd_ioctl;
201 static kbd_lock_t atkbd_lock;
202 static kbd_clear_state_t atkbd_clear_state;
203 static kbd_get_state_t atkbd_get_state;
204 static kbd_set_state_t atkbd_set_state;
205 static kbd_poll_mode_t atkbd_poll;
207 static keyboard_switch_t atkbdsw = {
229 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
231 /* local functions */
232 static int set_typematic(keyboard_t *kbd);
233 static int setup_kbd_port(KBDC kbdc, int port, int intr);
234 static int get_kbd_echo(KBDC kbdc);
235 static int probe_keyboard(KBDC kbdc, int flags);
236 static int init_keyboard(KBDC kbdc, int *type, int flags);
237 static int write_kbd(KBDC kbdc, int command, int data);
238 static int get_kbd_id(KBDC kbdc);
239 static int typematic(int delay, int rate);
240 static int typematic_delay(int delay);
241 static int typematic_rate(int rate);
243 /* local variables */
245 /* the initial key map, accent map and fkey strings */
246 #ifdef ATKBD_DFLT_KEYMAP
247 #define KBD_DFLT_KEYMAP
248 #include "atkbdmap.h"
250 #include <dev/kbd/kbdtables.h>
252 /* structures for the default keyboard */
253 static keyboard_t default_kbd;
254 static atkbd_state_t default_kbd_state;
255 static keymap_t default_keymap;
256 static accentmap_t default_accentmap;
257 static fkeytab_t default_fkeytab[NUM_FKEYS];
260 * The back door to the keyboard driver!
261 * This function is called by the console driver, via the kbdio module,
262 * to tickle keyboard drivers when the low-level console is being initialized.
263 * Almost nothing in the kernel has been initialied yet. Try to probe
264 * keyboards if possible.
265 * NOTE: because of the way the low-level console is initialized, this routine
266 * may be called more than once!!
269 atkbd_configure(int flags)
276 * Probe the keyboard controller, if not present or if the driver
277 * is disabled, unregister the keyboard if any.
279 if (atkbdc_configure() != 0 ||
280 resource_disabled("atkbd", ATKBD_DEFAULT)) {
281 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
283 kbd = kbd_get_keyboard(i);
285 kbd->kb_flags &= ~KB_REGISTERED;
290 /* XXX: a kludge to obtain the device configuration flags */
291 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
294 /* probe the default keyboard */
298 if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
300 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
303 /* return the number of found keyboards */
307 /* low-level functions */
309 /* detect a keyboard */
311 atkbd_probe(int unit, void *arg, int flags)
314 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
317 if (unit == ATKBD_DEFAULT) {
318 if (KBD_IS_PROBED(&default_kbd))
322 kbdc = atkbdc_open(data[0]);
325 if (probe_keyboard(kbdc, flags)) {
326 if (flags & KB_CONF_FAIL_IF_NO_KBD)
332 /* reset and initialize the device */
334 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
337 atkbd_state_t *state;
343 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
347 if (unit == ATKBD_DEFAULT) {
348 *kbdp = kbd = &default_kbd;
349 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
351 state = &default_kbd_state;
352 keymap = &default_keymap;
353 accmap = &default_accentmap;
354 fkeymap = default_fkeytab;
356 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
358 } else if (*kbdp == NULL) {
359 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
360 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
361 /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
362 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
363 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
364 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
365 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
367 if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
368 || (accmap == NULL) || (fkeymap == NULL)) {
372 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
376 state = (atkbd_state_t *)kbd->kb_data;
377 bzero(state, sizeof(*state));
378 keymap = kbd->kb_keymap;
379 accmap = kbd->kb_accentmap;
380 fkeymap = kbd->kb_fkeytab;
381 fkeymap_size = kbd->kb_fkeytab_size;
385 if (!KBD_IS_PROBED(kbd)) {
386 state->kbdc = atkbdc_open(data[0]);
387 if (state->kbdc == NULL) {
391 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
393 bcopy(&key_map, keymap, sizeof(key_map));
394 bcopy(&accent_map, accmap, sizeof(accent_map));
395 bcopy(fkey_tab, fkeymap,
396 imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab)));
397 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
398 kbd->kb_data = (void *)state;
400 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
401 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
406 KBD_FOUND_DEVICE(kbd);
408 atkbd_clear_state(kbd);
409 state->ks_mode = K_XLATE;
411 * FIXME: set the initial value for lock keys in ks_state
412 * according to the BIOS data?
416 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
417 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
418 if (KBD_HAS_DEVICE(kbd)
419 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
420 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
425 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
427 delay[0] = kbd->kb_delay1;
428 delay[1] = kbd->kb_delay2;
429 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
432 if (!KBD_IS_CONFIGURED(kbd)) {
433 if (kbd_register(kbd) < 0) {
437 KBD_CONFIG_DONE(kbd);
444 free(state, M_DEVBUF);
446 free(keymap, M_DEVBUF);
448 free(accmap, M_DEVBUF);
450 free(fkeymap, M_DEVBUF);
453 *kbdp = NULL; /* insure ref doesn't leak to caller */
459 /* finish using this keyboard */
461 atkbd_term(keyboard_t *kbd)
467 /* keyboard interrupt routine */
469 atkbd_intr(keyboard_t *kbd, void *arg)
471 atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
475 if (!KBD_HAS_DEVICE(kbd)) {
477 * The keyboard was not detected before;
478 * it must have been reconnected!
480 init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config);
481 KBD_FOUND_DEVICE(kbd);
482 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
484 delay[0] = kbd->kb_delay1;
485 delay[1] = kbd->kb_delay2;
486 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
489 if (state->ks_polling)
492 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
493 /* let the callback function to process the input */
494 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
495 kbd->kb_callback.kc_arg);
497 /* read and discard the input; no one is waiting for input */
499 c = atkbd_read_char(kbd, FALSE);
500 } while (c != NOKEY);
505 /* test the interface to the device */
507 atkbd_test_if(keyboard_t *kbd)
513 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
515 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
517 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
525 * Enable the access to the device; until this function is called,
526 * the client cannot read from the keyboard.
529 atkbd_enable(keyboard_t *kbd)
539 /* disallow the access to the device */
541 atkbd_disable(keyboard_t *kbd)
551 /* read one byte from the keyboard if it's allowed */
553 atkbd_read(keyboard_t *kbd, int wait)
558 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
560 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
563 return (KBD_IS_ACTIVE(kbd) ? c : -1);
566 /* check if data is waiting */
568 atkbd_check(keyboard_t *kbd)
570 if (!KBD_IS_ACTIVE(kbd))
572 return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
575 /* read char from the keyboard */
577 atkbd_read_char(keyboard_t *kbd, int wait)
579 atkbd_state_t *state;
584 state = (atkbd_state_t *)kbd->kb_data;
586 /* do we have a composed char to return? */
587 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
588 action = state->ks_composed_char;
589 state->ks_composed_char = 0;
590 if (action > UCHAR_MAX)
595 /* see if there is something in the keyboard port */
598 scancode = read_kbd_data(state->kbdc);
599 } while (scancode == -1);
601 scancode = read_kbd_data_no_wait(state->kbdc);
607 #if KBDIO_DEBUG >= 10
608 printf("atkbd_read_char(): scancode:0x%x\n", scancode);
611 /* return the byte as is for the K_RAW mode */
612 if (state->ks_mode == K_RAW)
615 /* translate the scan code into a keycode */
616 keycode = scancode & 0x7F;
617 switch (state->ks_prefix) {
618 case 0x00: /* normal scancode */
620 case 0xB8: /* left alt (compose key) released */
621 if (state->ks_flags & COMPOSE) {
622 state->ks_flags &= ~COMPOSE;
623 if (state->ks_composed_char > UCHAR_MAX)
624 state->ks_composed_char = 0;
627 case 0x38: /* left alt (compose key) pressed */
628 if (!(state->ks_flags & COMPOSE)) {
629 state->ks_flags |= COMPOSE;
630 state->ks_composed_char = 0;
635 state->ks_prefix = scancode;
639 case 0xE0: /* 0xE0 prefix */
640 state->ks_prefix = 0;
642 case 0x1C: /* right enter key */
645 case 0x1D: /* right ctrl key */
648 case 0x35: /* keypad divide key */
651 case 0x37: /* print scrn key */
654 case 0x38: /* right alt key (alt gr) */
657 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
660 case 0x47: /* grey home key */
663 case 0x48: /* grey up arrow key */
666 case 0x49: /* grey page up key */
669 case 0x4B: /* grey left arrow key */
672 case 0x4D: /* grey right arrow key */
675 case 0x4F: /* grey end key */
678 case 0x50: /* grey down arrow key */
681 case 0x51: /* grey page down key */
684 case 0x52: /* grey insert key */
687 case 0x53: /* grey delete key */
690 /* the following 3 are only used on the MS "Natural" keyboard */
691 case 0x5b: /* left Window key */
694 case 0x5c: /* right Window key */
697 case 0x5d: /* menu key */
700 case 0x5e: /* power key */
703 case 0x5f: /* sleep key */
706 case 0x63: /* wake key */
709 default: /* ignore everything else */
713 case 0xE1: /* 0xE1 prefix */
715 * The pause/break key on the 101 keyboard produces:
717 * Ctrl-pause/break produces:
718 * E0-46 E0-C6 (See above.)
720 state->ks_prefix = 0;
722 state->ks_prefix = 0x1D;
725 case 0x1D: /* pause / break */
726 state->ks_prefix = 0;
733 if (kbd->kb_type == KB_84) {
735 case 0x37: /* *(numpad)/print screen */
736 if (state->ks_flags & SHIFTS)
737 keycode = 0x5c; /* print screen */
739 case 0x45: /* num lock/pause */
740 if (state->ks_flags & CTLS)
741 keycode = 0x68; /* pause */
743 case 0x46: /* scroll lock/break */
744 if (state->ks_flags & CTLS)
745 keycode = 0x6c; /* break */
748 } else if (kbd->kb_type == KB_101) {
750 case 0x5c: /* print screen */
751 if (state->ks_flags & ALTS)
752 keycode = 0x54; /* sysrq */
754 case 0x68: /* pause/break */
755 if (state->ks_flags & CTLS)
756 keycode = 0x6c; /* break */
761 /* return the key code in the K_CODE mode */
762 if (state->ks_mode == K_CODE)
763 return (keycode | (scancode & 0x80));
765 /* compose a character code */
766 if (state->ks_flags & COMPOSE) {
767 switch (keycode | (scancode & 0x80)) {
768 /* key pressed, process it */
769 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
770 state->ks_composed_char *= 10;
771 state->ks_composed_char += keycode - 0x40;
772 if (state->ks_composed_char > UCHAR_MAX)
775 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
776 state->ks_composed_char *= 10;
777 state->ks_composed_char += keycode - 0x47;
778 if (state->ks_composed_char > UCHAR_MAX)
781 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
782 state->ks_composed_char *= 10;
783 state->ks_composed_char += keycode - 0x4E;
784 if (state->ks_composed_char > UCHAR_MAX)
787 case 0x52: /* keypad 0 */
788 state->ks_composed_char *= 10;
789 if (state->ks_composed_char > UCHAR_MAX)
793 /* key released, no interest here */
794 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
795 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
796 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
797 case 0xD2: /* keypad 0 */
800 case 0x38: /* left alt key */
804 if (state->ks_composed_char > 0) {
805 state->ks_flags &= ~COMPOSE;
806 state->ks_composed_char = 0;
813 /* keycode to key action */
814 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
815 &state->ks_state, &state->ks_accents);
822 /* check if char is waiting */
824 atkbd_check_char(keyboard_t *kbd)
826 atkbd_state_t *state;
828 if (!KBD_IS_ACTIVE(kbd))
830 state = (atkbd_state_t *)kbd->kb_data;
831 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
833 return kbdc_data_ready(state->kbdc);
836 /* some useful control functions */
838 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
840 /* translate LED_XXX bits into the device specific bits */
841 static u_char ledmap[8] = {
842 0, 4, 2, 6, 1, 5, 3, 7,
844 atkbd_state_t *state = kbd->kb_data;
848 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
849 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
856 case KDGKBMODE: /* get keyboard mode */
857 *(int *)arg = state->ks_mode;
859 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
860 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
862 ival = IOCPARM_IVAL(arg);
863 arg = (caddr_t)&ival;
866 case KDSKBMODE: /* set keyboard mode */
867 switch (*(int *)arg) {
869 if (state->ks_mode != K_XLATE) {
870 /* make lock key state and LED state match */
871 state->ks_state &= ~LOCK_MASK;
872 state->ks_state |= KBD_LED_VAL(kbd);
877 if (state->ks_mode != *(int *)arg) {
878 atkbd_clear_state(kbd);
879 state->ks_mode = *(int *)arg;
888 case KDGETLED: /* get keyboard LED */
889 *(int *)arg = KBD_LED_VAL(kbd);
891 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
892 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
894 ival = IOCPARM_IVAL(arg);
895 arg = (caddr_t)&ival;
898 case KDSETLED: /* set keyboard LED */
899 /* NOTE: lock key state in ks_state won't be changed */
900 if (*(int *)arg & ~LOCK_MASK) {
905 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
906 if (state->ks_mode == K_XLATE &&
907 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
913 if (KBD_HAS_DEVICE(kbd)) {
914 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
915 ledmap[i & LED_MASK]);
921 KBD_LED_VAL(kbd) = *(int *)arg;
924 case KDGKBSTATE: /* get lock key state */
925 *(int *)arg = state->ks_state & LOCK_MASK;
927 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
928 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
930 ival = IOCPARM_IVAL(arg);
931 arg = (caddr_t)&ival;
934 case KDSKBSTATE: /* set lock key state */
935 if (*(int *)arg & ~LOCK_MASK) {
939 state->ks_state &= ~LOCK_MASK;
940 state->ks_state |= *(int *)arg;
942 /* set LEDs and quit */
943 return atkbd_ioctl(kbd, KDSETLED, arg);
945 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
947 if (!KBD_HAS_DEVICE(kbd))
949 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
950 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
952 kbd->kb_delay1 = typematic_delay(i);
953 kbd->kb_delay2 = typematic_rate(i);
957 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
958 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
960 ival = IOCPARM_IVAL(arg);
961 arg = (caddr_t)&ival;
964 case KDSETRAD: /* set keyboard repeat rate (old interface) */
966 if (!KBD_HAS_DEVICE(kbd))
968 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
970 kbd->kb_delay1 = typematic_delay(*(int *)arg);
971 kbd->kb_delay2 = typematic_rate(*(int *)arg);
975 case PIO_KEYMAP: /* set keyboard translation table */
976 case OPIO_KEYMAP: /* set keyboard translation table (compat) */
977 case PIO_KEYMAPENT: /* set keyboard translation table entry */
978 case PIO_DEADKEYMAP: /* set accent key translation table */
979 state->ks_accents = 0;
983 return genkbd_commonioctl(kbd, cmd, arg);
990 /* lock the access to the keyboard */
992 atkbd_lock(keyboard_t *kbd, int lock)
994 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
997 /* clear the internal state of the keyboard */
999 atkbd_clear_state(keyboard_t *kbd)
1001 atkbd_state_t *state;
1003 state = (atkbd_state_t *)kbd->kb_data;
1004 state->ks_flags = 0;
1005 state->ks_polling = 0;
1006 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1007 state->ks_accents = 0;
1008 state->ks_composed_char = 0;
1010 state->ks_prefix = 0; /* XXX */
1014 /* save the internal state */
1016 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1019 return sizeof(atkbd_state_t);
1020 if (len < sizeof(atkbd_state_t))
1022 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
1026 /* set the internal state */
1028 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1030 if (len < sizeof(atkbd_state_t))
1032 if (((atkbd_state_t *)kbd->kb_data)->kbdc
1033 != ((atkbd_state_t *)buf)->kbdc)
1035 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1040 atkbd_poll(keyboard_t *kbd, int on)
1042 atkbd_state_t *state;
1045 state = (atkbd_state_t *)kbd->kb_data;
1048 ++state->ks_polling;
1050 --state->ks_polling;
1056 atkbd_shutdown_final(void *v)
1059 keyboard_t *kbd = v;
1060 KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc;
1063 * Turn off the translation in preparation for handing the keyboard
1064 * over to the OFW as the OBP driver doesn't use translation and
1065 * also doesn't disable it itself resulting in a broken keymap at
1066 * the boot prompt. Also disable the aux port and the interrupts as
1067 * the OBP driver doesn't use them, i.e. polls the keyboard. Not
1068 * disabling the interrupts doesn't cause real problems but the
1069 * responsiveness is a bit better when they are turned off.
1071 send_kbd_command(kbdc, KBDC_DISABLE_KBD);
1072 set_controller_command_byte(kbdc,
1073 KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION,
1074 KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT);
1075 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1079 /* local functions */
1082 set_typematic(keyboard_t *kbd)
1085 atkbd_state_t *state = kbd->kb_data;
1087 val = typematic(DEFAULT_DELAY, DEFAULT_RATE);
1088 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val);
1090 kbd->kb_delay1 = typematic_delay(val);
1091 kbd->kb_delay2 = typematic_rate(val);
1098 setup_kbd_port(KBDC kbdc, int port, int intr)
1100 if (!set_controller_command_byte(kbdc,
1101 KBD_KBD_CONTROL_BITS,
1102 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1103 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1109 get_kbd_echo(KBDC kbdc)
1111 /* enable the keyboard port, but disable the keyboard intr. */
1112 if (setup_kbd_port(kbdc, TRUE, FALSE))
1113 /* CONTROLLER ERROR: there is very little we can do... */
1116 /* see if something is present */
1117 write_kbd_command(kbdc, KBDC_ECHO);
1118 if (read_kbd_data(kbdc) != KBD_ECHO) {
1119 empty_both_buffers(kbdc, 10);
1120 test_controller(kbdc);
1121 test_kbd_port(kbdc);
1125 /* enable the keyboard port and intr. */
1126 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1129 * This is serious; the keyboard intr is left disabled!
1138 probe_keyboard(KBDC kbdc, int flags)
1141 * Don't try to print anything in this function. The low-level
1142 * console may not have been initialized yet...
1148 if (!kbdc_lock(kbdc, TRUE)) {
1153 /* temporarily block data transmission from the keyboard */
1154 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1156 /* flush any noise in the buffer */
1157 empty_both_buffers(kbdc, 100);
1159 /* save the current keyboard controller command byte */
1160 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1161 c = get_controller_command_byte(kbdc);
1163 /* CONTROLLER ERROR */
1164 kbdc_set_device_mask(kbdc, m);
1165 kbdc_lock(kbdc, FALSE);
1170 * The keyboard may have been screwed up by the boot block.
1171 * We may just be able to recover from error by testing the controller
1172 * and the keyboard port. The controller command byte needs to be
1173 * saved before this recovery operation, as some controllers seem
1174 * to set the command byte to particular values.
1176 test_controller(kbdc);
1177 if (!(flags & KB_CONF_NO_PROBE_TEST))
1178 test_kbd_port(kbdc);
1180 err = get_kbd_echo(kbdc);
1183 * Even if the keyboard doesn't seem to be present (err != 0),
1184 * we shall enable the keyboard port and interrupt so that
1185 * the driver will be operable when the keyboard is attached
1186 * to the system later. It is NOT recommended to hot-plug
1187 * the AT keyboard, but many people do so...
1189 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1190 setup_kbd_port(kbdc, TRUE, TRUE);
1193 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1195 /* try to restore the command byte as before */
1196 set_controller_command_byte(kbdc, 0xff, c);
1197 kbdc_set_device_mask(kbdc, m);
1201 kbdc_lock(kbdc, FALSE);
1206 init_keyboard(KBDC kbdc, int *type, int flags)
1212 if (!kbdc_lock(kbdc, TRUE)) {
1217 /* temporarily block data transmission from the keyboard */
1218 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1220 /* save the current controller command byte */
1221 empty_both_buffers(kbdc, 200);
1222 c = get_controller_command_byte(kbdc);
1224 /* CONTROLLER ERROR */
1225 kbdc_lock(kbdc, FALSE);
1226 printf("atkbd: unable to get the current command byte value.\n");
1230 printf("atkbd: the current kbd controller command byte %04x\n",
1233 /* override the keyboard lock switch */
1234 c |= KBD_OVERRIDE_KBD_LOCK;
1237 /* enable the keyboard port, but disable the keyboard intr. */
1238 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1239 /* CONTROLLER ERROR: there is very little we can do... */
1240 printf("atkbd: unable to set the command byte.\n");
1241 kbdc_lock(kbdc, FALSE);
1246 * Check if we have an XT keyboard before we attempt to reset it.
1247 * The procedure assumes that the keyboard and the controller have
1248 * been set up properly by BIOS and have not been messed up
1249 * during the boot process.
1252 if (flags & KB_CONF_ALT_SCANCODESET)
1253 /* the user says there is a XT keyboard */
1255 #ifdef KBD_DETECT_XT_KEYBOARD
1256 else if ((c & KBD_TRANSLATION) == 0) {
1257 /* SET_SCANCODE_SET is not always supported; ignore error */
1258 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1260 codeset = read_kbd_data(kbdc);
1263 printf("atkbd: scancode set %d\n", codeset);
1264 #endif /* KBD_DETECT_XT_KEYBOARD */
1267 id = get_kbd_id(kbdc);
1269 case 0x41ab: /* 101/102/... Enhanced */
1270 case 0x83ab: /* ditto */
1271 case 0x54ab: /* SpaceSaver */
1272 case 0x84ab: /* ditto */
1274 case 0x90ab: /* 'G' */
1275 case 0x91ab: /* 'P' */
1276 case 0x92ab: /* 'A' */
1280 case -1: /* AT 84 keyboard doesn't return ID */
1287 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1289 /* reset keyboard hardware */
1290 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1293 * Keyboard reset may fail either because the keyboard
1294 * doen't exist, or because the keyboard doesn't pass
1295 * the self-test, or the keyboard controller on the
1296 * motherboard and the keyboard somehow fail to shake hands.
1297 * It is just possible, particularly in the last case,
1298 * that the keyboard controller may be left in a hung state.
1299 * test_controller() and test_kbd_port() appear to bring
1300 * the keyboard controller back (I don't know why and how,
1303 empty_both_buffers(kbdc, 10);
1304 test_controller(kbdc);
1305 test_kbd_port(kbdc);
1307 * We could disable the keyboard port and interrupt... but,
1308 * the keyboard may still exist (see above).
1310 set_controller_command_byte(kbdc, 0xff, c);
1311 kbdc_lock(kbdc, FALSE);
1313 printf("atkbd: failed to reset the keyboard.\n");
1318 * Allow us to set the XT_KEYBD flag so that keyboards
1319 * such as those on the IBM ThinkPad laptop computers can be used
1320 * with the standard console driver.
1323 if (send_kbd_command_and_data(kbdc,
1324 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1325 /* XT kbd doesn't need scan code translation */
1326 c &= ~KBD_TRANSLATION;
1330 * The XT kbd isn't usable unless the proper scan
1331 * code set is selected.
1333 set_controller_command_byte(kbdc, 0xff, c);
1334 kbdc_lock(kbdc, FALSE);
1335 printf("atkbd: unable to set the XT keyboard mode.\n");
1340 #if defined(__sparc64__)
1341 if (send_kbd_command_and_data(
1342 kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1343 printf("atkbd: can't set translation.\n");
1345 c |= KBD_TRANSLATION;
1348 /* enable the keyboard port and intr. */
1349 if (!set_controller_command_byte(kbdc,
1350 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1351 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1352 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1355 * This is serious; we are left with the disabled
1358 set_controller_command_byte(kbdc, 0xff, c);
1359 kbdc_lock(kbdc, FALSE);
1360 printf("atkbd: unable to enable the keyboard port and intr.\n");
1364 kbdc_lock(kbdc, FALSE);
1369 write_kbd(KBDC kbdc, int command, int data)
1373 /* prevent the timeout routine from polling the keyboard */
1374 if (!kbdc_lock(kbdc, TRUE))
1377 /* disable the keyboard and mouse interrupt */
1380 c = get_controller_command_byte(kbdc);
1382 || !set_controller_command_byte(kbdc,
1383 kbdc_get_device_mask(kbdc),
1384 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1385 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1386 /* CONTROLLER ERROR */
1387 kbdc_lock(kbdc, FALSE);
1392 * Now that the keyboard controller is told not to generate
1393 * the keyboard and mouse interrupts, call `splx()' to allow
1394 * the other tty interrupts. The clock interrupt may also occur,
1395 * but the timeout routine (`scrn_timer()') will be blocked
1396 * by the lock flag set via `kbdc_lock()'
1400 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1401 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1403 /* restore the interrupts */
1404 if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc),
1405 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1406 /* CONTROLLER ERROR */
1411 kbdc_lock(kbdc, FALSE);
1417 get_kbd_id(KBDC kbdc)
1421 empty_both_buffers(kbdc, 10);
1423 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1426 DELAY(10000); /* 10 msec delay */
1427 id1 = read_kbd_data(kbdc);
1429 id2 = read_kbd_data(kbdc);
1431 if ((id1 == -1) || (id2 == -1)) {
1432 empty_both_buffers(kbdc, 10);
1433 test_controller(kbdc);
1434 test_kbd_port(kbdc);
1437 return ((id2 << 8) | id1);
1440 static int delays[] = { 250, 500, 750, 1000 };
1441 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1442 68, 76, 84, 92, 100, 110, 118, 126,
1443 136, 152, 168, 184, 200, 220, 236, 252,
1444 272, 304, 336, 368, 400, 440, 472, 504 };
1447 typematic_delay(int i)
1449 return delays[(i >> 5) & 3];
1453 typematic_rate(int i)
1455 return rates[i & 0x1f];
1459 typematic(int delay, int rate)
1464 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1465 if (delay >= delays[i])
1469 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1470 if (rate >= rates[i])