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 /*
268960SJan.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>
58*10617SPengcheng.Chen@Sun.COM #include <sys/policy.h>
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * For any keyboard, there is a unique code describing the position
620Sstevel@tonic-gate * of the key on a keyboard. We refer to the code as "station number".
630Sstevel@tonic-gate * The following table is used to map the station numbers from ps2
640Sstevel@tonic-gate * AT/XT keyboards to that of a USB one.
65822Ssethg *
66822Ssethg * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
67822Ssethg * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
68822Ssethg * when in KB_PC mode. Note that this does not need to be made conditional
69822Ssethg * on the architecture for which this module is complied because there are no
70822Ssethg * keys that map to K8042_STOP on non-SPARC platforms.
710Sstevel@tonic-gate */
720Sstevel@tonic-gate static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
730Sstevel@tonic-gate /* 0 */ 0, 53, 30, 31, 32, 33, 34, 35,
740Sstevel@tonic-gate /* 8 */ 36, 37, 38, 39, 45, 46, 137, 42,
750Sstevel@tonic-gate /* 16 */ 43, 20, 26, 8, 21, 23, 28, 24,
760Sstevel@tonic-gate /* 24 */ 12, 18, 19, 47, 48, 49, 57, 4,
770Sstevel@tonic-gate /* 32 */ 22, 7, 9, 10, 11, 13, 14, 15,
780Sstevel@tonic-gate /* 40 */ 51, 52, 50, 40, 225, 100, 29, 27,
790Sstevel@tonic-gate /* 48 */ 6, 25, 5, 17, 16, 54, 55, 56,
800Sstevel@tonic-gate /* 56 */ 135, 229, 224, 227, 226, 44, 230, 231,
810Sstevel@tonic-gate /* 64 */ 228, 101, 0, 0, 0, 0, 0, 0,
820Sstevel@tonic-gate /* 72 */ 0, 0, 0, 73, 76, 0, 0, 80,
830Sstevel@tonic-gate /* 80 */ 74, 77, 0, 82, 81, 75, 78, 0,
840Sstevel@tonic-gate /* 88 */ 0, 79, 83, 95, 92, 89, 0, 84,
850Sstevel@tonic-gate /* 96 */ 96, 93, 90, 98, 85, 97, 94, 91,
860Sstevel@tonic-gate /* 104 */ 99, 86, 87, 133, 88, 0, 41, 0,
870Sstevel@tonic-gate /* 112 */ 58, 59, 60, 61, 62, 63, 64, 65,
880Sstevel@tonic-gate /* 120 */ 66, 67, 68, 69, 70, 71, 72, 0,
890Sstevel@tonic-gate /* 128 */ 0, 0, 0, 139, 138, 136, 0, 0,
900Sstevel@tonic-gate /* 136 */ 0, 0, 0, 0, 0, 0, 0, 0,
910Sstevel@tonic-gate /* 144 */ 0, 0, 0, 0, 0, 0, 0, 0,
920Sstevel@tonic-gate /* 152 */ 0, 0, 0, 0, 0, 0, 0, 0,
93822Ssethg /* 160 */ 120, 0, 0, 0, 0, 0, 0, 0,
940Sstevel@tonic-gate /* 168 */ 0, 0, 0, 0, 0, 0, 0, 0,
950Sstevel@tonic-gate /* 176 */ 0, 0, 0, 0, 0, 0, 0, 0,
960Sstevel@tonic-gate /* 184 */ 0, 0, 0, 0, 0, 0, 0, 0,
970Sstevel@tonic-gate /* 192 */ 0, 0, 0, 0, 0, 0, 0, 0,
980Sstevel@tonic-gate /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0,
990Sstevel@tonic-gate /* 208 */ 0, 0, 0, 0, 0, 0, 0, 0,
1000Sstevel@tonic-gate /* 216 */ 0, 0, 0, 0, 0, 0, 0, 0,
1010Sstevel@tonic-gate /* 224 */ 0, 0, 0, 0, 0, 0, 0, 0,
1029026SSeth.Goldberg@Sun.COM /* 232 */ 0, 128, 129, 127, 0, 0, 0, 0,
1030Sstevel@tonic-gate /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0,
1040Sstevel@tonic-gate /* 248 */ 0, 0, 0, 0
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate
107822Ssethg #ifdef __sparc
108822Ssethg #define USECS_PER_WAIT 100
109822Ssethg #define MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
110822Ssethg #define MIN_DELAY_USECS USECS_PER_WAIT
111822Ssethg
112822Ssethg boolean_t kb8042_warn_unknown_scanset = B_TRUE;
113822Ssethg int kb8042_default_scanset = 2;
114822Ssethg
115822Ssethg #endif
116822Ssethg
11710087SSeth.Goldberg@Sun.COM #define MAX_KB8042_WAIT_MAX_MS 500 /* 500ms total */
11810087SSeth.Goldberg@Sun.COM #define MAX_KB8042_RETRIES 5
11910087SSeth.Goldberg@Sun.COM
1200Sstevel@tonic-gate enum state_return { STATE_NORMAL, STATE_INTERNAL };
1210Sstevel@tonic-gate
122822Ssethg static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
1230Sstevel@tonic-gate static uint_t kb8042_intr(caddr_t arg);
1240Sstevel@tonic-gate static void kb8042_wait_poweron(struct kb8042 *kb8042);
1250Sstevel@tonic-gate static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
1260Sstevel@tonic-gate static int kb8042_xlate_leds(int);
127822Ssethg static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
1280Sstevel@tonic-gate static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
1290Sstevel@tonic-gate static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
1300Sstevel@tonic-gate static boolean_t kb8042_polled_keycheck(
1310Sstevel@tonic-gate struct kbtrans_hardware *hw, int *key,
1320Sstevel@tonic-gate enum keystate *state);
1330Sstevel@tonic-gate static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
1340Sstevel@tonic-gate static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
1350Sstevel@tonic-gate enum keystate state);
1360Sstevel@tonic-gate static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
1370Sstevel@tonic-gate static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
1380Sstevel@tonic-gate static void kb8042_iocdatamsg(queue_t *, mblk_t *);
1390Sstevel@tonic-gate static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
1401762Slt200341 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
1411762Slt200341 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
142822Ssethg static void kb8042_cleanup(struct kb8042 *kb8042);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate static struct kbtrans_callbacks kb8042_callbacks = {
1450Sstevel@tonic-gate kb8042_streams_setled,
1460Sstevel@tonic-gate kb8042_polled_setled,
1470Sstevel@tonic-gate kb8042_polled_keycheck,
1480Sstevel@tonic-gate };
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate extern struct keyboard keyindex_pc;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate #define DRIVER_NAME(dip) ddi_driver_name(dip)
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate static char module_name[] = "kb8042";
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
1570Sstevel@tonic-gate cred_t *credp);
1580Sstevel@tonic-gate static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
1590Sstevel@tonic-gate static int kb8042_wsrv();
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate struct module_info kb8042_sinfo = {
1620Sstevel@tonic-gate 42, /* Module ID */
1630Sstevel@tonic-gate module_name,
1640Sstevel@tonic-gate 0, 32, /* Minimum and maximum packet sizes */
1650Sstevel@tonic-gate 256, 128 /* High and low water marks */
1660Sstevel@tonic-gate };
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate static struct qinit kb8042_rinit = {
1690Sstevel@tonic-gate NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1700Sstevel@tonic-gate };
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate static struct qinit kb8042_winit = {
1730Sstevel@tonic-gate putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
1740Sstevel@tonic-gate };
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate struct streamtab
1770Sstevel@tonic-gate kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate struct kb8042 Kdws = {0};
180822Ssethg static dev_info_t *kb8042_dip = NULL;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1830Sstevel@tonic-gate void **result);
1840Sstevel@tonic-gate static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
185822Ssethg static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate static struct cb_ops cb_kb8042_ops = {
1880Sstevel@tonic-gate nulldev, /* cb_open */
1890Sstevel@tonic-gate nulldev, /* cb_close */
1900Sstevel@tonic-gate nodev, /* cb_strategy */
1910Sstevel@tonic-gate nodev, /* cb_print */
1920Sstevel@tonic-gate nodev, /* cb_dump */
1930Sstevel@tonic-gate nodev, /* cb_read */
1940Sstevel@tonic-gate nodev, /* cb_write */
1950Sstevel@tonic-gate nodev, /* cb_ioctl */
1960Sstevel@tonic-gate nodev, /* cb_devmap */
1970Sstevel@tonic-gate nodev, /* cb_mmap */
1980Sstevel@tonic-gate nodev, /* cb_segmap */
1990Sstevel@tonic-gate nochpoll, /* cb_chpoll */
2000Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
2010Sstevel@tonic-gate &kb8042_str_info, /* cb_stream */
2020Sstevel@tonic-gate D_MP
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate struct dev_ops kb8042_ops = {
2060Sstevel@tonic-gate DEVO_REV, /* devo_rev */
2070Sstevel@tonic-gate 0, /* devo_refcnt */
2080Sstevel@tonic-gate kb8042_getinfo, /* devo_getinfo */
2090Sstevel@tonic-gate nulldev, /* devo_identify */
2100Sstevel@tonic-gate nulldev, /* devo_probe */
2110Sstevel@tonic-gate kb8042_attach, /* devo_attach */
212822Ssethg kb8042_detach, /* devo_detach */
2130Sstevel@tonic-gate nodev, /* devo_reset */
2140Sstevel@tonic-gate &cb_kb8042_ops, /* devo_cb_ops */
2157656SSherry.Moore@Sun.COM (struct bus_ops *)NULL, /* devo_bus_ops */
2167656SSherry.Moore@Sun.COM NULL, /* devo_power */
2177685SSeth.Goldberg@Sun.COM ddi_quiesce_not_needed, /* devo_quiesce */
2180Sstevel@tonic-gate };
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate * This is the loadable module wrapper.
2230Sstevel@tonic-gate */
2240Sstevel@tonic-gate #include <sys/modctl.h>
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * Module linkage information for the kernel.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate static struct modldrv modldrv = {
2300Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */
2317658SSeth.Goldberg@Sun.COM "PS/2 keyboard driver",
2320Sstevel@tonic-gate &kb8042_ops, /* driver ops */
2330Sstevel@tonic-gate };
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate static struct modlinkage modlinkage = {
2360Sstevel@tonic-gate MODREV_1,
2370Sstevel@tonic-gate (void *) &modldrv,
2380Sstevel@tonic-gate NULL
2390Sstevel@tonic-gate };
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate int
_init(void)2420Sstevel@tonic-gate _init(void)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate int rv;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate rv = mod_install(&modlinkage);
2470Sstevel@tonic-gate return (rv);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate int
_fini(void)2520Sstevel@tonic-gate _fini(void)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate return (mod_remove(&modlinkage));
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2580Sstevel@tonic-gate _info(struct modinfo *modinfop)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
263822Ssethg #ifdef __sparc
264822Ssethg static boolean_t
kb8042_is_input_avail(struct kb8042 * kb8042,int timeout_usec,boolean_t polled)265822Ssethg kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
266822Ssethg {
267822Ssethg int i;
268822Ssethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
269822Ssethg I8042_INT_INPUT_AVAIL;
270822Ssethg int reps = timeout_usec / USECS_PER_WAIT;
271822Ssethg
272822Ssethg for (i = 0; i < reps; i++) {
273822Ssethg if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
274822Ssethg return (B_TRUE);
275822Ssethg
276822Ssethg if (i < (reps - 1))
277822Ssethg drv_usecwait(USECS_PER_WAIT);
278822Ssethg }
279822Ssethg return (B_FALSE);
280822Ssethg }
281822Ssethg
282822Ssethg static void
kb8042_clear_input_buffer(struct kb8042 * kb8042,boolean_t polled)283822Ssethg kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
284822Ssethg {
285822Ssethg int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
286822Ssethg I8042_INT_INPUT_DATA;
287822Ssethg
288822Ssethg while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
289822Ssethg (void) ddi_get8(kb8042->handle, kb8042->addr + port);
290822Ssethg }
291822Ssethg }
292822Ssethg
29310087SSeth.Goldberg@Sun.COM /*
29410087SSeth.Goldberg@Sun.COM * kb8042_send_and_expect does all its I/O via polling interfaces
29510087SSeth.Goldberg@Sun.COM */
296822Ssethg static boolean_t
kb8042_send_and_expect(struct kb8042 * kb8042,uint8_t send,uint8_t expect,int timeout,int * error,uint8_t * got)297822Ssethg kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
29810087SSeth.Goldberg@Sun.COM int timeout, int *error, uint8_t *got)
299822Ssethg {
300822Ssethg uint8_t datab;
301822Ssethg int err;
302822Ssethg boolean_t rval;
303822Ssethg
30410087SSeth.Goldberg@Sun.COM ddi_put8(kb8042->handle,
30510087SSeth.Goldberg@Sun.COM kb8042->addr + I8042_POLL_OUTPUT_DATA, send);
306822Ssethg
30710087SSeth.Goldberg@Sun.COM if (kb8042_is_input_avail(kb8042, timeout, B_TRUE)) {
308822Ssethg err = 0;
30910087SSeth.Goldberg@Sun.COM datab = ddi_get8(kb8042->handle,
31010087SSeth.Goldberg@Sun.COM kb8042->addr + I8042_POLL_INPUT_DATA);
311822Ssethg rval = ((datab == expect) ? B_TRUE : B_FALSE);
312822Ssethg } else {
313822Ssethg err = EAGAIN;
314822Ssethg rval = B_FALSE;
315822Ssethg }
316822Ssethg
317822Ssethg if (error != NULL)
318822Ssethg *error = err;
319822Ssethg if (got != NULL)
320822Ssethg *got = datab;
321822Ssethg return (rval);
322822Ssethg }
323822Ssethg
324822Ssethg static const char *
kb8042_error_string(int errcode)325822Ssethg kb8042_error_string(int errcode)
326822Ssethg {
327822Ssethg switch (errcode) {
328822Ssethg case EAGAIN:
329822Ssethg return ("Timed out");
330822Ssethg default:
331822Ssethg return ("Unknown error");
332822Ssethg }
333822Ssethg }
334822Ssethg
33510087SSeth.Goldberg@Sun.COM /*
33610087SSeth.Goldberg@Sun.COM * kb8042_read_scanset works properly because it is called before ddi_add_intr
33710087SSeth.Goldberg@Sun.COM * (if it is called after ddi_add_intr, i8042_intr would call kb8042_intr
33810087SSeth.Goldberg@Sun.COM * instead of just storing the data that comes in from the keyboard, which
33910087SSeth.Goldberg@Sun.COM * would prevent the code here from getting it.)
34010087SSeth.Goldberg@Sun.COM */
341822Ssethg static int
kb8042_read_scanset(struct kb8042 * kb8042)34210087SSeth.Goldberg@Sun.COM kb8042_read_scanset(struct kb8042 *kb8042)
343822Ssethg {
344822Ssethg int scanset = -1;
345822Ssethg int err;
346822Ssethg uint8_t got;
347822Ssethg
348822Ssethg kb8042_clear_input_buffer(kb8042, B_TRUE);
349822Ssethg
350822Ssethg /*
351822Ssethg * Send a "change scan code set" command to the keyboard.
352822Ssethg * It should respond with an ACK.
353822Ssethg */
35410087SSeth.Goldberg@Sun.COM if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, MAX_WAIT_USECS,
35510087SSeth.Goldberg@Sun.COM &err, &got) != B_TRUE) {
356822Ssethg goto fail_read_scanset;
357822Ssethg }
358822Ssethg
359822Ssethg /*
3607658SSeth.Goldberg@Sun.COM * Send a 0. The keyboard should ACK the 0, then it should send the
3617658SSeth.Goldberg@Sun.COM * scan code set in use.
362822Ssethg */
36310087SSeth.Goldberg@Sun.COM if (kb8042_send_and_expect(kb8042, 0, KB_ACK, MAX_WAIT_USECS, &err,
36410087SSeth.Goldberg@Sun.COM &got) != B_TRUE) {
365822Ssethg goto fail_read_scanset;
366822Ssethg }
367822Ssethg
368822Ssethg /*
369822Ssethg * The next input byte from the keyboard should be the scan code
370822Ssethg * set in use, though some keyboards like to send a few more acks
371822Ssethg * just for fun, so blow past those to get the keyboard scan code.
372822Ssethg */
373822Ssethg while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
37410087SSeth.Goldberg@Sun.COM (scanset = ddi_get8(kb8042->handle,
37510087SSeth.Goldberg@Sun.COM kb8042->addr + I8042_POLL_INPUT_DATA)) == KB_ACK)
376822Ssethg ;
377822Ssethg
37810087SSeth.Goldberg@Sun.COM #ifdef DEBUG
379822Ssethg cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
380822Ssethg scanset);
381822Ssethg #endif
382822Ssethg
383822Ssethg return (scanset);
384822Ssethg
385822Ssethg fail_read_scanset:
38610087SSeth.Goldberg@Sun.COM #ifdef DEBUG
387822Ssethg if (err == 0)
388822Ssethg cmn_err(CE_NOTE, "Could not read current scan set from "
389822Ssethg "keyboard: %s. (Expected 0x%x, but got 0x%x).",
390822Ssethg kb8042_error_string(err), KB_ACK, got);
391822Ssethg else
392822Ssethg cmn_err(CE_NOTE, "Could not read current scan set from "
393822Ssethg "keyboard: %s.", kb8042_error_string(err));
394822Ssethg #endif
395822Ssethg return (-1);
396822Ssethg }
397822Ssethg #endif
398822Ssethg
3990Sstevel@tonic-gate static int
kb8042_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)4000Sstevel@tonic-gate kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate int rc;
403822Ssethg int scanset;
404822Ssethg int leds;
4050Sstevel@tonic-gate
406822Ssethg struct kb8042 *kb8042 = &Kdws;
4070Sstevel@tonic-gate static ddi_device_acc_attr_t attr = {
4080Sstevel@tonic-gate DDI_DEVICE_ATTR_V0,
4090Sstevel@tonic-gate DDI_NEVERSWAP_ACC,
4100Sstevel@tonic-gate DDI_STRICTORDER_ACC,
4110Sstevel@tonic-gate };
4120Sstevel@tonic-gate
413822Ssethg switch (cmd) {
414822Ssethg case DDI_RESUME:
415822Ssethg leds = kb8042->leds.commanded;
416822Ssethg kb8042->w_init = 0;
417822Ssethg kb8042_init(kb8042, B_TRUE);
418822Ssethg kb8042_setled(kb8042, leds, B_FALSE);
4195295Srandyf mutex_enter(&kb8042->w_hw_mutex);
4205295Srandyf kb8042->suspended = B_FALSE;
4215295Srandyf if (kb8042->w_qp != NULL) {
4225295Srandyf enableok(WR(kb8042->w_qp));
4235295Srandyf qenable(WR(kb8042->w_qp));
4245295Srandyf }
4255295Srandyf cv_broadcast(&kb8042->suspend_cv);
4265295Srandyf mutex_exit(&kb8042->w_hw_mutex);
427822Ssethg return (DDI_SUCCESS);
4280Sstevel@tonic-gate
429822Ssethg case DDI_ATTACH:
430822Ssethg if (kb8042_dip != NULL)
431822Ssethg return (DDI_FAILURE);
432822Ssethg /* The rest of the function is for attach */
433822Ssethg break;
4340Sstevel@tonic-gate
435822Ssethg default:
436822Ssethg return (DDI_FAILURE);
437822Ssethg }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate kb8042->debugger.mod1 = 58; /* Left Ctrl */
4400Sstevel@tonic-gate kb8042->debugger.mod2 = 60; /* Left Alt */
4410Sstevel@tonic-gate kb8042->debugger.trigger = 33; /* D */
4420Sstevel@tonic-gate kb8042->debugger.mod1_down = B_FALSE;
4430Sstevel@tonic-gate kb8042->debugger.mod2_down = B_FALSE;
4440Sstevel@tonic-gate kb8042->debugger.enabled = B_FALSE;
4450Sstevel@tonic-gate
446822Ssethg kb8042_dip = devi;
447822Ssethg kb8042->init_state = KB8042_UNINITIALIZED;
448822Ssethg
4490Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_FALSE;
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
4525129Smarx DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
453822Ssethg goto failure;
4540Sstevel@tonic-gate }
455822Ssethg
456822Ssethg kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
4595129Smarx (offset_t)0, (offset_t)0, &attr, &kb8042->handle);
4600Sstevel@tonic-gate if (rc != DDI_SUCCESS) {
46110087SSeth.Goldberg@Sun.COM #ifdef DEBUG
4620Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: can't map registers");
4630Sstevel@tonic-gate #endif
464822Ssethg goto failure;
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
467822Ssethg kb8042->init_state |= KB8042_REGS_MAPPED;
468822Ssethg
4690Sstevel@tonic-gate if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
4705129Smarx DDI_SUCCESS) {
4710Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie");
472822Ssethg goto failure;
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
4765295Srandyf cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
4775295Srandyf cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
47810087SSeth.Goldberg@Sun.COM cv_init(&kb8042->cmd_cv, NULL, CV_DRIVER, NULL);
479822Ssethg kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
480822Ssethg
481822Ssethg kb8042_init(kb8042, B_FALSE);
482822Ssethg
483822Ssethg #ifdef __sparc
484822Ssethg /* Detect the scan code set currently in use */
48510087SSeth.Goldberg@Sun.COM scanset = kb8042_read_scanset(kb8042);
486822Ssethg
487822Ssethg if (scanset < 0 && kb8042_warn_unknown_scanset) {
488822Ssethg
489822Ssethg cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
490822Ssethg cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
491822Ssethg cmn_err(CE_CONT, "Defaulting to scan code set %d. If the "
492822Ssethg "keyboard does not ", kb8042_default_scanset);
493822Ssethg cmn_err(CE_CONT, "work properly, add "
494822Ssethg "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
495822Ssethg (kb8042_default_scanset == 1) ? 2 : 1);
496822Ssethg cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
497822Ssethg "restart the system. If you ");
498822Ssethg cmn_err(CE_CONT, "do not want to see this message in the "
499822Ssethg "future, add ");
500822Ssethg cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
501822Ssethg "to /etc/system.\n");
502822Ssethg
503822Ssethg /* Use the default scan code set. */
504822Ssethg scanset = kb8042_default_scanset;
505822Ssethg }
506822Ssethg #else
507822Ssethg /* x86 systems use scan code set 1 -- no detection required */
508822Ssethg scanset = 1;
509822Ssethg #endif
510822Ssethg if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
511822Ssethg cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
512822Ssethg "Unknown scan code set `%d'.", scanset);
513822Ssethg /* Scan code set is not supported */
514822Ssethg goto failure;
515822Ssethg }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate * Turn on interrupts...
5190Sstevel@tonic-gate */
5200Sstevel@tonic-gate if (ddi_add_intr(devi, 0,
5215129Smarx &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
5225129Smarx kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
5230Sstevel@tonic-gate cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
524822Ssethg goto failure;
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate
527822Ssethg kb8042->init_state |= KB8042_INTR_ADDED;
528822Ssethg
5290Sstevel@tonic-gate ddi_report_dev(devi);
530822Ssethg
53110087SSeth.Goldberg@Sun.COM #ifdef DEBUG
5327658SSeth.Goldberg@Sun.COM cmn_err(CE_CONT, "?%s instance #%d READY\n",
5337656SSherry.Moore@Sun.COM DRIVER_NAME(devi), ddi_get_instance(devi));
5340Sstevel@tonic-gate #endif
535822Ssethg
5360Sstevel@tonic-gate return (DDI_SUCCESS);
537822Ssethg
538822Ssethg failure:
539822Ssethg kb8042_cleanup(kb8042);
540822Ssethg return (DDI_FAILURE);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate
543822Ssethg static int
kb8042_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)544822Ssethg kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
545822Ssethg {
546822Ssethg struct kb8042 *kb8042 = &Kdws;
547822Ssethg
548822Ssethg switch (cmd) {
549822Ssethg case DDI_SUSPEND:
5505295Srandyf mutex_enter(&kb8042->w_hw_mutex);
5515295Srandyf ASSERT(kb8042->ops >= 0);
5525295Srandyf while (kb8042->ops > 0)
5535295Srandyf cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
5545295Srandyf kb8042->suspended = B_TRUE;
5555295Srandyf mutex_exit(&kb8042->w_hw_mutex);
556822Ssethg return (DDI_SUCCESS);
557822Ssethg
558822Ssethg case DDI_DETACH:
559822Ssethg /* If someone has a stream open, fail to detach */
560822Ssethg if (kb8042->w_qp != NULL)
561822Ssethg return (DDI_FAILURE);
562822Ssethg
563822Ssethg ASSERT(kb8042_dip == dip);
564822Ssethg
565822Ssethg kb8042_cleanup(kb8042);
566822Ssethg
567822Ssethg return (DDI_SUCCESS);
568822Ssethg
569822Ssethg default:
570822Ssethg return (DDI_FAILURE);
571822Ssethg }
572822Ssethg }
573822Ssethg
5740Sstevel@tonic-gate /*ARGSUSED*/
5750Sstevel@tonic-gate static int
kb8042_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)5760Sstevel@tonic-gate kb8042_getinfo(
5770Sstevel@tonic-gate dev_info_t *dip,
5780Sstevel@tonic-gate ddi_info_cmd_t infocmd,
5790Sstevel@tonic-gate void *arg,
5800Sstevel@tonic-gate void **result)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate register int error;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate switch (infocmd) {
5850Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
5860Sstevel@tonic-gate if (kb8042_dip == NULL) {
5870Sstevel@tonic-gate error = DDI_FAILURE;
5880Sstevel@tonic-gate } else {
5890Sstevel@tonic-gate *result = (void *) kb8042_dip;
5900Sstevel@tonic-gate error = DDI_SUCCESS;
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate break;
5930Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
5940Sstevel@tonic-gate *result = (void *)0;
5950Sstevel@tonic-gate error = DDI_SUCCESS;
5960Sstevel@tonic-gate break;
5970Sstevel@tonic-gate default:
5980Sstevel@tonic-gate error = DDI_FAILURE;
5990Sstevel@tonic-gate break;
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate return (error);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate static void
kb8042_cleanup(struct kb8042 * kb8042)605822Ssethg kb8042_cleanup(struct kb8042 *kb8042)
606822Ssethg {
607845Ssethg ASSERT(kb8042_dip != NULL);
608822Ssethg
60910087SSeth.Goldberg@Sun.COM if (kb8042->init_state & KB8042_INTR_ADDED)
61010087SSeth.Goldberg@Sun.COM ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
61110087SSeth.Goldberg@Sun.COM
6125295Srandyf if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
61310087SSeth.Goldberg@Sun.COM cv_destroy(&kb8042->cmd_cv);
6145295Srandyf cv_destroy(&kb8042->suspend_cv);
6155295Srandyf cv_destroy(&kb8042->ops_cv);
616822Ssethg mutex_destroy(&kb8042->w_hw_mutex);
6175295Srandyf }
618822Ssethg
619822Ssethg if (kb8042->init_state & KB8042_REGS_MAPPED)
620822Ssethg ddi_regs_map_free(&kb8042->handle);
621822Ssethg
622822Ssethg if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
623822Ssethg ddi_remove_minor_node(kb8042_dip, NULL);
624822Ssethg
625822Ssethg kb8042->init_state = KB8042_UNINITIALIZED;
626845Ssethg kb8042_dip = NULL;
627822Ssethg }
628822Ssethg
629822Ssethg static void
kb8042_init(struct kb8042 * kb8042,boolean_t from_resume)630822Ssethg kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate if (kb8042->w_init)
6330Sstevel@tonic-gate return;
6340Sstevel@tonic-gate
635822Ssethg if (!from_resume) {
636822Ssethg kb8042->w_kblayout = 0; /* Default to US */
637822Ssethg kb8042->w_qp = (queue_t *)NULL;
638822Ssethg kb8042->simulated_kbd_type = KB_PC;
639822Ssethg kb8042->leds.commanded = -1; /* Unknown initial state */
640822Ssethg kb8042->leds.desired = -1; /* Unknown initial state */
641822Ssethg }
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate kb8042_wait_poweron(kb8042);
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate kb8042->kb_old_key_pos = 0;
6460Sstevel@tonic-gate
64710087SSeth.Goldberg@Sun.COM /*
64810087SSeth.Goldberg@Sun.COM * Explicitly grab and release the 8042 lock outside of
64910087SSeth.Goldberg@Sun.COM * kb8042_send_to_keyboard, because this is the only situation
65010087SSeth.Goldberg@Sun.COM * where a polling interface is used with locking required.
65110087SSeth.Goldberg@Sun.COM */
65210087SSeth.Goldberg@Sun.COM (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_LOCK);
6530Sstevel@tonic-gate /* Set up the command state machine and start it running. */
65410087SSeth.Goldberg@Sun.COM kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_TRUE);
65510087SSeth.Goldberg@Sun.COM (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_UNLOCK);
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate kb8042->w_init++;
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate /*ARGSUSED2*/
6630Sstevel@tonic-gate static int
kb8042_open(queue_t * qp,dev_t * devp,int flag,int sflag,cred_t * credp)6640Sstevel@tonic-gate kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate struct kb8042 *kb8042;
667*10617SPengcheng.Chen@Sun.COM int err = 0;
6680Sstevel@tonic-gate int initial_leds;
6690Sstevel@tonic-gate int initial_led_mask;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate kb8042 = &Kdws;
6720Sstevel@tonic-gate
6735295Srandyf mutex_enter(&kb8042->w_hw_mutex);
674*10617SPengcheng.Chen@Sun.COM if (qp->q_ptr) {
675*10617SPengcheng.Chen@Sun.COM kb8042->w_dev = *devp;
676*10617SPengcheng.Chen@Sun.COM mutex_exit(&kb8042->w_hw_mutex);
677*10617SPengcheng.Chen@Sun.COM return (0);
678*10617SPengcheng.Chen@Sun.COM }
679*10617SPengcheng.Chen@Sun.COM
680*10617SPengcheng.Chen@Sun.COM if (secpolicy_console(credp) != 0) {
681*10617SPengcheng.Chen@Sun.COM mutex_exit(&kb8042->w_hw_mutex);
682*10617SPengcheng.Chen@Sun.COM return (EPERM);
683*10617SPengcheng.Chen@Sun.COM }
684*10617SPengcheng.Chen@Sun.COM
6855295Srandyf while (kb8042->suspended) {
6865295Srandyf if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
6875295Srandyf 0) {
6885295Srandyf mutex_exit(&kb8042->w_hw_mutex);
6895295Srandyf return (EINTR);
6905295Srandyf }
6915295Srandyf }
6925295Srandyf
6930Sstevel@tonic-gate kb8042->w_dev = *devp;
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);
704*10617SPengcheng.Chen@Sun.COM err = kbtrans_streams_init(qp, sflag,
7055129Smarx (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
7065129Smarx &kb8042->hw_kbtrans,
7075129Smarx initial_leds, initial_led_mask);
7080Sstevel@tonic-gate if (err != 0)
709*10617SPengcheng.Chen@Sun.COM goto out;
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
733*10617SPengcheng.Chen@Sun.COM out:
7345295Srandyf mutex_enter(&kb8042->w_hw_mutex);
7355295Srandyf ASSERT(kb8042->ops > 0);
7365295Srandyf kb8042->ops--;
7375295Srandyf if (kb8042->ops == 0)
7385295Srandyf cv_broadcast(&kb8042->ops_cv);
7395295Srandyf mutex_exit(&kb8042->w_hw_mutex);
7405295Srandyf
741*10617SPengcheng.Chen@Sun.COM return (err);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate /*ARGSUSED1*/
7450Sstevel@tonic-gate static int
kb8042_close(queue_t * qp,int flag,cred_t * credp)7460Sstevel@tonic-gate kb8042_close(queue_t *qp, int flag, cred_t *credp)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate struct kb8042 *kb8042;
7490Sstevel@tonic-gate
7505129Smarx /* If a beep is in progress, stop that */
7515129Smarx (void) beeper_off();
7525129Smarx
7530Sstevel@tonic-gate kb8042 = (struct kb8042 *)qp->q_ptr;
7540Sstevel@tonic-gate
7555295Srandyf mutex_enter(&kb8042->w_hw_mutex);
7565295Srandyf while (kb8042->suspended) {
7575295Srandyf if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
7585295Srandyf 0) {
7595295Srandyf mutex_exit(&kb8042->w_hw_mutex);
7605295Srandyf return (EINTR);
7615295Srandyf }
7625295Srandyf }
7635295Srandyf
7645295Srandyf ASSERT(kb8042->ops >= 0);
7655295Srandyf kb8042->ops++;
7665295Srandyf mutex_exit(&kb8042->w_hw_mutex);
7675295Srandyf
768672Sry162471 (void) kbtrans_streams_fini(kb8042->hw_kbtrans);
769672Sry162471
7700Sstevel@tonic-gate kb8042->w_qp = (queue_t *)NULL;
7710Sstevel@tonic-gate qprocsoff(qp);
7720Sstevel@tonic-gate
7735295Srandyf mutex_enter(&kb8042->w_hw_mutex);
7745295Srandyf ASSERT(kb8042->ops > 0);
7755295Srandyf kb8042->ops--;
7765295Srandyf if (kb8042->ops == 0)
7775295Srandyf cv_broadcast(&kb8042->ops_cv);
7785295Srandyf mutex_exit(&kb8042->w_hw_mutex);
7795295Srandyf
7800Sstevel@tonic-gate return (0);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate static int
kb8042_wsrv(queue_t * qp)7840Sstevel@tonic-gate kb8042_wsrv(queue_t *qp)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate struct kb8042 *kb8042;
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate mblk_t *mp;
7895295Srandyf boolean_t suspended;
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate kb8042 = (struct kb8042 *)qp->q_ptr;
7920Sstevel@tonic-gate
7935295Srandyf mutex_enter(&kb8042->w_hw_mutex);
7945295Srandyf suspended = kb8042->suspended;
7955295Srandyf ASSERT(kb8042->ops >= 0);
7965295Srandyf if (!suspended)
7975295Srandyf kb8042->ops++;
7985295Srandyf mutex_exit(&kb8042->w_hw_mutex);
7995295Srandyf
8005295Srandyf #ifdef NO_KB_DEBUG
8015295Srandyf while (!suspended && (mp = getq(qp)) != NULL) {
8025295Srandyf #else
8035295Srandyf /*
8045295Srandyf * Not taking keyboard input while suspending can make debugging
8055295Srandyf * difficult. However, we still do the ops counting so that we
8065295Srandyf * don't suspend at a bad time.
8075295Srandyf */
8080Sstevel@tonic-gate while ((mp = getq(qp))) {
8095295Srandyf #endif
8100Sstevel@tonic-gate switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
8110Sstevel@tonic-gate case KBTRANS_MESSAGE_HANDLED:
8120Sstevel@tonic-gate continue;
8130Sstevel@tonic-gate case KBTRANS_MESSAGE_NOT_HANDLED:
8140Sstevel@tonic-gate break;
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate switch (mp->b_datap->db_type) {
8170Sstevel@tonic-gate case M_IOCTL:
8180Sstevel@tonic-gate kb8042_ioctlmsg(kb8042, qp, mp);
8190Sstevel@tonic-gate continue;
8200Sstevel@tonic-gate case M_IOCDATA:
8210Sstevel@tonic-gate kb8042_iocdatamsg(qp, mp);
8220Sstevel@tonic-gate continue;
8230Sstevel@tonic-gate case M_DELAY:
8240Sstevel@tonic-gate case M_STARTI:
8250Sstevel@tonic-gate case M_STOPI:
8260Sstevel@tonic-gate case M_READ: /* ignore, no buffered data */
8270Sstevel@tonic-gate freemsg(mp);
8280Sstevel@tonic-gate continue;
8290Sstevel@tonic-gate case M_FLUSH:
8300Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW;
8310Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR)
8320Sstevel@tonic-gate qreply(qp, mp);
8330Sstevel@tonic-gate else
8340Sstevel@tonic-gate freemsg(mp);
8350Sstevel@tonic-gate continue;
8360Sstevel@tonic-gate default:
8370Sstevel@tonic-gate cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
8385129Smarx mp->b_datap->db_type);
8390Sstevel@tonic-gate freemsg(mp);
8400Sstevel@tonic-gate continue;
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate }
8435295Srandyf
8445295Srandyf mutex_enter(&kb8042->w_hw_mutex);
8455295Srandyf if (!suspended) {
8465295Srandyf ASSERT(kb8042->ops > 0);
8475295Srandyf kb8042->ops--;
8485295Srandyf if (kb8042->ops == 0)
8495295Srandyf cv_broadcast(&kb8042->ops_cv);
8505295Srandyf }
8515295Srandyf mutex_exit(&kb8042->w_hw_mutex);
8525295Srandyf
8530Sstevel@tonic-gate return (0);
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate static void
8570Sstevel@tonic-gate kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate struct iocblk *iocp;
8600Sstevel@tonic-gate mblk_t *datap;
8610Sstevel@tonic-gate int error;
8620Sstevel@tonic-gate int tmp;
8635129Smarx int cycles;
8645129Smarx int frequency;
8655129Smarx int msecs;
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate switch (iocp->ioc_cmd) {
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate case CONSOPENPOLLEDIO:
8720Sstevel@tonic-gate error = miocpullup(mp, sizeof (struct cons_polledio *));
8730Sstevel@tonic-gate if (error != 0) {
8740Sstevel@tonic-gate miocnak(qp, mp, 0, error);
8750Sstevel@tonic-gate return;
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate /*
8790Sstevel@tonic-gate * We are given an appropriate-sized data block,
8800Sstevel@tonic-gate * and return a pointer to our structure in it.
8810Sstevel@tonic-gate */
8820Sstevel@tonic-gate *(struct cons_polledio **)mp->b_cont->b_rptr =
8830Sstevel@tonic-gate &kb8042->polledio;
8840Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK;
8850Sstevel@tonic-gate iocp->ioc_error = 0;
8860Sstevel@tonic-gate qreply(qp, mp);
8870Sstevel@tonic-gate break;
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate case CONSCLOSEPOLLEDIO:
8900Sstevel@tonic-gate miocack(qp, mp, 0, 0);
8910Sstevel@tonic-gate break;
8920Sstevel@tonic-gate
8930Sstevel@tonic-gate case CONSSETABORTENABLE:
8940Sstevel@tonic-gate if (iocp->ioc_count != TRANSPARENT) {
8950Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
8960Sstevel@tonic-gate return;
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
9000Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9010Sstevel@tonic-gate break;
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate /*
9040Sstevel@tonic-gate * Valid only in TR_UNTRANS_MODE mode.
9050Sstevel@tonic-gate */
9060Sstevel@tonic-gate case CONSSETKBDTYPE:
9070Sstevel@tonic-gate error = miocpullup(mp, sizeof (int));
9080Sstevel@tonic-gate if (error != 0) {
9090Sstevel@tonic-gate miocnak(qp, mp, 0, error);
9100Sstevel@tonic-gate return;
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate tmp = *(int *)mp->b_cont->b_rptr;
9130Sstevel@tonic-gate if (tmp != KB_PC && tmp != KB_USB) {
9140Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
9150Sstevel@tonic-gate break;
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate kb8042->simulated_kbd_type = tmp;
9180Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9190Sstevel@tonic-gate break;
9200Sstevel@tonic-gate
9210Sstevel@tonic-gate case KIOCLAYOUT:
9220Sstevel@tonic-gate if (kb8042->w_kblayout == -1) {
9230Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
9240Sstevel@tonic-gate return;
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9280Sstevel@tonic-gate miocnak(qp, mp, 0, ENOMEM);
9290Sstevel@tonic-gate return;
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate
9320Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_USB)
9330Sstevel@tonic-gate *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
9340Sstevel@tonic-gate else
9350Sstevel@tonic-gate *(int *)datap->b_wptr = kb8042->w_kblayout;
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate datap->b_wptr += sizeof (int);
9380Sstevel@tonic-gate if (mp->b_cont)
9390Sstevel@tonic-gate freemsg(mp->b_cont);
9400Sstevel@tonic-gate mp->b_cont = datap;
9410Sstevel@tonic-gate iocp->ioc_count = sizeof (int);
9420Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK;
9430Sstevel@tonic-gate iocp->ioc_error = 0;
9440Sstevel@tonic-gate qreply(qp, mp);
9450Sstevel@tonic-gate break;
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate case KIOCSLAYOUT:
9480Sstevel@tonic-gate if (iocp->ioc_count != TRANSPARENT) {
9490Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
9500Sstevel@tonic-gate return;
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
9540Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9550Sstevel@tonic-gate break;
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate case KIOCCMD:
9580Sstevel@tonic-gate error = miocpullup(mp, sizeof (int));
9590Sstevel@tonic-gate if (error != 0) {
9600Sstevel@tonic-gate miocnak(qp, mp, 0, error);
9610Sstevel@tonic-gate return;
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
9650Sstevel@tonic-gate miocack(qp, mp, 0, 0);
9660Sstevel@tonic-gate break;
9670Sstevel@tonic-gate
9685129Smarx case KIOCMKTONE:
9695129Smarx if (iocp->ioc_count != TRANSPARENT) {
9705129Smarx miocnak(qp, mp, 0, EINVAL);
9715129Smarx return;
9725129Smarx }
9735129Smarx
9745129Smarx tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
9755129Smarx cycles = tmp & 0xffff;
9765129Smarx msecs = (tmp >> 16) & 0xffff;
9775129Smarx
9785129Smarx if (cycles == 0)
9795129Smarx frequency = UINT16_MAX;
9805129Smarx else if (cycles == UINT16_MAX)
9815129Smarx frequency = 0;
9825129Smarx else {
9835129Smarx frequency = (PIT_HZ + cycles / 2) / cycles;
9845129Smarx if (frequency > UINT16_MAX)
9855129Smarx frequency = UINT16_MAX;
9865129Smarx }
9875129Smarx
9885129Smarx error = beep_mktone(frequency, msecs);
9895129Smarx if (error != 0)
9905129Smarx miocnak(qp, mp, 0, error);
9915129Smarx else
9925129Smarx miocack(qp, mp, 0, 0);
9935129Smarx break;
9945129Smarx
9950Sstevel@tonic-gate default:
9960Sstevel@tonic-gate #ifdef DEBUG1
9970Sstevel@tonic-gate cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
9980Sstevel@tonic-gate #endif
9990Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL);
10000Sstevel@tonic-gate return;
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate /*
10050Sstevel@tonic-gate * Process a byte received from the keyboard
10060Sstevel@tonic-gate */
10070Sstevel@tonic-gate static void
10080Sstevel@tonic-gate kb8042_received_byte(
10090Sstevel@tonic-gate struct kb8042 *kb8042,
10100Sstevel@tonic-gate int scancode) /* raw scan code */
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate boolean_t legit; /* is this a legit key pos'n? */
10130Sstevel@tonic-gate int key_pos = -1;
10140Sstevel@tonic-gate enum keystate state;
10150Sstevel@tonic-gate boolean_t synthetic_release_needed;
10160Sstevel@tonic-gate
101710087SSeth.Goldberg@Sun.COM /*
101810087SSeth.Goldberg@Sun.COM * Intercept ACK and RESEND and signal the condition that
101910087SSeth.Goldberg@Sun.COM * kb8042_send_and_wait is waiting for.
102010087SSeth.Goldberg@Sun.COM */
102110087SSeth.Goldberg@Sun.COM if (scancode == KB_ACK) {
102210087SSeth.Goldberg@Sun.COM mutex_enter(&kb8042->w_hw_mutex);
102310087SSeth.Goldberg@Sun.COM kb8042->acked = 1;
102410087SSeth.Goldberg@Sun.COM cv_signal(&kb8042->cmd_cv);
102510087SSeth.Goldberg@Sun.COM mutex_exit(&kb8042->w_hw_mutex);
102610087SSeth.Goldberg@Sun.COM return;
102710087SSeth.Goldberg@Sun.COM } else if (scancode == KB_RESEND) {
102810087SSeth.Goldberg@Sun.COM mutex_enter(&kb8042->w_hw_mutex);
102910087SSeth.Goldberg@Sun.COM kb8042->need_retry = 1;
103010087SSeth.Goldberg@Sun.COM cv_signal(&kb8042->cmd_cv);
103110087SSeth.Goldberg@Sun.COM mutex_exit(&kb8042->w_hw_mutex);
103210087SSeth.Goldberg@Sun.COM return;
103310087SSeth.Goldberg@Sun.COM }
10340Sstevel@tonic-gate
10350Sstevel@tonic-gate if (!kb8042->w_init) /* can't do anything anyway */
10360Sstevel@tonic-gate return;
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
10395129Smarx &synthetic_release_needed);
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate if (legit == 0) {
10420Sstevel@tonic-gate /* Eaten by translation */
10430Sstevel@tonic-gate return;
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate /*
10470Sstevel@tonic-gate * Don't know if we want this permanently, but it seems interesting
10480Sstevel@tonic-gate * for the moment.
10490Sstevel@tonic-gate */
10500Sstevel@tonic-gate if (key_pos == kb8042->debugger.mod1) {
10510Sstevel@tonic-gate kb8042->debugger.mod1_down = (state == KEY_PRESSED);
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate if (key_pos == kb8042->debugger.mod2) {
10540Sstevel@tonic-gate kb8042->debugger.mod2_down = (state == KEY_PRESSED);
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate if (kb8042->debugger.enabled &&
10570Sstevel@tonic-gate key_pos == kb8042->debugger.trigger &&
10580Sstevel@tonic-gate kb8042->debugger.mod1_down &&
10590Sstevel@tonic-gate kb8042->debugger.mod2_down) {
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate * Require new presses of the modifiers.
10620Sstevel@tonic-gate */
10630Sstevel@tonic-gate kb8042->debugger.mod1_down = B_FALSE;
10640Sstevel@tonic-gate kb8042->debugger.mod2_down = B_FALSE;
10650Sstevel@tonic-gate abort_sequence_enter(NULL);
10660Sstevel@tonic-gate return;
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate /*
10700Sstevel@tonic-gate * If there's no queue above us - as can happen if we've been
10710Sstevel@tonic-gate * attached but not opened - drop the keystroke.
10720Sstevel@tonic-gate * Note that we do this here instead of above so that
10730Sstevel@tonic-gate * Ctrl-Alt-D still works.
10740Sstevel@tonic-gate */
10750Sstevel@tonic-gate if (kb8042->w_qp == NULL) {
10760Sstevel@tonic-gate return;
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate /*
10800Sstevel@tonic-gate * This is to filter out auto repeat since it can't be
10810Sstevel@tonic-gate * turned off at the hardware. (Yeah, yeah, PS/2 keyboards
10820Sstevel@tonic-gate * can. Don't know whether they've taken over the world.
10830Sstevel@tonic-gate * Don't think so.)
10840Sstevel@tonic-gate */
10850Sstevel@tonic-gate if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
10860Sstevel@tonic-gate return;
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate
10890Sstevel@tonic-gate
109010087SSeth.Goldberg@Sun.COM kb8042_process_key(kb8042, key_pos, state);
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate * This is a total hack. For some stupid reason, the two additional
10940Sstevel@tonic-gate * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
10950Sstevel@tonic-gate * only. We synthesize a release immediately.
10960Sstevel@tonic-gate */
10970Sstevel@tonic-gate if (synthetic_release_needed) {
10980Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
10990Sstevel@tonic-gate kb8042_process_key(kb8042, key_pos, state);
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate
11030Sstevel@tonic-gate
11040Sstevel@tonic-gate static void
11050Sstevel@tonic-gate kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
11060Sstevel@tonic-gate enum keystate state)
11070Sstevel@tonic-gate {
11080Sstevel@tonic-gate kbtrans_key_t key;
11090Sstevel@tonic-gate
11100Sstevel@tonic-gate ASSERT(key_pos >= 0 && key_pos <= 255);
11110Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_PC) {
11120Sstevel@tonic-gate kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
11130Sstevel@tonic-gate } else if (kb8042->simulated_kbd_type == KB_USB) {
11140Sstevel@tonic-gate key = keytab_pc2usb[key_pos];
11150Sstevel@tonic-gate if (key != 0) {
11160Sstevel@tonic-gate kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate
11210Sstevel@tonic-gate /*
11220Sstevel@tonic-gate * Called from interrupt handler when keyboard interrupt occurs.
11230Sstevel@tonic-gate */
11240Sstevel@tonic-gate static uint_t
11250Sstevel@tonic-gate kb8042_intr(caddr_t arg)
11260Sstevel@tonic-gate {
11270Sstevel@tonic-gate uchar_t scancode; /* raw scan code */
11280Sstevel@tonic-gate int rc;
11290Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)arg;
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate rc = DDI_INTR_UNCLAIMED;
11320Sstevel@tonic-gate
1133822Ssethg if (kb8042->init_state == KB8042_UNINITIALIZED)
1134822Ssethg return (DDI_INTR_UNCLAIMED);
1135822Ssethg
11360Sstevel@tonic-gate /* don't care if drv_setparm succeeds */
11370Sstevel@tonic-gate (void) drv_setparm(SYSRINT, 1);
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
11400Sstevel@tonic-gate != 0) {
11410Sstevel@tonic-gate rc = DDI_INTR_CLAIMED;
11420Sstevel@tonic-gate
11430Sstevel@tonic-gate scancode = ddi_get8(kb8042->handle,
11445129Smarx kb8042->addr + I8042_INT_INPUT_DATA);
11450Sstevel@tonic-gate
11460Sstevel@tonic-gate kb8042_received_byte(kb8042, scancode);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate return (rc);
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate
11520Sstevel@tonic-gate static void
11530Sstevel@tonic-gate kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate struct copyresp *csp;
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate csp = (struct copyresp *)mp->b_rptr;
11580Sstevel@tonic-gate if (csp->cp_rval) {
11590Sstevel@tonic-gate freemsg(mp);
11600Sstevel@tonic-gate return;
11610Sstevel@tonic-gate }
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate switch (csp->cp_cmd) {
11640Sstevel@tonic-gate default:
11650Sstevel@tonic-gate miocack(qp, mp, 0, 0);
11660Sstevel@tonic-gate break;
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate
11700Sstevel@tonic-gate static boolean_t
11710Sstevel@tonic-gate kb8042_polled_keycheck(
11720Sstevel@tonic-gate struct kbtrans_hardware *hw,
11730Sstevel@tonic-gate int *key,
11740Sstevel@tonic-gate enum keystate *state)
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)hw;
11770Sstevel@tonic-gate int scancode;
11780Sstevel@tonic-gate boolean_t legit;
11790Sstevel@tonic-gate boolean_t synthetic_release_needed;
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate if (kb8042->polled_synthetic_release_pending) {
11820Sstevel@tonic-gate *key = kb8042->polled_synthetic_release_key;
11830Sstevel@tonic-gate *state = KEY_RELEASED;
11840Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_FALSE;
11850Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, *key, *state);
11860Sstevel@tonic-gate return (B_TRUE);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate for (;;) {
11900Sstevel@tonic-gate if (ddi_get8(kb8042->handle,
11910Sstevel@tonic-gate kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
11920Sstevel@tonic-gate return (B_FALSE);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate scancode = ddi_get8(kb8042->handle,
11965129Smarx kb8042->addr + I8042_POLL_INPUT_DATA);
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate legit = KeyboardConvertScan(kb8042, scancode, key, state,
11995129Smarx &synthetic_release_needed);
12000Sstevel@tonic-gate if (!legit) {
12010Sstevel@tonic-gate continue;
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate /*
12040Sstevel@tonic-gate * For the moment at least, we rely on hardware autorepeat
12050Sstevel@tonic-gate * for polled I/O autorepeat. However, for coordination
12060Sstevel@tonic-gate * with the interrupt-driven code, maintain the last key
12070Sstevel@tonic-gate * pressed.
12080Sstevel@tonic-gate */
12090Sstevel@tonic-gate (void) kb8042_autorepeat_detect(kb8042, *key, *state);
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate /*
12120Sstevel@tonic-gate * This is a total hack to support two additional keys
12130Sstevel@tonic-gate * on Korean keyboards. They report only on press, and
12140Sstevel@tonic-gate * so we synthesize a release. Most likely this will
12150Sstevel@tonic-gate * never be important to polled I/O, but if I do it
12160Sstevel@tonic-gate * "right" the first time it _won't_ be an issue.
12170Sstevel@tonic-gate */
12180Sstevel@tonic-gate if (synthetic_release_needed) {
12190Sstevel@tonic-gate kb8042->polled_synthetic_release_pending = B_TRUE;
12200Sstevel@tonic-gate kb8042->polled_synthetic_release_key = *key;
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate
12230Sstevel@tonic-gate if (kb8042->simulated_kbd_type == KB_USB) {
12240Sstevel@tonic-gate *key = keytab_pc2usb[*key];
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate return (B_TRUE);
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate
12300Sstevel@tonic-gate static void
1231822Ssethg kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1232822Ssethg {
1233822Ssethg kb8042->leds.desired = led_state;
1234822Ssethg
1235822Ssethg if (!polled)
1236822Ssethg mutex_enter(&kb8042->w_hw_mutex);
1237822Ssethg
12387658SSeth.Goldberg@Sun.COM if (kb8042->leds.desired != kb8042->leds.commanded) {
12397658SSeth.Goldberg@Sun.COM kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
12407658SSeth.Goldberg@Sun.COM }
1241822Ssethg
1242822Ssethg if (!polled)
1243822Ssethg mutex_exit(&kb8042->w_hw_mutex);
1244822Ssethg }
1245822Ssethg
1246822Ssethg static void
12470Sstevel@tonic-gate kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
12480Sstevel@tonic-gate {
12490Sstevel@tonic-gate struct kb8042 *kb8042 = (struct kb8042 *)hw;
1250822Ssethg kb8042_setled(kb8042, led_state, B_TRUE);
1251822Ssethg }
12520Sstevel@tonic-gate
1253822Ssethg static void
1254822Ssethg kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1255822Ssethg {
1256822Ssethg struct kb8042 *kb8042 = (struct kb8042 *)hw;
1257822Ssethg kb8042_setled(kb8042, led_state, B_FALSE);
12580Sstevel@tonic-gate }
12590Sstevel@tonic-gate
126010087SSeth.Goldberg@Sun.COM
126110087SSeth.Goldberg@Sun.COM static int
126210087SSeth.Goldberg@Sun.COM kb8042_send_and_wait(struct kb8042 *kb8042, uint8_t u8, boolean_t polled)
126310087SSeth.Goldberg@Sun.COM {
126410087SSeth.Goldberg@Sun.COM uint8_t *outp = kb8042->addr +
126510087SSeth.Goldberg@Sun.COM (polled ? I8042_POLL_OUTPUT_DATA : I8042_INT_OUTPUT_DATA);
126610087SSeth.Goldberg@Sun.COM uint8_t *inavp = kb8042->addr +
126710087SSeth.Goldberg@Sun.COM (polled ? I8042_POLL_INPUT_AVAIL : I8042_INT_INPUT_AVAIL);
126810087SSeth.Goldberg@Sun.COM uint8_t *inp = kb8042->addr +
126910087SSeth.Goldberg@Sun.COM (polled ? I8042_POLL_INPUT_DATA : I8042_INT_INPUT_DATA);
127010087SSeth.Goldberg@Sun.COM uint8_t b;
127110087SSeth.Goldberg@Sun.COM int ms_waited;
127210087SSeth.Goldberg@Sun.COM int timedout;
127310087SSeth.Goldberg@Sun.COM int expire;
127410087SSeth.Goldberg@Sun.COM int retries = 0;
127510087SSeth.Goldberg@Sun.COM
127610087SSeth.Goldberg@Sun.COM do {
127710087SSeth.Goldberg@Sun.COM kb8042->acked = 0;
127810087SSeth.Goldberg@Sun.COM kb8042->need_retry = 0;
127910087SSeth.Goldberg@Sun.COM ms_waited = 0; /* Zero it whether polled or not */
128010087SSeth.Goldberg@Sun.COM timedout = 0;
128110087SSeth.Goldberg@Sun.COM
128210087SSeth.Goldberg@Sun.COM ddi_put8(kb8042->handle, outp, u8);
128310087SSeth.Goldberg@Sun.COM
128410087SSeth.Goldberg@Sun.COM while (!kb8042->acked && !kb8042->need_retry && !timedout) {
128510087SSeth.Goldberg@Sun.COM
128610087SSeth.Goldberg@Sun.COM if (polled) {
128710087SSeth.Goldberg@Sun.COM if (ddi_get8(kb8042->handle, inavp)) {
128810087SSeth.Goldberg@Sun.COM b = ddi_get8(kb8042->handle, inp);
128910087SSeth.Goldberg@Sun.COM switch (b) {
129010087SSeth.Goldberg@Sun.COM case KB_ACK:
129110087SSeth.Goldberg@Sun.COM kb8042->acked = 1;
129210087SSeth.Goldberg@Sun.COM break;
129310087SSeth.Goldberg@Sun.COM case KB_RESEND:
129410087SSeth.Goldberg@Sun.COM kb8042->need_retry = 1;
129510087SSeth.Goldberg@Sun.COM break;
129610087SSeth.Goldberg@Sun.COM default:
129710087SSeth.Goldberg@Sun.COM /*
129810087SSeth.Goldberg@Sun.COM * drop it: We should never
129910087SSeth.Goldberg@Sun.COM * get scancodes while
130010087SSeth.Goldberg@Sun.COM * we're in the middle of a
130110087SSeth.Goldberg@Sun.COM * command anyway.
130210087SSeth.Goldberg@Sun.COM */
130310087SSeth.Goldberg@Sun.COM #ifdef DEBUG
130410087SSeth.Goldberg@Sun.COM cmn_err(CE_WARN, "!Unexpected "
130510087SSeth.Goldberg@Sun.COM " byte 0x%x", b);
130610087SSeth.Goldberg@Sun.COM #endif
130710087SSeth.Goldberg@Sun.COM break;
130810087SSeth.Goldberg@Sun.COM }
130910087SSeth.Goldberg@Sun.COM }
131010087SSeth.Goldberg@Sun.COM
131110087SSeth.Goldberg@Sun.COM /*
131210087SSeth.Goldberg@Sun.COM * Wait 1ms if an ACK wasn't received yet
131310087SSeth.Goldberg@Sun.COM */
131410087SSeth.Goldberg@Sun.COM if (!kb8042->acked) {
131510087SSeth.Goldberg@Sun.COM drv_usecwait(1000);
131610087SSeth.Goldberg@Sun.COM ms_waited++;
131710087SSeth.Goldberg@Sun.COM if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
131810087SSeth.Goldberg@Sun.COM timedout = B_TRUE;
131910087SSeth.Goldberg@Sun.COM }
132010087SSeth.Goldberg@Sun.COM } else {
132110087SSeth.Goldberg@Sun.COM /* Interrupt-driven */
132210087SSeth.Goldberg@Sun.COM expire = ddi_get_lbolt() +
132310087SSeth.Goldberg@Sun.COM drv_usectohz(MAX_KB8042_WAIT_MAX_MS * 1000);
132410087SSeth.Goldberg@Sun.COM
132510087SSeth.Goldberg@Sun.COM /*
132610087SSeth.Goldberg@Sun.COM * If cv_timedwait returned -1 and we neither
132710087SSeth.Goldberg@Sun.COM * received an ACK nor a RETRY response, then
132810087SSeth.Goldberg@Sun.COM * we timed out.
132910087SSeth.Goldberg@Sun.COM */
133010087SSeth.Goldberg@Sun.COM if (cv_timedwait(&kb8042->cmd_cv,
133110087SSeth.Goldberg@Sun.COM &kb8042->w_hw_mutex, expire) == -1 &&
133210087SSeth.Goldberg@Sun.COM !kb8042->acked && !kb8042->need_retry) {
133310087SSeth.Goldberg@Sun.COM timedout = B_TRUE;
133410087SSeth.Goldberg@Sun.COM }
133510087SSeth.Goldberg@Sun.COM }
133610087SSeth.Goldberg@Sun.COM
133710087SSeth.Goldberg@Sun.COM }
133810087SSeth.Goldberg@Sun.COM } while ((kb8042->need_retry || timedout) &&
133910087SSeth.Goldberg@Sun.COM ++retries < MAX_KB8042_RETRIES);
134010087SSeth.Goldberg@Sun.COM
134110087SSeth.Goldberg@Sun.COM return (kb8042->acked);
134210087SSeth.Goldberg@Sun.COM }
134310087SSeth.Goldberg@Sun.COM
134410087SSeth.Goldberg@Sun.COM /*
134510087SSeth.Goldberg@Sun.COM * kb8042_send_to_keyboard should be called with w_hw_mutex held if
134610087SSeth.Goldberg@Sun.COM * polled is FALSE.
134710087SSeth.Goldberg@Sun.COM */
13480Sstevel@tonic-gate static void
13490Sstevel@tonic-gate kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
13500Sstevel@tonic-gate {
13517658SSeth.Goldberg@Sun.COM
13527658SSeth.Goldberg@Sun.COM /*
135310087SSeth.Goldberg@Sun.COM * KB_SET_LED and KB_ENABLE are special commands which require blocking
135410087SSeth.Goldberg@Sun.COM * other 8042 consumers while executing.
13557658SSeth.Goldberg@Sun.COM *
13567658SSeth.Goldberg@Sun.COM * Other commands/data are sent using the single put8 I/O access
13577658SSeth.Goldberg@Sun.COM * function.
13587658SSeth.Goldberg@Sun.COM */
13597658SSeth.Goldberg@Sun.COM if (byte == KB_SET_LED) {
136010087SSeth.Goldberg@Sun.COM
136110087SSeth.Goldberg@Sun.COM if (!polled) {
136210087SSeth.Goldberg@Sun.COM (void) ddi_get8(kb8042->handle, kb8042->addr +
136310087SSeth.Goldberg@Sun.COM I8042_LOCK);
136410087SSeth.Goldberg@Sun.COM }
136510087SSeth.Goldberg@Sun.COM
136610087SSeth.Goldberg@Sun.COM if (kb8042_send_and_wait(kb8042, KB_SET_LED, polled)) {
136710087SSeth.Goldberg@Sun.COM /*
136810087SSeth.Goldberg@Sun.COM * Ignore return value, as there's nothing we can
136910087SSeth.Goldberg@Sun.COM * do about it if the SET LED command fails.
137010087SSeth.Goldberg@Sun.COM */
137110087SSeth.Goldberg@Sun.COM (void) kb8042_send_and_wait(kb8042,
137210087SSeth.Goldberg@Sun.COM kb8042_xlate_leds(kb8042->leds.desired), polled);
137310087SSeth.Goldberg@Sun.COM }
137410087SSeth.Goldberg@Sun.COM
137510087SSeth.Goldberg@Sun.COM if (!polled) {
137610087SSeth.Goldberg@Sun.COM (void) ddi_get8(kb8042->handle, kb8042->addr +
137710087SSeth.Goldberg@Sun.COM I8042_UNLOCK);
13787658SSeth.Goldberg@Sun.COM }
13797658SSeth.Goldberg@Sun.COM kb8042->leds.commanded = kb8042->leds.desired;
13807658SSeth.Goldberg@Sun.COM
13817658SSeth.Goldberg@Sun.COM } else if (byte == KB_ENABLE) {
13827658SSeth.Goldberg@Sun.COM
138310087SSeth.Goldberg@Sun.COM if (!polled) {
138410087SSeth.Goldberg@Sun.COM (void) ddi_get8(kb8042->handle, kb8042->addr +
138510087SSeth.Goldberg@Sun.COM I8042_LOCK);
13867658SSeth.Goldberg@Sun.COM }
138710087SSeth.Goldberg@Sun.COM
138810087SSeth.Goldberg@Sun.COM (void) kb8042_send_and_wait(kb8042, KB_ENABLE, polled);
138910087SSeth.Goldberg@Sun.COM
139010087SSeth.Goldberg@Sun.COM if (!polled) {
139110087SSeth.Goldberg@Sun.COM (void) ddi_get8(kb8042->handle, kb8042->addr +
139210087SSeth.Goldberg@Sun.COM I8042_UNLOCK);
139310087SSeth.Goldberg@Sun.COM }
139410087SSeth.Goldberg@Sun.COM
13950Sstevel@tonic-gate } else {
13967658SSeth.Goldberg@Sun.COM /* All other commands use the "normal" virtual output port */
13977658SSeth.Goldberg@Sun.COM if (polled) {
13987658SSeth.Goldberg@Sun.COM ddi_put8(kb8042->handle,
13997658SSeth.Goldberg@Sun.COM kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
14007658SSeth.Goldberg@Sun.COM } else {
14017658SSeth.Goldberg@Sun.COM ddi_put8(kb8042->handle,
14027658SSeth.Goldberg@Sun.COM kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
14037658SSeth.Goldberg@Sun.COM }
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate }
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate /*
14080Sstevel@tonic-gate * Wait until the keyboard is fully up, maybe.
14090Sstevel@tonic-gate * We may be the first person to talk to the keyboard, in which case
14100Sstevel@tonic-gate * it's patiently waiting to say "AA" to us to tell us it's up.
14110Sstevel@tonic-gate * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
14120Sstevel@tonic-gate * good bet that we've already spent that long getting to that point,
14130Sstevel@tonic-gate * so we'll only wait long enough for the communications electronics to
14140Sstevel@tonic-gate * run.
14150Sstevel@tonic-gate */
14160Sstevel@tonic-gate static void
14170Sstevel@tonic-gate kb8042_wait_poweron(struct kb8042 *kb8042)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate int cnt;
14200Sstevel@tonic-gate int ready;
14210Sstevel@tonic-gate
1422822Ssethg /* wait for up to 250 ms for a response */
14230Sstevel@tonic-gate for (cnt = 0; cnt < 250; cnt++) {
14240Sstevel@tonic-gate ready = ddi_get8(kb8042->handle,
14255129Smarx kb8042->addr + I8042_INT_INPUT_AVAIL);
14260Sstevel@tonic-gate if (ready != 0)
14270Sstevel@tonic-gate break;
14280Sstevel@tonic-gate drv_usecwait(1000);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate
14310Sstevel@tonic-gate /*
14320Sstevel@tonic-gate * If there's something pending, read and discard it. If not,
14330Sstevel@tonic-gate * assume things are OK anyway - maybe somebody else ate it
14340Sstevel@tonic-gate * already. (On a PC, the BIOS almost certainly did.)
14350Sstevel@tonic-gate */
14360Sstevel@tonic-gate if (ready != 0) {
143710087SSeth.Goldberg@Sun.COM (void) ddi_get8(kb8042->handle,
14385129Smarx kb8042->addr + I8042_INT_INPUT_DATA);
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate
14420Sstevel@tonic-gate static int
14430Sstevel@tonic-gate kb8042_xlate_leds(int led)
14440Sstevel@tonic-gate {
14450Sstevel@tonic-gate int res;
14460Sstevel@tonic-gate
14470Sstevel@tonic-gate res = 0;
14480Sstevel@tonic-gate
14490Sstevel@tonic-gate if (led & LED_NUM_LOCK)
14500Sstevel@tonic-gate res |= LED_NUM;
14510Sstevel@tonic-gate if (led & LED_SCROLL_LOCK)
14520Sstevel@tonic-gate res |= LED_SCR;
14530Sstevel@tonic-gate if (led & LED_CAPS_LOCK)
14540Sstevel@tonic-gate res |= LED_CAP;
14550Sstevel@tonic-gate
14560Sstevel@tonic-gate return (res);
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate
14590Sstevel@tonic-gate /*ARGSUSED*/
14600Sstevel@tonic-gate static void
14610Sstevel@tonic-gate kb8042_get_initial_leds(
14620Sstevel@tonic-gate struct kb8042 *kb8042,
14630Sstevel@tonic-gate int *initial_leds,
14640Sstevel@tonic-gate int *initial_led_mask)
14650Sstevel@tonic-gate {
14667658SSeth.Goldberg@Sun.COM #if defined(__i386) || defined(__amd64)
14670Sstevel@tonic-gate extern caddr_t p0_va;
14680Sstevel@tonic-gate uint8_t bios_kb_flag;
14690Sstevel@tonic-gate
14700Sstevel@tonic-gate bios_kb_flag = p0_va[BIOS_KB_FLAG];
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
14730Sstevel@tonic-gate *initial_leds = 0;
14740Sstevel@tonic-gate if (bios_kb_flag & BIOS_CAPS_STATE)
14750Sstevel@tonic-gate *initial_leds |= LED_CAPS_LOCK;
14760Sstevel@tonic-gate if (bios_kb_flag & BIOS_NUM_STATE)
14770Sstevel@tonic-gate *initial_leds |= LED_NUM_LOCK;
14780Sstevel@tonic-gate if (bios_kb_flag & BIOS_SCROLL_STATE)
14790Sstevel@tonic-gate *initial_leds |= LED_SCROLL_LOCK;
14800Sstevel@tonic-gate #else
14810Sstevel@tonic-gate *initial_leds = 0;
14820Sstevel@tonic-gate *initial_led_mask = 0;
14830Sstevel@tonic-gate #endif
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate
14860Sstevel@tonic-gate static boolean_t
14870Sstevel@tonic-gate kb8042_autorepeat_detect(
14880Sstevel@tonic-gate struct kb8042 *kb8042,
14890Sstevel@tonic-gate int key_pos,
14900Sstevel@tonic-gate enum keystate state)
14910Sstevel@tonic-gate {
14920Sstevel@tonic-gate if (state == KEY_RELEASED) {
14930Sstevel@tonic-gate if (kb8042->kb_old_key_pos == key_pos)
14940Sstevel@tonic-gate kb8042->kb_old_key_pos = 0;
14950Sstevel@tonic-gate } else {
14960Sstevel@tonic-gate if (kb8042->kb_old_key_pos == key_pos) {
14970Sstevel@tonic-gate return (B_TRUE);
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate kb8042->kb_old_key_pos = key_pos;
15000Sstevel@tonic-gate }
15010Sstevel@tonic-gate return (B_FALSE);
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate
15040Sstevel@tonic-gate /* ARGSUSED */
15050Sstevel@tonic-gate static void
15060Sstevel@tonic-gate kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
15070Sstevel@tonic-gate {
15080Sstevel@tonic-gate switch (cmd) {
15090Sstevel@tonic-gate case KBD_CMD_BELL:
15105129Smarx (void) beeper_on(BEEP_TYPE4);
15110Sstevel@tonic-gate break;
15120Sstevel@tonic-gate case KBD_CMD_NOBELL:
15135129Smarx (void) beeper_off();
15140Sstevel@tonic-gate break;
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate
15190Sstevel@tonic-gate /*
15200Sstevel@tonic-gate * This is a pass-thru routine to get a character at poll time.
15210Sstevel@tonic-gate */
15220Sstevel@tonic-gate static int
15231762Slt200341 kb8042_polled_getchar(cons_polledio_arg_t arg)
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate struct kb8042 *kb8042;
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate kb8042 = (struct kb8042 *)arg;
15280Sstevel@tonic-gate
15290Sstevel@tonic-gate return (kbtrans_getchar(kb8042->hw_kbtrans));
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate
15320Sstevel@tonic-gate /*
15330Sstevel@tonic-gate * This is a pass-thru routine to get a character at poll time.
15340Sstevel@tonic-gate */
15350Sstevel@tonic-gate static int
15361762Slt200341 kb8042_polled_ischar(cons_polledio_arg_t arg)
15370Sstevel@tonic-gate {
15380Sstevel@tonic-gate struct kb8042 *kb8042;
15390Sstevel@tonic-gate
15400Sstevel@tonic-gate kb8042 = (struct kb8042 *)arg;
15410Sstevel@tonic-gate
15420Sstevel@tonic-gate return (kbtrans_ischar(kb8042->hw_kbtrans));
15430Sstevel@tonic-gate }
1544