10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51762Slt200341 * Common Development and Distribution License (the "License"). 61762Slt200341 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 220Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 230Sstevel@tonic-gate /* All Rights Reserved */ 240Sstevel@tonic-gate 250Sstevel@tonic-gate /* 26*8960SJan.Setje-Eilers@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 270Sstevel@tonic-gate * Use is subject to license terms. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 307767SJohn.Beck@Sun.COM #define KEYMAP_SIZE_VARIABLE 317767SJohn.Beck@Sun.COM 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/errno.h> 340Sstevel@tonic-gate #include <sys/inline.h> 350Sstevel@tonic-gate #include <sys/termio.h> 360Sstevel@tonic-gate #include <sys/stropts.h> 370Sstevel@tonic-gate #include <sys/termios.h> 380Sstevel@tonic-gate #include <sys/stream.h> 390Sstevel@tonic-gate #include <sys/strtty.h> 400Sstevel@tonic-gate #include <sys/strsubr.h> 410Sstevel@tonic-gate #include <sys/strsun.h> 420Sstevel@tonic-gate #include <sys/ddi.h> 430Sstevel@tonic-gate #include <sys/sunddi.h> 440Sstevel@tonic-gate #include <sys/note.h> 450Sstevel@tonic-gate #include "sys/consdev.h" 460Sstevel@tonic-gate #include <sys/kbd.h> 470Sstevel@tonic-gate #include <sys/kbtrans.h> 480Sstevel@tonic-gate #include "kb8042.h" 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include <sys/i8042.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #include "sys/kbio.h" /* for KIOCSLAYOUT */ 530Sstevel@tonic-gate #include "sys/stat.h" 540Sstevel@tonic-gate #include "sys/reboot.h" 550Sstevel@tonic-gate #include <sys/promif.h> 560Sstevel@tonic-gate #include <sys/beep.h> 575129Smarx #include <sys/inttypes.h> 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * For any keyboard, there is a unique code describing the position 610Sstevel@tonic-gate * of the key on a keyboard. We refer to the code as "station number". 620Sstevel@tonic-gate * The following table is used to map the station numbers from ps2 630Sstevel@tonic-gate * AT/XT keyboards to that of a USB one. 64822Ssethg * 65822Ssethg * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which 66822Ssethg * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry 67822Ssethg * when in KB_PC mode. Note that this does not need to be made conditional 68822Ssethg * on the architecture for which this module is complied because there are no 69822Ssethg * keys that map to K8042_STOP on non-SPARC platforms. 700Sstevel@tonic-gate */ 710Sstevel@tonic-gate static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = { 720Sstevel@tonic-gate /* 0 */ 0, 53, 30, 31, 32, 33, 34, 35, 730Sstevel@tonic-gate /* 8 */ 36, 37, 38, 39, 45, 46, 137, 42, 740Sstevel@tonic-gate /* 16 */ 43, 20, 26, 8, 21, 23, 28, 24, 750Sstevel@tonic-gate /* 24 */ 12, 18, 19, 47, 48, 49, 57, 4, 760Sstevel@tonic-gate /* 32 */ 22, 7, 9, 10, 11, 13, 14, 15, 770Sstevel@tonic-gate /* 40 */ 51, 52, 50, 40, 225, 100, 29, 27, 780Sstevel@tonic-gate /* 48 */ 6, 25, 5, 17, 16, 54, 55, 56, 790Sstevel@tonic-gate /* 56 */ 135, 229, 224, 227, 226, 44, 230, 231, 800Sstevel@tonic-gate /* 64 */ 228, 101, 0, 0, 0, 0, 0, 0, 810Sstevel@tonic-gate /* 72 */ 0, 0, 0, 73, 76, 0, 0, 80, 820Sstevel@tonic-gate /* 80 */ 74, 77, 0, 82, 81, 75, 78, 0, 830Sstevel@tonic-gate /* 88 */ 0, 79, 83, 95, 92, 89, 0, 84, 840Sstevel@tonic-gate /* 96 */ 96, 93, 90, 98, 85, 97, 94, 91, 850Sstevel@tonic-gate /* 104 */ 99, 86, 87, 133, 88, 0, 41, 0, 860Sstevel@tonic-gate /* 112 */ 58, 59, 60, 61, 62, 63, 64, 65, 870Sstevel@tonic-gate /* 120 */ 66, 67, 68, 69, 70, 71, 72, 0, 880Sstevel@tonic-gate /* 128 */ 0, 0, 0, 139, 138, 136, 0, 0, 890Sstevel@tonic-gate /* 136 */ 0, 0, 0, 0, 0, 0, 0, 0, 900Sstevel@tonic-gate /* 144 */ 0, 0, 0, 0, 0, 0, 0, 0, 910Sstevel@tonic-gate /* 152 */ 0, 0, 0, 0, 0, 0, 0, 0, 92822Ssethg /* 160 */ 120, 0, 0, 0, 0, 0, 0, 0, 930Sstevel@tonic-gate /* 168 */ 0, 0, 0, 0, 0, 0, 0, 0, 940Sstevel@tonic-gate /* 176 */ 0, 0, 0, 0, 0, 0, 0, 0, 950Sstevel@tonic-gate /* 184 */ 0, 0, 0, 0, 0, 0, 0, 0, 960Sstevel@tonic-gate /* 192 */ 0, 0, 0, 0, 0, 0, 0, 0, 970Sstevel@tonic-gate /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0, 980Sstevel@tonic-gate /* 208 */ 0, 0, 0, 0, 0, 0, 0, 0, 990Sstevel@tonic-gate /* 216 */ 0, 0, 0, 0, 0, 0, 0, 0, 1000Sstevel@tonic-gate /* 224 */ 0, 0, 0, 0, 0, 0, 0, 0, 1010Sstevel@tonic-gate /* 232 */ 0, 0, 0, 0, 0, 0, 0, 0, 1020Sstevel@tonic-gate /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0, 1030Sstevel@tonic-gate /* 248 */ 0, 0, 0, 0 1040Sstevel@tonic-gate }; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * DEBUG (or KD_DEBUG for just this module) turns on a flag called 1080Sstevel@tonic-gate * kb8042_enable_debug_hotkey. If kb8042_enable_debug_hotkey is true, 1090Sstevel@tonic-gate * then the following hotkeys are enabled: 1100Sstevel@tonic-gate * F10 - turn on debugging "normal" translations 1110Sstevel@tonic-gate * F9 - turn on debugging "getchar" translations 1120Sstevel@tonic-gate * F8 - turn on "low level" debugging 1130Sstevel@tonic-gate * F7 - turn on terse press/release debugging 1140Sstevel@tonic-gate * F1 - turn off all debugging 1150Sstevel@tonic-gate * The default value for kb8042_enable_debug_hotkey is false, disabling 1160Sstevel@tonic-gate * these hotkeys. 1170Sstevel@tonic-gate */ 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate #if defined(DEBUG) || defined(lint) 1200Sstevel@tonic-gate #define KD_DEBUG 1210Sstevel@tonic-gate #endif 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate #ifdef KD_DEBUG 1240Sstevel@tonic-gate boolean_t kb8042_enable_debug_hotkey = B_FALSE; 1250Sstevel@tonic-gate boolean_t kb8042_debug = B_FALSE; 1260Sstevel@tonic-gate boolean_t kb8042_getchar_debug = B_FALSE; 1270Sstevel@tonic-gate boolean_t kb8042_low_level_debug = B_FALSE; 1280Sstevel@tonic-gate boolean_t kb8042_pressrelease_debug = B_FALSE; 1290Sstevel@tonic-gate static void kb8042_debug_hotkey(int scancode); 1300Sstevel@tonic-gate #endif 1310Sstevel@tonic-gate 132822Ssethg #ifdef __sparc 133822Ssethg #define USECS_PER_WAIT 100 134822Ssethg #define MAX_WAIT_USECS 100000 /* in usecs = 100 ms */ 135822Ssethg #define MIN_DELAY_USECS USECS_PER_WAIT 136822Ssethg 137822Ssethg boolean_t kb8042_warn_unknown_scanset = B_TRUE; 138822Ssethg int kb8042_default_scanset = 2; 139822Ssethg 140822Ssethg #endif 141822Ssethg 1420Sstevel@tonic-gate enum state_return { STATE_NORMAL, STATE_INTERNAL }; 1430Sstevel@tonic-gate 144822Ssethg static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume); 1450Sstevel@tonic-gate static uint_t kb8042_intr(caddr_t arg); 1460Sstevel@tonic-gate static void kb8042_wait_poweron(struct kb8042 *kb8042); 1470Sstevel@tonic-gate static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t); 1480Sstevel@tonic-gate static int kb8042_xlate_leds(int); 149822Ssethg static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled); 1500Sstevel@tonic-gate static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state); 1510Sstevel@tonic-gate static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state); 1520Sstevel@tonic-gate static boolean_t kb8042_polled_keycheck( 1530Sstevel@tonic-gate struct kbtrans_hardware *hw, int *key, 1540Sstevel@tonic-gate enum keystate *state); 1550Sstevel@tonic-gate static void kb8042_get_initial_leds(struct kb8042 *, int *, int *); 1560Sstevel@tonic-gate static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos, 1570Sstevel@tonic-gate enum keystate state); 1580Sstevel@tonic-gate static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd); 1590Sstevel@tonic-gate static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *); 1600Sstevel@tonic-gate static void kb8042_iocdatamsg(queue_t *, mblk_t *); 1610Sstevel@tonic-gate static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate); 1621762Slt200341 static int kb8042_polled_ischar(cons_polledio_arg_t arg); 1631762Slt200341 static int kb8042_polled_getchar(cons_polledio_arg_t arg); 164822Ssethg static void kb8042_cleanup(struct kb8042 *kb8042); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate static struct kbtrans_callbacks kb8042_callbacks = { 1670Sstevel@tonic-gate kb8042_streams_setled, 1680Sstevel@tonic-gate kb8042_polled_setled, 1690Sstevel@tonic-gate kb8042_polled_keycheck, 1700Sstevel@tonic-gate }; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate extern struct keyboard keyindex_pc; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate #define DRIVER_NAME(dip) ddi_driver_name(dip) 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate static char module_name[] = "kb8042"; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, 1790Sstevel@tonic-gate cred_t *credp); 1800Sstevel@tonic-gate static int kb8042_close(queue_t *qp, int flag, cred_t *credp); 1810Sstevel@tonic-gate static int kb8042_wsrv(); 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate struct module_info kb8042_sinfo = { 1840Sstevel@tonic-gate 42, /* Module ID */ 1850Sstevel@tonic-gate module_name, 1860Sstevel@tonic-gate 0, 32, /* Minimum and maximum packet sizes */ 1870Sstevel@tonic-gate 256, 128 /* High and low water marks */ 1880Sstevel@tonic-gate }; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate static struct qinit kb8042_rinit = { 1910Sstevel@tonic-gate NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL 1920Sstevel@tonic-gate }; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate static struct qinit kb8042_winit = { 1950Sstevel@tonic-gate putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL 1960Sstevel@tonic-gate }; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate struct streamtab 1990Sstevel@tonic-gate kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL }; 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate struct kb8042 Kdws = {0}; 202822Ssethg static dev_info_t *kb8042_dip = NULL; 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 2050Sstevel@tonic-gate void **result); 2060Sstevel@tonic-gate static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t); 207822Ssethg static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate static struct cb_ops cb_kb8042_ops = { 2100Sstevel@tonic-gate nulldev, /* cb_open */ 2110Sstevel@tonic-gate nulldev, /* cb_close */ 2120Sstevel@tonic-gate nodev, /* cb_strategy */ 2130Sstevel@tonic-gate nodev, /* cb_print */ 2140Sstevel@tonic-gate nodev, /* cb_dump */ 2150Sstevel@tonic-gate nodev, /* cb_read */ 2160Sstevel@tonic-gate nodev, /* cb_write */ 2170Sstevel@tonic-gate nodev, /* cb_ioctl */ 2180Sstevel@tonic-gate nodev, /* cb_devmap */ 2190Sstevel@tonic-gate nodev, /* cb_mmap */ 2200Sstevel@tonic-gate nodev, /* cb_segmap */ 2210Sstevel@tonic-gate nochpoll, /* cb_chpoll */ 2220Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 2230Sstevel@tonic-gate &kb8042_str_info, /* cb_stream */ 2240Sstevel@tonic-gate D_MP 2250Sstevel@tonic-gate }; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate struct dev_ops kb8042_ops = { 2280Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 2290Sstevel@tonic-gate 0, /* devo_refcnt */ 2300Sstevel@tonic-gate kb8042_getinfo, /* devo_getinfo */ 2310Sstevel@tonic-gate nulldev, /* devo_identify */ 2320Sstevel@tonic-gate nulldev, /* devo_probe */ 2330Sstevel@tonic-gate kb8042_attach, /* devo_attach */ 234822Ssethg kb8042_detach, /* devo_detach */ 2350Sstevel@tonic-gate nodev, /* devo_reset */ 2360Sstevel@tonic-gate &cb_kb8042_ops, /* devo_cb_ops */ 2377656SSherry.Moore@Sun.COM (struct bus_ops *)NULL, /* devo_bus_ops */ 2387656SSherry.Moore@Sun.COM NULL, /* devo_power */ 2397685SSeth.Goldberg@Sun.COM ddi_quiesce_not_needed, /* devo_quiesce */ 2400Sstevel@tonic-gate }; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * This is the loadable module wrapper. 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate #include <sys/modctl.h> 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate /* 2490Sstevel@tonic-gate * Module linkage information for the kernel. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate static struct modldrv modldrv = { 2520Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */ 2537658SSeth.Goldberg@Sun.COM "PS/2 keyboard driver", 2540Sstevel@tonic-gate &kb8042_ops, /* driver ops */ 2550Sstevel@tonic-gate }; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate static struct modlinkage modlinkage = { 2580Sstevel@tonic-gate MODREV_1, 2590Sstevel@tonic-gate (void *) &modldrv, 2600Sstevel@tonic-gate NULL 2610Sstevel@tonic-gate }; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate int 2640Sstevel@tonic-gate _init(void) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate int rv; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate rv = mod_install(&modlinkage); 2690Sstevel@tonic-gate return (rv); 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate int 2740Sstevel@tonic-gate _fini(void) 2750Sstevel@tonic-gate { 2760Sstevel@tonic-gate return (mod_remove(&modlinkage)); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate int 2800Sstevel@tonic-gate _info(struct modinfo *modinfop) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 285822Ssethg #ifdef __sparc 286822Ssethg static boolean_t 287822Ssethg kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled) 288822Ssethg { 289822Ssethg int i; 290822Ssethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL : 291822Ssethg I8042_INT_INPUT_AVAIL; 292822Ssethg int reps = timeout_usec / USECS_PER_WAIT; 293822Ssethg 294822Ssethg for (i = 0; i < reps; i++) { 295822Ssethg if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0) 296822Ssethg return (B_TRUE); 297822Ssethg 298822Ssethg if (i < (reps - 1)) 299822Ssethg drv_usecwait(USECS_PER_WAIT); 300822Ssethg } 301822Ssethg return (B_FALSE); 302822Ssethg } 303822Ssethg 304822Ssethg static void 305822Ssethg kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled) 306822Ssethg { 307822Ssethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA : 308822Ssethg I8042_INT_INPUT_DATA; 309822Ssethg 310822Ssethg while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) { 311822Ssethg (void) ddi_get8(kb8042->handle, kb8042->addr + port); 312822Ssethg } 313822Ssethg } 314822Ssethg 315822Ssethg static boolean_t 316822Ssethg kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect, 317822Ssethg boolean_t polled, int timeout, int *error, uint8_t *got) 318822Ssethg { 319822Ssethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA : 320822Ssethg I8042_INT_INPUT_DATA; 321822Ssethg uint8_t datab; 322822Ssethg int err; 323822Ssethg boolean_t rval; 324822Ssethg 325822Ssethg kb8042_send_to_keyboard(kb8042, send, polled); 326822Ssethg 327822Ssethg if (kb8042_is_input_avail(kb8042, timeout, polled)) { 328822Ssethg err = 0; 329822Ssethg datab = ddi_get8(kb8042->handle, kb8042->addr + port); 330822Ssethg rval = ((datab == expect) ? B_TRUE : B_FALSE); 331822Ssethg } else { 332822Ssethg err = EAGAIN; 333822Ssethg rval = B_FALSE; 334822Ssethg } 335822Ssethg 336822Ssethg if (error != NULL) 337822Ssethg *error = err; 338822Ssethg if (got != NULL) 339822Ssethg *got = datab; 340822Ssethg return (rval); 341822Ssethg } 342822Ssethg 343822Ssethg static const char * 344822Ssethg kb8042_error_string(int errcode) 345822Ssethg { 346822Ssethg switch (errcode) { 347822Ssethg case EAGAIN: 348822Ssethg return ("Timed out"); 349822Ssethg default: 350822Ssethg return ("Unknown error"); 351822Ssethg } 352822Ssethg } 353822Ssethg 354822Ssethg static int 355822Ssethg kb8042_read_scanset(struct kb8042 *kb8042, boolean_t polled) 356822Ssethg { 357822Ssethg int scanset = -1; 358822Ssethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA : 359822Ssethg I8042_INT_INPUT_DATA; 360822Ssethg int err; 361822Ssethg uint8_t got; 362822Ssethg 363822Ssethg kb8042_clear_input_buffer(kb8042, B_TRUE); 364822Ssethg 365822Ssethg /* 366822Ssethg * Send a "change scan code set" command to the keyboard. 367822Ssethg * It should respond with an ACK. 368822Ssethg */ 369822Ssethg if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, polled, 370822Ssethg MAX_WAIT_USECS, &err, &got) != B_TRUE) { 371822Ssethg goto fail_read_scanset; 372822Ssethg } 373822Ssethg 374822Ssethg /* 3757658SSeth.Goldberg@Sun.COM * Send a 0. The keyboard should ACK the 0, then it should send the 3767658SSeth.Goldberg@Sun.COM * scan code set in use. 377822Ssethg */ 378822Ssethg if (kb8042_send_and_expect(kb8042, 0, KB_ACK, polled, 379822Ssethg MAX_WAIT_USECS, &err, &got) != B_TRUE) { 380822Ssethg goto fail_read_scanset; 381822Ssethg } 382822Ssethg 383822Ssethg /* 384822Ssethg * The next input byte from the keyboard should be the scan code 385822Ssethg * set in use, though some keyboards like to send a few more acks 386822Ssethg * just for fun, so blow past those to get the keyboard scan code. 387822Ssethg */ 388822Ssethg while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) && 3895129Smarx (scanset = ddi_get8(kb8042->handle, kb8042->addr + port)) 3905129Smarx == KB_ACK) 391822Ssethg ; 392822Ssethg 393822Ssethg #ifdef KD_DEBUG 394822Ssethg cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.", 395822Ssethg scanset); 396822Ssethg #endif 397822Ssethg 398822Ssethg return (scanset); 399822Ssethg 400822Ssethg fail_read_scanset: 401822Ssethg #ifdef KD_DEBUG 402822Ssethg if (err == 0) 403822Ssethg cmn_err(CE_NOTE, "Could not read current scan set from " 404822Ssethg "keyboard: %s. (Expected 0x%x, but got 0x%x).", 405822Ssethg kb8042_error_string(err), KB_ACK, got); 406822Ssethg else 407822Ssethg cmn_err(CE_NOTE, "Could not read current scan set from " 408822Ssethg "keyboard: %s.", kb8042_error_string(err)); 409822Ssethg #endif 410822Ssethg return (-1); 411822Ssethg } 412822Ssethg #endif 413822Ssethg 4140Sstevel@tonic-gate static int 4150Sstevel@tonic-gate kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 4160Sstevel@tonic-gate { 4170Sstevel@tonic-gate int rc; 418822Ssethg int scanset; 419822Ssethg int leds; 4200Sstevel@tonic-gate 421822Ssethg struct kb8042 *kb8042 = &Kdws; 4220Sstevel@tonic-gate static ddi_device_acc_attr_t attr = { 4230Sstevel@tonic-gate DDI_DEVICE_ATTR_V0, 4240Sstevel@tonic-gate DDI_NEVERSWAP_ACC, 4250Sstevel@tonic-gate DDI_STRICTORDER_ACC, 4260Sstevel@tonic-gate }; 4270Sstevel@tonic-gate 428822Ssethg switch (cmd) { 429822Ssethg case DDI_RESUME: 430822Ssethg leds = kb8042->leds.commanded; 431822Ssethg kb8042->w_init = 0; 432822Ssethg kb8042_init(kb8042, B_TRUE); 433822Ssethg kb8042_setled(kb8042, leds, B_FALSE); 4345295Srandyf mutex_enter(&kb8042->w_hw_mutex); 4355295Srandyf kb8042->suspended = B_FALSE; 4365295Srandyf if (kb8042->w_qp != NULL) { 4375295Srandyf enableok(WR(kb8042->w_qp)); 4385295Srandyf qenable(WR(kb8042->w_qp)); 4395295Srandyf } 4405295Srandyf cv_broadcast(&kb8042->suspend_cv); 4415295Srandyf mutex_exit(&kb8042->w_hw_mutex); 442822Ssethg return (DDI_SUCCESS); 4430Sstevel@tonic-gate 444822Ssethg case DDI_ATTACH: 445822Ssethg if (kb8042_dip != NULL) 446822Ssethg return (DDI_FAILURE); 447822Ssethg /* The rest of the function is for attach */ 448822Ssethg break; 4490Sstevel@tonic-gate 450822Ssethg default: 451822Ssethg return (DDI_FAILURE); 452822Ssethg } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate kb8042->debugger.mod1 = 58; /* Left Ctrl */ 4550Sstevel@tonic-gate kb8042->debugger.mod2 = 60; /* Left Alt */ 4560Sstevel@tonic-gate kb8042->debugger.trigger = 33; /* D */ 4570Sstevel@tonic-gate kb8042->debugger.mod1_down = B_FALSE; 4580Sstevel@tonic-gate kb8042->debugger.mod2_down = B_FALSE; 4590Sstevel@tonic-gate kb8042->debugger.enabled = B_FALSE; 4600Sstevel@tonic-gate 461822Ssethg kb8042_dip = devi; 462822Ssethg kb8042->init_state = KB8042_UNINITIALIZED; 463822Ssethg 4640Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_FALSE; 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0, 4675129Smarx DDI_NT_KEYBOARD, 0) == DDI_FAILURE) { 468822Ssethg goto failure; 4690Sstevel@tonic-gate } 470822Ssethg 471822Ssethg kb8042->init_state |= KB8042_MINOR_NODE_CREATED; 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr, 4745129Smarx (offset_t)0, (offset_t)0, &attr, &kb8042->handle); 4750Sstevel@tonic-gate if (rc != DDI_SUCCESS) { 4760Sstevel@tonic-gate #if defined(KD_DEBUG) 4770Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: can't map registers"); 4780Sstevel@tonic-gate #endif 479822Ssethg goto failure; 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 482822Ssethg kb8042->init_state |= KB8042_REGS_MAPPED; 483822Ssethg 4840Sstevel@tonic-gate if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) != 4855129Smarx DDI_SUCCESS) { 4860Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie"); 487822Ssethg goto failure; 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock); 4915295Srandyf cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL); 4925295Srandyf cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL); 493822Ssethg kb8042->init_state |= KB8042_HW_MUTEX_INITTED; 494822Ssethg 495822Ssethg kb8042_init(kb8042, B_FALSE); 496822Ssethg 497822Ssethg #ifdef __sparc 498822Ssethg /* Detect the scan code set currently in use */ 499822Ssethg scanset = kb8042_read_scanset(kb8042, B_TRUE); 500822Ssethg 501822Ssethg if (scanset < 0 && kb8042_warn_unknown_scanset) { 502822Ssethg 503822Ssethg cmn_err(CE_WARN, "Cannot determine keyboard scan code set "); 504822Ssethg cmn_err(CE_CONT, "(is the keyboard plugged in?). "); 505822Ssethg cmn_err(CE_CONT, "Defaulting to scan code set %d. If the " 506822Ssethg "keyboard does not ", kb8042_default_scanset); 507822Ssethg cmn_err(CE_CONT, "work properly, add " 508822Ssethg "`set kb8042:kb8042_default_scanset=%d' to /etc/system ", 509822Ssethg (kb8042_default_scanset == 1) ? 2 : 1); 510822Ssethg cmn_err(CE_CONT, "(via network or with a USB keyboard) and " 511822Ssethg "restart the system. If you "); 512822Ssethg cmn_err(CE_CONT, "do not want to see this message in the " 513822Ssethg "future, add "); 514822Ssethg cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' " 515822Ssethg "to /etc/system.\n"); 516822Ssethg 517822Ssethg /* Use the default scan code set. */ 518822Ssethg scanset = kb8042_default_scanset; 519822Ssethg } 520822Ssethg #else 521822Ssethg /* x86 systems use scan code set 1 -- no detection required */ 522822Ssethg scanset = 1; 523822Ssethg #endif 524822Ssethg if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) { 525822Ssethg cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: " 526822Ssethg "Unknown scan code set `%d'.", scanset); 527822Ssethg /* Scan code set is not supported */ 528822Ssethg goto failure; 529822Ssethg } 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate /* 5320Sstevel@tonic-gate * Turn on interrupts... 5330Sstevel@tonic-gate */ 5340Sstevel@tonic-gate if (ddi_add_intr(devi, 0, 5355129Smarx &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL, 5365129Smarx kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) { 5370Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt"); 538822Ssethg goto failure; 5390Sstevel@tonic-gate } 5400Sstevel@tonic-gate 541822Ssethg kb8042->init_state |= KB8042_INTR_ADDED; 542822Ssethg 5430Sstevel@tonic-gate ddi_report_dev(devi); 544822Ssethg 5450Sstevel@tonic-gate #ifdef KD_DEBUG 5467658SSeth.Goldberg@Sun.COM cmn_err(CE_CONT, "?%s instance #%d READY\n", 5477656SSherry.Moore@Sun.COM DRIVER_NAME(devi), ddi_get_instance(devi)); 5480Sstevel@tonic-gate #endif 549822Ssethg 5500Sstevel@tonic-gate return (DDI_SUCCESS); 551822Ssethg 552822Ssethg failure: 553822Ssethg kb8042_cleanup(kb8042); 554822Ssethg return (DDI_FAILURE); 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 557822Ssethg static int 558822Ssethg kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 559822Ssethg { 560822Ssethg struct kb8042 *kb8042 = &Kdws; 561822Ssethg 562822Ssethg switch (cmd) { 563822Ssethg case DDI_SUSPEND: 5645295Srandyf mutex_enter(&kb8042->w_hw_mutex); 5655295Srandyf ASSERT(kb8042->ops >= 0); 5665295Srandyf while (kb8042->ops > 0) 5675295Srandyf cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex); 5685295Srandyf kb8042->suspended = B_TRUE; 5695295Srandyf mutex_exit(&kb8042->w_hw_mutex); 570822Ssethg return (DDI_SUCCESS); 571822Ssethg 572822Ssethg case DDI_DETACH: 573822Ssethg /* If someone has a stream open, fail to detach */ 574822Ssethg if (kb8042->w_qp != NULL) 575822Ssethg return (DDI_FAILURE); 576822Ssethg 577822Ssethg ASSERT(kb8042_dip == dip); 578822Ssethg 579822Ssethg kb8042_cleanup(kb8042); 580822Ssethg 581822Ssethg return (DDI_SUCCESS); 582822Ssethg 583822Ssethg default: 584822Ssethg return (DDI_FAILURE); 585822Ssethg } 586822Ssethg } 587822Ssethg 5880Sstevel@tonic-gate /*ARGSUSED*/ 5890Sstevel@tonic-gate static int 5900Sstevel@tonic-gate kb8042_getinfo( 5910Sstevel@tonic-gate dev_info_t *dip, 5920Sstevel@tonic-gate ddi_info_cmd_t infocmd, 5930Sstevel@tonic-gate void *arg, 5940Sstevel@tonic-gate void **result) 5950Sstevel@tonic-gate { 5960Sstevel@tonic-gate register int error; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate switch (infocmd) { 5990Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 6000Sstevel@tonic-gate if (kb8042_dip == NULL) { 6010Sstevel@tonic-gate error = DDI_FAILURE; 6020Sstevel@tonic-gate } else { 6030Sstevel@tonic-gate *result = (void *) kb8042_dip; 6040Sstevel@tonic-gate error = DDI_SUCCESS; 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate break; 6070Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 6080Sstevel@tonic-gate *result = (void *)0; 6090Sstevel@tonic-gate error = DDI_SUCCESS; 6100Sstevel@tonic-gate break; 6110Sstevel@tonic-gate default: 6120Sstevel@tonic-gate error = DDI_FAILURE; 6130Sstevel@tonic-gate break; 6140Sstevel@tonic-gate } 6150Sstevel@tonic-gate return (error); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate static void 619822Ssethg kb8042_cleanup(struct kb8042 *kb8042) 620822Ssethg { 621845Ssethg ASSERT(kb8042_dip != NULL); 622822Ssethg 6235295Srandyf if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) { 6245295Srandyf cv_destroy(&kb8042->suspend_cv); 6255295Srandyf cv_destroy(&kb8042->ops_cv); 626822Ssethg mutex_destroy(&kb8042->w_hw_mutex); 6275295Srandyf } 628822Ssethg 629822Ssethg if (kb8042->init_state & KB8042_INTR_ADDED) 630822Ssethg ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock); 631822Ssethg 632822Ssethg if (kb8042->init_state & KB8042_REGS_MAPPED) 633822Ssethg ddi_regs_map_free(&kb8042->handle); 634822Ssethg 635822Ssethg if (kb8042->init_state & KB8042_MINOR_NODE_CREATED) 636822Ssethg ddi_remove_minor_node(kb8042_dip, NULL); 637822Ssethg 638822Ssethg kb8042->init_state = KB8042_UNINITIALIZED; 639845Ssethg kb8042_dip = NULL; 640822Ssethg } 641822Ssethg 642822Ssethg static void 643822Ssethg kb8042_init(struct kb8042 *kb8042, boolean_t from_resume) 6440Sstevel@tonic-gate { 6450Sstevel@tonic-gate if (kb8042->w_init) 6460Sstevel@tonic-gate return; 6470Sstevel@tonic-gate 648822Ssethg if (!from_resume) { 649822Ssethg kb8042->w_kblayout = 0; /* Default to US */ 650822Ssethg kb8042->w_qp = (queue_t *)NULL; 651822Ssethg kb8042->simulated_kbd_type = KB_PC; 652822Ssethg kb8042->leds.commanded = -1; /* Unknown initial state */ 653822Ssethg kb8042->leds.desired = -1; /* Unknown initial state */ 654822Ssethg } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate kb8042_wait_poweron(kb8042); 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate kb8042->kb_old_key_pos = 0; 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* Set up the command state machine and start it running. */ 6610Sstevel@tonic-gate kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_FALSE); 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate kb8042->w_init++; 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */ 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate /*ARGSUSED2*/ 6690Sstevel@tonic-gate static int 6700Sstevel@tonic-gate kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp) 6710Sstevel@tonic-gate { 6720Sstevel@tonic-gate struct kb8042 *kb8042; 6730Sstevel@tonic-gate int err; 6740Sstevel@tonic-gate int initial_leds; 6750Sstevel@tonic-gate int initial_led_mask; 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate kb8042 = &Kdws; 6780Sstevel@tonic-gate 6795295Srandyf mutex_enter(&kb8042->w_hw_mutex); 6805295Srandyf while (kb8042->suspended) { 6815295Srandyf if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) == 6825295Srandyf 0) { 6835295Srandyf mutex_exit(&kb8042->w_hw_mutex); 6845295Srandyf return (EINTR); 6855295Srandyf } 6865295Srandyf } 6875295Srandyf 6880Sstevel@tonic-gate kb8042->w_dev = *devp; 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (qp->q_ptr) { 6915295Srandyf mutex_exit(&kb8042->w_hw_mutex); 6920Sstevel@tonic-gate return (0); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate qp->q_ptr = (caddr_t)kb8042; 6950Sstevel@tonic-gate WR(qp)->q_ptr = qp->q_ptr; 6960Sstevel@tonic-gate if (!kb8042->w_qp) 6970Sstevel@tonic-gate kb8042->w_qp = qp; 6980Sstevel@tonic-gate 6995295Srandyf ASSERT(kb8042->ops >= 0); 7005295Srandyf kb8042->ops++; 7015295Srandyf mutex_exit(&kb8042->w_hw_mutex); 7025295Srandyf 7030Sstevel@tonic-gate kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask); 7040Sstevel@tonic-gate err = kbtrans_streams_init(qp, sflag, credp, 7055129Smarx (struct kbtrans_hardware *)kb8042, &kb8042_callbacks, 7065129Smarx &kb8042->hw_kbtrans, 7075129Smarx initial_leds, initial_led_mask); 7080Sstevel@tonic-gate if (err != 0) 7090Sstevel@tonic-gate return (err); 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1; 7140Sstevel@tonic-gate kb8042->polledio.cons_polledio_argument = 7155129Smarx (cons_polledio_arg_t)kb8042; 7160Sstevel@tonic-gate kb8042->polledio.cons_polledio_putchar = NULL; 7170Sstevel@tonic-gate kb8042->polledio.cons_polledio_getchar = 7185129Smarx (int (*)(cons_polledio_arg_t))kb8042_polled_getchar; 7190Sstevel@tonic-gate kb8042->polledio.cons_polledio_ischar = 7205129Smarx (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar; 7210Sstevel@tonic-gate kb8042->polledio.cons_polledio_enter = NULL; 7220Sstevel@tonic-gate kb8042->polledio.cons_polledio_exit = NULL; 7230Sstevel@tonic-gate kb8042->polledio.cons_polledio_setled = 7245129Smarx (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled; 7250Sstevel@tonic-gate kb8042->polledio.cons_polledio_keycheck = 7265129Smarx (boolean_t (*)(cons_polledio_arg_t, int *, 7275129Smarx enum keystate *))kb8042_polled_keycheck; 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate qprocson(qp); 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate kbtrans_streams_enable(kb8042->hw_kbtrans); 7320Sstevel@tonic-gate 7335295Srandyf mutex_enter(&kb8042->w_hw_mutex); 7345295Srandyf ASSERT(kb8042->ops > 0); 7355295Srandyf kb8042->ops--; 7365295Srandyf if (kb8042->ops == 0) 7375295Srandyf cv_broadcast(&kb8042->ops_cv); 7385295Srandyf mutex_exit(&kb8042->w_hw_mutex); 7395295Srandyf 7400Sstevel@tonic-gate return (0); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate /*ARGSUSED1*/ 7440Sstevel@tonic-gate static int 7450Sstevel@tonic-gate kb8042_close(queue_t *qp, int flag, cred_t *credp) 7460Sstevel@tonic-gate { 7470Sstevel@tonic-gate struct kb8042 *kb8042; 7480Sstevel@tonic-gate 7495129Smarx /* If a beep is in progress, stop that */ 7505129Smarx (void) beeper_off(); 7515129Smarx 7520Sstevel@tonic-gate kb8042 = (struct kb8042 *)qp->q_ptr; 7530Sstevel@tonic-gate 7545295Srandyf mutex_enter(&kb8042->w_hw_mutex); 7555295Srandyf while (kb8042->suspended) { 7565295Srandyf if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) == 7575295Srandyf 0) { 7585295Srandyf mutex_exit(&kb8042->w_hw_mutex); 7595295Srandyf return (EINTR); 7605295Srandyf } 7615295Srandyf } 7625295Srandyf 7635295Srandyf ASSERT(kb8042->ops >= 0); 7645295Srandyf kb8042->ops++; 7655295Srandyf mutex_exit(&kb8042->w_hw_mutex); 7665295Srandyf 767672Sry162471 (void) kbtrans_streams_fini(kb8042->hw_kbtrans); 768672Sry162471 7690Sstevel@tonic-gate kb8042->w_qp = (queue_t *)NULL; 7700Sstevel@tonic-gate qprocsoff(qp); 7710Sstevel@tonic-gate 7725295Srandyf mutex_enter(&kb8042->w_hw_mutex); 7735295Srandyf ASSERT(kb8042->ops > 0); 7745295Srandyf kb8042->ops--; 7755295Srandyf if (kb8042->ops == 0) 7765295Srandyf cv_broadcast(&kb8042->ops_cv); 7775295Srandyf mutex_exit(&kb8042->w_hw_mutex); 7785295Srandyf 7790Sstevel@tonic-gate return (0); 7800Sstevel@tonic-gate } 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate static int 7830Sstevel@tonic-gate kb8042_wsrv(queue_t *qp) 7840Sstevel@tonic-gate { 7850Sstevel@tonic-gate struct kb8042 *kb8042; 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate mblk_t *mp; 7885295Srandyf boolean_t suspended; 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate kb8042 = (struct kb8042 *)qp->q_ptr; 7910Sstevel@tonic-gate 7925295Srandyf mutex_enter(&kb8042->w_hw_mutex); 7935295Srandyf suspended = kb8042->suspended; 7945295Srandyf ASSERT(kb8042->ops >= 0); 7955295Srandyf if (!suspended) 7965295Srandyf kb8042->ops++; 7975295Srandyf mutex_exit(&kb8042->w_hw_mutex); 7985295Srandyf 7995295Srandyf #ifdef NO_KB_DEBUG 8005295Srandyf while (!suspended && (mp = getq(qp)) != NULL) { 8015295Srandyf #else 8025295Srandyf /* 8035295Srandyf * Not taking keyboard input while suspending can make debugging 8045295Srandyf * difficult. However, we still do the ops counting so that we 8055295Srandyf * don't suspend at a bad time. 8065295Srandyf */ 8070Sstevel@tonic-gate while ((mp = getq(qp))) { 8085295Srandyf #endif 8090Sstevel@tonic-gate switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) { 8100Sstevel@tonic-gate case KBTRANS_MESSAGE_HANDLED: 8110Sstevel@tonic-gate continue; 8120Sstevel@tonic-gate case KBTRANS_MESSAGE_NOT_HANDLED: 8130Sstevel@tonic-gate break; 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate switch (mp->b_datap->db_type) { 8160Sstevel@tonic-gate case M_IOCTL: 8170Sstevel@tonic-gate kb8042_ioctlmsg(kb8042, qp, mp); 8180Sstevel@tonic-gate continue; 8190Sstevel@tonic-gate case M_IOCDATA: 8200Sstevel@tonic-gate kb8042_iocdatamsg(qp, mp); 8210Sstevel@tonic-gate continue; 8220Sstevel@tonic-gate case M_DELAY: 8230Sstevel@tonic-gate case M_STARTI: 8240Sstevel@tonic-gate case M_STOPI: 8250Sstevel@tonic-gate case M_READ: /* ignore, no buffered data */ 8260Sstevel@tonic-gate freemsg(mp); 8270Sstevel@tonic-gate continue; 8280Sstevel@tonic-gate case M_FLUSH: 8290Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 8300Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) 8310Sstevel@tonic-gate qreply(qp, mp); 8320Sstevel@tonic-gate else 8330Sstevel@tonic-gate freemsg(mp); 8340Sstevel@tonic-gate continue; 8350Sstevel@tonic-gate default: 8360Sstevel@tonic-gate cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x", 8375129Smarx mp->b_datap->db_type); 8380Sstevel@tonic-gate freemsg(mp); 8390Sstevel@tonic-gate continue; 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate } 8425295Srandyf 8435295Srandyf mutex_enter(&kb8042->w_hw_mutex); 8445295Srandyf if (!suspended) { 8455295Srandyf ASSERT(kb8042->ops > 0); 8465295Srandyf kb8042->ops--; 8475295Srandyf if (kb8042->ops == 0) 8485295Srandyf cv_broadcast(&kb8042->ops_cv); 8495295Srandyf } 8505295Srandyf mutex_exit(&kb8042->w_hw_mutex); 8515295Srandyf 8520Sstevel@tonic-gate return (0); 8530Sstevel@tonic-gate } 8540Sstevel@tonic-gate 8550Sstevel@tonic-gate static void 8560Sstevel@tonic-gate kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp) 8570Sstevel@tonic-gate { 8580Sstevel@tonic-gate struct iocblk *iocp; 8590Sstevel@tonic-gate mblk_t *datap; 8600Sstevel@tonic-gate int error; 8610Sstevel@tonic-gate int tmp; 8625129Smarx int cycles; 8635129Smarx int frequency; 8645129Smarx int msecs; 8650Sstevel@tonic-gate 8660Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate switch (iocp->ioc_cmd) { 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate case CONSOPENPOLLEDIO: 8710Sstevel@tonic-gate error = miocpullup(mp, sizeof (struct cons_polledio *)); 8720Sstevel@tonic-gate if (error != 0) { 8730Sstevel@tonic-gate miocnak(qp, mp, 0, error); 8740Sstevel@tonic-gate return; 8750Sstevel@tonic-gate } 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate /* 8780Sstevel@tonic-gate * We are given an appropriate-sized data block, 8790Sstevel@tonic-gate * and return a pointer to our structure in it. 8800Sstevel@tonic-gate */ 8810Sstevel@tonic-gate *(struct cons_polledio **)mp->b_cont->b_rptr = 8820Sstevel@tonic-gate &kb8042->polledio; 8830Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 8840Sstevel@tonic-gate iocp->ioc_error = 0; 8850Sstevel@tonic-gate qreply(qp, mp); 8860Sstevel@tonic-gate break; 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate case CONSCLOSEPOLLEDIO: 8890Sstevel@tonic-gate miocack(qp, mp, 0, 0); 8900Sstevel@tonic-gate break; 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate case CONSSETABORTENABLE: 8930Sstevel@tonic-gate if (iocp->ioc_count != TRANSPARENT) { 8940Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 8950Sstevel@tonic-gate return; 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr; 8990Sstevel@tonic-gate miocack(qp, mp, 0, 0); 9000Sstevel@tonic-gate break; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate /* 9030Sstevel@tonic-gate * Valid only in TR_UNTRANS_MODE mode. 9040Sstevel@tonic-gate */ 9050Sstevel@tonic-gate case CONSSETKBDTYPE: 9060Sstevel@tonic-gate error = miocpullup(mp, sizeof (int)); 9070Sstevel@tonic-gate if (error != 0) { 9080Sstevel@tonic-gate miocnak(qp, mp, 0, error); 9090Sstevel@tonic-gate return; 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate tmp = *(int *)mp->b_cont->b_rptr; 9120Sstevel@tonic-gate if (tmp != KB_PC && tmp != KB_USB) { 9130Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 9140Sstevel@tonic-gate break; 9150Sstevel@tonic-gate } 9160Sstevel@tonic-gate kb8042->simulated_kbd_type = tmp; 9170Sstevel@tonic-gate miocack(qp, mp, 0, 0); 9180Sstevel@tonic-gate break; 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate case KIOCLAYOUT: 9210Sstevel@tonic-gate if (kb8042->w_kblayout == -1) { 9220Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 9230Sstevel@tonic-gate return; 9240Sstevel@tonic-gate } 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) { 9270Sstevel@tonic-gate miocnak(qp, mp, 0, ENOMEM); 9280Sstevel@tonic-gate return; 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_USB) 9320Sstevel@tonic-gate *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT; 9330Sstevel@tonic-gate else 9340Sstevel@tonic-gate *(int *)datap->b_wptr = kb8042->w_kblayout; 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate datap->b_wptr += sizeof (int); 9370Sstevel@tonic-gate if (mp->b_cont) 9380Sstevel@tonic-gate freemsg(mp->b_cont); 9390Sstevel@tonic-gate mp->b_cont = datap; 9400Sstevel@tonic-gate iocp->ioc_count = sizeof (int); 9410Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 9420Sstevel@tonic-gate iocp->ioc_error = 0; 9430Sstevel@tonic-gate qreply(qp, mp); 9440Sstevel@tonic-gate break; 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate case KIOCSLAYOUT: 9470Sstevel@tonic-gate if (iocp->ioc_count != TRANSPARENT) { 9480Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 9490Sstevel@tonic-gate return; 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr; 9530Sstevel@tonic-gate miocack(qp, mp, 0, 0); 9540Sstevel@tonic-gate break; 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate case KIOCCMD: 9570Sstevel@tonic-gate error = miocpullup(mp, sizeof (int)); 9580Sstevel@tonic-gate if (error != 0) { 9590Sstevel@tonic-gate miocnak(qp, mp, 0, error); 9600Sstevel@tonic-gate return; 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr); 9640Sstevel@tonic-gate miocack(qp, mp, 0, 0); 9650Sstevel@tonic-gate break; 9660Sstevel@tonic-gate 9675129Smarx case KIOCMKTONE: 9685129Smarx if (iocp->ioc_count != TRANSPARENT) { 9695129Smarx miocnak(qp, mp, 0, EINVAL); 9705129Smarx return; 9715129Smarx } 9725129Smarx 9735129Smarx tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr); 9745129Smarx cycles = tmp & 0xffff; 9755129Smarx msecs = (tmp >> 16) & 0xffff; 9765129Smarx 9775129Smarx if (cycles == 0) 9785129Smarx frequency = UINT16_MAX; 9795129Smarx else if (cycles == UINT16_MAX) 9805129Smarx frequency = 0; 9815129Smarx else { 9825129Smarx frequency = (PIT_HZ + cycles / 2) / cycles; 9835129Smarx if (frequency > UINT16_MAX) 9845129Smarx frequency = UINT16_MAX; 9855129Smarx } 9865129Smarx 9875129Smarx error = beep_mktone(frequency, msecs); 9885129Smarx if (error != 0) 9895129Smarx miocnak(qp, mp, 0, error); 9905129Smarx else 9915129Smarx miocack(qp, mp, 0, 0); 9925129Smarx break; 9935129Smarx 9940Sstevel@tonic-gate default: 9950Sstevel@tonic-gate #ifdef DEBUG1 9960Sstevel@tonic-gate cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd); 9970Sstevel@tonic-gate #endif 9980Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 9990Sstevel@tonic-gate return; 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate } 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate /* 10040Sstevel@tonic-gate * Process a byte received from the keyboard 10050Sstevel@tonic-gate */ 10060Sstevel@tonic-gate static void 10070Sstevel@tonic-gate kb8042_received_byte( 10080Sstevel@tonic-gate struct kb8042 *kb8042, 10090Sstevel@tonic-gate int scancode) /* raw scan code */ 10100Sstevel@tonic-gate { 10110Sstevel@tonic-gate boolean_t legit; /* is this a legit key pos'n? */ 10120Sstevel@tonic-gate int key_pos = -1; 10130Sstevel@tonic-gate enum keystate state; 10140Sstevel@tonic-gate boolean_t synthetic_release_needed; 10150Sstevel@tonic-gate 1016*8960SJan.Setje-Eilers@Sun.COM static int attempt_one_reset = 1; 1017*8960SJan.Setje-Eilers@Sun.COM 10180Sstevel@tonic-gate #ifdef KD_DEBUG 10190Sstevel@tonic-gate kb8042_debug_hotkey(scancode); 10200Sstevel@tonic-gate #endif 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate if (!kb8042->w_init) /* can't do anything anyway */ 10230Sstevel@tonic-gate return; 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state, 10265129Smarx &synthetic_release_needed); 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate if (legit == 0) { 10290Sstevel@tonic-gate /* Eaten by translation */ 10300Sstevel@tonic-gate #ifdef KD_DEBUG 10310Sstevel@tonic-gate if (kb8042_debug) 10320Sstevel@tonic-gate prom_printf("kb8042_intr: 0x%x -> ignored\n", scancode); 10330Sstevel@tonic-gate #endif 10340Sstevel@tonic-gate return; 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate #ifdef KD_DEBUG 10380Sstevel@tonic-gate if (kb8042_debug) { 10390Sstevel@tonic-gate prom_printf("kb8042_intr: 0x%x -> %s %d", 10405129Smarx scancode, 10415129Smarx state == KEY_RELEASED ? "released" : "pressed", 10425129Smarx key_pos); 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate #endif 10450Sstevel@tonic-gate 1046*8960SJan.Setje-Eilers@Sun.COM /* trigger switch back to text mode */ 1047*8960SJan.Setje-Eilers@Sun.COM if (attempt_one_reset == 1) { 1048*8960SJan.Setje-Eilers@Sun.COM ldi_ident_t li; 1049*8960SJan.Setje-Eilers@Sun.COM extern void progressbar_key_abort(ldi_ident_t); 1050*8960SJan.Setje-Eilers@Sun.COM 1051*8960SJan.Setje-Eilers@Sun.COM if (ldi_ident_from_dev(kb8042->w_dev, &li) != 0) { 1052*8960SJan.Setje-Eilers@Sun.COM cmn_err(CE_NOTE, "!ldi_ident_from_stream failed"); 1053*8960SJan.Setje-Eilers@Sun.COM } else { 1054*8960SJan.Setje-Eilers@Sun.COM progressbar_key_abort(li); 1055*8960SJan.Setje-Eilers@Sun.COM ldi_ident_release(li); 1056*8960SJan.Setje-Eilers@Sun.COM } 1057*8960SJan.Setje-Eilers@Sun.COM attempt_one_reset = 0; 1058*8960SJan.Setje-Eilers@Sun.COM } 1059*8960SJan.Setje-Eilers@Sun.COM 10600Sstevel@tonic-gate /* 10610Sstevel@tonic-gate * Don't know if we want this permanently, but it seems interesting 10620Sstevel@tonic-gate * for the moment. 10630Sstevel@tonic-gate */ 10640Sstevel@tonic-gate if (key_pos == kb8042->debugger.mod1) { 10650Sstevel@tonic-gate #ifdef KD_DEBUG 10660Sstevel@tonic-gate if (kb8042_debug) 10670Sstevel@tonic-gate prom_printf(" -> debug mod1"); 10680Sstevel@tonic-gate #endif 10690Sstevel@tonic-gate kb8042->debugger.mod1_down = (state == KEY_PRESSED); 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate if (key_pos == kb8042->debugger.mod2) { 10720Sstevel@tonic-gate #ifdef KD_DEBUG 10730Sstevel@tonic-gate if (kb8042_debug) 10740Sstevel@tonic-gate prom_printf(" -> debug mod2"); 10750Sstevel@tonic-gate #endif 10760Sstevel@tonic-gate kb8042->debugger.mod2_down = (state == KEY_PRESSED); 10770Sstevel@tonic-gate } 10780Sstevel@tonic-gate if (kb8042->debugger.enabled && 10790Sstevel@tonic-gate key_pos == kb8042->debugger.trigger && 10800Sstevel@tonic-gate kb8042->debugger.mod1_down && 10810Sstevel@tonic-gate kb8042->debugger.mod2_down) { 10820Sstevel@tonic-gate #ifdef KD_DEBUG 10830Sstevel@tonic-gate if (kb8042_debug) 10840Sstevel@tonic-gate prom_printf(" -> debugger\n"); 10850Sstevel@tonic-gate #endif 10860Sstevel@tonic-gate /* 10870Sstevel@tonic-gate * Require new presses of the modifiers. 10880Sstevel@tonic-gate */ 10890Sstevel@tonic-gate kb8042->debugger.mod1_down = B_FALSE; 10900Sstevel@tonic-gate kb8042->debugger.mod2_down = B_FALSE; 10910Sstevel@tonic-gate abort_sequence_enter(NULL); 10920Sstevel@tonic-gate return; 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate /* 10960Sstevel@tonic-gate * If there's no queue above us - as can happen if we've been 10970Sstevel@tonic-gate * attached but not opened - drop the keystroke. 10980Sstevel@tonic-gate * Note that we do this here instead of above so that 10990Sstevel@tonic-gate * Ctrl-Alt-D still works. 11000Sstevel@tonic-gate */ 11010Sstevel@tonic-gate if (kb8042->w_qp == NULL) { 11020Sstevel@tonic-gate #ifdef KD_DEBUG 11030Sstevel@tonic-gate if (kb8042_debug) 11040Sstevel@tonic-gate prom_printf(" -> nobody home\n"); 11050Sstevel@tonic-gate #endif 11060Sstevel@tonic-gate return; 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate /* 11100Sstevel@tonic-gate * This is to filter out auto repeat since it can't be 11110Sstevel@tonic-gate * turned off at the hardware. (Yeah, yeah, PS/2 keyboards 11120Sstevel@tonic-gate * can. Don't know whether they've taken over the world. 11130Sstevel@tonic-gate * Don't think so.) 11140Sstevel@tonic-gate */ 11150Sstevel@tonic-gate if (kb8042_autorepeat_detect(kb8042, key_pos, state)) { 11160Sstevel@tonic-gate #ifdef KD_DEBUG 11170Sstevel@tonic-gate if (kb8042_debug) 11180Sstevel@tonic-gate prom_printf(" -> autorepeat ignored\n"); 11190Sstevel@tonic-gate #endif 11200Sstevel@tonic-gate return; 11210Sstevel@tonic-gate } 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate #ifdef KD_DEBUG 11240Sstevel@tonic-gate if (kb8042_debug) 11250Sstevel@tonic-gate prom_printf(" -> OK\n"); 11260Sstevel@tonic-gate #endif 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate #if defined(KD_DEBUG) 11290Sstevel@tonic-gate if (kb8042_pressrelease_debug) { 11300Sstevel@tonic-gate prom_printf(" %s%d ", 11315129Smarx state == KEY_PRESSED ? "+" : "-", 11325129Smarx key_pos); 11330Sstevel@tonic-gate } 11340Sstevel@tonic-gate #endif 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate kb8042_process_key(kb8042, key_pos, state); 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate /* 11390Sstevel@tonic-gate * This is a total hack. For some stupid reason, the two additional 11400Sstevel@tonic-gate * keys on Korean keyboards (Hangul and Hangul/Hanja) report press 11410Sstevel@tonic-gate * only. We synthesize a release immediately. 11420Sstevel@tonic-gate */ 11430Sstevel@tonic-gate if (synthetic_release_needed) { 11440Sstevel@tonic-gate #if defined(KD_DEBUG) 11450Sstevel@tonic-gate if (kb8042_debug) 11460Sstevel@tonic-gate prom_printf("synthetic release %d\n", key_pos); 11470Sstevel@tonic-gate if (kb8042_pressrelease_debug) 11480Sstevel@tonic-gate prom_printf(" -%d(s) ", key_pos); 11490Sstevel@tonic-gate #endif 11500Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED); 11510Sstevel@tonic-gate kb8042_process_key(kb8042, key_pos, state); 11520Sstevel@tonic-gate } 11530Sstevel@tonic-gate } 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate static void 11570Sstevel@tonic-gate kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos, 11580Sstevel@tonic-gate enum keystate state) 11590Sstevel@tonic-gate { 11600Sstevel@tonic-gate kbtrans_key_t key; 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate ASSERT(key_pos >= 0 && key_pos <= 255); 11630Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_PC) { 11640Sstevel@tonic-gate kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state); 11650Sstevel@tonic-gate } else if (kb8042->simulated_kbd_type == KB_USB) { 11660Sstevel@tonic-gate key = keytab_pc2usb[key_pos]; 11670Sstevel@tonic-gate if (key != 0) { 11680Sstevel@tonic-gate kbtrans_streams_key(kb8042->hw_kbtrans, key, state); 11690Sstevel@tonic-gate } 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate /* 11740Sstevel@tonic-gate * Called from interrupt handler when keyboard interrupt occurs. 11750Sstevel@tonic-gate */ 11760Sstevel@tonic-gate static uint_t 11770Sstevel@tonic-gate kb8042_intr(caddr_t arg) 11780Sstevel@tonic-gate { 11790Sstevel@tonic-gate uchar_t scancode; /* raw scan code */ 11800Sstevel@tonic-gate int rc; 11810Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)arg; 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate rc = DDI_INTR_UNCLAIMED; 11840Sstevel@tonic-gate 1185822Ssethg if (kb8042->init_state == KB8042_UNINITIALIZED) 1186822Ssethg return (DDI_INTR_UNCLAIMED); 1187822Ssethg 11880Sstevel@tonic-gate /* don't care if drv_setparm succeeds */ 11890Sstevel@tonic-gate (void) drv_setparm(SYSRINT, 1); 11900Sstevel@tonic-gate 11910Sstevel@tonic-gate while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL) 11920Sstevel@tonic-gate != 0) { 11930Sstevel@tonic-gate rc = DDI_INTR_CLAIMED; 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate scancode = ddi_get8(kb8042->handle, 11965129Smarx kb8042->addr + I8042_INT_INPUT_DATA); 11970Sstevel@tonic-gate 11980Sstevel@tonic-gate #if defined(KD_DEBUG) 11990Sstevel@tonic-gate if (kb8042_low_level_debug) 12000Sstevel@tonic-gate prom_printf(" <K:%x ", scancode); 12010Sstevel@tonic-gate #endif 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate kb8042_received_byte(kb8042, scancode); 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate 12060Sstevel@tonic-gate return (rc); 12070Sstevel@tonic-gate } 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate static void 12100Sstevel@tonic-gate kb8042_iocdatamsg(queue_t *qp, mblk_t *mp) 12110Sstevel@tonic-gate { 12120Sstevel@tonic-gate struct copyresp *csp; 12130Sstevel@tonic-gate 12140Sstevel@tonic-gate csp = (struct copyresp *)mp->b_rptr; 12150Sstevel@tonic-gate if (csp->cp_rval) { 12160Sstevel@tonic-gate freemsg(mp); 12170Sstevel@tonic-gate return; 12180Sstevel@tonic-gate } 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate switch (csp->cp_cmd) { 12210Sstevel@tonic-gate default: 12220Sstevel@tonic-gate miocack(qp, mp, 0, 0); 12230Sstevel@tonic-gate break; 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate } 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate static boolean_t 12280Sstevel@tonic-gate kb8042_polled_keycheck( 12290Sstevel@tonic-gate struct kbtrans_hardware *hw, 12300Sstevel@tonic-gate int *key, 12310Sstevel@tonic-gate enum keystate *state) 12320Sstevel@tonic-gate { 12330Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)hw; 12340Sstevel@tonic-gate int scancode; 12350Sstevel@tonic-gate boolean_t legit; 12360Sstevel@tonic-gate boolean_t synthetic_release_needed; 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate if (kb8042->polled_synthetic_release_pending) { 12390Sstevel@tonic-gate *key = kb8042->polled_synthetic_release_key; 12400Sstevel@tonic-gate *state = KEY_RELEASED; 12410Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_FALSE; 12420Sstevel@tonic-gate #if defined(KD_DEBUG) 12430Sstevel@tonic-gate if (kb8042_getchar_debug) 12440Sstevel@tonic-gate prom_printf("synthetic release 0x%x\n", *key); 12450Sstevel@tonic-gate #endif 12460Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, *key, *state); 12470Sstevel@tonic-gate return (B_TRUE); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate 12500Sstevel@tonic-gate for (;;) { 12510Sstevel@tonic-gate if (ddi_get8(kb8042->handle, 12520Sstevel@tonic-gate kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) { 12530Sstevel@tonic-gate return (B_FALSE); 12540Sstevel@tonic-gate } 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate scancode = ddi_get8(kb8042->handle, 12575129Smarx kb8042->addr + I8042_POLL_INPUT_DATA); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate #if defined(KD_DEBUG) 12600Sstevel@tonic-gate if (kb8042_low_level_debug) 12610Sstevel@tonic-gate prom_printf(" g<%x ", scancode); 12620Sstevel@tonic-gate #endif 12630Sstevel@tonic-gate 12640Sstevel@tonic-gate #ifdef KD_DEBUG 12650Sstevel@tonic-gate kb8042_debug_hotkey(scancode); 12660Sstevel@tonic-gate if (kb8042_getchar_debug) 12670Sstevel@tonic-gate prom_printf("polled 0x%x", scancode); 12680Sstevel@tonic-gate #endif 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate legit = KeyboardConvertScan(kb8042, scancode, key, state, 12715129Smarx &synthetic_release_needed); 12720Sstevel@tonic-gate if (!legit) { 12730Sstevel@tonic-gate #ifdef KD_DEBUG 12740Sstevel@tonic-gate if (kb8042_getchar_debug) 12750Sstevel@tonic-gate prom_printf(" -> ignored\n"); 12760Sstevel@tonic-gate #endif 12770Sstevel@tonic-gate continue; 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate #ifdef KD_DEBUG 12800Sstevel@tonic-gate if (kb8042_getchar_debug) { 12810Sstevel@tonic-gate prom_printf(" -> %s %d\n", 12825129Smarx *state == KEY_PRESSED ? "pressed" : "released", 12835129Smarx *key); 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate #endif 12860Sstevel@tonic-gate /* 12870Sstevel@tonic-gate * For the moment at least, we rely on hardware autorepeat 12880Sstevel@tonic-gate * for polled I/O autorepeat. However, for coordination 12890Sstevel@tonic-gate * with the interrupt-driven code, maintain the last key 12900Sstevel@tonic-gate * pressed. 12910Sstevel@tonic-gate */ 12920Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, *key, *state); 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate /* 12950Sstevel@tonic-gate * This is a total hack to support two additional keys 12960Sstevel@tonic-gate * on Korean keyboards. They report only on press, and 12970Sstevel@tonic-gate * so we synthesize a release. Most likely this will 12980Sstevel@tonic-gate * never be important to polled I/O, but if I do it 12990Sstevel@tonic-gate * "right" the first time it _won't_ be an issue. 13000Sstevel@tonic-gate */ 13010Sstevel@tonic-gate if (synthetic_release_needed) { 13020Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_TRUE; 13030Sstevel@tonic-gate kb8042->polled_synthetic_release_key = *key; 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_USB) { 13070Sstevel@tonic-gate *key = keytab_pc2usb[*key]; 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate return (B_TRUE); 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate } 13120Sstevel@tonic-gate 13130Sstevel@tonic-gate static void 1314822Ssethg kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled) 1315822Ssethg { 1316822Ssethg kb8042->leds.desired = led_state; 1317822Ssethg 1318822Ssethg if (!polled) 1319822Ssethg mutex_enter(&kb8042->w_hw_mutex); 1320822Ssethg 13217658SSeth.Goldberg@Sun.COM if (kb8042->leds.desired != kb8042->leds.commanded) { 13227658SSeth.Goldberg@Sun.COM kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled); 13237658SSeth.Goldberg@Sun.COM } 1324822Ssethg 1325822Ssethg if (!polled) 1326822Ssethg mutex_exit(&kb8042->w_hw_mutex); 1327822Ssethg } 1328822Ssethg 1329822Ssethg static void 13300Sstevel@tonic-gate kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state) 13310Sstevel@tonic-gate { 13320Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)hw; 1333822Ssethg kb8042_setled(kb8042, led_state, B_TRUE); 1334822Ssethg } 13350Sstevel@tonic-gate 1336822Ssethg static void 1337822Ssethg kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state) 1338822Ssethg { 1339822Ssethg struct kb8042 *kb8042 = (struct kb8042 *)hw; 1340822Ssethg kb8042_setled(kb8042, led_state, B_FALSE); 13410Sstevel@tonic-gate } 13420Sstevel@tonic-gate 13430Sstevel@tonic-gate static void 13440Sstevel@tonic-gate kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled) 13450Sstevel@tonic-gate { 13467685SSeth.Goldberg@Sun.COM uint8_t led_cmd[4]; 13477658SSeth.Goldberg@Sun.COM 13487658SSeth.Goldberg@Sun.COM /* 13497658SSeth.Goldberg@Sun.COM * KB_SET_LED and KB_ENABLE are special commands for which the nexus 13507658SSeth.Goldberg@Sun.COM * driver is requested to wait for responses before proceeding. 13517658SSeth.Goldberg@Sun.COM * KB_SET_LED also provides an option byte for the nexus to send to 13527658SSeth.Goldberg@Sun.COM * the keyboard after the acknowledgement. 13537658SSeth.Goldberg@Sun.COM * 13547658SSeth.Goldberg@Sun.COM * Other commands/data are sent using the single put8 I/O access 13557658SSeth.Goldberg@Sun.COM * function. 13567658SSeth.Goldberg@Sun.COM */ 13577658SSeth.Goldberg@Sun.COM if (byte == KB_SET_LED) { 13587658SSeth.Goldberg@Sun.COM /* 13597658SSeth.Goldberg@Sun.COM * Initialize the buffer used with _rep_put8. We 13607658SSeth.Goldberg@Sun.COM * expect an ACK after the SET_LED command, at which point 13617658SSeth.Goldberg@Sun.COM * the LED byte should be sent to the keyboard. 13627658SSeth.Goldberg@Sun.COM */ 13637658SSeth.Goldberg@Sun.COM led_cmd[0] = KB_SET_LED; 13647658SSeth.Goldberg@Sun.COM led_cmd[1] = KB_ACK; 13657685SSeth.Goldberg@Sun.COM led_cmd[2] = KB_RESEND; 13667685SSeth.Goldberg@Sun.COM led_cmd[3] = kb8042_xlate_leds(kb8042->leds.desired); 13677658SSeth.Goldberg@Sun.COM if (polled) { 13687658SSeth.Goldberg@Sun.COM ddi_rep_put8(kb8042->handle, &led_cmd[0], 13697685SSeth.Goldberg@Sun.COM kb8042->addr + I8042_POLL_CMD_PLUS_PARAM, 4, 0); 13707658SSeth.Goldberg@Sun.COM } else { 13717658SSeth.Goldberg@Sun.COM ddi_rep_put8(kb8042->handle, &led_cmd[0], 13727685SSeth.Goldberg@Sun.COM kb8042->addr + I8042_INT_CMD_PLUS_PARAM, 4, 0); 13737658SSeth.Goldberg@Sun.COM } 13747658SSeth.Goldberg@Sun.COM kb8042->leds.commanded = kb8042->leds.desired; 13757658SSeth.Goldberg@Sun.COM 13767658SSeth.Goldberg@Sun.COM } else if (byte == KB_ENABLE) { 13777658SSeth.Goldberg@Sun.COM 13787658SSeth.Goldberg@Sun.COM /* 13797658SSeth.Goldberg@Sun.COM * Initialize the buffer used with _rep_put8. We 13807658SSeth.Goldberg@Sun.COM * expect an ACK after the KB_ENABLE command. 13817658SSeth.Goldberg@Sun.COM */ 13827658SSeth.Goldberg@Sun.COM led_cmd[0] = KB_ENABLE; 13837658SSeth.Goldberg@Sun.COM led_cmd[1] = KB_ACK; 13847685SSeth.Goldberg@Sun.COM led_cmd[2] = KB_RESEND; 13857658SSeth.Goldberg@Sun.COM if (polled) { 13867658SSeth.Goldberg@Sun.COM ddi_rep_put8(kb8042->handle, &led_cmd[0], 13877685SSeth.Goldberg@Sun.COM kb8042->addr + I8042_POLL_CMD_PLUS_PARAM, 3, 0); 13887658SSeth.Goldberg@Sun.COM } else { 13897658SSeth.Goldberg@Sun.COM ddi_rep_put8(kb8042->handle, &led_cmd[0], 13907685SSeth.Goldberg@Sun.COM kb8042->addr + I8042_INT_CMD_PLUS_PARAM, 3, 0); 13917658SSeth.Goldberg@Sun.COM } 13920Sstevel@tonic-gate } else { 13937658SSeth.Goldberg@Sun.COM /* All other commands use the "normal" virtual output port */ 13947658SSeth.Goldberg@Sun.COM if (polled) { 13957658SSeth.Goldberg@Sun.COM ddi_put8(kb8042->handle, 13967658SSeth.Goldberg@Sun.COM kb8042->addr + I8042_POLL_OUTPUT_DATA, byte); 13977658SSeth.Goldberg@Sun.COM } else { 13987658SSeth.Goldberg@Sun.COM ddi_put8(kb8042->handle, 13997658SSeth.Goldberg@Sun.COM kb8042->addr + I8042_INT_OUTPUT_DATA, byte); 14007658SSeth.Goldberg@Sun.COM } 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate 14030Sstevel@tonic-gate #if defined(KD_DEBUG) 14040Sstevel@tonic-gate if (kb8042_low_level_debug) 14050Sstevel@tonic-gate prom_printf(" >K:%x ", byte); 14060Sstevel@tonic-gate #endif 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate /* 14100Sstevel@tonic-gate * Wait until the keyboard is fully up, maybe. 14110Sstevel@tonic-gate * We may be the first person to talk to the keyboard, in which case 14120Sstevel@tonic-gate * it's patiently waiting to say "AA" to us to tell us it's up. 14130Sstevel@tonic-gate * In theory it sends the AA in 300ms < n < 9s, but it's a pretty 14140Sstevel@tonic-gate * good bet that we've already spent that long getting to that point, 14150Sstevel@tonic-gate * so we'll only wait long enough for the communications electronics to 14160Sstevel@tonic-gate * run. 14170Sstevel@tonic-gate */ 14180Sstevel@tonic-gate static void 14190Sstevel@tonic-gate kb8042_wait_poweron(struct kb8042 *kb8042) 14200Sstevel@tonic-gate { 14210Sstevel@tonic-gate int cnt; 14220Sstevel@tonic-gate int ready; 14230Sstevel@tonic-gate unsigned char byt; 14240Sstevel@tonic-gate 1425822Ssethg /* wait for up to 250 ms for a response */ 14260Sstevel@tonic-gate for (cnt = 0; cnt < 250; cnt++) { 14270Sstevel@tonic-gate ready = ddi_get8(kb8042->handle, 14285129Smarx kb8042->addr + I8042_INT_INPUT_AVAIL); 14290Sstevel@tonic-gate if (ready != 0) 14300Sstevel@tonic-gate break; 14310Sstevel@tonic-gate drv_usecwait(1000); 14320Sstevel@tonic-gate } 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate /* 14350Sstevel@tonic-gate * If there's something pending, read and discard it. If not, 14360Sstevel@tonic-gate * assume things are OK anyway - maybe somebody else ate it 14370Sstevel@tonic-gate * already. (On a PC, the BIOS almost certainly did.) 14380Sstevel@tonic-gate */ 14390Sstevel@tonic-gate if (ready != 0) { 14400Sstevel@tonic-gate byt = ddi_get8(kb8042->handle, 14415129Smarx kb8042->addr + I8042_INT_INPUT_DATA); 14420Sstevel@tonic-gate #if defined(KD_DEBUG) 14430Sstevel@tonic-gate if (kb8042_low_level_debug) 14440Sstevel@tonic-gate prom_printf(" <K:%x ", byt); 14450Sstevel@tonic-gate #endif 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate } 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate static int 14500Sstevel@tonic-gate kb8042_xlate_leds(int led) 14510Sstevel@tonic-gate { 14520Sstevel@tonic-gate int res; 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate res = 0; 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate if (led & LED_NUM_LOCK) 14570Sstevel@tonic-gate res |= LED_NUM; 14580Sstevel@tonic-gate if (led & LED_SCROLL_LOCK) 14590Sstevel@tonic-gate res |= LED_SCR; 14600Sstevel@tonic-gate if (led & LED_CAPS_LOCK) 14610Sstevel@tonic-gate res |= LED_CAP; 14620Sstevel@tonic-gate 14630Sstevel@tonic-gate return (res); 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate /*ARGSUSED*/ 14670Sstevel@tonic-gate static void 14680Sstevel@tonic-gate kb8042_get_initial_leds( 14690Sstevel@tonic-gate struct kb8042 *kb8042, 14700Sstevel@tonic-gate int *initial_leds, 14710Sstevel@tonic-gate int *initial_led_mask) 14720Sstevel@tonic-gate { 14737658SSeth.Goldberg@Sun.COM #if defined(__i386) || defined(__amd64) 14740Sstevel@tonic-gate extern caddr_t p0_va; 14750Sstevel@tonic-gate uint8_t bios_kb_flag; 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate bios_kb_flag = p0_va[BIOS_KB_FLAG]; 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK; 14800Sstevel@tonic-gate *initial_leds = 0; 14810Sstevel@tonic-gate if (bios_kb_flag & BIOS_CAPS_STATE) 14820Sstevel@tonic-gate *initial_leds |= LED_CAPS_LOCK; 14830Sstevel@tonic-gate if (bios_kb_flag & BIOS_NUM_STATE) 14840Sstevel@tonic-gate *initial_leds |= LED_NUM_LOCK; 14850Sstevel@tonic-gate if (bios_kb_flag & BIOS_SCROLL_STATE) 14860Sstevel@tonic-gate *initial_leds |= LED_SCROLL_LOCK; 14870Sstevel@tonic-gate #else 14880Sstevel@tonic-gate *initial_leds = 0; 14890Sstevel@tonic-gate *initial_led_mask = 0; 14900Sstevel@tonic-gate #endif 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate #if defined(KD_DEBUG) 14940Sstevel@tonic-gate static void 14950Sstevel@tonic-gate kb8042_debug_hotkey(int scancode) 14960Sstevel@tonic-gate { 14970Sstevel@tonic-gate if (!kb8042_enable_debug_hotkey) 14980Sstevel@tonic-gate return; 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate switch (scancode) { 15010Sstevel@tonic-gate case 0x44: /* F10 in Scan Set 1 code. (Set 2 code is 0x09) */ 15020Sstevel@tonic-gate if (!kb8042_debug) { 15030Sstevel@tonic-gate prom_printf("\nKeyboard: normal debugging on\n"); 15040Sstevel@tonic-gate kb8042_debug = B_TRUE; 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate break; 15070Sstevel@tonic-gate case 0x43: /* F9 in Scan Set 1 code. (Set 2 code is 0x01) */ 15080Sstevel@tonic-gate if (!kb8042_getchar_debug) { 15090Sstevel@tonic-gate prom_printf("\nKeyboard: getchar debugging on\n"); 15100Sstevel@tonic-gate kb8042_getchar_debug = B_TRUE; 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate break; 15130Sstevel@tonic-gate case 0x42: /* F8 in Scan Set 1 code. (Set 2 code is 0x0a) */ 15140Sstevel@tonic-gate if (!kb8042_low_level_debug) { 15150Sstevel@tonic-gate prom_printf("\nKeyboard: low-level debugging on\n"); 15160Sstevel@tonic-gate kb8042_low_level_debug = B_TRUE; 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate break; 15190Sstevel@tonic-gate case 0x41: /* F7 in Scan Set 1 code. (Set 2 code is 0x83) */ 15200Sstevel@tonic-gate if (!kb8042_pressrelease_debug) { 15210Sstevel@tonic-gate prom_printf( 15220Sstevel@tonic-gate "\nKeyboard: press/release debugging on\n"); 15230Sstevel@tonic-gate kb8042_pressrelease_debug = B_TRUE; 15240Sstevel@tonic-gate } 15250Sstevel@tonic-gate break; 15260Sstevel@tonic-gate case 0x3b: /* F1 in Scan Set 1 code. (Set 2 code is 0x05) */ 15270Sstevel@tonic-gate if (kb8042_debug || 15280Sstevel@tonic-gate kb8042_getchar_debug || 15290Sstevel@tonic-gate kb8042_low_level_debug || 15300Sstevel@tonic-gate kb8042_pressrelease_debug) { 15310Sstevel@tonic-gate prom_printf("\nKeyboard: all debugging off\n"); 15320Sstevel@tonic-gate kb8042_debug = B_FALSE; 15330Sstevel@tonic-gate kb8042_getchar_debug = B_FALSE; 15340Sstevel@tonic-gate kb8042_low_level_debug = B_FALSE; 15350Sstevel@tonic-gate kb8042_pressrelease_debug = B_FALSE; 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate break; 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate } 15400Sstevel@tonic-gate #endif 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate static boolean_t 15430Sstevel@tonic-gate kb8042_autorepeat_detect( 15440Sstevel@tonic-gate struct kb8042 *kb8042, 15450Sstevel@tonic-gate int key_pos, 15460Sstevel@tonic-gate enum keystate state) 15470Sstevel@tonic-gate { 15480Sstevel@tonic-gate if (state == KEY_RELEASED) { 15490Sstevel@tonic-gate if (kb8042->kb_old_key_pos == key_pos) 15500Sstevel@tonic-gate kb8042->kb_old_key_pos = 0; 15510Sstevel@tonic-gate } else { 15520Sstevel@tonic-gate if (kb8042->kb_old_key_pos == key_pos) { 15530Sstevel@tonic-gate return (B_TRUE); 15540Sstevel@tonic-gate } 15550Sstevel@tonic-gate kb8042->kb_old_key_pos = key_pos; 15560Sstevel@tonic-gate } 15570Sstevel@tonic-gate return (B_FALSE); 15580Sstevel@tonic-gate } 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate /* ARGSUSED */ 15610Sstevel@tonic-gate static void 15620Sstevel@tonic-gate kb8042_type4_cmd(struct kb8042 *kb8042, int cmd) 15630Sstevel@tonic-gate { 15640Sstevel@tonic-gate switch (cmd) { 15650Sstevel@tonic-gate case KBD_CMD_BELL: 15665129Smarx (void) beeper_on(BEEP_TYPE4); 15670Sstevel@tonic-gate break; 15680Sstevel@tonic-gate case KBD_CMD_NOBELL: 15695129Smarx (void) beeper_off(); 15700Sstevel@tonic-gate break; 15710Sstevel@tonic-gate } 15720Sstevel@tonic-gate } 15730Sstevel@tonic-gate 15740Sstevel@tonic-gate 15750Sstevel@tonic-gate /* 15760Sstevel@tonic-gate * This is a pass-thru routine to get a character at poll time. 15770Sstevel@tonic-gate */ 15780Sstevel@tonic-gate static int 15791762Slt200341 kb8042_polled_getchar(cons_polledio_arg_t arg) 15800Sstevel@tonic-gate { 15810Sstevel@tonic-gate struct kb8042 *kb8042; 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate kb8042 = (struct kb8042 *)arg; 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate return (kbtrans_getchar(kb8042->hw_kbtrans)); 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate /* 15890Sstevel@tonic-gate * This is a pass-thru routine to get a character at poll time. 15900Sstevel@tonic-gate */ 15910Sstevel@tonic-gate static int 15921762Slt200341 kb8042_polled_ischar(cons_polledio_arg_t arg) 15930Sstevel@tonic-gate { 15940Sstevel@tonic-gate struct kb8042 *kb8042; 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate kb8042 = (struct kb8042 *)arg; 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate return (kbtrans_ischar(kb8042->hw_kbtrans)); 15990Sstevel@tonic-gate } 1600