1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*      Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
  22 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T       */
  23 /*        All Rights Reserved   */
  24 
  25 /*
  26  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 #define KEYMAP_SIZE_VARIABLE
  31 
  32 #include <sys/types.h>
  33 #include <sys/errno.h>
  34 #include <sys/inline.h>
  35 #include <sys/termio.h>
  36 #include <sys/stropts.h>
  37 #include <sys/termios.h>
  38 #include <sys/stream.h>
  39 #include <sys/strtty.h>
  40 #include <sys/strsubr.h>
  41 #include <sys/strsun.h>
  42 #include <sys/ddi.h>
  43 #include <sys/sunddi.h>
  44 #include <sys/note.h>
  45 #include "sys/consdev.h"
  46 #include <sys/kbd.h>
  47 #include <sys/kbtrans.h>
  48 #include "kb8042.h"
  49 
  50 #include <sys/i8042.h>
  51 
  52 #include "sys/kbio.h"           /* for KIOCSLAYOUT */
  53 #include "sys/stat.h"
  54 #include "sys/reboot.h"
  55 #include <sys/promif.h>
  56 #include <sys/beep.h>
  57 #include <sys/inttypes.h>
  58 #include <sys/policy.h>
  59 
  60 /*
  61  * For any keyboard, there is a unique code describing the position
  62  * of the key on a keyboard. We refer to the code as "station number".
  63  * The following table is used to map the station numbers from ps2
  64  * AT/XT keyboards to that of a USB one.
  65  *
  66  * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
  67  * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
  68  * when in KB_PC mode.  Note that this does not need to be made conditional
  69  * on the architecture for which this module is complied because there are no
  70  * keys that map to K8042_STOP on non-SPARC platforms.
  71  */
  72 static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
  73 /*  0 */        0,      53,     30,     31,     32,     33,     34,     35,
  74 /*  8 */        36,     37,     38,     39,     45,     46,     137,    42,
  75 /* 16 */        43,     20,     26,     8,      21,     23,     28,     24,
  76 /* 24 */        12,     18,     19,     47,     48,     49,     57,     4,
  77 /* 32 */        22,     7,      9,      10,     11,     13,     14,     15,
  78 /* 40 */        51,     52,     50,     40,     225,    100,    29,     27,
  79 /* 48 */        6,      25,     5,      17,     16,     54,     55,     56,
  80 /* 56 */        135,    229,    224,    227,    226,    44,     230,    231,
  81 /* 64 */        228,    101,    0,      0,      0,      0,      0,      0,
  82 /* 72 */        0,      0,      0,      73,     76,     0,      0,      80,
  83 /* 80 */        74,     77,     0,      82,     81,     75,     78,     0,
  84 /* 88 */        0,      79,     83,     95,     92,     89,     0,      84,
  85 /* 96 */        96,     93,     90,     98,     85,     97,     94,     91,
  86 /* 104 */       99,     86,     87,     133,    88,     0,      41,     0,
  87 /* 112 */       58,     59,     60,     61,     62,     63,     64,     65,
  88 /* 120 */       66,     67,     68,     69,     70,     71,     72,     0,
  89 /* 128 */       0,      0,      0,      139,    138,    136,    0,      0,
  90 /* 136 */       0,      0,      0,      0,      0,      0,      0,      0,
  91 /* 144 */       0,      0,      0,      0,      0,      0,      0,      0,
  92 /* 152 */       0,      0,      0,      0,      0,      0,      0,      0,
  93 /* 160 */       120,    0,      0,      0,      0,      0,      0,      0,
  94 /* 168 */       0,      0,      0,      0,      0,      0,      0,      0,
  95 /* 176 */       0,      0,      0,      0,      0,      0,      0,      0,
  96 /* 184 */       0,      0,      0,      0,      0,      0,      0,      0,
  97 /* 192 */       0,      0,      0,      0,      0,      0,      0,      0,
  98 /* 200 */       0,      0,      0,      0,      0,      0,      0,      0,
  99 /* 208 */       0,      0,      0,      0,      0,      0,      0,      0,
 100 /* 216 */       0,      0,      0,      0,      0,      0,      0,      0,
 101 /* 224 */       0,      0,      0,      0,      0,      0,      0,      0,
 102 /* 232 */       0,      128,    129,    127,    0,      0,      0,      0,
 103 /* 240 */       0,      0,      0,      0,      0,      0,      0,      0,
 104 /* 248 */       0,      0,      0,      0
 105 };
 106 
 107 #ifdef __sparc
 108 #define USECS_PER_WAIT 100
 109 #define MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
 110 #define MIN_DELAY_USECS USECS_PER_WAIT
 111 
 112 boolean_t kb8042_warn_unknown_scanset = B_TRUE;
 113 int kb8042_default_scanset = 2;
 114 
 115 #endif
 116 
 117 #define MAX_KB8042_WAIT_MAX_MS  500             /* 500ms total */
 118 #define MAX_KB8042_RETRIES      5
 119 
 120 enum state_return { STATE_NORMAL, STATE_INTERNAL };
 121 
 122 static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
 123 static uint_t kb8042_intr(caddr_t arg);
 124 static void kb8042_wait_poweron(struct kb8042 *kb8042);
 125 static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
 126 static int kb8042_xlate_leds(int);
 127 static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
 128 static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
 129 static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
 130 static boolean_t kb8042_polled_keycheck(
 131                         struct kbtrans_hardware *hw, int *key,
 132                         enum keystate *state);
 133 static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
 134 static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
 135                         enum keystate state);
 136 static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
 137 static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
 138 static void kb8042_iocdatamsg(queue_t *, mblk_t *);
 139 static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
 140 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
 141 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
 142 static void kb8042_cleanup(struct kb8042 *kb8042);
 143 
 144 static struct kbtrans_callbacks kb8042_callbacks = {
 145         kb8042_streams_setled,
 146         kb8042_polled_setled,
 147         kb8042_polled_keycheck,
 148 };
 149 
 150 extern struct keyboard keyindex_pc;
 151 
 152 #define DRIVER_NAME(dip) ddi_driver_name(dip)
 153 
 154 static  char    module_name[] = "kb8042";
 155 
 156 static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
 157                         cred_t *credp);
 158 static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
 159 static int kb8042_wsrv();
 160 
 161 struct module_info kb8042_sinfo = {
 162         42,             /* Module ID */
 163         module_name,
 164         0, 32,          /* Minimum and maximum packet sizes */
 165         256, 128        /* High and low water marks */
 166 };
 167 
 168 static struct qinit kb8042_rinit = {
 169         NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
 170 };
 171 
 172 static struct qinit kb8042_winit = {
 173         putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
 174 };
 175 
 176 struct streamtab
 177         kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
 178 
 179 struct kb8042   Kdws = {{{NULL}}};
 180 static dev_info_t *kb8042_dip = NULL;
 181 
 182 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
 183                 void **result);
 184 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
 185 static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
 186 
 187 static  struct cb_ops cb_kb8042_ops = {
 188         nulldev,                /* cb_open */
 189         nulldev,                /* cb_close */
 190         nodev,                  /* cb_strategy */
 191         nodev,                  /* cb_print */
 192         nodev,                  /* cb_dump */
 193         nodev,                  /* cb_read */
 194         nodev,                  /* cb_write */
 195         nodev,                  /* cb_ioctl */
 196         nodev,                  /* cb_devmap */
 197         nodev,                  /* cb_mmap */
 198         nodev,                  /* cb_segmap */
 199         nochpoll,               /* cb_chpoll */
 200         ddi_prop_op,            /* cb_prop_op */
 201         &kb8042_str_info,   /* cb_stream */
 202         D_MP
 203 };
 204 
 205 struct dev_ops kb8042_ops = {
 206         DEVO_REV,               /* devo_rev */
 207         0,                      /* devo_refcnt */
 208         kb8042_getinfo,         /* devo_getinfo */
 209         nulldev,                /* devo_identify */
 210         nulldev,                /* devo_probe */
 211         kb8042_attach,          /* devo_attach */
 212         kb8042_detach,          /* devo_detach */
 213         nodev,                  /* devo_reset */
 214         &cb_kb8042_ops,             /* devo_cb_ops */
 215         (struct bus_ops *)NULL, /* devo_bus_ops */
 216         NULL,                   /* devo_power */
 217         ddi_quiesce_not_needed, /* devo_quiesce */
 218 };
 219 
 220 
 221 /*
 222  * This is the loadable module wrapper.
 223  */
 224 #include <sys/modctl.h>
 225 
 226 /*
 227  * Module linkage information for the kernel.
 228  */
 229 static struct modldrv modldrv = {
 230         &mod_driverops, /* Type of module.  This one is a driver */
 231         "PS/2 keyboard driver",
 232         &kb8042_ops,        /* driver ops */
 233 };
 234 
 235 static struct modlinkage modlinkage = {
 236         MODREV_1,
 237         { (void *) &modldrv, NULL }
 238 };
 239 
 240 int
 241 _init(void)
 242 {
 243         int     rv;
 244 
 245         rv = mod_install(&modlinkage);
 246         return (rv);
 247 }
 248 
 249 
 250 int
 251 _fini(void)
 252 {
 253         return (mod_remove(&modlinkage));
 254 }
 255 
 256 int
 257 _info(struct modinfo *modinfop)
 258 {
 259         return (mod_info(&modlinkage, modinfop));
 260 }
 261 
 262 #ifdef __sparc
 263 static boolean_t
 264 kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
 265 {
 266         int i;
 267         int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
 268             I8042_INT_INPUT_AVAIL;
 269         int reps = timeout_usec / USECS_PER_WAIT;
 270 
 271         for (i = 0; i < reps; i++) {
 272                 if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
 273                         return (B_TRUE);
 274 
 275                 if (i < (reps - 1))
 276                         drv_usecwait(USECS_PER_WAIT);
 277         }
 278         return (B_FALSE);
 279 }
 280 
 281 static void
 282 kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
 283 {
 284         int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
 285             I8042_INT_INPUT_DATA;
 286 
 287         while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
 288                 (void) ddi_get8(kb8042->handle, kb8042->addr + port);
 289         }
 290 }
 291 
 292 /*
 293  * kb8042_send_and_expect does all its I/O via polling interfaces
 294  */
 295 static boolean_t
 296 kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
 297     int timeout, int *error, uint8_t *got)
 298 {
 299         uint8_t datab;
 300         int err;
 301         boolean_t rval;
 302 
 303         ddi_put8(kb8042->handle,
 304             kb8042->addr + I8042_POLL_OUTPUT_DATA, send);
 305 
 306         if (kb8042_is_input_avail(kb8042, timeout, B_TRUE)) {
 307                 err = 0;
 308                 datab = ddi_get8(kb8042->handle,
 309                     kb8042->addr + I8042_POLL_INPUT_DATA);
 310                 rval = ((datab == expect) ? B_TRUE : B_FALSE);
 311         } else {
 312                 err = EAGAIN;
 313                 rval = B_FALSE;
 314         }
 315 
 316         if (error != NULL)
 317                 *error = err;
 318         if (got != NULL)
 319                 *got = datab;
 320         return (rval);
 321 }
 322 
 323 static const char *
 324 kb8042_error_string(int errcode)
 325 {
 326         switch (errcode) {
 327         case EAGAIN:
 328                 return ("Timed out");
 329         default:
 330                 return ("Unknown error");
 331         }
 332 }
 333 
 334 /*
 335  * kb8042_read_scanset works properly because it is called before ddi_add_intr
 336  * (if it is called after ddi_add_intr, i8042_intr would call kb8042_intr
 337  * instead of just storing the data that comes in from the keyboard, which
 338  * would prevent the code here from getting it.)
 339  */
 340 static int
 341 kb8042_read_scanset(struct kb8042 *kb8042)
 342 {
 343         int scanset = -1;
 344         int err;
 345         uint8_t got;
 346 
 347         kb8042_clear_input_buffer(kb8042, B_TRUE);
 348 
 349         /*
 350          * Send a "change scan code set" command to the keyboard.
 351          * It should respond with an ACK.
 352          */
 353         if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, MAX_WAIT_USECS,
 354             &err, &got) != B_TRUE) {
 355                 goto fail_read_scanset;
 356         }
 357 
 358         /*
 359          * Send a 0.  The keyboard should ACK the 0, then it should send the
 360          * scan code set in use.
 361          */
 362         if (kb8042_send_and_expect(kb8042, 0, KB_ACK, MAX_WAIT_USECS, &err,
 363             &got) != B_TRUE) {
 364                 goto fail_read_scanset;
 365         }
 366 
 367         /*
 368          * The next input byte from the keyboard should be the scan code
 369          * set in use, though some keyboards like to send a few more acks
 370          * just for fun, so blow past those to get the keyboard scan code.
 371          */
 372         while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
 373             (scanset = ddi_get8(kb8042->handle,
 374             kb8042->addr + I8042_POLL_INPUT_DATA)) == KB_ACK)
 375                 ;
 376 
 377 #ifdef DEBUG
 378         cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
 379             scanset);
 380 #endif
 381 
 382         return (scanset);
 383 
 384 fail_read_scanset:
 385 #ifdef DEBUG
 386         if (err == 0)
 387                 cmn_err(CE_NOTE, "Could not read current scan set from "
 388                     "keyboard: %s. (Expected 0x%x, but got 0x%x).",
 389                     kb8042_error_string(err), KB_ACK, got);
 390         else
 391                 cmn_err(CE_NOTE, "Could not read current scan set from "
 392                     "keyboard: %s.", kb8042_error_string(err));
 393 #endif
 394         return (-1);
 395 }
 396 #endif
 397 
 398 static int
 399 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
 400 {
 401         int     rc;
 402         int     scanset;
 403         int     leds;
 404 
 405         struct kb8042   *kb8042 = &Kdws;
 406         static ddi_device_acc_attr_t attr = {
 407                 DDI_DEVICE_ATTR_V0,
 408                 DDI_NEVERSWAP_ACC,
 409                 DDI_STRICTORDER_ACC,
 410         };
 411 
 412         switch (cmd) {
 413         case DDI_RESUME:
 414                 leds = kb8042->leds.commanded;
 415                 kb8042->w_init = 0;
 416                 kb8042_init(kb8042, B_TRUE);
 417                 kb8042_setled(kb8042, leds, B_FALSE);
 418                 mutex_enter(&kb8042->w_hw_mutex);
 419                 kb8042->suspended = B_FALSE;
 420                 if (kb8042->w_qp != NULL) {
 421                         enableok(WR(kb8042->w_qp));
 422                         qenable(WR(kb8042->w_qp));
 423                 }
 424                 cv_broadcast(&kb8042->suspend_cv);
 425                 mutex_exit(&kb8042->w_hw_mutex);
 426                 return (DDI_SUCCESS);
 427 
 428         case DDI_ATTACH:
 429                 if (kb8042_dip != NULL)
 430                         return (DDI_FAILURE);
 431                 /* The rest of the function is for attach */
 432                 break;
 433 
 434         default:
 435                 return (DDI_FAILURE);
 436         }
 437 
 438         kb8042->debugger.mod1 = 58;  /* Left Ctrl */
 439         kb8042->debugger.mod2 = 60;  /* Left Alt */
 440         kb8042->debugger.trigger = 33;       /* D */
 441         kb8042->debugger.mod1_down = B_FALSE;
 442         kb8042->debugger.mod2_down = B_FALSE;
 443         kb8042->debugger.enabled = B_FALSE;
 444 
 445         kb8042_dip = devi;
 446         kb8042->init_state = KB8042_UNINITIALIZED;
 447 
 448         kb8042->polled_synthetic_release_pending = B_FALSE;
 449 
 450         if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
 451             DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
 452                 goto failure;
 453         }
 454 
 455         kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
 456 
 457         rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
 458             (offset_t)0, (offset_t)0, &attr, &kb8042->handle);
 459         if (rc != DDI_SUCCESS) {
 460 #ifdef DEBUG
 461                 cmn_err(CE_WARN, "kb8042_attach:  can't map registers");
 462 #endif
 463                 goto failure;
 464         }
 465 
 466         kb8042->init_state |= KB8042_REGS_MAPPED;
 467 
 468         if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
 469             DDI_SUCCESS) {
 470                 cmn_err(CE_WARN, "kb8042_attach:  Can't get iblock cookie");
 471                 goto failure;
 472         }
 473 
 474         mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
 475         cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
 476         cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
 477         cv_init(&kb8042->cmd_cv, NULL, CV_DRIVER, NULL);
 478         kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
 479 
 480         kb8042_init(kb8042, B_FALSE);
 481 
 482 #ifdef __sparc
 483         /* Detect the scan code set currently in use */
 484         scanset = kb8042_read_scanset(kb8042);
 485 
 486         if (scanset < 0 && kb8042_warn_unknown_scanset) {
 487 
 488                 cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
 489                 cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
 490                 cmn_err(CE_CONT, "Defaulting to scan code set %d.  If the "
 491                     "keyboard does not ", kb8042_default_scanset);
 492                 cmn_err(CE_CONT, "work properly, add "
 493                     "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
 494                     (kb8042_default_scanset == 1) ? 2 : 1);
 495                 cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
 496                     "restart the system.  If you ");
 497                 cmn_err(CE_CONT, "do not want to see this message in the "
 498                     "future, add ");
 499                 cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
 500                     "to /etc/system.\n");
 501 
 502                 /* Use the default scan code set. */
 503                 scanset = kb8042_default_scanset;
 504         }
 505 #else
 506         /* x86 systems use scan code set 1 -- no detection required */
 507         scanset = 1;
 508 #endif
 509         if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
 510                 cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
 511                     "Unknown scan code set `%d'.", scanset);
 512                 /* Scan code set is not supported */
 513                 goto failure;
 514         }
 515 
 516         /*
 517          * Turn on interrupts...
 518          */
 519         if (ddi_add_intr(devi, 0,
 520             &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
 521             kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
 522                 cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
 523                 goto failure;
 524         }
 525 
 526         kb8042->init_state |= KB8042_INTR_ADDED;
 527 
 528         ddi_report_dev(devi);
 529 
 530 #ifdef DEBUG
 531         cmn_err(CE_CONT, "?%s instance #%d READY\n",
 532             DRIVER_NAME(devi), ddi_get_instance(devi));
 533 #endif
 534 
 535         return (DDI_SUCCESS);
 536 
 537 failure:
 538         kb8042_cleanup(kb8042);
 539         return (DDI_FAILURE);
 540 }
 541 
 542 static int
 543 kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
 544 {
 545         struct kb8042 *kb8042 = &Kdws;
 546 
 547         switch (cmd) {
 548         case DDI_SUSPEND:
 549                 mutex_enter(&kb8042->w_hw_mutex);
 550                 ASSERT(kb8042->ops >= 0);
 551                 while (kb8042->ops > 0)
 552                         cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
 553                 kb8042->suspended = B_TRUE;
 554                 mutex_exit(&kb8042->w_hw_mutex);
 555                 return (DDI_SUCCESS);
 556 
 557         case DDI_DETACH:
 558                 /* If someone has a stream open, fail to detach */
 559                 if (kb8042->w_qp != NULL)
 560                         return (DDI_FAILURE);
 561 
 562                 ASSERT(kb8042_dip == dip);
 563 
 564                 kb8042_cleanup(kb8042);
 565 
 566                 return (DDI_SUCCESS);
 567 
 568         default:
 569                 return (DDI_FAILURE);
 570         }
 571 }
 572 
 573 /*ARGSUSED*/
 574 static int
 575 kb8042_getinfo(
 576     dev_info_t *dip,
 577     ddi_info_cmd_t infocmd,
 578     void *arg,
 579     void **result)
 580 {
 581         register int error;
 582 
 583         switch (infocmd) {
 584         case DDI_INFO_DEVT2DEVINFO:
 585                 if (kb8042_dip == NULL) {
 586                         error = DDI_FAILURE;
 587                 } else {
 588                         *result = (void *) kb8042_dip;
 589                         error = DDI_SUCCESS;
 590                 }
 591                 break;
 592         case DDI_INFO_DEVT2INSTANCE:
 593                 *result = (void *)0;
 594                 error = DDI_SUCCESS;
 595                 break;
 596         default:
 597                 error = DDI_FAILURE;
 598                 break;
 599         }
 600         return (error);
 601 }
 602 
 603 static void
 604 kb8042_cleanup(struct kb8042 *kb8042)
 605 {
 606         ASSERT(kb8042_dip != NULL);
 607 
 608         if (kb8042->init_state & KB8042_INTR_ADDED)
 609                 ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
 610 
 611         if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
 612                 cv_destroy(&kb8042->cmd_cv);
 613                 cv_destroy(&kb8042->suspend_cv);
 614                 cv_destroy(&kb8042->ops_cv);
 615                 mutex_destroy(&kb8042->w_hw_mutex);
 616         }
 617 
 618         if (kb8042->init_state & KB8042_REGS_MAPPED)
 619                 ddi_regs_map_free(&kb8042->handle);
 620 
 621         if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
 622                 ddi_remove_minor_node(kb8042_dip, NULL);
 623 
 624         kb8042->init_state = KB8042_UNINITIALIZED;
 625         kb8042_dip = NULL;
 626 }
 627 
 628 static void
 629 kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
 630 {
 631         if (kb8042->w_init)
 632                 return;
 633 
 634         if (!from_resume) {
 635                 kb8042->w_kblayout = 0;      /* Default to US */
 636                 kb8042->w_qp = (queue_t *)NULL;
 637                 kb8042->simulated_kbd_type = KB_PC;
 638                 kb8042->leds.commanded = -1; /* Unknown initial state */
 639                 kb8042->leds.desired = -1;   /* Unknown initial state */
 640         }
 641 
 642         kb8042_wait_poweron(kb8042);
 643 
 644         kb8042->kb_old_key_pos = 0;
 645 
 646         /*
 647          * Explicitly grab and release the 8042 lock outside of
 648          * kb8042_send_to_keyboard, because this is the only situation
 649          * where a polling interface is used with locking required.
 650          */
 651         (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_LOCK);
 652         /* Set up the command state machine and start it running. */
 653         kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_TRUE);
 654         (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_UNLOCK);
 655 
 656         kb8042->w_init++;
 657 
 658         (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */
 659 }
 660 
 661 /*ARGSUSED2*/
 662 static int
 663 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
 664 {
 665         struct kb8042   *kb8042;
 666         int err = 0;
 667         int initial_leds;
 668         int initial_led_mask;
 669 
 670         kb8042 = &Kdws;
 671 
 672         mutex_enter(&kb8042->w_hw_mutex);
 673         if (qp->q_ptr) {
 674                 kb8042->w_dev = *devp;
 675                 mutex_exit(&kb8042->w_hw_mutex);
 676                 return (0);
 677         }
 678 
 679         if (secpolicy_console(credp) != 0) {
 680                 mutex_exit(&kb8042->w_hw_mutex);
 681                 return (EPERM);
 682         }
 683 
 684         while (kb8042->suspended) {
 685                 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
 686                     0) {
 687                         mutex_exit(&kb8042->w_hw_mutex);
 688                         return (EINTR);
 689                 }
 690         }
 691 
 692         kb8042->w_dev = *devp;
 693         qp->q_ptr = (caddr_t)kb8042;
 694         WR(qp)->q_ptr = qp->q_ptr;
 695         if (!kb8042->w_qp)
 696                 kb8042->w_qp = qp;
 697 
 698         ASSERT(kb8042->ops >= 0);
 699         kb8042->ops++;
 700         mutex_exit(&kb8042->w_hw_mutex);
 701 
 702         kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
 703         err = kbtrans_streams_init(qp, sflag,
 704             (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
 705             &kb8042->hw_kbtrans,
 706             initial_leds, initial_led_mask);
 707         if (err != 0)
 708                 goto out;
 709 
 710         kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
 711 
 712         kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
 713         kb8042->polledio.cons_polledio_argument =
 714             (cons_polledio_arg_t)kb8042;
 715         kb8042->polledio.cons_polledio_putchar = NULL;
 716         kb8042->polledio.cons_polledio_getchar =
 717             (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
 718         kb8042->polledio.cons_polledio_ischar =
 719             (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
 720         kb8042->polledio.cons_polledio_enter = NULL;
 721         kb8042->polledio.cons_polledio_exit = NULL;
 722         kb8042->polledio.cons_polledio_setled =
 723             (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
 724         kb8042->polledio.cons_polledio_keycheck =
 725             (boolean_t (*)(cons_polledio_arg_t, int *,
 726             enum keystate *))kb8042_polled_keycheck;
 727 
 728         qprocson(qp);
 729 
 730         kbtrans_streams_enable(kb8042->hw_kbtrans);
 731 
 732 out:
 733         mutex_enter(&kb8042->w_hw_mutex);
 734         ASSERT(kb8042->ops > 0);
 735         kb8042->ops--;
 736         if (kb8042->ops == 0)
 737                 cv_broadcast(&kb8042->ops_cv);
 738         mutex_exit(&kb8042->w_hw_mutex);
 739 
 740         return (err);
 741 }
 742 
 743 /*ARGSUSED1*/
 744 static int
 745 kb8042_close(queue_t *qp, int flag, cred_t *credp)
 746 {
 747         struct kb8042   *kb8042;
 748 
 749         /* If a beep is in progress, stop that */
 750         (void) beeper_off();
 751 
 752         kb8042 = (struct kb8042 *)qp->q_ptr;
 753 
 754         mutex_enter(&kb8042->w_hw_mutex);
 755         while (kb8042->suspended) {
 756                 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
 757                     0) {
 758                         mutex_exit(&kb8042->w_hw_mutex);
 759                         return (EINTR);
 760                 }
 761         }
 762 
 763         ASSERT(kb8042->ops >= 0);
 764         kb8042->ops++;
 765         mutex_exit(&kb8042->w_hw_mutex);
 766 
 767         (void) kbtrans_streams_fini(kb8042->hw_kbtrans);
 768 
 769         kb8042->w_qp = (queue_t *)NULL;
 770         qprocsoff(qp);
 771 
 772         mutex_enter(&kb8042->w_hw_mutex);
 773         ASSERT(kb8042->ops > 0);
 774         kb8042->ops--;
 775         if (kb8042->ops == 0)
 776                 cv_broadcast(&kb8042->ops_cv);
 777         mutex_exit(&kb8042->w_hw_mutex);
 778 
 779         return (0);
 780 }
 781 
 782 static int
 783 kb8042_wsrv(queue_t *qp)
 784 {
 785         struct kb8042 *kb8042;
 786 
 787         mblk_t  *mp;
 788         boolean_t suspended;
 789 
 790         kb8042 = (struct kb8042 *)qp->q_ptr;
 791 
 792         mutex_enter(&kb8042->w_hw_mutex);
 793         suspended = kb8042->suspended;
 794         ASSERT(kb8042->ops >= 0);
 795         if (!suspended)
 796                 kb8042->ops++;
 797         mutex_exit(&kb8042->w_hw_mutex);
 798 
 799 #ifdef NO_KB_DEBUG
 800         while (!suspended && (mp = getq(qp)) != NULL) {
 801 #else
 802         /*
 803          * Not taking keyboard input while suspending can make debugging
 804          * difficult.  However, we still do the ops counting so that we
 805          * don't suspend at a bad time.
 806          */
 807         while ((mp = getq(qp))) {
 808 #endif
 809                 switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
 810                 case KBTRANS_MESSAGE_HANDLED:
 811                         continue;
 812                 case KBTRANS_MESSAGE_NOT_HANDLED:
 813                         break;
 814                 }
 815                 switch (mp->b_datap->db_type) {
 816                 case M_IOCTL:
 817                         kb8042_ioctlmsg(kb8042, qp, mp);
 818                         continue;
 819                 case M_IOCDATA:
 820                         kb8042_iocdatamsg(qp, mp);
 821                         continue;
 822                 case M_DELAY:
 823                 case M_STARTI:
 824                 case M_STOPI:
 825                 case M_READ:    /* ignore, no buffered data */
 826                         freemsg(mp);
 827                         continue;
 828                 case M_FLUSH:
 829                         *mp->b_rptr &= ~FLUSHW;
 830                         if (*mp->b_rptr & FLUSHR)
 831                                 qreply(qp, mp);
 832                         else
 833                                 freemsg(mp);
 834                         continue;
 835                 default:
 836                         cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
 837                             mp->b_datap->db_type);
 838                         freemsg(mp);
 839                         continue;
 840                 }
 841         }
 842 
 843         mutex_enter(&kb8042->w_hw_mutex);
 844         if (!suspended) {
 845                 ASSERT(kb8042->ops > 0);
 846                 kb8042->ops--;
 847                 if (kb8042->ops == 0)
 848                         cv_broadcast(&kb8042->ops_cv);
 849         }
 850         mutex_exit(&kb8042->w_hw_mutex);
 851 
 852         return (0);
 853 }
 854 
 855 static void
 856 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
 857 {
 858         struct iocblk   *iocp;
 859         mblk_t          *datap;
 860         int             error;
 861         int             tmp;
 862         int             cycles;
 863         int             frequency;
 864         int             msecs;
 865 
 866         iocp = (struct iocblk *)mp->b_rptr;
 867 
 868         switch (iocp->ioc_cmd) {
 869 
 870         case CONSOPENPOLLEDIO:
 871                 error = miocpullup(mp, sizeof (struct cons_polledio *));
 872                 if (error != 0) {
 873                         miocnak(qp, mp, 0, error);
 874                         return;
 875                 }
 876 
 877                 /*
 878                  * We are given an appropriate-sized data block,
 879                  * and return a pointer to our structure in it.
 880                  */
 881                 *(struct cons_polledio **)mp->b_cont->b_rptr =
 882                     &kb8042->polledio;
 883                 mp->b_datap->db_type = M_IOCACK;
 884                 iocp->ioc_error = 0;
 885                 qreply(qp, mp);
 886                 break;
 887 
 888         case CONSCLOSEPOLLEDIO:
 889                 miocack(qp, mp, 0, 0);
 890                 break;
 891 
 892         case CONSSETABORTENABLE:
 893                 if (iocp->ioc_count != TRANSPARENT) {
 894                         miocnak(qp, mp, 0, EINVAL);
 895                         return;
 896                 }
 897 
 898                 kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
 899                 miocack(qp, mp, 0, 0);
 900                 break;
 901 
 902         /*
 903          * Valid only in TR_UNTRANS_MODE mode.
 904          */
 905         case CONSSETKBDTYPE:
 906                 error = miocpullup(mp, sizeof (int));
 907                 if (error != 0) {
 908                         miocnak(qp, mp, 0, error);
 909                         return;
 910                 }
 911                 tmp =  *(int *)mp->b_cont->b_rptr;
 912                 if (tmp != KB_PC && tmp != KB_USB) {
 913                         miocnak(qp, mp, 0, EINVAL);
 914                         break;
 915                 }
 916                 kb8042->simulated_kbd_type = tmp;
 917                 miocack(qp, mp, 0, 0);
 918                 break;
 919 
 920         case KIOCLAYOUT:
 921                 if (kb8042->w_kblayout == -1) {
 922                         miocnak(qp, mp, 0, EINVAL);
 923                         return;
 924                 }
 925 
 926                 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
 927                         miocnak(qp, mp, 0, ENOMEM);
 928                         return;
 929                 }
 930 
 931                 if (kb8042->simulated_kbd_type == KB_USB)
 932                         *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
 933                 else
 934                         *(int *)datap->b_wptr = kb8042->w_kblayout;
 935 
 936                 datap->b_wptr += sizeof (int);
 937                 if (mp->b_cont)
 938                         freemsg(mp->b_cont);
 939                 mp->b_cont = datap;
 940                 iocp->ioc_count = sizeof (int);
 941                 mp->b_datap->db_type = M_IOCACK;
 942                 iocp->ioc_error = 0;
 943                 qreply(qp, mp);
 944                 break;
 945 
 946         case KIOCSLAYOUT:
 947                 if (iocp->ioc_count != TRANSPARENT) {
 948                         miocnak(qp, mp, 0, EINVAL);
 949                         return;
 950                 }
 951 
 952                 kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
 953                 miocack(qp, mp, 0, 0);
 954                 break;
 955 
 956         case KIOCCMD:
 957                 error = miocpullup(mp, sizeof (int));
 958                 if (error != 0) {
 959                         miocnak(qp, mp, 0, error);
 960                         return;
 961                 }
 962 
 963                 kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
 964                 miocack(qp, mp, 0, 0);
 965                 break;
 966 
 967         case KIOCMKTONE:
 968                 if (iocp->ioc_count != TRANSPARENT) {
 969                         miocnak(qp, mp, 0, EINVAL);
 970                         return;
 971                 }
 972 
 973                 tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
 974                 cycles = tmp & 0xffff;
 975                 msecs = (tmp >> 16) & 0xffff;
 976 
 977                 if (cycles == 0)
 978                         frequency = UINT16_MAX;
 979                 else if (cycles == UINT16_MAX)
 980                         frequency = 0;
 981                 else {
 982                         frequency = (PIT_HZ + cycles / 2) / cycles;
 983                         if (frequency > UINT16_MAX)
 984                                 frequency = UINT16_MAX;
 985                 }
 986 
 987                 error = beep_mktone(frequency, msecs);
 988                 if (error != 0)
 989                         miocnak(qp, mp, 0, error);
 990                 else
 991                         miocack(qp, mp, 0, 0);
 992                 break;
 993 
 994         default:
 995 #ifdef DEBUG1
 996                 cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
 997 #endif
 998                 miocnak(qp, mp, 0, EINVAL);
 999                 return;
1000         }
1001 }
1002 
1003 /*
1004  * Process a byte received from the keyboard
1005  */
1006 static void
1007 kb8042_received_byte(
1008         struct kb8042   *kb8042,
1009         int             scancode)       /* raw scan code */
1010 {
1011         boolean_t       legit;          /* is this a legit key pos'n? */
1012         int             key_pos = -1;
1013         enum keystate   state;
1014         boolean_t       synthetic_release_needed;
1015 
1016         /*
1017          * Intercept ACK and RESEND and signal the condition that
1018          * kb8042_send_and_wait is waiting for.
1019          */
1020         if (scancode == KB_ACK) {
1021                 mutex_enter(&kb8042->w_hw_mutex);
1022                 kb8042->acked = 1;
1023                 cv_signal(&kb8042->cmd_cv);
1024                 mutex_exit(&kb8042->w_hw_mutex);
1025                 return;
1026         } else if (scancode == KB_RESEND) {
1027                 mutex_enter(&kb8042->w_hw_mutex);
1028                 kb8042->need_retry = 1;
1029                 cv_signal(&kb8042->cmd_cv);
1030                 mutex_exit(&kb8042->w_hw_mutex);
1031                 return;
1032         }
1033 
1034         if (!kb8042->w_init) /* can't do anything anyway */
1035                 return;
1036 
1037         legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
1038             &synthetic_release_needed);
1039 
1040         if (legit == 0) {
1041                 /* Eaten by translation */
1042                 return;
1043         }
1044 
1045         /*
1046          * Don't know if we want this permanently, but it seems interesting
1047          * for the moment.
1048          */
1049         if (key_pos == kb8042->debugger.mod1) {
1050                 kb8042->debugger.mod1_down = (state == KEY_PRESSED);
1051         }
1052         if (key_pos == kb8042->debugger.mod2) {
1053                 kb8042->debugger.mod2_down = (state == KEY_PRESSED);
1054         }
1055         if (kb8042->debugger.enabled &&
1056             key_pos == kb8042->debugger.trigger &&
1057             kb8042->debugger.mod1_down &&
1058             kb8042->debugger.mod2_down) {
1059                 /*
1060                  * Require new presses of the modifiers.
1061                  */
1062                 kb8042->debugger.mod1_down = B_FALSE;
1063                 kb8042->debugger.mod2_down = B_FALSE;
1064                 abort_sequence_enter(NULL);
1065                 return;
1066         }
1067 
1068         /*
1069          * If there's no queue above us - as can happen if we've been
1070          * attached but not opened - drop the keystroke.
1071          * Note that we do this here instead of above so that
1072          * Ctrl-Alt-D still works.
1073          */
1074         if (kb8042->w_qp == NULL) {
1075                 return;
1076         }
1077 
1078         /*
1079          * This is to filter out auto repeat since it can't be
1080          * turned off at the hardware.  (Yeah, yeah, PS/2 keyboards
1081          * can.  Don't know whether they've taken over the world.
1082          * Don't think so.)
1083          */
1084         if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
1085                 return;
1086         }
1087 
1088 
1089         kb8042_process_key(kb8042, key_pos, state);
1090 
1091         /*
1092          * This is a total hack.  For some stupid reason, the two additional
1093          * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
1094          * only.  We synthesize a release immediately.
1095          */
1096         if (synthetic_release_needed) {
1097                 (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
1098                 kb8042_process_key(kb8042, key_pos, state);
1099         }
1100 }
1101 
1102 
1103 static void
1104 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
1105     enum keystate state)
1106 {
1107         kbtrans_key_t key;
1108 
1109         ASSERT(key_pos >= 0 && key_pos <= 255);
1110         if (kb8042->simulated_kbd_type == KB_PC) {
1111                 kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
1112         } else if (kb8042->simulated_kbd_type == KB_USB) {
1113                 key = keytab_pc2usb[key_pos];
1114                 if (key != 0) {
1115                         kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
1116                 }
1117         }
1118 }
1119 
1120 /*
1121  * Called from interrupt handler when keyboard interrupt occurs.
1122  */
1123 static uint_t
1124 kb8042_intr(caddr_t arg)
1125 {
1126         uchar_t scancode;       /* raw scan code */
1127         int rc;
1128         struct kb8042 *kb8042 = (struct kb8042 *)arg;
1129 
1130         rc = DDI_INTR_UNCLAIMED;
1131 
1132         if (kb8042->init_state == KB8042_UNINITIALIZED)
1133                 return (DDI_INTR_UNCLAIMED);
1134 
1135         /* don't care if drv_setparm succeeds */
1136         (void) drv_setparm(SYSRINT, 1);
1137 
1138         while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
1139             != 0) {
1140                 rc = DDI_INTR_CLAIMED;
1141 
1142                 scancode = ddi_get8(kb8042->handle,
1143                     kb8042->addr + I8042_INT_INPUT_DATA);
1144 
1145                 kb8042_received_byte(kb8042, scancode);
1146         }
1147 
1148         return (rc);
1149 }
1150 
1151 static void
1152 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
1153 {
1154         struct copyresp *csp;
1155 
1156         csp = (struct copyresp *)mp->b_rptr;
1157         if (csp->cp_rval) {
1158                 freemsg(mp);
1159                 return;
1160         }
1161 
1162         switch (csp->cp_cmd) {
1163         default:
1164                 miocack(qp, mp, 0, 0);
1165                 break;
1166         }
1167 }
1168 
1169 static boolean_t
1170 kb8042_polled_keycheck(
1171     struct kbtrans_hardware *hw,
1172     int *key,
1173     enum keystate *state)
1174 {
1175         struct kb8042 *kb8042 = (struct kb8042 *)hw;
1176         int     scancode;
1177         boolean_t       legit;
1178         boolean_t       synthetic_release_needed;
1179 
1180         if (kb8042->polled_synthetic_release_pending) {
1181                 *key = kb8042->polled_synthetic_release_key;
1182                 *state = KEY_RELEASED;
1183                 kb8042->polled_synthetic_release_pending = B_FALSE;
1184                 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1185                 return (B_TRUE);
1186         }
1187 
1188         for (;;) {
1189                 if (ddi_get8(kb8042->handle,
1190                     kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
1191                         return (B_FALSE);
1192                 }
1193 
1194                 scancode = ddi_get8(kb8042->handle,
1195                     kb8042->addr + I8042_POLL_INPUT_DATA);
1196 
1197                 legit = KeyboardConvertScan(kb8042, scancode, key, state,
1198                     &synthetic_release_needed);
1199                 if (!legit) {
1200                         continue;
1201                 }
1202                 /*
1203                  * For the moment at least, we rely on hardware autorepeat
1204                  * for polled I/O autorepeat.  However, for coordination
1205                  * with the interrupt-driven code, maintain the last key
1206                  * pressed.
1207                  */
1208                 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1209 
1210                 /*
1211                  * This is a total hack to support two additional keys
1212                  * on Korean keyboards.  They report only on press, and
1213                  * so we synthesize a release.  Most likely this will
1214                  * never be important to polled  I/O, but if I do it
1215                  * "right" the first time it _won't_ be an issue.
1216                  */
1217                 if (synthetic_release_needed) {
1218                         kb8042->polled_synthetic_release_pending = B_TRUE;
1219                         kb8042->polled_synthetic_release_key = *key;
1220                 }
1221 
1222                 if (kb8042->simulated_kbd_type == KB_USB) {
1223                         *key = keytab_pc2usb[*key];
1224                 }
1225                 return (B_TRUE);
1226         }
1227 }
1228 
1229 static void
1230 kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1231 {
1232         kb8042->leds.desired = led_state;
1233 
1234         if (!polled)
1235                 mutex_enter(&kb8042->w_hw_mutex);
1236 
1237         if (kb8042->leds.desired != kb8042->leds.commanded) {
1238                 kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1239         }
1240 
1241         if (!polled)
1242                 mutex_exit(&kb8042->w_hw_mutex);
1243 }
1244 
1245 static void
1246 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
1247 {
1248         struct kb8042 *kb8042 = (struct kb8042 *)hw;
1249         kb8042_setled(kb8042, led_state, B_TRUE);
1250 }
1251 
1252 static void
1253 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1254 {
1255         struct kb8042 *kb8042 = (struct kb8042 *)hw;
1256         kb8042_setled(kb8042, led_state, B_FALSE);
1257 }
1258 
1259 
1260 static int
1261 kb8042_send_and_wait(struct kb8042 *kb8042, uint8_t u8, boolean_t polled)
1262 {
1263         uint8_t *outp = kb8042->addr +
1264             (polled ? I8042_POLL_OUTPUT_DATA : I8042_INT_OUTPUT_DATA);
1265         uint8_t *inavp = kb8042->addr +
1266             (polled ? I8042_POLL_INPUT_AVAIL : I8042_INT_INPUT_AVAIL);
1267         uint8_t *inp = kb8042->addr +
1268             (polled ? I8042_POLL_INPUT_DATA : I8042_INT_INPUT_DATA);
1269         uint8_t b;
1270         int ms_waited;
1271         int timedout;
1272         int expire;
1273         int retries = 0;
1274 
1275         do {
1276                 kb8042->acked = 0;
1277                 kb8042->need_retry = 0;
1278                 ms_waited = 0;          /* Zero it whether polled or not */
1279                 timedout = 0;
1280 
1281                 ddi_put8(kb8042->handle, outp, u8);
1282 
1283                 while (!kb8042->acked && !kb8042->need_retry && !timedout) {
1284 
1285                         if (polled) {
1286                                 if (ddi_get8(kb8042->handle, inavp)) {
1287                                         b = ddi_get8(kb8042->handle, inp);
1288                                         switch (b) {
1289                                         case KB_ACK:
1290                                                 kb8042->acked = 1;
1291                                                 break;
1292                                         case KB_RESEND:
1293                                                 kb8042->need_retry = 1;
1294                                                 break;
1295                                         default:
1296                                                 /*
1297                                                  * drop it: We should never
1298                                                  * get scancodes while
1299                                                  * we're in the middle of a
1300                                                  * command anyway.
1301                                                  */
1302 #ifdef DEBUG
1303                                                 cmn_err(CE_WARN, "!Unexpected "
1304                                                     " byte 0x%x", b);
1305 #endif
1306                                                 break;
1307                                         }
1308                                 }
1309 
1310                                 /*
1311                                  * Wait 1ms if an ACK wasn't received yet
1312                                  */
1313                                 if (!kb8042->acked) {
1314                                         drv_usecwait(1000);
1315                                         ms_waited++;
1316                                         if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
1317                                                 timedout = B_TRUE;
1318                                 }
1319                         } else {
1320                                 /* Interrupt-driven */
1321                                 expire = ddi_get_lbolt() +
1322                                     drv_usectohz(MAX_KB8042_WAIT_MAX_MS * 1000);
1323 
1324                                 /*
1325                                  * If cv_timedwait returned -1 and we neither
1326                                  * received an ACK nor a RETRY response, then
1327                                  * we timed out.
1328                                  */
1329                                 if (cv_timedwait(&kb8042->cmd_cv,
1330                                     &kb8042->w_hw_mutex, expire) == -1 &&
1331                                     !kb8042->acked && !kb8042->need_retry) {
1332                                         timedout = B_TRUE;
1333                                 }
1334                         }
1335 
1336                 }
1337         } while ((kb8042->need_retry || timedout) &&
1338             ++retries < MAX_KB8042_RETRIES);
1339 
1340         return (kb8042->acked);
1341 }
1342 
1343 /*
1344  * kb8042_send_to_keyboard should be called with w_hw_mutex held if
1345  * polled is FALSE.
1346  */
1347 static void
1348 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
1349 {
1350 
1351         /*
1352          * KB_SET_LED and KB_ENABLE are special commands which require blocking
1353          * other 8042 consumers while executing.
1354          *
1355          * Other commands/data are sent using the single put8 I/O access
1356          * function.
1357          */
1358         if (byte == KB_SET_LED) {
1359 
1360                 if (!polled) {
1361                         (void) ddi_get8(kb8042->handle, kb8042->addr +
1362                             I8042_LOCK);
1363                 }
1364 
1365                 if (kb8042_send_and_wait(kb8042, KB_SET_LED, polled)) {
1366                         /*
1367                          * Ignore return value, as there's nothing we can
1368                          * do about it if the SET LED command fails.
1369                          */
1370                         (void) kb8042_send_and_wait(kb8042,
1371                             kb8042_xlate_leds(kb8042->leds.desired), polled);
1372                 }
1373 
1374                 if (!polled) {
1375                         (void) ddi_get8(kb8042->handle, kb8042->addr +
1376                             I8042_UNLOCK);
1377                 }
1378                 kb8042->leds.commanded = kb8042->leds.desired;
1379 
1380         } else if (byte == KB_ENABLE) {
1381 
1382                 if (!polled) {
1383                         (void) ddi_get8(kb8042->handle, kb8042->addr +
1384                             I8042_LOCK);
1385                 }
1386 
1387                 (void) kb8042_send_and_wait(kb8042, KB_ENABLE, polled);
1388 
1389                 if (!polled) {
1390                         (void) ddi_get8(kb8042->handle, kb8042->addr +
1391                             I8042_UNLOCK);
1392                 }
1393 
1394         } else {
1395                 /* All other commands use the "normal" virtual output port */
1396                 if (polled) {
1397                         ddi_put8(kb8042->handle,
1398                             kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
1399                 } else {
1400                         ddi_put8(kb8042->handle,
1401                             kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
1402                 }
1403         }
1404 }
1405 
1406 /*
1407  * Wait until the keyboard is fully up, maybe.
1408  * We may be the first person to talk to the keyboard, in which case
1409  * it's patiently waiting to say "AA" to us to tell us it's up.
1410  * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
1411  * good bet that we've already spent that long getting to that point,
1412  * so we'll only wait long enough for the communications electronics to
1413  * run.
1414  */
1415 static void
1416 kb8042_wait_poweron(struct kb8042 *kb8042)
1417 {
1418         int cnt;
1419         int ready;
1420 
1421         /* wait for up to 250 ms for a response */
1422         for (cnt = 0; cnt < 250; cnt++) {
1423                 ready = ddi_get8(kb8042->handle,
1424                     kb8042->addr + I8042_INT_INPUT_AVAIL);
1425                 if (ready != 0)
1426                         break;
1427                 drv_usecwait(1000);
1428         }
1429 
1430         /*
1431          * If there's something pending, read and discard it.  If not,
1432          * assume things are OK anyway - maybe somebody else ate it
1433          * already.  (On a PC, the BIOS almost certainly did.)
1434          */
1435         if (ready != 0) {
1436                 (void) ddi_get8(kb8042->handle,
1437                     kb8042->addr + I8042_INT_INPUT_DATA);
1438         }
1439 }
1440 
1441 static int
1442 kb8042_xlate_leds(int led)
1443 {
1444         int res;
1445 
1446         res = 0;
1447 
1448         if (led & LED_NUM_LOCK)
1449                 res |= LED_NUM;
1450         if (led & LED_SCROLL_LOCK)
1451                 res |= LED_SCR;
1452         if (led & LED_CAPS_LOCK)
1453                 res |= LED_CAP;
1454 
1455         return (res);
1456 }
1457 
1458 /*ARGSUSED*/
1459 static void
1460 kb8042_get_initial_leds(
1461     struct kb8042 *kb8042,
1462     int *initial_leds,
1463     int *initial_led_mask)
1464 {
1465 #if defined(__i386) || defined(__amd64)
1466         extern caddr_t  p0_va;
1467         uint8_t         bios_kb_flag;
1468 
1469         bios_kb_flag = p0_va[BIOS_KB_FLAG];
1470 
1471         *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
1472         *initial_leds = 0;
1473         if (bios_kb_flag & BIOS_CAPS_STATE)
1474                 *initial_leds |= LED_CAPS_LOCK;
1475         if (bios_kb_flag & BIOS_NUM_STATE)
1476                 *initial_leds |= LED_NUM_LOCK;
1477         if (bios_kb_flag & BIOS_SCROLL_STATE)
1478                 *initial_leds |= LED_SCROLL_LOCK;
1479 #else
1480         *initial_leds = 0;
1481         *initial_led_mask = 0;
1482 #endif
1483 }
1484 
1485 static boolean_t
1486 kb8042_autorepeat_detect(
1487     struct kb8042 *kb8042,
1488     int key_pos,
1489     enum keystate state)
1490 {
1491         if (state == KEY_RELEASED) {
1492                 if (kb8042->kb_old_key_pos == key_pos)
1493                         kb8042->kb_old_key_pos = 0;
1494         } else {
1495                 if (kb8042->kb_old_key_pos == key_pos) {
1496                         return (B_TRUE);
1497                 }
1498                 kb8042->kb_old_key_pos = key_pos;
1499         }
1500         return (B_FALSE);
1501 }
1502 
1503 /* ARGSUSED */
1504 static void
1505 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
1506 {
1507         switch (cmd) {
1508         case KBD_CMD_BELL:
1509                 (void) beeper_on(BEEP_TYPE4);
1510                 break;
1511         case KBD_CMD_NOBELL:
1512                 (void) beeper_off();
1513                 break;
1514         }
1515 }
1516 
1517 
1518 /*
1519  * This is a pass-thru routine to get a character at poll time.
1520  */
1521 static int
1522 kb8042_polled_getchar(cons_polledio_arg_t arg)
1523 {
1524         struct kb8042   *kb8042;
1525 
1526         kb8042 = (struct kb8042 *)arg;
1527 
1528         return (kbtrans_getchar(kb8042->hw_kbtrans));
1529 }
1530 
1531 /*
1532  * This is a pass-thru routine to get a character at poll time.
1533  */
1534 static int
1535 kb8042_polled_ischar(cons_polledio_arg_t arg)
1536 {
1537         struct kb8042   *kb8042;
1538 
1539         kb8042 = (struct kb8042 *)arg;
1540 
1541         return (kbtrans_ischar(kb8042->hw_kbtrans));
1542 }