atkbd.c revision 130312
142421Syokota/*- 242421Syokota * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 342421Syokota * All rights reserved. 442421Syokota * 542421Syokota * Redistribution and use in source and binary forms, with or without 642421Syokota * modification, are permitted provided that the following conditions 742421Syokota * are met: 842421Syokota * 1. Redistributions of source code must retain the above copyright 942421Syokota * notice, this list of conditions and the following disclaimer as 1042421Syokota * the first lines of this file unmodified. 1142421Syokota * 2. Redistributions in binary form must reproduce the above copyright 1242421Syokota * notice, this list of conditions and the following disclaimer in the 1342421Syokota * documentation and/or other materials provided with the distribution. 1442421Syokota * 1542421Syokota * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 1642421Syokota * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1742421Syokota * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1842421Syokota * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 1942421Syokota * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2042421Syokota * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2142421Syokota * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2242421Syokota * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2342421Syokota * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2442421Syokota * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2542421Syokota * 2642421Syokota */ 2742421Syokota 28119418Sobrien#include <sys/cdefs.h> 29119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/atkbdc/atkbd.c 130312 2004-06-10 20:31:00Z jhb $"); 30119418Sobrien 3142421Syokota#include "opt_kbd.h" 3244628Syokota#include "opt_atkbd.h" 3342421Syokota 3442421Syokota#include <sys/param.h> 3542421Syokota#include <sys/systm.h> 3642421Syokota#include <sys/kernel.h> 3747336Syokota#include <sys/bus.h> 3842421Syokota#include <sys/proc.h> 39114216Skan#include <sys/limits.h> 4042421Syokota#include <sys/malloc.h> 4142421Syokota 4258271Syokota#include <machine/bus.h> 4358271Syokota#include <machine/resource.h> 4458271Syokota 4555731Syokota#ifdef __i386__ 4655731Syokota#include <machine/md_var.h> 4755731Syokota#include <machine/psl.h> 4855731Syokota#include <machine/vm86.h> 4955731Syokota#include <machine/pc/bios.h> 5055731Syokota 5155731Syokota#include <vm/vm.h> 5255731Syokota#include <vm/pmap.h> 53130312Sjhb#include <vm/vm_param.h> 5455731Syokota#endif /* __i386__ */ 5555731Syokota 5680040Syokota#include <sys/kbio.h> 5742421Syokota#include <dev/kbd/kbdreg.h> 5842421Syokota#include <dev/kbd/atkbdreg.h> 5942421Syokota#include <dev/kbd/atkbdcreg.h> 6042421Syokota 6142831Syokota#include <isa/isareg.h> 6242831Syokota 6342421Syokotastatic timeout_t atkbd_timeout; 6442421Syokota 6542421Syokotaint 6658271Syokotaatkbd_probe_unit(int unit, int ctlr, int irq, int flags) 6742421Syokota{ 6842421Syokota keyboard_switch_t *sw; 6942421Syokota int args[2]; 7044628Syokota int error; 7142421Syokota 7242421Syokota sw = kbd_get_switch(ATKBD_DRIVER_NAME); 7342421Syokota if (sw == NULL) 7442421Syokota return ENXIO; 7542421Syokota 7658271Syokota args[0] = ctlr; 7742421Syokota args[1] = irq; 7844628Syokota error = (*sw->probe)(unit, args, flags); 7944628Syokota if (error) 8044628Syokota return error; 8144628Syokota return 0; 8242421Syokota} 8342421Syokota 8442421Syokotaint 8558271Syokotaatkbd_attach_unit(int unit, keyboard_t **kbd, int ctlr, int irq, int flags) 8642421Syokota{ 8742421Syokota keyboard_switch_t *sw; 8844628Syokota int args[2]; 8942421Syokota int error; 9042421Syokota 9142421Syokota sw = kbd_get_switch(ATKBD_DRIVER_NAME); 9242421Syokota if (sw == NULL) 9342421Syokota return ENXIO; 9442421Syokota 9542421Syokota /* reset, initialize and enable the device */ 9658271Syokota args[0] = ctlr; 9744628Syokota args[1] = irq; 9850154Syokota *kbd = NULL; 9944628Syokota error = (*sw->probe)(unit, args, flags); 10042421Syokota if (error) 10144628Syokota return error; 10250154Syokota error = (*sw->init)(unit, kbd, args, flags); 10344628Syokota if (error) 10444628Syokota return error; 10550154Syokota (*sw->enable)(*kbd); 10642421Syokota 10742421Syokota#ifdef KBD_INSTALL_CDEV 10842421Syokota /* attach a virtual keyboard cdev */ 10950154Syokota error = kbd_attach(*kbd); 11042421Syokota if (error) 11142421Syokota return error; 11242421Syokota#endif 11342421Syokota 11442421Syokota /* 11542421Syokota * This is a kludge to compensate for lost keyboard interrupts. 11642421Syokota * A similar code used to be in syscons. See below. XXX 11742421Syokota */ 11850154Syokota atkbd_timeout(*kbd); 11942421Syokota 12042421Syokota if (bootverbose) 12150154Syokota (*sw->diag)(*kbd, bootverbose); 12242421Syokota return 0; 12342421Syokota} 12442421Syokota 12542421Syokotastatic void 12642421Syokotaatkbd_timeout(void *arg) 12742421Syokota{ 12842421Syokota keyboard_t *kbd; 12942421Syokota int s; 13042421Syokota 13156334Syokota /* 13256334Syokota * The original text of the following comments are extracted 13356334Syokota * from syscons.c (1.287) 13456334Syokota * 13542421Syokota * With release 2.1 of the Xaccel server, the keyboard is left 13642421Syokota * hanging pretty often. Apparently an interrupt from the 13742421Syokota * keyboard is lost, and I don't know why (yet). 13856334Syokota * This ugly hack calls the low-level interrupt routine if input 13956334Syokota * is ready for the keyboard and conveniently hides the problem. XXX 14056334Syokota * 14156334Syokota * Try removing anything stuck in the keyboard controller; whether 14256334Syokota * it's a keyboard scan code or mouse data. The low-level 14356334Syokota * interrupt routine doesn't read the mouse data directly, 14456334Syokota * but the keyboard controller driver will, as a side effect. 14542421Syokota */ 14642421Syokota /* 14756334Syokota * And here is bde's original comment about this: 14856334Syokota * 14956334Syokota * This is necessary to handle edge triggered interrupts - if we 15056334Syokota * returned when our IRQ is high due to unserviced input, then there 15156334Syokota * would be no more keyboard IRQs until the keyboard is reset by 15256334Syokota * external powers. 15356334Syokota * 15456334Syokota * The keyboard apparently unwedges the irq in most cases. 15542421Syokota */ 15642421Syokota s = spltty(); 15742421Syokota kbd = (keyboard_t *)arg; 15842421Syokota if ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) { 15942421Syokota /* 16042421Syokota * We have seen the lock flag is not set. Let's reset 16142421Syokota * the flag early, otherwise the LED update routine fails 16242421Syokota * which may want the lock during the interrupt routine. 16342421Syokota */ 16442421Syokota (*kbdsw[kbd->kb_index]->lock)(kbd, FALSE); 16542421Syokota if ((*kbdsw[kbd->kb_index]->check_char)(kbd)) 16644628Syokota (*kbdsw[kbd->kb_index]->intr)(kbd, NULL); 16742421Syokota } 16842421Syokota splx(s); 16942421Syokota timeout(atkbd_timeout, arg, hz/10); 17042421Syokota} 17142421Syokota 17242421Syokota/* LOW-LEVEL */ 17342421Syokota 17442421Syokota#define ATKBD_DEFAULT 0 17542421Syokota 17642421Syokotatypedef struct atkbd_state { 17742421Syokota KBDC kbdc; /* keyboard controller */ 17842421Syokota /* XXX: don't move this field; pcvt 17942421Syokota * expects `kbdc' to be the first 18042421Syokota * field in this structure. */ 18142421Syokota int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 18242421Syokota int ks_flags; /* flags */ 18342421Syokota#define COMPOSE (1 << 0) 18444628Syokota int ks_polling; 18542421Syokota int ks_state; /* shift/lock key state */ 18642421Syokota int ks_accents; /* accent key index (> 0) */ 18742421Syokota u_int ks_composed_char; /* composed char code (> 0) */ 18842421Syokota u_char ks_prefix; /* AT scan code prefix */ 18942421Syokota} atkbd_state_t; 19042421Syokota 19142421Syokota/* keyboard driver declaration */ 19242421Syokotastatic int atkbd_configure(int flags); 19342421Syokotastatic kbd_probe_t atkbd_probe; 19442421Syokotastatic kbd_init_t atkbd_init; 19542421Syokotastatic kbd_term_t atkbd_term; 19642421Syokotastatic kbd_intr_t atkbd_intr; 19742421Syokotastatic kbd_test_if_t atkbd_test_if; 19842421Syokotastatic kbd_enable_t atkbd_enable; 19942421Syokotastatic kbd_disable_t atkbd_disable; 20042421Syokotastatic kbd_read_t atkbd_read; 20142421Syokotastatic kbd_check_t atkbd_check; 20242421Syokotastatic kbd_read_char_t atkbd_read_char; 20342421Syokotastatic kbd_check_char_t atkbd_check_char; 20442421Syokotastatic kbd_ioctl_t atkbd_ioctl; 20542421Syokotastatic kbd_lock_t atkbd_lock; 20642421Syokotastatic kbd_clear_state_t atkbd_clear_state; 20742421Syokotastatic kbd_get_state_t atkbd_get_state; 20842421Syokotastatic kbd_set_state_t atkbd_set_state; 20944628Syokotastatic kbd_poll_mode_t atkbd_poll; 21042421Syokota 211114293Smarkmstatic keyboard_switch_t atkbdsw = { 21242421Syokota atkbd_probe, 21342421Syokota atkbd_init, 21442421Syokota atkbd_term, 21542421Syokota atkbd_intr, 21642421Syokota atkbd_test_if, 21742421Syokota atkbd_enable, 21842421Syokota atkbd_disable, 21942421Syokota atkbd_read, 22042421Syokota atkbd_check, 22142421Syokota atkbd_read_char, 22242421Syokota atkbd_check_char, 22342421Syokota atkbd_ioctl, 22442421Syokota atkbd_lock, 22542421Syokota atkbd_clear_state, 22642421Syokota atkbd_get_state, 22742421Syokota atkbd_set_state, 22842421Syokota genkbd_get_fkeystr, 22944628Syokota atkbd_poll, 23042421Syokota genkbd_diag, 23142421Syokota}; 23242421Syokota 23342421SyokotaKEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure); 23442421Syokota 23542421Syokota/* local functions */ 23655731Syokotastatic int get_typematic(keyboard_t *kbd); 23742421Syokotastatic int setup_kbd_port(KBDC kbdc, int port, int intr); 23842421Syokotastatic int get_kbd_echo(KBDC kbdc); 23942421Syokotastatic int probe_keyboard(KBDC kbdc, int flags); 24042421Syokotastatic int init_keyboard(KBDC kbdc, int *type, int flags); 24142421Syokotastatic int write_kbd(KBDC kbdc, int command, int data); 24242421Syokotastatic int get_kbd_id(KBDC kbdc); 24344628Syokotastatic int typematic(int delay, int rate); 24454543Syokotastatic int typematic_delay(int delay); 24554543Syokotastatic int typematic_rate(int rate); 24642421Syokota 24742421Syokota/* local variables */ 24842421Syokota 24942421Syokota/* the initial key map, accent map and fkey strings */ 25044628Syokota#ifdef ATKBD_DFLT_KEYMAP 25144628Syokota#define KBD_DFLT_KEYMAP 25244628Syokota#include "atkbdmap.h" 25344628Syokota#endif 25442831Syokota#include <dev/kbd/kbdtables.h> 25542421Syokota 25642421Syokota/* structures for the default keyboard */ 25742421Syokotastatic keyboard_t default_kbd; 25842421Syokotastatic atkbd_state_t default_kbd_state; 25942421Syokotastatic keymap_t default_keymap; 26042421Syokotastatic accentmap_t default_accentmap; 26142421Syokotastatic fkeytab_t default_fkeytab[NUM_FKEYS]; 26242421Syokota 26342421Syokota/* 26442421Syokota * The back door to the keyboard driver! 26542421Syokota * This function is called by the console driver, via the kbdio module, 26642421Syokota * to tickle keyboard drivers when the low-level console is being initialized. 26742421Syokota * Almost nothing in the kernel has been initialied yet. Try to probe 26842421Syokota * keyboards if possible. 26994228Sasmodai * NOTE: because of the way the low-level console is initialized, this routine 27042421Syokota * may be called more than once!! 27142421Syokota */ 27242421Syokotastatic int 27342421Syokotaatkbd_configure(int flags) 27442421Syokota{ 27542421Syokota keyboard_t *kbd; 27642421Syokota int arg[2]; 27744628Syokota int i; 27842421Syokota 27948878Syokota /* probe the keyboard controller */ 28048878Syokota atkbdc_configure(); 28148878Syokota 28246764Syokota /* if the driver is disabled, unregister the keyboard if any */ 283117167Sjhb if (resource_disabled("atkbd", ATKBD_DEFAULT)) { 28446764Syokota i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT); 28546764Syokota if (i >= 0) { 28646764Syokota kbd = kbd_get_keyboard(i); 28746764Syokota kbd_unregister(kbd); 28846764Syokota kbd->kb_flags &= ~KB_REGISTERED; 28944628Syokota } 29048878Syokota return 0; 29144628Syokota } 29245720Speter 29346764Syokota /* XXX: a kludge to obtain the device configuration flags */ 29446764Syokota if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0) 29546764Syokota flags |= i; 29646764Syokota 29742421Syokota /* probe the default keyboard */ 29842421Syokota arg[0] = -1; 29942421Syokota arg[1] = -1; 30044628Syokota kbd = NULL; 30144628Syokota if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) 30242421Syokota return 0; 30344628Syokota if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) 30444628Syokota return 0; 30542421Syokota 30644628Syokota /* return the number of found keyboards */ 30744628Syokota return 1; 30844628Syokota} 30944628Syokota 31044628Syokota/* low-level functions */ 31144628Syokota 31244628Syokota/* detect a keyboard */ 31344628Syokotastatic int 31444628Syokotaatkbd_probe(int unit, void *arg, int flags) 31544628Syokota{ 31644628Syokota KBDC kbdc; 31758271Syokota int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ 31844628Syokota 31944628Syokota /* XXX */ 32044628Syokota if (unit == ATKBD_DEFAULT) { 32144628Syokota if (KBD_IS_PROBED(&default_kbd)) 32242421Syokota return 0; 32342421Syokota } 32442421Syokota 32558271Syokota kbdc = atkbdc_open(data[0]); 32644628Syokota if (kbdc == NULL) 32744628Syokota return ENXIO; 32844628Syokota if (probe_keyboard(kbdc, flags)) { 32944628Syokota if (flags & KB_CONF_FAIL_IF_NO_KBD) 33044628Syokota return ENXIO; 33142421Syokota } 33244628Syokota return 0; 33342421Syokota} 33442421Syokota 33544628Syokota/* reset and initialize the device */ 33642421Syokotastatic int 33744628Syokotaatkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 33842421Syokota{ 33942421Syokota keyboard_t *kbd; 34042421Syokota atkbd_state_t *state; 34142421Syokota keymap_t *keymap; 34242421Syokota accentmap_t *accmap; 34342421Syokota fkeytab_t *fkeymap; 34442421Syokota int fkeymap_size; 34555731Syokota int delay[2]; 34658271Syokota int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ 34742421Syokota 34842421Syokota /* XXX */ 34942421Syokota if (unit == ATKBD_DEFAULT) { 35042421Syokota *kbdp = kbd = &default_kbd; 35144628Syokota if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) 35242421Syokota return 0; 35342421Syokota state = &default_kbd_state; 35442421Syokota keymap = &default_keymap; 35542421Syokota accmap = &default_accentmap; 35642421Syokota fkeymap = default_fkeytab; 35742421Syokota fkeymap_size = 35842421Syokota sizeof(default_fkeytab)/sizeof(default_fkeytab[0]); 35942421Syokota } else if (*kbdp == NULL) { 36069781Sdwmalone *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO); 36169781Sdwmalone state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO); 36242421Syokota keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT); 36342421Syokota accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT); 36442421Syokota fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT); 36542421Syokota fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]); 36669781Sdwmalone if ((kbd == NULL) || (state == NULL) || (keymap == NULL) 36769781Sdwmalone || (accmap == NULL) || (fkeymap == NULL)) { 36842421Syokota if (state != NULL) 36942421Syokota free(state, M_DEVBUF); 37042421Syokota if (keymap != NULL) 37142421Syokota free(keymap, M_DEVBUF); 37242421Syokota if (accmap != NULL) 37342421Syokota free(accmap, M_DEVBUF); 37442421Syokota if (fkeymap != NULL) 37542421Syokota free(fkeymap, M_DEVBUF); 37671394Sdwmalone if (kbd != NULL) 37769781Sdwmalone free(kbd, M_DEVBUF); 37842421Syokota return ENOMEM; 37942421Syokota } 38044628Syokota } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { 38142421Syokota return 0; 38242421Syokota } else { 38342421Syokota kbd = *kbdp; 38442421Syokota state = (atkbd_state_t *)kbd->kb_data; 38542421Syokota bzero(state, sizeof(*state)); 38642421Syokota keymap = kbd->kb_keymap; 38742421Syokota accmap = kbd->kb_accentmap; 38842421Syokota fkeymap = kbd->kb_fkeytab; 38942421Syokota fkeymap_size = kbd->kb_fkeytab_size; 39042421Syokota } 39142421Syokota 39244628Syokota if (!KBD_IS_PROBED(kbd)) { 39358271Syokota state->kbdc = atkbdc_open(data[0]); 39444628Syokota if (state->kbdc == NULL) 39542421Syokota return ENXIO; 39644628Syokota kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags, 39758271Syokota 0, 0); 39844628Syokota bcopy(&key_map, keymap, sizeof(key_map)); 39944628Syokota bcopy(&accent_map, accmap, sizeof(accent_map)); 40044628Syokota bcopy(fkey_tab, fkeymap, 40144628Syokota imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab))); 40244628Syokota kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); 40344628Syokota kbd->kb_data = (void *)state; 40444628Syokota 40544628Syokota if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */ 40644628Syokota if (flags & KB_CONF_FAIL_IF_NO_KBD) 40744628Syokota return ENXIO; 40844628Syokota } else { 40944628Syokota KBD_FOUND_DEVICE(kbd); 41044628Syokota } 41144628Syokota atkbd_clear_state(kbd); 41244628Syokota state->ks_mode = K_XLATE; 41344628Syokota /* 41444628Syokota * FIXME: set the initial value for lock keys in ks_state 41544628Syokota * according to the BIOS data? 41644628Syokota */ 41744628Syokota KBD_PROBE_DONE(kbd); 41842421Syokota } 41944628Syokota if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { 42049820Syokota kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; 42142421Syokota if (KBD_HAS_DEVICE(kbd) 42244628Syokota && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config) 423117513Ssimokawa && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) { 424117513Ssimokawa kbd_unregister(kbd); 42542421Syokota return ENXIO; 426117513Ssimokawa } 42744628Syokota atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 42855731Syokota get_typematic(kbd); 42955731Syokota delay[0] = kbd->kb_delay1; 43055731Syokota delay[1] = kbd->kb_delay2; 43155731Syokota atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 43242421Syokota KBD_INIT_DONE(kbd); 43342421Syokota } 43442421Syokota if (!KBD_IS_CONFIGURED(kbd)) { 43542421Syokota if (kbd_register(kbd) < 0) 43642421Syokota return ENXIO; 43742421Syokota KBD_CONFIG_DONE(kbd); 43842421Syokota } 43942421Syokota 44042421Syokota return 0; 44142421Syokota} 44242421Syokota 44342421Syokota/* finish using this keyboard */ 44442421Syokotastatic int 44542421Syokotaatkbd_term(keyboard_t *kbd) 44642421Syokota{ 44742421Syokota kbd_unregister(kbd); 44842421Syokota return 0; 44942421Syokota} 45042421Syokota 45142421Syokota/* keyboard interrupt routine */ 45242421Syokotastatic int 45344628Syokotaatkbd_intr(keyboard_t *kbd, void *arg) 45442421Syokota{ 45542421Syokota atkbd_state_t *state; 45655731Syokota int delay[2]; 45742421Syokota int c; 45842421Syokota 45942421Syokota if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { 46042421Syokota /* let the callback function to process the input */ 46142421Syokota (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, 46242421Syokota kbd->kb_callback.kc_arg); 46342421Syokota } else { 46442421Syokota /* read and discard the input; no one is waiting for input */ 46542421Syokota do { 46642421Syokota c = atkbd_read_char(kbd, FALSE); 46742421Syokota } while (c != NOKEY); 46842421Syokota 46942421Syokota if (!KBD_HAS_DEVICE(kbd)) { 47042421Syokota /* 47142421Syokota * The keyboard was not detected before; 47242421Syokota * it must have been reconnected! 47342421Syokota */ 47442421Syokota state = (atkbd_state_t *)kbd->kb_data; 47542421Syokota init_keyboard(state->kbdc, &kbd->kb_type, 47642421Syokota kbd->kb_config); 47742421Syokota atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 47855731Syokota get_typematic(kbd); 47955731Syokota delay[0] = kbd->kb_delay1; 48055731Syokota delay[1] = kbd->kb_delay2; 48155731Syokota atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 48242421Syokota KBD_FOUND_DEVICE(kbd); 48342421Syokota } 48442421Syokota } 48542421Syokota return 0; 48642421Syokota} 48742421Syokota 48842421Syokota/* test the interface to the device */ 48942421Syokotastatic int 49042421Syokotaatkbd_test_if(keyboard_t *kbd) 49142421Syokota{ 49242421Syokota int error; 49342421Syokota int s; 49442421Syokota 49542421Syokota error = 0; 49642421Syokota empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10); 49742421Syokota s = spltty(); 49842421Syokota if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc)) 49942421Syokota error = EIO; 50042421Syokota else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0) 50142421Syokota error = EIO; 50242421Syokota splx(s); 50342421Syokota 50442421Syokota return error; 50542421Syokota} 50642421Syokota 50742421Syokota/* 50842421Syokota * Enable the access to the device; until this function is called, 50942421Syokota * the client cannot read from the keyboard. 51042421Syokota */ 51142421Syokotastatic int 51242421Syokotaatkbd_enable(keyboard_t *kbd) 51342421Syokota{ 51442421Syokota int s; 51542421Syokota 51642421Syokota s = spltty(); 51742421Syokota KBD_ACTIVATE(kbd); 51842421Syokota splx(s); 51942421Syokota return 0; 52042421Syokota} 52142421Syokota 52242421Syokota/* disallow the access to the device */ 52342421Syokotastatic int 52442421Syokotaatkbd_disable(keyboard_t *kbd) 52542421Syokota{ 52642421Syokota int s; 52742421Syokota 52842421Syokota s = spltty(); 52942421Syokota KBD_DEACTIVATE(kbd); 53042421Syokota splx(s); 53142421Syokota return 0; 53242421Syokota} 53342421Syokota 53442421Syokota/* read one byte from the keyboard if it's allowed */ 53542421Syokotastatic int 53642421Syokotaatkbd_read(keyboard_t *kbd, int wait) 53742421Syokota{ 53842421Syokota int c; 53942421Syokota 54042421Syokota if (wait) 54142421Syokota c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc); 54242421Syokota else 54342421Syokota c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc); 54454382Syokota if (c != -1) 54554382Syokota ++kbd->kb_count; 54642421Syokota return (KBD_IS_ACTIVE(kbd) ? c : -1); 54742421Syokota} 54842421Syokota 54942421Syokota/* check if data is waiting */ 55042421Syokotastatic int 55142421Syokotaatkbd_check(keyboard_t *kbd) 55242421Syokota{ 55342421Syokota if (!KBD_IS_ACTIVE(kbd)) 55442421Syokota return FALSE; 55542421Syokota return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc); 55642421Syokota} 55742421Syokota 55842421Syokota/* read char from the keyboard */ 55942421Syokotastatic u_int 56042421Syokotaatkbd_read_char(keyboard_t *kbd, int wait) 56142421Syokota{ 56242421Syokota atkbd_state_t *state; 56342421Syokota u_int action; 56442421Syokota int scancode; 56542421Syokota int keycode; 56642421Syokota 56742421Syokota state = (atkbd_state_t *)kbd->kb_data; 56842421Syokotanext_code: 56942421Syokota /* do we have a composed char to return? */ 57042421Syokota if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { 57142421Syokota action = state->ks_composed_char; 57242421Syokota state->ks_composed_char = 0; 57342421Syokota if (action > UCHAR_MAX) 57442421Syokota return ERRKEY; 57542421Syokota return action; 57642421Syokota } 57742421Syokota 57842421Syokota /* see if there is something in the keyboard port */ 57942421Syokota if (wait) { 58042421Syokota do { 58142421Syokota scancode = read_kbd_data(state->kbdc); 58242421Syokota } while (scancode == -1); 58342421Syokota } else { 58442421Syokota scancode = read_kbd_data_no_wait(state->kbdc); 58542421Syokota if (scancode == -1) 58642421Syokota return NOKEY; 58742421Syokota } 58854382Syokota ++kbd->kb_count; 58942421Syokota 59054382Syokota#if KBDIO_DEBUG >= 10 59154382Syokota printf("atkbd_read_char(): scancode:0x%x\n", scancode); 59254382Syokota#endif 59354382Syokota 59442421Syokota /* return the byte as is for the K_RAW mode */ 59542421Syokota if (state->ks_mode == K_RAW) 59642421Syokota return scancode; 59742421Syokota 59842421Syokota /* translate the scan code into a keycode */ 59942421Syokota keycode = scancode & 0x7F; 60042421Syokota switch (state->ks_prefix) { 60142421Syokota case 0x00: /* normal scancode */ 60242421Syokota switch(scancode) { 60342421Syokota case 0xB8: /* left alt (compose key) released */ 60442421Syokota if (state->ks_flags & COMPOSE) { 60542421Syokota state->ks_flags &= ~COMPOSE; 60642421Syokota if (state->ks_composed_char > UCHAR_MAX) 60742421Syokota state->ks_composed_char = 0; 60842421Syokota } 60942421Syokota break; 61042421Syokota case 0x38: /* left alt (compose key) pressed */ 61142421Syokota if (!(state->ks_flags & COMPOSE)) { 61242421Syokota state->ks_flags |= COMPOSE; 61342421Syokota state->ks_composed_char = 0; 61442421Syokota } 61542421Syokota break; 61642421Syokota case 0xE0: 61742421Syokota case 0xE1: 61842421Syokota state->ks_prefix = scancode; 61942421Syokota goto next_code; 62042421Syokota } 62142421Syokota break; 62242421Syokota case 0xE0: /* 0xE0 prefix */ 62342421Syokota state->ks_prefix = 0; 62442421Syokota switch (keycode) { 62542421Syokota case 0x1C: /* right enter key */ 62642421Syokota keycode = 0x59; 62742421Syokota break; 62842421Syokota case 0x1D: /* right ctrl key */ 62942421Syokota keycode = 0x5A; 63042421Syokota break; 63142421Syokota case 0x35: /* keypad divide key */ 63242421Syokota keycode = 0x5B; 63342421Syokota break; 63442421Syokota case 0x37: /* print scrn key */ 63542421Syokota keycode = 0x5C; 63642421Syokota break; 63742421Syokota case 0x38: /* right alt key (alt gr) */ 63842421Syokota keycode = 0x5D; 63942421Syokota break; 64043337Syokota case 0x46: /* ctrl-pause/break on AT 101 (see below) */ 64143337Syokota keycode = 0x68; 64243337Syokota break; 64342421Syokota case 0x47: /* grey home key */ 64442421Syokota keycode = 0x5E; 64542421Syokota break; 64642421Syokota case 0x48: /* grey up arrow key */ 64742421Syokota keycode = 0x5F; 64842421Syokota break; 64942421Syokota case 0x49: /* grey page up key */ 65042421Syokota keycode = 0x60; 65142421Syokota break; 65242421Syokota case 0x4B: /* grey left arrow key */ 65342421Syokota keycode = 0x61; 65442421Syokota break; 65542421Syokota case 0x4D: /* grey right arrow key */ 65642421Syokota keycode = 0x62; 65742421Syokota break; 65842421Syokota case 0x4F: /* grey end key */ 65942421Syokota keycode = 0x63; 66042421Syokota break; 66142421Syokota case 0x50: /* grey down arrow key */ 66242421Syokota keycode = 0x64; 66342421Syokota break; 66442421Syokota case 0x51: /* grey page down key */ 66542421Syokota keycode = 0x65; 66642421Syokota break; 66742421Syokota case 0x52: /* grey insert key */ 66842421Syokota keycode = 0x66; 66942421Syokota break; 67042421Syokota case 0x53: /* grey delete key */ 67142421Syokota keycode = 0x67; 67242421Syokota break; 67342421Syokota /* the following 3 are only used on the MS "Natural" keyboard */ 67442421Syokota case 0x5b: /* left Window key */ 67542421Syokota keycode = 0x69; 67642421Syokota break; 67742421Syokota case 0x5c: /* right Window key */ 67842421Syokota keycode = 0x6a; 67942421Syokota break; 68042421Syokota case 0x5d: /* menu key */ 68142421Syokota keycode = 0x6b; 68242421Syokota break; 683120875Sfjoe case 0x5e: /* power key */ 684120875Sfjoe keycode = 0x6d; 685120875Sfjoe break; 686120875Sfjoe case 0x5f: /* sleep key */ 687120875Sfjoe keycode = 0x6e; 688120875Sfjoe break; 689120875Sfjoe case 0x63: /* wake key */ 690120875Sfjoe keycode = 0x6f; 691120875Sfjoe break; 69242421Syokota default: /* ignore everything else */ 69342421Syokota goto next_code; 69442421Syokota } 69542421Syokota break; 69642421Syokota case 0xE1: /* 0xE1 prefix */ 69743337Syokota /* 69843337Syokota * The pause/break key on the 101 keyboard produces: 69943337Syokota * E1-1D-45 E1-9D-C5 70043337Syokota * Ctrl-pause/break produces: 70143337Syokota * E0-46 E0-C6 (See above.) 70243337Syokota */ 70342421Syokota state->ks_prefix = 0; 70442421Syokota if (keycode == 0x1D) 70542421Syokota state->ks_prefix = 0x1D; 70642421Syokota goto next_code; 70742421Syokota /* NOT REACHED */ 70842421Syokota case 0x1D: /* pause / break */ 70942421Syokota state->ks_prefix = 0; 71042421Syokota if (keycode != 0x45) 71143337Syokota goto next_code; 71242421Syokota keycode = 0x68; 71342421Syokota break; 71442421Syokota } 71542421Syokota 71643337Syokota if (kbd->kb_type == KB_84) { 71743337Syokota switch (keycode) { 71843337Syokota case 0x37: /* *(numpad)/print screen */ 71943337Syokota if (state->ks_flags & SHIFTS) 72043337Syokota keycode = 0x5c; /* print screen */ 72143337Syokota break; 72243337Syokota case 0x45: /* num lock/pause */ 72343337Syokota if (state->ks_flags & CTLS) 72443337Syokota keycode = 0x68; /* pause */ 72543337Syokota break; 72643337Syokota case 0x46: /* scroll lock/break */ 72743337Syokota if (state->ks_flags & CTLS) 72843337Syokota keycode = 0x6c; /* break */ 72943337Syokota break; 73043337Syokota } 73143337Syokota } else if (kbd->kb_type == KB_101) { 73243337Syokota switch (keycode) { 73343337Syokota case 0x5c: /* print screen */ 73443337Syokota if (state->ks_flags & ALTS) 73543337Syokota keycode = 0x54; /* sysrq */ 73643337Syokota break; 73743337Syokota case 0x68: /* pause/break */ 73843337Syokota if (state->ks_flags & CTLS) 73943337Syokota keycode = 0x6c; /* break */ 74043337Syokota break; 74143337Syokota } 74243337Syokota } 74343337Syokota 74442421Syokota /* return the key code in the K_CODE mode */ 74542421Syokota if (state->ks_mode == K_CODE) 74642421Syokota return (keycode | (scancode & 0x80)); 74742421Syokota 74842421Syokota /* compose a character code */ 74942421Syokota if (state->ks_flags & COMPOSE) { 75047293Syokota switch (keycode | (scancode & 0x80)) { 75142421Syokota /* key pressed, process it */ 75242421Syokota case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ 75342421Syokota state->ks_composed_char *= 10; 75446765Syokota state->ks_composed_char += keycode - 0x40; 75542421Syokota if (state->ks_composed_char > UCHAR_MAX) 75642421Syokota return ERRKEY; 75742421Syokota goto next_code; 75842421Syokota case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ 75942421Syokota state->ks_composed_char *= 10; 76046765Syokota state->ks_composed_char += keycode - 0x47; 76142421Syokota if (state->ks_composed_char > UCHAR_MAX) 76242421Syokota return ERRKEY; 76342421Syokota goto next_code; 76442421Syokota case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ 76542421Syokota state->ks_composed_char *= 10; 76646765Syokota state->ks_composed_char += keycode - 0x4E; 76742421Syokota if (state->ks_composed_char > UCHAR_MAX) 76842421Syokota return ERRKEY; 76942421Syokota goto next_code; 77042421Syokota case 0x52: /* keypad 0 */ 77142421Syokota state->ks_composed_char *= 10; 77242421Syokota if (state->ks_composed_char > UCHAR_MAX) 77342421Syokota return ERRKEY; 77442421Syokota goto next_code; 77542421Syokota 77642421Syokota /* key released, no interest here */ 77742421Syokota case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ 77842421Syokota case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ 77942421Syokota case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ 78042421Syokota case 0xD2: /* keypad 0 */ 78142421Syokota goto next_code; 78242421Syokota 78342421Syokota case 0x38: /* left alt key */ 78442421Syokota break; 78542421Syokota 78642421Syokota default: 78742421Syokota if (state->ks_composed_char > 0) { 78842421Syokota state->ks_flags &= ~COMPOSE; 78942421Syokota state->ks_composed_char = 0; 79042421Syokota return ERRKEY; 79142421Syokota } 79242421Syokota break; 79342421Syokota } 79442421Syokota } 79542421Syokota 79642421Syokota /* keycode to key action */ 79742421Syokota action = genkbd_keyaction(kbd, keycode, scancode & 0x80, 79842421Syokota &state->ks_state, &state->ks_accents); 79942421Syokota if (action == NOKEY) 80042421Syokota goto next_code; 80142421Syokota else 80242421Syokota return action; 80342421Syokota} 80442421Syokota 80542421Syokota/* check if char is waiting */ 80642421Syokotastatic int 80742421Syokotaatkbd_check_char(keyboard_t *kbd) 80842421Syokota{ 80942421Syokota atkbd_state_t *state; 81042421Syokota 81142421Syokota if (!KBD_IS_ACTIVE(kbd)) 81242421Syokota return FALSE; 81342421Syokota state = (atkbd_state_t *)kbd->kb_data; 81442421Syokota if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) 81542421Syokota return TRUE; 81642421Syokota return kbdc_data_ready(state->kbdc); 81742421Syokota} 81842421Syokota 81942421Syokota/* some useful control functions */ 82042421Syokotastatic int 82142421Syokotaatkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 82242421Syokota{ 82342421Syokota /* trasnlate LED_XXX bits into the device specific bits */ 82442421Syokota static u_char ledmap[8] = { 82542421Syokota 0, 4, 2, 6, 1, 5, 3, 7, 82642421Syokota }; 82742421Syokota atkbd_state_t *state = kbd->kb_data; 82842421Syokota int error; 82942421Syokota int s; 83042421Syokota int i; 83142421Syokota 83242421Syokota s = spltty(); 83342421Syokota switch (cmd) { 83442421Syokota 83542421Syokota case KDGKBMODE: /* get keyboard mode */ 83642421Syokota *(int *)arg = state->ks_mode; 83742421Syokota break; 83842421Syokota case KDSKBMODE: /* set keyboard mode */ 83942421Syokota switch (*(int *)arg) { 84042421Syokota case K_XLATE: 84142421Syokota if (state->ks_mode != K_XLATE) { 84242421Syokota /* make lock key state and LED state match */ 84342421Syokota state->ks_state &= ~LOCK_MASK; 84442421Syokota state->ks_state |= KBD_LED_VAL(kbd); 84542421Syokota } 846102412Scharnier /* FALLTHROUGH */ 84742421Syokota case K_RAW: 84842421Syokota case K_CODE: 84942421Syokota if (state->ks_mode != *(int *)arg) { 85042421Syokota atkbd_clear_state(kbd); 85142421Syokota state->ks_mode = *(int *)arg; 85242421Syokota } 85342421Syokota break; 85442421Syokota default: 85542421Syokota splx(s); 85642421Syokota return EINVAL; 85742421Syokota } 85842421Syokota break; 85942421Syokota 86042421Syokota case KDGETLED: /* get keyboard LED */ 86142421Syokota *(int *)arg = KBD_LED_VAL(kbd); 86242421Syokota break; 86342421Syokota case KDSETLED: /* set keyboard LED */ 86442421Syokota /* NOTE: lock key state in ks_state won't be changed */ 86542421Syokota if (*(int *)arg & ~LOCK_MASK) { 86642421Syokota splx(s); 86742421Syokota return EINVAL; 86842421Syokota } 86942421Syokota i = *(int *)arg; 87042421Syokota /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 87161004Sache if (state->ks_mode == K_XLATE && 87261004Sache kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 87342421Syokota if (i & ALKED) 87442421Syokota i |= CLKED; 87542421Syokota else 87642421Syokota i &= ~CLKED; 87742421Syokota } 87842421Syokota if (KBD_HAS_DEVICE(kbd)) { 87942421Syokota error = write_kbd(state->kbdc, KBDC_SET_LEDS, 88042421Syokota ledmap[i & LED_MASK]); 88142421Syokota if (error) { 88242421Syokota splx(s); 88342421Syokota return error; 88442421Syokota } 88542421Syokota } 88642421Syokota KBD_LED_VAL(kbd) = *(int *)arg; 88742421Syokota break; 88842421Syokota 88942421Syokota case KDGKBSTATE: /* get lock key state */ 89042421Syokota *(int *)arg = state->ks_state & LOCK_MASK; 89142421Syokota break; 89242421Syokota case KDSKBSTATE: /* set lock key state */ 89342421Syokota if (*(int *)arg & ~LOCK_MASK) { 89442421Syokota splx(s); 89542421Syokota return EINVAL; 89642421Syokota } 89742421Syokota state->ks_state &= ~LOCK_MASK; 89842421Syokota state->ks_state |= *(int *)arg; 89942421Syokota splx(s); 90042421Syokota /* set LEDs and quit */ 90142421Syokota return atkbd_ioctl(kbd, KDSETLED, arg); 90242421Syokota 90344628Syokota case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ 90442421Syokota splx(s); 90542421Syokota if (!KBD_HAS_DEVICE(kbd)) 90642421Syokota return 0; 90744628Syokota i = typematic(((int *)arg)[0], ((int *)arg)[1]); 90854543Syokota error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i); 90954543Syokota if (error == 0) { 91054543Syokota kbd->kb_delay1 = typematic_delay(i); 91154543Syokota kbd->kb_delay2 = typematic_rate(i); 91254543Syokota } 91354543Syokota return error; 91442421Syokota 91544628Syokota case KDSETRAD: /* set keyboard repeat rate (old interface) */ 91644628Syokota splx(s); 91744628Syokota if (!KBD_HAS_DEVICE(kbd)) 91844628Syokota return 0; 91954543Syokota error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg); 92054543Syokota if (error == 0) { 92154543Syokota kbd->kb_delay1 = typematic_delay(*(int *)arg); 92254543Syokota kbd->kb_delay2 = typematic_rate(*(int *)arg); 92354543Syokota } 92454543Syokota return error; 92544628Syokota 92642421Syokota case PIO_KEYMAP: /* set keyboard translation table */ 92742421Syokota case PIO_KEYMAPENT: /* set keyboard translation table entry */ 92842421Syokota case PIO_DEADKEYMAP: /* set accent key translation table */ 92942421Syokota state->ks_accents = 0; 930102412Scharnier /* FALLTHROUGH */ 93142421Syokota default: 93242421Syokota splx(s); 93342421Syokota return genkbd_commonioctl(kbd, cmd, arg); 93442421Syokota } 93542421Syokota 93642421Syokota splx(s); 93742421Syokota return 0; 93842421Syokota} 93942421Syokota 94042421Syokota/* lock the access to the keyboard */ 94142421Syokotastatic int 94242421Syokotaatkbd_lock(keyboard_t *kbd, int lock) 94342421Syokota{ 94442421Syokota return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock); 94542421Syokota} 94642421Syokota 94742421Syokota/* clear the internal state of the keyboard */ 94842421Syokotastatic void 94942421Syokotaatkbd_clear_state(keyboard_t *kbd) 95042421Syokota{ 95142421Syokota atkbd_state_t *state; 95242421Syokota 95342421Syokota state = (atkbd_state_t *)kbd->kb_data; 95442421Syokota state->ks_flags = 0; 95544628Syokota state->ks_polling = 0; 95642421Syokota state->ks_state &= LOCK_MASK; /* preserve locking key state */ 95742421Syokota state->ks_accents = 0; 95842421Syokota state->ks_composed_char = 0; 95942421Syokota#if 0 96042421Syokota state->ks_prefix = 0; /* XXX */ 96142421Syokota#endif 96242421Syokota} 96342421Syokota 96442421Syokota/* save the internal state */ 96542421Syokotastatic int 96642421Syokotaatkbd_get_state(keyboard_t *kbd, void *buf, size_t len) 96742421Syokota{ 96842421Syokota if (len == 0) 96942421Syokota return sizeof(atkbd_state_t); 97042421Syokota if (len < sizeof(atkbd_state_t)) 97142421Syokota return -1; 97242421Syokota bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t)); 97342421Syokota return 0; 97442421Syokota} 97542421Syokota 97642421Syokota/* set the internal state */ 97742421Syokotastatic int 97842421Syokotaatkbd_set_state(keyboard_t *kbd, void *buf, size_t len) 97942421Syokota{ 98042421Syokota if (len < sizeof(atkbd_state_t)) 98142421Syokota return ENOMEM; 98242421Syokota if (((atkbd_state_t *)kbd->kb_data)->kbdc 98342421Syokota != ((atkbd_state_t *)buf)->kbdc) 98442421Syokota return ENOMEM; 98542421Syokota bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t)); 98642421Syokota return 0; 98742421Syokota} 98842421Syokota 98944628Syokotastatic int 99044628Syokotaatkbd_poll(keyboard_t *kbd, int on) 99144628Syokota{ 99244628Syokota atkbd_state_t *state; 99344628Syokota int s; 99444628Syokota 99544628Syokota state = (atkbd_state_t *)kbd->kb_data; 99644628Syokota s = spltty(); 99744628Syokota if (on) 99844628Syokota ++state->ks_polling; 99944628Syokota else 100044628Syokota --state->ks_polling; 100144628Syokota splx(s); 100244628Syokota return 0; 100344628Syokota} 100444628Syokota 100542421Syokota/* local functions */ 100642421Syokota 100742421Syokotastatic int 100855731Syokotaget_typematic(keyboard_t *kbd) 100955731Syokota{ 101055731Syokota#ifdef __i386__ 101155731Syokota /* 101255731Syokota * Only some systems allow us to retrieve the keyboard repeat 101355731Syokota * rate previously set via the BIOS... 101455731Syokota */ 101555731Syokota struct vm86frame vmf; 101655731Syokota u_int32_t p; 101755731Syokota 101855731Syokota bzero(&vmf, sizeof(vmf)); 101955731Syokota vmf.vmf_ax = 0xc000; 102055731Syokota vm86_intcall(0x15, &vmf); 102155731Syokota if ((vmf.vmf_eflags & PSL_C) || vmf.vmf_ah) 102255731Syokota return ENODEV; 102355731Syokota p = BIOS_PADDRTOVADDR(((u_int32_t)vmf.vmf_es << 4) + vmf.vmf_bx); 102455731Syokota if ((readb(p + 6) & 0x40) == 0) /* int 16, function 0x09 supported? */ 102555731Syokota return ENODEV; 102655731Syokota vmf.vmf_ax = 0x0900; 102755731Syokota vm86_intcall(0x16, &vmf); 102855731Syokota if ((vmf.vmf_al & 0x08) == 0) /* int 16, function 0x0306 supported? */ 102955731Syokota return ENODEV; 103055731Syokota vmf.vmf_ax = 0x0306; 103155731Syokota vm86_intcall(0x16, &vmf); 103255731Syokota kbd->kb_delay1 = typematic_delay(vmf.vmf_bh << 5); 103355731Syokota kbd->kb_delay2 = typematic_rate(vmf.vmf_bl); 103455731Syokota return 0; 103555731Syokota#else 103655731Syokota return ENODEV; 103755731Syokota#endif /* __i386__ */ 103855731Syokota} 103955731Syokota 104055731Syokotastatic int 104142421Syokotasetup_kbd_port(KBDC kbdc, int port, int intr) 104242421Syokota{ 104342421Syokota if (!set_controller_command_byte(kbdc, 104442421Syokota KBD_KBD_CONTROL_BITS, 104542421Syokota ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT) 104642421Syokota | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT))) 104742421Syokota return 1; 104842421Syokota return 0; 104942421Syokota} 105042421Syokota 105142421Syokotastatic int 105242421Syokotaget_kbd_echo(KBDC kbdc) 105342421Syokota{ 105442421Syokota /* enable the keyboard port, but disable the keyboard intr. */ 105542421Syokota if (setup_kbd_port(kbdc, TRUE, FALSE)) 105642421Syokota /* CONTROLLER ERROR: there is very little we can do... */ 105742421Syokota return ENXIO; 105842421Syokota 105942421Syokota /* see if something is present */ 106042421Syokota write_kbd_command(kbdc, KBDC_ECHO); 106142421Syokota if (read_kbd_data(kbdc) != KBD_ECHO) { 106242421Syokota empty_both_buffers(kbdc, 10); 106342421Syokota test_controller(kbdc); 106442421Syokota test_kbd_port(kbdc); 106542421Syokota return ENXIO; 106642421Syokota } 106742421Syokota 106842421Syokota /* enable the keyboard port and intr. */ 106942421Syokota if (setup_kbd_port(kbdc, TRUE, TRUE)) { 107042421Syokota /* 107142421Syokota * CONTROLLER ERROR 107242421Syokota * This is serious; the keyboard intr is left disabled! 107342421Syokota */ 107442421Syokota return ENXIO; 107542421Syokota } 107642421Syokota 107742421Syokota return 0; 107842421Syokota} 107942421Syokota 108042421Syokotastatic int 108142421Syokotaprobe_keyboard(KBDC kbdc, int flags) 108242421Syokota{ 108342421Syokota /* 108442421Syokota * Don't try to print anything in this function. The low-level 108542421Syokota * console may not have been initialized yet... 108642421Syokota */ 108742421Syokota int err; 108842421Syokota int c; 108942421Syokota int m; 109042421Syokota 109142421Syokota if (!kbdc_lock(kbdc, TRUE)) { 109242421Syokota /* driver error? */ 109342421Syokota return ENXIO; 109442421Syokota } 109542421Syokota 109657131Syokota /* temporarily block data transmission from the keyboard */ 109757131Syokota write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); 109857131Syokota 109942421Syokota /* flush any noise in the buffer */ 110057131Syokota empty_both_buffers(kbdc, 100); 110142421Syokota 110242421Syokota /* save the current keyboard controller command byte */ 110342421Syokota m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS; 110442421Syokota c = get_controller_command_byte(kbdc); 110542421Syokota if (c == -1) { 110642421Syokota /* CONTROLLER ERROR */ 110742421Syokota kbdc_set_device_mask(kbdc, m); 110842421Syokota kbdc_lock(kbdc, FALSE); 110942421Syokota return ENXIO; 111042421Syokota } 111142421Syokota 111242421Syokota /* 111342421Syokota * The keyboard may have been screwed up by the boot block. 111442421Syokota * We may just be able to recover from error by testing the controller 111542421Syokota * and the keyboard port. The controller command byte needs to be 111642421Syokota * saved before this recovery operation, as some controllers seem 111742421Syokota * to set the command byte to particular values. 111842421Syokota */ 111942421Syokota test_controller(kbdc); 112042421Syokota test_kbd_port(kbdc); 112142421Syokota 112242421Syokota err = get_kbd_echo(kbdc); 112357902Syokota 112457902Syokota /* 112557902Syokota * Even if the keyboard doesn't seem to be present (err != 0), 112657902Syokota * we shall enable the keyboard port and interrupt so that 112757902Syokota * the driver will be operable when the keyboard is attached 112857902Syokota * to the system later. It is NOT recommended to hot-plug 112957902Syokota * the AT keyboard, but many people do so... 113057902Syokota */ 113157902Syokota kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); 113257902Syokota setup_kbd_port(kbdc, TRUE, TRUE); 113357902Syokota#if 0 113442421Syokota if (err == 0) { 113542421Syokota kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); 113642421Syokota } else { 113757131Syokota /* try to restore the command byte as before */ 113857131Syokota set_controller_command_byte(kbdc, 0xff, c); 113942421Syokota kbdc_set_device_mask(kbdc, m); 114042421Syokota } 114157902Syokota#endif 114242421Syokota 114342421Syokota kbdc_lock(kbdc, FALSE); 114442421Syokota return err; 114542421Syokota} 114642421Syokota 114742421Syokotastatic int 114842421Syokotainit_keyboard(KBDC kbdc, int *type, int flags) 114942421Syokota{ 115042421Syokota int codeset; 115142421Syokota int id; 115242421Syokota int c; 115342421Syokota 115442421Syokota if (!kbdc_lock(kbdc, TRUE)) { 115542421Syokota /* driver error? */ 115642421Syokota return EIO; 115742421Syokota } 115842421Syokota 115957131Syokota /* temporarily block data transmission from the keyboard */ 116057131Syokota write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); 116157131Syokota 116242421Syokota /* save the current controller command byte */ 116354382Syokota empty_both_buffers(kbdc, 200); 116442421Syokota c = get_controller_command_byte(kbdc); 116542421Syokota if (c == -1) { 116642421Syokota /* CONTROLLER ERROR */ 116742421Syokota kbdc_lock(kbdc, FALSE); 116842421Syokota printf("atkbd: unable to get the current command byte value.\n"); 116942421Syokota return EIO; 117042421Syokota } 117142421Syokota if (bootverbose) 117242421Syokota printf("atkbd: the current kbd controller command byte %04x\n", 117342421Syokota c); 117442421Syokota#if 0 117542421Syokota /* override the keyboard lock switch */ 117642421Syokota c |= KBD_OVERRIDE_KBD_LOCK; 117742421Syokota#endif 117842421Syokota 117942421Syokota /* enable the keyboard port, but disable the keyboard intr. */ 118042421Syokota if (setup_kbd_port(kbdc, TRUE, FALSE)) { 118142421Syokota /* CONTROLLER ERROR: there is very little we can do... */ 118242421Syokota printf("atkbd: unable to set the command byte.\n"); 118342421Syokota kbdc_lock(kbdc, FALSE); 118442421Syokota return EIO; 118542421Syokota } 118642421Syokota 118742421Syokota /* 118842421Syokota * Check if we have an XT keyboard before we attempt to reset it. 118942421Syokota * The procedure assumes that the keyboard and the controller have 119042421Syokota * been set up properly by BIOS and have not been messed up 119142421Syokota * during the boot process. 119242421Syokota */ 119342421Syokota codeset = -1; 119442421Syokota if (flags & KB_CONF_ALT_SCANCODESET) 119542421Syokota /* the user says there is a XT keyboard */ 119642421Syokota codeset = 1; 119742421Syokota#ifdef KBD_DETECT_XT_KEYBOARD 119842421Syokota else if ((c & KBD_TRANSLATION) == 0) { 119942421Syokota /* SET_SCANCODE_SET is not always supported; ignore error */ 120042421Syokota if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0) 120142421Syokota == KBD_ACK) 120242421Syokota codeset = read_kbd_data(kbdc); 120342421Syokota } 120442421Syokota if (bootverbose) 120542421Syokota printf("atkbd: scancode set %d\n", codeset); 120642421Syokota#endif /* KBD_DETECT_XT_KEYBOARD */ 120742421Syokota 120842421Syokota *type = KB_OTHER; 120942421Syokota id = get_kbd_id(kbdc); 121042421Syokota switch(id) { 121155730Syokota case 0x41ab: /* 101/102/... Enhanced */ 121255730Syokota case 0x83ab: /* ditto */ 121355730Syokota case 0x54ab: /* SpaceSaver */ 121455730Syokota case 0x84ab: /* ditto */ 121555730Syokota#if 0 121655730Syokota case 0x90ab: /* 'G' */ 121755730Syokota case 0x91ab: /* 'P' */ 121855730Syokota case 0x92ab: /* 'A' */ 121955730Syokota#endif 122042421Syokota *type = KB_101; 122142421Syokota break; 122242421Syokota case -1: /* AT 84 keyboard doesn't return ID */ 122342421Syokota *type = KB_84; 122442421Syokota break; 122542421Syokota default: 122642421Syokota break; 122742421Syokota } 122842421Syokota if (bootverbose) 122942421Syokota printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type); 123042421Syokota 123142421Syokota /* reset keyboard hardware */ 123242421Syokota if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) { 123342421Syokota /* 123442421Syokota * KEYBOARD ERROR 123542421Syokota * Keyboard reset may fail either because the keyboard 123642421Syokota * doen't exist, or because the keyboard doesn't pass 123742421Syokota * the self-test, or the keyboard controller on the 123842421Syokota * motherboard and the keyboard somehow fail to shake hands. 123942421Syokota * It is just possible, particularly in the last case, 1240110398Scharnier * that the keyboard controller may be left in a hung state. 124142421Syokota * test_controller() and test_kbd_port() appear to bring 124242421Syokota * the keyboard controller back (I don't know why and how, 124342421Syokota * though.) 124442421Syokota */ 124542421Syokota empty_both_buffers(kbdc, 10); 124642421Syokota test_controller(kbdc); 124742421Syokota test_kbd_port(kbdc); 124842421Syokota /* 124942421Syokota * We could disable the keyboard port and interrupt... but, 125042421Syokota * the keyboard may still exist (see above). 125142421Syokota */ 125242421Syokota set_controller_command_byte(kbdc, 0xff, c); 125342421Syokota kbdc_lock(kbdc, FALSE); 125442421Syokota if (bootverbose) 125542421Syokota printf("atkbd: failed to reset the keyboard.\n"); 125642421Syokota return EIO; 125742421Syokota } 125842421Syokota 125942421Syokota /* 126094275Sphk * Allow us to set the XT_KEYBD flag so that keyboards 126142421Syokota * such as those on the IBM ThinkPad laptop computers can be used 126242421Syokota * with the standard console driver. 126342421Syokota */ 126442421Syokota if (codeset == 1) { 126542421Syokota if (send_kbd_command_and_data(kbdc, 126642421Syokota KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) { 126742421Syokota /* XT kbd doesn't need scan code translation */ 126842421Syokota c &= ~KBD_TRANSLATION; 126942421Syokota } else { 127042421Syokota /* 127142421Syokota * KEYBOARD ERROR 127242421Syokota * The XT kbd isn't usable unless the proper scan 127342421Syokota * code set is selected. 127442421Syokota */ 127542421Syokota set_controller_command_byte(kbdc, 0xff, c); 127642421Syokota kbdc_lock(kbdc, FALSE); 127742421Syokota printf("atkbd: unable to set the XT keyboard mode.\n"); 127842421Syokota return EIO; 127942421Syokota } 128042421Syokota } 128142421Syokota 128242831Syokota#ifdef __alpha__ 128342831Syokota if (send_kbd_command_and_data( 128442831Syokota kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) { 128542831Syokota printf("atkbd: can't set translation.\n"); 128642831Syokota 128742831Syokota } 128842831Syokota c |= KBD_TRANSLATION; 128942831Syokota#endif 129042831Syokota 129142421Syokota /* enable the keyboard port and intr. */ 129242421Syokota if (!set_controller_command_byte(kbdc, 129342421Syokota KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK, 129442421Syokota (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK)) 129542421Syokota | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) { 129642421Syokota /* 129742421Syokota * CONTROLLER ERROR 129842421Syokota * This is serious; we are left with the disabled 129942421Syokota * keyboard intr. 130042421Syokota */ 130142421Syokota set_controller_command_byte(kbdc, 0xff, c); 130242421Syokota kbdc_lock(kbdc, FALSE); 130342421Syokota printf("atkbd: unable to enable the keyboard port and intr.\n"); 130442421Syokota return EIO; 130542421Syokota } 130642421Syokota 130742421Syokota kbdc_lock(kbdc, FALSE); 130842421Syokota return 0; 130942421Syokota} 131042421Syokota 131142421Syokotastatic int 131242421Syokotawrite_kbd(KBDC kbdc, int command, int data) 131342421Syokota{ 131442421Syokota int s; 131542421Syokota 131642421Syokota /* prevent the timeout routine from polling the keyboard */ 131742421Syokota if (!kbdc_lock(kbdc, TRUE)) 131842421Syokota return EBUSY; 131942421Syokota 132042421Syokota /* disable the keyboard and mouse interrupt */ 132142421Syokota s = spltty(); 132242421Syokota#if 0 132342421Syokota c = get_controller_command_byte(kbdc); 132442421Syokota if ((c == -1) 132542421Syokota || !set_controller_command_byte(kbdc, 132642421Syokota kbdc_get_device_mask(kbdc), 132742421Syokota KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 132842421Syokota | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 132942421Syokota /* CONTROLLER ERROR */ 133042421Syokota kbdc_lock(kbdc, FALSE); 133142421Syokota splx(s); 133242421Syokota return EIO; 133342421Syokota } 133442421Syokota /* 133542421Syokota * Now that the keyboard controller is told not to generate 133642421Syokota * the keyboard and mouse interrupts, call `splx()' to allow 133742421Syokota * the other tty interrupts. The clock interrupt may also occur, 133842421Syokota * but the timeout routine (`scrn_timer()') will be blocked 133942421Syokota * by the lock flag set via `kbdc_lock()' 134042421Syokota */ 134142421Syokota splx(s); 134242421Syokota#endif 134342421Syokota 134442421Syokota if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK) 134542421Syokota send_kbd_command(kbdc, KBDC_ENABLE_KBD); 134642421Syokota 134742421Syokota#if 0 134842421Syokota /* restore the interrupts */ 134942421Syokota if (!set_controller_command_byte(kbdc, 135042421Syokota kbdc_get_device_mask(kbdc), 135142421Syokota c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 135242421Syokota /* CONTROLLER ERROR */ 135342421Syokota } 135442421Syokota#else 135542421Syokota splx(s); 135642421Syokota#endif 135742421Syokota kbdc_lock(kbdc, FALSE); 135842421Syokota 135942421Syokota return 0; 136042421Syokota} 136142421Syokota 136242421Syokotastatic int 136342421Syokotaget_kbd_id(KBDC kbdc) 136442421Syokota{ 136542421Syokota int id1, id2; 136642421Syokota 136742421Syokota empty_both_buffers(kbdc, 10); 136842421Syokota id1 = id2 = -1; 136942421Syokota if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK) 137042421Syokota return -1; 137142421Syokota 137242421Syokota DELAY(10000); /* 10 msec delay */ 137342421Syokota id1 = read_kbd_data(kbdc); 137442421Syokota if (id1 != -1) 137542421Syokota id2 = read_kbd_data(kbdc); 137642421Syokota 137742421Syokota if ((id1 == -1) || (id2 == -1)) { 137842421Syokota empty_both_buffers(kbdc, 10); 137942421Syokota test_controller(kbdc); 138042421Syokota test_kbd_port(kbdc); 138142421Syokota return -1; 138242421Syokota } 138342421Syokota return ((id2 << 8) | id1); 138442421Syokota} 138542421Syokota 138654543Syokotastatic int delays[] = { 250, 500, 750, 1000 }; 138754543Syokotastatic int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 138854543Syokota 68, 76, 84, 92, 100, 110, 118, 126, 138954543Syokota 136, 152, 168, 184, 200, 220, 236, 252, 139054543Syokota 272, 304, 336, 368, 400, 440, 472, 504 }; 139154543Syokota 139244628Syokotastatic int 139354543Syokotatypematic_delay(int i) 139454543Syokota{ 139554543Syokota return delays[(i >> 5) & 3]; 139654543Syokota} 139754543Syokota 139854543Syokotastatic int 139954543Syokotatypematic_rate(int i) 140054543Syokota{ 140154543Syokota return rates[i & 0x1f]; 140254543Syokota} 140354543Syokota 140454543Syokotastatic int 140544628Syokotatypematic(int delay, int rate) 140644628Syokota{ 140744628Syokota int value; 140844628Syokota int i; 140944628Syokota 141044628Syokota for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) { 141144628Syokota if (delay >= delays[i]) 141244628Syokota break; 141344628Syokota } 141444628Syokota value = i << 5; 141544628Syokota for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) { 141644628Syokota if (rate >= rates[i]) 141744628Syokota break; 141844628Syokota } 141944628Syokota value |= i; 142044628Syokota return value; 142144628Syokota} 1422