17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
599d47a04Slq  * Common Development and Distribution License (the "License").
699d47a04Slq  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2199d47a04Slq 
227c478bd9Sstevel@tonic-gate /*
2348633f18SJan Setje-Eilers  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7d724debSJoshua M. Clulow  * Copyright 2019 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
29*7d724debSJoshua M. Clulow  * Generic keyboard support: STREAMS and administration.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #define	KEYMAP_SIZE_VARIABLE
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/stream.h>
377c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
387c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/vuid_event.h>
417c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/kbd.h>
467c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
477c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
487c478bd9Sstevel@tonic-gate #include <sys/kbtrans.h>
497c478bd9Sstevel@tonic-gate #include <sys/policy.h>
5048633f18SJan Setje-Eilers #include <sys/sunldi.h>
513bce8c8cSJan Setje-Eilers #include <sys/class.h>
523bce8c8cSJan Setje-Eilers #include <sys/spl.h>
537c478bd9Sstevel@tonic-gate #include "kbtrans_lower.h"
547c478bd9Sstevel@tonic-gate #include "kbtrans_streams.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #ifdef DEBUG
577c478bd9Sstevel@tonic-gate int	kbtrans_errmask;
587c478bd9Sstevel@tonic-gate int	kbtrans_errlevel;
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 
61aecfc01dSrui zang - Sun Microsystems - Beijing China #define	KB_NR_FUNCKEYS		12
62aecfc01dSrui zang - Sun Microsystems - Beijing China 
637c478bd9Sstevel@tonic-gate /*
64*7d724debSJoshua M. Clulow  * Overrides for tuning keyboard autorepeat behaviour:
657c478bd9Sstevel@tonic-gate  */
66*7d724debSJoshua M. Clulow int kbtrans_repeat_count = -1;
67*7d724debSJoshua M. Clulow int kbtrans_repeat_rate;
68*7d724debSJoshua M. Clulow int kbtrans_repeat_delay;
697c478bd9Sstevel@tonic-gate 
70*7d724debSJoshua M. Clulow /*
71*7d724debSJoshua M. Clulow  * Override whether to print a message when the keyboard event queue overflows:
72*7d724debSJoshua M. Clulow  */
73*7d724debSJoshua M. Clulow int kbtrans_overflow_msg = 1;
74*7d724debSJoshua M. Clulow 
75*7d724debSJoshua M. Clulow /*
76*7d724debSJoshua M. Clulow  * Override whether to drop "Scroll Lock" key presses in TR_ASCII mode:
77*7d724debSJoshua M. Clulow  */
78*7d724debSJoshua M. Clulow int kbtrans_ignore_scroll_lock = 1;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
81*7d724debSJoshua M. Clulow  * Override for the number of key stations we will track in the down state at
82*7d724debSJoshua M. Clulow  * one time.  By default we assume most keyboards will be operated by people
83*7d724debSJoshua M. Clulow  * with up to ten fingers, plus some safety margin.  This size is only read
84*7d724debSJoshua M. Clulow  * once at attach time.
857c478bd9Sstevel@tonic-gate  */
86*7d724debSJoshua M. Clulow int kbtrans_downs_size = 15;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * modload support
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc	= {
947c478bd9Sstevel@tonic-gate 	&mod_miscops,	/* Type	of module */
95aecfc01dSrui zang - Sun Microsystems - Beijing China 	"kbtrans (key translation)"
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
997c478bd9Sstevel@tonic-gate 	MODREV_1, (void	*)&modlmisc, NULL
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate int
_init(void)1037c478bd9Sstevel@tonic-gate _init(void)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate int
_fini(void)1097c478bd9Sstevel@tonic-gate _fini(void)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1157c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * Internal Function Prototypes
1227c478bd9Sstevel@tonic-gate  */
1235d420dfbSToomas Soome static char *kbtrans_strsetwithdecimal(char *, uint_t, uint_t);
1245d420dfbSToomas Soome static void kbtrans_set_translation_callback(struct kbtrans *);
1255d420dfbSToomas Soome static void kbtrans_reioctl(void *);
1265d420dfbSToomas Soome static void kbtrans_send_esc_event(char, struct kbtrans *);
1275d420dfbSToomas Soome static void kbtrans_keypressed(struct kbtrans *, uchar_t, Firm_event *,
1285d420dfbSToomas Soome     ushort_t);
1295d420dfbSToomas Soome static void kbtrans_putbuf(char *, queue_t *);
1305d420dfbSToomas Soome static void kbtrans_cancelrpt(struct kbtrans *);
1315d420dfbSToomas Soome static void kbtrans_queuepress(struct kbtrans *, uchar_t, Firm_event *);
1325d420dfbSToomas Soome static void kbtrans_putcode(register struct kbtrans *, uint_t);
1335d420dfbSToomas Soome static void kbtrans_keyreleased(struct kbtrans *, uchar_t);
1345d420dfbSToomas Soome static void kbtrans_queueevent(struct kbtrans *, Firm_event *);
1355d420dfbSToomas Soome static void kbtrans_untrans_keypressed_raw(struct kbtrans *, kbtrans_key_t);
1365d420dfbSToomas Soome static void kbtrans_untrans_keyreleased_raw(struct kbtrans *, kbtrans_key_t);
1375d420dfbSToomas Soome static void kbtrans_ascii_keypressed(struct kbtrans *, uint_t,
1385d420dfbSToomas Soome     kbtrans_key_t, uint_t);
1395d420dfbSToomas Soome static void kbtrans_ascii_keyreleased(struct kbtrans *, kbtrans_key_t);
1405d420dfbSToomas Soome static void kbtrans_ascii_setup_repeat(struct kbtrans *, uint_t, kbtrans_key_t);
1415d420dfbSToomas Soome static void kbtrans_trans_event_keypressed(struct kbtrans *, uint_t,
1425d420dfbSToomas Soome     kbtrans_key_t, uint_t);
1435d420dfbSToomas Soome static void kbtrans_trans_event_keyreleased(struct kbtrans *, kbtrans_key_t);
1445d420dfbSToomas Soome static void kbtrans_trans_event_setup_repeat(struct kbtrans *, uint_t,
1455d420dfbSToomas Soome     kbtrans_key_t);
1465d420dfbSToomas Soome static void kbtrans_rpt(void *);
1475d420dfbSToomas Soome static void kbtrans_setled(struct kbtrans *);
1485d420dfbSToomas Soome static void kbtrans_flush(struct kbtrans *);
1495d420dfbSToomas Soome static enum kbtrans_message_response kbtrans_ioctl(struct kbtrans *, mblk_t *);
1505d420dfbSToomas Soome static int kbtrans_setkey(struct kbtrans_lower *, struct kiockey *, cred_t *);
1515d420dfbSToomas Soome static int kbtrans_getkey(struct kbtrans_lower *, struct kiockey *);
1525d420dfbSToomas Soome static int kbtrans_skey(struct kbtrans_lower *, struct kiockeymap *, cred_t *);
1535d420dfbSToomas Soome static int kbtrans_gkey(struct kbtrans_lower *, struct kiockeymap *);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Keyboard Translation Mode (TR_NONE)
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * Functions to be called when keyboard translation is turned off
1597c478bd9Sstevel@tonic-gate  * and up/down key codes are reported.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate struct keyboard_callback	untrans_event_callback  = {
1627c478bd9Sstevel@tonic-gate 	kbtrans_untrans_keypressed_raw,
1637c478bd9Sstevel@tonic-gate 	kbtrans_untrans_keyreleased_raw,
1647c478bd9Sstevel@tonic-gate 	NULL,
1657c478bd9Sstevel@tonic-gate 	NULL,
1667c478bd9Sstevel@tonic-gate 	NULL,
1677c478bd9Sstevel@tonic-gate 	NULL,
1687c478bd9Sstevel@tonic-gate 	NULL,
1697c478bd9Sstevel@tonic-gate };
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Keyboard Translation Mode (TR_ASCII)
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * Functions to be called when ISO 8859/1 codes are reported
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate struct keyboard_callback	ascii_callback  = {
1777c478bd9Sstevel@tonic-gate 	NULL,
1787c478bd9Sstevel@tonic-gate 	NULL,
1797c478bd9Sstevel@tonic-gate 	kbtrans_ascii_keypressed,
1807c478bd9Sstevel@tonic-gate 	kbtrans_ascii_keyreleased,
1817c478bd9Sstevel@tonic-gate 	kbtrans_ascii_setup_repeat,
1827c478bd9Sstevel@tonic-gate 	kbtrans_cancelrpt,
1837c478bd9Sstevel@tonic-gate 	kbtrans_setled,
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Keyboard Translation Mode (TR_EVENT)
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  * Functions to be called when firm_events are reported.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate struct keyboard_callback	trans_event_callback  = {
1927c478bd9Sstevel@tonic-gate 	NULL,
1937c478bd9Sstevel@tonic-gate 	NULL,
1947c478bd9Sstevel@tonic-gate 	kbtrans_trans_event_keypressed,
1957c478bd9Sstevel@tonic-gate 	kbtrans_trans_event_keyreleased,
1967c478bd9Sstevel@tonic-gate 	kbtrans_trans_event_setup_repeat,
1977c478bd9Sstevel@tonic-gate 	kbtrans_cancelrpt,
1987c478bd9Sstevel@tonic-gate 	kbtrans_setled,
1997c478bd9Sstevel@tonic-gate };
2007c478bd9Sstevel@tonic-gate 
2013bce8c8cSJan Setje-Eilers static void
progressbar_key_abort_thread(struct kbtrans * upper)2023bce8c8cSJan Setje-Eilers progressbar_key_abort_thread(struct kbtrans *upper)
2033bce8c8cSJan Setje-Eilers {
2043bce8c8cSJan Setje-Eilers 	ldi_ident_t li;
2053bce8c8cSJan Setje-Eilers 	extern void progressbar_key_abort(ldi_ident_t);
2063bce8c8cSJan Setje-Eilers 
2073bce8c8cSJan Setje-Eilers 	if (ldi_ident_from_stream(upper->kbtrans_streams_readq, &li) != 0) {
2083bce8c8cSJan Setje-Eilers 		cmn_err(CE_NOTE, "!ldi_ident_from_stream failed");
2093bce8c8cSJan Setje-Eilers 	} else {
2103bce8c8cSJan Setje-Eilers 		mutex_enter(&upper->progressbar_key_abort_lock);
2113bce8c8cSJan Setje-Eilers 		while (upper->progressbar_key_abort_flag == 0)
2123bce8c8cSJan Setje-Eilers 			cv_wait(&upper->progressbar_key_abort_cv,
2133bce8c8cSJan Setje-Eilers 			    &upper->progressbar_key_abort_lock);
21463dd072cSJan Setje-Eilers 		if (upper->progressbar_key_abort_flag == 1) {
21563dd072cSJan Setje-Eilers 			mutex_exit(&upper->progressbar_key_abort_lock);
2163bce8c8cSJan Setje-Eilers 			progressbar_key_abort(li);
21763dd072cSJan Setje-Eilers 		} else {
21863dd072cSJan Setje-Eilers 			mutex_exit(&upper->progressbar_key_abort_lock);
21963dd072cSJan Setje-Eilers 		}
2203bce8c8cSJan Setje-Eilers 		ldi_ident_release(li);
2213bce8c8cSJan Setje-Eilers 	}
2223bce8c8cSJan Setje-Eilers 
2233bce8c8cSJan Setje-Eilers 	thread_exit();
2243bce8c8cSJan Setje-Eilers }
2253bce8c8cSJan Setje-Eilers 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * kbtrans_streams_init:
2287c478bd9Sstevel@tonic-gate  *	Initialize the stream, keytables, callbacks, etc.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate int
kbtrans_streams_init(queue_t * q,int sflag,struct kbtrans_hardware * hw,struct kbtrans_callbacks * hw_cb,struct kbtrans ** ret_kbd,int initial_leds,int initial_led_mask)2315d420dfbSToomas Soome kbtrans_streams_init(queue_t *q, int sflag, struct kbtrans_hardware *hw,
2325d420dfbSToomas Soome     struct kbtrans_callbacks *hw_cb, struct kbtrans **ret_kbd,
2335d420dfbSToomas Soome     int initial_leds, int initial_led_mask)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	struct kbtrans *upper;
2367c478bd9Sstevel@tonic-gate 	struct kbtrans_lower *lower;
2376087c6ddSJan Setje-Eilers 	kthread_t *tid;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Default to relatively generic tables.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	extern signed char			kb_compose_map[];
2437c478bd9Sstevel@tonic-gate 	extern struct compose_sequence_t	kb_compose_table[];
2447c478bd9Sstevel@tonic-gate 	extern struct fltaccent_sequence_t	kb_fltaccent_table[];
2457c478bd9Sstevel@tonic-gate 	extern char				keystringtab[][KTAB_STRLEN];
2467c478bd9Sstevel@tonic-gate 	extern unsigned char			kb_numlock_table[];
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* Set these up only once so that they could be changed from adb */
2497c478bd9Sstevel@tonic-gate 	if (!kbtrans_repeat_rate) {
2505d420dfbSToomas Soome 		kbtrans_repeat_rate = (hz + 29) / 30;
2515d420dfbSToomas Soome 		kbtrans_repeat_delay = hz / 2;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	switch (sflag) {
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	case MODOPEN:
2577c478bd9Sstevel@tonic-gate 		break;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	case CLONEOPEN:
2607c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L1, PRINT_MASK_OPEN, (NULL,
261aecfc01dSrui zang - Sun Microsystems - Beijing China 		    "kbtrans_streams_init: Clone open not supported"));
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		return (EINVAL);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/* allocate keyboard state structure */
2677c478bd9Sstevel@tonic-gate 	upper = kmem_zalloc(sizeof (struct kbtrans), KM_SLEEP);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	*ret_kbd = upper;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	upper->kbtrans_polled_buf[0] = '\0';
2727c478bd9Sstevel@tonic-gate 	upper->kbtrans_polled_pending_chars = upper->kbtrans_polled_buf;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_hw = hw;
2757c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_hw_callbacks = hw_cb;
2767c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_readq = q;
2777c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_iocpending = NULL;
2787c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_translatable = TR_CAN;
2797c478bd9Sstevel@tonic-gate 	upper->kbtrans_overflow_cnt = 0;
2807c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_translate_mode = TR_ASCII;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* Set the translation callback based on the translation type */
2837c478bd9Sstevel@tonic-gate 	kbtrans_set_translation_callback(upper);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	lower = &upper->kbtrans_lower;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Set defaults for relatively generic tables.
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	lower->kbtrans_compose_map = kb_compose_map;
2917c478bd9Sstevel@tonic-gate 	lower->kbtrans_compose_table = kb_compose_table;
2927c478bd9Sstevel@tonic-gate 	lower->kbtrans_fltaccent_table = kb_fltaccent_table;
2937c478bd9Sstevel@tonic-gate 	lower->kbtrans_numlock_table = kb_numlock_table;
2947c478bd9Sstevel@tonic-gate 	lower->kbtrans_keystringtab = keystringtab;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	lower->kbtrans_upper = upper;
2977c478bd9Sstevel@tonic-gate 	lower->kbtrans_compat = 1;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * We have a generic default for the LED state, and let the
3017c478bd9Sstevel@tonic-gate 	 * hardware-specific driver supply overrides.
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	lower->kbtrans_led_state = 0;
3047c478bd9Sstevel@tonic-gate 	lower->kbtrans_led_state &= ~initial_led_mask;
3057c478bd9Sstevel@tonic-gate 	lower->kbtrans_led_state |= initial_leds;
3067c478bd9Sstevel@tonic-gate 	lower->kbtrans_togglemask = 0;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (lower->kbtrans_led_state & LED_CAPS_LOCK)
3097c478bd9Sstevel@tonic-gate 		lower->kbtrans_togglemask |= CAPSMASK;
3107c478bd9Sstevel@tonic-gate 	if (lower->kbtrans_led_state & LED_NUM_LOCK)
3117c478bd9Sstevel@tonic-gate 		lower->kbtrans_togglemask |= NUMLOCKMASK;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate #if	defined(SCROLLMASK)
3147c478bd9Sstevel@tonic-gate 	if (lower->kbtrans_led_state & LED_SCROLL_LOCK)
3157c478bd9Sstevel@tonic-gate 		lower->kbtrans_togglemask |= SCROLLMASK;
3167c478bd9Sstevel@tonic-gate #endif
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	lower->kbtrans_shiftmask = lower->kbtrans_togglemask;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_vuid_addr.ascii = ASCII_FIRST;
3217c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_vuid_addr.top = TOP_FIRST;
3227c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_vuid_addr.vkey = VKEY_FIRST;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/* Allocate dynamic memory for downs table */
3257c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_num_downs_entries = kbtrans_downs_size;
3267c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_downs_bytes =
327aecfc01dSrui zang - Sun Microsystems - Beijing China 	    (uint32_t)(kbtrans_downs_size * sizeof (Key_event));
3287c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_downs =
329aecfc01dSrui zang - Sun Microsystems - Beijing China 	    kmem_zalloc(upper->kbtrans_streams_downs_bytes, KM_SLEEP);
3307c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_abortable = B_FALSE;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_flags = KBTRANS_STREAMS_OPEN;
3337c478bd9Sstevel@tonic-gate 
3343bce8c8cSJan Setje-Eilers 	upper->progressbar_key_abort_flag = 0;
3353bce8c8cSJan Setje-Eilers 	cv_init(&upper->progressbar_key_abort_cv, NULL, CV_DEFAULT, NULL);
3363bce8c8cSJan Setje-Eilers 	/* this counts on no keyboards being above ipl 12 */
3373bce8c8cSJan Setje-Eilers 	mutex_init(&upper->progressbar_key_abort_lock, NULL, MUTEX_SPIN,
3383bce8c8cSJan Setje-Eilers 	    (void *)ipltospl(12));
3396087c6ddSJan Setje-Eilers 	tid = thread_create(NULL, 0, progressbar_key_abort_thread, upper,
3406087c6ddSJan Setje-Eilers 	    0, &p0, TS_RUN, minclsyspri);
3416087c6ddSJan Setje-Eilers 	upper->progressbar_key_abort_t_did = tid->t_did;
3423bce8c8cSJan Setje-Eilers 
3437c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_OPEN, (upper, "kbtrans_streams_init "
344aecfc01dSrui zang - Sun Microsystems - Beijing China 	    "exiting"));
3457c478bd9Sstevel@tonic-gate 	return (0);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * kbtrans_streams_fini:
3517c478bd9Sstevel@tonic-gate  *	Free structures and uninitialize the stream
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate int
kbtrans_streams_fini(struct kbtrans * upper)3547c478bd9Sstevel@tonic-gate kbtrans_streams_fini(struct kbtrans *upper)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * Since we're about to destroy our private data, turn off
3587c478bd9Sstevel@tonic-gate 	 * our open flag first, so we don't accept any more input
3597c478bd9Sstevel@tonic-gate 	 * and try to use that data.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_flags = 0;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/* clear all timeouts */
3647c478bd9Sstevel@tonic-gate 	if (upper->kbtrans_streams_bufcallid) {
3657c478bd9Sstevel@tonic-gate 		qunbufcall(upper->kbtrans_streams_readq,
366aecfc01dSrui zang - Sun Microsystems - Beijing China 		    upper->kbtrans_streams_bufcallid);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	if (upper->kbtrans_streams_rptid) {
3697c478bd9Sstevel@tonic-gate 		(void) quntimeout(upper->kbtrans_streams_readq,
370aecfc01dSrui zang - Sun Microsystems - Beijing China 		    upper->kbtrans_streams_rptid);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	kmem_free(upper->kbtrans_streams_downs,
373aecfc01dSrui zang - Sun Microsystems - Beijing China 	    upper->kbtrans_streams_downs_bytes);
3743bce8c8cSJan Setje-Eilers 
3753bce8c8cSJan Setje-Eilers 	mutex_enter(&upper->progressbar_key_abort_lock);
3763bce8c8cSJan Setje-Eilers 	if (upper->progressbar_key_abort_flag == 0) {
3773bce8c8cSJan Setje-Eilers 		upper->progressbar_key_abort_flag = 2;
3783bce8c8cSJan Setje-Eilers 		cv_signal(&upper->progressbar_key_abort_cv);
3796087c6ddSJan Setje-Eilers 		mutex_exit(&upper->progressbar_key_abort_lock);
3806087c6ddSJan Setje-Eilers 		thread_join(upper->progressbar_key_abort_t_did);
3816087c6ddSJan Setje-Eilers 	} else {
3826087c6ddSJan Setje-Eilers 		mutex_exit(&upper->progressbar_key_abort_lock);
3833bce8c8cSJan Setje-Eilers 	}
3843bce8c8cSJan Setje-Eilers 	cv_destroy(&upper->progressbar_key_abort_cv);
3853bce8c8cSJan Setje-Eilers 	mutex_destroy(&upper->progressbar_key_abort_lock);
3863bce8c8cSJan Setje-Eilers 
3877c478bd9Sstevel@tonic-gate 	kmem_free(upper, sizeof (struct kbtrans));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_CLOSE, (upper, "kbtrans_streams_fini "
390aecfc01dSrui zang - Sun Microsystems - Beijing China 	    "exiting"));
3917c478bd9Sstevel@tonic-gate 	return (0);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate  * kbtrans_streams_releaseall :
3967c478bd9Sstevel@tonic-gate  *	This function releases all the held keys.
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate void
kbtrans_streams_releaseall(struct kbtrans * upper)3997c478bd9Sstevel@tonic-gate kbtrans_streams_releaseall(struct kbtrans *upper)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	register struct key_event *ke;
4027c478bd9Sstevel@tonic-gate 	register int i;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L0, PRINT_MASK_ALL, (NULL, "USBKBM RELEASE ALL\n"));
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* Scan table of down key stations */
4077c478bd9Sstevel@tonic-gate 	for (i = 0, ke = upper->kbtrans_streams_downs;
4087c478bd9Sstevel@tonic-gate 	    i < upper->kbtrans_streams_num_downs_entries; i++, ke++) {
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		/* Key station not zero */
4117c478bd9Sstevel@tonic-gate 		if (ke->key_station) {
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 			kbtrans_keyreleased(upper, ke->key_station);
4147c478bd9Sstevel@tonic-gate 			/* kbtrans_keyreleased resets downs entry */
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate  * kbtrans_streams_message:
4217c478bd9Sstevel@tonic-gate  *	keyboard module output queue put procedure: handles M_IOCTL
4227c478bd9Sstevel@tonic-gate  *	messages.
4237c478bd9Sstevel@tonic-gate  *
4245d420dfbSToomas Soome  *	Return KBTRANS_MESSAGE_HANDLED if the message was handled by
4257c478bd9Sstevel@tonic-gate  *	kbtrans and KBTRANS_MESSAGE_NOT_HANDLED otherwise. If
4267c478bd9Sstevel@tonic-gate  *	KBTRANS_MESSAGE_HANDLED is returned, no further action is required.
4277c478bd9Sstevel@tonic-gate  *	If KBTRANS_MESSAGE_NOT_HANDLED is returned, the hardware module
4287c478bd9Sstevel@tonic-gate  *	is responsible for any action.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate enum kbtrans_message_response
kbtrans_streams_message(struct kbtrans * upper,register mblk_t * mp)4317c478bd9Sstevel@tonic-gate kbtrans_streams_message(struct kbtrans *upper, register mblk_t *mp)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	queue_t *q = upper->kbtrans_streams_readq;
4347c478bd9Sstevel@tonic-gate 	enum kbtrans_message_response ret;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, (upper,
437aecfc01dSrui zang - Sun Microsystems - Beijing China 	    "kbtrans_streams_message entering"));
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * Process M_FLUSH, and some M_IOCTL, messages here; pass
4407c478bd9Sstevel@tonic-gate 	 * everything else down.
4417c478bd9Sstevel@tonic-gate 	 */
4427c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	case M_IOCTL:
4457c478bd9Sstevel@tonic-gate 		ret = kbtrans_ioctl(upper, mp);
4467c478bd9Sstevel@tonic-gate 		break;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	case M_FLUSH:
4497c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW)
4507c478bd9Sstevel@tonic-gate 			flushq(q, FLUSHDATA);
4517c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR)
4527c478bd9Sstevel@tonic-gate 			flushq(RD(q), FLUSHDATA);
4537c478bd9Sstevel@tonic-gate 		/*
4547c478bd9Sstevel@tonic-gate 		 * White lie:  we say we didn't handle the message,
4557c478bd9Sstevel@tonic-gate 		 * so that it gets handled by our client.
4567c478bd9Sstevel@tonic-gate 		 */
4577c478bd9Sstevel@tonic-gate 		ret = KBTRANS_MESSAGE_NOT_HANDLED;
4587c478bd9Sstevel@tonic-gate 		break;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	default:
4617c478bd9Sstevel@tonic-gate 		ret = KBTRANS_MESSAGE_NOT_HANDLED;
4627c478bd9Sstevel@tonic-gate 		break;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L1, PRINT_MASK_ALL, (upper,
466aecfc01dSrui zang - Sun Microsystems - Beijing China 	    "kbtrans_streams_message exiting\n"));
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	return (ret);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * kbtrans_streams_key:
4735d420dfbSToomas Soome  *	When a key is pressed or released, the hardware module should
4745d420dfbSToomas Soome  *	call kbtrans, passing the key number and its new
4755d420dfbSToomas Soome  *	state.  kbtrans is responsible for autorepeat handling;
4765d420dfbSToomas Soome  *	the hardware module should report only actual press/release
4775d420dfbSToomas Soome  *	events, suppressing any hardware-generated autorepeat.
4787c478bd9Sstevel@tonic-gate  */
4797c478bd9Sstevel@tonic-gate void
kbtrans_streams_key(struct kbtrans * upper,kbtrans_key_t key,enum keystate state)4805d420dfbSToomas Soome kbtrans_streams_key(struct kbtrans *upper, kbtrans_key_t key,
4817c478bd9Sstevel@tonic-gate     enum keystate state)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	struct kbtrans_lower *lower;
4847c478bd9Sstevel@tonic-gate 	struct keyboard *kp;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	lower = &upper->kbtrans_lower;
4877c478bd9Sstevel@tonic-gate 	kp = lower->kbtrans_keyboard;
4887c478bd9Sstevel@tonic-gate 
48948633f18SJan Setje-Eilers 	/* trigger switch back to text mode */
4903bce8c8cSJan Setje-Eilers 	mutex_enter(&upper->progressbar_key_abort_lock);
4913bce8c8cSJan Setje-Eilers 	if (upper->progressbar_key_abort_flag == 0) {
4923bce8c8cSJan Setje-Eilers 		upper->progressbar_key_abort_flag = 1;
4933bce8c8cSJan Setje-Eilers 		cv_signal(&upper->progressbar_key_abort_cv);
49448633f18SJan Setje-Eilers 	}
4953bce8c8cSJan Setje-Eilers 	mutex_exit(&upper->progressbar_key_abort_lock);
49648633f18SJan Setje-Eilers 
4977c478bd9Sstevel@tonic-gate 	if (upper->kbtrans_streams_abortable) {
4987c478bd9Sstevel@tonic-gate 		switch (upper->kbtrans_streams_abort_state) {
4997c478bd9Sstevel@tonic-gate 		case ABORT_NORMAL:
5007c478bd9Sstevel@tonic-gate 			if (state != KEY_PRESSED)
5017c478bd9Sstevel@tonic-gate 				break;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 			if (key == (kbtrans_key_t)kp->k_abort1 ||
5047c478bd9Sstevel@tonic-gate 			    key == (kbtrans_key_t)kp->k_abort1a) {
5057c478bd9Sstevel@tonic-gate 				upper->kbtrans_streams_abort_state =
506aecfc01dSrui zang - Sun Microsystems - Beijing China 				    ABORT_ABORT1_RECEIVED;
5077c478bd9Sstevel@tonic-gate 				upper->kbtrans_streams_abort1_key = key;
5087c478bd9Sstevel@tonic-gate 				return;
5097c478bd9Sstevel@tonic-gate 			}
510e57f7474Sqz 			/* Shift key needs to be sent to upper immediately */
511e4603304Sqz 			if (key == (kbtrans_key_t)kp->k_newabort1 ||
512e4603304Sqz 			    key == (kbtrans_key_t)kp->k_newabort1a) {
513e4603304Sqz 				upper->kbtrans_streams_abort_state =
514aecfc01dSrui zang - Sun Microsystems - Beijing China 				    NEW_ABORT_ABORT1_RECEIVED;
515e4603304Sqz 				upper->kbtrans_streams_new_abort1_key = key;
516e4603304Sqz 			}
5177c478bd9Sstevel@tonic-gate 			break;
5187c478bd9Sstevel@tonic-gate 		case ABORT_ABORT1_RECEIVED:
5197c478bd9Sstevel@tonic-gate 			upper->kbtrans_streams_abort_state = ABORT_NORMAL;
5207c478bd9Sstevel@tonic-gate 			if (state == KEY_PRESSED &&
5217c478bd9Sstevel@tonic-gate 			    key == (kbtrans_key_t)kp->k_abort2) {
5227c478bd9Sstevel@tonic-gate 				abort_sequence_enter((char *)NULL);
5237c478bd9Sstevel@tonic-gate 				return;
5247c478bd9Sstevel@tonic-gate 			} else {
5257c478bd9Sstevel@tonic-gate 				kbtrans_processkey(lower,
526aecfc01dSrui zang - Sun Microsystems - Beijing China 				    upper->kbtrans_streams_callback,
527aecfc01dSrui zang - Sun Microsystems - Beijing China 				    upper->kbtrans_streams_abort1_key,
528aecfc01dSrui zang - Sun Microsystems - Beijing China 				    KEY_PRESSED);
5297c478bd9Sstevel@tonic-gate 			}
530e4603304Sqz 			break;
531e4603304Sqz 		case NEW_ABORT_ABORT1_RECEIVED:
532e4603304Sqz 			upper->kbtrans_streams_abort_state = ABORT_NORMAL;
533e4603304Sqz 			if (state == KEY_PRESSED &&
534e4603304Sqz 			    key == (kbtrans_key_t)kp->k_newabort2) {
535e4603304Sqz 				abort_sequence_enter((char *)NULL);
536e4603304Sqz 				kbtrans_processkey(lower,
537aecfc01dSrui zang - Sun Microsystems - Beijing China 				    upper->kbtrans_streams_callback,
538aecfc01dSrui zang - Sun Microsystems - Beijing China 				    upper->kbtrans_streams_new_abort1_key,
539aecfc01dSrui zang - Sun Microsystems - Beijing China 				    KEY_RELEASED);
540e57f7474Sqz 				return;
541e4603304Sqz 			}
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	kbtrans_processkey(lower, upper->kbtrans_streams_callback, key, state);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * kbtrans_streams_set_keyboard:
5505d420dfbSToomas Soome  *	At any time after calling kbtrans_streams_init, the hardware
5515d420dfbSToomas Soome  *	module should make this call to report the id of the keyboard
5525d420dfbSToomas Soome  *	attached. id is the keyboard type, typically KB_SUN4,
5535d420dfbSToomas Soome  *	KB_PC, or KB_USB.
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate void
kbtrans_streams_set_keyboard(struct kbtrans * upper,int id,struct keyboard * k)5565d420dfbSToomas Soome kbtrans_streams_set_keyboard(struct kbtrans *upper, int id, struct keyboard *k)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	upper->kbtrans_lower.kbtrans_keyboard = k;
5597c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_id = id;
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  * kbtrans_streams_has_reset:
5645d420dfbSToomas Soome  *	At any time between kbtrans_streams_init and kbtrans_streams_fini,
5655d420dfbSToomas Soome  *	the hardware module can call this routine to report that the
5665d420dfbSToomas Soome  *	keyboard has been reset, e.g. by being unplugged and reattached.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5697c478bd9Sstevel@tonic-gate void
kbtrans_streams_has_reset(struct kbtrans * upper)5707c478bd9Sstevel@tonic-gate kbtrans_streams_has_reset(struct kbtrans *upper)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * If this routine is implemented it should probably (a)
5747c478bd9Sstevel@tonic-gate 	 * simulate releases of all pressed keys and (b) call
5757c478bd9Sstevel@tonic-gate 	 * the hardware module to set the LEDs.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * kbtrans_streams_enable:
5817c478bd9Sstevel@tonic-gate  *	This is the routine that is called back when the the stream is ready
5827c478bd9Sstevel@tonic-gate  *	to take messages.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate void
kbtrans_streams_enable(struct kbtrans * upper)5857c478bd9Sstevel@tonic-gate kbtrans_streams_enable(struct kbtrans *upper)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	/* Set the LED's */
5887c478bd9Sstevel@tonic-gate 	kbtrans_setled(upper);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
59199d47a04Slq /*
59299d47a04Slq  * kbtrans_streams_setled():
59399d47a04Slq  *	This is the routine that is called to only update the led state
59499d47a04Slq  *	in kbtrans.
59599d47a04Slq  */
59699d47a04Slq void
kbtrans_streams_setled(struct kbtrans * upper,int led_state)59799d47a04Slq kbtrans_streams_setled(struct kbtrans *upper, int led_state)
59899d47a04Slq {
59999d47a04Slq 	struct kbtrans_lower *lower;
60099d47a04Slq 
60199d47a04Slq 	lower = &upper->kbtrans_lower;
60299d47a04Slq 	lower->kbtrans_led_state = (uchar_t)led_state;
60399d47a04Slq 
60499d47a04Slq 	if (lower->kbtrans_led_state & LED_CAPS_LOCK)
60599d47a04Slq 		lower->kbtrans_togglemask |= CAPSMASK;
60699d47a04Slq 	if (lower->kbtrans_led_state & LED_NUM_LOCK)
60799d47a04Slq 		lower->kbtrans_togglemask |= NUMLOCKMASK;
60899d47a04Slq 
60999d47a04Slq #if	defined(SCROLLMASK)
61099d47a04Slq 	if (lower->kbtrans_led_state & LED_SCROLL_LOCK)
61199d47a04Slq 		lower->kbtrans_togglemask |= SCROLLMASK;
61299d47a04Slq #endif
61399d47a04Slq 
61499d47a04Slq 	lower->kbtrans_shiftmask = lower->kbtrans_togglemask;
61599d47a04Slq 
61699d47a04Slq }
61799d47a04Slq 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * kbtrans_streams_set_queue:
6207c478bd9Sstevel@tonic-gate  *      Set the overlying queue, to support multiplexors.
6217c478bd9Sstevel@tonic-gate  */
6227c478bd9Sstevel@tonic-gate void
kbtrans_streams_set_queue(struct kbtrans * upper,queue_t * q)6237c478bd9Sstevel@tonic-gate kbtrans_streams_set_queue(struct kbtrans *upper, queue_t *q)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_readq = q;
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate /*
6307c478bd9Sstevel@tonic-gate  * kbtrans_streams_get_queue:
6317c478bd9Sstevel@tonic-gate  *      Return the overlying queue.
6327c478bd9Sstevel@tonic-gate  */
6337c478bd9Sstevel@tonic-gate queue_t *
kbtrans_streams_get_queue(struct kbtrans * upper)6347c478bd9Sstevel@tonic-gate kbtrans_streams_get_queue(struct kbtrans *upper)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	return (upper->kbtrans_streams_readq);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * kbtrans_streams_untimeout
6417c478bd9Sstevel@tonic-gate  *      Cancell all timeout
6427c478bd9Sstevel@tonic-gate  */
6437c478bd9Sstevel@tonic-gate void
kbtrans_streams_untimeout(struct kbtrans * upper)6447c478bd9Sstevel@tonic-gate kbtrans_streams_untimeout(struct kbtrans *upper)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	/* clear all timeouts */
6477c478bd9Sstevel@tonic-gate 	if (upper->kbtrans_streams_bufcallid) {
6487c478bd9Sstevel@tonic-gate 		qunbufcall(upper->kbtrans_streams_readq,
649aecfc01dSrui zang - Sun Microsystems - Beijing China 		    upper->kbtrans_streams_bufcallid);
6507c478bd9Sstevel@tonic-gate 		upper->kbtrans_streams_bufcallid = 0;
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 	if (upper->kbtrans_streams_rptid) {
6537c478bd9Sstevel@tonic-gate 		(void) quntimeout(upper->kbtrans_streams_readq,
654aecfc01dSrui zang - Sun Microsystems - Beijing China 		    upper->kbtrans_streams_rptid);
6557c478bd9Sstevel@tonic-gate 		upper->kbtrans_streams_rptid = 0;
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * kbtrans_reioctl:
6615d420dfbSToomas Soome  *	This function is set up as call-back function should an ioctl fail
6625d420dfbSToomas Soome  *	to allocate required resources.
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate static void
kbtrans_reioctl(void * arg)6657c478bd9Sstevel@tonic-gate kbtrans_reioctl(void	*arg)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	struct kbtrans *upper = (struct kbtrans *)arg;
6687c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	upper->kbtrans_streams_bufcallid = 0;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if ((mp = upper->kbtrans_streams_iocpending) != NULL) {
6737c478bd9Sstevel@tonic-gate 		/* not pending any more */
6747c478bd9Sstevel@tonic-gate 		upper->kbtrans_streams_iocpending = NULL;
6757c478bd9Sstevel@tonic-gate 		(void) kbtrans_ioctl(upper, mp);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * kbtrans_ioctl:
6815d420dfbSToomas Soome  *	process ioctls we recognize and own.  Otherwise, pass it down.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate static enum kbtrans_message_response
kbtrans_ioctl(struct kbtrans * upper,register mblk_t * mp)6847c478bd9Sstevel@tonic-gate kbtrans_ioctl(struct kbtrans *upper, register mblk_t *mp)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	register struct iocblk *iocp;
6877c478bd9Sstevel@tonic-gate 	register short	new_translate;
6887c478bd9Sstevel@tonic-gate 	register Vuid_addr_probe *addr_probe;
6897c478bd9Sstevel@tonic-gate 	register short	*addr_ptr;
6907c478bd9Sstevel@tonic-gate 	size_t	ioctlrespsize;
6917c478bd9Sstevel@tonic-gate 	int	err = 0;
6927c478bd9Sstevel@tonic-gate 	struct kbtrans_lower *lower;
6937c478bd9Sstevel@tonic-gate 	mblk_t *datap;
6947c478bd9Sstevel@tonic-gate 	int	translate;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	static int kiocgetkey, kiocsetkey;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	lower = &upper->kbtrans_lower;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper,
703aecfc01dSrui zang - Sun Microsystems - Beijing China 	    "kbtrans_ioctl: ioc_cmd 0x%x - ", iocp->ioc_cmd));
7047c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	case VUIDSFORMAT:
7077c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "VUIDSFORMAT\n"));
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (int));
7107c478bd9Sstevel@tonic-gate 		if (err != 0)
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 		new_translate = (*(int *)mp->b_cont->b_rptr == VUID_NATIVE) ?
7137c478bd9Sstevel@tonic-gate 		    TR_ASCII : TR_EVENT;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		if (new_translate == upper->kbtrans_streams_translate_mode)
7167c478bd9Sstevel@tonic-gate 			break;
7177c478bd9Sstevel@tonic-gate 		upper->kbtrans_streams_translate_mode = new_translate;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		kbtrans_set_translation_callback(upper);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		kbtrans_flush(upper);
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	case KIOCTRANS:
7257c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCTRANS\n"));
7267c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (int));
7277c478bd9Sstevel@tonic-gate 		if (err != 0)
7287c478bd9Sstevel@tonic-gate 			break;
7297c478bd9Sstevel@tonic-gate 		new_translate = *(int *)mp->b_cont->b_rptr;
7307c478bd9Sstevel@tonic-gate 		if (new_translate == upper->kbtrans_streams_translate_mode)
7317c478bd9Sstevel@tonic-gate 			break;
7327c478bd9Sstevel@tonic-gate 		upper->kbtrans_streams_translate_mode = new_translate;
7337c478bd9Sstevel@tonic-gate 		kbtrans_set_translation_callback(upper);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		kbtrans_flush(upper);
7367c478bd9Sstevel@tonic-gate 		break;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	case KIOCSLED:
7397c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCSLED\n"));
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (uchar_t));
7427c478bd9Sstevel@tonic-gate 		if (err != 0)
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 		lower->kbtrans_led_state = *(uchar_t *)mp->b_cont->b_rptr;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		kbtrans_setled(upper);
7477c478bd9Sstevel@tonic-gate 		break;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	case KIOCGLED:
7507c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGLED\n"));
7517c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (uchar_t), BPRI_HI)) == NULL) {
7527c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
7537c478bd9Sstevel@tonic-gate 			goto allocfailure;
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		*(uchar_t *)datap->b_wptr = lower->kbtrans_led_state;
7577c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (uchar_t);
7587c478bd9Sstevel@tonic-gate 		if (mp->b_cont)
7597c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
7607c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
7617c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (uchar_t);
7627c478bd9Sstevel@tonic-gate 		break;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	case VUIDGFORMAT:
7657c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "VUIDGFORMAT\n"));
7667c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
7677c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
7687c478bd9Sstevel@tonic-gate 			goto allocfailure;
7697c478bd9Sstevel@tonic-gate 		}
7707c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr =
7717c478bd9Sstevel@tonic-gate 		    (upper->kbtrans_streams_translate_mode == TR_EVENT ||
7727c478bd9Sstevel@tonic-gate 		    upper->kbtrans_streams_translate_mode == TR_UNTRANS_EVENT) ?
773aecfc01dSrui zang - Sun Microsystems - Beijing China 		    VUID_FIRM_EVENT: VUID_NATIVE;
7747c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
7757c478bd9Sstevel@tonic-gate 		if (mp->b_cont)  /* free msg to prevent memory leak */
7767c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
7777c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
7787c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
7797c478bd9Sstevel@tonic-gate 		break;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	case KIOCGTRANS:
7827c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGTRANS\n"));
7837c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
7847c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
7857c478bd9Sstevel@tonic-gate 			goto allocfailure;
7867c478bd9Sstevel@tonic-gate 		}
7877c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = upper->kbtrans_streams_translate_mode;
7887c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
7897c478bd9Sstevel@tonic-gate 		if (mp->b_cont)  /* free msg to prevent memory leak */
7907c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
7917c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
7927c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
7937c478bd9Sstevel@tonic-gate 		break;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	case VUIDSADDR:
7967c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "VUIDSADDR\n"));
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (Vuid_addr_probe));
7997c478bd9Sstevel@tonic-gate 		if (err != 0)
8007c478bd9Sstevel@tonic-gate 			break;
8017c478bd9Sstevel@tonic-gate 		addr_probe = (Vuid_addr_probe *)mp->b_cont->b_rptr;
8027c478bd9Sstevel@tonic-gate 		switch (addr_probe->base) {
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		case ASCII_FIRST:
8057c478bd9Sstevel@tonic-gate 			addr_ptr = &upper->kbtrans_streams_vuid_addr.ascii;
8067c478bd9Sstevel@tonic-gate 			break;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		case TOP_FIRST:
8097c478bd9Sstevel@tonic-gate 			addr_ptr = &upper->kbtrans_streams_vuid_addr.top;
8107c478bd9Sstevel@tonic-gate 			break;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 		case VKEY_FIRST:
8137c478bd9Sstevel@tonic-gate 			addr_ptr = &upper->kbtrans_streams_vuid_addr.vkey;
8147c478bd9Sstevel@tonic-gate 			break;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 		default:
8177c478bd9Sstevel@tonic-gate 			err = ENODEV;
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		if ((err == 0) && (*addr_ptr != addr_probe->data.next)) {
8217c478bd9Sstevel@tonic-gate 			*addr_ptr = addr_probe->data.next;
8227c478bd9Sstevel@tonic-gate 			kbtrans_flush(upper);
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 		break;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	case VUIDGADDR:
8277c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "VUIDGADDR\n"));
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (Vuid_addr_probe));
8307c478bd9Sstevel@tonic-gate 		if (err != 0)
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		addr_probe = (Vuid_addr_probe *)mp->b_cont->b_rptr;
8337c478bd9Sstevel@tonic-gate 		switch (addr_probe->base) {
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		case ASCII_FIRST:
8367c478bd9Sstevel@tonic-gate 			addr_probe->data.current =
837aecfc01dSrui zang - Sun Microsystems - Beijing China 			    upper->kbtrans_streams_vuid_addr.ascii;
8387c478bd9Sstevel@tonic-gate 			break;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		case TOP_FIRST:
8417c478bd9Sstevel@tonic-gate 			addr_probe->data.current =
842aecfc01dSrui zang - Sun Microsystems - Beijing China 			    upper->kbtrans_streams_vuid_addr.top;
8437c478bd9Sstevel@tonic-gate 			break;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		case VKEY_FIRST:
8467c478bd9Sstevel@tonic-gate 			addr_probe->data.current =
847aecfc01dSrui zang - Sun Microsystems - Beijing China 			    upper->kbtrans_streams_vuid_addr.vkey;
8487c478bd9Sstevel@tonic-gate 			break;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		default:
8517c478bd9Sstevel@tonic-gate 			err = ENODEV;
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 		break;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	case KIOCTRANSABLE:
8567c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCTRANSABLE\n"));
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (int));
8597c478bd9Sstevel@tonic-gate 		if (err != 0)
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 		/*
8627c478bd9Sstevel@tonic-gate 		 * called during console setup in kbconfig()
8637c478bd9Sstevel@tonic-gate 		 * If set to false, means we are a serial keyboard,
8647c478bd9Sstevel@tonic-gate 		 * and we should pass all data up without modification.
8657c478bd9Sstevel@tonic-gate 		 */
8667c478bd9Sstevel@tonic-gate 		translate = *(int *)mp->b_cont->b_rptr;
8677c478bd9Sstevel@tonic-gate 		if (upper->kbtrans_streams_translatable != translate)
8687c478bd9Sstevel@tonic-gate 			upper->kbtrans_streams_translatable = translate;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		if (translate != TR_CAN)
8717c478bd9Sstevel@tonic-gate 			DPRINTF(PRINT_L4, PRINT_MASK_ALL, (upper,
8727c478bd9Sstevel@tonic-gate 			    "Cannot translate keyboard using tables.\n"));
8737c478bd9Sstevel@tonic-gate 		break;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	case KIOCGTRANSABLE:
8767c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGTRANSABLE\n"));
8777c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
8787c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
8797c478bd9Sstevel@tonic-gate 			goto allocfailure;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = upper->kbtrans_streams_translatable;
8827c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
8837c478bd9Sstevel@tonic-gate 		if (mp->b_cont)  /* free msg to prevent memory leak */
8847c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
8857c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
8867c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
8877c478bd9Sstevel@tonic-gate 		break;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	case KIOCSCOMPAT:
8907c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCSCOMPAT\n"));
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (int));
8937c478bd9Sstevel@tonic-gate 		if (err != 0)
8947c478bd9Sstevel@tonic-gate 			break;
8957c478bd9Sstevel@tonic-gate 		lower->kbtrans_compat = *(int *)mp->b_cont->b_rptr;
8967c478bd9Sstevel@tonic-gate 		break;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	case KIOCGCOMPAT:
8997c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGCOMPAT\n"));
9007c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9017c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
9027c478bd9Sstevel@tonic-gate 			goto allocfailure;
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = lower->kbtrans_compat;
9057c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9067c478bd9Sstevel@tonic-gate 		if (mp->b_cont)  /* free msg to prevent memory leak */
9077c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9087c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
9097c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
9107c478bd9Sstevel@tonic-gate 		break;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	case KIOCSETKEY:
9137c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCSETKEY %d\n",
914aecfc01dSrui zang - Sun Microsystems - Beijing China 		    kiocsetkey++));
9157c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (struct kiockey));
9167c478bd9Sstevel@tonic-gate 		if (err != 0)
9177c478bd9Sstevel@tonic-gate 			break;
9187c478bd9Sstevel@tonic-gate 		err = kbtrans_setkey(&upper->kbtrans_lower,
9197c478bd9Sstevel@tonic-gate 		    (struct kiockey *)mp->b_cont->b_rptr, iocp->ioc_cr);
9207c478bd9Sstevel@tonic-gate 		/*
9217c478bd9Sstevel@tonic-gate 		 * Since this only affects any subsequent key presses,
9227c478bd9Sstevel@tonic-gate 		 * don't flush soft state.  One might want to
9237c478bd9Sstevel@tonic-gate 		 * toggle the keytable entries dynamically.
9247c478bd9Sstevel@tonic-gate 		 */
9257c478bd9Sstevel@tonic-gate 		break;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	case KIOCGETKEY:
9287c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGETKEY %d\n",
929aecfc01dSrui zang - Sun Microsystems - Beijing China 		    kiocgetkey++));
9307c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (struct kiockey));
9317c478bd9Sstevel@tonic-gate 		if (err != 0)
9327c478bd9Sstevel@tonic-gate 			break;
9337c478bd9Sstevel@tonic-gate 		err = kbtrans_getkey(&upper->kbtrans_lower,
9347c478bd9Sstevel@tonic-gate 		    (struct kiockey *)mp->b_cont->b_rptr);
9357c478bd9Sstevel@tonic-gate 		break;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	case KIOCSKEY:
9387c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (struct kiockeymap));
9397c478bd9Sstevel@tonic-gate 		if (err != 0)
9407c478bd9Sstevel@tonic-gate 			break;
9417c478bd9Sstevel@tonic-gate 		err = kbtrans_skey(&upper->kbtrans_lower,
9427c478bd9Sstevel@tonic-gate 		    (struct kiockeymap *)mp->b_cont->b_rptr, iocp->ioc_cr);
9437c478bd9Sstevel@tonic-gate 		/*
9447c478bd9Sstevel@tonic-gate 		 * Since this only affects any subsequent key presses,
9457c478bd9Sstevel@tonic-gate 		 * don't flush soft state.  One might want to
9467c478bd9Sstevel@tonic-gate 		 * toggle the keytable entries dynamically.
9477c478bd9Sstevel@tonic-gate 		 */
9487c478bd9Sstevel@tonic-gate 		break;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	case KIOCGKEY:
9517c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (struct kiockeymap));
9527c478bd9Sstevel@tonic-gate 		if (err != 0)
9537c478bd9Sstevel@tonic-gate 			break;
9547c478bd9Sstevel@tonic-gate 		err = kbtrans_gkey(&upper->kbtrans_lower,
9557c478bd9Sstevel@tonic-gate 		    (struct kiockeymap *)mp->b_cont->b_rptr);
9567c478bd9Sstevel@tonic-gate 		break;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	case KIOCSDIRECT:
9597c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCSDIRECT\n"));
9607c478bd9Sstevel@tonic-gate 		kbtrans_flush(upper);
9617c478bd9Sstevel@tonic-gate 		break;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	case KIOCGDIRECT:
9647c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCSGDIRECT\n"));
9657c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9667c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
9677c478bd9Sstevel@tonic-gate 			goto allocfailure;
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = 1;	/* always direct */
9707c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9717c478bd9Sstevel@tonic-gate 		if (mp->b_cont) /* free msg to prevent memory leak */
9727c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9737c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
9747c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
9757c478bd9Sstevel@tonic-gate 		break;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	case KIOCTYPE:
9787c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCTYPE\n"));
9797c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
9807c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
9817c478bd9Sstevel@tonic-gate 			goto allocfailure;
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = upper->kbtrans_streams_id;
9847c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
9857c478bd9Sstevel@tonic-gate 		if (mp->b_cont) /* free msg to prevent memory leak */
9867c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
9877c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
9887c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
9897c478bd9Sstevel@tonic-gate 		break;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	case CONSSETABORTENABLE:
9927c478bd9Sstevel@tonic-gate 		/*
9937c478bd9Sstevel@tonic-gate 		 * Peek as it goes by; must be a TRANSPARENT ioctl.
9947c478bd9Sstevel@tonic-gate 		 */
9957c478bd9Sstevel@tonic-gate 		if (iocp->ioc_count != TRANSPARENT) {
9967c478bd9Sstevel@tonic-gate 			err = EINVAL;
9977c478bd9Sstevel@tonic-gate 			break;
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 		upper->kbtrans_streams_abortable =
10017c478bd9Sstevel@tonic-gate 		    (boolean_t)*(intptr_t *)mp->b_cont->b_rptr;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		/*
10047c478bd9Sstevel@tonic-gate 		 * Let the hardware module see it too.
10057c478bd9Sstevel@tonic-gate 		 */
10067c478bd9Sstevel@tonic-gate 		return (KBTRANS_MESSAGE_NOT_HANDLED);
10077c478bd9Sstevel@tonic-gate 
1008c0423dd8SToomas Soome 	case KIOCGRPTCOUNT:
1009c0423dd8SToomas Soome 		/*
1010c0423dd8SToomas Soome 		 * Report the autorepeat count
1011c0423dd8SToomas Soome 		 */
1012c0423dd8SToomas Soome 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGRPTCOUNT\n"));
1013c0423dd8SToomas Soome 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
1014c0423dd8SToomas Soome 			ioctlrespsize = sizeof (int);
1015c0423dd8SToomas Soome 			goto allocfailure;
1016c0423dd8SToomas Soome 		}
1017c0423dd8SToomas Soome 		*(int *)datap->b_wptr = kbtrans_repeat_count;
1018c0423dd8SToomas Soome 		datap->b_wptr += sizeof (int);
1019c0423dd8SToomas Soome 
1020c0423dd8SToomas Soome 		/* free msg to prevent memory leak */
1021c0423dd8SToomas Soome 		if (mp->b_cont != NULL)
1022c0423dd8SToomas Soome 			freemsg(mp->b_cont);
1023c0423dd8SToomas Soome 		mp->b_cont = datap;
1024c0423dd8SToomas Soome 		iocp->ioc_count = sizeof (int);
1025c0423dd8SToomas Soome 		break;
1026c0423dd8SToomas Soome 
1027c0423dd8SToomas Soome 	case KIOCSRPTCOUNT:
1028c0423dd8SToomas Soome 		/*
1029c0423dd8SToomas Soome 		 * Set the autorepeat count
1030c0423dd8SToomas Soome 		 */
1031c0423dd8SToomas Soome 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCSRPTCOUNT\n"));
1032c0423dd8SToomas Soome 		err = miocpullup(mp, sizeof (int));
1033c0423dd8SToomas Soome 
1034c0423dd8SToomas Soome 		if (err != 0)
1035c0423dd8SToomas Soome 			break;
1036c0423dd8SToomas Soome 
1037c0423dd8SToomas Soome 		/* validate the input */
1038c0423dd8SToomas Soome 		if (*(int *)mp->b_cont->b_rptr < -1) {
1039c0423dd8SToomas Soome 			err = EINVAL;
1040c0423dd8SToomas Soome 			break;
1041c0423dd8SToomas Soome 		}
1042c0423dd8SToomas Soome 		kbtrans_repeat_count = (*(int *)mp->b_cont->b_rptr);
1043c0423dd8SToomas Soome 		break;
1044c0423dd8SToomas Soome 
10457c478bd9Sstevel@tonic-gate 	case KIOCGRPTDELAY:
10467c478bd9Sstevel@tonic-gate 		/*
10477c478bd9Sstevel@tonic-gate 		 * Report the autorepeat delay, unit in millisecond
10487c478bd9Sstevel@tonic-gate 		 */
10497c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGRPTDELAY\n"));
10507c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
10517c478bd9Sstevel@tonic-gate 			ioctlrespsize = sizeof (int);
10527c478bd9Sstevel@tonic-gate 			goto allocfailure;
10537c478bd9Sstevel@tonic-gate 		}
10547c478bd9Sstevel@tonic-gate 		*(int *)datap->b_wptr = TICK_TO_MSEC(kbtrans_repeat_delay);
10557c478bd9Sstevel@tonic-gate 		datap->b_wptr += sizeof (int);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 		/* free msg to prevent memory leak */
10587c478bd9Sstevel@tonic-gate 		if (mp->b_cont != NULL)
10597c478bd9Sstevel@tonic-gate 			freemsg(mp->b_cont);
10607c478bd9Sstevel@tonic-gate 		mp->b_cont = datap;
10617c478bd9Sstevel@tonic-gate 		iocp->ioc_count = sizeof (int);
10627c478bd9Sstevel@tonic-gate 		break;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	case KIOCSRPTDELAY:
10657c478bd9Sstevel@tonic-gate 		/*
10667c478bd9Sstevel@tonic-gate 		 * Set the autorepeat delay
10677c478bd9Sstevel@tonic-gate 		 */
10687c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCSRPTDELAY\n"));
10697c478bd9Sstevel@tonic-gate 		err = miocpullup(mp, sizeof (int));
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 		if (err != 0)
10727c478bd9Sstevel@tonic-gate 			break;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 		/* validate the input */
10757c478bd9Sstevel@tonic-gate 		if (*(int *)mp->b_cont->b_rptr < KIOCRPTDELAY_MIN) {
10767c478bd9Sstevel@tonic-gate 			err = EINVAL;
10777c478bd9Sstevel@tonic-gate 			break;
10787c478bd9Sstevel@tonic-gate 		}
10797c478bd9Sstevel@tonic-gate 		kbtrans_repeat_delay = MSEC_TO_TICK(*(int *)mp->b_cont->b_rptr);
10807c478bd9Sstevel@tonic-gate 		if (kbtrans_repeat_delay <= 0)
10817c478bd9Sstevel@tonic-gate 			kbtrans_repeat_delay = 1;
10827c478bd9Sstevel@tonic-gate 		break;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	case KIOCGRPTRATE:
10857c478bd9Sstevel@tonic-gate 		/*
10867c478bd9Sstevel@tonic-gate 		 * Report the autorepeat rate
10877c478bd9Sstevel@tonic-gate 		 */
10887c478bd9Sstevel@tonic-gate 		DPRINTF(PRINT_L0, PRINT_MASK_ALL, (upper, "KIOCGRPTRATE\n"));
10897c478bd9Sstevel@tonic-gate 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
10907c478bd9Sstevel@tonic-gate