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.
30 #include "opt_atkbd.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
43 #include <machine/md_var.h>
44 #include <machine/psl.h>
45 #include <machine/vm86.h>
46 #include <machine/pc/bios.h>
53 #include <dev/kbd/kbdreg.h>
54 #include <dev/kbd/atkbdreg.h>
55 #include <dev/kbd/atkbdcreg.h>
57 #include <isa/isareg.h>
59 static timeout_t atkbd_timeout;
62 atkbd_probe_unit(int unit, int ctlr, int irq, int flags)
64 keyboard_switch_t *sw;
68 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
74 error = (*sw->probe)(unit, args, flags);
81 atkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags)
83 keyboard_switch_t *sw;
87 sw = kbd_get_switch(ATKBD_DRIVER_NAME);
91 /* reset, initialize and enable the device */
95 error = (*sw->probe)(unit, args, flags);
98 error = (*sw->init)(unit, kbd, args, flags);
103 #ifdef KBD_INSTALL_CDEV
104 /* attach a virtual keyboard cdev */
105 error = kbd_attach(*kbd);
111 * This is a kludge to compensate for lost keyboard interrupts.
112 * A similar code used to be in syscons. See below. XXX
117 (*sw->diag)(*kbd, bootverbose);
122 atkbd_timeout(void *arg)
128 * The original text of the following comments are extracted
129 * from syscons.c (1.287)
131 * With release 2.1 of the Xaccel server, the keyboard is left
132 * hanging pretty often. Apparently an interrupt from the
133 * keyboard is lost, and I don't know why (yet).
134 * This ugly hack calls the low-level interrupt routine if input
135 * is ready for the keyboard and conveniently hides the problem. XXX
137 * Try removing anything stuck in the keyboard controller; whether
138 * it's a keyboard scan code or mouse data. The low-level
139 * interrupt routine doesn't read the mouse data directly,
140 * but the keyboard controller driver will, as a side effect.
143 * And here is bde's original comment about this:
145 * This is necessary to handle edge triggered interrupts - if we
146 * returned when our IRQ is high due to unserviced input, then there
147 * would be no more keyboard IRQs until the keyboard is reset by
150 * The keyboard apparently unwedges the irq in most cases.
153 kbd = (keyboard_t *)arg;
154 if ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) {
156 * We have seen the lock flag is not set. Let's reset
157 * the flag early, otherwise the LED update routine fails
158 * which may want the lock during the interrupt routine.
160 (*kbdsw[kbd->kb_index]->lock)(kbd, FALSE);
161 if ((*kbdsw[kbd->kb_index]->check_char)(kbd))
162 (*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
165 timeout(atkbd_timeout, arg, hz/10);
170 #include <machine/limits.h>
172 #define ATKBD_DEFAULT 0
174 typedef struct atkbd_state {
175 KBDC kbdc; /* keyboard controller */
176 /* XXX: don't move this field; pcvt
177 * expects `kbdc' to be the first
178 * field in this structure. */
179 int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
180 int ks_flags; /* flags */
181 #define COMPOSE (1 << 0)
183 int ks_state; /* shift/lock key state */
184 int ks_accents; /* accent key index (> 0) */
185 u_int ks_composed_char; /* composed char code (> 0) */
186 u_char ks_prefix; /* AT scan code prefix */
189 /* keyboard driver declaration */
190 static int atkbd_configure(int flags);
191 static kbd_probe_t atkbd_probe;
192 static kbd_init_t atkbd_init;
193 static kbd_term_t atkbd_term;
194 static kbd_intr_t atkbd_intr;
195 static kbd_test_if_t atkbd_test_if;
196 static kbd_enable_t atkbd_enable;
197 static kbd_disable_t atkbd_disable;
198 static kbd_read_t atkbd_read;
199 static kbd_check_t atkbd_check;
200 static kbd_read_char_t atkbd_read_char;
201 static kbd_check_char_t atkbd_check_char;
202 static kbd_ioctl_t atkbd_ioctl;
203 static kbd_lock_t atkbd_lock;
204 static kbd_clear_state_t atkbd_clear_state;
205 static kbd_get_state_t atkbd_get_state;
206 static kbd_set_state_t atkbd_set_state;
207 static kbd_poll_mode_t atkbd_poll;
209 keyboard_switch_t atkbdsw = {
231 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
233 /* local functions */
234 static int get_typematic(keyboard_t *kbd);
235 static int setup_kbd_port(KBDC kbdc, int port, int intr);
236 static int get_kbd_echo(KBDC kbdc);
237 static int probe_keyboard(KBDC kbdc, int flags);
238 static int init_keyboard(KBDC kbdc, int *type, int flags);
239 static int write_kbd(KBDC kbdc, int command, int data);
240 static int get_kbd_id(KBDC kbdc);
241 static int typematic(int delay, int rate);
242 static int typematic_delay(int delay);
243 static int typematic_rate(int rate);
245 /* local variables */
247 /* the initial key map, accent map and fkey strings */
248 #ifdef ATKBD_DFLT_KEYMAP
249 #define KBD_DFLT_KEYMAP
250 #include "atkbdmap.h"
252 #include <dev/kbd/kbdtables.h>
254 /* structures for the default keyboard */
255 static keyboard_t default_kbd;
256 static atkbd_state_t default_kbd_state;
257 static keymap_t default_keymap;
258 static accentmap_t default_accentmap;
259 static fkeytab_t default_fkeytab[NUM_FKEYS];
262 * The back door to the keyboard driver!
263 * This function is called by the console driver, via the kbdio module,
264 * to tickle keyboard drivers when the low-level console is being initialized.
265 * Almost nothing in the kernel has been initialied yet. Try to probe
266 * keyboards if possible.
267 * NOTE: because of the way the low-level console is initialized, this routine
268 * may be called more than once!!
271 atkbd_configure(int flags)
277 /* probe the keyboard controller */
280 /* if the driver is disabled, unregister the keyboard if any */
281 if ((resource_int_value("atkbd", ATKBD_DEFAULT, "disabled", &i) == 0)
283 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
285 kbd = kbd_get_keyboard(i);
287 kbd->kb_flags &= ~KB_REGISTERED;
292 /* XXX: a kludge to obtain the device configuration flags */
293 if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
296 /* probe the default keyboard */
300 if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
302 if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
305 /* return the number of found keyboards */
309 /* low-level functions */
311 /* detect a keyboard */
313 atkbd_probe(int unit, void *arg, int flags)
316 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
319 if (unit == ATKBD_DEFAULT) {
320 if (KBD_IS_PROBED(&default_kbd))
324 kbdc = atkbdc_open(data[0]);
327 if (probe_keyboard(kbdc, flags)) {
328 if (flags & KB_CONF_FAIL_IF_NO_KBD)
334 /* reset and initialize the device */
336 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
339 atkbd_state_t *state;
345 int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
348 if (unit == ATKBD_DEFAULT) {
349 *kbdp = kbd = &default_kbd;
350 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
352 state = &default_kbd_state;
353 keymap = &default_keymap;
354 accmap = &default_accentmap;
355 fkeymap = default_fkeytab;
357 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 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
362 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
363 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
364 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
365 if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
366 || (accmap == NULL) || (fkeymap == NULL)) {
368 free(state, M_DEVBUF);
370 free(keymap, M_DEVBUF);
372 free(accmap, M_DEVBUF);
374 free(fkeymap, M_DEVBUF);
379 } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
383 state = (atkbd_state_t *)kbd->kb_data;
384 bzero(state, sizeof(*state));
385 keymap = kbd->kb_keymap;
386 accmap = kbd->kb_accentmap;
387 fkeymap = kbd->kb_fkeytab;
388 fkeymap_size = kbd->kb_fkeytab_size;
391 if (!KBD_IS_PROBED(kbd)) {
392 state->kbdc = atkbdc_open(data[0]);
393 if (state->kbdc == NULL)
395 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
397 bcopy(&key_map, keymap, sizeof(key_map));
398 bcopy(&accent_map, accmap, sizeof(accent_map));
399 bcopy(fkey_tab, fkeymap,
400 imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab)));
401 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
402 kbd->kb_data = (void *)state;
404 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
405 if (flags & KB_CONF_FAIL_IF_NO_KBD)
408 KBD_FOUND_DEVICE(kbd);
410 atkbd_clear_state(kbd);
411 state->ks_mode = K_XLATE;
413 * FIXME: set the initial value for lock keys in ks_state
414 * according to the BIOS data?
418 if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
419 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
420 if (KBD_HAS_DEVICE(kbd)
421 && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
422 && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD))
424 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
426 delay[0] = kbd->kb_delay1;
427 delay[1] = kbd->kb_delay2;
428 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
431 if (!KBD_IS_CONFIGURED(kbd)) {
432 if (kbd_register(kbd) < 0)
434 KBD_CONFIG_DONE(kbd);
440 /* finish using this keyboard */
442 atkbd_term(keyboard_t *kbd)
448 /* keyboard interrupt routine */
450 atkbd_intr(keyboard_t *kbd, void *arg)
452 atkbd_state_t *state;
456 if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
457 /* let the callback function to process the input */
458 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
459 kbd->kb_callback.kc_arg);
461 /* read and discard the input; no one is waiting for input */
463 c = atkbd_read_char(kbd, FALSE);
464 } while (c != NOKEY);
466 if (!KBD_HAS_DEVICE(kbd)) {
468 * The keyboard was not detected before;
469 * it must have been reconnected!
471 state = (atkbd_state_t *)kbd->kb_data;
472 init_keyboard(state->kbdc, &kbd->kb_type,
474 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
476 delay[0] = kbd->kb_delay1;
477 delay[1] = kbd->kb_delay2;
478 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
479 KBD_FOUND_DEVICE(kbd);
485 /* test the interface to the device */
487 atkbd_test_if(keyboard_t *kbd)
493 empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
495 if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
497 else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
505 * Enable the access to the device; until this function is called,
506 * the client cannot read from the keyboard.
509 atkbd_enable(keyboard_t *kbd)
519 /* disallow the access to the device */
521 atkbd_disable(keyboard_t *kbd)
531 /* read one byte from the keyboard if it's allowed */
533 atkbd_read(keyboard_t *kbd, int wait)
538 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
540 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
543 return (KBD_IS_ACTIVE(kbd) ? c : -1);
546 /* check if data is waiting */
548 atkbd_check(keyboard_t *kbd)
550 if (!KBD_IS_ACTIVE(kbd))
552 return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
555 /* read char from the keyboard */
557 atkbd_read_char(keyboard_t *kbd, int wait)
559 atkbd_state_t *state;
564 state = (atkbd_state_t *)kbd->kb_data;
566 /* do we have a composed char to return? */
567 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
568 action = state->ks_composed_char;
569 state->ks_composed_char = 0;
570 if (action > UCHAR_MAX)
575 /* see if there is something in the keyboard port */
578 scancode = read_kbd_data(state->kbdc);
579 } while (scancode == -1);
581 scancode = read_kbd_data_no_wait(state->kbdc);
587 #if KBDIO_DEBUG >= 10
588 printf("atkbd_read_char(): scancode:0x%x\n", scancode);
591 /* return the byte as is for the K_RAW mode */
592 if (state->ks_mode == K_RAW)
595 /* translate the scan code into a keycode */
596 keycode = scancode & 0x7F;
597 switch (state->ks_prefix) {
598 case 0x00: /* normal scancode */
600 case 0xB8: /* left alt (compose key) released */
601 if (state->ks_flags & COMPOSE) {
602 state->ks_flags &= ~COMPOSE;
603 if (state->ks_composed_char > UCHAR_MAX)
604 state->ks_composed_char = 0;
607 case 0x38: /* left alt (compose key) pressed */
608 if (!(state->ks_flags & COMPOSE)) {
609 state->ks_flags |= COMPOSE;
610 state->ks_composed_char = 0;
615 state->ks_prefix = scancode;
619 case 0xE0: /* 0xE0 prefix */
620 state->ks_prefix = 0;
622 case 0x1C: /* right enter key */
625 case 0x1D: /* right ctrl key */
628 case 0x35: /* keypad divide key */
631 case 0x37: /* print scrn key */
634 case 0x38: /* right alt key (alt gr) */
637 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
640 case 0x47: /* grey home key */
643 case 0x48: /* grey up arrow key */
646 case 0x49: /* grey page up key */
649 case 0x4B: /* grey left arrow key */
652 case 0x4D: /* grey right arrow key */
655 case 0x4F: /* grey end key */
658 case 0x50: /* grey down arrow key */
661 case 0x51: /* grey page down key */
664 case 0x52: /* grey insert key */
667 case 0x53: /* grey delete key */
670 /* the following 3 are only used on the MS "Natural" keyboard */
671 case 0x5b: /* left Window key */
674 case 0x5c: /* right Window key */
677 case 0x5d: /* menu key */
680 default: /* ignore everything else */
684 case 0xE1: /* 0xE1 prefix */
686 * The pause/break key on the 101 keyboard produces:
688 * Ctrl-pause/break produces:
689 * E0-46 E0-C6 (See above.)
691 state->ks_prefix = 0;
693 state->ks_prefix = 0x1D;
696 case 0x1D: /* pause / break */
697 state->ks_prefix = 0;
704 if (kbd->kb_type == KB_84) {
706 case 0x37: /* *(numpad)/print screen */
707 if (state->ks_flags & SHIFTS)
708 keycode = 0x5c; /* print screen */
710 case 0x45: /* num lock/pause */
711 if (state->ks_flags & CTLS)
712 keycode = 0x68; /* pause */
714 case 0x46: /* scroll lock/break */
715 if (state->ks_flags & CTLS)
716 keycode = 0x6c; /* break */
719 } else if (kbd->kb_type == KB_101) {
721 case 0x5c: /* print screen */
722 if (state->ks_flags & ALTS)
723 keycode = 0x54; /* sysrq */
725 case 0x68: /* pause/break */
726 if (state->ks_flags & CTLS)
727 keycode = 0x6c; /* break */
732 /* return the key code in the K_CODE mode */
733 if (state->ks_mode == K_CODE)
734 return (keycode | (scancode & 0x80));
736 /* compose a character code */
737 if (state->ks_flags & COMPOSE) {
738 switch (keycode | (scancode & 0x80)) {
739 /* key pressed, process it */
740 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
741 state->ks_composed_char *= 10;
742 state->ks_composed_char += keycode - 0x40;
743 if (state->ks_composed_char > UCHAR_MAX)
746 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
747 state->ks_composed_char *= 10;
748 state->ks_composed_char += keycode - 0x47;
749 if (state->ks_composed_char > UCHAR_MAX)
752 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
753 state->ks_composed_char *= 10;
754 state->ks_composed_char += keycode - 0x4E;
755 if (state->ks_composed_char > UCHAR_MAX)
758 case 0x52: /* keypad 0 */
759 state->ks_composed_char *= 10;
760 if (state->ks_composed_char > UCHAR_MAX)
764 /* key released, no interest here */
765 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
766 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
767 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
768 case 0xD2: /* keypad 0 */
771 case 0x38: /* left alt key */
775 if (state->ks_composed_char > 0) {
776 state->ks_flags &= ~COMPOSE;
777 state->ks_composed_char = 0;
784 /* keycode to key action */
785 action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
786 &state->ks_state, &state->ks_accents);
793 /* check if char is waiting */
795 atkbd_check_char(keyboard_t *kbd)
797 atkbd_state_t *state;
799 if (!KBD_IS_ACTIVE(kbd))
801 state = (atkbd_state_t *)kbd->kb_data;
802 if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
804 return kbdc_data_ready(state->kbdc);
807 /* some useful control functions */
809 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
811 /* trasnlate LED_XXX bits into the device specific bits */
812 static u_char ledmap[8] = {
813 0, 4, 2, 6, 1, 5, 3, 7,
815 atkbd_state_t *state = kbd->kb_data;
823 case KDGKBMODE: /* get keyboard mode */
824 *(int *)arg = state->ks_mode;
826 case KDSKBMODE: /* set keyboard mode */
827 switch (*(int *)arg) {
829 if (state->ks_mode != K_XLATE) {
830 /* make lock key state and LED state match */
831 state->ks_state &= ~LOCK_MASK;
832 state->ks_state |= KBD_LED_VAL(kbd);
837 if (state->ks_mode != *(int *)arg) {
838 atkbd_clear_state(kbd);
839 state->ks_mode = *(int *)arg;
848 case KDGETLED: /* get keyboard LED */
849 *(int *)arg = KBD_LED_VAL(kbd);
851 case KDSETLED: /* set keyboard LED */
852 /* NOTE: lock key state in ks_state won't be changed */
853 if (*(int *)arg & ~LOCK_MASK) {
858 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
859 if (state->ks_mode == K_XLATE &&
860 kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
866 if (KBD_HAS_DEVICE(kbd)) {
867 error = write_kbd(state->kbdc, KBDC_SET_LEDS,
868 ledmap[i & LED_MASK]);
874 KBD_LED_VAL(kbd) = *(int *)arg;
877 case KDGKBSTATE: /* get lock key state */
878 *(int *)arg = state->ks_state & LOCK_MASK;
880 case KDSKBSTATE: /* set lock key state */
881 if (*(int *)arg & ~LOCK_MASK) {
885 state->ks_state &= ~LOCK_MASK;
886 state->ks_state |= *(int *)arg;
888 /* set LEDs and quit */
889 return atkbd_ioctl(kbd, KDSETLED, arg);
891 case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
893 if (!KBD_HAS_DEVICE(kbd))
895 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
896 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
898 kbd->kb_delay1 = typematic_delay(i);
899 kbd->kb_delay2 = typematic_rate(i);
903 case KDSETRAD: /* set keyboard repeat rate (old interface) */
905 if (!KBD_HAS_DEVICE(kbd))
907 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
909 kbd->kb_delay1 = typematic_delay(*(int *)arg);
910 kbd->kb_delay2 = typematic_rate(*(int *)arg);
914 case PIO_KEYMAP: /* set keyboard translation table */
915 case PIO_KEYMAPENT: /* set keyboard translation table entry */
916 case PIO_DEADKEYMAP: /* set accent key translation table */
917 state->ks_accents = 0;
921 return genkbd_commonioctl(kbd, cmd, arg);
928 /* lock the access to the keyboard */
930 atkbd_lock(keyboard_t *kbd, int lock)
932 return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
935 /* clear the internal state of the keyboard */
937 atkbd_clear_state(keyboard_t *kbd)
939 atkbd_state_t *state;
941 state = (atkbd_state_t *)kbd->kb_data;
943 state->ks_polling = 0;
944 state->ks_state &= LOCK_MASK; /* preserve locking key state */
945 state->ks_accents = 0;
946 state->ks_composed_char = 0;
948 state->ks_prefix = 0; /* XXX */
952 /* save the internal state */
954 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
957 return sizeof(atkbd_state_t);
958 if (len < sizeof(atkbd_state_t))
960 bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
964 /* set the internal state */
966 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
968 if (len < sizeof(atkbd_state_t))
970 if (((atkbd_state_t *)kbd->kb_data)->kbdc
971 != ((atkbd_state_t *)buf)->kbdc)
973 bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
978 atkbd_poll(keyboard_t *kbd, int on)
980 atkbd_state_t *state;
983 state = (atkbd_state_t *)kbd->kb_data;
993 /* local functions */
996 get_typematic(keyboard_t *kbd)
1000 * Only some systems allow us to retrieve the keyboard repeat
1001 * rate previously set via the BIOS...
1003 struct vm86frame vmf;
1006 bzero(&vmf, sizeof(vmf));
1007 vmf.vmf_ax = 0xc000;
1008 vm86_intcall(0x15, &vmf);
1009 if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah)
1011 p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx);
1012 if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */
1014 vmf.vmf_ax = 0x0900;
1015 vm86_intcall(0x16, &vmf);
1016 if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */
1018 vmf.vmf_ax = 0x0306;
1019 vm86_intcall(0x16, &vmf);
1020 kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5);
1021 kbd->kb_delay2 = typematic_rate(vmf.vmf_bl);
1025 #endif /* __i386__ */
1029 setup_kbd_port(KBDC kbdc, int port, int intr)
1031 if (!set_controller_command_byte(kbdc,
1032 KBD_KBD_CONTROL_BITS,
1033 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1034 | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1040 get_kbd_echo(KBDC kbdc)
1042 /* enable the keyboard port, but disable the keyboard intr. */
1043 if (setup_kbd_port(kbdc, TRUE, FALSE))
1044 /* CONTROLLER ERROR: there is very little we can do... */
1047 /* see if something is present */
1048 write_kbd_command(kbdc, KBDC_ECHO);
1049 if (read_kbd_data(kbdc) != KBD_ECHO) {
1050 empty_both_buffers(kbdc, 10);
1051 test_controller(kbdc);
1052 test_kbd_port(kbdc);
1056 /* enable the keyboard port and intr. */
1057 if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1060 * This is serious; the keyboard intr is left disabled!
1069 probe_keyboard(KBDC kbdc, int flags)
1072 * Don't try to print anything in this function. The low-level
1073 * console may not have been initialized yet...
1079 if (!kbdc_lock(kbdc, TRUE)) {
1084 /* temporarily block data transmission from the keyboard */
1085 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1087 /* flush any noise in the buffer */
1088 empty_both_buffers(kbdc, 100);
1090 /* save the current keyboard controller command byte */
1091 m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1092 c = get_controller_command_byte(kbdc);
1094 /* CONTROLLER ERROR */
1095 kbdc_set_device_mask(kbdc, m);
1096 kbdc_lock(kbdc, FALSE);
1101 * The keyboard may have been screwed up by the boot block.
1102 * We may just be able to recover from error by testing the controller
1103 * and the keyboard port. The controller command byte needs to be
1104 * saved before this recovery operation, as some controllers seem
1105 * to set the command byte to particular values.
1107 test_controller(kbdc);
1108 test_kbd_port(kbdc);
1110 err = get_kbd_echo(kbdc);
1113 * Even if the keyboard doesn't seem to be present (err != 0),
1114 * we shall enable the keyboard port and interrupt so that
1115 * the driver will be operable when the keyboard is attached
1116 * to the system later. It is NOT recommended to hot-plug
1117 * the AT keyboard, but many people do so...
1119 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1120 setup_kbd_port(kbdc, TRUE, TRUE);
1123 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1125 /* try to restore the command byte as before */
1126 set_controller_command_byte(kbdc, 0xff, c);
1127 kbdc_set_device_mask(kbdc, m);
1131 kbdc_lock(kbdc, FALSE);
1136 init_keyboard(KBDC kbdc, int *type, int flags)
1142 if (!kbdc_lock(kbdc, TRUE)) {
1147 /* temporarily block data transmission from the keyboard */
1148 write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1150 /* save the current controller command byte */
1151 empty_both_buffers(kbdc, 200);
1152 c = get_controller_command_byte(kbdc);
1154 /* CONTROLLER ERROR */
1155 kbdc_lock(kbdc, FALSE);
1156 printf("atkbd: unable to get the current command byte value.\n");
1160 printf("atkbd: the current kbd controller command byte %04x\n",
1163 /* override the keyboard lock switch */
1164 c |= KBD_OVERRIDE_KBD_LOCK;
1167 /* enable the keyboard port, but disable the keyboard intr. */
1168 if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1169 /* CONTROLLER ERROR: there is very little we can do... */
1170 printf("atkbd: unable to set the command byte.\n");
1171 kbdc_lock(kbdc, FALSE);
1176 * Check if we have an XT keyboard before we attempt to reset it.
1177 * The procedure assumes that the keyboard and the controller have
1178 * been set up properly by BIOS and have not been messed up
1179 * during the boot process.
1182 if (flags & KB_CONF_ALT_SCANCODESET)
1183 /* the user says there is a XT keyboard */
1185 #ifdef KBD_DETECT_XT_KEYBOARD
1186 else if ((c & KBD_TRANSLATION) == 0) {
1187 /* SET_SCANCODE_SET is not always supported; ignore error */
1188 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1190 codeset = read_kbd_data(kbdc);
1193 printf("atkbd: scancode set %d\n", codeset);
1194 #endif /* KBD_DETECT_XT_KEYBOARD */
1197 id = get_kbd_id(kbdc);
1199 case 0x41ab: /* 101/102/... Enhanced */
1200 case 0x83ab: /* ditto */
1201 case 0x54ab: /* SpaceSaver */
1202 case 0x84ab: /* ditto */
1204 case 0x90ab: /* 'G' */
1205 case 0x91ab: /* 'P' */
1206 case 0x92ab: /* 'A' */
1210 case -1: /* AT 84 keyboard doesn't return ID */
1217 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1219 /* reset keyboard hardware */
1220 if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
1223 * Keyboard reset may fail either because the keyboard
1224 * doen't exist, or because the keyboard doesn't pass
1225 * the self-test, or the keyboard controller on the
1226 * motherboard and the keyboard somehow fail to shake hands.
1227 * It is just possible, particularly in the last case,
1228 * that the keyoard controller may be left in a hung state.
1229 * test_controller() and test_kbd_port() appear to bring
1230 * the keyboard controller back (I don't know why and how,
1233 empty_both_buffers(kbdc, 10);
1234 test_controller(kbdc);
1235 test_kbd_port(kbdc);
1237 * We could disable the keyboard port and interrupt... but,
1238 * the keyboard may still exist (see above).
1240 set_controller_command_byte(kbdc, 0xff, c);
1241 kbdc_lock(kbdc, FALSE);
1243 printf("atkbd: failed to reset the keyboard.\n");
1248 * Allow us to set the XT_KEYBD flag so that keyboards
1249 * such as those on the IBM ThinkPad laptop computers can be used
1250 * with the standard console driver.
1253 if (send_kbd_command_and_data(kbdc,
1254 KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1255 /* XT kbd doesn't need scan code translation */
1256 c &= ~KBD_TRANSLATION;
1260 * The XT kbd isn't usable unless the proper scan
1261 * code set is selected.
1263 set_controller_command_byte(kbdc, 0xff, c);
1264 kbdc_lock(kbdc, FALSE);
1265 printf("atkbd: unable to set the XT keyboard mode.\n");
1271 if (send_kbd_command_and_data(
1272 kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) {
1273 printf("atkbd: can't set translation.\n");
1276 c |= KBD_TRANSLATION;
1279 /* enable the keyboard port and intr. */
1280 if (!set_controller_command_byte(kbdc,
1281 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1282 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1283 | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1286 * This is serious; we are left with the disabled
1289 set_controller_command_byte(kbdc, 0xff, c);
1290 kbdc_lock(kbdc, FALSE);
1291 printf("atkbd: unable to enable the keyboard port and intr.\n");
1295 kbdc_lock(kbdc, FALSE);
1300 write_kbd(KBDC kbdc, int command, int data)
1304 /* prevent the timeout routine from polling the keyboard */
1305 if (!kbdc_lock(kbdc, TRUE))
1308 /* disable the keyboard and mouse interrupt */
1311 c = get_controller_command_byte(kbdc);
1313 || !set_controller_command_byte(kbdc,
1314 kbdc_get_device_mask(kbdc),
1315 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1316 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1317 /* CONTROLLER ERROR */
1318 kbdc_lock(kbdc, FALSE);
1323 * Now that the keyboard controller is told not to generate
1324 * the keyboard and mouse interrupts, call `splx()' to allow
1325 * the other tty interrupts. The clock interrupt may also occur,
1326 * but the timeout routine (`scrn_timer()') will be blocked
1327 * by the lock flag set via `kbdc_lock()'
1332 if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1333 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1336 /* restore the interrupts */
1337 if (!set_controller_command_byte(kbdc,
1338 kbdc_get_device_mask(kbdc),
1339 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1340 /* CONTROLLER ERROR */
1345 kbdc_lock(kbdc, FALSE);
1351 get_kbd_id(KBDC kbdc)
1355 empty_both_buffers(kbdc, 10);
1357 if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1360 DELAY(10000); /* 10 msec delay */
1361 id1 = read_kbd_data(kbdc);
1363 id2 = read_kbd_data(kbdc);
1365 if ((id1 == -1) || (id2 == -1)) {
1366 empty_both_buffers(kbdc, 10);
1367 test_controller(kbdc);
1368 test_kbd_port(kbdc);
1371 return ((id2 << 8) | id1);
1374 static int delays[] = { 250, 500, 750, 1000 };
1375 static int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1376 68, 76, 84, 92, 100, 110, 118, 126,
1377 136, 152, 168, 184, 200, 220, 236, 252,
1378 272, 304, 336, 368, 400, 440, 472, 504 };
1381 typematic_delay(int i)
1383 return delays[(i >> 5) & 3];
1387 typematic_rate(int i)
1389 return rates[i & 0x1f];
1393 typematic(int delay, int rate)
1398 for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) {
1399 if (delay >= delays[i])
1403 for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) {
1404 if (rate >= rates[i])