xref: /titanic_53/usr/src/uts/common/io/i8042.c (revision fbac63660cc7a6ccfe2042916ea594ac07d63fcb)
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
5a54f81fbSanish  * Common Development and Distribution License (the "License").
6a54f81fbSanish  * 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  */
217c478bd9Sstevel@tonic-gate /*
2219397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
27fd9cb95cSsethg #include <sys/ddi.h>
287c478bd9Sstevel@tonic-gate #include <sys/inline.h>
297c478bd9Sstevel@tonic-gate #include <sys/conf.h>
307c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
327c478bd9Sstevel@tonic-gate #include <sys/i8042.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/promif.h>	/* for prom_printf */
357c478bd9Sstevel@tonic-gate #include <sys/note.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
38fd9cb95cSsethg  * Note: For x86, this driver is used to create keyboard/mouse nodes when
397c478bd9Sstevel@tonic-gate  * booting with ACPI enumeration turned off (acpi-enum=off).
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Unfortunately, soft interrupts are implemented poorly.  Each additional
447c478bd9Sstevel@tonic-gate  * soft interrupt user impacts the performance of all existing soft interrupt
45fd9cb95cSsethg  * users.  This is not the case on SPARC, however.
467c478bd9Sstevel@tonic-gate  */
47fd9cb95cSsethg #ifdef __sparc
48fd9cb95cSsethg #define	USE_SOFT_INTRS
49fd9cb95cSsethg #else
507c478bd9Sstevel@tonic-gate #undef	USE_SOFT_INTRS
51fd9cb95cSsethg #endif
52fd9cb95cSsethg 
53fd9cb95cSsethg /*
54fd9cb95cSsethg  * The command bytes are different for x86 and for SPARC because on x86,
55fd9cb95cSsethg  * all modern 8042s can properly translate scan code set 2 codes to
56fd9cb95cSsethg  * scan code set 1.  On SPARC systems that have 8042s (e.g. Tadpole laptops),
57fd9cb95cSsethg  * setting the "translation" bit in the command byte has no effect.
58fd9cb95cSsethg  * This is potentially dangerous if, in the future, new SPARC systems uses 8042s
59fd9cb95cSsethg  * that implement the scan code translation when the translation bit is set.
60fd9cb95cSsethg  *
61fd9cb95cSsethg  * On SPARC, kb8042 will attempt to detect which scan code set the keyboard
62fd9cb95cSsethg  * is using.  In order for that code to work, the real scan code set must be the
63fd9cb95cSsethg  * set that is returned by the keyboard (and not a different set that is
64fd9cb95cSsethg  * translated by the 8042). (e.g. If the translation bit were enabled here,
65fd9cb95cSsethg  * and the keyboard returned scan code set 2 when kb8042 queried it, kb8042
66fd9cb95cSsethg  * would not be able to know with certainty that the scan codes it will receive
67fd9cb95cSsethg  * are set 2 scancodes, or set 1 translations made by the 8042).
68fd9cb95cSsethg  */
69fd9cb95cSsethg 
70fd9cb95cSsethg /*
71fd9cb95cSsethg  * 8042 Command Byte Layout:
72fd9cb95cSsethg  *
73fd9cb95cSsethg  * 0x80:  0   = Reserved, must be zero.
74fd9cb95cSsethg  * 0x40:  1   = Translate to XT codes. (0=No translation)
75fd9cb95cSsethg  * 0x20:  1   = Disable aux (mouse) port. (0=Enable port)
76fd9cb95cSsethg  * 0x10:  1   = Disable main (keyboard) port. (0=Enable port)
77fd9cb95cSsethg  * 0x08:  0   = Reserved, must be zero.
78fd9cb95cSsethg  * 0x04:  1   = System flag, 1 means passed self-test.
79fd9cb95cSsethg  *		Caution:  setting this bit to zero causes some
80fd9cb95cSsethg  *		systems (HP Kayak XA) to fail to reboot without
81fd9cb95cSsethg  *		a hard reset.
82fd9cb95cSsethg  * 0x02:  0   = Disable aux port interrupts. (1=Enable aux port interrupts)
83fd9cb95cSsethg  * 0x01:  0   = Disable main port interrupts. (1=Enable main port interrupts)
84fd9cb95cSsethg  *
85fd9cb95cSsethg  */
86fd9cb95cSsethg #if defined(__sparc)
87fd9cb95cSsethg #define	I8042_CMD_DISABLE_ALL	0x34
88fd9cb95cSsethg #define	I8042_CMD_ENABLE_ALL	0x07
89fd9cb95cSsethg #elif defined(__i386) || defined(__amd64)
90fd9cb95cSsethg #define	I8042_CMD_DISABLE_ALL	0x74
91fd9cb95cSsethg #define	I8042_CMD_ENABLE_ALL	0x47
92fd9cb95cSsethg #endif
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	BUFSIZ	64
957c478bd9Sstevel@tonic-gate 
96fd9cb95cSsethg /*
97fd9cb95cSsethg  * Child nodes, used to determine which to create at bus_config time
98fd9cb95cSsethg  */
99fd9cb95cSsethg #define	I8042_KEYBOARD 2
100fd9cb95cSsethg #define	I8042_MOUSE 1
101fd9cb95cSsethg 
1027c478bd9Sstevel@tonic-gate enum i8042_ports {
1037c478bd9Sstevel@tonic-gate 	MAIN_PORT = 0,
1047c478bd9Sstevel@tonic-gate 	AUX_PORT
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	NUM_PORTS	2
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
110fd9cb95cSsethg  * Only register at most MAX_INTERRUPTS interrupt handlers,
111fd9cb95cSsethg  * regardless of the number of interrupts in the prom node.
112fd9cb95cSsethg  * This is important, as registering for all interrupts on
113fd9cb95cSsethg  * some systems (e.g. Tadpole laptops) results in a flood
114fd9cb95cSsethg  * of spurious interrupts (for Tadpole, the first 2 interrupts
115fd9cb95cSsethg  * are for the keyboard and mouse, respectively, and the
116fd9cb95cSsethg  * third is for a proprietary device that is also accessed
117fd9cb95cSsethg  * via the same I/O addresses.)
118fd9cb95cSsethg  */
119fd9cb95cSsethg #define	MAX_INTERRUPTS	2
120fd9cb95cSsethg 
121fd9cb95cSsethg /*
1227c478bd9Sstevel@tonic-gate  * One of these for each port - main (keyboard) and aux (mouse).
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate struct i8042_port {
1257c478bd9Sstevel@tonic-gate 	boolean_t		initialized;
1267c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
1277c478bd9Sstevel@tonic-gate 	int			inumber;
1287c478bd9Sstevel@tonic-gate 	enum i8042_ports	which;		/* main or aux port */
1297c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
1307c478bd9Sstevel@tonic-gate 	ddi_softint_handle_t	soft_hdl;
1317c478bd9Sstevel@tonic-gate 	boolean_t		soft_intr_enabled;
1327c478bd9Sstevel@tonic-gate #else
133fd9cb95cSsethg 	kmutex_t		intr_mutex;
134fd9cb95cSsethg #endif
1357c478bd9Sstevel@tonic-gate 	uint_t			(*intr_func)(caddr_t arg1, caddr_t arg2);
1367c478bd9Sstevel@tonic-gate 	caddr_t			intr_arg1;
1377c478bd9Sstevel@tonic-gate 	caddr_t			intr_arg2;
1387c478bd9Sstevel@tonic-gate 	struct i8042		*i8042_global;
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * wptr is next byte to write
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	int			wptr;
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * rptr is next byte to read, == wptr means empty
1457c478bd9Sstevel@tonic-gate 	 * NB:  At full, one byte is unused.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	int			rptr;
1487c478bd9Sstevel@tonic-gate 	int			overruns;
1497c478bd9Sstevel@tonic-gate 	unsigned char		buf[BUFSIZ];
150bb2d7d5eSSeth Goldberg 
151bb2d7d5eSSeth Goldberg 	/*
152bb2d7d5eSSeth Goldberg 	 * Used during i8042_rep_put8 to intercept the 8042 response in
153bb2d7d5eSSeth Goldberg 	 * i8042_intr()
154bb2d7d5eSSeth Goldberg 	 */
155bb2d7d5eSSeth Goldberg 	boolean_t		intercept_complete;
156bb2d7d5eSSeth Goldberg 	boolean_t		intr_intercept_enabled;
157*fbac6366SSeth Goldberg 	uint8_t			intercept[2];
158*fbac6366SSeth Goldberg 	uint8_t			intercepted_byte;
159bb2d7d5eSSeth Goldberg 	kcondvar_t		intercept_cv;
160bb2d7d5eSSeth Goldberg 	kmutex_t		intercept_mutex;
1617c478bd9Sstevel@tonic-gate };
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * Describes entire 8042 device.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate struct i8042 {
167fd9cb95cSsethg 	dev_info_t		*dip;
1687c478bd9Sstevel@tonic-gate 	struct i8042_port	i8042_ports[NUM_PORTS];
1697c478bd9Sstevel@tonic-gate 	kmutex_t		i8042_mutex;
1707c478bd9Sstevel@tonic-gate 	kmutex_t		i8042_out_mutex;
1717c478bd9Sstevel@tonic-gate 	boolean_t		initialized;
1727c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t	io_handle;
1737c478bd9Sstevel@tonic-gate 	uint8_t			*io_addr;
174fd9cb95cSsethg 	int			nintrs;
175fd9cb95cSsethg 	ddi_iblock_cookie_t	*iblock_cookies;
176fd9cb95cSsethg 	uint_t			init_state;
177fd9cb95cSsethg /* Initialization states: */
178fd9cb95cSsethg #define	I8042_INIT_BASIC		0x00000001
179fd9cb95cSsethg #define	I8042_INIT_REGS_MAPPED		0x00000002
180fd9cb95cSsethg #define	I8042_INIT_MUTEXES		0x00000004
181fd9cb95cSsethg #define	I8042_INIT_INTRS_ENABLED	0x00000010
182fd9cb95cSsethg 	uint_t			intrs_added;
183fd9cb95cSsethg #ifdef __sparc
184fd9cb95cSsethg 	timeout_id_t		timeout_id;
185fd9cb95cSsethg #endif
186bb2d7d5eSSeth Goldberg #ifdef DEBUG
187bb2d7d5eSSeth Goldberg 	/*
188bb2d7d5eSSeth Goldberg 	 * intr_thread is set to curthread in i8042_intr and is
189bb2d7d5eSSeth Goldberg 	 * tested against curthread in i8402_rep_put8().
190bb2d7d5eSSeth Goldberg 	 */
191bb2d7d5eSSeth Goldberg 	kthread_t		*intr_thread;
192bb2d7d5eSSeth Goldberg #endif
193bb2d7d5eSSeth Goldberg 	ddi_softint_handle_t	intercept_sih;
1947c478bd9Sstevel@tonic-gate };
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * i8042 hardware register definitions
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * These are I/O registers, relative to the device's base (normally 0x60).
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate #define	I8042_DATA	0x00	/* read/write data here */
2047c478bd9Sstevel@tonic-gate #define	I8042_STAT	0x04	/* read status here */
2057c478bd9Sstevel@tonic-gate #define	I8042_CMD	0x04	/* write commands here */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * These are bits in I8042_STAT.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate #define	I8042_STAT_OUTBF	0x01	/* Output (to host) buffer full */
2117c478bd9Sstevel@tonic-gate #define	I8042_STAT_INBF		0x02	/* Input (from host) buffer full */
2127c478bd9Sstevel@tonic-gate #define	I8042_STAT_AUXBF	0x20	/* Output buffer data is from aux */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * These are commands to the i8042 itself (as distinct from the devices
2167c478bd9Sstevel@tonic-gate  * attached to it).
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate #define	I8042_CMD_RCB		0x20	/* Read command byte (we don't use) */
2197c478bd9Sstevel@tonic-gate #define	I8042_CMD_WCB		0x60	/* Write command byte */
2207c478bd9Sstevel@tonic-gate #define	I8042_CMD_WRITE_AUX	0xD4	/* Send next data byte to aux port */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
223fd9cb95cSsethg  * Maximum number of times to loop while clearing pending data from the
224fd9cb95cSsethg  * keyboard controller.
225fd9cb95cSsethg  */
226fd9cb95cSsethg #define	MAX_JUNK_ITERATIONS	1000
227fd9cb95cSsethg 
228fd9cb95cSsethg /*
229fd9cb95cSsethg  * Maximum time to wait for the keyboard to become ready to accept data
230fd9cb95cSsethg  * (maximum time = MAX_WAIT_ITERATIONS * USECS_PER_WAIT (default is 250ms))
231fd9cb95cSsethg  */
232fd9cb95cSsethg #define	MAX_WAIT_ITERATIONS	25000
233fd9cb95cSsethg #define	USECS_PER_WAIT		10
234fd9cb95cSsethg 
235fd9cb95cSsethg 
236fd9cb95cSsethg #ifdef __sparc
237fd9cb95cSsethg 
238fd9cb95cSsethg #define	PLATFORM_MATCH(s) (strncmp(ddi_get_name(ddi_root_node()), \
239fd9cb95cSsethg 	(s), strlen(s)) == 0)
240fd9cb95cSsethg 
241fd9cb95cSsethg /*
242fd9cb95cSsethg  * On some older SPARC platforms that have problems with the
243fd9cb95cSsethg  * interrupt line attached to the PS/2 keyboard/mouse, it
244fd9cb95cSsethg  * may be necessary to change the operating mode of the nexus
245fd9cb95cSsethg  * to a polling-based (instead of interrupt-based) method.
246fd9cb95cSsethg  * this variable is present to enable a worst-case workaround so
247fd9cb95cSsethg  * owners of these systems can still retain a working keyboard.
248fd9cb95cSsethg  *
249fd9cb95cSsethg  * The `i8042_polled_mode' variable can be used to force polled
250fd9cb95cSsethg  * mode for platforms that have this issue, but for which
251fd9cb95cSsethg  * automatic relief is not implemented.
252fd9cb95cSsethg  *
253fd9cb95cSsethg  * In the off chance that one of the platforms is misidentified
254fd9cb95cSsethg  * as requiried polling mode, `i8042_force_interrupt_mode' can
255fd9cb95cSsethg  * be set to force the nexus to use interrupts.
256fd9cb95cSsethg  */
257fd9cb95cSsethg #define	I8042_MIN_POLL_INTERVAL 1000	/* usecs */
258fd9cb95cSsethg int i8042_poll_interval = 8000;		/* usecs */
259fd9cb95cSsethg int i8042_fast_poll_interval;		/* usecs */
260fd9cb95cSsethg int i8042_slow_poll_interval;		/* usecs */
261fd9cb95cSsethg 
262fd9cb95cSsethg boolean_t i8042_polled_mode = B_FALSE;
263fd9cb95cSsethg boolean_t i8042_force_interrupt_mode = B_FALSE;
264fd9cb95cSsethg #endif /* __sparc */
265fd9cb95cSsethg 
266fd9cb95cSsethg int max_wait_iterations = MAX_WAIT_ITERATIONS;
267fd9cb95cSsethg 
268*fbac6366SSeth Goldberg #ifdef DEBUG
269*fbac6366SSeth Goldberg int i8042_debug = 0;
270*fbac6366SSeth Goldberg #endif
271*fbac6366SSeth Goldberg 
272fd9cb95cSsethg /*
2737c478bd9Sstevel@tonic-gate  * function prototypes for bus ops routines:
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate static int i8042_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
2767c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *addrp);
2777c478bd9Sstevel@tonic-gate static int i8042_ctlops(dev_info_t *dip, dev_info_t *rdip,
2787c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * function prototypes for dev ops routines:
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate static int i8042_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
2847c478bd9Sstevel@tonic-gate static int i8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
2857c478bd9Sstevel@tonic-gate static	int i8042_intr_ops(dev_info_t *dip, dev_info_t *rdip,
2867c478bd9Sstevel@tonic-gate 	ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
2877c478bd9Sstevel@tonic-gate static int i8042_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t,
2887c478bd9Sstevel@tonic-gate     void *, dev_info_t **);
2897c478bd9Sstevel@tonic-gate static int i8042_bus_unconfig(dev_info_t *, uint_t,
2907c478bd9Sstevel@tonic-gate     ddi_bus_config_op_t, void *);
291fd9cb95cSsethg #ifdef __sparc
292fd9cb95cSsethg static int i8042_build_interrupts_property(dev_info_t *dip);
293fd9cb95cSsethg static boolean_t i8042_is_polling_platform(void);
294fd9cb95cSsethg #endif
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * bus ops and dev ops structures:
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate static struct bus_ops i8042_bus_ops = {
3007c478bd9Sstevel@tonic-gate 	BUSO_REV,
3017c478bd9Sstevel@tonic-gate 	i8042_map,
3027c478bd9Sstevel@tonic-gate 	NULL,
3037c478bd9Sstevel@tonic-gate 	NULL,
3047c478bd9Sstevel@tonic-gate 	NULL,
3057c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_map_fault */
3067c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_map */
3077c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_allochdl */
3087c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_freehdl */
3097c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_bindhdl */
3107c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_unbindhdl */
3117c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_flush */
3127c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_win */
3137c478bd9Sstevel@tonic-gate 	NULL,		/* ddi_dma_mctl */
3147c478bd9Sstevel@tonic-gate 	i8042_ctlops,
3157c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,
3167c478bd9Sstevel@tonic-gate 	NULL,			/* (*bus_get_eventcookie)();	*/
3177c478bd9Sstevel@tonic-gate 	NULL,			/* (*bus_add_eventcall)();	*/
3187c478bd9Sstevel@tonic-gate 	NULL,			/* (*bus_remove_eventcall)();	*/
3197c478bd9Sstevel@tonic-gate 	NULL,			/* (*bus_post_event)();		*/
3207c478bd9Sstevel@tonic-gate 	NULL,			/* bus_intr_ctl */
3217c478bd9Sstevel@tonic-gate 	i8042_bus_config,	/* bus_config */
3227c478bd9Sstevel@tonic-gate 	i8042_bus_unconfig,	/* bus_unconfig */
3237c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_init */
3247c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_fini */
3257c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_enter */
3267c478bd9Sstevel@tonic-gate 	NULL,			/* bus_fm_access_exit */
3277c478bd9Sstevel@tonic-gate 	NULL,			/* bus_power */
3287c478bd9Sstevel@tonic-gate 	i8042_intr_ops		/* bus_intr_op */
3297c478bd9Sstevel@tonic-gate };
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate static struct dev_ops i8042_ops = {
3327c478bd9Sstevel@tonic-gate 	DEVO_REV,
3337c478bd9Sstevel@tonic-gate 	0,
3347c478bd9Sstevel@tonic-gate 	ddi_no_info,
3357c478bd9Sstevel@tonic-gate 	nulldev,
3367c478bd9Sstevel@tonic-gate 	0,
3377c478bd9Sstevel@tonic-gate 	i8042_attach,
3387c478bd9Sstevel@tonic-gate 	i8042_detach,
3397c478bd9Sstevel@tonic-gate 	nodev,
3407c478bd9Sstevel@tonic-gate 	(struct cb_ops *)0,
34119397407SSherry Moore 	&i8042_bus_ops,
34219397407SSherry Moore 	NULL,
34319397407SSherry Moore 	ddi_quiesce_not_needed,
3447c478bd9Sstevel@tonic-gate };
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * module definitions:
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
3517c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
3547c478bd9Sstevel@tonic-gate 	&mod_driverops, 	/* Type of module.  This one is a driver */
35519397407SSherry Moore 	"i8042 nexus driver",	/* Name of module. */
3567c478bd9Sstevel@tonic-gate 	&i8042_ops,		/* driver ops */
3577c478bd9Sstevel@tonic-gate };
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3607c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
3617c478bd9Sstevel@tonic-gate };
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate int
3647c478bd9Sstevel@tonic-gate _init(void)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	int e;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * Install the module.
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	e = mod_install(&modlinkage);
3727c478bd9Sstevel@tonic-gate 	return (e);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate int
3767c478bd9Sstevel@tonic-gate _fini(void)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	int e;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/*
3817c478bd9Sstevel@tonic-gate 	 * Remove the module.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	e = mod_remove(&modlinkage);
3847c478bd9Sstevel@tonic-gate 	if (e != 0)
3857c478bd9Sstevel@tonic-gate 		return (e);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	return (e);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate int
3917c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate #define	DRIVER_NAME(dip)	ddi_driver_name(dip)
3977c478bd9Sstevel@tonic-gate 
398fd9cb95cSsethg static void i8042_timeout(void *arg);
3997c478bd9Sstevel@tonic-gate static unsigned int i8042_intr(caddr_t arg);
400fd9cb95cSsethg static void i8042_write_command_byte(struct i8042 *, unsigned char);
4017c478bd9Sstevel@tonic-gate static uint8_t i8042_get8(ddi_acc_impl_t *handlep, uint8_t *addr);
4027c478bd9Sstevel@tonic-gate static void i8042_put8(ddi_acc_impl_t *handlep, uint8_t *addr,
4037c478bd9Sstevel@tonic-gate     uint8_t value);
404bb2d7d5eSSeth Goldberg static void i8042_put8_nolock(ddi_acc_impl_t *handlep, uint8_t *addr,
405bb2d7d5eSSeth Goldberg     uint8_t value);
406bb2d7d5eSSeth Goldberg static void i8042_rep_put8(ddi_acc_impl_t *handlep, uint8_t *haddr,
407bb2d7d5eSSeth Goldberg     uint8_t *daddr, size_t repcount, uint_t flags);
4087c478bd9Sstevel@tonic-gate static void i8042_send(struct i8042 *global, int reg, unsigned char cmd);
409bb2d7d5eSSeth Goldberg static uint_t i8042_intercept_softint(caddr_t arg1, caddr_t arg2);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate unsigned int i8042_unclaimed_interrupts = 0;
4127c478bd9Sstevel@tonic-gate 
4132df1fe9cSrandyf static void
4142df1fe9cSrandyf i8042_discard_junk_data(struct i8042 *global)
4152df1fe9cSrandyf {
4162df1fe9cSrandyf 	/* Discard any junk data that may have been left around */
4172df1fe9cSrandyf 	for (;;) {
4182df1fe9cSrandyf 		unsigned char		stat;
4192df1fe9cSrandyf 
4202df1fe9cSrandyf 		stat = ddi_get8(global->io_handle,
4212df1fe9cSrandyf 		    global->io_addr + I8042_STAT);
4222df1fe9cSrandyf 		if (! (stat & I8042_STAT_OUTBF))
4232df1fe9cSrandyf 			break;
4242df1fe9cSrandyf 		(void) ddi_get8(global->io_handle,
4252df1fe9cSrandyf 		    global->io_addr + I8042_DATA);
4262df1fe9cSrandyf 
4272df1fe9cSrandyf 	}
4282df1fe9cSrandyf }
4292df1fe9cSrandyf 
4307c478bd9Sstevel@tonic-gate static int
431fd9cb95cSsethg i8042_cleanup(struct i8042 *global)
432fd9cb95cSsethg {
433fd9cb95cSsethg 	int which_port, i;
434fd9cb95cSsethg 	struct i8042_port *port;
435fd9cb95cSsethg 
436fd9cb95cSsethg 	ASSERT(global != NULL);
437fd9cb95cSsethg 
438fd9cb95cSsethg 	if (global->initialized == B_TRUE) {
439fd9cb95cSsethg 		/*
440fd9cb95cSsethg 		 * If any children still have regs mapped or interrupts
441fd9cb95cSsethg 		 * registered, return immediate failure (and do nothing).
442fd9cb95cSsethg 		 */
443fd9cb95cSsethg 		mutex_enter(&global->i8042_mutex);
444fd9cb95cSsethg 
445fd9cb95cSsethg 		for (which_port = 0; which_port < NUM_PORTS; which_port++) {
446fd9cb95cSsethg 			port = &global->i8042_ports[which_port];
447fd9cb95cSsethg 
448fd9cb95cSsethg 			if (port->initialized == B_TRUE) {
449fd9cb95cSsethg 				mutex_exit(&global->i8042_mutex);
450fd9cb95cSsethg 				return (DDI_FAILURE);
451fd9cb95cSsethg 			}
452fd9cb95cSsethg #if defined(USE_SOFT_INTRS)
453fd9cb95cSsethg 			if (port->soft_hdl != 0) {
454fd9cb95cSsethg 				mutex_exit(&global->i8042_mutex);
455fd9cb95cSsethg 				return (DDI_FAILURE);
456fd9cb95cSsethg 			}
457fd9cb95cSsethg #else
458fd9cb95cSsethg 			mutex_enter(&port->intr_mutex);
459fd9cb95cSsethg 			if (port->intr_func != NULL) {
460fd9cb95cSsethg 				mutex_exit(&port->intr_mutex);
461fd9cb95cSsethg 				mutex_exit(&global->i8042_mutex);
462fd9cb95cSsethg 				return (DDI_FAILURE);
463fd9cb95cSsethg 			}
464fd9cb95cSsethg 			mutex_exit(&port->intr_mutex);
465fd9cb95cSsethg #endif
466fd9cb95cSsethg 		}
467fd9cb95cSsethg 		global->initialized = B_FALSE;
468fd9cb95cSsethg 
469fd9cb95cSsethg 		mutex_exit(&global->i8042_mutex);
470fd9cb95cSsethg 	}
471fd9cb95cSsethg 
472fd9cb95cSsethg #ifdef __sparc
473fd9cb95cSsethg 	/* If there may be an outstanding timeout, cancel it */
474fd9cb95cSsethg 	if (global->timeout_id != 0) {
475fd9cb95cSsethg 		(void) untimeout(global->timeout_id);
476fd9cb95cSsethg 	}
477fd9cb95cSsethg #endif
478fd9cb95cSsethg 
479fd9cb95cSsethg 	/* Stop the controller from generating interrupts */
480fd9cb95cSsethg 	if (global->init_state & I8042_INIT_INTRS_ENABLED)
481fd9cb95cSsethg 		i8042_write_command_byte(global, I8042_CMD_DISABLE_ALL);
482fd9cb95cSsethg 
483fd9cb95cSsethg 	if (global->intrs_added) {
484fd9cb95cSsethg 		/*
485fd9cb95cSsethg 		 * Remove the interrupts in the reverse order in
486fd9cb95cSsethg 		 * which they were added
487fd9cb95cSsethg 		 */
488fd9cb95cSsethg 		for (i = global->nintrs - 1; i >= 0; i--) {
489fd9cb95cSsethg 			if (global->intrs_added & (1 << i))
490fd9cb95cSsethg 				ddi_remove_intr(global->dip, i,
491fd9cb95cSsethg 				    global->iblock_cookies[i]);
492fd9cb95cSsethg 		}
493fd9cb95cSsethg 	}
494fd9cb95cSsethg 
495bb2d7d5eSSeth Goldberg 	(void) ddi_intr_remove_softint(global->intercept_sih);
496bb2d7d5eSSeth Goldberg 
497bb2d7d5eSSeth Goldberg 
498fd9cb95cSsethg 	if (global->init_state & I8042_INIT_MUTEXES) {
499fd9cb95cSsethg 		for (which_port = 0; which_port < NUM_PORTS; which_port++) {
500bb2d7d5eSSeth Goldberg #ifndef USE_SOFT_INTRS
501fd9cb95cSsethg 			port = &global->i8042_ports[which_port];
502fd9cb95cSsethg 			mutex_destroy(&port->intr_mutex);
503fd9cb95cSsethg #endif
504bb2d7d5eSSeth Goldberg 			mutex_destroy(&port->intercept_mutex);
505bb2d7d5eSSeth Goldberg 			cv_destroy(&port->intercept_cv);
506bb2d7d5eSSeth Goldberg 		}
507fd9cb95cSsethg 		mutex_destroy(&global->i8042_out_mutex);
508fd9cb95cSsethg 		mutex_destroy(&global->i8042_mutex);
509fd9cb95cSsethg 	}
510fd9cb95cSsethg 
511fd9cb95cSsethg 	if (global->init_state & I8042_INIT_REGS_MAPPED)
512fd9cb95cSsethg 		ddi_regs_map_free(&global->io_handle);
513fd9cb95cSsethg 
514fd9cb95cSsethg 	if (global->init_state & I8042_INIT_BASIC) {
515fd9cb95cSsethg 		ddi_set_driver_private(global->dip, (caddr_t)NULL);
516fd9cb95cSsethg 		if (global->nintrs > 0) {
517fd9cb95cSsethg 			kmem_free(global->iblock_cookies, global->nintrs *
518fd9cb95cSsethg 			    sizeof (ddi_iblock_cookie_t));
519fd9cb95cSsethg 		}
520fd9cb95cSsethg 		kmem_free(global, sizeof (struct i8042));
521fd9cb95cSsethg 	}
522fd9cb95cSsethg 
523fd9cb95cSsethg 	return (DDI_SUCCESS);
524fd9cb95cSsethg }
525fd9cb95cSsethg 
52653d6297cSmyers #define	OBF_WAIT_COUNT 1000	/* in granules of 10uS */
52753d6297cSmyers 
52853d6297cSmyers /*
52953d6297cSmyers  * Wait for the 8042 to fill the 'output' (from 8042 to host)
53053d6297cSmyers  * buffer.  If 8042 fails to fill the output buffer within an
53153d6297cSmyers  * allowed time, return 1 (which means there is no data available),
53253d6297cSmyers  * otherwise return 0
53353d6297cSmyers  */
53453d6297cSmyers static int
53553d6297cSmyers i8042_wait_obf(struct i8042 *global)
53653d6297cSmyers {
53753d6297cSmyers 	int timer = 0;
53853d6297cSmyers 
53953d6297cSmyers 	while (!(ddi_get8(global->io_handle, global->io_addr + I8042_STAT) &
54053d6297cSmyers 	    I8042_STAT_OUTBF)) {
54153d6297cSmyers 		if (++timer > OBF_WAIT_COUNT)
54253d6297cSmyers 			return (1);
54353d6297cSmyers 		drv_usecwait(10);
54453d6297cSmyers 	}
54553d6297cSmyers 	return (0);
54653d6297cSmyers }
54753d6297cSmyers 
54853d6297cSmyers /*
54953d6297cSmyers  * Drain all queued bytes from the 8042.
55053d6297cSmyers  * Return 0 for no error, <> 0 if there was an error.
55153d6297cSmyers  */
55253d6297cSmyers static int
55353d6297cSmyers i8042_purge_outbuf(struct i8042 *global)
55453d6297cSmyers {
55553d6297cSmyers 	int	i;
55653d6297cSmyers 
55753d6297cSmyers 	for (i = 0; i < MAX_JUNK_ITERATIONS; i++) {
55853d6297cSmyers 		if (i8042_wait_obf(global))
55953d6297cSmyers 			break;
56053d6297cSmyers 		(void) ddi_get8(global->io_handle,
56153d6297cSmyers 		    global->io_addr + I8042_DATA);
56253d6297cSmyers 	}
56353d6297cSmyers 
56453d6297cSmyers 	/*
56553d6297cSmyers 	 * If we hit the maximum number of iterations, then there
56653d6297cSmyers 	 * was a serious problem (e.g. our hardware may not be
56753d6297cSmyers 	 * present or working properly).
56853d6297cSmyers 	 */
56953d6297cSmyers 	return (i == MAX_JUNK_ITERATIONS);
57053d6297cSmyers }
57153d6297cSmyers 
572fd9cb95cSsethg static int
5737c478bd9Sstevel@tonic-gate i8042_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	struct i8042_port	*port;
5767c478bd9Sstevel@tonic-gate 	enum i8042_ports	which_port;
577fd9cb95cSsethg 	int			i;
578bb2d7d5eSSeth Goldberg #if !defined(USE_SOFT_INTRS)
579bb2d7d5eSSeth Goldberg 	ddi_iblock_cookie_t	cookie;
580bb2d7d5eSSeth Goldberg #endif
5817c478bd9Sstevel@tonic-gate 	static ddi_device_acc_attr_t attr = {
5827c478bd9Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
5837c478bd9Sstevel@tonic-gate 		DDI_NEVERSWAP_ACC,
5847c478bd9Sstevel@tonic-gate 		DDI_STRICTORDER_ACC,
5857c478bd9Sstevel@tonic-gate 	};
5867c478bd9Sstevel@tonic-gate 	struct i8042 *global;
587fd9cb95cSsethg #ifdef __sparc
588fd9cb95cSsethg 	int			interval;
589fd9cb95cSsethg #endif
5907c478bd9Sstevel@tonic-gate 
591fd9cb95cSsethg 	switch (cmd) {
592fd9cb95cSsethg 	case DDI_RESUME:
593fd9cb95cSsethg 		global = (struct i8042 *)ddi_get_driver_private(dip);
5942df1fe9cSrandyf 		i8042_discard_junk_data(global);
595fd9cb95cSsethg 		i8042_write_command_byte(global, I8042_CMD_ENABLE_ALL);
596fd9cb95cSsethg 		return (DDI_SUCCESS);
597fd9cb95cSsethg 
598fd9cb95cSsethg 	case DDI_ATTACH:
599fd9cb95cSsethg 		/* Handled in the main function block */
600fd9cb95cSsethg 		break;
601fd9cb95cSsethg 
602fd9cb95cSsethg 	default:
6037c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 
606fd9cb95cSsethg 	/*
607fd9cb95cSsethg 	 * DDI_ATTACH processing
608fd9cb95cSsethg 	 */
609fd9cb95cSsethg 
610fd9cb95cSsethg 	global = (struct i8042 *)kmem_zalloc(sizeof (struct i8042), KM_SLEEP);
611fd9cb95cSsethg 	ddi_set_driver_private(dip, (caddr_t)global);
612fd9cb95cSsethg 	global->dip = dip;
613fd9cb95cSsethg 	global->initialized = B_FALSE;
614fd9cb95cSsethg 
615fd9cb95cSsethg 	global->init_state |= I8042_INIT_BASIC;
616fd9cb95cSsethg 
617fd9cb95cSsethg 	if (ddi_regs_map_setup(dip, 0, (caddr_t *)&global->io_addr,
618fd9cb95cSsethg 	    (offset_t)0, (offset_t)0, &attr, &global->io_handle)
619fd9cb95cSsethg 	    != DDI_SUCCESS)
620fd9cb95cSsethg 		goto fail;
621fd9cb95cSsethg 
622fd9cb95cSsethg 	global->init_state |= I8042_INIT_REGS_MAPPED;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/*
625fd9cb95cSsethg 	 * Get the number of interrupts for this nexus
6267c478bd9Sstevel@tonic-gate 	 */
627fd9cb95cSsethg 	if (ddi_dev_nintrs(dip, &global->nintrs) == DDI_FAILURE)
628fd9cb95cSsethg 		goto fail;
6297c478bd9Sstevel@tonic-gate 
630fd9cb95cSsethg #ifdef __sparc
631fd9cb95cSsethg 	if ((i8042_polled_mode || i8042_is_polling_platform()) &&
632fd9cb95cSsethg 	    !i8042_force_interrupt_mode) {
633fd9cb95cSsethg 		/*
634fd9cb95cSsethg 		 * If we're on a platform that has known
635fd9cb95cSsethg 		 * interrupt issues with the keyboard/mouse,
636fd9cb95cSsethg 		 * use polled mode.
637fd9cb95cSsethg 		 */
638fd9cb95cSsethg 		i8042_polled_mode = B_TRUE;
639fd9cb95cSsethg 		global->nintrs = 0;
640fd9cb95cSsethg 	} else if (global->nintrs == 0) {
641fd9cb95cSsethg 		/*
642fd9cb95cSsethg 		 * If there are no interrupts on the i8042 node,
643fd9cb95cSsethg 		 * we may be on a brain-dead platform that only
644fd9cb95cSsethg 		 * has interrupts properties on i8042's children
645fd9cb95cSsethg 		 * (e.g. some UltraII-based boards)
646fd9cb95cSsethg 		 * In this case, scan first-level children, and
647fd9cb95cSsethg 		 * build a list of interrupts that each child uses,
648fd9cb95cSsethg 		 * then create an `interrupts' property on the nexus node
649fd9cb95cSsethg 		 * that contains the interrupts used by all children
650fd9cb95cSsethg 		 */
651fd9cb95cSsethg 		if (i8042_build_interrupts_property(dip) == DDI_FAILURE ||
652fd9cb95cSsethg 		    ddi_dev_nintrs(dip, &global->nintrs) == DDI_FAILURE ||
653fd9cb95cSsethg 		    global->nintrs == 0) {
654fd9cb95cSsethg 			cmn_err(CE_WARN, "i8042#%d: No interrupts defined!",
655fd9cb95cSsethg 			    ddi_get_instance(global->dip));
656fd9cb95cSsethg 			goto fail;
657fd9cb95cSsethg 		}
658fd9cb95cSsethg 	}
659fd9cb95cSsethg #else
660fd9cb95cSsethg 	if (global->nintrs == 0) {
661fd9cb95cSsethg 		cmn_err(CE_WARN, "i8042#%d: No interrupts defined!",
662fd9cb95cSsethg 		    ddi_get_instance(global->dip));
663fd9cb95cSsethg 		goto fail;
664fd9cb95cSsethg 	}
665fd9cb95cSsethg #endif
666fd9cb95cSsethg 
667fd9cb95cSsethg 	if (global->nintrs > MAX_INTERRUPTS)
668fd9cb95cSsethg 		global->nintrs = MAX_INTERRUPTS;
669fd9cb95cSsethg 
670fd9cb95cSsethg 	if (global->nintrs > 0) {
671fd9cb95cSsethg 		global->iblock_cookies = kmem_zalloc(global->nintrs *
672fd9cb95cSsethg 		    sizeof (ddi_iblock_cookie_t), KM_NOSLEEP);
673fd9cb95cSsethg 
674fd9cb95cSsethg 		for (i = 0; i < global->nintrs; i++) {
675fd9cb95cSsethg 			if (ddi_get_iblock_cookie(dip, i,
676fd9cb95cSsethg 			    &global->iblock_cookies[i]) != DDI_SUCCESS)
677fd9cb95cSsethg 				goto fail;
678fd9cb95cSsethg 		}
679fd9cb95cSsethg 	} else
680fd9cb95cSsethg 		global->iblock_cookies = NULL;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	mutex_init(&global->i8042_mutex, NULL, MUTEX_DRIVER,
683fd9cb95cSsethg 	    (global->nintrs > 0) ? global->iblock_cookies[0] : NULL);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	mutex_init(&global->i8042_out_mutex, NULL, MUTEX_DRIVER, NULL);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	for (which_port = 0; which_port < NUM_PORTS; ++which_port) {
6887c478bd9Sstevel@tonic-gate 		port = &global->i8042_ports[which_port];
6897c478bd9Sstevel@tonic-gate 		port->initialized = B_FALSE;
6907c478bd9Sstevel@tonic-gate 		port->i8042_global = global;
6917c478bd9Sstevel@tonic-gate 		port->which = which_port;
692bb2d7d5eSSeth Goldberg 		port->intr_intercept_enabled = B_FALSE;
693bb2d7d5eSSeth Goldberg 		cv_init(&port->intercept_cv, NULL, CV_DRIVER, NULL);
694fd9cb95cSsethg #if defined(USE_SOFT_INTRS)
695fd9cb95cSsethg 		port->soft_hdl = 0;
696fd9cb95cSsethg #else
697bb2d7d5eSSeth Goldberg 
698bb2d7d5eSSeth Goldberg 		mutex_init(&port->intercept_mutex, NULL, MUTEX_DRIVER,
699bb2d7d5eSSeth Goldberg 		    (void *)DDI_INTR_SOFTPRI_DEFAULT);
700bb2d7d5eSSeth Goldberg 
701fd9cb95cSsethg 		/*
702fd9cb95cSsethg 		 * Assume that the interrupt block cookie for port <n>
703fd9cb95cSsethg 		 * is iblock_cookies[<n>] (a 1:1 mapping).  If there are not
704fd9cb95cSsethg 		 * enough interrupts to cover the number of ports, use
705fd9cb95cSsethg 		 * the cookie from interrupt 0.
706fd9cb95cSsethg 		 */
707bb2d7d5eSSeth Goldberg 		if (global->nintrs > 0) {
708bb2d7d5eSSeth Goldberg 			cookie = global->iblock_cookies[
709bb2d7d5eSSeth Goldberg 			    (which_port < global->nintrs) ? which_port : 0];
710bb2d7d5eSSeth Goldberg 
7117c478bd9Sstevel@tonic-gate 			mutex_init(&port->intr_mutex, NULL, MUTEX_DRIVER,
712bb2d7d5eSSeth Goldberg 			    cookie);
713bb2d7d5eSSeth Goldberg 
714bb2d7d5eSSeth Goldberg 		} else {
715fd9cb95cSsethg 			mutex_init(&port->intr_mutex, NULL, MUTEX_DRIVER, NULL);
716bb2d7d5eSSeth Goldberg 			mutex_init(&port->intercept_mutex, NULL, MUTEX_DRIVER,
717bb2d7d5eSSeth Goldberg 			    NULL);
718bb2d7d5eSSeth Goldberg 		}
719fd9cb95cSsethg 
720fd9cb95cSsethg #endif
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
723fd9cb95cSsethg 	global->init_state |= I8042_INIT_MUTEXES;
724fd9cb95cSsethg 
7257c478bd9Sstevel@tonic-gate 	/*
7267c478bd9Sstevel@tonic-gate 	 * Disable input and interrupts from both the main and aux ports.
7277c478bd9Sstevel@tonic-gate 	 *
7287c478bd9Sstevel@tonic-gate 	 * It is difficult if not impossible to read the command byte in
7297c478bd9Sstevel@tonic-gate 	 * a completely clean way.  Reading the command byte may cause
7307c478bd9Sstevel@tonic-gate 	 * an interrupt, and there is no way to suppress interrupts without
7317c478bd9Sstevel@tonic-gate 	 * writing the command byte.  On a PC we might rely on the fact
7327c478bd9Sstevel@tonic-gate 	 * that IRQ 1 is disabled and guaranteed not shared, but on
7337c478bd9Sstevel@tonic-gate 	 * other platforms the interrupt line might be shared and so
7347c478bd9Sstevel@tonic-gate 	 * causing an interrupt could be bad.
7357c478bd9Sstevel@tonic-gate 	 *
7367c478bd9Sstevel@tonic-gate 	 * Since we can't read the command byte and update it, we
737fd9cb95cSsethg 	 * just set it to static values.
7387c478bd9Sstevel@tonic-gate 	 */
739fd9cb95cSsethg 	i8042_write_command_byte(global, I8042_CMD_DISABLE_ALL);
7407c478bd9Sstevel@tonic-gate 
741fd9cb95cSsethg 	global->init_state &= ~I8042_INIT_INTRS_ENABLED;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/* Discard any junk data that may have been left around */
74453d6297cSmyers 	if (i8042_purge_outbuf(global) != 0)
745fd9cb95cSsethg 		goto fail;
7467c478bd9Sstevel@tonic-gate 
747bb2d7d5eSSeth Goldberg 
748bb2d7d5eSSeth Goldberg 	if (ddi_intr_add_softint(dip, &global->intercept_sih,
749bb2d7d5eSSeth Goldberg 	    DDI_INTR_SOFTPRI_DEFAULT, i8042_intercept_softint, global)
750bb2d7d5eSSeth Goldberg 	    != DDI_SUCCESS)
751bb2d7d5eSSeth Goldberg 		goto fail;
752bb2d7d5eSSeth Goldberg 
753fd9cb95cSsethg 	/*
754fd9cb95cSsethg 	 * Assume the number of interrupts is less that the number of
755fd9cb95cSsethg 	 * bits in the variable used to keep track of which interrupt
756fd9cb95cSsethg 	 * was added.
757fd9cb95cSsethg 	 */
758fd9cb95cSsethg 	ASSERT(global->nintrs <= (sizeof (global->intrs_added) * NBBY));
759fd9cb95cSsethg 
760fd9cb95cSsethg 	for (i = 0; i < global->nintrs; i++) {
761fd9cb95cSsethg 		/*
762fd9cb95cSsethg 		 * The 8042 handles all interrupts, because all
763fd9cb95cSsethg 		 * device access goes through the same I/O addresses.
764fd9cb95cSsethg 		 */
765fd9cb95cSsethg 		if (ddi_add_intr(dip, i,
766fd9cb95cSsethg 		    (ddi_iblock_cookie_t *)NULL,
767fd9cb95cSsethg 		    (ddi_idevice_cookie_t *)NULL,
768fd9cb95cSsethg 		    i8042_intr, (caddr_t)global) != DDI_SUCCESS)
769fd9cb95cSsethg 			goto fail;
770fd9cb95cSsethg 
771fd9cb95cSsethg 		global->intrs_added |= (1 << i);
772fd9cb95cSsethg 	}
773fd9cb95cSsethg 
774fd9cb95cSsethg 	global->initialized = B_TRUE;
775fd9cb95cSsethg 
776fd9cb95cSsethg 	/*
777fd9cb95cSsethg 	 * Enable the main and aux data ports and interrupts
778fd9cb95cSsethg 	 */
779fd9cb95cSsethg 	i8042_write_command_byte(global, I8042_CMD_ENABLE_ALL);
780fd9cb95cSsethg 	global->init_state |= I8042_INIT_INTRS_ENABLED;
781fd9cb95cSsethg 
782fd9cb95cSsethg #ifdef __sparc
783fd9cb95cSsethg 	if (i8042_polled_mode) {
784fd9cb95cSsethg 		/*
785fd9cb95cSsethg 		 * Do not allow anyone to set the polling interval
786fd9cb95cSsethg 		 * to an interval more frequent than I8042_MIN_POLL_INTERVAL --
787fd9cb95cSsethg 		 * it could hose the system.
788fd9cb95cSsethg 		 */
789fd9cb95cSsethg 		interval = i8042_poll_interval;
790fd9cb95cSsethg 		if (interval < I8042_MIN_POLL_INTERVAL)
791fd9cb95cSsethg 			interval = I8042_MIN_POLL_INTERVAL;
792fd9cb95cSsethg 		i8042_fast_poll_interval = interval;
793fd9cb95cSsethg 		i8042_slow_poll_interval = interval << 3;
794fd9cb95cSsethg 
795fd9cb95cSsethg 		global->timeout_id = timeout(i8042_timeout, global,
796fd9cb95cSsethg 		    drv_usectohz(i8042_slow_poll_interval));
797fd9cb95cSsethg 	}
798fd9cb95cSsethg #endif
799fd9cb95cSsethg 
800fd9cb95cSsethg 	return (DDI_SUCCESS);
801fd9cb95cSsethg 
802fd9cb95cSsethg fail:
803fd9cb95cSsethg 	/* cleanup will succeed because no children have attached yet */
804fd9cb95cSsethg 	(void) i8042_cleanup(global);
805fd9cb95cSsethg 	return (DDI_FAILURE);
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8097c478bd9Sstevel@tonic-gate static int
8107c478bd9Sstevel@tonic-gate i8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8117c478bd9Sstevel@tonic-gate {
812fd9cb95cSsethg 	struct i8042 *global = (struct i8042 *)ddi_get_driver_private(dip);
8137c478bd9Sstevel@tonic-gate 
814fd9cb95cSsethg 	ASSERT(global != NULL);
815fd9cb95cSsethg 
816fd9cb95cSsethg 	switch (cmd) {
817fd9cb95cSsethg 	case DDI_SUSPEND:
8187c478bd9Sstevel@tonic-gate 		/*
819fd9cb95cSsethg 		 * Do not disable the keyboard controller for x86 suspend, as
820fd9cb95cSsethg 		 * the keyboard can be used to bring the system out of
821fd9cb95cSsethg 		 * suspend.
8227c478bd9Sstevel@tonic-gate 		 */
823fd9cb95cSsethg #ifdef __sparc
824fd9cb95cSsethg 		/* Disable interrupts and controller devices before suspend */
825fd9cb95cSsethg 		i8042_write_command_byte(global, I8042_CMD_DISABLE_ALL);
826fd9cb95cSsethg #endif
827fd9cb95cSsethg 		return (DDI_SUCCESS);
828fd9cb95cSsethg 
829fd9cb95cSsethg 	case DDI_DETACH:
830fd9cb95cSsethg 		/* DETACH can only succeed if cleanup succeeds */
831fd9cb95cSsethg 		return (i8042_cleanup(global));
832fd9cb95cSsethg 
833fd9cb95cSsethg 	default:
8347c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8357c478bd9Sstevel@tonic-gate 	}
836fd9cb95cSsethg }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * The primary interface to us from our children is via virtual registers.
8407c478bd9Sstevel@tonic-gate  * This is the entry point that allows our children to "map" these
8417c478bd9Sstevel@tonic-gate  * virtual registers.
8427c478bd9Sstevel@tonic-gate  */
8437c478bd9Sstevel@tonic-gate static int
8447c478bd9Sstevel@tonic-gate i8042_map(
8457c478bd9Sstevel@tonic-gate 	dev_info_t *dip,
8467c478bd9Sstevel@tonic-gate 	dev_info_t *rdip,
8477c478bd9Sstevel@tonic-gate 	ddi_map_req_t *mp,
8487c478bd9Sstevel@tonic-gate 	off_t offset,
8497c478bd9Sstevel@tonic-gate 	off_t len,
8507c478bd9Sstevel@tonic-gate 	caddr_t *addrp)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	struct i8042_port	*port;
8537c478bd9Sstevel@tonic-gate 	struct i8042		*global;
8547c478bd9Sstevel@tonic-gate 	enum i8042_ports	which_port;
8557c478bd9Sstevel@tonic-gate 	int			*iprop;
8567c478bd9Sstevel@tonic-gate 	unsigned int		iprop_len;
8577c478bd9Sstevel@tonic-gate 	int			rnumber;
8587c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t		*handle;
8597c478bd9Sstevel@tonic-gate 	ddi_acc_impl_t		*ap;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	global = ddi_get_driver_private(dip);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	switch (mp->map_type) {
8647c478bd9Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
8657c478bd9Sstevel@tonic-gate 		which_port = *(int *)mp->map_obj.rp;
8667c478bd9Sstevel@tonic-gate 		break;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
8697c478bd9Sstevel@tonic-gate 		rnumber = mp->map_obj.rnumber;
8707c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, rdip,
8717c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", &iprop, &iprop_len) !=
8727c478bd9Sstevel@tonic-gate 		    DDI_SUCCESS) {
8737c478bd9Sstevel@tonic-gate #if defined(DEBUG)
8747c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s #%d:  Missing 'reg' on %s@%s",
8757c478bd9Sstevel@tonic-gate 			    DRIVER_NAME(dip), ddi_get_instance(dip),
8767c478bd9Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip));
8777c478bd9Sstevel@tonic-gate #endif
8787c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate #if defined(DEBUG)
8817c478bd9Sstevel@tonic-gate 		if (iprop_len != 1) {
8827c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s #%d:  Malformed 'reg' on %s@%s",
8837c478bd9Sstevel@tonic-gate 			    DRIVER_NAME(dip), ddi_get_instance(dip),
8847c478bd9Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip));
8857c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 		if (rnumber < 0 || rnumber >= iprop_len) {
8887c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s #%d:  bad map request for %s@%s",
8897c478bd9Sstevel@tonic-gate 			    DRIVER_NAME(dip), ddi_get_instance(dip),
8907c478bd9Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip));
8917c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate #endif
8947c478bd9Sstevel@tonic-gate 		which_port = iprop[rnumber];
8957c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)iprop);
8967c478bd9Sstevel@tonic-gate #if defined(DEBUG)
8977c478bd9Sstevel@tonic-gate 		if (which_port != MAIN_PORT && which_port != AUX_PORT) {
8987c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
8997c478bd9Sstevel@tonic-gate 			    "%s #%d:  bad 'reg' value %d on %s@%s",
9007c478bd9Sstevel@tonic-gate 			    DRIVER_NAME(dip), ddi_get_instance(dip),
9017c478bd9Sstevel@tonic-gate 			    which_port,
9027c478bd9Sstevel@tonic-gate 			    ddi_node_name(rdip), ddi_get_name_addr(rdip));
9037c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate #endif
9067c478bd9Sstevel@tonic-gate 		break;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	default:
9097c478bd9Sstevel@tonic-gate #if defined(DEBUG)
9107c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s #%d:  unknown map type %d for %s@%s",
9117c478bd9Sstevel@tonic-gate 		    DRIVER_NAME(dip), ddi_get_instance(dip),
9127c478bd9Sstevel@tonic-gate 		    mp->map_type,
9137c478bd9Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip));
9147c478bd9Sstevel@tonic-gate #endif
9157c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9167c478bd9Sstevel@tonic-gate 	}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate #if defined(DEBUG)
9197c478bd9Sstevel@tonic-gate 	if (offset != 0 || len != 0) {
9207c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
9217c478bd9Sstevel@tonic-gate 		    "%s #%d:  partial mapping attempt for %s@%s ignored",
9227c478bd9Sstevel@tonic-gate 		    DRIVER_NAME(dip), ddi_get_instance(dip),
9237c478bd9Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip));
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate #endif
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	port = &global->i8042_ports[which_port];
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	switch (mp->map_op) {
9307c478bd9Sstevel@tonic-gate 	case DDI_MO_MAP_LOCKED:
9317c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
9327c478bd9Sstevel@tonic-gate 		port->soft_intr_enabled = B_FALSE;
9337c478bd9Sstevel@tonic-gate #else
9347c478bd9Sstevel@tonic-gate 		port->intr_func = NULL;
9357c478bd9Sstevel@tonic-gate #endif
9367c478bd9Sstevel@tonic-gate 		port->wptr = 0;
9377c478bd9Sstevel@tonic-gate 		port->rptr = 0;
9387c478bd9Sstevel@tonic-gate 		port->dip = dip;
9397c478bd9Sstevel@tonic-gate 		port->inumber = 0;
9407c478bd9Sstevel@tonic-gate 		port->initialized = B_TRUE;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		handle = mp->map_handlep;
9437c478bd9Sstevel@tonic-gate 		handle->ah_bus_private = port;
9447c478bd9Sstevel@tonic-gate 		handle->ah_addr = 0;
9457c478bd9Sstevel@tonic-gate 		ap = (ddi_acc_impl_t *)handle->ah_platform_private;
9467c478bd9Sstevel@tonic-gate 		/*
947bb2d7d5eSSeth Goldberg 		 * Support get8, put8 and _rep_put8
9487c478bd9Sstevel@tonic-gate 		 */
9497c478bd9Sstevel@tonic-gate 		ap->ahi_put8 = i8042_put8;
9507c478bd9Sstevel@tonic-gate 		ap->ahi_get8 = i8042_get8;
9517c478bd9Sstevel@tonic-gate 		ap->ahi_put16 = NULL;
9527c478bd9Sstevel@tonic-gate 		ap->ahi_get16 = NULL;
9537c478bd9Sstevel@tonic-gate 		ap->ahi_put32 = NULL;
9547c478bd9Sstevel@tonic-gate 		ap->ahi_get32 = NULL;
9557c478bd9Sstevel@tonic-gate 		ap->ahi_put64 = NULL;
9567c478bd9Sstevel@tonic-gate 		ap->ahi_get64 = NULL;
957bb2d7d5eSSeth Goldberg 		ap->ahi_rep_put8 = i8042_rep_put8;
9587c478bd9Sstevel@tonic-gate 		ap->ahi_rep_get8 = NULL;
9597c478bd9Sstevel@tonic-gate 		ap->ahi_rep_put16 = NULL;
9607c478bd9Sstevel@tonic-gate 		ap->ahi_rep_get16 = NULL;
9617c478bd9Sstevel@tonic-gate 		ap->ahi_rep_put32 = NULL;
9627c478bd9Sstevel@tonic-gate 		ap->ahi_rep_get32 = NULL;
9637c478bd9Sstevel@tonic-gate 		ap->ahi_rep_put64 = NULL;
9647c478bd9Sstevel@tonic-gate 		ap->ahi_rep_get64 = NULL;
9657c478bd9Sstevel@tonic-gate 		*addrp = 0;
9667c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	case DDI_MO_UNMAP:
9697c478bd9Sstevel@tonic-gate 		port->initialized = B_FALSE;
9707c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	default:
9737c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s:  map operation %d not supported",
9747c478bd9Sstevel@tonic-gate 		    DRIVER_NAME(dip), mp->map_op);
9757c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
979fd9cb95cSsethg #ifdef __sparc
980fd9cb95cSsethg static void
981fd9cb95cSsethg i8042_timeout(void *arg)
982fd9cb95cSsethg {
983fd9cb95cSsethg 	struct i8042 *i8042_p = (struct i8042 *)arg;
984fd9cb95cSsethg 	int interval;
985fd9cb95cSsethg 
986fd9cb95cSsethg 	/*
987fd9cb95cSsethg 	 * Allow the polling speed to be changed on the fly --
988fd9cb95cSsethg 	 * catch it here and update the intervals used.
989fd9cb95cSsethg 	 */
990fd9cb95cSsethg 	if (i8042_fast_poll_interval != i8042_poll_interval) {
991fd9cb95cSsethg 		interval = i8042_poll_interval;
992fd9cb95cSsethg 		if (interval < I8042_MIN_POLL_INTERVAL)
993fd9cb95cSsethg 			interval = I8042_MIN_POLL_INTERVAL;
994fd9cb95cSsethg 		i8042_fast_poll_interval = interval;
995fd9cb95cSsethg 		i8042_slow_poll_interval = interval << 3;
996fd9cb95cSsethg 	}
997fd9cb95cSsethg 
998fd9cb95cSsethg 	/*
999fd9cb95cSsethg 	 * If the ISR returned true, start polling at a faster rate to
1000fd9cb95cSsethg 	 * increate responsiveness.  Once the keyboard or mouse go idle,
1001fd9cb95cSsethg 	 * the ISR will return UNCLAIMED, and we'll go back to the slower
1002fd9cb95cSsethg 	 * polling rate.  This gives some positive hysteresis (but not
1003fd9cb95cSsethg 	 * negative, since we go back to the slower polling interval after
1004fd9cb95cSsethg 	 * only one UNCLAIMED).  This has shown to be responsive enough,
1005fd9cb95cSsethg 	 * even for fast typers.
1006fd9cb95cSsethg 	 */
1007fd9cb95cSsethg 	interval = (i8042_intr((caddr_t)i8042_p) == DDI_INTR_CLAIMED) ?
1008fd9cb95cSsethg 	    i8042_fast_poll_interval : i8042_slow_poll_interval;
1009fd9cb95cSsethg 
1010fd9cb95cSsethg 	if (i8042_polled_mode)
1011fd9cb95cSsethg 		i8042_p->timeout_id = timeout(i8042_timeout, arg,
1012fd9cb95cSsethg 		    drv_usectohz(interval));
1013fd9cb95cSsethg 	else
1014fd9cb95cSsethg 		i8042_p->timeout_id = 0;
1015fd9cb95cSsethg }
1016fd9cb95cSsethg #endif
1017fd9cb95cSsethg 
10187c478bd9Sstevel@tonic-gate /*
10197c478bd9Sstevel@tonic-gate  * i8042 hardware interrupt routine.  Called for both main and aux port
10207c478bd9Sstevel@tonic-gate  * interrupts.
10217c478bd9Sstevel@tonic-gate  */
10227c478bd9Sstevel@tonic-gate static unsigned int
10237c478bd9Sstevel@tonic-gate i8042_intr(caddr_t arg)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	struct i8042		*global = (struct i8042 *)arg;
10267c478bd9Sstevel@tonic-gate 	enum i8042_ports	which_port;
10277c478bd9Sstevel@tonic-gate 	unsigned char		stat;
10287c478bd9Sstevel@tonic-gate 	unsigned char		byte;
10297c478bd9Sstevel@tonic-gate 	int			new_wptr;
10307c478bd9Sstevel@tonic-gate 	struct i8042_port	*port;
10317c478bd9Sstevel@tonic-gate 
1032bb2d7d5eSSeth Goldberg #ifdef DEBUG
1033bb2d7d5eSSeth Goldberg 	global->intr_thread = curthread;
1034bb2d7d5eSSeth Goldberg #endif
10357c478bd9Sstevel@tonic-gate 	mutex_enter(&global->i8042_mutex);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	stat = ddi_get8(global->io_handle, global->io_addr + I8042_STAT);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if (! (stat & I8042_STAT_OUTBF)) {
10407c478bd9Sstevel@tonic-gate 		++i8042_unclaimed_interrupts;
10417c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
1042bb2d7d5eSSeth Goldberg #ifdef DEBUG
1043bb2d7d5eSSeth Goldberg 		global->intr_thread = NULL;
1044bb2d7d5eSSeth Goldberg #endif
10457c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	byte = ddi_get8(global->io_handle, global->io_addr + I8042_DATA);
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	which_port = (stat & I8042_STAT_AUXBF) ? AUX_PORT : MAIN_PORT;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	port = &global->i8042_ports[which_port];
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (! port->initialized) {
10557c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
1056bb2d7d5eSSeth Goldberg #ifdef DEBUG
1057bb2d7d5eSSeth Goldberg 		global->intr_thread = NULL;
1058bb2d7d5eSSeth Goldberg #endif
1059bb2d7d5eSSeth Goldberg 		return (DDI_INTR_CLAIMED);
1060bb2d7d5eSSeth Goldberg 	}
1061bb2d7d5eSSeth Goldberg 
1062bb2d7d5eSSeth Goldberg 	/*
1063bb2d7d5eSSeth Goldberg 	 * If interception is enabled, and the byte matches what is being
1064bb2d7d5eSSeth Goldberg 	 * waited for, clear the interception flag and trigger a softintr
1065bb2d7d5eSSeth Goldberg 	 * that will signal the waiter, then exit the interrupt handler
1066bb2d7d5eSSeth Goldberg 	 * without passing the byte to the child's interrupt handler.
1067bb2d7d5eSSeth Goldberg 	 */
1068*fbac6366SSeth Goldberg 	if (port->intr_intercept_enabled && (port->intercept[0] == byte ||
1069*fbac6366SSeth Goldberg 	    port->intercept[1] == byte)) {
1070*fbac6366SSeth Goldberg 		port->intercepted_byte = byte;
1071bb2d7d5eSSeth Goldberg 		port->intr_intercept_enabled = B_FALSE;
1072bb2d7d5eSSeth Goldberg 		(void) ddi_intr_trigger_softint(global->intercept_sih, port);
1073bb2d7d5eSSeth Goldberg 		mutex_exit(&global->i8042_mutex);
1074bb2d7d5eSSeth Goldberg #ifdef DEBUG
1075bb2d7d5eSSeth Goldberg 		global->intr_thread = NULL;
1076bb2d7d5eSSeth Goldberg #endif
10777c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	new_wptr = (port->wptr + 1) % BUFSIZ;
10817c478bd9Sstevel@tonic-gate 	if (new_wptr == port->rptr) {
10827c478bd9Sstevel@tonic-gate 		port->overruns++;
10837c478bd9Sstevel@tonic-gate #if defined(DEBUG)
10847c478bd9Sstevel@tonic-gate 		if (port->overruns % 50 == 1) {
10857c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "i8042/%d: %d overruns\n",
10867c478bd9Sstevel@tonic-gate 			    which_port, port->overruns);
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate #endif
1089bb2d7d5eSSeth Goldberg 
10907c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
1091bb2d7d5eSSeth Goldberg 
1092bb2d7d5eSSeth Goldberg #ifdef DEBUG
1093bb2d7d5eSSeth Goldberg 		global->intr_thread = NULL;
1094bb2d7d5eSSeth Goldberg #endif
10957c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	port->buf[port->wptr] = byte;
10997c478bd9Sstevel@tonic-gate 	port->wptr = new_wptr;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
11027c478bd9Sstevel@tonic-gate 	if (port->soft_intr_enabled)
1103fd9cb95cSsethg 		(void) ddi_intr_trigger_softint(port->soft_hdl,
1104fd9cb95cSsethg 		    port->intr_arg2);
11057c478bd9Sstevel@tonic-gate #endif
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	mutex_exit(&global->i8042_mutex);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate #if	!defined(USE_SOFT_INTRS)
11107c478bd9Sstevel@tonic-gate 	mutex_enter(&port->intr_mutex);
11117c478bd9Sstevel@tonic-gate 	if (port->intr_func != NULL)
11127c478bd9Sstevel@tonic-gate 		port->intr_func(port->intr_arg1, NULL);
11137c478bd9Sstevel@tonic-gate 	mutex_exit(&port->intr_mutex);
11147c478bd9Sstevel@tonic-gate #endif
11157c478bd9Sstevel@tonic-gate 
1116bb2d7d5eSSeth Goldberg #ifdef DEBUG
1117bb2d7d5eSSeth Goldberg 	global->intr_thread = NULL;
1118bb2d7d5eSSeth Goldberg #endif
11197c478bd9Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate static void
1123fd9cb95cSsethg i8042_write_command_byte(struct i8042 *global, unsigned char cb)
11247c478bd9Sstevel@tonic-gate {
1125fd9cb95cSsethg 	mutex_enter(&global->i8042_out_mutex);
11267c478bd9Sstevel@tonic-gate 	i8042_send(global, I8042_CMD, I8042_CMD_WCB);
11277c478bd9Sstevel@tonic-gate 	i8042_send(global, I8042_DATA, cb);
1128fd9cb95cSsethg 	mutex_exit(&global->i8042_out_mutex);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate  * Send a byte to either the i8042 command or data register, depending on
11337c478bd9Sstevel@tonic-gate  * the argument.
11347c478bd9Sstevel@tonic-gate  */
11357c478bd9Sstevel@tonic-gate static void
11367c478bd9Sstevel@tonic-gate i8042_send(struct i8042 *global, int reg, unsigned char val)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 	uint8_t stat;
1139fd9cb95cSsethg 	int tries = 0;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	/*
11427c478bd9Sstevel@tonic-gate 	 * First, wait for the i8042 to be ready to accept data.
11437c478bd9Sstevel@tonic-gate 	 */
1144fd9cb95cSsethg 	/*CONSTANTCONDITION*/
1145fd9cb95cSsethg 	while (1) {
11467c478bd9Sstevel@tonic-gate 		stat = ddi_get8(global->io_handle,
11477c478bd9Sstevel@tonic-gate 		    global->io_addr + I8042_STAT);
11487c478bd9Sstevel@tonic-gate 
1149fd9cb95cSsethg 		if ((stat & I8042_STAT_INBF) == 0) {
11507c478bd9Sstevel@tonic-gate 			ddi_put8(global->io_handle, global->io_addr+reg, val);
1151fd9cb95cSsethg 			break;
1152fd9cb95cSsethg 		}
1153fd9cb95cSsethg 
1154fd9cb95cSsethg 		/* Don't wait unless we're going to check again */
1155fd9cb95cSsethg 		if (++tries >= max_wait_iterations)
1156fd9cb95cSsethg 			break;
1157fd9cb95cSsethg 		else
1158fd9cb95cSsethg 			drv_usecwait(USECS_PER_WAIT);
1159fd9cb95cSsethg 	}
1160fd9cb95cSsethg 
1161fd9cb95cSsethg #ifdef DEBUG
1162fd9cb95cSsethg 	if (tries >= MAX_WAIT_ITERATIONS)
1163fd9cb95cSsethg 		cmn_err(CE_WARN, "i8042_send: timeout!");
1164fd9cb95cSsethg #endif
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate /*
11687c478bd9Sstevel@tonic-gate  * Here's the interface to the virtual registers on the device.
11697c478bd9Sstevel@tonic-gate  *
11707c478bd9Sstevel@tonic-gate  * Normal interrupt-driven I/O:
11717c478bd9Sstevel@tonic-gate  *
11727c478bd9Sstevel@tonic-gate  * I8042_INT_INPUT_AVAIL	(r/o)
11737c478bd9Sstevel@tonic-gate  *	Interrupt mode input bytes available?  Zero = No.
11747c478bd9Sstevel@tonic-gate  * I8042_INT_INPUT_DATA		(r/o)
11757c478bd9Sstevel@tonic-gate  *	Fetch interrupt mode input byte.
11767c478bd9Sstevel@tonic-gate  * I8042_INT_OUTPUT_DATA	(w/o)
11777c478bd9Sstevel@tonic-gate  *	Interrupt mode output byte.
11787c478bd9Sstevel@tonic-gate  *
11797c478bd9Sstevel@tonic-gate  * Polled I/O, used by (e.g.) kmdb, when normal system services are
11807c478bd9Sstevel@tonic-gate  * unavailable:
11817c478bd9Sstevel@tonic-gate  *
11827c478bd9Sstevel@tonic-gate  * I8042_POLL_INPUT_AVAIL	(r/o)
11837c478bd9Sstevel@tonic-gate  *	Polled mode input bytes available?  Zero = No.
11847c478bd9Sstevel@tonic-gate  * I8042_POLL_INPUT_DATA	(r/o)
11857c478bd9Sstevel@tonic-gate  *	Polled mode input byte.
11867c478bd9Sstevel@tonic-gate  * I8042_POLL_OUTPUT_DATA	(w/o)
11877c478bd9Sstevel@tonic-gate  *	Polled mode output byte.
11887c478bd9Sstevel@tonic-gate  *
11897c478bd9Sstevel@tonic-gate  * Note that in polled mode we cannot use cmn_err; only prom_printf is safe.
11907c478bd9Sstevel@tonic-gate  */
11917c478bd9Sstevel@tonic-gate static uint8_t
11927c478bd9Sstevel@tonic-gate i8042_get8(ddi_acc_impl_t *handlep, uint8_t *addr)
11937c478bd9Sstevel@tonic-gate {
11947c478bd9Sstevel@tonic-gate 	struct i8042_port *port;
11957c478bd9Sstevel@tonic-gate 	struct i8042 *global;
11967c478bd9Sstevel@tonic-gate 	uint8_t	ret;
11977c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t	*h;
11987c478bd9Sstevel@tonic-gate 	uint8_t stat;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	h = (ddi_acc_hdl_t *)handlep;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	port = (struct i8042_port *)h->ah_bus_private;
12037c478bd9Sstevel@tonic-gate 	global = port->i8042_global;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	switch ((uintptr_t)addr) {
12067c478bd9Sstevel@tonic-gate 	case I8042_INT_INPUT_AVAIL:
12077c478bd9Sstevel@tonic-gate 		mutex_enter(&global->i8042_mutex);
12087c478bd9Sstevel@tonic-gate 		ret = port->rptr != port->wptr;
12097c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
12107c478bd9Sstevel@tonic-gate 		return (ret);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	case I8042_INT_INPUT_DATA:
12137c478bd9Sstevel@tonic-gate 		mutex_enter(&global->i8042_mutex);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		if (port->rptr != port->wptr) {
12167c478bd9Sstevel@tonic-gate 			ret = port->buf[port->rptr];
12177c478bd9Sstevel@tonic-gate 			port->rptr = (port->rptr + 1) % BUFSIZ;
12187c478bd9Sstevel@tonic-gate 		} else {
12197c478bd9Sstevel@tonic-gate #if defined(DEBUG)
12207c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
12217c478bd9Sstevel@tonic-gate 			    "i8042:  Tried to read from empty buffer");
12227c478bd9Sstevel@tonic-gate #endif
12237c478bd9Sstevel@tonic-gate 			ret = 0;
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 		break;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate #if defined(DEBUG)
12327c478bd9Sstevel@tonic-gate 	case I8042_INT_OUTPUT_DATA:
12337c478bd9Sstevel@tonic-gate 	case I8042_POLL_OUTPUT_DATA:
12347c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i8042:  read of write-only register 0x%p",
12357c478bd9Sstevel@tonic-gate 		    (void *)addr);
12367c478bd9Sstevel@tonic-gate 		ret = 0;
12377c478bd9Sstevel@tonic-gate 		break;
12387c478bd9Sstevel@tonic-gate #endif
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	case I8042_POLL_INPUT_AVAIL:
12417c478bd9Sstevel@tonic-gate 		if (port->rptr != port->wptr)
12427c478bd9Sstevel@tonic-gate 			return (B_TRUE);
12437c478bd9Sstevel@tonic-gate 		for (;;) {
12447c478bd9Sstevel@tonic-gate 			stat = ddi_get8(global->io_handle,
12457c478bd9Sstevel@tonic-gate 			    global->io_addr + I8042_STAT);
12467c478bd9Sstevel@tonic-gate 			if ((stat & I8042_STAT_OUTBF) == 0)
12477c478bd9Sstevel@tonic-gate 				return (B_FALSE);
12487c478bd9Sstevel@tonic-gate 			switch (port->which) {
12497c478bd9Sstevel@tonic-gate 			case MAIN_PORT:
12507c478bd9Sstevel@tonic-gate 				if ((stat & I8042_STAT_AUXBF) == 0)
12517c478bd9Sstevel@tonic-gate 					return (B_TRUE);
12527c478bd9Sstevel@tonic-gate 				break;
12537c478bd9Sstevel@tonic-gate 			case AUX_PORT:
12547c478bd9Sstevel@tonic-gate 				if ((stat & I8042_STAT_AUXBF) != 0)
12557c478bd9Sstevel@tonic-gate 					return (B_TRUE);
12567c478bd9Sstevel@tonic-gate 				break;
1257fd9cb95cSsethg 			default:
1258fd9cb95cSsethg 				cmn_err(CE_WARN, "data from unknown port: %d",
1259fd9cb95cSsethg 				    port->which);
12607c478bd9Sstevel@tonic-gate 			}
12617c478bd9Sstevel@tonic-gate 			/*
12627c478bd9Sstevel@tonic-gate 			 * Data for wrong port pending; discard it.
12637c478bd9Sstevel@tonic-gate 			 */
12647c478bd9Sstevel@tonic-gate 			(void) ddi_get8(global->io_handle,
12657c478bd9Sstevel@tonic-gate 			    global->io_addr + I8042_DATA);
12667c478bd9Sstevel@tonic-gate 		}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	case I8042_POLL_INPUT_DATA:
12717c478bd9Sstevel@tonic-gate 		if (port->rptr != port->wptr) {
12727c478bd9Sstevel@tonic-gate 			ret = port->buf[port->rptr];
12737c478bd9Sstevel@tonic-gate 			port->rptr = (port->rptr + 1) % BUFSIZ;
12747c478bd9Sstevel@tonic-gate 			return (ret);
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		stat = ddi_get8(global->io_handle,
12787c478bd9Sstevel@tonic-gate 		    global->io_addr + I8042_STAT);
12797c478bd9Sstevel@tonic-gate 		if ((stat & I8042_STAT_OUTBF) == 0) {
12807c478bd9Sstevel@tonic-gate #if defined(DEBUG)
12817c478bd9Sstevel@tonic-gate 			prom_printf("I8042_POLL_INPUT_DATA:  no data!\n");
12827c478bd9Sstevel@tonic-gate #endif
12837c478bd9Sstevel@tonic-gate 			return (0);
12847c478bd9Sstevel@tonic-gate 		}
12857c478bd9Sstevel@tonic-gate 		ret = ddi_get8(global->io_handle,
12867c478bd9Sstevel@tonic-gate 		    global->io_addr + I8042_DATA);
12877c478bd9Sstevel@tonic-gate 		switch (port->which) {
12887c478bd9Sstevel@tonic-gate 		case MAIN_PORT:
12897c478bd9Sstevel@tonic-gate 			if ((stat & I8042_STAT_AUXBF) == 0)
12907c478bd9Sstevel@tonic-gate 				return (ret);
12917c478bd9Sstevel@tonic-gate 			break;
12927c478bd9Sstevel@tonic-gate 		case AUX_PORT:
12937c478bd9Sstevel@tonic-gate 			if ((stat & I8042_STAT_AUXBF) != 0)
12947c478bd9Sstevel@tonic-gate 				return (ret);
12957c478bd9Sstevel@tonic-gate 			break;
12967c478bd9Sstevel@tonic-gate 		}
12977c478bd9Sstevel@tonic-gate #if defined(DEBUG)
12987c478bd9Sstevel@tonic-gate 		prom_printf("I8042_POLL_INPUT_DATA:  data for wrong port!\n");
12997c478bd9Sstevel@tonic-gate #endif
13007c478bd9Sstevel@tonic-gate 		return (0);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	default:
13037c478bd9Sstevel@tonic-gate #if defined(DEBUG)
13047c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i8042:  read of undefined register 0x%p",
13057c478bd9Sstevel@tonic-gate 		    (void *)addr);
13067c478bd9Sstevel@tonic-gate #endif
13077c478bd9Sstevel@tonic-gate 		ret = 0;
13087c478bd9Sstevel@tonic-gate 		break;
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 	return (ret);
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
1313bb2d7d5eSSeth Goldberg /*
1314*fbac6366SSeth Goldberg  * Send the byte specified and wait for a reply -- either the retry response,
1315*fbac6366SSeth Goldberg  * or another response (assumed to be an acknowledgement).  If the retry
1316*fbac6366SSeth Goldberg  * response is received within the timeout period, the initial byte is resent
1317*fbac6366SSeth Goldberg  * to the 8042.
1318*fbac6366SSeth Goldberg  */
1319*fbac6366SSeth Goldberg static int
1320*fbac6366SSeth Goldberg i8042_do_intercept(ddi_acc_impl_t *handlep, struct i8042_port *port,
1321*fbac6366SSeth Goldberg     uint8_t *oaddr, uint8_t byte, uint8_t retry_response)
1322*fbac6366SSeth Goldberg {
1323*fbac6366SSeth Goldberg 	int 	timedout = 0;
1324*fbac6366SSeth Goldberg 	clock_t	tval;
1325*fbac6366SSeth Goldberg 
1326*fbac6366SSeth Goldberg 	/*
1327*fbac6366SSeth Goldberg 	 * Intercept the command response so that the 8042 interrupt handler
1328*fbac6366SSeth Goldberg 	 * does not call the port's interrupt handler.
1329*fbac6366SSeth Goldberg 	 */
1330*fbac6366SSeth Goldberg 	port->intercept_complete = B_FALSE;
1331*fbac6366SSeth Goldberg 	port->intr_intercept_enabled = B_TRUE;
1332*fbac6366SSeth Goldberg 
1333*fbac6366SSeth Goldberg 	/* Maximum time to wait: */
1334*fbac6366SSeth Goldberg 	tval = ddi_get_lbolt() + drv_usectohz(MAX_WAIT_ITERATIONS *
1335*fbac6366SSeth Goldberg 	    USECS_PER_WAIT);
1336*fbac6366SSeth Goldberg 
1337*fbac6366SSeth Goldberg 	do {
1338*fbac6366SSeth Goldberg 		i8042_put8_nolock(handlep, oaddr, byte);
1339*fbac6366SSeth Goldberg 
1340*fbac6366SSeth Goldberg 		/*
1341*fbac6366SSeth Goldberg 		 * Wait for the command response
1342*fbac6366SSeth Goldberg 		 */
1343*fbac6366SSeth Goldberg 		while (!port->intercept_complete) {
1344*fbac6366SSeth Goldberg 			if (cv_timedwait(&port->intercept_cv,
1345*fbac6366SSeth Goldberg 			    &port->intercept_mutex, tval) < 0 &&
1346*fbac6366SSeth Goldberg 			    !port->intercept_complete) {
1347*fbac6366SSeth Goldberg 				timedout = 1;
1348*fbac6366SSeth Goldberg 				break;
1349*fbac6366SSeth Goldberg 			}
1350*fbac6366SSeth Goldberg 		}
1351*fbac6366SSeth Goldberg 
1352*fbac6366SSeth Goldberg 		/*
1353*fbac6366SSeth Goldberg 		 * If the intercepted byte is the retry response, keep retrying
1354*fbac6366SSeth Goldberg 		 * until we time out, or until the success response is received.
1355*fbac6366SSeth Goldberg 		 */
1356*fbac6366SSeth Goldberg 		if (port->intercept_complete &&
1357*fbac6366SSeth Goldberg 		    port->intercepted_byte == retry_response)
1358*fbac6366SSeth Goldberg 			port->intercept_complete = B_FALSE;
1359*fbac6366SSeth Goldberg 
1360*fbac6366SSeth Goldberg 	} while (!timedout && !port->intercept_complete);
1361*fbac6366SSeth Goldberg 
1362*fbac6366SSeth Goldberg 	port->intr_intercept_enabled = B_FALSE;
1363*fbac6366SSeth Goldberg 
1364*fbac6366SSeth Goldberg 	return (timedout);
1365*fbac6366SSeth Goldberg }
1366*fbac6366SSeth Goldberg 
1367*fbac6366SSeth Goldberg /*
1368bb2d7d5eSSeth Goldberg  * The _rep_put8() operation is designed to allow child drivers to
1369bb2d7d5eSSeth Goldberg  * execute commands that have responses or that have responses plus an
1370bb2d7d5eSSeth Goldberg  * option byte.  These commands need to be executed atomically with respect
1371bb2d7d5eSSeth Goldberg  * to commands from other children (some 8042 implementations get confused
1372bb2d7d5eSSeth Goldberg  * when other child devices intersperse their commands while a command
1373bb2d7d5eSSeth Goldberg  * to a different 8042-connected device is in flight).
1374bb2d7d5eSSeth Goldberg  *
1375*fbac6366SSeth Goldberg  * haddr points to a buffer with either 3 or 4 bytes.  Three bytes if a
1376*fbac6366SSeth Goldberg  * command (byte 0) is being sent for which we expect a response code (byte 1)
1377*fbac6366SSeth Goldberg  * (this function blocks until we either read a response code (or the retry
1378*fbac6366SSeth Goldberg  * code (byte 2)) or until a timer expires).
1379*fbac6366SSeth Goldberg  * Four if the command (byte 0) requires a response (byte 1) and then an
1380*fbac6366SSeth Goldberg  * option byte (byte 3).  The option byte is only sent iff the response code
1381*fbac6366SSeth Goldberg  * expected is received before the timeout.  As with the 3-byte request, byte
1382*fbac6366SSeth Goldberg  * 2 is the retry response.
1383bb2d7d5eSSeth Goldberg  *
1384bb2d7d5eSSeth Goldberg  * While this function may technically called in interrupt context, it may
1385bb2d7d5eSSeth Goldberg  * block (depending on the IPL of the i8042 interrupt handler vs. the handler
1386bb2d7d5eSSeth Goldberg  * executing) for as long as the timeout (and fail if i8042_intr cannot run).
1387bb2d7d5eSSeth Goldberg  *
1388bb2d7d5eSSeth Goldberg  * flags are ignored.
1389bb2d7d5eSSeth Goldberg  *
1390bb2d7d5eSSeth Goldberg  */
1391bb2d7d5eSSeth Goldberg /*ARGSUSED*/
13927c478bd9Sstevel@tonic-gate static void
1393bb2d7d5eSSeth Goldberg i8042_rep_put8(ddi_acc_impl_t *handlep, uint8_t *haddr, uint8_t *daddr,
1394bb2d7d5eSSeth Goldberg     size_t repcount, uint_t flags)
1395bb2d7d5eSSeth Goldberg {
1396bb2d7d5eSSeth Goldberg 	struct i8042_port	*port;
1397bb2d7d5eSSeth Goldberg 	struct i8042		*global;
1398bb2d7d5eSSeth Goldberg 	uint8_t			*oaddr;
1399bb2d7d5eSSeth Goldberg 	uintptr_t		devaddr = (uintptr_t)daddr;
1400bb2d7d5eSSeth Goldberg 	int			timedout = 0;
1401bb2d7d5eSSeth Goldberg 	boolean_t		polled;
1402bb2d7d5eSSeth Goldberg 	ddi_acc_hdl_t		*h;
1403bb2d7d5eSSeth Goldberg 
1404bb2d7d5eSSeth Goldberg 	h = (ddi_acc_hdl_t *)handlep;
1405bb2d7d5eSSeth Goldberg 
1406bb2d7d5eSSeth Goldberg 	port = (struct i8042_port *)h->ah_bus_private;
1407bb2d7d5eSSeth Goldberg 	global = port->i8042_global;
1408bb2d7d5eSSeth Goldberg 
1409bb2d7d5eSSeth Goldberg 	/*
1410bb2d7d5eSSeth Goldberg 	 * If this function is called, somehow, while we're in i8042_intr,
1411bb2d7d5eSSeth Goldberg 	 * the logic below will not work.  That situation should never be
1412bb2d7d5eSSeth Goldberg 	 * possible.
1413bb2d7d5eSSeth Goldberg 	 */
1414bb2d7d5eSSeth Goldberg 	ASSERT(global->intr_thread != curthread);
1415bb2d7d5eSSeth Goldberg 
1416bb2d7d5eSSeth Goldberg 	/*
1417bb2d7d5eSSeth Goldberg 	 * Only support the main port for now
1418bb2d7d5eSSeth Goldberg 	 */
1419bb2d7d5eSSeth Goldberg 	if (port->which != MAIN_PORT || (devaddr != I8042_INT_CMD_PLUS_PARAM &&
1420bb2d7d5eSSeth Goldberg 	    devaddr != I8042_POLL_CMD_PLUS_PARAM)) {
1421bb2d7d5eSSeth Goldberg #ifdef DEBUG
1422bb2d7d5eSSeth Goldberg 		prom_printf("WARNING: i8042_rep_put8(): port or address "
1423bb2d7d5eSSeth Goldberg 		    "invalid\n");
1424bb2d7d5eSSeth Goldberg #endif
1425bb2d7d5eSSeth Goldberg 		return;
1426bb2d7d5eSSeth Goldberg 	}
1427bb2d7d5eSSeth Goldberg 
1428bb2d7d5eSSeth Goldberg 	/*
1429bb2d7d5eSSeth Goldberg 	 * Only support commands with MAX one parameter.  The format of the
1430bb2d7d5eSSeth Goldberg 	 * buffer supplied must be { <CMD>, <CMD_OK_RESPONSE>[, <PARAMETER>] }
1431bb2d7d5eSSeth Goldberg 	 */
1432*fbac6366SSeth Goldberg 	if (repcount != 3 && repcount != 4) {
1433bb2d7d5eSSeth Goldberg #ifdef DEBUG
1434bb2d7d5eSSeth Goldberg 		prom_printf("WARNING: i8042_rep_put8(): Invalid repetition "
1435bb2d7d5eSSeth Goldberg 		    "count (%d)\n", (int)repcount);
1436bb2d7d5eSSeth Goldberg #endif
1437bb2d7d5eSSeth Goldberg 		return;
1438bb2d7d5eSSeth Goldberg 	}
1439bb2d7d5eSSeth Goldberg 
1440bb2d7d5eSSeth Goldberg 	polled = (devaddr == I8042_POLL_CMD_PLUS_PARAM);
1441bb2d7d5eSSeth Goldberg 
1442bb2d7d5eSSeth Goldberg 	if (polled) {
1443bb2d7d5eSSeth Goldberg 		oaddr = (uint8_t *)I8042_POLL_OUTPUT_DATA;
1444bb2d7d5eSSeth Goldberg 	} else {
1445bb2d7d5eSSeth Goldberg 		oaddr = (uint8_t *)I8042_INT_OUTPUT_DATA;
1446bb2d7d5eSSeth Goldberg 		/*
1447bb2d7d5eSSeth Goldberg 		 * Mutexes are only required for the non-polled (polled
1448bb2d7d5eSSeth Goldberg 		 * via the virtual registers, NOT via the polling mechanism
1449bb2d7d5eSSeth Goldberg 		 * used for systems without 8042 interrupts) case, because
1450bb2d7d5eSSeth Goldberg 		 * when polling is used, the system is single-threaded
1451bb2d7d5eSSeth Goldberg 		 * with interrupts disabled.
1452bb2d7d5eSSeth Goldberg 		 */
1453bb2d7d5eSSeth Goldberg 		mutex_enter(&global->i8042_out_mutex);
1454bb2d7d5eSSeth Goldberg 	}
1455bb2d7d5eSSeth Goldberg 
1456*fbac6366SSeth Goldberg 	/* Initialize the response and retry bytes from the caller */
1457*fbac6366SSeth Goldberg 	port->intercept[0] = haddr[1];
1458*fbac6366SSeth Goldberg 	port->intercept[1] = haddr[2];
1459*fbac6366SSeth Goldberg 
1460bb2d7d5eSSeth Goldberg 	mutex_enter(&port->intercept_mutex);
1461bb2d7d5eSSeth Goldberg 
1462*fbac6366SSeth Goldberg 	timedout = i8042_do_intercept(handlep, port, oaddr, haddr[0], haddr[2]);
1463bb2d7d5eSSeth Goldberg 
1464bb2d7d5eSSeth Goldberg 	/*
1465*fbac6366SSeth Goldberg 	 * If the first byte was processed before the timeout period, and
1466*fbac6366SSeth Goldberg 	 * there's an option byte, send it now.
1467bb2d7d5eSSeth Goldberg 	 */
1468*fbac6366SSeth Goldberg 	if (!timedout && repcount == 4) {
1469*fbac6366SSeth Goldberg 		timedout = i8042_do_intercept(handlep, port, oaddr, haddr[3],
1470*fbac6366SSeth Goldberg 		    haddr[2]);
1471bb2d7d5eSSeth Goldberg 	}
1472bb2d7d5eSSeth Goldberg 
1473bb2d7d5eSSeth Goldberg 	mutex_exit(&port->intercept_mutex);
1474bb2d7d5eSSeth Goldberg 
1475bb2d7d5eSSeth Goldberg #ifdef DEBUG
1476*fbac6366SSeth Goldberg 	if (timedout && i8042_debug)
1477bb2d7d5eSSeth Goldberg 		prom_printf("WARNING: i8042_rep_put8(): timed out waiting for "
1478bb2d7d5eSSeth Goldberg 		    "command response\n");
1479bb2d7d5eSSeth Goldberg #endif
1480bb2d7d5eSSeth Goldberg 
1481bb2d7d5eSSeth Goldberg 	if (!polled)
1482bb2d7d5eSSeth Goldberg 		mutex_exit(&global->i8042_out_mutex);
1483bb2d7d5eSSeth Goldberg }
1484bb2d7d5eSSeth Goldberg 
1485bb2d7d5eSSeth Goldberg static void
1486bb2d7d5eSSeth Goldberg i8042_put8_nolock(ddi_acc_impl_t *handlep, uint8_t *addr, uint8_t value)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	struct i8042_port *port;
14897c478bd9Sstevel@tonic-gate 	struct i8042 *global;
14907c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t	*h;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	h = (ddi_acc_hdl_t *)handlep;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	port = (struct i8042_port *)h->ah_bus_private;
14957c478bd9Sstevel@tonic-gate 	global = port->i8042_global;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	switch ((uintptr_t)addr) {
14987c478bd9Sstevel@tonic-gate 	case I8042_INT_OUTPUT_DATA:
14997c478bd9Sstevel@tonic-gate 	case I8042_POLL_OUTPUT_DATA:
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 		if (port->which == AUX_PORT)
15027c478bd9Sstevel@tonic-gate 			i8042_send(global, I8042_CMD, I8042_CMD_WRITE_AUX);
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		i8042_send(global, I8042_DATA, value);
15057c478bd9Sstevel@tonic-gate 
1506bb2d7d5eSSeth Goldberg 		break;
1507bb2d7d5eSSeth Goldberg 	}
1508bb2d7d5eSSeth Goldberg }
1509bb2d7d5eSSeth Goldberg 
1510bb2d7d5eSSeth Goldberg static void
1511bb2d7d5eSSeth Goldberg i8042_put8(ddi_acc_impl_t *handlep, uint8_t *addr, uint8_t value)
1512bb2d7d5eSSeth Goldberg {
1513bb2d7d5eSSeth Goldberg 	struct i8042 *global;
1514bb2d7d5eSSeth Goldberg 	ddi_acc_hdl_t	*h;
1515bb2d7d5eSSeth Goldberg 
1516bb2d7d5eSSeth Goldberg 	h = (ddi_acc_hdl_t *)handlep;
1517bb2d7d5eSSeth Goldberg 	global = ((struct i8042_port *)h->ah_bus_private)->i8042_global;
1518bb2d7d5eSSeth Goldberg 
1519bb2d7d5eSSeth Goldberg 	switch ((uintptr_t)addr) {
1520bb2d7d5eSSeth Goldberg 	case I8042_INT_OUTPUT_DATA:
1521bb2d7d5eSSeth Goldberg 	case I8042_POLL_OUTPUT_DATA:
1522bb2d7d5eSSeth Goldberg 
1523bb2d7d5eSSeth Goldberg 		if ((uintptr_t)addr == I8042_INT_OUTPUT_DATA)
1524bb2d7d5eSSeth Goldberg 			mutex_enter(&global->i8042_out_mutex);
1525bb2d7d5eSSeth Goldberg 
1526bb2d7d5eSSeth Goldberg 		i8042_put8_nolock(handlep, addr, value);
1527bb2d7d5eSSeth Goldberg 
15287c478bd9Sstevel@tonic-gate 		if ((uintptr_t)addr == I8042_INT_OUTPUT_DATA)
15297c478bd9Sstevel@tonic-gate 			mutex_exit(&global->i8042_out_mutex);
1530bb2d7d5eSSeth Goldberg 
15317c478bd9Sstevel@tonic-gate 		break;
15327c478bd9Sstevel@tonic-gate 
1533bb2d7d5eSSeth Goldberg 	case I8042_INT_CMD_PLUS_PARAM:
1534bb2d7d5eSSeth Goldberg 	case I8042_POLL_CMD_PLUS_PARAM:
1535bb2d7d5eSSeth Goldberg 
1536bb2d7d5eSSeth Goldberg 		break;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate #if defined(DEBUG)
15397c478bd9Sstevel@tonic-gate 	case I8042_INT_INPUT_AVAIL:
15407c478bd9Sstevel@tonic-gate 	case I8042_INT_INPUT_DATA:
15417c478bd9Sstevel@tonic-gate 	case I8042_POLL_INPUT_AVAIL:
15427c478bd9Sstevel@tonic-gate 	case I8042_POLL_INPUT_DATA:
15437c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i8042:  write of read-only register 0x%p",
15447c478bd9Sstevel@tonic-gate 		    (void *)addr);
15457c478bd9Sstevel@tonic-gate 		break;
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	default:
15487c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i8042:  read of undefined register 0x%p",
15497c478bd9Sstevel@tonic-gate 		    (void *)addr);
15507c478bd9Sstevel@tonic-gate 		break;
15517c478bd9Sstevel@tonic-gate #endif
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate }
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate /* ARGSUSED */
15577c478bd9Sstevel@tonic-gate static int
15587c478bd9Sstevel@tonic-gate i8042_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
15597c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
15607c478bd9Sstevel@tonic-gate {
15617c478bd9Sstevel@tonic-gate 	struct i8042_port *port;
15627c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
15637c478bd9Sstevel@tonic-gate 	struct i8042	*global;
15647c478bd9Sstevel@tonic-gate 	int		ret;
15657c478bd9Sstevel@tonic-gate #endif
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	switch (intr_op) {
15687c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SUPPORTED_TYPES:
15697c478bd9Sstevel@tonic-gate 		*(int *)result = DDI_INTR_TYPE_FIXED;
15707c478bd9Sstevel@tonic-gate 		break;
15717c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
15727c478bd9Sstevel@tonic-gate 		if (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result)
15737c478bd9Sstevel@tonic-gate 		    == DDI_FAILURE)
15747c478bd9Sstevel@tonic-gate 			*(int *)result = 0;
15757c478bd9Sstevel@tonic-gate 		break;
15767c478bd9Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
1577a54f81fbSanish 	case DDI_INTROP_NAVAIL:
15787c478bd9Sstevel@tonic-gate 		*(int *)result = 1;
15797c478bd9Sstevel@tonic-gate 		break;
15807c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
15817c478bd9Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
15827c478bd9Sstevel@tonic-gate 		break;
15837c478bd9Sstevel@tonic-gate 	case DDI_INTROP_FREE:
15847c478bd9Sstevel@tonic-gate 		break;
15857c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
15867c478bd9Sstevel@tonic-gate 		/* Hard coding it for x86 */
15877c478bd9Sstevel@tonic-gate 		*(int *)result = 5;
15887c478bd9Sstevel@tonic-gate 		break;
15897c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
15907c478bd9Sstevel@tonic-gate 		port = ddi_get_parent_data(rdip);
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
15937c478bd9Sstevel@tonic-gate 		global = port->i8042_global;
15947c478bd9Sstevel@tonic-gate 		ret = ddi_intr_add_softint(rdip, &port->soft_hdl,
15957c478bd9Sstevel@tonic-gate 		    I8042_SOFTINT_PRI, hdlp->ih_cb_func, hdlp->ih_cb_arg1);
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS) {
15987c478bd9Sstevel@tonic-gate #if defined(DEBUG)
15997c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s #%d:  "
16007c478bd9Sstevel@tonic-gate 			    "Cannot add soft interrupt for %s #%d, ret=%d.",
16017c478bd9Sstevel@tonic-gate 			    DRIVER_NAME(dip), ddi_get_instance(dip),
16027c478bd9Sstevel@tonic-gate 			    DRIVER_NAME(rdip), ddi_get_instance(rdip), ret);
16037c478bd9Sstevel@tonic-gate #endif	/* defined(DEBUG) */
16047c478bd9Sstevel@tonic-gate 			return (ret);
16057c478bd9Sstevel@tonic-gate 		}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate #else	/* defined(USE_SOFT_INTRS) */
16087c478bd9Sstevel@tonic-gate 		mutex_enter(&port->intr_mutex);
16097c478bd9Sstevel@tonic-gate 		port->intr_func = hdlp->ih_cb_func;
16107c478bd9Sstevel@tonic-gate 		port->intr_arg1 = hdlp->ih_cb_arg1;
16117c478bd9Sstevel@tonic-gate 		port->intr_arg2 = hdlp->ih_cb_arg2;
16127c478bd9Sstevel@tonic-gate 		mutex_exit(&port->intr_mutex);
16137c478bd9Sstevel@tonic-gate #endif	/* defined(USE_SOFT_INTRS) */
16147c478bd9Sstevel@tonic-gate 		break;
16157c478bd9Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
16167c478bd9Sstevel@tonic-gate 		port = ddi_get_parent_data(rdip);
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
16197c478bd9Sstevel@tonic-gate 		global = port->i8042_global;
16207c478bd9Sstevel@tonic-gate 		mutex_enter(&global->i8042_mutex);
16217c478bd9Sstevel@tonic-gate 		port->soft_hdl = 0;
16227c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
16237c478bd9Sstevel@tonic-gate #else	/* defined(USE_SOFT_INTRS) */
16247c478bd9Sstevel@tonic-gate 		mutex_enter(&port->intr_mutex);
16257c478bd9Sstevel@tonic-gate 		port->intr_func = NULL;
16267c478bd9Sstevel@tonic-gate 		mutex_exit(&port->intr_mutex);
16277c478bd9Sstevel@tonic-gate #endif	/* defined(USE_SOFT_INTRS) */
16287c478bd9Sstevel@tonic-gate 		break;
16297c478bd9Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
16307c478bd9Sstevel@tonic-gate 		port = ddi_get_parent_data(rdip);
16317c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
16327c478bd9Sstevel@tonic-gate 		global = port->i8042_global;
16337c478bd9Sstevel@tonic-gate 		mutex_enter(&global->i8042_mutex);
16347c478bd9Sstevel@tonic-gate 		port->soft_intr_enabled = B_TRUE;
16357c478bd9Sstevel@tonic-gate 		if (port->wptr != port->rptr)
1636fd9cb95cSsethg 			(void) ddi_intr_trigger_softint(port->soft_hdl,
1637fd9cb95cSsethg 			    port->intr_arg2);
16387c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
16397c478bd9Sstevel@tonic-gate #else	/* defined(USE_SOFT_INTRS) */
16407c478bd9Sstevel@tonic-gate 		mutex_enter(&port->intr_mutex);
16417c478bd9Sstevel@tonic-gate 		if (port->wptr != port->rptr)
16427c478bd9Sstevel@tonic-gate 			port->intr_func(port->intr_arg1, port->intr_arg2);
16437c478bd9Sstevel@tonic-gate 		mutex_exit(&port->intr_mutex);
16447c478bd9Sstevel@tonic-gate #endif	/* defined(USE_SOFT_INTRS) */
16457c478bd9Sstevel@tonic-gate 		break;
16467c478bd9Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
16477c478bd9Sstevel@tonic-gate #if defined(USE_SOFT_INTRS)
16487c478bd9Sstevel@tonic-gate 		port = ddi_get_parent_data(rdip);
16497c478bd9Sstevel@tonic-gate 		global = port->i8042_global;
16507c478bd9Sstevel@tonic-gate 		mutex_enter(&global->i8042_mutex);
16517c478bd9Sstevel@tonic-gate 		port->soft_intr_enabled = B_FALSE;
1652fd9cb95cSsethg 		(void) ddi_intr_remove_softint(port->soft_hdl);
16537c478bd9Sstevel@tonic-gate 		mutex_exit(&global->i8042_mutex);
16547c478bd9Sstevel@tonic-gate #endif	/* defined(USE_SOFT_INTRS) */
16557c478bd9Sstevel@tonic-gate 		break;
16567c478bd9Sstevel@tonic-gate 	default:
16577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
16617c478bd9Sstevel@tonic-gate }
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate static int
16647c478bd9Sstevel@tonic-gate i8042_ctlops(dev_info_t *dip, dev_info_t *rdip,
16657c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result)
16667c478bd9Sstevel@tonic-gate {
16677c478bd9Sstevel@tonic-gate 	int	*iprop;
16687c478bd9Sstevel@tonic-gate 	unsigned int	iprop_len;
16697c478bd9Sstevel@tonic-gate 	int	which_port;
16707c478bd9Sstevel@tonic-gate 	char	name[16];
16717c478bd9Sstevel@tonic-gate 	struct i8042	*global;
16727c478bd9Sstevel@tonic-gate 	dev_info_t	*child;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	global = ddi_get_driver_private(dip);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	switch (op) {
16777c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
16787c478bd9Sstevel@tonic-gate 		child = (dev_info_t *)arg;
16797c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
16807c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", &iprop, &iprop_len) !=
16817c478bd9Sstevel@tonic-gate 		    DDI_SUCCESS) {
16827c478bd9Sstevel@tonic-gate #if defined(DEBUG)
16837c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s #%d:  Missing 'reg' on %s@???",
16847c478bd9Sstevel@tonic-gate 			    DRIVER_NAME(dip), ddi_get_instance(dip),
16857c478bd9Sstevel@tonic-gate 			    ddi_node_name(child));
16867c478bd9Sstevel@tonic-gate #endif
16877c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
16887c478bd9Sstevel@tonic-gate 		}
16897c478bd9Sstevel@tonic-gate 		which_port = iprop[0];
16907c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)iprop);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%d", which_port);
16937c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
16947c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child,
16957c478bd9Sstevel@tonic-gate 		    (caddr_t)&global->i8042_ports[which_port]);
16967c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
16997c478bd9Sstevel@tonic-gate 		child = (dev_info_t *)arg;
17007c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
17017c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
17027c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
17057c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?8042 device:  %s@%s, %s # %d\n",
17067c478bd9Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
17077c478bd9Sstevel@tonic-gate 		    DRIVER_NAME(rdip), ddi_get_instance(rdip));
17087c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	default:
17117c478bd9Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, op, arg, result));
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
17147c478bd9Sstevel@tonic-gate }
17157c478bd9Sstevel@tonic-gate 
1716fd9cb95cSsethg #if defined(__i386) || defined(__amd64)
1717fd9cb95cSsethg static dev_info_t *
1718fd9cb95cSsethg i8042_devi_findchild_by_node_name(dev_info_t *pdip, char *nodename)
17197c478bd9Sstevel@tonic-gate {
1720fd9cb95cSsethg 	dev_info_t *child;
17217c478bd9Sstevel@tonic-gate 
1722fd9cb95cSsethg 	ASSERT(DEVI_BUSY_OWNED(pdip));
17237c478bd9Sstevel@tonic-gate 
1724fd9cb95cSsethg 	if (nodename == NULL) {
1725fd9cb95cSsethg 		return ((dev_info_t *)NULL);
17267c478bd9Sstevel@tonic-gate 	}
1727fd9cb95cSsethg 
1728fd9cb95cSsethg 	for (child = ddi_get_child(pdip); child != NULL;
1729fd9cb95cSsethg 	    child = ddi_get_next_sibling(child)) {
1730fd9cb95cSsethg 
1731fd9cb95cSsethg 		if (strcmp(ddi_node_name(child), nodename) == 0)
1732fd9cb95cSsethg 			break;
1733fd9cb95cSsethg 	}
1734fd9cb95cSsethg 	return (child);
1735fd9cb95cSsethg }
1736fd9cb95cSsethg 
1737fd9cb95cSsethg static void
1738fd9cb95cSsethg alloc_kb_mouse(dev_info_t *i8042_dip, int nodes_needed)
1739fd9cb95cSsethg {
1740fd9cb95cSsethg 	dev_info_t *xdip;
1741fd9cb95cSsethg 	int acpi_off = 0;
1742fd9cb95cSsethg 	char *acpi_prop;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	/* don't alloc unless acpi is off */
17457c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
17467c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "acpi-enum", &acpi_prop) == DDI_PROP_SUCCESS) {
17477c478bd9Sstevel@tonic-gate 		if (strcmp("off", acpi_prop) == 0) {
17487c478bd9Sstevel@tonic-gate 			acpi_off = 1;
17497c478bd9Sstevel@tonic-gate 		}
17507c478bd9Sstevel@tonic-gate 		ddi_prop_free(acpi_prop);
17517c478bd9Sstevel@tonic-gate 	}
17527c478bd9Sstevel@tonic-gate 	if (acpi_off == 0) {
17537c478bd9Sstevel@tonic-gate 		return;
17547c478bd9Sstevel@tonic-gate 	}
17557c478bd9Sstevel@tonic-gate 
1756fd9cb95cSsethg 	if (nodes_needed & I8042_MOUSE) {
17577c478bd9Sstevel@tonic-gate 		/* mouse */
17587c478bd9Sstevel@tonic-gate 		ndi_devi_alloc_sleep(i8042_dip, "mouse",
1759fa9e4066Sahrens 		    (pnode_t)DEVI_SID_NODEID, &xdip);
17607c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
17617c478bd9Sstevel@tonic-gate 		    "reg", 1);
1762fd9cb95cSsethg 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
1763fd9cb95cSsethg 		    "interrupts", 2);
17647c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
17657c478bd9Sstevel@tonic-gate 		    "compatible", "pnpPNP,f03");
1766fd9cb95cSsethg 		/*
1767fd9cb95cSsethg 		 * The device_type property does not matter on SPARC.  Retain it
1768fd9cb95cSsethg 		 * on x86 for compatibility with the previous pseudo-prom.
1769fd9cb95cSsethg 		 */
17707c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
1771fd9cb95cSsethg 		    "device_type", "mouse");
17727c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(xdip, 0);
1773fd9cb95cSsethg 	}
17747c478bd9Sstevel@tonic-gate 
1775fd9cb95cSsethg 	if (nodes_needed & I8042_KEYBOARD) {
17767c478bd9Sstevel@tonic-gate 		/* keyboard */
17777c478bd9Sstevel@tonic-gate 		ndi_devi_alloc_sleep(i8042_dip, "keyboard",
1778fa9e4066Sahrens 		    (pnode_t)DEVI_SID_NODEID, &xdip);
17797c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
17807c478bd9Sstevel@tonic-gate 		    "reg", 0);
1781fd9cb95cSsethg 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, xdip,
1782fd9cb95cSsethg 		    "interrupts", 1);
17837c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
17847c478bd9Sstevel@tonic-gate 		    "compatible", "pnpPNP,303");
17857c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, xdip,
1786fd9cb95cSsethg 		    "device_type", "keyboard");
17877c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(xdip, 0);
17887c478bd9Sstevel@tonic-gate 	}
1789fd9cb95cSsethg }
1790fd9cb95cSsethg #endif
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate static int
17937c478bd9Sstevel@tonic-gate i8042_bus_config(dev_info_t *parent, uint_t flags,
17947c478bd9Sstevel@tonic-gate     ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
17957c478bd9Sstevel@tonic-gate {
1796fd9cb95cSsethg #if defined(__i386) || defined(__amd64)
1797fd9cb95cSsethg 	int nodes_needed = 0;
1798fd9cb95cSsethg 	int circ;
17997c478bd9Sstevel@tonic-gate 
1800fd9cb95cSsethg 	/*
1801fd9cb95cSsethg 	 * On x86 systems, if ACPI is disabled, the only way the
1802fd9cb95cSsethg 	 * keyboard and mouse can be enumerated is by creating them
1803fd9cb95cSsethg 	 * manually.  The following code searches for the existence of
1804fd9cb95cSsethg 	 * the keyboard and mouse nodes and creates them if they are not
1805fd9cb95cSsethg 	 * found.
1806fd9cb95cSsethg 	 */
1807fd9cb95cSsethg 	ndi_devi_enter(parent, &circ);
1808fd9cb95cSsethg 	if (i8042_devi_findchild_by_node_name(parent, "keyboard") == NULL)
1809fd9cb95cSsethg 		nodes_needed |= I8042_KEYBOARD;
1810fd9cb95cSsethg 	if (i8042_devi_findchild_by_node_name(parent, "mouse") == NULL)
1811fd9cb95cSsethg 		nodes_needed |= I8042_MOUSE;
1812fd9cb95cSsethg 
1813fd9cb95cSsethg 	/* If the mouse and keyboard nodes do not already exist, create them */
1814fd9cb95cSsethg 	if (nodes_needed)
1815fd9cb95cSsethg 		alloc_kb_mouse(parent, nodes_needed);
1816fd9cb95cSsethg 	ndi_devi_exit(parent, circ);
1817fd9cb95cSsethg #endif
18187c478bd9Sstevel@tonic-gate 	return (ndi_busop_bus_config(parent, flags, op, arg, childp, 0));
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate static int
18227c478bd9Sstevel@tonic-gate i8042_bus_unconfig(dev_info_t *parent, uint_t flags,
18237c478bd9Sstevel@tonic-gate     ddi_bus_config_op_t op, void *arg)
18247c478bd9Sstevel@tonic-gate {
1825fd9cb95cSsethg 	/*
1826fd9cb95cSsethg 	 * The NDI_UNCONFIG flag allows the reference count on this nexus to be
1827fd9cb95cSsethg 	 * decremented when children's drivers are unloaded, enabling the nexus
1828fd9cb95cSsethg 	 * itself to be unloaded.
1829fd9cb95cSsethg 	 */
1830fd9cb95cSsethg 	return (ndi_busop_bus_unconfig(parent, flags | NDI_UNCONFIG, op, arg));
18317c478bd9Sstevel@tonic-gate }
1832fd9cb95cSsethg 
1833fd9cb95cSsethg #ifdef __sparc
1834fd9cb95cSsethg static int
1835fd9cb95cSsethg i8042_build_interrupts_property(dev_info_t *dip)
1836fd9cb95cSsethg {
1837fd9cb95cSsethg 	dev_info_t *child = ddi_get_child(dip);
1838fd9cb95cSsethg 	uint_t nintr;
1839fd9cb95cSsethg 	int *intrs = NULL;
1840fd9cb95cSsethg 	int interrupts[MAX_INTERRUPTS];
1841fd9cb95cSsethg 	int i = 0;
1842fd9cb95cSsethg 
1843fd9cb95cSsethg 	/* Walk the children of this node, scanning for interrupts properties */
1844fd9cb95cSsethg 	while (child != NULL && i < MAX_INTERRUPTS) {
1845fd9cb95cSsethg 
1846fd9cb95cSsethg 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
1847fd9cb95cSsethg 		    DDI_PROP_DONTPASS, "interrupts", &intrs, &nintr)
1848fd9cb95cSsethg 		    == DDI_PROP_SUCCESS && intrs != NULL) {
1849fd9cb95cSsethg 
1850fd9cb95cSsethg 			while (nintr > 0 && i < MAX_INTERRUPTS) {
1851fd9cb95cSsethg 				interrupts[i++] = intrs[--nintr];
1852fd9cb95cSsethg 			}
1853fd9cb95cSsethg 			ddi_prop_free(intrs);
1854fd9cb95cSsethg 		}
1855fd9cb95cSsethg 
1856fd9cb95cSsethg 		child = ddi_get_next_sibling(child);
1857fd9cb95cSsethg 	}
1858fd9cb95cSsethg 
1859fd9cb95cSsethg 	if (ddi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts",
1860fd9cb95cSsethg 	    interrupts, i) != DDI_PROP_SUCCESS) {
1861fd9cb95cSsethg 
1862fd9cb95cSsethg 		return (DDI_FAILURE);
1863fd9cb95cSsethg 	}
1864fd9cb95cSsethg 
1865fd9cb95cSsethg 	/*
1866fd9cb95cSsethg 	 * Oh, the humanity. On the platforms on which we need to
1867fd9cb95cSsethg 	 * synthesize an interrupts property, we ALSO need to update the
1868fd9cb95cSsethg 	 * device_type property, and set it to "serial" in order for the
1869fd9cb95cSsethg 	 * correct interrupt PIL to be chosen by the framework.
1870fd9cb95cSsethg 	 */
1871fd9cb95cSsethg 	if (ddi_prop_update_string(DDI_DEV_T_NONE, dip, "device_type", "serial")
1872fd9cb95cSsethg 	    != DDI_PROP_SUCCESS) {
1873fd9cb95cSsethg 
1874fd9cb95cSsethg 		return (DDI_FAILURE);
1875fd9cb95cSsethg 	}
1876fd9cb95cSsethg 
1877fd9cb95cSsethg 	return (DDI_SUCCESS);
1878fd9cb95cSsethg }
1879fd9cb95cSsethg 
1880fd9cb95cSsethg static boolean_t
1881fd9cb95cSsethg i8042_is_polling_platform(void)
1882fd9cb95cSsethg {
1883fd9cb95cSsethg 	/*
1884fd9cb95cSsethg 	 * Returns true if this platform is one of the platforms
1885fd9cb95cSsethg 	 * that has interrupt issues with the PS/2 keyboard/mouse.
1886fd9cb95cSsethg 	 */
1887fd9cb95cSsethg 	if (PLATFORM_MATCH("SUNW,UltraAX-"))
1888fd9cb95cSsethg 		return (B_TRUE);
1889fd9cb95cSsethg 	else
1890fd9cb95cSsethg 		return (B_FALSE);
1891fd9cb95cSsethg }
1892fd9cb95cSsethg #endif
1893bb2d7d5eSSeth Goldberg 
1894bb2d7d5eSSeth Goldberg /*
1895bb2d7d5eSSeth Goldberg  * arg1 is the global i8042 state pointer (not used)
1896bb2d7d5eSSeth Goldberg  * arg2 is the port pointer for the intercepted port
1897bb2d7d5eSSeth Goldberg  */
1898bb2d7d5eSSeth Goldberg /*ARGSUSED*/
1899bb2d7d5eSSeth Goldberg static uint_t
1900bb2d7d5eSSeth Goldberg i8042_intercept_softint(caddr_t arg1, caddr_t arg2)
1901bb2d7d5eSSeth Goldberg {
1902bb2d7d5eSSeth Goldberg 	struct i8042_port *port = (struct i8042_port *)arg2;
1903bb2d7d5eSSeth Goldberg 	ASSERT(port != NULL);
1904bb2d7d5eSSeth Goldberg 
1905bb2d7d5eSSeth Goldberg 	mutex_enter(&port->intercept_mutex);
1906bb2d7d5eSSeth Goldberg 	if (!port->intercept_complete) {
1907bb2d7d5eSSeth Goldberg 		port->intercept_complete = B_TRUE;
1908bb2d7d5eSSeth Goldberg 		cv_signal(&port->intercept_cv);
1909bb2d7d5eSSeth Goldberg 		mutex_exit(&port->intercept_mutex);
1910bb2d7d5eSSeth Goldberg 		return (DDI_INTR_CLAIMED);
1911bb2d7d5eSSeth Goldberg 	}
1912bb2d7d5eSSeth Goldberg 	mutex_exit(&port->intercept_mutex);
1913bb2d7d5eSSeth Goldberg 	return (DDI_INTR_UNCLAIMED);
1914bb2d7d5eSSeth Goldberg }
1915