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

Cache object: fbf5574413e3788871207b80fd25dab0


[ 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.