The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/atkbdc/atkbd.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer as
   12  *    the first lines of this file unmodified.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  *
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #include "opt_kbd.h"
   34 #include "opt_atkbd.h"
   35 #include "opt_evdev.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/kernel.h>
   40 #include <sys/bus.h>
   41 #include <sys/eventhandler.h>
   42 #include <sys/proc.h>
   43 #include <sys/limits.h>
   44 #include <sys/malloc.h>
   45 #include <sys/sysctl.h>
   46 
   47 #include <machine/bus.h>
   48 #include <machine/resource.h>
   49 
   50 #include <sys/kbio.h>
   51 #include <dev/kbd/kbdreg.h>
   52 #include <dev/atkbdc/atkbdreg.h>
   53 #include <dev/atkbdc/atkbdcreg.h>
   54 
   55 #ifdef EVDEV_SUPPORT
   56 #include <dev/evdev/evdev.h>
   57 #include <dev/evdev/input.h>
   58 #endif
   59 
   60 typedef struct atkbd_state {
   61         KBDC            kbdc;           /* keyboard controller */
   62         int             ks_mode;        /* input mode (K_XLATE,K_RAW,K_CODE) */
   63         int             ks_flags;       /* flags */
   64 #define COMPOSE         (1 << 0)
   65         int             ks_polling;
   66         int             ks_state;       /* shift/lock key state */
   67         int             ks_accents;     /* accent key index (> 0) */
   68         u_int           ks_composed_char; /* composed char code (> 0) */
   69         u_char          ks_prefix;      /* AT scan code prefix */
   70         struct callout  ks_timer;
   71 #ifdef EVDEV_SUPPORT
   72         struct evdev_dev *ks_evdev;
   73         int             ks_evdev_state;
   74 #endif
   75 } atkbd_state_t;
   76 
   77 static SYSCTL_NODE(_hw, OID_AUTO, atkbd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
   78     "AT keyboard");
   79 
   80 static int atkbdhz = 0;
   81 SYSCTL_INT(_hw_atkbd, OID_AUTO, hz, CTLFLAG_RWTUN, &atkbdhz, 0,
   82     "Polling frequency (in hz)");
   83 
   84 static void             atkbd_timeout(void *arg);
   85 static int              atkbd_reset(KBDC kbdc, int flags, int c);
   86 
   87 #define HAS_QUIRK(p, q)         (((atkbdc_softc_t *)(p))->quirks & q)
   88 #define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED)
   89 
   90 #define DEFAULT_DELAY           0x1  /* 500ms */
   91 #define DEFAULT_RATE            0x10 /* 14Hz */
   92 
   93 #ifdef EVDEV_SUPPORT
   94 #define PS2_KEYBOARD_VENDOR     1
   95 #define PS2_KEYBOARD_PRODUCT    1
   96 #endif
   97 
   98 int
   99 atkbd_probe_unit(device_t dev, int irq, int flags)
  100 {
  101         keyboard_switch_t *sw;
  102         int args[2];
  103         int error;
  104 
  105         sw = kbd_get_switch(ATKBD_DRIVER_NAME);
  106         if (sw == NULL)
  107                 return ENXIO;
  108 
  109         args[0] = device_get_unit(device_get_parent(dev));
  110         args[1] = irq;
  111         error = (*sw->probe)(device_get_unit(dev), args, flags);
  112         if (error)
  113                 return error;
  114         return 0;
  115 }
  116 
  117 int
  118 atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags)
  119 {
  120         keyboard_switch_t *sw;
  121         atkbd_state_t *state;
  122         int args[2];
  123         int error;
  124         int unit;
  125 
  126         sw = kbd_get_switch(ATKBD_DRIVER_NAME);
  127         if (sw == NULL)
  128                 return ENXIO;
  129 
  130         /* reset, initialize and enable the device */
  131         unit = device_get_unit(dev);
  132         args[0] = device_get_unit(device_get_parent(dev));
  133         args[1] = irq;
  134         *kbd = NULL;
  135         error = (*sw->probe)(unit, args, flags);
  136         if (error)
  137                 return error;
  138         error = (*sw->init)(unit, kbd, args, flags);
  139         if (error)
  140                 return error;
  141         (*sw->enable)(*kbd);
  142 
  143 #ifdef KBD_INSTALL_CDEV
  144         /* attach a virtual keyboard cdev */
  145         error = kbd_attach(*kbd);
  146         if (error)
  147                 return error;
  148 #endif
  149 
  150         /*
  151          * This is a kludge to compensate for lost keyboard interrupts.
  152          * A similar code used to be in syscons. See below. XXX
  153          */
  154         state = (atkbd_state_t *)(*kbd)->kb_data;
  155         callout_init(&state->ks_timer, 0);
  156         atkbd_timeout(*kbd);
  157 
  158         if (bootverbose)
  159                 (*sw->diag)(*kbd, bootverbose);
  160 
  161         return 0;
  162 }
  163 
  164 static void
  165 atkbd_timeout(void *arg)
  166 {
  167         atkbd_state_t *state;
  168         keyboard_t *kbd;
  169         int s;
  170 
  171         /*
  172          * The original text of the following comments are extracted 
  173          * from syscons.c (1.287)
  174          * 
  175          * With release 2.1 of the Xaccel server, the keyboard is left
  176          * hanging pretty often. Apparently an interrupt from the
  177          * keyboard is lost, and I don't know why (yet).
  178          * This ugly hack calls the low-level interrupt routine if input
  179          * is ready for the keyboard and conveniently hides the problem. XXX
  180          *
  181          * Try removing anything stuck in the keyboard controller; whether
  182          * it's a keyboard scan code or mouse data. The low-level
  183          * interrupt routine doesn't read the mouse data directly, 
  184          * but the keyboard controller driver will, as a side effect.
  185          */
  186         /*
  187          * And here is bde's original comment about this:
  188          *
  189          * This is necessary to handle edge triggered interrupts - if we
  190          * returned when our IRQ is high due to unserviced input, then there
  191          * would be no more keyboard IRQs until the keyboard is reset by
  192          * external powers.
  193          *
  194          * The keyboard apparently unwedges the irq in most cases.
  195          */
  196         s = spltty();
  197         kbd = (keyboard_t *)arg;
  198         if (kbdd_lock(kbd, TRUE)) {
  199                 /*
  200                  * We have seen the lock flag is not set. Let's reset
  201                  * the flag early, otherwise the LED update routine fails
  202                  * which may want the lock during the interrupt routine.
  203                  */
  204                 kbdd_lock(kbd, FALSE);
  205                 if (kbdd_check_char(kbd))
  206                         kbdd_intr(kbd, NULL);
  207         }
  208         splx(s);
  209         if (atkbdhz > 0) {
  210                 state = (atkbd_state_t *)kbd->kb_data;
  211                 callout_reset_sbt(&state->ks_timer, SBT_1S / atkbdhz, 0,
  212                     atkbd_timeout, arg, C_PREL(1));
  213         }
  214 }
  215 
  216 /* LOW-LEVEL */
  217 
  218 #define ATKBD_DEFAULT   0
  219 
  220 /* keyboard driver declaration */
  221 static int              atkbd_configure(int flags);
  222 static kbd_probe_t      atkbd_probe;
  223 static kbd_init_t       atkbd_init;
  224 static kbd_term_t       atkbd_term;
  225 static kbd_intr_t       atkbd_intr;
  226 static kbd_test_if_t    atkbd_test_if;
  227 static kbd_enable_t     atkbd_enable;
  228 static kbd_disable_t    atkbd_disable;
  229 static kbd_read_t       atkbd_read;
  230 static kbd_check_t      atkbd_check;
  231 static kbd_read_char_t  atkbd_read_char;
  232 static kbd_check_char_t atkbd_check_char;
  233 static kbd_ioctl_t      atkbd_ioctl;
  234 static kbd_lock_t       atkbd_lock;
  235 static kbd_clear_state_t atkbd_clear_state;
  236 static kbd_get_state_t  atkbd_get_state;
  237 static kbd_set_state_t  atkbd_set_state;
  238 static kbd_poll_mode_t  atkbd_poll;
  239 
  240 static keyboard_switch_t atkbdsw = {
  241         .probe =        atkbd_probe,
  242         .init =         atkbd_init,
  243         .term =         atkbd_term,
  244         .intr =         atkbd_intr,
  245         .test_if =      atkbd_test_if,
  246         .enable =       atkbd_enable,
  247         .disable =      atkbd_disable,
  248         .read =         atkbd_read,
  249         .check =        atkbd_check,
  250         .read_char =    atkbd_read_char,
  251         .check_char =   atkbd_check_char,
  252         .ioctl =        atkbd_ioctl,
  253         .lock =         atkbd_lock,
  254         .clear_state =  atkbd_clear_state,
  255         .get_state =    atkbd_get_state,
  256         .set_state =    atkbd_set_state,
  257         .poll =         atkbd_poll,
  258 };
  259 
  260 KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
  261 
  262 /* local functions */
  263 static int              set_typematic(keyboard_t *kbd);
  264 static int              setup_kbd_port(KBDC kbdc, int port, int intr);
  265 static int              get_kbd_echo(KBDC kbdc);
  266 static int              probe_keyboard(KBDC kbdc, int flags);
  267 static int              init_keyboard(KBDC kbdc, int *type, int flags);
  268 static int              write_kbd(KBDC kbdc, int command, int data);
  269 static int              get_kbd_id(KBDC kbdc);
  270 static int              typematic(int delay, int rate);
  271 static int              typematic_delay(int delay);
  272 static int              typematic_rate(int rate);
  273 
  274 #ifdef EVDEV_SUPPORT
  275 static evdev_event_t atkbd_ev_event;
  276 
  277 static const struct evdev_methods atkbd_evdev_methods = {
  278         .ev_event = atkbd_ev_event,
  279 };
  280 #endif
  281 
  282 /* local variables */
  283 
  284 /* the initial key map, accent map and fkey strings */
  285 #ifdef ATKBD_DFLT_KEYMAP
  286 #define KBD_DFLT_KEYMAP
  287 #include "atkbdmap.h"
  288 #endif
  289 #include <dev/kbd/kbdtables.h>
  290 
  291 /* structures for the default keyboard */
  292 static keyboard_t       default_kbd;
  293 static atkbd_state_t    default_kbd_state;
  294 static keymap_t         default_keymap;
  295 static accentmap_t      default_accentmap;
  296 static fkeytab_t        default_fkeytab[NUM_FKEYS];
  297 
  298 /* 
  299  * The back door to the keyboard driver!
  300  * This function is called by the console driver, via the kbdio module,
  301  * to tickle keyboard drivers when the low-level console is being initialized.
  302  * Almost nothing in the kernel has been initialied yet.  Try to probe
  303  * keyboards if possible.
  304  * NOTE: because of the way the low-level console is initialized, this routine
  305  * may be called more than once!!
  306  */
  307 static int
  308 atkbd_configure(int flags)
  309 {
  310         keyboard_t *kbd;
  311         int arg[2];
  312         int i;
  313 
  314         /*
  315          * Probe the keyboard controller, if not present or if the driver
  316          * is disabled, unregister the keyboard if any.
  317          */
  318         if (atkbdc_configure() != 0 ||
  319             resource_disabled("atkbd", ATKBD_DEFAULT)) {
  320                 i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
  321                 if (i >= 0) {
  322                         kbd = kbd_get_keyboard(i);
  323                         kbd_unregister(kbd);
  324                         kbd->kb_flags &= ~KB_REGISTERED;
  325                 }
  326                 return 0;
  327         }
  328 
  329         /* XXX: a kludge to obtain the device configuration flags */
  330         if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
  331                 flags |= i;
  332 
  333         /* probe the default keyboard */
  334         arg[0] = -1;
  335         arg[1] = -1;
  336         kbd = NULL;
  337         if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
  338                 return 0;
  339         if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
  340                 return 0;
  341 
  342         /* return the number of found keyboards */
  343         return 1;
  344 }
  345 
  346 /* low-level functions */
  347 
  348 /* detect a keyboard */
  349 static int
  350 atkbd_probe(int unit, void *arg, int flags)
  351 {
  352         KBDC kbdc;
  353         int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
  354 
  355         /* XXX */
  356         if (unit == ATKBD_DEFAULT) {
  357                 if (KBD_IS_PROBED(&default_kbd))
  358                         return 0;
  359         }
  360 
  361         kbdc = atkbdc_open(data[0]);
  362         if (kbdc == NULL)
  363                 return ENXIO;
  364         if (probe_keyboard(kbdc, flags)) {
  365                 if (flags & KB_CONF_FAIL_IF_NO_KBD)
  366                         return ENXIO;
  367         }
  368         return 0;
  369 }
  370 
  371 /* reset and initialize the device */
  372 static int
  373 atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
  374 {
  375         keyboard_t *kbd;
  376         atkbd_state_t *state;
  377         keymap_t *keymap;
  378         accentmap_t *accmap;
  379         fkeytab_t *fkeymap;
  380         int fkeymap_size;
  381         int delay[2];
  382         int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
  383         int error, needfree;
  384 #ifdef EVDEV_SUPPORT
  385         struct evdev_dev *evdev;
  386         char phys_loc[8];
  387 #endif
  388 
  389         /* XXX */
  390         if (unit == ATKBD_DEFAULT) {
  391                 *kbdp = kbd = &default_kbd;
  392                 if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
  393                         return 0;
  394                 state = &default_kbd_state;
  395                 keymap = &default_keymap;
  396                 accmap = &default_accentmap;
  397                 fkeymap = default_fkeytab;
  398                 fkeymap_size = nitems(default_fkeytab);
  399                 needfree = 0;
  400         } else if (*kbdp == NULL) {
  401                 *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
  402                 state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
  403                 /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
  404                 keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
  405                 accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
  406                 fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
  407                 fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
  408                 needfree = 1;
  409                 if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
  410                      || (accmap == NULL) || (fkeymap == NULL)) {
  411                         error = ENOMEM;
  412                         goto bad;
  413                 }
  414         } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
  415                 return 0;
  416         } else {
  417                 kbd = *kbdp;
  418                 state = (atkbd_state_t *)kbd->kb_data;
  419                 bzero(state, sizeof(*state));
  420                 keymap = kbd->kb_keymap;
  421                 accmap = kbd->kb_accentmap;
  422                 fkeymap = kbd->kb_fkeytab;
  423                 fkeymap_size = kbd->kb_fkeytab_size;
  424                 needfree = 0;
  425         }
  426 
  427         if (!KBD_IS_PROBED(kbd)) {
  428                 state->kbdc = atkbdc_open(data[0]);
  429                 if (state->kbdc == NULL) {
  430                         error = ENXIO;
  431                         goto bad;
  432                 }
  433                 kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
  434                                 0, 0);
  435                 bcopy(&key_map, keymap, sizeof(key_map));
  436                 bcopy(&accent_map, accmap, sizeof(accent_map));
  437                 bcopy(fkey_tab, fkeymap,
  438                     imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab)));
  439                 kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
  440                 kbd->kb_data = (void *)state;
  441 
  442                 if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
  443                         if (flags & KB_CONF_FAIL_IF_NO_KBD) {
  444                                 error = ENXIO;
  445                                 goto bad;
  446                         }
  447                 } else {
  448                         KBD_FOUND_DEVICE(kbd);
  449                 }
  450                 atkbd_clear_state(kbd);
  451                 state->ks_mode = K_XLATE;
  452                 /* 
  453                  * FIXME: set the initial value for lock keys in ks_state
  454                  * according to the BIOS data?
  455                  */
  456                 KBD_PROBE_DONE(kbd);
  457         }
  458         if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
  459                 kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
  460                 if (KBD_HAS_DEVICE(kbd)
  461                     && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
  462                     && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
  463                         kbd_unregister(kbd);
  464                         error = ENXIO;
  465                         goto bad;
  466                 }
  467                 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
  468                 set_typematic(kbd);
  469                 delay[0] = kbd->kb_delay1;
  470                 delay[1] = kbd->kb_delay2;
  471                 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
  472 
  473 #ifdef EVDEV_SUPPORT
  474                 /* register as evdev provider on first init */
  475                 if (state->ks_evdev == NULL) {
  476                         snprintf(phys_loc, sizeof(phys_loc), "atkbd%d", unit);
  477                         evdev = evdev_alloc();
  478                         evdev_set_name(evdev, "AT keyboard");
  479                         evdev_set_phys(evdev, phys_loc);
  480                         evdev_set_id(evdev, BUS_I8042, PS2_KEYBOARD_VENDOR,
  481                             PS2_KEYBOARD_PRODUCT, 0);
  482                         evdev_set_methods(evdev, kbd, &atkbd_evdev_methods);
  483                         evdev_support_event(evdev, EV_SYN);
  484                         evdev_support_event(evdev, EV_KEY);
  485                         evdev_support_event(evdev, EV_LED);
  486                         evdev_support_event(evdev, EV_REP);
  487                         evdev_support_all_known_keys(evdev);
  488                         evdev_support_led(evdev, LED_NUML);
  489                         evdev_support_led(evdev, LED_CAPSL);
  490                         evdev_support_led(evdev, LED_SCROLLL);
  491 
  492                         if (evdev_register_mtx(evdev, &Giant))
  493                                 evdev_free(evdev);
  494                         else
  495                                 state->ks_evdev = evdev;
  496                         state->ks_evdev_state = 0;
  497                 }
  498 #endif
  499 
  500                 KBD_INIT_DONE(kbd);
  501         }
  502         if (!KBD_IS_CONFIGURED(kbd)) {
  503                 if (kbd_register(kbd) < 0) {
  504                         error = ENXIO;
  505                         goto bad;
  506                 }
  507                 KBD_CONFIG_DONE(kbd);
  508         }
  509 
  510         return 0;
  511 bad:
  512         if (needfree) {
  513                 if (state != NULL)
  514                         free(state, M_DEVBUF);
  515                 if (keymap != NULL)
  516                         free(keymap, M_DEVBUF);
  517                 if (accmap != NULL)
  518                         free(accmap, M_DEVBUF);
  519                 if (fkeymap != NULL)
  520                         free(fkeymap, M_DEVBUF);
  521                 if (kbd != NULL) {
  522                         free(kbd, M_DEVBUF);
  523                         *kbdp = NULL;   /* insure ref doesn't leak to caller */
  524                 }
  525         }
  526         return error;
  527 }
  528 
  529 /* finish using this keyboard */
  530 static int
  531 atkbd_term(keyboard_t *kbd)
  532 {
  533         atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
  534 
  535         kbd_unregister(kbd);
  536         callout_drain(&state->ks_timer);
  537         return 0;
  538 }
  539 
  540 /* keyboard interrupt routine */
  541 static int
  542 atkbd_intr(keyboard_t *kbd, void *arg)
  543 {
  544         atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
  545         int delay[2];
  546         int c;
  547 
  548         if (!KBD_HAS_DEVICE(kbd)) {
  549                 /*
  550                  * The keyboard was not detected before;
  551                  * it must have been reconnected!
  552                  */
  553                 init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config);
  554                 KBD_FOUND_DEVICE(kbd);
  555                 atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
  556                 set_typematic(kbd);
  557                 delay[0] = kbd->kb_delay1;
  558                 delay[1] = kbd->kb_delay2;
  559                 atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
  560         }
  561 
  562         if (state->ks_polling)
  563                 return 0;
  564 
  565         if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
  566                 /* let the callback function to process the input */
  567                 (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
  568                                             kbd->kb_callback.kc_arg);
  569         } else {
  570                 /* read and discard the input; no one is waiting for input */
  571                 do {
  572                         c = atkbd_read_char(kbd, FALSE);
  573                 } while (c != NOKEY);
  574         }
  575         return 0;
  576 }
  577 
  578 /* test the interface to the device */
  579 static int
  580 atkbd_test_if(keyboard_t *kbd)
  581 {
  582         int error;
  583         int s;
  584 
  585         error = 0;
  586         empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
  587         s = spltty();
  588         if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
  589                 error = EIO;
  590         else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
  591                 error = EIO;
  592         splx(s);
  593 
  594         return error;
  595 }
  596 
  597 /* 
  598  * Enable the access to the device; until this function is called,
  599  * the client cannot read from the keyboard.
  600  */
  601 static int
  602 atkbd_enable(keyboard_t *kbd)
  603 {
  604         int s;
  605 
  606         s = spltty();
  607         KBD_ACTIVATE(kbd);
  608         splx(s);
  609         return 0;
  610 }
  611 
  612 /* disallow the access to the device */
  613 static int
  614 atkbd_disable(keyboard_t *kbd)
  615 {
  616         int s;
  617 
  618         s = spltty();
  619         KBD_DEACTIVATE(kbd);
  620         splx(s);
  621         return 0;
  622 }
  623 
  624 /* read one byte from the keyboard if it's allowed */
  625 static int
  626 atkbd_read(keyboard_t *kbd, int wait)
  627 {
  628         int c;
  629 
  630         if (wait)
  631                 c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
  632         else
  633                 c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
  634         if (c != -1)
  635                 ++kbd->kb_count;
  636         return (KBD_IS_ACTIVE(kbd) ? c : -1);
  637 }
  638 
  639 /* check if data is waiting */
  640 static int
  641 atkbd_check(keyboard_t *kbd)
  642 {
  643         if (!KBD_IS_ACTIVE(kbd))
  644                 return FALSE;
  645         return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
  646 }
  647 
  648 /* read char from the keyboard */
  649 static u_int
  650 atkbd_read_char(keyboard_t *kbd, int wait)
  651 {
  652         atkbd_state_t *state;
  653         u_int action;
  654         int scancode;
  655         int keycode;
  656 
  657         state = (atkbd_state_t *)kbd->kb_data;
  658 next_code:
  659         /* do we have a composed char to return? */
  660         if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
  661                 action = state->ks_composed_char;
  662                 state->ks_composed_char = 0;
  663                 if (action > UCHAR_MAX)
  664                         return ERRKEY;
  665                 return action;
  666         }
  667 
  668         /* see if there is something in the keyboard port */
  669         if (wait) {
  670                 do {
  671                         scancode = read_kbd_data(state->kbdc);
  672                 } while (scancode == -1);
  673         } else {
  674                 scancode = read_kbd_data_no_wait(state->kbdc);
  675                 if (scancode == -1)
  676                         return NOKEY;
  677         }
  678         ++kbd->kb_count;
  679 
  680 #if KBDIO_DEBUG >= 10
  681         printf("atkbd_read_char(): scancode:0x%x\n", scancode);
  682 #endif
  683 
  684 #ifdef EVDEV_SUPPORT
  685         /* push evdev event */
  686         if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && state->ks_evdev != NULL) {
  687                 /* "hancha" and "han/yong" korean keys handling */
  688                 if (state->ks_evdev_state == 0 &&
  689                     (scancode == 0xF1 || scancode == 0xF2)) {
  690                         keycode = evdev_scancode2key(&state->ks_evdev_state,
  691                                 scancode & 0x7F);
  692                         evdev_push_event(state->ks_evdev, EV_KEY,
  693                             (uint16_t)keycode, 1);
  694                         evdev_sync(state->ks_evdev);
  695                 }
  696 
  697                 keycode = evdev_scancode2key(&state->ks_evdev_state,
  698                     scancode);
  699 
  700                 if (keycode != KEY_RESERVED) {
  701                         evdev_push_event(state->ks_evdev, EV_KEY,
  702                             (uint16_t)keycode, scancode & 0x80 ? 0 : 1);
  703                         evdev_sync(state->ks_evdev);
  704                 }
  705         }
  706 
  707         if (state->ks_evdev != NULL && evdev_is_grabbed(state->ks_evdev))
  708                 return (NOKEY);
  709 #endif
  710 
  711         /* return the byte as is for the K_RAW mode */
  712         if (state->ks_mode == K_RAW)
  713                 return scancode;
  714 
  715         /* translate the scan code into a keycode */
  716         keycode = scancode & 0x7F;
  717         switch (state->ks_prefix) {
  718         case 0x00:      /* normal scancode */
  719                 switch(scancode) {
  720                 case 0xB8:      /* left alt (compose key) released */
  721                         if (state->ks_flags & COMPOSE) {
  722                                 state->ks_flags &= ~COMPOSE;
  723                                 if (state->ks_composed_char > UCHAR_MAX)
  724                                         state->ks_composed_char = 0;
  725                         }
  726                         break;
  727                 case 0x38:      /* left alt (compose key) pressed */
  728                         if (!(state->ks_flags & COMPOSE)) {
  729                                 state->ks_flags |= COMPOSE;
  730                                 state->ks_composed_char = 0;
  731                         }
  732                         break;
  733                 case 0xE0:
  734                 case 0xE1:
  735                         state->ks_prefix = scancode;
  736                         goto next_code;
  737                 }
  738                 break;
  739         case 0xE0:              /* 0xE0 prefix */
  740                 state->ks_prefix = 0;
  741                 switch (keycode) {
  742                 case 0x1C:      /* right enter key */
  743                         keycode = 0x59;
  744                         break;
  745                 case 0x1D:      /* right ctrl key */
  746                         keycode = 0x5A;
  747                         break;
  748                 case 0x35:      /* keypad divide key */
  749                         keycode = 0x5B;
  750                         break;
  751                 case 0x37:      /* print scrn key */
  752                         keycode = 0x5C;
  753                         break;
  754                 case 0x38:      /* right alt key (alt gr) */
  755                         keycode = 0x5D;
  756                         break;
  757                 case 0x46:      /* ctrl-pause/break on AT 101 (see below) */
  758                         keycode = 0x68;
  759                         break;
  760                 case 0x47:      /* grey home key */
  761                         keycode = 0x5E;
  762                         break;
  763                 case 0x48:      /* grey up arrow key */
  764                         keycode = 0x5F;
  765                         break;
  766                 case 0x49:      /* grey page up key */
  767                         keycode = 0x60;
  768                         break;
  769                 case 0x4B:      /* grey left arrow key */
  770                         keycode = 0x61;
  771                         break;
  772                 case 0x4D:      /* grey right arrow key */
  773                         keycode = 0x62;
  774                         break;
  775                 case 0x4F:      /* grey end key */
  776                         keycode = 0x63;
  777                         break;
  778                 case 0x50:      /* grey down arrow key */
  779                         keycode = 0x64;
  780                         break;
  781                 case 0x51:      /* grey page down key */
  782                         keycode = 0x65;
  783                         break;
  784                 case 0x52:      /* grey insert key */
  785                         keycode = 0x66;
  786                         break;
  787                 case 0x53:      /* grey delete key */
  788                         keycode = 0x67;
  789                         break;
  790                         /* the following 3 are only used on the MS "Natural" keyboard */
  791                 case 0x5b:      /* left Window key */
  792                         keycode = 0x69;
  793                         break;
  794                 case 0x5c:      /* right Window key */
  795                         keycode = 0x6a;
  796                         break;
  797                 case 0x5d:      /* menu key */
  798                         keycode = 0x6b;
  799                         break;
  800                 case 0x5e:      /* power key */
  801                         keycode = 0x6d;
  802                         break;
  803                 case 0x5f:      /* sleep key */
  804                         keycode = 0x6e;
  805                         break;
  806                 case 0x63:      /* wake key */
  807                         keycode = 0x6f;
  808                         break;
  809                 default:        /* ignore everything else */
  810                         goto next_code;
  811                 }
  812                 break;
  813         case 0xE1:      /* 0xE1 prefix */
  814                 /* 
  815                  * The pause/break key on the 101 keyboard produces:
  816                  * E1-1D-45 E1-9D-C5
  817                  * Ctrl-pause/break produces:
  818                  * E0-46 E0-C6 (See above.)
  819                  */
  820                 state->ks_prefix = 0;
  821                 if (keycode == 0x1D)
  822                         state->ks_prefix = 0x1D;
  823                 goto next_code;
  824                 /* NOT REACHED */
  825         case 0x1D:      /* pause / break */
  826                 state->ks_prefix = 0;
  827                 if (keycode != 0x45)
  828                         goto next_code;
  829                 keycode = 0x68;
  830                 break;
  831         }
  832 
  833         if (kbd->kb_type == KB_84) {
  834                 switch (keycode) {
  835                 case 0x37:      /* *(numpad)/print screen */
  836                         if (state->ks_flags & SHIFTS)
  837                                 keycode = 0x5c; /* print screen */
  838                         break;
  839                 case 0x45:      /* num lock/pause */
  840                         if (state->ks_flags & CTLS)
  841                                 keycode = 0x68; /* pause */
  842                         break;
  843                 case 0x46:      /* scroll lock/break */
  844                         if (state->ks_flags & CTLS)
  845                                 keycode = 0x6c; /* break */
  846                         break;
  847                 }
  848         } else if (kbd->kb_type == KB_101) {
  849                 switch (keycode) {
  850                 case 0x5c:      /* print screen */
  851                         if (state->ks_flags & ALTS)
  852                                 keycode = 0x54; /* sysrq */
  853                         break;
  854                 case 0x68:      /* pause/break */
  855                         if (state->ks_flags & CTLS)
  856                                 keycode = 0x6c; /* break */
  857                         break;
  858                 }
  859         }
  860 
  861         /* return the key code in the K_CODE mode */
  862         if (state->ks_mode == K_CODE)
  863                 return (keycode | (scancode & 0x80));
  864 
  865         /* compose a character code */
  866         if (state->ks_flags & COMPOSE) {
  867                 switch (keycode | (scancode & 0x80)) {
  868                 /* key pressed, process it */
  869                 case 0x47: case 0x48: case 0x49:        /* keypad 7,8,9 */
  870                         state->ks_composed_char *= 10;
  871                         state->ks_composed_char += keycode - 0x40;
  872                         if (state->ks_composed_char > UCHAR_MAX)
  873                                 return ERRKEY;
  874                         goto next_code;
  875                 case 0x4B: case 0x4C: case 0x4D:        /* keypad 4,5,6 */
  876                         state->ks_composed_char *= 10;
  877                         state->ks_composed_char += keycode - 0x47;
  878                         if (state->ks_composed_char > UCHAR_MAX)
  879                                 return ERRKEY;
  880                         goto next_code;
  881                 case 0x4F: case 0x50: case 0x51:        /* keypad 1,2,3 */
  882                         state->ks_composed_char *= 10;
  883                         state->ks_composed_char += keycode - 0x4E;
  884                         if (state->ks_composed_char > UCHAR_MAX)
  885                                 return ERRKEY;
  886                         goto next_code;
  887                 case 0x52:                              /* keypad 0 */
  888                         state->ks_composed_char *= 10;
  889                         if (state->ks_composed_char > UCHAR_MAX)
  890                                 return ERRKEY;
  891                         goto next_code;
  892 
  893                 /* key released, no interest here */
  894                 case 0xC7: case 0xC8: case 0xC9:        /* keypad 7,8,9 */
  895                 case 0xCB: case 0xCC: case 0xCD:        /* keypad 4,5,6 */
  896                 case 0xCF: case 0xD0: case 0xD1:        /* keypad 1,2,3 */
  897                 case 0xD2:                              /* keypad 0 */
  898                         goto next_code;
  899 
  900                 case 0x38:                              /* left alt key */
  901                         break;
  902 
  903                 default:
  904                         if (state->ks_composed_char > 0) {
  905                                 state->ks_flags &= ~COMPOSE;
  906                                 state->ks_composed_char = 0;
  907                                 return ERRKEY;
  908                         }
  909                         break;
  910                 }
  911         }
  912 
  913         /* keycode to key action */
  914         action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
  915                                   &state->ks_state, &state->ks_accents);
  916         if (action == NOKEY)
  917                 goto next_code;
  918         else
  919                 return action;
  920 }
  921 
  922 /* check if char is waiting */
  923 static int
  924 atkbd_check_char(keyboard_t *kbd)
  925 {
  926         atkbd_state_t *state;
  927 
  928         if (!KBD_IS_ACTIVE(kbd))
  929                 return FALSE;
  930         state = (atkbd_state_t *)kbd->kb_data;
  931         if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
  932                 return TRUE;
  933         return kbdc_data_ready(state->kbdc);
  934 }
  935 
  936 /* some useful control functions */
  937 static int
  938 atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
  939 {
  940         /* translate LED_XXX bits into the device specific bits */
  941         static u_char ledmap[8] = {
  942                 0, 4, 2, 6, 1, 5, 3, 7,
  943         };
  944         atkbd_state_t *state = kbd->kb_data;
  945         int error;
  946         int s;
  947         int i;
  948 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
  949     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
  950         int ival;
  951 #endif
  952 
  953         s = spltty();
  954         switch (cmd) {
  955         case KDGKBMODE:         /* get keyboard mode */
  956                 *(int *)arg = state->ks_mode;
  957                 break;
  958 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
  959     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
  960         case _IO('K', 7):
  961                 ival = IOCPARM_IVAL(arg);
  962                 arg = (caddr_t)&ival;
  963                 /* FALLTHROUGH */
  964 #endif
  965         case KDSKBMODE:         /* set keyboard mode */
  966                 switch (*(int *)arg) {
  967                 case K_XLATE:
  968                         if (state->ks_mode != K_XLATE) {
  969                                 /* make lock key state and LED state match */
  970                                 state->ks_state &= ~LOCK_MASK;
  971                                 state->ks_state |= KBD_LED_VAL(kbd);
  972                         }
  973                         /* FALLTHROUGH */
  974                 case K_RAW:
  975                 case K_CODE:
  976                         if (state->ks_mode != *(int *)arg) {
  977                                 atkbd_clear_state(kbd);
  978                                 state->ks_mode = *(int *)arg;
  979                         }
  980                         break;
  981                 default:
  982                         splx(s);
  983                         return EINVAL;
  984                 }
  985                 break;
  986 
  987         case KDGETLED:          /* get keyboard LED */
  988                 *(int *)arg = KBD_LED_VAL(kbd);
  989                 break;
  990 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
  991     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
  992         case _IO('K', 66):
  993                 ival = IOCPARM_IVAL(arg);
  994                 arg = (caddr_t)&ival;
  995                 /* FALLTHROUGH */
  996 #endif
  997         case KDSETLED:          /* set keyboard LED */
  998                 /* NOTE: lock key state in ks_state won't be changed */
  999                 if (*(int *)arg & ~LOCK_MASK) {
 1000                         splx(s);
 1001                         return EINVAL;
 1002                 }
 1003                 i = *(int *)arg;
 1004                 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
 1005                 if (state->ks_mode == K_XLATE &&
 1006                     kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
 1007                         if (i & ALKED)
 1008                                 i |= CLKED;
 1009                         else
 1010                                 i &= ~CLKED;
 1011                 }
 1012                 if (KBD_HAS_DEVICE(kbd)) {
 1013                         error = write_kbd(state->kbdc, KBDC_SET_LEDS,
 1014                                           ledmap[i & LED_MASK]);
 1015                         if (error) {
 1016                                 splx(s);
 1017                                 return error;
 1018                         }
 1019                 }
 1020 #ifdef EVDEV_SUPPORT
 1021                 /* push LED states to evdev */
 1022                 if (state->ks_evdev != NULL &&
 1023                     evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
 1024                         evdev_push_leds(state->ks_evdev, *(int *)arg);
 1025 #endif
 1026                 KBD_LED_VAL(kbd) = *(int *)arg;
 1027                 break;
 1028 
 1029         case KDGKBSTATE:        /* get lock key state */
 1030                 *(int *)arg = state->ks_state & LOCK_MASK;
 1031                 break;
 1032 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
 1033     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
 1034         case _IO('K', 20):
 1035                 ival = IOCPARM_IVAL(arg);
 1036                 arg = (caddr_t)&ival;
 1037                 /* FALLTHROUGH */
 1038 #endif
 1039         case KDSKBSTATE:        /* set lock key state */
 1040                 if (*(int *)arg & ~LOCK_MASK) {
 1041                         splx(s);
 1042                         return EINVAL;
 1043                 }
 1044                 state->ks_state &= ~LOCK_MASK;
 1045                 state->ks_state |= *(int *)arg;
 1046                 splx(s);
 1047                 /* set LEDs and quit */
 1048                 return atkbd_ioctl(kbd, KDSETLED, arg);
 1049 
 1050         case KDSETREPEAT:       /* set keyboard repeat rate (new interface) */
 1051                 splx(s);
 1052                 if (!KBD_HAS_DEVICE(kbd))
 1053                         return 0;
 1054                 i = typematic(((int *)arg)[0], ((int *)arg)[1]);
 1055                 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
 1056                 if (error == 0) {
 1057                         kbd->kb_delay1 = typematic_delay(i);
 1058                         kbd->kb_delay2 = typematic_rate(i);
 1059 #ifdef EVDEV_SUPPORT
 1060                         if (state->ks_evdev != NULL &&
 1061                             evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
 1062                                 evdev_push_repeats(state->ks_evdev, kbd);
 1063 #endif
 1064                 }
 1065                 return error;
 1066 
 1067 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
 1068     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
 1069         case _IO('K', 67):
 1070                 ival = IOCPARM_IVAL(arg);
 1071                 arg = (caddr_t)&ival;
 1072                 /* FALLTHROUGH */
 1073 #endif
 1074         case KDSETRAD:          /* set keyboard repeat rate (old interface) */
 1075                 splx(s);
 1076                 if (!KBD_HAS_DEVICE(kbd))
 1077                         return 0;
 1078                 error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
 1079                 if (error == 0) {
 1080                         kbd->kb_delay1 = typematic_delay(*(int *)arg);
 1081                         kbd->kb_delay2 = typematic_rate(*(int *)arg);
 1082 #ifdef EVDEV_SUPPORT
 1083                         if (state->ks_evdev != NULL &&
 1084                             evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
 1085                                 evdev_push_repeats(state->ks_evdev, kbd);
 1086 #endif
 1087                 }
 1088                 return error;
 1089 
 1090         case PIO_KEYMAP:        /* set keyboard translation table */
 1091         case OPIO_KEYMAP:       /* set keyboard translation table (compat) */
 1092         case PIO_KEYMAPENT:     /* set keyboard translation table entry */
 1093         case PIO_DEADKEYMAP:    /* set accent key translation table */
 1094                 state->ks_accents = 0;
 1095                 /* FALLTHROUGH */
 1096         default:
 1097                 splx(s);
 1098                 return genkbd_commonioctl(kbd, cmd, arg);
 1099         }
 1100 
 1101         splx(s);
 1102         return 0;
 1103 }
 1104 
 1105 /* lock the access to the keyboard */
 1106 static int
 1107 atkbd_lock(keyboard_t *kbd, int lock)
 1108 {
 1109         return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
 1110 }
 1111 
 1112 /* clear the internal state of the keyboard */
 1113 static void
 1114 atkbd_clear_state(keyboard_t *kbd)
 1115 {
 1116         atkbd_state_t *state;
 1117 
 1118         state = (atkbd_state_t *)kbd->kb_data;
 1119         state->ks_flags = 0;
 1120         state->ks_polling = 0;
 1121         state->ks_state &= LOCK_MASK;   /* preserve locking key state */
 1122         state->ks_accents = 0;
 1123         state->ks_composed_char = 0;
 1124 #if 0
 1125         state->ks_prefix = 0; /* XXX */
 1126 #endif
 1127 }
 1128 
 1129 /* save the internal state */
 1130 static int
 1131 atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
 1132 {
 1133         if (len == 0)
 1134                 return sizeof(atkbd_state_t);
 1135         if (len < sizeof(atkbd_state_t))
 1136                 return -1;
 1137         bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
 1138         return 0;
 1139 }
 1140 
 1141 /* set the internal state */
 1142 static int
 1143 atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
 1144 {
 1145         if (len < sizeof(atkbd_state_t))
 1146                 return ENOMEM;
 1147         if (((atkbd_state_t *)kbd->kb_data)->kbdc
 1148                 != ((atkbd_state_t *)buf)->kbdc)
 1149                 return ENOMEM;
 1150         bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
 1151         return 0;
 1152 }
 1153 
 1154 static int
 1155 atkbd_poll(keyboard_t *kbd, int on)
 1156 {
 1157         atkbd_state_t *state;
 1158         int s;
 1159 
 1160         state = (atkbd_state_t *)kbd->kb_data;
 1161         s = spltty();
 1162         if (on)
 1163                 ++state->ks_polling;
 1164         else
 1165                 --state->ks_polling;
 1166         splx(s);
 1167         return 0;
 1168 }
 1169 
 1170 static int
 1171 atkbd_reset(KBDC kbdc, int flags, int c)
 1172 {
 1173         /* reset keyboard hardware */
 1174         if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
 1175                 /*
 1176                  * KEYBOARD ERROR
 1177                  * Keyboard reset may fail either because the keyboard
 1178                  * doen't exist, or because the keyboard doesn't pass
 1179                  * the self-test, or the keyboard controller on the
 1180                  * motherboard and the keyboard somehow fail to shake hands.
 1181                  * It is just possible, particularly in the last case,
 1182                  * that the keyboard controller may be left in a hung state.
 1183                  * test_controller() and test_kbd_port() appear to bring
 1184                  * the keyboard controller back (I don't know why and how,
 1185                  * though.)
 1186                  */
 1187                 empty_both_buffers(kbdc, 10);
 1188                 test_controller(kbdc);
 1189                 test_kbd_port(kbdc);
 1190                 /*
 1191                  * We could disable the keyboard port and interrupt... but, 
 1192                  * the keyboard may still exist (see above). 
 1193                  */
 1194                 set_controller_command_byte(kbdc,
 1195                     ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
 1196                 if (bootverbose)
 1197                         printf("atkbd: failed to reset the keyboard.\n");
 1198                 return (EIO);
 1199         }
 1200         return (0);
 1201 }
 1202 
 1203 #ifdef EVDEV_SUPPORT
 1204 static void
 1205 atkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
 1206     int32_t value)
 1207 {
 1208         keyboard_t *kbd = evdev_get_softc(evdev);
 1209 
 1210         if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
 1211             (type == EV_LED || type == EV_REP)) {
 1212                 mtx_lock(&Giant);
 1213                 kbd_ev_event(kbd, type, code, value);
 1214                 mtx_unlock(&Giant);
 1215         }
 1216 }
 1217 #endif
 1218 
 1219 /* local functions */
 1220 
 1221 static int
 1222 set_typematic(keyboard_t *kbd)
 1223 {
 1224         int val, error;
 1225         atkbd_state_t *state = kbd->kb_data;
 1226 
 1227         val = typematic(DEFAULT_DELAY, DEFAULT_RATE);
 1228         error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val);
 1229         if (error == 0) {
 1230                 kbd->kb_delay1 = typematic_delay(val);
 1231                 kbd->kb_delay2 = typematic_rate(val);
 1232         }
 1233 
 1234         return (error);
 1235 }
 1236 
 1237 static int
 1238 setup_kbd_port(KBDC kbdc, int port, int intr)
 1239 {
 1240         if (!set_controller_command_byte(kbdc,
 1241                 KBD_KBD_CONTROL_BITS,
 1242                 ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
 1243                     | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
 1244                 return 1;
 1245         return 0;
 1246 }
 1247 
 1248 static int
 1249 get_kbd_echo(KBDC kbdc)
 1250 {
 1251         /* enable the keyboard port, but disable the keyboard intr. */
 1252         if (setup_kbd_port(kbdc, TRUE, FALSE))
 1253                 /* CONTROLLER ERROR: there is very little we can do... */
 1254                 return ENXIO;
 1255 
 1256         /* see if something is present */
 1257         write_kbd_command(kbdc, KBDC_ECHO);
 1258         if (read_kbd_data(kbdc) != KBD_ECHO) {
 1259                 empty_both_buffers(kbdc, 10);
 1260                 test_controller(kbdc);
 1261                 test_kbd_port(kbdc);
 1262                 return ENXIO;
 1263         }
 1264 
 1265         /* enable the keyboard port and intr. */
 1266         if (setup_kbd_port(kbdc, TRUE, TRUE)) {
 1267                 /*
 1268                  * CONTROLLER ERROR 
 1269                  * This is serious; the keyboard intr is left disabled! 
 1270                  */
 1271                 return ENXIO;
 1272         }
 1273 
 1274         return 0;
 1275 }
 1276 
 1277 static int
 1278 probe_keyboard(KBDC kbdc, int flags)
 1279 {
 1280         /*
 1281          * Don't try to print anything in this function.  The low-level 
 1282          * console may not have been initialized yet...
 1283          */
 1284         int err;
 1285         int c;
 1286         int m;
 1287 
 1288         if (!kbdc_lock(kbdc, TRUE)) {
 1289                 /* driver error? */
 1290                 return ENXIO;
 1291         }
 1292 
 1293         /* temporarily block data transmission from the keyboard */
 1294         write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
 1295 
 1296         /* flush any noise in the buffer */
 1297         empty_both_buffers(kbdc, 100);
 1298 
 1299         /* save the current keyboard controller command byte */
 1300         m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
 1301         c = get_controller_command_byte(kbdc);
 1302         if (c == -1) {
 1303                 /* CONTROLLER ERROR */
 1304                 kbdc_set_device_mask(kbdc, m);
 1305                 kbdc_lock(kbdc, FALSE);
 1306                 return ENXIO;
 1307         }
 1308 
 1309         /* 
 1310          * The keyboard may have been screwed up by the boot block.
 1311          * We may just be able to recover from error by testing the controller
 1312          * and the keyboard port. The controller command byte needs to be
 1313          * saved before this recovery operation, as some controllers seem 
 1314          * to set the command byte to particular values.
 1315          */
 1316         test_controller(kbdc);
 1317         if (!(flags & KB_CONF_NO_PROBE_TEST))
 1318                 test_kbd_port(kbdc);
 1319 
 1320         err = get_kbd_echo(kbdc);
 1321 
 1322         /*
 1323          * Even if the keyboard doesn't seem to be present (err != 0),
 1324          * we shall enable the keyboard port and interrupt so that
 1325          * the driver will be operable when the keyboard is attached
 1326          * to the system later.  It is NOT recommended to hot-plug
 1327          * the AT keyboard, but many people do so...
 1328          */
 1329         kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
 1330         setup_kbd_port(kbdc, TRUE, TRUE);
 1331 #if 0
 1332         if (err == 0) {
 1333                 kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
 1334         } else {
 1335                 /* try to restore the command byte as before */
 1336                 set_controller_command_byte(kbdc,
 1337                     ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
 1338                 kbdc_set_device_mask(kbdc, m);
 1339         }
 1340 #endif
 1341 
 1342         kbdc_lock(kbdc, FALSE);
 1343         return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err);
 1344 }
 1345 
 1346 static int
 1347 init_keyboard(KBDC kbdc, int *type, int flags)
 1348 {
 1349         int codeset;
 1350         int id;
 1351         int c;
 1352 
 1353         if (!kbdc_lock(kbdc, TRUE)) {
 1354                 /* driver error? */
 1355                 return EIO;
 1356         }
 1357 
 1358         /* temporarily block data transmission from the keyboard */
 1359         write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
 1360 
 1361         /* save the current controller command byte */
 1362         empty_both_buffers(kbdc, 200);
 1363         c = get_controller_command_byte(kbdc);
 1364         if (c == -1) {
 1365                 /* CONTROLLER ERROR */
 1366                 kbdc_lock(kbdc, FALSE);
 1367                 printf("atkbd: unable to get the current command byte value.\n");
 1368                 return EIO;
 1369         }
 1370         if (bootverbose)
 1371                 printf("atkbd: the current kbd controller command byte %04x\n",
 1372                    c);
 1373 #if 0
 1374         /* override the keyboard lock switch */
 1375         c |= KBD_OVERRIDE_KBD_LOCK;
 1376 #endif
 1377 
 1378         /* enable the keyboard port, but disable the keyboard intr. */
 1379         if (setup_kbd_port(kbdc, TRUE, FALSE)) {
 1380                 /* CONTROLLER ERROR: there is very little we can do... */
 1381                 printf("atkbd: unable to set the command byte.\n");
 1382                 kbdc_lock(kbdc, FALSE);
 1383                 return EIO;
 1384         }
 1385 
 1386         if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
 1387             atkbd_reset(kbdc, flags, c)) {
 1388                 kbdc_lock(kbdc, FALSE);
 1389                 return EIO;
 1390         }
 1391 
 1392         /* 
 1393          * Check if we have an XT keyboard before we attempt to reset it. 
 1394          * The procedure assumes that the keyboard and the controller have 
 1395          * been set up properly by BIOS and have not been messed up 
 1396          * during the boot process.
 1397          */
 1398         codeset = -1;
 1399         if (flags & KB_CONF_ALT_SCANCODESET)
 1400                 /* the user says there is a XT keyboard */
 1401                 codeset = 1;
 1402 #ifdef KBD_DETECT_XT_KEYBOARD
 1403         else if ((c & KBD_TRANSLATION) == 0) {
 1404                 /* SET_SCANCODE_SET is not always supported; ignore error */
 1405                 if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
 1406                         == KBD_ACK) 
 1407                         codeset = read_kbd_data(kbdc);
 1408         }
 1409         if (bootverbose)
 1410                 printf("atkbd: scancode set %d\n", codeset);
 1411 #endif /* KBD_DETECT_XT_KEYBOARD */
 1412 
 1413         *type = KB_OTHER;
 1414         id = get_kbd_id(kbdc);
 1415         switch(id) {
 1416         case 0x41ab:    /* 101/102/... Enhanced */
 1417         case 0x83ab:    /* ditto */
 1418         case 0x54ab:    /* SpaceSaver */
 1419         case 0x84ab:    /* ditto */
 1420 #if 0
 1421         case 0x90ab:    /* 'G' */
 1422         case 0x91ab:    /* 'P' */
 1423         case 0x92ab:    /* 'A' */
 1424 #endif
 1425                 *type = KB_101;
 1426                 break;
 1427         case -1:        /* AT 84 keyboard doesn't return ID */
 1428                 *type = KB_84;
 1429                 break;
 1430         default:
 1431                 break;
 1432         }
 1433         if (bootverbose)
 1434                 printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
 1435 
 1436         if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
 1437             atkbd_reset(kbdc, flags, c)) {
 1438                 kbdc_lock(kbdc, FALSE);
 1439                 return EIO;
 1440         }
 1441 
 1442         /*
 1443          * Allow us to set the XT_KEYBD flag so that keyboards
 1444          * such as those on the IBM ThinkPad laptop computers can be used
 1445          * with the standard console driver.
 1446          */
 1447         if (codeset == 1) {
 1448                 if (send_kbd_command_and_data(kbdc,
 1449                         KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
 1450                         /* XT kbd doesn't need scan code translation */
 1451                         c &= ~KBD_TRANSLATION;
 1452                 } else {
 1453                         /*
 1454                          * KEYBOARD ERROR 
 1455                          * The XT kbd isn't usable unless the proper scan
 1456                          * code set is selected. 
 1457                          */
 1458                         set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
 1459                             ? 0xff : KBD_KBD_CONTROL_BITS, c);
 1460                         kbdc_lock(kbdc, FALSE);
 1461                         printf("atkbd: unable to set the XT keyboard mode.\n");
 1462                         return EIO;
 1463                 }
 1464         }
 1465 
 1466         /*
 1467          * Some keyboards require a SETLEDS command to be sent after
 1468          * the reset command before they will send keystrokes to us
 1469          */
 1470         if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) &&
 1471             send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) {
 1472                 printf("atkbd: setleds failed\n");
 1473         }
 1474         if (!ALLOW_DISABLE_KBD(kbdc))
 1475             send_kbd_command(kbdc, KBDC_ENABLE_KBD);
 1476 
 1477         /* enable the keyboard port and intr. */
 1478         if (!set_controller_command_byte(kbdc, 
 1479                 KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
 1480                 (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
 1481                     | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
 1482                 /*
 1483                  * CONTROLLER ERROR 
 1484                  * This is serious; we are left with the disabled
 1485                  * keyboard intr. 
 1486                  */
 1487                 set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
 1488                     ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
 1489                         KBD_OVERRIDE_KBD_LOCK), c);
 1490                 kbdc_lock(kbdc, FALSE);
 1491                 printf("atkbd: unable to enable the keyboard port and intr.\n");
 1492                 return EIO;
 1493         }
 1494 
 1495         kbdc_lock(kbdc, FALSE);
 1496         return 0;
 1497 }
 1498 
 1499 static int
 1500 write_kbd(KBDC kbdc, int command, int data)
 1501 {
 1502         int s;
 1503 
 1504         /* prevent the timeout routine from polling the keyboard */
 1505         if (!kbdc_lock(kbdc, TRUE)) 
 1506                 return EBUSY;
 1507 
 1508         /* disable the keyboard and mouse interrupt */
 1509         s = spltty();
 1510 #if 0
 1511         c = get_controller_command_byte(kbdc);
 1512         if ((c == -1) 
 1513             || !set_controller_command_byte(kbdc, 
 1514                 kbdc_get_device_mask(kbdc),
 1515                 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
 1516                 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
 1517                 /* CONTROLLER ERROR */
 1518                 kbdc_lock(kbdc, FALSE);
 1519                 splx(s);
 1520                 return EIO;
 1521         }
 1522         /* 
 1523          * Now that the keyboard controller is told not to generate 
 1524          * the keyboard and mouse interrupts, call `splx()' to allow 
 1525          * the other tty interrupts. The clock interrupt may also occur, 
 1526          * but the timeout routine (`scrn_timer()') will be blocked 
 1527          * by the lock flag set via `kbdc_lock()'
 1528          */
 1529         splx(s);
 1530 #endif
 1531         if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
 1532                 send_kbd_command(kbdc, KBDC_ENABLE_KBD);
 1533 #if 0
 1534         /* restore the interrupts */
 1535         if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc),
 1536             c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 
 1537                 /* CONTROLLER ERROR */
 1538         }
 1539 #else
 1540         splx(s);
 1541 #endif
 1542         kbdc_lock(kbdc, FALSE);
 1543 
 1544         return 0;
 1545 }
 1546 
 1547 static int
 1548 get_kbd_id(KBDC kbdc)
 1549 {
 1550         int id1, id2;
 1551 
 1552         empty_both_buffers(kbdc, 10);
 1553         id1 = id2 = -1;
 1554         if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
 1555                 return -1;
 1556 
 1557         DELAY(10000);   /* 10 msec delay */
 1558         id1 = read_kbd_data(kbdc);
 1559         if (id1 != -1)
 1560                 id2 = read_kbd_data(kbdc);
 1561 
 1562         if ((id1 == -1) || (id2 == -1)) {
 1563                 empty_both_buffers(kbdc, 10);
 1564                 test_controller(kbdc);
 1565                 test_kbd_port(kbdc);
 1566                 return -1;
 1567         }
 1568         return ((id2 << 8) | id1);
 1569 }
 1570 
 1571 static int delays[] = { 250, 500, 750, 1000 };
 1572 static int rates[] = {  34,  38,  42,  46,  50,  55,  59,  63,
 1573                         68,  76,  84,  92, 100, 110, 118, 126,
 1574                        136, 152, 168, 184, 200, 220, 236, 252,
 1575                        272, 304, 336, 368, 400, 440, 472, 504 };
 1576 
 1577 static int
 1578 typematic_delay(int i)
 1579 {
 1580         return delays[(i >> 5) & 3];
 1581 }
 1582 
 1583 static int
 1584 typematic_rate(int i)
 1585 {
 1586         return rates[i & 0x1f];
 1587 }
 1588 
 1589 static int
 1590 typematic(int delay, int rate)
 1591 {
 1592         int value;
 1593         int i;
 1594 
 1595         for (i = nitems(delays) - 1; i > 0; --i) {
 1596                 if (delay >= delays[i])
 1597                         break;
 1598         }
 1599         value = i << 5;
 1600         for (i = nitems(rates) - 1; i > 0; --i) {
 1601                 if (rate >= rates[i])
 1602                         break;
 1603         }
 1604         value |= i;
 1605         return value;
 1606 }

Cache object: a9d84c78ebebe67738c559c4b1fc66cf


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.