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

Cache object: 4795efaf2f32bc3fd4b8dfcd9f5be663


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