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 <machine/md_var.h>
49 #include <machine/psl.h>
50 #include <machine/vm86.h>
51 #include <machine/pc/bios.h>
55 #include <vm/vm_param.h>
57 #include <isa/isareg.h>
61 #include <dev/kbd/kbdreg.h>
62 #include <dev/atkbdc/atkbdreg.h>
63 #include <dev/atkbdc/atkbdcreg.h>
65 static timeout_t atkbd_timeout;
66 static void atkbd_shutdown_final(void *v);
69 atkbd_probe_unit(int unit, int ctlr, int irq, int flags)
71 keyboard_switch_t *sw;
75 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
81 error = (*sw->probe)(unit, args, flags);
88 atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
90 keyboard_switch_t *sw;
94 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
98 /* reset, initialize and enable the device */
102 error = (*sw->probe)(unit, args, flags);
105 error = (*sw->init)(unit, kbd, args, flags);
110 #ifdef KBD_INSTALL_CDEV
111 /* attach a virtual keyboard cdev */
112 error = kbd_attach(*kbd);
118 * This is a kludge to compensate for lost keyboard interrupts.
119 * A similar code used to be in syscons. See below. XXX
124 (*sw->diag)(*kbd, bootverbose);
126 EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd,
127 SHUTDOWN_PRI_DEFAULT);
133 atkbd_timeout(void *arg)
139 * The original text of the following comments are extracted
140 * from syscons.c (1.287)
142 * With release 2.1 of the Xaccel server, the keyboard is left
143 * hanging pretty often. Apparently an interrupt from the
144 * keyboard is lost, and I don't know why (yet).
145 * This ugly hack calls the low-level interrupt routine if input
146 * is ready for the keyboard and conveniently hides the problem. XXX
148 * Try removing anything stuck in the keyboard controller; whether
149 * it's a keyboard scan code or mouse data. The low-level
150 * interrupt routine doesn't read the mouse data directly,
151 * but the keyboard controller driver will, as a side effect.
154 * And here is bde's original comment about this:
156 * This is necessary to handle edge triggered interrupts - if we
157 * returned when our IRQ is high due to unserviced input, then there
158 * would be no more keyboard IRQs until the keyboard is reset by
161 * The keyboard apparently unwedges the irq in most cases.
164 kbd = (keyboard_t *)arg;
165 if (kbdd_lock(kbd, TRUE)) {
167 * We have seen the lock flag is not set. Let's reset
168 * the flag early, otherwise the LED update routine fails
169 * which may want the lock during the interrupt routine.
171 kbdd_lock(kbd, FALSE);
172 if (kbdd_check_char(kbd))
173 kbdd_intr(kbd, NULL);
176 timeout(atkbd_timeout, arg, hz/10);
181 #define ATKBD_DEFAULT 0
183 typedef struct atkbd_state {
184 KBDC kbdc; /* keyboard controller */
185 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
186 int ks_flags; /* flags */
187 #define COMPOSE (1 << 0)
189 int ks_state; /* shift/lock key state */
190 int ks_accents; /* accent key index (> 0) */
191 u_int ks_composed_char; /* composed char code (> 0) */
192 u_char ks_prefix; /* AT scan code prefix */
195 /* keyboard driver declaration */
196 static int atkbd_configure(int flags);
197 static kbd_probe_t atkbd_probe;
198 static kbd_init_t atkbd_init;
199 static kbd_term_t atkbd_term;
200 static kbd_intr_t atkbd_intr;
201 static kbd_test_if_t atkbd_test_if;
202 static kbd_enable_t atkbd_enable;
203 static kbd_disable_t atkbd_disable;
204 static kbd_read_t atkbd_read;
205 static kbd_check_t atkbd_check;
206 static kbd_read_char_t atkbd_read_char;
207 static kbd_check_char_t atkbd_check_char;
208 static kbd_ioctl_t atkbd_ioctl;
209 static kbd_lock_t atkbd_lock;
210 static kbd_clear_state_t atkbd_clear_state;
211 static kbd_get_state_t atkbd_get_state;
212 static kbd_set_state_t atkbd_set_state;
213 static kbd_poll_mode_t atkbd_poll;
215 static keyboard_switch_t atkbdsw = {
237 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
239 /* local functions */
240 static int get_typematic(keyboard_t *kbd);
241 static int setup_kbd_port(KBDC kbdc, int port, int intr);
242 static int get_kbd_echo(KBDC kbdc);
243 static int probe_keyboard(KBDC kbdc, int flags);
244 static int init_keyboard(KBDC kbdc, int *type, int flags);
245 static int write_kbd(KBDC kbdc, int command, int data);
246 static int get_kbd_id(KBDC kbdc);
247 static int typematic(int delay, int rate);
248 static int typematic_delay(int delay);
249 static int typematic_rate(int rate);
251 /* local variables */
253 /* the initial key map, accent map and fkey strings */
254 #ifdef ATKBD_DFLT_KEYMAP
255 #define KBD_DFLT_KEYMAP
256 #include "atkbdmap.h"
258 #include <dev/kbd/kbdtables.h>
260 /* structures for the default keyboard */
261 static keyboard_t default_kbd;
262 static atkbd_state_t default_kbd_state;
263 static keymap_t default_keymap;
264 static accentmap_t default_accentmap;
265 static fkeytab_t default_fkeytab[NUM_FKEYS];
268 * The back door to the keyboard driver!
269 * This function is called by the console driver, via the kbdio module,
270 * to tickle keyboard drivers when the low-level console is being initialized.
271 * Almost nothing in the kernel has been initialied yet. Try to probe
272 * keyboards if possible.
273 * NOTE: because of the way the low-level console is initialized, this routine
274 * may be called more than once!!
277 atkbd_configure(int flags)
284 * Probe the keyboard controller, if not present or if the driver
285 * is disabled, unregister the keyboard if any.
287 if (atkbdc_configure() != 0 ||
288 resource_disabled("atkbd", ATKBD_DEFAULT)) {
289 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
291 kbd = kbd_get_keyboard(i);
293 kbd->kb_flags &= ~KB_REGISTERED;
298 /* XXX: a kludge to obtain the device configuration flags */
299 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
302 /* probe the default keyboard */
306 if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
308 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
311 /* return the number of found keyboards */
315 /* low-level functions */
317 /* detect a keyboard */
319 atkbd_probe(int unit, void *arg, int flags)
322 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
325 if (unit == ATKBD_DEFAULT) {
326 if (KBD_IS_PROBED(&default_kbd))
330 kbdc = atkbdc_open(data[0]);
333 if (probe_keyboard(kbdc, flags)) {
334 if (flags & KB_CONF_FAIL_IF_NO_KBD)
340 /* reset and initialize the device */
342 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
345 atkbd_state_t *state;
351 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
355 if (unit == ATKBD_DEFAULT) {
356 *kbdp = kbd = &default_kbd;
357 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
359 state = &default_kbd_state;
360 keymap = &default_keymap;
361 accmap = &default_accentmap;
362 fkeymap = default_fkeytab;
364 sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
366 } else if (*kbdp == NULL) {
367 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
368 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
369 /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
370 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
371 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
372 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
373 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
375 if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
376 || (accmap == NULL) || (fkeymap == NULL)) {
380 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
384 state = (atkbd_state_t *)kbd->kb_data;
385 bzero(state, sizeof(*state));
386 keymap = kbd->kb_keymap;
387 accmap = kbd->kb_accentmap;
388 fkeymap = kbd->kb_fkeytab;
389 fkeymap_size = kbd->kb_fkeytab_size;
393 if (!KBD_IS_PROBED(kbd)) {
394 state->kbdc = atkbdc_open(data[0]);
395 if (state->kbdc == NULL) {
399 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
401 bcopy(&key_map, keymap, sizeof(key_map));
402 bcopy(&accent_map, accmap, sizeof(accent_map));
403 bcopy(fkey_tab, fkeymap,
404 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
405 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
406 kbd->kb_data = (void *)state;
408 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
409 if (flags & KB_CONF_FAIL_IF_NO_KBD) {
414 KBD_FOUND_DEVICE(kbd);
416 atkbd_clear_state(kbd);
417 state->ks_mode = K_XLATE;
419 * FIXME: set the initial value for lock keys in ks_state
420 * according to the BIOS data?
424 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
425 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
426 if (KBD_HAS_DEVICE(kbd)
427 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
428 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
433 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
435 delay[0] = kbd->kb_delay1;
436 delay[1] = kbd->kb_delay2;
437 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
440 if (!KBD_IS_CONFIGURED(kbd)) {
441 if (kbd_register(kbd) < 0) {
445 KBD_CONFIG_DONE(kbd);
452 free(state, M_DEVBUF);
454 free(keymap, M_DEVBUF);
456 free(accmap, M_DEVBUF);
458 free(fkeymap, M_DEVBUF);
461 *kbdp = NULL; /* insure ref doesn't leak to caller */
467 /* finish using this keyboard */
469 atkbd_term(keyboard_t *kbd)
475 /* keyboard interrupt routine */
477 atkbd_intr(keyboard_t *kbd, void *arg)
479 atkbd_state_t *state;
483 if (!KBD_HAS_DEVICE(kbd)) {
485 * The keyboard was not detected before;
486 * it must have been reconnected!
488 state = (atkbd_state_t *)kbd->kb_data;
489 init_keyboard(state->kbdc, &kbd->kb_type,
491 KBD_FOUND_DEVICE(kbd);
492 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
494 delay[0] = kbd->kb_delay1;
495 delay[1] = kbd->kb_delay2;
496 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
499 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
500 /* let the callback function to process the input */
501 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
502 kbd->kb_callback.kc_arg);
504 /* read and discard the input; no one is waiting for input */
506 c = atkbd_read_char(kbd, FALSE);
507 } while (c != NOKEY);
512 /* test the interface to the device */
514 atkbd_test_if(keyboard_t *kbd)
520 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
522 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
524 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
532 * Enable the access to the device; until this function is called,
533 * the client cannot read from the keyboard.
536 atkbd_enable(keyboard_t *kbd)
546 /* disallow the access to the device */
548 atkbd_disable(keyboard_t *kbd)
558 /* read one byte from the keyboard if it's allowed */
560 atkbd_read(keyboard_t *kbd, int wait)
565 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
567 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
570 return (KBD_IS_ACTIVE(kbd) ? c : -1);
573 /* check if data is waiting */
575 atkbd_check(keyboard_t *kbd)
577 if (!KBD_IS_ACTIVE(kbd))
579 return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
582 /* read char from the keyboard */
584 atkbd_read_char(keyboard_t *kbd, int wait)
586 atkbd_state_t *state;
591 state = (atkbd_state_t *)kbd->kb_data;
593 /* do we have a composed char to return? */
594 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
595 action = state->ks_composed_char;
596 state->ks_composed_char = 0;
597 if (action > UCHAR_MAX)
602 /* see if there is something in the keyboard port */
605 scancode = read_kbd_data(state->kbdc);
606 } while (scancode == -1);
608 scancode = read_kbd_data_no_wait(state->kbdc);
614 #if KBDIO_DEBUG >= 10
615 printf("atkbd_read_char(): scancode:0x%x\n", scancode);
618 /* return the byte as is for the K_RAW mode */
619 if (state->ks_mode == K_RAW)
622 /* translate the scan code into a keycode */
623 keycode = scancode & 0x7F;
624 switch (state->ks_prefix) {
625 case 0x00: /* normal scancode */
627 case 0xB8: /* left alt (compose key) released */
628 if (state->ks_flags & COMPOSE) {
629 state->ks_flags &= ~COMPOSE;
630 if (state->ks_composed_char > UCHAR_MAX)
631 state->ks_composed_char = 0;
634 case 0x38: /* left alt (compose key) pressed */
635 if (!(state->ks_flags & COMPOSE)) {
636 state->ks_flags |= COMPOSE;
637 state->ks_composed_char = 0;
642 state->ks_prefix = scancode;
646 case 0xE0: /* 0xE0 prefix */
647 state->ks_prefix = 0;
649 case 0x1C: /* right enter key */
652 case 0x1D: /* right ctrl key */
655 case 0x35: /* keypad divide key */
658 case 0x37: /* print scrn key */
661 case 0x38: /* right alt key (alt gr) */
664 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
667 case 0x47: /* grey home key */
670 case 0x48: /* grey up arrow key */
673 case 0x49: /* grey page up key */
676 case 0x4B: /* grey left arrow key */
679 case 0x4D: /* grey right arrow key */
682 case 0x4F: /* grey end key */
685 case 0x50: /* grey down arrow key */
688 case 0x51: /* grey page down key */
691 case 0x52: /* grey insert key */
694 case 0x53: /* grey delete key */
697 /* the following 3 are only used on the MS "Natural" keyboard */
698 case 0x5b: /* left Window key */
701 case 0x5c: /* right Window key */
704 case 0x5d: /* menu key */
707 case 0x5e: /* power key */
710 case 0x5f: /* sleep key */
713 case 0x63: /* wake key */
716 default: /* ignore everything else */
720 case 0xE1: /* 0xE1 prefix */
722 * The pause/break key on the 101 keyboard produces:
724 * Ctrl-pause/break produces:
725 * E0-46 E0-C6 (See above.)
727 state->ks_prefix = 0;
729 state->ks_prefix = 0x1D;
732 case 0x1D: /* pause / break */
733 state->ks_prefix = 0;
740 if (kbd->kb_type == KB_84) {
742 case 0x37: /* *(numpad)/print screen */
743 if (state->ks_flags & SHIFTS)
744 keycode = 0x5c; /* print screen */
746 case 0x45: /* num lock/pause */
747 if (state->ks_flags & CTLS)
748 keycode = 0x68; /* pause */
750 case 0x46: /* scroll lock/break */
751 if (state->ks_flags & CTLS)
752 keycode = 0x6c; /* break */
755 } else if (kbd->kb_type == KB_101) {
757 case 0x5c: /* print screen */
758 if (state->ks_flags & ALTS)
759 keycode = 0x54; /* sysrq */
761 case 0x68: /* pause/break */
762 if (state->ks_flags & CTLS)
763 keycode = 0x6c; /* break */
768 /* return the key code in the K_CODE mode */
769 if (state->ks_mode == K_CODE)
770 return (keycode | (scancode & 0x80));
772 /* compose a character code */
773 if (state->ks_flags & COMPOSE) {
774 switch (keycode | (scancode & 0x80)) {
775 /* key pressed, process it */
776 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
777 state->ks_composed_char *= 10;
778 state->ks_composed_char += keycode - 0x40;
779 if (state->ks_composed_char > UCHAR_MAX)
782 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
783 state->ks_composed_char *= 10;
784 state->ks_composed_char += keycode - 0x47;
785 if (state->ks_composed_char > UCHAR_MAX)
788 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
789 state->ks_composed_char *= 10;
790 state->ks_composed_char += keycode - 0x4E;
791 if (state->ks_composed_char > UCHAR_MAX)
794 case 0x52: /* keypad 0 */
795 state->ks_composed_char *= 10;
796 if (state->ks_composed_char > UCHAR_MAX)
800 /* key released, no interest here */
801 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
802 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
803 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
804 case 0xD2: /* keypad 0 */
807 case 0x38: /* left alt key */
811 if (state->ks_composed_char > 0) {
812 state->ks_flags &= ~COMPOSE;
813 state->ks_composed_char = 0;
820 /* keycode to key action */
821 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
822 &state->ks_state, &state->ks_accents);
829 /* check if char is waiting */
831 atkbd_check_char(keyboard_t *kbd)
833 atkbd_state_t *state;
835 if (!KBD_IS_ACTIVE(kbd))
837 state = (atkbd_state_t *)kbd->kb_data;
838 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
840 return kbdc_data_ready(state->kbdc);
843 /* some useful control functions */
845 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
847 /* translate LED_XXX bits into the device specific bits */
848 static u_char ledmap[8] = {
849 0, 4, 2, 6, 1, 5, 3, 7,
851 atkbd_state_t *state = kbd->kb_data;
855 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
856 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
863 case KDGKBMODE: /* get keyboard mode */
864 *(int *)arg = state->ks_mode;
866 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
867 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
869 ival = IOCPARM_IVAL(arg);
870 arg = (caddr_t)&ival;
873 case KDSKBMODE: /* set keyboard mode */
874 switch (*(int *)arg) {
876 if (state->ks_mode != K_XLATE) {
877 /* make lock key state and LED state match */
878 state->ks_state &= ~LOCK_MASK;
879 state->ks_state |= KBD_LED_VAL(kbd);
884 if (state->ks_mode != *(int *)arg) {
885 atkbd_clear_state(kbd);
886 state->ks_mode = *(int *)arg;
895 case KDGETLED: /* get keyboard LED */
896 *(int *)arg = KBD_LED_VAL(kbd);
898 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
899 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
901 ival = IOCPARM_IVAL(arg);
902 arg = (caddr_t)&ival;
905 case KDSETLED: /* set keyboard LED */
906 /* NOTE: lock key state in ks_state won't be changed */
907 if (*(int *)arg & ~LOCK_MASK) {
912 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
913 if (state->ks_mode == K_XLATE &&
914 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
920 if (KBD_HAS_DEVICE(kbd)) {
921 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
922 ledmap[i & LED_MASK]);
928 KBD_LED_VAL(kbd) = *(int *)arg;
931 case KDGKBSTATE: /* get lock key state */
932 *(int *)arg = state->ks_state & LOCK_MASK;
934 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
935 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
937 ival = IOCPARM_IVAL(arg);
938 arg = (caddr_t)&ival;
941 case KDSKBSTATE: /* set lock key state */
942 if (*(int *)arg & ~LOCK_MASK) {
946 state->ks_state &= ~LOCK_MASK;
947 state->ks_state |= *(int *)arg;
949 /* set LEDs and quit */
950 return atkbd_ioctl(kbd, KDSETLED, arg);
952 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
954 if (!KBD_HAS_DEVICE(kbd))
956 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
957 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
959 kbd->kb_delay1 = typematic_delay(i);
960 kbd->kb_delay2 = typematic_rate(i);
964 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
965 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
967 ival = IOCPARM_IVAL(arg);
968 arg = (caddr_t)&ival;
971 case KDSETRAD: /* set keyboard repeat rate (old interface) */
973 if (!KBD_HAS_DEVICE(kbd))
975 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
977 kbd->kb_delay1 = typematic_delay(*(int *)arg);
978 kbd->kb_delay2 = typematic_rate(*(int *)arg);
982 case PIO_KEYMAP: /* set keyboard translation table */
983 case PIO_KEYMAPENT: /* set keyboard translation table entry */
984 case PIO_DEADKEYMAP: /* set accent key translation table */
985 state->ks_accents = 0;
989 return genkbd_commonioctl(kbd, cmd, arg);
996 /* lock the access to the keyboard */
998 atkbd_lock(keyboard_t *kbd, int lock)
1000 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
1003 /* clear the internal state of the keyboard */
1005 atkbd_clear_state(keyboard_t *kbd)
1007 atkbd_state_t *state;
1009 state = (atkbd_state_t *)kbd->kb_data;
1010 state->ks_flags = 0;
1011 state->ks_polling = 0;
1012 state->ks_state &= LOCK_MASK; /* preserve locking key state */
1013 state->ks_accents = 0;
1014 state->ks_composed_char = 0;
1016 state->ks_prefix = 0; /* XXX */
1020 /* save the internal state */
1022 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1025 return sizeof(atkbd_state_t);
1026 if (len < sizeof(atkbd_state_t))
1028 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
1032 /* set the internal state */
1034 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1036 if (len < sizeof(atkbd_state_t))
1038 if (((atkbd_state_t *)kbd->kb_data)->kbdc
1039 != ((atkbd_state_t *)buf)->kbdc)
1041 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1046 atkbd_poll(keyboard_t *kbd, int on)
1048 atkbd_state_t *state;
1051 state = (atkbd_state_t *)kbd->kb_data;
1054 ++state->ks_polling;
1056 --state->ks_polling;
1062 atkbd_shutdown_final(void *v)
1065 keyboard_t *kbd = v;
1066 KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc;
1069 * Turn off the translation in preparation for handing the keyboard
1070 * over to the OFW as the OBP driver doesn't use translation and
1071 * also doesn't disable it itself resulting in a broken keymap at
1072 * the boot prompt. Also disable the aux port and the interrupts as
1073 * the OBP driver doesn't use them, i.e. polls the keyboard. Not
1074 * disabling the interrupts doesn't cause real problems but the
1075 * responsiveness is a bit better when they are turned off.
1077 send_kbd_command(kbdc, KBDC_DISABLE_KBD);
1078 set_controller_command_byte(kbdc,
1079 KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION,
1080 KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT);
1081 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1085 /* local functions */
1088 get_typematic(keyboard_t *kbd)
1092 * Only some systems allow us to retrieve the keyboard repeat
1093 * rate previously set via the BIOS...
1095 struct vm86frame vmf;
1098 bzero(&vmf, sizeof(vmf));
1099 vmf.vmf_ax = 0xc000;
1100 vm86_intcall(0x15, &vmf);
1101 if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
1103 p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
1104 if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */
1106 vmf.vmf_ax = 0x0900;
1107 vm86_intcall(0x16, &vmf);
1108 if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */
1110 vmf.vmf_ax = 0x0306;
1111 vm86_intcall(0x16, &vmf);
1112 kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
1113 kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
1117 #endif /* __i386__ */
1121 setup_kbd_port(KBDC kbdc, int port, int intr)
1123 if (!set_controller_command_byte(kbdc,
1124 KBD_KBD_CONTROL_BITS,
1125 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1126 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1132 get_kbd_echo(KBDC kbdc)
1134 /* enable the keyboard port, but disable the keyboard intr. */
1135 if (setup_kbd_port(kbdc, TRUE, FALSE))
1136 /* CONTROLLER ERROR: there is very little we can do... */
1139 /* see if something is present */
1140 write_kbd_command(kbdc, KBDC_ECHO);
1141 if (read_kbd_data(kbdc) != KBD_ECHO) {
1142 empty_both_buffers(kbdc, 10);
1143 test_controller(kbdc);
1144 test_kbd_port(kbdc);
1148 /* enable the keyboard port and intr. */
1149 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1152 * This is serious; the keyboard intr is left disabled!
1161 probe_keyboard(KBDC kbdc, int flags)
1164 * Don't try to print anything in this function. The low-level
1165 * console may not have been initialized yet...
1171 if (!kbdc_lock(kbdc, TRUE)) {
1176 /* temporarily block data transmission from the keyboard */
1177 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1179 /* flush any noise in the buffer */
1180 empty_both_buffers(kbdc, 100);
1182 /* save the current keyboard controller command byte */
1183 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1184 c = get_controller_command_byte(kbdc);
1186 /* CONTROLLER ERROR */
1187 kbdc_set_device_mask(kbdc, m);
1188 kbdc_lock(kbdc, FALSE);
1193 * The keyboard may have been screwed up by the boot block.
1194 * We may just be able to recover from error by testing the controller
1195 * and the keyboard port. The controller command byte needs to be
1196 * saved before this recovery operation, as some controllers seem
1197 * to set the command byte to particular values.
1199 test_controller(kbdc);
1200 if (!(flags & KB_CONF_NO_PROBE_TEST))
1201 test_kbd_port(kbdc);
1203 err = get_kbd_echo(kbdc);
1206 * Even if the keyboard doesn't seem to be present (err != 0),
1207 * we shall enable the keyboard port and interrupt so that
1208 * the driver will be operable when the keyboard is attached
1209 * to the system later. It is NOT recommended to hot-plug
1210 * the AT keyboard, but many people do so...
1212 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1213 setup_kbd_port(kbdc, TRUE, TRUE);
1216 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1218 /* try to restore the command byte as before */
1219 set_controller_command_byte(kbdc, 0xff, c);
1220 kbdc_set_device_mask(kbdc, m);
1224 kbdc_lock(kbdc, FALSE);
1229 init_keyboard(KBDC kbdc, int *type, int flags)
1235 if (!kbdc_lock(kbdc, TRUE)) {
1240 /* temporarily block data transmission from the keyboard */
1241 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1243 /* save the current controller command byte */
1244 empty_both_buffers(kbdc, 200);
1245 c = get_controller_command_byte(kbdc);
1247 /* CONTROLLER ERROR */
1248 kbdc_lock(kbdc, FALSE);
1249 printf("atkbd: unable to get the current command byte value.\n");
1253 printf("atkbd: the current kbd controller command byte %04x\n",
1256 /* override the keyboard lock switch */
1257 c |= KBD_OVERRIDE_KBD_LOCK;
1260 /* enable the keyboard port, but disable the keyboard intr. */
1261 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1262 /* CONTROLLER ERROR: there is very little we can do... */
1263 printf("atkbd: unable to set the command byte.\n");
1264 kbdc_lock(kbdc, FALSE);
1269 * Check if we have an XT keyboard before we attempt to reset it.
1270 * The procedure assumes that the keyboard and the controller have
1271 * been set up properly by BIOS and have not been messed up
1272 * during the boot process.
1275 if (flags & KB_CONF_ALT_SCANCODESET)
1276 /* the user says there is a XT keyboard */
1278 #ifdef KBD_DETECT_XT_KEYBOARD
1279 else if ((c & KBD_TRANSLATION) == 0) {
1280 /* SET_SCANCODE_SET is not always supported; ignore error */
1281 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1283 codeset = read_kbd_data(kbdc);
1286 printf("atkbd: scancode set %d\n", codeset);
1287 #endif /* KBD_DETECT_XT_KEYBOARD */
1290 id = get_kbd_id(kbdc);
1292 case 0x41ab: /* 101/102/... Enhanced */
1293 case 0x83ab: /* ditto */
1294 case 0x54ab: /* SpaceSaver */
1295 case 0x84ab: /* ditto */
1297 case 0x90ab: /* 'G' */
1298 case 0x91ab: /* 'P' */
1299 case 0x92ab: /* 'A' */
1303 case -1: /* AT 84 keyboard doesn't return ID */
1310 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1312 /* reset keyboard hardware */
1313 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1316 * Keyboard reset may fail either because the keyboard
1317 * doen't exist, or because the keyboard doesn't pass
1318 * the self-test, or the keyboard controller on the
1319 * motherboard and the keyboard somehow fail to shake hands.
1320 * It is just possible, particularly in the last case,
1321 * that the keyboard controller may be left in a hung state.
1322 * test_controller() and test_kbd_port() appear to bring
1323 * the keyboard controller back (I don't know why and how,
1326 empty_both_buffers(kbdc, 10);
1327 test_controller(kbdc);
1328 test_kbd_port(kbdc);
1330 * We could disable the keyboard port and interrupt... but,
1331 * the keyboard may still exist (see above).
1333 set_controller_command_byte(kbdc, 0xff, c);
1334 kbdc_lock(kbdc, FALSE);
1336 printf("atkbd: failed to reset the keyboard.\n");
1341 * Allow us to set the XT_KEYBD flag so that keyboards
1342 * such as those on the IBM ThinkPad laptop computers can be used
1343 * with the standard console driver.
1346 if (send_kbd_command_and_data(kbdc,
1347 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1348 /* XT kbd doesn't need scan code translation */
1349 c &= ~KBD_TRANSLATION;
1353 * The XT kbd isn't usable unless the proper scan
1354 * code set is selected.
1356 set_controller_command_byte(kbdc, 0xff, c);
1357 kbdc_lock(kbdc, FALSE);
1358 printf("atkbd: unable to set the XT keyboard mode.\n");
1363 #if defined(__sparc64__)
1364 if (send_kbd_command_and_data(
1365 kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1366 printf("atkbd: can't set translation.\n");
1368 c |= KBD_TRANSLATION;
1371 /* enable the keyboard port and intr. */
1372 if (!set_controller_command_byte(kbdc,
1373 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1374 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1375 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1378 * This is serious; we are left with the disabled
1381 set_controller_command_byte(kbdc, 0xff, c);
1382 kbdc_lock(kbdc, FALSE);
1383 printf("atkbd: unable to enable the keyboard port and intr.\n");
1387 kbdc_lock(kbdc, FALSE);
1392 write_kbd(KBDC kbdc, int command, int data)
1396 /* prevent the timeout routine from polling the keyboard */
1397 if (!kbdc_lock(kbdc, TRUE))
1400 /* disable the keyboard and mouse interrupt */
1403 c = get_controller_command_byte(kbdc);
1405 || !set_controller_command_byte(kbdc,
1406 kbdc_get_device_mask(kbdc),
1407 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1408 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1409 /* CONTROLLER ERROR */
1410 kbdc_lock(kbdc, FALSE);
1415 * Now that the keyboard controller is told not to generate
1416 * the keyboard and mouse interrupts, call `splx()' to allow
1417 * the other tty interrupts. The clock interrupt may also occur,
1418 * but the timeout routine (`scrn_timer()') will be blocked
1419 * by the lock flag set via `kbdc_lock()'
1424 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1425 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1428 /* restore the interrupts */
1429 if (!set_controller_command_byte(kbdc,
1430 kbdc_get_device_mask(kbdc),
1431 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1432 /* CONTROLLER ERROR */
1437 kbdc_lock(kbdc, FALSE);
1443 get_kbd_id(KBDC kbdc)
1447 empty_both_buffers(kbdc, 10);
1449 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1452 DELAY(10000); /* 10 msec delay */
1453 id1 = read_kbd_data(kbdc);
1455 id2 = read_kbd_data(kbdc);
1457 if ((id1 == -1) || (id2 == -1)) {
1458 empty_both_buffers(kbdc, 10);
1459 test_controller(kbdc);
1460 test_kbd_port(kbdc);
1463 return ((id2 << 8) | id1);
1466 static int delays[] = { 250, 500, 750, 1000 };
1467 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1468 68, 76, 84, 92, 100, 110, 118, 126,
1469 136, 152, 168, 184, 200, 220, 236, 252,
1470 272, 304, 336, 368, 400, 440, 472, 504 };
1473 typematic_delay(int i)
1475 return delays[(i >> 5) & 3];
1479 typematic_rate(int i)
1481 return rates[i & 0x1f];
1485 typematic(int delay, int rate)
1490 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1491 if (delay >= delays[i])
1495 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1496 if (rate >= rates[i])