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.
26 * $FreeBSD: src/sys/dev/kbd/atkbd.c,v 1.25.2.4 2002/04/08 19:21:38 asmodai Exp $
27 * $DragonFly: src/sys/dev/misc/kbd/atkbd.c,v 1.13 2006/12/23 00:26:17 swildner Exp $
31 #include "opt_atkbd.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/thread2.h>
42 #include <machine/md_var.h>
43 #include <machine/psl.h>
44 #include <machine/vm86.h>
45 #include <machine/pc/bios.h>
54 #include "atkbdcreg.h"
56 #include <bus/isa/isareg.h>
58 static timeout_t atkbd_timeout
;
61 atkbd_probe_unit(int unit
, int ctlr
, int irq
, int flags
)
63 keyboard_switch_t
*sw
;
67 sw
= kbd_get_switch(ATKBD_DRIVER_NAME
);
73 error
= (*sw
->probe
)(unit
, args
, flags
);
80 atkbd_attach_unit(int unit
, keyboard_t
**kbd
, int ctlr
, int irq
, int flags
)
82 keyboard_switch_t
*sw
;
86 sw
= kbd_get_switch(ATKBD_DRIVER_NAME
);
90 /* reset, initialize and enable the device */
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
);
121 atkbd_timeout(void *arg
)
126 * The original text of the following comments are extracted
127 * from syscons.c (1.287)
129 * With release 2.1 of the Xaccel server, the keyboard is left
130 * hanging pretty often. Apparently an interrupt from the
131 * keyboard is lost, and I don't know why (yet).
132 * This ugly hack calls the low-level interrupt routine if input
133 * is ready for the keyboard and conveniently hides the problem. XXX
135 * Try removing anything stuck in the keyboard controller; whether
136 * it's a keyboard scan code or mouse data. The low-level
137 * interrupt routine doesn't read the mouse data directly,
138 * but the keyboard controller driver will, as a side effect.
141 * And here is bde's original comment about this:
143 * This is necessary to handle edge triggered interrupts - if we
144 * returned when our IRQ is high due to unserviced input, then there
145 * would be no more keyboard IRQs until the keyboard is reset by
148 * The keyboard apparently unwedges the irq in most cases.
151 kbd
= (keyboard_t
*)arg
;
152 if ((*kbdsw
[kbd
->kb_index
]->lock
)(kbd
, TRUE
)) {
154 * We have seen the lock flag is not set. Let's reset
155 * the flag early, otherwise the LED update routine fails
156 * which may want the lock during the interrupt routine.
158 (*kbdsw
[kbd
->kb_index
]->lock
)(kbd
, FALSE
);
159 if ((*kbdsw
[kbd
->kb_index
]->check_char
)(kbd
))
160 (*kbdsw
[kbd
->kb_index
]->intr
)(kbd
, NULL
);
162 callout_reset(&kbd
->kb_atkbd_timeout_ch
, hz
/ 10, atkbd_timeout
, arg
);
168 #include <machine/limits.h>
169 #include <machine/clock.h>
171 #define ATKBD_DEFAULT 0
173 typedef struct atkbd_state
{
174 KBDC kbdc
; /* keyboard controller */
175 int ks_mode
; /* input mode (K_XLATE,K_RAW,K_CODE) */
176 int ks_flags
; /* flags */
177 #define COMPOSE (1 << 0)
179 int ks_state
; /* shift/lock key state */
180 int ks_accents
; /* accent key index (> 0) */
181 u_int ks_composed_char
; /* composed char code (> 0) */
182 u_char ks_prefix
; /* AT scan code prefix */
185 /* keyboard driver declaration */
186 static int atkbd_configure(int flags
);
187 static kbd_probe_t atkbd_probe
;
188 static kbd_init_t atkbd_init
;
189 static kbd_term_t atkbd_term
;
190 static kbd_intr_t atkbd_intr
;
191 static kbd_test_if_t atkbd_test_if
;
192 static kbd_enable_t atkbd_enable
;
193 static kbd_disable_t atkbd_disable
;
194 static kbd_read_t atkbd_read
;
195 static kbd_check_t atkbd_check
;
196 static kbd_read_char_t atkbd_read_char
;
197 static kbd_check_char_t atkbd_check_char
;
198 static kbd_ioctl_t atkbd_ioctl
;
199 static kbd_lock_t atkbd_lock
;
200 static kbd_clear_state_t atkbd_clear_state
;
201 static kbd_get_state_t atkbd_get_state
;
202 static kbd_set_state_t atkbd_set_state
;
203 static kbd_poll_mode_t atkbd_poll
;
205 keyboard_switch_t atkbdsw
= {
227 KEYBOARD_DRIVER(atkbd
, atkbdsw
, atkbd_configure
);
229 /* local functions */
230 static int get_typematic(keyboard_t
*kbd
);
231 static int setup_kbd_port(KBDC kbdc
, int port
, int intr
);
232 static int get_kbd_echo(KBDC kbdc
);
233 static int probe_keyboard(KBDC kbdc
, int flags
);
234 static int init_keyboard(KBDC kbdc
, int *type
, int flags
);
235 static int write_kbd(KBDC kbdc
, int command
, int data
);
236 static int get_kbd_id(KBDC kbdc
);
237 static int typematic(int delay
, int rate
);
238 static int typematic_delay(int delay
);
239 static int typematic_rate(int rate
);
241 /* local variables */
243 /* the initial key map, accent map and fkey strings */
244 #ifdef ATKBD_DFLT_KEYMAP
245 #define KBD_DFLT_KEYMAP
246 #include "atkbdmap.h"
248 #include "kbdtables.h"
250 /* structures for the default keyboard */
251 static keyboard_t default_kbd
;
252 static atkbd_state_t default_kbd_state
;
253 static keymap_t default_keymap
;
254 static accentmap_t default_accentmap
;
255 static fkeytab_t default_fkeytab
[NUM_FKEYS
];
258 * The back door to the keyboard driver!
259 * This function is called by the console driver, via the kbdio module,
260 * to tickle keyboard drivers when the low-level console is being initialized.
261 * Almost nothing in the kernel has been initialied yet. Try to probe
262 * keyboards if possible.
263 * NOTE: because of the way the low-level console is initialized, this routine
264 * may be called more than once!!
267 atkbd_configure(int flags
)
273 /* probe the keyboard controller */
276 /* if the driver is disabled, unregister the keyboard if any */
277 if ((resource_int_value("atkbd", ATKBD_DEFAULT
, "disabled", &i
) == 0)
279 i
= kbd_find_keyboard(ATKBD_DRIVER_NAME
, ATKBD_DEFAULT
);
281 kbd
= kbd_get_keyboard(i
);
283 kbd
->kb_flags
&= ~KB_REGISTERED
;
288 /* XXX: a kludge to obtain the device configuration flags */
289 if (resource_int_value("atkbd", ATKBD_DEFAULT
, "flags", &i
) == 0)
292 /* probe the default keyboard */
296 if (atkbd_probe(ATKBD_DEFAULT
, arg
, flags
))
298 if (atkbd_init(ATKBD_DEFAULT
, &kbd
, arg
, flags
))
301 /* return the number of found keyboards */
305 /* low-level functions */
307 /* detect a keyboard */
309 atkbd_probe(int unit
, void *arg
, int flags
)
312 int *data
= (int *)arg
; /* data[0]: controller, data[1]: irq */
314 if (unit
== ATKBD_DEFAULT
) {
315 if (KBD_IS_PROBED(&default_kbd
))
319 kbdc
= atkbdc_open(data
[0]);
322 if (probe_keyboard(kbdc
, flags
)) {
323 if (flags
& KB_CONF_FAIL_IF_NO_KBD
)
329 /* reset and initialize the device */
331 atkbd_init(int unit
, keyboard_t
**kbdp
, void *arg
, int flags
)
334 atkbd_state_t
*state
;
340 int *data
= (int *)arg
; /* data[0]: controller, data[1]: irq */
343 if (unit
== ATKBD_DEFAULT
) {
344 *kbdp
= kbd
= &default_kbd
;
345 if (KBD_IS_INITIALIZED(kbd
) && KBD_IS_CONFIGURED(kbd
))
347 state
= &default_kbd_state
;
348 keymap
= &default_keymap
;
349 accmap
= &default_accentmap
;
350 fkeymap
= default_fkeytab
;
352 sizeof(default_fkeytab
)/sizeof(default_fkeytab
[0]);
353 } else if (*kbdp
== NULL
) {
354 *kbdp
= kbd
= kmalloc(sizeof(*kbd
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
355 state
= kmalloc(sizeof(*state
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
356 keymap
= kmalloc(sizeof(key_map
), M_DEVBUF
, M_WAITOK
);
357 accmap
= kmalloc(sizeof(accent_map
), M_DEVBUF
, M_WAITOK
);
358 fkeymap
= kmalloc(sizeof(fkey_tab
), M_DEVBUF
, M_WAITOK
);
359 fkeymap_size
= sizeof(fkey_tab
)/sizeof(fkey_tab
[0]);
360 } else if (KBD_IS_INITIALIZED(*kbdp
) && KBD_IS_CONFIGURED(*kbdp
)) {
364 state
= (atkbd_state_t
*)kbd
->kb_data
;
365 bzero(state
, sizeof(*state
));
366 keymap
= kbd
->kb_keymap
;
367 accmap
= kbd
->kb_accentmap
;
368 fkeymap
= kbd
->kb_fkeytab
;
369 fkeymap_size
= kbd
->kb_fkeytab_size
;
372 if (!KBD_IS_PROBED(kbd
)) {
373 state
->kbdc
= atkbdc_open(data
[0]);
374 if (state
->kbdc
== NULL
)
376 kbd_init_struct(kbd
, ATKBD_DRIVER_NAME
, KB_OTHER
, unit
, flags
,
378 bcopy(&key_map
, keymap
, sizeof(key_map
));
379 bcopy(&accent_map
, accmap
, sizeof(accent_map
));
380 bcopy(fkey_tab
, fkeymap
,
381 imin(fkeymap_size
*sizeof(fkeymap
[0]), sizeof(fkey_tab
)));
382 kbd_set_maps(kbd
, keymap
, accmap
, fkeymap
, fkeymap_size
);
383 kbd
->kb_data
= (void *)state
;
385 if (probe_keyboard(state
->kbdc
, flags
)) { /* shouldn't happen */
386 if (flags
& KB_CONF_FAIL_IF_NO_KBD
)
389 KBD_FOUND_DEVICE(kbd
);
391 atkbd_clear_state(kbd
);
392 state
->ks_mode
= K_XLATE
;
394 * FIXME: set the initial value for lock keys in ks_state
395 * according to the BIOS data?
399 if (!KBD_IS_INITIALIZED(kbd
) && !(flags
& KB_CONF_PROBE_ONLY
)) {
400 kbd
->kb_config
= flags
& ~KB_CONF_PROBE_ONLY
;
401 if (KBD_HAS_DEVICE(kbd
)
402 && init_keyboard(state
->kbdc
, &kbd
->kb_type
, kbd
->kb_config
)
403 && (kbd
->kb_config
& KB_CONF_FAIL_IF_NO_KBD
))
405 atkbd_ioctl(kbd
, KDSETLED
, (caddr_t
)&state
->ks_state
);
407 delay
[0] = kbd
->kb_delay1
;
408 delay
[1] = kbd
->kb_delay2
;
409 atkbd_ioctl(kbd
, KDSETREPEAT
, (caddr_t
)delay
);
412 if (!KBD_IS_CONFIGURED(kbd
)) {
413 if (kbd_register(kbd
) < 0)
415 KBD_CONFIG_DONE(kbd
);
421 /* finish using this keyboard */
423 atkbd_term(keyboard_t
*kbd
)
429 /* keyboard interrupt routine */
431 atkbd_intr(keyboard_t
*kbd
, void *arg
)
433 atkbd_state_t
*state
;
437 if (KBD_IS_ACTIVE(kbd
) && KBD_IS_BUSY(kbd
)) {
438 /* let the callback function to process the input */
439 (*kbd
->kb_callback
.kc_func
)(kbd
, KBDIO_KEYINPUT
,
440 kbd
->kb_callback
.kc_arg
);
442 /* read and discard the input; no one is waiting for input */
444 c
= atkbd_read_char(kbd
, FALSE
);
445 } while (c
!= NOKEY
);
447 if (!KBD_HAS_DEVICE(kbd
)) {
449 * The keyboard was not detected before;
450 * it must have been reconnected!
452 state
= (atkbd_state_t
*)kbd
->kb_data
;
453 init_keyboard(state
->kbdc
, &kbd
->kb_type
,
455 atkbd_ioctl(kbd
, KDSETLED
, (caddr_t
)&state
->ks_state
);
457 delay
[0] = kbd
->kb_delay1
;
458 delay
[1] = kbd
->kb_delay2
;
459 atkbd_ioctl(kbd
, KDSETREPEAT
, (caddr_t
)delay
);
460 KBD_FOUND_DEVICE(kbd
);
466 /* test the interface to the device */
468 atkbd_test_if(keyboard_t
*kbd
)
473 empty_both_buffers(((atkbd_state_t
*)kbd
->kb_data
)->kbdc
, 10);
475 if (!test_controller(((atkbd_state_t
*)kbd
->kb_data
)->kbdc
))
477 else if (test_kbd_port(((atkbd_state_t
*)kbd
->kb_data
)->kbdc
) != 0)
485 * Enable the access to the device; until this function is called,
486 * the client cannot read from the keyboard.
489 atkbd_enable(keyboard_t
*kbd
)
497 /* disallow the access to the device */
499 atkbd_disable(keyboard_t
*kbd
)
507 /* read one byte from the keyboard if it's allowed */
509 atkbd_read(keyboard_t
*kbd
, int wait
)
514 c
= read_kbd_data(((atkbd_state_t
*)kbd
->kb_data
)->kbdc
);
516 c
= read_kbd_data_no_wait(((atkbd_state_t
*)kbd
->kb_data
)->kbdc
);
519 return (KBD_IS_ACTIVE(kbd
) ? c
: -1);
522 /* check if data is waiting */
524 atkbd_check(keyboard_t
*kbd
)
526 if (!KBD_IS_ACTIVE(kbd
))
528 return kbdc_data_ready(((atkbd_state_t
*)kbd
->kb_data
)->kbdc
);
531 /* read char from the keyboard */
533 atkbd_read_char(keyboard_t
*kbd
, int wait
)
535 atkbd_state_t
*state
;
540 state
= (atkbd_state_t
*)kbd
->kb_data
;
542 /* do we have a composed char to return? */
543 if (!(state
->ks_flags
& COMPOSE
) && (state
->ks_composed_char
> 0)) {
544 action
= state
->ks_composed_char
;
545 state
->ks_composed_char
= 0;
546 if (action
> UCHAR_MAX
)
551 /* see if there is something in the keyboard port */
554 scancode
= read_kbd_data(state
->kbdc
);
555 } while (scancode
== -1);
557 scancode
= read_kbd_data_no_wait(state
->kbdc
);
563 #if KBDIO_DEBUG >= 10
564 kprintf("atkbd_read_char(): scancode:0x%x\n", scancode
);
567 /* return the byte as is for the K_RAW mode */
568 if (state
->ks_mode
== K_RAW
)
571 /* translate the scan code into a keycode */
572 keycode
= scancode
& 0x7F;
573 switch (state
->ks_prefix
) {
574 case 0x00: /* normal scancode */
576 case 0xB8: /* left alt (compose key) released */
577 if (state
->ks_flags
& COMPOSE
) {
578 state
->ks_flags
&= ~COMPOSE
;
579 if (state
->ks_composed_char
> UCHAR_MAX
)
580 state
->ks_composed_char
= 0;
583 case 0x38: /* left alt (compose key) pressed */
584 if (!(state
->ks_flags
& COMPOSE
)) {
585 state
->ks_flags
|= COMPOSE
;
586 state
->ks_composed_char
= 0;
591 state
->ks_prefix
= scancode
;
595 case 0xE0: /* 0xE0 prefix */
596 state
->ks_prefix
= 0;
598 case 0x1C: /* right enter key */
601 case 0x1D: /* right ctrl key */
604 case 0x35: /* keypad divide key */
607 case 0x37: /* print scrn key */
610 case 0x38: /* right alt key (alt gr) */
613 case 0x46: /* ctrl-pause/break on AT 101 (see below) */
616 case 0x47: /* grey home key */
619 case 0x48: /* grey up arrow key */
622 case 0x49: /* grey page up key */
625 case 0x4B: /* grey left arrow key */
628 case 0x4D: /* grey right arrow key */
631 case 0x4F: /* grey end key */
634 case 0x50: /* grey down arrow key */
637 case 0x51: /* grey page down key */
640 case 0x52: /* grey insert key */
643 case 0x53: /* grey delete key */
646 /* the following 3 are only used on the MS "Natural" keyboard */
647 case 0x5b: /* left Window key */
650 case 0x5c: /* right Window key */
653 case 0x5d: /* menu key */
656 default: /* ignore everything else */
660 case 0xE1: /* 0xE1 prefix */
662 * The pause/break key on the 101 keyboard produces:
664 * Ctrl-pause/break produces:
665 * E0-46 E0-C6 (See above.)
667 state
->ks_prefix
= 0;
669 state
->ks_prefix
= 0x1D;
672 case 0x1D: /* pause / break */
673 state
->ks_prefix
= 0;
680 if (kbd
->kb_type
== KB_84
) {
682 case 0x37: /* *(numpad)/print screen */
683 if (state
->ks_flags
& SHIFTS
)
684 keycode
= 0x5c; /* print screen */
686 case 0x45: /* num lock/pause */
687 if (state
->ks_flags
& CTLS
)
688 keycode
= 0x68; /* pause */
690 case 0x46: /* scroll lock/break */
691 if (state
->ks_flags
& CTLS
)
692 keycode
= 0x6c; /* break */
695 } else if (kbd
->kb_type
== KB_101
) {
697 case 0x5c: /* print screen */
698 if (state
->ks_flags
& ALTS
)
699 keycode
= 0x54; /* sysrq */
701 case 0x68: /* pause/break */
702 if (state
->ks_flags
& CTLS
)
703 keycode
= 0x6c; /* break */
708 /* return the key code in the K_CODE mode */
709 if (state
->ks_mode
== K_CODE
)
710 return (keycode
| (scancode
& 0x80));
712 /* compose a character code */
713 if (state
->ks_flags
& COMPOSE
) {
714 switch (keycode
| (scancode
& 0x80)) {
715 /* key pressed, process it */
716 case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
717 state
->ks_composed_char
*= 10;
718 state
->ks_composed_char
+= keycode
- 0x40;
719 if (state
->ks_composed_char
> UCHAR_MAX
)
722 case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
723 state
->ks_composed_char
*= 10;
724 state
->ks_composed_char
+= keycode
- 0x47;
725 if (state
->ks_composed_char
> UCHAR_MAX
)
728 case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
729 state
->ks_composed_char
*= 10;
730 state
->ks_composed_char
+= keycode
- 0x4E;
731 if (state
->ks_composed_char
> UCHAR_MAX
)
734 case 0x52: /* keypad 0 */
735 state
->ks_composed_char
*= 10;
736 if (state
->ks_composed_char
> UCHAR_MAX
)
740 /* key released, no interest here */
741 case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
742 case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
743 case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
744 case 0xD2: /* keypad 0 */
747 case 0x38: /* left alt key */
751 if (state
->ks_composed_char
> 0) {
752 state
->ks_flags
&= ~COMPOSE
;
753 state
->ks_composed_char
= 0;
760 /* keycode to key action */
761 action
= genkbd_keyaction(kbd
, keycode
, scancode
& 0x80,
762 &state
->ks_state
, &state
->ks_accents
);
769 /* check if char is waiting */
771 atkbd_check_char(keyboard_t
*kbd
)
773 atkbd_state_t
*state
;
775 if (!KBD_IS_ACTIVE(kbd
))
777 state
= (atkbd_state_t
*)kbd
->kb_data
;
778 if (!(state
->ks_flags
& COMPOSE
) && (state
->ks_composed_char
> 0))
780 return kbdc_data_ready(state
->kbdc
);
783 /* some useful control functions */
785 atkbd_ioctl(keyboard_t
*kbd
, u_long cmd
, caddr_t arg
)
787 /* trasnlate LED_XXX bits into the device specific bits */
788 static u_char ledmap
[8] = {
789 0, 4, 2, 6, 1, 5, 3, 7,
791 atkbd_state_t
*state
= kbd
->kb_data
;
799 case KDGKBMODE
: /* get keyboard mode */
800 *(int *)arg
= state
->ks_mode
;
802 case KDSKBMODE
: /* set keyboard mode */
803 switch (*(int *)arg
) {
805 if (state
->ks_mode
!= K_XLATE
) {
806 /* make lock key state and LED state match */
807 state
->ks_state
&= ~LOCK_MASK
;
808 state
->ks_state
|= KBD_LED_VAL(kbd
);
813 if (state
->ks_mode
!= *(int *)arg
) {
814 atkbd_clear_state(kbd
);
815 state
->ks_mode
= *(int *)arg
;
824 case KDGETLED
: /* get keyboard LED */
825 *(int *)arg
= KBD_LED_VAL(kbd
);
827 case KDSETLED
: /* set keyboard LED */
828 /* NOTE: lock key state in ks_state won't be changed */
829 if (*(int *)arg
& ~LOCK_MASK
) {
834 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
835 if (state
->ks_mode
== K_XLATE
&&
836 kbd
->kb_keymap
->n_keys
> ALTGR_OFFSET
) {
842 if (KBD_HAS_DEVICE(kbd
)) {
843 error
= write_kbd(state
->kbdc
, KBDC_SET_LEDS
,
844 ledmap
[i
& LED_MASK
]);
850 KBD_LED_VAL(kbd
) = *(int *)arg
;
853 case KDGKBSTATE
: /* get lock key state */
854 *(int *)arg
= state
->ks_state
& LOCK_MASK
;
856 case KDSKBSTATE
: /* set lock key state */
857 if (*(int *)arg
& ~LOCK_MASK
) {
861 state
->ks_state
&= ~LOCK_MASK
;
862 state
->ks_state
|= *(int *)arg
;
864 /* set LEDs and quit */
865 return atkbd_ioctl(kbd
, KDSETLED
, arg
);
867 case KDSETREPEAT
: /* set keyboard repeat rate (new interface) */
869 if (!KBD_HAS_DEVICE(kbd
))
871 i
= typematic(((int *)arg
)[0], ((int *)arg
)[1]);
872 error
= write_kbd(state
->kbdc
, KBDC_SET_TYPEMATIC
, i
);
874 kbd
->kb_delay1
= typematic_delay(i
);
875 kbd
->kb_delay2
= typematic_rate(i
);
879 case KDSETRAD
: /* set keyboard repeat rate (old interface) */
881 if (!KBD_HAS_DEVICE(kbd
))
883 error
= write_kbd(state
->kbdc
, KBDC_SET_TYPEMATIC
, *(int *)arg
);
885 kbd
->kb_delay1
= typematic_delay(*(int *)arg
);
886 kbd
->kb_delay2
= typematic_rate(*(int *)arg
);
890 case PIO_KEYMAP
: /* set keyboard translation table */
891 case PIO_KEYMAPENT
: /* set keyboard translation table entry */
892 case PIO_DEADKEYMAP
: /* set accent key translation table */
893 state
->ks_accents
= 0;
897 return genkbd_commonioctl(kbd
, cmd
, arg
);
904 /* lock the access to the keyboard */
906 atkbd_lock(keyboard_t
*kbd
, int lock
)
908 return kbdc_lock(((atkbd_state_t
*)kbd
->kb_data
)->kbdc
, lock
);
911 /* clear the internal state of the keyboard */
913 atkbd_clear_state(keyboard_t
*kbd
)
915 atkbd_state_t
*state
;
917 state
= (atkbd_state_t
*)kbd
->kb_data
;
919 state
->ks_polling
= 0;
920 state
->ks_state
&= LOCK_MASK
; /* preserve locking key state */
921 state
->ks_accents
= 0;
922 state
->ks_composed_char
= 0;
924 state
->ks_prefix
= 0; /* XXX */
928 /* save the internal state */
930 atkbd_get_state(keyboard_t
*kbd
, void *buf
, size_t len
)
933 return sizeof(atkbd_state_t
);
934 if (len
< sizeof(atkbd_state_t
))
936 bcopy(kbd
->kb_data
, buf
, sizeof(atkbd_state_t
));
940 /* set the internal state */
942 atkbd_set_state(keyboard_t
*kbd
, void *buf
, size_t len
)
944 if (len
< sizeof(atkbd_state_t
))
946 if (((atkbd_state_t
*)kbd
->kb_data
)->kbdc
947 != ((atkbd_state_t
*)buf
)->kbdc
)
949 bcopy(buf
, kbd
->kb_data
, sizeof(atkbd_state_t
));
954 atkbd_poll(keyboard_t
*kbd
, int on
)
956 atkbd_state_t
*state
;
958 state
= (atkbd_state_t
*)kbd
->kb_data
;
968 /* local functions */
971 get_typematic(keyboard_t
*kbd
)
975 * Only some systems allow us to retrieve the keyboard repeat
976 * rate previously set via the BIOS...
978 struct vm86frame vmf
;
981 bzero(&vmf
, sizeof(vmf
));
983 vm86_intcall(0x15, &vmf
);
984 if ((vmf
.vmf_eflags
& PSL_C
) || vmf
.vmf_ah
)
986 p
= BIOS_PADDRTOVADDR(((u_int32_t
)vmf
.vmf_es
<< 4) + vmf
.vmf_bx
);
987 if ((readb(p
+ 6) & 0x40) == 0) /* int 16, function 0x09 supported? */
990 vm86_intcall(0x16, &vmf
);
991 if ((vmf
.vmf_al
& 0x08) == 0) /* int 16, function 0x0306 supported? */
994 vm86_intcall(0x16, &vmf
);
995 kbd
->kb_delay1
= typematic_delay(vmf
.vmf_bh
<< 5);
996 kbd
->kb_delay2
= typematic_rate(vmf
.vmf_bl
);
1000 #endif /* __i386__ */
1004 setup_kbd_port(KBDC kbdc
, int port
, int intr
)
1006 if (!set_controller_command_byte(kbdc
,
1007 KBD_KBD_CONTROL_BITS
,
1008 ((port
) ? KBD_ENABLE_KBD_PORT
: KBD_DISABLE_KBD_PORT
)
1009 | ((intr
) ? KBD_ENABLE_KBD_INT
: KBD_DISABLE_KBD_INT
)))
1015 get_kbd_echo(KBDC kbdc
)
1017 /* enable the keyboard port, but disable the keyboard intr. */
1018 if (setup_kbd_port(kbdc
, TRUE
, FALSE
))
1019 /* CONTROLLER ERROR: there is very little we can do... */
1022 /* see if something is present */
1023 write_kbd_command(kbdc
, KBDC_ECHO
);
1024 if (read_kbd_data(kbdc
) != KBD_ECHO
) {
1025 empty_both_buffers(kbdc
, 10);
1026 test_controller(kbdc
);
1027 test_kbd_port(kbdc
);
1031 /* enable the keyboard port and intr. */
1032 if (setup_kbd_port(kbdc
, TRUE
, TRUE
)) {
1035 * This is serious; the keyboard intr is left disabled!
1044 probe_keyboard(KBDC kbdc
, int flags
)
1047 * Don't try to print anything in this function. The low-level
1048 * console may not have been initialized yet...
1054 if (!kbdc_lock(kbdc
, TRUE
)) {
1059 /* temporarily block data transmission from the keyboard */
1060 write_controller_command(kbdc
, KBDC_DISABLE_KBD_PORT
);
1062 /* flush any noise in the buffer */
1063 empty_both_buffers(kbdc
, 100);
1065 /* save the current keyboard controller command byte */
1066 m
= kbdc_get_device_mask(kbdc
) & ~KBD_KBD_CONTROL_BITS
;
1067 c
= get_controller_command_byte(kbdc
);
1069 /* CONTROLLER ERROR */
1070 kbdc_set_device_mask(kbdc
, m
);
1071 kbdc_lock(kbdc
, FALSE
);
1076 * The keyboard may have been screwed up by the boot block.
1077 * We may just be able to recover from error by testing the controller
1078 * and the keyboard port. The controller command byte needs to be
1079 * saved before this recovery operation, as some controllers seem
1080 * to set the command byte to particular values.
1082 test_controller(kbdc
);
1083 test_kbd_port(kbdc
);
1085 err
= get_kbd_echo(kbdc
);
1088 * Even if the keyboard doesn't seem to be present (err != 0),
1089 * we shall enable the keyboard port and interrupt so that
1090 * the driver will be operable when the keyboard is attached
1091 * to the system later. It is NOT recommended to hot-plug
1092 * the AT keyboard, but many people do so...
1094 kbdc_set_device_mask(kbdc
, m
| KBD_KBD_CONTROL_BITS
);
1095 setup_kbd_port(kbdc
, TRUE
, TRUE
);
1098 kbdc_set_device_mask(kbdc
, m
| KBD_KBD_CONTROL_BITS
);
1100 /* try to restore the command byte as before */
1101 set_controller_command_byte(kbdc
, 0xff, c
);
1102 kbdc_set_device_mask(kbdc
, m
);
1106 kbdc_lock(kbdc
, FALSE
);
1111 init_keyboard(KBDC kbdc
, int *type
, int flags
)
1117 if (!kbdc_lock(kbdc
, TRUE
)) {
1122 /* temporarily block data transmission from the keyboard */
1123 write_controller_command(kbdc
, KBDC_DISABLE_KBD_PORT
);
1125 /* save the current controller command byte */
1126 empty_both_buffers(kbdc
, 200);
1127 c
= get_controller_command_byte(kbdc
);
1129 /* CONTROLLER ERROR */
1130 kbdc_lock(kbdc
, FALSE
);
1131 kprintf("atkbd: unable to get the current command byte value.\n");
1135 kprintf("atkbd: the current kbd controller command byte %04x\n",
1138 /* override the keyboard lock switch */
1139 c
|= KBD_OVERRIDE_KBD_LOCK
;
1142 /* enable the keyboard port, but disable the keyboard intr. */
1143 if (setup_kbd_port(kbdc
, TRUE
, FALSE
)) {
1144 /* CONTROLLER ERROR: there is very little we can do... */
1145 kprintf("atkbd: unable to set the command byte.\n");
1146 kbdc_lock(kbdc
, FALSE
);
1151 * Check if we have an XT keyboard before we attempt to reset it.
1152 * The procedure assumes that the keyboard and the controller have
1153 * been set up properly by BIOS and have not been messed up
1154 * during the boot process.
1157 if (flags
& KB_CONF_ALT_SCANCODESET
)
1158 /* the user says there is a XT keyboard */
1160 #ifdef KBD_DETECT_XT_KEYBOARD
1161 else if ((c
& KBD_TRANSLATION
) == 0) {
1162 /* SET_SCANCODE_SET is not always supported; ignore error */
1163 if (send_kbd_command_and_data(kbdc
, KBDC_SET_SCANCODE_SET
, 0)
1165 codeset
= read_kbd_data(kbdc
);
1168 kprintf("atkbd: scancode set %d\n", codeset
);
1169 #endif /* KBD_DETECT_XT_KEYBOARD */
1172 id
= get_kbd_id(kbdc
);
1174 case 0x41ab: /* 101/102/... Enhanced */
1175 case 0x83ab: /* ditto */
1176 case 0x54ab: /* SpaceSaver */
1177 case 0x84ab: /* ditto */
1179 case 0x90ab: /* 'G' */
1180 case 0x91ab: /* 'P' */
1181 case 0x92ab: /* 'A' */
1185 case -1: /* AT 84 keyboard doesn't return ID */
1192 kprintf("atkbd: keyboard ID 0x%x (%d)\n", id
, *type
);
1194 /* reset keyboard hardware */
1195 if (!(flags
& KB_CONF_NO_RESET
) && !reset_kbd(kbdc
)) {
1198 * Keyboard reset may fail either because the keyboard
1199 * doen't exist, or because the keyboard doesn't pass
1200 * the self-test, or the keyboard controller on the
1201 * motherboard and the keyboard somehow fail to shake hands.
1202 * It is just possible, particularly in the last case,
1203 * that the keyoard controller may be left in a hung state.
1204 * test_controller() and test_kbd_port() appear to bring
1205 * the keyboard controller back (I don't know why and how,
1208 empty_both_buffers(kbdc
, 10);
1209 test_controller(kbdc
);
1210 test_kbd_port(kbdc
);
1212 * We could disable the keyboard port and interrupt... but,
1213 * the keyboard may still exist (see above).
1215 set_controller_command_byte(kbdc
, 0xff, c
);
1216 kbdc_lock(kbdc
, FALSE
);
1218 kprintf("atkbd: failed to reset the keyboard.\n");
1223 * Allow us to set the XT_KEYBD flag in UserConfig so that keyboards
1224 * such as those on the IBM ThinkPad laptop computers can be used
1225 * with the standard console driver.
1228 if (send_kbd_command_and_data(kbdc
,
1229 KBDC_SET_SCANCODE_SET
, codeset
) == KBD_ACK
) {
1230 /* XT kbd doesn't need scan code translation */
1231 c
&= ~KBD_TRANSLATION
;
1235 * The XT kbd isn't usable unless the proper scan
1236 * code set is selected.
1238 set_controller_command_byte(kbdc
, 0xff, c
);
1239 kbdc_lock(kbdc
, FALSE
);
1240 kprintf("atkbd: unable to set the XT keyboard mode.\n");
1245 /* enable the keyboard port and intr. */
1246 if (!set_controller_command_byte(kbdc
,
1247 KBD_KBD_CONTROL_BITS
| KBD_TRANSLATION
| KBD_OVERRIDE_KBD_LOCK
,
1248 (c
& (KBD_TRANSLATION
| KBD_OVERRIDE_KBD_LOCK
))
1249 | KBD_ENABLE_KBD_PORT
| KBD_ENABLE_KBD_INT
)) {
1252 * This is serious; we are left with the disabled
1255 set_controller_command_byte(kbdc
, 0xff, c
);
1256 kbdc_lock(kbdc
, FALSE
);
1257 kprintf("atkbd: unable to enable the keyboard port and intr.\n");
1261 kbdc_lock(kbdc
, FALSE
);
1266 write_kbd(KBDC kbdc
, int command
, int data
)
1268 /* prevent the timeout routine from polling the keyboard */
1269 if (!kbdc_lock(kbdc
, TRUE
))
1272 /* disable the keyboard and mouse interrupt */
1275 c
= get_controller_command_byte(kbdc
);
1277 || !set_controller_command_byte(kbdc
,
1278 kbdc_get_device_mask(kbdc
),
1279 KBD_DISABLE_KBD_PORT
| KBD_DISABLE_KBD_INT
1280 | KBD_DISABLE_AUX_PORT
| KBD_DISABLE_AUX_INT
)) {
1281 /* CONTROLLER ERROR */
1282 kbdc_lock(kbdc
, FALSE
);
1287 * Now that the keyboard controller is told not to generate
1288 * the keyboard and mouse interrupts, call `splx()' to allow
1289 * the other tty interrupts. The clock interrupt may also occur,
1290 * but the timeout routine (`scrn_timer()') will be blocked
1291 * by the lock flag set via `kbdc_lock()'
1296 if (send_kbd_command_and_data(kbdc
, command
, data
) != KBD_ACK
)
1297 send_kbd_command(kbdc
, KBDC_ENABLE_KBD
);
1300 /* restore the interrupts */
1301 if (!set_controller_command_byte(kbdc
,
1302 kbdc_get_device_mask(kbdc
),
1303 c
& (KBD_KBD_CONTROL_BITS
| KBD_AUX_CONTROL_BITS
))) {
1304 /* CONTROLLER ERROR */
1309 kbdc_lock(kbdc
, FALSE
);
1315 get_kbd_id(KBDC kbdc
)
1319 empty_both_buffers(kbdc
, 10);
1321 if (send_kbd_command(kbdc
, KBDC_SEND_DEV_ID
) != KBD_ACK
)
1324 DELAY(10000); /* 10 msec delay */
1325 id1
= read_kbd_data(kbdc
);
1327 id2
= read_kbd_data(kbdc
);
1329 if ((id1
== -1) || (id2
== -1)) {
1330 empty_both_buffers(kbdc
, 10);
1331 test_controller(kbdc
);
1332 test_kbd_port(kbdc
);
1335 return ((id2
<< 8) | id1
);
1338 static int delays
[] = { 250, 500, 750, 1000 };
1339 static int rates
[] = { 34, 38, 42, 46, 50, 55, 59, 63,
1340 68, 76, 84, 92, 100, 110, 118, 126,
1341 136, 152, 168, 184, 200, 220, 236, 252,
1342 272, 304, 336, 368, 400, 440, 472, 504 };
1345 typematic_delay(int i
)
1347 return delays
[(i
>> 5) & 3];
1351 typematic_rate(int i
)
1353 return rates
[i
& 0x1f];
1357 typematic(int delay
, int rate
)
1362 for (i
= sizeof(delays
)/sizeof(delays
[0]) - 1; i
> 0; --i
) {
1363 if (delay
>= delays
[i
])
1367 for (i
= sizeof(rates
)/sizeof(rates
[0]) - 1; i
> 0; --i
) {
1368 if (rate
>= rates
[i
])