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

Cache object: 70e773dccd937af5803d2c8e630af34e


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