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