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: releng/10.3/sys/dev/atkbdc/atkbd.c 283219 2015-05-21 10:40:18Z royger $"); 30119418Sobrien 31162711Sru#include "opt_compat.h" 3242421Syokota#include "opt_kbd.h" 3344628Syokota#include "opt_atkbd.h" 3442421Syokota 3542421Syokota#include <sys/param.h> 3642421Syokota#include <sys/systm.h> 3742421Syokota#include <sys/kernel.h> 3847336Syokota#include <sys/bus.h> 39147271Smarius#include <sys/eventhandler.h> 4042421Syokota#include <sys/proc.h> 41114216Skan#include <sys/limits.h> 4242421Syokota#include <sys/malloc.h> 4342421Syokota 4458271Syokota#include <machine/bus.h> 4558271Syokota#include <machine/resource.h> 4658271Syokota 4780040Syokota#include <sys/kbio.h> 4842421Syokota#include <dev/kbd/kbdreg.h> 49147271Smarius#include <dev/atkbdc/atkbdreg.h> 50147271Smarius#include <dev/atkbdc/atkbdcreg.h> 5142421Syokota 5242421Syokotastatic timeout_t atkbd_timeout; 53147271Smariusstatic void atkbd_shutdown_final(void *v); 5442421Syokota 55283219Sroyger#define DEFAULT_DELAY 0x1 /* 500ms */ 56283219Sroyger#define DEFAULT_RATE 0x10 /* 14Hz */ 57283219Sroyger 5842421Syokotaint 59245315Simpatkbd_probe_unit(device_t dev, int irq, int flags) 6042421Syokota{ 6142421Syokota keyboard_switch_t *sw; 6242421Syokota int args[2]; 6344628Syokota int error; 6442421Syokota 6542421Syokota sw = kbd_get_switch(ATKBD_DRIVER_NAME); 6642421Syokota if (sw == NULL) 6742421Syokota return ENXIO; 6842421Syokota 69245315Simp args[0] = device_get_unit(device_get_parent(dev)); 7042421Syokota args[1] = irq; 71245315Simp error = (*sw->probe)(device_get_unit(dev), args, flags); 7244628Syokota if (error) 7344628Syokota return error; 7444628Syokota return 0; 7542421Syokota} 7642421Syokota 7742421Syokotaint 78245315Simpatkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags) 7942421Syokota{ 8042421Syokota keyboard_switch_t *sw; 8144628Syokota int args[2]; 8242421Syokota int error; 83245315Simp int unit; 8442421Syokota 8542421Syokota sw = kbd_get_switch(ATKBD_DRIVER_NAME); 8642421Syokota if (sw == NULL) 8742421Syokota return ENXIO; 8842421Syokota 8942421Syokota /* reset, initialize and enable the device */ 90245315Simp unit = device_get_unit(dev); 91245315Simp args[0] = device_get_unit(device_get_parent(dev)); 9244628Syokota args[1] = irq; 9350154Syokota *kbd = NULL; 9444628Syokota error = (*sw->probe)(unit, args, flags); 9542421Syokota if (error) 9644628Syokota return error; 9750154Syokota error = (*sw->init)(unit, kbd, args, flags); 9844628Syokota if (error) 9944628Syokota return error; 10050154Syokota (*sw->enable)(*kbd); 10142421Syokota 10242421Syokota#ifdef KBD_INSTALL_CDEV 10342421Syokota /* attach a virtual keyboard cdev */ 10450154Syokota error = kbd_attach(*kbd); 10542421Syokota if (error) 10642421Syokota return error; 10742421Syokota#endif 10842421Syokota 10942421Syokota /* 11042421Syokota * This is a kludge to compensate for lost keyboard interrupts. 11142421Syokota * A similar code used to be in syscons. See below. XXX 11242421Syokota */ 11350154Syokota atkbd_timeout(*kbd); 11442421Syokota 11542421Syokota if (bootverbose) 11650154Syokota (*sw->diag)(*kbd, bootverbose); 117147271Smarius 118147271Smarius EVENTHANDLER_REGISTER(shutdown_final, atkbd_shutdown_final, *kbd, 119147271Smarius SHUTDOWN_PRI_DEFAULT); 120147271Smarius 12142421Syokota return 0; 12242421Syokota} 12342421Syokota 12442421Syokotastatic void 12542421Syokotaatkbd_timeout(void *arg) 12642421Syokota{ 12742421Syokota keyboard_t *kbd; 12842421Syokota int s; 12942421Syokota 13056334Syokota /* 13156334Syokota * The original text of the following comments are extracted 13256334Syokota * from syscons.c (1.287) 13356334Syokota * 13442421Syokota * With release 2.1 of the Xaccel server, the keyboard is left 13542421Syokota * hanging pretty often. Apparently an interrupt from the 13642421Syokota * keyboard is lost, and I don't know why (yet). 13756334Syokota * This ugly hack calls the low-level interrupt routine if input 13856334Syokota * is ready for the keyboard and conveniently hides the problem. XXX 13956334Syokota * 14056334Syokota * Try removing anything stuck in the keyboard controller; whether 14156334Syokota * it's a keyboard scan code or mouse data. The low-level 14256334Syokota * interrupt routine doesn't read the mouse data directly, 14356334Syokota * but the keyboard controller driver will, as a side effect. 14442421Syokota */ 14542421Syokota /* 14656334Syokota * And here is bde's original comment about this: 14756334Syokota * 14856334Syokota * This is necessary to handle edge triggered interrupts - if we 14956334Syokota * returned when our IRQ is high due to unserviced input, then there 15056334Syokota * would be no more keyboard IRQs until the keyboard is reset by 15156334Syokota * external powers. 15256334Syokota * 15356334Syokota * The keyboard apparently unwedges the irq in most cases. 15442421Syokota */ 15542421Syokota s = spltty(); 15642421Syokota kbd = (keyboard_t *)arg; 157174984Swkoszek if (kbdd_lock(kbd, TRUE)) { 15842421Syokota /* 15942421Syokota * We have seen the lock flag is not set. Let's reset 16042421Syokota * the flag early, otherwise the LED update routine fails 16142421Syokota * which may want the lock during the interrupt routine. 16242421Syokota */ 163174984Swkoszek kbdd_lock(kbd, FALSE); 164174984Swkoszek if (kbdd_check_char(kbd)) 165174984Swkoszek kbdd_intr(kbd, NULL); 16642421Syokota } 16742421Syokota splx(s); 16842421Syokota timeout(atkbd_timeout, arg, hz/10); 16942421Syokota} 17042421Syokota 17142421Syokota/* LOW-LEVEL */ 17242421Syokota 17342421Syokota#define ATKBD_DEFAULT 0 17442421Syokota 17542421Syokotatypedef struct atkbd_state { 17642421Syokota KBDC kbdc; /* keyboard controller */ 17742421Syokota int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */ 17842421Syokota int ks_flags; /* flags */ 17942421Syokota#define COMPOSE (1 << 0) 18044628Syokota int ks_polling; 18142421Syokota int ks_state; /* shift/lock key state */ 18242421Syokota int ks_accents; /* accent key index (> 0) */ 18342421Syokota u_int ks_composed_char; /* composed char code (> 0) */ 18442421Syokota u_char ks_prefix; /* AT scan code prefix */ 18542421Syokota} atkbd_state_t; 18642421Syokota 18742421Syokota/* keyboard driver declaration */ 18842421Syokotastatic int atkbd_configure(int flags); 18942421Syokotastatic kbd_probe_t atkbd_probe; 19042421Syokotastatic kbd_init_t atkbd_init; 19142421Syokotastatic kbd_term_t atkbd_term; 19242421Syokotastatic kbd_intr_t atkbd_intr; 19342421Syokotastatic kbd_test_if_t atkbd_test_if; 19442421Syokotastatic kbd_enable_t atkbd_enable; 19542421Syokotastatic kbd_disable_t atkbd_disable; 19642421Syokotastatic kbd_read_t atkbd_read; 19742421Syokotastatic kbd_check_t atkbd_check; 19842421Syokotastatic kbd_read_char_t atkbd_read_char; 19942421Syokotastatic kbd_check_char_t atkbd_check_char; 20042421Syokotastatic kbd_ioctl_t atkbd_ioctl; 20142421Syokotastatic kbd_lock_t atkbd_lock; 20242421Syokotastatic kbd_clear_state_t atkbd_clear_state; 20342421Syokotastatic kbd_get_state_t atkbd_get_state; 20442421Syokotastatic kbd_set_state_t atkbd_set_state; 20544628Syokotastatic kbd_poll_mode_t atkbd_poll; 20642421Syokota 207114293Smarkmstatic keyboard_switch_t atkbdsw = { 20842421Syokota atkbd_probe, 20942421Syokota atkbd_init, 21042421Syokota atkbd_term, 21142421Syokota atkbd_intr, 21242421Syokota atkbd_test_if, 21342421Syokota atkbd_enable, 21442421Syokota atkbd_disable, 21542421Syokota atkbd_read, 21642421Syokota atkbd_check, 21742421Syokota atkbd_read_char, 21842421Syokota atkbd_check_char, 21942421Syokota atkbd_ioctl, 22042421Syokota atkbd_lock, 22142421Syokota atkbd_clear_state, 22242421Syokota atkbd_get_state, 22342421Syokota atkbd_set_state, 22442421Syokota genkbd_get_fkeystr, 22544628Syokota atkbd_poll, 22642421Syokota genkbd_diag, 22742421Syokota}; 22842421Syokota 22942421SyokotaKEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure); 23042421Syokota 23142421Syokota/* local functions */ 232283219Sroygerstatic int set_typematic(keyboard_t *kbd); 23342421Syokotastatic int setup_kbd_port(KBDC kbdc, int port, int intr); 23442421Syokotastatic int get_kbd_echo(KBDC kbdc); 23542421Syokotastatic int probe_keyboard(KBDC kbdc, int flags); 23642421Syokotastatic int init_keyboard(KBDC kbdc, int *type, int flags); 23742421Syokotastatic int write_kbd(KBDC kbdc, int command, int data); 23842421Syokotastatic int get_kbd_id(KBDC kbdc); 23944628Syokotastatic int typematic(int delay, int rate); 24054543Syokotastatic int typematic_delay(int delay); 24154543Syokotastatic int typematic_rate(int rate); 24242421Syokota 24342421Syokota/* local variables */ 24442421Syokota 24542421Syokota/* the initial key map, accent map and fkey strings */ 24644628Syokota#ifdef ATKBD_DFLT_KEYMAP 24744628Syokota#define KBD_DFLT_KEYMAP 24844628Syokota#include "atkbdmap.h" 24944628Syokota#endif 25042831Syokota#include <dev/kbd/kbdtables.h> 25142421Syokota 25242421Syokota/* structures for the default keyboard */ 25342421Syokotastatic keyboard_t default_kbd; 25442421Syokotastatic atkbd_state_t default_kbd_state; 25542421Syokotastatic keymap_t default_keymap; 25642421Syokotastatic accentmap_t default_accentmap; 25742421Syokotastatic fkeytab_t default_fkeytab[NUM_FKEYS]; 25842421Syokota 25942421Syokota/* 26042421Syokota * The back door to the keyboard driver! 26142421Syokota * This function is called by the console driver, via the kbdio module, 26242421Syokota * to tickle keyboard drivers when the low-level console is being initialized. 26342421Syokota * Almost nothing in the kernel has been initialied yet. Try to probe 26442421Syokota * keyboards if possible. 26594228Sasmodai * NOTE: because of the way the low-level console is initialized, this routine 26642421Syokota * may be called more than once!! 26742421Syokota */ 26842421Syokotastatic int 26942421Syokotaatkbd_configure(int flags) 27042421Syokota{ 27142421Syokota keyboard_t *kbd; 27242421Syokota int arg[2]; 27344628Syokota int i; 27442421Syokota 275158041Ssobomax /* 276158041Ssobomax * Probe the keyboard controller, if not present or if the driver 277158041Ssobomax * is disabled, unregister the keyboard if any. 278158041Ssobomax */ 279158041Ssobomax if (atkbdc_configure() != 0 || 280158041Ssobomax resource_disabled("atkbd", ATKBD_DEFAULT)) { 28146764Syokota i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT); 28246764Syokota if (i >= 0) { 28346764Syokota kbd = kbd_get_keyboard(i); 28446764Syokota kbd_unregister(kbd); 28546764Syokota kbd->kb_flags &= ~KB_REGISTERED; 28644628Syokota } 28748878Syokota return 0; 28844628Syokota } 28945720Speter 29046764Syokota /* XXX: a kludge to obtain the device configuration flags */ 29146764Syokota if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0) 29246764Syokota flags |= i; 29346764Syokota 29442421Syokota /* probe the default keyboard */ 29542421Syokota arg[0] = -1; 29642421Syokota arg[1] = -1; 29744628Syokota kbd = NULL; 29844628Syokota if (atkbd_probe(ATKBD_DEFAULT, arg, flags)) 29942421Syokota return 0; 30044628Syokota if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags)) 30144628Syokota return 0; 30242421Syokota 30344628Syokota /* return the number of found keyboards */ 30444628Syokota return 1; 30544628Syokota} 30644628Syokota 30744628Syokota/* low-level functions */ 30844628Syokota 30944628Syokota/* detect a keyboard */ 31044628Syokotastatic int 31144628Syokotaatkbd_probe(int unit, void *arg, int flags) 31244628Syokota{ 31344628Syokota KBDC kbdc; 31458271Syokota int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ 31544628Syokota 31644628Syokota /* XXX */ 31744628Syokota if (unit == ATKBD_DEFAULT) { 31844628Syokota if (KBD_IS_PROBED(&default_kbd)) 31942421Syokota return 0; 32042421Syokota } 32142421Syokota 32258271Syokota kbdc = atkbdc_open(data[0]); 32344628Syokota if (kbdc == NULL) 32444628Syokota return ENXIO; 32544628Syokota if (probe_keyboard(kbdc, flags)) { 32644628Syokota if (flags & KB_CONF_FAIL_IF_NO_KBD) 32744628Syokota return ENXIO; 32842421Syokota } 32944628Syokota return 0; 33042421Syokota} 33142421Syokota 33244628Syokota/* reset and initialize the device */ 33342421Syokotastatic int 33444628Syokotaatkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) 33542421Syokota{ 33642421Syokota keyboard_t *kbd; 33742421Syokota atkbd_state_t *state; 33842421Syokota keymap_t *keymap; 33942421Syokota accentmap_t *accmap; 34042421Syokota fkeytab_t *fkeymap; 34142421Syokota int fkeymap_size; 34255731Syokota int delay[2]; 34358271Syokota int *data = (int *)arg; /* data[0]: controller, data[1]: irq */ 344142569Ssam int error, needfree; 34542421Syokota 34642421Syokota /* XXX */ 34742421Syokota if (unit == ATKBD_DEFAULT) { 34842421Syokota *kbdp = kbd = &default_kbd; 34944628Syokota if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd)) 35042421Syokota return 0; 35142421Syokota state = &default_kbd_state; 35242421Syokota keymap = &default_keymap; 35342421Syokota accmap = &default_accentmap; 35442421Syokota fkeymap = default_fkeytab; 35542421Syokota fkeymap_size = 35642421Syokota sizeof(default_fkeytab)/sizeof(default_fkeytab[0]); 357142569Ssam needfree = 0; 35842421Syokota } else if (*kbdp == NULL) { 35969781Sdwmalone *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO); 36069781Sdwmalone state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO); 361142569Ssam /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */ 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]); 366142569Ssam needfree = 1; 36769781Sdwmalone if ((kbd == NULL) || (state == NULL) || (keymap == NULL) 36869781Sdwmalone || (accmap == NULL) || (fkeymap == NULL)) { 369142569Ssam error = ENOMEM; 370142569Ssam goto bad; 37142421Syokota } 37244628Syokota } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { 37342421Syokota return 0; 37442421Syokota } else { 37542421Syokota kbd = *kbdp; 37642421Syokota state = (atkbd_state_t *)kbd->kb_data; 37742421Syokota bzero(state, sizeof(*state)); 37842421Syokota keymap = kbd->kb_keymap; 37942421Syokota accmap = kbd->kb_accentmap; 38042421Syokota fkeymap = kbd->kb_fkeytab; 38142421Syokota fkeymap_size = kbd->kb_fkeytab_size; 382142569Ssam needfree = 0; 38342421Syokota } 38442421Syokota 38544628Syokota if (!KBD_IS_PROBED(kbd)) { 38658271Syokota state->kbdc = atkbdc_open(data[0]); 387142569Ssam if (state->kbdc == NULL) { 388142569Ssam error = ENXIO; 389142569Ssam goto bad; 390142569Ssam } 39144628Syokota kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags, 39258271Syokota 0, 0); 39344628Syokota bcopy(&key_map, keymap, sizeof(key_map)); 39444628Syokota bcopy(&accent_map, accmap, sizeof(accent_map)); 39544628Syokota bcopy(fkey_tab, fkeymap, 396245314Simp imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab))); 39744628Syokota kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); 39844628Syokota kbd->kb_data = (void *)state; 39944628Syokota 40044628Syokota if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */ 401142569Ssam if (flags & KB_CONF_FAIL_IF_NO_KBD) { 402142569Ssam error = ENXIO; 403142569Ssam goto bad; 404142569Ssam } 40544628Syokota } else { 40644628Syokota KBD_FOUND_DEVICE(kbd); 40744628Syokota } 40844628Syokota atkbd_clear_state(kbd); 40944628Syokota state->ks_mode = K_XLATE; 41044628Syokota /* 41144628Syokota * FIXME: set the initial value for lock keys in ks_state 41244628Syokota * according to the BIOS data? 41344628Syokota */ 41444628Syokota KBD_PROBE_DONE(kbd); 41542421Syokota } 41644628Syokota if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { 41749820Syokota kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; 41842421Syokota if (KBD_HAS_DEVICE(kbd) 419245314Simp && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config) 420245314Simp && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) { 421117513Ssimokawa kbd_unregister(kbd); 422142569Ssam error = ENXIO; 423142569Ssam goto bad; 424117513Ssimokawa } 42544628Syokota atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 426283219Sroyger set_typematic(kbd); 42755731Syokota delay[0] = kbd->kb_delay1; 42855731Syokota delay[1] = kbd->kb_delay2; 42955731Syokota atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 43042421Syokota KBD_INIT_DONE(kbd); 43142421Syokota } 43242421Syokota if (!KBD_IS_CONFIGURED(kbd)) { 433142569Ssam if (kbd_register(kbd) < 0) { 434142569Ssam error = ENXIO; 435142569Ssam goto bad; 436142569Ssam } 43742421Syokota KBD_CONFIG_DONE(kbd); 43842421Syokota } 43942421Syokota 44042421Syokota return 0; 441142569Ssambad: 442142569Ssam if (needfree) { 443142569Ssam if (state != NULL) 444142569Ssam free(state, M_DEVBUF); 445142569Ssam if (keymap != NULL) 446142569Ssam free(keymap, M_DEVBUF); 447142569Ssam if (accmap != NULL) 448142569Ssam free(accmap, M_DEVBUF); 449142569Ssam if (fkeymap != NULL) 450142569Ssam free(fkeymap, M_DEVBUF); 451142569Ssam if (kbd != NULL) { 452142569Ssam free(kbd, M_DEVBUF); 453142569Ssam *kbdp = NULL; /* insure ref doesn't leak to caller */ 454142569Ssam } 455142569Ssam } 456142569Ssam return error; 45742421Syokota} 45842421Syokota 45942421Syokota/* finish using this keyboard */ 46042421Syokotastatic int 46142421Syokotaatkbd_term(keyboard_t *kbd) 46242421Syokota{ 46342421Syokota kbd_unregister(kbd); 46442421Syokota return 0; 46542421Syokota} 46642421Syokota 46742421Syokota/* keyboard interrupt routine */ 46842421Syokotastatic int 46944628Syokotaatkbd_intr(keyboard_t *kbd, void *arg) 47042421Syokota{ 471191164Semax atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data; 47255731Syokota int delay[2]; 47342421Syokota int c; 47442421Syokota 475163687Sru if (!KBD_HAS_DEVICE(kbd)) { 476163687Sru /* 477163687Sru * The keyboard was not detected before; 478163687Sru * it must have been reconnected! 479163687Sru */ 480245314Simp init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config); 481163687Sru KBD_FOUND_DEVICE(kbd); 482163687Sru atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); 483283219Sroyger set_typematic(kbd); 484163687Sru delay[0] = kbd->kb_delay1; 485163687Sru delay[1] = kbd->kb_delay2; 486163687Sru atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); 487163687Sru } 488163687Sru 489191164Semax if (state->ks_polling) 490191164Semax return 0; 491191164Semax 49242421Syokota if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { 49342421Syokota /* let the callback function to process the input */ 49442421Syokota (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, 49542421Syokota kbd->kb_callback.kc_arg); 49642421Syokota } else { 49742421Syokota /* read and discard the input; no one is waiting for input */ 49842421Syokota do { 49942421Syokota c = atkbd_read_char(kbd, FALSE); 50042421Syokota } while (c != NOKEY); 50142421Syokota } 50242421Syokota return 0; 50342421Syokota} 50442421Syokota 50542421Syokota/* test the interface to the device */ 50642421Syokotastatic int 50742421Syokotaatkbd_test_if(keyboard_t *kbd) 50842421Syokota{ 50942421Syokota int error; 51042421Syokota int s; 51142421Syokota 51242421Syokota error = 0; 51342421Syokota empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10); 51442421Syokota s = spltty(); 51542421Syokota if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc)) 51642421Syokota error = EIO; 51742421Syokota else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0) 51842421Syokota error = EIO; 51942421Syokota splx(s); 52042421Syokota 52142421Syokota return error; 52242421Syokota} 52342421Syokota 52442421Syokota/* 52542421Syokota * Enable the access to the device; until this function is called, 52642421Syokota * the client cannot read from the keyboard. 52742421Syokota */ 52842421Syokotastatic int 52942421Syokotaatkbd_enable(keyboard_t *kbd) 53042421Syokota{ 53142421Syokota int s; 53242421Syokota 53342421Syokota s = spltty(); 53442421Syokota KBD_ACTIVATE(kbd); 53542421Syokota splx(s); 53642421Syokota return 0; 53742421Syokota} 53842421Syokota 53942421Syokota/* disallow the access to the device */ 54042421Syokotastatic int 54142421Syokotaatkbd_disable(keyboard_t *kbd) 54242421Syokota{ 54342421Syokota int s; 54442421Syokota 54542421Syokota s = spltty(); 54642421Syokota KBD_DEACTIVATE(kbd); 54742421Syokota splx(s); 54842421Syokota return 0; 54942421Syokota} 55042421Syokota 55142421Syokota/* read one byte from the keyboard if it's allowed */ 55242421Syokotastatic int 55342421Syokotaatkbd_read(keyboard_t *kbd, int wait) 55442421Syokota{ 55542421Syokota int c; 55642421Syokota 55742421Syokota if (wait) 55842421Syokota c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc); 55942421Syokota else 56042421Syokota c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc); 56154382Syokota if (c != -1) 56254382Syokota ++kbd->kb_count; 56342421Syokota return (KBD_IS_ACTIVE(kbd) ? c : -1); 56442421Syokota} 56542421Syokota 56642421Syokota/* check if data is waiting */ 56742421Syokotastatic int 56842421Syokotaatkbd_check(keyboard_t *kbd) 56942421Syokota{ 57042421Syokota if (!KBD_IS_ACTIVE(kbd)) 57142421Syokota return FALSE; 57242421Syokota return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc); 57342421Syokota} 57442421Syokota 57542421Syokota/* read char from the keyboard */ 57642421Syokotastatic u_int 57742421Syokotaatkbd_read_char(keyboard_t *kbd, int wait) 57842421Syokota{ 57942421Syokota atkbd_state_t *state; 58042421Syokota u_int action; 58142421Syokota int scancode; 58242421Syokota int keycode; 58342421Syokota 58442421Syokota state = (atkbd_state_t *)kbd->kb_data; 58542421Syokotanext_code: 58642421Syokota /* do we have a composed char to return? */ 58742421Syokota if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { 58842421Syokota action = state->ks_composed_char; 58942421Syokota state->ks_composed_char = 0; 59042421Syokota if (action > UCHAR_MAX) 59142421Syokota return ERRKEY; 59242421Syokota return action; 59342421Syokota } 59442421Syokota 59542421Syokota /* see if there is something in the keyboard port */ 59642421Syokota if (wait) { 59742421Syokota do { 59842421Syokota scancode = read_kbd_data(state->kbdc); 59942421Syokota } while (scancode == -1); 60042421Syokota } else { 60142421Syokota scancode = read_kbd_data_no_wait(state->kbdc); 60242421Syokota if (scancode == -1) 60342421Syokota return NOKEY; 60442421Syokota } 60554382Syokota ++kbd->kb_count; 60642421Syokota 60754382Syokota#if KBDIO_DEBUG >= 10 60854382Syokota printf("atkbd_read_char(): scancode:0x%x\n", scancode); 60954382Syokota#endif 61054382Syokota 61142421Syokota /* return the byte as is for the K_RAW mode */ 61242421Syokota if (state->ks_mode == K_RAW) 61342421Syokota return scancode; 61442421Syokota 61542421Syokota /* translate the scan code into a keycode */ 61642421Syokota keycode = scancode & 0x7F; 61742421Syokota switch (state->ks_prefix) { 61842421Syokota case 0x00: /* normal scancode */ 61942421Syokota switch(scancode) { 62042421Syokota case 0xB8: /* left alt (compose key) released */ 62142421Syokota if (state->ks_flags & COMPOSE) { 62242421Syokota state->ks_flags &= ~COMPOSE; 62342421Syokota if (state->ks_composed_char > UCHAR_MAX) 62442421Syokota state->ks_composed_char = 0; 62542421Syokota } 62642421Syokota break; 62742421Syokota case 0x38: /* left alt (compose key) pressed */ 62842421Syokota if (!(state->ks_flags & COMPOSE)) { 62942421Syokota state->ks_flags |= COMPOSE; 63042421Syokota state->ks_composed_char = 0; 63142421Syokota } 63242421Syokota break; 63342421Syokota case 0xE0: 63442421Syokota case 0xE1: 63542421Syokota state->ks_prefix = scancode; 63642421Syokota goto next_code; 63742421Syokota } 63842421Syokota break; 639245314Simp case 0xE0: /* 0xE0 prefix */ 64042421Syokota state->ks_prefix = 0; 64142421Syokota switch (keycode) { 64242421Syokota case 0x1C: /* right enter key */ 64342421Syokota keycode = 0x59; 64442421Syokota break; 64542421Syokota case 0x1D: /* right ctrl key */ 64642421Syokota keycode = 0x5A; 64742421Syokota break; 64842421Syokota case 0x35: /* keypad divide key */ 649245314Simp keycode = 0x5B; 650245314Simp break; 65142421Syokota case 0x37: /* print scrn key */ 652245314Simp keycode = 0x5C; 653245314Simp break; 65442421Syokota case 0x38: /* right alt key (alt gr) */ 655245314Simp keycode = 0x5D; 656245314Simp break; 65743337Syokota case 0x46: /* ctrl-pause/break on AT 101 (see below) */ 65843337Syokota keycode = 0x68; 659245314Simp break; 66042421Syokota case 0x47: /* grey home key */ 661245314Simp keycode = 0x5E; 662245314Simp break; 66342421Syokota case 0x48: /* grey up arrow key */ 664245314Simp keycode = 0x5F; 665245314Simp break; 66642421Syokota case 0x49: /* grey page up key */ 667245314Simp keycode = 0x60; 668245314Simp break; 66942421Syokota case 0x4B: /* grey left arrow key */ 670245314Simp keycode = 0x61; 671245314Simp break; 67242421Syokota case 0x4D: /* grey right arrow key */ 673245314Simp keycode = 0x62; 674245314Simp break; 67542421Syokota case 0x4F: /* grey end key */ 676245314Simp keycode = 0x63; 677245314Simp break; 67842421Syokota case 0x50: /* grey down arrow key */ 679245314Simp keycode = 0x64; 680245314Simp break; 68142421Syokota case 0x51: /* grey page down key */ 682245314Simp keycode = 0x65; 683245314Simp break; 68442421Syokota case 0x52: /* grey insert key */ 685245314Simp keycode = 0x66; 686245314Simp break; 68742421Syokota case 0x53: /* grey delete key */ 688245314Simp keycode = 0x67; 689245314Simp break; 690245314Simp /* the following 3 are only used on the MS "Natural" keyboard */ 69142421Syokota case 0x5b: /* left Window key */ 692245314Simp keycode = 0x69; 693245314Simp break; 69442421Syokota case 0x5c: /* right Window key */ 695245314Simp keycode = 0x6a; 696245314Simp break; 69742421Syokota case 0x5d: /* menu key */ 698245314Simp keycode = 0x6b; 699245314Simp break; 700120875Sfjoe case 0x5e: /* power key */ 701120875Sfjoe keycode = 0x6d; 702120875Sfjoe break; 703120875Sfjoe case 0x5f: /* sleep key */ 704120875Sfjoe keycode = 0x6e; 705120875Sfjoe break; 706120875Sfjoe case 0x63: /* wake key */ 707120875Sfjoe keycode = 0x6f; 708120875Sfjoe break; 70942421Syokota default: /* ignore everything else */ 710245314Simp goto next_code; 71142421Syokota } 71242421Syokota break; 713245314Simp case 0xE1: /* 0xE1 prefix */ 71443337Syokota /* 71543337Syokota * The pause/break key on the 101 keyboard produces: 71643337Syokota * E1-1D-45 E1-9D-C5 71743337Syokota * Ctrl-pause/break produces: 71843337Syokota * E0-46 E0-C6 (See above.) 71943337Syokota */ 72042421Syokota state->ks_prefix = 0; 72142421Syokota if (keycode == 0x1D) 722245314Simp state->ks_prefix = 0x1D; 72342421Syokota goto next_code; 72442421Syokota /* NOT REACHED */ 725245314Simp case 0x1D: /* pause / break */ 72642421Syokota state->ks_prefix = 0; 72742421Syokota if (keycode != 0x45) 72843337Syokota goto next_code; 72942421Syokota keycode = 0x68; 73042421Syokota break; 73142421Syokota } 73242421Syokota 73343337Syokota if (kbd->kb_type == KB_84) { 73443337Syokota switch (keycode) { 73543337Syokota case 0x37: /* *(numpad)/print screen */ 73643337Syokota if (state->ks_flags & SHIFTS) 737245314Simp keycode = 0x5c; /* print screen */ 73843337Syokota break; 73943337Syokota case 0x45: /* num lock/pause */ 74043337Syokota if (state->ks_flags & CTLS) 74143337Syokota keycode = 0x68; /* pause */ 74243337Syokota break; 74343337Syokota case 0x46: /* scroll lock/break */ 74443337Syokota if (state->ks_flags & CTLS) 74543337Syokota keycode = 0x6c; /* break */ 74643337Syokota break; 74743337Syokota } 74843337Syokota } else if (kbd->kb_type == KB_101) { 74943337Syokota switch (keycode) { 75043337Syokota case 0x5c: /* print screen */ 75143337Syokota if (state->ks_flags & ALTS) 75243337Syokota keycode = 0x54; /* sysrq */ 75343337Syokota break; 75443337Syokota case 0x68: /* pause/break */ 75543337Syokota if (state->ks_flags & CTLS) 75643337Syokota keycode = 0x6c; /* break */ 75743337Syokota break; 75843337Syokota } 75943337Syokota } 76043337Syokota 76142421Syokota /* return the key code in the K_CODE mode */ 76242421Syokota if (state->ks_mode == K_CODE) 76342421Syokota return (keycode | (scancode & 0x80)); 76442421Syokota 76542421Syokota /* compose a character code */ 76642421Syokota if (state->ks_flags & COMPOSE) { 76747293Syokota switch (keycode | (scancode & 0x80)) { 76842421Syokota /* key pressed, process it */ 76942421Syokota case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ 77042421Syokota state->ks_composed_char *= 10; 77146765Syokota state->ks_composed_char += keycode - 0x40; 77242421Syokota if (state->ks_composed_char > UCHAR_MAX) 77342421Syokota return ERRKEY; 77442421Syokota goto next_code; 77542421Syokota case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ 77642421Syokota state->ks_composed_char *= 10; 77746765Syokota state->ks_composed_char += keycode - 0x47; 77842421Syokota if (state->ks_composed_char > UCHAR_MAX) 77942421Syokota return ERRKEY; 78042421Syokota goto next_code; 78142421Syokota case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ 78242421Syokota state->ks_composed_char *= 10; 78346765Syokota state->ks_composed_char += keycode - 0x4E; 78442421Syokota if (state->ks_composed_char > UCHAR_MAX) 78542421Syokota return ERRKEY; 78642421Syokota goto next_code; 78742421Syokota case 0x52: /* keypad 0 */ 78842421Syokota state->ks_composed_char *= 10; 78942421Syokota if (state->ks_composed_char > UCHAR_MAX) 79042421Syokota return ERRKEY; 79142421Syokota goto next_code; 79242421Syokota 79342421Syokota /* key released, no interest here */ 79442421Syokota case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ 79542421Syokota case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ 79642421Syokota case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ 79742421Syokota case 0xD2: /* keypad 0 */ 79842421Syokota goto next_code; 79942421Syokota 80042421Syokota case 0x38: /* left alt key */ 80142421Syokota break; 80242421Syokota 80342421Syokota default: 80442421Syokota if (state->ks_composed_char > 0) { 80542421Syokota state->ks_flags &= ~COMPOSE; 80642421Syokota state->ks_composed_char = 0; 80742421Syokota return ERRKEY; 80842421Syokota } 80942421Syokota break; 81042421Syokota } 81142421Syokota } 81242421Syokota 81342421Syokota /* keycode to key action */ 81442421Syokota action = genkbd_keyaction(kbd, keycode, scancode & 0x80, 81542421Syokota &state->ks_state, &state->ks_accents); 81642421Syokota if (action == NOKEY) 81742421Syokota goto next_code; 81842421Syokota else 81942421Syokota return action; 82042421Syokota} 82142421Syokota 82242421Syokota/* check if char is waiting */ 82342421Syokotastatic int 82442421Syokotaatkbd_check_char(keyboard_t *kbd) 82542421Syokota{ 82642421Syokota atkbd_state_t *state; 82742421Syokota 82842421Syokota if (!KBD_IS_ACTIVE(kbd)) 82942421Syokota return FALSE; 83042421Syokota state = (atkbd_state_t *)kbd->kb_data; 83142421Syokota if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) 83242421Syokota return TRUE; 83342421Syokota return kbdc_data_ready(state->kbdc); 83442421Syokota} 83542421Syokota 83642421Syokota/* some useful control functions */ 83742421Syokotastatic int 83842421Syokotaatkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) 83942421Syokota{ 840147271Smarius /* translate LED_XXX bits into the device specific bits */ 84142421Syokota static u_char ledmap[8] = { 84242421Syokota 0, 4, 2, 6, 1, 5, 3, 7, 84342421Syokota }; 84442421Syokota atkbd_state_t *state = kbd->kb_data; 84542421Syokota int error; 84642421Syokota int s; 84742421Syokota int i; 848162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 849162711Sru defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 850162711Sru int ival; 851162711Sru#endif 85242421Syokota 85342421Syokota s = spltty(); 85442421Syokota switch (cmd) { 85542421Syokota 85642421Syokota case KDGKBMODE: /* get keyboard mode */ 85742421Syokota *(int *)arg = state->ks_mode; 85842421Syokota break; 859162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 860162711Sru defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 861162711Sru case _IO('K', 7): 862162711Sru ival = IOCPARM_IVAL(arg); 863162711Sru arg = (caddr_t)&ival; 864162711Sru /* FALLTHROUGH */ 865162711Sru#endif 86642421Syokota case KDSKBMODE: /* set keyboard mode */ 86742421Syokota switch (*(int *)arg) { 86842421Syokota case K_XLATE: 86942421Syokota if (state->ks_mode != K_XLATE) { 87042421Syokota /* make lock key state and LED state match */ 87142421Syokota state->ks_state &= ~LOCK_MASK; 87242421Syokota state->ks_state |= KBD_LED_VAL(kbd); 87342421Syokota } 874102412Scharnier /* FALLTHROUGH */ 87542421Syokota case K_RAW: 87642421Syokota case K_CODE: 87742421Syokota if (state->ks_mode != *(int *)arg) { 87842421Syokota atkbd_clear_state(kbd); 87942421Syokota state->ks_mode = *(int *)arg; 88042421Syokota } 88142421Syokota break; 88242421Syokota default: 88342421Syokota splx(s); 88442421Syokota return EINVAL; 88542421Syokota } 88642421Syokota break; 88742421Syokota 88842421Syokota case KDGETLED: /* get keyboard LED */ 88942421Syokota *(int *)arg = KBD_LED_VAL(kbd); 89042421Syokota break; 891162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 892162711Sru defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 893162711Sru case _IO('K', 66): 894162711Sru ival = IOCPARM_IVAL(arg); 895162711Sru arg = (caddr_t)&ival; 896162711Sru /* FALLTHROUGH */ 897162711Sru#endif 89842421Syokota case KDSETLED: /* set keyboard LED */ 89942421Syokota /* NOTE: lock key state in ks_state won't be changed */ 90042421Syokota if (*(int *)arg & ~LOCK_MASK) { 90142421Syokota splx(s); 90242421Syokota return EINVAL; 90342421Syokota } 90442421Syokota i = *(int *)arg; 90542421Syokota /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ 90661004Sache if (state->ks_mode == K_XLATE && 90761004Sache kbd->kb_keymap->n_keys > ALTGR_OFFSET) { 90842421Syokota if (i & ALKED) 90942421Syokota i |= CLKED; 91042421Syokota else 91142421Syokota i &= ~CLKED; 91242421Syokota } 91342421Syokota if (KBD_HAS_DEVICE(kbd)) { 91442421Syokota error = write_kbd(state->kbdc, KBDC_SET_LEDS, 91542421Syokota ledmap[i & LED_MASK]); 91642421Syokota if (error) { 91742421Syokota splx(s); 91842421Syokota return error; 91942421Syokota } 92042421Syokota } 92142421Syokota KBD_LED_VAL(kbd) = *(int *)arg; 92242421Syokota break; 92342421Syokota 92442421Syokota case KDGKBSTATE: /* get lock key state */ 92542421Syokota *(int *)arg = state->ks_state & LOCK_MASK; 92642421Syokota break; 927162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 928162711Sru defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 929162711Sru case _IO('K', 20): 930162711Sru ival = IOCPARM_IVAL(arg); 931162711Sru arg = (caddr_t)&ival; 932162711Sru /* FALLTHROUGH */ 933162711Sru#endif 93442421Syokota case KDSKBSTATE: /* set lock key state */ 93542421Syokota if (*(int *)arg & ~LOCK_MASK) { 93642421Syokota splx(s); 93742421Syokota return EINVAL; 93842421Syokota } 93942421Syokota state->ks_state &= ~LOCK_MASK; 94042421Syokota state->ks_state |= *(int *)arg; 94142421Syokota splx(s); 94242421Syokota /* set LEDs and quit */ 94342421Syokota return atkbd_ioctl(kbd, KDSETLED, arg); 94442421Syokota 94544628Syokota case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ 94642421Syokota splx(s); 94742421Syokota if (!KBD_HAS_DEVICE(kbd)) 94842421Syokota return 0; 94944628Syokota i = typematic(((int *)arg)[0], ((int *)arg)[1]); 95054543Syokota error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i); 95154543Syokota if (error == 0) { 95254543Syokota kbd->kb_delay1 = typematic_delay(i); 95354543Syokota kbd->kb_delay2 = typematic_rate(i); 95454543Syokota } 95554543Syokota return error; 95642421Syokota 957162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 958162711Sru defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 959162711Sru case _IO('K', 67): 960162711Sru ival = IOCPARM_IVAL(arg); 961162711Sru arg = (caddr_t)&ival; 962162711Sru /* FALLTHROUGH */ 963162711Sru#endif 96444628Syokota case KDSETRAD: /* set keyboard repeat rate (old interface) */ 96544628Syokota splx(s); 96644628Syokota if (!KBD_HAS_DEVICE(kbd)) 96744628Syokota return 0; 96854543Syokota error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg); 96954543Syokota if (error == 0) { 97054543Syokota kbd->kb_delay1 = typematic_delay(*(int *)arg); 97154543Syokota kbd->kb_delay2 = typematic_rate(*(int *)arg); 97254543Syokota } 97354543Syokota return error; 97444628Syokota 97542421Syokota case PIO_KEYMAP: /* set keyboard translation table */ 976224126Sed case OPIO_KEYMAP: /* set keyboard translation table (compat) */ 97742421Syokota case PIO_KEYMAPENT: /* set keyboard translation table entry */ 97842421Syokota case PIO_DEADKEYMAP: /* set accent key translation table */ 97942421Syokota state->ks_accents = 0; 980102412Scharnier /* FALLTHROUGH */ 98142421Syokota default: 98242421Syokota splx(s); 98342421Syokota return genkbd_commonioctl(kbd, cmd, arg); 98442421Syokota } 98542421Syokota 98642421Syokota splx(s); 98742421Syokota return 0; 98842421Syokota} 98942421Syokota 99042421Syokota/* lock the access to the keyboard */ 99142421Syokotastatic int 99242421Syokotaatkbd_lock(keyboard_t *kbd, int lock) 99342421Syokota{ 99442421Syokota return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock); 99542421Syokota} 99642421Syokota 99742421Syokota/* clear the internal state of the keyboard */ 99842421Syokotastatic void 99942421Syokotaatkbd_clear_state(keyboard_t *kbd) 100042421Syokota{ 100142421Syokota atkbd_state_t *state; 100242421Syokota 100342421Syokota state = (atkbd_state_t *)kbd->kb_data; 100442421Syokota state->ks_flags = 0; 100544628Syokota state->ks_polling = 0; 100642421Syokota state->ks_state &= LOCK_MASK; /* preserve locking key state */ 100742421Syokota state->ks_accents = 0; 100842421Syokota state->ks_composed_char = 0; 100942421Syokota#if 0 101042421Syokota state->ks_prefix = 0; /* XXX */ 101142421Syokota#endif 101242421Syokota} 101342421Syokota 101442421Syokota/* save the internal state */ 101542421Syokotastatic int 101642421Syokotaatkbd_get_state(keyboard_t *kbd, void *buf, size_t len) 101742421Syokota{ 101842421Syokota if (len == 0) 101942421Syokota return sizeof(atkbd_state_t); 102042421Syokota if (len < sizeof(atkbd_state_t)) 102142421Syokota return -1; 102242421Syokota bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t)); 102342421Syokota return 0; 102442421Syokota} 102542421Syokota 102642421Syokota/* set the internal state */ 102742421Syokotastatic int 102842421Syokotaatkbd_set_state(keyboard_t *kbd, void *buf, size_t len) 102942421Syokota{ 103042421Syokota if (len < sizeof(atkbd_state_t)) 103142421Syokota return ENOMEM; 103242421Syokota if (((atkbd_state_t *)kbd->kb_data)->kbdc 103342421Syokota != ((atkbd_state_t *)buf)->kbdc) 103442421Syokota return ENOMEM; 103542421Syokota bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t)); 103642421Syokota return 0; 103742421Syokota} 103842421Syokota 103944628Syokotastatic int 104044628Syokotaatkbd_poll(keyboard_t *kbd, int on) 104144628Syokota{ 104244628Syokota atkbd_state_t *state; 104344628Syokota int s; 104444628Syokota 104544628Syokota state = (atkbd_state_t *)kbd->kb_data; 104644628Syokota s = spltty(); 104744628Syokota if (on) 104844628Syokota ++state->ks_polling; 104944628Syokota else 105044628Syokota --state->ks_polling; 105144628Syokota splx(s); 105244628Syokota return 0; 105344628Syokota} 105444628Syokota 1055147271Smariusstatic void 1056147271Smariusatkbd_shutdown_final(void *v) 1057147271Smarius{ 1058147271Smarius#ifdef __sparc64__ 1059147271Smarius keyboard_t *kbd = v; 1060147271Smarius KBDC kbdc = ((atkbd_state_t *)kbd->kb_data)->kbdc; 1061147271Smarius 1062147271Smarius /* 1063147271Smarius * Turn off the translation in preparation for handing the keyboard 1064147271Smarius * over to the OFW as the OBP driver doesn't use translation and 1065147271Smarius * also doesn't disable it itself resulting in a broken keymap at 1066147271Smarius * the boot prompt. Also disable the aux port and the interrupts as 1067147271Smarius * the OBP driver doesn't use them, i.e. polls the keyboard. Not 1068147271Smarius * disabling the interrupts doesn't cause real problems but the 1069147271Smarius * responsiveness is a bit better when they are turned off. 1070147271Smarius */ 1071147271Smarius send_kbd_command(kbdc, KBDC_DISABLE_KBD); 1072147271Smarius set_controller_command_byte(kbdc, 1073147271Smarius KBD_AUX_CONTROL_BITS | KBD_KBD_CONTROL_BITS | KBD_TRANSLATION, 1074147271Smarius KBD_DISABLE_AUX_PORT | KBD_DISABLE_KBD_INT | KBD_ENABLE_KBD_PORT); 1075147271Smarius send_kbd_command(kbdc, KBDC_ENABLE_KBD); 1076147271Smarius#endif 1077147271Smarius} 1078147271Smarius 107942421Syokota/* local functions */ 108042421Syokota 108142421Syokotastatic int 1082283219Sroygerset_typematic(keyboard_t *kbd) 108355731Syokota{ 1084283219Sroyger int val, error; 1085283219Sroyger atkbd_state_t *state = kbd->kb_data; 108655731Syokota 1087283219Sroyger val = typematic(DEFAULT_DELAY, DEFAULT_RATE); 1088283219Sroyger error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val); 1089283219Sroyger if (error == 0) { 1090283219Sroyger kbd->kb_delay1 = typematic_delay(val); 1091283219Sroyger kbd->kb_delay2 = typematic_rate(val); 1092283219Sroyger } 1093198251Sjkim 1094283219Sroyger return (error); 109555731Syokota} 109655731Syokota 109755731Syokotastatic int 109842421Syokotasetup_kbd_port(KBDC kbdc, int port, int intr) 109942421Syokota{ 110042421Syokota if (!set_controller_command_byte(kbdc, 110142421Syokota KBD_KBD_CONTROL_BITS, 110242421Syokota ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT) 110342421Syokota | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT))) 110442421Syokota return 1; 110542421Syokota return 0; 110642421Syokota} 110742421Syokota 110842421Syokotastatic int 110942421Syokotaget_kbd_echo(KBDC kbdc) 111042421Syokota{ 111142421Syokota /* enable the keyboard port, but disable the keyboard intr. */ 111242421Syokota if (setup_kbd_port(kbdc, TRUE, FALSE)) 111342421Syokota /* CONTROLLER ERROR: there is very little we can do... */ 111442421Syokota return ENXIO; 111542421Syokota 111642421Syokota /* see if something is present */ 111742421Syokota write_kbd_command(kbdc, KBDC_ECHO); 111842421Syokota if (read_kbd_data(kbdc) != KBD_ECHO) { 111942421Syokota empty_both_buffers(kbdc, 10); 112042421Syokota test_controller(kbdc); 112142421Syokota test_kbd_port(kbdc); 112242421Syokota return ENXIO; 112342421Syokota } 112442421Syokota 112542421Syokota /* enable the keyboard port and intr. */ 112642421Syokota if (setup_kbd_port(kbdc, TRUE, TRUE)) { 112742421Syokota /* 112842421Syokota * CONTROLLER ERROR 112942421Syokota * This is serious; the keyboard intr is left disabled! 113042421Syokota */ 113142421Syokota return ENXIO; 113242421Syokota } 1133245314Simp 113442421Syokota return 0; 113542421Syokota} 113642421Syokota 113742421Syokotastatic int 113842421Syokotaprobe_keyboard(KBDC kbdc, int flags) 113942421Syokota{ 114042421Syokota /* 114142421Syokota * Don't try to print anything in this function. The low-level 114242421Syokota * console may not have been initialized yet... 114342421Syokota */ 114442421Syokota int err; 114542421Syokota int c; 114642421Syokota int m; 114742421Syokota 114842421Syokota if (!kbdc_lock(kbdc, TRUE)) { 114942421Syokota /* driver error? */ 115042421Syokota return ENXIO; 115142421Syokota } 115242421Syokota 115357131Syokota /* temporarily block data transmission from the keyboard */ 115457131Syokota write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); 115557131Syokota 115642421Syokota /* flush any noise in the buffer */ 115757131Syokota empty_both_buffers(kbdc, 100); 115842421Syokota 115942421Syokota /* save the current keyboard controller command byte */ 116042421Syokota m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS; 116142421Syokota c = get_controller_command_byte(kbdc); 116242421Syokota if (c == -1) { 116342421Syokota /* CONTROLLER ERROR */ 116442421Syokota kbdc_set_device_mask(kbdc, m); 116542421Syokota kbdc_lock(kbdc, FALSE); 116642421Syokota return ENXIO; 116742421Syokota } 116842421Syokota 116942421Syokota /* 117042421Syokota * The keyboard may have been screwed up by the boot block. 117142421Syokota * We may just be able to recover from error by testing the controller 117242421Syokota * and the keyboard port. The controller command byte needs to be 117342421Syokota * saved before this recovery operation, as some controllers seem 117442421Syokota * to set the command byte to particular values. 117542421Syokota */ 117642421Syokota test_controller(kbdc); 1177138900Sjhb if (!(flags & KB_CONF_NO_PROBE_TEST)) 1178138900Sjhb test_kbd_port(kbdc); 117942421Syokota 118042421Syokota err = get_kbd_echo(kbdc); 118157902Syokota 118257902Syokota /* 118357902Syokota * Even if the keyboard doesn't seem to be present (err != 0), 118457902Syokota * we shall enable the keyboard port and interrupt so that 118557902Syokota * the driver will be operable when the keyboard is attached 118657902Syokota * to the system later. It is NOT recommended to hot-plug 118757902Syokota * the AT keyboard, but many people do so... 118857902Syokota */ 118957902Syokota kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); 119057902Syokota setup_kbd_port(kbdc, TRUE, TRUE); 119157902Syokota#if 0 119242421Syokota if (err == 0) { 119342421Syokota kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS); 119442421Syokota } else { 119557131Syokota /* try to restore the command byte as before */ 119657131Syokota set_controller_command_byte(kbdc, 0xff, c); 119742421Syokota kbdc_set_device_mask(kbdc, m); 119842421Syokota } 119957902Syokota#endif 120042421Syokota 120142421Syokota kbdc_lock(kbdc, FALSE); 120242421Syokota return err; 120342421Syokota} 120442421Syokota 120542421Syokotastatic int 120642421Syokotainit_keyboard(KBDC kbdc, int *type, int flags) 120742421Syokota{ 120842421Syokota int codeset; 120942421Syokota int id; 121042421Syokota int c; 121142421Syokota 121242421Syokota if (!kbdc_lock(kbdc, TRUE)) { 121342421Syokota /* driver error? */ 121442421Syokota return EIO; 121542421Syokota } 121642421Syokota 121757131Syokota /* temporarily block data transmission from the keyboard */ 121857131Syokota write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT); 121957131Syokota 122042421Syokota /* save the current controller command byte */ 122154382Syokota empty_both_buffers(kbdc, 200); 122242421Syokota c = get_controller_command_byte(kbdc); 122342421Syokota if (c == -1) { 122442421Syokota /* CONTROLLER ERROR */ 122542421Syokota kbdc_lock(kbdc, FALSE); 122642421Syokota printf("atkbd: unable to get the current command byte value.\n"); 122742421Syokota return EIO; 122842421Syokota } 122942421Syokota if (bootverbose) 123042421Syokota printf("atkbd: the current kbd controller command byte %04x\n", 1231245314Simp c); 123242421Syokota#if 0 123342421Syokota /* override the keyboard lock switch */ 123442421Syokota c |= KBD_OVERRIDE_KBD_LOCK; 123542421Syokota#endif 123642421Syokota 123742421Syokota /* enable the keyboard port, but disable the keyboard intr. */ 123842421Syokota if (setup_kbd_port(kbdc, TRUE, FALSE)) { 123942421Syokota /* CONTROLLER ERROR: there is very little we can do... */ 124042421Syokota printf("atkbd: unable to set the command byte.\n"); 124142421Syokota kbdc_lock(kbdc, FALSE); 124242421Syokota return EIO; 124342421Syokota } 124442421Syokota 124542421Syokota /* 124642421Syokota * Check if we have an XT keyboard before we attempt to reset it. 124742421Syokota * The procedure assumes that the keyboard and the controller have 124842421Syokota * been set up properly by BIOS and have not been messed up 124942421Syokota * during the boot process. 125042421Syokota */ 125142421Syokota codeset = -1; 125242421Syokota if (flags & KB_CONF_ALT_SCANCODESET) 125342421Syokota /* the user says there is a XT keyboard */ 125442421Syokota codeset = 1; 125542421Syokota#ifdef KBD_DETECT_XT_KEYBOARD 125642421Syokota else if ((c & KBD_TRANSLATION) == 0) { 125742421Syokota /* SET_SCANCODE_SET is not always supported; ignore error */ 125842421Syokota if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0) 125942421Syokota == KBD_ACK) 126042421Syokota codeset = read_kbd_data(kbdc); 126142421Syokota } 126242421Syokota if (bootverbose) 126342421Syokota printf("atkbd: scancode set %d\n", codeset); 126442421Syokota#endif /* KBD_DETECT_XT_KEYBOARD */ 126542421Syokota 126642421Syokota *type = KB_OTHER; 126742421Syokota id = get_kbd_id(kbdc); 126842421Syokota switch(id) { 126955730Syokota case 0x41ab: /* 101/102/... Enhanced */ 127055730Syokota case 0x83ab: /* ditto */ 127155730Syokota case 0x54ab: /* SpaceSaver */ 127255730Syokota case 0x84ab: /* ditto */ 127355730Syokota#if 0 127455730Syokota case 0x90ab: /* 'G' */ 127555730Syokota case 0x91ab: /* 'P' */ 127655730Syokota case 0x92ab: /* 'A' */ 127755730Syokota#endif 127842421Syokota *type = KB_101; 127942421Syokota break; 128042421Syokota case -1: /* AT 84 keyboard doesn't return ID */ 128142421Syokota *type = KB_84; 128242421Syokota break; 128342421Syokota default: 128442421Syokota break; 128542421Syokota } 128642421Syokota if (bootverbose) 128742421Syokota printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type); 128842421Syokota 128942421Syokota /* reset keyboard hardware */ 129042421Syokota if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) { 129142421Syokota /* 129242421Syokota * KEYBOARD ERROR 129342421Syokota * Keyboard reset may fail either because the keyboard 129442421Syokota * doen't exist, or because the keyboard doesn't pass 129542421Syokota * the self-test, or the keyboard controller on the 129642421Syokota * motherboard and the keyboard somehow fail to shake hands. 129742421Syokota * It is just possible, particularly in the last case, 1298110398Scharnier * that the keyboard controller may be left in a hung state. 129942421Syokota * test_controller() and test_kbd_port() appear to bring 130042421Syokota * the keyboard controller back (I don't know why and how, 130142421Syokota * though.) 130242421Syokota */ 130342421Syokota empty_both_buffers(kbdc, 10); 130442421Syokota test_controller(kbdc); 130542421Syokota test_kbd_port(kbdc); 130642421Syokota /* 130742421Syokota * We could disable the keyboard port and interrupt... but, 130842421Syokota * the keyboard may still exist (see above). 130942421Syokota */ 131042421Syokota set_controller_command_byte(kbdc, 0xff, c); 131142421Syokota kbdc_lock(kbdc, FALSE); 131242421Syokota if (bootverbose) 131342421Syokota printf("atkbd: failed to reset the keyboard.\n"); 131442421Syokota return EIO; 131542421Syokota } 131642421Syokota 131742421Syokota /* 131894275Sphk * Allow us to set the XT_KEYBD flag so that keyboards 131942421Syokota * such as those on the IBM ThinkPad laptop computers can be used 132042421Syokota * with the standard console driver. 132142421Syokota */ 132242421Syokota if (codeset == 1) { 132342421Syokota if (send_kbd_command_and_data(kbdc, 132442421Syokota KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) { 132542421Syokota /* XT kbd doesn't need scan code translation */ 132642421Syokota c &= ~KBD_TRANSLATION; 132742421Syokota } else { 132842421Syokota /* 132942421Syokota * KEYBOARD ERROR 133042421Syokota * The XT kbd isn't usable unless the proper scan 133142421Syokota * code set is selected. 133242421Syokota */ 133342421Syokota set_controller_command_byte(kbdc, 0xff, c); 133442421Syokota kbdc_lock(kbdc, FALSE); 133542421Syokota printf("atkbd: unable to set the XT keyboard mode.\n"); 133642421Syokota return EIO; 133742421Syokota } 133842421Syokota } 133942421Syokota 1340158471Sjhb#if defined(__sparc64__) 134142831Syokota if (send_kbd_command_and_data( 134242831Syokota kbdc, KBDC_SET_SCANCODE_SET, 2) != KBD_ACK) { 134342831Syokota printf("atkbd: can't set translation.\n"); 134442831Syokota } 134542831Syokota c |= KBD_TRANSLATION; 134642831Syokota#endif 134742831Syokota 134842421Syokota /* enable the keyboard port and intr. */ 134942421Syokota if (!set_controller_command_byte(kbdc, 135042421Syokota KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK, 135142421Syokota (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK)) 135242421Syokota | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) { 135342421Syokota /* 135442421Syokota * CONTROLLER ERROR 135542421Syokota * This is serious; we are left with the disabled 135642421Syokota * keyboard intr. 135742421Syokota */ 135842421Syokota set_controller_command_byte(kbdc, 0xff, c); 135942421Syokota kbdc_lock(kbdc, FALSE); 136042421Syokota printf("atkbd: unable to enable the keyboard port and intr.\n"); 136142421Syokota return EIO; 136242421Syokota } 136342421Syokota 136442421Syokota kbdc_lock(kbdc, FALSE); 136542421Syokota return 0; 136642421Syokota} 136742421Syokota 136842421Syokotastatic int 136942421Syokotawrite_kbd(KBDC kbdc, int command, int data) 137042421Syokota{ 1371245314Simp int s; 137242421Syokota 1373245314Simp /* prevent the timeout routine from polling the keyboard */ 1374245314Simp if (!kbdc_lock(kbdc, TRUE)) 1375245314Simp return EBUSY; 137642421Syokota 1377245314Simp /* disable the keyboard and mouse interrupt */ 1378245314Simp s = spltty(); 137942421Syokota#if 0 1380245314Simp c = get_controller_command_byte(kbdc); 1381245314Simp if ((c == -1) 1382245314Simp || !set_controller_command_byte(kbdc, 1383245314Simp kbdc_get_device_mask(kbdc), 1384245314Simp KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT 1385245314Simp | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1386245314Simp /* CONTROLLER ERROR */ 1387245314Simp kbdc_lock(kbdc, FALSE); 1388245314Simp splx(s); 1389245314Simp return EIO; 1390245314Simp } 1391245314Simp /* 1392245314Simp * Now that the keyboard controller is told not to generate 1393245314Simp * the keyboard and mouse interrupts, call `splx()' to allow 1394245314Simp * the other tty interrupts. The clock interrupt may also occur, 1395245314Simp * but the timeout routine (`scrn_timer()') will be blocked 1396245314Simp * by the lock flag set via `kbdc_lock()' 1397245314Simp */ 139842421Syokota splx(s); 139942421Syokota#endif 1400245314Simp if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK) 1401245314Simp send_kbd_command(kbdc, KBDC_ENABLE_KBD); 140242421Syokota#if 0 1403245314Simp /* restore the interrupts */ 1404245314Simp if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc), 140542421Syokota c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 1406245314Simp /* CONTROLLER ERROR */ 1407245314Simp } 140842421Syokota#else 1409245314Simp splx(s); 141042421Syokota#endif 1411245314Simp kbdc_lock(kbdc, FALSE); 141242421Syokota 1413245314Simp return 0; 141442421Syokota} 141542421Syokota 141642421Syokotastatic int 141742421Syokotaget_kbd_id(KBDC kbdc) 141842421Syokota{ 141942421Syokota int id1, id2; 142042421Syokota 142142421Syokota empty_both_buffers(kbdc, 10); 142242421Syokota id1 = id2 = -1; 142342421Syokota if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK) 142442421Syokota return -1; 142542421Syokota 142642421Syokota DELAY(10000); /* 10 msec delay */ 142742421Syokota id1 = read_kbd_data(kbdc); 142842421Syokota if (id1 != -1) 142942421Syokota id2 = read_kbd_data(kbdc); 143042421Syokota 143142421Syokota if ((id1 == -1) || (id2 == -1)) { 143242421Syokota empty_both_buffers(kbdc, 10); 143342421Syokota test_controller(kbdc); 143442421Syokota test_kbd_port(kbdc); 143542421Syokota return -1; 143642421Syokota } 143742421Syokota return ((id2 << 8) | id1); 143842421Syokota} 143942421Syokota 144054543Syokotastatic int delays[] = { 250, 500, 750, 1000 }; 144154543Syokotastatic int rates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 144254543Syokota 68, 76, 84, 92, 100, 110, 118, 126, 144354543Syokota 136, 152, 168, 184, 200, 220, 236, 252, 144454543Syokota 272, 304, 336, 368, 400, 440, 472, 504 }; 144554543Syokota 144644628Syokotastatic int 144754543Syokotatypematic_delay(int i) 144854543Syokota{ 144954543Syokota return delays[(i >> 5) & 3]; 145054543Syokota} 145154543Syokota 145254543Syokotastatic int 145354543Syokotatypematic_rate(int i) 145454543Syokota{ 145554543Syokota return rates[i & 0x1f]; 145654543Syokota} 145754543Syokota 145854543Syokotastatic int 145944628Syokotatypematic(int delay, int rate) 146044628Syokota{ 146144628Syokota int value; 146244628Syokota int i; 146344628Syokota 146444628Syokota for (i = sizeof(delays)/sizeof(delays[0]) - 1; i > 0; --i) { 146544628Syokota if (delay >= delays[i]) 146644628Syokota break; 146744628Syokota } 146844628Syokota value = i << 5; 146944628Syokota for (i = sizeof(rates)/sizeof(rates[0]) - 1; i > 0; --i) { 147044628Syokota if (rate >= rates[i]) 147144628Syokota break; 147244628Syokota } 147344628Syokota value |= i; 147444628Syokota return value; 147544628Syokota} 1476